blob: 79a6346b96c2eb297a027c351fcc2e4caf3f3d44 [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 Lever13924022015-01-21 11:03:52 -0500883 return rep;
884
885out_free:
886 kfree(rep);
887out:
888 return ERR_PTR(rc);
889}
890
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400891int
Chuck Leverac920d02015-01-21 11:03:44 -0500892rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400893{
Chuck Leverac920d02015-01-21 11:03:44 -0500894 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400895 int i, rc;
896
Chuck Lever1e465fd2015-10-24 17:27:02 -0400897 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -0400898 buf->rb_bc_srv_max_requests = 0;
Chuck Lever23826c72016-03-04 11:28:27 -0500899 atomic_set(&buf->rb_credits, 1);
Chuck Levere2ac2362016-06-29 13:54:00 -0400900 spin_lock_init(&buf->rb_mwlock);
Chuck Lever505bbe62016-06-29 13:52:54 -0400901 spin_lock_init(&buf->rb_lock);
902 spin_lock_init(&buf->rb_recovery_lock);
Chuck Levere2ac2362016-06-29 13:54:00 -0400903 INIT_LIST_HEAD(&buf->rb_mws);
904 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever505bbe62016-06-29 13:52:54 -0400905 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -0400906 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
907 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -0400908 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
909 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400910
Chuck Levere2ac2362016-06-29 13:54:00 -0400911 rpcrdma_create_mrs(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400912
Chuck Lever1e465fd2015-10-24 17:27:02 -0400913 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400914 INIT_LIST_HEAD(&buf->rb_allreqs);
915 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400916 for (i = 0; i < buf->rb_max_requests; i++) {
917 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400918
Chuck Lever13924022015-01-21 11:03:52 -0500919 req = rpcrdma_create_req(r_xprt);
920 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400921 dprintk("RPC: %s: request buffer %d alloc"
922 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500923 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400924 goto out;
925 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400926 req->rl_backchannel = false;
Chuck Lever1e465fd2015-10-24 17:27:02 -0400927 list_add(&req->rl_free, &buf->rb_send_bufs);
928 }
929
930 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -0400931 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
Chuck Lever1e465fd2015-10-24 17:27:02 -0400932 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400933
Chuck Lever13924022015-01-21 11:03:52 -0500934 rep = rpcrdma_create_rep(r_xprt);
935 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400936 dprintk("RPC: %s: reply buffer %d alloc failed\n",
937 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500938 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400939 goto out;
940 }
Chuck Lever1e465fd2015-10-24 17:27:02 -0400941 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400942 }
Chuck Lever13924022015-01-21 11:03:52 -0500943
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400944 return 0;
945out:
946 rpcrdma_buffer_destroy(buf);
947 return rc;
948}
949
Chuck Lever1e465fd2015-10-24 17:27:02 -0400950static struct rpcrdma_req *
951rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
952{
953 struct rpcrdma_req *req;
954
955 req = list_first_entry(&buf->rb_send_bufs,
956 struct rpcrdma_req, rl_free);
957 list_del(&req->rl_free);
958 return req;
959}
960
961static struct rpcrdma_rep *
962rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
963{
964 struct rpcrdma_rep *rep;
965
966 rep = list_first_entry(&buf->rb_recv_bufs,
967 struct rpcrdma_rep, rr_list);
968 list_del(&rep->rr_list);
969 return rep;
970}
971
Chuck Lever2e845222014-07-29 17:25:38 -0400972static void
Chuck Lever13650c22016-09-15 10:56:26 -0400973rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -0500974{
Chuck Lever13650c22016-09-15 10:56:26 -0400975 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500976 kfree(rep);
977}
978
Chuck Leverf531a5d2015-10-24 17:27:43 -0400979void
Chuck Lever13650c22016-09-15 10:56:26 -0400980rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -0500981{
Chuck Lever13650c22016-09-15 10:56:26 -0400982 rpcrdma_free_regbuf(req->rl_recvbuf);
983 rpcrdma_free_regbuf(req->rl_sendbuf);
984 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500985 kfree(req);
986}
987
Chuck Levere2ac2362016-06-29 13:54:00 -0400988static void
989rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
990{
991 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
992 rx_buf);
993 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
994 struct rpcrdma_mw *mw;
995 unsigned int count;
996
997 count = 0;
998 spin_lock(&buf->rb_mwlock);
999 while (!list_empty(&buf->rb_all)) {
1000 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1001 list_del(&mw->mw_all);
1002
1003 spin_unlock(&buf->rb_mwlock);
1004 ia->ri_ops->ro_release_mr(mw);
1005 count++;
1006 spin_lock(&buf->rb_mwlock);
1007 }
1008 spin_unlock(&buf->rb_mwlock);
1009 r_xprt->rx_stats.mrs_allocated = 0;
1010
1011 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1012}
1013
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001014void
1015rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1016{
Chuck Lever505bbe62016-06-29 13:52:54 -04001017 cancel_delayed_work_sync(&buf->rb_recovery_worker);
1018
Chuck Lever1e465fd2015-10-24 17:27:02 -04001019 while (!list_empty(&buf->rb_recv_bufs)) {
1020 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001021
Chuck Lever1e465fd2015-10-24 17:27:02 -04001022 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001023 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001024 }
Chuck Lever05c97462016-09-06 11:22:58 -04001025 buf->rb_send_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001026
Chuck Leverf531a5d2015-10-24 17:27:43 -04001027 spin_lock(&buf->rb_reqslock);
1028 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001029 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001030
Chuck Leverf531a5d2015-10-24 17:27:43 -04001031 req = list_first_entry(&buf->rb_allreqs,
1032 struct rpcrdma_req, rl_all);
1033 list_del(&req->rl_all);
1034
1035 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001036 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001037 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001038 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001039 spin_unlock(&buf->rb_reqslock);
Chuck Lever05c97462016-09-06 11:22:58 -04001040 buf->rb_recv_count = 0;
Chuck Lever9f9d8022014-07-29 17:24:45 -04001041
Chuck Levere2ac2362016-06-29 13:54:00 -04001042 rpcrdma_destroy_mrs(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001043}
1044
Chuck Lever346aa662015-05-26 11:52:06 -04001045struct rpcrdma_mw *
1046rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001047{
Chuck Lever346aa662015-05-26 11:52:06 -04001048 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1049 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001050
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001051 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001052 if (!list_empty(&buf->rb_mws)) {
1053 mw = list_first_entry(&buf->rb_mws,
1054 struct rpcrdma_mw, mw_list);
1055 list_del_init(&mw->mw_list);
Chuck Leverc2922c02014-07-29 17:24:36 -04001056 }
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001057 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001058
1059 if (!mw)
Chuck Levere2ac2362016-06-29 13:54:00 -04001060 goto out_nomws;
Chuck Lever346aa662015-05-26 11:52:06 -04001061 return mw;
Chuck Levere2ac2362016-06-29 13:54:00 -04001062
1063out_nomws:
1064 dprintk("RPC: %s: no MWs available\n", __func__);
1065 schedule_delayed_work(&buf->rb_refresh_worker, 0);
1066
1067 /* Allow the reply handler and refresh worker to run */
1068 cond_resched();
1069
1070 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001071}
1072
Chuck Lever346aa662015-05-26 11:52:06 -04001073void
1074rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001075{
Chuck Lever346aa662015-05-26 11:52:06 -04001076 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001077
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001078 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001079 list_add_tail(&mw->mw_list, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001080 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001081}
1082
Chuck Lever05c97462016-09-06 11:22:58 -04001083static struct rpcrdma_rep *
1084rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1085{
1086 /* If an RPC previously completed without a reply (say, a
1087 * credential problem or a soft timeout occurs) then hold off
1088 * on supplying more Receive buffers until the number of new
1089 * pending RPCs catches up to the number of posted Receives.
1090 */
1091 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1092 return NULL;
1093
1094 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1095 return NULL;
1096 buffers->rb_recv_count++;
1097 return rpcrdma_buffer_get_rep_locked(buffers);
1098}
1099
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001100/*
1101 * Get a set of request/reply buffers.
Chuck Lever78d506e2016-09-06 11:22:49 -04001102 *
1103 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001104 */
1105struct rpcrdma_req *
1106rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1107{
1108 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001109
Chuck Levera5b027e2015-10-24 17:27:27 -04001110 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001111 if (list_empty(&buffers->rb_send_bufs))
1112 goto out_reqbuf;
Chuck Lever05c97462016-09-06 11:22:58 -04001113 buffers->rb_send_count++;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001114 req = rpcrdma_buffer_get_req_locked(buffers);
Chuck Lever05c97462016-09-06 11:22:58 -04001115 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001116 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001117 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001118
Chuck Lever1e465fd2015-10-24 17:27:02 -04001119out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001120 spin_unlock(&buffers->rb_lock);
Chuck Lever78d506e2016-09-06 11:22:49 -04001121 pr_warn("RPC: %s: out of request buffers\n", __func__);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001122 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001123}
1124
1125/*
1126 * Put request/reply buffers back into pool.
1127 * Pre-decrement counter/array index.
1128 */
1129void
1130rpcrdma_buffer_put(struct rpcrdma_req *req)
1131{
1132 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001133 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001134
Chuck Lever90aab602016-09-15 10:56:43 -04001135 req->rl_send_wr.num_sge = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001136 req->rl_reply = NULL;
1137
Chuck Levera5b027e2015-10-24 17:27:27 -04001138 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001139 buffers->rb_send_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001140 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001141 if (rep) {
1142 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001143 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001144 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001145 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001146}
1147
1148/*
1149 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001150 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001151 */
1152void
1153rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1154{
1155 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001156
Chuck Levera5b027e2015-10-24 17:27:27 -04001157 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001158 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001159 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001160}
1161
1162/*
1163 * Put reply buffers back into pool when not attached to
1164 * request. This happens in error conditions.
1165 */
1166void
1167rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1168{
Chuck Leverfed171b2015-05-26 11:51:37 -04001169 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001170
Chuck Levera5b027e2015-10-24 17:27:27 -04001171 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001172 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001173 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001174 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001175}
1176
Chuck Lever9128c3e2015-01-21 11:04:00 -05001177/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001178 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001179 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001180 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001181 * @flags: GFP flags
1182 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001183 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1184 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001185 *
1186 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001187 * receiving the payload of RDMA RECV operations. During Long Calls
1188 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001189 */
1190struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001191rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1192 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001193{
1194 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001195
Chuck Lever9128c3e2015-01-21 11:04:00 -05001196 rb = kmalloc(sizeof(*rb) + size, flags);
1197 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001198 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001199
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001200 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001201 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001202 rb->rg_iov.length = size;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001203
Chuck Lever9128c3e2015-01-21 11:04:00 -05001204 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001205}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001206
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001207/**
1208 * __rpcrdma_map_regbuf - DMA-map a regbuf
1209 * @ia: controlling rpcrdma_ia
1210 * @rb: regbuf to be mapped
1211 */
1212bool
1213__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1214{
1215 if (rb->rg_direction == DMA_NONE)
1216 return false;
1217
1218 rb->rg_iov.addr = ib_dma_map_single(ia->ri_device,
1219 (void *)rb->rg_base,
1220 rdmab_length(rb),
1221 rb->rg_direction);
1222 if (ib_dma_mapping_error(ia->ri_device, rdmab_addr(rb)))
1223 return false;
1224
1225 rb->rg_device = ia->ri_device;
1226 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1227 return true;
1228}
1229
1230static void
1231rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1232{
1233 if (!rpcrdma_regbuf_is_mapped(rb))
1234 return;
1235
1236 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1237 rdmab_length(rb), rb->rg_direction);
1238 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001239}
1240
1241/**
1242 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001243 * @rb: regbuf to be deregistered and freed
1244 */
1245void
Chuck Lever13650c22016-09-15 10:56:26 -04001246rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001247{
Chuck Levere531dca2015-08-03 13:03:20 -04001248 if (!rb)
1249 return;
1250
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001251 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001252 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001253}
1254
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001255/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001256 * Prepost any receive buffer, then post send.
1257 *
1258 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1259 */
1260int
1261rpcrdma_ep_post(struct rpcrdma_ia *ia,
1262 struct rpcrdma_ep *ep,
1263 struct rpcrdma_req *req)
1264{
Chuck Leverb3221d62015-08-03 13:03:39 -04001265 struct ib_device *device = ia->ri_device;
Chuck Lever90aab602016-09-15 10:56:43 -04001266 struct ib_send_wr *send_wr = &req->rl_send_wr;
1267 struct ib_send_wr *send_wr_fail;
1268 struct ib_sge *sge = req->rl_send_iov;
Chuck Leverb3221d62015-08-03 13:03:39 -04001269 int i, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001270
Chuck Lever90aab602016-09-15 10:56:43 -04001271 if (req->rl_reply) {
1272 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001273 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001274 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001275 req->rl_reply = NULL;
1276 }
1277
Chuck Lever90aab602016-09-15 10:56:43 -04001278 for (i = 0; i < send_wr->num_sge; i++)
1279 ib_dma_sync_single_for_device(device, sge[i].addr,
1280 sge[i].length, DMA_TO_DEVICE);
Chuck Leverb3221d62015-08-03 13:03:39 -04001281 dprintk("RPC: %s: posting %d s/g entries\n",
Chuck Lever90aab602016-09-15 10:56:43 -04001282 __func__, send_wr->num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001283
1284 if (DECR_CQCOUNT(ep) > 0)
Chuck Lever90aab602016-09-15 10:56:43 -04001285 send_wr->send_flags = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001286 else { /* Provider must take a send completion every now and then */
1287 INIT_CQCOUNT(ep);
Chuck Lever90aab602016-09-15 10:56:43 -04001288 send_wr->send_flags = IB_SEND_SIGNALED;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001289 }
1290
Chuck Lever90aab602016-09-15 10:56:43 -04001291 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001292 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001293 goto out_postsend_err;
1294 return 0;
1295
1296out_postsend_err:
1297 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1298 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001299}
1300
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001301int
1302rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001303 struct rpcrdma_rep *rep)
1304{
1305 struct ib_recv_wr recv_wr, *recv_wr_fail;
1306 int rc;
1307
1308 recv_wr.next = NULL;
Chuck Lever552bf222016-03-04 11:28:36 -05001309 recv_wr.wr_cqe = &rep->rr_cqe;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001310 recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001311 recv_wr.num_sge = 1;
1312
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001313 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1314 goto out_map;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001315 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001316 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001317 goto out_postrecv;
1318 return 0;
1319
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001320out_map:
1321 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1322 return -EIO;
1323
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001324out_postrecv:
1325 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1326 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001327}
Chuck Lever43e95982014-07-29 17:23:34 -04001328
Chuck Leverf531a5d2015-10-24 17:27:43 -04001329/**
1330 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1331 * @r_xprt: transport associated with these backchannel resources
1332 * @min_reqs: minimum number of incoming requests expected
1333 *
1334 * Returns zero if all requested buffers were posted, or a negative errno.
1335 */
1336int
1337rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1338{
1339 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1340 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001341 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001342 int rc;
1343
1344 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001345 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001346 if (list_empty(&buffers->rb_recv_bufs))
1347 goto out_reqbuf;
1348 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001349 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001350
Chuck Leverb1573802016-09-15 10:56:35 -04001351 rc = rpcrdma_ep_post_recv(ia, rep);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001352 if (rc)
1353 goto out_rc;
1354 }
1355
1356 return 0;
1357
1358out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001359 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001360 pr_warn("%s: no extra receive buffers\n", __func__);
1361 return -ENOMEM;
1362
1363out_rc:
1364 rpcrdma_recv_buffer_put(rep);
1365 return rc;
1366}