main.c 44 KB

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