verbs.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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. * verbs.c
  43. *
  44. * Encapsulates the major functions managing:
  45. * o adapters
  46. * o endpoints
  47. * o connections
  48. * o buffer memory
  49. */
  50. #include <linux/interrupt.h>
  51. #include <linux/slab.h>
  52. #include <linux/sunrpc/addr.h>
  53. #include <linux/sunrpc/svc_rdma.h>
  54. #include <linux/log2.h>
  55. #include <asm-generic/barrier.h>
  56. #include <asm/bitops.h>
  57. #include <rdma/ib_cm.h>
  58. #include "xprt_rdma.h"
  59. #include <trace/events/rpcrdma.h>
  60. /*
  61. * Globals/Macros
  62. */
  63. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  64. # define RPCDBG_FACILITY RPCDBG_TRANS
  65. #endif
  66. /*
  67. * internal functions
  68. */
  69. static int rpcrdma_sendctxs_create(struct rpcrdma_xprt *r_xprt);
  70. static void rpcrdma_sendctxs_destroy(struct rpcrdma_xprt *r_xprt);
  71. static void rpcrdma_sendctx_put_locked(struct rpcrdma_xprt *r_xprt,
  72. struct rpcrdma_sendctx *sc);
  73. static int rpcrdma_reqs_setup(struct rpcrdma_xprt *r_xprt);
  74. static void rpcrdma_reqs_reset(struct rpcrdma_xprt *r_xprt);
  75. static void rpcrdma_rep_destroy(struct rpcrdma_rep *rep);
  76. static void rpcrdma_reps_unmap(struct rpcrdma_xprt *r_xprt);
  77. static void rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt);
  78. static void rpcrdma_mrs_destroy(struct rpcrdma_xprt *r_xprt);
  79. static void rpcrdma_ep_get(struct rpcrdma_ep *ep);
  80. static int rpcrdma_ep_put(struct rpcrdma_ep *ep);
  81. static struct rpcrdma_regbuf *
  82. rpcrdma_regbuf_alloc(size_t size, enum dma_data_direction direction,
  83. gfp_t flags);
  84. static void rpcrdma_regbuf_dma_unmap(struct rpcrdma_regbuf *rb);
  85. static void rpcrdma_regbuf_free(struct rpcrdma_regbuf *rb);
  86. /* Wait for outstanding transport work to finish. ib_drain_qp
  87. * handles the drains in the wrong order for us, so open code
  88. * them here.
  89. */
  90. static void rpcrdma_xprt_drain(struct rpcrdma_xprt *r_xprt)
  91. {
  92. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  93. struct rdma_cm_id *id = ep->re_id;
  94. /* Flush Receives, then wait for deferred Reply work
  95. * to complete.
  96. */
  97. ib_drain_rq(id->qp);
  98. /* Deferred Reply processing might have scheduled
  99. * local invalidations.
  100. */
  101. ib_drain_sq(id->qp);
  102. rpcrdma_ep_put(ep);
  103. }
  104. /**
  105. * rpcrdma_qp_event_handler - Handle one QP event (error notification)
  106. * @event: details of the event
  107. * @context: ep that owns QP where event occurred
  108. *
  109. * Called from the RDMA provider (device driver) possibly in an interrupt
  110. * context. The QP is always destroyed before the ID, so the ID will be
  111. * reliably available when this handler is invoked.
  112. */
  113. static void rpcrdma_qp_event_handler(struct ib_event *event, void *context)
  114. {
  115. struct rpcrdma_ep *ep = context;
  116. trace_xprtrdma_qp_event(ep, event);
  117. }
  118. /* Ensure xprt_force_disconnect() is invoked exactly once when a
  119. * connection is closed or lost. (The important thing is it needs
  120. * to be invoked "at least" once).
  121. */
  122. static void rpcrdma_force_disconnect(struct rpcrdma_ep *ep)
  123. {
  124. if (atomic_add_unless(&ep->re_force_disconnect, 1, 1))
  125. xprt_force_disconnect(ep->re_xprt);
  126. }
  127. /**
  128. * rpcrdma_flush_disconnect - Disconnect on flushed completion
  129. * @r_xprt: transport to disconnect
  130. * @wc: work completion entry
  131. *
  132. * Must be called in process context.
  133. */
  134. void rpcrdma_flush_disconnect(struct rpcrdma_xprt *r_xprt, struct ib_wc *wc)
  135. {
  136. if (wc->status != IB_WC_SUCCESS)
  137. rpcrdma_force_disconnect(r_xprt->rx_ep);
  138. }
  139. /**
  140. * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
  141. * @cq: completion queue
  142. * @wc: WCE for a completed Send WR
  143. *
  144. */
  145. static void rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
  146. {
  147. struct ib_cqe *cqe = wc->wr_cqe;
  148. struct rpcrdma_sendctx *sc =
  149. container_of(cqe, struct rpcrdma_sendctx, sc_cqe);
  150. struct rpcrdma_xprt *r_xprt = cq->cq_context;
  151. /* WARNING: Only wr_cqe and status are reliable at this point */
  152. trace_xprtrdma_wc_send(sc, wc);
  153. rpcrdma_sendctx_put_locked(r_xprt, sc);
  154. rpcrdma_flush_disconnect(r_xprt, wc);
  155. }
  156. /**
  157. * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
  158. * @cq: completion queue
  159. * @wc: WCE for a completed Receive WR
  160. *
  161. */
  162. static void rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
  163. {
  164. struct ib_cqe *cqe = wc->wr_cqe;
  165. struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
  166. rr_cqe);
  167. struct rpcrdma_xprt *r_xprt = cq->cq_context;
  168. /* WARNING: Only wr_cqe and status are reliable at this point */
  169. trace_xprtrdma_wc_receive(wc);
  170. --r_xprt->rx_ep->re_receive_count;
  171. if (wc->status != IB_WC_SUCCESS)
  172. goto out_flushed;
  173. /* status == SUCCESS means all fields in wc are trustworthy */
  174. rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len);
  175. rep->rr_wc_flags = wc->wc_flags;
  176. rep->rr_inv_rkey = wc->ex.invalidate_rkey;
  177. ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
  178. rdmab_addr(rep->rr_rdmabuf),
  179. wc->byte_len, DMA_FROM_DEVICE);
  180. rpcrdma_reply_handler(rep);
  181. return;
  182. out_flushed:
  183. rpcrdma_flush_disconnect(r_xprt, wc);
  184. rpcrdma_rep_destroy(rep);
  185. }
  186. static void rpcrdma_update_cm_private(struct rpcrdma_ep *ep,
  187. struct rdma_conn_param *param)
  188. {
  189. const struct rpcrdma_connect_private *pmsg = param->private_data;
  190. unsigned int rsize, wsize;
  191. /* Default settings for RPC-over-RDMA Version One */
  192. ep->re_implicit_roundup = xprt_rdma_pad_optimize;
  193. rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
  194. wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
  195. if (pmsg &&
  196. pmsg->cp_magic == rpcrdma_cmp_magic &&
  197. pmsg->cp_version == RPCRDMA_CMP_VERSION) {
  198. ep->re_implicit_roundup = true;
  199. rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
  200. wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
  201. }
  202. if (rsize < ep->re_inline_recv)
  203. ep->re_inline_recv = rsize;
  204. if (wsize < ep->re_inline_send)
  205. ep->re_inline_send = wsize;
  206. rpcrdma_set_max_header_sizes(ep);
  207. }
  208. /**
  209. * rpcrdma_cm_event_handler - Handle RDMA CM events
  210. * @id: rdma_cm_id on which an event has occurred
  211. * @event: details of the event
  212. *
  213. * Called with @id's mutex held. Returns 1 if caller should
  214. * destroy @id, otherwise 0.
  215. */
  216. static int
  217. rpcrdma_cm_event_handler(struct rdma_cm_id *id, struct rdma_cm_event *event)
  218. {
  219. struct sockaddr *sap = (struct sockaddr *)&id->route.addr.dst_addr;
  220. struct rpcrdma_ep *ep = id->context;
  221. might_sleep();
  222. switch (event->event) {
  223. case RDMA_CM_EVENT_ADDR_RESOLVED:
  224. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  225. ep->re_async_rc = 0;
  226. complete(&ep->re_done);
  227. return 0;
  228. case RDMA_CM_EVENT_ADDR_ERROR:
  229. ep->re_async_rc = -EPROTO;
  230. complete(&ep->re_done);
  231. return 0;
  232. case RDMA_CM_EVENT_ROUTE_ERROR:
  233. ep->re_async_rc = -ENETUNREACH;
  234. complete(&ep->re_done);
  235. return 0;
  236. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  237. pr_info("rpcrdma: removing device %s for %pISpc\n",
  238. ep->re_id->device->name, sap);
  239. fallthrough;
  240. case RDMA_CM_EVENT_ADDR_CHANGE:
  241. ep->re_connect_status = -ENODEV;
  242. goto disconnected;
  243. case RDMA_CM_EVENT_ESTABLISHED:
  244. rpcrdma_ep_get(ep);
  245. ep->re_connect_status = 1;
  246. rpcrdma_update_cm_private(ep, &event->param.conn);
  247. trace_xprtrdma_inline_thresh(ep);
  248. wake_up_all(&ep->re_connect_wait);
  249. break;
  250. case RDMA_CM_EVENT_CONNECT_ERROR:
  251. ep->re_connect_status = -ENOTCONN;
  252. goto wake_connect_worker;
  253. case RDMA_CM_EVENT_UNREACHABLE:
  254. ep->re_connect_status = -ENETUNREACH;
  255. goto wake_connect_worker;
  256. case RDMA_CM_EVENT_REJECTED:
  257. dprintk("rpcrdma: connection to %pISpc rejected: %s\n",
  258. sap, rdma_reject_msg(id, event->status));
  259. ep->re_connect_status = -ECONNREFUSED;
  260. if (event->status == IB_CM_REJ_STALE_CONN)
  261. ep->re_connect_status = -ENOTCONN;
  262. wake_connect_worker:
  263. wake_up_all(&ep->re_connect_wait);
  264. return 0;
  265. case RDMA_CM_EVENT_DISCONNECTED:
  266. ep->re_connect_status = -ECONNABORTED;
  267. disconnected:
  268. rpcrdma_force_disconnect(ep);
  269. return rpcrdma_ep_put(ep);
  270. default:
  271. break;
  272. }
  273. dprintk("RPC: %s: %pISpc on %s/frwr: %s\n", __func__, sap,
  274. ep->re_id->device->name, rdma_event_msg(event->event));
  275. return 0;
  276. }
  277. static struct rdma_cm_id *rpcrdma_create_id(struct rpcrdma_xprt *r_xprt,
  278. struct rpcrdma_ep *ep)
  279. {
  280. unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
  281. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  282. struct rdma_cm_id *id;
  283. int rc;
  284. init_completion(&ep->re_done);
  285. id = rdma_create_id(xprt->xprt_net, rpcrdma_cm_event_handler, ep,
  286. RDMA_PS_TCP, IB_QPT_RC);
  287. if (IS_ERR(id))
  288. return id;
  289. ep->re_async_rc = -ETIMEDOUT;
  290. rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)&xprt->addr,
  291. RDMA_RESOLVE_TIMEOUT);
  292. if (rc)
  293. goto out;
  294. rc = wait_for_completion_interruptible_timeout(&ep->re_done, wtimeout);
  295. if (rc < 0)
  296. goto out;
  297. rc = ep->re_async_rc;
  298. if (rc)
  299. goto out;
  300. ep->re_async_rc = -ETIMEDOUT;
  301. rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
  302. if (rc)
  303. goto out;
  304. rc = wait_for_completion_interruptible_timeout(&ep->re_done, wtimeout);
  305. if (rc < 0)
  306. goto out;
  307. rc = ep->re_async_rc;
  308. if (rc)
  309. goto out;
  310. return id;
  311. out:
  312. rdma_destroy_id(id);
  313. return ERR_PTR(rc);
  314. }
  315. static void rpcrdma_ep_destroy(struct kref *kref)
  316. {
  317. struct rpcrdma_ep *ep = container_of(kref, struct rpcrdma_ep, re_kref);
  318. if (ep->re_id->qp) {
  319. rdma_destroy_qp(ep->re_id);
  320. ep->re_id->qp = NULL;
  321. }
  322. if (ep->re_attr.recv_cq)
  323. ib_free_cq(ep->re_attr.recv_cq);
  324. ep->re_attr.recv_cq = NULL;
  325. if (ep->re_attr.send_cq)
  326. ib_free_cq(ep->re_attr.send_cq);
  327. ep->re_attr.send_cq = NULL;
  328. if (ep->re_pd)
  329. ib_dealloc_pd(ep->re_pd);
  330. ep->re_pd = NULL;
  331. kfree(ep);
  332. module_put(THIS_MODULE);
  333. }
  334. static noinline void rpcrdma_ep_get(struct rpcrdma_ep *ep)
  335. {
  336. kref_get(&ep->re_kref);
  337. }
  338. /* Returns:
  339. * %0 if @ep still has a positive kref count, or
  340. * %1 if @ep was destroyed successfully.
  341. */
  342. static noinline int rpcrdma_ep_put(struct rpcrdma_ep *ep)
  343. {
  344. return kref_put(&ep->re_kref, rpcrdma_ep_destroy);
  345. }
  346. static int rpcrdma_ep_create(struct rpcrdma_xprt *r_xprt)
  347. {
  348. struct rpcrdma_connect_private *pmsg;
  349. struct ib_device *device;
  350. struct rdma_cm_id *id;
  351. struct rpcrdma_ep *ep;
  352. int rc;
  353. ep = kzalloc(sizeof(*ep), GFP_NOFS);
  354. if (!ep)
  355. return -ENOTCONN;
  356. ep->re_xprt = &r_xprt->rx_xprt;
  357. kref_init(&ep->re_kref);
  358. id = rpcrdma_create_id(r_xprt, ep);
  359. if (IS_ERR(id)) {
  360. kfree(ep);
  361. return PTR_ERR(id);
  362. }
  363. __module_get(THIS_MODULE);
  364. device = id->device;
  365. ep->re_id = id;
  366. ep->re_max_requests = r_xprt->rx_xprt.max_reqs;
  367. ep->re_inline_send = xprt_rdma_max_inline_write;
  368. ep->re_inline_recv = xprt_rdma_max_inline_read;
  369. rc = frwr_query_device(ep, device);
  370. if (rc)
  371. goto out_destroy;
  372. r_xprt->rx_buf.rb_max_requests = cpu_to_be32(ep->re_max_requests);
  373. ep->re_attr.event_handler = rpcrdma_qp_event_handler;
  374. ep->re_attr.qp_context = ep;
  375. ep->re_attr.srq = NULL;
  376. ep->re_attr.cap.max_inline_data = 0;
  377. ep->re_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  378. ep->re_attr.qp_type = IB_QPT_RC;
  379. ep->re_attr.port_num = ~0;
  380. dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
  381. "iovs: send %d recv %d\n",
  382. __func__,
  383. ep->re_attr.cap.max_send_wr,
  384. ep->re_attr.cap.max_recv_wr,
  385. ep->re_attr.cap.max_send_sge,
  386. ep->re_attr.cap.max_recv_sge);
  387. ep->re_send_batch = ep->re_max_requests >> 3;
  388. ep->re_send_count = ep->re_send_batch;
  389. init_waitqueue_head(&ep->re_connect_wait);
  390. ep->re_attr.send_cq = ib_alloc_cq_any(device, r_xprt,
  391. ep->re_attr.cap.max_send_wr,
  392. IB_POLL_WORKQUEUE);
  393. if (IS_ERR(ep->re_attr.send_cq)) {
  394. rc = PTR_ERR(ep->re_attr.send_cq);
  395. ep->re_attr.send_cq = NULL;
  396. goto out_destroy;
  397. }
  398. ep->re_attr.recv_cq = ib_alloc_cq_any(device, r_xprt,
  399. ep->re_attr.cap.max_recv_wr,
  400. IB_POLL_WORKQUEUE);
  401. if (IS_ERR(ep->re_attr.recv_cq)) {
  402. rc = PTR_ERR(ep->re_attr.recv_cq);
  403. ep->re_attr.recv_cq = NULL;
  404. goto out_destroy;
  405. }
  406. ep->re_receive_count = 0;
  407. /* Initialize cma parameters */
  408. memset(&ep->re_remote_cma, 0, sizeof(ep->re_remote_cma));
  409. /* Prepare RDMA-CM private message */
  410. pmsg = &ep->re_cm_private;
  411. pmsg->cp_magic = rpcrdma_cmp_magic;
  412. pmsg->cp_version = RPCRDMA_CMP_VERSION;
  413. pmsg->cp_flags |= RPCRDMA_CMP_F_SND_W_INV_OK;
  414. pmsg->cp_send_size = rpcrdma_encode_buffer_size(ep->re_inline_send);
  415. pmsg->cp_recv_size = rpcrdma_encode_buffer_size(ep->re_inline_recv);
  416. ep->re_remote_cma.private_data = pmsg;
  417. ep->re_remote_cma.private_data_len = sizeof(*pmsg);
  418. /* Client offers RDMA Read but does not initiate */
  419. ep->re_remote_cma.initiator_depth = 0;
  420. ep->re_remote_cma.responder_resources =
  421. min_t(int, U8_MAX, device->attrs.max_qp_rd_atom);
  422. /* Limit transport retries so client can detect server
  423. * GID changes quickly. RPC layer handles re-establishing
  424. * transport connection and retransmission.
  425. */
  426. ep->re_remote_cma.retry_count = 6;
  427. /* RPC-over-RDMA handles its own flow control. In addition,
  428. * make all RNR NAKs visible so we know that RPC-over-RDMA
  429. * flow control is working correctly (no NAKs should be seen).
  430. */
  431. ep->re_remote_cma.flow_control = 0;
  432. ep->re_remote_cma.rnr_retry_count = 0;
  433. ep->re_pd = ib_alloc_pd(device, 0);
  434. if (IS_ERR(ep->re_pd)) {
  435. rc = PTR_ERR(ep->re_pd);
  436. ep->re_pd = NULL;
  437. goto out_destroy;
  438. }
  439. rc = rdma_create_qp(id, ep->re_pd, &ep->re_attr);
  440. if (rc)
  441. goto out_destroy;
  442. r_xprt->rx_ep = ep;
  443. return 0;
  444. out_destroy:
  445. rpcrdma_ep_put(ep);
  446. rdma_destroy_id(id);
  447. return rc;
  448. }
  449. /**
  450. * rpcrdma_xprt_connect - Connect an unconnected transport
  451. * @r_xprt: controlling transport instance
  452. *
  453. * Returns 0 on success or a negative errno.
  454. */
  455. int rpcrdma_xprt_connect(struct rpcrdma_xprt *r_xprt)
  456. {
  457. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  458. struct rpcrdma_ep *ep;
  459. int rc;
  460. rc = rpcrdma_ep_create(r_xprt);
  461. if (rc)
  462. return rc;
  463. ep = r_xprt->rx_ep;
  464. xprt_clear_connected(xprt);
  465. rpcrdma_reset_cwnd(r_xprt);
  466. /* Bump the ep's reference count while there are
  467. * outstanding Receives.
  468. */
  469. rpcrdma_ep_get(ep);
  470. rpcrdma_post_recvs(r_xprt, 1, true);
  471. rc = rdma_connect(ep->re_id, &ep->re_remote_cma);
  472. if (rc)
  473. goto out;
  474. if (xprt->reestablish_timeout < RPCRDMA_INIT_REEST_TO)
  475. xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
  476. wait_event_interruptible(ep->re_connect_wait,
  477. ep->re_connect_status != 0);
  478. if (ep->re_connect_status <= 0) {
  479. rc = ep->re_connect_status;
  480. goto out;
  481. }
  482. rc = rpcrdma_sendctxs_create(r_xprt);
  483. if (rc) {
  484. rc = -ENOTCONN;
  485. goto out;
  486. }
  487. rc = rpcrdma_reqs_setup(r_xprt);
  488. if (rc) {
  489. rc = -ENOTCONN;
  490. goto out;
  491. }
  492. rpcrdma_mrs_create(r_xprt);
  493. out:
  494. trace_xprtrdma_connect(r_xprt, rc);
  495. return rc;
  496. }
  497. /**
  498. * rpcrdma_xprt_disconnect - Disconnect underlying transport
  499. * @r_xprt: controlling transport instance
  500. *
  501. * Caller serializes. Either the transport send lock is held,
  502. * or we're being called to destroy the transport.
  503. *
  504. * On return, @r_xprt is completely divested of all hardware
  505. * resources and prepared for the next ->connect operation.
  506. */
  507. void rpcrdma_xprt_disconnect(struct rpcrdma_xprt *r_xprt)
  508. {
  509. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  510. struct rdma_cm_id *id;
  511. int rc;
  512. if (!ep)
  513. return;
  514. id = ep->re_id;
  515. rc = rdma_disconnect(id);
  516. trace_xprtrdma_disconnect(r_xprt, rc);
  517. rpcrdma_xprt_drain(r_xprt);
  518. rpcrdma_reps_unmap(r_xprt);
  519. rpcrdma_reqs_reset(r_xprt);
  520. rpcrdma_mrs_destroy(r_xprt);
  521. rpcrdma_sendctxs_destroy(r_xprt);
  522. if (rpcrdma_ep_put(ep))
  523. rdma_destroy_id(id);
  524. r_xprt->rx_ep = NULL;
  525. }
  526. /* Fixed-size circular FIFO queue. This implementation is wait-free and
  527. * lock-free.
  528. *
  529. * Consumer is the code path that posts Sends. This path dequeues a
  530. * sendctx for use by a Send operation. Multiple consumer threads
  531. * are serialized by the RPC transport lock, which allows only one
  532. * ->send_request call at a time.
  533. *
  534. * Producer is the code path that handles Send completions. This path
  535. * enqueues a sendctx that has been completed. Multiple producer
  536. * threads are serialized by the ib_poll_cq() function.
  537. */
  538. /* rpcrdma_sendctxs_destroy() assumes caller has already quiesced
  539. * queue activity, and rpcrdma_xprt_drain has flushed all remaining
  540. * Send requests.
  541. */
  542. static void rpcrdma_sendctxs_destroy(struct rpcrdma_xprt *r_xprt)
  543. {
  544. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  545. unsigned long i;
  546. if (!buf->rb_sc_ctxs)
  547. return;
  548. for (i = 0; i <= buf->rb_sc_last; i++)
  549. kfree(buf->rb_sc_ctxs[i]);
  550. kfree(buf->rb_sc_ctxs);
  551. buf->rb_sc_ctxs = NULL;
  552. }
  553. static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ep *ep)
  554. {
  555. struct rpcrdma_sendctx *sc;
  556. sc = kzalloc(struct_size(sc, sc_sges, ep->re_attr.cap.max_send_sge),
  557. GFP_KERNEL);
  558. if (!sc)
  559. return NULL;
  560. sc->sc_cqe.done = rpcrdma_wc_send;
  561. return sc;
  562. }
  563. static int rpcrdma_sendctxs_create(struct rpcrdma_xprt *r_xprt)
  564. {
  565. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  566. struct rpcrdma_sendctx *sc;
  567. unsigned long i;
  568. /* Maximum number of concurrent outstanding Send WRs. Capping
  569. * the circular queue size stops Send Queue overflow by causing
  570. * the ->send_request call to fail temporarily before too many
  571. * Sends are posted.
  572. */
  573. i = r_xprt->rx_ep->re_max_requests + RPCRDMA_MAX_BC_REQUESTS;
  574. buf->rb_sc_ctxs = kcalloc(i, sizeof(sc), GFP_KERNEL);
  575. if (!buf->rb_sc_ctxs)
  576. return -ENOMEM;
  577. buf->rb_sc_last = i - 1;
  578. for (i = 0; i <= buf->rb_sc_last; i++) {
  579. sc = rpcrdma_sendctx_create(r_xprt->rx_ep);
  580. if (!sc)
  581. return -ENOMEM;
  582. buf->rb_sc_ctxs[i] = sc;
  583. }
  584. buf->rb_sc_head = 0;
  585. buf->rb_sc_tail = 0;
  586. return 0;
  587. }
  588. /* The sendctx queue is not guaranteed to have a size that is a
  589. * power of two, thus the helpers in circ_buf.h cannot be used.
  590. * The other option is to use modulus (%), which can be expensive.
  591. */
  592. static unsigned long rpcrdma_sendctx_next(struct rpcrdma_buffer *buf,
  593. unsigned long item)
  594. {
  595. return likely(item < buf->rb_sc_last) ? item + 1 : 0;
  596. }
  597. /**
  598. * rpcrdma_sendctx_get_locked - Acquire a send context
  599. * @r_xprt: controlling transport instance
  600. *
  601. * Returns pointer to a free send completion context; or NULL if
  602. * the queue is empty.
  603. *
  604. * Usage: Called to acquire an SGE array before preparing a Send WR.
  605. *
  606. * The caller serializes calls to this function (per transport), and
  607. * provides an effective memory barrier that flushes the new value
  608. * of rb_sc_head.
  609. */
  610. struct rpcrdma_sendctx *rpcrdma_sendctx_get_locked(struct rpcrdma_xprt *r_xprt)
  611. {
  612. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  613. struct rpcrdma_sendctx *sc;
  614. unsigned long next_head;
  615. next_head = rpcrdma_sendctx_next(buf, buf->rb_sc_head);
  616. if (next_head == READ_ONCE(buf->rb_sc_tail))
  617. goto out_emptyq;
  618. /* ORDER: item must be accessed _before_ head is updated */
  619. sc = buf->rb_sc_ctxs[next_head];
  620. /* Releasing the lock in the caller acts as a memory
  621. * barrier that flushes rb_sc_head.
  622. */
  623. buf->rb_sc_head = next_head;
  624. return sc;
  625. out_emptyq:
  626. /* The queue is "empty" if there have not been enough Send
  627. * completions recently. This is a sign the Send Queue is
  628. * backing up. Cause the caller to pause and try again.
  629. */
  630. xprt_wait_for_buffer_space(&r_xprt->rx_xprt);
  631. r_xprt->rx_stats.empty_sendctx_q++;
  632. return NULL;
  633. }
  634. /**
  635. * rpcrdma_sendctx_put_locked - Release a send context
  636. * @r_xprt: controlling transport instance
  637. * @sc: send context to release
  638. *
  639. * Usage: Called from Send completion to return a sendctxt
  640. * to the queue.
  641. *
  642. * The caller serializes calls to this function (per transport).
  643. */
  644. static void rpcrdma_sendctx_put_locked(struct rpcrdma_xprt *r_xprt,
  645. struct rpcrdma_sendctx *sc)
  646. {
  647. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  648. unsigned long next_tail;
  649. /* Unmap SGEs of previously completed but unsignaled
  650. * Sends by walking up the queue until @sc is found.
  651. */
  652. next_tail = buf->rb_sc_tail;
  653. do {
  654. next_tail = rpcrdma_sendctx_next(buf, next_tail);
  655. /* ORDER: item must be accessed _before_ tail is updated */
  656. rpcrdma_sendctx_unmap(buf->rb_sc_ctxs[next_tail]);
  657. } while (buf->rb_sc_ctxs[next_tail] != sc);
  658. /* Paired with READ_ONCE */
  659. smp_store_release(&buf->rb_sc_tail, next_tail);
  660. xprt_write_space(&r_xprt->rx_xprt);
  661. }
  662. static void
  663. rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt)
  664. {
  665. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  666. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  667. unsigned int count;
  668. for (count = 0; count < ep->re_max_rdma_segs; count++) {
  669. struct rpcrdma_mr *mr;
  670. int rc;
  671. mr = kzalloc(sizeof(*mr), GFP_NOFS);
  672. if (!mr)
  673. break;
  674. rc = frwr_mr_init(r_xprt, mr);
  675. if (rc) {
  676. kfree(mr);
  677. break;
  678. }
  679. spin_lock(&buf->rb_lock);
  680. rpcrdma_mr_push(mr, &buf->rb_mrs);
  681. list_add(&mr->mr_all, &buf->rb_all_mrs);
  682. spin_unlock(&buf->rb_lock);
  683. }
  684. r_xprt->rx_stats.mrs_allocated += count;
  685. trace_xprtrdma_createmrs(r_xprt, count);
  686. }
  687. static void
  688. rpcrdma_mr_refresh_worker(struct work_struct *work)
  689. {
  690. struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
  691. rb_refresh_worker);
  692. struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
  693. rx_buf);
  694. rpcrdma_mrs_create(r_xprt);
  695. xprt_write_space(&r_xprt->rx_xprt);
  696. }
  697. /**
  698. * rpcrdma_mrs_refresh - Wake the MR refresh worker
  699. * @r_xprt: controlling transport instance
  700. *
  701. */
  702. void rpcrdma_mrs_refresh(struct rpcrdma_xprt *r_xprt)
  703. {
  704. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  705. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  706. /* If there is no underlying connection, it's no use
  707. * to wake the refresh worker.
  708. */
  709. if (ep->re_connect_status == 1) {
  710. /* The work is scheduled on a WQ_MEM_RECLAIM
  711. * workqueue in order to prevent MR allocation
  712. * from recursing into NFS during direct reclaim.
  713. */
  714. queue_work(xprtiod_workqueue, &buf->rb_refresh_worker);
  715. }
  716. }
  717. /**
  718. * rpcrdma_req_create - Allocate an rpcrdma_req object
  719. * @r_xprt: controlling r_xprt
  720. * @size: initial size, in bytes, of send and receive buffers
  721. * @flags: GFP flags passed to memory allocators
  722. *
  723. * Returns an allocated and fully initialized rpcrdma_req or NULL.
  724. */
  725. struct rpcrdma_req *rpcrdma_req_create(struct rpcrdma_xprt *r_xprt, size_t size,
  726. gfp_t flags)
  727. {
  728. struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
  729. struct rpcrdma_req *req;
  730. req = kzalloc(sizeof(*req), flags);
  731. if (req == NULL)
  732. goto out1;
  733. req->rl_sendbuf = rpcrdma_regbuf_alloc(size, DMA_TO_DEVICE, flags);
  734. if (!req->rl_sendbuf)
  735. goto out2;
  736. req->rl_recvbuf = rpcrdma_regbuf_alloc(size, DMA_NONE, flags);
  737. if (!req->rl_recvbuf)
  738. goto out3;
  739. INIT_LIST_HEAD(&req->rl_free_mrs);
  740. INIT_LIST_HEAD(&req->rl_registered);
  741. spin_lock(&buffer->rb_lock);
  742. list_add(&req->rl_all, &buffer->rb_allreqs);
  743. spin_unlock(&buffer->rb_lock);
  744. return req;
  745. out3:
  746. kfree(req->rl_sendbuf);
  747. out2:
  748. kfree(req);
  749. out1:
  750. return NULL;
  751. }
  752. /**
  753. * rpcrdma_req_setup - Per-connection instance setup of an rpcrdma_req object
  754. * @r_xprt: controlling transport instance
  755. * @req: rpcrdma_req object to set up
  756. *
  757. * Returns zero on success, and a negative errno on failure.
  758. */
  759. int rpcrdma_req_setup(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
  760. {
  761. struct rpcrdma_regbuf *rb;
  762. size_t maxhdrsize;
  763. /* Compute maximum header buffer size in bytes */
  764. maxhdrsize = rpcrdma_fixed_maxsz + 3 +
  765. r_xprt->rx_ep->re_max_rdma_segs * rpcrdma_readchunk_maxsz;
  766. maxhdrsize *= sizeof(__be32);
  767. rb = rpcrdma_regbuf_alloc(__roundup_pow_of_two(maxhdrsize),
  768. DMA_TO_DEVICE, GFP_KERNEL);
  769. if (!rb)
  770. goto out;
  771. if (!__rpcrdma_regbuf_dma_map(r_xprt, rb))
  772. goto out_free;
  773. req->rl_rdmabuf = rb;
  774. xdr_buf_init(&req->rl_hdrbuf, rdmab_data(rb), rdmab_length(rb));
  775. return 0;
  776. out_free:
  777. rpcrdma_regbuf_free(rb);
  778. out:
  779. return -ENOMEM;
  780. }
  781. /* ASSUMPTION: the rb_allreqs list is stable for the duration,
  782. * and thus can be walked without holding rb_lock. Eg. the
  783. * caller is holding the transport send lock to exclude
  784. * device removal or disconnection.
  785. */
  786. static int rpcrdma_reqs_setup(struct rpcrdma_xprt *r_xprt)
  787. {
  788. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  789. struct rpcrdma_req *req;
  790. int rc;
  791. list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
  792. rc = rpcrdma_req_setup(r_xprt, req);
  793. if (rc)
  794. return rc;
  795. }
  796. return 0;
  797. }
  798. static void rpcrdma_req_reset(struct rpcrdma_req *req)
  799. {
  800. /* Credits are valid for only one connection */
  801. req->rl_slot.rq_cong = 0;
  802. rpcrdma_regbuf_free(req->rl_rdmabuf);
  803. req->rl_rdmabuf = NULL;
  804. rpcrdma_regbuf_dma_unmap(req->rl_sendbuf);
  805. rpcrdma_regbuf_dma_unmap(req->rl_recvbuf);
  806. frwr_reset(req);
  807. }
  808. /* ASSUMPTION: the rb_allreqs list is stable for the duration,
  809. * and thus can be walked without holding rb_lock. Eg. the
  810. * caller is holding the transport send lock to exclude
  811. * device removal or disconnection.
  812. */
  813. static void rpcrdma_reqs_reset(struct rpcrdma_xprt *r_xprt)
  814. {
  815. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  816. struct rpcrdma_req *req;
  817. list_for_each_entry(req, &buf->rb_allreqs, rl_all)
  818. rpcrdma_req_reset(req);
  819. }
  820. /* No locking needed here. This function is called only by the
  821. * Receive completion handler.
  822. */
  823. static noinline
  824. struct rpcrdma_rep *rpcrdma_rep_create(struct rpcrdma_xprt *r_xprt,
  825. bool temp)
  826. {
  827. struct rpcrdma_rep *rep;
  828. rep = kzalloc(sizeof(*rep), GFP_KERNEL);
  829. if (rep == NULL)
  830. goto out;
  831. rep->rr_rdmabuf = rpcrdma_regbuf_alloc(r_xprt->rx_ep->re_inline_recv,
  832. DMA_FROM_DEVICE, GFP_KERNEL);
  833. if (!rep->rr_rdmabuf)
  834. goto out_free;
  835. if (!rpcrdma_regbuf_dma_map(r_xprt, rep->rr_rdmabuf))
  836. goto out_free_regbuf;
  837. xdr_buf_init(&rep->rr_hdrbuf, rdmab_data(rep->rr_rdmabuf),
  838. rdmab_length(rep->rr_rdmabuf));
  839. rep->rr_cqe.done = rpcrdma_wc_receive;
  840. rep->rr_rxprt = r_xprt;
  841. rep->rr_recv_wr.next = NULL;
  842. rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
  843. rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
  844. rep->rr_recv_wr.num_sge = 1;
  845. rep->rr_temp = temp;
  846. list_add(&rep->rr_all, &r_xprt->rx_buf.rb_all_reps);
  847. return rep;
  848. out_free_regbuf:
  849. rpcrdma_regbuf_free(rep->rr_rdmabuf);
  850. out_free:
  851. kfree(rep);
  852. out:
  853. return NULL;
  854. }
  855. /* No locking needed here. This function is invoked only by the
  856. * Receive completion handler, or during transport shutdown.
  857. */
  858. static void rpcrdma_rep_destroy(struct rpcrdma_rep *rep)
  859. {
  860. list_del(&rep->rr_all);
  861. rpcrdma_regbuf_free(rep->rr_rdmabuf);
  862. kfree(rep);
  863. }
  864. static struct rpcrdma_rep *rpcrdma_rep_get_locked(struct rpcrdma_buffer *buf)
  865. {
  866. struct llist_node *node;
  867. /* Calls to llist_del_first are required to be serialized */
  868. node = llist_del_first(&buf->rb_free_reps);
  869. if (!node)
  870. return NULL;
  871. return llist_entry(node, struct rpcrdma_rep, rr_node);
  872. }
  873. static void rpcrdma_rep_put(struct rpcrdma_buffer *buf,
  874. struct rpcrdma_rep *rep)
  875. {
  876. llist_add(&rep->rr_node, &buf->rb_free_reps);
  877. }
  878. static void rpcrdma_reps_unmap(struct rpcrdma_xprt *r_xprt)
  879. {
  880. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  881. struct rpcrdma_rep *rep;
  882. list_for_each_entry(rep, &buf->rb_all_reps, rr_all) {
  883. rpcrdma_regbuf_dma_unmap(rep->rr_rdmabuf);
  884. rep->rr_temp = true;
  885. }
  886. }
  887. static void rpcrdma_reps_destroy(struct rpcrdma_buffer *buf)
  888. {
  889. struct rpcrdma_rep *rep;
  890. while ((rep = rpcrdma_rep_get_locked(buf)) != NULL)
  891. rpcrdma_rep_destroy(rep);
  892. }
  893. /**
  894. * rpcrdma_buffer_create - Create initial set of req/rep objects
  895. * @r_xprt: transport instance to (re)initialize
  896. *
  897. * Returns zero on success, otherwise a negative errno.
  898. */
  899. int rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
  900. {
  901. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  902. int i, rc;
  903. buf->rb_bc_srv_max_requests = 0;
  904. spin_lock_init(&buf->rb_lock);
  905. INIT_LIST_HEAD(&buf->rb_mrs);
  906. INIT_LIST_HEAD(&buf->rb_all_mrs);
  907. INIT_WORK(&buf->rb_refresh_worker, rpcrdma_mr_refresh_worker);
  908. INIT_LIST_HEAD(&buf->rb_send_bufs);
  909. INIT_LIST_HEAD(&buf->rb_allreqs);
  910. INIT_LIST_HEAD(&buf->rb_all_reps);
  911. rc = -ENOMEM;
  912. for (i = 0; i < r_xprt->rx_xprt.max_reqs; i++) {
  913. struct rpcrdma_req *req;
  914. req = rpcrdma_req_create(r_xprt, RPCRDMA_V1_DEF_INLINE_SIZE * 2,
  915. GFP_KERNEL);
  916. if (!req)
  917. goto out;
  918. list_add(&req->rl_list, &buf->rb_send_bufs);
  919. }
  920. init_llist_head(&buf->rb_free_reps);
  921. return 0;
  922. out:
  923. rpcrdma_buffer_destroy(buf);
  924. return rc;
  925. }
  926. /**
  927. * rpcrdma_req_destroy - Destroy an rpcrdma_req object
  928. * @req: unused object to be destroyed
  929. *
  930. * Relies on caller holding the transport send lock to protect
  931. * removing req->rl_all from buf->rb_all_reqs safely.
  932. */
  933. void rpcrdma_req_destroy(struct rpcrdma_req *req)
  934. {
  935. struct rpcrdma_mr *mr;
  936. list_del(&req->rl_all);
  937. while ((mr = rpcrdma_mr_pop(&req->rl_free_mrs))) {
  938. struct rpcrdma_buffer *buf = &mr->mr_xprt->rx_buf;
  939. spin_lock(&buf->rb_lock);
  940. list_del(&mr->mr_all);
  941. spin_unlock(&buf->rb_lock);
  942. frwr_release_mr(mr);
  943. }
  944. rpcrdma_regbuf_free(req->rl_recvbuf);
  945. rpcrdma_regbuf_free(req->rl_sendbuf);
  946. rpcrdma_regbuf_free(req->rl_rdmabuf);
  947. kfree(req);
  948. }
  949. /**
  950. * rpcrdma_mrs_destroy - Release all of a transport's MRs
  951. * @r_xprt: controlling transport instance
  952. *
  953. * Relies on caller holding the transport send lock to protect
  954. * removing mr->mr_list from req->rl_free_mrs safely.
  955. */
  956. static void rpcrdma_mrs_destroy(struct rpcrdma_xprt *r_xprt)
  957. {
  958. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  959. struct rpcrdma_mr *mr;
  960. cancel_work_sync(&buf->rb_refresh_worker);
  961. spin_lock(&buf->rb_lock);
  962. while ((mr = list_first_entry_or_null(&buf->rb_all_mrs,
  963. struct rpcrdma_mr,
  964. mr_all)) != NULL) {
  965. list_del(&mr->mr_list);
  966. list_del(&mr->mr_all);
  967. spin_unlock(&buf->rb_lock);
  968. frwr_release_mr(mr);
  969. spin_lock(&buf->rb_lock);
  970. }
  971. spin_unlock(&buf->rb_lock);
  972. }
  973. /**
  974. * rpcrdma_buffer_destroy - Release all hw resources
  975. * @buf: root control block for resources
  976. *
  977. * ORDERING: relies on a prior rpcrdma_xprt_drain :
  978. * - No more Send or Receive completions can occur
  979. * - All MRs, reps, and reqs are returned to their free lists
  980. */
  981. void
  982. rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
  983. {
  984. rpcrdma_reps_destroy(buf);
  985. while (!list_empty(&buf->rb_send_bufs)) {
  986. struct rpcrdma_req *req;
  987. req = list_first_entry(&buf->rb_send_bufs,
  988. struct rpcrdma_req, rl_list);
  989. list_del(&req->rl_list);
  990. rpcrdma_req_destroy(req);
  991. }
  992. }
  993. /**
  994. * rpcrdma_mr_get - Allocate an rpcrdma_mr object
  995. * @r_xprt: controlling transport
  996. *
  997. * Returns an initialized rpcrdma_mr or NULL if no free
  998. * rpcrdma_mr objects are available.
  999. */
  1000. struct rpcrdma_mr *
  1001. rpcrdma_mr_get(struct rpcrdma_xprt *r_xprt)
  1002. {
  1003. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  1004. struct rpcrdma_mr *mr;
  1005. spin_lock(&buf->rb_lock);
  1006. mr = rpcrdma_mr_pop(&buf->rb_mrs);
  1007. spin_unlock(&buf->rb_lock);
  1008. return mr;
  1009. }
  1010. /**
  1011. * rpcrdma_mr_put - DMA unmap an MR and release it
  1012. * @mr: MR to release
  1013. *
  1014. */
  1015. void rpcrdma_mr_put(struct rpcrdma_mr *mr)
  1016. {
  1017. struct rpcrdma_xprt *r_xprt = mr->mr_xprt;
  1018. if (mr->mr_dir != DMA_NONE) {
  1019. trace_xprtrdma_mr_unmap(mr);
  1020. ib_dma_unmap_sg(r_xprt->rx_ep->re_id->device,
  1021. mr->mr_sg, mr->mr_nents, mr->mr_dir);
  1022. mr->mr_dir = DMA_NONE;
  1023. }
  1024. rpcrdma_mr_push(mr, &mr->mr_req->rl_free_mrs);
  1025. }
  1026. /**
  1027. * rpcrdma_reply_put - Put reply buffers back into pool
  1028. * @buffers: buffer pool
  1029. * @req: object to return
  1030. *
  1031. */
  1032. void rpcrdma_reply_put(struct rpcrdma_buffer *buffers, struct rpcrdma_req *req)
  1033. {
  1034. if (req->rl_reply) {
  1035. rpcrdma_rep_put(buffers, req->rl_reply);
  1036. req->rl_reply = NULL;
  1037. }
  1038. }
  1039. /**
  1040. * rpcrdma_buffer_get - Get a request buffer
  1041. * @buffers: Buffer pool from which to obtain a buffer
  1042. *
  1043. * Returns a fresh rpcrdma_req, or NULL if none are available.
  1044. */
  1045. struct rpcrdma_req *
  1046. rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
  1047. {
  1048. struct rpcrdma_req *req;
  1049. spin_lock(&buffers->rb_lock);
  1050. req = list_first_entry_or_null(&buffers->rb_send_bufs,
  1051. struct rpcrdma_req, rl_list);
  1052. if (req)
  1053. list_del_init(&req->rl_list);
  1054. spin_unlock(&buffers->rb_lock);
  1055. return req;
  1056. }
  1057. /**
  1058. * rpcrdma_buffer_put - Put request/reply buffers back into pool
  1059. * @buffers: buffer pool
  1060. * @req: object to return
  1061. *
  1062. */
  1063. void rpcrdma_buffer_put(struct rpcrdma_buffer *buffers, struct rpcrdma_req *req)
  1064. {
  1065. rpcrdma_reply_put(buffers, req);
  1066. spin_lock(&buffers->rb_lock);
  1067. list_add(&req->rl_list, &buffers->rb_send_bufs);
  1068. spin_unlock(&buffers->rb_lock);
  1069. }
  1070. /**
  1071. * rpcrdma_recv_buffer_put - Release rpcrdma_rep back to free list
  1072. * @rep: rep to release
  1073. *
  1074. * Used after error conditions.
  1075. */
  1076. void rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
  1077. {
  1078. rpcrdma_rep_put(&rep->rr_rxprt->rx_buf, rep);
  1079. }
  1080. /* Returns a pointer to a rpcrdma_regbuf object, or NULL.
  1081. *
  1082. * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
  1083. * receiving the payload of RDMA RECV operations. During Long Calls
  1084. * or Replies they may be registered externally via frwr_map.
  1085. */
  1086. static struct rpcrdma_regbuf *
  1087. rpcrdma_regbuf_alloc(size_t size, enum dma_data_direction direction,
  1088. gfp_t flags)
  1089. {
  1090. struct rpcrdma_regbuf *rb;
  1091. rb = kmalloc(sizeof(*rb), flags);
  1092. if (!rb)
  1093. return NULL;
  1094. rb->rg_data = kmalloc(size, flags);
  1095. if (!rb->rg_data) {
  1096. kfree(rb);
  1097. return NULL;
  1098. }
  1099. rb->rg_device = NULL;
  1100. rb->rg_direction = direction;
  1101. rb->rg_iov.length = size;
  1102. return rb;
  1103. }
  1104. /**
  1105. * rpcrdma_regbuf_realloc - re-allocate a SEND/RECV buffer
  1106. * @rb: regbuf to reallocate
  1107. * @size: size of buffer to be allocated, in bytes
  1108. * @flags: GFP flags
  1109. *
  1110. * Returns true if reallocation was successful. If false is
  1111. * returned, @rb is left untouched.
  1112. */
  1113. bool rpcrdma_regbuf_realloc(struct rpcrdma_regbuf *rb, size_t size, gfp_t flags)
  1114. {
  1115. void *buf;
  1116. buf = kmalloc(size, flags);
  1117. if (!buf)
  1118. return false;
  1119. rpcrdma_regbuf_dma_unmap(rb);
  1120. kfree(rb->rg_data);
  1121. rb->rg_data = buf;
  1122. rb->rg_iov.length = size;
  1123. return true;
  1124. }
  1125. /**
  1126. * __rpcrdma_regbuf_dma_map - DMA-map a regbuf
  1127. * @r_xprt: controlling transport instance
  1128. * @rb: regbuf to be mapped
  1129. *
  1130. * Returns true if the buffer is now DMA mapped to @r_xprt's device
  1131. */
  1132. bool __rpcrdma_regbuf_dma_map(struct rpcrdma_xprt *r_xprt,
  1133. struct rpcrdma_regbuf *rb)
  1134. {
  1135. struct ib_device *device = r_xprt->rx_ep->re_id->device;
  1136. if (rb->rg_direction == DMA_NONE)
  1137. return false;
  1138. rb->rg_iov.addr = ib_dma_map_single(device, rdmab_data(rb),
  1139. rdmab_length(rb), rb->rg_direction);
  1140. if (ib_dma_mapping_error(device, rdmab_addr(rb))) {
  1141. trace_xprtrdma_dma_maperr(rdmab_addr(rb));
  1142. return false;
  1143. }
  1144. rb->rg_device = device;
  1145. rb->rg_iov.lkey = r_xprt->rx_ep->re_pd->local_dma_lkey;
  1146. return true;
  1147. }
  1148. static void rpcrdma_regbuf_dma_unmap(struct rpcrdma_regbuf *rb)
  1149. {
  1150. if (!rb)
  1151. return;
  1152. if (!rpcrdma_regbuf_is_mapped(rb))
  1153. return;
  1154. ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb), rdmab_length(rb),
  1155. rb->rg_direction);
  1156. rb->rg_device = NULL;
  1157. }
  1158. static void rpcrdma_regbuf_free(struct rpcrdma_regbuf *rb)
  1159. {
  1160. rpcrdma_regbuf_dma_unmap(rb);
  1161. if (rb)
  1162. kfree(rb->rg_data);
  1163. kfree(rb);
  1164. }
  1165. /**
  1166. * rpcrdma_post_sends - Post WRs to a transport's Send Queue
  1167. * @r_xprt: controlling transport instance
  1168. * @req: rpcrdma_req containing the Send WR to post
  1169. *
  1170. * Returns 0 if the post was successful, otherwise -ENOTCONN
  1171. * is returned.
  1172. */
  1173. int rpcrdma_post_sends(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
  1174. {
  1175. struct ib_send_wr *send_wr = &req->rl_wr;
  1176. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  1177. int rc;
  1178. if (!ep->re_send_count || kref_read(&req->rl_kref) > 1) {
  1179. send_wr->send_flags |= IB_SEND_SIGNALED;
  1180. ep->re_send_count = ep->re_send_batch;
  1181. } else {
  1182. send_wr->send_flags &= ~IB_SEND_SIGNALED;
  1183. --ep->re_send_count;
  1184. }
  1185. trace_xprtrdma_post_send(req);
  1186. rc = frwr_send(r_xprt, req);
  1187. if (rc)
  1188. return -ENOTCONN;
  1189. return 0;
  1190. }
  1191. /**
  1192. * rpcrdma_post_recvs - Refill the Receive Queue
  1193. * @r_xprt: controlling transport instance
  1194. * @needed: current credit grant
  1195. * @temp: mark Receive buffers to be deleted after one use
  1196. *
  1197. */
  1198. void rpcrdma_post_recvs(struct rpcrdma_xprt *r_xprt, int needed, bool temp)
  1199. {
  1200. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  1201. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  1202. struct ib_recv_wr *wr, *bad_wr;
  1203. struct rpcrdma_rep *rep;
  1204. int count, rc;
  1205. rc = 0;
  1206. count = 0;
  1207. if (likely(ep->re_receive_count > needed))
  1208. goto out;
  1209. needed -= ep->re_receive_count;
  1210. if (!temp)
  1211. needed += RPCRDMA_MAX_RECV_BATCH;
  1212. /* fast path: all needed reps can be found on the free list */
  1213. wr = NULL;
  1214. while (needed) {
  1215. rep = rpcrdma_rep_get_locked(buf);
  1216. if (rep && rep->rr_temp) {
  1217. rpcrdma_rep_destroy(rep);
  1218. continue;
  1219. }
  1220. if (!rep)
  1221. rep = rpcrdma_rep_create(r_xprt, temp);
  1222. if (!rep)
  1223. break;
  1224. trace_xprtrdma_post_recv(rep);
  1225. rep->rr_recv_wr.next = wr;
  1226. wr = &rep->rr_recv_wr;
  1227. --needed;
  1228. ++count;
  1229. }
  1230. if (!wr)
  1231. goto out;
  1232. rc = ib_post_recv(ep->re_id->qp, wr,
  1233. (const struct ib_recv_wr **)&bad_wr);
  1234. out:
  1235. trace_xprtrdma_post_recvs(r_xprt, count, rc);
  1236. if (rc) {
  1237. for (wr = bad_wr; wr;) {
  1238. struct rpcrdma_rep *rep;
  1239. rep = container_of(wr, struct rpcrdma_rep, rr_recv_wr);
  1240. wr = wr->next;
  1241. rpcrdma_recv_buffer_put(rep);
  1242. --count;
  1243. }
  1244. }
  1245. ep->re_receive_count += count;
  1246. return;
  1247. }