main.c 45 KB

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