svc_rdma_transport.c 37 KB

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