2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
4 * Copyright (c) 2017 Mellanox Technologies Ltd. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <linux/module.h>
39 #include <linux/err.h>
40 #include <linux/slab.h>
42 #include <rdma/ib_verbs.h>
44 #define IB_CQ_POLL_MAX 16
45 /* maximum number of completions per poll loop */
46 #define IB_CQ_POLL_BUDGET 65536
47 #define IB_CQ_POLL_FLAGS (IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS)
50 ib_cq_poll_work(struct work_struct *work)
52 struct ib_wc ib_wc[IB_CQ_POLL_MAX];
53 struct ib_cq *cq = container_of(work, struct ib_cq, work);
59 n = ib_poll_cq(cq, IB_CQ_POLL_MAX, ib_wc);
60 for (i = 0; i < n; i++) {
61 struct ib_wc *wc = ib_wc + i;
63 if (wc->wr_cqe != NULL)
64 wc->wr_cqe->done(cq, wc);
67 if (n != IB_CQ_POLL_MAX) {
68 if (ib_req_notify_cq(cq, IB_CQ_POLL_FLAGS) > 0)
74 if (total >= IB_CQ_POLL_BUDGET)
78 /* give other work structs a chance */
79 queue_work(ib_comp_wq, &cq->work);
83 ib_cq_completion_workqueue(struct ib_cq *cq, void *private)
85 queue_work(ib_comp_wq, &cq->work);
89 ib_alloc_cq(struct ib_device *dev, void *private,
90 int nr_cqe, int comp_vector, enum ib_poll_context poll_ctx)
92 struct ib_cq_init_attr cq_attr = {
94 .comp_vector = comp_vector,
99 * Check for invalid parameters early on to avoid
100 * extra error handling code:
104 case IB_POLL_SOFTIRQ:
105 case IB_POLL_WORKQUEUE:
108 return (ERR_PTR(-EINVAL));
111 cq = dev->create_cq(dev, &cq_attr, NULL, NULL);
117 cq->event_handler = NULL;
118 cq->cq_context = private;
119 cq->poll_ctx = poll_ctx;
120 atomic_set(&cq->usecnt, 0);
124 cq->comp_handler = NULL; /* no hardware completions */
126 case IB_POLL_SOFTIRQ:
127 case IB_POLL_WORKQUEUE:
128 cq->comp_handler = ib_cq_completion_workqueue;
129 INIT_WORK(&cq->work, ib_cq_poll_work);
130 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
137 EXPORT_SYMBOL(ib_alloc_cq);
140 ib_free_cq(struct ib_cq *cq)
143 if (WARN_ON_ONCE(atomic_read(&cq->usecnt) != 0))
146 switch (cq->poll_ctx) {
149 case IB_POLL_SOFTIRQ:
150 case IB_POLL_WORKQUEUE:
151 flush_work(&cq->work);
157 (void)cq->device->destroy_cq(cq);
159 EXPORT_SYMBOL(ib_free_cq);