mthca_provider.c 35 KB

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