rpc_rdma.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * Copyright (c) 2003-2007 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. /*
  40. * rpc_rdma.c
  41. *
  42. * This file contains the guts of the RPC RDMA protocol, and
  43. * does marshaling/unmarshaling, etc. It is also where interfacing
  44. * to the Linux RPC framework lives.
  45. */
  46. #include "xprt_rdma.h"
  47. #include <linux/highmem.h>
  48. #ifdef RPC_DEBUG
  49. # define RPCDBG_FACILITY RPCDBG_TRANS
  50. #endif
  51. enum rpcrdma_chunktype {
  52. rpcrdma_noch = 0,
  53. rpcrdma_readch,
  54. rpcrdma_areadch,
  55. rpcrdma_writech,
  56. rpcrdma_replych
  57. };
  58. #ifdef RPC_DEBUG
  59. static const char transfertypes[][12] = {
  60. "pure inline", /* no chunks */
  61. " read chunk", /* some argument via rdma read */
  62. "*read chunk", /* entire request via rdma read */
  63. "write chunk", /* some result via rdma write */
  64. "reply chunk" /* entire reply via rdma write */
  65. };
  66. #endif
  67. /*
  68. * Chunk assembly from upper layer xdr_buf.
  69. *
  70. * Prepare the passed-in xdr_buf into representation as RPC/RDMA chunk
  71. * elements. Segments are then coalesced when registered, if possible
  72. * within the selected memreg mode.
  73. *
  74. * Note, this routine is never called if the connection's memory
  75. * registration strategy is 0 (bounce buffers).
  76. */
  77. static int
  78. rpcrdma_convert_iovs(struct xdr_buf *xdrbuf, unsigned int pos,
  79. enum rpcrdma_chunktype type, struct rpcrdma_mr_seg *seg, int nsegs)
  80. {
  81. int len, n = 0, p;
  82. if (pos == 0 && xdrbuf->head[0].iov_len) {
  83. seg[n].mr_page = NULL;
  84. seg[n].mr_offset = xdrbuf->head[0].iov_base;
  85. seg[n].mr_len = xdrbuf->head[0].iov_len;
  86. ++n;
  87. }
  88. if (xdrbuf->page_len && (xdrbuf->pages[0] != NULL)) {
  89. if (n == nsegs)
  90. return 0;
  91. seg[n].mr_page = xdrbuf->pages[0];
  92. seg[n].mr_offset = (void *)(unsigned long) xdrbuf->page_base;
  93. seg[n].mr_len = min_t(u32,
  94. PAGE_SIZE - xdrbuf->page_base, xdrbuf->page_len);
  95. len = xdrbuf->page_len - seg[n].mr_len;
  96. ++n;
  97. p = 1;
  98. while (len > 0) {
  99. if (n == nsegs)
  100. return 0;
  101. seg[n].mr_page = xdrbuf->pages[p];
  102. seg[n].mr_offset = NULL;
  103. seg[n].mr_len = min_t(u32, PAGE_SIZE, len);
  104. len -= seg[n].mr_len;
  105. ++n;
  106. ++p;
  107. }
  108. }
  109. if (xdrbuf->tail[0].iov_len) {
  110. /* the rpcrdma protocol allows us to omit any trailing
  111. * xdr pad bytes, saving the server an RDMA operation. */
  112. if (xdrbuf->tail[0].iov_len < 4 && xprt_rdma_pad_optimize)
  113. return n;
  114. if (n == nsegs)
  115. return 0;
  116. seg[n].mr_page = NULL;
  117. seg[n].mr_offset = xdrbuf->tail[0].iov_base;
  118. seg[n].mr_len = xdrbuf->tail[0].iov_len;
  119. ++n;
  120. }
  121. return n;
  122. }
  123. /*
  124. * Create read/write chunk lists, and reply chunks, for RDMA
  125. *
  126. * Assume check against THRESHOLD has been done, and chunks are required.
  127. * Assume only encoding one list entry for read|write chunks. The NFSv3
  128. * protocol is simple enough to allow this as it only has a single "bulk
  129. * result" in each procedure - complicated NFSv4 COMPOUNDs are not. (The
  130. * RDMA/Sessions NFSv4 proposal addresses this for future v4 revs.)
  131. *
  132. * When used for a single reply chunk (which is a special write
  133. * chunk used for the entire reply, rather than just the data), it
  134. * is used primarily for READDIR and READLINK which would otherwise
  135. * be severely size-limited by a small rdma inline read max. The server
  136. * response will come back as an RDMA Write, followed by a message
  137. * of type RDMA_NOMSG carrying the xid and length. As a result, reply
  138. * chunks do not provide data alignment, however they do not require
  139. * "fixup" (moving the response to the upper layer buffer) either.
  140. *
  141. * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
  142. *
  143. * Read chunklist (a linked list):
  144. * N elements, position P (same P for all chunks of same arg!):
  145. * 1 - PHLOO - 1 - PHLOO - ... - 1 - PHLOO - 0
  146. *
  147. * Write chunklist (a list of (one) counted array):
  148. * N elements:
  149. * 1 - N - HLOO - HLOO - ... - HLOO - 0
  150. *
  151. * Reply chunk (a counted array):
  152. * N elements:
  153. * 1 - N - HLOO - HLOO - ... - HLOO
  154. */
  155. static unsigned int
  156. rpcrdma_create_chunks(struct rpc_rqst *rqst, struct xdr_buf *target,
  157. struct rpcrdma_msg *headerp, enum rpcrdma_chunktype type)
  158. {
  159. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  160. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_task->tk_xprt);
  161. int nsegs, nchunks = 0;
  162. unsigned int pos;
  163. struct rpcrdma_mr_seg *seg = req->rl_segments;
  164. struct rpcrdma_read_chunk *cur_rchunk = NULL;
  165. struct rpcrdma_write_array *warray = NULL;
  166. struct rpcrdma_write_chunk *cur_wchunk = NULL;
  167. __be32 *iptr = headerp->rm_body.rm_chunks;
  168. if (type == rpcrdma_readch || type == rpcrdma_areadch) {
  169. /* a read chunk - server will RDMA Read our memory */
  170. cur_rchunk = (struct rpcrdma_read_chunk *) iptr;
  171. } else {
  172. /* a write or reply chunk - server will RDMA Write our memory */
  173. *iptr++ = xdr_zero; /* encode a NULL read chunk list */
  174. if (type == rpcrdma_replych)
  175. *iptr++ = xdr_zero; /* a NULL write chunk list */
  176. warray = (struct rpcrdma_write_array *) iptr;
  177. cur_wchunk = (struct rpcrdma_write_chunk *) (warray + 1);
  178. }
  179. if (type == rpcrdma_replych || type == rpcrdma_areadch)
  180. pos = 0;
  181. else
  182. pos = target->head[0].iov_len;
  183. nsegs = rpcrdma_convert_iovs(target, pos, type, seg, RPCRDMA_MAX_SEGS);
  184. if (nsegs == 0)
  185. return 0;
  186. do {
  187. /* bind/register the memory, then build chunk from result. */
  188. int n = rpcrdma_register_external(seg, nsegs,
  189. cur_wchunk != NULL, r_xprt);
  190. if (n <= 0)
  191. goto out;
  192. if (cur_rchunk) { /* read */
  193. cur_rchunk->rc_discrim = xdr_one;
  194. /* all read chunks have the same "position" */
  195. cur_rchunk->rc_position = htonl(pos);
  196. cur_rchunk->rc_target.rs_handle = htonl(seg->mr_rkey);
  197. cur_rchunk->rc_target.rs_length = htonl(seg->mr_len);
  198. xdr_encode_hyper(
  199. (__be32 *)&cur_rchunk->rc_target.rs_offset,
  200. seg->mr_base);
  201. dprintk("RPC: %s: read chunk "
  202. "elem %d@0x%llx:0x%x pos %u (%s)\n", __func__,
  203. seg->mr_len, (unsigned long long)seg->mr_base,
  204. seg->mr_rkey, pos, n < nsegs ? "more" : "last");
  205. cur_rchunk++;
  206. r_xprt->rx_stats.read_chunk_count++;
  207. } else { /* write/reply */
  208. cur_wchunk->wc_target.rs_handle = htonl(seg->mr_rkey);
  209. cur_wchunk->wc_target.rs_length = htonl(seg->mr_len);
  210. xdr_encode_hyper(
  211. (__be32 *)&cur_wchunk->wc_target.rs_offset,
  212. seg->mr_base);
  213. dprintk("RPC: %s: %s chunk "
  214. "elem %d@0x%llx:0x%x (%s)\n", __func__,
  215. (type == rpcrdma_replych) ? "reply" : "write",
  216. seg->mr_len, (unsigned long long)seg->mr_base,
  217. seg->mr_rkey, n < nsegs ? "more" : "last");
  218. cur_wchunk++;
  219. if (type == rpcrdma_replych)
  220. r_xprt->rx_stats.reply_chunk_count++;
  221. else
  222. r_xprt->rx_stats.write_chunk_count++;
  223. r_xprt->rx_stats.total_rdma_request += seg->mr_len;
  224. }
  225. nchunks++;
  226. seg += n;
  227. nsegs -= n;
  228. } while (nsegs);
  229. /* success. all failures return above */
  230. req->rl_nchunks = nchunks;
  231. BUG_ON(nchunks == 0);
  232. BUG_ON((r_xprt->rx_ia.ri_memreg_strategy == RPCRDMA_FRMR)
  233. && (nchunks > 3));
  234. /*
  235. * finish off header. If write, marshal discrim and nchunks.
  236. */
  237. if (cur_rchunk) {
  238. iptr = (__be32 *) cur_rchunk;
  239. *iptr++ = xdr_zero; /* finish the read chunk list */
  240. *iptr++ = xdr_zero; /* encode a NULL write chunk list */
  241. *iptr++ = xdr_zero; /* encode a NULL reply chunk */
  242. } else {
  243. warray->wc_discrim = xdr_one;
  244. warray->wc_nchunks = htonl(nchunks);
  245. iptr = (__be32 *) cur_wchunk;
  246. if (type == rpcrdma_writech) {
  247. *iptr++ = xdr_zero; /* finish the write chunk list */
  248. *iptr++ = xdr_zero; /* encode a NULL reply chunk */
  249. }
  250. }
  251. /*
  252. * Return header size.
  253. */
  254. return (unsigned char *)iptr - (unsigned char *)headerp;
  255. out:
  256. for (pos = 0; nchunks--;)
  257. pos += rpcrdma_deregister_external(
  258. &req->rl_segments[pos], r_xprt, NULL);
  259. return 0;
  260. }
  261. /*
  262. * Copy write data inline.
  263. * This function is used for "small" requests. Data which is passed
  264. * to RPC via iovecs (or page list) is copied directly into the
  265. * pre-registered memory buffer for this request. For small amounts
  266. * of data, this is efficient. The cutoff value is tunable.
  267. */
  268. static int
  269. rpcrdma_inline_pullup(struct rpc_rqst *rqst, int pad)
  270. {
  271. int i, npages, curlen;
  272. int copy_len;
  273. unsigned char *srcp, *destp;
  274. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  275. destp = rqst->rq_svec[0].iov_base;
  276. curlen = rqst->rq_svec[0].iov_len;
  277. destp += curlen;
  278. /*
  279. * Do optional padding where it makes sense. Alignment of write
  280. * payload can help the server, if our setting is accurate.
  281. */
  282. pad -= (curlen + 36/*sizeof(struct rpcrdma_msg_padded)*/);
  283. if (pad < 0 || rqst->rq_slen - curlen < RPCRDMA_INLINE_PAD_THRESH)
  284. pad = 0; /* don't pad this request */
  285. dprintk("RPC: %s: pad %d destp 0x%p len %d hdrlen %d\n",
  286. __func__, pad, destp, rqst->rq_slen, curlen);
  287. copy_len = rqst->rq_snd_buf.page_len;
  288. if (rqst->rq_snd_buf.tail[0].iov_len) {
  289. curlen = rqst->rq_snd_buf.tail[0].iov_len;
  290. if (destp + copy_len != rqst->rq_snd_buf.tail[0].iov_base) {
  291. memmove(destp + copy_len,
  292. rqst->rq_snd_buf.tail[0].iov_base, curlen);
  293. r_xprt->rx_stats.pullup_copy_count += curlen;
  294. }
  295. dprintk("RPC: %s: tail destp 0x%p len %d\n",
  296. __func__, destp + copy_len, curlen);
  297. rqst->rq_svec[0].iov_len += curlen;
  298. }
  299. r_xprt->rx_stats.pullup_copy_count += copy_len;
  300. npages = PAGE_ALIGN(rqst->rq_snd_buf.page_base+copy_len) >> PAGE_SHIFT;
  301. for (i = 0; copy_len && i < npages; i++) {
  302. if (i == 0)
  303. curlen = PAGE_SIZE - rqst->rq_snd_buf.page_base;
  304. else
  305. curlen = PAGE_SIZE;
  306. if (curlen > copy_len)
  307. curlen = copy_len;
  308. dprintk("RPC: %s: page %d destp 0x%p len %d curlen %d\n",
  309. __func__, i, destp, copy_len, curlen);
  310. srcp = kmap_atomic(rqst->rq_snd_buf.pages[i],
  311. KM_SKB_SUNRPC_DATA);
  312. if (i == 0)
  313. memcpy(destp, srcp+rqst->rq_snd_buf.page_base, curlen);
  314. else
  315. memcpy(destp, srcp, curlen);
  316. kunmap_atomic(srcp, KM_SKB_SUNRPC_DATA);
  317. rqst->rq_svec[0].iov_len += curlen;
  318. destp += curlen;
  319. copy_len -= curlen;
  320. }
  321. /* header now contains entire send message */
  322. return pad;
  323. }
  324. /*
  325. * Marshal a request: the primary job of this routine is to choose
  326. * the transfer modes. See comments below.
  327. *
  328. * Uses multiple RDMA IOVs for a request:
  329. * [0] -- RPC RDMA header, which uses memory from the *start* of the
  330. * preregistered buffer that already holds the RPC data in
  331. * its middle.
  332. * [1] -- the RPC header/data, marshaled by RPC and the NFS protocol.
  333. * [2] -- optional padding.
  334. * [3] -- if padded, header only in [1] and data here.
  335. */
  336. int
  337. rpcrdma_marshal_req(struct rpc_rqst *rqst)
  338. {
  339. struct rpc_xprt *xprt = rqst->rq_task->tk_xprt;
  340. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  341. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  342. char *base;
  343. size_t hdrlen, rpclen, padlen;
  344. enum rpcrdma_chunktype rtype, wtype;
  345. struct rpcrdma_msg *headerp;
  346. /*
  347. * rpclen gets amount of data in first buffer, which is the
  348. * pre-registered buffer.
  349. */
  350. base = rqst->rq_svec[0].iov_base;
  351. rpclen = rqst->rq_svec[0].iov_len;
  352. /* build RDMA header in private area at front */
  353. headerp = (struct rpcrdma_msg *) req->rl_base;
  354. /* don't htonl XID, it's already done in request */
  355. headerp->rm_xid = rqst->rq_xid;
  356. headerp->rm_vers = xdr_one;
  357. headerp->rm_credit = htonl(r_xprt->rx_buf.rb_max_requests);
  358. headerp->rm_type = htonl(RDMA_MSG);
  359. /*
  360. * Chunks needed for results?
  361. *
  362. * o If the expected result is under the inline threshold, all ops
  363. * return as inline (but see later).
  364. * o Large non-read ops return as a single reply chunk.
  365. * o Large read ops return data as write chunk(s), header as inline.
  366. *
  367. * Note: the NFS code sending down multiple result segments implies
  368. * the op is one of read, readdir[plus], readlink or NFSv4 getacl.
  369. */
  370. /*
  371. * This code can handle read chunks, write chunks OR reply
  372. * chunks -- only one type. If the request is too big to fit
  373. * inline, then we will choose read chunks. If the request is
  374. * a READ, then use write chunks to separate the file data
  375. * into pages; otherwise use reply chunks.
  376. */
  377. if (rqst->rq_rcv_buf.buflen <= RPCRDMA_INLINE_READ_THRESHOLD(rqst))
  378. wtype = rpcrdma_noch;
  379. else if (rqst->rq_rcv_buf.page_len == 0)
  380. wtype = rpcrdma_replych;
  381. else if (rqst->rq_rcv_buf.flags & XDRBUF_READ)
  382. wtype = rpcrdma_writech;
  383. else
  384. wtype = rpcrdma_replych;
  385. /*
  386. * Chunks needed for arguments?
  387. *
  388. * o If the total request is under the inline threshold, all ops
  389. * are sent as inline.
  390. * o Large non-write ops are sent with the entire message as a
  391. * single read chunk (protocol 0-position special case).
  392. * o Large write ops transmit data as read chunk(s), header as
  393. * inline.
  394. *
  395. * Note: the NFS code sending down multiple argument segments
  396. * implies the op is a write.
  397. * TBD check NFSv4 setacl
  398. */
  399. if (rqst->rq_snd_buf.len <= RPCRDMA_INLINE_WRITE_THRESHOLD(rqst))
  400. rtype = rpcrdma_noch;
  401. else if (rqst->rq_snd_buf.page_len == 0)
  402. rtype = rpcrdma_areadch;
  403. else
  404. rtype = rpcrdma_readch;
  405. /* The following simplification is not true forever */
  406. if (rtype != rpcrdma_noch && wtype == rpcrdma_replych)
  407. wtype = rpcrdma_noch;
  408. BUG_ON(rtype != rpcrdma_noch && wtype != rpcrdma_noch);
  409. if (r_xprt->rx_ia.ri_memreg_strategy == RPCRDMA_BOUNCEBUFFERS &&
  410. (rtype != rpcrdma_noch || wtype != rpcrdma_noch)) {
  411. /* forced to "pure inline"? */
  412. dprintk("RPC: %s: too much data (%d/%d) for inline\n",
  413. __func__, rqst->rq_rcv_buf.len, rqst->rq_snd_buf.len);
  414. return -1;
  415. }
  416. hdrlen = 28; /*sizeof *headerp;*/
  417. padlen = 0;
  418. /*
  419. * Pull up any extra send data into the preregistered buffer.
  420. * When padding is in use and applies to the transfer, insert
  421. * it and change the message type.
  422. */
  423. if (rtype == rpcrdma_noch) {
  424. padlen = rpcrdma_inline_pullup(rqst,
  425. RPCRDMA_INLINE_PAD_VALUE(rqst));
  426. if (padlen) {
  427. headerp->rm_type = htonl(RDMA_MSGP);
  428. headerp->rm_body.rm_padded.rm_align =
  429. htonl(RPCRDMA_INLINE_PAD_VALUE(rqst));
  430. headerp->rm_body.rm_padded.rm_thresh =
  431. htonl(RPCRDMA_INLINE_PAD_THRESH);
  432. headerp->rm_body.rm_padded.rm_pempty[0] = xdr_zero;
  433. headerp->rm_body.rm_padded.rm_pempty[1] = xdr_zero;
  434. headerp->rm_body.rm_padded.rm_pempty[2] = xdr_zero;
  435. hdrlen += 2 * sizeof(u32); /* extra words in padhdr */
  436. BUG_ON(wtype != rpcrdma_noch);
  437. } else {
  438. headerp->rm_body.rm_nochunks.rm_empty[0] = xdr_zero;
  439. headerp->rm_body.rm_nochunks.rm_empty[1] = xdr_zero;
  440. headerp->rm_body.rm_nochunks.rm_empty[2] = xdr_zero;
  441. /* new length after pullup */
  442. rpclen = rqst->rq_svec[0].iov_len;
  443. /*
  444. * Currently we try to not actually use read inline.
  445. * Reply chunks have the desirable property that
  446. * they land, packed, directly in the target buffers
  447. * without headers, so they require no fixup. The
  448. * additional RDMA Write op sends the same amount
  449. * of data, streams on-the-wire and adds no overhead
  450. * on receive. Therefore, we request a reply chunk
  451. * for non-writes wherever feasible and efficient.
  452. */
  453. if (wtype == rpcrdma_noch &&
  454. r_xprt->rx_ia.ri_memreg_strategy > RPCRDMA_REGISTER)
  455. wtype = rpcrdma_replych;
  456. }
  457. }
  458. /*
  459. * Marshal chunks. This routine will return the header length
  460. * consumed by marshaling.
  461. */
  462. if (rtype != rpcrdma_noch) {
  463. hdrlen = rpcrdma_create_chunks(rqst,
  464. &rqst->rq_snd_buf, headerp, rtype);
  465. wtype = rtype; /* simplify dprintk */
  466. } else if (wtype != rpcrdma_noch) {
  467. hdrlen = rpcrdma_create_chunks(rqst,
  468. &rqst->rq_rcv_buf, headerp, wtype);
  469. }
  470. if (hdrlen == 0)
  471. return -1;
  472. dprintk("RPC: %s: %s: hdrlen %zd rpclen %zd padlen %zd"
  473. " headerp 0x%p base 0x%p lkey 0x%x\n",
  474. __func__, transfertypes[wtype], hdrlen, rpclen, padlen,
  475. headerp, base, req->rl_iov.lkey);
  476. /*
  477. * initialize send_iov's - normally only two: rdma chunk header and
  478. * single preregistered RPC header buffer, but if padding is present,
  479. * then use a preregistered (and zeroed) pad buffer between the RPC
  480. * header and any write data. In all non-rdma cases, any following
  481. * data has been copied into the RPC header buffer.
  482. */
  483. req->rl_send_iov[0].addr = req->rl_iov.addr;
  484. req->rl_send_iov[0].length = hdrlen;
  485. req->rl_send_iov[0].lkey = req->rl_iov.lkey;
  486. req->rl_send_iov[1].addr = req->rl_iov.addr + (base - req->rl_base);
  487. req->rl_send_iov[1].length = rpclen;
  488. req->rl_send_iov[1].lkey = req->rl_iov.lkey;
  489. req->rl_niovs = 2;
  490. if (padlen) {
  491. struct rpcrdma_ep *ep = &r_xprt->rx_ep;
  492. req->rl_send_iov[2].addr = ep->rep_pad.addr;
  493. req->rl_send_iov[2].length = padlen;
  494. req->rl_send_iov[2].lkey = ep->rep_pad.lkey;
  495. req->rl_send_iov[3].addr = req->rl_send_iov[1].addr + rpclen;
  496. req->rl_send_iov[3].length = rqst->rq_slen - rpclen;
  497. req->rl_send_iov[3].lkey = req->rl_iov.lkey;
  498. req->rl_niovs = 4;
  499. }
  500. return 0;
  501. }
  502. /*
  503. * Chase down a received write or reply chunklist to get length
  504. * RDMA'd by server. See map at rpcrdma_create_chunks()! :-)
  505. */
  506. static int
  507. rpcrdma_count_chunks(struct rpcrdma_rep *rep, unsigned int max, int wrchunk, __be32 **iptrp)
  508. {
  509. unsigned int i, total_len;
  510. struct rpcrdma_write_chunk *cur_wchunk;
  511. i = ntohl(**iptrp); /* get array count */
  512. if (i > max)
  513. return -1;
  514. cur_wchunk = (struct rpcrdma_write_chunk *) (*iptrp + 1);
  515. total_len = 0;
  516. while (i--) {
  517. struct rpcrdma_segment *seg = &cur_wchunk->wc_target;
  518. ifdebug(FACILITY) {
  519. u64 off;
  520. xdr_decode_hyper((__be32 *)&seg->rs_offset, &off);
  521. dprintk("RPC: %s: chunk %d@0x%llx:0x%x\n",
  522. __func__,
  523. ntohl(seg->rs_length),
  524. (unsigned long long)off,
  525. ntohl(seg->rs_handle));
  526. }
  527. total_len += ntohl(seg->rs_length);
  528. ++cur_wchunk;
  529. }
  530. /* check and adjust for properly terminated write chunk */
  531. if (wrchunk) {
  532. __be32 *w = (__be32 *) cur_wchunk;
  533. if (*w++ != xdr_zero)
  534. return -1;
  535. cur_wchunk = (struct rpcrdma_write_chunk *) w;
  536. }
  537. if ((char *) cur_wchunk > rep->rr_base + rep->rr_len)
  538. return -1;
  539. *iptrp = (__be32 *) cur_wchunk;
  540. return total_len;
  541. }
  542. /*
  543. * Scatter inline received data back into provided iov's.
  544. */
  545. static void
  546. rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad)
  547. {
  548. int i, npages, curlen, olen;
  549. char *destp;
  550. curlen = rqst->rq_rcv_buf.head[0].iov_len;
  551. if (curlen > copy_len) { /* write chunk header fixup */
  552. curlen = copy_len;
  553. rqst->rq_rcv_buf.head[0].iov_len = curlen;
  554. }
  555. dprintk("RPC: %s: srcp 0x%p len %d hdrlen %d\n",
  556. __func__, srcp, copy_len, curlen);
  557. /* Shift pointer for first receive segment only */
  558. rqst->rq_rcv_buf.head[0].iov_base = srcp;
  559. srcp += curlen;
  560. copy_len -= curlen;
  561. olen = copy_len;
  562. i = 0;
  563. rpcx_to_rdmax(rqst->rq_xprt)->rx_stats.fixup_copy_count += olen;
  564. if (copy_len && rqst->rq_rcv_buf.page_len) {
  565. npages = PAGE_ALIGN(rqst->rq_rcv_buf.page_base +
  566. rqst->rq_rcv_buf.page_len) >> PAGE_SHIFT;
  567. for (; i < npages; i++) {
  568. if (i == 0)
  569. curlen = PAGE_SIZE - rqst->rq_rcv_buf.page_base;
  570. else
  571. curlen = PAGE_SIZE;
  572. if (curlen > copy_len)
  573. curlen = copy_len;
  574. dprintk("RPC: %s: page %d"
  575. " srcp 0x%p len %d curlen %d\n",
  576. __func__, i, srcp, copy_len, curlen);
  577. destp = kmap_atomic(rqst->rq_rcv_buf.pages[i],
  578. KM_SKB_SUNRPC_DATA);
  579. if (i == 0)
  580. memcpy(destp + rqst->rq_rcv_buf.page_base,
  581. srcp, curlen);
  582. else
  583. memcpy(destp, srcp, curlen);
  584. flush_dcache_page(rqst->rq_rcv_buf.pages[i]);
  585. kunmap_atomic(destp, KM_SKB_SUNRPC_DATA);
  586. srcp += curlen;
  587. copy_len -= curlen;
  588. if (copy_len == 0)
  589. break;
  590. }
  591. rqst->rq_rcv_buf.page_len = olen - copy_len;
  592. } else
  593. rqst->rq_rcv_buf.page_len = 0;
  594. if (copy_len && rqst->rq_rcv_buf.tail[0].iov_len) {
  595. curlen = copy_len;
  596. if (curlen > rqst->rq_rcv_buf.tail[0].iov_len)
  597. curlen = rqst->rq_rcv_buf.tail[0].iov_len;
  598. if (rqst->rq_rcv_buf.tail[0].iov_base != srcp)
  599. memmove(rqst->rq_rcv_buf.tail[0].iov_base, srcp, curlen);
  600. dprintk("RPC: %s: tail srcp 0x%p len %d curlen %d\n",
  601. __func__, srcp, copy_len, curlen);
  602. rqst->rq_rcv_buf.tail[0].iov_len = curlen;
  603. copy_len -= curlen; ++i;
  604. } else
  605. rqst->rq_rcv_buf.tail[0].iov_len = 0;
  606. if (pad) {
  607. /* implicit padding on terminal chunk */
  608. unsigned char *p = rqst->rq_rcv_buf.tail[0].iov_base;
  609. while (pad--)
  610. p[rqst->rq_rcv_buf.tail[0].iov_len++] = 0;
  611. }
  612. if (copy_len)
  613. dprintk("RPC: %s: %d bytes in"
  614. " %d extra segments (%d lost)\n",
  615. __func__, olen, i, copy_len);
  616. /* TBD avoid a warning from call_decode() */
  617. rqst->rq_private_buf = rqst->rq_rcv_buf;
  618. }
  619. /*
  620. * This function is called when an async event is posted to
  621. * the connection which changes the connection state. All it
  622. * does at this point is mark the connection up/down, the rpc
  623. * timers do the rest.
  624. */
  625. void
  626. rpcrdma_conn_func(struct rpcrdma_ep *ep)
  627. {
  628. struct rpc_xprt *xprt = ep->rep_xprt;
  629. spin_lock_bh(&xprt->transport_lock);
  630. if (++xprt->connect_cookie == 0) /* maintain a reserved value */
  631. ++xprt->connect_cookie;
  632. if (ep->rep_connected > 0) {
  633. if (!xprt_test_and_set_connected(xprt))
  634. xprt_wake_pending_tasks(xprt, 0);
  635. } else {
  636. if (xprt_test_and_clear_connected(xprt))
  637. xprt_wake_pending_tasks(xprt, -ENOTCONN);
  638. }
  639. spin_unlock_bh(&xprt->transport_lock);
  640. }
  641. /*
  642. * This function is called when memory window unbind which we are waiting
  643. * for completes. Just use rr_func (zeroed by upcall) to signal completion.
  644. */
  645. static void
  646. rpcrdma_unbind_func(struct rpcrdma_rep *rep)
  647. {
  648. wake_up(&rep->rr_unbind);
  649. }
  650. /*
  651. * Called as a tasklet to do req/reply match and complete a request
  652. * Errors must result in the RPC task either being awakened, or
  653. * allowed to timeout, to discover the errors at that time.
  654. */
  655. void
  656. rpcrdma_reply_handler(struct rpcrdma_rep *rep)
  657. {
  658. struct rpcrdma_msg *headerp;
  659. struct rpcrdma_req *req;
  660. struct rpc_rqst *rqst;
  661. struct rpc_xprt *xprt = rep->rr_xprt;
  662. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  663. __be32 *iptr;
  664. int i, rdmalen, status;
  665. /* Check status. If bad, signal disconnect and return rep to pool */
  666. if (rep->rr_len == ~0U) {
  667. rpcrdma_recv_buffer_put(rep);
  668. if (r_xprt->rx_ep.rep_connected == 1) {
  669. r_xprt->rx_ep.rep_connected = -EIO;
  670. rpcrdma_conn_func(&r_xprt->rx_ep);
  671. }
  672. return;
  673. }
  674. if (rep->rr_len < 28) {
  675. dprintk("RPC: %s: short/invalid reply\n", __func__);
  676. goto repost;
  677. }
  678. headerp = (struct rpcrdma_msg *) rep->rr_base;
  679. if (headerp->rm_vers != xdr_one) {
  680. dprintk("RPC: %s: invalid version %d\n",
  681. __func__, ntohl(headerp->rm_vers));
  682. goto repost;
  683. }
  684. /* Get XID and try for a match. */
  685. spin_lock(&xprt->transport_lock);
  686. rqst = xprt_lookup_rqst(xprt, headerp->rm_xid);
  687. if (rqst == NULL) {
  688. spin_unlock(&xprt->transport_lock);
  689. dprintk("RPC: %s: reply 0x%p failed "
  690. "to match any request xid 0x%08x len %d\n",
  691. __func__, rep, headerp->rm_xid, rep->rr_len);
  692. repost:
  693. r_xprt->rx_stats.bad_reply_count++;
  694. rep->rr_func = rpcrdma_reply_handler;
  695. if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, &r_xprt->rx_ep, rep))
  696. rpcrdma_recv_buffer_put(rep);
  697. return;
  698. }
  699. /* get request object */
  700. req = rpcr_to_rdmar(rqst);
  701. dprintk("RPC: %s: reply 0x%p completes request 0x%p\n"
  702. " RPC request 0x%p xid 0x%08x\n",
  703. __func__, rep, req, rqst, headerp->rm_xid);
  704. BUG_ON(!req || req->rl_reply);
  705. /* from here on, the reply is no longer an orphan */
  706. req->rl_reply = rep;
  707. /* check for expected message types */
  708. /* The order of some of these tests is important. */
  709. switch (headerp->rm_type) {
  710. case htonl(RDMA_MSG):
  711. /* never expect read chunks */
  712. /* never expect reply chunks (two ways to check) */
  713. /* never expect write chunks without having offered RDMA */
  714. if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
  715. (headerp->rm_body.rm_chunks[1] == xdr_zero &&
  716. headerp->rm_body.rm_chunks[2] != xdr_zero) ||
  717. (headerp->rm_body.rm_chunks[1] != xdr_zero &&
  718. req->rl_nchunks == 0))
  719. goto badheader;
  720. if (headerp->rm_body.rm_chunks[1] != xdr_zero) {
  721. /* count any expected write chunks in read reply */
  722. /* start at write chunk array count */
  723. iptr = &headerp->rm_body.rm_chunks[2];
  724. rdmalen = rpcrdma_count_chunks(rep,
  725. req->rl_nchunks, 1, &iptr);
  726. /* check for validity, and no reply chunk after */
  727. if (rdmalen < 0 || *iptr++ != xdr_zero)
  728. goto badheader;
  729. rep->rr_len -=
  730. ((unsigned char *)iptr - (unsigned char *)headerp);
  731. status = rep->rr_len + rdmalen;
  732. r_xprt->rx_stats.total_rdma_reply += rdmalen;
  733. /* special case - last chunk may omit padding */
  734. if (rdmalen &= 3) {
  735. rdmalen = 4 - rdmalen;
  736. status += rdmalen;
  737. }
  738. } else {
  739. /* else ordinary inline */
  740. rdmalen = 0;
  741. iptr = (__be32 *)((unsigned char *)headerp + 28);
  742. rep->rr_len -= 28; /*sizeof *headerp;*/
  743. status = rep->rr_len;
  744. }
  745. /* Fix up the rpc results for upper layer */
  746. rpcrdma_inline_fixup(rqst, (char *)iptr, rep->rr_len, rdmalen);
  747. break;
  748. case htonl(RDMA_NOMSG):
  749. /* never expect read or write chunks, always reply chunks */
  750. if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
  751. headerp->rm_body.rm_chunks[1] != xdr_zero ||
  752. headerp->rm_body.rm_chunks[2] != xdr_one ||
  753. req->rl_nchunks == 0)
  754. goto badheader;
  755. iptr = (__be32 *)((unsigned char *)headerp + 28);
  756. rdmalen = rpcrdma_count_chunks(rep, req->rl_nchunks, 0, &iptr);
  757. if (rdmalen < 0)
  758. goto badheader;
  759. r_xprt->rx_stats.total_rdma_reply += rdmalen;
  760. /* Reply chunk buffer already is the reply vector - no fixup. */
  761. status = rdmalen;
  762. break;
  763. badheader:
  764. default:
  765. dprintk("%s: invalid rpcrdma reply header (type %d):"
  766. " chunks[012] == %d %d %d"
  767. " expected chunks <= %d\n",
  768. __func__, ntohl(headerp->rm_type),
  769. headerp->rm_body.rm_chunks[0],
  770. headerp->rm_body.rm_chunks[1],
  771. headerp->rm_body.rm_chunks[2],
  772. req->rl_nchunks);
  773. status = -EIO;
  774. r_xprt->rx_stats.bad_reply_count++;
  775. break;
  776. }
  777. /* If using mw bind, start the deregister process now. */
  778. /* (Note: if mr_free(), cannot perform it here, in tasklet context) */
  779. if (req->rl_nchunks) switch (r_xprt->rx_ia.ri_memreg_strategy) {
  780. case RPCRDMA_MEMWINDOWS:
  781. for (i = 0; req->rl_nchunks-- > 1;)
  782. i += rpcrdma_deregister_external(
  783. &req->rl_segments[i], r_xprt, NULL);
  784. /* Optionally wait (not here) for unbinds to complete */
  785. rep->rr_func = rpcrdma_unbind_func;
  786. (void) rpcrdma_deregister_external(&req->rl_segments[i],
  787. r_xprt, rep);
  788. break;
  789. case RPCRDMA_MEMWINDOWS_ASYNC:
  790. for (i = 0; req->rl_nchunks--;)
  791. i += rpcrdma_deregister_external(&req->rl_segments[i],
  792. r_xprt, NULL);
  793. break;
  794. default:
  795. break;
  796. }
  797. dprintk("RPC: %s: xprt_complete_rqst(0x%p, 0x%p, %d)\n",
  798. __func__, xprt, rqst, status);
  799. xprt_complete_rqst(rqst->rq_task, status);
  800. spin_unlock(&xprt->transport_lock);
  801. }