mthca_provider.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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 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 1397 2004-12-28 05:09:00Z roland $
  37. */
  38. #include <rdma/ib_smi.h>
  39. #include <linux/mm.h>
  40. #include "mthca_dev.h"
  41. #include "mthca_cmd.h"
  42. #include "mthca_user.h"
  43. #include "mthca_memfree.h"
  44. static int mthca_query_device(struct ib_device *ibdev,
  45. struct ib_device_attr *props)
  46. {
  47. struct ib_smp *in_mad = NULL;
  48. struct ib_smp *out_mad = NULL;
  49. int err = -ENOMEM;
  50. struct mthca_dev* mdev = to_mdev(ibdev);
  51. u8 status;
  52. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  53. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  54. if (!in_mad || !out_mad)
  55. goto out;
  56. memset(props, 0, sizeof *props);
  57. props->fw_ver = mdev->fw_ver;
  58. memset(in_mad, 0, sizeof *in_mad);
  59. in_mad->base_version = 1;
  60. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  61. in_mad->class_version = 1;
  62. in_mad->method = IB_MGMT_METHOD_GET;
  63. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  64. err = mthca_MAD_IFC(mdev, 1, 1,
  65. 1, NULL, NULL, in_mad, out_mad,
  66. &status);
  67. if (err)
  68. goto out;
  69. if (status) {
  70. err = -EINVAL;
  71. goto out;
  72. }
  73. props->device_cap_flags = mdev->device_cap_flags;
  74. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  75. 0xffffff;
  76. props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
  77. props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
  78. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  79. memcpy(&props->node_guid, out_mad->data + 12, 8);
  80. props->max_mr_size = ~0ull;
  81. props->max_qp = mdev->limits.num_qps - mdev->limits.reserved_qps;
  82. props->max_qp_wr = 0xffff;
  83. props->max_sge = mdev->limits.max_sg;
  84. props->max_cq = mdev->limits.num_cqs - mdev->limits.reserved_cqs;
  85. props->max_cqe = 0xffff;
  86. props->max_mr = mdev->limits.num_mpts - mdev->limits.reserved_mrws;
  87. props->max_pd = mdev->limits.num_pds - mdev->limits.reserved_pds;
  88. props->max_qp_rd_atom = 1 << mdev->qp_table.rdb_shift;
  89. props->max_qp_init_rd_atom = 1 << mdev->qp_table.rdb_shift;
  90. props->local_ca_ack_delay = mdev->limits.local_ca_ack_delay;
  91. props->atomic_cap = mdev->limits.flags & DEV_LIM_FLAG_ATOMIC ?
  92. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  93. err = 0;
  94. out:
  95. kfree(in_mad);
  96. kfree(out_mad);
  97. return err;
  98. }
  99. static int mthca_query_port(struct ib_device *ibdev,
  100. u8 port, struct ib_port_attr *props)
  101. {
  102. struct ib_smp *in_mad = NULL;
  103. struct ib_smp *out_mad = NULL;
  104. int err = -ENOMEM;
  105. u8 status;
  106. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  107. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  108. if (!in_mad || !out_mad)
  109. goto out;
  110. memset(props, 0, sizeof *props);
  111. memset(in_mad, 0, sizeof *in_mad);
  112. in_mad->base_version = 1;
  113. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  114. in_mad->class_version = 1;
  115. in_mad->method = IB_MGMT_METHOD_GET;
  116. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  117. in_mad->attr_mod = cpu_to_be32(port);
  118. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  119. port, NULL, NULL, in_mad, out_mad,
  120. &status);
  121. if (err)
  122. goto out;
  123. if (status) {
  124. err = -EINVAL;
  125. goto out;
  126. }
  127. props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
  128. props->lmc = out_mad->data[34] & 0x7;
  129. props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
  130. props->sm_sl = out_mad->data[36] & 0xf;
  131. props->state = out_mad->data[32] & 0xf;
  132. props->phys_state = out_mad->data[33] >> 4;
  133. props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
  134. props->gid_tbl_len = to_mdev(ibdev)->limits.gid_table_len;
  135. props->max_msg_sz = 0x80000000;
  136. props->pkey_tbl_len = to_mdev(ibdev)->limits.pkey_table_len;
  137. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  138. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  139. props->active_width = out_mad->data[31] & 0xf;
  140. props->active_speed = out_mad->data[35] >> 4;
  141. props->max_mtu = out_mad->data[41] & 0xf;
  142. props->active_mtu = out_mad->data[36] >> 4;
  143. props->subnet_timeout = out_mad->data[51] & 0x1f;
  144. out:
  145. kfree(in_mad);
  146. kfree(out_mad);
  147. return err;
  148. }
  149. static int mthca_modify_port(struct ib_device *ibdev,
  150. u8 port, int port_modify_mask,
  151. struct ib_port_modify *props)
  152. {
  153. struct mthca_set_ib_param set_ib;
  154. struct ib_port_attr attr;
  155. int err;
  156. u8 status;
  157. if (down_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
  158. return -ERESTARTSYS;
  159. err = mthca_query_port(ibdev, port, &attr);
  160. if (err)
  161. goto out;
  162. set_ib.set_si_guid = 0;
  163. set_ib.reset_qkey_viol = !!(port_modify_mask & IB_PORT_RESET_QKEY_CNTR);
  164. set_ib.cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  165. ~props->clr_port_cap_mask;
  166. err = mthca_SET_IB(to_mdev(ibdev), &set_ib, port, &status);
  167. if (err)
  168. goto out;
  169. if (status) {
  170. err = -EINVAL;
  171. goto out;
  172. }
  173. out:
  174. up(&to_mdev(ibdev)->cap_mask_mutex);
  175. return err;
  176. }
  177. static int mthca_query_pkey(struct ib_device *ibdev,
  178. u8 port, u16 index, u16 *pkey)
  179. {
  180. struct ib_smp *in_mad = NULL;
  181. struct ib_smp *out_mad = NULL;
  182. int err = -ENOMEM;
  183. u8 status;
  184. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  185. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  186. if (!in_mad || !out_mad)
  187. goto out;
  188. memset(in_mad, 0, sizeof *in_mad);
  189. in_mad->base_version = 1;
  190. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  191. in_mad->class_version = 1;
  192. in_mad->method = IB_MGMT_METHOD_GET;
  193. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  194. in_mad->attr_mod = cpu_to_be32(index / 32);
  195. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  196. port, NULL, NULL, in_mad, out_mad,
  197. &status);
  198. if (err)
  199. goto out;
  200. if (status) {
  201. err = -EINVAL;
  202. goto out;
  203. }
  204. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  205. out:
  206. kfree(in_mad);
  207. kfree(out_mad);
  208. return err;
  209. }
  210. static int mthca_query_gid(struct ib_device *ibdev, u8 port,
  211. int index, union ib_gid *gid)
  212. {
  213. struct ib_smp *in_mad = NULL;
  214. struct ib_smp *out_mad = NULL;
  215. int err = -ENOMEM;
  216. u8 status;
  217. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  218. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  219. if (!in_mad || !out_mad)
  220. goto out;
  221. memset(in_mad, 0, sizeof *in_mad);
  222. in_mad->base_version = 1;
  223. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  224. in_mad->class_version = 1;
  225. in_mad->method = IB_MGMT_METHOD_GET;
  226. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  227. in_mad->attr_mod = cpu_to_be32(port);
  228. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  229. port, NULL, NULL, in_mad, out_mad,
  230. &status);
  231. if (err)
  232. goto out;
  233. if (status) {
  234. err = -EINVAL;
  235. goto out;
  236. }
  237. memcpy(gid->raw, out_mad->data + 8, 8);
  238. memset(in_mad, 0, sizeof *in_mad);
  239. in_mad->base_version = 1;
  240. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  241. in_mad->class_version = 1;
  242. in_mad->method = IB_MGMT_METHOD_GET;
  243. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  244. in_mad->attr_mod = cpu_to_be32(index / 8);
  245. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  246. port, NULL, NULL, in_mad, out_mad,
  247. &status);
  248. if (err)
  249. goto out;
  250. if (status) {
  251. err = -EINVAL;
  252. goto out;
  253. }
  254. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 16, 8);
  255. out:
  256. kfree(in_mad);
  257. kfree(out_mad);
  258. return err;
  259. }
  260. static struct ib_ucontext *mthca_alloc_ucontext(struct ib_device *ibdev,
  261. struct ib_udata *udata)
  262. {
  263. struct mthca_alloc_ucontext_resp uresp;
  264. struct mthca_ucontext *context;
  265. int err;
  266. memset(&uresp, 0, sizeof uresp);
  267. uresp.qp_tab_size = to_mdev(ibdev)->limits.num_qps;
  268. if (mthca_is_memfree(to_mdev(ibdev)))
  269. uresp.uarc_size = to_mdev(ibdev)->uar_table.uarc_size;
  270. else
  271. uresp.uarc_size = 0;
  272. context = kmalloc(sizeof *context, GFP_KERNEL);
  273. if (!context)
  274. return ERR_PTR(-ENOMEM);
  275. err = mthca_uar_alloc(to_mdev(ibdev), &context->uar);
  276. if (err) {
  277. kfree(context);
  278. return ERR_PTR(err);
  279. }
  280. context->db_tab = mthca_init_user_db_tab(to_mdev(ibdev));
  281. if (IS_ERR(context->db_tab)) {
  282. err = PTR_ERR(context->db_tab);
  283. mthca_uar_free(to_mdev(ibdev), &context->uar);
  284. kfree(context);
  285. return ERR_PTR(err);
  286. }
  287. if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
  288. mthca_cleanup_user_db_tab(to_mdev(ibdev), &context->uar, context->db_tab);
  289. mthca_uar_free(to_mdev(ibdev), &context->uar);
  290. kfree(context);
  291. return ERR_PTR(-EFAULT);
  292. }
  293. return &context->ibucontext;
  294. }
  295. static int mthca_dealloc_ucontext(struct ib_ucontext *context)
  296. {
  297. mthca_cleanup_user_db_tab(to_mdev(context->device), &to_mucontext(context)->uar,
  298. to_mucontext(context)->db_tab);
  299. mthca_uar_free(to_mdev(context->device), &to_mucontext(context)->uar);
  300. kfree(to_mucontext(context));
  301. return 0;
  302. }
  303. static int mthca_mmap_uar(struct ib_ucontext *context,
  304. struct vm_area_struct *vma)
  305. {
  306. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  307. return -EINVAL;
  308. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  309. if (io_remap_pfn_range(vma, vma->vm_start,
  310. to_mucontext(context)->uar.pfn,
  311. PAGE_SIZE, vma->vm_page_prot))
  312. return -EAGAIN;
  313. return 0;
  314. }
  315. static struct ib_pd *mthca_alloc_pd(struct ib_device *ibdev,
  316. struct ib_ucontext *context,
  317. struct ib_udata *udata)
  318. {
  319. struct mthca_pd *pd;
  320. int err;
  321. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  322. if (!pd)
  323. return ERR_PTR(-ENOMEM);
  324. err = mthca_pd_alloc(to_mdev(ibdev), !context, pd);
  325. if (err) {
  326. kfree(pd);
  327. return ERR_PTR(err);
  328. }
  329. if (context) {
  330. if (ib_copy_to_udata(udata, &pd->pd_num, sizeof (__u32))) {
  331. mthca_pd_free(to_mdev(ibdev), pd);
  332. kfree(pd);
  333. return ERR_PTR(-EFAULT);
  334. }
  335. }
  336. return &pd->ibpd;
  337. }
  338. static int mthca_dealloc_pd(struct ib_pd *pd)
  339. {
  340. mthca_pd_free(to_mdev(pd->device), to_mpd(pd));
  341. kfree(pd);
  342. return 0;
  343. }
  344. static struct ib_ah *mthca_ah_create(struct ib_pd *pd,
  345. struct ib_ah_attr *ah_attr)
  346. {
  347. int err;
  348. struct mthca_ah *ah;
  349. ah = kmalloc(sizeof *ah, GFP_ATOMIC);
  350. if (!ah)
  351. return ERR_PTR(-ENOMEM);
  352. err = mthca_create_ah(to_mdev(pd->device), to_mpd(pd), ah_attr, ah);
  353. if (err) {
  354. kfree(ah);
  355. return ERR_PTR(err);
  356. }
  357. return &ah->ibah;
  358. }
  359. static int mthca_ah_destroy(struct ib_ah *ah)
  360. {
  361. mthca_destroy_ah(to_mdev(ah->device), to_mah(ah));
  362. kfree(ah);
  363. return 0;
  364. }
  365. static struct ib_srq *mthca_create_srq(struct ib_pd *pd,
  366. struct ib_srq_init_attr *init_attr,
  367. struct ib_udata *udata)
  368. {
  369. struct mthca_create_srq ucmd;
  370. struct mthca_ucontext *context = NULL;
  371. struct mthca_srq *srq;
  372. int err;
  373. srq = kmalloc(sizeof *srq, GFP_KERNEL);
  374. if (!srq)
  375. return ERR_PTR(-ENOMEM);
  376. if (pd->uobject) {
  377. context = to_mucontext(pd->uobject->context);
  378. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  379. return ERR_PTR(-EFAULT);
  380. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  381. context->db_tab, ucmd.db_index,
  382. ucmd.db_page);
  383. if (err)
  384. goto err_free;
  385. srq->mr.ibmr.lkey = ucmd.lkey;
  386. srq->db_index = ucmd.db_index;
  387. }
  388. err = mthca_alloc_srq(to_mdev(pd->device), to_mpd(pd),
  389. &init_attr->attr, srq);
  390. if (err && pd->uobject)
  391. mthca_unmap_user_db(to_mdev(pd->device), &context->uar,
  392. context->db_tab, ucmd.db_index);
  393. if (err)
  394. goto err_free;
  395. if (context && ib_copy_to_udata(udata, &srq->srqn, sizeof (__u32))) {
  396. mthca_free_srq(to_mdev(pd->device), srq);
  397. err = -EFAULT;
  398. goto err_free;
  399. }
  400. return &srq->ibsrq;
  401. err_free:
  402. kfree(srq);
  403. return ERR_PTR(err);
  404. }
  405. static int mthca_destroy_srq(struct ib_srq *srq)
  406. {
  407. struct mthca_ucontext *context;
  408. if (srq->uobject) {
  409. context = to_mucontext(srq->uobject->context);
  410. mthca_unmap_user_db(to_mdev(srq->device), &context->uar,
  411. context->db_tab, to_msrq(srq)->db_index);
  412. }
  413. mthca_free_srq(to_mdev(srq->device), to_msrq(srq));
  414. kfree(srq);
  415. return 0;
  416. }
  417. static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
  418. struct ib_qp_init_attr *init_attr,
  419. struct ib_udata *udata)
  420. {
  421. struct mthca_create_qp ucmd;
  422. struct mthca_qp *qp;
  423. int err;
  424. switch (init_attr->qp_type) {
  425. case IB_QPT_RC:
  426. case IB_QPT_UC:
  427. case IB_QPT_UD:
  428. {
  429. struct mthca_ucontext *context;
  430. qp = kmalloc(sizeof *qp, GFP_KERNEL);
  431. if (!qp)
  432. return ERR_PTR(-ENOMEM);
  433. if (pd->uobject) {
  434. context = to_mucontext(pd->uobject->context);
  435. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  436. return ERR_PTR(-EFAULT);
  437. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  438. context->db_tab,
  439. ucmd.sq_db_index, ucmd.sq_db_page);
  440. if (err) {
  441. kfree(qp);
  442. return ERR_PTR(err);
  443. }
  444. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  445. context->db_tab,
  446. ucmd.rq_db_index, ucmd.rq_db_page);
  447. if (err) {
  448. mthca_unmap_user_db(to_mdev(pd->device),
  449. &context->uar,
  450. context->db_tab,
  451. ucmd.sq_db_index);
  452. kfree(qp);
  453. return ERR_PTR(err);
  454. }
  455. qp->mr.ibmr.lkey = ucmd.lkey;
  456. qp->sq.db_index = ucmd.sq_db_index;
  457. qp->rq.db_index = ucmd.rq_db_index;
  458. }
  459. err = mthca_alloc_qp(to_mdev(pd->device), to_mpd(pd),
  460. to_mcq(init_attr->send_cq),
  461. to_mcq(init_attr->recv_cq),
  462. init_attr->qp_type, init_attr->sq_sig_type,
  463. &init_attr->cap, qp);
  464. if (err && pd->uobject) {
  465. context = to_mucontext(pd->uobject->context);
  466. mthca_unmap_user_db(to_mdev(pd->device),
  467. &context->uar,
  468. context->db_tab,
  469. ucmd.sq_db_index);
  470. mthca_unmap_user_db(to_mdev(pd->device),
  471. &context->uar,
  472. context->db_tab,
  473. ucmd.rq_db_index);
  474. }
  475. qp->ibqp.qp_num = qp->qpn;
  476. break;
  477. }
  478. case IB_QPT_SMI:
  479. case IB_QPT_GSI:
  480. {
  481. /* Don't allow userspace to create special QPs */
  482. if (pd->uobject)
  483. return ERR_PTR(-EINVAL);
  484. qp = kmalloc(sizeof (struct mthca_sqp), GFP_KERNEL);
  485. if (!qp)
  486. return ERR_PTR(-ENOMEM);
  487. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  488. err = mthca_alloc_sqp(to_mdev(pd->device), to_mpd(pd),
  489. to_mcq(init_attr->send_cq),
  490. to_mcq(init_attr->recv_cq),
  491. init_attr->sq_sig_type, &init_attr->cap,
  492. qp->ibqp.qp_num, init_attr->port_num,
  493. to_msqp(qp));
  494. break;
  495. }
  496. default:
  497. /* Don't support raw QPs */
  498. return ERR_PTR(-ENOSYS);
  499. }
  500. if (err) {
  501. kfree(qp);
  502. return ERR_PTR(err);
  503. }
  504. init_attr->cap.max_inline_data = 0;
  505. init_attr->cap.max_send_wr = qp->sq.max;
  506. init_attr->cap.max_recv_wr = qp->rq.max;
  507. init_attr->cap.max_send_sge = qp->sq.max_gs;
  508. init_attr->cap.max_recv_sge = qp->rq.max_gs;
  509. return &qp->ibqp;
  510. }
  511. static int mthca_destroy_qp(struct ib_qp *qp)
  512. {
  513. if (qp->uobject) {
  514. mthca_unmap_user_db(to_mdev(qp->device),
  515. &to_mucontext(qp->uobject->context)->uar,
  516. to_mucontext(qp->uobject->context)->db_tab,
  517. to_mqp(qp)->sq.db_index);
  518. mthca_unmap_user_db(to_mdev(qp->device),
  519. &to_mucontext(qp->uobject->context)->uar,
  520. to_mucontext(qp->uobject->context)->db_tab,
  521. to_mqp(qp)->rq.db_index);
  522. }
  523. mthca_free_qp(to_mdev(qp->device), to_mqp(qp));
  524. kfree(qp);
  525. return 0;
  526. }
  527. static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
  528. struct ib_ucontext *context,
  529. struct ib_udata *udata)
  530. {
  531. struct mthca_create_cq ucmd;
  532. struct mthca_cq *cq;
  533. int nent;
  534. int err;
  535. if (context) {
  536. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  537. return ERR_PTR(-EFAULT);
  538. err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  539. to_mucontext(context)->db_tab,
  540. ucmd.set_db_index, ucmd.set_db_page);
  541. if (err)
  542. return ERR_PTR(err);
  543. err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  544. to_mucontext(context)->db_tab,
  545. ucmd.arm_db_index, ucmd.arm_db_page);
  546. if (err)
  547. goto err_unmap_set;
  548. }
  549. cq = kmalloc(sizeof *cq, GFP_KERNEL);
  550. if (!cq) {
  551. err = -ENOMEM;
  552. goto err_unmap_arm;
  553. }
  554. if (context) {
  555. cq->mr.ibmr.lkey = ucmd.lkey;
  556. cq->set_ci_db_index = ucmd.set_db_index;
  557. cq->arm_db_index = ucmd.arm_db_index;
  558. }
  559. for (nent = 1; nent <= entries; nent <<= 1)
  560. ; /* nothing */
  561. err = mthca_init_cq(to_mdev(ibdev), nent,
  562. context ? to_mucontext(context) : NULL,
  563. context ? ucmd.pdn : to_mdev(ibdev)->driver_pd.pd_num,
  564. cq);
  565. if (err)
  566. goto err_free;
  567. if (context && ib_copy_to_udata(udata, &cq->cqn, sizeof (__u32))) {
  568. mthca_free_cq(to_mdev(ibdev), cq);
  569. goto err_free;
  570. }
  571. return &cq->ibcq;
  572. err_free:
  573. kfree(cq);
  574. err_unmap_arm:
  575. if (context)
  576. mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  577. to_mucontext(context)->db_tab, ucmd.arm_db_index);
  578. err_unmap_set:
  579. if (context)
  580. mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  581. to_mucontext(context)->db_tab, ucmd.set_db_index);
  582. return ERR_PTR(err);
  583. }
  584. static int mthca_destroy_cq(struct ib_cq *cq)
  585. {
  586. if (cq->uobject) {
  587. mthca_unmap_user_db(to_mdev(cq->device),
  588. &to_mucontext(cq->uobject->context)->uar,
  589. to_mucontext(cq->uobject->context)->db_tab,
  590. to_mcq(cq)->arm_db_index);
  591. mthca_unmap_user_db(to_mdev(cq->device),
  592. &to_mucontext(cq->uobject->context)->uar,
  593. to_mucontext(cq->uobject->context)->db_tab,
  594. to_mcq(cq)->set_ci_db_index);
  595. }
  596. mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
  597. kfree(cq);
  598. return 0;
  599. }
  600. static inline u32 convert_access(int acc)
  601. {
  602. return (acc & IB_ACCESS_REMOTE_ATOMIC ? MTHCA_MPT_FLAG_ATOMIC : 0) |
  603. (acc & IB_ACCESS_REMOTE_WRITE ? MTHCA_MPT_FLAG_REMOTE_WRITE : 0) |
  604. (acc & IB_ACCESS_REMOTE_READ ? MTHCA_MPT_FLAG_REMOTE_READ : 0) |
  605. (acc & IB_ACCESS_LOCAL_WRITE ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0) |
  606. MTHCA_MPT_FLAG_LOCAL_READ;
  607. }
  608. static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
  609. {
  610. struct mthca_mr *mr;
  611. int err;
  612. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  613. if (!mr)
  614. return ERR_PTR(-ENOMEM);
  615. err = mthca_mr_alloc_notrans(to_mdev(pd->device),
  616. to_mpd(pd)->pd_num,
  617. convert_access(acc), mr);
  618. if (err) {
  619. kfree(mr);
  620. return ERR_PTR(err);
  621. }
  622. return &mr->ibmr;
  623. }
  624. static struct ib_mr *mthca_reg_phys_mr(struct ib_pd *pd,
  625. struct ib_phys_buf *buffer_list,
  626. int num_phys_buf,
  627. int acc,
  628. u64 *iova_start)
  629. {
  630. struct mthca_mr *mr;
  631. u64 *page_list;
  632. u64 total_size;
  633. u64 mask;
  634. int shift;
  635. int npages;
  636. int err;
  637. int i, j, n;
  638. /* First check that we have enough alignment */
  639. if ((*iova_start & ~PAGE_MASK) != (buffer_list[0].addr & ~PAGE_MASK))
  640. return ERR_PTR(-EINVAL);
  641. if (num_phys_buf > 1 &&
  642. ((buffer_list[0].addr + buffer_list[0].size) & ~PAGE_MASK))
  643. return ERR_PTR(-EINVAL);
  644. mask = 0;
  645. total_size = 0;
  646. for (i = 0; i < num_phys_buf; ++i) {
  647. if (i != 0 && buffer_list[i].addr & ~PAGE_MASK)
  648. return ERR_PTR(-EINVAL);
  649. if (i != 0 && i != num_phys_buf - 1 &&
  650. (buffer_list[i].size & ~PAGE_MASK))
  651. return ERR_PTR(-EINVAL);
  652. total_size += buffer_list[i].size;
  653. if (i > 0)
  654. mask |= buffer_list[i].addr;
  655. }
  656. /* Find largest page shift we can use to cover buffers */
  657. for (shift = PAGE_SHIFT; shift < 31; ++shift)
  658. if (num_phys_buf > 1) {
  659. if ((1ULL << shift) & mask)
  660. break;
  661. } else {
  662. if (1ULL << shift >=
  663. buffer_list[0].size +
  664. (buffer_list[0].addr & ((1ULL << shift) - 1)))
  665. break;
  666. }
  667. buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
  668. buffer_list[0].addr &= ~0ull << shift;
  669. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  670. if (!mr)
  671. return ERR_PTR(-ENOMEM);
  672. npages = 0;
  673. for (i = 0; i < num_phys_buf; ++i)
  674. npages += (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  675. if (!npages)
  676. return &mr->ibmr;
  677. page_list = kmalloc(npages * sizeof *page_list, GFP_KERNEL);
  678. if (!page_list) {
  679. kfree(mr);
  680. return ERR_PTR(-ENOMEM);
  681. }
  682. n = 0;
  683. for (i = 0; i < num_phys_buf; ++i)
  684. for (j = 0;
  685. j < (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  686. ++j)
  687. page_list[n++] = buffer_list[i].addr + ((u64) j << shift);
  688. mthca_dbg(to_mdev(pd->device), "Registering memory at %llx (iova %llx) "
  689. "in PD %x; shift %d, npages %d.\n",
  690. (unsigned long long) buffer_list[0].addr,
  691. (unsigned long long) *iova_start,
  692. to_mpd(pd)->pd_num,
  693. shift, npages);
  694. err = mthca_mr_alloc_phys(to_mdev(pd->device),
  695. to_mpd(pd)->pd_num,
  696. page_list, shift, npages,
  697. *iova_start, total_size,
  698. convert_access(acc), mr);
  699. if (err) {
  700. kfree(page_list);
  701. kfree(mr);
  702. return ERR_PTR(err);
  703. }
  704. kfree(page_list);
  705. return &mr->ibmr;
  706. }
  707. static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
  708. int acc, struct ib_udata *udata)
  709. {
  710. struct mthca_dev *dev = to_mdev(pd->device);
  711. struct ib_umem_chunk *chunk;
  712. struct mthca_mr *mr;
  713. u64 *pages;
  714. int shift, n, len;
  715. int i, j, k;
  716. int err = 0;
  717. shift = ffs(region->page_size) - 1;
  718. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  719. if (!mr)
  720. return ERR_PTR(-ENOMEM);
  721. n = 0;
  722. list_for_each_entry(chunk, &region->chunk_list, list)
  723. n += chunk->nents;
  724. mr->mtt = mthca_alloc_mtt(dev, n);
  725. if (IS_ERR(mr->mtt)) {
  726. err = PTR_ERR(mr->mtt);
  727. goto err;
  728. }
  729. pages = (u64 *) __get_free_page(GFP_KERNEL);
  730. if (!pages) {
  731. err = -ENOMEM;
  732. goto err_mtt;
  733. }
  734. i = n = 0;
  735. list_for_each_entry(chunk, &region->chunk_list, list)
  736. for (j = 0; j < chunk->nmap; ++j) {
  737. len = sg_dma_len(&chunk->page_list[j]) >> shift;
  738. for (k = 0; k < len; ++k) {
  739. pages[i++] = sg_dma_address(&chunk->page_list[j]) +
  740. region->page_size * k;
  741. /*
  742. * Be friendly to WRITE_MTT command
  743. * and leave two empty slots for the
  744. * index and reserved fields of the
  745. * mailbox.
  746. */
  747. if (i == PAGE_SIZE / sizeof (u64) - 2) {
  748. err = mthca_write_mtt(dev, mr->mtt,
  749. n, pages, i);
  750. if (err)
  751. goto mtt_done;
  752. n += i;
  753. i = 0;
  754. }
  755. }
  756. }
  757. if (i)
  758. err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
  759. mtt_done:
  760. free_page((unsigned long) pages);
  761. if (err)
  762. goto err_mtt;
  763. err = mthca_mr_alloc(dev, to_mpd(pd)->pd_num, shift, region->virt_base,
  764. region->length, convert_access(acc), mr);
  765. if (err)
  766. goto err_mtt;
  767. return &mr->ibmr;
  768. err_mtt:
  769. mthca_free_mtt(dev, mr->mtt);
  770. err:
  771. kfree(mr);
  772. return ERR_PTR(err);
  773. }
  774. static int mthca_dereg_mr(struct ib_mr *mr)
  775. {
  776. struct mthca_mr *mmr = to_mmr(mr);
  777. mthca_free_mr(to_mdev(mr->device), mmr);
  778. kfree(mmr);
  779. return 0;
  780. }
  781. static struct ib_fmr *mthca_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
  782. struct ib_fmr_attr *fmr_attr)
  783. {
  784. struct mthca_fmr *fmr;
  785. int err;
  786. fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
  787. if (!fmr)
  788. return ERR_PTR(-ENOMEM);
  789. memcpy(&fmr->attr, fmr_attr, sizeof *fmr_attr);
  790. err = mthca_fmr_alloc(to_mdev(pd->device), to_mpd(pd)->pd_num,
  791. convert_access(mr_access_flags), fmr);
  792. if (err) {
  793. kfree(fmr);
  794. return ERR_PTR(err);
  795. }
  796. return &fmr->ibmr;
  797. }
  798. static int mthca_dealloc_fmr(struct ib_fmr *fmr)
  799. {
  800. struct mthca_fmr *mfmr = to_mfmr(fmr);
  801. int err;
  802. err = mthca_free_fmr(to_mdev(fmr->device), mfmr);
  803. if (err)
  804. return err;
  805. kfree(mfmr);
  806. return 0;
  807. }
  808. static int mthca_unmap_fmr(struct list_head *fmr_list)
  809. {
  810. struct ib_fmr *fmr;
  811. int err;
  812. u8 status;
  813. struct mthca_dev *mdev = NULL;
  814. list_for_each_entry(fmr, fmr_list, list) {
  815. if (mdev && to_mdev(fmr->device) != mdev)
  816. return -EINVAL;
  817. mdev = to_mdev(fmr->device);
  818. }
  819. if (!mdev)
  820. return 0;
  821. if (mthca_is_memfree(mdev)) {
  822. list_for_each_entry(fmr, fmr_list, list)
  823. mthca_arbel_fmr_unmap(mdev, to_mfmr(fmr));
  824. wmb();
  825. } else
  826. list_for_each_entry(fmr, fmr_list, list)
  827. mthca_tavor_fmr_unmap(mdev, to_mfmr(fmr));
  828. err = mthca_SYNC_TPT(mdev, &status);
  829. if (err)
  830. return err;
  831. if (status)
  832. return -EINVAL;
  833. return 0;
  834. }
  835. static ssize_t show_rev(struct class_device *cdev, char *buf)
  836. {
  837. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  838. return sprintf(buf, "%x\n", dev->rev_id);
  839. }
  840. static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
  841. {
  842. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  843. return sprintf(buf, "%x.%x.%x\n", (int) (dev->fw_ver >> 32),
  844. (int) (dev->fw_ver >> 16) & 0xffff,
  845. (int) dev->fw_ver & 0xffff);
  846. }
  847. static ssize_t show_hca(struct class_device *cdev, char *buf)
  848. {
  849. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  850. switch (dev->pdev->device) {
  851. case PCI_DEVICE_ID_MELLANOX_TAVOR:
  852. return sprintf(buf, "MT23108\n");
  853. case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
  854. return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
  855. case PCI_DEVICE_ID_MELLANOX_ARBEL:
  856. return sprintf(buf, "MT25208\n");
  857. case PCI_DEVICE_ID_MELLANOX_SINAI:
  858. case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
  859. return sprintf(buf, "MT25204\n");
  860. default:
  861. return sprintf(buf, "unknown\n");
  862. }
  863. }
  864. static ssize_t show_board(struct class_device *cdev, char *buf)
  865. {
  866. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  867. return sprintf(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
  868. }
  869. static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  870. static CLASS_DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  871. static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  872. static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  873. static struct class_device_attribute *mthca_class_attributes[] = {
  874. &class_device_attr_hw_rev,
  875. &class_device_attr_fw_ver,
  876. &class_device_attr_hca_type,
  877. &class_device_attr_board_id
  878. };
  879. int mthca_register_device(struct mthca_dev *dev)
  880. {
  881. int ret;
  882. int i;
  883. strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
  884. dev->ib_dev.owner = THIS_MODULE;
  885. dev->ib_dev.uverbs_abi_ver = MTHCA_UVERBS_ABI_VERSION;
  886. dev->ib_dev.node_type = IB_NODE_CA;
  887. dev->ib_dev.phys_port_cnt = dev->limits.num_ports;
  888. dev->ib_dev.dma_device = &dev->pdev->dev;
  889. dev->ib_dev.class_dev.dev = &dev->pdev->dev;
  890. dev->ib_dev.query_device = mthca_query_device;
  891. dev->ib_dev.query_port = mthca_query_port;
  892. dev->ib_dev.modify_port = mthca_modify_port;
  893. dev->ib_dev.query_pkey = mthca_query_pkey;
  894. dev->ib_dev.query_gid = mthca_query_gid;
  895. dev->ib_dev.alloc_ucontext = mthca_alloc_ucontext;
  896. dev->ib_dev.dealloc_ucontext = mthca_dealloc_ucontext;
  897. dev->ib_dev.mmap = mthca_mmap_uar;
  898. dev->ib_dev.alloc_pd = mthca_alloc_pd;
  899. dev->ib_dev.dealloc_pd = mthca_dealloc_pd;
  900. dev->ib_dev.create_ah = mthca_ah_create;
  901. dev->ib_dev.destroy_ah = mthca_ah_destroy;
  902. if (dev->mthca_flags & MTHCA_FLAG_SRQ) {
  903. dev->ib_dev.create_srq = mthca_create_srq;
  904. dev->ib_dev.modify_srq = mthca_modify_srq;
  905. dev->ib_dev.destroy_srq = mthca_destroy_srq;
  906. if (mthca_is_memfree(dev))
  907. dev->ib_dev.post_srq_recv = mthca_arbel_post_srq_recv;
  908. else
  909. dev->ib_dev.post_srq_recv = mthca_tavor_post_srq_recv;
  910. }
  911. dev->ib_dev.create_qp = mthca_create_qp;
  912. dev->ib_dev.modify_qp = mthca_modify_qp;
  913. dev->ib_dev.destroy_qp = mthca_destroy_qp;
  914. dev->ib_dev.create_cq = mthca_create_cq;
  915. dev->ib_dev.destroy_cq = mthca_destroy_cq;
  916. dev->ib_dev.poll_cq = mthca_poll_cq;
  917. dev->ib_dev.get_dma_mr = mthca_get_dma_mr;
  918. dev->ib_dev.reg_phys_mr = mthca_reg_phys_mr;
  919. dev->ib_dev.reg_user_mr = mthca_reg_user_mr;
  920. dev->ib_dev.dereg_mr = mthca_dereg_mr;
  921. if (dev->mthca_flags & MTHCA_FLAG_FMR) {
  922. dev->ib_dev.alloc_fmr = mthca_alloc_fmr;
  923. dev->ib_dev.unmap_fmr = mthca_unmap_fmr;
  924. dev->ib_dev.dealloc_fmr = mthca_dealloc_fmr;
  925. if (mthca_is_memfree(dev))
  926. dev->ib_dev.map_phys_fmr = mthca_arbel_map_phys_fmr;
  927. else
  928. dev->ib_dev.map_phys_fmr = mthca_tavor_map_phys_fmr;
  929. }
  930. dev->ib_dev.attach_mcast = mthca_multicast_attach;
  931. dev->ib_dev.detach_mcast = mthca_multicast_detach;
  932. dev->ib_dev.process_mad = mthca_process_mad;
  933. if (mthca_is_memfree(dev)) {
  934. dev->ib_dev.req_notify_cq = mthca_arbel_arm_cq;
  935. dev->ib_dev.post_send = mthca_arbel_post_send;
  936. dev->ib_dev.post_recv = mthca_arbel_post_receive;
  937. } else {
  938. dev->ib_dev.req_notify_cq = mthca_tavor_arm_cq;
  939. dev->ib_dev.post_send = mthca_tavor_post_send;
  940. dev->ib_dev.post_recv = mthca_tavor_post_receive;
  941. }
  942. init_MUTEX(&dev->cap_mask_mutex);
  943. ret = ib_register_device(&dev->ib_dev);
  944. if (ret)
  945. return ret;
  946. for (i = 0; i < ARRAY_SIZE(mthca_class_attributes); ++i) {
  947. ret = class_device_create_file(&dev->ib_dev.class_dev,
  948. mthca_class_attributes[i]);
  949. if (ret) {
  950. ib_unregister_device(&dev->ib_dev);
  951. return ret;
  952. }
  953. }
  954. return 0;
  955. }
  956. void mthca_unregister_device(struct mthca_dev *dev)
  957. {
  958. ib_unregister_device(&dev->ib_dev);
  959. }