1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation.
7 #include <rte_windows.h>
13 #include <ddk/ndisguid.h>
19 #include "pci_netuio.h"
22 send_ioctl(HANDLE f, DWORD ioctl,
23 void *in_buf, DWORD in_buf_size, void *out_buf, DWORD out_buf_size)
28 res = DeviceIoControl(f, ioctl, in_buf, in_buf_size,
29 out_buf, out_buf_size, &bytes_ret, NULL);
31 RTE_LOG_WIN32_ERR("DeviceIoControl:IOCTL query failed");
39 get_netuio_device_information_set(HDEVINFO dev_info,
40 PSP_DEVINFO_DATA dev_info_data)
43 DWORD required_size = 0;
44 TCHAR dev_instance_id[MAX_DEVICENAME_SZ];
45 HDEVINFO di_set = INVALID_HANDLE_VALUE;
47 /* obtain the driver interface for this device */
48 res = SetupDiGetDeviceInstanceId(dev_info, dev_info_data,
49 dev_instance_id, sizeof(dev_instance_id), &required_size);
51 RTE_LOG_WIN32_ERR("SetupDiGetDeviceInstanceId");
55 /* return the device information set for this device */
56 di_set = SetupDiGetClassDevs(&GUID_DEVINTERFACE_NETUIO,
57 dev_instance_id, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
58 if (di_set == INVALID_HANDLE_VALUE) {
59 RTE_LOG_WIN32_ERR("SetupDiGetClassDevs(device information set)");
66 static PSP_DEVICE_INTERFACE_DETAIL_DATA
67 get_netuio_device_interface_detail(HDEVINFO di_set)
70 DWORD required_size = 0;
71 SP_DEVICE_INTERFACE_DATA dev_ifx_data = { 0 };
72 PSP_DEVICE_INTERFACE_DETAIL_DATA dev_ifx_detail = NULL;
74 dev_ifx_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
76 /* enumerate the netUIO interfaces for this device information set */
77 res = SetupDiEnumDeviceInterfaces(di_set, 0, &GUID_DEVINTERFACE_NETUIO,
80 RTE_LOG_WIN32_ERR("SetupDiEnumDeviceInterfaces: no device interface");
84 /* request and allocate required size for the device interface detail */
86 res = SetupDiGetDeviceInterfaceDetail(di_set, &dev_ifx_data, NULL, 0,
87 &required_size, NULL);
89 /* ERROR_INSUFFICIENT_BUFFER is expected */
90 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
91 RTE_LOG_WIN32_ERR("SetupDiGetDeviceInterfaceDetail");
96 dev_ifx_detail = malloc(required_size);
97 if (!dev_ifx_detail) {
98 RTE_LOG(ERR, EAL, "Could not allocate memory for dev interface.\n");
101 dev_ifx_detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
103 res = SetupDiGetDeviceInterfaceDetail(di_set, &dev_ifx_data,
104 dev_ifx_detail, required_size, NULL, NULL);
106 RTE_LOG_WIN32_ERR("SetupDiGetDeviceInterfaceDetail");
107 free(dev_ifx_detail);
108 dev_ifx_detail = NULL;
113 return dev_ifx_detail;
117 * get device resource information by sending ioctl to netuio driver
120 get_netuio_device_info(HDEVINFO dev_info, PSP_DEVINFO_DATA dev_info_data,
121 struct rte_pci_device *dev)
124 HDEVINFO di_set = INVALID_HANDLE_VALUE;
125 PSP_DEVICE_INTERFACE_DETAIL_DATA dev_ifx_detail = NULL;
126 HANDLE netuio = INVALID_HANDLE_VALUE;
127 struct device_info hw_info = { 0 };
130 /* obtain the device information set for this device */
131 di_set = get_netuio_device_information_set(dev_info, dev_info_data);
132 if (di_set == INVALID_HANDLE_VALUE)
135 /* obtain the device interface detail for this device */
136 dev_ifx_detail = get_netuio_device_interface_detail(di_set);
140 /* open the kernel driver */
141 netuio = CreateFile(dev_ifx_detail->DevicePath,
142 GENERIC_READ | GENERIC_WRITE,
143 FILE_SHARE_READ | FILE_SHARE_WRITE,
146 FILE_ATTRIBUTE_NORMAL,
148 if (netuio == INVALID_HANDLE_VALUE) {
149 RTE_LOG_WIN32_ERR("CreateFile");
150 RTE_LOG(ERR, EAL, "Unable to open driver file \"%s\".\n",
151 dev_ifx_detail->DevicePath);
155 /* send ioctl to retrieve device information */
156 if (send_ioctl(netuio, IOCTL_NETUIO_MAP_HW_INTO_USERSPACE, NULL, 0,
157 &hw_info, sizeof(hw_info)) != ERROR_SUCCESS) {
158 RTE_LOG(ERR, EAL, "Unable to send ioctl to driver.\n");
162 /* set relevant values into the dev structure */
163 for (idx = 0; idx < PCI_MAX_RESOURCE; idx++) {
164 dev->mem_resource[idx].phys_addr =
165 hw_info.hw[idx].phys_addr.QuadPart;
166 dev->mem_resource[idx].addr =
167 hw_info.hw[idx].user_mapped_virt_addr;
168 dev->mem_resource[idx].len = hw_info.hw[idx].size;
173 if (ret != ERROR_SUCCESS) {
174 /* Only close the handle to the driver in case of an error.
175 * Otherwise, we want to keep the handle open. Closing it
176 * here will cause the driver to unmap all the process-mapped
177 * values resulting in invalid addresses.
179 if (netuio != INVALID_HANDLE_VALUE)
183 free(dev_ifx_detail);
185 if (di_set != INVALID_HANDLE_VALUE)
186 SetupDiDestroyDeviceInfoList(di_set);