verbs.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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. init_completion(&ia->ri_remove_done);
  220. set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
  221. ep->rep_connected = -ENODEV;
  222. xprt_force_disconnect(&xprt->rx_xprt);
  223. wait_for_completion(&ia->ri_remove_done);
  224. ia->ri_id = NULL;
  225. ia->ri_device = NULL;
  226. /* Return 1 to ensure the core destroys the id. */
  227. return 1;
  228. case RDMA_CM_EVENT_ESTABLISHED:
  229. ++xprt->rx_xprt.connect_cookie;
  230. connstate = 1;
  231. rpcrdma_update_connect_private(xprt, &event->param.conn);
  232. goto connected;
  233. case RDMA_CM_EVENT_CONNECT_ERROR:
  234. connstate = -ENOTCONN;
  235. goto connected;
  236. case RDMA_CM_EVENT_UNREACHABLE:
  237. connstate = -ENETUNREACH;
  238. goto connected;
  239. case RDMA_CM_EVENT_REJECTED:
  240. dprintk("rpcrdma: connection to %s:%s rejected: %s\n",
  241. rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt),
  242. rdma_reject_msg(id, event->status));
  243. connstate = -ECONNREFUSED;
  244. if (event->status == IB_CM_REJ_STALE_CONN)
  245. connstate = -EAGAIN;
  246. goto connected;
  247. case RDMA_CM_EVENT_DISCONNECTED:
  248. ++xprt->rx_xprt.connect_cookie;
  249. connstate = -ECONNABORTED;
  250. connected:
  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. id = rdma_create_id(xprt->rx_xprt.xprt_net, rpcrdma_conn_upcall,
  274. xprt, RDMA_PS_TCP, IB_QPT_RC);
  275. if (IS_ERR(id)) {
  276. rc = PTR_ERR(id);
  277. dprintk("RPC: %s: rdma_create_id() failed %i\n",
  278. __func__, rc);
  279. return id;
  280. }
  281. ia->ri_async_rc = -ETIMEDOUT;
  282. rc = rdma_resolve_addr(id, NULL,
  283. (struct sockaddr *)&xprt->rx_xprt.addr,
  284. RDMA_RESOLVE_TIMEOUT);
  285. if (rc) {
  286. dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
  287. __func__, rc);
  288. goto out;
  289. }
  290. rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
  291. if (rc < 0) {
  292. trace_xprtrdma_conn_tout(xprt);
  293. goto out;
  294. }
  295. rc = ia->ri_async_rc;
  296. if (rc)
  297. goto out;
  298. ia->ri_async_rc = -ETIMEDOUT;
  299. rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
  300. if (rc) {
  301. dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
  302. __func__, rc);
  303. goto out;
  304. }
  305. rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
  306. if (rc < 0) {
  307. trace_xprtrdma_conn_tout(xprt);
  308. goto out;
  309. }
  310. rc = ia->ri_async_rc;
  311. if (rc)
  312. goto out;
  313. return id;
  314. out:
  315. rdma_destroy_id(id);
  316. return ERR_PTR(rc);
  317. }
  318. /*
  319. * Exported functions.
  320. */
  321. /**
  322. * rpcrdma_ia_open - Open and initialize an Interface Adapter.
  323. * @xprt: transport with IA to (re)initialize
  324. *
  325. * Returns 0 on success, negative errno if an appropriate
  326. * Interface Adapter could not be found and opened.
  327. */
  328. int
  329. rpcrdma_ia_open(struct rpcrdma_xprt *xprt)
  330. {
  331. struct rpcrdma_ia *ia = &xprt->rx_ia;
  332. int rc;
  333. ia->ri_id = rpcrdma_create_id(xprt, ia);
  334. if (IS_ERR(ia->ri_id)) {
  335. rc = PTR_ERR(ia->ri_id);
  336. goto out_err;
  337. }
  338. ia->ri_device = ia->ri_id->device;
  339. ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
  340. if (IS_ERR(ia->ri_pd)) {
  341. rc = PTR_ERR(ia->ri_pd);
  342. pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
  343. goto out_err;
  344. }
  345. switch (xprt_rdma_memreg_strategy) {
  346. case RPCRDMA_FRWR:
  347. if (frwr_is_supported(ia)) {
  348. ia->ri_ops = &rpcrdma_frwr_memreg_ops;
  349. break;
  350. }
  351. /*FALLTHROUGH*/
  352. case RPCRDMA_MTHCAFMR:
  353. if (fmr_is_supported(ia)) {
  354. ia->ri_ops = &rpcrdma_fmr_memreg_ops;
  355. break;
  356. }
  357. /*FALLTHROUGH*/
  358. default:
  359. pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
  360. ia->ri_device->name, xprt_rdma_memreg_strategy);
  361. rc = -EINVAL;
  362. goto out_err;
  363. }
  364. return 0;
  365. out_err:
  366. rpcrdma_ia_close(ia);
  367. return rc;
  368. }
  369. /**
  370. * rpcrdma_ia_remove - Handle device driver unload
  371. * @ia: interface adapter being removed
  372. *
  373. * Divest transport H/W resources associated with this adapter,
  374. * but allow it to be restored later.
  375. */
  376. void
  377. rpcrdma_ia_remove(struct rpcrdma_ia *ia)
  378. {
  379. struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
  380. rx_ia);
  381. struct rpcrdma_ep *ep = &r_xprt->rx_ep;
  382. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  383. struct rpcrdma_req *req;
  384. struct rpcrdma_rep *rep;
  385. cancel_delayed_work_sync(&buf->rb_refresh_worker);
  386. /* This is similar to rpcrdma_ep_destroy, but:
  387. * - Don't cancel the connect worker.
  388. * - Don't call rpcrdma_ep_disconnect, which waits
  389. * for another conn upcall, which will deadlock.
  390. * - rdma_disconnect is unneeded, the underlying
  391. * connection is already gone.
  392. */
  393. if (ia->ri_id->qp) {
  394. ib_drain_qp(ia->ri_id->qp);
  395. rdma_destroy_qp(ia->ri_id);
  396. ia->ri_id->qp = NULL;
  397. }
  398. ib_free_cq(ep->rep_attr.recv_cq);
  399. ep->rep_attr.recv_cq = NULL;
  400. ib_free_cq(ep->rep_attr.send_cq);
  401. ep->rep_attr.send_cq = NULL;
  402. /* The ULP is responsible for ensuring all DMA
  403. * mappings and MRs are gone.
  404. */
  405. list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
  406. rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
  407. list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
  408. rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
  409. rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
  410. rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
  411. }
  412. rpcrdma_mrs_destroy(buf);
  413. ib_dealloc_pd(ia->ri_pd);
  414. ia->ri_pd = NULL;
  415. /* Allow waiters to continue */
  416. complete(&ia->ri_remove_done);
  417. trace_xprtrdma_remove(r_xprt);
  418. }
  419. /**
  420. * rpcrdma_ia_close - Clean up/close an IA.
  421. * @ia: interface adapter to close
  422. *
  423. */
  424. void
  425. rpcrdma_ia_close(struct rpcrdma_ia *ia)
  426. {
  427. if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
  428. if (ia->ri_id->qp)
  429. rdma_destroy_qp(ia->ri_id);
  430. rdma_destroy_id(ia->ri_id);
  431. }
  432. ia->ri_id = NULL;
  433. ia->ri_device = NULL;
  434. /* If the pd is still busy, xprtrdma missed freeing a resource */
  435. if (ia->ri_pd && !IS_ERR(ia->ri_pd))
  436. ib_dealloc_pd(ia->ri_pd);
  437. ia->ri_pd = NULL;
  438. }
  439. /*
  440. * Create unconnected endpoint.
  441. */
  442. int
  443. rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
  444. struct rpcrdma_create_data_internal *cdata)
  445. {
  446. struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
  447. struct ib_cq *sendcq, *recvcq;
  448. unsigned int max_sge;
  449. int rc;
  450. max_sge = min_t(unsigned int, ia->ri_device->attrs.max_send_sge,
  451. RPCRDMA_MAX_SEND_SGES);
  452. if (max_sge < RPCRDMA_MIN_SEND_SGES) {
  453. pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
  454. return -ENOMEM;
  455. }
  456. ia->ri_max_send_sges = max_sge;
  457. rc = ia->ri_ops->ro_open(ia, ep, cdata);
  458. if (rc)
  459. return rc;
  460. ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
  461. ep->rep_attr.qp_context = ep;
  462. ep->rep_attr.srq = NULL;
  463. ep->rep_attr.cap.max_send_sge = max_sge;
  464. ep->rep_attr.cap.max_recv_sge = 1;
  465. ep->rep_attr.cap.max_inline_data = 0;
  466. ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  467. ep->rep_attr.qp_type = IB_QPT_RC;
  468. ep->rep_attr.port_num = ~0;
  469. dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
  470. "iovs: send %d recv %d\n",
  471. __func__,
  472. ep->rep_attr.cap.max_send_wr,
  473. ep->rep_attr.cap.max_recv_wr,
  474. ep->rep_attr.cap.max_send_sge,
  475. ep->rep_attr.cap.max_recv_sge);
  476. /* set trigger for requesting send completion */
  477. ep->rep_send_batch = min_t(unsigned int, RPCRDMA_MAX_SEND_BATCH,
  478. cdata->max_requests >> 2);
  479. ep->rep_send_count = ep->rep_send_batch;
  480. init_waitqueue_head(&ep->rep_connect_wait);
  481. INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
  482. sendcq = ib_alloc_cq(ia->ri_device, NULL,
  483. ep->rep_attr.cap.max_send_wr + 1,
  484. ia->ri_device->num_comp_vectors > 1 ? 1 : 0,
  485. 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. rpcrdma_post_recvs(r_xprt, true);
  664. rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
  665. if (rc) {
  666. dprintk("RPC: %s: rdma_connect() failed with %i\n",
  667. __func__, rc);
  668. goto out;
  669. }
  670. wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
  671. if (ep->rep_connected <= 0) {
  672. if (ep->rep_connected == -EAGAIN)
  673. goto retry;
  674. rc = ep->rep_connected;
  675. goto out;
  676. }
  677. dprintk("RPC: %s: connected\n", __func__);
  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. return -ENOMEM;
  765. sc->sc_xprt = r_xprt;
  766. buf->rb_sc_ctxs[i] = sc;
  767. }
  768. buf->rb_flags = 0;
  769. return 0;
  770. }
  771. /* The sendctx queue is not guaranteed to have a size that is a
  772. * power of two, thus the helpers in circ_buf.h cannot be used.
  773. * The other option is to use modulus (%), which can be expensive.
  774. */
  775. static unsigned long rpcrdma_sendctx_next(struct rpcrdma_buffer *buf,
  776. unsigned long item)
  777. {
  778. return likely(item < buf->rb_sc_last) ? item + 1 : 0;
  779. }
  780. /**
  781. * rpcrdma_sendctx_get_locked - Acquire a send context
  782. * @buf: transport buffers from which to acquire an unused context
  783. *
  784. * Returns pointer to a free send completion context; or NULL if
  785. * the queue is empty.
  786. *
  787. * Usage: Called to acquire an SGE array before preparing a Send WR.
  788. *
  789. * The caller serializes calls to this function (per rpcrdma_buffer),
  790. * and provides an effective memory barrier that flushes the new value
  791. * of rb_sc_head.
  792. */
  793. struct rpcrdma_sendctx *rpcrdma_sendctx_get_locked(struct rpcrdma_buffer *buf)
  794. {
  795. struct rpcrdma_xprt *r_xprt;
  796. struct rpcrdma_sendctx *sc;
  797. unsigned long next_head;
  798. next_head = rpcrdma_sendctx_next(buf, buf->rb_sc_head);
  799. if (next_head == READ_ONCE(buf->rb_sc_tail))
  800. goto out_emptyq;
  801. /* ORDER: item must be accessed _before_ head is updated */
  802. sc = buf->rb_sc_ctxs[next_head];
  803. /* Releasing the lock in the caller acts as a memory
  804. * barrier that flushes rb_sc_head.
  805. */
  806. buf->rb_sc_head = next_head;
  807. return sc;
  808. out_emptyq:
  809. /* The queue is "empty" if there have not been enough Send
  810. * completions recently. This is a sign the Send Queue is
  811. * backing up. Cause the caller to pause and try again.
  812. */
  813. set_bit(RPCRDMA_BUF_F_EMPTY_SCQ, &buf->rb_flags);
  814. r_xprt = container_of(buf, struct rpcrdma_xprt, rx_buf);
  815. r_xprt->rx_stats.empty_sendctx_q++;
  816. return NULL;
  817. }
  818. /**
  819. * rpcrdma_sendctx_put_locked - Release a send context
  820. * @sc: send context to release
  821. *
  822. * Usage: Called from Send completion to return a sendctxt
  823. * to the queue.
  824. *
  825. * The caller serializes calls to this function (per rpcrdma_buffer).
  826. */
  827. static void
  828. rpcrdma_sendctx_put_locked(struct rpcrdma_sendctx *sc)
  829. {
  830. struct rpcrdma_buffer *buf = &sc->sc_xprt->rx_buf;
  831. unsigned long next_tail;
  832. /* Unmap SGEs of previously completed by unsignaled
  833. * Sends by walking up the queue until @sc is found.
  834. */
  835. next_tail = buf->rb_sc_tail;
  836. do {
  837. next_tail = rpcrdma_sendctx_next(buf, next_tail);
  838. /* ORDER: item must be accessed _before_ tail is updated */
  839. rpcrdma_unmap_sendctx(buf->rb_sc_ctxs[next_tail]);
  840. } while (buf->rb_sc_ctxs[next_tail] != sc);
  841. /* Paired with READ_ONCE */
  842. smp_store_release(&buf->rb_sc_tail, next_tail);
  843. if (test_and_clear_bit(RPCRDMA_BUF_F_EMPTY_SCQ, &buf->rb_flags)) {
  844. smp_mb__after_atomic();
  845. xprt_write_space(&sc->sc_xprt->rx_xprt);
  846. }
  847. }
  848. static void
  849. rpcrdma_mr_recovery_worker(struct work_struct *work)
  850. {
  851. struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
  852. rb_recovery_worker.work);
  853. struct rpcrdma_mr *mr;
  854. spin_lock(&buf->rb_recovery_lock);
  855. while (!list_empty(&buf->rb_stale_mrs)) {
  856. mr = rpcrdma_mr_pop(&buf->rb_stale_mrs);
  857. spin_unlock(&buf->rb_recovery_lock);
  858. trace_xprtrdma_recover_mr(mr);
  859. mr->mr_xprt->rx_ia.ri_ops->ro_recover_mr(mr);
  860. spin_lock(&buf->rb_recovery_lock);
  861. }
  862. spin_unlock(&buf->rb_recovery_lock);
  863. }
  864. void
  865. rpcrdma_mr_defer_recovery(struct rpcrdma_mr *mr)
  866. {
  867. struct rpcrdma_xprt *r_xprt = mr->mr_xprt;
  868. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  869. spin_lock(&buf->rb_recovery_lock);
  870. rpcrdma_mr_push(mr, &buf->rb_stale_mrs);
  871. spin_unlock(&buf->rb_recovery_lock);
  872. schedule_delayed_work(&buf->rb_recovery_worker, 0);
  873. }
  874. static void
  875. rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt)
  876. {
  877. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  878. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  879. unsigned int count;
  880. LIST_HEAD(free);
  881. LIST_HEAD(all);
  882. for (count = 0; count < 3; count++) {
  883. struct rpcrdma_mr *mr;
  884. int rc;
  885. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  886. if (!mr)
  887. break;
  888. rc = ia->ri_ops->ro_init_mr(ia, mr);
  889. if (rc) {
  890. kfree(mr);
  891. break;
  892. }
  893. mr->mr_xprt = r_xprt;
  894. list_add(&mr->mr_list, &free);
  895. list_add(&mr->mr_all, &all);
  896. }
  897. spin_lock(&buf->rb_mrlock);
  898. list_splice(&free, &buf->rb_mrs);
  899. list_splice(&all, &buf->rb_all);
  900. r_xprt->rx_stats.mrs_allocated += count;
  901. spin_unlock(&buf->rb_mrlock);
  902. trace_xprtrdma_createmrs(r_xprt, count);
  903. xprt_write_space(&r_xprt->rx_xprt);
  904. }
  905. static void
  906. rpcrdma_mr_refresh_worker(struct work_struct *work)
  907. {
  908. struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
  909. rb_refresh_worker.work);
  910. struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
  911. rx_buf);
  912. rpcrdma_mrs_create(r_xprt);
  913. }
  914. struct rpcrdma_req *
  915. rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
  916. {
  917. struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
  918. struct rpcrdma_regbuf *rb;
  919. struct rpcrdma_req *req;
  920. req = kzalloc(sizeof(*req), GFP_KERNEL);
  921. if (req == NULL)
  922. return ERR_PTR(-ENOMEM);
  923. rb = rpcrdma_alloc_regbuf(RPCRDMA_HDRBUF_SIZE,
  924. DMA_TO_DEVICE, GFP_KERNEL);
  925. if (IS_ERR(rb)) {
  926. kfree(req);
  927. return ERR_PTR(-ENOMEM);
  928. }
  929. req->rl_rdmabuf = rb;
  930. xdr_buf_init(&req->rl_hdrbuf, rb->rg_base, rdmab_length(rb));
  931. req->rl_buffer = buffer;
  932. INIT_LIST_HEAD(&req->rl_registered);
  933. spin_lock(&buffer->rb_reqslock);
  934. list_add(&req->rl_all, &buffer->rb_allreqs);
  935. spin_unlock(&buffer->rb_reqslock);
  936. return req;
  937. }
  938. static int
  939. rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt, bool temp)
  940. {
  941. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  942. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  943. struct rpcrdma_rep *rep;
  944. int rc;
  945. rc = -ENOMEM;
  946. rep = kzalloc(sizeof(*rep), GFP_KERNEL);
  947. if (rep == NULL)
  948. goto out;
  949. rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
  950. DMA_FROM_DEVICE, GFP_KERNEL);
  951. if (IS_ERR(rep->rr_rdmabuf)) {
  952. rc = PTR_ERR(rep->rr_rdmabuf);
  953. goto out_free;
  954. }
  955. xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base,
  956. rdmab_length(rep->rr_rdmabuf));
  957. rep->rr_cqe.done = rpcrdma_wc_receive;
  958. rep->rr_rxprt = r_xprt;
  959. INIT_WORK(&rep->rr_work, rpcrdma_deferred_completion);
  960. rep->rr_recv_wr.next = NULL;
  961. rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
  962. rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
  963. rep->rr_recv_wr.num_sge = 1;
  964. rep->rr_temp = temp;
  965. spin_lock(&buf->rb_lock);
  966. list_add(&rep->rr_list, &buf->rb_recv_bufs);
  967. spin_unlock(&buf->rb_lock);
  968. return 0;
  969. out_free:
  970. kfree(rep);
  971. out:
  972. dprintk("RPC: %s: reply buffer %d alloc failed\n",
  973. __func__, rc);
  974. return rc;
  975. }
  976. int
  977. rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
  978. {
  979. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  980. int i, rc;
  981. buf->rb_max_requests = r_xprt->rx_data.max_requests;
  982. buf->rb_bc_srv_max_requests = 0;
  983. spin_lock_init(&buf->rb_mrlock);
  984. spin_lock_init(&buf->rb_lock);
  985. spin_lock_init(&buf->rb_recovery_lock);
  986. INIT_LIST_HEAD(&buf->rb_mrs);
  987. INIT_LIST_HEAD(&buf->rb_all);
  988. INIT_LIST_HEAD(&buf->rb_stale_mrs);
  989. INIT_DELAYED_WORK(&buf->rb_refresh_worker,
  990. rpcrdma_mr_refresh_worker);
  991. INIT_DELAYED_WORK(&buf->rb_recovery_worker,
  992. rpcrdma_mr_recovery_worker);
  993. rpcrdma_mrs_create(r_xprt);
  994. INIT_LIST_HEAD(&buf->rb_send_bufs);
  995. INIT_LIST_HEAD(&buf->rb_allreqs);
  996. spin_lock_init(&buf->rb_reqslock);
  997. for (i = 0; i < buf->rb_max_requests; i++) {
  998. struct rpcrdma_req *req;
  999. req = rpcrdma_create_req(r_xprt);
  1000. if (IS_ERR(req)) {
  1001. dprintk("RPC: %s: request buffer %d alloc"
  1002. " failed\n", __func__, i);
  1003. rc = PTR_ERR(req);
  1004. goto out;
  1005. }
  1006. list_add(&req->rl_list, &buf->rb_send_bufs);
  1007. }
  1008. buf->rb_credits = 1;
  1009. buf->rb_posted_receives = 0;
  1010. INIT_LIST_HEAD(&buf->rb_recv_bufs);
  1011. rc = rpcrdma_sendctxs_create(r_xprt);
  1012. if (rc)
  1013. goto out;
  1014. return 0;
  1015. out:
  1016. rpcrdma_buffer_destroy(buf);
  1017. return rc;
  1018. }
  1019. static void
  1020. rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
  1021. {
  1022. rpcrdma_free_regbuf(rep->rr_rdmabuf);
  1023. kfree(rep);
  1024. }
  1025. void
  1026. rpcrdma_destroy_req(struct rpcrdma_req *req)
  1027. {
  1028. rpcrdma_free_regbuf(req->rl_recvbuf);
  1029. rpcrdma_free_regbuf(req->rl_sendbuf);
  1030. rpcrdma_free_regbuf(req->rl_rdmabuf);
  1031. kfree(req);
  1032. }
  1033. static void
  1034. rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf)
  1035. {
  1036. struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
  1037. rx_buf);
  1038. struct rpcrdma_ia *ia = rdmab_to_ia(buf);
  1039. struct rpcrdma_mr *mr;
  1040. unsigned int count;
  1041. count = 0;
  1042. spin_lock(&buf->rb_mrlock);
  1043. while (!list_empty(&buf->rb_all)) {
  1044. mr = list_entry(buf->rb_all.next, struct rpcrdma_mr, mr_all);
  1045. list_del(&mr->mr_all);
  1046. spin_unlock(&buf->rb_mrlock);
  1047. /* Ensure MW is not on any rl_registered list */
  1048. if (!list_empty(&mr->mr_list))
  1049. list_del(&mr->mr_list);
  1050. ia->ri_ops->ro_release_mr(mr);
  1051. count++;
  1052. spin_lock(&buf->rb_mrlock);
  1053. }
  1054. spin_unlock(&buf->rb_mrlock);
  1055. r_xprt->rx_stats.mrs_allocated = 0;
  1056. dprintk("RPC: %s: released %u MRs\n", __func__, count);
  1057. }
  1058. void
  1059. rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
  1060. {
  1061. cancel_delayed_work_sync(&buf->rb_recovery_worker);
  1062. cancel_delayed_work_sync(&buf->rb_refresh_worker);
  1063. rpcrdma_sendctxs_destroy(buf);
  1064. while (!list_empty(&buf->rb_recv_bufs)) {
  1065. struct rpcrdma_rep *rep;
  1066. rep = list_first_entry(&buf->rb_recv_bufs,
  1067. struct rpcrdma_rep, rr_list);
  1068. list_del(&rep->rr_list);
  1069. rpcrdma_destroy_rep(rep);
  1070. }
  1071. spin_lock(&buf->rb_reqslock);
  1072. while (!list_empty(&buf->rb_allreqs)) {
  1073. struct rpcrdma_req *req;
  1074. req = list_first_entry(&buf->rb_allreqs,
  1075. struct rpcrdma_req, rl_all);
  1076. list_del(&req->rl_all);
  1077. spin_unlock(&buf->rb_reqslock);
  1078. rpcrdma_destroy_req(req);
  1079. spin_lock(&buf->rb_reqslock);
  1080. }
  1081. spin_unlock(&buf->rb_reqslock);
  1082. rpcrdma_mrs_destroy(buf);
  1083. }
  1084. /**
  1085. * rpcrdma_mr_get - Allocate an rpcrdma_mr object
  1086. * @r_xprt: controlling transport
  1087. *
  1088. * Returns an initialized rpcrdma_mr or NULL if no free
  1089. * rpcrdma_mr objects are available.
  1090. */
  1091. struct rpcrdma_mr *
  1092. rpcrdma_mr_get(struct rpcrdma_xprt *r_xprt)
  1093. {
  1094. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  1095. struct rpcrdma_mr *mr = NULL;
  1096. spin_lock(&buf->rb_mrlock);
  1097. if (!list_empty(&buf->rb_mrs))
  1098. mr = rpcrdma_mr_pop(&buf->rb_mrs);
  1099. spin_unlock(&buf->rb_mrlock);
  1100. if (!mr)
  1101. goto out_nomrs;
  1102. return mr;
  1103. out_nomrs:
  1104. trace_xprtrdma_nomrs(r_xprt);
  1105. if (r_xprt->rx_ep.rep_connected != -ENODEV)
  1106. schedule_delayed_work(&buf->rb_refresh_worker, 0);
  1107. /* Allow the reply handler and refresh worker to run */
  1108. cond_resched();
  1109. return NULL;
  1110. }
  1111. static void
  1112. __rpcrdma_mr_put(struct rpcrdma_buffer *buf, struct rpcrdma_mr *mr)
  1113. {
  1114. spin_lock(&buf->rb_mrlock);
  1115. rpcrdma_mr_push(mr, &buf->rb_mrs);
  1116. spin_unlock(&buf->rb_mrlock);
  1117. }
  1118. /**
  1119. * rpcrdma_mr_put - Release an rpcrdma_mr object
  1120. * @mr: object to release
  1121. *
  1122. */
  1123. void
  1124. rpcrdma_mr_put(struct rpcrdma_mr *mr)
  1125. {
  1126. __rpcrdma_mr_put(&mr->mr_xprt->rx_buf, mr);
  1127. }
  1128. /**
  1129. * rpcrdma_mr_unmap_and_put - DMA unmap an MR and release it
  1130. * @mr: object to release
  1131. *
  1132. */
  1133. void
  1134. rpcrdma_mr_unmap_and_put(struct rpcrdma_mr *mr)
  1135. {
  1136. struct rpcrdma_xprt *r_xprt = mr->mr_xprt;
  1137. trace_xprtrdma_dma_unmap(mr);
  1138. ib_dma_unmap_sg(r_xprt->rx_ia.ri_device,
  1139. mr->mr_sg, mr->mr_nents, mr->mr_dir);
  1140. __rpcrdma_mr_put(&r_xprt->rx_buf, mr);
  1141. }
  1142. /**
  1143. * rpcrdma_buffer_get - Get a request buffer
  1144. * @buffers: Buffer pool from which to obtain a buffer
  1145. *
  1146. * Returns a fresh rpcrdma_req, or NULL if none are available.
  1147. */
  1148. struct rpcrdma_req *
  1149. rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
  1150. {
  1151. struct rpcrdma_req *req;
  1152. spin_lock(&buffers->rb_lock);
  1153. req = list_first_entry_or_null(&buffers->rb_send_bufs,
  1154. struct rpcrdma_req, rl_list);
  1155. if (req)
  1156. list_del_init(&req->rl_list);
  1157. spin_unlock(&buffers->rb_lock);
  1158. return req;
  1159. }
  1160. /**
  1161. * rpcrdma_buffer_put - Put request/reply buffers back into pool
  1162. * @req: object to return
  1163. *
  1164. */
  1165. void
  1166. rpcrdma_buffer_put(struct rpcrdma_req *req)
  1167. {
  1168. struct rpcrdma_buffer *buffers = req->rl_buffer;
  1169. struct rpcrdma_rep *rep = req->rl_reply;
  1170. req->rl_reply = NULL;
  1171. spin_lock(&buffers->rb_lock);
  1172. list_add(&req->rl_list, &buffers->rb_send_bufs);
  1173. if (rep) {
  1174. if (!rep->rr_temp) {
  1175. list_add(&rep->rr_list, &buffers->rb_recv_bufs);
  1176. rep = NULL;
  1177. }
  1178. }
  1179. spin_unlock(&buffers->rb_lock);
  1180. if (rep)
  1181. rpcrdma_destroy_rep(rep);
  1182. }
  1183. /*
  1184. * Put reply buffers back into pool when not attached to
  1185. * request. This happens in error conditions.
  1186. */
  1187. void
  1188. rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
  1189. {
  1190. struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
  1191. if (!rep->rr_temp) {
  1192. spin_lock(&buffers->rb_lock);
  1193. list_add(&rep->rr_list, &buffers->rb_recv_bufs);
  1194. spin_unlock(&buffers->rb_lock);
  1195. } else {
  1196. rpcrdma_destroy_rep(rep);
  1197. }
  1198. }
  1199. /**
  1200. * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
  1201. * @size: size of buffer to be allocated, in bytes
  1202. * @direction: direction of data movement
  1203. * @flags: GFP flags
  1204. *
  1205. * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
  1206. * can be persistently DMA-mapped for I/O.
  1207. *
  1208. * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
  1209. * receiving the payload of RDMA RECV operations. During Long Calls
  1210. * or Replies they may be registered externally via ro_map.
  1211. */
  1212. struct rpcrdma_regbuf *
  1213. rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
  1214. gfp_t flags)
  1215. {
  1216. struct rpcrdma_regbuf *rb;
  1217. rb = kmalloc(sizeof(*rb) + size, flags);
  1218. if (rb == NULL)
  1219. return ERR_PTR(-ENOMEM);
  1220. rb->rg_device = NULL;
  1221. rb->rg_direction = direction;
  1222. rb->rg_iov.length = size;
  1223. return rb;
  1224. }
  1225. /**
  1226. * __rpcrdma_map_regbuf - DMA-map a regbuf
  1227. * @ia: controlling rpcrdma_ia
  1228. * @rb: regbuf to be mapped
  1229. */
  1230. bool
  1231. __rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
  1232. {
  1233. struct ib_device *device = ia->ri_device;
  1234. if (rb->rg_direction == DMA_NONE)
  1235. return false;
  1236. rb->rg_iov.addr = ib_dma_map_single(device,
  1237. (void *)rb->rg_base,
  1238. rdmab_length(rb),
  1239. rb->rg_direction);
  1240. if (ib_dma_mapping_error(device, rdmab_addr(rb)))
  1241. return false;
  1242. rb->rg_device = device;
  1243. rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
  1244. return true;
  1245. }
  1246. static void
  1247. rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
  1248. {
  1249. if (!rb)
  1250. return;
  1251. if (!rpcrdma_regbuf_is_mapped(rb))
  1252. return;
  1253. ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
  1254. rdmab_length(rb), rb->rg_direction);
  1255. rb->rg_device = NULL;
  1256. }
  1257. /**
  1258. * rpcrdma_free_regbuf - deregister and free registered buffer
  1259. * @rb: regbuf to be deregistered and freed
  1260. */
  1261. void
  1262. rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
  1263. {
  1264. rpcrdma_dma_unmap_regbuf(rb);
  1265. kfree(rb);
  1266. }
  1267. /*
  1268. * Prepost any receive buffer, then post send.
  1269. *
  1270. * Receive buffer is donated to hardware, reclaimed upon recv completion.
  1271. */
  1272. int
  1273. rpcrdma_ep_post(struct rpcrdma_ia *ia,
  1274. struct rpcrdma_ep *ep,
  1275. struct rpcrdma_req *req)
  1276. {
  1277. struct ib_send_wr *send_wr = &req->rl_sendctx->sc_wr;
  1278. int rc;
  1279. if (!ep->rep_send_count ||
  1280. test_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags)) {
  1281. send_wr->send_flags |= IB_SEND_SIGNALED;
  1282. ep->rep_send_count = ep->rep_send_batch;
  1283. } else {
  1284. send_wr->send_flags &= ~IB_SEND_SIGNALED;
  1285. --ep->rep_send_count;
  1286. }
  1287. rc = ia->ri_ops->ro_send(ia, req);
  1288. trace_xprtrdma_post_send(req, rc);
  1289. if (rc)
  1290. return -ENOTCONN;
  1291. return 0;
  1292. }
  1293. /**
  1294. * rpcrdma_post_recvs - Maybe post some Receive buffers
  1295. * @r_xprt: controlling transport
  1296. * @temp: when true, allocate temp rpcrdma_rep objects
  1297. *
  1298. */
  1299. void
  1300. rpcrdma_post_recvs(struct rpcrdma_xprt *r_xprt, bool temp)
  1301. {
  1302. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  1303. struct ib_recv_wr *wr, *bad_wr;
  1304. int needed, count, rc;
  1305. needed = buf->rb_credits + (buf->rb_bc_srv_max_requests << 1);
  1306. if (buf->rb_posted_receives > needed)
  1307. return;
  1308. needed -= buf->rb_posted_receives;
  1309. count = 0;
  1310. wr = NULL;
  1311. while (needed) {
  1312. struct rpcrdma_regbuf *rb;
  1313. struct rpcrdma_rep *rep;
  1314. spin_lock(&buf->rb_lock);
  1315. rep = list_first_entry_or_null(&buf->rb_recv_bufs,
  1316. struct rpcrdma_rep, rr_list);
  1317. if (likely(rep))
  1318. list_del(&rep->rr_list);
  1319. spin_unlock(&buf->rb_lock);
  1320. if (!rep) {
  1321. if (rpcrdma_create_rep(r_xprt, temp))
  1322. break;
  1323. continue;
  1324. }
  1325. rb = rep->rr_rdmabuf;
  1326. if (!rpcrdma_regbuf_is_mapped(rb)) {
  1327. if (!__rpcrdma_dma_map_regbuf(&r_xprt->rx_ia, rb)) {
  1328. rpcrdma_recv_buffer_put(rep);
  1329. break;
  1330. }
  1331. }
  1332. trace_xprtrdma_post_recv(rep->rr_recv_wr.wr_cqe);
  1333. rep->rr_recv_wr.next = wr;
  1334. wr = &rep->rr_recv_wr;
  1335. ++count;
  1336. --needed;
  1337. }
  1338. if (!count)
  1339. return;
  1340. rc = ib_post_recv(r_xprt->rx_ia.ri_id->qp, wr,
  1341. (const struct ib_recv_wr **)&bad_wr);
  1342. if (rc) {
  1343. for (wr = bad_wr; wr;) {
  1344. struct rpcrdma_rep *rep;
  1345. rep = container_of(wr, struct rpcrdma_rep, rr_recv_wr);
  1346. wr = wr->next;
  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. }