main.c 34 KB

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