svc_rdma_transport.c 32 KB

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