blob: 23f4da419a648b2d03baf63f6196e3fd71282a0c [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 Lever2f6922c2016-11-29 10:53:21 -0500106 pr_err("rpcrdma: %s on device %s ep %p\n",
107 ib_event_msg(event->event), event->device->name, context);
108
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400109 if (ep->rep_connected == 1) {
110 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500111 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400112 wake_up_all(&ep->rep_connect_wait);
113 }
114}
115
Chuck Lever2fa8f882016-03-04 11:28:53 -0500116/**
117 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
118 * @cq: completion queue (ignored)
119 * @wc: completed WR
120 *
Chuck Lever4220a072015-10-24 17:26:45 -0400121 */
122static void
Chuck Lever2fa8f882016-03-04 11:28:53 -0500123rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400124{
Chuck Lever2fa8f882016-03-04 11:28:53 -0500125 /* WARNING: Only wr_cqe and status are reliable at this point */
126 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
127 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
128 ib_wc_status_msg(wc->status),
129 wc->status, wc->vendor_err);
Chuck Leverfc664482014-05-28 10:33:25 -0400130}
131
Chuck Lever23826c72016-03-04 11:28:27 -0500132/* Perform basic sanity checking to avoid using garbage
133 * to update the credit grant value.
134 */
135static void
136rpcrdma_update_granted_credits(struct rpcrdma_rep *rep)
137{
138 struct rpcrdma_msg *rmsgp = rdmab_to_msg(rep->rr_rdmabuf);
139 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
140 u32 credits;
141
142 if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
143 return;
144
145 credits = be32_to_cpu(rmsgp->rm_credit);
146 if (credits == 0)
147 credits = 1; /* don't deadlock */
148 else if (credits > buffer->rb_max_requests)
149 credits = buffer->rb_max_requests;
150
151 atomic_set(&buffer->rb_credits, credits);
152}
153
Chuck Lever552bf222016-03-04 11:28:36 -0500154/**
Chuck Lever1519e962016-09-15 10:57:49 -0400155 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
Chuck Lever552bf222016-03-04 11:28:36 -0500156 * @cq: completion queue (ignored)
157 * @wc: completed WR
158 *
159 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400160static void
Chuck Lever1519e962016-09-15 10:57:49 -0400161rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400162{
Chuck Lever552bf222016-03-04 11:28:36 -0500163 struct ib_cqe *cqe = wc->wr_cqe;
164 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
165 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400166
Chuck Lever85024272015-01-21 11:02:04 -0500167 /* WARNING: Only wr_id and status are reliable at this point */
168 if (wc->status != IB_WC_SUCCESS)
169 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400170
Chuck Lever85024272015-01-21 11:02:04 -0500171 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400172 if (wc->opcode != IB_WC_RECV)
173 return;
174
Chuck Lever85024272015-01-21 11:02:04 -0500175 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
176 __func__, rep, wc->byte_len);
177
Chuck Leverfc664482014-05-28 10:33:25 -0400178 rep->rr_len = wc->byte_len;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400179 rep->rr_wc_flags = wc->wc_flags;
180 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
181
Chuck Lever89e0d1122015-05-26 11:51:56 -0400182 ib_dma_sync_single_for_cpu(rep->rr_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -0500183 rdmab_addr(rep->rr_rdmabuf),
184 rep->rr_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500185
186 rpcrdma_update_granted_credits(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400187
188out_schedule:
Chuck Leverfe97b472015-10-24 17:27:10 -0400189 queue_work(rpcrdma_receive_wq, &rep->rr_work);
Chuck Lever85024272015-01-21 11:02:04 -0500190 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400191
Chuck Lever85024272015-01-21 11:02:04 -0500192out_fail:
193 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500194 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
195 ib_wc_status_msg(wc->status),
196 wc->status, wc->vendor_err);
Chuck Leverb0e178a2015-10-24 17:26:54 -0400197 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500198 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400199}
200
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400201static void
202rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
203 struct rdma_conn_param *param)
204{
205 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
206 const struct rpcrdma_connect_private *pmsg = param->private_data;
207 unsigned int rsize, wsize;
208
Chuck Leverc8b920b2016-09-15 10:57:16 -0400209 /* Default settings for RPC-over-RDMA Version One */
210 r_xprt->rx_ia.ri_reminv_expected = false;
Chuck Leverb5f0afb2017-02-08 16:59:54 -0500211 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400212 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
213 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
214
215 if (pmsg &&
216 pmsg->cp_magic == rpcrdma_cmp_magic &&
217 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
Chuck Leverc8b920b2016-09-15 10:57:16 -0400218 r_xprt->rx_ia.ri_reminv_expected = true;
Chuck Leverc95a3c62017-02-08 17:00:02 -0500219 r_xprt->rx_ia.ri_implicit_roundup = true;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400220 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
221 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
222 }
223
224 if (rsize < cdata->inline_rsize)
225 cdata->inline_rsize = rsize;
226 if (wsize < cdata->inline_wsize)
227 cdata->inline_wsize = wsize;
Chuck Lever6d6bf722016-11-29 10:53:13 -0500228 dprintk("RPC: %s: max send %u, max recv %u\n",
229 __func__, cdata->inline_wsize, cdata->inline_rsize);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400230 rpcrdma_set_max_header_sizes(r_xprt);
231}
232
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400233static int
234rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
235{
236 struct rpcrdma_xprt *xprt = id->context;
237 struct rpcrdma_ia *ia = &xprt->rx_ia;
238 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500239#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400240 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800241#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500242 struct ib_qp_attr *attr = &ia->ri_qp_attr;
243 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400244 int connstate = 0;
245
246 switch (event->event) {
247 case RDMA_CM_EVENT_ADDR_RESOLVED:
248 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400249 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400250 complete(&ia->ri_done);
251 break;
252 case RDMA_CM_EVENT_ADDR_ERROR:
253 ia->ri_async_rc = -EHOSTUNREACH;
254 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
255 __func__, ep);
256 complete(&ia->ri_done);
257 break;
258 case RDMA_CM_EVENT_ROUTE_ERROR:
259 ia->ri_async_rc = -ENETUNREACH;
260 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
261 __func__, ep);
262 complete(&ia->ri_done);
263 break;
264 case RDMA_CM_EVENT_ESTABLISHED:
265 connstate = 1;
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500266 ib_query_qp(ia->ri_id->qp, attr,
267 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
268 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400269 dprintk("RPC: %s: %d responder resources"
270 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500271 __func__, attr->max_dest_rd_atomic,
272 attr->max_rd_atomic);
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400273 rpcrdma_update_connect_private(xprt, &event->param.conn);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400274 goto connected;
275 case RDMA_CM_EVENT_CONNECT_ERROR:
276 connstate = -ENOTCONN;
277 goto connected;
278 case RDMA_CM_EVENT_UNREACHABLE:
279 connstate = -ENETDOWN;
280 goto connected;
281 case RDMA_CM_EVENT_REJECTED:
282 connstate = -ECONNREFUSED;
283 goto connected;
284 case RDMA_CM_EVENT_DISCONNECTED:
285 connstate = -ECONNABORTED;
286 goto connected;
287 case RDMA_CM_EVENT_DEVICE_REMOVAL:
288 connstate = -ENODEV;
289connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400290 dprintk("RPC: %s: %sconnected\n",
291 __func__, connstate > 0 ? "" : "dis");
Chuck Lever23826c72016-03-04 11:28:27 -0500292 atomic_set(&xprt->rx_buf.rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400293 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500294 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400295 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400296 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400297 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400298 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
299 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300300 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400301 break;
302 }
303
Jeff Laytonf895b252014-11-17 16:58:04 -0500304#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400305 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500306 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400307 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400308
Chuck Levera0ce85f2015-03-30 14:34:21 -0400309 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400310 sap, rpc_get_port(sap),
Chuck Lever89e0d1122015-05-26 11:51:56 -0400311 ia->ri_device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400312 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400313 xprt->rx_buf.rb_max_requests,
314 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
315 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400316 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
317 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400318 }
319#endif
320
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400321 return 0;
322}
323
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400324static void rpcrdma_destroy_id(struct rdma_cm_id *id)
325{
326 if (id) {
327 module_put(id->device->owner);
328 rdma_destroy_id(id);
329 }
330}
331
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400332static struct rdma_cm_id *
333rpcrdma_create_id(struct rpcrdma_xprt *xprt,
334 struct rpcrdma_ia *ia, struct sockaddr *addr)
335{
Chuck Lever109b88a2016-11-29 10:52:40 -0500336 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400337 struct rdma_cm_id *id;
338 int rc;
339
Tom Talpey1a954052008-10-09 15:01:31 -0400340 init_completion(&ia->ri_done);
341
Guy Shapirofa201052015-10-22 15:20:10 +0300342 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
343 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400344 if (IS_ERR(id)) {
345 rc = PTR_ERR(id);
346 dprintk("RPC: %s: rdma_create_id() failed %i\n",
347 __func__, rc);
348 return id;
349 }
350
Tom Talpey5675add2008-10-09 15:01:41 -0400351 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400352 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
353 if (rc) {
354 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
355 __func__, rc);
356 goto out;
357 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500358 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
359 if (rc < 0) {
360 dprintk("RPC: %s: wait() exited: %i\n",
361 __func__, rc);
362 goto out;
363 }
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400364
365 /* FIXME:
366 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
367 * be pinned while there are active NFS/RDMA mounts to prevent
368 * hangs and crashes at umount time.
369 */
370 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
371 dprintk("RPC: %s: Failed to get device module\n",
372 __func__);
373 ia->ri_async_rc = -ENODEV;
374 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400375 rc = ia->ri_async_rc;
376 if (rc)
377 goto out;
378
Tom Talpey5675add2008-10-09 15:01:41 -0400379 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400380 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
381 if (rc) {
382 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
383 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400384 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400385 }
Chuck Lever109b88a2016-11-29 10:52:40 -0500386 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
387 if (rc < 0) {
388 dprintk("RPC: %s: wait() exited: %i\n",
389 __func__, rc);
390 goto put;
391 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400392 rc = ia->ri_async_rc;
393 if (rc)
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400394 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400395
396 return id;
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400397put:
398 module_put(id->device->owner);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400399out:
400 rdma_destroy_id(id);
401 return ERR_PTR(rc);
402}
403
404/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400405 * Exported functions.
406 */
407
408/*
409 * Open and initialize an Interface Adapter.
410 * o initializes fields of struct rpcrdma_ia, including
411 * interface and provider attributes and protection zone.
412 */
413int
414rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
415{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400416 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400417 int rc;
418
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400419 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
420 if (IS_ERR(ia->ri_id)) {
421 rc = PTR_ERR(ia->ri_id);
422 goto out1;
423 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400424 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400425
Christoph Hellwiged082d32016-09-05 12:56:17 +0200426 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400427 if (IS_ERR(ia->ri_pd)) {
428 rc = PTR_ERR(ia->ri_pd);
Chuck Leverb54054c2016-06-29 13:53:27 -0400429 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400430 goto out2;
431 }
432
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400433 switch (memreg) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400434 case RPCRDMA_FRMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400435 if (frwr_is_supported(ia)) {
436 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
437 break;
438 }
439 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400440 case RPCRDMA_MTHCAFMR:
Chuck Leverb54054c2016-06-29 13:53:27 -0400441 if (fmr_is_supported(ia)) {
442 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
443 break;
444 }
445 /*FALLTHROUGH*/
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400446 default:
Chuck Leverb54054c2016-06-29 13:53:27 -0400447 pr_err("rpcrdma: Unsupported memory registration mode: %d\n",
448 memreg);
449 rc = -EINVAL;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500450 goto out3;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400451 }
452
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400453 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500454
455out3:
456 ib_dealloc_pd(ia->ri_pd);
457 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400458out2:
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400459 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400460 ia->ri_id = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400461out1:
462 return rc;
463}
464
465/*
466 * Clean up/close an IA.
467 * o if event handles and PD have been initialized, free them.
468 * o close the IA
469 */
470void
471rpcrdma_ia_close(struct rpcrdma_ia *ia)
472{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400473 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400474 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
475 if (ia->ri_id->qp)
476 rdma_destroy_qp(ia->ri_id);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400477 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400478 ia->ri_id = NULL;
479 }
Chuck Lever6d446982015-05-26 11:51:27 -0400480
481 /* If the pd is still busy, xprtrdma missed freeing a resource */
482 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600483 ib_dealloc_pd(ia->ri_pd);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400484}
485
486/*
487 * Create unconnected endpoint.
488 */
489int
490rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
491 struct rpcrdma_create_data_internal *cdata)
492{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400493 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
Chuck Leverfc664482014-05-28 10:33:25 -0400494 struct ib_cq *sendcq, *recvcq;
Chuck Lever124fa172015-10-24 17:27:51 -0400495 unsigned int max_qp_wr;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500496 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400497
Chuck Lever655fec62016-09-15 10:57:24 -0400498 if (ia->ri_device->attrs.max_sge < RPCRDMA_MAX_SEND_SGES) {
Chuck Leverb3221d62015-08-03 13:03:39 -0400499 dprintk("RPC: %s: insufficient sge's available\n",
500 __func__);
501 return -ENOMEM;
502 }
503
Or Gerlitze3e45b12015-12-18 10:59:48 +0200504 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400505 dprintk("RPC: %s: insufficient wqe's available\n",
506 __func__);
507 return -ENOMEM;
508 }
Chuck Lever550d7502016-05-02 14:41:47 -0400509 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
Chuck Lever124fa172015-10-24 17:27:51 -0400510
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400511 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400512 if (cdata->max_requests > max_qp_wr)
513 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400514
515 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
516 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400517 ep->rep_attr.srq = NULL;
518 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400519 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400520 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
Chuck Lever3968cb52015-03-30 14:35:26 -0400521 rc = ia->ri_ops->ro_open(ia, ep, cdata);
522 if (rc)
523 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400524 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400525 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400526 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
Chuck Lever655fec62016-09-15 10:57:24 -0400527 ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_SEND_SGES;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400528 ep->rep_attr.cap.max_recv_sge = 1;
529 ep->rep_attr.cap.max_inline_data = 0;
530 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
531 ep->rep_attr.qp_type = IB_QPT_RC;
532 ep->rep_attr.port_num = ~0;
533
534 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
535 "iovs: send %d recv %d\n",
536 __func__,
537 ep->rep_attr.cap.max_send_wr,
538 ep->rep_attr.cap.max_recv_wr,
539 ep->rep_attr.cap.max_send_sge,
540 ep->rep_attr.cap.max_recv_sge);
541
542 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400543 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500544 if (ep->rep_cqinit <= 2)
545 ep->rep_cqinit = 0; /* always signal? */
Chuck Lever8d38de62016-11-29 10:52:16 -0500546 rpcrdma_init_cqcount(ep, 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400547 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400548 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400549
Chuck Lever2fa8f882016-03-04 11:28:53 -0500550 sendcq = ib_alloc_cq(ia->ri_device, NULL,
551 ep->rep_attr.cap.max_send_wr + 1,
552 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400553 if (IS_ERR(sendcq)) {
554 rc = PTR_ERR(sendcq);
555 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400556 __func__, rc);
557 goto out1;
558 }
559
Chuck Lever552bf222016-03-04 11:28:36 -0500560 recvcq = ib_alloc_cq(ia->ri_device, NULL,
561 ep->rep_attr.cap.max_recv_wr + 1,
562 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400563 if (IS_ERR(recvcq)) {
564 rc = PTR_ERR(recvcq);
565 dprintk("RPC: %s: failed to create recv CQ: %i\n",
566 __func__, rc);
567 goto out2;
568 }
569
Chuck Leverfc664482014-05-28 10:33:25 -0400570 ep->rep_attr.send_cq = sendcq;
571 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400572
573 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400574 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400575
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400576 /* Prepare RDMA-CM private message */
577 pmsg->cp_magic = rpcrdma_cmp_magic;
578 pmsg->cp_version = RPCRDMA_CMP_VERSION;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400579 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400580 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
581 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
582 ep->rep_remote_cma.private_data = pmsg;
583 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400584
585 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400586 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200587 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400588 ep->rep_remote_cma.responder_resources = 32;
589 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500590 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200591 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400592
Chuck Leverb2dde942016-05-02 14:43:03 -0400593 /* Limit transport retries so client can detect server
594 * GID changes quickly. RPC layer handles re-establishing
595 * transport connection and retransmission.
596 */
597 ep->rep_remote_cma.retry_count = 6;
598
599 /* RPC-over-RDMA handles its own flow control. In addition,
600 * make all RNR NAKs visible so we know that RPC-over-RDMA
601 * flow control is working correctly (no NAKs should be seen).
602 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400603 ep->rep_remote_cma.flow_control = 0;
604 ep->rep_remote_cma.rnr_retry_count = 0;
605
606 return 0;
607
608out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500609 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400610out1:
611 return rc;
612}
613
614/*
615 * rpcrdma_ep_destroy
616 *
617 * Disconnect and destroy endpoint. After this, the only
618 * valid operations on the ep are to free it (if dynamically
619 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400620 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400621void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400622rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
623{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400624 dprintk("RPC: %s: entering, connected is %d\n",
625 __func__, ep->rep_connected);
626
Chuck Lever254f91e2014-05-28 10:32:17 -0400627 cancel_delayed_work_sync(&ep->rep_connect_worker);
628
Steve Wise72c02172015-09-21 12:24:23 -0500629 if (ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400630 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400631 rdma_destroy_qp(ia->ri_id);
632 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400633 }
634
Chuck Lever552bf222016-03-04 11:28:36 -0500635 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500636 ib_free_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400637}
638
639/*
640 * Connect unconnected endpoint.
641 */
642int
643rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
644{
Chuck Lever73806c82014-07-29 17:23:25 -0400645 struct rdma_cm_id *id, *old;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400646 int rc = 0;
647 int retry_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400648
Tom Talpeyc0555512008-10-10 11:32:45 -0400649 if (ep->rep_connected != 0) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400650 struct rpcrdma_xprt *xprt;
651retry:
Chuck Leverec62f402014-05-28 10:34:07 -0400652 dprintk("RPC: %s: reconnecting...\n", __func__);
Chuck Lever282191c2014-07-29 17:25:55 -0400653
654 rpcrdma_ep_disconnect(ep, ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400655
656 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
657 id = rpcrdma_create_id(xprt, ia,
658 (struct sockaddr *)&xprt->rx_data.addr);
659 if (IS_ERR(id)) {
Chuck Leverec62f402014-05-28 10:34:07 -0400660 rc = -EHOSTUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400661 goto out;
662 }
663 /* TEMP TEMP TEMP - fail if new device:
664 * Deregister/remarshal *all* requests!
665 * Close and recreate adapter, pd, etc!
666 * Re-determine all attributes still sane!
667 * More stuff I haven't thought of!
668 * Rrrgh!
669 */
Chuck Lever89e0d1122015-05-26 11:51:56 -0400670 if (ia->ri_device != id->device) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400671 printk("RPC: %s: can't reconnect on "
672 "different device!\n", __func__);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400673 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400674 rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400675 goto out;
676 }
677 /* END TEMP */
Chuck Leverec62f402014-05-28 10:34:07 -0400678 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
679 if (rc) {
680 dprintk("RPC: %s: rdma_create_qp failed %i\n",
681 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400682 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400683 rc = -ENETUNREACH;
684 goto out;
685 }
Chuck Lever73806c82014-07-29 17:23:25 -0400686
Chuck Lever73806c82014-07-29 17:23:25 -0400687 old = ia->ri_id;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400688 ia->ri_id = id;
Chuck Lever73806c82014-07-29 17:23:25 -0400689
690 rdma_destroy_qp(old);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400691 rpcrdma_destroy_id(old);
Chuck Leverec62f402014-05-28 10:34:07 -0400692 } else {
693 dprintk("RPC: %s: connecting...\n", __func__);
694 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
695 if (rc) {
696 dprintk("RPC: %s: rdma_create_qp failed %i\n",
697 __func__, rc);
698 /* do not update ep->rep_connected */
699 return -ENETUNREACH;
700 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400701 }
702
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400703 ep->rep_connected = 0;
704
705 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
706 if (rc) {
707 dprintk("RPC: %s: rdma_connect() failed with %i\n",
708 __func__, rc);
709 goto out;
710 }
711
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400712 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
713
714 /*
715 * Check state. A non-peer reject indicates no listener
716 * (ECONNREFUSED), which may be a transient state. All
717 * others indicate a transport condition which has already
718 * undergone a best-effort.
719 */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800720 if (ep->rep_connected == -ECONNREFUSED &&
721 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400722 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
723 goto retry;
724 }
725 if (ep->rep_connected <= 0) {
726 /* Sometimes, the only way to reliably connect to remote
727 * CMs is to use same nonzero values for ORD and IRD. */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400728 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
729 (ep->rep_remote_cma.responder_resources == 0 ||
730 ep->rep_remote_cma.initiator_depth !=
731 ep->rep_remote_cma.responder_resources)) {
732 if (ep->rep_remote_cma.responder_resources == 0)
733 ep->rep_remote_cma.responder_resources = 1;
734 ep->rep_remote_cma.initiator_depth =
735 ep->rep_remote_cma.responder_resources;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400736 goto retry;
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400737 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400738 rc = ep->rep_connected;
739 } else {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400740 struct rpcrdma_xprt *r_xprt;
741 unsigned int extras;
742
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400743 dprintk("RPC: %s: connected\n", __func__);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400744
745 r_xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
746 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
747
748 if (extras) {
749 rc = rpcrdma_ep_post_extra_recv(r_xprt, extras);
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300750 if (rc) {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400751 pr_warn("%s: rpcrdma_ep_post_extra_recv: %i\n",
752 __func__, rc);
753 rc = 0;
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300754 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400755 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400756 }
757
758out:
759 if (rc)
760 ep->rep_connected = rc;
761 return rc;
762}
763
764/*
765 * rpcrdma_ep_disconnect
766 *
767 * This is separate from destroy to facilitate the ability
768 * to reconnect without recreating the endpoint.
769 *
770 * This call is not reentrant, and must not be made in parallel
771 * on the same endpoint.
772 */
Chuck Lever282191c2014-07-29 17:25:55 -0400773void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400774rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
775{
776 int rc;
777
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400778 rc = rdma_disconnect(ia->ri_id);
779 if (!rc) {
780 /* returns without wait if not connected */
781 wait_event_interruptible(ep->rep_connect_wait,
782 ep->rep_connected != 1);
783 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
784 (ep->rep_connected == 1) ? "still " : "dis");
785 } else {
786 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
787 ep->rep_connected = rc;
788 }
Chuck Lever550d7502016-05-02 14:41:47 -0400789
790 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400791}
792
Chuck Lever505bbe62016-06-29 13:52:54 -0400793static void
794rpcrdma_mr_recovery_worker(struct work_struct *work)
795{
796 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
797 rb_recovery_worker.work);
798 struct rpcrdma_mw *mw;
799
800 spin_lock(&buf->rb_recovery_lock);
801 while (!list_empty(&buf->rb_stale_mrs)) {
802 mw = list_first_entry(&buf->rb_stale_mrs,
803 struct rpcrdma_mw, mw_list);
804 list_del_init(&mw->mw_list);
805 spin_unlock(&buf->rb_recovery_lock);
806
807 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
808 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
809
810 spin_lock(&buf->rb_recovery_lock);
kbuild test robot53d78522016-07-16 06:02:05 +0800811 }
Chuck Lever505bbe62016-06-29 13:52:54 -0400812 spin_unlock(&buf->rb_recovery_lock);
813}
814
815void
816rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
817{
818 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
819 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
820
821 spin_lock(&buf->rb_recovery_lock);
822 list_add(&mw->mw_list, &buf->rb_stale_mrs);
823 spin_unlock(&buf->rb_recovery_lock);
824
825 schedule_delayed_work(&buf->rb_recovery_worker, 0);
826}
827
Chuck Levere2ac2362016-06-29 13:54:00 -0400828static void
829rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
830{
831 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
832 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
833 unsigned int count;
834 LIST_HEAD(free);
835 LIST_HEAD(all);
836
837 for (count = 0; count < 32; count++) {
838 struct rpcrdma_mw *mw;
839 int rc;
840
841 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
842 if (!mw)
843 break;
844
845 rc = ia->ri_ops->ro_init_mr(ia, mw);
846 if (rc) {
847 kfree(mw);
848 break;
849 }
850
851 mw->mw_xprt = r_xprt;
852
853 list_add(&mw->mw_list, &free);
854 list_add(&mw->mw_all, &all);
855 }
856
857 spin_lock(&buf->rb_mwlock);
858 list_splice(&free, &buf->rb_mws);
859 list_splice(&all, &buf->rb_all);
860 r_xprt->rx_stats.mrs_allocated += count;
861 spin_unlock(&buf->rb_mwlock);
862
863 dprintk("RPC: %s: created %u MRs\n", __func__, count);
864}
865
866static void
867rpcrdma_mr_refresh_worker(struct work_struct *work)
868{
869 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
870 rb_refresh_worker.work);
871 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
872 rx_buf);
873
874 rpcrdma_create_mrs(r_xprt);
875}
876
Chuck Leverf531a5d2015-10-24 17:27:43 -0400877struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500878rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
879{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400880 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500881 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500882
Chuck Lever85275c82015-01-21 11:04:16 -0500883 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500884 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500885 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500886
Chuck Leverf531a5d2015-10-24 17:27:43 -0400887 INIT_LIST_HEAD(&req->rl_free);
888 spin_lock(&buffer->rb_reqslock);
889 list_add(&req->rl_all, &buffer->rb_allreqs);
890 spin_unlock(&buffer->rb_reqslock);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500891 req->rl_cqe.done = rpcrdma_wc_send;
Chuck Lever13924022015-01-21 11:03:52 -0500892 req->rl_buffer = &r_xprt->rx_buf;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400893 INIT_LIST_HEAD(&req->rl_registered);
Chuck Lever90aab602016-09-15 10:56:43 -0400894 req->rl_send_wr.next = NULL;
895 req->rl_send_wr.wr_cqe = &req->rl_cqe;
Chuck Lever655fec62016-09-15 10:57:24 -0400896 req->rl_send_wr.sg_list = req->rl_send_sge;
Chuck Lever90aab602016-09-15 10:56:43 -0400897 req->rl_send_wr.opcode = IB_WR_SEND;
Chuck Lever13924022015-01-21 11:03:52 -0500898 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500899}
900
Chuck Leverf531a5d2015-10-24 17:27:43 -0400901struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -0500902rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
903{
904 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500905 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
906 struct rpcrdma_rep *rep;
907 int rc;
908
909 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500910 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500911 if (rep == NULL)
912 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500913
Chuck Lever13650c22016-09-15 10:56:26 -0400914 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
Chuck Lever99ef4db2016-09-15 10:56:10 -0400915 DMA_FROM_DEVICE, GFP_KERNEL);
Chuck Lever6b1184c2015-01-21 11:04:25 -0500916 if (IS_ERR(rep->rr_rdmabuf)) {
917 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500918 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500919 }
Chuck Lever13924022015-01-21 11:03:52 -0500920
Chuck Lever89e0d1122015-05-26 11:51:56 -0400921 rep->rr_device = ia->ri_device;
Chuck Lever1519e962016-09-15 10:57:49 -0400922 rep->rr_cqe.done = rpcrdma_wc_receive;
Chuck Leverfed171b2015-05-26 11:51:37 -0400923 rep->rr_rxprt = r_xprt;
Chuck Lever496b77a2016-09-15 10:57:57 -0400924 INIT_WORK(&rep->rr_work, rpcrdma_reply_handler);
Chuck Lever6ea8e712016-09-15 10:56:51 -0400925 rep->rr_recv_wr.next = NULL;
926 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
927 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
928 rep->rr_recv_wr.num_sge = 1;
Chuck Lever13924022015-01-21 11:03:52 -0500929 return rep;
930
931out_free:
932 kfree(rep);
933out:
934 return ERR_PTR(rc);
935}
936
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400937int
Chuck Leverac920d02015-01-21 11:03:44 -0500938rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400939{
Chuck Leverac920d02015-01-21 11:03:44 -0500940 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400941 int i, rc;
942
Chuck Lever1e465fd2015-10-24 17:27:02 -0400943 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -0400944 buf->rb_bc_srv_max_requests = 0;
Chuck Lever23826c72016-03-04 11:28:27 -0500945 atomic_set(&buf->rb_credits, 1);
Chuck Levere2ac2362016-06-29 13:54:00 -0400946 spin_lock_init(&buf->rb_mwlock);
Chuck Lever505bbe62016-06-29 13:52:54 -0400947 spin_lock_init(&buf->rb_lock);
948 spin_lock_init(&buf->rb_recovery_lock);
Chuck Levere2ac2362016-06-29 13:54:00 -0400949 INIT_LIST_HEAD(&buf->rb_mws);
950 INIT_LIST_HEAD(&buf->rb_all);
Chuck Lever505bbe62016-06-29 13:52:54 -0400951 INIT_LIST_HEAD(&buf->rb_stale_mrs);
Chuck Levere2ac2362016-06-29 13:54:00 -0400952 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
953 rpcrdma_mr_refresh_worker);
Chuck Lever505bbe62016-06-29 13:52:54 -0400954 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
955 rpcrdma_mr_recovery_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400956
Chuck Levere2ac2362016-06-29 13:54:00 -0400957 rpcrdma_create_mrs(r_xprt);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400958
Chuck Lever1e465fd2015-10-24 17:27:02 -0400959 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400960 INIT_LIST_HEAD(&buf->rb_allreqs);
961 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400962 for (i = 0; i < buf->rb_max_requests; i++) {
963 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400964
Chuck Lever13924022015-01-21 11:03:52 -0500965 req = rpcrdma_create_req(r_xprt);
966 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400967 dprintk("RPC: %s: request buffer %d alloc"
968 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500969 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400970 goto out;
971 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400972 req->rl_backchannel = false;
Chuck Lever1e465fd2015-10-24 17:27:02 -0400973 list_add(&req->rl_free, &buf->rb_send_bufs);
974 }
975
976 INIT_LIST_HEAD(&buf->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -0400977 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
Chuck Lever1e465fd2015-10-24 17:27:02 -0400978 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400979
Chuck Lever13924022015-01-21 11:03:52 -0500980 rep = rpcrdma_create_rep(r_xprt);
981 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400982 dprintk("RPC: %s: reply buffer %d alloc failed\n",
983 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500984 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400985 goto out;
986 }
Chuck Lever1e465fd2015-10-24 17:27:02 -0400987 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400988 }
Chuck Lever13924022015-01-21 11:03:52 -0500989
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400990 return 0;
991out:
992 rpcrdma_buffer_destroy(buf);
993 return rc;
994}
995
Chuck Lever1e465fd2015-10-24 17:27:02 -0400996static struct rpcrdma_req *
997rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
998{
999 struct rpcrdma_req *req;
1000
1001 req = list_first_entry(&buf->rb_send_bufs,
1002 struct rpcrdma_req, rl_free);
1003 list_del(&req->rl_free);
1004 return req;
1005}
1006
1007static struct rpcrdma_rep *
1008rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1009{
1010 struct rpcrdma_rep *rep;
1011
1012 rep = list_first_entry(&buf->rb_recv_bufs,
1013 struct rpcrdma_rep, rr_list);
1014 list_del(&rep->rr_list);
1015 return rep;
1016}
1017
Chuck Lever2e845222014-07-29 17:25:38 -04001018static void
Chuck Lever13650c22016-09-15 10:56:26 -04001019rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
Chuck Lever13924022015-01-21 11:03:52 -05001020{
Chuck Lever13650c22016-09-15 10:56:26 -04001021 rpcrdma_free_regbuf(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001022 kfree(rep);
1023}
1024
Chuck Leverf531a5d2015-10-24 17:27:43 -04001025void
Chuck Lever13650c22016-09-15 10:56:26 -04001026rpcrdma_destroy_req(struct rpcrdma_req *req)
Chuck Lever13924022015-01-21 11:03:52 -05001027{
Chuck Lever13650c22016-09-15 10:56:26 -04001028 rpcrdma_free_regbuf(req->rl_recvbuf);
1029 rpcrdma_free_regbuf(req->rl_sendbuf);
1030 rpcrdma_free_regbuf(req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001031 kfree(req);
1032}
1033
Chuck Levere2ac2362016-06-29 13:54:00 -04001034static void
1035rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1036{
1037 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1038 rx_buf);
1039 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1040 struct rpcrdma_mw *mw;
1041 unsigned int count;
1042
1043 count = 0;
1044 spin_lock(&buf->rb_mwlock);
1045 while (!list_empty(&buf->rb_all)) {
1046 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1047 list_del(&mw->mw_all);
1048
1049 spin_unlock(&buf->rb_mwlock);
1050 ia->ri_ops->ro_release_mr(mw);
1051 count++;
1052 spin_lock(&buf->rb_mwlock);
1053 }
1054 spin_unlock(&buf->rb_mwlock);
1055 r_xprt->rx_stats.mrs_allocated = 0;
1056
1057 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1058}
1059
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001060void
1061rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1062{
Chuck Lever505bbe62016-06-29 13:52:54 -04001063 cancel_delayed_work_sync(&buf->rb_recovery_worker);
1064
Chuck Lever1e465fd2015-10-24 17:27:02 -04001065 while (!list_empty(&buf->rb_recv_bufs)) {
1066 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001067
Chuck Lever1e465fd2015-10-24 17:27:02 -04001068 rep = rpcrdma_buffer_get_rep_locked(buf);
Chuck Lever13650c22016-09-15 10:56:26 -04001069 rpcrdma_destroy_rep(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001070 }
Chuck Lever05c97462016-09-06 11:22:58 -04001071 buf->rb_send_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001072
Chuck Leverf531a5d2015-10-24 17:27:43 -04001073 spin_lock(&buf->rb_reqslock);
1074 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001075 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001076
Chuck Leverf531a5d2015-10-24 17:27:43 -04001077 req = list_first_entry(&buf->rb_allreqs,
1078 struct rpcrdma_req, rl_all);
1079 list_del(&req->rl_all);
1080
1081 spin_unlock(&buf->rb_reqslock);
Chuck Lever13650c22016-09-15 10:56:26 -04001082 rpcrdma_destroy_req(req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001083 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001084 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001085 spin_unlock(&buf->rb_reqslock);
Chuck Lever05c97462016-09-06 11:22:58 -04001086 buf->rb_recv_count = 0;
Chuck Lever9f9d8022014-07-29 17:24:45 -04001087
Chuck Levere2ac2362016-06-29 13:54:00 -04001088 rpcrdma_destroy_mrs(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001089}
1090
Chuck Lever346aa662015-05-26 11:52:06 -04001091struct rpcrdma_mw *
1092rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001093{
Chuck Lever346aa662015-05-26 11:52:06 -04001094 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1095 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001096
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001097 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001098 if (!list_empty(&buf->rb_mws)) {
1099 mw = list_first_entry(&buf->rb_mws,
1100 struct rpcrdma_mw, mw_list);
1101 list_del_init(&mw->mw_list);
Chuck Leverc2922c02014-07-29 17:24:36 -04001102 }
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001103 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001104
1105 if (!mw)
Chuck Levere2ac2362016-06-29 13:54:00 -04001106 goto out_nomws;
Chuck Lever346aa662015-05-26 11:52:06 -04001107 return mw;
Chuck Levere2ac2362016-06-29 13:54:00 -04001108
1109out_nomws:
1110 dprintk("RPC: %s: no MWs available\n", __func__);
1111 schedule_delayed_work(&buf->rb_refresh_worker, 0);
1112
1113 /* Allow the reply handler and refresh worker to run */
1114 cond_resched();
1115
1116 return NULL;
Chuck Leverc2922c02014-07-29 17:24:36 -04001117}
1118
Chuck Lever346aa662015-05-26 11:52:06 -04001119void
1120rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001121{
Chuck Lever346aa662015-05-26 11:52:06 -04001122 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001123
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001124 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001125 list_add_tail(&mw->mw_list, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001126 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001127}
1128
Chuck Lever05c97462016-09-06 11:22:58 -04001129static struct rpcrdma_rep *
1130rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1131{
1132 /* If an RPC previously completed without a reply (say, a
1133 * credential problem or a soft timeout occurs) then hold off
1134 * on supplying more Receive buffers until the number of new
1135 * pending RPCs catches up to the number of posted Receives.
1136 */
1137 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1138 return NULL;
1139
1140 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1141 return NULL;
1142 buffers->rb_recv_count++;
1143 return rpcrdma_buffer_get_rep_locked(buffers);
1144}
1145
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001146/*
1147 * Get a set of request/reply buffers.
Chuck Lever78d506e2016-09-06 11:22:49 -04001148 *
1149 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001150 */
1151struct rpcrdma_req *
1152rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1153{
1154 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001155
Chuck Levera5b027e2015-10-24 17:27:27 -04001156 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001157 if (list_empty(&buffers->rb_send_bufs))
1158 goto out_reqbuf;
Chuck Lever05c97462016-09-06 11:22:58 -04001159 buffers->rb_send_count++;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001160 req = rpcrdma_buffer_get_req_locked(buffers);
Chuck Lever05c97462016-09-06 11:22:58 -04001161 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001162 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001163 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001164
Chuck Lever1e465fd2015-10-24 17:27:02 -04001165out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001166 spin_unlock(&buffers->rb_lock);
Chuck Lever78d506e2016-09-06 11:22:49 -04001167 pr_warn("RPC: %s: out of request buffers\n", __func__);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001168 return NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001169}
1170
1171/*
1172 * Put request/reply buffers back into pool.
1173 * Pre-decrement counter/array index.
1174 */
1175void
1176rpcrdma_buffer_put(struct rpcrdma_req *req)
1177{
1178 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001179 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001180
Chuck Lever90aab602016-09-15 10:56:43 -04001181 req->rl_send_wr.num_sge = 0;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001182 req->rl_reply = NULL;
1183
Chuck Levera5b027e2015-10-24 17:27:27 -04001184 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001185 buffers->rb_send_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001186 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001187 if (rep) {
1188 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001189 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Lever05c97462016-09-06 11:22:58 -04001190 }
Chuck Levera5b027e2015-10-24 17:27:27 -04001191 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001192}
1193
1194/*
1195 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001196 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001197 */
1198void
1199rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1200{
1201 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001202
Chuck Levera5b027e2015-10-24 17:27:27 -04001203 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001204 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001205 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001206}
1207
1208/*
1209 * Put reply buffers back into pool when not attached to
1210 * request. This happens in error conditions.
1211 */
1212void
1213rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1214{
Chuck Leverfed171b2015-05-26 11:51:37 -04001215 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001216
Chuck Levera5b027e2015-10-24 17:27:27 -04001217 spin_lock(&buffers->rb_lock);
Chuck Lever05c97462016-09-06 11:22:58 -04001218 buffers->rb_recv_count--;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001219 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001220 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001221}
1222
Chuck Lever9128c3e2015-01-21 11:04:00 -05001223/**
Chuck Lever99ef4db2016-09-15 10:56:10 -04001224 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
Chuck Lever9128c3e2015-01-21 11:04:00 -05001225 * @size: size of buffer to be allocated, in bytes
Chuck Lever99ef4db2016-09-15 10:56:10 -04001226 * @direction: direction of data movement
Chuck Lever9128c3e2015-01-21 11:04:00 -05001227 * @flags: GFP flags
1228 *
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001229 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1230 * can be persistently DMA-mapped for I/O.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001231 *
1232 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
Chuck Lever99ef4db2016-09-15 10:56:10 -04001233 * receiving the payload of RDMA RECV operations. During Long Calls
1234 * or Replies they may be registered externally via ro_map.
Chuck Lever9128c3e2015-01-21 11:04:00 -05001235 */
1236struct rpcrdma_regbuf *
Chuck Lever13650c22016-09-15 10:56:26 -04001237rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1238 gfp_t flags)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001239{
1240 struct rpcrdma_regbuf *rb;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001241
Chuck Lever9128c3e2015-01-21 11:04:00 -05001242 rb = kmalloc(sizeof(*rb) + size, flags);
1243 if (rb == NULL)
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001244 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001245
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001246 rb->rg_device = NULL;
Chuck Lever99ef4db2016-09-15 10:56:10 -04001247 rb->rg_direction = direction;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001248 rb->rg_iov.length = size;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001249
1250 return rb;
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001251}
Chuck Lever9128c3e2015-01-21 11:04:00 -05001252
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001253/**
1254 * __rpcrdma_map_regbuf - DMA-map a regbuf
1255 * @ia: controlling rpcrdma_ia
1256 * @rb: regbuf to be mapped
1257 */
1258bool
1259__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1260{
1261 if (rb->rg_direction == DMA_NONE)
1262 return false;
1263
1264 rb->rg_iov.addr = ib_dma_map_single(ia->ri_device,
1265 (void *)rb->rg_base,
1266 rdmab_length(rb),
1267 rb->rg_direction);
1268 if (ib_dma_mapping_error(ia->ri_device, rdmab_addr(rb)))
1269 return false;
1270
1271 rb->rg_device = ia->ri_device;
1272 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1273 return true;
1274}
1275
1276static void
1277rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1278{
1279 if (!rpcrdma_regbuf_is_mapped(rb))
1280 return;
1281
1282 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1283 rdmab_length(rb), rb->rg_direction);
1284 rb->rg_device = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001285}
1286
1287/**
1288 * rpcrdma_free_regbuf - deregister and free registered buffer
Chuck Lever9128c3e2015-01-21 11:04:00 -05001289 * @rb: regbuf to be deregistered and freed
1290 */
1291void
Chuck Lever13650c22016-09-15 10:56:26 -04001292rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
Chuck Lever9128c3e2015-01-21 11:04:00 -05001293{
Chuck Levere531dca2015-08-03 13:03:20 -04001294 if (!rb)
1295 return;
1296
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001297 rpcrdma_dma_unmap_regbuf(rb);
Chuck Levere531dca2015-08-03 13:03:20 -04001298 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001299}
1300
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001301/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001302 * Prepost any receive buffer, then post send.
1303 *
1304 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1305 */
1306int
1307rpcrdma_ep_post(struct rpcrdma_ia *ia,
1308 struct rpcrdma_ep *ep,
1309 struct rpcrdma_req *req)
1310{
Chuck Lever90aab602016-09-15 10:56:43 -04001311 struct ib_send_wr *send_wr = &req->rl_send_wr;
1312 struct ib_send_wr *send_wr_fail;
Chuck Lever655fec62016-09-15 10:57:24 -04001313 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001314
Chuck Lever90aab602016-09-15 10:56:43 -04001315 if (req->rl_reply) {
1316 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001317 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001318 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001319 req->rl_reply = NULL;
1320 }
1321
Chuck Leverb3221d62015-08-03 13:03:39 -04001322 dprintk("RPC: %s: posting %d s/g entries\n",
Chuck Lever90aab602016-09-15 10:56:43 -04001323 __func__, send_wr->num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001324
Chuck Lever8d38de62016-11-29 10:52:16 -05001325 rpcrdma_set_signaled(ep, send_wr);
Chuck Lever90aab602016-09-15 10:56:43 -04001326 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001327 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001328 goto out_postsend_err;
1329 return 0;
1330
1331out_postsend_err:
1332 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1333 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001334}
1335
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001336int
1337rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001338 struct rpcrdma_rep *rep)
1339{
Chuck Lever6ea8e712016-09-15 10:56:51 -04001340 struct ib_recv_wr *recv_wr_fail;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001341 int rc;
1342
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001343 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1344 goto out_map;
Chuck Lever6ea8e712016-09-15 10:56:51 -04001345 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001346 if (rc)
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001347 goto out_postrecv;
1348 return 0;
1349
Chuck Lever54cbd6b2016-09-15 10:56:18 -04001350out_map:
1351 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1352 return -EIO;
1353
Chuck Lever7a89f9c2016-06-29 13:53:43 -04001354out_postrecv:
1355 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1356 return -ENOTCONN;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001357}
Chuck Lever43e95982014-07-29 17:23:34 -04001358
Chuck Leverf531a5d2015-10-24 17:27:43 -04001359/**
1360 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1361 * @r_xprt: transport associated with these backchannel resources
1362 * @min_reqs: minimum number of incoming requests expected
1363 *
1364 * Returns zero if all requested buffers were posted, or a negative errno.
1365 */
1366int
1367rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1368{
1369 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1370 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001371 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001372 int rc;
1373
1374 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001375 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001376 if (list_empty(&buffers->rb_recv_bufs))
1377 goto out_reqbuf;
1378 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001379 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001380
Chuck Leverb1573802016-09-15 10:56:35 -04001381 rc = rpcrdma_ep_post_recv(ia, rep);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001382 if (rc)
1383 goto out_rc;
1384 }
1385
1386 return 0;
1387
1388out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001389 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001390 pr_warn("%s: no extra receive buffers\n", __func__);
1391 return -ENOMEM;
1392
1393out_rc:
1394 rpcrdma_recv_buffer_put(rep);
1395 return rc;
1396}