svc_rdma_transport.c 31 KB

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