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