mthca_provider.c 35 KB

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