mthca_provider.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
  5. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  6. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  7. *
  8. * This software is available to you under a choice of one of two
  9. * licenses. You may choose to be licensed under the terms of the GNU
  10. * General Public License (GPL) Version 2, available from the file
  11. * COPYING in the main directory of this source tree, or the
  12. * OpenIB.org BSD license below:
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials
  25. * provided with the distribution.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  31. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  32. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  33. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  34. * SOFTWARE.
  35. */
  36. #include <rdma/ib_smi.h>
  37. #include <rdma/ib_umem.h>
  38. #include <rdma/ib_user_verbs.h>
  39. #include <rdma/uverbs_ioctl.h>
  40. #include <linux/sched.h>
  41. #include <linux/slab.h>
  42. #include <linux/stat.h>
  43. #include <linux/mm.h>
  44. #include <linux/export.h>
  45. #include "mthca_dev.h"
  46. #include "mthca_cmd.h"
  47. #include <rdma/mthca-abi.h>
  48. #include "mthca_memfree.h"
  49. static int mthca_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
  50. struct ib_udata *uhw)
  51. {
  52. struct ib_smp *in_mad;
  53. struct ib_smp *out_mad;
  54. int err = -ENOMEM;
  55. struct mthca_dev *mdev = to_mdev(ibdev);
  56. if (uhw->inlen || uhw->outlen)
  57. return -EINVAL;
  58. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  59. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  60. if (!in_mad || !out_mad)
  61. goto out;
  62. memset(props, 0, sizeof *props);
  63. props->fw_ver = mdev->fw_ver;
  64. ib_init_query_mad(in_mad);
  65. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  66. err = mthca_MAD_IFC(mdev, 1, 1,
  67. 1, NULL, NULL, in_mad, out_mad);
  68. if (err)
  69. goto out;
  70. props->device_cap_flags = mdev->device_cap_flags;
  71. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  72. 0xffffff;
  73. props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
  74. props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
  75. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  76. props->max_mr_size = ~0ull;
  77. props->page_size_cap = mdev->limits.page_size_cap;
  78. props->max_qp = mdev->limits.num_qps - mdev->limits.reserved_qps;
  79. props->max_qp_wr = mdev->limits.max_wqes;
  80. props->max_send_sge = mdev->limits.max_sg;
  81. props->max_recv_sge = mdev->limits.max_sg;
  82. props->max_sge_rd = mdev->limits.max_sg;
  83. props->max_cq = mdev->limits.num_cqs - mdev->limits.reserved_cqs;
  84. props->max_cqe = mdev->limits.max_cqes;
  85. props->max_mr = mdev->limits.num_mpts - mdev->limits.reserved_mrws;
  86. props->max_pd = mdev->limits.num_pds - mdev->limits.reserved_pds;
  87. props->max_qp_rd_atom = 1 << mdev->qp_table.rdb_shift;
  88. props->max_qp_init_rd_atom = mdev->limits.max_qp_init_rdma;
  89. props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
  90. props->max_srq = mdev->limits.num_srqs - mdev->limits.reserved_srqs;
  91. props->max_srq_wr = mdev->limits.max_srq_wqes;
  92. props->max_srq_sge = mdev->limits.max_srq_sge;
  93. props->local_ca_ack_delay = mdev->limits.local_ca_ack_delay;
  94. props->atomic_cap = mdev->limits.flags & DEV_LIM_FLAG_ATOMIC ?
  95. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  96. props->max_pkeys = mdev->limits.pkey_table_len;
  97. props->max_mcast_grp = mdev->limits.num_mgms + mdev->limits.num_amgms;
  98. props->max_mcast_qp_attach = MTHCA_QP_PER_MGM;
  99. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  100. props->max_mcast_grp;
  101. err = 0;
  102. out:
  103. kfree(in_mad);
  104. kfree(out_mad);
  105. return err;
  106. }
  107. static int mthca_query_port(struct ib_device *ibdev,
  108. u32 port, struct ib_port_attr *props)
  109. {
  110. struct ib_smp *in_mad;
  111. struct ib_smp *out_mad;
  112. int err = -ENOMEM;
  113. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  114. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  115. if (!in_mad || !out_mad)
  116. goto out;
  117. /* props being zeroed by the caller, avoid zeroing it here */
  118. ib_init_query_mad(in_mad);
  119. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  120. in_mad->attr_mod = cpu_to_be32(port);
  121. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  122. port, NULL, NULL, in_mad, out_mad);
  123. if (err)
  124. goto out;
  125. props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
  126. props->lmc = out_mad->data[34] & 0x7;
  127. props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
  128. props->sm_sl = out_mad->data[36] & 0xf;
  129. props->state = out_mad->data[32] & 0xf;
  130. props->phys_state = out_mad->data[33] >> 4;
  131. props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
  132. props->gid_tbl_len = to_mdev(ibdev)->limits.gid_table_len;
  133. props->max_msg_sz = 0x80000000;
  134. props->pkey_tbl_len = to_mdev(ibdev)->limits.pkey_table_len;
  135. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  136. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  137. props->active_width = out_mad->data[31] & 0xf;
  138. props->active_speed = out_mad->data[35] >> 4;
  139. props->max_mtu = out_mad->data[41] & 0xf;
  140. props->active_mtu = out_mad->data[36] >> 4;
  141. props->subnet_timeout = out_mad->data[51] & 0x1f;
  142. props->max_vl_num = out_mad->data[37] >> 4;
  143. props->init_type_reply = out_mad->data[41] >> 4;
  144. out:
  145. kfree(in_mad);
  146. kfree(out_mad);
  147. return err;
  148. }
  149. static int mthca_modify_device(struct ib_device *ibdev,
  150. int mask,
  151. struct ib_device_modify *props)
  152. {
  153. if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
  154. return -EOPNOTSUPP;
  155. if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
  156. if (mutex_lock_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
  157. return -ERESTARTSYS;
  158. memcpy(ibdev->node_desc, props->node_desc,
  159. IB_DEVICE_NODE_DESC_MAX);
  160. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  161. }
  162. return 0;
  163. }
  164. static int mthca_modify_port(struct ib_device *ibdev,
  165. u32 port, int port_modify_mask,
  166. struct ib_port_modify *props)
  167. {
  168. struct mthca_set_ib_param set_ib;
  169. struct ib_port_attr attr;
  170. int err;
  171. if (mutex_lock_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
  172. return -ERESTARTSYS;
  173. err = ib_query_port(ibdev, port, &attr);
  174. if (err)
  175. goto out;
  176. set_ib.set_si_guid = 0;
  177. set_ib.reset_qkey_viol = !!(port_modify_mask & IB_PORT_RESET_QKEY_CNTR);
  178. set_ib.cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  179. ~props->clr_port_cap_mask;
  180. err = mthca_SET_IB(to_mdev(ibdev), &set_ib, port);
  181. if (err)
  182. goto out;
  183. out:
  184. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  185. return err;
  186. }
  187. static int mthca_query_pkey(struct ib_device *ibdev,
  188. u32 port, u16 index, u16 *pkey)
  189. {
  190. struct ib_smp *in_mad;
  191. struct ib_smp *out_mad;
  192. int err = -ENOMEM;
  193. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  194. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  195. if (!in_mad || !out_mad)
  196. goto out;
  197. ib_init_query_mad(in_mad);
  198. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  199. in_mad->attr_mod = cpu_to_be32(index / 32);
  200. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  201. port, NULL, NULL, in_mad, out_mad);
  202. if (err)
  203. goto out;
  204. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  205. out:
  206. kfree(in_mad);
  207. kfree(out_mad);
  208. return err;
  209. }
  210. static int mthca_query_gid(struct ib_device *ibdev, u32 port,
  211. int index, union ib_gid *gid)
  212. {
  213. struct ib_smp *in_mad;
  214. struct ib_smp *out_mad;
  215. int err = -ENOMEM;
  216. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  217. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  218. if (!in_mad || !out_mad)
  219. goto out;
  220. ib_init_query_mad(in_mad);
  221. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  222. in_mad->attr_mod = cpu_to_be32(port);
  223. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  224. port, NULL, NULL, in_mad, out_mad);
  225. if (err)
  226. goto out;
  227. memcpy(gid->raw, out_mad->data + 8, 8);
  228. ib_init_query_mad(in_mad);
  229. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  230. in_mad->attr_mod = cpu_to_be32(index / 8);
  231. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  232. port, NULL, NULL, in_mad, out_mad);
  233. if (err)
  234. goto out;
  235. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  236. out:
  237. kfree(in_mad);
  238. kfree(out_mad);
  239. return err;
  240. }
  241. static int mthca_alloc_ucontext(struct ib_ucontext *uctx,
  242. struct ib_udata *udata)
  243. {
  244. struct ib_device *ibdev = uctx->device;
  245. struct mthca_alloc_ucontext_resp uresp = {};
  246. struct mthca_ucontext *context = to_mucontext(uctx);
  247. int err;
  248. if (!(to_mdev(ibdev)->active))
  249. return -EAGAIN;
  250. uresp.qp_tab_size = to_mdev(ibdev)->limits.num_qps;
  251. if (mthca_is_memfree(to_mdev(ibdev)))
  252. uresp.uarc_size = to_mdev(ibdev)->uar_table.uarc_size;
  253. else
  254. uresp.uarc_size = 0;
  255. err = mthca_uar_alloc(to_mdev(ibdev), &context->uar);
  256. if (err)
  257. return err;
  258. context->db_tab = mthca_init_user_db_tab(to_mdev(ibdev));
  259. if (IS_ERR(context->db_tab)) {
  260. err = PTR_ERR(context->db_tab);
  261. mthca_uar_free(to_mdev(ibdev), &context->uar);
  262. return err;
  263. }
  264. if (ib_copy_to_udata(udata, &uresp, sizeof(uresp))) {
  265. mthca_cleanup_user_db_tab(to_mdev(ibdev), &context->uar, context->db_tab);
  266. mthca_uar_free(to_mdev(ibdev), &context->uar);
  267. return -EFAULT;
  268. }
  269. context->reg_mr_warned = 0;
  270. return 0;
  271. }
  272. static void mthca_dealloc_ucontext(struct ib_ucontext *context)
  273. {
  274. mthca_cleanup_user_db_tab(to_mdev(context->device), &to_mucontext(context)->uar,
  275. to_mucontext(context)->db_tab);
  276. mthca_uar_free(to_mdev(context->device), &to_mucontext(context)->uar);
  277. }
  278. static int mthca_mmap_uar(struct ib_ucontext *context,
  279. struct vm_area_struct *vma)
  280. {
  281. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  282. return -EINVAL;
  283. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  284. if (io_remap_pfn_range(vma, vma->vm_start,
  285. to_mucontext(context)->uar.pfn,
  286. PAGE_SIZE, vma->vm_page_prot))
  287. return -EAGAIN;
  288. return 0;
  289. }
  290. static int mthca_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
  291. {
  292. struct ib_device *ibdev = ibpd->device;
  293. struct mthca_pd *pd = to_mpd(ibpd);
  294. int err;
  295. err = mthca_pd_alloc(to_mdev(ibdev), !udata, pd);
  296. if (err)
  297. return err;
  298. if (udata) {
  299. if (ib_copy_to_udata(udata, &pd->pd_num, sizeof (__u32))) {
  300. mthca_pd_free(to_mdev(ibdev), pd);
  301. return -EFAULT;
  302. }
  303. }
  304. return 0;
  305. }
  306. static int mthca_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
  307. {
  308. mthca_pd_free(to_mdev(pd->device), to_mpd(pd));
  309. return 0;
  310. }
  311. static int mthca_ah_create(struct ib_ah *ibah,
  312. struct rdma_ah_init_attr *init_attr,
  313. struct ib_udata *udata)
  314. {
  315. struct mthca_ah *ah = to_mah(ibah);
  316. return mthca_create_ah(to_mdev(ibah->device), to_mpd(ibah->pd),
  317. init_attr->ah_attr, ah);
  318. }
  319. static int mthca_ah_destroy(struct ib_ah *ah, u32 flags)
  320. {
  321. mthca_destroy_ah(to_mdev(ah->device), to_mah(ah));
  322. return 0;
  323. }
  324. static int mthca_create_srq(struct ib_srq *ibsrq,
  325. struct ib_srq_init_attr *init_attr,
  326. struct ib_udata *udata)
  327. {
  328. struct mthca_create_srq ucmd;
  329. struct mthca_ucontext *context = rdma_udata_to_drv_context(
  330. udata, struct mthca_ucontext, ibucontext);
  331. struct mthca_srq *srq = to_msrq(ibsrq);
  332. int err;
  333. if (init_attr->srq_type != IB_SRQT_BASIC)
  334. return -EOPNOTSUPP;
  335. if (udata) {
  336. if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd)))
  337. return -EFAULT;
  338. err = mthca_map_user_db(to_mdev(ibsrq->device), &context->uar,
  339. context->db_tab, ucmd.db_index,
  340. ucmd.db_page);
  341. if (err)
  342. return err;
  343. srq->mr.ibmr.lkey = ucmd.lkey;
  344. srq->db_index = ucmd.db_index;
  345. }
  346. err = mthca_alloc_srq(to_mdev(ibsrq->device), to_mpd(ibsrq->pd),
  347. &init_attr->attr, srq, udata);
  348. if (err && udata)
  349. mthca_unmap_user_db(to_mdev(ibsrq->device), &context->uar,
  350. context->db_tab, ucmd.db_index);
  351. if (err)
  352. return err;
  353. if (context && ib_copy_to_udata(udata, &srq->srqn, sizeof(__u32))) {
  354. mthca_free_srq(to_mdev(ibsrq->device), srq);
  355. return -EFAULT;
  356. }
  357. return 0;
  358. }
  359. static int mthca_destroy_srq(struct ib_srq *srq, struct ib_udata *udata)
  360. {
  361. if (udata) {
  362. struct mthca_ucontext *context =
  363. rdma_udata_to_drv_context(
  364. udata,
  365. struct mthca_ucontext,
  366. ibucontext);
  367. mthca_unmap_user_db(to_mdev(srq->device), &context->uar,
  368. context->db_tab, to_msrq(srq)->db_index);
  369. }
  370. mthca_free_srq(to_mdev(srq->device), to_msrq(srq));
  371. return 0;
  372. }
  373. static int mthca_create_qp(struct ib_qp *ibqp,
  374. struct ib_qp_init_attr *init_attr,
  375. struct ib_udata *udata)
  376. {
  377. struct mthca_ucontext *context = rdma_udata_to_drv_context(
  378. udata, struct mthca_ucontext, ibucontext);
  379. struct mthca_create_qp ucmd;
  380. struct mthca_qp *qp = to_mqp(ibqp);
  381. struct mthca_dev *dev = to_mdev(ibqp->device);
  382. int err;
  383. if (init_attr->create_flags)
  384. return -EOPNOTSUPP;
  385. switch (init_attr->qp_type) {
  386. case IB_QPT_RC:
  387. case IB_QPT_UC:
  388. case IB_QPT_UD:
  389. {
  390. if (udata) {
  391. if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd)))
  392. return -EFAULT;
  393. err = mthca_map_user_db(dev, &context->uar,
  394. context->db_tab,
  395. ucmd.sq_db_index,
  396. ucmd.sq_db_page);
  397. if (err)
  398. return err;
  399. err = mthca_map_user_db(dev, &context->uar,
  400. context->db_tab,
  401. ucmd.rq_db_index,
  402. ucmd.rq_db_page);
  403. if (err) {
  404. mthca_unmap_user_db(dev, &context->uar,
  405. context->db_tab,
  406. ucmd.sq_db_index);
  407. return err;
  408. }
  409. qp->mr.ibmr.lkey = ucmd.lkey;
  410. qp->sq.db_index = ucmd.sq_db_index;
  411. qp->rq.db_index = ucmd.rq_db_index;
  412. }
  413. err = mthca_alloc_qp(dev, to_mpd(ibqp->pd),
  414. to_mcq(init_attr->send_cq),
  415. to_mcq(init_attr->recv_cq),
  416. init_attr->qp_type, init_attr->sq_sig_type,
  417. &init_attr->cap, qp, udata);
  418. if (err && udata) {
  419. mthca_unmap_user_db(dev, &context->uar, context->db_tab,
  420. ucmd.sq_db_index);
  421. mthca_unmap_user_db(dev, &context->uar, context->db_tab,
  422. ucmd.rq_db_index);
  423. }
  424. qp->ibqp.qp_num = qp->qpn;
  425. break;
  426. }
  427. case IB_QPT_SMI:
  428. case IB_QPT_GSI:
  429. {
  430. qp->sqp = kzalloc(sizeof(struct mthca_sqp), GFP_KERNEL);
  431. if (!qp->sqp)
  432. return -ENOMEM;
  433. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  434. err = mthca_alloc_sqp(dev, to_mpd(ibqp->pd),
  435. to_mcq(init_attr->send_cq),
  436. to_mcq(init_attr->recv_cq),
  437. init_attr->sq_sig_type, &init_attr->cap,
  438. qp->ibqp.qp_num, init_attr->port_num, qp,
  439. udata);
  440. break;
  441. }
  442. default:
  443. /* Don't support raw QPs */
  444. return -EOPNOTSUPP;
  445. }
  446. if (err) {
  447. kfree(qp->sqp);
  448. return err;
  449. }
  450. init_attr->cap.max_send_wr = qp->sq.max;
  451. init_attr->cap.max_recv_wr = qp->rq.max;
  452. init_attr->cap.max_send_sge = qp->sq.max_gs;
  453. init_attr->cap.max_recv_sge = qp->rq.max_gs;
  454. init_attr->cap.max_inline_data = qp->max_inline_data;
  455. return 0;
  456. }
  457. static int mthca_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
  458. {
  459. if (udata) {
  460. struct mthca_ucontext *context =
  461. rdma_udata_to_drv_context(
  462. udata,
  463. struct mthca_ucontext,
  464. ibucontext);
  465. mthca_unmap_user_db(to_mdev(qp->device),
  466. &context->uar,
  467. context->db_tab,
  468. to_mqp(qp)->sq.db_index);
  469. mthca_unmap_user_db(to_mdev(qp->device),
  470. &context->uar,
  471. context->db_tab,
  472. to_mqp(qp)->rq.db_index);
  473. }
  474. mthca_free_qp(to_mdev(qp->device), to_mqp(qp));
  475. kfree(to_mqp(qp)->sqp);
  476. return 0;
  477. }
  478. static int mthca_create_cq(struct ib_cq *ibcq,
  479. const struct ib_cq_init_attr *attr,
  480. struct uverbs_attr_bundle *attrs)
  481. {
  482. struct ib_udata *udata = &attrs->driver_udata;
  483. struct ib_device *ibdev = ibcq->device;
  484. int entries = attr->cqe;
  485. struct mthca_create_cq ucmd;
  486. struct mthca_cq *cq;
  487. int nent;
  488. int err;
  489. struct mthca_ucontext *context = rdma_udata_to_drv_context(
  490. udata, struct mthca_ucontext, ibucontext);
  491. if (attr->flags)
  492. return -EOPNOTSUPP;
  493. if (entries < 1 || entries > to_mdev(ibdev)->limits.max_cqes)
  494. return -EINVAL;
  495. if (udata) {
  496. if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd)))
  497. return -EFAULT;
  498. err = mthca_map_user_db(to_mdev(ibdev), &context->uar,
  499. context->db_tab, ucmd.set_db_index,
  500. ucmd.set_db_page);
  501. if (err)
  502. return err;
  503. err = mthca_map_user_db(to_mdev(ibdev), &context->uar,
  504. context->db_tab, ucmd.arm_db_index,
  505. ucmd.arm_db_page);
  506. if (err)
  507. goto err_unmap_set;
  508. }
  509. cq = to_mcq(ibcq);
  510. if (udata) {
  511. cq->buf.mr.ibmr.lkey = ucmd.lkey;
  512. cq->set_ci_db_index = ucmd.set_db_index;
  513. cq->arm_db_index = ucmd.arm_db_index;
  514. }
  515. for (nent = 1; nent <= entries; nent <<= 1)
  516. ; /* nothing */
  517. err = mthca_init_cq(to_mdev(ibdev), nent, context,
  518. udata ? ucmd.pdn : to_mdev(ibdev)->driver_pd.pd_num,
  519. cq);
  520. if (err)
  521. goto err_unmap_arm;
  522. if (udata && ib_copy_to_udata(udata, &cq->cqn, sizeof(__u32))) {
  523. mthca_free_cq(to_mdev(ibdev), cq);
  524. err = -EFAULT;
  525. goto err_unmap_arm;
  526. }
  527. cq->resize_buf = NULL;
  528. return 0;
  529. err_unmap_arm:
  530. if (udata)
  531. mthca_unmap_user_db(to_mdev(ibdev), &context->uar,
  532. context->db_tab, ucmd.arm_db_index);
  533. err_unmap_set:
  534. if (udata)
  535. mthca_unmap_user_db(to_mdev(ibdev), &context->uar,
  536. context->db_tab, ucmd.set_db_index);
  537. return err;
  538. }
  539. static int mthca_alloc_resize_buf(struct mthca_dev *dev, struct mthca_cq *cq,
  540. int entries)
  541. {
  542. int ret;
  543. spin_lock_irq(&cq->lock);
  544. if (cq->resize_buf) {
  545. ret = -EBUSY;
  546. goto unlock;
  547. }
  548. cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_ATOMIC);
  549. if (!cq->resize_buf) {
  550. ret = -ENOMEM;
  551. goto unlock;
  552. }
  553. cq->resize_buf->state = CQ_RESIZE_ALLOC;
  554. ret = 0;
  555. unlock:
  556. spin_unlock_irq(&cq->lock);
  557. if (ret)
  558. return ret;
  559. ret = mthca_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
  560. if (ret) {
  561. spin_lock_irq(&cq->lock);
  562. kfree(cq->resize_buf);
  563. cq->resize_buf = NULL;
  564. spin_unlock_irq(&cq->lock);
  565. return ret;
  566. }
  567. cq->resize_buf->cqe = entries - 1;
  568. spin_lock_irq(&cq->lock);
  569. cq->resize_buf->state = CQ_RESIZE_READY;
  570. spin_unlock_irq(&cq->lock);
  571. return 0;
  572. }
  573. static int mthca_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
  574. {
  575. struct mthca_dev *dev = to_mdev(ibcq->device);
  576. struct mthca_cq *cq = to_mcq(ibcq);
  577. struct mthca_resize_cq ucmd;
  578. u32 lkey;
  579. int ret;
  580. if (entries < 1 || entries > dev->limits.max_cqes)
  581. return -EINVAL;
  582. mutex_lock(&cq->mutex);
  583. entries = roundup_pow_of_two(entries + 1);
  584. if (entries == ibcq->cqe + 1) {
  585. ret = 0;
  586. goto out;
  587. }
  588. if (cq->is_kernel) {
  589. ret = mthca_alloc_resize_buf(dev, cq, entries);
  590. if (ret)
  591. goto out;
  592. lkey = cq->resize_buf->buf.mr.ibmr.lkey;
  593. } else {
  594. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  595. ret = -EFAULT;
  596. goto out;
  597. }
  598. lkey = ucmd.lkey;
  599. }
  600. ret = mthca_RESIZE_CQ(dev, cq->cqn, lkey, ilog2(entries));
  601. if (ret) {
  602. if (cq->resize_buf) {
  603. mthca_free_cq_buf(dev, &cq->resize_buf->buf,
  604. cq->resize_buf->cqe);
  605. kfree(cq->resize_buf);
  606. spin_lock_irq(&cq->lock);
  607. cq->resize_buf = NULL;
  608. spin_unlock_irq(&cq->lock);
  609. }
  610. goto out;
  611. }
  612. if (cq->is_kernel) {
  613. struct mthca_cq_buf tbuf;
  614. int tcqe;
  615. spin_lock_irq(&cq->lock);
  616. if (cq->resize_buf->state == CQ_RESIZE_READY) {
  617. mthca_cq_resize_copy_cqes(cq);
  618. tbuf = cq->buf;
  619. tcqe = cq->ibcq.cqe;
  620. cq->buf = cq->resize_buf->buf;
  621. cq->ibcq.cqe = cq->resize_buf->cqe;
  622. } else {
  623. tbuf = cq->resize_buf->buf;
  624. tcqe = cq->resize_buf->cqe;
  625. }
  626. kfree(cq->resize_buf);
  627. cq->resize_buf = NULL;
  628. spin_unlock_irq(&cq->lock);
  629. mthca_free_cq_buf(dev, &tbuf, tcqe);
  630. } else
  631. ibcq->cqe = entries - 1;
  632. out:
  633. mutex_unlock(&cq->mutex);
  634. return ret;
  635. }
  636. static int mthca_destroy_cq(struct ib_cq *cq, struct ib_udata *udata)
  637. {
  638. if (udata) {
  639. struct mthca_ucontext *context =
  640. rdma_udata_to_drv_context(
  641. udata,
  642. struct mthca_ucontext,
  643. ibucontext);
  644. mthca_unmap_user_db(to_mdev(cq->device),
  645. &context->uar,
  646. context->db_tab,
  647. to_mcq(cq)->arm_db_index);
  648. mthca_unmap_user_db(to_mdev(cq->device),
  649. &context->uar,
  650. context->db_tab,
  651. to_mcq(cq)->set_ci_db_index);
  652. }
  653. mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
  654. return 0;
  655. }
  656. static inline u32 convert_access(int acc)
  657. {
  658. return (acc & IB_ACCESS_REMOTE_ATOMIC ? MTHCA_MPT_FLAG_ATOMIC : 0) |
  659. (acc & IB_ACCESS_REMOTE_WRITE ? MTHCA_MPT_FLAG_REMOTE_WRITE : 0) |
  660. (acc & IB_ACCESS_REMOTE_READ ? MTHCA_MPT_FLAG_REMOTE_READ : 0) |
  661. (acc & IB_ACCESS_LOCAL_WRITE ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0) |
  662. MTHCA_MPT_FLAG_LOCAL_READ;
  663. }
  664. static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
  665. {
  666. struct mthca_mr *mr;
  667. int err;
  668. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  669. if (!mr)
  670. return ERR_PTR(-ENOMEM);
  671. err = mthca_mr_alloc_notrans(to_mdev(pd->device),
  672. to_mpd(pd)->pd_num,
  673. convert_access(acc), mr);
  674. if (err) {
  675. kfree(mr);
  676. return ERR_PTR(err);
  677. }
  678. mr->umem = NULL;
  679. return &mr->ibmr;
  680. }
  681. static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  682. u64 virt, int acc, struct ib_udata *udata)
  683. {
  684. struct mthca_dev *dev = to_mdev(pd->device);
  685. struct ib_block_iter biter;
  686. struct mthca_ucontext *context = rdma_udata_to_drv_context(
  687. udata, struct mthca_ucontext, ibucontext);
  688. struct mthca_mr *mr;
  689. struct mthca_reg_mr ucmd;
  690. u64 *pages;
  691. int n, i;
  692. int err = 0;
  693. int write_mtt_size;
  694. if (udata->inlen < sizeof ucmd) {
  695. if (!context->reg_mr_warned) {
  696. mthca_warn(dev, "Process '%s' did not pass in MR attrs.\n",
  697. current->comm);
  698. mthca_warn(dev, " Update libmthca to fix this.\n");
  699. }
  700. ++context->reg_mr_warned;
  701. ucmd.mr_attrs = 0;
  702. } else if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  703. return ERR_PTR(-EFAULT);
  704. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  705. if (!mr)
  706. return ERR_PTR(-ENOMEM);
  707. mr->umem = ib_umem_get(pd->device, start, length, acc);
  708. if (IS_ERR(mr->umem)) {
  709. err = PTR_ERR(mr->umem);
  710. goto err;
  711. }
  712. n = ib_umem_num_dma_blocks(mr->umem, PAGE_SIZE);
  713. mr->mtt = mthca_alloc_mtt(dev, n);
  714. if (IS_ERR(mr->mtt)) {
  715. err = PTR_ERR(mr->mtt);
  716. goto err_umem;
  717. }
  718. pages = (u64 *) __get_free_page(GFP_KERNEL);
  719. if (!pages) {
  720. err = -ENOMEM;
  721. goto err_mtt;
  722. }
  723. i = n = 0;
  724. write_mtt_size = min(mthca_write_mtt_size(dev), (int) (PAGE_SIZE / sizeof *pages));
  725. rdma_umem_for_each_dma_block(mr->umem, &biter, PAGE_SIZE) {
  726. pages[i++] = rdma_block_iter_dma_address(&biter);
  727. /*
  728. * Be friendly to write_mtt and pass it chunks
  729. * of appropriate size.
  730. */
  731. if (i == write_mtt_size) {
  732. err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
  733. if (err)
  734. goto mtt_done;
  735. n += i;
  736. i = 0;
  737. }
  738. }
  739. if (i)
  740. err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
  741. mtt_done:
  742. free_page((unsigned long) pages);
  743. if (err)
  744. goto err_mtt;
  745. err = mthca_mr_alloc(dev, to_mpd(pd)->pd_num, PAGE_SHIFT, virt, length,
  746. convert_access(acc), mr);
  747. if (err)
  748. goto err_mtt;
  749. return &mr->ibmr;
  750. err_mtt:
  751. mthca_free_mtt(dev, mr->mtt);
  752. err_umem:
  753. ib_umem_release(mr->umem);
  754. err:
  755. kfree(mr);
  756. return ERR_PTR(err);
  757. }
  758. static int mthca_dereg_mr(struct ib_mr *mr, struct ib_udata *udata)
  759. {
  760. struct mthca_mr *mmr = to_mmr(mr);
  761. mthca_free_mr(to_mdev(mr->device), mmr);
  762. ib_umem_release(mmr->umem);
  763. kfree(mmr);
  764. return 0;
  765. }
  766. static ssize_t hw_rev_show(struct device *device,
  767. struct device_attribute *attr, char *buf)
  768. {
  769. struct mthca_dev *dev =
  770. rdma_device_to_drv_device(device, struct mthca_dev, ib_dev);
  771. return sysfs_emit(buf, "%x\n", dev->rev_id);
  772. }
  773. static DEVICE_ATTR_RO(hw_rev);
  774. static const char *hca_type_string(int hca_type)
  775. {
  776. switch (hca_type) {
  777. case PCI_DEVICE_ID_MELLANOX_TAVOR:
  778. return "MT23108";
  779. case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
  780. return "MT25208 (MT23108 compat mode)";
  781. case PCI_DEVICE_ID_MELLANOX_ARBEL:
  782. return "MT25208";
  783. case PCI_DEVICE_ID_MELLANOX_SINAI:
  784. case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
  785. return "MT25204";
  786. }
  787. return "unknown";
  788. }
  789. static ssize_t hca_type_show(struct device *device,
  790. struct device_attribute *attr, char *buf)
  791. {
  792. struct mthca_dev *dev =
  793. rdma_device_to_drv_device(device, struct mthca_dev, ib_dev);
  794. return sysfs_emit(buf, "%s\n", hca_type_string(dev->pdev->device));
  795. }
  796. static DEVICE_ATTR_RO(hca_type);
  797. static ssize_t board_id_show(struct device *device,
  798. struct device_attribute *attr, char *buf)
  799. {
  800. struct mthca_dev *dev =
  801. rdma_device_to_drv_device(device, struct mthca_dev, ib_dev);
  802. return sysfs_emit(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
  803. }
  804. static DEVICE_ATTR_RO(board_id);
  805. static struct attribute *mthca_dev_attributes[] = {
  806. &dev_attr_hw_rev.attr,
  807. &dev_attr_hca_type.attr,
  808. &dev_attr_board_id.attr,
  809. NULL
  810. };
  811. static const struct attribute_group mthca_attr_group = {
  812. .attrs = mthca_dev_attributes,
  813. };
  814. static int mthca_init_node_data(struct mthca_dev *dev)
  815. {
  816. struct ib_smp *in_mad;
  817. struct ib_smp *out_mad;
  818. int err = -ENOMEM;
  819. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  820. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  821. if (!in_mad || !out_mad)
  822. goto out;
  823. ib_init_query_mad(in_mad);
  824. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  825. err = mthca_MAD_IFC(dev, 1, 1,
  826. 1, NULL, NULL, in_mad, out_mad);
  827. if (err)
  828. goto out;
  829. memcpy(dev->ib_dev.node_desc, out_mad->data, IB_DEVICE_NODE_DESC_MAX);
  830. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  831. err = mthca_MAD_IFC(dev, 1, 1,
  832. 1, NULL, NULL, in_mad, out_mad);
  833. if (err)
  834. goto out;
  835. if (mthca_is_memfree(dev))
  836. dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
  837. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  838. out:
  839. kfree(in_mad);
  840. kfree(out_mad);
  841. return err;
  842. }
  843. static int mthca_port_immutable(struct ib_device *ibdev, u32 port_num,
  844. struct ib_port_immutable *immutable)
  845. {
  846. struct ib_port_attr attr;
  847. int err;
  848. immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
  849. err = ib_query_port(ibdev, port_num, &attr);
  850. if (err)
  851. return err;
  852. immutable->pkey_tbl_len = attr.pkey_tbl_len;
  853. immutable->gid_tbl_len = attr.gid_tbl_len;
  854. immutable->max_mad_size = IB_MGMT_MAD_SIZE;
  855. return 0;
  856. }
  857. static void get_dev_fw_str(struct ib_device *device, char *str)
  858. {
  859. struct mthca_dev *dev =
  860. container_of(device, struct mthca_dev, ib_dev);
  861. snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%d",
  862. (int) (dev->fw_ver >> 32),
  863. (int) (dev->fw_ver >> 16) & 0xffff,
  864. (int) dev->fw_ver & 0xffff);
  865. }
  866. static const struct ib_device_ops mthca_dev_ops = {
  867. .owner = THIS_MODULE,
  868. .driver_id = RDMA_DRIVER_MTHCA,
  869. .uverbs_abi_ver = MTHCA_UVERBS_ABI_VERSION,
  870. .uverbs_no_driver_id_binding = 1,
  871. .alloc_pd = mthca_alloc_pd,
  872. .alloc_ucontext = mthca_alloc_ucontext,
  873. .attach_mcast = mthca_multicast_attach,
  874. .create_ah = mthca_ah_create,
  875. .create_cq = mthca_create_cq,
  876. .create_qp = mthca_create_qp,
  877. .dealloc_pd = mthca_dealloc_pd,
  878. .dealloc_ucontext = mthca_dealloc_ucontext,
  879. .dereg_mr = mthca_dereg_mr,
  880. .destroy_ah = mthca_ah_destroy,
  881. .destroy_cq = mthca_destroy_cq,
  882. .destroy_qp = mthca_destroy_qp,
  883. .detach_mcast = mthca_multicast_detach,
  884. .device_group = &mthca_attr_group,
  885. .get_dev_fw_str = get_dev_fw_str,
  886. .get_dma_mr = mthca_get_dma_mr,
  887. .get_port_immutable = mthca_port_immutable,
  888. .mmap = mthca_mmap_uar,
  889. .modify_device = mthca_modify_device,
  890. .modify_port = mthca_modify_port,
  891. .modify_qp = mthca_modify_qp,
  892. .poll_cq = mthca_poll_cq,
  893. .process_mad = mthca_process_mad,
  894. .query_ah = mthca_ah_query,
  895. .query_device = mthca_query_device,
  896. .query_gid = mthca_query_gid,
  897. .query_pkey = mthca_query_pkey,
  898. .query_port = mthca_query_port,
  899. .query_qp = mthca_query_qp,
  900. .reg_user_mr = mthca_reg_user_mr,
  901. .resize_cq = mthca_resize_cq,
  902. INIT_RDMA_OBJ_SIZE(ib_ah, mthca_ah, ibah),
  903. INIT_RDMA_OBJ_SIZE(ib_cq, mthca_cq, ibcq),
  904. INIT_RDMA_OBJ_SIZE(ib_pd, mthca_pd, ibpd),
  905. INIT_RDMA_OBJ_SIZE(ib_qp, mthca_qp, ibqp),
  906. INIT_RDMA_OBJ_SIZE(ib_ucontext, mthca_ucontext, ibucontext),
  907. };
  908. static const struct ib_device_ops mthca_dev_arbel_srq_ops = {
  909. .create_srq = mthca_create_srq,
  910. .destroy_srq = mthca_destroy_srq,
  911. .modify_srq = mthca_modify_srq,
  912. .post_srq_recv = mthca_arbel_post_srq_recv,
  913. .query_srq = mthca_query_srq,
  914. INIT_RDMA_OBJ_SIZE(ib_srq, mthca_srq, ibsrq),
  915. };
  916. static const struct ib_device_ops mthca_dev_tavor_srq_ops = {
  917. .create_srq = mthca_create_srq,
  918. .destroy_srq = mthca_destroy_srq,
  919. .modify_srq = mthca_modify_srq,
  920. .post_srq_recv = mthca_tavor_post_srq_recv,
  921. .query_srq = mthca_query_srq,
  922. INIT_RDMA_OBJ_SIZE(ib_srq, mthca_srq, ibsrq),
  923. };
  924. static const struct ib_device_ops mthca_dev_arbel_ops = {
  925. .post_recv = mthca_arbel_post_receive,
  926. .post_send = mthca_arbel_post_send,
  927. .req_notify_cq = mthca_arbel_arm_cq,
  928. };
  929. static const struct ib_device_ops mthca_dev_tavor_ops = {
  930. .post_recv = mthca_tavor_post_receive,
  931. .post_send = mthca_tavor_post_send,
  932. .req_notify_cq = mthca_tavor_arm_cq,
  933. };
  934. int mthca_register_device(struct mthca_dev *dev)
  935. {
  936. int ret;
  937. ret = mthca_init_node_data(dev);
  938. if (ret)
  939. return ret;
  940. dev->ib_dev.node_type = RDMA_NODE_IB_CA;
  941. dev->ib_dev.phys_port_cnt = dev->limits.num_ports;
  942. dev->ib_dev.num_comp_vectors = 1;
  943. dev->ib_dev.dev.parent = &dev->pdev->dev;
  944. if (dev->mthca_flags & MTHCA_FLAG_SRQ) {
  945. if (mthca_is_memfree(dev))
  946. ib_set_device_ops(&dev->ib_dev,
  947. &mthca_dev_arbel_srq_ops);
  948. else
  949. ib_set_device_ops(&dev->ib_dev,
  950. &mthca_dev_tavor_srq_ops);
  951. }
  952. ib_set_device_ops(&dev->ib_dev, &mthca_dev_ops);
  953. if (mthca_is_memfree(dev))
  954. ib_set_device_ops(&dev->ib_dev, &mthca_dev_arbel_ops);
  955. else
  956. ib_set_device_ops(&dev->ib_dev, &mthca_dev_tavor_ops);
  957. mutex_init(&dev->cap_mask_mutex);
  958. ret = ib_register_device(&dev->ib_dev, "mthca%d", &dev->pdev->dev);
  959. if (ret)
  960. return ret;
  961. mthca_start_catas_poll(dev);
  962. return 0;
  963. }
  964. void mthca_unregister_device(struct mthca_dev *dev)
  965. {
  966. mthca_stop_catas_poll(dev);
  967. ib_unregister_device(&dev->ib_dev);
  968. }