xprtrdma: Pre-allocate Work Requests for backchannel
[firefly-linux-kernel-4.4.55.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(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC);
382         if (IS_ERR(id)) {
383                 rc = PTR_ERR(id);
384                 dprintk("RPC:       %s: rdma_create_id() failed %i\n",
385                         __func__, rc);
386                 return id;
387         }
388
389         ia->ri_async_rc = -ETIMEDOUT;
390         rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
391         if (rc) {
392                 dprintk("RPC:       %s: rdma_resolve_addr() failed %i\n",
393                         __func__, rc);
394                 goto out;
395         }
396         wait_for_completion_interruptible_timeout(&ia->ri_done,
397                                 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
398
399         /* FIXME:
400          * Until xprtrdma supports DEVICE_REMOVAL, the provider must
401          * be pinned while there are active NFS/RDMA mounts to prevent
402          * hangs and crashes at umount time.
403          */
404         if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
405                 dprintk("RPC:       %s: Failed to get device module\n",
406                         __func__);
407                 ia->ri_async_rc = -ENODEV;
408         }
409         rc = ia->ri_async_rc;
410         if (rc)
411                 goto out;
412
413         ia->ri_async_rc = -ETIMEDOUT;
414         rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
415         if (rc) {
416                 dprintk("RPC:       %s: rdma_resolve_route() failed %i\n",
417                         __func__, rc);
418                 goto put;
419         }
420         wait_for_completion_interruptible_timeout(&ia->ri_done,
421                                 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
422         rc = ia->ri_async_rc;
423         if (rc)
424                 goto put;
425
426         return id;
427 put:
428         module_put(id->device->owner);
429 out:
430         rdma_destroy_id(id);
431         return ERR_PTR(rc);
432 }
433
434 /*
435  * Drain any cq, prior to teardown.
436  */
437 static void
438 rpcrdma_clean_cq(struct ib_cq *cq)
439 {
440         struct ib_wc wc;
441         int count = 0;
442
443         while (1 == ib_poll_cq(cq, 1, &wc))
444                 ++count;
445
446         if (count)
447                 dprintk("RPC:       %s: flushed %d events (last 0x%x)\n",
448                         __func__, count, wc.opcode);
449 }
450
451 /*
452  * Exported functions.
453  */
454
455 /*
456  * Open and initialize an Interface Adapter.
457  *  o initializes fields of struct rpcrdma_ia, including
458  *    interface and provider attributes and protection zone.
459  */
460 int
461 rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
462 {
463         struct rpcrdma_ia *ia = &xprt->rx_ia;
464         struct ib_device_attr *devattr = &ia->ri_devattr;
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         rc = ib_query_device(ia->ri_device, devattr);
485         if (rc) {
486                 dprintk("RPC:       %s: ib_query_device failed %d\n",
487                         __func__, rc);
488                 goto out3;
489         }
490
491         if (memreg == RPCRDMA_FRMR) {
492                 if (!(devattr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) ||
493                     (devattr->max_fast_reg_page_list_len == 0)) {
494                         dprintk("RPC:       %s: FRMR registration "
495                                 "not supported by HCA\n", __func__);
496                         memreg = RPCRDMA_MTHCAFMR;
497                 }
498         }
499         if (memreg == RPCRDMA_MTHCAFMR) {
500                 if (!ia->ri_device->alloc_fmr) {
501                         dprintk("RPC:       %s: MTHCAFMR registration "
502                                 "not supported by HCA\n", __func__);
503                         rc = -EINVAL;
504                         goto out3;
505                 }
506         }
507
508         switch (memreg) {
509         case RPCRDMA_FRMR:
510                 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
511                 break;
512         case RPCRDMA_ALLPHYSICAL:
513                 ia->ri_ops = &rpcrdma_physical_memreg_ops;
514                 break;
515         case RPCRDMA_MTHCAFMR:
516                 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
517                 break;
518         default:
519                 printk(KERN_ERR "RPC: Unsupported memory "
520                                 "registration mode: %d\n", memreg);
521                 rc = -ENOMEM;
522                 goto out3;
523         }
524         dprintk("RPC:       %s: memory registration strategy is '%s'\n",
525                 __func__, ia->ri_ops->ro_displayname);
526
527         rwlock_init(&ia->ri_qplock);
528         return 0;
529
530 out3:
531         ib_dealloc_pd(ia->ri_pd);
532         ia->ri_pd = NULL;
533 out2:
534         rpcrdma_destroy_id(ia->ri_id);
535         ia->ri_id = NULL;
536 out1:
537         return rc;
538 }
539
540 /*
541  * Clean up/close an IA.
542  *   o if event handles and PD have been initialized, free them.
543  *   o close the IA
544  */
545 void
546 rpcrdma_ia_close(struct rpcrdma_ia *ia)
547 {
548         dprintk("RPC:       %s: entering\n", __func__);
549         if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
550                 if (ia->ri_id->qp)
551                         rdma_destroy_qp(ia->ri_id);
552                 rpcrdma_destroy_id(ia->ri_id);
553                 ia->ri_id = NULL;
554         }
555
556         /* If the pd is still busy, xprtrdma missed freeing a resource */
557         if (ia->ri_pd && !IS_ERR(ia->ri_pd))
558                 ib_dealloc_pd(ia->ri_pd);
559 }
560
561 /*
562  * Create unconnected endpoint.
563  */
564 int
565 rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
566                                 struct rpcrdma_create_data_internal *cdata)
567 {
568         struct ib_device_attr *devattr = &ia->ri_devattr;
569         struct ib_cq *sendcq, *recvcq;
570         struct ib_cq_init_attr cq_attr = {};
571         unsigned int max_qp_wr;
572         int rc, err;
573
574         if (devattr->max_sge < RPCRDMA_MAX_IOVS) {
575                 dprintk("RPC:       %s: insufficient sge's available\n",
576                         __func__);
577                 return -ENOMEM;
578         }
579
580         if (devattr->max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
581                 dprintk("RPC:       %s: insufficient wqe's available\n",
582                         __func__);
583                 return -ENOMEM;
584         }
585         max_qp_wr = devattr->max_qp_wr - RPCRDMA_BACKWARD_WRS;
586
587         /* check provider's send/recv wr limits */
588         if (cdata->max_requests > max_qp_wr)
589                 cdata->max_requests = max_qp_wr;
590
591         ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
592         ep->rep_attr.qp_context = ep;
593         ep->rep_attr.srq = NULL;
594         ep->rep_attr.cap.max_send_wr = cdata->max_requests;
595         ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
596         rc = ia->ri_ops->ro_open(ia, ep, cdata);
597         if (rc)
598                 return rc;
599         ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
600         ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
601         ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_IOVS;
602         ep->rep_attr.cap.max_recv_sge = 1;
603         ep->rep_attr.cap.max_inline_data = 0;
604         ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
605         ep->rep_attr.qp_type = IB_QPT_RC;
606         ep->rep_attr.port_num = ~0;
607
608         dprintk("RPC:       %s: requested max: dtos: send %d recv %d; "
609                 "iovs: send %d recv %d\n",
610                 __func__,
611                 ep->rep_attr.cap.max_send_wr,
612                 ep->rep_attr.cap.max_recv_wr,
613                 ep->rep_attr.cap.max_send_sge,
614                 ep->rep_attr.cap.max_recv_sge);
615
616         /* set trigger for requesting send completion */
617         ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
618         if (ep->rep_cqinit > RPCRDMA_MAX_UNSIGNALED_SENDS)
619                 ep->rep_cqinit = RPCRDMA_MAX_UNSIGNALED_SENDS;
620         else if (ep->rep_cqinit <= 2)
621                 ep->rep_cqinit = 0;
622         INIT_CQCOUNT(ep);
623         init_waitqueue_head(&ep->rep_connect_wait);
624         INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
625
626         cq_attr.cqe = ep->rep_attr.cap.max_send_wr + 1;
627         sendcq = ib_create_cq(ia->ri_device, rpcrdma_sendcq_upcall,
628                               rpcrdma_cq_async_error_upcall, NULL, &cq_attr);
629         if (IS_ERR(sendcq)) {
630                 rc = PTR_ERR(sendcq);
631                 dprintk("RPC:       %s: failed to create send CQ: %i\n",
632                         __func__, rc);
633                 goto out1;
634         }
635
636         rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
637         if (rc) {
638                 dprintk("RPC:       %s: ib_req_notify_cq failed: %i\n",
639                         __func__, rc);
640                 goto out2;
641         }
642
643         cq_attr.cqe = ep->rep_attr.cap.max_recv_wr + 1;
644         recvcq = ib_create_cq(ia->ri_device, rpcrdma_recvcq_upcall,
645                               rpcrdma_cq_async_error_upcall, NULL, &cq_attr);
646         if (IS_ERR(recvcq)) {
647                 rc = PTR_ERR(recvcq);
648                 dprintk("RPC:       %s: failed to create recv CQ: %i\n",
649                         __func__, rc);
650                 goto out2;
651         }
652
653         rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
654         if (rc) {
655                 dprintk("RPC:       %s: ib_req_notify_cq failed: %i\n",
656                         __func__, rc);
657                 ib_destroy_cq(recvcq);
658                 goto out2;
659         }
660
661         ep->rep_attr.send_cq = sendcq;
662         ep->rep_attr.recv_cq = recvcq;
663
664         /* Initialize cma parameters */
665
666         /* RPC/RDMA does not use private data */
667         ep->rep_remote_cma.private_data = NULL;
668         ep->rep_remote_cma.private_data_len = 0;
669
670         /* Client offers RDMA Read but does not initiate */
671         ep->rep_remote_cma.initiator_depth = 0;
672         if (devattr->max_qp_rd_atom > 32)       /* arbitrary but <= 255 */
673                 ep->rep_remote_cma.responder_resources = 32;
674         else
675                 ep->rep_remote_cma.responder_resources =
676                                                 devattr->max_qp_rd_atom;
677
678         ep->rep_remote_cma.retry_count = 7;
679         ep->rep_remote_cma.flow_control = 0;
680         ep->rep_remote_cma.rnr_retry_count = 0;
681
682         return 0;
683
684 out2:
685         err = ib_destroy_cq(sendcq);
686         if (err)
687                 dprintk("RPC:       %s: ib_destroy_cq returned %i\n",
688                         __func__, err);
689 out1:
690         if (ia->ri_dma_mr)
691                 ib_dereg_mr(ia->ri_dma_mr);
692         return rc;
693 }
694
695 /*
696  * rpcrdma_ep_destroy
697  *
698  * Disconnect and destroy endpoint. After this, the only
699  * valid operations on the ep are to free it (if dynamically
700  * allocated) or re-create it.
701  */
702 void
703 rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
704 {
705         int rc;
706
707         dprintk("RPC:       %s: entering, connected is %d\n",
708                 __func__, ep->rep_connected);
709
710         cancel_delayed_work_sync(&ep->rep_connect_worker);
711
712         if (ia->ri_id->qp)
713                 rpcrdma_ep_disconnect(ep, ia);
714
715         rpcrdma_clean_cq(ep->rep_attr.recv_cq);
716         rpcrdma_clean_cq(ep->rep_attr.send_cq);
717
718         if (ia->ri_id->qp) {
719                 rdma_destroy_qp(ia->ri_id);
720                 ia->ri_id->qp = NULL;
721         }
722
723         rc = ib_destroy_cq(ep->rep_attr.recv_cq);
724         if (rc)
725                 dprintk("RPC:       %s: ib_destroy_cq returned %i\n",
726                         __func__, rc);
727
728         rc = ib_destroy_cq(ep->rep_attr.send_cq);
729         if (rc)
730                 dprintk("RPC:       %s: ib_destroy_cq returned %i\n",
731                         __func__, rc);
732
733         if (ia->ri_dma_mr) {
734                 rc = ib_dereg_mr(ia->ri_dma_mr);
735                 dprintk("RPC:       %s: ib_dereg_mr returned %i\n",
736                         __func__, rc);
737         }
738 }
739
740 /*
741  * Connect unconnected endpoint.
742  */
743 int
744 rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
745 {
746         struct rdma_cm_id *id, *old;
747         int rc = 0;
748         int retry_count = 0;
749
750         if (ep->rep_connected != 0) {
751                 struct rpcrdma_xprt *xprt;
752 retry:
753                 dprintk("RPC:       %s: reconnecting...\n", __func__);
754
755                 rpcrdma_ep_disconnect(ep, ia);
756                 rpcrdma_flush_cqs(ep);
757
758                 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
759                 id = rpcrdma_create_id(xprt, ia,
760                                 (struct sockaddr *)&xprt->rx_data.addr);
761                 if (IS_ERR(id)) {
762                         rc = -EHOSTUNREACH;
763                         goto out;
764                 }
765                 /* TEMP TEMP TEMP - fail if new device:
766                  * Deregister/remarshal *all* requests!
767                  * Close and recreate adapter, pd, etc!
768                  * Re-determine all attributes still sane!
769                  * More stuff I haven't thought of!
770                  * Rrrgh!
771                  */
772                 if (ia->ri_device != id->device) {
773                         printk("RPC:       %s: can't reconnect on "
774                                 "different device!\n", __func__);
775                         rpcrdma_destroy_id(id);
776                         rc = -ENETUNREACH;
777                         goto out;
778                 }
779                 /* END TEMP */
780                 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
781                 if (rc) {
782                         dprintk("RPC:       %s: rdma_create_qp failed %i\n",
783                                 __func__, rc);
784                         rpcrdma_destroy_id(id);
785                         rc = -ENETUNREACH;
786                         goto out;
787                 }
788
789                 write_lock(&ia->ri_qplock);
790                 old = ia->ri_id;
791                 ia->ri_id = id;
792                 write_unlock(&ia->ri_qplock);
793
794                 rdma_destroy_qp(old);
795                 rpcrdma_destroy_id(old);
796         } else {
797                 dprintk("RPC:       %s: connecting...\n", __func__);
798                 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
799                 if (rc) {
800                         dprintk("RPC:       %s: rdma_create_qp failed %i\n",
801                                 __func__, rc);
802                         /* do not update ep->rep_connected */
803                         return -ENETUNREACH;
804                 }
805         }
806
807         ep->rep_connected = 0;
808
809         rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
810         if (rc) {
811                 dprintk("RPC:       %s: rdma_connect() failed with %i\n",
812                                 __func__, rc);
813                 goto out;
814         }
815
816         wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
817
818         /*
819          * Check state. A non-peer reject indicates no listener
820          * (ECONNREFUSED), which may be a transient state. All
821          * others indicate a transport condition which has already
822          * undergone a best-effort.
823          */
824         if (ep->rep_connected == -ECONNREFUSED &&
825             ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
826                 dprintk("RPC:       %s: non-peer_reject, retry\n", __func__);
827                 goto retry;
828         }
829         if (ep->rep_connected <= 0) {
830                 /* Sometimes, the only way to reliably connect to remote
831                  * CMs is to use same nonzero values for ORD and IRD. */
832                 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
833                     (ep->rep_remote_cma.responder_resources == 0 ||
834                      ep->rep_remote_cma.initiator_depth !=
835                                 ep->rep_remote_cma.responder_resources)) {
836                         if (ep->rep_remote_cma.responder_resources == 0)
837                                 ep->rep_remote_cma.responder_resources = 1;
838                         ep->rep_remote_cma.initiator_depth =
839                                 ep->rep_remote_cma.responder_resources;
840                         goto retry;
841                 }
842                 rc = ep->rep_connected;
843         } else {
844                 struct rpcrdma_xprt *r_xprt;
845                 unsigned int extras;
846
847                 dprintk("RPC:       %s: connected\n", __func__);
848
849                 r_xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
850                 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
851
852                 if (extras) {
853                         rc = rpcrdma_ep_post_extra_recv(r_xprt, extras);
854                         if (rc)
855                                 pr_warn("%s: rpcrdma_ep_post_extra_recv: %i\n",
856                                         __func__, rc);
857                                 rc = 0;
858                 }
859         }
860
861 out:
862         if (rc)
863                 ep->rep_connected = rc;
864         return rc;
865 }
866
867 /*
868  * rpcrdma_ep_disconnect
869  *
870  * This is separate from destroy to facilitate the ability
871  * to reconnect without recreating the endpoint.
872  *
873  * This call is not reentrant, and must not be made in parallel
874  * on the same endpoint.
875  */
876 void
877 rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
878 {
879         int rc;
880
881         rpcrdma_flush_cqs(ep);
882         rc = rdma_disconnect(ia->ri_id);
883         if (!rc) {
884                 /* returns without wait if not connected */
885                 wait_event_interruptible(ep->rep_connect_wait,
886                                                         ep->rep_connected != 1);
887                 dprintk("RPC:       %s: after wait, %sconnected\n", __func__,
888                         (ep->rep_connected == 1) ? "still " : "dis");
889         } else {
890                 dprintk("RPC:       %s: rdma_disconnect %i\n", __func__, rc);
891                 ep->rep_connected = rc;
892         }
893 }
894
895 struct rpcrdma_req *
896 rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
897 {
898         struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
899         struct rpcrdma_req *req;
900
901         req = kzalloc(sizeof(*req), GFP_KERNEL);
902         if (req == NULL)
903                 return ERR_PTR(-ENOMEM);
904
905         INIT_LIST_HEAD(&req->rl_free);
906         spin_lock(&buffer->rb_reqslock);
907         list_add(&req->rl_all, &buffer->rb_allreqs);
908         spin_unlock(&buffer->rb_reqslock);
909         req->rl_buffer = &r_xprt->rx_buf;
910         return req;
911 }
912
913 struct rpcrdma_rep *
914 rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
915 {
916         struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
917         struct rpcrdma_ia *ia = &r_xprt->rx_ia;
918         struct rpcrdma_rep *rep;
919         int rc;
920
921         rc = -ENOMEM;
922         rep = kzalloc(sizeof(*rep), GFP_KERNEL);
923         if (rep == NULL)
924                 goto out;
925
926         rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize,
927                                                GFP_KERNEL);
928         if (IS_ERR(rep->rr_rdmabuf)) {
929                 rc = PTR_ERR(rep->rr_rdmabuf);
930                 goto out_free;
931         }
932
933         rep->rr_device = ia->ri_device;
934         rep->rr_rxprt = r_xprt;
935         INIT_WORK(&rep->rr_work, rpcrdma_receive_worker);
936         return rep;
937
938 out_free:
939         kfree(rep);
940 out:
941         return ERR_PTR(rc);
942 }
943
944 int
945 rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
946 {
947         struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
948         struct rpcrdma_ia *ia = &r_xprt->rx_ia;
949         int i, rc;
950
951         buf->rb_max_requests = r_xprt->rx_data.max_requests;
952         buf->rb_bc_srv_max_requests = 0;
953         spin_lock_init(&buf->rb_lock);
954
955         rc = ia->ri_ops->ro_init(r_xprt);
956         if (rc)
957                 goto out;
958
959         INIT_LIST_HEAD(&buf->rb_send_bufs);
960         INIT_LIST_HEAD(&buf->rb_allreqs);
961         spin_lock_init(&buf->rb_reqslock);
962         for (i = 0; i < buf->rb_max_requests; i++) {
963                 struct rpcrdma_req *req;
964
965                 req = rpcrdma_create_req(r_xprt);
966                 if (IS_ERR(req)) {
967                         dprintk("RPC:       %s: request buffer %d alloc"
968                                 " failed\n", __func__, i);
969                         rc = PTR_ERR(req);
970                         goto out;
971                 }
972                 req->rl_backchannel = false;
973                 list_add(&req->rl_free, &buf->rb_send_bufs);
974         }
975
976         INIT_LIST_HEAD(&buf->rb_recv_bufs);
977         for (i = 0; i < buf->rb_max_requests + 2; i++) {
978                 struct rpcrdma_rep *rep;
979
980                 rep = rpcrdma_create_rep(r_xprt);
981                 if (IS_ERR(rep)) {
982                         dprintk("RPC:       %s: reply buffer %d alloc failed\n",
983                                 __func__, i);
984                         rc = PTR_ERR(rep);
985                         goto out;
986                 }
987                 list_add(&rep->rr_list, &buf->rb_recv_bufs);
988         }
989
990         return 0;
991 out:
992         rpcrdma_buffer_destroy(buf);
993         return rc;
994 }
995
996 static struct rpcrdma_req *
997 rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
998 {
999         struct rpcrdma_req *req;
1000
1001         req = list_first_entry(&buf->rb_send_bufs,
1002                                struct rpcrdma_req, rl_free);
1003         list_del(&req->rl_free);
1004         return req;
1005 }
1006
1007 static struct rpcrdma_rep *
1008 rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1009 {
1010         struct rpcrdma_rep *rep;
1011
1012         rep = list_first_entry(&buf->rb_recv_bufs,
1013                                struct rpcrdma_rep, rr_list);
1014         list_del(&rep->rr_list);
1015         return rep;
1016 }
1017
1018 static void
1019 rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
1020 {
1021         rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
1022         kfree(rep);
1023 }
1024
1025 void
1026 rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
1027 {
1028         rpcrdma_free_regbuf(ia, req->rl_sendbuf);
1029         rpcrdma_free_regbuf(ia, req->rl_rdmabuf);
1030         kfree(req);
1031 }
1032
1033 void
1034 rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1035 {
1036         struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1037
1038         while (!list_empty(&buf->rb_recv_bufs)) {
1039                 struct rpcrdma_rep *rep;
1040
1041                 rep = rpcrdma_buffer_get_rep_locked(buf);
1042                 rpcrdma_destroy_rep(ia, rep);
1043         }
1044
1045         spin_lock(&buf->rb_reqslock);
1046         while (!list_empty(&buf->rb_allreqs)) {
1047                 struct rpcrdma_req *req;
1048
1049                 req = list_first_entry(&buf->rb_allreqs,
1050                                        struct rpcrdma_req, rl_all);
1051                 list_del(&req->rl_all);
1052
1053                 spin_unlock(&buf->rb_reqslock);
1054                 rpcrdma_destroy_req(ia, req);
1055                 spin_lock(&buf->rb_reqslock);
1056         }
1057         spin_unlock(&buf->rb_reqslock);
1058
1059         ia->ri_ops->ro_destroy(buf);
1060 }
1061
1062 struct rpcrdma_mw *
1063 rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
1064 {
1065         struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1066         struct rpcrdma_mw *mw = NULL;
1067
1068         spin_lock(&buf->rb_mwlock);
1069         if (!list_empty(&buf->rb_mws)) {
1070                 mw = list_first_entry(&buf->rb_mws,
1071                                       struct rpcrdma_mw, mw_list);
1072                 list_del_init(&mw->mw_list);
1073         }
1074         spin_unlock(&buf->rb_mwlock);
1075
1076         if (!mw)
1077                 pr_err("RPC:       %s: no MWs available\n", __func__);
1078         return mw;
1079 }
1080
1081 void
1082 rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
1083 {
1084         struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1085
1086         spin_lock(&buf->rb_mwlock);
1087         list_add_tail(&mw->mw_list, &buf->rb_mws);
1088         spin_unlock(&buf->rb_mwlock);
1089 }
1090
1091 /*
1092  * Get a set of request/reply buffers.
1093  *
1094  * Reply buffer (if available) is attached to send buffer upon return.
1095  */
1096 struct rpcrdma_req *
1097 rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1098 {
1099         struct rpcrdma_req *req;
1100
1101         spin_lock(&buffers->rb_lock);
1102         if (list_empty(&buffers->rb_send_bufs))
1103                 goto out_reqbuf;
1104         req = rpcrdma_buffer_get_req_locked(buffers);
1105         if (list_empty(&buffers->rb_recv_bufs))
1106                 goto out_repbuf;
1107         req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
1108         spin_unlock(&buffers->rb_lock);
1109         return req;
1110
1111 out_reqbuf:
1112         spin_unlock(&buffers->rb_lock);
1113         pr_warn("RPC:       %s: out of request buffers\n", __func__);
1114         return NULL;
1115 out_repbuf:
1116         spin_unlock(&buffers->rb_lock);
1117         pr_warn("RPC:       %s: out of reply buffers\n", __func__);
1118         req->rl_reply = NULL;
1119         return req;
1120 }
1121
1122 /*
1123  * Put request/reply buffers back into pool.
1124  * Pre-decrement counter/array index.
1125  */
1126 void
1127 rpcrdma_buffer_put(struct rpcrdma_req *req)
1128 {
1129         struct rpcrdma_buffer *buffers = req->rl_buffer;
1130         struct rpcrdma_rep *rep = req->rl_reply;
1131
1132         req->rl_niovs = 0;
1133         req->rl_reply = NULL;
1134
1135         spin_lock(&buffers->rb_lock);
1136         list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
1137         if (rep)
1138                 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
1139         spin_unlock(&buffers->rb_lock);
1140 }
1141
1142 /*
1143  * Recover reply buffers from pool.
1144  * This happens when recovering from disconnect.
1145  */
1146 void
1147 rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1148 {
1149         struct rpcrdma_buffer *buffers = req->rl_buffer;
1150
1151         spin_lock(&buffers->rb_lock);
1152         if (!list_empty(&buffers->rb_recv_bufs))
1153                 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
1154         spin_unlock(&buffers->rb_lock);
1155 }
1156
1157 /*
1158  * Put reply buffers back into pool when not attached to
1159  * request. This happens in error conditions.
1160  */
1161 void
1162 rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1163 {
1164         struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
1165
1166         spin_lock(&buffers->rb_lock);
1167         list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
1168         spin_unlock(&buffers->rb_lock);
1169 }
1170
1171 /*
1172  * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1173  */
1174
1175 void
1176 rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg)
1177 {
1178         dprintk("RPC:       map_one: offset %p iova %llx len %zu\n",
1179                 seg->mr_offset,
1180                 (unsigned long long)seg->mr_dma, seg->mr_dmalen);
1181 }
1182
1183 /**
1184  * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
1185  * @ia: controlling rpcrdma_ia
1186  * @size: size of buffer to be allocated, in bytes
1187  * @flags: GFP flags
1188  *
1189  * Returns pointer to private header of an area of internally
1190  * registered memory, or an ERR_PTR. The registered buffer follows
1191  * the end of the private header.
1192  *
1193  * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1194  * receiving the payload of RDMA RECV operations. regbufs are not
1195  * used for RDMA READ/WRITE operations, thus are registered only for
1196  * LOCAL access.
1197  */
1198 struct rpcrdma_regbuf *
1199 rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
1200 {
1201         struct rpcrdma_regbuf *rb;
1202         struct ib_sge *iov;
1203
1204         rb = kmalloc(sizeof(*rb) + size, flags);
1205         if (rb == NULL)
1206                 goto out;
1207
1208         iov = &rb->rg_iov;
1209         iov->addr = ib_dma_map_single(ia->ri_device,
1210                                       (void *)rb->rg_base, size,
1211                                       DMA_BIDIRECTIONAL);
1212         if (ib_dma_mapping_error(ia->ri_device, iov->addr))
1213                 goto out_free;
1214
1215         iov->length = size;
1216         iov->lkey = ia->ri_pd->local_dma_lkey;
1217         rb->rg_size = size;
1218         rb->rg_owner = NULL;
1219         return rb;
1220
1221 out_free:
1222         kfree(rb);
1223 out:
1224         return ERR_PTR(-ENOMEM);
1225 }
1226
1227 /**
1228  * rpcrdma_free_regbuf - deregister and free registered buffer
1229  * @ia: controlling rpcrdma_ia
1230  * @rb: regbuf to be deregistered and freed
1231  */
1232 void
1233 rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1234 {
1235         struct ib_sge *iov;
1236
1237         if (!rb)
1238                 return;
1239
1240         iov = &rb->rg_iov;
1241         ib_dma_unmap_single(ia->ri_device,
1242                             iov->addr, iov->length, DMA_BIDIRECTIONAL);
1243         kfree(rb);
1244 }
1245
1246 /*
1247  * Prepost any receive buffer, then post send.
1248  *
1249  * Receive buffer is donated to hardware, reclaimed upon recv completion.
1250  */
1251 int
1252 rpcrdma_ep_post(struct rpcrdma_ia *ia,
1253                 struct rpcrdma_ep *ep,
1254                 struct rpcrdma_req *req)
1255 {
1256         struct ib_device *device = ia->ri_device;
1257         struct ib_send_wr send_wr, *send_wr_fail;
1258         struct rpcrdma_rep *rep = req->rl_reply;
1259         struct ib_sge *iov = req->rl_send_iov;
1260         int i, rc;
1261
1262         if (rep) {
1263                 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1264                 if (rc)
1265                         goto out;
1266                 req->rl_reply = NULL;
1267         }
1268
1269         send_wr.next = NULL;
1270         send_wr.wr_id = RPCRDMA_IGNORE_COMPLETION;
1271         send_wr.sg_list = iov;
1272         send_wr.num_sge = req->rl_niovs;
1273         send_wr.opcode = IB_WR_SEND;
1274
1275         for (i = 0; i < send_wr.num_sge; i++)
1276                 ib_dma_sync_single_for_device(device, iov[i].addr,
1277                                               iov[i].length, DMA_TO_DEVICE);
1278         dprintk("RPC:       %s: posting %d s/g entries\n",
1279                 __func__, send_wr.num_sge);
1280
1281         if (DECR_CQCOUNT(ep) > 0)
1282                 send_wr.send_flags = 0;
1283         else { /* Provider must take a send completion every now and then */
1284                 INIT_CQCOUNT(ep);
1285                 send_wr.send_flags = IB_SEND_SIGNALED;
1286         }
1287
1288         rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1289         if (rc)
1290                 dprintk("RPC:       %s: ib_post_send returned %i\n", __func__,
1291                         rc);
1292 out:
1293         return rc;
1294 }
1295
1296 /*
1297  * (Re)post a receive buffer.
1298  */
1299 int
1300 rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1301                      struct rpcrdma_ep *ep,
1302                      struct rpcrdma_rep *rep)
1303 {
1304         struct ib_recv_wr recv_wr, *recv_wr_fail;
1305         int rc;
1306
1307         recv_wr.next = NULL;
1308         recv_wr.wr_id = (u64) (unsigned long) rep;
1309         recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
1310         recv_wr.num_sge = 1;
1311
1312         ib_dma_sync_single_for_cpu(ia->ri_device,
1313                                    rdmab_addr(rep->rr_rdmabuf),
1314                                    rdmab_length(rep->rr_rdmabuf),
1315                                    DMA_BIDIRECTIONAL);
1316
1317         rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1318
1319         if (rc)
1320                 dprintk("RPC:       %s: ib_post_recv returned %i\n", __func__,
1321                         rc);
1322         return rc;
1323 }
1324
1325 /**
1326  * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1327  * @r_xprt: transport associated with these backchannel resources
1328  * @min_reqs: minimum number of incoming requests expected
1329  *
1330  * Returns zero if all requested buffers were posted, or a negative errno.
1331  */
1332 int
1333 rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1334 {
1335         struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1336         struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1337         struct rpcrdma_ep *ep = &r_xprt->rx_ep;
1338         struct rpcrdma_rep *rep;
1339         unsigned long flags;
1340         int rc;
1341
1342         while (count--) {
1343                 spin_lock_irqsave(&buffers->rb_lock, flags);
1344                 if (list_empty(&buffers->rb_recv_bufs))
1345                         goto out_reqbuf;
1346                 rep = rpcrdma_buffer_get_rep_locked(buffers);
1347                 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1348
1349                 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1350                 if (rc)
1351                         goto out_rc;
1352         }
1353
1354         return 0;
1355
1356 out_reqbuf:
1357         spin_unlock_irqrestore(&buffers->rb_lock, flags);
1358         pr_warn("%s: no extra receive buffers\n", __func__);
1359         return -ENOMEM;
1360
1361 out_rc:
1362         rpcrdma_recv_buffer_put(rep);
1363         return rc;
1364 }
1365
1366 /* How many chunk list items fit within our inline buffers?
1367  */
1368 unsigned int
1369 rpcrdma_max_segments(struct rpcrdma_xprt *r_xprt)
1370 {
1371         struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
1372         int bytes, segments;
1373
1374         bytes = min_t(unsigned int, cdata->inline_wsize, cdata->inline_rsize);
1375         bytes -= RPCRDMA_HDRLEN_MIN;
1376         if (bytes < sizeof(struct rpcrdma_segment) * 2) {
1377                 pr_warn("RPC:       %s: inline threshold too small\n",
1378                         __func__);
1379                 return 0;
1380         }
1381
1382         segments = 1 << (fls(bytes / sizeof(struct rpcrdma_segment)) - 1);
1383         dprintk("RPC:       %s: max chunk list size = %d segments\n",
1384                 __func__, segments);
1385         return segments;
1386 }