blob: a49c788aa59a1632cd397543e73c2cd42dab5035 [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
132static void
Chuck Leverfe97b472015-10-24 17:27:10 -0400133rpcrdma_receive_worker(struct work_struct *work)
134{
135 struct rpcrdma_rep *rep =
136 container_of(work, struct rpcrdma_rep, rr_work);
137
138 rpcrdma_reply_handler(rep);
139}
140
Chuck Lever23826c72016-03-04 11:28:27 -0500141/* Perform basic sanity checking to avoid using garbage
142 * to update the credit grant value.
143 */
144static void
145rpcrdma_update_granted_credits(struct rpcrdma_rep *rep)
146{
147 struct rpcrdma_msg *rmsgp = rdmab_to_msg(rep->rr_rdmabuf);
148 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
149 u32 credits;
150
151 if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
152 return;
153
154 credits = be32_to_cpu(rmsgp->rm_credit);
155 if (credits == 0)
156 credits = 1; /* don't deadlock */
157 else if (credits > buffer->rb_max_requests)
158 credits = buffer->rb_max_requests;
159
160 atomic_set(&buffer->rb_credits, credits);
161}
162
Chuck Lever552bf222016-03-04 11:28:36 -0500163/**
164 * rpcrdma_receive_wc - Invoked by RDMA provider for each polled Receive WC
165 * @cq: completion queue (ignored)
166 * @wc: completed WR
167 *
168 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400169static void
Chuck Lever552bf222016-03-04 11:28:36 -0500170rpcrdma_receive_wc(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400171{
Chuck Lever552bf222016-03-04 11:28:36 -0500172 struct ib_cqe *cqe = wc->wr_cqe;
173 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
174 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400175
Chuck Lever85024272015-01-21 11:02:04 -0500176 /* WARNING: Only wr_id and status are reliable at this point */
177 if (wc->status != IB_WC_SUCCESS)
178 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400179
Chuck Lever85024272015-01-21 11:02:04 -0500180 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400181 if (wc->opcode != IB_WC_RECV)
182 return;
183
Chuck Lever85024272015-01-21 11:02:04 -0500184 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
185 __func__, rep, wc->byte_len);
186
Chuck Leverfc664482014-05-28 10:33:25 -0400187 rep->rr_len = wc->byte_len;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400188 ib_dma_sync_single_for_cpu(rep->rr_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -0500189 rdmab_addr(rep->rr_rdmabuf),
190 rep->rr_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500191
192 rpcrdma_update_granted_credits(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400193
194out_schedule:
Chuck Leverfe97b472015-10-24 17:27:10 -0400195 queue_work(rpcrdma_receive_wq, &rep->rr_work);
Chuck Lever85024272015-01-21 11:02:04 -0500196 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400197
Chuck Lever85024272015-01-21 11:02:04 -0500198out_fail:
199 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500200 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
201 ib_wc_status_msg(wc->status),
202 wc->status, wc->vendor_err);
Chuck Leverb0e178a2015-10-24 17:26:54 -0400203 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500204 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400205}
206
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400207static int
208rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
209{
210 struct rpcrdma_xprt *xprt = id->context;
211 struct rpcrdma_ia *ia = &xprt->rx_ia;
212 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500213#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400214 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800215#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500216 struct ib_qp_attr *attr = &ia->ri_qp_attr;
217 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400218 int connstate = 0;
219
220 switch (event->event) {
221 case RDMA_CM_EVENT_ADDR_RESOLVED:
222 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400223 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400224 complete(&ia->ri_done);
225 break;
226 case RDMA_CM_EVENT_ADDR_ERROR:
227 ia->ri_async_rc = -EHOSTUNREACH;
228 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
229 __func__, ep);
230 complete(&ia->ri_done);
231 break;
232 case RDMA_CM_EVENT_ROUTE_ERROR:
233 ia->ri_async_rc = -ENETUNREACH;
234 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
235 __func__, ep);
236 complete(&ia->ri_done);
237 break;
238 case RDMA_CM_EVENT_ESTABLISHED:
239 connstate = 1;
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500240 ib_query_qp(ia->ri_id->qp, attr,
241 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
242 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400243 dprintk("RPC: %s: %d responder resources"
244 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500245 __func__, attr->max_dest_rd_atomic,
246 attr->max_rd_atomic);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400247 goto connected;
248 case RDMA_CM_EVENT_CONNECT_ERROR:
249 connstate = -ENOTCONN;
250 goto connected;
251 case RDMA_CM_EVENT_UNREACHABLE:
252 connstate = -ENETDOWN;
253 goto connected;
254 case RDMA_CM_EVENT_REJECTED:
255 connstate = -ECONNREFUSED;
256 goto connected;
257 case RDMA_CM_EVENT_DISCONNECTED:
258 connstate = -ECONNABORTED;
259 goto connected;
260 case RDMA_CM_EVENT_DEVICE_REMOVAL:
261 connstate = -ENODEV;
262connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400263 dprintk("RPC: %s: %sconnected\n",
264 __func__, connstate > 0 ? "" : "dis");
Chuck Lever23826c72016-03-04 11:28:27 -0500265 atomic_set(&xprt->rx_buf.rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400266 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500267 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400268 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400269 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400270 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400271 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
272 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300273 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400274 break;
275 }
276
Jeff Laytonf895b252014-11-17 16:58:04 -0500277#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400278 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500279 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400280 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400281
Chuck Levera0ce85f2015-03-30 14:34:21 -0400282 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400283 sap, rpc_get_port(sap),
Chuck Lever89e0d1122015-05-26 11:51:56 -0400284 ia->ri_device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400285 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400286 xprt->rx_buf.rb_max_requests,
287 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
288 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400289 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
290 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400291 }
292#endif
293
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400294 return 0;
295}
296
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400297static void rpcrdma_destroy_id(struct rdma_cm_id *id)
298{
299 if (id) {
300 module_put(id->device->owner);
301 rdma_destroy_id(id);
302 }
303}
304
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400305static struct rdma_cm_id *
306rpcrdma_create_id(struct rpcrdma_xprt *xprt,
307 struct rpcrdma_ia *ia, struct sockaddr *addr)
308{
309 struct rdma_cm_id *id;
310 int rc;
311
Tom Talpey1a954052008-10-09 15:01:31 -0400312 init_completion(&ia->ri_done);
313
Guy Shapirofa201052015-10-22 15:20:10 +0300314 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
315 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400316 if (IS_ERR(id)) {
317 rc = PTR_ERR(id);
318 dprintk("RPC: %s: rdma_create_id() failed %i\n",
319 __func__, rc);
320 return id;
321 }
322
Tom Talpey5675add2008-10-09 15:01:41 -0400323 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400324 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
325 if (rc) {
326 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
327 __func__, rc);
328 goto out;
329 }
Tom Talpey5675add2008-10-09 15:01:41 -0400330 wait_for_completion_interruptible_timeout(&ia->ri_done,
331 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400332
333 /* FIXME:
334 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
335 * be pinned while there are active NFS/RDMA mounts to prevent
336 * hangs and crashes at umount time.
337 */
338 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
339 dprintk("RPC: %s: Failed to get device module\n",
340 __func__);
341 ia->ri_async_rc = -ENODEV;
342 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400343 rc = ia->ri_async_rc;
344 if (rc)
345 goto out;
346
Tom Talpey5675add2008-10-09 15:01:41 -0400347 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400348 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
349 if (rc) {
350 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
351 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400352 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400353 }
Tom Talpey5675add2008-10-09 15:01:41 -0400354 wait_for_completion_interruptible_timeout(&ia->ri_done,
355 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400356 rc = ia->ri_async_rc;
357 if (rc)
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400358 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400359
360 return id;
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400361put:
362 module_put(id->device->owner);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400363out:
364 rdma_destroy_id(id);
365 return ERR_PTR(rc);
366}
367
368/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400369 * Exported functions.
370 */
371
372/*
373 * Open and initialize an Interface Adapter.
374 * o initializes fields of struct rpcrdma_ia, including
375 * interface and provider attributes and protection zone.
376 */
377int
378rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
379{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400380 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400381 int rc;
382
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400383 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
384 if (IS_ERR(ia->ri_id)) {
385 rc = PTR_ERR(ia->ri_id);
386 goto out1;
387 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400388 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400389
Chuck Lever89e0d1122015-05-26 11:51:56 -0400390 ia->ri_pd = ib_alloc_pd(ia->ri_device);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400391 if (IS_ERR(ia->ri_pd)) {
392 rc = PTR_ERR(ia->ri_pd);
Chuck Leverb54054c2016-06-29 13:53:27 -0400393 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400394 goto out2;
395 }
396
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400397 switch (memreg) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400398 case RPCRDMA_FRMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400399 if (frwr_is_supported(ia)) {
400 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
401 break;
402 }
403 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400404 case RPCRDMA_MTHCAFMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400405 if (fmr_is_supported(ia)) {
406 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
407 break;
408 }
409 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400410 default:
Chuck Leverb54054c2016-06-29 13:53:27 -0400411 pr_err("rpcrdma: Unsupported memory registration mode: %d\n",
412 memreg);
413 rc = -EINVAL;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500414 goto out3;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400415 }
416
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400417 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500418
419out3:
420 ib_dealloc_pd(ia->ri_pd);
421 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400422out2:
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400423 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400424 ia->ri_id = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400425out1:
426 return rc;
427}
428
429/*
430 * Clean up/close an IA.
431 * o if event handles and PD have been initialized, free them.
432 * o close the IA
433 */
434void
435rpcrdma_ia_close(struct rpcrdma_ia *ia)
436{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400437 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400438 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
439 if (ia->ri_id->qp)
440 rdma_destroy_qp(ia->ri_id);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400441 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400442 ia->ri_id = NULL;
443 }
Chuck Lever6d446982015-05-26 11:51:27 -0400444
445 /* If the pd is still busy, xprtrdma missed freeing a resource */
446 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600447 ib_dealloc_pd(ia->ri_pd);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400448}
449
450/*
451 * Create unconnected endpoint.
452 */
453int
454rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
455 struct rpcrdma_create_data_internal *cdata)
456{
Chuck Leverfc664482014-05-28 10:33:25 -0400457 struct ib_cq *sendcq, *recvcq;
Chuck Lever124fa172015-10-24 17:27:51 -0400458 unsigned int max_qp_wr;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500459 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400460
Or Gerlitze3e45b12015-12-18 10:59:48 +0200461 if (ia->ri_device->attrs.max_sge < RPCRDMA_MAX_IOVS) {
Chuck Leverb3221d62015-08-03 13:03:39 -0400462 dprintk("RPC: %s: insufficient sge's available\n",
463 __func__);
464 return -ENOMEM;
465 }
466
Or Gerlitze3e45b12015-12-18 10:59:48 +0200467 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400468 dprintk("RPC: %s: insufficient wqe's available\n",
469 __func__);
470 return -ENOMEM;
471 }
Chuck Lever550d7502016-05-02 14:41:47 -0400472 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
Chuck Lever124fa172015-10-24 17:27:51 -0400473
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400474 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400475 if (cdata->max_requests > max_qp_wr)
476 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400477
478 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
479 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400480 ep->rep_attr.srq = NULL;
481 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400482 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400483 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
Chuck Lever3968cb52015-03-30 14:35:26 -0400484 rc = ia->ri_ops->ro_open(ia, ep, cdata);
485 if (rc)
486 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400487 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400488 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400489 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
Chuck Leverb3221d62015-08-03 13:03:39 -0400490 ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_IOVS;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400491 ep->rep_attr.cap.max_recv_sge = 1;
492 ep->rep_attr.cap.max_inline_data = 0;
493 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
494 ep->rep_attr.qp_type = IB_QPT_RC;
495 ep->rep_attr.port_num = ~0;
496
497 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
498 "iovs: send %d recv %d\n",
499 __func__,
500 ep->rep_attr.cap.max_send_wr,
501 ep->rep_attr.cap.max_recv_wr,
502 ep->rep_attr.cap.max_send_sge,
503 ep->rep_attr.cap.max_recv_sge);
504
505 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400506 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500507 if (ep->rep_cqinit <= 2)
508 ep->rep_cqinit = 0; /* always signal? */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400509 INIT_CQCOUNT(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400510 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400511 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400512
Chuck Lever2fa8f882016-03-04 11:28:53 -0500513 sendcq = ib_alloc_cq(ia->ri_device, NULL,
514 ep->rep_attr.cap.max_send_wr + 1,
515 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400516 if (IS_ERR(sendcq)) {
517 rc = PTR_ERR(sendcq);
518 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400519 __func__, rc);
520 goto out1;
521 }
522
Chuck Lever552bf222016-03-04 11:28:36 -0500523 recvcq = ib_alloc_cq(ia->ri_device, NULL,
524 ep->rep_attr.cap.max_recv_wr + 1,
525 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400526 if (IS_ERR(recvcq)) {
527 rc = PTR_ERR(recvcq);
528 dprintk("RPC: %s: failed to create recv CQ: %i\n",
529 __func__, rc);
530 goto out2;
531 }
532
Chuck Leverfc664482014-05-28 10:33:25 -0400533 ep->rep_attr.send_cq = sendcq;
534 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400535
536 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400537 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400538
539 /* RPC/RDMA does not use private data */
540 ep->rep_remote_cma.private_data = NULL;
541 ep->rep_remote_cma.private_data_len = 0;
542
543 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400544 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200545 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400546 ep->rep_remote_cma.responder_resources = 32;
547 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500548 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200549 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400550
Chuck Leverb2dde942016-05-02 14:43:03 -0400551 /* Limit transport retries so client can detect server
552 * GID changes quickly. RPC layer handles re-establishing
553 * transport connection and retransmission.
554 */
555 ep->rep_remote_cma.retry_count = 6;
556
557 /* RPC-over-RDMA handles its own flow control. In addition,
558 * make all RNR NAKs visible so we know that RPC-over-RDMA
559 * flow control is working correctly (no NAKs should be seen).
560 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400561 ep->rep_remote_cma.flow_control = 0;
562 ep->rep_remote_cma.rnr_retry_count = 0;
563
564 return 0;
565
566out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500567 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400568out1:
569 return rc;
570}
571
572/*
573 * rpcrdma_ep_destroy
574 *
575 * Disconnect and destroy endpoint. After this, the only
576 * valid operations on the ep are to free it (if dynamically
577 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400578 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400579void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400580rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
581{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400582 dprintk("RPC: %s: entering, connected is %d\n",
583 __func__, ep->rep_connected);
584
Chuck Lever254f91e2014-05-28 10:32:17 -0400585 cancel_delayed_work_sync(&ep->rep_connect_worker);
586
Steve Wise72c02172015-09-21 12:24:23 -0500587 if (ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400588 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400589 rdma_destroy_qp(ia->ri_id);
590 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400591 }
592
Chuck Lever552bf222016-03-04 11:28:36 -0500593 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500594 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400595}
596
597/*
598 * Connect unconnected endpoint.
599 */
600int
601rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
602{
Chuck Lever73806c82014-07-29 17:23:25 -0400603 struct rdma_cm_id *id, *old;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400604 int rc = 0;
605 int retry_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400606
Tom Talpeyc0555512008-10-10 11:32:45 -0400607 if (ep->rep_connected != 0) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400608 struct rpcrdma_xprt *xprt;
609retry:
Chuck Leverec62f402014-05-28 10:34:07 -0400610 dprintk("RPC: %s: reconnecting...\n", __func__);
Chuck Lever282191c2014-07-29 17:25:55 -0400611
612 rpcrdma_ep_disconnect(ep, ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400613
614 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
615 id = rpcrdma_create_id(xprt, ia,
616 (struct sockaddr *)&xprt->rx_data.addr);
617 if (IS_ERR(id)) {
Chuck Leverec62f402014-05-28 10:34:07 -0400618 rc = -EHOSTUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400619 goto out;
620 }
621 /* TEMP TEMP TEMP - fail if new device:
622 * Deregister/remarshal *all* requests!
623 * Close and recreate adapter, pd, etc!
624 * Re-determine all attributes still sane!
625 * More stuff I haven't thought of!
626 * Rrrgh!
627 */
Chuck Lever89e0d1122015-05-26 11:51:56 -0400628 if (ia->ri_device != id->device) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400629 printk("RPC: %s: can't reconnect on "
630 "different device!\n", __func__);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400631 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400632 rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400633 goto out;
634 }
635 /* END TEMP */
Chuck Leverec62f402014-05-28 10:34:07 -0400636 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
637 if (rc) {
638 dprintk("RPC: %s: rdma_create_qp failed %i\n",
639 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400640 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400641 rc = -ENETUNREACH;
642 goto out;
643 }
Chuck Lever73806c82014-07-29 17:23:25 -0400644
Chuck Lever73806c82014-07-29 17:23:25 -0400645 old = ia->ri_id;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400646 ia->ri_id = id;
Chuck Lever73806c82014-07-29 17:23:25 -0400647
648 rdma_destroy_qp(old);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400649 rpcrdma_destroy_id(old);
Chuck Leverec62f402014-05-28 10:34:07 -0400650 } else {
651 dprintk("RPC: %s: connecting...\n", __func__);
652 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
653 if (rc) {
654 dprintk("RPC: %s: rdma_create_qp failed %i\n",
655 __func__, rc);
656 /* do not update ep->rep_connected */
657 return -ENETUNREACH;
658 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400659 }
660
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400661 ep->rep_connected = 0;
662
663 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
664 if (rc) {
665 dprintk("RPC: %s: rdma_connect() failed with %i\n",
666 __func__, rc);
667 goto out;
668 }
669
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400670 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
671
672 /*
673 * Check state. A non-peer reject indicates no listener
674 * (ECONNREFUSED), which may be a transient state. All
675 * others indicate a transport condition which has already
676 * undergone a best-effort.
677 */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800678 if (ep->rep_connected == -ECONNREFUSED &&
679 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400680 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
681 goto retry;
682 }
683 if (ep->rep_connected <= 0) {
684 /* Sometimes, the only way to reliably connect to remote
685 * CMs is to use same nonzero values for ORD and IRD. */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400686 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
687 (ep->rep_remote_cma.responder_resources == 0 ||
688 ep->rep_remote_cma.initiator_depth !=
689 ep->rep_remote_cma.responder_resources)) {
690 if (ep->rep_remote_cma.responder_resources == 0)
691 ep->rep_remote_cma.responder_resources = 1;
692 ep->rep_remote_cma.initiator_depth =
693 ep->rep_remote_cma.responder_resources;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400694 goto retry;
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400695 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400696 rc = ep->rep_connected;
697 } else {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400698 struct rpcrdma_xprt *r_xprt;
699 unsigned int extras;
700
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400701 dprintk("RPC: %s: connected\n", __func__);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400702
703 r_xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
704 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
705
706 if (extras) {
707 rc = rpcrdma_ep_post_extra_recv(r_xprt, extras);
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300708 if (rc) {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400709 pr_warn("%s: rpcrdma_ep_post_extra_recv: %i\n",
710 __func__, rc);
711 rc = 0;
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300712 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400713 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400714 }
715
716out:
717 if (rc)
718 ep->rep_connected = rc;
719 return rc;
720}
721
722/*
723 * rpcrdma_ep_disconnect
724 *
725 * This is separate from destroy to facilitate the ability
726 * to reconnect without recreating the endpoint.
727 *
728 * This call is not reentrant, and must not be made in parallel
729 * on the same endpoint.
730 */
Chuck Lever282191c2014-07-29 17:25:55 -0400731void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400732rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
733{
734 int rc;
735
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400736 rc = rdma_disconnect(ia->ri_id);
737 if (!rc) {
738 /* returns without wait if not connected */
739 wait_event_interruptible(ep->rep_connect_wait,
740 ep->rep_connected != 1);
741 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
742 (ep->rep_connected == 1) ? "still " : "dis");
743 } else {
744 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
745 ep->rep_connected = rc;
746 }
Chuck Lever550d7502016-05-02 14:41:47 -0400747
748 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400749}
750
Chuck Lever505bbe62016-06-29 13:52:54 -0400751static void
752rpcrdma_mr_recovery_worker(struct work_struct *work)
753{
754 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
755 rb_recovery_worker.work);
756 struct rpcrdma_mw *mw;
757
758 spin_lock(&buf->rb_recovery_lock);
759 while (!list_empty(&buf->rb_stale_mrs)) {
760 mw = list_first_entry(&buf->rb_stale_mrs,
761 struct rpcrdma_mw, mw_list);
762 list_del_init(&mw->mw_list);
763 spin_unlock(&buf->rb_recovery_lock);
764
765 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
766 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
767
768 spin_lock(&buf->rb_recovery_lock);
kbuild test robot53d78522016-07-16 06:02:05 +0800769 }
Chuck Lever505bbe62016-06-29 13:52:54 -0400770 spin_unlock(&buf->rb_recovery_lock);
771}
772
773void
774rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
775{
776 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
777 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
778
779 spin_lock(&buf->rb_recovery_lock);
780 list_add(&mw->mw_list, &buf->rb_stale_mrs);
781 spin_unlock(&buf->rb_recovery_lock);
782
783 schedule_delayed_work(&buf->rb_recovery_worker, 0);
784}
785
Chuck Levere2ac2362016-06-29 13:54:00 -0400786static void
787rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
788{
789 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
790 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
791 unsigned int count;
792 LIST_HEAD(free);
793 LIST_HEAD(all);
794
795 for (count = 0; count < 32; count++) {
796 struct rpcrdma_mw *mw;
797 int rc;
798
799 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
800 if (!mw)
801 break;
802
803 rc = ia->ri_ops->ro_init_mr(ia, mw);
804 if (rc) {
805 kfree(mw);
806 break;
807 }
808
809 mw->mw_xprt = r_xprt;
810
811 list_add(&mw->mw_list, &free);
812 list_add(&mw->mw_all, &all);
813 }
814
815 spin_lock(&buf->rb_mwlock);
816 list_splice(&free, &buf->rb_mws);
817 list_splice(&all, &buf->rb_all);
818 r_xprt->rx_stats.mrs_allocated += count;
819 spin_unlock(&buf->rb_mwlock);
820
821 dprintk("RPC: %s: created %u MRs\n", __func__, count);
822}
823
824static void
825rpcrdma_mr_refresh_worker(struct work_struct *work)
826{
827 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
828 rb_refresh_worker.work);
829 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
830 rx_buf);
831
832 rpcrdma_create_mrs(r_xprt);
833}
834
Chuck Leverf531a5d2015-10-24 17:27:43 -0400835struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500836rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
837{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400838 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500839 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500840
Chuck Lever85275c82015-01-21 11:04:16 -0500841 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500842 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500843 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500844
Chuck Leverf531a5d2015-10-24 17:27:43 -0400845 INIT_LIST_HEAD(&req->rl_free);
846 spin_lock(&buffer->rb_reqslock);
847 list_add(&req->rl_all, &buffer->rb_allreqs);
848 spin_unlock(&buffer->rb_reqslock);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500849 req->rl_cqe.done = rpcrdma_wc_send;
Chuck Lever13924022015-01-21 11:03:52 -0500850 req->rl_buffer = &r_xprt->rx_buf;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400851 INIT_LIST_HEAD(&req->rl_registered);
Chuck Lever90aab602016-09-15 10:56:43 -0400852 req->rl_send_wr.next = NULL;
853 req->rl_send_wr.wr_cqe = &req->rl_cqe;
854 req->rl_send_wr.sg_list = req->rl_send_iov;
855 req->rl_send_wr.opcode = IB_WR_SEND;
Chuck Lever13924022015-01-21 11:03:52 -0500856 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500857}
858
Chuck Leverf531a5d2015-10-24 17:27:43 -0400859struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -0500860rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
861{
862 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500863 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
864 struct rpcrdma_rep *rep;
865 int rc;
866
867 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500868 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500869 if (rep == NULL)
870 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500871
Chuck Lever13650c22016-09-15 10:56:26 -0400872 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -0400873 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever6b1184c2015-01-21 11:04:25 -0500874 if (IS_ERR(rep->rr_rdmabuf)) {
875 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500876 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500877 }
Chuck Lever13924022015-01-21 11:03:52 -0500878
Chuck Lever89e0d1122015-05-26 11:51:56 -0400879 rep->rr_device = ia->ri_device;
Chuck Lever552bf222016-03-04 11:28:36 -0500880 rep->rr_cqe.done = rpcrdma_receive_wc;
Chuck Leverfed171b2015-05-26 11:51:37 -0400881 rep->rr_rxprt = r_xprt;
Chuck Leverfe97b472015-10-24 17:27:10 -0400882 INIT_WORK(&rep->rr_work, rpcrdma_receive_worker);
Chuck Lever6ea8e712016-09-15 10:56:51 -0400883 rep->rr_recv_wr.next = NULL;
884 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
885 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
886 rep->rr_recv_wr.num_sge = 1;
Chuck Lever13924022015-01-21 11:03:52 -0500887 return rep;
888
889out_free:
890 kfree(rep);
891out:
892 return ERR_PTR(rc);
893}
894
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400895int
Chuck Leverac920d02015-01-21 11:03:44 -0500896rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400897{
Chuck Leverac920d02015-01-21 11:03:44 -0500898 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400899 int i, rc;
900
Chuck Lever1e465fd2015-10-24 17:27:02 -0400901 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -0400902 buf->rb_bc_srv_max_requests = 0;
Chuck Lever23826c72016-03-04 11:28:27 -0500903 atomic_set(&buf->rb_credits, 1);
Chuck Levere2ac2362016-06-29 13:54:00 -0400904 spin_lock_init(&buf->rb_mwlock);
Chuck Lever505bbe62016-06-29 13:52:54 -0400905 spin_lock_init(&buf->rb_lock);
906 spin_lock_init(&buf->rb_recovery_lock);
Chuck Levere2ac2362016-06-29 13:54:00 -0400907 INIT_LIST_HEAD(&buf->rb_mws);
908 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever505bbe62016-06-29 13:52:54 -0400909 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -0400910 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
911 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -0400912 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
913 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400914
Chuck Levere2ac2362016-06-29 13:54:00 -0400915 rpcrdma_create_mrs(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400916
Chuck Lever1e465fd2015-10-24 17:27:02 -0400917 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400918 INIT_LIST_HEAD(&buf->rb_allreqs);
919 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400920 for (i = 0; i < buf->rb_max_requests; i++) {
921 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400922
Chuck Lever13924022015-01-21 11:03:52 -0500923 req = rpcrdma_create_req(r_xprt);
924 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400925 dprintk("RPC: %s: request buffer %d alloc"
926 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500927 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400928 goto out;
929 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400930 req->rl_backchannel = false;
Chuck Lever1e465fd2015-10-24 17:27:02 -0400931 list_add(&req->rl_free, &buf->rb_send_bufs);
932 }
933
934 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -0400935 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
Chuck Lever1e465fd2015-10-24 17:27:02 -0400936 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400937
Chuck Lever13924022015-01-21 11:03:52 -0500938 rep = rpcrdma_create_rep(r_xprt);
939 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400940 dprintk("RPC: %s: reply buffer %d alloc failed\n",
941 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500942 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400943 goto out;
944 }
Chuck Lever1e465fd2015-10-24 17:27:02 -0400945 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400946 }
Chuck Lever13924022015-01-21 11:03:52 -0500947
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400948 return 0;
949out:
950 rpcrdma_buffer_destroy(buf);
951 return rc;
952}
953
Chuck Lever1e465fd2015-10-24 17:27:02 -0400954static struct rpcrdma_req *
955rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
956{
957 struct rpcrdma_req *req;
958
959 req = list_first_entry(&buf->rb_send_bufs,
960 struct rpcrdma_req, rl_free);
961 list_del(&req->rl_free);
962 return req;
963}
964
965static struct rpcrdma_rep *
966rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
967{
968 struct rpcrdma_rep *rep;
969
970 rep = list_first_entry(&buf->rb_recv_bufs,
971 struct rpcrdma_rep, rr_list);
972 list_del(&rep->rr_list);
973 return rep;
974}
975
Chuck Lever2e845222014-07-29 17:25:38 -0400976static void
Chuck Lever13650c22016-09-15 10:56:26 -0400977rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -0500978{
Chuck Lever13650c22016-09-15 10:56:26 -0400979 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500980 kfree(rep);
981}
982
Chuck Leverf531a5d2015-10-24 17:27:43 -0400983void
Chuck Lever13650c22016-09-15 10:56:26 -0400984rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -0500985{
Chuck Lever13650c22016-09-15 10:56:26 -0400986 rpcrdma_free_regbuf(req->rl_recvbuf);
987 rpcrdma_free_regbuf(req->rl_sendbuf);
988 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500989 kfree(req);
990}
991
Chuck Levere2ac2362016-06-29 13:54:00 -0400992static void
993rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
994{
995 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
996 rx_buf);
997 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
998 struct rpcrdma_mw *mw;
999 unsigned int count;
1000
1001 count = 0;
1002 spin_lock(&buf->rb_mwlock);
1003 while (!list_empty(&buf->rb_all)) {
1004 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1005 list_del(&mw->mw_all);
1006
1007 spin_unlock(&buf->rb_mwlock);
1008 ia->ri_ops->ro_release_mr(mw);
1009 count++;
1010 spin_lock(&buf->rb_mwlock);
1011 }
1012 spin_unlock(&buf->rb_mwlock);
1013 r_xprt->rx_stats.mrs_allocated = 0;
1014
1015 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1016}
1017
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001018void
1019rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1020{
Chuck Lever505bbe62016-06-29 13:52:54 -04001021 cancel_delayed_work_sync(&buf->rb_recovery_worker);
1022
Chuck Lever1e465fd2015-10-24 17:27:02 -04001023 while (!list_empty(&buf->rb_recv_bufs)) {
1024 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001025
Chuck Lever1e465fd2015-10-24 17:27:02 -04001026 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001027 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001028 }
Chuck Lever05c97462016-09-06 11:22:58 -04001029 buf->rb_send_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001030
Chuck Leverf531a5d2015-10-24 17:27:43 -04001031 spin_lock(&buf->rb_reqslock);
1032 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001033 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001034
Chuck Leverf531a5d2015-10-24 17:27:43 -04001035 req = list_first_entry(&buf->rb_allreqs,
1036 struct rpcrdma_req, rl_all);
1037 list_del(&req->rl_all);
1038
1039 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001040 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001041 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001042 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001043 spin_unlock(&buf->rb_reqslock);
Chuck Lever05c97462016-09-06 11:22:58 -04001044 buf->rb_recv_count = 0;
Chuck Lever9f9d8022014-07-29 17:24:45 -04001045
Chuck Levere2ac2362016-06-29 13:54:00 -04001046 rpcrdma_destroy_mrs(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001047}
1048
Chuck Lever346aa662015-05-26 11:52:06 -04001049struct rpcrdma_mw *
1050rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001051{
Chuck Lever346aa662015-05-26 11:52:06 -04001052 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1053 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001054
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001055 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001056 if (!list_empty(&buf->rb_mws)) {
1057 mw = list_first_entry(&buf->rb_mws,
1058 struct rpcrdma_mw, mw_list);
1059 list_del_init(&mw->mw_list);
Chuck Leverc2922c02014-07-29 17:24:36 -04001060 }
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001061 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001062
1063 if (!mw)
Chuck Levere2ac2362016-06-29 13:54:00 -04001064 goto out_nomws;
Chuck Lever346aa662015-05-26 11:52:06 -04001065 return mw;
Chuck Levere2ac2362016-06-29 13:54:00 -04001066
1067out_nomws:
1068 dprintk("RPC: %s: no MWs available\n", __func__);
1069 schedule_delayed_work(&buf->rb_refresh_worker, 0);
1070
1071 /* Allow the reply handler and refresh worker to run */
1072 cond_resched();
1073
1074 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001075}
1076
Chuck Lever346aa662015-05-26 11:52:06 -04001077void
1078rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001079{
Chuck Lever346aa662015-05-26 11:52:06 -04001080 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001081
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001082 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001083 list_add_tail(&mw->mw_list, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001084 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001085}
1086
Chuck Lever05c97462016-09-06 11:22:58 -04001087static struct rpcrdma_rep *
1088rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1089{
1090 /* If an RPC previously completed without a reply (say, a
1091 * credential problem or a soft timeout occurs) then hold off
1092 * on supplying more Receive buffers until the number of new
1093 * pending RPCs catches up to the number of posted Receives.
1094 */
1095 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1096 return NULL;
1097
1098 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1099 return NULL;
1100 buffers->rb_recv_count++;
1101 return rpcrdma_buffer_get_rep_locked(buffers);
1102}
1103
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001104/*
1105 * Get a set of request/reply buffers.
Chuck Lever78d506e2016-09-06 11:22:49 -04001106 *
1107 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001108 */
1109struct rpcrdma_req *
1110rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1111{
1112 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001113
Chuck Levera5b027e2015-10-24 17:27:27 -04001114 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001115 if (list_empty(&buffers->rb_send_bufs))
1116 goto out_reqbuf;
Chuck Lever05c97462016-09-06 11:22:58 -04001117 buffers->rb_send_count++;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001118 req = rpcrdma_buffer_get_req_locked(buffers);
Chuck Lever05c97462016-09-06 11:22:58 -04001119 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001120 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001121 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001122
Chuck Lever1e465fd2015-10-24 17:27:02 -04001123out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001124 spin_unlock(&buffers->rb_lock);
Chuck Lever78d506e2016-09-06 11:22:49 -04001125 pr_warn("RPC: %s: out of request buffers\n", __func__);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001126 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001127}
1128
1129/*
1130 * Put request/reply buffers back into pool.
1131 * Pre-decrement counter/array index.
1132 */
1133void
1134rpcrdma_buffer_put(struct rpcrdma_req *req)
1135{
1136 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001137 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001138
Chuck Lever90aab602016-09-15 10:56:43 -04001139 req->rl_send_wr.num_sge = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001140 req->rl_reply = NULL;
1141
Chuck Levera5b027e2015-10-24 17:27:27 -04001142 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001143 buffers->rb_send_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001144 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001145 if (rep) {
1146 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001147 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001148 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001149 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001150}
1151
1152/*
1153 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001154 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001155 */
1156void
1157rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1158{
1159 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001160
Chuck Levera5b027e2015-10-24 17:27:27 -04001161 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001162 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001163 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001164}
1165
1166/*
1167 * Put reply buffers back into pool when not attached to
1168 * request. This happens in error conditions.
1169 */
1170void
1171rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1172{
Chuck Leverfed171b2015-05-26 11:51:37 -04001173 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001174
Chuck Levera5b027e2015-10-24 17:27:27 -04001175 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001176 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001177 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001178 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001179}
1180
Chuck Lever9128c3e2015-01-21 11:04:00 -05001181/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001182 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001183 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001184 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001185 * @flags: GFP flags
1186 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001187 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1188 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001189 *
1190 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001191 * receiving the payload of RDMA RECV operations. During Long Calls
1192 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001193 */
1194struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001195rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1196 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001197{
1198 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001199
Chuck Lever9128c3e2015-01-21 11:04:00 -05001200 rb = kmalloc(sizeof(*rb) + size, flags);
1201 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001202 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001203
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001204 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001205 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001206 rb->rg_iov.length = size;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001207
Chuck Lever9128c3e2015-01-21 11:04:00 -05001208 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001209}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001210
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001211/**
1212 * __rpcrdma_map_regbuf - DMA-map a regbuf
1213 * @ia: controlling rpcrdma_ia
1214 * @rb: regbuf to be mapped
1215 */
1216bool
1217__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1218{
1219 if (rb->rg_direction == DMA_NONE)
1220 return false;
1221
1222 rb->rg_iov.addr = ib_dma_map_single(ia->ri_device,
1223 (void *)rb->rg_base,
1224 rdmab_length(rb),
1225 rb->rg_direction);
1226 if (ib_dma_mapping_error(ia->ri_device, rdmab_addr(rb)))
1227 return false;
1228
1229 rb->rg_device = ia->ri_device;
1230 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1231 return true;
1232}
1233
1234static void
1235rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1236{
1237 if (!rpcrdma_regbuf_is_mapped(rb))
1238 return;
1239
1240 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1241 rdmab_length(rb), rb->rg_direction);
1242 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001243}
1244
1245/**
1246 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001247 * @rb: regbuf to be deregistered and freed
1248 */
1249void
Chuck Lever13650c22016-09-15 10:56:26 -04001250rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001251{
Chuck Levere531dca2015-08-03 13:03:20 -04001252 if (!rb)
1253 return;
1254
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001255 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001256 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001257}
1258
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001259/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001260 * Prepost any receive buffer, then post send.
1261 *
1262 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1263 */
1264int
1265rpcrdma_ep_post(struct rpcrdma_ia *ia,
1266 struct rpcrdma_ep *ep,
1267 struct rpcrdma_req *req)
1268{
Chuck Leverb3221d62015-08-03 13:03:39 -04001269 struct ib_device *device = ia->ri_device;
Chuck Lever90aab602016-09-15 10:56:43 -04001270 struct ib_send_wr *send_wr = &req->rl_send_wr;
1271 struct ib_send_wr *send_wr_fail;
1272 struct ib_sge *sge = req->rl_send_iov;
Chuck Leverb3221d62015-08-03 13:03:39 -04001273 int i, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001274
Chuck Lever90aab602016-09-15 10:56:43 -04001275 if (req->rl_reply) {
1276 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001277 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001278 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001279 req->rl_reply = NULL;
1280 }
1281
Chuck Lever90aab602016-09-15 10:56:43 -04001282 for (i = 0; i < send_wr->num_sge; i++)
1283 ib_dma_sync_single_for_device(device, sge[i].addr,
1284 sge[i].length, DMA_TO_DEVICE);
Chuck Leverb3221d62015-08-03 13:03:39 -04001285 dprintk("RPC: %s: posting %d s/g entries\n",
Chuck Lever90aab602016-09-15 10:56:43 -04001286 __func__, send_wr->num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001287
1288 if (DECR_CQCOUNT(ep) > 0)
Chuck Lever90aab602016-09-15 10:56:43 -04001289 send_wr->send_flags = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001290 else { /* Provider must take a send completion every now and then */
1291 INIT_CQCOUNT(ep);
Chuck Lever90aab602016-09-15 10:56:43 -04001292 send_wr->send_flags = IB_SEND_SIGNALED;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001293 }
1294
Chuck Lever90aab602016-09-15 10:56:43 -04001295 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001296 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001297 goto out_postsend_err;
1298 return 0;
1299
1300out_postsend_err:
1301 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1302 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001303}
1304
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001305int
1306rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001307 struct rpcrdma_rep *rep)
1308{
Chuck Lever6ea8e712016-09-15 10:56:51 -04001309 struct ib_recv_wr *recv_wr_fail;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001310 int rc;
1311
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001312 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1313 goto out_map;
Chuck Lever6ea8e712016-09-15 10:56:51 -04001314 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001315 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001316 goto out_postrecv;
1317 return 0;
1318
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001319out_map:
1320 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1321 return -EIO;
1322
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001323out_postrecv:
1324 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1325 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001326}
Chuck Lever43e95982014-07-29 17:23:34 -04001327
Chuck Leverf531a5d2015-10-24 17:27:43 -04001328/**
1329 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1330 * @r_xprt: transport associated with these backchannel resources
1331 * @min_reqs: minimum number of incoming requests expected
1332 *
1333 * Returns zero if all requested buffers were posted, or a negative errno.
1334 */
1335int
1336rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1337{
1338 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1339 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001340 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001341 int rc;
1342
1343 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001344 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001345 if (list_empty(&buffers->rb_recv_bufs))
1346 goto out_reqbuf;
1347 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001348 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001349
Chuck Leverb1573802016-09-15 10:56:35 -04001350 rc = rpcrdma_ep_post_recv(ia, rep);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001351 if (rc)
1352 goto out_rc;
1353 }
1354
1355 return 0;
1356
1357out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001358 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001359 pr_warn("%s: no extra receive buffers\n", __func__);
1360 return -ENOMEM;
1361
1362out_rc:
1363 rpcrdma_recv_buffer_put(rep);
1364 return rc;
1365}