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/if_vlan.h>
36 #include <linux/export.h>
38 #include <linux/mlx4/cmd.h>
42 #define MLX4_MAC_VALID (1ull << 63)
44 #define MLX4_VLAN_VALID (1u << 31)
45 #define MLX4_VLAN_MASK 0xfff
47 #define MLX4_STATS_TRAFFIC_COUNTERS_MASK 0xfULL
48 #define MLX4_STATS_TRAFFIC_DROPS_MASK 0xc0ULL
49 #define MLX4_STATS_ERROR_COUNTERS_MASK 0x1ffc30ULL
50 #define MLX4_STATS_PORT_COUNTERS_MASK 0x1fe00000ULL
52 void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
56 mutex_init(&table->mutex);
57 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
58 table->entries[i] = 0;
61 table->max = 1 << dev->caps.log_num_macs;
65 void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
69 mutex_init(&table->mutex);
70 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
71 table->entries[i] = 0;
74 table->max = (1 << dev->caps.log_num_vlans) - MLX4_VLAN_REGULAR;
78 void mlx4_init_roce_gid_table(struct mlx4_dev *dev,
79 struct mlx4_roce_gid_table *table)
83 mutex_init(&table->mutex);
84 for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++)
85 memset(table->roce_gids[i].raw, 0, MLX4_ROCE_GID_ENTRY_SIZE);
88 static int validate_index(struct mlx4_dev *dev,
89 struct mlx4_mac_table *table, int index)
93 if (index < 0 || index >= table->max || !table->entries[index]) {
94 mlx4_warn(dev, "No valid Mac entry for the given index\n");
100 static int find_index(struct mlx4_dev *dev,
101 struct mlx4_mac_table *table, u64 mac)
105 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
106 if ((mac & MLX4_MAC_MASK) ==
107 (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
114 static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
117 struct mlx4_cmd_mailbox *mailbox;
121 mailbox = mlx4_alloc_cmd_mailbox(dev);
123 return PTR_ERR(mailbox);
125 memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
127 in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
129 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
130 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
132 mlx4_free_cmd_mailbox(dev, mailbox);
136 int mlx4_find_cached_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *idx)
138 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
139 struct mlx4_mac_table *table = &info->mac_table;
142 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
146 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
154 EXPORT_SYMBOL_GPL(mlx4_find_cached_mac);
156 int __mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
158 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
159 struct mlx4_mac_table *table = &info->mac_table;
163 mlx4_dbg(dev, "Registering MAC: 0x%llx for port %d\n",
164 (unsigned long long) mac, port);
166 mutex_lock(&table->mutex);
167 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
168 if (free < 0 && !table->entries[i]) {
173 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
174 /* MAC already registered, increment ref count */
181 mlx4_dbg(dev, "Free MAC index is %d\n", free);
183 if (table->total == table->max) {
184 /* No free mac entries */
189 /* Register new MAC */
190 table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
192 err = mlx4_set_port_mac_table(dev, port, table->entries);
194 mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
195 (unsigned long long) mac);
196 table->entries[free] = 0;
199 table->refs[free] = 1;
203 mutex_unlock(&table->mutex);
206 EXPORT_SYMBOL_GPL(__mlx4_register_mac);
208 int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
213 if (mlx4_is_mfunc(dev)) {
214 if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
215 err = mlx4_cmd_imm(dev, mac, &out_param,
216 ((u32) port) << 8 | (u32) RES_MAC,
217 RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
218 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
220 if (err && err == -EINVAL && mlx4_is_slave(dev)) {
221 /* retry using old REG_MAC format */
222 set_param_l(&out_param, port);
223 err = mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
224 RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
225 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
227 dev->flags |= MLX4_FLAG_OLD_REG_MAC;
232 return get_param_l(&out_param);
234 return __mlx4_register_mac(dev, port, mac);
236 EXPORT_SYMBOL_GPL(mlx4_register_mac);
238 int mlx4_get_base_qpn(struct mlx4_dev *dev, u8 port)
240 return dev->caps.reserved_qps_base[MLX4_QP_REGION_ETH_ADDR] +
241 (port - 1) * (1 << dev->caps.log_num_macs);
243 EXPORT_SYMBOL_GPL(mlx4_get_base_qpn);
245 void __mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
247 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
248 struct mlx4_mac_table *table = &info->mac_table;
251 mutex_lock(&table->mutex);
252 index = find_index(dev, table, mac);
254 if (validate_index(dev, table, index))
256 if (--table->refs[index]) {
257 mlx4_dbg(dev, "Have more references for index %d,"
258 "no need to modify mac table\n", index);
262 table->entries[index] = 0;
263 mlx4_set_port_mac_table(dev, port, table->entries);
266 mutex_unlock(&table->mutex);
268 EXPORT_SYMBOL_GPL(__mlx4_unregister_mac);
270 void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
274 if (mlx4_is_mfunc(dev)) {
275 if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
276 (void) mlx4_cmd_imm(dev, mac, &out_param,
277 ((u32) port) << 8 | (u32) RES_MAC,
278 RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
279 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
281 /* use old unregister mac format */
282 set_param_l(&out_param, port);
283 (void) mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
284 RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
285 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
289 __mlx4_unregister_mac(dev, port, mac);
292 EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
294 int __mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac)
296 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
297 struct mlx4_mac_table *table = &info->mac_table;
298 int index = qpn - info->base_qpn;
301 /* CX1 doesn't support multi-functions */
302 mutex_lock(&table->mutex);
304 err = validate_index(dev, table, index);
308 table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
310 err = mlx4_set_port_mac_table(dev, port, table->entries);
312 mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
313 (unsigned long long) new_mac);
314 table->entries[index] = 0;
317 mutex_unlock(&table->mutex);
320 EXPORT_SYMBOL_GPL(__mlx4_replace_mac);
322 static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
325 struct mlx4_cmd_mailbox *mailbox;
329 mailbox = mlx4_alloc_cmd_mailbox(dev);
331 return PTR_ERR(mailbox);
333 memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
334 in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
335 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
336 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
338 mlx4_free_cmd_mailbox(dev, mailbox);
343 int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
345 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
348 for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
349 if (table->refs[i] &&
350 (vid == (MLX4_VLAN_MASK &
351 be32_to_cpu(table->entries[i])))) {
352 /* VLAN already registered, increase reference count */
360 EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
362 int __mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan,
365 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
369 mutex_lock(&table->mutex);
371 if (table->total == table->max) {
372 /* No free vlan entries */
377 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
378 if (free < 0 && (table->refs[i] == 0)) {
383 if (table->refs[i] &&
384 (vlan == (MLX4_VLAN_MASK &
385 be32_to_cpu(table->entries[i])))) {
386 /* Vlan already registered, increase references count */
398 /* Register new VLAN */
399 table->refs[free] = 1;
400 table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
402 err = mlx4_set_port_vlan_table(dev, port, table->entries);
404 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
405 table->refs[free] = 0;
406 table->entries[free] = 0;
413 mutex_unlock(&table->mutex);
417 int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
425 if (mlx4_is_mfunc(dev)) {
426 err = mlx4_cmd_imm(dev, vlan, &out_param,
427 ((u32) port) << 8 | (u32) RES_VLAN,
428 RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
429 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
431 *index = get_param_l(&out_param);
435 return __mlx4_register_vlan(dev, port, vlan, index);
437 EXPORT_SYMBOL_GPL(mlx4_register_vlan);
439 void __mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
441 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
444 mutex_lock(&table->mutex);
445 if (mlx4_find_cached_vlan(dev, port, vlan, &index)) {
446 mlx4_warn(dev, "vlan 0x%x is not in the vlan table\n", vlan);
450 if (index < MLX4_VLAN_REGULAR) {
451 mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
455 if (--table->refs[index]) {
456 mlx4_dbg(dev, "Have %d more references for index %d,"
457 "no need to modify vlan table\n", table->refs[index],
461 table->entries[index] = 0;
462 mlx4_set_port_vlan_table(dev, port, table->entries);
465 mutex_unlock(&table->mutex);
468 void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
472 if (mlx4_is_mfunc(dev)) {
473 (void) mlx4_cmd_imm(dev, vlan, &out_param,
474 ((u32) port) << 8 | (u32) RES_VLAN,
475 RES_OP_RESERVE_AND_MAP,
476 MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
480 __mlx4_unregister_vlan(dev, port, vlan);
482 EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
484 int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
486 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
490 inmailbox = mlx4_alloc_cmd_mailbox(dev);
491 if (IS_ERR(inmailbox))
492 return PTR_ERR(inmailbox);
494 outmailbox = mlx4_alloc_cmd_mailbox(dev);
495 if (IS_ERR(outmailbox)) {
496 mlx4_free_cmd_mailbox(dev, inmailbox);
497 return PTR_ERR(outmailbox);
500 inbuf = inmailbox->buf;
501 outbuf = outmailbox->buf;
506 *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
507 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
509 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
510 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
513 *caps = *(__be32 *) (outbuf + 84);
514 mlx4_free_cmd_mailbox(dev, inmailbox);
515 mlx4_free_cmd_mailbox(dev, outmailbox);
518 static struct mlx4_roce_gid_entry zgid_entry;
520 int mlx4_get_slave_num_gids(struct mlx4_dev *dev, int slave, int port)
523 int slave_gid = slave;
525 struct mlx4_slaves_pport slaves_pport;
526 struct mlx4_active_ports actv_ports;
527 unsigned max_port_p_one;
530 return MLX4_ROCE_PF_GIDS;
533 slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
534 actv_ports = mlx4_get_active_ports(dev, slave);
535 max_port_p_one = find_first_bit(actv_ports.ports, dev->caps.num_ports) +
536 bitmap_weight(actv_ports.ports, dev->caps.num_ports) + 1;
538 for (i = 1; i < max_port_p_one; i++) {
539 struct mlx4_active_ports exclusive_ports;
540 struct mlx4_slaves_pport slaves_pport_actv;
541 bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
542 set_bit(i - 1, exclusive_ports.ports);
545 slaves_pport_actv = mlx4_phys_to_slaves_pport_actv(
546 dev, &exclusive_ports);
547 slave_gid -= bitmap_weight(slaves_pport_actv.slaves,
550 vfs = bitmap_weight(slaves_pport.slaves, dev->num_vfs + 1) - 1;
551 if (slave_gid <= ((MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) % vfs))
552 return ((MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) / vfs) + 1;
553 return (MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) / vfs;
556 int mlx4_get_base_gid_ix(struct mlx4_dev *dev, int slave, int port)
560 int slave_gid = slave;
563 struct mlx4_slaves_pport slaves_pport;
564 struct mlx4_active_ports actv_ports;
565 unsigned max_port_p_one;
570 slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
571 actv_ports = mlx4_get_active_ports(dev, slave);
572 max_port_p_one = find_first_bit(actv_ports.ports, dev->caps.num_ports) +
573 bitmap_weight(actv_ports.ports, dev->caps.num_ports) + 1;
575 for (i = 1; i < max_port_p_one; i++) {
576 struct mlx4_active_ports exclusive_ports;
577 struct mlx4_slaves_pport slaves_pport_actv;
578 bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
579 set_bit(i - 1, exclusive_ports.ports);
582 slaves_pport_actv = mlx4_phys_to_slaves_pport_actv(
583 dev, &exclusive_ports);
584 slave_gid -= bitmap_weight(slaves_pport_actv.slaves,
587 gids = MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS;
588 vfs = bitmap_weight(slaves_pport.slaves, dev->num_vfs + 1) - 1;
589 if (slave_gid <= gids % vfs)
590 return MLX4_ROCE_PF_GIDS + ((gids / vfs) + 1) * (slave_gid - 1);
592 return MLX4_ROCE_PF_GIDS + (gids % vfs) +
593 ((gids / vfs) * (slave_gid - 1));
595 EXPORT_SYMBOL_GPL(mlx4_get_base_gid_ix);
597 static int mlx4_reset_roce_port_gids(struct mlx4_dev *dev, int slave,
598 int port, struct mlx4_cmd_mailbox *mailbox)
600 struct mlx4_roce_gid_entry *gid_entry_mbox;
601 struct mlx4_priv *priv = mlx4_priv(dev);
602 int num_gids, base, offset;
605 num_gids = mlx4_get_slave_num_gids(dev, slave, port);
606 base = mlx4_get_base_gid_ix(dev, slave, port);
608 memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
610 mutex_lock(&(priv->port[port].gid_table.mutex));
611 /* Zero-out gids belonging to that slave in the port GID table */
612 for (i = 0, offset = base; i < num_gids; offset++, i++)
613 memcpy(priv->port[port].gid_table.roce_gids[offset].raw,
614 zgid_entry.raw, MLX4_ROCE_GID_ENTRY_SIZE);
616 /* Now, copy roce port gids table to mailbox for passing to FW */
617 gid_entry_mbox = (struct mlx4_roce_gid_entry *)mailbox->buf;
618 for (i = 0; i < MLX4_ROCE_MAX_GIDS; gid_entry_mbox++, i++)
619 memcpy(gid_entry_mbox->raw,
620 priv->port[port].gid_table.roce_gids[i].raw,
621 MLX4_ROCE_GID_ENTRY_SIZE);
623 err = mlx4_cmd(dev, mailbox->dma,
624 ((u32)port) | (MLX4_SET_PORT_GID_TABLE << 8), 1,
625 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
627 mutex_unlock(&(priv->port[port].gid_table.mutex));
632 void mlx4_reset_roce_gids(struct mlx4_dev *dev, int slave)
634 struct mlx4_active_ports actv_ports;
635 struct mlx4_cmd_mailbox *mailbox;
636 int num_eth_ports, err;
639 if (slave < 0 || slave > dev->num_vfs)
642 actv_ports = mlx4_get_active_ports(dev, slave);
644 for (i = 0, num_eth_ports = 0; i < dev->caps.num_ports; i++) {
645 if (test_bit(i, actv_ports.ports)) {
646 if (dev->caps.port_type[i + 1] != MLX4_PORT_TYPE_ETH)
655 /* have ETH ports. Alloc mailbox for SET_PORT command */
656 mailbox = mlx4_alloc_cmd_mailbox(dev);
660 for (i = 0; i < dev->caps.num_ports; i++) {
661 if (test_bit(i, actv_ports.ports)) {
662 if (dev->caps.port_type[i + 1] != MLX4_PORT_TYPE_ETH)
664 err = mlx4_reset_roce_port_gids(dev, slave, i + 1, mailbox);
666 mlx4_warn(dev, "Could not reset ETH port GID table for slave %d, port %d (%d)\n",
671 mlx4_free_cmd_mailbox(dev, mailbox);
675 static int mlx4_common_set_port(struct mlx4_dev *dev, int slave, u32 in_mod,
676 u8 op_mod, struct mlx4_cmd_mailbox *inbox)
678 struct mlx4_priv *priv = mlx4_priv(dev);
679 struct mlx4_port_info *port_info;
680 struct mlx4_mfunc_master_ctx *master = &priv->mfunc.master;
681 struct mlx4_slave_state *slave_st = &master->slave_state[slave];
682 struct mlx4_set_port_rqp_calc_context *qpn_context;
683 struct mlx4_set_port_general_context *gen_context;
684 struct mlx4_roce_gid_entry *gid_entry_tbl, *gid_entry_mbox, *gid_entry_mb1;
685 int reset_qkey_viols;
697 __be32 slave_cap_mask;
700 port = in_mod & 0xff;
701 in_modifier = in_mod >> 8;
703 port_info = &priv->port[port];
705 /* Slaves cannot perform SET_PORT operations except changing MTU */
707 if (slave != dev->caps.function &&
708 in_modifier != MLX4_SET_PORT_GENERAL &&
709 in_modifier != MLX4_SET_PORT_GID_TABLE) {
710 mlx4_warn(dev, "denying SET_PORT for slave:%d\n",
714 switch (in_modifier) {
715 case MLX4_SET_PORT_RQP_CALC:
716 qpn_context = inbox->buf;
717 qpn_context->base_qpn =
718 cpu_to_be32(port_info->base_qpn);
719 qpn_context->n_mac = 0x7;
720 promisc = be32_to_cpu(qpn_context->promisc) >>
721 SET_PORT_PROMISC_SHIFT;
722 qpn_context->promisc = cpu_to_be32(
723 promisc << SET_PORT_PROMISC_SHIFT |
724 port_info->base_qpn);
725 promisc = be32_to_cpu(qpn_context->mcast) >>
726 SET_PORT_MC_PROMISC_SHIFT;
727 qpn_context->mcast = cpu_to_be32(
728 promisc << SET_PORT_MC_PROMISC_SHIFT |
729 port_info->base_qpn);
731 case MLX4_SET_PORT_GENERAL:
732 gen_context = inbox->buf;
733 /* Mtu is configured as the max MTU among all the
734 * the functions on the port. */
735 mtu = be16_to_cpu(gen_context->mtu);
736 mtu = min_t(int, mtu, dev->caps.eth_mtu_cap[port] +
737 ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
738 prev_mtu = slave_st->mtu[port];
739 slave_st->mtu[port] = mtu;
740 if (mtu > master->max_mtu[port])
741 master->max_mtu[port] = mtu;
742 if (mtu < prev_mtu && prev_mtu ==
743 master->max_mtu[port]) {
744 slave_st->mtu[port] = mtu;
745 master->max_mtu[port] = mtu;
746 for (i = 0; i < dev->num_slaves; i++) {
747 master->max_mtu[port] =
748 max(master->max_mtu[port],
749 master->slave_state[i].mtu[port]);
753 gen_context->mtu = cpu_to_be16(master->max_mtu[port]);
755 case MLX4_SET_PORT_GID_TABLE:
756 /* change to MULTIPLE entries: number of guest's gids
757 * need a FOR-loop here over number of gids the guest has.
758 * 1. Check no duplicates in gids passed by slave
760 num_gids = mlx4_get_slave_num_gids(dev, slave, port);
761 base = mlx4_get_base_gid_ix(dev, slave, port);
762 gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
763 for (i = 0; i < num_gids; gid_entry_mbox++, i++) {
764 if (!memcmp(gid_entry_mbox->raw, zgid_entry.raw,
767 gid_entry_mb1 = gid_entry_mbox + 1;
768 for (j = i + 1; j < num_gids; gid_entry_mb1++, j++) {
769 if (!memcmp(gid_entry_mb1->raw,
770 zgid_entry.raw, sizeof(zgid_entry)))
772 if (!memcmp(gid_entry_mb1->raw, gid_entry_mbox->raw,
773 sizeof(gid_entry_mbox->raw))) {
774 /* found duplicate */
780 /* 2. Check that do not have duplicates in OTHER
781 * entries in the port GID table
784 mutex_lock(&(priv->port[port].gid_table.mutex));
785 for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++) {
786 if (i >= base && i < base + num_gids)
787 continue; /* don't compare to slave's current gids */
788 gid_entry_tbl = &priv->port[port].gid_table.roce_gids[i];
789 if (!memcmp(gid_entry_tbl->raw, zgid_entry.raw, sizeof(zgid_entry)))
791 gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
792 for (j = 0; j < num_gids; gid_entry_mbox++, j++) {
793 if (!memcmp(gid_entry_mbox->raw, zgid_entry.raw,
796 if (!memcmp(gid_entry_mbox->raw, gid_entry_tbl->raw,
797 sizeof(gid_entry_tbl->raw))) {
798 /* found duplicate */
799 mlx4_warn(dev, "requested gid entry for slave:%d "
800 "is a duplicate of gid at index %d\n",
802 mutex_unlock(&(priv->port[port].gid_table.mutex));
808 /* insert slave GIDs with memcpy, starting at slave's base index */
809 gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
810 for (i = 0, offset = base; i < num_gids; gid_entry_mbox++, offset++, i++)
811 memcpy(priv->port[port].gid_table.roce_gids[offset].raw,
812 gid_entry_mbox->raw, MLX4_ROCE_GID_ENTRY_SIZE);
814 /* Now, copy roce port gids table to current mailbox for passing to FW */
815 gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
816 for (i = 0; i < MLX4_ROCE_MAX_GIDS; gid_entry_mbox++, i++)
817 memcpy(gid_entry_mbox->raw,
818 priv->port[port].gid_table.roce_gids[i].raw,
819 MLX4_ROCE_GID_ENTRY_SIZE);
821 err = mlx4_cmd(dev, inbox->dma, in_mod & 0xffff, op_mod,
822 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
824 mutex_unlock(&(priv->port[port].gid_table.mutex));
828 return mlx4_cmd(dev, inbox->dma, in_mod & 0xffff, op_mod,
829 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
833 /* For IB, we only consider:
834 * - The capability mask, which is set to the aggregate of all
835 * slave function capabilities
836 * - The QKey violatin counter - reset according to each request.
839 if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
840 reset_qkey_viols = (*(u8 *) inbox->buf) & 0x40;
841 new_cap_mask = ((__be32 *) inbox->buf)[2];
843 reset_qkey_viols = ((u8 *) inbox->buf)[3] & 0x1;
844 new_cap_mask = ((__be32 *) inbox->buf)[1];
847 /* slave may not set the IS_SM capability for the port */
848 if (slave != mlx4_master_func_num(dev) &&
849 (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_IS_SM))
852 /* No DEV_MGMT in multifunc mode */
853 if (mlx4_is_mfunc(dev) &&
854 (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_DEV_MGMT_SUP))
859 priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
860 priv->mfunc.master.slave_state[slave].ib_cap_mask[port] = new_cap_mask;
861 for (i = 0; i < dev->num_slaves; i++)
863 priv->mfunc.master.slave_state[i].ib_cap_mask[port];
865 /* only clear mailbox for guests. Master may be setting
866 * MTU or PKEY table size
868 if (slave != dev->caps.function)
869 memset(inbox->buf, 0, 256);
870 if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
871 *(u8 *) inbox->buf |= !!reset_qkey_viols << 6;
872 ((__be32 *) inbox->buf)[2] = agg_cap_mask;
874 ((u8 *) inbox->buf)[3] |= !!reset_qkey_viols;
875 ((__be32 *) inbox->buf)[1] = agg_cap_mask;
878 err = mlx4_cmd(dev, inbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
879 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
881 priv->mfunc.master.slave_state[slave].ib_cap_mask[port] =
886 int mlx4_SET_PORT_wrapper(struct mlx4_dev *dev, int slave,
887 struct mlx4_vhcr *vhcr,
888 struct mlx4_cmd_mailbox *inbox,
889 struct mlx4_cmd_mailbox *outbox,
890 struct mlx4_cmd_info *cmd)
892 int port = mlx4_slave_convert_port(
893 dev, slave, vhcr->in_modifier & 0xFF);
898 vhcr->in_modifier = (vhcr->in_modifier & ~0xFF) |
901 return mlx4_common_set_port(dev, slave, vhcr->in_modifier,
902 vhcr->op_modifier, inbox);
905 /* bit locations for set port command with zero op modifier */
907 MLX4_SET_PORT_VL_CAP = 4, /* bits 7:4 */
908 MLX4_SET_PORT_MTU_CAP = 12, /* bits 15:12 */
909 MLX4_CHANGE_PORT_PKEY_TBL_SZ = 20,
910 MLX4_CHANGE_PORT_VL_CAP = 21,
911 MLX4_CHANGE_PORT_MTU_CAP = 22,
914 int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port, int pkey_tbl_sz)
916 struct mlx4_cmd_mailbox *mailbox;
917 int err, vl_cap, pkey_tbl_flag = 0;
919 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
922 mailbox = mlx4_alloc_cmd_mailbox(dev);
924 return PTR_ERR(mailbox);
926 ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
928 if (pkey_tbl_sz >= 0 && mlx4_is_master(dev)) {
930 ((__be16 *) mailbox->buf)[20] = cpu_to_be16(pkey_tbl_sz);
933 /* IB VL CAP enum isn't used by the firmware, just numerical values */
934 for (vl_cap = 8; vl_cap >= 1; vl_cap >>= 1) {
935 ((__be32 *) mailbox->buf)[0] = cpu_to_be32(
936 (1 << MLX4_CHANGE_PORT_MTU_CAP) |
937 (1 << MLX4_CHANGE_PORT_VL_CAP) |
938 (pkey_tbl_flag << MLX4_CHANGE_PORT_PKEY_TBL_SZ) |
939 (dev->caps.port_ib_mtu[port] << MLX4_SET_PORT_MTU_CAP) |
940 (vl_cap << MLX4_SET_PORT_VL_CAP));
941 err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
942 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
947 mlx4_free_cmd_mailbox(dev, mailbox);
951 int mlx4_SET_PORT_general(struct mlx4_dev *dev, u8 port, int mtu,
952 u8 pptx, u8 pfctx, u8 pprx, u8 pfcrx)
954 struct mlx4_cmd_mailbox *mailbox;
955 struct mlx4_set_port_general_context *context;
959 mailbox = mlx4_alloc_cmd_mailbox(dev);
961 return PTR_ERR(mailbox);
962 context = mailbox->buf;
963 context->flags = SET_PORT_GEN_ALL_VALID;
964 context->mtu = cpu_to_be16(mtu);
965 context->pptx = (pptx * (!pfctx)) << 7;
966 context->pfctx = pfctx;
967 context->pprx = (pprx * (!pfcrx)) << 7;
968 context->pfcrx = pfcrx;
970 in_mod = MLX4_SET_PORT_GENERAL << 8 | port;
971 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
972 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
974 mlx4_free_cmd_mailbox(dev, mailbox);
977 EXPORT_SYMBOL(mlx4_SET_PORT_general);
979 int mlx4_SET_PORT_qpn_calc(struct mlx4_dev *dev, u8 port, u32 base_qpn,
982 struct mlx4_cmd_mailbox *mailbox;
983 struct mlx4_set_port_rqp_calc_context *context;
986 u32 m_promisc = (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) ?
987 MCAST_DIRECT : MCAST_DEFAULT;
989 if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
992 mailbox = mlx4_alloc_cmd_mailbox(dev);
994 return PTR_ERR(mailbox);
995 context = mailbox->buf;
996 context->base_qpn = cpu_to_be32(base_qpn);
997 context->n_mac = dev->caps.log_num_macs;
998 context->promisc = cpu_to_be32(promisc << SET_PORT_PROMISC_SHIFT |
1000 context->mcast = cpu_to_be32(m_promisc << SET_PORT_MC_PROMISC_SHIFT |
1002 context->intra_no_vlan = 0;
1003 context->no_vlan = MLX4_NO_VLAN_IDX;
1004 context->intra_vlan_miss = 0;
1005 context->vlan_miss = MLX4_VLAN_MISS_IDX;
1007 in_mod = MLX4_SET_PORT_RQP_CALC << 8 | port;
1008 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
1009 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
1011 mlx4_free_cmd_mailbox(dev, mailbox);
1014 EXPORT_SYMBOL(mlx4_SET_PORT_qpn_calc);
1016 int mlx4_SET_PORT_PRIO2TC(struct mlx4_dev *dev, u8 port, u8 *prio2tc)
1018 struct mlx4_cmd_mailbox *mailbox;
1019 struct mlx4_set_port_prio2tc_context *context;
1024 mailbox = mlx4_alloc_cmd_mailbox(dev);
1025 if (IS_ERR(mailbox))
1026 return PTR_ERR(mailbox);
1027 context = mailbox->buf;
1028 for (i = 0; i < MLX4_NUM_UP; i += 2)
1029 context->prio2tc[i >> 1] = prio2tc[i] << 4 | prio2tc[i + 1];
1031 in_mod = MLX4_SET_PORT_PRIO2TC << 8 | port;
1032 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
1033 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
1035 mlx4_free_cmd_mailbox(dev, mailbox);
1038 EXPORT_SYMBOL(mlx4_SET_PORT_PRIO2TC);
1040 int mlx4_SET_PORT_SCHEDULER(struct mlx4_dev *dev, u8 port, u8 *tc_tx_bw,
1041 u8 *pg, u16 *ratelimit)
1043 struct mlx4_cmd_mailbox *mailbox;
1044 struct mlx4_set_port_scheduler_context *context;
1049 mailbox = mlx4_alloc_cmd_mailbox(dev);
1050 if (IS_ERR(mailbox))
1051 return PTR_ERR(mailbox);
1052 context = mailbox->buf;
1054 for (i = 0; i < MLX4_NUM_TC; i++) {
1055 struct mlx4_port_scheduler_tc_cfg_be *tc = &context->tc[i];
1056 u16 r = ratelimit && ratelimit[i] ? ratelimit[i] :
1057 MLX4_RATELIMIT_DEFAULT;
1059 tc->pg = htons(pg[i]);
1060 tc->bw_precentage = htons(tc_tx_bw[i]);
1062 tc->max_bw_units = htons(MLX4_RATELIMIT_UNITS);
1063 tc->max_bw_value = htons(r);
1066 in_mod = MLX4_SET_PORT_SCHEDULER << 8 | port;
1067 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
1068 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
1070 mlx4_free_cmd_mailbox(dev, mailbox);
1073 EXPORT_SYMBOL(mlx4_SET_PORT_SCHEDULER);
1076 VXLAN_ENABLE_MODIFY = 1 << 7,
1077 VXLAN_STEERING_MODIFY = 1 << 6,
1079 VXLAN_ENABLE = 1 << 7,
1082 struct mlx4_set_port_vxlan_context {
1090 int mlx4_SET_PORT_VXLAN(struct mlx4_dev *dev, u8 port, u8 steering, int enable)
1094 struct mlx4_cmd_mailbox *mailbox;
1095 struct mlx4_set_port_vxlan_context *context;
1097 mailbox = mlx4_alloc_cmd_mailbox(dev);
1098 if (IS_ERR(mailbox))
1099 return PTR_ERR(mailbox);
1100 context = mailbox->buf;
1101 memset(context, 0, sizeof(*context));
1103 context->modify_flags = VXLAN_ENABLE_MODIFY | VXLAN_STEERING_MODIFY;
1105 context->enable_flags = VXLAN_ENABLE;
1106 context->steering = steering;
1108 in_mod = MLX4_SET_PORT_VXLAN << 8 | port;
1109 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
1110 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
1112 mlx4_free_cmd_mailbox(dev, mailbox);
1115 EXPORT_SYMBOL(mlx4_SET_PORT_VXLAN);
1117 int mlx4_SET_MCAST_FLTR_wrapper(struct mlx4_dev *dev, int slave,
1118 struct mlx4_vhcr *vhcr,
1119 struct mlx4_cmd_mailbox *inbox,
1120 struct mlx4_cmd_mailbox *outbox,
1121 struct mlx4_cmd_info *cmd)
1128 int mlx4_SET_MCAST_FLTR(struct mlx4_dev *dev, u8 port,
1129 u64 mac, u64 clear, u8 mode)
1131 return mlx4_cmd(dev, (mac | (clear << 63)), port, mode,
1132 MLX4_CMD_SET_MCAST_FLTR, MLX4_CMD_TIME_CLASS_B,
1135 EXPORT_SYMBOL(mlx4_SET_MCAST_FLTR);
1137 int mlx4_SET_VLAN_FLTR_wrapper(struct mlx4_dev *dev, int slave,
1138 struct mlx4_vhcr *vhcr,
1139 struct mlx4_cmd_mailbox *inbox,
1140 struct mlx4_cmd_mailbox *outbox,
1141 struct mlx4_cmd_info *cmd)
1148 int mlx4_common_dump_eth_stats(struct mlx4_dev *dev, int slave,
1149 u32 in_mod, struct mlx4_cmd_mailbox *outbox)
1151 return mlx4_cmd_box(dev, 0, outbox->dma, in_mod, 0,
1152 MLX4_CMD_DUMP_ETH_STATS, MLX4_CMD_TIME_CLASS_B,
1156 int mlx4_DUMP_ETH_STATS_wrapper(struct mlx4_dev *dev, int slave,
1157 struct mlx4_vhcr *vhcr,
1158 struct mlx4_cmd_mailbox *inbox,
1159 struct mlx4_cmd_mailbox *outbox,
1160 struct mlx4_cmd_info *cmd)
1162 if (slave != dev->caps.function)
1164 return mlx4_common_dump_eth_stats(dev, slave,
1165 vhcr->in_modifier, outbox);
1168 void mlx4_set_stats_bitmap(struct mlx4_dev *dev, u64 *stats_bitmap)
1170 if (!mlx4_is_mfunc(dev)) {
1175 *stats_bitmap = (MLX4_STATS_TRAFFIC_COUNTERS_MASK |
1176 MLX4_STATS_TRAFFIC_DROPS_MASK |
1177 MLX4_STATS_PORT_COUNTERS_MASK);
1179 if (mlx4_is_master(dev))
1180 *stats_bitmap |= MLX4_STATS_ERROR_COUNTERS_MASK;
1182 EXPORT_SYMBOL(mlx4_set_stats_bitmap);
1184 int mlx4_get_slave_from_roce_gid(struct mlx4_dev *dev, int port, u8 *gid,
1187 struct mlx4_priv *priv = mlx4_priv(dev);
1188 int i, found_ix = -1;
1189 int vf_gids = MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS;
1190 struct mlx4_slaves_pport slaves_pport;
1194 if (!mlx4_is_mfunc(dev))
1197 slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
1198 num_vfs = bitmap_weight(slaves_pport.slaves, dev->num_vfs + 1) - 1;
1200 for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++) {
1201 if (!memcmp(priv->port[port].gid_table.roce_gids[i].raw, gid,
1202 MLX4_ROCE_GID_ENTRY_SIZE)) {
1208 if (found_ix >= 0) {
1209 /* Calculate a slave_gid which is the slave number in the gid
1210 * table and not a globally unique slave number.
1212 if (found_ix < MLX4_ROCE_PF_GIDS)
1214 else if (found_ix < MLX4_ROCE_PF_GIDS + (vf_gids % num_vfs) *
1215 (vf_gids / num_vfs + 1))
1216 slave_gid = ((found_ix - MLX4_ROCE_PF_GIDS) /
1217 (vf_gids / num_vfs + 1)) + 1;
1220 ((found_ix - MLX4_ROCE_PF_GIDS -
1221 ((vf_gids % num_vfs) * ((vf_gids / num_vfs + 1)))) /
1222 (vf_gids / num_vfs)) + vf_gids % num_vfs + 1;
1224 /* Calculate the globally unique slave id */
1226 struct mlx4_active_ports exclusive_ports;
1227 struct mlx4_active_ports actv_ports;
1228 struct mlx4_slaves_pport slaves_pport_actv;
1229 unsigned max_port_p_one;
1230 int num_vfs_before = 0;
1231 int candidate_slave_gid;
1233 /* Calculate how many VFs are on the previous port, if exists */
1234 for (i = 1; i < port; i++) {
1235 bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
1236 set_bit(i - 1, exclusive_ports.ports);
1238 mlx4_phys_to_slaves_pport_actv(
1239 dev, &exclusive_ports);
1240 num_vfs_before += bitmap_weight(
1241 slaves_pport_actv.slaves,
1245 /* candidate_slave_gid isn't necessarily the correct slave, but
1246 * it has the same number of ports and is assigned to the same
1247 * ports as the real slave we're looking for. On dual port VF,
1248 * slave_gid = [single port VFs on port <port>] +
1249 * [offset of the current slave from the first dual port VF] +
1252 candidate_slave_gid = slave_gid + num_vfs_before;
1254 actv_ports = mlx4_get_active_ports(dev, candidate_slave_gid);
1255 max_port_p_one = find_first_bit(
1256 actv_ports.ports, dev->caps.num_ports) +
1257 bitmap_weight(actv_ports.ports,
1258 dev->caps.num_ports) + 1;
1260 /* Calculate the real slave number */
1261 for (i = 1; i < max_port_p_one; i++) {
1264 bitmap_zero(exclusive_ports.ports,
1265 dev->caps.num_ports);
1266 set_bit(i - 1, exclusive_ports.ports);
1268 mlx4_phys_to_slaves_pport_actv(
1269 dev, &exclusive_ports);
1270 slave_gid += bitmap_weight(
1271 slaves_pport_actv.slaves,
1275 *slave_id = slave_gid;
1278 return (found_ix >= 0) ? 0 : -EINVAL;
1280 EXPORT_SYMBOL(mlx4_get_slave_from_roce_gid);
1282 int mlx4_get_roce_gid_from_slave(struct mlx4_dev *dev, int port, int slave_id,
1285 struct mlx4_priv *priv = mlx4_priv(dev);
1287 if (!mlx4_is_master(dev))
1290 memcpy(gid, priv->port[port].gid_table.roce_gids[slave_id].raw,
1291 MLX4_ROCE_GID_ENTRY_SIZE);
1294 EXPORT_SYMBOL(mlx4_get_roce_gid_from_slave);