svc_rdma_transport.c 38 KB

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