mthca_provider.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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. if (init_attr->srq_type != IB_SRQT_BASIC)
  367. return ERR_PTR(-ENOSYS);
  368. srq = kmalloc(sizeof *srq, GFP_KERNEL);
  369. if (!srq)
  370. return ERR_PTR(-ENOMEM);
  371. if (pd->uobject) {
  372. context = to_mucontext(pd->uobject->context);
  373. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  374. err = -EFAULT;
  375. goto err_free;
  376. }
  377. err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
  378. context->db_tab, ucmd.db_index,
  379. ucmd.db_page);
  380. if (err)
  381. goto err_free;
  382. srq->mr.ibmr.lkey = ucmd.lkey;
  383. srq->db_index = ucmd.db_index;
  384. }
  385. err = mthca_alloc_srq(to_mdev(pd->device), to_mpd(pd),
  386. &init_attr->attr, srq);
  387. if (err && pd->uobject)
  388. mthca_unmap_user_db(to_mdev(pd->device), &context->uar,
  389. context->db_tab, ucmd.db_index);
  390. if (err)
  391. goto err_free;
  392. if (context && ib_copy_to_udata(udata, &srq->srqn, sizeof (__u32))) {
  393. mthca_free_srq(to_mdev(pd->device), srq);
  394. err = -EFAULT;
  395. goto err_free;
  396. }
  397. return &srq->ibsrq;
  398. err_free:
  399. kfree(srq);
  400. return ERR_PTR(err);
  401. }
  402. static int mthca_destroy_srq(struct ib_srq *srq)
  403. {
  404. struct mthca_ucontext *context;
  405. if (srq->uobject) {
  406. context = to_mucontext(srq->uobject->context);
  407. mthca_unmap_user_db(to_mdev(srq->device), &context->uar,
  408. context->db_tab, to_msrq(srq)->db_index);
  409. }
  410. mthca_free_srq(to_mdev(srq->device), to_msrq(srq));
  411. kfree(srq);
  412. return 0;
  413. }
  414. static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
  415. struct ib_qp_init_attr *init_attr,
  416. struct ib_udata *udata)
  417. {
  418. struct mthca_create_qp ucmd;
  419. struct mthca_qp *qp;
  420. int err;
  421. if (init_attr->create_flags)
  422. return ERR_PTR(-EINVAL);
  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. int comp_vector,
  530. struct ib_ucontext *context,
  531. struct ib_udata *udata)
  532. {
  533. struct mthca_create_cq ucmd;
  534. struct mthca_cq *cq;
  535. int nent;
  536. int err;
  537. if (entries < 1 || entries > to_mdev(ibdev)->limits.max_cqes)
  538. return ERR_PTR(-EINVAL);
  539. if (context) {
  540. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  541. return ERR_PTR(-EFAULT);
  542. err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  543. to_mucontext(context)->db_tab,
  544. ucmd.set_db_index, ucmd.set_db_page);
  545. if (err)
  546. return ERR_PTR(err);
  547. err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  548. to_mucontext(context)->db_tab,
  549. ucmd.arm_db_index, ucmd.arm_db_page);
  550. if (err)
  551. goto err_unmap_set;
  552. }
  553. cq = kmalloc(sizeof *cq, GFP_KERNEL);
  554. if (!cq) {
  555. err = -ENOMEM;
  556. goto err_unmap_arm;
  557. }
  558. if (context) {
  559. cq->buf.mr.ibmr.lkey = ucmd.lkey;
  560. cq->set_ci_db_index = ucmd.set_db_index;
  561. cq->arm_db_index = ucmd.arm_db_index;
  562. }
  563. for (nent = 1; nent <= entries; nent <<= 1)
  564. ; /* nothing */
  565. err = mthca_init_cq(to_mdev(ibdev), nent,
  566. context ? to_mucontext(context) : NULL,
  567. context ? ucmd.pdn : to_mdev(ibdev)->driver_pd.pd_num,
  568. cq);
  569. if (err)
  570. goto err_free;
  571. if (context && ib_copy_to_udata(udata, &cq->cqn, sizeof (__u32))) {
  572. mthca_free_cq(to_mdev(ibdev), cq);
  573. goto err_free;
  574. }
  575. cq->resize_buf = NULL;
  576. return &cq->ibcq;
  577. err_free:
  578. kfree(cq);
  579. err_unmap_arm:
  580. if (context)
  581. mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  582. to_mucontext(context)->db_tab, ucmd.arm_db_index);
  583. err_unmap_set:
  584. if (context)
  585. mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
  586. to_mucontext(context)->db_tab, ucmd.set_db_index);
  587. return ERR_PTR(err);
  588. }
  589. static int mthca_alloc_resize_buf(struct mthca_dev *dev, struct mthca_cq *cq,
  590. int entries)
  591. {
  592. int ret;
  593. spin_lock_irq(&cq->lock);
  594. if (cq->resize_buf) {
  595. ret = -EBUSY;
  596. goto unlock;
  597. }
  598. cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_ATOMIC);
  599. if (!cq->resize_buf) {
  600. ret = -ENOMEM;
  601. goto unlock;
  602. }
  603. cq->resize_buf->state = CQ_RESIZE_ALLOC;
  604. ret = 0;
  605. unlock:
  606. spin_unlock_irq(&cq->lock);
  607. if (ret)
  608. return ret;
  609. ret = mthca_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
  610. if (ret) {
  611. spin_lock_irq(&cq->lock);
  612. kfree(cq->resize_buf);
  613. cq->resize_buf = NULL;
  614. spin_unlock_irq(&cq->lock);
  615. return ret;
  616. }
  617. cq->resize_buf->cqe = entries - 1;
  618. spin_lock_irq(&cq->lock);
  619. cq->resize_buf->state = CQ_RESIZE_READY;
  620. spin_unlock_irq(&cq->lock);
  621. return 0;
  622. }
  623. static int mthca_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
  624. {
  625. struct mthca_dev *dev = to_mdev(ibcq->device);
  626. struct mthca_cq *cq = to_mcq(ibcq);
  627. struct mthca_resize_cq ucmd;
  628. u32 lkey;
  629. int ret;
  630. if (entries < 1 || entries > dev->limits.max_cqes)
  631. return -EINVAL;
  632. mutex_lock(&cq->mutex);
  633. entries = roundup_pow_of_two(entries + 1);
  634. if (entries == ibcq->cqe + 1) {
  635. ret = 0;
  636. goto out;
  637. }
  638. if (cq->is_kernel) {
  639. ret = mthca_alloc_resize_buf(dev, cq, entries);
  640. if (ret)
  641. goto out;
  642. lkey = cq->resize_buf->buf.mr.ibmr.lkey;
  643. } else {
  644. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  645. ret = -EFAULT;
  646. goto out;
  647. }
  648. lkey = ucmd.lkey;
  649. }
  650. ret = mthca_RESIZE_CQ(dev, cq->cqn, lkey, ilog2(entries));
  651. if (ret) {
  652. if (cq->resize_buf) {
  653. mthca_free_cq_buf(dev, &cq->resize_buf->buf,
  654. cq->resize_buf->cqe);
  655. kfree(cq->resize_buf);
  656. spin_lock_irq(&cq->lock);
  657. cq->resize_buf = NULL;
  658. spin_unlock_irq(&cq->lock);
  659. }
  660. goto out;
  661. }
  662. if (cq->is_kernel) {
  663. struct mthca_cq_buf tbuf;
  664. int tcqe;
  665. spin_lock_irq(&cq->lock);
  666. if (cq->resize_buf->state == CQ_RESIZE_READY) {
  667. mthca_cq_resize_copy_cqes(cq);
  668. tbuf = cq->buf;
  669. tcqe = cq->ibcq.cqe;
  670. cq->buf = cq->resize_buf->buf;
  671. cq->ibcq.cqe = cq->resize_buf->cqe;
  672. } else {
  673. tbuf = cq->resize_buf->buf;
  674. tcqe = cq->resize_buf->cqe;
  675. }
  676. kfree(cq->resize_buf);
  677. cq->resize_buf = NULL;
  678. spin_unlock_irq(&cq->lock);
  679. mthca_free_cq_buf(dev, &tbuf, tcqe);
  680. } else
  681. ibcq->cqe = entries - 1;
  682. out:
  683. mutex_unlock(&cq->mutex);
  684. return ret;
  685. }
  686. static int mthca_destroy_cq(struct ib_cq *cq)
  687. {
  688. if (cq->uobject) {
  689. mthca_unmap_user_db(to_mdev(cq->device),
  690. &to_mucontext(cq->uobject->context)->uar,
  691. to_mucontext(cq->uobject->context)->db_tab,
  692. to_mcq(cq)->arm_db_index);
  693. mthca_unmap_user_db(to_mdev(cq->device),
  694. &to_mucontext(cq->uobject->context)->uar,
  695. to_mucontext(cq->uobject->context)->db_tab,
  696. to_mcq(cq)->set_ci_db_index);
  697. }
  698. mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
  699. kfree(cq);
  700. return 0;
  701. }
  702. static inline u32 convert_access(int acc)
  703. {
  704. return (acc & IB_ACCESS_REMOTE_ATOMIC ? MTHCA_MPT_FLAG_ATOMIC : 0) |
  705. (acc & IB_ACCESS_REMOTE_WRITE ? MTHCA_MPT_FLAG_REMOTE_WRITE : 0) |
  706. (acc & IB_ACCESS_REMOTE_READ ? MTHCA_MPT_FLAG_REMOTE_READ : 0) |
  707. (acc & IB_ACCESS_LOCAL_WRITE ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0) |
  708. MTHCA_MPT_FLAG_LOCAL_READ;
  709. }
  710. static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
  711. {
  712. struct mthca_mr *mr;
  713. int err;
  714. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  715. if (!mr)
  716. return ERR_PTR(-ENOMEM);
  717. err = mthca_mr_alloc_notrans(to_mdev(pd->device),
  718. to_mpd(pd)->pd_num,
  719. convert_access(acc), mr);
  720. if (err) {
  721. kfree(mr);
  722. return ERR_PTR(err);
  723. }
  724. mr->umem = NULL;
  725. return &mr->ibmr;
  726. }
  727. static struct ib_mr *mthca_reg_phys_mr(struct ib_pd *pd,
  728. struct ib_phys_buf *buffer_list,
  729. int num_phys_buf,
  730. int acc,
  731. u64 *iova_start)
  732. {
  733. struct mthca_mr *mr;
  734. u64 *page_list;
  735. u64 total_size;
  736. unsigned long mask;
  737. int shift;
  738. int npages;
  739. int err;
  740. int i, j, n;
  741. mask = buffer_list[0].addr ^ *iova_start;
  742. total_size = 0;
  743. for (i = 0; i < num_phys_buf; ++i) {
  744. if (i != 0)
  745. mask |= buffer_list[i].addr;
  746. if (i != num_phys_buf - 1)
  747. mask |= buffer_list[i].addr + buffer_list[i].size;
  748. total_size += buffer_list[i].size;
  749. }
  750. if (mask & ~PAGE_MASK)
  751. return ERR_PTR(-EINVAL);
  752. shift = __ffs(mask | 1 << 31);
  753. buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
  754. buffer_list[0].addr &= ~0ull << shift;
  755. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  756. if (!mr)
  757. return ERR_PTR(-ENOMEM);
  758. npages = 0;
  759. for (i = 0; i < num_phys_buf; ++i)
  760. npages += (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  761. if (!npages)
  762. return &mr->ibmr;
  763. page_list = kmalloc(npages * sizeof *page_list, GFP_KERNEL);
  764. if (!page_list) {
  765. kfree(mr);
  766. return ERR_PTR(-ENOMEM);
  767. }
  768. n = 0;
  769. for (i = 0; i < num_phys_buf; ++i)
  770. for (j = 0;
  771. j < (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  772. ++j)
  773. page_list[n++] = buffer_list[i].addr + ((u64) j << shift);
  774. mthca_dbg(to_mdev(pd->device), "Registering memory at %llx (iova %llx) "
  775. "in PD %x; shift %d, npages %d.\n",
  776. (unsigned long long) buffer_list[0].addr,
  777. (unsigned long long) *iova_start,
  778. to_mpd(pd)->pd_num,
  779. shift, npages);
  780. err = mthca_mr_alloc_phys(to_mdev(pd->device),
  781. to_mpd(pd)->pd_num,
  782. page_list, shift, npages,
  783. *iova_start, total_size,
  784. convert_access(acc), mr);
  785. if (err) {
  786. kfree(page_list);
  787. kfree(mr);
  788. return ERR_PTR(err);
  789. }
  790. kfree(page_list);
  791. mr->umem = NULL;
  792. return &mr->ibmr;
  793. }
  794. static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  795. u64 virt, int acc, struct ib_udata *udata)
  796. {
  797. struct mthca_dev *dev = to_mdev(pd->device);
  798. struct ib_umem_chunk *chunk;
  799. struct mthca_mr *mr;
  800. struct mthca_reg_mr ucmd;
  801. u64 *pages;
  802. int shift, n, len;
  803. int i, j, k;
  804. int err = 0;
  805. int write_mtt_size;
  806. if (udata->inlen - sizeof (struct ib_uverbs_cmd_hdr) < sizeof ucmd) {
  807. if (!to_mucontext(pd->uobject->context)->reg_mr_warned) {
  808. mthca_warn(dev, "Process '%s' did not pass in MR attrs.\n",
  809. current->comm);
  810. mthca_warn(dev, " Update libmthca to fix this.\n");
  811. }
  812. ++to_mucontext(pd->uobject->context)->reg_mr_warned;
  813. ucmd.mr_attrs = 0;
  814. } else if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
  815. return ERR_PTR(-EFAULT);
  816. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  817. if (!mr)
  818. return ERR_PTR(-ENOMEM);
  819. mr->umem = ib_umem_get(pd->uobject->context, start, length, acc,
  820. ucmd.mr_attrs & MTHCA_MR_DMASYNC);
  821. if (IS_ERR(mr->umem)) {
  822. err = PTR_ERR(mr->umem);
  823. goto err;
  824. }
  825. shift = ffs(mr->umem->page_size) - 1;
  826. n = 0;
  827. list_for_each_entry(chunk, &mr->umem->chunk_list, list)
  828. n += chunk->nents;
  829. mr->mtt = mthca_alloc_mtt(dev, n);
  830. if (IS_ERR(mr->mtt)) {
  831. err = PTR_ERR(mr->mtt);
  832. goto err_umem;
  833. }
  834. pages = (u64 *) __get_free_page(GFP_KERNEL);
  835. if (!pages) {
  836. err = -ENOMEM;
  837. goto err_mtt;
  838. }
  839. i = n = 0;
  840. write_mtt_size = min(mthca_write_mtt_size(dev), (int) (PAGE_SIZE / sizeof *pages));
  841. list_for_each_entry(chunk, &mr->umem->chunk_list, list)
  842. for (j = 0; j < chunk->nmap; ++j) {
  843. len = sg_dma_len(&chunk->page_list[j]) >> shift;
  844. for (k = 0; k < len; ++k) {
  845. pages[i++] = sg_dma_address(&chunk->page_list[j]) +
  846. mr->umem->page_size * k;
  847. /*
  848. * Be friendly to write_mtt and pass it chunks
  849. * of appropriate size.
  850. */
  851. if (i == write_mtt_size) {
  852. err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
  853. if (err)
  854. goto mtt_done;
  855. n += i;
  856. i = 0;
  857. }
  858. }
  859. }
  860. if (i)
  861. err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
  862. mtt_done:
  863. free_page((unsigned long) pages);
  864. if (err)
  865. goto err_mtt;
  866. err = mthca_mr_alloc(dev, to_mpd(pd)->pd_num, shift, virt, length,
  867. convert_access(acc), mr);
  868. if (err)
  869. goto err_mtt;
  870. return &mr->ibmr;
  871. err_mtt:
  872. mthca_free_mtt(dev, mr->mtt);
  873. err_umem:
  874. ib_umem_release(mr->umem);
  875. err:
  876. kfree(mr);
  877. return ERR_PTR(err);
  878. }
  879. static int mthca_dereg_mr(struct ib_mr *mr)
  880. {
  881. struct mthca_mr *mmr = to_mmr(mr);
  882. mthca_free_mr(to_mdev(mr->device), mmr);
  883. if (mmr->umem)
  884. ib_umem_release(mmr->umem);
  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. struct mthca_dev *mdev = NULL;
  920. list_for_each_entry(fmr, fmr_list, list) {
  921. if (mdev && to_mdev(fmr->device) != mdev)
  922. return -EINVAL;
  923. mdev = to_mdev(fmr->device);
  924. }
  925. if (!mdev)
  926. return 0;
  927. if (mthca_is_memfree(mdev)) {
  928. list_for_each_entry(fmr, fmr_list, list)
  929. mthca_arbel_fmr_unmap(mdev, to_mfmr(fmr));
  930. wmb();
  931. } else
  932. list_for_each_entry(fmr, fmr_list, list)
  933. mthca_tavor_fmr_unmap(mdev, to_mfmr(fmr));
  934. err = mthca_SYNC_TPT(mdev);
  935. return err;
  936. }
  937. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  938. char *buf)
  939. {
  940. struct mthca_dev *dev =
  941. container_of(device, struct mthca_dev, ib_dev.dev);
  942. return sprintf(buf, "%x\n", dev->rev_id);
  943. }
  944. static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
  945. char *buf)
  946. {
  947. struct mthca_dev *dev =
  948. container_of(device, struct mthca_dev, ib_dev.dev);
  949. return sprintf(buf, "%d.%d.%d\n", (int) (dev->fw_ver >> 32),
  950. (int) (dev->fw_ver >> 16) & 0xffff,
  951. (int) dev->fw_ver & 0xffff);
  952. }
  953. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  954. char *buf)
  955. {
  956. struct mthca_dev *dev =
  957. container_of(device, struct mthca_dev, ib_dev.dev);
  958. switch (dev->pdev->device) {
  959. case PCI_DEVICE_ID_MELLANOX_TAVOR:
  960. return sprintf(buf, "MT23108\n");
  961. case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
  962. return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
  963. case PCI_DEVICE_ID_MELLANOX_ARBEL:
  964. return sprintf(buf, "MT25208\n");
  965. case PCI_DEVICE_ID_MELLANOX_SINAI:
  966. case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
  967. return sprintf(buf, "MT25204\n");
  968. default:
  969. return sprintf(buf, "unknown\n");
  970. }
  971. }
  972. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  973. char *buf)
  974. {
  975. struct mthca_dev *dev =
  976. container_of(device, struct mthca_dev, ib_dev.dev);
  977. return sprintf(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
  978. }
  979. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  980. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  981. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  982. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  983. static struct device_attribute *mthca_dev_attributes[] = {
  984. &dev_attr_hw_rev,
  985. &dev_attr_fw_ver,
  986. &dev_attr_hca_type,
  987. &dev_attr_board_id
  988. };
  989. static int mthca_init_node_data(struct mthca_dev *dev)
  990. {
  991. struct ib_smp *in_mad = NULL;
  992. struct ib_smp *out_mad = NULL;
  993. int err = -ENOMEM;
  994. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  995. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  996. if (!in_mad || !out_mad)
  997. goto out;
  998. init_query_mad(in_mad);
  999. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  1000. err = mthca_MAD_IFC(dev, 1, 1,
  1001. 1, NULL, NULL, in_mad, out_mad);
  1002. if (err)
  1003. goto out;
  1004. memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
  1005. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  1006. err = mthca_MAD_IFC(dev, 1, 1,
  1007. 1, NULL, NULL, in_mad, out_mad);
  1008. if (err)
  1009. goto out;
  1010. if (mthca_is_memfree(dev))
  1011. dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
  1012. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  1013. out:
  1014. kfree(in_mad);
  1015. kfree(out_mad);
  1016. return err;
  1017. }
  1018. int mthca_register_device(struct mthca_dev *dev)
  1019. {
  1020. int ret;
  1021. int i;
  1022. ret = mthca_init_node_data(dev);
  1023. if (ret)
  1024. return ret;
  1025. strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
  1026. dev->ib_dev.owner = THIS_MODULE;
  1027. dev->ib_dev.uverbs_abi_ver = MTHCA_UVERBS_ABI_VERSION;
  1028. dev->ib_dev.uverbs_cmd_mask =
  1029. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  1030. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  1031. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  1032. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  1033. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  1034. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  1035. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  1036. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  1037. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  1038. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  1039. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  1040. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  1041. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  1042. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  1043. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  1044. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  1045. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST);
  1046. dev->ib_dev.node_type = RDMA_NODE_IB_CA;
  1047. dev->ib_dev.phys_port_cnt = dev->limits.num_ports;
  1048. dev->ib_dev.num_comp_vectors = 1;
  1049. dev->ib_dev.dma_device = &dev->pdev->dev;
  1050. dev->ib_dev.query_device = mthca_query_device;
  1051. dev->ib_dev.query_port = mthca_query_port;
  1052. dev->ib_dev.modify_device = mthca_modify_device;
  1053. dev->ib_dev.modify_port = mthca_modify_port;
  1054. dev->ib_dev.query_pkey = mthca_query_pkey;
  1055. dev->ib_dev.query_gid = mthca_query_gid;
  1056. dev->ib_dev.alloc_ucontext = mthca_alloc_ucontext;
  1057. dev->ib_dev.dealloc_ucontext = mthca_dealloc_ucontext;
  1058. dev->ib_dev.mmap = mthca_mmap_uar;
  1059. dev->ib_dev.alloc_pd = mthca_alloc_pd;
  1060. dev->ib_dev.dealloc_pd = mthca_dealloc_pd;
  1061. dev->ib_dev.create_ah = mthca_ah_create;
  1062. dev->ib_dev.query_ah = mthca_ah_query;
  1063. dev->ib_dev.destroy_ah = mthca_ah_destroy;
  1064. if (dev->mthca_flags & MTHCA_FLAG_SRQ) {
  1065. dev->ib_dev.create_srq = mthca_create_srq;
  1066. dev->ib_dev.modify_srq = mthca_modify_srq;
  1067. dev->ib_dev.query_srq = mthca_query_srq;
  1068. dev->ib_dev.destroy_srq = mthca_destroy_srq;
  1069. dev->ib_dev.uverbs_cmd_mask |=
  1070. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  1071. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  1072. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  1073. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
  1074. if (mthca_is_memfree(dev))
  1075. dev->ib_dev.post_srq_recv = mthca_arbel_post_srq_recv;
  1076. else
  1077. dev->ib_dev.post_srq_recv = mthca_tavor_post_srq_recv;
  1078. }
  1079. dev->ib_dev.create_qp = mthca_create_qp;
  1080. dev->ib_dev.modify_qp = mthca_modify_qp;
  1081. dev->ib_dev.query_qp = mthca_query_qp;
  1082. dev->ib_dev.destroy_qp = mthca_destroy_qp;
  1083. dev->ib_dev.create_cq = mthca_create_cq;
  1084. dev->ib_dev.resize_cq = mthca_resize_cq;
  1085. dev->ib_dev.destroy_cq = mthca_destroy_cq;
  1086. dev->ib_dev.poll_cq = mthca_poll_cq;
  1087. dev->ib_dev.get_dma_mr = mthca_get_dma_mr;
  1088. dev->ib_dev.reg_phys_mr = mthca_reg_phys_mr;
  1089. dev->ib_dev.reg_user_mr = mthca_reg_user_mr;
  1090. dev->ib_dev.dereg_mr = mthca_dereg_mr;
  1091. if (dev->mthca_flags & MTHCA_FLAG_FMR) {
  1092. dev->ib_dev.alloc_fmr = mthca_alloc_fmr;
  1093. dev->ib_dev.unmap_fmr = mthca_unmap_fmr;
  1094. dev->ib_dev.dealloc_fmr = mthca_dealloc_fmr;
  1095. if (mthca_is_memfree(dev))
  1096. dev->ib_dev.map_phys_fmr = mthca_arbel_map_phys_fmr;
  1097. else
  1098. dev->ib_dev.map_phys_fmr = mthca_tavor_map_phys_fmr;
  1099. }
  1100. dev->ib_dev.attach_mcast = mthca_multicast_attach;
  1101. dev->ib_dev.detach_mcast = mthca_multicast_detach;
  1102. dev->ib_dev.process_mad = mthca_process_mad;
  1103. if (mthca_is_memfree(dev)) {
  1104. dev->ib_dev.req_notify_cq = mthca_arbel_arm_cq;
  1105. dev->ib_dev.post_send = mthca_arbel_post_send;
  1106. dev->ib_dev.post_recv = mthca_arbel_post_receive;
  1107. } else {
  1108. dev->ib_dev.req_notify_cq = mthca_tavor_arm_cq;
  1109. dev->ib_dev.post_send = mthca_tavor_post_send;
  1110. dev->ib_dev.post_recv = mthca_tavor_post_receive;
  1111. }
  1112. mutex_init(&dev->cap_mask_mutex);
  1113. ret = ib_register_device(&dev->ib_dev, NULL);
  1114. if (ret)
  1115. return ret;
  1116. for (i = 0; i < ARRAY_SIZE(mthca_dev_attributes); ++i) {
  1117. ret = device_create_file(&dev->ib_dev.dev,
  1118. mthca_dev_attributes[i]);
  1119. if (ret) {
  1120. ib_unregister_device(&dev->ib_dev);
  1121. return ret;
  1122. }
  1123. }
  1124. mthca_start_catas_poll(dev);
  1125. return 0;
  1126. }
  1127. void mthca_unregister_device(struct mthca_dev *dev)
  1128. {
  1129. mthca_stop_catas_poll(dev);
  1130. ib_unregister_device(&dev->ib_dev);
  1131. }