svc_rdma_transport.c 38 KB

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