main.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
  115. props->device_cap_flags |= IB_DEVICE_XRC;
  116. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  117. 0xffffff;
  118. props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
  119. props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
  120. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  121. props->max_mr_size = ~0ull;
  122. props->page_size_cap = dev->dev->caps.page_size_cap;
  123. props->max_qp = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps;
  124. props->max_qp_wr = dev->dev->caps.max_wqes;
  125. props->max_sge = min(dev->dev->caps.max_sq_sg,
  126. dev->dev->caps.max_rq_sg);
  127. props->max_cq = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs;
  128. props->max_cqe = dev->dev->caps.max_cqes;
  129. props->max_mr = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws;
  130. props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
  131. props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
  132. props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
  133. props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
  134. props->max_srq = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs;
  135. props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
  136. props->max_srq_sge = dev->dev->caps.max_srq_sge;
  137. props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
  138. props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
  139. props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
  140. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  141. props->masked_atomic_cap = IB_ATOMIC_HCA;
  142. props->max_pkeys = dev->dev->caps.pkey_table_len[1];
  143. props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
  144. props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
  145. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  146. props->max_mcast_grp;
  147. props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
  148. out:
  149. kfree(in_mad);
  150. kfree(out_mad);
  151. return err;
  152. }
  153. static enum rdma_link_layer
  154. mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
  155. {
  156. struct mlx4_dev *dev = to_mdev(device)->dev;
  157. return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
  158. IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
  159. }
  160. static int ib_link_query_port(struct ib_device *ibdev, u8 port,
  161. struct ib_port_attr *props)
  162. {
  163. struct ib_smp *in_mad = NULL;
  164. struct ib_smp *out_mad = NULL;
  165. int ext_active_speed;
  166. int err = -ENOMEM;
  167. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  168. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  169. if (!in_mad || !out_mad)
  170. goto out;
  171. init_query_mad(in_mad);
  172. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  173. in_mad->attr_mod = cpu_to_be32(port);
  174. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL,
  175. in_mad, out_mad);
  176. if (err)
  177. goto out;
  178. props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
  179. props->lmc = out_mad->data[34] & 0x7;
  180. props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
  181. props->sm_sl = out_mad->data[36] & 0xf;
  182. props->state = out_mad->data[32] & 0xf;
  183. props->phys_state = out_mad->data[33] >> 4;
  184. props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
  185. props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
  186. props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
  187. props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
  188. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  189. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  190. props->active_width = out_mad->data[31] & 0xf;
  191. props->active_speed = out_mad->data[35] >> 4;
  192. props->max_mtu = out_mad->data[41] & 0xf;
  193. props->active_mtu = out_mad->data[36] >> 4;
  194. props->subnet_timeout = out_mad->data[51] & 0x1f;
  195. props->max_vl_num = out_mad->data[37] >> 4;
  196. props->init_type_reply = out_mad->data[41] >> 4;
  197. /* Check if extended speeds (EDR/FDR/...) are supported */
  198. if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
  199. ext_active_speed = out_mad->data[62] >> 4;
  200. switch (ext_active_speed) {
  201. case 1:
  202. props->active_speed = IB_SPEED_FDR;
  203. break;
  204. case 2:
  205. props->active_speed = IB_SPEED_EDR;
  206. break;
  207. }
  208. }
  209. /* If reported active speed is QDR, check if is FDR-10 */
  210. if (props->active_speed == IB_SPEED_QDR) {
  211. init_query_mad(in_mad);
  212. in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
  213. in_mad->attr_mod = cpu_to_be32(port);
  214. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port,
  215. NULL, NULL, in_mad, out_mad);
  216. if (err)
  217. return err;
  218. /* Checking LinkSpeedActive for FDR-10 */
  219. if (out_mad->data[15] & 0x1)
  220. props->active_speed = IB_SPEED_FDR10;
  221. }
  222. out:
  223. kfree(in_mad);
  224. kfree(out_mad);
  225. return err;
  226. }
  227. static u8 state_to_phys_state(enum ib_port_state state)
  228. {
  229. return state == IB_PORT_ACTIVE ? 5 : 3;
  230. }
  231. static int eth_link_query_port(struct ib_device *ibdev, u8 port,
  232. struct ib_port_attr *props)
  233. {
  234. struct mlx4_ib_dev *mdev = to_mdev(ibdev);
  235. struct mlx4_ib_iboe *iboe = &mdev->iboe;
  236. struct net_device *ndev;
  237. enum ib_mtu tmp;
  238. struct mlx4_cmd_mailbox *mailbox;
  239. int err = 0;
  240. mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
  241. if (IS_ERR(mailbox))
  242. return PTR_ERR(mailbox);
  243. err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
  244. MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
  245. MLX4_CMD_WRAPPED);
  246. if (err)
  247. goto out;
  248. props->active_width = (((u8 *)mailbox->buf)[5] == 0x40) ?
  249. IB_WIDTH_4X : IB_WIDTH_1X;
  250. props->active_speed = IB_SPEED_QDR;
  251. props->port_cap_flags = IB_PORT_CM_SUP;
  252. props->gid_tbl_len = mdev->dev->caps.gid_table_len[port];
  253. props->max_msg_sz = mdev->dev->caps.max_msg_sz;
  254. props->pkey_tbl_len = 1;
  255. props->max_mtu = IB_MTU_4096;
  256. props->max_vl_num = 2;
  257. props->state = IB_PORT_DOWN;
  258. props->phys_state = state_to_phys_state(props->state);
  259. props->active_mtu = IB_MTU_256;
  260. spin_lock(&iboe->lock);
  261. ndev = iboe->netdevs[port - 1];
  262. if (!ndev)
  263. goto out_unlock;
  264. tmp = iboe_get_mtu(ndev->mtu);
  265. props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
  266. props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
  267. IB_PORT_ACTIVE : IB_PORT_DOWN;
  268. props->phys_state = state_to_phys_state(props->state);
  269. out_unlock:
  270. spin_unlock(&iboe->lock);
  271. out:
  272. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  273. return err;
  274. }
  275. static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
  276. struct ib_port_attr *props)
  277. {
  278. int err;
  279. memset(props, 0, sizeof *props);
  280. err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
  281. ib_link_query_port(ibdev, port, props) :
  282. eth_link_query_port(ibdev, port, props);
  283. return err;
  284. }
  285. static int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  286. union ib_gid *gid)
  287. {
  288. struct ib_smp *in_mad = NULL;
  289. struct ib_smp *out_mad = NULL;
  290. int err = -ENOMEM;
  291. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  292. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  293. if (!in_mad || !out_mad)
  294. goto out;
  295. init_query_mad(in_mad);
  296. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  297. in_mad->attr_mod = cpu_to_be32(port);
  298. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  299. if (err)
  300. goto out;
  301. memcpy(gid->raw, out_mad->data + 8, 8);
  302. init_query_mad(in_mad);
  303. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  304. in_mad->attr_mod = cpu_to_be32(index / 8);
  305. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  306. if (err)
  307. goto out;
  308. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  309. out:
  310. kfree(in_mad);
  311. kfree(out_mad);
  312. return err;
  313. }
  314. static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
  315. union ib_gid *gid)
  316. {
  317. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  318. *gid = dev->iboe.gid_table[port - 1][index];
  319. return 0;
  320. }
  321. static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  322. union ib_gid *gid)
  323. {
  324. if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
  325. return __mlx4_ib_query_gid(ibdev, port, index, gid);
  326. else
  327. return iboe_query_gid(ibdev, port, index, gid);
  328. }
  329. static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  330. u16 *pkey)
  331. {
  332. struct ib_smp *in_mad = NULL;
  333. struct ib_smp *out_mad = NULL;
  334. int err = -ENOMEM;
  335. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  336. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  337. if (!in_mad || !out_mad)
  338. goto out;
  339. init_query_mad(in_mad);
  340. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  341. in_mad->attr_mod = cpu_to_be32(index / 32);
  342. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  343. if (err)
  344. goto out;
  345. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  346. out:
  347. kfree(in_mad);
  348. kfree(out_mad);
  349. return err;
  350. }
  351. static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
  352. struct ib_device_modify *props)
  353. {
  354. struct mlx4_cmd_mailbox *mailbox;
  355. if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
  356. return -EOPNOTSUPP;
  357. if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
  358. return 0;
  359. spin_lock(&to_mdev(ibdev)->sm_lock);
  360. memcpy(ibdev->node_desc, props->node_desc, 64);
  361. spin_unlock(&to_mdev(ibdev)->sm_lock);
  362. /*
  363. * If possible, pass node desc to FW, so it can generate
  364. * a 144 trap. If cmd fails, just ignore.
  365. */
  366. mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
  367. if (IS_ERR(mailbox))
  368. return 0;
  369. memset(mailbox->buf, 0, 256);
  370. memcpy(mailbox->buf, props->node_desc, 64);
  371. mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
  372. MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  373. mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
  374. return 0;
  375. }
  376. static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
  377. u32 cap_mask)
  378. {
  379. struct mlx4_cmd_mailbox *mailbox;
  380. int err;
  381. u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
  382. mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  383. if (IS_ERR(mailbox))
  384. return PTR_ERR(mailbox);
  385. memset(mailbox->buf, 0, 256);
  386. if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  387. *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
  388. ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
  389. } else {
  390. ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
  391. ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
  392. }
  393. err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
  394. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  395. mlx4_free_cmd_mailbox(dev->dev, mailbox);
  396. return err;
  397. }
  398. static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
  399. struct ib_port_modify *props)
  400. {
  401. struct ib_port_attr attr;
  402. u32 cap_mask;
  403. int err;
  404. mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
  405. err = mlx4_ib_query_port(ibdev, port, &attr);
  406. if (err)
  407. goto out;
  408. cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  409. ~props->clr_port_cap_mask;
  410. err = mlx4_SET_PORT(to_mdev(ibdev), port,
  411. !!(mask & IB_PORT_RESET_QKEY_CNTR),
  412. cap_mask);
  413. out:
  414. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  415. return err;
  416. }
  417. static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
  418. struct ib_udata *udata)
  419. {
  420. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  421. struct mlx4_ib_ucontext *context;
  422. struct mlx4_ib_alloc_ucontext_resp resp;
  423. int err;
  424. if (!dev->ib_active)
  425. return ERR_PTR(-EAGAIN);
  426. resp.qp_tab_size = dev->dev->caps.num_qps;
  427. resp.bf_reg_size = dev->dev->caps.bf_reg_size;
  428. resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
  429. context = kmalloc(sizeof *context, GFP_KERNEL);
  430. if (!context)
  431. return ERR_PTR(-ENOMEM);
  432. err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
  433. if (err) {
  434. kfree(context);
  435. return ERR_PTR(err);
  436. }
  437. INIT_LIST_HEAD(&context->db_page_list);
  438. mutex_init(&context->db_page_mutex);
  439. err = ib_copy_to_udata(udata, &resp, sizeof resp);
  440. if (err) {
  441. mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
  442. kfree(context);
  443. return ERR_PTR(-EFAULT);
  444. }
  445. return &context->ibucontext;
  446. }
  447. static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
  448. {
  449. struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
  450. mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
  451. kfree(context);
  452. return 0;
  453. }
  454. static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  455. {
  456. struct mlx4_ib_dev *dev = to_mdev(context->device);
  457. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  458. return -EINVAL;
  459. if (vma->vm_pgoff == 0) {
  460. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  461. if (io_remap_pfn_range(vma, vma->vm_start,
  462. to_mucontext(context)->uar.pfn,
  463. PAGE_SIZE, vma->vm_page_prot))
  464. return -EAGAIN;
  465. } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
  466. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  467. if (io_remap_pfn_range(vma, vma->vm_start,
  468. to_mucontext(context)->uar.pfn +
  469. dev->dev->caps.num_uars,
  470. PAGE_SIZE, vma->vm_page_prot))
  471. return -EAGAIN;
  472. } else
  473. return -EINVAL;
  474. return 0;
  475. }
  476. static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
  477. struct ib_ucontext *context,
  478. struct ib_udata *udata)
  479. {
  480. struct mlx4_ib_pd *pd;
  481. int err;
  482. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  483. if (!pd)
  484. return ERR_PTR(-ENOMEM);
  485. err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
  486. if (err) {
  487. kfree(pd);
  488. return ERR_PTR(err);
  489. }
  490. if (context)
  491. if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
  492. mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
  493. kfree(pd);
  494. return ERR_PTR(-EFAULT);
  495. }
  496. return &pd->ibpd;
  497. }
  498. static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
  499. {
  500. mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
  501. kfree(pd);
  502. return 0;
  503. }
  504. static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
  505. struct ib_ucontext *context,
  506. struct ib_udata *udata)
  507. {
  508. struct mlx4_ib_xrcd *xrcd;
  509. int err;
  510. if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  511. return ERR_PTR(-ENOSYS);
  512. xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
  513. if (!xrcd)
  514. return ERR_PTR(-ENOMEM);
  515. err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
  516. if (err)
  517. goto err1;
  518. xrcd->pd = ib_alloc_pd(ibdev);
  519. if (IS_ERR(xrcd->pd)) {
  520. err = PTR_ERR(xrcd->pd);
  521. goto err2;
  522. }
  523. xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, 1, 0);
  524. if (IS_ERR(xrcd->cq)) {
  525. err = PTR_ERR(xrcd->cq);
  526. goto err3;
  527. }
  528. return &xrcd->ibxrcd;
  529. err3:
  530. ib_dealloc_pd(xrcd->pd);
  531. err2:
  532. mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
  533. err1:
  534. kfree(xrcd);
  535. return ERR_PTR(err);
  536. }
  537. static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
  538. {
  539. ib_destroy_cq(to_mxrcd(xrcd)->cq);
  540. ib_dealloc_pd(to_mxrcd(xrcd)->pd);
  541. mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
  542. kfree(xrcd);
  543. return 0;
  544. }
  545. static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
  546. {
  547. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  548. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  549. struct mlx4_ib_gid_entry *ge;
  550. ge = kzalloc(sizeof *ge, GFP_KERNEL);
  551. if (!ge)
  552. return -ENOMEM;
  553. ge->gid = *gid;
  554. if (mlx4_ib_add_mc(mdev, mqp, gid)) {
  555. ge->port = mqp->port;
  556. ge->added = 1;
  557. }
  558. mutex_lock(&mqp->mutex);
  559. list_add_tail(&ge->list, &mqp->gid_list);
  560. mutex_unlock(&mqp->mutex);
  561. return 0;
  562. }
  563. int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
  564. union ib_gid *gid)
  565. {
  566. u8 mac[6];
  567. struct net_device *ndev;
  568. int ret = 0;
  569. if (!mqp->port)
  570. return 0;
  571. spin_lock(&mdev->iboe.lock);
  572. ndev = mdev->iboe.netdevs[mqp->port - 1];
  573. if (ndev)
  574. dev_hold(ndev);
  575. spin_unlock(&mdev->iboe.lock);
  576. if (ndev) {
  577. rdma_get_mcast_mac((struct in6_addr *)gid, mac);
  578. rtnl_lock();
  579. dev_mc_add(mdev->iboe.netdevs[mqp->port - 1], mac);
  580. ret = 1;
  581. rtnl_unlock();
  582. dev_put(ndev);
  583. }
  584. return ret;
  585. }
  586. static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  587. {
  588. int err;
  589. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  590. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  591. err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw,
  592. !!(mqp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
  593. MLX4_PROT_IB_IPV6);
  594. if (err)
  595. return err;
  596. err = add_gid_entry(ibqp, gid);
  597. if (err)
  598. goto err_add;
  599. return 0;
  600. err_add:
  601. mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw, MLX4_PROT_IB_IPV6);
  602. return err;
  603. }
  604. static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
  605. {
  606. struct mlx4_ib_gid_entry *ge;
  607. struct mlx4_ib_gid_entry *tmp;
  608. struct mlx4_ib_gid_entry *ret = NULL;
  609. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  610. if (!memcmp(raw, ge->gid.raw, 16)) {
  611. ret = ge;
  612. break;
  613. }
  614. }
  615. return ret;
  616. }
  617. static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  618. {
  619. int err;
  620. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  621. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  622. u8 mac[6];
  623. struct net_device *ndev;
  624. struct mlx4_ib_gid_entry *ge;
  625. err = mlx4_multicast_detach(mdev->dev,
  626. &mqp->mqp, gid->raw, MLX4_PROT_IB_IPV6);
  627. if (err)
  628. return err;
  629. mutex_lock(&mqp->mutex);
  630. ge = find_gid_entry(mqp, gid->raw);
  631. if (ge) {
  632. spin_lock(&mdev->iboe.lock);
  633. ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
  634. if (ndev)
  635. dev_hold(ndev);
  636. spin_unlock(&mdev->iboe.lock);
  637. rdma_get_mcast_mac((struct in6_addr *)gid, mac);
  638. if (ndev) {
  639. rtnl_lock();
  640. dev_mc_del(mdev->iboe.netdevs[ge->port - 1], mac);
  641. rtnl_unlock();
  642. dev_put(ndev);
  643. }
  644. list_del(&ge->list);
  645. kfree(ge);
  646. } else
  647. printk(KERN_WARNING "could not find mgid entry\n");
  648. mutex_unlock(&mqp->mutex);
  649. return 0;
  650. }
  651. static int init_node_data(struct mlx4_ib_dev *dev)
  652. {
  653. struct ib_smp *in_mad = NULL;
  654. struct ib_smp *out_mad = NULL;
  655. int err = -ENOMEM;
  656. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  657. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  658. if (!in_mad || !out_mad)
  659. goto out;
  660. init_query_mad(in_mad);
  661. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  662. err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  663. if (err)
  664. goto out;
  665. memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
  666. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  667. err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  668. if (err)
  669. goto out;
  670. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  671. out:
  672. kfree(in_mad);
  673. kfree(out_mad);
  674. return err;
  675. }
  676. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  677. char *buf)
  678. {
  679. struct mlx4_ib_dev *dev =
  680. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  681. return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
  682. }
  683. static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
  684. char *buf)
  685. {
  686. struct mlx4_ib_dev *dev =
  687. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  688. return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
  689. (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
  690. (int) dev->dev->caps.fw_ver & 0xffff);
  691. }
  692. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  693. char *buf)
  694. {
  695. struct mlx4_ib_dev *dev =
  696. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  697. return sprintf(buf, "%x\n", dev->dev->rev_id);
  698. }
  699. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  700. char *buf)
  701. {
  702. struct mlx4_ib_dev *dev =
  703. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  704. return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
  705. dev->dev->board_id);
  706. }
  707. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  708. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  709. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  710. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  711. static struct device_attribute *mlx4_class_attributes[] = {
  712. &dev_attr_hw_rev,
  713. &dev_attr_fw_ver,
  714. &dev_attr_hca_type,
  715. &dev_attr_board_id
  716. };
  717. static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev)
  718. {
  719. memcpy(eui, dev->dev_addr, 3);
  720. memcpy(eui + 5, dev->dev_addr + 3, 3);
  721. if (vlan_id < 0x1000) {
  722. eui[3] = vlan_id >> 8;
  723. eui[4] = vlan_id & 0xff;
  724. } else {
  725. eui[3] = 0xff;
  726. eui[4] = 0xfe;
  727. }
  728. eui[0] ^= 2;
  729. }
  730. static void update_gids_task(struct work_struct *work)
  731. {
  732. struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
  733. struct mlx4_cmd_mailbox *mailbox;
  734. union ib_gid *gids;
  735. int err;
  736. struct mlx4_dev *dev = gw->dev->dev;
  737. struct ib_event event;
  738. mailbox = mlx4_alloc_cmd_mailbox(dev);
  739. if (IS_ERR(mailbox)) {
  740. printk(KERN_WARNING "update gid table failed %ld\n", PTR_ERR(mailbox));
  741. return;
  742. }
  743. gids = mailbox->buf;
  744. memcpy(gids, gw->gids, sizeof gw->gids);
  745. err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
  746. 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  747. MLX4_CMD_NATIVE);
  748. if (err)
  749. printk(KERN_WARNING "set port command failed\n");
  750. else {
  751. memcpy(gw->dev->iboe.gid_table[gw->port - 1], gw->gids, sizeof gw->gids);
  752. event.device = &gw->dev->ib_dev;
  753. event.element.port_num = gw->port;
  754. event.event = IB_EVENT_GID_CHANGE;
  755. ib_dispatch_event(&event);
  756. }
  757. mlx4_free_cmd_mailbox(dev, mailbox);
  758. kfree(gw);
  759. }
  760. static int update_ipv6_gids(struct mlx4_ib_dev *dev, int port, int clear)
  761. {
  762. struct net_device *ndev = dev->iboe.netdevs[port - 1];
  763. struct update_gid_work *work;
  764. struct net_device *tmp;
  765. int i;
  766. u8 *hits;
  767. int ret;
  768. union ib_gid gid;
  769. int free;
  770. int found;
  771. int need_update = 0;
  772. u16 vid;
  773. work = kzalloc(sizeof *work, GFP_ATOMIC);
  774. if (!work)
  775. return -ENOMEM;
  776. hits = kzalloc(128, GFP_ATOMIC);
  777. if (!hits) {
  778. ret = -ENOMEM;
  779. goto out;
  780. }
  781. rcu_read_lock();
  782. for_each_netdev_rcu(&init_net, tmp) {
  783. if (ndev && (tmp == ndev || rdma_vlan_dev_real_dev(tmp) == ndev)) {
  784. gid.global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
  785. vid = rdma_vlan_dev_vlan_id(tmp);
  786. mlx4_addrconf_ifid_eui48(&gid.raw[8], vid, ndev);
  787. found = 0;
  788. free = -1;
  789. for (i = 0; i < 128; ++i) {
  790. if (free < 0 &&
  791. !memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
  792. free = i;
  793. if (!memcmp(&dev->iboe.gid_table[port - 1][i], &gid, sizeof gid)) {
  794. hits[i] = 1;
  795. found = 1;
  796. break;
  797. }
  798. }
  799. if (!found) {
  800. if (tmp == ndev &&
  801. (memcmp(&dev->iboe.gid_table[port - 1][0],
  802. &gid, sizeof gid) ||
  803. !memcmp(&dev->iboe.gid_table[port - 1][0],
  804. &zgid, sizeof gid))) {
  805. dev->iboe.gid_table[port - 1][0] = gid;
  806. ++need_update;
  807. hits[0] = 1;
  808. } else if (free >= 0) {
  809. dev->iboe.gid_table[port - 1][free] = gid;
  810. hits[free] = 1;
  811. ++need_update;
  812. }
  813. }
  814. }
  815. }
  816. rcu_read_unlock();
  817. for (i = 0; i < 128; ++i)
  818. if (!hits[i]) {
  819. if (memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
  820. ++need_update;
  821. dev->iboe.gid_table[port - 1][i] = zgid;
  822. }
  823. if (need_update) {
  824. memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof work->gids);
  825. INIT_WORK(&work->work, update_gids_task);
  826. work->port = port;
  827. work->dev = dev;
  828. queue_work(wq, &work->work);
  829. } else
  830. kfree(work);
  831. kfree(hits);
  832. return 0;
  833. out:
  834. kfree(work);
  835. return ret;
  836. }
  837. static void handle_en_event(struct mlx4_ib_dev *dev, int port, unsigned long event)
  838. {
  839. switch (event) {
  840. case NETDEV_UP:
  841. case NETDEV_CHANGEADDR:
  842. update_ipv6_gids(dev, port, 0);
  843. break;
  844. case NETDEV_DOWN:
  845. update_ipv6_gids(dev, port, 1);
  846. dev->iboe.netdevs[port - 1] = NULL;
  847. }
  848. }
  849. static void netdev_added(struct mlx4_ib_dev *dev, int port)
  850. {
  851. update_ipv6_gids(dev, port, 0);
  852. }
  853. static void netdev_removed(struct mlx4_ib_dev *dev, int port)
  854. {
  855. update_ipv6_gids(dev, port, 1);
  856. }
  857. static int mlx4_ib_netdev_event(struct notifier_block *this, unsigned long event,
  858. void *ptr)
  859. {
  860. struct net_device *dev = ptr;
  861. struct mlx4_ib_dev *ibdev;
  862. struct net_device *oldnd;
  863. struct mlx4_ib_iboe *iboe;
  864. int port;
  865. if (!net_eq(dev_net(dev), &init_net))
  866. return NOTIFY_DONE;
  867. ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
  868. iboe = &ibdev->iboe;
  869. spin_lock(&iboe->lock);
  870. mlx4_foreach_ib_transport_port(port, ibdev->dev) {
  871. oldnd = iboe->netdevs[port - 1];
  872. iboe->netdevs[port - 1] =
  873. mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
  874. if (oldnd != iboe->netdevs[port - 1]) {
  875. if (iboe->netdevs[port - 1])
  876. netdev_added(ibdev, port);
  877. else
  878. netdev_removed(ibdev, port);
  879. }
  880. }
  881. if (dev == iboe->netdevs[0] ||
  882. (iboe->netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[0]))
  883. handle_en_event(ibdev, 1, event);
  884. else if (dev == iboe->netdevs[1]
  885. || (iboe->netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[1]))
  886. handle_en_event(ibdev, 2, event);
  887. spin_unlock(&iboe->lock);
  888. return NOTIFY_DONE;
  889. }
  890. static void *mlx4_ib_add(struct mlx4_dev *dev)
  891. {
  892. struct mlx4_ib_dev *ibdev;
  893. int num_ports = 0;
  894. int i;
  895. int err;
  896. struct mlx4_ib_iboe *iboe;
  897. printk_once(KERN_INFO "%s", mlx4_ib_version);
  898. if (mlx4_is_mfunc(dev)) {
  899. printk(KERN_WARNING "IB not yet supported in SRIOV\n");
  900. return NULL;
  901. }
  902. mlx4_foreach_ib_transport_port(i, dev)
  903. num_ports++;
  904. /* No point in registering a device with no ports... */
  905. if (num_ports == 0)
  906. return NULL;
  907. ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
  908. if (!ibdev) {
  909. dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
  910. return NULL;
  911. }
  912. iboe = &ibdev->iboe;
  913. if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
  914. goto err_dealloc;
  915. if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
  916. goto err_pd;
  917. ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
  918. PAGE_SIZE);
  919. if (!ibdev->uar_map)
  920. goto err_uar;
  921. MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
  922. ibdev->dev = dev;
  923. strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
  924. ibdev->ib_dev.owner = THIS_MODULE;
  925. ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
  926. ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
  927. ibdev->num_ports = num_ports;
  928. ibdev->ib_dev.phys_port_cnt = ibdev->num_ports;
  929. ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
  930. ibdev->ib_dev.dma_device = &dev->pdev->dev;
  931. ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
  932. ibdev->ib_dev.uverbs_cmd_mask =
  933. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  934. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  935. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  936. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  937. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  938. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  939. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  940. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  941. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  942. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  943. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  944. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  945. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  946. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  947. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  948. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  949. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  950. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  951. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  952. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  953. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
  954. (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
  955. (1ull << IB_USER_VERBS_CMD_OPEN_QP);
  956. ibdev->ib_dev.query_device = mlx4_ib_query_device;
  957. ibdev->ib_dev.query_port = mlx4_ib_query_port;
  958. ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
  959. ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
  960. ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
  961. ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
  962. ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
  963. ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
  964. ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
  965. ibdev->ib_dev.mmap = mlx4_ib_mmap;
  966. ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
  967. ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
  968. ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
  969. ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
  970. ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
  971. ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
  972. ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
  973. ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
  974. ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
  975. ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
  976. ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
  977. ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
  978. ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
  979. ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
  980. ibdev->ib_dev.post_send = mlx4_ib_post_send;
  981. ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
  982. ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
  983. ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
  984. ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
  985. ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
  986. ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
  987. ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
  988. ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
  989. ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
  990. ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
  991. ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
  992. ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
  993. ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list;
  994. ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
  995. ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
  996. ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
  997. ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
  998. ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
  999. ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
  1000. ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
  1001. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
  1002. ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
  1003. ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
  1004. ibdev->ib_dev.uverbs_cmd_mask |=
  1005. (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
  1006. (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
  1007. }
  1008. spin_lock_init(&iboe->lock);
  1009. if (init_node_data(ibdev))
  1010. goto err_map;
  1011. for (i = 0; i < ibdev->num_ports; ++i) {
  1012. if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
  1013. IB_LINK_LAYER_ETHERNET) {
  1014. err = mlx4_counter_alloc(ibdev->dev, &ibdev->counters[i]);
  1015. if (err)
  1016. ibdev->counters[i] = -1;
  1017. } else
  1018. ibdev->counters[i] = -1;
  1019. }
  1020. spin_lock_init(&ibdev->sm_lock);
  1021. mutex_init(&ibdev->cap_mask_mutex);
  1022. if (ib_register_device(&ibdev->ib_dev, NULL))
  1023. goto err_counter;
  1024. if (mlx4_ib_mad_init(ibdev))
  1025. goto err_reg;
  1026. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE && !iboe->nb.notifier_call) {
  1027. iboe->nb.notifier_call = mlx4_ib_netdev_event;
  1028. err = register_netdevice_notifier(&iboe->nb);
  1029. if (err)
  1030. goto err_reg;
  1031. }
  1032. for (i = 0; i < ARRAY_SIZE(mlx4_class_attributes); ++i) {
  1033. if (device_create_file(&ibdev->ib_dev.dev,
  1034. mlx4_class_attributes[i]))
  1035. goto err_notif;
  1036. }
  1037. ibdev->ib_active = true;
  1038. return ibdev;
  1039. err_notif:
  1040. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  1041. printk(KERN_WARNING "failure unregistering notifier\n");
  1042. flush_workqueue(wq);
  1043. err_reg:
  1044. ib_unregister_device(&ibdev->ib_dev);
  1045. err_counter:
  1046. for (; i; --i)
  1047. if (ibdev->counters[i - 1] != -1)
  1048. mlx4_counter_free(ibdev->dev, ibdev->counters[i - 1]);
  1049. err_map:
  1050. iounmap(ibdev->uar_map);
  1051. err_uar:
  1052. mlx4_uar_free(dev, &ibdev->priv_uar);
  1053. err_pd:
  1054. mlx4_pd_free(dev, ibdev->priv_pdn);
  1055. err_dealloc:
  1056. ib_dealloc_device(&ibdev->ib_dev);
  1057. return NULL;
  1058. }
  1059. static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
  1060. {
  1061. struct mlx4_ib_dev *ibdev = ibdev_ptr;
  1062. int p;
  1063. mlx4_ib_mad_cleanup(ibdev);
  1064. ib_unregister_device(&ibdev->ib_dev);
  1065. if (ibdev->iboe.nb.notifier_call) {
  1066. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  1067. printk(KERN_WARNING "failure unregistering notifier\n");
  1068. ibdev->iboe.nb.notifier_call = NULL;
  1069. }
  1070. iounmap(ibdev->uar_map);
  1071. for (p = 0; p < ibdev->num_ports; ++p)
  1072. if (ibdev->counters[p] != -1)
  1073. mlx4_counter_free(ibdev->dev, ibdev->counters[p]);
  1074. mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
  1075. mlx4_CLOSE_PORT(dev, p);
  1076. mlx4_uar_free(dev, &ibdev->priv_uar);
  1077. mlx4_pd_free(dev, ibdev->priv_pdn);
  1078. ib_dealloc_device(&ibdev->ib_dev);
  1079. }
  1080. static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
  1081. enum mlx4_dev_event event, int port)
  1082. {
  1083. struct ib_event ibev;
  1084. struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
  1085. if (port > ibdev->num_ports)
  1086. return;
  1087. switch (event) {
  1088. case MLX4_DEV_EVENT_PORT_UP:
  1089. ibev.event = IB_EVENT_PORT_ACTIVE;
  1090. break;
  1091. case MLX4_DEV_EVENT_PORT_DOWN:
  1092. ibev.event = IB_EVENT_PORT_ERR;
  1093. break;
  1094. case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
  1095. ibdev->ib_active = false;
  1096. ibev.event = IB_EVENT_DEVICE_FATAL;
  1097. break;
  1098. default:
  1099. return;
  1100. }
  1101. ibev.device = ibdev_ptr;
  1102. ibev.element.port_num = port;
  1103. ib_dispatch_event(&ibev);
  1104. }
  1105. static struct mlx4_interface mlx4_ib_interface = {
  1106. .add = mlx4_ib_add,
  1107. .remove = mlx4_ib_remove,
  1108. .event = mlx4_ib_event,
  1109. .protocol = MLX4_PROT_IB_IPV6
  1110. };
  1111. static int __init mlx4_ib_init(void)
  1112. {
  1113. int err;
  1114. wq = create_singlethread_workqueue("mlx4_ib");
  1115. if (!wq)
  1116. return -ENOMEM;
  1117. err = mlx4_register_interface(&mlx4_ib_interface);
  1118. if (err) {
  1119. destroy_workqueue(wq);
  1120. return err;
  1121. }
  1122. return 0;
  1123. }
  1124. static void __exit mlx4_ib_cleanup(void)
  1125. {
  1126. mlx4_unregister_interface(&mlx4_ib_interface);
  1127. destroy_workqueue(wq);
  1128. }
  1129. module_init(mlx4_ib_init);
  1130. module_exit(mlx4_ib_cleanup);