main.c 45 KB

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