win-pvdrivers

view xenpci/xenpci_pdo.c @ 933:f64a6d173e8d

Fix regression - fake mmio resource must be uncached for some versions of Windows.
author James Harper <james.harper@bendigoit.com.au>
date Sun Jun 12 15:23:31 2011 +1000 (2011-06-12)
parents ca7b237a61b1
children ef1f25e2c281
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;
36 static EVT_WDF_DEVICE_PNP_STATE_CHANGE_NOTIFICATION XenPci_EvtDevicePnpStateChange;
38 /*
39 Called at PASSIVE_LEVEL(?)
40 Called during restore
41 */
42 static ULONG
43 XenPci_ReadBackendState(PXENPCI_PDO_DEVICE_DATA xppdd)
44 {
45 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
46 char path[128];
47 char *value;
48 char *err;
49 ULONG backend_state;
51 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
52 err = XenBus_Read(xpdd, XBT_NIL, path, &value);
53 if (err)
54 {
55 XenPci_FreeMem(err);
56 return XenbusStateUnknown;
57 }
58 else
59 {
60 backend_state = atoi(value);
61 XenPci_FreeMem(value);
62 return backend_state;
63 }
64 }
66 static NTSTATUS
67 XenPciPdo_ReconfigureCompletionRoutine(
68 PDEVICE_OBJECT device_object,
69 PIRP irp,
70 PVOID context)
71 {
72 UNREFERENCED_PARAMETER(device_object);
74 if (irp->PendingReturned)
75 {
76 KeSetEvent ((PKEVENT)context, IO_NO_INCREMENT, FALSE);
77 }
78 return STATUS_MORE_PROCESSING_REQUIRED;
79 }
81 static VOID
82 XenPci_UpdateBackendState(PVOID context)
83 {
84 WDFDEVICE device = context;
85 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
86 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
87 ULONG new_backend_state;
89 FUNCTION_ENTER();
91 ExAcquireFastMutex(&xppdd->backend_state_mutex);
93 new_backend_state = XenPci_ReadBackendState(xppdd);
94 if (new_backend_state == XenbusStateUnknown)
95 {
96 if (xpdd->suspend_state != SUSPEND_STATE_NONE)
97 {
98 ExReleaseFastMutex(&xppdd->backend_state_mutex);
99 return;
100 }
101 KdPrint(("Failed to read path, assuming closed\n"));
102 new_backend_state = XenbusStateClosed;
103 }
105 if (xppdd->backend_state == new_backend_state)
106 {
107 KdPrint((__DRIVER_NAME " state unchanged\n"));
108 ExReleaseFastMutex(&xppdd->backend_state_mutex);
109 return;
110 }
112 xppdd->backend_state = new_backend_state;
114 switch (xppdd->backend_state)
115 {
116 case XenbusStateUnknown:
117 KdPrint((__DRIVER_NAME " Backend State Changed to Unknown\n"));
118 break;
120 case XenbusStateInitialising:
121 KdPrint((__DRIVER_NAME " Backend State Changed to Initialising\n"));
122 break;
124 case XenbusStateInitWait:
125 KdPrint((__DRIVER_NAME " Backend State Changed to InitWait\n"));
126 break;
128 case XenbusStateInitialised:
129 KdPrint((__DRIVER_NAME " Backend State Changed to Initialised\n"));
130 break;
132 case XenbusStateConnected:
133 KdPrint((__DRIVER_NAME " Backend State Changed to Connected\n"));
134 break;
136 case XenbusStateClosing:
137 KdPrint((__DRIVER_NAME " Backend State Changed to Closing\n"));
138 if (xppdd->frontend_state != XenbusStateClosing)
139 {
140 xppdd->backend_initiated_remove = TRUE;
141 KdPrint((__DRIVER_NAME " Requesting eject\n"));
142 WdfPdoRequestEject(device);
143 }
144 break;
146 case XenbusStateClosed:
147 KdPrint((__DRIVER_NAME " Backend State Changed to Closed\n"));
148 break;
150 default:
151 KdPrint((__DRIVER_NAME " Backend State Changed to Undefined = %d\n", xppdd->backend_state));
152 break;
153 }
155 KeSetEvent(&xppdd->backend_state_event, 1, FALSE);
157 ExReleaseFastMutex(&xppdd->backend_state_mutex);
158 FUNCTION_EXIT();
160 return;
161 }
163 static VOID
164 XenPci_BackendStateHandler(char *path, PVOID context)
165 {
166 UNREFERENCED_PARAMETER(path);
168 /* check that path == device/id/state */
169 //RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->path);
171 XenPci_UpdateBackendState(context);
172 }
174 static NTSTATUS
175 XenPci_GetBackendAndAddWatch(WDFDEVICE device)
176 {
177 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
178 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
179 char path[128];
180 PCHAR res;
181 PCHAR value;
183 FUNCTION_ENTER();
184 /* Get backend path */
185 RtlStringCbPrintfA(path, ARRAY_SIZE(path),
186 "%s/backend", xppdd->path);
187 res = XenBus_Read(xpdd, XBT_NIL, path, &value);
188 if (res)
189 {
190 KdPrint((__DRIVER_NAME " Failed to read backend path\n"));
191 XenPci_FreeMem(res);
192 return STATUS_UNSUCCESSFUL;
193 }
194 RtlStringCbCopyA(xppdd->backend_path, ARRAY_SIZE(xppdd->backend_path), value);
195 XenPci_FreeMem(value);
197 /* Add watch on backend state */
198 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
199 XenBus_AddWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
201 FUNCTION_EXIT();
202 return STATUS_SUCCESS;
203 }
205 static NTSTATUS
206 XenConfig_InitConfigPage(WDFDEVICE device)
207 {
208 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
209 //PXENCONFIG_DEVICE_DATA xcdd = (PXENCONFIG_DEVICE_DATA)device_object->DeviceExtension;
210 //PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
211 //PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
212 PUCHAR ptr;
213 PDEVICE_OBJECT curr, prev;
214 PDRIVER_OBJECT fdo_driver_object;
215 PUCHAR fdo_driver_extension;
217 FUNCTION_ENTER();
219 ptr = MmGetMdlVirtualAddress(xppdd->config_page_mdl);
220 curr = IoGetAttachedDeviceReference(WdfDeviceWdmGetDeviceObject(device));
221 //curr = WdfDeviceWdmGetAttachedDevice(device);
222 while (curr != NULL)
223 {
224 fdo_driver_object = curr->DriverObject;
225 KdPrint((__DRIVER_NAME " fdo_driver_object = %p\n", fdo_driver_object));
226 if (fdo_driver_object)
227 {
228 fdo_driver_extension = IoGetDriverObjectExtension(fdo_driver_object, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC));
229 KdPrint((__DRIVER_NAME " fdo_driver_extension = %p\n", fdo_driver_extension));
230 if (fdo_driver_extension)
231 {
232 memcpy(ptr, fdo_driver_extension, PAGE_SIZE);
233 ObDereferenceObject(curr);
234 break;
235 }
236 }
237 prev = curr;
238 curr = IoGetLowerDeviceObject(curr);
239 ObDereferenceObject(prev);
240 }
242 FUNCTION_EXIT();
244 return STATUS_SUCCESS;
245 }
247 static NTSTATUS
248 XenPci_EvtChn_Bind(PVOID context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
249 {
250 WDFDEVICE device = context;
251 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
252 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
254 return EvtChn_Bind(xpdd, Port, ServiceRoutine, ServiceContext, EVT_ACTION_FLAGS_DEFAULT);
255 }
257 static NTSTATUS
258 XenPci_EvtChn_BindDpc(PVOID context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
259 {
260 WDFDEVICE device = context;
261 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
262 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
264 return EvtChn_BindDpc(xpdd, Port, ServiceRoutine, ServiceContext, EVT_ACTION_FLAGS_DEFAULT);
265 }
267 static NTSTATUS
268 XenPci_EvtChn_Unbind(PVOID context, evtchn_port_t Port)
269 {
270 WDFDEVICE device = context;
271 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
272 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
274 return EvtChn_Unbind(xpdd, Port);
275 }
277 static NTSTATUS
278 XenPci_EvtChn_Mask(PVOID context, evtchn_port_t Port)
279 {
280 WDFDEVICE device = context;
281 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
282 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
284 return EvtChn_Mask(xpdd, Port);
285 }
287 static NTSTATUS
288 XenPci_EvtChn_Unmask(PVOID context, evtchn_port_t Port)
289 {
290 WDFDEVICE device = context;
291 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
292 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
294 return EvtChn_Unmask(xpdd, Port);
295 }
297 static NTSTATUS
298 XenPci_EvtChn_Notify(PVOID context, evtchn_port_t Port)
299 {
300 WDFDEVICE device = context;
301 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
302 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
304 return EvtChn_Notify(xpdd, Port);
305 }
307 static BOOLEAN
308 XenPci_EvtChn_AckEvent(PVOID context, evtchn_port_t port, BOOLEAN *last_interrupt)
309 {
310 WDFDEVICE device = context;
311 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
312 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
314 return EvtChn_AckEvent(xpdd, port, last_interrupt);
315 }
317 typedef struct {
318 PXEN_EVTCHN_SYNC_ROUTINE sync_routine;
319 PVOID sync_context;
320 } sync_context_t;
322 static BOOLEAN
323 XenPci_EvtChn_Sync_Routine(WDFINTERRUPT interrupt, WDFCONTEXT context)
324 {
325 sync_context_t *wdf_sync_context = context;
326 UNREFERENCED_PARAMETER(interrupt);
327 return wdf_sync_context->sync_routine(wdf_sync_context->sync_context);
328 }
330 static BOOLEAN
331 XenPci_EvtChn_Sync(PVOID context, PXEN_EVTCHN_SYNC_ROUTINE sync_routine, PVOID sync_context)
332 {
333 WDFDEVICE device = context;
334 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
335 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
336 sync_context_t wdf_sync_context;
338 wdf_sync_context.sync_routine = sync_routine;
339 wdf_sync_context.sync_context = sync_context;
341 return WdfInterruptSynchronize(xpdd->interrupt, XenPci_EvtChn_Sync_Routine, &wdf_sync_context);
342 }
344 static grant_ref_t
345 XenPci_GntTbl_GrantAccess(PVOID context, domid_t domid, uint32_t frame, int readonly, grant_ref_t ref, ULONG tag)
346 {
347 WDFDEVICE device = context;
348 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
349 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
351 return GntTbl_GrantAccess(xpdd, domid, frame, readonly, ref, tag);
352 }
354 static BOOLEAN
355 XenPci_GntTbl_EndAccess(PVOID context, grant_ref_t ref, BOOLEAN keepref, ULONG tag)
356 {
357 WDFDEVICE device = context;
358 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
359 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
361 return GntTbl_EndAccess(xpdd, ref, keepref, tag);
362 }
364 static VOID
365 XenPci_GntTbl_PutRef(PVOID context, grant_ref_t ref, ULONG tag)
366 {
367 WDFDEVICE device = context;
368 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
369 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
371 GntTbl_PutRef(xpdd, ref, tag);
372 }
374 static grant_ref_t
375 XenPci_GntTbl_GetRef(PVOID context, ULONG tag)
376 {
377 WDFDEVICE device = context;
378 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
379 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
381 return GntTbl_GetRef(xpdd, tag);
382 }
384 PCHAR
385 XenPci_XenBus_Read(PVOID context, xenbus_transaction_t xbt, char *path, char **value)
386 {
387 WDFDEVICE device = context;
388 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
389 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
390 return XenBus_Read(xpdd, xbt, path, value);
391 }
393 PCHAR
394 XenPci_XenBus_Write(PVOID context, xenbus_transaction_t xbt, char *path, char *value)
395 {
396 WDFDEVICE device = context;
397 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
398 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
399 return XenBus_Write(xpdd, xbt, path, value);
400 }
402 PCHAR
403 XenPci_XenBus_Printf(PVOID context, xenbus_transaction_t xbt, char *path, char *fmt, ...)
404 {
405 //PXENPCI_PDO_DEVICE_DATA xppdd = Context;
406 //PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
407 //return XenBus_Printf(xpdd, xbt, path, value);
408 UNREFERENCED_PARAMETER(context);
409 UNREFERENCED_PARAMETER(xbt);
410 UNREFERENCED_PARAMETER(path);
411 UNREFERENCED_PARAMETER(fmt);
412 return NULL;
413 }
415 PCHAR
416 XenPci_XenBus_StartTransaction(PVOID context, xenbus_transaction_t *xbt)
417 {
418 WDFDEVICE device = context;
419 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
420 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
421 return XenBus_StartTransaction(xpdd, xbt);
422 }
424 PCHAR
425 XenPci_XenBus_EndTransaction(PVOID context, xenbus_transaction_t xbt, int abort, int *retry)
426 {
427 WDFDEVICE device = context;
428 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
429 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
430 return XenBus_EndTransaction(xpdd, xbt, abort, retry);
431 }
433 PCHAR
434 XenPci_XenBus_List(PVOID context, xenbus_transaction_t xbt, char *prefix, char ***contents)
435 {
436 WDFDEVICE device = context;
437 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
438 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
439 return XenBus_List(xpdd, xbt, prefix, contents);
440 }
442 PCHAR
443 XenPci_XenBus_AddWatch(PVOID context, xenbus_transaction_t xbt, char *path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
444 {
445 WDFDEVICE device = context;
446 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
447 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
448 PCHAR retval;
450 FUNCTION_ENTER();
451 retval = XenBus_AddWatch(xpdd, xbt, path, ServiceRoutine, ServiceContext);
452 if (retval == NULL)
453 {
454 KdPrint((__DRIVER_NAME " XenPci_XenBus_AddWatch - %s = NULL\n", path));
455 }
456 else
457 {
458 KdPrint((__DRIVER_NAME " XenPci_XenBus_AddWatch - %s = %s\n", path, retval));
459 }
460 FUNCTION_EXIT();
461 return retval;
462 }
464 PCHAR
465 XenPci_XenBus_RemWatch(PVOID context, xenbus_transaction_t xbt, char *path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
466 {
467 WDFDEVICE device = context;
468 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
469 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
470 return XenBus_RemWatch(xpdd, xbt, path, ServiceRoutine, ServiceContext);
471 }
473 /*
474 Called at PASSIVE_LEVEL
475 Called during restore
476 */
478 static NTSTATUS
479 XenPci_ChangeFrontendState(WDFDEVICE device, ULONG frontend_state_set, ULONG backend_state_response, ULONG maximum_wait_ms)
480 {
481 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
482 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
483 LARGE_INTEGER timeout;
484 ULONG remaining;
485 ULONG thiswait;
486 char path[128];
488 FUNCTION_ENTER();
490 xppdd->frontend_state = frontend_state_set;
492 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->path);
493 XenBus_Printf(xpdd, XBT_NIL, path, "%d", frontend_state_set);
495 remaining = maximum_wait_ms;
497 while (xppdd->backend_state != backend_state_response)
498 {
499 thiswait = min((LONG)remaining, 1000); // 1 second or remaining time, whichever is less
500 timeout.QuadPart = (LONGLONG)-1 * thiswait * 1000 * 10;
501 if (KeWaitForSingleObject(&xppdd->backend_state_event, Executive, KernelMode, FALSE, &timeout) == STATUS_TIMEOUT)
502 {
503 /* it's possible that the workitems are blocked because the pagefile isn't available. Lets just re-read the backend value for now */
504 XenPci_UpdateBackendState(device);
505 remaining -= thiswait;
506 if (remaining == 0)
507 {
508 KdPrint((__DRIVER_NAME " Timed out waiting for %d!\n", backend_state_response));
509 return STATUS_UNSUCCESSFUL;
510 }
511 KdPrint((__DRIVER_NAME " Still waiting for %d (currently %d)...\n", backend_state_response, xppdd->backend_state));
512 }
513 }
514 FUNCTION_EXIT();
515 return STATUS_SUCCESS;
516 }
518 static NTSTATUS
519 XenPci_ChangeFrontendStateMap(WDFDEVICE device, PXENPCI_STATE_MAP_ELEMENT map)
520 {
521 NTSTATUS status;
523 FUNCTION_ENTER();
524 while(map->front_target)
525 {
526 //KdPrint((__DRIVER_NAME " Changing state to %d expecting %d\n", (ULONG)map->front_target, (ULONG)map->back_expected));
527 if (!NT_SUCCESS(status = XenPci_ChangeFrontendState(device, (ULONG)map->front_target, (ULONG)map->back_expected, (ULONG)map->wait * 100)))
528 {
529 FUNCTION_EXIT();
530 return status;
531 }
532 map++;
533 }
534 FUNCTION_EXIT();
535 return STATUS_SUCCESS;
536 }
538 static NTSTATUS
539 XenPci_XenConfigDevice(WDFDEVICE device);
541 static NTSTATUS
542 XenPci_XenShutdownDevice(PVOID context)
543 {
544 WDFDEVICE device = context;
545 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
546 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
547 PUCHAR in_ptr;
548 ULONG i;
549 UCHAR type;
550 PVOID setting;
551 PVOID value;
552 PVOID value2;
554 FUNCTION_ENTER();
556 if (xppdd->backend_state == XenbusStateConnected)
557 {
558 XenPci_ChangeFrontendStateMap(device, xppdd->xb_shutdown_map);
559 }
560 else if (xppdd->backend_state == XenbusStateClosing)
561 {
562 XenPci_ChangeFrontendState(device, XenbusStateClosed, XenbusStateClosed, 30000);
563 }
565 if (xppdd->assigned_resources_start != NULL)
566 {
567 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
568 in_ptr = xppdd->assigned_resources_start;
569 while((type = GET_XEN_INIT_RSP(&in_ptr, &setting, &value, &value2)) != XEN_INIT_TYPE_END)
570 {
571 switch (type)
572 {
573 case XEN_INIT_TYPE_RING: /* frontend ring */
574 FreePages(value);
575 break;
576 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
577 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
578 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
579 EvtChn_Unbind(xpdd, PtrToUlong(value));
580 EvtChn_Close(xpdd, PtrToUlong(value));
581 break;
582 case XEN_INIT_TYPE_GRANT_ENTRIES:
583 for (i = 0; i < PtrToUlong(value); i++)
584 GntTbl_EndAccess(xpdd, ((grant_ref_t *)value2)[i], FALSE, PtrToUlong(setting));
585 break;
586 }
587 }
588 ExFreePoolWithTag(xppdd->assigned_resources_start, XENPCI_POOL_TAG);
589 xppdd->assigned_resources_start = NULL;
590 }
592 FUNCTION_EXIT();
594 return STATUS_SUCCESS;
595 }
597 struct dummy_sring {
598 RING_IDX req_prod, req_event;
599 RING_IDX rsp_prod, rsp_event;
600 uint8_t pad[48];
601 };
603 static NTSTATUS
604 XenPci_XenConfigDeviceSpecifyBuffers(WDFDEVICE device, PUCHAR src, PUCHAR dst)
605 {
606 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
607 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
608 NTSTATUS status = STATUS_SUCCESS;
609 ULONG i;
610 char path[128];
611 PCHAR setting, value;
612 PCHAR res;
613 PVOID address;
614 UCHAR type;
615 PUCHAR in_ptr;
616 PUCHAR out_ptr;
617 XENPCI_VECTORS vectors;
618 ULONG event_channel;
619 PMDL ring;
620 grant_ref_t gref;
621 PVOID value2;
622 int map_no;
624 FUNCTION_ENTER();
626 xppdd->xb_pre_connect_map[0].front_target = 0;
627 xppdd->xb_post_connect_map[0].front_target = 0;
628 xppdd->xb_shutdown_map[0].front_target = 0;
630 in_ptr = src;
631 out_ptr = dst;
633 // always add vectors
634 vectors.magic = XEN_DATA_MAGIC;
635 vectors.length = sizeof(XENPCI_VECTORS);
636 vectors.context = device;
637 vectors.EvtChn_Bind = XenPci_EvtChn_Bind;
638 vectors.EvtChn_BindDpc = XenPci_EvtChn_BindDpc;
639 vectors.EvtChn_Unbind = XenPci_EvtChn_Unbind;
640 vectors.EvtChn_Mask = XenPci_EvtChn_Mask;
641 vectors.EvtChn_Unmask = XenPci_EvtChn_Unmask;
642 vectors.EvtChn_Notify = XenPci_EvtChn_Notify;
643 vectors.EvtChn_AckEvent = XenPci_EvtChn_AckEvent;
644 vectors.EvtChn_Sync = XenPci_EvtChn_Sync;
645 vectors.GntTbl_GetRef = XenPci_GntTbl_GetRef;
646 vectors.GntTbl_PutRef = XenPci_GntTbl_PutRef;
647 vectors.GntTbl_GrantAccess = XenPci_GntTbl_GrantAccess;
648 vectors.GntTbl_EndAccess = XenPci_GntTbl_EndAccess;
649 vectors.XenPci_XenConfigDevice = XenPci_XenConfigDevice;
650 vectors.XenPci_XenShutdownDevice = XenPci_XenShutdownDevice;
651 RtlStringCbCopyA(vectors.path, 128, xppdd->path);
652 RtlStringCbCopyA(vectors.backend_path, 128, xppdd->backend_path);
653 //vectors.pdo_event_channel = xpdd->pdo_event_channel;
654 vectors.XenBus_Read = XenPci_XenBus_Read;
655 vectors.XenBus_Write = XenPci_XenBus_Write;
656 vectors.XenBus_Printf = XenPci_XenBus_Printf;
657 vectors.XenBus_StartTransaction = XenPci_XenBus_StartTransaction;
658 vectors.XenBus_EndTransaction = XenPci_XenBus_EndTransaction;
659 vectors.XenBus_List = XenPci_XenBus_List;
660 vectors.XenBus_AddWatch = XenPci_XenBus_AddWatch;
661 vectors.XenBus_RemWatch = XenPci_XenBus_RemWatch;
663 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_PROTOCOL_VERSION, NULL, UlongToPtr(qemu_protocol_version), NULL);
665 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_VECTORS, NULL, &vectors, NULL);
666 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_STATE_PTR, NULL, &xppdd->device_state, NULL);
668 // need to make sure we never get here while backend state is connected or closing...
669 // first pass, possibly before state == Connected
670 in_ptr = src;
671 while((type = GET_XEN_INIT_REQ(&in_ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
672 {
673 ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, type, setting, value, value2);
675 switch (type)
676 {
677 case XEN_INIT_TYPE_WRITE_STRING: /* frontend setting = value */
678 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_WRITE_STRING - %s = %s\n", setting, value));
679 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
680 XenBus_Printf(xpdd, XBT_NIL, path, "%s", value);
681 break;
682 case XEN_INIT_TYPE_RING: /* frontend ring */
683 /* we only allocate and do the SHARED_RING_INIT here */
684 if ((ring = AllocatePage()) != 0)
685 {
686 address = MmGetMdlVirtualAddress(ring);
687 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %p\n", setting, address));
688 SHARED_RING_INIT((struct dummy_sring *)address);
689 if ((gref = GntTbl_GrantAccess(
690 xpdd, 0, (ULONG)*MmGetMdlPfnArray(ring), FALSE, INVALID_GRANT_REF, (ULONG)'XPDO')) != INVALID_GRANT_REF)
691 {
692 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
693 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %d\n", setting, gref));
694 XenBus_Printf(xpdd, XBT_NIL, path, "%d", gref);
695 ADD_XEN_INIT_RSP(&out_ptr, type, setting, address, NULL);
696 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, type, setting, ring, NULL);
697 // add the grant entry too so it gets freed automatically
698 __ADD_XEN_INIT_UCHAR(&xppdd->assigned_resources_ptr, XEN_INIT_TYPE_GRANT_ENTRIES);
699 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, (ULONG)'XPDO');
700 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, 1);
701 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, gref);
702 }
703 else
704 {
705 FreePages(ring);
706 status = STATUS_UNSUCCESSFUL;
707 goto error;
708 }
709 }
710 else
711 {
712 status = STATUS_UNSUCCESSFUL;
713 goto error;
714 }
715 break;
716 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
717 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
718 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
719 if ((event_channel = EvtChn_AllocUnbound(xpdd, 0)) != 0)
720 {
721 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_EVENT_CHANNEL - %s = %d\n", setting, event_channel));
722 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
723 XenBus_Printf(xpdd, XBT_NIL, path, "%d", event_channel);
724 ADD_XEN_INIT_RSP(&out_ptr, type, setting, UlongToPtr(event_channel), NULL);
725 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, type, setting, UlongToPtr(event_channel), NULL);
726 if (type == XEN_INIT_TYPE_EVENT_CHANNEL_IRQ)
727 {
728 EvtChn_BindIrq(xpdd, event_channel, xppdd->irq_vector, path, EVT_ACTION_FLAGS_DEFAULT);
729 }
730 else if (type == XEN_INIT_TYPE_EVENT_CHANNEL_DPC)
731 {
732 #pragma warning(suppress:4055)
733 EvtChn_BindDpc(xpdd, event_channel, (PXEN_EVTCHN_SERVICE_ROUTINE)value, value2, EVT_ACTION_FLAGS_DEFAULT);
734 }
735 else
736 {
737 #pragma warning(suppress:4055)
738 EvtChn_Bind(xpdd, event_channel, (PXEN_EVTCHN_SERVICE_ROUTINE)value, value2, EVT_ACTION_FLAGS_DEFAULT);
739 }
740 }
741 else
742 {
743 status = STATUS_UNSUCCESSFUL;
744 goto error;
745 }
746 break;
747 case XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT:
748 map_no = 0;
749 while ((xppdd->xb_pre_connect_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
750 {
751 xppdd->xb_pre_connect_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
752 xppdd->xb_pre_connect_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
753 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].front_target);
754 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].back_expected);
755 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].wait);
756 map_no++;
757 }
758 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
759 break;
760 case XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT:
761 map_no = 0;
762 while ((xppdd->xb_post_connect_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
763 {
764 xppdd->xb_post_connect_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
765 xppdd->xb_post_connect_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
766 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].front_target);
767 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].back_expected);
768 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].wait);
769 map_no++;
770 }
771 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
772 break;
773 case XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN:
774 map_no = 0;
775 while ((xppdd->xb_shutdown_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
776 {
777 xppdd->xb_shutdown_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
778 xppdd->xb_shutdown_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
779 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].front_target);
780 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].back_expected);
781 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].wait);
782 map_no++;
783 }
784 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
785 break;
786 }
787 }
788 if (!NT_SUCCESS(status))
789 {
790 goto error;
791 }
793 if (XenPci_ChangeFrontendStateMap(device, xppdd->xb_pre_connect_map) != STATUS_SUCCESS)
794 {
795 status = STATUS_UNSUCCESSFUL;
796 goto error;
797 }
799 // second pass, possibly after state == Connected
800 in_ptr = src;
801 while((type = GET_XEN_INIT_REQ(&in_ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
802 {
803 switch(type)
804 {
805 case XEN_INIT_TYPE_READ_STRING_BACK:
806 case XEN_INIT_TYPE_READ_STRING_FRONT:
807 if (type == XEN_INIT_TYPE_READ_STRING_FRONT)
808 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
809 else
810 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->backend_path, setting);
811 res = XenBus_Read(xpdd, XBT_NIL, path, &value);
812 if (res)
813 {
814 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = <failed>\n", setting));
815 XenPci_FreeMem(res);
816 ADD_XEN_INIT_RSP(&out_ptr, type, setting, "", "");
817 }
818 else
819 {
820 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = %s\n", setting, value));
821 ADD_XEN_INIT_RSP(&out_ptr, type, setting, value, value2);
822 XenPci_FreeMem(value);
823 }
824 break;
825 case XEN_INIT_TYPE_VECTORS:
826 // this is always done so ignore the request
827 break;
828 case XEN_INIT_TYPE_GRANT_ENTRIES:
829 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_GRANT_ENTRIES - %d\n", PtrToUlong(value)));
830 __ADD_XEN_INIT_UCHAR(&out_ptr, type);
831 __ADD_XEN_INIT_UCHAR(&xppdd->assigned_resources_ptr, type);
832 __ADD_XEN_INIT_ULONG(&out_ptr, PtrToUlong(setting));
833 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, PtrToUlong(setting));
834 __ADD_XEN_INIT_ULONG(&out_ptr, PtrToUlong(value));
835 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, PtrToUlong(value));
836 for (i = 0; i < PtrToUlong(value); i++)
837 {
838 gref = GntTbl_GetRef(xpdd, PtrToUlong(setting));
839 __ADD_XEN_INIT_ULONG(&out_ptr, gref);
840 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, gref);
841 }
842 break;
843 /* just need to eat these */
844 case XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT:
845 case XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT:
846 case XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN:
847 while ((__GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
848 {
849 __GET_XEN_INIT_UCHAR(&in_ptr);
850 __GET_XEN_INIT_UCHAR(&in_ptr);
851 }
852 break;
853 }
854 }
855 if (qemu_hide_flags_value)
856 {
857 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_HIDE_FLAGS, NULL, UlongToPtr(qemu_hide_flags_value), NULL);
858 }
859 else
860 {
861 DECLARE_UNICODE_STRING_SIZE(my_device_name, 128);
863 RtlUnicodeStringPrintf(&my_device_name, L"#%S#", xppdd->device);
865 for (i = 0; i < WdfCollectionGetCount(qemu_hide_devices); i++)
866 {
867 WDFSTRING wdf_string = WdfCollectionGetItem(qemu_hide_devices, i);
868 UNICODE_STRING hide_device_name;
870 WdfStringGetUnicodeString(wdf_string, &hide_device_name);
872 if (RtlCompareUnicodeString(&hide_device_name, &my_device_name, TRUE) != 0)
873 {
874 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_HIDE_FILTER, NULL, NULL, NULL);
875 break;
876 }
877 }
878 }
880 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
882 if (XenPci_ChangeFrontendStateMap(device, xppdd->xb_post_connect_map) != STATUS_SUCCESS)
883 {
884 status = STATUS_UNSUCCESSFUL;
885 goto error;
886 }
887 FUNCTION_EXIT();
888 return status;
890 error:
891 KdPrint((__DRIVER_NAME " Error\n"));
892 XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 2000);
893 FUNCTION_EXIT_STATUS(status);
894 return status;
895 }
897 static NTSTATUS
898 XenPci_XenConfigDevice(WDFDEVICE device)
899 {
900 NTSTATUS status;
901 PUCHAR src, dst;
902 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
904 src = ExAllocatePoolWithTag(NonPagedPool, xppdd->config_page_length, XENPCI_POOL_TAG);
905 dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
906 memcpy(src, dst, xppdd->config_page_length);
908 status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
910 MmUnmapIoSpace(dst, xppdd->config_page_length);
911 ExFreePoolWithTag(src, XENPCI_POOL_TAG);
913 return status;
914 }
916 static NTSTATUS
917 XenPciPdo_EvtDeviceWdmIrpPreprocess_START_DEVICE(WDFDEVICE device, PIRP irp)
918 {
919 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
920 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
921 PIO_STACK_LOCATION stack;
922 PCM_PARTIAL_RESOURCE_LIST prl;
923 PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
924 ULONG i;
925 //char path[128];
926 //PMDL mdl;
928 FUNCTION_ENTER();
929 KdPrint((__DRIVER_NAME " %s\n", xppdd->path));
931 stack = IoGetCurrentIrpStackLocation(irp);
933 prl = &stack->Parameters.StartDevice.AllocatedResources->List[0].PartialResourceList;
934 for (i = 0; i < prl->Count; i++)
935 {
936 prd = & prl->PartialDescriptors[i];
937 switch (prd->Type)
938 {
939 case CmResourceTypeMemory:
940 if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart && prd->u.Memory.Length == 0)
941 {
942 prd->u.Memory.Start.QuadPart = MmGetMdlPfnArray(xppdd->config_page_mdl)[0] << PAGE_SHIFT;
943 prd->u.Memory.Length = MmGetMdlByteCount(xppdd->config_page_mdl);
944 }
945 else if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart + 1 && prd->u.Memory.Length == 0)
946 {
947 RtlZeroMemory(prd, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
948 prd->Type = CmResourceTypeInterrupt;
949 prd->ShareDisposition = CmResourceShareShared;
950 prd->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
951 prd->u.Interrupt.Level = 0; // Set group and level to zero (group = upper word)
952 prd->u.Interrupt.Level = xpdd->irq_number & 0xffff; // Only set the lower word
953 prd->u.Interrupt.Vector = xpdd->irq_number;
954 prd->u.Interrupt.Affinity = xpdd->irq_affinity; //KeQueryActiveProcessors();
955 xppdd->irq_number = xpdd->irq_number;
956 }
957 break;
958 }
959 }
961 prl = &stack->Parameters.StartDevice.AllocatedResourcesTranslated->List[0].PartialResourceList;
962 for (i = 0; i < prl->Count; i++)
963 {
964 prd = & prl->PartialDescriptors[i];
965 switch (prd->Type)
966 {
967 case CmResourceTypeMemory:
968 KdPrint((__DRIVER_NAME " CmResourceTypeMemory (%d)\n", i));
969 KdPrint((__DRIVER_NAME " Start = %08x, Length = %d\n", prd->u.Memory.Start.LowPart, prd->u.Memory.Length));
970 if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart)
971 {
972 if (prd->u.Memory.Length == 0)
973 {
974 KdPrint((__DRIVER_NAME " pfn[0] = %08x\n", (ULONG)MmGetMdlPfnArray(xppdd->config_page_mdl)[0]));
975 prd->u.Memory.Start.QuadPart = (ULONGLONG)MmGetMdlPfnArray(xppdd->config_page_mdl)[0] << PAGE_SHIFT;
976 prd->u.Memory.Length = MmGetMdlByteCount(xppdd->config_page_mdl);
977 KdPrint((__DRIVER_NAME " New Start = %08x%08x, Length = %d\n", prd->u.Memory.Start.HighPart, prd->u.Memory.Start.LowPart, prd->u.Memory.Length));
978 }
979 xppdd->config_page_phys = prd->u.Memory.Start;
980 xppdd->config_page_length = prd->u.Memory.Length;
981 xppdd->requested_resources_start = xppdd->requested_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
982 //xppdd->assigned_resources_start = xppdd->assigned_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
984 #if 0
985 status = XenPci_XenConfigDevice(device);
986 if (!NT_SUCCESS(status))
987 {
988 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
989 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
990 FUNCTION_EXIT_STATUS(status);
991 return status;
992 }
993 #endif
994 }
995 else if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart + 1 && prd->u.Memory.Length == 0)
996 {
997 RtlZeroMemory(prd, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
998 prd->Type = CmResourceTypeInterrupt;
999 prd->ShareDisposition = CmResourceShareShared;
1000 prd->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
1001 prd->u.Interrupt.Level = 0; // Set group and level to zero (group = upper word)
1002 prd->u.Interrupt.Level = xpdd->irq_level & 0xffff; // Only set the lower word
1003 prd->u.Interrupt.Vector = xpdd->irq_vector;
1004 prd->u.Interrupt.Affinity = xpdd->irq_affinity; //KeQueryActiveProcessors();
1005 xppdd->irq_vector = xpdd->irq_vector;
1006 xppdd->irq_level = xpdd->irq_level;
1008 break;
1012 IoSkipCurrentIrpStackLocation(irp);
1014 FUNCTION_EXIT();
1016 return WdfDeviceWdmDispatchPreprocessedIrp(device, irp);
1019 #if 0
1020 static NTSTATUS
1021 XenPciPdo_EvtDeviceResourcesQuery(WDFDEVICE device, WDFCMRESLIST resources)
1024 #endif
1026 static NTSTATUS
1027 XenPciPdo_EvtDeviceResourceRequirementsQuery(WDFDEVICE device, WDFIORESREQLIST requirements_list)
1029 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1030 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1031 WDFIORESLIST res_list;
1032 IO_RESOURCE_DESCRIPTOR ird;
1034 //FUNCTION_ENTER();
1036 WdfIoResourceRequirementsListSetInterfaceType(requirements_list, PNPBus);
1038 WdfIoResourceListCreate(requirements_list, WDF_NO_OBJECT_ATTRIBUTES, &res_list);
1039 ird.Option = 0;
1040 ird.Type = CmResourceTypeMemory;
1041 ird.ShareDisposition = CmResourceShareShared;
1042 ird.Flags = CM_RESOURCE_MEMORY_READ_WRITE | CM_RESOURCE_MEMORY_CACHEABLE;
1043 ird.u.Memory.MinimumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart;
1044 ird.u.Memory.MaximumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart;
1045 ird.u.Memory.Length = 0;
1046 ird.u.Memory.Alignment = 1; //PAGE_SIZE;
1047 WdfIoResourceListAppendDescriptor(res_list, &ird);
1049 ird.Option = 0;
1050 ird.Type = CmResourceTypeMemory;
1051 ird.ShareDisposition = CmResourceShareShared;
1052 ird.Flags = CM_RESOURCE_MEMORY_READ_WRITE | CM_RESOURCE_MEMORY_CACHEABLE;
1053 ird.u.Memory.MinimumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart + 1;
1054 ird.u.Memory.MaximumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart + 1;
1055 ird.u.Memory.Length = 0;
1056 ird.u.Memory.Alignment = 1; //PAGE_SIZE;
1057 WdfIoResourceListAppendDescriptor(res_list, &ird);
1059 WdfIoResourceRequirementsListAppendIoResList(requirements_list, res_list);
1061 //FUNCTION_EXIT();
1063 return STATUS_SUCCESS;
1066 NTSTATUS
1067 XenPciPdo_EvtDeviceD0Entry(WDFDEVICE device, WDF_POWER_DEVICE_STATE previous_state)
1069 NTSTATUS status = STATUS_SUCCESS;
1070 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1071 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1072 CHAR path[128];
1074 FUNCTION_ENTER();
1075 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
1077 switch (previous_state)
1079 case WdfPowerDeviceD0:
1080 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1081 break;
1082 case WdfPowerDeviceD1:
1083 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1084 break;
1085 case WdfPowerDeviceD2:
1086 KdPrint((__DRIVER_NAME " WdfPowerDeviceD2\n"));
1087 break;
1088 case WdfPowerDeviceD3:
1089 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3\n"));
1090 if (xppdd->hiber_usage_kludge)
1092 KdPrint((__DRIVER_NAME " (but really WdfPowerDevicePrepareForHibernation)\n"));
1093 previous_state = WdfPowerDevicePrepareForHibernation;
1095 break;
1096 case WdfPowerDeviceD3Final:
1097 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3Final\n"));
1098 break;
1099 case WdfPowerDevicePrepareForHibernation:
1100 KdPrint((__DRIVER_NAME " WdfPowerDevicePrepareForHibernation\n"));
1101 break;
1102 default:
1103 KdPrint((__DRIVER_NAME " Unknown WdfPowerDevice state %d\n", previous_state));
1104 break;
1107 status = XenPci_GetBackendAndAddWatch(device);
1108 if (!NT_SUCCESS(status))
1110 WdfDeviceSetFailed(device, WdfDeviceFailedNoRestart);
1111 FUNCTION_EXIT_STATUS(status);
1112 return status;
1115 if (previous_state == WdfPowerDeviceD3 || previous_state == WdfPowerDeviceD3Final)
1117 xppdd->requested_resources_ptr = xppdd->requested_resources_start;
1118 xppdd->assigned_resources_start = xppdd->assigned_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
1119 XenConfig_InitConfigPage(device);
1120 status = XenPci_XenConfigDevice(device);
1122 else if (previous_state == WdfPowerDevicePrepareForHibernation)
1124 PVOID src, dst;
1126 ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1127 src = xppdd->requested_resources_start;
1128 xppdd->requested_resources_ptr = xppdd->requested_resources_start = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
1129 xppdd->assigned_resources_ptr = xppdd->assigned_resources_start;
1131 dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
1133 status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
1135 MmUnmapIoSpace(dst, xppdd->config_page_length);
1136 ExFreePoolWithTag(src, XENPCI_POOL_TAG);
1139 if (!NT_SUCCESS(status))
1141 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
1142 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
1143 WdfDeviceSetFailed(device, WdfDeviceFailedNoRestart);
1144 FUNCTION_EXIT_STATUS(status);
1145 return status;
1148 FUNCTION_EXIT();
1150 return status;
1153 NTSTATUS
1154 XenPciPdo_EvtDeviceD0Exit(WDFDEVICE device, WDF_POWER_DEVICE_STATE target_state)
1156 NTSTATUS status = STATUS_SUCCESS;
1157 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1158 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1159 char path[128];
1161 UNREFERENCED_PARAMETER(device);
1162 UNREFERENCED_PARAMETER(target_state);
1164 FUNCTION_ENTER();
1165 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
1167 switch (target_state)
1169 case WdfPowerDeviceD0:
1170 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1171 break;
1172 case WdfPowerDeviceD1:
1173 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1174 break;
1175 case WdfPowerDeviceD2:
1176 KdPrint((__DRIVER_NAME " WdfPowerDeviceD2\n"));
1177 break;
1178 case WdfPowerDeviceD3:
1179 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3\n"));
1180 if (xppdd->hiber_usage_kludge)
1182 KdPrint((__DRIVER_NAME " (but really WdfPowerDevicePrepareForHibernation)\n"));
1183 target_state = WdfPowerDevicePrepareForHibernation;
1185 break;
1186 case WdfPowerDeviceD3Final:
1187 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3Final\n"));
1188 break;
1189 case WdfPowerDevicePrepareForHibernation:
1190 KdPrint((__DRIVER_NAME " WdfPowerDevicePrepareForHibernation\n"));
1191 break;
1192 default:
1193 KdPrint((__DRIVER_NAME " Unknown WdfPowerDevice state %d\n", target_state));
1194 break;
1197 if (target_state == WdfPowerDevicePrepareForHibernation)
1199 KdPrint((__DRIVER_NAME " not powering down as we are hibernating\n"));
1200 // should we set the backend state here so it's correct on resume???
1202 else
1204 status = XenPci_XenShutdownDevice(device);
1207 /* Remove watch on backend state */
1208 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
1209 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
1211 FUNCTION_EXIT();
1213 return status;
1216 NTSTATUS
1217 XenPciPdo_EvtDevicePrepareHardware(WDFDEVICE device, WDFCMRESLIST resources_raw, WDFCMRESLIST resources_translated)
1219 NTSTATUS status = STATUS_SUCCESS;
1221 UNREFERENCED_PARAMETER(device);
1222 UNREFERENCED_PARAMETER(resources_raw);
1223 UNREFERENCED_PARAMETER(resources_translated);
1225 FUNCTION_ENTER();
1226 FUNCTION_EXIT();
1228 return status;
1231 NTSTATUS
1232 XenPciPdo_EvtDeviceReleaseHardware(WDFDEVICE device, WDFCMRESLIST resources_translated)
1234 NTSTATUS status = STATUS_SUCCESS;
1236 UNREFERENCED_PARAMETER(device);
1237 UNREFERENCED_PARAMETER(resources_translated);
1239 FUNCTION_ENTER();
1240 FUNCTION_EXIT();
1242 return status;
1245 static VOID
1246 XenPciPdo_EvtDeviceUsageNotification(WDFDEVICE device, WDF_SPECIAL_FILE_TYPE notification_type, BOOLEAN is_in_notification_path)
1248 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1250 FUNCTION_ENTER();
1252 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
1253 switch (notification_type)
1255 case WdfSpecialFilePaging:
1256 KdPrint((__DRIVER_NAME " notification_type = Paging, flag = %d\n", is_in_notification_path));
1257 break;
1258 case WdfSpecialFileHibernation:
1259 xppdd->hiber_usage_kludge = is_in_notification_path;
1260 KdPrint((__DRIVER_NAME " notification_type = Hibernation, flag = %d\n", is_in_notification_path));
1261 break;
1262 case WdfSpecialFileDump:
1263 KdPrint((__DRIVER_NAME " notification_type = Dump, flag = %d\n", is_in_notification_path));
1264 break;
1265 default:
1266 KdPrint((__DRIVER_NAME " notification_type = %d, flag = %d\n", notification_type, is_in_notification_path));
1267 break;
1270 FUNCTION_EXIT();
1273 static VOID
1274 XenPci_EvtDevicePnpStateChange(WDFDEVICE device, PCWDF_DEVICE_PNP_NOTIFICATION_DATA notification_data)
1276 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1278 //FUNCTION_ENTER();
1280 if (xppdd->backend_initiated_remove
1281 && notification_data->Type == StateNotificationEnterState
1282 && notification_data->Data.EnterState.CurrentState == WdfDevStatePnpQueryRemovePending
1283 && notification_data->Data.EnterState.NewState == WdfDevStatePnpQueryCanceled)
1285 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1287 KdPrint((__DRIVER_NAME " Eject failed, doing surprise removal\n"));
1288 xppdd->do_not_enumerate = TRUE;
1289 XenPci_EvtChildListScanForChildren(xpdd->child_list);
1292 //FUNCTION_EXIT();
1294 //return STATUS_SUCCESS;
1297 NTSTATUS
1298 XenPci_EvtChildListCreateDevice(WDFCHILDLIST child_list,
1299 PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER identification_header,
1300 PWDFDEVICE_INIT child_init)
1302 NTSTATUS status = STATUS_SUCCESS;
1303 WDF_OBJECT_ATTRIBUTES child_attributes;
1304 WDFDEVICE child_device;
1305 PXENPCI_PDO_IDENTIFICATION_DESCRIPTION identification = (PXENPCI_PDO_IDENTIFICATION_DESCRIPTION)identification_header;
1306 WDF_DEVICE_PNP_CAPABILITIES child_pnp_capabilities;
1307 DECLARE_UNICODE_STRING_SIZE(buffer, 512);
1308 DECLARE_CONST_UNICODE_STRING(location, L"Xen Bus");
1309 PXENPCI_PDO_DEVICE_DATA xppdd;
1310 PXENPCI_DEVICE_DATA xpdd = GetXpdd(WdfChildListGetDevice(child_list));
1311 WDF_PDO_EVENT_CALLBACKS pdo_callbacks;
1312 WDF_PNPPOWER_EVENT_CALLBACKS child_pnp_power_callbacks;
1313 UCHAR pnp_minor_functions[] = { IRP_MN_START_DEVICE };
1314 WDF_DEVICE_POWER_CAPABILITIES child_power_capabilities;
1316 FUNCTION_ENTER();
1318 WdfDeviceInitSetDeviceType(child_init, FILE_DEVICE_UNKNOWN);
1320 WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&child_pnp_power_callbacks);
1321 child_pnp_power_callbacks.EvtDeviceD0Entry = XenPciPdo_EvtDeviceD0Entry;
1322 //child_pnp_power_callbacks.EvtDeviceD0EntryPostInterruptsEnabled = XenPciPdo_EvtDeviceD0EntryPostInterruptsEnabled;
1323 child_pnp_power_callbacks.EvtDeviceD0Exit = XenPciPdo_EvtDeviceD0Exit;
1324 //child_pnp_power_callbacks.EvtDeviceD0ExitPreInterruptsDisabled = XenPciPdo_EvtDeviceD0ExitPreInterruptsDisabled;
1325 child_pnp_power_callbacks.EvtDevicePrepareHardware = XenPciPdo_EvtDevicePrepareHardware;
1326 child_pnp_power_callbacks.EvtDeviceReleaseHardware = XenPciPdo_EvtDeviceReleaseHardware;
1327 child_pnp_power_callbacks.EvtDeviceUsageNotification = XenPciPdo_EvtDeviceUsageNotification;
1328 WdfDeviceInitSetPnpPowerEventCallbacks(child_init, &child_pnp_power_callbacks);
1330 KdPrint((__DRIVER_NAME " device = '%s', index = '%d', path = '%s'\n",
1331 identification->device, identification->index, identification->path));
1333 status = WdfDeviceInitAssignWdmIrpPreprocessCallback(child_init, XenPciPdo_EvtDeviceWdmIrpPreprocess_START_DEVICE,
1334 IRP_MJ_PNP, pnp_minor_functions, ARRAY_SIZE(pnp_minor_functions));
1335 if (!NT_SUCCESS(status))
1337 return status;
1340 WDF_PDO_EVENT_CALLBACKS_INIT(&pdo_callbacks);
1341 //pdo_callbacks.EvtDeviceResourcesQuery = XenPciPdo_EvtDeviceResourcesQuery;
1342 pdo_callbacks.EvtDeviceResourceRequirementsQuery = XenPciPdo_EvtDeviceResourceRequirementsQuery;
1343 //pdo_callbacks.EvtDeviceEject = XenPciPdo_EvtDeviceEject;
1344 //pdo_callbacks.EvtDeviceSetLock = XenPciPdo_EvtDeviceSetLock;
1345 WdfPdoInitSetEventCallbacks(child_init, &pdo_callbacks);
1347 RtlUnicodeStringPrintf(&buffer, L"xen\\%S", identification->device);
1348 status = WdfPdoInitAssignDeviceID(child_init, &buffer);
1349 if (!NT_SUCCESS(status))
1351 return status;
1353 status = WdfPdoInitAddHardwareID(child_init, &buffer);
1354 if (!NT_SUCCESS(status))
1356 return status;
1358 status = WdfPdoInitAddCompatibleID(child_init, &buffer);
1359 if (!NT_SUCCESS(status))
1361 return status;
1364 RtlUnicodeStringPrintf(&buffer, L"%02d", identification->index);
1365 status = WdfPdoInitAssignInstanceID(child_init, &buffer);
1366 if (!NT_SUCCESS(status))
1368 return status;
1371 RtlUnicodeStringPrintf(&buffer, L"Xen %S device #%d", identification->device, identification->index);
1372 status = WdfPdoInitAddDeviceText(child_init, &buffer, &location, 0x0409);
1373 if (!NT_SUCCESS(status))
1375 return status;
1377 WdfPdoInitSetDefaultLocale(child_init, 0x0409);
1379 WdfDeviceInitSetPowerNotPageable(child_init);
1381 WdfDeviceInitRegisterPnpStateChangeCallback(child_init, WdfDevStatePnpQueryCanceled, XenPci_EvtDevicePnpStateChange, StateNotificationEnterState);
1383 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&child_attributes, XENPCI_PDO_DEVICE_DATA);
1384 status = WdfDeviceCreate(&child_init, &child_attributes, &child_device);
1385 if (!NT_SUCCESS(status))
1387 return status;
1390 xppdd = GetXppdd(child_device);
1392 xppdd->wdf_device = child_device;
1393 xppdd->wdf_device_bus_fdo = WdfChildListGetDevice(child_list);
1395 xppdd->config_page_mdl = AllocateUncachedPage();
1397 xppdd->device_state.magic = XEN_DEVICE_STATE_MAGIC;
1398 xppdd->device_state.length = sizeof(XENPCI_DEVICE_STATE);
1399 xppdd->device_state.suspend_resume_state_pdo = SR_STATE_RUNNING;
1400 xppdd->device_state.suspend_resume_state_fdo = SR_STATE_RUNNING;
1401 xppdd->device_state.pdo_event_channel = xpdd->pdo_event_channel;
1402 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFilePaging, TRUE);
1403 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFileHibernation, TRUE);
1404 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFileDump, TRUE);
1406 WDF_DEVICE_PNP_CAPABILITIES_INIT(&child_pnp_capabilities);
1407 child_pnp_capabilities.LockSupported = WdfFalse;
1408 child_pnp_capabilities.EjectSupported = WdfTrue;
1409 child_pnp_capabilities.Removable = WdfTrue;
1410 child_pnp_capabilities.DockDevice = WdfFalse;
1411 child_pnp_capabilities.UniqueID = WdfFalse;
1412 child_pnp_capabilities.SilentInstall = WdfTrue;
1413 child_pnp_capabilities.SurpriseRemovalOK = WdfTrue;
1414 child_pnp_capabilities.HardwareDisabled = WdfFalse;
1415 WdfDeviceSetPnpCapabilities(child_device, &child_pnp_capabilities);
1417 WDF_DEVICE_POWER_CAPABILITIES_INIT(&child_power_capabilities);
1418 child_power_capabilities.DeviceD1 = WdfTrue;
1419 child_power_capabilities.WakeFromD1 = WdfTrue;
1420 child_power_capabilities.DeviceWake = PowerDeviceD1;
1421 child_power_capabilities.DeviceState[PowerSystemWorking] = PowerDeviceD0;
1422 child_power_capabilities.DeviceState[PowerSystemSleeping1] = PowerDeviceD1;
1423 child_power_capabilities.DeviceState[PowerSystemSleeping2] = PowerDeviceD2;
1424 child_power_capabilities.DeviceState[PowerSystemSleeping3] = PowerDeviceD2;
1425 child_power_capabilities.DeviceState[PowerSystemHibernate] = PowerDeviceD3;
1426 child_power_capabilities.DeviceState[PowerSystemShutdown] = PowerDeviceD3;
1427 WdfDeviceSetPowerCapabilities(child_device, &child_power_capabilities);
1429 RtlStringCbCopyA(xppdd->path, ARRAY_SIZE(xppdd->path), identification->path);
1430 RtlStringCbCopyA(xppdd->device, ARRAY_SIZE(xppdd->device), identification->device);
1431 xppdd->index = identification->index;
1432 KeInitializeEvent(&xppdd->backend_state_event, SynchronizationEvent, FALSE);
1433 ExInitializeFastMutex(&xppdd->backend_state_mutex);
1434 xppdd->backend_state = XenbusStateUnknown;
1435 xppdd->frontend_state = XenbusStateUnknown;
1436 xppdd->backend_path[0] = '\0';
1438 FUNCTION_EXIT();
1440 return status;
1443 static __forceinline VOID
1444 XenPci_Pdo_ChangeSuspendState(WDFDEVICE device, ULONG new_state)
1446 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1447 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1449 FUNCTION_ENTER();
1450 KdPrint((__DRIVER_NAME " setting pdo state to %d\n", new_state));
1451 xppdd->device_state.suspend_resume_state_pdo = new_state;
1452 KeMemoryBarrier();
1453 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xpdd->pdo_event_channel));
1454 EvtChn_Notify(xpdd, xpdd->pdo_event_channel);
1455 while(xppdd->device_state.suspend_resume_state_fdo != xppdd->device_state.suspend_resume_state_pdo)
1457 KdPrint((__DRIVER_NAME " waiting...\n"));
1458 KeWaitForSingleObject(&xpdd->pdo_suspend_event, Executive, KernelMode, FALSE, NULL);
1460 KdPrint((__DRIVER_NAME " fdo state set to %d\n", new_state));
1461 FUNCTION_EXIT();
1464 /* called at PASSIVE_LEVEL */
1465 NTSTATUS
1466 XenPci_Pdo_Suspend(WDFDEVICE device)
1468 NTSTATUS status = STATUS_SUCCESS;
1469 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1470 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1471 char path[128];
1472 PUCHAR in_ptr;
1473 UCHAR type;
1474 PVOID setting;
1475 PVOID value;
1476 PVOID value2;
1477 int i;
1479 KdPrint((__DRIVER_NAME " --> " __FUNCTION__ " (%s)\n", xppdd->path));
1481 if (xppdd->backend_state == XenbusStateConnected)
1483 xppdd->restart_on_resume = TRUE;
1484 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_SUSPENDING);
1486 XenPci_ChangeFrontendState(device, XenbusStateClosing, XenbusStateClosing, 30000);
1487 XenPci_ChangeFrontendState(device, XenbusStateClosed, XenbusStateClosed, 30000);
1488 XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 30000);
1490 if (xppdd->assigned_resources_start != NULL)
1492 in_ptr = xppdd->assigned_resources_ptr;
1493 ADD_XEN_INIT_RSP(&in_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1494 in_ptr = xppdd->assigned_resources_start;
1495 while((type = GET_XEN_INIT_RSP(&in_ptr, &setting, &value, &value2)) != XEN_INIT_TYPE_END)
1497 switch (type)
1499 case XEN_INIT_TYPE_RING: /* frontend ring */
1500 FreePages(value);
1501 break;
1502 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
1503 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
1504 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
1505 EvtChn_Unbind(xpdd, PtrToUlong(value));
1506 EvtChn_Close(xpdd, PtrToUlong(value));
1507 break;
1508 case XEN_INIT_TYPE_GRANT_ENTRIES:
1509 for (i = 0; i < (int)PtrToUlong(value); i++)
1510 GntTbl_EndAccess(xpdd, ((grant_ref_t *)value2)[i], FALSE, PtrToUlong(setting));
1511 break;
1516 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
1517 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
1519 else
1521 xppdd->restart_on_resume = FALSE;
1524 FUNCTION_EXIT();
1526 return status;
1529 NTSTATUS
1530 XenPci_Pdo_Resume(WDFDEVICE device)
1532 NTSTATUS status = STATUS_SUCCESS;
1533 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1534 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1535 ULONG old_backend_state;
1536 PUCHAR src, dst;
1538 FUNCTION_ENTER();
1539 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
1541 xppdd->device_state.pdo_event_channel = xpdd->pdo_event_channel;
1542 old_backend_state = xppdd->backend_state;
1544 if (xppdd->restart_on_resume)
1546 status = XenPci_GetBackendAndAddWatch(device);
1548 if (XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 30000) != STATUS_SUCCESS)
1550 KdPrint((__DRIVER_NAME " Failed to change frontend state to Initialising\n"));
1551 // this is probably an unrecoverable situation...
1552 FUNCTION_EXIT();
1553 return STATUS_UNSUCCESSFUL;
1555 if (xppdd->assigned_resources_ptr)
1557 // reset things - feed the 'requested resources' back in
1558 ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1559 src = xppdd->requested_resources_start;
1560 xppdd->requested_resources_ptr = xppdd->requested_resources_start = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);;
1561 xppdd->assigned_resources_ptr = xppdd->assigned_resources_start;
1563 dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
1565 status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
1567 MmUnmapIoSpace(dst, xppdd->config_page_length);
1568 ExFreePoolWithTag(src, XENPCI_POOL_TAG);
1570 if (XenPci_ChangeFrontendState(device, XenbusStateConnected, XenbusStateConnected, 30000) != STATUS_SUCCESS)
1572 // this is definitely an unrecoverable situation...
1573 KdPrint((__DRIVER_NAME " Failed to change frontend state to connected\n"));
1574 FUNCTION_EXIT();
1575 return STATUS_UNSUCCESSFUL;
1577 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_RESUMING);
1578 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_RUNNING);
1581 FUNCTION_EXIT();
1583 return STATUS_SUCCESS;