blob: 7f9e9025c42f2fbeffb736ffa0e3e34eb9e0c67c [file] [log] [blame]
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -04001/*
Chuck Lever62b56a62017-10-30 16:22:14 -04002 * Copyright (c) 2014-2017 Oracle. All rights reserved.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04003 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the BSD-type
9 * license below:
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials provided
21 * with the distribution.
22 *
23 * Neither the name of the Network Appliance, Inc. nor the names of
24 * its contributors may be used to endorse or promote products
25 * derived from this software without specific prior written
26 * permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040039 */
40
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040041/*
42 * verbs.c
43 *
44 * Encapsulates the major functions managing:
45 * o adapters
46 * o endpoints
47 * o connections
48 * o buffer memory
49 */
50
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000051#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Chuck Lever0dd39ca2015-03-30 14:33:43 -040053#include <linux/sunrpc/addr.h>
Chuck Lever05c97462016-09-06 11:22:58 -040054#include <linux/sunrpc/svc_rdma.h>
Chuck Leverae729502017-10-20 10:48:12 -040055
56#include <asm-generic/barrier.h>
Chuck Lever65866f82014-05-28 10:33:59 -040057#include <asm/bitops.h>
Chuck Lever56a6bd12017-04-11 13:23:34 -040058
Chuck Lever0a904872017-02-08 17:00:35 -050059#include <rdma/ib_cm.h>
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040060
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040061#include "xprt_rdma.h"
62
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040063/*
64 * Globals/Macros
65 */
66
Jeff Laytonf895b252014-11-17 16:58:04 -050067#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040068# define RPCDBG_FACILITY RPCDBG_TRANS
69#endif
70
71/*
72 * internal functions
73 */
Chuck Lever96cedde2017-12-14 20:57:55 -050074static void rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt);
75static void rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf);
Chuck Leverbebd0312017-04-11 13:23:10 -040076static void rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040077
Chuck Leverd8f532d2017-10-16 15:01:30 -040078struct workqueue_struct *rpcrdma_receive_wq __read_mostly;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040079
Chuck Leverfe97b472015-10-24 17:27:10 -040080int
81rpcrdma_alloc_wq(void)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040082{
Chuck Leverfe97b472015-10-24 17:27:10 -040083 struct workqueue_struct *recv_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040084
Chuck Leverfe97b472015-10-24 17:27:10 -040085 recv_wq = alloc_workqueue("xprtrdma_receive",
Chuck Leverccede752017-12-04 14:04:04 -050086 WQ_MEM_RECLAIM | WQ_HIGHPRI,
Chuck Leverfe97b472015-10-24 17:27:10 -040087 0);
88 if (!recv_wq)
89 return -ENOMEM;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040090
Chuck Leverfe97b472015-10-24 17:27:10 -040091 rpcrdma_receive_wq = recv_wq;
92 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040093}
94
Chuck Leverfe97b472015-10-24 17:27:10 -040095void
96rpcrdma_destroy_wq(void)
Chuck Leverf1a03b72014-11-08 20:14:37 -050097{
Chuck Leverfe97b472015-10-24 17:27:10 -040098 struct workqueue_struct *wq;
Chuck Leverf1a03b72014-11-08 20:14:37 -050099
Chuck Leverfe97b472015-10-24 17:27:10 -0400100 if (rpcrdma_receive_wq) {
101 wq = rpcrdma_receive_wq;
102 rpcrdma_receive_wq = NULL;
103 destroy_workqueue(wq);
104 }
Chuck Leverf1a03b72014-11-08 20:14:37 -0500105}
106
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400107static void
108rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
109{
110 struct rpcrdma_ep *ep = context;
111
Chuck Lever2f6922c2016-11-29 10:53:21 -0500112 pr_err("rpcrdma: %s on device %s ep %p\n",
113 ib_event_msg(event->event), event->device->name, context);
114
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400115 if (ep->rep_connected == 1) {
116 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500117 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400118 wake_up_all(&ep->rep_connect_wait);
119 }
120}
121
Chuck Lever2fa8f882016-03-04 11:28:53 -0500122/**
123 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
124 * @cq: completion queue (ignored)
125 * @wc: completed WR
126 *
Chuck Lever4220a072015-10-24 17:26:45 -0400127 */
128static void
Chuck Lever2fa8f882016-03-04 11:28:53 -0500129rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400130{
Chuck Leverae729502017-10-20 10:48:12 -0400131 struct ib_cqe *cqe = wc->wr_cqe;
132 struct rpcrdma_sendctx *sc =
133 container_of(cqe, struct rpcrdma_sendctx, sc_cqe);
134
Chuck Lever2fa8f882016-03-04 11:28:53 -0500135 /* WARNING: Only wr_cqe and status are reliable at this point */
Chuck Leverab03eff2017-12-20 16:30:40 -0500136 trace_xprtrdma_wc_send(sc, wc);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500137 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
138 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
139 ib_wc_status_msg(wc->status),
140 wc->status, wc->vendor_err);
Chuck Leverae729502017-10-20 10:48:12 -0400141
142 rpcrdma_sendctx_put_locked(sc);
Chuck Leverfc664482014-05-28 10:33:25 -0400143}
144
Chuck Lever552bf222016-03-04 11:28:36 -0500145/**
Chuck Lever1519e962016-09-15 10:57:49 -0400146 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
Chuck Lever552bf222016-03-04 11:28:36 -0500147 * @cq: completion queue (ignored)
148 * @wc: completed WR
149 *
150 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400151static void
Chuck Lever1519e962016-09-15 10:57:49 -0400152rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400153{
Chuck Lever552bf222016-03-04 11:28:36 -0500154 struct ib_cqe *cqe = wc->wr_cqe;
155 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
156 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400157
Chuck Lever85024272015-01-21 11:02:04 -0500158 /* WARNING: Only wr_id and status are reliable at this point */
Chuck Leverb4a7f912017-12-20 16:30:48 -0500159 trace_xprtrdma_wc_receive(rep, wc);
Chuck Lever85024272015-01-21 11:02:04 -0500160 if (wc->status != IB_WC_SUCCESS)
161 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400162
Chuck Lever85024272015-01-21 11:02:04 -0500163 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Lever96f87782017-08-03 14:30:03 -0400164 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len);
Chuck Leverc8b920b2016-09-15 10:57:16 -0400165 rep->rr_wc_flags = wc->wc_flags;
166 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
167
Chuck Lever91a10c52017-04-11 13:23:02 -0400168 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
Chuck Lever6b1184c2015-01-21 11:04:25 -0500169 rdmab_addr(rep->rr_rdmabuf),
Chuck Levere2a67192017-08-03 14:30:44 -0400170 wc->byte_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500171
Chuck Leverfc664482014-05-28 10:33:25 -0400172out_schedule:
Chuck Leverd8f532d2017-10-16 15:01:30 -0400173 rpcrdma_reply_handler(rep);
Chuck Lever85024272015-01-21 11:02:04 -0500174 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400175
Chuck Lever85024272015-01-21 11:02:04 -0500176out_fail:
177 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500178 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
179 ib_wc_status_msg(wc->status),
180 wc->status, wc->vendor_err);
Chuck Levere2a67192017-08-03 14:30:44 -0400181 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, 0);
Chuck Lever85024272015-01-21 11:02:04 -0500182 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400183}
184
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400185static void
186rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
187 struct rdma_conn_param *param)
188{
189 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
190 const struct rpcrdma_connect_private *pmsg = param->private_data;
191 unsigned int rsize, wsize;
192
Chuck Leverc8b920b2016-09-15 10:57:16 -0400193 /* Default settings for RPC-over-RDMA Version One */
Chuck Leverb5f0afb2017-02-08 16:59:54 -0500194 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400195 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
196 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
197
198 if (pmsg &&
199 pmsg->cp_magic == rpcrdma_cmp_magic &&
200 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
Chuck Leverc95a3c62017-02-08 17:00:02 -0500201 r_xprt->rx_ia.ri_implicit_roundup = true;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400202 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
203 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
204 }
205
206 if (rsize < cdata->inline_rsize)
207 cdata->inline_rsize = rsize;
208 if (wsize < cdata->inline_wsize)
209 cdata->inline_wsize = wsize;
Chuck Lever6d6bf722016-11-29 10:53:13 -0500210 dprintk("RPC: %s: max send %u, max recv %u\n",
211 __func__, cdata->inline_wsize, cdata->inline_rsize);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400212 rpcrdma_set_max_header_sizes(r_xprt);
213}
214
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400215static int
216rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
217{
218 struct rpcrdma_xprt *xprt = id->context;
219 struct rpcrdma_ia *ia = &xprt->rx_ia;
220 struct rpcrdma_ep *ep = &xprt->rx_ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400221 int connstate = 0;
222
Chuck Leverb4744e02017-12-20 16:31:29 -0500223 trace_xprtrdma_conn_upcall(xprt, event);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400224 switch (event->event) {
225 case RDMA_CM_EVENT_ADDR_RESOLVED:
226 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400227 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400228 complete(&ia->ri_done);
229 break;
230 case RDMA_CM_EVENT_ADDR_ERROR:
231 ia->ri_async_rc = -EHOSTUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400232 complete(&ia->ri_done);
233 break;
234 case RDMA_CM_EVENT_ROUTE_ERROR:
235 ia->ri_async_rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400236 complete(&ia->ri_done);
237 break;
Chuck Leverbebd0312017-04-11 13:23:10 -0400238 case RDMA_CM_EVENT_DEVICE_REMOVAL:
239#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Leverd461f1f2017-12-14 20:56:50 -0500240 pr_info("rpcrdma: removing device %s for %s:%s\n",
Chuck Lever173b8f42017-06-08 11:53:00 -0400241 ia->ri_device->name,
Chuck Leverd461f1f2017-12-14 20:56:50 -0500242 rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt));
Chuck Leverbebd0312017-04-11 13:23:10 -0400243#endif
244 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
245 ep->rep_connected = -ENODEV;
246 xprt_force_disconnect(&xprt->rx_xprt);
247 wait_for_completion(&ia->ri_remove_done);
248
249 ia->ri_id = NULL;
250 ia->ri_pd = NULL;
251 ia->ri_device = NULL;
252 /* Return 1 to ensure the core destroys the id. */
253 return 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400254 case RDMA_CM_EVENT_ESTABLISHED:
255 connstate = 1;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400256 rpcrdma_update_connect_private(xprt, &event->param.conn);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400257 goto connected;
258 case RDMA_CM_EVENT_CONNECT_ERROR:
259 connstate = -ENOTCONN;
260 goto connected;
261 case RDMA_CM_EVENT_UNREACHABLE:
262 connstate = -ENETDOWN;
263 goto connected;
264 case RDMA_CM_EVENT_REJECTED:
Chuck Leverd461f1f2017-12-14 20:56:50 -0500265 dprintk("rpcrdma: connection to %s:%s rejected: %s\n",
266 rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt),
Chuck Lever0a904872017-02-08 17:00:35 -0500267 rdma_reject_msg(id, event->status));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400268 connstate = -ECONNREFUSED;
Chuck Lever0a904872017-02-08 17:00:35 -0500269 if (event->status == IB_CM_REJ_STALE_CONN)
270 connstate = -EAGAIN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400271 goto connected;
272 case RDMA_CM_EVENT_DISCONNECTED:
273 connstate = -ECONNABORTED;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400274connected:
Chuck Leverbe798f92017-10-16 15:01:39 -0400275 xprt->rx_buf.rb_credits = 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400276 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500277 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400278 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400279 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400280 default:
Chuck Leverd461f1f2017-12-14 20:56:50 -0500281 dprintk("RPC: %s: %s:%s on %s/%s (ep 0x%p): %s\n",
282 __func__,
283 rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt),
Chuck Lever173b8f42017-06-08 11:53:00 -0400284 ia->ri_device->name, ia->ri_ops->ro_displayname,
285 ep, rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400286 break;
287 }
288
289 return 0;
290}
291
292static struct rdma_cm_id *
Chuck Leverdd229ce2017-12-14 20:56:58 -0500293rpcrdma_create_id(struct rpcrdma_xprt *xprt, struct rpcrdma_ia *ia)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400294{
Chuck Lever109b88a2016-11-29 10:52:40 -0500295 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400296 struct rdma_cm_id *id;
297 int rc;
298
Chuck Leverb4744e02017-12-20 16:31:29 -0500299 trace_xprtrdma_conn_start(xprt);
300
Tom Talpey1a954052008-10-09 15:01:31 -0400301 init_completion(&ia->ri_done);
Chuck Leverbebd0312017-04-11 13:23:10 -0400302 init_completion(&ia->ri_remove_done);
Tom Talpey1a954052008-10-09 15:01:31 -0400303
Guy Shapirofa201052015-10-22 15:20:10 +0300304 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
305 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400306 if (IS_ERR(id)) {
307 rc = PTR_ERR(id);
308 dprintk("RPC: %s: rdma_create_id() failed %i\n",
309 __func__, rc);
310 return id;
311 }
312
Tom Talpey5675add2008-10-09 15:01:41 -0400313 ia->ri_async_rc = -ETIMEDOUT;
Chuck Leverdd229ce2017-12-14 20:56:58 -0500314 rc = rdma_resolve_addr(id, NULL,
315 (struct sockaddr *)&xprt->rx_xprt.addr,
316 RDMA_RESOLVE_TIMEOUT);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400317 if (rc) {
318 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
319 __func__, rc);
320 goto out;
321 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500322 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
323 if (rc < 0) {
Chuck Leverb4744e02017-12-20 16:31:29 -0500324 trace_xprtrdma_conn_tout(xprt);
Chuck Lever109b88a2016-11-29 10:52:40 -0500325 goto out;
326 }
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400327
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400328 rc = ia->ri_async_rc;
329 if (rc)
330 goto out;
331
Tom Talpey5675add2008-10-09 15:01:41 -0400332 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400333 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
334 if (rc) {
335 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
336 __func__, rc);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400337 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400338 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500339 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
340 if (rc < 0) {
Chuck Leverb4744e02017-12-20 16:31:29 -0500341 trace_xprtrdma_conn_tout(xprt);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400342 goto out;
Chuck Lever109b88a2016-11-29 10:52:40 -0500343 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400344 rc = ia->ri_async_rc;
345 if (rc)
Chuck Lever56a6bd12017-04-11 13:23:34 -0400346 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400347
348 return id;
Chuck Lever56a6bd12017-04-11 13:23:34 -0400349
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400350out:
351 rdma_destroy_id(id);
352 return ERR_PTR(rc);
353}
354
355/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400356 * Exported functions.
357 */
358
Chuck Leverfff09592017-04-11 13:22:54 -0400359/**
360 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
Chuck Leverdd229ce2017-12-14 20:56:58 -0500361 * @xprt: transport with IA to (re)initialize
Chuck Leverfff09592017-04-11 13:22:54 -0400362 *
363 * Returns 0 on success, negative errno if an appropriate
364 * Interface Adapter could not be found and opened.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400365 */
366int
Chuck Leverdd229ce2017-12-14 20:56:58 -0500367rpcrdma_ia_open(struct rpcrdma_xprt *xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400368{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400369 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400370 int rc;
371
Chuck Leverdd229ce2017-12-14 20:56:58 -0500372 ia->ri_id = rpcrdma_create_id(xprt, ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400373 if (IS_ERR(ia->ri_id)) {
374 rc = PTR_ERR(ia->ri_id);
Chuck Leverfff09592017-04-11 13:22:54 -0400375 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400376 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400377 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400378
Christoph Hellwiged082d32016-09-05 12:56:17 +0200379 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400380 if (IS_ERR(ia->ri_pd)) {
381 rc = PTR_ERR(ia->ri_pd);
Chuck Leverb54054c2016-06-29 13:53:27 -0400382 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
Chuck Leverfff09592017-04-11 13:22:54 -0400383 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400384 }
385
Chuck Leverfff09592017-04-11 13:22:54 -0400386 switch (xprt_rdma_memreg_strategy) {
Chuck Leverce5b3712017-12-14 20:57:47 -0500387 case RPCRDMA_FRWR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400388 if (frwr_is_supported(ia)) {
389 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
390 break;
391 }
392 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400393 case RPCRDMA_MTHCAFMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400394 if (fmr_is_supported(ia)) {
395 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
396 break;
397 }
398 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400399 default:
Chuck Leverfff09592017-04-11 13:22:54 -0400400 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
401 ia->ri_device->name, xprt_rdma_memreg_strategy);
Chuck Leverb54054c2016-06-29 13:53:27 -0400402 rc = -EINVAL;
Chuck Leverfff09592017-04-11 13:22:54 -0400403 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400404 }
405
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400406 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500407
Chuck Leverfff09592017-04-11 13:22:54 -0400408out_err:
409 rpcrdma_ia_close(ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400410 return rc;
411}
412
Chuck Leverfff09592017-04-11 13:22:54 -0400413/**
Chuck Leverbebd0312017-04-11 13:23:10 -0400414 * rpcrdma_ia_remove - Handle device driver unload
415 * @ia: interface adapter being removed
416 *
417 * Divest transport H/W resources associated with this adapter,
418 * but allow it to be restored later.
419 */
420void
421rpcrdma_ia_remove(struct rpcrdma_ia *ia)
422{
423 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
424 rx_ia);
425 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
426 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
427 struct rpcrdma_req *req;
428 struct rpcrdma_rep *rep;
429
430 cancel_delayed_work_sync(&buf->rb_refresh_worker);
431
432 /* This is similar to rpcrdma_ep_destroy, but:
433 * - Don't cancel the connect worker.
434 * - Don't call rpcrdma_ep_disconnect, which waits
435 * for another conn upcall, which will deadlock.
436 * - rdma_disconnect is unneeded, the underlying
437 * connection is already gone.
438 */
439 if (ia->ri_id->qp) {
440 ib_drain_qp(ia->ri_id->qp);
441 rdma_destroy_qp(ia->ri_id);
442 ia->ri_id->qp = NULL;
443 }
444 ib_free_cq(ep->rep_attr.recv_cq);
445 ib_free_cq(ep->rep_attr.send_cq);
446
447 /* The ULP is responsible for ensuring all DMA
448 * mappings and MRs are gone.
449 */
450 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
451 rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
452 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
453 rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
454 rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
455 rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
456 }
Chuck Lever96cedde2017-12-14 20:57:55 -0500457 rpcrdma_mrs_destroy(buf);
Chuck Leverbebd0312017-04-11 13:23:10 -0400458
459 /* Allow waiters to continue */
460 complete(&ia->ri_remove_done);
Chuck Leverb4744e02017-12-20 16:31:29 -0500461
462 trace_xprtrdma_remove(r_xprt);
Chuck Leverbebd0312017-04-11 13:23:10 -0400463}
464
465/**
Chuck Leverfff09592017-04-11 13:22:54 -0400466 * rpcrdma_ia_close - Clean up/close an IA.
467 * @ia: interface adapter to close
468 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400469 */
470void
471rpcrdma_ia_close(struct rpcrdma_ia *ia)
472{
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400473 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
474 if (ia->ri_id->qp)
475 rdma_destroy_qp(ia->ri_id);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400476 rdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400477 }
Chuck Leverfff09592017-04-11 13:22:54 -0400478 ia->ri_id = NULL;
479 ia->ri_device = NULL;
Chuck Lever6d446982015-05-26 11:51:27 -0400480
481 /* If the pd is still busy, xprtrdma missed freeing a resource */
482 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600483 ib_dealloc_pd(ia->ri_pd);
Chuck Leverfff09592017-04-11 13:22:54 -0400484 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400485}
486
487/*
488 * Create unconnected endpoint.
489 */
490int
491rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
Chuck Lever16f906d2017-02-08 17:00:10 -0500492 struct rpcrdma_create_data_internal *cdata)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400493{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400494 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
Chuck Lever16f906d2017-02-08 17:00:10 -0500495 unsigned int max_qp_wr, max_sge;
Chuck Leverfc664482014-05-28 10:33:25 -0400496 struct ib_cq *sendcq, *recvcq;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500497 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400498
Chuck Levereed50872017-03-11 15:52:47 -0500499 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
500 RPCRDMA_MAX_SEND_SGES);
Chuck Lever16f906d2017-02-08 17:00:10 -0500501 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
502 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
Chuck Leverb3221d62015-08-03 13:03:39 -0400503 return -ENOMEM;
504 }
Chuck Lever16f906d2017-02-08 17:00:10 -0500505 ia->ri_max_send_sges = max_sge - RPCRDMA_MIN_SEND_SGES;
Chuck Leverb3221d62015-08-03 13:03:39 -0400506
Or Gerlitze3e45b12015-12-18 10:59:48 +0200507 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400508 dprintk("RPC: %s: insufficient wqe's available\n",
509 __func__);
510 return -ENOMEM;
511 }
Chuck Lever550d7502016-05-02 14:41:47 -0400512 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
Chuck Lever124fa172015-10-24 17:27:51 -0400513
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400514 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400515 if (cdata->max_requests > max_qp_wr)
516 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400517
518 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
519 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400520 ep->rep_attr.srq = NULL;
521 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400522 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400523 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
Chuck Lever3968cb52015-03-30 14:35:26 -0400524 rc = ia->ri_ops->ro_open(ia, ep, cdata);
525 if (rc)
526 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400527 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400528 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400529 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
Chuck Lever16f906d2017-02-08 17:00:10 -0500530 ep->rep_attr.cap.max_send_sge = max_sge;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400531 ep->rep_attr.cap.max_recv_sge = 1;
532 ep->rep_attr.cap.max_inline_data = 0;
533 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
534 ep->rep_attr.qp_type = IB_QPT_RC;
535 ep->rep_attr.port_num = ~0;
536
537 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
538 "iovs: send %d recv %d\n",
539 __func__,
540 ep->rep_attr.cap.max_send_wr,
541 ep->rep_attr.cap.max_recv_wr,
542 ep->rep_attr.cap.max_send_sge,
543 ep->rep_attr.cap.max_recv_sge);
544
545 /* set trigger for requesting send completion */
Chuck Leverae729502017-10-20 10:48:12 -0400546 ep->rep_send_batch = min_t(unsigned int, RPCRDMA_MAX_SEND_BATCH,
547 cdata->max_requests >> 2);
548 ep->rep_send_count = ep->rep_send_batch;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400549 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400550 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400551
Chuck Lever2fa8f882016-03-04 11:28:53 -0500552 sendcq = ib_alloc_cq(ia->ri_device, NULL,
553 ep->rep_attr.cap.max_send_wr + 1,
Chuck Levera4699f52017-10-30 16:21:49 -0400554 1, IB_POLL_WORKQUEUE);
Chuck Leverfc664482014-05-28 10:33:25 -0400555 if (IS_ERR(sendcq)) {
556 rc = PTR_ERR(sendcq);
557 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400558 __func__, rc);
559 goto out1;
560 }
561
Chuck Lever552bf222016-03-04 11:28:36 -0500562 recvcq = ib_alloc_cq(ia->ri_device, NULL,
563 ep->rep_attr.cap.max_recv_wr + 1,
Chuck Leverd8f532d2017-10-16 15:01:30 -0400564 0, IB_POLL_WORKQUEUE);
Chuck Leverfc664482014-05-28 10:33:25 -0400565 if (IS_ERR(recvcq)) {
566 rc = PTR_ERR(recvcq);
567 dprintk("RPC: %s: failed to create recv CQ: %i\n",
568 __func__, rc);
569 goto out2;
570 }
571
Chuck Leverfc664482014-05-28 10:33:25 -0400572 ep->rep_attr.send_cq = sendcq;
573 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400574
575 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400576 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400577
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400578 /* Prepare RDMA-CM private message */
579 pmsg->cp_magic = rpcrdma_cmp_magic;
580 pmsg->cp_version = RPCRDMA_CMP_VERSION;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400581 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400582 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
583 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
584 ep->rep_remote_cma.private_data = pmsg;
585 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400586
587 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400588 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200589 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400590 ep->rep_remote_cma.responder_resources = 32;
591 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500592 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200593 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400594
Chuck Leverb2dde942016-05-02 14:43:03 -0400595 /* Limit transport retries so client can detect server
596 * GID changes quickly. RPC layer handles re-establishing
597 * transport connection and retransmission.
598 */
599 ep->rep_remote_cma.retry_count = 6;
600
601 /* RPC-over-RDMA handles its own flow control. In addition,
602 * make all RNR NAKs visible so we know that RPC-over-RDMA
603 * flow control is working correctly (no NAKs should be seen).
604 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400605 ep->rep_remote_cma.flow_control = 0;
606 ep->rep_remote_cma.rnr_retry_count = 0;
607
608 return 0;
609
610out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500611 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400612out1:
613 return rc;
614}
615
616/*
617 * rpcrdma_ep_destroy
618 *
619 * Disconnect and destroy endpoint. After this, the only
620 * valid operations on the ep are to free it (if dynamically
621 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400622 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400623void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400624rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
625{
Chuck Lever254f91e2014-05-28 10:32:17 -0400626 cancel_delayed_work_sync(&ep->rep_connect_worker);
627
Steve Wise72c02172015-09-21 12:24:23 -0500628 if (ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400629 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400630 rdma_destroy_qp(ia->ri_id);
631 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400632 }
633
Chuck Lever552bf222016-03-04 11:28:36 -0500634 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500635 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400636}
637
Chuck Levera9b0e382017-04-11 13:23:26 -0400638/* Re-establish a connection after a device removal event.
639 * Unlike a normal reconnection, a fresh PD and a new set
640 * of MRs and buffers is needed.
641 */
642static int
643rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
644 struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
645{
Chuck Levera9b0e382017-04-11 13:23:26 -0400646 int rc, err;
647
Chuck Leverb4744e02017-12-20 16:31:29 -0500648 trace_xprtrdma_reinsert(r_xprt);
Chuck Levera9b0e382017-04-11 13:23:26 -0400649
650 rc = -EHOSTUNREACH;
Chuck Leverdd229ce2017-12-14 20:56:58 -0500651 if (rpcrdma_ia_open(r_xprt))
Chuck Levera9b0e382017-04-11 13:23:26 -0400652 goto out1;
653
654 rc = -ENOMEM;
655 err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
656 if (err) {
657 pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
658 goto out2;
659 }
660
661 rc = -ENETUNREACH;
662 err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
663 if (err) {
664 pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
665 goto out3;
666 }
667
Chuck Lever96cedde2017-12-14 20:57:55 -0500668 rpcrdma_mrs_create(r_xprt);
Chuck Levera9b0e382017-04-11 13:23:26 -0400669 return 0;
670
671out3:
672 rpcrdma_ep_destroy(ep, ia);
673out2:
674 rpcrdma_ia_close(ia);
675out1:
676 return rc;
677}
678
Chuck Lever18908962017-04-11 13:23:18 -0400679static int
680rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
681 struct rpcrdma_ia *ia)
682{
Chuck Lever18908962017-04-11 13:23:18 -0400683 struct rdma_cm_id *id, *old;
684 int err, rc;
685
Chuck Leverb4744e02017-12-20 16:31:29 -0500686 trace_xprtrdma_reconnect(r_xprt);
Chuck Lever18908962017-04-11 13:23:18 -0400687
688 rpcrdma_ep_disconnect(ep, ia);
689
690 rc = -EHOSTUNREACH;
Chuck Leverdd229ce2017-12-14 20:56:58 -0500691 id = rpcrdma_create_id(r_xprt, ia);
Chuck Lever18908962017-04-11 13:23:18 -0400692 if (IS_ERR(id))
693 goto out;
694
695 /* As long as the new ID points to the same device as the
696 * old ID, we can reuse the transport's existing PD and all
697 * previously allocated MRs. Also, the same device means
698 * the transport's previous DMA mappings are still valid.
699 *
700 * This is a sanity check only. There should be no way these
701 * point to two different devices here.
702 */
703 old = id;
704 rc = -ENETUNREACH;
705 if (ia->ri_device != id->device) {
706 pr_err("rpcrdma: can't reconnect on different device!\n");
707 goto out_destroy;
708 }
709
710 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
711 if (err) {
712 dprintk("RPC: %s: rdma_create_qp returned %d\n",
713 __func__, err);
714 goto out_destroy;
715 }
716
717 /* Atomically replace the transport's ID and QP. */
718 rc = 0;
719 old = ia->ri_id;
720 ia->ri_id = id;
721 rdma_destroy_qp(old);
722
723out_destroy:
Chuck Lever56a6bd12017-04-11 13:23:34 -0400724 rdma_destroy_id(old);
Chuck Lever18908962017-04-11 13:23:18 -0400725out:
726 return rc;
727}
728
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400729/*
730 * Connect unconnected endpoint.
731 */
732int
733rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
734{
Chuck Lever0a904872017-02-08 17:00:35 -0500735 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
736 rx_ia);
Chuck Lever0a904872017-02-08 17:00:35 -0500737 unsigned int extras;
Chuck Lever18908962017-04-11 13:23:18 -0400738 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400739
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400740retry:
Chuck Lever18908962017-04-11 13:23:18 -0400741 switch (ep->rep_connected) {
742 case 0:
Chuck Leverec62f402014-05-28 10:34:07 -0400743 dprintk("RPC: %s: connecting...\n", __func__);
744 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
745 if (rc) {
746 dprintk("RPC: %s: rdma_create_qp failed %i\n",
747 __func__, rc);
Chuck Lever18908962017-04-11 13:23:18 -0400748 rc = -ENETUNREACH;
749 goto out_noupdate;
Chuck Leverec62f402014-05-28 10:34:07 -0400750 }
Chuck Lever18908962017-04-11 13:23:18 -0400751 break;
Chuck Levera9b0e382017-04-11 13:23:26 -0400752 case -ENODEV:
753 rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
754 if (rc)
755 goto out_noupdate;
756 break;
Chuck Lever18908962017-04-11 13:23:18 -0400757 default:
758 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
759 if (rc)
760 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400761 }
762
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400763 ep->rep_connected = 0;
764
765 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
766 if (rc) {
767 dprintk("RPC: %s: rdma_connect() failed with %i\n",
768 __func__, rc);
769 goto out;
770 }
771
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400772 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400773 if (ep->rep_connected <= 0) {
Chuck Lever0a904872017-02-08 17:00:35 -0500774 if (ep->rep_connected == -EAGAIN)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400775 goto retry;
776 rc = ep->rep_connected;
Chuck Lever0a904872017-02-08 17:00:35 -0500777 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400778 }
779
Chuck Lever0a904872017-02-08 17:00:35 -0500780 dprintk("RPC: %s: connected\n", __func__);
781 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
782 if (extras)
783 rpcrdma_ep_post_extra_recv(r_xprt, extras);
784
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400785out:
786 if (rc)
787 ep->rep_connected = rc;
Chuck Lever18908962017-04-11 13:23:18 -0400788
789out_noupdate:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400790 return rc;
791}
792
793/*
794 * rpcrdma_ep_disconnect
795 *
796 * This is separate from destroy to facilitate the ability
797 * to reconnect without recreating the endpoint.
798 *
799 * This call is not reentrant, and must not be made in parallel
800 * on the same endpoint.
801 */
Chuck Lever282191c2014-07-29 17:25:55 -0400802void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400803rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
804{
805 int rc;
806
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400807 rc = rdma_disconnect(ia->ri_id);
Chuck Leverb4744e02017-12-20 16:31:29 -0500808 if (!rc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400809 /* returns without wait if not connected */
810 wait_event_interruptible(ep->rep_connect_wait,
811 ep->rep_connected != 1);
Chuck Leverb4744e02017-12-20 16:31:29 -0500812 else
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400813 ep->rep_connected = rc;
Chuck Leverb4744e02017-12-20 16:31:29 -0500814 trace_xprtrdma_disconnect(container_of(ep, struct rpcrdma_xprt,
815 rx_ep), rc);
Chuck Lever550d7502016-05-02 14:41:47 -0400816
817 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400818}
819
Chuck Leverae729502017-10-20 10:48:12 -0400820/* Fixed-size circular FIFO queue. This implementation is wait-free and
821 * lock-free.
822 *
823 * Consumer is the code path that posts Sends. This path dequeues a
824 * sendctx for use by a Send operation. Multiple consumer threads
825 * are serialized by the RPC transport lock, which allows only one
826 * ->send_request call at a time.
827 *
828 * Producer is the code path that handles Send completions. This path
829 * enqueues a sendctx that has been completed. Multiple producer
830 * threads are serialized by the ib_poll_cq() function.
831 */
832
833/* rpcrdma_sendctxs_destroy() assumes caller has already quiesced
834 * queue activity, and ib_drain_qp has flushed all remaining Send
835 * requests.
836 */
837static void rpcrdma_sendctxs_destroy(struct rpcrdma_buffer *buf)
838{
839 unsigned long i;
840
841 for (i = 0; i <= buf->rb_sc_last; i++)
842 kfree(buf->rb_sc_ctxs[i]);
843 kfree(buf->rb_sc_ctxs);
844}
845
846static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ia *ia)
847{
848 struct rpcrdma_sendctx *sc;
849
850 sc = kzalloc(sizeof(*sc) +
851 ia->ri_max_send_sges * sizeof(struct ib_sge),
852 GFP_KERNEL);
853 if (!sc)
854 return NULL;
855
856 sc->sc_wr.wr_cqe = &sc->sc_cqe;
857 sc->sc_wr.sg_list = sc->sc_sges;
858 sc->sc_wr.opcode = IB_WR_SEND;
859 sc->sc_cqe.done = rpcrdma_wc_send;
860 return sc;
861}
862
863static int rpcrdma_sendctxs_create(struct rpcrdma_xprt *r_xprt)
864{
865 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
866 struct rpcrdma_sendctx *sc;
867 unsigned long i;
868
869 /* Maximum number of concurrent outstanding Send WRs. Capping
870 * the circular queue size stops Send Queue overflow by causing
871 * the ->send_request call to fail temporarily before too many
872 * Sends are posted.
873 */
874 i = buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS;
875 dprintk("RPC: %s: allocating %lu send_ctxs\n", __func__, i);
876 buf->rb_sc_ctxs = kcalloc(i, sizeof(sc), GFP_KERNEL);
877 if (!buf->rb_sc_ctxs)
878 return -ENOMEM;
879
880 buf->rb_sc_last = i - 1;
881 for (i = 0; i <= buf->rb_sc_last; i++) {
882 sc = rpcrdma_sendctx_create(&r_xprt->rx_ia);
883 if (!sc)
884 goto out_destroy;
885
886 sc->sc_xprt = r_xprt;
887 buf->rb_sc_ctxs[i] = sc;
888 }
889
890 return 0;
891
892out_destroy:
893 rpcrdma_sendctxs_destroy(buf);
894 return -ENOMEM;
895}
896
897/* The sendctx queue is not guaranteed to have a size that is a
898 * power of two, thus the helpers in circ_buf.h cannot be used.
899 * The other option is to use modulus (%), which can be expensive.
900 */
901static unsigned long rpcrdma_sendctx_next(struct rpcrdma_buffer *buf,
902 unsigned long item)
903{
904 return likely(item < buf->rb_sc_last) ? item + 1 : 0;
905}
906
907/**
908 * rpcrdma_sendctx_get_locked - Acquire a send context
909 * @buf: transport buffers from which to acquire an unused context
910 *
911 * Returns pointer to a free send completion context; or NULL if
912 * the queue is empty.
913 *
914 * Usage: Called to acquire an SGE array before preparing a Send WR.
915 *
916 * The caller serializes calls to this function (per rpcrdma_buffer),
917 * and provides an effective memory barrier that flushes the new value
918 * of rb_sc_head.
919 */
920struct rpcrdma_sendctx *rpcrdma_sendctx_get_locked(struct rpcrdma_buffer *buf)
921{
922 struct rpcrdma_xprt *r_xprt;
923 struct rpcrdma_sendctx *sc;
924 unsigned long next_head;
925
926 next_head = rpcrdma_sendctx_next(buf, buf->rb_sc_head);
927
928 if (next_head == READ_ONCE(buf->rb_sc_tail))
929 goto out_emptyq;
930
931 /* ORDER: item must be accessed _before_ head is updated */
932 sc = buf->rb_sc_ctxs[next_head];
933
934 /* Releasing the lock in the caller acts as a memory
935 * barrier that flushes rb_sc_head.
936 */
937 buf->rb_sc_head = next_head;
938
939 return sc;
940
941out_emptyq:
942 /* The queue is "empty" if there have not been enough Send
943 * completions recently. This is a sign the Send Queue is
944 * backing up. Cause the caller to pause and try again.
945 */
946 dprintk("RPC: %s: empty sendctx queue\n", __func__);
947 r_xprt = container_of(buf, struct rpcrdma_xprt, rx_buf);
948 r_xprt->rx_stats.empty_sendctx_q++;
949 return NULL;
950}
951
952/**
953 * rpcrdma_sendctx_put_locked - Release a send context
954 * @sc: send context to release
955 *
956 * Usage: Called from Send completion to return a sendctxt
957 * to the queue.
958 *
959 * The caller serializes calls to this function (per rpcrdma_buffer).
960 */
961void rpcrdma_sendctx_put_locked(struct rpcrdma_sendctx *sc)
962{
963 struct rpcrdma_buffer *buf = &sc->sc_xprt->rx_buf;
964 unsigned long next_tail;
965
966 /* Unmap SGEs of previously completed by unsignaled
967 * Sends by walking up the queue until @sc is found.
968 */
969 next_tail = buf->rb_sc_tail;
970 do {
971 next_tail = rpcrdma_sendctx_next(buf, next_tail);
972
973 /* ORDER: item must be accessed _before_ tail is updated */
974 rpcrdma_unmap_sendctx(buf->rb_sc_ctxs[next_tail]);
975
976 } while (buf->rb_sc_ctxs[next_tail] != sc);
977
978 /* Paired with READ_ONCE */
979 smp_store_release(&buf->rb_sc_tail, next_tail);
980}
981
Chuck Lever505bbe62016-06-29 13:52:54 -0400982static void
983rpcrdma_mr_recovery_worker(struct work_struct *work)
984{
985 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
986 rb_recovery_worker.work);
Chuck Lever96cedde2017-12-14 20:57:55 -0500987 struct rpcrdma_mr *mr;
Chuck Lever505bbe62016-06-29 13:52:54 -0400988
989 spin_lock(&buf->rb_recovery_lock);
990 while (!list_empty(&buf->rb_stale_mrs)) {
Chuck Lever96cedde2017-12-14 20:57:55 -0500991 mr = rpcrdma_mr_pop(&buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400992 spin_unlock(&buf->rb_recovery_lock);
993
Chuck Lever1c443eff2017-12-20 16:31:21 -0500994 trace_xprtrdma_recover_mr(mr);
Chuck Lever96cedde2017-12-14 20:57:55 -0500995 mr->mr_xprt->rx_ia.ri_ops->ro_recover_mr(mr);
Chuck Lever505bbe62016-06-29 13:52:54 -0400996
997 spin_lock(&buf->rb_recovery_lock);
kbuild test robot53d78522016-07-16 06:02:05 +0800998 }
Chuck Lever505bbe62016-06-29 13:52:54 -0400999 spin_unlock(&buf->rb_recovery_lock);
1000}
1001
1002void
Chuck Lever96cedde2017-12-14 20:57:55 -05001003rpcrdma_mr_defer_recovery(struct rpcrdma_mr *mr)
Chuck Lever505bbe62016-06-29 13:52:54 -04001004{
Chuck Lever96cedde2017-12-14 20:57:55 -05001005 struct rpcrdma_xprt *r_xprt = mr->mr_xprt;
Chuck Lever505bbe62016-06-29 13:52:54 -04001006 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1007
1008 spin_lock(&buf->rb_recovery_lock);
Chuck Lever96cedde2017-12-14 20:57:55 -05001009 rpcrdma_mr_push(mr, &buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -04001010 spin_unlock(&buf->rb_recovery_lock);
1011
1012 schedule_delayed_work(&buf->rb_recovery_worker, 0);
1013}
1014
Chuck Levere2ac2362016-06-29 13:54:00 -04001015static void
Chuck Lever96cedde2017-12-14 20:57:55 -05001016rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt)
Chuck Levere2ac2362016-06-29 13:54:00 -04001017{
1018 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1019 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1020 unsigned int count;
1021 LIST_HEAD(free);
1022 LIST_HEAD(all);
1023
1024 for (count = 0; count < 32; count++) {
Chuck Lever96cedde2017-12-14 20:57:55 -05001025 struct rpcrdma_mr *mr;
Chuck Levere2ac2362016-06-29 13:54:00 -04001026 int rc;
1027
Chuck Lever96cedde2017-12-14 20:57:55 -05001028 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1029 if (!mr)
Chuck Levere2ac2362016-06-29 13:54:00 -04001030 break;
1031
Chuck Lever96cedde2017-12-14 20:57:55 -05001032 rc = ia->ri_ops->ro_init_mr(ia, mr);
Chuck Levere2ac2362016-06-29 13:54:00 -04001033 if (rc) {
Chuck Lever96cedde2017-12-14 20:57:55 -05001034 kfree(mr);
Chuck Levere2ac2362016-06-29 13:54:00 -04001035 break;
1036 }
1037
Chuck Lever96cedde2017-12-14 20:57:55 -05001038 mr->mr_xprt = r_xprt;
Chuck Levere2ac2362016-06-29 13:54:00 -04001039
Chuck Lever96cedde2017-12-14 20:57:55 -05001040 list_add(&mr->mr_list, &free);
1041 list_add(&mr->mr_all, &all);
Chuck Levere2ac2362016-06-29 13:54:00 -04001042 }
1043
Chuck Lever96cedde2017-12-14 20:57:55 -05001044 spin_lock(&buf->rb_mrlock);
1045 list_splice(&free, &buf->rb_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001046 list_splice(&all, &buf->rb_all);
1047 r_xprt->rx_stats.mrs_allocated += count;
Chuck Lever96cedde2017-12-14 20:57:55 -05001048 spin_unlock(&buf->rb_mrlock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001049
Chuck Lever1c443eff2017-12-20 16:31:21 -05001050 trace_xprtrdma_createmrs(r_xprt, count);
Chuck Levere2ac2362016-06-29 13:54:00 -04001051}
1052
1053static void
1054rpcrdma_mr_refresh_worker(struct work_struct *work)
1055{
1056 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
1057 rb_refresh_worker.work);
1058 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1059 rx_buf);
1060
Chuck Lever96cedde2017-12-14 20:57:55 -05001061 rpcrdma_mrs_create(r_xprt);
Chuck Levere2ac2362016-06-29 13:54:00 -04001062}
1063
Chuck Leverf531a5d2015-10-24 17:27:43 -04001064struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -05001065rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
1066{
Chuck Leverf531a5d2015-10-24 17:27:43 -04001067 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -05001068 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -05001069
Chuck Lever85275c82015-01-21 11:04:16 -05001070 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -05001071 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -05001072 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -05001073
Chuck Leverf531a5d2015-10-24 17:27:43 -04001074 spin_lock(&buffer->rb_reqslock);
1075 list_add(&req->rl_all, &buffer->rb_allreqs);
1076 spin_unlock(&buffer->rb_reqslock);
Chuck Lever13924022015-01-21 11:03:52 -05001077 req->rl_buffer = &r_xprt->rx_buf;
Chuck Lever9d6b0402016-06-29 13:54:16 -04001078 INIT_LIST_HEAD(&req->rl_registered);
Chuck Lever13924022015-01-21 11:03:52 -05001079 return req;
Chuck Lever13924022015-01-21 11:03:52 -05001080}
1081
Chuck Leverd698c4a2017-12-14 20:56:09 -05001082/**
1083 * rpcrdma_create_rep - Allocate an rpcrdma_rep object
1084 * @r_xprt: controlling transport
1085 *
1086 * Returns 0 on success or a negative errno on failure.
1087 */
1088int
Chuck Lever13924022015-01-21 11:03:52 -05001089rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
1090{
1091 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Leverd698c4a2017-12-14 20:56:09 -05001092 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -05001093 struct rpcrdma_rep *rep;
1094 int rc;
1095
1096 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001097 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -05001098 if (rep == NULL)
1099 goto out;
Chuck Lever13924022015-01-21 11:03:52 -05001100
Chuck Lever13650c22016-09-15 10:56:26 -04001101 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -04001102 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever6b1184c2015-01-21 11:04:25 -05001103 if (IS_ERR(rep->rr_rdmabuf)) {
1104 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001105 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001106 }
Chuck Lever96f87782017-08-03 14:30:03 -04001107 xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base,
1108 rdmab_length(rep->rr_rdmabuf));
Chuck Lever13924022015-01-21 11:03:52 -05001109
Chuck Lever1519e962016-09-15 10:57:49 -04001110 rep->rr_cqe.done = rpcrdma_wc_receive;
Chuck Leverfed171b2015-05-26 11:51:37 -04001111 rep->rr_rxprt = r_xprt;
Chuck Leverd8f532d2017-10-16 15:01:30 -04001112 INIT_WORK(&rep->rr_work, rpcrdma_deferred_completion);
Chuck Lever6ea8e712016-09-15 10:56:51 -04001113 rep->rr_recv_wr.next = NULL;
1114 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
1115 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
1116 rep->rr_recv_wr.num_sge = 1;
Chuck Leverd698c4a2017-12-14 20:56:09 -05001117
1118 spin_lock(&buf->rb_lock);
1119 list_add(&rep->rr_list, &buf->rb_recv_bufs);
1120 spin_unlock(&buf->rb_lock);
1121 return 0;
Chuck Lever13924022015-01-21 11:03:52 -05001122
1123out_free:
1124 kfree(rep);
1125out:
Chuck Leverd698c4a2017-12-14 20:56:09 -05001126 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1127 __func__, rc);
1128 return rc;
Chuck Lever13924022015-01-21 11:03:52 -05001129}
1130
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001131int
Chuck Leverac920d02015-01-21 11:03:44 -05001132rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001133{
Chuck Leverac920d02015-01-21 11:03:44 -05001134 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001135 int i, rc;
1136
Chuck Lever1e465fd2015-10-24 17:27:02 -04001137 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001138 buf->rb_bc_srv_max_requests = 0;
Chuck Lever96cedde2017-12-14 20:57:55 -05001139 spin_lock_init(&buf->rb_mrlock);
Chuck Lever505bbe62016-06-29 13:52:54 -04001140 spin_lock_init(&buf->rb_lock);
1141 spin_lock_init(&buf->rb_recovery_lock);
Chuck Lever96cedde2017-12-14 20:57:55 -05001142 INIT_LIST_HEAD(&buf->rb_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001143 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever505bbe62016-06-29 13:52:54 -04001144 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001145 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1146 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001147 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
1148 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001149
Chuck Lever96cedde2017-12-14 20:57:55 -05001150 rpcrdma_mrs_create(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001151
Chuck Lever1e465fd2015-10-24 17:27:02 -04001152 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001153 INIT_LIST_HEAD(&buf->rb_allreqs);
1154 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001155 for (i = 0; i < buf->rb_max_requests; i++) {
1156 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001157
Chuck Lever13924022015-01-21 11:03:52 -05001158 req = rpcrdma_create_req(r_xprt);
1159 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001160 dprintk("RPC: %s: request buffer %d alloc"
1161 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001162 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001163 goto out;
1164 }
Chuck Levera80d66c2017-06-08 11:52:12 -04001165 list_add(&req->rl_list, &buf->rb_send_bufs);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001166 }
1167
1168 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Leverd698c4a2017-12-14 20:56:09 -05001169 for (i = 0; i <= buf->rb_max_requests; i++) {
1170 rc = rpcrdma_create_rep(r_xprt);
1171 if (rc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001172 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001173 }
Chuck Lever13924022015-01-21 11:03:52 -05001174
Chuck Leverae729502017-10-20 10:48:12 -04001175 rc = rpcrdma_sendctxs_create(r_xprt);
1176 if (rc)
1177 goto out;
1178
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001179 return 0;
1180out:
1181 rpcrdma_buffer_destroy(buf);
1182 return rc;
1183}
1184
Chuck Lever1e465fd2015-10-24 17:27:02 -04001185static struct rpcrdma_req *
1186rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1187{
1188 struct rpcrdma_req *req;
1189
1190 req = list_first_entry(&buf->rb_send_bufs,
Chuck Levera80d66c2017-06-08 11:52:12 -04001191 struct rpcrdma_req, rl_list);
Chuck Lever431af642017-06-08 11:52:20 -04001192 list_del_init(&req->rl_list);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001193 return req;
1194}
1195
1196static struct rpcrdma_rep *
1197rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1198{
1199 struct rpcrdma_rep *rep;
1200
1201 rep = list_first_entry(&buf->rb_recv_bufs,
1202 struct rpcrdma_rep, rr_list);
1203 list_del(&rep->rr_list);
1204 return rep;
1205}
1206
Chuck Lever2e845222014-07-29 17:25:38 -04001207static void
Chuck Lever13650c22016-09-15 10:56:26 -04001208rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -05001209{
Chuck Lever13650c22016-09-15 10:56:26 -04001210 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001211 kfree(rep);
1212}
1213
Chuck Leverf531a5d2015-10-24 17:27:43 -04001214void
Chuck Lever13650c22016-09-15 10:56:26 -04001215rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -05001216{
Chuck Lever13650c22016-09-15 10:56:26 -04001217 rpcrdma_free_regbuf(req->rl_recvbuf);
1218 rpcrdma_free_regbuf(req->rl_sendbuf);
1219 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001220 kfree(req);
1221}
1222
Chuck Levere2ac2362016-06-29 13:54:00 -04001223static void
Chuck Lever96cedde2017-12-14 20:57:55 -05001224rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf)
Chuck Levere2ac2362016-06-29 13:54:00 -04001225{
1226 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1227 rx_buf);
1228 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
Chuck Lever96cedde2017-12-14 20:57:55 -05001229 struct rpcrdma_mr *mr;
Chuck Levere2ac2362016-06-29 13:54:00 -04001230 unsigned int count;
1231
1232 count = 0;
Chuck Lever96cedde2017-12-14 20:57:55 -05001233 spin_lock(&buf->rb_mrlock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001234 while (!list_empty(&buf->rb_all)) {
Chuck Lever96cedde2017-12-14 20:57:55 -05001235 mr = list_entry(buf->rb_all.next, struct rpcrdma_mr, mr_all);
1236 list_del(&mr->mr_all);
Chuck Levere2ac2362016-06-29 13:54:00 -04001237
Chuck Lever96cedde2017-12-14 20:57:55 -05001238 spin_unlock(&buf->rb_mrlock);
1239 ia->ri_ops->ro_release_mr(mr);
Chuck Levere2ac2362016-06-29 13:54:00 -04001240 count++;
Chuck Lever96cedde2017-12-14 20:57:55 -05001241 spin_lock(&buf->rb_mrlock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001242 }
Chuck Lever96cedde2017-12-14 20:57:55 -05001243 spin_unlock(&buf->rb_mrlock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001244 r_xprt->rx_stats.mrs_allocated = 0;
1245
1246 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1247}
1248
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001249void
1250rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1251{
Chuck Lever505bbe62016-06-29 13:52:54 -04001252 cancel_delayed_work_sync(&buf->rb_recovery_worker);
Chuck Lever9378b272017-04-11 13:22:29 -04001253 cancel_delayed_work_sync(&buf->rb_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001254
Chuck Leverae729502017-10-20 10:48:12 -04001255 rpcrdma_sendctxs_destroy(buf);
1256
Chuck Lever1e465fd2015-10-24 17:27:02 -04001257 while (!list_empty(&buf->rb_recv_bufs)) {
1258 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001259
Chuck Lever1e465fd2015-10-24 17:27:02 -04001260 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001261 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001262 }
Chuck Lever05c97462016-09-06 11:22:58 -04001263 buf->rb_send_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001264
Chuck Leverf531a5d2015-10-24 17:27:43 -04001265 spin_lock(&buf->rb_reqslock);
1266 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001267 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001268
Chuck Leverf531a5d2015-10-24 17:27:43 -04001269 req = list_first_entry(&buf->rb_allreqs,
1270 struct rpcrdma_req, rl_all);
1271 list_del(&req->rl_all);
1272
1273 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001274 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001275 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001276 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001277 spin_unlock(&buf->rb_reqslock);
Chuck Lever05c97462016-09-06 11:22:58 -04001278 buf->rb_recv_count = 0;
Chuck Lever9f9d8022014-07-29 17:24:45 -04001279
Chuck Lever96cedde2017-12-14 20:57:55 -05001280 rpcrdma_mrs_destroy(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001281}
1282
Chuck Lever96cedde2017-12-14 20:57:55 -05001283/**
1284 * rpcrdma_mr_get - Allocate an rpcrdma_mr object
1285 * @r_xprt: controlling transport
1286 *
1287 * Returns an initialized rpcrdma_mr or NULL if no free
1288 * rpcrdma_mr objects are available.
1289 */
1290struct rpcrdma_mr *
1291rpcrdma_mr_get(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001292{
Chuck Lever346aa662015-05-26 11:52:06 -04001293 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Lever96cedde2017-12-14 20:57:55 -05001294 struct rpcrdma_mr *mr = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001295
Chuck Lever96cedde2017-12-14 20:57:55 -05001296 spin_lock(&buf->rb_mrlock);
1297 if (!list_empty(&buf->rb_mrs))
1298 mr = rpcrdma_mr_pop(&buf->rb_mrs);
1299 spin_unlock(&buf->rb_mrlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001300
Chuck Lever96cedde2017-12-14 20:57:55 -05001301 if (!mr)
1302 goto out_nomrs;
1303 return mr;
Chuck Levere2ac2362016-06-29 13:54:00 -04001304
Chuck Lever96cedde2017-12-14 20:57:55 -05001305out_nomrs:
Chuck Lever1c443eff2017-12-20 16:31:21 -05001306 trace_xprtrdma_nomrs(r_xprt);
Chuck Leverbebd0312017-04-11 13:23:10 -04001307 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1308 schedule_delayed_work(&buf->rb_refresh_worker, 0);
Chuck Levere2ac2362016-06-29 13:54:00 -04001309
1310 /* Allow the reply handler and refresh worker to run */
1311 cond_resched();
1312
1313 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001314}
1315
Chuck Leverec12e472017-12-14 20:58:04 -05001316static void
1317__rpcrdma_mr_put(struct rpcrdma_buffer *buf, struct rpcrdma_mr *mr)
1318{
1319 spin_lock(&buf->rb_mrlock);
1320 rpcrdma_mr_push(mr, &buf->rb_mrs);
1321 spin_unlock(&buf->rb_mrlock);
1322}
1323
Chuck Lever96cedde2017-12-14 20:57:55 -05001324/**
1325 * rpcrdma_mr_put - Release an rpcrdma_mr object
1326 * @mr: object to release
1327 *
1328 */
Chuck Lever346aa662015-05-26 11:52:06 -04001329void
Chuck Lever96cedde2017-12-14 20:57:55 -05001330rpcrdma_mr_put(struct rpcrdma_mr *mr)
Chuck Leverc2922c02014-07-29 17:24:36 -04001331{
Chuck Leverec12e472017-12-14 20:58:04 -05001332 __rpcrdma_mr_put(&mr->mr_xprt->rx_buf, mr);
1333}
Chuck Leverc2922c02014-07-29 17:24:36 -04001334
Chuck Leverec12e472017-12-14 20:58:04 -05001335/**
1336 * rpcrdma_mr_unmap_and_put - DMA unmap an MR and release it
1337 * @mr: object to release
1338 *
1339 */
1340void
1341rpcrdma_mr_unmap_and_put(struct rpcrdma_mr *mr)
1342{
1343 struct rpcrdma_xprt *r_xprt = mr->mr_xprt;
1344
Chuck Lever2937fed2017-12-20 16:31:12 -05001345 trace_xprtrdma_dma_unmap(mr);
Chuck Leverec12e472017-12-14 20:58:04 -05001346 ib_dma_unmap_sg(r_xprt->rx_ia.ri_device,
1347 mr->mr_sg, mr->mr_nents, mr->mr_dir);
1348 __rpcrdma_mr_put(&r_xprt->rx_buf, mr);
Chuck Leverc2922c02014-07-29 17:24:36 -04001349}
1350
Chuck Lever05c97462016-09-06 11:22:58 -04001351static struct rpcrdma_rep *
1352rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1353{
1354 /* If an RPC previously completed without a reply (say, a
1355 * credential problem or a soft timeout occurs) then hold off
1356 * on supplying more Receive buffers until the number of new
1357 * pending RPCs catches up to the number of posted Receives.
1358 */
1359 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1360 return NULL;
1361
1362 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1363 return NULL;
1364 buffers->rb_recv_count++;
1365 return rpcrdma_buffer_get_rep_locked(buffers);
1366}
1367
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001368/*
1369 * Get a set of request/reply buffers.
Chuck Lever78d506e2016-09-06 11:22:49 -04001370 *
1371 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001372 */
1373struct rpcrdma_req *
1374rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1375{
1376 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001377
Chuck Levera5b027e2015-10-24 17:27:27 -04001378 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001379 if (list_empty(&buffers->rb_send_bufs))
1380 goto out_reqbuf;
Chuck Lever05c97462016-09-06 11:22:58 -04001381 buffers->rb_send_count++;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001382 req = rpcrdma_buffer_get_req_locked(buffers);
Chuck Lever05c97462016-09-06 11:22:58 -04001383 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001384 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001385 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001386
Chuck Lever1e465fd2015-10-24 17:27:02 -04001387out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001388 spin_unlock(&buffers->rb_lock);
Chuck Lever78d506e2016-09-06 11:22:49 -04001389 pr_warn("RPC: %s: out of request buffers\n", __func__);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001390 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001391}
1392
1393/*
1394 * Put request/reply buffers back into pool.
1395 * Pre-decrement counter/array index.
1396 */
1397void
1398rpcrdma_buffer_put(struct rpcrdma_req *req)
1399{
1400 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001401 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001402
Chuck Lever1e465fd2015-10-24 17:27:02 -04001403 req->rl_reply = NULL;
1404
Chuck Levera5b027e2015-10-24 17:27:27 -04001405 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001406 buffers->rb_send_count--;
Chuck Levera80d66c2017-06-08 11:52:12 -04001407 list_add_tail(&req->rl_list, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001408 if (rep) {
1409 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001410 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001411 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001412 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001413}
1414
1415/*
1416 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001417 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001418 */
1419void
1420rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1421{
1422 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001423
Chuck Levera5b027e2015-10-24 17:27:27 -04001424 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001425 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001426 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001427}
1428
1429/*
1430 * Put reply buffers back into pool when not attached to
1431 * request. This happens in error conditions.
1432 */
1433void
1434rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1435{
Chuck Leverfed171b2015-05-26 11:51:37 -04001436 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001437
Chuck Levera5b027e2015-10-24 17:27:27 -04001438 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001439 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001440 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001441 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001442}
1443
Chuck Lever9128c3e2015-01-21 11:04:00 -05001444/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001445 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001446 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001447 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001448 * @flags: GFP flags
1449 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001450 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1451 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001452 *
1453 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001454 * receiving the payload of RDMA RECV operations. During Long Calls
1455 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001456 */
1457struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001458rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1459 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001460{
1461 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001462
Chuck Lever9128c3e2015-01-21 11:04:00 -05001463 rb = kmalloc(sizeof(*rb) + size, flags);
1464 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001465 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001466
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001467 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001468 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001469 rb->rg_iov.length = size;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001470
1471 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001472}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001473
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001474/**
1475 * __rpcrdma_map_regbuf - DMA-map a regbuf
1476 * @ia: controlling rpcrdma_ia
1477 * @rb: regbuf to be mapped
1478 */
1479bool
1480__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1481{
Chuck Lever91a10c52017-04-11 13:23:02 -04001482 struct ib_device *device = ia->ri_device;
1483
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001484 if (rb->rg_direction == DMA_NONE)
1485 return false;
1486
Chuck Lever91a10c52017-04-11 13:23:02 -04001487 rb->rg_iov.addr = ib_dma_map_single(device,
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001488 (void *)rb->rg_base,
1489 rdmab_length(rb),
1490 rb->rg_direction);
Chuck Lever91a10c52017-04-11 13:23:02 -04001491 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001492 return false;
1493
Chuck Lever91a10c52017-04-11 13:23:02 -04001494 rb->rg_device = device;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001495 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1496 return true;
1497}
1498
1499static void
1500rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1501{
1502 if (!rpcrdma_regbuf_is_mapped(rb))
1503 return;
1504
1505 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1506 rdmab_length(rb), rb->rg_direction);
1507 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001508}
1509
1510/**
1511 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001512 * @rb: regbuf to be deregistered and freed
1513 */
1514void
Chuck Lever13650c22016-09-15 10:56:26 -04001515rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001516{
Chuck Levere531dca2015-08-03 13:03:20 -04001517 if (!rb)
1518 return;
1519
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001520 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001521 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001522}
1523
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001524/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001525 * Prepost any receive buffer, then post send.
1526 *
1527 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1528 */
1529int
1530rpcrdma_ep_post(struct rpcrdma_ia *ia,
1531 struct rpcrdma_ep *ep,
1532 struct rpcrdma_req *req)
1533{
Chuck Leverae729502017-10-20 10:48:12 -04001534 struct ib_send_wr *send_wr = &req->rl_sendctx->sc_wr;
Chuck Lever90aab602016-09-15 10:56:43 -04001535 struct ib_send_wr *send_wr_fail;
Chuck Lever655fec62016-09-15 10:57:24 -04001536 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001537
Chuck Lever90aab602016-09-15 10:56:43 -04001538 if (req->rl_reply) {
1539 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001540 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001541 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001542 req->rl_reply = NULL;
1543 }
1544
Chuck Lever01bb35c2017-10-20 10:48:36 -04001545 if (!ep->rep_send_count ||
1546 test_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags)) {
Chuck Leverae729502017-10-20 10:48:12 -04001547 send_wr->send_flags |= IB_SEND_SIGNALED;
1548 ep->rep_send_count = ep->rep_send_batch;
1549 } else {
1550 send_wr->send_flags &= ~IB_SEND_SIGNALED;
1551 --ep->rep_send_count;
1552 }
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001553
Chuck Leverab03eff2017-12-20 16:30:40 -05001554 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
1555 trace_xprtrdma_post_send(req, rc);
1556 if (rc)
1557 return -ENOTCONN;
1558 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001559}
1560
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001561int
1562rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001563 struct rpcrdma_rep *rep)
1564{
Chuck Lever6ea8e712016-09-15 10:56:51 -04001565 struct ib_recv_wr *recv_wr_fail;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001566 int rc;
1567
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001568 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1569 goto out_map;
Chuck Lever6ea8e712016-09-15 10:56:51 -04001570 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
Chuck Leverb4a7f912017-12-20 16:30:48 -05001571 trace_xprtrdma_post_recv(rep, rc);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001572 if (rc)
Chuck Leverb4a7f912017-12-20 16:30:48 -05001573 return -ENOTCONN;
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001574 return 0;
1575
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001576out_map:
1577 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1578 return -EIO;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001579}
Chuck Lever43e95982014-07-29 17:23:34 -04001580
Chuck Leverf531a5d2015-10-24 17:27:43 -04001581/**
1582 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1583 * @r_xprt: transport associated with these backchannel resources
1584 * @min_reqs: minimum number of incoming requests expected
1585 *
1586 * Returns zero if all requested buffers were posted, or a negative errno.
1587 */
1588int
1589rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1590{
1591 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1592 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001593 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001594 int rc;
1595
1596 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001597 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001598 if (list_empty(&buffers->rb_recv_bufs))
1599 goto out_reqbuf;
1600 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001601 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001602
Chuck Leverb1573802016-09-15 10:56:35 -04001603 rc = rpcrdma_ep_post_recv(ia, rep);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001604 if (rc)
1605 goto out_rc;
1606 }
1607
1608 return 0;
1609
1610out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001611 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001612 pr_warn("%s: no extra receive buffers\n", __func__);
1613 return -ENOMEM;
1614
1615out_rc:
1616 rpcrdma_recv_buffer_put(rep);
1617 return rc;
1618}