svc_rdma_transport.c 38 KB

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