mthca_provider.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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_phys_mr(struct ib_pd *pd,
  737. struct ib_phys_buf *buffer_list,
  738. int num_phys_buf,
  739. int acc,
  740. u64 *iova_start)
  741. {
  742. struct mthca_mr *mr;
  743. u64 *page_list;
  744. u64 total_size;
  745. unsigned long mask;
  746. int shift;
  747. int npages;
  748. int err;
  749. int i, j, n;
  750. mask = buffer_list[0].addr ^ *iova_start;
  751. total_size = 0;
  752. for (i = 0; i < num_phys_buf; ++i) {
  753. if (i != 0)
  754. mask |= buffer_list[i].addr;
  755. if (i != num_phys_buf - 1)
  756. mask |= buffer_list[i].addr + buffer_list[i].size;
  757. total_size += buffer_list[i].size;
  758. }
  759. if (mask & ~PAGE_MASK)
  760. return ERR_PTR(-EINVAL);
  761. shift = __ffs(mask | 1 << 31);
  762. buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
  763. buffer_list[0].addr &= ~0ull << shift;
  764. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  765. if (!mr)
  766. return ERR_PTR(-ENOMEM);
  767. npages = 0;
  768. for (i = 0; i < num_phys_buf; ++i)
  769. npages += (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  770. if (!npages)
  771. return &mr->ibmr;
  772. page_list = kmalloc(npages * sizeof *page_list, GFP_KERNEL);
  773. if (!page_list) {
  774. kfree(mr);
  775. return ERR_PTR(-ENOMEM);
  776. }
  777. n = 0;
  778. for (i = 0; i < num_phys_buf; ++i)
  779. for (j = 0;
  780. j < (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  781. ++j)
  782. page_list[n++] = buffer_list[i].addr + ((u64) j << shift);
  783. mthca_dbg(to_mdev(pd->device), "Registering memory at %llx (iova %llx) "
  784. "in PD %x; shift %d, npages %d.\n",
  785. (unsigned long long) buffer_list[0].addr,
  786. (unsigned long long) *iova_start,
  787. to_mpd(pd)->pd_num,
  788. shift, npages);
  789. err = mthca_mr_alloc_phys(to_mdev(pd->device),
  790. to_mpd(pd)->pd_num,
  791. page_list, shift, npages,
  792. *iova_start, total_size,
  793. convert_access(acc), mr);
  794. if (err) {
  795. kfree(page_list);
  796. kfree(mr);
  797. return ERR_PTR(err);
  798. }
  799. kfree(page_list);
  800. mr->umem = NULL;
  801. return &mr->ibmr;
  802. }
  803. static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  804. u64 virt, int acc, struct ib_udata *udata)
  805. {
  806. struct mthca_dev *dev = to_mdev(pd->device);
  807. struct scatterlist *sg;
  808. struct mthca_mr *mr;
  809. struct mthca_reg_mr ucmd;
  810. u64 *pages;
  811. int shift, n, len;
  812. int i, k, entry;
  813. int err = 0;
  814. int write_mtt_size;
  815. if (udata->inlen - sizeof (struct ib_uverbs_cmd_hdr) < sizeof ucmd) {
  816. if (!to_mucontext(pd->uobject->context)->reg_mr_warned) {
  817. mthca_warn(dev, "Process '%s' did not pass in MR attrs.\n",
  818. current->comm);
  819. mthca_warn(dev, " Update libmthca to fix this.\n");
  820. }
  821. ++to_mucontext(pd->uobject->context)->reg_mr_warned;
  822. ucmd.mr_attrs = 0;
  823. } else if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  824. return ERR_PTR(-EFAULT);
  825. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  826. if (!mr)
  827. return ERR_PTR(-ENOMEM);
  828. mr->umem = ib_umem_get(pd->uobject->context, start, length, acc,
  829. ucmd.mr_attrs & MTHCA_MR_DMASYNC);
  830. if (IS_ERR(mr->umem)) {
  831. err = PTR_ERR(mr->umem);
  832. goto err;
  833. }
  834. shift = ffs(mr->umem->page_size) - 1;
  835. n = mr->umem->nmap;
  836. mr->mtt = mthca_alloc_mtt(dev, n);
  837. if (IS_ERR(mr->mtt)) {
  838. err = PTR_ERR(mr->mtt);
  839. goto err_umem;
  840. }
  841. pages = (u64 *) __get_free_page(GFP_KERNEL);
  842. if (!pages) {
  843. err = -ENOMEM;
  844. goto err_mtt;
  845. }
  846. i = n = 0;
  847. write_mtt_size = min(mthca_write_mtt_size(dev), (int) (PAGE_SIZE / sizeof *pages));
  848. for_each_sg(mr->umem->sg_head.sgl, sg, mr->umem->nmap, entry) {
  849. len = sg_dma_len(sg) >> shift;
  850. for (k = 0; k < len; ++k) {
  851. pages[i++] = sg_dma_address(sg) +
  852. mr->umem->page_size * k;
  853. /*
  854. * Be friendly to write_mtt and pass it chunks
  855. * of appropriate size.
  856. */
  857. if (i == write_mtt_size) {
  858. err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
  859. if (err)
  860. goto mtt_done;
  861. n += i;
  862. i = 0;
  863. }
  864. }
  865. }
  866. if (i)
  867. err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
  868. mtt_done:
  869. free_page((unsigned long) pages);
  870. if (err)
  871. goto err_mtt;
  872. err = mthca_mr_alloc(dev, to_mpd(pd)->pd_num, shift, virt, length,
  873. convert_access(acc), mr);
  874. if (err)
  875. goto err_mtt;
  876. return &mr->ibmr;
  877. err_mtt:
  878. mthca_free_mtt(dev, mr->mtt);
  879. err_umem:
  880. ib_umem_release(mr->umem);
  881. err:
  882. kfree(mr);
  883. return ERR_PTR(err);
  884. }
  885. static int mthca_dereg_mr(struct ib_mr *mr)
  886. {
  887. struct mthca_mr *mmr = to_mmr(mr);
  888. mthca_free_mr(to_mdev(mr->device), mmr);
  889. if (mmr->umem)
  890. ib_umem_release(mmr->umem);
  891. kfree(mmr);
  892. return 0;
  893. }
  894. static struct ib_fmr *mthca_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
  895. struct ib_fmr_attr *fmr_attr)
  896. {
  897. struct mthca_fmr *fmr;
  898. int err;
  899. fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
  900. if (!fmr)
  901. return ERR_PTR(-ENOMEM);
  902. memcpy(&fmr->attr, fmr_attr, sizeof *fmr_attr);
  903. err = mthca_fmr_alloc(to_mdev(pd->device), to_mpd(pd)->pd_num,
  904. convert_access(mr_access_flags), fmr);
  905. if (err) {
  906. kfree(fmr);
  907. return ERR_PTR(err);
  908. }
  909. return &fmr->ibmr;
  910. }
  911. static int mthca_dealloc_fmr(struct ib_fmr *fmr)
  912. {
  913. struct mthca_fmr *mfmr = to_mfmr(fmr);
  914. int err;
  915. err = mthca_free_fmr(to_mdev(fmr->device), mfmr);
  916. if (err)
  917. return err;
  918. kfree(mfmr);
  919. return 0;
  920. }
  921. static int mthca_unmap_fmr(struct list_head *fmr_list)
  922. {
  923. struct ib_fmr *fmr;
  924. int err;
  925. struct mthca_dev *mdev = NULL;
  926. list_for_each_entry(fmr, fmr_list, list) {
  927. if (mdev && to_mdev(fmr->device) != mdev)
  928. return -EINVAL;
  929. mdev = to_mdev(fmr->device);
  930. }
  931. if (!mdev)
  932. return 0;
  933. if (mthca_is_memfree(mdev)) {
  934. list_for_each_entry(fmr, fmr_list, list)
  935. mthca_arbel_fmr_unmap(mdev, to_mfmr(fmr));
  936. wmb();
  937. } else
  938. list_for_each_entry(fmr, fmr_list, list)
  939. mthca_tavor_fmr_unmap(mdev, to_mfmr(fmr));
  940. err = mthca_SYNC_TPT(mdev);
  941. return err;
  942. }
  943. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  944. char *buf)
  945. {
  946. struct mthca_dev *dev =
  947. container_of(device, struct mthca_dev, ib_dev.dev);
  948. return sprintf(buf, "%x\n", dev->rev_id);
  949. }
  950. static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
  951. char *buf)
  952. {
  953. struct mthca_dev *dev =
  954. container_of(device, struct mthca_dev, ib_dev.dev);
  955. return sprintf(buf, "%d.%d.%d\n", (int) (dev->fw_ver >> 32),
  956. (int) (dev->fw_ver >> 16) & 0xffff,
  957. (int) dev->fw_ver & 0xffff);
  958. }
  959. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  960. char *buf)
  961. {
  962. struct mthca_dev *dev =
  963. container_of(device, struct mthca_dev, ib_dev.dev);
  964. switch (dev->pdev->device) {
  965. case PCI_DEVICE_ID_MELLANOX_TAVOR:
  966. return sprintf(buf, "MT23108\n");
  967. case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
  968. return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
  969. case PCI_DEVICE_ID_MELLANOX_ARBEL:
  970. return sprintf(buf, "MT25208\n");
  971. case PCI_DEVICE_ID_MELLANOX_SINAI:
  972. case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
  973. return sprintf(buf, "MT25204\n");
  974. default:
  975. return sprintf(buf, "unknown\n");
  976. }
  977. }
  978. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  979. char *buf)
  980. {
  981. struct mthca_dev *dev =
  982. container_of(device, struct mthca_dev, ib_dev.dev);
  983. return sprintf(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
  984. }
  985. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  986. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  987. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  988. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  989. static struct device_attribute *mthca_dev_attributes[] = {
  990. &dev_attr_hw_rev,
  991. &dev_attr_fw_ver,
  992. &dev_attr_hca_type,
  993. &dev_attr_board_id
  994. };
  995. static int mthca_init_node_data(struct mthca_dev *dev)
  996. {
  997. struct ib_smp *in_mad = NULL;
  998. struct ib_smp *out_mad = NULL;
  999. int err = -ENOMEM;
  1000. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  1001. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  1002. if (!in_mad || !out_mad)
  1003. goto out;
  1004. init_query_mad(in_mad);
  1005. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  1006. err = mthca_MAD_IFC(dev, 1, 1,
  1007. 1, NULL, NULL, in_mad, out_mad);
  1008. if (err)
  1009. goto out;
  1010. memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
  1011. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  1012. err = mthca_MAD_IFC(dev, 1, 1,
  1013. 1, NULL, NULL, in_mad, out_mad);
  1014. if (err)
  1015. goto out;
  1016. if (mthca_is_memfree(dev))
  1017. dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
  1018. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  1019. out:
  1020. kfree(in_mad);
  1021. kfree(out_mad);
  1022. return err;
  1023. }
  1024. static int mthca_port_immutable(struct ib_device *ibdev, u8 port_num,
  1025. struct ib_port_immutable *immutable)
  1026. {
  1027. struct ib_port_attr attr;
  1028. int err;
  1029. err = mthca_query_port(ibdev, port_num, &attr);
  1030. if (err)
  1031. return err;
  1032. immutable->pkey_tbl_len = attr.pkey_tbl_len;
  1033. immutable->gid_tbl_len = attr.gid_tbl_len;
  1034. immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
  1035. immutable->max_mad_size = IB_MGMT_MAD_SIZE;
  1036. return 0;
  1037. }
  1038. int mthca_register_device(struct mthca_dev *dev)
  1039. {
  1040. int ret;
  1041. int i;
  1042. ret = mthca_init_node_data(dev);
  1043. if (ret)
  1044. return ret;
  1045. strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
  1046. dev->ib_dev.owner = THIS_MODULE;
  1047. dev->ib_dev.uverbs_abi_ver = MTHCA_UVERBS_ABI_VERSION;
  1048. dev->ib_dev.uverbs_cmd_mask =
  1049. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  1050. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  1051. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  1052. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  1053. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  1054. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  1055. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  1056. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  1057. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  1058. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  1059. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  1060. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  1061. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  1062. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  1063. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  1064. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  1065. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST);
  1066. dev->ib_dev.node_type = RDMA_NODE_IB_CA;
  1067. dev->ib_dev.phys_port_cnt = dev->limits.num_ports;
  1068. dev->ib_dev.num_comp_vectors = 1;
  1069. dev->ib_dev.dma_device = &dev->pdev->dev;
  1070. dev->ib_dev.query_device = mthca_query_device;
  1071. dev->ib_dev.query_port = mthca_query_port;
  1072. dev->ib_dev.modify_device = mthca_modify_device;
  1073. dev->ib_dev.modify_port = mthca_modify_port;
  1074. dev->ib_dev.query_pkey = mthca_query_pkey;
  1075. dev->ib_dev.query_gid = mthca_query_gid;
  1076. dev->ib_dev.alloc_ucontext = mthca_alloc_ucontext;
  1077. dev->ib_dev.dealloc_ucontext = mthca_dealloc_ucontext;
  1078. dev->ib_dev.mmap = mthca_mmap_uar;
  1079. dev->ib_dev.alloc_pd = mthca_alloc_pd;
  1080. dev->ib_dev.dealloc_pd = mthca_dealloc_pd;
  1081. dev->ib_dev.create_ah = mthca_ah_create;
  1082. dev->ib_dev.query_ah = mthca_ah_query;
  1083. dev->ib_dev.destroy_ah = mthca_ah_destroy;
  1084. if (dev->mthca_flags & MTHCA_FLAG_SRQ) {
  1085. dev->ib_dev.create_srq = mthca_create_srq;
  1086. dev->ib_dev.modify_srq = mthca_modify_srq;
  1087. dev->ib_dev.query_srq = mthca_query_srq;
  1088. dev->ib_dev.destroy_srq = mthca_destroy_srq;
  1089. dev->ib_dev.uverbs_cmd_mask |=
  1090. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  1091. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  1092. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  1093. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
  1094. if (mthca_is_memfree(dev))
  1095. dev->ib_dev.post_srq_recv = mthca_arbel_post_srq_recv;
  1096. else
  1097. dev->ib_dev.post_srq_recv = mthca_tavor_post_srq_recv;
  1098. }
  1099. dev->ib_dev.create_qp = mthca_create_qp;
  1100. dev->ib_dev.modify_qp = mthca_modify_qp;
  1101. dev->ib_dev.query_qp = mthca_query_qp;
  1102. dev->ib_dev.destroy_qp = mthca_destroy_qp;
  1103. dev->ib_dev.create_cq = mthca_create_cq;
  1104. dev->ib_dev.resize_cq = mthca_resize_cq;
  1105. dev->ib_dev.destroy_cq = mthca_destroy_cq;
  1106. dev->ib_dev.poll_cq = mthca_poll_cq;
  1107. dev->ib_dev.get_dma_mr = mthca_get_dma_mr;
  1108. dev->ib_dev.reg_phys_mr = mthca_reg_phys_mr;
  1109. dev->ib_dev.reg_user_mr = mthca_reg_user_mr;
  1110. dev->ib_dev.dereg_mr = mthca_dereg_mr;
  1111. dev->ib_dev.get_port_immutable = mthca_port_immutable;
  1112. if (dev->mthca_flags & MTHCA_FLAG_FMR) {
  1113. dev->ib_dev.alloc_fmr = mthca_alloc_fmr;
  1114. dev->ib_dev.unmap_fmr = mthca_unmap_fmr;
  1115. dev->ib_dev.dealloc_fmr = mthca_dealloc_fmr;
  1116. if (mthca_is_memfree(dev))
  1117. dev->ib_dev.map_phys_fmr = mthca_arbel_map_phys_fmr;
  1118. else
  1119. dev->ib_dev.map_phys_fmr = mthca_tavor_map_phys_fmr;
  1120. }
  1121. dev->ib_dev.attach_mcast = mthca_multicast_attach;
  1122. dev->ib_dev.detach_mcast = mthca_multicast_detach;
  1123. dev->ib_dev.process_mad = mthca_process_mad;
  1124. if (mthca_is_memfree(dev)) {
  1125. dev->ib_dev.req_notify_cq = mthca_arbel_arm_cq;
  1126. dev->ib_dev.post_send = mthca_arbel_post_send;
  1127. dev->ib_dev.post_recv = mthca_arbel_post_receive;
  1128. } else {
  1129. dev->ib_dev.req_notify_cq = mthca_tavor_arm_cq;
  1130. dev->ib_dev.post_send = mthca_tavor_post_send;
  1131. dev->ib_dev.post_recv = mthca_tavor_post_receive;
  1132. }
  1133. mutex_init(&dev->cap_mask_mutex);
  1134. ret = ib_register_device(&dev->ib_dev, NULL);
  1135. if (ret)
  1136. return ret;
  1137. for (i = 0; i < ARRAY_SIZE(mthca_dev_attributes); ++i) {
  1138. ret = device_create_file(&dev->ib_dev.dev,
  1139. mthca_dev_attributes[i]);
  1140. if (ret) {
  1141. ib_unregister_device(&dev->ib_dev);
  1142. return ret;
  1143. }
  1144. }
  1145. mthca_start_catas_poll(dev);
  1146. return 0;
  1147. }
  1148. void mthca_unregister_device(struct mthca_dev *dev)
  1149. {
  1150. mthca_stop_catas_poll(dev);
  1151. ib_unregister_device(&dev->ib_dev);
  1152. }