svc_rdma_transport.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /*
  2. * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
  3. * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the BSD-type
  9. * license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials provided
  21. * with the distribution.
  22. *
  23. * Neither the name of the Network Appliance, Inc. nor the names of
  24. * its contributors may be used to endorse or promote products
  25. * derived from this software without specific prior written
  26. * permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  31. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  34. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  35. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  36. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * Author: Tom Tucker <tom@opengridcomputing.com>
  41. */
  42. #include <linux/sunrpc/svc_xprt.h>
  43. #include <linux/sunrpc/debug.h>
  44. #include <linux/sunrpc/rpc_rdma.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/sched.h>
  47. #include <linux/slab.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/workqueue.h>
  50. #include <rdma/ib_verbs.h>
  51. #include <rdma/rdma_cm.h>
  52. #include <linux/sunrpc/svc_rdma.h>
  53. #include <linux/export.h>
  54. #include "xprt_rdma.h"
  55. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  56. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  57. struct net *net,
  58. struct sockaddr *sa, int salen,
  59. int flags);
  60. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
  61. static void svc_rdma_release_rqst(struct svc_rqst *);
  62. static void dto_tasklet_func(unsigned long data);
  63. static void svc_rdma_detach(struct svc_xprt *xprt);
  64. static void svc_rdma_free(struct svc_xprt *xprt);
  65. static int svc_rdma_has_wspace(struct svc_xprt *xprt);
  66. static int svc_rdma_secure_port(struct svc_rqst *);
  67. static void rq_cq_reap(struct svcxprt_rdma *xprt);
  68. static void sq_cq_reap(struct svcxprt_rdma *xprt);
  69. static DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
  70. static DEFINE_SPINLOCK(dto_lock);
  71. static LIST_HEAD(dto_xprt_q);
  72. static struct svc_xprt_ops svc_rdma_ops = {
  73. .xpo_create = svc_rdma_create,
  74. .xpo_recvfrom = svc_rdma_recvfrom,
  75. .xpo_sendto = svc_rdma_sendto,
  76. .xpo_release_rqst = svc_rdma_release_rqst,
  77. .xpo_detach = svc_rdma_detach,
  78. .xpo_free = svc_rdma_free,
  79. .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
  80. .xpo_has_wspace = svc_rdma_has_wspace,
  81. .xpo_accept = svc_rdma_accept,
  82. .xpo_secure_port = svc_rdma_secure_port,
  83. };
  84. struct svc_xprt_class svc_rdma_class = {
  85. .xcl_name = "rdma",
  86. .xcl_owner = THIS_MODULE,
  87. .xcl_ops = &svc_rdma_ops,
  88. .xcl_max_payload = RPCSVC_MAXPAYLOAD_RDMA,
  89. .xcl_ident = XPRT_TRANSPORT_RDMA,
  90. };
  91. struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
  92. {
  93. struct svc_rdma_op_ctxt *ctxt;
  94. while (1) {
  95. ctxt = kmem_cache_alloc(svc_rdma_ctxt_cachep, GFP_KERNEL);
  96. if (ctxt)
  97. break;
  98. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  99. }
  100. ctxt->xprt = xprt;
  101. INIT_LIST_HEAD(&ctxt->dto_q);
  102. ctxt->count = 0;
  103. ctxt->frmr = NULL;
  104. atomic_inc(&xprt->sc_ctxt_used);
  105. return ctxt;
  106. }
  107. void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
  108. {
  109. struct svcxprt_rdma *xprt = ctxt->xprt;
  110. int i;
  111. for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
  112. /*
  113. * Unmap the DMA addr in the SGE if the lkey matches
  114. * the sc_dma_lkey, otherwise, ignore it since it is
  115. * an FRMR lkey and will be unmapped later when the
  116. * last WR that uses it completes.
  117. */
  118. if (ctxt->sge[i].lkey == xprt->sc_dma_lkey) {
  119. atomic_dec(&xprt->sc_dma_used);
  120. ib_dma_unmap_page(xprt->sc_cm_id->device,
  121. ctxt->sge[i].addr,
  122. ctxt->sge[i].length,
  123. ctxt->direction);
  124. }
  125. }
  126. }
  127. void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
  128. {
  129. struct svcxprt_rdma *xprt;
  130. int i;
  131. BUG_ON(!ctxt);
  132. xprt = ctxt->xprt;
  133. if (free_pages)
  134. for (i = 0; i < ctxt->count; i++)
  135. put_page(ctxt->pages[i]);
  136. kmem_cache_free(svc_rdma_ctxt_cachep, ctxt);
  137. atomic_dec(&xprt->sc_ctxt_used);
  138. }
  139. /*
  140. * Temporary NFS req mappings are shared across all transport
  141. * instances. These are short lived and should be bounded by the number
  142. * of concurrent server threads * depth of the SQ.
  143. */
  144. struct svc_rdma_req_map *svc_rdma_get_req_map(void)
  145. {
  146. struct svc_rdma_req_map *map;
  147. while (1) {
  148. map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
  149. if (map)
  150. break;
  151. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  152. }
  153. map->count = 0;
  154. 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. BUG_ON(ctxt->frmr);
  309. svc_rdma_put_context(ctxt, 1);
  310. break;
  311. case IB_WR_RDMA_WRITE:
  312. BUG_ON(ctxt->frmr);
  313. svc_rdma_put_context(ctxt, 0);
  314. break;
  315. case IB_WR_RDMA_READ:
  316. case IB_WR_RDMA_READ_WITH_INV:
  317. svc_rdma_put_frmr(xprt, ctxt->frmr);
  318. if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
  319. struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
  320. BUG_ON(!read_hdr);
  321. spin_lock_bh(&xprt->sc_rq_dto_lock);
  322. set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
  323. list_add_tail(&read_hdr->dto_q,
  324. &xprt->sc_read_complete_q);
  325. spin_unlock_bh(&xprt->sc_rq_dto_lock);
  326. svc_xprt_enqueue(&xprt->sc_xprt);
  327. }
  328. svc_rdma_put_context(ctxt, 0);
  329. break;
  330. default:
  331. BUG_ON(1);
  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_a[6];
  347. struct ib_wc *wc;
  348. struct ib_cq *cq = xprt->sc_sq_cq;
  349. int ret;
  350. memset(wc_a, 0, sizeof(wc_a));
  351. if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
  352. return;
  353. ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  354. atomic_inc(&rdma_stat_sq_poll);
  355. while ((ret = ib_poll_cq(cq, ARRAY_SIZE(wc_a), wc_a)) > 0) {
  356. int i;
  357. for (i = 0; i < ret; i++) {
  358. wc = &wc_a[i];
  359. if (wc->status != IB_WC_SUCCESS) {
  360. dprintk("svcrdma: sq wc err status %d\n",
  361. wc->status);
  362. /* Close the transport */
  363. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  364. }
  365. /* Decrement used SQ WR count */
  366. atomic_dec(&xprt->sc_sq_count);
  367. wake_up(&xprt->sc_send_wait);
  368. ctxt = (struct svc_rdma_op_ctxt *)
  369. (unsigned long)wc->wr_id;
  370. if (ctxt)
  371. process_context(xprt, ctxt);
  372. svc_xprt_put(&xprt->sc_xprt);
  373. }
  374. }
  375. if (ctxt)
  376. atomic_inc(&rdma_stat_sq_prod);
  377. }
  378. static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
  379. {
  380. struct svcxprt_rdma *xprt = cq_context;
  381. unsigned long flags;
  382. /* Guard against unconditional flush call for destroyed QP */
  383. if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
  384. return;
  385. /*
  386. * Set the bit regardless of whether or not it's on the list
  387. * because it may be on the list already due to an RQ
  388. * completion.
  389. */
  390. set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
  391. /*
  392. * If this transport is not already on the DTO transport queue,
  393. * add it
  394. */
  395. spin_lock_irqsave(&dto_lock, flags);
  396. if (list_empty(&xprt->sc_dto_q)) {
  397. svc_xprt_get(&xprt->sc_xprt);
  398. list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
  399. }
  400. spin_unlock_irqrestore(&dto_lock, flags);
  401. /* Tasklet does all the work to avoid irqsave locks. */
  402. tasklet_schedule(&dto_tasklet);
  403. }
  404. static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
  405. int listener)
  406. {
  407. struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
  408. if (!cma_xprt)
  409. return NULL;
  410. svc_xprt_init(&init_net, &svc_rdma_class, &cma_xprt->sc_xprt, serv);
  411. INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
  412. INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
  413. INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
  414. INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
  415. INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
  416. init_waitqueue_head(&cma_xprt->sc_send_wait);
  417. spin_lock_init(&cma_xprt->sc_lock);
  418. spin_lock_init(&cma_xprt->sc_rq_dto_lock);
  419. spin_lock_init(&cma_xprt->sc_frmr_q_lock);
  420. cma_xprt->sc_ord = svcrdma_ord;
  421. cma_xprt->sc_max_req_size = svcrdma_max_req_size;
  422. cma_xprt->sc_max_requests = svcrdma_max_requests;
  423. cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
  424. atomic_set(&cma_xprt->sc_sq_count, 0);
  425. atomic_set(&cma_xprt->sc_ctxt_used, 0);
  426. if (listener)
  427. set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
  428. return cma_xprt;
  429. }
  430. struct page *svc_rdma_get_page(void)
  431. {
  432. struct page *page;
  433. while ((page = alloc_page(GFP_KERNEL)) == NULL) {
  434. /* If we can't get memory, wait a bit and try again */
  435. printk(KERN_INFO "svcrdma: out of memory...retrying in 1s\n");
  436. schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
  437. }
  438. return page;
  439. }
  440. int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
  441. {
  442. struct ib_recv_wr recv_wr, *bad_recv_wr;
  443. struct svc_rdma_op_ctxt *ctxt;
  444. struct page *page;
  445. dma_addr_t pa;
  446. int sge_no;
  447. int buflen;
  448. int ret;
  449. ctxt = svc_rdma_get_context(xprt);
  450. buflen = 0;
  451. ctxt->direction = DMA_FROM_DEVICE;
  452. for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
  453. BUG_ON(sge_no >= xprt->sc_max_sge);
  454. page = svc_rdma_get_page();
  455. ctxt->pages[sge_no] = page;
  456. pa = ib_dma_map_page(xprt->sc_cm_id->device,
  457. page, 0, PAGE_SIZE,
  458. DMA_FROM_DEVICE);
  459. if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
  460. goto err_put_ctxt;
  461. atomic_inc(&xprt->sc_dma_used);
  462. ctxt->sge[sge_no].addr = pa;
  463. ctxt->sge[sge_no].length = PAGE_SIZE;
  464. ctxt->sge[sge_no].lkey = xprt->sc_dma_lkey;
  465. ctxt->count = sge_no + 1;
  466. buflen += PAGE_SIZE;
  467. }
  468. recv_wr.next = NULL;
  469. recv_wr.sg_list = &ctxt->sge[0];
  470. recv_wr.num_sge = ctxt->count;
  471. recv_wr.wr_id = (u64)(unsigned long)ctxt;
  472. svc_xprt_get(&xprt->sc_xprt);
  473. ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
  474. if (ret) {
  475. svc_rdma_unmap_dma(ctxt);
  476. svc_rdma_put_context(ctxt, 1);
  477. svc_xprt_put(&xprt->sc_xprt);
  478. }
  479. return ret;
  480. err_put_ctxt:
  481. svc_rdma_unmap_dma(ctxt);
  482. svc_rdma_put_context(ctxt, 1);
  483. return -ENOMEM;
  484. }
  485. /*
  486. * This function handles the CONNECT_REQUEST event on a listening
  487. * endpoint. It is passed the cma_id for the _new_ connection. The context in
  488. * this cma_id is inherited from the listening cma_id and is the svc_xprt
  489. * structure for the listening endpoint.
  490. *
  491. * This function creates a new xprt for the new connection and enqueues it on
  492. * the accept queue for the listent xprt. When the listen thread is kicked, it
  493. * will call the recvfrom method on the listen xprt which will accept the new
  494. * connection.
  495. */
  496. static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
  497. {
  498. struct svcxprt_rdma *listen_xprt = new_cma_id->context;
  499. struct svcxprt_rdma *newxprt;
  500. struct sockaddr *sa;
  501. /* Create a new transport */
  502. newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
  503. if (!newxprt) {
  504. dprintk("svcrdma: failed to create new transport\n");
  505. return;
  506. }
  507. newxprt->sc_cm_id = new_cma_id;
  508. new_cma_id->context = newxprt;
  509. dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
  510. newxprt, newxprt->sc_cm_id, listen_xprt);
  511. /* Save client advertised inbound read limit for use later in accept. */
  512. newxprt->sc_ord = client_ird;
  513. /* Set the local and remote addresses in the transport */
  514. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
  515. svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  516. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
  517. svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  518. /*
  519. * Enqueue the new transport on the accept queue of the listening
  520. * transport
  521. */
  522. spin_lock_bh(&listen_xprt->sc_lock);
  523. list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
  524. spin_unlock_bh(&listen_xprt->sc_lock);
  525. set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
  526. svc_xprt_enqueue(&listen_xprt->sc_xprt);
  527. }
  528. /*
  529. * Handles events generated on the listening endpoint. These events will be
  530. * either be incoming connect requests or adapter removal events.
  531. */
  532. static int rdma_listen_handler(struct rdma_cm_id *cma_id,
  533. struct rdma_cm_event *event)
  534. {
  535. struct svcxprt_rdma *xprt = cma_id->context;
  536. int ret = 0;
  537. switch (event->event) {
  538. case RDMA_CM_EVENT_CONNECT_REQUEST:
  539. dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
  540. "event=%d\n", cma_id, cma_id->context, event->event);
  541. handle_connect_req(cma_id,
  542. event->param.conn.initiator_depth);
  543. break;
  544. case RDMA_CM_EVENT_ESTABLISHED:
  545. /* Accept complete */
  546. dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
  547. "cm_id=%p\n", xprt, cma_id);
  548. break;
  549. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  550. dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
  551. xprt, cma_id);
  552. if (xprt)
  553. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  554. break;
  555. default:
  556. dprintk("svcrdma: Unexpected event on listening endpoint %p, "
  557. "event=%d\n", cma_id, event->event);
  558. break;
  559. }
  560. return ret;
  561. }
  562. static int rdma_cma_handler(struct rdma_cm_id *cma_id,
  563. struct rdma_cm_event *event)
  564. {
  565. struct svc_xprt *xprt = cma_id->context;
  566. struct svcxprt_rdma *rdma =
  567. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  568. switch (event->event) {
  569. case RDMA_CM_EVENT_ESTABLISHED:
  570. /* Accept complete */
  571. svc_xprt_get(xprt);
  572. dprintk("svcrdma: Connection completed on DTO xprt=%p, "
  573. "cm_id=%p\n", xprt, cma_id);
  574. clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
  575. svc_xprt_enqueue(xprt);
  576. break;
  577. case RDMA_CM_EVENT_DISCONNECTED:
  578. dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
  579. xprt, cma_id);
  580. if (xprt) {
  581. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  582. svc_xprt_enqueue(xprt);
  583. svc_xprt_put(xprt);
  584. }
  585. break;
  586. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  587. dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
  588. "event=%d\n", cma_id, xprt, event->event);
  589. if (xprt) {
  590. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  591. svc_xprt_enqueue(xprt);
  592. }
  593. break;
  594. default:
  595. dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
  596. "event=%d\n", cma_id, event->event);
  597. break;
  598. }
  599. return 0;
  600. }
  601. /*
  602. * Create a listening RDMA service endpoint.
  603. */
  604. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  605. struct net *net,
  606. struct sockaddr *sa, int salen,
  607. int flags)
  608. {
  609. struct rdma_cm_id *listen_id;
  610. struct svcxprt_rdma *cma_xprt;
  611. struct svc_xprt *xprt;
  612. int ret;
  613. dprintk("svcrdma: Creating RDMA socket\n");
  614. if (sa->sa_family != AF_INET) {
  615. dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
  616. return ERR_PTR(-EAFNOSUPPORT);
  617. }
  618. cma_xprt = rdma_create_xprt(serv, 1);
  619. if (!cma_xprt)
  620. return ERR_PTR(-ENOMEM);
  621. xprt = &cma_xprt->sc_xprt;
  622. listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP,
  623. IB_QPT_RC);
  624. if (IS_ERR(listen_id)) {
  625. ret = PTR_ERR(listen_id);
  626. dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
  627. goto err0;
  628. }
  629. ret = rdma_bind_addr(listen_id, sa);
  630. if (ret) {
  631. dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
  632. goto err1;
  633. }
  634. cma_xprt->sc_cm_id = listen_id;
  635. ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
  636. if (ret) {
  637. dprintk("svcrdma: rdma_listen failed = %d\n", ret);
  638. goto err1;
  639. }
  640. /*
  641. * We need to use the address from the cm_id in case the
  642. * caller specified 0 for the port number.
  643. */
  644. sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
  645. svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
  646. return &cma_xprt->sc_xprt;
  647. err1:
  648. rdma_destroy_id(listen_id);
  649. err0:
  650. kfree(cma_xprt);
  651. return ERR_PTR(ret);
  652. }
  653. static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
  654. {
  655. struct ib_mr *mr;
  656. struct ib_fast_reg_page_list *pl;
  657. struct svc_rdma_fastreg_mr *frmr;
  658. frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
  659. if (!frmr)
  660. goto err;
  661. mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
  662. if (IS_ERR(mr))
  663. goto err_free_frmr;
  664. pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
  665. RPCSVC_MAXPAGES);
  666. if (IS_ERR(pl))
  667. goto err_free_mr;
  668. frmr->mr = mr;
  669. frmr->page_list = pl;
  670. INIT_LIST_HEAD(&frmr->frmr_list);
  671. return frmr;
  672. err_free_mr:
  673. ib_dereg_mr(mr);
  674. err_free_frmr:
  675. kfree(frmr);
  676. err:
  677. return ERR_PTR(-ENOMEM);
  678. }
  679. static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
  680. {
  681. struct svc_rdma_fastreg_mr *frmr;
  682. while (!list_empty(&xprt->sc_frmr_q)) {
  683. frmr = list_entry(xprt->sc_frmr_q.next,
  684. struct svc_rdma_fastreg_mr, frmr_list);
  685. list_del_init(&frmr->frmr_list);
  686. ib_dereg_mr(frmr->mr);
  687. ib_free_fast_reg_page_list(frmr->page_list);
  688. kfree(frmr);
  689. }
  690. }
  691. struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
  692. {
  693. struct svc_rdma_fastreg_mr *frmr = NULL;
  694. spin_lock_bh(&rdma->sc_frmr_q_lock);
  695. if (!list_empty(&rdma->sc_frmr_q)) {
  696. frmr = list_entry(rdma->sc_frmr_q.next,
  697. struct svc_rdma_fastreg_mr, frmr_list);
  698. list_del_init(&frmr->frmr_list);
  699. frmr->map_len = 0;
  700. frmr->page_list_len = 0;
  701. }
  702. spin_unlock_bh(&rdma->sc_frmr_q_lock);
  703. if (frmr)
  704. return frmr;
  705. return rdma_alloc_frmr(rdma);
  706. }
  707. static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
  708. struct svc_rdma_fastreg_mr *frmr)
  709. {
  710. int page_no;
  711. for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
  712. dma_addr_t addr = frmr->page_list->page_list[page_no];
  713. if (ib_dma_mapping_error(frmr->mr->device, addr))
  714. continue;
  715. atomic_dec(&xprt->sc_dma_used);
  716. ib_dma_unmap_page(frmr->mr->device, addr, PAGE_SIZE,
  717. frmr->direction);
  718. }
  719. }
  720. void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
  721. struct svc_rdma_fastreg_mr *frmr)
  722. {
  723. if (frmr) {
  724. frmr_unmap_dma(rdma, frmr);
  725. spin_lock_bh(&rdma->sc_frmr_q_lock);
  726. BUG_ON(!list_empty(&frmr->frmr_list));
  727. list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
  728. spin_unlock_bh(&rdma->sc_frmr_q_lock);
  729. }
  730. }
  731. /*
  732. * This is the xpo_recvfrom function for listening endpoints. Its
  733. * purpose is to accept incoming connections. The CMA callback handler
  734. * has already created a new transport and attached it to the new CMA
  735. * ID.
  736. *
  737. * There is a queue of pending connections hung on the listening
  738. * transport. This queue contains the new svc_xprt structure. This
  739. * function takes svc_xprt structures off the accept_q and completes
  740. * the connection.
  741. */
  742. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
  743. {
  744. struct svcxprt_rdma *listen_rdma;
  745. struct svcxprt_rdma *newxprt = NULL;
  746. struct rdma_conn_param conn_param;
  747. struct ib_qp_init_attr qp_attr;
  748. struct ib_device_attr devattr;
  749. int uninitialized_var(dma_mr_acc);
  750. int need_dma_mr;
  751. int ret;
  752. int i;
  753. listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
  754. clear_bit(XPT_CONN, &xprt->xpt_flags);
  755. /* Get the next entry off the accept list */
  756. spin_lock_bh(&listen_rdma->sc_lock);
  757. if (!list_empty(&listen_rdma->sc_accept_q)) {
  758. newxprt = list_entry(listen_rdma->sc_accept_q.next,
  759. struct svcxprt_rdma, sc_accept_q);
  760. list_del_init(&newxprt->sc_accept_q);
  761. }
  762. if (!list_empty(&listen_rdma->sc_accept_q))
  763. set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
  764. spin_unlock_bh(&listen_rdma->sc_lock);
  765. if (!newxprt)
  766. return NULL;
  767. dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
  768. newxprt, newxprt->sc_cm_id);
  769. ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
  770. if (ret) {
  771. dprintk("svcrdma: could not query device attributes on "
  772. "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
  773. goto errout;
  774. }
  775. /* Qualify the transport resource defaults with the
  776. * capabilities of this particular device */
  777. newxprt->sc_max_sge = min((size_t)devattr.max_sge,
  778. (size_t)RPCSVC_MAXPAGES);
  779. newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
  780. (size_t)svcrdma_max_requests);
  781. newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
  782. /*
  783. * Limit ORD based on client limit, local device limit, and
  784. * configured svcrdma limit.
  785. */
  786. newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
  787. newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
  788. newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
  789. if (IS_ERR(newxprt->sc_pd)) {
  790. dprintk("svcrdma: error creating PD for connect request\n");
  791. goto errout;
  792. }
  793. newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  794. sq_comp_handler,
  795. cq_event_handler,
  796. newxprt,
  797. newxprt->sc_sq_depth,
  798. 0);
  799. if (IS_ERR(newxprt->sc_sq_cq)) {
  800. dprintk("svcrdma: error creating SQ CQ for connect request\n");
  801. goto errout;
  802. }
  803. newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  804. rq_comp_handler,
  805. cq_event_handler,
  806. newxprt,
  807. newxprt->sc_max_requests,
  808. 0);
  809. if (IS_ERR(newxprt->sc_rq_cq)) {
  810. dprintk("svcrdma: error creating RQ CQ for connect request\n");
  811. goto errout;
  812. }
  813. memset(&qp_attr, 0, sizeof qp_attr);
  814. qp_attr.event_handler = qp_event_handler;
  815. qp_attr.qp_context = &newxprt->sc_xprt;
  816. qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
  817. qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
  818. qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
  819. qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
  820. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  821. qp_attr.qp_type = IB_QPT_RC;
  822. qp_attr.send_cq = newxprt->sc_sq_cq;
  823. qp_attr.recv_cq = newxprt->sc_rq_cq;
  824. dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
  825. " cm_id->device=%p, sc_pd->device=%p\n"
  826. " cap.max_send_wr = %d\n"
  827. " cap.max_recv_wr = %d\n"
  828. " cap.max_send_sge = %d\n"
  829. " cap.max_recv_sge = %d\n",
  830. newxprt->sc_cm_id, newxprt->sc_pd,
  831. newxprt->sc_cm_id->device, newxprt->sc_pd->device,
  832. qp_attr.cap.max_send_wr,
  833. qp_attr.cap.max_recv_wr,
  834. qp_attr.cap.max_send_sge,
  835. qp_attr.cap.max_recv_sge);
  836. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
  837. if (ret) {
  838. dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
  839. goto errout;
  840. }
  841. newxprt->sc_qp = newxprt->sc_cm_id->qp;
  842. /*
  843. * Use the most secure set of MR resources based on the
  844. * transport type and available memory management features in
  845. * the device. Here's the table implemented below:
  846. *
  847. * Fast Global DMA Remote WR
  848. * Reg LKEY MR Access
  849. * Sup'd Sup'd Needed Needed
  850. *
  851. * IWARP N N Y Y
  852. * N Y Y Y
  853. * Y N Y N
  854. * Y Y N -
  855. *
  856. * IB N N Y N
  857. * N Y N -
  858. * Y N Y N
  859. * Y Y N -
  860. *
  861. * NB: iWARP requires remote write access for the data sink
  862. * of an RDMA_READ. IB does not.
  863. */
  864. if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
  865. newxprt->sc_frmr_pg_list_len =
  866. devattr.max_fast_reg_page_list_len;
  867. newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
  868. }
  869. /*
  870. * Determine if a DMA MR is required and if so, what privs are required
  871. */
  872. switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
  873. case RDMA_TRANSPORT_IWARP:
  874. newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
  875. if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
  876. need_dma_mr = 1;
  877. dma_mr_acc =
  878. (IB_ACCESS_LOCAL_WRITE |
  879. IB_ACCESS_REMOTE_WRITE);
  880. } else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
  881. need_dma_mr = 1;
  882. dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
  883. } else
  884. need_dma_mr = 0;
  885. break;
  886. case RDMA_TRANSPORT_IB:
  887. if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
  888. need_dma_mr = 1;
  889. dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
  890. } else if (!(devattr.device_cap_flags &
  891. IB_DEVICE_LOCAL_DMA_LKEY)) {
  892. need_dma_mr = 1;
  893. dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
  894. } else
  895. need_dma_mr = 0;
  896. break;
  897. default:
  898. goto errout;
  899. }
  900. /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
  901. if (need_dma_mr) {
  902. /* Register all of physical memory */
  903. newxprt->sc_phys_mr =
  904. ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
  905. if (IS_ERR(newxprt->sc_phys_mr)) {
  906. dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
  907. ret);
  908. goto errout;
  909. }
  910. newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
  911. } else
  912. newxprt->sc_dma_lkey =
  913. newxprt->sc_cm_id->device->local_dma_lkey;
  914. /* Post receive buffers */
  915. for (i = 0; i < newxprt->sc_max_requests; i++) {
  916. ret = svc_rdma_post_recv(newxprt);
  917. if (ret) {
  918. dprintk("svcrdma: failure posting receive buffers\n");
  919. goto errout;
  920. }
  921. }
  922. /* Swap out the handler */
  923. newxprt->sc_cm_id->event_handler = rdma_cma_handler;
  924. /*
  925. * Arm the CQs for the SQ and RQ before accepting so we can't
  926. * miss the first message
  927. */
  928. ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  929. ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
  930. /* Accept Connection */
  931. set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
  932. memset(&conn_param, 0, sizeof conn_param);
  933. conn_param.responder_resources = 0;
  934. conn_param.initiator_depth = newxprt->sc_ord;
  935. ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
  936. if (ret) {
  937. dprintk("svcrdma: failed to accept new connection, ret=%d\n",
  938. ret);
  939. goto errout;
  940. }
  941. dprintk("svcrdma: new connection %p accepted with the following "
  942. "attributes:\n"
  943. " local_ip : %pI4\n"
  944. " local_port : %d\n"
  945. " remote_ip : %pI4\n"
  946. " remote_port : %d\n"
  947. " max_sge : %d\n"
  948. " sq_depth : %d\n"
  949. " max_requests : %d\n"
  950. " ord : %d\n",
  951. newxprt,
  952. &((struct sockaddr_in *)&newxprt->sc_cm_id->
  953. route.addr.src_addr)->sin_addr.s_addr,
  954. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  955. route.addr.src_addr)->sin_port),
  956. &((struct sockaddr_in *)&newxprt->sc_cm_id->
  957. route.addr.dst_addr)->sin_addr.s_addr,
  958. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  959. route.addr.dst_addr)->sin_port),
  960. newxprt->sc_max_sge,
  961. newxprt->sc_sq_depth,
  962. newxprt->sc_max_requests,
  963. newxprt->sc_ord);
  964. return &newxprt->sc_xprt;
  965. errout:
  966. dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
  967. /* Take a reference in case the DTO handler runs */
  968. svc_xprt_get(&newxprt->sc_xprt);
  969. if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
  970. ib_destroy_qp(newxprt->sc_qp);
  971. rdma_destroy_id(newxprt->sc_cm_id);
  972. /* This call to put will destroy the transport */
  973. svc_xprt_put(&newxprt->sc_xprt);
  974. return NULL;
  975. }
  976. static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
  977. {
  978. }
  979. /*
  980. * When connected, an svc_xprt has at least two references:
  981. *
  982. * - A reference held by the cm_id between the ESTABLISHED and
  983. * DISCONNECTED events. If the remote peer disconnected first, this
  984. * reference could be gone.
  985. *
  986. * - A reference held by the svc_recv code that called this function
  987. * as part of close processing.
  988. *
  989. * At a minimum one references should still be held.
  990. */
  991. static void svc_rdma_detach(struct svc_xprt *xprt)
  992. {
  993. struct svcxprt_rdma *rdma =
  994. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  995. dprintk("svc: svc_rdma_detach(%p)\n", xprt);
  996. /* Disconnect and flush posted WQE */
  997. rdma_disconnect(rdma->sc_cm_id);
  998. }
  999. static void __svc_rdma_free(struct work_struct *work)
  1000. {
  1001. struct svcxprt_rdma *rdma =
  1002. container_of(work, struct svcxprt_rdma, sc_work);
  1003. dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
  1004. /* We should only be called from kref_put */
  1005. BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
  1006. /*
  1007. * Destroy queued, but not processed read completions. Note
  1008. * that this cleanup has to be done before destroying the
  1009. * cm_id because the device ptr is needed to unmap the dma in
  1010. * svc_rdma_put_context.
  1011. */
  1012. while (!list_empty(&rdma->sc_read_complete_q)) {
  1013. struct svc_rdma_op_ctxt *ctxt;
  1014. ctxt = list_entry(rdma->sc_read_complete_q.next,
  1015. struct svc_rdma_op_ctxt,
  1016. dto_q);
  1017. list_del_init(&ctxt->dto_q);
  1018. svc_rdma_put_context(ctxt, 1);
  1019. }
  1020. /* Destroy queued, but not processed recv completions */
  1021. while (!list_empty(&rdma->sc_rq_dto_q)) {
  1022. struct svc_rdma_op_ctxt *ctxt;
  1023. ctxt = list_entry(rdma->sc_rq_dto_q.next,
  1024. struct svc_rdma_op_ctxt,
  1025. dto_q);
  1026. list_del_init(&ctxt->dto_q);
  1027. svc_rdma_put_context(ctxt, 1);
  1028. }
  1029. /* Warn if we leaked a resource or under-referenced */
  1030. WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
  1031. WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
  1032. /* De-allocate fastreg mr */
  1033. rdma_dealloc_frmr_q(rdma);
  1034. /* Destroy the QP if present (not a listener) */
  1035. if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
  1036. ib_destroy_qp(rdma->sc_qp);
  1037. if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
  1038. ib_destroy_cq(rdma->sc_sq_cq);
  1039. if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
  1040. ib_destroy_cq(rdma->sc_rq_cq);
  1041. if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
  1042. ib_dereg_mr(rdma->sc_phys_mr);
  1043. if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
  1044. ib_dealloc_pd(rdma->sc_pd);
  1045. /* Destroy the CM ID */
  1046. rdma_destroy_id(rdma->sc_cm_id);
  1047. kfree(rdma);
  1048. }
  1049. static void svc_rdma_free(struct svc_xprt *xprt)
  1050. {
  1051. struct svcxprt_rdma *rdma =
  1052. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  1053. INIT_WORK(&rdma->sc_work, __svc_rdma_free);
  1054. queue_work(svc_rdma_wq, &rdma->sc_work);
  1055. }
  1056. static int svc_rdma_has_wspace(struct svc_xprt *xprt)
  1057. {
  1058. struct svcxprt_rdma *rdma =
  1059. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  1060. /*
  1061. * If there are already waiters on the SQ,
  1062. * return false.
  1063. */
  1064. if (waitqueue_active(&rdma->sc_send_wait))
  1065. return 0;
  1066. /* Otherwise return true. */
  1067. return 1;
  1068. }
  1069. static int svc_rdma_secure_port(struct svc_rqst *rqstp)
  1070. {
  1071. return 1;
  1072. }
  1073. /*
  1074. * Attempt to register the kvec representing the RPC memory with the
  1075. * device.
  1076. *
  1077. * Returns:
  1078. * NULL : The device does not support fastreg or there were no more
  1079. * fastreg mr.
  1080. * frmr : The kvec register request was successfully posted.
  1081. * <0 : An error was encountered attempting to register the kvec.
  1082. */
  1083. int svc_rdma_fastreg(struct svcxprt_rdma *xprt,
  1084. struct svc_rdma_fastreg_mr *frmr)
  1085. {
  1086. struct ib_send_wr fastreg_wr;
  1087. u8 key;
  1088. /* Bump the key */
  1089. key = (u8)(frmr->mr->lkey & 0x000000FF);
  1090. ib_update_fast_reg_key(frmr->mr, ++key);
  1091. /* Prepare FASTREG WR */
  1092. memset(&fastreg_wr, 0, sizeof fastreg_wr);
  1093. fastreg_wr.opcode = IB_WR_FAST_REG_MR;
  1094. fastreg_wr.send_flags = IB_SEND_SIGNALED;
  1095. fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
  1096. fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
  1097. fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
  1098. fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
  1099. fastreg_wr.wr.fast_reg.length = frmr->map_len;
  1100. fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
  1101. fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
  1102. return svc_rdma_send(xprt, &fastreg_wr);
  1103. }
  1104. int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
  1105. {
  1106. struct ib_send_wr *bad_wr, *n_wr;
  1107. int wr_count;
  1108. int i;
  1109. int ret;
  1110. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  1111. return -ENOTCONN;
  1112. BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
  1113. wr_count = 1;
  1114. for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
  1115. wr_count++;
  1116. /* If the SQ is full, wait until an SQ entry is available */
  1117. while (1) {
  1118. spin_lock_bh(&xprt->sc_lock);
  1119. if (xprt->sc_sq_depth < atomic_read(&xprt->sc_sq_count) + wr_count) {
  1120. spin_unlock_bh(&xprt->sc_lock);
  1121. atomic_inc(&rdma_stat_sq_starve);
  1122. /* See if we can opportunistically reap SQ WR to make room */
  1123. sq_cq_reap(xprt);
  1124. /* Wait until SQ WR available if SQ still full */
  1125. wait_event(xprt->sc_send_wait,
  1126. atomic_read(&xprt->sc_sq_count) <
  1127. xprt->sc_sq_depth);
  1128. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  1129. return -ENOTCONN;
  1130. continue;
  1131. }
  1132. /* Take a transport ref for each WR posted */
  1133. for (i = 0; i < wr_count; i++)
  1134. svc_xprt_get(&xprt->sc_xprt);
  1135. /* Bump used SQ WR count and post */
  1136. atomic_add(wr_count, &xprt->sc_sq_count);
  1137. ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
  1138. if (ret) {
  1139. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  1140. atomic_sub(wr_count, &xprt->sc_sq_count);
  1141. for (i = 0; i < wr_count; i ++)
  1142. svc_xprt_put(&xprt->sc_xprt);
  1143. dprintk("svcrdma: failed to post SQ WR rc=%d, "
  1144. "sc_sq_count=%d, sc_sq_depth=%d\n",
  1145. ret, atomic_read(&xprt->sc_sq_count),
  1146. xprt->sc_sq_depth);
  1147. }
  1148. spin_unlock_bh(&xprt->sc_lock);
  1149. if (ret)
  1150. wake_up(&xprt->sc_send_wait);
  1151. break;
  1152. }
  1153. return ret;
  1154. }
  1155. void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
  1156. enum rpcrdma_errcode err)
  1157. {
  1158. struct ib_send_wr err_wr;
  1159. struct page *p;
  1160. struct svc_rdma_op_ctxt *ctxt;
  1161. u32 *va;
  1162. int length;
  1163. int ret;
  1164. p = svc_rdma_get_page();
  1165. va = page_address(p);
  1166. /* XDR encode error */
  1167. length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
  1168. ctxt = svc_rdma_get_context(xprt);
  1169. ctxt->direction = DMA_FROM_DEVICE;
  1170. ctxt->count = 1;
  1171. ctxt->pages[0] = p;
  1172. /* Prepare SGE for local address */
  1173. ctxt->sge[0].addr = ib_dma_map_page(xprt->sc_cm_id->device,
  1174. p, 0, length, DMA_FROM_DEVICE);
  1175. if (ib_dma_mapping_error(xprt->sc_cm_id->device, ctxt->sge[0].addr)) {
  1176. put_page(p);
  1177. svc_rdma_put_context(ctxt, 1);
  1178. return;
  1179. }
  1180. atomic_inc(&xprt->sc_dma_used);
  1181. ctxt->sge[0].lkey = xprt->sc_dma_lkey;
  1182. ctxt->sge[0].length = length;
  1183. /* Prepare SEND WR */
  1184. memset(&err_wr, 0, sizeof err_wr);
  1185. ctxt->wr_op = IB_WR_SEND;
  1186. err_wr.wr_id = (unsigned long)ctxt;
  1187. err_wr.sg_list = ctxt->sge;
  1188. err_wr.num_sge = 1;
  1189. err_wr.opcode = IB_WR_SEND;
  1190. err_wr.send_flags = IB_SEND_SIGNALED;
  1191. /* Post It */
  1192. ret = svc_rdma_send(xprt, &err_wr);
  1193. if (ret) {
  1194. dprintk("svcrdma: Error %d posting send for protocol error\n",
  1195. ret);
  1196. svc_rdma_unmap_dma(ctxt);
  1197. svc_rdma_put_context(ctxt, 1);
  1198. }
  1199. }