blob: b044d98a1370207422d129689bb43b35766fc76f [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 Lever65866f82014-05-28 10:33:59 -040054#include <asm/bitops.h>
Devesh Sharmad0f36c42015-08-03 13:05:04 -040055#include <linux/module.h> /* try_module_get()/module_put() */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040056
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040057#include "xprt_rdma.h"
58
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040059/*
60 * Globals/Macros
61 */
62
Jeff Laytonf895b252014-11-17 16:58:04 -050063#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040064# define RPCDBG_FACILITY RPCDBG_TRANS
65#endif
66
67/*
68 * internal functions
69 */
70
Chuck Leverfe97b472015-10-24 17:27:10 -040071static struct workqueue_struct *rpcrdma_receive_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040072
Chuck Leverfe97b472015-10-24 17:27:10 -040073int
74rpcrdma_alloc_wq(void)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040075{
Chuck Leverfe97b472015-10-24 17:27:10 -040076 struct workqueue_struct *recv_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040077
Chuck Leverfe97b472015-10-24 17:27:10 -040078 recv_wq = alloc_workqueue("xprtrdma_receive",
79 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI,
80 0);
81 if (!recv_wq)
82 return -ENOMEM;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040083
Chuck Leverfe97b472015-10-24 17:27:10 -040084 rpcrdma_receive_wq = recv_wq;
85 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040086}
87
Chuck Leverfe97b472015-10-24 17:27:10 -040088void
89rpcrdma_destroy_wq(void)
Chuck Leverf1a03b72014-11-08 20:14:37 -050090{
Chuck Leverfe97b472015-10-24 17:27:10 -040091 struct workqueue_struct *wq;
Chuck Leverf1a03b72014-11-08 20:14:37 -050092
Chuck Leverfe97b472015-10-24 17:27:10 -040093 if (rpcrdma_receive_wq) {
94 wq = rpcrdma_receive_wq;
95 rpcrdma_receive_wq = NULL;
96 destroy_workqueue(wq);
97 }
Chuck Leverf1a03b72014-11-08 20:14:37 -050098}
99
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400100static void
101rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
102{
103 struct rpcrdma_ep *ep = context;
104
Chuck Lever7ff11de2014-11-08 20:15:01 -0500105 pr_err("RPC: %s: %s on device %s ep %p\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300106 __func__, ib_event_msg(event->event),
Chuck Lever7ff11de2014-11-08 20:15:01 -0500107 event->device->name, context);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400108 if (ep->rep_connected == 1) {
109 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500110 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400111 wake_up_all(&ep->rep_connect_wait);
112 }
113}
114
Chuck Lever2fa8f882016-03-04 11:28:53 -0500115/**
116 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
117 * @cq: completion queue (ignored)
118 * @wc: completed WR
119 *
Chuck Lever4220a072015-10-24 17:26:45 -0400120 */
121static void
Chuck Lever2fa8f882016-03-04 11:28:53 -0500122rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400123{
Chuck Lever2fa8f882016-03-04 11:28:53 -0500124 /* WARNING: Only wr_cqe and status are reliable at this point */
125 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
126 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
127 ib_wc_status_msg(wc->status),
128 wc->status, wc->vendor_err);
Chuck Leverfc664482014-05-28 10:33:25 -0400129}
130
131static void
Chuck Leverfe97b472015-10-24 17:27:10 -0400132rpcrdma_receive_worker(struct work_struct *work)
133{
134 struct rpcrdma_rep *rep =
135 container_of(work, struct rpcrdma_rep, rr_work);
136
137 rpcrdma_reply_handler(rep);
138}
139
Chuck Lever23826c72016-03-04 11:28:27 -0500140/* Perform basic sanity checking to avoid using garbage
141 * to update the credit grant value.
142 */
143static void
144rpcrdma_update_granted_credits(struct rpcrdma_rep *rep)
145{
146 struct rpcrdma_msg *rmsgp = rdmab_to_msg(rep->rr_rdmabuf);
147 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
148 u32 credits;
149
150 if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
151 return;
152
153 credits = be32_to_cpu(rmsgp->rm_credit);
154 if (credits == 0)
155 credits = 1; /* don't deadlock */
156 else if (credits > buffer->rb_max_requests)
157 credits = buffer->rb_max_requests;
158
159 atomic_set(&buffer->rb_credits, credits);
160}
161
Chuck Lever552bf222016-03-04 11:28:36 -0500162/**
163 * rpcrdma_receive_wc - Invoked by RDMA provider for each polled Receive WC
164 * @cq: completion queue (ignored)
165 * @wc: completed WR
166 *
167 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400168static void
Chuck Lever552bf222016-03-04 11:28:36 -0500169rpcrdma_receive_wc(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400170{
Chuck Lever552bf222016-03-04 11:28:36 -0500171 struct ib_cqe *cqe = wc->wr_cqe;
172 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
173 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400174
Chuck Lever85024272015-01-21 11:02:04 -0500175 /* WARNING: Only wr_id and status are reliable at this point */
176 if (wc->status != IB_WC_SUCCESS)
177 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400178
Chuck Lever85024272015-01-21 11:02:04 -0500179 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400180 if (wc->opcode != IB_WC_RECV)
181 return;
182
Chuck Lever85024272015-01-21 11:02:04 -0500183 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
184 __func__, rep, wc->byte_len);
185
Chuck Leverfc664482014-05-28 10:33:25 -0400186 rep->rr_len = wc->byte_len;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400187 ib_dma_sync_single_for_cpu(rep->rr_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -0500188 rdmab_addr(rep->rr_rdmabuf),
189 rep->rr_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500190
191 rpcrdma_update_granted_credits(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400192
193out_schedule:
Chuck Leverfe97b472015-10-24 17:27:10 -0400194 queue_work(rpcrdma_receive_wq, &rep->rr_work);
Chuck Lever85024272015-01-21 11:02:04 -0500195 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400196
Chuck Lever85024272015-01-21 11:02:04 -0500197out_fail:
198 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500199 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
200 ib_wc_status_msg(wc->status),
201 wc->status, wc->vendor_err);
Chuck Leverb0e178a2015-10-24 17:26:54 -0400202 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500203 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400204}
205
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400206static int
207rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
208{
209 struct rpcrdma_xprt *xprt = id->context;
210 struct rpcrdma_ia *ia = &xprt->rx_ia;
211 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500212#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400213 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800214#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500215 struct ib_qp_attr *attr = &ia->ri_qp_attr;
216 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400217 int connstate = 0;
218
219 switch (event->event) {
220 case RDMA_CM_EVENT_ADDR_RESOLVED:
221 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400222 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400223 complete(&ia->ri_done);
224 break;
225 case RDMA_CM_EVENT_ADDR_ERROR:
226 ia->ri_async_rc = -EHOSTUNREACH;
227 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
228 __func__, ep);
229 complete(&ia->ri_done);
230 break;
231 case RDMA_CM_EVENT_ROUTE_ERROR:
232 ia->ri_async_rc = -ENETUNREACH;
233 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
234 __func__, ep);
235 complete(&ia->ri_done);
236 break;
237 case RDMA_CM_EVENT_ESTABLISHED:
238 connstate = 1;
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500239 ib_query_qp(ia->ri_id->qp, attr,
240 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
241 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400242 dprintk("RPC: %s: %d responder resources"
243 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500244 __func__, attr->max_dest_rd_atomic,
245 attr->max_rd_atomic);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400246 goto connected;
247 case RDMA_CM_EVENT_CONNECT_ERROR:
248 connstate = -ENOTCONN;
249 goto connected;
250 case RDMA_CM_EVENT_UNREACHABLE:
251 connstate = -ENETDOWN;
252 goto connected;
253 case RDMA_CM_EVENT_REJECTED:
254 connstate = -ECONNREFUSED;
255 goto connected;
256 case RDMA_CM_EVENT_DISCONNECTED:
257 connstate = -ECONNABORTED;
258 goto connected;
259 case RDMA_CM_EVENT_DEVICE_REMOVAL:
260 connstate = -ENODEV;
261connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400262 dprintk("RPC: %s: %sconnected\n",
263 __func__, connstate > 0 ? "" : "dis");
Chuck Lever23826c72016-03-04 11:28:27 -0500264 atomic_set(&xprt->rx_buf.rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400265 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500266 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400267 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400268 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400269 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400270 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
271 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300272 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400273 break;
274 }
275
Jeff Laytonf895b252014-11-17 16:58:04 -0500276#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400277 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500278 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400279 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400280
Chuck Levera0ce85f2015-03-30 14:34:21 -0400281 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400282 sap, rpc_get_port(sap),
Chuck Lever89e0d1122015-05-26 11:51:56 -0400283 ia->ri_device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400284 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400285 xprt->rx_buf.rb_max_requests,
286 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
287 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400288 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
289 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400290 }
291#endif
292
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400293 return 0;
294}
295
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400296static void rpcrdma_destroy_id(struct rdma_cm_id *id)
297{
298 if (id) {
299 module_put(id->device->owner);
300 rdma_destroy_id(id);
301 }
302}
303
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400304static struct rdma_cm_id *
305rpcrdma_create_id(struct rpcrdma_xprt *xprt,
306 struct rpcrdma_ia *ia, struct sockaddr *addr)
307{
308 struct rdma_cm_id *id;
309 int rc;
310
Tom Talpey1a954052008-10-09 15:01:31 -0400311 init_completion(&ia->ri_done);
312
Guy Shapirofa201052015-10-22 15:20:10 +0300313 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
314 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400315 if (IS_ERR(id)) {
316 rc = PTR_ERR(id);
317 dprintk("RPC: %s: rdma_create_id() failed %i\n",
318 __func__, rc);
319 return id;
320 }
321
Tom Talpey5675add2008-10-09 15:01:41 -0400322 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400323 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
324 if (rc) {
325 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
326 __func__, rc);
327 goto out;
328 }
Tom Talpey5675add2008-10-09 15:01:41 -0400329 wait_for_completion_interruptible_timeout(&ia->ri_done,
330 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400331
332 /* FIXME:
333 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
334 * be pinned while there are active NFS/RDMA mounts to prevent
335 * hangs and crashes at umount time.
336 */
337 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
338 dprintk("RPC: %s: Failed to get device module\n",
339 __func__);
340 ia->ri_async_rc = -ENODEV;
341 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400342 rc = ia->ri_async_rc;
343 if (rc)
344 goto out;
345
Tom Talpey5675add2008-10-09 15:01:41 -0400346 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400347 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
348 if (rc) {
349 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
350 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400351 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400352 }
Tom Talpey5675add2008-10-09 15:01:41 -0400353 wait_for_completion_interruptible_timeout(&ia->ri_done,
354 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400355 rc = ia->ri_async_rc;
356 if (rc)
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400357 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400358
359 return id;
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400360put:
361 module_put(id->device->owner);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400362out:
363 rdma_destroy_id(id);
364 return ERR_PTR(rc);
365}
366
367/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400368 * Exported functions.
369 */
370
371/*
372 * Open and initialize an Interface Adapter.
373 * o initializes fields of struct rpcrdma_ia, including
374 * interface and provider attributes and protection zone.
375 */
376int
377rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
378{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400379 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400380 int rc;
381
382 ia->ri_dma_mr = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400383
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400384 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
385 if (IS_ERR(ia->ri_id)) {
386 rc = PTR_ERR(ia->ri_id);
387 goto out1;
388 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400389 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400390
Chuck Lever89e0d1122015-05-26 11:51:56 -0400391 ia->ri_pd = ib_alloc_pd(ia->ri_device);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400392 if (IS_ERR(ia->ri_pd)) {
393 rc = PTR_ERR(ia->ri_pd);
394 dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
395 __func__, rc);
396 goto out2;
397 }
398
Chuck Leverf10eafd2014-05-28 10:32:51 -0400399 if (memreg == RPCRDMA_FRMR) {
Or Gerlitze3e45b12015-12-18 10:59:48 +0200400 if (!(ia->ri_device->attrs.device_cap_flags &
401 IB_DEVICE_MEM_MGT_EXTENSIONS) ||
402 (ia->ri_device->attrs.max_fast_reg_page_list_len == 0)) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400403 dprintk("RPC: %s: FRMR registration "
Chuck Leverf10eafd2014-05-28 10:32:51 -0400404 "not supported by HCA\n", __func__);
405 memreg = RPCRDMA_MTHCAFMR;
Tom Talpey3197d3092008-10-09 15:00:20 -0400406 }
Chuck Leverf10eafd2014-05-28 10:32:51 -0400407 }
408 if (memreg == RPCRDMA_MTHCAFMR) {
Chuck Lever89e0d1122015-05-26 11:51:56 -0400409 if (!ia->ri_device->alloc_fmr) {
Chuck Leverf10eafd2014-05-28 10:32:51 -0400410 dprintk("RPC: %s: MTHCAFMR registration "
411 "not supported by HCA\n", __func__);
Sagi Grimbergf022fa82015-10-06 19:52:37 +0300412 rc = -EINVAL;
Chuck Leverd2310932015-08-03 13:03:09 -0400413 goto out3;
Chuck Leverf10eafd2014-05-28 10:32:51 -0400414 }
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400415 }
416
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400417 switch (memreg) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400418 case RPCRDMA_FRMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400419 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400420 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400421 case RPCRDMA_ALLPHYSICAL:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400422 ia->ri_ops = &rpcrdma_physical_memreg_ops;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400423 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400424 case RPCRDMA_MTHCAFMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400425 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400426 break;
427 default:
Chuck Levercdd9ade2014-05-28 10:33:00 -0400428 printk(KERN_ERR "RPC: Unsupported memory "
429 "registration mode: %d\n", memreg);
430 rc = -ENOMEM;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500431 goto out3;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400432 }
Chuck Levera0ce85f2015-03-30 14:34:21 -0400433 dprintk("RPC: %s: memory registration strategy is '%s'\n",
434 __func__, ia->ri_ops->ro_displayname);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400435
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400436 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500437
438out3:
439 ib_dealloc_pd(ia->ri_pd);
440 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400441out2:
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400442 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400443 ia->ri_id = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400444out1:
445 return rc;
446}
447
448/*
449 * Clean up/close an IA.
450 * o if event handles and PD have been initialized, free them.
451 * o close the IA
452 */
453void
454rpcrdma_ia_close(struct rpcrdma_ia *ia)
455{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400456 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400457 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
458 if (ia->ri_id->qp)
459 rdma_destroy_qp(ia->ri_id);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400460 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400461 ia->ri_id = NULL;
462 }
Chuck Lever6d446982015-05-26 11:51:27 -0400463
464 /* If the pd is still busy, xprtrdma missed freeing a resource */
465 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600466 ib_dealloc_pd(ia->ri_pd);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400467}
468
469/*
470 * Create unconnected endpoint.
471 */
472int
473rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
474 struct rpcrdma_create_data_internal *cdata)
475{
Chuck Leverfc664482014-05-28 10:33:25 -0400476 struct ib_cq *sendcq, *recvcq;
Chuck Lever124fa172015-10-24 17:27:51 -0400477 unsigned int max_qp_wr;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500478 int rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400479
Or Gerlitze3e45b12015-12-18 10:59:48 +0200480 if (ia->ri_device->attrs.max_sge < RPCRDMA_MAX_IOVS) {
Chuck Leverb3221d62015-08-03 13:03:39 -0400481 dprintk("RPC: %s: insufficient sge's available\n",
482 __func__);
483 return -ENOMEM;
484 }
485
Or Gerlitze3e45b12015-12-18 10:59:48 +0200486 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400487 dprintk("RPC: %s: insufficient wqe's available\n",
488 __func__);
489 return -ENOMEM;
490 }
Chuck Lever550d7502016-05-02 14:41:47 -0400491 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
Chuck Lever124fa172015-10-24 17:27:51 -0400492
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400493 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400494 if (cdata->max_requests > max_qp_wr)
495 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400496
497 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
498 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400499 ep->rep_attr.srq = NULL;
500 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400501 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400502 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
Chuck Lever3968cb52015-03-30 14:35:26 -0400503 rc = ia->ri_ops->ro_open(ia, ep, cdata);
504 if (rc)
505 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400506 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400507 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever550d7502016-05-02 14:41:47 -0400508 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
Chuck Leverb3221d62015-08-03 13:03:39 -0400509 ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_IOVS;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400510 ep->rep_attr.cap.max_recv_sge = 1;
511 ep->rep_attr.cap.max_inline_data = 0;
512 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
513 ep->rep_attr.qp_type = IB_QPT_RC;
514 ep->rep_attr.port_num = ~0;
515
516 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
517 "iovs: send %d recv %d\n",
518 __func__,
519 ep->rep_attr.cap.max_send_wr,
520 ep->rep_attr.cap.max_recv_wr,
521 ep->rep_attr.cap.max_send_sge,
522 ep->rep_attr.cap.max_recv_sge);
523
524 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400525 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500526 if (ep->rep_cqinit <= 2)
527 ep->rep_cqinit = 0; /* always signal? */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400528 INIT_CQCOUNT(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400529 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400530 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400531
Chuck Lever2fa8f882016-03-04 11:28:53 -0500532 sendcq = ib_alloc_cq(ia->ri_device, NULL,
533 ep->rep_attr.cap.max_send_wr + 1,
534 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400535 if (IS_ERR(sendcq)) {
536 rc = PTR_ERR(sendcq);
537 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400538 __func__, rc);
539 goto out1;
540 }
541
Chuck Lever552bf222016-03-04 11:28:36 -0500542 recvcq = ib_alloc_cq(ia->ri_device, NULL,
543 ep->rep_attr.cap.max_recv_wr + 1,
544 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400545 if (IS_ERR(recvcq)) {
546 rc = PTR_ERR(recvcq);
547 dprintk("RPC: %s: failed to create recv CQ: %i\n",
548 __func__, rc);
549 goto out2;
550 }
551
Chuck Leverfc664482014-05-28 10:33:25 -0400552 ep->rep_attr.send_cq = sendcq;
553 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400554
555 /* Initialize cma parameters */
Chuck Leverb2dde942016-05-02 14:43:03 -0400556 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400557
558 /* RPC/RDMA does not use private data */
559 ep->rep_remote_cma.private_data = NULL;
560 ep->rep_remote_cma.private_data_len = 0;
561
562 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400563 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200564 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400565 ep->rep_remote_cma.responder_resources = 32;
566 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500567 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200568 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400569
Chuck Leverb2dde942016-05-02 14:43:03 -0400570 /* Limit transport retries so client can detect server
571 * GID changes quickly. RPC layer handles re-establishing
572 * transport connection and retransmission.
573 */
574 ep->rep_remote_cma.retry_count = 6;
575
576 /* RPC-over-RDMA handles its own flow control. In addition,
577 * make all RNR NAKs visible so we know that RPC-over-RDMA
578 * flow control is working correctly (no NAKs should be seen).
579 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400580 ep->rep_remote_cma.flow_control = 0;
581 ep->rep_remote_cma.rnr_retry_count = 0;
582
583 return 0;
584
585out2:
Chuck Lever2fa8f882016-03-04 11:28:53 -0500586 ib_free_cq(sendcq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400587out1:
Chuck Leverd1ed8572015-08-03 13:03:30 -0400588 if (ia->ri_dma_mr)
589 ib_dereg_mr(ia->ri_dma_mr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400590 return rc;
591}
592
593/*
594 * rpcrdma_ep_destroy
595 *
596 * Disconnect and destroy endpoint. After this, the only
597 * valid operations on the ep are to free it (if dynamically
598 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400599 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400600void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400601rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
602{
603 int rc;
604
605 dprintk("RPC: %s: entering, connected is %d\n",
606 __func__, ep->rep_connected);
607
Chuck Lever254f91e2014-05-28 10:32:17 -0400608 cancel_delayed_work_sync(&ep->rep_connect_worker);
609
Steve Wise72c02172015-09-21 12:24:23 -0500610 if (ia->ri_id->qp) {
Chuck Lever550d7502016-05-02 14:41:47 -0400611 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400612 rdma_destroy_qp(ia->ri_id);
613 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400614 }
615
Chuck Lever552bf222016-03-04 11:28:36 -0500616 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500617 ib_free_cq(ep->rep_attr.send_cq);
Chuck Leverd1ed8572015-08-03 13:03:30 -0400618
619 if (ia->ri_dma_mr) {
620 rc = ib_dereg_mr(ia->ri_dma_mr);
621 dprintk("RPC: %s: ib_dereg_mr returned %i\n",
622 __func__, rc);
623 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400624}
625
626/*
627 * Connect unconnected endpoint.
628 */
629int
630rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
631{
Chuck Lever73806c82014-07-29 17:23:25 -0400632 struct rdma_cm_id *id, *old;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400633 int rc = 0;
634 int retry_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400635
Tom Talpeyc0555512008-10-10 11:32:45 -0400636 if (ep->rep_connected != 0) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400637 struct rpcrdma_xprt *xprt;
638retry:
Chuck Leverec62f402014-05-28 10:34:07 -0400639 dprintk("RPC: %s: reconnecting...\n", __func__);
Chuck Lever282191c2014-07-29 17:25:55 -0400640
641 rpcrdma_ep_disconnect(ep, ia);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400642
643 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
644 id = rpcrdma_create_id(xprt, ia,
645 (struct sockaddr *)&xprt->rx_data.addr);
646 if (IS_ERR(id)) {
Chuck Leverec62f402014-05-28 10:34:07 -0400647 rc = -EHOSTUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400648 goto out;
649 }
650 /* TEMP TEMP TEMP - fail if new device:
651 * Deregister/remarshal *all* requests!
652 * Close and recreate adapter, pd, etc!
653 * Re-determine all attributes still sane!
654 * More stuff I haven't thought of!
655 * Rrrgh!
656 */
Chuck Lever89e0d1122015-05-26 11:51:56 -0400657 if (ia->ri_device != id->device) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400658 printk("RPC: %s: can't reconnect on "
659 "different device!\n", __func__);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400660 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400661 rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400662 goto out;
663 }
664 /* END TEMP */
Chuck Leverec62f402014-05-28 10:34:07 -0400665 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
666 if (rc) {
667 dprintk("RPC: %s: rdma_create_qp failed %i\n",
668 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400669 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400670 rc = -ENETUNREACH;
671 goto out;
672 }
Chuck Lever73806c82014-07-29 17:23:25 -0400673
Chuck Lever73806c82014-07-29 17:23:25 -0400674 old = ia->ri_id;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400675 ia->ri_id = id;
Chuck Lever73806c82014-07-29 17:23:25 -0400676
677 rdma_destroy_qp(old);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400678 rpcrdma_destroy_id(old);
Chuck Leverec62f402014-05-28 10:34:07 -0400679 } else {
680 dprintk("RPC: %s: connecting...\n", __func__);
681 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
682 if (rc) {
683 dprintk("RPC: %s: rdma_create_qp failed %i\n",
684 __func__, rc);
685 /* do not update ep->rep_connected */
686 return -ENETUNREACH;
687 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400688 }
689
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400690 ep->rep_connected = 0;
691
692 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
693 if (rc) {
694 dprintk("RPC: %s: rdma_connect() failed with %i\n",
695 __func__, rc);
696 goto out;
697 }
698
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400699 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
700
701 /*
702 * Check state. A non-peer reject indicates no listener
703 * (ECONNREFUSED), which may be a transient state. All
704 * others indicate a transport condition which has already
705 * undergone a best-effort.
706 */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800707 if (ep->rep_connected == -ECONNREFUSED &&
708 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400709 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
710 goto retry;
711 }
712 if (ep->rep_connected <= 0) {
713 /* Sometimes, the only way to reliably connect to remote
714 * CMs is to use same nonzero values for ORD and IRD. */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400715 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
716 (ep->rep_remote_cma.responder_resources == 0 ||
717 ep->rep_remote_cma.initiator_depth !=
718 ep->rep_remote_cma.responder_resources)) {
719 if (ep->rep_remote_cma.responder_resources == 0)
720 ep->rep_remote_cma.responder_resources = 1;
721 ep->rep_remote_cma.initiator_depth =
722 ep->rep_remote_cma.responder_resources;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400723 goto retry;
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400724 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400725 rc = ep->rep_connected;
726 } else {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400727 struct rpcrdma_xprt *r_xprt;
728 unsigned int extras;
729
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400730 dprintk("RPC: %s: connected\n", __func__);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400731
732 r_xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
733 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
734
735 if (extras) {
736 rc = rpcrdma_ep_post_extra_recv(r_xprt, extras);
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300737 if (rc) {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400738 pr_warn("%s: rpcrdma_ep_post_extra_recv: %i\n",
739 __func__, rc);
740 rc = 0;
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300741 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400742 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400743 }
744
745out:
746 if (rc)
747 ep->rep_connected = rc;
748 return rc;
749}
750
751/*
752 * rpcrdma_ep_disconnect
753 *
754 * This is separate from destroy to facilitate the ability
755 * to reconnect without recreating the endpoint.
756 *
757 * This call is not reentrant, and must not be made in parallel
758 * on the same endpoint.
759 */
Chuck Lever282191c2014-07-29 17:25:55 -0400760void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400761rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
762{
763 int rc;
764
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400765 rc = rdma_disconnect(ia->ri_id);
766 if (!rc) {
767 /* returns without wait if not connected */
768 wait_event_interruptible(ep->rep_connect_wait,
769 ep->rep_connected != 1);
770 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
771 (ep->rep_connected == 1) ? "still " : "dis");
772 } else {
773 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
774 ep->rep_connected = rc;
775 }
Chuck Lever550d7502016-05-02 14:41:47 -0400776
777 ib_drain_qp(ia->ri_id->qp);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400778}
779
Chuck Leverf531a5d2015-10-24 17:27:43 -0400780struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500781rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
782{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400783 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500784 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500785
Chuck Lever85275c82015-01-21 11:04:16 -0500786 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500787 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500788 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500789
Chuck Leverf531a5d2015-10-24 17:27:43 -0400790 INIT_LIST_HEAD(&req->rl_free);
791 spin_lock(&buffer->rb_reqslock);
792 list_add(&req->rl_all, &buffer->rb_allreqs);
793 spin_unlock(&buffer->rb_reqslock);
Chuck Lever2fa8f882016-03-04 11:28:53 -0500794 req->rl_cqe.done = rpcrdma_wc_send;
Chuck Lever13924022015-01-21 11:03:52 -0500795 req->rl_buffer = &r_xprt->rx_buf;
796 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500797}
798
Chuck Leverf531a5d2015-10-24 17:27:43 -0400799struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -0500800rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
801{
802 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500803 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
804 struct rpcrdma_rep *rep;
805 int rc;
806
807 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500808 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500809 if (rep == NULL)
810 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500811
Chuck Lever6b1184c2015-01-21 11:04:25 -0500812 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize,
813 GFP_KERNEL);
814 if (IS_ERR(rep->rr_rdmabuf)) {
815 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500816 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500817 }
Chuck Lever13924022015-01-21 11:03:52 -0500818
Chuck Lever89e0d1122015-05-26 11:51:56 -0400819 rep->rr_device = ia->ri_device;
Chuck Lever552bf222016-03-04 11:28:36 -0500820 rep->rr_cqe.done = rpcrdma_receive_wc;
Chuck Leverfed171b2015-05-26 11:51:37 -0400821 rep->rr_rxprt = r_xprt;
Chuck Leverfe97b472015-10-24 17:27:10 -0400822 INIT_WORK(&rep->rr_work, rpcrdma_receive_worker);
Chuck Lever13924022015-01-21 11:03:52 -0500823 return rep;
824
825out_free:
826 kfree(rep);
827out:
828 return ERR_PTR(rc);
829}
830
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400831int
Chuck Leverac920d02015-01-21 11:03:44 -0500832rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400833{
Chuck Leverac920d02015-01-21 11:03:44 -0500834 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
835 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400836 int i, rc;
837
Chuck Lever1e465fd2015-10-24 17:27:02 -0400838 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -0400839 buf->rb_bc_srv_max_requests = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400840 spin_lock_init(&buf->rb_lock);
Chuck Lever23826c72016-03-04 11:28:27 -0500841 atomic_set(&buf->rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400842
Chuck Lever91e70e72015-03-30 14:34:58 -0400843 rc = ia->ri_ops->ro_init(r_xprt);
844 if (rc)
845 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400846
Chuck Lever1e465fd2015-10-24 17:27:02 -0400847 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400848 INIT_LIST_HEAD(&buf->rb_allreqs);
849 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400850 for (i = 0; i < buf->rb_max_requests; i++) {
851 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400852
Chuck Lever13924022015-01-21 11:03:52 -0500853 req = rpcrdma_create_req(r_xprt);
854 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400855 dprintk("RPC: %s: request buffer %d alloc"
856 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500857 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400858 goto out;
859 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400860 req->rl_backchannel = false;
Chuck Lever1e465fd2015-10-24 17:27:02 -0400861 list_add(&req->rl_free, &buf->rb_send_bufs);
862 }
863
864 INIT_LIST_HEAD(&buf->rb_recv_bufs);
865 for (i = 0; i < buf->rb_max_requests + 2; i++) {
866 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400867
Chuck Lever13924022015-01-21 11:03:52 -0500868 rep = rpcrdma_create_rep(r_xprt);
869 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400870 dprintk("RPC: %s: reply buffer %d alloc failed\n",
871 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500872 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400873 goto out;
874 }
Chuck Lever1e465fd2015-10-24 17:27:02 -0400875 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400876 }
Chuck Lever13924022015-01-21 11:03:52 -0500877
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400878 return 0;
879out:
880 rpcrdma_buffer_destroy(buf);
881 return rc;
882}
883
Chuck Lever1e465fd2015-10-24 17:27:02 -0400884static struct rpcrdma_req *
885rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
886{
887 struct rpcrdma_req *req;
888
889 req = list_first_entry(&buf->rb_send_bufs,
890 struct rpcrdma_req, rl_free);
891 list_del(&req->rl_free);
892 return req;
893}
894
895static struct rpcrdma_rep *
896rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
897{
898 struct rpcrdma_rep *rep;
899
900 rep = list_first_entry(&buf->rb_recv_bufs,
901 struct rpcrdma_rep, rr_list);
902 list_del(&rep->rr_list);
903 return rep;
904}
905
Chuck Lever2e845222014-07-29 17:25:38 -0400906static void
Chuck Lever13924022015-01-21 11:03:52 -0500907rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
908{
Chuck Lever6b1184c2015-01-21 11:04:25 -0500909 rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500910 kfree(rep);
911}
912
Chuck Leverf531a5d2015-10-24 17:27:43 -0400913void
Chuck Lever13924022015-01-21 11:03:52 -0500914rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
915{
Chuck Lever0ca77dc2015-01-21 11:04:08 -0500916 rpcrdma_free_regbuf(ia, req->rl_sendbuf);
Chuck Lever85275c82015-01-21 11:04:16 -0500917 rpcrdma_free_regbuf(ia, req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500918 kfree(req);
919}
920
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400921void
922rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
923{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400924 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
925
Chuck Lever1e465fd2015-10-24 17:27:02 -0400926 while (!list_empty(&buf->rb_recv_bufs)) {
927 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400928
Chuck Lever1e465fd2015-10-24 17:27:02 -0400929 rep = rpcrdma_buffer_get_rep_locked(buf);
930 rpcrdma_destroy_rep(ia, rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400931 }
932
Chuck Leverf531a5d2015-10-24 17:27:43 -0400933 spin_lock(&buf->rb_reqslock);
934 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -0400935 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -0400936
Chuck Leverf531a5d2015-10-24 17:27:43 -0400937 req = list_first_entry(&buf->rb_allreqs,
938 struct rpcrdma_req, rl_all);
939 list_del(&req->rl_all);
940
941 spin_unlock(&buf->rb_reqslock);
Chuck Lever1e465fd2015-10-24 17:27:02 -0400942 rpcrdma_destroy_req(ia, req);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400943 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -0400944 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400945 spin_unlock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -0400946
947 ia->ri_ops->ro_destroy(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400948}
949
Chuck Lever346aa662015-05-26 11:52:06 -0400950struct rpcrdma_mw *
951rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -0400952{
Chuck Lever346aa662015-05-26 11:52:06 -0400953 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
954 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -0400955
Chuck Lever58d1dcf2015-05-26 11:53:13 -0400956 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -0400957 if (!list_empty(&buf->rb_mws)) {
958 mw = list_first_entry(&buf->rb_mws,
959 struct rpcrdma_mw, mw_list);
960 list_del_init(&mw->mw_list);
Chuck Leverc2922c02014-07-29 17:24:36 -0400961 }
Chuck Lever58d1dcf2015-05-26 11:53:13 -0400962 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -0400963
964 if (!mw)
965 pr_err("RPC: %s: no MWs available\n", __func__);
966 return mw;
Chuck Leverc2922c02014-07-29 17:24:36 -0400967}
968
Chuck Lever346aa662015-05-26 11:52:06 -0400969void
970rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -0400971{
Chuck Lever346aa662015-05-26 11:52:06 -0400972 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -0400973
Chuck Lever58d1dcf2015-05-26 11:53:13 -0400974 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -0400975 list_add_tail(&mw->mw_list, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -0400976 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -0400977}
978
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400979/*
980 * Get a set of request/reply buffers.
981 *
Chuck Lever1e465fd2015-10-24 17:27:02 -0400982 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400983 */
984struct rpcrdma_req *
985rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
986{
987 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400988
Chuck Levera5b027e2015-10-24 17:27:27 -0400989 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -0400990 if (list_empty(&buffers->rb_send_bufs))
991 goto out_reqbuf;
992 req = rpcrdma_buffer_get_req_locked(buffers);
993 if (list_empty(&buffers->rb_recv_bufs))
994 goto out_repbuf;
995 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -0400996 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400997 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400998
Chuck Lever1e465fd2015-10-24 17:27:02 -0400999out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001000 spin_unlock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001001 pr_warn("RPC: %s: out of request buffers\n", __func__);
1002 return NULL;
1003out_repbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001004 spin_unlock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001005 pr_warn("RPC: %s: out of reply buffers\n", __func__);
1006 req->rl_reply = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001007 return req;
1008}
1009
1010/*
1011 * Put request/reply buffers back into pool.
1012 * Pre-decrement counter/array index.
1013 */
1014void
1015rpcrdma_buffer_put(struct rpcrdma_req *req)
1016{
1017 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001018 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001019
Chuck Lever1e465fd2015-10-24 17:27:02 -04001020 req->rl_niovs = 0;
1021 req->rl_reply = NULL;
1022
Chuck Levera5b027e2015-10-24 17:27:27 -04001023 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001024 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
1025 if (rep)
1026 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001027 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001028}
1029
1030/*
1031 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001032 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001033 */
1034void
1035rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1036{
1037 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001038
Chuck Levera5b027e2015-10-24 17:27:27 -04001039 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001040 if (!list_empty(&buffers->rb_recv_bufs))
1041 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001042 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001043}
1044
1045/*
1046 * Put reply buffers back into pool when not attached to
1047 * request. This happens in error conditions.
1048 */
1049void
1050rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1051{
Chuck Leverfed171b2015-05-26 11:51:37 -04001052 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001053
Chuck Levera5b027e2015-10-24 17:27:27 -04001054 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001055 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001056 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001057}
1058
1059/*
1060 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1061 */
1062
Chuck Leverd6547882015-03-30 14:35:44 -04001063void
1064rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg)
1065{
1066 dprintk("RPC: map_one: offset %p iova %llx len %zu\n",
1067 seg->mr_offset,
1068 (unsigned long long)seg->mr_dma, seg->mr_dmalen);
1069}
1070
Chuck Lever9128c3e2015-01-21 11:04:00 -05001071/**
1072 * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
1073 * @ia: controlling rpcrdma_ia
1074 * @size: size of buffer to be allocated, in bytes
1075 * @flags: GFP flags
1076 *
1077 * Returns pointer to private header of an area of internally
1078 * registered memory, or an ERR_PTR. The registered buffer follows
1079 * the end of the private header.
1080 *
1081 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1082 * receiving the payload of RDMA RECV operations. regbufs are not
1083 * used for RDMA READ/WRITE operations, thus are registered only for
1084 * LOCAL access.
1085 */
1086struct rpcrdma_regbuf *
1087rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
1088{
1089 struct rpcrdma_regbuf *rb;
Chuck Levere531dca2015-08-03 13:03:20 -04001090 struct ib_sge *iov;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001091
Chuck Lever9128c3e2015-01-21 11:04:00 -05001092 rb = kmalloc(sizeof(*rb) + size, flags);
1093 if (rb == NULL)
1094 goto out;
1095
Chuck Levere531dca2015-08-03 13:03:20 -04001096 iov = &rb->rg_iov;
1097 iov->addr = ib_dma_map_single(ia->ri_device,
1098 (void *)rb->rg_base, size,
1099 DMA_BIDIRECTIONAL);
1100 if (ib_dma_mapping_error(ia->ri_device, iov->addr))
Chuck Lever9128c3e2015-01-21 11:04:00 -05001101 goto out_free;
1102
Chuck Levere531dca2015-08-03 13:03:20 -04001103 iov->length = size;
Chuck Leverbb6c96d2015-09-24 10:34:21 +03001104 iov->lkey = ia->ri_pd->local_dma_lkey;
Chuck Levere531dca2015-08-03 13:03:20 -04001105 rb->rg_size = size;
1106 rb->rg_owner = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001107 return rb;
1108
1109out_free:
1110 kfree(rb);
1111out:
Chuck Levere531dca2015-08-03 13:03:20 -04001112 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001113}
1114
1115/**
1116 * rpcrdma_free_regbuf - deregister and free registered buffer
1117 * @ia: controlling rpcrdma_ia
1118 * @rb: regbuf to be deregistered and freed
1119 */
1120void
1121rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1122{
Chuck Levere531dca2015-08-03 13:03:20 -04001123 struct ib_sge *iov;
1124
1125 if (!rb)
1126 return;
1127
1128 iov = &rb->rg_iov;
1129 ib_dma_unmap_single(ia->ri_device,
1130 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1131 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001132}
1133
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001134/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001135 * Prepost any receive buffer, then post send.
1136 *
1137 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1138 */
1139int
1140rpcrdma_ep_post(struct rpcrdma_ia *ia,
1141 struct rpcrdma_ep *ep,
1142 struct rpcrdma_req *req)
1143{
Chuck Leverb3221d62015-08-03 13:03:39 -04001144 struct ib_device *device = ia->ri_device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001145 struct ib_send_wr send_wr, *send_wr_fail;
1146 struct rpcrdma_rep *rep = req->rl_reply;
Chuck Leverb3221d62015-08-03 13:03:39 -04001147 struct ib_sge *iov = req->rl_send_iov;
1148 int i, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001149
1150 if (rep) {
1151 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1152 if (rc)
1153 goto out;
1154 req->rl_reply = NULL;
1155 }
1156
1157 send_wr.next = NULL;
Chuck Lever2fa8f882016-03-04 11:28:53 -05001158 send_wr.wr_cqe = &req->rl_cqe;
Chuck Leverb3221d62015-08-03 13:03:39 -04001159 send_wr.sg_list = iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001160 send_wr.num_sge = req->rl_niovs;
1161 send_wr.opcode = IB_WR_SEND;
Chuck Leverb3221d62015-08-03 13:03:39 -04001162
1163 for (i = 0; i < send_wr.num_sge; i++)
1164 ib_dma_sync_single_for_device(device, iov[i].addr,
1165 iov[i].length, DMA_TO_DEVICE);
1166 dprintk("RPC: %s: posting %d s/g entries\n",
1167 __func__, send_wr.num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001168
1169 if (DECR_CQCOUNT(ep) > 0)
1170 send_wr.send_flags = 0;
1171 else { /* Provider must take a send completion every now and then */
1172 INIT_CQCOUNT(ep);
1173 send_wr.send_flags = IB_SEND_SIGNALED;
1174 }
1175
1176 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1177 if (rc)
1178 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
1179 rc);
1180out:
1181 return rc;
1182}
1183
1184/*
1185 * (Re)post a receive buffer.
1186 */
1187int
1188rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1189 struct rpcrdma_ep *ep,
1190 struct rpcrdma_rep *rep)
1191{
1192 struct ib_recv_wr recv_wr, *recv_wr_fail;
1193 int rc;
1194
1195 recv_wr.next = NULL;
Chuck Lever552bf222016-03-04 11:28:36 -05001196 recv_wr.wr_cqe = &rep->rr_cqe;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001197 recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001198 recv_wr.num_sge = 1;
1199
Chuck Lever89e0d1122015-05-26 11:51:56 -04001200 ib_dma_sync_single_for_cpu(ia->ri_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -05001201 rdmab_addr(rep->rr_rdmabuf),
1202 rdmab_length(rep->rr_rdmabuf),
1203 DMA_BIDIRECTIONAL);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001204
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001205 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1206
1207 if (rc)
1208 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
1209 rc);
1210 return rc;
1211}
Chuck Lever43e95982014-07-29 17:23:34 -04001212
Chuck Leverf531a5d2015-10-24 17:27:43 -04001213/**
1214 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1215 * @r_xprt: transport associated with these backchannel resources
1216 * @min_reqs: minimum number of incoming requests expected
1217 *
1218 * Returns zero if all requested buffers were posted, or a negative errno.
1219 */
1220int
1221rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1222{
1223 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1224 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1225 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
1226 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001227 int rc;
1228
1229 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001230 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001231 if (list_empty(&buffers->rb_recv_bufs))
1232 goto out_reqbuf;
1233 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001234 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001235
1236 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1237 if (rc)
1238 goto out_rc;
1239 }
1240
1241 return 0;
1242
1243out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001244 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001245 pr_warn("%s: no extra receive buffers\n", __func__);
1246 return -ENOMEM;
1247
1248out_rc:
1249 rpcrdma_recv_buffer_put(rep);
1250 return rc;
1251}