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