| 1 |
|
| 2 |
|
| 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; |
| 26 |
|
| 27 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 117 |
status = WdfDeviceCreateDeviceInterface(device, &GUID_DEVINTERFACE_netUIO, NULL); |
| 118 |
} |
| 119 |
|
| 120 |
if (NT_SUCCESS(status)) { |
| 121 |
|
| 122 |
status = get_pci_device_info(device); |
| 123 |
} |
| 124 |
|
| 125 |
if (NT_SUCCESS(status)) { |
| 126 |
|
| 127 |
status = create_device_specific_symbolic_link(device); |
| 128 |
} |
| 129 |
|
| 130 |
if (NT_SUCCESS(status)) { |
| 131 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 239 |
if (bar_value & PCI_TYPE_64BIT) { |
| 240 |
bar_index++; |
| 241 |
} |
| 242 |
} |
| 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 |
|
| 264 |
if (ctx->dpdk_hw[bar_index].mdl) { |
| 265 |
IoFreeMdl(ctx->dpdk_hw[bar_index].mdl); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 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 |
} |