1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/module.h>
14#include <linux/err.h>
15#include <linux/slab.h>
16#include <rdma/ib_verbs.h>
17
18
19#define IB_POLL_BATCH 16
20
21
22#define IB_POLL_BUDGET_IRQ 256
23#define IB_POLL_BUDGET_WORKQUEUE 65536
24
25#define IB_POLL_FLAGS \
26 (IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS)
27
28static int __ib_process_cq(struct ib_cq *cq, int budget)
29{
30 int i, n, completed = 0;
31
32
33
34
35
36
37 while ((n = ib_poll_cq(cq, min_t(u32, IB_POLL_BATCH,
38 budget - completed), cq->wc)) > 0) {
39 for (i = 0; i < n; i++) {
40 struct ib_wc *wc = &cq->wc[i];
41
42 if (wc->wr_cqe)
43 wc->wr_cqe->done(cq, wc);
44 else
45 WARN_ON_ONCE(wc->status == IB_WC_SUCCESS);
46 }
47
48 completed += n;
49
50 if (n != IB_POLL_BATCH ||
51 (budget != -1 && completed >= budget))
52 break;
53 }
54
55 return completed;
56}
57
58
59
60
61
62
63
64
65
66
67
68
69
70int ib_process_cq_direct(struct ib_cq *cq, int budget)
71{
72 WARN_ON_ONCE(cq->poll_ctx != IB_POLL_DIRECT);
73
74 return __ib_process_cq(cq, budget);
75}
76EXPORT_SYMBOL(ib_process_cq_direct);
77
78static void ib_cq_completion_direct(struct ib_cq *cq, void *private)
79{
80 WARN_ONCE(1, "got unsolicited completion for CQ 0x%p\n", cq);
81}
82
83static int ib_poll_handler(struct irq_poll *iop, int budget)
84{
85 struct ib_cq *cq = container_of(iop, struct ib_cq, iop);
86 int completed;
87
88 completed = __ib_process_cq(cq, budget);
89 if (completed < budget) {
90 irq_poll_complete(&cq->iop);
91 if (ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0)
92 irq_poll_sched(&cq->iop);
93 }
94
95 return completed;
96}
97
98static void ib_cq_completion_softirq(struct ib_cq *cq, void *private)
99{
100 irq_poll_sched(&cq->iop);
101}
102
103static void ib_cq_poll_work(struct work_struct *work)
104{
105 struct ib_cq *cq = container_of(work, struct ib_cq, work);
106 int completed;
107
108 completed = __ib_process_cq(cq, IB_POLL_BUDGET_WORKQUEUE);
109 if (completed >= IB_POLL_BUDGET_WORKQUEUE ||
110 ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0)
111 queue_work(ib_comp_wq, &cq->work);
112}
113
114static void ib_cq_completion_workqueue(struct ib_cq *cq, void *private)
115{
116 queue_work(ib_comp_wq, &cq->work);
117}
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132struct ib_cq *ib_alloc_cq(struct ib_device *dev, void *private,
133 int nr_cqe, int comp_vector, enum ib_poll_context poll_ctx)
134{
135 struct ib_cq_init_attr cq_attr = {
136 .cqe = nr_cqe,
137 .comp_vector = comp_vector,
138 };
139 struct ib_cq *cq;
140 int ret = -ENOMEM;
141
142 cq = dev->create_cq(dev, &cq_attr, NULL, NULL);
143 if (IS_ERR(cq))
144 return cq;
145
146 cq->device = dev;
147 cq->uobject = NULL;
148 cq->event_handler = NULL;
149 cq->cq_context = private;
150 cq->poll_ctx = poll_ctx;
151 atomic_set(&cq->usecnt, 0);
152
153 cq->wc = kmalloc_array(IB_POLL_BATCH, sizeof(*cq->wc), GFP_KERNEL);
154 if (!cq->wc)
155 goto out_destroy_cq;
156
157 switch (cq->poll_ctx) {
158 case IB_POLL_DIRECT:
159 cq->comp_handler = ib_cq_completion_direct;
160 break;
161 case IB_POLL_SOFTIRQ:
162 cq->comp_handler = ib_cq_completion_softirq;
163
164 irq_poll_init(&cq->iop, IB_POLL_BUDGET_IRQ, ib_poll_handler);
165 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
166 break;
167 case IB_POLL_WORKQUEUE:
168 cq->comp_handler = ib_cq_completion_workqueue;
169 INIT_WORK(&cq->work, ib_cq_poll_work);
170 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
171 break;
172 default:
173 ret = -EINVAL;
174 goto out_free_wc;
175 }
176
177 return cq;
178
179out_free_wc:
180 kfree(cq->wc);
181out_destroy_cq:
182 cq->device->destroy_cq(cq);
183 return ERR_PTR(ret);
184}
185EXPORT_SYMBOL(ib_alloc_cq);
186
187
188
189
190
191void ib_free_cq(struct ib_cq *cq)
192{
193 int ret;
194
195 if (WARN_ON_ONCE(atomic_read(&cq->usecnt)))
196 return;
197
198 switch (cq->poll_ctx) {
199 case IB_POLL_DIRECT:
200 break;
201 case IB_POLL_SOFTIRQ:
202 irq_poll_disable(&cq->iop);
203 break;
204 case IB_POLL_WORKQUEUE:
205 cancel_work_sync(&cq->work);
206 break;
207 default:
208 WARN_ON_ONCE(1);
209 }
210
211 kfree(cq->wc);
212 ret = cq->device->destroy_cq(cq);
213 WARN_ON_ONCE(ret);
214}
215EXPORT_SYMBOL(ib_free_cq);
216