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