blob: 601b78b6448323c28aa5fdfc3e6fad85303fa89f [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"
63
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040064/*
65 * Globals/Macros
66 */
67
Jeff Laytonf895b252014-11-17 16:58:04 -050068#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040069# define RPCDBG_FACILITY RPCDBG_TRANS
70#endif
71
72/*
73 * internal functions
74 */
Chuck Lever96cedde2017-12-14 20:57:55 -050075static void rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt);
76static void rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf);
Chuck Lever7c8d9e72018-05-04 15:35:20 -040077static int rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt, bool temp);
Chuck Leverbebd0312017-04-11 13:23:10 -040078static void rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040079
Chuck Leverd8f532d2017-10-16 15:01:30 -040080struct workqueue_struct *rpcrdma_receive_wq __read_mostly;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040081
Chuck Leverfe97b472015-10-24 17:27:10 -040082int
83rpcrdma_alloc_wq(void)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040084{
Chuck Leverfe97b472015-10-24 17:27:10 -040085 struct workqueue_struct *recv_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040086
Chuck Leverfe97b472015-10-24 17:27:10 -040087 recv_wq = alloc_workqueue("xprtrdma_receive",
Chuck Leverccede752017-12-04 14:04:04 -050088 WQ_MEM_RECLAIM | WQ_HIGHPRI,
Chuck Leverfe97b472015-10-24 17:27:10 -040089 0);
90 if (!recv_wq)
91 return -ENOMEM;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040092
Chuck Leverfe97b472015-10-24 17:27:10 -040093 rpcrdma_receive_wq = recv_wq;
94 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040095}
96
Chuck Leverfe97b472015-10-24 17:27:10 -040097void
98rpcrdma_destroy_wq(void)
Chuck Leverf1a03b72014-11-08 20:14:37 -050099{
Chuck Leverfe97b472015-10-24 17:27:10 -0400100 struct workqueue_struct *wq;
Chuck Leverf1a03b72014-11-08 20:14:37 -0500101
Chuck Leverfe97b472015-10-24 17:27:10 -0400102 if (rpcrdma_receive_wq) {
103 wq = rpcrdma_receive_wq;
104 rpcrdma_receive_wq = NULL;
105 destroy_workqueue(wq);
106 }
Chuck Leverf1a03b72014-11-08 20:14:37 -0500107}
108
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400109static void
110rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
111{
112 struct rpcrdma_ep *ep = context;
Chuck Lever643cf322017-12-20 16:31:45 -0500113 struct rpcrdma_xprt *r_xprt = container_of(ep, struct rpcrdma_xprt,
114 rx_ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400115
Chuck Lever643cf322017-12-20 16:31:45 -0500116 trace_xprtrdma_qp_error(r_xprt, event);
Chuck Lever2f6922c2016-11-29 10:53:21 -0500117 pr_err("rpcrdma: %s on device %s ep %p\n",
118 ib_event_msg(event->event), event->device->name, context);
119
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400120 if (ep->rep_connected == 1) {
121 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500122 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400123 wake_up_all(&ep->rep_connect_wait);
124 }
125}
126
Chuck Lever2fa8f882016-03-04 11:28:53 -0500127/**
128 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
129 * @cq: completion queue (ignored)
130 * @wc: completed WR
131 *
Chuck Lever4220a072015-10-24 17:26:45 -0400132 */
133static void
Chuck Lever2fa8f882016-03-04 11:28:53 -0500134rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400135{
Chuck Leverae729502017-10-20 10:48:12 -0400136 struct ib_cqe *cqe = wc->wr_cqe;
137 struct rpcrdma_sendctx *sc =
138 container_of(cqe, struct rpcrdma_sendctx, sc_cqe);
139
Chuck Lever2fa8f882016-03-04 11:28:53 -0500140 /* WARNING: Only wr_cqe and status are reliable at this point */
Chuck Leverab03eff2017-12-20 16:30:40 -0500141 trace_xprtrdma_wc_send(sc, wc);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500142 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
143 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
144 ib_wc_status_msg(wc->status),
145 wc->status, wc->vendor_err);
Chuck Leverae729502017-10-20 10:48:12 -0400146
147 rpcrdma_sendctx_put_locked(sc);
Chuck Leverfc664482014-05-28 10:33:25 -0400148}
149
Chuck Lever552bf222016-03-04 11:28:36 -0500150/**
Chuck Lever1519e962016-09-15 10:57:49 -0400151 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
Chuck Lever552bf222016-03-04 11:28:36 -0500152 * @cq: completion queue (ignored)
153 * @wc: completed WR
154 *
155 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400156static void
Chuck Lever1519e962016-09-15 10:57:49 -0400157rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400158{
Chuck Lever552bf222016-03-04 11:28:36 -0500159 struct ib_cqe *cqe = wc->wr_cqe;
160 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
161 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400162
Chuck Lever85024272015-01-21 11:02:04 -0500163 /* WARNING: Only wr_id and status are reliable at this point */
Chuck Lever0e0b8542018-05-04 15:35:14 -0400164 trace_xprtrdma_wc_receive(wc);
Chuck Lever85024272015-01-21 11:02:04 -0500165 if (wc->status != IB_WC_SUCCESS)
166 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400167
Chuck Lever85024272015-01-21 11:02:04 -0500168 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Lever96f87782017-08-03 14:30:03 -0400169 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len);
Chuck Leverc8b920b2016-09-15 10:57:16 -0400170 rep->rr_wc_flags = wc->wc_flags;
171 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
172
Chuck Lever91a10c52017-04-11 13:23:02 -0400173 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
Chuck Lever6b1184c2015-01-21 11:04:25 -0500174 rdmab_addr(rep->rr_rdmabuf),
Chuck Levere2a67192017-08-03 14:30:44 -0400175 wc->byte_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500176
Chuck Leverfc664482014-05-28 10:33:25 -0400177out_schedule:
Chuck Leverd8f532d2017-10-16 15:01:30 -0400178 rpcrdma_reply_handler(rep);
Chuck Lever85024272015-01-21 11:02:04 -0500179 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400180
Chuck Lever85024272015-01-21 11:02:04 -0500181out_fail:
182 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500183 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
184 ib_wc_status_msg(wc->status),
185 wc->status, wc->vendor_err);
Chuck Levere2a67192017-08-03 14:30:44 -0400186 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, 0);
Chuck Lever85024272015-01-21 11:02:04 -0500187 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400188}
189
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400190static void
191rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
192 struct rdma_conn_param *param)
193{
194 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
195 const struct rpcrdma_connect_private *pmsg = param->private_data;
196 unsigned int rsize, wsize;
197
Chuck Leverc8b920b2016-09-15 10:57:16 -0400198 /* Default settings for RPC-over-RDMA Version One */
Chuck Leverb5f0afb2017-02-08 16:59:54 -0500199 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400200 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
201 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
202
203 if (pmsg &&
204 pmsg->cp_magic == rpcrdma_cmp_magic &&
205 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
Chuck Leverc95a3c62017-02-08 17:00:02 -0500206 r_xprt->rx_ia.ri_implicit_roundup = true;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400207 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
208 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
209 }
210
211 if (rsize < cdata->inline_rsize)
212 cdata->inline_rsize = rsize;
213 if (wsize < cdata->inline_wsize)
214 cdata->inline_wsize = wsize;
Chuck Lever6d6bf722016-11-29 10:53:13 -0500215 dprintk("RPC: %s: max send %u, max recv %u\n",
216 __func__, cdata->inline_wsize, cdata->inline_rsize);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400217 rpcrdma_set_max_header_sizes(r_xprt);
218}
219
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400220static int
221rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
222{
223 struct rpcrdma_xprt *xprt = id->context;
224 struct rpcrdma_ia *ia = &xprt->rx_ia;
225 struct rpcrdma_ep *ep = &xprt->rx_ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400226 int connstate = 0;
227
Chuck Leverb4744e02017-12-20 16:31:29 -0500228 trace_xprtrdma_conn_upcall(xprt, event);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400229 switch (event->event) {
230 case RDMA_CM_EVENT_ADDR_RESOLVED:
231 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400232 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400233 complete(&ia->ri_done);
234 break;
235 case RDMA_CM_EVENT_ADDR_ERROR:
Chuck Lever52d28fe2018-05-04 15:34:37 -0400236 ia->ri_async_rc = -EPROTO;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400237 complete(&ia->ri_done);
238 break;
239 case RDMA_CM_EVENT_ROUTE_ERROR:
240 ia->ri_async_rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400241 complete(&ia->ri_done);
242 break;
Chuck Leverbebd0312017-04-11 13:23:10 -0400243 case RDMA_CM_EVENT_DEVICE_REMOVAL:
244#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Leverd461f1f2017-12-14 20:56:50 -0500245 pr_info("rpcrdma: removing device %s for %s:%s\n",
Chuck Lever173b8f42017-06-08 11:53:00 -0400246 ia->ri_device->name,
Chuck Leverd461f1f2017-12-14 20:56:50 -0500247 rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt));
Chuck Leverbebd0312017-04-11 13:23:10 -0400248#endif
249 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
250 ep->rep_connected = -ENODEV;
251 xprt_force_disconnect(&xprt->rx_xprt);
252 wait_for_completion(&ia->ri_remove_done);
253
254 ia->ri_id = NULL;
Chuck Leverbebd0312017-04-11 13:23:10 -0400255 ia->ri_device = NULL;
256 /* Return 1 to ensure the core destroys the id. */
257 return 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400258 case RDMA_CM_EVENT_ESTABLISHED:
Chuck Lever8a147932018-02-28 15:30:38 -0500259 ++xprt->rx_xprt.connect_cookie;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400260 connstate = 1;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400261 rpcrdma_update_connect_private(xprt, &event->param.conn);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400262 goto connected;
263 case RDMA_CM_EVENT_CONNECT_ERROR:
264 connstate = -ENOTCONN;
265 goto connected;
266 case RDMA_CM_EVENT_UNREACHABLE:
Chuck Lever52d28fe2018-05-04 15:34:37 -0400267 connstate = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400268 goto connected;
269 case RDMA_CM_EVENT_REJECTED:
Chuck Leverd461f1f2017-12-14 20:56:50 -0500270 dprintk("rpcrdma: connection to %s:%s rejected: %s\n",
271 rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt),
Chuck Lever0a904872017-02-08 17:00:35 -0500272 rdma_reject_msg(id, event->status));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400273 connstate = -ECONNREFUSED;
Chuck Lever0a904872017-02-08 17:00:35 -0500274 if (event->status == IB_CM_REJ_STALE_CONN)
275 connstate = -EAGAIN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400276 goto connected;
277 case RDMA_CM_EVENT_DISCONNECTED:
Chuck Lever8a147932018-02-28 15:30:38 -0500278 ++xprt->rx_xprt.connect_cookie;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400279 connstate = -ECONNABORTED;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400280connected:
Chuck Leverbe798f92017-10-16 15:01:39 -0400281 xprt->rx_buf.rb_credits = 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400282 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500283 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400284 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400285 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400286 default:
Chuck Leverd461f1f2017-12-14 20:56:50 -0500287 dprintk("RPC: %s: %s:%s on %s/%s (ep 0x%p): %s\n",
288 __func__,
289 rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt),
Chuck Lever173b8f42017-06-08 11:53:00 -0400290 ia->ri_device->name, ia->ri_ops->ro_displayname,
291 ep, rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400292 break;
293 }
294
295 return 0;
296}
297
298static struct rdma_cm_id *
Chuck Leverdd229ce2017-12-14 20:56:58 -0500299rpcrdma_create_id(struct rpcrdma_xprt *xprt, struct rpcrdma_ia *ia)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400300{
Chuck Lever109b88a2016-11-29 10:52:40 -0500301 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400302 struct rdma_cm_id *id;
303 int rc;
304
Chuck Leverb4744e02017-12-20 16:31:29 -0500305 trace_xprtrdma_conn_start(xprt);
306
Tom Talpey1a954052008-10-09 15:01:31 -0400307 init_completion(&ia->ri_done);
Chuck Leverbebd0312017-04-11 13:23:10 -0400308 init_completion(&ia->ri_remove_done);
Tom Talpey1a954052008-10-09 15:01:31 -0400309
Chuck Lever107c4be2018-05-04 15:34:42 -0400310 id = rdma_create_id(xprt->rx_xprt.xprt_net, rpcrdma_conn_upcall,
311 xprt, RDMA_PS_TCP, IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400312 if (IS_ERR(id)) {
313 rc = PTR_ERR(id);
314 dprintk("RPC: %s: rdma_create_id() failed %i\n",
315 __func__, rc);
316 return id;
317 }
318
Tom Talpey5675add2008-10-09 15:01:41 -0400319 ia->ri_async_rc = -ETIMEDOUT;
Chuck Leverdd229ce2017-12-14 20:56:58 -0500320 rc = rdma_resolve_addr(id, NULL,
321 (struct sockaddr *)&xprt->rx_xprt.addr,
322 RDMA_RESOLVE_TIMEOUT);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400323 if (rc) {
324 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
325 __func__, rc);
326 goto out;
327 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500328 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
329 if (rc < 0) {
Chuck Leverb4744e02017-12-20 16:31:29 -0500330 trace_xprtrdma_conn_tout(xprt);
Chuck Lever109b88a2016-11-29 10:52:40 -0500331 goto out;
332 }
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400333
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400334 rc = ia->ri_async_rc;
335 if (rc)
336 goto out;
337
Tom Talpey5675add2008-10-09 15:01:41 -0400338 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400339 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
340 if (rc) {
341 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
342 __func__, rc);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400343 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400344 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500345 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
346 if (rc < 0) {
Chuck Leverb4744e02017-12-20 16:31:29 -0500347 trace_xprtrdma_conn_tout(xprt);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400348 goto out;
Chuck Lever109b88a2016-11-29 10:52:40 -0500349 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400350 rc = ia->ri_async_rc;
351 if (rc)
Chuck Lever56a6bd12017-04-11 13:23:34 -0400352 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400353
354 return id;
Chuck Lever56a6bd12017-04-11 13:23:34 -0400355
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400356out:
357 rdma_destroy_id(id);
358 return ERR_PTR(rc);
359}
360
361/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400362 * Exported functions.
363 */
364
Chuck Leverfff09592017-04-11 13:22:54 -0400365/**
366 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
Chuck Leverdd229ce2017-12-14 20:56:58 -0500367 * @xprt: transport with IA to (re)initialize
Chuck Leverfff09592017-04-11 13:22:54 -0400368 *
369 * Returns 0 on success, negative errno if an appropriate
370 * Interface Adapter could not be found and opened.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400371 */
372int
Chuck Leverdd229ce2017-12-14 20:56:58 -0500373rpcrdma_ia_open(struct rpcrdma_xprt *xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400374{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400375 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400376 int rc;
377
Chuck Leverdd229ce2017-12-14 20:56:58 -0500378 ia->ri_id = rpcrdma_create_id(xprt, ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400379 if (IS_ERR(ia->ri_id)) {
380 rc = PTR_ERR(ia->ri_id);
Chuck Leverfff09592017-04-11 13:22:54 -0400381 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400382 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400383 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400384
Christoph Hellwiged082d32016-09-05 12:56:17 +0200385 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400386 if (IS_ERR(ia->ri_pd)) {
387 rc = PTR_ERR(ia->ri_pd);
Chuck Leverb54054c2016-06-29 13:53:27 -0400388 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
Chuck Leverfff09592017-04-11 13:22:54 -0400389 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400390 }
391
Chuck Leverfff09592017-04-11 13:22:54 -0400392 switch (xprt_rdma_memreg_strategy) {
Chuck Leverce5b3712017-12-14 20:57:47 -0500393 case RPCRDMA_FRWR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400394 if (frwr_is_supported(ia)) {
395 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
396 break;
397 }
398 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400399 case RPCRDMA_MTHCAFMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400400 if (fmr_is_supported(ia)) {
401 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
402 break;
403 }
404 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400405 default:
Chuck Leverfff09592017-04-11 13:22:54 -0400406 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
407 ia->ri_device->name, xprt_rdma_memreg_strategy);
Chuck Leverb54054c2016-06-29 13:53:27 -0400408 rc = -EINVAL;
Chuck Leverfff09592017-04-11 13:22:54 -0400409 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400410 }
411
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400412 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500413
Chuck Leverfff09592017-04-11 13:22:54 -0400414out_err:
415 rpcrdma_ia_close(ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400416 return rc;
417}
418
Chuck Leverfff09592017-04-11 13:22:54 -0400419/**
Chuck Leverbebd0312017-04-11 13:23:10 -0400420 * rpcrdma_ia_remove - Handle device driver unload
421 * @ia: interface adapter being removed
422 *
423 * Divest transport H/W resources associated with this adapter,
424 * but allow it to be restored later.
425 */
426void
427rpcrdma_ia_remove(struct rpcrdma_ia *ia)
428{
429 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
430 rx_ia);
431 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
432 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
433 struct rpcrdma_req *req;
434 struct rpcrdma_rep *rep;
435
436 cancel_delayed_work_sync(&buf->rb_refresh_worker);
437
438 /* This is similar to rpcrdma_ep_destroy, but:
439 * - Don't cancel the connect worker.
440 * - Don't call rpcrdma_ep_disconnect, which waits
441 * for another conn upcall, which will deadlock.
442 * - rdma_disconnect is unneeded, the underlying
443 * connection is already gone.
444 */
445 if (ia->ri_id->qp) {
446 ib_drain_qp(ia->ri_id->qp);
447 rdma_destroy_qp(ia->ri_id);
448 ia->ri_id->qp = NULL;
449 }
450 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever25524282018-03-19 14:23:16 -0400451 ep->rep_attr.recv_cq = NULL;
Chuck Leverbebd0312017-04-11 13:23:10 -0400452 ib_free_cq(ep->rep_attr.send_cq);
Chuck Lever25524282018-03-19 14:23:16 -0400453 ep->rep_attr.send_cq = NULL;
Chuck Leverbebd0312017-04-11 13:23:10 -0400454
455 /* The ULP is responsible for ensuring all DMA
456 * mappings and MRs are gone.
457 */
458 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
459 rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
460 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
461 rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
462 rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
463 rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
464 }
Chuck Lever96cedde2017-12-14 20:57:55 -0500465 rpcrdma_mrs_destroy(buf);
Chuck Lever25524282018-03-19 14:23:16 -0400466 ib_dealloc_pd(ia->ri_pd);
467 ia->ri_pd = NULL;
Chuck Leverbebd0312017-04-11 13:23:10 -0400468
469 /* Allow waiters to continue */
470 complete(&ia->ri_remove_done);
Chuck Leverb4744e02017-12-20 16:31:29 -0500471
472 trace_xprtrdma_remove(r_xprt);
Chuck Leverbebd0312017-04-11 13:23:10 -0400473}
474
475/**
Chuck Leverfff09592017-04-11 13:22:54 -0400476 * rpcrdma_ia_close - Clean up/close an IA.
477 * @ia: interface adapter to close
478 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400479 */
480void
481rpcrdma_ia_close(struct rpcrdma_ia *ia)
482{
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400483 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
484 if (ia->ri_id->qp)
485 rdma_destroy_qp(ia->ri_id);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400486 rdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400487 }
Chuck Leverfff09592017-04-11 13:22:54 -0400488 ia->ri_id = NULL;
489 ia->ri_device = NULL;
Chuck Lever6d446982015-05-26 11:51:27 -0400490
491 /* If the pd is still busy, xprtrdma missed freeing a resource */
492 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600493 ib_dealloc_pd(ia->ri_pd);
Chuck Leverfff09592017-04-11 13:22:54 -0400494 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400495}
496
497/*
498 * Create unconnected endpoint.
499 */
500int
501rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
Chuck Lever16f906d2017-02-08 17:00:10 -0500502 struct rpcrdma_create_data_internal *cdata)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400503{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400504 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
Chuck Leverfc664482014-05-28 10:33:25 -0400505 struct ib_cq *sendcq, *recvcq;
Chuck Lever914fcad2018-05-04 15:34:48 -0400506 unsigned int max_sge;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500507 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400508
Chuck Levereed50872017-03-11 15:52:47 -0500509 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
510 RPCRDMA_MAX_SEND_SGES);
Chuck Lever16f906d2017-02-08 17:00:10 -0500511 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
512 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
Chuck Leverb3221d62015-08-03 13:03:39 -0400513 return -ENOMEM;
514 }
Chuck Lever1179e2c2018-01-31 12:34:05 -0500515 ia->ri_max_send_sges = max_sge;
Chuck Leverb3221d62015-08-03 13:03:39 -0400516
Chuck Lever914fcad2018-05-04 15:34:48 -0400517 rc = ia->ri_ops->ro_open(ia, ep, cdata);
518 if (rc)
519 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400520
521 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
522 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400523 ep->rep_attr.srq = NULL;
Chuck Lever16f906d2017-02-08 17:00:10 -0500524 ep->rep_attr.cap.max_send_sge = max_sge;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400525 ep->rep_attr.cap.max_recv_sge = 1;
526 ep->rep_attr.cap.max_inline_data = 0;
527 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
528 ep->rep_attr.qp_type = IB_QPT_RC;
529 ep->rep_attr.port_num = ~0;
530
531 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
532 "iovs: send %d recv %d\n",
533 __func__,
534 ep->rep_attr.cap.max_send_wr,
535 ep->rep_attr.cap.max_recv_wr,
536 ep->rep_attr.cap.max_send_sge,
537 ep->rep_attr.cap.max_recv_sge);
538
539 /* set trigger for requesting send completion */
Chuck Leverae729502017-10-20 10:48:12 -0400540 ep->rep_send_batch = min_t(unsigned int, RPCRDMA_MAX_SEND_BATCH,
541 cdata->max_requests >> 2);
542 ep->rep_send_count = ep->rep_send_batch;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400543 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400544 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400545
Chuck Lever2fa8f882016-03-04 11:28:53 -0500546 sendcq = ib_alloc_cq(ia->ri_device, NULL,
547 ep->rep_attr.cap.max_send_wr + 1,
Chuck Levera4699f52017-10-30 16:21:49 -0400548 1, IB_POLL_WORKQUEUE);
Chuck Leverfc664482014-05-28 10:33:25 -0400549 if (IS_ERR(sendcq)) {
550 rc = PTR_ERR(sendcq);
551 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400552 __func__, rc);
553 goto out1;
554 }
555
Chuck Lever552bf222016-03-04 11:28:36 -0500556 recvcq = ib_alloc_cq(ia->ri_device, NULL,
557 ep->rep_attr.cap.max_recv_wr + 1,
Chuck Leverd8f532d2017-10-16 15:01:30 -0400558 0, IB_POLL_WORKQUEUE);
Chuck Leverfc664482014-05-28 10:33:25 -0400559 if (IS_ERR(recvcq)) {
560 rc = PTR_ERR(recvcq);
561 dprintk("RPC: %s: failed to create recv CQ: %i\n",
562 __func__, rc);
563 goto out2;
564 }
565
Chuck Leverfc664482014-05-28 10:33:25 -0400566 ep->rep_attr.send_cq = sendcq;
567 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400568
569 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400570 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400571
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400572 /* Prepare RDMA-CM private message */
573 pmsg->cp_magic = rpcrdma_cmp_magic;
574 pmsg->cp_version = RPCRDMA_CMP_VERSION;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400575 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400576 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
577 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
578 ep->rep_remote_cma.private_data = pmsg;
579 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400580
581 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400582 ep->rep_remote_cma.initiator_depth = 0;
Chuck Leverb7e85fff2018-02-28 15:30:33 -0500583 ep->rep_remote_cma.responder_resources =
584 min_t(int, U8_MAX, ia->ri_device->attrs.max_qp_rd_atom);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400585
Chuck Leverb2dde942016-05-02 14:43:03 -0400586 /* Limit transport retries so client can detect server
587 * GID changes quickly. RPC layer handles re-establishing
588 * transport connection and retransmission.
589 */
590 ep->rep_remote_cma.retry_count = 6;
591
592 /* RPC-over-RDMA handles its own flow control. In addition,
593 * make all RNR NAKs visible so we know that RPC-over-RDMA
594 * flow control is working correctly (no NAKs should be seen).
595 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400596 ep->rep_remote_cma.flow_control = 0;
597 ep->rep_remote_cma.rnr_retry_count = 0;
598
599 return 0;
600
601out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500602 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400603out1:
604 return rc;
605}
606
607/*
608 * rpcrdma_ep_destroy
609 *
610 * Disconnect and destroy endpoint. After this, the only
611 * valid operations on the ep are to free it (if dynamically
612 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400613 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400614void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400615rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
616{
Chuck Lever254f91e2014-05-28 10:32:17 -0400617 cancel_delayed_work_sync(&ep->rep_connect_worker);
618
Chuck Lever25524282018-03-19 14:23:16 -0400619 if (ia->ri_id && ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400620 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400621 rdma_destroy_qp(ia->ri_id);
622 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400623 }
624
Chuck Lever25524282018-03-19 14:23:16 -0400625 if (ep->rep_attr.recv_cq)
626 ib_free_cq(ep->rep_attr.recv_cq);
627 if (ep->rep_attr.send_cq)
628 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400629}
630
Chuck Levera9b0e382017-04-11 13:23:26 -0400631/* Re-establish a connection after a device removal event.
632 * Unlike a normal reconnection, a fresh PD and a new set
633 * of MRs and buffers is needed.
634 */
635static int
636rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
637 struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
638{
Chuck Levera9b0e382017-04-11 13:23:26 -0400639 int rc, err;
640
Chuck Leverb4744e02017-12-20 16:31:29 -0500641 trace_xprtrdma_reinsert(r_xprt);
Chuck Levera9b0e382017-04-11 13:23:26 -0400642
643 rc = -EHOSTUNREACH;
Chuck Leverdd229ce2017-12-14 20:56:58 -0500644 if (rpcrdma_ia_open(r_xprt))
Chuck Levera9b0e382017-04-11 13:23:26 -0400645 goto out1;
646
647 rc = -ENOMEM;
648 err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
649 if (err) {
650 pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
651 goto out2;
652 }
653
654 rc = -ENETUNREACH;
655 err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
656 if (err) {
657 pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
658 goto out3;
659 }
660
Chuck Lever96cedde2017-12-14 20:57:55 -0500661 rpcrdma_mrs_create(r_xprt);
Chuck Levera9b0e382017-04-11 13:23:26 -0400662 return 0;
663
664out3:
665 rpcrdma_ep_destroy(ep, ia);
666out2:
667 rpcrdma_ia_close(ia);
668out1:
669 return rc;
670}
671
Chuck Lever18908962017-04-11 13:23:18 -0400672static int
673rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
674 struct rpcrdma_ia *ia)
675{
Chuck Lever18908962017-04-11 13:23:18 -0400676 struct rdma_cm_id *id, *old;
677 int err, rc;
678
Chuck Leverb4744e02017-12-20 16:31:29 -0500679 trace_xprtrdma_reconnect(r_xprt);
Chuck Lever18908962017-04-11 13:23:18 -0400680
681 rpcrdma_ep_disconnect(ep, ia);
682
683 rc = -EHOSTUNREACH;
Chuck Leverdd229ce2017-12-14 20:56:58 -0500684 id = rpcrdma_create_id(r_xprt, ia);
Chuck Lever18908962017-04-11 13:23:18 -0400685 if (IS_ERR(id))
686 goto out;
687
688 /* As long as the new ID points to the same device as the
689 * old ID, we can reuse the transport's existing PD and all
690 * previously allocated MRs. Also, the same device means
691 * the transport's previous DMA mappings are still valid.
692 *
693 * This is a sanity check only. There should be no way these
694 * point to two different devices here.
695 */
696 old = id;
697 rc = -ENETUNREACH;
698 if (ia->ri_device != id->device) {
699 pr_err("rpcrdma: can't reconnect on different device!\n");
700 goto out_destroy;
701 }
702
703 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
704 if (err) {
705 dprintk("RPC: %s: rdma_create_qp returned %d\n",
706 __func__, err);
707 goto out_destroy;
708 }
709
710 /* Atomically replace the transport's ID and QP. */
711 rc = 0;
712 old = ia->ri_id;
713 ia->ri_id = id;
714 rdma_destroy_qp(old);
715
716out_destroy:
Chuck Lever56a6bd12017-04-11 13:23:34 -0400717 rdma_destroy_id(old);
Chuck Lever18908962017-04-11 13:23:18 -0400718out:
719 return rc;
720}
721
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400722/*
723 * Connect unconnected endpoint.
724 */
725int
726rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
727{
Chuck Lever0a904872017-02-08 17:00:35 -0500728 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
729 rx_ia);
Chuck Lever18908962017-04-11 13:23:18 -0400730 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400731
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400732retry:
Chuck Lever18908962017-04-11 13:23:18 -0400733 switch (ep->rep_connected) {
734 case 0:
Chuck Leverec62f402014-05-28 10:34:07 -0400735 dprintk("RPC: %s: connecting...\n", __func__);
736 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
737 if (rc) {
738 dprintk("RPC: %s: rdma_create_qp failed %i\n",
739 __func__, rc);
Chuck Lever18908962017-04-11 13:23:18 -0400740 rc = -ENETUNREACH;
741 goto out_noupdate;
Chuck Leverec62f402014-05-28 10:34:07 -0400742 }
Chuck Lever18908962017-04-11 13:23:18 -0400743 break;
Chuck Levera9b0e382017-04-11 13:23:26 -0400744 case -ENODEV:
745 rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
746 if (rc)
747 goto out_noupdate;
748 break;
Chuck Lever18908962017-04-11 13:23:18 -0400749 default:
750 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
751 if (rc)
752 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400753 }
754
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400755 ep->rep_connected = 0;
756
757 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
758 if (rc) {
759 dprintk("RPC: %s: rdma_connect() failed with %i\n",
760 __func__, rc);
761 goto out;
762 }
763
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400764 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400765 if (ep->rep_connected <= 0) {
Chuck Lever0a904872017-02-08 17:00:35 -0500766 if (ep->rep_connected == -EAGAIN)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400767 goto retry;
768 rc = ep->rep_connected;
Chuck Lever0a904872017-02-08 17:00:35 -0500769 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400770 }
771
Chuck Lever0a904872017-02-08 17:00:35 -0500772 dprintk("RPC: %s: connected\n", __func__);
Chuck Lever7c8d9e72018-05-04 15:35:20 -0400773
774 rpcrdma_post_recvs(r_xprt, true);
Chuck Lever0a904872017-02-08 17:00:35 -0500775
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400776out:
777 if (rc)
778 ep->rep_connected = rc;
Chuck Lever18908962017-04-11 13:23:18 -0400779
780out_noupdate:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400781 return rc;
782}
783
784/*
785 * rpcrdma_ep_disconnect
786 *
787 * This is separate from destroy to facilitate the ability
788 * to reconnect without recreating the endpoint.
789 *
790 * This call is not reentrant, and must not be made in parallel
791 * on the same endpoint.
792 */
Chuck Lever282191c2014-07-29 17:25:55 -0400793void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400794rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
795{
796 int rc;
797
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400798 rc = rdma_disconnect(ia->ri_id);
Chuck Leverb4744e02017-12-20 16:31:29 -0500799 if (!rc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400800 /* returns without wait if not connected */
801 wait_event_interruptible(ep->rep_connect_wait,
802 ep->rep_connected != 1);
Chuck Leverb4744e02017-12-20 16:31:29 -0500803 else
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400804 ep->rep_connected = rc;
Chuck Leverb4744e02017-12-20 16:31:29 -0500805 trace_xprtrdma_disconnect(container_of(ep, struct rpcrdma_xprt,
806 rx_ep), rc);
Chuck Lever550d7502016-05-02 14:41:47 -0400807
808 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400809}
810
Chuck Leverae729502017-10-20 10:48:12 -0400811/* Fixed-size circular FIFO queue. This implementation is wait-free and
812 * lock-free.
813 *
814 * Consumer is the code path that posts Sends. This path dequeues a
815 * sendctx for use by a Send operation. Multiple consumer threads
816 * are serialized by the RPC transport lock, which allows only one
817 * ->send_request call at a time.
818 *
819 * Producer is the code path that handles Send completions. This path
820 * enqueues a sendctx that has been completed. Multiple producer
821 * threads are serialized by the ib_poll_cq() function.
822 */
823
824/* rpcrdma_sendctxs_destroy() assumes caller has already quiesced
825 * queue activity, and ib_drain_qp has flushed all remaining Send
826 * requests.
827 */
828static void rpcrdma_sendctxs_destroy(struct rpcrdma_buffer *buf)
829{
830 unsigned long i;
831
832 for (i = 0; i <= buf->rb_sc_last; i++)
833 kfree(buf->rb_sc_ctxs[i]);
834 kfree(buf->rb_sc_ctxs);
835}
836
837static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ia *ia)
838{
839 struct rpcrdma_sendctx *sc;
840
841 sc = kzalloc(sizeof(*sc) +
842 ia->ri_max_send_sges * sizeof(struct ib_sge),
843 GFP_KERNEL);
844 if (!sc)
845 return NULL;
846
847 sc->sc_wr.wr_cqe = &sc->sc_cqe;
848 sc->sc_wr.sg_list = sc->sc_sges;
849 sc->sc_wr.opcode = IB_WR_SEND;
850 sc->sc_cqe.done = rpcrdma_wc_send;
851 return sc;
852}
853
854static int rpcrdma_sendctxs_create(struct rpcrdma_xprt *r_xprt)
855{
856 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
857 struct rpcrdma_sendctx *sc;
858 unsigned long i;
859
860 /* Maximum number of concurrent outstanding Send WRs. Capping
861 * the circular queue size stops Send Queue overflow by causing
862 * the ->send_request call to fail temporarily before too many
863 * Sends are posted.
864 */
865 i = buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS;
866 dprintk("RPC: %s: allocating %lu send_ctxs\n", __func__, i);
867 buf->rb_sc_ctxs = kcalloc(i, sizeof(sc), GFP_KERNEL);
868 if (!buf->rb_sc_ctxs)
869 return -ENOMEM;
870
871 buf->rb_sc_last = i - 1;
872 for (i = 0; i <= buf->rb_sc_last; i++) {
873 sc = rpcrdma_sendctx_create(&r_xprt->rx_ia);
874 if (!sc)
875 goto out_destroy;
876
877 sc->sc_xprt = r_xprt;
878 buf->rb_sc_ctxs[i] = sc;
879 }
880
881 return 0;
882
883out_destroy:
884 rpcrdma_sendctxs_destroy(buf);
885 return -ENOMEM;
886}
887
888/* The sendctx queue is not guaranteed to have a size that is a
889 * power of two, thus the helpers in circ_buf.h cannot be used.
890 * The other option is to use modulus (%), which can be expensive.
891 */
892static unsigned long rpcrdma_sendctx_next(struct rpcrdma_buffer *buf,
893 unsigned long item)
894{
895 return likely(item < buf->rb_sc_last) ? item + 1 : 0;
896}
897
898/**
899 * rpcrdma_sendctx_get_locked - Acquire a send context
900 * @buf: transport buffers from which to acquire an unused context
901 *
902 * Returns pointer to a free send completion context; or NULL if
903 * the queue is empty.
904 *
905 * Usage: Called to acquire an SGE array before preparing a Send WR.
906 *
907 * The caller serializes calls to this function (per rpcrdma_buffer),
908 * and provides an effective memory barrier that flushes the new value
909 * of rb_sc_head.
910 */
911struct rpcrdma_sendctx *rpcrdma_sendctx_get_locked(struct rpcrdma_buffer *buf)
912{
913 struct rpcrdma_xprt *r_xprt;
914 struct rpcrdma_sendctx *sc;
915 unsigned long next_head;
916
917 next_head = rpcrdma_sendctx_next(buf, buf->rb_sc_head);
918
919 if (next_head == READ_ONCE(buf->rb_sc_tail))
920 goto out_emptyq;
921
922 /* ORDER: item must be accessed _before_ head is updated */
923 sc = buf->rb_sc_ctxs[next_head];
924
925 /* Releasing the lock in the caller acts as a memory
926 * barrier that flushes rb_sc_head.
927 */
928 buf->rb_sc_head = next_head;
929
930 return sc;
931
932out_emptyq:
933 /* The queue is "empty" if there have not been enough Send
934 * completions recently. This is a sign the Send Queue is
935 * backing up. Cause the caller to pause and try again.
936 */
937 dprintk("RPC: %s: empty sendctx queue\n", __func__);
938 r_xprt = container_of(buf, struct rpcrdma_xprt, rx_buf);
939 r_xprt->rx_stats.empty_sendctx_q++;
940 return NULL;
941}
942
943/**
944 * rpcrdma_sendctx_put_locked - Release a send context
945 * @sc: send context to release
946 *
947 * Usage: Called from Send completion to return a sendctxt
948 * to the queue.
949 *
950 * The caller serializes calls to this function (per rpcrdma_buffer).
951 */
952void rpcrdma_sendctx_put_locked(struct rpcrdma_sendctx *sc)
953{
954 struct rpcrdma_buffer *buf = &sc->sc_xprt->rx_buf;
955 unsigned long next_tail;
956
957 /* Unmap SGEs of previously completed by unsignaled
958 * Sends by walking up the queue until @sc is found.
959 */
960 next_tail = buf->rb_sc_tail;
961 do {
962 next_tail = rpcrdma_sendctx_next(buf, next_tail);
963
964 /* ORDER: item must be accessed _before_ tail is updated */
965 rpcrdma_unmap_sendctx(buf->rb_sc_ctxs[next_tail]);
966
967 } while (buf->rb_sc_ctxs[next_tail] != sc);
968
969 /* Paired with READ_ONCE */
970 smp_store_release(&buf->rb_sc_tail, next_tail);
971}
972
Chuck Lever505bbe62016-06-29 13:52:54 -0400973static void
974rpcrdma_mr_recovery_worker(struct work_struct *work)
975{
976 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
977 rb_recovery_worker.work);
Chuck Lever96cedde2017-12-14 20:57:55 -0500978 struct rpcrdma_mr *mr;
Chuck Lever505bbe62016-06-29 13:52:54 -0400979
980 spin_lock(&buf->rb_recovery_lock);
981 while (!list_empty(&buf->rb_stale_mrs)) {
Chuck Lever96cedde2017-12-14 20:57:55 -0500982 mr = rpcrdma_mr_pop(&buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400983 spin_unlock(&buf->rb_recovery_lock);
984
Chuck Lever1c443eff2017-12-20 16:31:21 -0500985 trace_xprtrdma_recover_mr(mr);
Chuck Lever96cedde2017-12-14 20:57:55 -0500986 mr->mr_xprt->rx_ia.ri_ops->ro_recover_mr(mr);
Chuck Lever505bbe62016-06-29 13:52:54 -0400987
988 spin_lock(&buf->rb_recovery_lock);
kbuild test robot53d78522016-07-16 06:02:05 +0800989 }
Chuck Lever505bbe62016-06-29 13:52:54 -0400990 spin_unlock(&buf->rb_recovery_lock);
991}
992
993void
Chuck Lever96cedde2017-12-14 20:57:55 -0500994rpcrdma_mr_defer_recovery(struct rpcrdma_mr *mr)
Chuck Lever505bbe62016-06-29 13:52:54 -0400995{
Chuck Lever96cedde2017-12-14 20:57:55 -0500996 struct rpcrdma_xprt *r_xprt = mr->mr_xprt;
Chuck Lever505bbe62016-06-29 13:52:54 -0400997 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
998
999 spin_lock(&buf->rb_recovery_lock);
Chuck Lever96cedde2017-12-14 20:57:55 -05001000 rpcrdma_mr_push(mr, &buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -04001001 spin_unlock(&buf->rb_recovery_lock);
1002
1003 schedule_delayed_work(&buf->rb_recovery_worker, 0);
1004}
1005
Chuck Levere2ac2362016-06-29 13:54:00 -04001006static void
Chuck Lever96cedde2017-12-14 20:57:55 -05001007rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt)
Chuck Levere2ac2362016-06-29 13:54:00 -04001008{
1009 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1010 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1011 unsigned int count;
1012 LIST_HEAD(free);
1013 LIST_HEAD(all);
1014
Chuck Leverae741a82018-02-28 15:30:49 -05001015 for (count = 0; count < 3; count++) {
Chuck Lever96cedde2017-12-14 20:57:55 -05001016 struct rpcrdma_mr *mr;
Chuck Levere2ac2362016-06-29 13:54:00 -04001017 int rc;
1018
Chuck Lever96cedde2017-12-14 20:57:55 -05001019 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1020 if (!mr)
Chuck Levere2ac2362016-06-29 13:54:00 -04001021 break;
1022
Chuck Lever96cedde2017-12-14 20:57:55 -05001023 rc = ia->ri_ops->ro_init_mr(ia, mr);
Chuck Levere2ac2362016-06-29 13:54:00 -04001024 if (rc) {
Chuck Lever96cedde2017-12-14 20:57:55 -05001025 kfree(mr);
Chuck Levere2ac2362016-06-29 13:54:00 -04001026 break;
1027 }
1028
Chuck Lever96cedde2017-12-14 20:57:55 -05001029 mr->mr_xprt = r_xprt;
Chuck Levere2ac2362016-06-29 13:54:00 -04001030
Chuck Lever96cedde2017-12-14 20:57:55 -05001031 list_add(&mr->mr_list, &free);
1032 list_add(&mr->mr_all, &all);
Chuck Levere2ac2362016-06-29 13:54:00 -04001033 }
1034
Chuck Lever96cedde2017-12-14 20:57:55 -05001035 spin_lock(&buf->rb_mrlock);
1036 list_splice(&free, &buf->rb_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001037 list_splice(&all, &buf->rb_all);
1038 r_xprt->rx_stats.mrs_allocated += count;
Chuck Lever96cedde2017-12-14 20:57:55 -05001039 spin_unlock(&buf->rb_mrlock);
Chuck Lever1c443eff2017-12-20 16:31:21 -05001040 trace_xprtrdma_createmrs(r_xprt, count);
Chuck Lever9e679d52018-02-28 15:30:44 -05001041
1042 xprt_write_space(&r_xprt->rx_xprt);
Chuck Levere2ac2362016-06-29 13:54:00 -04001043}
1044
1045static void
1046rpcrdma_mr_refresh_worker(struct work_struct *work)
1047{
1048 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
1049 rb_refresh_worker.work);
1050 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1051 rx_buf);
1052
Chuck Lever96cedde2017-12-14 20:57:55 -05001053 rpcrdma_mrs_create(r_xprt);
Chuck Levere2ac2362016-06-29 13:54:00 -04001054}
1055
Chuck Leverf531a5d2015-10-24 17:27:43 -04001056struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -05001057rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
1058{
Chuck Leverf531a5d2015-10-24 17:27:43 -04001059 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever2dd4a012018-02-28 15:31:05 -05001060 struct rpcrdma_regbuf *rb;
Chuck Lever13924022015-01-21 11:03:52 -05001061 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -05001062
Chuck Lever85275c82015-01-21 11:04:16 -05001063 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -05001064 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -05001065 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -05001066
Chuck Lever2dd4a012018-02-28 15:31:05 -05001067 rb = rpcrdma_alloc_regbuf(RPCRDMA_HDRBUF_SIZE,
1068 DMA_TO_DEVICE, GFP_KERNEL);
1069 if (IS_ERR(rb)) {
1070 kfree(req);
1071 return ERR_PTR(-ENOMEM);
1072 }
1073 req->rl_rdmabuf = rb;
1074 xdr_buf_init(&req->rl_hdrbuf, rb->rg_base, rdmab_length(rb));
1075 req->rl_buffer = buffer;
1076 INIT_LIST_HEAD(&req->rl_registered);
1077
Chuck Leverf531a5d2015-10-24 17:27:43 -04001078 spin_lock(&buffer->rb_reqslock);
1079 list_add(&req->rl_all, &buffer->rb_allreqs);
1080 spin_unlock(&buffer->rb_reqslock);
Chuck Lever13924022015-01-21 11:03:52 -05001081 return req;
Chuck Lever13924022015-01-21 11:03:52 -05001082}
1083
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001084static int
1085rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt, bool temp)
Chuck Lever13924022015-01-21 11:03:52 -05001086{
1087 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Leverd698c4a2017-12-14 20:56:09 -05001088 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -05001089 struct rpcrdma_rep *rep;
1090 int rc;
1091
1092 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001093 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -05001094 if (rep == NULL)
1095 goto out;
Chuck Lever13924022015-01-21 11:03:52 -05001096
Chuck Lever13650c22016-09-15 10:56:26 -04001097 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -04001098 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever6b1184c2015-01-21 11:04:25 -05001099 if (IS_ERR(rep->rr_rdmabuf)) {
1100 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001101 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001102 }
Chuck Lever96f87782017-08-03 14:30:03 -04001103 xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base,
1104 rdmab_length(rep->rr_rdmabuf));
Chuck Lever13924022015-01-21 11:03:52 -05001105
Chuck Lever1519e962016-09-15 10:57:49 -04001106 rep->rr_cqe.done = rpcrdma_wc_receive;
Chuck Leverfed171b2015-05-26 11:51:37 -04001107 rep->rr_rxprt = r_xprt;
Chuck Leverd8f532d2017-10-16 15:01:30 -04001108 INIT_WORK(&rep->rr_work, rpcrdma_deferred_completion);
Chuck Lever6ea8e712016-09-15 10:56:51 -04001109 rep->rr_recv_wr.next = NULL;
1110 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
1111 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
1112 rep->rr_recv_wr.num_sge = 1;
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001113 rep->rr_temp = temp;
Chuck Leverd698c4a2017-12-14 20:56:09 -05001114
1115 spin_lock(&buf->rb_lock);
1116 list_add(&rep->rr_list, &buf->rb_recv_bufs);
1117 spin_unlock(&buf->rb_lock);
1118 return 0;
Chuck Lever13924022015-01-21 11:03:52 -05001119
1120out_free:
1121 kfree(rep);
1122out:
Chuck Leverd698c4a2017-12-14 20:56:09 -05001123 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1124 __func__, rc);
1125 return rc;
Chuck Lever13924022015-01-21 11:03:52 -05001126}
1127
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001128int
Chuck Leverac920d02015-01-21 11:03:44 -05001129rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001130{
Chuck Leverac920d02015-01-21 11:03:44 -05001131 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001132 int i, rc;
1133
Chuck Lever1e465fd2015-10-24 17:27:02 -04001134 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001135 buf->rb_bc_srv_max_requests = 0;
Chuck Lever96cedde2017-12-14 20:57:55 -05001136 spin_lock_init(&buf->rb_mrlock);
Chuck Lever505bbe62016-06-29 13:52:54 -04001137 spin_lock_init(&buf->rb_lock);
1138 spin_lock_init(&buf->rb_recovery_lock);
Chuck Lever96cedde2017-12-14 20:57:55 -05001139 INIT_LIST_HEAD(&buf->rb_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001140 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever505bbe62016-06-29 13:52:54 -04001141 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001142 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1143 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001144 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
1145 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001146
Chuck Lever96cedde2017-12-14 20:57:55 -05001147 rpcrdma_mrs_create(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001148
Chuck Lever1e465fd2015-10-24 17:27:02 -04001149 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001150 INIT_LIST_HEAD(&buf->rb_allreqs);
1151 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001152 for (i = 0; i < buf->rb_max_requests; i++) {
1153 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001154
Chuck Lever13924022015-01-21 11:03:52 -05001155 req = rpcrdma_create_req(r_xprt);
1156 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001157 dprintk("RPC: %s: request buffer %d alloc"
1158 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001159 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001160 goto out;
1161 }
Chuck Levera80d66c2017-06-08 11:52:12 -04001162 list_add(&req->rl_list, &buf->rb_send_bufs);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001163 }
1164
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001165 buf->rb_posted_receives = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001166 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever13924022015-01-21 11:03:52 -05001167
Chuck Leverae729502017-10-20 10:48:12 -04001168 rc = rpcrdma_sendctxs_create(r_xprt);
1169 if (rc)
1170 goto out;
1171
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001172 return 0;
1173out:
1174 rpcrdma_buffer_destroy(buf);
1175 return rc;
1176}
1177
Chuck Lever1e465fd2015-10-24 17:27:02 -04001178static struct rpcrdma_req *
1179rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1180{
1181 struct rpcrdma_req *req;
1182
1183 req = list_first_entry(&buf->rb_send_bufs,
Chuck Levera80d66c2017-06-08 11:52:12 -04001184 struct rpcrdma_req, rl_list);
Chuck Lever431af642017-06-08 11:52:20 -04001185 list_del_init(&req->rl_list);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001186 return req;
1187}
1188
1189static struct rpcrdma_rep *
1190rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1191{
1192 struct rpcrdma_rep *rep;
1193
1194 rep = list_first_entry(&buf->rb_recv_bufs,
1195 struct rpcrdma_rep, rr_list);
1196 list_del(&rep->rr_list);
1197 return rep;
1198}
1199
Chuck Lever2e845222014-07-29 17:25:38 -04001200static void
Chuck Lever13650c22016-09-15 10:56:26 -04001201rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -05001202{
Chuck Lever13650c22016-09-15 10:56:26 -04001203 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001204 kfree(rep);
1205}
1206
Chuck Leverf531a5d2015-10-24 17:27:43 -04001207void
Chuck Lever13650c22016-09-15 10:56:26 -04001208rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -05001209{
Chuck Lever13650c22016-09-15 10:56:26 -04001210 rpcrdma_free_regbuf(req->rl_recvbuf);
1211 rpcrdma_free_regbuf(req->rl_sendbuf);
1212 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001213 kfree(req);
1214}
1215
Chuck Levere2ac2362016-06-29 13:54:00 -04001216static void
Chuck Lever96cedde2017-12-14 20:57:55 -05001217rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf)
Chuck Levere2ac2362016-06-29 13:54:00 -04001218{
1219 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1220 rx_buf);
1221 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
Chuck Lever96cedde2017-12-14 20:57:55 -05001222 struct rpcrdma_mr *mr;
Chuck Levere2ac2362016-06-29 13:54:00 -04001223 unsigned int count;
1224
1225 count = 0;
Chuck Lever96cedde2017-12-14 20:57:55 -05001226 spin_lock(&buf->rb_mrlock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001227 while (!list_empty(&buf->rb_all)) {
Chuck Lever96cedde2017-12-14 20:57:55 -05001228 mr = list_entry(buf->rb_all.next, struct rpcrdma_mr, mr_all);
1229 list_del(&mr->mr_all);
Chuck Levere2ac2362016-06-29 13:54:00 -04001230
Chuck Lever96cedde2017-12-14 20:57:55 -05001231 spin_unlock(&buf->rb_mrlock);
1232 ia->ri_ops->ro_release_mr(mr);
Chuck Levere2ac2362016-06-29 13:54:00 -04001233 count++;
Chuck Lever96cedde2017-12-14 20:57:55 -05001234 spin_lock(&buf->rb_mrlock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001235 }
Chuck Lever96cedde2017-12-14 20:57:55 -05001236 spin_unlock(&buf->rb_mrlock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001237 r_xprt->rx_stats.mrs_allocated = 0;
1238
1239 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1240}
1241
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001242void
1243rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1244{
Chuck Lever505bbe62016-06-29 13:52:54 -04001245 cancel_delayed_work_sync(&buf->rb_recovery_worker);
Chuck Lever9378b272017-04-11 13:22:29 -04001246 cancel_delayed_work_sync(&buf->rb_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001247
Chuck Leverae729502017-10-20 10:48:12 -04001248 rpcrdma_sendctxs_destroy(buf);
1249
Chuck Lever1e465fd2015-10-24 17:27:02 -04001250 while (!list_empty(&buf->rb_recv_bufs)) {
1251 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001252
Chuck Lever1e465fd2015-10-24 17:27:02 -04001253 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001254 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001255 }
1256
Chuck Leverf531a5d2015-10-24 17:27:43 -04001257 spin_lock(&buf->rb_reqslock);
1258 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001259 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001260
Chuck Leverf531a5d2015-10-24 17:27:43 -04001261 req = list_first_entry(&buf->rb_allreqs,
1262 struct rpcrdma_req, rl_all);
1263 list_del(&req->rl_all);
1264
1265 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001266 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001267 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001268 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001269 spin_unlock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001270
Chuck Lever96cedde2017-12-14 20:57:55 -05001271 rpcrdma_mrs_destroy(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001272}
1273
Chuck Lever96cedde2017-12-14 20:57:55 -05001274/**
1275 * rpcrdma_mr_get - Allocate an rpcrdma_mr object
1276 * @r_xprt: controlling transport
1277 *
1278 * Returns an initialized rpcrdma_mr or NULL if no free
1279 * rpcrdma_mr objects are available.
1280 */
1281struct rpcrdma_mr *
1282rpcrdma_mr_get(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001283{
Chuck Lever346aa662015-05-26 11:52:06 -04001284 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Lever96cedde2017-12-14 20:57:55 -05001285 struct rpcrdma_mr *mr = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001286
Chuck Lever96cedde2017-12-14 20:57:55 -05001287 spin_lock(&buf->rb_mrlock);
1288 if (!list_empty(&buf->rb_mrs))
1289 mr = rpcrdma_mr_pop(&buf->rb_mrs);
1290 spin_unlock(&buf->rb_mrlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001291
Chuck Lever96cedde2017-12-14 20:57:55 -05001292 if (!mr)
1293 goto out_nomrs;
1294 return mr;
Chuck Levere2ac2362016-06-29 13:54:00 -04001295
Chuck Lever96cedde2017-12-14 20:57:55 -05001296out_nomrs:
Chuck Lever1c443eff2017-12-20 16:31:21 -05001297 trace_xprtrdma_nomrs(r_xprt);
Chuck Leverbebd0312017-04-11 13:23:10 -04001298 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1299 schedule_delayed_work(&buf->rb_refresh_worker, 0);
Chuck Levere2ac2362016-06-29 13:54:00 -04001300
1301 /* Allow the reply handler and refresh worker to run */
1302 cond_resched();
1303
1304 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001305}
1306
Chuck Leverec12e472017-12-14 20:58:04 -05001307static void
1308__rpcrdma_mr_put(struct rpcrdma_buffer *buf, struct rpcrdma_mr *mr)
1309{
1310 spin_lock(&buf->rb_mrlock);
1311 rpcrdma_mr_push(mr, &buf->rb_mrs);
1312 spin_unlock(&buf->rb_mrlock);
1313}
1314
Chuck Lever96cedde2017-12-14 20:57:55 -05001315/**
1316 * rpcrdma_mr_put - Release an rpcrdma_mr object
1317 * @mr: object to release
1318 *
1319 */
Chuck Lever346aa662015-05-26 11:52:06 -04001320void
Chuck Lever96cedde2017-12-14 20:57:55 -05001321rpcrdma_mr_put(struct rpcrdma_mr *mr)
Chuck Leverc2922c02014-07-29 17:24:36 -04001322{
Chuck Leverec12e472017-12-14 20:58:04 -05001323 __rpcrdma_mr_put(&mr->mr_xprt->rx_buf, mr);
1324}
Chuck Leverc2922c02014-07-29 17:24:36 -04001325
Chuck Leverec12e472017-12-14 20:58:04 -05001326/**
1327 * rpcrdma_mr_unmap_and_put - DMA unmap an MR and release it
1328 * @mr: object to release
1329 *
1330 */
1331void
1332rpcrdma_mr_unmap_and_put(struct rpcrdma_mr *mr)
1333{
1334 struct rpcrdma_xprt *r_xprt = mr->mr_xprt;
1335
Chuck Lever2937fed2017-12-20 16:31:12 -05001336 trace_xprtrdma_dma_unmap(mr);
Chuck Leverec12e472017-12-14 20:58:04 -05001337 ib_dma_unmap_sg(r_xprt->rx_ia.ri_device,
1338 mr->mr_sg, mr->mr_nents, mr->mr_dir);
1339 __rpcrdma_mr_put(&r_xprt->rx_buf, mr);
Chuck Leverc2922c02014-07-29 17:24:36 -04001340}
1341
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001342/**
1343 * rpcrdma_buffer_get - Get a request buffer
1344 * @buffers: Buffer pool from which to obtain a buffer
Chuck Lever78d506e2016-09-06 11:22:49 -04001345 *
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001346 * Returns a fresh rpcrdma_req, or NULL if none are available.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001347 */
1348struct rpcrdma_req *
1349rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1350{
1351 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001352
Chuck Levera5b027e2015-10-24 17:27:27 -04001353 spin_lock(&buffers->rb_lock);
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001354 if (unlikely(list_empty(&buffers->rb_send_bufs)))
1355 goto out_noreqs;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001356 req = rpcrdma_buffer_get_req_locked(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001357 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001358 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001359
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001360out_noreqs:
Chuck Levera5b027e2015-10-24 17:27:27 -04001361 spin_unlock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001362 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001363}
1364
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001365/**
1366 * rpcrdma_buffer_put - Put request/reply buffers back into pool
1367 * @req: object to return
1368 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001369 */
1370void
1371rpcrdma_buffer_put(struct rpcrdma_req *req)
1372{
1373 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001374 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001375
Chuck Lever1e465fd2015-10-24 17:27:02 -04001376 req->rl_reply = NULL;
1377
Chuck Levera5b027e2015-10-24 17:27:27 -04001378 spin_lock(&buffers->rb_lock);
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001379 list_add(&req->rl_list, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001380 if (rep) {
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001381 if (!rep->rr_temp) {
1382 list_add(&rep->rr_list, &buffers->rb_recv_bufs);
1383 rep = NULL;
1384 }
Chuck Lever05c97462016-09-06 11:22:58 -04001385 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001386 spin_unlock(&buffers->rb_lock);
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001387 if (rep)
1388 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001389}
1390
1391/*
1392 * Put reply buffers back into pool when not attached to
1393 * request. This happens in error conditions.
1394 */
1395void
1396rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1397{
Chuck Leverfed171b2015-05-26 11:51:37 -04001398 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001399
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001400 if (!rep->rr_temp) {
1401 spin_lock(&buffers->rb_lock);
1402 list_add(&rep->rr_list, &buffers->rb_recv_bufs);
1403 spin_unlock(&buffers->rb_lock);
1404 } else {
1405 rpcrdma_destroy_rep(rep);
1406 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001407}
1408
Chuck Lever9128c3e2015-01-21 11:04:00 -05001409/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001410 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001411 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001412 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001413 * @flags: GFP flags
1414 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001415 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1416 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001417 *
1418 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001419 * receiving the payload of RDMA RECV operations. During Long Calls
1420 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001421 */
1422struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001423rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1424 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001425{
1426 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001427
Chuck Lever9128c3e2015-01-21 11:04:00 -05001428 rb = kmalloc(sizeof(*rb) + size, flags);
1429 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001430 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001431
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001432 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001433 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001434 rb->rg_iov.length = size;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001435
1436 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001437}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001438
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001439/**
1440 * __rpcrdma_map_regbuf - DMA-map a regbuf
1441 * @ia: controlling rpcrdma_ia
1442 * @rb: regbuf to be mapped
1443 */
1444bool
1445__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1446{
Chuck Lever91a10c52017-04-11 13:23:02 -04001447 struct ib_device *device = ia->ri_device;
1448
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001449 if (rb->rg_direction == DMA_NONE)
1450 return false;
1451
Chuck Lever91a10c52017-04-11 13:23:02 -04001452 rb->rg_iov.addr = ib_dma_map_single(device,
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001453 (void *)rb->rg_base,
1454 rdmab_length(rb),
1455 rb->rg_direction);
Chuck Lever91a10c52017-04-11 13:23:02 -04001456 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001457 return false;
1458
Chuck Lever91a10c52017-04-11 13:23:02 -04001459 rb->rg_device = device;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001460 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1461 return true;
1462}
1463
1464static void
1465rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1466{
Chuck Levere89e8d8f2018-01-31 12:34:13 -05001467 if (!rb)
1468 return;
1469
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001470 if (!rpcrdma_regbuf_is_mapped(rb))
1471 return;
1472
1473 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1474 rdmab_length(rb), rb->rg_direction);
1475 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001476}
1477
1478/**
1479 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001480 * @rb: regbuf to be deregistered and freed
1481 */
1482void
Chuck Lever13650c22016-09-15 10:56:26 -04001483rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001484{
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001485 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001486 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001487}
1488
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001489/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001490 * Prepost any receive buffer, then post send.
1491 *
1492 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1493 */
1494int
1495rpcrdma_ep_post(struct rpcrdma_ia *ia,
1496 struct rpcrdma_ep *ep,
1497 struct rpcrdma_req *req)
1498{
Chuck Leverae729502017-10-20 10:48:12 -04001499 struct ib_send_wr *send_wr = &req->rl_sendctx->sc_wr;
Chuck Lever655fec62016-09-15 10:57:24 -04001500 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001501
Chuck Lever01bb35c2017-10-20 10:48:36 -04001502 if (!ep->rep_send_count ||
1503 test_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags)) {
Chuck Leverae729502017-10-20 10:48:12 -04001504 send_wr->send_flags |= IB_SEND_SIGNALED;
1505 ep->rep_send_count = ep->rep_send_batch;
1506 } else {
1507 send_wr->send_flags &= ~IB_SEND_SIGNALED;
1508 --ep->rep_send_count;
1509 }
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001510
Chuck Leverf2877622018-02-28 15:30:59 -05001511 rc = ia->ri_ops->ro_send(ia, req);
Chuck Leverab03eff2017-12-20 16:30:40 -05001512 trace_xprtrdma_post_send(req, rc);
1513 if (rc)
1514 return -ENOTCONN;
1515 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001516}
1517
Chuck Lever7c8d9e72018-05-04 15:35:20 -04001518/**
1519 * rpcrdma_post_recvs - Maybe post some Receive buffers
1520 * @r_xprt: controlling transport
1521 * @temp: when true, allocate temp rpcrdma_rep objects
1522 *
1523 */
1524void
1525rpcrdma_post_recvs(struct rpcrdma_xprt *r_xprt, bool temp)
1526{
1527 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1528 struct ib_recv_wr *wr, *bad_wr;
1529 int needed, count, rc;
1530
1531 needed = buf->rb_credits + (buf->rb_bc_srv_max_requests << 1);
1532 if (buf->rb_posted_receives > needed)
1533 return;
1534 needed -= buf->rb_posted_receives;
1535
1536 count = 0;
1537 wr = NULL;
1538 while (needed) {
1539 struct rpcrdma_regbuf *rb;
1540 struct rpcrdma_rep *rep;
1541
1542 spin_lock(&buf->rb_lock);
1543 rep = list_first_entry_or_null(&buf->rb_recv_bufs,
1544 struct rpcrdma_rep, rr_list);
1545 if (likely(rep))
1546 list_del(&rep->rr_list);
1547 spin_unlock(&buf->rb_lock);
1548 if (!rep) {
1549 if (rpcrdma_create_rep(r_xprt, temp))
1550 break;
1551 continue;
1552 }
1553
1554 rb = rep->rr_rdmabuf;
1555 if (!rpcrdma_regbuf_is_mapped(rb)) {
1556 if (!__rpcrdma_dma_map_regbuf(&r_xprt->rx_ia, rb)) {
1557 rpcrdma_recv_buffer_put(rep);
1558 break;
1559 }
1560 }
1561
1562 trace_xprtrdma_post_recv(rep->rr_recv_wr.wr_cqe);
1563 rep->rr_recv_wr.next = wr;
1564 wr = &rep->rr_recv_wr;
1565 ++count;
1566 --needed;
1567 }
1568 if (!count)
1569 return;
1570
1571 rc = ib_post_recv(r_xprt->rx_ia.ri_id->qp, wr, &bad_wr);
1572 if (rc) {
1573 for (wr = bad_wr; wr; wr = wr->next) {
1574 struct rpcrdma_rep *rep;
1575
1576 rep = container_of(wr, struct rpcrdma_rep, rr_recv_wr);
1577 rpcrdma_recv_buffer_put(rep);
1578 --count;
1579 }
1580 }
1581 buf->rb_posted_receives += count;
1582 trace_xprtrdma_post_recvs(r_xprt, count, rc);
1583}