mthca_provider.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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 <linux/sched.h>
  40. #include <linux/slab.h>
  41. #include <linux/stat.h>
  42. #include <linux/mm.h>
  43. #include <linux/export.h>
  44. #include "mthca_dev.h"
  45. #include "mthca_cmd.h"
  46. #include <rdma/mthca-abi.h>
  47. #include "mthca_memfree.h"
  48. static void init_query_mad(struct ib_smp *mad)
  49. {
  50. mad->base_version = 1;
  51. mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  52. mad->class_version = 1;
  53. mad->method = IB_MGMT_METHOD_GET;
  54. }
  55. static int mthca_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
  56. struct ib_udata *uhw)
  57. {
  58. struct ib_smp *in_mad = NULL;
  59. struct ib_smp *out_mad = NULL;
  60. int err = -ENOMEM;
  61. struct mthca_dev *mdev = to_mdev(ibdev);
  62. if (uhw->inlen || uhw->outlen)
  63. return -EINVAL;
  64. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  65. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  66. if (!in_mad || !out_mad)
  67. goto out;
  68. memset(props, 0, sizeof *props);
  69. props->fw_ver = mdev->fw_ver;
  70. init_query_mad(in_mad);
  71. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  72. err = mthca_MAD_IFC(mdev, 1, 1,
  73. 1, NULL, NULL, in_mad, out_mad);
  74. if (err)
  75. goto out;
  76. props->device_cap_flags = mdev->device_cap_flags;
  77. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  78. 0xffffff;
  79. props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
  80. props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
  81. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  82. props->max_mr_size = ~0ull;
  83. props->page_size_cap = mdev->limits.page_size_cap;
  84. props->max_qp = mdev->limits.num_qps - mdev->limits.reserved_qps;
  85. props->max_qp_wr = mdev->limits.max_wqes;
  86. props->max_sge = mdev->limits.max_sg;
  87. props->max_sge_rd = props->max_sge;
  88. props->max_cq = mdev->limits.num_cqs - mdev->limits.reserved_cqs;
  89. props->max_cqe = mdev->limits.max_cqes;
  90. props->max_mr = mdev->limits.num_mpts - mdev->limits.reserved_mrws;
  91. props->max_pd = mdev->limits.num_pds - mdev->limits.reserved_pds;
  92. props->max_qp_rd_atom = 1 << mdev->qp_table.rdb_shift;
  93. props->max_qp_init_rd_atom = mdev->limits.max_qp_init_rdma;
  94. props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
  95. props->max_srq = mdev->limits.num_srqs - mdev->limits.reserved_srqs;
  96. props->max_srq_wr = mdev->limits.max_srq_wqes;
  97. props->max_srq_sge = mdev->limits.max_srq_sge;
  98. props->local_ca_ack_delay = mdev->limits.local_ca_ack_delay;
  99. props->atomic_cap = mdev->limits.flags & DEV_LIM_FLAG_ATOMIC ?
  100. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  101. props->max_pkeys = mdev->limits.pkey_table_len;
  102. props->max_mcast_grp = mdev->limits.num_mgms + mdev->limits.num_amgms;
  103. props->max_mcast_qp_attach = MTHCA_QP_PER_MGM;
  104. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  105. props->max_mcast_grp;
  106. /*
  107. * If Sinai memory key optimization is being used, then only
  108. * the 8-bit key portion will change. For other HCAs, the
  109. * unused index bits will also be used for FMR remapping.
  110. */
  111. if (mdev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
  112. props->max_map_per_fmr = 255;
  113. else
  114. props->max_map_per_fmr =
  115. (1 << (32 - ilog2(mdev->limits.num_mpts))) - 1;
  116. err = 0;
  117. out:
  118. kfree(in_mad);
  119. kfree(out_mad);
  120. return err;
  121. }
  122. static int mthca_query_port(struct ib_device *ibdev,
  123. u8 port, struct ib_port_attr *props)
  124. {
  125. struct ib_smp *in_mad = NULL;
  126. struct ib_smp *out_mad = NULL;
  127. int err = -ENOMEM;
  128. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  129. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  130. if (!in_mad || !out_mad)
  131. goto out;
  132. /* props being zeroed by the caller, avoid zeroing it here */
  133. init_query_mad(in_mad);
  134. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  135. in_mad->attr_mod = cpu_to_be32(port);
  136. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  137. port, NULL, NULL, in_mad, out_mad);
  138. if (err)
  139. goto out;
  140. props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
  141. props->lmc = out_mad->data[34] & 0x7;
  142. props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
  143. props->sm_sl = out_mad->data[36] & 0xf;
  144. props->state = out_mad->data[32] & 0xf;
  145. props->phys_state = out_mad->data[33] >> 4;
  146. props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
  147. props->gid_tbl_len = to_mdev(ibdev)->limits.gid_table_len;
  148. props->max_msg_sz = 0x80000000;
  149. props->pkey_tbl_len = to_mdev(ibdev)->limits.pkey_table_len;
  150. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  151. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  152. props->active_width = out_mad->data[31] & 0xf;
  153. props->active_speed = out_mad->data[35] >> 4;
  154. props->max_mtu = out_mad->data[41] & 0xf;
  155. props->active_mtu = out_mad->data[36] >> 4;
  156. props->subnet_timeout = out_mad->data[51] & 0x1f;
  157. props->max_vl_num = out_mad->data[37] >> 4;
  158. props->init_type_reply = out_mad->data[41] >> 4;
  159. out:
  160. kfree(in_mad);
  161. kfree(out_mad);
  162. return err;
  163. }
  164. static int mthca_modify_device(struct ib_device *ibdev,
  165. int mask,
  166. struct ib_device_modify *props)
  167. {
  168. if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
  169. return -EOPNOTSUPP;
  170. if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
  171. if (mutex_lock_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
  172. return -ERESTARTSYS;
  173. memcpy(ibdev->node_desc, props->node_desc,
  174. IB_DEVICE_NODE_DESC_MAX);
  175. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  176. }
  177. return 0;
  178. }
  179. static int mthca_modify_port(struct ib_device *ibdev,
  180. u8 port, int port_modify_mask,
  181. struct ib_port_modify *props)
  182. {
  183. struct mthca_set_ib_param set_ib;
  184. struct ib_port_attr attr;
  185. int err;
  186. if (mutex_lock_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
  187. return -ERESTARTSYS;
  188. err = ib_query_port(ibdev, port, &attr);
  189. if (err)
  190. goto out;
  191. set_ib.set_si_guid = 0;
  192. set_ib.reset_qkey_viol = !!(port_modify_mask & IB_PORT_RESET_QKEY_CNTR);
  193. set_ib.cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  194. ~props->clr_port_cap_mask;
  195. err = mthca_SET_IB(to_mdev(ibdev), &set_ib, port);
  196. if (err)
  197. goto out;
  198. out:
  199. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  200. return err;
  201. }
  202. static int mthca_query_pkey(struct ib_device *ibdev,
  203. u8 port, u16 index, u16 *pkey)
  204. {
  205. struct ib_smp *in_mad = NULL;
  206. struct ib_smp *out_mad = NULL;
  207. int err = -ENOMEM;
  208. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  209. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  210. if (!in_mad || !out_mad)
  211. goto out;
  212. init_query_mad(in_mad);
  213. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  214. in_mad->attr_mod = cpu_to_be32(index / 32);
  215. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  216. port, NULL, NULL, in_mad, out_mad);
  217. if (err)
  218. goto out;
  219. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  220. out:
  221. kfree(in_mad);
  222. kfree(out_mad);
  223. return err;
  224. }
  225. static int mthca_query_gid(struct ib_device *ibdev, u8 port,
  226. int index, union ib_gid *gid)
  227. {
  228. struct ib_smp *in_mad = NULL;
  229. struct ib_smp *out_mad = NULL;
  230. int err = -ENOMEM;
  231. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  232. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  233. if (!in_mad || !out_mad)
  234. goto out;
  235. init_query_mad(in_mad);
  236. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  237. in_mad->attr_mod = cpu_to_be32(port);
  238. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  239. port, NULL, NULL, in_mad, out_mad);
  240. if (err)
  241. goto out;
  242. memcpy(gid->raw, out_mad->data + 8, 8);
  243. init_query_mad(in_mad);
  244. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  245. in_mad->attr_mod = cpu_to_be32(index / 8);
  246. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  247. port, NULL, NULL, in_mad, out_mad);
  248. if (err)
  249. goto out;
  250. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  251. out:
  252. kfree(in_mad);
  253. kfree(out_mad);
  254. return err;
  255. }
  256. static struct ib_ucontext *mthca_alloc_ucontext(struct ib_device *ibdev,
  257. struct ib_udata *udata)
  258. {
  259. struct mthca_alloc_ucontext_resp uresp;
  260. struct mthca_ucontext *context;
  261. int err;
  262. if (!(to_mdev(ibdev)->active))
  263. return ERR_PTR(-EAGAIN);
  264. memset(&uresp, 0, sizeof uresp);
  265. uresp.qp_tab_size = to_mdev(ibdev)->limits.num_qps;
  266. if (mthca_is_memfree(to_mdev(ibdev)))
  267. uresp.uarc_size = to_mdev(ibdev)->uar_table.uarc_size;
  268. else
  269. uresp.uarc_size = 0;
  270. context = kmalloc(sizeof *context, GFP_KERNEL);
  271. if (!context)
  272. return ERR_PTR(-ENOMEM);
  273. err = mthca_uar_alloc(to_mdev(ibdev), &context->uar);
  274. if (err) {
  275. kfree(context);
  276. return ERR_PTR(err);
  277. }
  278. context->db_tab = mthca_init_user_db_tab(to_mdev(ibdev));
  279. if (IS_ERR(context->db_tab)) {
  280. err = PTR_ERR(context->db_tab);
  281. mthca_uar_free(to_mdev(ibdev), &context->uar);
  282. kfree(context);
  283. return ERR_PTR(err);
  284. }
  285. if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
  286. mthca_cleanup_user_db_tab(to_mdev(ibdev), &context->uar, context->db_tab);
  287. mthca_uar_free(to_mdev(ibdev), &context->uar);
  288. kfree(context);
  289. return ERR_PTR(-EFAULT);
  290. }
  291. context->reg_mr_warned = 0;
  292. return &context->ibucontext;
  293. }
  294. static int mthca_dealloc_ucontext(struct ib_ucontext *context)
  295. {
  296. mthca_cleanup_user_db_tab(to_mdev(context->device), &to_mucontext(context)->uar,
  297. to_mucontext(context)->db_tab);
  298. mthca_uar_free(to_mdev(context->device), &to_mucontext(context)->uar);
  299. kfree(to_mucontext(context));
  300. return 0;
  301. }
  302. static int mthca_mmap_uar(struct ib_ucontext *context,
  303. struct vm_area_struct *vma)
  304. {
  305. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  306. return -EINVAL;
  307. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  308. if (io_remap_pfn_range(vma, vma->vm_start,
  309. to_mucontext(context)->uar.pfn,
  310. PAGE_SIZE, vma->vm_page_prot))
  311. return -EAGAIN;
  312. return 0;
  313. }
  314. static struct ib_pd *mthca_alloc_pd(struct ib_device *ibdev,
  315. struct ib_ucontext *context,
  316. struct ib_udata *udata)
  317. {
  318. struct mthca_pd *pd;
  319. int err;
  320. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  321. if (!pd)
  322. return ERR_PTR(-ENOMEM);
  323. err = mthca_pd_alloc(to_mdev(ibdev), !context, pd);
  324. if (err) {
  325. kfree(pd);
  326. return ERR_PTR(err);
  327. }
  328. if (context) {
  329. if (ib_copy_to_udata(udata, &pd->pd_num, sizeof (__u32))) {
  330. mthca_pd_free(to_mdev(ibdev), pd);
  331. kfree(pd);
  332. return ERR_PTR(-EFAULT);
  333. }
  334. }
  335. return &pd->ibpd;
  336. }
  337. static int mthca_dealloc_pd(struct ib_pd *pd)
  338. {
  339. mthca_pd_free(to_mdev(pd->device), to_mpd(pd));
  340. kfree(pd);
  341. return 0;
  342. }
  343. static struct ib_ah *mthca_ah_create(struct ib_pd *pd,
  344. struct rdma_ah_attr *ah_attr,
  345. struct ib_udata *udata)
  346. {
  347. int err;
  348. struct mthca_ah *ah;
  349. ah = kmalloc(sizeof *ah, GFP_ATOMIC);
  350. if (!ah)
  351. return ERR_PTR(-ENOMEM);
  352. err = mthca_create_ah(to_mdev(pd->device), to_mpd(pd), ah_attr, ah);
  353. if (err) {
  354. kfree(ah);
  355. return ERR_PTR(err);
  356. }
  357. return &ah->ibah;
  358. }
  359. static int mthca_ah_destroy(struct ib_ah *ah)
  360. {
  361. mthca_destroy_ah(to_mdev(ah->device), to_mah(ah));
  362. kfree(ah);
  363. return 0;
  364. }
  365. static struct ib_srq *mthca_create_srq(struct ib_pd *pd,
  366. struct ib_srq_init_attr *init_attr,
  367. struct ib_udata *udata)
  368. {
  369. struct mthca_create_srq ucmd;
  370. struct mthca_ucontext *context = NULL;
  371. struct mthca_srq *srq;
  372. int err;
  373. if (init_attr->srq_type != IB_SRQT_BASIC)
  374. return ERR_PTR(-ENOSYS);
  375. srq = kmalloc(sizeof *srq, GFP_KERNEL);
  376. if (!srq)
  377. return ERR_PTR(-ENOMEM);
  378. if (pd->uobject) {
  379. context = to_mucontext(pd->uobject->context);
  380. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  381. err = -EFAULT;
  382. goto err_free;
  383. }
  384. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  385. context->db_tab, ucmd.db_index,
  386. ucmd.db_page);
  387. if (err)
  388. goto err_free;
  389. srq->mr.ibmr.lkey = ucmd.lkey;
  390. srq->db_index = ucmd.db_index;
  391. }
  392. err = mthca_alloc_srq(to_mdev(pd->device), to_mpd(pd),
  393. &init_attr->attr, srq);
  394. if (err && pd->uobject)
  395. mthca_unmap_user_db(to_mdev(pd->device), &context->uar,
  396. context->db_tab, ucmd.db_index);
  397. if (err)
  398. goto err_free;
  399. if (context && ib_copy_to_udata(udata, &srq->srqn, sizeof (__u32))) {
  400. mthca_free_srq(to_mdev(pd->device), srq);
  401. err = -EFAULT;
  402. goto err_free;
  403. }
  404. return &srq->ibsrq;
  405. err_free:
  406. kfree(srq);
  407. return ERR_PTR(err);
  408. }
  409. static int mthca_destroy_srq(struct ib_srq *srq)
  410. {
  411. struct mthca_ucontext *context;
  412. if (srq->uobject) {
  413. context = to_mucontext(srq->uobject->context);
  414. mthca_unmap_user_db(to_mdev(srq->device), &context->uar,
  415. context->db_tab, to_msrq(srq)->db_index);
  416. }
  417. mthca_free_srq(to_mdev(srq->device), to_msrq(srq));
  418. kfree(srq);
  419. return 0;
  420. }
  421. static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
  422. struct ib_qp_init_attr *init_attr,
  423. struct ib_udata *udata)
  424. {
  425. struct mthca_create_qp ucmd;
  426. struct mthca_qp *qp;
  427. int err;
  428. if (init_attr->create_flags)
  429. return ERR_PTR(-EINVAL);
  430. switch (init_attr->qp_type) {
  431. case IB_QPT_RC:
  432. case IB_QPT_UC:
  433. case IB_QPT_UD:
  434. {
  435. struct mthca_ucontext *context;
  436. qp = kmalloc(sizeof *qp, GFP_KERNEL);
  437. if (!qp)
  438. return ERR_PTR(-ENOMEM);
  439. if (pd->uobject) {
  440. context = to_mucontext(pd->uobject->context);
  441. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  442. kfree(qp);
  443. return ERR_PTR(-EFAULT);
  444. }
  445. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  446. context->db_tab,
  447. ucmd.sq_db_index, ucmd.sq_db_page);
  448. if (err) {
  449. kfree(qp);
  450. return ERR_PTR(err);
  451. }
  452. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  453. context->db_tab,
  454. ucmd.rq_db_index, ucmd.rq_db_page);
  455. if (err) {
  456. mthca_unmap_user_db(to_mdev(pd->device),
  457. &context->uar,
  458. context->db_tab,
  459. ucmd.sq_db_index);
  460. kfree(qp);
  461. return ERR_PTR(err);
  462. }
  463. qp->mr.ibmr.lkey = ucmd.lkey;
  464. qp->sq.db_index = ucmd.sq_db_index;
  465. qp->rq.db_index = ucmd.rq_db_index;
  466. }
  467. err = mthca_alloc_qp(to_mdev(pd->device), to_mpd(pd),
  468. to_mcq(init_attr->send_cq),
  469. to_mcq(init_attr->recv_cq),
  470. init_attr->qp_type, init_attr->sq_sig_type,
  471. &init_attr->cap, qp);
  472. if (err && pd->uobject) {
  473. context = to_mucontext(pd->uobject->context);
  474. mthca_unmap_user_db(to_mdev(pd->device),
  475. &context->uar,
  476. context->db_tab,
  477. ucmd.sq_db_index);
  478. mthca_unmap_user_db(to_mdev(pd->device),
  479. &context->uar,
  480. context->db_tab,
  481. ucmd.rq_db_index);
  482. }
  483. qp->ibqp.qp_num = qp->qpn;
  484. break;
  485. }
  486. case IB_QPT_SMI:
  487. case IB_QPT_GSI:
  488. {
  489. /* Don't allow userspace to create special QPs */
  490. if (pd->uobject)
  491. return ERR_PTR(-EINVAL);
  492. qp = kmalloc(sizeof (struct mthca_sqp), GFP_KERNEL);
  493. if (!qp)
  494. return ERR_PTR(-ENOMEM);
  495. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  496. err = mthca_alloc_sqp(to_mdev(pd->device), to_mpd(pd),
  497. to_mcq(init_attr->send_cq),
  498. to_mcq(init_attr->recv_cq),
  499. init_attr->sq_sig_type, &init_attr->cap,
  500. qp->ibqp.qp_num, init_attr->port_num,
  501. to_msqp(qp));
  502. break;
  503. }
  504. default:
  505. /* Don't support raw QPs */
  506. return ERR_PTR(-ENOSYS);
  507. }
  508. if (err) {
  509. kfree(qp);
  510. return ERR_PTR(err);
  511. }
  512. init_attr->cap.max_send_wr = qp->sq.max;
  513. init_attr->cap.max_recv_wr = qp->rq.max;
  514. init_attr->cap.max_send_sge = qp->sq.max_gs;
  515. init_attr->cap.max_recv_sge = qp->rq.max_gs;
  516. init_attr->cap.max_inline_data = qp->max_inline_data;
  517. return &qp->ibqp;
  518. }
  519. static int mthca_destroy_qp(struct ib_qp *qp)
  520. {
  521. if (qp->uobject) {
  522. mthca_unmap_user_db(to_mdev(qp->device),
  523. &to_mucontext(qp->uobject->context)->uar,
  524. to_mucontext(qp->uobject->context)->db_tab,
  525. to_mqp(qp)->sq.db_index);
  526. mthca_unmap_user_db(to_mdev(qp->device),
  527. &to_mucontext(qp->uobject->context)->uar,
  528. to_mucontext(qp->uobject->context)->db_tab,
  529. to_mqp(qp)->rq.db_index);
  530. }
  531. mthca_free_qp(to_mdev(qp->device), to_mqp(qp));
  532. kfree(qp);
  533. return 0;
  534. }
  535. static struct ib_cq *mthca_create_cq(struct ib_device *ibdev,
  536. const struct ib_cq_init_attr *attr,
  537. struct ib_ucontext *context,
  538. struct ib_udata *udata)
  539. {
  540. int entries = attr->cqe;
  541. struct mthca_create_cq ucmd;
  542. struct mthca_cq *cq;
  543. int nent;
  544. int err;
  545. if (attr->flags)
  546. return ERR_PTR(-EINVAL);
  547. if (entries < 1 || entries > to_mdev(ibdev)->limits.max_cqes)
  548. return ERR_PTR(-EINVAL);
  549. if (context) {
  550. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  551. return ERR_PTR(-EFAULT);
  552. err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  553. to_mucontext(context)->db_tab,
  554. ucmd.set_db_index, ucmd.set_db_page);
  555. if (err)
  556. return ERR_PTR(err);
  557. err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  558. to_mucontext(context)->db_tab,
  559. ucmd.arm_db_index, ucmd.arm_db_page);
  560. if (err)
  561. goto err_unmap_set;
  562. }
  563. cq = kmalloc(sizeof *cq, GFP_KERNEL);
  564. if (!cq) {
  565. err = -ENOMEM;
  566. goto err_unmap_arm;
  567. }
  568. if (context) {
  569. cq->buf.mr.ibmr.lkey = ucmd.lkey;
  570. cq->set_ci_db_index = ucmd.set_db_index;
  571. cq->arm_db_index = ucmd.arm_db_index;
  572. }
  573. for (nent = 1; nent <= entries; nent <<= 1)
  574. ; /* nothing */
  575. err = mthca_init_cq(to_mdev(ibdev), nent,
  576. context ? to_mucontext(context) : NULL,
  577. context ? ucmd.pdn : to_mdev(ibdev)->driver_pd.pd_num,
  578. cq);
  579. if (err)
  580. goto err_free;
  581. if (context && ib_copy_to_udata(udata, &cq->cqn, sizeof (__u32))) {
  582. mthca_free_cq(to_mdev(ibdev), cq);
  583. err = -EFAULT;
  584. goto err_free;
  585. }
  586. cq->resize_buf = NULL;
  587. return &cq->ibcq;
  588. err_free:
  589. kfree(cq);
  590. err_unmap_arm:
  591. if (context)
  592. mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  593. to_mucontext(context)->db_tab, ucmd.arm_db_index);
  594. err_unmap_set:
  595. if (context)
  596. mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  597. to_mucontext(context)->db_tab, ucmd.set_db_index);
  598. return ERR_PTR(err);
  599. }
  600. static int mthca_alloc_resize_buf(struct mthca_dev *dev, struct mthca_cq *cq,
  601. int entries)
  602. {
  603. int ret;
  604. spin_lock_irq(&cq->lock);
  605. if (cq->resize_buf) {
  606. ret = -EBUSY;
  607. goto unlock;
  608. }
  609. cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_ATOMIC);
  610. if (!cq->resize_buf) {
  611. ret = -ENOMEM;
  612. goto unlock;
  613. }
  614. cq->resize_buf->state = CQ_RESIZE_ALLOC;
  615. ret = 0;
  616. unlock:
  617. spin_unlock_irq(&cq->lock);
  618. if (ret)
  619. return ret;
  620. ret = mthca_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
  621. if (ret) {
  622. spin_lock_irq(&cq->lock);
  623. kfree(cq->resize_buf);
  624. cq->resize_buf = NULL;
  625. spin_unlock_irq(&cq->lock);
  626. return ret;
  627. }
  628. cq->resize_buf->cqe = entries - 1;
  629. spin_lock_irq(&cq->lock);
  630. cq->resize_buf->state = CQ_RESIZE_READY;
  631. spin_unlock_irq(&cq->lock);
  632. return 0;
  633. }
  634. static int mthca_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
  635. {
  636. struct mthca_dev *dev = to_mdev(ibcq->device);
  637. struct mthca_cq *cq = to_mcq(ibcq);
  638. struct mthca_resize_cq ucmd;
  639. u32 lkey;
  640. int ret;
  641. if (entries < 1 || entries > dev->limits.max_cqes)
  642. return -EINVAL;
  643. mutex_lock(&cq->mutex);
  644. entries = roundup_pow_of_two(entries + 1);
  645. if (entries == ibcq->cqe + 1) {
  646. ret = 0;
  647. goto out;
  648. }
  649. if (cq->is_kernel) {
  650. ret = mthca_alloc_resize_buf(dev, cq, entries);
  651. if (ret)
  652. goto out;
  653. lkey = cq->resize_buf->buf.mr.ibmr.lkey;
  654. } else {
  655. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  656. ret = -EFAULT;
  657. goto out;
  658. }
  659. lkey = ucmd.lkey;
  660. }
  661. ret = mthca_RESIZE_CQ(dev, cq->cqn, lkey, ilog2(entries));
  662. if (ret) {
  663. if (cq->resize_buf) {
  664. mthca_free_cq_buf(dev, &cq->resize_buf->buf,
  665. cq->resize_buf->cqe);
  666. kfree(cq->resize_buf);
  667. spin_lock_irq(&cq->lock);
  668. cq->resize_buf = NULL;
  669. spin_unlock_irq(&cq->lock);
  670. }
  671. goto out;
  672. }
  673. if (cq->is_kernel) {
  674. struct mthca_cq_buf tbuf;
  675. int tcqe;
  676. spin_lock_irq(&cq->lock);
  677. if (cq->resize_buf->state == CQ_RESIZE_READY) {
  678. mthca_cq_resize_copy_cqes(cq);
  679. tbuf = cq->buf;
  680. tcqe = cq->ibcq.cqe;
  681. cq->buf = cq->resize_buf->buf;
  682. cq->ibcq.cqe = cq->resize_buf->cqe;
  683. } else {
  684. tbuf = cq->resize_buf->buf;
  685. tcqe = cq->resize_buf->cqe;
  686. }
  687. kfree(cq->resize_buf);
  688. cq->resize_buf = NULL;
  689. spin_unlock_irq(&cq->lock);
  690. mthca_free_cq_buf(dev, &tbuf, tcqe);
  691. } else
  692. ibcq->cqe = entries - 1;
  693. out:
  694. mutex_unlock(&cq->mutex);
  695. return ret;
  696. }
  697. static int mthca_destroy_cq(struct ib_cq *cq)
  698. {
  699. if (cq->uobject) {
  700. mthca_unmap_user_db(to_mdev(cq->device),
  701. &to_mucontext(cq->uobject->context)->uar,
  702. to_mucontext(cq->uobject->context)->db_tab,
  703. to_mcq(cq)->arm_db_index);
  704. mthca_unmap_user_db(to_mdev(cq->device),
  705. &to_mucontext(cq->uobject->context)->uar,
  706. to_mucontext(cq->uobject->context)->db_tab,
  707. to_mcq(cq)->set_ci_db_index);
  708. }
  709. mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
  710. kfree(cq);
  711. return 0;
  712. }
  713. static inline u32 convert_access(int acc)
  714. {
  715. return (acc & IB_ACCESS_REMOTE_ATOMIC ? MTHCA_MPT_FLAG_ATOMIC : 0) |
  716. (acc & IB_ACCESS_REMOTE_WRITE ? MTHCA_MPT_FLAG_REMOTE_WRITE : 0) |
  717. (acc & IB_ACCESS_REMOTE_READ ? MTHCA_MPT_FLAG_REMOTE_READ : 0) |
  718. (acc & IB_ACCESS_LOCAL_WRITE ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0) |
  719. MTHCA_MPT_FLAG_LOCAL_READ;
  720. }
  721. static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
  722. {
  723. struct mthca_mr *mr;
  724. int err;
  725. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  726. if (!mr)
  727. return ERR_PTR(-ENOMEM);
  728. err = mthca_mr_alloc_notrans(to_mdev(pd->device),
  729. to_mpd(pd)->pd_num,
  730. convert_access(acc), mr);
  731. if (err) {
  732. kfree(mr);
  733. return ERR_PTR(err);
  734. }
  735. mr->umem = NULL;
  736. return &mr->ibmr;
  737. }
  738. static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  739. u64 virt, int acc, struct ib_udata *udata)
  740. {
  741. struct mthca_dev *dev = to_mdev(pd->device);
  742. struct scatterlist *sg;
  743. struct mthca_mr *mr;
  744. struct mthca_reg_mr ucmd;
  745. u64 *pages;
  746. int shift, n, len;
  747. int i, k, entry;
  748. int err = 0;
  749. int write_mtt_size;
  750. if (udata->inlen < sizeof ucmd) {
  751. if (!to_mucontext(pd->uobject->context)->reg_mr_warned) {
  752. mthca_warn(dev, "Process '%s' did not pass in MR attrs.\n",
  753. current->comm);
  754. mthca_warn(dev, " Update libmthca to fix this.\n");
  755. }
  756. ++to_mucontext(pd->uobject->context)->reg_mr_warned;
  757. ucmd.mr_attrs = 0;
  758. } else if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  759. return ERR_PTR(-EFAULT);
  760. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  761. if (!mr)
  762. return ERR_PTR(-ENOMEM);
  763. mr->umem = ib_umem_get(pd->uobject->context, start, length, acc,
  764. ucmd.mr_attrs & MTHCA_MR_DMASYNC);
  765. if (IS_ERR(mr->umem)) {
  766. err = PTR_ERR(mr->umem);
  767. goto err;
  768. }
  769. shift = mr->umem->page_shift;
  770. n = mr->umem->nmap;
  771. mr->mtt = mthca_alloc_mtt(dev, n);
  772. if (IS_ERR(mr->mtt)) {
  773. err = PTR_ERR(mr->mtt);
  774. goto err_umem;
  775. }
  776. pages = (u64 *) __get_free_page(GFP_KERNEL);
  777. if (!pages) {
  778. err = -ENOMEM;
  779. goto err_mtt;
  780. }
  781. i = n = 0;
  782. write_mtt_size = min(mthca_write_mtt_size(dev), (int) (PAGE_SIZE / sizeof *pages));
  783. for_each_sg(mr->umem->sg_head.sgl, sg, mr->umem->nmap, entry) {
  784. len = sg_dma_len(sg) >> shift;
  785. for (k = 0; k < len; ++k) {
  786. pages[i++] = sg_dma_address(sg) + (k << shift);
  787. /*
  788. * Be friendly to write_mtt and pass it chunks
  789. * of appropriate size.
  790. */
  791. if (i == write_mtt_size) {
  792. err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
  793. if (err)
  794. goto mtt_done;
  795. n += i;
  796. i = 0;
  797. }
  798. }
  799. }
  800. if (i)
  801. err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
  802. mtt_done:
  803. free_page((unsigned long) pages);
  804. if (err)
  805. goto err_mtt;
  806. err = mthca_mr_alloc(dev, to_mpd(pd)->pd_num, shift, virt, length,
  807. convert_access(acc), mr);
  808. if (err)
  809. goto err_mtt;
  810. return &mr->ibmr;
  811. err_mtt:
  812. mthca_free_mtt(dev, mr->mtt);
  813. err_umem:
  814. ib_umem_release(mr->umem);
  815. err:
  816. kfree(mr);
  817. return ERR_PTR(err);
  818. }
  819. static int mthca_dereg_mr(struct ib_mr *mr)
  820. {
  821. struct mthca_mr *mmr = to_mmr(mr);
  822. mthca_free_mr(to_mdev(mr->device), mmr);
  823. if (mmr->umem)
  824. ib_umem_release(mmr->umem);
  825. kfree(mmr);
  826. return 0;
  827. }
  828. static struct ib_fmr *mthca_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
  829. struct ib_fmr_attr *fmr_attr)
  830. {
  831. struct mthca_fmr *fmr;
  832. int err;
  833. fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
  834. if (!fmr)
  835. return ERR_PTR(-ENOMEM);
  836. memcpy(&fmr->attr, fmr_attr, sizeof *fmr_attr);
  837. err = mthca_fmr_alloc(to_mdev(pd->device), to_mpd(pd)->pd_num,
  838. convert_access(mr_access_flags), fmr);
  839. if (err) {
  840. kfree(fmr);
  841. return ERR_PTR(err);
  842. }
  843. return &fmr->ibmr;
  844. }
  845. static int mthca_dealloc_fmr(struct ib_fmr *fmr)
  846. {
  847. struct mthca_fmr *mfmr = to_mfmr(fmr);
  848. int err;
  849. err = mthca_free_fmr(to_mdev(fmr->device), mfmr);
  850. if (err)
  851. return err;
  852. kfree(mfmr);
  853. return 0;
  854. }
  855. static int mthca_unmap_fmr(struct list_head *fmr_list)
  856. {
  857. struct ib_fmr *fmr;
  858. int err;
  859. struct mthca_dev *mdev = NULL;
  860. list_for_each_entry(fmr, fmr_list, list) {
  861. if (mdev && to_mdev(fmr->device) != mdev)
  862. return -EINVAL;
  863. mdev = to_mdev(fmr->device);
  864. }
  865. if (!mdev)
  866. return 0;
  867. if (mthca_is_memfree(mdev)) {
  868. list_for_each_entry(fmr, fmr_list, list)
  869. mthca_arbel_fmr_unmap(mdev, to_mfmr(fmr));
  870. wmb();
  871. } else
  872. list_for_each_entry(fmr, fmr_list, list)
  873. mthca_tavor_fmr_unmap(mdev, to_mfmr(fmr));
  874. err = mthca_SYNC_TPT(mdev);
  875. return err;
  876. }
  877. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  878. char *buf)
  879. {
  880. struct mthca_dev *dev =
  881. container_of(device, struct mthca_dev, ib_dev.dev);
  882. return sprintf(buf, "%x\n", dev->rev_id);
  883. }
  884. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  885. char *buf)
  886. {
  887. struct mthca_dev *dev =
  888. container_of(device, struct mthca_dev, ib_dev.dev);
  889. switch (dev->pdev->device) {
  890. case PCI_DEVICE_ID_MELLANOX_TAVOR:
  891. return sprintf(buf, "MT23108\n");
  892. case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
  893. return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
  894. case PCI_DEVICE_ID_MELLANOX_ARBEL:
  895. return sprintf(buf, "MT25208\n");
  896. case PCI_DEVICE_ID_MELLANOX_SINAI:
  897. case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
  898. return sprintf(buf, "MT25204\n");
  899. default:
  900. return sprintf(buf, "unknown\n");
  901. }
  902. }
  903. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  904. char *buf)
  905. {
  906. struct mthca_dev *dev =
  907. container_of(device, struct mthca_dev, ib_dev.dev);
  908. return sprintf(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
  909. }
  910. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  911. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  912. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  913. static struct device_attribute *mthca_dev_attributes[] = {
  914. &dev_attr_hw_rev,
  915. &dev_attr_hca_type,
  916. &dev_attr_board_id
  917. };
  918. static int mthca_init_node_data(struct mthca_dev *dev)
  919. {
  920. struct ib_smp *in_mad = NULL;
  921. struct ib_smp *out_mad = NULL;
  922. int err = -ENOMEM;
  923. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  924. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  925. if (!in_mad || !out_mad)
  926. goto out;
  927. init_query_mad(in_mad);
  928. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  929. err = mthca_MAD_IFC(dev, 1, 1,
  930. 1, NULL, NULL, in_mad, out_mad);
  931. if (err)
  932. goto out;
  933. memcpy(dev->ib_dev.node_desc, out_mad->data, IB_DEVICE_NODE_DESC_MAX);
  934. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  935. err = mthca_MAD_IFC(dev, 1, 1,
  936. 1, NULL, NULL, in_mad, out_mad);
  937. if (err)
  938. goto out;
  939. if (mthca_is_memfree(dev))
  940. dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
  941. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  942. out:
  943. kfree(in_mad);
  944. kfree(out_mad);
  945. return err;
  946. }
  947. static int mthca_port_immutable(struct ib_device *ibdev, u8 port_num,
  948. struct ib_port_immutable *immutable)
  949. {
  950. struct ib_port_attr attr;
  951. int err;
  952. immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
  953. err = ib_query_port(ibdev, port_num, &attr);
  954. if (err)
  955. return err;
  956. immutable->pkey_tbl_len = attr.pkey_tbl_len;
  957. immutable->gid_tbl_len = attr.gid_tbl_len;
  958. immutable->max_mad_size = IB_MGMT_MAD_SIZE;
  959. return 0;
  960. }
  961. static void get_dev_fw_str(struct ib_device *device, char *str)
  962. {
  963. struct mthca_dev *dev =
  964. container_of(device, struct mthca_dev, ib_dev);
  965. snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%d",
  966. (int) (dev->fw_ver >> 32),
  967. (int) (dev->fw_ver >> 16) & 0xffff,
  968. (int) dev->fw_ver & 0xffff);
  969. }
  970. int mthca_register_device(struct mthca_dev *dev)
  971. {
  972. int ret;
  973. int i;
  974. ret = mthca_init_node_data(dev);
  975. if (ret)
  976. return ret;
  977. strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
  978. dev->ib_dev.owner = THIS_MODULE;
  979. dev->ib_dev.uverbs_abi_ver = MTHCA_UVERBS_ABI_VERSION;
  980. dev->ib_dev.uverbs_cmd_mask =
  981. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  982. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  983. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  984. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  985. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  986. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  987. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  988. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  989. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  990. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  991. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  992. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  993. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  994. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  995. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  996. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  997. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST);
  998. dev->ib_dev.node_type = RDMA_NODE_IB_CA;
  999. dev->ib_dev.phys_port_cnt = dev->limits.num_ports;
  1000. dev->ib_dev.num_comp_vectors = 1;
  1001. dev->ib_dev.dev.parent = &dev->pdev->dev;
  1002. dev->ib_dev.query_device = mthca_query_device;
  1003. dev->ib_dev.query_port = mthca_query_port;
  1004. dev->ib_dev.modify_device = mthca_modify_device;
  1005. dev->ib_dev.modify_port = mthca_modify_port;
  1006. dev->ib_dev.query_pkey = mthca_query_pkey;
  1007. dev->ib_dev.query_gid = mthca_query_gid;
  1008. dev->ib_dev.alloc_ucontext = mthca_alloc_ucontext;
  1009. dev->ib_dev.dealloc_ucontext = mthca_dealloc_ucontext;
  1010. dev->ib_dev.mmap = mthca_mmap_uar;
  1011. dev->ib_dev.alloc_pd = mthca_alloc_pd;
  1012. dev->ib_dev.dealloc_pd = mthca_dealloc_pd;
  1013. dev->ib_dev.create_ah = mthca_ah_create;
  1014. dev->ib_dev.query_ah = mthca_ah_query;
  1015. dev->ib_dev.destroy_ah = mthca_ah_destroy;
  1016. if (dev->mthca_flags & MTHCA_FLAG_SRQ) {
  1017. dev->ib_dev.create_srq = mthca_create_srq;
  1018. dev->ib_dev.modify_srq = mthca_modify_srq;
  1019. dev->ib_dev.query_srq = mthca_query_srq;
  1020. dev->ib_dev.destroy_srq = mthca_destroy_srq;
  1021. dev->ib_dev.uverbs_cmd_mask |=
  1022. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  1023. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  1024. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  1025. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
  1026. if (mthca_is_memfree(dev))
  1027. dev->ib_dev.post_srq_recv = mthca_arbel_post_srq_recv;
  1028. else
  1029. dev->ib_dev.post_srq_recv = mthca_tavor_post_srq_recv;
  1030. }
  1031. dev->ib_dev.create_qp = mthca_create_qp;
  1032. dev->ib_dev.modify_qp = mthca_modify_qp;
  1033. dev->ib_dev.query_qp = mthca_query_qp;
  1034. dev->ib_dev.destroy_qp = mthca_destroy_qp;
  1035. dev->ib_dev.create_cq = mthca_create_cq;
  1036. dev->ib_dev.resize_cq = mthca_resize_cq;
  1037. dev->ib_dev.destroy_cq = mthca_destroy_cq;
  1038. dev->ib_dev.poll_cq = mthca_poll_cq;
  1039. dev->ib_dev.get_dma_mr = mthca_get_dma_mr;
  1040. dev->ib_dev.reg_user_mr = mthca_reg_user_mr;
  1041. dev->ib_dev.dereg_mr = mthca_dereg_mr;
  1042. dev->ib_dev.get_port_immutable = mthca_port_immutable;
  1043. dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
  1044. if (dev->mthca_flags & MTHCA_FLAG_FMR) {
  1045. dev->ib_dev.alloc_fmr = mthca_alloc_fmr;
  1046. dev->ib_dev.unmap_fmr = mthca_unmap_fmr;
  1047. dev->ib_dev.dealloc_fmr = mthca_dealloc_fmr;
  1048. if (mthca_is_memfree(dev))
  1049. dev->ib_dev.map_phys_fmr = mthca_arbel_map_phys_fmr;
  1050. else
  1051. dev->ib_dev.map_phys_fmr = mthca_tavor_map_phys_fmr;
  1052. }
  1053. dev->ib_dev.attach_mcast = mthca_multicast_attach;
  1054. dev->ib_dev.detach_mcast = mthca_multicast_detach;
  1055. dev->ib_dev.process_mad = mthca_process_mad;
  1056. if (mthca_is_memfree(dev)) {
  1057. dev->ib_dev.req_notify_cq = mthca_arbel_arm_cq;
  1058. dev->ib_dev.post_send = mthca_arbel_post_send;
  1059. dev->ib_dev.post_recv = mthca_arbel_post_receive;
  1060. } else {
  1061. dev->ib_dev.req_notify_cq = mthca_tavor_arm_cq;
  1062. dev->ib_dev.post_send = mthca_tavor_post_send;
  1063. dev->ib_dev.post_recv = mthca_tavor_post_receive;
  1064. }
  1065. mutex_init(&dev->cap_mask_mutex);
  1066. dev->ib_dev.driver_id = RDMA_DRIVER_MTHCA;
  1067. ret = ib_register_device(&dev->ib_dev, NULL);
  1068. if (ret)
  1069. return ret;
  1070. for (i = 0; i < ARRAY_SIZE(mthca_dev_attributes); ++i) {
  1071. ret = device_create_file(&dev->ib_dev.dev,
  1072. mthca_dev_attributes[i]);
  1073. if (ret) {
  1074. ib_unregister_device(&dev->ib_dev);
  1075. return ret;
  1076. }
  1077. }
  1078. mthca_start_catas_poll(dev);
  1079. return 0;
  1080. }
  1081. void mthca_unregister_device(struct mthca_dev *dev)
  1082. {
  1083. mthca_stop_catas_poll(dev);
  1084. ib_unregister_device(&dev->ib_dev);
  1085. }