svc_rdma_transport.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the BSD-type
  8. * license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials provided
  20. * with the distribution.
  21. *
  22. * Neither the name of the Network Appliance, Inc. nor the names of
  23. * its contributors may be used to endorse or promote products
  24. * derived from this software without specific prior written
  25. * permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * Author: Tom Tucker <tom@opengridcomputing.com>
  40. */
  41. #include <linux/sunrpc/svc_xprt.h>
  42. #include <linux/sunrpc/debug.h>
  43. #include <linux/sunrpc/rpc_rdma.h>
  44. #include <linux/spinlock.h>
  45. #include <rdma/ib_verbs.h>
  46. #include <rdma/rdma_cm.h>
  47. #include <linux/sunrpc/svc_rdma.h>
  48. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  49. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  50. struct sockaddr *sa, int salen,
  51. int flags);
  52. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
  53. static void svc_rdma_release_rqst(struct svc_rqst *);
  54. static void dto_tasklet_func(unsigned long data);
  55. static void svc_rdma_detach(struct svc_xprt *xprt);
  56. static void svc_rdma_free(struct svc_xprt *xprt);
  57. static int svc_rdma_has_wspace(struct svc_xprt *xprt);
  58. static void rq_cq_reap(struct svcxprt_rdma *xprt);
  59. static void sq_cq_reap(struct svcxprt_rdma *xprt);
  60. DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
  61. static DEFINE_SPINLOCK(dto_lock);
  62. static LIST_HEAD(dto_xprt_q);
  63. static struct svc_xprt_ops svc_rdma_ops = {
  64. .xpo_create = svc_rdma_create,
  65. .xpo_recvfrom = svc_rdma_recvfrom,
  66. .xpo_sendto = svc_rdma_sendto,
  67. .xpo_release_rqst = svc_rdma_release_rqst,
  68. .xpo_detach = svc_rdma_detach,
  69. .xpo_free = svc_rdma_free,
  70. .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
  71. .xpo_has_wspace = svc_rdma_has_wspace,
  72. .xpo_accept = svc_rdma_accept,
  73. };
  74. struct svc_xprt_class svc_rdma_class = {
  75. .xcl_name = "rdma",
  76. .xcl_owner = THIS_MODULE,
  77. .xcl_ops = &svc_rdma_ops,
  78. .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
  79. };
  80. static int rdma_bump_context_cache(struct svcxprt_rdma *xprt)
  81. {
  82. int target;
  83. int at_least_one = 0;
  84. struct svc_rdma_op_ctxt *ctxt;
  85. target = min(xprt->sc_ctxt_cnt + xprt->sc_ctxt_bump,
  86. xprt->sc_ctxt_max);
  87. spin_lock_bh(&xprt->sc_ctxt_lock);
  88. while (xprt->sc_ctxt_cnt < target) {
  89. xprt->sc_ctxt_cnt++;
  90. spin_unlock_bh(&xprt->sc_ctxt_lock);
  91. ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
  92. spin_lock_bh(&xprt->sc_ctxt_lock);
  93. if (ctxt) {
  94. at_least_one = 1;
  95. INIT_LIST_HEAD(&ctxt->free_list);
  96. list_add(&ctxt->free_list, &xprt->sc_ctxt_free);
  97. } else {
  98. /* kmalloc failed...give up for now */
  99. xprt->sc_ctxt_cnt--;
  100. break;
  101. }
  102. }
  103. spin_unlock_bh(&xprt->sc_ctxt_lock);
  104. dprintk("svcrdma: sc_ctxt_max=%d, sc_ctxt_cnt=%d\n",
  105. xprt->sc_ctxt_max, xprt->sc_ctxt_cnt);
  106. return at_least_one;
  107. }
  108. struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
  109. {
  110. struct svc_rdma_op_ctxt *ctxt;
  111. while (1) {
  112. spin_lock_bh(&xprt->sc_ctxt_lock);
  113. if (unlikely(list_empty(&xprt->sc_ctxt_free))) {
  114. /* Try to bump my cache. */
  115. spin_unlock_bh(&xprt->sc_ctxt_lock);
  116. if (rdma_bump_context_cache(xprt))
  117. continue;
  118. printk(KERN_INFO "svcrdma: sleeping waiting for "
  119. "context memory on xprt=%p\n",
  120. xprt);
  121. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  122. continue;
  123. }
  124. ctxt = list_entry(xprt->sc_ctxt_free.next,
  125. struct svc_rdma_op_ctxt,
  126. free_list);
  127. list_del_init(&ctxt->free_list);
  128. spin_unlock_bh(&xprt->sc_ctxt_lock);
  129. ctxt->xprt = xprt;
  130. INIT_LIST_HEAD(&ctxt->dto_q);
  131. ctxt->count = 0;
  132. atomic_inc(&xprt->sc_ctxt_used);
  133. break;
  134. }
  135. return ctxt;
  136. }
  137. void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
  138. {
  139. struct svcxprt_rdma *xprt;
  140. int i;
  141. BUG_ON(!ctxt);
  142. xprt = ctxt->xprt;
  143. if (free_pages)
  144. for (i = 0; i < ctxt->count; i++)
  145. put_page(ctxt->pages[i]);
  146. for (i = 0; i < ctxt->count; i++)
  147. dma_unmap_single(xprt->sc_cm_id->device->dma_device,
  148. ctxt->sge[i].addr,
  149. ctxt->sge[i].length,
  150. ctxt->direction);
  151. spin_lock_bh(&xprt->sc_ctxt_lock);
  152. list_add(&ctxt->free_list, &xprt->sc_ctxt_free);
  153. spin_unlock_bh(&xprt->sc_ctxt_lock);
  154. atomic_dec(&xprt->sc_ctxt_used);
  155. }
  156. /* ib_cq event handler */
  157. static void cq_event_handler(struct ib_event *event, void *context)
  158. {
  159. struct svc_xprt *xprt = context;
  160. dprintk("svcrdma: received CQ event id=%d, context=%p\n",
  161. event->event, context);
  162. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  163. }
  164. /* QP event handler */
  165. static void qp_event_handler(struct ib_event *event, void *context)
  166. {
  167. struct svc_xprt *xprt = context;
  168. switch (event->event) {
  169. /* These are considered benign events */
  170. case IB_EVENT_PATH_MIG:
  171. case IB_EVENT_COMM_EST:
  172. case IB_EVENT_SQ_DRAINED:
  173. case IB_EVENT_QP_LAST_WQE_REACHED:
  174. dprintk("svcrdma: QP event %d received for QP=%p\n",
  175. event->event, event->element.qp);
  176. break;
  177. /* These are considered fatal events */
  178. case IB_EVENT_PATH_MIG_ERR:
  179. case IB_EVENT_QP_FATAL:
  180. case IB_EVENT_QP_REQ_ERR:
  181. case IB_EVENT_QP_ACCESS_ERR:
  182. case IB_EVENT_DEVICE_FATAL:
  183. default:
  184. dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
  185. "closing transport\n",
  186. event->event, event->element.qp);
  187. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  188. break;
  189. }
  190. }
  191. /*
  192. * Data Transfer Operation Tasklet
  193. *
  194. * Walks a list of transports with I/O pending, removing entries as
  195. * they are added to the server's I/O pending list. Two bits indicate
  196. * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
  197. * spinlock that serializes access to the transport list with the RQ
  198. * and SQ interrupt handlers.
  199. */
  200. static void dto_tasklet_func(unsigned long data)
  201. {
  202. struct svcxprt_rdma *xprt;
  203. unsigned long flags;
  204. spin_lock_irqsave(&dto_lock, flags);
  205. while (!list_empty(&dto_xprt_q)) {
  206. xprt = list_entry(dto_xprt_q.next,
  207. struct svcxprt_rdma, sc_dto_q);
  208. list_del_init(&xprt->sc_dto_q);
  209. spin_unlock_irqrestore(&dto_lock, flags);
  210. rq_cq_reap(xprt);
  211. sq_cq_reap(xprt);
  212. svc_xprt_put(&xprt->sc_xprt);
  213. spin_lock_irqsave(&dto_lock, flags);
  214. }
  215. spin_unlock_irqrestore(&dto_lock, flags);
  216. }
  217. /*
  218. * Receive Queue Completion Handler
  219. *
  220. * Since an RQ completion handler is called on interrupt context, we
  221. * need to defer the handling of the I/O to a tasklet
  222. */
  223. static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
  224. {
  225. struct svcxprt_rdma *xprt = cq_context;
  226. unsigned long flags;
  227. /*
  228. * Set the bit regardless of whether or not it's on the list
  229. * because it may be on the list already due to an SQ
  230. * completion.
  231. */
  232. set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
  233. /*
  234. * If this transport is not already on the DTO transport queue,
  235. * add it
  236. */
  237. spin_lock_irqsave(&dto_lock, flags);
  238. if (list_empty(&xprt->sc_dto_q)) {
  239. svc_xprt_get(&xprt->sc_xprt);
  240. list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
  241. }
  242. spin_unlock_irqrestore(&dto_lock, flags);
  243. /* Tasklet does all the work to avoid irqsave locks. */
  244. tasklet_schedule(&dto_tasklet);
  245. }
  246. /*
  247. * rq_cq_reap - Process the RQ CQ.
  248. *
  249. * Take all completing WC off the CQE and enqueue the associated DTO
  250. * context on the dto_q for the transport.
  251. */
  252. static void rq_cq_reap(struct svcxprt_rdma *xprt)
  253. {
  254. int ret;
  255. struct ib_wc wc;
  256. struct svc_rdma_op_ctxt *ctxt = NULL;
  257. if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
  258. return;
  259. ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
  260. atomic_inc(&rdma_stat_rq_poll);
  261. while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
  262. ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
  263. ctxt->wc_status = wc.status;
  264. ctxt->byte_len = wc.byte_len;
  265. if (wc.status != IB_WC_SUCCESS) {
  266. /* Close the transport */
  267. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  268. svc_rdma_put_context(ctxt, 1);
  269. continue;
  270. }
  271. spin_lock_bh(&xprt->sc_rq_dto_lock);
  272. list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
  273. spin_unlock_bh(&xprt->sc_rq_dto_lock);
  274. }
  275. if (ctxt)
  276. atomic_inc(&rdma_stat_rq_prod);
  277. set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
  278. /*
  279. * If data arrived before established event,
  280. * don't enqueue. This defers RPC I/O until the
  281. * RDMA connection is complete.
  282. */
  283. if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
  284. svc_xprt_enqueue(&xprt->sc_xprt);
  285. }
  286. /*
  287. * Send Queue Completion Handler - potentially called on interrupt context.
  288. */
  289. static void sq_cq_reap(struct svcxprt_rdma *xprt)
  290. {
  291. struct svc_rdma_op_ctxt *ctxt = NULL;
  292. struct ib_wc wc;
  293. struct ib_cq *cq = xprt->sc_sq_cq;
  294. int ret;
  295. if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
  296. return;
  297. ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  298. atomic_inc(&rdma_stat_sq_poll);
  299. while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
  300. ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
  301. xprt = ctxt->xprt;
  302. if (wc.status != IB_WC_SUCCESS)
  303. /* Close the transport */
  304. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  305. /* Decrement used SQ WR count */
  306. atomic_dec(&xprt->sc_sq_count);
  307. wake_up(&xprt->sc_send_wait);
  308. switch (ctxt->wr_op) {
  309. case IB_WR_SEND:
  310. case IB_WR_RDMA_WRITE:
  311. svc_rdma_put_context(ctxt, 1);
  312. break;
  313. case IB_WR_RDMA_READ:
  314. if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
  315. struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
  316. BUG_ON(!read_hdr);
  317. set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
  318. spin_lock_bh(&xprt->sc_read_complete_lock);
  319. list_add_tail(&read_hdr->dto_q,
  320. &xprt->sc_read_complete_q);
  321. spin_unlock_bh(&xprt->sc_read_complete_lock);
  322. svc_xprt_enqueue(&xprt->sc_xprt);
  323. }
  324. svc_rdma_put_context(ctxt, 0);
  325. break;
  326. default:
  327. printk(KERN_ERR "svcrdma: unexpected completion type, "
  328. "opcode=%d, status=%d\n",
  329. wc.opcode, wc.status);
  330. break;
  331. }
  332. }
  333. if (ctxt)
  334. atomic_inc(&rdma_stat_sq_prod);
  335. }
  336. static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
  337. {
  338. struct svcxprt_rdma *xprt = cq_context;
  339. unsigned long flags;
  340. /*
  341. * Set the bit regardless of whether or not it's on the list
  342. * because it may be on the list already due to an RQ
  343. * completion.
  344. */
  345. set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
  346. /*
  347. * If this transport is not already on the DTO transport queue,
  348. * add it
  349. */
  350. spin_lock_irqsave(&dto_lock, flags);
  351. if (list_empty(&xprt->sc_dto_q)) {
  352. svc_xprt_get(&xprt->sc_xprt);
  353. list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
  354. }
  355. spin_unlock_irqrestore(&dto_lock, flags);
  356. /* Tasklet does all the work to avoid irqsave locks. */
  357. tasklet_schedule(&dto_tasklet);
  358. }
  359. static void create_context_cache(struct svcxprt_rdma *xprt,
  360. int ctxt_count, int ctxt_bump, int ctxt_max)
  361. {
  362. struct svc_rdma_op_ctxt *ctxt;
  363. int i;
  364. xprt->sc_ctxt_max = ctxt_max;
  365. xprt->sc_ctxt_bump = ctxt_bump;
  366. xprt->sc_ctxt_cnt = 0;
  367. atomic_set(&xprt->sc_ctxt_used, 0);
  368. INIT_LIST_HEAD(&xprt->sc_ctxt_free);
  369. for (i = 0; i < ctxt_count; i++) {
  370. ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
  371. if (ctxt) {
  372. INIT_LIST_HEAD(&ctxt->free_list);
  373. list_add(&ctxt->free_list, &xprt->sc_ctxt_free);
  374. xprt->sc_ctxt_cnt++;
  375. }
  376. }
  377. }
  378. static void destroy_context_cache(struct svcxprt_rdma *xprt)
  379. {
  380. while (!list_empty(&xprt->sc_ctxt_free)) {
  381. struct svc_rdma_op_ctxt *ctxt;
  382. ctxt = list_entry(xprt->sc_ctxt_free.next,
  383. struct svc_rdma_op_ctxt,
  384. free_list);
  385. list_del_init(&ctxt->free_list);
  386. kfree(ctxt);
  387. }
  388. }
  389. static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
  390. int listener)
  391. {
  392. struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
  393. if (!cma_xprt)
  394. return NULL;
  395. svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
  396. INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
  397. INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
  398. INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
  399. INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
  400. init_waitqueue_head(&cma_xprt->sc_send_wait);
  401. spin_lock_init(&cma_xprt->sc_lock);
  402. spin_lock_init(&cma_xprt->sc_read_complete_lock);
  403. spin_lock_init(&cma_xprt->sc_ctxt_lock);
  404. spin_lock_init(&cma_xprt->sc_rq_dto_lock);
  405. cma_xprt->sc_ord = svcrdma_ord;
  406. cma_xprt->sc_max_req_size = svcrdma_max_req_size;
  407. cma_xprt->sc_max_requests = svcrdma_max_requests;
  408. cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
  409. atomic_set(&cma_xprt->sc_sq_count, 0);
  410. if (!listener) {
  411. int reqs = cma_xprt->sc_max_requests;
  412. create_context_cache(cma_xprt,
  413. reqs << 1, /* starting size */
  414. reqs, /* bump amount */
  415. reqs +
  416. cma_xprt->sc_sq_depth +
  417. RPCRDMA_MAX_THREADS + 1); /* max */
  418. if (list_empty(&cma_xprt->sc_ctxt_free)) {
  419. kfree(cma_xprt);
  420. return NULL;
  421. }
  422. clear_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
  423. } else
  424. set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
  425. return cma_xprt;
  426. }
  427. struct page *svc_rdma_get_page(void)
  428. {
  429. struct page *page;
  430. while ((page = alloc_page(GFP_KERNEL)) == NULL) {
  431. /* If we can't get memory, wait a bit and try again */
  432. printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
  433. "jiffies.\n");
  434. schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
  435. }
  436. return page;
  437. }
  438. int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
  439. {
  440. struct ib_recv_wr recv_wr, *bad_recv_wr;
  441. struct svc_rdma_op_ctxt *ctxt;
  442. struct page *page;
  443. unsigned long pa;
  444. int sge_no;
  445. int buflen;
  446. int ret;
  447. ctxt = svc_rdma_get_context(xprt);
  448. buflen = 0;
  449. ctxt->direction = DMA_FROM_DEVICE;
  450. for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
  451. BUG_ON(sge_no >= xprt->sc_max_sge);
  452. page = svc_rdma_get_page();
  453. ctxt->pages[sge_no] = page;
  454. pa = ib_dma_map_page(xprt->sc_cm_id->device,
  455. page, 0, PAGE_SIZE,
  456. DMA_FROM_DEVICE);
  457. ctxt->sge[sge_no].addr = pa;
  458. ctxt->sge[sge_no].length = PAGE_SIZE;
  459. ctxt->sge[sge_no].lkey = xprt->sc_phys_mr->lkey;
  460. buflen += PAGE_SIZE;
  461. }
  462. ctxt->count = sge_no;
  463. recv_wr.next = NULL;
  464. recv_wr.sg_list = &ctxt->sge[0];
  465. recv_wr.num_sge = ctxt->count;
  466. recv_wr.wr_id = (u64)(unsigned long)ctxt;
  467. ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
  468. if (ret)
  469. svc_rdma_put_context(ctxt, 1);
  470. return ret;
  471. }
  472. /*
  473. * This function handles the CONNECT_REQUEST event on a listening
  474. * endpoint. It is passed the cma_id for the _new_ connection. The context in
  475. * this cma_id is inherited from the listening cma_id and is the svc_xprt
  476. * structure for the listening endpoint.
  477. *
  478. * This function creates a new xprt for the new connection and enqueues it on
  479. * the accept queue for the listent xprt. When the listen thread is kicked, it
  480. * will call the recvfrom method on the listen xprt which will accept the new
  481. * connection.
  482. */
  483. static void handle_connect_req(struct rdma_cm_id *new_cma_id)
  484. {
  485. struct svcxprt_rdma *listen_xprt = new_cma_id->context;
  486. struct svcxprt_rdma *newxprt;
  487. /* Create a new transport */
  488. newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
  489. if (!newxprt) {
  490. dprintk("svcrdma: failed to create new transport\n");
  491. return;
  492. }
  493. newxprt->sc_cm_id = new_cma_id;
  494. new_cma_id->context = newxprt;
  495. dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
  496. newxprt, newxprt->sc_cm_id, listen_xprt);
  497. /*
  498. * Enqueue the new transport on the accept queue of the listening
  499. * transport
  500. */
  501. spin_lock_bh(&listen_xprt->sc_lock);
  502. list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
  503. spin_unlock_bh(&listen_xprt->sc_lock);
  504. /*
  505. * Can't use svc_xprt_received here because we are not on a
  506. * rqstp thread
  507. */
  508. set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
  509. svc_xprt_enqueue(&listen_xprt->sc_xprt);
  510. }
  511. /*
  512. * Handles events generated on the listening endpoint. These events will be
  513. * either be incoming connect requests or adapter removal events.
  514. */
  515. static int rdma_listen_handler(struct rdma_cm_id *cma_id,
  516. struct rdma_cm_event *event)
  517. {
  518. struct svcxprt_rdma *xprt = cma_id->context;
  519. int ret = 0;
  520. switch (event->event) {
  521. case RDMA_CM_EVENT_CONNECT_REQUEST:
  522. dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
  523. "event=%d\n", cma_id, cma_id->context, event->event);
  524. handle_connect_req(cma_id);
  525. break;
  526. case RDMA_CM_EVENT_ESTABLISHED:
  527. /* Accept complete */
  528. dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
  529. "cm_id=%p\n", xprt, cma_id);
  530. break;
  531. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  532. dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
  533. xprt, cma_id);
  534. if (xprt)
  535. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  536. break;
  537. default:
  538. dprintk("svcrdma: Unexpected event on listening endpoint %p, "
  539. "event=%d\n", cma_id, event->event);
  540. break;
  541. }
  542. return ret;
  543. }
  544. static int rdma_cma_handler(struct rdma_cm_id *cma_id,
  545. struct rdma_cm_event *event)
  546. {
  547. struct svc_xprt *xprt = cma_id->context;
  548. struct svcxprt_rdma *rdma =
  549. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  550. switch (event->event) {
  551. case RDMA_CM_EVENT_ESTABLISHED:
  552. /* Accept complete */
  553. svc_xprt_get(xprt);
  554. dprintk("svcrdma: Connection completed on DTO xprt=%p, "
  555. "cm_id=%p\n", xprt, cma_id);
  556. clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
  557. svc_xprt_enqueue(xprt);
  558. break;
  559. case RDMA_CM_EVENT_DISCONNECTED:
  560. dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
  561. xprt, cma_id);
  562. if (xprt) {
  563. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  564. svc_xprt_enqueue(xprt);
  565. svc_xprt_put(xprt);
  566. }
  567. break;
  568. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  569. dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
  570. "event=%d\n", cma_id, xprt, event->event);
  571. if (xprt) {
  572. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  573. svc_xprt_enqueue(xprt);
  574. }
  575. break;
  576. default:
  577. dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
  578. "event=%d\n", cma_id, event->event);
  579. break;
  580. }
  581. return 0;
  582. }
  583. /*
  584. * Create a listening RDMA service endpoint.
  585. */
  586. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  587. struct sockaddr *sa, int salen,
  588. int flags)
  589. {
  590. struct rdma_cm_id *listen_id;
  591. struct svcxprt_rdma *cma_xprt;
  592. struct svc_xprt *xprt;
  593. int ret;
  594. dprintk("svcrdma: Creating RDMA socket\n");
  595. cma_xprt = rdma_create_xprt(serv, 1);
  596. if (!cma_xprt)
  597. return ERR_PTR(-ENOMEM);
  598. xprt = &cma_xprt->sc_xprt;
  599. listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
  600. if (IS_ERR(listen_id)) {
  601. ret = PTR_ERR(listen_id);
  602. dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
  603. goto err0;
  604. }
  605. ret = rdma_bind_addr(listen_id, sa);
  606. if (ret) {
  607. dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
  608. goto err1;
  609. }
  610. cma_xprt->sc_cm_id = listen_id;
  611. ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
  612. if (ret) {
  613. dprintk("svcrdma: rdma_listen failed = %d\n", ret);
  614. goto err1;
  615. }
  616. /*
  617. * We need to use the address from the cm_id in case the
  618. * caller specified 0 for the port number.
  619. */
  620. sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
  621. svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
  622. return &cma_xprt->sc_xprt;
  623. err1:
  624. rdma_destroy_id(listen_id);
  625. err0:
  626. kfree(cma_xprt);
  627. return ERR_PTR(ret);
  628. }
  629. /*
  630. * This is the xpo_recvfrom function for listening endpoints. Its
  631. * purpose is to accept incoming connections. The CMA callback handler
  632. * has already created a new transport and attached it to the new CMA
  633. * ID.
  634. *
  635. * There is a queue of pending connections hung on the listening
  636. * transport. This queue contains the new svc_xprt structure. This
  637. * function takes svc_xprt structures off the accept_q and completes
  638. * the connection.
  639. */
  640. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
  641. {
  642. struct svcxprt_rdma *listen_rdma;
  643. struct svcxprt_rdma *newxprt = NULL;
  644. struct rdma_conn_param conn_param;
  645. struct ib_qp_init_attr qp_attr;
  646. struct ib_device_attr devattr;
  647. struct sockaddr *sa;
  648. int ret;
  649. int i;
  650. listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
  651. clear_bit(XPT_CONN, &xprt->xpt_flags);
  652. /* Get the next entry off the accept list */
  653. spin_lock_bh(&listen_rdma->sc_lock);
  654. if (!list_empty(&listen_rdma->sc_accept_q)) {
  655. newxprt = list_entry(listen_rdma->sc_accept_q.next,
  656. struct svcxprt_rdma, sc_accept_q);
  657. list_del_init(&newxprt->sc_accept_q);
  658. }
  659. if (!list_empty(&listen_rdma->sc_accept_q))
  660. set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
  661. spin_unlock_bh(&listen_rdma->sc_lock);
  662. if (!newxprt)
  663. return NULL;
  664. dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
  665. newxprt, newxprt->sc_cm_id);
  666. ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
  667. if (ret) {
  668. dprintk("svcrdma: could not query device attributes on "
  669. "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
  670. goto errout;
  671. }
  672. /* Qualify the transport resource defaults with the
  673. * capabilities of this particular device */
  674. newxprt->sc_max_sge = min((size_t)devattr.max_sge,
  675. (size_t)RPCSVC_MAXPAGES);
  676. newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
  677. (size_t)svcrdma_max_requests);
  678. newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
  679. newxprt->sc_ord = min((size_t)devattr.max_qp_rd_atom,
  680. (size_t)svcrdma_ord);
  681. newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
  682. if (IS_ERR(newxprt->sc_pd)) {
  683. dprintk("svcrdma: error creating PD for connect request\n");
  684. goto errout;
  685. }
  686. newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  687. sq_comp_handler,
  688. cq_event_handler,
  689. newxprt,
  690. newxprt->sc_sq_depth,
  691. 0);
  692. if (IS_ERR(newxprt->sc_sq_cq)) {
  693. dprintk("svcrdma: error creating SQ CQ for connect request\n");
  694. goto errout;
  695. }
  696. newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  697. rq_comp_handler,
  698. cq_event_handler,
  699. newxprt,
  700. newxprt->sc_max_requests,
  701. 0);
  702. if (IS_ERR(newxprt->sc_rq_cq)) {
  703. dprintk("svcrdma: error creating RQ CQ for connect request\n");
  704. goto errout;
  705. }
  706. memset(&qp_attr, 0, sizeof qp_attr);
  707. qp_attr.event_handler = qp_event_handler;
  708. qp_attr.qp_context = &newxprt->sc_xprt;
  709. qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
  710. qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
  711. qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
  712. qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
  713. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  714. qp_attr.qp_type = IB_QPT_RC;
  715. qp_attr.send_cq = newxprt->sc_sq_cq;
  716. qp_attr.recv_cq = newxprt->sc_rq_cq;
  717. dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
  718. " cm_id->device=%p, sc_pd->device=%p\n"
  719. " cap.max_send_wr = %d\n"
  720. " cap.max_recv_wr = %d\n"
  721. " cap.max_send_sge = %d\n"
  722. " cap.max_recv_sge = %d\n",
  723. newxprt->sc_cm_id, newxprt->sc_pd,
  724. newxprt->sc_cm_id->device, newxprt->sc_pd->device,
  725. qp_attr.cap.max_send_wr,
  726. qp_attr.cap.max_recv_wr,
  727. qp_attr.cap.max_send_sge,
  728. qp_attr.cap.max_recv_sge);
  729. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
  730. if (ret) {
  731. /*
  732. * XXX: This is a hack. We need a xx_request_qp interface
  733. * that will adjust the qp_attr's with a best-effort
  734. * number
  735. */
  736. qp_attr.cap.max_send_sge -= 2;
  737. qp_attr.cap.max_recv_sge -= 2;
  738. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
  739. &qp_attr);
  740. if (ret) {
  741. dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
  742. goto errout;
  743. }
  744. newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
  745. newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
  746. newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
  747. newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
  748. }
  749. svc_xprt_get(&newxprt->sc_xprt);
  750. newxprt->sc_qp = newxprt->sc_cm_id->qp;
  751. /* Register all of physical memory */
  752. newxprt->sc_phys_mr = ib_get_dma_mr(newxprt->sc_pd,
  753. IB_ACCESS_LOCAL_WRITE |
  754. IB_ACCESS_REMOTE_WRITE);
  755. if (IS_ERR(newxprt->sc_phys_mr)) {
  756. dprintk("svcrdma: Failed to create DMA MR ret=%d\n", ret);
  757. goto errout;
  758. }
  759. /* Post receive buffers */
  760. for (i = 0; i < newxprt->sc_max_requests; i++) {
  761. ret = svc_rdma_post_recv(newxprt);
  762. if (ret) {
  763. dprintk("svcrdma: failure posting receive buffers\n");
  764. goto errout;
  765. }
  766. }
  767. /* Swap out the handler */
  768. newxprt->sc_cm_id->event_handler = rdma_cma_handler;
  769. /* Accept Connection */
  770. set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
  771. memset(&conn_param, 0, sizeof conn_param);
  772. conn_param.responder_resources = 0;
  773. conn_param.initiator_depth = newxprt->sc_ord;
  774. ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
  775. if (ret) {
  776. dprintk("svcrdma: failed to accept new connection, ret=%d\n",
  777. ret);
  778. goto errout;
  779. }
  780. dprintk("svcrdma: new connection %p accepted with the following "
  781. "attributes:\n"
  782. " local_ip : %d.%d.%d.%d\n"
  783. " local_port : %d\n"
  784. " remote_ip : %d.%d.%d.%d\n"
  785. " remote_port : %d\n"
  786. " max_sge : %d\n"
  787. " sq_depth : %d\n"
  788. " max_requests : %d\n"
  789. " ord : %d\n",
  790. newxprt,
  791. NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
  792. route.addr.src_addr)->sin_addr.s_addr),
  793. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  794. route.addr.src_addr)->sin_port),
  795. NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
  796. route.addr.dst_addr)->sin_addr.s_addr),
  797. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  798. route.addr.dst_addr)->sin_port),
  799. newxprt->sc_max_sge,
  800. newxprt->sc_sq_depth,
  801. newxprt->sc_max_requests,
  802. newxprt->sc_ord);
  803. /* Set the local and remote addresses in the transport */
  804. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
  805. svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  806. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
  807. svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  808. ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  809. ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
  810. return &newxprt->sc_xprt;
  811. errout:
  812. dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
  813. /* Take a reference in case the DTO handler runs */
  814. svc_xprt_get(&newxprt->sc_xprt);
  815. if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp)) {
  816. ib_destroy_qp(newxprt->sc_qp);
  817. svc_xprt_put(&newxprt->sc_xprt);
  818. }
  819. rdma_destroy_id(newxprt->sc_cm_id);
  820. /* This call to put will destroy the transport */
  821. svc_xprt_put(&newxprt->sc_xprt);
  822. return NULL;
  823. }
  824. static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
  825. {
  826. }
  827. /*
  828. * When connected, an svc_xprt has at least three references:
  829. *
  830. * - A reference held by the QP. We still hold that here because this
  831. * code deletes the QP and puts the reference.
  832. *
  833. * - A reference held by the cm_id between the ESTABLISHED and
  834. * DISCONNECTED events. If the remote peer disconnected first, this
  835. * reference could be gone.
  836. *
  837. * - A reference held by the svc_recv code that called this function
  838. * as part of close processing.
  839. *
  840. * At a minimum two references should still be held.
  841. */
  842. static void svc_rdma_detach(struct svc_xprt *xprt)
  843. {
  844. struct svcxprt_rdma *rdma =
  845. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  846. dprintk("svc: svc_rdma_detach(%p)\n", xprt);
  847. /* Disconnect and flush posted WQE */
  848. rdma_disconnect(rdma->sc_cm_id);
  849. /* Destroy the QP if present (not a listener) */
  850. if (rdma->sc_qp && !IS_ERR(rdma->sc_qp)) {
  851. ib_destroy_qp(rdma->sc_qp);
  852. svc_xprt_put(xprt);
  853. }
  854. /* Destroy the CM ID */
  855. rdma_destroy_id(rdma->sc_cm_id);
  856. }
  857. static void svc_rdma_free(struct svc_xprt *xprt)
  858. {
  859. struct svcxprt_rdma *rdma = (struct svcxprt_rdma *)xprt;
  860. dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
  861. /* We should only be called from kref_put */
  862. BUG_ON(atomic_read(&xprt->xpt_ref.refcount) != 0);
  863. if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
  864. ib_destroy_cq(rdma->sc_sq_cq);
  865. if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
  866. ib_destroy_cq(rdma->sc_rq_cq);
  867. if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
  868. ib_dereg_mr(rdma->sc_phys_mr);
  869. if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
  870. ib_dealloc_pd(rdma->sc_pd);
  871. destroy_context_cache(rdma);
  872. kfree(rdma);
  873. }
  874. static int svc_rdma_has_wspace(struct svc_xprt *xprt)
  875. {
  876. struct svcxprt_rdma *rdma =
  877. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  878. /*
  879. * If there are fewer SQ WR available than required to send a
  880. * simple response, return false.
  881. */
  882. if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
  883. return 0;
  884. /*
  885. * ...or there are already waiters on the SQ,
  886. * return false.
  887. */
  888. if (waitqueue_active(&rdma->sc_send_wait))
  889. return 0;
  890. /* Otherwise return true. */
  891. return 1;
  892. }
  893. int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
  894. {
  895. struct ib_send_wr *bad_wr;
  896. int ret;
  897. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  898. return -ENOTCONN;
  899. BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
  900. BUG_ON(((struct svc_rdma_op_ctxt *)(unsigned long)wr->wr_id)->wr_op !=
  901. wr->opcode);
  902. /* If the SQ is full, wait until an SQ entry is available */
  903. while (1) {
  904. spin_lock_bh(&xprt->sc_lock);
  905. if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) {
  906. spin_unlock_bh(&xprt->sc_lock);
  907. atomic_inc(&rdma_stat_sq_starve);
  908. /* See if we can opportunistically reap SQ WR to make room */
  909. sq_cq_reap(xprt);
  910. /* Wait until SQ WR available if SQ still full */
  911. wait_event(xprt->sc_send_wait,
  912. atomic_read(&xprt->sc_sq_count) <
  913. xprt->sc_sq_depth);
  914. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  915. return 0;
  916. continue;
  917. }
  918. /* Bumped used SQ WR count and post */
  919. ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
  920. if (!ret)
  921. atomic_inc(&xprt->sc_sq_count);
  922. else
  923. dprintk("svcrdma: failed to post SQ WR rc=%d, "
  924. "sc_sq_count=%d, sc_sq_depth=%d\n",
  925. ret, atomic_read(&xprt->sc_sq_count),
  926. xprt->sc_sq_depth);
  927. spin_unlock_bh(&xprt->sc_lock);
  928. break;
  929. }
  930. return ret;
  931. }
  932. int svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
  933. enum rpcrdma_errcode err)
  934. {
  935. struct ib_send_wr err_wr;
  936. struct ib_sge sge;
  937. struct page *p;
  938. struct svc_rdma_op_ctxt *ctxt;
  939. u32 *va;
  940. int length;
  941. int ret;
  942. p = svc_rdma_get_page();
  943. va = page_address(p);
  944. /* XDR encode error */
  945. length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
  946. /* Prepare SGE for local address */
  947. sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
  948. p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  949. sge.lkey = xprt->sc_phys_mr->lkey;
  950. sge.length = length;
  951. ctxt = svc_rdma_get_context(xprt);
  952. ctxt->count = 1;
  953. ctxt->pages[0] = p;
  954. /* Prepare SEND WR */
  955. memset(&err_wr, 0, sizeof err_wr);
  956. ctxt->wr_op = IB_WR_SEND;
  957. err_wr.wr_id = (unsigned long)ctxt;
  958. err_wr.sg_list = &sge;
  959. err_wr.num_sge = 1;
  960. err_wr.opcode = IB_WR_SEND;
  961. err_wr.send_flags = IB_SEND_SIGNALED;
  962. /* Post It */
  963. ret = svc_rdma_send(xprt, &err_wr);
  964. if (ret) {
  965. dprintk("svcrdma: Error posting send = %d\n", ret);
  966. svc_rdma_put_context(ctxt, 1);
  967. }
  968. return ret;
  969. }