verbs.c 51 KB

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