verbs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. /*
  2. * Copyright (c) 2004 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2004 Infinicon Corporation. All rights reserved.
  4. * Copyright (c) 2004 Intel Corporation. All rights reserved.
  5. * Copyright (c) 2004 Topspin Corporation. All rights reserved.
  6. * Copyright (c) 2004 Voltaire Corporation. All rights reserved.
  7. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  8. * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
  9. *
  10. * This software is available to you under a choice of one of two
  11. * licenses. You may choose to be licensed under the terms of the GNU
  12. * General Public License (GPL) Version 2, available from the file
  13. * COPYING in the main directory of this source tree, or the
  14. * OpenIB.org BSD license below:
  15. *
  16. * Redistribution and use in source and binary forms, with or
  17. * without modification, are permitted provided that the following
  18. * conditions are met:
  19. *
  20. * - Redistributions of source code must retain the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer.
  23. *
  24. * - Redistributions in binary form must reproduce the above
  25. * copyright notice, this list of conditions and the following
  26. * disclaimer in the documentation and/or other materials
  27. * provided with the distribution.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  32. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  33. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  34. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  35. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  36. * SOFTWARE.
  37. */
  38. #include <linux/errno.h>
  39. #include <linux/err.h>
  40. #include <linux/export.h>
  41. #include <linux/string.h>
  42. #include <linux/slab.h>
  43. #include <rdma/ib_verbs.h>
  44. #include <rdma/ib_cache.h>
  45. int ib_rate_to_mult(enum ib_rate rate)
  46. {
  47. switch (rate) {
  48. case IB_RATE_2_5_GBPS: return 1;
  49. case IB_RATE_5_GBPS: return 2;
  50. case IB_RATE_10_GBPS: return 4;
  51. case IB_RATE_20_GBPS: return 8;
  52. case IB_RATE_30_GBPS: return 12;
  53. case IB_RATE_40_GBPS: return 16;
  54. case IB_RATE_60_GBPS: return 24;
  55. case IB_RATE_80_GBPS: return 32;
  56. case IB_RATE_120_GBPS: return 48;
  57. default: return -1;
  58. }
  59. }
  60. EXPORT_SYMBOL(ib_rate_to_mult);
  61. enum ib_rate mult_to_ib_rate(int mult)
  62. {
  63. switch (mult) {
  64. case 1: return IB_RATE_2_5_GBPS;
  65. case 2: return IB_RATE_5_GBPS;
  66. case 4: return IB_RATE_10_GBPS;
  67. case 8: return IB_RATE_20_GBPS;
  68. case 12: return IB_RATE_30_GBPS;
  69. case 16: return IB_RATE_40_GBPS;
  70. case 24: return IB_RATE_60_GBPS;
  71. case 32: return IB_RATE_80_GBPS;
  72. case 48: return IB_RATE_120_GBPS;
  73. default: return IB_RATE_PORT_CURRENT;
  74. }
  75. }
  76. EXPORT_SYMBOL(mult_to_ib_rate);
  77. int ib_rate_to_mbps(enum ib_rate rate)
  78. {
  79. switch (rate) {
  80. case IB_RATE_2_5_GBPS: return 2500;
  81. case IB_RATE_5_GBPS: return 5000;
  82. case IB_RATE_10_GBPS: return 10000;
  83. case IB_RATE_20_GBPS: return 20000;
  84. case IB_RATE_30_GBPS: return 30000;
  85. case IB_RATE_40_GBPS: return 40000;
  86. case IB_RATE_60_GBPS: return 60000;
  87. case IB_RATE_80_GBPS: return 80000;
  88. case IB_RATE_120_GBPS: return 120000;
  89. case IB_RATE_14_GBPS: return 14062;
  90. case IB_RATE_56_GBPS: return 56250;
  91. case IB_RATE_112_GBPS: return 112500;
  92. case IB_RATE_168_GBPS: return 168750;
  93. case IB_RATE_25_GBPS: return 25781;
  94. case IB_RATE_100_GBPS: return 103125;
  95. case IB_RATE_200_GBPS: return 206250;
  96. case IB_RATE_300_GBPS: return 309375;
  97. default: return -1;
  98. }
  99. }
  100. EXPORT_SYMBOL(ib_rate_to_mbps);
  101. enum rdma_transport_type
  102. rdma_node_get_transport(enum rdma_node_type node_type)
  103. {
  104. switch (node_type) {
  105. case RDMA_NODE_IB_CA:
  106. case RDMA_NODE_IB_SWITCH:
  107. case RDMA_NODE_IB_ROUTER:
  108. return RDMA_TRANSPORT_IB;
  109. case RDMA_NODE_RNIC:
  110. return RDMA_TRANSPORT_IWARP;
  111. default:
  112. BUG();
  113. return 0;
  114. }
  115. }
  116. EXPORT_SYMBOL(rdma_node_get_transport);
  117. enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
  118. {
  119. if (device->get_link_layer)
  120. return device->get_link_layer(device, port_num);
  121. switch (rdma_node_get_transport(device->node_type)) {
  122. case RDMA_TRANSPORT_IB:
  123. return IB_LINK_LAYER_INFINIBAND;
  124. case RDMA_TRANSPORT_IWARP:
  125. return IB_LINK_LAYER_ETHERNET;
  126. default:
  127. return IB_LINK_LAYER_UNSPECIFIED;
  128. }
  129. }
  130. EXPORT_SYMBOL(rdma_port_get_link_layer);
  131. /* Protection domains */
  132. struct ib_pd *ib_alloc_pd(struct ib_device *device)
  133. {
  134. struct ib_pd *pd;
  135. pd = device->alloc_pd(device, NULL, NULL);
  136. if (!IS_ERR(pd)) {
  137. pd->device = device;
  138. pd->uobject = NULL;
  139. atomic_set(&pd->usecnt, 0);
  140. }
  141. return pd;
  142. }
  143. EXPORT_SYMBOL(ib_alloc_pd);
  144. int ib_dealloc_pd(struct ib_pd *pd)
  145. {
  146. if (atomic_read(&pd->usecnt))
  147. return -EBUSY;
  148. return pd->device->dealloc_pd(pd);
  149. }
  150. EXPORT_SYMBOL(ib_dealloc_pd);
  151. /* Address handles */
  152. struct ib_ah *ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
  153. {
  154. struct ib_ah *ah;
  155. ah = pd->device->create_ah(pd, ah_attr);
  156. if (!IS_ERR(ah)) {
  157. ah->device = pd->device;
  158. ah->pd = pd;
  159. ah->uobject = NULL;
  160. atomic_inc(&pd->usecnt);
  161. }
  162. return ah;
  163. }
  164. EXPORT_SYMBOL(ib_create_ah);
  165. int ib_init_ah_from_wc(struct ib_device *device, u8 port_num, struct ib_wc *wc,
  166. struct ib_grh *grh, struct ib_ah_attr *ah_attr)
  167. {
  168. u32 flow_class;
  169. u16 gid_index;
  170. int ret;
  171. memset(ah_attr, 0, sizeof *ah_attr);
  172. ah_attr->dlid = wc->slid;
  173. ah_attr->sl = wc->sl;
  174. ah_attr->src_path_bits = wc->dlid_path_bits;
  175. ah_attr->port_num = port_num;
  176. if (wc->wc_flags & IB_WC_GRH) {
  177. ah_attr->ah_flags = IB_AH_GRH;
  178. ah_attr->grh.dgid = grh->sgid;
  179. ret = ib_find_cached_gid(device, &grh->dgid, &port_num,
  180. &gid_index);
  181. if (ret)
  182. return ret;
  183. ah_attr->grh.sgid_index = (u8) gid_index;
  184. flow_class = be32_to_cpu(grh->version_tclass_flow);
  185. ah_attr->grh.flow_label = flow_class & 0xFFFFF;
  186. ah_attr->grh.hop_limit = 0xFF;
  187. ah_attr->grh.traffic_class = (flow_class >> 20) & 0xFF;
  188. }
  189. return 0;
  190. }
  191. EXPORT_SYMBOL(ib_init_ah_from_wc);
  192. struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, struct ib_wc *wc,
  193. struct ib_grh *grh, u8 port_num)
  194. {
  195. struct ib_ah_attr ah_attr;
  196. int ret;
  197. ret = ib_init_ah_from_wc(pd->device, port_num, wc, grh, &ah_attr);
  198. if (ret)
  199. return ERR_PTR(ret);
  200. return ib_create_ah(pd, &ah_attr);
  201. }
  202. EXPORT_SYMBOL(ib_create_ah_from_wc);
  203. int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
  204. {
  205. return ah->device->modify_ah ?
  206. ah->device->modify_ah(ah, ah_attr) :
  207. -ENOSYS;
  208. }
  209. EXPORT_SYMBOL(ib_modify_ah);
  210. int ib_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
  211. {
  212. return ah->device->query_ah ?
  213. ah->device->query_ah(ah, ah_attr) :
  214. -ENOSYS;
  215. }
  216. EXPORT_SYMBOL(ib_query_ah);
  217. int ib_destroy_ah(struct ib_ah *ah)
  218. {
  219. struct ib_pd *pd;
  220. int ret;
  221. pd = ah->pd;
  222. ret = ah->device->destroy_ah(ah);
  223. if (!ret)
  224. atomic_dec(&pd->usecnt);
  225. return ret;
  226. }
  227. EXPORT_SYMBOL(ib_destroy_ah);
  228. /* Shared receive queues */
  229. struct ib_srq *ib_create_srq(struct ib_pd *pd,
  230. struct ib_srq_init_attr *srq_init_attr)
  231. {
  232. struct ib_srq *srq;
  233. if (!pd->device->create_srq)
  234. return ERR_PTR(-ENOSYS);
  235. srq = pd->device->create_srq(pd, srq_init_attr, NULL);
  236. if (!IS_ERR(srq)) {
  237. srq->device = pd->device;
  238. srq->pd = pd;
  239. srq->uobject = NULL;
  240. srq->event_handler = srq_init_attr->event_handler;
  241. srq->srq_context = srq_init_attr->srq_context;
  242. srq->srq_type = srq_init_attr->srq_type;
  243. if (srq->srq_type == IB_SRQT_XRC) {
  244. srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
  245. srq->ext.xrc.cq = srq_init_attr->ext.xrc.cq;
  246. atomic_inc(&srq->ext.xrc.xrcd->usecnt);
  247. atomic_inc(&srq->ext.xrc.cq->usecnt);
  248. }
  249. atomic_inc(&pd->usecnt);
  250. atomic_set(&srq->usecnt, 0);
  251. }
  252. return srq;
  253. }
  254. EXPORT_SYMBOL(ib_create_srq);
  255. int ib_modify_srq(struct ib_srq *srq,
  256. struct ib_srq_attr *srq_attr,
  257. enum ib_srq_attr_mask srq_attr_mask)
  258. {
  259. return srq->device->modify_srq ?
  260. srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
  261. -ENOSYS;
  262. }
  263. EXPORT_SYMBOL(ib_modify_srq);
  264. int ib_query_srq(struct ib_srq *srq,
  265. struct ib_srq_attr *srq_attr)
  266. {
  267. return srq->device->query_srq ?
  268. srq->device->query_srq(srq, srq_attr) : -ENOSYS;
  269. }
  270. EXPORT_SYMBOL(ib_query_srq);
  271. int ib_destroy_srq(struct ib_srq *srq)
  272. {
  273. struct ib_pd *pd;
  274. enum ib_srq_type srq_type;
  275. struct ib_xrcd *uninitialized_var(xrcd);
  276. struct ib_cq *uninitialized_var(cq);
  277. int ret;
  278. if (atomic_read(&srq->usecnt))
  279. return -EBUSY;
  280. pd = srq->pd;
  281. srq_type = srq->srq_type;
  282. if (srq_type == IB_SRQT_XRC) {
  283. xrcd = srq->ext.xrc.xrcd;
  284. cq = srq->ext.xrc.cq;
  285. }
  286. ret = srq->device->destroy_srq(srq);
  287. if (!ret) {
  288. atomic_dec(&pd->usecnt);
  289. if (srq_type == IB_SRQT_XRC) {
  290. atomic_dec(&xrcd->usecnt);
  291. atomic_dec(&cq->usecnt);
  292. }
  293. }
  294. return ret;
  295. }
  296. EXPORT_SYMBOL(ib_destroy_srq);
  297. /* Queue pairs */
  298. static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
  299. {
  300. struct ib_qp *qp = context;
  301. list_for_each_entry(event->element.qp, &qp->open_list, open_list)
  302. event->element.qp->event_handler(event, event->element.qp->qp_context);
  303. }
  304. static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
  305. {
  306. mutex_lock(&xrcd->tgt_qp_mutex);
  307. list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
  308. mutex_unlock(&xrcd->tgt_qp_mutex);
  309. }
  310. static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
  311. void (*event_handler)(struct ib_event *, void *),
  312. void *qp_context)
  313. {
  314. struct ib_qp *qp;
  315. unsigned long flags;
  316. qp = kzalloc(sizeof *qp, GFP_KERNEL);
  317. if (!qp)
  318. return ERR_PTR(-ENOMEM);
  319. qp->real_qp = real_qp;
  320. atomic_inc(&real_qp->usecnt);
  321. qp->device = real_qp->device;
  322. qp->event_handler = event_handler;
  323. qp->qp_context = qp_context;
  324. qp->qp_num = real_qp->qp_num;
  325. qp->qp_type = real_qp->qp_type;
  326. spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
  327. list_add(&qp->open_list, &real_qp->open_list);
  328. spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
  329. return qp;
  330. }
  331. struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
  332. struct ib_qp_open_attr *qp_open_attr)
  333. {
  334. struct ib_qp *qp, *real_qp;
  335. if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
  336. return ERR_PTR(-EINVAL);
  337. qp = ERR_PTR(-EINVAL);
  338. mutex_lock(&xrcd->tgt_qp_mutex);
  339. list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
  340. if (real_qp->qp_num == qp_open_attr->qp_num) {
  341. qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
  342. qp_open_attr->qp_context);
  343. break;
  344. }
  345. }
  346. mutex_unlock(&xrcd->tgt_qp_mutex);
  347. return qp;
  348. }
  349. EXPORT_SYMBOL(ib_open_qp);
  350. struct ib_qp *ib_create_qp(struct ib_pd *pd,
  351. struct ib_qp_init_attr *qp_init_attr)
  352. {
  353. struct ib_qp *qp, *real_qp;
  354. struct ib_device *device;
  355. device = pd ? pd->device : qp_init_attr->xrcd->device;
  356. qp = device->create_qp(pd, qp_init_attr, NULL);
  357. if (!IS_ERR(qp)) {
  358. qp->device = device;
  359. qp->real_qp = qp;
  360. qp->uobject = NULL;
  361. qp->qp_type = qp_init_attr->qp_type;
  362. atomic_set(&qp->usecnt, 0);
  363. if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) {
  364. qp->event_handler = __ib_shared_qp_event_handler;
  365. qp->qp_context = qp;
  366. qp->pd = NULL;
  367. qp->send_cq = qp->recv_cq = NULL;
  368. qp->srq = NULL;
  369. qp->xrcd = qp_init_attr->xrcd;
  370. atomic_inc(&qp_init_attr->xrcd->usecnt);
  371. INIT_LIST_HEAD(&qp->open_list);
  372. real_qp = qp;
  373. qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
  374. qp_init_attr->qp_context);
  375. if (!IS_ERR(qp))
  376. __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
  377. else
  378. real_qp->device->destroy_qp(real_qp);
  379. } else {
  380. qp->event_handler = qp_init_attr->event_handler;
  381. qp->qp_context = qp_init_attr->qp_context;
  382. if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
  383. qp->recv_cq = NULL;
  384. qp->srq = NULL;
  385. } else {
  386. qp->recv_cq = qp_init_attr->recv_cq;
  387. atomic_inc(&qp_init_attr->recv_cq->usecnt);
  388. qp->srq = qp_init_attr->srq;
  389. if (qp->srq)
  390. atomic_inc(&qp_init_attr->srq->usecnt);
  391. }
  392. qp->pd = pd;
  393. qp->send_cq = qp_init_attr->send_cq;
  394. qp->xrcd = NULL;
  395. atomic_inc(&pd->usecnt);
  396. atomic_inc(&qp_init_attr->send_cq->usecnt);
  397. }
  398. }
  399. return qp;
  400. }
  401. EXPORT_SYMBOL(ib_create_qp);
  402. static const struct {
  403. int valid;
  404. enum ib_qp_attr_mask req_param[IB_QPT_MAX];
  405. enum ib_qp_attr_mask opt_param[IB_QPT_MAX];
  406. } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
  407. [IB_QPS_RESET] = {
  408. [IB_QPS_RESET] = { .valid = 1 },
  409. [IB_QPS_INIT] = {
  410. .valid = 1,
  411. .req_param = {
  412. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  413. IB_QP_PORT |
  414. IB_QP_QKEY),
  415. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  416. IB_QP_PORT |
  417. IB_QP_ACCESS_FLAGS),
  418. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  419. IB_QP_PORT |
  420. IB_QP_ACCESS_FLAGS),
  421. [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
  422. IB_QP_PORT |
  423. IB_QP_ACCESS_FLAGS),
  424. [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
  425. IB_QP_PORT |
  426. IB_QP_ACCESS_FLAGS),
  427. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  428. IB_QP_QKEY),
  429. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  430. IB_QP_QKEY),
  431. }
  432. },
  433. },
  434. [IB_QPS_INIT] = {
  435. [IB_QPS_RESET] = { .valid = 1 },
  436. [IB_QPS_ERR] = { .valid = 1 },
  437. [IB_QPS_INIT] = {
  438. .valid = 1,
  439. .opt_param = {
  440. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  441. IB_QP_PORT |
  442. IB_QP_QKEY),
  443. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  444. IB_QP_PORT |
  445. IB_QP_ACCESS_FLAGS),
  446. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  447. IB_QP_PORT |
  448. IB_QP_ACCESS_FLAGS),
  449. [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
  450. IB_QP_PORT |
  451. IB_QP_ACCESS_FLAGS),
  452. [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
  453. IB_QP_PORT |
  454. IB_QP_ACCESS_FLAGS),
  455. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  456. IB_QP_QKEY),
  457. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  458. IB_QP_QKEY),
  459. }
  460. },
  461. [IB_QPS_RTR] = {
  462. .valid = 1,
  463. .req_param = {
  464. [IB_QPT_UC] = (IB_QP_AV |
  465. IB_QP_PATH_MTU |
  466. IB_QP_DEST_QPN |
  467. IB_QP_RQ_PSN),
  468. [IB_QPT_RC] = (IB_QP_AV |
  469. IB_QP_PATH_MTU |
  470. IB_QP_DEST_QPN |
  471. IB_QP_RQ_PSN |
  472. IB_QP_MAX_DEST_RD_ATOMIC |
  473. IB_QP_MIN_RNR_TIMER),
  474. [IB_QPT_XRC_INI] = (IB_QP_AV |
  475. IB_QP_PATH_MTU |
  476. IB_QP_DEST_QPN |
  477. IB_QP_RQ_PSN),
  478. [IB_QPT_XRC_TGT] = (IB_QP_AV |
  479. IB_QP_PATH_MTU |
  480. IB_QP_DEST_QPN |
  481. IB_QP_RQ_PSN |
  482. IB_QP_MAX_DEST_RD_ATOMIC |
  483. IB_QP_MIN_RNR_TIMER),
  484. },
  485. .opt_param = {
  486. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  487. IB_QP_QKEY),
  488. [IB_QPT_UC] = (IB_QP_ALT_PATH |
  489. IB_QP_ACCESS_FLAGS |
  490. IB_QP_PKEY_INDEX),
  491. [IB_QPT_RC] = (IB_QP_ALT_PATH |
  492. IB_QP_ACCESS_FLAGS |
  493. IB_QP_PKEY_INDEX),
  494. [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH |
  495. IB_QP_ACCESS_FLAGS |
  496. IB_QP_PKEY_INDEX),
  497. [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH |
  498. IB_QP_ACCESS_FLAGS |
  499. IB_QP_PKEY_INDEX),
  500. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  501. IB_QP_QKEY),
  502. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  503. IB_QP_QKEY),
  504. }
  505. }
  506. },
  507. [IB_QPS_RTR] = {
  508. [IB_QPS_RESET] = { .valid = 1 },
  509. [IB_QPS_ERR] = { .valid = 1 },
  510. [IB_QPS_RTS] = {
  511. .valid = 1,
  512. .req_param = {
  513. [IB_QPT_UD] = IB_QP_SQ_PSN,
  514. [IB_QPT_UC] = IB_QP_SQ_PSN,
  515. [IB_QPT_RC] = (IB_QP_TIMEOUT |
  516. IB_QP_RETRY_CNT |
  517. IB_QP_RNR_RETRY |
  518. IB_QP_SQ_PSN |
  519. IB_QP_MAX_QP_RD_ATOMIC),
  520. [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT |
  521. IB_QP_RETRY_CNT |
  522. IB_QP_RNR_RETRY |
  523. IB_QP_SQ_PSN |
  524. IB_QP_MAX_QP_RD_ATOMIC),
  525. [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT |
  526. IB_QP_SQ_PSN),
  527. [IB_QPT_SMI] = IB_QP_SQ_PSN,
  528. [IB_QPT_GSI] = IB_QP_SQ_PSN,
  529. },
  530. .opt_param = {
  531. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  532. IB_QP_QKEY),
  533. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  534. IB_QP_ALT_PATH |
  535. IB_QP_ACCESS_FLAGS |
  536. IB_QP_PATH_MIG_STATE),
  537. [IB_QPT_RC] = (IB_QP_CUR_STATE |
  538. IB_QP_ALT_PATH |
  539. IB_QP_ACCESS_FLAGS |
  540. IB_QP_MIN_RNR_TIMER |
  541. IB_QP_PATH_MIG_STATE),
  542. [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
  543. IB_QP_ALT_PATH |
  544. IB_QP_ACCESS_FLAGS |
  545. IB_QP_PATH_MIG_STATE),
  546. [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
  547. IB_QP_ALT_PATH |
  548. IB_QP_ACCESS_FLAGS |
  549. IB_QP_MIN_RNR_TIMER |
  550. IB_QP_PATH_MIG_STATE),
  551. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  552. IB_QP_QKEY),
  553. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  554. IB_QP_QKEY),
  555. }
  556. }
  557. },
  558. [IB_QPS_RTS] = {
  559. [IB_QPS_RESET] = { .valid = 1 },
  560. [IB_QPS_ERR] = { .valid = 1 },
  561. [IB_QPS_RTS] = {
  562. .valid = 1,
  563. .opt_param = {
  564. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  565. IB_QP_QKEY),
  566. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  567. IB_QP_ACCESS_FLAGS |
  568. IB_QP_ALT_PATH |
  569. IB_QP_PATH_MIG_STATE),
  570. [IB_QPT_RC] = (IB_QP_CUR_STATE |
  571. IB_QP_ACCESS_FLAGS |
  572. IB_QP_ALT_PATH |
  573. IB_QP_PATH_MIG_STATE |
  574. IB_QP_MIN_RNR_TIMER),
  575. [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
  576. IB_QP_ACCESS_FLAGS |
  577. IB_QP_ALT_PATH |
  578. IB_QP_PATH_MIG_STATE),
  579. [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
  580. IB_QP_ACCESS_FLAGS |
  581. IB_QP_ALT_PATH |
  582. IB_QP_PATH_MIG_STATE |
  583. IB_QP_MIN_RNR_TIMER),
  584. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  585. IB_QP_QKEY),
  586. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  587. IB_QP_QKEY),
  588. }
  589. },
  590. [IB_QPS_SQD] = {
  591. .valid = 1,
  592. .opt_param = {
  593. [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  594. [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  595. [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  596. [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  597. [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
  598. [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  599. [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
  600. }
  601. },
  602. },
  603. [IB_QPS_SQD] = {
  604. [IB_QPS_RESET] = { .valid = 1 },
  605. [IB_QPS_ERR] = { .valid = 1 },
  606. [IB_QPS_RTS] = {
  607. .valid = 1,
  608. .opt_param = {
  609. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  610. IB_QP_QKEY),
  611. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  612. IB_QP_ALT_PATH |
  613. IB_QP_ACCESS_FLAGS |
  614. IB_QP_PATH_MIG_STATE),
  615. [IB_QPT_RC] = (IB_QP_CUR_STATE |
  616. IB_QP_ALT_PATH |
  617. IB_QP_ACCESS_FLAGS |
  618. IB_QP_MIN_RNR_TIMER |
  619. IB_QP_PATH_MIG_STATE),
  620. [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
  621. IB_QP_ALT_PATH |
  622. IB_QP_ACCESS_FLAGS |
  623. IB_QP_PATH_MIG_STATE),
  624. [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
  625. IB_QP_ALT_PATH |
  626. IB_QP_ACCESS_FLAGS |
  627. IB_QP_MIN_RNR_TIMER |
  628. IB_QP_PATH_MIG_STATE),
  629. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  630. IB_QP_QKEY),
  631. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  632. IB_QP_QKEY),
  633. }
  634. },
  635. [IB_QPS_SQD] = {
  636. .valid = 1,
  637. .opt_param = {
  638. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  639. IB_QP_QKEY),
  640. [IB_QPT_UC] = (IB_QP_AV |
  641. IB_QP_ALT_PATH |
  642. IB_QP_ACCESS_FLAGS |
  643. IB_QP_PKEY_INDEX |
  644. IB_QP_PATH_MIG_STATE),
  645. [IB_QPT_RC] = (IB_QP_PORT |
  646. IB_QP_AV |
  647. IB_QP_TIMEOUT |
  648. IB_QP_RETRY_CNT |
  649. IB_QP_RNR_RETRY |
  650. IB_QP_MAX_QP_RD_ATOMIC |
  651. IB_QP_MAX_DEST_RD_ATOMIC |
  652. IB_QP_ALT_PATH |
  653. IB_QP_ACCESS_FLAGS |
  654. IB_QP_PKEY_INDEX |
  655. IB_QP_MIN_RNR_TIMER |
  656. IB_QP_PATH_MIG_STATE),
  657. [IB_QPT_XRC_INI] = (IB_QP_PORT |
  658. IB_QP_AV |
  659. IB_QP_TIMEOUT |
  660. IB_QP_RETRY_CNT |
  661. IB_QP_RNR_RETRY |
  662. IB_QP_MAX_QP_RD_ATOMIC |
  663. IB_QP_ALT_PATH |
  664. IB_QP_ACCESS_FLAGS |
  665. IB_QP_PKEY_INDEX |
  666. IB_QP_PATH_MIG_STATE),
  667. [IB_QPT_XRC_TGT] = (IB_QP_PORT |
  668. IB_QP_AV |
  669. IB_QP_TIMEOUT |
  670. IB_QP_MAX_DEST_RD_ATOMIC |
  671. IB_QP_ALT_PATH |
  672. IB_QP_ACCESS_FLAGS |
  673. IB_QP_PKEY_INDEX |
  674. IB_QP_MIN_RNR_TIMER |
  675. IB_QP_PATH_MIG_STATE),
  676. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  677. IB_QP_QKEY),
  678. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  679. IB_QP_QKEY),
  680. }
  681. }
  682. },
  683. [IB_QPS_SQE] = {
  684. [IB_QPS_RESET] = { .valid = 1 },
  685. [IB_QPS_ERR] = { .valid = 1 },
  686. [IB_QPS_RTS] = {
  687. .valid = 1,
  688. .opt_param = {
  689. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  690. IB_QP_QKEY),
  691. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  692. IB_QP_ACCESS_FLAGS),
  693. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  694. IB_QP_QKEY),
  695. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  696. IB_QP_QKEY),
  697. }
  698. }
  699. },
  700. [IB_QPS_ERR] = {
  701. [IB_QPS_RESET] = { .valid = 1 },
  702. [IB_QPS_ERR] = { .valid = 1 }
  703. }
  704. };
  705. int ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
  706. enum ib_qp_type type, enum ib_qp_attr_mask mask)
  707. {
  708. enum ib_qp_attr_mask req_param, opt_param;
  709. if (cur_state < 0 || cur_state > IB_QPS_ERR ||
  710. next_state < 0 || next_state > IB_QPS_ERR)
  711. return 0;
  712. if (mask & IB_QP_CUR_STATE &&
  713. cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
  714. cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
  715. return 0;
  716. if (!qp_state_table[cur_state][next_state].valid)
  717. return 0;
  718. req_param = qp_state_table[cur_state][next_state].req_param[type];
  719. opt_param = qp_state_table[cur_state][next_state].opt_param[type];
  720. if ((mask & req_param) != req_param)
  721. return 0;
  722. if (mask & ~(req_param | opt_param | IB_QP_STATE))
  723. return 0;
  724. return 1;
  725. }
  726. EXPORT_SYMBOL(ib_modify_qp_is_ok);
  727. int ib_modify_qp(struct ib_qp *qp,
  728. struct ib_qp_attr *qp_attr,
  729. int qp_attr_mask)
  730. {
  731. return qp->device->modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
  732. }
  733. EXPORT_SYMBOL(ib_modify_qp);
  734. int ib_query_qp(struct ib_qp *qp,
  735. struct ib_qp_attr *qp_attr,
  736. int qp_attr_mask,
  737. struct ib_qp_init_attr *qp_init_attr)
  738. {
  739. return qp->device->query_qp ?
  740. qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
  741. -ENOSYS;
  742. }
  743. EXPORT_SYMBOL(ib_query_qp);
  744. int ib_close_qp(struct ib_qp *qp)
  745. {
  746. struct ib_qp *real_qp;
  747. unsigned long flags;
  748. real_qp = qp->real_qp;
  749. if (real_qp == qp)
  750. return -EINVAL;
  751. spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
  752. list_del(&qp->open_list);
  753. spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
  754. atomic_dec(&real_qp->usecnt);
  755. kfree(qp);
  756. return 0;
  757. }
  758. EXPORT_SYMBOL(ib_close_qp);
  759. static int __ib_destroy_shared_qp(struct ib_qp *qp)
  760. {
  761. struct ib_xrcd *xrcd;
  762. struct ib_qp *real_qp;
  763. int ret;
  764. real_qp = qp->real_qp;
  765. xrcd = real_qp->xrcd;
  766. mutex_lock(&xrcd->tgt_qp_mutex);
  767. ib_close_qp(qp);
  768. if (atomic_read(&real_qp->usecnt) == 0)
  769. list_del(&real_qp->xrcd_list);
  770. else
  771. real_qp = NULL;
  772. mutex_unlock(&xrcd->tgt_qp_mutex);
  773. if (real_qp) {
  774. ret = ib_destroy_qp(real_qp);
  775. if (!ret)
  776. atomic_dec(&xrcd->usecnt);
  777. else
  778. __ib_insert_xrcd_qp(xrcd, real_qp);
  779. }
  780. return 0;
  781. }
  782. int ib_destroy_qp(struct ib_qp *qp)
  783. {
  784. struct ib_pd *pd;
  785. struct ib_cq *scq, *rcq;
  786. struct ib_srq *srq;
  787. int ret;
  788. if (atomic_read(&qp->usecnt))
  789. return -EBUSY;
  790. if (qp->real_qp != qp)
  791. return __ib_destroy_shared_qp(qp);
  792. pd = qp->pd;
  793. scq = qp->send_cq;
  794. rcq = qp->recv_cq;
  795. srq = qp->srq;
  796. ret = qp->device->destroy_qp(qp);
  797. if (!ret) {
  798. if (pd)
  799. atomic_dec(&pd->usecnt);
  800. if (scq)
  801. atomic_dec(&scq->usecnt);
  802. if (rcq)
  803. atomic_dec(&rcq->usecnt);
  804. if (srq)
  805. atomic_dec(&srq->usecnt);
  806. }
  807. return ret;
  808. }
  809. EXPORT_SYMBOL(ib_destroy_qp);
  810. /* Completion queues */
  811. struct ib_cq *ib_create_cq(struct ib_device *device,
  812. ib_comp_handler comp_handler,
  813. void (*event_handler)(struct ib_event *, void *),
  814. void *cq_context, int cqe, int comp_vector)
  815. {
  816. struct ib_cq *cq;
  817. cq = device->create_cq(device, cqe, comp_vector, NULL, NULL);
  818. if (!IS_ERR(cq)) {
  819. cq->device = device;
  820. cq->uobject = NULL;
  821. cq->comp_handler = comp_handler;
  822. cq->event_handler = event_handler;
  823. cq->cq_context = cq_context;
  824. atomic_set(&cq->usecnt, 0);
  825. }
  826. return cq;
  827. }
  828. EXPORT_SYMBOL(ib_create_cq);
  829. int ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
  830. {
  831. return cq->device->modify_cq ?
  832. cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS;
  833. }
  834. EXPORT_SYMBOL(ib_modify_cq);
  835. int ib_destroy_cq(struct ib_cq *cq)
  836. {
  837. if (atomic_read(&cq->usecnt))
  838. return -EBUSY;
  839. return cq->device->destroy_cq(cq);
  840. }
  841. EXPORT_SYMBOL(ib_destroy_cq);
  842. int ib_resize_cq(struct ib_cq *cq, int cqe)
  843. {
  844. return cq->device->resize_cq ?
  845. cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS;
  846. }
  847. EXPORT_SYMBOL(ib_resize_cq);
  848. /* Memory regions */
  849. struct ib_mr *ib_get_dma_mr(struct ib_pd *pd, int mr_access_flags)
  850. {
  851. struct ib_mr *mr;
  852. mr = pd->device->get_dma_mr(pd, mr_access_flags);
  853. if (!IS_ERR(mr)) {
  854. mr->device = pd->device;
  855. mr->pd = pd;
  856. mr->uobject = NULL;
  857. atomic_inc(&pd->usecnt);
  858. atomic_set(&mr->usecnt, 0);
  859. }
  860. return mr;
  861. }
  862. EXPORT_SYMBOL(ib_get_dma_mr);
  863. struct ib_mr *ib_reg_phys_mr(struct ib_pd *pd,
  864. struct ib_phys_buf *phys_buf_array,
  865. int num_phys_buf,
  866. int mr_access_flags,
  867. u64 *iova_start)
  868. {
  869. struct ib_mr *mr;
  870. if (!pd->device->reg_phys_mr)
  871. return ERR_PTR(-ENOSYS);
  872. mr = pd->device->reg_phys_mr(pd, phys_buf_array, num_phys_buf,
  873. mr_access_flags, iova_start);
  874. if (!IS_ERR(mr)) {
  875. mr->device = pd->device;
  876. mr->pd = pd;
  877. mr->uobject = NULL;
  878. atomic_inc(&pd->usecnt);
  879. atomic_set(&mr->usecnt, 0);
  880. }
  881. return mr;
  882. }
  883. EXPORT_SYMBOL(ib_reg_phys_mr);
  884. int ib_rereg_phys_mr(struct ib_mr *mr,
  885. int mr_rereg_mask,
  886. struct ib_pd *pd,
  887. struct ib_phys_buf *phys_buf_array,
  888. int num_phys_buf,
  889. int mr_access_flags,
  890. u64 *iova_start)
  891. {
  892. struct ib_pd *old_pd;
  893. int ret;
  894. if (!mr->device->rereg_phys_mr)
  895. return -ENOSYS;
  896. if (atomic_read(&mr->usecnt))
  897. return -EBUSY;
  898. old_pd = mr->pd;
  899. ret = mr->device->rereg_phys_mr(mr, mr_rereg_mask, pd,
  900. phys_buf_array, num_phys_buf,
  901. mr_access_flags, iova_start);
  902. if (!ret && (mr_rereg_mask & IB_MR_REREG_PD)) {
  903. atomic_dec(&old_pd->usecnt);
  904. atomic_inc(&pd->usecnt);
  905. }
  906. return ret;
  907. }
  908. EXPORT_SYMBOL(ib_rereg_phys_mr);
  909. int ib_query_mr(struct ib_mr *mr, struct ib_mr_attr *mr_attr)
  910. {
  911. return mr->device->query_mr ?
  912. mr->device->query_mr(mr, mr_attr) : -ENOSYS;
  913. }
  914. EXPORT_SYMBOL(ib_query_mr);
  915. int ib_dereg_mr(struct ib_mr *mr)
  916. {
  917. struct ib_pd *pd;
  918. int ret;
  919. if (atomic_read(&mr->usecnt))
  920. return -EBUSY;
  921. pd = mr->pd;
  922. ret = mr->device->dereg_mr(mr);
  923. if (!ret)
  924. atomic_dec(&pd->usecnt);
  925. return ret;
  926. }
  927. EXPORT_SYMBOL(ib_dereg_mr);
  928. struct ib_mr *ib_alloc_fast_reg_mr(struct ib_pd *pd, int max_page_list_len)
  929. {
  930. struct ib_mr *mr;
  931. if (!pd->device->alloc_fast_reg_mr)
  932. return ERR_PTR(-ENOSYS);
  933. mr = pd->device->alloc_fast_reg_mr(pd, max_page_list_len);
  934. if (!IS_ERR(mr)) {
  935. mr->device = pd->device;
  936. mr->pd = pd;
  937. mr->uobject = NULL;
  938. atomic_inc(&pd->usecnt);
  939. atomic_set(&mr->usecnt, 0);
  940. }
  941. return mr;
  942. }
  943. EXPORT_SYMBOL(ib_alloc_fast_reg_mr);
  944. struct ib_fast_reg_page_list *ib_alloc_fast_reg_page_list(struct ib_device *device,
  945. int max_page_list_len)
  946. {
  947. struct ib_fast_reg_page_list *page_list;
  948. if (!device->alloc_fast_reg_page_list)
  949. return ERR_PTR(-ENOSYS);
  950. page_list = device->alloc_fast_reg_page_list(device, max_page_list_len);
  951. if (!IS_ERR(page_list)) {
  952. page_list->device = device;
  953. page_list->max_page_list_len = max_page_list_len;
  954. }
  955. return page_list;
  956. }
  957. EXPORT_SYMBOL(ib_alloc_fast_reg_page_list);
  958. void ib_free_fast_reg_page_list(struct ib_fast_reg_page_list *page_list)
  959. {
  960. page_list->device->free_fast_reg_page_list(page_list);
  961. }
  962. EXPORT_SYMBOL(ib_free_fast_reg_page_list);
  963. /* Memory windows */
  964. struct ib_mw *ib_alloc_mw(struct ib_pd *pd)
  965. {
  966. struct ib_mw *mw;
  967. if (!pd->device->alloc_mw)
  968. return ERR_PTR(-ENOSYS);
  969. mw = pd->device->alloc_mw(pd);
  970. if (!IS_ERR(mw)) {
  971. mw->device = pd->device;
  972. mw->pd = pd;
  973. mw->uobject = NULL;
  974. atomic_inc(&pd->usecnt);
  975. }
  976. return mw;
  977. }
  978. EXPORT_SYMBOL(ib_alloc_mw);
  979. int ib_dealloc_mw(struct ib_mw *mw)
  980. {
  981. struct ib_pd *pd;
  982. int ret;
  983. pd = mw->pd;
  984. ret = mw->device->dealloc_mw(mw);
  985. if (!ret)
  986. atomic_dec(&pd->usecnt);
  987. return ret;
  988. }
  989. EXPORT_SYMBOL(ib_dealloc_mw);
  990. /* "Fast" memory regions */
  991. struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
  992. int mr_access_flags,
  993. struct ib_fmr_attr *fmr_attr)
  994. {
  995. struct ib_fmr *fmr;
  996. if (!pd->device->alloc_fmr)
  997. return ERR_PTR(-ENOSYS);
  998. fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
  999. if (!IS_ERR(fmr)) {
  1000. fmr->device = pd->device;
  1001. fmr->pd = pd;
  1002. atomic_inc(&pd->usecnt);
  1003. }
  1004. return fmr;
  1005. }
  1006. EXPORT_SYMBOL(ib_alloc_fmr);
  1007. int ib_unmap_fmr(struct list_head *fmr_list)
  1008. {
  1009. struct ib_fmr *fmr;
  1010. if (list_empty(fmr_list))
  1011. return 0;
  1012. fmr = list_entry(fmr_list->next, struct ib_fmr, list);
  1013. return fmr->device->unmap_fmr(fmr_list);
  1014. }
  1015. EXPORT_SYMBOL(ib_unmap_fmr);
  1016. int ib_dealloc_fmr(struct ib_fmr *fmr)
  1017. {
  1018. struct ib_pd *pd;
  1019. int ret;
  1020. pd = fmr->pd;
  1021. ret = fmr->device->dealloc_fmr(fmr);
  1022. if (!ret)
  1023. atomic_dec(&pd->usecnt);
  1024. return ret;
  1025. }
  1026. EXPORT_SYMBOL(ib_dealloc_fmr);
  1027. /* Multicast groups */
  1028. int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
  1029. {
  1030. if (!qp->device->attach_mcast)
  1031. return -ENOSYS;
  1032. if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
  1033. return -EINVAL;
  1034. return qp->device->attach_mcast(qp, gid, lid);
  1035. }
  1036. EXPORT_SYMBOL(ib_attach_mcast);
  1037. int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
  1038. {
  1039. if (!qp->device->detach_mcast)
  1040. return -ENOSYS;
  1041. if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
  1042. return -EINVAL;
  1043. return qp->device->detach_mcast(qp, gid, lid);
  1044. }
  1045. EXPORT_SYMBOL(ib_detach_mcast);
  1046. struct ib_xrcd *ib_alloc_xrcd(struct ib_device *device)
  1047. {
  1048. struct ib_xrcd *xrcd;
  1049. if (!device->alloc_xrcd)
  1050. return ERR_PTR(-ENOSYS);
  1051. xrcd = device->alloc_xrcd(device, NULL, NULL);
  1052. if (!IS_ERR(xrcd)) {
  1053. xrcd->device = device;
  1054. xrcd->inode = NULL;
  1055. atomic_set(&xrcd->usecnt, 0);
  1056. mutex_init(&xrcd->tgt_qp_mutex);
  1057. INIT_LIST_HEAD(&xrcd->tgt_qp_list);
  1058. }
  1059. return xrcd;
  1060. }
  1061. EXPORT_SYMBOL(ib_alloc_xrcd);
  1062. int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
  1063. {
  1064. struct ib_qp *qp;
  1065. int ret;
  1066. if (atomic_read(&xrcd->usecnt))
  1067. return -EBUSY;
  1068. while (!list_empty(&xrcd->tgt_qp_list)) {
  1069. qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
  1070. ret = ib_destroy_qp(qp);
  1071. if (ret)
  1072. return ret;
  1073. }
  1074. return xrcd->device->dealloc_xrcd(xrcd);
  1075. }
  1076. EXPORT_SYMBOL(ib_dealloc_xrcd);