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>
36 #include <linux/mlx4/cmd.h>
40 #define MLX4_MAC_VALID (1ull << 63)
41 #define MLX4_MAC_MASK 0xffffffffffffULL
43 #define MLX4_VLAN_VALID (1u << 31)
44 #define MLX4_VLAN_MASK 0xfff
46 void mlx4_init_mac_table(struct mlx4_dev
*dev
, struct mlx4_mac_table
*table
)
50 mutex_init(&table
->mutex
);
51 for (i
= 0; i
< MLX4_MAX_MAC_NUM
; i
++) {
52 table
->entries
[i
] = 0;
55 table
->max
= 1 << dev
->caps
.log_num_macs
;
59 void mlx4_init_vlan_table(struct mlx4_dev
*dev
, struct mlx4_vlan_table
*table
)
63 mutex_init(&table
->mutex
);
64 for (i
= 0; i
< MLX4_MAX_VLAN_NUM
; i
++) {
65 table
->entries
[i
] = 0;
68 table
->max
= 1 << dev
->caps
.log_num_vlans
;
72 static int mlx4_set_port_mac_table(struct mlx4_dev
*dev
, u8 port
,
75 struct mlx4_cmd_mailbox
*mailbox
;
79 mailbox
= mlx4_alloc_cmd_mailbox(dev
);
81 return PTR_ERR(mailbox
);
83 memcpy(mailbox
->buf
, entries
, MLX4_MAC_TABLE_SIZE
);
85 in_mod
= MLX4_SET_PORT_MAC_TABLE
<< 8 | port
;
86 err
= mlx4_cmd(dev
, mailbox
->dma
, in_mod
, 1, MLX4_CMD_SET_PORT
,
87 MLX4_CMD_TIME_CLASS_B
);
89 mlx4_free_cmd_mailbox(dev
, mailbox
);
93 static int mlx4_uc_steer_add(struct mlx4_dev
*dev
, u8 port
,
94 u64 mac
, int *qpn
, u8 reserve
)
101 err
= mlx4_qp_reserve_range(dev
, 1, 1, qpn
);
103 mlx4_err(dev
, "Failed to reserve qp for mac registration\n");
109 mac
&= 0xffffffffffffULL
;
110 mac
= cpu_to_be64(mac
<< 16);
111 memcpy(&gid
[10], &mac
, ETH_ALEN
);
113 gid
[7] = MLX4_UC_STEER
<< 1;
115 err
= mlx4_qp_attach_common(dev
, &qp
, gid
, 0,
116 MLX4_PROT_ETH
, MLX4_UC_STEER
);
118 mlx4_qp_release_range(dev
, *qpn
, 1);
123 static void mlx4_uc_steer_release(struct mlx4_dev
*dev
, u8 port
,
124 u64 mac
, int qpn
, u8 free
)
130 mac
&= 0xffffffffffffULL
;
131 mac
= cpu_to_be64(mac
<< 16);
132 memcpy(&gid
[10], &mac
, ETH_ALEN
);
134 gid
[7] = MLX4_UC_STEER
<< 1;
136 mlx4_qp_detach_common(dev
, &qp
, gid
, MLX4_PROT_ETH
, MLX4_UC_STEER
);
138 mlx4_qp_release_range(dev
, qpn
, 1);
141 int mlx4_register_mac(struct mlx4_dev
*dev
, u8 port
, u64 mac
, int *qpn
, u8 wrap
)
143 struct mlx4_port_info
*info
= &mlx4_priv(dev
)->port
[port
];
144 struct mlx4_mac_table
*table
= &info
->mac_table
;
145 struct mlx4_mac_entry
*entry
;
149 if (dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_VEP_UC_STEER
) {
150 err
= mlx4_uc_steer_add(dev
, port
, mac
, qpn
, 1);
152 entry
= kmalloc(sizeof *entry
, GFP_KERNEL
);
154 mlx4_uc_steer_release(dev
, port
, mac
, *qpn
, 1);
158 err
= radix_tree_insert(&info
->mac_tree
, *qpn
, entry
);
160 mlx4_uc_steer_release(dev
, port
, mac
, *qpn
, 1);
166 mlx4_dbg(dev
, "Registering MAC: 0x%llx\n", (unsigned long long) mac
);
167 mutex_lock(&table
->mutex
);
168 for (i
= 0; i
< MLX4_MAX_MAC_NUM
- 1; i
++) {
169 if (free
< 0 && !table
->refs
[i
]) {
174 if (mac
== (MLX4_MAC_MASK
& be64_to_cpu(table
->entries
[i
]))) {
175 /* MAC already registered, increase references count */
186 mlx4_dbg(dev
, "Free MAC index is %d\n", free
);
188 if (table
->total
== table
->max
) {
189 /* No free mac entries */
194 /* Register new MAC */
195 table
->refs
[free
] = 1;
196 table
->entries
[free
] = cpu_to_be64(mac
| MLX4_MAC_VALID
);
198 err
= mlx4_set_port_mac_table(dev
, port
, table
->entries
);
200 mlx4_err(dev
, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac
);
201 table
->refs
[free
] = 0;
202 table
->entries
[free
] = 0;
206 if (!(dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_VEP_UC_STEER
))
207 *qpn
= info
->base_qpn
+ free
;
210 mutex_unlock(&table
->mutex
);
213 EXPORT_SYMBOL_GPL(mlx4_register_mac
);
215 static int validate_index(struct mlx4_dev
*dev
,
216 struct mlx4_mac_table
*table
, int index
)
220 if (index
< 0 || index
>= table
->max
|| !table
->entries
[index
]) {
221 mlx4_warn(dev
, "No valid Mac entry for the given index\n");
227 static int find_index(struct mlx4_dev
*dev
,
228 struct mlx4_mac_table
*table
, u64 mac
)
231 for (i
= 0; i
< MLX4_MAX_MAC_NUM
; i
++) {
232 if (mac
== (MLX4_MAC_MASK
& be64_to_cpu(table
->entries
[i
])))
239 void mlx4_unregister_mac(struct mlx4_dev
*dev
, u8 port
, int qpn
)
241 struct mlx4_port_info
*info
= &mlx4_priv(dev
)->port
[port
];
242 struct mlx4_mac_table
*table
= &info
->mac_table
;
243 int index
= qpn
- info
->base_qpn
;
244 struct mlx4_mac_entry
*entry
;
246 if (dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_VEP_UC_STEER
) {
247 entry
= radix_tree_lookup(&info
->mac_tree
, qpn
);
249 mlx4_uc_steer_release(dev
, port
, entry
->mac
, qpn
, 1);
250 radix_tree_delete(&info
->mac_tree
, qpn
);
251 index
= find_index(dev
, table
, entry
->mac
);
256 mutex_lock(&table
->mutex
);
258 if (validate_index(dev
, table
, index
))
261 /* Check whether this address has reference count */
262 if (!(--table
->refs
[index
])) {
263 table
->entries
[index
] = 0;
264 mlx4_set_port_mac_table(dev
, port
, table
->entries
);
268 mutex_unlock(&table
->mutex
);
270 EXPORT_SYMBOL_GPL(mlx4_unregister_mac
);
272 int mlx4_replace_mac(struct mlx4_dev
*dev
, u8 port
, int qpn
, u64 new_mac
, u8 wrap
)
274 struct mlx4_port_info
*info
= &mlx4_priv(dev
)->port
[port
];
275 struct mlx4_mac_table
*table
= &info
->mac_table
;
276 int index
= qpn
- info
->base_qpn
;
277 struct mlx4_mac_entry
*entry
;
280 if (dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_VEP_UC_STEER
) {
281 entry
= radix_tree_lookup(&info
->mac_tree
, qpn
);
284 index
= find_index(dev
, table
, entry
->mac
);
285 mlx4_uc_steer_release(dev
, port
, entry
->mac
, qpn
, 0);
286 entry
->mac
= new_mac
;
287 err
= mlx4_uc_steer_add(dev
, port
, entry
->mac
, &qpn
, 0);
288 if (err
|| index
< 0)
292 mutex_lock(&table
->mutex
);
294 err
= validate_index(dev
, table
, index
);
298 table
->entries
[index
] = cpu_to_be64(new_mac
| MLX4_MAC_VALID
);
300 err
= mlx4_set_port_mac_table(dev
, port
, table
->entries
);
302 mlx4_err(dev
, "Failed adding MAC: 0x%llx\n", (unsigned long long) new_mac
);
303 table
->entries
[index
] = 0;
306 mutex_unlock(&table
->mutex
);
309 EXPORT_SYMBOL_GPL(mlx4_replace_mac
);
310 static int mlx4_set_port_vlan_table(struct mlx4_dev
*dev
, u8 port
,
313 struct mlx4_cmd_mailbox
*mailbox
;
317 mailbox
= mlx4_alloc_cmd_mailbox(dev
);
319 return PTR_ERR(mailbox
);
321 memcpy(mailbox
->buf
, entries
, MLX4_VLAN_TABLE_SIZE
);
322 in_mod
= MLX4_SET_PORT_VLAN_TABLE
<< 8 | port
;
323 err
= mlx4_cmd(dev
, mailbox
->dma
, in_mod
, 1, MLX4_CMD_SET_PORT
,
324 MLX4_CMD_TIME_CLASS_B
);
326 mlx4_free_cmd_mailbox(dev
, mailbox
);
331 int mlx4_find_cached_vlan(struct mlx4_dev
*dev
, u8 port
, u16 vid
, int *idx
)
333 struct mlx4_vlan_table
*table
= &mlx4_priv(dev
)->port
[port
].vlan_table
;
336 for (i
= 0; i
< MLX4_MAX_VLAN_NUM
; ++i
) {
337 if (table
->refs
[i
] &&
338 (vid
== (MLX4_VLAN_MASK
&
339 be32_to_cpu(table
->entries
[i
])))) {
340 /* VLAN already registered, increase reference count */
348 EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan
);
350 int mlx4_register_vlan(struct mlx4_dev
*dev
, u8 port
, u16 vlan
, int *index
)
352 struct mlx4_vlan_table
*table
= &mlx4_priv(dev
)->port
[port
].vlan_table
;
356 mutex_lock(&table
->mutex
);
357 for (i
= MLX4_VLAN_REGULAR
; i
< MLX4_MAX_VLAN_NUM
; i
++) {
358 if (free
< 0 && (table
->refs
[i
] == 0)) {
363 if (table
->refs
[i
] &&
364 (vlan
== (MLX4_VLAN_MASK
&
365 be32_to_cpu(table
->entries
[i
])))) {
366 /* Vlan already registered, increase references count */
378 if (table
->total
== table
->max
) {
379 /* No free vlan entries */
384 /* Register new MAC */
385 table
->refs
[free
] = 1;
386 table
->entries
[free
] = cpu_to_be32(vlan
| MLX4_VLAN_VALID
);
388 err
= mlx4_set_port_vlan_table(dev
, port
, table
->entries
);
390 mlx4_warn(dev
, "Failed adding vlan: %u\n", vlan
);
391 table
->refs
[free
] = 0;
392 table
->entries
[free
] = 0;
399 mutex_unlock(&table
->mutex
);
402 EXPORT_SYMBOL_GPL(mlx4_register_vlan
);
404 void mlx4_unregister_vlan(struct mlx4_dev
*dev
, u8 port
, int index
)
406 struct mlx4_vlan_table
*table
= &mlx4_priv(dev
)->port
[port
].vlan_table
;
408 if (index
< MLX4_VLAN_REGULAR
) {
409 mlx4_warn(dev
, "Trying to free special vlan index %d\n", index
);
413 mutex_lock(&table
->mutex
);
414 if (!table
->refs
[index
]) {
415 mlx4_warn(dev
, "No vlan entry for index %d\n", index
);
418 if (--table
->refs
[index
]) {
419 mlx4_dbg(dev
, "Have more references for index %d,"
420 "no need to modify vlan table\n", index
);
423 table
->entries
[index
] = 0;
424 mlx4_set_port_vlan_table(dev
, port
, table
->entries
);
427 mutex_unlock(&table
->mutex
);
429 EXPORT_SYMBOL_GPL(mlx4_unregister_vlan
);
431 int mlx4_get_port_ib_caps(struct mlx4_dev
*dev
, u8 port
, __be32
*caps
)
433 struct mlx4_cmd_mailbox
*inmailbox
, *outmailbox
;
437 inmailbox
= mlx4_alloc_cmd_mailbox(dev
);
438 if (IS_ERR(inmailbox
))
439 return PTR_ERR(inmailbox
);
441 outmailbox
= mlx4_alloc_cmd_mailbox(dev
);
442 if (IS_ERR(outmailbox
)) {
443 mlx4_free_cmd_mailbox(dev
, inmailbox
);
444 return PTR_ERR(outmailbox
);
447 inbuf
= inmailbox
->buf
;
448 outbuf
= outmailbox
->buf
;
449 memset(inbuf
, 0, 256);
450 memset(outbuf
, 0, 256);
455 *(__be16
*) (&inbuf
[16]) = cpu_to_be16(0x0015);
456 *(__be32
*) (&inbuf
[20]) = cpu_to_be32(port
);
458 err
= mlx4_cmd_box(dev
, inmailbox
->dma
, outmailbox
->dma
, port
, 3,
459 MLX4_CMD_MAD_IFC
, MLX4_CMD_TIME_CLASS_C
);
461 *caps
= *(__be32
*) (outbuf
+ 84);
462 mlx4_free_cmd_mailbox(dev
, inmailbox
);
463 mlx4_free_cmd_mailbox(dev
, outmailbox
);
467 int mlx4_SET_PORT(struct mlx4_dev
*dev
, u8 port
)
469 struct mlx4_cmd_mailbox
*mailbox
;
472 if (dev
->caps
.port_type
[port
] == MLX4_PORT_TYPE_ETH
)
475 mailbox
= mlx4_alloc_cmd_mailbox(dev
);
477 return PTR_ERR(mailbox
);
479 memset(mailbox
->buf
, 0, 256);
481 ((__be32
*) mailbox
->buf
)[1] = dev
->caps
.ib_port_def_cap
[port
];
482 err
= mlx4_cmd(dev
, mailbox
->dma
, port
, 0, MLX4_CMD_SET_PORT
,
483 MLX4_CMD_TIME_CLASS_B
);
485 mlx4_free_cmd_mailbox(dev
, mailbox
);