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.11/bus_pci_driver.h"
11 : : #include "22.11/bus_driver.h"
12 : : #include "22.11/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 : : /* Following API was added in versions later than DPDK 22.11.
20 : : * It is unused right now, if this changes a new pci_dpdk_* should be added.
21 : : */
22 : : #define rte_pci_mmio_read(...) SPDK_STATIC_ASSERT(false, "rte_pci_mmio_read requires new pci_dpdk_2307 compat layer")
23 : : #define rte_pci_mmio_write(...) SPDK_STATIC_ASSERT(false, "rte_pci_mmio_write requires new pci_dpdk_2307 compat layer")
24 : : #define rte_pci_pasid_set_state(...) SPDK_STATIC_ASSERT(false, "rte_pci_pasid_set_state requires new pci_dpdk_2307 compat layer")
25 : :
26 : : static struct rte_mem_resource *
27 : 19508 : pci_device_get_mem_resource_2211(struct rte_pci_device *dev, uint32_t bar)
28 : : {
29 [ - + ]: 19508 : if (bar >= PCI_MAX_RESOURCE) {
30 : 0 : assert(false);
31 : : return NULL;
32 : : }
33 : :
34 : 19508 : return &dev->mem_resource[bar];
35 : : }
36 : :
37 : : static const char *
38 : 1111 : pci_device_get_name_2211(struct rte_pci_device *rte_dev)
39 : : {
40 : 1111 : return rte_dev->name;
41 : : }
42 : :
43 : : static struct rte_devargs *
44 : 3942 : pci_device_get_devargs_2211(struct rte_pci_device *rte_dev)
45 : : {
46 : 3942 : return rte_dev->device.devargs;
47 : : }
48 : :
49 : : static struct rte_pci_addr *
50 : 1862 : pci_device_get_addr_2211(struct rte_pci_device *_dev)
51 : : {
52 : 1862 : return &_dev->addr;
53 : : }
54 : :
55 : : static struct rte_pci_id *
56 : 1862 : pci_device_get_id_2211(struct rte_pci_device *_dev)
57 : : {
58 : 1862 : return &_dev->id;
59 : : }
60 : :
61 : : static int
62 : 1862 : pci_device_get_numa_node_2211(struct rte_pci_device *_dev)
63 : : {
64 : 1862 : return _dev->device.numa_node;
65 : : }
66 : :
67 : : static int
68 : 1162 : pci_device_read_config_2211(struct rte_pci_device *dev, void *value, uint32_t len, uint32_t offset)
69 : : {
70 : : int rc;
71 : :
72 : 1162 : rc = rte_pci_read_config(dev, value, len, offset);
73 : :
74 [ + - + - ]: 1162 : return (rc > 0 && (uint32_t) rc == len) ? 0 : -1;
75 : : }
76 : :
77 : : static int
78 : 1138 : pci_device_write_config_2211(struct rte_pci_device *dev, void *value, uint32_t len, uint32_t offset)
79 : : {
80 : : int rc;
81 : :
82 : 1138 : rc = rte_pci_write_config(dev, value, len, offset);
83 : :
84 : : #ifdef __FreeBSD__
85 : : /* DPDK returns 0 on success and -1 on failure */
86 : : return rc;
87 : : #endif
88 [ + - + - ]: 1138 : return (rc > 0 && (uint32_t) rc == len) ? 0 : -1;
89 : : }
90 : :
91 : : /* translate spdk_pci_driver to an rte_pci_driver and register it to dpdk */
92 : : static int
93 : 15375 : pci_driver_register_2211(struct spdk_pci_driver *driver,
94 : : int (*probe_fn)(struct rte_pci_driver *driver, struct rte_pci_device *device),
95 : : int (*remove_fn)(struct rte_pci_device *device))
96 : :
97 : : {
98 : 15375 : unsigned pci_id_count = 0;
99 : : struct rte_pci_id *rte_id_table;
100 : : char *rte_name;
101 : : size_t rte_name_len;
102 : : uint32_t rte_flags;
103 : :
104 [ - + ]: 15375 : assert(driver->id_table);
105 [ + + ]: 199840 : while (driver->id_table[pci_id_count].vendor_id) {
106 : 184465 : pci_id_count++;
107 : : }
108 [ - + ]: 15375 : assert(pci_id_count > 0);
109 : :
110 : 15375 : rte_id_table = calloc(pci_id_count + 1, sizeof(*rte_id_table));
111 [ - + ]: 15375 : if (!rte_id_table) {
112 : 0 : return -ENOMEM;
113 : : }
114 : :
115 [ + + ]: 199840 : while (pci_id_count > 0) {
116 : 184465 : struct rte_pci_id *rte_id = &rte_id_table[pci_id_count - 1];
117 : 184465 : const struct spdk_pci_id *spdk_id = &driver->id_table[pci_id_count - 1];
118 : :
119 : 184465 : rte_id->class_id = spdk_id->class_id;
120 : 184465 : rte_id->vendor_id = spdk_id->vendor_id;
121 : 184465 : rte_id->device_id = spdk_id->device_id;
122 : 184465 : rte_id->subsystem_vendor_id = spdk_id->subvendor_id;
123 : 184465 : rte_id->subsystem_device_id = spdk_id->subdevice_id;
124 : 184465 : pci_id_count--;
125 : : }
126 : :
127 [ - + ]: 15375 : assert(driver->name);
128 [ - + ]: 15375 : rte_name_len = strlen(driver->name) + strlen("spdk_") + 1;
129 : 15375 : rte_name = calloc(rte_name_len, 1);
130 [ - + ]: 15375 : if (!rte_name) {
131 : 0 : free(rte_id_table);
132 : 0 : return -ENOMEM;
133 : : }
134 : :
135 : 15375 : snprintf(rte_name, rte_name_len, "spdk_%s", driver->name);
136 : 15375 : driver->driver->driver.name = rte_name;
137 : 15375 : driver->driver->id_table = rte_id_table;
138 : :
139 : 15375 : rte_flags = 0;
140 [ + - ]: 15375 : if (driver->drv_flags & SPDK_PCI_DRIVER_NEED_MAPPING) {
141 : 15375 : rte_flags |= RTE_PCI_DRV_NEED_MAPPING;
142 : : }
143 [ + + ]: 15375 : if (driver->drv_flags & SPDK_PCI_DRIVER_WC_ACTIVATE) {
144 : 8985 : rte_flags |= RTE_PCI_DRV_WC_ACTIVATE;
145 : : }
146 : 15375 : driver->driver->drv_flags = rte_flags;
147 : :
148 : 15375 : driver->driver->probe = probe_fn;
149 : 15375 : driver->driver->remove = remove_fn;
150 : :
151 : 15375 : rte_pci_register(driver->driver);
152 : 15375 : return 0;
153 : : }
154 : :
155 : : static int
156 : 0 : pci_device_enable_interrupt_2211(struct rte_pci_device *rte_dev)
157 : : {
158 : 0 : return rte_intr_enable(rte_dev->intr_handle);
159 : : }
160 : :
161 : : static int
162 : 0 : pci_device_disable_interrupt_2211(struct rte_pci_device *rte_dev)
163 : : {
164 : 0 : return rte_intr_disable(rte_dev->intr_handle);
165 : : }
166 : :
167 : : static int
168 : 0 : pci_device_get_interrupt_efd_2211(struct rte_pci_device *rte_dev)
169 : : {
170 : 0 : return rte_intr_fd_get(rte_dev->intr_handle);
171 : : }
172 : :
173 : : static int
174 : 97350 : bus_probe_2211(void)
175 : : {
176 : 97350 : return rte_bus_probe();
177 : : }
178 : :
179 : : static void
180 : 100711 : bus_scan_2211(void)
181 : : {
182 : 100711 : rte_bus_scan();
183 : 100711 : }
184 : :
185 : : static struct rte_devargs *
186 : 1043904 : device_get_devargs_2211(struct rte_device *dev)
187 : : {
188 : 1043904 : return dev->devargs;
189 : : }
190 : :
191 : : static void
192 : 103319 : device_set_devargs_2211(struct rte_device *dev, struct rte_devargs *devargs)
193 : : {
194 : 103319 : dev->devargs = devargs;
195 : 103319 : }
196 : :
197 : : static const char *
198 : 103319 : device_get_name_2211(struct rte_device *dev)
199 : : {
200 : 103319 : return dev->name;
201 : : }
202 : :
203 : : static bool
204 : 103319 : device_scan_allowed_2211(struct rte_device *dev)
205 : : {
206 : 103319 : return dev->bus->conf.scan_mode == RTE_BUS_SCAN_ALLOWLIST;
207 : : }
208 : :
209 : : struct dpdk_fn_table fn_table_2211 = {
210 : : .pci_device_get_mem_resource = pci_device_get_mem_resource_2211,
211 : : .pci_device_get_name = pci_device_get_name_2211,
212 : : .pci_device_get_devargs = pci_device_get_devargs_2211,
213 : : .pci_device_get_addr = pci_device_get_addr_2211,
214 : : .pci_device_get_id = pci_device_get_id_2211,
215 : : .pci_device_get_numa_node = pci_device_get_numa_node_2211,
216 : : .pci_device_read_config = pci_device_read_config_2211,
217 : : .pci_device_write_config = pci_device_write_config_2211,
218 : : .pci_driver_register = pci_driver_register_2211,
219 : : .pci_device_enable_interrupt = pci_device_enable_interrupt_2211,
220 : : .pci_device_disable_interrupt = pci_device_disable_interrupt_2211,
221 : : .pci_device_get_interrupt_efd = pci_device_get_interrupt_efd_2211,
222 : : .bus_scan = bus_scan_2211,
223 : : .bus_probe = bus_probe_2211,
224 : : .device_get_devargs = device_get_devargs_2211,
225 : : .device_set_devargs = device_set_devargs_2211,
226 : : .device_get_name = device_get_name_2211,
227 : : .device_scan_allowed = device_scan_allowed_2211,
228 : : };
|