Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2022 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #define ALLOW_INTERNAL_API
7 : : #include <rte_config.h>
8 : : #include <rte_version.h>
9 : : #include "pci_dpdk.h"
10 : : #include "22.07/rte_dev.h"
11 : : #include "22.07/rte_bus.h"
12 : : #include "22.07/rte_bus_pci.h"
13 : : #include "spdk/assert.h"
14 : :
15 : : SPDK_STATIC_ASSERT(offsetof(struct spdk_pci_driver, driver_buf) == 0, "driver_buf must be first");
16 : : SPDK_STATIC_ASSERT(offsetof(struct spdk_pci_driver, driver) >= sizeof(struct rte_pci_driver),
17 : : "driver_buf not big enough");
18 : :
19 : : static struct rte_mem_resource *
20 : 0 : pci_device_get_mem_resource_2207(struct rte_pci_device *dev, uint32_t bar)
21 : : {
22 [ # # ]: 0 : if (bar >= PCI_MAX_RESOURCE) {
23 : 0 : assert(false);
24 : : return NULL;
25 : : }
26 : :
27 : 0 : return &dev->mem_resource[bar];
28 : : }
29 : :
30 : : static const char *
31 : 0 : pci_device_get_name_2207(struct rte_pci_device *rte_dev)
32 : : {
33 : 0 : return rte_dev->name;
34 : : }
35 : :
36 : : static struct rte_devargs *
37 : 0 : pci_device_get_devargs_2207(struct rte_pci_device *rte_dev)
38 : : {
39 : 0 : return rte_dev->device.devargs;
40 : : }
41 : :
42 : : static struct rte_pci_addr *
43 : 0 : pci_device_get_addr_2207(struct rte_pci_device *_dev)
44 : : {
45 : 0 : return &_dev->addr;
46 : : }
47 : :
48 : : static struct rte_pci_id *
49 : 0 : pci_device_get_id_2207(struct rte_pci_device *_dev)
50 : : {
51 : 0 : return &_dev->id;
52 : : }
53 : :
54 : : static int
55 : 0 : pci_device_get_numa_node_2207(struct rte_pci_device *_dev)
56 : : {
57 : 0 : return _dev->device.numa_node;
58 : : }
59 : :
60 : : static int
61 : 0 : pci_device_read_config_2207(struct rte_pci_device *dev, void *value, uint32_t len, uint32_t offset)
62 : : {
63 : : int rc;
64 : :
65 : 0 : rc = rte_pci_read_config(dev, value, len, offset);
66 : :
67 [ # # ]: 0 : return (rc > 0 && (uint32_t) rc == len) ? 0 : -1;
68 : : }
69 : :
70 : : static int
71 : 0 : pci_device_write_config_2207(struct rte_pci_device *dev, void *value, uint32_t len, uint32_t offset)
72 : : {
73 : : int rc;
74 : :
75 : 0 : rc = rte_pci_write_config(dev, value, len, offset);
76 : :
77 : : #ifdef __FreeBSD__
78 : : /* DPDK returns 0 on success and -1 on failure */
79 : 0 : return rc;
80 : : #endif
81 : : return (rc > 0 && (uint32_t) rc == len) ? 0 : -1;
82 : : }
83 : :
84 : : /* translate spdk_pci_driver to an rte_pci_driver and register it to dpdk */
85 : : static int
86 : 0 : pci_driver_register_2207(struct spdk_pci_driver *driver,
87 : : int (*probe_fn)(struct rte_pci_driver *driver, struct rte_pci_device *device),
88 : : int (*remove_fn)(struct rte_pci_device *device))
89 : :
90 : : {
91 : 0 : unsigned pci_id_count = 0;
92 : : struct rte_pci_id *rte_id_table;
93 : : char *rte_name;
94 : : size_t rte_name_len;
95 : : uint32_t rte_flags;
96 : :
97 [ # # ]: 0 : assert(driver->id_table);
98 [ # # ]: 0 : while (driver->id_table[pci_id_count].vendor_id) {
99 : 0 : pci_id_count++;
100 : : }
101 [ # # ]: 0 : assert(pci_id_count > 0);
102 : :
103 : 0 : rte_id_table = calloc(pci_id_count + 1, sizeof(*rte_id_table));
104 [ # # ]: 0 : if (!rte_id_table) {
105 : 0 : return -ENOMEM;
106 : : }
107 : :
108 [ # # ]: 0 : while (pci_id_count > 0) {
109 : 0 : struct rte_pci_id *rte_id = &rte_id_table[pci_id_count - 1];
110 : 0 : const struct spdk_pci_id *spdk_id = &driver->id_table[pci_id_count - 1];
111 : :
112 : 0 : rte_id->class_id = spdk_id->class_id;
113 : 0 : rte_id->vendor_id = spdk_id->vendor_id;
114 : 0 : rte_id->device_id = spdk_id->device_id;
115 : 0 : rte_id->subsystem_vendor_id = spdk_id->subvendor_id;
116 : 0 : rte_id->subsystem_device_id = spdk_id->subdevice_id;
117 : 0 : pci_id_count--;
118 : : }
119 : :
120 [ # # ]: 0 : assert(driver->name);
121 : 0 : rte_name_len = strlen(driver->name) + strlen("spdk_") + 1;
122 : 0 : rte_name = calloc(rte_name_len, 1);
123 [ # # ]: 0 : if (!rte_name) {
124 : 0 : free(rte_id_table);
125 : 0 : return -ENOMEM;
126 : : }
127 : :
128 : 0 : snprintf(rte_name, rte_name_len, "spdk_%s", driver->name);
129 : 0 : driver->driver->driver.name = rte_name;
130 : 0 : driver->driver->id_table = rte_id_table;
131 : :
132 : 0 : rte_flags = 0;
133 [ # # ]: 0 : if (driver->drv_flags & SPDK_PCI_DRIVER_NEED_MAPPING) {
134 : 0 : rte_flags |= RTE_PCI_DRV_NEED_MAPPING;
135 : 0 : }
136 [ # # ]: 0 : if (driver->drv_flags & SPDK_PCI_DRIVER_WC_ACTIVATE) {
137 : 0 : rte_flags |= RTE_PCI_DRV_WC_ACTIVATE;
138 : 0 : }
139 : 0 : driver->driver->drv_flags = rte_flags;
140 : :
141 : 0 : driver->driver->probe = probe_fn;
142 : 0 : driver->driver->remove = remove_fn;
143 : :
144 : 0 : rte_pci_register(driver->driver);
145 : 0 : return 0;
146 : 0 : }
147 : :
148 : : static int
149 : 0 : pci_device_enable_interrupt_2207(struct rte_pci_device *rte_dev)
150 : : {
151 : 0 : return rte_intr_enable(rte_dev->intr_handle);
152 : : }
153 : :
154 : : static int
155 : 0 : pci_device_disable_interrupt_2207(struct rte_pci_device *rte_dev)
156 : : {
157 : 0 : return rte_intr_disable(rte_dev->intr_handle);
158 : : }
159 : :
160 : : static int
161 : 0 : pci_device_get_interrupt_efd_2207(struct rte_pci_device *rte_dev)
162 : : {
163 : 0 : return rte_intr_fd_get(rte_dev->intr_handle);
164 : : }
165 : :
166 : : static int
167 : 0 : pci_device_create_interrupt_efds_2207(struct rte_pci_device *rte_dev, uint32_t count)
168 : : {
169 : 0 : return rte_intr_efd_enable(rte_dev->intr_handle, count);
170 : : }
171 : :
172 : : static void
173 : 0 : pci_device_delete_interrupt_efds_2207(struct rte_pci_device *rte_dev)
174 : : {
175 : 0 : return rte_intr_efd_disable(rte_dev->intr_handle);
176 : : }
177 : :
178 : : static int
179 : 0 : pci_device_get_interrupt_efd_by_index_2207(struct rte_pci_device *rte_dev, uint32_t index)
180 : : {
181 : 0 : return rte_intr_efds_index_get(rte_dev->intr_handle, index);
182 : : }
183 : :
184 : : static int
185 : 0 : pci_device_interrupt_cap_multi_2207(struct rte_pci_device *rte_dev)
186 : : {
187 : 0 : return rte_intr_cap_multiple(rte_dev->intr_handle);
188 : : }
189 : :
190 : : static int
191 : 0 : bus_probe_2207(void)
192 : : {
193 : 0 : return rte_bus_probe();
194 : : }
195 : :
196 : : static void
197 : 0 : bus_scan_2207(void)
198 : : {
199 : 0 : rte_bus_scan();
200 : 0 : }
201 : :
202 : : static struct rte_devargs *
203 : 0 : device_get_devargs_2207(struct rte_device *dev)
204 : : {
205 : 0 : return dev->devargs;
206 : : }
207 : :
208 : : static void
209 : 0 : device_set_devargs_2207(struct rte_device *dev, struct rte_devargs *devargs)
210 : : {
211 : 0 : dev->devargs = devargs;
212 : 0 : }
213 : :
214 : : static const char *
215 : 0 : device_get_name_2207(struct rte_device *dev)
216 : : {
217 : 0 : return dev->name;
218 : : }
219 : :
220 : : static bool
221 : 0 : device_scan_allowed_2207(struct rte_device *dev)
222 : : {
223 : 0 : return dev->bus->conf.scan_mode == RTE_BUS_SCAN_ALLOWLIST;
224 : : }
225 : :
226 : : struct dpdk_fn_table fn_table_2207 = {
227 : : .pci_device_get_mem_resource = pci_device_get_mem_resource_2207,
228 : : .pci_device_get_name = pci_device_get_name_2207,
229 : : .pci_device_get_devargs = pci_device_get_devargs_2207,
230 : : .pci_device_get_addr = pci_device_get_addr_2207,
231 : : .pci_device_get_id = pci_device_get_id_2207,
232 : : .pci_device_get_numa_node = pci_device_get_numa_node_2207,
233 : : .pci_device_read_config = pci_device_read_config_2207,
234 : : .pci_device_write_config = pci_device_write_config_2207,
235 : : .pci_driver_register = pci_driver_register_2207,
236 : : .pci_device_enable_interrupt = pci_device_enable_interrupt_2207,
237 : : .pci_device_disable_interrupt = pci_device_disable_interrupt_2207,
238 : : .pci_device_get_interrupt_efd = pci_device_get_interrupt_efd_2207,
239 : : .pci_device_create_interrupt_efds = pci_device_create_interrupt_efds_2207,
240 : : .pci_device_delete_interrupt_efds = pci_device_delete_interrupt_efds_2207,
241 : : .pci_device_get_interrupt_efd_by_index = pci_device_get_interrupt_efd_by_index_2207,
242 : : .pci_device_interrupt_cap_multi = pci_device_interrupt_cap_multi_2207,
243 : : .bus_scan = bus_scan_2207,
244 : : .bus_probe = bus_probe_2207,
245 : : .device_get_devargs = device_get_devargs_2207,
246 : : .device_set_devargs = device_set_devargs_2207,
247 : : .device_get_name = device_get_name_2207,
248 : : .device_scan_allowed = device_scan_allowed_2207,
249 : : };
|