blob: 81548fc2ee7f0d7b2f4ef356df04af41d68b817d [file] [log] [blame]
Chuck Levera2268cf2018-05-04 15:34:32 -04001// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -04002/*
Chuck Lever62b56a62017-10-30 16:22:14 -04003 * Copyright (c) 2014-2017 Oracle. All rights reserved.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04004 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the BSD-type
10 * license below:
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * Neither the name of the Network Appliance, Inc. nor the names of
25 * its contributors may be used to endorse or promote products
26 * derived from this software without specific prior written
27 * permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040040 */
41
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040042/*
43 * verbs.c
44 *
45 * Encapsulates the major functions managing:
46 * o adapters
47 * o endpoints
48 * o connections
49 * o buffer memory
50 */
51
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000052#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
Chuck Lever0dd39ca2015-03-30 14:33:43 -040054#include <linux/sunrpc/addr.h>
Chuck Lever05c97462016-09-06 11:22:58 -040055#include <linux/sunrpc/svc_rdma.h>
Chuck Leverae729502017-10-20 10:48:12 -040056
57#include <asm-generic/barrier.h>
Chuck Lever65866f82014-05-28 10:33:59 -040058#include <asm/bitops.h>
Chuck Lever56a6bd12017-04-11 13:23:34 -040059
Chuck Lever0a904872017-02-08 17:00:35 -050060#include <rdma/ib_cm.h>
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040061
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040062#include "xprt_rdma.h"
Chuck Leverb6e717cb2018-05-07 15:27:05 -040063#include <trace/events/rpcrdma.h>
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040064
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040065/*
66 * Globals/Macros
67 */
68
Jeff Laytonf895b252014-11-17 16:58:04 -050069#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040070# define RPCDBG_FACILITY RPCDBG_TRANS
71#endif
72
73/*
74 * internal functions
75 */
Chuck Leverefd81e92018-05-04 15:35:41 -040076static void rpcrdma_sendctx_put_locked(struct rpcrdma_sendctx *sc);
Chuck Lever96cedde2017-12-14 20:57:55 -050077static void rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt);
78static void rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf);
Chuck Leverd2832af2019-04-24 09:39:32 -040079static struct rpcrdma_regbuf *
80rpcrdma_regbuf_alloc(size_t size, enum dma_data_direction direction,
81 gfp_t flags);
82static void rpcrdma_regbuf_dma_unmap(struct rpcrdma_regbuf *rb);
83static void rpcrdma_regbuf_free(struct rpcrdma_regbuf *rb);
Chuck Lever6ceea362018-12-19 10:58:24 -050084static void rpcrdma_post_recvs(struct rpcrdma_xprt *r_xprt, bool temp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040085
Chuck Lever6d2d0ee2018-12-19 10:58:29 -050086/* Wait for outstanding transport work to finish.
87 */
88static void rpcrdma_xprt_drain(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040089{
Chuck Lever6d2d0ee2018-12-19 10:58:29 -050090 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
91 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040092
Chuck Lever6d2d0ee2018-12-19 10:58:29 -050093 /* Flush Receives, then wait for deferred Reply work
94 * to complete.
95 */
Chuck Levere1ede312019-04-09 17:04:09 -040096 ib_drain_rq(ia->ri_id->qp);
Chuck Lever6d2d0ee2018-12-19 10:58:29 -050097 drain_workqueue(buf->rb_completion_wq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040098
Chuck Lever6d2d0ee2018-12-19 10:58:29 -050099 /* Deferred Reply processing might have scheduled
100 * local invalidations.
101 */
102 ib_drain_sq(ia->ri_id->qp);
Chuck Leverf1a03b72014-11-08 20:14:37 -0500103}
104
Chuck Lever31e62d22018-10-01 14:26:08 -0400105/**
Chuck Leverf9521d52018-10-01 14:26:13 -0400106 * rpcrdma_qp_event_handler - Handle one QP event (error notification)
107 * @event: details of the event
108 * @context: ep that owns QP where event occurred
109 *
110 * Called from the RDMA provider (device driver) possibly in an interrupt
111 * context.
112 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400113static void
Chuck Leverf9521d52018-10-01 14:26:13 -0400114rpcrdma_qp_event_handler(struct ib_event *event, void *context)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400115{
116 struct rpcrdma_ep *ep = context;
Chuck Lever643cf322017-12-20 16:31:45 -0500117 struct rpcrdma_xprt *r_xprt = container_of(ep, struct rpcrdma_xprt,
118 rx_ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400119
Chuck Leverf9521d52018-10-01 14:26:13 -0400120 trace_xprtrdma_qp_event(r_xprt, event);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400121}
122
Chuck Lever2fa8f882016-03-04 11:28:53 -0500123/**
124 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
125 * @cq: completion queue (ignored)
126 * @wc: completed WR
127 *
Chuck Lever4220a072015-10-24 17:26:45 -0400128 */
129static void
Chuck Lever2fa8f882016-03-04 11:28:53 -0500130rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400131{
Chuck Leverae729502017-10-20 10:48:12 -0400132 struct ib_cqe *cqe = wc->wr_cqe;
133 struct rpcrdma_sendctx *sc =
134 container_of(cqe, struct rpcrdma_sendctx, sc_cqe);
135
Chuck Lever2fa8f882016-03-04 11:28:53 -0500136 /* WARNING: Only wr_cqe and status are reliable at this point */
Chuck Leverab03eff2017-12-20 16:30:40 -0500137 trace_xprtrdma_wc_send(sc, wc);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500138 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
139 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
140 ib_wc_status_msg(wc->status),
141 wc->status, wc->vendor_err);
Chuck Leverae729502017-10-20 10:48:12 -0400142
143 rpcrdma_sendctx_put_locked(sc);
Chuck Leverfc664482014-05-28 10:33:25 -0400144}
145
Chuck Lever552bf222016-03-04 11:28:36 -0500146/**
Chuck Lever1519e962016-09-15 10:57:49 -0400147 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
Chuck Lever552bf222016-03-04 11:28:36 -0500148 * @cq: completion queue (ignored)
149 * @wc: completed WR
150 *
151 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400152static void
Chuck Lever1519e962016-09-15 10:57:49 -0400153rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400154{
Chuck Lever552bf222016-03-04 11:28:36 -0500155 struct ib_cqe *cqe = wc->wr_cqe;
156 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
157 rr_cqe);
Chuck Lever6ceea362018-12-19 10:58:24 -0500158 struct rpcrdma_xprt *r_xprt = rep->rr_rxprt;
Chuck Leverfc664482014-05-28 10:33:25 -0400159
Chuck Lever6ceea362018-12-19 10:58:24 -0500160 /* WARNING: Only wr_cqe and status are reliable at this point */
Chuck Lever0e0b8542018-05-04 15:35:14 -0400161 trace_xprtrdma_wc_receive(wc);
Chuck Lever6ceea362018-12-19 10:58:24 -0500162 --r_xprt->rx_ep.rep_receive_count;
Chuck Lever85024272015-01-21 11:02:04 -0500163 if (wc->status != IB_WC_SUCCESS)
Chuck Lever6ceea362018-12-19 10:58:24 -0500164 goto out_flushed;
Chuck Leverfc664482014-05-28 10:33:25 -0400165
Chuck Lever85024272015-01-21 11:02:04 -0500166 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Lever96f87782017-08-03 14:30:03 -0400167 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len);
Chuck Leverc8b920b2016-09-15 10:57:16 -0400168 rep->rr_wc_flags = wc->wc_flags;
169 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
170
Chuck Lever91a10c52017-04-11 13:23:02 -0400171 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
Chuck Lever6b1184c2015-01-21 11:04:25 -0500172 rdmab_addr(rep->rr_rdmabuf),
Chuck Levere2a67192017-08-03 14:30:44 -0400173 wc->byte_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500174
Chuck Lever6ceea362018-12-19 10:58:24 -0500175 rpcrdma_post_recvs(r_xprt, false);
Chuck Leverd8f532d2017-10-16 15:01:30 -0400176 rpcrdma_reply_handler(rep);
Chuck Lever85024272015-01-21 11:02:04 -0500177 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400178
Chuck Lever6ceea362018-12-19 10:58:24 -0500179out_flushed:
Chuck Lever85024272015-01-21 11:02:04 -0500180 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500181 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
182 ib_wc_status_msg(wc->status),
183 wc->status, wc->vendor_err);
Chuck Lever6ceea362018-12-19 10:58:24 -0500184 rpcrdma_recv_buffer_put(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400185}
186
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400187static void
188rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
189 struct rdma_conn_param *param)
190{
191 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
192 const struct rpcrdma_connect_private *pmsg = param->private_data;
193 unsigned int rsize, wsize;
194
Chuck Leverc8b920b2016-09-15 10:57:16 -0400195 /* Default settings for RPC-over-RDMA Version One */
Chuck Leverb5f0afb2017-02-08 16:59:54 -0500196 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400197 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
198 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
199
200 if (pmsg &&
201 pmsg->cp_magic == rpcrdma_cmp_magic &&
202 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
Chuck Leverc95a3c62017-02-08 17:00:02 -0500203 r_xprt->rx_ia.ri_implicit_roundup = true;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400204 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
205 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
206 }
207
208 if (rsize < cdata->inline_rsize)
209 cdata->inline_rsize = rsize;
210 if (wsize < cdata->inline_wsize)
211 cdata->inline_wsize = wsize;
Chuck Lever6d6bf722016-11-29 10:53:13 -0500212 dprintk("RPC: %s: max send %u, max recv %u\n",
213 __func__, cdata->inline_wsize, cdata->inline_rsize);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400214 rpcrdma_set_max_header_sizes(r_xprt);
215}
216
Chuck Leverae382882018-10-01 14:25:47 -0400217/**
218 * rpcrdma_cm_event_handler - Handle RDMA CM events
219 * @id: rdma_cm_id on which an event has occurred
220 * @event: details of the event
221 *
222 * Called with @id's mutex held. Returns 1 if caller should
223 * destroy @id, otherwise 0.
224 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400225static int
Chuck Leverae382882018-10-01 14:25:47 -0400226rpcrdma_cm_event_handler(struct rdma_cm_id *id, struct rdma_cm_event *event)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400227{
Chuck Levered97f1f2018-10-01 14:25:52 -0400228 struct rpcrdma_xprt *r_xprt = id->context;
229 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
230 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
231 struct rpc_xprt *xprt = &r_xprt->rx_xprt;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400232
Chuck Leverae382882018-10-01 14:25:47 -0400233 might_sleep();
234
Chuck Levered97f1f2018-10-01 14:25:52 -0400235 trace_xprtrdma_cm_event(r_xprt, event);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400236 switch (event->event) {
237 case RDMA_CM_EVENT_ADDR_RESOLVED:
238 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400239 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400240 complete(&ia->ri_done);
Chuck Lever316a6162018-10-01 14:26:03 -0400241 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400242 case RDMA_CM_EVENT_ADDR_ERROR:
Chuck Lever52d28fe2018-05-04 15:34:37 -0400243 ia->ri_async_rc = -EPROTO;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400244 complete(&ia->ri_done);
Chuck Lever316a6162018-10-01 14:26:03 -0400245 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400246 case RDMA_CM_EVENT_ROUTE_ERROR:
247 ia->ri_async_rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400248 complete(&ia->ri_done);
Chuck Lever316a6162018-10-01 14:26:03 -0400249 return 0;
Chuck Leverbebd0312017-04-11 13:23:10 -0400250 case RDMA_CM_EVENT_DEVICE_REMOVAL:
251#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Leverd461f1f2017-12-14 20:56:50 -0500252 pr_info("rpcrdma: removing device %s for %s:%s\n",
Chuck Lever173b8f42017-06-08 11:53:00 -0400253 ia->ri_device->name,
Chuck Levered97f1f2018-10-01 14:25:52 -0400254 rpcrdma_addrstr(r_xprt), rpcrdma_portstr(r_xprt));
Chuck Leverbebd0312017-04-11 13:23:10 -0400255#endif
256 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
257 ep->rep_connected = -ENODEV;
Chuck Levered97f1f2018-10-01 14:25:52 -0400258 xprt_force_disconnect(xprt);
Chuck Leverbebd0312017-04-11 13:23:10 -0400259 wait_for_completion(&ia->ri_remove_done);
260
261 ia->ri_id = NULL;
Chuck Leverbebd0312017-04-11 13:23:10 -0400262 ia->ri_device = NULL;
263 /* Return 1 to ensure the core destroys the id. */
264 return 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400265 case RDMA_CM_EVENT_ESTABLISHED:
Chuck Levered97f1f2018-10-01 14:25:52 -0400266 ++xprt->connect_cookie;
Chuck Leveraadc5a92018-10-01 14:25:57 -0400267 ep->rep_connected = 1;
Chuck Levered97f1f2018-10-01 14:25:52 -0400268 rpcrdma_update_connect_private(r_xprt, &event->param.conn);
Chuck Lever31e62d22018-10-01 14:26:08 -0400269 wake_up_all(&ep->rep_connect_wait);
270 break;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400271 case RDMA_CM_EVENT_CONNECT_ERROR:
Chuck Leveraadc5a92018-10-01 14:25:57 -0400272 ep->rep_connected = -ENOTCONN;
Chuck Lever31e62d22018-10-01 14:26:08 -0400273 goto disconnected;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400274 case RDMA_CM_EVENT_UNREACHABLE:
Chuck Leveraadc5a92018-10-01 14:25:57 -0400275 ep->rep_connected = -ENETUNREACH;
Chuck Lever31e62d22018-10-01 14:26:08 -0400276 goto disconnected;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400277 case RDMA_CM_EVENT_REJECTED:
Chuck Leverd461f1f2017-12-14 20:56:50 -0500278 dprintk("rpcrdma: connection to %s:%s rejected: %s\n",
Chuck Levered97f1f2018-10-01 14:25:52 -0400279 rpcrdma_addrstr(r_xprt), rpcrdma_portstr(r_xprt),
Chuck Lever0a904872017-02-08 17:00:35 -0500280 rdma_reject_msg(id, event->status));
Chuck Leveraadc5a92018-10-01 14:25:57 -0400281 ep->rep_connected = -ECONNREFUSED;
Chuck Lever0a904872017-02-08 17:00:35 -0500282 if (event->status == IB_CM_REJ_STALE_CONN)
Chuck Leveraadc5a92018-10-01 14:25:57 -0400283 ep->rep_connected = -EAGAIN;
Chuck Lever31e62d22018-10-01 14:26:08 -0400284 goto disconnected;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400285 case RDMA_CM_EVENT_DISCONNECTED:
Chuck Leveraadc5a92018-10-01 14:25:57 -0400286 ep->rep_connected = -ECONNABORTED;
Chuck Lever31e62d22018-10-01 14:26:08 -0400287disconnected:
288 xprt_force_disconnect(xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400289 wake_up_all(&ep->rep_connect_wait);
Chuck Lever316a6162018-10-01 14:26:03 -0400290 break;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400291 default:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400292 break;
293 }
294
Chuck Lever5f624122018-12-19 10:59:01 -0500295 dprintk("RPC: %s: %s:%s on %s/frwr: %s\n", __func__,
Chuck Lever316a6162018-10-01 14:26:03 -0400296 rpcrdma_addrstr(r_xprt), rpcrdma_portstr(r_xprt),
Chuck Lever5f624122018-12-19 10:59:01 -0500297 ia->ri_device->name, rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400298 return 0;
299}
300
301static struct rdma_cm_id *
Chuck Leverdd229ce2017-12-14 20:56:58 -0500302rpcrdma_create_id(struct rpcrdma_xprt *xprt, struct rpcrdma_ia *ia)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400303{
Chuck Lever109b88a2016-11-29 10:52:40 -0500304 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400305 struct rdma_cm_id *id;
306 int rc;
307
Chuck Leverb4744e02017-12-20 16:31:29 -0500308 trace_xprtrdma_conn_start(xprt);
309
Tom Talpey1a954052008-10-09 15:01:31 -0400310 init_completion(&ia->ri_done);
Chuck Leverbebd0312017-04-11 13:23:10 -0400311 init_completion(&ia->ri_remove_done);
Tom Talpey1a954052008-10-09 15:01:31 -0400312
Chuck Leverae382882018-10-01 14:25:47 -0400313 id = rdma_create_id(xprt->rx_xprt.xprt_net, rpcrdma_cm_event_handler,
Chuck Lever107c4be2018-05-04 15:34:42 -0400314 xprt, RDMA_PS_TCP, IB_QPT_RC);
Chuck Leverddbb3472018-12-19 10:59:39 -0500315 if (IS_ERR(id))
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400316 return id;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400317
Tom Talpey5675add2008-10-09 15:01:41 -0400318 ia->ri_async_rc = -ETIMEDOUT;
Chuck Leverdd229ce2017-12-14 20:56:58 -0500319 rc = rdma_resolve_addr(id, NULL,
320 (struct sockaddr *)&xprt->rx_xprt.addr,
321 RDMA_RESOLVE_TIMEOUT);
Chuck Leverddbb3472018-12-19 10:59:39 -0500322 if (rc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400323 goto out;
Chuck Lever109b88a2016-11-29 10:52:40 -0500324 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
325 if (rc < 0) {
Chuck Leverb4744e02017-12-20 16:31:29 -0500326 trace_xprtrdma_conn_tout(xprt);
Chuck Lever109b88a2016-11-29 10:52:40 -0500327 goto out;
328 }
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400329
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400330 rc = ia->ri_async_rc;
331 if (rc)
332 goto out;
333
Tom Talpey5675add2008-10-09 15:01:41 -0400334 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400335 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
Chuck Leverddbb3472018-12-19 10:59:39 -0500336 if (rc)
Chuck Lever56a6bd12017-04-11 13:23:34 -0400337 goto out;
Chuck Lever109b88a2016-11-29 10:52:40 -0500338 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
339 if (rc < 0) {
Chuck Leverb4744e02017-12-20 16:31:29 -0500340 trace_xprtrdma_conn_tout(xprt);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400341 goto out;
Chuck Lever109b88a2016-11-29 10:52:40 -0500342 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400343 rc = ia->ri_async_rc;
344 if (rc)
Chuck Lever56a6bd12017-04-11 13:23:34 -0400345 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400346
347 return id;
Chuck Lever56a6bd12017-04-11 13:23:34 -0400348
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400349out:
350 rdma_destroy_id(id);
351 return ERR_PTR(rc);
352}
353
354/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400355 * Exported functions.
356 */
357
Chuck Leverfff09592017-04-11 13:22:54 -0400358/**
359 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
Chuck Leverdd229ce2017-12-14 20:56:58 -0500360 * @xprt: transport with IA to (re)initialize
Chuck Leverfff09592017-04-11 13:22:54 -0400361 *
362 * Returns 0 on success, negative errno if an appropriate
363 * Interface Adapter could not be found and opened.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400364 */
365int
Chuck Leverdd229ce2017-12-14 20:56:58 -0500366rpcrdma_ia_open(struct rpcrdma_xprt *xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400367{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400368 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400369 int rc;
370
Chuck Leverdd229ce2017-12-14 20:56:58 -0500371 ia->ri_id = rpcrdma_create_id(xprt, ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400372 if (IS_ERR(ia->ri_id)) {
373 rc = PTR_ERR(ia->ri_id);
Chuck Leverfff09592017-04-11 13:22:54 -0400374 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400375 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400376 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400377
Christoph Hellwiged082d32016-09-05 12:56:17 +0200378 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400379 if (IS_ERR(ia->ri_pd)) {
380 rc = PTR_ERR(ia->ri_pd);
Chuck Leverb54054c2016-06-29 13:53:27 -0400381 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
Chuck Leverfff09592017-04-11 13:22:54 -0400382 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400383 }
384
Chuck Leverfff09592017-04-11 13:22:54 -0400385 switch (xprt_rdma_memreg_strategy) {
Chuck Leverce5b3712017-12-14 20:57:47 -0500386 case RPCRDMA_FRWR:
Chuck Lever5f624122018-12-19 10:59:01 -0500387 if (frwr_is_supported(ia))
Chuck Leverb54054c2016-06-29 13:53:27 -0400388 break;
Chuck Leverb54054c2016-06-29 13:53:27 -0400389 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400390 default:
Chuck Leverfff09592017-04-11 13:22:54 -0400391 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
392 ia->ri_device->name, xprt_rdma_memreg_strategy);
Chuck Leverb54054c2016-06-29 13:53:27 -0400393 rc = -EINVAL;
Chuck Leverfff09592017-04-11 13:22:54 -0400394 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400395 }
396
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400397 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500398
Chuck Leverfff09592017-04-11 13:22:54 -0400399out_err:
400 rpcrdma_ia_close(ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400401 return rc;
402}
403
Chuck Leverfff09592017-04-11 13:22:54 -0400404/**
Chuck Leverbebd0312017-04-11 13:23:10 -0400405 * rpcrdma_ia_remove - Handle device driver unload
406 * @ia: interface adapter being removed
407 *
408 * Divest transport H/W resources associated with this adapter,
409 * but allow it to be restored later.
410 */
411void
412rpcrdma_ia_remove(struct rpcrdma_ia *ia)
413{
414 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
415 rx_ia);
416 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
417 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
418 struct rpcrdma_req *req;
419 struct rpcrdma_rep *rep;
420
421 cancel_delayed_work_sync(&buf->rb_refresh_worker);
422
423 /* This is similar to rpcrdma_ep_destroy, but:
424 * - Don't cancel the connect worker.
425 * - Don't call rpcrdma_ep_disconnect, which waits
426 * for another conn upcall, which will deadlock.
427 * - rdma_disconnect is unneeded, the underlying
428 * connection is already gone.
429 */
430 if (ia->ri_id->qp) {
Chuck Lever6d2d0ee2018-12-19 10:58:29 -0500431 rpcrdma_xprt_drain(r_xprt);
Chuck Leverbebd0312017-04-11 13:23:10 -0400432 rdma_destroy_qp(ia->ri_id);
433 ia->ri_id->qp = NULL;
434 }
435 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever25524282018-03-19 14:23:16 -0400436 ep->rep_attr.recv_cq = NULL;
Chuck Leverbebd0312017-04-11 13:23:10 -0400437 ib_free_cq(ep->rep_attr.send_cq);
Chuck Lever25524282018-03-19 14:23:16 -0400438 ep->rep_attr.send_cq = NULL;
Chuck Leverbebd0312017-04-11 13:23:10 -0400439
440 /* The ULP is responsible for ensuring all DMA
441 * mappings and MRs are gone.
442 */
443 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
Chuck Leverd2832af2019-04-24 09:39:32 -0400444 rpcrdma_regbuf_dma_unmap(rep->rr_rdmabuf);
Chuck Leverbebd0312017-04-11 13:23:10 -0400445 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
Chuck Leverd2832af2019-04-24 09:39:32 -0400446 rpcrdma_regbuf_dma_unmap(req->rl_rdmabuf);
447 rpcrdma_regbuf_dma_unmap(req->rl_sendbuf);
448 rpcrdma_regbuf_dma_unmap(req->rl_recvbuf);
Chuck Leverbebd0312017-04-11 13:23:10 -0400449 }
Chuck Lever96cedde2017-12-14 20:57:55 -0500450 rpcrdma_mrs_destroy(buf);
Chuck Lever25524282018-03-19 14:23:16 -0400451 ib_dealloc_pd(ia->ri_pd);
452 ia->ri_pd = NULL;
Chuck Leverbebd0312017-04-11 13:23:10 -0400453
454 /* Allow waiters to continue */
455 complete(&ia->ri_remove_done);
Chuck Leverb4744e02017-12-20 16:31:29 -0500456
457 trace_xprtrdma_remove(r_xprt);
Chuck Leverbebd0312017-04-11 13:23:10 -0400458}
459
460/**
Chuck Leverfff09592017-04-11 13:22:54 -0400461 * rpcrdma_ia_close - Clean up/close an IA.
462 * @ia: interface adapter to close
463 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400464 */
465void
466rpcrdma_ia_close(struct rpcrdma_ia *ia)
467{
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400468 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
469 if (ia->ri_id->qp)
470 rdma_destroy_qp(ia->ri_id);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400471 rdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400472 }
Chuck Leverfff09592017-04-11 13:22:54 -0400473 ia->ri_id = NULL;
474 ia->ri_device = NULL;
Chuck Lever6d446982015-05-26 11:51:27 -0400475
476 /* If the pd is still busy, xprtrdma missed freeing a resource */
477 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600478 ib_dealloc_pd(ia->ri_pd);
Chuck Leverfff09592017-04-11 13:22:54 -0400479 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400480}
481
482/*
483 * Create unconnected endpoint.
484 */
485int
486rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
Chuck Lever16f906d2017-02-08 17:00:10 -0500487 struct rpcrdma_create_data_internal *cdata)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400488{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400489 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
Chuck Leverfc664482014-05-28 10:33:25 -0400490 struct ib_cq *sendcq, *recvcq;
Chuck Lever914fcad2018-05-04 15:34:48 -0400491 unsigned int max_sge;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500492 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400493
Steve Wise33023fb2018-06-18 08:05:26 -0700494 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_send_sge,
Chuck Levereed50872017-03-11 15:52:47 -0500495 RPCRDMA_MAX_SEND_SGES);
Chuck Lever16f906d2017-02-08 17:00:10 -0500496 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
497 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
Chuck Leverb3221d62015-08-03 13:03:39 -0400498 return -ENOMEM;
499 }
Chuck Lever1179e2c2018-01-31 12:34:05 -0500500 ia->ri_max_send_sges = max_sge;
Chuck Leverb3221d62015-08-03 13:03:39 -0400501
Chuck Lever5f624122018-12-19 10:59:01 -0500502 rc = frwr_open(ia, ep, cdata);
Chuck Lever914fcad2018-05-04 15:34:48 -0400503 if (rc)
504 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400505
Chuck Leverf9521d52018-10-01 14:26:13 -0400506 ep->rep_attr.event_handler = rpcrdma_qp_event_handler;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400507 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400508 ep->rep_attr.srq = NULL;
Chuck Lever16f906d2017-02-08 17:00:10 -0500509 ep->rep_attr.cap.max_send_sge = max_sge;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400510 ep->rep_attr.cap.max_recv_sge = 1;
511 ep->rep_attr.cap.max_inline_data = 0;
512 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
513 ep->rep_attr.qp_type = IB_QPT_RC;
514 ep->rep_attr.port_num = ~0;
515
516 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
517 "iovs: send %d recv %d\n",
518 __func__,
519 ep->rep_attr.cap.max_send_wr,
520 ep->rep_attr.cap.max_recv_wr,
521 ep->rep_attr.cap.max_send_sge,
522 ep->rep_attr.cap.max_recv_sge);
523
524 /* set trigger for requesting send completion */
Chuck Leverae729502017-10-20 10:48:12 -0400525 ep->rep_send_batch = min_t(unsigned int, RPCRDMA_MAX_SEND_BATCH,
526 cdata->max_requests >> 2);
527 ep->rep_send_count = ep->rep_send_batch;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400528 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever6ceea362018-12-19 10:58:24 -0500529 ep->rep_receive_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400530
Chuck Lever2fa8f882016-03-04 11:28:53 -0500531 sendcq = ib_alloc_cq(ia->ri_device, NULL,
532 ep->rep_attr.cap.max_send_wr + 1,
Nicolas Morey-Chaisemartina4cb5bd2019-02-05 18:21:02 +0100533 ia->ri_device->num_comp_vectors > 1 ? 1 : 0,
534 IB_POLL_WORKQUEUE);
Chuck Leverfc664482014-05-28 10:33:25 -0400535 if (IS_ERR(sendcq)) {
536 rc = PTR_ERR(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400537 goto out1;
538 }
539
Chuck Lever552bf222016-03-04 11:28:36 -0500540 recvcq = ib_alloc_cq(ia->ri_device, NULL,
541 ep->rep_attr.cap.max_recv_wr + 1,
Chuck Leverd8f532d2017-10-16 15:01:30 -0400542 0, IB_POLL_WORKQUEUE);
Chuck Leverfc664482014-05-28 10:33:25 -0400543 if (IS_ERR(recvcq)) {
544 rc = PTR_ERR(recvcq);
Chuck Leverfc664482014-05-28 10:33:25 -0400545 goto out2;
546 }
547
Chuck Leverfc664482014-05-28 10:33:25 -0400548 ep->rep_attr.send_cq = sendcq;
549 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400550
551 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400552 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400553
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400554 /* Prepare RDMA-CM private message */
555 pmsg->cp_magic = rpcrdma_cmp_magic;
556 pmsg->cp_version = RPCRDMA_CMP_VERSION;
Chuck Lever5f624122018-12-19 10:59:01 -0500557 pmsg->cp_flags |= RPCRDMA_CMP_F_SND_W_INV_OK;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400558 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
559 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
560 ep->rep_remote_cma.private_data = pmsg;
561 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400562
563 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400564 ep->rep_remote_cma.initiator_depth = 0;
Chuck Leverb7e85fff2018-02-28 15:30:33 -0500565 ep->rep_remote_cma.responder_resources =
566 min_t(int, U8_MAX, ia->ri_device->attrs.max_qp_rd_atom);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400567
Chuck Leverb2dde942016-05-02 14:43:03 -0400568 /* Limit transport retries so client can detect server
569 * GID changes quickly. RPC layer handles re-establishing
570 * transport connection and retransmission.
571 */
572 ep->rep_remote_cma.retry_count = 6;
573
574 /* RPC-over-RDMA handles its own flow control. In addition,
575 * make all RNR NAKs visible so we know that RPC-over-RDMA
576 * flow control is working correctly (no NAKs should be seen).
577 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400578 ep->rep_remote_cma.flow_control = 0;
579 ep->rep_remote_cma.rnr_retry_count = 0;
580
581 return 0;
582
583out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500584 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400585out1:
586 return rc;
587}
588
589/*
590 * rpcrdma_ep_destroy
591 *
592 * Disconnect and destroy endpoint. After this, the only
593 * valid operations on the ep are to free it (if dynamically
594 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400595 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400596void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400597rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
598{
Chuck Lever25524282018-03-19 14:23:16 -0400599 if (ia->ri_id && ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400600 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400601 rdma_destroy_qp(ia->ri_id);
602 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400603 }
604
Chuck Lever25524282018-03-19 14:23:16 -0400605 if (ep->rep_attr.recv_cq)
606 ib_free_cq(ep->rep_attr.recv_cq);
607 if (ep->rep_attr.send_cq)
608 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400609}
610
Chuck Levera9b0e382017-04-11 13:23:26 -0400611/* Re-establish a connection after a device removal event.
612 * Unlike a normal reconnection, a fresh PD and a new set
613 * of MRs and buffers is needed.
614 */
615static int
616rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
617 struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
618{
Chuck Levera9b0e382017-04-11 13:23:26 -0400619 int rc, err;
620
Chuck Leverb4744e02017-12-20 16:31:29 -0500621 trace_xprtrdma_reinsert(r_xprt);
Chuck Levera9b0e382017-04-11 13:23:26 -0400622
623 rc = -EHOSTUNREACH;
Chuck Leverdd229ce2017-12-14 20:56:58 -0500624 if (rpcrdma_ia_open(r_xprt))
Chuck Levera9b0e382017-04-11 13:23:26 -0400625 goto out1;
626
627 rc = -ENOMEM;
628 err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
629 if (err) {
630 pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
631 goto out2;
632 }
633
634 rc = -ENETUNREACH;
635 err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
636 if (err) {
637 pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
638 goto out3;
639 }
640
Chuck Lever96cedde2017-12-14 20:57:55 -0500641 rpcrdma_mrs_create(r_xprt);
Chuck Levera9b0e382017-04-11 13:23:26 -0400642 return 0;
643
644out3:
645 rpcrdma_ep_destroy(ep, ia);
646out2:
647 rpcrdma_ia_close(ia);
648out1:
649 return rc;
650}
651
Chuck Lever18908962017-04-11 13:23:18 -0400652static int
653rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
654 struct rpcrdma_ia *ia)
655{
Chuck Lever18908962017-04-11 13:23:18 -0400656 struct rdma_cm_id *id, *old;
657 int err, rc;
658
Chuck Leverb4744e02017-12-20 16:31:29 -0500659 trace_xprtrdma_reconnect(r_xprt);
Chuck Lever18908962017-04-11 13:23:18 -0400660
661 rpcrdma_ep_disconnect(ep, ia);
662
663 rc = -EHOSTUNREACH;
Chuck Leverdd229ce2017-12-14 20:56:58 -0500664 id = rpcrdma_create_id(r_xprt, ia);
Chuck Lever18908962017-04-11 13:23:18 -0400665 if (IS_ERR(id))
666 goto out;
667
668 /* As long as the new ID points to the same device as the
669 * old ID, we can reuse the transport's existing PD and all
670 * previously allocated MRs. Also, the same device means
671 * the transport's previous DMA mappings are still valid.
672 *
673 * This is a sanity check only. There should be no way these
674 * point to two different devices here.
675 */
676 old = id;
677 rc = -ENETUNREACH;
678 if (ia->ri_device != id->device) {
679 pr_err("rpcrdma: can't reconnect on different device!\n");
680 goto out_destroy;
681 }
682
683 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
Chuck Leverddbb3472018-12-19 10:59:39 -0500684 if (err)
Chuck Lever18908962017-04-11 13:23:18 -0400685 goto out_destroy;
Chuck Lever18908962017-04-11 13:23:18 -0400686
687 /* Atomically replace the transport's ID and QP. */
688 rc = 0;
689 old = ia->ri_id;
690 ia->ri_id = id;
691 rdma_destroy_qp(old);
692
693out_destroy:
Chuck Lever56a6bd12017-04-11 13:23:34 -0400694 rdma_destroy_id(old);
Chuck Lever18908962017-04-11 13:23:18 -0400695out:
696 return rc;
697}
698
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400699/*
700 * Connect unconnected endpoint.
701 */
702int
703rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
704{
Chuck Lever0a904872017-02-08 17:00:35 -0500705 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
706 rx_ia);
Chuck Lever31e62d22018-10-01 14:26:08 -0400707 struct rpc_xprt *xprt = &r_xprt->rx_xprt;
Chuck Lever18908962017-04-11 13:23:18 -0400708 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400709
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400710retry:
Chuck Lever18908962017-04-11 13:23:18 -0400711 switch (ep->rep_connected) {
712 case 0:
Chuck Leverec62f402014-05-28 10:34:07 -0400713 dprintk("RPC: %s: connecting...\n", __func__);
714 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
715 if (rc) {
Chuck Lever18908962017-04-11 13:23:18 -0400716 rc = -ENETUNREACH;
717 goto out_noupdate;
Chuck Leverec62f402014-05-28 10:34:07 -0400718 }
Chuck Lever18908962017-04-11 13:23:18 -0400719 break;
Chuck Levera9b0e382017-04-11 13:23:26 -0400720 case -ENODEV:
721 rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
722 if (rc)
723 goto out_noupdate;
724 break;
Chuck Lever18908962017-04-11 13:23:18 -0400725 default:
726 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
727 if (rc)
728 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400729 }
730
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400731 ep->rep_connected = 0;
Chuck Lever31e62d22018-10-01 14:26:08 -0400732 xprt_clear_connected(xprt);
733
Chuck Lever8d4fb8f2018-07-28 10:46:47 -0400734 rpcrdma_post_recvs(r_xprt, true);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400735
736 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
Chuck Leverddbb3472018-12-19 10:59:39 -0500737 if (rc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400738 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400739
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400740 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400741 if (ep->rep_connected <= 0) {
Chuck Lever0a904872017-02-08 17:00:35 -0500742 if (ep->rep_connected == -EAGAIN)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400743 goto retry;
744 rc = ep->rep_connected;
Chuck Lever0a904872017-02-08 17:00:35 -0500745 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400746 }
747
Chuck Lever0a904872017-02-08 17:00:35 -0500748 dprintk("RPC: %s: connected\n", __func__);
Chuck Lever7c8d9e72018-05-04 15:35:20 -0400749
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400750out:
751 if (rc)
752 ep->rep_connected = rc;
Chuck Lever18908962017-04-11 13:23:18 -0400753
754out_noupdate:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400755 return rc;
756}
757
Chuck Lever6d2d0ee2018-12-19 10:58:29 -0500758/**
759 * rpcrdma_ep_disconnect - Disconnect underlying transport
760 * @ep: endpoint to disconnect
761 * @ia: associated interface adapter
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400762 *
763 * This is separate from destroy to facilitate the ability
764 * to reconnect without recreating the endpoint.
765 *
766 * This call is not reentrant, and must not be made in parallel
767 * on the same endpoint.
768 */
Chuck Lever282191c2014-07-29 17:25:55 -0400769void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400770rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
771{
Chuck Lever6d2d0ee2018-12-19 10:58:29 -0500772 struct rpcrdma_xprt *r_xprt = container_of(ep, struct rpcrdma_xprt,
773 rx_ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400774 int rc;
775
Chuck Lever6d2d0ee2018-12-19 10:58:29 -0500776 /* returns without wait if ID is not connected */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400777 rc = rdma_disconnect(ia->ri_id);
Chuck Leverb4744e02017-12-20 16:31:29 -0500778 if (!rc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400779 wait_event_interruptible(ep->rep_connect_wait,
780 ep->rep_connected != 1);
Chuck Leverb4744e02017-12-20 16:31:29 -0500781 else
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400782 ep->rep_connected = rc;
Chuck Lever6d2d0ee2018-12-19 10:58:29 -0500783 trace_xprtrdma_disconnect(r_xprt, rc);
Chuck Lever550d7502016-05-02 14:41:47 -0400784
Chuck Lever6d2d0ee2018-12-19 10:58:29 -0500785 rpcrdma_xprt_drain(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400786}
787
Chuck Leverae729502017-10-20 10:48:12 -0400788/* Fixed-size circular FIFO queue. This implementation is wait-free and
789 * lock-free.
790 *
791 * Consumer is the code path that posts Sends. This path dequeues a
792 * sendctx for use by a Send operation. Multiple consumer threads
793 * are serialized by the RPC transport lock, which allows only one
794 * ->send_request call at a time.
795 *
796 * Producer is the code path that handles Send completions. This path
797 * enqueues a sendctx that has been completed. Multiple producer
798 * threads are serialized by the ib_poll_cq() function.
799 */
800
801/* rpcrdma_sendctxs_destroy() assumes caller has already quiesced
802 * queue activity, and ib_drain_qp has flushed all remaining Send
803 * requests.
804 */
805static void rpcrdma_sendctxs_destroy(struct rpcrdma_buffer *buf)
806{
807 unsigned long i;
808
809 for (i = 0; i <= buf->rb_sc_last; i++)
810 kfree(buf->rb_sc_ctxs[i]);
811 kfree(buf->rb_sc_ctxs);
812}
813
814static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ia *ia)
815{
816 struct rpcrdma_sendctx *sc;
817
818 sc = kzalloc(sizeof(*sc) +
819 ia->ri_max_send_sges * sizeof(struct ib_sge),
820 GFP_KERNEL);
821 if (!sc)
822 return NULL;
823
824 sc->sc_wr.wr_cqe = &sc->sc_cqe;
825 sc->sc_wr.sg_list = sc->sc_sges;
826 sc->sc_wr.opcode = IB_WR_SEND;
827 sc->sc_cqe.done = rpcrdma_wc_send;
828 return sc;
829}
830
831static int rpcrdma_sendctxs_create(struct rpcrdma_xprt *r_xprt)
832{
833 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
834 struct rpcrdma_sendctx *sc;
835 unsigned long i;
836
837 /* Maximum number of concurrent outstanding Send WRs. Capping
838 * the circular queue size stops Send Queue overflow by causing
839 * the ->send_request call to fail temporarily before too many
840 * Sends are posted.
841 */
842 i = buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS;
843 dprintk("RPC: %s: allocating %lu send_ctxs\n", __func__, i);
844 buf->rb_sc_ctxs = kcalloc(i, sizeof(sc), GFP_KERNEL);
845 if (!buf->rb_sc_ctxs)
846 return -ENOMEM;
847
848 buf->rb_sc_last = i - 1;
849 for (i = 0; i <= buf->rb_sc_last; i++) {
850 sc = rpcrdma_sendctx_create(&r_xprt->rx_ia);
851 if (!sc)
Dan Carpenter6e17f582019-01-05 16:06:48 +0300852 return -ENOMEM;
Chuck Leverae729502017-10-20 10:48:12 -0400853
854 sc->sc_xprt = r_xprt;
855 buf->rb_sc_ctxs[i] = sc;
856 }
857
858 return 0;
Chuck Leverae729502017-10-20 10:48:12 -0400859}
860
861/* The sendctx queue is not guaranteed to have a size that is a
862 * power of two, thus the helpers in circ_buf.h cannot be used.
863 * The other option is to use modulus (%), which can be expensive.
864 */
865static unsigned long rpcrdma_sendctx_next(struct rpcrdma_buffer *buf,
866 unsigned long item)
867{
868 return likely(item < buf->rb_sc_last) ? item + 1 : 0;
869}
870
871/**
872 * rpcrdma_sendctx_get_locked - Acquire a send context
873 * @buf: transport buffers from which to acquire an unused context
874 *
875 * Returns pointer to a free send completion context; or NULL if
876 * the queue is empty.
877 *
878 * Usage: Called to acquire an SGE array before preparing a Send WR.
879 *
880 * The caller serializes calls to this function (per rpcrdma_buffer),
881 * and provides an effective memory barrier that flushes the new value
882 * of rb_sc_head.
883 */
884struct rpcrdma_sendctx *rpcrdma_sendctx_get_locked(struct rpcrdma_buffer *buf)
885{
886 struct rpcrdma_xprt *r_xprt;
887 struct rpcrdma_sendctx *sc;
888 unsigned long next_head;
889
890 next_head = rpcrdma_sendctx_next(buf, buf->rb_sc_head);
891
892 if (next_head == READ_ONCE(buf->rb_sc_tail))
893 goto out_emptyq;
894
895 /* ORDER: item must be accessed _before_ head is updated */
896 sc = buf->rb_sc_ctxs[next_head];
897
898 /* Releasing the lock in the caller acts as a memory
899 * barrier that flushes rb_sc_head.
900 */
901 buf->rb_sc_head = next_head;
902
903 return sc;
904
905out_emptyq:
906 /* The queue is "empty" if there have not been enough Send
907 * completions recently. This is a sign the Send Queue is
908 * backing up. Cause the caller to pause and try again.
909 */
Chuck Lever2fad6592018-05-04 15:35:57 -0400910 set_bit(RPCRDMA_BUF_F_EMPTY_SCQ, &buf->rb_flags);
Chuck Leverae729502017-10-20 10:48:12 -0400911 r_xprt = container_of(buf, struct rpcrdma_xprt, rx_buf);
912 r_xprt->rx_stats.empty_sendctx_q++;
913 return NULL;
914}
915
916/**
917 * rpcrdma_sendctx_put_locked - Release a send context
918 * @sc: send context to release
919 *
920 * Usage: Called from Send completion to return a sendctxt
921 * to the queue.
922 *
923 * The caller serializes calls to this function (per rpcrdma_buffer).
924 */
Chuck Leverefd81e92018-05-04 15:35:41 -0400925static void
926rpcrdma_sendctx_put_locked(struct rpcrdma_sendctx *sc)
Chuck Leverae729502017-10-20 10:48:12 -0400927{
928 struct rpcrdma_buffer *buf = &sc->sc_xprt->rx_buf;
929 unsigned long next_tail;
930
931 /* Unmap SGEs of previously completed by unsignaled
932 * Sends by walking up the queue until @sc is found.
933 */
934 next_tail = buf->rb_sc_tail;
935 do {
936 next_tail = rpcrdma_sendctx_next(buf, next_tail);
937
938 /* ORDER: item must be accessed _before_ tail is updated */
939 rpcrdma_unmap_sendctx(buf->rb_sc_ctxs[next_tail]);
940
941 } while (buf->rb_sc_ctxs[next_tail] != sc);
942
943 /* Paired with READ_ONCE */
944 smp_store_release(&buf->rb_sc_tail, next_tail);
Chuck Lever2fad6592018-05-04 15:35:57 -0400945
946 if (test_and_clear_bit(RPCRDMA_BUF_F_EMPTY_SCQ, &buf->rb_flags)) {
947 smp_mb__after_atomic();
948 xprt_write_space(&sc->sc_xprt->rx_xprt);
949 }
Chuck Leverae729502017-10-20 10:48:12 -0400950}
951
Chuck Lever505bbe62016-06-29 13:52:54 -0400952static void
Chuck Lever96cedde2017-12-14 20:57:55 -0500953rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt)
Chuck Levere2ac2362016-06-29 13:54:00 -0400954{
955 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
956 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
957 unsigned int count;
958 LIST_HEAD(free);
959 LIST_HEAD(all);
960
Chuck Leverc421ece2018-10-01 14:25:20 -0400961 for (count = 0; count < ia->ri_max_segs; count++) {
Chuck Lever96cedde2017-12-14 20:57:55 -0500962 struct rpcrdma_mr *mr;
Chuck Levere2ac2362016-06-29 13:54:00 -0400963 int rc;
964
Chuck Lever96cedde2017-12-14 20:57:55 -0500965 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
966 if (!mr)
Chuck Levere2ac2362016-06-29 13:54:00 -0400967 break;
968
Chuck Lever5f624122018-12-19 10:59:01 -0500969 rc = frwr_init_mr(ia, mr);
Chuck Levere2ac2362016-06-29 13:54:00 -0400970 if (rc) {
Chuck Lever96cedde2017-12-14 20:57:55 -0500971 kfree(mr);
Chuck Levere2ac2362016-06-29 13:54:00 -0400972 break;
973 }
974
Chuck Lever96cedde2017-12-14 20:57:55 -0500975 mr->mr_xprt = r_xprt;
Chuck Levere2ac2362016-06-29 13:54:00 -0400976
Chuck Lever96cedde2017-12-14 20:57:55 -0500977 list_add(&mr->mr_list, &free);
978 list_add(&mr->mr_all, &all);
Chuck Levere2ac2362016-06-29 13:54:00 -0400979 }
980
Chuck Lever96cedde2017-12-14 20:57:55 -0500981 spin_lock(&buf->rb_mrlock);
982 list_splice(&free, &buf->rb_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -0400983 list_splice(&all, &buf->rb_all);
984 r_xprt->rx_stats.mrs_allocated += count;
Chuck Lever96cedde2017-12-14 20:57:55 -0500985 spin_unlock(&buf->rb_mrlock);
Chuck Lever1c443eff2017-12-20 16:31:21 -0500986 trace_xprtrdma_createmrs(r_xprt, count);
Chuck Lever9e679d52018-02-28 15:30:44 -0500987
988 xprt_write_space(&r_xprt->rx_xprt);
Chuck Levere2ac2362016-06-29 13:54:00 -0400989}
990
991static void
992rpcrdma_mr_refresh_worker(struct work_struct *work)
993{
994 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
995 rb_refresh_worker.work);
996 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
997 rx_buf);
998
Chuck Lever96cedde2017-12-14 20:57:55 -0500999 rpcrdma_mrs_create(r_xprt);
Chuck Levere2ac2362016-06-29 13:54:00 -04001000}
1001
Chuck Lever1769e6a2019-04-24 09:39:05 -04001002/**
1003 * rpcrdma_req_create - Allocate an rpcrdma_req object
1004 * @r_xprt: controlling r_xprt
Chuck Leverbb93a1a2019-04-24 09:39:21 -04001005 * @size: initial size, in bytes, of send and receive buffers
Chuck Lever1769e6a2019-04-24 09:39:05 -04001006 * @flags: GFP flags passed to memory allocators
1007 *
1008 * Returns an allocated and fully initialized rpcrdma_req or NULL.
1009 */
Chuck Leverbb93a1a2019-04-24 09:39:21 -04001010struct rpcrdma_req *rpcrdma_req_create(struct rpcrdma_xprt *r_xprt, size_t size,
1011 gfp_t flags)
Chuck Lever13924022015-01-21 11:03:52 -05001012{
Chuck Leverf531a5d2015-10-24 17:27:43 -04001013 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever2dd4a012018-02-28 15:31:05 -05001014 struct rpcrdma_regbuf *rb;
Chuck Lever13924022015-01-21 11:03:52 -05001015 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -05001016
Chuck Lever1769e6a2019-04-24 09:39:05 -04001017 req = kzalloc(sizeof(*req), flags);
Chuck Lever13924022015-01-21 11:03:52 -05001018 if (req == NULL)
Chuck Leverbb93a1a2019-04-24 09:39:21 -04001019 goto out1;
Chuck Lever13924022015-01-21 11:03:52 -05001020
Chuck Leverd2832af2019-04-24 09:39:32 -04001021 rb = rpcrdma_regbuf_alloc(RPCRDMA_HDRBUF_SIZE, DMA_TO_DEVICE, flags);
Chuck Leverbb93a1a2019-04-24 09:39:21 -04001022 if (!rb)
1023 goto out2;
Chuck Lever2dd4a012018-02-28 15:31:05 -05001024 req->rl_rdmabuf = rb;
Chuck Lever8cec3db2019-04-24 09:39:16 -04001025 xdr_buf_init(&req->rl_hdrbuf, rdmab_data(rb), rdmab_length(rb));
Chuck Leverbb93a1a2019-04-24 09:39:21 -04001026
Chuck Leverd2832af2019-04-24 09:39:32 -04001027 req->rl_sendbuf = rpcrdma_regbuf_alloc(size, DMA_TO_DEVICE, flags);
Chuck Leverbb93a1a2019-04-24 09:39:21 -04001028 if (!req->rl_sendbuf)
1029 goto out3;
1030
Chuck Leverd2832af2019-04-24 09:39:32 -04001031 req->rl_recvbuf = rpcrdma_regbuf_alloc(size, DMA_NONE, flags);
Chuck Leverbb93a1a2019-04-24 09:39:21 -04001032 if (!req->rl_recvbuf)
1033 goto out4;
1034
Chuck Lever2dd4a012018-02-28 15:31:05 -05001035 req->rl_buffer = buffer;
1036 INIT_LIST_HEAD(&req->rl_registered);
Chuck Lever92f44332018-12-19 10:59:33 -05001037 spin_lock(&buffer->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001038 list_add(&req->rl_all, &buffer->rb_allreqs);
Chuck Lever92f44332018-12-19 10:59:33 -05001039 spin_unlock(&buffer->rb_lock);
Chuck Lever13924022015-01-21 11:03:52 -05001040 return req;
Chuck Leverbb93a1a2019-04-24 09:39:21 -04001041
1042out4:
1043 kfree(req->rl_sendbuf);
1044out3:
1045 kfree(req->rl_rdmabuf);
1046out2:
1047 kfree(req);
1048out1:
1049 return NULL;
Chuck Lever13924022015-01-21 11:03:52 -05001050}
1051
Chuck Lever23146502019-04-24 09:39:11 -04001052static bool rpcrdma_rep_create(struct rpcrdma_xprt *r_xprt, bool temp)
Chuck Lever13924022015-01-21 11:03:52 -05001053{
1054 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Leverd698c4a2017-12-14 20:56:09 -05001055 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -05001056 struct rpcrdma_rep *rep;
Chuck Lever13924022015-01-21 11:03:52 -05001057
Chuck Lever6b1184c2015-01-21 11:04:25 -05001058 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -05001059 if (rep == NULL)
1060 goto out;
Chuck Lever13924022015-01-21 11:03:52 -05001061
Chuck Leverd2832af2019-04-24 09:39:32 -04001062 rep->rr_rdmabuf = rpcrdma_regbuf_alloc(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -04001063 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever8cec3db2019-04-24 09:39:16 -04001064 if (!rep->rr_rdmabuf)
Chuck Lever13924022015-01-21 11:03:52 -05001065 goto out_free;
Chuck Lever8cec3db2019-04-24 09:39:16 -04001066 xdr_buf_init(&rep->rr_hdrbuf, rdmab_data(rep->rr_rdmabuf),
Chuck Lever96f87782017-08-03 14:30:03 -04001067 rdmab_length(rep->rr_rdmabuf));
Chuck Lever13924022015-01-21 11:03:52 -05001068
Chuck Lever1519e962016-09-15 10:57:49 -04001069 rep->rr_cqe.done = rpcrdma_wc_receive;
Chuck Leverfed171b2015-05-26 11:51:37 -04001070 rep->rr_rxprt = r_xprt;
Chuck Leverd8f532d2017-10-16 15:01:30 -04001071 INIT_WORK(&rep->rr_work, rpcrdma_deferred_completion);
Chuck Lever6ea8e712016-09-15 10:56:51 -04001072 rep->rr_recv_wr.next = NULL;
1073 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
1074 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
1075 rep->rr_recv_wr.num_sge = 1;
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001076 rep->rr_temp = temp;
Chuck Leverd698c4a2017-12-14 20:56:09 -05001077
1078 spin_lock(&buf->rb_lock);
1079 list_add(&rep->rr_list, &buf->rb_recv_bufs);
1080 spin_unlock(&buf->rb_lock);
Chuck Lever23146502019-04-24 09:39:11 -04001081 return true;
Chuck Lever13924022015-01-21 11:03:52 -05001082
1083out_free:
1084 kfree(rep);
1085out:
Chuck Lever23146502019-04-24 09:39:11 -04001086 return false;
Chuck Lever13924022015-01-21 11:03:52 -05001087}
1088
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001089int
Chuck Leverac920d02015-01-21 11:03:44 -05001090rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001091{
Chuck Leverac920d02015-01-21 11:03:44 -05001092 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001093 int i, rc;
1094
Chuck Lever512ccfb2018-10-01 14:26:29 -04001095 buf->rb_flags = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001096 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001097 buf->rb_bc_srv_max_requests = 0;
Chuck Lever96cedde2017-12-14 20:57:55 -05001098 spin_lock_init(&buf->rb_mrlock);
Chuck Lever505bbe62016-06-29 13:52:54 -04001099 spin_lock_init(&buf->rb_lock);
Chuck Lever96cedde2017-12-14 20:57:55 -05001100 INIT_LIST_HEAD(&buf->rb_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001101 INIT_LIST_HEAD(&buf->rb_all);
Chuck Levere2ac2362016-06-29 13:54:00 -04001102 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1103 rpcrdma_mr_refresh_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001104
Chuck Lever96cedde2017-12-14 20:57:55 -05001105 rpcrdma_mrs_create(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001106
Chuck Lever1e465fd2015-10-24 17:27:02 -04001107 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001108 INIT_LIST_HEAD(&buf->rb_allreqs);
Chuck Lever1769e6a2019-04-24 09:39:05 -04001109
1110 rc = -ENOMEM;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001111 for (i = 0; i < buf->rb_max_requests; i++) {
1112 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001113
Chuck Leverbb93a1a2019-04-24 09:39:21 -04001114 req = rpcrdma_req_create(r_xprt, RPCRDMA_V1_DEF_INLINE_SIZE,
1115 GFP_KERNEL);
Chuck Lever1769e6a2019-04-24 09:39:05 -04001116 if (!req)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001117 goto out;
Chuck Levera80d66c2017-06-08 11:52:12 -04001118 list_add(&req->rl_list, &buf->rb_send_bufs);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001119 }
1120
Chuck Lever8d4fb8f2018-07-28 10:46:47 -04001121 buf->rb_credits = 1;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001122 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever13924022015-01-21 11:03:52 -05001123
Chuck Leverae729502017-10-20 10:48:12 -04001124 rc = rpcrdma_sendctxs_create(r_xprt);
1125 if (rc)
1126 goto out;
1127
Chuck Lever6d2d0ee2018-12-19 10:58:29 -05001128 buf->rb_completion_wq = alloc_workqueue("rpcrdma-%s",
1129 WQ_MEM_RECLAIM | WQ_HIGHPRI,
1130 0,
1131 r_xprt->rx_xprt.address_strings[RPC_DISPLAY_ADDR]);
Dan Carpenter4429b662019-01-05 16:06:07 +03001132 if (!buf->rb_completion_wq) {
1133 rc = -ENOMEM;
Chuck Lever6d2d0ee2018-12-19 10:58:29 -05001134 goto out;
Dan Carpenter4429b662019-01-05 16:06:07 +03001135 }
Chuck Lever6d2d0ee2018-12-19 10:58:29 -05001136
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001137 return 0;
1138out:
1139 rpcrdma_buffer_destroy(buf);
1140 return rc;
1141}
1142
Chuck Lever23146502019-04-24 09:39:11 -04001143static void rpcrdma_rep_destroy(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -05001144{
Chuck Leverd2832af2019-04-24 09:39:32 -04001145 rpcrdma_regbuf_free(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001146 kfree(rep);
1147}
1148
Chuck Lever92f44332018-12-19 10:59:33 -05001149/**
1150 * rpcrdma_req_destroy - Destroy an rpcrdma_req object
1151 * @req: unused object to be destroyed
1152 *
1153 * This function assumes that the caller prevents concurrent device
1154 * unload and transport tear-down.
1155 */
Chuck Leverf531a5d2015-10-24 17:27:43 -04001156void
Chuck Lever92f44332018-12-19 10:59:33 -05001157rpcrdma_req_destroy(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -05001158{
Chuck Lever92f44332018-12-19 10:59:33 -05001159 list_del(&req->rl_all);
1160
Chuck Leverd2832af2019-04-24 09:39:32 -04001161 rpcrdma_regbuf_free(req->rl_recvbuf);
1162 rpcrdma_regbuf_free(req->rl_sendbuf);
1163 rpcrdma_regbuf_free(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001164 kfree(req);
1165}
1166
Chuck Levere2ac2362016-06-29 13:54:00 -04001167static void
Chuck Lever96cedde2017-12-14 20:57:55 -05001168rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf)
Chuck Levere2ac2362016-06-29 13:54:00 -04001169{
1170 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1171 rx_buf);
Chuck Lever96cedde2017-12-14 20:57:55 -05001172 struct rpcrdma_mr *mr;
Chuck Levere2ac2362016-06-29 13:54:00 -04001173 unsigned int count;
1174
1175 count = 0;
Chuck Lever96cedde2017-12-14 20:57:55 -05001176 spin_lock(&buf->rb_mrlock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001177 while (!list_empty(&buf->rb_all)) {
Chuck Lever96cedde2017-12-14 20:57:55 -05001178 mr = list_entry(buf->rb_all.next, struct rpcrdma_mr, mr_all);
1179 list_del(&mr->mr_all);
Chuck Levere2ac2362016-06-29 13:54:00 -04001180
Chuck Lever96cedde2017-12-14 20:57:55 -05001181 spin_unlock(&buf->rb_mrlock);
Chuck Lever054f1552018-05-01 11:37:14 -04001182
1183 /* Ensure MW is not on any rl_registered list */
1184 if (!list_empty(&mr->mr_list))
1185 list_del(&mr->mr_list);
1186
Chuck Lever5f624122018-12-19 10:59:01 -05001187 frwr_release_mr(mr);
Chuck Levere2ac2362016-06-29 13:54:00 -04001188 count++;
Chuck Lever96cedde2017-12-14 20:57:55 -05001189 spin_lock(&buf->rb_mrlock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001190 }
Chuck Lever96cedde2017-12-14 20:57:55 -05001191 spin_unlock(&buf->rb_mrlock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001192 r_xprt->rx_stats.mrs_allocated = 0;
1193
1194 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1195}
1196
Chuck Leveraf65ed42018-12-19 11:00:37 -05001197/**
1198 * rpcrdma_buffer_destroy - Release all hw resources
1199 * @buf: root control block for resources
1200 *
1201 * ORDERING: relies on a prior ib_drain_qp :
1202 * - No more Send or Receive completions can occur
1203 * - All MRs, reps, and reqs are returned to their free lists
1204 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001205void
1206rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1207{
Chuck Lever9378b272017-04-11 13:22:29 -04001208 cancel_delayed_work_sync(&buf->rb_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001209
Chuck Lever6d2d0ee2018-12-19 10:58:29 -05001210 if (buf->rb_completion_wq) {
1211 destroy_workqueue(buf->rb_completion_wq);
1212 buf->rb_completion_wq = NULL;
1213 }
1214
Chuck Leverae729502017-10-20 10:48:12 -04001215 rpcrdma_sendctxs_destroy(buf);
1216
Chuck Lever1e465fd2015-10-24 17:27:02 -04001217 while (!list_empty(&buf->rb_recv_bufs)) {
1218 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001219
Chuck Lever9d95cd52018-05-04 15:35:36 -04001220 rep = list_first_entry(&buf->rb_recv_bufs,
1221 struct rpcrdma_rep, rr_list);
1222 list_del(&rep->rr_list);
Chuck Lever23146502019-04-24 09:39:11 -04001223 rpcrdma_rep_destroy(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001224 }
1225
Chuck Lever92f44332018-12-19 10:59:33 -05001226 while (!list_empty(&buf->rb_send_bufs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001227 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001228
Chuck Lever92f44332018-12-19 10:59:33 -05001229 req = list_first_entry(&buf->rb_send_bufs,
1230 struct rpcrdma_req, rl_list);
1231 list_del(&req->rl_list);
1232 rpcrdma_req_destroy(req);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001233 }
1234
Chuck Lever96cedde2017-12-14 20:57:55 -05001235 rpcrdma_mrs_destroy(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001236}
1237
Chuck Lever96cedde2017-12-14 20:57:55 -05001238/**
1239 * rpcrdma_mr_get - Allocate an rpcrdma_mr object
1240 * @r_xprt: controlling transport
1241 *
1242 * Returns an initialized rpcrdma_mr or NULL if no free
1243 * rpcrdma_mr objects are available.
1244 */
1245struct rpcrdma_mr *
1246rpcrdma_mr_get(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001247{
Chuck Lever346aa662015-05-26 11:52:06 -04001248 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Lever96cedde2017-12-14 20:57:55 -05001249 struct rpcrdma_mr *mr = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001250
Chuck Lever96cedde2017-12-14 20:57:55 -05001251 spin_lock(&buf->rb_mrlock);
1252 if (!list_empty(&buf->rb_mrs))
1253 mr = rpcrdma_mr_pop(&buf->rb_mrs);
1254 spin_unlock(&buf->rb_mrlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001255
Chuck Lever96cedde2017-12-14 20:57:55 -05001256 if (!mr)
1257 goto out_nomrs;
1258 return mr;
Chuck Levere2ac2362016-06-29 13:54:00 -04001259
Chuck Lever96cedde2017-12-14 20:57:55 -05001260out_nomrs:
Chuck Lever1c443eff2017-12-20 16:31:21 -05001261 trace_xprtrdma_nomrs(r_xprt);
Chuck Leverbebd0312017-04-11 13:23:10 -04001262 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1263 schedule_delayed_work(&buf->rb_refresh_worker, 0);
Chuck Levere2ac2362016-06-29 13:54:00 -04001264
1265 /* Allow the reply handler and refresh worker to run */
1266 cond_resched();
1267
1268 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001269}
1270
Chuck Leverec12e472017-12-14 20:58:04 -05001271static void
1272__rpcrdma_mr_put(struct rpcrdma_buffer *buf, struct rpcrdma_mr *mr)
1273{
1274 spin_lock(&buf->rb_mrlock);
1275 rpcrdma_mr_push(mr, &buf->rb_mrs);
1276 spin_unlock(&buf->rb_mrlock);
1277}
1278
Chuck Lever96cedde2017-12-14 20:57:55 -05001279/**
1280 * rpcrdma_mr_put - Release an rpcrdma_mr object
1281 * @mr: object to release
1282 *
1283 */
Chuck Lever346aa662015-05-26 11:52:06 -04001284void
Chuck Lever96cedde2017-12-14 20:57:55 -05001285rpcrdma_mr_put(struct rpcrdma_mr *mr)
Chuck Leverc2922c02014-07-29 17:24:36 -04001286{
Chuck Leverec12e472017-12-14 20:58:04 -05001287 __rpcrdma_mr_put(&mr->mr_xprt->rx_buf, mr);
1288}
Chuck Leverc2922c02014-07-29 17:24:36 -04001289
Chuck Leverec12e472017-12-14 20:58:04 -05001290/**
1291 * rpcrdma_mr_unmap_and_put - DMA unmap an MR and release it
1292 * @mr: object to release
1293 *
1294 */
1295void
1296rpcrdma_mr_unmap_and_put(struct rpcrdma_mr *mr)
1297{
1298 struct rpcrdma_xprt *r_xprt = mr->mr_xprt;
1299
Chuck Levere2f34e22018-12-19 10:58:13 -05001300 if (mr->mr_dir != DMA_NONE) {
1301 trace_xprtrdma_mr_unmap(mr);
1302 ib_dma_unmap_sg(r_xprt->rx_ia.ri_device,
1303 mr->mr_sg, mr->mr_nents, mr->mr_dir);
1304 mr->mr_dir = DMA_NONE;
1305 }
Chuck Leverec12e472017-12-14 20:58:04 -05001306 __rpcrdma_mr_put(&r_xprt->rx_buf, mr);
Chuck Leverc2922c02014-07-29 17:24:36 -04001307}
1308
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001309/**
1310 * rpcrdma_buffer_get - Get a request buffer
1311 * @buffers: Buffer pool from which to obtain a buffer
Chuck Lever78d506e2016-09-06 11:22:49 -04001312 *
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001313 * Returns a fresh rpcrdma_req, or NULL if none are available.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001314 */
1315struct rpcrdma_req *
1316rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1317{
1318 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001319
Chuck Levera5b027e2015-10-24 17:27:27 -04001320 spin_lock(&buffers->rb_lock);
Chuck Levere68699c2018-05-04 15:35:31 -04001321 req = list_first_entry_or_null(&buffers->rb_send_bufs,
1322 struct rpcrdma_req, rl_list);
1323 if (req)
1324 list_del_init(&req->rl_list);
Chuck Levera5b027e2015-10-24 17:27:27 -04001325 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001326 return req;
1327}
1328
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001329/**
1330 * rpcrdma_buffer_put - Put request/reply buffers back into pool
1331 * @req: object to return
1332 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001333 */
1334void
1335rpcrdma_buffer_put(struct rpcrdma_req *req)
1336{
1337 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001338 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001339
Chuck Lever1e465fd2015-10-24 17:27:02 -04001340 req->rl_reply = NULL;
1341
Chuck Levera5b027e2015-10-24 17:27:27 -04001342 spin_lock(&buffers->rb_lock);
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001343 list_add(&req->rl_list, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001344 if (rep) {
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001345 if (!rep->rr_temp) {
1346 list_add(&rep->rr_list, &buffers->rb_recv_bufs);
1347 rep = NULL;
1348 }
Chuck Lever05c97462016-09-06 11:22:58 -04001349 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001350 spin_unlock(&buffers->rb_lock);
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001351 if (rep)
Chuck Lever23146502019-04-24 09:39:11 -04001352 rpcrdma_rep_destroy(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001353}
1354
1355/*
1356 * Put reply buffers back into pool when not attached to
Chuck Leverb45ccfd2014-05-28 10:32:34 -04001357 * request. This happens in error conditions.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001358 */
1359void
1360rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1361{
Chuck Leverfed171b2015-05-26 11:51:37 -04001362 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001363
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001364 if (!rep->rr_temp) {
1365 spin_lock(&buffers->rb_lock);
1366 list_add(&rep->rr_list, &buffers->rb_recv_bufs);
1367 spin_unlock(&buffers->rb_lock);
1368 } else {
Chuck Lever23146502019-04-24 09:39:11 -04001369 rpcrdma_rep_destroy(rep);
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001370 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001371}
1372
Chuck Leverd2832af2019-04-24 09:39:32 -04001373/* Returns a pointer to a rpcrdma_regbuf object, or NULL.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001374 *
1375 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001376 * receiving the payload of RDMA RECV operations. During Long Calls
Chuck Lever5f624122018-12-19 10:59:01 -05001377 * or Replies they may be registered externally via frwr_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001378 */
Chuck Leverd2832af2019-04-24 09:39:32 -04001379static struct rpcrdma_regbuf *
1380rpcrdma_regbuf_alloc(size_t size, enum dma_data_direction direction,
Chuck Lever13650c22016-09-15 10:56:26 -04001381 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001382{
1383 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001384
Chuck Lever8cec3db2019-04-24 09:39:16 -04001385 rb = kmalloc(sizeof(*rb), flags);
1386 if (!rb)
1387 return NULL;
1388 rb->rg_data = kmalloc(size, flags);
1389 if (!rb->rg_data) {
1390 kfree(rb);
1391 return NULL;
1392 }
Chuck Lever9128c3e2015-01-21 11:04:00 -05001393
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001394 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001395 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001396 rb->rg_iov.length = size;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001397 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001398}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001399
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001400/**
Chuck Lever0f665ce2019-04-24 09:39:27 -04001401 * rpcrdma_regbuf_realloc - re-allocate a SEND/RECV buffer
1402 * @rb: regbuf to reallocate
1403 * @size: size of buffer to be allocated, in bytes
1404 * @flags: GFP flags
1405 *
1406 * Returns true if reallocation was successful. If false is
1407 * returned, @rb is left untouched.
1408 */
1409bool rpcrdma_regbuf_realloc(struct rpcrdma_regbuf *rb, size_t size, gfp_t flags)
1410{
1411 void *buf;
1412
1413 buf = kmalloc(size, flags);
1414 if (!buf)
1415 return false;
1416
Chuck Leverd2832af2019-04-24 09:39:32 -04001417 rpcrdma_regbuf_dma_unmap(rb);
Chuck Lever0f665ce2019-04-24 09:39:27 -04001418 kfree(rb->rg_data);
1419
1420 rb->rg_data = buf;
1421 rb->rg_iov.length = size;
1422 return true;
1423}
1424
1425/**
Chuck Leverd2832af2019-04-24 09:39:32 -04001426 * __rpcrdma_regbuf_dma_map - DMA-map a regbuf
1427 * @r_xprt: controlling transport instance
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001428 * @rb: regbuf to be mapped
Chuck Leverd2832af2019-04-24 09:39:32 -04001429 *
1430 * Returns true if the buffer is now DMA mapped to @r_xprt's device
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001431 */
Chuck Leverd2832af2019-04-24 09:39:32 -04001432bool __rpcrdma_regbuf_dma_map(struct rpcrdma_xprt *r_xprt,
1433 struct rpcrdma_regbuf *rb)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001434{
Chuck Leverd2832af2019-04-24 09:39:32 -04001435 struct ib_device *device = r_xprt->rx_ia.ri_device;
Chuck Lever91a10c52017-04-11 13:23:02 -04001436
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001437 if (rb->rg_direction == DMA_NONE)
1438 return false;
1439
Chuck Leverd2832af2019-04-24 09:39:32 -04001440 rb->rg_iov.addr = ib_dma_map_single(device, rdmab_data(rb),
1441 rdmab_length(rb), rb->rg_direction);
Chuck Lever53b2c1c2018-12-19 11:00:06 -05001442 if (ib_dma_mapping_error(device, rdmab_addr(rb))) {
1443 trace_xprtrdma_dma_maperr(rdmab_addr(rb));
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001444 return false;
Chuck Lever53b2c1c2018-12-19 11:00:06 -05001445 }
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001446
Chuck Lever91a10c52017-04-11 13:23:02 -04001447 rb->rg_device = device;
Chuck Leverd2832af2019-04-24 09:39:32 -04001448 rb->rg_iov.lkey = r_xprt->rx_ia.ri_pd->local_dma_lkey;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001449 return true;
1450}
1451
Chuck Leverd2832af2019-04-24 09:39:32 -04001452static void rpcrdma_regbuf_dma_unmap(struct rpcrdma_regbuf *rb)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001453{
Chuck Levere89e8d8f2018-01-31 12:34:13 -05001454 if (!rb)
1455 return;
1456
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001457 if (!rpcrdma_regbuf_is_mapped(rb))
1458 return;
1459
Chuck Leverd2832af2019-04-24 09:39:32 -04001460 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb), rdmab_length(rb),
1461 rb->rg_direction);
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001462 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001463}
1464
Chuck Leverd2832af2019-04-24 09:39:32 -04001465static void rpcrdma_regbuf_free(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001466{
Chuck Leverd2832af2019-04-24 09:39:32 -04001467 rpcrdma_regbuf_dma_unmap(rb);
Chuck Lever8cec3db2019-04-24 09:39:16 -04001468 if (rb)
1469 kfree(rb->rg_data);
Chuck Levere531dca2015-08-03 13:03:20 -04001470 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001471}
1472
Chuck Lever995d3122018-12-19 11:00:32 -05001473/**
1474 * rpcrdma_ep_post - Post WRs to a transport's Send Queue
1475 * @ia: transport's device information
1476 * @ep: transport's RDMA endpoint information
1477 * @req: rpcrdma_req containing the Send WR to post
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001478 *
Chuck Lever995d3122018-12-19 11:00:32 -05001479 * Returns 0 if the post was successful, otherwise -ENOTCONN
1480 * is returned.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001481 */
1482int
1483rpcrdma_ep_post(struct rpcrdma_ia *ia,
1484 struct rpcrdma_ep *ep,
1485 struct rpcrdma_req *req)
1486{
Chuck Leverae729502017-10-20 10:48:12 -04001487 struct ib_send_wr *send_wr = &req->rl_sendctx->sc_wr;
Chuck Lever655fec62016-09-15 10:57:24 -04001488 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001489
Chuck Lever01bb35c2017-10-20 10:48:36 -04001490 if (!ep->rep_send_count ||
1491 test_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags)) {
Chuck Leverae729502017-10-20 10:48:12 -04001492 send_wr->send_flags |= IB_SEND_SIGNALED;
1493 ep->rep_send_count = ep->rep_send_batch;
1494 } else {
1495 send_wr->send_flags &= ~IB_SEND_SIGNALED;
1496 --ep->rep_send_count;
1497 }
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001498
Chuck Lever5f624122018-12-19 10:59:01 -05001499 rc = frwr_send(ia, req);
Chuck Leverab03eff2017-12-20 16:30:40 -05001500 trace_xprtrdma_post_send(req, rc);
1501 if (rc)
1502 return -ENOTCONN;
1503 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001504}
1505
Chuck Lever6ceea362018-12-19 10:58:24 -05001506static void
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001507rpcrdma_post_recvs(struct rpcrdma_xprt *r_xprt, bool temp)
Chuck Leverf531a5d2015-10-24 17:27:43 -04001508{
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001509 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Lever6ceea362018-12-19 10:58:24 -05001510 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001511 struct ib_recv_wr *wr, *bad_wr;
1512 int needed, count, rc;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001513
Chuck Lever61c208a2018-10-01 14:26:35 -04001514 rc = 0;
1515 count = 0;
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001516 needed = buf->rb_credits + (buf->rb_bc_srv_max_requests << 1);
Chuck Lever6ceea362018-12-19 10:58:24 -05001517 if (ep->rep_receive_count > needed)
Chuck Lever61c208a2018-10-01 14:26:35 -04001518 goto out;
Chuck Lever6ceea362018-12-19 10:58:24 -05001519 needed -= ep->rep_receive_count;
Chuck Levere340c2d2019-02-11 11:23:54 -05001520 if (!temp)
1521 needed += RPCRDMA_MAX_RECV_BATCH;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001522
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001523 count = 0;
1524 wr = NULL;
1525 while (needed) {
1526 struct rpcrdma_regbuf *rb;
1527 struct rpcrdma_rep *rep;
1528
1529 spin_lock(&buf->rb_lock);
1530 rep = list_first_entry_or_null(&buf->rb_recv_bufs,
1531 struct rpcrdma_rep, rr_list);
1532 if (likely(rep))
1533 list_del(&rep->rr_list);
1534 spin_unlock(&buf->rb_lock);
1535 if (!rep) {
Chuck Lever23146502019-04-24 09:39:11 -04001536 if (!rpcrdma_rep_create(r_xprt, temp))
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001537 break;
1538 continue;
1539 }
1540
1541 rb = rep->rr_rdmabuf;
Chuck Leverd2832af2019-04-24 09:39:32 -04001542 if (!rpcrdma_regbuf_dma_map(r_xprt, rb)) {
1543 rpcrdma_recv_buffer_put(rep);
1544 break;
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001545 }
1546
1547 trace_xprtrdma_post_recv(rep->rr_recv_wr.wr_cqe);
1548 rep->rr_recv_wr.next = wr;
1549 wr = &rep->rr_recv_wr;
1550 ++count;
1551 --needed;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001552 }
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001553 if (!count)
Chuck Lever61c208a2018-10-01 14:26:35 -04001554 goto out;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001555
Bart Van Assched34ac5c2018-07-18 09:25:32 -07001556 rc = ib_post_recv(r_xprt->rx_ia.ri_id->qp, wr,
1557 (const struct ib_recv_wr **)&bad_wr);
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001558 if (rc) {
1559 for (wr = bad_wr; wr; wr = wr->next) {
1560 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001561
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001562 rep = container_of(wr, struct rpcrdma_rep, rr_recv_wr);
1563 rpcrdma_recv_buffer_put(rep);
1564 --count;
1565 }
1566 }
Chuck Lever6ceea362018-12-19 10:58:24 -05001567 ep->rep_receive_count += count;
Chuck Lever61c208a2018-10-01 14:26:35 -04001568out:
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001569 trace_xprtrdma_post_recvs(r_xprt, count, rc);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001570}