verbs.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257
  1. /*
  2. * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the BSD-type
  8. * license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials provided
  20. * with the distribution.
  21. *
  22. * Neither the name of the Network Appliance, Inc. nor the names of
  23. * its contributors may be used to endorse or promote products
  24. * derived from this software without specific prior written
  25. * permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. /*
  40. * verbs.c
  41. *
  42. * Encapsulates the major functions managing:
  43. * o adapters
  44. * o endpoints
  45. * o connections
  46. * o buffer memory
  47. */
  48. #include <linux/interrupt.h>
  49. #include <linux/slab.h>
  50. #include <linux/prefetch.h>
  51. #include <asm/bitops.h>
  52. #include "xprt_rdma.h"
  53. /*
  54. * Globals/Macros
  55. */
  56. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  57. # define RPCDBG_FACILITY RPCDBG_TRANS
  58. #endif
  59. static void rpcrdma_reset_frmrs(struct rpcrdma_ia *);
  60. static void rpcrdma_reset_fmrs(struct rpcrdma_ia *);
  61. /*
  62. * internal functions
  63. */
  64. /*
  65. * handle replies in tasklet context, using a single, global list
  66. * rdma tasklet function -- just turn around and call the func
  67. * for all replies on the list
  68. */
  69. static DEFINE_SPINLOCK(rpcrdma_tk_lock_g);
  70. static LIST_HEAD(rpcrdma_tasklets_g);
  71. static void
  72. rpcrdma_run_tasklet(unsigned long data)
  73. {
  74. struct rpcrdma_rep *rep;
  75. void (*func)(struct rpcrdma_rep *);
  76. unsigned long flags;
  77. data = data;
  78. spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
  79. while (!list_empty(&rpcrdma_tasklets_g)) {
  80. rep = list_entry(rpcrdma_tasklets_g.next,
  81. struct rpcrdma_rep, rr_list);
  82. list_del(&rep->rr_list);
  83. func = rep->rr_func;
  84. rep->rr_func = NULL;
  85. spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
  86. if (func)
  87. func(rep);
  88. else
  89. rpcrdma_recv_buffer_put(rep);
  90. spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
  91. }
  92. spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
  93. }
  94. static DECLARE_TASKLET(rpcrdma_tasklet_g, rpcrdma_run_tasklet, 0UL);
  95. static const char * const async_event[] = {
  96. "CQ error",
  97. "QP fatal error",
  98. "QP request error",
  99. "QP access error",
  100. "communication established",
  101. "send queue drained",
  102. "path migration successful",
  103. "path mig error",
  104. "device fatal error",
  105. "port active",
  106. "port error",
  107. "LID change",
  108. "P_key change",
  109. "SM change",
  110. "SRQ error",
  111. "SRQ limit reached",
  112. "last WQE reached",
  113. "client reregister",
  114. "GID change",
  115. };
  116. #define ASYNC_MSG(status) \
  117. ((status) < ARRAY_SIZE(async_event) ? \
  118. async_event[(status)] : "unknown async error")
  119. static void
  120. rpcrdma_schedule_tasklet(struct list_head *sched_list)
  121. {
  122. unsigned long flags;
  123. spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
  124. list_splice_tail(sched_list, &rpcrdma_tasklets_g);
  125. spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
  126. tasklet_schedule(&rpcrdma_tasklet_g);
  127. }
  128. static void
  129. rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
  130. {
  131. struct rpcrdma_ep *ep = context;
  132. pr_err("RPC: %s: %s on device %s ep %p\n",
  133. __func__, ASYNC_MSG(event->event),
  134. event->device->name, context);
  135. if (ep->rep_connected == 1) {
  136. ep->rep_connected = -EIO;
  137. rpcrdma_conn_func(ep);
  138. wake_up_all(&ep->rep_connect_wait);
  139. }
  140. }
  141. static void
  142. rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context)
  143. {
  144. struct rpcrdma_ep *ep = context;
  145. pr_err("RPC: %s: %s on device %s ep %p\n",
  146. __func__, ASYNC_MSG(event->event),
  147. event->device->name, context);
  148. if (ep->rep_connected == 1) {
  149. ep->rep_connected = -EIO;
  150. rpcrdma_conn_func(ep);
  151. wake_up_all(&ep->rep_connect_wait);
  152. }
  153. }
  154. static const char * const wc_status[] = {
  155. "success",
  156. "local length error",
  157. "local QP operation error",
  158. "local EE context operation error",
  159. "local protection error",
  160. "WR flushed",
  161. "memory management operation error",
  162. "bad response error",
  163. "local access error",
  164. "remote invalid request error",
  165. "remote access error",
  166. "remote operation error",
  167. "transport retry counter exceeded",
  168. "RNR retrycounter exceeded",
  169. "local RDD violation error",
  170. "remove invalid RD request",
  171. "operation aborted",
  172. "invalid EE context number",
  173. "invalid EE context state",
  174. "fatal error",
  175. "response timeout error",
  176. "general error",
  177. };
  178. #define COMPLETION_MSG(status) \
  179. ((status) < ARRAY_SIZE(wc_status) ? \
  180. wc_status[(status)] : "unexpected completion error")
  181. static void
  182. rpcrdma_sendcq_process_wc(struct ib_wc *wc)
  183. {
  184. if (likely(wc->status == IB_WC_SUCCESS))
  185. return;
  186. /* WARNING: Only wr_id and status are reliable at this point */
  187. if (wc->wr_id == 0ULL) {
  188. if (wc->status != IB_WC_WR_FLUSH_ERR)
  189. pr_err("RPC: %s: SEND: %s\n",
  190. __func__, COMPLETION_MSG(wc->status));
  191. } else {
  192. struct rpcrdma_mw *r;
  193. r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
  194. r->r.frmr.fr_state = FRMR_IS_STALE;
  195. pr_err("RPC: %s: frmr %p (stale): %s\n",
  196. __func__, r, COMPLETION_MSG(wc->status));
  197. }
  198. }
  199. static int
  200. rpcrdma_sendcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
  201. {
  202. struct ib_wc *wcs;
  203. int budget, count, rc;
  204. budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
  205. do {
  206. wcs = ep->rep_send_wcs;
  207. rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
  208. if (rc <= 0)
  209. return rc;
  210. count = rc;
  211. while (count-- > 0)
  212. rpcrdma_sendcq_process_wc(wcs++);
  213. } while (rc == RPCRDMA_POLLSIZE && --budget);
  214. return 0;
  215. }
  216. /*
  217. * Handle send, fast_reg_mr, and local_inv completions.
  218. *
  219. * Send events are typically suppressed and thus do not result
  220. * in an upcall. Occasionally one is signaled, however. This
  221. * prevents the provider's completion queue from wrapping and
  222. * losing a completion.
  223. */
  224. static void
  225. rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context)
  226. {
  227. struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
  228. int rc;
  229. rc = rpcrdma_sendcq_poll(cq, ep);
  230. if (rc) {
  231. dprintk("RPC: %s: ib_poll_cq failed: %i\n",
  232. __func__, rc);
  233. return;
  234. }
  235. rc = ib_req_notify_cq(cq,
  236. IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
  237. if (rc == 0)
  238. return;
  239. if (rc < 0) {
  240. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  241. __func__, rc);
  242. return;
  243. }
  244. rpcrdma_sendcq_poll(cq, ep);
  245. }
  246. static void
  247. rpcrdma_recvcq_process_wc(struct ib_wc *wc, struct list_head *sched_list)
  248. {
  249. struct rpcrdma_rep *rep =
  250. (struct rpcrdma_rep *)(unsigned long)wc->wr_id;
  251. /* WARNING: Only wr_id and status are reliable at this point */
  252. if (wc->status != IB_WC_SUCCESS)
  253. goto out_fail;
  254. /* status == SUCCESS means all fields in wc are trustworthy */
  255. if (wc->opcode != IB_WC_RECV)
  256. return;
  257. dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
  258. __func__, rep, wc->byte_len);
  259. rep->rr_len = wc->byte_len;
  260. ib_dma_sync_single_for_cpu(rdmab_to_ia(rep->rr_buffer)->ri_id->device,
  261. rdmab_addr(rep->rr_rdmabuf),
  262. rep->rr_len, DMA_FROM_DEVICE);
  263. prefetch(rdmab_to_msg(rep->rr_rdmabuf));
  264. out_schedule:
  265. list_add_tail(&rep->rr_list, sched_list);
  266. return;
  267. out_fail:
  268. if (wc->status != IB_WC_WR_FLUSH_ERR)
  269. pr_err("RPC: %s: rep %p: %s\n",
  270. __func__, rep, COMPLETION_MSG(wc->status));
  271. rep->rr_len = ~0U;
  272. goto out_schedule;
  273. }
  274. static int
  275. rpcrdma_recvcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
  276. {
  277. struct list_head sched_list;
  278. struct ib_wc *wcs;
  279. int budget, count, rc;
  280. INIT_LIST_HEAD(&sched_list);
  281. budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
  282. do {
  283. wcs = ep->rep_recv_wcs;
  284. rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
  285. if (rc <= 0)
  286. goto out_schedule;
  287. count = rc;
  288. while (count-- > 0)
  289. rpcrdma_recvcq_process_wc(wcs++, &sched_list);
  290. } while (rc == RPCRDMA_POLLSIZE && --budget);
  291. rc = 0;
  292. out_schedule:
  293. rpcrdma_schedule_tasklet(&sched_list);
  294. return rc;
  295. }
  296. /*
  297. * Handle receive completions.
  298. *
  299. * It is reentrant but processes single events in order to maintain
  300. * ordering of receives to keep server credits.
  301. *
  302. * It is the responsibility of the scheduled tasklet to return
  303. * recv buffers to the pool. NOTE: this affects synchronization of
  304. * connection shutdown. That is, the structures required for
  305. * the completion of the reply handler must remain intact until
  306. * all memory has been reclaimed.
  307. */
  308. static void
  309. rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
  310. {
  311. struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
  312. int rc;
  313. rc = rpcrdma_recvcq_poll(cq, ep);
  314. if (rc) {
  315. dprintk("RPC: %s: ib_poll_cq failed: %i\n",
  316. __func__, rc);
  317. return;
  318. }
  319. rc = ib_req_notify_cq(cq,
  320. IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
  321. if (rc == 0)
  322. return;
  323. if (rc < 0) {
  324. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  325. __func__, rc);
  326. return;
  327. }
  328. rpcrdma_recvcq_poll(cq, ep);
  329. }
  330. static void
  331. rpcrdma_flush_cqs(struct rpcrdma_ep *ep)
  332. {
  333. struct ib_wc wc;
  334. LIST_HEAD(sched_list);
  335. while (ib_poll_cq(ep->rep_attr.recv_cq, 1, &wc) > 0)
  336. rpcrdma_recvcq_process_wc(&wc, &sched_list);
  337. if (!list_empty(&sched_list))
  338. rpcrdma_schedule_tasklet(&sched_list);
  339. while (ib_poll_cq(ep->rep_attr.send_cq, 1, &wc) > 0)
  340. rpcrdma_sendcq_process_wc(&wc);
  341. }
  342. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  343. static const char * const conn[] = {
  344. "address resolved",
  345. "address error",
  346. "route resolved",
  347. "route error",
  348. "connect request",
  349. "connect response",
  350. "connect error",
  351. "unreachable",
  352. "rejected",
  353. "established",
  354. "disconnected",
  355. "device removal",
  356. "multicast join",
  357. "multicast error",
  358. "address change",
  359. "timewait exit",
  360. };
  361. #define CONNECTION_MSG(status) \
  362. ((status) < ARRAY_SIZE(conn) ? \
  363. conn[(status)] : "unrecognized connection error")
  364. #endif
  365. static int
  366. rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
  367. {
  368. struct rpcrdma_xprt *xprt = id->context;
  369. struct rpcrdma_ia *ia = &xprt->rx_ia;
  370. struct rpcrdma_ep *ep = &xprt->rx_ep;
  371. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  372. struct sockaddr_in *addr = (struct sockaddr_in *) &ep->rep_remote_addr;
  373. #endif
  374. struct ib_qp_attr *attr = &ia->ri_qp_attr;
  375. struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
  376. int connstate = 0;
  377. switch (event->event) {
  378. case RDMA_CM_EVENT_ADDR_RESOLVED:
  379. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  380. ia->ri_async_rc = 0;
  381. complete(&ia->ri_done);
  382. break;
  383. case RDMA_CM_EVENT_ADDR_ERROR:
  384. ia->ri_async_rc = -EHOSTUNREACH;
  385. dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
  386. __func__, ep);
  387. complete(&ia->ri_done);
  388. break;
  389. case RDMA_CM_EVENT_ROUTE_ERROR:
  390. ia->ri_async_rc = -ENETUNREACH;
  391. dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
  392. __func__, ep);
  393. complete(&ia->ri_done);
  394. break;
  395. case RDMA_CM_EVENT_ESTABLISHED:
  396. connstate = 1;
  397. ib_query_qp(ia->ri_id->qp, attr,
  398. IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
  399. iattr);
  400. dprintk("RPC: %s: %d responder resources"
  401. " (%d initiator)\n",
  402. __func__, attr->max_dest_rd_atomic,
  403. attr->max_rd_atomic);
  404. goto connected;
  405. case RDMA_CM_EVENT_CONNECT_ERROR:
  406. connstate = -ENOTCONN;
  407. goto connected;
  408. case RDMA_CM_EVENT_UNREACHABLE:
  409. connstate = -ENETDOWN;
  410. goto connected;
  411. case RDMA_CM_EVENT_REJECTED:
  412. connstate = -ECONNREFUSED;
  413. goto connected;
  414. case RDMA_CM_EVENT_DISCONNECTED:
  415. connstate = -ECONNABORTED;
  416. goto connected;
  417. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  418. connstate = -ENODEV;
  419. connected:
  420. dprintk("RPC: %s: %sconnected\n",
  421. __func__, connstate > 0 ? "" : "dis");
  422. ep->rep_connected = connstate;
  423. rpcrdma_conn_func(ep);
  424. wake_up_all(&ep->rep_connect_wait);
  425. /*FALLTHROUGH*/
  426. default:
  427. dprintk("RPC: %s: %pI4:%u (ep 0x%p): %s\n",
  428. __func__, &addr->sin_addr.s_addr,
  429. ntohs(addr->sin_port), ep,
  430. CONNECTION_MSG(event->event));
  431. break;
  432. }
  433. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  434. if (connstate == 1) {
  435. int ird = attr->max_dest_rd_atomic;
  436. int tird = ep->rep_remote_cma.responder_resources;
  437. printk(KERN_INFO "rpcrdma: connection to %pI4:%u "
  438. "on %s, memreg %d slots %d ird %d%s\n",
  439. &addr->sin_addr.s_addr,
  440. ntohs(addr->sin_port),
  441. ia->ri_id->device->name,
  442. ia->ri_memreg_strategy,
  443. xprt->rx_buf.rb_max_requests,
  444. ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
  445. } else if (connstate < 0) {
  446. printk(KERN_INFO "rpcrdma: connection to %pI4:%u closed (%d)\n",
  447. &addr->sin_addr.s_addr,
  448. ntohs(addr->sin_port),
  449. connstate);
  450. }
  451. #endif
  452. return 0;
  453. }
  454. static struct rdma_cm_id *
  455. rpcrdma_create_id(struct rpcrdma_xprt *xprt,
  456. struct rpcrdma_ia *ia, struct sockaddr *addr)
  457. {
  458. struct rdma_cm_id *id;
  459. int rc;
  460. init_completion(&ia->ri_done);
  461. id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC);
  462. if (IS_ERR(id)) {
  463. rc = PTR_ERR(id);
  464. dprintk("RPC: %s: rdma_create_id() failed %i\n",
  465. __func__, rc);
  466. return id;
  467. }
  468. ia->ri_async_rc = -ETIMEDOUT;
  469. rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
  470. if (rc) {
  471. dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
  472. __func__, rc);
  473. goto out;
  474. }
  475. wait_for_completion_interruptible_timeout(&ia->ri_done,
  476. msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
  477. rc = ia->ri_async_rc;
  478. if (rc)
  479. goto out;
  480. ia->ri_async_rc = -ETIMEDOUT;
  481. rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
  482. if (rc) {
  483. dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
  484. __func__, rc);
  485. goto out;
  486. }
  487. wait_for_completion_interruptible_timeout(&ia->ri_done,
  488. msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
  489. rc = ia->ri_async_rc;
  490. if (rc)
  491. goto out;
  492. return id;
  493. out:
  494. rdma_destroy_id(id);
  495. return ERR_PTR(rc);
  496. }
  497. /*
  498. * Drain any cq, prior to teardown.
  499. */
  500. static void
  501. rpcrdma_clean_cq(struct ib_cq *cq)
  502. {
  503. struct ib_wc wc;
  504. int count = 0;
  505. while (1 == ib_poll_cq(cq, 1, &wc))
  506. ++count;
  507. if (count)
  508. dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
  509. __func__, count, wc.opcode);
  510. }
  511. /*
  512. * Exported functions.
  513. */
  514. /*
  515. * Open and initialize an Interface Adapter.
  516. * o initializes fields of struct rpcrdma_ia, including
  517. * interface and provider attributes and protection zone.
  518. */
  519. int
  520. rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
  521. {
  522. int rc, mem_priv;
  523. struct rpcrdma_ia *ia = &xprt->rx_ia;
  524. struct ib_device_attr *devattr = &ia->ri_devattr;
  525. ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
  526. if (IS_ERR(ia->ri_id)) {
  527. rc = PTR_ERR(ia->ri_id);
  528. goto out1;
  529. }
  530. ia->ri_pd = ib_alloc_pd(ia->ri_id->device);
  531. if (IS_ERR(ia->ri_pd)) {
  532. rc = PTR_ERR(ia->ri_pd);
  533. dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
  534. __func__, rc);
  535. goto out2;
  536. }
  537. rc = ib_query_device(ia->ri_id->device, devattr);
  538. if (rc) {
  539. dprintk("RPC: %s: ib_query_device failed %d\n",
  540. __func__, rc);
  541. goto out3;
  542. }
  543. if (devattr->device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) {
  544. ia->ri_have_dma_lkey = 1;
  545. ia->ri_dma_lkey = ia->ri_id->device->local_dma_lkey;
  546. }
  547. if (memreg == RPCRDMA_FRMR) {
  548. /* Requires both frmr reg and local dma lkey */
  549. if ((devattr->device_cap_flags &
  550. (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) !=
  551. (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) {
  552. dprintk("RPC: %s: FRMR registration "
  553. "not supported by HCA\n", __func__);
  554. memreg = RPCRDMA_MTHCAFMR;
  555. } else {
  556. /* Mind the ia limit on FRMR page list depth */
  557. ia->ri_max_frmr_depth = min_t(unsigned int,
  558. RPCRDMA_MAX_DATA_SEGS,
  559. devattr->max_fast_reg_page_list_len);
  560. }
  561. }
  562. if (memreg == RPCRDMA_MTHCAFMR) {
  563. if (!ia->ri_id->device->alloc_fmr) {
  564. dprintk("RPC: %s: MTHCAFMR registration "
  565. "not supported by HCA\n", __func__);
  566. memreg = RPCRDMA_ALLPHYSICAL;
  567. }
  568. }
  569. /*
  570. * Optionally obtain an underlying physical identity mapping in
  571. * order to do a memory window-based bind. This base registration
  572. * is protected from remote access - that is enabled only by binding
  573. * for the specific bytes targeted during each RPC operation, and
  574. * revoked after the corresponding completion similar to a storage
  575. * adapter.
  576. */
  577. switch (memreg) {
  578. case RPCRDMA_FRMR:
  579. break;
  580. case RPCRDMA_ALLPHYSICAL:
  581. mem_priv = IB_ACCESS_LOCAL_WRITE |
  582. IB_ACCESS_REMOTE_WRITE |
  583. IB_ACCESS_REMOTE_READ;
  584. goto register_setup;
  585. case RPCRDMA_MTHCAFMR:
  586. if (ia->ri_have_dma_lkey)
  587. break;
  588. mem_priv = IB_ACCESS_LOCAL_WRITE;
  589. register_setup:
  590. ia->ri_bind_mem = ib_get_dma_mr(ia->ri_pd, mem_priv);
  591. if (IS_ERR(ia->ri_bind_mem)) {
  592. printk(KERN_ALERT "%s: ib_get_dma_mr for "
  593. "phys register failed with %lX\n",
  594. __func__, PTR_ERR(ia->ri_bind_mem));
  595. rc = -ENOMEM;
  596. goto out3;
  597. }
  598. break;
  599. default:
  600. printk(KERN_ERR "RPC: Unsupported memory "
  601. "registration mode: %d\n", memreg);
  602. rc = -ENOMEM;
  603. goto out3;
  604. }
  605. dprintk("RPC: %s: memory registration strategy is %d\n",
  606. __func__, memreg);
  607. /* Else will do memory reg/dereg for each chunk */
  608. ia->ri_memreg_strategy = memreg;
  609. rwlock_init(&ia->ri_qplock);
  610. return 0;
  611. out3:
  612. ib_dealloc_pd(ia->ri_pd);
  613. ia->ri_pd = NULL;
  614. out2:
  615. rdma_destroy_id(ia->ri_id);
  616. ia->ri_id = NULL;
  617. out1:
  618. return rc;
  619. }
  620. /*
  621. * Clean up/close an IA.
  622. * o if event handles and PD have been initialized, free them.
  623. * o close the IA
  624. */
  625. void
  626. rpcrdma_ia_close(struct rpcrdma_ia *ia)
  627. {
  628. int rc;
  629. dprintk("RPC: %s: entering\n", __func__);
  630. if (ia->ri_bind_mem != NULL) {
  631. rc = ib_dereg_mr(ia->ri_bind_mem);
  632. dprintk("RPC: %s: ib_dereg_mr returned %i\n",
  633. __func__, rc);
  634. }
  635. if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
  636. if (ia->ri_id->qp)
  637. rdma_destroy_qp(ia->ri_id);
  638. rdma_destroy_id(ia->ri_id);
  639. ia->ri_id = NULL;
  640. }
  641. if (ia->ri_pd != NULL && !IS_ERR(ia->ri_pd)) {
  642. rc = ib_dealloc_pd(ia->ri_pd);
  643. dprintk("RPC: %s: ib_dealloc_pd returned %i\n",
  644. __func__, rc);
  645. }
  646. }
  647. /*
  648. * Create unconnected endpoint.
  649. */
  650. int
  651. rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
  652. struct rpcrdma_create_data_internal *cdata)
  653. {
  654. struct ib_device_attr *devattr = &ia->ri_devattr;
  655. struct ib_cq *sendcq, *recvcq;
  656. int rc, err;
  657. /* check provider's send/recv wr limits */
  658. if (cdata->max_requests > devattr->max_qp_wr)
  659. cdata->max_requests = devattr->max_qp_wr;
  660. ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
  661. ep->rep_attr.qp_context = ep;
  662. /* send_cq and recv_cq initialized below */
  663. ep->rep_attr.srq = NULL;
  664. ep->rep_attr.cap.max_send_wr = cdata->max_requests;
  665. switch (ia->ri_memreg_strategy) {
  666. case RPCRDMA_FRMR: {
  667. int depth = 7;
  668. /* Add room for frmr register and invalidate WRs.
  669. * 1. FRMR reg WR for head
  670. * 2. FRMR invalidate WR for head
  671. * 3. N FRMR reg WRs for pagelist
  672. * 4. N FRMR invalidate WRs for pagelist
  673. * 5. FRMR reg WR for tail
  674. * 6. FRMR invalidate WR for tail
  675. * 7. The RDMA_SEND WR
  676. */
  677. /* Calculate N if the device max FRMR depth is smaller than
  678. * RPCRDMA_MAX_DATA_SEGS.
  679. */
  680. if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) {
  681. int delta = RPCRDMA_MAX_DATA_SEGS -
  682. ia->ri_max_frmr_depth;
  683. do {
  684. depth += 2; /* FRMR reg + invalidate */
  685. delta -= ia->ri_max_frmr_depth;
  686. } while (delta > 0);
  687. }
  688. ep->rep_attr.cap.max_send_wr *= depth;
  689. if (ep->rep_attr.cap.max_send_wr > devattr->max_qp_wr) {
  690. cdata->max_requests = devattr->max_qp_wr / depth;
  691. if (!cdata->max_requests)
  692. return -EINVAL;
  693. ep->rep_attr.cap.max_send_wr = cdata->max_requests *
  694. depth;
  695. }
  696. break;
  697. }
  698. default:
  699. break;
  700. }
  701. ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
  702. ep->rep_attr.cap.max_send_sge = (cdata->padding ? 4 : 2);
  703. ep->rep_attr.cap.max_recv_sge = 1;
  704. ep->rep_attr.cap.max_inline_data = 0;
  705. ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  706. ep->rep_attr.qp_type = IB_QPT_RC;
  707. ep->rep_attr.port_num = ~0;
  708. if (cdata->padding) {
  709. ep->rep_padbuf = rpcrdma_alloc_regbuf(ia, cdata->padding,
  710. GFP_KERNEL);
  711. if (IS_ERR(ep->rep_padbuf))
  712. return PTR_ERR(ep->rep_padbuf);
  713. } else
  714. ep->rep_padbuf = NULL;
  715. dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
  716. "iovs: send %d recv %d\n",
  717. __func__,
  718. ep->rep_attr.cap.max_send_wr,
  719. ep->rep_attr.cap.max_recv_wr,
  720. ep->rep_attr.cap.max_send_sge,
  721. ep->rep_attr.cap.max_recv_sge);
  722. /* set trigger for requesting send completion */
  723. ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
  724. if (ep->rep_cqinit > RPCRDMA_MAX_UNSIGNALED_SENDS)
  725. ep->rep_cqinit = RPCRDMA_MAX_UNSIGNALED_SENDS;
  726. else if (ep->rep_cqinit <= 2)
  727. ep->rep_cqinit = 0;
  728. INIT_CQCOUNT(ep);
  729. init_waitqueue_head(&ep->rep_connect_wait);
  730. INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
  731. sendcq = ib_create_cq(ia->ri_id->device, rpcrdma_sendcq_upcall,
  732. rpcrdma_cq_async_error_upcall, ep,
  733. ep->rep_attr.cap.max_send_wr + 1, 0);
  734. if (IS_ERR(sendcq)) {
  735. rc = PTR_ERR(sendcq);
  736. dprintk("RPC: %s: failed to create send CQ: %i\n",
  737. __func__, rc);
  738. goto out1;
  739. }
  740. rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
  741. if (rc) {
  742. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  743. __func__, rc);
  744. goto out2;
  745. }
  746. recvcq = ib_create_cq(ia->ri_id->device, rpcrdma_recvcq_upcall,
  747. rpcrdma_cq_async_error_upcall, ep,
  748. ep->rep_attr.cap.max_recv_wr + 1, 0);
  749. if (IS_ERR(recvcq)) {
  750. rc = PTR_ERR(recvcq);
  751. dprintk("RPC: %s: failed to create recv CQ: %i\n",
  752. __func__, rc);
  753. goto out2;
  754. }
  755. rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
  756. if (rc) {
  757. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  758. __func__, rc);
  759. ib_destroy_cq(recvcq);
  760. goto out2;
  761. }
  762. ep->rep_attr.send_cq = sendcq;
  763. ep->rep_attr.recv_cq = recvcq;
  764. /* Initialize cma parameters */
  765. /* RPC/RDMA does not use private data */
  766. ep->rep_remote_cma.private_data = NULL;
  767. ep->rep_remote_cma.private_data_len = 0;
  768. /* Client offers RDMA Read but does not initiate */
  769. ep->rep_remote_cma.initiator_depth = 0;
  770. if (devattr->max_qp_rd_atom > 32) /* arbitrary but <= 255 */
  771. ep->rep_remote_cma.responder_resources = 32;
  772. else
  773. ep->rep_remote_cma.responder_resources =
  774. devattr->max_qp_rd_atom;
  775. ep->rep_remote_cma.retry_count = 7;
  776. ep->rep_remote_cma.flow_control = 0;
  777. ep->rep_remote_cma.rnr_retry_count = 0;
  778. return 0;
  779. out2:
  780. err = ib_destroy_cq(sendcq);
  781. if (err)
  782. dprintk("RPC: %s: ib_destroy_cq returned %i\n",
  783. __func__, err);
  784. out1:
  785. rpcrdma_free_regbuf(ia, ep->rep_padbuf);
  786. return rc;
  787. }
  788. /*
  789. * rpcrdma_ep_destroy
  790. *
  791. * Disconnect and destroy endpoint. After this, the only
  792. * valid operations on the ep are to free it (if dynamically
  793. * allocated) or re-create it.
  794. */
  795. void
  796. rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  797. {
  798. int rc;
  799. dprintk("RPC: %s: entering, connected is %d\n",
  800. __func__, ep->rep_connected);
  801. cancel_delayed_work_sync(&ep->rep_connect_worker);
  802. if (ia->ri_id->qp) {
  803. rpcrdma_ep_disconnect(ep, ia);
  804. rdma_destroy_qp(ia->ri_id);
  805. ia->ri_id->qp = NULL;
  806. }
  807. rpcrdma_free_regbuf(ia, ep->rep_padbuf);
  808. rpcrdma_clean_cq(ep->rep_attr.recv_cq);
  809. rc = ib_destroy_cq(ep->rep_attr.recv_cq);
  810. if (rc)
  811. dprintk("RPC: %s: ib_destroy_cq returned %i\n",
  812. __func__, rc);
  813. rpcrdma_clean_cq(ep->rep_attr.send_cq);
  814. rc = ib_destroy_cq(ep->rep_attr.send_cq);
  815. if (rc)
  816. dprintk("RPC: %s: ib_destroy_cq returned %i\n",
  817. __func__, rc);
  818. }
  819. /*
  820. * Connect unconnected endpoint.
  821. */
  822. int
  823. rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  824. {
  825. struct rdma_cm_id *id, *old;
  826. int rc = 0;
  827. int retry_count = 0;
  828. if (ep->rep_connected != 0) {
  829. struct rpcrdma_xprt *xprt;
  830. retry:
  831. dprintk("RPC: %s: reconnecting...\n", __func__);
  832. rpcrdma_ep_disconnect(ep, ia);
  833. rpcrdma_flush_cqs(ep);
  834. switch (ia->ri_memreg_strategy) {
  835. case RPCRDMA_FRMR:
  836. rpcrdma_reset_frmrs(ia);
  837. break;
  838. case RPCRDMA_MTHCAFMR:
  839. rpcrdma_reset_fmrs(ia);
  840. break;
  841. case RPCRDMA_ALLPHYSICAL:
  842. break;
  843. default:
  844. rc = -EIO;
  845. goto out;
  846. }
  847. xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
  848. id = rpcrdma_create_id(xprt, ia,
  849. (struct sockaddr *)&xprt->rx_data.addr);
  850. if (IS_ERR(id)) {
  851. rc = -EHOSTUNREACH;
  852. goto out;
  853. }
  854. /* TEMP TEMP TEMP - fail if new device:
  855. * Deregister/remarshal *all* requests!
  856. * Close and recreate adapter, pd, etc!
  857. * Re-determine all attributes still sane!
  858. * More stuff I haven't thought of!
  859. * Rrrgh!
  860. */
  861. if (ia->ri_id->device != id->device) {
  862. printk("RPC: %s: can't reconnect on "
  863. "different device!\n", __func__);
  864. rdma_destroy_id(id);
  865. rc = -ENETUNREACH;
  866. goto out;
  867. }
  868. /* END TEMP */
  869. rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
  870. if (rc) {
  871. dprintk("RPC: %s: rdma_create_qp failed %i\n",
  872. __func__, rc);
  873. rdma_destroy_id(id);
  874. rc = -ENETUNREACH;
  875. goto out;
  876. }
  877. write_lock(&ia->ri_qplock);
  878. old = ia->ri_id;
  879. ia->ri_id = id;
  880. write_unlock(&ia->ri_qplock);
  881. rdma_destroy_qp(old);
  882. rdma_destroy_id(old);
  883. } else {
  884. dprintk("RPC: %s: connecting...\n", __func__);
  885. rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
  886. if (rc) {
  887. dprintk("RPC: %s: rdma_create_qp failed %i\n",
  888. __func__, rc);
  889. /* do not update ep->rep_connected */
  890. return -ENETUNREACH;
  891. }
  892. }
  893. ep->rep_connected = 0;
  894. rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
  895. if (rc) {
  896. dprintk("RPC: %s: rdma_connect() failed with %i\n",
  897. __func__, rc);
  898. goto out;
  899. }
  900. wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
  901. /*
  902. * Check state. A non-peer reject indicates no listener
  903. * (ECONNREFUSED), which may be a transient state. All
  904. * others indicate a transport condition which has already
  905. * undergone a best-effort.
  906. */
  907. if (ep->rep_connected == -ECONNREFUSED &&
  908. ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
  909. dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
  910. goto retry;
  911. }
  912. if (ep->rep_connected <= 0) {
  913. /* Sometimes, the only way to reliably connect to remote
  914. * CMs is to use same nonzero values for ORD and IRD. */
  915. if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
  916. (ep->rep_remote_cma.responder_resources == 0 ||
  917. ep->rep_remote_cma.initiator_depth !=
  918. ep->rep_remote_cma.responder_resources)) {
  919. if (ep->rep_remote_cma.responder_resources == 0)
  920. ep->rep_remote_cma.responder_resources = 1;
  921. ep->rep_remote_cma.initiator_depth =
  922. ep->rep_remote_cma.responder_resources;
  923. goto retry;
  924. }
  925. rc = ep->rep_connected;
  926. } else {
  927. dprintk("RPC: %s: connected\n", __func__);
  928. }
  929. out:
  930. if (rc)
  931. ep->rep_connected = rc;
  932. return rc;
  933. }
  934. /*
  935. * rpcrdma_ep_disconnect
  936. *
  937. * This is separate from destroy to facilitate the ability
  938. * to reconnect without recreating the endpoint.
  939. *
  940. * This call is not reentrant, and must not be made in parallel
  941. * on the same endpoint.
  942. */
  943. void
  944. rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  945. {
  946. int rc;
  947. rpcrdma_flush_cqs(ep);
  948. rc = rdma_disconnect(ia->ri_id);
  949. if (!rc) {
  950. /* returns without wait if not connected */
  951. wait_event_interruptible(ep->rep_connect_wait,
  952. ep->rep_connected != 1);
  953. dprintk("RPC: %s: after wait, %sconnected\n", __func__,
  954. (ep->rep_connected == 1) ? "still " : "dis");
  955. } else {
  956. dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
  957. ep->rep_connected = rc;
  958. }
  959. }
  960. static struct rpcrdma_req *
  961. rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
  962. {
  963. struct rpcrdma_req *req;
  964. req = kzalloc(sizeof(*req), GFP_KERNEL);
  965. if (req == NULL)
  966. return ERR_PTR(-ENOMEM);
  967. req->rl_buffer = &r_xprt->rx_buf;
  968. return req;
  969. }
  970. static struct rpcrdma_rep *
  971. rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
  972. {
  973. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  974. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  975. struct rpcrdma_rep *rep;
  976. int rc;
  977. rc = -ENOMEM;
  978. rep = kzalloc(sizeof(*rep), GFP_KERNEL);
  979. if (rep == NULL)
  980. goto out;
  981. rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize,
  982. GFP_KERNEL);
  983. if (IS_ERR(rep->rr_rdmabuf)) {
  984. rc = PTR_ERR(rep->rr_rdmabuf);
  985. goto out_free;
  986. }
  987. rep->rr_buffer = &r_xprt->rx_buf;
  988. return rep;
  989. out_free:
  990. kfree(rep);
  991. out:
  992. return ERR_PTR(rc);
  993. }
  994. static int
  995. rpcrdma_init_fmrs(struct rpcrdma_ia *ia, struct rpcrdma_buffer *buf)
  996. {
  997. int mr_access_flags = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ;
  998. struct ib_fmr_attr fmr_attr = {
  999. .max_pages = RPCRDMA_MAX_DATA_SEGS,
  1000. .max_maps = 1,
  1001. .page_shift = PAGE_SHIFT
  1002. };
  1003. struct rpcrdma_mw *r;
  1004. int i, rc;
  1005. i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS;
  1006. dprintk("RPC: %s: initalizing %d FMRs\n", __func__, i);
  1007. while (i--) {
  1008. r = kzalloc(sizeof(*r), GFP_KERNEL);
  1009. if (r == NULL)
  1010. return -ENOMEM;
  1011. r->r.fmr = ib_alloc_fmr(ia->ri_pd, mr_access_flags, &fmr_attr);
  1012. if (IS_ERR(r->r.fmr)) {
  1013. rc = PTR_ERR(r->r.fmr);
  1014. dprintk("RPC: %s: ib_alloc_fmr failed %i\n",
  1015. __func__, rc);
  1016. goto out_free;
  1017. }
  1018. list_add(&r->mw_list, &buf->rb_mws);
  1019. list_add(&r->mw_all, &buf->rb_all);
  1020. }
  1021. return 0;
  1022. out_free:
  1023. kfree(r);
  1024. return rc;
  1025. }
  1026. static int
  1027. rpcrdma_init_frmrs(struct rpcrdma_ia *ia, struct rpcrdma_buffer *buf)
  1028. {
  1029. struct rpcrdma_frmr *f;
  1030. struct rpcrdma_mw *r;
  1031. int i, rc;
  1032. i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS;
  1033. dprintk("RPC: %s: initalizing %d FRMRs\n", __func__, i);
  1034. while (i--) {
  1035. r = kzalloc(sizeof(*r), GFP_KERNEL);
  1036. if (r == NULL)
  1037. return -ENOMEM;
  1038. f = &r->r.frmr;
  1039. f->fr_mr = ib_alloc_fast_reg_mr(ia->ri_pd,
  1040. ia->ri_max_frmr_depth);
  1041. if (IS_ERR(f->fr_mr)) {
  1042. rc = PTR_ERR(f->fr_mr);
  1043. dprintk("RPC: %s: ib_alloc_fast_reg_mr "
  1044. "failed %i\n", __func__, rc);
  1045. goto out_free;
  1046. }
  1047. f->fr_pgl = ib_alloc_fast_reg_page_list(ia->ri_id->device,
  1048. ia->ri_max_frmr_depth);
  1049. if (IS_ERR(f->fr_pgl)) {
  1050. rc = PTR_ERR(f->fr_pgl);
  1051. dprintk("RPC: %s: ib_alloc_fast_reg_page_list "
  1052. "failed %i\n", __func__, rc);
  1053. ib_dereg_mr(f->fr_mr);
  1054. goto out_free;
  1055. }
  1056. list_add(&r->mw_list, &buf->rb_mws);
  1057. list_add(&r->mw_all, &buf->rb_all);
  1058. }
  1059. return 0;
  1060. out_free:
  1061. kfree(r);
  1062. return rc;
  1063. }
  1064. int
  1065. rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
  1066. {
  1067. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  1068. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  1069. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  1070. char *p;
  1071. size_t len;
  1072. int i, rc;
  1073. buf->rb_max_requests = cdata->max_requests;
  1074. spin_lock_init(&buf->rb_lock);
  1075. /* Need to allocate:
  1076. * 1. arrays for send and recv pointers
  1077. * 2. arrays of struct rpcrdma_req to fill in pointers
  1078. * 3. array of struct rpcrdma_rep for replies
  1079. * Send/recv buffers in req/rep need to be registered
  1080. */
  1081. len = buf->rb_max_requests *
  1082. (sizeof(struct rpcrdma_req *) + sizeof(struct rpcrdma_rep *));
  1083. p = kzalloc(len, GFP_KERNEL);
  1084. if (p == NULL) {
  1085. dprintk("RPC: %s: req_t/rep_t/pad kzalloc(%zd) failed\n",
  1086. __func__, len);
  1087. rc = -ENOMEM;
  1088. goto out;
  1089. }
  1090. buf->rb_pool = p; /* for freeing it later */
  1091. buf->rb_send_bufs = (struct rpcrdma_req **) p;
  1092. p = (char *) &buf->rb_send_bufs[buf->rb_max_requests];
  1093. buf->rb_recv_bufs = (struct rpcrdma_rep **) p;
  1094. p = (char *) &buf->rb_recv_bufs[buf->rb_max_requests];
  1095. INIT_LIST_HEAD(&buf->rb_mws);
  1096. INIT_LIST_HEAD(&buf->rb_all);
  1097. switch (ia->ri_memreg_strategy) {
  1098. case RPCRDMA_FRMR:
  1099. rc = rpcrdma_init_frmrs(ia, buf);
  1100. if (rc)
  1101. goto out;
  1102. break;
  1103. case RPCRDMA_MTHCAFMR:
  1104. rc = rpcrdma_init_fmrs(ia, buf);
  1105. if (rc)
  1106. goto out;
  1107. break;
  1108. default:
  1109. break;
  1110. }
  1111. for (i = 0; i < buf->rb_max_requests; i++) {
  1112. struct rpcrdma_req *req;
  1113. struct rpcrdma_rep *rep;
  1114. req = rpcrdma_create_req(r_xprt);
  1115. if (IS_ERR(req)) {
  1116. dprintk("RPC: %s: request buffer %d alloc"
  1117. " failed\n", __func__, i);
  1118. rc = PTR_ERR(req);
  1119. goto out;
  1120. }
  1121. buf->rb_send_bufs[i] = req;
  1122. rep = rpcrdma_create_rep(r_xprt);
  1123. if (IS_ERR(rep)) {
  1124. dprintk("RPC: %s: reply buffer %d alloc failed\n",
  1125. __func__, i);
  1126. rc = PTR_ERR(rep);
  1127. goto out;
  1128. }
  1129. buf->rb_recv_bufs[i] = rep;
  1130. }
  1131. return 0;
  1132. out:
  1133. rpcrdma_buffer_destroy(buf);
  1134. return rc;
  1135. }
  1136. static void
  1137. rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
  1138. {
  1139. if (!rep)
  1140. return;
  1141. rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
  1142. kfree(rep);
  1143. }
  1144. static void
  1145. rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
  1146. {
  1147. if (!req)
  1148. return;
  1149. rpcrdma_free_regbuf(ia, req->rl_sendbuf);
  1150. rpcrdma_free_regbuf(ia, req->rl_rdmabuf);
  1151. kfree(req);
  1152. }
  1153. static void
  1154. rpcrdma_destroy_fmrs(struct rpcrdma_buffer *buf)
  1155. {
  1156. struct rpcrdma_mw *r;
  1157. int rc;
  1158. while (!list_empty(&buf->rb_all)) {
  1159. r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
  1160. list_del(&r->mw_all);
  1161. list_del(&r->mw_list);
  1162. rc = ib_dealloc_fmr(r->r.fmr);
  1163. if (rc)
  1164. dprintk("RPC: %s: ib_dealloc_fmr failed %i\n",
  1165. __func__, rc);
  1166. kfree(r);
  1167. }
  1168. }
  1169. static void
  1170. rpcrdma_destroy_frmrs(struct rpcrdma_buffer *buf)
  1171. {
  1172. struct rpcrdma_mw *r;
  1173. int rc;
  1174. while (!list_empty(&buf->rb_all)) {
  1175. r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
  1176. list_del(&r->mw_all);
  1177. list_del(&r->mw_list);
  1178. rc = ib_dereg_mr(r->r.frmr.fr_mr);
  1179. if (rc)
  1180. dprintk("RPC: %s: ib_dereg_mr failed %i\n",
  1181. __func__, rc);
  1182. ib_free_fast_reg_page_list(r->r.frmr.fr_pgl);
  1183. kfree(r);
  1184. }
  1185. }
  1186. void
  1187. rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
  1188. {
  1189. struct rpcrdma_ia *ia = rdmab_to_ia(buf);
  1190. int i;
  1191. /* clean up in reverse order from create
  1192. * 1. recv mr memory (mr free, then kfree)
  1193. * 2. send mr memory (mr free, then kfree)
  1194. * 3. MWs
  1195. */
  1196. dprintk("RPC: %s: entering\n", __func__);
  1197. for (i = 0; i < buf->rb_max_requests; i++) {
  1198. if (buf->rb_recv_bufs)
  1199. rpcrdma_destroy_rep(ia, buf->rb_recv_bufs[i]);
  1200. if (buf->rb_send_bufs)
  1201. rpcrdma_destroy_req(ia, buf->rb_send_bufs[i]);
  1202. }
  1203. switch (ia->ri_memreg_strategy) {
  1204. case RPCRDMA_FRMR:
  1205. rpcrdma_destroy_frmrs(buf);
  1206. break;
  1207. case RPCRDMA_MTHCAFMR:
  1208. rpcrdma_destroy_fmrs(buf);
  1209. break;
  1210. default:
  1211. break;
  1212. }
  1213. kfree(buf->rb_pool);
  1214. }
  1215. /* After a disconnect, unmap all FMRs.
  1216. *
  1217. * This is invoked only in the transport connect worker in order
  1218. * to serialize with rpcrdma_register_fmr_external().
  1219. */
  1220. static void
  1221. rpcrdma_reset_fmrs(struct rpcrdma_ia *ia)
  1222. {
  1223. struct rpcrdma_xprt *r_xprt =
  1224. container_of(ia, struct rpcrdma_xprt, rx_ia);
  1225. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  1226. struct list_head *pos;
  1227. struct rpcrdma_mw *r;
  1228. LIST_HEAD(l);
  1229. int rc;
  1230. list_for_each(pos, &buf->rb_all) {
  1231. r = list_entry(pos, struct rpcrdma_mw, mw_all);
  1232. INIT_LIST_HEAD(&l);
  1233. list_add(&r->r.fmr->list, &l);
  1234. rc = ib_unmap_fmr(&l);
  1235. if (rc)
  1236. dprintk("RPC: %s: ib_unmap_fmr failed %i\n",
  1237. __func__, rc);
  1238. }
  1239. }
  1240. /* After a disconnect, a flushed FAST_REG_MR can leave an FRMR in
  1241. * an unusable state. Find FRMRs in this state and dereg / reg
  1242. * each. FRMRs that are VALID and attached to an rpcrdma_req are
  1243. * also torn down.
  1244. *
  1245. * This gives all in-use FRMRs a fresh rkey and leaves them INVALID.
  1246. *
  1247. * This is invoked only in the transport connect worker in order
  1248. * to serialize with rpcrdma_register_frmr_external().
  1249. */
  1250. static void
  1251. rpcrdma_reset_frmrs(struct rpcrdma_ia *ia)
  1252. {
  1253. struct rpcrdma_xprt *r_xprt =
  1254. container_of(ia, struct rpcrdma_xprt, rx_ia);
  1255. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  1256. struct list_head *pos;
  1257. struct rpcrdma_mw *r;
  1258. int rc;
  1259. list_for_each(pos, &buf->rb_all) {
  1260. r = list_entry(pos, struct rpcrdma_mw, mw_all);
  1261. if (r->r.frmr.fr_state == FRMR_IS_INVALID)
  1262. continue;
  1263. rc = ib_dereg_mr(r->r.frmr.fr_mr);
  1264. if (rc)
  1265. dprintk("RPC: %s: ib_dereg_mr failed %i\n",
  1266. __func__, rc);
  1267. ib_free_fast_reg_page_list(r->r.frmr.fr_pgl);
  1268. r->r.frmr.fr_mr = ib_alloc_fast_reg_mr(ia->ri_pd,
  1269. ia->ri_max_frmr_depth);
  1270. if (IS_ERR(r->r.frmr.fr_mr)) {
  1271. rc = PTR_ERR(r->r.frmr.fr_mr);
  1272. dprintk("RPC: %s: ib_alloc_fast_reg_mr"
  1273. " failed %i\n", __func__, rc);
  1274. continue;
  1275. }
  1276. r->r.frmr.fr_pgl = ib_alloc_fast_reg_page_list(
  1277. ia->ri_id->device,
  1278. ia->ri_max_frmr_depth);
  1279. if (IS_ERR(r->r.frmr.fr_pgl)) {
  1280. rc = PTR_ERR(r->r.frmr.fr_pgl);
  1281. dprintk("RPC: %s: "
  1282. "ib_alloc_fast_reg_page_list "
  1283. "failed %i\n", __func__, rc);
  1284. ib_dereg_mr(r->r.frmr.fr_mr);
  1285. continue;
  1286. }
  1287. r->r.frmr.fr_state = FRMR_IS_INVALID;
  1288. }
  1289. }
  1290. /* "*mw" can be NULL when rpcrdma_buffer_get_mrs() fails, leaving
  1291. * some req segments uninitialized.
  1292. */
  1293. static void
  1294. rpcrdma_buffer_put_mr(struct rpcrdma_mw **mw, struct rpcrdma_buffer *buf)
  1295. {
  1296. if (*mw) {
  1297. list_add_tail(&(*mw)->mw_list, &buf->rb_mws);
  1298. *mw = NULL;
  1299. }
  1300. }
  1301. /* Cycle mw's back in reverse order, and "spin" them.
  1302. * This delays and scrambles reuse as much as possible.
  1303. */
  1304. static void
  1305. rpcrdma_buffer_put_mrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
  1306. {
  1307. struct rpcrdma_mr_seg *seg = req->rl_segments;
  1308. struct rpcrdma_mr_seg *seg1 = seg;
  1309. int i;
  1310. for (i = 1, seg++; i < RPCRDMA_MAX_SEGS; seg++, i++)
  1311. rpcrdma_buffer_put_mr(&seg->rl_mw, buf);
  1312. rpcrdma_buffer_put_mr(&seg1->rl_mw, buf);
  1313. }
  1314. static void
  1315. rpcrdma_buffer_put_sendbuf(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
  1316. {
  1317. buf->rb_send_bufs[--buf->rb_send_index] = req;
  1318. req->rl_niovs = 0;
  1319. if (req->rl_reply) {
  1320. buf->rb_recv_bufs[--buf->rb_recv_index] = req->rl_reply;
  1321. req->rl_reply->rr_func = NULL;
  1322. req->rl_reply = NULL;
  1323. }
  1324. }
  1325. /* rpcrdma_unmap_one() was already done by rpcrdma_deregister_frmr_external().
  1326. * Redo only the ib_post_send().
  1327. */
  1328. static void
  1329. rpcrdma_retry_local_inv(struct rpcrdma_mw *r, struct rpcrdma_ia *ia)
  1330. {
  1331. struct rpcrdma_xprt *r_xprt =
  1332. container_of(ia, struct rpcrdma_xprt, rx_ia);
  1333. struct ib_send_wr invalidate_wr, *bad_wr;
  1334. int rc;
  1335. dprintk("RPC: %s: FRMR %p is stale\n", __func__, r);
  1336. /* When this FRMR is re-inserted into rb_mws, it is no longer stale */
  1337. r->r.frmr.fr_state = FRMR_IS_INVALID;
  1338. memset(&invalidate_wr, 0, sizeof(invalidate_wr));
  1339. invalidate_wr.wr_id = (unsigned long)(void *)r;
  1340. invalidate_wr.opcode = IB_WR_LOCAL_INV;
  1341. invalidate_wr.ex.invalidate_rkey = r->r.frmr.fr_mr->rkey;
  1342. DECR_CQCOUNT(&r_xprt->rx_ep);
  1343. dprintk("RPC: %s: frmr %p invalidating rkey %08x\n",
  1344. __func__, r, r->r.frmr.fr_mr->rkey);
  1345. read_lock(&ia->ri_qplock);
  1346. rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr);
  1347. read_unlock(&ia->ri_qplock);
  1348. if (rc) {
  1349. /* Force rpcrdma_buffer_get() to retry */
  1350. r->r.frmr.fr_state = FRMR_IS_STALE;
  1351. dprintk("RPC: %s: ib_post_send failed, %i\n",
  1352. __func__, rc);
  1353. }
  1354. }
  1355. static void
  1356. rpcrdma_retry_flushed_linv(struct list_head *stale,
  1357. struct rpcrdma_buffer *buf)
  1358. {
  1359. struct rpcrdma_ia *ia = rdmab_to_ia(buf);
  1360. struct list_head *pos;
  1361. struct rpcrdma_mw *r;
  1362. unsigned long flags;
  1363. list_for_each(pos, stale) {
  1364. r = list_entry(pos, struct rpcrdma_mw, mw_list);
  1365. rpcrdma_retry_local_inv(r, ia);
  1366. }
  1367. spin_lock_irqsave(&buf->rb_lock, flags);
  1368. list_splice_tail(stale, &buf->rb_mws);
  1369. spin_unlock_irqrestore(&buf->rb_lock, flags);
  1370. }
  1371. static struct rpcrdma_req *
  1372. rpcrdma_buffer_get_frmrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf,
  1373. struct list_head *stale)
  1374. {
  1375. struct rpcrdma_mw *r;
  1376. int i;
  1377. i = RPCRDMA_MAX_SEGS - 1;
  1378. while (!list_empty(&buf->rb_mws)) {
  1379. r = list_entry(buf->rb_mws.next,
  1380. struct rpcrdma_mw, mw_list);
  1381. list_del(&r->mw_list);
  1382. if (r->r.frmr.fr_state == FRMR_IS_STALE) {
  1383. list_add(&r->mw_list, stale);
  1384. continue;
  1385. }
  1386. req->rl_segments[i].rl_mw = r;
  1387. if (unlikely(i-- == 0))
  1388. return req; /* Success */
  1389. }
  1390. /* Not enough entries on rb_mws for this req */
  1391. rpcrdma_buffer_put_sendbuf(req, buf);
  1392. rpcrdma_buffer_put_mrs(req, buf);
  1393. return NULL;
  1394. }
  1395. static struct rpcrdma_req *
  1396. rpcrdma_buffer_get_fmrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
  1397. {
  1398. struct rpcrdma_mw *r;
  1399. int i;
  1400. i = RPCRDMA_MAX_SEGS - 1;
  1401. while (!list_empty(&buf->rb_mws)) {
  1402. r = list_entry(buf->rb_mws.next,
  1403. struct rpcrdma_mw, mw_list);
  1404. list_del(&r->mw_list);
  1405. req->rl_segments[i].rl_mw = r;
  1406. if (unlikely(i-- == 0))
  1407. return req; /* Success */
  1408. }
  1409. /* Not enough entries on rb_mws for this req */
  1410. rpcrdma_buffer_put_sendbuf(req, buf);
  1411. rpcrdma_buffer_put_mrs(req, buf);
  1412. return NULL;
  1413. }
  1414. /*
  1415. * Get a set of request/reply buffers.
  1416. *
  1417. * Reply buffer (if needed) is attached to send buffer upon return.
  1418. * Rule:
  1419. * rb_send_index and rb_recv_index MUST always be pointing to the
  1420. * *next* available buffer (non-NULL). They are incremented after
  1421. * removing buffers, and decremented *before* returning them.
  1422. */
  1423. struct rpcrdma_req *
  1424. rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
  1425. {
  1426. struct rpcrdma_ia *ia = rdmab_to_ia(buffers);
  1427. struct list_head stale;
  1428. struct rpcrdma_req *req;
  1429. unsigned long flags;
  1430. spin_lock_irqsave(&buffers->rb_lock, flags);
  1431. if (buffers->rb_send_index == buffers->rb_max_requests) {
  1432. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1433. dprintk("RPC: %s: out of request buffers\n", __func__);
  1434. return ((struct rpcrdma_req *)NULL);
  1435. }
  1436. req = buffers->rb_send_bufs[buffers->rb_send_index];
  1437. if (buffers->rb_send_index < buffers->rb_recv_index) {
  1438. dprintk("RPC: %s: %d extra receives outstanding (ok)\n",
  1439. __func__,
  1440. buffers->rb_recv_index - buffers->rb_send_index);
  1441. req->rl_reply = NULL;
  1442. } else {
  1443. req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
  1444. buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
  1445. }
  1446. buffers->rb_send_bufs[buffers->rb_send_index++] = NULL;
  1447. INIT_LIST_HEAD(&stale);
  1448. switch (ia->ri_memreg_strategy) {
  1449. case RPCRDMA_FRMR:
  1450. req = rpcrdma_buffer_get_frmrs(req, buffers, &stale);
  1451. break;
  1452. case RPCRDMA_MTHCAFMR:
  1453. req = rpcrdma_buffer_get_fmrs(req, buffers);
  1454. break;
  1455. default:
  1456. break;
  1457. }
  1458. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1459. if (!list_empty(&stale))
  1460. rpcrdma_retry_flushed_linv(&stale, buffers);
  1461. return req;
  1462. }
  1463. /*
  1464. * Put request/reply buffers back into pool.
  1465. * Pre-decrement counter/array index.
  1466. */
  1467. void
  1468. rpcrdma_buffer_put(struct rpcrdma_req *req)
  1469. {
  1470. struct rpcrdma_buffer *buffers = req->rl_buffer;
  1471. struct rpcrdma_ia *ia = rdmab_to_ia(buffers);
  1472. unsigned long flags;
  1473. spin_lock_irqsave(&buffers->rb_lock, flags);
  1474. rpcrdma_buffer_put_sendbuf(req, buffers);
  1475. switch (ia->ri_memreg_strategy) {
  1476. case RPCRDMA_FRMR:
  1477. case RPCRDMA_MTHCAFMR:
  1478. rpcrdma_buffer_put_mrs(req, buffers);
  1479. break;
  1480. default:
  1481. break;
  1482. }
  1483. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1484. }
  1485. /*
  1486. * Recover reply buffers from pool.
  1487. * This happens when recovering from error conditions.
  1488. * Post-increment counter/array index.
  1489. */
  1490. void
  1491. rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
  1492. {
  1493. struct rpcrdma_buffer *buffers = req->rl_buffer;
  1494. unsigned long flags;
  1495. spin_lock_irqsave(&buffers->rb_lock, flags);
  1496. if (buffers->rb_recv_index < buffers->rb_max_requests) {
  1497. req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
  1498. buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
  1499. }
  1500. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1501. }
  1502. /*
  1503. * Put reply buffers back into pool when not attached to
  1504. * request. This happens in error conditions.
  1505. */
  1506. void
  1507. rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
  1508. {
  1509. struct rpcrdma_buffer *buffers = rep->rr_buffer;
  1510. unsigned long flags;
  1511. rep->rr_func = NULL;
  1512. spin_lock_irqsave(&buffers->rb_lock, flags);
  1513. buffers->rb_recv_bufs[--buffers->rb_recv_index] = rep;
  1514. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1515. }
  1516. /*
  1517. * Wrappers for internal-use kmalloc memory registration, used by buffer code.
  1518. */
  1519. static int
  1520. rpcrdma_register_internal(struct rpcrdma_ia *ia, void *va, int len,
  1521. struct ib_mr **mrp, struct ib_sge *iov)
  1522. {
  1523. struct ib_phys_buf ipb;
  1524. struct ib_mr *mr;
  1525. int rc;
  1526. /*
  1527. * All memory passed here was kmalloc'ed, therefore phys-contiguous.
  1528. */
  1529. iov->addr = ib_dma_map_single(ia->ri_id->device,
  1530. va, len, DMA_BIDIRECTIONAL);
  1531. if (ib_dma_mapping_error(ia->ri_id->device, iov->addr))
  1532. return -ENOMEM;
  1533. iov->length = len;
  1534. if (ia->ri_have_dma_lkey) {
  1535. *mrp = NULL;
  1536. iov->lkey = ia->ri_dma_lkey;
  1537. return 0;
  1538. } else if (ia->ri_bind_mem != NULL) {
  1539. *mrp = NULL;
  1540. iov->lkey = ia->ri_bind_mem->lkey;
  1541. return 0;
  1542. }
  1543. ipb.addr = iov->addr;
  1544. ipb.size = iov->length;
  1545. mr = ib_reg_phys_mr(ia->ri_pd, &ipb, 1,
  1546. IB_ACCESS_LOCAL_WRITE, &iov->addr);
  1547. dprintk("RPC: %s: phys convert: 0x%llx "
  1548. "registered 0x%llx length %d\n",
  1549. __func__, (unsigned long long)ipb.addr,
  1550. (unsigned long long)iov->addr, len);
  1551. if (IS_ERR(mr)) {
  1552. *mrp = NULL;
  1553. rc = PTR_ERR(mr);
  1554. dprintk("RPC: %s: failed with %i\n", __func__, rc);
  1555. } else {
  1556. *mrp = mr;
  1557. iov->lkey = mr->lkey;
  1558. rc = 0;
  1559. }
  1560. return rc;
  1561. }
  1562. static int
  1563. rpcrdma_deregister_internal(struct rpcrdma_ia *ia,
  1564. struct ib_mr *mr, struct ib_sge *iov)
  1565. {
  1566. int rc;
  1567. ib_dma_unmap_single(ia->ri_id->device,
  1568. iov->addr, iov->length, DMA_BIDIRECTIONAL);
  1569. if (NULL == mr)
  1570. return 0;
  1571. rc = ib_dereg_mr(mr);
  1572. if (rc)
  1573. dprintk("RPC: %s: ib_dereg_mr failed %i\n", __func__, rc);
  1574. return rc;
  1575. }
  1576. /**
  1577. * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
  1578. * @ia: controlling rpcrdma_ia
  1579. * @size: size of buffer to be allocated, in bytes
  1580. * @flags: GFP flags
  1581. *
  1582. * Returns pointer to private header of an area of internally
  1583. * registered memory, or an ERR_PTR. The registered buffer follows
  1584. * the end of the private header.
  1585. *
  1586. * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
  1587. * receiving the payload of RDMA RECV operations. regbufs are not
  1588. * used for RDMA READ/WRITE operations, thus are registered only for
  1589. * LOCAL access.
  1590. */
  1591. struct rpcrdma_regbuf *
  1592. rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
  1593. {
  1594. struct rpcrdma_regbuf *rb;
  1595. int rc;
  1596. rc = -ENOMEM;
  1597. rb = kmalloc(sizeof(*rb) + size, flags);
  1598. if (rb == NULL)
  1599. goto out;
  1600. rb->rg_size = size;
  1601. rb->rg_owner = NULL;
  1602. rc = rpcrdma_register_internal(ia, rb->rg_base, size,
  1603. &rb->rg_mr, &rb->rg_iov);
  1604. if (rc)
  1605. goto out_free;
  1606. return rb;
  1607. out_free:
  1608. kfree(rb);
  1609. out:
  1610. return ERR_PTR(rc);
  1611. }
  1612. /**
  1613. * rpcrdma_free_regbuf - deregister and free registered buffer
  1614. * @ia: controlling rpcrdma_ia
  1615. * @rb: regbuf to be deregistered and freed
  1616. */
  1617. void
  1618. rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
  1619. {
  1620. if (rb) {
  1621. rpcrdma_deregister_internal(ia, rb->rg_mr, &rb->rg_iov);
  1622. kfree(rb);
  1623. }
  1624. }
  1625. /*
  1626. * Wrappers for chunk registration, shared by read/write chunk code.
  1627. */
  1628. static void
  1629. rpcrdma_map_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg, int writing)
  1630. {
  1631. seg->mr_dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1632. seg->mr_dmalen = seg->mr_len;
  1633. if (seg->mr_page)
  1634. seg->mr_dma = ib_dma_map_page(ia->ri_id->device,
  1635. seg->mr_page, offset_in_page(seg->mr_offset),
  1636. seg->mr_dmalen, seg->mr_dir);
  1637. else
  1638. seg->mr_dma = ib_dma_map_single(ia->ri_id->device,
  1639. seg->mr_offset,
  1640. seg->mr_dmalen, seg->mr_dir);
  1641. if (ib_dma_mapping_error(ia->ri_id->device, seg->mr_dma)) {
  1642. dprintk("RPC: %s: mr_dma %llx mr_offset %p mr_dma_len %zu\n",
  1643. __func__,
  1644. (unsigned long long)seg->mr_dma,
  1645. seg->mr_offset, seg->mr_dmalen);
  1646. }
  1647. }
  1648. static void
  1649. rpcrdma_unmap_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg)
  1650. {
  1651. if (seg->mr_page)
  1652. ib_dma_unmap_page(ia->ri_id->device,
  1653. seg->mr_dma, seg->mr_dmalen, seg->mr_dir);
  1654. else
  1655. ib_dma_unmap_single(ia->ri_id->device,
  1656. seg->mr_dma, seg->mr_dmalen, seg->mr_dir);
  1657. }
  1658. static int
  1659. rpcrdma_register_frmr_external(struct rpcrdma_mr_seg *seg,
  1660. int *nsegs, int writing, struct rpcrdma_ia *ia,
  1661. struct rpcrdma_xprt *r_xprt)
  1662. {
  1663. struct rpcrdma_mr_seg *seg1 = seg;
  1664. struct rpcrdma_mw *mw = seg1->rl_mw;
  1665. struct rpcrdma_frmr *frmr = &mw->r.frmr;
  1666. struct ib_mr *mr = frmr->fr_mr;
  1667. struct ib_send_wr fastreg_wr, *bad_wr;
  1668. u8 key;
  1669. int len, pageoff;
  1670. int i, rc;
  1671. int seg_len;
  1672. u64 pa;
  1673. int page_no;
  1674. pageoff = offset_in_page(seg1->mr_offset);
  1675. seg1->mr_offset -= pageoff; /* start of page */
  1676. seg1->mr_len += pageoff;
  1677. len = -pageoff;
  1678. if (*nsegs > ia->ri_max_frmr_depth)
  1679. *nsegs = ia->ri_max_frmr_depth;
  1680. for (page_no = i = 0; i < *nsegs;) {
  1681. rpcrdma_map_one(ia, seg, writing);
  1682. pa = seg->mr_dma;
  1683. for (seg_len = seg->mr_len; seg_len > 0; seg_len -= PAGE_SIZE) {
  1684. frmr->fr_pgl->page_list[page_no++] = pa;
  1685. pa += PAGE_SIZE;
  1686. }
  1687. len += seg->mr_len;
  1688. ++seg;
  1689. ++i;
  1690. /* Check for holes */
  1691. if ((i < *nsegs && offset_in_page(seg->mr_offset)) ||
  1692. offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
  1693. break;
  1694. }
  1695. dprintk("RPC: %s: Using frmr %p to map %d segments\n",
  1696. __func__, mw, i);
  1697. frmr->fr_state = FRMR_IS_VALID;
  1698. memset(&fastreg_wr, 0, sizeof(fastreg_wr));
  1699. fastreg_wr.wr_id = (unsigned long)(void *)mw;
  1700. fastreg_wr.opcode = IB_WR_FAST_REG_MR;
  1701. fastreg_wr.wr.fast_reg.iova_start = seg1->mr_dma;
  1702. fastreg_wr.wr.fast_reg.page_list = frmr->fr_pgl;
  1703. fastreg_wr.wr.fast_reg.page_list_len = page_no;
  1704. fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
  1705. fastreg_wr.wr.fast_reg.length = page_no << PAGE_SHIFT;
  1706. if (fastreg_wr.wr.fast_reg.length < len) {
  1707. rc = -EIO;
  1708. goto out_err;
  1709. }
  1710. /* Bump the key */
  1711. key = (u8)(mr->rkey & 0x000000FF);
  1712. ib_update_fast_reg_key(mr, ++key);
  1713. fastreg_wr.wr.fast_reg.access_flags = (writing ?
  1714. IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
  1715. IB_ACCESS_REMOTE_READ);
  1716. fastreg_wr.wr.fast_reg.rkey = mr->rkey;
  1717. DECR_CQCOUNT(&r_xprt->rx_ep);
  1718. rc = ib_post_send(ia->ri_id->qp, &fastreg_wr, &bad_wr);
  1719. if (rc) {
  1720. dprintk("RPC: %s: failed ib_post_send for register,"
  1721. " status %i\n", __func__, rc);
  1722. ib_update_fast_reg_key(mr, --key);
  1723. goto out_err;
  1724. } else {
  1725. seg1->mr_rkey = mr->rkey;
  1726. seg1->mr_base = seg1->mr_dma + pageoff;
  1727. seg1->mr_nsegs = i;
  1728. seg1->mr_len = len;
  1729. }
  1730. *nsegs = i;
  1731. return 0;
  1732. out_err:
  1733. frmr->fr_state = FRMR_IS_INVALID;
  1734. while (i--)
  1735. rpcrdma_unmap_one(ia, --seg);
  1736. return rc;
  1737. }
  1738. static int
  1739. rpcrdma_deregister_frmr_external(struct rpcrdma_mr_seg *seg,
  1740. struct rpcrdma_ia *ia, struct rpcrdma_xprt *r_xprt)
  1741. {
  1742. struct rpcrdma_mr_seg *seg1 = seg;
  1743. struct ib_send_wr invalidate_wr, *bad_wr;
  1744. int rc;
  1745. seg1->rl_mw->r.frmr.fr_state = FRMR_IS_INVALID;
  1746. memset(&invalidate_wr, 0, sizeof invalidate_wr);
  1747. invalidate_wr.wr_id = (unsigned long)(void *)seg1->rl_mw;
  1748. invalidate_wr.opcode = IB_WR_LOCAL_INV;
  1749. invalidate_wr.ex.invalidate_rkey = seg1->rl_mw->r.frmr.fr_mr->rkey;
  1750. DECR_CQCOUNT(&r_xprt->rx_ep);
  1751. read_lock(&ia->ri_qplock);
  1752. while (seg1->mr_nsegs--)
  1753. rpcrdma_unmap_one(ia, seg++);
  1754. rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr);
  1755. read_unlock(&ia->ri_qplock);
  1756. if (rc) {
  1757. /* Force rpcrdma_buffer_get() to retry */
  1758. seg1->rl_mw->r.frmr.fr_state = FRMR_IS_STALE;
  1759. dprintk("RPC: %s: failed ib_post_send for invalidate,"
  1760. " status %i\n", __func__, rc);
  1761. }
  1762. return rc;
  1763. }
  1764. static int
  1765. rpcrdma_register_fmr_external(struct rpcrdma_mr_seg *seg,
  1766. int *nsegs, int writing, struct rpcrdma_ia *ia)
  1767. {
  1768. struct rpcrdma_mr_seg *seg1 = seg;
  1769. u64 physaddrs[RPCRDMA_MAX_DATA_SEGS];
  1770. int len, pageoff, i, rc;
  1771. pageoff = offset_in_page(seg1->mr_offset);
  1772. seg1->mr_offset -= pageoff; /* start of page */
  1773. seg1->mr_len += pageoff;
  1774. len = -pageoff;
  1775. if (*nsegs > RPCRDMA_MAX_DATA_SEGS)
  1776. *nsegs = RPCRDMA_MAX_DATA_SEGS;
  1777. for (i = 0; i < *nsegs;) {
  1778. rpcrdma_map_one(ia, seg, writing);
  1779. physaddrs[i] = seg->mr_dma;
  1780. len += seg->mr_len;
  1781. ++seg;
  1782. ++i;
  1783. /* Check for holes */
  1784. if ((i < *nsegs && offset_in_page(seg->mr_offset)) ||
  1785. offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
  1786. break;
  1787. }
  1788. rc = ib_map_phys_fmr(seg1->rl_mw->r.fmr, physaddrs, i, seg1->mr_dma);
  1789. if (rc) {
  1790. dprintk("RPC: %s: failed ib_map_phys_fmr "
  1791. "%u@0x%llx+%i (%d)... status %i\n", __func__,
  1792. len, (unsigned long long)seg1->mr_dma,
  1793. pageoff, i, rc);
  1794. while (i--)
  1795. rpcrdma_unmap_one(ia, --seg);
  1796. } else {
  1797. seg1->mr_rkey = seg1->rl_mw->r.fmr->rkey;
  1798. seg1->mr_base = seg1->mr_dma + pageoff;
  1799. seg1->mr_nsegs = i;
  1800. seg1->mr_len = len;
  1801. }
  1802. *nsegs = i;
  1803. return rc;
  1804. }
  1805. static int
  1806. rpcrdma_deregister_fmr_external(struct rpcrdma_mr_seg *seg,
  1807. struct rpcrdma_ia *ia)
  1808. {
  1809. struct rpcrdma_mr_seg *seg1 = seg;
  1810. LIST_HEAD(l);
  1811. int rc;
  1812. list_add(&seg1->rl_mw->r.fmr->list, &l);
  1813. rc = ib_unmap_fmr(&l);
  1814. read_lock(&ia->ri_qplock);
  1815. while (seg1->mr_nsegs--)
  1816. rpcrdma_unmap_one(ia, seg++);
  1817. read_unlock(&ia->ri_qplock);
  1818. if (rc)
  1819. dprintk("RPC: %s: failed ib_unmap_fmr,"
  1820. " status %i\n", __func__, rc);
  1821. return rc;
  1822. }
  1823. int
  1824. rpcrdma_register_external(struct rpcrdma_mr_seg *seg,
  1825. int nsegs, int writing, struct rpcrdma_xprt *r_xprt)
  1826. {
  1827. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  1828. int rc = 0;
  1829. switch (ia->ri_memreg_strategy) {
  1830. case RPCRDMA_ALLPHYSICAL:
  1831. rpcrdma_map_one(ia, seg, writing);
  1832. seg->mr_rkey = ia->ri_bind_mem->rkey;
  1833. seg->mr_base = seg->mr_dma;
  1834. seg->mr_nsegs = 1;
  1835. nsegs = 1;
  1836. break;
  1837. /* Registration using frmr registration */
  1838. case RPCRDMA_FRMR:
  1839. rc = rpcrdma_register_frmr_external(seg, &nsegs, writing, ia, r_xprt);
  1840. break;
  1841. /* Registration using fmr memory registration */
  1842. case RPCRDMA_MTHCAFMR:
  1843. rc = rpcrdma_register_fmr_external(seg, &nsegs, writing, ia);
  1844. break;
  1845. default:
  1846. return -EIO;
  1847. }
  1848. if (rc)
  1849. return rc;
  1850. return nsegs;
  1851. }
  1852. int
  1853. rpcrdma_deregister_external(struct rpcrdma_mr_seg *seg,
  1854. struct rpcrdma_xprt *r_xprt)
  1855. {
  1856. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  1857. int nsegs = seg->mr_nsegs, rc;
  1858. switch (ia->ri_memreg_strategy) {
  1859. case RPCRDMA_ALLPHYSICAL:
  1860. read_lock(&ia->ri_qplock);
  1861. rpcrdma_unmap_one(ia, seg);
  1862. read_unlock(&ia->ri_qplock);
  1863. break;
  1864. case RPCRDMA_FRMR:
  1865. rc = rpcrdma_deregister_frmr_external(seg, ia, r_xprt);
  1866. break;
  1867. case RPCRDMA_MTHCAFMR:
  1868. rc = rpcrdma_deregister_fmr_external(seg, ia);
  1869. break;
  1870. default:
  1871. break;
  1872. }
  1873. return nsegs;
  1874. }
  1875. /*
  1876. * Prepost any receive buffer, then post send.
  1877. *
  1878. * Receive buffer is donated to hardware, reclaimed upon recv completion.
  1879. */
  1880. int
  1881. rpcrdma_ep_post(struct rpcrdma_ia *ia,
  1882. struct rpcrdma_ep *ep,
  1883. struct rpcrdma_req *req)
  1884. {
  1885. struct ib_send_wr send_wr, *send_wr_fail;
  1886. struct rpcrdma_rep *rep = req->rl_reply;
  1887. int rc;
  1888. if (rep) {
  1889. rc = rpcrdma_ep_post_recv(ia, ep, rep);
  1890. if (rc)
  1891. goto out;
  1892. req->rl_reply = NULL;
  1893. }
  1894. send_wr.next = NULL;
  1895. send_wr.wr_id = 0ULL; /* no send cookie */
  1896. send_wr.sg_list = req->rl_send_iov;
  1897. send_wr.num_sge = req->rl_niovs;
  1898. send_wr.opcode = IB_WR_SEND;
  1899. if (send_wr.num_sge == 4) /* no need to sync any pad (constant) */
  1900. ib_dma_sync_single_for_device(ia->ri_id->device,
  1901. req->rl_send_iov[3].addr, req->rl_send_iov[3].length,
  1902. DMA_TO_DEVICE);
  1903. ib_dma_sync_single_for_device(ia->ri_id->device,
  1904. req->rl_send_iov[1].addr, req->rl_send_iov[1].length,
  1905. DMA_TO_DEVICE);
  1906. ib_dma_sync_single_for_device(ia->ri_id->device,
  1907. req->rl_send_iov[0].addr, req->rl_send_iov[0].length,
  1908. DMA_TO_DEVICE);
  1909. if (DECR_CQCOUNT(ep) > 0)
  1910. send_wr.send_flags = 0;
  1911. else { /* Provider must take a send completion every now and then */
  1912. INIT_CQCOUNT(ep);
  1913. send_wr.send_flags = IB_SEND_SIGNALED;
  1914. }
  1915. rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
  1916. if (rc)
  1917. dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
  1918. rc);
  1919. out:
  1920. return rc;
  1921. }
  1922. /*
  1923. * (Re)post a receive buffer.
  1924. */
  1925. int
  1926. rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
  1927. struct rpcrdma_ep *ep,
  1928. struct rpcrdma_rep *rep)
  1929. {
  1930. struct ib_recv_wr recv_wr, *recv_wr_fail;
  1931. int rc;
  1932. recv_wr.next = NULL;
  1933. recv_wr.wr_id = (u64) (unsigned long) rep;
  1934. recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
  1935. recv_wr.num_sge = 1;
  1936. ib_dma_sync_single_for_cpu(ia->ri_id->device,
  1937. rdmab_addr(rep->rr_rdmabuf),
  1938. rdmab_length(rep->rr_rdmabuf),
  1939. DMA_BIDIRECTIONAL);
  1940. rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
  1941. if (rc)
  1942. dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
  1943. rc);
  1944. return rc;
  1945. }
  1946. /* Physical mapping means one Read/Write list entry per-page.
  1947. * All list entries must fit within an inline buffer
  1948. *
  1949. * NB: The server must return a Write list for NFS READ,
  1950. * which has the same constraint. Factor in the inline
  1951. * rsize as well.
  1952. */
  1953. static size_t
  1954. rpcrdma_physical_max_payload(struct rpcrdma_xprt *r_xprt)
  1955. {
  1956. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  1957. unsigned int inline_size, pages;
  1958. inline_size = min_t(unsigned int,
  1959. cdata->inline_wsize, cdata->inline_rsize);
  1960. inline_size -= RPCRDMA_HDRLEN_MIN;
  1961. pages = inline_size / sizeof(struct rpcrdma_segment);
  1962. return pages << PAGE_SHIFT;
  1963. }
  1964. static size_t
  1965. rpcrdma_mr_max_payload(struct rpcrdma_xprt *r_xprt)
  1966. {
  1967. return RPCRDMA_MAX_DATA_SEGS << PAGE_SHIFT;
  1968. }
  1969. size_t
  1970. rpcrdma_max_payload(struct rpcrdma_xprt *r_xprt)
  1971. {
  1972. size_t result;
  1973. switch (r_xprt->rx_ia.ri_memreg_strategy) {
  1974. case RPCRDMA_ALLPHYSICAL:
  1975. result = rpcrdma_physical_max_payload(r_xprt);
  1976. break;
  1977. default:
  1978. result = rpcrdma_mr_max_payload(r_xprt);
  1979. }
  1980. return result;
  1981. }