GNU Linux-libre 4.14.259-gnu1
[releases.git] / drivers / infiniband / core / cq.c
1 /*
2  * Copyright (c) 2015 HGST, a Western Digital Company.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  */
13 #include <linux/module.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <rdma/ib_verbs.h>
17
18 /* # of WCs to poll for with a single call to ib_poll_cq */
19 #define IB_POLL_BATCH                   16
20 #define IB_POLL_BATCH_DIRECT            8
21
22 /* # of WCs to iterate over before yielding */
23 #define IB_POLL_BUDGET_IRQ              256
24 #define IB_POLL_BUDGET_WORKQUEUE        65536
25
26 #define IB_POLL_FLAGS \
27         (IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS)
28
29 static int __ib_process_cq(struct ib_cq *cq, int budget, struct ib_wc *wcs,
30                            int batch)
31 {
32         int i, n, completed = 0;
33
34         /*
35          * budget might be (-1) if the caller does not
36          * want to bound this call, thus we need unsigned
37          * minimum here.
38          */
39         while ((n = ib_poll_cq(cq, min_t(u32, batch,
40                                          budget - completed), wcs)) > 0) {
41                 for (i = 0; i < n; i++) {
42                         struct ib_wc *wc = &wcs[i];
43
44                         if (wc->wr_cqe)
45                                 wc->wr_cqe->done(cq, wc);
46                         else
47                                 WARN_ON_ONCE(wc->status == IB_WC_SUCCESS);
48                 }
49
50                 completed += n;
51
52                 if (n != batch || (budget != -1 && completed >= budget))
53                         break;
54         }
55
56         return completed;
57 }
58
59 /**
60  * ib_process_direct_cq - process a CQ in caller context
61  * @cq:         CQ to process
62  * @budget:     number of CQEs to poll for
63  *
64  * This function is used to process all outstanding CQ entries.
65  * It does not offload CQ processing to a different context and does
66  * not ask for completion interrupts from the HCA.
67  * Using direct processing on CQ with non IB_POLL_DIRECT type may trigger
68  * concurrent processing.
69  *
70  * Note: do not pass -1 as %budget unless it is guaranteed that the number
71  * of completions that will be processed is small.
72  */
73 int ib_process_cq_direct(struct ib_cq *cq, int budget)
74 {
75         struct ib_wc wcs[IB_POLL_BATCH_DIRECT];
76
77         return __ib_process_cq(cq, budget, wcs, IB_POLL_BATCH_DIRECT);
78 }
79 EXPORT_SYMBOL(ib_process_cq_direct);
80
81 static void ib_cq_completion_direct(struct ib_cq *cq, void *private)
82 {
83         WARN_ONCE(1, "got unsolicited completion for CQ 0x%p\n", cq);
84 }
85
86 static int ib_poll_handler(struct irq_poll *iop, int budget)
87 {
88         struct ib_cq *cq = container_of(iop, struct ib_cq, iop);
89         int completed;
90
91         completed = __ib_process_cq(cq, budget, cq->wc, IB_POLL_BATCH);
92         if (completed < budget) {
93                 irq_poll_complete(&cq->iop);
94                 if (ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0)
95                         irq_poll_sched(&cq->iop);
96         }
97
98         return completed;
99 }
100
101 static void ib_cq_completion_softirq(struct ib_cq *cq, void *private)
102 {
103         irq_poll_sched(&cq->iop);
104 }
105
106 static void ib_cq_poll_work(struct work_struct *work)
107 {
108         struct ib_cq *cq = container_of(work, struct ib_cq, work);
109         int completed;
110
111         completed = __ib_process_cq(cq, IB_POLL_BUDGET_WORKQUEUE, cq->wc,
112                                     IB_POLL_BATCH);
113         if (completed >= IB_POLL_BUDGET_WORKQUEUE ||
114             ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0)
115                 queue_work(cq->comp_wq, &cq->work);
116 }
117
118 static void ib_cq_completion_workqueue(struct ib_cq *cq, void *private)
119 {
120         queue_work(cq->comp_wq, &cq->work);
121 }
122
123 /**
124  * ib_alloc_cq - allocate a completion queue
125  * @dev:                device to allocate the CQ for
126  * @private:            driver private data, accessible from cq->cq_context
127  * @nr_cqe:             number of CQEs to allocate
128  * @comp_vector:        HCA completion vectors for this CQ
129  * @poll_ctx:           context to poll the CQ from.
130  *
131  * This is the proper interface to allocate a CQ for in-kernel users. A
132  * CQ allocated with this interface will automatically be polled from the
133  * specified context. The ULP must use wr->wr_cqe instead of wr->wr_id
134  * to use this CQ abstraction.
135  */
136 struct ib_cq *ib_alloc_cq(struct ib_device *dev, void *private,
137                 int nr_cqe, int comp_vector, enum ib_poll_context poll_ctx)
138 {
139         struct ib_cq_init_attr cq_attr = {
140                 .cqe            = nr_cqe,
141                 .comp_vector    = comp_vector,
142         };
143         struct ib_cq *cq;
144         int ret = -ENOMEM;
145
146         cq = dev->create_cq(dev, &cq_attr, NULL, NULL);
147         if (IS_ERR(cq))
148                 return cq;
149
150         cq->device = dev;
151         cq->uobject = NULL;
152         cq->event_handler = NULL;
153         cq->cq_context = private;
154         cq->poll_ctx = poll_ctx;
155         atomic_set(&cq->usecnt, 0);
156
157         cq->wc = kmalloc_array(IB_POLL_BATCH, sizeof(*cq->wc), GFP_KERNEL);
158         if (!cq->wc)
159                 goto out_destroy_cq;
160
161         switch (cq->poll_ctx) {
162         case IB_POLL_DIRECT:
163                 cq->comp_handler = ib_cq_completion_direct;
164                 break;
165         case IB_POLL_SOFTIRQ:
166                 cq->comp_handler = ib_cq_completion_softirq;
167
168                 irq_poll_init(&cq->iop, IB_POLL_BUDGET_IRQ, ib_poll_handler);
169                 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
170                 break;
171         case IB_POLL_WORKQUEUE:
172         case IB_POLL_UNBOUND_WORKQUEUE:
173                 cq->comp_handler = ib_cq_completion_workqueue;
174                 INIT_WORK(&cq->work, ib_cq_poll_work);
175                 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
176                 cq->comp_wq = (cq->poll_ctx == IB_POLL_WORKQUEUE) ?
177                                 ib_comp_wq : ib_comp_unbound_wq;
178                 break;
179         default:
180                 ret = -EINVAL;
181                 goto out_free_wc;
182         }
183
184         return cq;
185
186 out_free_wc:
187         kfree(cq->wc);
188 out_destroy_cq:
189         cq->device->destroy_cq(cq);
190         return ERR_PTR(ret);
191 }
192 EXPORT_SYMBOL(ib_alloc_cq);
193
194 /**
195  * ib_free_cq - free a completion queue
196  * @cq:         completion queue to free.
197  */
198 void ib_free_cq(struct ib_cq *cq)
199 {
200         int ret;
201
202         if (WARN_ON_ONCE(atomic_read(&cq->usecnt)))
203                 return;
204
205         switch (cq->poll_ctx) {
206         case IB_POLL_DIRECT:
207                 break;
208         case IB_POLL_SOFTIRQ:
209                 irq_poll_disable(&cq->iop);
210                 break;
211         case IB_POLL_WORKQUEUE:
212         case IB_POLL_UNBOUND_WORKQUEUE:
213                 cancel_work_sync(&cq->work);
214                 break;
215         default:
216                 WARN_ON_ONCE(1);
217         }
218
219         kfree(cq->wc);
220         ret = cq->device->destroy_cq(cq);
221         WARN_ON_ONCE(ret);
222 }
223 EXPORT_SYMBOL(ib_free_cq);