svc_rdma_sendto.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. /* Encode an XDR as an array of IB SGE
  50. *
  51. * Assumptions:
  52. * - head[0] is physically contiguous.
  53. * - tail[0] is physically contiguous.
  54. * - pages[] is not physically or virtually contiguous and consists of
  55. * PAGE_SIZE elements.
  56. *
  57. * Output:
  58. * SGE[0] reserved for RCPRDMA header
  59. * SGE[1] data from xdr->head[]
  60. * SGE[2..sge_count-2] data from xdr->pages[]
  61. * SGE[sge_count-1] data from xdr->tail.
  62. *
  63. * The max SGE we need is the length of the XDR / pagesize + one for
  64. * head + one for tail + one for RPCRDMA header. Since RPCSVC_MAXPAGES
  65. * reserves a page for both the request and the reply header, and this
  66. * array is only concerned with the reply we are assured that we have
  67. * on extra page for the RPCRMDA header.
  68. */
  69. static int fast_reg_xdr(struct svcxprt_rdma *xprt,
  70. struct xdr_buf *xdr,
  71. struct svc_rdma_req_map *vec)
  72. {
  73. int sge_no;
  74. u32 sge_bytes;
  75. u32 page_bytes;
  76. u32 page_off;
  77. int page_no = 0;
  78. u8 *frva;
  79. struct svc_rdma_fastreg_mr *frmr;
  80. frmr = svc_rdma_get_frmr(xprt);
  81. if (IS_ERR(frmr))
  82. return -ENOMEM;
  83. vec->frmr = frmr;
  84. /* Skip the RPCRDMA header */
  85. sge_no = 1;
  86. /* Map the head. */
  87. frva = (void *)((unsigned long)(xdr->head[0].iov_base) & PAGE_MASK);
  88. vec->sge[sge_no].iov_base = xdr->head[0].iov_base;
  89. vec->sge[sge_no].iov_len = xdr->head[0].iov_len;
  90. vec->count = 2;
  91. sge_no++;
  92. /* Map the XDR head */
  93. frmr->kva = frva;
  94. frmr->direction = DMA_TO_DEVICE;
  95. frmr->access_flags = 0;
  96. frmr->map_len = PAGE_SIZE;
  97. frmr->page_list_len = 1;
  98. page_off = (unsigned long)xdr->head[0].iov_base & ~PAGE_MASK;
  99. frmr->page_list->page_list[page_no] =
  100. ib_dma_map_page(xprt->sc_cm_id->device,
  101. virt_to_page(xdr->head[0].iov_base),
  102. page_off,
  103. PAGE_SIZE - page_off,
  104. DMA_TO_DEVICE);
  105. if (ib_dma_mapping_error(xprt->sc_cm_id->device,
  106. frmr->page_list->page_list[page_no]))
  107. goto fatal_err;
  108. atomic_inc(&xprt->sc_dma_used);
  109. /* Map the XDR page list */
  110. page_off = xdr->page_base;
  111. page_bytes = xdr->page_len + page_off;
  112. if (!page_bytes)
  113. goto encode_tail;
  114. /* Map the pages */
  115. vec->sge[sge_no].iov_base = frva + frmr->map_len + page_off;
  116. vec->sge[sge_no].iov_len = page_bytes;
  117. sge_no++;
  118. while (page_bytes) {
  119. struct page *page;
  120. page = xdr->pages[page_no++];
  121. sge_bytes = min_t(u32, page_bytes, (PAGE_SIZE - page_off));
  122. page_bytes -= sge_bytes;
  123. frmr->page_list->page_list[page_no] =
  124. ib_dma_map_page(xprt->sc_cm_id->device,
  125. page, page_off,
  126. sge_bytes, DMA_TO_DEVICE);
  127. if (ib_dma_mapping_error(xprt->sc_cm_id->device,
  128. frmr->page_list->page_list[page_no]))
  129. goto fatal_err;
  130. atomic_inc(&xprt->sc_dma_used);
  131. page_off = 0; /* reset for next time through loop */
  132. frmr->map_len += PAGE_SIZE;
  133. frmr->page_list_len++;
  134. }
  135. vec->count++;
  136. encode_tail:
  137. /* Map tail */
  138. if (0 == xdr->tail[0].iov_len)
  139. goto done;
  140. vec->count++;
  141. vec->sge[sge_no].iov_len = xdr->tail[0].iov_len;
  142. if (((unsigned long)xdr->tail[0].iov_base & PAGE_MASK) ==
  143. ((unsigned long)xdr->head[0].iov_base & PAGE_MASK)) {
  144. /*
  145. * If head and tail use the same page, we don't need
  146. * to map it again.
  147. */
  148. vec->sge[sge_no].iov_base = xdr->tail[0].iov_base;
  149. } else {
  150. void *va;
  151. /* Map another page for the tail */
  152. page_off = (unsigned long)xdr->tail[0].iov_base & ~PAGE_MASK;
  153. va = (void *)((unsigned long)xdr->tail[0].iov_base & PAGE_MASK);
  154. vec->sge[sge_no].iov_base = frva + frmr->map_len + page_off;
  155. frmr->page_list->page_list[page_no] =
  156. ib_dma_map_page(xprt->sc_cm_id->device, virt_to_page(va),
  157. page_off,
  158. PAGE_SIZE,
  159. DMA_TO_DEVICE);
  160. if (ib_dma_mapping_error(xprt->sc_cm_id->device,
  161. frmr->page_list->page_list[page_no]))
  162. goto fatal_err;
  163. atomic_inc(&xprt->sc_dma_used);
  164. frmr->map_len += PAGE_SIZE;
  165. frmr->page_list_len++;
  166. }
  167. done:
  168. if (svc_rdma_fastreg(xprt, frmr))
  169. goto fatal_err;
  170. return 0;
  171. fatal_err:
  172. printk("svcrdma: Error fast registering memory for xprt %p\n", xprt);
  173. vec->frmr = NULL;
  174. svc_rdma_put_frmr(xprt, frmr);
  175. return -EIO;
  176. }
  177. static int map_xdr(struct svcxprt_rdma *xprt,
  178. struct xdr_buf *xdr,
  179. struct svc_rdma_req_map *vec)
  180. {
  181. int sge_no;
  182. u32 sge_bytes;
  183. u32 page_bytes;
  184. u32 page_off;
  185. int page_no;
  186. BUG_ON(xdr->len !=
  187. (xdr->head[0].iov_len + xdr->page_len + xdr->tail[0].iov_len));
  188. if (xprt->sc_frmr_pg_list_len)
  189. return fast_reg_xdr(xprt, xdr, vec);
  190. /* Skip the first sge, this is for the RPCRDMA header */
  191. sge_no = 1;
  192. /* Head SGE */
  193. vec->sge[sge_no].iov_base = xdr->head[0].iov_base;
  194. vec->sge[sge_no].iov_len = xdr->head[0].iov_len;
  195. sge_no++;
  196. /* pages SGE */
  197. page_no = 0;
  198. page_bytes = xdr->page_len;
  199. page_off = xdr->page_base;
  200. while (page_bytes) {
  201. vec->sge[sge_no].iov_base =
  202. page_address(xdr->pages[page_no]) + page_off;
  203. sge_bytes = min_t(u32, page_bytes, (PAGE_SIZE - page_off));
  204. page_bytes -= sge_bytes;
  205. vec->sge[sge_no].iov_len = sge_bytes;
  206. sge_no++;
  207. page_no++;
  208. page_off = 0; /* reset for next time through loop */
  209. }
  210. /* Tail SGE */
  211. if (xdr->tail[0].iov_len) {
  212. vec->sge[sge_no].iov_base = xdr->tail[0].iov_base;
  213. vec->sge[sge_no].iov_len = xdr->tail[0].iov_len;
  214. sge_no++;
  215. }
  216. dprintk("svcrdma: map_xdr: sge_no %d page_no %d "
  217. "page_base %u page_len %u head_len %zu tail_len %zu\n",
  218. sge_no, page_no, xdr->page_base, xdr->page_len,
  219. xdr->head[0].iov_len, xdr->tail[0].iov_len);
  220. vec->count = sge_no;
  221. return 0;
  222. }
  223. static dma_addr_t dma_map_xdr(struct svcxprt_rdma *xprt,
  224. struct xdr_buf *xdr,
  225. u32 xdr_off, size_t len, int dir)
  226. {
  227. struct page *page;
  228. dma_addr_t dma_addr;
  229. if (xdr_off < xdr->head[0].iov_len) {
  230. /* This offset is in the head */
  231. xdr_off += (unsigned long)xdr->head[0].iov_base & ~PAGE_MASK;
  232. page = virt_to_page(xdr->head[0].iov_base);
  233. } else {
  234. xdr_off -= xdr->head[0].iov_len;
  235. if (xdr_off < xdr->page_len) {
  236. /* This offset is in the page list */
  237. page = xdr->pages[xdr_off >> PAGE_SHIFT];
  238. xdr_off &= ~PAGE_MASK;
  239. } else {
  240. /* This offset is in the tail */
  241. xdr_off -= xdr->page_len;
  242. xdr_off += (unsigned long)
  243. xdr->tail[0].iov_base & ~PAGE_MASK;
  244. page = virt_to_page(xdr->tail[0].iov_base);
  245. }
  246. }
  247. dma_addr = ib_dma_map_page(xprt->sc_cm_id->device, page, xdr_off,
  248. min_t(size_t, PAGE_SIZE, len), dir);
  249. return dma_addr;
  250. }
  251. /* Assumptions:
  252. * - We are using FRMR
  253. * - or -
  254. * - The specified write_len can be represented in sc_max_sge * PAGE_SIZE
  255. */
  256. static int send_write(struct svcxprt_rdma *xprt, struct svc_rqst *rqstp,
  257. u32 rmr, u64 to,
  258. u32 xdr_off, int write_len,
  259. struct svc_rdma_req_map *vec)
  260. {
  261. struct ib_send_wr write_wr;
  262. struct ib_sge *sge;
  263. int xdr_sge_no;
  264. int sge_no;
  265. int sge_bytes;
  266. int sge_off;
  267. int bc;
  268. struct svc_rdma_op_ctxt *ctxt;
  269. BUG_ON(vec->count > RPCSVC_MAXPAGES);
  270. dprintk("svcrdma: RDMA_WRITE rmr=%x, to=%llx, xdr_off=%d, "
  271. "write_len=%d, vec->sge=%p, vec->count=%lu\n",
  272. rmr, (unsigned long long)to, xdr_off,
  273. write_len, vec->sge, vec->count);
  274. ctxt = svc_rdma_get_context(xprt);
  275. ctxt->direction = DMA_TO_DEVICE;
  276. sge = ctxt->sge;
  277. /* Find the SGE associated with xdr_off */
  278. for (bc = xdr_off, xdr_sge_no = 1; bc && xdr_sge_no < vec->count;
  279. xdr_sge_no++) {
  280. if (vec->sge[xdr_sge_no].iov_len > bc)
  281. break;
  282. bc -= vec->sge[xdr_sge_no].iov_len;
  283. }
  284. sge_off = bc;
  285. bc = write_len;
  286. sge_no = 0;
  287. /* Copy the remaining SGE */
  288. while (bc != 0) {
  289. sge_bytes = min_t(size_t,
  290. bc, vec->sge[xdr_sge_no].iov_len-sge_off);
  291. sge[sge_no].length = sge_bytes;
  292. if (!vec->frmr) {
  293. sge[sge_no].addr =
  294. dma_map_xdr(xprt, &rqstp->rq_res, xdr_off,
  295. sge_bytes, DMA_TO_DEVICE);
  296. xdr_off += sge_bytes;
  297. if (ib_dma_mapping_error(xprt->sc_cm_id->device,
  298. sge[sge_no].addr))
  299. goto err;
  300. atomic_inc(&xprt->sc_dma_used);
  301. sge[sge_no].lkey = xprt->sc_dma_lkey;
  302. } else {
  303. sge[sge_no].addr = (unsigned long)
  304. vec->sge[xdr_sge_no].iov_base + sge_off;
  305. sge[sge_no].lkey = vec->frmr->mr->lkey;
  306. }
  307. ctxt->count++;
  308. ctxt->frmr = vec->frmr;
  309. sge_off = 0;
  310. sge_no++;
  311. xdr_sge_no++;
  312. BUG_ON(xdr_sge_no > vec->count);
  313. bc -= sge_bytes;
  314. }
  315. /* Prepare WRITE WR */
  316. memset(&write_wr, 0, sizeof write_wr);
  317. ctxt->wr_op = IB_WR_RDMA_WRITE;
  318. write_wr.wr_id = (unsigned long)ctxt;
  319. write_wr.sg_list = &sge[0];
  320. write_wr.num_sge = sge_no;
  321. write_wr.opcode = IB_WR_RDMA_WRITE;
  322. write_wr.send_flags = IB_SEND_SIGNALED;
  323. write_wr.wr.rdma.rkey = rmr;
  324. write_wr.wr.rdma.remote_addr = to;
  325. /* Post It */
  326. atomic_inc(&rdma_stat_write);
  327. if (svc_rdma_send(xprt, &write_wr))
  328. goto err;
  329. return 0;
  330. err:
  331. svc_rdma_unmap_dma(ctxt);
  332. svc_rdma_put_frmr(xprt, vec->frmr);
  333. svc_rdma_put_context(ctxt, 0);
  334. /* Fatal error, close transport */
  335. return -EIO;
  336. }
  337. static int send_write_chunks(struct svcxprt_rdma *xprt,
  338. struct rpcrdma_msg *rdma_argp,
  339. struct rpcrdma_msg *rdma_resp,
  340. struct svc_rqst *rqstp,
  341. struct svc_rdma_req_map *vec)
  342. {
  343. u32 xfer_len = rqstp->rq_res.page_len + rqstp->rq_res.tail[0].iov_len;
  344. int write_len;
  345. int max_write;
  346. u32 xdr_off;
  347. int chunk_off;
  348. int chunk_no;
  349. struct rpcrdma_write_array *arg_ary;
  350. struct rpcrdma_write_array *res_ary;
  351. int ret;
  352. arg_ary = svc_rdma_get_write_array(rdma_argp);
  353. if (!arg_ary)
  354. return 0;
  355. res_ary = (struct rpcrdma_write_array *)
  356. &rdma_resp->rm_body.rm_chunks[1];
  357. if (vec->frmr)
  358. max_write = vec->frmr->map_len;
  359. else
  360. max_write = xprt->sc_max_sge * PAGE_SIZE;
  361. /* Write chunks start at the pagelist */
  362. for (xdr_off = rqstp->rq_res.head[0].iov_len, chunk_no = 0;
  363. xfer_len && chunk_no < arg_ary->wc_nchunks;
  364. chunk_no++) {
  365. struct rpcrdma_segment *arg_ch;
  366. u64 rs_offset;
  367. arg_ch = &arg_ary->wc_array[chunk_no].wc_target;
  368. write_len = min(xfer_len, arg_ch->rs_length);
  369. /* Prepare the response chunk given the length actually
  370. * written */
  371. rs_offset = get_unaligned(&(arg_ch->rs_offset));
  372. svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
  373. arg_ch->rs_handle,
  374. rs_offset,
  375. write_len);
  376. chunk_off = 0;
  377. while (write_len) {
  378. int this_write;
  379. this_write = min(write_len, max_write);
  380. ret = send_write(xprt, rqstp,
  381. arg_ch->rs_handle,
  382. rs_offset + chunk_off,
  383. xdr_off,
  384. this_write,
  385. vec);
  386. if (ret) {
  387. dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
  388. ret);
  389. return -EIO;
  390. }
  391. chunk_off += this_write;
  392. xdr_off += this_write;
  393. xfer_len -= this_write;
  394. write_len -= this_write;
  395. }
  396. }
  397. /* Update the req with the number of chunks actually used */
  398. svc_rdma_xdr_encode_write_list(rdma_resp, chunk_no);
  399. return rqstp->rq_res.page_len + rqstp->rq_res.tail[0].iov_len;
  400. }
  401. static int send_reply_chunks(struct svcxprt_rdma *xprt,
  402. struct rpcrdma_msg *rdma_argp,
  403. struct rpcrdma_msg *rdma_resp,
  404. struct svc_rqst *rqstp,
  405. struct svc_rdma_req_map *vec)
  406. {
  407. u32 xfer_len = rqstp->rq_res.len;
  408. int write_len;
  409. int max_write;
  410. u32 xdr_off;
  411. int chunk_no;
  412. int chunk_off;
  413. struct rpcrdma_segment *ch;
  414. struct rpcrdma_write_array *arg_ary;
  415. struct rpcrdma_write_array *res_ary;
  416. int ret;
  417. arg_ary = svc_rdma_get_reply_array(rdma_argp);
  418. if (!arg_ary)
  419. return 0;
  420. /* XXX: need to fix when reply lists occur with read-list and or
  421. * write-list */
  422. res_ary = (struct rpcrdma_write_array *)
  423. &rdma_resp->rm_body.rm_chunks[2];
  424. if (vec->frmr)
  425. max_write = vec->frmr->map_len;
  426. else
  427. max_write = xprt->sc_max_sge * PAGE_SIZE;
  428. /* xdr offset starts at RPC message */
  429. for (xdr_off = 0, chunk_no = 0;
  430. xfer_len && chunk_no < arg_ary->wc_nchunks;
  431. chunk_no++) {
  432. u64 rs_offset;
  433. ch = &arg_ary->wc_array[chunk_no].wc_target;
  434. write_len = min(xfer_len, ch->rs_length);
  435. /* Prepare the reply chunk given the length actually
  436. * written */
  437. rs_offset = get_unaligned(&(ch->rs_offset));
  438. svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
  439. ch->rs_handle, rs_offset,
  440. write_len);
  441. chunk_off = 0;
  442. while (write_len) {
  443. int this_write;
  444. this_write = min(write_len, max_write);
  445. ret = send_write(xprt, rqstp,
  446. ch->rs_handle,
  447. rs_offset + chunk_off,
  448. xdr_off,
  449. this_write,
  450. vec);
  451. if (ret) {
  452. dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
  453. ret);
  454. return -EIO;
  455. }
  456. chunk_off += this_write;
  457. xdr_off += this_write;
  458. xfer_len -= this_write;
  459. write_len -= this_write;
  460. }
  461. }
  462. /* Update the req with the number of chunks actually used */
  463. svc_rdma_xdr_encode_reply_array(res_ary, chunk_no);
  464. return rqstp->rq_res.len;
  465. }
  466. /* This function prepares the portion of the RPCRDMA message to be
  467. * sent in the RDMA_SEND. This function is called after data sent via
  468. * RDMA has already been transmitted. There are three cases:
  469. * - The RPCRDMA header, RPC header, and payload are all sent in a
  470. * single RDMA_SEND. This is the "inline" case.
  471. * - The RPCRDMA header and some portion of the RPC header and data
  472. * are sent via this RDMA_SEND and another portion of the data is
  473. * sent via RDMA.
  474. * - The RPCRDMA header [NOMSG] is sent in this RDMA_SEND and the RPC
  475. * header and data are all transmitted via RDMA.
  476. * In all three cases, this function prepares the RPCRDMA header in
  477. * sge[0], the 'type' parameter indicates the type to place in the
  478. * RPCRDMA header, and the 'byte_count' field indicates how much of
  479. * the XDR to include in this RDMA_SEND. NB: The offset of the payload
  480. * to send is zero in the XDR.
  481. */
  482. static int send_reply(struct svcxprt_rdma *rdma,
  483. struct svc_rqst *rqstp,
  484. struct page *page,
  485. struct rpcrdma_msg *rdma_resp,
  486. struct svc_rdma_op_ctxt *ctxt,
  487. struct svc_rdma_req_map *vec,
  488. int byte_count)
  489. {
  490. struct ib_send_wr send_wr;
  491. struct ib_send_wr inv_wr;
  492. int sge_no;
  493. int sge_bytes;
  494. int page_no;
  495. int ret;
  496. /* Post a recv buffer to handle another request. */
  497. ret = svc_rdma_post_recv(rdma);
  498. if (ret) {
  499. printk(KERN_INFO
  500. "svcrdma: could not post a receive buffer, err=%d."
  501. "Closing transport %p.\n", ret, rdma);
  502. set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
  503. svc_rdma_put_frmr(rdma, vec->frmr);
  504. svc_rdma_put_context(ctxt, 0);
  505. return -ENOTCONN;
  506. }
  507. /* Prepare the context */
  508. ctxt->pages[0] = page;
  509. ctxt->count = 1;
  510. ctxt->frmr = vec->frmr;
  511. if (vec->frmr)
  512. set_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags);
  513. else
  514. clear_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags);
  515. /* Prepare the SGE for the RPCRDMA Header */
  516. ctxt->sge[0].lkey = rdma->sc_dma_lkey;
  517. ctxt->sge[0].length = svc_rdma_xdr_get_reply_hdr_len(rdma_resp);
  518. ctxt->sge[0].addr =
  519. ib_dma_map_page(rdma->sc_cm_id->device, page, 0,
  520. ctxt->sge[0].length, DMA_TO_DEVICE);
  521. if (ib_dma_mapping_error(rdma->sc_cm_id->device, ctxt->sge[0].addr))
  522. goto err;
  523. atomic_inc(&rdma->sc_dma_used);
  524. ctxt->direction = DMA_TO_DEVICE;
  525. /* Map the payload indicated by 'byte_count' */
  526. for (sge_no = 1; byte_count && sge_no < vec->count; sge_no++) {
  527. int xdr_off = 0;
  528. sge_bytes = min_t(size_t, vec->sge[sge_no].iov_len, byte_count);
  529. byte_count -= sge_bytes;
  530. if (!vec->frmr) {
  531. ctxt->sge[sge_no].addr =
  532. dma_map_xdr(rdma, &rqstp->rq_res, xdr_off,
  533. sge_bytes, DMA_TO_DEVICE);
  534. xdr_off += sge_bytes;
  535. if (ib_dma_mapping_error(rdma->sc_cm_id->device,
  536. ctxt->sge[sge_no].addr))
  537. goto err;
  538. atomic_inc(&rdma->sc_dma_used);
  539. ctxt->sge[sge_no].lkey = rdma->sc_dma_lkey;
  540. } else {
  541. ctxt->sge[sge_no].addr = (unsigned long)
  542. vec->sge[sge_no].iov_base;
  543. ctxt->sge[sge_no].lkey = vec->frmr->mr->lkey;
  544. }
  545. ctxt->sge[sge_no].length = sge_bytes;
  546. }
  547. BUG_ON(byte_count != 0);
  548. /* Save all respages in the ctxt and remove them from the
  549. * respages array. They are our pages until the I/O
  550. * completes.
  551. */
  552. for (page_no = 0; page_no < rqstp->rq_resused; page_no++) {
  553. ctxt->pages[page_no+1] = rqstp->rq_respages[page_no];
  554. ctxt->count++;
  555. rqstp->rq_respages[page_no] = NULL;
  556. /*
  557. * If there are more pages than SGE, terminate SGE
  558. * list so that svc_rdma_unmap_dma doesn't attempt to
  559. * unmap garbage.
  560. */
  561. if (page_no+1 >= sge_no)
  562. ctxt->sge[page_no+1].length = 0;
  563. }
  564. BUG_ON(sge_no > rdma->sc_max_sge);
  565. memset(&send_wr, 0, sizeof send_wr);
  566. ctxt->wr_op = IB_WR_SEND;
  567. send_wr.wr_id = (unsigned long)ctxt;
  568. send_wr.sg_list = ctxt->sge;
  569. send_wr.num_sge = sge_no;
  570. send_wr.opcode = IB_WR_SEND;
  571. send_wr.send_flags = IB_SEND_SIGNALED;
  572. if (vec->frmr) {
  573. /* Prepare INVALIDATE WR */
  574. memset(&inv_wr, 0, sizeof inv_wr);
  575. inv_wr.opcode = IB_WR_LOCAL_INV;
  576. inv_wr.send_flags = IB_SEND_SIGNALED;
  577. inv_wr.ex.invalidate_rkey =
  578. vec->frmr->mr->lkey;
  579. send_wr.next = &inv_wr;
  580. }
  581. ret = svc_rdma_send(rdma, &send_wr);
  582. if (ret)
  583. goto err;
  584. return 0;
  585. err:
  586. svc_rdma_unmap_dma(ctxt);
  587. svc_rdma_put_frmr(rdma, vec->frmr);
  588. svc_rdma_put_context(ctxt, 1);
  589. return -EIO;
  590. }
  591. void svc_rdma_prep_reply_hdr(struct svc_rqst *rqstp)
  592. {
  593. }
  594. /*
  595. * Return the start of an xdr buffer.
  596. */
  597. static void *xdr_start(struct xdr_buf *xdr)
  598. {
  599. return xdr->head[0].iov_base -
  600. (xdr->len -
  601. xdr->page_len -
  602. xdr->tail[0].iov_len -
  603. xdr->head[0].iov_len);
  604. }
  605. int svc_rdma_sendto(struct svc_rqst *rqstp)
  606. {
  607. struct svc_xprt *xprt = rqstp->rq_xprt;
  608. struct svcxprt_rdma *rdma =
  609. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  610. struct rpcrdma_msg *rdma_argp;
  611. struct rpcrdma_msg *rdma_resp;
  612. struct rpcrdma_write_array *reply_ary;
  613. enum rpcrdma_proc reply_type;
  614. int ret;
  615. int inline_bytes;
  616. struct page *res_page;
  617. struct svc_rdma_op_ctxt *ctxt;
  618. struct svc_rdma_req_map *vec;
  619. dprintk("svcrdma: sending response for rqstp=%p\n", rqstp);
  620. /* Get the RDMA request header. */
  621. rdma_argp = xdr_start(&rqstp->rq_arg);
  622. /* Build an req vec for the XDR */
  623. ctxt = svc_rdma_get_context(rdma);
  624. ctxt->direction = DMA_TO_DEVICE;
  625. vec = svc_rdma_get_req_map();
  626. ret = map_xdr(rdma, &rqstp->rq_res, vec);
  627. if (ret)
  628. goto err0;
  629. inline_bytes = rqstp->rq_res.len;
  630. /* Create the RDMA response header */
  631. res_page = svc_rdma_get_page();
  632. rdma_resp = page_address(res_page);
  633. reply_ary = svc_rdma_get_reply_array(rdma_argp);
  634. if (reply_ary)
  635. reply_type = RDMA_NOMSG;
  636. else
  637. reply_type = RDMA_MSG;
  638. svc_rdma_xdr_encode_reply_header(rdma, rdma_argp,
  639. rdma_resp, reply_type);
  640. /* Send any write-chunk data and build resp write-list */
  641. ret = send_write_chunks(rdma, rdma_argp, rdma_resp,
  642. rqstp, vec);
  643. if (ret < 0) {
  644. printk(KERN_ERR "svcrdma: failed to send write chunks, rc=%d\n",
  645. ret);
  646. goto err1;
  647. }
  648. inline_bytes -= ret;
  649. /* Send any reply-list data and update resp reply-list */
  650. ret = send_reply_chunks(rdma, rdma_argp, rdma_resp,
  651. rqstp, vec);
  652. if (ret < 0) {
  653. printk(KERN_ERR "svcrdma: failed to send reply chunks, rc=%d\n",
  654. ret);
  655. goto err1;
  656. }
  657. inline_bytes -= ret;
  658. ret = send_reply(rdma, rqstp, res_page, rdma_resp, ctxt, vec,
  659. inline_bytes);
  660. svc_rdma_put_req_map(vec);
  661. dprintk("svcrdma: send_reply returns %d\n", ret);
  662. return ret;
  663. err1:
  664. put_page(res_page);
  665. err0:
  666. svc_rdma_put_req_map(vec);
  667. svc_rdma_put_context(ctxt, 0);
  668. return ret;
  669. }