1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation.
5 #include <rte_windows.h>
11 #include "pci_netuio.h"
14 send_ioctl(HANDLE f, DWORD ioctl,
15 void *in_buf, DWORD in_buf_size, void *out_buf, DWORD out_buf_size)
20 res = DeviceIoControl(f, ioctl, in_buf, in_buf_size,
21 out_buf, out_buf_size, &bytes_ret, NULL);
23 RTE_LOG_WIN32_ERR("DeviceIoControl:IOCTL query failed");
31 get_netuio_device_information_set(HDEVINFO dev_info,
32 PSP_DEVINFO_DATA dev_info_data)
35 DWORD required_size = 0;
36 TCHAR dev_instance_id[MAX_DEVICENAME_SZ];
37 HDEVINFO di_set = INVALID_HANDLE_VALUE;
39 /* obtain the driver interface for this device */
40 res = SetupDiGetDeviceInstanceId(dev_info, dev_info_data,
41 dev_instance_id, sizeof(dev_instance_id), &required_size);
43 RTE_LOG_WIN32_ERR("SetupDiGetDeviceInstanceId");
47 /* return the device information set for this device */
48 di_set = SetupDiGetClassDevs(&GUID_DEVINTERFACE_NETUIO,
49 dev_instance_id, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
50 if (di_set == INVALID_HANDLE_VALUE) {
51 RTE_LOG_WIN32_ERR("SetupDiGetClassDevs(device information set)");
58 static PSP_DEVICE_INTERFACE_DETAIL_DATA
59 get_netuio_device_interface_detail(HDEVINFO di_set)
62 DWORD required_size = 0;
63 SP_DEVICE_INTERFACE_DATA dev_ifx_data = { 0 };
64 PSP_DEVICE_INTERFACE_DETAIL_DATA dev_ifx_detail = NULL;
66 dev_ifx_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
68 /* enumerate the netUIO interfaces for this device information set */
69 res = SetupDiEnumDeviceInterfaces(di_set, 0, &GUID_DEVINTERFACE_NETUIO,
72 RTE_LOG_WIN32_ERR("SetupDiEnumDeviceInterfaces: no device interface");
76 /* request and allocate required size for the device interface detail */
78 res = SetupDiGetDeviceInterfaceDetail(di_set, &dev_ifx_data, NULL, 0,
79 &required_size, NULL);
81 /* ERROR_INSUFFICIENT_BUFFER is expected */
82 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
83 RTE_LOG_WIN32_ERR("SetupDiGetDeviceInterfaceDetail");
88 dev_ifx_detail = malloc(required_size);
89 if (!dev_ifx_detail) {
90 RTE_LOG(ERR, EAL, "Could not allocate memory for dev interface.\n");
93 dev_ifx_detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
95 res = SetupDiGetDeviceInterfaceDetail(di_set, &dev_ifx_data,
96 dev_ifx_detail, required_size, NULL, NULL);
98 RTE_LOG_WIN32_ERR("SetupDiGetDeviceInterfaceDetail");
100 dev_ifx_detail = NULL;
105 return dev_ifx_detail;
109 * get device resource information by sending ioctl to netuio driver
112 get_netuio_device_info(HDEVINFO dev_info, PSP_DEVINFO_DATA dev_info_data,
113 struct rte_pci_device *dev)
116 HDEVINFO di_set = INVALID_HANDLE_VALUE;
117 PSP_DEVICE_INTERFACE_DETAIL_DATA dev_ifx_detail = NULL;
118 HANDLE netuio = INVALID_HANDLE_VALUE;
119 struct device_info hw_info = { 0 };
122 /* obtain the device information set for this device */
123 di_set = get_netuio_device_information_set(dev_info, dev_info_data);
124 if (di_set == INVALID_HANDLE_VALUE)
127 /* obtain the device interface detail for this device */
128 dev_ifx_detail = get_netuio_device_interface_detail(di_set);
132 /* open the kernel driver */
133 netuio = CreateFile(dev_ifx_detail->DevicePath,
134 GENERIC_READ | GENERIC_WRITE,
135 FILE_SHARE_READ | FILE_SHARE_WRITE,
138 FILE_ATTRIBUTE_NORMAL,
140 if (netuio == INVALID_HANDLE_VALUE) {
141 RTE_LOG_WIN32_ERR("CreateFile");
142 RTE_LOG(ERR, EAL, "Unable to open driver file \"%s\".\n",
143 dev_ifx_detail->DevicePath);
147 /* send ioctl to retrieve device information */
148 if (send_ioctl(netuio, IOCTL_NETUIO_MAP_HW_INTO_USERSPACE, NULL, 0,
149 &hw_info, sizeof(hw_info)) != ERROR_SUCCESS) {
150 RTE_LOG(ERR, EAL, "Unable to send ioctl to driver.\n");
154 /* set relevant values into the dev structure */
155 for (idx = 0; idx < PCI_MAX_RESOURCE; idx++) {
156 dev->mem_resource[idx].phys_addr =
157 hw_info.hw[idx].phys_addr.QuadPart;
158 dev->mem_resource[idx].addr =
159 hw_info.hw[idx].user_mapped_virt_addr;
160 dev->mem_resource[idx].len = hw_info.hw[idx].size;
165 if (ret != ERROR_SUCCESS) {
166 /* Only close the handle to the driver in case of an error.
167 * Otherwise, we want to keep the handle open. Closing it
168 * here will cause the driver to unmap all the process-mapped
169 * values resulting in invalid addresses.
171 if (netuio != INVALID_HANDLE_VALUE)
176 free(dev_ifx_detail);
178 if (di_set != INVALID_HANDLE_VALUE)
179 SetupDiDestroyDeviceInfoList(di_set);