svc_rdma_transport.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2015-2018 Oracle. All rights reserved.
  4. * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
  5. * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the BSD-type
  11. * license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * Neither the name of the Network Appliance, Inc. nor the names of
  26. * its contributors may be used to endorse or promote products
  27. * derived from this software without specific prior written
  28. * permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  36. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  37. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  38. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  40. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * Author: Tom Tucker <tom@opengridcomputing.com>
  43. */
  44. #include <linux/interrupt.h>
  45. #include <linux/sched.h>
  46. #include <linux/slab.h>
  47. #include <linux/spinlock.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/export.h>
  50. #include <rdma/ib_verbs.h>
  51. #include <rdma/rdma_cm.h>
  52. #include <rdma/rw.h>
  53. #include <linux/sunrpc/addr.h>
  54. #include <linux/sunrpc/debug.h>
  55. #include <linux/sunrpc/rpc_rdma.h>
  56. #include <linux/sunrpc/svc_xprt.h>
  57. #include <linux/sunrpc/svc_rdma.h>
  58. #include "xprt_rdma.h"
  59. #include <trace/events/rpcrdma.h>
  60. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  61. static struct svcxprt_rdma *svc_rdma_create_xprt(struct svc_serv *serv,
  62. struct net *net);
  63. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  64. struct net *net,
  65. struct sockaddr *sa, int salen,
  66. int flags);
  67. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
  68. static void svc_rdma_release_rqst(struct svc_rqst *);
  69. static void svc_rdma_detach(struct svc_xprt *xprt);
  70. static void svc_rdma_free(struct svc_xprt *xprt);
  71. static int svc_rdma_has_wspace(struct svc_xprt *xprt);
  72. static void svc_rdma_secure_port(struct svc_rqst *);
  73. static void svc_rdma_kill_temp_xprt(struct svc_xprt *);
  74. static const struct svc_xprt_ops svc_rdma_ops = {
  75. .xpo_create = svc_rdma_create,
  76. .xpo_recvfrom = svc_rdma_recvfrom,
  77. .xpo_sendto = svc_rdma_sendto,
  78. .xpo_release_rqst = svc_rdma_release_rqst,
  79. .xpo_detach = svc_rdma_detach,
  80. .xpo_free = svc_rdma_free,
  81. .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
  82. .xpo_has_wspace = svc_rdma_has_wspace,
  83. .xpo_accept = svc_rdma_accept,
  84. .xpo_secure_port = svc_rdma_secure_port,
  85. .xpo_kill_temp_xprt = svc_rdma_kill_temp_xprt,
  86. };
  87. struct svc_xprt_class svc_rdma_class = {
  88. .xcl_name = "rdma",
  89. .xcl_owner = THIS_MODULE,
  90. .xcl_ops = &svc_rdma_ops,
  91. .xcl_max_payload = RPCSVC_MAXPAYLOAD_RDMA,
  92. .xcl_ident = XPRT_TRANSPORT_RDMA,
  93. };
  94. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  95. static struct svc_xprt *svc_rdma_bc_create(struct svc_serv *, struct net *,
  96. struct sockaddr *, int, int);
  97. static void svc_rdma_bc_detach(struct svc_xprt *);
  98. static void svc_rdma_bc_free(struct svc_xprt *);
  99. static const struct svc_xprt_ops svc_rdma_bc_ops = {
  100. .xpo_create = svc_rdma_bc_create,
  101. .xpo_detach = svc_rdma_bc_detach,
  102. .xpo_free = svc_rdma_bc_free,
  103. .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
  104. .xpo_secure_port = svc_rdma_secure_port,
  105. };
  106. struct svc_xprt_class svc_rdma_bc_class = {
  107. .xcl_name = "rdma-bc",
  108. .xcl_owner = THIS_MODULE,
  109. .xcl_ops = &svc_rdma_bc_ops,
  110. .xcl_max_payload = (1024 - RPCRDMA_HDRLEN_MIN)
  111. };
  112. static struct svc_xprt *svc_rdma_bc_create(struct svc_serv *serv,
  113. struct net *net,
  114. struct sockaddr *sa, int salen,
  115. int flags)
  116. {
  117. struct svcxprt_rdma *cma_xprt;
  118. struct svc_xprt *xprt;
  119. cma_xprt = svc_rdma_create_xprt(serv, net);
  120. if (!cma_xprt)
  121. return ERR_PTR(-ENOMEM);
  122. xprt = &cma_xprt->sc_xprt;
  123. svc_xprt_init(net, &svc_rdma_bc_class, xprt, serv);
  124. set_bit(XPT_CONG_CTRL, &xprt->xpt_flags);
  125. serv->sv_bc_xprt = xprt;
  126. dprintk("svcrdma: %s(%p)\n", __func__, xprt);
  127. return xprt;
  128. }
  129. static void svc_rdma_bc_detach(struct svc_xprt *xprt)
  130. {
  131. dprintk("svcrdma: %s(%p)\n", __func__, xprt);
  132. }
  133. static void svc_rdma_bc_free(struct svc_xprt *xprt)
  134. {
  135. struct svcxprt_rdma *rdma =
  136. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  137. dprintk("svcrdma: %s(%p)\n", __func__, xprt);
  138. if (xprt)
  139. kfree(rdma);
  140. }
  141. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  142. /* QP event handler */
  143. static void qp_event_handler(struct ib_event *event, void *context)
  144. {
  145. struct svc_xprt *xprt = context;
  146. trace_svcrdma_qp_error(event, (struct sockaddr *)&xprt->xpt_remote);
  147. switch (event->event) {
  148. /* These are considered benign events */
  149. case IB_EVENT_PATH_MIG:
  150. case IB_EVENT_COMM_EST:
  151. case IB_EVENT_SQ_DRAINED:
  152. case IB_EVENT_QP_LAST_WQE_REACHED:
  153. break;
  154. /* These are considered fatal events */
  155. case IB_EVENT_PATH_MIG_ERR:
  156. case IB_EVENT_QP_FATAL:
  157. case IB_EVENT_QP_REQ_ERR:
  158. case IB_EVENT_QP_ACCESS_ERR:
  159. case IB_EVENT_DEVICE_FATAL:
  160. default:
  161. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  162. svc_xprt_enqueue(xprt);
  163. break;
  164. }
  165. }
  166. static struct svcxprt_rdma *svc_rdma_create_xprt(struct svc_serv *serv,
  167. struct net *net)
  168. {
  169. struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
  170. if (!cma_xprt) {
  171. dprintk("svcrdma: failed to create new transport\n");
  172. return NULL;
  173. }
  174. svc_xprt_init(net, &svc_rdma_class, &cma_xprt->sc_xprt, serv);
  175. INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
  176. INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
  177. INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
  178. INIT_LIST_HEAD(&cma_xprt->sc_send_ctxts);
  179. INIT_LIST_HEAD(&cma_xprt->sc_recv_ctxts);
  180. INIT_LIST_HEAD(&cma_xprt->sc_rw_ctxts);
  181. init_waitqueue_head(&cma_xprt->sc_send_wait);
  182. spin_lock_init(&cma_xprt->sc_lock);
  183. spin_lock_init(&cma_xprt->sc_rq_dto_lock);
  184. spin_lock_init(&cma_xprt->sc_send_lock);
  185. spin_lock_init(&cma_xprt->sc_recv_lock);
  186. spin_lock_init(&cma_xprt->sc_rw_ctxt_lock);
  187. /*
  188. * Note that this implies that the underlying transport support
  189. * has some form of congestion control (see RFC 7530 section 3.1
  190. * paragraph 2). For now, we assume that all supported RDMA
  191. * transports are suitable here.
  192. */
  193. set_bit(XPT_CONG_CTRL, &cma_xprt->sc_xprt.xpt_flags);
  194. return cma_xprt;
  195. }
  196. static void
  197. svc_rdma_parse_connect_private(struct svcxprt_rdma *newxprt,
  198. struct rdma_conn_param *param)
  199. {
  200. const struct rpcrdma_connect_private *pmsg = param->private_data;
  201. if (pmsg &&
  202. pmsg->cp_magic == rpcrdma_cmp_magic &&
  203. pmsg->cp_version == RPCRDMA_CMP_VERSION) {
  204. newxprt->sc_snd_w_inv = pmsg->cp_flags &
  205. RPCRDMA_CMP_F_SND_W_INV_OK;
  206. dprintk("svcrdma: client send_size %u, recv_size %u "
  207. "remote inv %ssupported\n",
  208. rpcrdma_decode_buffer_size(pmsg->cp_send_size),
  209. rpcrdma_decode_buffer_size(pmsg->cp_recv_size),
  210. newxprt->sc_snd_w_inv ? "" : "un");
  211. }
  212. }
  213. /*
  214. * This function handles the CONNECT_REQUEST event on a listening
  215. * endpoint. It is passed the cma_id for the _new_ connection. The context in
  216. * this cma_id is inherited from the listening cma_id and is the svc_xprt
  217. * structure for the listening endpoint.
  218. *
  219. * This function creates a new xprt for the new connection and enqueues it on
  220. * the accept queue for the listent xprt. When the listen thread is kicked, it
  221. * will call the recvfrom method on the listen xprt which will accept the new
  222. * connection.
  223. */
  224. static void handle_connect_req(struct rdma_cm_id *new_cma_id,
  225. struct rdma_conn_param *param)
  226. {
  227. struct svcxprt_rdma *listen_xprt = new_cma_id->context;
  228. struct svcxprt_rdma *newxprt;
  229. struct sockaddr *sa;
  230. /* Create a new transport */
  231. newxprt = svc_rdma_create_xprt(listen_xprt->sc_xprt.xpt_server,
  232. listen_xprt->sc_xprt.xpt_net);
  233. if (!newxprt)
  234. return;
  235. newxprt->sc_cm_id = new_cma_id;
  236. new_cma_id->context = newxprt;
  237. svc_rdma_parse_connect_private(newxprt, param);
  238. /* Save client advertised inbound read limit for use later in accept. */
  239. newxprt->sc_ord = param->initiator_depth;
  240. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
  241. svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  242. /* The remote port is arbitrary and not under the control of the
  243. * client ULP. Set it to a fixed value so that the DRC continues
  244. * to be effective after a reconnect.
  245. */
  246. rpc_set_port((struct sockaddr *)&newxprt->sc_xprt.xpt_remote, 0);
  247. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
  248. svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  249. /*
  250. * Enqueue the new transport on the accept queue of the listening
  251. * transport
  252. */
  253. spin_lock_bh(&listen_xprt->sc_lock);
  254. list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
  255. spin_unlock_bh(&listen_xprt->sc_lock);
  256. set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
  257. svc_xprt_enqueue(&listen_xprt->sc_xprt);
  258. }
  259. /*
  260. * Handles events generated on the listening endpoint. These events will be
  261. * either be incoming connect requests or adapter removal events.
  262. */
  263. static int rdma_listen_handler(struct rdma_cm_id *cma_id,
  264. struct rdma_cm_event *event)
  265. {
  266. struct sockaddr *sap = (struct sockaddr *)&cma_id->route.addr.src_addr;
  267. trace_svcrdma_cm_event(event, sap);
  268. switch (event->event) {
  269. case RDMA_CM_EVENT_CONNECT_REQUEST:
  270. dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
  271. "event = %s (%d)\n", cma_id, cma_id->context,
  272. rdma_event_msg(event->event), event->event);
  273. handle_connect_req(cma_id, &event->param.conn);
  274. break;
  275. default:
  276. /* NB: No device removal upcall for INADDR_ANY listeners */
  277. dprintk("svcrdma: Unexpected event on listening endpoint %p, "
  278. "event = %s (%d)\n", cma_id,
  279. rdma_event_msg(event->event), event->event);
  280. break;
  281. }
  282. return 0;
  283. }
  284. static int rdma_cma_handler(struct rdma_cm_id *cma_id,
  285. struct rdma_cm_event *event)
  286. {
  287. struct sockaddr *sap = (struct sockaddr *)&cma_id->route.addr.dst_addr;
  288. struct svcxprt_rdma *rdma = cma_id->context;
  289. struct svc_xprt *xprt = &rdma->sc_xprt;
  290. trace_svcrdma_cm_event(event, sap);
  291. switch (event->event) {
  292. case RDMA_CM_EVENT_ESTABLISHED:
  293. /* Accept complete */
  294. svc_xprt_get(xprt);
  295. dprintk("svcrdma: Connection completed on DTO xprt=%p, "
  296. "cm_id=%p\n", xprt, cma_id);
  297. clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
  298. svc_xprt_enqueue(xprt);
  299. break;
  300. case RDMA_CM_EVENT_DISCONNECTED:
  301. dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
  302. xprt, cma_id);
  303. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  304. svc_xprt_enqueue(xprt);
  305. svc_xprt_put(xprt);
  306. break;
  307. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  308. dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
  309. "event = %s (%d)\n", cma_id, xprt,
  310. rdma_event_msg(event->event), event->event);
  311. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  312. svc_xprt_enqueue(xprt);
  313. svc_xprt_put(xprt);
  314. break;
  315. default:
  316. dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
  317. "event = %s (%d)\n", cma_id,
  318. rdma_event_msg(event->event), event->event);
  319. break;
  320. }
  321. return 0;
  322. }
  323. /*
  324. * Create a listening RDMA service endpoint.
  325. */
  326. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  327. struct net *net,
  328. struct sockaddr *sa, int salen,
  329. int flags)
  330. {
  331. struct rdma_cm_id *listen_id;
  332. struct svcxprt_rdma *cma_xprt;
  333. int ret;
  334. dprintk("svcrdma: Creating RDMA listener\n");
  335. if ((sa->sa_family != AF_INET) && (sa->sa_family != AF_INET6)) {
  336. dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
  337. return ERR_PTR(-EAFNOSUPPORT);
  338. }
  339. cma_xprt = svc_rdma_create_xprt(serv, net);
  340. if (!cma_xprt)
  341. return ERR_PTR(-ENOMEM);
  342. set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
  343. strcpy(cma_xprt->sc_xprt.xpt_remotebuf, "listener");
  344. listen_id = rdma_create_id(net, rdma_listen_handler, cma_xprt,
  345. RDMA_PS_TCP, IB_QPT_RC);
  346. if (IS_ERR(listen_id)) {
  347. ret = PTR_ERR(listen_id);
  348. dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
  349. goto err0;
  350. }
  351. /* Allow both IPv4 and IPv6 sockets to bind a single port
  352. * at the same time.
  353. */
  354. #if IS_ENABLED(CONFIG_IPV6)
  355. ret = rdma_set_afonly(listen_id, 1);
  356. if (ret) {
  357. dprintk("svcrdma: rdma_set_afonly failed = %d\n", ret);
  358. goto err1;
  359. }
  360. #endif
  361. ret = rdma_bind_addr(listen_id, sa);
  362. if (ret) {
  363. dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
  364. goto err1;
  365. }
  366. cma_xprt->sc_cm_id = listen_id;
  367. ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
  368. if (ret) {
  369. dprintk("svcrdma: rdma_listen failed = %d\n", ret);
  370. goto err1;
  371. }
  372. /*
  373. * We need to use the address from the cm_id in case the
  374. * caller specified 0 for the port number.
  375. */
  376. sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
  377. svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
  378. return &cma_xprt->sc_xprt;
  379. err1:
  380. rdma_destroy_id(listen_id);
  381. err0:
  382. kfree(cma_xprt);
  383. return ERR_PTR(ret);
  384. }
  385. /*
  386. * This is the xpo_recvfrom function for listening endpoints. Its
  387. * purpose is to accept incoming connections. The CMA callback handler
  388. * has already created a new transport and attached it to the new CMA
  389. * ID.
  390. *
  391. * There is a queue of pending connections hung on the listening
  392. * transport. This queue contains the new svc_xprt structure. This
  393. * function takes svc_xprt structures off the accept_q and completes
  394. * the connection.
  395. */
  396. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
  397. {
  398. struct svcxprt_rdma *listen_rdma;
  399. struct svcxprt_rdma *newxprt = NULL;
  400. struct rdma_conn_param conn_param;
  401. struct rpcrdma_connect_private pmsg;
  402. struct ib_qp_init_attr qp_attr;
  403. unsigned int ctxts, rq_depth;
  404. struct ib_device *dev;
  405. struct sockaddr *sap;
  406. int ret = 0;
  407. listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
  408. clear_bit(XPT_CONN, &xprt->xpt_flags);
  409. /* Get the next entry off the accept list */
  410. spin_lock_bh(&listen_rdma->sc_lock);
  411. if (!list_empty(&listen_rdma->sc_accept_q)) {
  412. newxprt = list_entry(listen_rdma->sc_accept_q.next,
  413. struct svcxprt_rdma, sc_accept_q);
  414. list_del_init(&newxprt->sc_accept_q);
  415. }
  416. if (!list_empty(&listen_rdma->sc_accept_q))
  417. set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
  418. spin_unlock_bh(&listen_rdma->sc_lock);
  419. if (!newxprt)
  420. return NULL;
  421. dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
  422. newxprt, newxprt->sc_cm_id);
  423. dev = newxprt->sc_cm_id->device;
  424. newxprt->sc_port_num = newxprt->sc_cm_id->port_num;
  425. /* Qualify the transport resource defaults with the
  426. * capabilities of this particular device */
  427. /* Transport header, head iovec, tail iovec */
  428. newxprt->sc_max_send_sges = 3;
  429. /* Add one SGE per page list entry */
  430. newxprt->sc_max_send_sges += (svcrdma_max_req_size / PAGE_SIZE) + 1;
  431. if (newxprt->sc_max_send_sges > dev->attrs.max_send_sge)
  432. newxprt->sc_max_send_sges = dev->attrs.max_send_sge;
  433. newxprt->sc_max_req_size = svcrdma_max_req_size;
  434. newxprt->sc_max_requests = svcrdma_max_requests;
  435. newxprt->sc_max_bc_requests = svcrdma_max_bc_requests;
  436. rq_depth = newxprt->sc_max_requests + newxprt->sc_max_bc_requests;
  437. if (rq_depth > dev->attrs.max_qp_wr) {
  438. pr_warn("svcrdma: reducing receive depth to %d\n",
  439. dev->attrs.max_qp_wr);
  440. rq_depth = dev->attrs.max_qp_wr;
  441. newxprt->sc_max_requests = rq_depth - 2;
  442. newxprt->sc_max_bc_requests = 2;
  443. }
  444. newxprt->sc_fc_credits = cpu_to_be32(newxprt->sc_max_requests);
  445. ctxts = rdma_rw_mr_factor(dev, newxprt->sc_port_num, RPCSVC_MAXPAGES);
  446. ctxts *= newxprt->sc_max_requests;
  447. newxprt->sc_sq_depth = rq_depth + ctxts;
  448. if (newxprt->sc_sq_depth > dev->attrs.max_qp_wr) {
  449. pr_warn("svcrdma: reducing send depth to %d\n",
  450. dev->attrs.max_qp_wr);
  451. newxprt->sc_sq_depth = dev->attrs.max_qp_wr;
  452. }
  453. atomic_set(&newxprt->sc_sq_avail, newxprt->sc_sq_depth);
  454. newxprt->sc_pd = ib_alloc_pd(dev, 0);
  455. if (IS_ERR(newxprt->sc_pd)) {
  456. dprintk("svcrdma: error creating PD for connect request\n");
  457. goto errout;
  458. }
  459. newxprt->sc_sq_cq = ib_alloc_cq(dev, newxprt, newxprt->sc_sq_depth,
  460. 0, IB_POLL_WORKQUEUE);
  461. if (IS_ERR(newxprt->sc_sq_cq)) {
  462. dprintk("svcrdma: error creating SQ CQ for connect request\n");
  463. goto errout;
  464. }
  465. newxprt->sc_rq_cq = ib_alloc_cq(dev, newxprt, rq_depth,
  466. 0, IB_POLL_WORKQUEUE);
  467. if (IS_ERR(newxprt->sc_rq_cq)) {
  468. dprintk("svcrdma: error creating RQ CQ for connect request\n");
  469. goto errout;
  470. }
  471. memset(&qp_attr, 0, sizeof qp_attr);
  472. qp_attr.event_handler = qp_event_handler;
  473. qp_attr.qp_context = &newxprt->sc_xprt;
  474. qp_attr.port_num = newxprt->sc_port_num;
  475. qp_attr.cap.max_rdma_ctxs = ctxts;
  476. qp_attr.cap.max_send_wr = newxprt->sc_sq_depth - ctxts;
  477. qp_attr.cap.max_recv_wr = rq_depth;
  478. qp_attr.cap.max_send_sge = newxprt->sc_max_send_sges;
  479. qp_attr.cap.max_recv_sge = 1;
  480. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  481. qp_attr.qp_type = IB_QPT_RC;
  482. qp_attr.send_cq = newxprt->sc_sq_cq;
  483. qp_attr.recv_cq = newxprt->sc_rq_cq;
  484. dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n",
  485. newxprt->sc_cm_id, newxprt->sc_pd);
  486. dprintk(" cap.max_send_wr = %d, cap.max_recv_wr = %d\n",
  487. qp_attr.cap.max_send_wr, qp_attr.cap.max_recv_wr);
  488. dprintk(" cap.max_send_sge = %d, cap.max_recv_sge = %d\n",
  489. qp_attr.cap.max_send_sge, qp_attr.cap.max_recv_sge);
  490. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
  491. if (ret) {
  492. dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
  493. goto errout;
  494. }
  495. newxprt->sc_qp = newxprt->sc_cm_id->qp;
  496. if (!(dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
  497. newxprt->sc_snd_w_inv = false;
  498. if (!rdma_protocol_iwarp(dev, newxprt->sc_port_num) &&
  499. !rdma_ib_or_roce(dev, newxprt->sc_port_num))
  500. goto errout;
  501. if (!svc_rdma_post_recvs(newxprt))
  502. goto errout;
  503. /* Swap out the handler */
  504. newxprt->sc_cm_id->event_handler = rdma_cma_handler;
  505. /* Construct RDMA-CM private message */
  506. pmsg.cp_magic = rpcrdma_cmp_magic;
  507. pmsg.cp_version = RPCRDMA_CMP_VERSION;
  508. pmsg.cp_flags = 0;
  509. pmsg.cp_send_size = pmsg.cp_recv_size =
  510. rpcrdma_encode_buffer_size(newxprt->sc_max_req_size);
  511. /* Accept Connection */
  512. set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
  513. memset(&conn_param, 0, sizeof conn_param);
  514. conn_param.responder_resources = 0;
  515. conn_param.initiator_depth = min_t(int, newxprt->sc_ord,
  516. dev->attrs.max_qp_init_rd_atom);
  517. if (!conn_param.initiator_depth) {
  518. dprintk("svcrdma: invalid ORD setting\n");
  519. ret = -EINVAL;
  520. goto errout;
  521. }
  522. conn_param.private_data = &pmsg;
  523. conn_param.private_data_len = sizeof(pmsg);
  524. ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
  525. if (ret)
  526. goto errout;
  527. dprintk("svcrdma: new connection %p accepted:\n", newxprt);
  528. sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
  529. dprintk(" local address : %pIS:%u\n", sap, rpc_get_port(sap));
  530. sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
  531. dprintk(" remote address : %pIS:%u\n", sap, rpc_get_port(sap));
  532. dprintk(" max_sge : %d\n", newxprt->sc_max_send_sges);
  533. dprintk(" sq_depth : %d\n", newxprt->sc_sq_depth);
  534. dprintk(" rdma_rw_ctxs : %d\n", ctxts);
  535. dprintk(" max_requests : %d\n", newxprt->sc_max_requests);
  536. dprintk(" ord : %d\n", conn_param.initiator_depth);
  537. trace_svcrdma_xprt_accept(&newxprt->sc_xprt);
  538. return &newxprt->sc_xprt;
  539. errout:
  540. dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
  541. trace_svcrdma_xprt_fail(&newxprt->sc_xprt);
  542. /* Take a reference in case the DTO handler runs */
  543. svc_xprt_get(&newxprt->sc_xprt);
  544. if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
  545. ib_destroy_qp(newxprt->sc_qp);
  546. rdma_destroy_id(newxprt->sc_cm_id);
  547. /* This call to put will destroy the transport */
  548. svc_xprt_put(&newxprt->sc_xprt);
  549. return NULL;
  550. }
  551. static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
  552. {
  553. }
  554. /*
  555. * When connected, an svc_xprt has at least two references:
  556. *
  557. * - A reference held by the cm_id between the ESTABLISHED and
  558. * DISCONNECTED events. If the remote peer disconnected first, this
  559. * reference could be gone.
  560. *
  561. * - A reference held by the svc_recv code that called this function
  562. * as part of close processing.
  563. *
  564. * At a minimum one references should still be held.
  565. */
  566. static void svc_rdma_detach(struct svc_xprt *xprt)
  567. {
  568. struct svcxprt_rdma *rdma =
  569. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  570. /* Disconnect and flush posted WQE */
  571. rdma_disconnect(rdma->sc_cm_id);
  572. }
  573. static void __svc_rdma_free(struct work_struct *work)
  574. {
  575. struct svcxprt_rdma *rdma =
  576. container_of(work, struct svcxprt_rdma, sc_work);
  577. struct svc_xprt *xprt = &rdma->sc_xprt;
  578. trace_svcrdma_xprt_free(xprt);
  579. if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
  580. ib_drain_qp(rdma->sc_qp);
  581. /* We should only be called from kref_put */
  582. if (kref_read(&xprt->xpt_ref) != 0)
  583. pr_err("svcrdma: sc_xprt still in use? (%d)\n",
  584. kref_read(&xprt->xpt_ref));
  585. svc_rdma_flush_recv_queues(rdma);
  586. /* Final put of backchannel client transport */
  587. if (xprt->xpt_bc_xprt) {
  588. xprt_put(xprt->xpt_bc_xprt);
  589. xprt->xpt_bc_xprt = NULL;
  590. }
  591. svc_rdma_destroy_rw_ctxts(rdma);
  592. svc_rdma_send_ctxts_destroy(rdma);
  593. svc_rdma_recv_ctxts_destroy(rdma);
  594. /* Destroy the QP if present (not a listener) */
  595. if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
  596. ib_destroy_qp(rdma->sc_qp);
  597. if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
  598. ib_free_cq(rdma->sc_sq_cq);
  599. if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
  600. ib_free_cq(rdma->sc_rq_cq);
  601. if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
  602. ib_dealloc_pd(rdma->sc_pd);
  603. /* Destroy the CM ID */
  604. rdma_destroy_id(rdma->sc_cm_id);
  605. kfree(rdma);
  606. }
  607. static void svc_rdma_free(struct svc_xprt *xprt)
  608. {
  609. struct svcxprt_rdma *rdma =
  610. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  611. INIT_WORK(&rdma->sc_work, __svc_rdma_free);
  612. queue_work(svc_rdma_wq, &rdma->sc_work);
  613. }
  614. static int svc_rdma_has_wspace(struct svc_xprt *xprt)
  615. {
  616. struct svcxprt_rdma *rdma =
  617. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  618. /*
  619. * If there are already waiters on the SQ,
  620. * return false.
  621. */
  622. if (waitqueue_active(&rdma->sc_send_wait))
  623. return 0;
  624. /* Otherwise return true. */
  625. return 1;
  626. }
  627. static void svc_rdma_secure_port(struct svc_rqst *rqstp)
  628. {
  629. set_bit(RQ_SECURE, &rqstp->rq_flags);
  630. }
  631. static void svc_rdma_kill_temp_xprt(struct svc_xprt *xprt)
  632. {
  633. }