svc_rdma_transport.c 36 KB

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