svc_rdma_sendto.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 <[email protected]>
  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/svc_rdma.h>
  106. #include "xprt_rdma.h"
  107. #include <trace/events/rpcrdma.h>
  108. static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc);
  109. static void svc_rdma_send_cid_init(struct svcxprt_rdma *rdma,
  110. struct rpc_rdma_cid *cid)
  111. {
  112. cid->ci_queue_id = rdma->sc_sq_cq->res.id;
  113. cid->ci_completion_id = atomic_inc_return(&rdma->sc_completion_ids);
  114. }
  115. static struct svc_rdma_send_ctxt *
  116. svc_rdma_send_ctxt_alloc(struct svcxprt_rdma *rdma)
  117. {
  118. struct svc_rdma_send_ctxt *ctxt;
  119. dma_addr_t addr;
  120. void *buffer;
  121. size_t size;
  122. int i;
  123. size = sizeof(*ctxt);
  124. size += rdma->sc_max_send_sges * sizeof(struct ib_sge);
  125. ctxt = kmalloc(size, GFP_KERNEL);
  126. if (!ctxt)
  127. goto fail0;
  128. buffer = kmalloc(rdma->sc_max_req_size, GFP_KERNEL);
  129. if (!buffer)
  130. goto fail1;
  131. addr = ib_dma_map_single(rdma->sc_pd->device, buffer,
  132. rdma->sc_max_req_size, DMA_TO_DEVICE);
  133. if (ib_dma_mapping_error(rdma->sc_pd->device, addr))
  134. goto fail2;
  135. svc_rdma_send_cid_init(rdma, &ctxt->sc_cid);
  136. ctxt->sc_send_wr.next = NULL;
  137. ctxt->sc_send_wr.wr_cqe = &ctxt->sc_cqe;
  138. ctxt->sc_send_wr.sg_list = ctxt->sc_sges;
  139. ctxt->sc_send_wr.send_flags = IB_SEND_SIGNALED;
  140. init_completion(&ctxt->sc_done);
  141. ctxt->sc_cqe.done = svc_rdma_wc_send;
  142. ctxt->sc_xprt_buf = buffer;
  143. xdr_buf_init(&ctxt->sc_hdrbuf, ctxt->sc_xprt_buf,
  144. rdma->sc_max_req_size);
  145. ctxt->sc_sges[0].addr = addr;
  146. for (i = 0; i < rdma->sc_max_send_sges; i++)
  147. ctxt->sc_sges[i].lkey = rdma->sc_pd->local_dma_lkey;
  148. return ctxt;
  149. fail2:
  150. kfree(buffer);
  151. fail1:
  152. kfree(ctxt);
  153. fail0:
  154. return NULL;
  155. }
  156. /**
  157. * svc_rdma_send_ctxts_destroy - Release all send_ctxt's for an xprt
  158. * @rdma: svcxprt_rdma being torn down
  159. *
  160. */
  161. void svc_rdma_send_ctxts_destroy(struct svcxprt_rdma *rdma)
  162. {
  163. struct svc_rdma_send_ctxt *ctxt;
  164. struct llist_node *node;
  165. while ((node = llist_del_first(&rdma->sc_send_ctxts)) != NULL) {
  166. ctxt = llist_entry(node, struct svc_rdma_send_ctxt, sc_node);
  167. ib_dma_unmap_single(rdma->sc_pd->device,
  168. ctxt->sc_sges[0].addr,
  169. rdma->sc_max_req_size,
  170. DMA_TO_DEVICE);
  171. kfree(ctxt->sc_xprt_buf);
  172. kfree(ctxt);
  173. }
  174. }
  175. /**
  176. * svc_rdma_send_ctxt_get - Get a free send_ctxt
  177. * @rdma: controlling svcxprt_rdma
  178. *
  179. * Returns a ready-to-use send_ctxt, or NULL if none are
  180. * available and a fresh one cannot be allocated.
  181. */
  182. struct svc_rdma_send_ctxt *svc_rdma_send_ctxt_get(struct svcxprt_rdma *rdma)
  183. {
  184. struct svc_rdma_send_ctxt *ctxt;
  185. struct llist_node *node;
  186. spin_lock(&rdma->sc_send_lock);
  187. node = llist_del_first(&rdma->sc_send_ctxts);
  188. if (!node)
  189. goto out_empty;
  190. ctxt = llist_entry(node, struct svc_rdma_send_ctxt, sc_node);
  191. spin_unlock(&rdma->sc_send_lock);
  192. out:
  193. rpcrdma_set_xdrlen(&ctxt->sc_hdrbuf, 0);
  194. xdr_init_encode(&ctxt->sc_stream, &ctxt->sc_hdrbuf,
  195. ctxt->sc_xprt_buf, NULL);
  196. ctxt->sc_send_wr.num_sge = 0;
  197. ctxt->sc_cur_sge_no = 0;
  198. return ctxt;
  199. out_empty:
  200. spin_unlock(&rdma->sc_send_lock);
  201. ctxt = svc_rdma_send_ctxt_alloc(rdma);
  202. if (!ctxt)
  203. return NULL;
  204. goto out;
  205. }
  206. /**
  207. * svc_rdma_send_ctxt_put - Return send_ctxt to free list
  208. * @rdma: controlling svcxprt_rdma
  209. * @ctxt: object to return to the free list
  210. */
  211. void svc_rdma_send_ctxt_put(struct svcxprt_rdma *rdma,
  212. struct svc_rdma_send_ctxt *ctxt)
  213. {
  214. struct ib_device *device = rdma->sc_cm_id->device;
  215. unsigned int i;
  216. /* The first SGE contains the transport header, which
  217. * remains mapped until @ctxt is destroyed.
  218. */
  219. for (i = 1; i < ctxt->sc_send_wr.num_sge; i++) {
  220. ib_dma_unmap_page(device,
  221. ctxt->sc_sges[i].addr,
  222. ctxt->sc_sges[i].length,
  223. DMA_TO_DEVICE);
  224. trace_svcrdma_dma_unmap_page(rdma,
  225. ctxt->sc_sges[i].addr,
  226. ctxt->sc_sges[i].length);
  227. }
  228. llist_add(&ctxt->sc_node, &rdma->sc_send_ctxts);
  229. }
  230. /**
  231. * svc_rdma_wake_send_waiters - manage Send Queue accounting
  232. * @rdma: controlling transport
  233. * @avail: Number of additional SQEs that are now available
  234. *
  235. */
  236. void svc_rdma_wake_send_waiters(struct svcxprt_rdma *rdma, int avail)
  237. {
  238. atomic_add(avail, &rdma->sc_sq_avail);
  239. smp_mb__after_atomic();
  240. if (unlikely(waitqueue_active(&rdma->sc_send_wait)))
  241. wake_up(&rdma->sc_send_wait);
  242. }
  243. /**
  244. * svc_rdma_wc_send - Invoked by RDMA provider for each polled Send WC
  245. * @cq: Completion Queue context
  246. * @wc: Work Completion object
  247. *
  248. * NB: The svc_xprt/svcxprt_rdma is pinned whenever it's possible that
  249. * the Send completion handler could be running.
  250. */
  251. static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
  252. {
  253. struct svcxprt_rdma *rdma = cq->cq_context;
  254. struct ib_cqe *cqe = wc->wr_cqe;
  255. struct svc_rdma_send_ctxt *ctxt =
  256. container_of(cqe, struct svc_rdma_send_ctxt, sc_cqe);
  257. svc_rdma_wake_send_waiters(rdma, 1);
  258. complete(&ctxt->sc_done);
  259. if (unlikely(wc->status != IB_WC_SUCCESS))
  260. goto flushed;
  261. trace_svcrdma_wc_send(wc, &ctxt->sc_cid);
  262. return;
  263. flushed:
  264. if (wc->status != IB_WC_WR_FLUSH_ERR)
  265. trace_svcrdma_wc_send_err(wc, &ctxt->sc_cid);
  266. else
  267. trace_svcrdma_wc_send_flush(wc, &ctxt->sc_cid);
  268. svc_xprt_deferred_close(&rdma->sc_xprt);
  269. }
  270. /**
  271. * svc_rdma_send - Post a single Send WR
  272. * @rdma: transport on which to post the WR
  273. * @ctxt: send ctxt with a Send WR ready to post
  274. *
  275. * Returns zero if the Send WR was posted successfully. Otherwise, a
  276. * negative errno is returned.
  277. */
  278. int svc_rdma_send(struct svcxprt_rdma *rdma, struct svc_rdma_send_ctxt *ctxt)
  279. {
  280. struct ib_send_wr *wr = &ctxt->sc_send_wr;
  281. int ret;
  282. reinit_completion(&ctxt->sc_done);
  283. /* Sync the transport header buffer */
  284. ib_dma_sync_single_for_device(rdma->sc_pd->device,
  285. wr->sg_list[0].addr,
  286. wr->sg_list[0].length,
  287. DMA_TO_DEVICE);
  288. /* If the SQ is full, wait until an SQ entry is available */
  289. while (1) {
  290. if ((atomic_dec_return(&rdma->sc_sq_avail) < 0)) {
  291. percpu_counter_inc(&svcrdma_stat_sq_starve);
  292. trace_svcrdma_sq_full(rdma);
  293. atomic_inc(&rdma->sc_sq_avail);
  294. wait_event(rdma->sc_send_wait,
  295. atomic_read(&rdma->sc_sq_avail) > 1);
  296. if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags))
  297. return -ENOTCONN;
  298. trace_svcrdma_sq_retry(rdma);
  299. continue;
  300. }
  301. trace_svcrdma_post_send(ctxt);
  302. ret = ib_post_send(rdma->sc_qp, wr, NULL);
  303. if (ret)
  304. break;
  305. return 0;
  306. }
  307. trace_svcrdma_sq_post_err(rdma, ret);
  308. svc_xprt_deferred_close(&rdma->sc_xprt);
  309. wake_up(&rdma->sc_send_wait);
  310. return ret;
  311. }
  312. /**
  313. * svc_rdma_encode_read_list - Encode RPC Reply's Read chunk list
  314. * @sctxt: Send context for the RPC Reply
  315. *
  316. * Return values:
  317. * On success, returns length in bytes of the Reply XDR buffer
  318. * that was consumed by the Reply Read list
  319. * %-EMSGSIZE on XDR buffer overflow
  320. */
  321. static ssize_t svc_rdma_encode_read_list(struct svc_rdma_send_ctxt *sctxt)
  322. {
  323. /* RPC-over-RDMA version 1 replies never have a Read list. */
  324. return xdr_stream_encode_item_absent(&sctxt->sc_stream);
  325. }
  326. /**
  327. * svc_rdma_encode_write_segment - Encode one Write segment
  328. * @sctxt: Send context for the RPC Reply
  329. * @chunk: Write chunk to push
  330. * @remaining: remaining bytes of the payload left in the Write chunk
  331. * @segno: which segment in the chunk
  332. *
  333. * Return values:
  334. * On success, returns length in bytes of the Reply XDR buffer
  335. * that was consumed by the Write segment, and updates @remaining
  336. * %-EMSGSIZE on XDR buffer overflow
  337. */
  338. static ssize_t svc_rdma_encode_write_segment(struct svc_rdma_send_ctxt *sctxt,
  339. const struct svc_rdma_chunk *chunk,
  340. u32 *remaining, unsigned int segno)
  341. {
  342. const struct svc_rdma_segment *segment = &chunk->ch_segments[segno];
  343. const size_t len = rpcrdma_segment_maxsz * sizeof(__be32);
  344. u32 length;
  345. __be32 *p;
  346. p = xdr_reserve_space(&sctxt->sc_stream, len);
  347. if (!p)
  348. return -EMSGSIZE;
  349. length = min_t(u32, *remaining, segment->rs_length);
  350. *remaining -= length;
  351. xdr_encode_rdma_segment(p, segment->rs_handle, length,
  352. segment->rs_offset);
  353. trace_svcrdma_encode_wseg(sctxt, segno, segment->rs_handle, length,
  354. segment->rs_offset);
  355. return len;
  356. }
  357. /**
  358. * svc_rdma_encode_write_chunk - Encode one Write chunk
  359. * @sctxt: Send context for the RPC Reply
  360. * @chunk: Write chunk to push
  361. *
  362. * Copy a Write chunk from the Call transport header to the
  363. * Reply transport header. Update each segment's length field
  364. * to reflect the number of bytes written in that segment.
  365. *
  366. * Return values:
  367. * On success, returns length in bytes of the Reply XDR buffer
  368. * that was consumed by the Write chunk
  369. * %-EMSGSIZE on XDR buffer overflow
  370. */
  371. static ssize_t svc_rdma_encode_write_chunk(struct svc_rdma_send_ctxt *sctxt,
  372. const struct svc_rdma_chunk *chunk)
  373. {
  374. u32 remaining = chunk->ch_payload_length;
  375. unsigned int segno;
  376. ssize_t len, ret;
  377. len = 0;
  378. ret = xdr_stream_encode_item_present(&sctxt->sc_stream);
  379. if (ret < 0)
  380. return ret;
  381. len += ret;
  382. ret = xdr_stream_encode_u32(&sctxt->sc_stream, chunk->ch_segcount);
  383. if (ret < 0)
  384. return ret;
  385. len += ret;
  386. for (segno = 0; segno < chunk->ch_segcount; segno++) {
  387. ret = svc_rdma_encode_write_segment(sctxt, chunk, &remaining, segno);
  388. if (ret < 0)
  389. return ret;
  390. len += ret;
  391. }
  392. return len;
  393. }
  394. /**
  395. * svc_rdma_encode_write_list - Encode RPC Reply's Write chunk list
  396. * @rctxt: Reply context with information about the RPC Call
  397. * @sctxt: Send context for the RPC Reply
  398. *
  399. * Return values:
  400. * On success, returns length in bytes of the Reply XDR buffer
  401. * that was consumed by the Reply's Write list
  402. * %-EMSGSIZE on XDR buffer overflow
  403. */
  404. static ssize_t svc_rdma_encode_write_list(struct svc_rdma_recv_ctxt *rctxt,
  405. struct svc_rdma_send_ctxt *sctxt)
  406. {
  407. struct svc_rdma_chunk *chunk;
  408. ssize_t len, ret;
  409. len = 0;
  410. pcl_for_each_chunk(chunk, &rctxt->rc_write_pcl) {
  411. ret = svc_rdma_encode_write_chunk(sctxt, chunk);
  412. if (ret < 0)
  413. return ret;
  414. len += ret;
  415. }
  416. /* Terminate the Write list */
  417. ret = xdr_stream_encode_item_absent(&sctxt->sc_stream);
  418. if (ret < 0)
  419. return ret;
  420. return len + ret;
  421. }
  422. /**
  423. * svc_rdma_encode_reply_chunk - Encode RPC Reply's Reply chunk
  424. * @rctxt: Reply context with information about the RPC Call
  425. * @sctxt: Send context for the RPC Reply
  426. * @length: size in bytes of the payload in the Reply chunk
  427. *
  428. * Return values:
  429. * On success, returns length in bytes of the Reply XDR buffer
  430. * that was consumed by the Reply's Reply chunk
  431. * %-EMSGSIZE on XDR buffer overflow
  432. * %-E2BIG if the RPC message is larger than the Reply chunk
  433. */
  434. static ssize_t
  435. svc_rdma_encode_reply_chunk(struct svc_rdma_recv_ctxt *rctxt,
  436. struct svc_rdma_send_ctxt *sctxt,
  437. unsigned int length)
  438. {
  439. struct svc_rdma_chunk *chunk;
  440. if (pcl_is_empty(&rctxt->rc_reply_pcl))
  441. return xdr_stream_encode_item_absent(&sctxt->sc_stream);
  442. chunk = pcl_first_chunk(&rctxt->rc_reply_pcl);
  443. if (length > chunk->ch_length)
  444. return -E2BIG;
  445. chunk->ch_payload_length = length;
  446. return svc_rdma_encode_write_chunk(sctxt, chunk);
  447. }
  448. struct svc_rdma_map_data {
  449. struct svcxprt_rdma *md_rdma;
  450. struct svc_rdma_send_ctxt *md_ctxt;
  451. };
  452. /**
  453. * svc_rdma_page_dma_map - DMA map one page
  454. * @data: pointer to arguments
  455. * @page: struct page to DMA map
  456. * @offset: offset into the page
  457. * @len: number of bytes to map
  458. *
  459. * Returns:
  460. * %0 if DMA mapping was successful
  461. * %-EIO if the page cannot be DMA mapped
  462. */
  463. static int svc_rdma_page_dma_map(void *data, struct page *page,
  464. unsigned long offset, unsigned int len)
  465. {
  466. struct svc_rdma_map_data *args = data;
  467. struct svcxprt_rdma *rdma = args->md_rdma;
  468. struct svc_rdma_send_ctxt *ctxt = args->md_ctxt;
  469. struct ib_device *dev = rdma->sc_cm_id->device;
  470. dma_addr_t dma_addr;
  471. ++ctxt->sc_cur_sge_no;
  472. dma_addr = ib_dma_map_page(dev, page, offset, len, DMA_TO_DEVICE);
  473. if (ib_dma_mapping_error(dev, dma_addr))
  474. goto out_maperr;
  475. trace_svcrdma_dma_map_page(rdma, dma_addr, len);
  476. ctxt->sc_sges[ctxt->sc_cur_sge_no].addr = dma_addr;
  477. ctxt->sc_sges[ctxt->sc_cur_sge_no].length = len;
  478. ctxt->sc_send_wr.num_sge++;
  479. return 0;
  480. out_maperr:
  481. trace_svcrdma_dma_map_err(rdma, dma_addr, len);
  482. return -EIO;
  483. }
  484. /**
  485. * svc_rdma_iov_dma_map - DMA map an iovec
  486. * @data: pointer to arguments
  487. * @iov: kvec to DMA map
  488. *
  489. * ib_dma_map_page() is used here because svc_rdma_dma_unmap()
  490. * handles DMA-unmap and it uses ib_dma_unmap_page() exclusively.
  491. *
  492. * Returns:
  493. * %0 if DMA mapping was successful
  494. * %-EIO if the iovec cannot be DMA mapped
  495. */
  496. static int svc_rdma_iov_dma_map(void *data, const struct kvec *iov)
  497. {
  498. if (!iov->iov_len)
  499. return 0;
  500. return svc_rdma_page_dma_map(data, virt_to_page(iov->iov_base),
  501. offset_in_page(iov->iov_base),
  502. iov->iov_len);
  503. }
  504. /**
  505. * svc_rdma_xb_dma_map - DMA map all segments of an xdr_buf
  506. * @xdr: xdr_buf containing portion of an RPC message to transmit
  507. * @data: pointer to arguments
  508. *
  509. * Returns:
  510. * %0 if DMA mapping was successful
  511. * %-EIO if DMA mapping failed
  512. *
  513. * On failure, any DMA mappings that have been already done must be
  514. * unmapped by the caller.
  515. */
  516. static int svc_rdma_xb_dma_map(const struct xdr_buf *xdr, void *data)
  517. {
  518. unsigned int len, remaining;
  519. unsigned long pageoff;
  520. struct page **ppages;
  521. int ret;
  522. ret = svc_rdma_iov_dma_map(data, &xdr->head[0]);
  523. if (ret < 0)
  524. return ret;
  525. ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
  526. pageoff = offset_in_page(xdr->page_base);
  527. remaining = xdr->page_len;
  528. while (remaining) {
  529. len = min_t(u32, PAGE_SIZE - pageoff, remaining);
  530. ret = svc_rdma_page_dma_map(data, *ppages++, pageoff, len);
  531. if (ret < 0)
  532. return ret;
  533. remaining -= len;
  534. pageoff = 0;
  535. }
  536. ret = svc_rdma_iov_dma_map(data, &xdr->tail[0]);
  537. if (ret < 0)
  538. return ret;
  539. return xdr->len;
  540. }
  541. struct svc_rdma_pullup_data {
  542. u8 *pd_dest;
  543. unsigned int pd_length;
  544. unsigned int pd_num_sges;
  545. };
  546. /**
  547. * svc_rdma_xb_count_sges - Count how many SGEs will be needed
  548. * @xdr: xdr_buf containing portion of an RPC message to transmit
  549. * @data: pointer to arguments
  550. *
  551. * Returns:
  552. * Number of SGEs needed to Send the contents of @xdr inline
  553. */
  554. static int svc_rdma_xb_count_sges(const struct xdr_buf *xdr,
  555. void *data)
  556. {
  557. struct svc_rdma_pullup_data *args = data;
  558. unsigned int remaining;
  559. unsigned long offset;
  560. if (xdr->head[0].iov_len)
  561. ++args->pd_num_sges;
  562. offset = offset_in_page(xdr->page_base);
  563. remaining = xdr->page_len;
  564. while (remaining) {
  565. ++args->pd_num_sges;
  566. remaining -= min_t(u32, PAGE_SIZE - offset, remaining);
  567. offset = 0;
  568. }
  569. if (xdr->tail[0].iov_len)
  570. ++args->pd_num_sges;
  571. args->pd_length += xdr->len;
  572. return 0;
  573. }
  574. /**
  575. * svc_rdma_pull_up_needed - Determine whether to use pull-up
  576. * @rdma: controlling transport
  577. * @sctxt: send_ctxt for the Send WR
  578. * @rctxt: Write and Reply chunks provided by client
  579. * @xdr: xdr_buf containing RPC message to transmit
  580. *
  581. * Returns:
  582. * %true if pull-up must be used
  583. * %false otherwise
  584. */
  585. static bool svc_rdma_pull_up_needed(const struct svcxprt_rdma *rdma,
  586. const struct svc_rdma_send_ctxt *sctxt,
  587. const struct svc_rdma_recv_ctxt *rctxt,
  588. const struct xdr_buf *xdr)
  589. {
  590. /* Resources needed for the transport header */
  591. struct svc_rdma_pullup_data args = {
  592. .pd_length = sctxt->sc_hdrbuf.len,
  593. .pd_num_sges = 1,
  594. };
  595. int ret;
  596. ret = pcl_process_nonpayloads(&rctxt->rc_write_pcl, xdr,
  597. svc_rdma_xb_count_sges, &args);
  598. if (ret < 0)
  599. return false;
  600. if (args.pd_length < RPCRDMA_PULLUP_THRESH)
  601. return true;
  602. return args.pd_num_sges >= rdma->sc_max_send_sges;
  603. }
  604. /**
  605. * svc_rdma_xb_linearize - Copy region of xdr_buf to flat buffer
  606. * @xdr: xdr_buf containing portion of an RPC message to copy
  607. * @data: pointer to arguments
  608. *
  609. * Returns:
  610. * Always zero.
  611. */
  612. static int svc_rdma_xb_linearize(const struct xdr_buf *xdr,
  613. void *data)
  614. {
  615. struct svc_rdma_pullup_data *args = data;
  616. unsigned int len, remaining;
  617. unsigned long pageoff;
  618. struct page **ppages;
  619. if (xdr->head[0].iov_len) {
  620. memcpy(args->pd_dest, xdr->head[0].iov_base, xdr->head[0].iov_len);
  621. args->pd_dest += xdr->head[0].iov_len;
  622. }
  623. ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
  624. pageoff = offset_in_page(xdr->page_base);
  625. remaining = xdr->page_len;
  626. while (remaining) {
  627. len = min_t(u32, PAGE_SIZE - pageoff, remaining);
  628. memcpy(args->pd_dest, page_address(*ppages) + pageoff, len);
  629. remaining -= len;
  630. args->pd_dest += len;
  631. pageoff = 0;
  632. ppages++;
  633. }
  634. if (xdr->tail[0].iov_len) {
  635. memcpy(args->pd_dest, xdr->tail[0].iov_base, xdr->tail[0].iov_len);
  636. args->pd_dest += xdr->tail[0].iov_len;
  637. }
  638. args->pd_length += xdr->len;
  639. return 0;
  640. }
  641. /**
  642. * svc_rdma_pull_up_reply_msg - Copy Reply into a single buffer
  643. * @rdma: controlling transport
  644. * @sctxt: send_ctxt for the Send WR; xprt hdr is already prepared
  645. * @rctxt: Write and Reply chunks provided by client
  646. * @xdr: prepared xdr_buf containing RPC message
  647. *
  648. * The device is not capable of sending the reply directly.
  649. * Assemble the elements of @xdr into the transport header buffer.
  650. *
  651. * Assumptions:
  652. * pull_up_needed has determined that @xdr will fit in the buffer.
  653. *
  654. * Returns:
  655. * %0 if pull-up was successful
  656. * %-EMSGSIZE if a buffer manipulation problem occurred
  657. */
  658. static int svc_rdma_pull_up_reply_msg(const struct svcxprt_rdma *rdma,
  659. struct svc_rdma_send_ctxt *sctxt,
  660. const struct svc_rdma_recv_ctxt *rctxt,
  661. const struct xdr_buf *xdr)
  662. {
  663. struct svc_rdma_pullup_data args = {
  664. .pd_dest = sctxt->sc_xprt_buf + sctxt->sc_hdrbuf.len,
  665. };
  666. int ret;
  667. ret = pcl_process_nonpayloads(&rctxt->rc_write_pcl, xdr,
  668. svc_rdma_xb_linearize, &args);
  669. if (ret < 0)
  670. return ret;
  671. sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len + args.pd_length;
  672. trace_svcrdma_send_pullup(sctxt, args.pd_length);
  673. return 0;
  674. }
  675. /* svc_rdma_map_reply_msg - DMA map the buffer holding RPC message
  676. * @rdma: controlling transport
  677. * @sctxt: send_ctxt for the Send WR
  678. * @rctxt: Write and Reply chunks provided by client
  679. * @xdr: prepared xdr_buf containing RPC message
  680. *
  681. * Returns:
  682. * %0 if DMA mapping was successful.
  683. * %-EMSGSIZE if a buffer manipulation problem occurred
  684. * %-EIO if DMA mapping failed
  685. *
  686. * The Send WR's num_sge field is set in all cases.
  687. */
  688. int svc_rdma_map_reply_msg(struct svcxprt_rdma *rdma,
  689. struct svc_rdma_send_ctxt *sctxt,
  690. const struct svc_rdma_recv_ctxt *rctxt,
  691. const struct xdr_buf *xdr)
  692. {
  693. struct svc_rdma_map_data args = {
  694. .md_rdma = rdma,
  695. .md_ctxt = sctxt,
  696. };
  697. /* Set up the (persistently-mapped) transport header SGE. */
  698. sctxt->sc_send_wr.num_sge = 1;
  699. sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len;
  700. /* If there is a Reply chunk, nothing follows the transport
  701. * header, and we're done here.
  702. */
  703. if (!pcl_is_empty(&rctxt->rc_reply_pcl))
  704. return 0;
  705. /* For pull-up, svc_rdma_send() will sync the transport header.
  706. * No additional DMA mapping is necessary.
  707. */
  708. if (svc_rdma_pull_up_needed(rdma, sctxt, rctxt, xdr))
  709. return svc_rdma_pull_up_reply_msg(rdma, sctxt, rctxt, xdr);
  710. return pcl_process_nonpayloads(&rctxt->rc_write_pcl, xdr,
  711. svc_rdma_xb_dma_map, &args);
  712. }
  713. /* Prepare the portion of the RPC Reply that will be transmitted
  714. * via RDMA Send. The RPC-over-RDMA transport header is prepared
  715. * in sc_sges[0], and the RPC xdr_buf is prepared in following sges.
  716. *
  717. * Depending on whether a Write list or Reply chunk is present,
  718. * the server may send all, a portion of, or none of the xdr_buf.
  719. * In the latter case, only the transport header (sc_sges[0]) is
  720. * transmitted.
  721. *
  722. * RDMA Send is the last step of transmitting an RPC reply. Pages
  723. * involved in the earlier RDMA Writes are here transferred out
  724. * of the rqstp and into the sctxt's page array. These pages are
  725. * DMA unmapped by each Write completion, but the subsequent Send
  726. * completion finally releases these pages.
  727. *
  728. * Assumptions:
  729. * - The Reply's transport header will never be larger than a page.
  730. */
  731. static int svc_rdma_send_reply_msg(struct svcxprt_rdma *rdma,
  732. struct svc_rdma_send_ctxt *sctxt,
  733. const struct svc_rdma_recv_ctxt *rctxt,
  734. struct svc_rqst *rqstp)
  735. {
  736. int ret;
  737. ret = svc_rdma_map_reply_msg(rdma, sctxt, rctxt, &rqstp->rq_res);
  738. if (ret < 0)
  739. return ret;
  740. if (rctxt->rc_inv_rkey) {
  741. sctxt->sc_send_wr.opcode = IB_WR_SEND_WITH_INV;
  742. sctxt->sc_send_wr.ex.invalidate_rkey = rctxt->rc_inv_rkey;
  743. } else {
  744. sctxt->sc_send_wr.opcode = IB_WR_SEND;
  745. }
  746. ret = svc_rdma_send(rdma, sctxt);
  747. if (ret < 0)
  748. return ret;
  749. ret = wait_for_completion_killable(&sctxt->sc_done);
  750. svc_rdma_send_ctxt_put(rdma, sctxt);
  751. return ret;
  752. }
  753. /**
  754. * svc_rdma_send_error_msg - Send an RPC/RDMA v1 error response
  755. * @rdma: controlling transport context
  756. * @sctxt: Send context for the response
  757. * @rctxt: Receive context for incoming bad message
  758. * @status: negative errno indicating error that occurred
  759. *
  760. * Given the client-provided Read, Write, and Reply chunks, the
  761. * server was not able to parse the Call or form a complete Reply.
  762. * Return an RDMA_ERROR message so the client can retire the RPC
  763. * transaction.
  764. *
  765. * The caller does not have to release @sctxt. It is released by
  766. * Send completion, or by this function on error.
  767. */
  768. void svc_rdma_send_error_msg(struct svcxprt_rdma *rdma,
  769. struct svc_rdma_send_ctxt *sctxt,
  770. struct svc_rdma_recv_ctxt *rctxt,
  771. int status)
  772. {
  773. __be32 *rdma_argp = rctxt->rc_recv_buf;
  774. __be32 *p;
  775. rpcrdma_set_xdrlen(&sctxt->sc_hdrbuf, 0);
  776. xdr_init_encode(&sctxt->sc_stream, &sctxt->sc_hdrbuf,
  777. sctxt->sc_xprt_buf, NULL);
  778. p = xdr_reserve_space(&sctxt->sc_stream,
  779. rpcrdma_fixed_maxsz * sizeof(*p));
  780. if (!p)
  781. goto put_ctxt;
  782. *p++ = *rdma_argp;
  783. *p++ = *(rdma_argp + 1);
  784. *p++ = rdma->sc_fc_credits;
  785. *p = rdma_error;
  786. switch (status) {
  787. case -EPROTONOSUPPORT:
  788. p = xdr_reserve_space(&sctxt->sc_stream, 3 * sizeof(*p));
  789. if (!p)
  790. goto put_ctxt;
  791. *p++ = err_vers;
  792. *p++ = rpcrdma_version;
  793. *p = rpcrdma_version;
  794. trace_svcrdma_err_vers(*rdma_argp);
  795. break;
  796. default:
  797. p = xdr_reserve_space(&sctxt->sc_stream, sizeof(*p));
  798. if (!p)
  799. goto put_ctxt;
  800. *p = err_chunk;
  801. trace_svcrdma_err_chunk(*rdma_argp);
  802. }
  803. /* Remote Invalidation is skipped for simplicity. */
  804. sctxt->sc_send_wr.num_sge = 1;
  805. sctxt->sc_send_wr.opcode = IB_WR_SEND;
  806. sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len;
  807. if (svc_rdma_send(rdma, sctxt))
  808. goto put_ctxt;
  809. wait_for_completion_killable(&sctxt->sc_done);
  810. put_ctxt:
  811. svc_rdma_send_ctxt_put(rdma, sctxt);
  812. }
  813. /**
  814. * svc_rdma_sendto - Transmit an RPC reply
  815. * @rqstp: processed RPC request, reply XDR already in ::rq_res
  816. *
  817. * Any resources still associated with @rqstp are released upon return.
  818. * If no reply message was possible, the connection is closed.
  819. *
  820. * Returns:
  821. * %0 if an RPC reply has been successfully posted,
  822. * %-ENOMEM if a resource shortage occurred (connection is lost),
  823. * %-ENOTCONN if posting failed (connection is lost).
  824. */
  825. int svc_rdma_sendto(struct svc_rqst *rqstp)
  826. {
  827. struct svc_xprt *xprt = rqstp->rq_xprt;
  828. struct svcxprt_rdma *rdma =
  829. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  830. struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
  831. __be32 *rdma_argp = rctxt->rc_recv_buf;
  832. struct svc_rdma_send_ctxt *sctxt;
  833. unsigned int rc_size;
  834. __be32 *p;
  835. int ret;
  836. ret = -ENOTCONN;
  837. if (svc_xprt_is_dead(xprt))
  838. goto drop_connection;
  839. ret = -ENOMEM;
  840. sctxt = svc_rdma_send_ctxt_get(rdma);
  841. if (!sctxt)
  842. goto drop_connection;
  843. ret = -EMSGSIZE;
  844. p = xdr_reserve_space(&sctxt->sc_stream,
  845. rpcrdma_fixed_maxsz * sizeof(*p));
  846. if (!p)
  847. goto put_ctxt;
  848. ret = svc_rdma_send_reply_chunk(rdma, rctxt, &rqstp->rq_res);
  849. if (ret < 0)
  850. goto reply_chunk;
  851. rc_size = ret;
  852. *p++ = *rdma_argp;
  853. *p++ = *(rdma_argp + 1);
  854. *p++ = rdma->sc_fc_credits;
  855. *p = pcl_is_empty(&rctxt->rc_reply_pcl) ? rdma_msg : rdma_nomsg;
  856. ret = svc_rdma_encode_read_list(sctxt);
  857. if (ret < 0)
  858. goto put_ctxt;
  859. ret = svc_rdma_encode_write_list(rctxt, sctxt);
  860. if (ret < 0)
  861. goto put_ctxt;
  862. ret = svc_rdma_encode_reply_chunk(rctxt, sctxt, rc_size);
  863. if (ret < 0)
  864. goto put_ctxt;
  865. ret = svc_rdma_send_reply_msg(rdma, sctxt, rctxt, rqstp);
  866. if (ret < 0)
  867. goto put_ctxt;
  868. /* Prevent svc_xprt_release() from releasing the page backing
  869. * rq_res.head[0].iov_base. It's no longer being accessed by
  870. * the I/O device. */
  871. rqstp->rq_respages++;
  872. return 0;
  873. reply_chunk:
  874. if (ret != -E2BIG && ret != -EINVAL)
  875. goto put_ctxt;
  876. svc_rdma_send_error_msg(rdma, sctxt, rctxt, ret);
  877. return 0;
  878. put_ctxt:
  879. svc_rdma_send_ctxt_put(rdma, sctxt);
  880. drop_connection:
  881. trace_svcrdma_send_err(rqstp, ret);
  882. svc_xprt_deferred_close(&rdma->sc_xprt);
  883. return -ENOTCONN;
  884. }
  885. /**
  886. * svc_rdma_result_payload - special processing for a result payload
  887. * @rqstp: svc_rqst to operate on
  888. * @offset: payload's byte offset in @xdr
  889. * @length: size of payload, in bytes
  890. *
  891. * Return values:
  892. * %0 if successful or nothing needed to be done
  893. * %-EMSGSIZE on XDR buffer overflow
  894. * %-E2BIG if the payload was larger than the Write chunk
  895. * %-EINVAL if client provided too many segments
  896. * %-ENOMEM if rdma_rw context pool was exhausted
  897. * %-ENOTCONN if posting failed (connection is lost)
  898. * %-EIO if rdma_rw initialization failed (DMA mapping, etc)
  899. */
  900. int svc_rdma_result_payload(struct svc_rqst *rqstp, unsigned int offset,
  901. unsigned int length)
  902. {
  903. struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
  904. struct svc_rdma_chunk *chunk;
  905. struct svcxprt_rdma *rdma;
  906. struct xdr_buf subbuf;
  907. int ret;
  908. chunk = rctxt->rc_cur_result_payload;
  909. if (!length || !chunk)
  910. return 0;
  911. rctxt->rc_cur_result_payload =
  912. pcl_next_chunk(&rctxt->rc_write_pcl, chunk);
  913. if (length > chunk->ch_length)
  914. return -E2BIG;
  915. chunk->ch_position = offset;
  916. chunk->ch_payload_length = length;
  917. if (xdr_buf_subsegment(&rqstp->rq_res, &subbuf, offset, length))
  918. return -EMSGSIZE;
  919. rdma = container_of(rqstp->rq_xprt, struct svcxprt_rdma, sc_xprt);
  920. ret = svc_rdma_send_write_chunk(rdma, chunk, &subbuf);
  921. if (ret < 0)
  922. return ret;
  923. return 0;
  924. }