]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/sunrpc/xprtrdma/svc_rdma_transport.c
svcrdma: Remove frmr cache
[karo-tx-linux.git] / net / sunrpc / xprtrdma / svc_rdma_transport.c
1 /*
2  * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
3  * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the BSD-type
9  * license below:
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  *      Redistributions of source code must retain the above copyright
16  *      notice, this list of conditions and the following disclaimer.
17  *
18  *      Redistributions in binary form must reproduce the above
19  *      copyright notice, this list of conditions and the following
20  *      disclaimer in the documentation and/or other materials provided
21  *      with the distribution.
22  *
23  *      Neither the name of the Network Appliance, Inc. nor the names of
24  *      its contributors may be used to endorse or promote products
25  *      derived from this software without specific prior written
26  *      permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * Author: Tom Tucker <tom@opengridcomputing.com>
41  */
42
43 #include <linux/sunrpc/svc_xprt.h>
44 #include <linux/sunrpc/addr.h>
45 #include <linux/sunrpc/debug.h>
46 #include <linux/sunrpc/rpc_rdma.h>
47 #include <linux/interrupt.h>
48 #include <linux/sched.h>
49 #include <linux/slab.h>
50 #include <linux/spinlock.h>
51 #include <linux/workqueue.h>
52 #include <rdma/ib_verbs.h>
53 #include <rdma/rdma_cm.h>
54 #include <linux/sunrpc/svc_rdma.h>
55 #include <linux/export.h>
56 #include "xprt_rdma.h"
57
58 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
59
60 static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *, int);
61 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
62                                         struct net *net,
63                                         struct sockaddr *sa, int salen,
64                                         int flags);
65 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
66 static void svc_rdma_release_rqst(struct svc_rqst *);
67 static void svc_rdma_detach(struct svc_xprt *xprt);
68 static void svc_rdma_free(struct svc_xprt *xprt);
69 static int svc_rdma_has_wspace(struct svc_xprt *xprt);
70 static int svc_rdma_secure_port(struct svc_rqst *);
71 static void svc_rdma_kill_temp_xprt(struct svc_xprt *);
72
73 static struct svc_xprt_ops svc_rdma_ops = {
74         .xpo_create = svc_rdma_create,
75         .xpo_recvfrom = svc_rdma_recvfrom,
76         .xpo_sendto = svc_rdma_sendto,
77         .xpo_release_rqst = svc_rdma_release_rqst,
78         .xpo_detach = svc_rdma_detach,
79         .xpo_free = svc_rdma_free,
80         .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
81         .xpo_has_wspace = svc_rdma_has_wspace,
82         .xpo_accept = svc_rdma_accept,
83         .xpo_secure_port = svc_rdma_secure_port,
84         .xpo_kill_temp_xprt = svc_rdma_kill_temp_xprt,
85 };
86
87 struct svc_xprt_class svc_rdma_class = {
88         .xcl_name = "rdma",
89         .xcl_owner = THIS_MODULE,
90         .xcl_ops = &svc_rdma_ops,
91         .xcl_max_payload = RPCSVC_MAXPAYLOAD_RDMA,
92         .xcl_ident = XPRT_TRANSPORT_RDMA,
93 };
94
95 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
96 static struct svc_xprt *svc_rdma_bc_create(struct svc_serv *, struct net *,
97                                            struct sockaddr *, int, int);
98 static void svc_rdma_bc_detach(struct svc_xprt *);
99 static void svc_rdma_bc_free(struct svc_xprt *);
100
101 static struct svc_xprt_ops svc_rdma_bc_ops = {
102         .xpo_create = svc_rdma_bc_create,
103         .xpo_detach = svc_rdma_bc_detach,
104         .xpo_free = svc_rdma_bc_free,
105         .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
106         .xpo_secure_port = svc_rdma_secure_port,
107 };
108
109 struct svc_xprt_class svc_rdma_bc_class = {
110         .xcl_name = "rdma-bc",
111         .xcl_owner = THIS_MODULE,
112         .xcl_ops = &svc_rdma_bc_ops,
113         .xcl_max_payload = (1024 - RPCRDMA_HDRLEN_MIN)
114 };
115
116 static struct svc_xprt *svc_rdma_bc_create(struct svc_serv *serv,
117                                            struct net *net,
118                                            struct sockaddr *sa, int salen,
119                                            int flags)
120 {
121         struct svcxprt_rdma *cma_xprt;
122         struct svc_xprt *xprt;
123
124         cma_xprt = rdma_create_xprt(serv, 0);
125         if (!cma_xprt)
126                 return ERR_PTR(-ENOMEM);
127         xprt = &cma_xprt->sc_xprt;
128
129         svc_xprt_init(net, &svc_rdma_bc_class, xprt, serv);
130         set_bit(XPT_CONG_CTRL, &xprt->xpt_flags);
131         serv->sv_bc_xprt = xprt;
132
133         dprintk("svcrdma: %s(%p)\n", __func__, xprt);
134         return xprt;
135 }
136
137 static void svc_rdma_bc_detach(struct svc_xprt *xprt)
138 {
139         dprintk("svcrdma: %s(%p)\n", __func__, xprt);
140 }
141
142 static void svc_rdma_bc_free(struct svc_xprt *xprt)
143 {
144         struct svcxprt_rdma *rdma =
145                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
146
147         dprintk("svcrdma: %s(%p)\n", __func__, xprt);
148         if (xprt)
149                 kfree(rdma);
150 }
151 #endif  /* CONFIG_SUNRPC_BACKCHANNEL */
152
153 static struct svc_rdma_op_ctxt *alloc_ctxt(struct svcxprt_rdma *xprt,
154                                            gfp_t flags)
155 {
156         struct svc_rdma_op_ctxt *ctxt;
157
158         ctxt = kmalloc(sizeof(*ctxt), flags);
159         if (ctxt) {
160                 ctxt->xprt = xprt;
161                 INIT_LIST_HEAD(&ctxt->list);
162         }
163         return ctxt;
164 }
165
166 static bool svc_rdma_prealloc_ctxts(struct svcxprt_rdma *xprt)
167 {
168         unsigned int i;
169
170         /* Each RPC/RDMA credit can consume a number of send
171          * and receive WQEs. One ctxt is allocated for each.
172          */
173         i = xprt->sc_sq_depth + xprt->sc_rq_depth;
174
175         while (i--) {
176                 struct svc_rdma_op_ctxt *ctxt;
177
178                 ctxt = alloc_ctxt(xprt, GFP_KERNEL);
179                 if (!ctxt) {
180                         dprintk("svcrdma: No memory for RDMA ctxt\n");
181                         return false;
182                 }
183                 list_add(&ctxt->list, &xprt->sc_ctxts);
184         }
185         return true;
186 }
187
188 struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
189 {
190         struct svc_rdma_op_ctxt *ctxt = NULL;
191
192         spin_lock(&xprt->sc_ctxt_lock);
193         xprt->sc_ctxt_used++;
194         if (list_empty(&xprt->sc_ctxts))
195                 goto out_empty;
196
197         ctxt = list_first_entry(&xprt->sc_ctxts,
198                                 struct svc_rdma_op_ctxt, list);
199         list_del(&ctxt->list);
200         spin_unlock(&xprt->sc_ctxt_lock);
201
202 out:
203         ctxt->count = 0;
204         ctxt->mapped_sges = 0;
205         return ctxt;
206
207 out_empty:
208         /* Either pre-allocation missed the mark, or send
209          * queue accounting is broken.
210          */
211         spin_unlock(&xprt->sc_ctxt_lock);
212
213         ctxt = alloc_ctxt(xprt, GFP_NOIO);
214         if (ctxt)
215                 goto out;
216
217         spin_lock(&xprt->sc_ctxt_lock);
218         xprt->sc_ctxt_used--;
219         spin_unlock(&xprt->sc_ctxt_lock);
220         WARN_ONCE(1, "svcrdma: empty RDMA ctxt list?\n");
221         return NULL;
222 }
223
224 void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
225 {
226         struct svcxprt_rdma *xprt = ctxt->xprt;
227         struct ib_device *device = xprt->sc_cm_id->device;
228         u32 lkey = xprt->sc_pd->local_dma_lkey;
229         unsigned int i;
230
231         for (i = 0; i < ctxt->mapped_sges; i++) {
232                 /*
233                  * Unmap the DMA addr in the SGE if the lkey matches
234                  * the local_dma_lkey, otherwise, ignore it since it is
235                  * an FRMR lkey and will be unmapped later when the
236                  * last WR that uses it completes.
237                  */
238                 if (ctxt->sge[i].lkey == lkey)
239                         ib_dma_unmap_page(device,
240                                             ctxt->sge[i].addr,
241                                             ctxt->sge[i].length,
242                                             ctxt->direction);
243         }
244         ctxt->mapped_sges = 0;
245 }
246
247 void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
248 {
249         struct svcxprt_rdma *xprt = ctxt->xprt;
250         int i;
251
252         if (free_pages)
253                 for (i = 0; i < ctxt->count; i++)
254                         put_page(ctxt->pages[i]);
255
256         spin_lock(&xprt->sc_ctxt_lock);
257         xprt->sc_ctxt_used--;
258         list_add(&ctxt->list, &xprt->sc_ctxts);
259         spin_unlock(&xprt->sc_ctxt_lock);
260 }
261
262 static void svc_rdma_destroy_ctxts(struct svcxprt_rdma *xprt)
263 {
264         while (!list_empty(&xprt->sc_ctxts)) {
265                 struct svc_rdma_op_ctxt *ctxt;
266
267                 ctxt = list_first_entry(&xprt->sc_ctxts,
268                                         struct svc_rdma_op_ctxt, list);
269                 list_del(&ctxt->list);
270                 kfree(ctxt);
271         }
272 }
273
274 /* QP event handler */
275 static void qp_event_handler(struct ib_event *event, void *context)
276 {
277         struct svc_xprt *xprt = context;
278
279         switch (event->event) {
280         /* These are considered benign events */
281         case IB_EVENT_PATH_MIG:
282         case IB_EVENT_COMM_EST:
283         case IB_EVENT_SQ_DRAINED:
284         case IB_EVENT_QP_LAST_WQE_REACHED:
285                 dprintk("svcrdma: QP event %s (%d) received for QP=%p\n",
286                         ib_event_msg(event->event), event->event,
287                         event->element.qp);
288                 break;
289         /* These are considered fatal events */
290         case IB_EVENT_PATH_MIG_ERR:
291         case IB_EVENT_QP_FATAL:
292         case IB_EVENT_QP_REQ_ERR:
293         case IB_EVENT_QP_ACCESS_ERR:
294         case IB_EVENT_DEVICE_FATAL:
295         default:
296                 dprintk("svcrdma: QP ERROR event %s (%d) received for QP=%p, "
297                         "closing transport\n",
298                         ib_event_msg(event->event), event->event,
299                         event->element.qp);
300                 set_bit(XPT_CLOSE, &xprt->xpt_flags);
301                 break;
302         }
303 }
304
305 /**
306  * svc_rdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
307  * @cq:        completion queue
308  * @wc:        completed WR
309  *
310  */
311 static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
312 {
313         struct svcxprt_rdma *xprt = cq->cq_context;
314         struct ib_cqe *cqe = wc->wr_cqe;
315         struct svc_rdma_op_ctxt *ctxt;
316
317         /* WARNING: Only wc->wr_cqe and wc->status are reliable */
318         ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
319         svc_rdma_unmap_dma(ctxt);
320
321         if (wc->status != IB_WC_SUCCESS)
322                 goto flushed;
323
324         /* All wc fields are now known to be valid */
325         ctxt->byte_len = wc->byte_len;
326         spin_lock(&xprt->sc_rq_dto_lock);
327         list_add_tail(&ctxt->list, &xprt->sc_rq_dto_q);
328         spin_unlock(&xprt->sc_rq_dto_lock);
329
330         set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
331         if (test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
332                 goto out;
333         svc_xprt_enqueue(&xprt->sc_xprt);
334         goto out;
335
336 flushed:
337         if (wc->status != IB_WC_WR_FLUSH_ERR)
338                 pr_warn("svcrdma: receive: %s (%u/0x%x)\n",
339                         ib_wc_status_msg(wc->status),
340                         wc->status, wc->vendor_err);
341         set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
342         svc_rdma_put_context(ctxt, 1);
343
344 out:
345         svc_xprt_put(&xprt->sc_xprt);
346 }
347
348 /**
349  * svc_rdma_wc_send - Invoked by RDMA provider for each polled Send WC
350  * @cq:        completion queue
351  * @wc:        completed WR
352  *
353  */
354 void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
355 {
356         struct svcxprt_rdma *xprt = cq->cq_context;
357         struct ib_cqe *cqe = wc->wr_cqe;
358         struct svc_rdma_op_ctxt *ctxt;
359
360         atomic_inc(&xprt->sc_sq_avail);
361         wake_up(&xprt->sc_send_wait);
362
363         ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
364         svc_rdma_unmap_dma(ctxt);
365         svc_rdma_put_context(ctxt, 1);
366
367         if (unlikely(wc->status != IB_WC_SUCCESS)) {
368                 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
369                 if (wc->status != IB_WC_WR_FLUSH_ERR)
370                         pr_err("svcrdma: Send: %s (%u/0x%x)\n",
371                                ib_wc_status_msg(wc->status),
372                                wc->status, wc->vendor_err);
373         }
374
375         svc_xprt_put(&xprt->sc_xprt);
376 }
377
378 static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
379                                              int listener)
380 {
381         struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
382
383         if (!cma_xprt)
384                 return NULL;
385         svc_xprt_init(&init_net, &svc_rdma_class, &cma_xprt->sc_xprt, serv);
386         INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
387         INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
388         INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
389         INIT_LIST_HEAD(&cma_xprt->sc_ctxts);
390         INIT_LIST_HEAD(&cma_xprt->sc_rw_ctxts);
391         init_waitqueue_head(&cma_xprt->sc_send_wait);
392
393         spin_lock_init(&cma_xprt->sc_lock);
394         spin_lock_init(&cma_xprt->sc_rq_dto_lock);
395         spin_lock_init(&cma_xprt->sc_ctxt_lock);
396         spin_lock_init(&cma_xprt->sc_rw_ctxt_lock);
397
398         /*
399          * Note that this implies that the underlying transport support
400          * has some form of congestion control (see RFC 7530 section 3.1
401          * paragraph 2). For now, we assume that all supported RDMA
402          * transports are suitable here.
403          */
404         set_bit(XPT_CONG_CTRL, &cma_xprt->sc_xprt.xpt_flags);
405
406         if (listener)
407                 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
408
409         return cma_xprt;
410 }
411
412 int svc_rdma_post_recv(struct svcxprt_rdma *xprt, gfp_t flags)
413 {
414         struct ib_recv_wr recv_wr, *bad_recv_wr;
415         struct svc_rdma_op_ctxt *ctxt;
416         struct page *page;
417         dma_addr_t pa;
418         int sge_no;
419         int buflen;
420         int ret;
421
422         ctxt = svc_rdma_get_context(xprt);
423         buflen = 0;
424         ctxt->direction = DMA_FROM_DEVICE;
425         ctxt->cqe.done = svc_rdma_wc_receive;
426         for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
427                 if (sge_no >= xprt->sc_max_sge) {
428                         pr_err("svcrdma: Too many sges (%d)\n", sge_no);
429                         goto err_put_ctxt;
430                 }
431                 page = alloc_page(flags);
432                 if (!page)
433                         goto err_put_ctxt;
434                 ctxt->pages[sge_no] = page;
435                 pa = ib_dma_map_page(xprt->sc_cm_id->device,
436                                      page, 0, PAGE_SIZE,
437                                      DMA_FROM_DEVICE);
438                 if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
439                         goto err_put_ctxt;
440                 svc_rdma_count_mappings(xprt, ctxt);
441                 ctxt->sge[sge_no].addr = pa;
442                 ctxt->sge[sge_no].length = PAGE_SIZE;
443                 ctxt->sge[sge_no].lkey = xprt->sc_pd->local_dma_lkey;
444                 ctxt->count = sge_no + 1;
445                 buflen += PAGE_SIZE;
446         }
447         recv_wr.next = NULL;
448         recv_wr.sg_list = &ctxt->sge[0];
449         recv_wr.num_sge = ctxt->count;
450         recv_wr.wr_cqe = &ctxt->cqe;
451
452         svc_xprt_get(&xprt->sc_xprt);
453         ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
454         if (ret) {
455                 svc_rdma_unmap_dma(ctxt);
456                 svc_rdma_put_context(ctxt, 1);
457                 svc_xprt_put(&xprt->sc_xprt);
458         }
459         return ret;
460
461  err_put_ctxt:
462         svc_rdma_unmap_dma(ctxt);
463         svc_rdma_put_context(ctxt, 1);
464         return -ENOMEM;
465 }
466
467 int svc_rdma_repost_recv(struct svcxprt_rdma *xprt, gfp_t flags)
468 {
469         int ret = 0;
470
471         ret = svc_rdma_post_recv(xprt, flags);
472         if (ret) {
473                 pr_err("svcrdma: could not post a receive buffer, err=%d.\n",
474                        ret);
475                 pr_err("svcrdma: closing transport %p.\n", xprt);
476                 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
477                 ret = -ENOTCONN;
478         }
479         return ret;
480 }
481
482 static void
483 svc_rdma_parse_connect_private(struct svcxprt_rdma *newxprt,
484                                struct rdma_conn_param *param)
485 {
486         const struct rpcrdma_connect_private *pmsg = param->private_data;
487
488         if (pmsg &&
489             pmsg->cp_magic == rpcrdma_cmp_magic &&
490             pmsg->cp_version == RPCRDMA_CMP_VERSION) {
491                 newxprt->sc_snd_w_inv = pmsg->cp_flags &
492                                         RPCRDMA_CMP_F_SND_W_INV_OK;
493
494                 dprintk("svcrdma: client send_size %u, recv_size %u "
495                         "remote inv %ssupported\n",
496                         rpcrdma_decode_buffer_size(pmsg->cp_send_size),
497                         rpcrdma_decode_buffer_size(pmsg->cp_recv_size),
498                         newxprt->sc_snd_w_inv ? "" : "un");
499         }
500 }
501
502 /*
503  * This function handles the CONNECT_REQUEST event on a listening
504  * endpoint. It is passed the cma_id for the _new_ connection. The context in
505  * this cma_id is inherited from the listening cma_id and is the svc_xprt
506  * structure for the listening endpoint.
507  *
508  * This function creates a new xprt for the new connection and enqueues it on
509  * the accept queue for the listent xprt. When the listen thread is kicked, it
510  * will call the recvfrom method on the listen xprt which will accept the new
511  * connection.
512  */
513 static void handle_connect_req(struct rdma_cm_id *new_cma_id,
514                                struct rdma_conn_param *param)
515 {
516         struct svcxprt_rdma *listen_xprt = new_cma_id->context;
517         struct svcxprt_rdma *newxprt;
518         struct sockaddr *sa;
519
520         /* Create a new transport */
521         newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
522         if (!newxprt) {
523                 dprintk("svcrdma: failed to create new transport\n");
524                 return;
525         }
526         newxprt->sc_cm_id = new_cma_id;
527         new_cma_id->context = newxprt;
528         dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
529                 newxprt, newxprt->sc_cm_id, listen_xprt);
530         svc_rdma_parse_connect_private(newxprt, param);
531
532         /* Save client advertised inbound read limit for use later in accept. */
533         newxprt->sc_ord = param->initiator_depth;
534
535         /* Set the local and remote addresses in the transport */
536         sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
537         svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
538         sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
539         svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
540
541         /*
542          * Enqueue the new transport on the accept queue of the listening
543          * transport
544          */
545         spin_lock_bh(&listen_xprt->sc_lock);
546         list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
547         spin_unlock_bh(&listen_xprt->sc_lock);
548
549         set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
550         svc_xprt_enqueue(&listen_xprt->sc_xprt);
551 }
552
553 /*
554  * Handles events generated on the listening endpoint. These events will be
555  * either be incoming connect requests or adapter removal  events.
556  */
557 static int rdma_listen_handler(struct rdma_cm_id *cma_id,
558                                struct rdma_cm_event *event)
559 {
560         struct svcxprt_rdma *xprt = cma_id->context;
561         int ret = 0;
562
563         switch (event->event) {
564         case RDMA_CM_EVENT_CONNECT_REQUEST:
565                 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
566                         "event = %s (%d)\n", cma_id, cma_id->context,
567                         rdma_event_msg(event->event), event->event);
568                 handle_connect_req(cma_id, &event->param.conn);
569                 break;
570
571         case RDMA_CM_EVENT_ESTABLISHED:
572                 /* Accept complete */
573                 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
574                         "cm_id=%p\n", xprt, cma_id);
575                 break;
576
577         case RDMA_CM_EVENT_DEVICE_REMOVAL:
578                 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
579                         xprt, cma_id);
580                 if (xprt)
581                         set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
582                 break;
583
584         default:
585                 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
586                         "event = %s (%d)\n", cma_id,
587                         rdma_event_msg(event->event), event->event);
588                 break;
589         }
590
591         return ret;
592 }
593
594 static int rdma_cma_handler(struct rdma_cm_id *cma_id,
595                             struct rdma_cm_event *event)
596 {
597         struct svc_xprt *xprt = cma_id->context;
598         struct svcxprt_rdma *rdma =
599                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
600         switch (event->event) {
601         case RDMA_CM_EVENT_ESTABLISHED:
602                 /* Accept complete */
603                 svc_xprt_get(xprt);
604                 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
605                         "cm_id=%p\n", xprt, cma_id);
606                 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
607                 svc_xprt_enqueue(xprt);
608                 break;
609         case RDMA_CM_EVENT_DISCONNECTED:
610                 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
611                         xprt, cma_id);
612                 if (xprt) {
613                         set_bit(XPT_CLOSE, &xprt->xpt_flags);
614                         svc_xprt_enqueue(xprt);
615                         svc_xprt_put(xprt);
616                 }
617                 break;
618         case RDMA_CM_EVENT_DEVICE_REMOVAL:
619                 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
620                         "event = %s (%d)\n", cma_id, xprt,
621                         rdma_event_msg(event->event), event->event);
622                 if (xprt) {
623                         set_bit(XPT_CLOSE, &xprt->xpt_flags);
624                         svc_xprt_enqueue(xprt);
625                         svc_xprt_put(xprt);
626                 }
627                 break;
628         default:
629                 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
630                         "event = %s (%d)\n", cma_id,
631                         rdma_event_msg(event->event), event->event);
632                 break;
633         }
634         return 0;
635 }
636
637 /*
638  * Create a listening RDMA service endpoint.
639  */
640 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
641                                         struct net *net,
642                                         struct sockaddr *sa, int salen,
643                                         int flags)
644 {
645         struct rdma_cm_id *listen_id;
646         struct svcxprt_rdma *cma_xprt;
647         int ret;
648
649         dprintk("svcrdma: Creating RDMA socket\n");
650         if ((sa->sa_family != AF_INET) && (sa->sa_family != AF_INET6)) {
651                 dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
652                 return ERR_PTR(-EAFNOSUPPORT);
653         }
654         cma_xprt = rdma_create_xprt(serv, 1);
655         if (!cma_xprt)
656                 return ERR_PTR(-ENOMEM);
657
658         listen_id = rdma_create_id(&init_net, rdma_listen_handler, cma_xprt,
659                                    RDMA_PS_TCP, IB_QPT_RC);
660         if (IS_ERR(listen_id)) {
661                 ret = PTR_ERR(listen_id);
662                 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
663                 goto err0;
664         }
665
666         /* Allow both IPv4 and IPv6 sockets to bind a single port
667          * at the same time.
668          */
669 #if IS_ENABLED(CONFIG_IPV6)
670         ret = rdma_set_afonly(listen_id, 1);
671         if (ret) {
672                 dprintk("svcrdma: rdma_set_afonly failed = %d\n", ret);
673                 goto err1;
674         }
675 #endif
676         ret = rdma_bind_addr(listen_id, sa);
677         if (ret) {
678                 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
679                 goto err1;
680         }
681         cma_xprt->sc_cm_id = listen_id;
682
683         ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
684         if (ret) {
685                 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
686                 goto err1;
687         }
688
689         /*
690          * We need to use the address from the cm_id in case the
691          * caller specified 0 for the port number.
692          */
693         sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
694         svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
695
696         return &cma_xprt->sc_xprt;
697
698  err1:
699         rdma_destroy_id(listen_id);
700  err0:
701         kfree(cma_xprt);
702         return ERR_PTR(ret);
703 }
704
705 /*
706  * This is the xpo_recvfrom function for listening endpoints. Its
707  * purpose is to accept incoming connections. The CMA callback handler
708  * has already created a new transport and attached it to the new CMA
709  * ID.
710  *
711  * There is a queue of pending connections hung on the listening
712  * transport. This queue contains the new svc_xprt structure. This
713  * function takes svc_xprt structures off the accept_q and completes
714  * the connection.
715  */
716 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
717 {
718         struct svcxprt_rdma *listen_rdma;
719         struct svcxprt_rdma *newxprt = NULL;
720         struct rdma_conn_param conn_param;
721         struct rpcrdma_connect_private pmsg;
722         struct ib_qp_init_attr qp_attr;
723         struct ib_device *dev;
724         struct sockaddr *sap;
725         unsigned int i;
726         int ret = 0;
727
728         listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
729         clear_bit(XPT_CONN, &xprt->xpt_flags);
730         /* Get the next entry off the accept list */
731         spin_lock_bh(&listen_rdma->sc_lock);
732         if (!list_empty(&listen_rdma->sc_accept_q)) {
733                 newxprt = list_entry(listen_rdma->sc_accept_q.next,
734                                      struct svcxprt_rdma, sc_accept_q);
735                 list_del_init(&newxprt->sc_accept_q);
736         }
737         if (!list_empty(&listen_rdma->sc_accept_q))
738                 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
739         spin_unlock_bh(&listen_rdma->sc_lock);
740         if (!newxprt)
741                 return NULL;
742
743         dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
744                 newxprt, newxprt->sc_cm_id);
745
746         dev = newxprt->sc_cm_id->device;
747         newxprt->sc_port_num = newxprt->sc_cm_id->port_num;
748
749         /* Qualify the transport resource defaults with the
750          * capabilities of this particular device */
751         newxprt->sc_max_sge = min((size_t)dev->attrs.max_sge,
752                                   (size_t)RPCSVC_MAXPAGES);
753         newxprt->sc_max_req_size = svcrdma_max_req_size;
754         newxprt->sc_max_requests = min_t(u32, dev->attrs.max_qp_wr,
755                                          svcrdma_max_requests);
756         newxprt->sc_fc_credits = cpu_to_be32(newxprt->sc_max_requests);
757         newxprt->sc_max_bc_requests = min_t(u32, dev->attrs.max_qp_wr,
758                                             svcrdma_max_bc_requests);
759         newxprt->sc_rq_depth = newxprt->sc_max_requests +
760                                newxprt->sc_max_bc_requests;
761         newxprt->sc_sq_depth = newxprt->sc_rq_depth;
762         atomic_set(&newxprt->sc_sq_avail, newxprt->sc_sq_depth);
763
764         if (!svc_rdma_prealloc_ctxts(newxprt))
765                 goto errout;
766
767         /*
768          * Limit ORD based on client limit, local device limit, and
769          * configured svcrdma limit.
770          */
771         newxprt->sc_ord = min_t(size_t, dev->attrs.max_qp_rd_atom, newxprt->sc_ord);
772         newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
773
774         newxprt->sc_pd = ib_alloc_pd(dev, 0);
775         if (IS_ERR(newxprt->sc_pd)) {
776                 dprintk("svcrdma: error creating PD for connect request\n");
777                 goto errout;
778         }
779         newxprt->sc_sq_cq = ib_alloc_cq(dev, newxprt, newxprt->sc_sq_depth,
780                                         0, IB_POLL_WORKQUEUE);
781         if (IS_ERR(newxprt->sc_sq_cq)) {
782                 dprintk("svcrdma: error creating SQ CQ for connect request\n");
783                 goto errout;
784         }
785         newxprt->sc_rq_cq = ib_alloc_cq(dev, newxprt, newxprt->sc_rq_depth,
786                                         0, IB_POLL_WORKQUEUE);
787         if (IS_ERR(newxprt->sc_rq_cq)) {
788                 dprintk("svcrdma: error creating RQ CQ for connect request\n");
789                 goto errout;
790         }
791
792         memset(&qp_attr, 0, sizeof qp_attr);
793         qp_attr.event_handler = qp_event_handler;
794         qp_attr.qp_context = &newxprt->sc_xprt;
795         qp_attr.port_num = newxprt->sc_cm_id->port_num;
796         qp_attr.cap.max_rdma_ctxs = newxprt->sc_max_requests;
797         qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
798         qp_attr.cap.max_recv_wr = newxprt->sc_rq_depth;
799         qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
800         qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
801         qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
802         qp_attr.qp_type = IB_QPT_RC;
803         qp_attr.send_cq = newxprt->sc_sq_cq;
804         qp_attr.recv_cq = newxprt->sc_rq_cq;
805         dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n",
806                 newxprt->sc_cm_id, newxprt->sc_pd);
807         dprintk("    cap.max_send_wr = %d, cap.max_recv_wr = %d\n",
808                 qp_attr.cap.max_send_wr, qp_attr.cap.max_recv_wr);
809         dprintk("    cap.max_send_sge = %d, cap.max_recv_sge = %d\n",
810                 qp_attr.cap.max_send_sge, qp_attr.cap.max_recv_sge);
811
812         ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
813         if (ret) {
814                 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
815                 goto errout;
816         }
817         newxprt->sc_qp = newxprt->sc_cm_id->qp;
818
819         /*
820          * Use the most secure set of MR resources based on the
821          * transport type and available memory management features in
822          * the device. Here's the table implemented below:
823          *
824          *              Fast    Global  DMA     Remote WR
825          *              Reg     LKEY    MR      Access
826          *              Sup'd   Sup'd   Needed  Needed
827          *
828          * IWARP        N       N       Y       Y
829          *              N       Y       Y       Y
830          *              Y       N       Y       N
831          *              Y       Y       N       -
832          *
833          * IB           N       N       Y       N
834          *              N       Y       N       -
835          *              Y       N       Y       N
836          *              Y       Y       N       -
837          *
838          * NB:  iWARP requires remote write access for the data sink
839          *      of an RDMA_READ. IB does not.
840          */
841         if (dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
842                 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
843         } else
844                 newxprt->sc_snd_w_inv = false;
845
846         /*
847          * Determine if a DMA MR is required and if so, what privs are required
848          */
849         if (!rdma_protocol_iwarp(dev, newxprt->sc_cm_id->port_num) &&
850             !rdma_ib_or_roce(dev, newxprt->sc_cm_id->port_num))
851                 goto errout;
852
853         if (rdma_protocol_iwarp(dev, newxprt->sc_cm_id->port_num))
854                 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
855
856         /* Post receive buffers */
857         for (i = 0; i < newxprt->sc_max_requests; i++) {
858                 ret = svc_rdma_post_recv(newxprt, GFP_KERNEL);
859                 if (ret) {
860                         dprintk("svcrdma: failure posting receive buffers\n");
861                         goto errout;
862                 }
863         }
864
865         /* Swap out the handler */
866         newxprt->sc_cm_id->event_handler = rdma_cma_handler;
867
868         /* Construct RDMA-CM private message */
869         pmsg.cp_magic = rpcrdma_cmp_magic;
870         pmsg.cp_version = RPCRDMA_CMP_VERSION;
871         pmsg.cp_flags = 0;
872         pmsg.cp_send_size = pmsg.cp_recv_size =
873                 rpcrdma_encode_buffer_size(newxprt->sc_max_req_size);
874
875         /* Accept Connection */
876         set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
877         memset(&conn_param, 0, sizeof conn_param);
878         conn_param.responder_resources = 0;
879         conn_param.initiator_depth = newxprt->sc_ord;
880         conn_param.private_data = &pmsg;
881         conn_param.private_data_len = sizeof(pmsg);
882         ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
883         if (ret) {
884                 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
885                        ret);
886                 goto errout;
887         }
888
889         dprintk("svcrdma: new connection %p accepted:\n", newxprt);
890         sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
891         dprintk("    local address   : %pIS:%u\n", sap, rpc_get_port(sap));
892         sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
893         dprintk("    remote address  : %pIS:%u\n", sap, rpc_get_port(sap));
894         dprintk("    max_sge         : %d\n", newxprt->sc_max_sge);
895         dprintk("    sq_depth        : %d\n", newxprt->sc_sq_depth);
896         dprintk("    max_requests    : %d\n", newxprt->sc_max_requests);
897         dprintk("    ord             : %d\n", newxprt->sc_ord);
898
899         return &newxprt->sc_xprt;
900
901  errout:
902         dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
903         /* Take a reference in case the DTO handler runs */
904         svc_xprt_get(&newxprt->sc_xprt);
905         if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
906                 ib_destroy_qp(newxprt->sc_qp);
907         rdma_destroy_id(newxprt->sc_cm_id);
908         /* This call to put will destroy the transport */
909         svc_xprt_put(&newxprt->sc_xprt);
910         return NULL;
911 }
912
913 static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
914 {
915 }
916
917 /*
918  * When connected, an svc_xprt has at least two references:
919  *
920  * - A reference held by the cm_id between the ESTABLISHED and
921  *   DISCONNECTED events. If the remote peer disconnected first, this
922  *   reference could be gone.
923  *
924  * - A reference held by the svc_recv code that called this function
925  *   as part of close processing.
926  *
927  * At a minimum one references should still be held.
928  */
929 static void svc_rdma_detach(struct svc_xprt *xprt)
930 {
931         struct svcxprt_rdma *rdma =
932                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
933         dprintk("svc: svc_rdma_detach(%p)\n", xprt);
934
935         /* Disconnect and flush posted WQE */
936         rdma_disconnect(rdma->sc_cm_id);
937 }
938
939 static void __svc_rdma_free(struct work_struct *work)
940 {
941         struct svcxprt_rdma *rdma =
942                 container_of(work, struct svcxprt_rdma, sc_work);
943         struct svc_xprt *xprt = &rdma->sc_xprt;
944
945         dprintk("svcrdma: %s(%p)\n", __func__, rdma);
946
947         if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
948                 ib_drain_qp(rdma->sc_qp);
949
950         /* We should only be called from kref_put */
951         if (kref_read(&xprt->xpt_ref) != 0)
952                 pr_err("svcrdma: sc_xprt still in use? (%d)\n",
953                        kref_read(&xprt->xpt_ref));
954
955         while (!list_empty(&rdma->sc_read_complete_q)) {
956                 struct svc_rdma_op_ctxt *ctxt;
957                 ctxt = list_first_entry(&rdma->sc_read_complete_q,
958                                         struct svc_rdma_op_ctxt, list);
959                 list_del(&ctxt->list);
960                 svc_rdma_put_context(ctxt, 1);
961         }
962         while (!list_empty(&rdma->sc_rq_dto_q)) {
963                 struct svc_rdma_op_ctxt *ctxt;
964                 ctxt = list_first_entry(&rdma->sc_rq_dto_q,
965                                         struct svc_rdma_op_ctxt, list);
966                 list_del(&ctxt->list);
967                 svc_rdma_put_context(ctxt, 1);
968         }
969
970         /* Warn if we leaked a resource or under-referenced */
971         if (rdma->sc_ctxt_used != 0)
972                 pr_err("svcrdma: ctxt still in use? (%d)\n",
973                        rdma->sc_ctxt_used);
974
975         /* Final put of backchannel client transport */
976         if (xprt->xpt_bc_xprt) {
977                 xprt_put(xprt->xpt_bc_xprt);
978                 xprt->xpt_bc_xprt = NULL;
979         }
980
981         svc_rdma_destroy_rw_ctxts(rdma);
982         svc_rdma_destroy_ctxts(rdma);
983
984         /* Destroy the QP if present (not a listener) */
985         if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
986                 ib_destroy_qp(rdma->sc_qp);
987
988         if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
989                 ib_free_cq(rdma->sc_sq_cq);
990
991         if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
992                 ib_free_cq(rdma->sc_rq_cq);
993
994         if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
995                 ib_dealloc_pd(rdma->sc_pd);
996
997         /* Destroy the CM ID */
998         rdma_destroy_id(rdma->sc_cm_id);
999
1000         kfree(rdma);
1001 }
1002
1003 static void svc_rdma_free(struct svc_xprt *xprt)
1004 {
1005         struct svcxprt_rdma *rdma =
1006                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1007         INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1008         queue_work(svc_rdma_wq, &rdma->sc_work);
1009 }
1010
1011 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1012 {
1013         struct svcxprt_rdma *rdma =
1014                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1015
1016         /*
1017          * If there are already waiters on the SQ,
1018          * return false.
1019          */
1020         if (waitqueue_active(&rdma->sc_send_wait))
1021                 return 0;
1022
1023         /* Otherwise return true. */
1024         return 1;
1025 }
1026
1027 static int svc_rdma_secure_port(struct svc_rqst *rqstp)
1028 {
1029         return 1;
1030 }
1031
1032 static void svc_rdma_kill_temp_xprt(struct svc_xprt *xprt)
1033 {
1034 }
1035
1036 int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1037 {
1038         struct ib_send_wr *bad_wr, *n_wr;
1039         int wr_count;
1040         int i;
1041         int ret;
1042
1043         if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1044                 return -ENOTCONN;
1045
1046         wr_count = 1;
1047         for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
1048                 wr_count++;
1049
1050         /* If the SQ is full, wait until an SQ entry is available */
1051         while (1) {
1052                 if ((atomic_sub_return(wr_count, &xprt->sc_sq_avail) < 0)) {
1053                         atomic_inc(&rdma_stat_sq_starve);
1054
1055                         /* Wait until SQ WR available if SQ still full */
1056                         atomic_add(wr_count, &xprt->sc_sq_avail);
1057                         wait_event(xprt->sc_send_wait,
1058                                    atomic_read(&xprt->sc_sq_avail) > wr_count);
1059                         if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1060                                 return -ENOTCONN;
1061                         continue;
1062                 }
1063                 /* Take a transport ref for each WR posted */
1064                 for (i = 0; i < wr_count; i++)
1065                         svc_xprt_get(&xprt->sc_xprt);
1066
1067                 /* Bump used SQ WR count and post */
1068                 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1069                 if (ret) {
1070                         set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
1071                         for (i = 0; i < wr_count; i ++)
1072                                 svc_xprt_put(&xprt->sc_xprt);
1073                         dprintk("svcrdma: failed to post SQ WR rc=%d\n", ret);
1074                         dprintk("    sc_sq_avail=%d, sc_sq_depth=%d\n",
1075                                 atomic_read(&xprt->sc_sq_avail),
1076                                 xprt->sc_sq_depth);
1077                         wake_up(&xprt->sc_send_wait);
1078                 }
1079                 break;
1080         }
1081         return ret;
1082 }