svc_rdma_transport.c 34 KB

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