svc_rdma_transport.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. dma_addr_t 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. pa = ib_dma_map_page(xprt->sc_cm_id->device,
  437. page, 0, PAGE_SIZE,
  438. DMA_FROM_DEVICE);
  439. if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
  440. goto err_put_ctxt;
  441. atomic_inc(&xprt->sc_dma_used);
  442. ctxt->sge[sge_no].addr = pa;
  443. ctxt->sge[sge_no].length = PAGE_SIZE;
  444. ctxt->sge[sge_no].lkey = xprt->sc_dma_lkey;
  445. buflen += PAGE_SIZE;
  446. }
  447. ctxt->count = sge_no;
  448. recv_wr.next = NULL;
  449. recv_wr.sg_list = &ctxt->sge[0];
  450. recv_wr.num_sge = ctxt->count;
  451. recv_wr.wr_id = (u64)(unsigned long)ctxt;
  452. svc_xprt_get(&xprt->sc_xprt);
  453. ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
  454. if (ret) {
  455. svc_xprt_put(&xprt->sc_xprt);
  456. svc_rdma_put_context(ctxt, 1);
  457. }
  458. return ret;
  459. err_put_ctxt:
  460. svc_rdma_put_context(ctxt, 1);
  461. return -ENOMEM;
  462. }
  463. /*
  464. * This function handles the CONNECT_REQUEST event on a listening
  465. * endpoint. It is passed the cma_id for the _new_ connection. The context in
  466. * this cma_id is inherited from the listening cma_id and is the svc_xprt
  467. * structure for the listening endpoint.
  468. *
  469. * This function creates a new xprt for the new connection and enqueues it on
  470. * the accept queue for the listent xprt. When the listen thread is kicked, it
  471. * will call the recvfrom method on the listen xprt which will accept the new
  472. * connection.
  473. */
  474. static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
  475. {
  476. struct svcxprt_rdma *listen_xprt = new_cma_id->context;
  477. struct svcxprt_rdma *newxprt;
  478. struct sockaddr *sa;
  479. /* Create a new transport */
  480. newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
  481. if (!newxprt) {
  482. dprintk("svcrdma: failed to create new transport\n");
  483. return;
  484. }
  485. newxprt->sc_cm_id = new_cma_id;
  486. new_cma_id->context = newxprt;
  487. dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
  488. newxprt, newxprt->sc_cm_id, listen_xprt);
  489. /* Save client advertised inbound read limit for use later in accept. */
  490. newxprt->sc_ord = client_ird;
  491. /* Set the local and remote addresses in the transport */
  492. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
  493. svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  494. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
  495. svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  496. /*
  497. * Enqueue the new transport on the accept queue of the listening
  498. * transport
  499. */
  500. spin_lock_bh(&listen_xprt->sc_lock);
  501. list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
  502. spin_unlock_bh(&listen_xprt->sc_lock);
  503. /*
  504. * Can't use svc_xprt_received here because we are not on a
  505. * rqstp thread
  506. */
  507. set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
  508. svc_xprt_enqueue(&listen_xprt->sc_xprt);
  509. }
  510. /*
  511. * Handles events generated on the listening endpoint. These events will be
  512. * either be incoming connect requests or adapter removal events.
  513. */
  514. static int rdma_listen_handler(struct rdma_cm_id *cma_id,
  515. struct rdma_cm_event *event)
  516. {
  517. struct svcxprt_rdma *xprt = cma_id->context;
  518. int ret = 0;
  519. switch (event->event) {
  520. case RDMA_CM_EVENT_CONNECT_REQUEST:
  521. dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
  522. "event=%d\n", cma_id, cma_id->context, event->event);
  523. handle_connect_req(cma_id,
  524. event->param.conn.responder_resources);
  525. break;
  526. case RDMA_CM_EVENT_ESTABLISHED:
  527. /* Accept complete */
  528. dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
  529. "cm_id=%p\n", xprt, cma_id);
  530. break;
  531. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  532. dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
  533. xprt, cma_id);
  534. if (xprt)
  535. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  536. break;
  537. default:
  538. dprintk("svcrdma: Unexpected event on listening endpoint %p, "
  539. "event=%d\n", cma_id, event->event);
  540. break;
  541. }
  542. return ret;
  543. }
  544. static int rdma_cma_handler(struct rdma_cm_id *cma_id,
  545. struct rdma_cm_event *event)
  546. {
  547. struct svc_xprt *xprt = cma_id->context;
  548. struct svcxprt_rdma *rdma =
  549. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  550. switch (event->event) {
  551. case RDMA_CM_EVENT_ESTABLISHED:
  552. /* Accept complete */
  553. svc_xprt_get(xprt);
  554. dprintk("svcrdma: Connection completed on DTO xprt=%p, "
  555. "cm_id=%p\n", xprt, cma_id);
  556. clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
  557. svc_xprt_enqueue(xprt);
  558. break;
  559. case RDMA_CM_EVENT_DISCONNECTED:
  560. dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
  561. xprt, cma_id);
  562. if (xprt) {
  563. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  564. svc_xprt_enqueue(xprt);
  565. svc_xprt_put(xprt);
  566. }
  567. break;
  568. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  569. dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
  570. "event=%d\n", cma_id, xprt, event->event);
  571. if (xprt) {
  572. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  573. svc_xprt_enqueue(xprt);
  574. }
  575. break;
  576. default:
  577. dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
  578. "event=%d\n", cma_id, event->event);
  579. break;
  580. }
  581. return 0;
  582. }
  583. /*
  584. * Create a listening RDMA service endpoint.
  585. */
  586. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  587. struct sockaddr *sa, int salen,
  588. int flags)
  589. {
  590. struct rdma_cm_id *listen_id;
  591. struct svcxprt_rdma *cma_xprt;
  592. struct svc_xprt *xprt;
  593. int ret;
  594. dprintk("svcrdma: Creating RDMA socket\n");
  595. cma_xprt = rdma_create_xprt(serv, 1);
  596. if (!cma_xprt)
  597. return ERR_PTR(-ENOMEM);
  598. xprt = &cma_xprt->sc_xprt;
  599. listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
  600. if (IS_ERR(listen_id)) {
  601. ret = PTR_ERR(listen_id);
  602. dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
  603. goto err0;
  604. }
  605. ret = rdma_bind_addr(listen_id, sa);
  606. if (ret) {
  607. dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
  608. goto err1;
  609. }
  610. cma_xprt->sc_cm_id = listen_id;
  611. ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
  612. if (ret) {
  613. dprintk("svcrdma: rdma_listen failed = %d\n", ret);
  614. goto err1;
  615. }
  616. /*
  617. * We need to use the address from the cm_id in case the
  618. * caller specified 0 for the port number.
  619. */
  620. sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
  621. svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
  622. return &cma_xprt->sc_xprt;
  623. err1:
  624. rdma_destroy_id(listen_id);
  625. err0:
  626. kfree(cma_xprt);
  627. return ERR_PTR(ret);
  628. }
  629. static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
  630. {
  631. struct ib_mr *mr;
  632. struct ib_fast_reg_page_list *pl;
  633. struct svc_rdma_fastreg_mr *frmr;
  634. frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
  635. if (!frmr)
  636. goto err;
  637. mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
  638. if (!mr)
  639. goto err_free_frmr;
  640. pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
  641. RPCSVC_MAXPAGES);
  642. if (!pl)
  643. goto err_free_mr;
  644. frmr->mr = mr;
  645. frmr->page_list = pl;
  646. INIT_LIST_HEAD(&frmr->frmr_list);
  647. return frmr;
  648. err_free_mr:
  649. ib_dereg_mr(mr);
  650. err_free_frmr:
  651. kfree(frmr);
  652. err:
  653. return ERR_PTR(-ENOMEM);
  654. }
  655. static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
  656. {
  657. struct svc_rdma_fastreg_mr *frmr;
  658. while (!list_empty(&xprt->sc_frmr_q)) {
  659. frmr = list_entry(xprt->sc_frmr_q.next,
  660. struct svc_rdma_fastreg_mr, frmr_list);
  661. list_del_init(&frmr->frmr_list);
  662. ib_dereg_mr(frmr->mr);
  663. ib_free_fast_reg_page_list(frmr->page_list);
  664. kfree(frmr);
  665. }
  666. }
  667. struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
  668. {
  669. struct svc_rdma_fastreg_mr *frmr = NULL;
  670. spin_lock_bh(&rdma->sc_frmr_q_lock);
  671. if (!list_empty(&rdma->sc_frmr_q)) {
  672. frmr = list_entry(rdma->sc_frmr_q.next,
  673. struct svc_rdma_fastreg_mr, frmr_list);
  674. list_del_init(&frmr->frmr_list);
  675. frmr->map_len = 0;
  676. frmr->page_list_len = 0;
  677. }
  678. spin_unlock_bh(&rdma->sc_frmr_q_lock);
  679. if (frmr)
  680. return frmr;
  681. return rdma_alloc_frmr(rdma);
  682. }
  683. static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
  684. struct svc_rdma_fastreg_mr *frmr)
  685. {
  686. int page_no;
  687. for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
  688. dma_addr_t addr = frmr->page_list->page_list[page_no];
  689. if (ib_dma_mapping_error(frmr->mr->device, addr))
  690. continue;
  691. atomic_dec(&xprt->sc_dma_used);
  692. ib_dma_unmap_single(frmr->mr->device, addr, PAGE_SIZE,
  693. frmr->direction);
  694. }
  695. }
  696. void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
  697. struct svc_rdma_fastreg_mr *frmr)
  698. {
  699. if (frmr) {
  700. frmr_unmap_dma(rdma, frmr);
  701. spin_lock_bh(&rdma->sc_frmr_q_lock);
  702. BUG_ON(!list_empty(&frmr->frmr_list));
  703. list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
  704. spin_unlock_bh(&rdma->sc_frmr_q_lock);
  705. }
  706. }
  707. /*
  708. * This is the xpo_recvfrom function for listening endpoints. Its
  709. * purpose is to accept incoming connections. The CMA callback handler
  710. * has already created a new transport and attached it to the new CMA
  711. * ID.
  712. *
  713. * There is a queue of pending connections hung on the listening
  714. * transport. This queue contains the new svc_xprt structure. This
  715. * function takes svc_xprt structures off the accept_q and completes
  716. * the connection.
  717. */
  718. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
  719. {
  720. struct svcxprt_rdma *listen_rdma;
  721. struct svcxprt_rdma *newxprt = NULL;
  722. struct rdma_conn_param conn_param;
  723. struct ib_qp_init_attr qp_attr;
  724. struct ib_device_attr devattr;
  725. int dma_mr_acc;
  726. int need_dma_mr;
  727. int ret;
  728. int i;
  729. listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
  730. clear_bit(XPT_CONN, &xprt->xpt_flags);
  731. /* Get the next entry off the accept list */
  732. spin_lock_bh(&listen_rdma->sc_lock);
  733. if (!list_empty(&listen_rdma->sc_accept_q)) {
  734. newxprt = list_entry(listen_rdma->sc_accept_q.next,
  735. struct svcxprt_rdma, sc_accept_q);
  736. list_del_init(&newxprt->sc_accept_q);
  737. }
  738. if (!list_empty(&listen_rdma->sc_accept_q))
  739. set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
  740. spin_unlock_bh(&listen_rdma->sc_lock);
  741. if (!newxprt)
  742. return NULL;
  743. dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
  744. newxprt, newxprt->sc_cm_id);
  745. ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
  746. if (ret) {
  747. dprintk("svcrdma: could not query device attributes on "
  748. "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
  749. goto errout;
  750. }
  751. /* Qualify the transport resource defaults with the
  752. * capabilities of this particular device */
  753. newxprt->sc_max_sge = min((size_t)devattr.max_sge,
  754. (size_t)RPCSVC_MAXPAGES);
  755. newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
  756. (size_t)svcrdma_max_requests);
  757. newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
  758. /*
  759. * Limit ORD based on client limit, local device limit, and
  760. * configured svcrdma limit.
  761. */
  762. newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
  763. newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
  764. newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
  765. if (IS_ERR(newxprt->sc_pd)) {
  766. dprintk("svcrdma: error creating PD for connect request\n");
  767. goto errout;
  768. }
  769. newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  770. sq_comp_handler,
  771. cq_event_handler,
  772. newxprt,
  773. newxprt->sc_sq_depth,
  774. 0);
  775. if (IS_ERR(newxprt->sc_sq_cq)) {
  776. dprintk("svcrdma: error creating SQ CQ for connect request\n");
  777. goto errout;
  778. }
  779. newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  780. rq_comp_handler,
  781. cq_event_handler,
  782. newxprt,
  783. newxprt->sc_max_requests,
  784. 0);
  785. if (IS_ERR(newxprt->sc_rq_cq)) {
  786. dprintk("svcrdma: error creating RQ CQ for connect request\n");
  787. goto errout;
  788. }
  789. memset(&qp_attr, 0, sizeof qp_attr);
  790. qp_attr.event_handler = qp_event_handler;
  791. qp_attr.qp_context = &newxprt->sc_xprt;
  792. qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
  793. qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
  794. qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
  795. qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
  796. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  797. qp_attr.qp_type = IB_QPT_RC;
  798. qp_attr.send_cq = newxprt->sc_sq_cq;
  799. qp_attr.recv_cq = newxprt->sc_rq_cq;
  800. dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
  801. " cm_id->device=%p, sc_pd->device=%p\n"
  802. " cap.max_send_wr = %d\n"
  803. " cap.max_recv_wr = %d\n"
  804. " cap.max_send_sge = %d\n"
  805. " cap.max_recv_sge = %d\n",
  806. newxprt->sc_cm_id, newxprt->sc_pd,
  807. newxprt->sc_cm_id->device, newxprt->sc_pd->device,
  808. qp_attr.cap.max_send_wr,
  809. qp_attr.cap.max_recv_wr,
  810. qp_attr.cap.max_send_sge,
  811. qp_attr.cap.max_recv_sge);
  812. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
  813. if (ret) {
  814. /*
  815. * XXX: This is a hack. We need a xx_request_qp interface
  816. * that will adjust the qp_attr's with a best-effort
  817. * number
  818. */
  819. qp_attr.cap.max_send_sge -= 2;
  820. qp_attr.cap.max_recv_sge -= 2;
  821. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
  822. &qp_attr);
  823. if (ret) {
  824. dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
  825. goto errout;
  826. }
  827. newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
  828. newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
  829. newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
  830. newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
  831. }
  832. newxprt->sc_qp = newxprt->sc_cm_id->qp;
  833. /*
  834. * Use the most secure set of MR resources based on the
  835. * transport type and available memory management features in
  836. * the device. Here's the table implemented below:
  837. *
  838. * Fast Global DMA Remote WR
  839. * Reg LKEY MR Access
  840. * Sup'd Sup'd Needed Needed
  841. *
  842. * IWARP N N Y Y
  843. * N Y Y Y
  844. * Y N Y N
  845. * Y Y N -
  846. *
  847. * IB N N Y N
  848. * N Y N -
  849. * Y N Y N
  850. * Y Y N -
  851. *
  852. * NB: iWARP requires remote write access for the data sink
  853. * of an RDMA_READ. IB does not.
  854. */
  855. if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
  856. newxprt->sc_frmr_pg_list_len =
  857. devattr.max_fast_reg_page_list_len;
  858. newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
  859. }
  860. /*
  861. * Determine if a DMA MR is required and if so, what privs are required
  862. */
  863. switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
  864. case RDMA_TRANSPORT_IWARP:
  865. newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
  866. if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
  867. need_dma_mr = 1;
  868. dma_mr_acc =
  869. (IB_ACCESS_LOCAL_WRITE |
  870. IB_ACCESS_REMOTE_WRITE);
  871. } else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
  872. need_dma_mr = 1;
  873. dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
  874. } else
  875. need_dma_mr = 0;
  876. break;
  877. case RDMA_TRANSPORT_IB:
  878. if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
  879. need_dma_mr = 1;
  880. dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
  881. } else
  882. need_dma_mr = 0;
  883. break;
  884. default:
  885. goto errout;
  886. }
  887. /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
  888. if (need_dma_mr) {
  889. /* Register all of physical memory */
  890. newxprt->sc_phys_mr =
  891. ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
  892. if (IS_ERR(newxprt->sc_phys_mr)) {
  893. dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
  894. ret);
  895. goto errout;
  896. }
  897. newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
  898. } else
  899. newxprt->sc_dma_lkey =
  900. newxprt->sc_cm_id->device->local_dma_lkey;
  901. /* Post receive buffers */
  902. for (i = 0; i < newxprt->sc_max_requests; i++) {
  903. ret = svc_rdma_post_recv(newxprt);
  904. if (ret) {
  905. dprintk("svcrdma: failure posting receive buffers\n");
  906. goto errout;
  907. }
  908. }
  909. /* Swap out the handler */
  910. newxprt->sc_cm_id->event_handler = rdma_cma_handler;
  911. /*
  912. * Arm the CQs for the SQ and RQ before accepting so we can't
  913. * miss the first message
  914. */
  915. ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  916. ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
  917. /* Accept Connection */
  918. set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
  919. memset(&conn_param, 0, sizeof conn_param);
  920. conn_param.responder_resources = 0;
  921. conn_param.initiator_depth = newxprt->sc_ord;
  922. ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
  923. if (ret) {
  924. dprintk("svcrdma: failed to accept new connection, ret=%d\n",
  925. ret);
  926. goto errout;
  927. }
  928. dprintk("svcrdma: new connection %p accepted with the following "
  929. "attributes:\n"
  930. " local_ip : %d.%d.%d.%d\n"
  931. " local_port : %d\n"
  932. " remote_ip : %d.%d.%d.%d\n"
  933. " remote_port : %d\n"
  934. " max_sge : %d\n"
  935. " sq_depth : %d\n"
  936. " max_requests : %d\n"
  937. " ord : %d\n",
  938. newxprt,
  939. NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
  940. route.addr.src_addr)->sin_addr.s_addr),
  941. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  942. route.addr.src_addr)->sin_port),
  943. NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
  944. route.addr.dst_addr)->sin_addr.s_addr),
  945. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  946. route.addr.dst_addr)->sin_port),
  947. newxprt->sc_max_sge,
  948. newxprt->sc_sq_depth,
  949. newxprt->sc_max_requests,
  950. newxprt->sc_ord);
  951. return &newxprt->sc_xprt;
  952. errout:
  953. dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
  954. /* Take a reference in case the DTO handler runs */
  955. svc_xprt_get(&newxprt->sc_xprt);
  956. if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
  957. ib_destroy_qp(newxprt->sc_qp);
  958. rdma_destroy_id(newxprt->sc_cm_id);
  959. /* This call to put will destroy the transport */
  960. svc_xprt_put(&newxprt->sc_xprt);
  961. return NULL;
  962. }
  963. static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
  964. {
  965. }
  966. /*
  967. * When connected, an svc_xprt has at least two references:
  968. *
  969. * - A reference held by the cm_id between the ESTABLISHED and
  970. * DISCONNECTED events. If the remote peer disconnected first, this
  971. * reference could be gone.
  972. *
  973. * - A reference held by the svc_recv code that called this function
  974. * as part of close processing.
  975. *
  976. * At a minimum one references should still be held.
  977. */
  978. static void svc_rdma_detach(struct svc_xprt *xprt)
  979. {
  980. struct svcxprt_rdma *rdma =
  981. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  982. dprintk("svc: svc_rdma_detach(%p)\n", xprt);
  983. /* Disconnect and flush posted WQE */
  984. rdma_disconnect(rdma->sc_cm_id);
  985. }
  986. static void __svc_rdma_free(struct work_struct *work)
  987. {
  988. struct svcxprt_rdma *rdma =
  989. container_of(work, struct svcxprt_rdma, sc_work);
  990. dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
  991. /* We should only be called from kref_put */
  992. BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
  993. /*
  994. * Destroy queued, but not processed read completions. Note
  995. * that this cleanup has to be done before destroying the
  996. * cm_id because the device ptr is needed to unmap the dma in
  997. * svc_rdma_put_context.
  998. */
  999. while (!list_empty(&rdma->sc_read_complete_q)) {
  1000. struct svc_rdma_op_ctxt *ctxt;
  1001. ctxt = list_entry(rdma->sc_read_complete_q.next,
  1002. struct svc_rdma_op_ctxt,
  1003. dto_q);
  1004. list_del_init(&ctxt->dto_q);
  1005. svc_rdma_put_context(ctxt, 1);
  1006. }
  1007. /* Destroy queued, but not processed recv completions */
  1008. while (!list_empty(&rdma->sc_rq_dto_q)) {
  1009. struct svc_rdma_op_ctxt *ctxt;
  1010. ctxt = list_entry(rdma->sc_rq_dto_q.next,
  1011. struct svc_rdma_op_ctxt,
  1012. dto_q);
  1013. list_del_init(&ctxt->dto_q);
  1014. svc_rdma_put_context(ctxt, 1);
  1015. }
  1016. /* Warn if we leaked a resource or under-referenced */
  1017. WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
  1018. WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
  1019. /* De-allocate fastreg mr */
  1020. rdma_dealloc_frmr_q(rdma);
  1021. /* Destroy the QP if present (not a listener) */
  1022. if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
  1023. ib_destroy_qp(rdma->sc_qp);
  1024. if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
  1025. ib_destroy_cq(rdma->sc_sq_cq);
  1026. if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
  1027. ib_destroy_cq(rdma->sc_rq_cq);
  1028. if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
  1029. ib_dereg_mr(rdma->sc_phys_mr);
  1030. if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
  1031. ib_dealloc_pd(rdma->sc_pd);
  1032. /* Destroy the CM ID */
  1033. rdma_destroy_id(rdma->sc_cm_id);
  1034. kfree(rdma);
  1035. }
  1036. static void svc_rdma_free(struct svc_xprt *xprt)
  1037. {
  1038. struct svcxprt_rdma *rdma =
  1039. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  1040. INIT_WORK(&rdma->sc_work, __svc_rdma_free);
  1041. schedule_work(&rdma->sc_work);
  1042. }
  1043. static int svc_rdma_has_wspace(struct svc_xprt *xprt)
  1044. {
  1045. struct svcxprt_rdma *rdma =
  1046. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  1047. /*
  1048. * If there are fewer SQ WR available than required to send a
  1049. * simple response, return false.
  1050. */
  1051. if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
  1052. return 0;
  1053. /*
  1054. * ...or there are already waiters on the SQ,
  1055. * return false.
  1056. */
  1057. if (waitqueue_active(&rdma->sc_send_wait))
  1058. return 0;
  1059. /* Otherwise return true. */
  1060. return 1;
  1061. }
  1062. /*
  1063. * Attempt to register the kvec representing the RPC memory with the
  1064. * device.
  1065. *
  1066. * Returns:
  1067. * NULL : The device does not support fastreg or there were no more
  1068. * fastreg mr.
  1069. * frmr : The kvec register request was successfully posted.
  1070. * <0 : An error was encountered attempting to register the kvec.
  1071. */
  1072. int svc_rdma_fastreg(struct svcxprt_rdma *xprt,
  1073. struct svc_rdma_fastreg_mr *frmr)
  1074. {
  1075. struct ib_send_wr fastreg_wr;
  1076. u8 key;
  1077. /* Bump the key */
  1078. key = (u8)(frmr->mr->lkey & 0x000000FF);
  1079. ib_update_fast_reg_key(frmr->mr, ++key);
  1080. /* Prepare FASTREG WR */
  1081. memset(&fastreg_wr, 0, sizeof fastreg_wr);
  1082. fastreg_wr.opcode = IB_WR_FAST_REG_MR;
  1083. fastreg_wr.send_flags = IB_SEND_SIGNALED;
  1084. fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
  1085. fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
  1086. fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
  1087. fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
  1088. fastreg_wr.wr.fast_reg.length = frmr->map_len;
  1089. fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
  1090. fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
  1091. return svc_rdma_send(xprt, &fastreg_wr);
  1092. }
  1093. int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
  1094. {
  1095. struct ib_send_wr *bad_wr;
  1096. int ret;
  1097. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  1098. return -ENOTCONN;
  1099. BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
  1100. /* If the SQ is full, wait until an SQ entry is available */
  1101. while (1) {
  1102. spin_lock_bh(&xprt->sc_lock);
  1103. if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) {
  1104. spin_unlock_bh(&xprt->sc_lock);
  1105. atomic_inc(&rdma_stat_sq_starve);
  1106. /* See if we can opportunistically reap SQ WR to make room */
  1107. sq_cq_reap(xprt);
  1108. /* Wait until SQ WR available if SQ still full */
  1109. wait_event(xprt->sc_send_wait,
  1110. atomic_read(&xprt->sc_sq_count) <
  1111. xprt->sc_sq_depth);
  1112. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  1113. return 0;
  1114. continue;
  1115. }
  1116. /* Bumped used SQ WR count and post */
  1117. svc_xprt_get(&xprt->sc_xprt);
  1118. ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
  1119. if (!ret)
  1120. atomic_inc(&xprt->sc_sq_count);
  1121. else {
  1122. svc_xprt_put(&xprt->sc_xprt);
  1123. dprintk("svcrdma: failed to post SQ WR rc=%d, "
  1124. "sc_sq_count=%d, sc_sq_depth=%d\n",
  1125. ret, atomic_read(&xprt->sc_sq_count),
  1126. xprt->sc_sq_depth);
  1127. }
  1128. spin_unlock_bh(&xprt->sc_lock);
  1129. break;
  1130. }
  1131. return ret;
  1132. }
  1133. void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
  1134. enum rpcrdma_errcode err)
  1135. {
  1136. struct ib_send_wr err_wr;
  1137. struct ib_sge sge;
  1138. struct page *p;
  1139. struct svc_rdma_op_ctxt *ctxt;
  1140. u32 *va;
  1141. int length;
  1142. int ret;
  1143. p = svc_rdma_get_page();
  1144. va = page_address(p);
  1145. /* XDR encode error */
  1146. length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
  1147. /* Prepare SGE for local address */
  1148. atomic_inc(&xprt->sc_dma_used);
  1149. sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
  1150. p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  1151. sge.lkey = xprt->sc_phys_mr->lkey;
  1152. sge.length = length;
  1153. ctxt = svc_rdma_get_context(xprt);
  1154. ctxt->count = 1;
  1155. ctxt->pages[0] = p;
  1156. /* Prepare SEND WR */
  1157. memset(&err_wr, 0, sizeof err_wr);
  1158. ctxt->wr_op = IB_WR_SEND;
  1159. err_wr.wr_id = (unsigned long)ctxt;
  1160. err_wr.sg_list = &sge;
  1161. err_wr.num_sge = 1;
  1162. err_wr.opcode = IB_WR_SEND;
  1163. err_wr.send_flags = IB_SEND_SIGNALED;
  1164. /* Post It */
  1165. ret = svc_rdma_send(xprt, &err_wr);
  1166. if (ret) {
  1167. dprintk("svcrdma: Error %d posting send for protocol error\n",
  1168. ret);
  1169. svc_rdma_put_context(ctxt, 1);
  1170. }
  1171. }