svc_rdma_transport.c 38 KB

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