main.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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_DRV_NAME
  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), MLX4_MAD_IFC_IGNORE_KEYS,
  87. 1, NULL, NULL, in_mad, out_mad);
  88. if (err)
  89. goto out;
  90. memset(props, 0, sizeof *props);
  91. props->fw_ver = dev->dev->caps.fw_ver;
  92. props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
  93. IB_DEVICE_PORT_ACTIVE_EVENT |
  94. IB_DEVICE_SYS_IMAGE_GUID |
  95. IB_DEVICE_RC_RNR_NAK_GEN |
  96. IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
  97. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
  98. props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
  99. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
  100. props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
  101. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM)
  102. props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
  103. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
  104. props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
  105. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
  106. props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
  107. if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH)
  108. props->device_cap_flags |= IB_DEVICE_UD_TSO;
  109. if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
  110. props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
  111. if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
  112. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
  113. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
  114. props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
  115. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
  116. props->device_cap_flags |= IB_DEVICE_XRC;
  117. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  118. 0xffffff;
  119. props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
  120. props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
  121. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  122. props->max_mr_size = ~0ull;
  123. props->page_size_cap = dev->dev->caps.page_size_cap;
  124. props->max_qp = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps;
  125. props->max_qp_wr = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
  126. props->max_sge = min(dev->dev->caps.max_sq_sg,
  127. dev->dev->caps.max_rq_sg);
  128. props->max_cq = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs;
  129. props->max_cqe = dev->dev->caps.max_cqes;
  130. props->max_mr = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws;
  131. props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
  132. props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
  133. props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
  134. props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
  135. props->max_srq = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs;
  136. props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
  137. props->max_srq_sge = dev->dev->caps.max_srq_sge;
  138. props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
  139. props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
  140. props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
  141. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  142. props->masked_atomic_cap = props->atomic_cap;
  143. props->max_pkeys = dev->dev->caps.pkey_table_len[1];
  144. props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
  145. props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
  146. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  147. props->max_mcast_grp;
  148. props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
  149. out:
  150. kfree(in_mad);
  151. kfree(out_mad);
  152. return err;
  153. }
  154. static enum rdma_link_layer
  155. mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
  156. {
  157. struct mlx4_dev *dev = to_mdev(device)->dev;
  158. return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
  159. IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
  160. }
  161. static int ib_link_query_port(struct ib_device *ibdev, u8 port,
  162. struct ib_port_attr *props, int netw_view)
  163. {
  164. struct ib_smp *in_mad = NULL;
  165. struct ib_smp *out_mad = NULL;
  166. int ext_active_speed;
  167. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  168. int err = -ENOMEM;
  169. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  170. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  171. if (!in_mad || !out_mad)
  172. goto out;
  173. init_query_mad(in_mad);
  174. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  175. in_mad->attr_mod = cpu_to_be32(port);
  176. if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
  177. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  178. err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
  179. in_mad, out_mad);
  180. if (err)
  181. goto out;
  182. props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
  183. props->lmc = out_mad->data[34] & 0x7;
  184. props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
  185. props->sm_sl = out_mad->data[36] & 0xf;
  186. props->state = out_mad->data[32] & 0xf;
  187. props->phys_state = out_mad->data[33] >> 4;
  188. props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
  189. if (netw_view)
  190. props->gid_tbl_len = out_mad->data[50];
  191. else
  192. props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
  193. props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
  194. props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
  195. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  196. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  197. props->active_width = out_mad->data[31] & 0xf;
  198. props->active_speed = out_mad->data[35] >> 4;
  199. props->max_mtu = out_mad->data[41] & 0xf;
  200. props->active_mtu = out_mad->data[36] >> 4;
  201. props->subnet_timeout = out_mad->data[51] & 0x1f;
  202. props->max_vl_num = out_mad->data[37] >> 4;
  203. props->init_type_reply = out_mad->data[41] >> 4;
  204. /* Check if extended speeds (EDR/FDR/...) are supported */
  205. if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
  206. ext_active_speed = out_mad->data[62] >> 4;
  207. switch (ext_active_speed) {
  208. case 1:
  209. props->active_speed = IB_SPEED_FDR;
  210. break;
  211. case 2:
  212. props->active_speed = IB_SPEED_EDR;
  213. break;
  214. }
  215. }
  216. /* If reported active speed is QDR, check if is FDR-10 */
  217. if (props->active_speed == IB_SPEED_QDR) {
  218. init_query_mad(in_mad);
  219. in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
  220. in_mad->attr_mod = cpu_to_be32(port);
  221. err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port,
  222. NULL, NULL, in_mad, out_mad);
  223. if (err)
  224. goto out;
  225. /* Checking LinkSpeedActive for FDR-10 */
  226. if (out_mad->data[15] & 0x1)
  227. props->active_speed = IB_SPEED_FDR10;
  228. }
  229. /* Avoid wrong speed value returned by FW if the IB link is down. */
  230. if (props->state == IB_PORT_DOWN)
  231. props->active_speed = IB_SPEED_SDR;
  232. out:
  233. kfree(in_mad);
  234. kfree(out_mad);
  235. return err;
  236. }
  237. static u8 state_to_phys_state(enum ib_port_state state)
  238. {
  239. return state == IB_PORT_ACTIVE ? 5 : 3;
  240. }
  241. static int eth_link_query_port(struct ib_device *ibdev, u8 port,
  242. struct ib_port_attr *props, int netw_view)
  243. {
  244. struct mlx4_ib_dev *mdev = to_mdev(ibdev);
  245. struct mlx4_ib_iboe *iboe = &mdev->iboe;
  246. struct net_device *ndev;
  247. enum ib_mtu tmp;
  248. struct mlx4_cmd_mailbox *mailbox;
  249. int err = 0;
  250. mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
  251. if (IS_ERR(mailbox))
  252. return PTR_ERR(mailbox);
  253. err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
  254. MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
  255. MLX4_CMD_WRAPPED);
  256. if (err)
  257. goto out;
  258. props->active_width = (((u8 *)mailbox->buf)[5] == 0x40) ?
  259. IB_WIDTH_4X : IB_WIDTH_1X;
  260. props->active_speed = IB_SPEED_QDR;
  261. props->port_cap_flags = IB_PORT_CM_SUP;
  262. props->gid_tbl_len = mdev->dev->caps.gid_table_len[port];
  263. props->max_msg_sz = mdev->dev->caps.max_msg_sz;
  264. props->pkey_tbl_len = 1;
  265. props->max_mtu = IB_MTU_4096;
  266. props->max_vl_num = 2;
  267. props->state = IB_PORT_DOWN;
  268. props->phys_state = state_to_phys_state(props->state);
  269. props->active_mtu = IB_MTU_256;
  270. spin_lock(&iboe->lock);
  271. ndev = iboe->netdevs[port - 1];
  272. if (!ndev)
  273. goto out_unlock;
  274. tmp = iboe_get_mtu(ndev->mtu);
  275. props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
  276. props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
  277. IB_PORT_ACTIVE : IB_PORT_DOWN;
  278. props->phys_state = state_to_phys_state(props->state);
  279. out_unlock:
  280. spin_unlock(&iboe->lock);
  281. out:
  282. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  283. return err;
  284. }
  285. int __mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
  286. struct ib_port_attr *props, int netw_view)
  287. {
  288. int err;
  289. memset(props, 0, sizeof *props);
  290. err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
  291. ib_link_query_port(ibdev, port, props, netw_view) :
  292. eth_link_query_port(ibdev, port, props, netw_view);
  293. return err;
  294. }
  295. static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
  296. struct ib_port_attr *props)
  297. {
  298. /* returns host view */
  299. return __mlx4_ib_query_port(ibdev, port, props, 0);
  300. }
  301. static int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  302. union ib_gid *gid)
  303. {
  304. struct ib_smp *in_mad = NULL;
  305. struct ib_smp *out_mad = NULL;
  306. int err = -ENOMEM;
  307. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  308. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  309. if (!in_mad || !out_mad)
  310. goto out;
  311. init_query_mad(in_mad);
  312. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  313. in_mad->attr_mod = cpu_to_be32(port);
  314. err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS, port,
  315. NULL, NULL, in_mad, out_mad);
  316. if (err)
  317. goto out;
  318. memcpy(gid->raw, out_mad->data + 8, 8);
  319. init_query_mad(in_mad);
  320. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  321. in_mad->attr_mod = cpu_to_be32(index / 8);
  322. err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS, port,
  323. NULL, NULL, in_mad, out_mad);
  324. if (err)
  325. goto out;
  326. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  327. out:
  328. kfree(in_mad);
  329. kfree(out_mad);
  330. return err;
  331. }
  332. static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
  333. union ib_gid *gid)
  334. {
  335. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  336. *gid = dev->iboe.gid_table[port - 1][index];
  337. return 0;
  338. }
  339. static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  340. union ib_gid *gid)
  341. {
  342. if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
  343. return __mlx4_ib_query_gid(ibdev, port, index, gid);
  344. else
  345. return iboe_query_gid(ibdev, port, index, gid);
  346. }
  347. int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  348. u16 *pkey, int netw_view)
  349. {
  350. struct ib_smp *in_mad = NULL;
  351. struct ib_smp *out_mad = NULL;
  352. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  353. int err = -ENOMEM;
  354. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  355. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  356. if (!in_mad || !out_mad)
  357. goto out;
  358. init_query_mad(in_mad);
  359. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  360. in_mad->attr_mod = cpu_to_be32(index / 32);
  361. if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
  362. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  363. err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
  364. in_mad, out_mad);
  365. if (err)
  366. goto out;
  367. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  368. out:
  369. kfree(in_mad);
  370. kfree(out_mad);
  371. return err;
  372. }
  373. static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
  374. {
  375. return __mlx4_ib_query_pkey(ibdev, port, index, pkey, 0);
  376. }
  377. static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
  378. struct ib_device_modify *props)
  379. {
  380. struct mlx4_cmd_mailbox *mailbox;
  381. unsigned long flags;
  382. if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
  383. return -EOPNOTSUPP;
  384. if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
  385. return 0;
  386. spin_lock_irqsave(&to_mdev(ibdev)->sm_lock, flags);
  387. memcpy(ibdev->node_desc, props->node_desc, 64);
  388. spin_unlock_irqrestore(&to_mdev(ibdev)->sm_lock, flags);
  389. /*
  390. * If possible, pass node desc to FW, so it can generate
  391. * a 144 trap. If cmd fails, just ignore.
  392. */
  393. mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
  394. if (IS_ERR(mailbox))
  395. return 0;
  396. memset(mailbox->buf, 0, 256);
  397. memcpy(mailbox->buf, props->node_desc, 64);
  398. mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
  399. MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  400. mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
  401. return 0;
  402. }
  403. static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
  404. u32 cap_mask)
  405. {
  406. struct mlx4_cmd_mailbox *mailbox;
  407. int err;
  408. u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
  409. mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  410. if (IS_ERR(mailbox))
  411. return PTR_ERR(mailbox);
  412. memset(mailbox->buf, 0, 256);
  413. if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  414. *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
  415. ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
  416. } else {
  417. ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
  418. ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
  419. }
  420. err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
  421. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  422. mlx4_free_cmd_mailbox(dev->dev, mailbox);
  423. return err;
  424. }
  425. static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
  426. struct ib_port_modify *props)
  427. {
  428. struct ib_port_attr attr;
  429. u32 cap_mask;
  430. int err;
  431. mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
  432. err = mlx4_ib_query_port(ibdev, port, &attr);
  433. if (err)
  434. goto out;
  435. cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  436. ~props->clr_port_cap_mask;
  437. err = mlx4_SET_PORT(to_mdev(ibdev), port,
  438. !!(mask & IB_PORT_RESET_QKEY_CNTR),
  439. cap_mask);
  440. out:
  441. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  442. return err;
  443. }
  444. static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
  445. struct ib_udata *udata)
  446. {
  447. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  448. struct mlx4_ib_ucontext *context;
  449. struct mlx4_ib_alloc_ucontext_resp resp;
  450. int err;
  451. if (!dev->ib_active)
  452. return ERR_PTR(-EAGAIN);
  453. resp.qp_tab_size = dev->dev->caps.num_qps;
  454. resp.bf_reg_size = dev->dev->caps.bf_reg_size;
  455. resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
  456. context = kmalloc(sizeof *context, GFP_KERNEL);
  457. if (!context)
  458. return ERR_PTR(-ENOMEM);
  459. err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
  460. if (err) {
  461. kfree(context);
  462. return ERR_PTR(err);
  463. }
  464. INIT_LIST_HEAD(&context->db_page_list);
  465. mutex_init(&context->db_page_mutex);
  466. err = ib_copy_to_udata(udata, &resp, sizeof resp);
  467. if (err) {
  468. mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
  469. kfree(context);
  470. return ERR_PTR(-EFAULT);
  471. }
  472. return &context->ibucontext;
  473. }
  474. static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
  475. {
  476. struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
  477. mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
  478. kfree(context);
  479. return 0;
  480. }
  481. static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  482. {
  483. struct mlx4_ib_dev *dev = to_mdev(context->device);
  484. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  485. return -EINVAL;
  486. if (vma->vm_pgoff == 0) {
  487. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  488. if (io_remap_pfn_range(vma, vma->vm_start,
  489. to_mucontext(context)->uar.pfn,
  490. PAGE_SIZE, vma->vm_page_prot))
  491. return -EAGAIN;
  492. } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
  493. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  494. if (io_remap_pfn_range(vma, vma->vm_start,
  495. to_mucontext(context)->uar.pfn +
  496. dev->dev->caps.num_uars,
  497. PAGE_SIZE, vma->vm_page_prot))
  498. return -EAGAIN;
  499. } else
  500. return -EINVAL;
  501. return 0;
  502. }
  503. static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
  504. struct ib_ucontext *context,
  505. struct ib_udata *udata)
  506. {
  507. struct mlx4_ib_pd *pd;
  508. int err;
  509. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  510. if (!pd)
  511. return ERR_PTR(-ENOMEM);
  512. err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
  513. if (err) {
  514. kfree(pd);
  515. return ERR_PTR(err);
  516. }
  517. if (context)
  518. if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
  519. mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
  520. kfree(pd);
  521. return ERR_PTR(-EFAULT);
  522. }
  523. return &pd->ibpd;
  524. }
  525. static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
  526. {
  527. mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
  528. kfree(pd);
  529. return 0;
  530. }
  531. static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
  532. struct ib_ucontext *context,
  533. struct ib_udata *udata)
  534. {
  535. struct mlx4_ib_xrcd *xrcd;
  536. int err;
  537. if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  538. return ERR_PTR(-ENOSYS);
  539. xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
  540. if (!xrcd)
  541. return ERR_PTR(-ENOMEM);
  542. err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
  543. if (err)
  544. goto err1;
  545. xrcd->pd = ib_alloc_pd(ibdev);
  546. if (IS_ERR(xrcd->pd)) {
  547. err = PTR_ERR(xrcd->pd);
  548. goto err2;
  549. }
  550. xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, 1, 0);
  551. if (IS_ERR(xrcd->cq)) {
  552. err = PTR_ERR(xrcd->cq);
  553. goto err3;
  554. }
  555. return &xrcd->ibxrcd;
  556. err3:
  557. ib_dealloc_pd(xrcd->pd);
  558. err2:
  559. mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
  560. err1:
  561. kfree(xrcd);
  562. return ERR_PTR(err);
  563. }
  564. static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
  565. {
  566. ib_destroy_cq(to_mxrcd(xrcd)->cq);
  567. ib_dealloc_pd(to_mxrcd(xrcd)->pd);
  568. mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
  569. kfree(xrcd);
  570. return 0;
  571. }
  572. static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
  573. {
  574. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  575. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  576. struct mlx4_ib_gid_entry *ge;
  577. ge = kzalloc(sizeof *ge, GFP_KERNEL);
  578. if (!ge)
  579. return -ENOMEM;
  580. ge->gid = *gid;
  581. if (mlx4_ib_add_mc(mdev, mqp, gid)) {
  582. ge->port = mqp->port;
  583. ge->added = 1;
  584. }
  585. mutex_lock(&mqp->mutex);
  586. list_add_tail(&ge->list, &mqp->gid_list);
  587. mutex_unlock(&mqp->mutex);
  588. return 0;
  589. }
  590. int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
  591. union ib_gid *gid)
  592. {
  593. u8 mac[6];
  594. struct net_device *ndev;
  595. int ret = 0;
  596. if (!mqp->port)
  597. return 0;
  598. spin_lock(&mdev->iboe.lock);
  599. ndev = mdev->iboe.netdevs[mqp->port - 1];
  600. if (ndev)
  601. dev_hold(ndev);
  602. spin_unlock(&mdev->iboe.lock);
  603. if (ndev) {
  604. rdma_get_mcast_mac((struct in6_addr *)gid, mac);
  605. rtnl_lock();
  606. dev_mc_add(mdev->iboe.netdevs[mqp->port - 1], mac);
  607. ret = 1;
  608. rtnl_unlock();
  609. dev_put(ndev);
  610. }
  611. return ret;
  612. }
  613. struct mlx4_ib_steering {
  614. struct list_head list;
  615. u64 reg_id;
  616. union ib_gid gid;
  617. };
  618. static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  619. {
  620. int err;
  621. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  622. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  623. u64 reg_id;
  624. struct mlx4_ib_steering *ib_steering = NULL;
  625. if (mdev->dev->caps.steering_mode ==
  626. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  627. ib_steering = kmalloc(sizeof(*ib_steering), GFP_KERNEL);
  628. if (!ib_steering)
  629. return -ENOMEM;
  630. }
  631. err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw, mqp->port,
  632. !!(mqp->flags &
  633. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
  634. MLX4_PROT_IB_IPV6, &reg_id);
  635. if (err)
  636. goto err_malloc;
  637. err = add_gid_entry(ibqp, gid);
  638. if (err)
  639. goto err_add;
  640. if (ib_steering) {
  641. memcpy(ib_steering->gid.raw, gid->raw, 16);
  642. ib_steering->reg_id = reg_id;
  643. mutex_lock(&mqp->mutex);
  644. list_add(&ib_steering->list, &mqp->steering_rules);
  645. mutex_unlock(&mqp->mutex);
  646. }
  647. return 0;
  648. err_add:
  649. mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  650. MLX4_PROT_IB_IPV6, reg_id);
  651. err_malloc:
  652. kfree(ib_steering);
  653. return err;
  654. }
  655. static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
  656. {
  657. struct mlx4_ib_gid_entry *ge;
  658. struct mlx4_ib_gid_entry *tmp;
  659. struct mlx4_ib_gid_entry *ret = NULL;
  660. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  661. if (!memcmp(raw, ge->gid.raw, 16)) {
  662. ret = ge;
  663. break;
  664. }
  665. }
  666. return ret;
  667. }
  668. static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  669. {
  670. int err;
  671. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  672. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  673. u8 mac[6];
  674. struct net_device *ndev;
  675. struct mlx4_ib_gid_entry *ge;
  676. u64 reg_id = 0;
  677. if (mdev->dev->caps.steering_mode ==
  678. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  679. struct mlx4_ib_steering *ib_steering;
  680. mutex_lock(&mqp->mutex);
  681. list_for_each_entry(ib_steering, &mqp->steering_rules, list) {
  682. if (!memcmp(ib_steering->gid.raw, gid->raw, 16)) {
  683. list_del(&ib_steering->list);
  684. break;
  685. }
  686. }
  687. mutex_unlock(&mqp->mutex);
  688. if (&ib_steering->list == &mqp->steering_rules) {
  689. pr_err("Couldn't find reg_id for mgid. Steering rule is left attached\n");
  690. return -EINVAL;
  691. }
  692. reg_id = ib_steering->reg_id;
  693. kfree(ib_steering);
  694. }
  695. err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  696. MLX4_PROT_IB_IPV6, reg_id);
  697. if (err)
  698. return err;
  699. mutex_lock(&mqp->mutex);
  700. ge = find_gid_entry(mqp, gid->raw);
  701. if (ge) {
  702. spin_lock(&mdev->iboe.lock);
  703. ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
  704. if (ndev)
  705. dev_hold(ndev);
  706. spin_unlock(&mdev->iboe.lock);
  707. rdma_get_mcast_mac((struct in6_addr *)gid, mac);
  708. if (ndev) {
  709. rtnl_lock();
  710. dev_mc_del(mdev->iboe.netdevs[ge->port - 1], mac);
  711. rtnl_unlock();
  712. dev_put(ndev);
  713. }
  714. list_del(&ge->list);
  715. kfree(ge);
  716. } else
  717. pr_warn("could not find mgid entry\n");
  718. mutex_unlock(&mqp->mutex);
  719. return 0;
  720. }
  721. static int init_node_data(struct mlx4_ib_dev *dev)
  722. {
  723. struct ib_smp *in_mad = NULL;
  724. struct ib_smp *out_mad = NULL;
  725. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  726. int err = -ENOMEM;
  727. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  728. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  729. if (!in_mad || !out_mad)
  730. goto out;
  731. init_query_mad(in_mad);
  732. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  733. if (mlx4_is_master(dev->dev))
  734. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  735. err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
  736. if (err)
  737. goto out;
  738. memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
  739. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  740. err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
  741. if (err)
  742. goto out;
  743. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  744. out:
  745. kfree(in_mad);
  746. kfree(out_mad);
  747. return err;
  748. }
  749. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  750. char *buf)
  751. {
  752. struct mlx4_ib_dev *dev =
  753. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  754. return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
  755. }
  756. static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
  757. char *buf)
  758. {
  759. struct mlx4_ib_dev *dev =
  760. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  761. return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
  762. (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
  763. (int) dev->dev->caps.fw_ver & 0xffff);
  764. }
  765. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  766. char *buf)
  767. {
  768. struct mlx4_ib_dev *dev =
  769. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  770. return sprintf(buf, "%x\n", dev->dev->rev_id);
  771. }
  772. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  773. char *buf)
  774. {
  775. struct mlx4_ib_dev *dev =
  776. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  777. return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
  778. dev->dev->board_id);
  779. }
  780. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  781. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  782. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  783. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  784. static struct device_attribute *mlx4_class_attributes[] = {
  785. &dev_attr_hw_rev,
  786. &dev_attr_fw_ver,
  787. &dev_attr_hca_type,
  788. &dev_attr_board_id
  789. };
  790. static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev)
  791. {
  792. memcpy(eui, dev->dev_addr, 3);
  793. memcpy(eui + 5, dev->dev_addr + 3, 3);
  794. if (vlan_id < 0x1000) {
  795. eui[3] = vlan_id >> 8;
  796. eui[4] = vlan_id & 0xff;
  797. } else {
  798. eui[3] = 0xff;
  799. eui[4] = 0xfe;
  800. }
  801. eui[0] ^= 2;
  802. }
  803. static void update_gids_task(struct work_struct *work)
  804. {
  805. struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
  806. struct mlx4_cmd_mailbox *mailbox;
  807. union ib_gid *gids;
  808. int err;
  809. struct mlx4_dev *dev = gw->dev->dev;
  810. mailbox = mlx4_alloc_cmd_mailbox(dev);
  811. if (IS_ERR(mailbox)) {
  812. pr_warn("update gid table failed %ld\n", PTR_ERR(mailbox));
  813. return;
  814. }
  815. gids = mailbox->buf;
  816. memcpy(gids, gw->gids, sizeof gw->gids);
  817. err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
  818. 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  819. MLX4_CMD_NATIVE);
  820. if (err)
  821. pr_warn("set port command failed\n");
  822. else {
  823. memcpy(gw->dev->iboe.gid_table[gw->port - 1], gw->gids, sizeof gw->gids);
  824. mlx4_ib_dispatch_event(gw->dev, gw->port, IB_EVENT_GID_CHANGE);
  825. }
  826. mlx4_free_cmd_mailbox(dev, mailbox);
  827. kfree(gw);
  828. }
  829. static int update_ipv6_gids(struct mlx4_ib_dev *dev, int port, int clear)
  830. {
  831. struct net_device *ndev = dev->iboe.netdevs[port - 1];
  832. struct update_gid_work *work;
  833. struct net_device *tmp;
  834. int i;
  835. u8 *hits;
  836. int ret;
  837. union ib_gid gid;
  838. int free;
  839. int found;
  840. int need_update = 0;
  841. u16 vid;
  842. work = kzalloc(sizeof *work, GFP_ATOMIC);
  843. if (!work)
  844. return -ENOMEM;
  845. hits = kzalloc(128, GFP_ATOMIC);
  846. if (!hits) {
  847. ret = -ENOMEM;
  848. goto out;
  849. }
  850. rcu_read_lock();
  851. for_each_netdev_rcu(&init_net, tmp) {
  852. if (ndev && (tmp == ndev || rdma_vlan_dev_real_dev(tmp) == ndev)) {
  853. gid.global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
  854. vid = rdma_vlan_dev_vlan_id(tmp);
  855. mlx4_addrconf_ifid_eui48(&gid.raw[8], vid, ndev);
  856. found = 0;
  857. free = -1;
  858. for (i = 0; i < 128; ++i) {
  859. if (free < 0 &&
  860. !memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
  861. free = i;
  862. if (!memcmp(&dev->iboe.gid_table[port - 1][i], &gid, sizeof gid)) {
  863. hits[i] = 1;
  864. found = 1;
  865. break;
  866. }
  867. }
  868. if (!found) {
  869. if (tmp == ndev &&
  870. (memcmp(&dev->iboe.gid_table[port - 1][0],
  871. &gid, sizeof gid) ||
  872. !memcmp(&dev->iboe.gid_table[port - 1][0],
  873. &zgid, sizeof gid))) {
  874. dev->iboe.gid_table[port - 1][0] = gid;
  875. ++need_update;
  876. hits[0] = 1;
  877. } else if (free >= 0) {
  878. dev->iboe.gid_table[port - 1][free] = gid;
  879. hits[free] = 1;
  880. ++need_update;
  881. }
  882. }
  883. }
  884. }
  885. rcu_read_unlock();
  886. for (i = 0; i < 128; ++i)
  887. if (!hits[i]) {
  888. if (memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
  889. ++need_update;
  890. dev->iboe.gid_table[port - 1][i] = zgid;
  891. }
  892. if (need_update) {
  893. memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof work->gids);
  894. INIT_WORK(&work->work, update_gids_task);
  895. work->port = port;
  896. work->dev = dev;
  897. queue_work(wq, &work->work);
  898. } else
  899. kfree(work);
  900. kfree(hits);
  901. return 0;
  902. out:
  903. kfree(work);
  904. return ret;
  905. }
  906. static void handle_en_event(struct mlx4_ib_dev *dev, int port, unsigned long event)
  907. {
  908. switch (event) {
  909. case NETDEV_UP:
  910. case NETDEV_CHANGEADDR:
  911. update_ipv6_gids(dev, port, 0);
  912. break;
  913. case NETDEV_DOWN:
  914. update_ipv6_gids(dev, port, 1);
  915. dev->iboe.netdevs[port - 1] = NULL;
  916. }
  917. }
  918. static void netdev_added(struct mlx4_ib_dev *dev, int port)
  919. {
  920. update_ipv6_gids(dev, port, 0);
  921. }
  922. static void netdev_removed(struct mlx4_ib_dev *dev, int port)
  923. {
  924. update_ipv6_gids(dev, port, 1);
  925. }
  926. static int mlx4_ib_netdev_event(struct notifier_block *this, unsigned long event,
  927. void *ptr)
  928. {
  929. struct net_device *dev = ptr;
  930. struct mlx4_ib_dev *ibdev;
  931. struct net_device *oldnd;
  932. struct mlx4_ib_iboe *iboe;
  933. int port;
  934. if (!net_eq(dev_net(dev), &init_net))
  935. return NOTIFY_DONE;
  936. ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
  937. iboe = &ibdev->iboe;
  938. spin_lock(&iboe->lock);
  939. mlx4_foreach_ib_transport_port(port, ibdev->dev) {
  940. oldnd = iboe->netdevs[port - 1];
  941. iboe->netdevs[port - 1] =
  942. mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
  943. if (oldnd != iboe->netdevs[port - 1]) {
  944. if (iboe->netdevs[port - 1])
  945. netdev_added(ibdev, port);
  946. else
  947. netdev_removed(ibdev, port);
  948. }
  949. }
  950. if (dev == iboe->netdevs[0] ||
  951. (iboe->netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[0]))
  952. handle_en_event(ibdev, 1, event);
  953. else if (dev == iboe->netdevs[1]
  954. || (iboe->netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[1]))
  955. handle_en_event(ibdev, 2, event);
  956. spin_unlock(&iboe->lock);
  957. return NOTIFY_DONE;
  958. }
  959. static void init_pkeys(struct mlx4_ib_dev *ibdev)
  960. {
  961. int port;
  962. int slave;
  963. int i;
  964. if (mlx4_is_master(ibdev->dev)) {
  965. for (slave = 0; slave <= ibdev->dev->num_vfs; ++slave) {
  966. for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
  967. for (i = 0;
  968. i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
  969. ++i) {
  970. ibdev->pkeys.virt2phys_pkey[slave][port - 1][i] =
  971. /* master has the identity virt2phys pkey mapping */
  972. (slave == mlx4_master_func_num(ibdev->dev) || !i) ? i :
  973. ibdev->dev->phys_caps.pkey_phys_table_len[port] - 1;
  974. mlx4_sync_pkey_table(ibdev->dev, slave, port, i,
  975. ibdev->pkeys.virt2phys_pkey[slave][port - 1][i]);
  976. }
  977. }
  978. }
  979. /* initialize pkey cache */
  980. for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
  981. for (i = 0;
  982. i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
  983. ++i)
  984. ibdev->pkeys.phys_pkey_cache[port-1][i] =
  985. (i) ? 0 : 0xFFFF;
  986. }
  987. }
  988. }
  989. static void mlx4_ib_alloc_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
  990. {
  991. char name[32];
  992. int eq_per_port = 0;
  993. int added_eqs = 0;
  994. int total_eqs = 0;
  995. int i, j, eq;
  996. /* Legacy mode or comp_pool is not large enough */
  997. if (dev->caps.comp_pool == 0 ||
  998. dev->caps.num_ports > dev->caps.comp_pool)
  999. return;
  1000. eq_per_port = rounddown_pow_of_two(dev->caps.comp_pool/
  1001. dev->caps.num_ports);
  1002. /* Init eq table */
  1003. added_eqs = 0;
  1004. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  1005. added_eqs += eq_per_port;
  1006. total_eqs = dev->caps.num_comp_vectors + added_eqs;
  1007. ibdev->eq_table = kzalloc(total_eqs * sizeof(int), GFP_KERNEL);
  1008. if (!ibdev->eq_table)
  1009. return;
  1010. ibdev->eq_added = added_eqs;
  1011. eq = 0;
  1012. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB) {
  1013. for (j = 0; j < eq_per_port; j++) {
  1014. sprintf(name, "mlx4-ib-%d-%d@%s",
  1015. i, j, dev->pdev->bus->name);
  1016. /* Set IRQ for specific name (per ring) */
  1017. if (mlx4_assign_eq(dev, name, NULL,
  1018. &ibdev->eq_table[eq])) {
  1019. /* Use legacy (same as mlx4_en driver) */
  1020. pr_warn("Can't allocate EQ %d; reverting to legacy\n", eq);
  1021. ibdev->eq_table[eq] =
  1022. (eq % dev->caps.num_comp_vectors);
  1023. }
  1024. eq++;
  1025. }
  1026. }
  1027. /* Fill the reset of the vector with legacy EQ */
  1028. for (i = 0, eq = added_eqs; i < dev->caps.num_comp_vectors; i++)
  1029. ibdev->eq_table[eq++] = i;
  1030. /* Advertise the new number of EQs to clients */
  1031. ibdev->ib_dev.num_comp_vectors = total_eqs;
  1032. }
  1033. static void mlx4_ib_free_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
  1034. {
  1035. int i;
  1036. /* no additional eqs were added */
  1037. if (!ibdev->eq_table)
  1038. return;
  1039. /* Reset the advertised EQ number */
  1040. ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
  1041. /* Free only the added eqs */
  1042. for (i = 0; i < ibdev->eq_added; i++) {
  1043. /* Don't free legacy eqs if used */
  1044. if (ibdev->eq_table[i] <= dev->caps.num_comp_vectors)
  1045. continue;
  1046. mlx4_release_eq(dev, ibdev->eq_table[i]);
  1047. }
  1048. kfree(ibdev->eq_table);
  1049. }
  1050. static void *mlx4_ib_add(struct mlx4_dev *dev)
  1051. {
  1052. struct mlx4_ib_dev *ibdev;
  1053. int num_ports = 0;
  1054. int i, j;
  1055. int err;
  1056. struct mlx4_ib_iboe *iboe;
  1057. pr_info_once("%s", mlx4_ib_version);
  1058. if (mlx4_is_mfunc(dev)) {
  1059. pr_warn("IB not yet supported in SRIOV\n");
  1060. return NULL;
  1061. }
  1062. mlx4_foreach_ib_transport_port(i, dev)
  1063. num_ports++;
  1064. /* No point in registering a device with no ports... */
  1065. if (num_ports == 0)
  1066. return NULL;
  1067. ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
  1068. if (!ibdev) {
  1069. dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
  1070. return NULL;
  1071. }
  1072. iboe = &ibdev->iboe;
  1073. if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
  1074. goto err_dealloc;
  1075. if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
  1076. goto err_pd;
  1077. ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
  1078. PAGE_SIZE);
  1079. if (!ibdev->uar_map)
  1080. goto err_uar;
  1081. MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
  1082. ibdev->dev = dev;
  1083. strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
  1084. ibdev->ib_dev.owner = THIS_MODULE;
  1085. ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
  1086. ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
  1087. ibdev->num_ports = num_ports;
  1088. ibdev->ib_dev.phys_port_cnt = ibdev->num_ports;
  1089. ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
  1090. ibdev->ib_dev.dma_device = &dev->pdev->dev;
  1091. ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
  1092. ibdev->ib_dev.uverbs_cmd_mask =
  1093. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  1094. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  1095. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  1096. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  1097. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  1098. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  1099. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  1100. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  1101. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  1102. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  1103. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  1104. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  1105. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  1106. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  1107. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  1108. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  1109. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  1110. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  1111. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  1112. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  1113. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
  1114. (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
  1115. (1ull << IB_USER_VERBS_CMD_OPEN_QP);
  1116. ibdev->ib_dev.query_device = mlx4_ib_query_device;
  1117. ibdev->ib_dev.query_port = mlx4_ib_query_port;
  1118. ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
  1119. ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
  1120. ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
  1121. ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
  1122. ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
  1123. ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
  1124. ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
  1125. ibdev->ib_dev.mmap = mlx4_ib_mmap;
  1126. ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
  1127. ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
  1128. ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
  1129. ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
  1130. ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
  1131. ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
  1132. ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
  1133. ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
  1134. ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
  1135. ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
  1136. ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
  1137. ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
  1138. ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
  1139. ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
  1140. ibdev->ib_dev.post_send = mlx4_ib_post_send;
  1141. ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
  1142. ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
  1143. ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
  1144. ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
  1145. ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
  1146. ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
  1147. ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
  1148. ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
  1149. ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
  1150. ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
  1151. ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
  1152. ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
  1153. ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list;
  1154. ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
  1155. ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
  1156. ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
  1157. ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
  1158. ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
  1159. ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
  1160. ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
  1161. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
  1162. ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
  1163. ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
  1164. ibdev->ib_dev.uverbs_cmd_mask |=
  1165. (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
  1166. (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
  1167. }
  1168. mlx4_ib_alloc_eqs(dev, ibdev);
  1169. spin_lock_init(&iboe->lock);
  1170. if (init_node_data(ibdev))
  1171. goto err_map;
  1172. for (i = 0; i < ibdev->num_ports; ++i) {
  1173. if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
  1174. IB_LINK_LAYER_ETHERNET) {
  1175. err = mlx4_counter_alloc(ibdev->dev, &ibdev->counters[i]);
  1176. if (err)
  1177. ibdev->counters[i] = -1;
  1178. } else
  1179. ibdev->counters[i] = -1;
  1180. }
  1181. spin_lock_init(&ibdev->sm_lock);
  1182. mutex_init(&ibdev->cap_mask_mutex);
  1183. if (ib_register_device(&ibdev->ib_dev, NULL))
  1184. goto err_counter;
  1185. if (mlx4_ib_mad_init(ibdev))
  1186. goto err_reg;
  1187. if (mlx4_ib_init_sriov(ibdev))
  1188. goto err_mad;
  1189. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE && !iboe->nb.notifier_call) {
  1190. iboe->nb.notifier_call = mlx4_ib_netdev_event;
  1191. err = register_netdevice_notifier(&iboe->nb);
  1192. if (err)
  1193. goto err_sriov;
  1194. }
  1195. for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
  1196. if (device_create_file(&ibdev->ib_dev.dev,
  1197. mlx4_class_attributes[j]))
  1198. goto err_notif;
  1199. }
  1200. ibdev->ib_active = true;
  1201. if (mlx4_is_mfunc(ibdev->dev))
  1202. init_pkeys(ibdev);
  1203. return ibdev;
  1204. err_notif:
  1205. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  1206. pr_warn("failure unregistering notifier\n");
  1207. flush_workqueue(wq);
  1208. err_sriov:
  1209. mlx4_ib_close_sriov(ibdev);
  1210. err_mad:
  1211. mlx4_ib_mad_cleanup(ibdev);
  1212. err_reg:
  1213. ib_unregister_device(&ibdev->ib_dev);
  1214. err_counter:
  1215. for (; i; --i)
  1216. if (ibdev->counters[i - 1] != -1)
  1217. mlx4_counter_free(ibdev->dev, ibdev->counters[i - 1]);
  1218. err_map:
  1219. iounmap(ibdev->uar_map);
  1220. err_uar:
  1221. mlx4_uar_free(dev, &ibdev->priv_uar);
  1222. err_pd:
  1223. mlx4_pd_free(dev, ibdev->priv_pdn);
  1224. err_dealloc:
  1225. ib_dealloc_device(&ibdev->ib_dev);
  1226. return NULL;
  1227. }
  1228. static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
  1229. {
  1230. struct mlx4_ib_dev *ibdev = ibdev_ptr;
  1231. int p;
  1232. mlx4_ib_close_sriov(ibdev);
  1233. mlx4_ib_mad_cleanup(ibdev);
  1234. ib_unregister_device(&ibdev->ib_dev);
  1235. if (ibdev->iboe.nb.notifier_call) {
  1236. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  1237. pr_warn("failure unregistering notifier\n");
  1238. ibdev->iboe.nb.notifier_call = NULL;
  1239. }
  1240. iounmap(ibdev->uar_map);
  1241. for (p = 0; p < ibdev->num_ports; ++p)
  1242. if (ibdev->counters[p] != -1)
  1243. mlx4_counter_free(ibdev->dev, ibdev->counters[p]);
  1244. mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
  1245. mlx4_CLOSE_PORT(dev, p);
  1246. mlx4_ib_free_eqs(dev, ibdev);
  1247. mlx4_uar_free(dev, &ibdev->priv_uar);
  1248. mlx4_pd_free(dev, ibdev->priv_pdn);
  1249. ib_dealloc_device(&ibdev->ib_dev);
  1250. }
  1251. static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init)
  1252. {
  1253. struct mlx4_ib_demux_work **dm = NULL;
  1254. struct mlx4_dev *dev = ibdev->dev;
  1255. int i;
  1256. unsigned long flags;
  1257. if (!mlx4_is_master(dev))
  1258. return;
  1259. dm = kcalloc(dev->caps.num_ports, sizeof *dm, GFP_ATOMIC);
  1260. if (!dm) {
  1261. pr_err("failed to allocate memory for tunneling qp update\n");
  1262. goto out;
  1263. }
  1264. for (i = 0; i < dev->caps.num_ports; i++) {
  1265. dm[i] = kmalloc(sizeof (struct mlx4_ib_demux_work), GFP_ATOMIC);
  1266. if (!dm[i]) {
  1267. pr_err("failed to allocate memory for tunneling qp update work struct\n");
  1268. for (i = 0; i < dev->caps.num_ports; i++) {
  1269. if (dm[i])
  1270. kfree(dm[i]);
  1271. }
  1272. goto out;
  1273. }
  1274. }
  1275. /* initialize or tear down tunnel QPs for the slave */
  1276. for (i = 0; i < dev->caps.num_ports; i++) {
  1277. INIT_WORK(&dm[i]->work, mlx4_ib_tunnels_update_work);
  1278. dm[i]->port = i + 1;
  1279. dm[i]->slave = slave;
  1280. dm[i]->do_init = do_init;
  1281. dm[i]->dev = ibdev;
  1282. spin_lock_irqsave(&ibdev->sriov.going_down_lock, flags);
  1283. if (!ibdev->sriov.is_going_down)
  1284. queue_work(ibdev->sriov.demux[i].ud_wq, &dm[i]->work);
  1285. spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
  1286. }
  1287. out:
  1288. if (dm)
  1289. kfree(dm);
  1290. return;
  1291. }
  1292. static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
  1293. enum mlx4_dev_event event, unsigned long param)
  1294. {
  1295. struct ib_event ibev;
  1296. struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
  1297. struct mlx4_eqe *eqe = NULL;
  1298. struct ib_event_work *ew;
  1299. int p = 0;
  1300. if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
  1301. eqe = (struct mlx4_eqe *)param;
  1302. else
  1303. p = (int) param;
  1304. switch (event) {
  1305. case MLX4_DEV_EVENT_PORT_UP:
  1306. if (p > ibdev->num_ports)
  1307. return;
  1308. ibev.event = IB_EVENT_PORT_ACTIVE;
  1309. break;
  1310. case MLX4_DEV_EVENT_PORT_DOWN:
  1311. if (p > ibdev->num_ports)
  1312. return;
  1313. ibev.event = IB_EVENT_PORT_ERR;
  1314. break;
  1315. case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
  1316. ibdev->ib_active = false;
  1317. ibev.event = IB_EVENT_DEVICE_FATAL;
  1318. break;
  1319. case MLX4_DEV_EVENT_PORT_MGMT_CHANGE:
  1320. ew = kmalloc(sizeof *ew, GFP_ATOMIC);
  1321. if (!ew) {
  1322. pr_err("failed to allocate memory for events work\n");
  1323. break;
  1324. }
  1325. INIT_WORK(&ew->work, handle_port_mgmt_change_event);
  1326. memcpy(&ew->ib_eqe, eqe, sizeof *eqe);
  1327. ew->ib_dev = ibdev;
  1328. handle_port_mgmt_change_event(&ew->work);
  1329. return;
  1330. case MLX4_DEV_EVENT_SLAVE_INIT:
  1331. /* here, p is the slave id */
  1332. do_slave_init(ibdev, p, 1);
  1333. return;
  1334. case MLX4_DEV_EVENT_SLAVE_SHUTDOWN:
  1335. /* here, p is the slave id */
  1336. do_slave_init(ibdev, p, 0);
  1337. return;
  1338. default:
  1339. return;
  1340. }
  1341. ibev.device = ibdev_ptr;
  1342. ibev.element.port_num = (u8) p;
  1343. ib_dispatch_event(&ibev);
  1344. }
  1345. static struct mlx4_interface mlx4_ib_interface = {
  1346. .add = mlx4_ib_add,
  1347. .remove = mlx4_ib_remove,
  1348. .event = mlx4_ib_event,
  1349. .protocol = MLX4_PROT_IB_IPV6
  1350. };
  1351. static int __init mlx4_ib_init(void)
  1352. {
  1353. int err;
  1354. wq = create_singlethread_workqueue("mlx4_ib");
  1355. if (!wq)
  1356. return -ENOMEM;
  1357. err = mlx4_ib_mcg_init();
  1358. if (err)
  1359. goto clean_wq;
  1360. err = mlx4_register_interface(&mlx4_ib_interface);
  1361. if (err)
  1362. goto clean_mcg;
  1363. return 0;
  1364. clean_mcg:
  1365. mlx4_ib_mcg_destroy();
  1366. clean_wq:
  1367. destroy_workqueue(wq);
  1368. return err;
  1369. }
  1370. static void __exit mlx4_ib_cleanup(void)
  1371. {
  1372. mlx4_unregister_interface(&mlx4_ib_interface);
  1373. mlx4_ib_mcg_destroy();
  1374. destroy_workqueue(wq);
  1375. }
  1376. module_init(mlx4_ib_init);
  1377. module_exit(mlx4_ib_cleanup);