34 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/errno.h>
38 #include <linux/netdevice.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/if_vlan.h>
53 #define DRV_NAME MLX4_IB_DRV_NAME
54 #define DRV_VERSION "1.0"
55 #define DRV_RELDATE "April 4, 2008"
64 MODULE_PARM_DESC(sm_guid_assign,
"Enable SM alias_GUID assignment if sm_guid_assign > 0 (Default: 1)");
66 static const char mlx4_ib_version[] =
67 DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
77 static void do_slave_init(
struct mlx4_ib_dev *ibdev,
int slave,
int do_init);
81 static void init_query_mad(
struct ib_smp *
mad)
91 static int mlx4_ib_query_device(
struct ib_device *ibdev,
101 if (!in_mad || !out_mad)
104 init_query_mad(in_mad);
112 memset(props, 0,
sizeof *props);
149 props->
max_qp = dev->
dev->caps.num_qps - dev->
dev->caps.reserved_qps;
152 dev->
dev->caps.max_rq_sg);
153 props->
max_cq = dev->
dev->caps.num_cqs - dev->
dev->caps.reserved_cqs;
155 props->
max_mr = dev->
dev->caps.num_mpts - dev->
dev->caps.reserved_mrws;
156 props->
max_pd = dev->
dev->caps.num_pds - dev->
dev->caps.reserved_pds;
160 props->
max_srq = dev->
dev->caps.num_srqs - dev->
dev->caps.reserved_srqs;
196 int ext_active_speed;
202 if (!in_mad || !out_mad)
205 init_query_mad(in_mad);
209 if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
219 props->
lmc = out_mad->
data[34] & 0x7;
229 props->
max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
243 ext_active_speed = out_mad->
data[62] >> 4;
245 switch (ext_active_speed) {
257 init_query_mad(in_mad);
267 if (out_mad->
data[15] & 0x1)
286 static int eth_link_query_port(
struct ib_device *ibdev,
u8 port,
299 return PTR_ERR(mailbox);
301 err = mlx4_cmd_box(mdev->
dev, 0, mailbox->
dma, port, 0,
319 spin_lock(&iboe->
lock);
320 ndev = iboe->
netdevs[port - 1];
324 tmp = iboe_get_mtu(ndev->
mtu);
327 props->
state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
331 spin_unlock(&iboe->
lock);
342 memset(props, 0,
sizeof *props);
345 ib_link_query_port(ibdev, port, props, netw_view) :
346 eth_link_query_port(ibdev, port, props, netw_view);
351 static int mlx4_ib_query_port(
struct ib_device *ibdev,
u8 port,
370 if (!in_mad || !out_mad)
373 init_query_mad(in_mad);
377 if (mlx4_is_mfunc(dev->
dev) && netw_view)
386 if (mlx4_is_mfunc(dev->
dev) && !netw_view) {
395 init_query_mad(in_mad);
419 *gid = dev->
iboe.gid_table[port - 1][
index];
430 return iboe_query_gid(ibdev, port, index, gid);
443 if (!in_mad || !out_mad)
446 init_query_mad(in_mad);
450 if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
471 static int mlx4_ib_modify_device(
struct ib_device *ibdev,
int mask,
483 if (mlx4_is_slave(to_mdev(ibdev)->dev))
488 spin_unlock_irqrestore(&to_mdev(ibdev)->sm_lock, flags);
508 static int mlx4_SET_PORT(
struct mlx4_ib_dev *dev,
u8 port,
int reset_qkey_viols,
517 return PTR_ERR(mailbox);
522 *(
u8 *) mailbox->
buf = !!reset_qkey_viols << 6;
525 ((
u8 *) mailbox->
buf)[3] = !!reset_qkey_viols;
536 static int mlx4_ib_modify_port(
struct ib_device *ibdev,
u8 port,
int mask,
545 err = mlx4_ib_query_port(ibdev, port, &
attr);
552 err = mlx4_SET_PORT(to_mdev(ibdev),
port,
572 resp.qp_tab_size = dev->
dev->caps.num_qps;
573 resp.bf_reg_size = dev->
dev->caps.bf_reg_size;
574 resp.bf_regs_per_page = dev->
dev->caps.bf_regs_per_page;
589 err = ib_copy_to_udata(udata, &
resp,
sizeof resp);
599 static int mlx4_ib_dealloc_ucontext(
struct ib_ucontext *ibcontext)
620 to_mucontext(context)->uar.pfn,
623 }
else if (vma->
vm_pgoff == 1 && dev->
dev->caps.bf_reg_size != 0) {
627 to_mucontext(context)->uar.pfn +
628 dev->
dev->caps.num_uars,
664 static int mlx4_ib_dealloc_pd(
struct ib_pd *pd)
691 if (IS_ERR(xrcd->
pd)) {
692 err = PTR_ERR(xrcd->
pd);
697 if (IS_ERR(xrcd->
cq)) {
698 err = PTR_ERR(xrcd->
cq);
713 static int mlx4_ib_dealloc_xrcd(
struct ib_xrcd *xrcd)
723 static int add_gid_entry(
struct ib_qp *ibqp,
union ib_gid *gid)
756 spin_lock(&mdev->
iboe.lock);
757 ndev = mdev->
iboe.netdevs[mqp->
port - 1];
760 spin_unlock(&mdev->
iboe.lock);
763 rdma_get_mcast_mac((
struct in6_addr *)gid, mac);
788 if (mdev->
dev->caps.steering_mode ==
802 err = add_gid_entry(ibqp, gid);
840 static int mlx4_ib_mcg_detach(
struct ib_qp *ibqp,
union ib_gid *gid,
u16 lid)
850 if (mdev->
dev->caps.steering_mode ==
863 pr_err(
"Couldn't find reg_id for mgid. Steering rule is left attached\n");
866 reg_id = ib_steering->
reg_id;
876 ge = find_gid_entry(mqp, gid->
raw);
878 spin_lock(&mdev->
iboe.lock);
882 spin_unlock(&mdev->
iboe.lock);
883 rdma_get_mcast_mac((
struct in6_addr *)gid, mac);
893 pr_warn(
"could not find mgid entry\n");
909 if (!in_mad || !out_mad)
912 init_query_mad(in_mad);
914 if (mlx4_is_master(dev->
dev))
943 return sprintf(buf,
"MT%d\n", dev->
dev->pdev->device);
951 return sprintf(buf,
"%d.%d.%d\n", (
int) (dev->
dev->caps.fw_ver >> 32),
952 (
int) (dev->
dev->caps.fw_ver >> 16) & 0xffff,
953 (
int) dev->
dev->caps.fw_ver & 0xffff);
961 return sprintf(buf,
"%x\n", dev->
dev->rev_id);
989 if (vlan_id < 0x1000) {
990 eui[3] = vlan_id >> 8;
991 eui[4] = vlan_id & 0xff;
1008 if (IS_ERR(mailbox)) {
1009 pr_warn(
"update gid table failed %ld\n", PTR_ERR(mailbox));
1013 gids = mailbox->
buf;
1020 pr_warn(
"set port command failed\n");
1041 int need_update = 0;
1056 if (ndev && (tmp == ndev || rdma_vlan_dev_real_dev(tmp) == ndev)) {
1058 vid = rdma_vlan_dev_vlan_id(tmp);
1059 mlx4_addrconf_ifid_eui48(&gid.
raw[8], vid, ndev);
1062 for (i = 0; i < 128; ++
i) {
1064 !
memcmp(&dev->
iboe.gid_table[port - 1][i], &zgid,
sizeof zgid))
1066 if (!
memcmp(&dev->
iboe.gid_table[port - 1][i], &gid,
sizeof gid)) {
1076 &gid,
sizeof gid) ||
1078 &zgid,
sizeof gid))) {
1079 dev->
iboe.gid_table[port - 1][0] =
gid;
1082 }
else if (free >= 0) {
1092 for (i = 0; i < 128; ++
i)
1094 if (
memcmp(&dev->
iboe.gid_table[port - 1][i], &zgid,
sizeof zgid))
1096 dev->
iboe.gid_table[port - 1][
i] = zgid;
1116 static void handle_en_event(
struct mlx4_ib_dev *dev,
int port,
unsigned long event)
1121 update_ipv6_gids(dev, port, 0);
1125 update_ipv6_gids(dev, port, 1);
1126 dev->
iboe.netdevs[port - 1] =
NULL;
1130 static void netdev_added(
struct mlx4_ib_dev *dev,
int port)
1132 update_ipv6_gids(dev, port, 0);
1135 static void netdev_removed(
struct mlx4_ib_dev *dev,
int port)
1137 update_ipv6_gids(dev, port, 1);
1140 static int mlx4_ib_netdev_event(
struct notifier_block *
this,
unsigned long event,
1149 if (!net_eq(dev_net(dev), &
init_net))
1153 iboe = &ibdev->
iboe;
1155 spin_lock(&iboe->
lock);
1157 oldnd = iboe->
netdevs[port - 1];
1160 if (oldnd != iboe->
netdevs[port - 1]) {
1162 netdev_added(ibdev, port);
1164 netdev_removed(ibdev, port);
1168 if (dev == iboe->
netdevs[0] ||
1169 (iboe->
netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->
netdevs[0]))
1170 handle_en_event(ibdev, 1, event);
1171 else if (dev == iboe->
netdevs[1]
1172 || (iboe->
netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->
netdevs[1]))
1173 handle_en_event(ibdev, 2, event);
1175 spin_unlock(&iboe->
lock);
1186 if (mlx4_is_master(ibdev->
dev)) {
1187 for (slave = 0; slave <= ibdev->
dev->num_vfs; ++slave) {
1188 for (port = 1; port <= ibdev->
dev->caps.num_ports; ++
port) {
1190 i < ibdev->
dev->phys_caps.pkey_phys_table_len[
port];
1192 ibdev->
pkeys.virt2phys_pkey[slave][port - 1][
i] =
1194 (slave == mlx4_master_func_num(ibdev->
dev) || !
i) ? i :
1195 ibdev->
dev->phys_caps.pkey_phys_table_len[port] - 1;
1197 ibdev->
pkeys.virt2phys_pkey[slave][port - 1][i]);
1202 for (port = 1; port <= ibdev->
dev->caps.num_ports; ++
port) {
1204 i < ibdev->
dev->phys_caps.pkey_phys_table_len[
port];
1206 ibdev->
pkeys.phys_pkey_cache[port-1][i] =
1215 int eq_per_port = 0;
1221 if (dev->
caps.comp_pool == 0 ||
1222 dev->
caps.num_ports > dev->
caps.comp_pool)
1226 dev->
caps.num_ports);
1231 added_eqs += eq_per_port;
1233 total_eqs = dev->
caps.num_comp_vectors + added_eqs;
1236 if (!ibdev->eq_table)
1239 ibdev->eq_added = added_eqs;
1243 for (j = 0; j < eq_per_port; j++) {
1244 sprintf(name,
"mlx4-ib-%d-%d@%s",
1245 i, j, dev->pdev->bus->name);
1250 pr_warn(
"Can't allocate EQ %d; reverting to legacy\n", eq);
1252 (eq % dev->caps.num_comp_vectors);
1259 for (i = 0, eq = added_eqs; i < dev->caps.num_comp_vectors; i++)
1263 ibdev->
ib_dev.num_comp_vectors = total_eqs;
1275 ibdev->
ib_dev.num_comp_vectors = dev->
caps.num_comp_vectors;
1278 for (i = 0; i < ibdev->
eq_added; i++) {
1288 static void *mlx4_ib_add(
struct mlx4_dev *dev)
1301 if (mlx4_is_mfunc(dev) && num_ports) {
1302 dev_err(&dev->
pdev->dev,
"RoCE is not supported over SRIOV as yet\n");
1316 dev_err(&dev->
pdev->dev,
"Device struct alloc failed\n");
1320 iboe = &ibdev->
iboe;
1339 ibdev->
ib_dev.local_dma_lkey = dev->
caps.reserved_lkey;
1342 ibdev->
ib_dev.num_comp_vectors = dev->
caps.num_comp_vectors;
1346 ibdev->
ib_dev.uverbs_cmd_mask =
1371 ibdev->
ib_dev.query_device = mlx4_ib_query_device;
1372 ibdev->
ib_dev.query_port = mlx4_ib_query_port;
1373 ibdev->
ib_dev.get_link_layer = mlx4_ib_port_link_layer;
1374 ibdev->
ib_dev.query_gid = mlx4_ib_query_gid;
1375 ibdev->
ib_dev.query_pkey = mlx4_ib_query_pkey;
1376 ibdev->
ib_dev.modify_device = mlx4_ib_modify_device;
1377 ibdev->
ib_dev.modify_port = mlx4_ib_modify_port;
1378 ibdev->
ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
1379 ibdev->
ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
1380 ibdev->
ib_dev.mmap = mlx4_ib_mmap;
1381 ibdev->
ib_dev.alloc_pd = mlx4_ib_alloc_pd;
1382 ibdev->
ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
1409 ibdev->
ib_dev.attach_mcast = mlx4_ib_mcg_attach;
1410 ibdev->
ib_dev.detach_mcast = mlx4_ib_mcg_detach;
1413 if (!mlx4_is_slave(ibdev->
dev)) {
1421 ibdev->
ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
1422 ibdev->
ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
1423 ibdev->
ib_dev.uverbs_cmd_mask |=
1428 mlx4_ib_alloc_eqs(dev, ibdev);
1432 if (init_node_data(ibdev))
1436 if (mlx4_ib_port_link_layer(&ibdev->
ib_dev, i + 1) ==
1458 iboe->
nb.notifier_call = mlx4_ib_netdev_event;
1464 for (j = 0; j <
ARRAY_SIZE(mlx4_class_attributes); ++
j) {
1466 mlx4_class_attributes[j]))
1472 if (mlx4_is_mfunc(ibdev->
dev))
1476 if (mlx4_is_master(ibdev->
dev)) {
1478 if (j == mlx4_master_func_num(ibdev->
dev))
1481 do_slave_init(ibdev, j, 1);
1488 pr_warn(
"failure unregistering notifier\n");
1520 static void mlx4_ib_remove(
struct mlx4_dev *dev,
void *ibdev_ptr)
1528 if (ibdev->
iboe.nb.notifier_call) {
1530 pr_warn(
"failure unregistering notifier\n");
1531 ibdev->
iboe.nb.notifier_call =
NULL;
1540 mlx4_ib_free_eqs(dev, ibdev);
1552 unsigned long flags;
1554 if (!mlx4_is_master(dev))
1559 pr_err(
"failed to allocate memory for tunneling qp update\n");
1563 for (i = 0; i < dev->
caps.num_ports; i++) {
1566 pr_err(
"failed to allocate memory for tunneling qp update work struct\n");
1567 for (i = 0; i < dev->
caps.num_ports; i++) {
1575 for (i = 0; i < dev->
caps.num_ports; i++) {
1577 dm[
i]->
port = i + 1;
1582 if (!ibdev->sriov.is_going_down)
1584 spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
1592 static void mlx4_ib_event(
struct mlx4_dev *dev,
void *ibdev_ptr,
1610 if (mlx4_is_master(dev) &&
1632 pr_err(
"failed to allocate memory for events work\n");
1640 if (mlx4_is_master(dev))
1648 do_slave_init(ibdev, p, 1);
1653 do_slave_init(ibdev, p, 0);
1660 ibev.device = ibdev_ptr;
1661 ibev.element.port_num = (
u8) p;
1668 .remove = mlx4_ib_remove,
1669 .event = mlx4_ib_event,
1673 static int __init mlx4_ib_init(
void)
1699 static void __exit mlx4_ib_cleanup(
void)