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