blob: 451f5f27d8af07887f38b8b6368b7cba7c7b52ee [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() */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040057
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040058#include "xprt_rdma.h"
59
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040060/*
61 * Globals/Macros
62 */
63
Jeff Laytonf895b252014-11-17 16:58:04 -050064#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040065# define RPCDBG_FACILITY RPCDBG_TRANS
66#endif
67
68/*
69 * internal functions
70 */
71
Chuck Leverfe97b472015-10-24 17:27:10 -040072static struct workqueue_struct *rpcrdma_receive_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040073
Chuck Leverfe97b472015-10-24 17:27:10 -040074int
75rpcrdma_alloc_wq(void)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040076{
Chuck Leverfe97b472015-10-24 17:27:10 -040077 struct workqueue_struct *recv_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040078
Chuck Leverfe97b472015-10-24 17:27:10 -040079 recv_wq = alloc_workqueue("xprtrdma_receive",
80 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI,
81 0);
82 if (!recv_wq)
83 return -ENOMEM;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040084
Chuck Leverfe97b472015-10-24 17:27:10 -040085 rpcrdma_receive_wq = recv_wq;
86 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040087}
88
Chuck Leverfe97b472015-10-24 17:27:10 -040089void
90rpcrdma_destroy_wq(void)
Chuck Leverf1a03b72014-11-08 20:14:37 -050091{
Chuck Leverfe97b472015-10-24 17:27:10 -040092 struct workqueue_struct *wq;
Chuck Leverf1a03b72014-11-08 20:14:37 -050093
Chuck Leverfe97b472015-10-24 17:27:10 -040094 if (rpcrdma_receive_wq) {
95 wq = rpcrdma_receive_wq;
96 rpcrdma_receive_wq = NULL;
97 destroy_workqueue(wq);
98 }
Chuck Leverf1a03b72014-11-08 20:14:37 -050099}
100
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400101static void
102rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
103{
104 struct rpcrdma_ep *ep = context;
105
Chuck Lever7ff11de2014-11-08 20:15:01 -0500106 pr_err("RPC: %s: %s on device %s ep %p\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300107 __func__, ib_event_msg(event->event),
Chuck Lever7ff11de2014-11-08 20:15:01 -0500108 event->device->name, context);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400109 if (ep->rep_connected == 1) {
110 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500111 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400112 wake_up_all(&ep->rep_connect_wait);
113 }
114}
115
Chuck Lever2fa8f882016-03-04 11:28:53 -0500116/**
117 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
118 * @cq: completion queue (ignored)
119 * @wc: completed WR
120 *
Chuck Lever4220a072015-10-24 17:26:45 -0400121 */
122static void
Chuck Lever2fa8f882016-03-04 11:28:53 -0500123rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400124{
Chuck Lever2fa8f882016-03-04 11:28:53 -0500125 /* WARNING: Only wr_cqe and status are reliable at this point */
126 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
127 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
128 ib_wc_status_msg(wc->status),
129 wc->status, wc->vendor_err);
Chuck Leverfc664482014-05-28 10:33:25 -0400130}
131
Chuck Lever23826c72016-03-04 11:28:27 -0500132/* Perform basic sanity checking to avoid using garbage
133 * to update the credit grant value.
134 */
135static void
136rpcrdma_update_granted_credits(struct rpcrdma_rep *rep)
137{
138 struct rpcrdma_msg *rmsgp = rdmab_to_msg(rep->rr_rdmabuf);
139 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
140 u32 credits;
141
142 if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
143 return;
144
145 credits = be32_to_cpu(rmsgp->rm_credit);
146 if (credits == 0)
147 credits = 1; /* don't deadlock */
148 else if (credits > buffer->rb_max_requests)
149 credits = buffer->rb_max_requests;
150
151 atomic_set(&buffer->rb_credits, credits);
152}
153
Chuck Lever552bf222016-03-04 11:28:36 -0500154/**
Chuck Lever1519e962016-09-15 10:57:49 -0400155 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
Chuck Lever552bf222016-03-04 11:28:36 -0500156 * @cq: completion queue (ignored)
157 * @wc: completed WR
158 *
159 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400160static void
Chuck Lever1519e962016-09-15 10:57:49 -0400161rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400162{
Chuck Lever552bf222016-03-04 11:28:36 -0500163 struct ib_cqe *cqe = wc->wr_cqe;
164 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
165 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400166
Chuck Lever85024272015-01-21 11:02:04 -0500167 /* WARNING: Only wr_id and status are reliable at this point */
168 if (wc->status != IB_WC_SUCCESS)
169 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400170
Chuck Lever85024272015-01-21 11:02:04 -0500171 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400172 if (wc->opcode != IB_WC_RECV)
173 return;
174
Chuck Lever85024272015-01-21 11:02:04 -0500175 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
176 __func__, rep, wc->byte_len);
177
Chuck Leverfc664482014-05-28 10:33:25 -0400178 rep->rr_len = wc->byte_len;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400179 rep->rr_wc_flags = wc->wc_flags;
180 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
181
Chuck Lever89e0d1122015-05-26 11:51:56 -0400182 ib_dma_sync_single_for_cpu(rep->rr_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -0500183 rdmab_addr(rep->rr_rdmabuf),
184 rep->rr_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500185
186 rpcrdma_update_granted_credits(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400187
188out_schedule:
Chuck Leverfe97b472015-10-24 17:27:10 -0400189 queue_work(rpcrdma_receive_wq, &rep->rr_work);
Chuck Lever85024272015-01-21 11:02:04 -0500190 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400191
Chuck Lever85024272015-01-21 11:02:04 -0500192out_fail:
193 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500194 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
195 ib_wc_status_msg(wc->status),
196 wc->status, wc->vendor_err);
Chuck Leverb0e178a2015-10-24 17:26:54 -0400197 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500198 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400199}
200
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400201static void
202rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
203 struct rdma_conn_param *param)
204{
205 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
206 const struct rpcrdma_connect_private *pmsg = param->private_data;
207 unsigned int rsize, wsize;
208
Chuck Leverc8b920b2016-09-15 10:57:16 -0400209 /* Default settings for RPC-over-RDMA Version One */
210 r_xprt->rx_ia.ri_reminv_expected = false;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400211 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
212 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
213
214 if (pmsg &&
215 pmsg->cp_magic == rpcrdma_cmp_magic &&
216 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
Chuck Leverc8b920b2016-09-15 10:57:16 -0400217 r_xprt->rx_ia.ri_reminv_expected = true;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400218 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
219 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
220 }
221
222 if (rsize < cdata->inline_rsize)
223 cdata->inline_rsize = rsize;
224 if (wsize < cdata->inline_wsize)
225 cdata->inline_wsize = wsize;
226 pr_info("rpcrdma: max send %u, max recv %u\n",
227 cdata->inline_wsize, cdata->inline_rsize);
228 rpcrdma_set_max_header_sizes(r_xprt);
229}
230
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400231static int
232rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
233{
234 struct rpcrdma_xprt *xprt = id->context;
235 struct rpcrdma_ia *ia = &xprt->rx_ia;
236 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500237#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400238 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800239#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500240 struct ib_qp_attr *attr = &ia->ri_qp_attr;
241 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400242 int connstate = 0;
243
244 switch (event->event) {
245 case RDMA_CM_EVENT_ADDR_RESOLVED:
246 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400247 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400248 complete(&ia->ri_done);
249 break;
250 case RDMA_CM_EVENT_ADDR_ERROR:
251 ia->ri_async_rc = -EHOSTUNREACH;
252 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
253 __func__, ep);
254 complete(&ia->ri_done);
255 break;
256 case RDMA_CM_EVENT_ROUTE_ERROR:
257 ia->ri_async_rc = -ENETUNREACH;
258 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
259 __func__, ep);
260 complete(&ia->ri_done);
261 break;
262 case RDMA_CM_EVENT_ESTABLISHED:
263 connstate = 1;
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500264 ib_query_qp(ia->ri_id->qp, attr,
265 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
266 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400267 dprintk("RPC: %s: %d responder resources"
268 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500269 __func__, attr->max_dest_rd_atomic,
270 attr->max_rd_atomic);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400271 rpcrdma_update_connect_private(xprt, &event->param.conn);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400272 goto connected;
273 case RDMA_CM_EVENT_CONNECT_ERROR:
274 connstate = -ENOTCONN;
275 goto connected;
276 case RDMA_CM_EVENT_UNREACHABLE:
277 connstate = -ENETDOWN;
278 goto connected;
279 case RDMA_CM_EVENT_REJECTED:
280 connstate = -ECONNREFUSED;
281 goto connected;
282 case RDMA_CM_EVENT_DISCONNECTED:
283 connstate = -ECONNABORTED;
284 goto connected;
285 case RDMA_CM_EVENT_DEVICE_REMOVAL:
286 connstate = -ENODEV;
287connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400288 dprintk("RPC: %s: %sconnected\n",
289 __func__, connstate > 0 ? "" : "dis");
Chuck Lever23826c72016-03-04 11:28:27 -0500290 atomic_set(&xprt->rx_buf.rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400291 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500292 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400293 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400294 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400295 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400296 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
297 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300298 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400299 break;
300 }
301
Jeff Laytonf895b252014-11-17 16:58:04 -0500302#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400303 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500304 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400305 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400306
Chuck Levera0ce85f2015-03-30 14:34:21 -0400307 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400308 sap, rpc_get_port(sap),
Chuck Lever89e0d1122015-05-26 11:51:56 -0400309 ia->ri_device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400310 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400311 xprt->rx_buf.rb_max_requests,
312 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
313 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400314 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
315 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400316 }
317#endif
318
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400319 return 0;
320}
321
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400322static void rpcrdma_destroy_id(struct rdma_cm_id *id)
323{
324 if (id) {
325 module_put(id->device->owner);
326 rdma_destroy_id(id);
327 }
328}
329
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400330static struct rdma_cm_id *
331rpcrdma_create_id(struct rpcrdma_xprt *xprt,
332 struct rpcrdma_ia *ia, struct sockaddr *addr)
333{
334 struct rdma_cm_id *id;
335 int rc;
336
Tom Talpey1a954052008-10-09 15:01:31 -0400337 init_completion(&ia->ri_done);
338
Guy Shapirofa201052015-10-22 15:20:10 +0300339 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
340 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400341 if (IS_ERR(id)) {
342 rc = PTR_ERR(id);
343 dprintk("RPC: %s: rdma_create_id() failed %i\n",
344 __func__, rc);
345 return id;
346 }
347
Tom Talpey5675add2008-10-09 15:01:41 -0400348 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400349 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
350 if (rc) {
351 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
352 __func__, rc);
353 goto out;
354 }
Tom Talpey5675add2008-10-09 15:01:41 -0400355 wait_for_completion_interruptible_timeout(&ia->ri_done,
356 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400357
358 /* FIXME:
359 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
360 * be pinned while there are active NFS/RDMA mounts to prevent
361 * hangs and crashes at umount time.
362 */
363 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
364 dprintk("RPC: %s: Failed to get device module\n",
365 __func__);
366 ia->ri_async_rc = -ENODEV;
367 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400368 rc = ia->ri_async_rc;
369 if (rc)
370 goto out;
371
Tom Talpey5675add2008-10-09 15:01:41 -0400372 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400373 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
374 if (rc) {
375 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
376 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400377 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400378 }
Tom Talpey5675add2008-10-09 15:01:41 -0400379 wait_for_completion_interruptible_timeout(&ia->ri_done,
380 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400381 rc = ia->ri_async_rc;
382 if (rc)
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400383 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400384
385 return id;
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400386put:
387 module_put(id->device->owner);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400388out:
389 rdma_destroy_id(id);
390 return ERR_PTR(rc);
391}
392
393/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400394 * Exported functions.
395 */
396
397/*
398 * Open and initialize an Interface Adapter.
399 * o initializes fields of struct rpcrdma_ia, including
400 * interface and provider attributes and protection zone.
401 */
402int
403rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
404{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400405 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400406 int rc;
407
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400408 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
409 if (IS_ERR(ia->ri_id)) {
410 rc = PTR_ERR(ia->ri_id);
411 goto out1;
412 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400413 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400414
Christoph Hellwiged082d32016-09-05 12:56:17 +0200415 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400416 if (IS_ERR(ia->ri_pd)) {
417 rc = PTR_ERR(ia->ri_pd);
Chuck Leverb54054c2016-06-29 13:53:27 -0400418 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400419 goto out2;
420 }
421
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400422 switch (memreg) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400423 case RPCRDMA_FRMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400424 if (frwr_is_supported(ia)) {
425 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
426 break;
427 }
428 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400429 case RPCRDMA_MTHCAFMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400430 if (fmr_is_supported(ia)) {
431 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
432 break;
433 }
434 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400435 default:
Chuck Leverb54054c2016-06-29 13:53:27 -0400436 pr_err("rpcrdma: Unsupported memory registration mode: %d\n",
437 memreg);
438 rc = -EINVAL;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500439 goto out3;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400440 }
441
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400442 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500443
444out3:
445 ib_dealloc_pd(ia->ri_pd);
446 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400447out2:
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400448 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400449 ia->ri_id = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400450out1:
451 return rc;
452}
453
454/*
455 * Clean up/close an IA.
456 * o if event handles and PD have been initialized, free them.
457 * o close the IA
458 */
459void
460rpcrdma_ia_close(struct rpcrdma_ia *ia)
461{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400462 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400463 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
464 if (ia->ri_id->qp)
465 rdma_destroy_qp(ia->ri_id);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400466 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400467 ia->ri_id = NULL;
468 }
Chuck Lever6d446982015-05-26 11:51:27 -0400469
470 /* If the pd is still busy, xprtrdma missed freeing a resource */
471 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600472 ib_dealloc_pd(ia->ri_pd);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400473}
474
475/*
476 * Create unconnected endpoint.
477 */
478int
479rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
480 struct rpcrdma_create_data_internal *cdata)
481{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400482 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
Chuck Leverfc664482014-05-28 10:33:25 -0400483 struct ib_cq *sendcq, *recvcq;
Chuck Lever124fa172015-10-24 17:27:51 -0400484 unsigned int max_qp_wr;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500485 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400486
Chuck Lever655fec62016-09-15 10:57:24 -0400487 if (ia->ri_device->attrs.max_sge < RPCRDMA_MAX_SEND_SGES) {
Chuck Leverb3221d62015-08-03 13:03:39 -0400488 dprintk("RPC: %s: insufficient sge's available\n",
489 __func__);
490 return -ENOMEM;
491 }
492
Or Gerlitze3e45b12015-12-18 10:59:48 +0200493 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400494 dprintk("RPC: %s: insufficient wqe's available\n",
495 __func__);
496 return -ENOMEM;
497 }
Chuck Lever550d7502016-05-02 14:41:47 -0400498 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
Chuck Lever124fa172015-10-24 17:27:51 -0400499
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400500 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400501 if (cdata->max_requests > max_qp_wr)
502 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400503
504 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
505 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400506 ep->rep_attr.srq = NULL;
507 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400508 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400509 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
Chuck Lever3968cb52015-03-30 14:35:26 -0400510 rc = ia->ri_ops->ro_open(ia, ep, cdata);
511 if (rc)
512 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400513 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400514 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400515 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
Chuck Lever655fec62016-09-15 10:57:24 -0400516 ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_SEND_SGES;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400517 ep->rep_attr.cap.max_recv_sge = 1;
518 ep->rep_attr.cap.max_inline_data = 0;
519 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
520 ep->rep_attr.qp_type = IB_QPT_RC;
521 ep->rep_attr.port_num = ~0;
522
523 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
524 "iovs: send %d recv %d\n",
525 __func__,
526 ep->rep_attr.cap.max_send_wr,
527 ep->rep_attr.cap.max_recv_wr,
528 ep->rep_attr.cap.max_send_sge,
529 ep->rep_attr.cap.max_recv_sge);
530
531 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400532 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500533 if (ep->rep_cqinit <= 2)
534 ep->rep_cqinit = 0; /* always signal? */
Chuck Lever8d38de62016-11-29 10:52:16 -0500535 rpcrdma_init_cqcount(ep, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400536 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400537 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400538
Chuck Lever2fa8f882016-03-04 11:28:53 -0500539 sendcq = ib_alloc_cq(ia->ri_device, NULL,
540 ep->rep_attr.cap.max_send_wr + 1,
541 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400542 if (IS_ERR(sendcq)) {
543 rc = PTR_ERR(sendcq);
544 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400545 __func__, rc);
546 goto out1;
547 }
548
Chuck Lever552bf222016-03-04 11:28:36 -0500549 recvcq = ib_alloc_cq(ia->ri_device, NULL,
550 ep->rep_attr.cap.max_recv_wr + 1,
551 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400552 if (IS_ERR(recvcq)) {
553 rc = PTR_ERR(recvcq);
554 dprintk("RPC: %s: failed to create recv CQ: %i\n",
555 __func__, rc);
556 goto out2;
557 }
558
Chuck Leverfc664482014-05-28 10:33:25 -0400559 ep->rep_attr.send_cq = sendcq;
560 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400561
562 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400563 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400564
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400565 /* Prepare RDMA-CM private message */
566 pmsg->cp_magic = rpcrdma_cmp_magic;
567 pmsg->cp_version = RPCRDMA_CMP_VERSION;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400568 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400569 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
570 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
571 ep->rep_remote_cma.private_data = pmsg;
572 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400573
574 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400575 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200576 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400577 ep->rep_remote_cma.responder_resources = 32;
578 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500579 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200580 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400581
Chuck Leverb2dde942016-05-02 14:43:03 -0400582 /* Limit transport retries so client can detect server
583 * GID changes quickly. RPC layer handles re-establishing
584 * transport connection and retransmission.
585 */
586 ep->rep_remote_cma.retry_count = 6;
587
588 /* RPC-over-RDMA handles its own flow control. In addition,
589 * make all RNR NAKs visible so we know that RPC-over-RDMA
590 * flow control is working correctly (no NAKs should be seen).
591 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400592 ep->rep_remote_cma.flow_control = 0;
593 ep->rep_remote_cma.rnr_retry_count = 0;
594
595 return 0;
596
597out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500598 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400599out1:
600 return rc;
601}
602
603/*
604 * rpcrdma_ep_destroy
605 *
606 * Disconnect and destroy endpoint. After this, the only
607 * valid operations on the ep are to free it (if dynamically
608 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400609 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400610void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400611rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
612{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400613 dprintk("RPC: %s: entering, connected is %d\n",
614 __func__, ep->rep_connected);
615
Chuck Lever254f91e2014-05-28 10:32:17 -0400616 cancel_delayed_work_sync(&ep->rep_connect_worker);
617
Steve Wise72c02172015-09-21 12:24:23 -0500618 if (ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400619 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400620 rdma_destroy_qp(ia->ri_id);
621 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400622 }
623
Chuck Lever552bf222016-03-04 11:28:36 -0500624 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500625 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400626}
627
628/*
629 * Connect unconnected endpoint.
630 */
631int
632rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
633{
Chuck Lever73806c82014-07-29 17:23:25 -0400634 struct rdma_cm_id *id, *old;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400635 int rc = 0;
636 int retry_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400637
Tom Talpeyc0555512008-10-10 11:32:45 -0400638 if (ep->rep_connected != 0) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400639 struct rpcrdma_xprt *xprt;
640retry:
Chuck Leverec62f402014-05-28 10:34:07 -0400641 dprintk("RPC: %s: reconnecting...\n", __func__);
Chuck Lever282191c2014-07-29 17:25:55 -0400642
643 rpcrdma_ep_disconnect(ep, ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400644
645 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
646 id = rpcrdma_create_id(xprt, ia,
647 (struct sockaddr *)&xprt->rx_data.addr);
648 if (IS_ERR(id)) {
Chuck Leverec62f402014-05-28 10:34:07 -0400649 rc = -EHOSTUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400650 goto out;
651 }
652 /* TEMP TEMP TEMP - fail if new device:
653 * Deregister/remarshal *all* requests!
654 * Close and recreate adapter, pd, etc!
655 * Re-determine all attributes still sane!
656 * More stuff I haven't thought of!
657 * Rrrgh!
658 */
Chuck Lever89e0d1122015-05-26 11:51:56 -0400659 if (ia->ri_device != id->device) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400660 printk("RPC: %s: can't reconnect on "
661 "different device!\n", __func__);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400662 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400663 rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400664 goto out;
665 }
666 /* END TEMP */
Chuck Leverec62f402014-05-28 10:34:07 -0400667 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
668 if (rc) {
669 dprintk("RPC: %s: rdma_create_qp failed %i\n",
670 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400671 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400672 rc = -ENETUNREACH;
673 goto out;
674 }
Chuck Lever73806c82014-07-29 17:23:25 -0400675
Chuck Lever73806c82014-07-29 17:23:25 -0400676 old = ia->ri_id;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400677 ia->ri_id = id;
Chuck Lever73806c82014-07-29 17:23:25 -0400678
679 rdma_destroy_qp(old);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400680 rpcrdma_destroy_id(old);
Chuck Leverec62f402014-05-28 10:34:07 -0400681 } else {
682 dprintk("RPC: %s: connecting...\n", __func__);
683 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
684 if (rc) {
685 dprintk("RPC: %s: rdma_create_qp failed %i\n",
686 __func__, rc);
687 /* do not update ep->rep_connected */
688 return -ENETUNREACH;
689 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400690 }
691
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400692 ep->rep_connected = 0;
693
694 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
695 if (rc) {
696 dprintk("RPC: %s: rdma_connect() failed with %i\n",
697 __func__, rc);
698 goto out;
699 }
700
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400701 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
702
703 /*
704 * Check state. A non-peer reject indicates no listener
705 * (ECONNREFUSED), which may be a transient state. All
706 * others indicate a transport condition which has already
707 * undergone a best-effort.
708 */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800709 if (ep->rep_connected == -ECONNREFUSED &&
710 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400711 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
712 goto retry;
713 }
714 if (ep->rep_connected <= 0) {
715 /* Sometimes, the only way to reliably connect to remote
716 * CMs is to use same nonzero values for ORD and IRD. */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400717 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
718 (ep->rep_remote_cma.responder_resources == 0 ||
719 ep->rep_remote_cma.initiator_depth !=
720 ep->rep_remote_cma.responder_resources)) {
721 if (ep->rep_remote_cma.responder_resources == 0)
722 ep->rep_remote_cma.responder_resources = 1;
723 ep->rep_remote_cma.initiator_depth =
724 ep->rep_remote_cma.responder_resources;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400725 goto retry;
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400726 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400727 rc = ep->rep_connected;
728 } else {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400729 struct rpcrdma_xprt *r_xprt;
730 unsigned int extras;
731
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400732 dprintk("RPC: %s: connected\n", __func__);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400733
734 r_xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
735 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
736
737 if (extras) {
738 rc = rpcrdma_ep_post_extra_recv(r_xprt, extras);
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300739 if (rc) {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400740 pr_warn("%s: rpcrdma_ep_post_extra_recv: %i\n",
741 __func__, rc);
742 rc = 0;
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300743 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400744 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400745 }
746
747out:
748 if (rc)
749 ep->rep_connected = rc;
750 return rc;
751}
752
753/*
754 * rpcrdma_ep_disconnect
755 *
756 * This is separate from destroy to facilitate the ability
757 * to reconnect without recreating the endpoint.
758 *
759 * This call is not reentrant, and must not be made in parallel
760 * on the same endpoint.
761 */
Chuck Lever282191c2014-07-29 17:25:55 -0400762void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400763rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
764{
765 int rc;
766
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400767 rc = rdma_disconnect(ia->ri_id);
768 if (!rc) {
769 /* returns without wait if not connected */
770 wait_event_interruptible(ep->rep_connect_wait,
771 ep->rep_connected != 1);
772 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
773 (ep->rep_connected == 1) ? "still " : "dis");
774 } else {
775 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
776 ep->rep_connected = rc;
777 }
Chuck Lever550d7502016-05-02 14:41:47 -0400778
779 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400780}
781
Chuck Lever505bbe62016-06-29 13:52:54 -0400782static void
783rpcrdma_mr_recovery_worker(struct work_struct *work)
784{
785 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
786 rb_recovery_worker.work);
787 struct rpcrdma_mw *mw;
788
789 spin_lock(&buf->rb_recovery_lock);
790 while (!list_empty(&buf->rb_stale_mrs)) {
791 mw = list_first_entry(&buf->rb_stale_mrs,
792 struct rpcrdma_mw, mw_list);
793 list_del_init(&mw->mw_list);
794 spin_unlock(&buf->rb_recovery_lock);
795
796 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
797 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
798
799 spin_lock(&buf->rb_recovery_lock);
kbuild test robot53d78522016-07-16 06:02:05 +0800800 }
Chuck Lever505bbe62016-06-29 13:52:54 -0400801 spin_unlock(&buf->rb_recovery_lock);
802}
803
804void
805rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
806{
807 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
808 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
809
810 spin_lock(&buf->rb_recovery_lock);
811 list_add(&mw->mw_list, &buf->rb_stale_mrs);
812 spin_unlock(&buf->rb_recovery_lock);
813
814 schedule_delayed_work(&buf->rb_recovery_worker, 0);
815}
816
Chuck Levere2ac2362016-06-29 13:54:00 -0400817static void
818rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
819{
820 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
821 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
822 unsigned int count;
823 LIST_HEAD(free);
824 LIST_HEAD(all);
825
826 for (count = 0; count < 32; count++) {
827 struct rpcrdma_mw *mw;
828 int rc;
829
830 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
831 if (!mw)
832 break;
833
834 rc = ia->ri_ops->ro_init_mr(ia, mw);
835 if (rc) {
836 kfree(mw);
837 break;
838 }
839
840 mw->mw_xprt = r_xprt;
841
842 list_add(&mw->mw_list, &free);
843 list_add(&mw->mw_all, &all);
844 }
845
846 spin_lock(&buf->rb_mwlock);
847 list_splice(&free, &buf->rb_mws);
848 list_splice(&all, &buf->rb_all);
849 r_xprt->rx_stats.mrs_allocated += count;
850 spin_unlock(&buf->rb_mwlock);
851
852 dprintk("RPC: %s: created %u MRs\n", __func__, count);
853}
854
855static void
856rpcrdma_mr_refresh_worker(struct work_struct *work)
857{
858 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
859 rb_refresh_worker.work);
860 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
861 rx_buf);
862
863 rpcrdma_create_mrs(r_xprt);
864}
865
Chuck Leverf531a5d2015-10-24 17:27:43 -0400866struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500867rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
868{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400869 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500870 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500871
Chuck Lever85275c82015-01-21 11:04:16 -0500872 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500873 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500874 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500875
Chuck Leverf531a5d2015-10-24 17:27:43 -0400876 INIT_LIST_HEAD(&req->rl_free);
877 spin_lock(&buffer->rb_reqslock);
878 list_add(&req->rl_all, &buffer->rb_allreqs);
879 spin_unlock(&buffer->rb_reqslock);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500880 req->rl_cqe.done = rpcrdma_wc_send;
Chuck Lever13924022015-01-21 11:03:52 -0500881 req->rl_buffer = &r_xprt->rx_buf;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400882 INIT_LIST_HEAD(&req->rl_registered);
Chuck Lever90aab602016-09-15 10:56:43 -0400883 req->rl_send_wr.next = NULL;
884 req->rl_send_wr.wr_cqe = &req->rl_cqe;
Chuck Lever655fec62016-09-15 10:57:24 -0400885 req->rl_send_wr.sg_list = req->rl_send_sge;
Chuck Lever90aab602016-09-15 10:56:43 -0400886 req->rl_send_wr.opcode = IB_WR_SEND;
Chuck Lever13924022015-01-21 11:03:52 -0500887 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500888}
889
Chuck Leverf531a5d2015-10-24 17:27:43 -0400890struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -0500891rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
892{
893 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500894 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
895 struct rpcrdma_rep *rep;
896 int rc;
897
898 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500899 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500900 if (rep == NULL)
901 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500902
Chuck Lever13650c22016-09-15 10:56:26 -0400903 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -0400904 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever6b1184c2015-01-21 11:04:25 -0500905 if (IS_ERR(rep->rr_rdmabuf)) {
906 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500907 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500908 }
Chuck Lever13924022015-01-21 11:03:52 -0500909
Chuck Lever89e0d1122015-05-26 11:51:56 -0400910 rep->rr_device = ia->ri_device;
Chuck Lever1519e962016-09-15 10:57:49 -0400911 rep->rr_cqe.done = rpcrdma_wc_receive;
Chuck Leverfed171b2015-05-26 11:51:37 -0400912 rep->rr_rxprt = r_xprt;
Chuck Lever496b77a2016-09-15 10:57:57 -0400913 INIT_WORK(&rep->rr_work, rpcrdma_reply_handler);
Chuck Lever6ea8e712016-09-15 10:56:51 -0400914 rep->rr_recv_wr.next = NULL;
915 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
916 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
917 rep->rr_recv_wr.num_sge = 1;
Chuck Lever13924022015-01-21 11:03:52 -0500918 return rep;
919
920out_free:
921 kfree(rep);
922out:
923 return ERR_PTR(rc);
924}
925
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400926int
Chuck Leverac920d02015-01-21 11:03:44 -0500927rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400928{
Chuck Leverac920d02015-01-21 11:03:44 -0500929 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400930 int i, rc;
931
Chuck Lever1e465fd2015-10-24 17:27:02 -0400932 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -0400933 buf->rb_bc_srv_max_requests = 0;
Chuck Lever23826c72016-03-04 11:28:27 -0500934 atomic_set(&buf->rb_credits, 1);
Chuck Levere2ac2362016-06-29 13:54:00 -0400935 spin_lock_init(&buf->rb_mwlock);
Chuck Lever505bbe62016-06-29 13:52:54 -0400936 spin_lock_init(&buf->rb_lock);
937 spin_lock_init(&buf->rb_recovery_lock);
Chuck Levere2ac2362016-06-29 13:54:00 -0400938 INIT_LIST_HEAD(&buf->rb_mws);
939 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever505bbe62016-06-29 13:52:54 -0400940 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -0400941 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
942 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -0400943 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
944 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400945
Chuck Levere2ac2362016-06-29 13:54:00 -0400946 rpcrdma_create_mrs(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400947
Chuck Lever1e465fd2015-10-24 17:27:02 -0400948 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400949 INIT_LIST_HEAD(&buf->rb_allreqs);
950 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400951 for (i = 0; i < buf->rb_max_requests; i++) {
952 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400953
Chuck Lever13924022015-01-21 11:03:52 -0500954 req = rpcrdma_create_req(r_xprt);
955 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400956 dprintk("RPC: %s: request buffer %d alloc"
957 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500958 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400959 goto out;
960 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400961 req->rl_backchannel = false;
Chuck Lever1e465fd2015-10-24 17:27:02 -0400962 list_add(&req->rl_free, &buf->rb_send_bufs);
963 }
964
965 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -0400966 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
Chuck Lever1e465fd2015-10-24 17:27:02 -0400967 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400968
Chuck Lever13924022015-01-21 11:03:52 -0500969 rep = rpcrdma_create_rep(r_xprt);
970 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400971 dprintk("RPC: %s: reply buffer %d alloc failed\n",
972 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500973 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400974 goto out;
975 }
Chuck Lever1e465fd2015-10-24 17:27:02 -0400976 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400977 }
Chuck Lever13924022015-01-21 11:03:52 -0500978
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400979 return 0;
980out:
981 rpcrdma_buffer_destroy(buf);
982 return rc;
983}
984
Chuck Lever1e465fd2015-10-24 17:27:02 -0400985static struct rpcrdma_req *
986rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
987{
988 struct rpcrdma_req *req;
989
990 req = list_first_entry(&buf->rb_send_bufs,
991 struct rpcrdma_req, rl_free);
992 list_del(&req->rl_free);
993 return req;
994}
995
996static struct rpcrdma_rep *
997rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
998{
999 struct rpcrdma_rep *rep;
1000
1001 rep = list_first_entry(&buf->rb_recv_bufs,
1002 struct rpcrdma_rep, rr_list);
1003 list_del(&rep->rr_list);
1004 return rep;
1005}
1006
Chuck Lever2e845222014-07-29 17:25:38 -04001007static void
Chuck Lever13650c22016-09-15 10:56:26 -04001008rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -05001009{
Chuck Lever13650c22016-09-15 10:56:26 -04001010 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001011 kfree(rep);
1012}
1013
Chuck Leverf531a5d2015-10-24 17:27:43 -04001014void
Chuck Lever13650c22016-09-15 10:56:26 -04001015rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -05001016{
Chuck Lever13650c22016-09-15 10:56:26 -04001017 rpcrdma_free_regbuf(req->rl_recvbuf);
1018 rpcrdma_free_regbuf(req->rl_sendbuf);
1019 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001020 kfree(req);
1021}
1022
Chuck Levere2ac2362016-06-29 13:54:00 -04001023static void
1024rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1025{
1026 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1027 rx_buf);
1028 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1029 struct rpcrdma_mw *mw;
1030 unsigned int count;
1031
1032 count = 0;
1033 spin_lock(&buf->rb_mwlock);
1034 while (!list_empty(&buf->rb_all)) {
1035 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1036 list_del(&mw->mw_all);
1037
1038 spin_unlock(&buf->rb_mwlock);
1039 ia->ri_ops->ro_release_mr(mw);
1040 count++;
1041 spin_lock(&buf->rb_mwlock);
1042 }
1043 spin_unlock(&buf->rb_mwlock);
1044 r_xprt->rx_stats.mrs_allocated = 0;
1045
1046 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1047}
1048
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001049void
1050rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1051{
Chuck Lever505bbe62016-06-29 13:52:54 -04001052 cancel_delayed_work_sync(&buf->rb_recovery_worker);
1053
Chuck Lever1e465fd2015-10-24 17:27:02 -04001054 while (!list_empty(&buf->rb_recv_bufs)) {
1055 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001056
Chuck Lever1e465fd2015-10-24 17:27:02 -04001057 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001058 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001059 }
Chuck Lever05c97462016-09-06 11:22:58 -04001060 buf->rb_send_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001061
Chuck Leverf531a5d2015-10-24 17:27:43 -04001062 spin_lock(&buf->rb_reqslock);
1063 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001064 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001065
Chuck Leverf531a5d2015-10-24 17:27:43 -04001066 req = list_first_entry(&buf->rb_allreqs,
1067 struct rpcrdma_req, rl_all);
1068 list_del(&req->rl_all);
1069
1070 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001071 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001072 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001073 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001074 spin_unlock(&buf->rb_reqslock);
Chuck Lever05c97462016-09-06 11:22:58 -04001075 buf->rb_recv_count = 0;
Chuck Lever9f9d8022014-07-29 17:24:45 -04001076
Chuck Levere2ac2362016-06-29 13:54:00 -04001077 rpcrdma_destroy_mrs(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001078}
1079
Chuck Lever346aa662015-05-26 11:52:06 -04001080struct rpcrdma_mw *
1081rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001082{
Chuck Lever346aa662015-05-26 11:52:06 -04001083 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1084 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001085
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001086 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001087 if (!list_empty(&buf->rb_mws)) {
1088 mw = list_first_entry(&buf->rb_mws,
1089 struct rpcrdma_mw, mw_list);
1090 list_del_init(&mw->mw_list);
Chuck Leverc2922c02014-07-29 17:24:36 -04001091 }
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001092 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001093
1094 if (!mw)
Chuck Levere2ac2362016-06-29 13:54:00 -04001095 goto out_nomws;
Chuck Lever346aa662015-05-26 11:52:06 -04001096 return mw;
Chuck Levere2ac2362016-06-29 13:54:00 -04001097
1098out_nomws:
1099 dprintk("RPC: %s: no MWs available\n", __func__);
1100 schedule_delayed_work(&buf->rb_refresh_worker, 0);
1101
1102 /* Allow the reply handler and refresh worker to run */
1103 cond_resched();
1104
1105 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001106}
1107
Chuck Lever346aa662015-05-26 11:52:06 -04001108void
1109rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001110{
Chuck Lever346aa662015-05-26 11:52:06 -04001111 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001112
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001113 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001114 list_add_tail(&mw->mw_list, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001115 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001116}
1117
Chuck Lever05c97462016-09-06 11:22:58 -04001118static struct rpcrdma_rep *
1119rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1120{
1121 /* If an RPC previously completed without a reply (say, a
1122 * credential problem or a soft timeout occurs) then hold off
1123 * on supplying more Receive buffers until the number of new
1124 * pending RPCs catches up to the number of posted Receives.
1125 */
1126 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1127 return NULL;
1128
1129 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1130 return NULL;
1131 buffers->rb_recv_count++;
1132 return rpcrdma_buffer_get_rep_locked(buffers);
1133}
1134
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001135/*
1136 * Get a set of request/reply buffers.
Chuck Lever78d506e2016-09-06 11:22:49 -04001137 *
1138 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001139 */
1140struct rpcrdma_req *
1141rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1142{
1143 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001144
Chuck Levera5b027e2015-10-24 17:27:27 -04001145 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001146 if (list_empty(&buffers->rb_send_bufs))
1147 goto out_reqbuf;
Chuck Lever05c97462016-09-06 11:22:58 -04001148 buffers->rb_send_count++;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001149 req = rpcrdma_buffer_get_req_locked(buffers);
Chuck Lever05c97462016-09-06 11:22:58 -04001150 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001151 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001152 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001153
Chuck Lever1e465fd2015-10-24 17:27:02 -04001154out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001155 spin_unlock(&buffers->rb_lock);
Chuck Lever78d506e2016-09-06 11:22:49 -04001156 pr_warn("RPC: %s: out of request buffers\n", __func__);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001157 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001158}
1159
1160/*
1161 * Put request/reply buffers back into pool.
1162 * Pre-decrement counter/array index.
1163 */
1164void
1165rpcrdma_buffer_put(struct rpcrdma_req *req)
1166{
1167 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001168 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001169
Chuck Lever90aab602016-09-15 10:56:43 -04001170 req->rl_send_wr.num_sge = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001171 req->rl_reply = NULL;
1172
Chuck Levera5b027e2015-10-24 17:27:27 -04001173 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001174 buffers->rb_send_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001175 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001176 if (rep) {
1177 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001178 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001179 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001180 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001181}
1182
1183/*
1184 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001185 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001186 */
1187void
1188rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1189{
1190 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001191
Chuck Levera5b027e2015-10-24 17:27:27 -04001192 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001193 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001194 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001195}
1196
1197/*
1198 * Put reply buffers back into pool when not attached to
1199 * request. This happens in error conditions.
1200 */
1201void
1202rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1203{
Chuck Leverfed171b2015-05-26 11:51:37 -04001204 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001205
Chuck Levera5b027e2015-10-24 17:27:27 -04001206 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001207 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001208 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001209 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001210}
1211
Chuck Lever9128c3e2015-01-21 11:04:00 -05001212/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001213 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001214 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001215 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001216 * @flags: GFP flags
1217 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001218 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1219 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001220 *
1221 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001222 * receiving the payload of RDMA RECV operations. During Long Calls
1223 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001224 */
1225struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001226rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1227 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001228{
1229 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001230
Chuck Lever9128c3e2015-01-21 11:04:00 -05001231 rb = kmalloc(sizeof(*rb) + size, flags);
1232 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001233 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001234
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001235 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001236 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001237 rb->rg_iov.length = size;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001238
1239 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001240}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001241
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001242/**
1243 * __rpcrdma_map_regbuf - DMA-map a regbuf
1244 * @ia: controlling rpcrdma_ia
1245 * @rb: regbuf to be mapped
1246 */
1247bool
1248__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1249{
1250 if (rb->rg_direction == DMA_NONE)
1251 return false;
1252
1253 rb->rg_iov.addr = ib_dma_map_single(ia->ri_device,
1254 (void *)rb->rg_base,
1255 rdmab_length(rb),
1256 rb->rg_direction);
1257 if (ib_dma_mapping_error(ia->ri_device, rdmab_addr(rb)))
1258 return false;
1259
1260 rb->rg_device = ia->ri_device;
1261 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1262 return true;
1263}
1264
1265static void
1266rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1267{
1268 if (!rpcrdma_regbuf_is_mapped(rb))
1269 return;
1270
1271 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1272 rdmab_length(rb), rb->rg_direction);
1273 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001274}
1275
1276/**
1277 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001278 * @rb: regbuf to be deregistered and freed
1279 */
1280void
Chuck Lever13650c22016-09-15 10:56:26 -04001281rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001282{
Chuck Levere531dca2015-08-03 13:03:20 -04001283 if (!rb)
1284 return;
1285
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001286 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001287 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001288}
1289
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001290/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001291 * Prepost any receive buffer, then post send.
1292 *
1293 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1294 */
1295int
1296rpcrdma_ep_post(struct rpcrdma_ia *ia,
1297 struct rpcrdma_ep *ep,
1298 struct rpcrdma_req *req)
1299{
Chuck Lever90aab602016-09-15 10:56:43 -04001300 struct ib_send_wr *send_wr = &req->rl_send_wr;
1301 struct ib_send_wr *send_wr_fail;
Chuck Lever655fec62016-09-15 10:57:24 -04001302 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001303
Chuck Lever90aab602016-09-15 10:56:43 -04001304 if (req->rl_reply) {
1305 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001306 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001307 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001308 req->rl_reply = NULL;
1309 }
1310
Chuck Leverb3221d62015-08-03 13:03:39 -04001311 dprintk("RPC: %s: posting %d s/g entries\n",
Chuck Lever90aab602016-09-15 10:56:43 -04001312 __func__, send_wr->num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001313
Chuck Lever8d38de62016-11-29 10:52:16 -05001314 rpcrdma_set_signaled(ep, send_wr);
Chuck Lever90aab602016-09-15 10:56:43 -04001315 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001316 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001317 goto out_postsend_err;
1318 return 0;
1319
1320out_postsend_err:
1321 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1322 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001323}
1324
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001325int
1326rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001327 struct rpcrdma_rep *rep)
1328{
Chuck Lever6ea8e712016-09-15 10:56:51 -04001329 struct ib_recv_wr *recv_wr_fail;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001330 int rc;
1331
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001332 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1333 goto out_map;
Chuck Lever6ea8e712016-09-15 10:56:51 -04001334 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001335 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001336 goto out_postrecv;
1337 return 0;
1338
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001339out_map:
1340 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1341 return -EIO;
1342
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001343out_postrecv:
1344 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1345 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001346}
Chuck Lever43e95982014-07-29 17:23:34 -04001347
Chuck Leverf531a5d2015-10-24 17:27:43 -04001348/**
1349 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1350 * @r_xprt: transport associated with these backchannel resources
1351 * @min_reqs: minimum number of incoming requests expected
1352 *
1353 * Returns zero if all requested buffers were posted, or a negative errno.
1354 */
1355int
1356rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1357{
1358 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1359 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001360 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001361 int rc;
1362
1363 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001364 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001365 if (list_empty(&buffers->rb_recv_bufs))
1366 goto out_reqbuf;
1367 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001368 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001369
Chuck Leverb1573802016-09-15 10:56:35 -04001370 rc = rpcrdma_ep_post_recv(ia, rep);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001371 if (rc)
1372 goto out_rc;
1373 }
1374
1375 return 0;
1376
1377out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001378 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001379 pr_warn("%s: no extra receive buffers\n", __func__);
1380 return -ENOMEM;
1381
1382out_rc:
1383 rpcrdma_recv_buffer_put(rep);
1384 return rc;
1385}