2 * Copyright (c) 2007, 2014 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 #define LINUXKPI_PARAM_PREFIX mlx4_
35 #include <linux/errno.h>
36 #include <linux/if_ether.h>
37 #include <linux/module.h>
38 #include <linux/err.h>
40 #include <dev/mlx4/cmd.h>
41 #include <linux/moduleparam.h>
43 #include <dev/mlx4/stats.h>
46 int mlx4_set_4k_mtu = -1;
47 module_param_named(set_4k_mtu, mlx4_set_4k_mtu, int, 0444);
48 MODULE_PARM_DESC(set_4k_mtu,
49 "(Obsolete) attempt to set 4K MTU to all ConnectX ports");
52 #define MLX4_MAC_VALID (1ull << 63)
54 #define MLX4_VLAN_VALID (1u << 31)
55 #define MLX4_VLAN_MASK 0xfff
57 void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
61 mutex_init(&table->mutex);
62 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
63 table->entries[i] = 0;
66 table->max = 1 << dev->caps.log_num_macs;
70 void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
74 mutex_init(&table->mutex);
75 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
76 table->entries[i] = 0;
79 table->max = (1 << dev->caps.log_num_vlans) - MLX4_VLAN_REGULAR;
83 static int validate_index(struct mlx4_dev *dev,
84 struct mlx4_mac_table *table, int index)
88 if (index < 0 || index >= table->max || !table->refs[index]) {
89 mlx4_warn(dev, "No valid Mac entry for the given index\n");
95 static int find_index(struct mlx4_dev *dev,
96 struct mlx4_mac_table *table, u64 mac)
100 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
101 if ((mac & MLX4_MAC_MASK) ==
102 (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
109 static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
112 struct mlx4_cmd_mailbox *mailbox;
116 mailbox = mlx4_alloc_cmd_mailbox(dev);
118 return PTR_ERR(mailbox);
120 memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
122 in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
124 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
125 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
127 mlx4_free_cmd_mailbox(dev, mailbox);
131 int __mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
133 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
134 struct mlx4_mac_table *table = &info->mac_table;
138 mlx4_dbg(dev, "Registering MAC: 0x%llx for port %d\n",
139 (unsigned long long) mac, port);
141 mutex_lock(&table->mutex);
142 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
143 if (free < 0 && !table->refs[i]) {
148 if ((mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) &&
150 /* MAC already registered, Must not have duplicates */
157 mlx4_dbg(dev, "Free MAC index is %d\n", free);
159 if (table->total == table->max) {
160 /* No free mac entries */
165 /* Register new MAC */
166 table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
168 err = mlx4_set_port_mac_table(dev, port, table->entries);
170 mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
171 (unsigned long long) mac);
172 table->entries[free] = 0;
175 table->refs[free] = 1;
180 mutex_unlock(&table->mutex);
183 EXPORT_SYMBOL_GPL(__mlx4_register_mac);
185 int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
190 if (mlx4_is_mfunc(dev)) {
191 if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
192 err = mlx4_cmd_imm(dev, mac, &out_param,
193 ((u32) port) << 8 | (u32) RES_MAC,
194 RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
195 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
197 if (err && err == -EINVAL && mlx4_is_slave(dev)) {
198 /* retry using old REG_MAC format */
199 set_param_l(&out_param, port);
200 err = mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
201 RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
202 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
204 dev->flags |= MLX4_FLAG_OLD_REG_MAC;
209 return get_param_l(&out_param);
211 return __mlx4_register_mac(dev, port, mac);
213 EXPORT_SYMBOL_GPL(mlx4_register_mac);
215 int mlx4_get_base_qpn(struct mlx4_dev *dev, u8 port)
217 return dev->caps.reserved_qps_base[MLX4_QP_REGION_ETH_ADDR] +
218 (port - 1) * (1 << dev->caps.log_num_macs);
220 EXPORT_SYMBOL_GPL(mlx4_get_base_qpn);
222 void __mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
224 struct mlx4_port_info *info;
225 struct mlx4_mac_table *table;
228 if (port < 1 || port > dev->caps.num_ports) {
229 mlx4_warn(dev, "invalid port number (%d), aborting...\n", port);
232 info = &mlx4_priv(dev)->port[port];
233 table = &info->mac_table;
234 mutex_lock(&table->mutex);
236 index = find_index(dev, table, mac);
238 if (validate_index(dev, table, index))
241 if (--table->refs[index]) {
242 mlx4_dbg(dev, "Have more references for index %d,"
243 "no need to modify mac table\n", index);
247 table->entries[index] = 0;
248 mlx4_set_port_mac_table(dev, port, table->entries);
251 mutex_unlock(&table->mutex);
253 EXPORT_SYMBOL_GPL(__mlx4_unregister_mac);
255 void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
259 if (mlx4_is_mfunc(dev)) {
260 if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
261 (void) mlx4_cmd_imm(dev, mac, &out_param,
262 ((u32) port) << 8 | (u32) RES_MAC,
263 RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
264 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
266 /* use old unregister mac format */
267 set_param_l(&out_param, port);
268 (void) mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
269 RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
270 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
274 __mlx4_unregister_mac(dev, port, mac);
277 EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
279 int __mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac)
281 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
282 struct mlx4_mac_table *table = &info->mac_table;
283 int index = qpn - info->base_qpn;
286 /* CX1 doesn't support multi-functions */
287 mutex_lock(&table->mutex);
289 err = validate_index(dev, table, index);
293 table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
295 err = mlx4_set_port_mac_table(dev, port, table->entries);
297 mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
298 (unsigned long long) new_mac);
299 table->entries[index] = 0;
302 mutex_unlock(&table->mutex);
305 EXPORT_SYMBOL_GPL(__mlx4_replace_mac);
307 static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
310 struct mlx4_cmd_mailbox *mailbox;
314 mailbox = mlx4_alloc_cmd_mailbox(dev);
316 return PTR_ERR(mailbox);
318 memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
319 in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
320 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
321 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
323 mlx4_free_cmd_mailbox(dev, mailbox);
328 int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
330 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
333 for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
334 if (table->refs[i] &&
335 (vid == (MLX4_VLAN_MASK &
336 be32_to_cpu(table->entries[i])))) {
337 /* VLAN already registered, increase reference count */
345 EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
347 int __mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan,
350 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
354 mutex_lock(&table->mutex);
356 if (table->total == table->max) {
357 /* No free vlan entries */
362 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
363 if (free < 0 && (table->refs[i] == 0)) {
368 if (table->refs[i] &&
369 (vlan == (MLX4_VLAN_MASK &
370 be32_to_cpu(table->entries[i])))) {
371 /* Vlan already registered, increase references count */
383 /* Register new VLAN */
384 table->refs[free] = 1;
385 table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
387 err = mlx4_set_port_vlan_table(dev, port, table->entries);
389 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
390 table->refs[free] = 0;
391 table->entries[free] = 0;
398 mutex_unlock(&table->mutex);
402 int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
410 if (mlx4_is_mfunc(dev)) {
411 err = mlx4_cmd_imm(dev, vlan, &out_param,
412 ((u32) port) << 8 | (u32) RES_VLAN,
413 RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
414 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
416 *index = get_param_l(&out_param);
420 return __mlx4_register_vlan(dev, port, vlan, index);
422 EXPORT_SYMBOL_GPL(mlx4_register_vlan);
424 void __mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
426 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
429 mutex_lock(&table->mutex);
430 if (mlx4_find_cached_vlan(dev, port, vlan, &index)) {
431 mlx4_warn(dev, "vlan 0x%x is not in the vlan table\n", vlan);
435 if (index < MLX4_VLAN_REGULAR) {
436 mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
440 if (--table->refs[index]) {
441 mlx4_dbg(dev, "Have %d more references for index %d, "
442 "no need to modify vlan table\n", table->refs[index],
446 table->entries[index] = 0;
447 mlx4_set_port_vlan_table(dev, port, table->entries);
450 mutex_unlock(&table->mutex);
453 void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
457 if (mlx4_is_mfunc(dev)) {
458 (void) mlx4_cmd_imm(dev, vlan, &out_param,
459 ((u32) port) << 8 | (u32) RES_VLAN,
460 RES_OP_RESERVE_AND_MAP,
461 MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
465 __mlx4_unregister_vlan(dev, port, vlan);
467 EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
469 int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
471 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
475 inmailbox = mlx4_alloc_cmd_mailbox(dev);
476 if (IS_ERR(inmailbox))
477 return PTR_ERR(inmailbox);
479 outmailbox = mlx4_alloc_cmd_mailbox(dev);
480 if (IS_ERR(outmailbox)) {
481 mlx4_free_cmd_mailbox(dev, inmailbox);
482 return PTR_ERR(outmailbox);
485 inbuf = inmailbox->buf;
486 outbuf = outmailbox->buf;
487 memset(inbuf, 0, 256);
488 memset(outbuf, 0, 256);
493 *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
494 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
496 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
497 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
500 *caps = *(__be32 *) (outbuf + 84);
501 mlx4_free_cmd_mailbox(dev, inmailbox);
502 mlx4_free_cmd_mailbox(dev, outmailbox);
505 static struct mlx4_roce_gid_entry zgid_entry;
507 int mlx4_get_slave_num_gids(struct mlx4_dev *dev, int slave)
510 return MLX4_ROCE_PF_GIDS;
511 if (slave <= ((MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) % dev->num_vfs))
512 return ((MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) / dev->num_vfs) + 1;
513 return (MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) / dev->num_vfs;
516 int mlx4_get_base_gid_ix(struct mlx4_dev *dev, int slave)
521 gids = MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS;
526 if (slave <= gids % vfs)
527 return MLX4_ROCE_PF_GIDS + ((gids / vfs) + 1) * (slave - 1);
529 return MLX4_ROCE_PF_GIDS + (gids % vfs) + ((gids / vfs) * (slave - 1));
532 static int mlx4_common_set_port(struct mlx4_dev *dev, int slave, u32 in_mod,
533 u8 op_mod, struct mlx4_cmd_mailbox *inbox)
535 struct mlx4_priv *priv = mlx4_priv(dev);
536 struct mlx4_port_info *port_info;
537 struct mlx4_mfunc_master_ctx *master = &priv->mfunc.master;
538 struct mlx4_slave_state *slave_st = &master->slave_state[slave];
539 struct mlx4_set_port_rqp_calc_context *qpn_context;
540 struct mlx4_set_port_general_context *gen_context;
541 struct mlx4_roce_gid_entry *gid_entry_tbl, *gid_entry_mbox, *gid_entry_mb1;
542 int reset_qkey_viols;
554 __be32 slave_cap_mask;
557 port = in_mod & 0xff;
558 in_modifier = (in_mod >> 8) & 0xff;
560 port_info = &priv->port[port];
565 /* Slaves cannot perform SET_PORT operations except changing MTU */
567 if (slave != dev->caps.function &&
568 in_modifier != MLX4_SET_PORT_GENERAL &&
569 in_modifier != MLX4_SET_PORT_GID_TABLE) {
570 mlx4_warn(dev, "denying SET_PORT for slave:%d,"
571 "port %d, config_select 0x%x\n",
572 slave, port, in_modifier);
575 switch (in_modifier) {
576 case MLX4_SET_PORT_RQP_CALC:
577 qpn_context = inbox->buf;
578 qpn_context->base_qpn =
579 cpu_to_be32(port_info->base_qpn);
580 qpn_context->n_mac = 0x7;
581 promisc = be32_to_cpu(qpn_context->promisc) >>
582 SET_PORT_PROMISC_SHIFT;
583 qpn_context->promisc = cpu_to_be32(
584 promisc << SET_PORT_PROMISC_SHIFT |
585 port_info->base_qpn);
586 promisc = be32_to_cpu(qpn_context->mcast) >>
587 SET_PORT_MC_PROMISC_SHIFT;
588 qpn_context->mcast = cpu_to_be32(
589 promisc << SET_PORT_MC_PROMISC_SHIFT |
590 port_info->base_qpn);
592 case MLX4_SET_PORT_GENERAL:
593 gen_context = inbox->buf;
594 /* Mtu is configured as the max MTU among all the
595 * the functions on the port. */
596 mtu = be16_to_cpu(gen_context->mtu);
597 mtu = min_t(int, mtu, dev->caps.eth_mtu_cap[port] +
598 ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
599 prev_mtu = slave_st->mtu[port];
600 slave_st->mtu[port] = mtu;
601 if (mtu > master->max_mtu[port])
602 master->max_mtu[port] = mtu;
603 if (mtu < prev_mtu && prev_mtu ==
604 master->max_mtu[port]) {
605 slave_st->mtu[port] = mtu;
606 master->max_mtu[port] = mtu;
607 for (i = 0; i < dev->num_slaves; i++) {
608 master->max_mtu[port] =
609 max(master->max_mtu[port],
610 master->slave_state[i].mtu[port]);
614 gen_context->mtu = cpu_to_be16(master->max_mtu[port]);
616 case MLX4_SET_PORT_GID_TABLE:
617 /* change to MULTIPLE entries: number of guest's gids
618 * need a FOR-loop here over number of gids the guest has.
619 * 1. Check no duplicates in gids passed by slave
621 num_gids = mlx4_get_slave_num_gids(dev, slave);
622 base = mlx4_get_base_gid_ix(dev, slave);
623 gid_entry_mbox = (struct mlx4_roce_gid_entry *) (inbox->buf);
624 for (i = 0; i < num_gids; gid_entry_mbox++, i++) {
625 if (!memcmp(gid_entry_mbox->raw, zgid_entry.raw,
628 gid_entry_mb1 = gid_entry_mbox + 1;
629 for (j = i + 1; j < num_gids; gid_entry_mb1++, j++) {
630 if (!memcmp(gid_entry_mb1->raw,
631 zgid_entry.raw, sizeof(zgid_entry)))
633 if (!memcmp(gid_entry_mb1->raw, gid_entry_mbox->raw,
634 sizeof(gid_entry_mbox->raw))) {
635 /* found duplicate */
641 /* 2. Check that do not have duplicates in OTHER
642 * entries in the port GID table
644 for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++) {
645 if (i >= base && i < base + num_gids)
646 continue; /* don't compare to slave's current gids */
647 gid_entry_tbl = &priv->roce_gids[port - 1][i];
648 if (!memcmp(gid_entry_tbl->raw, zgid_entry.raw, sizeof(zgid_entry)))
650 gid_entry_mbox = (struct mlx4_roce_gid_entry *) (inbox->buf);
651 for (j = 0; j < num_gids; gid_entry_mbox++, j++) {
652 if (!memcmp(gid_entry_mbox->raw, zgid_entry.raw,
655 if (!memcmp(gid_entry_mbox->raw, gid_entry_tbl->raw,
656 sizeof(gid_entry_tbl->raw))) {
657 /* found duplicate */
658 mlx4_warn(dev, "requested gid entry for slave:%d "
659 "is a duplicate of gid at index %d\n",
666 /* insert slave GIDs with memcpy, starting at slave's base index */
667 gid_entry_mbox = (struct mlx4_roce_gid_entry *) (inbox->buf);
668 for (i = 0, offset = base; i < num_gids; gid_entry_mbox++, offset++, i++)
669 memcpy(priv->roce_gids[port - 1][offset].raw, gid_entry_mbox->raw, 16);
671 /* Now, copy roce port gids table to current mailbox for passing to FW */
672 gid_entry_mbox = (struct mlx4_roce_gid_entry *) (inbox->buf);
673 for (i = 0; i < MLX4_ROCE_MAX_GIDS; gid_entry_mbox++, i++)
674 memcpy(gid_entry_mbox->raw, priv->roce_gids[port - 1][i].raw, 16);
678 return mlx4_cmd(dev, inbox->dma, in_mod & 0xffff, op_mod,
679 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
683 /* For IB, we only consider:
684 * - The capability mask, which is set to the aggregate of all
685 * slave function capabilities
686 * - The QKey violatin counter - reset according to each request.
689 if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
690 reset_qkey_viols = (*(u8 *) inbox->buf) & 0x40;
691 new_cap_mask = ((__be32 *) inbox->buf)[2];
693 reset_qkey_viols = ((u8 *) inbox->buf)[3] & 0x1;
694 new_cap_mask = ((__be32 *) inbox->buf)[1];
697 /* slave may not set the IS_SM capability for the port */
698 if (slave != mlx4_master_func_num(dev) &&
699 (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_IS_SM))
702 /* No DEV_MGMT in multifunc mode */
703 if (mlx4_is_mfunc(dev) &&
704 (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_DEV_MGMT_SUP))
709 priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
710 priv->mfunc.master.slave_state[slave].ib_cap_mask[port] = new_cap_mask;
711 for (i = 0; i < dev->num_slaves; i++)
713 priv->mfunc.master.slave_state[i].ib_cap_mask[port];
715 /* only clear mailbox for guests. Master may be setting
716 * MTU or PKEY table size
718 if (slave != dev->caps.function)
719 memset(inbox->buf, 0, 256);
720 if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
721 *(u8 *) inbox->buf |= !!reset_qkey_viols << 6;
722 ((__be32 *) inbox->buf)[2] = agg_cap_mask;
724 ((u8 *) inbox->buf)[3] |= !!reset_qkey_viols;
725 ((__be32 *) inbox->buf)[1] = agg_cap_mask;
728 err = mlx4_cmd(dev, inbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
729 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
731 priv->mfunc.master.slave_state[slave].ib_cap_mask[port] =
736 int mlx4_SET_PORT_wrapper(struct mlx4_dev *dev, int slave,
737 struct mlx4_vhcr *vhcr,
738 struct mlx4_cmd_mailbox *inbox,
739 struct mlx4_cmd_mailbox *outbox,
740 struct mlx4_cmd_info *cmd)
742 return mlx4_common_set_port(dev, slave, vhcr->in_modifier,
743 vhcr->op_modifier, inbox);
746 /* bit locations for set port command with zero op modifier */
748 MLX4_SET_PORT_VL_CAP = 4, /* bits 7:4 */
749 MLX4_SET_PORT_MTU_CAP = 12, /* bits 15:12 */
750 MLX4_CHANGE_PORT_PKEY_TBL_SZ = 20,
751 MLX4_CHANGE_PORT_VL_CAP = 21,
752 MLX4_CHANGE_PORT_MTU_CAP = 22,
755 int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port, int pkey_tbl_sz)
757 struct mlx4_cmd_mailbox *mailbox;
758 int err = -EINVAL, vl_cap, pkey_tbl_flag = 0;
761 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_NONE)
764 mailbox = mlx4_alloc_cmd_mailbox(dev);
766 return PTR_ERR(mailbox);
768 memset(mailbox->buf, 0, 256);
770 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH) {
771 in_mod = MLX4_SET_PORT_GENERAL << 8 | port;
772 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1,
773 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
776 ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
778 if (pkey_tbl_sz >= 0 && mlx4_is_master(dev)) {
780 ((__be16 *) mailbox->buf)[20] = cpu_to_be16(pkey_tbl_sz);
783 /* IB VL CAP enum isn't used by the firmware, just numerical values */
784 for (vl_cap = dev->caps.vl_cap[port];
785 vl_cap >= 1; vl_cap >>= 1) {
786 ((__be32 *) mailbox->buf)[0] = cpu_to_be32(
787 (1 << MLX4_CHANGE_PORT_MTU_CAP) |
788 (1 << MLX4_CHANGE_PORT_VL_CAP) |
789 (pkey_tbl_flag << MLX4_CHANGE_PORT_PKEY_TBL_SZ) |
790 (dev->caps.port_ib_mtu[port] << MLX4_SET_PORT_MTU_CAP) |
791 (vl_cap << MLX4_SET_PORT_VL_CAP));
792 err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
793 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
799 mlx4_free_cmd_mailbox(dev, mailbox);
803 int mlx4_SET_PORT_general(struct mlx4_dev *dev, u8 port, int mtu,
804 u8 pptx, u8 pfctx, u8 pprx, u8 pfcrx)
806 struct mlx4_cmd_mailbox *mailbox;
807 struct mlx4_set_port_general_context *context;
811 mailbox = mlx4_alloc_cmd_mailbox(dev);
813 return PTR_ERR(mailbox);
814 context = mailbox->buf;
815 memset(context, 0, sizeof *context);
817 context->flags = SET_PORT_GEN_ALL_VALID;
818 context->mtu = cpu_to_be16(mtu);
819 context->pptx = (pptx * (!pfctx)) << 7;
820 context->pfctx = pfctx;
821 context->pprx = (pprx * (!pfcrx)) << 7;
822 context->pfcrx = pfcrx;
824 in_mod = MLX4_SET_PORT_GENERAL << 8 | port;
825 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
826 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
828 mlx4_free_cmd_mailbox(dev, mailbox);
831 EXPORT_SYMBOL(mlx4_SET_PORT_general);
833 int mlx4_SET_PORT_qpn_calc(struct mlx4_dev *dev, u8 port, u32 base_qpn,
836 struct mlx4_cmd_mailbox *mailbox;
837 struct mlx4_set_port_rqp_calc_context *context;
840 u32 m_promisc = (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) ?
841 MCAST_DIRECT : MCAST_DEFAULT;
843 if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
846 mailbox = mlx4_alloc_cmd_mailbox(dev);
848 return PTR_ERR(mailbox);
849 context = mailbox->buf;
850 memset(context, 0, sizeof *context);
852 context->base_qpn = cpu_to_be32(base_qpn);
853 context->n_mac = dev->caps.log_num_macs;
854 context->promisc = cpu_to_be32(promisc << SET_PORT_PROMISC_SHIFT |
856 context->mcast = cpu_to_be32(m_promisc << SET_PORT_MC_PROMISC_SHIFT |
858 context->intra_no_vlan = 0;
859 context->no_vlan = MLX4_NO_VLAN_IDX;
860 context->intra_vlan_miss = 0;
861 context->vlan_miss = MLX4_VLAN_MISS_IDX;
863 in_mod = MLX4_SET_PORT_RQP_CALC << 8 | port;
864 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
865 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
867 mlx4_free_cmd_mailbox(dev, mailbox);
870 EXPORT_SYMBOL(mlx4_SET_PORT_qpn_calc);
872 int mlx4_SET_PORT_PRIO2TC(struct mlx4_dev *dev, u8 port, u8 *prio2tc)
874 struct mlx4_cmd_mailbox *mailbox;
875 struct mlx4_set_port_prio2tc_context *context;
880 mailbox = mlx4_alloc_cmd_mailbox(dev);
882 return PTR_ERR(mailbox);
883 context = mailbox->buf;
884 memset(context, 0, sizeof *context);
886 for (i = 0; i < MLX4_NUM_UP; i += 2)
887 context->prio2tc[i >> 1] = prio2tc[i] << 4 | prio2tc[i + 1];
889 in_mod = MLX4_SET_PORT_PRIO2TC << 8 | port;
890 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
891 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
893 mlx4_free_cmd_mailbox(dev, mailbox);
896 EXPORT_SYMBOL(mlx4_SET_PORT_PRIO2TC);
898 int mlx4_SET_PORT_SCHEDULER(struct mlx4_dev *dev, u8 port, u8 *tc_tx_bw,
899 u8 *pg, u16 *ratelimit)
901 struct mlx4_cmd_mailbox *mailbox;
902 struct mlx4_set_port_scheduler_context *context;
907 mailbox = mlx4_alloc_cmd_mailbox(dev);
909 return PTR_ERR(mailbox);
910 context = mailbox->buf;
911 memset(context, 0, sizeof *context);
913 for (i = 0; i < MLX4_NUM_TC; i++) {
914 struct mlx4_port_scheduler_tc_cfg_be *tc = &context->tc[i];
916 if (ratelimit && ratelimit[i]) {
917 if (ratelimit[i] <= MLX4_MAX_100M_UNITS_VAL) {
920 htons(MLX4_RATELIMIT_100M_UNITS);
924 htons(MLX4_RATELIMIT_1G_UNITS);
926 tc->max_bw_value = htons(r);
928 tc->max_bw_value = htons(MLX4_RATELIMIT_DEFAULT);
929 tc->max_bw_units = htons(MLX4_RATELIMIT_1G_UNITS);
932 tc->pg = htons(pg[i]);
933 tc->bw_precentage = htons(tc_tx_bw[i]);
936 in_mod = MLX4_SET_PORT_SCHEDULER << 8 | port;
937 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
938 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
940 mlx4_free_cmd_mailbox(dev, mailbox);
943 EXPORT_SYMBOL(mlx4_SET_PORT_SCHEDULER);
945 int mlx4_SET_MCAST_FLTR_wrapper(struct mlx4_dev *dev, int slave,
946 struct mlx4_vhcr *vhcr,
947 struct mlx4_cmd_mailbox *inbox,
948 struct mlx4_cmd_mailbox *outbox,
949 struct mlx4_cmd_info *cmd)
956 int mlx4_SET_MCAST_FLTR(struct mlx4_dev *dev, u8 port,
957 u64 mac, u64 clear, u8 mode)
959 return mlx4_cmd(dev, (mac | (clear << 63)), port, mode,
960 MLX4_CMD_SET_MCAST_FLTR, MLX4_CMD_TIME_CLASS_B,
963 EXPORT_SYMBOL(mlx4_SET_MCAST_FLTR);
965 int mlx4_SET_VLAN_FLTR_wrapper(struct mlx4_dev *dev, int slave,
966 struct mlx4_vhcr *vhcr,
967 struct mlx4_cmd_mailbox *inbox,
968 struct mlx4_cmd_mailbox *outbox,
969 struct mlx4_cmd_info *cmd)
976 int mlx4_DUMP_ETH_STATS_wrapper(struct mlx4_dev *dev, int slave,
977 struct mlx4_vhcr *vhcr,
978 struct mlx4_cmd_mailbox *inbox,
979 struct mlx4_cmd_mailbox *outbox,
980 struct mlx4_cmd_info *cmd)
985 void mlx4_set_stats_bitmap(struct mlx4_dev *dev, unsigned long *stats_bitmap)
989 bitmap_zero(stats_bitmap, NUM_ALL_STATS);
991 if (mlx4_is_slave(dev)) {
992 last_i = dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN ?
993 NUM_PKT_STATS + NUM_FLOW_STATS : NUM_PKT_STATS;
995 bitmap_set(stats_bitmap, last_i, NUM_PKT_STATS);
996 last_i = NUM_PKT_STATS;
998 if (dev->caps.flags2 &
999 MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN) {
1000 bitmap_set(stats_bitmap, last_i, NUM_FLOW_STATS);
1001 last_i += NUM_FLOW_STATS;
1005 if (mlx4_is_slave(dev))
1006 bitmap_set(stats_bitmap, last_i, NUM_VF_STATS);
1007 last_i += NUM_VF_STATS;
1009 if (mlx4_is_master(dev))
1010 bitmap_set(stats_bitmap, last_i, NUM_VPORT_STATS);
1011 last_i += NUM_VPORT_STATS;
1013 bitmap_set(stats_bitmap, last_i, NUM_PORT_STATS);
1015 EXPORT_SYMBOL(mlx4_set_stats_bitmap);
1017 int mlx4_get_slave_from_roce_gid(struct mlx4_dev *dev, int port, u8 *gid, int *slave_id)
1019 struct mlx4_priv *priv = mlx4_priv(dev);
1020 int i, found_ix = -1;
1021 int vf_gids = MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS;
1023 if (!mlx4_is_mfunc(dev))
1026 for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++) {
1027 if (!memcmp(priv->roce_gids[port - 1][i].raw, gid, 16)) {
1033 if (found_ix >= 0) {
1034 if (found_ix < MLX4_ROCE_PF_GIDS)
1036 else if (found_ix < MLX4_ROCE_PF_GIDS + (vf_gids % dev->num_vfs) *
1037 (vf_gids / dev->num_vfs + 1))
1038 *slave_id = ((found_ix - MLX4_ROCE_PF_GIDS) /
1039 (vf_gids / dev->num_vfs + 1)) + 1;
1042 ((found_ix - MLX4_ROCE_PF_GIDS -
1043 ((vf_gids % dev->num_vfs) * ((vf_gids / dev->num_vfs + 1)))) /
1044 (vf_gids / dev->num_vfs)) + vf_gids % dev->num_vfs + 1;
1047 return (found_ix >= 0) ? 0 : -EINVAL;
1049 EXPORT_SYMBOL(mlx4_get_slave_from_roce_gid);
1051 int mlx4_get_roce_gid_from_slave(struct mlx4_dev *dev, int port, int slave_id, u8 *gid)
1053 struct mlx4_priv *priv = mlx4_priv(dev);
1055 if (!mlx4_is_master(dev))
1058 memcpy(gid, priv->roce_gids[port - 1][slave_id].raw, 16);
1061 EXPORT_SYMBOL(mlx4_get_roce_gid_from_slave);
1063 /* Cable Module Info */
1064 #define MODULE_INFO_MAX_READ 48
1066 #define I2C_ADDR_LOW 0x50
1067 #define I2C_ADDR_HIGH 0x51
1068 #define I2C_PAGE_SIZE 256
1070 /* Module Info Data */
1071 struct mlx4_cable_info {
1074 __be16 dev_mem_address;
1077 __be32 reserved2[2];
1078 u8 data[MODULE_INFO_MAX_READ];
1081 enum cable_info_err {
1082 CABLE_INF_INV_PORT = 0x1,
1083 CABLE_INF_OP_NOSUP = 0x2,
1084 CABLE_INF_NOT_CONN = 0x3,
1085 CABLE_INF_NO_EEPRM = 0x4,
1086 CABLE_INF_PAGE_ERR = 0x5,
1087 CABLE_INF_INV_ADDR = 0x6,
1088 CABLE_INF_I2C_ADDR = 0x7,
1089 CABLE_INF_QSFP_VIO = 0x8,
1090 CABLE_INF_I2C_BUSY = 0x9,
1093 #define MAD_STATUS_2_CABLE_ERR(mad_status) ((mad_status >> 8) & 0xFF)
1096 static inline const char *cable_info_mad_err_str(u16 mad_status)
1098 u8 err = MAD_STATUS_2_CABLE_ERR(mad_status);
1101 case CABLE_INF_INV_PORT:
1102 return "invalid port selected";
1103 case CABLE_INF_OP_NOSUP:
1104 return "operation not supported for this port (the port is of type CX4 or internal)";
1105 case CABLE_INF_NOT_CONN:
1106 return "cable is not connected";
1107 case CABLE_INF_NO_EEPRM:
1108 return "the connected cable has no EPROM (passive copper cable)";
1109 case CABLE_INF_PAGE_ERR:
1110 return "page number is greater than 15";
1111 case CABLE_INF_INV_ADDR:
1112 return "invalid device_address or size (that is, size equals 0 or address+size is greater than 256)";
1113 case CABLE_INF_I2C_ADDR:
1114 return "invalid I2C slave address";
1115 case CABLE_INF_QSFP_VIO:
1116 return "at least one cable violates the QSFP specification and ignores the modsel signal";
1117 case CABLE_INF_I2C_BUSY:
1118 return "I2C bus is constantly busy";
1120 return "Unknown Error";
1125 * mlx4_get_module_info - Read cable module eeprom data
1127 * @port: port number.
1128 * @offset: byte offset in eeprom to start reading data from.
1129 * @size: num of bytes to read.
1130 * @data: output buffer to put the requested data into.
1132 * Reads cable module eeprom data, puts the outcome data into
1133 * data pointer paramer.
1134 * Returns num of read bytes on success or a negative error
1137 int mlx4_get_module_info(struct mlx4_dev *dev, u8 port, u16 offset,
1140 struct mlx4_cmd_mailbox *inbox, *outbox;
1141 struct mlx4_mad_ifc *inmad, *outmad;
1142 struct mlx4_cable_info *cable_info;
1146 if (size > MODULE_INFO_MAX_READ)
1147 size = MODULE_INFO_MAX_READ;
1149 inbox = mlx4_alloc_cmd_mailbox(dev);
1150 if (IS_ERR(inbox)) {
1152 "mlx4_alloc_cmd_mailbox returned with error(%lx)", PTR_ERR(inbox));
1153 return PTR_ERR(inbox);
1156 outbox = mlx4_alloc_cmd_mailbox(dev);
1157 if (IS_ERR(outbox)) {
1158 mlx4_free_cmd_mailbox(dev, inbox);
1160 "mlx4_alloc_cmd_mailbox returned with error(%lx)", PTR_ERR(outbox));
1161 return PTR_ERR(outbox);
1164 inmad = (struct mlx4_mad_ifc *)(inbox->buf);
1165 outmad = (struct mlx4_mad_ifc *)(outbox->buf);
1167 inmad->method = 0x1; /* Get */
1168 inmad->class_version = 0x1;
1169 inmad->mgmt_class = 0x1;
1170 inmad->base_version = 0x1;
1171 inmad->attr_id = cpu_to_be16(0xFF60); /* Module Info */
1173 if (offset < I2C_PAGE_SIZE && offset + size > I2C_PAGE_SIZE)
1174 /* Cross pages reads are not allowed
1175 * read until offset 256 in low page
1177 size -= offset + size - I2C_PAGE_SIZE;
1179 i2c_addr = I2C_ADDR_LOW;
1180 if (offset >= I2C_PAGE_SIZE) {
1181 /* Reset offset to high page */
1182 i2c_addr = I2C_ADDR_HIGH;
1183 offset -= I2C_PAGE_SIZE;
1186 cable_info = (struct mlx4_cable_info *)inmad->data;
1187 cable_info->dev_mem_address = cpu_to_be16(offset);
1188 cable_info->page_num = 0;
1189 cable_info->i2c_addr = i2c_addr;
1190 cable_info->size = cpu_to_be16(size);
1192 ret = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
1193 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
1197 if (be16_to_cpu(outmad->status)) {
1198 /* Mad returned with bad status */
1199 ret = be16_to_cpu(outmad->status);
1201 mlx4_warn(dev, "MLX4_CMD_MAD_IFC Get Module info attr(%x) "
1202 "port(%d) i2c_addr(%x) offset(%d) size(%d): Response "
1203 "Mad Status(%x) - %s\n", 0xFF60, port, i2c_addr, offset,
1204 size, ret, cable_info_mad_err_str(ret));
1206 if (i2c_addr == I2C_ADDR_HIGH &&
1207 MAD_STATUS_2_CABLE_ERR(ret) == CABLE_INF_I2C_ADDR)
1208 /* Some SFP cables do not support i2c slave
1209 * address 0x51 (high page), abort silently.
1216 cable_info = (struct mlx4_cable_info *)outmad->data;
1217 memcpy(data, cable_info->data, size);
1220 mlx4_free_cmd_mailbox(dev, inbox);
1221 mlx4_free_cmd_mailbox(dev, outbox);
1224 EXPORT_SYMBOL(mlx4_get_module_info);