win-pvdrivers

view xenpci/xenpci.c @ 75:b7863ede7a37

Created a 'xenenum' driver for when the device driver should attach to xen\???\xxx rather than xen\xxx
author James Harper <james.harper@bendigoit.com.au>
date Thu Dec 27 20:49:42 2007 +1100 (2007-12-27)
parents f74723639713
children dfa772949c6e
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #include "xenpci.h"
21 #include "hypercall.h"
22 #include <stdlib.h>
24 #define SHUTDOWN_PATH "control/shutdown"
25 #define BALLOON_PATH "memory/target"
27 DRIVER_INITIALIZE DriverEntry;
28 static NTSTATUS
29 XenPCI_AddDevice(WDFDRIVER Driver, PWDFDEVICE_INIT DeviceInit);
30 static NTSTATUS
31 XenPCI_PrepareHardware(WDFDEVICE hDevice, WDFCMRESLIST Resources, WDFCMRESLIST ResourcesTranslated);
32 static NTSTATUS
33 XenPCI_ReleaseHardware(WDFDEVICE Device, WDFCMRESLIST ResourcesTranslated);
34 static NTSTATUS
35 XenPCI_D0Entry(WDFDEVICE Device, WDF_POWER_DEVICE_STATE PreviousState);
36 static NTSTATUS
37 XenPCI_D0EntryPostInterruptsEnabled(WDFDEVICE Device, WDF_POWER_DEVICE_STATE PreviousState);
38 static NTSTATUS
39 XenPCI_D0Exit(WDFDEVICE Device, WDF_POWER_DEVICE_STATE TargetState);
40 static NTSTATUS
41 XenPCI_D0ExitPreInterruptsDisabled(WDFDEVICE Device, WDF_POWER_DEVICE_STATE TargetState);
42 static VOID
43 XenPCI_IoDefault(WDFQUEUE Queue, WDFREQUEST Request);
44 static VOID
45 XenPCI_IoRead(WDFQUEUE Queue, WDFREQUEST Request, size_t Length);
46 static NTSTATUS
47 XenPCI_InterruptEnable(WDFINTERRUPT Interrupt, WDFDEVICE AssociatedDevice);
48 static NTSTATUS
49 XenPCI_InterruptDisable(WDFINTERRUPT Interrupt, WDFDEVICE AssociatedDevice);
50 static NTSTATUS
51 XenPCI_ChildListCreateDevice(WDFCHILDLIST ChildList, PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER IdentificationDescription, PWDFDEVICE_INIT ChildInit);
52 static NTSTATUS
53 XenPCI_DeviceResourceRequirementsQuery(WDFDEVICE Device, WDFIORESREQLIST IoResourceRequirementsList);
54 static NTSTATUS
55 XenPCI_FilterRemoveResourceRequirements(WDFDEVICE Device, WDFIORESREQLIST IoResourceRequirementsList);
56 static NTSTATUS
57 XenPCI_FilterAddResourceRequirements(WDFDEVICE Device, WDFIORESREQLIST RequirementsList);
58 static NTSTATUS
59 XenPCI_RemoveAddedResources(WDFDEVICE Device, WDFCMRESLIST ResourcesRaw, WDFCMRESLIST ResourcesTranslated);
61 static VOID
62 XenBus_ShutdownHandler(char *Path, PVOID Data);
63 static VOID
64 XenBus_BalloonHandler(char *Path, PVOID Data);
65 static VOID
66 XenPCI_XenBusWatchHandler(char *Path, PVOID Data);
68 #ifdef ALLOC_PRAGMA
69 #pragma alloc_text (INIT, DriverEntry)
70 #pragma alloc_text (PAGE, XenPCI_AddDevice)
71 #endif
73 /* Global (driver-wide) variables */
74 static BOOLEAN AutoEnumerate;
75 static LIST_ENTRY ShutdownMsgList;
77 typedef struct {
78 LIST_ENTRY ListEntry;
79 ULONG Ptr;
80 // ULONG Len;
81 CHAR Buf[0];
82 } SHUTDOWN_MSG_ENTRY, *PSHUTDOWN_MSG_ENTRY;
84 static KSPIN_LOCK ShutdownMsgLock;
86 CM_PARTIAL_RESOURCE_DESCRIPTOR InterruptRaw;
87 CM_PARTIAL_RESOURCE_DESCRIPTOR InterruptTranslated;
89 NTSTATUS
90 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
91 {
92 WDF_DRIVER_CONFIG config;
93 NTSTATUS status;
94 UNICODE_STRING RegKeyName;
95 UNICODE_STRING RegValueName;
96 HANDLE RegHandle;
97 OBJECT_ATTRIBUTES RegObjectAttributes;
98 char Buf[200];
99 ULONG BufLen = 200;
100 PKEY_VALUE_PARTIAL_INFORMATION KeyPartialValue;
101 int State = 0;
102 int StartPos = 0;
103 WCHAR *SystemStartOptions;
104 size_t SystemStartOptionsLen;
105 size_t i;
107 KdPrint((__DRIVER_NAME " --> DriverEntry\n"));
109 InitializeListHead(&ShutdownMsgList);
110 KeInitializeSpinLock(&ShutdownMsgLock);
112 RtlInitUnicodeString(&RegKeyName, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control");
113 InitializeObjectAttributes(&RegObjectAttributes, &RegKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);
114 status = ZwOpenKey(&RegHandle, KEY_READ, &RegObjectAttributes);
115 if(!NT_SUCCESS(status))
116 {
117 KdPrint((__DRIVER_NAME " ZwOpenKey returned %08x\n", status));
118 }
120 RtlInitUnicodeString(&RegValueName, L"SystemStartOptions");
121 status = ZwQueryValueKey(RegHandle, &RegValueName, KeyValuePartialInformation, Buf, BufLen, &BufLen);
122 if(!NT_SUCCESS(status))
123 {
124 KdPrint((__DRIVER_NAME " ZwQueryKeyValue returned %08x\n", status));
125 }
126 //KdPrint((__DRIVER_NAME " BufLen = %d\n", BufLen));
127 KeyPartialValue = (PKEY_VALUE_PARTIAL_INFORMATION)Buf;
128 KdPrint((__DRIVER_NAME " Buf = %ws\n", KeyPartialValue->Data));
129 SystemStartOptions = (WCHAR *)KeyPartialValue->Data;
131 AutoEnumerate = FALSE;
133 RtlStringCbLengthW(SystemStartOptions, KeyPartialValue->DataLength, &SystemStartOptionsLen);
135 for (i = 0; i <= SystemStartOptionsLen/2; i++)
136 {
137 //KdPrint((__DRIVER_NAME " pos = %d, state = %d, char = '%wc' (%d)\n", i, State, SystemStartOptions[i], SystemStartOptions[i]));
139 switch (State)
140 {
141 case 0:
142 if (SystemStartOptions[i] == L'G')
143 {
144 StartPos = i;
145 State = 2;
146 } else if (SystemStartOptions[i] != L' ')
147 {
148 State = 1;
149 }
150 break;
151 case 1:
152 if (SystemStartOptions[i] == L' ')
153 State = 0;
154 break;
155 case 2:
156 if (SystemStartOptions[i] == L'P')
157 State = 3;
158 else
159 State = 0;
160 break;
161 case 3:
162 if (SystemStartOptions[i] == L'L')
163 State = 4;
164 else
165 State = 0;
166 break;
167 case 4:
168 if (SystemStartOptions[i] == L'P')
169 State = 5;
170 else
171 State = 0;
172 break;
173 case 5:
174 if (SystemStartOptions[i] == L'V')
175 State = 6;
176 else
177 State = 0;
178 break;
179 case 6:
180 if (SystemStartOptions[i] == L' ' || SystemStartOptions[i] == 0)
181 AutoEnumerate = TRUE;
182 State = 0;
183 break;
184 }
185 }
187 KdPrint((__DRIVER_NAME " AutoEnumerate = %d\n", AutoEnumerate));
189 WDF_DRIVER_CONFIG_INIT(&config, XenPCI_AddDevice);
190 status = WdfDriverCreate(
191 DriverObject,
192 RegistryPath,
193 WDF_NO_OBJECT_ATTRIBUTES,
194 &config,
195 WDF_NO_HANDLE);
196 if(!NT_SUCCESS(status))
197 {
198 KdPrint((__DRIVER_NAME " WdfDriverCreate failed with status 0x%08x\n", status));
199 }
201 KdPrint((__DRIVER_NAME " <-- DriverEntry\n"));
203 return status;
204 }
206 static NTSTATUS
207 get_hypercall_stubs(WDFDEVICE Device)
208 {
209 PXENPCI_DEVICE_DATA xpdd = GetDeviceData(Device);
210 DWORD32 cpuid_output[4];
211 char xensig[13];
212 ULONG i;
213 ULONG pages;
214 ULONG msr;
216 __cpuid(cpuid_output, 0x40000000);
217 *(ULONG*)(xensig + 0) = cpuid_output[1];
218 *(ULONG*)(xensig + 4) = cpuid_output[2];
219 *(ULONG*)(xensig + 8) = cpuid_output[3];
220 xensig[12] = '\0';
221 KdPrint((__DRIVER_NAME " Xen Signature = %s, EAX = 0x%08x\n", xensig, cpuid_output[0]));
223 __cpuid(cpuid_output, 0x40000002);
224 pages = cpuid_output[0];
225 msr = cpuid_output[1];
226 //KdPrint((__DRIVER_NAME " Hypercall area is %u pages.\n", pages));
228 xpdd->hypercall_stubs = ExAllocatePoolWithTag(NonPagedPool, pages * PAGE_SIZE, XENPCI_POOL_TAG);
229 //KdPrint((__DRIVER_NAME " Hypercall area at %08x\n", hypercall_stubs));
231 if (!xpdd->hypercall_stubs)
232 return 1;
233 for (i = 0; i < pages; i++) {
234 ULONG pfn;
235 //pfn = vmalloc_to_pfn((char *)hypercall_stubs + i * PAGE_SIZE);
236 pfn = (ULONG)(MmGetPhysicalAddress(xpdd->hypercall_stubs + i * PAGE_SIZE).QuadPart >> PAGE_SHIFT);
237 //KdPrint((__DRIVER_NAME " pfn = %08X\n", pfn));
238 __writemsr(msr, ((ULONGLONG)pfn << PAGE_SHIFT) + i);
239 }
240 return STATUS_SUCCESS;
241 }
243 PHYSICAL_ADDRESS
244 XenPCI_AllocMMIO(WDFDEVICE Device, ULONG len)
245 {
246 PXENPCI_DEVICE_DATA xpdd = GetDeviceData(Device);
248 PHYSICAL_ADDRESS addr;
250 addr = xpdd->platform_mmio_addr;
251 addr.QuadPart += xpdd->platform_mmio_alloc;
252 xpdd->platform_mmio_alloc += len;
254 return addr;
255 }
257 static int
258 init_xen_info(WDFDEVICE Device)
259 {
260 PXENPCI_DEVICE_DATA xpdd = GetDeviceData(Device);
261 struct xen_add_to_physmap xatp;
262 int ret;
263 PHYSICAL_ADDRESS shared_info_area_unmapped;
265 //setup_xen_features();
266 //KdPrint((__DRIVER_NAME " init_xen_info Hypercall area at %08x\n", hypercall_stubs));
268 shared_info_area_unmapped = XenPCI_AllocMMIO(Device, PAGE_SIZE);
269 xatp.domid = DOMID_SELF;
270 xatp.idx = 0;
271 xatp.space = XENMAPSPACE_shared_info;
272 xatp.gpfn = (xen_pfn_t)(shared_info_area_unmapped.QuadPart >> PAGE_SHIFT);
273 ret = HYPERVISOR_memory_op(Device, XENMEM_add_to_physmap, &xatp);
274 //KdPrint((__DRIVER_NAME " ret = %d\n", ret));
276 xpdd->shared_info_area = MmMapIoSpace(shared_info_area_unmapped,
277 PAGE_SIZE, MmNonCached);
279 return 0;
280 }
282 static int
283 set_callback_irq(WDFDEVICE Device, ULONGLONG irq)
284 {
285 struct xen_hvm_param a;
286 int retval;
288 //KdPrint((__DRIVER_NAME " --> set_callback_irq\n"));
289 a.domid = DOMID_SELF;
290 a.index = HVM_PARAM_CALLBACK_IRQ;
291 a.value = irq;
292 retval = HYPERVISOR_hvm_op(Device, HVMOP_set_param, &a);
293 //KdPrint((__DRIVER_NAME " HYPERVISOR_hvm_op retval = %d\n", retval));
294 //KdPrint((__DRIVER_NAME " <-- set_callback_irq\n"));
295 return retval;
296 }
298 WDFQUEUE ReadQueue;
300 static NTSTATUS
301 XenPCI_AddDevice(
302 IN WDFDRIVER Driver,
303 IN PWDFDEVICE_INIT DeviceInit
304 )
305 {
306 NTSTATUS Status;
307 WDF_CHILD_LIST_CONFIG config;
308 WDF_OBJECT_ATTRIBUTES attributes;
309 WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
310 WDF_IO_QUEUE_CONFIG IoQConfig;
311 WDF_INTERRUPT_CONFIG InterruptConfig;
312 PNP_BUS_INFORMATION busInfo;
313 BUS_INTERFACE_STANDARD BusInterface;
314 DECLARE_CONST_UNICODE_STRING(DeviceName, L"\\Device\\XenShutdown");
315 DECLARE_CONST_UNICODE_STRING(SymbolicName, L"\\DosDevices\\XenShutdown");
316 WDFDEVICE Device;
317 PXENPCI_DEVICE_DATA xpdd;
319 UNREFERENCED_PARAMETER(Driver);
321 KdPrint((__DRIVER_NAME " --> DeviceAdd\n"));
323 WdfDeviceInitSetDeviceType(DeviceInit, FILE_DEVICE_BUS_EXTENDER);
324 WDF_CHILD_LIST_CONFIG_INIT(&config, sizeof(XENPCI_IDENTIFICATION_DESCRIPTION), XenPCI_ChildListCreateDevice);
325 WdfFdoInitSetDefaultChildListConfig(DeviceInit, &config, WDF_NO_OBJECT_ATTRIBUTES);
327 WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
328 pnpPowerCallbacks.EvtDevicePrepareHardware = XenPCI_PrepareHardware;
329 pnpPowerCallbacks.EvtDeviceReleaseHardware = XenPCI_ReleaseHardware;
330 pnpPowerCallbacks.EvtDeviceD0Entry = XenPCI_D0Entry;
331 pnpPowerCallbacks.EvtDeviceD0EntryPostInterruptsEnabled
332 = XenPCI_D0EntryPostInterruptsEnabled;
333 pnpPowerCallbacks.EvtDeviceD0ExitPreInterruptsDisabled
334 = XenPCI_D0ExitPreInterruptsDisabled;
335 pnpPowerCallbacks.EvtDeviceD0Exit = XenPCI_D0Exit;
336 WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);
338 WdfDeviceInitSetIoType(DeviceInit, WdfDeviceIoBuffered);
340 Status = WdfDeviceInitAssignName(DeviceInit, &DeviceName);
341 if (!NT_SUCCESS(Status))
342 {
343 KdPrint((__DRIVER_NAME " WdfDeviceInitAssignName failed 0x%08x\n", Status));
344 return Status;
345 }
347 /*initialize storage for the device context*/
348 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, XENPCI_DEVICE_DATA);
350 /*create a device instance.*/
351 Status = WdfDeviceCreate(&DeviceInit, &attributes, &Device);
352 if(!NT_SUCCESS(Status))
353 {
354 KdPrint((__DRIVER_NAME " WdfDeviceCreate failed with Status 0x%08x\n", Status));
355 return Status;
356 }
357 xpdd = GetDeviceData(Device);
358 xpdd->Device = Device;
360 WdfDeviceSetSpecialFileSupport(Device, WdfSpecialFilePaging, TRUE);
361 WdfDeviceSetSpecialFileSupport(Device, WdfSpecialFileHibernation, TRUE);
362 WdfDeviceSetSpecialFileSupport(Device, WdfSpecialFileDump, TRUE);
364 Status = WdfFdoQueryForInterface(Device, &GUID_BUS_INTERFACE_STANDARD, (PINTERFACE) &BusInterface, sizeof(BUS_INTERFACE_STANDARD), 1, NULL);
365 if(!NT_SUCCESS(Status))
366 {
367 KdPrint((__DRIVER_NAME " WdfFdoQueryForInterface (BusInterface) failed with Status 0x%08x\n", Status));
368 }
370 busInfo.BusTypeGuid = GUID_XENPCI_DEVCLASS;
371 busInfo.LegacyBusType = Internal;
372 busInfo.BusNumber = 0;
374 WdfDeviceSetBusInformationForChildren(Device, &busInfo);
376 WDF_INTERRUPT_CONFIG_INIT(&InterruptConfig, EvtChn_Interrupt, NULL); //EvtChn_InterruptDpc);
377 InterruptConfig.EvtInterruptEnable = XenPCI_InterruptEnable;
378 InterruptConfig.EvtInterruptDisable = XenPCI_InterruptDisable;
379 Status = WdfInterruptCreate(Device, &InterruptConfig, WDF_NO_OBJECT_ATTRIBUTES, &xpdd->XenInterrupt);
380 if (!NT_SUCCESS (Status))
381 {
382 KdPrint((__DRIVER_NAME " WdfInterruptCreate failed 0x%08x\n", Status));
383 return Status;
384 }
386 Status = WdfDeviceCreateDeviceInterface(Device, (LPGUID)&GUID_XEN_IFACE, NULL);
387 if (!NT_SUCCESS(Status))
388 {
389 KdPrint((__DRIVER_NAME " WdfDeviceCreateDeviceInterface failed 0x%08x\n", Status));
390 return Status;
391 }
392 WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&IoQConfig, WdfIoQueueDispatchSequential);
393 IoQConfig.EvtIoDefault = XenPCI_IoDefault;
395 Status = WdfIoQueueCreate(Device, &IoQConfig, WDF_NO_OBJECT_ATTRIBUTES, NULL);
396 if (!NT_SUCCESS(Status))
397 {
398 KdPrint((__DRIVER_NAME " WdfIoQueueCreate failed 0x%08x\n", Status));
399 return Status;
400 }
402 WDF_IO_QUEUE_CONFIG_INIT(&IoQConfig, WdfIoQueueDispatchSequential);
403 IoQConfig.EvtIoRead = XenPCI_IoRead;
405 Status = WdfIoQueueCreate(Device, &IoQConfig, WDF_NO_OBJECT_ATTRIBUTES, &ReadQueue);
406 if (!NT_SUCCESS(Status))
407 {
408 KdPrint((__DRIVER_NAME " WdfIoQueueCreate (ReadQueue) failed 0x%08x\n", Status));
409 switch (Status)
410 {
411 case STATUS_INVALID_PARAMETER:
412 KdPrint((__DRIVER_NAME " STATUS_INVALID_PARAMETER\n"));
413 break;
414 case STATUS_INFO_LENGTH_MISMATCH:
415 KdPrint((__DRIVER_NAME " STATUS_INFO_LENGTH_MISMATCH\n"));
416 break;
417 case STATUS_POWER_STATE_INVALID:
418 KdPrint((__DRIVER_NAME " STATUS_POWER_STATE_INVALID\n"));
419 break;
420 case STATUS_INSUFFICIENT_RESOURCES:
421 KdPrint((__DRIVER_NAME " STATUS_INSUFFICIENT_RESOURCES\n"));
422 break;
423 case STATUS_WDF_NO_CALLBACK:
424 KdPrint((__DRIVER_NAME " STATUS_WDF_NO_CALLBACK\n"));
425 break;
426 case STATUS_UNSUCCESSFUL:
427 KdPrint((__DRIVER_NAME " STATUS_UNSUCCESSFUL\n"));
428 break;
429 }
430 return Status;
431 }
432 WdfIoQueueStopSynchronously(ReadQueue);
433 WdfDeviceConfigureRequestDispatching(Device, ReadQueue, WdfRequestTypeRead);
435 Status = WdfDeviceCreateSymbolicLink(Device, &SymbolicName);
436 if (!NT_SUCCESS(Status))
437 {
438 KdPrint((__DRIVER_NAME " WdfDeviceCreateSymbolicLink failed 0x%08x\n", Status));
439 return Status;
440 }
442 KdPrint((__DRIVER_NAME " <-- DeviceAdd\n"));
443 return Status;
444 }
446 static NTSTATUS
447 XenPCI_PrepareHardware(
448 IN WDFDEVICE Device,
449 IN WDFCMRESLIST ResourceList,
450 IN WDFCMRESLIST ResourceListTranslated)
451 {
452 NTSTATUS status = STATUS_SUCCESS;
453 PCM_PARTIAL_RESOURCE_DESCRIPTOR descriptor;
454 ULONG i;
455 PXENPCI_DEVICE_DATA xpdd = GetDeviceData(Device);
457 KdPrint((__DRIVER_NAME " --> EvtDevicePrepareHardware\n"));
459 for (i = 0; i < WdfCmResourceListGetCount(ResourceList); i++)
460 {
461 descriptor = WdfCmResourceListGetDescriptor(ResourceList, i);
462 if(!descriptor)
463 continue;
464 switch (descriptor->Type)
465 {
466 case CmResourceTypeInterrupt:
467 xpdd->irqNumber = descriptor->u.Interrupt.Vector;
468 break;
469 }
470 }
472 //KdPrint((__DRIVER_NAME " GSI = %d\n", irqNumber));
474 //KdPrint((__DRIVER_NAME " ResourceListTranslated\n"));
475 for (i = 0; i < WdfCmResourceListGetCount(ResourceListTranslated); i++)
476 {
477 descriptor = WdfCmResourceListGetDescriptor(ResourceListTranslated, i);
478 if(!descriptor)
479 {
480 KdPrint((__DRIVER_NAME " --> EvtDevicePrepareHardware (No descriptor)\n"));
481 return STATUS_DEVICE_CONFIGURATION_ERROR;
482 }
483 switch (descriptor->Type) {
484 case CmResourceTypePort:
485 //KdPrint((__DRIVER_NAME " I/O mapped CSR: (%x) Length: (%d)\n", descriptor->u.Port.Start.LowPart, descriptor->u.Port.Length));
486 break;
487 case CmResourceTypeMemory:
488 KdPrint((__DRIVER_NAME " Memory mapped CSR:(%x:%x) Length:(%d)\n", descriptor->u.Memory.Start.LowPart, descriptor->u.Memory.Start.HighPart, descriptor->u.Memory.Length));
489 xpdd->platform_mmio_addr = descriptor->u.Memory.Start; //(ULONG)MmMapIoSpace(descriptor->u.Memory.Start, descriptor->u.Memory.Length, MmNonCached);
490 xpdd->platform_mmio_len = descriptor->u.Memory.Length;
491 xpdd->platform_mmio_alloc = 0;
492 break;
493 case CmResourceTypeInterrupt:
494 //KdPrint((__DRIVER_NAME " Interrupt level: 0x%0x, Vector: 0x%0x\n", descriptor->u.Interrupt.Level, descriptor->u.Interrupt.Vector));
495 memcpy(&InterruptRaw, WdfCmResourceListGetDescriptor(ResourceList, i), sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
496 memcpy(&InterruptTranslated, WdfCmResourceListGetDescriptor(ResourceListTranslated, i), sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
497 break;
498 case CmResourceTypeDevicePrivate:
499 //KdPrint((__DRIVER_NAME " Private Data: 0x%02x 0x%02x 0x%02x\n", descriptor->u.DevicePrivate.Data[0], descriptor->u.DevicePrivate.Data[1], descriptor->u.DevicePrivate.Data[2] ));
500 break;
501 default:
502 //KdPrint((__DRIVER_NAME " Unhandled resource type (0x%x)\n", descriptor->Type));
503 break;
504 }
505 }
507 get_hypercall_stubs(Device);
509 init_xen_info(Device);
511 GntTbl_Init(Device);
513 EvtChn_Init(Device);
515 set_callback_irq(Device, xpdd->irqNumber);
517 XenBus_Init(Device);
519 //KdPrint((__DRIVER_NAME " upcall_pending = %d\n", shared_info_area->vcpu_info[0].evtchn_upcall_pending));
521 xpdd->shared_info_area->vcpu_info[0].evtchn_upcall_mask = 0;
523 //xen_reboot_init();
525 KdPrint((__DRIVER_NAME " <-- EvtDevicePrepareHardware\n"));
527 return status;
528 }
530 static NTSTATUS
531 XenPCI_ReleaseHardware(WDFDEVICE Device, WDFCMRESLIST ResourcesTranslated)
532 {
533 UNREFERENCED_PARAMETER(Device);
534 UNREFERENCED_PARAMETER(ResourcesTranslated);
536 return STATUS_SUCCESS;
537 }
539 static NTSTATUS
540 XenPCI_D0Entry(
541 IN WDFDEVICE Device,
542 IN WDF_POWER_DEVICE_STATE PreviousState
543 )
544 {
545 NTSTATUS status = STATUS_SUCCESS;
547 UNREFERENCED_PARAMETER(Device);
548 UNREFERENCED_PARAMETER(PreviousState);
550 KdPrint((__DRIVER_NAME " --> EvtDeviceD0Entry\n"));
552 KdPrint((__DRIVER_NAME " <-- EvtDeviceD0Entry\n"));
554 return status;
555 }
557 static NTSTATUS
558 XenPCI_D0EntryPostInterruptsEnabled(WDFDEVICE Device, WDF_POWER_DEVICE_STATE PreviousState)
559 {
560 NTSTATUS status = STATUS_SUCCESS;
561 //OBJECT_ATTRIBUTES oa;
562 char *response;
563 char *msgTypes;
564 char **Types;
565 int i;
566 char buffer[128];
568 UNREFERENCED_PARAMETER(PreviousState);
570 KdPrint((__DRIVER_NAME " --> EvtDeviceD0EntryPostInterruptsEnabled\n"));
572 XenBus_Start(Device);
574 KdPrint((__DRIVER_NAME " A\n"));
576 response = XenBus_AddWatch(Device, XBT_NIL, SHUTDOWN_PATH, XenBus_ShutdownHandler, Device);
577 KdPrint((__DRIVER_NAME " shutdown watch response = '%s'\n", response));
579 response = XenBus_AddWatch(Device, XBT_NIL, BALLOON_PATH, XenBus_BalloonHandler, Device);
580 KdPrint((__DRIVER_NAME " shutdown watch response = '%s'\n", response));
582 response = XenBus_AddWatch(Device, XBT_NIL, "device", XenPCI_XenBusWatchHandler, Device);
583 KdPrint((__DRIVER_NAME " device watch response = '%s'\n", response));
585 msgTypes = XenBus_List(Device, XBT_NIL, "device", &Types);
586 if (!msgTypes) {
587 for (i = 0; Types[i]; i++)
588 {
589 RtlStringCbPrintfA(buffer, ARRAY_SIZE(buffer), "device/%s", Types[i]);
590 //KdPrint((__DRIVER_NAME " ls device[%d] -> %s\n", i, Types[i]));
591 XenPCI_XenBusWatchHandler(buffer, Device);
592 ExFreePoolWithTag(Types[i], XENPCI_POOL_TAG);
593 }
594 }
595 KdPrint((__DRIVER_NAME " <-- EvtDeviceD0EntryPostInterruptsEnabled\n"));
597 return status;
598 }
600 static NTSTATUS
601 XenPCI_D0ExitPreInterruptsDisabled(WDFDEVICE Device, WDF_POWER_DEVICE_STATE TargetState)
602 {
603 NTSTATUS status = STATUS_SUCCESS;
605 UNREFERENCED_PARAMETER(TargetState);
607 KdPrint((__DRIVER_NAME " --> D0ExitPreInterruptsDisabled\n"));
609 XenBus_Stop(Device);
611 KdPrint((__DRIVER_NAME " <-- D0ExitPreInterruptsDisabled\n"));
613 return status;
614 }
616 static NTSTATUS
617 XenPCI_D0Exit(WDFDEVICE Device, WDF_POWER_DEVICE_STATE TargetState)
618 {
619 NTSTATUS status = STATUS_SUCCESS;
621 UNREFERENCED_PARAMETER(Device);
622 UNREFERENCED_PARAMETER(TargetState);
624 KdPrint((__DRIVER_NAME " --> DeviceD0Exit\n"));
626 XenBus_Close(Device);
628 KdPrint((__DRIVER_NAME " <-- DeviceD0Exit\n"));
630 return status;
631 }
633 static VOID
634 XenPCI_IoDefault(
635 IN WDFQUEUE Queue,
636 IN WDFREQUEST Request
637 )
638 {
639 UNREFERENCED_PARAMETER(Queue);
641 KdPrint((__DRIVER_NAME " --> EvtDeviceIoDefault\n"));
643 WdfRequestComplete(Request, STATUS_NOT_IMPLEMENTED);
645 KdPrint((__DRIVER_NAME " <-- EvtDeviceIoDefault\n"));
646 }
649 static VOID
650 XenPCI_IoRead(WDFQUEUE Queue, WDFREQUEST Request, size_t Length)
651 {
652 PSHUTDOWN_MSG_ENTRY Entry;
653 ULONG Remaining;
654 ULONG CopyLen;
655 PCHAR Buffer;
656 size_t BufLen;
657 KIRQL OldIrql;
659 UNREFERENCED_PARAMETER(Queue);
660 UNREFERENCED_PARAMETER(Length);
662 KdPrint((__DRIVER_NAME " --> IoRead\n"));
664 WdfRequestRetrieveOutputBuffer(Request, 1, &Buffer, &BufLen);
666 KeAcquireSpinLock(&ShutdownMsgLock, &OldIrql);
668 Entry = (PSHUTDOWN_MSG_ENTRY)RemoveHeadList(&ShutdownMsgList);
670 if ((PLIST_ENTRY)Entry == &ShutdownMsgList)
671 {
672 KdPrint((__DRIVER_NAME " <-- IoRead (Nothing in queue... xenpci is now broken)\n"));
673 return;
674 }
676 Remaining = strlen(Entry->Buf + Entry->Ptr);
677 CopyLen = min(Remaining, BufLen);
679 memcpy(Buffer, Entry->Buf + Entry->Ptr, CopyLen);
681 if (Entry->Buf[Entry->Ptr] == 0)
682 {
683 KdPrint((__DRIVER_NAME " All done... stopping queue\n"));
684 if (IsListEmpty(&ShutdownMsgList))
685 WdfIoQueueStop(ReadQueue, NULL, NULL);
686 }
687 else
688 {
689 KdPrint((__DRIVER_NAME " More to do...\n"));
690 Entry->Ptr += CopyLen;
691 InsertHeadList(&ShutdownMsgList, &Entry->ListEntry);
692 }
694 KeReleaseSpinLock(&ShutdownMsgLock, OldIrql);
696 WdfRequestCompleteWithInformation(Request, STATUS_SUCCESS, CopyLen);
698 KdPrint((__DRIVER_NAME " <-- IoRead\n"));
699 }
702 static NTSTATUS
703 XenPCI_InterruptEnable(WDFINTERRUPT Interrupt, WDFDEVICE AssociatedDevice)
704 {
705 PXENPCI_DEVICE_DATA xpdd = GetDeviceData(AssociatedDevice);
707 UNREFERENCED_PARAMETER(Interrupt);
709 KdPrint((__DRIVER_NAME " --> EvtInterruptEnable\n"));
711 xpdd->shared_info_area->vcpu_info[0].evtchn_upcall_mask = 0;
713 KdPrint((__DRIVER_NAME " <-- EvtInterruptEnable\n"));
715 return STATUS_SUCCESS;
716 }
718 static NTSTATUS
719 XenPCI_InterruptDisable(WDFINTERRUPT Interrupt, WDFDEVICE AssociatedDevice)
720 {
721 PXENPCI_DEVICE_DATA xpdd = GetDeviceData(AssociatedDevice);
723 UNREFERENCED_PARAMETER(Interrupt);
725 //KdPrint((__DRIVER_NAME " --> EvtInterruptDisable\n"));
727 xpdd->shared_info_area->vcpu_info[0].evtchn_upcall_mask = 1;
728 // should we kick off any pending interrupts here?
730 //KdPrint((__DRIVER_NAME " <-- EvtInterruptDisable\n"));
732 return STATUS_SUCCESS;
733 }
735 static NTSTATUS
736 XenPCI_ChildListCreateDevice(
737 WDFCHILDLIST ChildList,
738 PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER IdentificationDescription,
739 PWDFDEVICE_INIT ChildInit)
740 {
741 NTSTATUS status;
742 WDFDEVICE ChildDevice = NULL;
743 PXENPCI_IDENTIFICATION_DESCRIPTION XenIdentificationDesc;
744 DECLARE_UNICODE_STRING_SIZE(buffer, 20);
745 WDF_OBJECT_ATTRIBUTES PdoAttributes;
746 DECLARE_CONST_UNICODE_STRING(DeviceLocation, L"Xen Bus");
747 WDF_QUERY_INTERFACE_CONFIG qiConfig;
748 PXENPCI_XEN_DEVICE_DATA ChildDeviceData = NULL;
750 UNREFERENCED_PARAMETER(ChildList);
752 //KdPrint((__DRIVER_NAME " --> ChildListCreateDevice\n"));
754 XenIdentificationDesc = CONTAINING_RECORD(IdentificationDescription, XENPCI_IDENTIFICATION_DESCRIPTION, Header);
756 //KdPrint((__DRIVER_NAME " Type = %wZ\n", &XenIdentificationDesc->DeviceType));
758 //DeviceInit = WdfPdoInitAllocate(Device);
759 WdfDeviceInitSetDeviceType(ChildInit, FILE_DEVICE_CONTROLLER);
761 status = RtlUnicodeStringPrintf(&buffer, L"Xen\\%wZ\0", &XenIdentificationDesc->DeviceType);
762 status = WdfPdoInitAssignDeviceID(ChildInit, &buffer);
763 status = WdfPdoInitAddHardwareID(ChildInit, &buffer);
764 status = WdfPdoInitAddCompatibleID(ChildInit, &buffer);
766 status = RtlUnicodeStringPrintf(&buffer, L"%02d", 0);
767 status = WdfPdoInitAssignInstanceID(ChildInit, &buffer);
769 status = RtlUnicodeStringPrintf( &buffer, L"%wZ", &XenIdentificationDesc->DeviceType);
770 status = WdfPdoInitAddDeviceText(ChildInit, &buffer, &DeviceLocation, 0x409);
772 WdfPdoInitSetDefaultLocale(ChildInit, 0x409);
774 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&PdoAttributes, XENPCI_XEN_DEVICE_DATA);
776 // WDF_PDO_EVENT_CALLBACKS_INIT(&PdoCallbacks);
777 // PdoCallbacks.EvtDeviceResourceRequirementsQuery = XenPCI_DeviceResourceRequirementsQuery;
778 // WdfPdoInitSetEventCallbacks(ChildInit, &PdoCallbacks);
780 status = WdfDeviceCreate(&ChildInit, &PdoAttributes, &ChildDevice);
781 if (!NT_SUCCESS(status))
782 {
783 KdPrint((__DRIVER_NAME " WdfDeviceCreate status = %08X\n", status));
784 }
786 WdfDeviceSetSpecialFileSupport(ChildDevice, WdfSpecialFilePaging, TRUE);
787 WdfDeviceSetSpecialFileSupport(ChildDevice, WdfSpecialFileHibernation, TRUE);
788 WdfDeviceSetSpecialFileSupport(ChildDevice, WdfSpecialFileDump, TRUE);
790 ChildDeviceData = GetXenDeviceData(ChildDevice);
791 ChildDeviceData->Magic = XEN_DATA_MAGIC;
792 ChildDeviceData->AutoEnumerate = AutoEnumerate;
793 ChildDeviceData->WatchHandler = NULL;
794 strncpy(ChildDeviceData->BasePath, XenIdentificationDesc->Path, 128);
795 memcpy(&ChildDeviceData->InterruptRaw, &InterruptRaw, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
796 memcpy(&ChildDeviceData->InterruptTranslated, &InterruptTranslated, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
798 ChildDeviceData->XenInterface.InterfaceHeader.Size = sizeof(ChildDeviceData->XenInterface);
799 ChildDeviceData->XenInterface.InterfaceHeader.Version = 1;
800 ChildDeviceData->XenInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
801 ChildDeviceData->XenInterface.InterfaceHeader.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
802 ChildDeviceData->XenInterface.InterfaceHeader.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
804 ChildDeviceData->XenInterface.AllocMMIO = XenPCI_AllocMMIO;
806 ChildDeviceData->XenInterface.EvtChn_Bind = EvtChn_Bind;
807 ChildDeviceData->XenInterface.EvtChn_Unbind = EvtChn_Unbind;
808 ChildDeviceData->XenInterface.EvtChn_Mask = EvtChn_Mask;
809 ChildDeviceData->XenInterface.EvtChn_Unmask = EvtChn_Unmask;
810 ChildDeviceData->XenInterface.EvtChn_Notify = EvtChn_Notify;
811 ChildDeviceData->XenInterface.EvtChn_AllocUnbound = EvtChn_AllocUnbound;
812 ChildDeviceData->XenInterface.EvtChn_BindDpc = EvtChn_BindDpc;
814 ChildDeviceData->XenInterface.GntTbl_GrantAccess = GntTbl_GrantAccess;
815 ChildDeviceData->XenInterface.GntTbl_EndAccess = GntTbl_EndAccess;
817 ChildDeviceData->XenInterface.XenBus_Read = XenBus_Read;
818 ChildDeviceData->XenInterface.XenBus_Write = XenBus_Write;
819 ChildDeviceData->XenInterface.XenBus_Printf = XenBus_Printf;
820 ChildDeviceData->XenInterface.XenBus_StartTransaction = XenBus_StartTransaction;
821 ChildDeviceData->XenInterface.XenBus_EndTransaction = XenBus_EndTransaction;
822 ChildDeviceData->XenInterface.XenBus_List = XenBus_List;
823 ChildDeviceData->XenInterface.XenBus_AddWatch = XenBus_AddWatch;
824 ChildDeviceData->XenInterface.XenBus_RemWatch = XenBus_RemWatch;
826 WDF_QUERY_INTERFACE_CONFIG_INIT(&qiConfig, (PINTERFACE)&ChildDeviceData->XenInterface, &GUID_XEN_IFACE, NULL);
827 status = WdfDeviceAddQueryInterface(ChildDevice, &qiConfig);
828 if (!NT_SUCCESS(status)) {
829 return status;
830 }
832 //KdPrint((__DRIVER_NAME " <-- ChildListCreateDevice\n"));
834 return status;
835 }
837 VOID
838 XenPCI_XenBusWatchHandler(char *Path, PVOID Data)
839 {
840 XENPCI_IDENTIFICATION_DESCRIPTION description;
841 NTSTATUS status;
842 char **Bits;
843 int Count;
844 WDFDEVICE Device = Data;
845 WDFCHILDLIST ChildList;
846 WDF_CHILD_LIST_ITERATOR ChildIterator;
847 WDFDEVICE ChildDevice;
848 PXENPCI_XEN_DEVICE_DATA ChildDeviceData;
850 ANSI_STRING AnsiBuf;
852 UNREFERENCED_PARAMETER(Data);
854 KdPrint((__DRIVER_NAME " --> XenBusWatchHandler\n"));
856 //KdPrint((__DRIVER_NAME " %s\n", Path));
858 ChildList = WdfFdoGetDefaultChildList(Device);
860 Bits = SplitString(Path, '/', 3, &Count);
861 switch (Count)
862 {
863 case 0:
864 case 1:
865 break;
866 case 2:
867 // add or update the device node
868 WDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER_INIT(&description.Header, sizeof(description));
869 strncpy(description.Path, Path, 128);
870 RtlInitAnsiString(&AnsiBuf, Bits[1]);
871 //KdPrint((__DRIVER_NAME " Name = %s\n", Bits[1]));
872 RtlAnsiStringToUnicodeString(&description.DeviceType, &AnsiBuf, TRUE);
873 status = WdfChildListAddOrUpdateChildDescriptionAsPresent(ChildList, &description.Header, NULL);
874 break;
875 case 3:
876 WDF_CHILD_LIST_ITERATOR_INIT(&ChildIterator, WdfRetrievePresentChildren);
877 WdfChildListBeginIteration(ChildList, &ChildIterator);
878 while (NT_SUCCESS(WdfChildListRetrieveNextDevice(ChildList, &ChildIterator, &ChildDevice, NULL)))
879 {
880 ChildDeviceData = GetXenDeviceData(ChildDevice);
881 if (!ChildDeviceData)
882 {
883 KdPrint((__FUNCTION__ " No child device data, should never happen\n"));
884 continue;
885 }
886 if (strncmp(ChildDeviceData->BasePath, Path, strlen(ChildDeviceData->BasePath)) == 0 && Path[strlen(ChildDeviceData->BasePath)] == '/')
887 {
888 //KdPrint((__DRIVER_NAME " Child Path = %s (Match - WatchHandler = %08x)\n", ChildDeviceData->BasePath, ChildDeviceData->WatchHandler));
889 if (ChildDeviceData->WatchHandler != NULL)
890 ChildDeviceData->WatchHandler(Path, ChildDeviceData->WatchContext);
891 }
892 else
893 {
894 //KdPrint((__DRIVER_NAME " Child Path = %s (No Match)\n", ChildDeviceData->BasePath));
895 }
896 }
897 WdfChildListEndIteration(ChildList, &ChildIterator);
898 break;
899 default:
900 KdPrint((__FUNCTION__ ": Unknown case %d\n", Count));
901 break;
902 }
904 FreeSplitString(Bits, Count);
906 KdPrint((__DRIVER_NAME " <-- XenBusWatchHandler\n"));
907 }
909 static void
910 XenBus_ShutdownHandler(char *Path, PVOID Data)
911 {
912 WDFDEVICE Device = Data;
913 char *Value;
914 xenbus_transaction_t xbt;
915 int retry;
916 PSHUTDOWN_MSG_ENTRY Entry;
918 UNREFERENCED_PARAMETER(Path);
920 KdPrint((__DRIVER_NAME " --> XenBus_ShutdownHandler\n"));
922 XenBus_StartTransaction(Device, &xbt);
924 XenBus_Read(Device, XBT_NIL, SHUTDOWN_PATH, &Value);
926 if (Value != NULL && strlen(Value) != 0)
927 XenBus_Write(Device, XBT_NIL, SHUTDOWN_PATH, "");
929 if (Value != NULL)
930 {
931 KdPrint((__DRIVER_NAME " Shutdown Value = %s\n", Value));
932 KdPrint((__DRIVER_NAME " strlen(...) = %d\n", strlen(Value)));
933 }
934 else
935 {
936 KdPrint((__DRIVER_NAME " Shutdown Value = <null>\n"));
937 }
939 XenBus_EndTransaction(Device, xbt, 0, &retry);
941 if (Value != NULL && strlen(Value) != 0)
942 {
943 Entry = (PSHUTDOWN_MSG_ENTRY)ExAllocatePoolWithTag(NonPagedPool, sizeof(SHUTDOWN_MSG_ENTRY) + strlen(Value) + 1 + 1, XENPCI_POOL_TAG);
944 Entry->Ptr = 0;
945 RtlStringCbPrintfA(Entry->Buf, sizeof(SHUTDOWN_MSG_ENTRY) + strlen(Value) + 1 + 1, "%s\n", Value);
946 InsertTailList(&ShutdownMsgList, &Entry->ListEntry);
947 WdfIoQueueStart(ReadQueue);
948 }
949 KdPrint((__DRIVER_NAME " <-- XenBus_ShutdownHandler\n"));
950 }
952 static VOID
953 XenBus_BalloonHandler(char *Path, PVOID Data)
954 {
955 WDFDEVICE Device = Data;
956 char *value;
957 xenbus_transaction_t xbt;
958 int retry;
960 UNREFERENCED_PARAMETER(Path);
962 KdPrint((__DRIVER_NAME " --> XenBus_BalloonHandler\n"));
964 XenBus_StartTransaction(Device, &xbt);
966 XenBus_Read(Device, XBT_NIL, BALLOON_PATH, &value);
968 KdPrint((__DRIVER_NAME " Balloon Value = %s\n", value));
970 // use the memory_op(unsigned int op, void *arg) hypercall to adjust this
971 // use XENMEM_increase_reservation and XENMEM_decrease_reservation
973 XenBus_EndTransaction(Device, xbt, 0, &retry);
975 KdPrint((__DRIVER_NAME " <-- XenBus_BalloonHandler\n"));
976 }
978 /*
979 IO_RESOURCE_DESCRIPTOR MemoryDescriptor;
981 static NTSTATUS
982 XenPCI_FilterRemoveResourceRequirements(WDFDEVICE Device, WDFIORESREQLIST RequirementsList)
983 {
984 NTSTATUS status;
985 WDFIORESLIST ResourceList;
986 PIO_RESOURCE_DESCRIPTOR Descriptor;
988 int i, j;
989 int offset;
991 //KdPrint((__DRIVER_NAME " --> FilterRemoveResourceRequirements\n"));
993 for (i = 0; i < WdfIoResourceRequirementsListGetCount(RequirementsList); i++)
994 {
995 ResourceList = WdfIoResourceRequirementsListGetIoResList(RequirementsList, i);
996 //KdPrint((__DRIVER_NAME " Resource List %d\n", i));
997 //KdPrint((__DRIVER_NAME " %d resources in list\n", WdfIoResourceListGetCount(ResourceList)));
998 offset = 0;
999 for (j = 0; j < WdfIoResourceListGetCount(ResourceList); j++)
1001 //KdPrint((__DRIVER_NAME " Resource %d\n", j));
1002 Descriptor = WdfIoResourceListGetDescriptor(ResourceList, j - offset);
1004 switch (Descriptor->Type) {
1005 case CmResourceTypePort:
1006 //KdPrint((__DRIVER_NAME " Port\n"));
1007 break;
1008 case CmResourceTypeMemory:
1009 //KdPrint((__DRIVER_NAME " Memory %08X%08X - %08X%08X\n", Descriptor->u.Memory.MinimumAddress.HighPart, Descriptor->u.Memory.MinimumAddress.LowPart, Descriptor->u.Memory.MaximumAddress.HighPart, Descriptor->u.Memory.MaximumAddress.LowPart));
1010 //KdPrint((__DRIVER_NAME " Length %08X\n", Descriptor->u.Memory.Length));
1011 //KdPrint((__DRIVER_NAME " ShareDisposition %02X\n", Descriptor->ShareDisposition));
1012 //KdPrint((__DRIVER_NAME " Option %02X\n", Descriptor->Option));
1013 if (!Descriptor->Option || Descriptor->Option == IO_RESOURCE_PREFERRED) {
1014 memcpy(&MemoryDescriptor, Descriptor, sizeof(IO_RESOURCE_DESCRIPTOR));
1015 //platform_mmio_orig_len = MemoryDescriptor.u.Memory.Length;
1016 //MemoryDescriptor.u.Memory.Length = PAGE_SIZE;
1017 MemoryDescriptor.ShareDisposition = CmResourceShareShared;
1019 WdfIoResourceListRemove(ResourceList, j - offset);
1020 offset++;
1021 break;
1022 case CmResourceTypeInterrupt:
1023 //KdPrint((__DRIVER_NAME " Interrupt\n"));
1024 break;
1025 case CmResourceTypeDevicePrivate:
1026 //KdPrint((__DRIVER_NAME " Private\n"));
1027 break;
1028 default:
1029 //KdPrint((__DRIVER_NAME " Unknown Type (0x%x)\n", Descriptor->Type));
1030 break;
1034 status = STATUS_SUCCESS;
1036 KdPrint((__DRIVER_NAME " <-- FilterRemoveResourceRequirements\n"));
1038 return status;
1042 static NTSTATUS
1043 XenPCI_FilterAddResourceRequirements(WDFDEVICE Device, WDFIORESREQLIST RequirementsList)
1045 NTSTATUS status;
1046 WDFIORESLIST ResourceList;
1047 PIO_RESOURCE_DESCRIPTOR Descriptor;
1049 int i, j;
1051 KdPrint((__DRIVER_NAME " --> FilterAddResourceRequirements\n"));
1054 for (i = 0; i < WdfIoResourceRequirementsListGetCount(RequirementsList); i++)
1056 ResourceList = WdfIoResourceRequirementsListGetIoResList(RequirementsList, i);
1057 //KdPrint((__DRIVER_NAME " Resource List %d\n", i));
1058 //KdPrint((__DRIVER_NAME " %d resources in list\n", WdfIoResourceListGetCount(ResourceList)));
1059 WdfIoResourceListAppendDescriptor(ResourceList, &MemoryDescriptor);
1060 //KdPrint((__DRIVER_NAME " Memory %08X%08X - %08X%08X\n", MemoryDescriptor.u.Memory.MinimumAddress.HighPart, MemoryDescriptor.u.Memory.MinimumAddress.LowPart, MemoryDescriptor.u.Memory.MaximumAddress.HighPart, MemoryDescriptor.u.Memory.MaximumAddress.LowPart));
1061 //KdPrint((__DRIVER_NAME " Length %08X\n", MemoryDescriptor.u.Memory.Length));
1062 for (j = 0; j < WdfIoResourceListGetCount(ResourceList); j++)
1064 //KdPrint((__DRIVER_NAME " Resource %d\n", j));
1065 Descriptor = WdfIoResourceListGetDescriptor(ResourceList, j);
1067 switch (Descriptor->Type) {
1068 case CmResourceTypePort:
1069 //KdPrint((__DRIVER_NAME " Port\n"));
1070 break;
1071 case CmResourceTypeMemory:
1072 //KdPrint((__DRIVER_NAME " Memory %08X%08X - %08X%08X\n", Descriptor->u.Memory.MinimumAddress.HighPart, Descriptor->u.Memory.MinimumAddress.LowPart, Descriptor->u.Memory.MaximumAddress.HighPart, Descriptor->u.Memory.MaximumAddress.LowPart));
1073 //KdPrint((__DRIVER_NAME " Length %08X\n", Descriptor->u.Memory.Length));
1074 //KdPrint((__DRIVER_NAME " ShareDisposition %02X\n", Descriptor->ShareDisposition));
1075 //Descriptor->ShareDisposition = CmResourceShareShared;
1076 //memcpy(&MemoryDescriptor, Descriptor, sizeof(IO_RESOURCE_DESCRIPTOR));
1077 //platform_mmio_orig_len = MemoryDescriptor.u.Memory.Length;
1078 //MemoryDescriptor.u.Memory.Length = PAGE_SIZE;
1079 //WdfIoResourceListRemove(ResourceList, j);
1080 break;
1081 case CmResourceTypeInterrupt:
1082 //KdPrint((__DRIVER_NAME " Interrupt\n"));
1083 break;
1084 case CmResourceTypeDevicePrivate:
1085 //KdPrint((__DRIVER_NAME " Private\n"));
1086 break;
1087 default:
1088 //KdPrint((__DRIVER_NAME " Unknown Type (0x%x)\n", Descriptor->Type));
1089 break;
1093 status = STATUS_SUCCESS;
1095 //KdPrint((__DRIVER_NAME " <-- FilterAddResourceRequirements\n"));
1097 return status;
1100 static NTSTATUS
1101 XenPCI_RemoveAddedResources(WDFDEVICE Device, WDFCMRESLIST ResourcesRaw, WDFCMRESLIST ResourcesTranslated)
1103 //KdPrint((__DRIVER_NAME " --> RemoveAddedResources\n"));
1104 //KdPrint((__DRIVER_NAME " <-- RemoveAddedResources\n"));
1106 return STATUS_SUCCESS;
1109 */
1111 static NTSTATUS
1112 XenPCI_DeviceResourceRequirementsQuery(WDFDEVICE Device, WDFIORESREQLIST IoResourceRequirementsList)
1114 NTSTATUS status;
1115 WDFIORESLIST resourceList;
1116 IO_RESOURCE_DESCRIPTOR descriptor;
1117 PXENPCI_DEVICE_DATA xpdd = GetDeviceData(Device);
1119 //KdPrint((__DRIVER_NAME " --> DeviceResourceRequirementsQuery\n"));
1121 status = WdfIoResourceListCreate(IoResourceRequirementsList, WDF_NO_OBJECT_ATTRIBUTES, &resourceList);
1122 if (!NT_SUCCESS(status))
1123 return status;
1125 /*
1126 RtlZeroMemory(&descriptor, sizeof(descriptor));
1128 descriptor.Option = 0;
1129 descriptor.Type = CmResourceTypeInterrupt;
1130 descriptor.ShareDisposition = CmResourceShareDeviceExclusive;
1131 descriptor.Flags = CM_RESOURCE_MEMORY_READ_WRITE;
1132 descriptor.u.Interrupt.MinimumVector = 1024;
1133 descriptor.u.Interrupt.MaximumVector = 1024+255;
1135 //KdPrint((__DRIVER_NAME " MinimumVector = %d, MaximumVector = %d\n", descriptor.u.Interrupt.MinimumVector, descriptor.u.Interrupt.MaximumVector));
1137 status = WdfIoResourceListAppendDescriptor(resourceList, &descriptor);
1138 if (!NT_SUCCESS(status))
1139 return status;
1140 */
1142 RtlZeroMemory(&descriptor, sizeof(descriptor));
1144 descriptor.Option = 0;
1145 descriptor.Type = CmResourceTypeMemory;
1146 descriptor.ShareDisposition = CmResourceShareShared; //CmResourceShareDeviceExclusive;
1147 descriptor.Flags = CM_RESOURCE_MEMORY_READ_WRITE;
1148 descriptor.u.Memory.Length = PAGE_SIZE;
1149 descriptor.u.Memory.Alignment = PAGE_SIZE;
1150 descriptor.u.Memory.MinimumAddress.QuadPart
1151 = xpdd->platform_mmio_addr.QuadPart + PAGE_SIZE;
1152 descriptor.u.Memory.MaximumAddress.QuadPart
1153 = xpdd->platform_mmio_addr.QuadPart + xpdd->platform_mmio_len - 1;
1155 //KdPrint((__DRIVER_NAME " MinimumAddress = %08x, MaximumAddress = %08X\n", descriptor.u.Memory.MinimumAddress.LowPart, descriptor.u.Memory.MaximumAddress.LowPart));
1157 status = WdfIoResourceListAppendDescriptor(resourceList, &descriptor);
1158 if (!NT_SUCCESS(status))
1159 return status;
1161 status = WdfIoResourceRequirementsListAppendIoResList(IoResourceRequirementsList, resourceList);
1162 if (!NT_SUCCESS(status))
1163 return status;
1165 //KdPrint((__DRIVER_NAME " <-- DeviceResourceRequirementsQuery\n"));
1167 return status;