mthca_provider.c 34 KB

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