rpc_rdma.c 27 KB

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