blob: e4171f2abe37d966b82b9a858300843027e7224c [file] [log] [blame]
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -04001/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04002 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the BSD-type
8 * license below:
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
21 *
22 * Neither the name of the Network Appliance, Inc. nor the names of
23 * its contributors may be used to endorse or promote products
24 * derived from this software without specific prior written
25 * permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040038 */
39
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040040/*
41 * verbs.c
42 *
43 * Encapsulates the major functions managing:
44 * o adapters
45 * o endpoints
46 * o connections
47 * o buffer memory
48 */
49
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000050#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Chuck Levereba8ff62015-01-21 11:03:02 -050052#include <linux/prefetch.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 Lever65866f82014-05-28 10:33:59 -040055#include <asm/bitops.h>
Chuck Lever56a6bd12017-04-11 13:23:34 -040056
Chuck Lever0a904872017-02-08 17:00:35 -050057#include <rdma/ib_cm.h>
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040058
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040059#include "xprt_rdma.h"
60
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040061/*
62 * Globals/Macros
63 */
64
Jeff Laytonf895b252014-11-17 16:58:04 -050065#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040066# define RPCDBG_FACILITY RPCDBG_TRANS
67#endif
68
69/*
70 * internal functions
71 */
Chuck Levera9b0e382017-04-11 13:23:26 -040072static void rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt);
Chuck Leverbebd0312017-04-11 13:23:10 -040073static void rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf);
74static void rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040075
Chuck Lever7d7fa9b2017-04-11 13:23:43 -040076static struct workqueue_struct *rpcrdma_receive_wq __read_mostly;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040077
Chuck Leverfe97b472015-10-24 17:27:10 -040078int
79rpcrdma_alloc_wq(void)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040080{
Chuck Leverfe97b472015-10-24 17:27:10 -040081 struct workqueue_struct *recv_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040082
Chuck Leverfe97b472015-10-24 17:27:10 -040083 recv_wq = alloc_workqueue("xprtrdma_receive",
84 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI,
85 0);
86 if (!recv_wq)
87 return -ENOMEM;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040088
Chuck Leverfe97b472015-10-24 17:27:10 -040089 rpcrdma_receive_wq = recv_wq;
90 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040091}
92
Chuck Leverfe97b472015-10-24 17:27:10 -040093void
94rpcrdma_destroy_wq(void)
Chuck Leverf1a03b72014-11-08 20:14:37 -050095{
Chuck Leverfe97b472015-10-24 17:27:10 -040096 struct workqueue_struct *wq;
Chuck Leverf1a03b72014-11-08 20:14:37 -050097
Chuck Leverfe97b472015-10-24 17:27:10 -040098 if (rpcrdma_receive_wq) {
99 wq = rpcrdma_receive_wq;
100 rpcrdma_receive_wq = NULL;
101 destroy_workqueue(wq);
102 }
Chuck Leverf1a03b72014-11-08 20:14:37 -0500103}
104
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400105static void
106rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
107{
108 struct rpcrdma_ep *ep = context;
109
Chuck Lever2f6922c2016-11-29 10:53:21 -0500110 pr_err("rpcrdma: %s on device %s ep %p\n",
111 ib_event_msg(event->event), event->device->name, context);
112
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400113 if (ep->rep_connected == 1) {
114 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500115 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400116 wake_up_all(&ep->rep_connect_wait);
117 }
118}
119
Chuck Lever2fa8f882016-03-04 11:28:53 -0500120/**
121 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
122 * @cq: completion queue (ignored)
123 * @wc: completed WR
124 *
Chuck Lever4220a072015-10-24 17:26:45 -0400125 */
126static void
Chuck Lever2fa8f882016-03-04 11:28:53 -0500127rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400128{
Chuck Lever2fa8f882016-03-04 11:28:53 -0500129 /* WARNING: Only wr_cqe and status are reliable at this point */
130 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
131 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
132 ib_wc_status_msg(wc->status),
133 wc->status, wc->vendor_err);
Chuck Leverfc664482014-05-28 10:33:25 -0400134}
135
Chuck Lever23826c72016-03-04 11:28:27 -0500136/* Perform basic sanity checking to avoid using garbage
137 * to update the credit grant value.
138 */
139static void
140rpcrdma_update_granted_credits(struct rpcrdma_rep *rep)
141{
142 struct rpcrdma_msg *rmsgp = rdmab_to_msg(rep->rr_rdmabuf);
143 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
144 u32 credits;
145
146 if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
147 return;
148
149 credits = be32_to_cpu(rmsgp->rm_credit);
150 if (credits == 0)
151 credits = 1; /* don't deadlock */
152 else if (credits > buffer->rb_max_requests)
153 credits = buffer->rb_max_requests;
154
155 atomic_set(&buffer->rb_credits, credits);
156}
157
Chuck Lever552bf222016-03-04 11:28:36 -0500158/**
Chuck Lever1519e962016-09-15 10:57:49 -0400159 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
Chuck Lever552bf222016-03-04 11:28:36 -0500160 * @cq: completion queue (ignored)
161 * @wc: completed WR
162 *
163 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400164static void
Chuck Lever1519e962016-09-15 10:57:49 -0400165rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400166{
Chuck Lever552bf222016-03-04 11:28:36 -0500167 struct ib_cqe *cqe = wc->wr_cqe;
168 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
169 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400170
Chuck Lever85024272015-01-21 11:02:04 -0500171 /* WARNING: Only wr_id and status are reliable at this point */
172 if (wc->status != IB_WC_SUCCESS)
173 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400174
Chuck Lever85024272015-01-21 11:02:04 -0500175 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400176 if (wc->opcode != IB_WC_RECV)
177 return;
178
Chuck Lever85024272015-01-21 11:02:04 -0500179 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
180 __func__, rep, wc->byte_len);
181
Chuck Leverfc664482014-05-28 10:33:25 -0400182 rep->rr_len = wc->byte_len;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400183 rep->rr_wc_flags = wc->wc_flags;
184 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
185
Chuck Lever91a10c52017-04-11 13:23:02 -0400186 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
Chuck Lever6b1184c2015-01-21 11:04:25 -0500187 rdmab_addr(rep->rr_rdmabuf),
188 rep->rr_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500189
190 rpcrdma_update_granted_credits(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400191
192out_schedule:
Chuck Leverfe97b472015-10-24 17:27:10 -0400193 queue_work(rpcrdma_receive_wq, &rep->rr_work);
Chuck Lever85024272015-01-21 11:02:04 -0500194 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400195
Chuck Lever85024272015-01-21 11:02:04 -0500196out_fail:
197 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500198 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
199 ib_wc_status_msg(wc->status),
200 wc->status, wc->vendor_err);
Chuck Leverb0e178a2015-10-24 17:26:54 -0400201 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500202 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400203}
204
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400205static void
206rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
207 struct rdma_conn_param *param)
208{
209 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
210 const struct rpcrdma_connect_private *pmsg = param->private_data;
211 unsigned int rsize, wsize;
212
Chuck Leverc8b920b2016-09-15 10:57:16 -0400213 /* Default settings for RPC-over-RDMA Version One */
214 r_xprt->rx_ia.ri_reminv_expected = false;
Chuck Leverb5f0afb2017-02-08 16:59:54 -0500215 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400216 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
217 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
218
219 if (pmsg &&
220 pmsg->cp_magic == rpcrdma_cmp_magic &&
221 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
Chuck Leverc8b920b2016-09-15 10:57:16 -0400222 r_xprt->rx_ia.ri_reminv_expected = true;
Chuck Leverc95a3c62017-02-08 17:00:02 -0500223 r_xprt->rx_ia.ri_implicit_roundup = true;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400224 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
225 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
226 }
227
228 if (rsize < cdata->inline_rsize)
229 cdata->inline_rsize = rsize;
230 if (wsize < cdata->inline_wsize)
231 cdata->inline_wsize = wsize;
Chuck Lever6d6bf722016-11-29 10:53:13 -0500232 dprintk("RPC: %s: max send %u, max recv %u\n",
233 __func__, cdata->inline_wsize, cdata->inline_rsize);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400234 rpcrdma_set_max_header_sizes(r_xprt);
235}
236
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400237static int
238rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
239{
240 struct rpcrdma_xprt *xprt = id->context;
241 struct rpcrdma_ia *ia = &xprt->rx_ia;
242 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500243#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400244 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800245#endif
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400246 int connstate = 0;
247
248 switch (event->event) {
249 case RDMA_CM_EVENT_ADDR_RESOLVED:
250 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400251 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400252 complete(&ia->ri_done);
253 break;
254 case RDMA_CM_EVENT_ADDR_ERROR:
255 ia->ri_async_rc = -EHOSTUNREACH;
256 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
257 __func__, ep);
258 complete(&ia->ri_done);
259 break;
260 case RDMA_CM_EVENT_ROUTE_ERROR:
261 ia->ri_async_rc = -ENETUNREACH;
262 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
263 __func__, ep);
264 complete(&ia->ri_done);
265 break;
Chuck Leverbebd0312017-04-11 13:23:10 -0400266 case RDMA_CM_EVENT_DEVICE_REMOVAL:
267#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever173b8f42017-06-08 11:53:00 -0400268 pr_info("rpcrdma: removing device %s for %pIS:%u\n",
269 ia->ri_device->name,
Chuck Leverbebd0312017-04-11 13:23:10 -0400270 sap, rpc_get_port(sap));
271#endif
272 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
273 ep->rep_connected = -ENODEV;
274 xprt_force_disconnect(&xprt->rx_xprt);
275 wait_for_completion(&ia->ri_remove_done);
276
277 ia->ri_id = NULL;
278 ia->ri_pd = NULL;
279 ia->ri_device = NULL;
280 /* Return 1 to ensure the core destroys the id. */
281 return 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400282 case RDMA_CM_EVENT_ESTABLISHED:
283 connstate = 1;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400284 rpcrdma_update_connect_private(xprt, &event->param.conn);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400285 goto connected;
286 case RDMA_CM_EVENT_CONNECT_ERROR:
287 connstate = -ENOTCONN;
288 goto connected;
289 case RDMA_CM_EVENT_UNREACHABLE:
290 connstate = -ENETDOWN;
291 goto connected;
292 case RDMA_CM_EVENT_REJECTED:
Chuck Lever173b8f42017-06-08 11:53:00 -0400293 dprintk("rpcrdma: connection to %pIS:%u rejected: %s\n",
294 sap, rpc_get_port(sap),
Chuck Lever0a904872017-02-08 17:00:35 -0500295 rdma_reject_msg(id, event->status));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400296 connstate = -ECONNREFUSED;
Chuck Lever0a904872017-02-08 17:00:35 -0500297 if (event->status == IB_CM_REJ_STALE_CONN)
298 connstate = -EAGAIN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400299 goto connected;
300 case RDMA_CM_EVENT_DISCONNECTED:
301 connstate = -ECONNABORTED;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400302connected:
Chuck Lever23826c72016-03-04 11:28:27 -0500303 atomic_set(&xprt->rx_buf.rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400304 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500305 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400306 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400307 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400308 default:
Chuck Lever173b8f42017-06-08 11:53:00 -0400309 dprintk("RPC: %s: %pIS:%u on %s/%s (ep 0x%p): %s\n",
310 __func__, sap, rpc_get_port(sap),
311 ia->ri_device->name, ia->ri_ops->ro_displayname,
312 ep, rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400313 break;
314 }
315
316 return 0;
317}
318
319static struct rdma_cm_id *
320rpcrdma_create_id(struct rpcrdma_xprt *xprt,
321 struct rpcrdma_ia *ia, struct sockaddr *addr)
322{
Chuck Lever109b88a2016-11-29 10:52:40 -0500323 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400324 struct rdma_cm_id *id;
325 int rc;
326
Tom Talpey1a954052008-10-09 15:01:31 -0400327 init_completion(&ia->ri_done);
Chuck Leverbebd0312017-04-11 13:23:10 -0400328 init_completion(&ia->ri_remove_done);
Tom Talpey1a954052008-10-09 15:01:31 -0400329
Guy Shapirofa201052015-10-22 15:20:10 +0300330 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
331 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400332 if (IS_ERR(id)) {
333 rc = PTR_ERR(id);
334 dprintk("RPC: %s: rdma_create_id() failed %i\n",
335 __func__, rc);
336 return id;
337 }
338
Tom Talpey5675add2008-10-09 15:01:41 -0400339 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400340 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
341 if (rc) {
342 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
343 __func__, rc);
344 goto out;
345 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500346 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
347 if (rc < 0) {
348 dprintk("RPC: %s: wait() exited: %i\n",
349 __func__, rc);
350 goto out;
351 }
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400352
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400353 rc = ia->ri_async_rc;
354 if (rc)
355 goto out;
356
Tom Talpey5675add2008-10-09 15:01:41 -0400357 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400358 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
359 if (rc) {
360 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
361 __func__, rc);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400362 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400363 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500364 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
365 if (rc < 0) {
366 dprintk("RPC: %s: wait() exited: %i\n",
367 __func__, rc);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400368 goto out;
Chuck Lever109b88a2016-11-29 10:52:40 -0500369 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400370 rc = ia->ri_async_rc;
371 if (rc)
Chuck Lever56a6bd12017-04-11 13:23:34 -0400372 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400373
374 return id;
Chuck Lever56a6bd12017-04-11 13:23:34 -0400375
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400376out:
377 rdma_destroy_id(id);
378 return ERR_PTR(rc);
379}
380
381/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400382 * Exported functions.
383 */
384
Chuck Leverfff09592017-04-11 13:22:54 -0400385/**
386 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
387 * @xprt: controlling transport
388 * @addr: IP address of remote peer
389 *
390 * Returns 0 on success, negative errno if an appropriate
391 * Interface Adapter could not be found and opened.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400392 */
393int
Chuck Leverfff09592017-04-11 13:22:54 -0400394rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400395{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400396 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400397 int rc;
398
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400399 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
400 if (IS_ERR(ia->ri_id)) {
401 rc = PTR_ERR(ia->ri_id);
Chuck Leverfff09592017-04-11 13:22:54 -0400402 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400403 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400404 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400405
Christoph Hellwiged082d32016-09-05 12:56:17 +0200406 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400407 if (IS_ERR(ia->ri_pd)) {
408 rc = PTR_ERR(ia->ri_pd);
Chuck Leverb54054c2016-06-29 13:53:27 -0400409 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
Chuck Leverfff09592017-04-11 13:22:54 -0400410 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400411 }
412
Chuck Leverfff09592017-04-11 13:22:54 -0400413 switch (xprt_rdma_memreg_strategy) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400414 case RPCRDMA_FRMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400415 if (frwr_is_supported(ia)) {
416 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
417 break;
418 }
419 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400420 case RPCRDMA_MTHCAFMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400421 if (fmr_is_supported(ia)) {
422 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
423 break;
424 }
425 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400426 default:
Chuck Leverfff09592017-04-11 13:22:54 -0400427 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
428 ia->ri_device->name, xprt_rdma_memreg_strategy);
Chuck Leverb54054c2016-06-29 13:53:27 -0400429 rc = -EINVAL;
Chuck Leverfff09592017-04-11 13:22:54 -0400430 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400431 }
432
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400433 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500434
Chuck Leverfff09592017-04-11 13:22:54 -0400435out_err:
436 rpcrdma_ia_close(ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400437 return rc;
438}
439
Chuck Leverfff09592017-04-11 13:22:54 -0400440/**
Chuck Leverbebd0312017-04-11 13:23:10 -0400441 * rpcrdma_ia_remove - Handle device driver unload
442 * @ia: interface adapter being removed
443 *
444 * Divest transport H/W resources associated with this adapter,
445 * but allow it to be restored later.
446 */
447void
448rpcrdma_ia_remove(struct rpcrdma_ia *ia)
449{
450 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
451 rx_ia);
452 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
453 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
454 struct rpcrdma_req *req;
455 struct rpcrdma_rep *rep;
456
457 cancel_delayed_work_sync(&buf->rb_refresh_worker);
458
459 /* This is similar to rpcrdma_ep_destroy, but:
460 * - Don't cancel the connect worker.
461 * - Don't call rpcrdma_ep_disconnect, which waits
462 * for another conn upcall, which will deadlock.
463 * - rdma_disconnect is unneeded, the underlying
464 * connection is already gone.
465 */
466 if (ia->ri_id->qp) {
467 ib_drain_qp(ia->ri_id->qp);
468 rdma_destroy_qp(ia->ri_id);
469 ia->ri_id->qp = NULL;
470 }
471 ib_free_cq(ep->rep_attr.recv_cq);
472 ib_free_cq(ep->rep_attr.send_cq);
473
474 /* The ULP is responsible for ensuring all DMA
475 * mappings and MRs are gone.
476 */
477 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
478 rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
479 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
480 rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
481 rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
482 rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
483 }
484 rpcrdma_destroy_mrs(buf);
485
486 /* Allow waiters to continue */
487 complete(&ia->ri_remove_done);
488}
489
490/**
Chuck Leverfff09592017-04-11 13:22:54 -0400491 * rpcrdma_ia_close - Clean up/close an IA.
492 * @ia: interface adapter to close
493 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400494 */
495void
496rpcrdma_ia_close(struct rpcrdma_ia *ia)
497{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400498 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400499 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
500 if (ia->ri_id->qp)
501 rdma_destroy_qp(ia->ri_id);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400502 rdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400503 }
Chuck Leverfff09592017-04-11 13:22:54 -0400504 ia->ri_id = NULL;
505 ia->ri_device = NULL;
Chuck Lever6d446982015-05-26 11:51:27 -0400506
507 /* If the pd is still busy, xprtrdma missed freeing a resource */
508 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600509 ib_dealloc_pd(ia->ri_pd);
Chuck Leverfff09592017-04-11 13:22:54 -0400510 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400511}
512
513/*
514 * Create unconnected endpoint.
515 */
516int
517rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
Chuck Lever16f906d2017-02-08 17:00:10 -0500518 struct rpcrdma_create_data_internal *cdata)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400519{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400520 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
Chuck Lever16f906d2017-02-08 17:00:10 -0500521 unsigned int max_qp_wr, max_sge;
Chuck Leverfc664482014-05-28 10:33:25 -0400522 struct ib_cq *sendcq, *recvcq;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500523 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400524
Chuck Levereed50872017-03-11 15:52:47 -0500525 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
526 RPCRDMA_MAX_SEND_SGES);
Chuck Lever16f906d2017-02-08 17:00:10 -0500527 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
528 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
Chuck Leverb3221d62015-08-03 13:03:39 -0400529 return -ENOMEM;
530 }
Chuck Lever16f906d2017-02-08 17:00:10 -0500531 ia->ri_max_send_sges = max_sge - RPCRDMA_MIN_SEND_SGES;
Chuck Leverb3221d62015-08-03 13:03:39 -0400532
Or Gerlitze3e45b12015-12-18 10:59:48 +0200533 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400534 dprintk("RPC: %s: insufficient wqe's available\n",
535 __func__);
536 return -ENOMEM;
537 }
Chuck Lever550d7502016-05-02 14:41:47 -0400538 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
Chuck Lever124fa172015-10-24 17:27:51 -0400539
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400540 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400541 if (cdata->max_requests > max_qp_wr)
542 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400543
544 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
545 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400546 ep->rep_attr.srq = NULL;
547 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400548 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400549 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
Chuck Lever3968cb52015-03-30 14:35:26 -0400550 rc = ia->ri_ops->ro_open(ia, ep, cdata);
551 if (rc)
552 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400553 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400554 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400555 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
Chuck Lever16f906d2017-02-08 17:00:10 -0500556 ep->rep_attr.cap.max_send_sge = max_sge;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400557 ep->rep_attr.cap.max_recv_sge = 1;
558 ep->rep_attr.cap.max_inline_data = 0;
559 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
560 ep->rep_attr.qp_type = IB_QPT_RC;
561 ep->rep_attr.port_num = ~0;
562
563 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
564 "iovs: send %d recv %d\n",
565 __func__,
566 ep->rep_attr.cap.max_send_wr,
567 ep->rep_attr.cap.max_recv_wr,
568 ep->rep_attr.cap.max_send_sge,
569 ep->rep_attr.cap.max_recv_sge);
570
571 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400572 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500573 if (ep->rep_cqinit <= 2)
574 ep->rep_cqinit = 0; /* always signal? */
Chuck Lever8d38de62016-11-29 10:52:16 -0500575 rpcrdma_init_cqcount(ep, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400576 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400577 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400578
Chuck Lever2fa8f882016-03-04 11:28:53 -0500579 sendcq = ib_alloc_cq(ia->ri_device, NULL,
580 ep->rep_attr.cap.max_send_wr + 1,
581 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400582 if (IS_ERR(sendcq)) {
583 rc = PTR_ERR(sendcq);
584 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400585 __func__, rc);
586 goto out1;
587 }
588
Chuck Lever552bf222016-03-04 11:28:36 -0500589 recvcq = ib_alloc_cq(ia->ri_device, NULL,
590 ep->rep_attr.cap.max_recv_wr + 1,
591 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400592 if (IS_ERR(recvcq)) {
593 rc = PTR_ERR(recvcq);
594 dprintk("RPC: %s: failed to create recv CQ: %i\n",
595 __func__, rc);
596 goto out2;
597 }
598
Chuck Leverfc664482014-05-28 10:33:25 -0400599 ep->rep_attr.send_cq = sendcq;
600 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400601
602 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400603 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400604
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400605 /* Prepare RDMA-CM private message */
606 pmsg->cp_magic = rpcrdma_cmp_magic;
607 pmsg->cp_version = RPCRDMA_CMP_VERSION;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400608 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400609 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
610 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
611 ep->rep_remote_cma.private_data = pmsg;
612 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400613
614 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400615 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200616 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400617 ep->rep_remote_cma.responder_resources = 32;
618 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500619 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200620 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400621
Chuck Leverb2dde942016-05-02 14:43:03 -0400622 /* Limit transport retries so client can detect server
623 * GID changes quickly. RPC layer handles re-establishing
624 * transport connection and retransmission.
625 */
626 ep->rep_remote_cma.retry_count = 6;
627
628 /* RPC-over-RDMA handles its own flow control. In addition,
629 * make all RNR NAKs visible so we know that RPC-over-RDMA
630 * flow control is working correctly (no NAKs should be seen).
631 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400632 ep->rep_remote_cma.flow_control = 0;
633 ep->rep_remote_cma.rnr_retry_count = 0;
634
635 return 0;
636
637out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500638 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400639out1:
640 return rc;
641}
642
643/*
644 * rpcrdma_ep_destroy
645 *
646 * Disconnect and destroy endpoint. After this, the only
647 * valid operations on the ep are to free it (if dynamically
648 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400649 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400650void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400651rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
652{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400653 dprintk("RPC: %s: entering, connected is %d\n",
654 __func__, ep->rep_connected);
655
Chuck Lever254f91e2014-05-28 10:32:17 -0400656 cancel_delayed_work_sync(&ep->rep_connect_worker);
657
Steve Wise72c02172015-09-21 12:24:23 -0500658 if (ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400659 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400660 rdma_destroy_qp(ia->ri_id);
661 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400662 }
663
Chuck Lever552bf222016-03-04 11:28:36 -0500664 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500665 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400666}
667
Chuck Levera9b0e382017-04-11 13:23:26 -0400668/* Re-establish a connection after a device removal event.
669 * Unlike a normal reconnection, a fresh PD and a new set
670 * of MRs and buffers is needed.
671 */
672static int
673rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
674 struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
675{
676 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
677 int rc, err;
678
679 pr_info("%s: r_xprt = %p\n", __func__, r_xprt);
680
681 rc = -EHOSTUNREACH;
682 if (rpcrdma_ia_open(r_xprt, sap))
683 goto out1;
684
685 rc = -ENOMEM;
686 err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
687 if (err) {
688 pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
689 goto out2;
690 }
691
692 rc = -ENETUNREACH;
693 err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
694 if (err) {
695 pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
696 goto out3;
697 }
698
699 rpcrdma_create_mrs(r_xprt);
700 return 0;
701
702out3:
703 rpcrdma_ep_destroy(ep, ia);
704out2:
705 rpcrdma_ia_close(ia);
706out1:
707 return rc;
708}
709
Chuck Lever18908962017-04-11 13:23:18 -0400710static int
711rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
712 struct rpcrdma_ia *ia)
713{
714 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
715 struct rdma_cm_id *id, *old;
716 int err, rc;
717
718 dprintk("RPC: %s: reconnecting...\n", __func__);
719
720 rpcrdma_ep_disconnect(ep, ia);
721
722 rc = -EHOSTUNREACH;
723 id = rpcrdma_create_id(r_xprt, ia, sap);
724 if (IS_ERR(id))
725 goto out;
726
727 /* As long as the new ID points to the same device as the
728 * old ID, we can reuse the transport's existing PD and all
729 * previously allocated MRs. Also, the same device means
730 * the transport's previous DMA mappings are still valid.
731 *
732 * This is a sanity check only. There should be no way these
733 * point to two different devices here.
734 */
735 old = id;
736 rc = -ENETUNREACH;
737 if (ia->ri_device != id->device) {
738 pr_err("rpcrdma: can't reconnect on different device!\n");
739 goto out_destroy;
740 }
741
742 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
743 if (err) {
744 dprintk("RPC: %s: rdma_create_qp returned %d\n",
745 __func__, err);
746 goto out_destroy;
747 }
748
749 /* Atomically replace the transport's ID and QP. */
750 rc = 0;
751 old = ia->ri_id;
752 ia->ri_id = id;
753 rdma_destroy_qp(old);
754
755out_destroy:
Chuck Lever56a6bd12017-04-11 13:23:34 -0400756 rdma_destroy_id(old);
Chuck Lever18908962017-04-11 13:23:18 -0400757out:
758 return rc;
759}
760
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400761/*
762 * Connect unconnected endpoint.
763 */
764int
765rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
766{
Chuck Lever0a904872017-02-08 17:00:35 -0500767 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
768 rx_ia);
Chuck Lever0a904872017-02-08 17:00:35 -0500769 unsigned int extras;
Chuck Lever18908962017-04-11 13:23:18 -0400770 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400771
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400772retry:
Chuck Lever18908962017-04-11 13:23:18 -0400773 switch (ep->rep_connected) {
774 case 0:
Chuck Leverec62f402014-05-28 10:34:07 -0400775 dprintk("RPC: %s: connecting...\n", __func__);
776 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
777 if (rc) {
778 dprintk("RPC: %s: rdma_create_qp failed %i\n",
779 __func__, rc);
Chuck Lever18908962017-04-11 13:23:18 -0400780 rc = -ENETUNREACH;
781 goto out_noupdate;
Chuck Leverec62f402014-05-28 10:34:07 -0400782 }
Chuck Lever18908962017-04-11 13:23:18 -0400783 break;
Chuck Levera9b0e382017-04-11 13:23:26 -0400784 case -ENODEV:
785 rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
786 if (rc)
787 goto out_noupdate;
788 break;
Chuck Lever18908962017-04-11 13:23:18 -0400789 default:
790 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
791 if (rc)
792 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400793 }
794
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400795 ep->rep_connected = 0;
796
797 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
798 if (rc) {
799 dprintk("RPC: %s: rdma_connect() failed with %i\n",
800 __func__, rc);
801 goto out;
802 }
803
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400804 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400805 if (ep->rep_connected <= 0) {
Chuck Lever0a904872017-02-08 17:00:35 -0500806 if (ep->rep_connected == -EAGAIN)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400807 goto retry;
808 rc = ep->rep_connected;
Chuck Lever0a904872017-02-08 17:00:35 -0500809 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400810 }
811
Chuck Lever0a904872017-02-08 17:00:35 -0500812 dprintk("RPC: %s: connected\n", __func__);
813 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
814 if (extras)
815 rpcrdma_ep_post_extra_recv(r_xprt, extras);
816
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400817out:
818 if (rc)
819 ep->rep_connected = rc;
Chuck Lever18908962017-04-11 13:23:18 -0400820
821out_noupdate:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400822 return rc;
823}
824
825/*
826 * rpcrdma_ep_disconnect
827 *
828 * This is separate from destroy to facilitate the ability
829 * to reconnect without recreating the endpoint.
830 *
831 * This call is not reentrant, and must not be made in parallel
832 * on the same endpoint.
833 */
Chuck Lever282191c2014-07-29 17:25:55 -0400834void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400835rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
836{
837 int rc;
838
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400839 rc = rdma_disconnect(ia->ri_id);
840 if (!rc) {
841 /* returns without wait if not connected */
842 wait_event_interruptible(ep->rep_connect_wait,
843 ep->rep_connected != 1);
844 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
845 (ep->rep_connected == 1) ? "still " : "dis");
846 } else {
847 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
848 ep->rep_connected = rc;
849 }
Chuck Lever550d7502016-05-02 14:41:47 -0400850
851 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400852}
853
Chuck Lever505bbe62016-06-29 13:52:54 -0400854static void
855rpcrdma_mr_recovery_worker(struct work_struct *work)
856{
857 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
858 rb_recovery_worker.work);
859 struct rpcrdma_mw *mw;
860
861 spin_lock(&buf->rb_recovery_lock);
862 while (!list_empty(&buf->rb_stale_mrs)) {
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500863 mw = rpcrdma_pop_mw(&buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400864 spin_unlock(&buf->rb_recovery_lock);
865
866 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
867 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
868
869 spin_lock(&buf->rb_recovery_lock);
kbuild test robot53d78522016-07-16 06:02:05 +0800870 }
Chuck Lever505bbe62016-06-29 13:52:54 -0400871 spin_unlock(&buf->rb_recovery_lock);
872}
873
874void
875rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
876{
877 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
878 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
879
880 spin_lock(&buf->rb_recovery_lock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500881 rpcrdma_push_mw(mw, &buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400882 spin_unlock(&buf->rb_recovery_lock);
883
884 schedule_delayed_work(&buf->rb_recovery_worker, 0);
885}
886
Chuck Levere2ac2362016-06-29 13:54:00 -0400887static void
888rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
889{
890 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
891 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
892 unsigned int count;
893 LIST_HEAD(free);
894 LIST_HEAD(all);
895
896 for (count = 0; count < 32; count++) {
897 struct rpcrdma_mw *mw;
898 int rc;
899
900 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
901 if (!mw)
902 break;
903
904 rc = ia->ri_ops->ro_init_mr(ia, mw);
905 if (rc) {
906 kfree(mw);
907 break;
908 }
909
910 mw->mw_xprt = r_xprt;
911
912 list_add(&mw->mw_list, &free);
913 list_add(&mw->mw_all, &all);
914 }
915
916 spin_lock(&buf->rb_mwlock);
917 list_splice(&free, &buf->rb_mws);
918 list_splice(&all, &buf->rb_all);
919 r_xprt->rx_stats.mrs_allocated += count;
920 spin_unlock(&buf->rb_mwlock);
921
922 dprintk("RPC: %s: created %u MRs\n", __func__, count);
923}
924
925static void
926rpcrdma_mr_refresh_worker(struct work_struct *work)
927{
928 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
929 rb_refresh_worker.work);
930 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
931 rx_buf);
932
933 rpcrdma_create_mrs(r_xprt);
934}
935
Chuck Leverf531a5d2015-10-24 17:27:43 -0400936struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500937rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
938{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400939 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500940 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500941
Chuck Lever85275c82015-01-21 11:04:16 -0500942 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500943 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500944 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500945
Chuck Leverf531a5d2015-10-24 17:27:43 -0400946 spin_lock(&buffer->rb_reqslock);
947 list_add(&req->rl_all, &buffer->rb_allreqs);
948 spin_unlock(&buffer->rb_reqslock);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500949 req->rl_cqe.done = rpcrdma_wc_send;
Chuck Lever13924022015-01-21 11:03:52 -0500950 req->rl_buffer = &r_xprt->rx_buf;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400951 INIT_LIST_HEAD(&req->rl_registered);
Chuck Lever90aab602016-09-15 10:56:43 -0400952 req->rl_send_wr.next = NULL;
953 req->rl_send_wr.wr_cqe = &req->rl_cqe;
Chuck Lever655fec62016-09-15 10:57:24 -0400954 req->rl_send_wr.sg_list = req->rl_send_sge;
Chuck Lever90aab602016-09-15 10:56:43 -0400955 req->rl_send_wr.opcode = IB_WR_SEND;
Chuck Lever13924022015-01-21 11:03:52 -0500956 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500957}
958
Chuck Leverf531a5d2015-10-24 17:27:43 -0400959struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -0500960rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
961{
962 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500963 struct rpcrdma_rep *rep;
964 int rc;
965
966 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500967 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500968 if (rep == NULL)
969 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500970
Chuck Lever13650c22016-09-15 10:56:26 -0400971 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -0400972 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever6b1184c2015-01-21 11:04:25 -0500973 if (IS_ERR(rep->rr_rdmabuf)) {
974 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500975 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500976 }
Chuck Lever13924022015-01-21 11:03:52 -0500977
Chuck Lever1519e962016-09-15 10:57:49 -0400978 rep->rr_cqe.done = rpcrdma_wc_receive;
Chuck Leverfed171b2015-05-26 11:51:37 -0400979 rep->rr_rxprt = r_xprt;
Chuck Lever496b77a2016-09-15 10:57:57 -0400980 INIT_WORK(&rep->rr_work, rpcrdma_reply_handler);
Chuck Lever6ea8e712016-09-15 10:56:51 -0400981 rep->rr_recv_wr.next = NULL;
982 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
983 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
984 rep->rr_recv_wr.num_sge = 1;
Chuck Lever13924022015-01-21 11:03:52 -0500985 return rep;
986
987out_free:
988 kfree(rep);
989out:
990 return ERR_PTR(rc);
991}
992
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400993int
Chuck Leverac920d02015-01-21 11:03:44 -0500994rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400995{
Chuck Leverac920d02015-01-21 11:03:44 -0500996 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400997 int i, rc;
998
Chuck Lever1e465fd2015-10-24 17:27:02 -0400999 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001000 buf->rb_bc_srv_max_requests = 0;
Chuck Lever23826c72016-03-04 11:28:27 -05001001 atomic_set(&buf->rb_credits, 1);
Chuck Levere2ac2362016-06-29 13:54:00 -04001002 spin_lock_init(&buf->rb_mwlock);
Chuck Lever505bbe62016-06-29 13:52:54 -04001003 spin_lock_init(&buf->rb_lock);
1004 spin_lock_init(&buf->rb_recovery_lock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001005 INIT_LIST_HEAD(&buf->rb_mws);
1006 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever431af642017-06-08 11:52:20 -04001007 INIT_LIST_HEAD(&buf->rb_pending);
Chuck Lever505bbe62016-06-29 13:52:54 -04001008 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001009 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1010 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001011 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
1012 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001013
Chuck Levere2ac2362016-06-29 13:54:00 -04001014 rpcrdma_create_mrs(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001015
Chuck Lever1e465fd2015-10-24 17:27:02 -04001016 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001017 INIT_LIST_HEAD(&buf->rb_allreqs);
1018 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001019 for (i = 0; i < buf->rb_max_requests; i++) {
1020 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001021
Chuck Lever13924022015-01-21 11:03:52 -05001022 req = rpcrdma_create_req(r_xprt);
1023 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001024 dprintk("RPC: %s: request buffer %d alloc"
1025 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001026 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001027 goto out;
1028 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001029 req->rl_backchannel = false;
Chuck Levera80d66c2017-06-08 11:52:12 -04001030 list_add(&req->rl_list, &buf->rb_send_bufs);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001031 }
1032
1033 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001034 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001035 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001036
Chuck Lever13924022015-01-21 11:03:52 -05001037 rep = rpcrdma_create_rep(r_xprt);
1038 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001039 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1040 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001041 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001042 goto out;
1043 }
Chuck Lever1e465fd2015-10-24 17:27:02 -04001044 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001045 }
Chuck Lever13924022015-01-21 11:03:52 -05001046
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001047 return 0;
1048out:
1049 rpcrdma_buffer_destroy(buf);
1050 return rc;
1051}
1052
Chuck Lever1e465fd2015-10-24 17:27:02 -04001053static struct rpcrdma_req *
1054rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1055{
1056 struct rpcrdma_req *req;
1057
1058 req = list_first_entry(&buf->rb_send_bufs,
Chuck Levera80d66c2017-06-08 11:52:12 -04001059 struct rpcrdma_req, rl_list);
Chuck Lever431af642017-06-08 11:52:20 -04001060 list_del_init(&req->rl_list);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001061 return req;
1062}
1063
1064static struct rpcrdma_rep *
1065rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1066{
1067 struct rpcrdma_rep *rep;
1068
1069 rep = list_first_entry(&buf->rb_recv_bufs,
1070 struct rpcrdma_rep, rr_list);
1071 list_del(&rep->rr_list);
1072 return rep;
1073}
1074
Chuck Lever2e845222014-07-29 17:25:38 -04001075static void
Chuck Lever13650c22016-09-15 10:56:26 -04001076rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -05001077{
Chuck Lever13650c22016-09-15 10:56:26 -04001078 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001079 kfree(rep);
1080}
1081
Chuck Leverf531a5d2015-10-24 17:27:43 -04001082void
Chuck Lever13650c22016-09-15 10:56:26 -04001083rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -05001084{
Chuck Lever13650c22016-09-15 10:56:26 -04001085 rpcrdma_free_regbuf(req->rl_recvbuf);
1086 rpcrdma_free_regbuf(req->rl_sendbuf);
1087 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001088 kfree(req);
1089}
1090
Chuck Levere2ac2362016-06-29 13:54:00 -04001091static void
1092rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1093{
1094 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1095 rx_buf);
1096 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1097 struct rpcrdma_mw *mw;
1098 unsigned int count;
1099
1100 count = 0;
1101 spin_lock(&buf->rb_mwlock);
1102 while (!list_empty(&buf->rb_all)) {
1103 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1104 list_del(&mw->mw_all);
1105
1106 spin_unlock(&buf->rb_mwlock);
1107 ia->ri_ops->ro_release_mr(mw);
1108 count++;
1109 spin_lock(&buf->rb_mwlock);
1110 }
1111 spin_unlock(&buf->rb_mwlock);
1112 r_xprt->rx_stats.mrs_allocated = 0;
1113
1114 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1115}
1116
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001117void
1118rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1119{
Chuck Lever505bbe62016-06-29 13:52:54 -04001120 cancel_delayed_work_sync(&buf->rb_recovery_worker);
Chuck Lever9378b272017-04-11 13:22:29 -04001121 cancel_delayed_work_sync(&buf->rb_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001122
Chuck Lever1e465fd2015-10-24 17:27:02 -04001123 while (!list_empty(&buf->rb_recv_bufs)) {
1124 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001125
Chuck Lever1e465fd2015-10-24 17:27:02 -04001126 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001127 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001128 }
Chuck Lever05c97462016-09-06 11:22:58 -04001129 buf->rb_send_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001130
Chuck Leverf531a5d2015-10-24 17:27:43 -04001131 spin_lock(&buf->rb_reqslock);
1132 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001133 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001134
Chuck Leverf531a5d2015-10-24 17:27:43 -04001135 req = list_first_entry(&buf->rb_allreqs,
1136 struct rpcrdma_req, rl_all);
1137 list_del(&req->rl_all);
1138
1139 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001140 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001141 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001142 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001143 spin_unlock(&buf->rb_reqslock);
Chuck Lever05c97462016-09-06 11:22:58 -04001144 buf->rb_recv_count = 0;
Chuck Lever9f9d8022014-07-29 17:24:45 -04001145
Chuck Levere2ac2362016-06-29 13:54:00 -04001146 rpcrdma_destroy_mrs(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001147}
1148
Chuck Lever346aa662015-05-26 11:52:06 -04001149struct rpcrdma_mw *
1150rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001151{
Chuck Lever346aa662015-05-26 11:52:06 -04001152 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1153 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001154
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001155 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001156 if (!list_empty(&buf->rb_mws))
1157 mw = rpcrdma_pop_mw(&buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001158 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001159
1160 if (!mw)
Chuck Levere2ac2362016-06-29 13:54:00 -04001161 goto out_nomws;
Chuck Lever4b196dc62017-06-08 11:51:56 -04001162 mw->mw_flags = 0;
Chuck Lever346aa662015-05-26 11:52:06 -04001163 return mw;
Chuck Levere2ac2362016-06-29 13:54:00 -04001164
1165out_nomws:
1166 dprintk("RPC: %s: no MWs available\n", __func__);
Chuck Leverbebd0312017-04-11 13:23:10 -04001167 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1168 schedule_delayed_work(&buf->rb_refresh_worker, 0);
Chuck Levere2ac2362016-06-29 13:54:00 -04001169
1170 /* Allow the reply handler and refresh worker to run */
1171 cond_resched();
1172
1173 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001174}
1175
Chuck Lever346aa662015-05-26 11:52:06 -04001176void
1177rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001178{
Chuck Lever346aa662015-05-26 11:52:06 -04001179 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001180
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001181 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001182 rpcrdma_push_mw(mw, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001183 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001184}
1185
Chuck Lever05c97462016-09-06 11:22:58 -04001186static struct rpcrdma_rep *
1187rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1188{
1189 /* If an RPC previously completed without a reply (say, a
1190 * credential problem or a soft timeout occurs) then hold off
1191 * on supplying more Receive buffers until the number of new
1192 * pending RPCs catches up to the number of posted Receives.
1193 */
1194 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1195 return NULL;
1196
1197 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1198 return NULL;
1199 buffers->rb_recv_count++;
1200 return rpcrdma_buffer_get_rep_locked(buffers);
1201}
1202
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001203/*
1204 * Get a set of request/reply buffers.
Chuck Lever78d506e2016-09-06 11:22:49 -04001205 *
1206 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001207 */
1208struct rpcrdma_req *
1209rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1210{
1211 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001212
Chuck Levera5b027e2015-10-24 17:27:27 -04001213 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001214 if (list_empty(&buffers->rb_send_bufs))
1215 goto out_reqbuf;
Chuck Lever05c97462016-09-06 11:22:58 -04001216 buffers->rb_send_count++;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001217 req = rpcrdma_buffer_get_req_locked(buffers);
Chuck Lever05c97462016-09-06 11:22:58 -04001218 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001219 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001220 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001221
Chuck Lever1e465fd2015-10-24 17:27:02 -04001222out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001223 spin_unlock(&buffers->rb_lock);
Chuck Lever78d506e2016-09-06 11:22:49 -04001224 pr_warn("RPC: %s: out of request buffers\n", __func__);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001225 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001226}
1227
1228/*
1229 * Put request/reply buffers back into pool.
1230 * Pre-decrement counter/array index.
1231 */
1232void
1233rpcrdma_buffer_put(struct rpcrdma_req *req)
1234{
1235 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001236 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001237
Chuck Lever90aab602016-09-15 10:56:43 -04001238 req->rl_send_wr.num_sge = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001239 req->rl_reply = NULL;
1240
Chuck Levera5b027e2015-10-24 17:27:27 -04001241 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001242 buffers->rb_send_count--;
Chuck Levera80d66c2017-06-08 11:52:12 -04001243 list_add_tail(&req->rl_list, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001244 if (rep) {
1245 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001246 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001247 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001248 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001249}
1250
1251/*
1252 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001253 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001254 */
1255void
1256rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1257{
1258 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001259
Chuck Levera5b027e2015-10-24 17:27:27 -04001260 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001261 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001262 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001263}
1264
1265/*
1266 * Put reply buffers back into pool when not attached to
1267 * request. This happens in error conditions.
1268 */
1269void
1270rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1271{
Chuck Leverfed171b2015-05-26 11:51:37 -04001272 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001273
Chuck Levera5b027e2015-10-24 17:27:27 -04001274 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001275 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001276 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001277 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001278}
1279
Chuck Lever9128c3e2015-01-21 11:04:00 -05001280/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001281 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001282 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001283 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001284 * @flags: GFP flags
1285 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001286 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1287 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001288 *
1289 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001290 * receiving the payload of RDMA RECV operations. During Long Calls
1291 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001292 */
1293struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001294rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1295 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001296{
1297 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001298
Chuck Lever9128c3e2015-01-21 11:04:00 -05001299 rb = kmalloc(sizeof(*rb) + size, flags);
1300 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001301 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001302
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001303 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001304 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001305 rb->rg_iov.length = size;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001306
1307 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001308}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001309
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001310/**
1311 * __rpcrdma_map_regbuf - DMA-map a regbuf
1312 * @ia: controlling rpcrdma_ia
1313 * @rb: regbuf to be mapped
1314 */
1315bool
1316__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1317{
Chuck Lever91a10c52017-04-11 13:23:02 -04001318 struct ib_device *device = ia->ri_device;
1319
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001320 if (rb->rg_direction == DMA_NONE)
1321 return false;
1322
Chuck Lever91a10c52017-04-11 13:23:02 -04001323 rb->rg_iov.addr = ib_dma_map_single(device,
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001324 (void *)rb->rg_base,
1325 rdmab_length(rb),
1326 rb->rg_direction);
Chuck Lever91a10c52017-04-11 13:23:02 -04001327 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001328 return false;
1329
Chuck Lever91a10c52017-04-11 13:23:02 -04001330 rb->rg_device = device;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001331 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1332 return true;
1333}
1334
1335static void
1336rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1337{
1338 if (!rpcrdma_regbuf_is_mapped(rb))
1339 return;
1340
1341 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1342 rdmab_length(rb), rb->rg_direction);
1343 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001344}
1345
1346/**
1347 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001348 * @rb: regbuf to be deregistered and freed
1349 */
1350void
Chuck Lever13650c22016-09-15 10:56:26 -04001351rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001352{
Chuck Levere531dca2015-08-03 13:03:20 -04001353 if (!rb)
1354 return;
1355
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001356 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001357 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001358}
1359
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001360/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001361 * Prepost any receive buffer, then post send.
1362 *
1363 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1364 */
1365int
1366rpcrdma_ep_post(struct rpcrdma_ia *ia,
1367 struct rpcrdma_ep *ep,
1368 struct rpcrdma_req *req)
1369{
Chuck Lever90aab602016-09-15 10:56:43 -04001370 struct ib_send_wr *send_wr = &req->rl_send_wr;
1371 struct ib_send_wr *send_wr_fail;
Chuck Lever655fec62016-09-15 10:57:24 -04001372 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001373
Chuck Lever90aab602016-09-15 10:56:43 -04001374 if (req->rl_reply) {
1375 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001376 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001377 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001378 req->rl_reply = NULL;
1379 }
1380
Chuck Leverb3221d62015-08-03 13:03:39 -04001381 dprintk("RPC: %s: posting %d s/g entries\n",
Chuck Lever90aab602016-09-15 10:56:43 -04001382 __func__, send_wr->num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001383
Chuck Lever8d38de62016-11-29 10:52:16 -05001384 rpcrdma_set_signaled(ep, send_wr);
Chuck Lever90aab602016-09-15 10:56:43 -04001385 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001386 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001387 goto out_postsend_err;
1388 return 0;
1389
1390out_postsend_err:
1391 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1392 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001393}
1394
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001395int
1396rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001397 struct rpcrdma_rep *rep)
1398{
Chuck Lever6ea8e712016-09-15 10:56:51 -04001399 struct ib_recv_wr *recv_wr_fail;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001400 int rc;
1401
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001402 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1403 goto out_map;
Chuck Lever6ea8e712016-09-15 10:56:51 -04001404 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001405 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001406 goto out_postrecv;
1407 return 0;
1408
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001409out_map:
1410 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1411 return -EIO;
1412
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001413out_postrecv:
1414 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1415 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001416}
Chuck Lever43e95982014-07-29 17:23:34 -04001417
Chuck Leverf531a5d2015-10-24 17:27:43 -04001418/**
1419 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1420 * @r_xprt: transport associated with these backchannel resources
1421 * @min_reqs: minimum number of incoming requests expected
1422 *
1423 * Returns zero if all requested buffers were posted, or a negative errno.
1424 */
1425int
1426rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1427{
1428 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1429 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001430 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001431 int rc;
1432
1433 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001434 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001435 if (list_empty(&buffers->rb_recv_bufs))
1436 goto out_reqbuf;
1437 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001438 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001439
Chuck Leverb1573802016-09-15 10:56:35 -04001440 rc = rpcrdma_ep_post_recv(ia, rep);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001441 if (rc)
1442 goto out_rc;
1443 }
1444
1445 return 0;
1446
1447out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001448 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001449 pr_warn("%s: no extra receive buffers\n", __func__);
1450 return -ENOMEM;
1451
1452out_rc:
1453 rpcrdma_recv_buffer_put(rep);
1454 return rc;
1455}