verbs.c 54 KB

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