mthca_provider.c 35 KB

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