svc_rdma_transport.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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. struct sockaddr *sa;
  505. /* Create a new transport */
  506. newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
  507. if (!newxprt) {
  508. dprintk("svcrdma: failed to create new transport\n");
  509. return;
  510. }
  511. newxprt->sc_cm_id = new_cma_id;
  512. new_cma_id->context = newxprt;
  513. dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
  514. newxprt, newxprt->sc_cm_id, listen_xprt);
  515. /* Set the local and remote addresses in the transport */
  516. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
  517. svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  518. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
  519. svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  520. /*
  521. * Enqueue the new transport on the accept queue of the listening
  522. * transport
  523. */
  524. spin_lock_bh(&listen_xprt->sc_lock);
  525. list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
  526. spin_unlock_bh(&listen_xprt->sc_lock);
  527. /*
  528. * Can't use svc_xprt_received here because we are not on a
  529. * rqstp thread
  530. */
  531. set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
  532. svc_xprt_enqueue(&listen_xprt->sc_xprt);
  533. }
  534. /*
  535. * Handles events generated on the listening endpoint. These events will be
  536. * either be incoming connect requests or adapter removal events.
  537. */
  538. static int rdma_listen_handler(struct rdma_cm_id *cma_id,
  539. struct rdma_cm_event *event)
  540. {
  541. struct svcxprt_rdma *xprt = cma_id->context;
  542. int ret = 0;
  543. switch (event->event) {
  544. case RDMA_CM_EVENT_CONNECT_REQUEST:
  545. dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
  546. "event=%d\n", cma_id, cma_id->context, event->event);
  547. handle_connect_req(cma_id);
  548. break;
  549. case RDMA_CM_EVENT_ESTABLISHED:
  550. /* Accept complete */
  551. dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
  552. "cm_id=%p\n", xprt, cma_id);
  553. break;
  554. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  555. dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
  556. xprt, cma_id);
  557. if (xprt)
  558. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  559. break;
  560. default:
  561. dprintk("svcrdma: Unexpected event on listening endpoint %p, "
  562. "event=%d\n", cma_id, event->event);
  563. break;
  564. }
  565. return ret;
  566. }
  567. static int rdma_cma_handler(struct rdma_cm_id *cma_id,
  568. struct rdma_cm_event *event)
  569. {
  570. struct svc_xprt *xprt = cma_id->context;
  571. struct svcxprt_rdma *rdma =
  572. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  573. switch (event->event) {
  574. case RDMA_CM_EVENT_ESTABLISHED:
  575. /* Accept complete */
  576. svc_xprt_get(xprt);
  577. dprintk("svcrdma: Connection completed on DTO xprt=%p, "
  578. "cm_id=%p\n", xprt, cma_id);
  579. clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
  580. svc_xprt_enqueue(xprt);
  581. break;
  582. case RDMA_CM_EVENT_DISCONNECTED:
  583. dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
  584. xprt, cma_id);
  585. if (xprt) {
  586. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  587. svc_xprt_enqueue(xprt);
  588. svc_xprt_put(xprt);
  589. }
  590. break;
  591. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  592. dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
  593. "event=%d\n", cma_id, xprt, event->event);
  594. if (xprt) {
  595. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  596. svc_xprt_enqueue(xprt);
  597. }
  598. break;
  599. default:
  600. dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
  601. "event=%d\n", cma_id, event->event);
  602. break;
  603. }
  604. return 0;
  605. }
  606. /*
  607. * Create a listening RDMA service endpoint.
  608. */
  609. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  610. struct sockaddr *sa, int salen,
  611. int flags)
  612. {
  613. struct rdma_cm_id *listen_id;
  614. struct svcxprt_rdma *cma_xprt;
  615. struct svc_xprt *xprt;
  616. int ret;
  617. dprintk("svcrdma: Creating RDMA socket\n");
  618. cma_xprt = rdma_create_xprt(serv, 1);
  619. if (!cma_xprt)
  620. return ERR_PTR(-ENOMEM);
  621. xprt = &cma_xprt->sc_xprt;
  622. listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
  623. if (IS_ERR(listen_id)) {
  624. ret = PTR_ERR(listen_id);
  625. dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
  626. goto err0;
  627. }
  628. ret = rdma_bind_addr(listen_id, sa);
  629. if (ret) {
  630. dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
  631. goto err1;
  632. }
  633. cma_xprt->sc_cm_id = listen_id;
  634. ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
  635. if (ret) {
  636. dprintk("svcrdma: rdma_listen failed = %d\n", ret);
  637. goto err1;
  638. }
  639. /*
  640. * We need to use the address from the cm_id in case the
  641. * caller specified 0 for the port number.
  642. */
  643. sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
  644. svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
  645. return &cma_xprt->sc_xprt;
  646. err1:
  647. rdma_destroy_id(listen_id);
  648. err0:
  649. kfree(cma_xprt);
  650. return ERR_PTR(ret);
  651. }
  652. /*
  653. * This is the xpo_recvfrom function for listening endpoints. Its
  654. * purpose is to accept incoming connections. The CMA callback handler
  655. * has already created a new transport and attached it to the new CMA
  656. * ID.
  657. *
  658. * There is a queue of pending connections hung on the listening
  659. * transport. This queue contains the new svc_xprt structure. This
  660. * function takes svc_xprt structures off the accept_q and completes
  661. * the connection.
  662. */
  663. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
  664. {
  665. struct svcxprt_rdma *listen_rdma;
  666. struct svcxprt_rdma *newxprt = NULL;
  667. struct rdma_conn_param conn_param;
  668. struct ib_qp_init_attr qp_attr;
  669. struct ib_device_attr devattr;
  670. int ret;
  671. int i;
  672. listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
  673. clear_bit(XPT_CONN, &xprt->xpt_flags);
  674. /* Get the next entry off the accept list */
  675. spin_lock_bh(&listen_rdma->sc_lock);
  676. if (!list_empty(&listen_rdma->sc_accept_q)) {
  677. newxprt = list_entry(listen_rdma->sc_accept_q.next,
  678. struct svcxprt_rdma, sc_accept_q);
  679. list_del_init(&newxprt->sc_accept_q);
  680. }
  681. if (!list_empty(&listen_rdma->sc_accept_q))
  682. set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
  683. spin_unlock_bh(&listen_rdma->sc_lock);
  684. if (!newxprt)
  685. return NULL;
  686. dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
  687. newxprt, newxprt->sc_cm_id);
  688. ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
  689. if (ret) {
  690. dprintk("svcrdma: could not query device attributes on "
  691. "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
  692. goto errout;
  693. }
  694. /* Qualify the transport resource defaults with the
  695. * capabilities of this particular device */
  696. newxprt->sc_max_sge = min((size_t)devattr.max_sge,
  697. (size_t)RPCSVC_MAXPAGES);
  698. newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
  699. (size_t)svcrdma_max_requests);
  700. newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
  701. newxprt->sc_ord = min((size_t)devattr.max_qp_rd_atom,
  702. (size_t)svcrdma_ord);
  703. newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
  704. if (IS_ERR(newxprt->sc_pd)) {
  705. dprintk("svcrdma: error creating PD for connect request\n");
  706. goto errout;
  707. }
  708. newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  709. sq_comp_handler,
  710. cq_event_handler,
  711. newxprt,
  712. newxprt->sc_sq_depth,
  713. 0);
  714. if (IS_ERR(newxprt->sc_sq_cq)) {
  715. dprintk("svcrdma: error creating SQ CQ for connect request\n");
  716. goto errout;
  717. }
  718. newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  719. rq_comp_handler,
  720. cq_event_handler,
  721. newxprt,
  722. newxprt->sc_max_requests,
  723. 0);
  724. if (IS_ERR(newxprt->sc_rq_cq)) {
  725. dprintk("svcrdma: error creating RQ CQ for connect request\n");
  726. goto errout;
  727. }
  728. memset(&qp_attr, 0, sizeof qp_attr);
  729. qp_attr.event_handler = qp_event_handler;
  730. qp_attr.qp_context = &newxprt->sc_xprt;
  731. qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
  732. qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
  733. qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
  734. qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
  735. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  736. qp_attr.qp_type = IB_QPT_RC;
  737. qp_attr.send_cq = newxprt->sc_sq_cq;
  738. qp_attr.recv_cq = newxprt->sc_rq_cq;
  739. dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
  740. " cm_id->device=%p, sc_pd->device=%p\n"
  741. " cap.max_send_wr = %d\n"
  742. " cap.max_recv_wr = %d\n"
  743. " cap.max_send_sge = %d\n"
  744. " cap.max_recv_sge = %d\n",
  745. newxprt->sc_cm_id, newxprt->sc_pd,
  746. newxprt->sc_cm_id->device, newxprt->sc_pd->device,
  747. qp_attr.cap.max_send_wr,
  748. qp_attr.cap.max_recv_wr,
  749. qp_attr.cap.max_send_sge,
  750. qp_attr.cap.max_recv_sge);
  751. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
  752. if (ret) {
  753. /*
  754. * XXX: This is a hack. We need a xx_request_qp interface
  755. * that will adjust the qp_attr's with a best-effort
  756. * number
  757. */
  758. qp_attr.cap.max_send_sge -= 2;
  759. qp_attr.cap.max_recv_sge -= 2;
  760. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
  761. &qp_attr);
  762. if (ret) {
  763. dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
  764. goto errout;
  765. }
  766. newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
  767. newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
  768. newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
  769. newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
  770. }
  771. newxprt->sc_qp = newxprt->sc_cm_id->qp;
  772. /* Register all of physical memory */
  773. newxprt->sc_phys_mr = ib_get_dma_mr(newxprt->sc_pd,
  774. IB_ACCESS_LOCAL_WRITE |
  775. IB_ACCESS_REMOTE_WRITE);
  776. if (IS_ERR(newxprt->sc_phys_mr)) {
  777. dprintk("svcrdma: Failed to create DMA MR ret=%d\n", ret);
  778. goto errout;
  779. }
  780. /* Post receive buffers */
  781. for (i = 0; i < newxprt->sc_max_requests; i++) {
  782. ret = svc_rdma_post_recv(newxprt);
  783. if (ret) {
  784. dprintk("svcrdma: failure posting receive buffers\n");
  785. goto errout;
  786. }
  787. }
  788. /* Swap out the handler */
  789. newxprt->sc_cm_id->event_handler = rdma_cma_handler;
  790. /*
  791. * Arm the CQs for the SQ and RQ before accepting so we can't
  792. * miss the first message
  793. */
  794. ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  795. ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
  796. /* Accept Connection */
  797. set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
  798. memset(&conn_param, 0, sizeof conn_param);
  799. conn_param.responder_resources = 0;
  800. conn_param.initiator_depth = newxprt->sc_ord;
  801. ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
  802. if (ret) {
  803. dprintk("svcrdma: failed to accept new connection, ret=%d\n",
  804. ret);
  805. goto errout;
  806. }
  807. dprintk("svcrdma: new connection %p accepted with the following "
  808. "attributes:\n"
  809. " local_ip : %d.%d.%d.%d\n"
  810. " local_port : %d\n"
  811. " remote_ip : %d.%d.%d.%d\n"
  812. " remote_port : %d\n"
  813. " max_sge : %d\n"
  814. " sq_depth : %d\n"
  815. " max_requests : %d\n"
  816. " ord : %d\n",
  817. newxprt,
  818. NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
  819. route.addr.src_addr)->sin_addr.s_addr),
  820. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  821. route.addr.src_addr)->sin_port),
  822. NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
  823. route.addr.dst_addr)->sin_addr.s_addr),
  824. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  825. route.addr.dst_addr)->sin_port),
  826. newxprt->sc_max_sge,
  827. newxprt->sc_sq_depth,
  828. newxprt->sc_max_requests,
  829. newxprt->sc_ord);
  830. return &newxprt->sc_xprt;
  831. errout:
  832. dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
  833. /* Take a reference in case the DTO handler runs */
  834. svc_xprt_get(&newxprt->sc_xprt);
  835. if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
  836. ib_destroy_qp(newxprt->sc_qp);
  837. rdma_destroy_id(newxprt->sc_cm_id);
  838. /* This call to put will destroy the transport */
  839. svc_xprt_put(&newxprt->sc_xprt);
  840. return NULL;
  841. }
  842. static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
  843. {
  844. }
  845. /*
  846. * When connected, an svc_xprt has at least two references:
  847. *
  848. * - A reference held by the cm_id between the ESTABLISHED and
  849. * DISCONNECTED events. If the remote peer disconnected first, this
  850. * reference could be gone.
  851. *
  852. * - A reference held by the svc_recv code that called this function
  853. * as part of close processing.
  854. *
  855. * At a minimum one references should still be held.
  856. */
  857. static void svc_rdma_detach(struct svc_xprt *xprt)
  858. {
  859. struct svcxprt_rdma *rdma =
  860. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  861. dprintk("svc: svc_rdma_detach(%p)\n", xprt);
  862. /* Disconnect and flush posted WQE */
  863. rdma_disconnect(rdma->sc_cm_id);
  864. }
  865. static void __svc_rdma_free(struct work_struct *work)
  866. {
  867. struct svcxprt_rdma *rdma =
  868. container_of(work, struct svcxprt_rdma, sc_work);
  869. dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
  870. /* We should only be called from kref_put */
  871. BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
  872. /*
  873. * Destroy queued, but not processed read completions. Note
  874. * that this cleanup has to be done before destroying the
  875. * cm_id because the device ptr is needed to unmap the dma in
  876. * svc_rdma_put_context.
  877. */
  878. spin_lock_bh(&rdma->sc_read_complete_lock);
  879. while (!list_empty(&rdma->sc_read_complete_q)) {
  880. struct svc_rdma_op_ctxt *ctxt;
  881. ctxt = list_entry(rdma->sc_read_complete_q.next,
  882. struct svc_rdma_op_ctxt,
  883. dto_q);
  884. list_del_init(&ctxt->dto_q);
  885. svc_rdma_put_context(ctxt, 1);
  886. }
  887. spin_unlock_bh(&rdma->sc_read_complete_lock);
  888. /* Destroy queued, but not processed recv completions */
  889. spin_lock_bh(&rdma->sc_rq_dto_lock);
  890. while (!list_empty(&rdma->sc_rq_dto_q)) {
  891. struct svc_rdma_op_ctxt *ctxt;
  892. ctxt = list_entry(rdma->sc_rq_dto_q.next,
  893. struct svc_rdma_op_ctxt,
  894. dto_q);
  895. list_del_init(&ctxt->dto_q);
  896. svc_rdma_put_context(ctxt, 1);
  897. }
  898. spin_unlock_bh(&rdma->sc_rq_dto_lock);
  899. /* Warn if we leaked a resource or under-referenced */
  900. WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
  901. /* Destroy the QP if present (not a listener) */
  902. if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
  903. ib_destroy_qp(rdma->sc_qp);
  904. if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
  905. ib_destroy_cq(rdma->sc_sq_cq);
  906. if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
  907. ib_destroy_cq(rdma->sc_rq_cq);
  908. if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
  909. ib_dereg_mr(rdma->sc_phys_mr);
  910. if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
  911. ib_dealloc_pd(rdma->sc_pd);
  912. /* Destroy the CM ID */
  913. rdma_destroy_id(rdma->sc_cm_id);
  914. destroy_context_cache(rdma);
  915. kfree(rdma);
  916. }
  917. static void svc_rdma_free(struct svc_xprt *xprt)
  918. {
  919. struct svcxprt_rdma *rdma =
  920. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  921. INIT_WORK(&rdma->sc_work, __svc_rdma_free);
  922. schedule_work(&rdma->sc_work);
  923. }
  924. static int svc_rdma_has_wspace(struct svc_xprt *xprt)
  925. {
  926. struct svcxprt_rdma *rdma =
  927. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  928. /*
  929. * If there are fewer SQ WR available than required to send a
  930. * simple response, return false.
  931. */
  932. if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
  933. return 0;
  934. /*
  935. * ...or there are already waiters on the SQ,
  936. * return false.
  937. */
  938. if (waitqueue_active(&rdma->sc_send_wait))
  939. return 0;
  940. /* Otherwise return true. */
  941. return 1;
  942. }
  943. int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
  944. {
  945. struct ib_send_wr *bad_wr;
  946. int ret;
  947. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  948. return -ENOTCONN;
  949. BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
  950. BUG_ON(((struct svc_rdma_op_ctxt *)(unsigned long)wr->wr_id)->wr_op !=
  951. wr->opcode);
  952. /* If the SQ is full, wait until an SQ entry is available */
  953. while (1) {
  954. spin_lock_bh(&xprt->sc_lock);
  955. if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) {
  956. spin_unlock_bh(&xprt->sc_lock);
  957. atomic_inc(&rdma_stat_sq_starve);
  958. /* See if we can opportunistically reap SQ WR to make room */
  959. sq_cq_reap(xprt);
  960. /* Wait until SQ WR available if SQ still full */
  961. wait_event(xprt->sc_send_wait,
  962. atomic_read(&xprt->sc_sq_count) <
  963. xprt->sc_sq_depth);
  964. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  965. return 0;
  966. continue;
  967. }
  968. /* Bumped used SQ WR count and post */
  969. svc_xprt_get(&xprt->sc_xprt);
  970. ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
  971. if (!ret)
  972. atomic_inc(&xprt->sc_sq_count);
  973. else {
  974. svc_xprt_put(&xprt->sc_xprt);
  975. dprintk("svcrdma: failed to post SQ WR rc=%d, "
  976. "sc_sq_count=%d, sc_sq_depth=%d\n",
  977. ret, atomic_read(&xprt->sc_sq_count),
  978. xprt->sc_sq_depth);
  979. }
  980. spin_unlock_bh(&xprt->sc_lock);
  981. break;
  982. }
  983. return ret;
  984. }
  985. void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
  986. enum rpcrdma_errcode err)
  987. {
  988. struct ib_send_wr err_wr;
  989. struct ib_sge sge;
  990. struct page *p;
  991. struct svc_rdma_op_ctxt *ctxt;
  992. u32 *va;
  993. int length;
  994. int ret;
  995. p = svc_rdma_get_page();
  996. va = page_address(p);
  997. /* XDR encode error */
  998. length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
  999. /* Prepare SGE for local address */
  1000. sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
  1001. p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  1002. sge.lkey = xprt->sc_phys_mr->lkey;
  1003. sge.length = length;
  1004. ctxt = svc_rdma_get_context(xprt);
  1005. ctxt->count = 1;
  1006. ctxt->pages[0] = p;
  1007. /* Prepare SEND WR */
  1008. memset(&err_wr, 0, sizeof err_wr);
  1009. ctxt->wr_op = IB_WR_SEND;
  1010. err_wr.wr_id = (unsigned long)ctxt;
  1011. err_wr.sg_list = &sge;
  1012. err_wr.num_sge = 1;
  1013. err_wr.opcode = IB_WR_SEND;
  1014. err_wr.send_flags = IB_SEND_SIGNALED;
  1015. /* Post It */
  1016. ret = svc_rdma_send(xprt, &err_wr);
  1017. if (ret) {
  1018. dprintk("svcrdma: Error %d posting send for protocol error\n",
  1019. ret);
  1020. svc_rdma_put_context(ctxt, 1);
  1021. }
  1022. }