main.c 21 KB

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