mthca_provider.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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_sg;
  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_port(struct ib_device *ibdev,
  159. u8 port, int port_modify_mask,
  160. struct ib_port_modify *props)
  161. {
  162. struct mthca_set_ib_param set_ib;
  163. struct ib_port_attr attr;
  164. int err;
  165. u8 status;
  166. if (mutex_lock_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
  167. return -ERESTARTSYS;
  168. err = mthca_query_port(ibdev, port, &attr);
  169. if (err)
  170. goto out;
  171. set_ib.set_si_guid = 0;
  172. set_ib.reset_qkey_viol = !!(port_modify_mask & IB_PORT_RESET_QKEY_CNTR);
  173. set_ib.cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  174. ~props->clr_port_cap_mask;
  175. err = mthca_SET_IB(to_mdev(ibdev), &set_ib, port, &status);
  176. if (err)
  177. goto out;
  178. if (status) {
  179. err = -EINVAL;
  180. goto out;
  181. }
  182. out:
  183. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  184. return err;
  185. }
  186. static int mthca_query_pkey(struct ib_device *ibdev,
  187. u8 port, u16 index, u16 *pkey)
  188. {
  189. struct ib_smp *in_mad = NULL;
  190. struct ib_smp *out_mad = NULL;
  191. int err = -ENOMEM;
  192. u8 status;
  193. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  194. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  195. if (!in_mad || !out_mad)
  196. goto out;
  197. init_query_mad(in_mad);
  198. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  199. in_mad->attr_mod = cpu_to_be32(index / 32);
  200. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  201. port, NULL, NULL, in_mad, out_mad,
  202. &status);
  203. if (err)
  204. goto out;
  205. if (status) {
  206. err = -EINVAL;
  207. goto out;
  208. }
  209. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  210. out:
  211. kfree(in_mad);
  212. kfree(out_mad);
  213. return err;
  214. }
  215. static int mthca_query_gid(struct ib_device *ibdev, u8 port,
  216. int index, union ib_gid *gid)
  217. {
  218. struct ib_smp *in_mad = NULL;
  219. struct ib_smp *out_mad = NULL;
  220. int err = -ENOMEM;
  221. u8 status;
  222. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  223. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  224. if (!in_mad || !out_mad)
  225. goto out;
  226. init_query_mad(in_mad);
  227. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  228. in_mad->attr_mod = cpu_to_be32(port);
  229. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  230. port, NULL, NULL, in_mad, out_mad,
  231. &status);
  232. if (err)
  233. goto out;
  234. if (status) {
  235. err = -EINVAL;
  236. goto out;
  237. }
  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. &status);
  245. if (err)
  246. goto out;
  247. if (status) {
  248. err = -EINVAL;
  249. goto out;
  250. }
  251. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 16, 8);
  252. out:
  253. kfree(in_mad);
  254. kfree(out_mad);
  255. return err;
  256. }
  257. static struct ib_ucontext *mthca_alloc_ucontext(struct ib_device *ibdev,
  258. struct ib_udata *udata)
  259. {
  260. struct mthca_alloc_ucontext_resp uresp;
  261. struct mthca_ucontext *context;
  262. int err;
  263. memset(&uresp, 0, sizeof uresp);
  264. uresp.qp_tab_size = to_mdev(ibdev)->limits.num_qps;
  265. if (mthca_is_memfree(to_mdev(ibdev)))
  266. uresp.uarc_size = to_mdev(ibdev)->uar_table.uarc_size;
  267. else
  268. uresp.uarc_size = 0;
  269. context = kmalloc(sizeof *context, GFP_KERNEL);
  270. if (!context)
  271. return ERR_PTR(-ENOMEM);
  272. err = mthca_uar_alloc(to_mdev(ibdev), &context->uar);
  273. if (err) {
  274. kfree(context);
  275. return ERR_PTR(err);
  276. }
  277. context->db_tab = mthca_init_user_db_tab(to_mdev(ibdev));
  278. if (IS_ERR(context->db_tab)) {
  279. err = PTR_ERR(context->db_tab);
  280. mthca_uar_free(to_mdev(ibdev), &context->uar);
  281. kfree(context);
  282. return ERR_PTR(err);
  283. }
  284. if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
  285. mthca_cleanup_user_db_tab(to_mdev(ibdev), &context->uar, context->db_tab);
  286. mthca_uar_free(to_mdev(ibdev), &context->uar);
  287. kfree(context);
  288. return ERR_PTR(-EFAULT);
  289. }
  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. 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. switch (init_attr->qp_type) {
  424. case IB_QPT_RC:
  425. case IB_QPT_UC:
  426. case IB_QPT_UD:
  427. {
  428. struct mthca_ucontext *context;
  429. qp = kmalloc(sizeof *qp, GFP_KERNEL);
  430. if (!qp)
  431. return ERR_PTR(-ENOMEM);
  432. if (pd->uobject) {
  433. context = to_mucontext(pd->uobject->context);
  434. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  435. kfree(qp);
  436. return ERR_PTR(-EFAULT);
  437. }
  438. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  439. context->db_tab,
  440. ucmd.sq_db_index, ucmd.sq_db_page);
  441. if (err) {
  442. kfree(qp);
  443. return ERR_PTR(err);
  444. }
  445. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  446. context->db_tab,
  447. ucmd.rq_db_index, ucmd.rq_db_page);
  448. if (err) {
  449. mthca_unmap_user_db(to_mdev(pd->device),
  450. &context->uar,
  451. context->db_tab,
  452. ucmd.sq_db_index);
  453. kfree(qp);
  454. return ERR_PTR(err);
  455. }
  456. qp->mr.ibmr.lkey = ucmd.lkey;
  457. qp->sq.db_index = ucmd.sq_db_index;
  458. qp->rq.db_index = ucmd.rq_db_index;
  459. }
  460. err = mthca_alloc_qp(to_mdev(pd->device), to_mpd(pd),
  461. to_mcq(init_attr->send_cq),
  462. to_mcq(init_attr->recv_cq),
  463. init_attr->qp_type, init_attr->sq_sig_type,
  464. &init_attr->cap, qp);
  465. if (err && pd->uobject) {
  466. context = to_mucontext(pd->uobject->context);
  467. mthca_unmap_user_db(to_mdev(pd->device),
  468. &context->uar,
  469. context->db_tab,
  470. ucmd.sq_db_index);
  471. mthca_unmap_user_db(to_mdev(pd->device),
  472. &context->uar,
  473. context->db_tab,
  474. ucmd.rq_db_index);
  475. }
  476. qp->ibqp.qp_num = qp->qpn;
  477. break;
  478. }
  479. case IB_QPT_SMI:
  480. case IB_QPT_GSI:
  481. {
  482. /* Don't allow userspace to create special QPs */
  483. if (pd->uobject)
  484. return ERR_PTR(-EINVAL);
  485. qp = kmalloc(sizeof (struct mthca_sqp), GFP_KERNEL);
  486. if (!qp)
  487. return ERR_PTR(-ENOMEM);
  488. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  489. err = mthca_alloc_sqp(to_mdev(pd->device), to_mpd(pd),
  490. to_mcq(init_attr->send_cq),
  491. to_mcq(init_attr->recv_cq),
  492. init_attr->sq_sig_type, &init_attr->cap,
  493. qp->ibqp.qp_num, init_attr->port_num,
  494. to_msqp(qp));
  495. break;
  496. }
  497. default:
  498. /* Don't support raw QPs */
  499. return ERR_PTR(-ENOSYS);
  500. }
  501. if (err) {
  502. kfree(qp);
  503. return ERR_PTR(err);
  504. }
  505. init_attr->cap.max_send_wr = qp->sq.max;
  506. init_attr->cap.max_recv_wr = qp->rq.max;
  507. init_attr->cap.max_send_sge = qp->sq.max_gs;
  508. init_attr->cap.max_recv_sge = qp->rq.max_gs;
  509. init_attr->cap.max_inline_data = qp->max_inline_data;
  510. return &qp->ibqp;
  511. }
  512. static int mthca_destroy_qp(struct ib_qp *qp)
  513. {
  514. if (qp->uobject) {
  515. mthca_unmap_user_db(to_mdev(qp->device),
  516. &to_mucontext(qp->uobject->context)->uar,
  517. to_mucontext(qp->uobject->context)->db_tab,
  518. to_mqp(qp)->sq.db_index);
  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)->rq.db_index);
  523. }
  524. mthca_free_qp(to_mdev(qp->device), to_mqp(qp));
  525. kfree(qp);
  526. return 0;
  527. }
  528. static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
  529. struct ib_ucontext *context,
  530. struct ib_udata *udata)
  531. {
  532. struct mthca_create_cq ucmd;
  533. struct mthca_cq *cq;
  534. int nent;
  535. int err;
  536. if (entries < 1 || entries > to_mdev(ibdev)->limits.max_cqes)
  537. return ERR_PTR(-EINVAL);
  538. if (context) {
  539. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  540. return ERR_PTR(-EFAULT);
  541. err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  542. to_mucontext(context)->db_tab,
  543. ucmd.set_db_index, ucmd.set_db_page);
  544. if (err)
  545. return ERR_PTR(err);
  546. err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  547. to_mucontext(context)->db_tab,
  548. ucmd.arm_db_index, ucmd.arm_db_page);
  549. if (err)
  550. goto err_unmap_set;
  551. }
  552. cq = kmalloc(sizeof *cq, GFP_KERNEL);
  553. if (!cq) {
  554. err = -ENOMEM;
  555. goto err_unmap_arm;
  556. }
  557. if (context) {
  558. cq->buf.mr.ibmr.lkey = ucmd.lkey;
  559. cq->set_ci_db_index = ucmd.set_db_index;
  560. cq->arm_db_index = ucmd.arm_db_index;
  561. }
  562. for (nent = 1; nent <= entries; nent <<= 1)
  563. ; /* nothing */
  564. err = mthca_init_cq(to_mdev(ibdev), nent,
  565. context ? to_mucontext(context) : NULL,
  566. context ? ucmd.pdn : to_mdev(ibdev)->driver_pd.pd_num,
  567. cq);
  568. if (err)
  569. goto err_free;
  570. if (context && ib_copy_to_udata(udata, &cq->cqn, sizeof (__u32))) {
  571. mthca_free_cq(to_mdev(ibdev), cq);
  572. goto err_free;
  573. }
  574. cq->resize_buf = NULL;
  575. return &cq->ibcq;
  576. err_free:
  577. kfree(cq);
  578. err_unmap_arm:
  579. if (context)
  580. mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  581. to_mucontext(context)->db_tab, ucmd.arm_db_index);
  582. err_unmap_set:
  583. if (context)
  584. mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  585. to_mucontext(context)->db_tab, ucmd.set_db_index);
  586. return ERR_PTR(err);
  587. }
  588. static int mthca_alloc_resize_buf(struct mthca_dev *dev, struct mthca_cq *cq,
  589. int entries)
  590. {
  591. int ret;
  592. spin_lock_irq(&cq->lock);
  593. if (cq->resize_buf) {
  594. ret = -EBUSY;
  595. goto unlock;
  596. }
  597. cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_ATOMIC);
  598. if (!cq->resize_buf) {
  599. ret = -ENOMEM;
  600. goto unlock;
  601. }
  602. cq->resize_buf->state = CQ_RESIZE_ALLOC;
  603. ret = 0;
  604. unlock:
  605. spin_unlock_irq(&cq->lock);
  606. if (ret)
  607. return ret;
  608. ret = mthca_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
  609. if (ret) {
  610. spin_lock_irq(&cq->lock);
  611. kfree(cq->resize_buf);
  612. cq->resize_buf = NULL;
  613. spin_unlock_irq(&cq->lock);
  614. return ret;
  615. }
  616. cq->resize_buf->cqe = entries - 1;
  617. spin_lock_irq(&cq->lock);
  618. cq->resize_buf->state = CQ_RESIZE_READY;
  619. spin_unlock_irq(&cq->lock);
  620. return 0;
  621. }
  622. static int mthca_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
  623. {
  624. struct mthca_dev *dev = to_mdev(ibcq->device);
  625. struct mthca_cq *cq = to_mcq(ibcq);
  626. struct mthca_resize_cq ucmd;
  627. u32 lkey;
  628. u8 status;
  629. int ret;
  630. if (entries < 1 || entries > dev->limits.max_cqes)
  631. return -EINVAL;
  632. entries = roundup_pow_of_two(entries + 1);
  633. if (entries == ibcq->cqe + 1)
  634. return 0;
  635. if (cq->is_kernel) {
  636. ret = mthca_alloc_resize_buf(dev, cq, entries);
  637. if (ret)
  638. return ret;
  639. lkey = cq->resize_buf->buf.mr.ibmr.lkey;
  640. } else {
  641. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  642. return -EFAULT;
  643. lkey = ucmd.lkey;
  644. }
  645. ret = mthca_RESIZE_CQ(dev, cq->cqn, lkey, long_log2(entries), &status);
  646. if (status)
  647. ret = -EINVAL;
  648. if (ret) {
  649. if (cq->resize_buf) {
  650. mthca_free_cq_buf(dev, &cq->resize_buf->buf,
  651. cq->resize_buf->cqe);
  652. kfree(cq->resize_buf);
  653. spin_lock_irq(&cq->lock);
  654. cq->resize_buf = NULL;
  655. spin_unlock_irq(&cq->lock);
  656. }
  657. return ret;
  658. }
  659. if (cq->is_kernel) {
  660. struct mthca_cq_buf tbuf;
  661. int tcqe;
  662. spin_lock_irq(&cq->lock);
  663. if (cq->resize_buf->state == CQ_RESIZE_READY) {
  664. mthca_cq_resize_copy_cqes(cq);
  665. tbuf = cq->buf;
  666. tcqe = cq->ibcq.cqe;
  667. cq->buf = cq->resize_buf->buf;
  668. cq->ibcq.cqe = cq->resize_buf->cqe;
  669. } else {
  670. tbuf = cq->resize_buf->buf;
  671. tcqe = cq->resize_buf->cqe;
  672. }
  673. kfree(cq->resize_buf);
  674. cq->resize_buf = NULL;
  675. spin_unlock_irq(&cq->lock);
  676. mthca_free_cq_buf(dev, &tbuf, tcqe);
  677. } else
  678. ibcq->cqe = entries - 1;
  679. return 0;
  680. }
  681. static int mthca_destroy_cq(struct ib_cq *cq)
  682. {
  683. if (cq->uobject) {
  684. mthca_unmap_user_db(to_mdev(cq->device),
  685. &to_mucontext(cq->uobject->context)->uar,
  686. to_mucontext(cq->uobject->context)->db_tab,
  687. to_mcq(cq)->arm_db_index);
  688. mthca_unmap_user_db(to_mdev(cq->device),
  689. &to_mucontext(cq->uobject->context)->uar,
  690. to_mucontext(cq->uobject->context)->db_tab,
  691. to_mcq(cq)->set_ci_db_index);
  692. }
  693. mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
  694. kfree(cq);
  695. return 0;
  696. }
  697. static inline u32 convert_access(int acc)
  698. {
  699. return (acc & IB_ACCESS_REMOTE_ATOMIC ? MTHCA_MPT_FLAG_ATOMIC : 0) |
  700. (acc & IB_ACCESS_REMOTE_WRITE ? MTHCA_MPT_FLAG_REMOTE_WRITE : 0) |
  701. (acc & IB_ACCESS_REMOTE_READ ? MTHCA_MPT_FLAG_REMOTE_READ : 0) |
  702. (acc & IB_ACCESS_LOCAL_WRITE ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0) |
  703. MTHCA_MPT_FLAG_LOCAL_READ;
  704. }
  705. static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
  706. {
  707. struct mthca_mr *mr;
  708. int err;
  709. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  710. if (!mr)
  711. return ERR_PTR(-ENOMEM);
  712. err = mthca_mr_alloc_notrans(to_mdev(pd->device),
  713. to_mpd(pd)->pd_num,
  714. convert_access(acc), mr);
  715. if (err) {
  716. kfree(mr);
  717. return ERR_PTR(err);
  718. }
  719. return &mr->ibmr;
  720. }
  721. static struct ib_mr *mthca_reg_phys_mr(struct ib_pd *pd,
  722. struct ib_phys_buf *buffer_list,
  723. int num_phys_buf,
  724. int acc,
  725. u64 *iova_start)
  726. {
  727. struct mthca_mr *mr;
  728. u64 *page_list;
  729. u64 total_size;
  730. u64 mask;
  731. int shift;
  732. int npages;
  733. int err;
  734. int i, j, n;
  735. /* First check that we have enough alignment */
  736. if ((*iova_start & ~PAGE_MASK) != (buffer_list[0].addr & ~PAGE_MASK))
  737. return ERR_PTR(-EINVAL);
  738. mask = 0;
  739. total_size = 0;
  740. for (i = 0; i < num_phys_buf; ++i) {
  741. if (i != 0)
  742. mask |= buffer_list[i].addr;
  743. if (i != num_phys_buf - 1)
  744. mask |= buffer_list[i].addr + buffer_list[i].size;
  745. total_size += buffer_list[i].size;
  746. }
  747. if (mask & ~PAGE_MASK)
  748. return ERR_PTR(-EINVAL);
  749. /* Find largest page shift we can use to cover buffers */
  750. for (shift = PAGE_SHIFT; shift < 31; ++shift)
  751. if (num_phys_buf > 1) {
  752. if ((1ULL << shift) & mask)
  753. break;
  754. } else {
  755. if (1ULL << shift >=
  756. buffer_list[0].size +
  757. (buffer_list[0].addr & ((1ULL << shift) - 1)))
  758. break;
  759. }
  760. buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
  761. buffer_list[0].addr &= ~0ull << shift;
  762. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  763. if (!mr)
  764. return ERR_PTR(-ENOMEM);
  765. npages = 0;
  766. for (i = 0; i < num_phys_buf; ++i)
  767. npages += (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  768. if (!npages)
  769. return &mr->ibmr;
  770. page_list = kmalloc(npages * sizeof *page_list, GFP_KERNEL);
  771. if (!page_list) {
  772. kfree(mr);
  773. return ERR_PTR(-ENOMEM);
  774. }
  775. n = 0;
  776. for (i = 0; i < num_phys_buf; ++i)
  777. for (j = 0;
  778. j < (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  779. ++j)
  780. page_list[n++] = buffer_list[i].addr + ((u64) j << shift);
  781. mthca_dbg(to_mdev(pd->device), "Registering memory at %llx (iova %llx) "
  782. "in PD %x; shift %d, npages %d.\n",
  783. (unsigned long long) buffer_list[0].addr,
  784. (unsigned long long) *iova_start,
  785. to_mpd(pd)->pd_num,
  786. shift, npages);
  787. err = mthca_mr_alloc_phys(to_mdev(pd->device),
  788. to_mpd(pd)->pd_num,
  789. page_list, shift, npages,
  790. *iova_start, total_size,
  791. convert_access(acc), mr);
  792. if (err) {
  793. kfree(page_list);
  794. kfree(mr);
  795. return ERR_PTR(err);
  796. }
  797. kfree(page_list);
  798. return &mr->ibmr;
  799. }
  800. static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
  801. int acc, struct ib_udata *udata)
  802. {
  803. struct mthca_dev *dev = to_mdev(pd->device);
  804. struct ib_umem_chunk *chunk;
  805. struct mthca_mr *mr;
  806. u64 *pages;
  807. int shift, n, len;
  808. int i, j, k;
  809. int err = 0;
  810. shift = ffs(region->page_size) - 1;
  811. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  812. if (!mr)
  813. return ERR_PTR(-ENOMEM);
  814. n = 0;
  815. list_for_each_entry(chunk, &region->chunk_list, list)
  816. n += chunk->nents;
  817. mr->mtt = mthca_alloc_mtt(dev, n);
  818. if (IS_ERR(mr->mtt)) {
  819. err = PTR_ERR(mr->mtt);
  820. goto err;
  821. }
  822. pages = (u64 *) __get_free_page(GFP_KERNEL);
  823. if (!pages) {
  824. err = -ENOMEM;
  825. goto err_mtt;
  826. }
  827. i = n = 0;
  828. list_for_each_entry(chunk, &region->chunk_list, list)
  829. for (j = 0; j < chunk->nmap; ++j) {
  830. len = sg_dma_len(&chunk->page_list[j]) >> shift;
  831. for (k = 0; k < len; ++k) {
  832. pages[i++] = sg_dma_address(&chunk->page_list[j]) +
  833. region->page_size * k;
  834. /*
  835. * Be friendly to WRITE_MTT command
  836. * and leave two empty slots for the
  837. * index and reserved fields of the
  838. * mailbox.
  839. */
  840. if (i == PAGE_SIZE / sizeof (u64) - 2) {
  841. err = mthca_write_mtt(dev, mr->mtt,
  842. n, pages, i);
  843. if (err)
  844. goto mtt_done;
  845. n += i;
  846. i = 0;
  847. }
  848. }
  849. }
  850. if (i)
  851. err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
  852. mtt_done:
  853. free_page((unsigned long) pages);
  854. if (err)
  855. goto err_mtt;
  856. err = mthca_mr_alloc(dev, to_mpd(pd)->pd_num, shift, region->virt_base,
  857. region->length, convert_access(acc), mr);
  858. if (err)
  859. goto err_mtt;
  860. return &mr->ibmr;
  861. err_mtt:
  862. mthca_free_mtt(dev, mr->mtt);
  863. err:
  864. kfree(mr);
  865. return ERR_PTR(err);
  866. }
  867. static int mthca_dereg_mr(struct ib_mr *mr)
  868. {
  869. struct mthca_mr *mmr = to_mmr(mr);
  870. mthca_free_mr(to_mdev(mr->device), mmr);
  871. kfree(mmr);
  872. return 0;
  873. }
  874. static struct ib_fmr *mthca_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
  875. struct ib_fmr_attr *fmr_attr)
  876. {
  877. struct mthca_fmr *fmr;
  878. int err;
  879. fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
  880. if (!fmr)
  881. return ERR_PTR(-ENOMEM);
  882. memcpy(&fmr->attr, fmr_attr, sizeof *fmr_attr);
  883. err = mthca_fmr_alloc(to_mdev(pd->device), to_mpd(pd)->pd_num,
  884. convert_access(mr_access_flags), fmr);
  885. if (err) {
  886. kfree(fmr);
  887. return ERR_PTR(err);
  888. }
  889. return &fmr->ibmr;
  890. }
  891. static int mthca_dealloc_fmr(struct ib_fmr *fmr)
  892. {
  893. struct mthca_fmr *mfmr = to_mfmr(fmr);
  894. int err;
  895. err = mthca_free_fmr(to_mdev(fmr->device), mfmr);
  896. if (err)
  897. return err;
  898. kfree(mfmr);
  899. return 0;
  900. }
  901. static int mthca_unmap_fmr(struct list_head *fmr_list)
  902. {
  903. struct ib_fmr *fmr;
  904. int err;
  905. u8 status;
  906. struct mthca_dev *mdev = NULL;
  907. list_for_each_entry(fmr, fmr_list, list) {
  908. if (mdev && to_mdev(fmr->device) != mdev)
  909. return -EINVAL;
  910. mdev = to_mdev(fmr->device);
  911. }
  912. if (!mdev)
  913. return 0;
  914. if (mthca_is_memfree(mdev)) {
  915. list_for_each_entry(fmr, fmr_list, list)
  916. mthca_arbel_fmr_unmap(mdev, to_mfmr(fmr));
  917. wmb();
  918. } else
  919. list_for_each_entry(fmr, fmr_list, list)
  920. mthca_tavor_fmr_unmap(mdev, to_mfmr(fmr));
  921. err = mthca_SYNC_TPT(mdev, &status);
  922. if (err)
  923. return err;
  924. if (status)
  925. return -EINVAL;
  926. return 0;
  927. }
  928. static ssize_t show_rev(struct class_device *cdev, char *buf)
  929. {
  930. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  931. return sprintf(buf, "%x\n", dev->rev_id);
  932. }
  933. static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
  934. {
  935. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  936. return sprintf(buf, "%d.%d.%d\n", (int) (dev->fw_ver >> 32),
  937. (int) (dev->fw_ver >> 16) & 0xffff,
  938. (int) dev->fw_ver & 0xffff);
  939. }
  940. static ssize_t show_hca(struct class_device *cdev, char *buf)
  941. {
  942. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  943. switch (dev->pdev->device) {
  944. case PCI_DEVICE_ID_MELLANOX_TAVOR:
  945. return sprintf(buf, "MT23108\n");
  946. case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
  947. return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
  948. case PCI_DEVICE_ID_MELLANOX_ARBEL:
  949. return sprintf(buf, "MT25208\n");
  950. case PCI_DEVICE_ID_MELLANOX_SINAI:
  951. case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
  952. return sprintf(buf, "MT25204\n");
  953. default:
  954. return sprintf(buf, "unknown\n");
  955. }
  956. }
  957. static ssize_t show_board(struct class_device *cdev, char *buf)
  958. {
  959. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  960. return sprintf(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
  961. }
  962. static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  963. static CLASS_DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  964. static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  965. static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  966. static struct class_device_attribute *mthca_class_attributes[] = {
  967. &class_device_attr_hw_rev,
  968. &class_device_attr_fw_ver,
  969. &class_device_attr_hca_type,
  970. &class_device_attr_board_id
  971. };
  972. static int mthca_init_node_data(struct mthca_dev *dev)
  973. {
  974. struct ib_smp *in_mad = NULL;
  975. struct ib_smp *out_mad = NULL;
  976. int err = -ENOMEM;
  977. u8 status;
  978. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  979. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  980. if (!in_mad || !out_mad)
  981. goto out;
  982. init_query_mad(in_mad);
  983. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  984. err = mthca_MAD_IFC(dev, 1, 1,
  985. 1, NULL, NULL, in_mad, out_mad,
  986. &status);
  987. if (err)
  988. goto out;
  989. if (status) {
  990. err = -EINVAL;
  991. goto out;
  992. }
  993. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  994. out:
  995. kfree(in_mad);
  996. kfree(out_mad);
  997. return err;
  998. }
  999. int mthca_register_device(struct mthca_dev *dev)
  1000. {
  1001. int ret;
  1002. int i;
  1003. ret = mthca_init_node_data(dev);
  1004. if (ret)
  1005. return ret;
  1006. strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
  1007. dev->ib_dev.owner = THIS_MODULE;
  1008. dev->ib_dev.uverbs_abi_ver = MTHCA_UVERBS_ABI_VERSION;
  1009. dev->ib_dev.uverbs_cmd_mask =
  1010. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  1011. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  1012. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  1013. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  1014. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  1015. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  1016. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  1017. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  1018. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  1019. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  1020. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  1021. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  1022. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  1023. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  1024. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  1025. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  1026. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  1027. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  1028. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
  1029. dev->ib_dev.node_type = IB_NODE_CA;
  1030. dev->ib_dev.phys_port_cnt = dev->limits.num_ports;
  1031. dev->ib_dev.dma_device = &dev->pdev->dev;
  1032. dev->ib_dev.class_dev.dev = &dev->pdev->dev;
  1033. dev->ib_dev.query_device = mthca_query_device;
  1034. dev->ib_dev.query_port = mthca_query_port;
  1035. dev->ib_dev.modify_port = mthca_modify_port;
  1036. dev->ib_dev.query_pkey = mthca_query_pkey;
  1037. dev->ib_dev.query_gid = mthca_query_gid;
  1038. dev->ib_dev.alloc_ucontext = mthca_alloc_ucontext;
  1039. dev->ib_dev.dealloc_ucontext = mthca_dealloc_ucontext;
  1040. dev->ib_dev.mmap = mthca_mmap_uar;
  1041. dev->ib_dev.alloc_pd = mthca_alloc_pd;
  1042. dev->ib_dev.dealloc_pd = mthca_dealloc_pd;
  1043. dev->ib_dev.create_ah = mthca_ah_create;
  1044. dev->ib_dev.destroy_ah = mthca_ah_destroy;
  1045. if (dev->mthca_flags & MTHCA_FLAG_SRQ) {
  1046. dev->ib_dev.create_srq = mthca_create_srq;
  1047. dev->ib_dev.modify_srq = mthca_modify_srq;
  1048. dev->ib_dev.destroy_srq = mthca_destroy_srq;
  1049. if (mthca_is_memfree(dev))
  1050. dev->ib_dev.post_srq_recv = mthca_arbel_post_srq_recv;
  1051. else
  1052. dev->ib_dev.post_srq_recv = mthca_tavor_post_srq_recv;
  1053. }
  1054. dev->ib_dev.create_qp = mthca_create_qp;
  1055. dev->ib_dev.modify_qp = mthca_modify_qp;
  1056. dev->ib_dev.destroy_qp = mthca_destroy_qp;
  1057. dev->ib_dev.create_cq = mthca_create_cq;
  1058. dev->ib_dev.resize_cq = mthca_resize_cq;
  1059. dev->ib_dev.destroy_cq = mthca_destroy_cq;
  1060. dev->ib_dev.poll_cq = mthca_poll_cq;
  1061. dev->ib_dev.get_dma_mr = mthca_get_dma_mr;
  1062. dev->ib_dev.reg_phys_mr = mthca_reg_phys_mr;
  1063. dev->ib_dev.reg_user_mr = mthca_reg_user_mr;
  1064. dev->ib_dev.dereg_mr = mthca_dereg_mr;
  1065. if (dev->mthca_flags & MTHCA_FLAG_FMR) {
  1066. dev->ib_dev.alloc_fmr = mthca_alloc_fmr;
  1067. dev->ib_dev.unmap_fmr = mthca_unmap_fmr;
  1068. dev->ib_dev.dealloc_fmr = mthca_dealloc_fmr;
  1069. if (mthca_is_memfree(dev))
  1070. dev->ib_dev.map_phys_fmr = mthca_arbel_map_phys_fmr;
  1071. else
  1072. dev->ib_dev.map_phys_fmr = mthca_tavor_map_phys_fmr;
  1073. }
  1074. dev->ib_dev.attach_mcast = mthca_multicast_attach;
  1075. dev->ib_dev.detach_mcast = mthca_multicast_detach;
  1076. dev->ib_dev.process_mad = mthca_process_mad;
  1077. if (mthca_is_memfree(dev)) {
  1078. dev->ib_dev.req_notify_cq = mthca_arbel_arm_cq;
  1079. dev->ib_dev.post_send = mthca_arbel_post_send;
  1080. dev->ib_dev.post_recv = mthca_arbel_post_receive;
  1081. } else {
  1082. dev->ib_dev.req_notify_cq = mthca_tavor_arm_cq;
  1083. dev->ib_dev.post_send = mthca_tavor_post_send;
  1084. dev->ib_dev.post_recv = mthca_tavor_post_receive;
  1085. }
  1086. mutex_init(&dev->cap_mask_mutex);
  1087. ret = ib_register_device(&dev->ib_dev);
  1088. if (ret)
  1089. return ret;
  1090. for (i = 0; i < ARRAY_SIZE(mthca_class_attributes); ++i) {
  1091. ret = class_device_create_file(&dev->ib_dev.class_dev,
  1092. mthca_class_attributes[i]);
  1093. if (ret) {
  1094. ib_unregister_device(&dev->ib_dev);
  1095. return ret;
  1096. }
  1097. }
  1098. mthca_start_catas_poll(dev);
  1099. return 0;
  1100. }
  1101. void mthca_unregister_device(struct mthca_dev *dev)
  1102. {
  1103. mthca_stop_catas_poll(dev);
  1104. ib_unregister_device(&dev->ib_dev);
  1105. }