verbs.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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 <asm-generic/barrier.h>
  55. #include <asm/bitops.h>
  56. #include <rdma/ib_cm.h>
  57. #include "xprt_rdma.h"
  58. #include <trace/events/rpcrdma.h>
  59. /*
  60. * Globals/Macros
  61. */
  62. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  63. # define RPCDBG_FACILITY RPCDBG_TRANS
  64. #endif
  65. /*
  66. * internal functions
  67. */
  68. static void rpcrdma_sendctx_put_locked(struct rpcrdma_sendctx *sc);
  69. static void rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt);
  70. static void rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf);
  71. static int rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt, bool temp);
  72. static void rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb);
  73. struct workqueue_struct *rpcrdma_receive_wq __read_mostly;
  74. int
  75. rpcrdma_alloc_wq(void)
  76. {
  77. struct workqueue_struct *recv_wq;
  78. recv_wq = alloc_workqueue("xprtrdma_receive",
  79. WQ_MEM_RECLAIM | WQ_HIGHPRI,
  80. 0);
  81. if (!recv_wq)
  82. return -ENOMEM;
  83. rpcrdma_receive_wq = recv_wq;
  84. return 0;
  85. }
  86. void
  87. rpcrdma_destroy_wq(void)
  88. {
  89. struct workqueue_struct *wq;
  90. if (rpcrdma_receive_wq) {
  91. wq = rpcrdma_receive_wq;
  92. rpcrdma_receive_wq = NULL;
  93. destroy_workqueue(wq);
  94. }
  95. }
  96. static void
  97. rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
  98. {
  99. struct rpcrdma_ep *ep = context;
  100. struct rpcrdma_xprt *r_xprt = container_of(ep, struct rpcrdma_xprt,
  101. rx_ep);
  102. trace_xprtrdma_qp_error(r_xprt, event);
  103. pr_err("rpcrdma: %s on device %s ep %p\n",
  104. ib_event_msg(event->event), event->device->name, context);
  105. if (ep->rep_connected == 1) {
  106. ep->rep_connected = -EIO;
  107. rpcrdma_conn_func(ep);
  108. wake_up_all(&ep->rep_connect_wait);
  109. }
  110. }
  111. /**
  112. * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
  113. * @cq: completion queue (ignored)
  114. * @wc: completed WR
  115. *
  116. */
  117. static void
  118. rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
  119. {
  120. struct ib_cqe *cqe = wc->wr_cqe;
  121. struct rpcrdma_sendctx *sc =
  122. container_of(cqe, struct rpcrdma_sendctx, sc_cqe);
  123. /* WARNING: Only wr_cqe and status are reliable at this point */
  124. trace_xprtrdma_wc_send(sc, wc);
  125. if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
  126. pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
  127. ib_wc_status_msg(wc->status),
  128. wc->status, wc->vendor_err);
  129. rpcrdma_sendctx_put_locked(sc);
  130. }
  131. /**
  132. * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
  133. * @cq: completion queue (ignored)
  134. * @wc: completed WR
  135. *
  136. */
  137. static void
  138. rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
  139. {
  140. struct ib_cqe *cqe = wc->wr_cqe;
  141. struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
  142. rr_cqe);
  143. /* WARNING: Only wr_id and status are reliable at this point */
  144. trace_xprtrdma_wc_receive(wc);
  145. if (wc->status != IB_WC_SUCCESS)
  146. goto out_fail;
  147. /* status == SUCCESS means all fields in wc are trustworthy */
  148. rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len);
  149. rep->rr_wc_flags = wc->wc_flags;
  150. rep->rr_inv_rkey = wc->ex.invalidate_rkey;
  151. ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
  152. rdmab_addr(rep->rr_rdmabuf),
  153. wc->byte_len, DMA_FROM_DEVICE);
  154. out_schedule:
  155. rpcrdma_reply_handler(rep);
  156. return;
  157. out_fail:
  158. if (wc->status != IB_WC_WR_FLUSH_ERR)
  159. pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
  160. ib_wc_status_msg(wc->status),
  161. wc->status, wc->vendor_err);
  162. rpcrdma_set_xdrlen(&rep->rr_hdrbuf, 0);
  163. goto out_schedule;
  164. }
  165. static void
  166. rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
  167. struct rdma_conn_param *param)
  168. {
  169. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  170. const struct rpcrdma_connect_private *pmsg = param->private_data;
  171. unsigned int rsize, wsize;
  172. /* Default settings for RPC-over-RDMA Version One */
  173. r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
  174. rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
  175. wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
  176. if (pmsg &&
  177. pmsg->cp_magic == rpcrdma_cmp_magic &&
  178. pmsg->cp_version == RPCRDMA_CMP_VERSION) {
  179. r_xprt->rx_ia.ri_implicit_roundup = true;
  180. rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
  181. wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
  182. }
  183. if (rsize < cdata->inline_rsize)
  184. cdata->inline_rsize = rsize;
  185. if (wsize < cdata->inline_wsize)
  186. cdata->inline_wsize = wsize;
  187. dprintk("RPC: %s: max send %u, max recv %u\n",
  188. __func__, cdata->inline_wsize, cdata->inline_rsize);
  189. rpcrdma_set_max_header_sizes(r_xprt);
  190. }
  191. static int
  192. rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
  193. {
  194. struct rpcrdma_xprt *xprt = id->context;
  195. struct rpcrdma_ia *ia = &xprt->rx_ia;
  196. struct rpcrdma_ep *ep = &xprt->rx_ep;
  197. int connstate = 0;
  198. trace_xprtrdma_conn_upcall(xprt, event);
  199. switch (event->event) {
  200. case RDMA_CM_EVENT_ADDR_RESOLVED:
  201. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  202. ia->ri_async_rc = 0;
  203. complete(&ia->ri_done);
  204. break;
  205. case RDMA_CM_EVENT_ADDR_ERROR:
  206. ia->ri_async_rc = -EPROTO;
  207. complete(&ia->ri_done);
  208. break;
  209. case RDMA_CM_EVENT_ROUTE_ERROR:
  210. ia->ri_async_rc = -ENETUNREACH;
  211. complete(&ia->ri_done);
  212. break;
  213. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  214. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  215. pr_info("rpcrdma: removing device %s for %s:%s\n",
  216. ia->ri_device->name,
  217. rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt));
  218. #endif
  219. set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
  220. ep->rep_connected = -ENODEV;
  221. xprt_force_disconnect(&xprt->rx_xprt);
  222. wait_for_completion(&ia->ri_remove_done);
  223. ia->ri_id = NULL;
  224. ia->ri_device = NULL;
  225. /* Return 1 to ensure the core destroys the id. */
  226. return 1;
  227. case RDMA_CM_EVENT_ESTABLISHED:
  228. ++xprt->rx_xprt.connect_cookie;
  229. connstate = 1;
  230. rpcrdma_update_connect_private(xprt, &event->param.conn);
  231. goto connected;
  232. case RDMA_CM_EVENT_CONNECT_ERROR:
  233. connstate = -ENOTCONN;
  234. goto connected;
  235. case RDMA_CM_EVENT_UNREACHABLE:
  236. connstate = -ENETUNREACH;
  237. goto connected;
  238. case RDMA_CM_EVENT_REJECTED:
  239. dprintk("rpcrdma: connection to %s:%s rejected: %s\n",
  240. rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt),
  241. rdma_reject_msg(id, event->status));
  242. connstate = -ECONNREFUSED;
  243. if (event->status == IB_CM_REJ_STALE_CONN)
  244. connstate = -EAGAIN;
  245. goto connected;
  246. case RDMA_CM_EVENT_DISCONNECTED:
  247. ++xprt->rx_xprt.connect_cookie;
  248. connstate = -ECONNABORTED;
  249. connected:
  250. xprt->rx_buf.rb_credits = 1;
  251. ep->rep_connected = connstate;
  252. rpcrdma_conn_func(ep);
  253. wake_up_all(&ep->rep_connect_wait);
  254. /*FALLTHROUGH*/
  255. default:
  256. dprintk("RPC: %s: %s:%s on %s/%s (ep 0x%p): %s\n",
  257. __func__,
  258. rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt),
  259. ia->ri_device->name, ia->ri_ops->ro_displayname,
  260. ep, rdma_event_msg(event->event));
  261. break;
  262. }
  263. return 0;
  264. }
  265. static struct rdma_cm_id *
  266. rpcrdma_create_id(struct rpcrdma_xprt *xprt, struct rpcrdma_ia *ia)
  267. {
  268. unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
  269. struct rdma_cm_id *id;
  270. int rc;
  271. trace_xprtrdma_conn_start(xprt);
  272. init_completion(&ia->ri_done);
  273. init_completion(&ia->ri_remove_done);
  274. id = rdma_create_id(xprt->rx_xprt.xprt_net, rpcrdma_conn_upcall,
  275. xprt, RDMA_PS_TCP, IB_QPT_RC);
  276. if (IS_ERR(id)) {
  277. rc = PTR_ERR(id);
  278. dprintk("RPC: %s: rdma_create_id() failed %i\n",
  279. __func__, rc);
  280. return id;
  281. }
  282. ia->ri_async_rc = -ETIMEDOUT;
  283. rc = rdma_resolve_addr(id, NULL,
  284. (struct sockaddr *)&xprt->rx_xprt.addr,
  285. RDMA_RESOLVE_TIMEOUT);
  286. if (rc) {
  287. dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
  288. __func__, rc);
  289. goto out;
  290. }
  291. rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
  292. if (rc < 0) {
  293. trace_xprtrdma_conn_tout(xprt);
  294. goto out;
  295. }
  296. rc = ia->ri_async_rc;
  297. if (rc)
  298. goto out;
  299. ia->ri_async_rc = -ETIMEDOUT;
  300. rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
  301. if (rc) {
  302. dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
  303. __func__, rc);
  304. goto out;
  305. }
  306. rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
  307. if (rc < 0) {
  308. trace_xprtrdma_conn_tout(xprt);
  309. goto out;
  310. }
  311. rc = ia->ri_async_rc;
  312. if (rc)
  313. goto out;
  314. return id;
  315. out:
  316. rdma_destroy_id(id);
  317. return ERR_PTR(rc);
  318. }
  319. /*
  320. * Exported functions.
  321. */
  322. /**
  323. * rpcrdma_ia_open - Open and initialize an Interface Adapter.
  324. * @xprt: transport with IA to (re)initialize
  325. *
  326. * Returns 0 on success, negative errno if an appropriate
  327. * Interface Adapter could not be found and opened.
  328. */
  329. int
  330. rpcrdma_ia_open(struct rpcrdma_xprt *xprt)
  331. {
  332. struct rpcrdma_ia *ia = &xprt->rx_ia;
  333. int rc;
  334. ia->ri_id = rpcrdma_create_id(xprt, ia);
  335. if (IS_ERR(ia->ri_id)) {
  336. rc = PTR_ERR(ia->ri_id);
  337. goto out_err;
  338. }
  339. ia->ri_device = ia->ri_id->device;
  340. ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
  341. if (IS_ERR(ia->ri_pd)) {
  342. rc = PTR_ERR(ia->ri_pd);
  343. pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
  344. goto out_err;
  345. }
  346. switch (xprt_rdma_memreg_strategy) {
  347. case RPCRDMA_FRWR:
  348. if (frwr_is_supported(ia)) {
  349. ia->ri_ops = &rpcrdma_frwr_memreg_ops;
  350. break;
  351. }
  352. /*FALLTHROUGH*/
  353. case RPCRDMA_MTHCAFMR:
  354. if (fmr_is_supported(ia)) {
  355. ia->ri_ops = &rpcrdma_fmr_memreg_ops;
  356. break;
  357. }
  358. /*FALLTHROUGH*/
  359. default:
  360. pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
  361. ia->ri_device->name, xprt_rdma_memreg_strategy);
  362. rc = -EINVAL;
  363. goto out_err;
  364. }
  365. return 0;
  366. out_err:
  367. rpcrdma_ia_close(ia);
  368. return rc;
  369. }
  370. /**
  371. * rpcrdma_ia_remove - Handle device driver unload
  372. * @ia: interface adapter being removed
  373. *
  374. * Divest transport H/W resources associated with this adapter,
  375. * but allow it to be restored later.
  376. */
  377. void
  378. rpcrdma_ia_remove(struct rpcrdma_ia *ia)
  379. {
  380. struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
  381. rx_ia);
  382. struct rpcrdma_ep *ep = &r_xprt->rx_ep;
  383. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  384. struct rpcrdma_req *req;
  385. struct rpcrdma_rep *rep;
  386. cancel_delayed_work_sync(&buf->rb_refresh_worker);
  387. /* This is similar to rpcrdma_ep_destroy, but:
  388. * - Don't cancel the connect worker.
  389. * - Don't call rpcrdma_ep_disconnect, which waits
  390. * for another conn upcall, which will deadlock.
  391. * - rdma_disconnect is unneeded, the underlying
  392. * connection is already gone.
  393. */
  394. if (ia->ri_id->qp) {
  395. ib_drain_qp(ia->ri_id->qp);
  396. rdma_destroy_qp(ia->ri_id);
  397. ia->ri_id->qp = NULL;
  398. }
  399. ib_free_cq(ep->rep_attr.recv_cq);
  400. ep->rep_attr.recv_cq = NULL;
  401. ib_free_cq(ep->rep_attr.send_cq);
  402. ep->rep_attr.send_cq = NULL;
  403. /* The ULP is responsible for ensuring all DMA
  404. * mappings and MRs are gone.
  405. */
  406. list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
  407. rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
  408. list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
  409. rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
  410. rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
  411. rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
  412. }
  413. rpcrdma_mrs_destroy(buf);
  414. ib_dealloc_pd(ia->ri_pd);
  415. ia->ri_pd = NULL;
  416. /* Allow waiters to continue */
  417. complete(&ia->ri_remove_done);
  418. trace_xprtrdma_remove(r_xprt);
  419. }
  420. /**
  421. * rpcrdma_ia_close - Clean up/close an IA.
  422. * @ia: interface adapter to close
  423. *
  424. */
  425. void
  426. rpcrdma_ia_close(struct rpcrdma_ia *ia)
  427. {
  428. if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
  429. if (ia->ri_id->qp)
  430. rdma_destroy_qp(ia->ri_id);
  431. rdma_destroy_id(ia->ri_id);
  432. }
  433. ia->ri_id = NULL;
  434. ia->ri_device = NULL;
  435. /* If the pd is still busy, xprtrdma missed freeing a resource */
  436. if (ia->ri_pd && !IS_ERR(ia->ri_pd))
  437. ib_dealloc_pd(ia->ri_pd);
  438. ia->ri_pd = NULL;
  439. }
  440. /*
  441. * Create unconnected endpoint.
  442. */
  443. int
  444. rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
  445. struct rpcrdma_create_data_internal *cdata)
  446. {
  447. struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
  448. struct ib_cq *sendcq, *recvcq;
  449. unsigned int max_sge;
  450. int rc;
  451. max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
  452. RPCRDMA_MAX_SEND_SGES);
  453. if (max_sge < RPCRDMA_MIN_SEND_SGES) {
  454. pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
  455. return -ENOMEM;
  456. }
  457. ia->ri_max_send_sges = max_sge;
  458. rc = ia->ri_ops->ro_open(ia, ep, cdata);
  459. if (rc)
  460. return rc;
  461. ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
  462. ep->rep_attr.qp_context = ep;
  463. ep->rep_attr.srq = NULL;
  464. ep->rep_attr.cap.max_send_sge = max_sge;
  465. ep->rep_attr.cap.max_recv_sge = 1;
  466. ep->rep_attr.cap.max_inline_data = 0;
  467. ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  468. ep->rep_attr.qp_type = IB_QPT_RC;
  469. ep->rep_attr.port_num = ~0;
  470. dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
  471. "iovs: send %d recv %d\n",
  472. __func__,
  473. ep->rep_attr.cap.max_send_wr,
  474. ep->rep_attr.cap.max_recv_wr,
  475. ep->rep_attr.cap.max_send_sge,
  476. ep->rep_attr.cap.max_recv_sge);
  477. /* set trigger for requesting send completion */
  478. ep->rep_send_batch = min_t(unsigned int, RPCRDMA_MAX_SEND_BATCH,
  479. cdata->max_requests >> 2);
  480. ep->rep_send_count = ep->rep_send_batch;
  481. init_waitqueue_head(&ep->rep_connect_wait);
  482. INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
  483. sendcq = ib_alloc_cq(ia->ri_device, NULL,
  484. ep->rep_attr.cap.max_send_wr + 1,
  485. 1, IB_POLL_WORKQUEUE);
  486. if (IS_ERR(sendcq)) {
  487. rc = PTR_ERR(sendcq);
  488. dprintk("RPC: %s: failed to create send CQ: %i\n",
  489. __func__, rc);
  490. goto out1;
  491. }
  492. recvcq = ib_alloc_cq(ia->ri_device, NULL,
  493. ep->rep_attr.cap.max_recv_wr + 1,
  494. 0, IB_POLL_WORKQUEUE);
  495. if (IS_ERR(recvcq)) {
  496. rc = PTR_ERR(recvcq);
  497. dprintk("RPC: %s: failed to create recv CQ: %i\n",
  498. __func__, rc);
  499. goto out2;
  500. }
  501. ep->rep_attr.send_cq = sendcq;
  502. ep->rep_attr.recv_cq = recvcq;
  503. /* Initialize cma parameters */
  504. memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
  505. /* Prepare RDMA-CM private message */
  506. pmsg->cp_magic = rpcrdma_cmp_magic;
  507. pmsg->cp_version = RPCRDMA_CMP_VERSION;
  508. pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
  509. pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
  510. pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
  511. ep->rep_remote_cma.private_data = pmsg;
  512. ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
  513. /* Client offers RDMA Read but does not initiate */
  514. ep->rep_remote_cma.initiator_depth = 0;
  515. ep->rep_remote_cma.responder_resources =
  516. min_t(int, U8_MAX, ia->ri_device->attrs.max_qp_rd_atom);
  517. /* Limit transport retries so client can detect server
  518. * GID changes quickly. RPC layer handles re-establishing
  519. * transport connection and retransmission.
  520. */
  521. ep->rep_remote_cma.retry_count = 6;
  522. /* RPC-over-RDMA handles its own flow control. In addition,
  523. * make all RNR NAKs visible so we know that RPC-over-RDMA
  524. * flow control is working correctly (no NAKs should be seen).
  525. */
  526. ep->rep_remote_cma.flow_control = 0;
  527. ep->rep_remote_cma.rnr_retry_count = 0;
  528. return 0;
  529. out2:
  530. ib_free_cq(sendcq);
  531. out1:
  532. return rc;
  533. }
  534. /*
  535. * rpcrdma_ep_destroy
  536. *
  537. * Disconnect and destroy endpoint. After this, the only
  538. * valid operations on the ep are to free it (if dynamically
  539. * allocated) or re-create it.
  540. */
  541. void
  542. rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  543. {
  544. cancel_delayed_work_sync(&ep->rep_connect_worker);
  545. if (ia->ri_id && ia->ri_id->qp) {
  546. rpcrdma_ep_disconnect(ep, ia);
  547. rdma_destroy_qp(ia->ri_id);
  548. ia->ri_id->qp = NULL;
  549. }
  550. if (ep->rep_attr.recv_cq)
  551. ib_free_cq(ep->rep_attr.recv_cq);
  552. if (ep->rep_attr.send_cq)
  553. ib_free_cq(ep->rep_attr.send_cq);
  554. }
  555. /* Re-establish a connection after a device removal event.
  556. * Unlike a normal reconnection, a fresh PD and a new set
  557. * of MRs and buffers is needed.
  558. */
  559. static int
  560. rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
  561. struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  562. {
  563. int rc, err;
  564. trace_xprtrdma_reinsert(r_xprt);
  565. rc = -EHOSTUNREACH;
  566. if (rpcrdma_ia_open(r_xprt))
  567. goto out1;
  568. rc = -ENOMEM;
  569. err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
  570. if (err) {
  571. pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
  572. goto out2;
  573. }
  574. rc = -ENETUNREACH;
  575. err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
  576. if (err) {
  577. pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
  578. goto out3;
  579. }
  580. rpcrdma_mrs_create(r_xprt);
  581. return 0;
  582. out3:
  583. rpcrdma_ep_destroy(ep, ia);
  584. out2:
  585. rpcrdma_ia_close(ia);
  586. out1:
  587. return rc;
  588. }
  589. static int
  590. rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
  591. struct rpcrdma_ia *ia)
  592. {
  593. struct rdma_cm_id *id, *old;
  594. int err, rc;
  595. trace_xprtrdma_reconnect(r_xprt);
  596. rpcrdma_ep_disconnect(ep, ia);
  597. rc = -EHOSTUNREACH;
  598. id = rpcrdma_create_id(r_xprt, ia);
  599. if (IS_ERR(id))
  600. goto out;
  601. /* As long as the new ID points to the same device as the
  602. * old ID, we can reuse the transport's existing PD and all
  603. * previously allocated MRs. Also, the same device means
  604. * the transport's previous DMA mappings are still valid.
  605. *
  606. * This is a sanity check only. There should be no way these
  607. * point to two different devices here.
  608. */
  609. old = id;
  610. rc = -ENETUNREACH;
  611. if (ia->ri_device != id->device) {
  612. pr_err("rpcrdma: can't reconnect on different device!\n");
  613. goto out_destroy;
  614. }
  615. err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
  616. if (err) {
  617. dprintk("RPC: %s: rdma_create_qp returned %d\n",
  618. __func__, err);
  619. goto out_destroy;
  620. }
  621. /* Atomically replace the transport's ID and QP. */
  622. rc = 0;
  623. old = ia->ri_id;
  624. ia->ri_id = id;
  625. rdma_destroy_qp(old);
  626. out_destroy:
  627. rdma_destroy_id(old);
  628. out:
  629. return rc;
  630. }
  631. /*
  632. * Connect unconnected endpoint.
  633. */
  634. int
  635. rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  636. {
  637. struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
  638. rx_ia);
  639. int rc;
  640. retry:
  641. switch (ep->rep_connected) {
  642. case 0:
  643. dprintk("RPC: %s: connecting...\n", __func__);
  644. rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
  645. if (rc) {
  646. dprintk("RPC: %s: rdma_create_qp failed %i\n",
  647. __func__, rc);
  648. rc = -ENETUNREACH;
  649. goto out_noupdate;
  650. }
  651. break;
  652. case -ENODEV:
  653. rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
  654. if (rc)
  655. goto out_noupdate;
  656. break;
  657. default:
  658. rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
  659. if (rc)
  660. goto out;
  661. }
  662. ep->rep_connected = 0;
  663. rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
  664. if (rc) {
  665. dprintk("RPC: %s: rdma_connect() failed with %i\n",
  666. __func__, rc);
  667. goto out;
  668. }
  669. wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
  670. if (ep->rep_connected <= 0) {
  671. if (ep->rep_connected == -EAGAIN)
  672. goto retry;
  673. rc = ep->rep_connected;
  674. goto out;
  675. }
  676. dprintk("RPC: %s: connected\n", __func__);
  677. rpcrdma_post_recvs(r_xprt, true);
  678. out:
  679. if (rc)
  680. ep->rep_connected = rc;
  681. out_noupdate:
  682. return rc;
  683. }
  684. /*
  685. * rpcrdma_ep_disconnect
  686. *
  687. * This is separate from destroy to facilitate the ability
  688. * to reconnect without recreating the endpoint.
  689. *
  690. * This call is not reentrant, and must not be made in parallel
  691. * on the same endpoint.
  692. */
  693. void
  694. rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  695. {
  696. int rc;
  697. rc = rdma_disconnect(ia->ri_id);
  698. if (!rc)
  699. /* returns without wait if not connected */
  700. wait_event_interruptible(ep->rep_connect_wait,
  701. ep->rep_connected != 1);
  702. else
  703. ep->rep_connected = rc;
  704. trace_xprtrdma_disconnect(container_of(ep, struct rpcrdma_xprt,
  705. rx_ep), rc);
  706. ib_drain_qp(ia->ri_id->qp);
  707. }
  708. /* Fixed-size circular FIFO queue. This implementation is wait-free and
  709. * lock-free.
  710. *
  711. * Consumer is the code path that posts Sends. This path dequeues a
  712. * sendctx for use by a Send operation. Multiple consumer threads
  713. * are serialized by the RPC transport lock, which allows only one
  714. * ->send_request call at a time.
  715. *
  716. * Producer is the code path that handles Send completions. This path
  717. * enqueues a sendctx that has been completed. Multiple producer
  718. * threads are serialized by the ib_poll_cq() function.
  719. */
  720. /* rpcrdma_sendctxs_destroy() assumes caller has already quiesced
  721. * queue activity, and ib_drain_qp has flushed all remaining Send
  722. * requests.
  723. */
  724. static void rpcrdma_sendctxs_destroy(struct rpcrdma_buffer *buf)
  725. {
  726. unsigned long i;
  727. for (i = 0; i <= buf->rb_sc_last; i++)
  728. kfree(buf->rb_sc_ctxs[i]);
  729. kfree(buf->rb_sc_ctxs);
  730. }
  731. static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ia *ia)
  732. {
  733. struct rpcrdma_sendctx *sc;
  734. sc = kzalloc(sizeof(*sc) +
  735. ia->ri_max_send_sges * sizeof(struct ib_sge),
  736. GFP_KERNEL);
  737. if (!sc)
  738. return NULL;
  739. sc->sc_wr.wr_cqe = &sc->sc_cqe;
  740. sc->sc_wr.sg_list = sc->sc_sges;
  741. sc->sc_wr.opcode = IB_WR_SEND;
  742. sc->sc_cqe.done = rpcrdma_wc_send;
  743. return sc;
  744. }
  745. static int rpcrdma_sendctxs_create(struct rpcrdma_xprt *r_xprt)
  746. {
  747. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  748. struct rpcrdma_sendctx *sc;
  749. unsigned long i;
  750. /* Maximum number of concurrent outstanding Send WRs. Capping
  751. * the circular queue size stops Send Queue overflow by causing
  752. * the ->send_request call to fail temporarily before too many
  753. * Sends are posted.
  754. */
  755. i = buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS;
  756. dprintk("RPC: %s: allocating %lu send_ctxs\n", __func__, i);
  757. buf->rb_sc_ctxs = kcalloc(i, sizeof(sc), GFP_KERNEL);
  758. if (!buf->rb_sc_ctxs)
  759. return -ENOMEM;
  760. buf->rb_sc_last = i - 1;
  761. for (i = 0; i <= buf->rb_sc_last; i++) {
  762. sc = rpcrdma_sendctx_create(&r_xprt->rx_ia);
  763. if (!sc)
  764. goto out_destroy;
  765. sc->sc_xprt = r_xprt;
  766. buf->rb_sc_ctxs[i] = sc;
  767. }
  768. buf->rb_flags = 0;
  769. return 0;
  770. out_destroy:
  771. rpcrdma_sendctxs_destroy(buf);
  772. return -ENOMEM;
  773. }
  774. /* The sendctx queue is not guaranteed to have a size that is a
  775. * power of two, thus the helpers in circ_buf.h cannot be used.
  776. * The other option is to use modulus (%), which can be expensive.
  777. */
  778. static unsigned long rpcrdma_sendctx_next(struct rpcrdma_buffer *buf,
  779. unsigned long item)
  780. {
  781. return likely(item < buf->rb_sc_last) ? item + 1 : 0;
  782. }
  783. /**
  784. * rpcrdma_sendctx_get_locked - Acquire a send context
  785. * @buf: transport buffers from which to acquire an unused context
  786. *
  787. * Returns pointer to a free send completion context; or NULL if
  788. * the queue is empty.
  789. *
  790. * Usage: Called to acquire an SGE array before preparing a Send WR.
  791. *
  792. * The caller serializes calls to this function (per rpcrdma_buffer),
  793. * and provides an effective memory barrier that flushes the new value
  794. * of rb_sc_head.
  795. */
  796. struct rpcrdma_sendctx *rpcrdma_sendctx_get_locked(struct rpcrdma_buffer *buf)
  797. {
  798. struct rpcrdma_xprt *r_xprt;
  799. struct rpcrdma_sendctx *sc;
  800. unsigned long next_head;
  801. next_head = rpcrdma_sendctx_next(buf, buf->rb_sc_head);
  802. if (next_head == READ_ONCE(buf->rb_sc_tail))
  803. goto out_emptyq;
  804. /* ORDER: item must be accessed _before_ head is updated */
  805. sc = buf->rb_sc_ctxs[next_head];
  806. /* Releasing the lock in the caller acts as a memory
  807. * barrier that flushes rb_sc_head.
  808. */
  809. buf->rb_sc_head = next_head;
  810. return sc;
  811. out_emptyq:
  812. /* The queue is "empty" if there have not been enough Send
  813. * completions recently. This is a sign the Send Queue is
  814. * backing up. Cause the caller to pause and try again.
  815. */
  816. set_bit(RPCRDMA_BUF_F_EMPTY_SCQ, &buf->rb_flags);
  817. r_xprt = container_of(buf, struct rpcrdma_xprt, rx_buf);
  818. r_xprt->rx_stats.empty_sendctx_q++;
  819. return NULL;
  820. }
  821. /**
  822. * rpcrdma_sendctx_put_locked - Release a send context
  823. * @sc: send context to release
  824. *
  825. * Usage: Called from Send completion to return a sendctxt
  826. * to the queue.
  827. *
  828. * The caller serializes calls to this function (per rpcrdma_buffer).
  829. */
  830. static void
  831. rpcrdma_sendctx_put_locked(struct rpcrdma_sendctx *sc)
  832. {
  833. struct rpcrdma_buffer *buf = &sc->sc_xprt->rx_buf;
  834. unsigned long next_tail;
  835. /* Unmap SGEs of previously completed by unsignaled
  836. * Sends by walking up the queue until @sc is found.
  837. */
  838. next_tail = buf->rb_sc_tail;
  839. do {
  840. next_tail = rpcrdma_sendctx_next(buf, next_tail);
  841. /* ORDER: item must be accessed _before_ tail is updated */
  842. rpcrdma_unmap_sendctx(buf->rb_sc_ctxs[next_tail]);
  843. } while (buf->rb_sc_ctxs[next_tail] != sc);
  844. /* Paired with READ_ONCE */
  845. smp_store_release(&buf->rb_sc_tail, next_tail);
  846. if (test_and_clear_bit(RPCRDMA_BUF_F_EMPTY_SCQ, &buf->rb_flags)) {
  847. smp_mb__after_atomic();
  848. xprt_write_space(&sc->sc_xprt->rx_xprt);
  849. }
  850. }
  851. static void
  852. rpcrdma_mr_recovery_worker(struct work_struct *work)
  853. {
  854. struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
  855. rb_recovery_worker.work);
  856. struct rpcrdma_mr *mr;
  857. spin_lock(&buf->rb_recovery_lock);
  858. while (!list_empty(&buf->rb_stale_mrs)) {
  859. mr = rpcrdma_mr_pop(&buf->rb_stale_mrs);
  860. spin_unlock(&buf->rb_recovery_lock);
  861. trace_xprtrdma_recover_mr(mr);
  862. mr->mr_xprt->rx_ia.ri_ops->ro_recover_mr(mr);
  863. spin_lock(&buf->rb_recovery_lock);
  864. }
  865. spin_unlock(&buf->rb_recovery_lock);
  866. }
  867. void
  868. rpcrdma_mr_defer_recovery(struct rpcrdma_mr *mr)
  869. {
  870. struct rpcrdma_xprt *r_xprt = mr->mr_xprt;
  871. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  872. spin_lock(&buf->rb_recovery_lock);
  873. rpcrdma_mr_push(mr, &buf->rb_stale_mrs);
  874. spin_unlock(&buf->rb_recovery_lock);
  875. schedule_delayed_work(&buf->rb_recovery_worker, 0);
  876. }
  877. static void
  878. rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt)
  879. {
  880. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  881. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  882. unsigned int count;
  883. LIST_HEAD(free);
  884. LIST_HEAD(all);
  885. for (count = 0; count < 3; count++) {
  886. struct rpcrdma_mr *mr;
  887. int rc;
  888. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  889. if (!mr)
  890. break;
  891. rc = ia->ri_ops->ro_init_mr(ia, mr);
  892. if (rc) {
  893. kfree(mr);
  894. break;
  895. }
  896. mr->mr_xprt = r_xprt;
  897. list_add(&mr->mr_list, &free);
  898. list_add(&mr->mr_all, &all);
  899. }
  900. spin_lock(&buf->rb_mrlock);
  901. list_splice(&free, &buf->rb_mrs);
  902. list_splice(&all, &buf->rb_all);
  903. r_xprt->rx_stats.mrs_allocated += count;
  904. spin_unlock(&buf->rb_mrlock);
  905. trace_xprtrdma_createmrs(r_xprt, count);
  906. xprt_write_space(&r_xprt->rx_xprt);
  907. }
  908. static void
  909. rpcrdma_mr_refresh_worker(struct work_struct *work)
  910. {
  911. struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
  912. rb_refresh_worker.work);
  913. struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
  914. rx_buf);
  915. rpcrdma_mrs_create(r_xprt);
  916. }
  917. struct rpcrdma_req *
  918. rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
  919. {
  920. struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
  921. struct rpcrdma_regbuf *rb;
  922. struct rpcrdma_req *req;
  923. req = kzalloc(sizeof(*req), GFP_KERNEL);
  924. if (req == NULL)
  925. return ERR_PTR(-ENOMEM);
  926. rb = rpcrdma_alloc_regbuf(RPCRDMA_HDRBUF_SIZE,
  927. DMA_TO_DEVICE, GFP_KERNEL);
  928. if (IS_ERR(rb)) {
  929. kfree(req);
  930. return ERR_PTR(-ENOMEM);
  931. }
  932. req->rl_rdmabuf = rb;
  933. xdr_buf_init(&req->rl_hdrbuf, rb->rg_base, rdmab_length(rb));
  934. req->rl_buffer = buffer;
  935. INIT_LIST_HEAD(&req->rl_registered);
  936. spin_lock(&buffer->rb_reqslock);
  937. list_add(&req->rl_all, &buffer->rb_allreqs);
  938. spin_unlock(&buffer->rb_reqslock);
  939. return req;
  940. }
  941. static int
  942. rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt, bool temp)
  943. {
  944. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  945. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  946. struct rpcrdma_rep *rep;
  947. int rc;
  948. rc = -ENOMEM;
  949. rep = kzalloc(sizeof(*rep), GFP_KERNEL);
  950. if (rep == NULL)
  951. goto out;
  952. rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
  953. DMA_FROM_DEVICE, GFP_KERNEL);
  954. if (IS_ERR(rep->rr_rdmabuf)) {
  955. rc = PTR_ERR(rep->rr_rdmabuf);
  956. goto out_free;
  957. }
  958. xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base,
  959. rdmab_length(rep->rr_rdmabuf));
  960. rep->rr_cqe.done = rpcrdma_wc_receive;
  961. rep->rr_rxprt = r_xprt;
  962. INIT_WORK(&rep->rr_work, rpcrdma_deferred_completion);
  963. rep->rr_recv_wr.next = NULL;
  964. rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
  965. rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
  966. rep->rr_recv_wr.num_sge = 1;
  967. rep->rr_temp = temp;
  968. spin_lock(&buf->rb_lock);
  969. list_add(&rep->rr_list, &buf->rb_recv_bufs);
  970. spin_unlock(&buf->rb_lock);
  971. return 0;
  972. out_free:
  973. kfree(rep);
  974. out:
  975. dprintk("RPC: %s: reply buffer %d alloc failed\n",
  976. __func__, rc);
  977. return rc;
  978. }
  979. int
  980. rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
  981. {
  982. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  983. int i, rc;
  984. buf->rb_max_requests = r_xprt->rx_data.max_requests;
  985. buf->rb_bc_srv_max_requests = 0;
  986. spin_lock_init(&buf->rb_mrlock);
  987. spin_lock_init(&buf->rb_lock);
  988. spin_lock_init(&buf->rb_recovery_lock);
  989. INIT_LIST_HEAD(&buf->rb_mrs);
  990. INIT_LIST_HEAD(&buf->rb_all);
  991. INIT_LIST_HEAD(&buf->rb_stale_mrs);
  992. INIT_DELAYED_WORK(&buf->rb_refresh_worker,
  993. rpcrdma_mr_refresh_worker);
  994. INIT_DELAYED_WORK(&buf->rb_recovery_worker,
  995. rpcrdma_mr_recovery_worker);
  996. rpcrdma_mrs_create(r_xprt);
  997. INIT_LIST_HEAD(&buf->rb_send_bufs);
  998. INIT_LIST_HEAD(&buf->rb_allreqs);
  999. spin_lock_init(&buf->rb_reqslock);
  1000. for (i = 0; i < buf->rb_max_requests; i++) {
  1001. struct rpcrdma_req *req;
  1002. req = rpcrdma_create_req(r_xprt);
  1003. if (IS_ERR(req)) {
  1004. dprintk("RPC: %s: request buffer %d alloc"
  1005. " failed\n", __func__, i);
  1006. rc = PTR_ERR(req);
  1007. goto out;
  1008. }
  1009. list_add(&req->rl_list, &buf->rb_send_bufs);
  1010. }
  1011. buf->rb_posted_receives = 0;
  1012. INIT_LIST_HEAD(&buf->rb_recv_bufs);
  1013. rc = rpcrdma_sendctxs_create(r_xprt);
  1014. if (rc)
  1015. goto out;
  1016. return 0;
  1017. out:
  1018. rpcrdma_buffer_destroy(buf);
  1019. return rc;
  1020. }
  1021. static void
  1022. rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
  1023. {
  1024. rpcrdma_free_regbuf(rep->rr_rdmabuf);
  1025. kfree(rep);
  1026. }
  1027. void
  1028. rpcrdma_destroy_req(struct rpcrdma_req *req)
  1029. {
  1030. rpcrdma_free_regbuf(req->rl_recvbuf);
  1031. rpcrdma_free_regbuf(req->rl_sendbuf);
  1032. rpcrdma_free_regbuf(req->rl_rdmabuf);
  1033. kfree(req);
  1034. }
  1035. static void
  1036. rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf)
  1037. {
  1038. struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
  1039. rx_buf);
  1040. struct rpcrdma_ia *ia = rdmab_to_ia(buf);
  1041. struct rpcrdma_mr *mr;
  1042. unsigned int count;
  1043. count = 0;
  1044. spin_lock(&buf->rb_mrlock);
  1045. while (!list_empty(&buf->rb_all)) {
  1046. mr = list_entry(buf->rb_all.next, struct rpcrdma_mr, mr_all);
  1047. list_del(&mr->mr_all);
  1048. spin_unlock(&buf->rb_mrlock);
  1049. /* Ensure MW is not on any rl_registered list */
  1050. if (!list_empty(&mr->mr_list))
  1051. list_del(&mr->mr_list);
  1052. ia->ri_ops->ro_release_mr(mr);
  1053. count++;
  1054. spin_lock(&buf->rb_mrlock);
  1055. }
  1056. spin_unlock(&buf->rb_mrlock);
  1057. r_xprt->rx_stats.mrs_allocated = 0;
  1058. dprintk("RPC: %s: released %u MRs\n", __func__, count);
  1059. }
  1060. void
  1061. rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
  1062. {
  1063. cancel_delayed_work_sync(&buf->rb_recovery_worker);
  1064. cancel_delayed_work_sync(&buf->rb_refresh_worker);
  1065. rpcrdma_sendctxs_destroy(buf);
  1066. while (!list_empty(&buf->rb_recv_bufs)) {
  1067. struct rpcrdma_rep *rep;
  1068. rep = list_first_entry(&buf->rb_recv_bufs,
  1069. struct rpcrdma_rep, rr_list);
  1070. list_del(&rep->rr_list);
  1071. rpcrdma_destroy_rep(rep);
  1072. }
  1073. spin_lock(&buf->rb_reqslock);
  1074. while (!list_empty(&buf->rb_allreqs)) {
  1075. struct rpcrdma_req *req;
  1076. req = list_first_entry(&buf->rb_allreqs,
  1077. struct rpcrdma_req, rl_all);
  1078. list_del(&req->rl_all);
  1079. spin_unlock(&buf->rb_reqslock);
  1080. rpcrdma_destroy_req(req);
  1081. spin_lock(&buf->rb_reqslock);
  1082. }
  1083. spin_unlock(&buf->rb_reqslock);
  1084. rpcrdma_mrs_destroy(buf);
  1085. }
  1086. /**
  1087. * rpcrdma_mr_get - Allocate an rpcrdma_mr object
  1088. * @r_xprt: controlling transport
  1089. *
  1090. * Returns an initialized rpcrdma_mr or NULL if no free
  1091. * rpcrdma_mr objects are available.
  1092. */
  1093. struct rpcrdma_mr *
  1094. rpcrdma_mr_get(struct rpcrdma_xprt *r_xprt)
  1095. {
  1096. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  1097. struct rpcrdma_mr *mr = NULL;
  1098. spin_lock(&buf->rb_mrlock);
  1099. if (!list_empty(&buf->rb_mrs))
  1100. mr = rpcrdma_mr_pop(&buf->rb_mrs);
  1101. spin_unlock(&buf->rb_mrlock);
  1102. if (!mr)
  1103. goto out_nomrs;
  1104. return mr;
  1105. out_nomrs:
  1106. trace_xprtrdma_nomrs(r_xprt);
  1107. if (r_xprt->rx_ep.rep_connected != -ENODEV)
  1108. schedule_delayed_work(&buf->rb_refresh_worker, 0);
  1109. /* Allow the reply handler and refresh worker to run */
  1110. cond_resched();
  1111. return NULL;
  1112. }
  1113. static void
  1114. __rpcrdma_mr_put(struct rpcrdma_buffer *buf, struct rpcrdma_mr *mr)
  1115. {
  1116. spin_lock(&buf->rb_mrlock);
  1117. rpcrdma_mr_push(mr, &buf->rb_mrs);
  1118. spin_unlock(&buf->rb_mrlock);
  1119. }
  1120. /**
  1121. * rpcrdma_mr_put - Release an rpcrdma_mr object
  1122. * @mr: object to release
  1123. *
  1124. */
  1125. void
  1126. rpcrdma_mr_put(struct rpcrdma_mr *mr)
  1127. {
  1128. __rpcrdma_mr_put(&mr->mr_xprt->rx_buf, mr);
  1129. }
  1130. /**
  1131. * rpcrdma_mr_unmap_and_put - DMA unmap an MR and release it
  1132. * @mr: object to release
  1133. *
  1134. */
  1135. void
  1136. rpcrdma_mr_unmap_and_put(struct rpcrdma_mr *mr)
  1137. {
  1138. struct rpcrdma_xprt *r_xprt = mr->mr_xprt;
  1139. trace_xprtrdma_dma_unmap(mr);
  1140. ib_dma_unmap_sg(r_xprt->rx_ia.ri_device,
  1141. mr->mr_sg, mr->mr_nents, mr->mr_dir);
  1142. __rpcrdma_mr_put(&r_xprt->rx_buf, mr);
  1143. }
  1144. /**
  1145. * rpcrdma_buffer_get - Get a request buffer
  1146. * @buffers: Buffer pool from which to obtain a buffer
  1147. *
  1148. * Returns a fresh rpcrdma_req, or NULL if none are available.
  1149. */
  1150. struct rpcrdma_req *
  1151. rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
  1152. {
  1153. struct rpcrdma_req *req;
  1154. spin_lock(&buffers->rb_lock);
  1155. req = list_first_entry_or_null(&buffers->rb_send_bufs,
  1156. struct rpcrdma_req, rl_list);
  1157. if (req)
  1158. list_del_init(&req->rl_list);
  1159. spin_unlock(&buffers->rb_lock);
  1160. return req;
  1161. }
  1162. /**
  1163. * rpcrdma_buffer_put - Put request/reply buffers back into pool
  1164. * @req: object to return
  1165. *
  1166. */
  1167. void
  1168. rpcrdma_buffer_put(struct rpcrdma_req *req)
  1169. {
  1170. struct rpcrdma_buffer *buffers = req->rl_buffer;
  1171. struct rpcrdma_rep *rep = req->rl_reply;
  1172. req->rl_reply = NULL;
  1173. spin_lock(&buffers->rb_lock);
  1174. list_add(&req->rl_list, &buffers->rb_send_bufs);
  1175. if (rep) {
  1176. if (!rep->rr_temp) {
  1177. list_add(&rep->rr_list, &buffers->rb_recv_bufs);
  1178. rep = NULL;
  1179. }
  1180. }
  1181. spin_unlock(&buffers->rb_lock);
  1182. if (rep)
  1183. rpcrdma_destroy_rep(rep);
  1184. }
  1185. /*
  1186. * Put reply buffers back into pool when not attached to
  1187. * request. This happens in error conditions.
  1188. */
  1189. void
  1190. rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
  1191. {
  1192. struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
  1193. if (!rep->rr_temp) {
  1194. spin_lock(&buffers->rb_lock);
  1195. list_add(&rep->rr_list, &buffers->rb_recv_bufs);
  1196. spin_unlock(&buffers->rb_lock);
  1197. } else {
  1198. rpcrdma_destroy_rep(rep);
  1199. }
  1200. }
  1201. /**
  1202. * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
  1203. * @size: size of buffer to be allocated, in bytes
  1204. * @direction: direction of data movement
  1205. * @flags: GFP flags
  1206. *
  1207. * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
  1208. * can be persistently DMA-mapped for I/O.
  1209. *
  1210. * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
  1211. * receiving the payload of RDMA RECV operations. During Long Calls
  1212. * or Replies they may be registered externally via ro_map.
  1213. */
  1214. struct rpcrdma_regbuf *
  1215. rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
  1216. gfp_t flags)
  1217. {
  1218. struct rpcrdma_regbuf *rb;
  1219. rb = kmalloc(sizeof(*rb) + size, flags);
  1220. if (rb == NULL)
  1221. return ERR_PTR(-ENOMEM);
  1222. rb->rg_device = NULL;
  1223. rb->rg_direction = direction;
  1224. rb->rg_iov.length = size;
  1225. return rb;
  1226. }
  1227. /**
  1228. * __rpcrdma_map_regbuf - DMA-map a regbuf
  1229. * @ia: controlling rpcrdma_ia
  1230. * @rb: regbuf to be mapped
  1231. */
  1232. bool
  1233. __rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
  1234. {
  1235. struct ib_device *device = ia->ri_device;
  1236. if (rb->rg_direction == DMA_NONE)
  1237. return false;
  1238. rb->rg_iov.addr = ib_dma_map_single(device,
  1239. (void *)rb->rg_base,
  1240. rdmab_length(rb),
  1241. rb->rg_direction);
  1242. if (ib_dma_mapping_error(device, rdmab_addr(rb)))
  1243. return false;
  1244. rb->rg_device = device;
  1245. rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
  1246. return true;
  1247. }
  1248. static void
  1249. rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
  1250. {
  1251. if (!rb)
  1252. return;
  1253. if (!rpcrdma_regbuf_is_mapped(rb))
  1254. return;
  1255. ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
  1256. rdmab_length(rb), rb->rg_direction);
  1257. rb->rg_device = NULL;
  1258. }
  1259. /**
  1260. * rpcrdma_free_regbuf - deregister and free registered buffer
  1261. * @rb: regbuf to be deregistered and freed
  1262. */
  1263. void
  1264. rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
  1265. {
  1266. rpcrdma_dma_unmap_regbuf(rb);
  1267. kfree(rb);
  1268. }
  1269. /*
  1270. * Prepost any receive buffer, then post send.
  1271. *
  1272. * Receive buffer is donated to hardware, reclaimed upon recv completion.
  1273. */
  1274. int
  1275. rpcrdma_ep_post(struct rpcrdma_ia *ia,
  1276. struct rpcrdma_ep *ep,
  1277. struct rpcrdma_req *req)
  1278. {
  1279. struct ib_send_wr *send_wr = &req->rl_sendctx->sc_wr;
  1280. int rc;
  1281. if (!ep->rep_send_count ||
  1282. test_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags)) {
  1283. send_wr->send_flags |= IB_SEND_SIGNALED;
  1284. ep->rep_send_count = ep->rep_send_batch;
  1285. } else {
  1286. send_wr->send_flags &= ~IB_SEND_SIGNALED;
  1287. --ep->rep_send_count;
  1288. }
  1289. rc = ia->ri_ops->ro_send(ia, req);
  1290. trace_xprtrdma_post_send(req, rc);
  1291. if (rc)
  1292. return -ENOTCONN;
  1293. return 0;
  1294. }
  1295. /**
  1296. * rpcrdma_post_recvs - Maybe post some Receive buffers
  1297. * @r_xprt: controlling transport
  1298. * @temp: when true, allocate temp rpcrdma_rep objects
  1299. *
  1300. */
  1301. void
  1302. rpcrdma_post_recvs(struct rpcrdma_xprt *r_xprt, bool temp)
  1303. {
  1304. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  1305. struct ib_recv_wr *wr, *bad_wr;
  1306. int needed, count, rc;
  1307. needed = buf->rb_credits + (buf->rb_bc_srv_max_requests << 1);
  1308. if (buf->rb_posted_receives > needed)
  1309. return;
  1310. needed -= buf->rb_posted_receives;
  1311. count = 0;
  1312. wr = NULL;
  1313. while (needed) {
  1314. struct rpcrdma_regbuf *rb;
  1315. struct rpcrdma_rep *rep;
  1316. spin_lock(&buf->rb_lock);
  1317. rep = list_first_entry_or_null(&buf->rb_recv_bufs,
  1318. struct rpcrdma_rep, rr_list);
  1319. if (likely(rep))
  1320. list_del(&rep->rr_list);
  1321. spin_unlock(&buf->rb_lock);
  1322. if (!rep) {
  1323. if (rpcrdma_create_rep(r_xprt, temp))
  1324. break;
  1325. continue;
  1326. }
  1327. rb = rep->rr_rdmabuf;
  1328. if (!rpcrdma_regbuf_is_mapped(rb)) {
  1329. if (!__rpcrdma_dma_map_regbuf(&r_xprt->rx_ia, rb)) {
  1330. rpcrdma_recv_buffer_put(rep);
  1331. break;
  1332. }
  1333. }
  1334. trace_xprtrdma_post_recv(rep->rr_recv_wr.wr_cqe);
  1335. rep->rr_recv_wr.next = wr;
  1336. wr = &rep->rr_recv_wr;
  1337. ++count;
  1338. --needed;
  1339. }
  1340. if (!count)
  1341. return;
  1342. rc = ib_post_recv(r_xprt->rx_ia.ri_id->qp, wr, &bad_wr);
  1343. if (rc) {
  1344. for (wr = bad_wr; wr; wr = wr->next) {
  1345. struct rpcrdma_rep *rep;
  1346. rep = container_of(wr, struct rpcrdma_rep, rr_recv_wr);
  1347. rpcrdma_recv_buffer_put(rep);
  1348. --count;
  1349. }
  1350. }
  1351. buf->rb_posted_receives += count;
  1352. trace_xprtrdma_post_recvs(r_xprt, count, rc);
  1353. }