1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4 
5 #include <rte_alarm.h>
6 #include <rte_common.h>
7 #include <rte_eal.h>
8 #include <rte_interrupts.h>
9 
10 #include "otx2_common.h"
11 #include "otx2_irq.h"
12 
13 #ifdef RTE_EAL_VFIO
14 
15 #include <inttypes.h>
16 #include <linux/vfio.h>
17 #include <sys/eventfd.h>
18 #include <sys/ioctl.h>
19 #include <unistd.h>
20 
21 #define MAX_INTR_VEC_ID RTE_MAX_RXTX_INTR_VEC_ID
22 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
23 			      sizeof(int) * (MAX_INTR_VEC_ID))
24 
25 static int
irq_get_info(struct rte_intr_handle * intr_handle)26 irq_get_info(struct rte_intr_handle *intr_handle)
27 {
28 	struct vfio_irq_info irq = { .argsz = sizeof(irq) };
29 	int rc;
30 
31 	irq.index = VFIO_PCI_MSIX_IRQ_INDEX;
32 
33 	rc = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
34 	if (rc < 0) {
35 		otx2_err("Failed to get IRQ info rc=%d errno=%d", rc, errno);
36 		return rc;
37 	}
38 
39 	otx2_base_dbg("Flags=0x%x index=0x%x count=0x%x max_intr_vec_id=0x%x",
40 		      irq.flags, irq.index, irq.count, MAX_INTR_VEC_ID);
41 
42 	if (irq.count > MAX_INTR_VEC_ID) {
43 		otx2_err("HW max=%d > MAX_INTR_VEC_ID: %d",
44 			 intr_handle->max_intr, MAX_INTR_VEC_ID);
45 		intr_handle->max_intr = MAX_INTR_VEC_ID;
46 	} else {
47 		intr_handle->max_intr = irq.count;
48 	}
49 
50 	return 0;
51 }
52 
53 static int
irq_config(struct rte_intr_handle * intr_handle,unsigned int vec)54 irq_config(struct rte_intr_handle *intr_handle, unsigned int vec)
55 {
56 	char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
57 	struct vfio_irq_set *irq_set;
58 	int32_t *fd_ptr;
59 	int len, rc;
60 
61 	if (vec > intr_handle->max_intr) {
62 		otx2_err("vector=%d greater than max_intr=%d", vec,
63 				intr_handle->max_intr);
64 		return -EINVAL;
65 	}
66 
67 	len = sizeof(struct vfio_irq_set) + sizeof(int32_t);
68 
69 	irq_set = (struct vfio_irq_set *)irq_set_buf;
70 	irq_set->argsz = len;
71 
72 	irq_set->start = vec;
73 	irq_set->count = 1;
74 	irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
75 			VFIO_IRQ_SET_ACTION_TRIGGER;
76 	irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
77 
78 	/* Use vec fd to set interrupt vectors */
79 	fd_ptr = (int32_t *)&irq_set->data[0];
80 	fd_ptr[0] = intr_handle->efds[vec];
81 
82 	rc = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
83 	if (rc)
84 		otx2_err("Failed to set_irqs vector=0x%x rc=%d", vec, rc);
85 
86 	return rc;
87 }
88 
89 static int
irq_init(struct rte_intr_handle * intr_handle)90 irq_init(struct rte_intr_handle *intr_handle)
91 {
92 	char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
93 	struct vfio_irq_set *irq_set;
94 	int32_t *fd_ptr;
95 	int len, rc;
96 	uint32_t i;
97 
98 	if (intr_handle->max_intr > MAX_INTR_VEC_ID) {
99 		otx2_err("Max_intr=%d greater than MAX_INTR_VEC_ID=%d",
100 				intr_handle->max_intr, MAX_INTR_VEC_ID);
101 		return -ERANGE;
102 	}
103 
104 	len = sizeof(struct vfio_irq_set) +
105 		sizeof(int32_t) * intr_handle->max_intr;
106 
107 	irq_set = (struct vfio_irq_set *)irq_set_buf;
108 	irq_set->argsz = len;
109 	irq_set->start = 0;
110 	irq_set->count = intr_handle->max_intr;
111 	irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
112 			VFIO_IRQ_SET_ACTION_TRIGGER;
113 	irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
114 
115 	fd_ptr = (int32_t *)&irq_set->data[0];
116 	for (i = 0; i < irq_set->count; i++)
117 		fd_ptr[i] = -1;
118 
119 	rc = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
120 	if (rc)
121 		otx2_err("Failed to set irqs vector rc=%d", rc);
122 
123 	return rc;
124 }
125 
126 /**
127  * @internal
128  * Disable IRQ
129  */
130 int
otx2_disable_irqs(struct rte_intr_handle * intr_handle)131 otx2_disable_irqs(struct rte_intr_handle *intr_handle)
132 {
133 	/* Clear max_intr to indicate re-init next time */
134 	intr_handle->max_intr = 0;
135 	return rte_intr_disable(intr_handle);
136 }
137 
138 /**
139  * @internal
140  * Register IRQ
141  */
142 int
otx2_register_irq(struct rte_intr_handle * intr_handle,rte_intr_callback_fn cb,void * data,unsigned int vec)143 otx2_register_irq(struct rte_intr_handle *intr_handle,
144 		  rte_intr_callback_fn cb, void *data, unsigned int vec)
145 {
146 	struct rte_intr_handle tmp_handle;
147 	int rc;
148 
149 	/* If no max_intr read from VFIO */
150 	if (intr_handle->max_intr == 0) {
151 		irq_get_info(intr_handle);
152 		irq_init(intr_handle);
153 	}
154 
155 	if (vec > intr_handle->max_intr) {
156 		otx2_err("Vector=%d greater than max_intr=%d", vec,
157 				 intr_handle->max_intr);
158 		return -EINVAL;
159 	}
160 
161 	tmp_handle = *intr_handle;
162 	/* Create new eventfd for interrupt vector */
163 	tmp_handle.fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
164 	if (tmp_handle.fd == -1)
165 		return -ENODEV;
166 
167 	/* Register vector interrupt callback */
168 	rc = rte_intr_callback_register(&tmp_handle, cb, data);
169 	if (rc) {
170 		otx2_err("Failed to register vector:0x%x irq callback.", vec);
171 		return rc;
172 	}
173 
174 	intr_handle->efds[vec] = tmp_handle.fd;
175 	intr_handle->nb_efd = (vec > intr_handle->nb_efd) ?
176 			vec : intr_handle->nb_efd;
177 	if ((intr_handle->nb_efd + 1) > intr_handle->max_intr)
178 		intr_handle->max_intr = intr_handle->nb_efd + 1;
179 
180 	otx2_base_dbg("Enable vector:0x%x for vfio (efds: %d, max:%d)",
181 		vec, intr_handle->nb_efd, intr_handle->max_intr);
182 
183 	/* Enable MSIX vectors to VFIO */
184 	return irq_config(intr_handle, vec);
185 }
186 
187 /**
188  * @internal
189  * Unregister IRQ
190  */
191 void
otx2_unregister_irq(struct rte_intr_handle * intr_handle,rte_intr_callback_fn cb,void * data,unsigned int vec)192 otx2_unregister_irq(struct rte_intr_handle *intr_handle,
193 		    rte_intr_callback_fn cb, void *data, unsigned int vec)
194 {
195 	struct rte_intr_handle tmp_handle;
196 	uint8_t retries = 5; /* 5 ms */
197 	int rc;
198 
199 	if (vec > intr_handle->max_intr) {
200 		otx2_err("Error unregistering MSI-X interrupts vec:%d > %d",
201 			vec, intr_handle->max_intr);
202 		return;
203 	}
204 
205 	tmp_handle = *intr_handle;
206 	tmp_handle.fd = intr_handle->efds[vec];
207 	if (tmp_handle.fd == -1)
208 		return;
209 
210 	do {
211 		/* Un-register callback func from eal lib */
212 		rc = rte_intr_callback_unregister(&tmp_handle, cb, data);
213 		/* Retry only if -EAGAIN */
214 		if (rc != -EAGAIN)
215 			break;
216 		rte_delay_ms(1);
217 		retries--;
218 	} while (retries);
219 
220 	if (rc < 0) {
221 		otx2_err("Error unregistering MSI-X intr vec %d cb, rc=%d",
222 			 vec, rc);
223 		return;
224 	}
225 
226 	otx2_base_dbg("Disable vector:0x%x for vfio (efds: %d, max:%d)",
227 			vec, intr_handle->nb_efd, intr_handle->max_intr);
228 
229 	if (intr_handle->efds[vec] != -1)
230 		close(intr_handle->efds[vec]);
231 	/* Disable MSIX vectors from VFIO */
232 	intr_handle->efds[vec] = -1;
233 	irq_config(intr_handle, vec);
234 }
235 
236 #else
237 
238 /**
239  * @internal
240  * Register IRQ
241  */
otx2_register_irq(__rte_unused struct rte_intr_handle * intr_handle,__rte_unused rte_intr_callback_fn cb,__rte_unused void * data,__rte_unused unsigned int vec)242 int otx2_register_irq(__rte_unused struct rte_intr_handle *intr_handle,
243 		      __rte_unused rte_intr_callback_fn cb,
244 		      __rte_unused void *data, __rte_unused unsigned int vec)
245 {
246 	return -ENOTSUP;
247 }
248 
249 
250 /**
251  * @internal
252  * Unregister IRQ
253  */
otx2_unregister_irq(__rte_unused struct rte_intr_handle * intr_handle,__rte_unused rte_intr_callback_fn cb,__rte_unused void * data,__rte_unused unsigned int vec)254 void otx2_unregister_irq(__rte_unused struct rte_intr_handle *intr_handle,
255 			 __rte_unused rte_intr_callback_fn cb,
256 			 __rte_unused void *data, __rte_unused unsigned int vec)
257 {
258 }
259 
260 /**
261  * @internal
262  * Disable IRQ
263  */
otx2_disable_irqs(__rte_unused struct rte_intr_handle * intr_handle)264 int otx2_disable_irqs(__rte_unused struct rte_intr_handle *intr_handle)
265 {
266 	return -ENOTSUP;
267 }
268 
269 #endif /* RTE_EAL_VFIO */
270