rpc_rdma.c 27 KB

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