rpc_rdma.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2014-2017 Oracle. All rights reserved.
  4. * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the BSD-type
  10. * license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * Neither the name of the Network Appliance, Inc. nor the names of
  25. * its contributors may be used to endorse or promote products
  26. * derived from this software without specific prior written
  27. * permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. /*
  42. * rpc_rdma.c
  43. *
  44. * This file contains the guts of the RPC RDMA protocol, and
  45. * does marshaling/unmarshaling, etc. It is also where interfacing
  46. * to the Linux RPC framework lives.
  47. */
  48. #include <linux/highmem.h>
  49. #include <linux/sunrpc/svc_rdma.h>
  50. #include "xprt_rdma.h"
  51. #include <trace/events/rpcrdma.h>
  52. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  53. # define RPCDBG_FACILITY RPCDBG_TRANS
  54. #endif
  55. /* Returns size of largest RPC-over-RDMA header in a Call message
  56. *
  57. * The largest Call header contains a full-size Read list and a
  58. * minimal Reply chunk.
  59. */
  60. static unsigned int rpcrdma_max_call_header_size(unsigned int maxsegs)
  61. {
  62. unsigned int size;
  63. /* Fixed header fields and list discriminators */
  64. size = RPCRDMA_HDRLEN_MIN;
  65. /* Maximum Read list size */
  66. maxsegs += 2; /* segment for head and tail buffers */
  67. size = maxsegs * rpcrdma_readchunk_maxsz * sizeof(__be32);
  68. /* Minimal Read chunk size */
  69. size += sizeof(__be32); /* segment count */
  70. size += rpcrdma_segment_maxsz * sizeof(__be32);
  71. size += sizeof(__be32); /* list discriminator */
  72. dprintk("RPC: %s: max call header size = %u\n",
  73. __func__, size);
  74. return size;
  75. }
  76. /* Returns size of largest RPC-over-RDMA header in a Reply message
  77. *
  78. * There is only one Write list or one Reply chunk per Reply
  79. * message. The larger list is the Write list.
  80. */
  81. static unsigned int rpcrdma_max_reply_header_size(unsigned int maxsegs)
  82. {
  83. unsigned int size;
  84. /* Fixed header fields and list discriminators */
  85. size = RPCRDMA_HDRLEN_MIN;
  86. /* Maximum Write list size */
  87. maxsegs += 2; /* segment for head and tail buffers */
  88. size = sizeof(__be32); /* segment count */
  89. size += maxsegs * rpcrdma_segment_maxsz * sizeof(__be32);
  90. size += sizeof(__be32); /* list discriminator */
  91. dprintk("RPC: %s: max reply header size = %u\n",
  92. __func__, size);
  93. return size;
  94. }
  95. void rpcrdma_set_max_header_sizes(struct rpcrdma_xprt *r_xprt)
  96. {
  97. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  98. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  99. unsigned int maxsegs = ia->ri_max_segs;
  100. ia->ri_max_inline_write = cdata->inline_wsize -
  101. rpcrdma_max_call_header_size(maxsegs);
  102. ia->ri_max_inline_read = cdata->inline_rsize -
  103. rpcrdma_max_reply_header_size(maxsegs);
  104. }
  105. /* The client can send a request inline as long as the RPCRDMA header
  106. * plus the RPC call fit under the transport's inline limit. If the
  107. * combined call message size exceeds that limit, the client must use
  108. * a Read chunk for this operation.
  109. *
  110. * A Read chunk is also required if sending the RPC call inline would
  111. * exceed this device's max_sge limit.
  112. */
  113. static bool rpcrdma_args_inline(struct rpcrdma_xprt *r_xprt,
  114. struct rpc_rqst *rqst)
  115. {
  116. struct xdr_buf *xdr = &rqst->rq_snd_buf;
  117. unsigned int count, remaining, offset;
  118. if (xdr->len > r_xprt->rx_ia.ri_max_inline_write)
  119. return false;
  120. if (xdr->page_len) {
  121. remaining = xdr->page_len;
  122. offset = offset_in_page(xdr->page_base);
  123. count = RPCRDMA_MIN_SEND_SGES;
  124. while (remaining) {
  125. remaining -= min_t(unsigned int,
  126. PAGE_SIZE - offset, remaining);
  127. offset = 0;
  128. if (++count > r_xprt->rx_ia.ri_max_send_sges)
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. /* The client can't know how large the actual reply will be. Thus it
  135. * plans for the largest possible reply for that particular ULP
  136. * operation. If the maximum combined reply message size exceeds that
  137. * limit, the client must provide a write list or a reply chunk for
  138. * this request.
  139. */
  140. static bool rpcrdma_results_inline(struct rpcrdma_xprt *r_xprt,
  141. struct rpc_rqst *rqst)
  142. {
  143. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  144. return rqst->rq_rcv_buf.buflen <= ia->ri_max_inline_read;
  145. }
  146. /* Split @vec on page boundaries into SGEs. FMR registers pages, not
  147. * a byte range. Other modes coalesce these SGEs into a single MR
  148. * when they can.
  149. *
  150. * Returns pointer to next available SGE, and bumps the total number
  151. * of SGEs consumed.
  152. */
  153. static struct rpcrdma_mr_seg *
  154. rpcrdma_convert_kvec(struct kvec *vec, struct rpcrdma_mr_seg *seg,
  155. unsigned int *n)
  156. {
  157. u32 remaining, page_offset;
  158. char *base;
  159. base = vec->iov_base;
  160. page_offset = offset_in_page(base);
  161. remaining = vec->iov_len;
  162. while (remaining) {
  163. seg->mr_page = NULL;
  164. seg->mr_offset = base;
  165. seg->mr_len = min_t(u32, PAGE_SIZE - page_offset, remaining);
  166. remaining -= seg->mr_len;
  167. base += seg->mr_len;
  168. ++seg;
  169. ++(*n);
  170. page_offset = 0;
  171. }
  172. return seg;
  173. }
  174. /* Convert @xdrbuf into SGEs no larger than a page each. As they
  175. * are registered, these SGEs are then coalesced into RDMA segments
  176. * when the selected memreg mode supports it.
  177. *
  178. * Returns positive number of SGEs consumed, or a negative errno.
  179. */
  180. static int
  181. rpcrdma_convert_iovs(struct rpcrdma_xprt *r_xprt, struct xdr_buf *xdrbuf,
  182. unsigned int pos, enum rpcrdma_chunktype type,
  183. struct rpcrdma_mr_seg *seg)
  184. {
  185. unsigned long page_base;
  186. unsigned int len, n;
  187. struct page **ppages;
  188. n = 0;
  189. if (pos == 0)
  190. seg = rpcrdma_convert_kvec(&xdrbuf->head[0], seg, &n);
  191. len = xdrbuf->page_len;
  192. ppages = xdrbuf->pages + (xdrbuf->page_base >> PAGE_SHIFT);
  193. page_base = offset_in_page(xdrbuf->page_base);
  194. while (len) {
  195. if (unlikely(!*ppages)) {
  196. /* XXX: Certain upper layer operations do
  197. * not provide receive buffer pages.
  198. */
  199. *ppages = alloc_page(GFP_ATOMIC);
  200. if (!*ppages)
  201. return -ENOBUFS;
  202. }
  203. seg->mr_page = *ppages;
  204. seg->mr_offset = (char *)page_base;
  205. seg->mr_len = min_t(u32, PAGE_SIZE - page_base, len);
  206. len -= seg->mr_len;
  207. ++ppages;
  208. ++seg;
  209. ++n;
  210. page_base = 0;
  211. }
  212. /* When encoding a Read chunk, the tail iovec contains an
  213. * XDR pad and may be omitted.
  214. */
  215. if (type == rpcrdma_readch && r_xprt->rx_ia.ri_implicit_roundup)
  216. goto out;
  217. /* When encoding a Write chunk, some servers need to see an
  218. * extra segment for non-XDR-aligned Write chunks. The upper
  219. * layer provides space in the tail iovec that may be used
  220. * for this purpose.
  221. */
  222. if (type == rpcrdma_writech && r_xprt->rx_ia.ri_implicit_roundup)
  223. goto out;
  224. if (xdrbuf->tail[0].iov_len)
  225. seg = rpcrdma_convert_kvec(&xdrbuf->tail[0], seg, &n);
  226. out:
  227. if (unlikely(n > RPCRDMA_MAX_SEGS))
  228. return -EIO;
  229. return n;
  230. }
  231. static inline int
  232. encode_item_present(struct xdr_stream *xdr)
  233. {
  234. __be32 *p;
  235. p = xdr_reserve_space(xdr, sizeof(*p));
  236. if (unlikely(!p))
  237. return -EMSGSIZE;
  238. *p = xdr_one;
  239. return 0;
  240. }
  241. static inline int
  242. encode_item_not_present(struct xdr_stream *xdr)
  243. {
  244. __be32 *p;
  245. p = xdr_reserve_space(xdr, sizeof(*p));
  246. if (unlikely(!p))
  247. return -EMSGSIZE;
  248. *p = xdr_zero;
  249. return 0;
  250. }
  251. static void
  252. xdr_encode_rdma_segment(__be32 *iptr, struct rpcrdma_mr *mr)
  253. {
  254. *iptr++ = cpu_to_be32(mr->mr_handle);
  255. *iptr++ = cpu_to_be32(mr->mr_length);
  256. xdr_encode_hyper(iptr, mr->mr_offset);
  257. }
  258. static int
  259. encode_rdma_segment(struct xdr_stream *xdr, struct rpcrdma_mr *mr)
  260. {
  261. __be32 *p;
  262. p = xdr_reserve_space(xdr, 4 * sizeof(*p));
  263. if (unlikely(!p))
  264. return -EMSGSIZE;
  265. xdr_encode_rdma_segment(p, mr);
  266. return 0;
  267. }
  268. static int
  269. encode_read_segment(struct xdr_stream *xdr, struct rpcrdma_mr *mr,
  270. u32 position)
  271. {
  272. __be32 *p;
  273. p = xdr_reserve_space(xdr, 6 * sizeof(*p));
  274. if (unlikely(!p))
  275. return -EMSGSIZE;
  276. *p++ = xdr_one; /* Item present */
  277. *p++ = cpu_to_be32(position);
  278. xdr_encode_rdma_segment(p, mr);
  279. return 0;
  280. }
  281. /* Register and XDR encode the Read list. Supports encoding a list of read
  282. * segments that belong to a single read chunk.
  283. *
  284. * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
  285. *
  286. * Read chunklist (a linked list):
  287. * N elements, position P (same P for all chunks of same arg!):
  288. * 1 - PHLOO - 1 - PHLOO - ... - 1 - PHLOO - 0
  289. *
  290. * Returns zero on success, or a negative errno if a failure occurred.
  291. * @xdr is advanced to the next position in the stream.
  292. *
  293. * Only a single @pos value is currently supported.
  294. */
  295. static noinline int
  296. rpcrdma_encode_read_list(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  297. struct rpc_rqst *rqst, enum rpcrdma_chunktype rtype)
  298. {
  299. struct xdr_stream *xdr = &req->rl_stream;
  300. struct rpcrdma_mr_seg *seg;
  301. struct rpcrdma_mr *mr;
  302. unsigned int pos;
  303. int nsegs;
  304. pos = rqst->rq_snd_buf.head[0].iov_len;
  305. if (rtype == rpcrdma_areadch)
  306. pos = 0;
  307. seg = req->rl_segments;
  308. nsegs = rpcrdma_convert_iovs(r_xprt, &rqst->rq_snd_buf, pos,
  309. rtype, seg);
  310. if (nsegs < 0)
  311. return nsegs;
  312. do {
  313. seg = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs,
  314. false, &mr);
  315. if (IS_ERR(seg))
  316. return PTR_ERR(seg);
  317. rpcrdma_mr_push(mr, &req->rl_registered);
  318. if (encode_read_segment(xdr, mr, pos) < 0)
  319. return -EMSGSIZE;
  320. trace_xprtrdma_read_chunk(rqst->rq_task, pos, mr, nsegs);
  321. r_xprt->rx_stats.read_chunk_count++;
  322. nsegs -= mr->mr_nents;
  323. } while (nsegs);
  324. return 0;
  325. }
  326. /* Register and XDR encode the Write list. Supports encoding a list
  327. * containing one array of plain segments that belong to a single
  328. * write chunk.
  329. *
  330. * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
  331. *
  332. * Write chunklist (a list of (one) counted array):
  333. * N elements:
  334. * 1 - N - HLOO - HLOO - ... - HLOO - 0
  335. *
  336. * Returns zero on success, or a negative errno if a failure occurred.
  337. * @xdr is advanced to the next position in the stream.
  338. *
  339. * Only a single Write chunk is currently supported.
  340. */
  341. static noinline int
  342. rpcrdma_encode_write_list(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  343. struct rpc_rqst *rqst, enum rpcrdma_chunktype wtype)
  344. {
  345. struct xdr_stream *xdr = &req->rl_stream;
  346. struct rpcrdma_mr_seg *seg;
  347. struct rpcrdma_mr *mr;
  348. int nsegs, nchunks;
  349. __be32 *segcount;
  350. seg = req->rl_segments;
  351. nsegs = rpcrdma_convert_iovs(r_xprt, &rqst->rq_rcv_buf,
  352. rqst->rq_rcv_buf.head[0].iov_len,
  353. wtype, seg);
  354. if (nsegs < 0)
  355. return nsegs;
  356. if (encode_item_present(xdr) < 0)
  357. return -EMSGSIZE;
  358. segcount = xdr_reserve_space(xdr, sizeof(*segcount));
  359. if (unlikely(!segcount))
  360. return -EMSGSIZE;
  361. /* Actual value encoded below */
  362. nchunks = 0;
  363. do {
  364. seg = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs,
  365. true, &mr);
  366. if (IS_ERR(seg))
  367. return PTR_ERR(seg);
  368. rpcrdma_mr_push(mr, &req->rl_registered);
  369. if (encode_rdma_segment(xdr, mr) < 0)
  370. return -EMSGSIZE;
  371. trace_xprtrdma_write_chunk(rqst->rq_task, mr, nsegs);
  372. r_xprt->rx_stats.write_chunk_count++;
  373. r_xprt->rx_stats.total_rdma_request += mr->mr_length;
  374. nchunks++;
  375. nsegs -= mr->mr_nents;
  376. } while (nsegs);
  377. /* Update count of segments in this Write chunk */
  378. *segcount = cpu_to_be32(nchunks);
  379. return 0;
  380. }
  381. /* Register and XDR encode the Reply chunk. Supports encoding an array
  382. * of plain segments that belong to a single write (reply) chunk.
  383. *
  384. * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
  385. *
  386. * Reply chunk (a counted array):
  387. * N elements:
  388. * 1 - N - HLOO - HLOO - ... - HLOO
  389. *
  390. * Returns zero on success, or a negative errno if a failure occurred.
  391. * @xdr is advanced to the next position in the stream.
  392. */
  393. static noinline int
  394. rpcrdma_encode_reply_chunk(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  395. struct rpc_rqst *rqst, enum rpcrdma_chunktype wtype)
  396. {
  397. struct xdr_stream *xdr = &req->rl_stream;
  398. struct rpcrdma_mr_seg *seg;
  399. struct rpcrdma_mr *mr;
  400. int nsegs, nchunks;
  401. __be32 *segcount;
  402. seg = req->rl_segments;
  403. nsegs = rpcrdma_convert_iovs(r_xprt, &rqst->rq_rcv_buf, 0, wtype, seg);
  404. if (nsegs < 0)
  405. return nsegs;
  406. if (encode_item_present(xdr) < 0)
  407. return -EMSGSIZE;
  408. segcount = xdr_reserve_space(xdr, sizeof(*segcount));
  409. if (unlikely(!segcount))
  410. return -EMSGSIZE;
  411. /* Actual value encoded below */
  412. nchunks = 0;
  413. do {
  414. seg = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs,
  415. true, &mr);
  416. if (IS_ERR(seg))
  417. return PTR_ERR(seg);
  418. rpcrdma_mr_push(mr, &req->rl_registered);
  419. if (encode_rdma_segment(xdr, mr) < 0)
  420. return -EMSGSIZE;
  421. trace_xprtrdma_reply_chunk(rqst->rq_task, mr, nsegs);
  422. r_xprt->rx_stats.reply_chunk_count++;
  423. r_xprt->rx_stats.total_rdma_request += mr->mr_length;
  424. nchunks++;
  425. nsegs -= mr->mr_nents;
  426. } while (nsegs);
  427. /* Update count of segments in the Reply chunk */
  428. *segcount = cpu_to_be32(nchunks);
  429. return 0;
  430. }
  431. /**
  432. * rpcrdma_unmap_sendctx - DMA-unmap Send buffers
  433. * @sc: sendctx containing SGEs to unmap
  434. *
  435. */
  436. void
  437. rpcrdma_unmap_sendctx(struct rpcrdma_sendctx *sc)
  438. {
  439. struct rpcrdma_ia *ia = &sc->sc_xprt->rx_ia;
  440. struct ib_sge *sge;
  441. unsigned int count;
  442. /* The first two SGEs contain the transport header and
  443. * the inline buffer. These are always left mapped so
  444. * they can be cheaply re-used.
  445. */
  446. sge = &sc->sc_sges[2];
  447. for (count = sc->sc_unmap_count; count; ++sge, --count)
  448. ib_dma_unmap_page(ia->ri_device,
  449. sge->addr, sge->length, DMA_TO_DEVICE);
  450. if (test_and_clear_bit(RPCRDMA_REQ_F_TX_RESOURCES, &sc->sc_req->rl_flags)) {
  451. smp_mb__after_atomic();
  452. wake_up_bit(&sc->sc_req->rl_flags, RPCRDMA_REQ_F_TX_RESOURCES);
  453. }
  454. }
  455. /* Prepare an SGE for the RPC-over-RDMA transport header.
  456. */
  457. static bool
  458. rpcrdma_prepare_hdr_sge(struct rpcrdma_ia *ia, struct rpcrdma_req *req,
  459. u32 len)
  460. {
  461. struct rpcrdma_sendctx *sc = req->rl_sendctx;
  462. struct rpcrdma_regbuf *rb = req->rl_rdmabuf;
  463. struct ib_sge *sge = sc->sc_sges;
  464. if (!rpcrdma_dma_map_regbuf(ia, rb))
  465. goto out_regbuf;
  466. sge->addr = rdmab_addr(rb);
  467. sge->length = len;
  468. sge->lkey = rdmab_lkey(rb);
  469. ib_dma_sync_single_for_device(rdmab_device(rb), sge->addr,
  470. sge->length, DMA_TO_DEVICE);
  471. sc->sc_wr.num_sge++;
  472. return true;
  473. out_regbuf:
  474. pr_err("rpcrdma: failed to DMA map a Send buffer\n");
  475. return false;
  476. }
  477. /* Prepare the Send SGEs. The head and tail iovec, and each entry
  478. * in the page list, gets its own SGE.
  479. */
  480. static bool
  481. rpcrdma_prepare_msg_sges(struct rpcrdma_ia *ia, struct rpcrdma_req *req,
  482. struct xdr_buf *xdr, enum rpcrdma_chunktype rtype)
  483. {
  484. struct rpcrdma_sendctx *sc = req->rl_sendctx;
  485. unsigned int sge_no, page_base, len, remaining;
  486. struct rpcrdma_regbuf *rb = req->rl_sendbuf;
  487. struct ib_device *device = ia->ri_device;
  488. struct ib_sge *sge = sc->sc_sges;
  489. u32 lkey = ia->ri_pd->local_dma_lkey;
  490. struct page *page, **ppages;
  491. /* The head iovec is straightforward, as it is already
  492. * DMA-mapped. Sync the content that has changed.
  493. */
  494. if (!rpcrdma_dma_map_regbuf(ia, rb))
  495. goto out_regbuf;
  496. sge_no = 1;
  497. sge[sge_no].addr = rdmab_addr(rb);
  498. sge[sge_no].length = xdr->head[0].iov_len;
  499. sge[sge_no].lkey = rdmab_lkey(rb);
  500. ib_dma_sync_single_for_device(rdmab_device(rb), sge[sge_no].addr,
  501. sge[sge_no].length, DMA_TO_DEVICE);
  502. /* If there is a Read chunk, the page list is being handled
  503. * via explicit RDMA, and thus is skipped here. However, the
  504. * tail iovec may include an XDR pad for the page list, as
  505. * well as additional content, and may not reside in the
  506. * same page as the head iovec.
  507. */
  508. if (rtype == rpcrdma_readch) {
  509. len = xdr->tail[0].iov_len;
  510. /* Do not include the tail if it is only an XDR pad */
  511. if (len < 4)
  512. goto out;
  513. page = virt_to_page(xdr->tail[0].iov_base);
  514. page_base = offset_in_page(xdr->tail[0].iov_base);
  515. /* If the content in the page list is an odd length,
  516. * xdr_write_pages() has added a pad at the beginning
  517. * of the tail iovec. Force the tail's non-pad content
  518. * to land at the next XDR position in the Send message.
  519. */
  520. page_base += len & 3;
  521. len -= len & 3;
  522. goto map_tail;
  523. }
  524. /* If there is a page list present, temporarily DMA map
  525. * and prepare an SGE for each page to be sent.
  526. */
  527. if (xdr->page_len) {
  528. ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
  529. page_base = offset_in_page(xdr->page_base);
  530. remaining = xdr->page_len;
  531. while (remaining) {
  532. sge_no++;
  533. if (sge_no > RPCRDMA_MAX_SEND_SGES - 2)
  534. goto out_mapping_overflow;
  535. len = min_t(u32, PAGE_SIZE - page_base, remaining);
  536. sge[sge_no].addr = ib_dma_map_page(device, *ppages,
  537. page_base, len,
  538. DMA_TO_DEVICE);
  539. if (ib_dma_mapping_error(device, sge[sge_no].addr))
  540. goto out_mapping_err;
  541. sge[sge_no].length = len;
  542. sge[sge_no].lkey = lkey;
  543. sc->sc_unmap_count++;
  544. ppages++;
  545. remaining -= len;
  546. page_base = 0;
  547. }
  548. }
  549. /* The tail iovec is not always constructed in the same
  550. * page where the head iovec resides (see, for example,
  551. * gss_wrap_req_priv). To neatly accommodate that case,
  552. * DMA map it separately.
  553. */
  554. if (xdr->tail[0].iov_len) {
  555. page = virt_to_page(xdr->tail[0].iov_base);
  556. page_base = offset_in_page(xdr->tail[0].iov_base);
  557. len = xdr->tail[0].iov_len;
  558. map_tail:
  559. sge_no++;
  560. sge[sge_no].addr = ib_dma_map_page(device, page,
  561. page_base, len,
  562. DMA_TO_DEVICE);
  563. if (ib_dma_mapping_error(device, sge[sge_no].addr))
  564. goto out_mapping_err;
  565. sge[sge_no].length = len;
  566. sge[sge_no].lkey = lkey;
  567. sc->sc_unmap_count++;
  568. }
  569. out:
  570. sc->sc_wr.num_sge += sge_no;
  571. if (sc->sc_unmap_count)
  572. __set_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags);
  573. return true;
  574. out_regbuf:
  575. pr_err("rpcrdma: failed to DMA map a Send buffer\n");
  576. return false;
  577. out_mapping_overflow:
  578. rpcrdma_unmap_sendctx(sc);
  579. pr_err("rpcrdma: too many Send SGEs (%u)\n", sge_no);
  580. return false;
  581. out_mapping_err:
  582. rpcrdma_unmap_sendctx(sc);
  583. pr_err("rpcrdma: Send mapping error\n");
  584. return false;
  585. }
  586. /**
  587. * rpcrdma_prepare_send_sges - Construct SGEs for a Send WR
  588. * @r_xprt: controlling transport
  589. * @req: context of RPC Call being marshalled
  590. * @hdrlen: size of transport header, in bytes
  591. * @xdr: xdr_buf containing RPC Call
  592. * @rtype: chunk type being encoded
  593. *
  594. * Returns 0 on success; otherwise a negative errno is returned.
  595. */
  596. int
  597. rpcrdma_prepare_send_sges(struct rpcrdma_xprt *r_xprt,
  598. struct rpcrdma_req *req, u32 hdrlen,
  599. struct xdr_buf *xdr, enum rpcrdma_chunktype rtype)
  600. {
  601. req->rl_sendctx = rpcrdma_sendctx_get_locked(&r_xprt->rx_buf);
  602. if (!req->rl_sendctx)
  603. return -EAGAIN;
  604. req->rl_sendctx->sc_wr.num_sge = 0;
  605. req->rl_sendctx->sc_unmap_count = 0;
  606. req->rl_sendctx->sc_req = req;
  607. __clear_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags);
  608. if (!rpcrdma_prepare_hdr_sge(&r_xprt->rx_ia, req, hdrlen))
  609. return -EIO;
  610. if (rtype != rpcrdma_areadch)
  611. if (!rpcrdma_prepare_msg_sges(&r_xprt->rx_ia, req, xdr, rtype))
  612. return -EIO;
  613. return 0;
  614. }
  615. /**
  616. * rpcrdma_marshal_req - Marshal and send one RPC request
  617. * @r_xprt: controlling transport
  618. * @rqst: RPC request to be marshaled
  619. *
  620. * For the RPC in "rqst", this function:
  621. * - Chooses the transfer mode (eg., RDMA_MSG or RDMA_NOMSG)
  622. * - Registers Read, Write, and Reply chunks
  623. * - Constructs the transport header
  624. * - Posts a Send WR to send the transport header and request
  625. *
  626. * Returns:
  627. * %0 if the RPC was sent successfully,
  628. * %-ENOTCONN if the connection was lost,
  629. * %-EAGAIN if the caller should call again with the same arguments,
  630. * %-ENOBUFS if the caller should call again after a delay,
  631. * %-EMSGSIZE if the transport header is too small,
  632. * %-EIO if a permanent problem occurred while marshaling.
  633. */
  634. int
  635. rpcrdma_marshal_req(struct rpcrdma_xprt *r_xprt, struct rpc_rqst *rqst)
  636. {
  637. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  638. struct xdr_stream *xdr = &req->rl_stream;
  639. enum rpcrdma_chunktype rtype, wtype;
  640. bool ddp_allowed;
  641. __be32 *p;
  642. int ret;
  643. rpcrdma_set_xdrlen(&req->rl_hdrbuf, 0);
  644. xdr_init_encode(xdr, &req->rl_hdrbuf,
  645. req->rl_rdmabuf->rg_base);
  646. /* Fixed header fields */
  647. ret = -EMSGSIZE;
  648. p = xdr_reserve_space(xdr, 4 * sizeof(*p));
  649. if (!p)
  650. goto out_err;
  651. *p++ = rqst->rq_xid;
  652. *p++ = rpcrdma_version;
  653. *p++ = cpu_to_be32(r_xprt->rx_buf.rb_max_requests);
  654. /* When the ULP employs a GSS flavor that guarantees integrity
  655. * or privacy, direct data placement of individual data items
  656. * is not allowed.
  657. */
  658. ddp_allowed = !(rqst->rq_cred->cr_auth->au_flags &
  659. RPCAUTH_AUTH_DATATOUCH);
  660. /*
  661. * Chunks needed for results?
  662. *
  663. * o If the expected result is under the inline threshold, all ops
  664. * return as inline.
  665. * o Large read ops return data as write chunk(s), header as
  666. * inline.
  667. * o Large non-read ops return as a single reply chunk.
  668. */
  669. if (rpcrdma_results_inline(r_xprt, rqst))
  670. wtype = rpcrdma_noch;
  671. else if (ddp_allowed && rqst->rq_rcv_buf.flags & XDRBUF_READ)
  672. wtype = rpcrdma_writech;
  673. else
  674. wtype = rpcrdma_replych;
  675. /*
  676. * Chunks needed for arguments?
  677. *
  678. * o If the total request is under the inline threshold, all ops
  679. * are sent as inline.
  680. * o Large write ops transmit data as read chunk(s), header as
  681. * inline.
  682. * o Large non-write ops are sent with the entire message as a
  683. * single read chunk (protocol 0-position special case).
  684. *
  685. * This assumes that the upper layer does not present a request
  686. * that both has a data payload, and whose non-data arguments
  687. * by themselves are larger than the inline threshold.
  688. */
  689. if (rpcrdma_args_inline(r_xprt, rqst)) {
  690. *p++ = rdma_msg;
  691. rtype = rpcrdma_noch;
  692. } else if (ddp_allowed && rqst->rq_snd_buf.flags & XDRBUF_WRITE) {
  693. *p++ = rdma_msg;
  694. rtype = rpcrdma_readch;
  695. } else {
  696. r_xprt->rx_stats.nomsg_call_count++;
  697. *p++ = rdma_nomsg;
  698. rtype = rpcrdma_areadch;
  699. }
  700. /* If this is a retransmit, discard previously registered
  701. * chunks. Very likely the connection has been replaced,
  702. * so these registrations are invalid and unusable.
  703. */
  704. while (unlikely(!list_empty(&req->rl_registered))) {
  705. struct rpcrdma_mr *mr;
  706. mr = rpcrdma_mr_pop(&req->rl_registered);
  707. rpcrdma_mr_defer_recovery(mr);
  708. }
  709. /* This implementation supports the following combinations
  710. * of chunk lists in one RPC-over-RDMA Call message:
  711. *
  712. * - Read list
  713. * - Write list
  714. * - Reply chunk
  715. * - Read list + Reply chunk
  716. *
  717. * It might not yet support the following combinations:
  718. *
  719. * - Read list + Write list
  720. *
  721. * It does not support the following combinations:
  722. *
  723. * - Write list + Reply chunk
  724. * - Read list + Write list + Reply chunk
  725. *
  726. * This implementation supports only a single chunk in each
  727. * Read or Write list. Thus for example the client cannot
  728. * send a Call message with a Position Zero Read chunk and a
  729. * regular Read chunk at the same time.
  730. */
  731. if (rtype != rpcrdma_noch) {
  732. ret = rpcrdma_encode_read_list(r_xprt, req, rqst, rtype);
  733. if (ret)
  734. goto out_err;
  735. }
  736. ret = encode_item_not_present(xdr);
  737. if (ret)
  738. goto out_err;
  739. if (wtype == rpcrdma_writech) {
  740. ret = rpcrdma_encode_write_list(r_xprt, req, rqst, wtype);
  741. if (ret)
  742. goto out_err;
  743. }
  744. ret = encode_item_not_present(xdr);
  745. if (ret)
  746. goto out_err;
  747. if (wtype != rpcrdma_replych)
  748. ret = encode_item_not_present(xdr);
  749. else
  750. ret = rpcrdma_encode_reply_chunk(r_xprt, req, rqst, wtype);
  751. if (ret)
  752. goto out_err;
  753. trace_xprtrdma_marshal(rqst, xdr_stream_pos(xdr), rtype, wtype);
  754. ret = rpcrdma_prepare_send_sges(r_xprt, req, xdr_stream_pos(xdr),
  755. &rqst->rq_snd_buf, rtype);
  756. if (ret)
  757. goto out_err;
  758. return 0;
  759. out_err:
  760. switch (ret) {
  761. case -EAGAIN:
  762. xprt_wait_for_buffer_space(rqst->rq_task, NULL);
  763. break;
  764. case -ENOBUFS:
  765. break;
  766. default:
  767. r_xprt->rx_stats.failed_marshal_count++;
  768. }
  769. return ret;
  770. }
  771. /**
  772. * rpcrdma_inline_fixup - Scatter inline received data into rqst's iovecs
  773. * @rqst: controlling RPC request
  774. * @srcp: points to RPC message payload in receive buffer
  775. * @copy_len: remaining length of receive buffer content
  776. * @pad: Write chunk pad bytes needed (zero for pure inline)
  777. *
  778. * The upper layer has set the maximum number of bytes it can
  779. * receive in each component of rq_rcv_buf. These values are set in
  780. * the head.iov_len, page_len, tail.iov_len, and buflen fields.
  781. *
  782. * Unlike the TCP equivalent (xdr_partial_copy_from_skb), in
  783. * many cases this function simply updates iov_base pointers in
  784. * rq_rcv_buf to point directly to the received reply data, to
  785. * avoid copying reply data.
  786. *
  787. * Returns the count of bytes which had to be memcopied.
  788. */
  789. static unsigned long
  790. rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad)
  791. {
  792. unsigned long fixup_copy_count;
  793. int i, npages, curlen;
  794. char *destp;
  795. struct page **ppages;
  796. int page_base;
  797. /* The head iovec is redirected to the RPC reply message
  798. * in the receive buffer, to avoid a memcopy.
  799. */
  800. rqst->rq_rcv_buf.head[0].iov_base = srcp;
  801. rqst->rq_private_buf.head[0].iov_base = srcp;
  802. /* The contents of the receive buffer that follow
  803. * head.iov_len bytes are copied into the page list.
  804. */
  805. curlen = rqst->rq_rcv_buf.head[0].iov_len;
  806. if (curlen > copy_len)
  807. curlen = copy_len;
  808. trace_xprtrdma_fixup(rqst, copy_len, curlen);
  809. srcp += curlen;
  810. copy_len -= curlen;
  811. ppages = rqst->rq_rcv_buf.pages +
  812. (rqst->rq_rcv_buf.page_base >> PAGE_SHIFT);
  813. page_base = offset_in_page(rqst->rq_rcv_buf.page_base);
  814. fixup_copy_count = 0;
  815. if (copy_len && rqst->rq_rcv_buf.page_len) {
  816. int pagelist_len;
  817. pagelist_len = rqst->rq_rcv_buf.page_len;
  818. if (pagelist_len > copy_len)
  819. pagelist_len = copy_len;
  820. npages = PAGE_ALIGN(page_base + pagelist_len) >> PAGE_SHIFT;
  821. for (i = 0; i < npages; i++) {
  822. curlen = PAGE_SIZE - page_base;
  823. if (curlen > pagelist_len)
  824. curlen = pagelist_len;
  825. trace_xprtrdma_fixup_pg(rqst, i, srcp,
  826. copy_len, curlen);
  827. destp = kmap_atomic(ppages[i]);
  828. memcpy(destp + page_base, srcp, curlen);
  829. flush_dcache_page(ppages[i]);
  830. kunmap_atomic(destp);
  831. srcp += curlen;
  832. copy_len -= curlen;
  833. fixup_copy_count += curlen;
  834. pagelist_len -= curlen;
  835. if (!pagelist_len)
  836. break;
  837. page_base = 0;
  838. }
  839. /* Implicit padding for the last segment in a Write
  840. * chunk is inserted inline at the front of the tail
  841. * iovec. The upper layer ignores the content of
  842. * the pad. Simply ensure inline content in the tail
  843. * that follows the Write chunk is properly aligned.
  844. */
  845. if (pad)
  846. srcp -= pad;
  847. }
  848. /* The tail iovec is redirected to the remaining data
  849. * in the receive buffer, to avoid a memcopy.
  850. */
  851. if (copy_len || pad) {
  852. rqst->rq_rcv_buf.tail[0].iov_base = srcp;
  853. rqst->rq_private_buf.tail[0].iov_base = srcp;
  854. }
  855. return fixup_copy_count;
  856. }
  857. /* By convention, backchannel calls arrive via rdma_msg type
  858. * messages, and never populate the chunk lists. This makes
  859. * the RPC/RDMA header small and fixed in size, so it is
  860. * straightforward to check the RPC header's direction field.
  861. */
  862. static bool
  863. rpcrdma_is_bcall(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep)
  864. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  865. {
  866. struct xdr_stream *xdr = &rep->rr_stream;
  867. __be32 *p;
  868. if (rep->rr_proc != rdma_msg)
  869. return false;
  870. /* Peek at stream contents without advancing. */
  871. p = xdr_inline_decode(xdr, 0);
  872. /* Chunk lists */
  873. if (*p++ != xdr_zero)
  874. return false;
  875. if (*p++ != xdr_zero)
  876. return false;
  877. if (*p++ != xdr_zero)
  878. return false;
  879. /* RPC header */
  880. if (*p++ != rep->rr_xid)
  881. return false;
  882. if (*p != cpu_to_be32(RPC_CALL))
  883. return false;
  884. /* Now that we are sure this is a backchannel call,
  885. * advance to the RPC header.
  886. */
  887. p = xdr_inline_decode(xdr, 3 * sizeof(*p));
  888. if (unlikely(!p))
  889. goto out_short;
  890. rpcrdma_bc_receive_call(r_xprt, rep);
  891. return true;
  892. out_short:
  893. pr_warn("RPC/RDMA short backward direction call\n");
  894. return true;
  895. }
  896. #else /* CONFIG_SUNRPC_BACKCHANNEL */
  897. {
  898. return false;
  899. }
  900. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  901. static int decode_rdma_segment(struct xdr_stream *xdr, u32 *length)
  902. {
  903. u32 handle;
  904. u64 offset;
  905. __be32 *p;
  906. p = xdr_inline_decode(xdr, 4 * sizeof(*p));
  907. if (unlikely(!p))
  908. return -EIO;
  909. handle = be32_to_cpup(p++);
  910. *length = be32_to_cpup(p++);
  911. xdr_decode_hyper(p, &offset);
  912. trace_xprtrdma_decode_seg(handle, *length, offset);
  913. return 0;
  914. }
  915. static int decode_write_chunk(struct xdr_stream *xdr, u32 *length)
  916. {
  917. u32 segcount, seglength;
  918. __be32 *p;
  919. p = xdr_inline_decode(xdr, sizeof(*p));
  920. if (unlikely(!p))
  921. return -EIO;
  922. *length = 0;
  923. segcount = be32_to_cpup(p);
  924. while (segcount--) {
  925. if (decode_rdma_segment(xdr, &seglength))
  926. return -EIO;
  927. *length += seglength;
  928. }
  929. return 0;
  930. }
  931. /* In RPC-over-RDMA Version One replies, a Read list is never
  932. * expected. This decoder is a stub that returns an error if
  933. * a Read list is present.
  934. */
  935. static int decode_read_list(struct xdr_stream *xdr)
  936. {
  937. __be32 *p;
  938. p = xdr_inline_decode(xdr, sizeof(*p));
  939. if (unlikely(!p))
  940. return -EIO;
  941. if (unlikely(*p != xdr_zero))
  942. return -EIO;
  943. return 0;
  944. }
  945. /* Supports only one Write chunk in the Write list
  946. */
  947. static int decode_write_list(struct xdr_stream *xdr, u32 *length)
  948. {
  949. u32 chunklen;
  950. bool first;
  951. __be32 *p;
  952. *length = 0;
  953. first = true;
  954. do {
  955. p = xdr_inline_decode(xdr, sizeof(*p));
  956. if (unlikely(!p))
  957. return -EIO;
  958. if (*p == xdr_zero)
  959. break;
  960. if (!first)
  961. return -EIO;
  962. if (decode_write_chunk(xdr, &chunklen))
  963. return -EIO;
  964. *length += chunklen;
  965. first = false;
  966. } while (true);
  967. return 0;
  968. }
  969. static int decode_reply_chunk(struct xdr_stream *xdr, u32 *length)
  970. {
  971. __be32 *p;
  972. p = xdr_inline_decode(xdr, sizeof(*p));
  973. if (unlikely(!p))
  974. return -EIO;
  975. *length = 0;
  976. if (*p != xdr_zero)
  977. if (decode_write_chunk(xdr, length))
  978. return -EIO;
  979. return 0;
  980. }
  981. static int
  982. rpcrdma_decode_msg(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep,
  983. struct rpc_rqst *rqst)
  984. {
  985. struct xdr_stream *xdr = &rep->rr_stream;
  986. u32 writelist, replychunk, rpclen;
  987. char *base;
  988. /* Decode the chunk lists */
  989. if (decode_read_list(xdr))
  990. return -EIO;
  991. if (decode_write_list(xdr, &writelist))
  992. return -EIO;
  993. if (decode_reply_chunk(xdr, &replychunk))
  994. return -EIO;
  995. /* RDMA_MSG sanity checks */
  996. if (unlikely(replychunk))
  997. return -EIO;
  998. /* Build the RPC reply's Payload stream in rqst->rq_rcv_buf */
  999. base = (char *)xdr_inline_decode(xdr, 0);
  1000. rpclen = xdr_stream_remaining(xdr);
  1001. r_xprt->rx_stats.fixup_copy_count +=
  1002. rpcrdma_inline_fixup(rqst, base, rpclen, writelist & 3);
  1003. r_xprt->rx_stats.total_rdma_reply += writelist;
  1004. return rpclen + xdr_align_size(writelist);
  1005. }
  1006. static noinline int
  1007. rpcrdma_decode_nomsg(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep)
  1008. {
  1009. struct xdr_stream *xdr = &rep->rr_stream;
  1010. u32 writelist, replychunk;
  1011. /* Decode the chunk lists */
  1012. if (decode_read_list(xdr))
  1013. return -EIO;
  1014. if (decode_write_list(xdr, &writelist))
  1015. return -EIO;
  1016. if (decode_reply_chunk(xdr, &replychunk))
  1017. return -EIO;
  1018. /* RDMA_NOMSG sanity checks */
  1019. if (unlikely(writelist))
  1020. return -EIO;
  1021. if (unlikely(!replychunk))
  1022. return -EIO;
  1023. /* Reply chunk buffer already is the reply vector */
  1024. r_xprt->rx_stats.total_rdma_reply += replychunk;
  1025. return replychunk;
  1026. }
  1027. static noinline int
  1028. rpcrdma_decode_error(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep,
  1029. struct rpc_rqst *rqst)
  1030. {
  1031. struct xdr_stream *xdr = &rep->rr_stream;
  1032. __be32 *p;
  1033. p = xdr_inline_decode(xdr, sizeof(*p));
  1034. if (unlikely(!p))
  1035. return -EIO;
  1036. switch (*p) {
  1037. case err_vers:
  1038. p = xdr_inline_decode(xdr, 2 * sizeof(*p));
  1039. if (!p)
  1040. break;
  1041. dprintk("RPC: %5u: %s: server reports version error (%u-%u)\n",
  1042. rqst->rq_task->tk_pid, __func__,
  1043. be32_to_cpup(p), be32_to_cpu(*(p + 1)));
  1044. break;
  1045. case err_chunk:
  1046. dprintk("RPC: %5u: %s: server reports header decoding error\n",
  1047. rqst->rq_task->tk_pid, __func__);
  1048. break;
  1049. default:
  1050. dprintk("RPC: %5u: %s: server reports unrecognized error %d\n",
  1051. rqst->rq_task->tk_pid, __func__, be32_to_cpup(p));
  1052. }
  1053. r_xprt->rx_stats.bad_reply_count++;
  1054. return -EREMOTEIO;
  1055. }
  1056. /* Perform XID lookup, reconstruction of the RPC reply, and
  1057. * RPC completion while holding the transport lock to ensure
  1058. * the rep, rqst, and rq_task pointers remain stable.
  1059. */
  1060. void rpcrdma_complete_rqst(struct rpcrdma_rep *rep)
  1061. {
  1062. struct rpcrdma_xprt *r_xprt = rep->rr_rxprt;
  1063. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  1064. struct rpc_rqst *rqst = rep->rr_rqst;
  1065. unsigned long cwnd;
  1066. int status;
  1067. xprt->reestablish_timeout = 0;
  1068. switch (rep->rr_proc) {
  1069. case rdma_msg:
  1070. status = rpcrdma_decode_msg(r_xprt, rep, rqst);
  1071. break;
  1072. case rdma_nomsg:
  1073. status = rpcrdma_decode_nomsg(r_xprt, rep);
  1074. break;
  1075. case rdma_error:
  1076. status = rpcrdma_decode_error(r_xprt, rep, rqst);
  1077. break;
  1078. default:
  1079. status = -EIO;
  1080. }
  1081. if (status < 0)
  1082. goto out_badheader;
  1083. out:
  1084. spin_lock(&xprt->recv_lock);
  1085. cwnd = xprt->cwnd;
  1086. xprt->cwnd = r_xprt->rx_buf.rb_credits << RPC_CWNDSHIFT;
  1087. if (xprt->cwnd > cwnd)
  1088. xprt_release_rqst_cong(rqst->rq_task);
  1089. xprt_complete_rqst(rqst->rq_task, status);
  1090. xprt_unpin_rqst(rqst);
  1091. spin_unlock(&xprt->recv_lock);
  1092. return;
  1093. /* If the incoming reply terminated a pending RPC, the next
  1094. * RPC call will post a replacement receive buffer as it is
  1095. * being marshaled.
  1096. */
  1097. out_badheader:
  1098. trace_xprtrdma_reply_hdr(rep);
  1099. r_xprt->rx_stats.bad_reply_count++;
  1100. status = -EIO;
  1101. goto out;
  1102. }
  1103. void rpcrdma_release_rqst(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
  1104. {
  1105. /* Invalidate and unmap the data payloads before waking
  1106. * the waiting application. This guarantees the memory
  1107. * regions are properly fenced from the server before the
  1108. * application accesses the data. It also ensures proper
  1109. * send flow control: waking the next RPC waits until this
  1110. * RPC has relinquished all its Send Queue entries.
  1111. */
  1112. if (!list_empty(&req->rl_registered))
  1113. r_xprt->rx_ia.ri_ops->ro_unmap_sync(r_xprt,
  1114. &req->rl_registered);
  1115. /* Ensure that any DMA mapped pages associated with
  1116. * the Send of the RPC Call have been unmapped before
  1117. * allowing the RPC to complete. This protects argument
  1118. * memory not controlled by the RPC client from being
  1119. * re-used before we're done with it.
  1120. */
  1121. if (test_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags)) {
  1122. r_xprt->rx_stats.reply_waits_for_send++;
  1123. out_of_line_wait_on_bit(&req->rl_flags,
  1124. RPCRDMA_REQ_F_TX_RESOURCES,
  1125. bit_wait,
  1126. TASK_UNINTERRUPTIBLE);
  1127. }
  1128. }
  1129. /* Reply handling runs in the poll worker thread. Anything that
  1130. * might wait is deferred to a separate workqueue.
  1131. */
  1132. void rpcrdma_deferred_completion(struct work_struct *work)
  1133. {
  1134. struct rpcrdma_rep *rep =
  1135. container_of(work, struct rpcrdma_rep, rr_work);
  1136. struct rpcrdma_req *req = rpcr_to_rdmar(rep->rr_rqst);
  1137. struct rpcrdma_xprt *r_xprt = rep->rr_rxprt;
  1138. trace_xprtrdma_defer_cmp(rep);
  1139. if (rep->rr_wc_flags & IB_WC_WITH_INVALIDATE)
  1140. r_xprt->rx_ia.ri_ops->ro_reminv(rep, &req->rl_registered);
  1141. rpcrdma_release_rqst(r_xprt, req);
  1142. rpcrdma_complete_rqst(rep);
  1143. }
  1144. /* Process received RPC/RDMA messages.
  1145. *
  1146. * Errors must result in the RPC task either being awakened, or
  1147. * allowed to timeout, to discover the errors at that time.
  1148. */
  1149. void rpcrdma_reply_handler(struct rpcrdma_rep *rep)
  1150. {
  1151. struct rpcrdma_xprt *r_xprt = rep->rr_rxprt;
  1152. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  1153. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  1154. struct rpcrdma_req *req;
  1155. struct rpc_rqst *rqst;
  1156. u32 credits;
  1157. __be32 *p;
  1158. --buf->rb_posted_receives;
  1159. if (rep->rr_hdrbuf.head[0].iov_len == 0)
  1160. goto out_badstatus;
  1161. /* Fixed transport header fields */
  1162. xdr_init_decode(&rep->rr_stream, &rep->rr_hdrbuf,
  1163. rep->rr_hdrbuf.head[0].iov_base);
  1164. p = xdr_inline_decode(&rep->rr_stream, 4 * sizeof(*p));
  1165. if (unlikely(!p))
  1166. goto out_shortreply;
  1167. rep->rr_xid = *p++;
  1168. rep->rr_vers = *p++;
  1169. credits = be32_to_cpu(*p++);
  1170. rep->rr_proc = *p++;
  1171. if (rep->rr_vers != rpcrdma_version)
  1172. goto out_badversion;
  1173. if (rpcrdma_is_bcall(r_xprt, rep))
  1174. return;
  1175. /* Match incoming rpcrdma_rep to an rpcrdma_req to
  1176. * get context for handling any incoming chunks.
  1177. */
  1178. spin_lock(&xprt->recv_lock);
  1179. rqst = xprt_lookup_rqst(xprt, rep->rr_xid);
  1180. if (!rqst)
  1181. goto out_norqst;
  1182. xprt_pin_rqst(rqst);
  1183. if (credits == 0)
  1184. credits = 1; /* don't deadlock */
  1185. else if (credits > buf->rb_max_requests)
  1186. credits = buf->rb_max_requests;
  1187. buf->rb_credits = credits;
  1188. spin_unlock(&xprt->recv_lock);
  1189. req = rpcr_to_rdmar(rqst);
  1190. if (req->rl_reply) {
  1191. trace_xprtrdma_leaked_rep(rqst, req->rl_reply);
  1192. rpcrdma_recv_buffer_put(req->rl_reply);
  1193. }
  1194. req->rl_reply = rep;
  1195. rep->rr_rqst = rqst;
  1196. clear_bit(RPCRDMA_REQ_F_PENDING, &req->rl_flags);
  1197. trace_xprtrdma_reply(rqst->rq_task, rep, req, credits);
  1198. rpcrdma_post_recvs(r_xprt, false);
  1199. queue_work(rpcrdma_receive_wq, &rep->rr_work);
  1200. return;
  1201. out_badversion:
  1202. trace_xprtrdma_reply_vers(rep);
  1203. goto repost;
  1204. /* The RPC transaction has already been terminated, or the header
  1205. * is corrupt.
  1206. */
  1207. out_norqst:
  1208. spin_unlock(&xprt->recv_lock);
  1209. trace_xprtrdma_reply_rqst(rep);
  1210. goto repost;
  1211. out_shortreply:
  1212. trace_xprtrdma_reply_short(rep);
  1213. /* If no pending RPC transaction was matched, post a replacement
  1214. * receive buffer before returning.
  1215. */
  1216. repost:
  1217. rpcrdma_post_recvs(r_xprt, false);
  1218. out_badstatus:
  1219. rpcrdma_recv_buffer_put(rep);
  1220. }