Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (c) 2021 NVIDIA Corporation & Affiliates
3 : : */
4 : :
5 : : #include <string.h>
6 : : #include <inttypes.h>
7 : : #include <stdint.h>
8 : : #include <stdbool.h>
9 : : #include <stdlib.h>
10 : : #include <stdio.h>
11 : : #include <sys/queue.h>
12 : : #include <eal_export.h>
13 : : #include <rte_errno.h>
14 : : #include <rte_interrupts.h>
15 : : #include <rte_log.h>
16 : : #include <bus_driver.h>
17 : : #include <rte_per_lcore.h>
18 : : #include <rte_memory.h>
19 : : #include <rte_eal.h>
20 : : #include <rte_eal_paging.h>
21 : : #include <rte_lcore.h>
22 : : #include <rte_string_fns.h>
23 : : #include <rte_common.h>
24 : : #include <rte_devargs.h>
25 : :
26 : : #include "private.h"
27 : :
28 : : #ifndef AUXILIARY_OS_SUPPORTED
29 : : /*
30 : : * Test whether the auxiliary device exist.
31 : : *
32 : : * Stub for OS not supporting auxiliary bus.
33 : : */
34 : : bool
35 : : auxiliary_dev_exists(const char *name)
36 : : {
37 : : RTE_SET_USED(name);
38 : : return false;
39 : : }
40 : :
41 : : /*
42 : : * Scan the devices in the auxiliary bus.
43 : : *
44 : : * Stub for OS not supporting auxiliary bus.
45 : : */
46 : : int
47 : : auxiliary_scan(void)
48 : : {
49 : : return 0;
50 : : }
51 : : #endif /* AUXILIARY_OS_SUPPORTED */
52 : :
53 : : /*
54 : : * Update a device's devargs being scanned.
55 : : */
56 : : void
57 : 0 : auxiliary_on_scan(struct rte_auxiliary_device *aux_dev)
58 : : {
59 : 0 : aux_dev->device.devargs = rte_bus_find_devargs(&auxiliary_bus, aux_dev->name);
60 : 0 : }
61 : :
62 : : static bool
63 : 0 : auxiliary_bus_match(const struct rte_driver *drv, const struct rte_device *dev)
64 : : {
65 : : const struct rte_auxiliary_driver *aux_drv = RTE_BUS_DRIVER(drv, *aux_drv);
66 : : const struct rte_auxiliary_device *aux_dev = RTE_BUS_DEVICE(dev, *aux_dev);
67 : :
68 [ # # ]: 0 : if (aux_drv->match == NULL)
69 : : return false;
70 : 0 : return aux_drv->match(aux_dev->name);
71 : : }
72 : :
73 : : /*
74 : : * Call the probe() function of the driver.
75 : : */
76 : : static int
77 : 0 : auxiliary_probe_device(struct rte_driver *drv, struct rte_device *dev)
78 : : {
79 : : struct rte_auxiliary_device *aux_dev = RTE_BUS_DEVICE(dev, *aux_dev);
80 : : struct rte_auxiliary_driver *aux_drv = RTE_BUS_DRIVER(drv, *aux_drv);
81 : : enum rte_iova_mode iova_mode;
82 : : int ret;
83 : :
84 [ # # ]: 0 : if (!auxiliary_dev_exists(dev->name))
85 : : return -ENOENT;
86 : :
87 [ # # # # ]: 0 : if (aux_dev->device.numa_node < 0 && rte_socket_count() > 1)
88 : 0 : AUXILIARY_LOG(INFO, "Device %s is not NUMA-aware", aux_dev->name);
89 : :
90 : 0 : iova_mode = rte_eal_iova_mode();
91 [ # # # # ]: 0 : if ((aux_drv->drv_flags & RTE_AUXILIARY_DRV_NEED_IOVA_AS_VA) > 0 &&
92 : : iova_mode != RTE_IOVA_VA) {
93 : 0 : AUXILIARY_LOG(ERR, "Driver %s expecting VA IOVA mode but current mode is PA, not initializing",
94 : : aux_drv->driver.name);
95 : 0 : return -EINVAL;
96 : : }
97 : :
98 : : /* Allocate interrupt instance */
99 : 0 : aux_dev->intr_handle =
100 : 0 : rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
101 [ # # ]: 0 : if (aux_dev->intr_handle == NULL) {
102 : 0 : AUXILIARY_LOG(ERR, "Could not allocate interrupt instance for device %s",
103 : : aux_dev->name);
104 : 0 : return -ENOMEM;
105 : : }
106 : :
107 : 0 : AUXILIARY_LOG(INFO, "Probe auxiliary driver: %s device: %s (NUMA node %i)",
108 : : aux_drv->driver.name, aux_dev->name, aux_dev->device.numa_node);
109 : 0 : ret = aux_drv->probe(aux_drv, aux_dev);
110 [ # # ]: 0 : if (ret != 0) {
111 : 0 : rte_intr_instance_free(aux_dev->intr_handle);
112 : 0 : aux_dev->intr_handle = NULL;
113 : : }
114 : :
115 : : return ret;
116 : : }
117 : :
118 : : static int
119 : 0 : auxiliary_unplug_device(struct rte_device *rte_dev)
120 : : {
121 : 0 : const struct rte_auxiliary_driver *drv = RTE_BUS_DRIVER(rte_dev->driver, *drv);
122 : : struct rte_auxiliary_device *dev = RTE_BUS_DEVICE(rte_dev, *dev);
123 : : int ret = 0;
124 : :
125 : 0 : AUXILIARY_LOG(DEBUG, "Driver %s remove auxiliary device %s on NUMA node %i",
126 : : drv->driver.name, dev->name, dev->device.numa_node);
127 : :
128 [ # # ]: 0 : if (drv->remove != NULL) {
129 : 0 : ret = drv->remove(dev);
130 [ # # ]: 0 : if (ret < 0)
131 : : return ret;
132 : : }
133 : :
134 : 0 : rte_intr_instance_free(dev->intr_handle);
135 : 0 : dev->intr_handle = NULL;
136 : :
137 : 0 : return 0;
138 : : }
139 : :
140 : : static int
141 : 42 : auxiliary_parse(const char *name, void *addr)
142 : : {
143 : : struct rte_auxiliary_driver *drv = NULL;
144 : : const char **out = addr;
145 : :
146 : : /* Allow empty device name "auxiliary:" to bypass entire bus scan. */
147 [ + - ]: 42 : if (strlen(name) == 0)
148 : : return 0;
149 : :
150 [ + + ]: 84 : RTE_BUS_FOREACH_DRV(drv, &auxiliary_bus) {
151 [ + - ]: 42 : if (drv->match(name))
152 : : break;
153 : : }
154 [ - + ]: 42 : if (drv != NULL && addr != NULL)
155 : 0 : *out = name;
156 [ + - ]: 42 : return drv != NULL ? 0 : -1;
157 : : }
158 : :
159 : : /* Register a driver */
160 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_auxiliary_register)
161 : : void
162 : 303 : rte_auxiliary_register(struct rte_auxiliary_driver *driver)
163 : : {
164 : 303 : rte_bus_add_driver(&auxiliary_bus, &driver->driver);
165 : 303 : }
166 : :
167 : : /* Unregister a driver */
168 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_auxiliary_unregister)
169 : : void
170 : 303 : rte_auxiliary_unregister(struct rte_auxiliary_driver *driver)
171 : : {
172 : 303 : rte_bus_remove_driver(&auxiliary_bus, &driver->driver);
173 : 303 : }
174 : :
175 : : static void
176 : 0 : auxiliary_free_device(struct rte_device *dev)
177 : : {
178 : 0 : free(RTE_BUS_DEVICE(dev, struct rte_auxiliary_device));
179 : 0 : }
180 : :
181 : : static int
182 : 0 : auxiliary_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
183 : : {
184 : : struct rte_auxiliary_device *aux_dev = RTE_BUS_DEVICE(dev, *aux_dev);
185 : 0 : const struct rte_auxiliary_driver *aux_drv = RTE_BUS_DRIVER(dev->driver, *aux_drv);
186 : :
187 [ # # ]: 0 : if (aux_drv->dma_map == NULL) {
188 : 0 : rte_errno = ENOTSUP;
189 : 0 : return -1;
190 : : }
191 : 0 : return aux_drv->dma_map(aux_dev, addr, iova, len);
192 : : }
193 : :
194 : : static int
195 : 0 : auxiliary_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova,
196 : : size_t len)
197 : : {
198 : : struct rte_auxiliary_device *aux_dev = RTE_BUS_DEVICE(dev, *aux_dev);
199 : 0 : const struct rte_auxiliary_driver *aux_drv = RTE_BUS_DRIVER(dev->driver, *aux_drv);
200 : :
201 [ # # ]: 0 : if (aux_drv->dma_unmap == NULL) {
202 : 0 : rte_errno = ENOTSUP;
203 : 0 : return -1;
204 : : }
205 : 0 : return aux_drv->dma_unmap(aux_dev, addr, iova, len);
206 : : }
207 : :
208 : : static enum rte_iova_mode
209 : 227 : auxiliary_get_iommu_class(void)
210 : : {
211 : : const struct rte_auxiliary_driver *drv;
212 : :
213 [ + + ]: 454 : RTE_BUS_FOREACH_DRV(drv, &auxiliary_bus) {
214 [ + - ]: 227 : if ((drv->drv_flags & RTE_AUXILIARY_DRV_NEED_IOVA_AS_VA) > 0)
215 : : return RTE_IOVA_VA;
216 : : }
217 : :
218 : : return RTE_IOVA_DC;
219 : : }
220 : :
221 : : struct rte_bus auxiliary_bus = {
222 : : .scan = auxiliary_scan,
223 : : .probe = rte_bus_generic_probe,
224 : : .free_device = auxiliary_free_device,
225 : : .cleanup = rte_bus_generic_cleanup,
226 : : .find_device = rte_bus_generic_find_device,
227 : : .match = auxiliary_bus_match,
228 : : .probe_device = auxiliary_probe_device,
229 : : .unplug_device = auxiliary_unplug_device,
230 : : .parse = auxiliary_parse,
231 : : .dma_map = auxiliary_dma_map,
232 : : .dma_unmap = auxiliary_dma_unmap,
233 : : .get_iommu_class = auxiliary_get_iommu_class,
234 : : .dev_iterate = rte_bus_generic_dev_iterate,
235 : : };
236 : :
237 : 303 : RTE_REGISTER_BUS(auxiliary, auxiliary_bus);
238 [ - + ]: 303 : RTE_LOG_REGISTER_DEFAULT(auxiliary_bus_logtype, NOTICE);
|