blob: b69daa30874fd5350f8f09415df9fe412f5f98f4 [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>
Devesh Sharmad0f36c42015-08-03 13:05:04 -040056#include <linux/module.h> /* try_module_get()/module_put() */
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 Leverfe97b472015-10-24 17:27:10 -040076static struct workqueue_struct *rpcrdma_receive_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040077
Chuck Leverfe97b472015-10-24 17:27:10 -040078int
79rpcrdma_alloc_wq(void)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040080{
Chuck Leverfe97b472015-10-24 17:27:10 -040081 struct workqueue_struct *recv_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040082
Chuck Leverfe97b472015-10-24 17:27:10 -040083 recv_wq = alloc_workqueue("xprtrdma_receive",
84 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI,
85 0);
86 if (!recv_wq)
87 return -ENOMEM;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040088
Chuck Leverfe97b472015-10-24 17:27:10 -040089 rpcrdma_receive_wq = recv_wq;
90 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040091}
92
Chuck Leverfe97b472015-10-24 17:27:10 -040093void
94rpcrdma_destroy_wq(void)
Chuck Leverf1a03b72014-11-08 20:14:37 -050095{
Chuck Leverfe97b472015-10-24 17:27:10 -040096 struct workqueue_struct *wq;
Chuck Leverf1a03b72014-11-08 20:14:37 -050097
Chuck Leverfe97b472015-10-24 17:27:10 -040098 if (rpcrdma_receive_wq) {
99 wq = rpcrdma_receive_wq;
100 rpcrdma_receive_wq = NULL;
101 destroy_workqueue(wq);
102 }
Chuck Leverf1a03b72014-11-08 20:14:37 -0500103}
104
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400105static void
106rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
107{
108 struct rpcrdma_ep *ep = context;
109
Chuck Lever2f6922c2016-11-29 10:53:21 -0500110 pr_err("rpcrdma: %s on device %s ep %p\n",
111 ib_event_msg(event->event), event->device->name, context);
112
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400113 if (ep->rep_connected == 1) {
114 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500115 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400116 wake_up_all(&ep->rep_connect_wait);
117 }
118}
119
Chuck Lever2fa8f882016-03-04 11:28:53 -0500120/**
121 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
122 * @cq: completion queue (ignored)
123 * @wc: completed WR
124 *
Chuck Lever4220a072015-10-24 17:26:45 -0400125 */
126static void
Chuck Lever2fa8f882016-03-04 11:28:53 -0500127rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400128{
Chuck Lever2fa8f882016-03-04 11:28:53 -0500129 /* WARNING: Only wr_cqe and status are reliable at this point */
130 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
131 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
132 ib_wc_status_msg(wc->status),
133 wc->status, wc->vendor_err);
Chuck Leverfc664482014-05-28 10:33:25 -0400134}
135
Chuck Lever23826c72016-03-04 11:28:27 -0500136/* Perform basic sanity checking to avoid using garbage
137 * to update the credit grant value.
138 */
139static void
140rpcrdma_update_granted_credits(struct rpcrdma_rep *rep)
141{
142 struct rpcrdma_msg *rmsgp = rdmab_to_msg(rep->rr_rdmabuf);
143 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
144 u32 credits;
145
146 if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
147 return;
148
149 credits = be32_to_cpu(rmsgp->rm_credit);
150 if (credits == 0)
151 credits = 1; /* don't deadlock */
152 else if (credits > buffer->rb_max_requests)
153 credits = buffer->rb_max_requests;
154
155 atomic_set(&buffer->rb_credits, credits);
156}
157
Chuck Lever552bf222016-03-04 11:28:36 -0500158/**
Chuck Lever1519e962016-09-15 10:57:49 -0400159 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
Chuck Lever552bf222016-03-04 11:28:36 -0500160 * @cq: completion queue (ignored)
161 * @wc: completed WR
162 *
163 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400164static void
Chuck Lever1519e962016-09-15 10:57:49 -0400165rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400166{
Chuck Lever552bf222016-03-04 11:28:36 -0500167 struct ib_cqe *cqe = wc->wr_cqe;
168 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
169 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400170
Chuck Lever85024272015-01-21 11:02:04 -0500171 /* WARNING: Only wr_id and status are reliable at this point */
172 if (wc->status != IB_WC_SUCCESS)
173 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400174
Chuck Lever85024272015-01-21 11:02:04 -0500175 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400176 if (wc->opcode != IB_WC_RECV)
177 return;
178
Chuck Lever85024272015-01-21 11:02:04 -0500179 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
180 __func__, rep, wc->byte_len);
181
Chuck Leverfc664482014-05-28 10:33:25 -0400182 rep->rr_len = wc->byte_len;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400183 rep->rr_wc_flags = wc->wc_flags;
184 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
185
Chuck Lever91a10c52017-04-11 13:23:02 -0400186 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
Chuck Lever6b1184c2015-01-21 11:04:25 -0500187 rdmab_addr(rep->rr_rdmabuf),
188 rep->rr_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500189
190 rpcrdma_update_granted_credits(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400191
192out_schedule:
Chuck Leverfe97b472015-10-24 17:27:10 -0400193 queue_work(rpcrdma_receive_wq, &rep->rr_work);
Chuck Lever85024272015-01-21 11:02:04 -0500194 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400195
Chuck Lever85024272015-01-21 11:02:04 -0500196out_fail:
197 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500198 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
199 ib_wc_status_msg(wc->status),
200 wc->status, wc->vendor_err);
Chuck Leverb0e178a2015-10-24 17:26:54 -0400201 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500202 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400203}
204
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400205static void
206rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
207 struct rdma_conn_param *param)
208{
209 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
210 const struct rpcrdma_connect_private *pmsg = param->private_data;
211 unsigned int rsize, wsize;
212
Chuck Leverc8b920b2016-09-15 10:57:16 -0400213 /* Default settings for RPC-over-RDMA Version One */
214 r_xprt->rx_ia.ri_reminv_expected = false;
Chuck Leverb5f0afb2017-02-08 16:59:54 -0500215 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400216 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
217 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
218
219 if (pmsg &&
220 pmsg->cp_magic == rpcrdma_cmp_magic &&
221 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
Chuck Leverc8b920b2016-09-15 10:57:16 -0400222 r_xprt->rx_ia.ri_reminv_expected = true;
Chuck Leverc95a3c62017-02-08 17:00:02 -0500223 r_xprt->rx_ia.ri_implicit_roundup = true;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400224 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
225 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
226 }
227
228 if (rsize < cdata->inline_rsize)
229 cdata->inline_rsize = rsize;
230 if (wsize < cdata->inline_wsize)
231 cdata->inline_wsize = wsize;
Chuck Lever6d6bf722016-11-29 10:53:13 -0500232 dprintk("RPC: %s: max send %u, max recv %u\n",
233 __func__, cdata->inline_wsize, cdata->inline_rsize);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400234 rpcrdma_set_max_header_sizes(r_xprt);
235}
236
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400237static int
238rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
239{
240 struct rpcrdma_xprt *xprt = id->context;
241 struct rpcrdma_ia *ia = &xprt->rx_ia;
242 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500243#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400244 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800245#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500246 struct ib_qp_attr *attr = &ia->ri_qp_attr;
247 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400248 int connstate = 0;
249
250 switch (event->event) {
251 case RDMA_CM_EVENT_ADDR_RESOLVED:
252 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400253 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400254 complete(&ia->ri_done);
255 break;
256 case RDMA_CM_EVENT_ADDR_ERROR:
257 ia->ri_async_rc = -EHOSTUNREACH;
258 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
259 __func__, ep);
260 complete(&ia->ri_done);
261 break;
262 case RDMA_CM_EVENT_ROUTE_ERROR:
263 ia->ri_async_rc = -ENETUNREACH;
264 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
265 __func__, ep);
266 complete(&ia->ri_done);
267 break;
Chuck Leverbebd0312017-04-11 13:23:10 -0400268 case RDMA_CM_EVENT_DEVICE_REMOVAL:
269#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
270 pr_info("rpcrdma: removing device for %pIS:%u\n",
271 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 Leverce1ab9a2015-01-21 11:03:35 -0500285 ib_query_qp(ia->ri_id->qp, attr,
286 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
287 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400288 dprintk("RPC: %s: %d responder resources"
289 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500290 __func__, attr->max_dest_rd_atomic,
291 attr->max_rd_atomic);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400292 rpcrdma_update_connect_private(xprt, &event->param.conn);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400293 goto connected;
294 case RDMA_CM_EVENT_CONNECT_ERROR:
295 connstate = -ENOTCONN;
296 goto connected;
297 case RDMA_CM_EVENT_UNREACHABLE:
298 connstate = -ENETDOWN;
299 goto connected;
300 case RDMA_CM_EVENT_REJECTED:
Chuck Lever0a904872017-02-08 17:00:35 -0500301#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
302 pr_info("rpcrdma: connection to %pIS:%u on %s rejected: %s\n",
303 sap, rpc_get_port(sap), ia->ri_device->name,
304 rdma_reject_msg(id, event->status));
305#endif
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400306 connstate = -ECONNREFUSED;
Chuck Lever0a904872017-02-08 17:00:35 -0500307 if (event->status == IB_CM_REJ_STALE_CONN)
308 connstate = -EAGAIN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400309 goto connected;
310 case RDMA_CM_EVENT_DISCONNECTED:
311 connstate = -ECONNABORTED;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400312connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400313 dprintk("RPC: %s: %sconnected\n",
314 __func__, connstate > 0 ? "" : "dis");
Chuck Lever23826c72016-03-04 11:28:27 -0500315 atomic_set(&xprt->rx_buf.rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400316 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500317 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400318 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400319 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400320 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400321 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
322 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300323 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400324 break;
325 }
326
Jeff Laytonf895b252014-11-17 16:58:04 -0500327#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400328 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500329 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400330 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400331
Chuck Levera0ce85f2015-03-30 14:34:21 -0400332 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400333 sap, rpc_get_port(sap),
Chuck Lever89e0d1122015-05-26 11:51:56 -0400334 ia->ri_device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400335 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400336 xprt->rx_buf.rb_max_requests,
337 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
338 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400339 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
340 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400341 }
342#endif
343
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400344 return 0;
345}
346
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400347static void rpcrdma_destroy_id(struct rdma_cm_id *id)
348{
349 if (id) {
350 module_put(id->device->owner);
351 rdma_destroy_id(id);
352 }
353}
354
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400355static struct rdma_cm_id *
356rpcrdma_create_id(struct rpcrdma_xprt *xprt,
357 struct rpcrdma_ia *ia, struct sockaddr *addr)
358{
Chuck Lever109b88a2016-11-29 10:52:40 -0500359 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400360 struct rdma_cm_id *id;
361 int rc;
362
Tom Talpey1a954052008-10-09 15:01:31 -0400363 init_completion(&ia->ri_done);
Chuck Leverbebd0312017-04-11 13:23:10 -0400364 init_completion(&ia->ri_remove_done);
Tom Talpey1a954052008-10-09 15:01:31 -0400365
Guy Shapirofa201052015-10-22 15:20:10 +0300366 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
367 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400368 if (IS_ERR(id)) {
369 rc = PTR_ERR(id);
370 dprintk("RPC: %s: rdma_create_id() failed %i\n",
371 __func__, rc);
372 return id;
373 }
374
Tom Talpey5675add2008-10-09 15:01:41 -0400375 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400376 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
377 if (rc) {
378 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
379 __func__, rc);
380 goto out;
381 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500382 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
383 if (rc < 0) {
384 dprintk("RPC: %s: wait() exited: %i\n",
385 __func__, rc);
386 goto out;
387 }
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400388
389 /* FIXME:
390 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
391 * be pinned while there are active NFS/RDMA mounts to prevent
392 * hangs and crashes at umount time.
393 */
394 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
395 dprintk("RPC: %s: Failed to get device module\n",
396 __func__);
397 ia->ri_async_rc = -ENODEV;
398 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400399 rc = ia->ri_async_rc;
400 if (rc)
401 goto out;
402
Tom Talpey5675add2008-10-09 15:01:41 -0400403 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400404 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
405 if (rc) {
406 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
407 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400408 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400409 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500410 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
411 if (rc < 0) {
412 dprintk("RPC: %s: wait() exited: %i\n",
413 __func__, rc);
414 goto put;
415 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400416 rc = ia->ri_async_rc;
417 if (rc)
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400418 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400419
420 return id;
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400421put:
422 module_put(id->device->owner);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400423out:
424 rdma_destroy_id(id);
425 return ERR_PTR(rc);
426}
427
428/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400429 * Exported functions.
430 */
431
Chuck Leverfff09592017-04-11 13:22:54 -0400432/**
433 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
434 * @xprt: controlling transport
435 * @addr: IP address of remote peer
436 *
437 * Returns 0 on success, negative errno if an appropriate
438 * Interface Adapter could not be found and opened.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400439 */
440int
Chuck Leverfff09592017-04-11 13:22:54 -0400441rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400442{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400443 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400444 int rc;
445
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400446 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
447 if (IS_ERR(ia->ri_id)) {
448 rc = PTR_ERR(ia->ri_id);
Chuck Leverfff09592017-04-11 13:22:54 -0400449 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400450 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400451 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400452
Christoph Hellwiged082d32016-09-05 12:56:17 +0200453 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400454 if (IS_ERR(ia->ri_pd)) {
455 rc = PTR_ERR(ia->ri_pd);
Chuck Leverb54054c2016-06-29 13:53:27 -0400456 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
Chuck Leverfff09592017-04-11 13:22:54 -0400457 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400458 }
459
Chuck Leverfff09592017-04-11 13:22:54 -0400460 switch (xprt_rdma_memreg_strategy) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400461 case RPCRDMA_FRMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400462 if (frwr_is_supported(ia)) {
463 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
464 break;
465 }
466 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400467 case RPCRDMA_MTHCAFMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400468 if (fmr_is_supported(ia)) {
469 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
470 break;
471 }
472 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400473 default:
Chuck Leverfff09592017-04-11 13:22:54 -0400474 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
475 ia->ri_device->name, xprt_rdma_memreg_strategy);
Chuck Leverb54054c2016-06-29 13:53:27 -0400476 rc = -EINVAL;
Chuck Leverfff09592017-04-11 13:22:54 -0400477 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400478 }
479
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400480 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500481
Chuck Leverfff09592017-04-11 13:22:54 -0400482out_err:
483 rpcrdma_ia_close(ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400484 return rc;
485}
486
Chuck Leverfff09592017-04-11 13:22:54 -0400487/**
Chuck Leverbebd0312017-04-11 13:23:10 -0400488 * rpcrdma_ia_remove - Handle device driver unload
489 * @ia: interface adapter being removed
490 *
491 * Divest transport H/W resources associated with this adapter,
492 * but allow it to be restored later.
493 */
494void
495rpcrdma_ia_remove(struct rpcrdma_ia *ia)
496{
497 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
498 rx_ia);
499 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
500 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
501 struct rpcrdma_req *req;
502 struct rpcrdma_rep *rep;
503
504 cancel_delayed_work_sync(&buf->rb_refresh_worker);
505
506 /* This is similar to rpcrdma_ep_destroy, but:
507 * - Don't cancel the connect worker.
508 * - Don't call rpcrdma_ep_disconnect, which waits
509 * for another conn upcall, which will deadlock.
510 * - rdma_disconnect is unneeded, the underlying
511 * connection is already gone.
512 */
513 if (ia->ri_id->qp) {
514 ib_drain_qp(ia->ri_id->qp);
515 rdma_destroy_qp(ia->ri_id);
516 ia->ri_id->qp = NULL;
517 }
518 ib_free_cq(ep->rep_attr.recv_cq);
519 ib_free_cq(ep->rep_attr.send_cq);
520
521 /* The ULP is responsible for ensuring all DMA
522 * mappings and MRs are gone.
523 */
524 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
525 rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
526 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
527 rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
528 rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
529 rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
530 }
531 rpcrdma_destroy_mrs(buf);
532
533 /* Allow waiters to continue */
534 complete(&ia->ri_remove_done);
535}
536
537/**
Chuck Leverfff09592017-04-11 13:22:54 -0400538 * rpcrdma_ia_close - Clean up/close an IA.
539 * @ia: interface adapter to close
540 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400541 */
542void
543rpcrdma_ia_close(struct rpcrdma_ia *ia)
544{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400545 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400546 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
547 if (ia->ri_id->qp)
548 rdma_destroy_qp(ia->ri_id);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400549 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400550 }
Chuck Leverfff09592017-04-11 13:22:54 -0400551 ia->ri_id = NULL;
552 ia->ri_device = NULL;
Chuck Lever6d446982015-05-26 11:51:27 -0400553
554 /* If the pd is still busy, xprtrdma missed freeing a resource */
555 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600556 ib_dealloc_pd(ia->ri_pd);
Chuck Leverfff09592017-04-11 13:22:54 -0400557 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400558}
559
560/*
561 * Create unconnected endpoint.
562 */
563int
564rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
Chuck Lever16f906d2017-02-08 17:00:10 -0500565 struct rpcrdma_create_data_internal *cdata)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400566{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400567 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
Chuck Lever16f906d2017-02-08 17:00:10 -0500568 unsigned int max_qp_wr, max_sge;
Chuck Leverfc664482014-05-28 10:33:25 -0400569 struct ib_cq *sendcq, *recvcq;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500570 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400571
Chuck Levereed50872017-03-11 15:52:47 -0500572 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
573 RPCRDMA_MAX_SEND_SGES);
Chuck Lever16f906d2017-02-08 17:00:10 -0500574 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
575 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
Chuck Leverb3221d62015-08-03 13:03:39 -0400576 return -ENOMEM;
577 }
Chuck Lever16f906d2017-02-08 17:00:10 -0500578 ia->ri_max_send_sges = max_sge - RPCRDMA_MIN_SEND_SGES;
Chuck Leverb3221d62015-08-03 13:03:39 -0400579
Or Gerlitze3e45b12015-12-18 10:59:48 +0200580 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400581 dprintk("RPC: %s: insufficient wqe's available\n",
582 __func__);
583 return -ENOMEM;
584 }
Chuck Lever550d7502016-05-02 14:41:47 -0400585 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
Chuck Lever124fa172015-10-24 17:27:51 -0400586
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400587 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400588 if (cdata->max_requests > max_qp_wr)
589 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400590
591 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
592 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400593 ep->rep_attr.srq = NULL;
594 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400595 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400596 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
Chuck Lever3968cb52015-03-30 14:35:26 -0400597 rc = ia->ri_ops->ro_open(ia, ep, cdata);
598 if (rc)
599 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400600 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400601 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400602 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
Chuck Lever16f906d2017-02-08 17:00:10 -0500603 ep->rep_attr.cap.max_send_sge = max_sge;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400604 ep->rep_attr.cap.max_recv_sge = 1;
605 ep->rep_attr.cap.max_inline_data = 0;
606 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
607 ep->rep_attr.qp_type = IB_QPT_RC;
608 ep->rep_attr.port_num = ~0;
609
610 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
611 "iovs: send %d recv %d\n",
612 __func__,
613 ep->rep_attr.cap.max_send_wr,
614 ep->rep_attr.cap.max_recv_wr,
615 ep->rep_attr.cap.max_send_sge,
616 ep->rep_attr.cap.max_recv_sge);
617
618 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400619 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500620 if (ep->rep_cqinit <= 2)
621 ep->rep_cqinit = 0; /* always signal? */
Chuck Lever8d38de62016-11-29 10:52:16 -0500622 rpcrdma_init_cqcount(ep, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400623 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400624 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400625
Chuck Lever2fa8f882016-03-04 11:28:53 -0500626 sendcq = ib_alloc_cq(ia->ri_device, NULL,
627 ep->rep_attr.cap.max_send_wr + 1,
628 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400629 if (IS_ERR(sendcq)) {
630 rc = PTR_ERR(sendcq);
631 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400632 __func__, rc);
633 goto out1;
634 }
635
Chuck Lever552bf222016-03-04 11:28:36 -0500636 recvcq = ib_alloc_cq(ia->ri_device, NULL,
637 ep->rep_attr.cap.max_recv_wr + 1,
638 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400639 if (IS_ERR(recvcq)) {
640 rc = PTR_ERR(recvcq);
641 dprintk("RPC: %s: failed to create recv CQ: %i\n",
642 __func__, rc);
643 goto out2;
644 }
645
Chuck Leverfc664482014-05-28 10:33:25 -0400646 ep->rep_attr.send_cq = sendcq;
647 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400648
649 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400650 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400651
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400652 /* Prepare RDMA-CM private message */
653 pmsg->cp_magic = rpcrdma_cmp_magic;
654 pmsg->cp_version = RPCRDMA_CMP_VERSION;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400655 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400656 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
657 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
658 ep->rep_remote_cma.private_data = pmsg;
659 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400660
661 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400662 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200663 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400664 ep->rep_remote_cma.responder_resources = 32;
665 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500666 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200667 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400668
Chuck Leverb2dde942016-05-02 14:43:03 -0400669 /* Limit transport retries so client can detect server
670 * GID changes quickly. RPC layer handles re-establishing
671 * transport connection and retransmission.
672 */
673 ep->rep_remote_cma.retry_count = 6;
674
675 /* RPC-over-RDMA handles its own flow control. In addition,
676 * make all RNR NAKs visible so we know that RPC-over-RDMA
677 * flow control is working correctly (no NAKs should be seen).
678 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400679 ep->rep_remote_cma.flow_control = 0;
680 ep->rep_remote_cma.rnr_retry_count = 0;
681
682 return 0;
683
684out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500685 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400686out1:
687 return rc;
688}
689
690/*
691 * rpcrdma_ep_destroy
692 *
693 * Disconnect and destroy endpoint. After this, the only
694 * valid operations on the ep are to free it (if dynamically
695 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400696 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400697void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400698rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
699{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400700 dprintk("RPC: %s: entering, connected is %d\n",
701 __func__, ep->rep_connected);
702
Chuck Lever254f91e2014-05-28 10:32:17 -0400703 cancel_delayed_work_sync(&ep->rep_connect_worker);
704
Steve Wise72c02172015-09-21 12:24:23 -0500705 if (ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400706 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400707 rdma_destroy_qp(ia->ri_id);
708 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400709 }
710
Chuck Lever552bf222016-03-04 11:28:36 -0500711 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500712 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400713}
714
Chuck Levera9b0e382017-04-11 13:23:26 -0400715/* Re-establish a connection after a device removal event.
716 * Unlike a normal reconnection, a fresh PD and a new set
717 * of MRs and buffers is needed.
718 */
719static int
720rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
721 struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
722{
723 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
724 int rc, err;
725
726 pr_info("%s: r_xprt = %p\n", __func__, r_xprt);
727
728 rc = -EHOSTUNREACH;
729 if (rpcrdma_ia_open(r_xprt, sap))
730 goto out1;
731
732 rc = -ENOMEM;
733 err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
734 if (err) {
735 pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
736 goto out2;
737 }
738
739 rc = -ENETUNREACH;
740 err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
741 if (err) {
742 pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
743 goto out3;
744 }
745
746 rpcrdma_create_mrs(r_xprt);
747 return 0;
748
749out3:
750 rpcrdma_ep_destroy(ep, ia);
751out2:
752 rpcrdma_ia_close(ia);
753out1:
754 return rc;
755}
756
Chuck Lever18908962017-04-11 13:23:18 -0400757static int
758rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
759 struct rpcrdma_ia *ia)
760{
761 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
762 struct rdma_cm_id *id, *old;
763 int err, rc;
764
765 dprintk("RPC: %s: reconnecting...\n", __func__);
766
767 rpcrdma_ep_disconnect(ep, ia);
768
769 rc = -EHOSTUNREACH;
770 id = rpcrdma_create_id(r_xprt, ia, sap);
771 if (IS_ERR(id))
772 goto out;
773
774 /* As long as the new ID points to the same device as the
775 * old ID, we can reuse the transport's existing PD and all
776 * previously allocated MRs. Also, the same device means
777 * the transport's previous DMA mappings are still valid.
778 *
779 * This is a sanity check only. There should be no way these
780 * point to two different devices here.
781 */
782 old = id;
783 rc = -ENETUNREACH;
784 if (ia->ri_device != id->device) {
785 pr_err("rpcrdma: can't reconnect on different device!\n");
786 goto out_destroy;
787 }
788
789 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
790 if (err) {
791 dprintk("RPC: %s: rdma_create_qp returned %d\n",
792 __func__, err);
793 goto out_destroy;
794 }
795
796 /* Atomically replace the transport's ID and QP. */
797 rc = 0;
798 old = ia->ri_id;
799 ia->ri_id = id;
800 rdma_destroy_qp(old);
801
802out_destroy:
803 rpcrdma_destroy_id(old);
804out:
805 return rc;
806}
807
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400808/*
809 * Connect unconnected endpoint.
810 */
811int
812rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
813{
Chuck Lever0a904872017-02-08 17:00:35 -0500814 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
815 rx_ia);
Chuck Lever0a904872017-02-08 17:00:35 -0500816 unsigned int extras;
Chuck Lever18908962017-04-11 13:23:18 -0400817 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400818
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400819retry:
Chuck Lever18908962017-04-11 13:23:18 -0400820 switch (ep->rep_connected) {
821 case 0:
Chuck Leverec62f402014-05-28 10:34:07 -0400822 dprintk("RPC: %s: connecting...\n", __func__);
823 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
824 if (rc) {
825 dprintk("RPC: %s: rdma_create_qp failed %i\n",
826 __func__, rc);
Chuck Lever18908962017-04-11 13:23:18 -0400827 rc = -ENETUNREACH;
828 goto out_noupdate;
Chuck Leverec62f402014-05-28 10:34:07 -0400829 }
Chuck Lever18908962017-04-11 13:23:18 -0400830 break;
Chuck Levera9b0e382017-04-11 13:23:26 -0400831 case -ENODEV:
832 rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
833 if (rc)
834 goto out_noupdate;
835 break;
Chuck Lever18908962017-04-11 13:23:18 -0400836 default:
837 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
838 if (rc)
839 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400840 }
841
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400842 ep->rep_connected = 0;
843
844 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
845 if (rc) {
846 dprintk("RPC: %s: rdma_connect() failed with %i\n",
847 __func__, rc);
848 goto out;
849 }
850
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400851 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400852 if (ep->rep_connected <= 0) {
Chuck Lever0a904872017-02-08 17:00:35 -0500853 if (ep->rep_connected == -EAGAIN)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400854 goto retry;
855 rc = ep->rep_connected;
Chuck Lever0a904872017-02-08 17:00:35 -0500856 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400857 }
858
Chuck Lever0a904872017-02-08 17:00:35 -0500859 dprintk("RPC: %s: connected\n", __func__);
860 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
861 if (extras)
862 rpcrdma_ep_post_extra_recv(r_xprt, extras);
863
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400864out:
865 if (rc)
866 ep->rep_connected = rc;
Chuck Lever18908962017-04-11 13:23:18 -0400867
868out_noupdate:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400869 return rc;
870}
871
872/*
873 * rpcrdma_ep_disconnect
874 *
875 * This is separate from destroy to facilitate the ability
876 * to reconnect without recreating the endpoint.
877 *
878 * This call is not reentrant, and must not be made in parallel
879 * on the same endpoint.
880 */
Chuck Lever282191c2014-07-29 17:25:55 -0400881void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400882rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
883{
884 int rc;
885
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400886 rc = rdma_disconnect(ia->ri_id);
887 if (!rc) {
888 /* returns without wait if not connected */
889 wait_event_interruptible(ep->rep_connect_wait,
890 ep->rep_connected != 1);
891 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
892 (ep->rep_connected == 1) ? "still " : "dis");
893 } else {
894 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
895 ep->rep_connected = rc;
896 }
Chuck Lever550d7502016-05-02 14:41:47 -0400897
898 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400899}
900
Chuck Lever505bbe62016-06-29 13:52:54 -0400901static void
902rpcrdma_mr_recovery_worker(struct work_struct *work)
903{
904 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
905 rb_recovery_worker.work);
906 struct rpcrdma_mw *mw;
907
908 spin_lock(&buf->rb_recovery_lock);
909 while (!list_empty(&buf->rb_stale_mrs)) {
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500910 mw = rpcrdma_pop_mw(&buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400911 spin_unlock(&buf->rb_recovery_lock);
912
913 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
914 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
915
916 spin_lock(&buf->rb_recovery_lock);
kbuild test robot53d78522016-07-16 06:02:05 +0800917 }
Chuck Lever505bbe62016-06-29 13:52:54 -0400918 spin_unlock(&buf->rb_recovery_lock);
919}
920
921void
922rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
923{
924 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
925 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
926
927 spin_lock(&buf->rb_recovery_lock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500928 rpcrdma_push_mw(mw, &buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400929 spin_unlock(&buf->rb_recovery_lock);
930
931 schedule_delayed_work(&buf->rb_recovery_worker, 0);
932}
933
Chuck Levere2ac2362016-06-29 13:54:00 -0400934static void
935rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
936{
937 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
938 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
939 unsigned int count;
940 LIST_HEAD(free);
941 LIST_HEAD(all);
942
943 for (count = 0; count < 32; count++) {
944 struct rpcrdma_mw *mw;
945 int rc;
946
947 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
948 if (!mw)
949 break;
950
951 rc = ia->ri_ops->ro_init_mr(ia, mw);
952 if (rc) {
953 kfree(mw);
954 break;
955 }
956
957 mw->mw_xprt = r_xprt;
958
959 list_add(&mw->mw_list, &free);
960 list_add(&mw->mw_all, &all);
961 }
962
963 spin_lock(&buf->rb_mwlock);
964 list_splice(&free, &buf->rb_mws);
965 list_splice(&all, &buf->rb_all);
966 r_xprt->rx_stats.mrs_allocated += count;
967 spin_unlock(&buf->rb_mwlock);
968
969 dprintk("RPC: %s: created %u MRs\n", __func__, count);
970}
971
972static void
973rpcrdma_mr_refresh_worker(struct work_struct *work)
974{
975 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
976 rb_refresh_worker.work);
977 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
978 rx_buf);
979
980 rpcrdma_create_mrs(r_xprt);
981}
982
Chuck Leverf531a5d2015-10-24 17:27:43 -0400983struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500984rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
985{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400986 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500987 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500988
Chuck Lever85275c82015-01-21 11:04:16 -0500989 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500990 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500991 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500992
Chuck Leverf531a5d2015-10-24 17:27:43 -0400993 INIT_LIST_HEAD(&req->rl_free);
994 spin_lock(&buffer->rb_reqslock);
995 list_add(&req->rl_all, &buffer->rb_allreqs);
996 spin_unlock(&buffer->rb_reqslock);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500997 req->rl_cqe.done = rpcrdma_wc_send;
Chuck Lever13924022015-01-21 11:03:52 -0500998 req->rl_buffer = &r_xprt->rx_buf;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400999 INIT_LIST_HEAD(&req->rl_registered);
Chuck Lever90aab602016-09-15 10:56:43 -04001000 req->rl_send_wr.next = NULL;
1001 req->rl_send_wr.wr_cqe = &req->rl_cqe;
Chuck Lever655fec62016-09-15 10:57:24 -04001002 req->rl_send_wr.sg_list = req->rl_send_sge;
Chuck Lever90aab602016-09-15 10:56:43 -04001003 req->rl_send_wr.opcode = IB_WR_SEND;
Chuck Lever13924022015-01-21 11:03:52 -05001004 return req;
Chuck Lever13924022015-01-21 11:03:52 -05001005}
1006
Chuck Leverf531a5d2015-10-24 17:27:43 -04001007struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -05001008rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
1009{
1010 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -05001011 struct rpcrdma_rep *rep;
1012 int rc;
1013
1014 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001015 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -05001016 if (rep == NULL)
1017 goto out;
Chuck Lever13924022015-01-21 11:03:52 -05001018
Chuck Lever13650c22016-09-15 10:56:26 -04001019 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -04001020 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever6b1184c2015-01-21 11:04:25 -05001021 if (IS_ERR(rep->rr_rdmabuf)) {
1022 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001023 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001024 }
Chuck Lever13924022015-01-21 11:03:52 -05001025
Chuck Lever1519e962016-09-15 10:57:49 -04001026 rep->rr_cqe.done = rpcrdma_wc_receive;
Chuck Leverfed171b2015-05-26 11:51:37 -04001027 rep->rr_rxprt = r_xprt;
Chuck Lever496b77a2016-09-15 10:57:57 -04001028 INIT_WORK(&rep->rr_work, rpcrdma_reply_handler);
Chuck Lever6ea8e712016-09-15 10:56:51 -04001029 rep->rr_recv_wr.next = NULL;
1030 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
1031 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
1032 rep->rr_recv_wr.num_sge = 1;
Chuck Lever13924022015-01-21 11:03:52 -05001033 return rep;
1034
1035out_free:
1036 kfree(rep);
1037out:
1038 return ERR_PTR(rc);
1039}
1040
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001041int
Chuck Leverac920d02015-01-21 11:03:44 -05001042rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001043{
Chuck Leverac920d02015-01-21 11:03:44 -05001044 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001045 int i, rc;
1046
Chuck Lever1e465fd2015-10-24 17:27:02 -04001047 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001048 buf->rb_bc_srv_max_requests = 0;
Chuck Lever23826c72016-03-04 11:28:27 -05001049 atomic_set(&buf->rb_credits, 1);
Chuck Levere2ac2362016-06-29 13:54:00 -04001050 spin_lock_init(&buf->rb_mwlock);
Chuck Lever505bbe62016-06-29 13:52:54 -04001051 spin_lock_init(&buf->rb_lock);
1052 spin_lock_init(&buf->rb_recovery_lock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001053 INIT_LIST_HEAD(&buf->rb_mws);
1054 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever505bbe62016-06-29 13:52:54 -04001055 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001056 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1057 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001058 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
1059 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001060
Chuck Levere2ac2362016-06-29 13:54:00 -04001061 rpcrdma_create_mrs(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001062
Chuck Lever1e465fd2015-10-24 17:27:02 -04001063 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001064 INIT_LIST_HEAD(&buf->rb_allreqs);
1065 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001066 for (i = 0; i < buf->rb_max_requests; i++) {
1067 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001068
Chuck Lever13924022015-01-21 11:03:52 -05001069 req = rpcrdma_create_req(r_xprt);
1070 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001071 dprintk("RPC: %s: request buffer %d alloc"
1072 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001073 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001074 goto out;
1075 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001076 req->rl_backchannel = false;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001077 list_add(&req->rl_free, &buf->rb_send_bufs);
1078 }
1079
1080 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001081 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001082 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001083
Chuck Lever13924022015-01-21 11:03:52 -05001084 rep = rpcrdma_create_rep(r_xprt);
1085 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001086 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1087 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001088 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001089 goto out;
1090 }
Chuck Lever1e465fd2015-10-24 17:27:02 -04001091 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001092 }
Chuck Lever13924022015-01-21 11:03:52 -05001093
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001094 return 0;
1095out:
1096 rpcrdma_buffer_destroy(buf);
1097 return rc;
1098}
1099
Chuck Lever1e465fd2015-10-24 17:27:02 -04001100static struct rpcrdma_req *
1101rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1102{
1103 struct rpcrdma_req *req;
1104
1105 req = list_first_entry(&buf->rb_send_bufs,
1106 struct rpcrdma_req, rl_free);
1107 list_del(&req->rl_free);
1108 return req;
1109}
1110
1111static struct rpcrdma_rep *
1112rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1113{
1114 struct rpcrdma_rep *rep;
1115
1116 rep = list_first_entry(&buf->rb_recv_bufs,
1117 struct rpcrdma_rep, rr_list);
1118 list_del(&rep->rr_list);
1119 return rep;
1120}
1121
Chuck Lever2e845222014-07-29 17:25:38 -04001122static void
Chuck Lever13650c22016-09-15 10:56:26 -04001123rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -05001124{
Chuck Lever13650c22016-09-15 10:56:26 -04001125 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001126 kfree(rep);
1127}
1128
Chuck Leverf531a5d2015-10-24 17:27:43 -04001129void
Chuck Lever13650c22016-09-15 10:56:26 -04001130rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -05001131{
Chuck Lever13650c22016-09-15 10:56:26 -04001132 rpcrdma_free_regbuf(req->rl_recvbuf);
1133 rpcrdma_free_regbuf(req->rl_sendbuf);
1134 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001135 kfree(req);
1136}
1137
Chuck Levere2ac2362016-06-29 13:54:00 -04001138static void
1139rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1140{
1141 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1142 rx_buf);
1143 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1144 struct rpcrdma_mw *mw;
1145 unsigned int count;
1146
1147 count = 0;
1148 spin_lock(&buf->rb_mwlock);
1149 while (!list_empty(&buf->rb_all)) {
1150 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1151 list_del(&mw->mw_all);
1152
1153 spin_unlock(&buf->rb_mwlock);
1154 ia->ri_ops->ro_release_mr(mw);
1155 count++;
1156 spin_lock(&buf->rb_mwlock);
1157 }
1158 spin_unlock(&buf->rb_mwlock);
1159 r_xprt->rx_stats.mrs_allocated = 0;
1160
1161 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1162}
1163
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001164void
1165rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1166{
Chuck Lever505bbe62016-06-29 13:52:54 -04001167 cancel_delayed_work_sync(&buf->rb_recovery_worker);
Chuck Lever9378b272017-04-11 13:22:29 -04001168 cancel_delayed_work_sync(&buf->rb_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001169
Chuck Lever1e465fd2015-10-24 17:27:02 -04001170 while (!list_empty(&buf->rb_recv_bufs)) {
1171 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001172
Chuck Lever1e465fd2015-10-24 17:27:02 -04001173 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001174 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001175 }
Chuck Lever05c97462016-09-06 11:22:58 -04001176 buf->rb_send_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001177
Chuck Leverf531a5d2015-10-24 17:27:43 -04001178 spin_lock(&buf->rb_reqslock);
1179 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001180 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001181
Chuck Leverf531a5d2015-10-24 17:27:43 -04001182 req = list_first_entry(&buf->rb_allreqs,
1183 struct rpcrdma_req, rl_all);
1184 list_del(&req->rl_all);
1185
1186 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001187 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001188 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001189 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001190 spin_unlock(&buf->rb_reqslock);
Chuck Lever05c97462016-09-06 11:22:58 -04001191 buf->rb_recv_count = 0;
Chuck Lever9f9d8022014-07-29 17:24:45 -04001192
Chuck Levere2ac2362016-06-29 13:54:00 -04001193 rpcrdma_destroy_mrs(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001194}
1195
Chuck Lever346aa662015-05-26 11:52:06 -04001196struct rpcrdma_mw *
1197rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001198{
Chuck Lever346aa662015-05-26 11:52:06 -04001199 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1200 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001201
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001202 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001203 if (!list_empty(&buf->rb_mws))
1204 mw = rpcrdma_pop_mw(&buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001205 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001206
1207 if (!mw)
Chuck Levere2ac2362016-06-29 13:54:00 -04001208 goto out_nomws;
Chuck Lever346aa662015-05-26 11:52:06 -04001209 return mw;
Chuck Levere2ac2362016-06-29 13:54:00 -04001210
1211out_nomws:
1212 dprintk("RPC: %s: no MWs available\n", __func__);
Chuck Leverbebd0312017-04-11 13:23:10 -04001213 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1214 schedule_delayed_work(&buf->rb_refresh_worker, 0);
Chuck Levere2ac2362016-06-29 13:54:00 -04001215
1216 /* Allow the reply handler and refresh worker to run */
1217 cond_resched();
1218
1219 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001220}
1221
Chuck Lever346aa662015-05-26 11:52:06 -04001222void
1223rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001224{
Chuck Lever346aa662015-05-26 11:52:06 -04001225 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001226
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001227 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001228 rpcrdma_push_mw(mw, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001229 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001230}
1231
Chuck Lever05c97462016-09-06 11:22:58 -04001232static struct rpcrdma_rep *
1233rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1234{
1235 /* If an RPC previously completed without a reply (say, a
1236 * credential problem or a soft timeout occurs) then hold off
1237 * on supplying more Receive buffers until the number of new
1238 * pending RPCs catches up to the number of posted Receives.
1239 */
1240 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1241 return NULL;
1242
1243 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1244 return NULL;
1245 buffers->rb_recv_count++;
1246 return rpcrdma_buffer_get_rep_locked(buffers);
1247}
1248
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001249/*
1250 * Get a set of request/reply buffers.
Chuck Lever78d506e2016-09-06 11:22:49 -04001251 *
1252 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001253 */
1254struct rpcrdma_req *
1255rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1256{
1257 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001258
Chuck Levera5b027e2015-10-24 17:27:27 -04001259 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001260 if (list_empty(&buffers->rb_send_bufs))
1261 goto out_reqbuf;
Chuck Lever05c97462016-09-06 11:22:58 -04001262 buffers->rb_send_count++;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001263 req = rpcrdma_buffer_get_req_locked(buffers);
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 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001267
Chuck Lever1e465fd2015-10-24 17:27:02 -04001268out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001269 spin_unlock(&buffers->rb_lock);
Chuck Lever78d506e2016-09-06 11:22:49 -04001270 pr_warn("RPC: %s: out of request buffers\n", __func__);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001271 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001272}
1273
1274/*
1275 * Put request/reply buffers back into pool.
1276 * Pre-decrement counter/array index.
1277 */
1278void
1279rpcrdma_buffer_put(struct rpcrdma_req *req)
1280{
1281 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001282 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001283
Chuck Lever90aab602016-09-15 10:56:43 -04001284 req->rl_send_wr.num_sge = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001285 req->rl_reply = NULL;
1286
Chuck Levera5b027e2015-10-24 17:27:27 -04001287 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001288 buffers->rb_send_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001289 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001290 if (rep) {
1291 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001292 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001293 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001294 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001295}
1296
1297/*
1298 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001299 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001300 */
1301void
1302rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1303{
1304 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001305
Chuck Levera5b027e2015-10-24 17:27:27 -04001306 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001307 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001308 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001309}
1310
1311/*
1312 * Put reply buffers back into pool when not attached to
1313 * request. This happens in error conditions.
1314 */
1315void
1316rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1317{
Chuck Leverfed171b2015-05-26 11:51:37 -04001318 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001319
Chuck Levera5b027e2015-10-24 17:27:27 -04001320 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001321 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001322 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001323 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001324}
1325
Chuck Lever9128c3e2015-01-21 11:04:00 -05001326/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001327 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001328 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001329 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001330 * @flags: GFP flags
1331 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001332 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1333 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001334 *
1335 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001336 * receiving the payload of RDMA RECV operations. During Long Calls
1337 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001338 */
1339struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001340rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1341 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001342{
1343 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001344
Chuck Lever9128c3e2015-01-21 11:04:00 -05001345 rb = kmalloc(sizeof(*rb) + size, flags);
1346 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001347 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001348
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001349 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001350 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001351 rb->rg_iov.length = size;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001352
1353 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001354}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001355
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001356/**
1357 * __rpcrdma_map_regbuf - DMA-map a regbuf
1358 * @ia: controlling rpcrdma_ia
1359 * @rb: regbuf to be mapped
1360 */
1361bool
1362__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1363{
Chuck Lever91a10c52017-04-11 13:23:02 -04001364 struct ib_device *device = ia->ri_device;
1365
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001366 if (rb->rg_direction == DMA_NONE)
1367 return false;
1368
Chuck Lever91a10c52017-04-11 13:23:02 -04001369 rb->rg_iov.addr = ib_dma_map_single(device,
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001370 (void *)rb->rg_base,
1371 rdmab_length(rb),
1372 rb->rg_direction);
Chuck Lever91a10c52017-04-11 13:23:02 -04001373 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001374 return false;
1375
Chuck Lever91a10c52017-04-11 13:23:02 -04001376 rb->rg_device = device;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001377 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1378 return true;
1379}
1380
1381static void
1382rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1383{
1384 if (!rpcrdma_regbuf_is_mapped(rb))
1385 return;
1386
1387 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1388 rdmab_length(rb), rb->rg_direction);
1389 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001390}
1391
1392/**
1393 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001394 * @rb: regbuf to be deregistered and freed
1395 */
1396void
Chuck Lever13650c22016-09-15 10:56:26 -04001397rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001398{
Chuck Levere531dca2015-08-03 13:03:20 -04001399 if (!rb)
1400 return;
1401
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001402 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001403 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001404}
1405
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001406/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001407 * Prepost any receive buffer, then post send.
1408 *
1409 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1410 */
1411int
1412rpcrdma_ep_post(struct rpcrdma_ia *ia,
1413 struct rpcrdma_ep *ep,
1414 struct rpcrdma_req *req)
1415{
Chuck Lever90aab602016-09-15 10:56:43 -04001416 struct ib_send_wr *send_wr = &req->rl_send_wr;
1417 struct ib_send_wr *send_wr_fail;
Chuck Lever655fec62016-09-15 10:57:24 -04001418 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001419
Chuck Lever90aab602016-09-15 10:56:43 -04001420 if (req->rl_reply) {
1421 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001422 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001423 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001424 req->rl_reply = NULL;
1425 }
1426
Chuck Leverb3221d62015-08-03 13:03:39 -04001427 dprintk("RPC: %s: posting %d s/g entries\n",
Chuck Lever90aab602016-09-15 10:56:43 -04001428 __func__, send_wr->num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001429
Chuck Lever8d38de62016-11-29 10:52:16 -05001430 rpcrdma_set_signaled(ep, send_wr);
Chuck Lever90aab602016-09-15 10:56:43 -04001431 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001432 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001433 goto out_postsend_err;
1434 return 0;
1435
1436out_postsend_err:
1437 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1438 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001439}
1440
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001441int
1442rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001443 struct rpcrdma_rep *rep)
1444{
Chuck Lever6ea8e712016-09-15 10:56:51 -04001445 struct ib_recv_wr *recv_wr_fail;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001446 int rc;
1447
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001448 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1449 goto out_map;
Chuck Lever6ea8e712016-09-15 10:56:51 -04001450 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001451 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001452 goto out_postrecv;
1453 return 0;
1454
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001455out_map:
1456 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1457 return -EIO;
1458
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001459out_postrecv:
1460 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1461 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001462}
Chuck Lever43e95982014-07-29 17:23:34 -04001463
Chuck Leverf531a5d2015-10-24 17:27:43 -04001464/**
1465 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1466 * @r_xprt: transport associated with these backchannel resources
1467 * @min_reqs: minimum number of incoming requests expected
1468 *
1469 * Returns zero if all requested buffers were posted, or a negative errno.
1470 */
1471int
1472rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1473{
1474 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1475 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001476 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001477 int rc;
1478
1479 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001480 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001481 if (list_empty(&buffers->rb_recv_bufs))
1482 goto out_reqbuf;
1483 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001484 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001485
Chuck Leverb1573802016-09-15 10:56:35 -04001486 rc = rpcrdma_ep_post_recv(ia, rep);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001487 if (rc)
1488 goto out_rc;
1489 }
1490
1491 return 0;
1492
1493out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001494 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001495 pr_warn("%s: no extra receive buffers\n", __func__);
1496 return -ENOMEM;
1497
1498out_rc:
1499 rpcrdma_recv_buffer_put(rep);
1500 return rc;
1501}