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