mthca_provider.c 35 KB

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