svc_rdma_transport.c 38 KB

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