mthca_provider.c 34 KB

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