svc_rdma_sendto.c 19 KB

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