1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3 *
4 * Copyright (c) 2017 Mellanox Technologies Ltd. All rights reserved.
5 *
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:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
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.
24 *
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
32 * SOFTWARE.
33 */
34
35 #include <sys/cdefs.h>
36 #include <linux/module.h>
37 #include <linux/err.h>
38 #include <linux/slab.h>
39
40 #include <rdma/ib_verbs.h>
41
42 #define IB_CQ_POLL_MAX 16
43 /* maximum number of completions per poll loop */
44 #define IB_CQ_POLL_BUDGET 65536
45 #define IB_CQ_POLL_FLAGS (IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS)
46
47 static void
ib_cq_poll_work(struct work_struct * work)48 ib_cq_poll_work(struct work_struct *work)
49 {
50 struct ib_wc ib_wc[IB_CQ_POLL_MAX];
51 struct ib_cq *cq = container_of(work, struct ib_cq, work);
52 int total = 0;
53 int i;
54 int n;
55
56 while (1) {
57 n = ib_poll_cq(cq, IB_CQ_POLL_MAX, ib_wc);
58 for (i = 0; i < n; i++) {
59 struct ib_wc *wc = ib_wc + i;
60
61 if (wc->wr_cqe != NULL)
62 wc->wr_cqe->done(cq, wc);
63 }
64
65 if (n != IB_CQ_POLL_MAX) {
66 if (ib_req_notify_cq(cq, IB_CQ_POLL_FLAGS) > 0)
67 break;
68 else
69 return;
70 }
71 total += n;
72 if (total >= IB_CQ_POLL_BUDGET)
73 break;
74 }
75
76 /* give other work structs a chance */
77 queue_work(ib_comp_wq, &cq->work);
78 }
79
80 static void
ib_cq_completion_workqueue(struct ib_cq * cq,void * private)81 ib_cq_completion_workqueue(struct ib_cq *cq, void *private)
82 {
83 queue_work(ib_comp_wq, &cq->work);
84 }
85
86 struct ib_cq *
ib_alloc_cq(struct ib_device * dev,void * private,int nr_cqe,int comp_vector,enum ib_poll_context poll_ctx)87 ib_alloc_cq(struct ib_device *dev, void *private,
88 int nr_cqe, int comp_vector, enum ib_poll_context poll_ctx)
89 {
90 struct ib_cq_init_attr cq_attr = {
91 .cqe = nr_cqe,
92 .comp_vector = comp_vector,
93 };
94 struct ib_cq *cq;
95
96 /*
97 * Check for invalid parameters early on to avoid
98 * extra error handling code:
99 */
100 switch (poll_ctx) {
101 case IB_POLL_DIRECT:
102 case IB_POLL_SOFTIRQ:
103 case IB_POLL_WORKQUEUE:
104 break;
105 default:
106 return (ERR_PTR(-EINVAL));
107 }
108
109 cq = dev->create_cq(dev, &cq_attr, NULL, NULL);
110 if (IS_ERR(cq))
111 return (cq);
112
113 cq->device = dev;
114 cq->uobject = NULL;
115 cq->event_handler = NULL;
116 cq->cq_context = private;
117 cq->poll_ctx = poll_ctx;
118 atomic_set(&cq->usecnt, 0);
119
120 switch (poll_ctx) {
121 case IB_POLL_DIRECT:
122 cq->comp_handler = NULL; /* no hardware completions */
123 break;
124 case IB_POLL_SOFTIRQ:
125 case IB_POLL_WORKQUEUE:
126 cq->comp_handler = ib_cq_completion_workqueue;
127 INIT_WORK(&cq->work, ib_cq_poll_work);
128 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
129 break;
130 default:
131 break;
132 }
133 return (cq);
134 }
135 EXPORT_SYMBOL(ib_alloc_cq);
136
137 void
ib_free_cq(struct ib_cq * cq)138 ib_free_cq(struct ib_cq *cq)
139 {
140
141 if (WARN_ON_ONCE(atomic_read(&cq->usecnt) != 0))
142 return;
143
144 switch (cq->poll_ctx) {
145 case IB_POLL_DIRECT:
146 break;
147 case IB_POLL_SOFTIRQ:
148 case IB_POLL_WORKQUEUE:
149 flush_work(&cq->work);
150 break;
151 default:
152 break;
153 }
154
155 (void)cq->device->destroy_cq(cq);
156 }
157 EXPORT_SYMBOL(ib_free_cq);
158