1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
4 #include <rte_windows.h>
10 #include "pci_netuio.h"
14 #if defined RTE_TOOLCHAIN_GCC && (__MINGW64_VERSION_MAJOR < 8)
15 #include <devpropdef.h>
16 DEFINE_DEVPROPKEY(DEVPKEY_Device_Numa_Node, 0x540b947e, 0x8b40, 0x45bc,
17 0xa8, 0xa2, 0x6a, 0x0b, 0x89, 0x4c, 0xbd, 0xa2, 3);
21 * This code is used to simulate a PCI probe by parsing information in
22 * the registry hive for PCI devices.
25 /* The functions below are not implemented on Windows,
26 * but need to be defined for compilation purposes
31 rte_pci_map_device(struct rte_pci_device *dev __rte_unused)
33 /* This function is not implemented on Windows.
34 * We really should short-circuit the call to these functions by
35 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
36 * in the rte_pci_driver flags.
41 /* Unmap pci device */
43 rte_pci_unmap_device(struct rte_pci_device *dev __rte_unused)
45 /* This function is not implemented on Windows.
46 * We really should short-circuit the call to these functions by
47 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
48 * in the rte_pci_driver flags.
52 /* Read PCI config space. */
54 rte_pci_read_config(const struct rte_pci_device *dev __rte_unused,
55 void *buf __rte_unused, size_t len __rte_unused,
56 off_t offset __rte_unused)
58 /* This function is not implemented on Windows.
59 * We really should short-circuit the call to these functions by
60 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
61 * in the rte_pci_driver flags.
66 /* Write PCI config space. */
68 rte_pci_write_config(const struct rte_pci_device *dev __rte_unused,
69 const void *buf __rte_unused, size_t len __rte_unused,
70 off_t offset __rte_unused)
72 /* This function is not implemented on Windows.
73 * We really should short-circuit the call to these functions by
74 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
75 * in the rte_pci_driver flags.
81 pci_device_iova_mode(const struct rte_pci_driver *pdrv __rte_unused,
82 const struct rte_pci_device *pdev __rte_unused)
84 /* This function is not implemented on Windows.
85 * We really should short-circuit the call to these functions by
86 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
87 * in the rte_pci_driver flags.
93 rte_pci_ioport_map(struct rte_pci_device *dev __rte_unused,
94 int bar __rte_unused, struct rte_pci_ioport *p __rte_unused)
96 /* This function is not implemented on Windows.
97 * We really should short-circuit the call to these functions by
98 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
99 * in the rte_pci_driver flags.
106 rte_pci_ioport_read(struct rte_pci_ioport *p __rte_unused,
107 void *data __rte_unused, size_t len __rte_unused,
108 off_t offset __rte_unused)
110 /* This function is not implemented on Windows.
111 * We really should short-circuit the call to these functions by
112 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
113 * in the rte_pci_driver flags.
118 rte_pci_ioport_unmap(struct rte_pci_ioport *p __rte_unused)
120 /* This function is not implemented on Windows.
121 * We really should short-circuit the call to these functions by
122 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
123 * in the rte_pci_driver flags.
129 pci_device_iommu_support_va(const struct rte_pci_device *dev __rte_unused)
131 /* This function is not implemented on Windows.
132 * We really should short-circuit the call to these functions by
133 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
134 * in the rte_pci_driver flags.
140 rte_pci_ioport_write(struct rte_pci_ioport *p __rte_unused,
141 const void *data __rte_unused, size_t len __rte_unused,
142 off_t offset __rte_unused)
144 /* This function is not implemented on Windows.
145 * We really should short-circuit the call to these functions by
146 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
147 * in the rte_pci_driver flags.
151 /* remap the PCI resource of a PCI device in anonymous virtual memory */
153 pci_uio_remap_resource(struct rte_pci_device *dev __rte_unused)
155 /* This function is not implemented on Windows.
156 * We really should short-circuit the call to these functions by
157 * clearing the RTE_PCI_DRV_NEED_MAPPING flag
158 * in the rte_pci_driver flags.
164 get_device_pci_address(HDEVINFO dev_info,
165 PSP_DEVINFO_DATA device_info_data, struct rte_pci_addr *addr)
168 ULONG bus_num, dev_and_func;
170 res = SetupDiGetDeviceRegistryProperty(dev_info, device_info_data,
171 SPDRP_BUSNUMBER, NULL, (PBYTE)&bus_num, sizeof(bus_num), NULL);
174 "SetupDiGetDeviceRegistryProperty(SPDRP_BUSNUMBER)");
178 res = SetupDiGetDeviceRegistryProperty(dev_info, device_info_data,
179 SPDRP_ADDRESS, NULL, (PBYTE)&dev_and_func, sizeof(dev_and_func),
183 "SetupDiGetDeviceRegistryProperty(SPDRP_ADDRESS)");
187 addr->domain = (bus_num >> 8) & 0xffff;
188 addr->bus = bus_num & 0xff;
189 addr->devid = dev_and_func >> 16;
190 addr->function = dev_and_func & 0xffff;
195 get_device_resource_info(HDEVINFO dev_info,
196 PSP_DEVINFO_DATA dev_info_data, struct rte_pci_device *dev)
198 DEVPROPTYPE property_type;
204 case RTE_PCI_KDRV_NONE:
205 /* mem_resource - Unneeded for RTE_PCI_KDRV_NONE */
206 dev->mem_resource[0].phys_addr = 0;
207 dev->mem_resource[0].len = 0;
208 dev->mem_resource[0].addr = NULL;
210 case RTE_PCI_KDRV_NIC_UIO:
211 /* get device info from netuio kernel driver */
212 ret = get_netuio_device_info(dev_info, dev_info_data, dev);
215 "Could not retrieve device info for PCI device "
217 dev->addr.domain, dev->addr.bus,
218 dev->addr.devid, dev->addr.function);
223 /* kernel driver type is unsupported */
225 "Kernel driver type for PCI device " PCI_PRI_FMT ","
227 dev->addr.domain, dev->addr.bus,
228 dev->addr.devid, dev->addr.function);
232 /* Get NUMA node using DEVPKEY_Device_Numa_Node */
233 res = SetupDiGetDevicePropertyW(dev_info, dev_info_data,
234 &DEVPKEY_Device_Numa_Node, &property_type,
235 (BYTE *)&numa_node, sizeof(numa_node), NULL, 0);
237 RTE_LOG_WIN32_ERR("SetupDiGetDevicePropertyW"
238 "(DEVPKEY_Device_Numa_Node)");
241 dev->device.numa_node = numa_node;
243 return ERROR_SUCCESS;
247 * get string that contains the list of hardware IDs for a device
250 get_pci_hardware_id(HDEVINFO dev_info, PSP_DEVINFO_DATA device_info_data,
251 char *pci_device_info, size_t pci_device_info_len)
255 /* Retrieve PCI device IDs */
256 res = SetupDiGetDeviceRegistryPropertyA(dev_info, device_info_data,
257 SPDRP_HARDWAREID, NULL, (BYTE *)pci_device_info,
258 pci_device_info_len, NULL);
261 "SetupDiGetDeviceRegistryPropertyA(SPDRP_HARDWAREID)");
269 * parse the SPDRP_HARDWAREID output and assign to rte_pci_id
272 parse_pci_hardware_id(const char *buf, struct rte_pci_id *pci_id)
275 uint16_t vendor_id, device_id;
276 uint32_t subvendor_id = 0;
278 ids = sscanf_s(buf, "PCI\\VEN_%" PRIx16 "&DEV_%" PRIx16 "&SUBSYS_%"
279 PRIx32, &vendor_id, &device_id, &subvendor_id);
283 pci_id->vendor_id = vendor_id;
284 pci_id->device_id = device_id;
285 pci_id->subsystem_device_id = subvendor_id >> 16;
286 pci_id->subsystem_vendor_id = subvendor_id & 0xffff;
291 set_kernel_driver_type(PSP_DEVINFO_DATA device_info_data,
292 struct rte_pci_device *dev)
294 /* set kernel driver type based on device class */
295 if (IsEqualGUID(&(device_info_data->ClassGuid), &GUID_DEVCLASS_NETUIO))
296 dev->kdrv = RTE_PCI_KDRV_NIC_UIO;
298 dev->kdrv = RTE_PCI_KDRV_NONE;
302 pci_scan_one(HDEVINFO dev_info, PSP_DEVINFO_DATA device_info_data)
304 struct rte_pci_device *dev;
306 char pci_device_info[PATH_MAX];
307 struct rte_pci_addr addr;
308 struct rte_pci_id pci_id;
310 dev = malloc(sizeof(*dev));
314 memset(dev, 0, sizeof(*dev));
316 ret = get_pci_hardware_id(dev_info, device_info_data,
317 pci_device_info, PATH_MAX);
321 ret = parse_pci_hardware_id((const char *)&pci_device_info, &pci_id);
324 * We won't add this device, but we want to continue
325 * looking for supported devices
327 ret = ERROR_CONTINUE;
331 ret = get_device_pci_address(dev_info, device_info_data, &addr);
337 dev->max_vfs = 0; /* TODO: get max_vfs */
341 set_kernel_driver_type(device_info_data, dev);
344 if (get_device_resource_info(dev_info, device_info_data, dev)
349 /* device is valid, add in list (sorted) */
350 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
351 rte_pci_add_device(dev);
353 struct rte_pci_device *dev2 = NULL;
356 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
357 ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
360 } else if (ret < 0) {
361 rte_pci_insert_device(dev2, dev);
362 } else { /* already registered */
363 dev2->kdrv = dev->kdrv;
364 dev2->max_vfs = dev->max_vfs;
365 memmove(dev2->mem_resource, dev->mem_resource,
366 sizeof(dev->mem_resource));
371 rte_pci_add_device(dev);
382 * Scan the contents of the PCI bus
383 * and add all network class devices into the devices list.
389 DWORD device_index = 0, found_device = 0;
391 SP_DEVINFO_DATA device_info_data;
393 /* for debug purposes, PCI can be disabled */
394 if (!rte_eal_has_pci())
397 dev_info = SetupDiGetClassDevs(NULL, TEXT("PCI"), NULL,
398 DIGCF_PRESENT | DIGCF_ALLCLASSES);
399 if (dev_info == INVALID_HANDLE_VALUE) {
400 RTE_LOG_WIN32_ERR("SetupDiGetClassDevs(pci_scan)");
401 RTE_LOG(ERR, EAL, "Unable to enumerate PCI devices.\n");
405 device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
408 while (SetupDiEnumDeviceInfo(dev_info, device_index,
409 &device_info_data)) {
411 /* we only want to enumerate net & netuio class devices */
412 if (IsEqualGUID(&(device_info_data.ClassGuid),
413 &GUID_DEVCLASS_NET) ||
414 IsEqualGUID(&(device_info_data.ClassGuid),
415 &GUID_DEVCLASS_NETUIO)) {
416 ret = pci_scan_one(dev_info, &device_info_data);
417 if (ret == ERROR_SUCCESS)
419 else if (ret != ERROR_CONTINUE)
422 memset(&device_info_data, 0, sizeof(SP_DEVINFO_DATA));
423 device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
426 RTE_LOG(DEBUG, EAL, "PCI scan found %lu devices\n", found_device);
429 if (dev_info != INVALID_HANDLE_VALUE)
430 SetupDiDestroyDeviceInfoList(dev_info);