win-pvdrivers

view xenpci/xenpci_pdo.c @ 1001:c21dd04d3ae6

Less noise for xennet6
author James Harper <james.harper@bendigoit.com.au>
date Fri Dec 14 21:26:22 2012 +1100 (2012-12-14)
parents 941699790045
children e9011a1ba5bb a5d1d333e0e2
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 /* Get backend id */
198 RtlStringCbPrintfA(path, ARRAY_SIZE(path),
199 "%s/backend-id", xppdd->path);
200 res = XenBus_Read(xpdd, XBT_NIL, path, &value);
201 if (res) {
202 KdPrint((__DRIVER_NAME " Failed to read backend id\n"));
203 XenPci_FreeMem(res);
204 return STATUS_UNSUCCESSFUL;
205 }
206 xppdd->backend_id = (domid_t)atoi(value);
207 XenPci_FreeMem(value);
209 /* Add watch on backend state */
210 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
211 XenBus_AddWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
213 FUNCTION_EXIT();
214 return STATUS_SUCCESS;
215 }
217 static NTSTATUS
218 XenConfig_InitConfigPage(WDFDEVICE device)
219 {
220 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
221 //PXENCONFIG_DEVICE_DATA xcdd = (PXENCONFIG_DEVICE_DATA)device_object->DeviceExtension;
222 //PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
223 //PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
224 PUCHAR ptr;
225 PDEVICE_OBJECT curr, prev;
226 PDRIVER_OBJECT fdo_driver_object;
227 PUCHAR fdo_driver_extension;
229 FUNCTION_ENTER();
231 ptr = MmGetMdlVirtualAddress(xppdd->config_page_mdl);
232 curr = IoGetAttachedDeviceReference(WdfDeviceWdmGetDeviceObject(device));
233 //curr = WdfDeviceWdmGetAttachedDevice(device);
234 while (curr != NULL)
235 {
236 fdo_driver_object = curr->DriverObject;
237 KdPrint((__DRIVER_NAME " fdo_driver_object = %p\n", fdo_driver_object));
238 if (fdo_driver_object)
239 {
240 fdo_driver_extension = IoGetDriverObjectExtension(fdo_driver_object, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC));
241 KdPrint((__DRIVER_NAME " fdo_driver_extension = %p\n", fdo_driver_extension));
242 if (fdo_driver_extension)
243 {
244 memcpy(ptr, fdo_driver_extension, PAGE_SIZE);
245 ObDereferenceObject(curr);
246 break;
247 }
248 }
249 prev = curr;
250 curr = IoGetLowerDeviceObject(curr);
251 ObDereferenceObject(prev);
252 }
254 FUNCTION_EXIT();
256 return STATUS_SUCCESS;
257 }
259 static NTSTATUS
260 XenPci_EvtChn_Bind(PVOID context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
261 {
262 WDFDEVICE device = context;
263 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
264 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
266 return EvtChn_Bind(xpdd, Port, ServiceRoutine, ServiceContext, EVT_ACTION_FLAGS_DEFAULT);
267 }
269 static NTSTATUS
270 XenPci_EvtChn_BindDpc(PVOID context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
271 {
272 WDFDEVICE device = context;
273 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
274 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
276 return EvtChn_BindDpc(xpdd, Port, ServiceRoutine, ServiceContext, EVT_ACTION_FLAGS_DEFAULT);
277 }
279 static NTSTATUS
280 XenPci_EvtChn_Unbind(PVOID context, evtchn_port_t Port)
281 {
282 WDFDEVICE device = context;
283 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
284 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
286 return EvtChn_Unbind(xpdd, Port);
287 }
289 static NTSTATUS
290 XenPci_EvtChn_Mask(PVOID context, evtchn_port_t Port)
291 {
292 WDFDEVICE device = context;
293 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
294 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
296 return EvtChn_Mask(xpdd, Port);
297 }
299 static NTSTATUS
300 XenPci_EvtChn_Unmask(PVOID context, evtchn_port_t Port)
301 {
302 WDFDEVICE device = context;
303 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
304 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
306 return EvtChn_Unmask(xpdd, Port);
307 }
309 static NTSTATUS
310 XenPci_EvtChn_Notify(PVOID context, evtchn_port_t Port)
311 {
312 WDFDEVICE device = context;
313 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
314 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
316 return EvtChn_Notify(xpdd, Port);
317 }
319 static BOOLEAN
320 XenPci_EvtChn_AckEvent(PVOID context, evtchn_port_t port, BOOLEAN *last_interrupt)
321 {
322 WDFDEVICE device = context;
323 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
324 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
326 return EvtChn_AckEvent(xpdd, port, last_interrupt);
327 }
329 typedef struct {
330 PXEN_EVTCHN_SYNC_ROUTINE sync_routine;
331 PVOID sync_context;
332 } sync_context_t;
334 static BOOLEAN
335 XenPci_EvtChn_Sync_Routine(WDFINTERRUPT interrupt, WDFCONTEXT context)
336 {
337 sync_context_t *wdf_sync_context = context;
338 UNREFERENCED_PARAMETER(interrupt);
339 return wdf_sync_context->sync_routine(wdf_sync_context->sync_context);
340 }
342 static BOOLEAN
343 XenPci_EvtChn_Sync(PVOID context, PXEN_EVTCHN_SYNC_ROUTINE sync_routine, PVOID sync_context)
344 {
345 WDFDEVICE device = context;
346 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
347 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
348 sync_context_t wdf_sync_context;
350 wdf_sync_context.sync_routine = sync_routine;
351 wdf_sync_context.sync_context = sync_context;
353 return WdfInterruptSynchronize(xpdd->interrupt, XenPci_EvtChn_Sync_Routine, &wdf_sync_context);
354 }
356 static grant_ref_t
357 XenPci_GntTbl_GrantAccess(PVOID context, uint32_t frame, int readonly, grant_ref_t ref, ULONG tag)
358 {
359 WDFDEVICE device = context;
360 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
361 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
363 return GntTbl_GrantAccess(xpdd, xppdd->backend_id, frame, readonly, ref, tag);
364 }
366 static BOOLEAN
367 XenPci_GntTbl_EndAccess(PVOID context, grant_ref_t ref, BOOLEAN keepref, ULONG tag)
368 {
369 WDFDEVICE device = context;
370 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
371 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
373 return GntTbl_EndAccess(xpdd, ref, keepref, tag);
374 }
376 static VOID
377 XenPci_GntTbl_PutRef(PVOID context, grant_ref_t ref, ULONG tag)
378 {
379 WDFDEVICE device = context;
380 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
381 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
383 GntTbl_PutRef(xpdd, ref, tag);
384 }
386 static grant_ref_t
387 XenPci_GntTbl_GetRef(PVOID context, ULONG tag)
388 {
389 WDFDEVICE device = context;
390 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
391 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
393 return GntTbl_GetRef(xpdd, tag);
394 }
396 PCHAR
397 XenPci_XenBus_Read(PVOID context, xenbus_transaction_t xbt, char *path, char **value)
398 {
399 WDFDEVICE device = context;
400 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
401 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
402 return XenBus_Read(xpdd, xbt, path, value);
403 }
405 PCHAR
406 XenPci_XenBus_Write(PVOID context, xenbus_transaction_t xbt, char *path, char *value)
407 {
408 WDFDEVICE device = context;
409 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
410 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
411 return XenBus_Write(xpdd, xbt, path, value);
412 }
414 PCHAR
415 XenPci_XenBus_Printf(PVOID context, xenbus_transaction_t xbt, char *path, char *fmt, ...)
416 {
417 //PXENPCI_PDO_DEVICE_DATA xppdd = Context;
418 //PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
419 //return XenBus_Printf(xpdd, xbt, path, value);
420 UNREFERENCED_PARAMETER(context);
421 UNREFERENCED_PARAMETER(xbt);
422 UNREFERENCED_PARAMETER(path);
423 UNREFERENCED_PARAMETER(fmt);
424 return NULL;
425 }
427 PCHAR
428 XenPci_XenBus_StartTransaction(PVOID context, xenbus_transaction_t *xbt)
429 {
430 WDFDEVICE device = context;
431 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
432 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
433 return XenBus_StartTransaction(xpdd, xbt);
434 }
436 PCHAR
437 XenPci_XenBus_EndTransaction(PVOID context, xenbus_transaction_t xbt, int abort, int *retry)
438 {
439 WDFDEVICE device = context;
440 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
441 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
442 return XenBus_EndTransaction(xpdd, xbt, abort, retry);
443 }
445 PCHAR
446 XenPci_XenBus_List(PVOID context, xenbus_transaction_t xbt, char *prefix, char ***contents)
447 {
448 WDFDEVICE device = context;
449 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
450 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
451 return XenBus_List(xpdd, xbt, prefix, contents);
452 }
454 PCHAR
455 XenPci_XenBus_AddWatch(PVOID context, xenbus_transaction_t xbt, char *path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
456 {
457 WDFDEVICE device = context;
458 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
459 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
460 PCHAR retval;
462 FUNCTION_ENTER();
463 retval = XenBus_AddWatch(xpdd, xbt, path, ServiceRoutine, ServiceContext);
464 if (retval == NULL)
465 {
466 KdPrint((__DRIVER_NAME " XenPci_XenBus_AddWatch - %s = NULL\n", path));
467 }
468 else
469 {
470 KdPrint((__DRIVER_NAME " XenPci_XenBus_AddWatch - %s = %s\n", path, retval));
471 }
472 FUNCTION_EXIT();
473 return retval;
474 }
476 PCHAR
477 XenPci_XenBus_RemWatch(PVOID context, xenbus_transaction_t xbt, char *path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
478 {
479 WDFDEVICE device = context;
480 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
481 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
482 return XenBus_RemWatch(xpdd, xbt, path, ServiceRoutine, ServiceContext);
483 }
485 /*
486 Called at PASSIVE_LEVEL
487 Called during restore
488 */
490 static NTSTATUS
491 XenPci_ChangeFrontendState(WDFDEVICE device, ULONG frontend_state_set, ULONG backend_state_response, ULONG maximum_wait_ms)
492 {
493 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
494 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
495 LARGE_INTEGER timeout;
496 ULONG remaining;
497 ULONG thiswait;
498 char path[128];
500 FUNCTION_ENTER();
502 xppdd->frontend_state = frontend_state_set;
504 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->path);
505 XenBus_Printf(xpdd, XBT_NIL, path, "%d", frontend_state_set);
507 remaining = maximum_wait_ms;
509 while (xppdd->backend_state != backend_state_response)
510 {
511 thiswait = min((LONG)remaining, 1000); // 1 second or remaining time, whichever is less
512 timeout.QuadPart = (LONGLONG)-1 * thiswait * 1000 * 10;
513 if (KeWaitForSingleObject(&xppdd->backend_state_event, Executive, KernelMode, FALSE, &timeout) == STATUS_TIMEOUT)
514 {
515 /* it's possible that the workitems are blocked because the pagefile isn't available. Lets just re-read the backend value for now */
516 XenPci_UpdateBackendState(device);
517 remaining -= thiswait;
518 if (remaining == 0)
519 {
520 KdPrint((__DRIVER_NAME " Timed out waiting for %d!\n", backend_state_response));
521 return STATUS_UNSUCCESSFUL;
522 }
523 KdPrint((__DRIVER_NAME " Still waiting for %d (currently %d)...\n", backend_state_response, xppdd->backend_state));
524 }
525 }
526 FUNCTION_EXIT();
527 return STATUS_SUCCESS;
528 }
530 static NTSTATUS
531 XenPci_ChangeFrontendStateMap(WDFDEVICE device, PXENPCI_STATE_MAP_ELEMENT map)
532 {
533 NTSTATUS status;
535 FUNCTION_ENTER();
536 while(map->front_target)
537 {
538 //KdPrint((__DRIVER_NAME " Changing state to %d expecting %d\n", (ULONG)map->front_target, (ULONG)map->back_expected));
539 if (!NT_SUCCESS(status = XenPci_ChangeFrontendState(device, (ULONG)map->front_target, (ULONG)map->back_expected, (ULONG)map->wait * 100)))
540 {
541 FUNCTION_EXIT();
542 return status;
543 }
544 map++;
545 }
546 FUNCTION_EXIT();
547 return STATUS_SUCCESS;
548 }
550 static NTSTATUS
551 XenPci_XenConfigDevice(WDFDEVICE device);
553 static NTSTATUS
554 XenPci_XenShutdownDevice(PVOID context)
555 {
556 WDFDEVICE device = context;
557 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
558 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
559 PUCHAR in_ptr;
560 ULONG i;
561 UCHAR type;
562 PVOID setting;
563 PVOID value;
564 PVOID value2;
566 FUNCTION_ENTER();
568 if (xppdd->backend_state == XenbusStateConnected)
569 {
570 XenPci_ChangeFrontendStateMap(device, xppdd->xb_shutdown_map);
571 }
572 else if (xppdd->backend_state == XenbusStateClosing)
573 {
574 XenPci_ChangeFrontendState(device, XenbusStateClosed, XenbusStateClosed, 30000);
575 }
577 if (xppdd->assigned_resources_start != NULL)
578 {
579 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
580 in_ptr = xppdd->assigned_resources_start;
581 while((type = GET_XEN_INIT_RSP(&in_ptr, &setting, &value, &value2)) != XEN_INIT_TYPE_END)
582 {
583 switch (type)
584 {
585 case XEN_INIT_TYPE_RING: /* frontend ring */
586 FreePages(value);
587 break;
588 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
589 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
590 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
591 EvtChn_Unbind(xpdd, PtrToUlong(value));
592 EvtChn_Close(xpdd, PtrToUlong(value));
593 break;
594 case XEN_INIT_TYPE_GRANT_ENTRIES:
595 for (i = 0; i < PtrToUlong(value); i++)
596 GntTbl_EndAccess(xpdd, ((grant_ref_t *)value2)[i], FALSE, PtrToUlong(setting));
597 break;
598 }
599 }
600 ExFreePoolWithTag(xppdd->assigned_resources_start, XENPCI_POOL_TAG);
601 xppdd->assigned_resources_start = NULL;
602 }
604 FUNCTION_EXIT();
606 return STATUS_SUCCESS;
607 }
609 struct dummy_sring {
610 RING_IDX req_prod, req_event;
611 RING_IDX rsp_prod, rsp_event;
612 uint8_t pad[48];
613 };
615 static NTSTATUS
616 XenPci_XenConfigDeviceSpecifyBuffers(WDFDEVICE device, PUCHAR src, PUCHAR dst)
617 {
618 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
619 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
620 NTSTATUS status = STATUS_SUCCESS;
621 ULONG i;
622 char path[128];
623 PCHAR setting, value;
624 PCHAR res;
625 PVOID address;
626 UCHAR type;
627 PUCHAR in_ptr;
628 PUCHAR out_ptr;
629 XENPCI_VECTORS vectors;
630 ULONG event_channel;
631 PMDL ring;
632 grant_ref_t gref;
633 PVOID value2;
634 int map_no;
636 FUNCTION_ENTER();
638 xppdd->xb_pre_connect_map[0].front_target = 0;
639 xppdd->xb_post_connect_map[0].front_target = 0;
640 xppdd->xb_shutdown_map[0].front_target = 0;
642 in_ptr = src;
643 out_ptr = dst;
645 // always add vectors
646 vectors.magic = XEN_DATA_MAGIC;
647 vectors.length = sizeof(XENPCI_VECTORS);
648 vectors.context = device;
649 vectors.EvtChn_Bind = XenPci_EvtChn_Bind;
650 vectors.EvtChn_BindDpc = XenPci_EvtChn_BindDpc;
651 vectors.EvtChn_Unbind = XenPci_EvtChn_Unbind;
652 vectors.EvtChn_Mask = XenPci_EvtChn_Mask;
653 vectors.EvtChn_Unmask = XenPci_EvtChn_Unmask;
654 vectors.EvtChn_Notify = XenPci_EvtChn_Notify;
655 vectors.EvtChn_AckEvent = XenPci_EvtChn_AckEvent;
656 vectors.EvtChn_Sync = XenPci_EvtChn_Sync;
657 vectors.GntTbl_GetRef = XenPci_GntTbl_GetRef;
658 vectors.GntTbl_PutRef = XenPci_GntTbl_PutRef;
659 vectors.GntTbl_GrantAccess = XenPci_GntTbl_GrantAccess;
660 vectors.GntTbl_EndAccess = XenPci_GntTbl_EndAccess;
661 vectors.XenPci_XenConfigDevice = XenPci_XenConfigDevice;
662 vectors.XenPci_XenShutdownDevice = XenPci_XenShutdownDevice;
663 RtlStringCbCopyA(vectors.path, 128, xppdd->path);
664 RtlStringCbCopyA(vectors.backend_path, 128, xppdd->backend_path);
665 //vectors.pdo_event_channel = xpdd->pdo_event_channel;
666 vectors.XenBus_Read = XenPci_XenBus_Read;
667 vectors.XenBus_Write = XenPci_XenBus_Write;
668 vectors.XenBus_Printf = XenPci_XenBus_Printf;
669 vectors.XenBus_StartTransaction = XenPci_XenBus_StartTransaction;
670 vectors.XenBus_EndTransaction = XenPci_XenBus_EndTransaction;
671 vectors.XenBus_List = XenPci_XenBus_List;
672 vectors.XenBus_AddWatch = XenPci_XenBus_AddWatch;
673 vectors.XenBus_RemWatch = XenPci_XenBus_RemWatch;
675 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_PROTOCOL_VERSION, NULL, UlongToPtr(qemu_protocol_version), NULL);
677 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_VECTORS, NULL, &vectors, NULL);
678 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_STATE_PTR, NULL, &xppdd->device_state, NULL);
680 // need to make sure we never get here while backend state is connected or closing...
681 // first pass, possibly before state == Connected
682 in_ptr = src;
683 while((type = GET_XEN_INIT_REQ(&in_ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
684 {
685 ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, type, setting, value, value2);
687 switch (type)
688 {
689 case XEN_INIT_TYPE_WRITE_STRING: /* frontend setting = value */
690 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_WRITE_STRING - %s = %s\n", setting, value));
691 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
692 XenBus_Printf(xpdd, XBT_NIL, path, "%s", value);
693 break;
694 case XEN_INIT_TYPE_RING: /* frontend ring */
695 /* we only allocate and do the SHARED_RING_INIT here */
696 if ((ring = AllocatePage()) != 0)
697 {
698 address = MmGetMdlVirtualAddress(ring);
699 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %p\n", setting, address));
700 SHARED_RING_INIT((struct dummy_sring *)address);
701 if ((gref = GntTbl_GrantAccess(
702 xpdd, xppdd->backend_id, (ULONG)*MmGetMdlPfnArray(ring), FALSE, INVALID_GRANT_REF, (ULONG)'XPDO')) != INVALID_GRANT_REF)
703 {
704 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
705 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %d\n", setting, gref));
706 XenBus_Printf(xpdd, XBT_NIL, path, "%d", gref);
707 ADD_XEN_INIT_RSP(&out_ptr, type, setting, address, NULL);
708 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, type, setting, ring, NULL);
709 // add the grant entry too so it gets freed automatically
710 __ADD_XEN_INIT_UCHAR(&xppdd->assigned_resources_ptr, XEN_INIT_TYPE_GRANT_ENTRIES);
711 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, (ULONG)'XPDO');
712 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, 1);
713 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, gref);
714 }
715 else
716 {
717 FreePages(ring);
718 status = STATUS_UNSUCCESSFUL;
719 goto error;
720 }
721 }
722 else
723 {
724 status = STATUS_UNSUCCESSFUL;
725 goto error;
726 }
727 break;
728 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
729 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
730 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
731 if ((event_channel = EvtChn_AllocUnbound(xpdd, xppdd->backend_id)) != 0)
732 {
733 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_EVENT_CHANNEL - %s = %d\n", setting, event_channel));
734 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
735 XenBus_Printf(xpdd, XBT_NIL, path, "%d", event_channel);
736 ADD_XEN_INIT_RSP(&out_ptr, type, setting, UlongToPtr(event_channel), NULL);
737 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, type, setting, UlongToPtr(event_channel), NULL);
738 if (type == XEN_INIT_TYPE_EVENT_CHANNEL_IRQ)
739 {
740 EvtChn_BindIrq(xpdd, event_channel, xppdd->irq_vector, path, EVT_ACTION_FLAGS_DEFAULT);
741 }
742 else if (type == XEN_INIT_TYPE_EVENT_CHANNEL_DPC)
743 {
744 #pragma warning(suppress:4055)
745 EvtChn_BindDpc(xpdd, event_channel, (PXEN_EVTCHN_SERVICE_ROUTINE)value, value2, EVT_ACTION_FLAGS_DEFAULT);
746 }
747 else
748 {
749 #pragma warning(suppress:4055)
750 EvtChn_Bind(xpdd, event_channel, (PXEN_EVTCHN_SERVICE_ROUTINE)value, value2, EVT_ACTION_FLAGS_DEFAULT);
751 }
752 }
753 else
754 {
755 status = STATUS_UNSUCCESSFUL;
756 goto error;
757 }
758 break;
759 case XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT:
760 map_no = 0;
761 while ((xppdd->xb_pre_connect_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
762 {
763 xppdd->xb_pre_connect_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
764 xppdd->xb_pre_connect_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
765 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].front_target);
766 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].back_expected);
767 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].wait);
768 map_no++;
769 }
770 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
771 break;
772 case XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT:
773 map_no = 0;
774 while ((xppdd->xb_post_connect_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
775 {
776 xppdd->xb_post_connect_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
777 xppdd->xb_post_connect_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
778 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].front_target);
779 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].back_expected);
780 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].wait);
781 map_no++;
782 }
783 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
784 break;
785 case XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN:
786 map_no = 0;
787 while ((xppdd->xb_shutdown_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
788 {
789 xppdd->xb_shutdown_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
790 xppdd->xb_shutdown_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
791 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].front_target);
792 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].back_expected);
793 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].wait);
794 map_no++;
795 }
796 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
797 break;
798 }
799 }
800 if (!NT_SUCCESS(status))
801 {
802 goto error;
803 }
805 if (XenPci_ChangeFrontendStateMap(device, xppdd->xb_pre_connect_map) != STATUS_SUCCESS)
806 {
807 status = STATUS_UNSUCCESSFUL;
808 goto error;
809 }
811 // second pass, possibly after state == Connected
812 in_ptr = src;
813 while((type = GET_XEN_INIT_REQ(&in_ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
814 {
815 switch(type)
816 {
817 case XEN_INIT_TYPE_READ_STRING_BACK:
818 case XEN_INIT_TYPE_READ_STRING_FRONT:
819 if (type == XEN_INIT_TYPE_READ_STRING_FRONT)
820 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
821 else
822 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->backend_path, setting);
823 res = XenBus_Read(xpdd, XBT_NIL, path, &value);
824 if (res)
825 {
826 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = <failed>\n", setting));
827 XenPci_FreeMem(res);
828 ADD_XEN_INIT_RSP(&out_ptr, type, setting, "", "");
829 }
830 else
831 {
832 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = %s\n", setting, value));
833 ADD_XEN_INIT_RSP(&out_ptr, type, setting, value, value2);
834 XenPci_FreeMem(value);
835 }
836 break;
837 case XEN_INIT_TYPE_VECTORS:
838 // this is always done so ignore the request
839 break;
840 case XEN_INIT_TYPE_GRANT_ENTRIES:
841 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_GRANT_ENTRIES - %d\n", PtrToUlong(value)));
842 __ADD_XEN_INIT_UCHAR(&out_ptr, type);
843 __ADD_XEN_INIT_UCHAR(&xppdd->assigned_resources_ptr, type);
844 __ADD_XEN_INIT_ULONG(&out_ptr, PtrToUlong(setting));
845 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, PtrToUlong(setting));
846 __ADD_XEN_INIT_ULONG(&out_ptr, PtrToUlong(value));
847 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, PtrToUlong(value));
848 for (i = 0; i < PtrToUlong(value); i++)
849 {
850 gref = GntTbl_GetRef(xpdd, PtrToUlong(setting));
851 __ADD_XEN_INIT_ULONG(&out_ptr, gref);
852 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, gref);
853 }
854 break;
855 /* just need to eat these */
856 case XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT:
857 case XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT:
858 case XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN:
859 while ((__GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
860 {
861 __GET_XEN_INIT_UCHAR(&in_ptr);
862 __GET_XEN_INIT_UCHAR(&in_ptr);
863 }
864 break;
865 }
866 }
867 if (qemu_hide_flags_value)
868 {
869 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_HIDE_FLAGS, NULL, UlongToPtr(qemu_hide_flags_value), NULL);
870 }
871 else
872 {
873 DECLARE_UNICODE_STRING_SIZE(my_device_name, 128);
875 RtlUnicodeStringPrintf(&my_device_name, L"#%S#", xppdd->device);
877 for (i = 0; i < WdfCollectionGetCount(qemu_hide_devices); i++)
878 {
879 WDFSTRING wdf_string = WdfCollectionGetItem(qemu_hide_devices, i);
880 UNICODE_STRING hide_device_name;
882 WdfStringGetUnicodeString(wdf_string, &hide_device_name);
884 if (RtlCompareUnicodeString(&hide_device_name, &my_device_name, TRUE) != 0)
885 {
886 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_HIDE_FILTER, NULL, NULL, NULL);
887 break;
888 }
889 }
890 }
892 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
894 if (XenPci_ChangeFrontendStateMap(device, xppdd->xb_post_connect_map) != STATUS_SUCCESS)
895 {
896 status = STATUS_UNSUCCESSFUL;
897 goto error;
898 }
899 FUNCTION_EXIT();
900 return status;
902 error:
903 KdPrint((__DRIVER_NAME " Error\n"));
904 XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 2000);
905 FUNCTION_EXIT_STATUS(status);
906 return status;
907 }
909 static NTSTATUS
910 XenPci_XenConfigDevice(WDFDEVICE device)
911 {
912 NTSTATUS status;
913 PUCHAR src, dst;
914 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
916 src = ExAllocatePoolWithTag(NonPagedPool, xppdd->config_page_length, XENPCI_POOL_TAG);
917 dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
918 memcpy(src, dst, xppdd->config_page_length);
920 status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
922 MmUnmapIoSpace(dst, xppdd->config_page_length);
923 ExFreePoolWithTag(src, XENPCI_POOL_TAG);
925 return status;
926 }
928 static NTSTATUS
929 XenPciPdo_EvtDeviceWdmIrpPreprocess_START_DEVICE(WDFDEVICE device, PIRP irp)
930 {
931 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
932 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
933 PIO_STACK_LOCATION stack;
934 PCM_PARTIAL_RESOURCE_LIST prl;
935 PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
936 ULONG i;
937 //char path[128];
938 //PMDL mdl;
940 FUNCTION_ENTER();
941 KdPrint((__DRIVER_NAME " %s\n", xppdd->path));
943 stack = IoGetCurrentIrpStackLocation(irp);
945 prl = &stack->Parameters.StartDevice.AllocatedResources->List[0].PartialResourceList;
946 for (i = 0; i < prl->Count; i++)
947 {
948 prd = & prl->PartialDescriptors[i];
949 switch (prd->Type)
950 {
951 case CmResourceTypeMemory:
952 if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart && prd->u.Memory.Length == 0)
953 {
954 prd->u.Memory.Start.QuadPart = MmGetMdlPfnArray(xppdd->config_page_mdl)[0] << PAGE_SHIFT;
955 prd->u.Memory.Length = MmGetMdlByteCount(xppdd->config_page_mdl);
956 }
957 else if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart + 1 && prd->u.Memory.Length == 0)
958 {
959 RtlZeroMemory(prd, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
960 prd->Type = CmResourceTypeInterrupt;
961 prd->ShareDisposition = CmResourceShareShared;
962 prd->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
963 prd->u.Interrupt.Level = 0; // Set group and level to zero (group = upper word)
964 prd->u.Interrupt.Level = xpdd->irq_number & 0xffff; // Only set the lower word
965 prd->u.Interrupt.Vector = xpdd->irq_number;
966 prd->u.Interrupt.Affinity = xpdd->irq_affinity; //KeQueryActiveProcessors();
967 xppdd->irq_number = xpdd->irq_number;
968 }
969 break;
970 }
971 }
973 prl = &stack->Parameters.StartDevice.AllocatedResourcesTranslated->List[0].PartialResourceList;
974 for (i = 0; i < prl->Count; i++)
975 {
976 prd = & prl->PartialDescriptors[i];
977 switch (prd->Type)
978 {
979 case CmResourceTypeMemory:
980 KdPrint((__DRIVER_NAME " CmResourceTypeMemory (%d)\n", i));
981 KdPrint((__DRIVER_NAME " Start = %08x, Length = %d\n", prd->u.Memory.Start.LowPart, prd->u.Memory.Length));
982 if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart)
983 {
984 if (prd->u.Memory.Length == 0)
985 {
986 KdPrint((__DRIVER_NAME " pfn[0] = %08x\n", (ULONG)MmGetMdlPfnArray(xppdd->config_page_mdl)[0]));
987 prd->u.Memory.Start.QuadPart = (ULONGLONG)MmGetMdlPfnArray(xppdd->config_page_mdl)[0] << PAGE_SHIFT;
988 prd->u.Memory.Length = MmGetMdlByteCount(xppdd->config_page_mdl);
989 KdPrint((__DRIVER_NAME " New Start = %08x%08x, Length = %d\n", prd->u.Memory.Start.HighPart, prd->u.Memory.Start.LowPart, prd->u.Memory.Length));
990 }
991 xppdd->config_page_phys = prd->u.Memory.Start;
992 xppdd->config_page_length = prd->u.Memory.Length;
993 xppdd->requested_resources_start = xppdd->requested_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
994 //xppdd->assigned_resources_start = xppdd->assigned_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
996 #if 0
997 status = XenPci_XenConfigDevice(device);
998 if (!NT_SUCCESS(status))
999 {
1000 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
1001 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
1002 FUNCTION_EXIT_STATUS(status);
1003 return status;
1005 #endif
1007 else if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart + 1 && prd->u.Memory.Length == 0)
1009 RtlZeroMemory(prd, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
1010 prd->Type = CmResourceTypeInterrupt;
1011 prd->ShareDisposition = CmResourceShareShared;
1012 prd->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
1013 prd->u.Interrupt.Level = 0; // Set group and level to zero (group = upper word)
1014 prd->u.Interrupt.Level = xpdd->irq_level & 0xffff; // Only set the lower word
1015 prd->u.Interrupt.Vector = xpdd->irq_vector;
1016 prd->u.Interrupt.Affinity = xpdd->irq_affinity; //KeQueryActiveProcessors();
1017 xppdd->irq_vector = xpdd->irq_vector;
1018 xppdd->irq_level = xpdd->irq_level;
1020 break;
1024 IoSkipCurrentIrpStackLocation(irp);
1026 FUNCTION_EXIT();
1028 return WdfDeviceWdmDispatchPreprocessedIrp(device, irp);
1031 #if 0
1032 static NTSTATUS
1033 XenPciPdo_EvtDeviceResourcesQuery(WDFDEVICE device, WDFCMRESLIST resources)
1036 #endif
1038 static NTSTATUS
1039 XenPciPdo_EvtDeviceResourceRequirementsQuery(WDFDEVICE device, WDFIORESREQLIST requirements_list)
1041 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1042 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1043 WDFIORESLIST res_list;
1044 IO_RESOURCE_DESCRIPTOR ird;
1046 //FUNCTION_ENTER();
1048 WdfIoResourceRequirementsListSetInterfaceType(requirements_list, PNPBus);
1050 WdfIoResourceListCreate(requirements_list, WDF_NO_OBJECT_ATTRIBUTES, &res_list);
1051 ird.Option = 0;
1052 ird.Type = CmResourceTypeMemory;
1053 ird.ShareDisposition = CmResourceShareShared;
1054 ird.Flags = CM_RESOURCE_MEMORY_READ_WRITE | CM_RESOURCE_MEMORY_CACHEABLE;
1055 ird.u.Memory.MinimumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart;
1056 ird.u.Memory.MaximumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart;
1057 ird.u.Memory.Length = 0;
1058 ird.u.Memory.Alignment = 1; //PAGE_SIZE;
1059 WdfIoResourceListAppendDescriptor(res_list, &ird);
1061 ird.Option = 0;
1062 ird.Type = CmResourceTypeMemory;
1063 ird.ShareDisposition = CmResourceShareShared;
1064 ird.Flags = CM_RESOURCE_MEMORY_READ_WRITE | CM_RESOURCE_MEMORY_CACHEABLE;
1065 ird.u.Memory.MinimumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart + 1;
1066 ird.u.Memory.MaximumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart + 1;
1067 ird.u.Memory.Length = 0;
1068 ird.u.Memory.Alignment = 1; //PAGE_SIZE;
1069 WdfIoResourceListAppendDescriptor(res_list, &ird);
1071 WdfIoResourceRequirementsListAppendIoResList(requirements_list, res_list);
1073 //FUNCTION_EXIT();
1075 return STATUS_SUCCESS;
1078 NTSTATUS
1079 XenPciPdo_EvtDeviceD0Entry(WDFDEVICE device, WDF_POWER_DEVICE_STATE previous_state)
1081 NTSTATUS status = STATUS_SUCCESS;
1082 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1083 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1084 CHAR path[128];
1086 FUNCTION_ENTER();
1087 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
1089 switch (previous_state)
1091 case WdfPowerDeviceD0:
1092 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1093 break;
1094 case WdfPowerDeviceD1:
1095 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1096 break;
1097 case WdfPowerDeviceD2:
1098 KdPrint((__DRIVER_NAME " WdfPowerDeviceD2\n"));
1099 break;
1100 case WdfPowerDeviceD3:
1101 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3\n"));
1102 if (xppdd->hiber_usage_kludge)
1104 KdPrint((__DRIVER_NAME " (but really WdfPowerDevicePrepareForHibernation)\n"));
1105 previous_state = WdfPowerDevicePrepareForHibernation;
1107 break;
1108 case WdfPowerDeviceD3Final:
1109 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3Final\n"));
1110 break;
1111 case WdfPowerDevicePrepareForHibernation:
1112 KdPrint((__DRIVER_NAME " WdfPowerDevicePrepareForHibernation\n"));
1113 break;
1114 default:
1115 KdPrint((__DRIVER_NAME " Unknown WdfPowerDevice state %d\n", previous_state));
1116 break;
1119 status = XenPci_GetBackendAndAddWatch(device);
1120 if (!NT_SUCCESS(status))
1122 WdfDeviceSetFailed(device, WdfDeviceFailedNoRestart);
1123 FUNCTION_EXIT_STATUS(status);
1124 return status;
1127 if (previous_state == WdfPowerDeviceD3 || previous_state == WdfPowerDeviceD3Final)
1129 xppdd->requested_resources_ptr = xppdd->requested_resources_start;
1130 xppdd->assigned_resources_start = xppdd->assigned_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
1131 XenConfig_InitConfigPage(device);
1132 status = XenPci_XenConfigDevice(device);
1134 else if (previous_state == WdfPowerDevicePrepareForHibernation)
1136 PVOID src, dst;
1138 ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1139 src = xppdd->requested_resources_start;
1140 xppdd->requested_resources_ptr = xppdd->requested_resources_start = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
1141 xppdd->assigned_resources_ptr = xppdd->assigned_resources_start;
1143 dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
1145 status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
1147 MmUnmapIoSpace(dst, xppdd->config_page_length);
1148 ExFreePoolWithTag(src, XENPCI_POOL_TAG);
1151 if (!NT_SUCCESS(status))
1153 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
1154 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
1155 WdfDeviceSetFailed(device, WdfDeviceFailedNoRestart);
1156 FUNCTION_EXIT_STATUS(status);
1157 return status;
1160 FUNCTION_EXIT();
1162 return status;
1165 NTSTATUS
1166 XenPciPdo_EvtDeviceD0Exit(WDFDEVICE device, WDF_POWER_DEVICE_STATE target_state)
1168 NTSTATUS status = STATUS_SUCCESS;
1169 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1170 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1171 char path[128];
1173 UNREFERENCED_PARAMETER(device);
1174 UNREFERENCED_PARAMETER(target_state);
1176 FUNCTION_ENTER();
1177 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
1179 switch (target_state)
1181 case WdfPowerDeviceD0:
1182 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1183 break;
1184 case WdfPowerDeviceD1:
1185 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
1186 break;
1187 case WdfPowerDeviceD2:
1188 KdPrint((__DRIVER_NAME " WdfPowerDeviceD2\n"));
1189 break;
1190 case WdfPowerDeviceD3:
1191 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3\n"));
1192 if (xppdd->hiber_usage_kludge)
1194 KdPrint((__DRIVER_NAME " (but really WdfPowerDevicePrepareForHibernation)\n"));
1195 target_state = WdfPowerDevicePrepareForHibernation;
1197 break;
1198 case WdfPowerDeviceD3Final:
1199 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3Final\n"));
1200 break;
1201 case WdfPowerDevicePrepareForHibernation:
1202 KdPrint((__DRIVER_NAME " WdfPowerDevicePrepareForHibernation\n"));
1203 break;
1204 default:
1205 KdPrint((__DRIVER_NAME " Unknown WdfPowerDevice state %d\n", target_state));
1206 break;
1209 if (target_state == WdfPowerDevicePrepareForHibernation)
1211 KdPrint((__DRIVER_NAME " not powering down as we are hibernating\n"));
1212 // should we set the backend state here so it's correct on resume???
1214 else
1216 status = XenPci_XenShutdownDevice(device);
1219 /* Remove watch on backend state */
1220 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
1221 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
1223 FUNCTION_EXIT();
1225 return status;
1228 NTSTATUS
1229 XenPciPdo_EvtDevicePrepareHardware(WDFDEVICE device, WDFCMRESLIST resources_raw, WDFCMRESLIST resources_translated)
1231 NTSTATUS status = STATUS_SUCCESS;
1233 UNREFERENCED_PARAMETER(device);
1234 UNREFERENCED_PARAMETER(resources_raw);
1235 UNREFERENCED_PARAMETER(resources_translated);
1237 FUNCTION_ENTER();
1238 FUNCTION_EXIT();
1240 return status;
1243 NTSTATUS
1244 XenPciPdo_EvtDeviceReleaseHardware(WDFDEVICE device, WDFCMRESLIST resources_translated)
1246 NTSTATUS status = STATUS_SUCCESS;
1248 UNREFERENCED_PARAMETER(device);
1249 UNREFERENCED_PARAMETER(resources_translated);
1251 FUNCTION_ENTER();
1252 FUNCTION_EXIT();
1254 return status;
1257 static VOID
1258 XenPciPdo_EvtDeviceUsageNotification(WDFDEVICE device, WDF_SPECIAL_FILE_TYPE notification_type, BOOLEAN is_in_notification_path)
1260 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1262 FUNCTION_ENTER();
1264 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
1265 switch (notification_type)
1267 case WdfSpecialFilePaging:
1268 KdPrint((__DRIVER_NAME " notification_type = Paging, flag = %d\n", is_in_notification_path));
1269 break;
1270 case WdfSpecialFileHibernation:
1271 xppdd->hiber_usage_kludge = is_in_notification_path;
1272 KdPrint((__DRIVER_NAME " notification_type = Hibernation, flag = %d\n", is_in_notification_path));
1273 break;
1274 case WdfSpecialFileDump:
1275 KdPrint((__DRIVER_NAME " notification_type = Dump, flag = %d\n", is_in_notification_path));
1276 break;
1277 default:
1278 KdPrint((__DRIVER_NAME " notification_type = %d, flag = %d\n", notification_type, is_in_notification_path));
1279 break;
1282 FUNCTION_EXIT();
1285 static VOID
1286 XenPci_EvtDevicePnpStateChange(WDFDEVICE device, PCWDF_DEVICE_PNP_NOTIFICATION_DATA notification_data)
1288 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1290 //FUNCTION_ENTER();
1292 if (xppdd->backend_initiated_remove
1293 && notification_data->Type == StateNotificationEnterState
1294 && notification_data->Data.EnterState.CurrentState == WdfDevStatePnpQueryRemovePending
1295 && notification_data->Data.EnterState.NewState == WdfDevStatePnpQueryCanceled)
1297 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1299 KdPrint((__DRIVER_NAME " Eject failed, doing surprise removal\n"));
1300 xppdd->do_not_enumerate = TRUE;
1301 XenPci_EvtChildListScanForChildren(xpdd->child_list);
1304 //FUNCTION_EXIT();
1306 //return STATUS_SUCCESS;
1309 NTSTATUS
1310 XenPci_EvtChildListCreateDevice(WDFCHILDLIST child_list,
1311 PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER identification_header,
1312 PWDFDEVICE_INIT child_init)
1314 NTSTATUS status = STATUS_SUCCESS;
1315 WDF_OBJECT_ATTRIBUTES child_attributes;
1316 WDFDEVICE child_device;
1317 PXENPCI_PDO_IDENTIFICATION_DESCRIPTION identification = (PXENPCI_PDO_IDENTIFICATION_DESCRIPTION)identification_header;
1318 WDF_DEVICE_PNP_CAPABILITIES child_pnp_capabilities;
1319 DECLARE_UNICODE_STRING_SIZE(buffer, 512);
1320 DECLARE_CONST_UNICODE_STRING(location, L"Xen Bus");
1321 PXENPCI_PDO_DEVICE_DATA xppdd;
1322 PXENPCI_DEVICE_DATA xpdd = GetXpdd(WdfChildListGetDevice(child_list));
1323 WDF_PDO_EVENT_CALLBACKS pdo_callbacks;
1324 WDF_PNPPOWER_EVENT_CALLBACKS child_pnp_power_callbacks;
1325 UCHAR pnp_minor_functions[] = { IRP_MN_START_DEVICE };
1326 WDF_DEVICE_POWER_CAPABILITIES child_power_capabilities;
1328 FUNCTION_ENTER();
1330 WdfDeviceInitSetDeviceType(child_init, FILE_DEVICE_UNKNOWN);
1332 WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&child_pnp_power_callbacks);
1333 child_pnp_power_callbacks.EvtDeviceD0Entry = XenPciPdo_EvtDeviceD0Entry;
1334 //child_pnp_power_callbacks.EvtDeviceD0EntryPostInterruptsEnabled = XenPciPdo_EvtDeviceD0EntryPostInterruptsEnabled;
1335 child_pnp_power_callbacks.EvtDeviceD0Exit = XenPciPdo_EvtDeviceD0Exit;
1336 //child_pnp_power_callbacks.EvtDeviceD0ExitPreInterruptsDisabled = XenPciPdo_EvtDeviceD0ExitPreInterruptsDisabled;
1337 child_pnp_power_callbacks.EvtDevicePrepareHardware = XenPciPdo_EvtDevicePrepareHardware;
1338 child_pnp_power_callbacks.EvtDeviceReleaseHardware = XenPciPdo_EvtDeviceReleaseHardware;
1339 child_pnp_power_callbacks.EvtDeviceUsageNotification = XenPciPdo_EvtDeviceUsageNotification;
1340 WdfDeviceInitSetPnpPowerEventCallbacks(child_init, &child_pnp_power_callbacks);
1342 KdPrint((__DRIVER_NAME " device = '%s', index = '%d', path = '%s'\n",
1343 identification->device, identification->index, identification->path));
1345 status = WdfDeviceInitAssignWdmIrpPreprocessCallback(child_init, XenPciPdo_EvtDeviceWdmIrpPreprocess_START_DEVICE,
1346 IRP_MJ_PNP, pnp_minor_functions, ARRAY_SIZE(pnp_minor_functions));
1347 if (!NT_SUCCESS(status))
1349 return status;
1352 WDF_PDO_EVENT_CALLBACKS_INIT(&pdo_callbacks);
1353 //pdo_callbacks.EvtDeviceResourcesQuery = XenPciPdo_EvtDeviceResourcesQuery;
1354 pdo_callbacks.EvtDeviceResourceRequirementsQuery = XenPciPdo_EvtDeviceResourceRequirementsQuery;
1355 //pdo_callbacks.EvtDeviceEject = XenPciPdo_EvtDeviceEject;
1356 //pdo_callbacks.EvtDeviceSetLock = XenPciPdo_EvtDeviceSetLock;
1357 WdfPdoInitSetEventCallbacks(child_init, &pdo_callbacks);
1359 RtlUnicodeStringPrintf(&buffer, L"xen\\%S", identification->device);
1360 status = WdfPdoInitAssignDeviceID(child_init, &buffer);
1361 if (!NT_SUCCESS(status))
1363 return status;
1365 status = WdfPdoInitAddHardwareID(child_init, &buffer);
1366 if (!NT_SUCCESS(status))
1368 return status;
1370 status = WdfPdoInitAddCompatibleID(child_init, &buffer);
1371 if (!NT_SUCCESS(status))
1373 return status;
1376 RtlUnicodeStringPrintf(&buffer, L"%02d", identification->index);
1377 status = WdfPdoInitAssignInstanceID(child_init, &buffer);
1378 if (!NT_SUCCESS(status))
1380 return status;
1383 RtlUnicodeStringPrintf(&buffer, L"Xen %S device #%d", identification->device, identification->index);
1384 status = WdfPdoInitAddDeviceText(child_init, &buffer, &location, 0x0409);
1385 if (!NT_SUCCESS(status))
1387 return status;
1389 WdfPdoInitSetDefaultLocale(child_init, 0x0409);
1391 WdfDeviceInitSetPowerNotPageable(child_init);
1393 WdfDeviceInitRegisterPnpStateChangeCallback(child_init, WdfDevStatePnpQueryCanceled, XenPci_EvtDevicePnpStateChange, StateNotificationEnterState);
1395 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&child_attributes, XENPCI_PDO_DEVICE_DATA);
1396 status = WdfDeviceCreate(&child_init, &child_attributes, &child_device);
1397 if (!NT_SUCCESS(status))
1399 return status;
1402 xppdd = GetXppdd(child_device);
1404 xppdd->wdf_device = child_device;
1405 xppdd->wdf_device_bus_fdo = WdfChildListGetDevice(child_list);
1407 xppdd->config_page_mdl = AllocateUncachedPage();
1409 xppdd->device_state.magic = XEN_DEVICE_STATE_MAGIC;
1410 xppdd->device_state.length = sizeof(XENPCI_DEVICE_STATE);
1411 xppdd->device_state.suspend_resume_state_pdo = SR_STATE_RUNNING;
1412 xppdd->device_state.suspend_resume_state_fdo = SR_STATE_RUNNING;
1413 xppdd->device_state.pdo_event_channel = xpdd->pdo_event_channel;
1414 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFilePaging, TRUE);
1415 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFileHibernation, TRUE);
1416 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFileDump, TRUE);
1418 WDF_DEVICE_PNP_CAPABILITIES_INIT(&child_pnp_capabilities);
1419 child_pnp_capabilities.LockSupported = WdfFalse;
1420 child_pnp_capabilities.EjectSupported = WdfTrue;
1421 child_pnp_capabilities.Removable = WdfTrue;
1422 child_pnp_capabilities.DockDevice = WdfFalse;
1423 child_pnp_capabilities.UniqueID = WdfFalse;
1424 child_pnp_capabilities.SilentInstall = WdfTrue;
1425 child_pnp_capabilities.SurpriseRemovalOK = WdfTrue;
1426 child_pnp_capabilities.HardwareDisabled = WdfFalse;
1427 WdfDeviceSetPnpCapabilities(child_device, &child_pnp_capabilities);
1429 WDF_DEVICE_POWER_CAPABILITIES_INIT(&child_power_capabilities);
1430 child_power_capabilities.DeviceD1 = WdfTrue;
1431 child_power_capabilities.WakeFromD1 = WdfTrue;
1432 child_power_capabilities.DeviceWake = PowerDeviceD1;
1433 child_power_capabilities.DeviceState[PowerSystemWorking] = PowerDeviceD0;
1434 child_power_capabilities.DeviceState[PowerSystemSleeping1] = PowerDeviceD1;
1435 child_power_capabilities.DeviceState[PowerSystemSleeping2] = PowerDeviceD2;
1436 child_power_capabilities.DeviceState[PowerSystemSleeping3] = PowerDeviceD2;
1437 child_power_capabilities.DeviceState[PowerSystemHibernate] = PowerDeviceD3;
1438 child_power_capabilities.DeviceState[PowerSystemShutdown] = PowerDeviceD3;
1439 WdfDeviceSetPowerCapabilities(child_device, &child_power_capabilities);
1441 RtlStringCbCopyA(xppdd->path, ARRAY_SIZE(xppdd->path), identification->path);
1442 RtlStringCbCopyA(xppdd->device, ARRAY_SIZE(xppdd->device), identification->device);
1443 xppdd->index = identification->index;
1444 KeInitializeEvent(&xppdd->backend_state_event, SynchronizationEvent, FALSE);
1445 ExInitializeFastMutex(&xppdd->backend_state_mutex);
1446 xppdd->backend_state = XenbusStateUnknown;
1447 xppdd->frontend_state = XenbusStateUnknown;
1448 xppdd->backend_path[0] = '\0';
1449 xppdd->backend_id = 0;
1451 FUNCTION_EXIT();
1453 return status;
1456 static __forceinline VOID
1457 XenPci_Pdo_ChangeSuspendState(WDFDEVICE device, ULONG new_state)
1459 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1460 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1462 FUNCTION_ENTER();
1463 KdPrint((__DRIVER_NAME " setting pdo state to %d\n", new_state));
1464 xppdd->device_state.suspend_resume_state_pdo = new_state;
1465 KeMemoryBarrier();
1466 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xpdd->pdo_event_channel));
1467 EvtChn_Notify(xpdd, xpdd->pdo_event_channel);
1468 while(xppdd->device_state.suspend_resume_state_fdo != xppdd->device_state.suspend_resume_state_pdo)
1470 KdPrint((__DRIVER_NAME " waiting...\n"));
1471 KeWaitForSingleObject(&xpdd->pdo_suspend_event, Executive, KernelMode, FALSE, NULL);
1473 KdPrint((__DRIVER_NAME " fdo state set to %d\n", new_state));
1474 FUNCTION_EXIT();
1477 /* called at PASSIVE_LEVEL */
1478 NTSTATUS
1479 XenPci_Pdo_Suspend(WDFDEVICE device)
1481 NTSTATUS status = STATUS_SUCCESS;
1482 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1483 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1484 char path[128];
1485 PUCHAR in_ptr;
1486 UCHAR type;
1487 PVOID setting;
1488 PVOID value;
1489 PVOID value2;
1490 int i;
1492 KdPrint((__DRIVER_NAME " --> " __FUNCTION__ " (%s)\n", xppdd->path));
1494 if (xppdd->backend_state == XenbusStateConnected)
1496 xppdd->restart_on_resume = TRUE;
1497 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_SUSPENDING);
1499 XenPci_ChangeFrontendState(device, XenbusStateClosing, XenbusStateClosing, 30000);
1500 XenPci_ChangeFrontendState(device, XenbusStateClosed, XenbusStateClosed, 30000);
1501 XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 30000);
1503 if (xppdd->assigned_resources_start != NULL)
1505 in_ptr = xppdd->assigned_resources_ptr;
1506 ADD_XEN_INIT_RSP(&in_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1507 in_ptr = xppdd->assigned_resources_start;
1508 while((type = GET_XEN_INIT_RSP(&in_ptr, &setting, &value, &value2)) != XEN_INIT_TYPE_END)
1510 switch (type)
1512 case XEN_INIT_TYPE_RING: /* frontend ring */
1513 FreePages(value);
1514 break;
1515 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
1516 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
1517 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
1518 EvtChn_Unbind(xpdd, PtrToUlong(value));
1519 EvtChn_Close(xpdd, PtrToUlong(value));
1520 break;
1521 case XEN_INIT_TYPE_GRANT_ENTRIES:
1522 for (i = 0; i < (int)PtrToUlong(value); i++)
1523 GntTbl_EndAccess(xpdd, ((grant_ref_t *)value2)[i], FALSE, PtrToUlong(setting));
1524 break;
1529 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
1530 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
1532 else
1534 xppdd->restart_on_resume = FALSE;
1537 FUNCTION_EXIT();
1539 return status;
1542 NTSTATUS
1543 XenPci_Pdo_Resume(WDFDEVICE device)
1545 NTSTATUS status = STATUS_SUCCESS;
1546 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
1547 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
1548 ULONG old_backend_state;
1549 PUCHAR src, dst;
1551 FUNCTION_ENTER();
1552 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
1554 xppdd->device_state.pdo_event_channel = xpdd->pdo_event_channel;
1555 old_backend_state = xppdd->backend_state;
1557 if (xppdd->restart_on_resume)
1559 status = XenPci_GetBackendAndAddWatch(device);
1561 if (XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 30000) != STATUS_SUCCESS)
1563 KdPrint((__DRIVER_NAME " Failed to change frontend state to Initialising\n"));
1564 // this is probably an unrecoverable situation...
1565 FUNCTION_EXIT();
1566 return STATUS_UNSUCCESSFUL;
1568 if (xppdd->assigned_resources_ptr)
1570 // reset things - feed the 'requested resources' back in
1571 ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1572 src = xppdd->requested_resources_start;
1573 xppdd->requested_resources_ptr = xppdd->requested_resources_start = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);;
1574 xppdd->assigned_resources_ptr = xppdd->assigned_resources_start;
1576 dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
1578 status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
1580 MmUnmapIoSpace(dst, xppdd->config_page_length);
1581 ExFreePoolWithTag(src, XENPCI_POOL_TAG);
1583 if (XenPci_ChangeFrontendState(device, XenbusStateConnected, XenbusStateConnected, 30000) != STATUS_SUCCESS)
1585 // this is definitely an unrecoverable situation...
1586 KdPrint((__DRIVER_NAME " Failed to change frontend state to connected\n"));
1587 FUNCTION_EXIT();
1588 return STATUS_UNSUCCESSFUL;
1590 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_RESUMING);
1591 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_RUNNING);
1594 FUNCTION_EXIT();
1596 return STATUS_SUCCESS;