verbs.c 52 KB

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