svc_rdma_transport.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. * Copyright (c) 2005-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. * Author: Tom Tucker <tom@opengridcomputing.com>
  40. */
  41. #include <linux/sunrpc/svc_xprt.h>
  42. #include <linux/sunrpc/debug.h>
  43. #include <linux/sunrpc/rpc_rdma.h>
  44. #include <linux/spinlock.h>
  45. #include <rdma/ib_verbs.h>
  46. #include <rdma/rdma_cm.h>
  47. #include <linux/sunrpc/svc_rdma.h>
  48. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  49. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  50. struct sockaddr *sa, int salen,
  51. int flags);
  52. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
  53. static void svc_rdma_release_rqst(struct svc_rqst *);
  54. static void dto_tasklet_func(unsigned long data);
  55. static void svc_rdma_detach(struct svc_xprt *xprt);
  56. static void svc_rdma_free(struct svc_xprt *xprt);
  57. static int svc_rdma_has_wspace(struct svc_xprt *xprt);
  58. static void rq_cq_reap(struct svcxprt_rdma *xprt);
  59. static void sq_cq_reap(struct svcxprt_rdma *xprt);
  60. static DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
  61. static DEFINE_SPINLOCK(dto_lock);
  62. static LIST_HEAD(dto_xprt_q);
  63. static struct svc_xprt_ops svc_rdma_ops = {
  64. .xpo_create = svc_rdma_create,
  65. .xpo_recvfrom = svc_rdma_recvfrom,
  66. .xpo_sendto = svc_rdma_sendto,
  67. .xpo_release_rqst = svc_rdma_release_rqst,
  68. .xpo_detach = svc_rdma_detach,
  69. .xpo_free = svc_rdma_free,
  70. .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
  71. .xpo_has_wspace = svc_rdma_has_wspace,
  72. .xpo_accept = svc_rdma_accept,
  73. };
  74. struct svc_xprt_class svc_rdma_class = {
  75. .xcl_name = "rdma",
  76. .xcl_owner = THIS_MODULE,
  77. .xcl_ops = &svc_rdma_ops,
  78. .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
  79. };
  80. /* WR context cache. Created in svc_rdma.c */
  81. extern struct kmem_cache *svc_rdma_ctxt_cachep;
  82. struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
  83. {
  84. struct svc_rdma_op_ctxt *ctxt;
  85. while (1) {
  86. ctxt = kmem_cache_alloc(svc_rdma_ctxt_cachep, GFP_KERNEL);
  87. if (ctxt)
  88. break;
  89. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  90. }
  91. ctxt->xprt = xprt;
  92. INIT_LIST_HEAD(&ctxt->dto_q);
  93. ctxt->count = 0;
  94. ctxt->frmr = NULL;
  95. atomic_inc(&xprt->sc_ctxt_used);
  96. return ctxt;
  97. }
  98. void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
  99. {
  100. struct svcxprt_rdma *xprt = ctxt->xprt;
  101. int i;
  102. for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
  103. /*
  104. * Unmap the DMA addr in the SGE if the lkey matches
  105. * the sc_dma_lkey, otherwise, ignore it since it is
  106. * an FRMR lkey and will be unmapped later when the
  107. * last WR that uses it completes.
  108. */
  109. if (ctxt->sge[i].lkey == xprt->sc_dma_lkey) {
  110. atomic_dec(&xprt->sc_dma_used);
  111. ib_dma_unmap_single(xprt->sc_cm_id->device,
  112. ctxt->sge[i].addr,
  113. ctxt->sge[i].length,
  114. ctxt->direction);
  115. }
  116. }
  117. }
  118. void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
  119. {
  120. struct svcxprt_rdma *xprt;
  121. int i;
  122. BUG_ON(!ctxt);
  123. xprt = ctxt->xprt;
  124. if (free_pages)
  125. for (i = 0; i < ctxt->count; i++)
  126. put_page(ctxt->pages[i]);
  127. kmem_cache_free(svc_rdma_ctxt_cachep, ctxt);
  128. atomic_dec(&xprt->sc_ctxt_used);
  129. }
  130. /* Temporary NFS request map cache. Created in svc_rdma.c */
  131. extern struct kmem_cache *svc_rdma_map_cachep;
  132. /*
  133. * Temporary NFS req mappings are shared across all transport
  134. * instances. These are short lived and should be bounded by the number
  135. * of concurrent server threads * depth of the SQ.
  136. */
  137. struct svc_rdma_req_map *svc_rdma_get_req_map(void)
  138. {
  139. struct svc_rdma_req_map *map;
  140. while (1) {
  141. map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
  142. if (map)
  143. break;
  144. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  145. }
  146. map->count = 0;
  147. map->frmr = NULL;
  148. return map;
  149. }
  150. void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
  151. {
  152. kmem_cache_free(svc_rdma_map_cachep, map);
  153. }
  154. /* ib_cq event handler */
  155. static void cq_event_handler(struct ib_event *event, void *context)
  156. {
  157. struct svc_xprt *xprt = context;
  158. dprintk("svcrdma: received CQ event id=%d, context=%p\n",
  159. event->event, context);
  160. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  161. }
  162. /* QP event handler */
  163. static void qp_event_handler(struct ib_event *event, void *context)
  164. {
  165. struct svc_xprt *xprt = context;
  166. switch (event->event) {
  167. /* These are considered benign events */
  168. case IB_EVENT_PATH_MIG:
  169. case IB_EVENT_COMM_EST:
  170. case IB_EVENT_SQ_DRAINED:
  171. case IB_EVENT_QP_LAST_WQE_REACHED:
  172. dprintk("svcrdma: QP event %d received for QP=%p\n",
  173. event->event, event->element.qp);
  174. break;
  175. /* These are considered fatal events */
  176. case IB_EVENT_PATH_MIG_ERR:
  177. case IB_EVENT_QP_FATAL:
  178. case IB_EVENT_QP_REQ_ERR:
  179. case IB_EVENT_QP_ACCESS_ERR:
  180. case IB_EVENT_DEVICE_FATAL:
  181. default:
  182. dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
  183. "closing transport\n",
  184. event->event, event->element.qp);
  185. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  186. break;
  187. }
  188. }
  189. /*
  190. * Data Transfer Operation Tasklet
  191. *
  192. * Walks a list of transports with I/O pending, removing entries as
  193. * they are added to the server's I/O pending list. Two bits indicate
  194. * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
  195. * spinlock that serializes access to the transport list with the RQ
  196. * and SQ interrupt handlers.
  197. */
  198. static void dto_tasklet_func(unsigned long data)
  199. {
  200. struct svcxprt_rdma *xprt;
  201. unsigned long flags;
  202. spin_lock_irqsave(&dto_lock, flags);
  203. while (!list_empty(&dto_xprt_q)) {
  204. xprt = list_entry(dto_xprt_q.next,
  205. struct svcxprt_rdma, sc_dto_q);
  206. list_del_init(&xprt->sc_dto_q);
  207. spin_unlock_irqrestore(&dto_lock, flags);
  208. rq_cq_reap(xprt);
  209. sq_cq_reap(xprt);
  210. svc_xprt_put(&xprt->sc_xprt);
  211. spin_lock_irqsave(&dto_lock, flags);
  212. }
  213. spin_unlock_irqrestore(&dto_lock, flags);
  214. }
  215. /*
  216. * Receive Queue Completion Handler
  217. *
  218. * Since an RQ completion handler is called on interrupt context, we
  219. * need to defer the handling of the I/O to a tasklet
  220. */
  221. static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
  222. {
  223. struct svcxprt_rdma *xprt = cq_context;
  224. unsigned long flags;
  225. /* Guard against unconditional flush call for destroyed QP */
  226. if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
  227. return;
  228. /*
  229. * Set the bit regardless of whether or not it's on the list
  230. * because it may be on the list already due to an SQ
  231. * completion.
  232. */
  233. set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
  234. /*
  235. * If this transport is not already on the DTO transport queue,
  236. * add it
  237. */
  238. spin_lock_irqsave(&dto_lock, flags);
  239. if (list_empty(&xprt->sc_dto_q)) {
  240. svc_xprt_get(&xprt->sc_xprt);
  241. list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
  242. }
  243. spin_unlock_irqrestore(&dto_lock, flags);
  244. /* Tasklet does all the work to avoid irqsave locks. */
  245. tasklet_schedule(&dto_tasklet);
  246. }
  247. /*
  248. * rq_cq_reap - Process the RQ CQ.
  249. *
  250. * Take all completing WC off the CQE and enqueue the associated DTO
  251. * context on the dto_q for the transport.
  252. *
  253. * Note that caller must hold a transport reference.
  254. */
  255. static void rq_cq_reap(struct svcxprt_rdma *xprt)
  256. {
  257. int ret;
  258. struct ib_wc wc;
  259. struct svc_rdma_op_ctxt *ctxt = NULL;
  260. if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
  261. return;
  262. ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
  263. atomic_inc(&rdma_stat_rq_poll);
  264. while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
  265. ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
  266. ctxt->wc_status = wc.status;
  267. ctxt->byte_len = wc.byte_len;
  268. svc_rdma_unmap_dma(ctxt);
  269. if (wc.status != IB_WC_SUCCESS) {
  270. /* Close the transport */
  271. dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
  272. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  273. svc_rdma_put_context(ctxt, 1);
  274. svc_xprt_put(&xprt->sc_xprt);
  275. continue;
  276. }
  277. spin_lock_bh(&xprt->sc_rq_dto_lock);
  278. list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
  279. spin_unlock_bh(&xprt->sc_rq_dto_lock);
  280. svc_xprt_put(&xprt->sc_xprt);
  281. }
  282. if (ctxt)
  283. atomic_inc(&rdma_stat_rq_prod);
  284. set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
  285. /*
  286. * If data arrived before established event,
  287. * don't enqueue. This defers RPC I/O until the
  288. * RDMA connection is complete.
  289. */
  290. if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
  291. svc_xprt_enqueue(&xprt->sc_xprt);
  292. }
  293. /*
  294. * Processs a completion context
  295. */
  296. static void process_context(struct svcxprt_rdma *xprt,
  297. struct svc_rdma_op_ctxt *ctxt)
  298. {
  299. svc_rdma_unmap_dma(ctxt);
  300. switch (ctxt->wr_op) {
  301. case IB_WR_SEND:
  302. if (test_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags))
  303. svc_rdma_put_frmr(xprt, ctxt->frmr);
  304. svc_rdma_put_context(ctxt, 1);
  305. break;
  306. case IB_WR_RDMA_WRITE:
  307. svc_rdma_put_context(ctxt, 0);
  308. break;
  309. case IB_WR_RDMA_READ:
  310. case IB_WR_RDMA_READ_WITH_INV:
  311. if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
  312. struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
  313. BUG_ON(!read_hdr);
  314. if (test_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags))
  315. svc_rdma_put_frmr(xprt, ctxt->frmr);
  316. spin_lock_bh(&xprt->sc_rq_dto_lock);
  317. set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
  318. list_add_tail(&read_hdr->dto_q,
  319. &xprt->sc_read_complete_q);
  320. spin_unlock_bh(&xprt->sc_rq_dto_lock);
  321. svc_xprt_enqueue(&xprt->sc_xprt);
  322. }
  323. svc_rdma_put_context(ctxt, 0);
  324. break;
  325. default:
  326. printk(KERN_ERR "svcrdma: unexpected completion type, "
  327. "opcode=%d\n",
  328. ctxt->wr_op);
  329. break;
  330. }
  331. }
  332. /*
  333. * Send Queue Completion Handler - potentially called on interrupt context.
  334. *
  335. * Note that caller must hold a transport reference.
  336. */
  337. static void sq_cq_reap(struct svcxprt_rdma *xprt)
  338. {
  339. struct svc_rdma_op_ctxt *ctxt = NULL;
  340. struct ib_wc wc;
  341. struct ib_cq *cq = xprt->sc_sq_cq;
  342. int ret;
  343. if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
  344. return;
  345. ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  346. atomic_inc(&rdma_stat_sq_poll);
  347. while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
  348. if (wc.status != IB_WC_SUCCESS)
  349. /* Close the transport */
  350. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  351. /* Decrement used SQ WR count */
  352. atomic_dec(&xprt->sc_sq_count);
  353. wake_up(&xprt->sc_send_wait);
  354. ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
  355. if (ctxt)
  356. process_context(xprt, ctxt);
  357. svc_xprt_put(&xprt->sc_xprt);
  358. }
  359. if (ctxt)
  360. atomic_inc(&rdma_stat_sq_prod);
  361. }
  362. static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
  363. {
  364. struct svcxprt_rdma *xprt = cq_context;
  365. unsigned long flags;
  366. /* Guard against unconditional flush call for destroyed QP */
  367. if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
  368. return;
  369. /*
  370. * Set the bit regardless of whether or not it's on the list
  371. * because it may be on the list already due to an RQ
  372. * completion.
  373. */
  374. set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
  375. /*
  376. * If this transport is not already on the DTO transport queue,
  377. * add it
  378. */
  379. spin_lock_irqsave(&dto_lock, flags);
  380. if (list_empty(&xprt->sc_dto_q)) {
  381. svc_xprt_get(&xprt->sc_xprt);
  382. list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
  383. }
  384. spin_unlock_irqrestore(&dto_lock, flags);
  385. /* Tasklet does all the work to avoid irqsave locks. */
  386. tasklet_schedule(&dto_tasklet);
  387. }
  388. static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
  389. int listener)
  390. {
  391. struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
  392. if (!cma_xprt)
  393. return NULL;
  394. svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
  395. INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
  396. INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
  397. INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
  398. INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
  399. INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
  400. init_waitqueue_head(&cma_xprt->sc_send_wait);
  401. spin_lock_init(&cma_xprt->sc_lock);
  402. spin_lock_init(&cma_xprt->sc_rq_dto_lock);
  403. spin_lock_init(&cma_xprt->sc_frmr_q_lock);
  404. cma_xprt->sc_ord = svcrdma_ord;
  405. cma_xprt->sc_max_req_size = svcrdma_max_req_size;
  406. cma_xprt->sc_max_requests = svcrdma_max_requests;
  407. cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
  408. atomic_set(&cma_xprt->sc_sq_count, 0);
  409. atomic_set(&cma_xprt->sc_ctxt_used, 0);
  410. if (listener)
  411. set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
  412. return cma_xprt;
  413. }
  414. struct page *svc_rdma_get_page(void)
  415. {
  416. struct page *page;
  417. while ((page = alloc_page(GFP_KERNEL)) == NULL) {
  418. /* If we can't get memory, wait a bit and try again */
  419. printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
  420. "jiffies.\n");
  421. schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
  422. }
  423. return page;
  424. }
  425. int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
  426. {
  427. struct ib_recv_wr recv_wr, *bad_recv_wr;
  428. struct svc_rdma_op_ctxt *ctxt;
  429. struct page *page;
  430. dma_addr_t pa;
  431. int sge_no;
  432. int buflen;
  433. int ret;
  434. ctxt = svc_rdma_get_context(xprt);
  435. buflen = 0;
  436. ctxt->direction = DMA_FROM_DEVICE;
  437. for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
  438. BUG_ON(sge_no >= xprt->sc_max_sge);
  439. page = svc_rdma_get_page();
  440. ctxt->pages[sge_no] = page;
  441. pa = ib_dma_map_page(xprt->sc_cm_id->device,
  442. page, 0, PAGE_SIZE,
  443. DMA_FROM_DEVICE);
  444. if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
  445. goto err_put_ctxt;
  446. atomic_inc(&xprt->sc_dma_used);
  447. ctxt->sge[sge_no].addr = pa;
  448. ctxt->sge[sge_no].length = PAGE_SIZE;
  449. ctxt->sge[sge_no].lkey = xprt->sc_dma_lkey;
  450. buflen += PAGE_SIZE;
  451. }
  452. ctxt->count = sge_no;
  453. recv_wr.next = NULL;
  454. recv_wr.sg_list = &ctxt->sge[0];
  455. recv_wr.num_sge = ctxt->count;
  456. recv_wr.wr_id = (u64)(unsigned long)ctxt;
  457. svc_xprt_get(&xprt->sc_xprt);
  458. ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
  459. if (ret) {
  460. svc_rdma_unmap_dma(ctxt);
  461. svc_rdma_put_context(ctxt, 1);
  462. svc_xprt_put(&xprt->sc_xprt);
  463. }
  464. return ret;
  465. err_put_ctxt:
  466. svc_rdma_put_context(ctxt, 1);
  467. return -ENOMEM;
  468. }
  469. /*
  470. * This function handles the CONNECT_REQUEST event on a listening
  471. * endpoint. It is passed the cma_id for the _new_ connection. The context in
  472. * this cma_id is inherited from the listening cma_id and is the svc_xprt
  473. * structure for the listening endpoint.
  474. *
  475. * This function creates a new xprt for the new connection and enqueues it on
  476. * the accept queue for the listent xprt. When the listen thread is kicked, it
  477. * will call the recvfrom method on the listen xprt which will accept the new
  478. * connection.
  479. */
  480. static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
  481. {
  482. struct svcxprt_rdma *listen_xprt = new_cma_id->context;
  483. struct svcxprt_rdma *newxprt;
  484. struct sockaddr *sa;
  485. /* Create a new transport */
  486. newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
  487. if (!newxprt) {
  488. dprintk("svcrdma: failed to create new transport\n");
  489. return;
  490. }
  491. newxprt->sc_cm_id = new_cma_id;
  492. new_cma_id->context = newxprt;
  493. dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
  494. newxprt, newxprt->sc_cm_id, listen_xprt);
  495. /* Save client advertised inbound read limit for use later in accept. */
  496. newxprt->sc_ord = client_ird;
  497. /* Set the local and remote addresses in the transport */
  498. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
  499. svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  500. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
  501. svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  502. /*
  503. * Enqueue the new transport on the accept queue of the listening
  504. * transport
  505. */
  506. spin_lock_bh(&listen_xprt->sc_lock);
  507. list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
  508. spin_unlock_bh(&listen_xprt->sc_lock);
  509. /*
  510. * Can't use svc_xprt_received here because we are not on a
  511. * rqstp thread
  512. */
  513. set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
  514. svc_xprt_enqueue(&listen_xprt->sc_xprt);
  515. }
  516. /*
  517. * Handles events generated on the listening endpoint. These events will be
  518. * either be incoming connect requests or adapter removal events.
  519. */
  520. static int rdma_listen_handler(struct rdma_cm_id *cma_id,
  521. struct rdma_cm_event *event)
  522. {
  523. struct svcxprt_rdma *xprt = cma_id->context;
  524. int ret = 0;
  525. switch (event->event) {
  526. case RDMA_CM_EVENT_CONNECT_REQUEST:
  527. dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
  528. "event=%d\n", cma_id, cma_id->context, event->event);
  529. handle_connect_req(cma_id,
  530. event->param.conn.initiator_depth);
  531. break;
  532. case RDMA_CM_EVENT_ESTABLISHED:
  533. /* Accept complete */
  534. dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
  535. "cm_id=%p\n", xprt, cma_id);
  536. break;
  537. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  538. dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
  539. xprt, cma_id);
  540. if (xprt)
  541. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  542. break;
  543. default:
  544. dprintk("svcrdma: Unexpected event on listening endpoint %p, "
  545. "event=%d\n", cma_id, event->event);
  546. break;
  547. }
  548. return ret;
  549. }
  550. static int rdma_cma_handler(struct rdma_cm_id *cma_id,
  551. struct rdma_cm_event *event)
  552. {
  553. struct svc_xprt *xprt = cma_id->context;
  554. struct svcxprt_rdma *rdma =
  555. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  556. switch (event->event) {
  557. case RDMA_CM_EVENT_ESTABLISHED:
  558. /* Accept complete */
  559. svc_xprt_get(xprt);
  560. dprintk("svcrdma: Connection completed on DTO xprt=%p, "
  561. "cm_id=%p\n", xprt, cma_id);
  562. clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
  563. svc_xprt_enqueue(xprt);
  564. break;
  565. case RDMA_CM_EVENT_DISCONNECTED:
  566. dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
  567. xprt, cma_id);
  568. if (xprt) {
  569. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  570. svc_xprt_enqueue(xprt);
  571. svc_xprt_put(xprt);
  572. }
  573. break;
  574. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  575. dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
  576. "event=%d\n", cma_id, xprt, event->event);
  577. if (xprt) {
  578. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  579. svc_xprt_enqueue(xprt);
  580. }
  581. break;
  582. default:
  583. dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
  584. "event=%d\n", cma_id, event->event);
  585. break;
  586. }
  587. return 0;
  588. }
  589. /*
  590. * Create a listening RDMA service endpoint.
  591. */
  592. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  593. struct sockaddr *sa, int salen,
  594. int flags)
  595. {
  596. struct rdma_cm_id *listen_id;
  597. struct svcxprt_rdma *cma_xprt;
  598. struct svc_xprt *xprt;
  599. int ret;
  600. dprintk("svcrdma: Creating RDMA socket\n");
  601. cma_xprt = rdma_create_xprt(serv, 1);
  602. if (!cma_xprt)
  603. return ERR_PTR(-ENOMEM);
  604. xprt = &cma_xprt->sc_xprt;
  605. listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
  606. if (IS_ERR(listen_id)) {
  607. ret = PTR_ERR(listen_id);
  608. dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
  609. goto err0;
  610. }
  611. ret = rdma_bind_addr(listen_id, sa);
  612. if (ret) {
  613. dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
  614. goto err1;
  615. }
  616. cma_xprt->sc_cm_id = listen_id;
  617. ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
  618. if (ret) {
  619. dprintk("svcrdma: rdma_listen failed = %d\n", ret);
  620. goto err1;
  621. }
  622. /*
  623. * We need to use the address from the cm_id in case the
  624. * caller specified 0 for the port number.
  625. */
  626. sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
  627. svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
  628. return &cma_xprt->sc_xprt;
  629. err1:
  630. rdma_destroy_id(listen_id);
  631. err0:
  632. kfree(cma_xprt);
  633. return ERR_PTR(ret);
  634. }
  635. static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
  636. {
  637. struct ib_mr *mr;
  638. struct ib_fast_reg_page_list *pl;
  639. struct svc_rdma_fastreg_mr *frmr;
  640. frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
  641. if (!frmr)
  642. goto err;
  643. mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
  644. if (!mr)
  645. goto err_free_frmr;
  646. pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
  647. RPCSVC_MAXPAGES);
  648. if (!pl)
  649. goto err_free_mr;
  650. frmr->mr = mr;
  651. frmr->page_list = pl;
  652. INIT_LIST_HEAD(&frmr->frmr_list);
  653. return frmr;
  654. err_free_mr:
  655. ib_dereg_mr(mr);
  656. err_free_frmr:
  657. kfree(frmr);
  658. err:
  659. return ERR_PTR(-ENOMEM);
  660. }
  661. static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
  662. {
  663. struct svc_rdma_fastreg_mr *frmr;
  664. while (!list_empty(&xprt->sc_frmr_q)) {
  665. frmr = list_entry(xprt->sc_frmr_q.next,
  666. struct svc_rdma_fastreg_mr, frmr_list);
  667. list_del_init(&frmr->frmr_list);
  668. ib_dereg_mr(frmr->mr);
  669. ib_free_fast_reg_page_list(frmr->page_list);
  670. kfree(frmr);
  671. }
  672. }
  673. struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
  674. {
  675. struct svc_rdma_fastreg_mr *frmr = NULL;
  676. spin_lock_bh(&rdma->sc_frmr_q_lock);
  677. if (!list_empty(&rdma->sc_frmr_q)) {
  678. frmr = list_entry(rdma->sc_frmr_q.next,
  679. struct svc_rdma_fastreg_mr, frmr_list);
  680. list_del_init(&frmr->frmr_list);
  681. frmr->map_len = 0;
  682. frmr->page_list_len = 0;
  683. }
  684. spin_unlock_bh(&rdma->sc_frmr_q_lock);
  685. if (frmr)
  686. return frmr;
  687. return rdma_alloc_frmr(rdma);
  688. }
  689. static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
  690. struct svc_rdma_fastreg_mr *frmr)
  691. {
  692. int page_no;
  693. for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
  694. dma_addr_t addr = frmr->page_list->page_list[page_no];
  695. if (ib_dma_mapping_error(frmr->mr->device, addr))
  696. continue;
  697. atomic_dec(&xprt->sc_dma_used);
  698. ib_dma_unmap_single(frmr->mr->device, addr, PAGE_SIZE,
  699. frmr->direction);
  700. }
  701. }
  702. void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
  703. struct svc_rdma_fastreg_mr *frmr)
  704. {
  705. if (frmr) {
  706. frmr_unmap_dma(rdma, frmr);
  707. spin_lock_bh(&rdma->sc_frmr_q_lock);
  708. BUG_ON(!list_empty(&frmr->frmr_list));
  709. list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
  710. spin_unlock_bh(&rdma->sc_frmr_q_lock);
  711. }
  712. }
  713. /*
  714. * This is the xpo_recvfrom function for listening endpoints. Its
  715. * purpose is to accept incoming connections. The CMA callback handler
  716. * has already created a new transport and attached it to the new CMA
  717. * ID.
  718. *
  719. * There is a queue of pending connections hung on the listening
  720. * transport. This queue contains the new svc_xprt structure. This
  721. * function takes svc_xprt structures off the accept_q and completes
  722. * the connection.
  723. */
  724. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
  725. {
  726. struct svcxprt_rdma *listen_rdma;
  727. struct svcxprt_rdma *newxprt = NULL;
  728. struct rdma_conn_param conn_param;
  729. struct ib_qp_init_attr qp_attr;
  730. struct ib_device_attr devattr;
  731. int uninitialized_var(dma_mr_acc);
  732. int need_dma_mr;
  733. int ret;
  734. int i;
  735. listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
  736. clear_bit(XPT_CONN, &xprt->xpt_flags);
  737. /* Get the next entry off the accept list */
  738. spin_lock_bh(&listen_rdma->sc_lock);
  739. if (!list_empty(&listen_rdma->sc_accept_q)) {
  740. newxprt = list_entry(listen_rdma->sc_accept_q.next,
  741. struct svcxprt_rdma, sc_accept_q);
  742. list_del_init(&newxprt->sc_accept_q);
  743. }
  744. if (!list_empty(&listen_rdma->sc_accept_q))
  745. set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
  746. spin_unlock_bh(&listen_rdma->sc_lock);
  747. if (!newxprt)
  748. return NULL;
  749. dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
  750. newxprt, newxprt->sc_cm_id);
  751. ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
  752. if (ret) {
  753. dprintk("svcrdma: could not query device attributes on "
  754. "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
  755. goto errout;
  756. }
  757. /* Qualify the transport resource defaults with the
  758. * capabilities of this particular device */
  759. newxprt->sc_max_sge = min((size_t)devattr.max_sge,
  760. (size_t)RPCSVC_MAXPAGES);
  761. newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
  762. (size_t)svcrdma_max_requests);
  763. newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
  764. /*
  765. * Limit ORD based on client limit, local device limit, and
  766. * configured svcrdma limit.
  767. */
  768. newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
  769. newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
  770. newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
  771. if (IS_ERR(newxprt->sc_pd)) {
  772. dprintk("svcrdma: error creating PD for connect request\n");
  773. goto errout;
  774. }
  775. newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  776. sq_comp_handler,
  777. cq_event_handler,
  778. newxprt,
  779. newxprt->sc_sq_depth,
  780. 0);
  781. if (IS_ERR(newxprt->sc_sq_cq)) {
  782. dprintk("svcrdma: error creating SQ CQ for connect request\n");
  783. goto errout;
  784. }
  785. newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  786. rq_comp_handler,
  787. cq_event_handler,
  788. newxprt,
  789. newxprt->sc_max_requests,
  790. 0);
  791. if (IS_ERR(newxprt->sc_rq_cq)) {
  792. dprintk("svcrdma: error creating RQ CQ for connect request\n");
  793. goto errout;
  794. }
  795. memset(&qp_attr, 0, sizeof qp_attr);
  796. qp_attr.event_handler = qp_event_handler;
  797. qp_attr.qp_context = &newxprt->sc_xprt;
  798. qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
  799. qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
  800. qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
  801. qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
  802. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  803. qp_attr.qp_type = IB_QPT_RC;
  804. qp_attr.send_cq = newxprt->sc_sq_cq;
  805. qp_attr.recv_cq = newxprt->sc_rq_cq;
  806. dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
  807. " cm_id->device=%p, sc_pd->device=%p\n"
  808. " cap.max_send_wr = %d\n"
  809. " cap.max_recv_wr = %d\n"
  810. " cap.max_send_sge = %d\n"
  811. " cap.max_recv_sge = %d\n",
  812. newxprt->sc_cm_id, newxprt->sc_pd,
  813. newxprt->sc_cm_id->device, newxprt->sc_pd->device,
  814. qp_attr.cap.max_send_wr,
  815. qp_attr.cap.max_recv_wr,
  816. qp_attr.cap.max_send_sge,
  817. qp_attr.cap.max_recv_sge);
  818. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
  819. if (ret) {
  820. /*
  821. * XXX: This is a hack. We need a xx_request_qp interface
  822. * that will adjust the qp_attr's with a best-effort
  823. * number
  824. */
  825. qp_attr.cap.max_send_sge -= 2;
  826. qp_attr.cap.max_recv_sge -= 2;
  827. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
  828. &qp_attr);
  829. if (ret) {
  830. dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
  831. goto errout;
  832. }
  833. newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
  834. newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
  835. newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
  836. newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
  837. }
  838. newxprt->sc_qp = newxprt->sc_cm_id->qp;
  839. /*
  840. * Use the most secure set of MR resources based on the
  841. * transport type and available memory management features in
  842. * the device. Here's the table implemented below:
  843. *
  844. * Fast Global DMA Remote WR
  845. * Reg LKEY MR Access
  846. * Sup'd Sup'd Needed Needed
  847. *
  848. * IWARP N N Y Y
  849. * N Y Y Y
  850. * Y N Y N
  851. * Y Y N -
  852. *
  853. * IB N N Y N
  854. * N Y N -
  855. * Y N Y N
  856. * Y Y N -
  857. *
  858. * NB: iWARP requires remote write access for the data sink
  859. * of an RDMA_READ. IB does not.
  860. */
  861. if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
  862. newxprt->sc_frmr_pg_list_len =
  863. devattr.max_fast_reg_page_list_len;
  864. newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
  865. }
  866. /*
  867. * Determine if a DMA MR is required and if so, what privs are required
  868. */
  869. switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
  870. case RDMA_TRANSPORT_IWARP:
  871. newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
  872. if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
  873. need_dma_mr = 1;
  874. dma_mr_acc =
  875. (IB_ACCESS_LOCAL_WRITE |
  876. IB_ACCESS_REMOTE_WRITE);
  877. } else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
  878. need_dma_mr = 1;
  879. dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
  880. } else
  881. need_dma_mr = 0;
  882. break;
  883. case RDMA_TRANSPORT_IB:
  884. if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
  885. need_dma_mr = 1;
  886. dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
  887. } else
  888. need_dma_mr = 0;
  889. break;
  890. default:
  891. goto errout;
  892. }
  893. /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
  894. if (need_dma_mr) {
  895. /* Register all of physical memory */
  896. newxprt->sc_phys_mr =
  897. ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
  898. if (IS_ERR(newxprt->sc_phys_mr)) {
  899. dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
  900. ret);
  901. goto errout;
  902. }
  903. newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
  904. } else
  905. newxprt->sc_dma_lkey =
  906. newxprt->sc_cm_id->device->local_dma_lkey;
  907. /* Post receive buffers */
  908. for (i = 0; i < newxprt->sc_max_requests; i++) {
  909. ret = svc_rdma_post_recv(newxprt);
  910. if (ret) {
  911. dprintk("svcrdma: failure posting receive buffers\n");
  912. goto errout;
  913. }
  914. }
  915. /* Swap out the handler */
  916. newxprt->sc_cm_id->event_handler = rdma_cma_handler;
  917. /*
  918. * Arm the CQs for the SQ and RQ before accepting so we can't
  919. * miss the first message
  920. */
  921. ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  922. ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
  923. /* Accept Connection */
  924. set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
  925. memset(&conn_param, 0, sizeof conn_param);
  926. conn_param.responder_resources = 0;
  927. conn_param.initiator_depth = newxprt->sc_ord;
  928. ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
  929. if (ret) {
  930. dprintk("svcrdma: failed to accept new connection, ret=%d\n",
  931. ret);
  932. goto errout;
  933. }
  934. dprintk("svcrdma: new connection %p accepted with the following "
  935. "attributes:\n"
  936. " local_ip : %pI4\n"
  937. " local_port : %d\n"
  938. " remote_ip : %pI4\n"
  939. " remote_port : %d\n"
  940. " max_sge : %d\n"
  941. " sq_depth : %d\n"
  942. " max_requests : %d\n"
  943. " ord : %d\n",
  944. newxprt,
  945. &((struct sockaddr_in *)&newxprt->sc_cm_id->
  946. route.addr.src_addr)->sin_addr.s_addr,
  947. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  948. route.addr.src_addr)->sin_port),
  949. &((struct sockaddr_in *)&newxprt->sc_cm_id->
  950. route.addr.dst_addr)->sin_addr.s_addr,
  951. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  952. route.addr.dst_addr)->sin_port),
  953. newxprt->sc_max_sge,
  954. newxprt->sc_sq_depth,
  955. newxprt->sc_max_requests,
  956. newxprt->sc_ord);
  957. return &newxprt->sc_xprt;
  958. errout:
  959. dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
  960. /* Take a reference in case the DTO handler runs */
  961. svc_xprt_get(&newxprt->sc_xprt);
  962. if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
  963. ib_destroy_qp(newxprt->sc_qp);
  964. rdma_destroy_id(newxprt->sc_cm_id);
  965. /* This call to put will destroy the transport */
  966. svc_xprt_put(&newxprt->sc_xprt);
  967. return NULL;
  968. }
  969. static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
  970. {
  971. }
  972. /*
  973. * When connected, an svc_xprt has at least two references:
  974. *
  975. * - A reference held by the cm_id between the ESTABLISHED and
  976. * DISCONNECTED events. If the remote peer disconnected first, this
  977. * reference could be gone.
  978. *
  979. * - A reference held by the svc_recv code that called this function
  980. * as part of close processing.
  981. *
  982. * At a minimum one references should still be held.
  983. */
  984. static void svc_rdma_detach(struct svc_xprt *xprt)
  985. {
  986. struct svcxprt_rdma *rdma =
  987. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  988. dprintk("svc: svc_rdma_detach(%p)\n", xprt);
  989. /* Disconnect and flush posted WQE */
  990. rdma_disconnect(rdma->sc_cm_id);
  991. }
  992. static void __svc_rdma_free(struct work_struct *work)
  993. {
  994. struct svcxprt_rdma *rdma =
  995. container_of(work, struct svcxprt_rdma, sc_work);
  996. dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
  997. /* We should only be called from kref_put */
  998. BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
  999. /*
  1000. * Destroy queued, but not processed read completions. Note
  1001. * that this cleanup has to be done before destroying the
  1002. * cm_id because the device ptr is needed to unmap the dma in
  1003. * svc_rdma_put_context.
  1004. */
  1005. while (!list_empty(&rdma->sc_read_complete_q)) {
  1006. struct svc_rdma_op_ctxt *ctxt;
  1007. ctxt = list_entry(rdma->sc_read_complete_q.next,
  1008. struct svc_rdma_op_ctxt,
  1009. dto_q);
  1010. list_del_init(&ctxt->dto_q);
  1011. svc_rdma_put_context(ctxt, 1);
  1012. }
  1013. /* Destroy queued, but not processed recv completions */
  1014. while (!list_empty(&rdma->sc_rq_dto_q)) {
  1015. struct svc_rdma_op_ctxt *ctxt;
  1016. ctxt = list_entry(rdma->sc_rq_dto_q.next,
  1017. struct svc_rdma_op_ctxt,
  1018. dto_q);
  1019. list_del_init(&ctxt->dto_q);
  1020. svc_rdma_put_context(ctxt, 1);
  1021. }
  1022. /* Warn if we leaked a resource or under-referenced */
  1023. WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
  1024. WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
  1025. /* De-allocate fastreg mr */
  1026. rdma_dealloc_frmr_q(rdma);
  1027. /* Destroy the QP if present (not a listener) */
  1028. if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
  1029. ib_destroy_qp(rdma->sc_qp);
  1030. if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
  1031. ib_destroy_cq(rdma->sc_sq_cq);
  1032. if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
  1033. ib_destroy_cq(rdma->sc_rq_cq);
  1034. if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
  1035. ib_dereg_mr(rdma->sc_phys_mr);
  1036. if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
  1037. ib_dealloc_pd(rdma->sc_pd);
  1038. /* Destroy the CM ID */
  1039. rdma_destroy_id(rdma->sc_cm_id);
  1040. kfree(rdma);
  1041. }
  1042. static void svc_rdma_free(struct svc_xprt *xprt)
  1043. {
  1044. struct svcxprt_rdma *rdma =
  1045. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  1046. INIT_WORK(&rdma->sc_work, __svc_rdma_free);
  1047. schedule_work(&rdma->sc_work);
  1048. }
  1049. static int svc_rdma_has_wspace(struct svc_xprt *xprt)
  1050. {
  1051. struct svcxprt_rdma *rdma =
  1052. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  1053. /*
  1054. * If there are fewer SQ WR available than required to send a
  1055. * simple response, return false.
  1056. */
  1057. if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
  1058. return 0;
  1059. /*
  1060. * ...or there are already waiters on the SQ,
  1061. * return false.
  1062. */
  1063. if (waitqueue_active(&rdma->sc_send_wait))
  1064. return 0;
  1065. /* Otherwise return true. */
  1066. return 1;
  1067. }
  1068. /*
  1069. * Attempt to register the kvec representing the RPC memory with the
  1070. * device.
  1071. *
  1072. * Returns:
  1073. * NULL : The device does not support fastreg or there were no more
  1074. * fastreg mr.
  1075. * frmr : The kvec register request was successfully posted.
  1076. * <0 : An error was encountered attempting to register the kvec.
  1077. */
  1078. int svc_rdma_fastreg(struct svcxprt_rdma *xprt,
  1079. struct svc_rdma_fastreg_mr *frmr)
  1080. {
  1081. struct ib_send_wr fastreg_wr;
  1082. u8 key;
  1083. /* Bump the key */
  1084. key = (u8)(frmr->mr->lkey & 0x000000FF);
  1085. ib_update_fast_reg_key(frmr->mr, ++key);
  1086. /* Prepare FASTREG WR */
  1087. memset(&fastreg_wr, 0, sizeof fastreg_wr);
  1088. fastreg_wr.opcode = IB_WR_FAST_REG_MR;
  1089. fastreg_wr.send_flags = IB_SEND_SIGNALED;
  1090. fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
  1091. fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
  1092. fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
  1093. fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
  1094. fastreg_wr.wr.fast_reg.length = frmr->map_len;
  1095. fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
  1096. fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
  1097. return svc_rdma_send(xprt, &fastreg_wr);
  1098. }
  1099. int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
  1100. {
  1101. struct ib_send_wr *bad_wr, *n_wr;
  1102. int wr_count;
  1103. int i;
  1104. int ret;
  1105. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  1106. return -ENOTCONN;
  1107. BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
  1108. wr_count = 1;
  1109. for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
  1110. wr_count++;
  1111. /* If the SQ is full, wait until an SQ entry is available */
  1112. while (1) {
  1113. spin_lock_bh(&xprt->sc_lock);
  1114. if (xprt->sc_sq_depth < atomic_read(&xprt->sc_sq_count) + wr_count) {
  1115. spin_unlock_bh(&xprt->sc_lock);
  1116. atomic_inc(&rdma_stat_sq_starve);
  1117. /* See if we can opportunistically reap SQ WR to make room */
  1118. sq_cq_reap(xprt);
  1119. /* Wait until SQ WR available if SQ still full */
  1120. wait_event(xprt->sc_send_wait,
  1121. atomic_read(&xprt->sc_sq_count) <
  1122. xprt->sc_sq_depth);
  1123. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  1124. return 0;
  1125. continue;
  1126. }
  1127. /* Take a transport ref for each WR posted */
  1128. for (i = 0; i < wr_count; i++)
  1129. svc_xprt_get(&xprt->sc_xprt);
  1130. /* Bump used SQ WR count and post */
  1131. atomic_add(wr_count, &xprt->sc_sq_count);
  1132. ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
  1133. if (ret) {
  1134. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  1135. atomic_sub(wr_count, &xprt->sc_sq_count);
  1136. for (i = 0; i < wr_count; i ++)
  1137. svc_xprt_put(&xprt->sc_xprt);
  1138. dprintk("svcrdma: failed to post SQ WR rc=%d, "
  1139. "sc_sq_count=%d, sc_sq_depth=%d\n",
  1140. ret, atomic_read(&xprt->sc_sq_count),
  1141. xprt->sc_sq_depth);
  1142. }
  1143. spin_unlock_bh(&xprt->sc_lock);
  1144. if (ret)
  1145. wake_up(&xprt->sc_send_wait);
  1146. break;
  1147. }
  1148. return ret;
  1149. }
  1150. void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
  1151. enum rpcrdma_errcode err)
  1152. {
  1153. struct ib_send_wr err_wr;
  1154. struct ib_sge sge;
  1155. struct page *p;
  1156. struct svc_rdma_op_ctxt *ctxt;
  1157. u32 *va;
  1158. int length;
  1159. int ret;
  1160. p = svc_rdma_get_page();
  1161. va = page_address(p);
  1162. /* XDR encode error */
  1163. length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
  1164. /* Prepare SGE for local address */
  1165. sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
  1166. p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  1167. if (ib_dma_mapping_error(xprt->sc_cm_id->device, sge.addr)) {
  1168. put_page(p);
  1169. return;
  1170. }
  1171. atomic_inc(&xprt->sc_dma_used);
  1172. sge.lkey = xprt->sc_dma_lkey;
  1173. sge.length = length;
  1174. ctxt = svc_rdma_get_context(xprt);
  1175. ctxt->count = 1;
  1176. ctxt->pages[0] = p;
  1177. /* Prepare SEND WR */
  1178. memset(&err_wr, 0, sizeof err_wr);
  1179. ctxt->wr_op = IB_WR_SEND;
  1180. err_wr.wr_id = (unsigned long)ctxt;
  1181. err_wr.sg_list = &sge;
  1182. err_wr.num_sge = 1;
  1183. err_wr.opcode = IB_WR_SEND;
  1184. err_wr.send_flags = IB_SEND_SIGNALED;
  1185. /* Post It */
  1186. ret = svc_rdma_send(xprt, &err_wr);
  1187. if (ret) {
  1188. dprintk("svcrdma: Error %d posting send for protocol error\n",
  1189. ret);
  1190. ib_dma_unmap_page(xprt->sc_cm_id->device,
  1191. sge.addr, PAGE_SIZE,
  1192. DMA_FROM_DEVICE);
  1193. svc_rdma_put_context(ctxt, 1);
  1194. }
  1195. }