svc_rdma_sendto.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 void xdr_to_sge(struct svcxprt_rdma *xprt,
  70. struct xdr_buf *xdr,
  71. struct svc_rdma_req_map *vec)
  72. {
  73. int sge_max = (xdr->len+PAGE_SIZE-1) / PAGE_SIZE + 3;
  74. int sge_no;
  75. u32 sge_bytes;
  76. u32 page_bytes;
  77. u32 page_off;
  78. int page_no;
  79. BUG_ON(xdr->len !=
  80. (xdr->head[0].iov_len + xdr->page_len + xdr->tail[0].iov_len));
  81. /* Skip the first sge, this is for the RPCRDMA header */
  82. sge_no = 1;
  83. /* Head SGE */
  84. vec->sge[sge_no].iov_base = xdr->head[0].iov_base;
  85. vec->sge[sge_no].iov_len = xdr->head[0].iov_len;
  86. sge_no++;
  87. /* pages SGE */
  88. page_no = 0;
  89. page_bytes = xdr->page_len;
  90. page_off = xdr->page_base;
  91. while (page_bytes) {
  92. vec->sge[sge_no].iov_base =
  93. page_address(xdr->pages[page_no]) + page_off;
  94. sge_bytes = min_t(u32, page_bytes, (PAGE_SIZE - page_off));
  95. page_bytes -= sge_bytes;
  96. vec->sge[sge_no].iov_len = sge_bytes;
  97. sge_no++;
  98. page_no++;
  99. page_off = 0; /* reset for next time through loop */
  100. }
  101. /* Tail SGE */
  102. if (xdr->tail[0].iov_len) {
  103. vec->sge[sge_no].iov_base = xdr->tail[0].iov_base;
  104. vec->sge[sge_no].iov_len = xdr->tail[0].iov_len;
  105. sge_no++;
  106. }
  107. BUG_ON(sge_no > sge_max);
  108. vec->count = sge_no;
  109. }
  110. /* Assumptions:
  111. * - The specified write_len can be represented in sc_max_sge * PAGE_SIZE
  112. */
  113. static int send_write(struct svcxprt_rdma *xprt, struct svc_rqst *rqstp,
  114. u32 rmr, u64 to,
  115. u32 xdr_off, int write_len,
  116. struct svc_rdma_req_map *vec)
  117. {
  118. struct ib_send_wr write_wr;
  119. struct ib_sge *sge;
  120. int xdr_sge_no;
  121. int sge_no;
  122. int sge_bytes;
  123. int sge_off;
  124. int bc;
  125. struct svc_rdma_op_ctxt *ctxt;
  126. BUG_ON(vec->count > RPCSVC_MAXPAGES);
  127. dprintk("svcrdma: RDMA_WRITE rmr=%x, to=%llx, xdr_off=%d, "
  128. "write_len=%d, vec->sge=%p, vec->count=%lu\n",
  129. rmr, (unsigned long long)to, xdr_off,
  130. write_len, vec->sge, vec->count);
  131. ctxt = svc_rdma_get_context(xprt);
  132. ctxt->direction = DMA_TO_DEVICE;
  133. sge = ctxt->sge;
  134. /* Find the SGE associated with xdr_off */
  135. for (bc = xdr_off, xdr_sge_no = 1; bc && xdr_sge_no < vec->count;
  136. xdr_sge_no++) {
  137. if (vec->sge[xdr_sge_no].iov_len > bc)
  138. break;
  139. bc -= vec->sge[xdr_sge_no].iov_len;
  140. }
  141. sge_off = bc;
  142. bc = write_len;
  143. sge_no = 0;
  144. /* Copy the remaining SGE */
  145. while (bc != 0 && xdr_sge_no < vec->count) {
  146. sge[sge_no].lkey = xprt->sc_phys_mr->lkey;
  147. sge_bytes = min((size_t)bc,
  148. (size_t)(vec->sge[xdr_sge_no].iov_len-sge_off));
  149. sge[sge_no].length = sge_bytes;
  150. atomic_inc(&xprt->sc_dma_used);
  151. sge[sge_no].addr =
  152. ib_dma_map_single(xprt->sc_cm_id->device,
  153. (void *)
  154. vec->sge[xdr_sge_no].iov_base + sge_off,
  155. sge_bytes, DMA_TO_DEVICE);
  156. if (dma_mapping_error(xprt->sc_cm_id->device->dma_device,
  157. sge[sge_no].addr))
  158. goto err;
  159. sge_off = 0;
  160. sge_no++;
  161. ctxt->count++;
  162. xdr_sge_no++;
  163. bc -= sge_bytes;
  164. }
  165. BUG_ON(bc != 0);
  166. BUG_ON(xdr_sge_no > vec->count);
  167. /* Prepare WRITE WR */
  168. memset(&write_wr, 0, sizeof write_wr);
  169. ctxt->wr_op = IB_WR_RDMA_WRITE;
  170. write_wr.wr_id = (unsigned long)ctxt;
  171. write_wr.sg_list = &sge[0];
  172. write_wr.num_sge = sge_no;
  173. write_wr.opcode = IB_WR_RDMA_WRITE;
  174. write_wr.send_flags = IB_SEND_SIGNALED;
  175. write_wr.wr.rdma.rkey = rmr;
  176. write_wr.wr.rdma.remote_addr = to;
  177. /* Post It */
  178. atomic_inc(&rdma_stat_write);
  179. if (svc_rdma_send(xprt, &write_wr))
  180. goto err;
  181. return 0;
  182. err:
  183. svc_rdma_put_context(ctxt, 0);
  184. /* Fatal error, close transport */
  185. return -EIO;
  186. }
  187. static int send_write_chunks(struct svcxprt_rdma *xprt,
  188. struct rpcrdma_msg *rdma_argp,
  189. struct rpcrdma_msg *rdma_resp,
  190. struct svc_rqst *rqstp,
  191. struct svc_rdma_req_map *vec)
  192. {
  193. u32 xfer_len = rqstp->rq_res.page_len + rqstp->rq_res.tail[0].iov_len;
  194. int write_len;
  195. int max_write;
  196. u32 xdr_off;
  197. int chunk_off;
  198. int chunk_no;
  199. struct rpcrdma_write_array *arg_ary;
  200. struct rpcrdma_write_array *res_ary;
  201. int ret;
  202. arg_ary = svc_rdma_get_write_array(rdma_argp);
  203. if (!arg_ary)
  204. return 0;
  205. res_ary = (struct rpcrdma_write_array *)
  206. &rdma_resp->rm_body.rm_chunks[1];
  207. max_write = xprt->sc_max_sge * PAGE_SIZE;
  208. /* Write chunks start at the pagelist */
  209. for (xdr_off = rqstp->rq_res.head[0].iov_len, chunk_no = 0;
  210. xfer_len && chunk_no < arg_ary->wc_nchunks;
  211. chunk_no++) {
  212. struct rpcrdma_segment *arg_ch;
  213. u64 rs_offset;
  214. arg_ch = &arg_ary->wc_array[chunk_no].wc_target;
  215. write_len = min(xfer_len, arg_ch->rs_length);
  216. /* Prepare the response chunk given the length actually
  217. * written */
  218. rs_offset = get_unaligned(&(arg_ch->rs_offset));
  219. svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
  220. arg_ch->rs_handle,
  221. rs_offset,
  222. write_len);
  223. chunk_off = 0;
  224. while (write_len) {
  225. int this_write;
  226. this_write = min(write_len, max_write);
  227. ret = send_write(xprt, rqstp,
  228. arg_ch->rs_handle,
  229. rs_offset + chunk_off,
  230. xdr_off,
  231. this_write,
  232. vec);
  233. if (ret) {
  234. dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
  235. ret);
  236. return -EIO;
  237. }
  238. chunk_off += this_write;
  239. xdr_off += this_write;
  240. xfer_len -= this_write;
  241. write_len -= this_write;
  242. }
  243. }
  244. /* Update the req with the number of chunks actually used */
  245. svc_rdma_xdr_encode_write_list(rdma_resp, chunk_no);
  246. return rqstp->rq_res.page_len + rqstp->rq_res.tail[0].iov_len;
  247. }
  248. static int send_reply_chunks(struct svcxprt_rdma *xprt,
  249. struct rpcrdma_msg *rdma_argp,
  250. struct rpcrdma_msg *rdma_resp,
  251. struct svc_rqst *rqstp,
  252. struct svc_rdma_req_map *vec)
  253. {
  254. u32 xfer_len = rqstp->rq_res.len;
  255. int write_len;
  256. int max_write;
  257. u32 xdr_off;
  258. int chunk_no;
  259. int chunk_off;
  260. struct rpcrdma_segment *ch;
  261. struct rpcrdma_write_array *arg_ary;
  262. struct rpcrdma_write_array *res_ary;
  263. int ret;
  264. arg_ary = svc_rdma_get_reply_array(rdma_argp);
  265. if (!arg_ary)
  266. return 0;
  267. /* XXX: need to fix when reply lists occur with read-list and or
  268. * write-list */
  269. res_ary = (struct rpcrdma_write_array *)
  270. &rdma_resp->rm_body.rm_chunks[2];
  271. max_write = xprt->sc_max_sge * PAGE_SIZE;
  272. /* xdr offset starts at RPC message */
  273. for (xdr_off = 0, chunk_no = 0;
  274. xfer_len && chunk_no < arg_ary->wc_nchunks;
  275. chunk_no++) {
  276. u64 rs_offset;
  277. ch = &arg_ary->wc_array[chunk_no].wc_target;
  278. write_len = min(xfer_len, ch->rs_length);
  279. /* Prepare the reply chunk given the length actually
  280. * written */
  281. rs_offset = get_unaligned(&(ch->rs_offset));
  282. svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
  283. ch->rs_handle, rs_offset,
  284. write_len);
  285. chunk_off = 0;
  286. while (write_len) {
  287. int this_write;
  288. this_write = min(write_len, max_write);
  289. ret = send_write(xprt, rqstp,
  290. ch->rs_handle,
  291. rs_offset + chunk_off,
  292. xdr_off,
  293. this_write,
  294. vec);
  295. if (ret) {
  296. dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
  297. ret);
  298. return -EIO;
  299. }
  300. chunk_off += this_write;
  301. xdr_off += this_write;
  302. xfer_len -= this_write;
  303. write_len -= this_write;
  304. }
  305. }
  306. /* Update the req with the number of chunks actually used */
  307. svc_rdma_xdr_encode_reply_array(res_ary, chunk_no);
  308. return rqstp->rq_res.len;
  309. }
  310. /* This function prepares the portion of the RPCRDMA message to be
  311. * sent in the RDMA_SEND. This function is called after data sent via
  312. * RDMA has already been transmitted. There are three cases:
  313. * - The RPCRDMA header, RPC header, and payload are all sent in a
  314. * single RDMA_SEND. This is the "inline" case.
  315. * - The RPCRDMA header and some portion of the RPC header and data
  316. * are sent via this RDMA_SEND and another portion of the data is
  317. * sent via RDMA.
  318. * - The RPCRDMA header [NOMSG] is sent in this RDMA_SEND and the RPC
  319. * header and data are all transmitted via RDMA.
  320. * In all three cases, this function prepares the RPCRDMA header in
  321. * sge[0], the 'type' parameter indicates the type to place in the
  322. * RPCRDMA header, and the 'byte_count' field indicates how much of
  323. * the XDR to include in this RDMA_SEND.
  324. */
  325. static int send_reply(struct svcxprt_rdma *rdma,
  326. struct svc_rqst *rqstp,
  327. struct page *page,
  328. struct rpcrdma_msg *rdma_resp,
  329. struct svc_rdma_op_ctxt *ctxt,
  330. struct svc_rdma_req_map *vec,
  331. int byte_count)
  332. {
  333. struct ib_send_wr send_wr;
  334. int sge_no;
  335. int sge_bytes;
  336. int page_no;
  337. int ret;
  338. /* Post a recv buffer to handle another request. */
  339. ret = svc_rdma_post_recv(rdma);
  340. if (ret) {
  341. printk(KERN_INFO
  342. "svcrdma: could not post a receive buffer, err=%d."
  343. "Closing transport %p.\n", ret, rdma);
  344. set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
  345. svc_rdma_put_context(ctxt, 0);
  346. return -ENOTCONN;
  347. }
  348. /* Prepare the context */
  349. ctxt->pages[0] = page;
  350. ctxt->count = 1;
  351. /* Prepare the SGE for the RPCRDMA Header */
  352. atomic_inc(&rdma->sc_dma_used);
  353. ctxt->sge[0].addr =
  354. ib_dma_map_page(rdma->sc_cm_id->device,
  355. page, 0, PAGE_SIZE, DMA_TO_DEVICE);
  356. ctxt->direction = DMA_TO_DEVICE;
  357. ctxt->sge[0].length = svc_rdma_xdr_get_reply_hdr_len(rdma_resp);
  358. ctxt->sge[0].lkey = rdma->sc_phys_mr->lkey;
  359. /* Determine how many of our SGE are to be transmitted */
  360. for (sge_no = 1; byte_count && sge_no < vec->count; sge_no++) {
  361. sge_bytes = min_t(size_t, vec->sge[sge_no].iov_len, byte_count);
  362. byte_count -= sge_bytes;
  363. atomic_inc(&rdma->sc_dma_used);
  364. ctxt->sge[sge_no].addr =
  365. ib_dma_map_single(rdma->sc_cm_id->device,
  366. vec->sge[sge_no].iov_base,
  367. sge_bytes, DMA_TO_DEVICE);
  368. ctxt->sge[sge_no].length = sge_bytes;
  369. ctxt->sge[sge_no].lkey = rdma->sc_phys_mr->lkey;
  370. }
  371. BUG_ON(byte_count != 0);
  372. /* Save all respages in the ctxt and remove them from the
  373. * respages array. They are our pages until the I/O
  374. * completes.
  375. */
  376. for (page_no = 0; page_no < rqstp->rq_resused; page_no++) {
  377. ctxt->pages[page_no+1] = rqstp->rq_respages[page_no];
  378. ctxt->count++;
  379. rqstp->rq_respages[page_no] = NULL;
  380. /* If there are more pages than SGE, terminate SGE list */
  381. if (page_no+1 >= sge_no)
  382. ctxt->sge[page_no+1].length = 0;
  383. }
  384. BUG_ON(sge_no > rdma->sc_max_sge);
  385. memset(&send_wr, 0, sizeof send_wr);
  386. ctxt->wr_op = IB_WR_SEND;
  387. send_wr.wr_id = (unsigned long)ctxt;
  388. send_wr.sg_list = ctxt->sge;
  389. send_wr.num_sge = sge_no;
  390. send_wr.opcode = IB_WR_SEND;
  391. send_wr.send_flags = IB_SEND_SIGNALED;
  392. ret = svc_rdma_send(rdma, &send_wr);
  393. if (ret)
  394. svc_rdma_put_context(ctxt, 1);
  395. return ret;
  396. }
  397. void svc_rdma_prep_reply_hdr(struct svc_rqst *rqstp)
  398. {
  399. }
  400. /*
  401. * Return the start of an xdr buffer.
  402. */
  403. static void *xdr_start(struct xdr_buf *xdr)
  404. {
  405. return xdr->head[0].iov_base -
  406. (xdr->len -
  407. xdr->page_len -
  408. xdr->tail[0].iov_len -
  409. xdr->head[0].iov_len);
  410. }
  411. int svc_rdma_sendto(struct svc_rqst *rqstp)
  412. {
  413. struct svc_xprt *xprt = rqstp->rq_xprt;
  414. struct svcxprt_rdma *rdma =
  415. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  416. struct rpcrdma_msg *rdma_argp;
  417. struct rpcrdma_msg *rdma_resp;
  418. struct rpcrdma_write_array *reply_ary;
  419. enum rpcrdma_proc reply_type;
  420. int ret;
  421. int inline_bytes;
  422. struct page *res_page;
  423. struct svc_rdma_op_ctxt *ctxt;
  424. struct svc_rdma_req_map *vec;
  425. dprintk("svcrdma: sending response for rqstp=%p\n", rqstp);
  426. /* Get the RDMA request header. */
  427. rdma_argp = xdr_start(&rqstp->rq_arg);
  428. /* Build an req vec for the XDR */
  429. ctxt = svc_rdma_get_context(rdma);
  430. ctxt->direction = DMA_TO_DEVICE;
  431. vec = svc_rdma_get_req_map();
  432. xdr_to_sge(rdma, &rqstp->rq_res, vec);
  433. inline_bytes = rqstp->rq_res.len;
  434. /* Create the RDMA response header */
  435. res_page = svc_rdma_get_page();
  436. rdma_resp = page_address(res_page);
  437. reply_ary = svc_rdma_get_reply_array(rdma_argp);
  438. if (reply_ary)
  439. reply_type = RDMA_NOMSG;
  440. else
  441. reply_type = RDMA_MSG;
  442. svc_rdma_xdr_encode_reply_header(rdma, rdma_argp,
  443. rdma_resp, reply_type);
  444. /* Send any write-chunk data and build resp write-list */
  445. ret = send_write_chunks(rdma, rdma_argp, rdma_resp,
  446. rqstp, vec);
  447. if (ret < 0) {
  448. printk(KERN_ERR "svcrdma: failed to send write chunks, rc=%d\n",
  449. ret);
  450. goto error;
  451. }
  452. inline_bytes -= ret;
  453. /* Send any reply-list data and update resp reply-list */
  454. ret = send_reply_chunks(rdma, rdma_argp, rdma_resp,
  455. rqstp, vec);
  456. if (ret < 0) {
  457. printk(KERN_ERR "svcrdma: failed to send reply chunks, rc=%d\n",
  458. ret);
  459. goto error;
  460. }
  461. inline_bytes -= ret;
  462. ret = send_reply(rdma, rqstp, res_page, rdma_resp, ctxt, vec,
  463. inline_bytes);
  464. svc_rdma_put_req_map(vec);
  465. dprintk("svcrdma: send_reply returns %d\n", ret);
  466. return ret;
  467. error:
  468. svc_rdma_put_req_map(vec);
  469. svc_rdma_put_context(ctxt, 0);
  470. put_page(res_page);
  471. return ret;
  472. }