2 * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/errno.h>
34 #include <linux/if_ether.h>
35 #include <linux/export.h>
37 #include <linux/mlx4/cmd.h>
41 #define MLX4_MAC_VALID (1ull << 63)
42 #define MLX4_MAC_MASK 0xffffffffffffULL
44 #define MLX4_VLAN_VALID (1u << 31)
45 #define MLX4_VLAN_MASK 0xfff
47 void mlx4_init_mac_table(struct mlx4_dev
*dev
, struct mlx4_mac_table
*table
)
51 mutex_init(&table
->mutex
);
52 for (i
= 0; i
< MLX4_MAX_MAC_NUM
; i
++) {
53 table
->entries
[i
] = 0;
56 table
->max
= 1 << dev
->caps
.log_num_macs
;
60 void mlx4_init_vlan_table(struct mlx4_dev
*dev
, struct mlx4_vlan_table
*table
)
64 mutex_init(&table
->mutex
);
65 for (i
= 0; i
< MLX4_MAX_VLAN_NUM
; i
++) {
66 table
->entries
[i
] = 0;
69 table
->max
= 1 << dev
->caps
.log_num_vlans
;
73 static int mlx4_set_port_mac_table(struct mlx4_dev
*dev
, u8 port
,
76 struct mlx4_cmd_mailbox
*mailbox
;
80 mailbox
= mlx4_alloc_cmd_mailbox(dev
);
82 return PTR_ERR(mailbox
);
84 memcpy(mailbox
->buf
, entries
, MLX4_MAC_TABLE_SIZE
);
86 in_mod
= MLX4_SET_PORT_MAC_TABLE
<< 8 | port
;
87 err
= mlx4_cmd(dev
, mailbox
->dma
, in_mod
, 1, MLX4_CMD_SET_PORT
,
88 MLX4_CMD_TIME_CLASS_B
);
90 mlx4_free_cmd_mailbox(dev
, mailbox
);
94 static int mlx4_uc_steer_add(struct mlx4_dev
*dev
, u8 port
,
95 u64 mac
, int *qpn
, u8 reserve
)
102 err
= mlx4_qp_reserve_range(dev
, 1, 1, qpn
);
104 mlx4_err(dev
, "Failed to reserve qp for mac registration\n");
110 mac
&= 0xffffffffffffULL
;
111 mac
= cpu_to_be64(mac
<< 16);
112 memcpy(&gid
[10], &mac
, ETH_ALEN
);
114 gid
[7] = MLX4_UC_STEER
<< 1;
116 err
= mlx4_qp_attach_common(dev
, &qp
, gid
, 0,
117 MLX4_PROT_ETH
, MLX4_UC_STEER
);
119 mlx4_qp_release_range(dev
, *qpn
, 1);
124 static void mlx4_uc_steer_release(struct mlx4_dev
*dev
, u8 port
,
125 u64 mac
, int qpn
, u8 free
)
131 mac
&= 0xffffffffffffULL
;
132 mac
= cpu_to_be64(mac
<< 16);
133 memcpy(&gid
[10], &mac
, ETH_ALEN
);
135 gid
[7] = MLX4_UC_STEER
<< 1;
137 mlx4_qp_detach_common(dev
, &qp
, gid
, MLX4_PROT_ETH
, MLX4_UC_STEER
);
139 mlx4_qp_release_range(dev
, qpn
, 1);
142 int mlx4_register_mac(struct mlx4_dev
*dev
, u8 port
, u64 mac
, int *qpn
, u8 wrap
)
144 struct mlx4_port_info
*info
= &mlx4_priv(dev
)->port
[port
];
145 struct mlx4_mac_table
*table
= &info
->mac_table
;
146 struct mlx4_mac_entry
*entry
;
150 if (dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_VEP_UC_STEER
) {
151 err
= mlx4_uc_steer_add(dev
, port
, mac
, qpn
, 1);
153 entry
= kmalloc(sizeof *entry
, GFP_KERNEL
);
155 mlx4_uc_steer_release(dev
, port
, mac
, *qpn
, 1);
159 err
= radix_tree_insert(&info
->mac_tree
, *qpn
, entry
);
161 mlx4_uc_steer_release(dev
, port
, mac
, *qpn
, 1);
167 mlx4_dbg(dev
, "Registering MAC: 0x%llx\n", (unsigned long long) mac
);
168 mutex_lock(&table
->mutex
);
169 for (i
= 0; i
< MLX4_MAX_MAC_NUM
- 1; i
++) {
170 if (free
< 0 && !table
->refs
[i
]) {
175 if (mac
== (MLX4_MAC_MASK
& be64_to_cpu(table
->entries
[i
]))) {
176 /* MAC already registered, increase references count */
187 mlx4_dbg(dev
, "Free MAC index is %d\n", free
);
189 if (table
->total
== table
->max
) {
190 /* No free mac entries */
195 /* Register new MAC */
196 table
->refs
[free
] = 1;
197 table
->entries
[free
] = cpu_to_be64(mac
| MLX4_MAC_VALID
);
199 err
= mlx4_set_port_mac_table(dev
, port
, table
->entries
);
201 mlx4_err(dev
, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac
);
202 table
->refs
[free
] = 0;
203 table
->entries
[free
] = 0;
207 if (!(dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_VEP_UC_STEER
))
208 *qpn
= info
->base_qpn
+ free
;
211 mutex_unlock(&table
->mutex
);
214 EXPORT_SYMBOL_GPL(mlx4_register_mac
);
216 static int validate_index(struct mlx4_dev
*dev
,
217 struct mlx4_mac_table
*table
, int index
)
221 if (index
< 0 || index
>= table
->max
|| !table
->entries
[index
]) {
222 mlx4_warn(dev
, "No valid Mac entry for the given index\n");
228 static int find_index(struct mlx4_dev
*dev
,
229 struct mlx4_mac_table
*table
, u64 mac
)
232 for (i
= 0; i
< MLX4_MAX_MAC_NUM
; i
++) {
233 if (mac
== (MLX4_MAC_MASK
& be64_to_cpu(table
->entries
[i
])))
240 void mlx4_unregister_mac(struct mlx4_dev
*dev
, u8 port
, int qpn
)
242 struct mlx4_port_info
*info
= &mlx4_priv(dev
)->port
[port
];
243 struct mlx4_mac_table
*table
= &info
->mac_table
;
244 int index
= qpn
- info
->base_qpn
;
245 struct mlx4_mac_entry
*entry
;
247 if (dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_VEP_UC_STEER
) {
248 entry
= radix_tree_lookup(&info
->mac_tree
, qpn
);
250 mlx4_uc_steer_release(dev
, port
, entry
->mac
, qpn
, 1);
251 radix_tree_delete(&info
->mac_tree
, qpn
);
252 index
= find_index(dev
, table
, entry
->mac
);
257 mutex_lock(&table
->mutex
);
259 if (validate_index(dev
, table
, index
))
262 /* Check whether this address has reference count */
263 if (!(--table
->refs
[index
])) {
264 table
->entries
[index
] = 0;
265 mlx4_set_port_mac_table(dev
, port
, table
->entries
);
269 mutex_unlock(&table
->mutex
);
271 EXPORT_SYMBOL_GPL(mlx4_unregister_mac
);
273 int mlx4_replace_mac(struct mlx4_dev
*dev
, u8 port
, int qpn
, u64 new_mac
, u8 wrap
)
275 struct mlx4_port_info
*info
= &mlx4_priv(dev
)->port
[port
];
276 struct mlx4_mac_table
*table
= &info
->mac_table
;
277 int index
= qpn
- info
->base_qpn
;
278 struct mlx4_mac_entry
*entry
;
281 if (dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_VEP_UC_STEER
) {
282 entry
= radix_tree_lookup(&info
->mac_tree
, qpn
);
285 index
= find_index(dev
, table
, entry
->mac
);
286 mlx4_uc_steer_release(dev
, port
, entry
->mac
, qpn
, 0);
287 entry
->mac
= new_mac
;
288 err
= mlx4_uc_steer_add(dev
, port
, entry
->mac
, &qpn
, 0);
289 if (err
|| index
< 0)
293 mutex_lock(&table
->mutex
);
295 err
= validate_index(dev
, table
, index
);
299 table
->entries
[index
] = cpu_to_be64(new_mac
| MLX4_MAC_VALID
);
301 err
= mlx4_set_port_mac_table(dev
, port
, table
->entries
);
303 mlx4_err(dev
, "Failed adding MAC: 0x%llx\n", (unsigned long long) new_mac
);
304 table
->entries
[index
] = 0;
307 mutex_unlock(&table
->mutex
);
310 EXPORT_SYMBOL_GPL(mlx4_replace_mac
);
311 static int mlx4_set_port_vlan_table(struct mlx4_dev
*dev
, u8 port
,
314 struct mlx4_cmd_mailbox
*mailbox
;
318 mailbox
= mlx4_alloc_cmd_mailbox(dev
);
320 return PTR_ERR(mailbox
);
322 memcpy(mailbox
->buf
, entries
, MLX4_VLAN_TABLE_SIZE
);
323 in_mod
= MLX4_SET_PORT_VLAN_TABLE
<< 8 | port
;
324 err
= mlx4_cmd(dev
, mailbox
->dma
, in_mod
, 1, MLX4_CMD_SET_PORT
,
325 MLX4_CMD_TIME_CLASS_B
);
327 mlx4_free_cmd_mailbox(dev
, mailbox
);
332 int mlx4_find_cached_vlan(struct mlx4_dev
*dev
, u8 port
, u16 vid
, int *idx
)
334 struct mlx4_vlan_table
*table
= &mlx4_priv(dev
)->port
[port
].vlan_table
;
337 for (i
= 0; i
< MLX4_MAX_VLAN_NUM
; ++i
) {
338 if (table
->refs
[i
] &&
339 (vid
== (MLX4_VLAN_MASK
&
340 be32_to_cpu(table
->entries
[i
])))) {
341 /* VLAN already registered, increase reference count */
349 EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan
);
351 int mlx4_register_vlan(struct mlx4_dev
*dev
, u8 port
, u16 vlan
, int *index
)
353 struct mlx4_vlan_table
*table
= &mlx4_priv(dev
)->port
[port
].vlan_table
;
357 mutex_lock(&table
->mutex
);
358 for (i
= MLX4_VLAN_REGULAR
; i
< MLX4_MAX_VLAN_NUM
; i
++) {
359 if (free
< 0 && (table
->refs
[i
] == 0)) {
364 if (table
->refs
[i
] &&
365 (vlan
== (MLX4_VLAN_MASK
&
366 be32_to_cpu(table
->entries
[i
])))) {
367 /* Vlan already registered, increase references count */
379 if (table
->total
== table
->max
) {
380 /* No free vlan entries */
385 /* Register new MAC */
386 table
->refs
[free
] = 1;
387 table
->entries
[free
] = cpu_to_be32(vlan
| MLX4_VLAN_VALID
);
389 err
= mlx4_set_port_vlan_table(dev
, port
, table
->entries
);
391 mlx4_warn(dev
, "Failed adding vlan: %u\n", vlan
);
392 table
->refs
[free
] = 0;
393 table
->entries
[free
] = 0;
400 mutex_unlock(&table
->mutex
);
403 EXPORT_SYMBOL_GPL(mlx4_register_vlan
);
405 void mlx4_unregister_vlan(struct mlx4_dev
*dev
, u8 port
, int index
)
407 struct mlx4_vlan_table
*table
= &mlx4_priv(dev
)->port
[port
].vlan_table
;
409 if (index
< MLX4_VLAN_REGULAR
) {
410 mlx4_warn(dev
, "Trying to free special vlan index %d\n", index
);
414 mutex_lock(&table
->mutex
);
415 if (!table
->refs
[index
]) {
416 mlx4_warn(dev
, "No vlan entry for index %d\n", index
);
419 if (--table
->refs
[index
]) {
420 mlx4_dbg(dev
, "Have more references for index %d,"
421 "no need to modify vlan table\n", index
);
424 table
->entries
[index
] = 0;
425 mlx4_set_port_vlan_table(dev
, port
, table
->entries
);
428 mutex_unlock(&table
->mutex
);
430 EXPORT_SYMBOL_GPL(mlx4_unregister_vlan
);
432 int mlx4_get_port_ib_caps(struct mlx4_dev
*dev
, u8 port
, __be32
*caps
)
434 struct mlx4_cmd_mailbox
*inmailbox
, *outmailbox
;
438 inmailbox
= mlx4_alloc_cmd_mailbox(dev
);
439 if (IS_ERR(inmailbox
))
440 return PTR_ERR(inmailbox
);
442 outmailbox
= mlx4_alloc_cmd_mailbox(dev
);
443 if (IS_ERR(outmailbox
)) {
444 mlx4_free_cmd_mailbox(dev
, inmailbox
);
445 return PTR_ERR(outmailbox
);
448 inbuf
= inmailbox
->buf
;
449 outbuf
= outmailbox
->buf
;
450 memset(inbuf
, 0, 256);
451 memset(outbuf
, 0, 256);
456 *(__be16
*) (&inbuf
[16]) = cpu_to_be16(0x0015);
457 *(__be32
*) (&inbuf
[20]) = cpu_to_be32(port
);
459 err
= mlx4_cmd_box(dev
, inmailbox
->dma
, outmailbox
->dma
, port
, 3,
460 MLX4_CMD_MAD_IFC
, MLX4_CMD_TIME_CLASS_C
);
462 *caps
= *(__be32
*) (outbuf
+ 84);
463 mlx4_free_cmd_mailbox(dev
, inmailbox
);
464 mlx4_free_cmd_mailbox(dev
, outmailbox
);
468 int mlx4_SET_PORT(struct mlx4_dev
*dev
, u8 port
)
470 struct mlx4_cmd_mailbox
*mailbox
;
473 if (dev
->caps
.port_type
[port
] == MLX4_PORT_TYPE_ETH
)
476 mailbox
= mlx4_alloc_cmd_mailbox(dev
);
478 return PTR_ERR(mailbox
);
480 memset(mailbox
->buf
, 0, 256);
482 ((__be32
*) mailbox
->buf
)[1] = dev
->caps
.ib_port_def_cap
[port
];
483 err
= mlx4_cmd(dev
, mailbox
->dma
, port
, 0, MLX4_CMD_SET_PORT
,
484 MLX4_CMD_TIME_CLASS_B
);
486 mlx4_free_cmd_mailbox(dev
, mailbox
);