main.c 22 KB

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