svc_rdma_recvfrom.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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_recvfrom. This is called from
  47. * svc_recv when the transport indicates there is incoming data to
  48. * be read. "Data Ready" is signaled when an RDMA Receive completes,
  49. * or when a set of RDMA Reads complete.
  50. *
  51. * An svc_rqst is passed in. This structure contains an array of
  52. * free pages (rq_pages) that will contain the incoming RPC message.
  53. *
  54. * Short messages are moved directly into svc_rqst::rq_arg, and
  55. * the RPC Call is ready to be processed by the Upper Layer.
  56. * svc_rdma_recvfrom returns the length of the RPC Call message,
  57. * completing the reception of the RPC Call.
  58. *
  59. * However, when an incoming message has Read chunks,
  60. * svc_rdma_recvfrom must post RDMA Reads to pull the RPC Call's
  61. * data payload from the client. svc_rdma_recvfrom sets up the
  62. * RDMA Reads using pages in svc_rqst::rq_pages, which are
  63. * transferred to an svc_rdma_recv_ctxt for the duration of the
  64. * I/O. svc_rdma_recvfrom then returns zero, since the RPC message
  65. * is still not yet ready.
  66. *
  67. * When the Read chunk payloads have become available on the
  68. * server, "Data Ready" is raised again, and svc_recv calls
  69. * svc_rdma_recvfrom again. This second call may use a different
  70. * svc_rqst than the first one, thus any information that needs
  71. * to be preserved across these two calls is kept in an
  72. * svc_rdma_recv_ctxt.
  73. *
  74. * The second call to svc_rdma_recvfrom performs final assembly
  75. * of the RPC Call message, using the RDMA Read sink pages kept in
  76. * the svc_rdma_recv_ctxt. The xdr_buf is copied from the
  77. * svc_rdma_recv_ctxt to the second svc_rqst. The second call returns
  78. * the length of the completed RPC Call message.
  79. *
  80. * Page Management
  81. *
  82. * Pages under I/O must be transferred from the first svc_rqst to an
  83. * svc_rdma_recv_ctxt before the first svc_rdma_recvfrom call returns.
  84. *
  85. * The first svc_rqst supplies pages for RDMA Reads. These are moved
  86. * from rqstp::rq_pages into ctxt::pages. The consumed elements of
  87. * the rq_pages array are set to NULL and refilled with the first
  88. * svc_rdma_recvfrom call returns.
  89. *
  90. * During the second svc_rdma_recvfrom call, RDMA Read sink pages
  91. * are transferred from the svc_rdma_recv_ctxt to the second svc_rqst
  92. * (see rdma_read_complete() below).
  93. */
  94. #include <linux/spinlock.h>
  95. #include <asm/unaligned.h>
  96. #include <rdma/ib_verbs.h>
  97. #include <rdma/rdma_cm.h>
  98. #include <linux/sunrpc/xdr.h>
  99. #include <linux/sunrpc/debug.h>
  100. #include <linux/sunrpc/rpc_rdma.h>
  101. #include <linux/sunrpc/svc_rdma.h>
  102. #include "xprt_rdma.h"
  103. #include <trace/events/rpcrdma.h>
  104. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  105. static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc);
  106. static inline struct svc_rdma_recv_ctxt *
  107. svc_rdma_next_recv_ctxt(struct list_head *list)
  108. {
  109. return list_first_entry_or_null(list, struct svc_rdma_recv_ctxt,
  110. rc_list);
  111. }
  112. static struct svc_rdma_recv_ctxt *
  113. svc_rdma_recv_ctxt_alloc(struct svcxprt_rdma *rdma)
  114. {
  115. struct svc_rdma_recv_ctxt *ctxt;
  116. dma_addr_t addr;
  117. void *buffer;
  118. ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
  119. if (!ctxt)
  120. goto fail0;
  121. buffer = kmalloc(rdma->sc_max_req_size, GFP_KERNEL);
  122. if (!buffer)
  123. goto fail1;
  124. addr = ib_dma_map_single(rdma->sc_pd->device, buffer,
  125. rdma->sc_max_req_size, DMA_FROM_DEVICE);
  126. if (ib_dma_mapping_error(rdma->sc_pd->device, addr))
  127. goto fail2;
  128. ctxt->rc_recv_wr.next = NULL;
  129. ctxt->rc_recv_wr.wr_cqe = &ctxt->rc_cqe;
  130. ctxt->rc_recv_wr.sg_list = &ctxt->rc_recv_sge;
  131. ctxt->rc_recv_wr.num_sge = 1;
  132. ctxt->rc_cqe.done = svc_rdma_wc_receive;
  133. ctxt->rc_recv_sge.addr = addr;
  134. ctxt->rc_recv_sge.length = rdma->sc_max_req_size;
  135. ctxt->rc_recv_sge.lkey = rdma->sc_pd->local_dma_lkey;
  136. ctxt->rc_recv_buf = buffer;
  137. ctxt->rc_temp = false;
  138. return ctxt;
  139. fail2:
  140. kfree(buffer);
  141. fail1:
  142. kfree(ctxt);
  143. fail0:
  144. return NULL;
  145. }
  146. static void svc_rdma_recv_ctxt_destroy(struct svcxprt_rdma *rdma,
  147. struct svc_rdma_recv_ctxt *ctxt)
  148. {
  149. ib_dma_unmap_single(rdma->sc_pd->device, ctxt->rc_recv_sge.addr,
  150. ctxt->rc_recv_sge.length, DMA_FROM_DEVICE);
  151. kfree(ctxt->rc_recv_buf);
  152. kfree(ctxt);
  153. }
  154. /**
  155. * svc_rdma_recv_ctxts_destroy - Release all recv_ctxt's for an xprt
  156. * @rdma: svcxprt_rdma being torn down
  157. *
  158. */
  159. void svc_rdma_recv_ctxts_destroy(struct svcxprt_rdma *rdma)
  160. {
  161. struct svc_rdma_recv_ctxt *ctxt;
  162. while ((ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_recv_ctxts))) {
  163. list_del(&ctxt->rc_list);
  164. svc_rdma_recv_ctxt_destroy(rdma, ctxt);
  165. }
  166. }
  167. static struct svc_rdma_recv_ctxt *
  168. svc_rdma_recv_ctxt_get(struct svcxprt_rdma *rdma)
  169. {
  170. struct svc_rdma_recv_ctxt *ctxt;
  171. spin_lock(&rdma->sc_recv_lock);
  172. ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_recv_ctxts);
  173. if (!ctxt)
  174. goto out_empty;
  175. list_del(&ctxt->rc_list);
  176. spin_unlock(&rdma->sc_recv_lock);
  177. out:
  178. ctxt->rc_page_count = 0;
  179. return ctxt;
  180. out_empty:
  181. spin_unlock(&rdma->sc_recv_lock);
  182. ctxt = svc_rdma_recv_ctxt_alloc(rdma);
  183. if (!ctxt)
  184. return NULL;
  185. goto out;
  186. }
  187. /**
  188. * svc_rdma_recv_ctxt_put - Return recv_ctxt to free list
  189. * @rdma: controlling svcxprt_rdma
  190. * @ctxt: object to return to the free list
  191. *
  192. */
  193. void svc_rdma_recv_ctxt_put(struct svcxprt_rdma *rdma,
  194. struct svc_rdma_recv_ctxt *ctxt)
  195. {
  196. unsigned int i;
  197. for (i = 0; i < ctxt->rc_page_count; i++)
  198. put_page(ctxt->rc_pages[i]);
  199. if (!ctxt->rc_temp) {
  200. spin_lock(&rdma->sc_recv_lock);
  201. list_add(&ctxt->rc_list, &rdma->sc_recv_ctxts);
  202. spin_unlock(&rdma->sc_recv_lock);
  203. } else
  204. svc_rdma_recv_ctxt_destroy(rdma, ctxt);
  205. }
  206. static int __svc_rdma_post_recv(struct svcxprt_rdma *rdma,
  207. struct svc_rdma_recv_ctxt *ctxt)
  208. {
  209. int ret;
  210. svc_xprt_get(&rdma->sc_xprt);
  211. ret = ib_post_recv(rdma->sc_qp, &ctxt->rc_recv_wr, NULL);
  212. trace_svcrdma_post_recv(&ctxt->rc_recv_wr, ret);
  213. if (ret)
  214. goto err_post;
  215. return 0;
  216. err_post:
  217. svc_rdma_recv_ctxt_put(rdma, ctxt);
  218. svc_xprt_put(&rdma->sc_xprt);
  219. return ret;
  220. }
  221. static int svc_rdma_post_recv(struct svcxprt_rdma *rdma)
  222. {
  223. struct svc_rdma_recv_ctxt *ctxt;
  224. ctxt = svc_rdma_recv_ctxt_get(rdma);
  225. if (!ctxt)
  226. return -ENOMEM;
  227. return __svc_rdma_post_recv(rdma, ctxt);
  228. }
  229. /**
  230. * svc_rdma_post_recvs - Post initial set of Recv WRs
  231. * @rdma: fresh svcxprt_rdma
  232. *
  233. * Returns true if successful, otherwise false.
  234. */
  235. bool svc_rdma_post_recvs(struct svcxprt_rdma *rdma)
  236. {
  237. struct svc_rdma_recv_ctxt *ctxt;
  238. unsigned int i;
  239. int ret;
  240. for (i = 0; i < rdma->sc_max_requests; i++) {
  241. ctxt = svc_rdma_recv_ctxt_get(rdma);
  242. if (!ctxt)
  243. return false;
  244. ctxt->rc_temp = true;
  245. ret = __svc_rdma_post_recv(rdma, ctxt);
  246. if (ret) {
  247. pr_err("svcrdma: failure posting recv buffers: %d\n",
  248. ret);
  249. return false;
  250. }
  251. }
  252. return true;
  253. }
  254. /**
  255. * svc_rdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
  256. * @cq: Completion Queue context
  257. * @wc: Work Completion object
  258. *
  259. * NB: The svc_xprt/svcxprt_rdma is pinned whenever it's possible that
  260. * the Receive completion handler could be running.
  261. */
  262. static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
  263. {
  264. struct svcxprt_rdma *rdma = cq->cq_context;
  265. struct ib_cqe *cqe = wc->wr_cqe;
  266. struct svc_rdma_recv_ctxt *ctxt;
  267. trace_svcrdma_wc_receive(wc);
  268. /* WARNING: Only wc->wr_cqe and wc->status are reliable */
  269. ctxt = container_of(cqe, struct svc_rdma_recv_ctxt, rc_cqe);
  270. if (wc->status != IB_WC_SUCCESS)
  271. goto flushed;
  272. if (svc_rdma_post_recv(rdma))
  273. goto post_err;
  274. /* All wc fields are now known to be valid */
  275. ctxt->rc_byte_len = wc->byte_len;
  276. ib_dma_sync_single_for_cpu(rdma->sc_pd->device,
  277. ctxt->rc_recv_sge.addr,
  278. wc->byte_len, DMA_FROM_DEVICE);
  279. spin_lock(&rdma->sc_rq_dto_lock);
  280. list_add_tail(&ctxt->rc_list, &rdma->sc_rq_dto_q);
  281. spin_unlock(&rdma->sc_rq_dto_lock);
  282. set_bit(XPT_DATA, &rdma->sc_xprt.xpt_flags);
  283. if (!test_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags))
  284. svc_xprt_enqueue(&rdma->sc_xprt);
  285. goto out;
  286. flushed:
  287. if (wc->status != IB_WC_WR_FLUSH_ERR)
  288. pr_err("svcrdma: Recv: %s (%u/0x%x)\n",
  289. ib_wc_status_msg(wc->status),
  290. wc->status, wc->vendor_err);
  291. post_err:
  292. svc_rdma_recv_ctxt_put(rdma, ctxt);
  293. set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
  294. svc_xprt_enqueue(&rdma->sc_xprt);
  295. out:
  296. svc_xprt_put(&rdma->sc_xprt);
  297. }
  298. /**
  299. * svc_rdma_flush_recv_queues - Drain pending Receive work
  300. * @rdma: svcxprt_rdma being shut down
  301. *
  302. */
  303. void svc_rdma_flush_recv_queues(struct svcxprt_rdma *rdma)
  304. {
  305. struct svc_rdma_recv_ctxt *ctxt;
  306. while ((ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_read_complete_q))) {
  307. list_del(&ctxt->rc_list);
  308. svc_rdma_recv_ctxt_put(rdma, ctxt);
  309. }
  310. while ((ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_rq_dto_q))) {
  311. list_del(&ctxt->rc_list);
  312. svc_rdma_recv_ctxt_put(rdma, ctxt);
  313. }
  314. }
  315. static void svc_rdma_build_arg_xdr(struct svc_rqst *rqstp,
  316. struct svc_rdma_recv_ctxt *ctxt)
  317. {
  318. struct xdr_buf *arg = &rqstp->rq_arg;
  319. arg->head[0].iov_base = ctxt->rc_recv_buf;
  320. arg->head[0].iov_len = ctxt->rc_byte_len;
  321. arg->tail[0].iov_base = NULL;
  322. arg->tail[0].iov_len = 0;
  323. arg->page_len = 0;
  324. arg->page_base = 0;
  325. arg->buflen = ctxt->rc_byte_len;
  326. arg->len = ctxt->rc_byte_len;
  327. }
  328. /* This accommodates the largest possible Write chunk,
  329. * in one segment.
  330. */
  331. #define MAX_BYTES_WRITE_SEG ((u32)(RPCSVC_MAXPAGES << PAGE_SHIFT))
  332. /* This accommodates the largest possible Position-Zero
  333. * Read chunk or Reply chunk, in one segment.
  334. */
  335. #define MAX_BYTES_SPECIAL_SEG ((u32)((RPCSVC_MAXPAGES + 2) << PAGE_SHIFT))
  336. /* Sanity check the Read list.
  337. *
  338. * Implementation limits:
  339. * - This implementation supports only one Read chunk.
  340. *
  341. * Sanity checks:
  342. * - Read list does not overflow buffer.
  343. * - Segment size limited by largest NFS data payload.
  344. *
  345. * The segment count is limited to how many segments can
  346. * fit in the transport header without overflowing the
  347. * buffer. That's about 40 Read segments for a 1KB inline
  348. * threshold.
  349. *
  350. * Returns pointer to the following Write list.
  351. */
  352. static __be32 *xdr_check_read_list(__be32 *p, const __be32 *end)
  353. {
  354. u32 position;
  355. bool first;
  356. first = true;
  357. while (*p++ != xdr_zero) {
  358. if (first) {
  359. position = be32_to_cpup(p++);
  360. first = false;
  361. } else if (be32_to_cpup(p++) != position) {
  362. return NULL;
  363. }
  364. p++; /* handle */
  365. if (be32_to_cpup(p++) > MAX_BYTES_SPECIAL_SEG)
  366. return NULL;
  367. p += 2; /* offset */
  368. if (p > end)
  369. return NULL;
  370. }
  371. return p;
  372. }
  373. /* The segment count is limited to how many segments can
  374. * fit in the transport header without overflowing the
  375. * buffer. That's about 60 Write segments for a 1KB inline
  376. * threshold.
  377. */
  378. static __be32 *xdr_check_write_chunk(__be32 *p, const __be32 *end,
  379. u32 maxlen)
  380. {
  381. u32 i, segcount;
  382. segcount = be32_to_cpup(p++);
  383. for (i = 0; i < segcount; i++) {
  384. p++; /* handle */
  385. if (be32_to_cpup(p++) > maxlen)
  386. return NULL;
  387. p += 2; /* offset */
  388. if (p > end)
  389. return NULL;
  390. }
  391. return p;
  392. }
  393. /* Sanity check the Write list.
  394. *
  395. * Implementation limits:
  396. * - This implementation supports only one Write chunk.
  397. *
  398. * Sanity checks:
  399. * - Write list does not overflow buffer.
  400. * - Segment size limited by largest NFS data payload.
  401. *
  402. * Returns pointer to the following Reply chunk.
  403. */
  404. static __be32 *xdr_check_write_list(__be32 *p, const __be32 *end)
  405. {
  406. u32 chcount;
  407. chcount = 0;
  408. while (*p++ != xdr_zero) {
  409. p = xdr_check_write_chunk(p, end, MAX_BYTES_WRITE_SEG);
  410. if (!p)
  411. return NULL;
  412. if (chcount++ > 1)
  413. return NULL;
  414. }
  415. return p;
  416. }
  417. /* Sanity check the Reply chunk.
  418. *
  419. * Sanity checks:
  420. * - Reply chunk does not overflow buffer.
  421. * - Segment size limited by largest NFS data payload.
  422. *
  423. * Returns pointer to the following RPC header.
  424. */
  425. static __be32 *xdr_check_reply_chunk(__be32 *p, const __be32 *end)
  426. {
  427. if (*p++ != xdr_zero) {
  428. p = xdr_check_write_chunk(p, end, MAX_BYTES_SPECIAL_SEG);
  429. if (!p)
  430. return NULL;
  431. }
  432. return p;
  433. }
  434. /* On entry, xdr->head[0].iov_base points to first byte in the
  435. * RPC-over-RDMA header.
  436. *
  437. * On successful exit, head[0] points to first byte past the
  438. * RPC-over-RDMA header. For RDMA_MSG, this is the RPC message.
  439. * The length of the RPC-over-RDMA header is returned.
  440. *
  441. * Assumptions:
  442. * - The transport header is entirely contained in the head iovec.
  443. */
  444. static int svc_rdma_xdr_decode_req(struct xdr_buf *rq_arg)
  445. {
  446. __be32 *p, *end, *rdma_argp;
  447. unsigned int hdr_len;
  448. /* Verify that there's enough bytes for header + something */
  449. if (rq_arg->len <= RPCRDMA_HDRLEN_ERR)
  450. goto out_short;
  451. rdma_argp = rq_arg->head[0].iov_base;
  452. if (*(rdma_argp + 1) != rpcrdma_version)
  453. goto out_version;
  454. switch (*(rdma_argp + 3)) {
  455. case rdma_msg:
  456. break;
  457. case rdma_nomsg:
  458. break;
  459. case rdma_done:
  460. goto out_drop;
  461. case rdma_error:
  462. goto out_drop;
  463. default:
  464. goto out_proc;
  465. }
  466. end = (__be32 *)((unsigned long)rdma_argp + rq_arg->len);
  467. p = xdr_check_read_list(rdma_argp + 4, end);
  468. if (!p)
  469. goto out_inval;
  470. p = xdr_check_write_list(p, end);
  471. if (!p)
  472. goto out_inval;
  473. p = xdr_check_reply_chunk(p, end);
  474. if (!p)
  475. goto out_inval;
  476. if (p > end)
  477. goto out_inval;
  478. rq_arg->head[0].iov_base = p;
  479. hdr_len = (unsigned long)p - (unsigned long)rdma_argp;
  480. rq_arg->head[0].iov_len -= hdr_len;
  481. rq_arg->len -= hdr_len;
  482. trace_svcrdma_decode_rqst(rdma_argp, hdr_len);
  483. return hdr_len;
  484. out_short:
  485. trace_svcrdma_decode_short(rq_arg->len);
  486. return -EINVAL;
  487. out_version:
  488. trace_svcrdma_decode_badvers(rdma_argp);
  489. return -EPROTONOSUPPORT;
  490. out_drop:
  491. trace_svcrdma_decode_drop(rdma_argp);
  492. return 0;
  493. out_proc:
  494. trace_svcrdma_decode_badproc(rdma_argp);
  495. return -EINVAL;
  496. out_inval:
  497. trace_svcrdma_decode_parse(rdma_argp);
  498. return -EINVAL;
  499. }
  500. static void rdma_read_complete(struct svc_rqst *rqstp,
  501. struct svc_rdma_recv_ctxt *head)
  502. {
  503. int page_no;
  504. /* Move Read chunk pages to rqstp so that they will be released
  505. * when svc_process is done with them.
  506. */
  507. for (page_no = 0; page_no < head->rc_page_count; page_no++) {
  508. put_page(rqstp->rq_pages[page_no]);
  509. rqstp->rq_pages[page_no] = head->rc_pages[page_no];
  510. }
  511. head->rc_page_count = 0;
  512. /* Point rq_arg.pages past header */
  513. rqstp->rq_arg.pages = &rqstp->rq_pages[head->rc_hdr_count];
  514. rqstp->rq_arg.page_len = head->rc_arg.page_len;
  515. /* rq_respages starts after the last arg page */
  516. rqstp->rq_respages = &rqstp->rq_pages[page_no];
  517. rqstp->rq_next_page = rqstp->rq_respages + 1;
  518. /* Rebuild rq_arg head and tail. */
  519. rqstp->rq_arg.head[0] = head->rc_arg.head[0];
  520. rqstp->rq_arg.tail[0] = head->rc_arg.tail[0];
  521. rqstp->rq_arg.len = head->rc_arg.len;
  522. rqstp->rq_arg.buflen = head->rc_arg.buflen;
  523. }
  524. static void svc_rdma_send_error(struct svcxprt_rdma *xprt,
  525. __be32 *rdma_argp, int status)
  526. {
  527. struct svc_rdma_send_ctxt *ctxt;
  528. unsigned int length;
  529. __be32 *p;
  530. int ret;
  531. ctxt = svc_rdma_send_ctxt_get(xprt);
  532. if (!ctxt)
  533. return;
  534. p = ctxt->sc_xprt_buf;
  535. *p++ = *rdma_argp;
  536. *p++ = *(rdma_argp + 1);
  537. *p++ = xprt->sc_fc_credits;
  538. *p++ = rdma_error;
  539. switch (status) {
  540. case -EPROTONOSUPPORT:
  541. *p++ = err_vers;
  542. *p++ = rpcrdma_version;
  543. *p++ = rpcrdma_version;
  544. trace_svcrdma_err_vers(*rdma_argp);
  545. break;
  546. default:
  547. *p++ = err_chunk;
  548. trace_svcrdma_err_chunk(*rdma_argp);
  549. }
  550. length = (unsigned long)p - (unsigned long)ctxt->sc_xprt_buf;
  551. svc_rdma_sync_reply_hdr(xprt, ctxt, length);
  552. ctxt->sc_send_wr.opcode = IB_WR_SEND;
  553. ret = svc_rdma_send(xprt, &ctxt->sc_send_wr);
  554. if (ret)
  555. svc_rdma_send_ctxt_put(xprt, ctxt);
  556. }
  557. /* By convention, backchannel calls arrive via rdma_msg type
  558. * messages, and never populate the chunk lists. This makes
  559. * the RPC/RDMA header small and fixed in size, so it is
  560. * straightforward to check the RPC header's direction field.
  561. */
  562. static bool svc_rdma_is_backchannel_reply(struct svc_xprt *xprt,
  563. __be32 *rdma_resp)
  564. {
  565. __be32 *p;
  566. if (!xprt->xpt_bc_xprt)
  567. return false;
  568. p = rdma_resp + 3;
  569. if (*p++ != rdma_msg)
  570. return false;
  571. if (*p++ != xdr_zero)
  572. return false;
  573. if (*p++ != xdr_zero)
  574. return false;
  575. if (*p++ != xdr_zero)
  576. return false;
  577. /* XID sanity */
  578. if (*p++ != *rdma_resp)
  579. return false;
  580. /* call direction */
  581. if (*p == cpu_to_be32(RPC_CALL))
  582. return false;
  583. return true;
  584. }
  585. /**
  586. * svc_rdma_recvfrom - Receive an RPC call
  587. * @rqstp: request structure into which to receive an RPC Call
  588. *
  589. * Returns:
  590. * The positive number of bytes in the RPC Call message,
  591. * %0 if there were no Calls ready to return,
  592. * %-EINVAL if the Read chunk data is too large,
  593. * %-ENOMEM if rdma_rw context pool was exhausted,
  594. * %-ENOTCONN if posting failed (connection is lost),
  595. * %-EIO if rdma_rw initialization failed (DMA mapping, etc).
  596. *
  597. * Called in a loop when XPT_DATA is set. XPT_DATA is cleared only
  598. * when there are no remaining ctxt's to process.
  599. *
  600. * The next ctxt is removed from the "receive" lists.
  601. *
  602. * - If the ctxt completes a Read, then finish assembling the Call
  603. * message and return the number of bytes in the message.
  604. *
  605. * - If the ctxt completes a Receive, then construct the Call
  606. * message from the contents of the Receive buffer.
  607. *
  608. * - If there are no Read chunks in this message, then finish
  609. * assembling the Call message and return the number of bytes
  610. * in the message.
  611. *
  612. * - If there are Read chunks in this message, post Read WRs to
  613. * pull that payload and return 0.
  614. */
  615. int svc_rdma_recvfrom(struct svc_rqst *rqstp)
  616. {
  617. struct svc_xprt *xprt = rqstp->rq_xprt;
  618. struct svcxprt_rdma *rdma_xprt =
  619. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  620. struct svc_rdma_recv_ctxt *ctxt;
  621. __be32 *p;
  622. int ret;
  623. spin_lock(&rdma_xprt->sc_rq_dto_lock);
  624. ctxt = svc_rdma_next_recv_ctxt(&rdma_xprt->sc_read_complete_q);
  625. if (ctxt) {
  626. list_del(&ctxt->rc_list);
  627. spin_unlock(&rdma_xprt->sc_rq_dto_lock);
  628. rdma_read_complete(rqstp, ctxt);
  629. goto complete;
  630. }
  631. ctxt = svc_rdma_next_recv_ctxt(&rdma_xprt->sc_rq_dto_q);
  632. if (!ctxt) {
  633. /* No new incoming requests, terminate the loop */
  634. clear_bit(XPT_DATA, &xprt->xpt_flags);
  635. spin_unlock(&rdma_xprt->sc_rq_dto_lock);
  636. return 0;
  637. }
  638. list_del(&ctxt->rc_list);
  639. spin_unlock(&rdma_xprt->sc_rq_dto_lock);
  640. atomic_inc(&rdma_stat_recv);
  641. svc_rdma_build_arg_xdr(rqstp, ctxt);
  642. /* Prevent svc_xprt_release from releasing pages in rq_pages
  643. * if we return 0 or an error.
  644. */
  645. rqstp->rq_respages = rqstp->rq_pages;
  646. rqstp->rq_next_page = rqstp->rq_respages;
  647. p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
  648. ret = svc_rdma_xdr_decode_req(&rqstp->rq_arg);
  649. if (ret < 0)
  650. goto out_err;
  651. if (ret == 0)
  652. goto out_drop;
  653. rqstp->rq_xprt_hlen = ret;
  654. if (svc_rdma_is_backchannel_reply(xprt, p)) {
  655. ret = svc_rdma_handle_bc_reply(xprt->xpt_bc_xprt, p,
  656. &rqstp->rq_arg);
  657. svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
  658. return ret;
  659. }
  660. p += rpcrdma_fixed_maxsz;
  661. if (*p != xdr_zero)
  662. goto out_readchunk;
  663. complete:
  664. rqstp->rq_xprt_ctxt = ctxt;
  665. rqstp->rq_prot = IPPROTO_MAX;
  666. svc_xprt_copy_addrs(rqstp, xprt);
  667. return rqstp->rq_arg.len;
  668. out_readchunk:
  669. ret = svc_rdma_recv_read_chunk(rdma_xprt, rqstp, ctxt, p);
  670. if (ret < 0)
  671. goto out_postfail;
  672. return 0;
  673. out_err:
  674. svc_rdma_send_error(rdma_xprt, p, ret);
  675. svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
  676. return 0;
  677. out_postfail:
  678. if (ret == -EINVAL)
  679. svc_rdma_send_error(rdma_xprt, p, ret);
  680. svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
  681. return ret;
  682. out_drop:
  683. svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
  684. return 0;
  685. }