xref: /freebsd/sys/ofed/drivers/infiniband/core/ib_cq.c (revision 190cef3d)
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 __FBSDID("$FreeBSD$");
37 
38 #include <linux/module.h>
39 #include <linux/err.h>
40 #include <linux/slab.h>
41 
42 #include <rdma/ib_verbs.h>
43 
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)
48 
49 static void
50 ib_cq_poll_work(struct work_struct *work)
51 {
52 	struct ib_wc ib_wc[IB_CQ_POLL_MAX];
53 	struct ib_cq *cq = container_of(work, struct ib_cq, work);
54 	int total = 0;
55 	int i;
56 	int n;
57 
58 	while (1) {
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;
62 
63 			if (wc->wr_cqe != NULL)
64 				wc->wr_cqe->done(cq, wc);
65 		}
66 
67 		if (n != IB_CQ_POLL_MAX) {
68 			if (ib_req_notify_cq(cq, IB_CQ_POLL_FLAGS) > 0)
69 				break;
70 			else
71 				return;
72 		}
73 		total += n;
74 		if (total >= IB_CQ_POLL_BUDGET)
75 			break;
76 	}
77 
78 	/* give other work structs a chance */
79 	queue_work(ib_comp_wq, &cq->work);
80 }
81 
82 static void
83 ib_cq_completion_workqueue(struct ib_cq *cq, void *private)
84 {
85 	queue_work(ib_comp_wq, &cq->work);
86 }
87 
88 struct ib_cq *
89 ib_alloc_cq(struct ib_device *dev, void *private,
90     int nr_cqe, int comp_vector, enum ib_poll_context poll_ctx)
91 {
92 	struct ib_cq_init_attr cq_attr = {
93 		.cqe = nr_cqe,
94 		.comp_vector = comp_vector,
95 	};
96 	struct ib_cq *cq;
97 
98 	/*
99 	 * Check for invalid parameters early on to avoid
100 	 * extra error handling code:
101 	 */
102 	switch (poll_ctx) {
103 	case IB_POLL_DIRECT:
104 	case IB_POLL_SOFTIRQ:
105 	case IB_POLL_WORKQUEUE:
106 		break;
107 	default:
108 		return (ERR_PTR(-EINVAL));
109 	}
110 
111 	cq = dev->create_cq(dev, &cq_attr, NULL, NULL);
112 	if (IS_ERR(cq))
113 		return (cq);
114 
115 	cq->device = dev;
116 	cq->uobject = NULL;
117 	cq->event_handler = NULL;
118 	cq->cq_context = private;
119 	cq->poll_ctx = poll_ctx;
120 	atomic_set(&cq->usecnt, 0);
121 
122 	switch (poll_ctx) {
123 	case IB_POLL_DIRECT:
124 		cq->comp_handler = NULL;	/* no hardware completions */
125 		break;
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);
131 		break;
132 	default:
133 		break;
134 	}
135 	return (cq);
136 }
137 EXPORT_SYMBOL(ib_alloc_cq);
138 
139 void
140 ib_free_cq(struct ib_cq *cq)
141 {
142 
143 	if (WARN_ON_ONCE(atomic_read(&cq->usecnt) != 0))
144 		return;
145 
146 	switch (cq->poll_ctx) {
147 	case IB_POLL_DIRECT:
148 		break;
149 	case IB_POLL_SOFTIRQ:
150 	case IB_POLL_WORKQUEUE:
151 		flush_work(&cq->work);
152 		break;
153 	default:
154 		break;
155 	}
156 
157 	(void)cq->device->destroy_cq(cq);
158 }
159 EXPORT_SYMBOL(ib_free_cq);
160