svc_rdma_transport.c 32 KB

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