xref: /openbmc/linux/drivers/infiniband/core/cq.c (revision f519f0be)
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  * @caller:		module owner name.
131  * @udata:		Valid user data or NULL for kernel object
132  *
133  * This is the proper interface to allocate a CQ for in-kernel users. A
134  * CQ allocated with this interface will automatically be polled from the
135  * specified context. The ULP must use wr->wr_cqe instead of wr->wr_id
136  * to use this CQ abstraction.
137  */
138 struct ib_cq *__ib_alloc_cq_user(struct ib_device *dev, void *private,
139 				 int nr_cqe, int comp_vector,
140 				 enum ib_poll_context poll_ctx,
141 				 const char *caller, struct ib_udata *udata)
142 {
143 	struct ib_cq_init_attr cq_attr = {
144 		.cqe		= nr_cqe,
145 		.comp_vector	= comp_vector,
146 	};
147 	struct ib_cq *cq;
148 	int ret = -ENOMEM;
149 
150 	cq = dev->ops.create_cq(dev, &cq_attr, NULL);
151 	if (IS_ERR(cq))
152 		return cq;
153 
154 	cq->device = dev;
155 	cq->uobject = NULL;
156 	cq->event_handler = NULL;
157 	cq->cq_context = private;
158 	cq->poll_ctx = poll_ctx;
159 	atomic_set(&cq->usecnt, 0);
160 
161 	cq->wc = kmalloc_array(IB_POLL_BATCH, sizeof(*cq->wc), GFP_KERNEL);
162 	if (!cq->wc)
163 		goto out_destroy_cq;
164 
165 	cq->res.type = RDMA_RESTRACK_CQ;
166 	rdma_restrack_set_task(&cq->res, caller);
167 	rdma_restrack_kadd(&cq->res);
168 
169 	switch (cq->poll_ctx) {
170 	case IB_POLL_DIRECT:
171 		cq->comp_handler = ib_cq_completion_direct;
172 		break;
173 	case IB_POLL_SOFTIRQ:
174 		cq->comp_handler = ib_cq_completion_softirq;
175 
176 		irq_poll_init(&cq->iop, IB_POLL_BUDGET_IRQ, ib_poll_handler);
177 		ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
178 		break;
179 	case IB_POLL_WORKQUEUE:
180 	case IB_POLL_UNBOUND_WORKQUEUE:
181 		cq->comp_handler = ib_cq_completion_workqueue;
182 		INIT_WORK(&cq->work, ib_cq_poll_work);
183 		ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
184 		cq->comp_wq = (cq->poll_ctx == IB_POLL_WORKQUEUE) ?
185 				ib_comp_wq : ib_comp_unbound_wq;
186 		break;
187 	default:
188 		ret = -EINVAL;
189 		goto out_free_wc;
190 	}
191 
192 	return cq;
193 
194 out_free_wc:
195 	kfree(cq->wc);
196 	rdma_restrack_del(&cq->res);
197 out_destroy_cq:
198 	cq->device->ops.destroy_cq(cq, udata);
199 	return ERR_PTR(ret);
200 }
201 EXPORT_SYMBOL(__ib_alloc_cq_user);
202 
203 /**
204  * ib_free_cq - free a completion queue
205  * @cq:		completion queue to free.
206  * @udata:	User data or NULL for kernel object
207  */
208 void ib_free_cq_user(struct ib_cq *cq, struct ib_udata *udata)
209 {
210 	int ret;
211 
212 	if (WARN_ON_ONCE(atomic_read(&cq->usecnt)))
213 		return;
214 
215 	switch (cq->poll_ctx) {
216 	case IB_POLL_DIRECT:
217 		break;
218 	case IB_POLL_SOFTIRQ:
219 		irq_poll_disable(&cq->iop);
220 		break;
221 	case IB_POLL_WORKQUEUE:
222 	case IB_POLL_UNBOUND_WORKQUEUE:
223 		cancel_work_sync(&cq->work);
224 		break;
225 	default:
226 		WARN_ON_ONCE(1);
227 	}
228 
229 	kfree(cq->wc);
230 	rdma_restrack_del(&cq->res);
231 	ret = cq->device->ops.destroy_cq(cq, udata);
232 	WARN_ON_ONCE(ret);
233 }
234 EXPORT_SYMBOL(ib_free_cq_user);
235