svc_rdma_transport.c 33 KB

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