svc_rdma_sendto.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*
  2. * Copyright (c) 2016 Oracle. All rights reserved.
  3. * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
  4. * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the BSD-type
  10. * license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * Neither the name of the Network Appliance, Inc. nor the names of
  25. * its contributors may be used to endorse or promote products
  26. * derived from this software without specific prior written
  27. * permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * Author: Tom Tucker <tom@opengridcomputing.com>
  42. */
  43. /* Operation
  44. *
  45. * The main entry point is svc_rdma_sendto. This is called by the
  46. * RPC server when an RPC Reply is ready to be transmitted to a client.
  47. *
  48. * The passed-in svc_rqst contains a struct xdr_buf which holds an
  49. * XDR-encoded RPC Reply message. sendto must construct the RPC-over-RDMA
  50. * transport header, post all Write WRs needed for this Reply, then post
  51. * a Send WR conveying the transport header and the RPC message itself to
  52. * the client.
  53. *
  54. * svc_rdma_sendto must fully transmit the Reply before returning, as
  55. * the svc_rqst will be recycled as soon as sendto returns. Remaining
  56. * resources referred to by the svc_rqst are also recycled at that time.
  57. * Therefore any resources that must remain longer must be detached
  58. * from the svc_rqst and released later.
  59. *
  60. * Page Management
  61. *
  62. * The I/O that performs Reply transmission is asynchronous, and may
  63. * complete well after sendto returns. Thus pages under I/O must be
  64. * removed from the svc_rqst before sendto returns.
  65. *
  66. * The logic here depends on Send Queue and completion ordering. Since
  67. * the Send WR is always posted last, it will always complete last. Thus
  68. * when it completes, it is guaranteed that all previous Write WRs have
  69. * also completed.
  70. *
  71. * Write WRs are constructed and posted. Each Write segment gets its own
  72. * svc_rdma_rw_ctxt, allowing the Write completion handler to find and
  73. * DMA-unmap the pages under I/O for that Write segment. The Write
  74. * completion handler does not release any pages.
  75. *
  76. * When the Send WR is constructed, it also gets its own svc_rdma_op_ctxt.
  77. * The ownership of all of the Reply's pages are transferred into that
  78. * ctxt, the Send WR is posted, and sendto returns.
  79. *
  80. * The svc_rdma_op_ctxt is presented when the Send WR completes. The
  81. * Send completion handler finally releases the Reply's pages.
  82. *
  83. * This mechanism also assumes that completions on the transport's Send
  84. * Completion Queue do not run in parallel. Otherwise a Write completion
  85. * and Send completion running at the same time could release pages that
  86. * are still DMA-mapped.
  87. *
  88. * Error Handling
  89. *
  90. * - If the Send WR is posted successfully, it will either complete
  91. * successfully, or get flushed. Either way, the Send completion
  92. * handler releases the Reply's pages.
  93. * - If the Send WR cannot be not posted, the forward path releases
  94. * the Reply's pages.
  95. *
  96. * This handles the case, without the use of page reference counting,
  97. * where two different Write segments send portions of the same page.
  98. */
  99. #include <linux/sunrpc/debug.h>
  100. #include <linux/sunrpc/rpc_rdma.h>
  101. #include <linux/spinlock.h>
  102. #include <asm/unaligned.h>
  103. #include <rdma/ib_verbs.h>
  104. #include <rdma/rdma_cm.h>
  105. #include <linux/sunrpc/svc_rdma.h>
  106. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  107. static u32 xdr_padsize(u32 len)
  108. {
  109. return (len & 3) ? (4 - (len & 3)) : 0;
  110. }
  111. /* Returns length of transport header, in bytes.
  112. */
  113. static unsigned int svc_rdma_reply_hdr_len(__be32 *rdma_resp)
  114. {
  115. unsigned int nsegs;
  116. __be32 *p;
  117. p = rdma_resp;
  118. /* RPC-over-RDMA V1 replies never have a Read list. */
  119. p += rpcrdma_fixed_maxsz + 1;
  120. /* Skip Write list. */
  121. while (*p++ != xdr_zero) {
  122. nsegs = be32_to_cpup(p++);
  123. p += nsegs * rpcrdma_segment_maxsz;
  124. }
  125. /* Skip Reply chunk. */
  126. if (*p++ != xdr_zero) {
  127. nsegs = be32_to_cpup(p++);
  128. p += nsegs * rpcrdma_segment_maxsz;
  129. }
  130. return (unsigned long)p - (unsigned long)rdma_resp;
  131. }
  132. /* One Write chunk is copied from Call transport header to Reply
  133. * transport header. Each segment's length field is updated to
  134. * reflect number of bytes consumed in the segment.
  135. *
  136. * Returns number of segments in this chunk.
  137. */
  138. static unsigned int xdr_encode_write_chunk(__be32 *dst, __be32 *src,
  139. unsigned int remaining)
  140. {
  141. unsigned int i, nsegs;
  142. u32 seg_len;
  143. /* Write list discriminator */
  144. *dst++ = *src++;
  145. /* number of segments in this chunk */
  146. nsegs = be32_to_cpup(src);
  147. *dst++ = *src++;
  148. for (i = nsegs; i; i--) {
  149. /* segment's RDMA handle */
  150. *dst++ = *src++;
  151. /* bytes returned in this segment */
  152. seg_len = be32_to_cpu(*src);
  153. if (remaining >= seg_len) {
  154. /* entire segment was consumed */
  155. *dst = *src;
  156. remaining -= seg_len;
  157. } else {
  158. /* segment only partly filled */
  159. *dst = cpu_to_be32(remaining);
  160. remaining = 0;
  161. }
  162. dst++; src++;
  163. /* segment's RDMA offset */
  164. *dst++ = *src++;
  165. *dst++ = *src++;
  166. }
  167. return nsegs;
  168. }
  169. /* The client provided a Write list in the Call message. Fill in
  170. * the segments in the first Write chunk in the Reply's transport
  171. * header with the number of bytes consumed in each segment.
  172. * Remaining chunks are returned unused.
  173. *
  174. * Assumptions:
  175. * - Client has provided only one Write chunk
  176. */
  177. static void svc_rdma_xdr_encode_write_list(__be32 *rdma_resp, __be32 *wr_ch,
  178. unsigned int consumed)
  179. {
  180. unsigned int nsegs;
  181. __be32 *p, *q;
  182. /* RPC-over-RDMA V1 replies never have a Read list. */
  183. p = rdma_resp + rpcrdma_fixed_maxsz + 1;
  184. q = wr_ch;
  185. while (*q != xdr_zero) {
  186. nsegs = xdr_encode_write_chunk(p, q, consumed);
  187. q += 2 + nsegs * rpcrdma_segment_maxsz;
  188. p += 2 + nsegs * rpcrdma_segment_maxsz;
  189. consumed = 0;
  190. }
  191. /* Terminate Write list */
  192. *p++ = xdr_zero;
  193. /* Reply chunk discriminator; may be replaced later */
  194. *p = xdr_zero;
  195. }
  196. /* The client provided a Reply chunk in the Call message. Fill in
  197. * the segments in the Reply chunk in the Reply message with the
  198. * number of bytes consumed in each segment.
  199. *
  200. * Assumptions:
  201. * - Reply can always fit in the provided Reply chunk
  202. */
  203. static void svc_rdma_xdr_encode_reply_chunk(__be32 *rdma_resp, __be32 *rp_ch,
  204. unsigned int consumed)
  205. {
  206. __be32 *p;
  207. /* Find the Reply chunk in the Reply's xprt header.
  208. * RPC-over-RDMA V1 replies never have a Read list.
  209. */
  210. p = rdma_resp + rpcrdma_fixed_maxsz + 1;
  211. /* Skip past Write list */
  212. while (*p++ != xdr_zero)
  213. p += 1 + be32_to_cpup(p) * rpcrdma_segment_maxsz;
  214. xdr_encode_write_chunk(p, rp_ch, consumed);
  215. }
  216. /* Parse the RPC Call's transport header.
  217. */
  218. static void svc_rdma_get_write_arrays(__be32 *rdma_argp,
  219. __be32 **write, __be32 **reply)
  220. {
  221. __be32 *p;
  222. p = rdma_argp + rpcrdma_fixed_maxsz;
  223. /* Read list */
  224. while (*p++ != xdr_zero)
  225. p += 5;
  226. /* Write list */
  227. if (*p != xdr_zero) {
  228. *write = p;
  229. while (*p++ != xdr_zero)
  230. p += 1 + be32_to_cpu(*p) * 4;
  231. } else {
  232. *write = NULL;
  233. p++;
  234. }
  235. /* Reply chunk */
  236. if (*p != xdr_zero)
  237. *reply = p;
  238. else
  239. *reply = NULL;
  240. }
  241. /* RPC-over-RDMA Version One private extension: Remote Invalidation.
  242. * Responder's choice: requester signals it can handle Send With
  243. * Invalidate, and responder chooses one rkey to invalidate.
  244. *
  245. * Find a candidate rkey to invalidate when sending a reply. Picks the
  246. * first R_key it finds in the chunk lists.
  247. *
  248. * Returns zero if RPC's chunk lists are empty.
  249. */
  250. static u32 svc_rdma_get_inv_rkey(__be32 *rdma_argp,
  251. __be32 *wr_lst, __be32 *rp_ch)
  252. {
  253. __be32 *p;
  254. p = rdma_argp + rpcrdma_fixed_maxsz;
  255. if (*p != xdr_zero)
  256. p += 2;
  257. else if (wr_lst && be32_to_cpup(wr_lst + 1))
  258. p = wr_lst + 2;
  259. else if (rp_ch && be32_to_cpup(rp_ch + 1))
  260. p = rp_ch + 2;
  261. else
  262. return 0;
  263. return be32_to_cpup(p);
  264. }
  265. /* ib_dma_map_page() is used here because svc_rdma_dma_unmap()
  266. * is used during completion to DMA-unmap this memory, and
  267. * it uses ib_dma_unmap_page() exclusively.
  268. */
  269. static int svc_rdma_dma_map_buf(struct svcxprt_rdma *rdma,
  270. struct svc_rdma_op_ctxt *ctxt,
  271. unsigned int sge_no,
  272. unsigned char *base,
  273. unsigned int len)
  274. {
  275. unsigned long offset = (unsigned long)base & ~PAGE_MASK;
  276. struct ib_device *dev = rdma->sc_cm_id->device;
  277. dma_addr_t dma_addr;
  278. dma_addr = ib_dma_map_page(dev, virt_to_page(base),
  279. offset, len, DMA_TO_DEVICE);
  280. if (ib_dma_mapping_error(dev, dma_addr))
  281. goto out_maperr;
  282. ctxt->sge[sge_no].addr = dma_addr;
  283. ctxt->sge[sge_no].length = len;
  284. ctxt->sge[sge_no].lkey = rdma->sc_pd->local_dma_lkey;
  285. svc_rdma_count_mappings(rdma, ctxt);
  286. return 0;
  287. out_maperr:
  288. pr_err("svcrdma: failed to map buffer\n");
  289. return -EIO;
  290. }
  291. static int svc_rdma_dma_map_page(struct svcxprt_rdma *rdma,
  292. struct svc_rdma_op_ctxt *ctxt,
  293. unsigned int sge_no,
  294. struct page *page,
  295. unsigned int offset,
  296. unsigned int len)
  297. {
  298. struct ib_device *dev = rdma->sc_cm_id->device;
  299. dma_addr_t dma_addr;
  300. dma_addr = ib_dma_map_page(dev, page, offset, len, DMA_TO_DEVICE);
  301. if (ib_dma_mapping_error(dev, dma_addr))
  302. goto out_maperr;
  303. ctxt->sge[sge_no].addr = dma_addr;
  304. ctxt->sge[sge_no].length = len;
  305. ctxt->sge[sge_no].lkey = rdma->sc_pd->local_dma_lkey;
  306. svc_rdma_count_mappings(rdma, ctxt);
  307. return 0;
  308. out_maperr:
  309. pr_err("svcrdma: failed to map page\n");
  310. return -EIO;
  311. }
  312. /**
  313. * svc_rdma_map_reply_hdr - DMA map the transport header buffer
  314. * @rdma: controlling transport
  315. * @ctxt: op_ctxt for the Send WR
  316. * @rdma_resp: buffer containing transport header
  317. * @len: length of transport header
  318. *
  319. * Returns:
  320. * %0 if the header is DMA mapped,
  321. * %-EIO if DMA mapping failed.
  322. */
  323. int svc_rdma_map_reply_hdr(struct svcxprt_rdma *rdma,
  324. struct svc_rdma_op_ctxt *ctxt,
  325. __be32 *rdma_resp,
  326. unsigned int len)
  327. {
  328. ctxt->direction = DMA_TO_DEVICE;
  329. ctxt->pages[0] = virt_to_page(rdma_resp);
  330. ctxt->count = 1;
  331. return svc_rdma_dma_map_page(rdma, ctxt, 0, ctxt->pages[0], 0, len);
  332. }
  333. /* Load the xdr_buf into the ctxt's sge array, and DMA map each
  334. * element as it is added.
  335. *
  336. * Returns the number of sge elements loaded on success, or
  337. * a negative errno on failure.
  338. */
  339. static int svc_rdma_map_reply_msg(struct svcxprt_rdma *rdma,
  340. struct svc_rdma_op_ctxt *ctxt,
  341. struct xdr_buf *xdr, __be32 *wr_lst)
  342. {
  343. unsigned int len, sge_no, remaining, page_off;
  344. struct page **ppages;
  345. unsigned char *base;
  346. u32 xdr_pad;
  347. int ret;
  348. sge_no = 1;
  349. ret = svc_rdma_dma_map_buf(rdma, ctxt, sge_no++,
  350. xdr->head[0].iov_base,
  351. xdr->head[0].iov_len);
  352. if (ret < 0)
  353. return ret;
  354. /* If a Write chunk is present, the xdr_buf's page list
  355. * is not included inline. However the Upper Layer may
  356. * have added XDR padding in the tail buffer, and that
  357. * should not be included inline.
  358. */
  359. if (wr_lst) {
  360. base = xdr->tail[0].iov_base;
  361. len = xdr->tail[0].iov_len;
  362. xdr_pad = xdr_padsize(xdr->page_len);
  363. if (len && xdr_pad) {
  364. base += xdr_pad;
  365. len -= xdr_pad;
  366. }
  367. goto tail;
  368. }
  369. ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
  370. page_off = xdr->page_base & ~PAGE_MASK;
  371. remaining = xdr->page_len;
  372. while (remaining) {
  373. len = min_t(u32, PAGE_SIZE - page_off, remaining);
  374. ret = svc_rdma_dma_map_page(rdma, ctxt, sge_no++,
  375. *ppages++, page_off, len);
  376. if (ret < 0)
  377. return ret;
  378. remaining -= len;
  379. page_off = 0;
  380. }
  381. base = xdr->tail[0].iov_base;
  382. len = xdr->tail[0].iov_len;
  383. tail:
  384. if (len) {
  385. ret = svc_rdma_dma_map_buf(rdma, ctxt, sge_no++, base, len);
  386. if (ret < 0)
  387. return ret;
  388. }
  389. return sge_no - 1;
  390. }
  391. /* The svc_rqst and all resources it owns are released as soon as
  392. * svc_rdma_sendto returns. Transfer pages under I/O to the ctxt
  393. * so they are released by the Send completion handler.
  394. */
  395. static void svc_rdma_save_io_pages(struct svc_rqst *rqstp,
  396. struct svc_rdma_op_ctxt *ctxt)
  397. {
  398. int i, pages = rqstp->rq_next_page - rqstp->rq_respages;
  399. ctxt->count += pages;
  400. for (i = 0; i < pages; i++) {
  401. ctxt->pages[i + 1] = rqstp->rq_respages[i];
  402. rqstp->rq_respages[i] = NULL;
  403. }
  404. rqstp->rq_next_page = rqstp->rq_respages + 1;
  405. }
  406. /**
  407. * svc_rdma_post_send_wr - Set up and post one Send Work Request
  408. * @rdma: controlling transport
  409. * @ctxt: op_ctxt for transmitting the Send WR
  410. * @num_sge: number of SGEs to send
  411. * @inv_rkey: R_key argument to Send With Invalidate, or zero
  412. *
  413. * Returns:
  414. * %0 if the Send* was posted successfully,
  415. * %-ENOTCONN if the connection was lost or dropped,
  416. * %-EINVAL if there was a problem with the Send we built,
  417. * %-ENOMEM if ib_post_send failed.
  418. */
  419. int svc_rdma_post_send_wr(struct svcxprt_rdma *rdma,
  420. struct svc_rdma_op_ctxt *ctxt, int num_sge,
  421. u32 inv_rkey)
  422. {
  423. struct ib_send_wr *send_wr = &ctxt->send_wr;
  424. dprintk("svcrdma: posting Send WR with %u sge(s)\n", num_sge);
  425. send_wr->next = NULL;
  426. ctxt->cqe.done = svc_rdma_wc_send;
  427. send_wr->wr_cqe = &ctxt->cqe;
  428. send_wr->sg_list = ctxt->sge;
  429. send_wr->num_sge = num_sge;
  430. send_wr->send_flags = IB_SEND_SIGNALED;
  431. if (inv_rkey) {
  432. send_wr->opcode = IB_WR_SEND_WITH_INV;
  433. send_wr->ex.invalidate_rkey = inv_rkey;
  434. } else {
  435. send_wr->opcode = IB_WR_SEND;
  436. }
  437. return svc_rdma_send(rdma, send_wr);
  438. }
  439. /* Prepare the portion of the RPC Reply that will be transmitted
  440. * via RDMA Send. The RPC-over-RDMA transport header is prepared
  441. * in sge[0], and the RPC xdr_buf is prepared in following sges.
  442. *
  443. * Depending on whether a Write list or Reply chunk is present,
  444. * the server may send all, a portion of, or none of the xdr_buf.
  445. * In the latter case, only the transport header (sge[0]) is
  446. * transmitted.
  447. *
  448. * RDMA Send is the last step of transmitting an RPC reply. Pages
  449. * involved in the earlier RDMA Writes are here transferred out
  450. * of the rqstp and into the ctxt's page array. These pages are
  451. * DMA unmapped by each Write completion, but the subsequent Send
  452. * completion finally releases these pages.
  453. *
  454. * Assumptions:
  455. * - The Reply's transport header will never be larger than a page.
  456. */
  457. static int svc_rdma_send_reply_msg(struct svcxprt_rdma *rdma,
  458. __be32 *rdma_argp, __be32 *rdma_resp,
  459. struct svc_rqst *rqstp,
  460. __be32 *wr_lst, __be32 *rp_ch)
  461. {
  462. struct svc_rdma_op_ctxt *ctxt;
  463. u32 inv_rkey;
  464. int ret;
  465. dprintk("svcrdma: sending %s reply: head=%zu, pagelen=%u, tail=%zu\n",
  466. (rp_ch ? "RDMA_NOMSG" : "RDMA_MSG"),
  467. rqstp->rq_res.head[0].iov_len,
  468. rqstp->rq_res.page_len,
  469. rqstp->rq_res.tail[0].iov_len);
  470. ctxt = svc_rdma_get_context(rdma);
  471. ret = svc_rdma_map_reply_hdr(rdma, ctxt, rdma_resp,
  472. svc_rdma_reply_hdr_len(rdma_resp));
  473. if (ret < 0)
  474. goto err;
  475. if (!rp_ch) {
  476. ret = svc_rdma_map_reply_msg(rdma, ctxt,
  477. &rqstp->rq_res, wr_lst);
  478. if (ret < 0)
  479. goto err;
  480. }
  481. svc_rdma_save_io_pages(rqstp, ctxt);
  482. inv_rkey = 0;
  483. if (rdma->sc_snd_w_inv)
  484. inv_rkey = svc_rdma_get_inv_rkey(rdma_argp, wr_lst, rp_ch);
  485. ret = svc_rdma_post_send_wr(rdma, ctxt, 1 + ret, inv_rkey);
  486. if (ret)
  487. goto err;
  488. return 0;
  489. err:
  490. svc_rdma_unmap_dma(ctxt);
  491. svc_rdma_put_context(ctxt, 1);
  492. return ret;
  493. }
  494. /* Given the client-provided Write and Reply chunks, the server was not
  495. * able to form a complete reply. Return an RDMA_ERROR message so the
  496. * client can retire this RPC transaction. As above, the Send completion
  497. * routine releases payload pages that were part of a previous RDMA Write.
  498. *
  499. * Remote Invalidation is skipped for simplicity.
  500. */
  501. static int svc_rdma_send_error_msg(struct svcxprt_rdma *rdma,
  502. __be32 *rdma_resp, struct svc_rqst *rqstp)
  503. {
  504. struct svc_rdma_op_ctxt *ctxt;
  505. __be32 *p;
  506. int ret;
  507. ctxt = svc_rdma_get_context(rdma);
  508. /* Replace the original transport header with an
  509. * RDMA_ERROR response. XID etc are preserved.
  510. */
  511. p = rdma_resp + 3;
  512. *p++ = rdma_error;
  513. *p = err_chunk;
  514. ret = svc_rdma_map_reply_hdr(rdma, ctxt, rdma_resp, 20);
  515. if (ret < 0)
  516. goto err;
  517. svc_rdma_save_io_pages(rqstp, ctxt);
  518. ret = svc_rdma_post_send_wr(rdma, ctxt, 1 + ret, 0);
  519. if (ret)
  520. goto err;
  521. return 0;
  522. err:
  523. pr_err("svcrdma: failed to post Send WR (%d)\n", ret);
  524. svc_rdma_unmap_dma(ctxt);
  525. svc_rdma_put_context(ctxt, 1);
  526. return ret;
  527. }
  528. void svc_rdma_prep_reply_hdr(struct svc_rqst *rqstp)
  529. {
  530. }
  531. /**
  532. * svc_rdma_sendto - Transmit an RPC reply
  533. * @rqstp: processed RPC request, reply XDR already in ::rq_res
  534. *
  535. * Any resources still associated with @rqstp are released upon return.
  536. * If no reply message was possible, the connection is closed.
  537. *
  538. * Returns:
  539. * %0 if an RPC reply has been successfully posted,
  540. * %-ENOMEM if a resource shortage occurred (connection is lost),
  541. * %-ENOTCONN if posting failed (connection is lost).
  542. */
  543. int svc_rdma_sendto(struct svc_rqst *rqstp)
  544. {
  545. struct svc_xprt *xprt = rqstp->rq_xprt;
  546. struct svcxprt_rdma *rdma =
  547. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  548. __be32 *p, *rdma_argp, *rdma_resp, *wr_lst, *rp_ch;
  549. struct xdr_buf *xdr = &rqstp->rq_res;
  550. struct page *res_page;
  551. int ret;
  552. /* Find the call's chunk lists to decide how to send the reply.
  553. * Receive places the Call's xprt header at the start of page 0.
  554. */
  555. rdma_argp = page_address(rqstp->rq_pages[0]);
  556. svc_rdma_get_write_arrays(rdma_argp, &wr_lst, &rp_ch);
  557. dprintk("svcrdma: preparing response for XID 0x%08x\n",
  558. be32_to_cpup(rdma_argp));
  559. /* Create the RDMA response header. xprt->xpt_mutex,
  560. * acquired in svc_send(), serializes RPC replies. The
  561. * code path below that inserts the credit grant value
  562. * into each transport header runs only inside this
  563. * critical section.
  564. */
  565. ret = -ENOMEM;
  566. res_page = alloc_page(GFP_KERNEL);
  567. if (!res_page)
  568. goto err0;
  569. rdma_resp = page_address(res_page);
  570. p = rdma_resp;
  571. *p++ = *rdma_argp;
  572. *p++ = *(rdma_argp + 1);
  573. *p++ = rdma->sc_fc_credits;
  574. *p++ = rp_ch ? rdma_nomsg : rdma_msg;
  575. /* Start with empty chunks */
  576. *p++ = xdr_zero;
  577. *p++ = xdr_zero;
  578. *p = xdr_zero;
  579. if (wr_lst) {
  580. /* XXX: Presume the client sent only one Write chunk */
  581. ret = svc_rdma_send_write_chunk(rdma, wr_lst, xdr);
  582. if (ret < 0)
  583. goto err2;
  584. svc_rdma_xdr_encode_write_list(rdma_resp, wr_lst, ret);
  585. }
  586. if (rp_ch) {
  587. ret = svc_rdma_send_reply_chunk(rdma, rp_ch, wr_lst, xdr);
  588. if (ret < 0)
  589. goto err2;
  590. svc_rdma_xdr_encode_reply_chunk(rdma_resp, rp_ch, ret);
  591. }
  592. ret = svc_rdma_send_reply_msg(rdma, rdma_argp, rdma_resp, rqstp,
  593. wr_lst, rp_ch);
  594. if (ret < 0)
  595. goto err0;
  596. return 0;
  597. err2:
  598. if (ret != -E2BIG && ret != -EINVAL)
  599. goto err1;
  600. ret = svc_rdma_send_error_msg(rdma, rdma_resp, rqstp);
  601. if (ret < 0)
  602. goto err0;
  603. return 0;
  604. err1:
  605. put_page(res_page);
  606. err0:
  607. pr_err("svcrdma: Could not send reply, err=%d. Closing transport.\n",
  608. ret);
  609. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  610. return -ENOTCONN;
  611. }