svc_rdma_transport.c 38 KB

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