mthca_provider.c 36 KB

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