svc_rdma_sendto.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
  3. * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the BSD-type
  9. * license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials provided
  21. * with the distribution.
  22. *
  23. * Neither the name of the Network Appliance, Inc. nor the names of
  24. * its contributors may be used to endorse or promote products
  25. * derived from this software without specific prior written
  26. * permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  31. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  34. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  35. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  36. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * Author: Tom Tucker <tom@opengridcomputing.com>
  41. */
  42. #include <linux/sunrpc/debug.h>
  43. #include <linux/sunrpc/rpc_rdma.h>
  44. #include <linux/spinlock.h>
  45. #include <asm/unaligned.h>
  46. #include <rdma/ib_verbs.h>
  47. #include <rdma/rdma_cm.h>
  48. #include <linux/sunrpc/svc_rdma.h>
  49. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  50. int svc_rdma_map_xdr(struct svcxprt_rdma *xprt,
  51. struct xdr_buf *xdr,
  52. struct svc_rdma_req_map *vec)
  53. {
  54. int sge_no;
  55. u32 sge_bytes;
  56. u32 page_bytes;
  57. u32 page_off;
  58. int page_no;
  59. if (xdr->len !=
  60. (xdr->head[0].iov_len + xdr->page_len + xdr->tail[0].iov_len)) {
  61. pr_err("svcrdma: %s: XDR buffer length error\n", __func__);
  62. return -EIO;
  63. }
  64. /* Skip the first sge, this is for the RPCRDMA header */
  65. sge_no = 1;
  66. /* Head SGE */
  67. vec->sge[sge_no].iov_base = xdr->head[0].iov_base;
  68. vec->sge[sge_no].iov_len = xdr->head[0].iov_len;
  69. sge_no++;
  70. /* pages SGE */
  71. page_no = 0;
  72. page_bytes = xdr->page_len;
  73. page_off = xdr->page_base;
  74. while (page_bytes) {
  75. vec->sge[sge_no].iov_base =
  76. page_address(xdr->pages[page_no]) + page_off;
  77. sge_bytes = min_t(u32, page_bytes, (PAGE_SIZE - page_off));
  78. page_bytes -= sge_bytes;
  79. vec->sge[sge_no].iov_len = sge_bytes;
  80. sge_no++;
  81. page_no++;
  82. page_off = 0; /* reset for next time through loop */
  83. }
  84. /* Tail SGE */
  85. if (xdr->tail[0].iov_len) {
  86. vec->sge[sge_no].iov_base = xdr->tail[0].iov_base;
  87. vec->sge[sge_no].iov_len = xdr->tail[0].iov_len;
  88. sge_no++;
  89. }
  90. dprintk("svcrdma: %s: sge_no %d page_no %d "
  91. "page_base %u page_len %u head_len %zu tail_len %zu\n",
  92. __func__, sge_no, page_no, xdr->page_base, xdr->page_len,
  93. xdr->head[0].iov_len, xdr->tail[0].iov_len);
  94. vec->count = sge_no;
  95. return 0;
  96. }
  97. static dma_addr_t dma_map_xdr(struct svcxprt_rdma *xprt,
  98. struct xdr_buf *xdr,
  99. u32 xdr_off, size_t len, int dir)
  100. {
  101. struct page *page;
  102. dma_addr_t dma_addr;
  103. if (xdr_off < xdr->head[0].iov_len) {
  104. /* This offset is in the head */
  105. xdr_off += (unsigned long)xdr->head[0].iov_base & ~PAGE_MASK;
  106. page = virt_to_page(xdr->head[0].iov_base);
  107. } else {
  108. xdr_off -= xdr->head[0].iov_len;
  109. if (xdr_off < xdr->page_len) {
  110. /* This offset is in the page list */
  111. xdr_off += xdr->page_base;
  112. page = xdr->pages[xdr_off >> PAGE_SHIFT];
  113. xdr_off &= ~PAGE_MASK;
  114. } else {
  115. /* This offset is in the tail */
  116. xdr_off -= xdr->page_len;
  117. xdr_off += (unsigned long)
  118. xdr->tail[0].iov_base & ~PAGE_MASK;
  119. page = virt_to_page(xdr->tail[0].iov_base);
  120. }
  121. }
  122. dma_addr = ib_dma_map_page(xprt->sc_cm_id->device, page, xdr_off,
  123. min_t(size_t, PAGE_SIZE, len), dir);
  124. return dma_addr;
  125. }
  126. /* Returns the address of the first read chunk or <nul> if no read chunk
  127. * is present
  128. */
  129. struct rpcrdma_read_chunk *
  130. svc_rdma_get_read_chunk(struct rpcrdma_msg *rmsgp)
  131. {
  132. struct rpcrdma_read_chunk *ch =
  133. (struct rpcrdma_read_chunk *)&rmsgp->rm_body.rm_chunks[0];
  134. if (ch->rc_discrim == xdr_zero)
  135. return NULL;
  136. return ch;
  137. }
  138. /* Returns the address of the first read write array element or <nul>
  139. * if no write array list is present
  140. */
  141. static struct rpcrdma_write_array *
  142. svc_rdma_get_write_array(struct rpcrdma_msg *rmsgp)
  143. {
  144. if (rmsgp->rm_body.rm_chunks[0] != xdr_zero ||
  145. rmsgp->rm_body.rm_chunks[1] == xdr_zero)
  146. return NULL;
  147. return (struct rpcrdma_write_array *)&rmsgp->rm_body.rm_chunks[1];
  148. }
  149. /* Returns the address of the first reply array element or <nul> if no
  150. * reply array is present
  151. */
  152. static struct rpcrdma_write_array *
  153. svc_rdma_get_reply_array(struct rpcrdma_msg *rmsgp)
  154. {
  155. struct rpcrdma_read_chunk *rch;
  156. struct rpcrdma_write_array *wr_ary;
  157. struct rpcrdma_write_array *rp_ary;
  158. /* XXX: Need to fix when reply chunk may occur with read list
  159. * and/or write list.
  160. */
  161. if (rmsgp->rm_body.rm_chunks[0] != xdr_zero ||
  162. rmsgp->rm_body.rm_chunks[1] != xdr_zero)
  163. return NULL;
  164. rch = svc_rdma_get_read_chunk(rmsgp);
  165. if (rch) {
  166. while (rch->rc_discrim != xdr_zero)
  167. rch++;
  168. /* The reply chunk follows an empty write array located
  169. * at 'rc_position' here. The reply array is at rc_target.
  170. */
  171. rp_ary = (struct rpcrdma_write_array *)&rch->rc_target;
  172. goto found_it;
  173. }
  174. wr_ary = svc_rdma_get_write_array(rmsgp);
  175. if (wr_ary) {
  176. int chunk = be32_to_cpu(wr_ary->wc_nchunks);
  177. rp_ary = (struct rpcrdma_write_array *)
  178. &wr_ary->wc_array[chunk].wc_target.rs_length;
  179. goto found_it;
  180. }
  181. /* No read list, no write list */
  182. rp_ary = (struct rpcrdma_write_array *)&rmsgp->rm_body.rm_chunks[2];
  183. found_it:
  184. if (rp_ary->wc_discrim == xdr_zero)
  185. return NULL;
  186. return rp_ary;
  187. }
  188. /* Assumptions:
  189. * - The specified write_len can be represented in sc_max_sge * PAGE_SIZE
  190. */
  191. static int send_write(struct svcxprt_rdma *xprt, struct svc_rqst *rqstp,
  192. u32 rmr, u64 to,
  193. u32 xdr_off, int write_len,
  194. struct svc_rdma_req_map *vec)
  195. {
  196. struct ib_rdma_wr write_wr;
  197. struct ib_sge *sge;
  198. int xdr_sge_no;
  199. int sge_no;
  200. int sge_bytes;
  201. int sge_off;
  202. int bc;
  203. struct svc_rdma_op_ctxt *ctxt;
  204. if (vec->count > RPCSVC_MAXPAGES) {
  205. pr_err("svcrdma: Too many pages (%lu)\n", vec->count);
  206. return -EIO;
  207. }
  208. dprintk("svcrdma: RDMA_WRITE rmr=%x, to=%llx, xdr_off=%d, "
  209. "write_len=%d, vec->sge=%p, vec->count=%lu\n",
  210. rmr, (unsigned long long)to, xdr_off,
  211. write_len, vec->sge, vec->count);
  212. ctxt = svc_rdma_get_context(xprt);
  213. ctxt->direction = DMA_TO_DEVICE;
  214. sge = ctxt->sge;
  215. /* Find the SGE associated with xdr_off */
  216. for (bc = xdr_off, xdr_sge_no = 1; bc && xdr_sge_no < vec->count;
  217. xdr_sge_no++) {
  218. if (vec->sge[xdr_sge_no].iov_len > bc)
  219. break;
  220. bc -= vec->sge[xdr_sge_no].iov_len;
  221. }
  222. sge_off = bc;
  223. bc = write_len;
  224. sge_no = 0;
  225. /* Copy the remaining SGE */
  226. while (bc != 0) {
  227. sge_bytes = min_t(size_t,
  228. bc, vec->sge[xdr_sge_no].iov_len-sge_off);
  229. sge[sge_no].length = sge_bytes;
  230. sge[sge_no].addr =
  231. dma_map_xdr(xprt, &rqstp->rq_res, xdr_off,
  232. sge_bytes, DMA_TO_DEVICE);
  233. xdr_off += sge_bytes;
  234. if (ib_dma_mapping_error(xprt->sc_cm_id->device,
  235. sge[sge_no].addr))
  236. goto err;
  237. atomic_inc(&xprt->sc_dma_used);
  238. sge[sge_no].lkey = xprt->sc_pd->local_dma_lkey;
  239. ctxt->count++;
  240. sge_off = 0;
  241. sge_no++;
  242. xdr_sge_no++;
  243. if (xdr_sge_no > vec->count) {
  244. pr_err("svcrdma: Too many sges (%d)\n", xdr_sge_no);
  245. goto err;
  246. }
  247. bc -= sge_bytes;
  248. if (sge_no == xprt->sc_max_sge)
  249. break;
  250. }
  251. /* Prepare WRITE WR */
  252. memset(&write_wr, 0, sizeof write_wr);
  253. ctxt->wr_op = IB_WR_RDMA_WRITE;
  254. write_wr.wr.wr_id = (unsigned long)ctxt;
  255. write_wr.wr.sg_list = &sge[0];
  256. write_wr.wr.num_sge = sge_no;
  257. write_wr.wr.opcode = IB_WR_RDMA_WRITE;
  258. write_wr.wr.send_flags = IB_SEND_SIGNALED;
  259. write_wr.rkey = rmr;
  260. write_wr.remote_addr = to;
  261. /* Post It */
  262. atomic_inc(&rdma_stat_write);
  263. if (svc_rdma_send(xprt, &write_wr.wr))
  264. goto err;
  265. return write_len - bc;
  266. err:
  267. svc_rdma_unmap_dma(ctxt);
  268. svc_rdma_put_context(ctxt, 0);
  269. /* Fatal error, close transport */
  270. return -EIO;
  271. }
  272. static int send_write_chunks(struct svcxprt_rdma *xprt,
  273. struct rpcrdma_msg *rdma_argp,
  274. struct rpcrdma_msg *rdma_resp,
  275. struct svc_rqst *rqstp,
  276. struct svc_rdma_req_map *vec)
  277. {
  278. u32 xfer_len = rqstp->rq_res.page_len + rqstp->rq_res.tail[0].iov_len;
  279. int write_len;
  280. u32 xdr_off;
  281. int chunk_off;
  282. int chunk_no;
  283. int nchunks;
  284. struct rpcrdma_write_array *arg_ary;
  285. struct rpcrdma_write_array *res_ary;
  286. int ret;
  287. arg_ary = svc_rdma_get_write_array(rdma_argp);
  288. if (!arg_ary)
  289. return 0;
  290. res_ary = (struct rpcrdma_write_array *)
  291. &rdma_resp->rm_body.rm_chunks[1];
  292. /* Write chunks start at the pagelist */
  293. nchunks = be32_to_cpu(arg_ary->wc_nchunks);
  294. for (xdr_off = rqstp->rq_res.head[0].iov_len, chunk_no = 0;
  295. xfer_len && chunk_no < nchunks;
  296. chunk_no++) {
  297. struct rpcrdma_segment *arg_ch;
  298. u64 rs_offset;
  299. arg_ch = &arg_ary->wc_array[chunk_no].wc_target;
  300. write_len = min(xfer_len, be32_to_cpu(arg_ch->rs_length));
  301. /* Prepare the response chunk given the length actually
  302. * written */
  303. xdr_decode_hyper((__be32 *)&arg_ch->rs_offset, &rs_offset);
  304. svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
  305. arg_ch->rs_handle,
  306. arg_ch->rs_offset,
  307. write_len);
  308. chunk_off = 0;
  309. while (write_len) {
  310. ret = send_write(xprt, rqstp,
  311. be32_to_cpu(arg_ch->rs_handle),
  312. rs_offset + chunk_off,
  313. xdr_off,
  314. write_len,
  315. vec);
  316. if (ret <= 0) {
  317. dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
  318. ret);
  319. return -EIO;
  320. }
  321. chunk_off += ret;
  322. xdr_off += ret;
  323. xfer_len -= ret;
  324. write_len -= ret;
  325. }
  326. }
  327. /* Update the req with the number of chunks actually used */
  328. svc_rdma_xdr_encode_write_list(rdma_resp, chunk_no);
  329. return rqstp->rq_res.page_len + rqstp->rq_res.tail[0].iov_len;
  330. }
  331. static int send_reply_chunks(struct svcxprt_rdma *xprt,
  332. struct rpcrdma_msg *rdma_argp,
  333. struct rpcrdma_msg *rdma_resp,
  334. struct svc_rqst *rqstp,
  335. struct svc_rdma_req_map *vec)
  336. {
  337. u32 xfer_len = rqstp->rq_res.len;
  338. int write_len;
  339. u32 xdr_off;
  340. int chunk_no;
  341. int chunk_off;
  342. int nchunks;
  343. struct rpcrdma_segment *ch;
  344. struct rpcrdma_write_array *arg_ary;
  345. struct rpcrdma_write_array *res_ary;
  346. int ret;
  347. arg_ary = svc_rdma_get_reply_array(rdma_argp);
  348. if (!arg_ary)
  349. return 0;
  350. /* XXX: need to fix when reply lists occur with read-list and or
  351. * write-list */
  352. res_ary = (struct rpcrdma_write_array *)
  353. &rdma_resp->rm_body.rm_chunks[2];
  354. /* xdr offset starts at RPC message */
  355. nchunks = be32_to_cpu(arg_ary->wc_nchunks);
  356. for (xdr_off = 0, chunk_no = 0;
  357. xfer_len && chunk_no < nchunks;
  358. chunk_no++) {
  359. u64 rs_offset;
  360. ch = &arg_ary->wc_array[chunk_no].wc_target;
  361. write_len = min(xfer_len, be32_to_cpu(ch->rs_length));
  362. /* Prepare the reply chunk given the length actually
  363. * written */
  364. xdr_decode_hyper((__be32 *)&ch->rs_offset, &rs_offset);
  365. svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
  366. ch->rs_handle, ch->rs_offset,
  367. write_len);
  368. chunk_off = 0;
  369. while (write_len) {
  370. ret = send_write(xprt, rqstp,
  371. be32_to_cpu(ch->rs_handle),
  372. rs_offset + chunk_off,
  373. xdr_off,
  374. write_len,
  375. vec);
  376. if (ret <= 0) {
  377. dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
  378. ret);
  379. return -EIO;
  380. }
  381. chunk_off += ret;
  382. xdr_off += ret;
  383. xfer_len -= ret;
  384. write_len -= ret;
  385. }
  386. }
  387. /* Update the req with the number of chunks actually used */
  388. svc_rdma_xdr_encode_reply_array(res_ary, chunk_no);
  389. return rqstp->rq_res.len;
  390. }
  391. /* This function prepares the portion of the RPCRDMA message to be
  392. * sent in the RDMA_SEND. This function is called after data sent via
  393. * RDMA has already been transmitted. There are three cases:
  394. * - The RPCRDMA header, RPC header, and payload are all sent in a
  395. * single RDMA_SEND. This is the "inline" case.
  396. * - The RPCRDMA header and some portion of the RPC header and data
  397. * are sent via this RDMA_SEND and another portion of the data is
  398. * sent via RDMA.
  399. * - The RPCRDMA header [NOMSG] is sent in this RDMA_SEND and the RPC
  400. * header and data are all transmitted via RDMA.
  401. * In all three cases, this function prepares the RPCRDMA header in
  402. * sge[0], the 'type' parameter indicates the type to place in the
  403. * RPCRDMA header, and the 'byte_count' field indicates how much of
  404. * the XDR to include in this RDMA_SEND. NB: The offset of the payload
  405. * to send is zero in the XDR.
  406. */
  407. static int send_reply(struct svcxprt_rdma *rdma,
  408. struct svc_rqst *rqstp,
  409. struct page *page,
  410. struct rpcrdma_msg *rdma_resp,
  411. struct svc_rdma_op_ctxt *ctxt,
  412. struct svc_rdma_req_map *vec,
  413. int byte_count)
  414. {
  415. struct ib_send_wr send_wr;
  416. u32 xdr_off;
  417. int sge_no;
  418. int sge_bytes;
  419. int page_no;
  420. int pages;
  421. int ret;
  422. /* Post a recv buffer to handle another request. */
  423. ret = svc_rdma_post_recv(rdma, GFP_KERNEL);
  424. if (ret) {
  425. printk(KERN_INFO
  426. "svcrdma: could not post a receive buffer, err=%d."
  427. "Closing transport %p.\n", ret, rdma);
  428. set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
  429. svc_rdma_put_context(ctxt, 0);
  430. return -ENOTCONN;
  431. }
  432. /* Prepare the context */
  433. ctxt->pages[0] = page;
  434. ctxt->count = 1;
  435. /* Prepare the SGE for the RPCRDMA Header */
  436. ctxt->sge[0].lkey = rdma->sc_pd->local_dma_lkey;
  437. ctxt->sge[0].length = svc_rdma_xdr_get_reply_hdr_len(rdma_resp);
  438. ctxt->sge[0].addr =
  439. ib_dma_map_page(rdma->sc_cm_id->device, page, 0,
  440. ctxt->sge[0].length, DMA_TO_DEVICE);
  441. if (ib_dma_mapping_error(rdma->sc_cm_id->device, ctxt->sge[0].addr))
  442. goto err;
  443. atomic_inc(&rdma->sc_dma_used);
  444. ctxt->direction = DMA_TO_DEVICE;
  445. /* Map the payload indicated by 'byte_count' */
  446. xdr_off = 0;
  447. for (sge_no = 1; byte_count && sge_no < vec->count; sge_no++) {
  448. sge_bytes = min_t(size_t, vec->sge[sge_no].iov_len, byte_count);
  449. byte_count -= sge_bytes;
  450. ctxt->sge[sge_no].addr =
  451. dma_map_xdr(rdma, &rqstp->rq_res, xdr_off,
  452. sge_bytes, DMA_TO_DEVICE);
  453. xdr_off += sge_bytes;
  454. if (ib_dma_mapping_error(rdma->sc_cm_id->device,
  455. ctxt->sge[sge_no].addr))
  456. goto err;
  457. atomic_inc(&rdma->sc_dma_used);
  458. ctxt->sge[sge_no].lkey = rdma->sc_pd->local_dma_lkey;
  459. ctxt->sge[sge_no].length = sge_bytes;
  460. }
  461. if (byte_count != 0) {
  462. pr_err("svcrdma: Could not map %d bytes\n", byte_count);
  463. goto err;
  464. }
  465. /* Save all respages in the ctxt and remove them from the
  466. * respages array. They are our pages until the I/O
  467. * completes.
  468. */
  469. pages = rqstp->rq_next_page - rqstp->rq_respages;
  470. for (page_no = 0; page_no < pages; page_no++) {
  471. ctxt->pages[page_no+1] = rqstp->rq_respages[page_no];
  472. ctxt->count++;
  473. rqstp->rq_respages[page_no] = NULL;
  474. /*
  475. * If there are more pages than SGE, terminate SGE
  476. * list so that svc_rdma_unmap_dma doesn't attempt to
  477. * unmap garbage.
  478. */
  479. if (page_no+1 >= sge_no)
  480. ctxt->sge[page_no+1].length = 0;
  481. }
  482. rqstp->rq_next_page = rqstp->rq_respages + 1;
  483. /* The loop above bumps sc_dma_used for each sge. The
  484. * xdr_buf.tail gets a separate sge, but resides in the
  485. * same page as xdr_buf.head. Don't count it twice.
  486. */
  487. if (sge_no > ctxt->count)
  488. atomic_dec(&rdma->sc_dma_used);
  489. if (sge_no > rdma->sc_max_sge) {
  490. pr_err("svcrdma: Too many sges (%d)\n", sge_no);
  491. goto err;
  492. }
  493. memset(&send_wr, 0, sizeof send_wr);
  494. ctxt->wr_op = IB_WR_SEND;
  495. send_wr.wr_id = (unsigned long)ctxt;
  496. send_wr.sg_list = ctxt->sge;
  497. send_wr.num_sge = sge_no;
  498. send_wr.opcode = IB_WR_SEND;
  499. send_wr.send_flags = IB_SEND_SIGNALED;
  500. ret = svc_rdma_send(rdma, &send_wr);
  501. if (ret)
  502. goto err;
  503. return 0;
  504. err:
  505. svc_rdma_unmap_dma(ctxt);
  506. svc_rdma_put_context(ctxt, 1);
  507. return -EIO;
  508. }
  509. void svc_rdma_prep_reply_hdr(struct svc_rqst *rqstp)
  510. {
  511. }
  512. int svc_rdma_sendto(struct svc_rqst *rqstp)
  513. {
  514. struct svc_xprt *xprt = rqstp->rq_xprt;
  515. struct svcxprt_rdma *rdma =
  516. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  517. struct rpcrdma_msg *rdma_argp;
  518. struct rpcrdma_msg *rdma_resp;
  519. struct rpcrdma_write_array *reply_ary;
  520. enum rpcrdma_proc reply_type;
  521. int ret;
  522. int inline_bytes;
  523. struct page *res_page;
  524. struct svc_rdma_op_ctxt *ctxt;
  525. struct svc_rdma_req_map *vec;
  526. dprintk("svcrdma: sending response for rqstp=%p\n", rqstp);
  527. /* Get the RDMA request header. The receive logic always
  528. * places this at the start of page 0.
  529. */
  530. rdma_argp = page_address(rqstp->rq_pages[0]);
  531. /* Build an req vec for the XDR */
  532. ctxt = svc_rdma_get_context(rdma);
  533. ctxt->direction = DMA_TO_DEVICE;
  534. vec = svc_rdma_get_req_map(rdma);
  535. ret = svc_rdma_map_xdr(rdma, &rqstp->rq_res, vec);
  536. if (ret)
  537. goto err0;
  538. inline_bytes = rqstp->rq_res.len;
  539. /* Create the RDMA response header */
  540. ret = -ENOMEM;
  541. res_page = alloc_page(GFP_KERNEL);
  542. if (!res_page)
  543. goto err0;
  544. rdma_resp = page_address(res_page);
  545. reply_ary = svc_rdma_get_reply_array(rdma_argp);
  546. if (reply_ary)
  547. reply_type = RDMA_NOMSG;
  548. else
  549. reply_type = RDMA_MSG;
  550. svc_rdma_xdr_encode_reply_header(rdma, rdma_argp,
  551. rdma_resp, reply_type);
  552. /* Send any write-chunk data and build resp write-list */
  553. ret = send_write_chunks(rdma, rdma_argp, rdma_resp,
  554. rqstp, vec);
  555. if (ret < 0) {
  556. printk(KERN_ERR "svcrdma: failed to send write chunks, rc=%d\n",
  557. ret);
  558. goto err1;
  559. }
  560. inline_bytes -= ret;
  561. /* Send any reply-list data and update resp reply-list */
  562. ret = send_reply_chunks(rdma, rdma_argp, rdma_resp,
  563. rqstp, vec);
  564. if (ret < 0) {
  565. printk(KERN_ERR "svcrdma: failed to send reply chunks, rc=%d\n",
  566. ret);
  567. goto err1;
  568. }
  569. inline_bytes -= ret;
  570. ret = send_reply(rdma, rqstp, res_page, rdma_resp, ctxt, vec,
  571. inline_bytes);
  572. svc_rdma_put_req_map(rdma, vec);
  573. dprintk("svcrdma: send_reply returns %d\n", ret);
  574. return ret;
  575. err1:
  576. put_page(res_page);
  577. err0:
  578. svc_rdma_put_req_map(rdma, vec);
  579. svc_rdma_put_context(ctxt, 0);
  580. return ret;
  581. }