mthca_provider.c 35 KB

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