main.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/errno.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/inetdevice.h>
  39. #include <linux/rtnetlink.h>
  40. #include <linux/if_vlan.h>
  41. #include <rdma/ib_smi.h>
  42. #include <rdma/ib_user_verbs.h>
  43. #include <rdma/ib_addr.h>
  44. #include <linux/mlx4/driver.h>
  45. #include <linux/mlx4/cmd.h>
  46. #include "mlx4_ib.h"
  47. #include "user.h"
  48. #define DRV_NAME "mlx4_ib"
  49. #define DRV_VERSION "1.0"
  50. #define DRV_RELDATE "April 4, 2008"
  51. MODULE_AUTHOR("Roland Dreier");
  52. MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
  53. MODULE_LICENSE("Dual BSD/GPL");
  54. MODULE_VERSION(DRV_VERSION);
  55. static const char mlx4_ib_version[] =
  56. DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
  57. DRV_VERSION " (" DRV_RELDATE ")\n";
  58. struct update_gid_work {
  59. struct work_struct work;
  60. union ib_gid gids[128];
  61. struct mlx4_ib_dev *dev;
  62. int port;
  63. };
  64. static struct workqueue_struct *wq;
  65. static void init_query_mad(struct ib_smp *mad)
  66. {
  67. mad->base_version = 1;
  68. mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  69. mad->class_version = 1;
  70. mad->method = IB_MGMT_METHOD_GET;
  71. }
  72. static union ib_gid zgid;
  73. static int mlx4_ib_query_device(struct ib_device *ibdev,
  74. struct ib_device_attr *props)
  75. {
  76. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  77. struct ib_smp *in_mad = NULL;
  78. struct ib_smp *out_mad = NULL;
  79. int err = -ENOMEM;
  80. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  81. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  82. if (!in_mad || !out_mad)
  83. goto out;
  84. init_query_mad(in_mad);
  85. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  86. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad, out_mad);
  87. if (err)
  88. goto out;
  89. memset(props, 0, sizeof *props);
  90. props->fw_ver = dev->dev->caps.fw_ver;
  91. props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
  92. IB_DEVICE_PORT_ACTIVE_EVENT |
  93. IB_DEVICE_SYS_IMAGE_GUID |
  94. IB_DEVICE_RC_RNR_NAK_GEN |
  95. IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
  96. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
  97. props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
  98. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
  99. props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
  100. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM)
  101. props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
  102. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
  103. props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
  104. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
  105. props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
  106. if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH)
  107. props->device_cap_flags |= IB_DEVICE_UD_TSO;
  108. if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
  109. props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
  110. if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
  111. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
  112. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
  113. props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
  114. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  115. 0xffffff;
  116. props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
  117. props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
  118. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  119. props->max_mr_size = ~0ull;
  120. props->page_size_cap = dev->dev->caps.page_size_cap;
  121. props->max_qp = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps;
  122. props->max_qp_wr = dev->dev->caps.max_wqes;
  123. props->max_sge = min(dev->dev->caps.max_sq_sg,
  124. dev->dev->caps.max_rq_sg);
  125. props->max_cq = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs;
  126. props->max_cqe = dev->dev->caps.max_cqes;
  127. props->max_mr = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws;
  128. props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
  129. props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
  130. props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
  131. props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
  132. props->max_srq = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs;
  133. props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
  134. props->max_srq_sge = dev->dev->caps.max_srq_sge;
  135. props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
  136. props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
  137. props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
  138. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  139. props->masked_atomic_cap = IB_ATOMIC_HCA;
  140. props->max_pkeys = dev->dev->caps.pkey_table_len[1];
  141. props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
  142. props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
  143. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  144. props->max_mcast_grp;
  145. props->max_map_per_fmr = (1 << (32 - ilog2(dev->dev->caps.num_mpts))) - 1;
  146. out:
  147. kfree(in_mad);
  148. kfree(out_mad);
  149. return err;
  150. }
  151. static enum rdma_link_layer
  152. mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
  153. {
  154. struct mlx4_dev *dev = to_mdev(device)->dev;
  155. return dev->caps.port_mask & (1 << (port_num - 1)) ?
  156. IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
  157. }
  158. static int ib_link_query_port(struct ib_device *ibdev, u8 port,
  159. struct ib_port_attr *props,
  160. struct ib_smp *out_mad)
  161. {
  162. props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
  163. props->lmc = out_mad->data[34] & 0x7;
  164. props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
  165. props->sm_sl = out_mad->data[36] & 0xf;
  166. props->state = out_mad->data[32] & 0xf;
  167. props->phys_state = out_mad->data[33] >> 4;
  168. props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
  169. props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
  170. props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
  171. props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
  172. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  173. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  174. props->active_width = out_mad->data[31] & 0xf;
  175. props->active_speed = out_mad->data[35] >> 4;
  176. props->max_mtu = out_mad->data[41] & 0xf;
  177. props->active_mtu = out_mad->data[36] >> 4;
  178. props->subnet_timeout = out_mad->data[51] & 0x1f;
  179. props->max_vl_num = out_mad->data[37] >> 4;
  180. props->init_type_reply = out_mad->data[41] >> 4;
  181. return 0;
  182. }
  183. static u8 state_to_phys_state(enum ib_port_state state)
  184. {
  185. return state == IB_PORT_ACTIVE ? 5 : 3;
  186. }
  187. static int eth_link_query_port(struct ib_device *ibdev, u8 port,
  188. struct ib_port_attr *props,
  189. struct ib_smp *out_mad)
  190. {
  191. struct mlx4_ib_iboe *iboe = &to_mdev(ibdev)->iboe;
  192. struct net_device *ndev;
  193. enum ib_mtu tmp;
  194. props->active_width = IB_WIDTH_1X;
  195. props->active_speed = 4;
  196. props->port_cap_flags = IB_PORT_CM_SUP;
  197. props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
  198. props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
  199. props->pkey_tbl_len = 1;
  200. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  201. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  202. props->max_mtu = IB_MTU_2048;
  203. props->subnet_timeout = 0;
  204. props->max_vl_num = out_mad->data[37] >> 4;
  205. props->init_type_reply = 0;
  206. props->state = IB_PORT_DOWN;
  207. props->phys_state = state_to_phys_state(props->state);
  208. props->active_mtu = IB_MTU_256;
  209. spin_lock(&iboe->lock);
  210. ndev = iboe->netdevs[port - 1];
  211. if (!ndev)
  212. goto out;
  213. tmp = iboe_get_mtu(ndev->mtu);
  214. props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
  215. props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
  216. IB_PORT_ACTIVE : IB_PORT_DOWN;
  217. props->phys_state = state_to_phys_state(props->state);
  218. out:
  219. spin_unlock(&iboe->lock);
  220. return 0;
  221. }
  222. static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
  223. struct ib_port_attr *props)
  224. {
  225. struct ib_smp *in_mad = NULL;
  226. struct ib_smp *out_mad = NULL;
  227. int err = -ENOMEM;
  228. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  229. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  230. if (!in_mad || !out_mad)
  231. goto out;
  232. memset(props, 0, sizeof *props);
  233. init_query_mad(in_mad);
  234. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  235. in_mad->attr_mod = cpu_to_be32(port);
  236. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  237. if (err)
  238. goto out;
  239. err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
  240. ib_link_query_port(ibdev, port, props, out_mad) :
  241. eth_link_query_port(ibdev, port, props, out_mad);
  242. out:
  243. kfree(in_mad);
  244. kfree(out_mad);
  245. return err;
  246. }
  247. static int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  248. union ib_gid *gid)
  249. {
  250. struct ib_smp *in_mad = NULL;
  251. struct ib_smp *out_mad = NULL;
  252. int err = -ENOMEM;
  253. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  254. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  255. if (!in_mad || !out_mad)
  256. goto out;
  257. init_query_mad(in_mad);
  258. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  259. in_mad->attr_mod = cpu_to_be32(port);
  260. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  261. if (err)
  262. goto out;
  263. memcpy(gid->raw, out_mad->data + 8, 8);
  264. init_query_mad(in_mad);
  265. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  266. in_mad->attr_mod = cpu_to_be32(index / 8);
  267. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  268. if (err)
  269. goto out;
  270. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  271. out:
  272. kfree(in_mad);
  273. kfree(out_mad);
  274. return err;
  275. }
  276. static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
  277. union ib_gid *gid)
  278. {
  279. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  280. *gid = dev->iboe.gid_table[port - 1][index];
  281. return 0;
  282. }
  283. static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  284. union ib_gid *gid)
  285. {
  286. if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
  287. return __mlx4_ib_query_gid(ibdev, port, index, gid);
  288. else
  289. return iboe_query_gid(ibdev, port, index, gid);
  290. }
  291. static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  292. u16 *pkey)
  293. {
  294. struct ib_smp *in_mad = NULL;
  295. struct ib_smp *out_mad = NULL;
  296. int err = -ENOMEM;
  297. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  298. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  299. if (!in_mad || !out_mad)
  300. goto out;
  301. init_query_mad(in_mad);
  302. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  303. in_mad->attr_mod = cpu_to_be32(index / 32);
  304. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  305. if (err)
  306. goto out;
  307. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  308. out:
  309. kfree(in_mad);
  310. kfree(out_mad);
  311. return err;
  312. }
  313. static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
  314. struct ib_device_modify *props)
  315. {
  316. struct mlx4_cmd_mailbox *mailbox;
  317. if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
  318. return -EOPNOTSUPP;
  319. if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
  320. return 0;
  321. spin_lock(&to_mdev(ibdev)->sm_lock);
  322. memcpy(ibdev->node_desc, props->node_desc, 64);
  323. spin_unlock(&to_mdev(ibdev)->sm_lock);
  324. /*
  325. * If possible, pass node desc to FW, so it can generate
  326. * a 144 trap. If cmd fails, just ignore.
  327. */
  328. mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
  329. if (IS_ERR(mailbox))
  330. return 0;
  331. memset(mailbox->buf, 0, 256);
  332. memcpy(mailbox->buf, props->node_desc, 64);
  333. mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
  334. MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A);
  335. mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
  336. return 0;
  337. }
  338. static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
  339. u32 cap_mask)
  340. {
  341. struct mlx4_cmd_mailbox *mailbox;
  342. int err;
  343. u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
  344. mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  345. if (IS_ERR(mailbox))
  346. return PTR_ERR(mailbox);
  347. memset(mailbox->buf, 0, 256);
  348. if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  349. *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
  350. ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
  351. } else {
  352. ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
  353. ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
  354. }
  355. err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
  356. MLX4_CMD_TIME_CLASS_B);
  357. mlx4_free_cmd_mailbox(dev->dev, mailbox);
  358. return err;
  359. }
  360. static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
  361. struct ib_port_modify *props)
  362. {
  363. struct ib_port_attr attr;
  364. u32 cap_mask;
  365. int err;
  366. mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
  367. err = mlx4_ib_query_port(ibdev, port, &attr);
  368. if (err)
  369. goto out;
  370. cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  371. ~props->clr_port_cap_mask;
  372. err = mlx4_SET_PORT(to_mdev(ibdev), port,
  373. !!(mask & IB_PORT_RESET_QKEY_CNTR),
  374. cap_mask);
  375. out:
  376. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  377. return err;
  378. }
  379. static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
  380. struct ib_udata *udata)
  381. {
  382. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  383. struct mlx4_ib_ucontext *context;
  384. struct mlx4_ib_alloc_ucontext_resp resp;
  385. int err;
  386. if (!dev->ib_active)
  387. return ERR_PTR(-EAGAIN);
  388. resp.qp_tab_size = dev->dev->caps.num_qps;
  389. resp.bf_reg_size = dev->dev->caps.bf_reg_size;
  390. resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
  391. context = kmalloc(sizeof *context, GFP_KERNEL);
  392. if (!context)
  393. return ERR_PTR(-ENOMEM);
  394. err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
  395. if (err) {
  396. kfree(context);
  397. return ERR_PTR(err);
  398. }
  399. INIT_LIST_HEAD(&context->db_page_list);
  400. mutex_init(&context->db_page_mutex);
  401. err = ib_copy_to_udata(udata, &resp, sizeof resp);
  402. if (err) {
  403. mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
  404. kfree(context);
  405. return ERR_PTR(-EFAULT);
  406. }
  407. return &context->ibucontext;
  408. }
  409. static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
  410. {
  411. struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
  412. mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
  413. kfree(context);
  414. return 0;
  415. }
  416. static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  417. {
  418. struct mlx4_ib_dev *dev = to_mdev(context->device);
  419. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  420. return -EINVAL;
  421. if (vma->vm_pgoff == 0) {
  422. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  423. if (io_remap_pfn_range(vma, vma->vm_start,
  424. to_mucontext(context)->uar.pfn,
  425. PAGE_SIZE, vma->vm_page_prot))
  426. return -EAGAIN;
  427. } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
  428. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  429. if (io_remap_pfn_range(vma, vma->vm_start,
  430. to_mucontext(context)->uar.pfn +
  431. dev->dev->caps.num_uars,
  432. PAGE_SIZE, vma->vm_page_prot))
  433. return -EAGAIN;
  434. } else
  435. return -EINVAL;
  436. return 0;
  437. }
  438. static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
  439. struct ib_ucontext *context,
  440. struct ib_udata *udata)
  441. {
  442. struct mlx4_ib_pd *pd;
  443. int err;
  444. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  445. if (!pd)
  446. return ERR_PTR(-ENOMEM);
  447. err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
  448. if (err) {
  449. kfree(pd);
  450. return ERR_PTR(err);
  451. }
  452. if (context)
  453. if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
  454. mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
  455. kfree(pd);
  456. return ERR_PTR(-EFAULT);
  457. }
  458. return &pd->ibpd;
  459. }
  460. static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
  461. {
  462. mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
  463. kfree(pd);
  464. return 0;
  465. }
  466. static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
  467. {
  468. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  469. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  470. struct mlx4_ib_gid_entry *ge;
  471. ge = kzalloc(sizeof *ge, GFP_KERNEL);
  472. if (!ge)
  473. return -ENOMEM;
  474. ge->gid = *gid;
  475. if (mlx4_ib_add_mc(mdev, mqp, gid)) {
  476. ge->port = mqp->port;
  477. ge->added = 1;
  478. }
  479. mutex_lock(&mqp->mutex);
  480. list_add_tail(&ge->list, &mqp->gid_list);
  481. mutex_unlock(&mqp->mutex);
  482. return 0;
  483. }
  484. int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
  485. union ib_gid *gid)
  486. {
  487. u8 mac[6];
  488. struct net_device *ndev;
  489. int ret = 0;
  490. if (!mqp->port)
  491. return 0;
  492. spin_lock(&mdev->iboe.lock);
  493. ndev = mdev->iboe.netdevs[mqp->port - 1];
  494. if (ndev)
  495. dev_hold(ndev);
  496. spin_unlock(&mdev->iboe.lock);
  497. if (ndev) {
  498. rdma_get_mcast_mac((struct in6_addr *)gid, mac);
  499. rtnl_lock();
  500. dev_mc_add(mdev->iboe.netdevs[mqp->port - 1], mac);
  501. ret = 1;
  502. rtnl_unlock();
  503. dev_put(ndev);
  504. }
  505. return ret;
  506. }
  507. static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  508. {
  509. int err;
  510. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  511. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  512. err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw,
  513. !!(mqp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
  514. MLX4_PROTOCOL_IB);
  515. if (err)
  516. return err;
  517. err = add_gid_entry(ibqp, gid);
  518. if (err)
  519. goto err_add;
  520. return 0;
  521. err_add:
  522. mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw, MLX4_PROTOCOL_IB);
  523. return err;
  524. }
  525. static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
  526. {
  527. struct mlx4_ib_gid_entry *ge;
  528. struct mlx4_ib_gid_entry *tmp;
  529. struct mlx4_ib_gid_entry *ret = NULL;
  530. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  531. if (!memcmp(raw, ge->gid.raw, 16)) {
  532. ret = ge;
  533. break;
  534. }
  535. }
  536. return ret;
  537. }
  538. static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  539. {
  540. int err;
  541. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  542. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  543. u8 mac[6];
  544. struct net_device *ndev;
  545. struct mlx4_ib_gid_entry *ge;
  546. err = mlx4_multicast_detach(mdev->dev,
  547. &mqp->mqp, gid->raw, MLX4_PROTOCOL_IB);
  548. if (err)
  549. return err;
  550. mutex_lock(&mqp->mutex);
  551. ge = find_gid_entry(mqp, gid->raw);
  552. if (ge) {
  553. spin_lock(&mdev->iboe.lock);
  554. ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
  555. if (ndev)
  556. dev_hold(ndev);
  557. spin_unlock(&mdev->iboe.lock);
  558. rdma_get_mcast_mac((struct in6_addr *)gid, mac);
  559. if (ndev) {
  560. rtnl_lock();
  561. dev_mc_del(mdev->iboe.netdevs[ge->port - 1], mac);
  562. rtnl_unlock();
  563. dev_put(ndev);
  564. }
  565. list_del(&ge->list);
  566. kfree(ge);
  567. } else
  568. printk(KERN_WARNING "could not find mgid entry\n");
  569. mutex_unlock(&mqp->mutex);
  570. return 0;
  571. }
  572. static int init_node_data(struct mlx4_ib_dev *dev)
  573. {
  574. struct ib_smp *in_mad = NULL;
  575. struct ib_smp *out_mad = NULL;
  576. int err = -ENOMEM;
  577. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  578. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  579. if (!in_mad || !out_mad)
  580. goto out;
  581. init_query_mad(in_mad);
  582. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  583. err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  584. if (err)
  585. goto out;
  586. memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
  587. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  588. err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  589. if (err)
  590. goto out;
  591. dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
  592. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  593. out:
  594. kfree(in_mad);
  595. kfree(out_mad);
  596. return err;
  597. }
  598. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  599. char *buf)
  600. {
  601. struct mlx4_ib_dev *dev =
  602. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  603. return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
  604. }
  605. static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
  606. char *buf)
  607. {
  608. struct mlx4_ib_dev *dev =
  609. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  610. return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
  611. (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
  612. (int) dev->dev->caps.fw_ver & 0xffff);
  613. }
  614. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  615. char *buf)
  616. {
  617. struct mlx4_ib_dev *dev =
  618. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  619. return sprintf(buf, "%x\n", dev->dev->rev_id);
  620. }
  621. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  622. char *buf)
  623. {
  624. struct mlx4_ib_dev *dev =
  625. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  626. return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
  627. dev->dev->board_id);
  628. }
  629. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  630. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  631. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  632. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  633. static struct device_attribute *mlx4_class_attributes[] = {
  634. &dev_attr_hw_rev,
  635. &dev_attr_fw_ver,
  636. &dev_attr_hca_type,
  637. &dev_attr_board_id
  638. };
  639. static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev)
  640. {
  641. memcpy(eui, dev->dev_addr, 3);
  642. memcpy(eui + 5, dev->dev_addr + 3, 3);
  643. if (vlan_id < 0x1000) {
  644. eui[3] = vlan_id >> 8;
  645. eui[4] = vlan_id & 0xff;
  646. } else {
  647. eui[3] = 0xff;
  648. eui[4] = 0xfe;
  649. }
  650. eui[0] ^= 2;
  651. }
  652. static void update_gids_task(struct work_struct *work)
  653. {
  654. struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
  655. struct mlx4_cmd_mailbox *mailbox;
  656. union ib_gid *gids;
  657. int err;
  658. struct mlx4_dev *dev = gw->dev->dev;
  659. struct ib_event event;
  660. mailbox = mlx4_alloc_cmd_mailbox(dev);
  661. if (IS_ERR(mailbox)) {
  662. printk(KERN_WARNING "update gid table failed %ld\n", PTR_ERR(mailbox));
  663. return;
  664. }
  665. gids = mailbox->buf;
  666. memcpy(gids, gw->gids, sizeof gw->gids);
  667. err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
  668. 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B);
  669. if (err)
  670. printk(KERN_WARNING "set port command failed\n");
  671. else {
  672. memcpy(gw->dev->iboe.gid_table[gw->port - 1], gw->gids, sizeof gw->gids);
  673. event.device = &gw->dev->ib_dev;
  674. event.element.port_num = gw->port;
  675. event.event = IB_EVENT_LID_CHANGE;
  676. ib_dispatch_event(&event);
  677. }
  678. mlx4_free_cmd_mailbox(dev, mailbox);
  679. kfree(gw);
  680. }
  681. static int update_ipv6_gids(struct mlx4_ib_dev *dev, int port, int clear)
  682. {
  683. struct net_device *ndev = dev->iboe.netdevs[port - 1];
  684. struct update_gid_work *work;
  685. struct net_device *tmp;
  686. int i;
  687. u8 *hits;
  688. int ret;
  689. union ib_gid gid;
  690. int free;
  691. int found;
  692. int need_update = 0;
  693. u16 vid;
  694. work = kzalloc(sizeof *work, GFP_ATOMIC);
  695. if (!work)
  696. return -ENOMEM;
  697. hits = kzalloc(128, GFP_ATOMIC);
  698. if (!hits) {
  699. ret = -ENOMEM;
  700. goto out;
  701. }
  702. rcu_read_lock();
  703. for_each_netdev_rcu(&init_net, tmp) {
  704. if (ndev && (tmp == ndev || rdma_vlan_dev_real_dev(tmp) == ndev)) {
  705. gid.global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
  706. vid = rdma_vlan_dev_vlan_id(tmp);
  707. mlx4_addrconf_ifid_eui48(&gid.raw[8], vid, ndev);
  708. found = 0;
  709. free = -1;
  710. for (i = 0; i < 128; ++i) {
  711. if (free < 0 &&
  712. !memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
  713. free = i;
  714. if (!memcmp(&dev->iboe.gid_table[port - 1][i], &gid, sizeof gid)) {
  715. hits[i] = 1;
  716. found = 1;
  717. break;
  718. }
  719. }
  720. if (!found) {
  721. if (tmp == ndev &&
  722. (memcmp(&dev->iboe.gid_table[port - 1][0],
  723. &gid, sizeof gid) ||
  724. !memcmp(&dev->iboe.gid_table[port - 1][0],
  725. &zgid, sizeof gid))) {
  726. dev->iboe.gid_table[port - 1][0] = gid;
  727. ++need_update;
  728. hits[0] = 1;
  729. } else if (free >= 0) {
  730. dev->iboe.gid_table[port - 1][free] = gid;
  731. hits[free] = 1;
  732. ++need_update;
  733. }
  734. }
  735. }
  736. }
  737. rcu_read_unlock();
  738. for (i = 0; i < 128; ++i)
  739. if (!hits[i]) {
  740. if (memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
  741. ++need_update;
  742. dev->iboe.gid_table[port - 1][i] = zgid;
  743. }
  744. if (need_update) {
  745. memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof work->gids);
  746. INIT_WORK(&work->work, update_gids_task);
  747. work->port = port;
  748. work->dev = dev;
  749. queue_work(wq, &work->work);
  750. } else
  751. kfree(work);
  752. kfree(hits);
  753. return 0;
  754. out:
  755. kfree(work);
  756. return ret;
  757. }
  758. static void handle_en_event(struct mlx4_ib_dev *dev, int port, unsigned long event)
  759. {
  760. switch (event) {
  761. case NETDEV_UP:
  762. case NETDEV_CHANGEADDR:
  763. update_ipv6_gids(dev, port, 0);
  764. break;
  765. case NETDEV_DOWN:
  766. update_ipv6_gids(dev, port, 1);
  767. dev->iboe.netdevs[port - 1] = NULL;
  768. }
  769. }
  770. static void netdev_added(struct mlx4_ib_dev *dev, int port)
  771. {
  772. update_ipv6_gids(dev, port, 0);
  773. }
  774. static void netdev_removed(struct mlx4_ib_dev *dev, int port)
  775. {
  776. update_ipv6_gids(dev, port, 1);
  777. }
  778. static int mlx4_ib_netdev_event(struct notifier_block *this, unsigned long event,
  779. void *ptr)
  780. {
  781. struct net_device *dev = ptr;
  782. struct mlx4_ib_dev *ibdev;
  783. struct net_device *oldnd;
  784. struct mlx4_ib_iboe *iboe;
  785. int port;
  786. if (!net_eq(dev_net(dev), &init_net))
  787. return NOTIFY_DONE;
  788. ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
  789. iboe = &ibdev->iboe;
  790. spin_lock(&iboe->lock);
  791. mlx4_foreach_ib_transport_port(port, ibdev->dev) {
  792. oldnd = iboe->netdevs[port - 1];
  793. iboe->netdevs[port - 1] =
  794. mlx4_get_protocol_dev(ibdev->dev, MLX4_PROTOCOL_EN, port);
  795. if (oldnd != iboe->netdevs[port - 1]) {
  796. if (iboe->netdevs[port - 1])
  797. netdev_added(ibdev, port);
  798. else
  799. netdev_removed(ibdev, port);
  800. }
  801. }
  802. if (dev == iboe->netdevs[0] ||
  803. (iboe->netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[0]))
  804. handle_en_event(ibdev, 1, event);
  805. else if (dev == iboe->netdevs[1]
  806. || (iboe->netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[1]))
  807. handle_en_event(ibdev, 2, event);
  808. spin_unlock(&iboe->lock);
  809. return NOTIFY_DONE;
  810. }
  811. static void *mlx4_ib_add(struct mlx4_dev *dev)
  812. {
  813. struct mlx4_ib_dev *ibdev;
  814. int num_ports = 0;
  815. int i;
  816. int err;
  817. struct mlx4_ib_iboe *iboe;
  818. printk_once(KERN_INFO "%s", mlx4_ib_version);
  819. mlx4_foreach_ib_transport_port(i, dev)
  820. num_ports++;
  821. /* No point in registering a device with no ports... */
  822. if (num_ports == 0)
  823. return NULL;
  824. ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
  825. if (!ibdev) {
  826. dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
  827. return NULL;
  828. }
  829. iboe = &ibdev->iboe;
  830. if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
  831. goto err_dealloc;
  832. if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
  833. goto err_pd;
  834. ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
  835. PAGE_SIZE);
  836. if (!ibdev->uar_map)
  837. goto err_uar;
  838. MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
  839. ibdev->dev = dev;
  840. strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
  841. ibdev->ib_dev.owner = THIS_MODULE;
  842. ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
  843. ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
  844. ibdev->num_ports = num_ports;
  845. ibdev->ib_dev.phys_port_cnt = ibdev->num_ports;
  846. ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
  847. ibdev->ib_dev.dma_device = &dev->pdev->dev;
  848. ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
  849. ibdev->ib_dev.uverbs_cmd_mask =
  850. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  851. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  852. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  853. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  854. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  855. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  856. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  857. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  858. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  859. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  860. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  861. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  862. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  863. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  864. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  865. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  866. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  867. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  868. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  869. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  870. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
  871. ibdev->ib_dev.query_device = mlx4_ib_query_device;
  872. ibdev->ib_dev.query_port = mlx4_ib_query_port;
  873. ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
  874. ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
  875. ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
  876. ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
  877. ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
  878. ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
  879. ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
  880. ibdev->ib_dev.mmap = mlx4_ib_mmap;
  881. ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
  882. ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
  883. ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
  884. ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
  885. ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
  886. ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
  887. ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
  888. ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
  889. ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
  890. ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
  891. ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
  892. ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
  893. ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
  894. ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
  895. ibdev->ib_dev.post_send = mlx4_ib_post_send;
  896. ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
  897. ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
  898. ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
  899. ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
  900. ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
  901. ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
  902. ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
  903. ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
  904. ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
  905. ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
  906. ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
  907. ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
  908. ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list;
  909. ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
  910. ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
  911. ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
  912. ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
  913. ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
  914. ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
  915. ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
  916. spin_lock_init(&iboe->lock);
  917. if (init_node_data(ibdev))
  918. goto err_map;
  919. spin_lock_init(&ibdev->sm_lock);
  920. mutex_init(&ibdev->cap_mask_mutex);
  921. if (ib_register_device(&ibdev->ib_dev, NULL))
  922. goto err_map;
  923. if (mlx4_ib_mad_init(ibdev))
  924. goto err_reg;
  925. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE && !iboe->nb.notifier_call) {
  926. iboe->nb.notifier_call = mlx4_ib_netdev_event;
  927. err = register_netdevice_notifier(&iboe->nb);
  928. if (err)
  929. goto err_reg;
  930. }
  931. for (i = 0; i < ARRAY_SIZE(mlx4_class_attributes); ++i) {
  932. if (device_create_file(&ibdev->ib_dev.dev,
  933. mlx4_class_attributes[i]))
  934. goto err_notif;
  935. }
  936. ibdev->ib_active = true;
  937. return ibdev;
  938. err_notif:
  939. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  940. printk(KERN_WARNING "failure unregistering notifier\n");
  941. flush_workqueue(wq);
  942. err_reg:
  943. ib_unregister_device(&ibdev->ib_dev);
  944. err_map:
  945. iounmap(ibdev->uar_map);
  946. err_uar:
  947. mlx4_uar_free(dev, &ibdev->priv_uar);
  948. err_pd:
  949. mlx4_pd_free(dev, ibdev->priv_pdn);
  950. err_dealloc:
  951. ib_dealloc_device(&ibdev->ib_dev);
  952. return NULL;
  953. }
  954. static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
  955. {
  956. struct mlx4_ib_dev *ibdev = ibdev_ptr;
  957. int p;
  958. mlx4_ib_mad_cleanup(ibdev);
  959. ib_unregister_device(&ibdev->ib_dev);
  960. if (ibdev->iboe.nb.notifier_call) {
  961. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  962. printk(KERN_WARNING "failure unregistering notifier\n");
  963. ibdev->iboe.nb.notifier_call = NULL;
  964. }
  965. iounmap(ibdev->uar_map);
  966. mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
  967. mlx4_CLOSE_PORT(dev, p);
  968. mlx4_uar_free(dev, &ibdev->priv_uar);
  969. mlx4_pd_free(dev, ibdev->priv_pdn);
  970. ib_dealloc_device(&ibdev->ib_dev);
  971. }
  972. static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
  973. enum mlx4_dev_event event, int port)
  974. {
  975. struct ib_event ibev;
  976. struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
  977. if (port > ibdev->num_ports)
  978. return;
  979. switch (event) {
  980. case MLX4_DEV_EVENT_PORT_UP:
  981. ibev.event = IB_EVENT_PORT_ACTIVE;
  982. break;
  983. case MLX4_DEV_EVENT_PORT_DOWN:
  984. ibev.event = IB_EVENT_PORT_ERR;
  985. break;
  986. case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
  987. ibdev->ib_active = false;
  988. ibev.event = IB_EVENT_DEVICE_FATAL;
  989. break;
  990. default:
  991. return;
  992. }
  993. ibev.device = ibdev_ptr;
  994. ibev.element.port_num = port;
  995. ib_dispatch_event(&ibev);
  996. }
  997. static struct mlx4_interface mlx4_ib_interface = {
  998. .add = mlx4_ib_add,
  999. .remove = mlx4_ib_remove,
  1000. .event = mlx4_ib_event,
  1001. .protocol = MLX4_PROTOCOL_IB
  1002. };
  1003. static int __init mlx4_ib_init(void)
  1004. {
  1005. int err;
  1006. wq = create_singlethread_workqueue("mlx4_ib");
  1007. if (!wq)
  1008. return -ENOMEM;
  1009. err = mlx4_register_interface(&mlx4_ib_interface);
  1010. if (err) {
  1011. destroy_workqueue(wq);
  1012. return err;
  1013. }
  1014. return 0;
  1015. }
  1016. static void __exit mlx4_ib_cleanup(void)
  1017. {
  1018. mlx4_unregister_interface(&mlx4_ib_interface);
  1019. destroy_workqueue(wq);
  1020. }
  1021. module_init(mlx4_ib_init);
  1022. module_exit(mlx4_ib_cleanup);