svc_rdma_sendto.c 19 KB

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