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) - MLX4_VLAN_REGULAR;
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);
154 entry = kmalloc(sizeof *entry, GFP_KERNEL);
156 mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
161 err = radix_tree_insert(&info->mac_tree, *qpn, entry);
164 mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
169 mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
171 mutex_lock(&table->mutex);
172 for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
173 if (free < 0 && !table->refs[i]) {
178 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
179 /* MAC already registered, increase references count */
190 mlx4_dbg(dev, "Free MAC index is %d\n", free);
192 if (table->total == table->max) {
193 /* No free mac entries */
198 /* Register new MAC */
199 table->refs[free] = 1;
200 table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
202 err = mlx4_set_port_mac_table(dev, port, table->entries);
204 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
205 table->refs[free] = 0;
206 table->entries[free] = 0;
210 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
211 *qpn = info->base_qpn + free;
214 mutex_unlock(&table->mutex);
217 EXPORT_SYMBOL_GPL(mlx4_register_mac);
219 static int validate_index(struct mlx4_dev *dev,
220 struct mlx4_mac_table *table, int index)
224 if (index < 0 || index >= table->max || !table->entries[index]) {
225 mlx4_warn(dev, "No valid Mac entry for the given index\n");
231 static int find_index(struct mlx4_dev *dev,
232 struct mlx4_mac_table *table, u64 mac)
235 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
236 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
243 void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int qpn)
245 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
246 struct mlx4_mac_table *table = &info->mac_table;
247 int index = qpn - info->base_qpn;
248 struct mlx4_mac_entry *entry;
250 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
251 entry = radix_tree_lookup(&info->mac_tree, qpn);
253 mlx4_uc_steer_release(dev, port, entry->mac, qpn, 1);
254 radix_tree_delete(&info->mac_tree, qpn);
255 index = find_index(dev, table, entry->mac);
260 mutex_lock(&table->mutex);
262 if (validate_index(dev, table, index))
265 /* Check whether this address has reference count */
266 if (!(--table->refs[index])) {
267 table->entries[index] = 0;
268 mlx4_set_port_mac_table(dev, port, table->entries);
272 mutex_unlock(&table->mutex);
274 EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
276 int mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac, u8 wrap)
278 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
279 struct mlx4_mac_table *table = &info->mac_table;
280 int index = qpn - info->base_qpn;
281 struct mlx4_mac_entry *entry;
284 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
285 entry = radix_tree_lookup(&info->mac_tree, qpn);
288 index = find_index(dev, table, entry->mac);
289 mlx4_uc_steer_release(dev, port, entry->mac, qpn, 0);
290 entry->mac = new_mac;
291 err = mlx4_uc_steer_add(dev, port, entry->mac, &qpn, 0);
292 if (err || index < 0)
296 mutex_lock(&table->mutex);
298 err = validate_index(dev, table, index);
302 table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
304 err = mlx4_set_port_mac_table(dev, port, table->entries);
306 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) new_mac);
307 table->entries[index] = 0;
310 mutex_unlock(&table->mutex);
313 EXPORT_SYMBOL_GPL(mlx4_replace_mac);
314 static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
317 struct mlx4_cmd_mailbox *mailbox;
321 mailbox = mlx4_alloc_cmd_mailbox(dev);
323 return PTR_ERR(mailbox);
325 memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
326 in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
327 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
328 MLX4_CMD_TIME_CLASS_B);
330 mlx4_free_cmd_mailbox(dev, mailbox);
335 int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
337 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
340 for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
341 if (table->refs[i] &&
342 (vid == (MLX4_VLAN_MASK &
343 be32_to_cpu(table->entries[i])))) {
344 /* VLAN already registered, increase reference count */
352 EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
354 int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
356 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
360 mutex_lock(&table->mutex);
362 if (table->total == table->max) {
363 /* No free vlan entries */
368 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
369 if (free < 0 && (table->refs[i] == 0)) {
374 if (table->refs[i] &&
375 (vlan == (MLX4_VLAN_MASK &
376 be32_to_cpu(table->entries[i])))) {
377 /* Vlan already registered, increase references count */
389 /* Register new MAC */
390 table->refs[free] = 1;
391 table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
393 err = mlx4_set_port_vlan_table(dev, port, table->entries);
395 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
396 table->refs[free] = 0;
397 table->entries[free] = 0;
404 mutex_unlock(&table->mutex);
407 EXPORT_SYMBOL_GPL(mlx4_register_vlan);
409 void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
411 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
413 if (index < MLX4_VLAN_REGULAR) {
414 mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
418 mutex_lock(&table->mutex);
419 if (!table->refs[index]) {
420 mlx4_warn(dev, "No vlan entry for index %d\n", index);
423 if (--table->refs[index]) {
424 mlx4_dbg(dev, "Have more references for index %d,"
425 "no need to modify vlan table\n", index);
428 table->entries[index] = 0;
429 mlx4_set_port_vlan_table(dev, port, table->entries);
432 mutex_unlock(&table->mutex);
434 EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
436 int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
438 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
442 inmailbox = mlx4_alloc_cmd_mailbox(dev);
443 if (IS_ERR(inmailbox))
444 return PTR_ERR(inmailbox);
446 outmailbox = mlx4_alloc_cmd_mailbox(dev);
447 if (IS_ERR(outmailbox)) {
448 mlx4_free_cmd_mailbox(dev, inmailbox);
449 return PTR_ERR(outmailbox);
452 inbuf = inmailbox->buf;
453 outbuf = outmailbox->buf;
454 memset(inbuf, 0, 256);
455 memset(outbuf, 0, 256);
460 *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
461 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
463 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
464 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
466 *caps = *(__be32 *) (outbuf + 84);
467 mlx4_free_cmd_mailbox(dev, inmailbox);
468 mlx4_free_cmd_mailbox(dev, outmailbox);
472 int mlx4_check_ext_port_caps(struct mlx4_dev *dev, u8 port)
474 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
476 int err, packet_error;
478 inmailbox = mlx4_alloc_cmd_mailbox(dev);
479 if (IS_ERR(inmailbox))
480 return PTR_ERR(inmailbox);
482 outmailbox = mlx4_alloc_cmd_mailbox(dev);
483 if (IS_ERR(outmailbox)) {
484 mlx4_free_cmd_mailbox(dev, inmailbox);
485 return PTR_ERR(outmailbox);
488 inbuf = inmailbox->buf;
489 outbuf = outmailbox->buf;
490 memset(inbuf, 0, 256);
491 memset(outbuf, 0, 256);
497 *(__be16 *) (&inbuf[16]) = MLX4_ATTR_EXTENDED_PORT_INFO;
498 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
500 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
501 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
503 packet_error = be16_to_cpu(*(__be16 *) (outbuf + 4));
505 dev->caps.ext_port_cap[port] = (!err && !packet_error) ?
506 MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO
509 mlx4_free_cmd_mailbox(dev, inmailbox);
510 mlx4_free_cmd_mailbox(dev, outmailbox);
514 int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
516 struct mlx4_cmd_mailbox *mailbox;
519 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
522 mailbox = mlx4_alloc_cmd_mailbox(dev);
524 return PTR_ERR(mailbox);
526 memset(mailbox->buf, 0, 256);
528 ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
529 err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
530 MLX4_CMD_TIME_CLASS_B);
532 mlx4_free_cmd_mailbox(dev, mailbox);