1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 Mellanox Technologies, Ltd
7 * Interrupts handling for failsafe driver.
11 #include <sys/epoll.h>
15 #include <rte_alarm.h>
16 #include <rte_errno.h>
17 #include <rte_ethdev.h>
18 #include <rte_interrupts.h>
20 #include <rte_service_component.h>
22 #include "failsafe_private.h"
24 #define NUM_RX_PROXIES (FAILSAFE_MAX_ETHPORTS * RTE_MAX_RXTX_INTR_VEC_ID)
28 * Open an epoll file descriptor.
31 * Flags for defining epoll behavior.
33 * 0 on success, negative errno value otherwise.
36 fs_epoll_create1(int flags)
39 return epoll_create1(flags);
47 * Install failsafe Rx event proxy service.
48 * The Rx event proxy is the service that listens to Rx events from the
49 * subdevices and triggers failsafe Rx events accordingly.
52 * Pointer to failsafe private structure.
54 * 0 on success, negative errno value otherwise.
57 fs_rx_event_proxy_routine(void *data)
61 struct rte_epoll_event *events;
68 events = priv->rxp.evec;
69 n = rte_epoll_wait(priv->rxp.efd, events, NUM_RX_PROXIES, -1);
70 for (i = 0; i < n; i++) {
71 rxq = events[i].epdata.data;
72 if (rxq->enable_events && rxq->event_fd != -1) {
73 if (write(rxq->event_fd, &u64, sizeof(u64)) !=
75 ERROR("Failed to proxy Rx event to socket %d",
85 * Uninstall failsafe Rx event proxy service.
88 * Pointer to failsafe private structure.
91 fs_rx_event_proxy_service_uninstall(struct fs_priv *priv)
93 /* Unregister the event service. */
94 switch (priv->rxp.sstate) {
96 rte_service_map_lcore_set(priv->rxp.sid, priv->rxp.scid, 0);
99 rte_service_runstate_set(priv->rxp.sid, 0);
100 rte_service_set_stats_enable(priv->rxp.sid, 0);
101 rte_service_component_runstate_set(priv->rxp.sid, 0);
104 rte_service_component_unregister(priv->rxp.sid);
112 * Install the failsafe Rx event proxy service.
115 * Pointer to failsafe private structure.
117 * 0 on success, negative errno value otherwise.
120 fs_rx_event_proxy_service_install(struct fs_priv *priv)
122 struct rte_service_spec service;
123 int32_t num_service_cores;
126 num_service_cores = rte_service_lcore_count();
127 if (num_service_cores <= 0) {
128 ERROR("Failed to install Rx interrupts, "
129 "no service core found");
132 /* prepare service info */
133 memset(&service, 0, sizeof(struct rte_service_spec));
134 snprintf(service.name, sizeof(service.name), "%s_Rx_service",
136 service.socket_id = priv->data->numa_node;
137 service.callback = fs_rx_event_proxy_routine;
138 service.callback_userdata = priv;
140 if (priv->rxp.sstate == SS_NO_SERVICE) {
141 uint32_t service_core_list[num_service_cores];
143 /* get a service core to work with */
144 ret = rte_service_lcore_list(service_core_list,
147 ERROR("Failed to install Rx interrupts, "
148 "service core list empty or corrupted");
151 priv->rxp.scid = service_core_list[0];
152 ret = rte_service_lcore_add(priv->rxp.scid);
153 if (ret && ret != -EALREADY) {
154 ERROR("Failed adding service core");
157 /* service core may be in "stopped" state, start it */
158 ret = rte_service_lcore_start(priv->rxp.scid);
159 if (ret && (ret != -EALREADY)) {
160 ERROR("Failed to install Rx interrupts, "
161 "service core not started");
164 /* register our service */
165 int32_t ret = rte_service_component_register(&service,
168 ERROR("service register() failed");
171 priv->rxp.sstate = SS_REGISTERED;
172 /* run the service */
173 ret = rte_service_component_runstate_set(priv->rxp.sid, 1);
175 ERROR("Failed Setting component runstate\n");
178 ret = rte_service_set_stats_enable(priv->rxp.sid, 1);
180 ERROR("Failed enabling stats\n");
183 ret = rte_service_runstate_set(priv->rxp.sid, 1);
185 ERROR("Failed to run service\n");
188 priv->rxp.sstate = SS_READY;
189 /* map the service with the service core */
190 ret = rte_service_map_lcore_set(priv->rxp.sid,
193 ERROR("Failed to install Rx interrupts, "
194 "could not map service core");
197 priv->rxp.sstate = SS_RUNNING;
203 * Install failsafe Rx event proxy subsystem.
204 * This is the way the failsafe PMD generates Rx events on behalf of its
208 * Pointer to failsafe private structure.
210 * 0 on success, negative errno value otherwise and rte_errno is set.
213 fs_rx_event_proxy_install(struct fs_priv *priv)
218 * Create the epoll fd and event vector for the proxy service to
219 * wait on for Rx events generated by the subdevices.
221 priv->rxp.efd = fs_epoll_create1(0);
222 if (priv->rxp.efd < 0) {
224 ERROR("Failed to create epoll,"
225 " Rx interrupts will not be supported");
228 priv->rxp.evec = calloc(NUM_RX_PROXIES, sizeof(*priv->rxp.evec));
229 if (priv->rxp.evec == NULL) {
230 ERROR("Failed to allocate memory for event vectors,"
231 " Rx interrupts will not be supported");
235 rc = fs_rx_event_proxy_service_install(priv);
240 if (priv->rxp.efd >= 0) {
241 close(priv->rxp.efd);
244 if (priv->rxp.evec != NULL) {
245 free(priv->rxp.evec);
246 priv->rxp.evec = NULL;
253 * RX Interrupt control per subdevice.
256 * Pointer to sub-device structure.
258 * The operation be performed for the vector.
259 * Operation type of {RTE_INTR_EVENT_ADD, RTE_INTR_EVENT_DEL}.
261 * - On success, zero.
262 * - On failure, a negative value.
265 failsafe_eth_rx_intr_ctl_subdevice(struct sub_device *sdev, int op)
267 struct rte_eth_dev *dev;
268 struct rte_eth_dev *fsdev;
276 fsdev = fs_dev(sdev);
277 if (sdev == NULL || (ETH(sdev) == NULL) ||
278 fsdev == NULL || (PRIV(fsdev) == NULL)) {
279 ERROR("Called with invalid arguments");
283 epfd = PRIV(fsdev)->rxp.efd;
287 if (op == RTE_INTR_EVENT_ADD) {
288 ERROR("Proxy events are not initialized");
294 if (dev->data->nb_rx_queues > fsdev->data->nb_rx_queues) {
295 ERROR("subdevice has too many queues,"
296 " Interrupts will not be enabled");
299 for (qid = 0; qid < dev->data->nb_rx_queues; qid++) {
300 fsrxq = fsdev->data->rx_queues[qid];
301 rc = rte_eth_dev_rx_intr_ctl_q(pid, qid, epfd,
304 ERROR("rte_eth_dev_rx_intr_ctl_q failed for "
305 "port %d queue %d, epfd %d, error %d",
314 * Install Rx interrupts subsystem for a subdevice.
315 * This is a support for dynamically adding subdevices.
318 * Pointer to subdevice structure.
321 * 0 on success, negative errno value otherwise and rte_errno is set.
323 int failsafe_rx_intr_install_subdevice(struct sub_device *sdev)
327 struct rte_eth_dev *fsdev;
329 const struct rte_intr_conf *const intr_conf =
330 Ð(sdev)->data->dev_conf.intr_conf;
332 fsdev = fs_dev(sdev);
333 rxq = (struct rxq **)fsdev->data->rx_queues;
334 if (intr_conf->rxq == 0)
336 rc = failsafe_eth_rx_intr_ctl_subdevice(sdev, RTE_INTR_EVENT_ADD);
339 /* enable interrupts on already-enabled queues */
340 for (qid = 0; qid < ETH(sdev)->data->nb_rx_queues; qid++) {
341 if (rxq[qid]->enable_events) {
342 int ret = rte_eth_dev_rx_intr_enable(PORT_ID(sdev),
344 if (ret && (ret != -ENOTSUP)) {
345 ERROR("Failed to enable interrupts on "
346 "port %d queue %d", PORT_ID(sdev), qid);
355 * Uninstall Rx interrupts subsystem for a subdevice.
356 * This is a support for dynamically removing subdevices.
359 * Pointer to subdevice structure.
362 * 0 on success, negative errno value otherwise and rte_errno is set.
364 void failsafe_rx_intr_uninstall_subdevice(struct sub_device *sdev)
367 struct rte_eth_dev *fsdev;
370 fsdev = fs_dev(sdev);
371 for (qid = 0; qid < ETH(sdev)->data->nb_rx_queues; qid++) {
372 if (qid < fsdev->data->nb_rx_queues) {
373 fsrxq = fsdev->data->rx_queues[qid];
374 if (fsrxq != NULL && fsrxq->enable_events)
375 rte_eth_dev_rx_intr_disable(PORT_ID(sdev),
379 failsafe_eth_rx_intr_ctl_subdevice(sdev, RTE_INTR_EVENT_DEL);
383 * Uninstall failsafe Rx event proxy.
386 * Pointer to failsafe private structure.
389 fs_rx_event_proxy_uninstall(struct fs_priv *priv)
391 fs_rx_event_proxy_service_uninstall(priv);
392 if (priv->rxp.evec != NULL) {
393 free(priv->rxp.evec);
394 priv->rxp.evec = NULL;
396 if (priv->rxp.efd >= 0) {
397 close(priv->rxp.efd);
403 * Uninstall failsafe interrupt vector.
406 * Pointer to failsafe private structure.
409 fs_rx_intr_vec_uninstall(struct fs_priv *priv)
411 struct rte_intr_handle *intr_handle;
413 intr_handle = &priv->intr_handle;
414 if (intr_handle->intr_vec != NULL) {
415 free(intr_handle->intr_vec);
416 intr_handle->intr_vec = NULL;
418 intr_handle->nb_efd = 0;
422 * Installs failsafe interrupt vector to be registered with EAL later on.
425 * Pointer to failsafe private structure.
428 * 0 on success, negative errno value otherwise and rte_errno is set.
431 fs_rx_intr_vec_install(struct fs_priv *priv)
437 struct rte_intr_handle *intr_handle;
439 rxqs_n = priv->data->nb_rx_queues;
440 n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
442 intr_handle = &priv->intr_handle;
443 RTE_ASSERT(intr_handle->intr_vec == NULL);
444 /* Allocate the interrupt vector of the failsafe Rx proxy interrupts */
445 intr_handle->intr_vec = malloc(n * sizeof(intr_handle->intr_vec[0]));
446 if (intr_handle->intr_vec == NULL) {
447 fs_rx_intr_vec_uninstall(priv);
449 ERROR("Failed to allocate memory for interrupt vector,"
450 " Rx interrupts will not be supported");
453 for (i = 0; i < n; i++) {
454 struct rxq *rxq = priv->data->rx_queues[i];
456 /* Skip queues that cannot request interrupts. */
457 if (rxq == NULL || rxq->event_fd < 0) {
458 /* Use invalid intr_vec[] index to disable entry. */
459 intr_handle->intr_vec[i] =
460 RTE_INTR_VEC_RXTX_OFFSET +
461 RTE_MAX_RXTX_INTR_VEC_ID;
464 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
466 ERROR("Too many Rx queues for interrupt vector size"
467 " (%d), Rx interrupts cannot be enabled",
468 RTE_MAX_RXTX_INTR_VEC_ID);
469 fs_rx_intr_vec_uninstall(priv);
472 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
473 intr_handle->efds[count] = rxq->event_fd;
477 fs_rx_intr_vec_uninstall(priv);
479 intr_handle->nb_efd = count;
480 intr_handle->efd_counter_size = sizeof(uint64_t);
487 * Uninstall failsafe Rx interrupts subsystem.
490 * Pointer to private structure.
493 * 0 on success, negative errno value otherwise and rte_errno is set.
496 failsafe_rx_intr_uninstall(struct rte_eth_dev *dev)
498 struct fs_priv *priv;
499 struct rte_intr_handle *intr_handle;
502 intr_handle = &priv->intr_handle;
503 rte_intr_free_epoll_fd(intr_handle);
504 fs_rx_event_proxy_uninstall(priv);
505 fs_rx_intr_vec_uninstall(priv);
506 dev->intr_handle = NULL;
510 * Install failsafe Rx interrupts subsystem.
513 * Pointer to private structure.
516 * 0 on success, negative errno value otherwise and rte_errno is set.
519 failsafe_rx_intr_install(struct rte_eth_dev *dev)
521 struct fs_priv *priv = PRIV(dev);
522 const struct rte_intr_conf *const intr_conf =
523 &priv->data->dev_conf.intr_conf;
525 if (intr_conf->rxq == 0 || dev->intr_handle != NULL)
527 if (fs_rx_intr_vec_install(priv) < 0)
529 if (fs_rx_event_proxy_install(priv) < 0) {
530 fs_rx_intr_vec_uninstall(priv);
533 dev->intr_handle = &priv->intr_handle;