svc_rdma_sendto.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. trace_svcrdma_post_send(wr);
  281. ret = ib_post_send(rdma->sc_qp, wr, NULL);
  282. if (ret)
  283. break;
  284. return 0;
  285. }
  286. trace_svcrdma_sq_post_err(rdma, ret);
  287. set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
  288. svc_xprt_put(&rdma->sc_xprt);
  289. wake_up(&rdma->sc_send_wait);
  290. return ret;
  291. }
  292. static u32 xdr_padsize(u32 len)
  293. {
  294. return (len & 3) ? (4 - (len & 3)) : 0;
  295. }
  296. /* Returns length of transport header, in bytes.
  297. */
  298. static unsigned int svc_rdma_reply_hdr_len(__be32 *rdma_resp)
  299. {
  300. unsigned int nsegs;
  301. __be32 *p;
  302. p = rdma_resp;
  303. /* RPC-over-RDMA V1 replies never have a Read list. */
  304. p += rpcrdma_fixed_maxsz + 1;
  305. /* Skip Write list. */
  306. while (*p++ != xdr_zero) {
  307. nsegs = be32_to_cpup(p++);
  308. p += nsegs * rpcrdma_segment_maxsz;
  309. }
  310. /* Skip Reply chunk. */
  311. if (*p++ != xdr_zero) {
  312. nsegs = be32_to_cpup(p++);
  313. p += nsegs * rpcrdma_segment_maxsz;
  314. }
  315. return (unsigned long)p - (unsigned long)rdma_resp;
  316. }
  317. /* One Write chunk is copied from Call transport header to Reply
  318. * transport header. Each segment's length field is updated to
  319. * reflect number of bytes consumed in the segment.
  320. *
  321. * Returns number of segments in this chunk.
  322. */
  323. static unsigned int xdr_encode_write_chunk(__be32 *dst, __be32 *src,
  324. unsigned int remaining)
  325. {
  326. unsigned int i, nsegs;
  327. u32 seg_len;
  328. /* Write list discriminator */
  329. *dst++ = *src++;
  330. /* number of segments in this chunk */
  331. nsegs = be32_to_cpup(src);
  332. *dst++ = *src++;
  333. for (i = nsegs; i; i--) {
  334. /* segment's RDMA handle */
  335. *dst++ = *src++;
  336. /* bytes returned in this segment */
  337. seg_len = be32_to_cpu(*src);
  338. if (remaining >= seg_len) {
  339. /* entire segment was consumed */
  340. *dst = *src;
  341. remaining -= seg_len;
  342. } else {
  343. /* segment only partly filled */
  344. *dst = cpu_to_be32(remaining);
  345. remaining = 0;
  346. }
  347. dst++; src++;
  348. /* segment's RDMA offset */
  349. *dst++ = *src++;
  350. *dst++ = *src++;
  351. }
  352. return nsegs;
  353. }
  354. /* The client provided a Write list in the Call message. Fill in
  355. * the segments in the first Write chunk in the Reply's transport
  356. * header with the number of bytes consumed in each segment.
  357. * Remaining chunks are returned unused.
  358. *
  359. * Assumptions:
  360. * - Client has provided only one Write chunk
  361. */
  362. static void svc_rdma_xdr_encode_write_list(__be32 *rdma_resp, __be32 *wr_ch,
  363. unsigned int consumed)
  364. {
  365. unsigned int nsegs;
  366. __be32 *p, *q;
  367. /* RPC-over-RDMA V1 replies never have a Read list. */
  368. p = rdma_resp + rpcrdma_fixed_maxsz + 1;
  369. q = wr_ch;
  370. while (*q != xdr_zero) {
  371. nsegs = xdr_encode_write_chunk(p, q, consumed);
  372. q += 2 + nsegs * rpcrdma_segment_maxsz;
  373. p += 2 + nsegs * rpcrdma_segment_maxsz;
  374. consumed = 0;
  375. }
  376. /* Terminate Write list */
  377. *p++ = xdr_zero;
  378. /* Reply chunk discriminator; may be replaced later */
  379. *p = xdr_zero;
  380. }
  381. /* The client provided a Reply chunk in the Call message. Fill in
  382. * the segments in the Reply chunk in the Reply message with the
  383. * number of bytes consumed in each segment.
  384. *
  385. * Assumptions:
  386. * - Reply can always fit in the provided Reply chunk
  387. */
  388. static void svc_rdma_xdr_encode_reply_chunk(__be32 *rdma_resp, __be32 *rp_ch,
  389. unsigned int consumed)
  390. {
  391. __be32 *p;
  392. /* Find the Reply chunk in the Reply's xprt header.
  393. * RPC-over-RDMA V1 replies never have a Read list.
  394. */
  395. p = rdma_resp + rpcrdma_fixed_maxsz + 1;
  396. /* Skip past Write list */
  397. while (*p++ != xdr_zero)
  398. p += 1 + be32_to_cpup(p) * rpcrdma_segment_maxsz;
  399. xdr_encode_write_chunk(p, rp_ch, consumed);
  400. }
  401. /* Parse the RPC Call's transport header.
  402. */
  403. static void svc_rdma_get_write_arrays(__be32 *rdma_argp,
  404. __be32 **write, __be32 **reply)
  405. {
  406. __be32 *p;
  407. p = rdma_argp + rpcrdma_fixed_maxsz;
  408. /* Read list */
  409. while (*p++ != xdr_zero)
  410. p += 5;
  411. /* Write list */
  412. if (*p != xdr_zero) {
  413. *write = p;
  414. while (*p++ != xdr_zero)
  415. p += 1 + be32_to_cpu(*p) * 4;
  416. } else {
  417. *write = NULL;
  418. p++;
  419. }
  420. /* Reply chunk */
  421. if (*p != xdr_zero)
  422. *reply = p;
  423. else
  424. *reply = NULL;
  425. }
  426. /* RPC-over-RDMA Version One private extension: Remote Invalidation.
  427. * Responder's choice: requester signals it can handle Send With
  428. * Invalidate, and responder chooses one rkey to invalidate.
  429. *
  430. * Find a candidate rkey to invalidate when sending a reply. Picks the
  431. * first R_key it finds in the chunk lists.
  432. *
  433. * Returns zero if RPC's chunk lists are empty.
  434. */
  435. static u32 svc_rdma_get_inv_rkey(__be32 *rdma_argp,
  436. __be32 *wr_lst, __be32 *rp_ch)
  437. {
  438. __be32 *p;
  439. p = rdma_argp + rpcrdma_fixed_maxsz;
  440. if (*p != xdr_zero)
  441. p += 2;
  442. else if (wr_lst && be32_to_cpup(wr_lst + 1))
  443. p = wr_lst + 2;
  444. else if (rp_ch && be32_to_cpup(rp_ch + 1))
  445. p = rp_ch + 2;
  446. else
  447. return 0;
  448. return be32_to_cpup(p);
  449. }
  450. static int svc_rdma_dma_map_page(struct svcxprt_rdma *rdma,
  451. struct svc_rdma_send_ctxt *ctxt,
  452. struct page *page,
  453. unsigned long offset,
  454. unsigned int len)
  455. {
  456. struct ib_device *dev = rdma->sc_cm_id->device;
  457. dma_addr_t dma_addr;
  458. dma_addr = ib_dma_map_page(dev, page, offset, len, DMA_TO_DEVICE);
  459. if (ib_dma_mapping_error(dev, dma_addr))
  460. goto out_maperr;
  461. ctxt->sc_sges[ctxt->sc_cur_sge_no].addr = dma_addr;
  462. ctxt->sc_sges[ctxt->sc_cur_sge_no].length = len;
  463. ctxt->sc_send_wr.num_sge++;
  464. return 0;
  465. out_maperr:
  466. trace_svcrdma_dma_map_page(rdma, page);
  467. return -EIO;
  468. }
  469. /* ib_dma_map_page() is used here because svc_rdma_dma_unmap()
  470. * handles DMA-unmap and it uses ib_dma_unmap_page() exclusively.
  471. */
  472. static int svc_rdma_dma_map_buf(struct svcxprt_rdma *rdma,
  473. struct svc_rdma_send_ctxt *ctxt,
  474. unsigned char *base,
  475. unsigned int len)
  476. {
  477. return svc_rdma_dma_map_page(rdma, ctxt, virt_to_page(base),
  478. offset_in_page(base), len);
  479. }
  480. /**
  481. * svc_rdma_sync_reply_hdr - DMA sync the transport header buffer
  482. * @rdma: controlling transport
  483. * @ctxt: send_ctxt for the Send WR
  484. * @len: length of transport header
  485. *
  486. */
  487. void svc_rdma_sync_reply_hdr(struct svcxprt_rdma *rdma,
  488. struct svc_rdma_send_ctxt *ctxt,
  489. unsigned int len)
  490. {
  491. ctxt->sc_sges[0].length = len;
  492. ctxt->sc_send_wr.num_sge++;
  493. ib_dma_sync_single_for_device(rdma->sc_pd->device,
  494. ctxt->sc_sges[0].addr, len,
  495. DMA_TO_DEVICE);
  496. }
  497. /* If the xdr_buf has more elements than the device can
  498. * transmit in a single RDMA Send, then the reply will
  499. * have to be copied into a bounce buffer.
  500. */
  501. static bool svc_rdma_pull_up_needed(struct svcxprt_rdma *rdma,
  502. struct xdr_buf *xdr,
  503. __be32 *wr_lst)
  504. {
  505. int elements;
  506. /* xdr->head */
  507. elements = 1;
  508. /* xdr->pages */
  509. if (!wr_lst) {
  510. unsigned int remaining;
  511. unsigned long pageoff;
  512. pageoff = xdr->page_base & ~PAGE_MASK;
  513. remaining = xdr->page_len;
  514. while (remaining) {
  515. ++elements;
  516. remaining -= min_t(u32, PAGE_SIZE - pageoff,
  517. remaining);
  518. pageoff = 0;
  519. }
  520. }
  521. /* xdr->tail */
  522. if (xdr->tail[0].iov_len)
  523. ++elements;
  524. /* assume 1 SGE is needed for the transport header */
  525. return elements >= rdma->sc_max_send_sges;
  526. }
  527. /* The device is not capable of sending the reply directly.
  528. * Assemble the elements of @xdr into the transport header
  529. * buffer.
  530. */
  531. static int svc_rdma_pull_up_reply_msg(struct svcxprt_rdma *rdma,
  532. struct svc_rdma_send_ctxt *ctxt,
  533. struct xdr_buf *xdr, __be32 *wr_lst)
  534. {
  535. unsigned char *dst, *tailbase;
  536. unsigned int taillen;
  537. dst = ctxt->sc_xprt_buf;
  538. dst += ctxt->sc_sges[0].length;
  539. memcpy(dst, xdr->head[0].iov_base, xdr->head[0].iov_len);
  540. dst += xdr->head[0].iov_len;
  541. tailbase = xdr->tail[0].iov_base;
  542. taillen = xdr->tail[0].iov_len;
  543. if (wr_lst) {
  544. u32 xdrpad;
  545. xdrpad = xdr_padsize(xdr->page_len);
  546. if (taillen && xdrpad) {
  547. tailbase += xdrpad;
  548. taillen -= xdrpad;
  549. }
  550. } else {
  551. unsigned int len, remaining;
  552. unsigned long pageoff;
  553. struct page **ppages;
  554. ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
  555. pageoff = xdr->page_base & ~PAGE_MASK;
  556. remaining = xdr->page_len;
  557. while (remaining) {
  558. len = min_t(u32, PAGE_SIZE - pageoff, remaining);
  559. memcpy(dst, page_address(*ppages) + pageoff, len);
  560. remaining -= len;
  561. dst += len;
  562. pageoff = 0;
  563. ppages++;
  564. }
  565. }
  566. if (taillen)
  567. memcpy(dst, tailbase, taillen);
  568. ctxt->sc_sges[0].length += xdr->len;
  569. ib_dma_sync_single_for_device(rdma->sc_pd->device,
  570. ctxt->sc_sges[0].addr,
  571. ctxt->sc_sges[0].length,
  572. DMA_TO_DEVICE);
  573. return 0;
  574. }
  575. /* svc_rdma_map_reply_msg - Map the buffer holding RPC message
  576. * @rdma: controlling transport
  577. * @ctxt: send_ctxt for the Send WR
  578. * @xdr: prepared xdr_buf containing RPC message
  579. * @wr_lst: pointer to Call header's Write list, or NULL
  580. *
  581. * Load the xdr_buf into the ctxt's sge array, and DMA map each
  582. * element as it is added.
  583. *
  584. * Returns zero on success, or a negative errno on failure.
  585. */
  586. int svc_rdma_map_reply_msg(struct svcxprt_rdma *rdma,
  587. struct svc_rdma_send_ctxt *ctxt,
  588. struct xdr_buf *xdr, __be32 *wr_lst)
  589. {
  590. unsigned int len, remaining;
  591. unsigned long page_off;
  592. struct page **ppages;
  593. unsigned char *base;
  594. u32 xdr_pad;
  595. int ret;
  596. if (svc_rdma_pull_up_needed(rdma, xdr, wr_lst))
  597. return svc_rdma_pull_up_reply_msg(rdma, ctxt, xdr, wr_lst);
  598. ++ctxt->sc_cur_sge_no;
  599. ret = svc_rdma_dma_map_buf(rdma, ctxt,
  600. xdr->head[0].iov_base,
  601. xdr->head[0].iov_len);
  602. if (ret < 0)
  603. return ret;
  604. /* If a Write chunk is present, the xdr_buf's page list
  605. * is not included inline. However the Upper Layer may
  606. * have added XDR padding in the tail buffer, and that
  607. * should not be included inline.
  608. */
  609. if (wr_lst) {
  610. base = xdr->tail[0].iov_base;
  611. len = xdr->tail[0].iov_len;
  612. xdr_pad = xdr_padsize(xdr->page_len);
  613. if (len && xdr_pad) {
  614. base += xdr_pad;
  615. len -= xdr_pad;
  616. }
  617. goto tail;
  618. }
  619. ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
  620. page_off = xdr->page_base & ~PAGE_MASK;
  621. remaining = xdr->page_len;
  622. while (remaining) {
  623. len = min_t(u32, PAGE_SIZE - page_off, remaining);
  624. ++ctxt->sc_cur_sge_no;
  625. ret = svc_rdma_dma_map_page(rdma, ctxt, *ppages++,
  626. page_off, len);
  627. if (ret < 0)
  628. return ret;
  629. remaining -= len;
  630. page_off = 0;
  631. }
  632. base = xdr->tail[0].iov_base;
  633. len = xdr->tail[0].iov_len;
  634. tail:
  635. if (len) {
  636. ++ctxt->sc_cur_sge_no;
  637. ret = svc_rdma_dma_map_buf(rdma, ctxt, base, len);
  638. if (ret < 0)
  639. return ret;
  640. }
  641. return 0;
  642. }
  643. /* The svc_rqst and all resources it owns are released as soon as
  644. * svc_rdma_sendto returns. Transfer pages under I/O to the ctxt
  645. * so they are released by the Send completion handler.
  646. */
  647. static void svc_rdma_save_io_pages(struct svc_rqst *rqstp,
  648. struct svc_rdma_send_ctxt *ctxt)
  649. {
  650. int i, pages = rqstp->rq_next_page - rqstp->rq_respages;
  651. ctxt->sc_page_count += pages;
  652. for (i = 0; i < pages; i++) {
  653. ctxt->sc_pages[i] = rqstp->rq_respages[i];
  654. rqstp->rq_respages[i] = NULL;
  655. }
  656. /* Prevent svc_xprt_release from releasing pages in rq_pages */
  657. rqstp->rq_next_page = rqstp->rq_respages;
  658. }
  659. /* Prepare the portion of the RPC Reply that will be transmitted
  660. * via RDMA Send. The RPC-over-RDMA transport header is prepared
  661. * in sc_sges[0], and the RPC xdr_buf is prepared in following sges.
  662. *
  663. * Depending on whether a Write list or Reply chunk is present,
  664. * the server may send all, a portion of, or none of the xdr_buf.
  665. * In the latter case, only the transport header (sc_sges[0]) is
  666. * transmitted.
  667. *
  668. * RDMA Send is the last step of transmitting an RPC reply. Pages
  669. * involved in the earlier RDMA Writes are here transferred out
  670. * of the rqstp and into the ctxt's page array. These pages are
  671. * DMA unmapped by each Write completion, but the subsequent Send
  672. * completion finally releases these pages.
  673. *
  674. * Assumptions:
  675. * - The Reply's transport header will never be larger than a page.
  676. */
  677. static int svc_rdma_send_reply_msg(struct svcxprt_rdma *rdma,
  678. struct svc_rdma_send_ctxt *ctxt,
  679. __be32 *rdma_argp,
  680. struct svc_rqst *rqstp,
  681. __be32 *wr_lst, __be32 *rp_ch)
  682. {
  683. int ret;
  684. if (!rp_ch) {
  685. ret = svc_rdma_map_reply_msg(rdma, ctxt,
  686. &rqstp->rq_res, wr_lst);
  687. if (ret < 0)
  688. return ret;
  689. }
  690. svc_rdma_save_io_pages(rqstp, ctxt);
  691. ctxt->sc_send_wr.opcode = IB_WR_SEND;
  692. if (rdma->sc_snd_w_inv) {
  693. ctxt->sc_send_wr.ex.invalidate_rkey =
  694. svc_rdma_get_inv_rkey(rdma_argp, wr_lst, rp_ch);
  695. if (ctxt->sc_send_wr.ex.invalidate_rkey)
  696. ctxt->sc_send_wr.opcode = IB_WR_SEND_WITH_INV;
  697. }
  698. dprintk("svcrdma: posting Send WR with %u sge(s)\n",
  699. ctxt->sc_send_wr.num_sge);
  700. return svc_rdma_send(rdma, &ctxt->sc_send_wr);
  701. }
  702. /* Given the client-provided Write and Reply chunks, the server was not
  703. * able to form a complete reply. Return an RDMA_ERROR message so the
  704. * client can retire this RPC transaction. As above, the Send completion
  705. * routine releases payload pages that were part of a previous RDMA Write.
  706. *
  707. * Remote Invalidation is skipped for simplicity.
  708. */
  709. static int svc_rdma_send_error_msg(struct svcxprt_rdma *rdma,
  710. struct svc_rdma_send_ctxt *ctxt,
  711. struct svc_rqst *rqstp)
  712. {
  713. __be32 *p;
  714. int ret;
  715. p = ctxt->sc_xprt_buf;
  716. trace_svcrdma_err_chunk(*p);
  717. p += 3;
  718. *p++ = rdma_error;
  719. *p = err_chunk;
  720. svc_rdma_sync_reply_hdr(rdma, ctxt, RPCRDMA_HDRLEN_ERR);
  721. svc_rdma_save_io_pages(rqstp, ctxt);
  722. ctxt->sc_send_wr.opcode = IB_WR_SEND;
  723. ret = svc_rdma_send(rdma, &ctxt->sc_send_wr);
  724. if (ret) {
  725. svc_rdma_send_ctxt_put(rdma, ctxt);
  726. return ret;
  727. }
  728. return 0;
  729. }
  730. void svc_rdma_prep_reply_hdr(struct svc_rqst *rqstp)
  731. {
  732. }
  733. /**
  734. * svc_rdma_sendto - Transmit an RPC reply
  735. * @rqstp: processed RPC request, reply XDR already in ::rq_res
  736. *
  737. * Any resources still associated with @rqstp are released upon return.
  738. * If no reply message was possible, the connection is closed.
  739. *
  740. * Returns:
  741. * %0 if an RPC reply has been successfully posted,
  742. * %-ENOMEM if a resource shortage occurred (connection is lost),
  743. * %-ENOTCONN if posting failed (connection is lost).
  744. */
  745. int svc_rdma_sendto(struct svc_rqst *rqstp)
  746. {
  747. struct svc_xprt *xprt = rqstp->rq_xprt;
  748. struct svcxprt_rdma *rdma =
  749. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  750. struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
  751. __be32 *p, *rdma_argp, *rdma_resp, *wr_lst, *rp_ch;
  752. struct xdr_buf *xdr = &rqstp->rq_res;
  753. struct svc_rdma_send_ctxt *sctxt;
  754. int ret;
  755. rdma_argp = rctxt->rc_recv_buf;
  756. svc_rdma_get_write_arrays(rdma_argp, &wr_lst, &rp_ch);
  757. /* Create the RDMA response header. xprt->xpt_mutex,
  758. * acquired in svc_send(), serializes RPC replies. The
  759. * code path below that inserts the credit grant value
  760. * into each transport header runs only inside this
  761. * critical section.
  762. */
  763. ret = -ENOMEM;
  764. sctxt = svc_rdma_send_ctxt_get(rdma);
  765. if (!sctxt)
  766. goto err0;
  767. rdma_resp = sctxt->sc_xprt_buf;
  768. p = rdma_resp;
  769. *p++ = *rdma_argp;
  770. *p++ = *(rdma_argp + 1);
  771. *p++ = rdma->sc_fc_credits;
  772. *p++ = rp_ch ? rdma_nomsg : rdma_msg;
  773. /* Start with empty chunks */
  774. *p++ = xdr_zero;
  775. *p++ = xdr_zero;
  776. *p = xdr_zero;
  777. if (wr_lst) {
  778. /* XXX: Presume the client sent only one Write chunk */
  779. ret = svc_rdma_send_write_chunk(rdma, wr_lst, xdr);
  780. if (ret < 0)
  781. goto err2;
  782. svc_rdma_xdr_encode_write_list(rdma_resp, wr_lst, ret);
  783. }
  784. if (rp_ch) {
  785. ret = svc_rdma_send_reply_chunk(rdma, rp_ch, wr_lst, xdr);
  786. if (ret < 0)
  787. goto err2;
  788. svc_rdma_xdr_encode_reply_chunk(rdma_resp, rp_ch, ret);
  789. }
  790. svc_rdma_sync_reply_hdr(rdma, sctxt, svc_rdma_reply_hdr_len(rdma_resp));
  791. ret = svc_rdma_send_reply_msg(rdma, sctxt, rdma_argp, rqstp,
  792. wr_lst, rp_ch);
  793. if (ret < 0)
  794. goto err1;
  795. return 0;
  796. err2:
  797. if (ret != -E2BIG && ret != -EINVAL)
  798. goto err1;
  799. ret = svc_rdma_send_error_msg(rdma, sctxt, rqstp);
  800. if (ret < 0)
  801. goto err1;
  802. return 0;
  803. err1:
  804. svc_rdma_send_ctxt_put(rdma, sctxt);
  805. err0:
  806. trace_svcrdma_send_failed(rqstp, ret);
  807. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  808. return -ENOTCONN;
  809. }