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