blob: 6479ad3fe69df74ea06a5bc4ac8d90a01b97f30d [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 Leverbebd0312017-04-11 13:23:10 -040072static void rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf);
73static void rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040074
Chuck Leverfe97b472015-10-24 17:27:10 -040075static struct workqueue_struct *rpcrdma_receive_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040076
Chuck Leverfe97b472015-10-24 17:27:10 -040077int
78rpcrdma_alloc_wq(void)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040079{
Chuck Leverfe97b472015-10-24 17:27:10 -040080 struct workqueue_struct *recv_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040081
Chuck Leverfe97b472015-10-24 17:27:10 -040082 recv_wq = alloc_workqueue("xprtrdma_receive",
83 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI,
84 0);
85 if (!recv_wq)
86 return -ENOMEM;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040087
Chuck Leverfe97b472015-10-24 17:27:10 -040088 rpcrdma_receive_wq = recv_wq;
89 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040090}
91
Chuck Leverfe97b472015-10-24 17:27:10 -040092void
93rpcrdma_destroy_wq(void)
Chuck Leverf1a03b72014-11-08 20:14:37 -050094{
Chuck Leverfe97b472015-10-24 17:27:10 -040095 struct workqueue_struct *wq;
Chuck Leverf1a03b72014-11-08 20:14:37 -050096
Chuck Leverfe97b472015-10-24 17:27:10 -040097 if (rpcrdma_receive_wq) {
98 wq = rpcrdma_receive_wq;
99 rpcrdma_receive_wq = NULL;
100 destroy_workqueue(wq);
101 }
Chuck Leverf1a03b72014-11-08 20:14:37 -0500102}
103
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400104static void
105rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
106{
107 struct rpcrdma_ep *ep = context;
108
Chuck Lever2f6922c2016-11-29 10:53:21 -0500109 pr_err("rpcrdma: %s on device %s ep %p\n",
110 ib_event_msg(event->event), event->device->name, context);
111
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400112 if (ep->rep_connected == 1) {
113 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500114 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400115 wake_up_all(&ep->rep_connect_wait);
116 }
117}
118
Chuck Lever2fa8f882016-03-04 11:28:53 -0500119/**
120 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
121 * @cq: completion queue (ignored)
122 * @wc: completed WR
123 *
Chuck Lever4220a072015-10-24 17:26:45 -0400124 */
125static void
Chuck Lever2fa8f882016-03-04 11:28:53 -0500126rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400127{
Chuck Lever2fa8f882016-03-04 11:28:53 -0500128 /* WARNING: Only wr_cqe and status are reliable at this point */
129 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
130 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
131 ib_wc_status_msg(wc->status),
132 wc->status, wc->vendor_err);
Chuck Leverfc664482014-05-28 10:33:25 -0400133}
134
Chuck Lever23826c72016-03-04 11:28:27 -0500135/* Perform basic sanity checking to avoid using garbage
136 * to update the credit grant value.
137 */
138static void
139rpcrdma_update_granted_credits(struct rpcrdma_rep *rep)
140{
141 struct rpcrdma_msg *rmsgp = rdmab_to_msg(rep->rr_rdmabuf);
142 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
143 u32 credits;
144
145 if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
146 return;
147
148 credits = be32_to_cpu(rmsgp->rm_credit);
149 if (credits == 0)
150 credits = 1; /* don't deadlock */
151 else if (credits > buffer->rb_max_requests)
152 credits = buffer->rb_max_requests;
153
154 atomic_set(&buffer->rb_credits, credits);
155}
156
Chuck Lever552bf222016-03-04 11:28:36 -0500157/**
Chuck Lever1519e962016-09-15 10:57:49 -0400158 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
Chuck Lever552bf222016-03-04 11:28:36 -0500159 * @cq: completion queue (ignored)
160 * @wc: completed WR
161 *
162 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400163static void
Chuck Lever1519e962016-09-15 10:57:49 -0400164rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400165{
Chuck Lever552bf222016-03-04 11:28:36 -0500166 struct ib_cqe *cqe = wc->wr_cqe;
167 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
168 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400169
Chuck Lever85024272015-01-21 11:02:04 -0500170 /* WARNING: Only wr_id and status are reliable at this point */
171 if (wc->status != IB_WC_SUCCESS)
172 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400173
Chuck Lever85024272015-01-21 11:02:04 -0500174 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400175 if (wc->opcode != IB_WC_RECV)
176 return;
177
Chuck Lever85024272015-01-21 11:02:04 -0500178 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
179 __func__, rep, wc->byte_len);
180
Chuck Leverfc664482014-05-28 10:33:25 -0400181 rep->rr_len = wc->byte_len;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400182 rep->rr_wc_flags = wc->wc_flags;
183 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
184
Chuck Lever91a10c52017-04-11 13:23:02 -0400185 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
Chuck Lever6b1184c2015-01-21 11:04:25 -0500186 rdmab_addr(rep->rr_rdmabuf),
187 rep->rr_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500188
189 rpcrdma_update_granted_credits(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400190
191out_schedule:
Chuck Leverfe97b472015-10-24 17:27:10 -0400192 queue_work(rpcrdma_receive_wq, &rep->rr_work);
Chuck Lever85024272015-01-21 11:02:04 -0500193 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400194
Chuck Lever85024272015-01-21 11:02:04 -0500195out_fail:
196 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500197 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
198 ib_wc_status_msg(wc->status),
199 wc->status, wc->vendor_err);
Chuck Leverb0e178a2015-10-24 17:26:54 -0400200 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500201 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400202}
203
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400204static void
205rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
206 struct rdma_conn_param *param)
207{
208 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
209 const struct rpcrdma_connect_private *pmsg = param->private_data;
210 unsigned int rsize, wsize;
211
Chuck Leverc8b920b2016-09-15 10:57:16 -0400212 /* Default settings for RPC-over-RDMA Version One */
213 r_xprt->rx_ia.ri_reminv_expected = false;
Chuck Leverb5f0afb2017-02-08 16:59:54 -0500214 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400215 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
216 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
217
218 if (pmsg &&
219 pmsg->cp_magic == rpcrdma_cmp_magic &&
220 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
Chuck Leverc8b920b2016-09-15 10:57:16 -0400221 r_xprt->rx_ia.ri_reminv_expected = true;
Chuck Leverc95a3c62017-02-08 17:00:02 -0500222 r_xprt->rx_ia.ri_implicit_roundup = true;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400223 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
224 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
225 }
226
227 if (rsize < cdata->inline_rsize)
228 cdata->inline_rsize = rsize;
229 if (wsize < cdata->inline_wsize)
230 cdata->inline_wsize = wsize;
Chuck Lever6d6bf722016-11-29 10:53:13 -0500231 dprintk("RPC: %s: max send %u, max recv %u\n",
232 __func__, cdata->inline_wsize, cdata->inline_rsize);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400233 rpcrdma_set_max_header_sizes(r_xprt);
234}
235
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400236static int
237rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
238{
239 struct rpcrdma_xprt *xprt = id->context;
240 struct rpcrdma_ia *ia = &xprt->rx_ia;
241 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500242#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400243 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800244#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500245 struct ib_qp_attr *attr = &ia->ri_qp_attr;
246 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400247 int connstate = 0;
248
249 switch (event->event) {
250 case RDMA_CM_EVENT_ADDR_RESOLVED:
251 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400252 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400253 complete(&ia->ri_done);
254 break;
255 case RDMA_CM_EVENT_ADDR_ERROR:
256 ia->ri_async_rc = -EHOSTUNREACH;
257 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
258 __func__, ep);
259 complete(&ia->ri_done);
260 break;
261 case RDMA_CM_EVENT_ROUTE_ERROR:
262 ia->ri_async_rc = -ENETUNREACH;
263 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
264 __func__, ep);
265 complete(&ia->ri_done);
266 break;
Chuck Leverbebd0312017-04-11 13:23:10 -0400267 case RDMA_CM_EVENT_DEVICE_REMOVAL:
268#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
269 pr_info("rpcrdma: removing device for %pIS:%u\n",
270 sap, rpc_get_port(sap));
271#endif
272 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
273 ep->rep_connected = -ENODEV;
274 xprt_force_disconnect(&xprt->rx_xprt);
275 wait_for_completion(&ia->ri_remove_done);
276
277 ia->ri_id = NULL;
278 ia->ri_pd = NULL;
279 ia->ri_device = NULL;
280 /* Return 1 to ensure the core destroys the id. */
281 return 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400282 case RDMA_CM_EVENT_ESTABLISHED:
283 connstate = 1;
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500284 ib_query_qp(ia->ri_id->qp, attr,
285 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
286 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400287 dprintk("RPC: %s: %d responder resources"
288 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500289 __func__, attr->max_dest_rd_atomic,
290 attr->max_rd_atomic);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400291 rpcrdma_update_connect_private(xprt, &event->param.conn);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400292 goto connected;
293 case RDMA_CM_EVENT_CONNECT_ERROR:
294 connstate = -ENOTCONN;
295 goto connected;
296 case RDMA_CM_EVENT_UNREACHABLE:
297 connstate = -ENETDOWN;
298 goto connected;
299 case RDMA_CM_EVENT_REJECTED:
Chuck Lever0a904872017-02-08 17:00:35 -0500300#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
301 pr_info("rpcrdma: connection to %pIS:%u on %s rejected: %s\n",
302 sap, rpc_get_port(sap), ia->ri_device->name,
303 rdma_reject_msg(id, event->status));
304#endif
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400305 connstate = -ECONNREFUSED;
Chuck Lever0a904872017-02-08 17:00:35 -0500306 if (event->status == IB_CM_REJ_STALE_CONN)
307 connstate = -EAGAIN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400308 goto connected;
309 case RDMA_CM_EVENT_DISCONNECTED:
310 connstate = -ECONNABORTED;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400311connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400312 dprintk("RPC: %s: %sconnected\n",
313 __func__, connstate > 0 ? "" : "dis");
Chuck Lever23826c72016-03-04 11:28:27 -0500314 atomic_set(&xprt->rx_buf.rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400315 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500316 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400317 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400318 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400319 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400320 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
321 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300322 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400323 break;
324 }
325
Jeff Laytonf895b252014-11-17 16:58:04 -0500326#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400327 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500328 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400329 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400330
Chuck Levera0ce85f2015-03-30 14:34:21 -0400331 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400332 sap, rpc_get_port(sap),
Chuck Lever89e0d1122015-05-26 11:51:56 -0400333 ia->ri_device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400334 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400335 xprt->rx_buf.rb_max_requests,
336 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
337 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400338 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
339 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400340 }
341#endif
342
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400343 return 0;
344}
345
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400346static void rpcrdma_destroy_id(struct rdma_cm_id *id)
347{
348 if (id) {
349 module_put(id->device->owner);
350 rdma_destroy_id(id);
351 }
352}
353
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400354static struct rdma_cm_id *
355rpcrdma_create_id(struct rpcrdma_xprt *xprt,
356 struct rpcrdma_ia *ia, struct sockaddr *addr)
357{
Chuck Lever109b88a2016-11-29 10:52:40 -0500358 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400359 struct rdma_cm_id *id;
360 int rc;
361
Tom Talpey1a954052008-10-09 15:01:31 -0400362 init_completion(&ia->ri_done);
Chuck Leverbebd0312017-04-11 13:23:10 -0400363 init_completion(&ia->ri_remove_done);
Tom Talpey1a954052008-10-09 15:01:31 -0400364
Guy Shapirofa201052015-10-22 15:20:10 +0300365 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
366 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400367 if (IS_ERR(id)) {
368 rc = PTR_ERR(id);
369 dprintk("RPC: %s: rdma_create_id() failed %i\n",
370 __func__, rc);
371 return id;
372 }
373
Tom Talpey5675add2008-10-09 15:01:41 -0400374 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400375 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
376 if (rc) {
377 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
378 __func__, rc);
379 goto out;
380 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500381 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
382 if (rc < 0) {
383 dprintk("RPC: %s: wait() exited: %i\n",
384 __func__, rc);
385 goto out;
386 }
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400387
388 /* FIXME:
389 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
390 * be pinned while there are active NFS/RDMA mounts to prevent
391 * hangs and crashes at umount time.
392 */
393 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
394 dprintk("RPC: %s: Failed to get device module\n",
395 __func__);
396 ia->ri_async_rc = -ENODEV;
397 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400398 rc = ia->ri_async_rc;
399 if (rc)
400 goto out;
401
Tom Talpey5675add2008-10-09 15:01:41 -0400402 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400403 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
404 if (rc) {
405 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
406 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400407 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400408 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500409 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
410 if (rc < 0) {
411 dprintk("RPC: %s: wait() exited: %i\n",
412 __func__, rc);
413 goto put;
414 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400415 rc = ia->ri_async_rc;
416 if (rc)
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400417 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400418
419 return id;
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400420put:
421 module_put(id->device->owner);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400422out:
423 rdma_destroy_id(id);
424 return ERR_PTR(rc);
425}
426
427/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400428 * Exported functions.
429 */
430
Chuck Leverfff09592017-04-11 13:22:54 -0400431/**
432 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
433 * @xprt: controlling transport
434 * @addr: IP address of remote peer
435 *
436 * Returns 0 on success, negative errno if an appropriate
437 * Interface Adapter could not be found and opened.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400438 */
439int
Chuck Leverfff09592017-04-11 13:22:54 -0400440rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400441{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400442 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400443 int rc;
444
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400445 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
446 if (IS_ERR(ia->ri_id)) {
447 rc = PTR_ERR(ia->ri_id);
Chuck Leverfff09592017-04-11 13:22:54 -0400448 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400449 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400450 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400451
Christoph Hellwiged082d32016-09-05 12:56:17 +0200452 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400453 if (IS_ERR(ia->ri_pd)) {
454 rc = PTR_ERR(ia->ri_pd);
Chuck Leverb54054c2016-06-29 13:53:27 -0400455 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
Chuck Leverfff09592017-04-11 13:22:54 -0400456 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400457 }
458
Chuck Leverfff09592017-04-11 13:22:54 -0400459 switch (xprt_rdma_memreg_strategy) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400460 case RPCRDMA_FRMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400461 if (frwr_is_supported(ia)) {
462 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
463 break;
464 }
465 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400466 case RPCRDMA_MTHCAFMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400467 if (fmr_is_supported(ia)) {
468 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
469 break;
470 }
471 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400472 default:
Chuck Leverfff09592017-04-11 13:22:54 -0400473 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
474 ia->ri_device->name, xprt_rdma_memreg_strategy);
Chuck Leverb54054c2016-06-29 13:53:27 -0400475 rc = -EINVAL;
Chuck Leverfff09592017-04-11 13:22:54 -0400476 goto out_err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400477 }
478
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400479 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500480
Chuck Leverfff09592017-04-11 13:22:54 -0400481out_err:
482 rpcrdma_ia_close(ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400483 return rc;
484}
485
Chuck Leverfff09592017-04-11 13:22:54 -0400486/**
Chuck Leverbebd0312017-04-11 13:23:10 -0400487 * rpcrdma_ia_remove - Handle device driver unload
488 * @ia: interface adapter being removed
489 *
490 * Divest transport H/W resources associated with this adapter,
491 * but allow it to be restored later.
492 */
493void
494rpcrdma_ia_remove(struct rpcrdma_ia *ia)
495{
496 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
497 rx_ia);
498 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
499 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
500 struct rpcrdma_req *req;
501 struct rpcrdma_rep *rep;
502
503 cancel_delayed_work_sync(&buf->rb_refresh_worker);
504
505 /* This is similar to rpcrdma_ep_destroy, but:
506 * - Don't cancel the connect worker.
507 * - Don't call rpcrdma_ep_disconnect, which waits
508 * for another conn upcall, which will deadlock.
509 * - rdma_disconnect is unneeded, the underlying
510 * connection is already gone.
511 */
512 if (ia->ri_id->qp) {
513 ib_drain_qp(ia->ri_id->qp);
514 rdma_destroy_qp(ia->ri_id);
515 ia->ri_id->qp = NULL;
516 }
517 ib_free_cq(ep->rep_attr.recv_cq);
518 ib_free_cq(ep->rep_attr.send_cq);
519
520 /* The ULP is responsible for ensuring all DMA
521 * mappings and MRs are gone.
522 */
523 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
524 rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
525 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
526 rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
527 rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
528 rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
529 }
530 rpcrdma_destroy_mrs(buf);
531
532 /* Allow waiters to continue */
533 complete(&ia->ri_remove_done);
534}
535
536/**
Chuck Leverfff09592017-04-11 13:22:54 -0400537 * rpcrdma_ia_close - Clean up/close an IA.
538 * @ia: interface adapter to close
539 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400540 */
541void
542rpcrdma_ia_close(struct rpcrdma_ia *ia)
543{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400544 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400545 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
546 if (ia->ri_id->qp)
547 rdma_destroy_qp(ia->ri_id);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400548 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400549 }
Chuck Leverfff09592017-04-11 13:22:54 -0400550 ia->ri_id = NULL;
551 ia->ri_device = NULL;
Chuck Lever6d446982015-05-26 11:51:27 -0400552
553 /* If the pd is still busy, xprtrdma missed freeing a resource */
554 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600555 ib_dealloc_pd(ia->ri_pd);
Chuck Leverfff09592017-04-11 13:22:54 -0400556 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400557}
558
559/*
560 * Create unconnected endpoint.
561 */
562int
563rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
Chuck Lever16f906d2017-02-08 17:00:10 -0500564 struct rpcrdma_create_data_internal *cdata)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400565{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400566 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
Chuck Lever16f906d2017-02-08 17:00:10 -0500567 unsigned int max_qp_wr, max_sge;
Chuck Leverfc664482014-05-28 10:33:25 -0400568 struct ib_cq *sendcq, *recvcq;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500569 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400570
Chuck Levereed50872017-03-11 15:52:47 -0500571 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
572 RPCRDMA_MAX_SEND_SGES);
Chuck Lever16f906d2017-02-08 17:00:10 -0500573 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
574 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
Chuck Leverb3221d62015-08-03 13:03:39 -0400575 return -ENOMEM;
576 }
Chuck Lever16f906d2017-02-08 17:00:10 -0500577 ia->ri_max_send_sges = max_sge - RPCRDMA_MIN_SEND_SGES;
Chuck Leverb3221d62015-08-03 13:03:39 -0400578
Or Gerlitze3e45b12015-12-18 10:59:48 +0200579 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400580 dprintk("RPC: %s: insufficient wqe's available\n",
581 __func__);
582 return -ENOMEM;
583 }
Chuck Lever550d7502016-05-02 14:41:47 -0400584 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
Chuck Lever124fa172015-10-24 17:27:51 -0400585
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400586 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400587 if (cdata->max_requests > max_qp_wr)
588 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400589
590 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
591 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400592 ep->rep_attr.srq = NULL;
593 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400594 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400595 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
Chuck Lever3968cb52015-03-30 14:35:26 -0400596 rc = ia->ri_ops->ro_open(ia, ep, cdata);
597 if (rc)
598 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400599 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400600 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400601 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
Chuck Lever16f906d2017-02-08 17:00:10 -0500602 ep->rep_attr.cap.max_send_sge = max_sge;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400603 ep->rep_attr.cap.max_recv_sge = 1;
604 ep->rep_attr.cap.max_inline_data = 0;
605 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
606 ep->rep_attr.qp_type = IB_QPT_RC;
607 ep->rep_attr.port_num = ~0;
608
609 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
610 "iovs: send %d recv %d\n",
611 __func__,
612 ep->rep_attr.cap.max_send_wr,
613 ep->rep_attr.cap.max_recv_wr,
614 ep->rep_attr.cap.max_send_sge,
615 ep->rep_attr.cap.max_recv_sge);
616
617 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400618 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500619 if (ep->rep_cqinit <= 2)
620 ep->rep_cqinit = 0; /* always signal? */
Chuck Lever8d38de62016-11-29 10:52:16 -0500621 rpcrdma_init_cqcount(ep, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400622 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400623 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400624
Chuck Lever2fa8f882016-03-04 11:28:53 -0500625 sendcq = ib_alloc_cq(ia->ri_device, NULL,
626 ep->rep_attr.cap.max_send_wr + 1,
627 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400628 if (IS_ERR(sendcq)) {
629 rc = PTR_ERR(sendcq);
630 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400631 __func__, rc);
632 goto out1;
633 }
634
Chuck Lever552bf222016-03-04 11:28:36 -0500635 recvcq = ib_alloc_cq(ia->ri_device, NULL,
636 ep->rep_attr.cap.max_recv_wr + 1,
637 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400638 if (IS_ERR(recvcq)) {
639 rc = PTR_ERR(recvcq);
640 dprintk("RPC: %s: failed to create recv CQ: %i\n",
641 __func__, rc);
642 goto out2;
643 }
644
Chuck Leverfc664482014-05-28 10:33:25 -0400645 ep->rep_attr.send_cq = sendcq;
646 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400647
648 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400649 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400650
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400651 /* Prepare RDMA-CM private message */
652 pmsg->cp_magic = rpcrdma_cmp_magic;
653 pmsg->cp_version = RPCRDMA_CMP_VERSION;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400654 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400655 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
656 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
657 ep->rep_remote_cma.private_data = pmsg;
658 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400659
660 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400661 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200662 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400663 ep->rep_remote_cma.responder_resources = 32;
664 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500665 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200666 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400667
Chuck Leverb2dde942016-05-02 14:43:03 -0400668 /* Limit transport retries so client can detect server
669 * GID changes quickly. RPC layer handles re-establishing
670 * transport connection and retransmission.
671 */
672 ep->rep_remote_cma.retry_count = 6;
673
674 /* RPC-over-RDMA handles its own flow control. In addition,
675 * make all RNR NAKs visible so we know that RPC-over-RDMA
676 * flow control is working correctly (no NAKs should be seen).
677 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400678 ep->rep_remote_cma.flow_control = 0;
679 ep->rep_remote_cma.rnr_retry_count = 0;
680
681 return 0;
682
683out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500684 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400685out1:
686 return rc;
687}
688
689/*
690 * rpcrdma_ep_destroy
691 *
692 * Disconnect and destroy endpoint. After this, the only
693 * valid operations on the ep are to free it (if dynamically
694 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400695 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400696void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400697rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
698{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400699 dprintk("RPC: %s: entering, connected is %d\n",
700 __func__, ep->rep_connected);
701
Chuck Lever254f91e2014-05-28 10:32:17 -0400702 cancel_delayed_work_sync(&ep->rep_connect_worker);
703
Steve Wise72c02172015-09-21 12:24:23 -0500704 if (ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400705 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400706 rdma_destroy_qp(ia->ri_id);
707 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400708 }
709
Chuck Lever552bf222016-03-04 11:28:36 -0500710 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500711 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400712}
713
Chuck Lever18908962017-04-11 13:23:18 -0400714static int
715rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
716 struct rpcrdma_ia *ia)
717{
718 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
719 struct rdma_cm_id *id, *old;
720 int err, rc;
721
722 dprintk("RPC: %s: reconnecting...\n", __func__);
723
724 rpcrdma_ep_disconnect(ep, ia);
725
726 rc = -EHOSTUNREACH;
727 id = rpcrdma_create_id(r_xprt, ia, sap);
728 if (IS_ERR(id))
729 goto out;
730
731 /* As long as the new ID points to the same device as the
732 * old ID, we can reuse the transport's existing PD and all
733 * previously allocated MRs. Also, the same device means
734 * the transport's previous DMA mappings are still valid.
735 *
736 * This is a sanity check only. There should be no way these
737 * point to two different devices here.
738 */
739 old = id;
740 rc = -ENETUNREACH;
741 if (ia->ri_device != id->device) {
742 pr_err("rpcrdma: can't reconnect on different device!\n");
743 goto out_destroy;
744 }
745
746 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
747 if (err) {
748 dprintk("RPC: %s: rdma_create_qp returned %d\n",
749 __func__, err);
750 goto out_destroy;
751 }
752
753 /* Atomically replace the transport's ID and QP. */
754 rc = 0;
755 old = ia->ri_id;
756 ia->ri_id = id;
757 rdma_destroy_qp(old);
758
759out_destroy:
760 rpcrdma_destroy_id(old);
761out:
762 return rc;
763}
764
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400765/*
766 * Connect unconnected endpoint.
767 */
768int
769rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
770{
Chuck Lever0a904872017-02-08 17:00:35 -0500771 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
772 rx_ia);
Chuck Lever0a904872017-02-08 17:00:35 -0500773 unsigned int extras;
Chuck Lever18908962017-04-11 13:23:18 -0400774 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400775
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400776retry:
Chuck Lever18908962017-04-11 13:23:18 -0400777 switch (ep->rep_connected) {
778 case 0:
Chuck Leverec62f402014-05-28 10:34:07 -0400779 dprintk("RPC: %s: connecting...\n", __func__);
780 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
781 if (rc) {
782 dprintk("RPC: %s: rdma_create_qp failed %i\n",
783 __func__, rc);
Chuck Lever18908962017-04-11 13:23:18 -0400784 rc = -ENETUNREACH;
785 goto out_noupdate;
Chuck Leverec62f402014-05-28 10:34:07 -0400786 }
Chuck Lever18908962017-04-11 13:23:18 -0400787 break;
788 default:
789 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
790 if (rc)
791 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400792 }
793
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400794 ep->rep_connected = 0;
795
796 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
797 if (rc) {
798 dprintk("RPC: %s: rdma_connect() failed with %i\n",
799 __func__, rc);
800 goto out;
801 }
802
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400803 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400804 if (ep->rep_connected <= 0) {
Chuck Lever0a904872017-02-08 17:00:35 -0500805 if (ep->rep_connected == -EAGAIN)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400806 goto retry;
807 rc = ep->rep_connected;
Chuck Lever0a904872017-02-08 17:00:35 -0500808 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400809 }
810
Chuck Lever0a904872017-02-08 17:00:35 -0500811 dprintk("RPC: %s: connected\n", __func__);
812 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
813 if (extras)
814 rpcrdma_ep_post_extra_recv(r_xprt, extras);
815
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400816out:
817 if (rc)
818 ep->rep_connected = rc;
Chuck Lever18908962017-04-11 13:23:18 -0400819
820out_noupdate:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400821 return rc;
822}
823
824/*
825 * rpcrdma_ep_disconnect
826 *
827 * This is separate from destroy to facilitate the ability
828 * to reconnect without recreating the endpoint.
829 *
830 * This call is not reentrant, and must not be made in parallel
831 * on the same endpoint.
832 */
Chuck Lever282191c2014-07-29 17:25:55 -0400833void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400834rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
835{
836 int rc;
837
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400838 rc = rdma_disconnect(ia->ri_id);
839 if (!rc) {
840 /* returns without wait if not connected */
841 wait_event_interruptible(ep->rep_connect_wait,
842 ep->rep_connected != 1);
843 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
844 (ep->rep_connected == 1) ? "still " : "dis");
845 } else {
846 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
847 ep->rep_connected = rc;
848 }
Chuck Lever550d7502016-05-02 14:41:47 -0400849
850 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400851}
852
Chuck Lever505bbe62016-06-29 13:52:54 -0400853static void
854rpcrdma_mr_recovery_worker(struct work_struct *work)
855{
856 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
857 rb_recovery_worker.work);
858 struct rpcrdma_mw *mw;
859
860 spin_lock(&buf->rb_recovery_lock);
861 while (!list_empty(&buf->rb_stale_mrs)) {
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500862 mw = rpcrdma_pop_mw(&buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400863 spin_unlock(&buf->rb_recovery_lock);
864
865 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
866 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
867
868 spin_lock(&buf->rb_recovery_lock);
kbuild test robot53d78522016-07-16 06:02:05 +0800869 }
Chuck Lever505bbe62016-06-29 13:52:54 -0400870 spin_unlock(&buf->rb_recovery_lock);
871}
872
873void
874rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
875{
876 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
877 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
878
879 spin_lock(&buf->rb_recovery_lock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -0500880 rpcrdma_push_mw(mw, &buf->rb_stale_mrs);
Chuck Lever505bbe62016-06-29 13:52:54 -0400881 spin_unlock(&buf->rb_recovery_lock);
882
883 schedule_delayed_work(&buf->rb_recovery_worker, 0);
884}
885
Chuck Levere2ac2362016-06-29 13:54:00 -0400886static void
887rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
888{
889 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
890 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
891 unsigned int count;
892 LIST_HEAD(free);
893 LIST_HEAD(all);
894
895 for (count = 0; count < 32; count++) {
896 struct rpcrdma_mw *mw;
897 int rc;
898
899 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
900 if (!mw)
901 break;
902
903 rc = ia->ri_ops->ro_init_mr(ia, mw);
904 if (rc) {
905 kfree(mw);
906 break;
907 }
908
909 mw->mw_xprt = r_xprt;
910
911 list_add(&mw->mw_list, &free);
912 list_add(&mw->mw_all, &all);
913 }
914
915 spin_lock(&buf->rb_mwlock);
916 list_splice(&free, &buf->rb_mws);
917 list_splice(&all, &buf->rb_all);
918 r_xprt->rx_stats.mrs_allocated += count;
919 spin_unlock(&buf->rb_mwlock);
920
921 dprintk("RPC: %s: created %u MRs\n", __func__, count);
922}
923
924static void
925rpcrdma_mr_refresh_worker(struct work_struct *work)
926{
927 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
928 rb_refresh_worker.work);
929 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
930 rx_buf);
931
932 rpcrdma_create_mrs(r_xprt);
933}
934
Chuck Leverf531a5d2015-10-24 17:27:43 -0400935struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500936rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
937{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400938 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500939 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500940
Chuck Lever85275c82015-01-21 11:04:16 -0500941 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500942 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500943 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500944
Chuck Leverf531a5d2015-10-24 17:27:43 -0400945 INIT_LIST_HEAD(&req->rl_free);
946 spin_lock(&buffer->rb_reqslock);
947 list_add(&req->rl_all, &buffer->rb_allreqs);
948 spin_unlock(&buffer->rb_reqslock);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500949 req->rl_cqe.done = rpcrdma_wc_send;
Chuck Lever13924022015-01-21 11:03:52 -0500950 req->rl_buffer = &r_xprt->rx_buf;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400951 INIT_LIST_HEAD(&req->rl_registered);
Chuck Lever90aab602016-09-15 10:56:43 -0400952 req->rl_send_wr.next = NULL;
953 req->rl_send_wr.wr_cqe = &req->rl_cqe;
Chuck Lever655fec62016-09-15 10:57:24 -0400954 req->rl_send_wr.sg_list = req->rl_send_sge;
Chuck Lever90aab602016-09-15 10:56:43 -0400955 req->rl_send_wr.opcode = IB_WR_SEND;
Chuck Lever13924022015-01-21 11:03:52 -0500956 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500957}
958
Chuck Leverf531a5d2015-10-24 17:27:43 -0400959struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -0500960rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
961{
962 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500963 struct rpcrdma_rep *rep;
964 int rc;
965
966 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500967 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500968 if (rep == NULL)
969 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500970
Chuck Lever13650c22016-09-15 10:56:26 -0400971 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -0400972 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever6b1184c2015-01-21 11:04:25 -0500973 if (IS_ERR(rep->rr_rdmabuf)) {
974 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500975 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500976 }
Chuck Lever13924022015-01-21 11:03:52 -0500977
Chuck Lever1519e962016-09-15 10:57:49 -0400978 rep->rr_cqe.done = rpcrdma_wc_receive;
Chuck Leverfed171b2015-05-26 11:51:37 -0400979 rep->rr_rxprt = r_xprt;
Chuck Lever496b77a2016-09-15 10:57:57 -0400980 INIT_WORK(&rep->rr_work, rpcrdma_reply_handler);
Chuck Lever6ea8e712016-09-15 10:56:51 -0400981 rep->rr_recv_wr.next = NULL;
982 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
983 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
984 rep->rr_recv_wr.num_sge = 1;
Chuck Lever13924022015-01-21 11:03:52 -0500985 return rep;
986
987out_free:
988 kfree(rep);
989out:
990 return ERR_PTR(rc);
991}
992
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400993int
Chuck Leverac920d02015-01-21 11:03:44 -0500994rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400995{
Chuck Leverac920d02015-01-21 11:03:44 -0500996 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400997 int i, rc;
998
Chuck Lever1e465fd2015-10-24 17:27:02 -0400999 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001000 buf->rb_bc_srv_max_requests = 0;
Chuck Lever23826c72016-03-04 11:28:27 -05001001 atomic_set(&buf->rb_credits, 1);
Chuck Levere2ac2362016-06-29 13:54:00 -04001002 spin_lock_init(&buf->rb_mwlock);
Chuck Lever505bbe62016-06-29 13:52:54 -04001003 spin_lock_init(&buf->rb_lock);
1004 spin_lock_init(&buf->rb_recovery_lock);
Chuck Levere2ac2362016-06-29 13:54:00 -04001005 INIT_LIST_HEAD(&buf->rb_mws);
1006 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever505bbe62016-06-29 13:52:54 -04001007 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -04001008 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1009 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001010 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
1011 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001012
Chuck Levere2ac2362016-06-29 13:54:00 -04001013 rpcrdma_create_mrs(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001014
Chuck Lever1e465fd2015-10-24 17:27:02 -04001015 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001016 INIT_LIST_HEAD(&buf->rb_allreqs);
1017 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001018 for (i = 0; i < buf->rb_max_requests; i++) {
1019 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001020
Chuck Lever13924022015-01-21 11:03:52 -05001021 req = rpcrdma_create_req(r_xprt);
1022 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001023 dprintk("RPC: %s: request buffer %d alloc"
1024 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001025 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001026 goto out;
1027 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001028 req->rl_backchannel = false;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001029 list_add(&req->rl_free, &buf->rb_send_bufs);
1030 }
1031
1032 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001033 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001034 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001035
Chuck Lever13924022015-01-21 11:03:52 -05001036 rep = rpcrdma_create_rep(r_xprt);
1037 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001038 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1039 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001040 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001041 goto out;
1042 }
Chuck Lever1e465fd2015-10-24 17:27:02 -04001043 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001044 }
Chuck Lever13924022015-01-21 11:03:52 -05001045
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001046 return 0;
1047out:
1048 rpcrdma_buffer_destroy(buf);
1049 return rc;
1050}
1051
Chuck Lever1e465fd2015-10-24 17:27:02 -04001052static struct rpcrdma_req *
1053rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1054{
1055 struct rpcrdma_req *req;
1056
1057 req = list_first_entry(&buf->rb_send_bufs,
1058 struct rpcrdma_req, rl_free);
1059 list_del(&req->rl_free);
1060 return req;
1061}
1062
1063static struct rpcrdma_rep *
1064rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1065{
1066 struct rpcrdma_rep *rep;
1067
1068 rep = list_first_entry(&buf->rb_recv_bufs,
1069 struct rpcrdma_rep, rr_list);
1070 list_del(&rep->rr_list);
1071 return rep;
1072}
1073
Chuck Lever2e845222014-07-29 17:25:38 -04001074static void
Chuck Lever13650c22016-09-15 10:56:26 -04001075rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -05001076{
Chuck Lever13650c22016-09-15 10:56:26 -04001077 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001078 kfree(rep);
1079}
1080
Chuck Leverf531a5d2015-10-24 17:27:43 -04001081void
Chuck Lever13650c22016-09-15 10:56:26 -04001082rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -05001083{
Chuck Lever13650c22016-09-15 10:56:26 -04001084 rpcrdma_free_regbuf(req->rl_recvbuf);
1085 rpcrdma_free_regbuf(req->rl_sendbuf);
1086 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001087 kfree(req);
1088}
1089
Chuck Levere2ac2362016-06-29 13:54:00 -04001090static void
1091rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1092{
1093 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1094 rx_buf);
1095 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1096 struct rpcrdma_mw *mw;
1097 unsigned int count;
1098
1099 count = 0;
1100 spin_lock(&buf->rb_mwlock);
1101 while (!list_empty(&buf->rb_all)) {
1102 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1103 list_del(&mw->mw_all);
1104
1105 spin_unlock(&buf->rb_mwlock);
1106 ia->ri_ops->ro_release_mr(mw);
1107 count++;
1108 spin_lock(&buf->rb_mwlock);
1109 }
1110 spin_unlock(&buf->rb_mwlock);
1111 r_xprt->rx_stats.mrs_allocated = 0;
1112
1113 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1114}
1115
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001116void
1117rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1118{
Chuck Lever505bbe62016-06-29 13:52:54 -04001119 cancel_delayed_work_sync(&buf->rb_recovery_worker);
Chuck Lever9378b272017-04-11 13:22:29 -04001120 cancel_delayed_work_sync(&buf->rb_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -04001121
Chuck Lever1e465fd2015-10-24 17:27:02 -04001122 while (!list_empty(&buf->rb_recv_bufs)) {
1123 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001124
Chuck Lever1e465fd2015-10-24 17:27:02 -04001125 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001126 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001127 }
Chuck Lever05c97462016-09-06 11:22:58 -04001128 buf->rb_send_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001129
Chuck Leverf531a5d2015-10-24 17:27:43 -04001130 spin_lock(&buf->rb_reqslock);
1131 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001132 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001133
Chuck Leverf531a5d2015-10-24 17:27:43 -04001134 req = list_first_entry(&buf->rb_allreqs,
1135 struct rpcrdma_req, rl_all);
1136 list_del(&req->rl_all);
1137
1138 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001139 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001140 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001141 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001142 spin_unlock(&buf->rb_reqslock);
Chuck Lever05c97462016-09-06 11:22:58 -04001143 buf->rb_recv_count = 0;
Chuck Lever9f9d8022014-07-29 17:24:45 -04001144
Chuck Levere2ac2362016-06-29 13:54:00 -04001145 rpcrdma_destroy_mrs(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001146}
1147
Chuck Lever346aa662015-05-26 11:52:06 -04001148struct rpcrdma_mw *
1149rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001150{
Chuck Lever346aa662015-05-26 11:52:06 -04001151 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1152 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001153
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001154 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001155 if (!list_empty(&buf->rb_mws))
1156 mw = rpcrdma_pop_mw(&buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001157 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001158
1159 if (!mw)
Chuck Levere2ac2362016-06-29 13:54:00 -04001160 goto out_nomws;
Chuck Lever346aa662015-05-26 11:52:06 -04001161 return mw;
Chuck Levere2ac2362016-06-29 13:54:00 -04001162
1163out_nomws:
1164 dprintk("RPC: %s: no MWs available\n", __func__);
Chuck Leverbebd0312017-04-11 13:23:10 -04001165 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1166 schedule_delayed_work(&buf->rb_refresh_worker, 0);
Chuck Levere2ac2362016-06-29 13:54:00 -04001167
1168 /* Allow the reply handler and refresh worker to run */
1169 cond_resched();
1170
1171 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001172}
1173
Chuck Lever346aa662015-05-26 11:52:06 -04001174void
1175rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001176{
Chuck Lever346aa662015-05-26 11:52:06 -04001177 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001178
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001179 spin_lock(&buf->rb_mwlock);
Chuck Lever9a5c63e2017-02-08 17:00:43 -05001180 rpcrdma_push_mw(mw, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001181 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001182}
1183
Chuck Lever05c97462016-09-06 11:22:58 -04001184static struct rpcrdma_rep *
1185rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1186{
1187 /* If an RPC previously completed without a reply (say, a
1188 * credential problem or a soft timeout occurs) then hold off
1189 * on supplying more Receive buffers until the number of new
1190 * pending RPCs catches up to the number of posted Receives.
1191 */
1192 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1193 return NULL;
1194
1195 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1196 return NULL;
1197 buffers->rb_recv_count++;
1198 return rpcrdma_buffer_get_rep_locked(buffers);
1199}
1200
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001201/*
1202 * Get a set of request/reply buffers.
Chuck Lever78d506e2016-09-06 11:22:49 -04001203 *
1204 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001205 */
1206struct rpcrdma_req *
1207rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1208{
1209 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001210
Chuck Levera5b027e2015-10-24 17:27:27 -04001211 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001212 if (list_empty(&buffers->rb_send_bufs))
1213 goto out_reqbuf;
Chuck Lever05c97462016-09-06 11:22:58 -04001214 buffers->rb_send_count++;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001215 req = rpcrdma_buffer_get_req_locked(buffers);
Chuck Lever05c97462016-09-06 11:22:58 -04001216 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001217 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001218 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001219
Chuck Lever1e465fd2015-10-24 17:27:02 -04001220out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001221 spin_unlock(&buffers->rb_lock);
Chuck Lever78d506e2016-09-06 11:22:49 -04001222 pr_warn("RPC: %s: out of request buffers\n", __func__);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001223 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001224}
1225
1226/*
1227 * Put request/reply buffers back into pool.
1228 * Pre-decrement counter/array index.
1229 */
1230void
1231rpcrdma_buffer_put(struct rpcrdma_req *req)
1232{
1233 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001234 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001235
Chuck Lever90aab602016-09-15 10:56:43 -04001236 req->rl_send_wr.num_sge = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001237 req->rl_reply = NULL;
1238
Chuck Levera5b027e2015-10-24 17:27:27 -04001239 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001240 buffers->rb_send_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001241 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001242 if (rep) {
1243 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001244 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001245 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001246 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001247}
1248
1249/*
1250 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001251 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001252 */
1253void
1254rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1255{
1256 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001257
Chuck Levera5b027e2015-10-24 17:27:27 -04001258 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001259 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001260 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001261}
1262
1263/*
1264 * Put reply buffers back into pool when not attached to
1265 * request. This happens in error conditions.
1266 */
1267void
1268rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1269{
Chuck Leverfed171b2015-05-26 11:51:37 -04001270 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001271
Chuck Levera5b027e2015-10-24 17:27:27 -04001272 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001273 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001274 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001275 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001276}
1277
Chuck Lever9128c3e2015-01-21 11:04:00 -05001278/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001279 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001280 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001281 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001282 * @flags: GFP flags
1283 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001284 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1285 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001286 *
1287 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001288 * receiving the payload of RDMA RECV operations. During Long Calls
1289 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001290 */
1291struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001292rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1293 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001294{
1295 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001296
Chuck Lever9128c3e2015-01-21 11:04:00 -05001297 rb = kmalloc(sizeof(*rb) + size, flags);
1298 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001299 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001300
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001301 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001302 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001303 rb->rg_iov.length = size;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001304
1305 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001306}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001307
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001308/**
1309 * __rpcrdma_map_regbuf - DMA-map a regbuf
1310 * @ia: controlling rpcrdma_ia
1311 * @rb: regbuf to be mapped
1312 */
1313bool
1314__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1315{
Chuck Lever91a10c52017-04-11 13:23:02 -04001316 struct ib_device *device = ia->ri_device;
1317
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001318 if (rb->rg_direction == DMA_NONE)
1319 return false;
1320
Chuck Lever91a10c52017-04-11 13:23:02 -04001321 rb->rg_iov.addr = ib_dma_map_single(device,
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001322 (void *)rb->rg_base,
1323 rdmab_length(rb),
1324 rb->rg_direction);
Chuck Lever91a10c52017-04-11 13:23:02 -04001325 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001326 return false;
1327
Chuck Lever91a10c52017-04-11 13:23:02 -04001328 rb->rg_device = device;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001329 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1330 return true;
1331}
1332
1333static void
1334rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1335{
1336 if (!rpcrdma_regbuf_is_mapped(rb))
1337 return;
1338
1339 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1340 rdmab_length(rb), rb->rg_direction);
1341 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001342}
1343
1344/**
1345 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001346 * @rb: regbuf to be deregistered and freed
1347 */
1348void
Chuck Lever13650c22016-09-15 10:56:26 -04001349rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001350{
Chuck Levere531dca2015-08-03 13:03:20 -04001351 if (!rb)
1352 return;
1353
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001354 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001355 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001356}
1357
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001358/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001359 * Prepost any receive buffer, then post send.
1360 *
1361 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1362 */
1363int
1364rpcrdma_ep_post(struct rpcrdma_ia *ia,
1365 struct rpcrdma_ep *ep,
1366 struct rpcrdma_req *req)
1367{
Chuck Lever90aab602016-09-15 10:56:43 -04001368 struct ib_send_wr *send_wr = &req->rl_send_wr;
1369 struct ib_send_wr *send_wr_fail;
Chuck Lever655fec62016-09-15 10:57:24 -04001370 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001371
Chuck Lever90aab602016-09-15 10:56:43 -04001372 if (req->rl_reply) {
1373 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001374 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001375 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001376 req->rl_reply = NULL;
1377 }
1378
Chuck Leverb3221d62015-08-03 13:03:39 -04001379 dprintk("RPC: %s: posting %d s/g entries\n",
Chuck Lever90aab602016-09-15 10:56:43 -04001380 __func__, send_wr->num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001381
Chuck Lever8d38de62016-11-29 10:52:16 -05001382 rpcrdma_set_signaled(ep, send_wr);
Chuck Lever90aab602016-09-15 10:56:43 -04001383 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001384 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001385 goto out_postsend_err;
1386 return 0;
1387
1388out_postsend_err:
1389 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1390 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001391}
1392
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001393int
1394rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001395 struct rpcrdma_rep *rep)
1396{
Chuck Lever6ea8e712016-09-15 10:56:51 -04001397 struct ib_recv_wr *recv_wr_fail;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001398 int rc;
1399
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001400 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1401 goto out_map;
Chuck Lever6ea8e712016-09-15 10:56:51 -04001402 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001403 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001404 goto out_postrecv;
1405 return 0;
1406
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001407out_map:
1408 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1409 return -EIO;
1410
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001411out_postrecv:
1412 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1413 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001414}
Chuck Lever43e95982014-07-29 17:23:34 -04001415
Chuck Leverf531a5d2015-10-24 17:27:43 -04001416/**
1417 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1418 * @r_xprt: transport associated with these backchannel resources
1419 * @min_reqs: minimum number of incoming requests expected
1420 *
1421 * Returns zero if all requested buffers were posted, or a negative errno.
1422 */
1423int
1424rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1425{
1426 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1427 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001428 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001429 int rc;
1430
1431 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001432 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001433 if (list_empty(&buffers->rb_recv_bufs))
1434 goto out_reqbuf;
1435 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001436 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001437
Chuck Leverb1573802016-09-15 10:56:35 -04001438 rc = rpcrdma_ep_post_recv(ia, rep);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001439 if (rc)
1440 goto out_rc;
1441 }
1442
1443 return 0;
1444
1445out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001446 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001447 pr_warn("%s: no extra receive buffers\n", __func__);
1448 return -ENOMEM;
1449
1450out_rc:
1451 rpcrdma_recv_buffer_put(rep);
1452 return rc;
1453}