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) - MLX4_VLAN_REGULAR;
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);
155 entry = kmalloc(sizeof *entry, GFP_KERNEL);
157 mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
162 err = radix_tree_insert(&info->mac_tree, *qpn, entry);
165 mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
170 mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
172 mutex_lock(&table->mutex);
173 for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
174 if (free < 0 && !table->refs[i]) {
179 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
180 /* MAC already registered, increase references count */
191 mlx4_dbg(dev, "Free MAC index is %d\n", free);
193 if (table->total == table->max) {
194 /* No free mac entries */
199 /* Register new MAC */
200 table->refs[free] = 1;
201 table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
203 err = mlx4_set_port_mac_table(dev, port, table->entries);
205 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
206 table->refs[free] = 0;
207 table->entries[free] = 0;
211 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
212 *qpn = info->base_qpn + free;
215 mutex_unlock(&table->mutex);
218 EXPORT_SYMBOL_GPL(mlx4_register_mac);
220 static int validate_index(struct mlx4_dev *dev,
221 struct mlx4_mac_table *table, int index)
225 if (index < 0 || index >= table->max || !table->entries[index]) {
226 mlx4_warn(dev, "No valid Mac entry for the given index\n");
232 static int find_index(struct mlx4_dev *dev,
233 struct mlx4_mac_table *table, u64 mac)
236 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
237 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
244 void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int qpn)
246 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
247 struct mlx4_mac_table *table = &info->mac_table;
248 int index = qpn - info->base_qpn;
249 struct mlx4_mac_entry *entry;
251 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
252 entry = radix_tree_lookup(&info->mac_tree, qpn);
254 mlx4_uc_steer_release(dev, port, entry->mac, qpn, 1);
255 radix_tree_delete(&info->mac_tree, qpn);
256 index = find_index(dev, table, entry->mac);
261 mutex_lock(&table->mutex);
263 if (validate_index(dev, table, index))
266 /* Check whether this address has reference count */
267 if (!(--table->refs[index])) {
268 table->entries[index] = 0;
269 mlx4_set_port_mac_table(dev, port, table->entries);
273 mutex_unlock(&table->mutex);
275 EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
277 int mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac, u8 wrap)
279 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
280 struct mlx4_mac_table *table = &info->mac_table;
281 int index = qpn - info->base_qpn;
282 struct mlx4_mac_entry *entry;
285 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
286 entry = radix_tree_lookup(&info->mac_tree, qpn);
289 index = find_index(dev, table, entry->mac);
290 mlx4_uc_steer_release(dev, port, entry->mac, qpn, 0);
291 entry->mac = new_mac;
292 err = mlx4_uc_steer_add(dev, port, entry->mac, &qpn, 0);
293 if (err || index < 0)
297 mutex_lock(&table->mutex);
299 err = validate_index(dev, table, index);
303 table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
305 err = mlx4_set_port_mac_table(dev, port, table->entries);
307 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) new_mac);
308 table->entries[index] = 0;
311 mutex_unlock(&table->mutex);
314 EXPORT_SYMBOL_GPL(mlx4_replace_mac);
315 static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
318 struct mlx4_cmd_mailbox *mailbox;
322 mailbox = mlx4_alloc_cmd_mailbox(dev);
324 return PTR_ERR(mailbox);
326 memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
327 in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
328 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
329 MLX4_CMD_TIME_CLASS_B);
331 mlx4_free_cmd_mailbox(dev, mailbox);
336 int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
338 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
341 for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
342 if (table->refs[i] &&
343 (vid == (MLX4_VLAN_MASK &
344 be32_to_cpu(table->entries[i])))) {
345 /* VLAN already registered, increase reference count */
353 EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
355 int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
357 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
361 mutex_lock(&table->mutex);
363 if (table->total == table->max) {
364 /* No free vlan entries */
369 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
370 if (free < 0 && (table->refs[i] == 0)) {
375 if (table->refs[i] &&
376 (vlan == (MLX4_VLAN_MASK &
377 be32_to_cpu(table->entries[i])))) {
378 /* Vlan already registered, increase references count */
390 /* Register new MAC */
391 table->refs[free] = 1;
392 table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
394 err = mlx4_set_port_vlan_table(dev, port, table->entries);
396 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
397 table->refs[free] = 0;
398 table->entries[free] = 0;
405 mutex_unlock(&table->mutex);
408 EXPORT_SYMBOL_GPL(mlx4_register_vlan);
410 void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
412 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
414 if (index < MLX4_VLAN_REGULAR) {
415 mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
419 mutex_lock(&table->mutex);
420 if (!table->refs[index]) {
421 mlx4_warn(dev, "No vlan entry for index %d\n", index);
424 if (--table->refs[index]) {
425 mlx4_dbg(dev, "Have more references for index %d,"
426 "no need to modify vlan table\n", index);
429 table->entries[index] = 0;
430 mlx4_set_port_vlan_table(dev, port, table->entries);
433 mutex_unlock(&table->mutex);
435 EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
437 int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
439 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
443 inmailbox = mlx4_alloc_cmd_mailbox(dev);
444 if (IS_ERR(inmailbox))
445 return PTR_ERR(inmailbox);
447 outmailbox = mlx4_alloc_cmd_mailbox(dev);
448 if (IS_ERR(outmailbox)) {
449 mlx4_free_cmd_mailbox(dev, inmailbox);
450 return PTR_ERR(outmailbox);
453 inbuf = inmailbox->buf;
454 outbuf = outmailbox->buf;
455 memset(inbuf, 0, 256);
456 memset(outbuf, 0, 256);
461 *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
462 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
464 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
465 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
467 *caps = *(__be32 *) (outbuf + 84);
468 mlx4_free_cmd_mailbox(dev, inmailbox);
469 mlx4_free_cmd_mailbox(dev, outmailbox);
473 int mlx4_check_ext_port_caps(struct mlx4_dev *dev, u8 port)
475 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
477 int err, packet_error;
479 inmailbox = mlx4_alloc_cmd_mailbox(dev);
480 if (IS_ERR(inmailbox))
481 return PTR_ERR(inmailbox);
483 outmailbox = mlx4_alloc_cmd_mailbox(dev);
484 if (IS_ERR(outmailbox)) {
485 mlx4_free_cmd_mailbox(dev, inmailbox);
486 return PTR_ERR(outmailbox);
489 inbuf = inmailbox->buf;
490 outbuf = outmailbox->buf;
491 memset(inbuf, 0, 256);
492 memset(outbuf, 0, 256);
498 *(__be16 *) (&inbuf[16]) = MLX4_ATTR_EXTENDED_PORT_INFO;
499 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
501 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
502 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
504 packet_error = be16_to_cpu(*(__be16 *) (outbuf + 4));
506 dev->caps.ext_port_cap[port] = (!err && !packet_error) ?
507 MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO
510 mlx4_free_cmd_mailbox(dev, inmailbox);
511 mlx4_free_cmd_mailbox(dev, outmailbox);
515 int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
517 struct mlx4_cmd_mailbox *mailbox;
520 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
523 mailbox = mlx4_alloc_cmd_mailbox(dev);
525 return PTR_ERR(mailbox);
527 memset(mailbox->buf, 0, 256);
529 ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
530 err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
531 MLX4_CMD_TIME_CLASS_B);
533 mlx4_free_cmd_mailbox(dev, mailbox);