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