Debian Codegraph @

windows/netuio/netuio_dev.c

raw symbols SCIP
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Microsoft Corporation.
3 */
4
5 #include <stdio.h>
6 #include "netuio_drv.h"
7
8 #include <wdmguid.h>
9 #include <ntstrsafe.h>
10
11 #ifdef ALLOC_PRAGMA
12 #pragma alloc_text (PAGE, netuio_create_device)
13 #pragma alloc_text (PAGE, netuio_map_hw_resources)
14 #pragma alloc_text (PAGE, netuio_free_hw_resources)
15 #endif
16
17 static NTSTATUS
18 get_pci_device_info(_In_ WDFOBJECT device)
19 {
20 NTSTATUS status = STATUS_UNSUCCESSFUL;
21
22 PNETUIO_CONTEXT_DATA ctx;
23 ctx = netuio_get_context_data(device);
24
25 ctx->wdf_device = device; // Store for later use
26
27 // Obtain the BUS_INTERFACE_STANDARD interface from the Bus Driver
28 status = WdfFdoQueryForInterface(device,
29 &GUID_BUS_INTERFACE_STANDARD,
30 (PINTERFACE)&ctx->bus_interface,
31 sizeof(BUS_INTERFACE_STANDARD),
32 1,
33 NULL);
34 if (!NT_SUCCESS(status))
35 return status;
36
37 // Retrieve the B:D:F details of our device
38 PDEVICE_OBJECT pdo = NULL;
39 pdo = WdfDeviceWdmGetPhysicalDevice(device);
40 if (pdo) {
41 ULONG prop = 0, length = 0;
42 status = IoGetDeviceProperty(pdo, DevicePropertyBusNumber, sizeof(ULONG), (PVOID)&ctx->addr.bus_num, &length);
43 if (!NT_SUCCESS(status)) {
44 return status;
45 }
46
47 status = IoGetDeviceProperty(pdo, DevicePropertyAddress, sizeof(ULONG), (PVOID)&prop, &length);
48 if (!NT_SUCCESS(status)) {
49 return status;
50 }
51
52 ctx->addr.func_num = prop & 0x0000FFFF;
53 ctx->addr.dev_num = ((prop >> 16) & 0x0000FFFF);
54 }
55
56 return status;
57 }
58
59 static NTSTATUS
60 create_device_specific_symbolic_link(_In_ WDFOBJECT device)
61 {
62 DECLARE_UNICODE_STRING_SIZE(symbolic_link, NETUIO_MAX_SYMLINK_LEN);
63
64 PNETUIO_CONTEXT_DATA ctx;
65 ctx = netuio_get_context_data(device);
66
67 // Build symbolic link name as <netuio_symbolic_link>_BDF (bus/device/func)
68 RtlUnicodeStringPrintf(&symbolic_link,
69 L"%s_%04x%02x%02x",
70 NETUIO_DEVICE_SYMBOLIC_LINK_UNICODE,
71 ctx->addr.bus_num,
72 ctx->addr.dev_num,
73 ctx->addr.func_num);
74
75 return WdfDeviceCreateSymbolicLink(device, &symbolic_link);
76 }
77
78 /*
79 Routine Description:
80 Worker routine called to create a device and its software resources.
81
82 Return Value:
83 NTSTATUS
84 */
85 _Use_decl_annotations_
86 NTSTATUS
87 netuio_create_device(PWDFDEVICE_INIT DeviceInit)
88 {
89 NTSTATUS status;
90 WDFDEVICE device = NULL;
91
92 WDF_OBJECT_ATTRIBUTES deviceAttributes;
93 WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
94 WDF_FILEOBJECT_CONFIG fileConfig;
95 WDF_OBJECT_ATTRIBUTES fileAttributes;
96
97 PAGED_CODE();
98
99 // Register PnP power management callbacks
100 WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
101 pnpPowerCallbacks.EvtDevicePrepareHardware = netuio_evt_prepare_hw;
102 pnpPowerCallbacks.EvtDeviceReleaseHardware = netuio_evt_release_hw;
103 WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);
104
105 // Register callbacks for when a HANDLE is opened or closed.
106 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&fileAttributes, NETUIO_FILE_CONTEXT_DATA);
107 WDF_FILEOBJECT_CONFIG_INIT(&fileConfig, WDF_NO_EVENT_CALLBACK, WDF_NO_EVENT_CALLBACK, netuio_evt_file_cleanup);
108 WdfDeviceInitSetFileObjectConfig(DeviceInit, &fileConfig, &fileAttributes);
109
110 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&deviceAttributes, NETUIO_CONTEXT_DATA);
111 WdfDeviceInitSetIoInCallerContextCallback(DeviceInit, netuio_evt_IO_in_caller_context);
112
113 status = WdfDeviceCreate(&DeviceInit, &deviceAttributes, &device);
114
115 if (NT_SUCCESS(status)) {
116 // Create a device interface so that applications can find and talk to us.
117 status = WdfDeviceCreateDeviceInterface(device, &GUID_DEVINTERFACE_netUIO, NULL);
118 }
119
120 if (NT_SUCCESS(status)) {
121 // Retrieve and store PCI information
122 status = get_pci_device_info(device);
123 }
124
125 if (NT_SUCCESS(status)) {
126 // Create a symbolic link name for user-space access
127 status = create_device_specific_symbolic_link(device);
128 }
129
130 if (NT_SUCCESS(status)) {
131 // Initialize the I/O Package and any Queues
132 status = netuio_queue_initialize(device);
133 }
134
135 return status;
136 }
137
138 _Use_decl_annotations_
139 NTSTATUS
140 netuio_map_hw_resources(WDFDEVICE Device, WDFCMRESLIST Resources, WDFCMRESLIST ResourcesTranslated)
141 {
142 UNREFERENCED_PARAMETER(Resources);
143
144 NTSTATUS status;
145
146 PNETUIO_CONTEXT_DATA ctx;
147 ctx = netuio_get_context_data(Device);
148
149 PCI_COMMON_HEADER pci_config = {0};
150 ULONG bytes_returned;
151
152 // Read PCI configuration space
153 bytes_returned = ctx->bus_interface.GetBusData(
154 ctx->bus_interface.Context,
155 PCI_WHICHSPACE_CONFIG,
156 &pci_config,
157 0,
158 sizeof(pci_config));
159
160 if (bytes_returned != sizeof(pci_config)) {
161 status = STATUS_NOT_SUPPORTED;
162 goto end;
163 }
164
165 // Device type is implictly enforced by .inf
166 ASSERT(PCI_CONFIGURATION_TYPE(&pci_config) == PCI_DEVICE_TYPE);
167
168 RtlZeroMemory(ctx->bar, sizeof(ctx->bar));
169 RtlZeroMemory(ctx->dpdk_hw, sizeof(ctx->dpdk_hw));
170
171 PCM_PARTIAL_RESOURCE_DESCRIPTOR descriptor;
172 ULONG next_descriptor = 0;
173
174 /*
175 * ResourcesTranslated report MMIO BARs in the correct order, but their
176 * indices differ from physical ones.
177
178 * For example:
179 * The values returned by WdfCmResourceListGetDescriptor can be condensed like this:
180 * Bar[0] = c840000c
181 * Bar[1] = c800000c
182 * Bar[2] = c844000c
183
184 * But the BAR array has to be filled like this :
185 * Bar[0] = c840000c
186 * Bar[1] = null
187 * Bar[2] = c800000c
188 * Bar[3] = null
189 * Bar[4] = c844000c
190 * Bar[5] = null
191
192 * Traverse PCI configuration manually to maintain physical index,
193 * searching for the next MMIO resource each time.
194 */
195 for (INT bar_index = 0; bar_index < PCI_MAX_BAR; bar_index++) {
196 ULONG bar_value = pci_config.u.type0.BaseAddresses[bar_index];
197
198 if (bar_value == 0) {
199 continue;
200 }
201
202 // Find next CmResourceTypeMemory
203 do {
204 descriptor = WdfCmResourceListGetDescriptor(ResourcesTranslated, next_descriptor);
205 next_descriptor++;
206
207 if (descriptor == NULL) {
208 status = STATUS_DEVICE_CONFIGURATION_ERROR;
209 goto end;
210 }
211 } while ((descriptor->Type != CmResourceTypeMemory) ||
212 !(descriptor->Flags & CM_RESOURCE_MEMORY_BAR));
213
214 // Retrieve and map the BARs
215 ctx->bar[bar_index].base_addr.QuadPart = descriptor->u.Memory.Start.QuadPart;
216 ctx->bar[bar_index].size = descriptor->u.Memory.Length;
217 ctx->bar[bar_index].virt_addr = MmMapIoSpace(descriptor->u.Memory.Start,
218 descriptor->u.Memory.Length,
219 MmNonCached);
220 if (ctx->bar[bar_index].virt_addr == NULL) {
221 status = STATUS_INSUFFICIENT_RESOURCES;
222 goto end;
223 }
224
225 // Allocate an MDL for the device BAR, so we can map it to the user's process context later.
226 ctx->dpdk_hw[bar_index].mdl = IoAllocateMdl(ctx->bar[bar_index].virt_addr,
227 (ULONG)ctx->bar[bar_index].size,
228 FALSE,
229 FALSE,
230 NULL);
231 if (!ctx->dpdk_hw[bar_index].mdl) {
232 status = STATUS_INSUFFICIENT_RESOURCES;
233 goto end;
234 }
235
236 ctx->dpdk_hw[bar_index].mem.size = ctx->bar[bar_index].size;
237
238 // Skip the next BAR slot used by the current 64-bit address.
239 if (bar_value & PCI_TYPE_64BIT) {
240 bar_index++;
241 }
242 } // for bar_index
243
244 status = STATUS_SUCCESS;
245
246 end:
247 if (status != STATUS_SUCCESS) {
248 netuio_free_hw_resources(Device);
249 }
250
251 return status;
252 }
253
254 _Use_decl_annotations_
255 VOID
256 netuio_free_hw_resources(WDFDEVICE Device)
257 {
258 PNETUIO_CONTEXT_DATA ctx;
259 ctx = netuio_get_context_data(Device);
260
261 for (UINT8 bar_index = 0; bar_index < PCI_MAX_BAR; bar_index++) {
262
263 // Free the allocated MDLs
264 if (ctx->dpdk_hw[bar_index].mdl) {
265 IoFreeMdl(ctx->dpdk_hw[bar_index].mdl);
266 }
267
268 // Unmap all the BAR regions previously mapped
269 if (ctx->bar[bar_index].virt_addr) {
270 MmUnmapIoSpace(ctx->bar[bar_index].virt_addr, ctx->bar[bar_index].size);
271 }
272 }
273
274 RtlZeroMemory(ctx->dpdk_hw, sizeof(ctx->dpdk_hw));
275 RtlZeroMemory(ctx->bar, sizeof(ctx->bar));
276 }