blob: d45695408df3b4d7ab70bc6bc90f0dc738cf03e6 [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 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{
Chuck Lever23826c72016-03-04 11:28:27 -0500142 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
Chuck Leverc1bcb682017-08-03 14:30:52 -0400143 __be32 *p = rep->rr_rdmabuf->rg_base;
Chuck Lever23826c72016-03-04 11:28:27 -0500144 u32 credits;
145
Chuck Leverc1bcb682017-08-03 14:30:52 -0400146 credits = be32_to_cpup(p + 2);
Chuck Lever23826c72016-03-04 11:28:27 -0500147 if (credits == 0)
148 credits = 1; /* don't deadlock */
149 else if (credits > buffer->rb_max_requests)
150 credits = buffer->rb_max_requests;
151
152 atomic_set(&buffer->rb_credits, credits);
153}
154
Chuck Lever552bf222016-03-04 11:28:36 -0500155/**
Chuck Lever1519e962016-09-15 10:57:49 -0400156 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
Chuck Lever552bf222016-03-04 11:28:36 -0500157 * @cq: completion queue (ignored)
158 * @wc: completed WR
159 *
160 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400161static void
Chuck Lever1519e962016-09-15 10:57:49 -0400162rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400163{
Chuck Lever552bf222016-03-04 11:28:36 -0500164 struct ib_cqe *cqe = wc->wr_cqe;
165 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
166 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400167
Chuck Lever85024272015-01-21 11:02:04 -0500168 /* WARNING: Only wr_id and status are reliable at this point */
169 if (wc->status != IB_WC_SUCCESS)
170 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400171
Chuck Lever85024272015-01-21 11:02:04 -0500172 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Lever85024272015-01-21 11:02:04 -0500173 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
174 __func__, rep, wc->byte_len);
175
Chuck Lever96f87782017-08-03 14:30:03 -0400176 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len);
Chuck Leverc8b920b2016-09-15 10:57:16 -0400177 rep->rr_wc_flags = wc->wc_flags;
178 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
179
Chuck Lever91a10c52017-04-11 13:23:02 -0400180 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
Chuck Lever6b1184c2015-01-21 11:04:25 -0500181 rdmab_addr(rep->rr_rdmabuf),
Chuck Levere2a67192017-08-03 14:30:44 -0400182 wc->byte_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500183
Chuck Levere2a67192017-08-03 14:30:44 -0400184 if (wc->byte_len >= RPCRDMA_HDRLEN_ERR)
185 rpcrdma_update_granted_credits(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400186
187out_schedule:
Chuck Leverd8f532d2017-10-16 15:01:30 -0400188 rpcrdma_reply_handler(rep);
Chuck Lever85024272015-01-21 11:02:04 -0500189 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400190
Chuck Lever85024272015-01-21 11:02:04 -0500191out_fail:
192 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500193 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
194 ib_wc_status_msg(wc->status),
195 wc->status, wc->vendor_err);
Chuck Levere2a67192017-08-03 14:30:44 -0400196 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, 0);
Chuck Lever85024272015-01-21 11:02:04 -0500197 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400198}
199
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400200static void
201rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
202 struct rdma_conn_param *param)
203{
204 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
205 const struct rpcrdma_connect_private *pmsg = param->private_data;
206 unsigned int rsize, wsize;
207
Chuck Leverc8b920b2016-09-15 10:57:16 -0400208 /* Default settings for RPC-over-RDMA Version One */
209 r_xprt->rx_ia.ri_reminv_expected = false;
Chuck Leverb5f0afb2017-02-08 16:59:54 -0500210 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400211 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
212 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
213
214 if (pmsg &&
215 pmsg->cp_magic == rpcrdma_cmp_magic &&
216 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
Chuck Leverc8b920b2016-09-15 10:57:16 -0400217 r_xprt->rx_ia.ri_reminv_expected = true;
Chuck Leverc95a3c62017-02-08 17:00:02 -0500218 r_xprt->rx_ia.ri_implicit_roundup = true;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400219 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
220 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
221 }
222
223 if (rsize < cdata->inline_rsize)
224 cdata->inline_rsize = rsize;
225 if (wsize < cdata->inline_wsize)
226 cdata->inline_wsize = wsize;
Chuck Lever6d6bf722016-11-29 10:53:13 -0500227 dprintk("RPC: %s: max send %u, max recv %u\n",
228 __func__, cdata->inline_wsize, cdata->inline_rsize);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400229 rpcrdma_set_max_header_sizes(r_xprt);
230}
231
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400232static int
233rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
234{
235 struct rpcrdma_xprt *xprt = id->context;
236 struct rpcrdma_ia *ia = &xprt->rx_ia;
237 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500238#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400239 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800240#endif
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400241 int connstate = 0;
242
243 switch (event->event) {
244 case RDMA_CM_EVENT_ADDR_RESOLVED:
245 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400246 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400247 complete(&ia->ri_done);
248 break;
249 case RDMA_CM_EVENT_ADDR_ERROR:
250 ia->ri_async_rc = -EHOSTUNREACH;
251 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
252 __func__, ep);
253 complete(&ia->ri_done);
254 break;
255 case RDMA_CM_EVENT_ROUTE_ERROR:
256 ia->ri_async_rc = -ENETUNREACH;
257 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
258 __func__, ep);
259 complete(&ia->ri_done);
260 break;
Chuck Leverbebd0312017-04-11 13:23:10 -0400261 case RDMA_CM_EVENT_DEVICE_REMOVAL:
262#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever173b8f42017-06-08 11:53:00 -0400263 pr_info("rpcrdma: removing device %s for %pIS:%u\n",
264 ia->ri_device->name,
Chuck Leverbebd0312017-04-11 13:23:10 -0400265 sap, rpc_get_port(sap));
266#endif
267 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
268 ep->rep_connected = -ENODEV;
269 xprt_force_disconnect(&xprt->rx_xprt);
270 wait_for_completion(&ia->ri_remove_done);
271
272 ia->ri_id = NULL;
273 ia->ri_pd = NULL;
274 ia->ri_device = NULL;
275 /* Return 1 to ensure the core destroys the id. */
276 return 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400277 case RDMA_CM_EVENT_ESTABLISHED:
278 connstate = 1;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400279 rpcrdma_update_connect_private(xprt, &event->param.conn);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400280 goto connected;
281 case RDMA_CM_EVENT_CONNECT_ERROR:
282 connstate = -ENOTCONN;
283 goto connected;
284 case RDMA_CM_EVENT_UNREACHABLE:
285 connstate = -ENETDOWN;
286 goto connected;
287 case RDMA_CM_EVENT_REJECTED:
Chuck Lever173b8f42017-06-08 11:53:00 -0400288 dprintk("rpcrdma: connection to %pIS:%u rejected: %s\n",
289 sap, rpc_get_port(sap),
Chuck Lever0a904872017-02-08 17:00:35 -0500290 rdma_reject_msg(id, event->status));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400291 connstate = -ECONNREFUSED;
Chuck Lever0a904872017-02-08 17:00:35 -0500292 if (event->status == IB_CM_REJ_STALE_CONN)
293 connstate = -EAGAIN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400294 goto connected;
295 case RDMA_CM_EVENT_DISCONNECTED:
296 connstate = -ECONNABORTED;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400297connected:
Chuck Lever23826c72016-03-04 11:28:27 -0500298 atomic_set(&xprt->rx_buf.rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400299 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500300 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400301 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400302 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400303 default:
Chuck Lever173b8f42017-06-08 11:53:00 -0400304 dprintk("RPC: %s: %pIS:%u on %s/%s (ep 0x%p): %s\n",
305 __func__, sap, rpc_get_port(sap),
306 ia->ri_device->name, ia->ri_ops->ro_displayname,
307 ep, rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400308 break;
309 }
310
311 return 0;
312}
313
314static struct rdma_cm_id *
315rpcrdma_create_id(struct rpcrdma_xprt *xprt,
316 struct rpcrdma_ia *ia, struct sockaddr *addr)
317{
Chuck Lever109b88a2016-11-29 10:52:40 -0500318 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400319 struct rdma_cm_id *id;
320 int rc;
321
Tom Talpey1a954052008-10-09 15:01:31 -0400322 init_completion(&ia->ri_done);
Chuck Leverbebd0312017-04-11 13:23:10 -0400323 init_completion(&ia->ri_remove_done);
Tom Talpey1a954052008-10-09 15:01:31 -0400324
Guy Shapirofa201052015-10-22 15:20:10 +0300325 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
326 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400327 if (IS_ERR(id)) {
328 rc = PTR_ERR(id);
329 dprintk("RPC: %s: rdma_create_id() failed %i\n",
330 __func__, rc);
331 return id;
332 }
333
Tom Talpey5675add2008-10-09 15:01:41 -0400334 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400335 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
336 if (rc) {
337 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
338 __func__, rc);
339 goto out;
340 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500341 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
342 if (rc < 0) {
343 dprintk("RPC: %s: wait() exited: %i\n",
344 __func__, rc);
345 goto out;
346 }
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400347
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400348 rc = ia->ri_async_rc;
349 if (rc)
350 goto out;
351
Tom Talpey5675add2008-10-09 15:01:41 -0400352 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400353 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
354 if (rc) {
355 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
356 __func__, rc);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400357 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400358 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500359 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
360 if (rc < 0) {
361 dprintk("RPC: %s: wait() exited: %i\n",
362 __func__, rc);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400363 goto out;
Chuck Lever109b88a2016-11-29 10:52:40 -0500364 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400365 rc = ia->ri_async_rc;
366 if (rc)
Chuck Lever56a6bd12017-04-11 13:23:34 -0400367 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400368
369 return id;
Chuck Lever56a6bd12017-04-11 13:23:34 -0400370
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400371out:
372 rdma_destroy_id(id);
373 return ERR_PTR(rc);
374}
375
376/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400377 * Exported functions.
378 */
379
Chuck Leverfff09592017-04-11 13:22:54 -0400380/**
381 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
382 * @xprt: controlling transport
383 * @addr: IP address of remote peer
384 *
385 * Returns 0 on success, negative errno if an appropriate
386 * Interface Adapter could not be found and opened.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400387 */
388int
Chuck Leverfff09592017-04-11 13:22:54 -0400389rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400390{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400391 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400392 int rc;
393
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400394 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
395 if (IS_ERR(ia->ri_id)) {
396 rc = PTR_ERR(ia->ri_id);
Chuck Leverfff09592017-04-11 13:22:54 -0400397 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400398 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400399 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400400
Christoph Hellwiged082d32016-09-05 12:56:17 +0200401 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400402 if (IS_ERR(ia->ri_pd)) {
403 rc = PTR_ERR(ia->ri_pd);
Chuck Leverb54054c2016-06-29 13:53:27 -0400404 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
Chuck Leverfff09592017-04-11 13:22:54 -0400405 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400406 }
407
Chuck Leverfff09592017-04-11 13:22:54 -0400408 switch (xprt_rdma_memreg_strategy) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400409 case RPCRDMA_FRMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400410 if (frwr_is_supported(ia)) {
411 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
412 break;
413 }
414 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400415 case RPCRDMA_MTHCAFMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400416 if (fmr_is_supported(ia)) {
417 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
418 break;
419 }
420 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400421 default:
Chuck Leverfff09592017-04-11 13:22:54 -0400422 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
423 ia->ri_device->name, xprt_rdma_memreg_strategy);
Chuck Leverb54054c2016-06-29 13:53:27 -0400424 rc = -EINVAL;
Chuck Leverfff09592017-04-11 13:22:54 -0400425 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400426 }
427
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400428 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500429
Chuck Leverfff09592017-04-11 13:22:54 -0400430out_err:
431 rpcrdma_ia_close(ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400432 return rc;
433}
434
Chuck Leverfff09592017-04-11 13:22:54 -0400435/**
Chuck Leverbebd0312017-04-11 13:23:10 -0400436 * rpcrdma_ia_remove - Handle device driver unload
437 * @ia: interface adapter being removed
438 *
439 * Divest transport H/W resources associated with this adapter,
440 * but allow it to be restored later.
441 */
442void
443rpcrdma_ia_remove(struct rpcrdma_ia *ia)
444{
445 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
446 rx_ia);
447 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
448 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
449 struct rpcrdma_req *req;
450 struct rpcrdma_rep *rep;
451
452 cancel_delayed_work_sync(&buf->rb_refresh_worker);
453
454 /* This is similar to rpcrdma_ep_destroy, but:
455 * - Don't cancel the connect worker.
456 * - Don't call rpcrdma_ep_disconnect, which waits
457 * for another conn upcall, which will deadlock.
458 * - rdma_disconnect is unneeded, the underlying
459 * connection is already gone.
460 */
461 if (ia->ri_id->qp) {
462 ib_drain_qp(ia->ri_id->qp);
463 rdma_destroy_qp(ia->ri_id);
464 ia->ri_id->qp = NULL;
465 }
466 ib_free_cq(ep->rep_attr.recv_cq);
467 ib_free_cq(ep->rep_attr.send_cq);
468
469 /* The ULP is responsible for ensuring all DMA
470 * mappings and MRs are gone.
471 */
472 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
473 rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
474 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
475 rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
476 rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
477 rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
478 }
479 rpcrdma_destroy_mrs(buf);
480
481 /* Allow waiters to continue */
482 complete(&ia->ri_remove_done);
483}
484
485/**
Chuck Leverfff09592017-04-11 13:22:54 -0400486 * rpcrdma_ia_close - Clean up/close an IA.
487 * @ia: interface adapter to close
488 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400489 */
490void
491rpcrdma_ia_close(struct rpcrdma_ia *ia)
492{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400493 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400494 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
495 if (ia->ri_id->qp)
496 rdma_destroy_qp(ia->ri_id);
Chuck Lever56a6bd12017-04-11 13:23:34 -0400497 rdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400498 }
Chuck Leverfff09592017-04-11 13:22:54 -0400499 ia->ri_id = NULL;
500 ia->ri_device = NULL;
Chuck Lever6d446982015-05-26 11:51:27 -0400501
502 /* If the pd is still busy, xprtrdma missed freeing a resource */
503 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600504 ib_dealloc_pd(ia->ri_pd);
Chuck Leverfff09592017-04-11 13:22:54 -0400505 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400506}
507
508/*
509 * Create unconnected endpoint.
510 */
511int
512rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
Chuck Lever16f906d2017-02-08 17:00:10 -0500513 struct rpcrdma_create_data_internal *cdata)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400514{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400515 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
Chuck Lever16f906d2017-02-08 17:00:10 -0500516 unsigned int max_qp_wr, max_sge;
Chuck Leverfc664482014-05-28 10:33:25 -0400517 struct ib_cq *sendcq, *recvcq;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500518 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400519
Chuck Levereed50872017-03-11 15:52:47 -0500520 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
521 RPCRDMA_MAX_SEND_SGES);
Chuck Lever16f906d2017-02-08 17:00:10 -0500522 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
523 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
Chuck Leverb3221d62015-08-03 13:03:39 -0400524 return -ENOMEM;
525 }
Chuck Lever16f906d2017-02-08 17:00:10 -0500526 ia->ri_max_send_sges = max_sge - RPCRDMA_MIN_SEND_SGES;
Chuck Leverb3221d62015-08-03 13:03:39 -0400527
Or Gerlitze3e45b12015-12-18 10:59:48 +0200528 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400529 dprintk("RPC: %s: insufficient wqe's available\n",
530 __func__);
531 return -ENOMEM;
532 }
Chuck Lever550d7502016-05-02 14:41:47 -0400533 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
Chuck Lever124fa172015-10-24 17:27:51 -0400534
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400535 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400536 if (cdata->max_requests > max_qp_wr)
537 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400538
539 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
540 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400541 ep->rep_attr.srq = NULL;
542 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400543 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400544 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
Chuck Lever3968cb52015-03-30 14:35:26 -0400545 rc = ia->ri_ops->ro_open(ia, ep, cdata);
546 if (rc)
547 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400548 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400549 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400550 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
Chuck Lever16f906d2017-02-08 17:00:10 -0500551 ep->rep_attr.cap.max_send_sge = max_sge;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400552 ep->rep_attr.cap.max_recv_sge = 1;
553 ep->rep_attr.cap.max_inline_data = 0;
554 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
555 ep->rep_attr.qp_type = IB_QPT_RC;
556 ep->rep_attr.port_num = ~0;
557
558 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
559 "iovs: send %d recv %d\n",
560 __func__,
561 ep->rep_attr.cap.max_send_wr,
562 ep->rep_attr.cap.max_recv_wr,
563 ep->rep_attr.cap.max_send_sge,
564 ep->rep_attr.cap.max_recv_sge);
565
566 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400567 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500568 if (ep->rep_cqinit <= 2)
569 ep->rep_cqinit = 0; /* always signal? */
Chuck Lever8d38de62016-11-29 10:52:16 -0500570 rpcrdma_init_cqcount(ep, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400571 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400572 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400573
Chuck Lever2fa8f882016-03-04 11:28:53 -0500574 sendcq = ib_alloc_cq(ia->ri_device, NULL,
575 ep->rep_attr.cap.max_send_wr + 1,
576 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400577 if (IS_ERR(sendcq)) {
578 rc = PTR_ERR(sendcq);
579 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400580 __func__, rc);
581 goto out1;
582 }
583
Chuck Lever552bf222016-03-04 11:28:36 -0500584 recvcq = ib_alloc_cq(ia->ri_device, NULL,
585 ep->rep_attr.cap.max_recv_wr + 1,
Chuck Leverd8f532d2017-10-16 15:01:30 -0400586 0, IB_POLL_WORKQUEUE);
Chuck Leverfc664482014-05-28 10:33:25 -0400587 if (IS_ERR(recvcq)) {
588 rc = PTR_ERR(recvcq);
589 dprintk("RPC: %s: failed to create recv CQ: %i\n",
590 __func__, rc);
591 goto out2;
592 }
593
Chuck Leverfc664482014-05-28 10:33:25 -0400594 ep->rep_attr.send_cq = sendcq;
595 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400596
597 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400598 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400599
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400600 /* Prepare RDMA-CM private message */
601 pmsg->cp_magic = rpcrdma_cmp_magic;
602 pmsg->cp_version = RPCRDMA_CMP_VERSION;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400603 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400604 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
605 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
606 ep->rep_remote_cma.private_data = pmsg;
607 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400608
609 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400610 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200611 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400612 ep->rep_remote_cma.responder_resources = 32;
613 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500614 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200615 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400616
Chuck Leverb2dde942016-05-02 14:43:03 -0400617 /* Limit transport retries so client can detect server
618 * GID changes quickly. RPC layer handles re-establishing
619 * transport connection and retransmission.
620 */
621 ep->rep_remote_cma.retry_count = 6;
622
623 /* RPC-over-RDMA handles its own flow control. In addition,
624 * make all RNR NAKs visible so we know that RPC-over-RDMA
625 * flow control is working correctly (no NAKs should be seen).
626 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400627 ep->rep_remote_cma.flow_control = 0;
628 ep->rep_remote_cma.rnr_retry_count = 0;
629
630 return 0;
631
632out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500633 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400634out1:
635 return rc;
636}
637
638/*
639 * rpcrdma_ep_destroy
640 *
641 * Disconnect and destroy endpoint. After this, the only
642 * valid operations on the ep are to free it (if dynamically
643 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400644 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400645void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400646rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
647{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400648 dprintk("RPC: %s: entering, connected is %d\n",
649 __func__, ep->rep_connected);
650
Chuck Lever254f91e2014-05-28 10:32:17 -0400651 cancel_delayed_work_sync(&ep->rep_connect_worker);
652
Steve Wise72c02172015-09-21 12:24:23 -0500653 if (ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400654 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400655 rdma_destroy_qp(ia->ri_id);
656 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400657 }
658
Chuck Lever552bf222016-03-04 11:28:36 -0500659 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500660 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400661}
662
Chuck Levera9b0e382017-04-11 13:23:26 -0400663/* Re-establish a connection after a device removal event.
664 * Unlike a normal reconnection, a fresh PD and a new set
665 * of MRs and buffers is needed.
666 */
667static int
668rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
669 struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
670{
671 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
672 int rc, err;
673
674 pr_info("%s: r_xprt = %p\n", __func__, r_xprt);
675
676 rc = -EHOSTUNREACH;
677 if (rpcrdma_ia_open(r_xprt, sap))
678 goto out1;
679
680 rc = -ENOMEM;
681 err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
682 if (err) {
683 pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
684 goto out2;
685 }
686
687 rc = -ENETUNREACH;
688 err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
689 if (err) {
690 pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
691 goto out3;
692 }
693
694 rpcrdma_create_mrs(r_xprt);
695 return 0;
696
697out3:
698 rpcrdma_ep_destroy(ep, ia);
699out2:
700 rpcrdma_ia_close(ia);
701out1:
702 return rc;
703}
704
Chuck Lever18908962017-04-11 13:23:18 -0400705static int
706rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
707 struct rpcrdma_ia *ia)
708{
709 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
710 struct rdma_cm_id *id, *old;
711 int err, rc;
712
713 dprintk("RPC: %s: reconnecting...\n", __func__);
714
715 rpcrdma_ep_disconnect(ep, ia);
716
717 rc = -EHOSTUNREACH;
718 id = rpcrdma_create_id(r_xprt, ia, sap);
719 if (IS_ERR(id))
720 goto out;
721
722 /* As long as the new ID points to the same device as the
723 * old ID, we can reuse the transport's existing PD and all
724 * previously allocated MRs. Also, the same device means
725 * the transport's previous DMA mappings are still valid.
726 *
727 * This is a sanity check only. There should be no way these
728 * point to two different devices here.
729 */
730 old = id;
731 rc = -ENETUNREACH;
732 if (ia->ri_device != id->device) {
733 pr_err("rpcrdma: can't reconnect on different device!\n");
734 goto out_destroy;
735 }
736
737 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
738 if (err) {
739 dprintk("RPC: %s: rdma_create_qp returned %d\n",
740 __func__, err);
741 goto out_destroy;
742 }
743
744 /* Atomically replace the transport's ID and QP. */
745 rc = 0;
746 old = ia->ri_id;
747 ia->ri_id = id;
748 rdma_destroy_qp(old);
749
750out_destroy:
Chuck Lever56a6bd12017-04-11 13:23:34 -0400751 rdma_destroy_id(old);
Chuck Lever18908962017-04-11 13:23:18 -0400752out:
753 return rc;
754}
755
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400756/*
757 * Connect unconnected endpoint.
758 */
759int
760rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
761{
Chuck Lever0a904872017-02-08 17:00:35 -0500762 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
763 rx_ia);
Chuck Lever0a904872017-02-08 17:00:35 -0500764 unsigned int extras;
Chuck Lever18908962017-04-11 13:23:18 -0400765 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400766
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400767retry:
Chuck Lever18908962017-04-11 13:23:18 -0400768 switch (ep->rep_connected) {
769 case 0:
Chuck Leverec62f402014-05-28 10:34:07 -0400770 dprintk("RPC: %s: connecting...\n", __func__);
771 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
772 if (rc) {
773 dprintk("RPC: %s: rdma_create_qp failed %i\n",
774 __func__, rc);
Chuck Lever18908962017-04-11 13:23:18 -0400775 rc = -ENETUNREACH;
776 goto out_noupdate;
Chuck Leverec62f402014-05-28 10:34:07 -0400777 }
Chuck Lever18908962017-04-11 13:23:18 -0400778 break;
Chuck Levera9b0e382017-04-11 13:23:26 -0400779 case -ENODEV:
780 rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
781 if (rc)
782 goto out_noupdate;
783 break;
Chuck Lever18908962017-04-11 13:23:18 -0400784 default:
785 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
786 if (rc)
787 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400788 }
789
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400790 ep->rep_connected = 0;
791
792 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
793 if (rc) {
794 dprintk("RPC: %s: rdma_connect() failed with %i\n",
795 __func__, rc);
796 goto out;
797 }
798
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400799 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400800 if (ep->rep_connected <= 0) {
Chuck Lever0a904872017-02-08 17:00:35 -0500801 if (ep->rep_connected == -EAGAIN)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400802 goto retry;
803 rc = ep->rep_connected;
Chuck Lever0a904872017-02-08 17:00:35 -0500804 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400805 }
806
Chuck Lever0a904872017-02-08 17:00:35 -0500807 dprintk("RPC: %s: connected\n", __func__);
808 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
809 if (extras)
810 rpcrdma_ep_post_extra_recv(r_xprt, extras);
811
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400812out:
813 if (rc)
814 ep->rep_connected = rc;
Chuck Lever18908962017-04-11 13:23:18 -0400815
816out_noupdate:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400817 return rc;
818}
819
820/*
821 * rpcrdma_ep_disconnect
822 *
823 * This is separate from destroy to facilitate the ability
824 * to reconnect without recreating the endpoint.
825 *
826 * This call is not reentrant, and must not be made in parallel
827 * on the same endpoint.
828 */
Chuck Lever282191c2014-07-29 17:25:55 -0400829void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400830rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
831{
832 int rc;
833
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400834 rc = rdma_disconnect(ia->ri_id);
835 if (!rc) {
836 /* returns without wait if not connected */
837 wait_event_interruptible(ep->rep_connect_wait,
838 ep->rep_connected != 1);
839 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
840 (ep->rep_connected == 1) ? "still " : "dis");
841 } else {
842 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
843 ep->rep_connected = rc;
844 }
Chuck Lever550d7502016-05-02 14:41:47 -0400845
846 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400847}
848
Chuck Lever505bbe62016-06-29 13:52:54 -0400849static void
850rpcrdma_mr_recovery_worker(struct work_struct *work)
851{
852 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
853 rb_recovery_worker.work);
854 struct rpcrdma_mw *mw;
855
856 spin_lock(&buf->rb_recovery_lock);
857 while (!list_empty(&buf->rb_stale_mrs)) {
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500858 mw = rpcrdma_pop_mw(&buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400859 spin_unlock(&buf->rb_recovery_lock);
860
861 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
862 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
863
864 spin_lock(&buf->rb_recovery_lock);
kbuild test robot53d78522016-07-16 06:02:05 +0800865 }
Chuck Lever505bbe62016-06-29 13:52:54 -0400866 spin_unlock(&buf->rb_recovery_lock);
867}
868
869void
870rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
871{
872 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
873 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
874
875 spin_lock(&buf->rb_recovery_lock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500876 rpcrdma_push_mw(mw, &buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400877 spin_unlock(&buf->rb_recovery_lock);
878
879 schedule_delayed_work(&buf->rb_recovery_worker, 0);
880}
881
Chuck Levere2ac2362016-06-29 13:54:00 -0400882static void
883rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
884{
885 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
886 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
887 unsigned int count;
888 LIST_HEAD(free);
889 LIST_HEAD(all);
890
891 for (count = 0; count < 32; count++) {
892 struct rpcrdma_mw *mw;
893 int rc;
894
895 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
896 if (!mw)
897 break;
898
899 rc = ia->ri_ops->ro_init_mr(ia, mw);
900 if (rc) {
901 kfree(mw);
902 break;
903 }
904
905 mw->mw_xprt = r_xprt;
906
907 list_add(&mw->mw_list, &free);
908 list_add(&mw->mw_all, &all);
909 }
910
911 spin_lock(&buf->rb_mwlock);
912 list_splice(&free, &buf->rb_mws);
913 list_splice(&all, &buf->rb_all);
914 r_xprt->rx_stats.mrs_allocated += count;
915 spin_unlock(&buf->rb_mwlock);
916
917 dprintk("RPC: %s: created %u MRs\n", __func__, count);
918}
919
920static void
921rpcrdma_mr_refresh_worker(struct work_struct *work)
922{
923 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
924 rb_refresh_worker.work);
925 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
926 rx_buf);
927
928 rpcrdma_create_mrs(r_xprt);
929}
930
Chuck Leverf531a5d2015-10-24 17:27:43 -0400931struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500932rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
933{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400934 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500935 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500936
Chuck Lever85275c82015-01-21 11:04:16 -0500937 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500938 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500939 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500940
Chuck Leverf531a5d2015-10-24 17:27:43 -0400941 spin_lock(&buffer->rb_reqslock);
942 list_add(&req->rl_all, &buffer->rb_allreqs);
943 spin_unlock(&buffer->rb_reqslock);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500944 req->rl_cqe.done = rpcrdma_wc_send;
Chuck Lever13924022015-01-21 11:03:52 -0500945 req->rl_buffer = &r_xprt->rx_buf;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400946 INIT_LIST_HEAD(&req->rl_registered);
Chuck Lever90aab602016-09-15 10:56:43 -0400947 req->rl_send_wr.next = NULL;
948 req->rl_send_wr.wr_cqe = &req->rl_cqe;
Chuck Lever655fec62016-09-15 10:57:24 -0400949 req->rl_send_wr.sg_list = req->rl_send_sge;
Chuck Lever90aab602016-09-15 10:56:43 -0400950 req->rl_send_wr.opcode = IB_WR_SEND;
Chuck Lever13924022015-01-21 11:03:52 -0500951 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500952}
953
Chuck Leverf531a5d2015-10-24 17:27:43 -0400954struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -0500955rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
956{
957 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500958 struct rpcrdma_rep *rep;
959 int rc;
960
961 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500962 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500963 if (rep == NULL)
964 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500965
Chuck Lever13650c22016-09-15 10:56:26 -0400966 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -0400967 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever6b1184c2015-01-21 11:04:25 -0500968 if (IS_ERR(rep->rr_rdmabuf)) {
969 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500970 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500971 }
Chuck Lever96f87782017-08-03 14:30:03 -0400972 xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base,
973 rdmab_length(rep->rr_rdmabuf));
Chuck Lever13924022015-01-21 11:03:52 -0500974
Chuck Lever1519e962016-09-15 10:57:49 -0400975 rep->rr_cqe.done = rpcrdma_wc_receive;
Chuck Leverfed171b2015-05-26 11:51:37 -0400976 rep->rr_rxprt = r_xprt;
Chuck Leverd8f532d2017-10-16 15:01:30 -0400977 INIT_WORK(&rep->rr_work, rpcrdma_deferred_completion);
Chuck Lever6ea8e712016-09-15 10:56:51 -0400978 rep->rr_recv_wr.next = NULL;
979 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
980 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
981 rep->rr_recv_wr.num_sge = 1;
Chuck Lever13924022015-01-21 11:03:52 -0500982 return rep;
983
984out_free:
985 kfree(rep);
986out:
987 return ERR_PTR(rc);
988}
989
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400990int
Chuck Leverac920d02015-01-21 11:03:44 -0500991rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400992{
Chuck Leverac920d02015-01-21 11:03:44 -0500993 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400994 int i, rc;
995
Chuck Lever1e465fd2015-10-24 17:27:02 -0400996 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -0400997 buf->rb_bc_srv_max_requests = 0;
Chuck Lever23826c72016-03-04 11:28:27 -0500998 atomic_set(&buf->rb_credits, 1);
Chuck Levere2ac2362016-06-29 13:54:00 -0400999 spin_lock_init(&buf->rb_mwlock);
Chuck Lever505bbe62016-06-29 13:52:54 -04001000 spin_lock_init(&buf->rb_lock);
1001 spin_lock_init(&buf->rb_recovery_lock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001002 INIT_LIST_HEAD(&buf->rb_mws);
1003 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever505bbe62016-06-29 13:52:54 -04001004 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001005 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1006 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001007 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
1008 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001009
Chuck Levere2ac2362016-06-29 13:54:00 -04001010 rpcrdma_create_mrs(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001011
Chuck Lever1e465fd2015-10-24 17:27:02 -04001012 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001013 INIT_LIST_HEAD(&buf->rb_allreqs);
1014 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001015 for (i = 0; i < buf->rb_max_requests; i++) {
1016 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001017
Chuck Lever13924022015-01-21 11:03:52 -05001018 req = rpcrdma_create_req(r_xprt);
1019 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001020 dprintk("RPC: %s: request buffer %d alloc"
1021 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001022 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001023 goto out;
1024 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001025 req->rl_backchannel = false;
Chuck Levera80d66c2017-06-08 11:52:12 -04001026 list_add(&req->rl_list, &buf->rb_send_bufs);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001027 }
1028
1029 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001030 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001031 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001032
Chuck Lever13924022015-01-21 11:03:52 -05001033 rep = rpcrdma_create_rep(r_xprt);
1034 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001035 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1036 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001037 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001038 goto out;
1039 }
Chuck Lever1e465fd2015-10-24 17:27:02 -04001040 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001041 }
Chuck Lever13924022015-01-21 11:03:52 -05001042
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001043 return 0;
1044out:
1045 rpcrdma_buffer_destroy(buf);
1046 return rc;
1047}
1048
Chuck Lever1e465fd2015-10-24 17:27:02 -04001049static struct rpcrdma_req *
1050rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1051{
1052 struct rpcrdma_req *req;
1053
1054 req = list_first_entry(&buf->rb_send_bufs,
Chuck Levera80d66c2017-06-08 11:52:12 -04001055 struct rpcrdma_req, rl_list);
Chuck Lever431af642017-06-08 11:52:20 -04001056 list_del_init(&req->rl_list);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001057 return req;
1058}
1059
1060static struct rpcrdma_rep *
1061rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1062{
1063 struct rpcrdma_rep *rep;
1064
1065 rep = list_first_entry(&buf->rb_recv_bufs,
1066 struct rpcrdma_rep, rr_list);
1067 list_del(&rep->rr_list);
1068 return rep;
1069}
1070
Chuck Lever2e845222014-07-29 17:25:38 -04001071static void
Chuck Lever13650c22016-09-15 10:56:26 -04001072rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -05001073{
Chuck Lever13650c22016-09-15 10:56:26 -04001074 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001075 kfree(rep);
1076}
1077
Chuck Leverf531a5d2015-10-24 17:27:43 -04001078void
Chuck Lever13650c22016-09-15 10:56:26 -04001079rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -05001080{
Chuck Lever13650c22016-09-15 10:56:26 -04001081 rpcrdma_free_regbuf(req->rl_recvbuf);
1082 rpcrdma_free_regbuf(req->rl_sendbuf);
1083 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001084 kfree(req);
1085}
1086
Chuck Levere2ac2362016-06-29 13:54:00 -04001087static void
1088rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1089{
1090 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1091 rx_buf);
1092 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1093 struct rpcrdma_mw *mw;
1094 unsigned int count;
1095
1096 count = 0;
1097 spin_lock(&buf->rb_mwlock);
1098 while (!list_empty(&buf->rb_all)) {
1099 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1100 list_del(&mw->mw_all);
1101
1102 spin_unlock(&buf->rb_mwlock);
1103 ia->ri_ops->ro_release_mr(mw);
1104 count++;
1105 spin_lock(&buf->rb_mwlock);
1106 }
1107 spin_unlock(&buf->rb_mwlock);
1108 r_xprt->rx_stats.mrs_allocated = 0;
1109
1110 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1111}
1112
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001113void
1114rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1115{
Chuck Lever505bbe62016-06-29 13:52:54 -04001116 cancel_delayed_work_sync(&buf->rb_recovery_worker);
Chuck Lever9378b272017-04-11 13:22:29 -04001117 cancel_delayed_work_sync(&buf->rb_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001118
Chuck Lever1e465fd2015-10-24 17:27:02 -04001119 while (!list_empty(&buf->rb_recv_bufs)) {
1120 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001121
Chuck Lever1e465fd2015-10-24 17:27:02 -04001122 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001123 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001124 }
Chuck Lever05c97462016-09-06 11:22:58 -04001125 buf->rb_send_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001126
Chuck Leverf531a5d2015-10-24 17:27:43 -04001127 spin_lock(&buf->rb_reqslock);
1128 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001129 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001130
Chuck Leverf531a5d2015-10-24 17:27:43 -04001131 req = list_first_entry(&buf->rb_allreqs,
1132 struct rpcrdma_req, rl_all);
1133 list_del(&req->rl_all);
1134
1135 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001136 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001137 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001138 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001139 spin_unlock(&buf->rb_reqslock);
Chuck Lever05c97462016-09-06 11:22:58 -04001140 buf->rb_recv_count = 0;
Chuck Lever9f9d8022014-07-29 17:24:45 -04001141
Chuck Levere2ac2362016-06-29 13:54:00 -04001142 rpcrdma_destroy_mrs(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001143}
1144
Chuck Lever346aa662015-05-26 11:52:06 -04001145struct rpcrdma_mw *
1146rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001147{
Chuck Lever346aa662015-05-26 11:52:06 -04001148 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1149 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001150
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001151 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001152 if (!list_empty(&buf->rb_mws))
1153 mw = rpcrdma_pop_mw(&buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001154 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001155
1156 if (!mw)
Chuck Levere2ac2362016-06-29 13:54:00 -04001157 goto out_nomws;
Chuck Lever4b196dc62017-06-08 11:51:56 -04001158 mw->mw_flags = 0;
Chuck Lever346aa662015-05-26 11:52:06 -04001159 return mw;
Chuck Levere2ac2362016-06-29 13:54:00 -04001160
1161out_nomws:
1162 dprintk("RPC: %s: no MWs available\n", __func__);
Chuck Leverbebd0312017-04-11 13:23:10 -04001163 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1164 schedule_delayed_work(&buf->rb_refresh_worker, 0);
Chuck Levere2ac2362016-06-29 13:54:00 -04001165
1166 /* Allow the reply handler and refresh worker to run */
1167 cond_resched();
1168
1169 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001170}
1171
Chuck Lever346aa662015-05-26 11:52:06 -04001172void
1173rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001174{
Chuck Lever346aa662015-05-26 11:52:06 -04001175 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001176
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001177 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001178 rpcrdma_push_mw(mw, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001179 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001180}
1181
Chuck Lever05c97462016-09-06 11:22:58 -04001182static struct rpcrdma_rep *
1183rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1184{
1185 /* If an RPC previously completed without a reply (say, a
1186 * credential problem or a soft timeout occurs) then hold off
1187 * on supplying more Receive buffers until the number of new
1188 * pending RPCs catches up to the number of posted Receives.
1189 */
1190 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1191 return NULL;
1192
1193 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1194 return NULL;
1195 buffers->rb_recv_count++;
1196 return rpcrdma_buffer_get_rep_locked(buffers);
1197}
1198
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001199/*
1200 * Get a set of request/reply buffers.
Chuck Lever78d506e2016-09-06 11:22:49 -04001201 *
1202 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001203 */
1204struct rpcrdma_req *
1205rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1206{
1207 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001208
Chuck Levera5b027e2015-10-24 17:27:27 -04001209 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001210 if (list_empty(&buffers->rb_send_bufs))
1211 goto out_reqbuf;
Chuck Lever05c97462016-09-06 11:22:58 -04001212 buffers->rb_send_count++;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001213 req = rpcrdma_buffer_get_req_locked(buffers);
Chuck Lever05c97462016-09-06 11:22:58 -04001214 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001215 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001216 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001217
Chuck Lever1e465fd2015-10-24 17:27:02 -04001218out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001219 spin_unlock(&buffers->rb_lock);
Chuck Lever78d506e2016-09-06 11:22:49 -04001220 pr_warn("RPC: %s: out of request buffers\n", __func__);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001221 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001222}
1223
1224/*
1225 * Put request/reply buffers back into pool.
1226 * Pre-decrement counter/array index.
1227 */
1228void
1229rpcrdma_buffer_put(struct rpcrdma_req *req)
1230{
1231 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001232 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001233
Chuck Lever90aab602016-09-15 10:56:43 -04001234 req->rl_send_wr.num_sge = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001235 req->rl_reply = NULL;
1236
Chuck Levera5b027e2015-10-24 17:27:27 -04001237 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001238 buffers->rb_send_count--;
Chuck Levera80d66c2017-06-08 11:52:12 -04001239 list_add_tail(&req->rl_list, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001240 if (rep) {
1241 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001242 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001243 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001244 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001245}
1246
1247/*
1248 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001249 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001250 */
1251void
1252rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1253{
1254 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001255
Chuck Levera5b027e2015-10-24 17:27:27 -04001256 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001257 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001258 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001259}
1260
1261/*
1262 * Put reply buffers back into pool when not attached to
1263 * request. This happens in error conditions.
1264 */
1265void
1266rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1267{
Chuck Leverfed171b2015-05-26 11:51:37 -04001268 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001269
Chuck Levera5b027e2015-10-24 17:27:27 -04001270 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001271 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001272 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001273 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001274}
1275
Chuck Lever9128c3e2015-01-21 11:04:00 -05001276/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001277 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001278 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001279 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001280 * @flags: GFP flags
1281 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001282 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1283 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001284 *
1285 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001286 * receiving the payload of RDMA RECV operations. During Long Calls
1287 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001288 */
1289struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001290rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1291 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001292{
1293 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001294
Chuck Lever9128c3e2015-01-21 11:04:00 -05001295 rb = kmalloc(sizeof(*rb) + size, flags);
1296 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001297 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001298
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001299 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001300 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001301 rb->rg_iov.length = size;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001302
1303 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001304}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001305
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001306/**
1307 * __rpcrdma_map_regbuf - DMA-map a regbuf
1308 * @ia: controlling rpcrdma_ia
1309 * @rb: regbuf to be mapped
1310 */
1311bool
1312__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1313{
Chuck Lever91a10c52017-04-11 13:23:02 -04001314 struct ib_device *device = ia->ri_device;
1315
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001316 if (rb->rg_direction == DMA_NONE)
1317 return false;
1318
Chuck Lever91a10c52017-04-11 13:23:02 -04001319 rb->rg_iov.addr = ib_dma_map_single(device,
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001320 (void *)rb->rg_base,
1321 rdmab_length(rb),
1322 rb->rg_direction);
Chuck Lever91a10c52017-04-11 13:23:02 -04001323 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001324 return false;
1325
Chuck Lever91a10c52017-04-11 13:23:02 -04001326 rb->rg_device = device;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001327 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1328 return true;
1329}
1330
1331static void
1332rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1333{
1334 if (!rpcrdma_regbuf_is_mapped(rb))
1335 return;
1336
1337 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1338 rdmab_length(rb), rb->rg_direction);
1339 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001340}
1341
1342/**
1343 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001344 * @rb: regbuf to be deregistered and freed
1345 */
1346void
Chuck Lever13650c22016-09-15 10:56:26 -04001347rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001348{
Chuck Levere531dca2015-08-03 13:03:20 -04001349 if (!rb)
1350 return;
1351
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001352 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001353 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001354}
1355
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001356/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001357 * Prepost any receive buffer, then post send.
1358 *
1359 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1360 */
1361int
1362rpcrdma_ep_post(struct rpcrdma_ia *ia,
1363 struct rpcrdma_ep *ep,
1364 struct rpcrdma_req *req)
1365{
Chuck Lever90aab602016-09-15 10:56:43 -04001366 struct ib_send_wr *send_wr = &req->rl_send_wr;
1367 struct ib_send_wr *send_wr_fail;
Chuck Lever655fec62016-09-15 10:57:24 -04001368 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001369
Chuck Lever90aab602016-09-15 10:56:43 -04001370 if (req->rl_reply) {
1371 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001372 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001373 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001374 req->rl_reply = NULL;
1375 }
1376
Chuck Leverb3221d62015-08-03 13:03:39 -04001377 dprintk("RPC: %s: posting %d s/g entries\n",
Chuck Lever90aab602016-09-15 10:56:43 -04001378 __func__, send_wr->num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001379
Chuck Lever8d38de62016-11-29 10:52:16 -05001380 rpcrdma_set_signaled(ep, send_wr);
Chuck Lever90aab602016-09-15 10:56:43 -04001381 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001382 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001383 goto out_postsend_err;
1384 return 0;
1385
1386out_postsend_err:
1387 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1388 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001389}
1390
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001391int
1392rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001393 struct rpcrdma_rep *rep)
1394{
Chuck Lever6ea8e712016-09-15 10:56:51 -04001395 struct ib_recv_wr *recv_wr_fail;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001396 int rc;
1397
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001398 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1399 goto out_map;
Chuck Lever6ea8e712016-09-15 10:56:51 -04001400 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001401 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001402 goto out_postrecv;
1403 return 0;
1404
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001405out_map:
1406 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1407 return -EIO;
1408
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001409out_postrecv:
1410 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1411 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001412}
Chuck Lever43e95982014-07-29 17:23:34 -04001413
Chuck Leverf531a5d2015-10-24 17:27:43 -04001414/**
1415 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1416 * @r_xprt: transport associated with these backchannel resources
1417 * @min_reqs: minimum number of incoming requests expected
1418 *
1419 * Returns zero if all requested buffers were posted, or a negative errno.
1420 */
1421int
1422rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1423{
1424 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1425 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001426 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001427 int rc;
1428
1429 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001430 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001431 if (list_empty(&buffers->rb_recv_bufs))
1432 goto out_reqbuf;
1433 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001434 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001435
Chuck Leverb1573802016-09-15 10:56:35 -04001436 rc = rpcrdma_ep_post_recv(ia, rep);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001437 if (rc)
1438 goto out_rc;
1439 }
1440
1441 return 0;
1442
1443out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001444 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001445 pr_warn("%s: no extra receive buffers\n", __func__);
1446 return -ENOMEM;
1447
1448out_rc:
1449 rpcrdma_recv_buffer_put(rep);
1450 return rc;
1451}