blob: f1b1c372f7fbced7fcc68dbd82956f4abdce9afe [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 Lever96f87782017-08-03 14:30:03 -0400183 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len);
Chuck Leverc8b920b2016-09-15 10:57:16 -0400184 rep->rr_wc_flags = wc->wc_flags;
185 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
186
Chuck Lever91a10c52017-04-11 13:23:02 -0400187 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
Chuck Lever6b1184c2015-01-21 11:04:25 -0500188 rdmab_addr(rep->rr_rdmabuf),
189 rep->rr_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500190
191 rpcrdma_update_granted_credits(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400192
193out_schedule:
Chuck Leverfe97b472015-10-24 17:27:10 -0400194 queue_work(rpcrdma_receive_wq, &rep->rr_work);
Chuck Lever85024272015-01-21 11:02:04 -0500195 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400196
Chuck Lever85024272015-01-21 11:02:04 -0500197out_fail:
198 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500199 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
200 ib_wc_status_msg(wc->status),
201 wc->status, wc->vendor_err);
Chuck Leverb0e178a2015-10-24 17:26:54 -0400202 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500203 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400204}
205
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400206static void
207rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
208 struct rdma_conn_param *param)
209{
210 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
211 const struct rpcrdma_connect_private *pmsg = param->private_data;
212 unsigned int rsize, wsize;
213
Chuck Leverc8b920b2016-09-15 10:57:16 -0400214 /* Default settings for RPC-over-RDMA Version One */
215 r_xprt->rx_ia.ri_reminv_expected = false;
Chuck Leverb5f0afb2017-02-08 16:59:54 -0500216 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400217 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
218 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
219
220 if (pmsg &&
221 pmsg->cp_magic == rpcrdma_cmp_magic &&
222 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
Chuck Leverc8b920b2016-09-15 10:57:16 -0400223 r_xprt->rx_ia.ri_reminv_expected = true;
Chuck Leverc95a3c62017-02-08 17:00:02 -0500224 r_xprt->rx_ia.ri_implicit_roundup = true;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400225 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
226 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
227 }
228
229 if (rsize < cdata->inline_rsize)
230 cdata->inline_rsize = rsize;
231 if (wsize < cdata->inline_wsize)
232 cdata->inline_wsize = wsize;
Chuck Lever6d6bf722016-11-29 10:53:13 -0500233 dprintk("RPC: %s: max send %u, max recv %u\n",
234 __func__, cdata->inline_wsize, cdata->inline_rsize);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400235 rpcrdma_set_max_header_sizes(r_xprt);
236}
237
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400238static int
239rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
240{
241 struct rpcrdma_xprt *xprt = id->context;
242 struct rpcrdma_ia *ia = &xprt->rx_ia;
243 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500244#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400245 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800246#endif
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400247 int connstate = 0;
248
249 switch (event->event) {
250 case RDMA_CM_EVENT_ADDR_RESOLVED:
251 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400252 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400253 complete(&ia->ri_done);
254 break;
255 case RDMA_CM_EVENT_ADDR_ERROR:
256 ia->ri_async_rc = -EHOSTUNREACH;
257 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
258 __func__, ep);
259 complete(&ia->ri_done);
260 break;
261 case RDMA_CM_EVENT_ROUTE_ERROR:
262 ia->ri_async_rc = -ENETUNREACH;
263 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
264 __func__, ep);
265 complete(&ia->ri_done);
266 break;
Chuck Leverbebd0312017-04-11 13:23:10 -0400267 case RDMA_CM_EVENT_DEVICE_REMOVAL:
268#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever173b8f42017-06-08 11:53:00 -0400269 pr_info("rpcrdma: removing device %s for %pIS:%u\n",
270 ia->ri_device->name,
Chuck Leverbebd0312017-04-11 13:23:10 -0400271 sap, rpc_get_port(sap));
272#endif
273 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
274 ep->rep_connected = -ENODEV;
275 xprt_force_disconnect(&xprt->rx_xprt);
276 wait_for_completion(&ia->ri_remove_done);
277
278 ia->ri_id = NULL;
279 ia->ri_pd = NULL;
280 ia->ri_device = NULL;
281 /* Return 1 to ensure the core destroys the id. */
282 return 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400283 case RDMA_CM_EVENT_ESTABLISHED:
284 connstate = 1;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400285 rpcrdma_update_connect_private(xprt, &event->param.conn);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400286 goto connected;
287 case RDMA_CM_EVENT_CONNECT_ERROR:
288 connstate = -ENOTCONN;
289 goto connected;
290 case RDMA_CM_EVENT_UNREACHABLE:
291 connstate = -ENETDOWN;
292 goto connected;
293 case RDMA_CM_EVENT_REJECTED:
Chuck Lever173b8f42017-06-08 11:53:00 -0400294 dprintk("rpcrdma: connection to %pIS:%u rejected: %s\n",
295 sap, rpc_get_port(sap),
Chuck Lever0a904872017-02-08 17:00:35 -0500296 rdma_reject_msg(id, event->status));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400297 connstate = -ECONNREFUSED;
Chuck Lever0a904872017-02-08 17:00:35 -0500298 if (event->status == IB_CM_REJ_STALE_CONN)
299 connstate = -EAGAIN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400300 goto connected;
301 case RDMA_CM_EVENT_DISCONNECTED:
302 connstate = -ECONNABORTED;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400303connected:
Chuck Lever23826c72016-03-04 11:28:27 -0500304 atomic_set(&xprt->rx_buf.rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400305 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500306 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400307 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400308 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400309 default:
Chuck Lever173b8f42017-06-08 11:53:00 -0400310 dprintk("RPC: %s: %pIS:%u on %s/%s (ep 0x%p): %s\n",
311 __func__, sap, rpc_get_port(sap),
312 ia->ri_device->name, ia->ri_ops->ro_displayname,
313 ep, rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400314 break;
315 }
316
317 return 0;
318}
319
320static struct rdma_cm_id *
321rpcrdma_create_id(struct rpcrdma_xprt *xprt,
322 struct rpcrdma_ia *ia, struct sockaddr *addr)
323{
Chuck Lever109b88a2016-11-29 10:52:40 -0500324 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400325 struct rdma_cm_id *id;
326 int rc;
327
Tom Talpey1a954052008-10-09 15:01:31 -0400328 init_completion(&ia->ri_done);
Chuck Leverbebd0312017-04-11 13:23:10 -0400329 init_completion(&ia->ri_remove_done);
Tom Talpey1a954052008-10-09 15:01:31 -0400330
Guy Shapirofa201052015-10-22 15:20:10 +0300331 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
332 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400333 if (IS_ERR(id)) {
334 rc = PTR_ERR(id);
335 dprintk("RPC: %s: rdma_create_id() failed %i\n",
336 __func__, rc);
337 return id;
338 }
339
Tom Talpey5675add2008-10-09 15:01:41 -0400340 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400341 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
342 if (rc) {
343 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
344 __func__, rc);
345 goto out;
346 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500347 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
348 if (rc < 0) {
349 dprintk("RPC: %s: wait() exited: %i\n",
350 __func__, rc);
351 goto out;
352 }
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400353
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400354 rc = ia->ri_async_rc;
355 if (rc)
356 goto out;
357
Tom Talpey5675add2008-10-09 15:01:41 -0400358 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400359 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
360 if (rc) {
361 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
362 __func__, rc);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400363 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400364 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500365 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
366 if (rc < 0) {
367 dprintk("RPC: %s: wait() exited: %i\n",
368 __func__, rc);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400369 goto out;
Chuck Lever109b88a2016-11-29 10:52:40 -0500370 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400371 rc = ia->ri_async_rc;
372 if (rc)
Chuck Lever56a6bd12017-04-11 13:23:34 -0400373 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400374
375 return id;
Chuck Lever56a6bd12017-04-11 13:23:34 -0400376
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400377out:
378 rdma_destroy_id(id);
379 return ERR_PTR(rc);
380}
381
382/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400383 * Exported functions.
384 */
385
Chuck Leverfff09592017-04-11 13:22:54 -0400386/**
387 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
388 * @xprt: controlling transport
389 * @addr: IP address of remote peer
390 *
391 * Returns 0 on success, negative errno if an appropriate
392 * Interface Adapter could not be found and opened.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400393 */
394int
Chuck Leverfff09592017-04-11 13:22:54 -0400395rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400396{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400397 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400398 int rc;
399
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400400 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
401 if (IS_ERR(ia->ri_id)) {
402 rc = PTR_ERR(ia->ri_id);
Chuck Leverfff09592017-04-11 13:22:54 -0400403 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400404 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400405 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400406
Christoph Hellwiged082d32016-09-05 12:56:17 +0200407 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400408 if (IS_ERR(ia->ri_pd)) {
409 rc = PTR_ERR(ia->ri_pd);
Chuck Leverb54054c2016-06-29 13:53:27 -0400410 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
Chuck Leverfff09592017-04-11 13:22:54 -0400411 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400412 }
413
Chuck Leverfff09592017-04-11 13:22:54 -0400414 switch (xprt_rdma_memreg_strategy) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400415 case RPCRDMA_FRMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400416 if (frwr_is_supported(ia)) {
417 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
418 break;
419 }
420 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400421 case RPCRDMA_MTHCAFMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400422 if (fmr_is_supported(ia)) {
423 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
424 break;
425 }
426 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400427 default:
Chuck Leverfff09592017-04-11 13:22:54 -0400428 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
429 ia->ri_device->name, xprt_rdma_memreg_strategy);
Chuck Leverb54054c2016-06-29 13:53:27 -0400430 rc = -EINVAL;
Chuck Leverfff09592017-04-11 13:22:54 -0400431 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400432 }
433
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400434 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500435
Chuck Leverfff09592017-04-11 13:22:54 -0400436out_err:
437 rpcrdma_ia_close(ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400438 return rc;
439}
440
Chuck Leverfff09592017-04-11 13:22:54 -0400441/**
Chuck Leverbebd0312017-04-11 13:23:10 -0400442 * rpcrdma_ia_remove - Handle device driver unload
443 * @ia: interface adapter being removed
444 *
445 * Divest transport H/W resources associated with this adapter,
446 * but allow it to be restored later.
447 */
448void
449rpcrdma_ia_remove(struct rpcrdma_ia *ia)
450{
451 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
452 rx_ia);
453 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
454 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
455 struct rpcrdma_req *req;
456 struct rpcrdma_rep *rep;
457
458 cancel_delayed_work_sync(&buf->rb_refresh_worker);
459
460 /* This is similar to rpcrdma_ep_destroy, but:
461 * - Don't cancel the connect worker.
462 * - Don't call rpcrdma_ep_disconnect, which waits
463 * for another conn upcall, which will deadlock.
464 * - rdma_disconnect is unneeded, the underlying
465 * connection is already gone.
466 */
467 if (ia->ri_id->qp) {
468 ib_drain_qp(ia->ri_id->qp);
469 rdma_destroy_qp(ia->ri_id);
470 ia->ri_id->qp = NULL;
471 }
472 ib_free_cq(ep->rep_attr.recv_cq);
473 ib_free_cq(ep->rep_attr.send_cq);
474
475 /* The ULP is responsible for ensuring all DMA
476 * mappings and MRs are gone.
477 */
478 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
479 rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
480 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
481 rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
482 rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
483 rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
484 }
485 rpcrdma_destroy_mrs(buf);
486
487 /* Allow waiters to continue */
488 complete(&ia->ri_remove_done);
489}
490
491/**
Chuck Leverfff09592017-04-11 13:22:54 -0400492 * rpcrdma_ia_close - Clean up/close an IA.
493 * @ia: interface adapter to close
494 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400495 */
496void
497rpcrdma_ia_close(struct rpcrdma_ia *ia)
498{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400499 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400500 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
501 if (ia->ri_id->qp)
502 rdma_destroy_qp(ia->ri_id);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400503 rdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400504 }
Chuck Leverfff09592017-04-11 13:22:54 -0400505 ia->ri_id = NULL;
506 ia->ri_device = NULL;
Chuck Lever6d446982015-05-26 11:51:27 -0400507
508 /* If the pd is still busy, xprtrdma missed freeing a resource */
509 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600510 ib_dealloc_pd(ia->ri_pd);
Chuck Leverfff09592017-04-11 13:22:54 -0400511 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400512}
513
514/*
515 * Create unconnected endpoint.
516 */
517int
518rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
Chuck Lever16f906d2017-02-08 17:00:10 -0500519 struct rpcrdma_create_data_internal *cdata)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400520{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400521 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
Chuck Lever16f906d2017-02-08 17:00:10 -0500522 unsigned int max_qp_wr, max_sge;
Chuck Leverfc664482014-05-28 10:33:25 -0400523 struct ib_cq *sendcq, *recvcq;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500524 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400525
Chuck Levereed50872017-03-11 15:52:47 -0500526 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
527 RPCRDMA_MAX_SEND_SGES);
Chuck Lever16f906d2017-02-08 17:00:10 -0500528 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
529 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
Chuck Leverb3221d62015-08-03 13:03:39 -0400530 return -ENOMEM;
531 }
Chuck Lever16f906d2017-02-08 17:00:10 -0500532 ia->ri_max_send_sges = max_sge - RPCRDMA_MIN_SEND_SGES;
Chuck Leverb3221d62015-08-03 13:03:39 -0400533
Or Gerlitze3e45b12015-12-18 10:59:48 +0200534 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400535 dprintk("RPC: %s: insufficient wqe's available\n",
536 __func__);
537 return -ENOMEM;
538 }
Chuck Lever550d7502016-05-02 14:41:47 -0400539 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
Chuck Lever124fa172015-10-24 17:27:51 -0400540
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400541 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400542 if (cdata->max_requests > max_qp_wr)
543 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400544
545 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
546 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400547 ep->rep_attr.srq = NULL;
548 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400549 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400550 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
Chuck Lever3968cb52015-03-30 14:35:26 -0400551 rc = ia->ri_ops->ro_open(ia, ep, cdata);
552 if (rc)
553 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400554 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400555 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400556 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
Chuck Lever16f906d2017-02-08 17:00:10 -0500557 ep->rep_attr.cap.max_send_sge = max_sge;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400558 ep->rep_attr.cap.max_recv_sge = 1;
559 ep->rep_attr.cap.max_inline_data = 0;
560 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
561 ep->rep_attr.qp_type = IB_QPT_RC;
562 ep->rep_attr.port_num = ~0;
563
564 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
565 "iovs: send %d recv %d\n",
566 __func__,
567 ep->rep_attr.cap.max_send_wr,
568 ep->rep_attr.cap.max_recv_wr,
569 ep->rep_attr.cap.max_send_sge,
570 ep->rep_attr.cap.max_recv_sge);
571
572 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400573 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500574 if (ep->rep_cqinit <= 2)
575 ep->rep_cqinit = 0; /* always signal? */
Chuck Lever8d38de62016-11-29 10:52:16 -0500576 rpcrdma_init_cqcount(ep, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400577 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400578 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400579
Chuck Lever2fa8f882016-03-04 11:28:53 -0500580 sendcq = ib_alloc_cq(ia->ri_device, NULL,
581 ep->rep_attr.cap.max_send_wr + 1,
582 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400583 if (IS_ERR(sendcq)) {
584 rc = PTR_ERR(sendcq);
585 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400586 __func__, rc);
587 goto out1;
588 }
589
Chuck Lever552bf222016-03-04 11:28:36 -0500590 recvcq = ib_alloc_cq(ia->ri_device, NULL,
591 ep->rep_attr.cap.max_recv_wr + 1,
592 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400593 if (IS_ERR(recvcq)) {
594 rc = PTR_ERR(recvcq);
595 dprintk("RPC: %s: failed to create recv CQ: %i\n",
596 __func__, rc);
597 goto out2;
598 }
599
Chuck Leverfc664482014-05-28 10:33:25 -0400600 ep->rep_attr.send_cq = sendcq;
601 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400602
603 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400604 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400605
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400606 /* Prepare RDMA-CM private message */
607 pmsg->cp_magic = rpcrdma_cmp_magic;
608 pmsg->cp_version = RPCRDMA_CMP_VERSION;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400609 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400610 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
611 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
612 ep->rep_remote_cma.private_data = pmsg;
613 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400614
615 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400616 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200617 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400618 ep->rep_remote_cma.responder_resources = 32;
619 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500620 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200621 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400622
Chuck Leverb2dde942016-05-02 14:43:03 -0400623 /* Limit transport retries so client can detect server
624 * GID changes quickly. RPC layer handles re-establishing
625 * transport connection and retransmission.
626 */
627 ep->rep_remote_cma.retry_count = 6;
628
629 /* RPC-over-RDMA handles its own flow control. In addition,
630 * make all RNR NAKs visible so we know that RPC-over-RDMA
631 * flow control is working correctly (no NAKs should be seen).
632 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400633 ep->rep_remote_cma.flow_control = 0;
634 ep->rep_remote_cma.rnr_retry_count = 0;
635
636 return 0;
637
638out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500639 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400640out1:
641 return rc;
642}
643
644/*
645 * rpcrdma_ep_destroy
646 *
647 * Disconnect and destroy endpoint. After this, the only
648 * valid operations on the ep are to free it (if dynamically
649 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400650 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400651void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400652rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
653{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400654 dprintk("RPC: %s: entering, connected is %d\n",
655 __func__, ep->rep_connected);
656
Chuck Lever254f91e2014-05-28 10:32:17 -0400657 cancel_delayed_work_sync(&ep->rep_connect_worker);
658
Steve Wise72c02172015-09-21 12:24:23 -0500659 if (ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400660 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400661 rdma_destroy_qp(ia->ri_id);
662 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400663 }
664
Chuck Lever552bf222016-03-04 11:28:36 -0500665 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500666 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400667}
668
Chuck Levera9b0e382017-04-11 13:23:26 -0400669/* Re-establish a connection after a device removal event.
670 * Unlike a normal reconnection, a fresh PD and a new set
671 * of MRs and buffers is needed.
672 */
673static int
674rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
675 struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
676{
677 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
678 int rc, err;
679
680 pr_info("%s: r_xprt = %p\n", __func__, r_xprt);
681
682 rc = -EHOSTUNREACH;
683 if (rpcrdma_ia_open(r_xprt, sap))
684 goto out1;
685
686 rc = -ENOMEM;
687 err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
688 if (err) {
689 pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
690 goto out2;
691 }
692
693 rc = -ENETUNREACH;
694 err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
695 if (err) {
696 pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
697 goto out3;
698 }
699
700 rpcrdma_create_mrs(r_xprt);
701 return 0;
702
703out3:
704 rpcrdma_ep_destroy(ep, ia);
705out2:
706 rpcrdma_ia_close(ia);
707out1:
708 return rc;
709}
710
Chuck Lever18908962017-04-11 13:23:18 -0400711static int
712rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
713 struct rpcrdma_ia *ia)
714{
715 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
716 struct rdma_cm_id *id, *old;
717 int err, rc;
718
719 dprintk("RPC: %s: reconnecting...\n", __func__);
720
721 rpcrdma_ep_disconnect(ep, ia);
722
723 rc = -EHOSTUNREACH;
724 id = rpcrdma_create_id(r_xprt, ia, sap);
725 if (IS_ERR(id))
726 goto out;
727
728 /* As long as the new ID points to the same device as the
729 * old ID, we can reuse the transport's existing PD and all
730 * previously allocated MRs. Also, the same device means
731 * the transport's previous DMA mappings are still valid.
732 *
733 * This is a sanity check only. There should be no way these
734 * point to two different devices here.
735 */
736 old = id;
737 rc = -ENETUNREACH;
738 if (ia->ri_device != id->device) {
739 pr_err("rpcrdma: can't reconnect on different device!\n");
740 goto out_destroy;
741 }
742
743 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
744 if (err) {
745 dprintk("RPC: %s: rdma_create_qp returned %d\n",
746 __func__, err);
747 goto out_destroy;
748 }
749
750 /* Atomically replace the transport's ID and QP. */
751 rc = 0;
752 old = ia->ri_id;
753 ia->ri_id = id;
754 rdma_destroy_qp(old);
755
756out_destroy:
Chuck Lever56a6bd12017-04-11 13:23:34 -0400757 rdma_destroy_id(old);
Chuck Lever18908962017-04-11 13:23:18 -0400758out:
759 return rc;
760}
761
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400762/*
763 * Connect unconnected endpoint.
764 */
765int
766rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
767{
Chuck Lever0a904872017-02-08 17:00:35 -0500768 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
769 rx_ia);
Chuck Lever0a904872017-02-08 17:00:35 -0500770 unsigned int extras;
Chuck Lever18908962017-04-11 13:23:18 -0400771 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400772
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400773retry:
Chuck Lever18908962017-04-11 13:23:18 -0400774 switch (ep->rep_connected) {
775 case 0:
Chuck Leverec62f402014-05-28 10:34:07 -0400776 dprintk("RPC: %s: connecting...\n", __func__);
777 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
778 if (rc) {
779 dprintk("RPC: %s: rdma_create_qp failed %i\n",
780 __func__, rc);
Chuck Lever18908962017-04-11 13:23:18 -0400781 rc = -ENETUNREACH;
782 goto out_noupdate;
Chuck Leverec62f402014-05-28 10:34:07 -0400783 }
Chuck Lever18908962017-04-11 13:23:18 -0400784 break;
Chuck Levera9b0e382017-04-11 13:23:26 -0400785 case -ENODEV:
786 rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
787 if (rc)
788 goto out_noupdate;
789 break;
Chuck Lever18908962017-04-11 13:23:18 -0400790 default:
791 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
792 if (rc)
793 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400794 }
795
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400796 ep->rep_connected = 0;
797
798 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
799 if (rc) {
800 dprintk("RPC: %s: rdma_connect() failed with %i\n",
801 __func__, rc);
802 goto out;
803 }
804
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400805 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400806 if (ep->rep_connected <= 0) {
Chuck Lever0a904872017-02-08 17:00:35 -0500807 if (ep->rep_connected == -EAGAIN)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400808 goto retry;
809 rc = ep->rep_connected;
Chuck Lever0a904872017-02-08 17:00:35 -0500810 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400811 }
812
Chuck Lever0a904872017-02-08 17:00:35 -0500813 dprintk("RPC: %s: connected\n", __func__);
814 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
815 if (extras)
816 rpcrdma_ep_post_extra_recv(r_xprt, extras);
817
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400818out:
819 if (rc)
820 ep->rep_connected = rc;
Chuck Lever18908962017-04-11 13:23:18 -0400821
822out_noupdate:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400823 return rc;
824}
825
826/*
827 * rpcrdma_ep_disconnect
828 *
829 * This is separate from destroy to facilitate the ability
830 * to reconnect without recreating the endpoint.
831 *
832 * This call is not reentrant, and must not be made in parallel
833 * on the same endpoint.
834 */
Chuck Lever282191c2014-07-29 17:25:55 -0400835void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400836rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
837{
838 int rc;
839
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400840 rc = rdma_disconnect(ia->ri_id);
841 if (!rc) {
842 /* returns without wait if not connected */
843 wait_event_interruptible(ep->rep_connect_wait,
844 ep->rep_connected != 1);
845 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
846 (ep->rep_connected == 1) ? "still " : "dis");
847 } else {
848 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
849 ep->rep_connected = rc;
850 }
Chuck Lever550d7502016-05-02 14:41:47 -0400851
852 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400853}
854
Chuck Lever505bbe62016-06-29 13:52:54 -0400855static void
856rpcrdma_mr_recovery_worker(struct work_struct *work)
857{
858 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
859 rb_recovery_worker.work);
860 struct rpcrdma_mw *mw;
861
862 spin_lock(&buf->rb_recovery_lock);
863 while (!list_empty(&buf->rb_stale_mrs)) {
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500864 mw = rpcrdma_pop_mw(&buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400865 spin_unlock(&buf->rb_recovery_lock);
866
867 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
868 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
869
870 spin_lock(&buf->rb_recovery_lock);
kbuild test robot53d78522016-07-16 06:02:05 +0800871 }
Chuck Lever505bbe62016-06-29 13:52:54 -0400872 spin_unlock(&buf->rb_recovery_lock);
873}
874
875void
876rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
877{
878 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
879 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
880
881 spin_lock(&buf->rb_recovery_lock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500882 rpcrdma_push_mw(mw, &buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400883 spin_unlock(&buf->rb_recovery_lock);
884
885 schedule_delayed_work(&buf->rb_recovery_worker, 0);
886}
887
Chuck Levere2ac2362016-06-29 13:54:00 -0400888static void
889rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
890{
891 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
892 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
893 unsigned int count;
894 LIST_HEAD(free);
895 LIST_HEAD(all);
896
897 for (count = 0; count < 32; count++) {
898 struct rpcrdma_mw *mw;
899 int rc;
900
901 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
902 if (!mw)
903 break;
904
905 rc = ia->ri_ops->ro_init_mr(ia, mw);
906 if (rc) {
907 kfree(mw);
908 break;
909 }
910
911 mw->mw_xprt = r_xprt;
912
913 list_add(&mw->mw_list, &free);
914 list_add(&mw->mw_all, &all);
915 }
916
917 spin_lock(&buf->rb_mwlock);
918 list_splice(&free, &buf->rb_mws);
919 list_splice(&all, &buf->rb_all);
920 r_xprt->rx_stats.mrs_allocated += count;
921 spin_unlock(&buf->rb_mwlock);
922
923 dprintk("RPC: %s: created %u MRs\n", __func__, count);
924}
925
926static void
927rpcrdma_mr_refresh_worker(struct work_struct *work)
928{
929 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
930 rb_refresh_worker.work);
931 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
932 rx_buf);
933
934 rpcrdma_create_mrs(r_xprt);
935}
936
Chuck Leverf531a5d2015-10-24 17:27:43 -0400937struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500938rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
939{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400940 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500941 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500942
Chuck Lever85275c82015-01-21 11:04:16 -0500943 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500944 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500945 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500946
Chuck Leverf531a5d2015-10-24 17:27:43 -0400947 spin_lock(&buffer->rb_reqslock);
948 list_add(&req->rl_all, &buffer->rb_allreqs);
949 spin_unlock(&buffer->rb_reqslock);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500950 req->rl_cqe.done = rpcrdma_wc_send;
Chuck Lever13924022015-01-21 11:03:52 -0500951 req->rl_buffer = &r_xprt->rx_buf;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400952 INIT_LIST_HEAD(&req->rl_registered);
Chuck Lever90aab602016-09-15 10:56:43 -0400953 req->rl_send_wr.next = NULL;
954 req->rl_send_wr.wr_cqe = &req->rl_cqe;
Chuck Lever655fec62016-09-15 10:57:24 -0400955 req->rl_send_wr.sg_list = req->rl_send_sge;
Chuck Lever90aab602016-09-15 10:56:43 -0400956 req->rl_send_wr.opcode = IB_WR_SEND;
Chuck Lever13924022015-01-21 11:03:52 -0500957 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500958}
959
Chuck Leverf531a5d2015-10-24 17:27:43 -0400960struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -0500961rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
962{
963 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500964 struct rpcrdma_rep *rep;
965 int rc;
966
967 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500968 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500969 if (rep == NULL)
970 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500971
Chuck Lever13650c22016-09-15 10:56:26 -0400972 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -0400973 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever6b1184c2015-01-21 11:04:25 -0500974 if (IS_ERR(rep->rr_rdmabuf)) {
975 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500976 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500977 }
Chuck Lever96f87782017-08-03 14:30:03 -0400978 xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base,
979 rdmab_length(rep->rr_rdmabuf));
Chuck Lever13924022015-01-21 11:03:52 -0500980
Chuck Lever1519e962016-09-15 10:57:49 -0400981 rep->rr_cqe.done = rpcrdma_wc_receive;
Chuck Leverfed171b2015-05-26 11:51:37 -0400982 rep->rr_rxprt = r_xprt;
Chuck Lever496b77a2016-09-15 10:57:57 -0400983 INIT_WORK(&rep->rr_work, rpcrdma_reply_handler);
Chuck Lever6ea8e712016-09-15 10:56:51 -0400984 rep->rr_recv_wr.next = NULL;
985 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
986 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
987 rep->rr_recv_wr.num_sge = 1;
Chuck Lever13924022015-01-21 11:03:52 -0500988 return rep;
989
990out_free:
991 kfree(rep);
992out:
993 return ERR_PTR(rc);
994}
995
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400996int
Chuck Leverac920d02015-01-21 11:03:44 -0500997rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400998{
Chuck Leverac920d02015-01-21 11:03:44 -0500999 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001000 int i, rc;
1001
Chuck Lever1e465fd2015-10-24 17:27:02 -04001002 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001003 buf->rb_bc_srv_max_requests = 0;
Chuck Lever23826c72016-03-04 11:28:27 -05001004 atomic_set(&buf->rb_credits, 1);
Chuck Levere2ac2362016-06-29 13:54:00 -04001005 spin_lock_init(&buf->rb_mwlock);
Chuck Lever505bbe62016-06-29 13:52:54 -04001006 spin_lock_init(&buf->rb_lock);
1007 spin_lock_init(&buf->rb_recovery_lock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001008 INIT_LIST_HEAD(&buf->rb_mws);
1009 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever431af642017-06-08 11:52:20 -04001010 INIT_LIST_HEAD(&buf->rb_pending);
Chuck Lever505bbe62016-06-29 13:52:54 -04001011 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001012 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1013 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001014 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
1015 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001016
Chuck Levere2ac2362016-06-29 13:54:00 -04001017 rpcrdma_create_mrs(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001018
Chuck Lever1e465fd2015-10-24 17:27:02 -04001019 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001020 INIT_LIST_HEAD(&buf->rb_allreqs);
1021 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001022 for (i = 0; i < buf->rb_max_requests; i++) {
1023 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001024
Chuck Lever13924022015-01-21 11:03:52 -05001025 req = rpcrdma_create_req(r_xprt);
1026 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001027 dprintk("RPC: %s: request buffer %d alloc"
1028 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001029 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001030 goto out;
1031 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001032 req->rl_backchannel = false;
Chuck Levera80d66c2017-06-08 11:52:12 -04001033 list_add(&req->rl_list, &buf->rb_send_bufs);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001034 }
1035
1036 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001037 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001038 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001039
Chuck Lever13924022015-01-21 11:03:52 -05001040 rep = rpcrdma_create_rep(r_xprt);
1041 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001042 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1043 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001044 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001045 goto out;
1046 }
Chuck Lever1e465fd2015-10-24 17:27:02 -04001047 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001048 }
Chuck Lever13924022015-01-21 11:03:52 -05001049
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001050 return 0;
1051out:
1052 rpcrdma_buffer_destroy(buf);
1053 return rc;
1054}
1055
Chuck Lever1e465fd2015-10-24 17:27:02 -04001056static struct rpcrdma_req *
1057rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1058{
1059 struct rpcrdma_req *req;
1060
1061 req = list_first_entry(&buf->rb_send_bufs,
Chuck Levera80d66c2017-06-08 11:52:12 -04001062 struct rpcrdma_req, rl_list);
Chuck Lever431af642017-06-08 11:52:20 -04001063 list_del_init(&req->rl_list);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001064 return req;
1065}
1066
1067static struct rpcrdma_rep *
1068rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1069{
1070 struct rpcrdma_rep *rep;
1071
1072 rep = list_first_entry(&buf->rb_recv_bufs,
1073 struct rpcrdma_rep, rr_list);
1074 list_del(&rep->rr_list);
1075 return rep;
1076}
1077
Chuck Lever2e845222014-07-29 17:25:38 -04001078static void
Chuck Lever13650c22016-09-15 10:56:26 -04001079rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -05001080{
Chuck Lever13650c22016-09-15 10:56:26 -04001081 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001082 kfree(rep);
1083}
1084
Chuck Leverf531a5d2015-10-24 17:27:43 -04001085void
Chuck Lever13650c22016-09-15 10:56:26 -04001086rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -05001087{
Chuck Lever13650c22016-09-15 10:56:26 -04001088 rpcrdma_free_regbuf(req->rl_recvbuf);
1089 rpcrdma_free_regbuf(req->rl_sendbuf);
1090 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001091 kfree(req);
1092}
1093
Chuck Levere2ac2362016-06-29 13:54:00 -04001094static void
1095rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1096{
1097 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1098 rx_buf);
1099 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1100 struct rpcrdma_mw *mw;
1101 unsigned int count;
1102
1103 count = 0;
1104 spin_lock(&buf->rb_mwlock);
1105 while (!list_empty(&buf->rb_all)) {
1106 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1107 list_del(&mw->mw_all);
1108
1109 spin_unlock(&buf->rb_mwlock);
1110 ia->ri_ops->ro_release_mr(mw);
1111 count++;
1112 spin_lock(&buf->rb_mwlock);
1113 }
1114 spin_unlock(&buf->rb_mwlock);
1115 r_xprt->rx_stats.mrs_allocated = 0;
1116
1117 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1118}
1119
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001120void
1121rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1122{
Chuck Lever505bbe62016-06-29 13:52:54 -04001123 cancel_delayed_work_sync(&buf->rb_recovery_worker);
Chuck Lever9378b272017-04-11 13:22:29 -04001124 cancel_delayed_work_sync(&buf->rb_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001125
Chuck Lever1e465fd2015-10-24 17:27:02 -04001126 while (!list_empty(&buf->rb_recv_bufs)) {
1127 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001128
Chuck Lever1e465fd2015-10-24 17:27:02 -04001129 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001130 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001131 }
Chuck Lever05c97462016-09-06 11:22:58 -04001132 buf->rb_send_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001133
Chuck Leverf531a5d2015-10-24 17:27:43 -04001134 spin_lock(&buf->rb_reqslock);
1135 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001136 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001137
Chuck Leverf531a5d2015-10-24 17:27:43 -04001138 req = list_first_entry(&buf->rb_allreqs,
1139 struct rpcrdma_req, rl_all);
1140 list_del(&req->rl_all);
1141
1142 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001143 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001144 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001145 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001146 spin_unlock(&buf->rb_reqslock);
Chuck Lever05c97462016-09-06 11:22:58 -04001147 buf->rb_recv_count = 0;
Chuck Lever9f9d8022014-07-29 17:24:45 -04001148
Chuck Levere2ac2362016-06-29 13:54:00 -04001149 rpcrdma_destroy_mrs(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001150}
1151
Chuck Lever346aa662015-05-26 11:52:06 -04001152struct rpcrdma_mw *
1153rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001154{
Chuck Lever346aa662015-05-26 11:52:06 -04001155 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1156 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001157
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001158 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001159 if (!list_empty(&buf->rb_mws))
1160 mw = rpcrdma_pop_mw(&buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001161 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001162
1163 if (!mw)
Chuck Levere2ac2362016-06-29 13:54:00 -04001164 goto out_nomws;
Chuck Lever4b196dc62017-06-08 11:51:56 -04001165 mw->mw_flags = 0;
Chuck Lever346aa662015-05-26 11:52:06 -04001166 return mw;
Chuck Levere2ac2362016-06-29 13:54:00 -04001167
1168out_nomws:
1169 dprintk("RPC: %s: no MWs available\n", __func__);
Chuck Leverbebd0312017-04-11 13:23:10 -04001170 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1171 schedule_delayed_work(&buf->rb_refresh_worker, 0);
Chuck Levere2ac2362016-06-29 13:54:00 -04001172
1173 /* Allow the reply handler and refresh worker to run */
1174 cond_resched();
1175
1176 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001177}
1178
Chuck Lever346aa662015-05-26 11:52:06 -04001179void
1180rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001181{
Chuck Lever346aa662015-05-26 11:52:06 -04001182 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001183
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001184 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001185 rpcrdma_push_mw(mw, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001186 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001187}
1188
Chuck Lever05c97462016-09-06 11:22:58 -04001189static struct rpcrdma_rep *
1190rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1191{
1192 /* If an RPC previously completed without a reply (say, a
1193 * credential problem or a soft timeout occurs) then hold off
1194 * on supplying more Receive buffers until the number of new
1195 * pending RPCs catches up to the number of posted Receives.
1196 */
1197 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1198 return NULL;
1199
1200 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1201 return NULL;
1202 buffers->rb_recv_count++;
1203 return rpcrdma_buffer_get_rep_locked(buffers);
1204}
1205
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001206/*
1207 * Get a set of request/reply buffers.
Chuck Lever78d506e2016-09-06 11:22:49 -04001208 *
1209 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001210 */
1211struct rpcrdma_req *
1212rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1213{
1214 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001215
Chuck Levera5b027e2015-10-24 17:27:27 -04001216 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001217 if (list_empty(&buffers->rb_send_bufs))
1218 goto out_reqbuf;
Chuck Lever05c97462016-09-06 11:22:58 -04001219 buffers->rb_send_count++;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001220 req = rpcrdma_buffer_get_req_locked(buffers);
Chuck Lever05c97462016-09-06 11:22:58 -04001221 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001222 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001223 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001224
Chuck Lever1e465fd2015-10-24 17:27:02 -04001225out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001226 spin_unlock(&buffers->rb_lock);
Chuck Lever78d506e2016-09-06 11:22:49 -04001227 pr_warn("RPC: %s: out of request buffers\n", __func__);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001228 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001229}
1230
1231/*
1232 * Put request/reply buffers back into pool.
1233 * Pre-decrement counter/array index.
1234 */
1235void
1236rpcrdma_buffer_put(struct rpcrdma_req *req)
1237{
1238 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001239 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001240
Chuck Lever90aab602016-09-15 10:56:43 -04001241 req->rl_send_wr.num_sge = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001242 req->rl_reply = NULL;
1243
Chuck Levera5b027e2015-10-24 17:27:27 -04001244 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001245 buffers->rb_send_count--;
Chuck Levera80d66c2017-06-08 11:52:12 -04001246 list_add_tail(&req->rl_list, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001247 if (rep) {
1248 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001249 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001250 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001251 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001252}
1253
1254/*
1255 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001256 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001257 */
1258void
1259rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1260{
1261 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001262
Chuck Levera5b027e2015-10-24 17:27:27 -04001263 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001264 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001265 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001266}
1267
1268/*
1269 * Put reply buffers back into pool when not attached to
1270 * request. This happens in error conditions.
1271 */
1272void
1273rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1274{
Chuck Leverfed171b2015-05-26 11:51:37 -04001275 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001276
Chuck Levera5b027e2015-10-24 17:27:27 -04001277 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001278 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001279 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001280 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001281}
1282
Chuck Lever9128c3e2015-01-21 11:04:00 -05001283/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001284 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001285 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001286 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001287 * @flags: GFP flags
1288 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001289 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1290 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001291 *
1292 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001293 * receiving the payload of RDMA RECV operations. During Long Calls
1294 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001295 */
1296struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001297rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1298 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001299{
1300 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001301
Chuck Lever9128c3e2015-01-21 11:04:00 -05001302 rb = kmalloc(sizeof(*rb) + size, flags);
1303 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001304 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001305
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001306 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001307 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001308 rb->rg_iov.length = size;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001309
1310 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001311}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001312
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001313/**
1314 * __rpcrdma_map_regbuf - DMA-map a regbuf
1315 * @ia: controlling rpcrdma_ia
1316 * @rb: regbuf to be mapped
1317 */
1318bool
1319__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1320{
Chuck Lever91a10c52017-04-11 13:23:02 -04001321 struct ib_device *device = ia->ri_device;
1322
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001323 if (rb->rg_direction == DMA_NONE)
1324 return false;
1325
Chuck Lever91a10c52017-04-11 13:23:02 -04001326 rb->rg_iov.addr = ib_dma_map_single(device,
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001327 (void *)rb->rg_base,
1328 rdmab_length(rb),
1329 rb->rg_direction);
Chuck Lever91a10c52017-04-11 13:23:02 -04001330 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001331 return false;
1332
Chuck Lever91a10c52017-04-11 13:23:02 -04001333 rb->rg_device = device;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001334 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1335 return true;
1336}
1337
1338static void
1339rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1340{
1341 if (!rpcrdma_regbuf_is_mapped(rb))
1342 return;
1343
1344 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1345 rdmab_length(rb), rb->rg_direction);
1346 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001347}
1348
1349/**
1350 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001351 * @rb: regbuf to be deregistered and freed
1352 */
1353void
Chuck Lever13650c22016-09-15 10:56:26 -04001354rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001355{
Chuck Levere531dca2015-08-03 13:03:20 -04001356 if (!rb)
1357 return;
1358
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001359 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001360 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001361}
1362
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001363/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001364 * Prepost any receive buffer, then post send.
1365 *
1366 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1367 */
1368int
1369rpcrdma_ep_post(struct rpcrdma_ia *ia,
1370 struct rpcrdma_ep *ep,
1371 struct rpcrdma_req *req)
1372{
Chuck Lever90aab602016-09-15 10:56:43 -04001373 struct ib_send_wr *send_wr = &req->rl_send_wr;
1374 struct ib_send_wr *send_wr_fail;
Chuck Lever655fec62016-09-15 10:57:24 -04001375 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001376
Chuck Lever90aab602016-09-15 10:56:43 -04001377 if (req->rl_reply) {
1378 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001379 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001380 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001381 req->rl_reply = NULL;
1382 }
1383
Chuck Leverb3221d62015-08-03 13:03:39 -04001384 dprintk("RPC: %s: posting %d s/g entries\n",
Chuck Lever90aab602016-09-15 10:56:43 -04001385 __func__, send_wr->num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001386
Chuck Lever8d38de62016-11-29 10:52:16 -05001387 rpcrdma_set_signaled(ep, send_wr);
Chuck Lever90aab602016-09-15 10:56:43 -04001388 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001389 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001390 goto out_postsend_err;
1391 return 0;
1392
1393out_postsend_err:
1394 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1395 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001396}
1397
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001398int
1399rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001400 struct rpcrdma_rep *rep)
1401{
Chuck Lever6ea8e712016-09-15 10:56:51 -04001402 struct ib_recv_wr *recv_wr_fail;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001403 int rc;
1404
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001405 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1406 goto out_map;
Chuck Lever6ea8e712016-09-15 10:56:51 -04001407 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001408 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001409 goto out_postrecv;
1410 return 0;
1411
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001412out_map:
1413 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1414 return -EIO;
1415
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001416out_postrecv:
1417 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1418 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001419}
Chuck Lever43e95982014-07-29 17:23:34 -04001420
Chuck Leverf531a5d2015-10-24 17:27:43 -04001421/**
1422 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1423 * @r_xprt: transport associated with these backchannel resources
1424 * @min_reqs: minimum number of incoming requests expected
1425 *
1426 * Returns zero if all requested buffers were posted, or a negative errno.
1427 */
1428int
1429rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1430{
1431 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1432 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001433 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001434 int rc;
1435
1436 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001437 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001438 if (list_empty(&buffers->rb_recv_bufs))
1439 goto out_reqbuf;
1440 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001441 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001442
Chuck Leverb1573802016-09-15 10:56:35 -04001443 rc = rpcrdma_ep_post_recv(ia, rep);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001444 if (rc)
1445 goto out_rc;
1446 }
1447
1448 return 0;
1449
1450out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001451 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001452 pr_warn("%s: no extra receive buffers\n", __func__);
1453 return -ENOMEM;
1454
1455out_rc:
1456 rpcrdma_recv_buffer_put(rep);
1457 return rc;
1458}