1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
4 #include <rte_windows.h>
10 #include "pci_netuio.h"
15 #if defined RTE_TOOLCHAIN_GCC && (__MINGW64_VERSION_MAJOR < 8)
16 #include <devpropdef.h>
17 DEFINE_DEVPROPKEY(DEVPKEY_Device_Numa_Node, 0x540b947e, 0x8b40, 0x45bc,
18 0xa8, 0xa2, 0x6a, 0x0b, 0x89, 0x4c, 0xbd, 0xa2, 3);
22 * This code is used to simulate a PCI probe by parsing information in
23 * the registry hive for PCI devices.
26 /* The functions below are not implemented on Windows,
27 * but need to be defined for compilation purposes
32 rte_pci_map_device(struct rte_pci_device *dev __rte_unused)
34 /* This function is not implemented on Windows.
35 * We really should short-circuit the call to these functions by
36 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
37 * in the rte_pci_driver flags.
42 /* Unmap pci device */
44 rte_pci_unmap_device(struct rte_pci_device *dev __rte_unused)
46 /* This function is not implemented on Windows.
47 * We really should short-circuit the call to these functions by
48 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
49 * in the rte_pci_driver flags.
53 /* Read PCI config space. */
55 rte_pci_read_config(const struct rte_pci_device *dev __rte_unused,
56 void *buf __rte_unused, size_t len __rte_unused,
57 off_t offset __rte_unused)
59 /* This function is not implemented on Windows.
60 * We really should short-circuit the call to these functions by
61 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
62 * in the rte_pci_driver flags.
67 /* Write PCI config space. */
69 rte_pci_write_config(const struct rte_pci_device *dev __rte_unused,
70 const void *buf __rte_unused, size_t len __rte_unused,
71 off_t offset __rte_unused)
73 /* This function is not implemented on Windows.
74 * We really should short-circuit the call to these functions by
75 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
76 * in the rte_pci_driver flags.
82 pci_device_iova_mode(const struct rte_pci_driver *pdrv __rte_unused,
83 const struct rte_pci_device *pdev __rte_unused)
85 /* This function is not implemented on Windows.
86 * We really should short-circuit the call to these functions by
87 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
88 * in the rte_pci_driver flags.
94 rte_pci_ioport_map(struct rte_pci_device *dev __rte_unused,
95 int bar __rte_unused, struct rte_pci_ioport *p __rte_unused)
97 /* This function is not implemented on Windows.
98 * We really should short-circuit the call to these functions by
99 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
100 * in the rte_pci_driver flags.
107 rte_pci_ioport_read(struct rte_pci_ioport *p __rte_unused,
108 void *data __rte_unused, size_t len __rte_unused,
109 off_t offset __rte_unused)
111 /* This function is not implemented on Windows.
112 * We really should short-circuit the call to these functions by
113 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
114 * in the rte_pci_driver flags.
119 rte_pci_ioport_unmap(struct rte_pci_ioport *p __rte_unused)
121 /* This function is not implemented on Windows.
122 * We really should short-circuit the call to these functions by
123 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
124 * in the rte_pci_driver flags.
130 pci_device_iommu_support_va(const struct rte_pci_device *dev __rte_unused)
132 /* This function is not implemented on Windows.
133 * We really should short-circuit the call to these functions by
134 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
135 * in the rte_pci_driver flags.
141 rte_pci_ioport_write(struct rte_pci_ioport *p __rte_unused,
142 const void *data __rte_unused, size_t len __rte_unused,
143 off_t offset __rte_unused)
145 /* This function is not implemented on Windows.
146 * We really should short-circuit the call to these functions by
147 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
148 * in the rte_pci_driver flags.
152 /* remap the PCI resource of a PCI device in anonymous virtual memory */
154 pci_uio_remap_resource(struct rte_pci_device *dev __rte_unused)
156 /* This function is not implemented on Windows.
157 * We really should short-circuit the call to these functions by
158 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
159 * in the rte_pci_driver flags.
165 get_device_pci_address(HDEVINFO dev_info,
166 PSP_DEVINFO_DATA device_info_data, struct rte_pci_addr *addr)
169 ULONG bus_num, dev_and_func;
171 res = SetupDiGetDeviceRegistryProperty(dev_info, device_info_data,
172 SPDRP_BUSNUMBER, NULL, (PBYTE)&bus_num, sizeof(bus_num), NULL);
175 "SetupDiGetDeviceRegistryProperty(SPDRP_BUSNUMBER)");
179 res = SetupDiGetDeviceRegistryProperty(dev_info, device_info_data,
180 SPDRP_ADDRESS, NULL, (PBYTE)&dev_and_func, sizeof(dev_and_func),
184 "SetupDiGetDeviceRegistryProperty(SPDRP_ADDRESS)");
188 addr->domain = (bus_num >> 8) & 0xffff;
189 addr->bus = bus_num & 0xff;
190 addr->devid = dev_and_func >> 16;
191 addr->function = dev_and_func & 0xffff;
196 get_device_resource_info(HDEVINFO dev_info,
197 PSP_DEVINFO_DATA dev_info_data, struct rte_pci_device *dev)
199 DEVPROPTYPE property_type;
205 case RTE_PCI_KDRV_NONE:
206 /* mem_resource - Unneeded for RTE_PCI_KDRV_NONE */
207 dev->mem_resource[0].phys_addr = 0;
208 dev->mem_resource[0].len = 0;
209 dev->mem_resource[0].addr = NULL;
211 case RTE_PCI_KDRV_NIC_UIO:
212 /* get device info from netuio kernel driver */
213 ret = get_netuio_device_info(dev_info, dev_info_data, dev);
216 "Could not retrieve device info for PCI device "
218 dev->addr.domain, dev->addr.bus,
219 dev->addr.devid, dev->addr.function);
224 /* kernel driver type is unsupported */
226 "Kernel driver type for PCI device " PCI_PRI_FMT ","
228 dev->addr.domain, dev->addr.bus,
229 dev->addr.devid, dev->addr.function);
233 /* Get NUMA node using DEVPKEY_Device_Numa_Node */
234 res = SetupDiGetDevicePropertyW(dev_info, dev_info_data,
235 &DEVPKEY_Device_Numa_Node, &property_type,
236 (BYTE *)&numa_node, sizeof(numa_node), NULL, 0);
238 RTE_LOG_WIN32_ERR("SetupDiGetDevicePropertyW"
239 "(DEVPKEY_Device_Numa_Node)");
242 dev->device.numa_node = numa_node;
244 return ERROR_SUCCESS;
248 * get string that contains the list of hardware IDs for a device
251 get_pci_hardware_id(HDEVINFO dev_info, PSP_DEVINFO_DATA device_info_data,
252 char *pci_device_info, size_t pci_device_info_len)
256 /* Retrieve PCI device IDs */
257 res = SetupDiGetDeviceRegistryPropertyA(dev_info, device_info_data,
258 SPDRP_HARDWAREID, NULL, (BYTE *)pci_device_info,
259 pci_device_info_len, NULL);
262 "SetupDiGetDeviceRegistryPropertyA(SPDRP_HARDWAREID)");
270 * parse the SPDRP_HARDWAREID output and assign to rte_pci_id
273 parse_pci_hardware_id(const char *buf, struct rte_pci_id *pci_id)
276 uint16_t vendor_id, device_id;
277 uint32_t subvendor_id = 0;
279 ids = sscanf_s(buf, "PCI\\VEN_%" PRIx16 "&DEV_%" PRIx16 "&SUBSYS_%"
280 PRIx32, &vendor_id, &device_id, &subvendor_id);
284 pci_id->vendor_id = vendor_id;
285 pci_id->device_id = device_id;
286 pci_id->subsystem_device_id = subvendor_id >> 16;
287 pci_id->subsystem_vendor_id = subvendor_id & 0xffff;
292 set_kernel_driver_type(PSP_DEVINFO_DATA device_info_data,
293 struct rte_pci_device *dev)
295 /* set kernel driver type based on device class */
296 if (IsEqualGUID(&(device_info_data->ClassGuid), &GUID_DEVCLASS_NETUIO))
297 dev->kdrv = RTE_PCI_KDRV_NIC_UIO;
299 dev->kdrv = RTE_PCI_KDRV_NONE;
303 pci_scan_one(HDEVINFO dev_info, PSP_DEVINFO_DATA device_info_data)
305 struct rte_pci_device *dev;
307 char pci_device_info[REGSTR_VAL_MAX_HCID_LEN];
308 struct rte_pci_addr addr;
309 struct rte_pci_id pci_id;
311 dev = malloc(sizeof(*dev));
315 memset(dev, 0, sizeof(*dev));
317 ret = get_pci_hardware_id(dev_info, device_info_data,
318 pci_device_info, sizeof(pci_device_info));
322 ret = parse_pci_hardware_id((const char *)&pci_device_info, &pci_id);
325 * We won't add this device, but we want to continue
326 * looking for supported devices
328 ret = ERROR_CONTINUE;
332 ret = get_device_pci_address(dev_info, device_info_data, &addr);
338 dev->max_vfs = 0; /* TODO: get max_vfs */
342 set_kernel_driver_type(device_info_data, dev);
345 if (get_device_resource_info(dev_info, device_info_data, dev)
350 /* device is valid, add in list (sorted) */
351 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
352 rte_pci_add_device(dev);
354 struct rte_pci_device *dev2 = NULL;
357 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
358 ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
361 } else if (ret < 0) {
362 rte_pci_insert_device(dev2, dev);
363 } else { /* already registered */
364 dev2->kdrv = dev->kdrv;
365 dev2->max_vfs = dev->max_vfs;
366 memmove(dev2->mem_resource, dev->mem_resource,
367 sizeof(dev->mem_resource));
372 rte_pci_add_device(dev);
383 * Scan the contents of the PCI bus
384 * and add all network class devices into the devices list.
390 DWORD device_index = 0, found_device = 0;
392 SP_DEVINFO_DATA device_info_data;
394 /* for debug purposes, PCI can be disabled */
395 if (!rte_eal_has_pci())
398 dev_info = SetupDiGetClassDevs(NULL, TEXT("PCI"), NULL,
399 DIGCF_PRESENT | DIGCF_ALLCLASSES);
400 if (dev_info == INVALID_HANDLE_VALUE) {
401 RTE_LOG_WIN32_ERR("SetupDiGetClassDevs(pci_scan)");
402 RTE_LOG(ERR, EAL, "Unable to enumerate PCI devices.\n");
406 device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
409 while (SetupDiEnumDeviceInfo(dev_info, device_index,
410 &device_info_data)) {
412 /* we only want to enumerate net & netuio class devices */
413 if (IsEqualGUID(&(device_info_data.ClassGuid),
414 &GUID_DEVCLASS_NET) ||
415 IsEqualGUID(&(device_info_data.ClassGuid),
416 &GUID_DEVCLASS_NETUIO)) {
417 ret = pci_scan_one(dev_info, &device_info_data);
418 if (ret == ERROR_SUCCESS)
420 else if (ret != ERROR_CONTINUE)
423 memset(&device_info_data, 0, sizeof(SP_DEVINFO_DATA));
424 device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
427 RTE_LOG(DEBUG, EAL, "PCI scan found %lu devices\n", found_device);
430 if (dev_info != INVALID_HANDLE_VALUE)
431 SetupDiDestroyDeviceInfoList(dev_info);