win-pvdrivers

annotate xenpci/xenpci_pdo.c @ 927:ca7b237a61b1

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