1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 HGST, a Western Digital Company.
5 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <rdma/ib_verbs.h>
10 #include <trace/events/rdma_core.h>
12 /* # of WCs to poll for with a single call to ib_poll_cq */
13 #define IB_POLL_BATCH 16
14 #define IB_POLL_BATCH_DIRECT 8
16 /* # of WCs to iterate over before yielding */
17 #define IB_POLL_BUDGET_IRQ 256
18 #define IB_POLL_BUDGET_WORKQUEUE 65536
20 #define IB_POLL_FLAGS \
21 (IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS)
23 static const struct dim_cq_moder
24 rdma_dim_prof
[RDMA_DIM_PARAMS_NUM_PROFILES
] = {
36 static void ib_cq_rdma_dim_work(struct work_struct
*w
)
38 struct dim
*dim
= container_of(w
, struct dim
, work
);
39 struct ib_cq
*cq
= dim
->priv
;
41 u16 usec
= rdma_dim_prof
[dim
->profile_ix
].usec
;
42 u16 comps
= rdma_dim_prof
[dim
->profile_ix
].comps
;
44 dim
->state
= DIM_START_MEASURE
;
46 trace_cq_modify(cq
, comps
, usec
);
47 cq
->device
->ops
.modify_cq(cq
, comps
, usec
);
50 static void rdma_dim_init(struct ib_cq
*cq
)
54 if (!cq
->device
->ops
.modify_cq
|| !cq
->device
->use_cq_dim
||
55 cq
->poll_ctx
== IB_POLL_DIRECT
)
58 dim
= kzalloc(sizeof(struct dim
), GFP_KERNEL
);
62 dim
->state
= DIM_START_MEASURE
;
63 dim
->tune_state
= DIM_GOING_RIGHT
;
64 dim
->profile_ix
= RDMA_DIM_START_PROFILE
;
68 INIT_WORK(&dim
->work
, ib_cq_rdma_dim_work
);
71 static int __poll_cq(struct ib_cq
*cq
, int num_entries
, struct ib_wc
*wc
)
75 rc
= ib_poll_cq(cq
, num_entries
, wc
);
76 trace_cq_poll(cq
, num_entries
, rc
);
80 static int __ib_process_cq(struct ib_cq
*cq
, int budget
, struct ib_wc
*wcs
,
83 int i
, n
, completed
= 0;
88 * budget might be (-1) if the caller does not
89 * want to bound this call, thus we need unsigned
92 while ((n
= __poll_cq(cq
, min_t(u32
, batch
,
93 budget
- completed
), wcs
)) > 0) {
94 for (i
= 0; i
< n
; i
++) {
95 struct ib_wc
*wc
= &wcs
[i
];
98 wc
->wr_cqe
->done(cq
, wc
);
100 WARN_ON_ONCE(wc
->status
== IB_WC_SUCCESS
);
105 if (n
!= batch
|| (budget
!= -1 && completed
>= budget
))
113 * ib_process_direct_cq - process a CQ in caller context
115 * @budget: number of CQEs to poll for
117 * This function is used to process all outstanding CQ entries.
118 * It does not offload CQ processing to a different context and does
119 * not ask for completion interrupts from the HCA.
120 * Using direct processing on CQ with non IB_POLL_DIRECT type may trigger
121 * concurrent processing.
123 * Note: do not pass -1 as %budget unless it is guaranteed that the number
124 * of completions that will be processed is small.
126 int ib_process_cq_direct(struct ib_cq
*cq
, int budget
)
128 struct ib_wc wcs
[IB_POLL_BATCH_DIRECT
];
130 return __ib_process_cq(cq
, budget
, wcs
, IB_POLL_BATCH_DIRECT
);
132 EXPORT_SYMBOL(ib_process_cq_direct
);
134 static void ib_cq_completion_direct(struct ib_cq
*cq
, void *private)
136 WARN_ONCE(1, "got unsolicited completion for CQ 0x%p\n", cq
);
139 static int ib_poll_handler(struct irq_poll
*iop
, int budget
)
141 struct ib_cq
*cq
= container_of(iop
, struct ib_cq
, iop
);
142 struct dim
*dim
= cq
->dim
;
145 completed
= __ib_process_cq(cq
, budget
, cq
->wc
, IB_POLL_BATCH
);
146 if (completed
< budget
) {
147 irq_poll_complete(&cq
->iop
);
148 if (ib_req_notify_cq(cq
, IB_POLL_FLAGS
) > 0) {
149 trace_cq_reschedule(cq
);
150 irq_poll_sched(&cq
->iop
);
155 rdma_dim(dim
, completed
);
160 static void ib_cq_completion_softirq(struct ib_cq
*cq
, void *private)
162 trace_cq_schedule(cq
);
163 irq_poll_sched(&cq
->iop
);
166 static void ib_cq_poll_work(struct work_struct
*work
)
168 struct ib_cq
*cq
= container_of(work
, struct ib_cq
, work
);
171 completed
= __ib_process_cq(cq
, IB_POLL_BUDGET_WORKQUEUE
, cq
->wc
,
173 if (completed
>= IB_POLL_BUDGET_WORKQUEUE
||
174 ib_req_notify_cq(cq
, IB_POLL_FLAGS
) > 0)
175 queue_work(cq
->comp_wq
, &cq
->work
);
177 rdma_dim(cq
->dim
, completed
);
180 static void ib_cq_completion_workqueue(struct ib_cq
*cq
, void *private)
182 trace_cq_schedule(cq
);
183 queue_work(cq
->comp_wq
, &cq
->work
);
187 * __ib_alloc_cq_user - allocate a completion queue
188 * @dev: device to allocate the CQ for
189 * @private: driver private data, accessible from cq->cq_context
190 * @nr_cqe: number of CQEs to allocate
191 * @comp_vector: HCA completion vectors for this CQ
192 * @poll_ctx: context to poll the CQ from.
193 * @caller: module owner name.
194 * @udata: Valid user data or NULL for kernel object
196 * This is the proper interface to allocate a CQ for in-kernel users. A
197 * CQ allocated with this interface will automatically be polled from the
198 * specified context. The ULP must use wr->wr_cqe instead of wr->wr_id
199 * to use this CQ abstraction.
201 struct ib_cq
*__ib_alloc_cq_user(struct ib_device
*dev
, void *private,
202 int nr_cqe
, int comp_vector
,
203 enum ib_poll_context poll_ctx
,
204 const char *caller
, struct ib_udata
*udata
)
206 struct ib_cq_init_attr cq_attr
= {
208 .comp_vector
= comp_vector
,
213 cq
= rdma_zalloc_drv_obj(dev
, ib_cq
);
218 cq
->cq_context
= private;
219 cq
->poll_ctx
= poll_ctx
;
220 atomic_set(&cq
->usecnt
, 0);
222 cq
->wc
= kmalloc_array(IB_POLL_BATCH
, sizeof(*cq
->wc
), GFP_KERNEL
);
226 cq
->res
.type
= RDMA_RESTRACK_CQ
;
227 rdma_restrack_set_task(&cq
->res
, caller
);
229 ret
= dev
->ops
.create_cq(cq
, &cq_attr
, NULL
);
233 rdma_restrack_kadd(&cq
->res
);
237 switch (cq
->poll_ctx
) {
239 cq
->comp_handler
= ib_cq_completion_direct
;
241 case IB_POLL_SOFTIRQ
:
242 cq
->comp_handler
= ib_cq_completion_softirq
;
244 irq_poll_init(&cq
->iop
, IB_POLL_BUDGET_IRQ
, ib_poll_handler
);
245 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
247 case IB_POLL_WORKQUEUE
:
248 case IB_POLL_UNBOUND_WORKQUEUE
:
249 cq
->comp_handler
= ib_cq_completion_workqueue
;
250 INIT_WORK(&cq
->work
, ib_cq_poll_work
);
251 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
252 cq
->comp_wq
= (cq
->poll_ctx
== IB_POLL_WORKQUEUE
) ?
253 ib_comp_wq
: ib_comp_unbound_wq
;
260 trace_cq_alloc(cq
, nr_cqe
, comp_vector
, poll_ctx
);
264 rdma_restrack_del(&cq
->res
);
265 cq
->device
->ops
.destroy_cq(cq
, udata
);
270 trace_cq_alloc_error(nr_cqe
, comp_vector
, poll_ctx
, ret
);
273 EXPORT_SYMBOL(__ib_alloc_cq_user
);
276 * __ib_alloc_cq_any - allocate a completion queue
277 * @dev: device to allocate the CQ for
278 * @private: driver private data, accessible from cq->cq_context
279 * @nr_cqe: number of CQEs to allocate
280 * @poll_ctx: context to poll the CQ from
281 * @caller: module owner name
283 * Attempt to spread ULP Completion Queues over each device's interrupt
284 * vectors. A simple best-effort mechanism is used.
286 struct ib_cq
*__ib_alloc_cq_any(struct ib_device
*dev
, void *private,
287 int nr_cqe
, enum ib_poll_context poll_ctx
,
290 static atomic_t counter
;
293 if (dev
->num_comp_vectors
> 1)
295 atomic_inc_return(&counter
) %
296 min_t(int, dev
->num_comp_vectors
, num_online_cpus());
298 return __ib_alloc_cq_user(dev
, private, nr_cqe
, comp_vector
, poll_ctx
,
301 EXPORT_SYMBOL(__ib_alloc_cq_any
);
304 * ib_free_cq_user - free a completion queue
305 * @cq: completion queue to free.
306 * @udata: User data or NULL for kernel object
308 void ib_free_cq_user(struct ib_cq
*cq
, struct ib_udata
*udata
)
310 if (WARN_ON_ONCE(atomic_read(&cq
->usecnt
)))
313 switch (cq
->poll_ctx
) {
316 case IB_POLL_SOFTIRQ
:
317 irq_poll_disable(&cq
->iop
);
319 case IB_POLL_WORKQUEUE
:
320 case IB_POLL_UNBOUND_WORKQUEUE
:
321 cancel_work_sync(&cq
->work
);
328 rdma_restrack_del(&cq
->res
);
329 cq
->device
->ops
.destroy_cq(cq
, udata
);
331 cancel_work_sync(&cq
->dim
->work
);
336 EXPORT_SYMBOL(ib_free_cq_user
);