svc_rdma_transport.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. /* WR context cache. Created in svc_rdma.c */
  81. extern struct kmem_cache *svc_rdma_ctxt_cachep;
  82. struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
  83. {
  84. struct svc_rdma_op_ctxt *ctxt;
  85. while (1) {
  86. ctxt = kmem_cache_alloc(svc_rdma_ctxt_cachep, GFP_KERNEL);
  87. if (ctxt)
  88. break;
  89. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  90. }
  91. ctxt->xprt = xprt;
  92. INIT_LIST_HEAD(&ctxt->dto_q);
  93. ctxt->count = 0;
  94. atomic_inc(&xprt->sc_ctxt_used);
  95. return ctxt;
  96. }
  97. static void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
  98. {
  99. struct svcxprt_rdma *xprt = ctxt->xprt;
  100. int i;
  101. for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
  102. atomic_dec(&xprt->sc_dma_used);
  103. ib_dma_unmap_single(xprt->sc_cm_id->device,
  104. ctxt->sge[i].addr,
  105. ctxt->sge[i].length,
  106. ctxt->direction);
  107. }
  108. }
  109. void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
  110. {
  111. struct svcxprt_rdma *xprt;
  112. int i;
  113. BUG_ON(!ctxt);
  114. xprt = ctxt->xprt;
  115. if (free_pages)
  116. for (i = 0; i < ctxt->count; i++)
  117. put_page(ctxt->pages[i]);
  118. kmem_cache_free(svc_rdma_ctxt_cachep, ctxt);
  119. atomic_dec(&xprt->sc_ctxt_used);
  120. }
  121. /* Temporary NFS request map cache. Created in svc_rdma.c */
  122. extern struct kmem_cache *svc_rdma_map_cachep;
  123. /*
  124. * Temporary NFS req mappings are shared across all transport
  125. * instances. These are short lived and should be bounded by the number
  126. * of concurrent server threads * depth of the SQ.
  127. */
  128. struct svc_rdma_req_map *svc_rdma_get_req_map(void)
  129. {
  130. struct svc_rdma_req_map *map;
  131. while (1) {
  132. map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
  133. if (map)
  134. break;
  135. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  136. }
  137. map->count = 0;
  138. return map;
  139. }
  140. void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
  141. {
  142. kmem_cache_free(svc_rdma_map_cachep, map);
  143. }
  144. /* ib_cq event handler */
  145. static void cq_event_handler(struct ib_event *event, void *context)
  146. {
  147. struct svc_xprt *xprt = context;
  148. dprintk("svcrdma: received CQ event id=%d, context=%p\n",
  149. event->event, context);
  150. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  151. }
  152. /* QP event handler */
  153. static void qp_event_handler(struct ib_event *event, void *context)
  154. {
  155. struct svc_xprt *xprt = context;
  156. switch (event->event) {
  157. /* These are considered benign events */
  158. case IB_EVENT_PATH_MIG:
  159. case IB_EVENT_COMM_EST:
  160. case IB_EVENT_SQ_DRAINED:
  161. case IB_EVENT_QP_LAST_WQE_REACHED:
  162. dprintk("svcrdma: QP event %d received for QP=%p\n",
  163. event->event, event->element.qp);
  164. break;
  165. /* These are considered fatal events */
  166. case IB_EVENT_PATH_MIG_ERR:
  167. case IB_EVENT_QP_FATAL:
  168. case IB_EVENT_QP_REQ_ERR:
  169. case IB_EVENT_QP_ACCESS_ERR:
  170. case IB_EVENT_DEVICE_FATAL:
  171. default:
  172. dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
  173. "closing transport\n",
  174. event->event, event->element.qp);
  175. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  176. break;
  177. }
  178. }
  179. /*
  180. * Data Transfer Operation Tasklet
  181. *
  182. * Walks a list of transports with I/O pending, removing entries as
  183. * they are added to the server's I/O pending list. Two bits indicate
  184. * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
  185. * spinlock that serializes access to the transport list with the RQ
  186. * and SQ interrupt handlers.
  187. */
  188. static void dto_tasklet_func(unsigned long data)
  189. {
  190. struct svcxprt_rdma *xprt;
  191. unsigned long flags;
  192. spin_lock_irqsave(&dto_lock, flags);
  193. while (!list_empty(&dto_xprt_q)) {
  194. xprt = list_entry(dto_xprt_q.next,
  195. struct svcxprt_rdma, sc_dto_q);
  196. list_del_init(&xprt->sc_dto_q);
  197. spin_unlock_irqrestore(&dto_lock, flags);
  198. rq_cq_reap(xprt);
  199. sq_cq_reap(xprt);
  200. svc_xprt_put(&xprt->sc_xprt);
  201. spin_lock_irqsave(&dto_lock, flags);
  202. }
  203. spin_unlock_irqrestore(&dto_lock, flags);
  204. }
  205. /*
  206. * Receive Queue Completion Handler
  207. *
  208. * Since an RQ completion handler is called on interrupt context, we
  209. * need to defer the handling of the I/O to a tasklet
  210. */
  211. static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
  212. {
  213. struct svcxprt_rdma *xprt = cq_context;
  214. unsigned long flags;
  215. /* Guard against unconditional flush call for destroyed QP */
  216. if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
  217. return;
  218. /*
  219. * Set the bit regardless of whether or not it's on the list
  220. * because it may be on the list already due to an SQ
  221. * completion.
  222. */
  223. set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
  224. /*
  225. * If this transport is not already on the DTO transport queue,
  226. * add it
  227. */
  228. spin_lock_irqsave(&dto_lock, flags);
  229. if (list_empty(&xprt->sc_dto_q)) {
  230. svc_xprt_get(&xprt->sc_xprt);
  231. list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
  232. }
  233. spin_unlock_irqrestore(&dto_lock, flags);
  234. /* Tasklet does all the work to avoid irqsave locks. */
  235. tasklet_schedule(&dto_tasklet);
  236. }
  237. /*
  238. * rq_cq_reap - Process the RQ CQ.
  239. *
  240. * Take all completing WC off the CQE and enqueue the associated DTO
  241. * context on the dto_q for the transport.
  242. *
  243. * Note that caller must hold a transport reference.
  244. */
  245. static void rq_cq_reap(struct svcxprt_rdma *xprt)
  246. {
  247. int ret;
  248. struct ib_wc wc;
  249. struct svc_rdma_op_ctxt *ctxt = NULL;
  250. if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
  251. return;
  252. ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
  253. atomic_inc(&rdma_stat_rq_poll);
  254. while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
  255. ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
  256. ctxt->wc_status = wc.status;
  257. ctxt->byte_len = wc.byte_len;
  258. svc_rdma_unmap_dma(ctxt);
  259. if (wc.status != IB_WC_SUCCESS) {
  260. /* Close the transport */
  261. dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
  262. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  263. svc_rdma_put_context(ctxt, 1);
  264. svc_xprt_put(&xprt->sc_xprt);
  265. continue;
  266. }
  267. spin_lock_bh(&xprt->sc_rq_dto_lock);
  268. list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
  269. spin_unlock_bh(&xprt->sc_rq_dto_lock);
  270. svc_xprt_put(&xprt->sc_xprt);
  271. }
  272. if (ctxt)
  273. atomic_inc(&rdma_stat_rq_prod);
  274. set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
  275. /*
  276. * If data arrived before established event,
  277. * don't enqueue. This defers RPC I/O until the
  278. * RDMA connection is complete.
  279. */
  280. if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
  281. svc_xprt_enqueue(&xprt->sc_xprt);
  282. }
  283. /*
  284. * Send Queue Completion Handler - potentially called on interrupt context.
  285. *
  286. * Note that caller must hold a transport reference.
  287. */
  288. static void sq_cq_reap(struct svcxprt_rdma *xprt)
  289. {
  290. struct svc_rdma_op_ctxt *ctxt = NULL;
  291. struct ib_wc wc;
  292. struct ib_cq *cq = xprt->sc_sq_cq;
  293. int ret;
  294. if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
  295. return;
  296. ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  297. atomic_inc(&rdma_stat_sq_poll);
  298. while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
  299. ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
  300. xprt = ctxt->xprt;
  301. svc_rdma_unmap_dma(ctxt);
  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. svc_rdma_put_context(ctxt, 1);
  311. break;
  312. case IB_WR_RDMA_WRITE:
  313. svc_rdma_put_context(ctxt, 0);
  314. break;
  315. case IB_WR_RDMA_READ:
  316. if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
  317. struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
  318. BUG_ON(!read_hdr);
  319. spin_lock_bh(&xprt->sc_rq_dto_lock);
  320. set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
  321. list_add_tail(&read_hdr->dto_q,
  322. &xprt->sc_read_complete_q);
  323. spin_unlock_bh(&xprt->sc_rq_dto_lock);
  324. svc_xprt_enqueue(&xprt->sc_xprt);
  325. }
  326. svc_rdma_put_context(ctxt, 0);
  327. break;
  328. default:
  329. printk(KERN_ERR "svcrdma: unexpected completion type, "
  330. "opcode=%d, status=%d\n",
  331. wc.opcode, wc.status);
  332. break;
  333. }
  334. svc_xprt_put(&xprt->sc_xprt);
  335. }
  336. if (ctxt)
  337. atomic_inc(&rdma_stat_sq_prod);
  338. }
  339. static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
  340. {
  341. struct svcxprt_rdma *xprt = cq_context;
  342. unsigned long flags;
  343. /* Guard against unconditional flush call for destroyed QP */
  344. if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
  345. return;
  346. /*
  347. * Set the bit regardless of whether or not it's on the list
  348. * because it may be on the list already due to an RQ
  349. * completion.
  350. */
  351. set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
  352. /*
  353. * If this transport is not already on the DTO transport queue,
  354. * add it
  355. */
  356. spin_lock_irqsave(&dto_lock, flags);
  357. if (list_empty(&xprt->sc_dto_q)) {
  358. svc_xprt_get(&xprt->sc_xprt);
  359. list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
  360. }
  361. spin_unlock_irqrestore(&dto_lock, flags);
  362. /* Tasklet does all the work to avoid irqsave locks. */
  363. tasklet_schedule(&dto_tasklet);
  364. }
  365. static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
  366. int listener)
  367. {
  368. struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
  369. if (!cma_xprt)
  370. return NULL;
  371. svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
  372. INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
  373. INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
  374. INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
  375. INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
  376. init_waitqueue_head(&cma_xprt->sc_send_wait);
  377. spin_lock_init(&cma_xprt->sc_lock);
  378. spin_lock_init(&cma_xprt->sc_rq_dto_lock);
  379. cma_xprt->sc_ord = svcrdma_ord;
  380. cma_xprt->sc_max_req_size = svcrdma_max_req_size;
  381. cma_xprt->sc_max_requests = svcrdma_max_requests;
  382. cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
  383. atomic_set(&cma_xprt->sc_sq_count, 0);
  384. atomic_set(&cma_xprt->sc_ctxt_used, 0);
  385. if (listener)
  386. set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
  387. return cma_xprt;
  388. }
  389. struct page *svc_rdma_get_page(void)
  390. {
  391. struct page *page;
  392. while ((page = alloc_page(GFP_KERNEL)) == NULL) {
  393. /* If we can't get memory, wait a bit and try again */
  394. printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
  395. "jiffies.\n");
  396. schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
  397. }
  398. return page;
  399. }
  400. int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
  401. {
  402. struct ib_recv_wr recv_wr, *bad_recv_wr;
  403. struct svc_rdma_op_ctxt *ctxt;
  404. struct page *page;
  405. unsigned long pa;
  406. int sge_no;
  407. int buflen;
  408. int ret;
  409. ctxt = svc_rdma_get_context(xprt);
  410. buflen = 0;
  411. ctxt->direction = DMA_FROM_DEVICE;
  412. for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
  413. BUG_ON(sge_no >= xprt->sc_max_sge);
  414. page = svc_rdma_get_page();
  415. ctxt->pages[sge_no] = page;
  416. atomic_inc(&xprt->sc_dma_used);
  417. pa = ib_dma_map_page(xprt->sc_cm_id->device,
  418. page, 0, PAGE_SIZE,
  419. DMA_FROM_DEVICE);
  420. ctxt->sge[sge_no].addr = pa;
  421. ctxt->sge[sge_no].length = PAGE_SIZE;
  422. ctxt->sge[sge_no].lkey = xprt->sc_phys_mr->lkey;
  423. buflen += PAGE_SIZE;
  424. }
  425. ctxt->count = sge_no;
  426. recv_wr.next = NULL;
  427. recv_wr.sg_list = &ctxt->sge[0];
  428. recv_wr.num_sge = ctxt->count;
  429. recv_wr.wr_id = (u64)(unsigned long)ctxt;
  430. svc_xprt_get(&xprt->sc_xprt);
  431. ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
  432. if (ret) {
  433. svc_xprt_put(&xprt->sc_xprt);
  434. svc_rdma_put_context(ctxt, 1);
  435. }
  436. return ret;
  437. }
  438. /*
  439. * This function handles the CONNECT_REQUEST event on a listening
  440. * endpoint. It is passed the cma_id for the _new_ connection. The context in
  441. * this cma_id is inherited from the listening cma_id and is the svc_xprt
  442. * structure for the listening endpoint.
  443. *
  444. * This function creates a new xprt for the new connection and enqueues it on
  445. * the accept queue for the listent xprt. When the listen thread is kicked, it
  446. * will call the recvfrom method on the listen xprt which will accept the new
  447. * connection.
  448. */
  449. static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
  450. {
  451. struct svcxprt_rdma *listen_xprt = new_cma_id->context;
  452. struct svcxprt_rdma *newxprt;
  453. struct sockaddr *sa;
  454. /* Create a new transport */
  455. newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
  456. if (!newxprt) {
  457. dprintk("svcrdma: failed to create new transport\n");
  458. return;
  459. }
  460. newxprt->sc_cm_id = new_cma_id;
  461. new_cma_id->context = newxprt;
  462. dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
  463. newxprt, newxprt->sc_cm_id, listen_xprt);
  464. /* Save client advertised inbound read limit for use later in accept. */
  465. newxprt->sc_ord = client_ird;
  466. /* Set the local and remote addresses in the transport */
  467. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
  468. svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  469. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
  470. svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  471. /*
  472. * Enqueue the new transport on the accept queue of the listening
  473. * transport
  474. */
  475. spin_lock_bh(&listen_xprt->sc_lock);
  476. list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
  477. spin_unlock_bh(&listen_xprt->sc_lock);
  478. /*
  479. * Can't use svc_xprt_received here because we are not on a
  480. * rqstp thread
  481. */
  482. set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
  483. svc_xprt_enqueue(&listen_xprt->sc_xprt);
  484. }
  485. /*
  486. * Handles events generated on the listening endpoint. These events will be
  487. * either be incoming connect requests or adapter removal events.
  488. */
  489. static int rdma_listen_handler(struct rdma_cm_id *cma_id,
  490. struct rdma_cm_event *event)
  491. {
  492. struct svcxprt_rdma *xprt = cma_id->context;
  493. int ret = 0;
  494. switch (event->event) {
  495. case RDMA_CM_EVENT_CONNECT_REQUEST:
  496. dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
  497. "event=%d\n", cma_id, cma_id->context, event->event);
  498. handle_connect_req(cma_id,
  499. event->param.conn.responder_resources);
  500. break;
  501. case RDMA_CM_EVENT_ESTABLISHED:
  502. /* Accept complete */
  503. dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
  504. "cm_id=%p\n", xprt, cma_id);
  505. break;
  506. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  507. dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
  508. xprt, cma_id);
  509. if (xprt)
  510. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  511. break;
  512. default:
  513. dprintk("svcrdma: Unexpected event on listening endpoint %p, "
  514. "event=%d\n", cma_id, event->event);
  515. break;
  516. }
  517. return ret;
  518. }
  519. static int rdma_cma_handler(struct rdma_cm_id *cma_id,
  520. struct rdma_cm_event *event)
  521. {
  522. struct svc_xprt *xprt = cma_id->context;
  523. struct svcxprt_rdma *rdma =
  524. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  525. switch (event->event) {
  526. case RDMA_CM_EVENT_ESTABLISHED:
  527. /* Accept complete */
  528. svc_xprt_get(xprt);
  529. dprintk("svcrdma: Connection completed on DTO xprt=%p, "
  530. "cm_id=%p\n", xprt, cma_id);
  531. clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
  532. svc_xprt_enqueue(xprt);
  533. break;
  534. case RDMA_CM_EVENT_DISCONNECTED:
  535. dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
  536. xprt, cma_id);
  537. if (xprt) {
  538. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  539. svc_xprt_enqueue(xprt);
  540. svc_xprt_put(xprt);
  541. }
  542. break;
  543. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  544. dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
  545. "event=%d\n", cma_id, xprt, event->event);
  546. if (xprt) {
  547. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  548. svc_xprt_enqueue(xprt);
  549. }
  550. break;
  551. default:
  552. dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
  553. "event=%d\n", cma_id, event->event);
  554. break;
  555. }
  556. return 0;
  557. }
  558. /*
  559. * Create a listening RDMA service endpoint.
  560. */
  561. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  562. struct sockaddr *sa, int salen,
  563. int flags)
  564. {
  565. struct rdma_cm_id *listen_id;
  566. struct svcxprt_rdma *cma_xprt;
  567. struct svc_xprt *xprt;
  568. int ret;
  569. dprintk("svcrdma: Creating RDMA socket\n");
  570. cma_xprt = rdma_create_xprt(serv, 1);
  571. if (!cma_xprt)
  572. return ERR_PTR(-ENOMEM);
  573. xprt = &cma_xprt->sc_xprt;
  574. listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
  575. if (IS_ERR(listen_id)) {
  576. ret = PTR_ERR(listen_id);
  577. dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
  578. goto err0;
  579. }
  580. ret = rdma_bind_addr(listen_id, sa);
  581. if (ret) {
  582. dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
  583. goto err1;
  584. }
  585. cma_xprt->sc_cm_id = listen_id;
  586. ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
  587. if (ret) {
  588. dprintk("svcrdma: rdma_listen failed = %d\n", ret);
  589. goto err1;
  590. }
  591. /*
  592. * We need to use the address from the cm_id in case the
  593. * caller specified 0 for the port number.
  594. */
  595. sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
  596. svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
  597. return &cma_xprt->sc_xprt;
  598. err1:
  599. rdma_destroy_id(listen_id);
  600. err0:
  601. kfree(cma_xprt);
  602. return ERR_PTR(ret);
  603. }
  604. /*
  605. * This is the xpo_recvfrom function for listening endpoints. Its
  606. * purpose is to accept incoming connections. The CMA callback handler
  607. * has already created a new transport and attached it to the new CMA
  608. * ID.
  609. *
  610. * There is a queue of pending connections hung on the listening
  611. * transport. This queue contains the new svc_xprt structure. This
  612. * function takes svc_xprt structures off the accept_q and completes
  613. * the connection.
  614. */
  615. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
  616. {
  617. struct svcxprt_rdma *listen_rdma;
  618. struct svcxprt_rdma *newxprt = NULL;
  619. struct rdma_conn_param conn_param;
  620. struct ib_qp_init_attr qp_attr;
  621. struct ib_device_attr devattr;
  622. int ret;
  623. int i;
  624. listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
  625. clear_bit(XPT_CONN, &xprt->xpt_flags);
  626. /* Get the next entry off the accept list */
  627. spin_lock_bh(&listen_rdma->sc_lock);
  628. if (!list_empty(&listen_rdma->sc_accept_q)) {
  629. newxprt = list_entry(listen_rdma->sc_accept_q.next,
  630. struct svcxprt_rdma, sc_accept_q);
  631. list_del_init(&newxprt->sc_accept_q);
  632. }
  633. if (!list_empty(&listen_rdma->sc_accept_q))
  634. set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
  635. spin_unlock_bh(&listen_rdma->sc_lock);
  636. if (!newxprt)
  637. return NULL;
  638. dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
  639. newxprt, newxprt->sc_cm_id);
  640. ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
  641. if (ret) {
  642. dprintk("svcrdma: could not query device attributes on "
  643. "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
  644. goto errout;
  645. }
  646. /* Qualify the transport resource defaults with the
  647. * capabilities of this particular device */
  648. newxprt->sc_max_sge = min((size_t)devattr.max_sge,
  649. (size_t)RPCSVC_MAXPAGES);
  650. newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
  651. (size_t)svcrdma_max_requests);
  652. newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
  653. /*
  654. * Limit ORD based on client limit, local device limit, and
  655. * configured svcrdma limit.
  656. */
  657. newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
  658. newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
  659. newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
  660. if (IS_ERR(newxprt->sc_pd)) {
  661. dprintk("svcrdma: error creating PD for connect request\n");
  662. goto errout;
  663. }
  664. newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  665. sq_comp_handler,
  666. cq_event_handler,
  667. newxprt,
  668. newxprt->sc_sq_depth,
  669. 0);
  670. if (IS_ERR(newxprt->sc_sq_cq)) {
  671. dprintk("svcrdma: error creating SQ CQ for connect request\n");
  672. goto errout;
  673. }
  674. newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  675. rq_comp_handler,
  676. cq_event_handler,
  677. newxprt,
  678. newxprt->sc_max_requests,
  679. 0);
  680. if (IS_ERR(newxprt->sc_rq_cq)) {
  681. dprintk("svcrdma: error creating RQ CQ for connect request\n");
  682. goto errout;
  683. }
  684. memset(&qp_attr, 0, sizeof qp_attr);
  685. qp_attr.event_handler = qp_event_handler;
  686. qp_attr.qp_context = &newxprt->sc_xprt;
  687. qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
  688. qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
  689. qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
  690. qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
  691. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  692. qp_attr.qp_type = IB_QPT_RC;
  693. qp_attr.send_cq = newxprt->sc_sq_cq;
  694. qp_attr.recv_cq = newxprt->sc_rq_cq;
  695. dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
  696. " cm_id->device=%p, sc_pd->device=%p\n"
  697. " cap.max_send_wr = %d\n"
  698. " cap.max_recv_wr = %d\n"
  699. " cap.max_send_sge = %d\n"
  700. " cap.max_recv_sge = %d\n",
  701. newxprt->sc_cm_id, newxprt->sc_pd,
  702. newxprt->sc_cm_id->device, newxprt->sc_pd->device,
  703. qp_attr.cap.max_send_wr,
  704. qp_attr.cap.max_recv_wr,
  705. qp_attr.cap.max_send_sge,
  706. qp_attr.cap.max_recv_sge);
  707. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
  708. if (ret) {
  709. /*
  710. * XXX: This is a hack. We need a xx_request_qp interface
  711. * that will adjust the qp_attr's with a best-effort
  712. * number
  713. */
  714. qp_attr.cap.max_send_sge -= 2;
  715. qp_attr.cap.max_recv_sge -= 2;
  716. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
  717. &qp_attr);
  718. if (ret) {
  719. dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
  720. goto errout;
  721. }
  722. newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
  723. newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
  724. newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
  725. newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
  726. }
  727. newxprt->sc_qp = newxprt->sc_cm_id->qp;
  728. /* Register all of physical memory */
  729. newxprt->sc_phys_mr = ib_get_dma_mr(newxprt->sc_pd,
  730. IB_ACCESS_LOCAL_WRITE |
  731. IB_ACCESS_REMOTE_WRITE);
  732. if (IS_ERR(newxprt->sc_phys_mr)) {
  733. dprintk("svcrdma: Failed to create DMA MR ret=%d\n", ret);
  734. goto errout;
  735. }
  736. /* Post receive buffers */
  737. for (i = 0; i < newxprt->sc_max_requests; i++) {
  738. ret = svc_rdma_post_recv(newxprt);
  739. if (ret) {
  740. dprintk("svcrdma: failure posting receive buffers\n");
  741. goto errout;
  742. }
  743. }
  744. /* Swap out the handler */
  745. newxprt->sc_cm_id->event_handler = rdma_cma_handler;
  746. /*
  747. * Arm the CQs for the SQ and RQ before accepting so we can't
  748. * miss the first message
  749. */
  750. ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  751. ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
  752. /* Accept Connection */
  753. set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
  754. memset(&conn_param, 0, sizeof conn_param);
  755. conn_param.responder_resources = 0;
  756. conn_param.initiator_depth = newxprt->sc_ord;
  757. ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
  758. if (ret) {
  759. dprintk("svcrdma: failed to accept new connection, ret=%d\n",
  760. ret);
  761. goto errout;
  762. }
  763. dprintk("svcrdma: new connection %p accepted with the following "
  764. "attributes:\n"
  765. " local_ip : %d.%d.%d.%d\n"
  766. " local_port : %d\n"
  767. " remote_ip : %d.%d.%d.%d\n"
  768. " remote_port : %d\n"
  769. " max_sge : %d\n"
  770. " sq_depth : %d\n"
  771. " max_requests : %d\n"
  772. " ord : %d\n",
  773. newxprt,
  774. NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
  775. route.addr.src_addr)->sin_addr.s_addr),
  776. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  777. route.addr.src_addr)->sin_port),
  778. NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
  779. route.addr.dst_addr)->sin_addr.s_addr),
  780. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  781. route.addr.dst_addr)->sin_port),
  782. newxprt->sc_max_sge,
  783. newxprt->sc_sq_depth,
  784. newxprt->sc_max_requests,
  785. newxprt->sc_ord);
  786. return &newxprt->sc_xprt;
  787. errout:
  788. dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
  789. /* Take a reference in case the DTO handler runs */
  790. svc_xprt_get(&newxprt->sc_xprt);
  791. if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
  792. ib_destroy_qp(newxprt->sc_qp);
  793. rdma_destroy_id(newxprt->sc_cm_id);
  794. /* This call to put will destroy the transport */
  795. svc_xprt_put(&newxprt->sc_xprt);
  796. return NULL;
  797. }
  798. static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
  799. {
  800. }
  801. /*
  802. * When connected, an svc_xprt has at least two references:
  803. *
  804. * - A reference held by the cm_id between the ESTABLISHED and
  805. * DISCONNECTED events. If the remote peer disconnected first, this
  806. * reference could be gone.
  807. *
  808. * - A reference held by the svc_recv code that called this function
  809. * as part of close processing.
  810. *
  811. * At a minimum one references should still be held.
  812. */
  813. static void svc_rdma_detach(struct svc_xprt *xprt)
  814. {
  815. struct svcxprt_rdma *rdma =
  816. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  817. dprintk("svc: svc_rdma_detach(%p)\n", xprt);
  818. /* Disconnect and flush posted WQE */
  819. rdma_disconnect(rdma->sc_cm_id);
  820. }
  821. static void __svc_rdma_free(struct work_struct *work)
  822. {
  823. struct svcxprt_rdma *rdma =
  824. container_of(work, struct svcxprt_rdma, sc_work);
  825. dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
  826. /* We should only be called from kref_put */
  827. BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
  828. /*
  829. * Destroy queued, but not processed read completions. Note
  830. * that this cleanup has to be done before destroying the
  831. * cm_id because the device ptr is needed to unmap the dma in
  832. * svc_rdma_put_context.
  833. */
  834. while (!list_empty(&rdma->sc_read_complete_q)) {
  835. struct svc_rdma_op_ctxt *ctxt;
  836. ctxt = list_entry(rdma->sc_read_complete_q.next,
  837. struct svc_rdma_op_ctxt,
  838. dto_q);
  839. list_del_init(&ctxt->dto_q);
  840. svc_rdma_put_context(ctxt, 1);
  841. }
  842. /* Destroy queued, but not processed recv completions */
  843. while (!list_empty(&rdma->sc_rq_dto_q)) {
  844. struct svc_rdma_op_ctxt *ctxt;
  845. ctxt = list_entry(rdma->sc_rq_dto_q.next,
  846. struct svc_rdma_op_ctxt,
  847. dto_q);
  848. list_del_init(&ctxt->dto_q);
  849. svc_rdma_put_context(ctxt, 1);
  850. }
  851. /* Warn if we leaked a resource or under-referenced */
  852. WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
  853. WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
  854. /* Destroy the QP if present (not a listener) */
  855. if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
  856. ib_destroy_qp(rdma->sc_qp);
  857. if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
  858. ib_destroy_cq(rdma->sc_sq_cq);
  859. if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
  860. ib_destroy_cq(rdma->sc_rq_cq);
  861. if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
  862. ib_dereg_mr(rdma->sc_phys_mr);
  863. if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
  864. ib_dealloc_pd(rdma->sc_pd);
  865. /* Destroy the CM ID */
  866. rdma_destroy_id(rdma->sc_cm_id);
  867. kfree(rdma);
  868. }
  869. static void svc_rdma_free(struct svc_xprt *xprt)
  870. {
  871. struct svcxprt_rdma *rdma =
  872. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  873. INIT_WORK(&rdma->sc_work, __svc_rdma_free);
  874. schedule_work(&rdma->sc_work);
  875. }
  876. static int svc_rdma_has_wspace(struct svc_xprt *xprt)
  877. {
  878. struct svcxprt_rdma *rdma =
  879. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  880. /*
  881. * If there are fewer SQ WR available than required to send a
  882. * simple response, return false.
  883. */
  884. if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
  885. return 0;
  886. /*
  887. * ...or there are already waiters on the SQ,
  888. * return false.
  889. */
  890. if (waitqueue_active(&rdma->sc_send_wait))
  891. return 0;
  892. /* Otherwise return true. */
  893. return 1;
  894. }
  895. int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
  896. {
  897. struct ib_send_wr *bad_wr;
  898. int ret;
  899. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  900. return -ENOTCONN;
  901. BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
  902. BUG_ON(((struct svc_rdma_op_ctxt *)(unsigned long)wr->wr_id)->wr_op !=
  903. wr->opcode);
  904. /* If the SQ is full, wait until an SQ entry is available */
  905. while (1) {
  906. spin_lock_bh(&xprt->sc_lock);
  907. if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) {
  908. spin_unlock_bh(&xprt->sc_lock);
  909. atomic_inc(&rdma_stat_sq_starve);
  910. /* See if we can opportunistically reap SQ WR to make room */
  911. sq_cq_reap(xprt);
  912. /* Wait until SQ WR available if SQ still full */
  913. wait_event(xprt->sc_send_wait,
  914. atomic_read(&xprt->sc_sq_count) <
  915. xprt->sc_sq_depth);
  916. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  917. return 0;
  918. continue;
  919. }
  920. /* Bumped used SQ WR count and post */
  921. svc_xprt_get(&xprt->sc_xprt);
  922. ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
  923. if (!ret)
  924. atomic_inc(&xprt->sc_sq_count);
  925. else {
  926. svc_xprt_put(&xprt->sc_xprt);
  927. dprintk("svcrdma: failed to post SQ WR rc=%d, "
  928. "sc_sq_count=%d, sc_sq_depth=%d\n",
  929. ret, atomic_read(&xprt->sc_sq_count),
  930. xprt->sc_sq_depth);
  931. }
  932. spin_unlock_bh(&xprt->sc_lock);
  933. break;
  934. }
  935. return ret;
  936. }
  937. void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
  938. enum rpcrdma_errcode err)
  939. {
  940. struct ib_send_wr err_wr;
  941. struct ib_sge sge;
  942. struct page *p;
  943. struct svc_rdma_op_ctxt *ctxt;
  944. u32 *va;
  945. int length;
  946. int ret;
  947. p = svc_rdma_get_page();
  948. va = page_address(p);
  949. /* XDR encode error */
  950. length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
  951. /* Prepare SGE for local address */
  952. atomic_inc(&xprt->sc_dma_used);
  953. sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
  954. p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  955. sge.lkey = xprt->sc_phys_mr->lkey;
  956. sge.length = length;
  957. ctxt = svc_rdma_get_context(xprt);
  958. ctxt->count = 1;
  959. ctxt->pages[0] = p;
  960. /* Prepare SEND WR */
  961. memset(&err_wr, 0, sizeof err_wr);
  962. ctxt->wr_op = IB_WR_SEND;
  963. err_wr.wr_id = (unsigned long)ctxt;
  964. err_wr.sg_list = &sge;
  965. err_wr.num_sge = 1;
  966. err_wr.opcode = IB_WR_SEND;
  967. err_wr.send_flags = IB_SEND_SIGNALED;
  968. /* Post It */
  969. ret = svc_rdma_send(xprt, &err_wr);
  970. if (ret) {
  971. dprintk("svcrdma: Error %d posting send for protocol error\n",
  972. ret);
  973. svc_rdma_put_context(ctxt, 1);
  974. }
  975. }