xprtrdma: Clean up XDR decoding in rpcrdma_update_granted_credits()
[linux-2.6-block.git] / net / sunrpc / xprtrdma / verbs.c
CommitLineData
f58851e6 1/*
c56c65fb
TT
2 * 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.
f58851e6
TT
38 */
39
c56c65fb
TT
40/*
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
a6b7a407 50#include <linux/interrupt.h>
5a0e3ad6 51#include <linux/slab.h>
eba8ff66 52#include <linux/prefetch.h>
0dd39cae 53#include <linux/sunrpc/addr.h>
05c97466 54#include <linux/sunrpc/svc_rdma.h>
65866f82 55#include <asm/bitops.h>
56a6bd15 56
0a90487b 57#include <rdma/ib_cm.h>
c56c65fb 58
f58851e6
TT
59#include "xprt_rdma.h"
60
c56c65fb
TT
61/*
62 * Globals/Macros
63 */
64
f895b252 65#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
c56c65fb
TT
66# define RPCDBG_FACILITY RPCDBG_TRANS
67#endif
68
69/*
70 * internal functions
71 */
a9b0e381 72static void rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt);
bebd0318
CL
73static void rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf);
74static void rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb);
c56c65fb 75
7d7fa9b5 76static struct workqueue_struct *rpcrdma_receive_wq __read_mostly;
c56c65fb 77
fe97b47c
CL
78int
79rpcrdma_alloc_wq(void)
c56c65fb 80{
fe97b47c 81 struct workqueue_struct *recv_wq;
c56c65fb 82
fe97b47c
CL
83 recv_wq = alloc_workqueue("xprtrdma_receive",
84 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI,
85 0);
86 if (!recv_wq)
87 return -ENOMEM;
c56c65fb 88
fe97b47c
CL
89 rpcrdma_receive_wq = recv_wq;
90 return 0;
c56c65fb
TT
91}
92
fe97b47c
CL
93void
94rpcrdma_destroy_wq(void)
f1a03b76 95{
fe97b47c 96 struct workqueue_struct *wq;
f1a03b76 97
fe97b47c
CL
98 if (rpcrdma_receive_wq) {
99 wq = rpcrdma_receive_wq;
100 rpcrdma_receive_wq = NULL;
101 destroy_workqueue(wq);
102 }
f1a03b76
CL
103}
104
c56c65fb
TT
105static void
106rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
107{
108 struct rpcrdma_ep *ep = context;
109
2f6922ca
CL
110 pr_err("rpcrdma: %s on device %s ep %p\n",
111 ib_event_msg(event->event), event->device->name, context);
112
c56c65fb
TT
113 if (ep->rep_connected == 1) {
114 ep->rep_connected = -EIO;
afadc468 115 rpcrdma_conn_func(ep);
c56c65fb
TT
116 wake_up_all(&ep->rep_connect_wait);
117 }
118}
119
2fa8f88d
CL
120/**
121 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
122 * @cq: completion queue (ignored)
123 * @wc: completed WR
124 *
fc664485
CL
125 */
126static void
2fa8f88d 127rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
fc664485 128{
2fa8f88d
CL
129 /* WARNING: Only wr_cqe and status are reliable at this point */
130 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
131 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
132 ib_wc_status_msg(wc->status),
133 wc->status, wc->vendor_err);
fc664485 134}
c56c65fb 135
23826c7a
CL
136/* Perform basic sanity checking to avoid using garbage
137 * to update the credit grant value.
138 */
139static void
140rpcrdma_update_granted_credits(struct rpcrdma_rep *rep)
141{
23826c7a 142 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
c1bcb68e 143 __be32 *p = rep->rr_rdmabuf->rg_base;
23826c7a
CL
144 u32 credits;
145
c1bcb68e 146 credits = be32_to_cpup(p + 2);
23826c7a
CL
147 if (credits == 0)
148 credits = 1; /* don't deadlock */
149 else if (credits > buffer->rb_max_requests)
150 credits = buffer->rb_max_requests;
151
152 atomic_set(&buffer->rb_credits, credits);
153}
154
552bf225 155/**
1519e969 156 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
552bf225
CL
157 * @cq: completion queue (ignored)
158 * @wc: completed WR
159 *
160 */
fc664485 161static void
1519e969 162rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
fc664485 163{
552bf225
CL
164 struct ib_cqe *cqe = wc->wr_cqe;
165 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
166 rr_cqe);
fc664485 167
8502427c
CL
168 /* WARNING: Only wr_id and status are reliable at this point */
169 if (wc->status != IB_WC_SUCCESS)
170 goto out_fail;
fc664485 171
8502427c 172 /* status == SUCCESS means all fields in wc are trustworthy */
8502427c
CL
173 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
174 __func__, rep, wc->byte_len);
175
96f8778f 176 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len);
c8b920bb
CL
177 rep->rr_wc_flags = wc->wc_flags;
178 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
179
91a10c52 180 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
6b1184cd 181 rdmab_addr(rep->rr_rdmabuf),
e2a67190 182 wc->byte_len, DMA_FROM_DEVICE);
23826c7a 183
e2a67190
CL
184 if (wc->byte_len >= RPCRDMA_HDRLEN_ERR)
185 rpcrdma_update_granted_credits(rep);
fc664485
CL
186
187out_schedule:
fe97b47c 188 queue_work(rpcrdma_receive_wq, &rep->rr_work);
8502427c 189 return;
fe97b47c 190
8502427c
CL
191out_fail:
192 if (wc->status != IB_WC_WR_FLUSH_ERR)
552bf225
CL
193 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
194 ib_wc_status_msg(wc->status),
195 wc->status, wc->vendor_err);
e2a67190 196 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, 0);
8502427c 197 goto out_schedule;
fc664485
CL
198}
199
87cfb9a0
CL
200static void
201rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
202 struct rdma_conn_param *param)
203{
204 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
205 const struct rpcrdma_connect_private *pmsg = param->private_data;
206 unsigned int rsize, wsize;
207
c8b920bb
CL
208 /* Default settings for RPC-over-RDMA Version One */
209 r_xprt->rx_ia.ri_reminv_expected = false;
b5f0afbe 210 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
87cfb9a0
CL
211 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
212 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
213
214 if (pmsg &&
215 pmsg->cp_magic == rpcrdma_cmp_magic &&
216 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
c8b920bb 217 r_xprt->rx_ia.ri_reminv_expected = true;
c95a3c6b 218 r_xprt->rx_ia.ri_implicit_roundup = true;
87cfb9a0
CL
219 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
220 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
221 }
222
223 if (rsize < cdata->inline_rsize)
224 cdata->inline_rsize = rsize;
225 if (wsize < cdata->inline_wsize)
226 cdata->inline_wsize = wsize;
6d6bf72d
CL
227 dprintk("RPC: %s: max send %u, max recv %u\n",
228 __func__, cdata->inline_wsize, cdata->inline_rsize);
87cfb9a0
CL
229 rpcrdma_set_max_header_sizes(r_xprt);
230}
231
c56c65fb
TT
232static int
233rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
234{
235 struct rpcrdma_xprt *xprt = id->context;
236 struct rpcrdma_ia *ia = &xprt->rx_ia;
237 struct rpcrdma_ep *ep = &xprt->rx_ep;
f895b252 238#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
0dd39cae 239 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
ff0db049 240#endif
c56c65fb
TT
241 int connstate = 0;
242
243 switch (event->event) {
244 case RDMA_CM_EVENT_ADDR_RESOLVED:
245 case RDMA_CM_EVENT_ROUTE_RESOLVED:
5675add3 246 ia->ri_async_rc = 0;
c56c65fb
TT
247 complete(&ia->ri_done);
248 break;
249 case RDMA_CM_EVENT_ADDR_ERROR:
250 ia->ri_async_rc = -EHOSTUNREACH;
251 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
252 __func__, ep);
253 complete(&ia->ri_done);
254 break;
255 case RDMA_CM_EVENT_ROUTE_ERROR:
256 ia->ri_async_rc = -ENETUNREACH;
257 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
258 __func__, ep);
259 complete(&ia->ri_done);
260 break;
bebd0318
CL
261 case RDMA_CM_EVENT_DEVICE_REMOVAL:
262#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
173b8f49
CL
263 pr_info("rpcrdma: removing device %s for %pIS:%u\n",
264 ia->ri_device->name,
bebd0318
CL
265 sap, rpc_get_port(sap));
266#endif
267 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
268 ep->rep_connected = -ENODEV;
269 xprt_force_disconnect(&xprt->rx_xprt);
270 wait_for_completion(&ia->ri_remove_done);
271
272 ia->ri_id = NULL;
273 ia->ri_pd = NULL;
274 ia->ri_device = NULL;
275 /* Return 1 to ensure the core destroys the id. */
276 return 1;
c56c65fb
TT
277 case RDMA_CM_EVENT_ESTABLISHED:
278 connstate = 1;
87cfb9a0 279 rpcrdma_update_connect_private(xprt, &event->param.conn);
c56c65fb
TT
280 goto connected;
281 case RDMA_CM_EVENT_CONNECT_ERROR:
282 connstate = -ENOTCONN;
283 goto connected;
284 case RDMA_CM_EVENT_UNREACHABLE:
285 connstate = -ENETDOWN;
286 goto connected;
287 case RDMA_CM_EVENT_REJECTED:
173b8f49
CL
288 dprintk("rpcrdma: connection to %pIS:%u rejected: %s\n",
289 sap, rpc_get_port(sap),
0a90487b 290 rdma_reject_msg(id, event->status));
c56c65fb 291 connstate = -ECONNREFUSED;
0a90487b
CL
292 if (event->status == IB_CM_REJ_STALE_CONN)
293 connstate = -EAGAIN;
c56c65fb
TT
294 goto connected;
295 case RDMA_CM_EVENT_DISCONNECTED:
296 connstate = -ECONNABORTED;
c56c65fb 297connected:
23826c7a 298 atomic_set(&xprt->rx_buf.rb_credits, 1);
c56c65fb 299 ep->rep_connected = connstate;
afadc468 300 rpcrdma_conn_func(ep);
c56c65fb 301 wake_up_all(&ep->rep_connect_wait);
8079fb78 302 /*FALLTHROUGH*/
c56c65fb 303 default:
173b8f49
CL
304 dprintk("RPC: %s: %pIS:%u on %s/%s (ep 0x%p): %s\n",
305 __func__, sap, rpc_get_port(sap),
306 ia->ri_device->name, ia->ri_ops->ro_displayname,
307 ep, rdma_event_msg(event->event));
c56c65fb
TT
308 break;
309 }
310
311 return 0;
312}
313
314static struct rdma_cm_id *
315rpcrdma_create_id(struct rpcrdma_xprt *xprt,
316 struct rpcrdma_ia *ia, struct sockaddr *addr)
317{
109b88ab 318 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
c56c65fb
TT
319 struct rdma_cm_id *id;
320 int rc;
321
1a954051 322 init_completion(&ia->ri_done);
bebd0318 323 init_completion(&ia->ri_remove_done);
1a954051 324
fa20105e
GS
325 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
326 IB_QPT_RC);
c56c65fb
TT
327 if (IS_ERR(id)) {
328 rc = PTR_ERR(id);
329 dprintk("RPC: %s: rdma_create_id() failed %i\n",
330 __func__, rc);
331 return id;
332 }
333
5675add3 334 ia->ri_async_rc = -ETIMEDOUT;
c56c65fb
TT
335 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
336 if (rc) {
337 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
338 __func__, rc);
339 goto out;
340 }
109b88ab
CL
341 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
342 if (rc < 0) {
343 dprintk("RPC: %s: wait() exited: %i\n",
344 __func__, rc);
345 goto out;
346 }
d0f36c46 347
c56c65fb
TT
348 rc = ia->ri_async_rc;
349 if (rc)
350 goto out;
351
5675add3 352 ia->ri_async_rc = -ETIMEDOUT;
c56c65fb
TT
353 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
354 if (rc) {
355 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
356 __func__, rc);
56a6bd15 357 goto out;
c56c65fb 358 }
109b88ab
CL
359 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
360 if (rc < 0) {
361 dprintk("RPC: %s: wait() exited: %i\n",
362 __func__, rc);
56a6bd15 363 goto out;
109b88ab 364 }
c56c65fb
TT
365 rc = ia->ri_async_rc;
366 if (rc)
56a6bd15 367 goto out;
c56c65fb
TT
368
369 return id;
56a6bd15 370
c56c65fb
TT
371out:
372 rdma_destroy_id(id);
373 return ERR_PTR(rc);
374}
375
c56c65fb
TT
376/*
377 * Exported functions.
378 */
379
fff09594
CL
380/**
381 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
382 * @xprt: controlling transport
383 * @addr: IP address of remote peer
384 *
385 * Returns 0 on success, negative errno if an appropriate
386 * Interface Adapter could not be found and opened.
c56c65fb
TT
387 */
388int
fff09594 389rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr)
c56c65fb 390{
c56c65fb 391 struct rpcrdma_ia *ia = &xprt->rx_ia;
d1ed857e
CL
392 int rc;
393
c56c65fb
TT
394 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
395 if (IS_ERR(ia->ri_id)) {
396 rc = PTR_ERR(ia->ri_id);
fff09594 397 goto out_err;
c56c65fb 398 }
89e0d112 399 ia->ri_device = ia->ri_id->device;
c56c65fb 400
ed082d36 401 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
c56c65fb
TT
402 if (IS_ERR(ia->ri_pd)) {
403 rc = PTR_ERR(ia->ri_pd);
b54054ca 404 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
fff09594 405 goto out_err;
c56c65fb
TT
406 }
407
fff09594 408 switch (xprt_rdma_memreg_strategy) {
3197d309 409 case RPCRDMA_FRMR:
b54054ca
CL
410 if (frwr_is_supported(ia)) {
411 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
412 break;
413 }
414 /*FALLTHROUGH*/
bd7ed1d1 415 case RPCRDMA_MTHCAFMR:
b54054ca
CL
416 if (fmr_is_supported(ia)) {
417 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
418 break;
419 }
420 /*FALLTHROUGH*/
bd7ed1d1 421 default:
fff09594
CL
422 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
423 ia->ri_device->name, xprt_rdma_memreg_strategy);
b54054ca 424 rc = -EINVAL;
fff09594 425 goto out_err;
c56c65fb
TT
426 }
427
c56c65fb 428 return 0;
5ae711a2 429
fff09594
CL
430out_err:
431 rpcrdma_ia_close(ia);
c56c65fb
TT
432 return rc;
433}
434
bebd0318
CL
435/**
436 * rpcrdma_ia_remove - Handle device driver unload
437 * @ia: interface adapter being removed
438 *
439 * Divest transport H/W resources associated with this adapter,
440 * but allow it to be restored later.
441 */
442void
443rpcrdma_ia_remove(struct rpcrdma_ia *ia)
444{
445 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
446 rx_ia);
447 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
448 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
449 struct rpcrdma_req *req;
450 struct rpcrdma_rep *rep;
451
452 cancel_delayed_work_sync(&buf->rb_refresh_worker);
453
454 /* This is similar to rpcrdma_ep_destroy, but:
455 * - Don't cancel the connect worker.
456 * - Don't call rpcrdma_ep_disconnect, which waits
457 * for another conn upcall, which will deadlock.
458 * - rdma_disconnect is unneeded, the underlying
459 * connection is already gone.
460 */
461 if (ia->ri_id->qp) {
462 ib_drain_qp(ia->ri_id->qp);
463 rdma_destroy_qp(ia->ri_id);
464 ia->ri_id->qp = NULL;
465 }
466 ib_free_cq(ep->rep_attr.recv_cq);
467 ib_free_cq(ep->rep_attr.send_cq);
468
469 /* The ULP is responsible for ensuring all DMA
470 * mappings and MRs are gone.
471 */
472 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
473 rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
474 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
475 rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
476 rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
477 rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
478 }
479 rpcrdma_destroy_mrs(buf);
480
481 /* Allow waiters to continue */
482 complete(&ia->ri_remove_done);
483}
484
fff09594
CL
485/**
486 * rpcrdma_ia_close - Clean up/close an IA.
487 * @ia: interface adapter to close
488 *
c56c65fb
TT
489 */
490void
491rpcrdma_ia_close(struct rpcrdma_ia *ia)
492{
c56c65fb 493 dprintk("RPC: %s: entering\n", __func__);
fee08caf
TT
494 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
495 if (ia->ri_id->qp)
496 rdma_destroy_qp(ia->ri_id);
56a6bd15 497 rdma_destroy_id(ia->ri_id);
fee08caf 498 }
fff09594
CL
499 ia->ri_id = NULL;
500 ia->ri_device = NULL;
6d44698d
CL
501
502 /* If the pd is still busy, xprtrdma missed freeing a resource */
503 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
7dd78647 504 ib_dealloc_pd(ia->ri_pd);
fff09594 505 ia->ri_pd = NULL;
c56c65fb
TT
506}
507
508/*
509 * Create unconnected endpoint.
510 */
511int
512rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
16f906d6 513 struct rpcrdma_create_data_internal *cdata)
c56c65fb 514{
87cfb9a0 515 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
16f906d6 516 unsigned int max_qp_wr, max_sge;
fc664485 517 struct ib_cq *sendcq, *recvcq;
2fa8f88d 518 int rc;
c56c65fb 519
eed50879
CL
520 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
521 RPCRDMA_MAX_SEND_SGES);
16f906d6
CL
522 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
523 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
b3221d6a
CL
524 return -ENOMEM;
525 }
16f906d6 526 ia->ri_max_send_sges = max_sge - RPCRDMA_MIN_SEND_SGES;
b3221d6a 527
e3e45b1b 528 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
124fa17d
CL
529 dprintk("RPC: %s: insufficient wqe's available\n",
530 __func__);
531 return -ENOMEM;
532 }
550d7502 533 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
124fa17d 534
c56c65fb 535 /* check provider's send/recv wr limits */
124fa17d
CL
536 if (cdata->max_requests > max_qp_wr)
537 cdata->max_requests = max_qp_wr;
c56c65fb
TT
538
539 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
540 ep->rep_attr.qp_context = ep;
c56c65fb
TT
541 ep->rep_attr.srq = NULL;
542 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
124fa17d 543 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
550d7502 544 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
3968cb58
CL
545 rc = ia->ri_ops->ro_open(ia, ep, cdata);
546 if (rc)
547 return rc;
c56c65fb 548 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
124fa17d 549 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
550d7502 550 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
16f906d6 551 ep->rep_attr.cap.max_send_sge = max_sge;
c56c65fb
TT
552 ep->rep_attr.cap.max_recv_sge = 1;
553 ep->rep_attr.cap.max_inline_data = 0;
554 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
555 ep->rep_attr.qp_type = IB_QPT_RC;
556 ep->rep_attr.port_num = ~0;
557
558 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
559 "iovs: send %d recv %d\n",
560 __func__,
561 ep->rep_attr.cap.max_send_wr,
562 ep->rep_attr.cap.max_recv_wr,
563 ep->rep_attr.cap.max_send_sge,
564 ep->rep_attr.cap.max_recv_sge);
565
566 /* set trigger for requesting send completion */
fc664485 567 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
26ae9d1c
CL
568 if (ep->rep_cqinit <= 2)
569 ep->rep_cqinit = 0; /* always signal? */
8d38de65 570 rpcrdma_init_cqcount(ep, 0);
c56c65fb 571 init_waitqueue_head(&ep->rep_connect_wait);
254f91e2 572 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
c56c65fb 573
2fa8f88d
CL
574 sendcq = ib_alloc_cq(ia->ri_device, NULL,
575 ep->rep_attr.cap.max_send_wr + 1,
576 0, IB_POLL_SOFTIRQ);
fc664485
CL
577 if (IS_ERR(sendcq)) {
578 rc = PTR_ERR(sendcq);
579 dprintk("RPC: %s: failed to create send CQ: %i\n",
c56c65fb
TT
580 __func__, rc);
581 goto out1;
582 }
583
552bf225
CL
584 recvcq = ib_alloc_cq(ia->ri_device, NULL,
585 ep->rep_attr.cap.max_recv_wr + 1,
586 0, IB_POLL_SOFTIRQ);
fc664485
CL
587 if (IS_ERR(recvcq)) {
588 rc = PTR_ERR(recvcq);
589 dprintk("RPC: %s: failed to create recv CQ: %i\n",
590 __func__, rc);
591 goto out2;
592 }
593
fc664485
CL
594 ep->rep_attr.send_cq = sendcq;
595 ep->rep_attr.recv_cq = recvcq;
c56c65fb
TT
596
597 /* Initialize cma parameters */
b2dde94b 598 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
c56c65fb 599
87cfb9a0
CL
600 /* Prepare RDMA-CM private message */
601 pmsg->cp_magic = rpcrdma_cmp_magic;
602 pmsg->cp_version = RPCRDMA_CMP_VERSION;
c8b920bb 603 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
87cfb9a0
CL
604 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
605 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
606 ep->rep_remote_cma.private_data = pmsg;
607 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
c56c65fb
TT
608
609 /* Client offers RDMA Read but does not initiate */
b334eaab 610 ep->rep_remote_cma.initiator_depth = 0;
e3e45b1b 611 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
b334eaab
TT
612 ep->rep_remote_cma.responder_resources = 32;
613 else
7bc7972c 614 ep->rep_remote_cma.responder_resources =
e3e45b1b 615 ia->ri_device->attrs.max_qp_rd_atom;
c56c65fb 616
b2dde94b
CL
617 /* Limit transport retries so client can detect server
618 * GID changes quickly. RPC layer handles re-establishing
619 * transport connection and retransmission.
620 */
621 ep->rep_remote_cma.retry_count = 6;
622
623 /* RPC-over-RDMA handles its own flow control. In addition,
624 * make all RNR NAKs visible so we know that RPC-over-RDMA
625 * flow control is working correctly (no NAKs should be seen).
626 */
c56c65fb
TT
627 ep->rep_remote_cma.flow_control = 0;
628 ep->rep_remote_cma.rnr_retry_count = 0;
629
630 return 0;
631
632out2:
2fa8f88d 633 ib_free_cq(sendcq);
c56c65fb
TT
634out1:
635 return rc;
636}
637
638/*
639 * rpcrdma_ep_destroy
640 *
641 * Disconnect and destroy endpoint. After this, the only
642 * valid operations on the ep are to free it (if dynamically
643 * allocated) or re-create it.
c56c65fb 644 */
7f1d5419 645void
c56c65fb
TT
646rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
647{
c56c65fb
TT
648 dprintk("RPC: %s: entering, connected is %d\n",
649 __func__, ep->rep_connected);
650
254f91e2
CL
651 cancel_delayed_work_sync(&ep->rep_connect_worker);
652
72c02173 653 if (ia->ri_id->qp) {
550d7502 654 rpcrdma_ep_disconnect(ep, ia);
fee08caf
TT
655 rdma_destroy_qp(ia->ri_id);
656 ia->ri_id->qp = NULL;
c56c65fb
TT
657 }
658
552bf225 659 ib_free_cq(ep->rep_attr.recv_cq);
2fa8f88d 660 ib_free_cq(ep->rep_attr.send_cq);
c56c65fb
TT
661}
662
a9b0e381
CL
663/* Re-establish a connection after a device removal event.
664 * Unlike a normal reconnection, a fresh PD and a new set
665 * of MRs and buffers is needed.
666 */
667static int
668rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
669 struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
670{
671 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
672 int rc, err;
673
674 pr_info("%s: r_xprt = %p\n", __func__, r_xprt);
675
676 rc = -EHOSTUNREACH;
677 if (rpcrdma_ia_open(r_xprt, sap))
678 goto out1;
679
680 rc = -ENOMEM;
681 err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
682 if (err) {
683 pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
684 goto out2;
685 }
686
687 rc = -ENETUNREACH;
688 err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
689 if (err) {
690 pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
691 goto out3;
692 }
693
694 rpcrdma_create_mrs(r_xprt);
695 return 0;
696
697out3:
698 rpcrdma_ep_destroy(ep, ia);
699out2:
700 rpcrdma_ia_close(ia);
701out1:
702 return rc;
703}
704
1890896b
CL
705static int
706rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
707 struct rpcrdma_ia *ia)
708{
709 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
710 struct rdma_cm_id *id, *old;
711 int err, rc;
712
713 dprintk("RPC: %s: reconnecting...\n", __func__);
714
715 rpcrdma_ep_disconnect(ep, ia);
716
717 rc = -EHOSTUNREACH;
718 id = rpcrdma_create_id(r_xprt, ia, sap);
719 if (IS_ERR(id))
720 goto out;
721
722 /* As long as the new ID points to the same device as the
723 * old ID, we can reuse the transport's existing PD and all
724 * previously allocated MRs. Also, the same device means
725 * the transport's previous DMA mappings are still valid.
726 *
727 * This is a sanity check only. There should be no way these
728 * point to two different devices here.
729 */
730 old = id;
731 rc = -ENETUNREACH;
732 if (ia->ri_device != id->device) {
733 pr_err("rpcrdma: can't reconnect on different device!\n");
734 goto out_destroy;
735 }
736
737 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
738 if (err) {
739 dprintk("RPC: %s: rdma_create_qp returned %d\n",
740 __func__, err);
741 goto out_destroy;
742 }
743
744 /* Atomically replace the transport's ID and QP. */
745 rc = 0;
746 old = ia->ri_id;
747 ia->ri_id = id;
748 rdma_destroy_qp(old);
749
750out_destroy:
56a6bd15 751 rdma_destroy_id(old);
1890896b
CL
752out:
753 return rc;
754}
755
c56c65fb
TT
756/*
757 * Connect unconnected endpoint.
758 */
759int
760rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
761{
0a90487b
CL
762 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
763 rx_ia);
0a90487b 764 unsigned int extras;
1890896b 765 int rc;
c56c65fb 766
c56c65fb 767retry:
1890896b
CL
768 switch (ep->rep_connected) {
769 case 0:
ec62f40d
CL
770 dprintk("RPC: %s: connecting...\n", __func__);
771 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
772 if (rc) {
773 dprintk("RPC: %s: rdma_create_qp failed %i\n",
774 __func__, rc);
1890896b
CL
775 rc = -ENETUNREACH;
776 goto out_noupdate;
ec62f40d 777 }
1890896b 778 break;
a9b0e381
CL
779 case -ENODEV:
780 rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
781 if (rc)
782 goto out_noupdate;
783 break;
1890896b
CL
784 default:
785 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
786 if (rc)
787 goto out;
c56c65fb
TT
788 }
789
c56c65fb
TT
790 ep->rep_connected = 0;
791
792 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
793 if (rc) {
794 dprintk("RPC: %s: rdma_connect() failed with %i\n",
795 __func__, rc);
796 goto out;
797 }
798
c56c65fb 799 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
c56c65fb 800 if (ep->rep_connected <= 0) {
0a90487b 801 if (ep->rep_connected == -EAGAIN)
c56c65fb
TT
802 goto retry;
803 rc = ep->rep_connected;
0a90487b 804 goto out;
c56c65fb
TT
805 }
806
0a90487b
CL
807 dprintk("RPC: %s: connected\n", __func__);
808 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
809 if (extras)
810 rpcrdma_ep_post_extra_recv(r_xprt, extras);
811
c56c65fb
TT
812out:
813 if (rc)
814 ep->rep_connected = rc;
1890896b
CL
815
816out_noupdate:
c56c65fb
TT
817 return rc;
818}
819
820/*
821 * rpcrdma_ep_disconnect
822 *
823 * This is separate from destroy to facilitate the ability
824 * to reconnect without recreating the endpoint.
825 *
826 * This call is not reentrant, and must not be made in parallel
827 * on the same endpoint.
828 */
282191cb 829void
c56c65fb
TT
830rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
831{
832 int rc;
833
c56c65fb
TT
834 rc = rdma_disconnect(ia->ri_id);
835 if (!rc) {
836 /* returns without wait if not connected */
837 wait_event_interruptible(ep->rep_connect_wait,
838 ep->rep_connected != 1);
839 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
840 (ep->rep_connected == 1) ? "still " : "dis");
841 } else {
842 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
843 ep->rep_connected = rc;
844 }
550d7502
CL
845
846 ib_drain_qp(ia->ri_id->qp);
c56c65fb
TT
847}
848
505bbe64
CL
849static void
850rpcrdma_mr_recovery_worker(struct work_struct *work)
851{
852 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
853 rb_recovery_worker.work);
854 struct rpcrdma_mw *mw;
855
856 spin_lock(&buf->rb_recovery_lock);
857 while (!list_empty(&buf->rb_stale_mrs)) {
9a5c63e9 858 mw = rpcrdma_pop_mw(&buf->rb_stale_mrs);
505bbe64
CL
859 spin_unlock(&buf->rb_recovery_lock);
860
861 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
862 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
863
864 spin_lock(&buf->rb_recovery_lock);
53d78523 865 }
505bbe64
CL
866 spin_unlock(&buf->rb_recovery_lock);
867}
868
869void
870rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
871{
872 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
873 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
874
875 spin_lock(&buf->rb_recovery_lock);
9a5c63e9 876 rpcrdma_push_mw(mw, &buf->rb_stale_mrs);
505bbe64
CL
877 spin_unlock(&buf->rb_recovery_lock);
878
879 schedule_delayed_work(&buf->rb_recovery_worker, 0);
880}
881
e2ac236c
CL
882static void
883rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
884{
885 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
886 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
887 unsigned int count;
888 LIST_HEAD(free);
889 LIST_HEAD(all);
890
891 for (count = 0; count < 32; count++) {
892 struct rpcrdma_mw *mw;
893 int rc;
894
895 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
896 if (!mw)
897 break;
898
899 rc = ia->ri_ops->ro_init_mr(ia, mw);
900 if (rc) {
901 kfree(mw);
902 break;
903 }
904
905 mw->mw_xprt = r_xprt;
906
907 list_add(&mw->mw_list, &free);
908 list_add(&mw->mw_all, &all);
909 }
910
911 spin_lock(&buf->rb_mwlock);
912 list_splice(&free, &buf->rb_mws);
913 list_splice(&all, &buf->rb_all);
914 r_xprt->rx_stats.mrs_allocated += count;
915 spin_unlock(&buf->rb_mwlock);
916
917 dprintk("RPC: %s: created %u MRs\n", __func__, count);
918}
919
920static void
921rpcrdma_mr_refresh_worker(struct work_struct *work)
922{
923 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
924 rb_refresh_worker.work);
925 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
926 rx_buf);
927
928 rpcrdma_create_mrs(r_xprt);
929}
930
f531a5db 931struct rpcrdma_req *
1392402c
CL
932rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
933{
f531a5db 934 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
1392402c 935 struct rpcrdma_req *req;
1392402c 936
85275c87 937 req = kzalloc(sizeof(*req), GFP_KERNEL);
1392402c 938 if (req == NULL)
85275c87 939 return ERR_PTR(-ENOMEM);
1392402c 940
f531a5db
CL
941 spin_lock(&buffer->rb_reqslock);
942 list_add(&req->rl_all, &buffer->rb_allreqs);
943 spin_unlock(&buffer->rb_reqslock);
2fa8f88d 944 req->rl_cqe.done = rpcrdma_wc_send;
1392402c 945 req->rl_buffer = &r_xprt->rx_buf;
9d6b0409 946 INIT_LIST_HEAD(&req->rl_registered);
90aab602
CL
947 req->rl_send_wr.next = NULL;
948 req->rl_send_wr.wr_cqe = &req->rl_cqe;
655fec69 949 req->rl_send_wr.sg_list = req->rl_send_sge;
90aab602 950 req->rl_send_wr.opcode = IB_WR_SEND;
1392402c 951 return req;
1392402c
CL
952}
953
f531a5db 954struct rpcrdma_rep *
1392402c
CL
955rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
956{
957 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
1392402c
CL
958 struct rpcrdma_rep *rep;
959 int rc;
960
961 rc = -ENOMEM;
6b1184cd 962 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1392402c
CL
963 if (rep == NULL)
964 goto out;
1392402c 965
13650c23 966 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
99ef4db3 967 DMA_FROM_DEVICE, GFP_KERNEL);
6b1184cd
CL
968 if (IS_ERR(rep->rr_rdmabuf)) {
969 rc = PTR_ERR(rep->rr_rdmabuf);
1392402c 970 goto out_free;
6b1184cd 971 }
96f8778f
CL
972 xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base,
973 rdmab_length(rep->rr_rdmabuf));
1392402c 974
1519e969 975 rep->rr_cqe.done = rpcrdma_wc_receive;
fed171b3 976 rep->rr_rxprt = r_xprt;
496b77a5 977 INIT_WORK(&rep->rr_work, rpcrdma_reply_handler);
6ea8e711
CL
978 rep->rr_recv_wr.next = NULL;
979 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
980 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
981 rep->rr_recv_wr.num_sge = 1;
1392402c
CL
982 return rep;
983
984out_free:
985 kfree(rep);
986out:
987 return ERR_PTR(rc);
988}
989
c56c65fb 990int
ac920d04 991rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
c56c65fb 992{
ac920d04 993 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
c56c65fb
TT
994 int i, rc;
995
1e465fd4 996 buf->rb_max_requests = r_xprt->rx_data.max_requests;
f531a5db 997 buf->rb_bc_srv_max_requests = 0;
23826c7a 998 atomic_set(&buf->rb_credits, 1);
e2ac236c 999 spin_lock_init(&buf->rb_mwlock);
505bbe64
CL
1000 spin_lock_init(&buf->rb_lock);
1001 spin_lock_init(&buf->rb_recovery_lock);
e2ac236c
CL
1002 INIT_LIST_HEAD(&buf->rb_mws);
1003 INIT_LIST_HEAD(&buf->rb_all);
431af645 1004 INIT_LIST_HEAD(&buf->rb_pending);
505bbe64 1005 INIT_LIST_HEAD(&buf->rb_stale_mrs);
e2ac236c
CL
1006 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1007 rpcrdma_mr_refresh_worker);
505bbe64
CL
1008 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
1009 rpcrdma_mr_recovery_worker);
c56c65fb 1010
e2ac236c 1011 rpcrdma_create_mrs(r_xprt);
c56c65fb 1012
1e465fd4 1013 INIT_LIST_HEAD(&buf->rb_send_bufs);
f531a5db
CL
1014 INIT_LIST_HEAD(&buf->rb_allreqs);
1015 spin_lock_init(&buf->rb_reqslock);
c56c65fb
TT
1016 for (i = 0; i < buf->rb_max_requests; i++) {
1017 struct rpcrdma_req *req;
c56c65fb 1018
1392402c
CL
1019 req = rpcrdma_create_req(r_xprt);
1020 if (IS_ERR(req)) {
c56c65fb
TT
1021 dprintk("RPC: %s: request buffer %d alloc"
1022 " failed\n", __func__, i);
1392402c 1023 rc = PTR_ERR(req);
c56c65fb
TT
1024 goto out;
1025 }
f531a5db 1026 req->rl_backchannel = false;
a80d66c9 1027 list_add(&req->rl_list, &buf->rb_send_bufs);
1e465fd4
CL
1028 }
1029
1030 INIT_LIST_HEAD(&buf->rb_recv_bufs);
05c97466 1031 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
1e465fd4 1032 struct rpcrdma_rep *rep;
c56c65fb 1033
1392402c
CL
1034 rep = rpcrdma_create_rep(r_xprt);
1035 if (IS_ERR(rep)) {
c56c65fb
TT
1036 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1037 __func__, i);
1392402c 1038 rc = PTR_ERR(rep);
c56c65fb
TT
1039 goto out;
1040 }
1e465fd4 1041 list_add(&rep->rr_list, &buf->rb_recv_bufs);
c56c65fb 1042 }
1392402c 1043
c56c65fb
TT
1044 return 0;
1045out:
1046 rpcrdma_buffer_destroy(buf);
1047 return rc;
1048}
1049
1e465fd4
CL
1050static struct rpcrdma_req *
1051rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1052{
1053 struct rpcrdma_req *req;
1054
1055 req = list_first_entry(&buf->rb_send_bufs,
a80d66c9 1056 struct rpcrdma_req, rl_list);
431af645 1057 list_del_init(&req->rl_list);
1e465fd4
CL
1058 return req;
1059}
1060
1061static struct rpcrdma_rep *
1062rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1063{
1064 struct rpcrdma_rep *rep;
1065
1066 rep = list_first_entry(&buf->rb_recv_bufs,
1067 struct rpcrdma_rep, rr_list);
1068 list_del(&rep->rr_list);
1069 return rep;
1070}
1071
1392402c 1072static void
13650c23 1073rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
1392402c 1074{
13650c23 1075 rpcrdma_free_regbuf(rep->rr_rdmabuf);
1392402c
CL
1076 kfree(rep);
1077}
1078
f531a5db 1079void
13650c23 1080rpcrdma_destroy_req(struct rpcrdma_req *req)
1392402c 1081{
13650c23
CL
1082 rpcrdma_free_regbuf(req->rl_recvbuf);
1083 rpcrdma_free_regbuf(req->rl_sendbuf);
1084 rpcrdma_free_regbuf(req->rl_rdmabuf);
1392402c
CL
1085 kfree(req);
1086}
1087
e2ac236c
CL
1088static void
1089rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1090{
1091 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1092 rx_buf);
1093 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1094 struct rpcrdma_mw *mw;
1095 unsigned int count;
1096
1097 count = 0;
1098 spin_lock(&buf->rb_mwlock);
1099 while (!list_empty(&buf->rb_all)) {
1100 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1101 list_del(&mw->mw_all);
1102
1103 spin_unlock(&buf->rb_mwlock);
1104 ia->ri_ops->ro_release_mr(mw);
1105 count++;
1106 spin_lock(&buf->rb_mwlock);
1107 }
1108 spin_unlock(&buf->rb_mwlock);
1109 r_xprt->rx_stats.mrs_allocated = 0;
1110
1111 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1112}
1113
c56c65fb
TT
1114void
1115rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1116{
505bbe64 1117 cancel_delayed_work_sync(&buf->rb_recovery_worker);
9378b274 1118 cancel_delayed_work_sync(&buf->rb_refresh_worker);
505bbe64 1119
1e465fd4
CL
1120 while (!list_empty(&buf->rb_recv_bufs)) {
1121 struct rpcrdma_rep *rep;
c56c65fb 1122
1e465fd4 1123 rep = rpcrdma_buffer_get_rep_locked(buf);
13650c23 1124 rpcrdma_destroy_rep(rep);
c56c65fb 1125 }
05c97466 1126 buf->rb_send_count = 0;
c56c65fb 1127
f531a5db
CL
1128 spin_lock(&buf->rb_reqslock);
1129 while (!list_empty(&buf->rb_allreqs)) {
1e465fd4 1130 struct rpcrdma_req *req;
4034ba04 1131
f531a5db
CL
1132 req = list_first_entry(&buf->rb_allreqs,
1133 struct rpcrdma_req, rl_all);
1134 list_del(&req->rl_all);
1135
1136 spin_unlock(&buf->rb_reqslock);
13650c23 1137 rpcrdma_destroy_req(req);
f531a5db 1138 spin_lock(&buf->rb_reqslock);
1e465fd4 1139 }
f531a5db 1140 spin_unlock(&buf->rb_reqslock);
05c97466 1141 buf->rb_recv_count = 0;
4034ba04 1142
e2ac236c 1143 rpcrdma_destroy_mrs(buf);
c56c65fb
TT
1144}
1145
346aa66b
CL
1146struct rpcrdma_mw *
1147rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
c2922c02 1148{
346aa66b
CL
1149 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1150 struct rpcrdma_mw *mw = NULL;
346aa66b 1151
58d1dcf5 1152 spin_lock(&buf->rb_mwlock);
9a5c63e9
CL
1153 if (!list_empty(&buf->rb_mws))
1154 mw = rpcrdma_pop_mw(&buf->rb_mws);
58d1dcf5 1155 spin_unlock(&buf->rb_mwlock);
346aa66b
CL
1156
1157 if (!mw)
e2ac236c 1158 goto out_nomws;
4b196dc6 1159 mw->mw_flags = 0;
346aa66b 1160 return mw;
e2ac236c
CL
1161
1162out_nomws:
1163 dprintk("RPC: %s: no MWs available\n", __func__);
bebd0318
CL
1164 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1165 schedule_delayed_work(&buf->rb_refresh_worker, 0);
e2ac236c
CL
1166
1167 /* Allow the reply handler and refresh worker to run */
1168 cond_resched();
1169
1170 return NULL;
c2922c02
CL
1171}
1172
346aa66b
CL
1173void
1174rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
c2922c02 1175{
346aa66b 1176 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
c2922c02 1177
58d1dcf5 1178 spin_lock(&buf->rb_mwlock);
9a5c63e9 1179 rpcrdma_push_mw(mw, &buf->rb_mws);
58d1dcf5 1180 spin_unlock(&buf->rb_mwlock);
c2922c02
CL
1181}
1182
05c97466
CL
1183static struct rpcrdma_rep *
1184rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1185{
1186 /* If an RPC previously completed without a reply (say, a
1187 * credential problem or a soft timeout occurs) then hold off
1188 * on supplying more Receive buffers until the number of new
1189 * pending RPCs catches up to the number of posted Receives.
1190 */
1191 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1192 return NULL;
1193
1194 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1195 return NULL;
1196 buffers->rb_recv_count++;
1197 return rpcrdma_buffer_get_rep_locked(buffers);
1198}
1199
c56c65fb
TT
1200/*
1201 * Get a set of request/reply buffers.
78d506e1
CL
1202 *
1203 * Reply buffer (if available) is attached to send buffer upon return.
c56c65fb
TT
1204 */
1205struct rpcrdma_req *
1206rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1207{
1208 struct rpcrdma_req *req;
c14d86e5 1209
a5b027e1 1210 spin_lock(&buffers->rb_lock);
1e465fd4
CL
1211 if (list_empty(&buffers->rb_send_bufs))
1212 goto out_reqbuf;
05c97466 1213 buffers->rb_send_count++;
1e465fd4 1214 req = rpcrdma_buffer_get_req_locked(buffers);
05c97466 1215 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
a5b027e1 1216 spin_unlock(&buffers->rb_lock);
1e465fd4 1217 return req;
ddb6bebc 1218
1e465fd4 1219out_reqbuf:
a5b027e1 1220 spin_unlock(&buffers->rb_lock);
78d506e1 1221 pr_warn("RPC: %s: out of request buffers\n", __func__);
1e465fd4 1222 return NULL;
c56c65fb
TT
1223}
1224
1225/*
1226 * Put request/reply buffers back into pool.
1227 * Pre-decrement counter/array index.
1228 */
1229void
1230rpcrdma_buffer_put(struct rpcrdma_req *req)
1231{
1232 struct rpcrdma_buffer *buffers = req->rl_buffer;
1e465fd4 1233 struct rpcrdma_rep *rep = req->rl_reply;
c56c65fb 1234
90aab602 1235 req->rl_send_wr.num_sge = 0;
1e465fd4
CL
1236 req->rl_reply = NULL;
1237
a5b027e1 1238 spin_lock(&buffers->rb_lock);
05c97466 1239 buffers->rb_send_count--;
a80d66c9 1240 list_add_tail(&req->rl_list, &buffers->rb_send_bufs);
05c97466
CL
1241 if (rep) {
1242 buffers->rb_recv_count--;
1e465fd4 1243 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
05c97466 1244 }
a5b027e1 1245 spin_unlock(&buffers->rb_lock);
c56c65fb
TT
1246}
1247
1248/*
1249 * Recover reply buffers from pool.
1e465fd4 1250 * This happens when recovering from disconnect.
c56c65fb
TT
1251 */
1252void
1253rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1254{
1255 struct rpcrdma_buffer *buffers = req->rl_buffer;
c56c65fb 1256
a5b027e1 1257 spin_lock(&buffers->rb_lock);
05c97466 1258 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
a5b027e1 1259 spin_unlock(&buffers->rb_lock);
c56c65fb
TT
1260}
1261
1262/*
1263 * Put reply buffers back into pool when not attached to
b45ccfd2 1264 * request. This happens in error conditions.
c56c65fb
TT
1265 */
1266void
1267rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1268{
fed171b3 1269 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
c56c65fb 1270
a5b027e1 1271 spin_lock(&buffers->rb_lock);
05c97466 1272 buffers->rb_recv_count--;
1e465fd4 1273 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
a5b027e1 1274 spin_unlock(&buffers->rb_lock);
c56c65fb
TT
1275}
1276
9128c3e7 1277/**
99ef4db3 1278 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
9128c3e7 1279 * @size: size of buffer to be allocated, in bytes
99ef4db3 1280 * @direction: direction of data movement
9128c3e7
CL
1281 * @flags: GFP flags
1282 *
54cbd6b0
CL
1283 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1284 * can be persistently DMA-mapped for I/O.
9128c3e7
CL
1285 *
1286 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
99ef4db3
CL
1287 * receiving the payload of RDMA RECV operations. During Long Calls
1288 * or Replies they may be registered externally via ro_map.
9128c3e7
CL
1289 */
1290struct rpcrdma_regbuf *
13650c23
CL
1291rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1292 gfp_t flags)
9128c3e7
CL
1293{
1294 struct rpcrdma_regbuf *rb;
9128c3e7 1295
9128c3e7
CL
1296 rb = kmalloc(sizeof(*rb) + size, flags);
1297 if (rb == NULL)
54cbd6b0 1298 return ERR_PTR(-ENOMEM);
9128c3e7 1299
54cbd6b0 1300 rb->rg_device = NULL;
99ef4db3 1301 rb->rg_direction = direction;
54cbd6b0 1302 rb->rg_iov.length = size;
9128c3e7
CL
1303
1304 return rb;
54cbd6b0 1305}
9128c3e7 1306
54cbd6b0
CL
1307/**
1308 * __rpcrdma_map_regbuf - DMA-map a regbuf
1309 * @ia: controlling rpcrdma_ia
1310 * @rb: regbuf to be mapped
1311 */
1312bool
1313__rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1314{
91a10c52
CL
1315 struct ib_device *device = ia->ri_device;
1316
54cbd6b0
CL
1317 if (rb->rg_direction == DMA_NONE)
1318 return false;
1319
91a10c52 1320 rb->rg_iov.addr = ib_dma_map_single(device,
54cbd6b0
CL
1321 (void *)rb->rg_base,
1322 rdmab_length(rb),
1323 rb->rg_direction);
91a10c52 1324 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
54cbd6b0
CL
1325 return false;
1326
91a10c52 1327 rb->rg_device = device;
54cbd6b0
CL
1328 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1329 return true;
1330}
1331
1332static void
1333rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1334{
1335 if (!rpcrdma_regbuf_is_mapped(rb))
1336 return;
1337
1338 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1339 rdmab_length(rb), rb->rg_direction);
1340 rb->rg_device = NULL;
9128c3e7
CL
1341}
1342
1343/**
1344 * rpcrdma_free_regbuf - deregister and free registered buffer
9128c3e7
CL
1345 * @rb: regbuf to be deregistered and freed
1346 */
1347void
13650c23 1348rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
9128c3e7 1349{
e531dcab
CL
1350 if (!rb)
1351 return;
1352
54cbd6b0 1353 rpcrdma_dma_unmap_regbuf(rb);
e531dcab 1354 kfree(rb);
9128c3e7
CL
1355}
1356
c56c65fb
TT
1357/*
1358 * Prepost any receive buffer, then post send.
1359 *
1360 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1361 */
1362int
1363rpcrdma_ep_post(struct rpcrdma_ia *ia,
1364 struct rpcrdma_ep *ep,
1365 struct rpcrdma_req *req)
1366{
90aab602
CL
1367 struct ib_send_wr *send_wr = &req->rl_send_wr;
1368 struct ib_send_wr *send_wr_fail;
655fec69 1369 int rc;
c56c65fb 1370
90aab602
CL
1371 if (req->rl_reply) {
1372 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
c56c65fb 1373 if (rc)
7a89f9c6 1374 return rc;
c56c65fb
TT
1375 req->rl_reply = NULL;
1376 }
1377
b3221d6a 1378 dprintk("RPC: %s: posting %d s/g entries\n",
90aab602 1379 __func__, send_wr->num_sge);
c56c65fb 1380
8d38de65 1381 rpcrdma_set_signaled(ep, send_wr);
90aab602 1382 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
c56c65fb 1383 if (rc)
7a89f9c6
CL
1384 goto out_postsend_err;
1385 return 0;
1386
1387out_postsend_err:
1388 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1389 return -ENOTCONN;
c56c65fb
TT
1390}
1391
c56c65fb
TT
1392int
1393rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
c56c65fb
TT
1394 struct rpcrdma_rep *rep)
1395{
6ea8e711 1396 struct ib_recv_wr *recv_wr_fail;
c56c65fb
TT
1397 int rc;
1398
54cbd6b0
CL
1399 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1400 goto out_map;
6ea8e711 1401 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
c56c65fb 1402 if (rc)
7a89f9c6
CL
1403 goto out_postrecv;
1404 return 0;
1405
54cbd6b0
CL
1406out_map:
1407 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1408 return -EIO;
1409
7a89f9c6
CL
1410out_postrecv:
1411 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1412 return -ENOTCONN;
c56c65fb 1413}
43e95988 1414
f531a5db
CL
1415/**
1416 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1417 * @r_xprt: transport associated with these backchannel resources
1418 * @min_reqs: minimum number of incoming requests expected
1419 *
1420 * Returns zero if all requested buffers were posted, or a negative errno.
1421 */
1422int
1423rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1424{
1425 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1426 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
f531a5db 1427 struct rpcrdma_rep *rep;
f531a5db
CL
1428 int rc;
1429
1430 while (count--) {
9b06688b 1431 spin_lock(&buffers->rb_lock);
f531a5db
CL
1432 if (list_empty(&buffers->rb_recv_bufs))
1433 goto out_reqbuf;
1434 rep = rpcrdma_buffer_get_rep_locked(buffers);
9b06688b 1435 spin_unlock(&buffers->rb_lock);
f531a5db 1436
b157380a 1437 rc = rpcrdma_ep_post_recv(ia, rep);
f531a5db
CL
1438 if (rc)
1439 goto out_rc;
1440 }
1441
1442 return 0;
1443
1444out_reqbuf:
9b06688b 1445 spin_unlock(&buffers->rb_lock);
f531a5db
CL
1446 pr_warn("%s: no extra receive buffers\n", __func__);
1447 return -ENOMEM;
1448
1449out_rc:
1450 rpcrdma_recv_buffer_put(rep);
1451 return rc;
1452}