svc_rdma_transport.c 38 KB

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