mthca_provider.c 34 KB

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