svc_rdma_recvfrom.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the BSD-type
  8. * license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials provided
  20. * with the distribution.
  21. *
  22. * Neither the name of the Network Appliance, Inc. nor the names of
  23. * its contributors may be used to endorse or promote products
  24. * derived from this software without specific prior written
  25. * permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * Author: Tom Tucker <tom@opengridcomputing.com>
  40. */
  41. #include <linux/sunrpc/debug.h>
  42. #include <linux/sunrpc/rpc_rdma.h>
  43. #include <linux/spinlock.h>
  44. #include <asm/unaligned.h>
  45. #include <rdma/ib_verbs.h>
  46. #include <rdma/rdma_cm.h>
  47. #include <linux/sunrpc/svc_rdma.h>
  48. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  49. /*
  50. * Replace the pages in the rq_argpages array with the pages from the SGE in
  51. * the RDMA_RECV completion. The SGL should contain full pages up until the
  52. * last one.
  53. */
  54. static void rdma_build_arg_xdr(struct svc_rqst *rqstp,
  55. struct svc_rdma_op_ctxt *ctxt,
  56. u32 byte_count)
  57. {
  58. struct page *page;
  59. u32 bc;
  60. int sge_no;
  61. /* Swap the page in the SGE with the page in argpages */
  62. page = ctxt->pages[0];
  63. put_page(rqstp->rq_pages[0]);
  64. rqstp->rq_pages[0] = page;
  65. /* Set up the XDR head */
  66. rqstp->rq_arg.head[0].iov_base = page_address(page);
  67. rqstp->rq_arg.head[0].iov_len = min(byte_count, ctxt->sge[0].length);
  68. rqstp->rq_arg.len = byte_count;
  69. rqstp->rq_arg.buflen = byte_count;
  70. /* Compute bytes past head in the SGL */
  71. bc = byte_count - rqstp->rq_arg.head[0].iov_len;
  72. /* If data remains, store it in the pagelist */
  73. rqstp->rq_arg.page_len = bc;
  74. rqstp->rq_arg.page_base = 0;
  75. rqstp->rq_arg.pages = &rqstp->rq_pages[1];
  76. sge_no = 1;
  77. while (bc && sge_no < ctxt->count) {
  78. page = ctxt->pages[sge_no];
  79. put_page(rqstp->rq_pages[sge_no]);
  80. rqstp->rq_pages[sge_no] = page;
  81. bc -= min(bc, ctxt->sge[sge_no].length);
  82. rqstp->rq_arg.buflen += ctxt->sge[sge_no].length;
  83. sge_no++;
  84. }
  85. rqstp->rq_respages = &rqstp->rq_pages[sge_no];
  86. /* We should never run out of SGE because the limit is defined to
  87. * support the max allowed RPC data length
  88. */
  89. BUG_ON(bc && (sge_no == ctxt->count));
  90. BUG_ON((rqstp->rq_arg.head[0].iov_len + rqstp->rq_arg.page_len)
  91. != byte_count);
  92. BUG_ON(rqstp->rq_arg.len != byte_count);
  93. /* If not all pages were used from the SGL, free the remaining ones */
  94. bc = sge_no;
  95. while (sge_no < ctxt->count) {
  96. page = ctxt->pages[sge_no++];
  97. put_page(page);
  98. }
  99. ctxt->count = bc;
  100. /* Set up tail */
  101. rqstp->rq_arg.tail[0].iov_base = NULL;
  102. rqstp->rq_arg.tail[0].iov_len = 0;
  103. }
  104. /* Encode a read-chunk-list as an array of IB SGE
  105. *
  106. * Assumptions:
  107. * - chunk[0]->position points to pages[0] at an offset of 0
  108. * - pages[] is not physically or virtually contiguous and consists of
  109. * PAGE_SIZE elements.
  110. *
  111. * Output:
  112. * - sge array pointing into pages[] array.
  113. * - chunk_sge array specifying sge index and count for each
  114. * chunk in the read list
  115. *
  116. */
  117. static int map_read_chunks(struct svcxprt_rdma *xprt,
  118. struct svc_rqst *rqstp,
  119. struct svc_rdma_op_ctxt *head,
  120. struct rpcrdma_msg *rmsgp,
  121. struct svc_rdma_req_map *rpl_map,
  122. struct svc_rdma_req_map *chl_map,
  123. int ch_count,
  124. int byte_count)
  125. {
  126. int sge_no;
  127. int sge_bytes;
  128. int page_off;
  129. int page_no;
  130. int ch_bytes;
  131. int ch_no;
  132. struct rpcrdma_read_chunk *ch;
  133. sge_no = 0;
  134. page_no = 0;
  135. page_off = 0;
  136. ch = (struct rpcrdma_read_chunk *)&rmsgp->rm_body.rm_chunks[0];
  137. ch_no = 0;
  138. ch_bytes = ch->rc_target.rs_length;
  139. head->arg.head[0] = rqstp->rq_arg.head[0];
  140. head->arg.tail[0] = rqstp->rq_arg.tail[0];
  141. head->arg.pages = &head->pages[head->count];
  142. head->hdr_count = head->count; /* save count of hdr pages */
  143. head->arg.page_base = 0;
  144. head->arg.page_len = ch_bytes;
  145. head->arg.len = rqstp->rq_arg.len + ch_bytes;
  146. head->arg.buflen = rqstp->rq_arg.buflen + ch_bytes;
  147. head->count++;
  148. chl_map->ch[0].start = 0;
  149. while (byte_count) {
  150. rpl_map->sge[sge_no].iov_base =
  151. page_address(rqstp->rq_arg.pages[page_no]) + page_off;
  152. sge_bytes = min_t(int, PAGE_SIZE-page_off, ch_bytes);
  153. rpl_map->sge[sge_no].iov_len = sge_bytes;
  154. /*
  155. * Don't bump head->count here because the same page
  156. * may be used by multiple SGE.
  157. */
  158. head->arg.pages[page_no] = rqstp->rq_arg.pages[page_no];
  159. rqstp->rq_respages = &rqstp->rq_arg.pages[page_no+1];
  160. byte_count -= sge_bytes;
  161. ch_bytes -= sge_bytes;
  162. sge_no++;
  163. /*
  164. * If all bytes for this chunk have been mapped to an
  165. * SGE, move to the next SGE
  166. */
  167. if (ch_bytes == 0) {
  168. chl_map->ch[ch_no].count =
  169. sge_no - chl_map->ch[ch_no].start;
  170. ch_no++;
  171. ch++;
  172. chl_map->ch[ch_no].start = sge_no;
  173. ch_bytes = ch->rc_target.rs_length;
  174. /* If bytes remaining account for next chunk */
  175. if (byte_count) {
  176. head->arg.page_len += ch_bytes;
  177. head->arg.len += ch_bytes;
  178. head->arg.buflen += ch_bytes;
  179. }
  180. }
  181. /*
  182. * If this SGE consumed all of the page, move to the
  183. * next page
  184. */
  185. if ((sge_bytes + page_off) == PAGE_SIZE) {
  186. page_no++;
  187. page_off = 0;
  188. /*
  189. * If there are still bytes left to map, bump
  190. * the page count
  191. */
  192. if (byte_count)
  193. head->count++;
  194. } else
  195. page_off += sge_bytes;
  196. }
  197. BUG_ON(byte_count != 0);
  198. return sge_no;
  199. }
  200. /* Map a read-chunk-list to an XDR and fast register the page-list.
  201. *
  202. * Assumptions:
  203. * - chunk[0] position points to pages[0] at an offset of 0
  204. * - pages[] will be made physically contiguous by creating a one-off memory
  205. * region using the fastreg verb.
  206. * - byte_count is # of bytes in read-chunk-list
  207. * - ch_count is # of chunks in read-chunk-list
  208. *
  209. * Output:
  210. * - sge array pointing into pages[] array.
  211. * - chunk_sge array specifying sge index and count for each
  212. * chunk in the read list
  213. */
  214. static int fast_reg_read_chunks(struct svcxprt_rdma *xprt,
  215. struct svc_rqst *rqstp,
  216. struct svc_rdma_op_ctxt *head,
  217. struct rpcrdma_msg *rmsgp,
  218. struct svc_rdma_req_map *rpl_map,
  219. struct svc_rdma_req_map *chl_map,
  220. int ch_count,
  221. int byte_count)
  222. {
  223. int page_no;
  224. int ch_no;
  225. u32 offset;
  226. struct rpcrdma_read_chunk *ch;
  227. struct svc_rdma_fastreg_mr *frmr;
  228. int ret = 0;
  229. frmr = svc_rdma_get_frmr(xprt);
  230. if (IS_ERR(frmr))
  231. return -ENOMEM;
  232. head->frmr = frmr;
  233. head->arg.head[0] = rqstp->rq_arg.head[0];
  234. head->arg.tail[0] = rqstp->rq_arg.tail[0];
  235. head->arg.pages = &head->pages[head->count];
  236. head->hdr_count = head->count; /* save count of hdr pages */
  237. head->arg.page_base = 0;
  238. head->arg.page_len = byte_count;
  239. head->arg.len = rqstp->rq_arg.len + byte_count;
  240. head->arg.buflen = rqstp->rq_arg.buflen + byte_count;
  241. /* Fast register the page list */
  242. frmr->kva = page_address(rqstp->rq_arg.pages[0]);
  243. frmr->direction = DMA_FROM_DEVICE;
  244. frmr->access_flags = (IB_ACCESS_LOCAL_WRITE|IB_ACCESS_REMOTE_WRITE);
  245. frmr->map_len = byte_count;
  246. frmr->page_list_len = PAGE_ALIGN(byte_count) >> PAGE_SHIFT;
  247. for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
  248. frmr->page_list->page_list[page_no] =
  249. ib_dma_map_single(xprt->sc_cm_id->device,
  250. page_address(rqstp->rq_arg.pages[page_no]),
  251. PAGE_SIZE, DMA_FROM_DEVICE);
  252. if (ib_dma_mapping_error(xprt->sc_cm_id->device,
  253. frmr->page_list->page_list[page_no]))
  254. goto fatal_err;
  255. atomic_inc(&xprt->sc_dma_used);
  256. head->arg.pages[page_no] = rqstp->rq_arg.pages[page_no];
  257. }
  258. head->count += page_no;
  259. /* rq_respages points one past arg pages */
  260. rqstp->rq_respages = &rqstp->rq_arg.pages[page_no];
  261. /* Create the reply and chunk maps */
  262. offset = 0;
  263. ch = (struct rpcrdma_read_chunk *)&rmsgp->rm_body.rm_chunks[0];
  264. for (ch_no = 0; ch_no < ch_count; ch_no++) {
  265. rpl_map->sge[ch_no].iov_base = frmr->kva + offset;
  266. rpl_map->sge[ch_no].iov_len = ch->rc_target.rs_length;
  267. chl_map->ch[ch_no].count = 1;
  268. chl_map->ch[ch_no].start = ch_no;
  269. offset += ch->rc_target.rs_length;
  270. ch++;
  271. }
  272. ret = svc_rdma_fastreg(xprt, frmr);
  273. if (ret)
  274. goto fatal_err;
  275. return ch_no;
  276. fatal_err:
  277. printk("svcrdma: error fast registering xdr for xprt %p", xprt);
  278. svc_rdma_put_frmr(xprt, frmr);
  279. return -EIO;
  280. }
  281. static int rdma_set_ctxt_sge(struct svcxprt_rdma *xprt,
  282. struct svc_rdma_op_ctxt *ctxt,
  283. struct svc_rdma_fastreg_mr *frmr,
  284. struct kvec *vec,
  285. u64 *sgl_offset,
  286. int count)
  287. {
  288. int i;
  289. ctxt->count = count;
  290. ctxt->direction = DMA_FROM_DEVICE;
  291. for (i = 0; i < count; i++) {
  292. ctxt->sge[i].length = 0; /* in case map fails */
  293. if (!frmr) {
  294. ctxt->sge[i].addr =
  295. ib_dma_map_single(xprt->sc_cm_id->device,
  296. vec[i].iov_base,
  297. vec[i].iov_len,
  298. DMA_FROM_DEVICE);
  299. if (ib_dma_mapping_error(xprt->sc_cm_id->device,
  300. ctxt->sge[i].addr))
  301. return -EINVAL;
  302. ctxt->sge[i].lkey = xprt->sc_dma_lkey;
  303. atomic_inc(&xprt->sc_dma_used);
  304. } else {
  305. ctxt->sge[i].addr = (unsigned long)vec[i].iov_base;
  306. ctxt->sge[i].lkey = frmr->mr->lkey;
  307. }
  308. ctxt->sge[i].length = vec[i].iov_len;
  309. *sgl_offset = *sgl_offset + vec[i].iov_len;
  310. }
  311. return 0;
  312. }
  313. static int rdma_read_max_sge(struct svcxprt_rdma *xprt, int sge_count)
  314. {
  315. if ((RDMA_TRANSPORT_IWARP ==
  316. rdma_node_get_transport(xprt->sc_cm_id->
  317. device->node_type))
  318. && sge_count > 1)
  319. return 1;
  320. else
  321. return min_t(int, sge_count, xprt->sc_max_sge);
  322. }
  323. /*
  324. * Use RDMA_READ to read data from the advertised client buffer into the
  325. * XDR stream starting at rq_arg.head[0].iov_base.
  326. * Each chunk in the array
  327. * contains the following fields:
  328. * discrim - '1', This isn't used for data placement
  329. * position - The xdr stream offset (the same for every chunk)
  330. * handle - RMR for client memory region
  331. * length - data transfer length
  332. * offset - 64 bit tagged offset in remote memory region
  333. *
  334. * On our side, we need to read into a pagelist. The first page immediately
  335. * follows the RPC header.
  336. *
  337. * This function returns:
  338. * 0 - No error and no read-list found.
  339. *
  340. * 1 - Successful read-list processing. The data is not yet in
  341. * the pagelist and therefore the RPC request must be deferred. The
  342. * I/O completion will enqueue the transport again and
  343. * svc_rdma_recvfrom will complete the request.
  344. *
  345. * <0 - Error processing/posting read-list.
  346. *
  347. * NOTE: The ctxt must not be touched after the last WR has been posted
  348. * because the I/O completion processing may occur on another
  349. * processor and free / modify the context. Ne touche pas!
  350. */
  351. static int rdma_read_xdr(struct svcxprt_rdma *xprt,
  352. struct rpcrdma_msg *rmsgp,
  353. struct svc_rqst *rqstp,
  354. struct svc_rdma_op_ctxt *hdr_ctxt)
  355. {
  356. struct ib_send_wr read_wr;
  357. struct ib_send_wr inv_wr;
  358. int err = 0;
  359. int ch_no;
  360. int ch_count;
  361. int byte_count;
  362. int sge_count;
  363. u64 sgl_offset;
  364. struct rpcrdma_read_chunk *ch;
  365. struct svc_rdma_op_ctxt *ctxt = NULL;
  366. struct svc_rdma_req_map *rpl_map;
  367. struct svc_rdma_req_map *chl_map;
  368. /* If no read list is present, return 0 */
  369. ch = svc_rdma_get_read_chunk(rmsgp);
  370. if (!ch)
  371. return 0;
  372. /* Allocate temporary reply and chunk maps */
  373. rpl_map = svc_rdma_get_req_map();
  374. chl_map = svc_rdma_get_req_map();
  375. svc_rdma_rcl_chunk_counts(ch, &ch_count, &byte_count);
  376. if (ch_count > RPCSVC_MAXPAGES)
  377. return -EINVAL;
  378. if (!xprt->sc_frmr_pg_list_len)
  379. sge_count = map_read_chunks(xprt, rqstp, hdr_ctxt, rmsgp,
  380. rpl_map, chl_map, ch_count,
  381. byte_count);
  382. else
  383. sge_count = fast_reg_read_chunks(xprt, rqstp, hdr_ctxt, rmsgp,
  384. rpl_map, chl_map, ch_count,
  385. byte_count);
  386. if (sge_count < 0) {
  387. err = -EIO;
  388. goto out;
  389. }
  390. sgl_offset = 0;
  391. ch_no = 0;
  392. for (ch = (struct rpcrdma_read_chunk *)&rmsgp->rm_body.rm_chunks[0];
  393. ch->rc_discrim != 0; ch++, ch_no++) {
  394. next_sge:
  395. ctxt = svc_rdma_get_context(xprt);
  396. ctxt->direction = DMA_FROM_DEVICE;
  397. ctxt->frmr = hdr_ctxt->frmr;
  398. ctxt->read_hdr = NULL;
  399. clear_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags);
  400. clear_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags);
  401. /* Prepare READ WR */
  402. memset(&read_wr, 0, sizeof read_wr);
  403. read_wr.wr_id = (unsigned long)ctxt;
  404. read_wr.opcode = IB_WR_RDMA_READ;
  405. ctxt->wr_op = read_wr.opcode;
  406. read_wr.send_flags = IB_SEND_SIGNALED;
  407. read_wr.wr.rdma.rkey = ch->rc_target.rs_handle;
  408. read_wr.wr.rdma.remote_addr =
  409. get_unaligned(&(ch->rc_target.rs_offset)) +
  410. sgl_offset;
  411. read_wr.sg_list = ctxt->sge;
  412. read_wr.num_sge =
  413. rdma_read_max_sge(xprt, chl_map->ch[ch_no].count);
  414. err = rdma_set_ctxt_sge(xprt, ctxt, hdr_ctxt->frmr,
  415. &rpl_map->sge[chl_map->ch[ch_no].start],
  416. &sgl_offset,
  417. read_wr.num_sge);
  418. if (err) {
  419. svc_rdma_unmap_dma(ctxt);
  420. svc_rdma_put_context(ctxt, 0);
  421. goto out;
  422. }
  423. if (((ch+1)->rc_discrim == 0) &&
  424. (read_wr.num_sge == chl_map->ch[ch_no].count)) {
  425. /*
  426. * Mark the last RDMA_READ with a bit to
  427. * indicate all RPC data has been fetched from
  428. * the client and the RPC needs to be enqueued.
  429. */
  430. set_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags);
  431. if (hdr_ctxt->frmr) {
  432. set_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags);
  433. /*
  434. * Invalidate the local MR used to map the data
  435. * sink.
  436. */
  437. if (xprt->sc_dev_caps &
  438. SVCRDMA_DEVCAP_READ_W_INV) {
  439. read_wr.opcode =
  440. IB_WR_RDMA_READ_WITH_INV;
  441. ctxt->wr_op = read_wr.opcode;
  442. read_wr.ex.invalidate_rkey =
  443. ctxt->frmr->mr->lkey;
  444. } else {
  445. /* Prepare INVALIDATE WR */
  446. memset(&inv_wr, 0, sizeof inv_wr);
  447. inv_wr.opcode = IB_WR_LOCAL_INV;
  448. inv_wr.send_flags = IB_SEND_SIGNALED;
  449. inv_wr.ex.invalidate_rkey =
  450. hdr_ctxt->frmr->mr->lkey;
  451. read_wr.next = &inv_wr;
  452. }
  453. }
  454. ctxt->read_hdr = hdr_ctxt;
  455. }
  456. /* Post the read */
  457. err = svc_rdma_send(xprt, &read_wr);
  458. if (err) {
  459. printk(KERN_ERR "svcrdma: Error %d posting RDMA_READ\n",
  460. err);
  461. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  462. svc_rdma_put_context(ctxt, 0);
  463. goto out;
  464. }
  465. atomic_inc(&rdma_stat_read);
  466. if (read_wr.num_sge < chl_map->ch[ch_no].count) {
  467. chl_map->ch[ch_no].count -= read_wr.num_sge;
  468. chl_map->ch[ch_no].start += read_wr.num_sge;
  469. goto next_sge;
  470. }
  471. sgl_offset = 0;
  472. err = 1;
  473. }
  474. out:
  475. svc_rdma_put_req_map(rpl_map);
  476. svc_rdma_put_req_map(chl_map);
  477. /* Detach arg pages. svc_recv will replenish them */
  478. for (ch_no = 0; &rqstp->rq_pages[ch_no] < rqstp->rq_respages; ch_no++)
  479. rqstp->rq_pages[ch_no] = NULL;
  480. /*
  481. * Detach res pages. svc_release must see a resused count of
  482. * zero or it will attempt to put them.
  483. */
  484. while (rqstp->rq_resused)
  485. rqstp->rq_respages[--rqstp->rq_resused] = NULL;
  486. return err;
  487. }
  488. static int rdma_read_complete(struct svc_rqst *rqstp,
  489. struct svc_rdma_op_ctxt *head)
  490. {
  491. int page_no;
  492. int ret;
  493. BUG_ON(!head);
  494. /* Copy RPC pages */
  495. for (page_no = 0; page_no < head->count; page_no++) {
  496. put_page(rqstp->rq_pages[page_no]);
  497. rqstp->rq_pages[page_no] = head->pages[page_no];
  498. }
  499. /* Point rq_arg.pages past header */
  500. rqstp->rq_arg.pages = &rqstp->rq_pages[head->hdr_count];
  501. rqstp->rq_arg.page_len = head->arg.page_len;
  502. rqstp->rq_arg.page_base = head->arg.page_base;
  503. /* rq_respages starts after the last arg page */
  504. rqstp->rq_respages = &rqstp->rq_arg.pages[page_no];
  505. rqstp->rq_resused = 0;
  506. /* Rebuild rq_arg head and tail. */
  507. rqstp->rq_arg.head[0] = head->arg.head[0];
  508. rqstp->rq_arg.tail[0] = head->arg.tail[0];
  509. rqstp->rq_arg.len = head->arg.len;
  510. rqstp->rq_arg.buflen = head->arg.buflen;
  511. /* Free the context */
  512. svc_rdma_put_context(head, 0);
  513. /* XXX: What should this be? */
  514. rqstp->rq_prot = IPPROTO_MAX;
  515. svc_xprt_copy_addrs(rqstp, rqstp->rq_xprt);
  516. ret = rqstp->rq_arg.head[0].iov_len
  517. + rqstp->rq_arg.page_len
  518. + rqstp->rq_arg.tail[0].iov_len;
  519. dprintk("svcrdma: deferred read ret=%d, rq_arg.len =%d, "
  520. "rq_arg.head[0].iov_base=%p, rq_arg.head[0].iov_len = %zd\n",
  521. ret, rqstp->rq_arg.len, rqstp->rq_arg.head[0].iov_base,
  522. rqstp->rq_arg.head[0].iov_len);
  523. svc_xprt_received(rqstp->rq_xprt);
  524. return ret;
  525. }
  526. /*
  527. * Set up the rqstp thread context to point to the RQ buffer. If
  528. * necessary, pull additional data from the client with an RDMA_READ
  529. * request.
  530. */
  531. int svc_rdma_recvfrom(struct svc_rqst *rqstp)
  532. {
  533. struct svc_xprt *xprt = rqstp->rq_xprt;
  534. struct svcxprt_rdma *rdma_xprt =
  535. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  536. struct svc_rdma_op_ctxt *ctxt = NULL;
  537. struct rpcrdma_msg *rmsgp;
  538. int ret = 0;
  539. int len;
  540. dprintk("svcrdma: rqstp=%p\n", rqstp);
  541. spin_lock_bh(&rdma_xprt->sc_rq_dto_lock);
  542. if (!list_empty(&rdma_xprt->sc_read_complete_q)) {
  543. ctxt = list_entry(rdma_xprt->sc_read_complete_q.next,
  544. struct svc_rdma_op_ctxt,
  545. dto_q);
  546. list_del_init(&ctxt->dto_q);
  547. }
  548. if (ctxt) {
  549. spin_unlock_bh(&rdma_xprt->sc_rq_dto_lock);
  550. return rdma_read_complete(rqstp, ctxt);
  551. }
  552. if (!list_empty(&rdma_xprt->sc_rq_dto_q)) {
  553. ctxt = list_entry(rdma_xprt->sc_rq_dto_q.next,
  554. struct svc_rdma_op_ctxt,
  555. dto_q);
  556. list_del_init(&ctxt->dto_q);
  557. } else {
  558. atomic_inc(&rdma_stat_rq_starve);
  559. clear_bit(XPT_DATA, &xprt->xpt_flags);
  560. ctxt = NULL;
  561. }
  562. spin_unlock_bh(&rdma_xprt->sc_rq_dto_lock);
  563. if (!ctxt) {
  564. /* This is the EAGAIN path. The svc_recv routine will
  565. * return -EAGAIN, the nfsd thread will go to call into
  566. * svc_recv again and we shouldn't be on the active
  567. * transport list
  568. */
  569. if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
  570. goto close_out;
  571. BUG_ON(ret);
  572. goto out;
  573. }
  574. dprintk("svcrdma: processing ctxt=%p on xprt=%p, rqstp=%p, status=%d\n",
  575. ctxt, rdma_xprt, rqstp, ctxt->wc_status);
  576. BUG_ON(ctxt->wc_status != IB_WC_SUCCESS);
  577. atomic_inc(&rdma_stat_recv);
  578. /* Build up the XDR from the receive buffers. */
  579. rdma_build_arg_xdr(rqstp, ctxt, ctxt->byte_len);
  580. /* Decode the RDMA header. */
  581. len = svc_rdma_xdr_decode_req(&rmsgp, rqstp);
  582. rqstp->rq_xprt_hlen = len;
  583. /* If the request is invalid, reply with an error */
  584. if (len < 0) {
  585. if (len == -ENOSYS)
  586. svc_rdma_send_error(rdma_xprt, rmsgp, ERR_VERS);
  587. goto close_out;
  588. }
  589. /* Read read-list data. */
  590. ret = rdma_read_xdr(rdma_xprt, rmsgp, rqstp, ctxt);
  591. if (ret > 0) {
  592. /* read-list posted, defer until data received from client. */
  593. goto defer;
  594. }
  595. if (ret < 0) {
  596. /* Post of read-list failed, free context. */
  597. svc_rdma_put_context(ctxt, 1);
  598. return 0;
  599. }
  600. ret = rqstp->rq_arg.head[0].iov_len
  601. + rqstp->rq_arg.page_len
  602. + rqstp->rq_arg.tail[0].iov_len;
  603. svc_rdma_put_context(ctxt, 0);
  604. out:
  605. dprintk("svcrdma: ret = %d, rq_arg.len =%d, "
  606. "rq_arg.head[0].iov_base=%p, rq_arg.head[0].iov_len = %zd\n",
  607. ret, rqstp->rq_arg.len,
  608. rqstp->rq_arg.head[0].iov_base,
  609. rqstp->rq_arg.head[0].iov_len);
  610. rqstp->rq_prot = IPPROTO_MAX;
  611. svc_xprt_copy_addrs(rqstp, xprt);
  612. svc_xprt_received(xprt);
  613. return ret;
  614. close_out:
  615. if (ctxt)
  616. svc_rdma_put_context(ctxt, 1);
  617. dprintk("svcrdma: transport %p is closing\n", xprt);
  618. /*
  619. * Set the close bit and enqueue it. svc_recv will see the
  620. * close bit and call svc_xprt_delete
  621. */
  622. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  623. defer:
  624. svc_xprt_received(xprt);
  625. return 0;
  626. }