blob: 247b00b715c293c95e7b407312c4b50590c92ad7 [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 Leverd8f532d2017-10-16 15:01:30 -040076struct 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 Lever552bf222016-03-04 11:28:36 -0500136/**
Chuck Lever1519e962016-09-15 10:57:49 -0400137 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
Chuck Lever552bf222016-03-04 11:28:36 -0500138 * @cq: completion queue (ignored)
139 * @wc: completed WR
140 *
141 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400142static void
Chuck Lever1519e962016-09-15 10:57:49 -0400143rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400144{
Chuck Lever552bf222016-03-04 11:28:36 -0500145 struct ib_cqe *cqe = wc->wr_cqe;
146 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
147 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400148
Chuck Lever85024272015-01-21 11:02:04 -0500149 /* WARNING: Only wr_id and status are reliable at this point */
150 if (wc->status != IB_WC_SUCCESS)
151 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400152
Chuck Lever85024272015-01-21 11:02:04 -0500153 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Lever85024272015-01-21 11:02:04 -0500154 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
155 __func__, rep, wc->byte_len);
156
Chuck Lever96f87782017-08-03 14:30:03 -0400157 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len);
Chuck Leverc8b920b2016-09-15 10:57:16 -0400158 rep->rr_wc_flags = wc->wc_flags;
159 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
160
Chuck Lever91a10c52017-04-11 13:23:02 -0400161 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
Chuck Lever6b1184c2015-01-21 11:04:25 -0500162 rdmab_addr(rep->rr_rdmabuf),
Chuck Levere2a67192017-08-03 14:30:44 -0400163 wc->byte_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500164
Chuck Leverfc664482014-05-28 10:33:25 -0400165out_schedule:
Chuck Leverd8f532d2017-10-16 15:01:30 -0400166 rpcrdma_reply_handler(rep);
Chuck Lever85024272015-01-21 11:02:04 -0500167 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400168
Chuck Lever85024272015-01-21 11:02:04 -0500169out_fail:
170 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500171 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
172 ib_wc_status_msg(wc->status),
173 wc->status, wc->vendor_err);
Chuck Levere2a67192017-08-03 14:30:44 -0400174 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, 0);
Chuck Lever85024272015-01-21 11:02:04 -0500175 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400176}
177
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400178static void
179rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
180 struct rdma_conn_param *param)
181{
182 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
183 const struct rpcrdma_connect_private *pmsg = param->private_data;
184 unsigned int rsize, wsize;
185
Chuck Leverc8b920b2016-09-15 10:57:16 -0400186 /* Default settings for RPC-over-RDMA Version One */
187 r_xprt->rx_ia.ri_reminv_expected = false;
Chuck Leverb5f0afb2017-02-08 16:59:54 -0500188 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400189 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
190 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
191
192 if (pmsg &&
193 pmsg->cp_magic == rpcrdma_cmp_magic &&
194 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
Chuck Leverc8b920b2016-09-15 10:57:16 -0400195 r_xprt->rx_ia.ri_reminv_expected = true;
Chuck Leverc95a3c62017-02-08 17:00:02 -0500196 r_xprt->rx_ia.ri_implicit_roundup = true;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400197 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
198 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
199 }
200
201 if (rsize < cdata->inline_rsize)
202 cdata->inline_rsize = rsize;
203 if (wsize < cdata->inline_wsize)
204 cdata->inline_wsize = wsize;
Chuck Lever6d6bf722016-11-29 10:53:13 -0500205 dprintk("RPC: %s: max send %u, max recv %u\n",
206 __func__, cdata->inline_wsize, cdata->inline_rsize);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400207 rpcrdma_set_max_header_sizes(r_xprt);
208}
209
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400210static int
211rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
212{
213 struct rpcrdma_xprt *xprt = id->context;
214 struct rpcrdma_ia *ia = &xprt->rx_ia;
215 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500216#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400217 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800218#endif
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400219 int connstate = 0;
220
221 switch (event->event) {
222 case RDMA_CM_EVENT_ADDR_RESOLVED:
223 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400224 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400225 complete(&ia->ri_done);
226 break;
227 case RDMA_CM_EVENT_ADDR_ERROR:
228 ia->ri_async_rc = -EHOSTUNREACH;
229 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
230 __func__, ep);
231 complete(&ia->ri_done);
232 break;
233 case RDMA_CM_EVENT_ROUTE_ERROR:
234 ia->ri_async_rc = -ENETUNREACH;
235 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
236 __func__, ep);
237 complete(&ia->ri_done);
238 break;
Chuck Leverbebd0312017-04-11 13:23:10 -0400239 case RDMA_CM_EVENT_DEVICE_REMOVAL:
240#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever173b8f42017-06-08 11:53:00 -0400241 pr_info("rpcrdma: removing device %s for %pIS:%u\n",
242 ia->ri_device->name,
Chuck Leverbebd0312017-04-11 13:23:10 -0400243 sap, rpc_get_port(sap));
244#endif
245 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
246 ep->rep_connected = -ENODEV;
247 xprt_force_disconnect(&xprt->rx_xprt);
248 wait_for_completion(&ia->ri_remove_done);
249
250 ia->ri_id = NULL;
251 ia->ri_pd = NULL;
252 ia->ri_device = NULL;
253 /* Return 1 to ensure the core destroys the id. */
254 return 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400255 case RDMA_CM_EVENT_ESTABLISHED:
256 connstate = 1;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400257 rpcrdma_update_connect_private(xprt, &event->param.conn);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400258 goto connected;
259 case RDMA_CM_EVENT_CONNECT_ERROR:
260 connstate = -ENOTCONN;
261 goto connected;
262 case RDMA_CM_EVENT_UNREACHABLE:
263 connstate = -ENETDOWN;
264 goto connected;
265 case RDMA_CM_EVENT_REJECTED:
Chuck Lever173b8f42017-06-08 11:53:00 -0400266 dprintk("rpcrdma: connection to %pIS:%u rejected: %s\n",
267 sap, rpc_get_port(sap),
Chuck Lever0a904872017-02-08 17:00:35 -0500268 rdma_reject_msg(id, event->status));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400269 connstate = -ECONNREFUSED;
Chuck Lever0a904872017-02-08 17:00:35 -0500270 if (event->status == IB_CM_REJ_STALE_CONN)
271 connstate = -EAGAIN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400272 goto connected;
273 case RDMA_CM_EVENT_DISCONNECTED:
274 connstate = -ECONNABORTED;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400275connected:
Chuck Leverbe798f92017-10-16 15:01:39 -0400276 xprt->rx_buf.rb_credits = 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400277 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500278 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400279 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400280 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400281 default:
Chuck Lever173b8f42017-06-08 11:53:00 -0400282 dprintk("RPC: %s: %pIS:%u on %s/%s (ep 0x%p): %s\n",
283 __func__, sap, rpc_get_port(sap),
284 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 *
293rpcrdma_create_id(struct rpcrdma_xprt *xprt,
294 struct rpcrdma_ia *ia, struct sockaddr *addr)
295{
Chuck Lever109b88a2016-11-29 10:52:40 -0500296 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400297 struct rdma_cm_id *id;
298 int rc;
299
Tom Talpey1a954052008-10-09 15:01:31 -0400300 init_completion(&ia->ri_done);
Chuck Leverbebd0312017-04-11 13:23:10 -0400301 init_completion(&ia->ri_remove_done);
Tom Talpey1a954052008-10-09 15:01:31 -0400302
Guy Shapirofa201052015-10-22 15:20:10 +0300303 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
304 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400305 if (IS_ERR(id)) {
306 rc = PTR_ERR(id);
307 dprintk("RPC: %s: rdma_create_id() failed %i\n",
308 __func__, rc);
309 return id;
310 }
311
Tom Talpey5675add2008-10-09 15:01:41 -0400312 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400313 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
314 if (rc) {
315 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
316 __func__, rc);
317 goto out;
318 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500319 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
320 if (rc < 0) {
321 dprintk("RPC: %s: wait() exited: %i\n",
322 __func__, rc);
323 goto out;
324 }
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400325
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400326 rc = ia->ri_async_rc;
327 if (rc)
328 goto out;
329
Tom Talpey5675add2008-10-09 15:01:41 -0400330 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400331 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
332 if (rc) {
333 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
334 __func__, rc);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400335 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400336 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500337 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
338 if (rc < 0) {
339 dprintk("RPC: %s: wait() exited: %i\n",
340 __func__, rc);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400341 goto out;
Chuck Lever109b88a2016-11-29 10:52:40 -0500342 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400343 rc = ia->ri_async_rc;
344 if (rc)
Chuck Lever56a6bd12017-04-11 13:23:34 -0400345 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400346
347 return id;
Chuck Lever56a6bd12017-04-11 13:23:34 -0400348
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400349out:
350 rdma_destroy_id(id);
351 return ERR_PTR(rc);
352}
353
354/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400355 * Exported functions.
356 */
357
Chuck Leverfff09592017-04-11 13:22:54 -0400358/**
359 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
360 * @xprt: controlling transport
361 * @addr: IP address of remote peer
362 *
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 Leverfff09592017-04-11 13:22:54 -0400367rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr)
\"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
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400372 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
373 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) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400387 case RPCRDMA_FRMR:
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 }
457 rpcrdma_destroy_mrs(buf);
458
459 /* Allow waiters to continue */
460 complete(&ia->ri_remove_done);
461}
462
463/**
Chuck Leverfff09592017-04-11 13:22:54 -0400464 * rpcrdma_ia_close - Clean up/close an IA.
465 * @ia: interface adapter to close
466 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400467 */
468void
469rpcrdma_ia_close(struct rpcrdma_ia *ia)
470{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400471 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400472 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
473 if (ia->ri_id->qp)
474 rdma_destroy_qp(ia->ri_id);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400475 rdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400476 }
Chuck Leverfff09592017-04-11 13:22:54 -0400477 ia->ri_id = NULL;
478 ia->ri_device = NULL;
Chuck Lever6d446982015-05-26 11:51:27 -0400479
480 /* If the pd is still busy, xprtrdma missed freeing a resource */
481 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600482 ib_dealloc_pd(ia->ri_pd);
Chuck Leverfff09592017-04-11 13:22:54 -0400483 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400484}
485
486/*
487 * Create unconnected endpoint.
488 */
489int
490rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
Chuck Lever16f906d2017-02-08 17:00:10 -0500491 struct rpcrdma_create_data_internal *cdata)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400492{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400493 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
Chuck Lever16f906d2017-02-08 17:00:10 -0500494 unsigned int max_qp_wr, max_sge;
Chuck Leverfc664482014-05-28 10:33:25 -0400495 struct ib_cq *sendcq, *recvcq;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500496 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400497
Chuck Levereed50872017-03-11 15:52:47 -0500498 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
499 RPCRDMA_MAX_SEND_SGES);
Chuck Lever16f906d2017-02-08 17:00:10 -0500500 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
501 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
Chuck Leverb3221d62015-08-03 13:03:39 -0400502 return -ENOMEM;
503 }
Chuck Lever16f906d2017-02-08 17:00:10 -0500504 ia->ri_max_send_sges = max_sge - RPCRDMA_MIN_SEND_SGES;
Chuck Leverb3221d62015-08-03 13:03:39 -0400505
Or Gerlitze3e45b12015-12-18 10:59:48 +0200506 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400507 dprintk("RPC: %s: insufficient wqe's available\n",
508 __func__);
509 return -ENOMEM;
510 }
Chuck Lever550d7502016-05-02 14:41:47 -0400511 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
Chuck Lever124fa172015-10-24 17:27:51 -0400512
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400513 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400514 if (cdata->max_requests > max_qp_wr)
515 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400516
517 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
518 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400519 ep->rep_attr.srq = NULL;
520 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400521 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400522 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
Chuck Lever3968cb52015-03-30 14:35:26 -0400523 rc = ia->ri_ops->ro_open(ia, ep, cdata);
524 if (rc)
525 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400526 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400527 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400528 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
Chuck Lever16f906d2017-02-08 17:00:10 -0500529 ep->rep_attr.cap.max_send_sge = max_sge;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400530 ep->rep_attr.cap.max_recv_sge = 1;
531 ep->rep_attr.cap.max_inline_data = 0;
532 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
533 ep->rep_attr.qp_type = IB_QPT_RC;
534 ep->rep_attr.port_num = ~0;
535
536 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
537 "iovs: send %d recv %d\n",
538 __func__,
539 ep->rep_attr.cap.max_send_wr,
540 ep->rep_attr.cap.max_recv_wr,
541 ep->rep_attr.cap.max_send_sge,
542 ep->rep_attr.cap.max_recv_sge);
543
544 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400545 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500546 if (ep->rep_cqinit <= 2)
547 ep->rep_cqinit = 0; /* always signal? */
Chuck Lever8d38de62016-11-29 10:52:16 -0500548 rpcrdma_init_cqcount(ep, 0);
\"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,
554 0, IB_POLL_SOFTIRQ);
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{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400626 dprintk("RPC: %s: entering, connected is %d\n",
627 __func__, ep->rep_connected);
628
Chuck Lever254f91e2014-05-28 10:32:17 -0400629 cancel_delayed_work_sync(&ep->rep_connect_worker);
630
Steve Wise72c02172015-09-21 12:24:23 -0500631 if (ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400632 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400633 rdma_destroy_qp(ia->ri_id);
634 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400635 }
636
Chuck Lever552bf222016-03-04 11:28:36 -0500637 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500638 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400639}
640
Chuck Levera9b0e382017-04-11 13:23:26 -0400641/* Re-establish a connection after a device removal event.
642 * Unlike a normal reconnection, a fresh PD and a new set
643 * of MRs and buffers is needed.
644 */
645static int
646rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
647 struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
648{
649 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
650 int rc, err;
651
652 pr_info("%s: r_xprt = %p\n", __func__, r_xprt);
653
654 rc = -EHOSTUNREACH;
655 if (rpcrdma_ia_open(r_xprt, sap))
656 goto out1;
657
658 rc = -ENOMEM;
659 err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
660 if (err) {
661 pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
662 goto out2;
663 }
664
665 rc = -ENETUNREACH;
666 err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
667 if (err) {
668 pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
669 goto out3;
670 }
671
672 rpcrdma_create_mrs(r_xprt);
673 return 0;
674
675out3:
676 rpcrdma_ep_destroy(ep, ia);
677out2:
678 rpcrdma_ia_close(ia);
679out1:
680 return rc;
681}
682
Chuck Lever18908962017-04-11 13:23:18 -0400683static int
684rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
685 struct rpcrdma_ia *ia)
686{
687 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
688 struct rdma_cm_id *id, *old;
689 int err, rc;
690
691 dprintk("RPC: %s: reconnecting...\n", __func__);
692
693 rpcrdma_ep_disconnect(ep, ia);
694
695 rc = -EHOSTUNREACH;
696 id = rpcrdma_create_id(r_xprt, ia, sap);
697 if (IS_ERR(id))
698 goto out;
699
700 /* As long as the new ID points to the same device as the
701 * old ID, we can reuse the transport's existing PD and all
702 * previously allocated MRs. Also, the same device means
703 * the transport's previous DMA mappings are still valid.
704 *
705 * This is a sanity check only. There should be no way these
706 * point to two different devices here.
707 */
708 old = id;
709 rc = -ENETUNREACH;
710 if (ia->ri_device != id->device) {
711 pr_err("rpcrdma: can't reconnect on different device!\n");
712 goto out_destroy;
713 }
714
715 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
716 if (err) {
717 dprintk("RPC: %s: rdma_create_qp returned %d\n",
718 __func__, err);
719 goto out_destroy;
720 }
721
722 /* Atomically replace the transport's ID and QP. */
723 rc = 0;
724 old = ia->ri_id;
725 ia->ri_id = id;
726 rdma_destroy_qp(old);
727
728out_destroy:
Chuck Lever56a6bd12017-04-11 13:23:34 -0400729 rdma_destroy_id(old);
Chuck Lever18908962017-04-11 13:23:18 -0400730out:
731 return rc;
732}
733
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400734/*
735 * Connect unconnected endpoint.
736 */
737int
738rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
739{
Chuck Lever0a904872017-02-08 17:00:35 -0500740 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
741 rx_ia);
Chuck Lever0a904872017-02-08 17:00:35 -0500742 unsigned int extras;
Chuck Lever18908962017-04-11 13:23:18 -0400743 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400744
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400745retry:
Chuck Lever18908962017-04-11 13:23:18 -0400746 switch (ep->rep_connected) {
747 case 0:
Chuck Leverec62f402014-05-28 10:34:07 -0400748 dprintk("RPC: %s: connecting...\n", __func__);
749 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
750 if (rc) {
751 dprintk("RPC: %s: rdma_create_qp failed %i\n",
752 __func__, rc);
Chuck Lever18908962017-04-11 13:23:18 -0400753 rc = -ENETUNREACH;
754 goto out_noupdate;
Chuck Leverec62f402014-05-28 10:34:07 -0400755 }
Chuck Lever18908962017-04-11 13:23:18 -0400756 break;
Chuck Levera9b0e382017-04-11 13:23:26 -0400757 case -ENODEV:
758 rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
759 if (rc)
760 goto out_noupdate;
761 break;
Chuck Lever18908962017-04-11 13:23:18 -0400762 default:
763 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
764 if (rc)
765 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400766 }
767
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400768 ep->rep_connected = 0;
769
770 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
771 if (rc) {
772 dprintk("RPC: %s: rdma_connect() failed with %i\n",
773 __func__, rc);
774 goto out;
775 }
776
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400777 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400778 if (ep->rep_connected <= 0) {
Chuck Lever0a904872017-02-08 17:00:35 -0500779 if (ep->rep_connected == -EAGAIN)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400780 goto retry;
781 rc = ep->rep_connected;
Chuck Lever0a904872017-02-08 17:00:35 -0500782 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400783 }
784
Chuck Lever0a904872017-02-08 17:00:35 -0500785 dprintk("RPC: %s: connected\n", __func__);
786 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
787 if (extras)
788 rpcrdma_ep_post_extra_recv(r_xprt, extras);
789
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400790out:
791 if (rc)
792 ep->rep_connected = rc;
Chuck Lever18908962017-04-11 13:23:18 -0400793
794out_noupdate:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400795 return rc;
796}
797
798/*
799 * rpcrdma_ep_disconnect
800 *
801 * This is separate from destroy to facilitate the ability
802 * to reconnect without recreating the endpoint.
803 *
804 * This call is not reentrant, and must not be made in parallel
805 * on the same endpoint.
806 */
Chuck Lever282191c2014-07-29 17:25:55 -0400807void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400808rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
809{
810 int rc;
811
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400812 rc = rdma_disconnect(ia->ri_id);
813 if (!rc) {
814 /* returns without wait if not connected */
815 wait_event_interruptible(ep->rep_connect_wait,
816 ep->rep_connected != 1);
817 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
818 (ep->rep_connected == 1) ? "still " : "dis");
819 } else {
820 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
821 ep->rep_connected = rc;
822 }
Chuck Lever550d7502016-05-02 14:41:47 -0400823
824 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400825}
826
Chuck Lever505bbe62016-06-29 13:52:54 -0400827static void
828rpcrdma_mr_recovery_worker(struct work_struct *work)
829{
830 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
831 rb_recovery_worker.work);
832 struct rpcrdma_mw *mw;
833
834 spin_lock(&buf->rb_recovery_lock);
835 while (!list_empty(&buf->rb_stale_mrs)) {
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500836 mw = rpcrdma_pop_mw(&buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400837 spin_unlock(&buf->rb_recovery_lock);
838
839 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
840 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
841
842 spin_lock(&buf->rb_recovery_lock);
kbuild test robot53d78522016-07-16 06:02:05 +0800843 }
Chuck Lever505bbe62016-06-29 13:52:54 -0400844 spin_unlock(&buf->rb_recovery_lock);
845}
846
847void
848rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
849{
850 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
851 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
852
853 spin_lock(&buf->rb_recovery_lock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500854 rpcrdma_push_mw(mw, &buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400855 spin_unlock(&buf->rb_recovery_lock);
856
857 schedule_delayed_work(&buf->rb_recovery_worker, 0);
858}
859
Chuck Levere2ac2362016-06-29 13:54:00 -0400860static void
861rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
862{
863 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
864 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
865 unsigned int count;
866 LIST_HEAD(free);
867 LIST_HEAD(all);
868
869 for (count = 0; count < 32; count++) {
870 struct rpcrdma_mw *mw;
871 int rc;
872
873 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
874 if (!mw)
875 break;
876
877 rc = ia->ri_ops->ro_init_mr(ia, mw);
878 if (rc) {
879 kfree(mw);
880 break;
881 }
882
883 mw->mw_xprt = r_xprt;
884
885 list_add(&mw->mw_list, &free);
886 list_add(&mw->mw_all, &all);
887 }
888
889 spin_lock(&buf->rb_mwlock);
890 list_splice(&free, &buf->rb_mws);
891 list_splice(&all, &buf->rb_all);
892 r_xprt->rx_stats.mrs_allocated += count;
893 spin_unlock(&buf->rb_mwlock);
894
895 dprintk("RPC: %s: created %u MRs\n", __func__, count);
896}
897
898static void
899rpcrdma_mr_refresh_worker(struct work_struct *work)
900{
901 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
902 rb_refresh_worker.work);
903 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
904 rx_buf);
905
906 rpcrdma_create_mrs(r_xprt);
907}
908
Chuck Leverf531a5d2015-10-24 17:27:43 -0400909struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500910rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
911{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400912 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500913 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500914
Chuck Lever85275c82015-01-21 11:04:16 -0500915 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500916 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500917 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500918
Chuck Leverf531a5d2015-10-24 17:27:43 -0400919 spin_lock(&buffer->rb_reqslock);
920 list_add(&req->rl_all, &buffer->rb_allreqs);
921 spin_unlock(&buffer->rb_reqslock);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500922 req->rl_cqe.done = rpcrdma_wc_send;
Chuck Lever13924022015-01-21 11:03:52 -0500923 req->rl_buffer = &r_xprt->rx_buf;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400924 INIT_LIST_HEAD(&req->rl_registered);
Chuck Lever90aab602016-09-15 10:56:43 -0400925 req->rl_send_wr.next = NULL;
926 req->rl_send_wr.wr_cqe = &req->rl_cqe;
Chuck Lever655fec62016-09-15 10:57:24 -0400927 req->rl_send_wr.sg_list = req->rl_send_sge;
Chuck Lever90aab602016-09-15 10:56:43 -0400928 req->rl_send_wr.opcode = IB_WR_SEND;
Chuck Lever13924022015-01-21 11:03:52 -0500929 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500930}
931
Chuck Leverf531a5d2015-10-24 17:27:43 -0400932struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -0500933rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
934{
935 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500936 struct rpcrdma_rep *rep;
937 int rc;
938
939 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500940 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500941 if (rep == NULL)
942 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500943
Chuck Lever13650c22016-09-15 10:56:26 -0400944 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -0400945 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever6b1184c2015-01-21 11:04:25 -0500946 if (IS_ERR(rep->rr_rdmabuf)) {
947 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500948 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500949 }
Chuck Lever96f87782017-08-03 14:30:03 -0400950 xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base,
951 rdmab_length(rep->rr_rdmabuf));
Chuck Lever13924022015-01-21 11:03:52 -0500952
Chuck Lever1519e962016-09-15 10:57:49 -0400953 rep->rr_cqe.done = rpcrdma_wc_receive;
Chuck Leverfed171b2015-05-26 11:51:37 -0400954 rep->rr_rxprt = r_xprt;
Chuck Leverd8f532d2017-10-16 15:01:30 -0400955 INIT_WORK(&rep->rr_work, rpcrdma_deferred_completion);
Chuck Lever6ea8e712016-09-15 10:56:51 -0400956 rep->rr_recv_wr.next = NULL;
957 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
958 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
959 rep->rr_recv_wr.num_sge = 1;
Chuck Lever13924022015-01-21 11:03:52 -0500960 return rep;
961
962out_free:
963 kfree(rep);
964out:
965 return ERR_PTR(rc);
966}
967
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400968int
Chuck Leverac920d02015-01-21 11:03:44 -0500969rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400970{
Chuck Leverac920d02015-01-21 11:03:44 -0500971 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400972 int i, rc;
973
Chuck Lever1e465fd2015-10-24 17:27:02 -0400974 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -0400975 buf->rb_bc_srv_max_requests = 0;
Chuck Levere2ac2362016-06-29 13:54:00 -0400976 spin_lock_init(&buf->rb_mwlock);
Chuck Lever505bbe62016-06-29 13:52:54 -0400977 spin_lock_init(&buf->rb_lock);
978 spin_lock_init(&buf->rb_recovery_lock);
Chuck Levere2ac2362016-06-29 13:54:00 -0400979 INIT_LIST_HEAD(&buf->rb_mws);
980 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever505bbe62016-06-29 13:52:54 -0400981 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -0400982 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
983 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -0400984 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
985 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400986
Chuck Levere2ac2362016-06-29 13:54:00 -0400987 rpcrdma_create_mrs(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400988
Chuck Lever1e465fd2015-10-24 17:27:02 -0400989 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400990 INIT_LIST_HEAD(&buf->rb_allreqs);
991 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400992 for (i = 0; i < buf->rb_max_requests; i++) {
993 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400994
Chuck Lever13924022015-01-21 11:03:52 -0500995 req = rpcrdma_create_req(r_xprt);
996 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400997 dprintk("RPC: %s: request buffer %d alloc"
998 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500999 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001000 goto out;
1001 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001002 req->rl_backchannel = false;
Chuck Levera80d66c2017-06-08 11:52:12 -04001003 list_add(&req->rl_list, &buf->rb_send_bufs);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001004 }
1005
1006 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001007 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001008 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001009
Chuck Lever13924022015-01-21 11:03:52 -05001010 rep = rpcrdma_create_rep(r_xprt);
1011 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001012 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1013 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001014 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001015 goto out;
1016 }
Chuck Lever1e465fd2015-10-24 17:27:02 -04001017 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001018 }
Chuck Lever13924022015-01-21 11:03:52 -05001019
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001020 return 0;
1021out:
1022 rpcrdma_buffer_destroy(buf);
1023 return rc;
1024}
1025
Chuck Lever1e465fd2015-10-24 17:27:02 -04001026static struct rpcrdma_req *
1027rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1028{
1029 struct rpcrdma_req *req;
1030
1031 req = list_first_entry(&buf->rb_send_bufs,
Chuck Levera80d66c2017-06-08 11:52:12 -04001032 struct rpcrdma_req, rl_list);
Chuck Lever431af642017-06-08 11:52:20 -04001033 list_del_init(&req->rl_list);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001034 return req;
1035}
1036
1037static struct rpcrdma_rep *
1038rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1039{
1040 struct rpcrdma_rep *rep;
1041
1042 rep = list_first_entry(&buf->rb_recv_bufs,
1043 struct rpcrdma_rep, rr_list);
1044 list_del(&rep->rr_list);
1045 return rep;
1046}
1047
Chuck Lever2e845222014-07-29 17:25:38 -04001048static void
Chuck Lever13650c22016-09-15 10:56:26 -04001049rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -05001050{
Chuck Lever13650c22016-09-15 10:56:26 -04001051 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001052 kfree(rep);
1053}
1054
Chuck Leverf531a5d2015-10-24 17:27:43 -04001055void
Chuck Lever13650c22016-09-15 10:56:26 -04001056rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -05001057{
Chuck Lever13650c22016-09-15 10:56:26 -04001058 rpcrdma_free_regbuf(req->rl_recvbuf);
1059 rpcrdma_free_regbuf(req->rl_sendbuf);
1060 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001061 kfree(req);
1062}
1063
Chuck Levere2ac2362016-06-29 13:54:00 -04001064static void
1065rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1066{
1067 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1068 rx_buf);
1069 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1070 struct rpcrdma_mw *mw;
1071 unsigned int count;
1072
1073 count = 0;
1074 spin_lock(&buf->rb_mwlock);
1075 while (!list_empty(&buf->rb_all)) {
1076 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1077 list_del(&mw->mw_all);
1078
1079 spin_unlock(&buf->rb_mwlock);
1080 ia->ri_ops->ro_release_mr(mw);
1081 count++;
1082 spin_lock(&buf->rb_mwlock);
1083 }
1084 spin_unlock(&buf->rb_mwlock);
1085 r_xprt->rx_stats.mrs_allocated = 0;
1086
1087 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1088}
1089
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001090void
1091rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1092{
Chuck Lever505bbe62016-06-29 13:52:54 -04001093 cancel_delayed_work_sync(&buf->rb_recovery_worker);
Chuck Lever9378b272017-04-11 13:22:29 -04001094 cancel_delayed_work_sync(&buf->rb_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001095
Chuck Lever1e465fd2015-10-24 17:27:02 -04001096 while (!list_empty(&buf->rb_recv_bufs)) {
1097 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001098
Chuck Lever1e465fd2015-10-24 17:27:02 -04001099 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001100 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001101 }
Chuck Lever05c97462016-09-06 11:22:58 -04001102 buf->rb_send_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001103
Chuck Leverf531a5d2015-10-24 17:27:43 -04001104 spin_lock(&buf->rb_reqslock);
1105 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001106 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001107
Chuck Leverf531a5d2015-10-24 17:27:43 -04001108 req = list_first_entry(&buf->rb_allreqs,
1109 struct rpcrdma_req, rl_all);
1110 list_del(&req->rl_all);
1111
1112 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001113 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001114 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001115 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001116 spin_unlock(&buf->rb_reqslock);
Chuck Lever05c97462016-09-06 11:22:58 -04001117 buf->rb_recv_count = 0;
Chuck Lever9f9d8022014-07-29 17:24:45 -04001118
Chuck Levere2ac2362016-06-29 13:54:00 -04001119 rpcrdma_destroy_mrs(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001120}
1121
Chuck Lever346aa662015-05-26 11:52:06 -04001122struct rpcrdma_mw *
1123rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001124{
Chuck Lever346aa662015-05-26 11:52:06 -04001125 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1126 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001127
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001128 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001129 if (!list_empty(&buf->rb_mws))
1130 mw = rpcrdma_pop_mw(&buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001131 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001132
1133 if (!mw)
Chuck Levere2ac2362016-06-29 13:54:00 -04001134 goto out_nomws;
Chuck Lever4b196dc62017-06-08 11:51:56 -04001135 mw->mw_flags = 0;
Chuck Lever346aa662015-05-26 11:52:06 -04001136 return mw;
Chuck Levere2ac2362016-06-29 13:54:00 -04001137
1138out_nomws:
1139 dprintk("RPC: %s: no MWs available\n", __func__);
Chuck Leverbebd0312017-04-11 13:23:10 -04001140 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1141 schedule_delayed_work(&buf->rb_refresh_worker, 0);
Chuck Levere2ac2362016-06-29 13:54:00 -04001142
1143 /* Allow the reply handler and refresh worker to run */
1144 cond_resched();
1145
1146 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001147}
1148
Chuck Lever346aa662015-05-26 11:52:06 -04001149void
1150rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001151{
Chuck Lever346aa662015-05-26 11:52:06 -04001152 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001153
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001154 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001155 rpcrdma_push_mw(mw, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001156 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001157}
1158
Chuck Lever05c97462016-09-06 11:22:58 -04001159static struct rpcrdma_rep *
1160rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1161{
1162 /* If an RPC previously completed without a reply (say, a
1163 * credential problem or a soft timeout occurs) then hold off
1164 * on supplying more Receive buffers until the number of new
1165 * pending RPCs catches up to the number of posted Receives.
1166 */
1167 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1168 return NULL;
1169
1170 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1171 return NULL;
1172 buffers->rb_recv_count++;
1173 return rpcrdma_buffer_get_rep_locked(buffers);
1174}
1175
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001176/*
1177 * Get a set of request/reply buffers.
Chuck Lever78d506e2016-09-06 11:22:49 -04001178 *
1179 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001180 */
1181struct rpcrdma_req *
1182rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1183{
1184 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001185
Chuck Levera5b027e2015-10-24 17:27:27 -04001186 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001187 if (list_empty(&buffers->rb_send_bufs))
1188 goto out_reqbuf;
Chuck Lever05c97462016-09-06 11:22:58 -04001189 buffers->rb_send_count++;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001190 req = rpcrdma_buffer_get_req_locked(buffers);
Chuck Lever05c97462016-09-06 11:22:58 -04001191 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001192 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001193 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001194
Chuck Lever1e465fd2015-10-24 17:27:02 -04001195out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001196 spin_unlock(&buffers->rb_lock);
Chuck Lever78d506e2016-09-06 11:22:49 -04001197 pr_warn("RPC: %s: out of request buffers\n", __func__);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001198 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001199}
1200
1201/*
1202 * Put request/reply buffers back into pool.
1203 * Pre-decrement counter/array index.
1204 */
1205void
1206rpcrdma_buffer_put(struct rpcrdma_req *req)
1207{
1208 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001209 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001210
Chuck Lever90aab602016-09-15 10:56:43 -04001211 req->rl_send_wr.num_sge = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001212 req->rl_reply = NULL;
1213
Chuck Levera5b027e2015-10-24 17:27:27 -04001214 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001215 buffers->rb_send_count--;
Chuck Levera80d66c2017-06-08 11:52:12 -04001216 list_add_tail(&req->rl_list, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001217 if (rep) {
1218 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001219 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001220 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001221 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001222}
1223
1224/*
1225 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001226 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001227 */
1228void
1229rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1230{
1231 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001232
Chuck Levera5b027e2015-10-24 17:27:27 -04001233 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001234 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001235 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001236}
1237
1238/*
1239 * Put reply buffers back into pool when not attached to
1240 * request. This happens in error conditions.
1241 */
1242void
1243rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1244{
Chuck Leverfed171b2015-05-26 11:51:37 -04001245 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001246
Chuck Levera5b027e2015-10-24 17:27:27 -04001247 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001248 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001249 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001250 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001251}
1252
Chuck Lever9128c3e2015-01-21 11:04:00 -05001253/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001254 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001255 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001256 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001257 * @flags: GFP flags
1258 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001259 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1260 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001261 *
1262 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001263 * receiving the payload of RDMA RECV operations. During Long Calls
1264 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001265 */
1266struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001267rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1268 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001269{
1270 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001271
Chuck Lever9128c3e2015-01-21 11:04:00 -05001272 rb = kmalloc(sizeof(*rb) + size, flags);
1273 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001274 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001275
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001276 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001277 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001278 rb->rg_iov.length = size;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001279
1280 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001281}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001282
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001283/**
1284 * __rpcrdma_map_regbuf - DMA-map a regbuf
1285 * @ia: controlling rpcrdma_ia
1286 * @rb: regbuf to be mapped
1287 */
1288bool
1289__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1290{
Chuck Lever91a10c52017-04-11 13:23:02 -04001291 struct ib_device *device = ia->ri_device;
1292
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001293 if (rb->rg_direction == DMA_NONE)
1294 return false;
1295
Chuck Lever91a10c52017-04-11 13:23:02 -04001296 rb->rg_iov.addr = ib_dma_map_single(device,
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001297 (void *)rb->rg_base,
1298 rdmab_length(rb),
1299 rb->rg_direction);
Chuck Lever91a10c52017-04-11 13:23:02 -04001300 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001301 return false;
1302
Chuck Lever91a10c52017-04-11 13:23:02 -04001303 rb->rg_device = device;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001304 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1305 return true;
1306}
1307
1308static void
1309rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1310{
1311 if (!rpcrdma_regbuf_is_mapped(rb))
1312 return;
1313
1314 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1315 rdmab_length(rb), rb->rg_direction);
1316 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001317}
1318
1319/**
1320 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001321 * @rb: regbuf to be deregistered and freed
1322 */
1323void
Chuck Lever13650c22016-09-15 10:56:26 -04001324rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001325{
Chuck Levere531dca2015-08-03 13:03:20 -04001326 if (!rb)
1327 return;
1328
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001329 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001330 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001331}
1332
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001333/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001334 * Prepost any receive buffer, then post send.
1335 *
1336 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1337 */
1338int
1339rpcrdma_ep_post(struct rpcrdma_ia *ia,
1340 struct rpcrdma_ep *ep,
1341 struct rpcrdma_req *req)
1342{
Chuck Lever90aab602016-09-15 10:56:43 -04001343 struct ib_send_wr *send_wr = &req->rl_send_wr;
1344 struct ib_send_wr *send_wr_fail;
Chuck Lever655fec62016-09-15 10:57:24 -04001345 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001346
Chuck Lever90aab602016-09-15 10:56:43 -04001347 if (req->rl_reply) {
1348 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001349 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001350 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001351 req->rl_reply = NULL;
1352 }
1353
Chuck Leverb3221d62015-08-03 13:03:39 -04001354 dprintk("RPC: %s: posting %d s/g entries\n",
Chuck Lever90aab602016-09-15 10:56:43 -04001355 __func__, send_wr->num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001356
Chuck Lever8d38de62016-11-29 10:52:16 -05001357 rpcrdma_set_signaled(ep, send_wr);
Chuck Lever90aab602016-09-15 10:56:43 -04001358 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001359 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001360 goto out_postsend_err;
1361 return 0;
1362
1363out_postsend_err:
1364 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1365 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001366}
1367
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001368int
1369rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001370 struct rpcrdma_rep *rep)
1371{
Chuck Lever6ea8e712016-09-15 10:56:51 -04001372 struct ib_recv_wr *recv_wr_fail;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001373 int rc;
1374
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001375 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1376 goto out_map;
Chuck Lever6ea8e712016-09-15 10:56:51 -04001377 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001378 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001379 goto out_postrecv;
1380 return 0;
1381
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001382out_map:
1383 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1384 return -EIO;
1385
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001386out_postrecv:
1387 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1388 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001389}
Chuck Lever43e95982014-07-29 17:23:34 -04001390
Chuck Leverf531a5d2015-10-24 17:27:43 -04001391/**
1392 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1393 * @r_xprt: transport associated with these backchannel resources
1394 * @min_reqs: minimum number of incoming requests expected
1395 *
1396 * Returns zero if all requested buffers were posted, or a negative errno.
1397 */
1398int
1399rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1400{
1401 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1402 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001403 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001404 int rc;
1405
1406 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001407 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001408 if (list_empty(&buffers->rb_recv_bufs))
1409 goto out_reqbuf;
1410 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001411 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001412
Chuck Leverb1573802016-09-15 10:56:35 -04001413 rc = rpcrdma_ep_post_recv(ia, rep);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001414 if (rc)
1415 goto out_rc;
1416 }
1417
1418 return 0;
1419
1420out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001421 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001422 pr_warn("%s: no extra receive buffers\n", __func__);
1423 return -ENOMEM;
1424
1425out_rc:
1426 rpcrdma_recv_buffer_put(rep);
1427 return rc;
1428}