main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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 <rdma/ib_smi.h>
  38. #include <rdma/ib_user_verbs.h>
  39. #include <linux/mlx4/driver.h>
  40. #include <linux/mlx4/cmd.h>
  41. #include "mlx4_ib.h"
  42. #include "user.h"
  43. #define DRV_NAME "mlx4_ib"
  44. #define DRV_VERSION "1.0"
  45. #define DRV_RELDATE "April 4, 2008"
  46. MODULE_AUTHOR("Roland Dreier");
  47. MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
  48. MODULE_LICENSE("Dual BSD/GPL");
  49. MODULE_VERSION(DRV_VERSION);
  50. static const char mlx4_ib_version[] =
  51. DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
  52. DRV_VERSION " (" DRV_RELDATE ")\n";
  53. static void init_query_mad(struct ib_smp *mad)
  54. {
  55. mad->base_version = 1;
  56. mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  57. mad->class_version = 1;
  58. mad->method = IB_MGMT_METHOD_GET;
  59. }
  60. static int mlx4_ib_query_device(struct ib_device *ibdev,
  61. struct ib_device_attr *props)
  62. {
  63. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  64. struct ib_smp *in_mad = NULL;
  65. struct ib_smp *out_mad = NULL;
  66. int err = -ENOMEM;
  67. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  68. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  69. if (!in_mad || !out_mad)
  70. goto out;
  71. init_query_mad(in_mad);
  72. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  73. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad, out_mad);
  74. if (err)
  75. goto out;
  76. memset(props, 0, sizeof *props);
  77. props->fw_ver = dev->dev->caps.fw_ver;
  78. props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
  79. IB_DEVICE_PORT_ACTIVE_EVENT |
  80. IB_DEVICE_SYS_IMAGE_GUID |
  81. IB_DEVICE_RC_RNR_NAK_GEN |
  82. IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
  83. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
  84. props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
  85. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
  86. props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
  87. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM)
  88. props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
  89. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
  90. props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
  91. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
  92. props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
  93. if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH)
  94. props->device_cap_flags |= IB_DEVICE_UD_TSO;
  95. if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
  96. props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
  97. if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
  98. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
  99. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
  100. props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
  101. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  102. 0xffffff;
  103. props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
  104. props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
  105. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  106. props->max_mr_size = ~0ull;
  107. props->page_size_cap = dev->dev->caps.page_size_cap;
  108. props->max_qp = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps;
  109. props->max_qp_wr = dev->dev->caps.max_wqes;
  110. props->max_sge = min(dev->dev->caps.max_sq_sg,
  111. dev->dev->caps.max_rq_sg);
  112. props->max_cq = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs;
  113. props->max_cqe = dev->dev->caps.max_cqes;
  114. props->max_mr = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws;
  115. props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
  116. props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
  117. props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
  118. props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
  119. props->max_srq = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs;
  120. props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
  121. props->max_srq_sge = dev->dev->caps.max_srq_sge;
  122. props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
  123. props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
  124. props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
  125. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  126. props->masked_atomic_cap = IB_ATOMIC_HCA;
  127. props->max_pkeys = dev->dev->caps.pkey_table_len[1];
  128. props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
  129. props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
  130. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  131. props->max_mcast_grp;
  132. props->max_map_per_fmr = (1 << (32 - ilog2(dev->dev->caps.num_mpts))) - 1;
  133. out:
  134. kfree(in_mad);
  135. kfree(out_mad);
  136. return err;
  137. }
  138. static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
  139. struct ib_port_attr *props)
  140. {
  141. struct ib_smp *in_mad = NULL;
  142. struct ib_smp *out_mad = NULL;
  143. int err = -ENOMEM;
  144. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  145. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  146. if (!in_mad || !out_mad)
  147. goto out;
  148. memset(props, 0, sizeof *props);
  149. init_query_mad(in_mad);
  150. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  151. in_mad->attr_mod = cpu_to_be32(port);
  152. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  153. if (err)
  154. goto out;
  155. props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
  156. props->lmc = out_mad->data[34] & 0x7;
  157. props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
  158. props->sm_sl = out_mad->data[36] & 0xf;
  159. props->state = out_mad->data[32] & 0xf;
  160. props->phys_state = out_mad->data[33] >> 4;
  161. props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
  162. props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
  163. props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
  164. props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
  165. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  166. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  167. props->active_width = out_mad->data[31] & 0xf;
  168. props->active_speed = out_mad->data[35] >> 4;
  169. props->max_mtu = out_mad->data[41] & 0xf;
  170. props->active_mtu = out_mad->data[36] >> 4;
  171. props->subnet_timeout = out_mad->data[51] & 0x1f;
  172. props->max_vl_num = out_mad->data[37] >> 4;
  173. props->init_type_reply = out_mad->data[41] >> 4;
  174. out:
  175. kfree(in_mad);
  176. kfree(out_mad);
  177. return err;
  178. }
  179. static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  180. union ib_gid *gid)
  181. {
  182. struct ib_smp *in_mad = NULL;
  183. struct ib_smp *out_mad = NULL;
  184. int err = -ENOMEM;
  185. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  186. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  187. if (!in_mad || !out_mad)
  188. goto out;
  189. init_query_mad(in_mad);
  190. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  191. in_mad->attr_mod = cpu_to_be32(port);
  192. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  193. if (err)
  194. goto out;
  195. memcpy(gid->raw, out_mad->data + 8, 8);
  196. init_query_mad(in_mad);
  197. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  198. in_mad->attr_mod = cpu_to_be32(index / 8);
  199. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  200. if (err)
  201. goto out;
  202. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  203. out:
  204. kfree(in_mad);
  205. kfree(out_mad);
  206. return err;
  207. }
  208. static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  209. u16 *pkey)
  210. {
  211. struct ib_smp *in_mad = NULL;
  212. struct ib_smp *out_mad = NULL;
  213. int err = -ENOMEM;
  214. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  215. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  216. if (!in_mad || !out_mad)
  217. goto out;
  218. init_query_mad(in_mad);
  219. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  220. in_mad->attr_mod = cpu_to_be32(index / 32);
  221. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  222. if (err)
  223. goto out;
  224. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  225. out:
  226. kfree(in_mad);
  227. kfree(out_mad);
  228. return err;
  229. }
  230. static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
  231. struct ib_device_modify *props)
  232. {
  233. struct mlx4_cmd_mailbox *mailbox;
  234. if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
  235. return -EOPNOTSUPP;
  236. if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
  237. return 0;
  238. spin_lock(&to_mdev(ibdev)->sm_lock);
  239. memcpy(ibdev->node_desc, props->node_desc, 64);
  240. spin_unlock(&to_mdev(ibdev)->sm_lock);
  241. /*
  242. * If possible, pass node desc to FW, so it can generate
  243. * a 144 trap. If cmd fails, just ignore.
  244. */
  245. mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
  246. if (IS_ERR(mailbox))
  247. return 0;
  248. memset(mailbox->buf, 0, 256);
  249. memcpy(mailbox->buf, props->node_desc, 64);
  250. mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
  251. MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A);
  252. mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
  253. return 0;
  254. }
  255. static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
  256. u32 cap_mask)
  257. {
  258. struct mlx4_cmd_mailbox *mailbox;
  259. int err;
  260. mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  261. if (IS_ERR(mailbox))
  262. return PTR_ERR(mailbox);
  263. memset(mailbox->buf, 0, 256);
  264. if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  265. *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
  266. ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
  267. } else {
  268. ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
  269. ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
  270. }
  271. err = mlx4_cmd(dev->dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
  272. MLX4_CMD_TIME_CLASS_B);
  273. mlx4_free_cmd_mailbox(dev->dev, mailbox);
  274. return err;
  275. }
  276. static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
  277. struct ib_port_modify *props)
  278. {
  279. struct ib_port_attr attr;
  280. u32 cap_mask;
  281. int err;
  282. mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
  283. err = mlx4_ib_query_port(ibdev, port, &attr);
  284. if (err)
  285. goto out;
  286. cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  287. ~props->clr_port_cap_mask;
  288. err = mlx4_SET_PORT(to_mdev(ibdev), port,
  289. !!(mask & IB_PORT_RESET_QKEY_CNTR),
  290. cap_mask);
  291. out:
  292. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  293. return err;
  294. }
  295. static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
  296. struct ib_udata *udata)
  297. {
  298. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  299. struct mlx4_ib_ucontext *context;
  300. struct mlx4_ib_alloc_ucontext_resp resp;
  301. int err;
  302. if (!dev->ib_active)
  303. return ERR_PTR(-EAGAIN);
  304. resp.qp_tab_size = dev->dev->caps.num_qps;
  305. resp.bf_reg_size = dev->dev->caps.bf_reg_size;
  306. resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
  307. context = kmalloc(sizeof *context, GFP_KERNEL);
  308. if (!context)
  309. return ERR_PTR(-ENOMEM);
  310. err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
  311. if (err) {
  312. kfree(context);
  313. return ERR_PTR(err);
  314. }
  315. INIT_LIST_HEAD(&context->db_page_list);
  316. mutex_init(&context->db_page_mutex);
  317. err = ib_copy_to_udata(udata, &resp, sizeof resp);
  318. if (err) {
  319. mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
  320. kfree(context);
  321. return ERR_PTR(-EFAULT);
  322. }
  323. return &context->ibucontext;
  324. }
  325. static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
  326. {
  327. struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
  328. mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
  329. kfree(context);
  330. return 0;
  331. }
  332. static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  333. {
  334. struct mlx4_ib_dev *dev = to_mdev(context->device);
  335. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  336. return -EINVAL;
  337. if (vma->vm_pgoff == 0) {
  338. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  339. if (io_remap_pfn_range(vma, vma->vm_start,
  340. to_mucontext(context)->uar.pfn,
  341. PAGE_SIZE, vma->vm_page_prot))
  342. return -EAGAIN;
  343. } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
  344. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  345. if (io_remap_pfn_range(vma, vma->vm_start,
  346. to_mucontext(context)->uar.pfn +
  347. dev->dev->caps.num_uars,
  348. PAGE_SIZE, vma->vm_page_prot))
  349. return -EAGAIN;
  350. } else
  351. return -EINVAL;
  352. return 0;
  353. }
  354. static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
  355. struct ib_ucontext *context,
  356. struct ib_udata *udata)
  357. {
  358. struct mlx4_ib_pd *pd;
  359. int err;
  360. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  361. if (!pd)
  362. return ERR_PTR(-ENOMEM);
  363. err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
  364. if (err) {
  365. kfree(pd);
  366. return ERR_PTR(err);
  367. }
  368. if (context)
  369. if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
  370. mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
  371. kfree(pd);
  372. return ERR_PTR(-EFAULT);
  373. }
  374. return &pd->ibpd;
  375. }
  376. static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
  377. {
  378. mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
  379. kfree(pd);
  380. return 0;
  381. }
  382. static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  383. {
  384. return mlx4_multicast_attach(to_mdev(ibqp->device)->dev,
  385. &to_mqp(ibqp)->mqp, gid->raw,
  386. !!(to_mqp(ibqp)->flags &
  387. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK));
  388. }
  389. static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  390. {
  391. return mlx4_multicast_detach(to_mdev(ibqp->device)->dev,
  392. &to_mqp(ibqp)->mqp, gid->raw);
  393. }
  394. static int init_node_data(struct mlx4_ib_dev *dev)
  395. {
  396. struct ib_smp *in_mad = NULL;
  397. struct ib_smp *out_mad = NULL;
  398. int err = -ENOMEM;
  399. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  400. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  401. if (!in_mad || !out_mad)
  402. goto out;
  403. init_query_mad(in_mad);
  404. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  405. err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  406. if (err)
  407. goto out;
  408. memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
  409. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  410. err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  411. if (err)
  412. goto out;
  413. dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
  414. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  415. out:
  416. kfree(in_mad);
  417. kfree(out_mad);
  418. return err;
  419. }
  420. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  421. char *buf)
  422. {
  423. struct mlx4_ib_dev *dev =
  424. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  425. return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
  426. }
  427. static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
  428. char *buf)
  429. {
  430. struct mlx4_ib_dev *dev =
  431. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  432. return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
  433. (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
  434. (int) dev->dev->caps.fw_ver & 0xffff);
  435. }
  436. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  437. char *buf)
  438. {
  439. struct mlx4_ib_dev *dev =
  440. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  441. return sprintf(buf, "%x\n", dev->dev->rev_id);
  442. }
  443. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  444. char *buf)
  445. {
  446. struct mlx4_ib_dev *dev =
  447. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  448. return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
  449. dev->dev->board_id);
  450. }
  451. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  452. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  453. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  454. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  455. static struct device_attribute *mlx4_class_attributes[] = {
  456. &dev_attr_hw_rev,
  457. &dev_attr_fw_ver,
  458. &dev_attr_hca_type,
  459. &dev_attr_board_id
  460. };
  461. static void *mlx4_ib_add(struct mlx4_dev *dev)
  462. {
  463. struct mlx4_ib_dev *ibdev;
  464. int num_ports = 0;
  465. int i;
  466. printk_once(KERN_INFO "%s", mlx4_ib_version);
  467. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  468. num_ports++;
  469. /* No point in registering a device with no ports... */
  470. if (num_ports == 0)
  471. return NULL;
  472. ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
  473. if (!ibdev) {
  474. dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
  475. return NULL;
  476. }
  477. if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
  478. goto err_dealloc;
  479. if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
  480. goto err_pd;
  481. ibdev->uar_map = ioremap(ibdev->priv_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
  482. if (!ibdev->uar_map)
  483. goto err_uar;
  484. MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
  485. ibdev->dev = dev;
  486. strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
  487. ibdev->ib_dev.owner = THIS_MODULE;
  488. ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
  489. ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
  490. ibdev->num_ports = num_ports;
  491. ibdev->ib_dev.phys_port_cnt = ibdev->num_ports;
  492. ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
  493. ibdev->ib_dev.dma_device = &dev->pdev->dev;
  494. ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
  495. ibdev->ib_dev.uverbs_cmd_mask =
  496. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  497. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  498. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  499. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  500. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  501. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  502. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  503. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  504. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  505. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  506. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  507. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  508. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  509. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  510. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  511. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  512. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  513. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  514. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  515. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  516. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
  517. ibdev->ib_dev.query_device = mlx4_ib_query_device;
  518. ibdev->ib_dev.query_port = mlx4_ib_query_port;
  519. ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
  520. ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
  521. ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
  522. ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
  523. ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
  524. ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
  525. ibdev->ib_dev.mmap = mlx4_ib_mmap;
  526. ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
  527. ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
  528. ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
  529. ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
  530. ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
  531. ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
  532. ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
  533. ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
  534. ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
  535. ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
  536. ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
  537. ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
  538. ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
  539. ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
  540. ibdev->ib_dev.post_send = mlx4_ib_post_send;
  541. ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
  542. ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
  543. ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
  544. ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
  545. ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
  546. ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
  547. ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
  548. ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
  549. ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
  550. ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
  551. ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
  552. ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
  553. ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list;
  554. ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
  555. ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
  556. ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
  557. ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
  558. ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
  559. ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
  560. ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
  561. if (init_node_data(ibdev))
  562. goto err_map;
  563. spin_lock_init(&ibdev->sm_lock);
  564. mutex_init(&ibdev->cap_mask_mutex);
  565. if (ib_register_device(&ibdev->ib_dev, NULL))
  566. goto err_map;
  567. if (mlx4_ib_mad_init(ibdev))
  568. goto err_reg;
  569. for (i = 0; i < ARRAY_SIZE(mlx4_class_attributes); ++i) {
  570. if (device_create_file(&ibdev->ib_dev.dev,
  571. mlx4_class_attributes[i]))
  572. goto err_reg;
  573. }
  574. ibdev->ib_active = true;
  575. return ibdev;
  576. err_reg:
  577. ib_unregister_device(&ibdev->ib_dev);
  578. err_map:
  579. iounmap(ibdev->uar_map);
  580. err_uar:
  581. mlx4_uar_free(dev, &ibdev->priv_uar);
  582. err_pd:
  583. mlx4_pd_free(dev, ibdev->priv_pdn);
  584. err_dealloc:
  585. ib_dealloc_device(&ibdev->ib_dev);
  586. return NULL;
  587. }
  588. static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
  589. {
  590. struct mlx4_ib_dev *ibdev = ibdev_ptr;
  591. int p;
  592. mlx4_ib_mad_cleanup(ibdev);
  593. ib_unregister_device(&ibdev->ib_dev);
  594. for (p = 1; p <= ibdev->num_ports; ++p)
  595. mlx4_CLOSE_PORT(dev, p);
  596. iounmap(ibdev->uar_map);
  597. mlx4_uar_free(dev, &ibdev->priv_uar);
  598. mlx4_pd_free(dev, ibdev->priv_pdn);
  599. ib_dealloc_device(&ibdev->ib_dev);
  600. }
  601. static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
  602. enum mlx4_dev_event event, int port)
  603. {
  604. struct ib_event ibev;
  605. struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
  606. if (port > ibdev->num_ports)
  607. return;
  608. switch (event) {
  609. case MLX4_DEV_EVENT_PORT_UP:
  610. ibev.event = IB_EVENT_PORT_ACTIVE;
  611. break;
  612. case MLX4_DEV_EVENT_PORT_DOWN:
  613. ibev.event = IB_EVENT_PORT_ERR;
  614. break;
  615. case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
  616. ibdev->ib_active = false;
  617. ibev.event = IB_EVENT_DEVICE_FATAL;
  618. break;
  619. default:
  620. return;
  621. }
  622. ibev.device = ibdev_ptr;
  623. ibev.element.port_num = port;
  624. ib_dispatch_event(&ibev);
  625. }
  626. static struct mlx4_interface mlx4_ib_interface = {
  627. .add = mlx4_ib_add,
  628. .remove = mlx4_ib_remove,
  629. .event = mlx4_ib_event
  630. };
  631. static int __init mlx4_ib_init(void)
  632. {
  633. return mlx4_register_interface(&mlx4_ib_interface);
  634. }
  635. static void __exit mlx4_ib_cleanup(void)
  636. {
  637. mlx4_unregister_interface(&mlx4_ib_interface);
  638. }
  639. module_init(mlx4_ib_init);
  640. module_exit(mlx4_ib_cleanup);