svc_rdma_sendto.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2016-2018 Oracle. All rights reserved.
  4. * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
  5. * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the BSD-type
  11. * license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * Neither the name of the Network Appliance, Inc. nor the names of
  26. * its contributors may be used to endorse or promote products
  27. * derived from this software without specific prior written
  28. * permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  36. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  37. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  38. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  40. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * Author: Tom Tucker <tom@opengridcomputing.com>
  43. */
  44. /* Operation
  45. *
  46. * The main entry point is svc_rdma_sendto. This is called by the
  47. * RPC server when an RPC Reply is ready to be transmitted to a client.
  48. *
  49. * The passed-in svc_rqst contains a struct xdr_buf which holds an
  50. * XDR-encoded RPC Reply message. sendto must construct the RPC-over-RDMA
  51. * transport header, post all Write WRs needed for this Reply, then post
  52. * a Send WR conveying the transport header and the RPC message itself to
  53. * the client.
  54. *
  55. * svc_rdma_sendto must fully transmit the Reply before returning, as
  56. * the svc_rqst will be recycled as soon as sendto returns. Remaining
  57. * resources referred to by the svc_rqst are also recycled at that time.
  58. * Therefore any resources that must remain longer must be detached
  59. * from the svc_rqst and released later.
  60. *
  61. * Page Management
  62. *
  63. * The I/O that performs Reply transmission is asynchronous, and may
  64. * complete well after sendto returns. Thus pages under I/O must be
  65. * removed from the svc_rqst before sendto returns.
  66. *
  67. * The logic here depends on Send Queue and completion ordering. Since
  68. * the Send WR is always posted last, it will always complete last. Thus
  69. * when it completes, it is guaranteed that all previous Write WRs have
  70. * also completed.
  71. *
  72. * Write WRs are constructed and posted. Each Write segment gets its own
  73. * svc_rdma_rw_ctxt, allowing the Write completion handler to find and
  74. * DMA-unmap the pages under I/O for that Write segment. The Write
  75. * completion handler does not release any pages.
  76. *
  77. * When the Send WR is constructed, it also gets its own svc_rdma_send_ctxt.
  78. * The ownership of all of the Reply's pages are transferred into that
  79. * ctxt, the Send WR is posted, and sendto returns.
  80. *
  81. * The svc_rdma_send_ctxt is presented when the Send WR completes. The
  82. * Send completion handler finally releases the Reply's pages.
  83. *
  84. * This mechanism also assumes that completions on the transport's Send
  85. * Completion Queue do not run in parallel. Otherwise a Write completion
  86. * and Send completion running at the same time could release pages that
  87. * are still DMA-mapped.
  88. *
  89. * Error Handling
  90. *
  91. * - If the Send WR is posted successfully, it will either complete
  92. * successfully, or get flushed. Either way, the Send completion
  93. * handler releases the Reply's pages.
  94. * - If the Send WR cannot be not posted, the forward path releases
  95. * the Reply's pages.
  96. *
  97. * This handles the case, without the use of page reference counting,
  98. * where two different Write segments send portions of the same page.
  99. */
  100. #include <linux/spinlock.h>
  101. #include <asm/unaligned.h>
  102. #include <rdma/ib_verbs.h>
  103. #include <rdma/rdma_cm.h>
  104. #include <linux/sunrpc/debug.h>
  105. #include <linux/sunrpc/rpc_rdma.h>
  106. #include <linux/sunrpc/svc_rdma.h>
  107. #include "xprt_rdma.h"
  108. #include <trace/events/rpcrdma.h>
  109. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  110. static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc);
  111. static inline struct svc_rdma_send_ctxt *
  112. svc_rdma_next_send_ctxt(struct list_head *list)
  113. {
  114. return list_first_entry_or_null(list, struct svc_rdma_send_ctxt,
  115. sc_list);
  116. }
  117. static struct svc_rdma_send_ctxt *
  118. svc_rdma_send_ctxt_alloc(struct svcxprt_rdma *rdma)
  119. {
  120. struct svc_rdma_send_ctxt *ctxt;
  121. dma_addr_t addr;
  122. void *buffer;
  123. size_t size;
  124. int i;
  125. size = sizeof(*ctxt);
  126. size += rdma->sc_max_send_sges * sizeof(struct ib_sge);
  127. ctxt = kmalloc(size, GFP_KERNEL);
  128. if (!ctxt)
  129. goto fail0;
  130. buffer = kmalloc(rdma->sc_max_req_size, GFP_KERNEL);
  131. if (!buffer)
  132. goto fail1;
  133. addr = ib_dma_map_single(rdma->sc_pd->device, buffer,
  134. rdma->sc_max_req_size, DMA_TO_DEVICE);
  135. if (ib_dma_mapping_error(rdma->sc_pd->device, addr))
  136. goto fail2;
  137. ctxt->sc_send_wr.next = NULL;
  138. ctxt->sc_send_wr.wr_cqe = &ctxt->sc_cqe;
  139. ctxt->sc_send_wr.sg_list = ctxt->sc_sges;
  140. ctxt->sc_send_wr.send_flags = IB_SEND_SIGNALED;
  141. ctxt->sc_cqe.done = svc_rdma_wc_send;
  142. ctxt->sc_xprt_buf = buffer;
  143. ctxt->sc_sges[0].addr = addr;
  144. for (i = 0; i < rdma->sc_max_send_sges; i++)
  145. ctxt->sc_sges[i].lkey = rdma->sc_pd->local_dma_lkey;
  146. return ctxt;
  147. fail2:
  148. kfree(buffer);
  149. fail1:
  150. kfree(ctxt);
  151. fail0:
  152. return NULL;
  153. }
  154. /**
  155. * svc_rdma_send_ctxts_destroy - Release all send_ctxt's for an xprt
  156. * @rdma: svcxprt_rdma being torn down
  157. *
  158. */
  159. void svc_rdma_send_ctxts_destroy(struct svcxprt_rdma *rdma)
  160. {
  161. struct svc_rdma_send_ctxt *ctxt;
  162. while ((ctxt = svc_rdma_next_send_ctxt(&rdma->sc_send_ctxts))) {
  163. list_del(&ctxt->sc_list);
  164. ib_dma_unmap_single(rdma->sc_pd->device,
  165. ctxt->sc_sges[0].addr,
  166. rdma->sc_max_req_size,
  167. DMA_TO_DEVICE);
  168. kfree(ctxt->sc_xprt_buf);
  169. kfree(ctxt);
  170. }
  171. }
  172. /**
  173. * svc_rdma_send_ctxt_get - Get a free send_ctxt
  174. * @rdma: controlling svcxprt_rdma
  175. *
  176. * Returns a ready-to-use send_ctxt, or NULL if none are
  177. * available and a fresh one cannot be allocated.
  178. */
  179. struct svc_rdma_send_ctxt *svc_rdma_send_ctxt_get(struct svcxprt_rdma *rdma)
  180. {
  181. struct svc_rdma_send_ctxt *ctxt;
  182. spin_lock(&rdma->sc_send_lock);
  183. ctxt = svc_rdma_next_send_ctxt(&rdma->sc_send_ctxts);
  184. if (!ctxt)
  185. goto out_empty;
  186. list_del(&ctxt->sc_list);
  187. spin_unlock(&rdma->sc_send_lock);
  188. out:
  189. ctxt->sc_send_wr.num_sge = 0;
  190. ctxt->sc_cur_sge_no = 0;
  191. ctxt->sc_page_count = 0;
  192. return ctxt;
  193. out_empty:
  194. spin_unlock(&rdma->sc_send_lock);
  195. ctxt = svc_rdma_send_ctxt_alloc(rdma);
  196. if (!ctxt)
  197. return NULL;
  198. goto out;
  199. }
  200. /**
  201. * svc_rdma_send_ctxt_put - Return send_ctxt to free list
  202. * @rdma: controlling svcxprt_rdma
  203. * @ctxt: object to return to the free list
  204. *
  205. * Pages left in sc_pages are DMA unmapped and released.
  206. */
  207. void svc_rdma_send_ctxt_put(struct svcxprt_rdma *rdma,
  208. struct svc_rdma_send_ctxt *ctxt)
  209. {
  210. struct ib_device *device = rdma->sc_cm_id->device;
  211. unsigned int i;
  212. /* The first SGE contains the transport header, which
  213. * remains mapped until @ctxt is destroyed.
  214. */
  215. for (i = 1; i < ctxt->sc_send_wr.num_sge; i++)
  216. ib_dma_unmap_page(device,
  217. ctxt->sc_sges[i].addr,
  218. ctxt->sc_sges[i].length,
  219. DMA_TO_DEVICE);
  220. for (i = 0; i < ctxt->sc_page_count; ++i)
  221. put_page(ctxt->sc_pages[i]);
  222. spin_lock(&rdma->sc_send_lock);
  223. list_add(&ctxt->sc_list, &rdma->sc_send_ctxts);
  224. spin_unlock(&rdma->sc_send_lock);
  225. }
  226. /**
  227. * svc_rdma_wc_send - Invoked by RDMA provider for each polled Send WC
  228. * @cq: Completion Queue context
  229. * @wc: Work Completion object
  230. *
  231. * NB: The svc_xprt/svcxprt_rdma is pinned whenever it's possible that
  232. * the Send completion handler could be running.
  233. */
  234. static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
  235. {
  236. struct svcxprt_rdma *rdma = cq->cq_context;
  237. struct ib_cqe *cqe = wc->wr_cqe;
  238. struct svc_rdma_send_ctxt *ctxt;
  239. trace_svcrdma_wc_send(wc);
  240. atomic_inc(&rdma->sc_sq_avail);
  241. wake_up(&rdma->sc_send_wait);
  242. ctxt = container_of(cqe, struct svc_rdma_send_ctxt, sc_cqe);
  243. svc_rdma_send_ctxt_put(rdma, ctxt);
  244. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  245. set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
  246. svc_xprt_enqueue(&rdma->sc_xprt);
  247. if (wc->status != IB_WC_WR_FLUSH_ERR)
  248. pr_err("svcrdma: Send: %s (%u/0x%x)\n",
  249. ib_wc_status_msg(wc->status),
  250. wc->status, wc->vendor_err);
  251. }
  252. svc_xprt_put(&rdma->sc_xprt);
  253. }
  254. /**
  255. * svc_rdma_send - Post a single Send WR
  256. * @rdma: transport on which to post the WR
  257. * @wr: prepared Send WR to post
  258. *
  259. * Returns zero the Send WR was posted successfully. Otherwise, a
  260. * negative errno is returned.
  261. */
  262. int svc_rdma_send(struct svcxprt_rdma *rdma, struct ib_send_wr *wr)
  263. {
  264. int ret;
  265. might_sleep();
  266. /* If the SQ is full, wait until an SQ entry is available */
  267. while (1) {
  268. if ((atomic_dec_return(&rdma->sc_sq_avail) < 0)) {
  269. atomic_inc(&rdma_stat_sq_starve);
  270. trace_svcrdma_sq_full(rdma);
  271. atomic_inc(&rdma->sc_sq_avail);
  272. wait_event(rdma->sc_send_wait,
  273. atomic_read(&rdma->sc_sq_avail) > 1);
  274. if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags))
  275. return -ENOTCONN;
  276. trace_svcrdma_sq_retry(rdma);
  277. continue;
  278. }
  279. svc_xprt_get(&rdma->sc_xprt);
  280. ret = ib_post_send(rdma->sc_qp, wr, NULL);
  281. trace_svcrdma_post_send(wr, ret);
  282. if (ret) {
  283. set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
  284. svc_xprt_put(&rdma->sc_xprt);
  285. wake_up(&rdma->sc_send_wait);
  286. }
  287. break;
  288. }
  289. return ret;
  290. }
  291. static u32 xdr_padsize(u32 len)
  292. {
  293. return (len & 3) ? (4 - (len & 3)) : 0;
  294. }
  295. /* Returns length of transport header, in bytes.
  296. */
  297. static unsigned int svc_rdma_reply_hdr_len(__be32 *rdma_resp)
  298. {
  299. unsigned int nsegs;
  300. __be32 *p;
  301. p = rdma_resp;
  302. /* RPC-over-RDMA V1 replies never have a Read list. */
  303. p += rpcrdma_fixed_maxsz + 1;
  304. /* Skip Write list. */
  305. while (*p++ != xdr_zero) {
  306. nsegs = be32_to_cpup(p++);
  307. p += nsegs * rpcrdma_segment_maxsz;
  308. }
  309. /* Skip Reply chunk. */
  310. if (*p++ != xdr_zero) {
  311. nsegs = be32_to_cpup(p++);
  312. p += nsegs * rpcrdma_segment_maxsz;
  313. }
  314. return (unsigned long)p - (unsigned long)rdma_resp;
  315. }
  316. /* One Write chunk is copied from Call transport header to Reply
  317. * transport header. Each segment's length field is updated to
  318. * reflect number of bytes consumed in the segment.
  319. *
  320. * Returns number of segments in this chunk.
  321. */
  322. static unsigned int xdr_encode_write_chunk(__be32 *dst, __be32 *src,
  323. unsigned int remaining)
  324. {
  325. unsigned int i, nsegs;
  326. u32 seg_len;
  327. /* Write list discriminator */
  328. *dst++ = *src++;
  329. /* number of segments in this chunk */
  330. nsegs = be32_to_cpup(src);
  331. *dst++ = *src++;
  332. for (i = nsegs; i; i--) {
  333. /* segment's RDMA handle */
  334. *dst++ = *src++;
  335. /* bytes returned in this segment */
  336. seg_len = be32_to_cpu(*src);
  337. if (remaining >= seg_len) {
  338. /* entire segment was consumed */
  339. *dst = *src;
  340. remaining -= seg_len;
  341. } else {
  342. /* segment only partly filled */
  343. *dst = cpu_to_be32(remaining);
  344. remaining = 0;
  345. }
  346. dst++; src++;
  347. /* segment's RDMA offset */
  348. *dst++ = *src++;
  349. *dst++ = *src++;
  350. }
  351. return nsegs;
  352. }
  353. /* The client provided a Write list in the Call message. Fill in
  354. * the segments in the first Write chunk in the Reply's transport
  355. * header with the number of bytes consumed in each segment.
  356. * Remaining chunks are returned unused.
  357. *
  358. * Assumptions:
  359. * - Client has provided only one Write chunk
  360. */
  361. static void svc_rdma_xdr_encode_write_list(__be32 *rdma_resp, __be32 *wr_ch,
  362. unsigned int consumed)
  363. {
  364. unsigned int nsegs;
  365. __be32 *p, *q;
  366. /* RPC-over-RDMA V1 replies never have a Read list. */
  367. p = rdma_resp + rpcrdma_fixed_maxsz + 1;
  368. q = wr_ch;
  369. while (*q != xdr_zero) {
  370. nsegs = xdr_encode_write_chunk(p, q, consumed);
  371. q += 2 + nsegs * rpcrdma_segment_maxsz;
  372. p += 2 + nsegs * rpcrdma_segment_maxsz;
  373. consumed = 0;
  374. }
  375. /* Terminate Write list */
  376. *p++ = xdr_zero;
  377. /* Reply chunk discriminator; may be replaced later */
  378. *p = xdr_zero;
  379. }
  380. /* The client provided a Reply chunk in the Call message. Fill in
  381. * the segments in the Reply chunk in the Reply message with the
  382. * number of bytes consumed in each segment.
  383. *
  384. * Assumptions:
  385. * - Reply can always fit in the provided Reply chunk
  386. */
  387. static void svc_rdma_xdr_encode_reply_chunk(__be32 *rdma_resp, __be32 *rp_ch,
  388. unsigned int consumed)
  389. {
  390. __be32 *p;
  391. /* Find the Reply chunk in the Reply's xprt header.
  392. * RPC-over-RDMA V1 replies never have a Read list.
  393. */
  394. p = rdma_resp + rpcrdma_fixed_maxsz + 1;
  395. /* Skip past Write list */
  396. while (*p++ != xdr_zero)
  397. p += 1 + be32_to_cpup(p) * rpcrdma_segment_maxsz;
  398. xdr_encode_write_chunk(p, rp_ch, consumed);
  399. }
  400. /* Parse the RPC Call's transport header.
  401. */
  402. static void svc_rdma_get_write_arrays(__be32 *rdma_argp,
  403. __be32 **write, __be32 **reply)
  404. {
  405. __be32 *p;
  406. p = rdma_argp + rpcrdma_fixed_maxsz;
  407. /* Read list */
  408. while (*p++ != xdr_zero)
  409. p += 5;
  410. /* Write list */
  411. if (*p != xdr_zero) {
  412. *write = p;
  413. while (*p++ != xdr_zero)
  414. p += 1 + be32_to_cpu(*p) * 4;
  415. } else {
  416. *write = NULL;
  417. p++;
  418. }
  419. /* Reply chunk */
  420. if (*p != xdr_zero)
  421. *reply = p;
  422. else
  423. *reply = NULL;
  424. }
  425. /* RPC-over-RDMA Version One private extension: Remote Invalidation.
  426. * Responder's choice: requester signals it can handle Send With
  427. * Invalidate, and responder chooses one rkey to invalidate.
  428. *
  429. * Find a candidate rkey to invalidate when sending a reply. Picks the
  430. * first R_key it finds in the chunk lists.
  431. *
  432. * Returns zero if RPC's chunk lists are empty.
  433. */
  434. static u32 svc_rdma_get_inv_rkey(__be32 *rdma_argp,
  435. __be32 *wr_lst, __be32 *rp_ch)
  436. {
  437. __be32 *p;
  438. p = rdma_argp + rpcrdma_fixed_maxsz;
  439. if (*p != xdr_zero)
  440. p += 2;
  441. else if (wr_lst && be32_to_cpup(wr_lst + 1))
  442. p = wr_lst + 2;
  443. else if (rp_ch && be32_to_cpup(rp_ch + 1))
  444. p = rp_ch + 2;
  445. else
  446. return 0;
  447. return be32_to_cpup(p);
  448. }
  449. static int svc_rdma_dma_map_page(struct svcxprt_rdma *rdma,
  450. struct svc_rdma_send_ctxt *ctxt,
  451. struct page *page,
  452. unsigned long offset,
  453. unsigned int len)
  454. {
  455. struct ib_device *dev = rdma->sc_cm_id->device;
  456. dma_addr_t dma_addr;
  457. dma_addr = ib_dma_map_page(dev, page, offset, len, DMA_TO_DEVICE);
  458. if (ib_dma_mapping_error(dev, dma_addr))
  459. goto out_maperr;
  460. ctxt->sc_sges[ctxt->sc_cur_sge_no].addr = dma_addr;
  461. ctxt->sc_sges[ctxt->sc_cur_sge_no].length = len;
  462. ctxt->sc_send_wr.num_sge++;
  463. return 0;
  464. out_maperr:
  465. trace_svcrdma_dma_map_page(rdma, page);
  466. return -EIO;
  467. }
  468. /* ib_dma_map_page() is used here because svc_rdma_dma_unmap()
  469. * handles DMA-unmap and it uses ib_dma_unmap_page() exclusively.
  470. */
  471. static int svc_rdma_dma_map_buf(struct svcxprt_rdma *rdma,
  472. struct svc_rdma_send_ctxt *ctxt,
  473. unsigned char *base,
  474. unsigned int len)
  475. {
  476. return svc_rdma_dma_map_page(rdma, ctxt, virt_to_page(base),
  477. offset_in_page(base), len);
  478. }
  479. /**
  480. * svc_rdma_sync_reply_hdr - DMA sync the transport header buffer
  481. * @rdma: controlling transport
  482. * @ctxt: send_ctxt for the Send WR
  483. * @len: length of transport header
  484. *
  485. */
  486. void svc_rdma_sync_reply_hdr(struct svcxprt_rdma *rdma,
  487. struct svc_rdma_send_ctxt *ctxt,
  488. unsigned int len)
  489. {
  490. ctxt->sc_sges[0].length = len;
  491. ctxt->sc_send_wr.num_sge++;
  492. ib_dma_sync_single_for_device(rdma->sc_pd->device,
  493. ctxt->sc_sges[0].addr, len,
  494. DMA_TO_DEVICE);
  495. }
  496. /* If the xdr_buf has more elements than the device can
  497. * transmit in a single RDMA Send, then the reply will
  498. * have to be copied into a bounce buffer.
  499. */
  500. static bool svc_rdma_pull_up_needed(struct svcxprt_rdma *rdma,
  501. struct xdr_buf *xdr,
  502. __be32 *wr_lst)
  503. {
  504. int elements;
  505. /* xdr->head */
  506. elements = 1;
  507. /* xdr->pages */
  508. if (!wr_lst) {
  509. unsigned int remaining;
  510. unsigned long pageoff;
  511. pageoff = xdr->page_base & ~PAGE_MASK;
  512. remaining = xdr->page_len;
  513. while (remaining) {
  514. ++elements;
  515. remaining -= min_t(u32, PAGE_SIZE - pageoff,
  516. remaining);
  517. pageoff = 0;
  518. }
  519. }
  520. /* xdr->tail */
  521. if (xdr->tail[0].iov_len)
  522. ++elements;
  523. /* assume 1 SGE is needed for the transport header */
  524. return elements >= rdma->sc_max_send_sges;
  525. }
  526. /* The device is not capable of sending the reply directly.
  527. * Assemble the elements of @xdr into the transport header
  528. * buffer.
  529. */
  530. static int svc_rdma_pull_up_reply_msg(struct svcxprt_rdma *rdma,
  531. struct svc_rdma_send_ctxt *ctxt,
  532. struct xdr_buf *xdr, __be32 *wr_lst)
  533. {
  534. unsigned char *dst, *tailbase;
  535. unsigned int taillen;
  536. dst = ctxt->sc_xprt_buf;
  537. dst += ctxt->sc_sges[0].length;
  538. memcpy(dst, xdr->head[0].iov_base, xdr->head[0].iov_len);
  539. dst += xdr->head[0].iov_len;
  540. tailbase = xdr->tail[0].iov_base;
  541. taillen = xdr->tail[0].iov_len;
  542. if (wr_lst) {
  543. u32 xdrpad;
  544. xdrpad = xdr_padsize(xdr->page_len);
  545. if (taillen && xdrpad) {
  546. tailbase += xdrpad;
  547. taillen -= xdrpad;
  548. }
  549. } else {
  550. unsigned int len, remaining;
  551. unsigned long pageoff;
  552. struct page **ppages;
  553. ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
  554. pageoff = xdr->page_base & ~PAGE_MASK;
  555. remaining = xdr->page_len;
  556. while (remaining) {
  557. len = min_t(u32, PAGE_SIZE - pageoff, remaining);
  558. memcpy(dst, page_address(*ppages), len);
  559. remaining -= len;
  560. dst += len;
  561. pageoff = 0;
  562. }
  563. }
  564. if (taillen)
  565. memcpy(dst, tailbase, taillen);
  566. ctxt->sc_sges[0].length += xdr->len;
  567. ib_dma_sync_single_for_device(rdma->sc_pd->device,
  568. ctxt->sc_sges[0].addr,
  569. ctxt->sc_sges[0].length,
  570. DMA_TO_DEVICE);
  571. return 0;
  572. }
  573. /* svc_rdma_map_reply_msg - Map the buffer holding RPC message
  574. * @rdma: controlling transport
  575. * @ctxt: send_ctxt for the Send WR
  576. * @xdr: prepared xdr_buf containing RPC message
  577. * @wr_lst: pointer to Call header's Write list, or NULL
  578. *
  579. * Load the xdr_buf into the ctxt's sge array, and DMA map each
  580. * element as it is added.
  581. *
  582. * Returns zero on success, or a negative errno on failure.
  583. */
  584. int svc_rdma_map_reply_msg(struct svcxprt_rdma *rdma,
  585. struct svc_rdma_send_ctxt *ctxt,
  586. struct xdr_buf *xdr, __be32 *wr_lst)
  587. {
  588. unsigned int len, remaining;
  589. unsigned long page_off;
  590. struct page **ppages;
  591. unsigned char *base;
  592. u32 xdr_pad;
  593. int ret;
  594. if (svc_rdma_pull_up_needed(rdma, xdr, wr_lst))
  595. return svc_rdma_pull_up_reply_msg(rdma, ctxt, xdr, wr_lst);
  596. ++ctxt->sc_cur_sge_no;
  597. ret = svc_rdma_dma_map_buf(rdma, ctxt,
  598. xdr->head[0].iov_base,
  599. xdr->head[0].iov_len);
  600. if (ret < 0)
  601. return ret;
  602. /* If a Write chunk is present, the xdr_buf's page list
  603. * is not included inline. However the Upper Layer may
  604. * have added XDR padding in the tail buffer, and that
  605. * should not be included inline.
  606. */
  607. if (wr_lst) {
  608. base = xdr->tail[0].iov_base;
  609. len = xdr->tail[0].iov_len;
  610. xdr_pad = xdr_padsize(xdr->page_len);
  611. if (len && xdr_pad) {
  612. base += xdr_pad;
  613. len -= xdr_pad;
  614. }
  615. goto tail;
  616. }
  617. ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
  618. page_off = xdr->page_base & ~PAGE_MASK;
  619. remaining = xdr->page_len;
  620. while (remaining) {
  621. len = min_t(u32, PAGE_SIZE - page_off, remaining);
  622. ++ctxt->sc_cur_sge_no;
  623. ret = svc_rdma_dma_map_page(rdma, ctxt, *ppages++,
  624. page_off, len);
  625. if (ret < 0)
  626. return ret;
  627. remaining -= len;
  628. page_off = 0;
  629. }
  630. base = xdr->tail[0].iov_base;
  631. len = xdr->tail[0].iov_len;
  632. tail:
  633. if (len) {
  634. ++ctxt->sc_cur_sge_no;
  635. ret = svc_rdma_dma_map_buf(rdma, ctxt, base, len);
  636. if (ret < 0)
  637. return ret;
  638. }
  639. return 0;
  640. }
  641. /* The svc_rqst and all resources it owns are released as soon as
  642. * svc_rdma_sendto returns. Transfer pages under I/O to the ctxt
  643. * so they are released by the Send completion handler.
  644. */
  645. static void svc_rdma_save_io_pages(struct svc_rqst *rqstp,
  646. struct svc_rdma_send_ctxt *ctxt)
  647. {
  648. int i, pages = rqstp->rq_next_page - rqstp->rq_respages;
  649. ctxt->sc_page_count += pages;
  650. for (i = 0; i < pages; i++) {
  651. ctxt->sc_pages[i] = rqstp->rq_respages[i];
  652. rqstp->rq_respages[i] = NULL;
  653. }
  654. /* Prevent svc_xprt_release from releasing pages in rq_pages */
  655. rqstp->rq_next_page = rqstp->rq_respages;
  656. }
  657. /* Prepare the portion of the RPC Reply that will be transmitted
  658. * via RDMA Send. The RPC-over-RDMA transport header is prepared
  659. * in sc_sges[0], and the RPC xdr_buf is prepared in following sges.
  660. *
  661. * Depending on whether a Write list or Reply chunk is present,
  662. * the server may send all, a portion of, or none of the xdr_buf.
  663. * In the latter case, only the transport header (sc_sges[0]) is
  664. * transmitted.
  665. *
  666. * RDMA Send is the last step of transmitting an RPC reply. Pages
  667. * involved in the earlier RDMA Writes are here transferred out
  668. * of the rqstp and into the ctxt's page array. These pages are
  669. * DMA unmapped by each Write completion, but the subsequent Send
  670. * completion finally releases these pages.
  671. *
  672. * Assumptions:
  673. * - The Reply's transport header will never be larger than a page.
  674. */
  675. static int svc_rdma_send_reply_msg(struct svcxprt_rdma *rdma,
  676. struct svc_rdma_send_ctxt *ctxt,
  677. __be32 *rdma_argp,
  678. struct svc_rqst *rqstp,
  679. __be32 *wr_lst, __be32 *rp_ch)
  680. {
  681. int ret;
  682. if (!rp_ch) {
  683. ret = svc_rdma_map_reply_msg(rdma, ctxt,
  684. &rqstp->rq_res, wr_lst);
  685. if (ret < 0)
  686. return ret;
  687. }
  688. svc_rdma_save_io_pages(rqstp, ctxt);
  689. ctxt->sc_send_wr.opcode = IB_WR_SEND;
  690. if (rdma->sc_snd_w_inv) {
  691. ctxt->sc_send_wr.ex.invalidate_rkey =
  692. svc_rdma_get_inv_rkey(rdma_argp, wr_lst, rp_ch);
  693. if (ctxt->sc_send_wr.ex.invalidate_rkey)
  694. ctxt->sc_send_wr.opcode = IB_WR_SEND_WITH_INV;
  695. }
  696. dprintk("svcrdma: posting Send WR with %u sge(s)\n",
  697. ctxt->sc_send_wr.num_sge);
  698. return svc_rdma_send(rdma, &ctxt->sc_send_wr);
  699. }
  700. /* Given the client-provided Write and Reply chunks, the server was not
  701. * able to form a complete reply. Return an RDMA_ERROR message so the
  702. * client can retire this RPC transaction. As above, the Send completion
  703. * routine releases payload pages that were part of a previous RDMA Write.
  704. *
  705. * Remote Invalidation is skipped for simplicity.
  706. */
  707. static int svc_rdma_send_error_msg(struct svcxprt_rdma *rdma,
  708. struct svc_rdma_send_ctxt *ctxt,
  709. struct svc_rqst *rqstp)
  710. {
  711. __be32 *p;
  712. int ret;
  713. p = ctxt->sc_xprt_buf;
  714. trace_svcrdma_err_chunk(*p);
  715. p += 3;
  716. *p++ = rdma_error;
  717. *p = err_chunk;
  718. svc_rdma_sync_reply_hdr(rdma, ctxt, RPCRDMA_HDRLEN_ERR);
  719. svc_rdma_save_io_pages(rqstp, ctxt);
  720. ctxt->sc_send_wr.opcode = IB_WR_SEND;
  721. ret = svc_rdma_send(rdma, &ctxt->sc_send_wr);
  722. if (ret) {
  723. svc_rdma_send_ctxt_put(rdma, ctxt);
  724. return ret;
  725. }
  726. return 0;
  727. }
  728. void svc_rdma_prep_reply_hdr(struct svc_rqst *rqstp)
  729. {
  730. }
  731. /**
  732. * svc_rdma_sendto - Transmit an RPC reply
  733. * @rqstp: processed RPC request, reply XDR already in ::rq_res
  734. *
  735. * Any resources still associated with @rqstp are released upon return.
  736. * If no reply message was possible, the connection is closed.
  737. *
  738. * Returns:
  739. * %0 if an RPC reply has been successfully posted,
  740. * %-ENOMEM if a resource shortage occurred (connection is lost),
  741. * %-ENOTCONN if posting failed (connection is lost).
  742. */
  743. int svc_rdma_sendto(struct svc_rqst *rqstp)
  744. {
  745. struct svc_xprt *xprt = rqstp->rq_xprt;
  746. struct svcxprt_rdma *rdma =
  747. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  748. struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
  749. __be32 *p, *rdma_argp, *rdma_resp, *wr_lst, *rp_ch;
  750. struct xdr_buf *xdr = &rqstp->rq_res;
  751. struct svc_rdma_send_ctxt *sctxt;
  752. int ret;
  753. rdma_argp = rctxt->rc_recv_buf;
  754. svc_rdma_get_write_arrays(rdma_argp, &wr_lst, &rp_ch);
  755. /* Create the RDMA response header. xprt->xpt_mutex,
  756. * acquired in svc_send(), serializes RPC replies. The
  757. * code path below that inserts the credit grant value
  758. * into each transport header runs only inside this
  759. * critical section.
  760. */
  761. ret = -ENOMEM;
  762. sctxt = svc_rdma_send_ctxt_get(rdma);
  763. if (!sctxt)
  764. goto err0;
  765. rdma_resp = sctxt->sc_xprt_buf;
  766. p = rdma_resp;
  767. *p++ = *rdma_argp;
  768. *p++ = *(rdma_argp + 1);
  769. *p++ = rdma->sc_fc_credits;
  770. *p++ = rp_ch ? rdma_nomsg : rdma_msg;
  771. /* Start with empty chunks */
  772. *p++ = xdr_zero;
  773. *p++ = xdr_zero;
  774. *p = xdr_zero;
  775. if (wr_lst) {
  776. /* XXX: Presume the client sent only one Write chunk */
  777. ret = svc_rdma_send_write_chunk(rdma, wr_lst, xdr);
  778. if (ret < 0)
  779. goto err2;
  780. svc_rdma_xdr_encode_write_list(rdma_resp, wr_lst, ret);
  781. }
  782. if (rp_ch) {
  783. ret = svc_rdma_send_reply_chunk(rdma, rp_ch, wr_lst, xdr);
  784. if (ret < 0)
  785. goto err2;
  786. svc_rdma_xdr_encode_reply_chunk(rdma_resp, rp_ch, ret);
  787. }
  788. svc_rdma_sync_reply_hdr(rdma, sctxt, svc_rdma_reply_hdr_len(rdma_resp));
  789. ret = svc_rdma_send_reply_msg(rdma, sctxt, rdma_argp, rqstp,
  790. wr_lst, rp_ch);
  791. if (ret < 0)
  792. goto err1;
  793. ret = 0;
  794. out:
  795. rqstp->rq_xprt_ctxt = NULL;
  796. svc_rdma_recv_ctxt_put(rdma, rctxt);
  797. return ret;
  798. err2:
  799. if (ret != -E2BIG && ret != -EINVAL)
  800. goto err1;
  801. ret = svc_rdma_send_error_msg(rdma, sctxt, rqstp);
  802. if (ret < 0)
  803. goto err1;
  804. ret = 0;
  805. goto out;
  806. err1:
  807. svc_rdma_send_ctxt_put(rdma, sctxt);
  808. err0:
  809. trace_svcrdma_send_failed(rqstp, ret);
  810. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  811. ret = -ENOTCONN;
  812. goto out;
  813. }