svc_rdma_transport.c 37 KB

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