svc_rdma_transport.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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/addr.h>
  44. #include <linux/sunrpc/debug.h>
  45. #include <linux/sunrpc/rpc_rdma.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/sched.h>
  48. #include <linux/slab.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/workqueue.h>
  51. #include <rdma/ib_verbs.h>
  52. #include <rdma/rdma_cm.h>
  53. #include <linux/sunrpc/svc_rdma.h>
  54. #include <linux/export.h>
  55. #include "xprt_rdma.h"
  56. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  57. static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *, int);
  58. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  59. struct net *net,
  60. struct sockaddr *sa, int salen,
  61. int flags);
  62. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
  63. static void svc_rdma_release_rqst(struct svc_rqst *);
  64. static void svc_rdma_detach(struct svc_xprt *xprt);
  65. static void svc_rdma_free(struct svc_xprt *xprt);
  66. static int svc_rdma_has_wspace(struct svc_xprt *xprt);
  67. static int svc_rdma_secure_port(struct svc_rqst *);
  68. static void svc_rdma_kill_temp_xprt(struct svc_xprt *);
  69. static struct svc_xprt_ops svc_rdma_ops = {
  70. .xpo_create = svc_rdma_create,
  71. .xpo_recvfrom = svc_rdma_recvfrom,
  72. .xpo_sendto = svc_rdma_sendto,
  73. .xpo_release_rqst = svc_rdma_release_rqst,
  74. .xpo_detach = svc_rdma_detach,
  75. .xpo_free = svc_rdma_free,
  76. .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
  77. .xpo_has_wspace = svc_rdma_has_wspace,
  78. .xpo_accept = svc_rdma_accept,
  79. .xpo_secure_port = svc_rdma_secure_port,
  80. .xpo_kill_temp_xprt = svc_rdma_kill_temp_xprt,
  81. };
  82. struct svc_xprt_class svc_rdma_class = {
  83. .xcl_name = "rdma",
  84. .xcl_owner = THIS_MODULE,
  85. .xcl_ops = &svc_rdma_ops,
  86. .xcl_max_payload = RPCSVC_MAXPAYLOAD_RDMA,
  87. .xcl_ident = XPRT_TRANSPORT_RDMA,
  88. };
  89. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  90. static struct svc_xprt *svc_rdma_bc_create(struct svc_serv *, struct net *,
  91. struct sockaddr *, int, int);
  92. static void svc_rdma_bc_detach(struct svc_xprt *);
  93. static void svc_rdma_bc_free(struct svc_xprt *);
  94. static struct svc_xprt_ops svc_rdma_bc_ops = {
  95. .xpo_create = svc_rdma_bc_create,
  96. .xpo_detach = svc_rdma_bc_detach,
  97. .xpo_free = svc_rdma_bc_free,
  98. .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
  99. .xpo_secure_port = svc_rdma_secure_port,
  100. };
  101. struct svc_xprt_class svc_rdma_bc_class = {
  102. .xcl_name = "rdma-bc",
  103. .xcl_owner = THIS_MODULE,
  104. .xcl_ops = &svc_rdma_bc_ops,
  105. .xcl_max_payload = (1024 - RPCRDMA_HDRLEN_MIN)
  106. };
  107. static struct svc_xprt *svc_rdma_bc_create(struct svc_serv *serv,
  108. struct net *net,
  109. struct sockaddr *sa, int salen,
  110. int flags)
  111. {
  112. struct svcxprt_rdma *cma_xprt;
  113. struct svc_xprt *xprt;
  114. cma_xprt = rdma_create_xprt(serv, 0);
  115. if (!cma_xprt)
  116. return ERR_PTR(-ENOMEM);
  117. xprt = &cma_xprt->sc_xprt;
  118. svc_xprt_init(net, &svc_rdma_bc_class, xprt, serv);
  119. serv->sv_bc_xprt = xprt;
  120. dprintk("svcrdma: %s(%p)\n", __func__, xprt);
  121. return xprt;
  122. }
  123. static void svc_rdma_bc_detach(struct svc_xprt *xprt)
  124. {
  125. dprintk("svcrdma: %s(%p)\n", __func__, xprt);
  126. }
  127. static void svc_rdma_bc_free(struct svc_xprt *xprt)
  128. {
  129. struct svcxprt_rdma *rdma =
  130. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  131. dprintk("svcrdma: %s(%p)\n", __func__, xprt);
  132. if (xprt)
  133. kfree(rdma);
  134. }
  135. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  136. static struct svc_rdma_op_ctxt *alloc_ctxt(struct svcxprt_rdma *xprt,
  137. gfp_t flags)
  138. {
  139. struct svc_rdma_op_ctxt *ctxt;
  140. ctxt = kmalloc(sizeof(*ctxt), flags);
  141. if (ctxt) {
  142. ctxt->xprt = xprt;
  143. INIT_LIST_HEAD(&ctxt->free);
  144. INIT_LIST_HEAD(&ctxt->dto_q);
  145. }
  146. return ctxt;
  147. }
  148. static bool svc_rdma_prealloc_ctxts(struct svcxprt_rdma *xprt)
  149. {
  150. unsigned int i;
  151. /* Each RPC/RDMA credit can consume a number of send
  152. * and receive WQEs. One ctxt is allocated for each.
  153. */
  154. i = xprt->sc_sq_depth + xprt->sc_rq_depth;
  155. while (i--) {
  156. struct svc_rdma_op_ctxt *ctxt;
  157. ctxt = alloc_ctxt(xprt, GFP_KERNEL);
  158. if (!ctxt) {
  159. dprintk("svcrdma: No memory for RDMA ctxt\n");
  160. return false;
  161. }
  162. list_add(&ctxt->free, &xprt->sc_ctxts);
  163. }
  164. return true;
  165. }
  166. struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
  167. {
  168. struct svc_rdma_op_ctxt *ctxt = NULL;
  169. spin_lock_bh(&xprt->sc_ctxt_lock);
  170. xprt->sc_ctxt_used++;
  171. if (list_empty(&xprt->sc_ctxts))
  172. goto out_empty;
  173. ctxt = list_first_entry(&xprt->sc_ctxts,
  174. struct svc_rdma_op_ctxt, free);
  175. list_del_init(&ctxt->free);
  176. spin_unlock_bh(&xprt->sc_ctxt_lock);
  177. out:
  178. ctxt->count = 0;
  179. ctxt->mapped_sges = 0;
  180. ctxt->frmr = NULL;
  181. return ctxt;
  182. out_empty:
  183. /* Either pre-allocation missed the mark, or send
  184. * queue accounting is broken.
  185. */
  186. spin_unlock_bh(&xprt->sc_ctxt_lock);
  187. ctxt = alloc_ctxt(xprt, GFP_NOIO);
  188. if (ctxt)
  189. goto out;
  190. spin_lock_bh(&xprt->sc_ctxt_lock);
  191. xprt->sc_ctxt_used--;
  192. spin_unlock_bh(&xprt->sc_ctxt_lock);
  193. WARN_ONCE(1, "svcrdma: empty RDMA ctxt list?\n");
  194. return NULL;
  195. }
  196. void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
  197. {
  198. struct svcxprt_rdma *xprt = ctxt->xprt;
  199. struct ib_device *device = xprt->sc_cm_id->device;
  200. u32 lkey = xprt->sc_pd->local_dma_lkey;
  201. unsigned int i;
  202. for (i = 0; i < ctxt->mapped_sges; i++) {
  203. /*
  204. * Unmap the DMA addr in the SGE if the lkey matches
  205. * the local_dma_lkey, otherwise, ignore it since it is
  206. * an FRMR lkey and will be unmapped later when the
  207. * last WR that uses it completes.
  208. */
  209. if (ctxt->sge[i].lkey == lkey)
  210. ib_dma_unmap_page(device,
  211. ctxt->sge[i].addr,
  212. ctxt->sge[i].length,
  213. ctxt->direction);
  214. }
  215. ctxt->mapped_sges = 0;
  216. }
  217. void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
  218. {
  219. struct svcxprt_rdma *xprt = ctxt->xprt;
  220. int i;
  221. if (free_pages)
  222. for (i = 0; i < ctxt->count; i++)
  223. put_page(ctxt->pages[i]);
  224. spin_lock_bh(&xprt->sc_ctxt_lock);
  225. xprt->sc_ctxt_used--;
  226. list_add(&ctxt->free, &xprt->sc_ctxts);
  227. spin_unlock_bh(&xprt->sc_ctxt_lock);
  228. }
  229. static void svc_rdma_destroy_ctxts(struct svcxprt_rdma *xprt)
  230. {
  231. while (!list_empty(&xprt->sc_ctxts)) {
  232. struct svc_rdma_op_ctxt *ctxt;
  233. ctxt = list_first_entry(&xprt->sc_ctxts,
  234. struct svc_rdma_op_ctxt, free);
  235. list_del(&ctxt->free);
  236. kfree(ctxt);
  237. }
  238. }
  239. static struct svc_rdma_req_map *alloc_req_map(gfp_t flags)
  240. {
  241. struct svc_rdma_req_map *map;
  242. map = kmalloc(sizeof(*map), flags);
  243. if (map)
  244. INIT_LIST_HEAD(&map->free);
  245. return map;
  246. }
  247. static bool svc_rdma_prealloc_maps(struct svcxprt_rdma *xprt)
  248. {
  249. unsigned int i;
  250. /* One for each receive buffer on this connection. */
  251. i = xprt->sc_max_requests;
  252. while (i--) {
  253. struct svc_rdma_req_map *map;
  254. map = alloc_req_map(GFP_KERNEL);
  255. if (!map) {
  256. dprintk("svcrdma: No memory for request map\n");
  257. return false;
  258. }
  259. list_add(&map->free, &xprt->sc_maps);
  260. }
  261. return true;
  262. }
  263. struct svc_rdma_req_map *svc_rdma_get_req_map(struct svcxprt_rdma *xprt)
  264. {
  265. struct svc_rdma_req_map *map = NULL;
  266. spin_lock(&xprt->sc_map_lock);
  267. if (list_empty(&xprt->sc_maps))
  268. goto out_empty;
  269. map = list_first_entry(&xprt->sc_maps,
  270. struct svc_rdma_req_map, free);
  271. list_del_init(&map->free);
  272. spin_unlock(&xprt->sc_map_lock);
  273. out:
  274. map->count = 0;
  275. return map;
  276. out_empty:
  277. spin_unlock(&xprt->sc_map_lock);
  278. /* Pre-allocation amount was incorrect */
  279. map = alloc_req_map(GFP_NOIO);
  280. if (map)
  281. goto out;
  282. WARN_ONCE(1, "svcrdma: empty request map list?\n");
  283. return NULL;
  284. }
  285. void svc_rdma_put_req_map(struct svcxprt_rdma *xprt,
  286. struct svc_rdma_req_map *map)
  287. {
  288. spin_lock(&xprt->sc_map_lock);
  289. list_add(&map->free, &xprt->sc_maps);
  290. spin_unlock(&xprt->sc_map_lock);
  291. }
  292. static void svc_rdma_destroy_maps(struct svcxprt_rdma *xprt)
  293. {
  294. while (!list_empty(&xprt->sc_maps)) {
  295. struct svc_rdma_req_map *map;
  296. map = list_first_entry(&xprt->sc_maps,
  297. struct svc_rdma_req_map, free);
  298. list_del(&map->free);
  299. kfree(map);
  300. }
  301. }
  302. /* QP event handler */
  303. static void qp_event_handler(struct ib_event *event, void *context)
  304. {
  305. struct svc_xprt *xprt = context;
  306. switch (event->event) {
  307. /* These are considered benign events */
  308. case IB_EVENT_PATH_MIG:
  309. case IB_EVENT_COMM_EST:
  310. case IB_EVENT_SQ_DRAINED:
  311. case IB_EVENT_QP_LAST_WQE_REACHED:
  312. dprintk("svcrdma: QP event %s (%d) received for QP=%p\n",
  313. ib_event_msg(event->event), event->event,
  314. event->element.qp);
  315. break;
  316. /* These are considered fatal events */
  317. case IB_EVENT_PATH_MIG_ERR:
  318. case IB_EVENT_QP_FATAL:
  319. case IB_EVENT_QP_REQ_ERR:
  320. case IB_EVENT_QP_ACCESS_ERR:
  321. case IB_EVENT_DEVICE_FATAL:
  322. default:
  323. dprintk("svcrdma: QP ERROR event %s (%d) received for QP=%p, "
  324. "closing transport\n",
  325. ib_event_msg(event->event), event->event,
  326. event->element.qp);
  327. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  328. break;
  329. }
  330. }
  331. /**
  332. * svc_rdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
  333. * @cq: completion queue
  334. * @wc: completed WR
  335. *
  336. */
  337. static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
  338. {
  339. struct svcxprt_rdma *xprt = cq->cq_context;
  340. struct ib_cqe *cqe = wc->wr_cqe;
  341. struct svc_rdma_op_ctxt *ctxt;
  342. /* WARNING: Only wc->wr_cqe and wc->status are reliable */
  343. ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
  344. svc_rdma_unmap_dma(ctxt);
  345. if (wc->status != IB_WC_SUCCESS)
  346. goto flushed;
  347. /* All wc fields are now known to be valid */
  348. ctxt->byte_len = wc->byte_len;
  349. spin_lock(&xprt->sc_rq_dto_lock);
  350. list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
  351. spin_unlock(&xprt->sc_rq_dto_lock);
  352. set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
  353. if (test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
  354. goto out;
  355. svc_xprt_enqueue(&xprt->sc_xprt);
  356. goto out;
  357. flushed:
  358. if (wc->status != IB_WC_WR_FLUSH_ERR)
  359. pr_warn("svcrdma: receive: %s (%u/0x%x)\n",
  360. ib_wc_status_msg(wc->status),
  361. wc->status, wc->vendor_err);
  362. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  363. svc_rdma_put_context(ctxt, 1);
  364. out:
  365. svc_xprt_put(&xprt->sc_xprt);
  366. }
  367. static void svc_rdma_send_wc_common(struct svcxprt_rdma *xprt,
  368. struct ib_wc *wc,
  369. const char *opname)
  370. {
  371. if (wc->status != IB_WC_SUCCESS)
  372. goto err;
  373. out:
  374. atomic_inc(&xprt->sc_sq_avail);
  375. wake_up(&xprt->sc_send_wait);
  376. return;
  377. err:
  378. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  379. if (wc->status != IB_WC_WR_FLUSH_ERR)
  380. pr_err("svcrdma: %s: %s (%u/0x%x)\n",
  381. opname, ib_wc_status_msg(wc->status),
  382. wc->status, wc->vendor_err);
  383. goto out;
  384. }
  385. static void svc_rdma_send_wc_common_put(struct ib_cq *cq, struct ib_wc *wc,
  386. const char *opname)
  387. {
  388. struct svcxprt_rdma *xprt = cq->cq_context;
  389. svc_rdma_send_wc_common(xprt, wc, opname);
  390. svc_xprt_put(&xprt->sc_xprt);
  391. }
  392. /**
  393. * svc_rdma_wc_send - Invoked by RDMA provider for each polled Send WC
  394. * @cq: completion queue
  395. * @wc: completed WR
  396. *
  397. */
  398. void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
  399. {
  400. struct ib_cqe *cqe = wc->wr_cqe;
  401. struct svc_rdma_op_ctxt *ctxt;
  402. svc_rdma_send_wc_common_put(cq, wc, "send");
  403. ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
  404. svc_rdma_unmap_dma(ctxt);
  405. svc_rdma_put_context(ctxt, 1);
  406. }
  407. /**
  408. * svc_rdma_wc_write - Invoked by RDMA provider for each polled Write WC
  409. * @cq: completion queue
  410. * @wc: completed WR
  411. *
  412. */
  413. void svc_rdma_wc_write(struct ib_cq *cq, struct ib_wc *wc)
  414. {
  415. struct ib_cqe *cqe = wc->wr_cqe;
  416. struct svc_rdma_op_ctxt *ctxt;
  417. svc_rdma_send_wc_common_put(cq, wc, "write");
  418. ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
  419. svc_rdma_unmap_dma(ctxt);
  420. svc_rdma_put_context(ctxt, 0);
  421. }
  422. /**
  423. * svc_rdma_wc_reg - Invoked by RDMA provider for each polled FASTREG WC
  424. * @cq: completion queue
  425. * @wc: completed WR
  426. *
  427. */
  428. void svc_rdma_wc_reg(struct ib_cq *cq, struct ib_wc *wc)
  429. {
  430. svc_rdma_send_wc_common_put(cq, wc, "fastreg");
  431. }
  432. /**
  433. * svc_rdma_wc_read - Invoked by RDMA provider for each polled Read WC
  434. * @cq: completion queue
  435. * @wc: completed WR
  436. *
  437. */
  438. void svc_rdma_wc_read(struct ib_cq *cq, struct ib_wc *wc)
  439. {
  440. struct svcxprt_rdma *xprt = cq->cq_context;
  441. struct ib_cqe *cqe = wc->wr_cqe;
  442. struct svc_rdma_op_ctxt *ctxt;
  443. svc_rdma_send_wc_common(xprt, wc, "read");
  444. ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
  445. svc_rdma_unmap_dma(ctxt);
  446. svc_rdma_put_frmr(xprt, ctxt->frmr);
  447. if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
  448. struct svc_rdma_op_ctxt *read_hdr;
  449. read_hdr = ctxt->read_hdr;
  450. spin_lock(&xprt->sc_rq_dto_lock);
  451. list_add_tail(&read_hdr->dto_q,
  452. &xprt->sc_read_complete_q);
  453. spin_unlock(&xprt->sc_rq_dto_lock);
  454. set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
  455. svc_xprt_enqueue(&xprt->sc_xprt);
  456. }
  457. svc_rdma_put_context(ctxt, 0);
  458. svc_xprt_put(&xprt->sc_xprt);
  459. }
  460. /**
  461. * svc_rdma_wc_inv - Invoked by RDMA provider for each polled LOCAL_INV WC
  462. * @cq: completion queue
  463. * @wc: completed WR
  464. *
  465. */
  466. void svc_rdma_wc_inv(struct ib_cq *cq, struct ib_wc *wc)
  467. {
  468. svc_rdma_send_wc_common_put(cq, wc, "localInv");
  469. }
  470. static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
  471. int listener)
  472. {
  473. struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
  474. if (!cma_xprt)
  475. return NULL;
  476. svc_xprt_init(&init_net, &svc_rdma_class, &cma_xprt->sc_xprt, serv);
  477. INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
  478. INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
  479. INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
  480. INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
  481. INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
  482. INIT_LIST_HEAD(&cma_xprt->sc_ctxts);
  483. INIT_LIST_HEAD(&cma_xprt->sc_maps);
  484. init_waitqueue_head(&cma_xprt->sc_send_wait);
  485. spin_lock_init(&cma_xprt->sc_lock);
  486. spin_lock_init(&cma_xprt->sc_rq_dto_lock);
  487. spin_lock_init(&cma_xprt->sc_frmr_q_lock);
  488. spin_lock_init(&cma_xprt->sc_ctxt_lock);
  489. spin_lock_init(&cma_xprt->sc_map_lock);
  490. if (listener)
  491. set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
  492. return cma_xprt;
  493. }
  494. int svc_rdma_post_recv(struct svcxprt_rdma *xprt, gfp_t flags)
  495. {
  496. struct ib_recv_wr recv_wr, *bad_recv_wr;
  497. struct svc_rdma_op_ctxt *ctxt;
  498. struct page *page;
  499. dma_addr_t pa;
  500. int sge_no;
  501. int buflen;
  502. int ret;
  503. ctxt = svc_rdma_get_context(xprt);
  504. buflen = 0;
  505. ctxt->direction = DMA_FROM_DEVICE;
  506. ctxt->cqe.done = svc_rdma_wc_receive;
  507. for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
  508. if (sge_no >= xprt->sc_max_sge) {
  509. pr_err("svcrdma: Too many sges (%d)\n", sge_no);
  510. goto err_put_ctxt;
  511. }
  512. page = alloc_page(flags);
  513. if (!page)
  514. goto err_put_ctxt;
  515. ctxt->pages[sge_no] = page;
  516. pa = ib_dma_map_page(xprt->sc_cm_id->device,
  517. page, 0, PAGE_SIZE,
  518. DMA_FROM_DEVICE);
  519. if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
  520. goto err_put_ctxt;
  521. svc_rdma_count_mappings(xprt, ctxt);
  522. ctxt->sge[sge_no].addr = pa;
  523. ctxt->sge[sge_no].length = PAGE_SIZE;
  524. ctxt->sge[sge_no].lkey = xprt->sc_pd->local_dma_lkey;
  525. ctxt->count = sge_no + 1;
  526. buflen += PAGE_SIZE;
  527. }
  528. recv_wr.next = NULL;
  529. recv_wr.sg_list = &ctxt->sge[0];
  530. recv_wr.num_sge = ctxt->count;
  531. recv_wr.wr_cqe = &ctxt->cqe;
  532. svc_xprt_get(&xprt->sc_xprt);
  533. ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
  534. if (ret) {
  535. svc_rdma_unmap_dma(ctxt);
  536. svc_rdma_put_context(ctxt, 1);
  537. svc_xprt_put(&xprt->sc_xprt);
  538. }
  539. return ret;
  540. err_put_ctxt:
  541. svc_rdma_unmap_dma(ctxt);
  542. svc_rdma_put_context(ctxt, 1);
  543. return -ENOMEM;
  544. }
  545. int svc_rdma_repost_recv(struct svcxprt_rdma *xprt, gfp_t flags)
  546. {
  547. int ret = 0;
  548. ret = svc_rdma_post_recv(xprt, flags);
  549. if (ret) {
  550. pr_err("svcrdma: could not post a receive buffer, err=%d.\n",
  551. ret);
  552. pr_err("svcrdma: closing transport %p.\n", xprt);
  553. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  554. ret = -ENOTCONN;
  555. }
  556. return ret;
  557. }
  558. static void
  559. svc_rdma_parse_connect_private(struct svcxprt_rdma *newxprt,
  560. struct rdma_conn_param *param)
  561. {
  562. const struct rpcrdma_connect_private *pmsg = param->private_data;
  563. if (pmsg &&
  564. pmsg->cp_magic == rpcrdma_cmp_magic &&
  565. pmsg->cp_version == RPCRDMA_CMP_VERSION) {
  566. newxprt->sc_snd_w_inv = pmsg->cp_flags &
  567. RPCRDMA_CMP_F_SND_W_INV_OK;
  568. dprintk("svcrdma: client send_size %u, recv_size %u "
  569. "remote inv %ssupported\n",
  570. rpcrdma_decode_buffer_size(pmsg->cp_send_size),
  571. rpcrdma_decode_buffer_size(pmsg->cp_recv_size),
  572. newxprt->sc_snd_w_inv ? "" : "un");
  573. }
  574. }
  575. /*
  576. * This function handles the CONNECT_REQUEST event on a listening
  577. * endpoint. It is passed the cma_id for the _new_ connection. The context in
  578. * this cma_id is inherited from the listening cma_id and is the svc_xprt
  579. * structure for the listening endpoint.
  580. *
  581. * This function creates a new xprt for the new connection and enqueues it on
  582. * the accept queue for the listent xprt. When the listen thread is kicked, it
  583. * will call the recvfrom method on the listen xprt which will accept the new
  584. * connection.
  585. */
  586. static void handle_connect_req(struct rdma_cm_id *new_cma_id,
  587. struct rdma_conn_param *param)
  588. {
  589. struct svcxprt_rdma *listen_xprt = new_cma_id->context;
  590. struct svcxprt_rdma *newxprt;
  591. struct sockaddr *sa;
  592. /* Create a new transport */
  593. newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
  594. if (!newxprt) {
  595. dprintk("svcrdma: failed to create new transport\n");
  596. return;
  597. }
  598. newxprt->sc_cm_id = new_cma_id;
  599. new_cma_id->context = newxprt;
  600. dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
  601. newxprt, newxprt->sc_cm_id, listen_xprt);
  602. svc_rdma_parse_connect_private(newxprt, param);
  603. /* Save client advertised inbound read limit for use later in accept. */
  604. newxprt->sc_ord = param->initiator_depth;
  605. /* Set the local and remote addresses in the transport */
  606. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
  607. svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  608. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
  609. svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  610. /*
  611. * Enqueue the new transport on the accept queue of the listening
  612. * transport
  613. */
  614. spin_lock_bh(&listen_xprt->sc_lock);
  615. list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
  616. spin_unlock_bh(&listen_xprt->sc_lock);
  617. set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
  618. svc_xprt_enqueue(&listen_xprt->sc_xprt);
  619. }
  620. /*
  621. * Handles events generated on the listening endpoint. These events will be
  622. * either be incoming connect requests or adapter removal events.
  623. */
  624. static int rdma_listen_handler(struct rdma_cm_id *cma_id,
  625. struct rdma_cm_event *event)
  626. {
  627. struct svcxprt_rdma *xprt = cma_id->context;
  628. int ret = 0;
  629. switch (event->event) {
  630. case RDMA_CM_EVENT_CONNECT_REQUEST:
  631. dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
  632. "event = %s (%d)\n", cma_id, cma_id->context,
  633. rdma_event_msg(event->event), event->event);
  634. handle_connect_req(cma_id, &event->param.conn);
  635. break;
  636. case RDMA_CM_EVENT_ESTABLISHED:
  637. /* Accept complete */
  638. dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
  639. "cm_id=%p\n", xprt, cma_id);
  640. break;
  641. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  642. dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
  643. xprt, cma_id);
  644. if (xprt)
  645. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  646. break;
  647. default:
  648. dprintk("svcrdma: Unexpected event on listening endpoint %p, "
  649. "event = %s (%d)\n", cma_id,
  650. rdma_event_msg(event->event), event->event);
  651. break;
  652. }
  653. return ret;
  654. }
  655. static int rdma_cma_handler(struct rdma_cm_id *cma_id,
  656. struct rdma_cm_event *event)
  657. {
  658. struct svc_xprt *xprt = cma_id->context;
  659. struct svcxprt_rdma *rdma =
  660. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  661. switch (event->event) {
  662. case RDMA_CM_EVENT_ESTABLISHED:
  663. /* Accept complete */
  664. svc_xprt_get(xprt);
  665. dprintk("svcrdma: Connection completed on DTO xprt=%p, "
  666. "cm_id=%p\n", xprt, cma_id);
  667. clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
  668. svc_xprt_enqueue(xprt);
  669. break;
  670. case RDMA_CM_EVENT_DISCONNECTED:
  671. dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
  672. xprt, cma_id);
  673. if (xprt) {
  674. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  675. svc_xprt_enqueue(xprt);
  676. svc_xprt_put(xprt);
  677. }
  678. break;
  679. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  680. dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
  681. "event = %s (%d)\n", cma_id, xprt,
  682. rdma_event_msg(event->event), event->event);
  683. if (xprt) {
  684. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  685. svc_xprt_enqueue(xprt);
  686. svc_xprt_put(xprt);
  687. }
  688. break;
  689. default:
  690. dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
  691. "event = %s (%d)\n", cma_id,
  692. rdma_event_msg(event->event), event->event);
  693. break;
  694. }
  695. return 0;
  696. }
  697. /*
  698. * Create a listening RDMA service endpoint.
  699. */
  700. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  701. struct net *net,
  702. struct sockaddr *sa, int salen,
  703. int flags)
  704. {
  705. struct rdma_cm_id *listen_id;
  706. struct svcxprt_rdma *cma_xprt;
  707. int ret;
  708. dprintk("svcrdma: Creating RDMA socket\n");
  709. if ((sa->sa_family != AF_INET) && (sa->sa_family != AF_INET6)) {
  710. dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
  711. return ERR_PTR(-EAFNOSUPPORT);
  712. }
  713. cma_xprt = rdma_create_xprt(serv, 1);
  714. if (!cma_xprt)
  715. return ERR_PTR(-ENOMEM);
  716. listen_id = rdma_create_id(&init_net, rdma_listen_handler, cma_xprt,
  717. RDMA_PS_TCP, IB_QPT_RC);
  718. if (IS_ERR(listen_id)) {
  719. ret = PTR_ERR(listen_id);
  720. dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
  721. goto err0;
  722. }
  723. /* Allow both IPv4 and IPv6 sockets to bind a single port
  724. * at the same time.
  725. */
  726. #if IS_ENABLED(CONFIG_IPV6)
  727. ret = rdma_set_afonly(listen_id, 1);
  728. if (ret) {
  729. dprintk("svcrdma: rdma_set_afonly failed = %d\n", ret);
  730. goto err1;
  731. }
  732. #endif
  733. ret = rdma_bind_addr(listen_id, sa);
  734. if (ret) {
  735. dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
  736. goto err1;
  737. }
  738. cma_xprt->sc_cm_id = listen_id;
  739. ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
  740. if (ret) {
  741. dprintk("svcrdma: rdma_listen failed = %d\n", ret);
  742. goto err1;
  743. }
  744. /*
  745. * We need to use the address from the cm_id in case the
  746. * caller specified 0 for the port number.
  747. */
  748. sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
  749. svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
  750. return &cma_xprt->sc_xprt;
  751. err1:
  752. rdma_destroy_id(listen_id);
  753. err0:
  754. kfree(cma_xprt);
  755. return ERR_PTR(ret);
  756. }
  757. static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
  758. {
  759. struct ib_mr *mr;
  760. struct scatterlist *sg;
  761. struct svc_rdma_fastreg_mr *frmr;
  762. u32 num_sg;
  763. frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
  764. if (!frmr)
  765. goto err;
  766. num_sg = min_t(u32, RPCSVC_MAXPAGES, xprt->sc_frmr_pg_list_len);
  767. mr = ib_alloc_mr(xprt->sc_pd, IB_MR_TYPE_MEM_REG, num_sg);
  768. if (IS_ERR(mr))
  769. goto err_free_frmr;
  770. sg = kcalloc(RPCSVC_MAXPAGES, sizeof(*sg), GFP_KERNEL);
  771. if (!sg)
  772. goto err_free_mr;
  773. sg_init_table(sg, RPCSVC_MAXPAGES);
  774. frmr->mr = mr;
  775. frmr->sg = sg;
  776. INIT_LIST_HEAD(&frmr->frmr_list);
  777. return frmr;
  778. err_free_mr:
  779. ib_dereg_mr(mr);
  780. err_free_frmr:
  781. kfree(frmr);
  782. err:
  783. return ERR_PTR(-ENOMEM);
  784. }
  785. static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
  786. {
  787. struct svc_rdma_fastreg_mr *frmr;
  788. while (!list_empty(&xprt->sc_frmr_q)) {
  789. frmr = list_entry(xprt->sc_frmr_q.next,
  790. struct svc_rdma_fastreg_mr, frmr_list);
  791. list_del_init(&frmr->frmr_list);
  792. kfree(frmr->sg);
  793. ib_dereg_mr(frmr->mr);
  794. kfree(frmr);
  795. }
  796. }
  797. struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
  798. {
  799. struct svc_rdma_fastreg_mr *frmr = NULL;
  800. spin_lock_bh(&rdma->sc_frmr_q_lock);
  801. if (!list_empty(&rdma->sc_frmr_q)) {
  802. frmr = list_entry(rdma->sc_frmr_q.next,
  803. struct svc_rdma_fastreg_mr, frmr_list);
  804. list_del_init(&frmr->frmr_list);
  805. frmr->sg_nents = 0;
  806. }
  807. spin_unlock_bh(&rdma->sc_frmr_q_lock);
  808. if (frmr)
  809. return frmr;
  810. return rdma_alloc_frmr(rdma);
  811. }
  812. void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
  813. struct svc_rdma_fastreg_mr *frmr)
  814. {
  815. if (frmr) {
  816. ib_dma_unmap_sg(rdma->sc_cm_id->device,
  817. frmr->sg, frmr->sg_nents, frmr->direction);
  818. spin_lock_bh(&rdma->sc_frmr_q_lock);
  819. WARN_ON_ONCE(!list_empty(&frmr->frmr_list));
  820. list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
  821. spin_unlock_bh(&rdma->sc_frmr_q_lock);
  822. }
  823. }
  824. /*
  825. * This is the xpo_recvfrom function for listening endpoints. Its
  826. * purpose is to accept incoming connections. The CMA callback handler
  827. * has already created a new transport and attached it to the new CMA
  828. * ID.
  829. *
  830. * There is a queue of pending connections hung on the listening
  831. * transport. This queue contains the new svc_xprt structure. This
  832. * function takes svc_xprt structures off the accept_q and completes
  833. * the connection.
  834. */
  835. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
  836. {
  837. struct svcxprt_rdma *listen_rdma;
  838. struct svcxprt_rdma *newxprt = NULL;
  839. struct rdma_conn_param conn_param;
  840. struct rpcrdma_connect_private pmsg;
  841. struct ib_qp_init_attr qp_attr;
  842. struct ib_device *dev;
  843. struct sockaddr *sap;
  844. unsigned int i;
  845. int ret = 0;
  846. listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
  847. clear_bit(XPT_CONN, &xprt->xpt_flags);
  848. /* Get the next entry off the accept list */
  849. spin_lock_bh(&listen_rdma->sc_lock);
  850. if (!list_empty(&listen_rdma->sc_accept_q)) {
  851. newxprt = list_entry(listen_rdma->sc_accept_q.next,
  852. struct svcxprt_rdma, sc_accept_q);
  853. list_del_init(&newxprt->sc_accept_q);
  854. }
  855. if (!list_empty(&listen_rdma->sc_accept_q))
  856. set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
  857. spin_unlock_bh(&listen_rdma->sc_lock);
  858. if (!newxprt)
  859. return NULL;
  860. dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
  861. newxprt, newxprt->sc_cm_id);
  862. dev = newxprt->sc_cm_id->device;
  863. /* Qualify the transport resource defaults with the
  864. * capabilities of this particular device */
  865. newxprt->sc_max_sge = min((size_t)dev->attrs.max_sge,
  866. (size_t)RPCSVC_MAXPAGES);
  867. newxprt->sc_max_sge_rd = min_t(size_t, dev->attrs.max_sge_rd,
  868. RPCSVC_MAXPAGES);
  869. newxprt->sc_max_req_size = svcrdma_max_req_size;
  870. newxprt->sc_max_requests = min_t(u32, dev->attrs.max_qp_wr,
  871. svcrdma_max_requests);
  872. newxprt->sc_max_bc_requests = min_t(u32, dev->attrs.max_qp_wr,
  873. svcrdma_max_bc_requests);
  874. newxprt->sc_rq_depth = newxprt->sc_max_requests +
  875. newxprt->sc_max_bc_requests;
  876. newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_rq_depth;
  877. atomic_set(&newxprt->sc_sq_avail, newxprt->sc_sq_depth);
  878. if (!svc_rdma_prealloc_ctxts(newxprt))
  879. goto errout;
  880. if (!svc_rdma_prealloc_maps(newxprt))
  881. goto errout;
  882. /*
  883. * Limit ORD based on client limit, local device limit, and
  884. * configured svcrdma limit.
  885. */
  886. newxprt->sc_ord = min_t(size_t, dev->attrs.max_qp_rd_atom, newxprt->sc_ord);
  887. newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
  888. newxprt->sc_pd = ib_alloc_pd(dev, 0);
  889. if (IS_ERR(newxprt->sc_pd)) {
  890. dprintk("svcrdma: error creating PD for connect request\n");
  891. goto errout;
  892. }
  893. newxprt->sc_sq_cq = ib_alloc_cq(dev, newxprt, newxprt->sc_sq_depth,
  894. 0, IB_POLL_SOFTIRQ);
  895. if (IS_ERR(newxprt->sc_sq_cq)) {
  896. dprintk("svcrdma: error creating SQ CQ for connect request\n");
  897. goto errout;
  898. }
  899. newxprt->sc_rq_cq = ib_alloc_cq(dev, newxprt, newxprt->sc_rq_depth,
  900. 0, IB_POLL_SOFTIRQ);
  901. if (IS_ERR(newxprt->sc_rq_cq)) {
  902. dprintk("svcrdma: error creating RQ CQ for connect request\n");
  903. goto errout;
  904. }
  905. memset(&qp_attr, 0, sizeof qp_attr);
  906. qp_attr.event_handler = qp_event_handler;
  907. qp_attr.qp_context = &newxprt->sc_xprt;
  908. qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
  909. qp_attr.cap.max_recv_wr = newxprt->sc_rq_depth;
  910. qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
  911. qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
  912. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  913. qp_attr.qp_type = IB_QPT_RC;
  914. qp_attr.send_cq = newxprt->sc_sq_cq;
  915. qp_attr.recv_cq = newxprt->sc_rq_cq;
  916. dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n",
  917. newxprt->sc_cm_id, newxprt->sc_pd);
  918. dprintk(" cap.max_send_wr = %d, cap.max_recv_wr = %d\n",
  919. qp_attr.cap.max_send_wr, qp_attr.cap.max_recv_wr);
  920. dprintk(" cap.max_send_sge = %d, cap.max_recv_sge = %d\n",
  921. qp_attr.cap.max_send_sge, qp_attr.cap.max_recv_sge);
  922. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
  923. if (ret) {
  924. dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
  925. goto errout;
  926. }
  927. newxprt->sc_qp = newxprt->sc_cm_id->qp;
  928. /*
  929. * Use the most secure set of MR resources based on the
  930. * transport type and available memory management features in
  931. * the device. Here's the table implemented below:
  932. *
  933. * Fast Global DMA Remote WR
  934. * Reg LKEY MR Access
  935. * Sup'd Sup'd Needed Needed
  936. *
  937. * IWARP N N Y Y
  938. * N Y Y Y
  939. * Y N Y N
  940. * Y Y N -
  941. *
  942. * IB N N Y N
  943. * N Y N -
  944. * Y N Y N
  945. * Y Y N -
  946. *
  947. * NB: iWARP requires remote write access for the data sink
  948. * of an RDMA_READ. IB does not.
  949. */
  950. newxprt->sc_reader = rdma_read_chunk_lcl;
  951. if (dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
  952. newxprt->sc_frmr_pg_list_len =
  953. dev->attrs.max_fast_reg_page_list_len;
  954. newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
  955. newxprt->sc_reader = rdma_read_chunk_frmr;
  956. } else
  957. newxprt->sc_snd_w_inv = false;
  958. /*
  959. * Determine if a DMA MR is required and if so, what privs are required
  960. */
  961. if (!rdma_protocol_iwarp(dev, newxprt->sc_cm_id->port_num) &&
  962. !rdma_ib_or_roce(dev, newxprt->sc_cm_id->port_num))
  963. goto errout;
  964. if (rdma_protocol_iwarp(dev, newxprt->sc_cm_id->port_num))
  965. newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
  966. /* Post receive buffers */
  967. for (i = 0; i < newxprt->sc_max_requests; i++) {
  968. ret = svc_rdma_post_recv(newxprt, GFP_KERNEL);
  969. if (ret) {
  970. dprintk("svcrdma: failure posting receive buffers\n");
  971. goto errout;
  972. }
  973. }
  974. /* Swap out the handler */
  975. newxprt->sc_cm_id->event_handler = rdma_cma_handler;
  976. /* Construct RDMA-CM private message */
  977. pmsg.cp_magic = rpcrdma_cmp_magic;
  978. pmsg.cp_version = RPCRDMA_CMP_VERSION;
  979. pmsg.cp_flags = 0;
  980. pmsg.cp_send_size = pmsg.cp_recv_size =
  981. rpcrdma_encode_buffer_size(newxprt->sc_max_req_size);
  982. /* Accept Connection */
  983. set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
  984. memset(&conn_param, 0, sizeof conn_param);
  985. conn_param.responder_resources = 0;
  986. conn_param.initiator_depth = newxprt->sc_ord;
  987. conn_param.private_data = &pmsg;
  988. conn_param.private_data_len = sizeof(pmsg);
  989. ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
  990. if (ret) {
  991. dprintk("svcrdma: failed to accept new connection, ret=%d\n",
  992. ret);
  993. goto errout;
  994. }
  995. dprintk("svcrdma: new connection %p accepted:\n", newxprt);
  996. sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
  997. dprintk(" local address : %pIS:%u\n", sap, rpc_get_port(sap));
  998. sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
  999. dprintk(" remote address : %pIS:%u\n", sap, rpc_get_port(sap));
  1000. dprintk(" max_sge : %d\n", newxprt->sc_max_sge);
  1001. dprintk(" max_sge_rd : %d\n", newxprt->sc_max_sge_rd);
  1002. dprintk(" sq_depth : %d\n", newxprt->sc_sq_depth);
  1003. dprintk(" max_requests : %d\n", newxprt->sc_max_requests);
  1004. dprintk(" ord : %d\n", newxprt->sc_ord);
  1005. return &newxprt->sc_xprt;
  1006. errout:
  1007. dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
  1008. /* Take a reference in case the DTO handler runs */
  1009. svc_xprt_get(&newxprt->sc_xprt);
  1010. if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
  1011. ib_destroy_qp(newxprt->sc_qp);
  1012. rdma_destroy_id(newxprt->sc_cm_id);
  1013. /* This call to put will destroy the transport */
  1014. svc_xprt_put(&newxprt->sc_xprt);
  1015. return NULL;
  1016. }
  1017. static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
  1018. {
  1019. }
  1020. /*
  1021. * When connected, an svc_xprt has at least two references:
  1022. *
  1023. * - A reference held by the cm_id between the ESTABLISHED and
  1024. * DISCONNECTED events. If the remote peer disconnected first, this
  1025. * reference could be gone.
  1026. *
  1027. * - A reference held by the svc_recv code that called this function
  1028. * as part of close processing.
  1029. *
  1030. * At a minimum one references should still be held.
  1031. */
  1032. static void svc_rdma_detach(struct svc_xprt *xprt)
  1033. {
  1034. struct svcxprt_rdma *rdma =
  1035. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  1036. dprintk("svc: svc_rdma_detach(%p)\n", xprt);
  1037. /* Disconnect and flush posted WQE */
  1038. rdma_disconnect(rdma->sc_cm_id);
  1039. }
  1040. static void __svc_rdma_free(struct work_struct *work)
  1041. {
  1042. struct svcxprt_rdma *rdma =
  1043. container_of(work, struct svcxprt_rdma, sc_work);
  1044. struct svc_xprt *xprt = &rdma->sc_xprt;
  1045. dprintk("svcrdma: %s(%p)\n", __func__, rdma);
  1046. if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
  1047. ib_drain_qp(rdma->sc_qp);
  1048. /* We should only be called from kref_put */
  1049. if (atomic_read(&xprt->xpt_ref.refcount) != 0)
  1050. pr_err("svcrdma: sc_xprt still in use? (%d)\n",
  1051. atomic_read(&xprt->xpt_ref.refcount));
  1052. /*
  1053. * Destroy queued, but not processed read completions. Note
  1054. * that this cleanup has to be done before destroying the
  1055. * cm_id because the device ptr is needed to unmap the dma in
  1056. * svc_rdma_put_context.
  1057. */
  1058. while (!list_empty(&rdma->sc_read_complete_q)) {
  1059. struct svc_rdma_op_ctxt *ctxt;
  1060. ctxt = list_entry(rdma->sc_read_complete_q.next,
  1061. struct svc_rdma_op_ctxt,
  1062. dto_q);
  1063. list_del_init(&ctxt->dto_q);
  1064. svc_rdma_put_context(ctxt, 1);
  1065. }
  1066. /* Destroy queued, but not processed recv completions */
  1067. while (!list_empty(&rdma->sc_rq_dto_q)) {
  1068. struct svc_rdma_op_ctxt *ctxt;
  1069. ctxt = list_entry(rdma->sc_rq_dto_q.next,
  1070. struct svc_rdma_op_ctxt,
  1071. dto_q);
  1072. list_del_init(&ctxt->dto_q);
  1073. svc_rdma_put_context(ctxt, 1);
  1074. }
  1075. /* Warn if we leaked a resource or under-referenced */
  1076. if (rdma->sc_ctxt_used != 0)
  1077. pr_err("svcrdma: ctxt still in use? (%d)\n",
  1078. rdma->sc_ctxt_used);
  1079. /* Final put of backchannel client transport */
  1080. if (xprt->xpt_bc_xprt) {
  1081. xprt_put(xprt->xpt_bc_xprt);
  1082. xprt->xpt_bc_xprt = NULL;
  1083. }
  1084. rdma_dealloc_frmr_q(rdma);
  1085. svc_rdma_destroy_ctxts(rdma);
  1086. svc_rdma_destroy_maps(rdma);
  1087. /* Destroy the QP if present (not a listener) */
  1088. if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
  1089. ib_destroy_qp(rdma->sc_qp);
  1090. if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
  1091. ib_free_cq(rdma->sc_sq_cq);
  1092. if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
  1093. ib_free_cq(rdma->sc_rq_cq);
  1094. if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
  1095. ib_dealloc_pd(rdma->sc_pd);
  1096. /* Destroy the CM ID */
  1097. rdma_destroy_id(rdma->sc_cm_id);
  1098. kfree(rdma);
  1099. }
  1100. static void svc_rdma_free(struct svc_xprt *xprt)
  1101. {
  1102. struct svcxprt_rdma *rdma =
  1103. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  1104. INIT_WORK(&rdma->sc_work, __svc_rdma_free);
  1105. queue_work(svc_rdma_wq, &rdma->sc_work);
  1106. }
  1107. static int svc_rdma_has_wspace(struct svc_xprt *xprt)
  1108. {
  1109. struct svcxprt_rdma *rdma =
  1110. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  1111. /*
  1112. * If there are already waiters on the SQ,
  1113. * return false.
  1114. */
  1115. if (waitqueue_active(&rdma->sc_send_wait))
  1116. return 0;
  1117. /* Otherwise return true. */
  1118. return 1;
  1119. }
  1120. static int svc_rdma_secure_port(struct svc_rqst *rqstp)
  1121. {
  1122. return 1;
  1123. }
  1124. static void svc_rdma_kill_temp_xprt(struct svc_xprt *xprt)
  1125. {
  1126. }
  1127. int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
  1128. {
  1129. struct ib_send_wr *bad_wr, *n_wr;
  1130. int wr_count;
  1131. int i;
  1132. int ret;
  1133. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  1134. return -ENOTCONN;
  1135. wr_count = 1;
  1136. for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
  1137. wr_count++;
  1138. /* If the SQ is full, wait until an SQ entry is available */
  1139. while (1) {
  1140. if ((atomic_sub_return(wr_count, &xprt->sc_sq_avail) < 0)) {
  1141. atomic_inc(&rdma_stat_sq_starve);
  1142. /* Wait until SQ WR available if SQ still full */
  1143. atomic_add(wr_count, &xprt->sc_sq_avail);
  1144. wait_event(xprt->sc_send_wait,
  1145. atomic_read(&xprt->sc_sq_avail) > wr_count);
  1146. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  1147. return -ENOTCONN;
  1148. continue;
  1149. }
  1150. /* Take a transport ref for each WR posted */
  1151. for (i = 0; i < wr_count; i++)
  1152. svc_xprt_get(&xprt->sc_xprt);
  1153. /* Bump used SQ WR count and post */
  1154. ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
  1155. if (ret) {
  1156. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  1157. for (i = 0; i < wr_count; i ++)
  1158. svc_xprt_put(&xprt->sc_xprt);
  1159. dprintk("svcrdma: failed to post SQ WR rc=%d\n", ret);
  1160. dprintk(" sc_sq_avail=%d, sc_sq_depth=%d\n",
  1161. atomic_read(&xprt->sc_sq_avail),
  1162. xprt->sc_sq_depth);
  1163. wake_up(&xprt->sc_send_wait);
  1164. }
  1165. break;
  1166. }
  1167. return ret;
  1168. }