xref: /dpdk/drivers/net/tap/tap_intr.c (revision ee5ff0d3)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 Mellanox Technologies, Ltd
3  */
4 
5 /**
6  * @file
7  * Interrupts handling for tap driver.
8  */
9 
10 #include <assert.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 
18 #include <rte_eth_tap.h>
19 #include <rte_errno.h>
20 #include <rte_interrupts.h>
21 
22 
23 /**
24  * Unregister Rx interrupts free the queue interrupt vector.
25  *
26  * @param dev
27  *   Pointer to the tap rte_eth_dev structure.
28  */
29 static void
30 tap_rx_intr_vec_uninstall(struct rte_eth_dev *dev)
31 {
32 	struct pmd_internals *pmd = dev->data->dev_private;
33 	struct rte_intr_handle *intr_handle = &pmd->intr_handle;
34 
35 	rte_intr_free_epoll_fd(intr_handle);
36 	free(intr_handle->intr_vec);
37 	intr_handle->intr_vec = NULL;
38 	intr_handle->nb_efd = 0;
39 }
40 
41 /**
42  * Allocate Rx queue interrupt vector and register Rx interrupts.
43  *
44  * @param dev
45  *   Pointer to the tap rte_eth_dev device structure.
46  *
47  * @return
48  *   0 on success, negative errno value otherwise and rte_errno is set.
49  */
50 static int
51 tap_rx_intr_vec_install(struct rte_eth_dev *dev)
52 {
53 	struct pmd_internals *pmd = dev->data->dev_private;
54 	struct pmd_process_private *process_private = dev->process_private;
55 	unsigned int rxqs_n = pmd->dev->data->nb_rx_queues;
56 	struct rte_intr_handle *intr_handle = &pmd->intr_handle;
57 	unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
58 	unsigned int i;
59 	unsigned int count = 0;
60 
61 	if (!dev->data->dev_conf.intr_conf.rxq)
62 		return 0;
63 	intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
64 	if (intr_handle->intr_vec == NULL) {
65 		rte_errno = ENOMEM;
66 		TAP_LOG(ERR,
67 			"failed to allocate memory for interrupt vector,"
68 			" Rx interrupts will not be supported");
69 		return -rte_errno;
70 	}
71 	for (i = 0; i < n; i++) {
72 		struct rx_queue *rxq = pmd->dev->data->rx_queues[i];
73 
74 		/* Skip queues that cannot request interrupts. */
75 		if (!rxq || process_private->rxq_fds[i] <= 0) {
76 			/* Use invalid intr_vec[] index to disable entry. */
77 			intr_handle->intr_vec[i] =
78 				RTE_INTR_VEC_RXTX_OFFSET +
79 				RTE_MAX_RXTX_INTR_VEC_ID;
80 			continue;
81 		}
82 		intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
83 		intr_handle->efds[count] = process_private->rxq_fds[i];
84 		count++;
85 	}
86 	if (!count)
87 		tap_rx_intr_vec_uninstall(dev);
88 	else
89 		intr_handle->nb_efd = count;
90 	return 0;
91 }
92 
93 /**
94  * Register or unregister the Rx interrupts.
95  *
96  * @param dev
97  *   Pointer to the tap rte_eth_dev device structure.
98  * @param set
99  *   should the operation be register or unregister the interrupts.
100  *
101  * @return
102  *   0 on success, negative errno value otherwise and rte_errno is set.
103  */
104 int
105 tap_rx_intr_vec_set(struct rte_eth_dev *dev, int set)
106 {
107 	tap_rx_intr_vec_uninstall(dev);
108 	if (set)
109 		return tap_rx_intr_vec_install(dev);
110 	return 0;
111 }
112