win-pvdrivers

annotate xenpci/xenpci_pdo.c @ 790:467005e7f509

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