win-pvdrivers

view xenpci/xenpci_pdo.c @ 761:acc551273d8b

cleanup
author James Harper <james.harper@bendigoit.com.au>
date Sat Jan 16 22:20:25 2010 +1100 (2010-01-16)
parents f43c89df09b2
children 6300617040e0
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 <stdlib.h>
22 #include <io/ring.h>
24 #pragma warning(disable : 4200) // zero-sized array
25 #pragma warning(disable: 4127) // conditional expression is constant
27 /* Not really necessary but keeps PREfast happy */
28 static EVT_WDF_INTERRUPT_SYNCHRONIZE XenPci_EvtChn_Sync_Routine;
29 static EVT_WDF_DEVICE_D0_ENTRY XenPciPdo_EvtDeviceD0Entry;
30 static EVT_WDF_DEVICE_D0_EXIT XenPciPdo_EvtDeviceD0Exit;
31 static EVT_WDF_DEVICE_PREPARE_HARDWARE XenPciPdo_EvtDevicePrepareHardware;
32 static EVT_WDF_DEVICE_RELEASE_HARDWARE XenPciPdo_EvtDeviceReleaseHardware;
33 static EVT_WDF_DEVICE_USAGE_NOTIFICATION XenPciPdo_EvtDeviceUsageNotification;
34 static EVT_WDFDEVICE_WDM_IRP_PREPROCESS XenPciPdo_EvtDeviceWdmIrpPreprocess_START_DEVICE;
35 static EVT_WDF_DEVICE_RESOURCE_REQUIREMENTS_QUERY XenPciPdo_EvtDeviceResourceRequirementsQuery;
37 /*
38 Called at PASSIVE_LEVEL(?)
39 Called during restore
40 */
41 static ULONG
42 XenPci_ReadBackendState(PXENPCI_PDO_DEVICE_DATA xppdd)
43 {
44 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
45 char path[128];
46 char *value;
47 char *err;
48 ULONG backend_state;
50 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
51 err = XenBus_Read(xpdd, XBT_NIL, path, &value);
52 if (err)
53 {
54 XenPci_FreeMem(err);
55 return XenbusStateUnknown;
56 }
57 else
58 {
59 backend_state = atoi(value);
60 XenPci_FreeMem(value);
61 return backend_state;
62 }
63 }
65 static NTSTATUS
66 XenPciPdo_ReconfigureCompletionRoutine(
67 PDEVICE_OBJECT device_object,
68 PIRP irp,
69 PVOID context)
70 {
71 UNREFERENCED_PARAMETER(device_object);
73 if (irp->PendingReturned)
74 {
75 KeSetEvent ((PKEVENT)context, IO_NO_INCREMENT, FALSE);
76 }
77 return STATUS_MORE_PROCESSING_REQUIRED;
78 }
80 static VOID
81 XenPci_UpdateBackendState(PVOID context)
82 {
83 WDFDEVICE device = context;
84 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
85 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
86 ULONG new_backend_state;
88 FUNCTION_ENTER();
90 ExAcquireFastMutex(&xppdd->backend_state_mutex);
92 new_backend_state = XenPci_ReadBackendState(xppdd);
93 if (new_backend_state == XenbusStateUnknown)
94 {
95 if (xpdd->suspend_state != SUSPEND_STATE_NONE)
96 {
97 ExReleaseFastMutex(&xppdd->backend_state_mutex);
98 return;
99 }
100 KdPrint(("Failed to read path, assuming closed\n"));
101 new_backend_state = XenbusStateClosed;
102 }
104 if (xppdd->backend_state == new_backend_state)
105 {
106 KdPrint((__DRIVER_NAME " state unchanged\n"));
107 ExReleaseFastMutex(&xppdd->backend_state_mutex);
108 return;
109 }
111 xppdd->backend_state = new_backend_state;
113 switch (xppdd->backend_state)
114 {
115 case XenbusStateUnknown:
116 KdPrint((__DRIVER_NAME " Backend State Changed to Unknown\n"));
117 break;
119 case XenbusStateInitialising:
120 KdPrint((__DRIVER_NAME " Backend State Changed to Initialising\n"));
121 break;
123 case XenbusStateInitWait:
124 KdPrint((__DRIVER_NAME " Backend State Changed to InitWait\n"));
125 break;
127 case XenbusStateInitialised:
128 KdPrint((__DRIVER_NAME " Backend State Changed to Initialised\n"));
129 break;
131 case XenbusStateConnected:
132 KdPrint((__DRIVER_NAME " Backend State Changed to Connected\n"));
133 break;
135 case XenbusStateClosing:
136 KdPrint((__DRIVER_NAME " Backend State Changed to Closing\n"));
137 if (xppdd->frontend_state != XenbusStateClosing)
138 {
139 KdPrint((__DRIVER_NAME " Requesting eject\n"));
140 WdfPdoRequestEject(device);
141 }
142 break;
144 case XenbusStateClosed:
145 KdPrint((__DRIVER_NAME " Backend State Changed to Closed\n"));
146 break;
148 default:
149 KdPrint((__DRIVER_NAME " Backend State Changed to Undefined = %d\n", xppdd->backend_state));
150 break;
151 }
153 KeSetEvent(&xppdd->backend_state_event, 1, FALSE);
155 ExReleaseFastMutex(&xppdd->backend_state_mutex);
156 FUNCTION_EXIT();
158 return;
159 }
161 static VOID
162 XenPci_BackendStateHandler(char *path, PVOID context)
163 {
164 UNREFERENCED_PARAMETER(path);
166 /* check that path == device/id/state */
167 //RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->path);
169 XenPci_UpdateBackendState(context);
170 }
172 static NTSTATUS
173 XenPci_GetBackendAndAddWatch(WDFDEVICE device)
174 {
175 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
176 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
177 char path[128];
178 PCHAR res;
179 PCHAR value;
181 FUNCTION_ENTER();
182 /* Get backend path */
183 RtlStringCbPrintfA(path, ARRAY_SIZE(path),
184 "%s/backend", xppdd->path);
185 res = XenBus_Read(xpdd, XBT_NIL, path, &value);
186 if (res)
187 {
188 KdPrint((__DRIVER_NAME " Failed to read backend path\n"));
189 XenPci_FreeMem(res);
190 return STATUS_UNSUCCESSFUL;
191 }
192 RtlStringCbCopyA(xppdd->backend_path, ARRAY_SIZE(xppdd->backend_path), value);
193 XenPci_FreeMem(value);
195 /* Add watch on backend state */
196 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
197 XenBus_AddWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
199 FUNCTION_EXIT();
200 return STATUS_SUCCESS;
201 }
203 static NTSTATUS
204 XenConfig_InitConfigPage(WDFDEVICE device)
205 {
206 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
207 //PXENCONFIG_DEVICE_DATA xcdd = (PXENCONFIG_DEVICE_DATA)device_object->DeviceExtension;
208 //PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
209 //PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
210 PUCHAR ptr;
211 PDEVICE_OBJECT curr, prev;
212 PDRIVER_OBJECT fdo_driver_object;
213 PUCHAR fdo_driver_extension;
215 FUNCTION_ENTER();
217 ptr = MmGetMdlVirtualAddress(xppdd->config_page_mdl);
218 curr = IoGetAttachedDeviceReference(WdfDeviceWdmGetDeviceObject(device));
219 //curr = WdfDeviceWdmGetAttachedDevice(device);
220 while (curr != NULL)
221 {
222 fdo_driver_object = curr->DriverObject;
223 KdPrint((__DRIVER_NAME " fdo_driver_object = %p\n", fdo_driver_object));
224 if (fdo_driver_object)
225 {
226 fdo_driver_extension = IoGetDriverObjectExtension(fdo_driver_object, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC));
227 KdPrint((__DRIVER_NAME " fdo_driver_extension = %p\n", fdo_driver_extension));
228 if (fdo_driver_extension)
229 {
230 memcpy(ptr, fdo_driver_extension, PAGE_SIZE);
231 ObDereferenceObject(curr);
232 break;
233 }
234 }
235 prev = curr;
236 curr = IoGetLowerDeviceObject(curr);
237 ObDereferenceObject(prev);
238 }
240 FUNCTION_EXIT();
242 return STATUS_SUCCESS;
243 }
245 static NTSTATUS
246 XenPci_EvtChn_Bind(PVOID context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
247 {
248 WDFDEVICE device = context;
249 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
250 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
252 return EvtChn_Bind(xpdd, Port, ServiceRoutine, ServiceContext);
253 }
255 static NTSTATUS
256 XenPci_EvtChn_BindDpc(PVOID context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
257 {
258 WDFDEVICE device = context;
259 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
260 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
262 return EvtChn_BindDpc(xpdd, Port, ServiceRoutine, ServiceContext);
263 }
265 static NTSTATUS
266 XenPci_EvtChn_Unbind(PVOID context, evtchn_port_t Port)
267 {
268 WDFDEVICE device = context;
269 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
270 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
272 return EvtChn_Unbind(xpdd, Port);
273 }
275 static NTSTATUS
276 XenPci_EvtChn_Mask(PVOID context, evtchn_port_t Port)
277 {
278 WDFDEVICE device = context;
279 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
280 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
282 return EvtChn_Mask(xpdd, Port);
283 }
285 static NTSTATUS
286 XenPci_EvtChn_Unmask(PVOID context, evtchn_port_t Port)
287 {
288 WDFDEVICE device = context;
289 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
290 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
292 return EvtChn_Unmask(xpdd, Port);
293 }
295 static NTSTATUS
296 XenPci_EvtChn_Notify(PVOID context, evtchn_port_t Port)
297 {
298 WDFDEVICE device = context;
299 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
300 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
302 return EvtChn_Notify(xpdd, Port);
303 }
305 static BOOLEAN
306 XenPci_EvtChn_AckEvent(PVOID context, evtchn_port_t port, BOOLEAN *last_interrupt)
307 {
308 WDFDEVICE device = context;
309 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
310 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
312 return EvtChn_AckEvent(xpdd, port, last_interrupt);
313 }
315 typedef struct {
316 PXEN_EVTCHN_SYNC_ROUTINE sync_routine;
317 PVOID sync_context;
318 } sync_context_t;
320 static BOOLEAN
321 XenPci_EvtChn_Sync_Routine(WDFINTERRUPT interrupt, WDFCONTEXT context)
322 {
323 sync_context_t *wdf_sync_context = context;
324 UNREFERENCED_PARAMETER(interrupt);
325 return wdf_sync_context->sync_routine(wdf_sync_context->sync_context);
326 }
328 static BOOLEAN
329 XenPci_EvtChn_Sync(PVOID context, PXEN_EVTCHN_SYNC_ROUTINE sync_routine, PVOID sync_context)
330 {
331 WDFDEVICE device = context;
332 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
333 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
334 sync_context_t wdf_sync_context;
336 wdf_sync_context.sync_routine = sync_routine;
337 wdf_sync_context.sync_context = sync_context;
339 return WdfInterruptSynchronize(xpdd->interrupt, XenPci_EvtChn_Sync_Routine, &wdf_sync_context);
340 }
342 static grant_ref_t
343 XenPci_GntTbl_GrantAccess(PVOID context, domid_t domid, uint32_t frame, int readonly, grant_ref_t ref)
344 {
345 WDFDEVICE device = context;
346 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
347 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
349 return GntTbl_GrantAccess(xpdd, domid, frame, readonly, ref);
350 }
352 static BOOLEAN
353 XenPci_GntTbl_EndAccess(PVOID context, grant_ref_t ref, BOOLEAN keepref)
354 {
355 WDFDEVICE device = context;
356 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
357 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
359 return GntTbl_EndAccess(xpdd, ref, keepref);
360 }
362 static VOID
363 XenPci_GntTbl_PutRef(PVOID context, grant_ref_t ref)
364 {
365 WDFDEVICE device = context;
366 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
367 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
369 GntTbl_PutRef(xpdd, ref);
370 }
372 static grant_ref_t
373 XenPci_GntTbl_GetRef(PVOID context)
374 {
375 WDFDEVICE device = context;
376 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
377 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
379 return GntTbl_GetRef(xpdd);
380 }
382 PCHAR
383 XenPci_XenBus_Read(PVOID context, xenbus_transaction_t xbt, char *path, char **value)
384 {
385 WDFDEVICE device = context;
386 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
387 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
388 return XenBus_Read(xpdd, xbt, path, value);
389 }
391 PCHAR
392 XenPci_XenBus_Write(PVOID context, xenbus_transaction_t xbt, char *path, char *value)
393 {
394 WDFDEVICE device = context;
395 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
396 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
397 return XenBus_Write(xpdd, xbt, path, value);
398 }
400 PCHAR
401 XenPci_XenBus_Printf(PVOID context, xenbus_transaction_t xbt, char *path, char *fmt, ...)
402 {
403 //PXENPCI_PDO_DEVICE_DATA xppdd = Context;
404 //PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
405 //return XenBus_Printf(xpdd, xbt, path, value);
406 UNREFERENCED_PARAMETER(context);
407 UNREFERENCED_PARAMETER(xbt);
408 UNREFERENCED_PARAMETER(path);
409 UNREFERENCED_PARAMETER(fmt);
410 return NULL;
411 }
413 PCHAR
414 XenPci_XenBus_StartTransaction(PVOID context, xenbus_transaction_t *xbt)
415 {
416 WDFDEVICE device = context;
417 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
418 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
419 return XenBus_StartTransaction(xpdd, xbt);
420 }
422 PCHAR
423 XenPci_XenBus_EndTransaction(PVOID context, xenbus_transaction_t xbt, int abort, int *retry)
424 {
425 WDFDEVICE device = context;
426 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
427 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
428 return XenBus_EndTransaction(xpdd, xbt, abort, retry);
429 }
431 PCHAR
432 XenPci_XenBus_List(PVOID context, xenbus_transaction_t xbt, char *prefix, char ***contents)
433 {
434 WDFDEVICE device = context;
435 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
436 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
437 return XenBus_List(xpdd, xbt, prefix, contents);
438 }
440 PCHAR
441 XenPci_XenBus_AddWatch(PVOID context, xenbus_transaction_t xbt, char *path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
442 {
443 WDFDEVICE device = context;
444 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
445 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
446 PCHAR retval;
448 FUNCTION_ENTER();
449 retval = XenBus_AddWatch(xpdd, xbt, path, ServiceRoutine, ServiceContext);
450 if (retval == NULL)
451 {
452 KdPrint((__DRIVER_NAME " XenPci_XenBus_AddWatch - %s = NULL\n", path));
453 }
454 else
455 {
456 KdPrint((__DRIVER_NAME " XenPci_XenBus_AddWatch - %s = %s\n", path, retval));
457 }
458 FUNCTION_EXIT();
459 return retval;
460 }
462 PCHAR
463 XenPci_XenBus_RemWatch(PVOID context, xenbus_transaction_t xbt, char *path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
464 {
465 WDFDEVICE device = context;
466 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
467 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
468 return XenBus_RemWatch(xpdd, xbt, path, ServiceRoutine, ServiceContext);
469 }
471 /*
472 Called at PASSIVE_LEVEL
473 Called during restore
474 */
476 static NTSTATUS
477 XenPci_ChangeFrontendState(WDFDEVICE device, ULONG frontend_state_set, ULONG backend_state_response, ULONG maximum_wait_ms)
478 {
479 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
480 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
481 LARGE_INTEGER timeout;
482 ULONG remaining;
483 ULONG thiswait;
484 char path[128];
486 FUNCTION_ENTER();
488 xppdd->frontend_state = frontend_state_set;
490 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->path);
491 XenBus_Printf(xpdd, XBT_NIL, path, "%d", frontend_state_set);
493 remaining = maximum_wait_ms;
495 while (xppdd->backend_state != backend_state_response)
496 {
497 thiswait = min((LONG)remaining, 1000); // 1 second or remaining time, whichever is less
498 timeout.QuadPart = (LONGLONG)-1 * thiswait * 1000 * 10;
499 if (KeWaitForSingleObject(&xppdd->backend_state_event, Executive, KernelMode, FALSE, &timeout) == STATUS_TIMEOUT)
500 {
501 /* it's possible that the workitems are blocked because the pagefile isn't available. Lets just re-read the backend value for now */
502 XenPci_UpdateBackendState(device);
503 remaining -= thiswait;
504 if (remaining == 0)
505 {
506 KdPrint((__DRIVER_NAME " Timed out waiting for %d!\n", backend_state_response));
507 return STATUS_UNSUCCESSFUL;
508 }
509 KdPrint((__DRIVER_NAME " Still waiting for %d (currently %d)...\n", backend_state_response, xppdd->backend_state));
510 }
511 }
512 FUNCTION_EXIT();
513 return STATUS_SUCCESS;
514 }
516 static NTSTATUS
517 XenPci_XenConfigDevice(WDFDEVICE device);
519 static NTSTATUS
520 XenPci_XenShutdownDevice(PVOID context)
521 {
522 WDFDEVICE device = context;
523 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
524 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
525 PUCHAR in_ptr;
526 ULONG i;
527 UCHAR type;
528 PVOID setting;
529 PVOID value;
530 PVOID value2;
532 FUNCTION_ENTER();
534 if (xppdd->backend_state == XenbusStateConnected)
535 {
536 XenPci_ChangeFrontendState(device, XenbusStateClosing, XenbusStateClosing, 30000);
537 if (xppdd->backend_state == XenbusStateClosing)
538 XenPci_ChangeFrontendState(device, XenbusStateClosed, XenbusStateClosed, 30000);
539 if (xppdd->backend_state == XenbusStateClosed)
540 XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 30000);
541 }
542 else
543 {
544 if (xppdd->backend_state == XenbusStateClosing)
545 XenPci_ChangeFrontendState(device, XenbusStateClosed, XenbusStateClosed, 30000);
546 }
548 if (xppdd->assigned_resources_start != NULL)
549 {
550 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
551 in_ptr = xppdd->assigned_resources_start;
552 while((type = GET_XEN_INIT_RSP(&in_ptr, &setting, &value, &value2)) != XEN_INIT_TYPE_END)
553 {
554 switch (type)
555 {
556 case XEN_INIT_TYPE_RING: /* frontend ring */
557 FreePages(value);
558 break;
559 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
560 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
561 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
562 EvtChn_Unbind(xpdd, PtrToUlong(value));
563 EvtChn_Close(xpdd, PtrToUlong(value));
564 break;
565 case XEN_INIT_TYPE_GRANT_ENTRIES:
566 for (i = 0; i < PtrToUlong(setting); i++)
567 GntTbl_EndAccess(xpdd, ((grant_ref_t *)value)[i], FALSE);
568 break;
569 }
570 }
571 ExFreePoolWithTag(xppdd->assigned_resources_start, XENPCI_POOL_TAG);
572 xppdd->assigned_resources_start = NULL;
573 }
575 FUNCTION_EXIT();
577 return STATUS_SUCCESS;
578 }
580 struct dummy_sring {
581 RING_IDX req_prod, req_event;
582 RING_IDX rsp_prod, rsp_event;
583 uint8_t pad[48];
584 };
586 static NTSTATUS
587 XenPci_XenConfigDeviceSpecifyBuffers(WDFDEVICE device, PUCHAR src, PUCHAR dst)
588 {
589 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
590 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
591 NTSTATUS status = STATUS_SUCCESS;
592 ULONG i;
593 char path[128];
594 PCHAR setting, value;
595 PCHAR res;
596 PVOID address;
597 UCHAR type;
598 PUCHAR in_ptr;
599 PUCHAR out_ptr;
600 XENPCI_VECTORS vectors;
601 ULONG event_channel;
602 ULONG run_type = 0;
603 PMDL ring;
604 grant_ref_t gref;
605 BOOLEAN done_xenbus_init = FALSE;
606 PVOID value2;
608 FUNCTION_ENTER();
610 in_ptr = src;
611 out_ptr = dst;
613 // always add vectors
614 vectors.magic = XEN_DATA_MAGIC;
615 vectors.length = sizeof(XENPCI_VECTORS);
616 vectors.context = device;
617 vectors.EvtChn_Bind = XenPci_EvtChn_Bind;
618 vectors.EvtChn_BindDpc = XenPci_EvtChn_BindDpc;
619 vectors.EvtChn_Unbind = XenPci_EvtChn_Unbind;
620 vectors.EvtChn_Mask = XenPci_EvtChn_Mask;
621 vectors.EvtChn_Unmask = XenPci_EvtChn_Unmask;
622 vectors.EvtChn_Notify = XenPci_EvtChn_Notify;
623 vectors.EvtChn_AckEvent = XenPci_EvtChn_AckEvent;
624 vectors.EvtChn_Sync = XenPci_EvtChn_Sync;
625 vectors.GntTbl_GetRef = XenPci_GntTbl_GetRef;
626 vectors.GntTbl_PutRef = XenPci_GntTbl_PutRef;
627 vectors.GntTbl_GrantAccess = XenPci_GntTbl_GrantAccess;
628 vectors.GntTbl_EndAccess = XenPci_GntTbl_EndAccess;
629 vectors.XenPci_XenConfigDevice = XenPci_XenConfigDevice;
630 vectors.XenPci_XenShutdownDevice = XenPci_XenShutdownDevice;
631 RtlStringCbCopyA(vectors.path, 128, xppdd->path);
632 RtlStringCbCopyA(vectors.backend_path, 128, xppdd->backend_path);
633 //vectors.pdo_event_channel = xpdd->pdo_event_channel;
634 vectors.XenBus_Read = XenPci_XenBus_Read;
635 vectors.XenBus_Write = XenPci_XenBus_Write;
636 vectors.XenBus_Printf = XenPci_XenBus_Printf;
637 vectors.XenBus_StartTransaction = XenPci_XenBus_StartTransaction;
638 vectors.XenBus_EndTransaction = XenPci_XenBus_EndTransaction;
639 vectors.XenBus_List = XenPci_XenBus_List;
640 vectors.XenBus_AddWatch = XenPci_XenBus_AddWatch;
641 vectors.XenBus_RemWatch = XenPci_XenBus_RemWatch;
643 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_PROTOCOL_VERSION, NULL, UlongToPtr(qemu_protocol_version), NULL);
645 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_VECTORS, NULL, &vectors, NULL);
646 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_STATE_PTR, NULL, &xppdd->device_state, NULL);
648 // first pass, possibly before state == Connected
649 in_ptr = src;
650 while((type = GET_XEN_INIT_REQ(&in_ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
651 {
652 if (!done_xenbus_init)
653 {
654 if (XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 2000) != STATUS_SUCCESS)
655 {
656 status = STATUS_UNSUCCESSFUL;
657 goto error;
658 }
659 done_xenbus_init = TRUE;
660 }
662 ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, type, setting, value, value2);
664 switch (type)
665 {
666 case XEN_INIT_TYPE_RUN:
667 run_type++;
668 break;
669 case XEN_INIT_TYPE_WRITE_STRING: /* frontend setting = value */
670 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_WRITE_STRING - %s = %s\n", setting, value));
671 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
672 XenBus_Printf(xpdd, XBT_NIL, path, "%s", value);
673 break;
674 case XEN_INIT_TYPE_RING: /* frontend ring */
675 /* we only allocate and do the SHARED_RING_INIT here */
676 if ((ring = AllocatePage()) != 0)
677 {
678 address = MmGetMdlVirtualAddress(ring);
679 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %p\n", setting, address));
680 SHARED_RING_INIT((struct dummy_sring *)address);
681 if ((gref = GntTbl_GrantAccess(
682 xpdd, 0, (ULONG)*MmGetMdlPfnArray(ring), FALSE, INVALID_GRANT_REF)) != INVALID_GRANT_REF)
683 {
684 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
685 XenBus_Printf(xpdd, XBT_NIL, path, "%d", gref);
686 ADD_XEN_INIT_RSP(&out_ptr, type, setting, address, NULL);
687 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, type, setting, ring, NULL);
688 // add the grant entry too so it gets freed automatically
689 __ADD_XEN_INIT_UCHAR(&xppdd->assigned_resources_ptr, XEN_INIT_TYPE_GRANT_ENTRIES);
690 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, 1);
691 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, gref);
692 }
693 else
694 {
695 FreePages(ring);
696 status = STATUS_UNSUCCESSFUL;
697 goto error;
698 }
699 }
700 else
701 {
702 status = STATUS_UNSUCCESSFUL;
703 goto error;
704 }
705 break;
706 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
707 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
708 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
709 if ((event_channel = EvtChn_AllocUnbound(xpdd, 0)) != 0)
710 {
711 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_EVENT_CHANNEL - %s = %d\n", setting, event_channel));
712 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
713 XenBus_Printf(xpdd, XBT_NIL, path, "%d", event_channel);
714 ADD_XEN_INIT_RSP(&out_ptr, type, setting, UlongToPtr(event_channel), NULL);
715 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, type, setting, UlongToPtr(event_channel), NULL);
716 if (type == XEN_INIT_TYPE_EVENT_CHANNEL_IRQ)
717 {
718 EvtChn_BindIrq(xpdd, event_channel, xppdd->irq_vector, path);
719 }
720 else if (type == XEN_INIT_TYPE_EVENT_CHANNEL_DPC)
721 {
722 #pragma warning(suppress:4055)
723 EvtChn_BindDpc(xpdd, event_channel, (PXEN_EVTCHN_SERVICE_ROUTINE)value, value2);
724 }
725 else
726 {
727 #pragma warning(suppress:4055)
728 EvtChn_Bind(xpdd, event_channel, (PXEN_EVTCHN_SERVICE_ROUTINE)value, value2);
729 }
730 }
731 else
732 {
733 status = STATUS_UNSUCCESSFUL;
734 goto error;
735 }
736 break;
737 }
738 }
739 if (!NT_SUCCESS(status))
740 {
741 goto error;
742 }
743 // If XEN_INIT_TYPE_RUN was specified more than once then we skip XenbusStateInitialised here and go straight to XenbusStateConnected at the end
744 if (run_type == 1)
745 {
746 if (XenPci_ChangeFrontendState(device, XenbusStateInitialised, XenbusStateConnected, 2000) != STATUS_SUCCESS)
747 {
748 status = STATUS_UNSUCCESSFUL;
749 goto error;
750 }
751 }
753 // second pass, possibly after state == Connected
754 in_ptr = src;
755 while((type = GET_XEN_INIT_REQ(&in_ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
756 {
757 switch(type)
758 {
759 case XEN_INIT_TYPE_READ_STRING_BACK:
760 case XEN_INIT_TYPE_READ_STRING_FRONT:
761 if (type == XEN_INIT_TYPE_READ_STRING_FRONT)
762 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
763 else
764 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->backend_path, setting);
765 res = XenBus_Read(xpdd, XBT_NIL, path, &value);
766 if (res)
767 {
768 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = <failed>\n", setting));
769 XenPci_FreeMem(res);
770 ADD_XEN_INIT_RSP(&out_ptr, type, setting, "", "");
771 }
772 else
773 {
774 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = %s\n", setting, value));
775 ADD_XEN_INIT_RSP(&out_ptr, type, setting, value, value2);
776 XenPci_FreeMem(value);
777 }
778 break;
779 case XEN_INIT_TYPE_VECTORS:
780 // this is always done so ignore the request
781 break;
782 case XEN_INIT_TYPE_GRANT_ENTRIES:
783 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_GRANT_ENTRIES - %d\n", PtrToUlong(value)));
784 __ADD_XEN_INIT_UCHAR(&out_ptr, type);
785 __ADD_XEN_INIT_UCHAR(&xppdd->assigned_resources_ptr, type);
786 __ADD_XEN_INIT_ULONG(&out_ptr, PtrToUlong(value));
787 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, PtrToUlong(value));
788 for (i = 0; i < PtrToUlong(value); i++)
789 {
790 gref = GntTbl_GetRef(xpdd);
791 __ADD_XEN_INIT_ULONG(&out_ptr, gref);
792 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, gref);
793 }
794 break;
795 }
796 }
797 if (qemu_hide_flags_value)
798 {
799 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_HIDE_FLAGS, NULL, UlongToPtr(qemu_hide_flags_value), NULL);
800 }
801 else
802 {
803 DECLARE_UNICODE_STRING_SIZE(my_device_name, 128);
805 RtlUnicodeStringPrintf(&my_device_name, L"#%S#", xppdd->device);
807 for (i = 0; i < WdfCollectionGetCount(qemu_hide_devices); i++)
808 {
809 WDFSTRING wdf_string = WdfCollectionGetItem(qemu_hide_devices, i);
810 UNICODE_STRING hide_device_name;
812 WdfStringGetUnicodeString(wdf_string, &hide_device_name);
814 if (RtlCompareUnicodeString(&hide_device_name, &my_device_name, TRUE) != 0)
815 {
816 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_HIDE_FILTER, NULL, NULL, NULL);
817 break;
818 }
819 }
820 }
822 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
824 if (run_type)
825 {
826 if (XenPci_ChangeFrontendState(device, XenbusStateConnected, XenbusStateConnected, 2000) != STATUS_SUCCESS)
827 {
828 status = STATUS_UNSUCCESSFUL;
829 goto error;
830 }
831 }
832 FUNCTION_EXIT();
833 return status;
835 error:
836 XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 2000);
837 FUNCTION_EXIT_STATUS(status);
838 return status;
839 }
841 static NTSTATUS
842 XenPci_XenConfigDevice(WDFDEVICE device)
843 {
844 NTSTATUS status;
845 PUCHAR src, dst;
846 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
848 src = ExAllocatePoolWithTag(NonPagedPool, xppdd->config_page_length, XENPCI_POOL_TAG);
849 dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
850 memcpy(src, dst, xppdd->config_page_length);
852 status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
854 MmUnmapIoSpace(dst, xppdd->config_page_length);
855 ExFreePoolWithTag(src, XENPCI_POOL_TAG);
857 return status;
858 }
860 static NTSTATUS
861 XenPciPdo_EvtDeviceWdmIrpPreprocess_START_DEVICE(WDFDEVICE device, PIRP irp)
862 {
863 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
864 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
865 PIO_STACK_LOCATION stack;
866 PCM_PARTIAL_RESOURCE_LIST prl;
867 PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
868 ULONG i;
869 //char path[128];
870 //PMDL mdl;
872 FUNCTION_ENTER();
873 KdPrint((__DRIVER_NAME " %s\n", xppdd->path));
875 stack = IoGetCurrentIrpStackLocation(irp);
877 prl = &stack->Parameters.StartDevice.AllocatedResources->List[0].PartialResourceList;
878 for (i = 0; i < prl->Count; i++)
879 {
880 prd = & prl->PartialDescriptors[i];
881 switch (prd->Type)
882 {
883 case CmResourceTypeMemory:
884 if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart && prd->u.Memory.Length == 0)
885 {
886 prd->u.Memory.Start.QuadPart = MmGetMdlPfnArray(xppdd->config_page_mdl)[0] << PAGE_SHIFT;
887 prd->u.Memory.Length = MmGetMdlByteCount(xppdd->config_page_mdl);
888 }
889 else if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart + 1 && prd->u.Memory.Length == 0)
890 {
891 RtlZeroMemory(prd, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
892 prd->Type = CmResourceTypeInterrupt;
893 prd->ShareDisposition = CmResourceShareShared;
894 prd->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
895 prd->u.Interrupt.Level = 0; // Set group and level to zero (group = upper word)
896 prd->u.Interrupt.Level = xpdd->irq_number & 0xffff; // Only set the lower word
897 prd->u.Interrupt.Vector = xpdd->irq_number;
898 prd->u.Interrupt.Affinity = xpdd->irq_affinity; //KeQueryActiveProcessors();
899 xppdd->irq_number = xpdd->irq_number;
900 }
901 break;
902 }
903 }
905 prl = &stack->Parameters.StartDevice.AllocatedResourcesTranslated->List[0].PartialResourceList;
906 for (i = 0; i < prl->Count; i++)
907 {
908 prd = & prl->PartialDescriptors[i];
909 switch (prd->Type)
910 {
911 case CmResourceTypeMemory:
912 KdPrint((__DRIVER_NAME " CmResourceTypeMemory (%d)\n", i));
913 KdPrint((__DRIVER_NAME " Start = %08x, Length = %d\n", prd->u.Memory.Start.LowPart, prd->u.Memory.Length));
914 if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart)
915 {
916 if (prd->u.Memory.Length == 0)
917 {
918 KdPrint((__DRIVER_NAME " pfn[0] = %08x\n", (ULONG)MmGetMdlPfnArray(xppdd->config_page_mdl)[0]));
919 prd->u.Memory.Start.QuadPart = (ULONGLONG)MmGetMdlPfnArray(xppdd->config_page_mdl)[0] << PAGE_SHIFT;
920 prd->u.Memory.Length = MmGetMdlByteCount(xppdd->config_page_mdl);
921 KdPrint((__DRIVER_NAME " New Start = %08x%08x, Length = %d\n", prd->u.Memory.Start.HighPart, prd->u.Memory.Start.LowPart, prd->u.Memory.Length));
922 }
923 xppdd->config_page_phys = prd->u.Memory.Start;
924 xppdd->config_page_length = prd->u.Memory.Length;
925 xppdd->requested_resources_start = xppdd->requested_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
926 //xppdd->assigned_resources_start = xppdd->assigned_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
928 #if 0
929 status = XenPci_XenConfigDevice(device);
930 if (!NT_SUCCESS(status))
931 {
932 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
933 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
934 FUNCTION_EXIT_STATUS(status);
935 return status;
936 }
937 #endif
938 }
939 else if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart + 1 && prd->u.Memory.Length == 0)
940 {
941 RtlZeroMemory(prd, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
942 prd->Type = CmResourceTypeInterrupt;
943 prd->ShareDisposition = CmResourceShareShared;
944 prd->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
945 prd->u.Interrupt.Level = 0; // Set group and level to zero (group = upper word)
946 prd->u.Interrupt.Level = xpdd->irq_level & 0xffff; // Only set the lower word
947 prd->u.Interrupt.Vector = xpdd->irq_vector;
948 prd->u.Interrupt.Affinity = xpdd->irq_affinity; //KeQueryActiveProcessors();
949 xppdd->irq_vector = xpdd->irq_vector;
950 xppdd->irq_level = xpdd->irq_level;
951 }
952 break;
953 }
954 }
956 IoSkipCurrentIrpStackLocation(irp);
958 FUNCTION_EXIT();
960 return WdfDeviceWdmDispatchPreprocessedIrp(device, irp);
961 }
963 #if 0
964 static NTSTATUS
965 XenPciPdo_EvtDeviceResourcesQuery(WDFDEVICE device, WDFCMRESLIST resources)
966 {
967 }
968 #endif
970 static NTSTATUS
971 XenPciPdo_EvtDeviceResourceRequirementsQuery(WDFDEVICE device, WDFIORESREQLIST requirements_list)
972 {
973 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
974 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
975 WDFIORESLIST res_list;
976 IO_RESOURCE_DESCRIPTOR ird;
978 //FUNCTION_ENTER();
980 WdfIoResourceRequirementsListSetInterfaceType(requirements_list, PNPBus);
982 WdfIoResourceListCreate(requirements_list, WDF_NO_OBJECT_ATTRIBUTES, &res_list);
983 ird.Option = 0;
984 ird.Type = CmResourceTypeMemory;
985 ird.ShareDisposition = CmResourceShareShared;
986 ird.Flags = CM_RESOURCE_MEMORY_READ_WRITE | CM_RESOURCE_MEMORY_CACHEABLE;
987 ird.u.Memory.MinimumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart;
988 ird.u.Memory.MaximumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart;
989 ird.u.Memory.Length = 0;
990 ird.u.Memory.Alignment = 1; //PAGE_SIZE;
991 WdfIoResourceListAppendDescriptor(res_list, &ird);
993 ird.Option = 0;
994 ird.Type = CmResourceTypeMemory;
995 ird.ShareDisposition = CmResourceShareShared;
996 ird.Flags = CM_RESOURCE_MEMORY_READ_WRITE | CM_RESOURCE_MEMORY_CACHEABLE;
997 ird.u.Memory.MinimumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart + 1;
998 ird.u.Memory.MaximumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart + 1;
999 ird.u.Memory.Length = 0;
1000 ird.u.Memory.Alignment = 1; //PAGE_SIZE;
1001 WdfIoResourceListAppendDescriptor(res_list, &ird);
1003 WdfIoResourceRequirementsListAppendIoResList(requirements_list, res_list);
1005 //FUNCTION_EXIT();
1007 return STATUS_SUCCESS;
1010 NTSTATUS
1011 XenPciPdo_EvtDeviceD0Entry(WDFDEVICE device, WDF_POWER_DEVICE_STATE previous_state)
1013 NTSTATUS status = STATUS_SUCCESS;
1014 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1015 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1016 CHAR path[128];
1018 FUNCTION_ENTER();
1020 switch (previous_state)
1022 case WdfPowerDeviceD0:
1023 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1024 break;
1025 case WdfPowerDeviceD1:
1026 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1027 break;
1028 case WdfPowerDeviceD2:
1029 KdPrint((__DRIVER_NAME " WdfPowerDeviceD2\n"));
1030 break;
1031 case WdfPowerDeviceD3:
1032 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3\n"));
1033 break;
1034 case WdfPowerDeviceD3Final:
1035 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3Final\n"));
1036 break;
1037 case WdfPowerDevicePrepareForHibernation:
1038 KdPrint((__DRIVER_NAME " WdfPowerDevicePrepareForHibernation\n"));
1039 break;
1040 default:
1041 KdPrint((__DRIVER_NAME " Unknown WdfPowerDevice state %d\n", previous_state));
1042 break;
1045 if (previous_state == WdfPowerDevicePrepareForHibernation
1046 || (previous_state == WdfPowerDeviceD3 && xppdd->hiber_usage_kludge))
1048 KdPrint((__DRIVER_NAME " starting up from hibernation\n"));
1050 else
1054 if (previous_state == WdfPowerDevicePrepareForHibernation || previous_state == WdfPowerDeviceD3 || previous_state == WdfPowerDeviceD3Final)
1056 xppdd->requested_resources_ptr = xppdd->requested_resources_start;
1057 xppdd->assigned_resources_start = xppdd->assigned_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
1060 XenConfig_InitConfigPage(device);
1062 status = XenPci_GetBackendAndAddWatch(device);
1063 if (!NT_SUCCESS(status))
1065 WdfDeviceSetFailed(device, WdfDeviceFailedNoRestart);
1066 FUNCTION_EXIT_STATUS(status);
1067 return status;
1069 status = XenPci_XenConfigDevice(device);
1070 if (!NT_SUCCESS(status))
1072 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
1073 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
1074 WdfDeviceSetFailed(device, WdfDeviceFailedNoRestart);
1075 FUNCTION_EXIT_STATUS(status);
1076 return status;
1079 FUNCTION_EXIT();
1081 return status;
1084 NTSTATUS
1085 XenPciPdo_EvtDeviceD0Exit(WDFDEVICE device, WDF_POWER_DEVICE_STATE target_state)
1087 NTSTATUS status = STATUS_SUCCESS;
1088 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1089 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1090 char path[128];
1092 UNREFERENCED_PARAMETER(device);
1093 UNREFERENCED_PARAMETER(target_state);
1095 FUNCTION_ENTER();
1097 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
1100 switch (target_state)
1102 case WdfPowerDeviceD0:
1103 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1104 break;
1105 case WdfPowerDeviceD1:
1106 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1107 break;
1108 case WdfPowerDeviceD2:
1109 KdPrint((__DRIVER_NAME " WdfPowerDeviceD2\n"));
1110 break;
1111 case WdfPowerDeviceD3:
1112 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3\n"));
1113 break;
1114 case WdfPowerDeviceD3Final:
1115 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3Final\n"));
1116 break;
1117 case WdfPowerDevicePrepareForHibernation:
1118 KdPrint((__DRIVER_NAME " WdfPowerDevicePrepareForHibernation\n"));
1119 break;
1120 default:
1121 KdPrint((__DRIVER_NAME " Unknown WdfPowerDevice state %d\n", target_state));
1122 break;
1125 if (target_state == WdfPowerDevicePrepareForHibernation
1126 || (target_state == WdfPowerDeviceD3 && xppdd->hiber_usage_kludge))
1128 KdPrint((__DRIVER_NAME " not powering down as we are hibernating\n"));
1130 else
1132 status = XenPci_XenShutdownDevice(device);
1135 /* Remove watch on backend state */
1136 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
1137 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
1139 FUNCTION_EXIT();
1141 return status;
1144 NTSTATUS
1145 XenPciPdo_EvtDevicePrepareHardware(WDFDEVICE device, WDFCMRESLIST resources_raw, WDFCMRESLIST resources_translated)
1147 NTSTATUS status = STATUS_SUCCESS;
1149 UNREFERENCED_PARAMETER(device);
1150 UNREFERENCED_PARAMETER(resources_raw);
1151 UNREFERENCED_PARAMETER(resources_translated);
1153 FUNCTION_ENTER();
1154 FUNCTION_EXIT();
1156 return status;
1159 NTSTATUS
1160 XenPciPdo_EvtDeviceReleaseHardware(WDFDEVICE device, WDFCMRESLIST resources_translated)
1162 NTSTATUS status = STATUS_SUCCESS;
1164 UNREFERENCED_PARAMETER(device);
1165 UNREFERENCED_PARAMETER(resources_translated);
1167 FUNCTION_ENTER();
1168 FUNCTION_EXIT();
1170 return status;
1173 static VOID
1174 XenPciPdo_EvtDeviceUsageNotification(WDFDEVICE device, WDF_SPECIAL_FILE_TYPE notification_type, BOOLEAN is_in_notification_path)
1176 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1178 FUNCTION_ENTER();
1180 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
1181 switch (notification_type)
1183 case WdfSpecialFilePaging:
1184 KdPrint((__DRIVER_NAME " notification_type = Paging, flag = %d\n", is_in_notification_path));
1185 break;
1186 case WdfSpecialFileHibernation:
1187 xppdd->hiber_usage_kludge = is_in_notification_path;
1188 KdPrint((__DRIVER_NAME " notification_type = Hibernation, flag = %d\n", is_in_notification_path));
1189 break;
1190 case WdfSpecialFileDump:
1191 KdPrint((__DRIVER_NAME " notification_type = Dump, flag = %d\n", is_in_notification_path));
1192 break;
1193 default:
1194 KdPrint((__DRIVER_NAME " notification_type = %d, flag = %d\n", notification_type, is_in_notification_path));
1195 break;
1198 FUNCTION_EXIT();
1201 NTSTATUS
1202 XenPci_EvtChildListCreateDevice(WDFCHILDLIST child_list,
1203 PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER identification_header,
1204 PWDFDEVICE_INIT child_init)
1206 NTSTATUS status = STATUS_SUCCESS;
1207 WDF_OBJECT_ATTRIBUTES child_attributes;
1208 WDFDEVICE child_device;
1209 PXENPCI_PDO_IDENTIFICATION_DESCRIPTION identification = (PXENPCI_PDO_IDENTIFICATION_DESCRIPTION)identification_header;
1210 WDF_DEVICE_PNP_CAPABILITIES child_pnp_capabilities;
1211 DECLARE_UNICODE_STRING_SIZE(buffer, 512);
1212 DECLARE_CONST_UNICODE_STRING(location, L"Xen Bus");
1213 PXENPCI_PDO_DEVICE_DATA xppdd;
1214 PXENPCI_DEVICE_DATA xpdd = GetXpdd(WdfChildListGetDevice(child_list));
1215 WDF_QUERY_INTERFACE_CONFIG interface_config;
1216 BUS_INTERFACE_STANDARD bus_interface;
1217 WDF_PDO_EVENT_CALLBACKS pdo_callbacks;
1218 WDF_PNPPOWER_EVENT_CALLBACKS child_pnp_power_callbacks;
1219 UCHAR pnp_minor_functions[] = { IRP_MN_START_DEVICE };
1220 WDF_DEVICE_POWER_CAPABILITIES child_power_capabilities;
1222 FUNCTION_ENTER();
1224 WdfDeviceInitSetDeviceType(child_init, FILE_DEVICE_UNKNOWN);
1226 WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&child_pnp_power_callbacks);
1227 child_pnp_power_callbacks.EvtDeviceD0Entry = XenPciPdo_EvtDeviceD0Entry;
1228 //child_pnp_power_callbacks.EvtDeviceD0EntryPostInterruptsEnabled = XenPciPdo_EvtDeviceD0EntryPostInterruptsEnabled;
1229 child_pnp_power_callbacks.EvtDeviceD0Exit = XenPciPdo_EvtDeviceD0Exit;
1230 //child_pnp_power_callbacks.EvtDeviceD0ExitPreInterruptsDisabled = XenPciPdo_EvtDeviceD0ExitPreInterruptsDisabled;
1231 child_pnp_power_callbacks.EvtDevicePrepareHardware = XenPciPdo_EvtDevicePrepareHardware;
1232 child_pnp_power_callbacks.EvtDeviceReleaseHardware = XenPciPdo_EvtDeviceReleaseHardware;
1233 child_pnp_power_callbacks.EvtDeviceUsageNotification = XenPciPdo_EvtDeviceUsageNotification;
1234 WdfDeviceInitSetPnpPowerEventCallbacks(child_init, &child_pnp_power_callbacks);
1236 KdPrint((__DRIVER_NAME " device = '%s', index = '%d', path = '%s'\n",
1237 identification->device, identification->index, identification->path));
1239 status = WdfDeviceInitAssignWdmIrpPreprocessCallback(child_init, XenPciPdo_EvtDeviceWdmIrpPreprocess_START_DEVICE,
1240 IRP_MJ_PNP, pnp_minor_functions, ARRAY_SIZE(pnp_minor_functions));
1241 if (!NT_SUCCESS(status))
1243 return status;
1246 WDF_PDO_EVENT_CALLBACKS_INIT(&pdo_callbacks);
1247 //pdo_callbacks.EvtDeviceResourcesQuery = XenPciPdo_EvtDeviceResourcesQuery;
1248 pdo_callbacks.EvtDeviceResourceRequirementsQuery = XenPciPdo_EvtDeviceResourceRequirementsQuery;
1249 //pdo_callbacks.EvtDeviceEject = XenPciPdo_EvtDeviceEject;
1250 //pdo_callbacks.EvtDeviceSetLock = XenPciPdo_EvtDeviceSetLock;
1251 WdfPdoInitSetEventCallbacks(child_init, &pdo_callbacks);
1253 RtlUnicodeStringPrintf(&buffer, L"xen\\%S", identification->device);
1254 status = WdfPdoInitAssignDeviceID(child_init, &buffer);
1255 if (!NT_SUCCESS(status))
1257 return status;
1259 status = WdfPdoInitAddHardwareID(child_init, &buffer);
1260 if (!NT_SUCCESS(status))
1262 return status;
1264 status = WdfPdoInitAddCompatibleID(child_init, &buffer);
1265 if (!NT_SUCCESS(status))
1267 return status;
1270 RtlUnicodeStringPrintf(&buffer, L"%02d", identification->index);
1271 status = WdfPdoInitAssignInstanceID(child_init, &buffer);
1272 if (!NT_SUCCESS(status))
1274 return status;
1277 RtlUnicodeStringPrintf(&buffer, L"Xen %S device #%d", identification->device, identification->index);
1278 status = WdfPdoInitAddDeviceText(child_init, &buffer, &location, 0x0409);
1279 if (!NT_SUCCESS(status))
1281 return status;
1283 WdfPdoInitSetDefaultLocale(child_init, 0x0409);
1285 WdfDeviceInitSetPowerNotPageable(child_init);
1287 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&child_attributes, XENPCI_PDO_DEVICE_DATA);
1288 status = WdfDeviceCreate(&child_init, &child_attributes, &child_device);
1289 if (!NT_SUCCESS(status))
1291 return status;
1294 xppdd = GetXppdd(child_device);
1296 xppdd->wdf_device = child_device;
1297 xppdd->wdf_device_bus_fdo = WdfChildListGetDevice(child_list);
1299 xppdd->config_page_mdl = AllocateUncachedPage();
1301 xppdd->device_state.magic = XEN_DEVICE_STATE_MAGIC;
1302 xppdd->device_state.length = sizeof(XENPCI_DEVICE_STATE);
1303 xppdd->device_state.suspend_resume_state_pdo = SR_STATE_RUNNING;
1304 xppdd->device_state.suspend_resume_state_fdo = SR_STATE_RUNNING;
1305 xppdd->device_state.pdo_event_channel = xpdd->pdo_event_channel;
1306 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFilePaging, TRUE);
1307 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFileHibernation, TRUE);
1308 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFileDump, TRUE);
1310 WDF_DEVICE_PNP_CAPABILITIES_INIT(&child_pnp_capabilities);
1311 child_pnp_capabilities.LockSupported = WdfFalse;
1312 child_pnp_capabilities.EjectSupported = WdfTrue;
1313 child_pnp_capabilities.Removable = WdfTrue;
1314 child_pnp_capabilities.DockDevice = WdfFalse;
1315 child_pnp_capabilities.UniqueID = WdfFalse;
1316 child_pnp_capabilities.SilentInstall = WdfTrue;
1317 child_pnp_capabilities.SurpriseRemovalOK = WdfTrue;
1318 child_pnp_capabilities.HardwareDisabled = WdfFalse;
1319 WdfDeviceSetPnpCapabilities(child_device, &child_pnp_capabilities);
1321 WDF_DEVICE_POWER_CAPABILITIES_INIT(&child_power_capabilities);
1322 child_power_capabilities.DeviceD1 = WdfTrue;
1323 child_power_capabilities.WakeFromD1 = WdfTrue;
1324 child_power_capabilities.DeviceWake = PowerDeviceD1;
1325 child_power_capabilities.DeviceState[PowerSystemWorking] = PowerDeviceD1;
1326 child_power_capabilities.DeviceState[PowerSystemSleeping1] = PowerDeviceD1;
1327 child_power_capabilities.DeviceState[PowerSystemSleeping2] = PowerDeviceD2;
1328 child_power_capabilities.DeviceState[PowerSystemSleeping3] = PowerDeviceD2;
1329 child_power_capabilities.DeviceState[PowerSystemHibernate] = PowerDeviceD3;
1330 child_power_capabilities.DeviceState[PowerSystemShutdown] = PowerDeviceD3;
1331 WdfDeviceSetPowerCapabilities(child_device, &child_power_capabilities);
1333 bus_interface.Size = sizeof(BUS_INTERFACE_STANDARD);
1334 bus_interface.Version = 1; //BUS_INTERFACE_STANDARD_VERSION;
1335 bus_interface.Context = xppdd;
1336 bus_interface.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
1337 bus_interface.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
1338 bus_interface.TranslateBusAddress = XenPci_BIS_TranslateBusAddress;
1339 bus_interface.GetDmaAdapter = XenPci_BIS_GetDmaAdapter;
1340 bus_interface.SetBusData = XenPci_BIS_SetBusData;
1341 bus_interface.GetBusData = XenPci_BIS_GetBusData;
1342 WDF_QUERY_INTERFACE_CONFIG_INIT(&interface_config, (PINTERFACE)&bus_interface, &GUID_BUS_INTERFACE_STANDARD, NULL);
1343 status = WdfDeviceAddQueryInterface(child_device, &interface_config);
1344 if (!NT_SUCCESS(status))
1346 return status;
1349 RtlStringCbCopyA(xppdd->path, ARRAY_SIZE(xppdd->path), identification->path);
1350 RtlStringCbCopyA(xppdd->device, ARRAY_SIZE(xppdd->device), identification->device);
1351 xppdd->index = identification->index;
1352 KeInitializeEvent(&xppdd->backend_state_event, SynchronizationEvent, FALSE);
1353 ExInitializeFastMutex(&xppdd->backend_state_mutex);
1354 xppdd->backend_state = XenbusStateUnknown;
1355 xppdd->frontend_state = XenbusStateUnknown;
1356 xppdd->backend_path[0] = '\0';
1358 FUNCTION_EXIT();
1360 return status;
1363 static __forceinline VOID
1364 XenPci_Pdo_ChangeSuspendState(WDFDEVICE device, ULONG new_state)
1366 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1367 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1369 FUNCTION_ENTER();
1370 KdPrint((__DRIVER_NAME " setting pdo state to %d\n", new_state));
1371 xppdd->device_state.suspend_resume_state_pdo = new_state;
1372 KeMemoryBarrier();
1373 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xpdd->pdo_event_channel));
1374 EvtChn_Notify(xpdd, xpdd->pdo_event_channel);
1375 while(xppdd->device_state.suspend_resume_state_fdo != xppdd->device_state.suspend_resume_state_pdo)
1377 KdPrint((__DRIVER_NAME " waiting...\n"));
1378 KeWaitForSingleObject(&xpdd->pdo_suspend_event, Executive, KernelMode, FALSE, NULL);
1380 KdPrint((__DRIVER_NAME " fdo state set to %d\n", new_state));
1381 FUNCTION_EXIT();
1384 /* called at PASSIVE_LEVEL */
1385 NTSTATUS
1386 XenPci_Pdo_Suspend(WDFDEVICE device)
1388 NTSTATUS status = STATUS_SUCCESS;
1389 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1390 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1391 //LARGE_INTEGER wait_time;
1392 char path[128];
1393 PUCHAR in_ptr;
1394 UCHAR type;
1395 PVOID setting;
1396 PVOID value;
1397 PVOID value2;
1399 KdPrint((__DRIVER_NAME " --> " __FUNCTION__ " (%s)\n", xppdd->path));
1401 if (xppdd->backend_state == XenbusStateConnected)
1403 xppdd->restart_on_resume = TRUE;
1404 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_SUSPENDING);
1406 XenPci_ChangeFrontendState(device, XenbusStateClosing, XenbusStateClosing, 30000);
1407 XenPci_ChangeFrontendState(device, XenbusStateClosed, XenbusStateClosed, 30000);
1408 XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 30000);
1410 if (xppdd->assigned_resources_start != NULL)
1412 in_ptr = xppdd->assigned_resources_ptr;
1413 ADD_XEN_INIT_RSP(&in_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1414 in_ptr = xppdd->assigned_resources_start;
1415 while((type = GET_XEN_INIT_RSP(&in_ptr, &setting, &value, &value2)) != XEN_INIT_TYPE_END)
1417 switch (type)
1419 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
1420 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
1421 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
1422 EvtChn_Unbind(xpdd, PtrToUlong(value));
1423 EvtChn_Close(xpdd, PtrToUlong(value));
1424 break;
1429 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
1430 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, xppdd);
1432 else
1434 xppdd->restart_on_resume = FALSE;
1437 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
1439 return status;
1442 NTSTATUS
1443 XenPci_Pdo_Resume(WDFDEVICE device)
1445 NTSTATUS status = STATUS_SUCCESS;
1446 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1447 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1448 ULONG old_backend_state;
1449 PUCHAR src, dst;
1451 FUNCTION_ENTER();
1452 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
1454 xppdd->device_state.pdo_event_channel = xpdd->pdo_event_channel;
1455 old_backend_state = xppdd->backend_state;
1457 if (xppdd->restart_on_resume)
1459 status = XenPci_GetBackendAndAddWatch(device);
1461 if (XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 30000) != STATUS_SUCCESS)
1463 KdPrint((__DRIVER_NAME " Failed to change frontend state to Initialising\n"));
1464 // this is probably an unrecoverable situation...
1465 FUNCTION_EXIT();
1466 return STATUS_UNSUCCESSFUL;
1468 if (xppdd->assigned_resources_ptr)
1470 // reset things - feed the 'requested resources' back in
1471 ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1472 src = xppdd->requested_resources_start;
1473 xppdd->requested_resources_ptr = xppdd->requested_resources_start = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);;
1474 xppdd->assigned_resources_ptr = xppdd->assigned_resources_start;
1476 dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
1478 status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
1480 MmUnmapIoSpace(dst, xppdd->config_page_length);
1481 ExFreePoolWithTag(src, XENPCI_POOL_TAG);
1483 if (XenPci_ChangeFrontendState(device, XenbusStateConnected, XenbusStateConnected, 30000) != STATUS_SUCCESS)
1485 // this is definitely an unrecoverable situation...
1486 KdPrint((__DRIVER_NAME " Failed to change frontend state to connected\n"));
1487 FUNCTION_EXIT();
1488 return STATUS_UNSUCCESSFUL;
1490 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_RESUMING);
1491 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_RUNNING);
1494 FUNCTION_EXIT();
1496 return STATUS_SUCCESS;