mthca_provider.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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 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. * $Id: mthca_provider.c 1397 2004-12-28 05:09:00Z roland $
  37. */
  38. #include <rdma/ib_smi.h>
  39. #include <linux/mm.h>
  40. #include "mthca_dev.h"
  41. #include "mthca_cmd.h"
  42. #include "mthca_user.h"
  43. #include "mthca_memfree.h"
  44. static int mthca_query_device(struct ib_device *ibdev,
  45. struct ib_device_attr *props)
  46. {
  47. struct ib_smp *in_mad = NULL;
  48. struct ib_smp *out_mad = NULL;
  49. int err = -ENOMEM;
  50. struct mthca_dev* mdev = to_mdev(ibdev);
  51. u8 status;
  52. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  53. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  54. if (!in_mad || !out_mad)
  55. goto out;
  56. memset(props, 0, sizeof *props);
  57. props->fw_ver = mdev->fw_ver;
  58. memset(in_mad, 0, sizeof *in_mad);
  59. in_mad->base_version = 1;
  60. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  61. in_mad->class_version = 1;
  62. in_mad->method = IB_MGMT_METHOD_GET;
  63. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  64. err = mthca_MAD_IFC(mdev, 1, 1,
  65. 1, NULL, NULL, in_mad, out_mad,
  66. &status);
  67. if (err)
  68. goto out;
  69. if (status) {
  70. err = -EINVAL;
  71. goto out;
  72. }
  73. props->device_cap_flags = mdev->device_cap_flags;
  74. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  75. 0xffffff;
  76. props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
  77. props->hw_ver = be16_to_cpup((__be16 *) (out_mad->data + 32));
  78. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  79. memcpy(&props->node_guid, out_mad->data + 12, 8);
  80. props->max_mr_size = ~0ull;
  81. props->max_qp = mdev->limits.num_qps - mdev->limits.reserved_qps;
  82. props->max_qp_wr = 0xffff;
  83. props->max_sge = mdev->limits.max_sg;
  84. props->max_cq = mdev->limits.num_cqs - mdev->limits.reserved_cqs;
  85. props->max_cqe = 0xffff;
  86. props->max_mr = mdev->limits.num_mpts - mdev->limits.reserved_mrws;
  87. props->max_pd = mdev->limits.num_pds - mdev->limits.reserved_pds;
  88. props->max_qp_rd_atom = 1 << mdev->qp_table.rdb_shift;
  89. props->max_qp_init_rd_atom = 1 << mdev->qp_table.rdb_shift;
  90. props->local_ca_ack_delay = mdev->limits.local_ca_ack_delay;
  91. err = 0;
  92. out:
  93. kfree(in_mad);
  94. kfree(out_mad);
  95. return err;
  96. }
  97. static int mthca_query_port(struct ib_device *ibdev,
  98. u8 port, struct ib_port_attr *props)
  99. {
  100. struct ib_smp *in_mad = NULL;
  101. struct ib_smp *out_mad = NULL;
  102. int err = -ENOMEM;
  103. u8 status;
  104. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  105. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  106. if (!in_mad || !out_mad)
  107. goto out;
  108. memset(props, 0, sizeof *props);
  109. memset(in_mad, 0, sizeof *in_mad);
  110. in_mad->base_version = 1;
  111. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  112. in_mad->class_version = 1;
  113. in_mad->method = IB_MGMT_METHOD_GET;
  114. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  115. in_mad->attr_mod = cpu_to_be32(port);
  116. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  117. port, NULL, NULL, in_mad, out_mad,
  118. &status);
  119. if (err)
  120. goto out;
  121. if (status) {
  122. err = -EINVAL;
  123. goto out;
  124. }
  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->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  136. props->active_width = out_mad->data[31] & 0xf;
  137. props->active_speed = out_mad->data[35] >> 4;
  138. out:
  139. kfree(in_mad);
  140. kfree(out_mad);
  141. return err;
  142. }
  143. static int mthca_modify_port(struct ib_device *ibdev,
  144. u8 port, int port_modify_mask,
  145. struct ib_port_modify *props)
  146. {
  147. struct mthca_set_ib_param set_ib;
  148. struct ib_port_attr attr;
  149. int err;
  150. u8 status;
  151. if (down_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
  152. return -ERESTARTSYS;
  153. err = mthca_query_port(ibdev, port, &attr);
  154. if (err)
  155. goto out;
  156. set_ib.set_si_guid = 0;
  157. set_ib.reset_qkey_viol = !!(port_modify_mask & IB_PORT_RESET_QKEY_CNTR);
  158. set_ib.cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  159. ~props->clr_port_cap_mask;
  160. err = mthca_SET_IB(to_mdev(ibdev), &set_ib, port, &status);
  161. if (err)
  162. goto out;
  163. if (status) {
  164. err = -EINVAL;
  165. goto out;
  166. }
  167. out:
  168. up(&to_mdev(ibdev)->cap_mask_mutex);
  169. return err;
  170. }
  171. static int mthca_query_pkey(struct ib_device *ibdev,
  172. u8 port, u16 index, u16 *pkey)
  173. {
  174. struct ib_smp *in_mad = NULL;
  175. struct ib_smp *out_mad = NULL;
  176. int err = -ENOMEM;
  177. u8 status;
  178. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  179. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  180. if (!in_mad || !out_mad)
  181. goto out;
  182. memset(in_mad, 0, sizeof *in_mad);
  183. in_mad->base_version = 1;
  184. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  185. in_mad->class_version = 1;
  186. in_mad->method = IB_MGMT_METHOD_GET;
  187. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  188. in_mad->attr_mod = cpu_to_be32(index / 32);
  189. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  190. port, NULL, NULL, in_mad, out_mad,
  191. &status);
  192. if (err)
  193. goto out;
  194. if (status) {
  195. err = -EINVAL;
  196. goto out;
  197. }
  198. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  199. out:
  200. kfree(in_mad);
  201. kfree(out_mad);
  202. return err;
  203. }
  204. static int mthca_query_gid(struct ib_device *ibdev, u8 port,
  205. int index, union ib_gid *gid)
  206. {
  207. struct ib_smp *in_mad = NULL;
  208. struct ib_smp *out_mad = NULL;
  209. int err = -ENOMEM;
  210. u8 status;
  211. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  212. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  213. if (!in_mad || !out_mad)
  214. goto out;
  215. memset(in_mad, 0, sizeof *in_mad);
  216. in_mad->base_version = 1;
  217. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  218. in_mad->class_version = 1;
  219. in_mad->method = IB_MGMT_METHOD_GET;
  220. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  221. in_mad->attr_mod = cpu_to_be32(port);
  222. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  223. port, NULL, NULL, in_mad, out_mad,
  224. &status);
  225. if (err)
  226. goto out;
  227. if (status) {
  228. err = -EINVAL;
  229. goto out;
  230. }
  231. memcpy(gid->raw, out_mad->data + 8, 8);
  232. memset(in_mad, 0, sizeof *in_mad);
  233. in_mad->base_version = 1;
  234. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  235. in_mad->class_version = 1;
  236. in_mad->method = IB_MGMT_METHOD_GET;
  237. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  238. in_mad->attr_mod = cpu_to_be32(index / 8);
  239. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  240. port, NULL, NULL, in_mad, out_mad,
  241. &status);
  242. if (err)
  243. goto out;
  244. if (status) {
  245. err = -EINVAL;
  246. goto out;
  247. }
  248. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 16, 8);
  249. out:
  250. kfree(in_mad);
  251. kfree(out_mad);
  252. return err;
  253. }
  254. static struct ib_ucontext *mthca_alloc_ucontext(struct ib_device *ibdev,
  255. struct ib_udata *udata)
  256. {
  257. struct mthca_alloc_ucontext_resp uresp;
  258. struct mthca_ucontext *context;
  259. int err;
  260. memset(&uresp, 0, sizeof uresp);
  261. uresp.qp_tab_size = to_mdev(ibdev)->limits.num_qps;
  262. if (mthca_is_memfree(to_mdev(ibdev)))
  263. uresp.uarc_size = to_mdev(ibdev)->uar_table.uarc_size;
  264. else
  265. uresp.uarc_size = 0;
  266. context = kmalloc(sizeof *context, GFP_KERNEL);
  267. if (!context)
  268. return ERR_PTR(-ENOMEM);
  269. err = mthca_uar_alloc(to_mdev(ibdev), &context->uar);
  270. if (err) {
  271. kfree(context);
  272. return ERR_PTR(err);
  273. }
  274. context->db_tab = mthca_init_user_db_tab(to_mdev(ibdev));
  275. if (IS_ERR(context->db_tab)) {
  276. err = PTR_ERR(context->db_tab);
  277. mthca_uar_free(to_mdev(ibdev), &context->uar);
  278. kfree(context);
  279. return ERR_PTR(err);
  280. }
  281. if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
  282. mthca_cleanup_user_db_tab(to_mdev(ibdev), &context->uar, context->db_tab);
  283. mthca_uar_free(to_mdev(ibdev), &context->uar);
  284. kfree(context);
  285. return ERR_PTR(-EFAULT);
  286. }
  287. return &context->ibucontext;
  288. }
  289. static int mthca_dealloc_ucontext(struct ib_ucontext *context)
  290. {
  291. mthca_cleanup_user_db_tab(to_mdev(context->device), &to_mucontext(context)->uar,
  292. to_mucontext(context)->db_tab);
  293. mthca_uar_free(to_mdev(context->device), &to_mucontext(context)->uar);
  294. kfree(to_mucontext(context));
  295. return 0;
  296. }
  297. static int mthca_mmap_uar(struct ib_ucontext *context,
  298. struct vm_area_struct *vma)
  299. {
  300. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  301. return -EINVAL;
  302. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  303. if (io_remap_pfn_range(vma, vma->vm_start,
  304. to_mucontext(context)->uar.pfn,
  305. PAGE_SIZE, vma->vm_page_prot))
  306. return -EAGAIN;
  307. return 0;
  308. }
  309. static struct ib_pd *mthca_alloc_pd(struct ib_device *ibdev,
  310. struct ib_ucontext *context,
  311. struct ib_udata *udata)
  312. {
  313. struct mthca_pd *pd;
  314. int err;
  315. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  316. if (!pd)
  317. return ERR_PTR(-ENOMEM);
  318. err = mthca_pd_alloc(to_mdev(ibdev), !context, pd);
  319. if (err) {
  320. kfree(pd);
  321. return ERR_PTR(err);
  322. }
  323. if (context) {
  324. if (ib_copy_to_udata(udata, &pd->pd_num, sizeof (__u32))) {
  325. mthca_pd_free(to_mdev(ibdev), pd);
  326. kfree(pd);
  327. return ERR_PTR(-EFAULT);
  328. }
  329. }
  330. return &pd->ibpd;
  331. }
  332. static int mthca_dealloc_pd(struct ib_pd *pd)
  333. {
  334. mthca_pd_free(to_mdev(pd->device), to_mpd(pd));
  335. kfree(pd);
  336. return 0;
  337. }
  338. static struct ib_ah *mthca_ah_create(struct ib_pd *pd,
  339. struct ib_ah_attr *ah_attr)
  340. {
  341. int err;
  342. struct mthca_ah *ah;
  343. ah = kmalloc(sizeof *ah, GFP_ATOMIC);
  344. if (!ah)
  345. return ERR_PTR(-ENOMEM);
  346. err = mthca_create_ah(to_mdev(pd->device), to_mpd(pd), ah_attr, ah);
  347. if (err) {
  348. kfree(ah);
  349. return ERR_PTR(err);
  350. }
  351. return &ah->ibah;
  352. }
  353. static int mthca_ah_destroy(struct ib_ah *ah)
  354. {
  355. mthca_destroy_ah(to_mdev(ah->device), to_mah(ah));
  356. kfree(ah);
  357. return 0;
  358. }
  359. static struct ib_srq *mthca_create_srq(struct ib_pd *pd,
  360. struct ib_srq_init_attr *init_attr,
  361. struct ib_udata *udata)
  362. {
  363. struct mthca_create_srq ucmd;
  364. struct mthca_ucontext *context = NULL;
  365. struct mthca_srq *srq;
  366. int err;
  367. srq = kmalloc(sizeof *srq, GFP_KERNEL);
  368. if (!srq)
  369. return ERR_PTR(-ENOMEM);
  370. if (pd->uobject) {
  371. context = to_mucontext(pd->uobject->context);
  372. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  373. return ERR_PTR(-EFAULT);
  374. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  375. context->db_tab, ucmd.db_index,
  376. ucmd.db_page);
  377. if (err)
  378. goto err_free;
  379. srq->mr.ibmr.lkey = ucmd.lkey;
  380. srq->db_index = ucmd.db_index;
  381. }
  382. err = mthca_alloc_srq(to_mdev(pd->device), to_mpd(pd),
  383. &init_attr->attr, srq);
  384. if (err && pd->uobject)
  385. mthca_unmap_user_db(to_mdev(pd->device), &context->uar,
  386. context->db_tab, ucmd.db_index);
  387. if (err)
  388. goto err_free;
  389. if (context && ib_copy_to_udata(udata, &srq->srqn, sizeof (__u32))) {
  390. mthca_free_srq(to_mdev(pd->device), srq);
  391. err = -EFAULT;
  392. goto err_free;
  393. }
  394. return &srq->ibsrq;
  395. err_free:
  396. kfree(srq);
  397. return ERR_PTR(err);
  398. }
  399. static int mthca_destroy_srq(struct ib_srq *srq)
  400. {
  401. struct mthca_ucontext *context;
  402. if (srq->uobject) {
  403. context = to_mucontext(srq->uobject->context);
  404. mthca_unmap_user_db(to_mdev(srq->device), &context->uar,
  405. context->db_tab, to_msrq(srq)->db_index);
  406. }
  407. mthca_free_srq(to_mdev(srq->device), to_msrq(srq));
  408. kfree(srq);
  409. return 0;
  410. }
  411. static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
  412. struct ib_qp_init_attr *init_attr,
  413. struct ib_udata *udata)
  414. {
  415. struct mthca_create_qp ucmd;
  416. struct mthca_qp *qp;
  417. int err;
  418. switch (init_attr->qp_type) {
  419. case IB_QPT_RC:
  420. case IB_QPT_UC:
  421. case IB_QPT_UD:
  422. {
  423. struct mthca_ucontext *context;
  424. qp = kmalloc(sizeof *qp, GFP_KERNEL);
  425. if (!qp)
  426. return ERR_PTR(-ENOMEM);
  427. if (pd->uobject) {
  428. context = to_mucontext(pd->uobject->context);
  429. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  430. return ERR_PTR(-EFAULT);
  431. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  432. context->db_tab,
  433. ucmd.sq_db_index, ucmd.sq_db_page);
  434. if (err) {
  435. kfree(qp);
  436. return ERR_PTR(err);
  437. }
  438. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  439. context->db_tab,
  440. ucmd.rq_db_index, ucmd.rq_db_page);
  441. if (err) {
  442. mthca_unmap_user_db(to_mdev(pd->device),
  443. &context->uar,
  444. context->db_tab,
  445. ucmd.sq_db_index);
  446. kfree(qp);
  447. return ERR_PTR(err);
  448. }
  449. qp->mr.ibmr.lkey = ucmd.lkey;
  450. qp->sq.db_index = ucmd.sq_db_index;
  451. qp->rq.db_index = ucmd.rq_db_index;
  452. }
  453. err = mthca_alloc_qp(to_mdev(pd->device), to_mpd(pd),
  454. to_mcq(init_attr->send_cq),
  455. to_mcq(init_attr->recv_cq),
  456. init_attr->qp_type, init_attr->sq_sig_type,
  457. &init_attr->cap, qp);
  458. if (err && pd->uobject) {
  459. context = to_mucontext(pd->uobject->context);
  460. mthca_unmap_user_db(to_mdev(pd->device),
  461. &context->uar,
  462. context->db_tab,
  463. ucmd.sq_db_index);
  464. mthca_unmap_user_db(to_mdev(pd->device),
  465. &context->uar,
  466. context->db_tab,
  467. ucmd.rq_db_index);
  468. }
  469. qp->ibqp.qp_num = qp->qpn;
  470. break;
  471. }
  472. case IB_QPT_SMI:
  473. case IB_QPT_GSI:
  474. {
  475. /* Don't allow userspace to create special QPs */
  476. if (pd->uobject)
  477. return ERR_PTR(-EINVAL);
  478. qp = kmalloc(sizeof (struct mthca_sqp), GFP_KERNEL);
  479. if (!qp)
  480. return ERR_PTR(-ENOMEM);
  481. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  482. err = mthca_alloc_sqp(to_mdev(pd->device), to_mpd(pd),
  483. to_mcq(init_attr->send_cq),
  484. to_mcq(init_attr->recv_cq),
  485. init_attr->sq_sig_type, &init_attr->cap,
  486. qp->ibqp.qp_num, init_attr->port_num,
  487. to_msqp(qp));
  488. break;
  489. }
  490. default:
  491. /* Don't support raw QPs */
  492. return ERR_PTR(-ENOSYS);
  493. }
  494. if (err) {
  495. kfree(qp);
  496. return ERR_PTR(err);
  497. }
  498. init_attr->cap.max_inline_data = 0;
  499. init_attr->cap.max_send_wr = qp->sq.max;
  500. init_attr->cap.max_recv_wr = qp->rq.max;
  501. init_attr->cap.max_send_sge = qp->sq.max_gs;
  502. init_attr->cap.max_recv_sge = qp->rq.max_gs;
  503. return &qp->ibqp;
  504. }
  505. static int mthca_destroy_qp(struct ib_qp *qp)
  506. {
  507. if (qp->uobject) {
  508. mthca_unmap_user_db(to_mdev(qp->device),
  509. &to_mucontext(qp->uobject->context)->uar,
  510. to_mucontext(qp->uobject->context)->db_tab,
  511. to_mqp(qp)->sq.db_index);
  512. mthca_unmap_user_db(to_mdev(qp->device),
  513. &to_mucontext(qp->uobject->context)->uar,
  514. to_mucontext(qp->uobject->context)->db_tab,
  515. to_mqp(qp)->rq.db_index);
  516. }
  517. mthca_free_qp(to_mdev(qp->device), to_mqp(qp));
  518. kfree(qp);
  519. return 0;
  520. }
  521. static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
  522. struct ib_ucontext *context,
  523. struct ib_udata *udata)
  524. {
  525. struct mthca_create_cq ucmd;
  526. struct mthca_cq *cq;
  527. int nent;
  528. int err;
  529. if (context) {
  530. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  531. return ERR_PTR(-EFAULT);
  532. err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  533. to_mucontext(context)->db_tab,
  534. ucmd.set_db_index, ucmd.set_db_page);
  535. if (err)
  536. return ERR_PTR(err);
  537. err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  538. to_mucontext(context)->db_tab,
  539. ucmd.arm_db_index, ucmd.arm_db_page);
  540. if (err)
  541. goto err_unmap_set;
  542. }
  543. cq = kmalloc(sizeof *cq, GFP_KERNEL);
  544. if (!cq) {
  545. err = -ENOMEM;
  546. goto err_unmap_arm;
  547. }
  548. if (context) {
  549. cq->mr.ibmr.lkey = ucmd.lkey;
  550. cq->set_ci_db_index = ucmd.set_db_index;
  551. cq->arm_db_index = ucmd.arm_db_index;
  552. }
  553. for (nent = 1; nent <= entries; nent <<= 1)
  554. ; /* nothing */
  555. err = mthca_init_cq(to_mdev(ibdev), nent,
  556. context ? to_mucontext(context) : NULL,
  557. context ? ucmd.pdn : to_mdev(ibdev)->driver_pd.pd_num,
  558. cq);
  559. if (err)
  560. goto err_free;
  561. if (context && ib_copy_to_udata(udata, &cq->cqn, sizeof (__u32))) {
  562. mthca_free_cq(to_mdev(ibdev), cq);
  563. goto err_free;
  564. }
  565. return &cq->ibcq;
  566. err_free:
  567. kfree(cq);
  568. err_unmap_arm:
  569. if (context)
  570. mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  571. to_mucontext(context)->db_tab, ucmd.arm_db_index);
  572. err_unmap_set:
  573. if (context)
  574. mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  575. to_mucontext(context)->db_tab, ucmd.set_db_index);
  576. return ERR_PTR(err);
  577. }
  578. static int mthca_destroy_cq(struct ib_cq *cq)
  579. {
  580. if (cq->uobject) {
  581. mthca_unmap_user_db(to_mdev(cq->device),
  582. &to_mucontext(cq->uobject->context)->uar,
  583. to_mucontext(cq->uobject->context)->db_tab,
  584. to_mcq(cq)->arm_db_index);
  585. mthca_unmap_user_db(to_mdev(cq->device),
  586. &to_mucontext(cq->uobject->context)->uar,
  587. to_mucontext(cq->uobject->context)->db_tab,
  588. to_mcq(cq)->set_ci_db_index);
  589. }
  590. mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
  591. kfree(cq);
  592. return 0;
  593. }
  594. static inline u32 convert_access(int acc)
  595. {
  596. return (acc & IB_ACCESS_REMOTE_ATOMIC ? MTHCA_MPT_FLAG_ATOMIC : 0) |
  597. (acc & IB_ACCESS_REMOTE_WRITE ? MTHCA_MPT_FLAG_REMOTE_WRITE : 0) |
  598. (acc & IB_ACCESS_REMOTE_READ ? MTHCA_MPT_FLAG_REMOTE_READ : 0) |
  599. (acc & IB_ACCESS_LOCAL_WRITE ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0) |
  600. MTHCA_MPT_FLAG_LOCAL_READ;
  601. }
  602. static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
  603. {
  604. struct mthca_mr *mr;
  605. int err;
  606. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  607. if (!mr)
  608. return ERR_PTR(-ENOMEM);
  609. err = mthca_mr_alloc_notrans(to_mdev(pd->device),
  610. to_mpd(pd)->pd_num,
  611. convert_access(acc), mr);
  612. if (err) {
  613. kfree(mr);
  614. return ERR_PTR(err);
  615. }
  616. return &mr->ibmr;
  617. }
  618. static struct ib_mr *mthca_reg_phys_mr(struct ib_pd *pd,
  619. struct ib_phys_buf *buffer_list,
  620. int num_phys_buf,
  621. int acc,
  622. u64 *iova_start)
  623. {
  624. struct mthca_mr *mr;
  625. u64 *page_list;
  626. u64 total_size;
  627. u64 mask;
  628. int shift;
  629. int npages;
  630. int err;
  631. int i, j, n;
  632. /* First check that we have enough alignment */
  633. if ((*iova_start & ~PAGE_MASK) != (buffer_list[0].addr & ~PAGE_MASK))
  634. return ERR_PTR(-EINVAL);
  635. if (num_phys_buf > 1 &&
  636. ((buffer_list[0].addr + buffer_list[0].size) & ~PAGE_MASK))
  637. return ERR_PTR(-EINVAL);
  638. mask = 0;
  639. total_size = 0;
  640. for (i = 0; i < num_phys_buf; ++i) {
  641. if (i != 0 && buffer_list[i].addr & ~PAGE_MASK)
  642. return ERR_PTR(-EINVAL);
  643. if (i != 0 && i != num_phys_buf - 1 &&
  644. (buffer_list[i].size & ~PAGE_MASK))
  645. return ERR_PTR(-EINVAL);
  646. total_size += buffer_list[i].size;
  647. if (i > 0)
  648. mask |= buffer_list[i].addr;
  649. }
  650. /* Find largest page shift we can use to cover buffers */
  651. for (shift = PAGE_SHIFT; shift < 31; ++shift)
  652. if (num_phys_buf > 1) {
  653. if ((1ULL << shift) & mask)
  654. break;
  655. } else {
  656. if (1ULL << shift >=
  657. buffer_list[0].size +
  658. (buffer_list[0].addr & ((1ULL << shift) - 1)))
  659. break;
  660. }
  661. buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
  662. buffer_list[0].addr &= ~0ull << shift;
  663. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  664. if (!mr)
  665. return ERR_PTR(-ENOMEM);
  666. npages = 0;
  667. for (i = 0; i < num_phys_buf; ++i)
  668. npages += (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  669. if (!npages)
  670. return &mr->ibmr;
  671. page_list = kmalloc(npages * sizeof *page_list, GFP_KERNEL);
  672. if (!page_list) {
  673. kfree(mr);
  674. return ERR_PTR(-ENOMEM);
  675. }
  676. n = 0;
  677. for (i = 0; i < num_phys_buf; ++i)
  678. for (j = 0;
  679. j < (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  680. ++j)
  681. page_list[n++] = buffer_list[i].addr + ((u64) j << shift);
  682. mthca_dbg(to_mdev(pd->device), "Registering memory at %llx (iova %llx) "
  683. "in PD %x; shift %d, npages %d.\n",
  684. (unsigned long long) buffer_list[0].addr,
  685. (unsigned long long) *iova_start,
  686. to_mpd(pd)->pd_num,
  687. shift, npages);
  688. err = mthca_mr_alloc_phys(to_mdev(pd->device),
  689. to_mpd(pd)->pd_num,
  690. page_list, shift, npages,
  691. *iova_start, total_size,
  692. convert_access(acc), mr);
  693. if (err) {
  694. kfree(page_list);
  695. kfree(mr);
  696. return ERR_PTR(err);
  697. }
  698. kfree(page_list);
  699. return &mr->ibmr;
  700. }
  701. static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
  702. int acc, struct ib_udata *udata)
  703. {
  704. struct mthca_dev *dev = to_mdev(pd->device);
  705. struct ib_umem_chunk *chunk;
  706. struct mthca_mr *mr;
  707. u64 *pages;
  708. int shift, n, len;
  709. int i, j, k;
  710. int err = 0;
  711. shift = ffs(region->page_size) - 1;
  712. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  713. if (!mr)
  714. return ERR_PTR(-ENOMEM);
  715. n = 0;
  716. list_for_each_entry(chunk, &region->chunk_list, list)
  717. n += chunk->nents;
  718. mr->mtt = mthca_alloc_mtt(dev, n);
  719. if (IS_ERR(mr->mtt)) {
  720. err = PTR_ERR(mr->mtt);
  721. goto err;
  722. }
  723. pages = (u64 *) __get_free_page(GFP_KERNEL);
  724. if (!pages) {
  725. err = -ENOMEM;
  726. goto err_mtt;
  727. }
  728. i = n = 0;
  729. list_for_each_entry(chunk, &region->chunk_list, list)
  730. for (j = 0; j < chunk->nmap; ++j) {
  731. len = sg_dma_len(&chunk->page_list[j]) >> shift;
  732. for (k = 0; k < len; ++k) {
  733. pages[i++] = sg_dma_address(&chunk->page_list[j]) +
  734. region->page_size * k;
  735. /*
  736. * Be friendly to WRITE_MTT command
  737. * and leave two empty slots for the
  738. * index and reserved fields of the
  739. * mailbox.
  740. */
  741. if (i == PAGE_SIZE / sizeof (u64) - 2) {
  742. err = mthca_write_mtt(dev, mr->mtt,
  743. n, pages, i);
  744. if (err)
  745. goto mtt_done;
  746. n += i;
  747. i = 0;
  748. }
  749. }
  750. }
  751. if (i)
  752. err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
  753. mtt_done:
  754. free_page((unsigned long) pages);
  755. if (err)
  756. goto err_mtt;
  757. err = mthca_mr_alloc(dev, to_mpd(pd)->pd_num, shift, region->virt_base,
  758. region->length, convert_access(acc), mr);
  759. if (err)
  760. goto err_mtt;
  761. return &mr->ibmr;
  762. err_mtt:
  763. mthca_free_mtt(dev, mr->mtt);
  764. err:
  765. kfree(mr);
  766. return ERR_PTR(err);
  767. }
  768. static int mthca_dereg_mr(struct ib_mr *mr)
  769. {
  770. struct mthca_mr *mmr = to_mmr(mr);
  771. mthca_free_mr(to_mdev(mr->device), mmr);
  772. kfree(mmr);
  773. return 0;
  774. }
  775. static struct ib_fmr *mthca_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
  776. struct ib_fmr_attr *fmr_attr)
  777. {
  778. struct mthca_fmr *fmr;
  779. int err;
  780. fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
  781. if (!fmr)
  782. return ERR_PTR(-ENOMEM);
  783. memcpy(&fmr->attr, fmr_attr, sizeof *fmr_attr);
  784. err = mthca_fmr_alloc(to_mdev(pd->device), to_mpd(pd)->pd_num,
  785. convert_access(mr_access_flags), fmr);
  786. if (err) {
  787. kfree(fmr);
  788. return ERR_PTR(err);
  789. }
  790. return &fmr->ibmr;
  791. }
  792. static int mthca_dealloc_fmr(struct ib_fmr *fmr)
  793. {
  794. struct mthca_fmr *mfmr = to_mfmr(fmr);
  795. int err;
  796. err = mthca_free_fmr(to_mdev(fmr->device), mfmr);
  797. if (err)
  798. return err;
  799. kfree(mfmr);
  800. return 0;
  801. }
  802. static int mthca_unmap_fmr(struct list_head *fmr_list)
  803. {
  804. struct ib_fmr *fmr;
  805. int err;
  806. u8 status;
  807. struct mthca_dev *mdev = NULL;
  808. list_for_each_entry(fmr, fmr_list, list) {
  809. if (mdev && to_mdev(fmr->device) != mdev)
  810. return -EINVAL;
  811. mdev = to_mdev(fmr->device);
  812. }
  813. if (!mdev)
  814. return 0;
  815. if (mthca_is_memfree(mdev)) {
  816. list_for_each_entry(fmr, fmr_list, list)
  817. mthca_arbel_fmr_unmap(mdev, to_mfmr(fmr));
  818. wmb();
  819. } else
  820. list_for_each_entry(fmr, fmr_list, list)
  821. mthca_tavor_fmr_unmap(mdev, to_mfmr(fmr));
  822. err = mthca_SYNC_TPT(mdev, &status);
  823. if (err)
  824. return err;
  825. if (status)
  826. return -EINVAL;
  827. return 0;
  828. }
  829. static ssize_t show_rev(struct class_device *cdev, char *buf)
  830. {
  831. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  832. return sprintf(buf, "%x\n", dev->rev_id);
  833. }
  834. static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
  835. {
  836. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  837. return sprintf(buf, "%x.%x.%x\n", (int) (dev->fw_ver >> 32),
  838. (int) (dev->fw_ver >> 16) & 0xffff,
  839. (int) dev->fw_ver & 0xffff);
  840. }
  841. static ssize_t show_hca(struct class_device *cdev, char *buf)
  842. {
  843. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  844. switch (dev->pdev->device) {
  845. case PCI_DEVICE_ID_MELLANOX_TAVOR:
  846. return sprintf(buf, "MT23108\n");
  847. case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
  848. return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
  849. case PCI_DEVICE_ID_MELLANOX_ARBEL:
  850. return sprintf(buf, "MT25208\n");
  851. case PCI_DEVICE_ID_MELLANOX_SINAI:
  852. case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
  853. return sprintf(buf, "MT25204\n");
  854. default:
  855. return sprintf(buf, "unknown\n");
  856. }
  857. }
  858. static ssize_t show_board(struct class_device *cdev, char *buf)
  859. {
  860. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  861. return sprintf(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
  862. }
  863. static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  864. static CLASS_DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  865. static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  866. static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  867. static struct class_device_attribute *mthca_class_attributes[] = {
  868. &class_device_attr_hw_rev,
  869. &class_device_attr_fw_ver,
  870. &class_device_attr_hca_type,
  871. &class_device_attr_board_id
  872. };
  873. int mthca_register_device(struct mthca_dev *dev)
  874. {
  875. int ret;
  876. int i;
  877. strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
  878. dev->ib_dev.owner = THIS_MODULE;
  879. dev->ib_dev.node_type = IB_NODE_CA;
  880. dev->ib_dev.phys_port_cnt = dev->limits.num_ports;
  881. dev->ib_dev.dma_device = &dev->pdev->dev;
  882. dev->ib_dev.class_dev.dev = &dev->pdev->dev;
  883. dev->ib_dev.query_device = mthca_query_device;
  884. dev->ib_dev.query_port = mthca_query_port;
  885. dev->ib_dev.modify_port = mthca_modify_port;
  886. dev->ib_dev.query_pkey = mthca_query_pkey;
  887. dev->ib_dev.query_gid = mthca_query_gid;
  888. dev->ib_dev.alloc_ucontext = mthca_alloc_ucontext;
  889. dev->ib_dev.dealloc_ucontext = mthca_dealloc_ucontext;
  890. dev->ib_dev.mmap = mthca_mmap_uar;
  891. dev->ib_dev.alloc_pd = mthca_alloc_pd;
  892. dev->ib_dev.dealloc_pd = mthca_dealloc_pd;
  893. dev->ib_dev.create_ah = mthca_ah_create;
  894. dev->ib_dev.destroy_ah = mthca_ah_destroy;
  895. if (dev->mthca_flags & MTHCA_FLAG_SRQ) {
  896. dev->ib_dev.create_srq = mthca_create_srq;
  897. dev->ib_dev.destroy_srq = mthca_destroy_srq;
  898. if (mthca_is_memfree(dev))
  899. dev->ib_dev.post_srq_recv = mthca_arbel_post_srq_recv;
  900. else
  901. dev->ib_dev.post_srq_recv = mthca_tavor_post_srq_recv;
  902. }
  903. dev->ib_dev.create_qp = mthca_create_qp;
  904. dev->ib_dev.modify_qp = mthca_modify_qp;
  905. dev->ib_dev.destroy_qp = mthca_destroy_qp;
  906. dev->ib_dev.create_cq = mthca_create_cq;
  907. dev->ib_dev.destroy_cq = mthca_destroy_cq;
  908. dev->ib_dev.poll_cq = mthca_poll_cq;
  909. dev->ib_dev.get_dma_mr = mthca_get_dma_mr;
  910. dev->ib_dev.reg_phys_mr = mthca_reg_phys_mr;
  911. dev->ib_dev.reg_user_mr = mthca_reg_user_mr;
  912. dev->ib_dev.dereg_mr = mthca_dereg_mr;
  913. if (dev->mthca_flags & MTHCA_FLAG_FMR) {
  914. dev->ib_dev.alloc_fmr = mthca_alloc_fmr;
  915. dev->ib_dev.unmap_fmr = mthca_unmap_fmr;
  916. dev->ib_dev.dealloc_fmr = mthca_dealloc_fmr;
  917. if (mthca_is_memfree(dev))
  918. dev->ib_dev.map_phys_fmr = mthca_arbel_map_phys_fmr;
  919. else
  920. dev->ib_dev.map_phys_fmr = mthca_tavor_map_phys_fmr;
  921. }
  922. dev->ib_dev.attach_mcast = mthca_multicast_attach;
  923. dev->ib_dev.detach_mcast = mthca_multicast_detach;
  924. dev->ib_dev.process_mad = mthca_process_mad;
  925. if (mthca_is_memfree(dev)) {
  926. dev->ib_dev.req_notify_cq = mthca_arbel_arm_cq;
  927. dev->ib_dev.post_send = mthca_arbel_post_send;
  928. dev->ib_dev.post_recv = mthca_arbel_post_receive;
  929. } else {
  930. dev->ib_dev.req_notify_cq = mthca_tavor_arm_cq;
  931. dev->ib_dev.post_send = mthca_tavor_post_send;
  932. dev->ib_dev.post_recv = mthca_tavor_post_receive;
  933. }
  934. init_MUTEX(&dev->cap_mask_mutex);
  935. ret = ib_register_device(&dev->ib_dev);
  936. if (ret)
  937. return ret;
  938. for (i = 0; i < ARRAY_SIZE(mthca_class_attributes); ++i) {
  939. ret = class_device_create_file(&dev->ib_dev.class_dev,
  940. mthca_class_attributes[i]);
  941. if (ret) {
  942. ib_unregister_device(&dev->ib_dev);
  943. return ret;
  944. }
  945. }
  946. return 0;
  947. }
  948. void mthca_unregister_device(struct mthca_dev *dev)
  949. {
  950. ib_unregister_device(&dev->ib_dev);
  951. }