verbs.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. /*
  2. * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the BSD-type
  8. * license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials provided
  20. * with the distribution.
  21. *
  22. * Neither the name of the Network Appliance, Inc. nor the names of
  23. * its contributors may be used to endorse or promote products
  24. * derived from this software without specific prior written
  25. * permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. /*
  40. * verbs.c
  41. *
  42. * Encapsulates the major functions managing:
  43. * o adapters
  44. * o endpoints
  45. * o connections
  46. * o buffer memory
  47. */
  48. #include <linux/interrupt.h>
  49. #include <linux/slab.h>
  50. #include <linux/prefetch.h>
  51. #include <linux/sunrpc/addr.h>
  52. #include <asm/bitops.h>
  53. #include "xprt_rdma.h"
  54. /*
  55. * Globals/Macros
  56. */
  57. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  58. # define RPCDBG_FACILITY RPCDBG_TRANS
  59. #endif
  60. /*
  61. * internal functions
  62. */
  63. /*
  64. * handle replies in tasklet context, using a single, global list
  65. * rdma tasklet function -- just turn around and call the func
  66. * for all replies on the list
  67. */
  68. static DEFINE_SPINLOCK(rpcrdma_tk_lock_g);
  69. static LIST_HEAD(rpcrdma_tasklets_g);
  70. static void
  71. rpcrdma_run_tasklet(unsigned long data)
  72. {
  73. struct rpcrdma_rep *rep;
  74. void (*func)(struct rpcrdma_rep *);
  75. unsigned long flags;
  76. data = data;
  77. spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
  78. while (!list_empty(&rpcrdma_tasklets_g)) {
  79. rep = list_entry(rpcrdma_tasklets_g.next,
  80. struct rpcrdma_rep, rr_list);
  81. list_del(&rep->rr_list);
  82. func = rep->rr_func;
  83. rep->rr_func = NULL;
  84. spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
  85. if (func)
  86. func(rep);
  87. else
  88. rpcrdma_recv_buffer_put(rep);
  89. spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
  90. }
  91. spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
  92. }
  93. static DECLARE_TASKLET(rpcrdma_tasklet_g, rpcrdma_run_tasklet, 0UL);
  94. static const char * const async_event[] = {
  95. "CQ error",
  96. "QP fatal error",
  97. "QP request error",
  98. "QP access error",
  99. "communication established",
  100. "send queue drained",
  101. "path migration successful",
  102. "path mig error",
  103. "device fatal error",
  104. "port active",
  105. "port error",
  106. "LID change",
  107. "P_key change",
  108. "SM change",
  109. "SRQ error",
  110. "SRQ limit reached",
  111. "last WQE reached",
  112. "client reregister",
  113. "GID change",
  114. };
  115. #define ASYNC_MSG(status) \
  116. ((status) < ARRAY_SIZE(async_event) ? \
  117. async_event[(status)] : "unknown async error")
  118. static void
  119. rpcrdma_schedule_tasklet(struct list_head *sched_list)
  120. {
  121. unsigned long flags;
  122. spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
  123. list_splice_tail(sched_list, &rpcrdma_tasklets_g);
  124. spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
  125. tasklet_schedule(&rpcrdma_tasklet_g);
  126. }
  127. static void
  128. rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
  129. {
  130. struct rpcrdma_ep *ep = context;
  131. pr_err("RPC: %s: %s on device %s ep %p\n",
  132. __func__, ASYNC_MSG(event->event),
  133. event->device->name, context);
  134. if (ep->rep_connected == 1) {
  135. ep->rep_connected = -EIO;
  136. rpcrdma_conn_func(ep);
  137. wake_up_all(&ep->rep_connect_wait);
  138. }
  139. }
  140. static void
  141. rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context)
  142. {
  143. struct rpcrdma_ep *ep = context;
  144. pr_err("RPC: %s: %s on device %s ep %p\n",
  145. __func__, ASYNC_MSG(event->event),
  146. event->device->name, context);
  147. if (ep->rep_connected == 1) {
  148. ep->rep_connected = -EIO;
  149. rpcrdma_conn_func(ep);
  150. wake_up_all(&ep->rep_connect_wait);
  151. }
  152. }
  153. static const char * const wc_status[] = {
  154. "success",
  155. "local length error",
  156. "local QP operation error",
  157. "local EE context operation error",
  158. "local protection error",
  159. "WR flushed",
  160. "memory management operation error",
  161. "bad response error",
  162. "local access error",
  163. "remote invalid request error",
  164. "remote access error",
  165. "remote operation error",
  166. "transport retry counter exceeded",
  167. "RNR retry counter exceeded",
  168. "local RDD violation error",
  169. "remove invalid RD request",
  170. "operation aborted",
  171. "invalid EE context number",
  172. "invalid EE context state",
  173. "fatal error",
  174. "response timeout error",
  175. "general error",
  176. };
  177. #define COMPLETION_MSG(status) \
  178. ((status) < ARRAY_SIZE(wc_status) ? \
  179. wc_status[(status)] : "unexpected completion error")
  180. static void
  181. rpcrdma_sendcq_process_wc(struct ib_wc *wc)
  182. {
  183. /* WARNING: Only wr_id and status are reliable at this point */
  184. if (wc->wr_id == RPCRDMA_IGNORE_COMPLETION) {
  185. if (wc->status != IB_WC_SUCCESS &&
  186. wc->status != IB_WC_WR_FLUSH_ERR)
  187. pr_err("RPC: %s: SEND: %s\n",
  188. __func__, COMPLETION_MSG(wc->status));
  189. } else {
  190. struct rpcrdma_mw *r;
  191. r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
  192. r->mw_sendcompletion(wc);
  193. }
  194. }
  195. static int
  196. rpcrdma_sendcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
  197. {
  198. struct ib_wc *wcs;
  199. int budget, count, rc;
  200. budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
  201. do {
  202. wcs = ep->rep_send_wcs;
  203. rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
  204. if (rc <= 0)
  205. return rc;
  206. count = rc;
  207. while (count-- > 0)
  208. rpcrdma_sendcq_process_wc(wcs++);
  209. } while (rc == RPCRDMA_POLLSIZE && --budget);
  210. return 0;
  211. }
  212. /*
  213. * Handle send, fast_reg_mr, and local_inv completions.
  214. *
  215. * Send events are typically suppressed and thus do not result
  216. * in an upcall. Occasionally one is signaled, however. This
  217. * prevents the provider's completion queue from wrapping and
  218. * losing a completion.
  219. */
  220. static void
  221. rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context)
  222. {
  223. struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
  224. int rc;
  225. rc = rpcrdma_sendcq_poll(cq, ep);
  226. if (rc) {
  227. dprintk("RPC: %s: ib_poll_cq failed: %i\n",
  228. __func__, rc);
  229. return;
  230. }
  231. rc = ib_req_notify_cq(cq,
  232. IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
  233. if (rc == 0)
  234. return;
  235. if (rc < 0) {
  236. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  237. __func__, rc);
  238. return;
  239. }
  240. rpcrdma_sendcq_poll(cq, ep);
  241. }
  242. static void
  243. rpcrdma_recvcq_process_wc(struct ib_wc *wc, struct list_head *sched_list)
  244. {
  245. struct rpcrdma_rep *rep =
  246. (struct rpcrdma_rep *)(unsigned long)wc->wr_id;
  247. /* WARNING: Only wr_id and status are reliable at this point */
  248. if (wc->status != IB_WC_SUCCESS)
  249. goto out_fail;
  250. /* status == SUCCESS means all fields in wc are trustworthy */
  251. if (wc->opcode != IB_WC_RECV)
  252. return;
  253. dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
  254. __func__, rep, wc->byte_len);
  255. rep->rr_len = wc->byte_len;
  256. ib_dma_sync_single_for_cpu(rdmab_to_ia(rep->rr_buffer)->ri_id->device,
  257. rdmab_addr(rep->rr_rdmabuf),
  258. rep->rr_len, DMA_FROM_DEVICE);
  259. prefetch(rdmab_to_msg(rep->rr_rdmabuf));
  260. out_schedule:
  261. list_add_tail(&rep->rr_list, sched_list);
  262. return;
  263. out_fail:
  264. if (wc->status != IB_WC_WR_FLUSH_ERR)
  265. pr_err("RPC: %s: rep %p: %s\n",
  266. __func__, rep, COMPLETION_MSG(wc->status));
  267. rep->rr_len = ~0U;
  268. goto out_schedule;
  269. }
  270. static int
  271. rpcrdma_recvcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
  272. {
  273. struct list_head sched_list;
  274. struct ib_wc *wcs;
  275. int budget, count, rc;
  276. INIT_LIST_HEAD(&sched_list);
  277. budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
  278. do {
  279. wcs = ep->rep_recv_wcs;
  280. rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
  281. if (rc <= 0)
  282. goto out_schedule;
  283. count = rc;
  284. while (count-- > 0)
  285. rpcrdma_recvcq_process_wc(wcs++, &sched_list);
  286. } while (rc == RPCRDMA_POLLSIZE && --budget);
  287. rc = 0;
  288. out_schedule:
  289. rpcrdma_schedule_tasklet(&sched_list);
  290. return rc;
  291. }
  292. /*
  293. * Handle receive completions.
  294. *
  295. * It is reentrant but processes single events in order to maintain
  296. * ordering of receives to keep server credits.
  297. *
  298. * It is the responsibility of the scheduled tasklet to return
  299. * recv buffers to the pool. NOTE: this affects synchronization of
  300. * connection shutdown. That is, the structures required for
  301. * the completion of the reply handler must remain intact until
  302. * all memory has been reclaimed.
  303. */
  304. static void
  305. rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
  306. {
  307. struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
  308. int rc;
  309. rc = rpcrdma_recvcq_poll(cq, ep);
  310. if (rc) {
  311. dprintk("RPC: %s: ib_poll_cq failed: %i\n",
  312. __func__, rc);
  313. return;
  314. }
  315. rc = ib_req_notify_cq(cq,
  316. IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
  317. if (rc == 0)
  318. return;
  319. if (rc < 0) {
  320. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  321. __func__, rc);
  322. return;
  323. }
  324. rpcrdma_recvcq_poll(cq, ep);
  325. }
  326. static void
  327. rpcrdma_flush_cqs(struct rpcrdma_ep *ep)
  328. {
  329. struct ib_wc wc;
  330. LIST_HEAD(sched_list);
  331. while (ib_poll_cq(ep->rep_attr.recv_cq, 1, &wc) > 0)
  332. rpcrdma_recvcq_process_wc(&wc, &sched_list);
  333. if (!list_empty(&sched_list))
  334. rpcrdma_schedule_tasklet(&sched_list);
  335. while (ib_poll_cq(ep->rep_attr.send_cq, 1, &wc) > 0)
  336. rpcrdma_sendcq_process_wc(&wc);
  337. }
  338. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  339. static const char * const conn[] = {
  340. "address resolved",
  341. "address error",
  342. "route resolved",
  343. "route error",
  344. "connect request",
  345. "connect response",
  346. "connect error",
  347. "unreachable",
  348. "rejected",
  349. "established",
  350. "disconnected",
  351. "device removal",
  352. "multicast join",
  353. "multicast error",
  354. "address change",
  355. "timewait exit",
  356. };
  357. #define CONNECTION_MSG(status) \
  358. ((status) < ARRAY_SIZE(conn) ? \
  359. conn[(status)] : "unrecognized connection error")
  360. #endif
  361. static int
  362. rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
  363. {
  364. struct rpcrdma_xprt *xprt = id->context;
  365. struct rpcrdma_ia *ia = &xprt->rx_ia;
  366. struct rpcrdma_ep *ep = &xprt->rx_ep;
  367. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  368. struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
  369. #endif
  370. struct ib_qp_attr *attr = &ia->ri_qp_attr;
  371. struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
  372. int connstate = 0;
  373. switch (event->event) {
  374. case RDMA_CM_EVENT_ADDR_RESOLVED:
  375. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  376. ia->ri_async_rc = 0;
  377. complete(&ia->ri_done);
  378. break;
  379. case RDMA_CM_EVENT_ADDR_ERROR:
  380. ia->ri_async_rc = -EHOSTUNREACH;
  381. dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
  382. __func__, ep);
  383. complete(&ia->ri_done);
  384. break;
  385. case RDMA_CM_EVENT_ROUTE_ERROR:
  386. ia->ri_async_rc = -ENETUNREACH;
  387. dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
  388. __func__, ep);
  389. complete(&ia->ri_done);
  390. break;
  391. case RDMA_CM_EVENT_ESTABLISHED:
  392. connstate = 1;
  393. ib_query_qp(ia->ri_id->qp, attr,
  394. IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
  395. iattr);
  396. dprintk("RPC: %s: %d responder resources"
  397. " (%d initiator)\n",
  398. __func__, attr->max_dest_rd_atomic,
  399. attr->max_rd_atomic);
  400. goto connected;
  401. case RDMA_CM_EVENT_CONNECT_ERROR:
  402. connstate = -ENOTCONN;
  403. goto connected;
  404. case RDMA_CM_EVENT_UNREACHABLE:
  405. connstate = -ENETDOWN;
  406. goto connected;
  407. case RDMA_CM_EVENT_REJECTED:
  408. connstate = -ECONNREFUSED;
  409. goto connected;
  410. case RDMA_CM_EVENT_DISCONNECTED:
  411. connstate = -ECONNABORTED;
  412. goto connected;
  413. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  414. connstate = -ENODEV;
  415. connected:
  416. dprintk("RPC: %s: %sconnected\n",
  417. __func__, connstate > 0 ? "" : "dis");
  418. ep->rep_connected = connstate;
  419. rpcrdma_conn_func(ep);
  420. wake_up_all(&ep->rep_connect_wait);
  421. /*FALLTHROUGH*/
  422. default:
  423. dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
  424. __func__, sap, rpc_get_port(sap), ep,
  425. CONNECTION_MSG(event->event));
  426. break;
  427. }
  428. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  429. if (connstate == 1) {
  430. int ird = attr->max_dest_rd_atomic;
  431. int tird = ep->rep_remote_cma.responder_resources;
  432. pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
  433. sap, rpc_get_port(sap),
  434. ia->ri_id->device->name,
  435. ia->ri_ops->ro_displayname,
  436. xprt->rx_buf.rb_max_requests,
  437. ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
  438. } else if (connstate < 0) {
  439. pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
  440. sap, rpc_get_port(sap), connstate);
  441. }
  442. #endif
  443. return 0;
  444. }
  445. static struct rdma_cm_id *
  446. rpcrdma_create_id(struct rpcrdma_xprt *xprt,
  447. struct rpcrdma_ia *ia, struct sockaddr *addr)
  448. {
  449. struct rdma_cm_id *id;
  450. int rc;
  451. init_completion(&ia->ri_done);
  452. id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC);
  453. if (IS_ERR(id)) {
  454. rc = PTR_ERR(id);
  455. dprintk("RPC: %s: rdma_create_id() failed %i\n",
  456. __func__, rc);
  457. return id;
  458. }
  459. ia->ri_async_rc = -ETIMEDOUT;
  460. rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
  461. if (rc) {
  462. dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
  463. __func__, rc);
  464. goto out;
  465. }
  466. wait_for_completion_interruptible_timeout(&ia->ri_done,
  467. msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
  468. rc = ia->ri_async_rc;
  469. if (rc)
  470. goto out;
  471. ia->ri_async_rc = -ETIMEDOUT;
  472. rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
  473. if (rc) {
  474. dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
  475. __func__, rc);
  476. goto out;
  477. }
  478. wait_for_completion_interruptible_timeout(&ia->ri_done,
  479. msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
  480. rc = ia->ri_async_rc;
  481. if (rc)
  482. goto out;
  483. return id;
  484. out:
  485. rdma_destroy_id(id);
  486. return ERR_PTR(rc);
  487. }
  488. /*
  489. * Drain any cq, prior to teardown.
  490. */
  491. static void
  492. rpcrdma_clean_cq(struct ib_cq *cq)
  493. {
  494. struct ib_wc wc;
  495. int count = 0;
  496. while (1 == ib_poll_cq(cq, 1, &wc))
  497. ++count;
  498. if (count)
  499. dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
  500. __func__, count, wc.opcode);
  501. }
  502. /*
  503. * Exported functions.
  504. */
  505. /*
  506. * Open and initialize an Interface Adapter.
  507. * o initializes fields of struct rpcrdma_ia, including
  508. * interface and provider attributes and protection zone.
  509. */
  510. int
  511. rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
  512. {
  513. int rc, mem_priv;
  514. struct rpcrdma_ia *ia = &xprt->rx_ia;
  515. struct ib_device_attr *devattr = &ia->ri_devattr;
  516. ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
  517. if (IS_ERR(ia->ri_id)) {
  518. rc = PTR_ERR(ia->ri_id);
  519. goto out1;
  520. }
  521. ia->ri_pd = ib_alloc_pd(ia->ri_id->device);
  522. if (IS_ERR(ia->ri_pd)) {
  523. rc = PTR_ERR(ia->ri_pd);
  524. dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
  525. __func__, rc);
  526. goto out2;
  527. }
  528. rc = ib_query_device(ia->ri_id->device, devattr);
  529. if (rc) {
  530. dprintk("RPC: %s: ib_query_device failed %d\n",
  531. __func__, rc);
  532. goto out3;
  533. }
  534. if (devattr->device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) {
  535. ia->ri_have_dma_lkey = 1;
  536. ia->ri_dma_lkey = ia->ri_id->device->local_dma_lkey;
  537. }
  538. if (memreg == RPCRDMA_FRMR) {
  539. /* Requires both frmr reg and local dma lkey */
  540. if (((devattr->device_cap_flags &
  541. (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) !=
  542. (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) ||
  543. (devattr->max_fast_reg_page_list_len == 0)) {
  544. dprintk("RPC: %s: FRMR registration "
  545. "not supported by HCA\n", __func__);
  546. memreg = RPCRDMA_MTHCAFMR;
  547. }
  548. }
  549. if (memreg == RPCRDMA_MTHCAFMR) {
  550. if (!ia->ri_id->device->alloc_fmr) {
  551. dprintk("RPC: %s: MTHCAFMR registration "
  552. "not supported by HCA\n", __func__);
  553. memreg = RPCRDMA_ALLPHYSICAL;
  554. }
  555. }
  556. /*
  557. * Optionally obtain an underlying physical identity mapping in
  558. * order to do a memory window-based bind. This base registration
  559. * is protected from remote access - that is enabled only by binding
  560. * for the specific bytes targeted during each RPC operation, and
  561. * revoked after the corresponding completion similar to a storage
  562. * adapter.
  563. */
  564. switch (memreg) {
  565. case RPCRDMA_FRMR:
  566. ia->ri_ops = &rpcrdma_frwr_memreg_ops;
  567. break;
  568. case RPCRDMA_ALLPHYSICAL:
  569. ia->ri_ops = &rpcrdma_physical_memreg_ops;
  570. mem_priv = IB_ACCESS_LOCAL_WRITE |
  571. IB_ACCESS_REMOTE_WRITE |
  572. IB_ACCESS_REMOTE_READ;
  573. goto register_setup;
  574. case RPCRDMA_MTHCAFMR:
  575. ia->ri_ops = &rpcrdma_fmr_memreg_ops;
  576. if (ia->ri_have_dma_lkey)
  577. break;
  578. mem_priv = IB_ACCESS_LOCAL_WRITE;
  579. register_setup:
  580. ia->ri_bind_mem = ib_get_dma_mr(ia->ri_pd, mem_priv);
  581. if (IS_ERR(ia->ri_bind_mem)) {
  582. printk(KERN_ALERT "%s: ib_get_dma_mr for "
  583. "phys register failed with %lX\n",
  584. __func__, PTR_ERR(ia->ri_bind_mem));
  585. rc = -ENOMEM;
  586. goto out3;
  587. }
  588. break;
  589. default:
  590. printk(KERN_ERR "RPC: Unsupported memory "
  591. "registration mode: %d\n", memreg);
  592. rc = -ENOMEM;
  593. goto out3;
  594. }
  595. dprintk("RPC: %s: memory registration strategy is '%s'\n",
  596. __func__, ia->ri_ops->ro_displayname);
  597. /* Else will do memory reg/dereg for each chunk */
  598. ia->ri_memreg_strategy = memreg;
  599. rwlock_init(&ia->ri_qplock);
  600. return 0;
  601. out3:
  602. ib_dealloc_pd(ia->ri_pd);
  603. ia->ri_pd = NULL;
  604. out2:
  605. rdma_destroy_id(ia->ri_id);
  606. ia->ri_id = NULL;
  607. out1:
  608. return rc;
  609. }
  610. /*
  611. * Clean up/close an IA.
  612. * o if event handles and PD have been initialized, free them.
  613. * o close the IA
  614. */
  615. void
  616. rpcrdma_ia_close(struct rpcrdma_ia *ia)
  617. {
  618. int rc;
  619. dprintk("RPC: %s: entering\n", __func__);
  620. if (ia->ri_bind_mem != NULL) {
  621. rc = ib_dereg_mr(ia->ri_bind_mem);
  622. dprintk("RPC: %s: ib_dereg_mr returned %i\n",
  623. __func__, rc);
  624. }
  625. if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
  626. if (ia->ri_id->qp)
  627. rdma_destroy_qp(ia->ri_id);
  628. rdma_destroy_id(ia->ri_id);
  629. ia->ri_id = NULL;
  630. }
  631. if (ia->ri_pd != NULL && !IS_ERR(ia->ri_pd)) {
  632. rc = ib_dealloc_pd(ia->ri_pd);
  633. dprintk("RPC: %s: ib_dealloc_pd returned %i\n",
  634. __func__, rc);
  635. }
  636. }
  637. /*
  638. * Create unconnected endpoint.
  639. */
  640. int
  641. rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
  642. struct rpcrdma_create_data_internal *cdata)
  643. {
  644. struct ib_device_attr *devattr = &ia->ri_devattr;
  645. struct ib_cq *sendcq, *recvcq;
  646. int rc, err;
  647. /* check provider's send/recv wr limits */
  648. if (cdata->max_requests > devattr->max_qp_wr)
  649. cdata->max_requests = devattr->max_qp_wr;
  650. ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
  651. ep->rep_attr.qp_context = ep;
  652. ep->rep_attr.srq = NULL;
  653. ep->rep_attr.cap.max_send_wr = cdata->max_requests;
  654. rc = ia->ri_ops->ro_open(ia, ep, cdata);
  655. if (rc)
  656. return rc;
  657. ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
  658. ep->rep_attr.cap.max_send_sge = (cdata->padding ? 4 : 2);
  659. ep->rep_attr.cap.max_recv_sge = 1;
  660. ep->rep_attr.cap.max_inline_data = 0;
  661. ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  662. ep->rep_attr.qp_type = IB_QPT_RC;
  663. ep->rep_attr.port_num = ~0;
  664. if (cdata->padding) {
  665. ep->rep_padbuf = rpcrdma_alloc_regbuf(ia, cdata->padding,
  666. GFP_KERNEL);
  667. if (IS_ERR(ep->rep_padbuf))
  668. return PTR_ERR(ep->rep_padbuf);
  669. } else
  670. ep->rep_padbuf = NULL;
  671. dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
  672. "iovs: send %d recv %d\n",
  673. __func__,
  674. ep->rep_attr.cap.max_send_wr,
  675. ep->rep_attr.cap.max_recv_wr,
  676. ep->rep_attr.cap.max_send_sge,
  677. ep->rep_attr.cap.max_recv_sge);
  678. /* set trigger for requesting send completion */
  679. ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
  680. if (ep->rep_cqinit > RPCRDMA_MAX_UNSIGNALED_SENDS)
  681. ep->rep_cqinit = RPCRDMA_MAX_UNSIGNALED_SENDS;
  682. else if (ep->rep_cqinit <= 2)
  683. ep->rep_cqinit = 0;
  684. INIT_CQCOUNT(ep);
  685. init_waitqueue_head(&ep->rep_connect_wait);
  686. INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
  687. sendcq = ib_create_cq(ia->ri_id->device, rpcrdma_sendcq_upcall,
  688. rpcrdma_cq_async_error_upcall, ep,
  689. ep->rep_attr.cap.max_send_wr + 1, 0);
  690. if (IS_ERR(sendcq)) {
  691. rc = PTR_ERR(sendcq);
  692. dprintk("RPC: %s: failed to create send CQ: %i\n",
  693. __func__, rc);
  694. goto out1;
  695. }
  696. rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
  697. if (rc) {
  698. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  699. __func__, rc);
  700. goto out2;
  701. }
  702. recvcq = ib_create_cq(ia->ri_id->device, rpcrdma_recvcq_upcall,
  703. rpcrdma_cq_async_error_upcall, ep,
  704. ep->rep_attr.cap.max_recv_wr + 1, 0);
  705. if (IS_ERR(recvcq)) {
  706. rc = PTR_ERR(recvcq);
  707. dprintk("RPC: %s: failed to create recv CQ: %i\n",
  708. __func__, rc);
  709. goto out2;
  710. }
  711. rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
  712. if (rc) {
  713. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  714. __func__, rc);
  715. ib_destroy_cq(recvcq);
  716. goto out2;
  717. }
  718. ep->rep_attr.send_cq = sendcq;
  719. ep->rep_attr.recv_cq = recvcq;
  720. /* Initialize cma parameters */
  721. /* RPC/RDMA does not use private data */
  722. ep->rep_remote_cma.private_data = NULL;
  723. ep->rep_remote_cma.private_data_len = 0;
  724. /* Client offers RDMA Read but does not initiate */
  725. ep->rep_remote_cma.initiator_depth = 0;
  726. if (devattr->max_qp_rd_atom > 32) /* arbitrary but <= 255 */
  727. ep->rep_remote_cma.responder_resources = 32;
  728. else
  729. ep->rep_remote_cma.responder_resources =
  730. devattr->max_qp_rd_atom;
  731. ep->rep_remote_cma.retry_count = 7;
  732. ep->rep_remote_cma.flow_control = 0;
  733. ep->rep_remote_cma.rnr_retry_count = 0;
  734. return 0;
  735. out2:
  736. err = ib_destroy_cq(sendcq);
  737. if (err)
  738. dprintk("RPC: %s: ib_destroy_cq returned %i\n",
  739. __func__, err);
  740. out1:
  741. rpcrdma_free_regbuf(ia, ep->rep_padbuf);
  742. return rc;
  743. }
  744. /*
  745. * rpcrdma_ep_destroy
  746. *
  747. * Disconnect and destroy endpoint. After this, the only
  748. * valid operations on the ep are to free it (if dynamically
  749. * allocated) or re-create it.
  750. */
  751. void
  752. rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  753. {
  754. int rc;
  755. dprintk("RPC: %s: entering, connected is %d\n",
  756. __func__, ep->rep_connected);
  757. cancel_delayed_work_sync(&ep->rep_connect_worker);
  758. if (ia->ri_id->qp) {
  759. rpcrdma_ep_disconnect(ep, ia);
  760. rdma_destroy_qp(ia->ri_id);
  761. ia->ri_id->qp = NULL;
  762. }
  763. rpcrdma_free_regbuf(ia, ep->rep_padbuf);
  764. rpcrdma_clean_cq(ep->rep_attr.recv_cq);
  765. rc = ib_destroy_cq(ep->rep_attr.recv_cq);
  766. if (rc)
  767. dprintk("RPC: %s: ib_destroy_cq returned %i\n",
  768. __func__, rc);
  769. rpcrdma_clean_cq(ep->rep_attr.send_cq);
  770. rc = ib_destroy_cq(ep->rep_attr.send_cq);
  771. if (rc)
  772. dprintk("RPC: %s: ib_destroy_cq returned %i\n",
  773. __func__, rc);
  774. }
  775. /*
  776. * Connect unconnected endpoint.
  777. */
  778. int
  779. rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  780. {
  781. struct rdma_cm_id *id, *old;
  782. int rc = 0;
  783. int retry_count = 0;
  784. if (ep->rep_connected != 0) {
  785. struct rpcrdma_xprt *xprt;
  786. retry:
  787. dprintk("RPC: %s: reconnecting...\n", __func__);
  788. rpcrdma_ep_disconnect(ep, ia);
  789. rpcrdma_flush_cqs(ep);
  790. xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
  791. ia->ri_ops->ro_reset(xprt);
  792. id = rpcrdma_create_id(xprt, ia,
  793. (struct sockaddr *)&xprt->rx_data.addr);
  794. if (IS_ERR(id)) {
  795. rc = -EHOSTUNREACH;
  796. goto out;
  797. }
  798. /* TEMP TEMP TEMP - fail if new device:
  799. * Deregister/remarshal *all* requests!
  800. * Close and recreate adapter, pd, etc!
  801. * Re-determine all attributes still sane!
  802. * More stuff I haven't thought of!
  803. * Rrrgh!
  804. */
  805. if (ia->ri_id->device != id->device) {
  806. printk("RPC: %s: can't reconnect on "
  807. "different device!\n", __func__);
  808. rdma_destroy_id(id);
  809. rc = -ENETUNREACH;
  810. goto out;
  811. }
  812. /* END TEMP */
  813. rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
  814. if (rc) {
  815. dprintk("RPC: %s: rdma_create_qp failed %i\n",
  816. __func__, rc);
  817. rdma_destroy_id(id);
  818. rc = -ENETUNREACH;
  819. goto out;
  820. }
  821. write_lock(&ia->ri_qplock);
  822. old = ia->ri_id;
  823. ia->ri_id = id;
  824. write_unlock(&ia->ri_qplock);
  825. rdma_destroy_qp(old);
  826. rdma_destroy_id(old);
  827. } else {
  828. dprintk("RPC: %s: connecting...\n", __func__);
  829. rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
  830. if (rc) {
  831. dprintk("RPC: %s: rdma_create_qp failed %i\n",
  832. __func__, rc);
  833. /* do not update ep->rep_connected */
  834. return -ENETUNREACH;
  835. }
  836. }
  837. ep->rep_connected = 0;
  838. rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
  839. if (rc) {
  840. dprintk("RPC: %s: rdma_connect() failed with %i\n",
  841. __func__, rc);
  842. goto out;
  843. }
  844. wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
  845. /*
  846. * Check state. A non-peer reject indicates no listener
  847. * (ECONNREFUSED), which may be a transient state. All
  848. * others indicate a transport condition which has already
  849. * undergone a best-effort.
  850. */
  851. if (ep->rep_connected == -ECONNREFUSED &&
  852. ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
  853. dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
  854. goto retry;
  855. }
  856. if (ep->rep_connected <= 0) {
  857. /* Sometimes, the only way to reliably connect to remote
  858. * CMs is to use same nonzero values for ORD and IRD. */
  859. if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
  860. (ep->rep_remote_cma.responder_resources == 0 ||
  861. ep->rep_remote_cma.initiator_depth !=
  862. ep->rep_remote_cma.responder_resources)) {
  863. if (ep->rep_remote_cma.responder_resources == 0)
  864. ep->rep_remote_cma.responder_resources = 1;
  865. ep->rep_remote_cma.initiator_depth =
  866. ep->rep_remote_cma.responder_resources;
  867. goto retry;
  868. }
  869. rc = ep->rep_connected;
  870. } else {
  871. dprintk("RPC: %s: connected\n", __func__);
  872. }
  873. out:
  874. if (rc)
  875. ep->rep_connected = rc;
  876. return rc;
  877. }
  878. /*
  879. * rpcrdma_ep_disconnect
  880. *
  881. * This is separate from destroy to facilitate the ability
  882. * to reconnect without recreating the endpoint.
  883. *
  884. * This call is not reentrant, and must not be made in parallel
  885. * on the same endpoint.
  886. */
  887. void
  888. rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  889. {
  890. int rc;
  891. rpcrdma_flush_cqs(ep);
  892. rc = rdma_disconnect(ia->ri_id);
  893. if (!rc) {
  894. /* returns without wait if not connected */
  895. wait_event_interruptible(ep->rep_connect_wait,
  896. ep->rep_connected != 1);
  897. dprintk("RPC: %s: after wait, %sconnected\n", __func__,
  898. (ep->rep_connected == 1) ? "still " : "dis");
  899. } else {
  900. dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
  901. ep->rep_connected = rc;
  902. }
  903. }
  904. static struct rpcrdma_req *
  905. rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
  906. {
  907. struct rpcrdma_req *req;
  908. req = kzalloc(sizeof(*req), GFP_KERNEL);
  909. if (req == NULL)
  910. return ERR_PTR(-ENOMEM);
  911. req->rl_buffer = &r_xprt->rx_buf;
  912. return req;
  913. }
  914. static struct rpcrdma_rep *
  915. rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
  916. {
  917. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  918. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  919. struct rpcrdma_rep *rep;
  920. int rc;
  921. rc = -ENOMEM;
  922. rep = kzalloc(sizeof(*rep), GFP_KERNEL);
  923. if (rep == NULL)
  924. goto out;
  925. rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize,
  926. GFP_KERNEL);
  927. if (IS_ERR(rep->rr_rdmabuf)) {
  928. rc = PTR_ERR(rep->rr_rdmabuf);
  929. goto out_free;
  930. }
  931. rep->rr_buffer = &r_xprt->rx_buf;
  932. return rep;
  933. out_free:
  934. kfree(rep);
  935. out:
  936. return ERR_PTR(rc);
  937. }
  938. int
  939. rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
  940. {
  941. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  942. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  943. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  944. char *p;
  945. size_t len;
  946. int i, rc;
  947. buf->rb_max_requests = cdata->max_requests;
  948. spin_lock_init(&buf->rb_lock);
  949. /* Need to allocate:
  950. * 1. arrays for send and recv pointers
  951. * 2. arrays of struct rpcrdma_req to fill in pointers
  952. * 3. array of struct rpcrdma_rep for replies
  953. * Send/recv buffers in req/rep need to be registered
  954. */
  955. len = buf->rb_max_requests *
  956. (sizeof(struct rpcrdma_req *) + sizeof(struct rpcrdma_rep *));
  957. p = kzalloc(len, GFP_KERNEL);
  958. if (p == NULL) {
  959. dprintk("RPC: %s: req_t/rep_t/pad kzalloc(%zd) failed\n",
  960. __func__, len);
  961. rc = -ENOMEM;
  962. goto out;
  963. }
  964. buf->rb_pool = p; /* for freeing it later */
  965. buf->rb_send_bufs = (struct rpcrdma_req **) p;
  966. p = (char *) &buf->rb_send_bufs[buf->rb_max_requests];
  967. buf->rb_recv_bufs = (struct rpcrdma_rep **) p;
  968. p = (char *) &buf->rb_recv_bufs[buf->rb_max_requests];
  969. rc = ia->ri_ops->ro_init(r_xprt);
  970. if (rc)
  971. goto out;
  972. for (i = 0; i < buf->rb_max_requests; i++) {
  973. struct rpcrdma_req *req;
  974. struct rpcrdma_rep *rep;
  975. req = rpcrdma_create_req(r_xprt);
  976. if (IS_ERR(req)) {
  977. dprintk("RPC: %s: request buffer %d alloc"
  978. " failed\n", __func__, i);
  979. rc = PTR_ERR(req);
  980. goto out;
  981. }
  982. buf->rb_send_bufs[i] = req;
  983. rep = rpcrdma_create_rep(r_xprt);
  984. if (IS_ERR(rep)) {
  985. dprintk("RPC: %s: reply buffer %d alloc failed\n",
  986. __func__, i);
  987. rc = PTR_ERR(rep);
  988. goto out;
  989. }
  990. buf->rb_recv_bufs[i] = rep;
  991. }
  992. return 0;
  993. out:
  994. rpcrdma_buffer_destroy(buf);
  995. return rc;
  996. }
  997. static void
  998. rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
  999. {
  1000. if (!rep)
  1001. return;
  1002. rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
  1003. kfree(rep);
  1004. }
  1005. static void
  1006. rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
  1007. {
  1008. if (!req)
  1009. return;
  1010. rpcrdma_free_regbuf(ia, req->rl_sendbuf);
  1011. rpcrdma_free_regbuf(ia, req->rl_rdmabuf);
  1012. kfree(req);
  1013. }
  1014. void
  1015. rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
  1016. {
  1017. struct rpcrdma_ia *ia = rdmab_to_ia(buf);
  1018. int i;
  1019. /* clean up in reverse order from create
  1020. * 1. recv mr memory (mr free, then kfree)
  1021. * 2. send mr memory (mr free, then kfree)
  1022. * 3. MWs
  1023. */
  1024. dprintk("RPC: %s: entering\n", __func__);
  1025. for (i = 0; i < buf->rb_max_requests; i++) {
  1026. if (buf->rb_recv_bufs)
  1027. rpcrdma_destroy_rep(ia, buf->rb_recv_bufs[i]);
  1028. if (buf->rb_send_bufs)
  1029. rpcrdma_destroy_req(ia, buf->rb_send_bufs[i]);
  1030. }
  1031. ia->ri_ops->ro_destroy(buf);
  1032. kfree(buf->rb_pool);
  1033. }
  1034. /* "*mw" can be NULL when rpcrdma_buffer_get_mrs() fails, leaving
  1035. * some req segments uninitialized.
  1036. */
  1037. static void
  1038. rpcrdma_buffer_put_mr(struct rpcrdma_mw **mw, struct rpcrdma_buffer *buf)
  1039. {
  1040. if (*mw) {
  1041. list_add_tail(&(*mw)->mw_list, &buf->rb_mws);
  1042. *mw = NULL;
  1043. }
  1044. }
  1045. /* Cycle mw's back in reverse order, and "spin" them.
  1046. * This delays and scrambles reuse as much as possible.
  1047. */
  1048. static void
  1049. rpcrdma_buffer_put_mrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
  1050. {
  1051. struct rpcrdma_mr_seg *seg = req->rl_segments;
  1052. struct rpcrdma_mr_seg *seg1 = seg;
  1053. int i;
  1054. for (i = 1, seg++; i < RPCRDMA_MAX_SEGS; seg++, i++)
  1055. rpcrdma_buffer_put_mr(&seg->rl_mw, buf);
  1056. rpcrdma_buffer_put_mr(&seg1->rl_mw, buf);
  1057. }
  1058. static void
  1059. rpcrdma_buffer_put_sendbuf(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
  1060. {
  1061. buf->rb_send_bufs[--buf->rb_send_index] = req;
  1062. req->rl_niovs = 0;
  1063. if (req->rl_reply) {
  1064. buf->rb_recv_bufs[--buf->rb_recv_index] = req->rl_reply;
  1065. req->rl_reply->rr_func = NULL;
  1066. req->rl_reply = NULL;
  1067. }
  1068. }
  1069. /* rpcrdma_unmap_one() was already done during deregistration.
  1070. * Redo only the ib_post_send().
  1071. */
  1072. static void
  1073. rpcrdma_retry_local_inv(struct rpcrdma_mw *r, struct rpcrdma_ia *ia)
  1074. {
  1075. struct rpcrdma_xprt *r_xprt =
  1076. container_of(ia, struct rpcrdma_xprt, rx_ia);
  1077. struct ib_send_wr invalidate_wr, *bad_wr;
  1078. int rc;
  1079. dprintk("RPC: %s: FRMR %p is stale\n", __func__, r);
  1080. /* When this FRMR is re-inserted into rb_mws, it is no longer stale */
  1081. r->r.frmr.fr_state = FRMR_IS_INVALID;
  1082. memset(&invalidate_wr, 0, sizeof(invalidate_wr));
  1083. invalidate_wr.wr_id = (unsigned long)(void *)r;
  1084. invalidate_wr.opcode = IB_WR_LOCAL_INV;
  1085. invalidate_wr.ex.invalidate_rkey = r->r.frmr.fr_mr->rkey;
  1086. DECR_CQCOUNT(&r_xprt->rx_ep);
  1087. dprintk("RPC: %s: frmr %p invalidating rkey %08x\n",
  1088. __func__, r, r->r.frmr.fr_mr->rkey);
  1089. read_lock(&ia->ri_qplock);
  1090. rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr);
  1091. read_unlock(&ia->ri_qplock);
  1092. if (rc) {
  1093. /* Force rpcrdma_buffer_get() to retry */
  1094. r->r.frmr.fr_state = FRMR_IS_STALE;
  1095. dprintk("RPC: %s: ib_post_send failed, %i\n",
  1096. __func__, rc);
  1097. }
  1098. }
  1099. static void
  1100. rpcrdma_retry_flushed_linv(struct list_head *stale,
  1101. struct rpcrdma_buffer *buf)
  1102. {
  1103. struct rpcrdma_ia *ia = rdmab_to_ia(buf);
  1104. struct list_head *pos;
  1105. struct rpcrdma_mw *r;
  1106. unsigned long flags;
  1107. list_for_each(pos, stale) {
  1108. r = list_entry(pos, struct rpcrdma_mw, mw_list);
  1109. rpcrdma_retry_local_inv(r, ia);
  1110. }
  1111. spin_lock_irqsave(&buf->rb_lock, flags);
  1112. list_splice_tail(stale, &buf->rb_mws);
  1113. spin_unlock_irqrestore(&buf->rb_lock, flags);
  1114. }
  1115. static struct rpcrdma_req *
  1116. rpcrdma_buffer_get_frmrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf,
  1117. struct list_head *stale)
  1118. {
  1119. struct rpcrdma_mw *r;
  1120. int i;
  1121. i = RPCRDMA_MAX_SEGS - 1;
  1122. while (!list_empty(&buf->rb_mws)) {
  1123. r = list_entry(buf->rb_mws.next,
  1124. struct rpcrdma_mw, mw_list);
  1125. list_del(&r->mw_list);
  1126. if (r->r.frmr.fr_state == FRMR_IS_STALE) {
  1127. list_add(&r->mw_list, stale);
  1128. continue;
  1129. }
  1130. req->rl_segments[i].rl_mw = r;
  1131. if (unlikely(i-- == 0))
  1132. return req; /* Success */
  1133. }
  1134. /* Not enough entries on rb_mws for this req */
  1135. rpcrdma_buffer_put_sendbuf(req, buf);
  1136. rpcrdma_buffer_put_mrs(req, buf);
  1137. return NULL;
  1138. }
  1139. static struct rpcrdma_req *
  1140. rpcrdma_buffer_get_fmrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
  1141. {
  1142. struct rpcrdma_mw *r;
  1143. int i;
  1144. i = RPCRDMA_MAX_SEGS - 1;
  1145. while (!list_empty(&buf->rb_mws)) {
  1146. r = list_entry(buf->rb_mws.next,
  1147. struct rpcrdma_mw, mw_list);
  1148. list_del(&r->mw_list);
  1149. req->rl_segments[i].rl_mw = r;
  1150. if (unlikely(i-- == 0))
  1151. return req; /* Success */
  1152. }
  1153. /* Not enough entries on rb_mws for this req */
  1154. rpcrdma_buffer_put_sendbuf(req, buf);
  1155. rpcrdma_buffer_put_mrs(req, buf);
  1156. return NULL;
  1157. }
  1158. /*
  1159. * Get a set of request/reply buffers.
  1160. *
  1161. * Reply buffer (if needed) is attached to send buffer upon return.
  1162. * Rule:
  1163. * rb_send_index and rb_recv_index MUST always be pointing to the
  1164. * *next* available buffer (non-NULL). They are incremented after
  1165. * removing buffers, and decremented *before* returning them.
  1166. */
  1167. struct rpcrdma_req *
  1168. rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
  1169. {
  1170. struct rpcrdma_ia *ia = rdmab_to_ia(buffers);
  1171. struct list_head stale;
  1172. struct rpcrdma_req *req;
  1173. unsigned long flags;
  1174. spin_lock_irqsave(&buffers->rb_lock, flags);
  1175. if (buffers->rb_send_index == buffers->rb_max_requests) {
  1176. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1177. dprintk("RPC: %s: out of request buffers\n", __func__);
  1178. return ((struct rpcrdma_req *)NULL);
  1179. }
  1180. req = buffers->rb_send_bufs[buffers->rb_send_index];
  1181. if (buffers->rb_send_index < buffers->rb_recv_index) {
  1182. dprintk("RPC: %s: %d extra receives outstanding (ok)\n",
  1183. __func__,
  1184. buffers->rb_recv_index - buffers->rb_send_index);
  1185. req->rl_reply = NULL;
  1186. } else {
  1187. req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
  1188. buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
  1189. }
  1190. buffers->rb_send_bufs[buffers->rb_send_index++] = NULL;
  1191. INIT_LIST_HEAD(&stale);
  1192. switch (ia->ri_memreg_strategy) {
  1193. case RPCRDMA_FRMR:
  1194. req = rpcrdma_buffer_get_frmrs(req, buffers, &stale);
  1195. break;
  1196. case RPCRDMA_MTHCAFMR:
  1197. req = rpcrdma_buffer_get_fmrs(req, buffers);
  1198. break;
  1199. default:
  1200. break;
  1201. }
  1202. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1203. if (!list_empty(&stale))
  1204. rpcrdma_retry_flushed_linv(&stale, buffers);
  1205. return req;
  1206. }
  1207. /*
  1208. * Put request/reply buffers back into pool.
  1209. * Pre-decrement counter/array index.
  1210. */
  1211. void
  1212. rpcrdma_buffer_put(struct rpcrdma_req *req)
  1213. {
  1214. struct rpcrdma_buffer *buffers = req->rl_buffer;
  1215. struct rpcrdma_ia *ia = rdmab_to_ia(buffers);
  1216. unsigned long flags;
  1217. spin_lock_irqsave(&buffers->rb_lock, flags);
  1218. rpcrdma_buffer_put_sendbuf(req, buffers);
  1219. switch (ia->ri_memreg_strategy) {
  1220. case RPCRDMA_FRMR:
  1221. case RPCRDMA_MTHCAFMR:
  1222. rpcrdma_buffer_put_mrs(req, buffers);
  1223. break;
  1224. default:
  1225. break;
  1226. }
  1227. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1228. }
  1229. /*
  1230. * Recover reply buffers from pool.
  1231. * This happens when recovering from error conditions.
  1232. * Post-increment counter/array index.
  1233. */
  1234. void
  1235. rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
  1236. {
  1237. struct rpcrdma_buffer *buffers = req->rl_buffer;
  1238. unsigned long flags;
  1239. spin_lock_irqsave(&buffers->rb_lock, flags);
  1240. if (buffers->rb_recv_index < buffers->rb_max_requests) {
  1241. req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
  1242. buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
  1243. }
  1244. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1245. }
  1246. /*
  1247. * Put reply buffers back into pool when not attached to
  1248. * request. This happens in error conditions.
  1249. */
  1250. void
  1251. rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
  1252. {
  1253. struct rpcrdma_buffer *buffers = rep->rr_buffer;
  1254. unsigned long flags;
  1255. rep->rr_func = NULL;
  1256. spin_lock_irqsave(&buffers->rb_lock, flags);
  1257. buffers->rb_recv_bufs[--buffers->rb_recv_index] = rep;
  1258. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1259. }
  1260. /*
  1261. * Wrappers for internal-use kmalloc memory registration, used by buffer code.
  1262. */
  1263. void
  1264. rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg)
  1265. {
  1266. dprintk("RPC: map_one: offset %p iova %llx len %zu\n",
  1267. seg->mr_offset,
  1268. (unsigned long long)seg->mr_dma, seg->mr_dmalen);
  1269. }
  1270. static int
  1271. rpcrdma_register_internal(struct rpcrdma_ia *ia, void *va, int len,
  1272. struct ib_mr **mrp, struct ib_sge *iov)
  1273. {
  1274. struct ib_phys_buf ipb;
  1275. struct ib_mr *mr;
  1276. int rc;
  1277. /*
  1278. * All memory passed here was kmalloc'ed, therefore phys-contiguous.
  1279. */
  1280. iov->addr = ib_dma_map_single(ia->ri_id->device,
  1281. va, len, DMA_BIDIRECTIONAL);
  1282. if (ib_dma_mapping_error(ia->ri_id->device, iov->addr))
  1283. return -ENOMEM;
  1284. iov->length = len;
  1285. if (ia->ri_have_dma_lkey) {
  1286. *mrp = NULL;
  1287. iov->lkey = ia->ri_dma_lkey;
  1288. return 0;
  1289. } else if (ia->ri_bind_mem != NULL) {
  1290. *mrp = NULL;
  1291. iov->lkey = ia->ri_bind_mem->lkey;
  1292. return 0;
  1293. }
  1294. ipb.addr = iov->addr;
  1295. ipb.size = iov->length;
  1296. mr = ib_reg_phys_mr(ia->ri_pd, &ipb, 1,
  1297. IB_ACCESS_LOCAL_WRITE, &iov->addr);
  1298. dprintk("RPC: %s: phys convert: 0x%llx "
  1299. "registered 0x%llx length %d\n",
  1300. __func__, (unsigned long long)ipb.addr,
  1301. (unsigned long long)iov->addr, len);
  1302. if (IS_ERR(mr)) {
  1303. *mrp = NULL;
  1304. rc = PTR_ERR(mr);
  1305. dprintk("RPC: %s: failed with %i\n", __func__, rc);
  1306. } else {
  1307. *mrp = mr;
  1308. iov->lkey = mr->lkey;
  1309. rc = 0;
  1310. }
  1311. return rc;
  1312. }
  1313. static int
  1314. rpcrdma_deregister_internal(struct rpcrdma_ia *ia,
  1315. struct ib_mr *mr, struct ib_sge *iov)
  1316. {
  1317. int rc;
  1318. ib_dma_unmap_single(ia->ri_id->device,
  1319. iov->addr, iov->length, DMA_BIDIRECTIONAL);
  1320. if (NULL == mr)
  1321. return 0;
  1322. rc = ib_dereg_mr(mr);
  1323. if (rc)
  1324. dprintk("RPC: %s: ib_dereg_mr failed %i\n", __func__, rc);
  1325. return rc;
  1326. }
  1327. /**
  1328. * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
  1329. * @ia: controlling rpcrdma_ia
  1330. * @size: size of buffer to be allocated, in bytes
  1331. * @flags: GFP flags
  1332. *
  1333. * Returns pointer to private header of an area of internally
  1334. * registered memory, or an ERR_PTR. The registered buffer follows
  1335. * the end of the private header.
  1336. *
  1337. * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
  1338. * receiving the payload of RDMA RECV operations. regbufs are not
  1339. * used for RDMA READ/WRITE operations, thus are registered only for
  1340. * LOCAL access.
  1341. */
  1342. struct rpcrdma_regbuf *
  1343. rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
  1344. {
  1345. struct rpcrdma_regbuf *rb;
  1346. int rc;
  1347. rc = -ENOMEM;
  1348. rb = kmalloc(sizeof(*rb) + size, flags);
  1349. if (rb == NULL)
  1350. goto out;
  1351. rb->rg_size = size;
  1352. rb->rg_owner = NULL;
  1353. rc = rpcrdma_register_internal(ia, rb->rg_base, size,
  1354. &rb->rg_mr, &rb->rg_iov);
  1355. if (rc)
  1356. goto out_free;
  1357. return rb;
  1358. out_free:
  1359. kfree(rb);
  1360. out:
  1361. return ERR_PTR(rc);
  1362. }
  1363. /**
  1364. * rpcrdma_free_regbuf - deregister and free registered buffer
  1365. * @ia: controlling rpcrdma_ia
  1366. * @rb: regbuf to be deregistered and freed
  1367. */
  1368. void
  1369. rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
  1370. {
  1371. if (rb) {
  1372. rpcrdma_deregister_internal(ia, rb->rg_mr, &rb->rg_iov);
  1373. kfree(rb);
  1374. }
  1375. }
  1376. /*
  1377. * Prepost any receive buffer, then post send.
  1378. *
  1379. * Receive buffer is donated to hardware, reclaimed upon recv completion.
  1380. */
  1381. int
  1382. rpcrdma_ep_post(struct rpcrdma_ia *ia,
  1383. struct rpcrdma_ep *ep,
  1384. struct rpcrdma_req *req)
  1385. {
  1386. struct ib_send_wr send_wr, *send_wr_fail;
  1387. struct rpcrdma_rep *rep = req->rl_reply;
  1388. int rc;
  1389. if (rep) {
  1390. rc = rpcrdma_ep_post_recv(ia, ep, rep);
  1391. if (rc)
  1392. goto out;
  1393. req->rl_reply = NULL;
  1394. }
  1395. send_wr.next = NULL;
  1396. send_wr.wr_id = RPCRDMA_IGNORE_COMPLETION;
  1397. send_wr.sg_list = req->rl_send_iov;
  1398. send_wr.num_sge = req->rl_niovs;
  1399. send_wr.opcode = IB_WR_SEND;
  1400. if (send_wr.num_sge == 4) /* no need to sync any pad (constant) */
  1401. ib_dma_sync_single_for_device(ia->ri_id->device,
  1402. req->rl_send_iov[3].addr, req->rl_send_iov[3].length,
  1403. DMA_TO_DEVICE);
  1404. ib_dma_sync_single_for_device(ia->ri_id->device,
  1405. req->rl_send_iov[1].addr, req->rl_send_iov[1].length,
  1406. DMA_TO_DEVICE);
  1407. ib_dma_sync_single_for_device(ia->ri_id->device,
  1408. req->rl_send_iov[0].addr, req->rl_send_iov[0].length,
  1409. DMA_TO_DEVICE);
  1410. if (DECR_CQCOUNT(ep) > 0)
  1411. send_wr.send_flags = 0;
  1412. else { /* Provider must take a send completion every now and then */
  1413. INIT_CQCOUNT(ep);
  1414. send_wr.send_flags = IB_SEND_SIGNALED;
  1415. }
  1416. rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
  1417. if (rc)
  1418. dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
  1419. rc);
  1420. out:
  1421. return rc;
  1422. }
  1423. /*
  1424. * (Re)post a receive buffer.
  1425. */
  1426. int
  1427. rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
  1428. struct rpcrdma_ep *ep,
  1429. struct rpcrdma_rep *rep)
  1430. {
  1431. struct ib_recv_wr recv_wr, *recv_wr_fail;
  1432. int rc;
  1433. recv_wr.next = NULL;
  1434. recv_wr.wr_id = (u64) (unsigned long) rep;
  1435. recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
  1436. recv_wr.num_sge = 1;
  1437. ib_dma_sync_single_for_cpu(ia->ri_id->device,
  1438. rdmab_addr(rep->rr_rdmabuf),
  1439. rdmab_length(rep->rr_rdmabuf),
  1440. DMA_BIDIRECTIONAL);
  1441. rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
  1442. if (rc)
  1443. dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
  1444. rc);
  1445. return rc;
  1446. }
  1447. /* How many chunk list items fit within our inline buffers?
  1448. */
  1449. unsigned int
  1450. rpcrdma_max_segments(struct rpcrdma_xprt *r_xprt)
  1451. {
  1452. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  1453. int bytes, segments;
  1454. bytes = min_t(unsigned int, cdata->inline_wsize, cdata->inline_rsize);
  1455. bytes -= RPCRDMA_HDRLEN_MIN;
  1456. if (bytes < sizeof(struct rpcrdma_segment) * 2) {
  1457. pr_warn("RPC: %s: inline threshold too small\n",
  1458. __func__);
  1459. return 0;
  1460. }
  1461. segments = 1 << (fls(bytes / sizeof(struct rpcrdma_segment)) - 1);
  1462. dprintk("RPC: %s: max chunk list size = %d segments\n",
  1463. __func__, segments);
  1464. return segments;
  1465. }