win-pvdrivers

annotate xenpci/xenpci_pdo.c @ 818:4e6f162a054c

Fixes for build under Window 2000
author James Harper <james.harper@bendigoit.com.au>
date Sun Oct 10 10:34:26 2010 +1100 (2010-10-10)
parents 551d17b09bc1
children 4db4fd1c87fb
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@536 254 return EvtChn_Bind(xpdd, Port, ServiceRoutine, ServiceContext);
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@536 264 return EvtChn_BindDpc(xpdd, Port, ServiceRoutine, ServiceContext);
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@536 560 else
james@536 561 {
james@536 562 if (xppdd->backend_state == XenbusStateClosing)
james@536 563 XenPci_ChangeFrontendState(device, XenbusStateClosed, XenbusStateClosed, 30000);
james@536 564 }
james@536 565
james@536 566 if (xppdd->assigned_resources_start != NULL)
james@536 567 {
james@536 568 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
james@536 569 in_ptr = xppdd->assigned_resources_start;
james@536 570 while((type = GET_XEN_INIT_RSP(&in_ptr, &setting, &value, &value2)) != XEN_INIT_TYPE_END)
james@536 571 {
james@536 572 switch (type)
james@536 573 {
james@536 574 case XEN_INIT_TYPE_RING: /* frontend ring */
james@536 575 FreePages(value);
james@536 576 break;
james@536 577 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
james@617 578 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
james@536 579 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
james@536 580 EvtChn_Unbind(xpdd, PtrToUlong(value));
james@536 581 EvtChn_Close(xpdd, PtrToUlong(value));
james@536 582 break;
james@536 583 case XEN_INIT_TYPE_GRANT_ENTRIES:
james@536 584 for (i = 0; i < PtrToUlong(setting); i++)
james@790 585 GntTbl_EndAccess(xpdd, ((grant_ref_t *)value)[i], FALSE, (ULONG)'XPDO');
james@536 586 break;
james@536 587 }
james@536 588 }
james@536 589 ExFreePoolWithTag(xppdd->assigned_resources_start, XENPCI_POOL_TAG);
james@536 590 xppdd->assigned_resources_start = NULL;
james@536 591 }
james@536 592
james@536 593 FUNCTION_EXIT();
james@536 594
james@536 595 return STATUS_SUCCESS;
james@536 596 }
james@536 597
james@536 598 struct dummy_sring {
james@536 599 RING_IDX req_prod, req_event;
james@536 600 RING_IDX rsp_prod, rsp_event;
james@536 601 uint8_t pad[48];
james@536 602 };
james@536 603
james@536 604 static NTSTATUS
james@536 605 XenPci_XenConfigDeviceSpecifyBuffers(WDFDEVICE device, PUCHAR src, PUCHAR dst)
james@536 606 {
james@536 607 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
james@536 608 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
james@536 609 NTSTATUS status = STATUS_SUCCESS;
james@536 610 ULONG i;
james@536 611 char path[128];
james@536 612 PCHAR setting, value;
james@536 613 PCHAR res;
james@536 614 PVOID address;
james@536 615 UCHAR type;
james@551 616 PUCHAR in_ptr;
james@551 617 PUCHAR out_ptr;
james@536 618 XENPCI_VECTORS vectors;
james@536 619 ULONG event_channel;
james@536 620 PMDL ring;
james@536 621 grant_ref_t gref;
james@536 622 PVOID value2;
james@803 623 int map_no;
james@536 624
james@536 625 FUNCTION_ENTER();
james@536 626
james@803 627 xppdd->xb_pre_connect_map[0].front_target = 0;
james@803 628 xppdd->xb_post_connect_map[0].front_target = 0;
james@803 629 xppdd->xb_shutdown_map[0].front_target = 0;
james@803 630
james@536 631 in_ptr = src;
james@536 632 out_ptr = dst;
james@536 633
james@536 634 // always add vectors
james@536 635 vectors.magic = XEN_DATA_MAGIC;
james@536 636 vectors.length = sizeof(XENPCI_VECTORS);
james@536 637 vectors.context = device;
james@536 638 vectors.EvtChn_Bind = XenPci_EvtChn_Bind;
james@536 639 vectors.EvtChn_BindDpc = XenPci_EvtChn_BindDpc;
james@536 640 vectors.EvtChn_Unbind = XenPci_EvtChn_Unbind;
james@536 641 vectors.EvtChn_Mask = XenPci_EvtChn_Mask;
james@536 642 vectors.EvtChn_Unmask = XenPci_EvtChn_Unmask;
james@536 643 vectors.EvtChn_Notify = XenPci_EvtChn_Notify;
james@536 644 vectors.EvtChn_AckEvent = XenPci_EvtChn_AckEvent;
james@536 645 vectors.EvtChn_Sync = XenPci_EvtChn_Sync;
james@536 646 vectors.GntTbl_GetRef = XenPci_GntTbl_GetRef;
james@536 647 vectors.GntTbl_PutRef = XenPci_GntTbl_PutRef;
james@536 648 vectors.GntTbl_GrantAccess = XenPci_GntTbl_GrantAccess;
james@536 649 vectors.GntTbl_EndAccess = XenPci_GntTbl_EndAccess;
james@536 650 vectors.XenPci_XenConfigDevice = XenPci_XenConfigDevice;
james@536 651 vectors.XenPci_XenShutdownDevice = XenPci_XenShutdownDevice;
james@716 652 RtlStringCbCopyA(vectors.path, 128, xppdd->path);
james@716 653 RtlStringCbCopyA(vectors.backend_path, 128, xppdd->backend_path);
james@536 654 //vectors.pdo_event_channel = xpdd->pdo_event_channel;
james@536 655 vectors.XenBus_Read = XenPci_XenBus_Read;
james@536 656 vectors.XenBus_Write = XenPci_XenBus_Write;
james@536 657 vectors.XenBus_Printf = XenPci_XenBus_Printf;
james@536 658 vectors.XenBus_StartTransaction = XenPci_XenBus_StartTransaction;
james@536 659 vectors.XenBus_EndTransaction = XenPci_XenBus_EndTransaction;
james@536 660 vectors.XenBus_List = XenPci_XenBus_List;
james@536 661 vectors.XenBus_AddWatch = XenPci_XenBus_AddWatch;
james@536 662 vectors.XenBus_RemWatch = XenPci_XenBus_RemWatch;
james@536 663
james@536 664 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_PROTOCOL_VERSION, NULL, UlongToPtr(qemu_protocol_version), NULL);
james@536 665
james@536 666 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_VECTORS, NULL, &vectors, NULL);
james@536 667 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_STATE_PTR, NULL, &xppdd->device_state, NULL);
james@551 668
james@803 669 // need to make sure we never get here while backend state is connected or closing...
james@536 670 // first pass, possibly before state == Connected
james@551 671 in_ptr = src;
james@536 672 while((type = GET_XEN_INIT_REQ(&in_ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
james@536 673 {
james@803 674 //KdPrint((__DRIVER_NAME " in_ptr = %p, type = %d\n", in_ptr, type));
james@536 675 ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, type, setting, value, value2);
james@536 676
james@536 677 switch (type)
james@536 678 {
james@536 679 case XEN_INIT_TYPE_WRITE_STRING: /* frontend setting = value */
james@536 680 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_WRITE_STRING - %s = %s\n", setting, value));
james@536 681 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
james@536 682 XenBus_Printf(xpdd, XBT_NIL, path, "%s", value);
james@536 683 break;
james@536 684 case XEN_INIT_TYPE_RING: /* frontend ring */
james@536 685 /* we only allocate and do the SHARED_RING_INIT here */
james@536 686 if ((ring = AllocatePage()) != 0)
james@536 687 {
james@536 688 address = MmGetMdlVirtualAddress(ring);
james@803 689 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %p\n", setting, address));
james@536 690 SHARED_RING_INIT((struct dummy_sring *)address);
james@536 691 if ((gref = GntTbl_GrantAccess(
james@790 692 xpdd, 0, (ULONG)*MmGetMdlPfnArray(ring), FALSE, INVALID_GRANT_REF, (ULONG)'XPDO')) != INVALID_GRANT_REF)
james@536 693 {
james@536 694 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
james@803 695 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %d\n", setting, gref));
james@536 696 XenBus_Printf(xpdd, XBT_NIL, path, "%d", gref);
james@536 697 ADD_XEN_INIT_RSP(&out_ptr, type, setting, address, NULL);
james@536 698 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, type, setting, ring, NULL);
james@536 699 // add the grant entry too so it gets freed automatically
james@536 700 __ADD_XEN_INIT_UCHAR(&xppdd->assigned_resources_ptr, XEN_INIT_TYPE_GRANT_ENTRIES);
james@536 701 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, 1);
james@536 702 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, gref);
james@536 703 }
james@536 704 else
james@536 705 {
james@536 706 FreePages(ring);
james@536 707 status = STATUS_UNSUCCESSFUL;
james@536 708 goto error;
james@536 709 }
james@536 710 }
james@536 711 else
james@536 712 {
james@536 713 status = STATUS_UNSUCCESSFUL;
james@536 714 goto error;
james@536 715 }
james@536 716 break;
james@536 717 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
james@617 718 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
james@536 719 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
james@536 720 if ((event_channel = EvtChn_AllocUnbound(xpdd, 0)) != 0)
james@536 721 {
james@617 722 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_EVENT_CHANNEL - %s = %d\n", setting, event_channel));
james@536 723 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
james@536 724 XenBus_Printf(xpdd, XBT_NIL, path, "%d", event_channel);
james@536 725 ADD_XEN_INIT_RSP(&out_ptr, type, setting, UlongToPtr(event_channel), NULL);
james@536 726 ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, type, setting, UlongToPtr(event_channel), NULL);
james@536 727 if (type == XEN_INIT_TYPE_EVENT_CHANNEL_IRQ)
james@536 728 {
james@536 729 EvtChn_BindIrq(xpdd, event_channel, xppdd->irq_vector, path);
james@536 730 }
james@617 731 else if (type == XEN_INIT_TYPE_EVENT_CHANNEL_DPC)
james@617 732 {
james@617 733 #pragma warning(suppress:4055)
james@617 734 EvtChn_BindDpc(xpdd, event_channel, (PXEN_EVTCHN_SERVICE_ROUTINE)value, value2);
james@617 735 }
james@536 736 else
james@536 737 {
james@537 738 #pragma warning(suppress:4055)
james@536 739 EvtChn_Bind(xpdd, event_channel, (PXEN_EVTCHN_SERVICE_ROUTINE)value, value2);
james@536 740 }
james@536 741 }
james@536 742 else
james@536 743 {
james@536 744 status = STATUS_UNSUCCESSFUL;
james@536 745 goto error;
james@536 746 }
james@536 747 break;
james@803 748 case XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT:
james@803 749 map_no = 0;
james@803 750 while ((xppdd->xb_pre_connect_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
james@803 751 {
james@803 752 xppdd->xb_pre_connect_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
james@803 753 xppdd->xb_pre_connect_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
james@803 754 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].front_target);
james@803 755 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].back_expected);
james@803 756 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].wait);
james@803 757 map_no++;
james@803 758 }
james@803 759 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
james@803 760 break;
james@803 761 case XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT:
james@803 762 map_no = 0;
james@803 763 while ((xppdd->xb_post_connect_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
james@803 764 {
james@803 765 xppdd->xb_post_connect_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
james@803 766 xppdd->xb_post_connect_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
james@803 767 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].front_target);
james@803 768 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].back_expected);
james@803 769 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].wait);
james@803 770 map_no++;
james@803 771 }
james@803 772 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
james@803 773 break;
james@803 774 case XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN:
james@803 775 map_no = 0;
james@803 776 while ((xppdd->xb_shutdown_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
james@803 777 {
james@803 778 xppdd->xb_shutdown_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
james@803 779 xppdd->xb_shutdown_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
james@803 780 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].front_target);
james@803 781 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].back_expected);
james@803 782 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].wait);
james@803 783 map_no++;
james@803 784 }
james@803 785 __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
james@803 786 break;
james@536 787 }
james@536 788 }
james@536 789 if (!NT_SUCCESS(status))
james@536 790 {
james@536 791 goto error;
james@536 792 }
james@803 793
james@803 794 if (XenPci_ChangeFrontendStateMap(device, xppdd->xb_pre_connect_map) != STATUS_SUCCESS)
james@536 795 {
james@803 796 status = STATUS_UNSUCCESSFUL;
james@803 797 goto error;
james@536 798 }
james@536 799
james@536 800 // second pass, possibly after state == Connected
james@536 801 in_ptr = src;
james@536 802 while((type = GET_XEN_INIT_REQ(&in_ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
james@536 803 {
james@536 804 switch(type)
james@536 805 {
james@536 806 case XEN_INIT_TYPE_READ_STRING_BACK:
james@536 807 case XEN_INIT_TYPE_READ_STRING_FRONT:
james@536 808 if (type == XEN_INIT_TYPE_READ_STRING_FRONT)
james@536 809 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
james@536 810 else
james@536 811 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->backend_path, setting);
james@536 812 res = XenBus_Read(xpdd, XBT_NIL, path, &value);
james@536 813 if (res)
james@536 814 {
james@617 815 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = <failed>\n", setting));
james@536 816 XenPci_FreeMem(res);
james@617 817 ADD_XEN_INIT_RSP(&out_ptr, type, setting, "", "");
james@536 818 }
james@536 819 else
james@536 820 {
james@536 821 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = %s\n", setting, value));
james@536 822 ADD_XEN_INIT_RSP(&out_ptr, type, setting, value, value2);
james@536 823 XenPci_FreeMem(value);
james@536 824 }
james@536 825 break;
james@536 826 case XEN_INIT_TYPE_VECTORS:
james@536 827 // this is always done so ignore the request
james@536 828 break;
james@536 829 case XEN_INIT_TYPE_GRANT_ENTRIES:
james@536 830 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_GRANT_ENTRIES - %d\n", PtrToUlong(value)));
james@536 831 __ADD_XEN_INIT_UCHAR(&out_ptr, type);
james@536 832 __ADD_XEN_INIT_UCHAR(&xppdd->assigned_resources_ptr, type);
james@536 833 __ADD_XEN_INIT_ULONG(&out_ptr, PtrToUlong(value));
james@536 834 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, PtrToUlong(value));
james@536 835 for (i = 0; i < PtrToUlong(value); i++)
james@536 836 {
james@790 837 gref = GntTbl_GetRef(xpdd, 'XPDO');
james@536 838 __ADD_XEN_INIT_ULONG(&out_ptr, gref);
james@536 839 __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, gref);
james@536 840 }
james@536 841 break;
james@803 842 /* just need to eat these */
james@803 843 case XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT:
james@803 844 case XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT:
james@803 845 case XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN:
james@803 846 while ((__GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
james@803 847 {
james@803 848 __GET_XEN_INIT_UCHAR(&in_ptr);
james@803 849 __GET_XEN_INIT_UCHAR(&in_ptr);
james@803 850 }
james@803 851 break;
james@536 852 }
james@536 853 }
james@747 854 if (qemu_hide_flags_value)
james@551 855 {
james@747 856 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_HIDE_FLAGS, NULL, UlongToPtr(qemu_hide_flags_value), NULL);
james@551 857 }
james@747 858 else
james@747 859 {
james@747 860 DECLARE_UNICODE_STRING_SIZE(my_device_name, 128);
james@747 861
james@747 862 RtlUnicodeStringPrintf(&my_device_name, L"#%S#", xppdd->device);
james@747 863
james@747 864 for (i = 0; i < WdfCollectionGetCount(qemu_hide_devices); i++)
james@747 865 {
james@747 866 WDFSTRING wdf_string = WdfCollectionGetItem(qemu_hide_devices, i);
james@747 867 UNICODE_STRING hide_device_name;
james@747 868
james@747 869 WdfStringGetUnicodeString(wdf_string, &hide_device_name);
james@747 870
james@747 871 if (RtlCompareUnicodeString(&hide_device_name, &my_device_name, TRUE) != 0)
james@747 872 {
james@747 873 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_HIDE_FILTER, NULL, NULL, NULL);
james@747 874 break;
james@747 875 }
james@747 876 }
james@747 877 }
james@747 878
james@536 879 ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
james@536 880
james@803 881 if (XenPci_ChangeFrontendStateMap(device, xppdd->xb_post_connect_map) != STATUS_SUCCESS)
james@536 882 {
james@803 883 status = STATUS_UNSUCCESSFUL;
james@803 884 goto error;
james@536 885 }
james@536 886 FUNCTION_EXIT();
james@536 887 return status;
james@551 888
james@536 889 error:
james@803 890 KdPrint((__DRIVER_NAME " Error\n"));
james@551 891 XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 2000);
james@536 892 FUNCTION_EXIT_STATUS(status);
james@536 893 return status;
james@536 894 }
james@536 895
james@536 896 static NTSTATUS
james@536 897 XenPci_XenConfigDevice(WDFDEVICE device)
james@536 898 {
james@536 899 NTSTATUS status;
james@536 900 PUCHAR src, dst;
james@536 901 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
james@536 902
james@536 903 src = ExAllocatePoolWithTag(NonPagedPool, xppdd->config_page_length, XENPCI_POOL_TAG);
james@536 904 dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
james@536 905 memcpy(src, dst, xppdd->config_page_length);
james@536 906
james@536 907 status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
james@536 908
james@536 909 MmUnmapIoSpace(dst, xppdd->config_page_length);
james@536 910 ExFreePoolWithTag(src, XENPCI_POOL_TAG);
james@536 911
james@536 912 return status;
james@536 913 }
james@536 914
james@536 915 static NTSTATUS
james@536 916 XenPciPdo_EvtDeviceWdmIrpPreprocess_START_DEVICE(WDFDEVICE device, PIRP irp)
james@536 917 {
james@536 918 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
james@536 919 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
james@536 920 PIO_STACK_LOCATION stack;
james@536 921 PCM_PARTIAL_RESOURCE_LIST prl;
james@536 922 PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
james@536 923 ULONG i;
james@551 924 //char path[128];
james@551 925 //PMDL mdl;
james@536 926
james@536 927 FUNCTION_ENTER();
james@536 928 KdPrint((__DRIVER_NAME " %s\n", xppdd->path));
james@536 929
james@536 930 stack = IoGetCurrentIrpStackLocation(irp);
james@536 931
james@536 932 prl = &stack->Parameters.StartDevice.AllocatedResources->List[0].PartialResourceList;
james@536 933 for (i = 0; i < prl->Count; i++)
james@536 934 {
james@536 935 prd = & prl->PartialDescriptors[i];
james@536 936 switch (prd->Type)
james@536 937 {
james@536 938 case CmResourceTypeMemory:
james@536 939 if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart && prd->u.Memory.Length == 0)
james@536 940 {
james@551 941 prd->u.Memory.Start.QuadPart = MmGetMdlPfnArray(xppdd->config_page_mdl)[0] << PAGE_SHIFT;
james@551 942 prd->u.Memory.Length = MmGetMdlByteCount(xppdd->config_page_mdl);
james@536 943 }
james@536 944 else if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart + 1 && prd->u.Memory.Length == 0)
james@536 945 {
james@536 946 RtlZeroMemory(prd, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
james@536 947 prd->Type = CmResourceTypeInterrupt;
james@536 948 prd->ShareDisposition = CmResourceShareShared;
james@536 949 prd->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
steve@621 950 prd->u.Interrupt.Level = 0; // Set group and level to zero (group = upper word)
steve@621 951 prd->u.Interrupt.Level = xpdd->irq_number & 0xffff; // Only set the lower word
james@536 952 prd->u.Interrupt.Vector = xpdd->irq_number;
james@705 953 prd->u.Interrupt.Affinity = xpdd->irq_affinity; //KeQueryActiveProcessors();
james@536 954 xppdd->irq_number = xpdd->irq_number;
james@536 955 }
james@536 956 break;
james@536 957 }
james@536 958 }
james@536 959
james@536 960 prl = &stack->Parameters.StartDevice.AllocatedResourcesTranslated->List[0].PartialResourceList;
james@536 961 for (i = 0; i < prl->Count; i++)
james@536 962 {
james@536 963 prd = & prl->PartialDescriptors[i];
james@536 964 switch (prd->Type)
james@536 965 {
james@536 966 case CmResourceTypeMemory:
james@536 967 KdPrint((__DRIVER_NAME " CmResourceTypeMemory (%d)\n", i));
james@536 968 KdPrint((__DRIVER_NAME " Start = %08x, Length = %d\n", prd->u.Memory.Start.LowPart, prd->u.Memory.Length));
james@536 969 if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart)
james@536 970 {
james@536 971 if (prd->u.Memory.Length == 0)
james@536 972 {
james@551 973 KdPrint((__DRIVER_NAME " pfn[0] = %08x\n", (ULONG)MmGetMdlPfnArray(xppdd->config_page_mdl)[0]));
james@551 974 prd->u.Memory.Start.QuadPart = (ULONGLONG)MmGetMdlPfnArray(xppdd->config_page_mdl)[0] << PAGE_SHIFT;
james@551 975 prd->u.Memory.Length = MmGetMdlByteCount(xppdd->config_page_mdl);
james@536 976 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 977 }
james@536 978 xppdd->config_page_phys = prd->u.Memory.Start;
james@536 979 xppdd->config_page_length = prd->u.Memory.Length;
james@536 980 xppdd->requested_resources_start = xppdd->requested_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
james@634 981 //xppdd->assigned_resources_start = xppdd->assigned_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
james@536 982
james@551 983 #if 0
james@536 984 status = XenPci_XenConfigDevice(device);
james@536 985 if (!NT_SUCCESS(status))
james@536 986 {
james@536 987 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
james@624 988 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
james@702 989 FUNCTION_EXIT_STATUS(status);
james@536 990 return status;
james@536 991 }
james@551 992 #endif
james@536 993 }
james@536 994 else if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart + 1 && prd->u.Memory.Length == 0)
james@536 995 {
james@536 996 RtlZeroMemory(prd, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
james@536 997 prd->Type = CmResourceTypeInterrupt;
james@536 998 prd->ShareDisposition = CmResourceShareShared;
james@536 999 prd->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
steve@621 1000 prd->u.Interrupt.Level = 0; // Set group and level to zero (group = upper word)
steve@621 1001 prd->u.Interrupt.Level = xpdd->irq_level & 0xffff; // Only set the lower word
james@536 1002 prd->u.Interrupt.Vector = xpdd->irq_vector;
james@705 1003 prd->u.Interrupt.Affinity = xpdd->irq_affinity; //KeQueryActiveProcessors();
james@536 1004 xppdd->irq_vector = xpdd->irq_vector;
james@536 1005 xppdd->irq_level = xpdd->irq_level;
james@536 1006 }
james@536 1007 break;
james@536 1008 }
james@536 1009 }
james@536 1010
james@536 1011 IoSkipCurrentIrpStackLocation(irp);
james@536 1012
james@536 1013 FUNCTION_EXIT();
james@536 1014
james@536 1015 return WdfDeviceWdmDispatchPreprocessedIrp(device, irp);
james@536 1016 }
james@536 1017
james@536 1018 #if 0
james@536 1019 static NTSTATUS
james@536 1020 XenPciPdo_EvtDeviceResourcesQuery(WDFDEVICE device, WDFCMRESLIST resources)
james@536 1021 {
james@536 1022 }
james@536 1023 #endif
james@536 1024
james@536 1025 static NTSTATUS
james@536 1026 XenPciPdo_EvtDeviceResourceRequirementsQuery(WDFDEVICE device, WDFIORESREQLIST requirements_list)
james@536 1027 {
james@536 1028 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
james@536 1029 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
james@536 1030 WDFIORESLIST res_list;
james@536 1031 IO_RESOURCE_DESCRIPTOR ird;
james@536 1032
james@538 1033 //FUNCTION_ENTER();
james@536 1034
james@536 1035 WdfIoResourceRequirementsListSetInterfaceType(requirements_list, PNPBus);
james@536 1036
james@536 1037 WdfIoResourceListCreate(requirements_list, WDF_NO_OBJECT_ATTRIBUTES, &res_list);
james@536 1038 ird.Option = 0;
james@536 1039 ird.Type = CmResourceTypeMemory;
james@536 1040 ird.ShareDisposition = CmResourceShareShared;
james@536 1041 ird.Flags = CM_RESOURCE_MEMORY_READ_WRITE | CM_RESOURCE_MEMORY_CACHEABLE;
james@536 1042 ird.u.Memory.MinimumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart;
james@536 1043 ird.u.Memory.MaximumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart;
james@536 1044 ird.u.Memory.Length = 0;
james@536 1045 ird.u.Memory.Alignment = 1; //PAGE_SIZE;
james@536 1046 WdfIoResourceListAppendDescriptor(res_list, &ird);
james@536 1047
james@536 1048 ird.Option = 0;
james@536 1049 ird.Type = CmResourceTypeMemory;
james@536 1050 ird.ShareDisposition = CmResourceShareShared;
james@536 1051 ird.Flags = CM_RESOURCE_MEMORY_READ_WRITE | CM_RESOURCE_MEMORY_CACHEABLE;
james@536 1052 ird.u.Memory.MinimumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart + 1;
james@536 1053 ird.u.Memory.MaximumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart + 1;
james@536 1054 ird.u.Memory.Length = 0;
james@536 1055 ird.u.Memory.Alignment = 1; //PAGE_SIZE;
james@536 1056 WdfIoResourceListAppendDescriptor(res_list, &ird);
james@536 1057
james@536 1058 WdfIoResourceRequirementsListAppendIoResList(requirements_list, res_list);
james@536 1059
james@538 1060 //FUNCTION_EXIT();
james@538 1061
james@538 1062 return STATUS_SUCCESS;
james@538 1063 }
james@538 1064
james@538 1065 NTSTATUS
james@538 1066 XenPciPdo_EvtDeviceD0Entry(WDFDEVICE device, WDF_POWER_DEVICE_STATE previous_state)
james@538 1067 {
james@538 1068 NTSTATUS status = STATUS_SUCCESS;
james@551 1069 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
james@551 1070 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
james@551 1071 CHAR path[128];
james@538 1072
james@538 1073 FUNCTION_ENTER();
james@812 1074 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
james@538 1075
james@538 1076 switch (previous_state)
james@538 1077 {
james@538 1078 case WdfPowerDeviceD0:
james@538 1079 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
james@538 1080 break;
james@538 1081 case WdfPowerDeviceD1:
james@538 1082 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
james@538 1083 break;
james@538 1084 case WdfPowerDeviceD2:
james@538 1085 KdPrint((__DRIVER_NAME " WdfPowerDeviceD2\n"));
james@538 1086 break;
james@538 1087 case WdfPowerDeviceD3:
james@538 1088 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3\n"));
james@538 1089 break;
james@538 1090 case WdfPowerDeviceD3Final:
james@538 1091 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3Final\n"));
james@538 1092 break;
james@538 1093 case WdfPowerDevicePrepareForHibernation:
james@538 1094 KdPrint((__DRIVER_NAME " WdfPowerDevicePrepareForHibernation\n"));
james@538 1095 break;
james@538 1096 default:
james@538 1097 KdPrint((__DRIVER_NAME " Unknown WdfPowerDevice state %d\n", previous_state));
james@538 1098 break;
james@538 1099 }
james@634 1100
james@551 1101 if (previous_state == WdfPowerDevicePrepareForHibernation
james@551 1102 || (previous_state == WdfPowerDeviceD3 && xppdd->hiber_usage_kludge))
james@551 1103 {
james@551 1104 KdPrint((__DRIVER_NAME " starting up from hibernation\n"));
james@551 1105 }
james@551 1106 else
james@551 1107 {
james@551 1108 }
james@551 1109
james@634 1110 if (previous_state == WdfPowerDevicePrepareForHibernation || previous_state == WdfPowerDeviceD3 || previous_state == WdfPowerDeviceD3Final)
james@634 1111 {
james@634 1112 xppdd->requested_resources_ptr = xppdd->requested_resources_start;
james@634 1113 xppdd->assigned_resources_start = xppdd->assigned_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
james@634 1114 }
james@634 1115
james@551 1116 XenConfig_InitConfigPage(device);
james@551 1117
james@551 1118 status = XenPci_GetBackendAndAddWatch(device);
james@551 1119 if (!NT_SUCCESS(status))
james@551 1120 {
james@551 1121 WdfDeviceSetFailed(device, WdfDeviceFailedNoRestart);
james@702 1122 FUNCTION_EXIT_STATUS(status);
james@551 1123 return status;
james@551 1124 }
james@551 1125 status = XenPci_XenConfigDevice(device);
james@551 1126 if (!NT_SUCCESS(status))
james@551 1127 {
james@551 1128 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
james@624 1129 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
james@551 1130 WdfDeviceSetFailed(device, WdfDeviceFailedNoRestart);
james@702 1131 FUNCTION_EXIT_STATUS(status);
james@551 1132 return status;
james@551 1133 }
james@538 1134
james@536 1135 FUNCTION_EXIT();
james@536 1136
james@538 1137 return status;
james@538 1138 }
james@538 1139
james@538 1140 NTSTATUS
james@538 1141 XenPciPdo_EvtDeviceD0Exit(WDFDEVICE device, WDF_POWER_DEVICE_STATE target_state)
james@538 1142 {
james@538 1143 NTSTATUS status = STATUS_SUCCESS;
james@538 1144 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
james@538 1145 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
james@538 1146 char path[128];
james@538 1147
james@538 1148 UNREFERENCED_PARAMETER(device);
james@538 1149 UNREFERENCED_PARAMETER(target_state);
james@538 1150
james@538 1151 FUNCTION_ENTER();
james@538 1152 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
james@538 1153
james@538 1154 switch (target_state)
james@538 1155 {
james@538 1156 case WdfPowerDeviceD0:
james@538 1157 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
james@538 1158 break;
james@538 1159 case WdfPowerDeviceD1:
james@538 1160 KdPrint((__DRIVER_NAME " WdfPowerDeviceD1\n"));
james@538 1161 break;
james@538 1162 case WdfPowerDeviceD2:
james@538 1163 KdPrint((__DRIVER_NAME " WdfPowerDeviceD2\n"));
james@538 1164 break;
james@538 1165 case WdfPowerDeviceD3:
james@538 1166 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3\n"));
james@538 1167 break;
james@538 1168 case WdfPowerDeviceD3Final:
james@538 1169 KdPrint((__DRIVER_NAME " WdfPowerDeviceD3Final\n"));
james@538 1170 break;
james@538 1171 case WdfPowerDevicePrepareForHibernation:
james@538 1172 KdPrint((__DRIVER_NAME " WdfPowerDevicePrepareForHibernation\n"));
james@538 1173 break;
james@538 1174 default:
james@538 1175 KdPrint((__DRIVER_NAME " Unknown WdfPowerDevice state %d\n", target_state));
james@538 1176 break;
james@538 1177 }
james@692 1178
james@538 1179 if (target_state == WdfPowerDevicePrepareForHibernation
james@538 1180 || (target_state == WdfPowerDeviceD3 && xppdd->hiber_usage_kludge))
james@538 1181 {
james@538 1182 KdPrint((__DRIVER_NAME " not powering down as we are hibernating\n"));
james@538 1183 }
james@538 1184 else
james@538 1185 {
james@538 1186 status = XenPci_XenShutdownDevice(device);
james@538 1187 }
amir@679 1188
amir@679 1189 /* Remove watch on backend state */
amir@679 1190 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
amir@679 1191 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, device);
amir@679 1192
james@538 1193 FUNCTION_EXIT();
james@538 1194
james@538 1195 return status;
james@538 1196 }
james@538 1197
james@538 1198 NTSTATUS
james@551 1199 XenPciPdo_EvtDevicePrepareHardware(WDFDEVICE device, WDFCMRESLIST resources_raw, WDFCMRESLIST resources_translated)
james@538 1200 {
james@538 1201 NTSTATUS status = STATUS_SUCCESS;
james@551 1202
james@538 1203 UNREFERENCED_PARAMETER(device);
james@538 1204 UNREFERENCED_PARAMETER(resources_raw);
james@538 1205 UNREFERENCED_PARAMETER(resources_translated);
james@538 1206
james@538 1207 FUNCTION_ENTER();
james@538 1208 FUNCTION_EXIT();
james@538 1209
james@538 1210 return status;
james@538 1211 }
james@538 1212
james@538 1213 NTSTATUS
james@538 1214 XenPciPdo_EvtDeviceReleaseHardware(WDFDEVICE device, WDFCMRESLIST resources_translated)
james@538 1215 {
james@538 1216 NTSTATUS status = STATUS_SUCCESS;
james@538 1217
james@538 1218 UNREFERENCED_PARAMETER(device);
james@538 1219 UNREFERENCED_PARAMETER(resources_translated);
james@538 1220
james@538 1221 FUNCTION_ENTER();
james@538 1222 FUNCTION_EXIT();
james@538 1223
james@538 1224 return status;
james@538 1225 }
james@538 1226
james@538 1227 static VOID
james@538 1228 XenPciPdo_EvtDeviceUsageNotification(WDFDEVICE device, WDF_SPECIAL_FILE_TYPE notification_type, BOOLEAN is_in_notification_path)
james@538 1229 {
james@538 1230 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
james@538 1231
james@538 1232 FUNCTION_ENTER();
james@538 1233
james@538 1234 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
james@538 1235 switch (notification_type)
james@538 1236 {
james@538 1237 case WdfSpecialFilePaging:
james@538 1238 KdPrint((__DRIVER_NAME " notification_type = Paging, flag = %d\n", is_in_notification_path));
james@538 1239 break;
james@538 1240 case WdfSpecialFileHibernation:
james@538 1241 xppdd->hiber_usage_kludge = is_in_notification_path;
james@538 1242 KdPrint((__DRIVER_NAME " notification_type = Hibernation, flag = %d\n", is_in_notification_path));
james@538 1243 break;
james@538 1244 case WdfSpecialFileDump:
james@538 1245 KdPrint((__DRIVER_NAME " notification_type = Dump, flag = %d\n", is_in_notification_path));
james@538 1246 break;
james@538 1247 default:
james@538 1248 KdPrint((__DRIVER_NAME " notification_type = %d, flag = %d\n", notification_type, is_in_notification_path));
james@538 1249 break;
james@538 1250 }
james@538 1251
james@761 1252 FUNCTION_EXIT();
james@536 1253 }
james@536 1254
james@818 1255 static VOID
james@803 1256 XenPci_EvtDevicePnpStateChange(WDFDEVICE device, PCWDF_DEVICE_PNP_NOTIFICATION_DATA notification_data)
james@803 1257 {
james@803 1258 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
james@803 1259
james@803 1260 //FUNCTION_ENTER();
james@803 1261
james@803 1262 if (xppdd->backend_initiated_remove
james@803 1263 && notification_data->Type == StateNotificationEnterState
james@803 1264 && notification_data->Data.EnterState.CurrentState == WdfDevStatePnpQueryRemovePending
james@803 1265 && notification_data->Data.EnterState.NewState == WdfDevStatePnpQueryCanceled)
james@803 1266 {
james@803 1267 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
james@803 1268
james@803 1269 KdPrint((__DRIVER_NAME " Eject failed, doing surprise removal\n"));
james@803 1270 xppdd->do_not_enumerate = TRUE;
james@803 1271 XenPci_EvtChildListScanForChildren(xpdd->child_list);
james@803 1272 }
james@803 1273
james@803 1274 //FUNCTION_EXIT();
james@803 1275
james@818 1276 //return STATUS_SUCCESS;
james@803 1277 }
james@803 1278
james@536 1279 NTSTATUS
james@536 1280 XenPci_EvtChildListCreateDevice(WDFCHILDLIST child_list,
james@536 1281 PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER identification_header,
james@536 1282 PWDFDEVICE_INIT child_init)
james@536 1283 {
james@536 1284 NTSTATUS status = STATUS_SUCCESS;
james@536 1285 WDF_OBJECT_ATTRIBUTES child_attributes;
james@536 1286 WDFDEVICE child_device;
james@536 1287 PXENPCI_PDO_IDENTIFICATION_DESCRIPTION identification = (PXENPCI_PDO_IDENTIFICATION_DESCRIPTION)identification_header;
james@536 1288 WDF_DEVICE_PNP_CAPABILITIES child_pnp_capabilities;
james@536 1289 DECLARE_UNICODE_STRING_SIZE(buffer, 512);
james@536 1290 DECLARE_CONST_UNICODE_STRING(location, L"Xen Bus");
james@536 1291 PXENPCI_PDO_DEVICE_DATA xppdd;
james@536 1292 PXENPCI_DEVICE_DATA xpdd = GetXpdd(WdfChildListGetDevice(child_list));
james@536 1293 WDF_QUERY_INTERFACE_CONFIG interface_config;
james@536 1294 BUS_INTERFACE_STANDARD bus_interface;
james@536 1295 WDF_PDO_EVENT_CALLBACKS pdo_callbacks;
james@538 1296 WDF_PNPPOWER_EVENT_CALLBACKS child_pnp_power_callbacks;
james@536 1297 UCHAR pnp_minor_functions[] = { IRP_MN_START_DEVICE };
james@538 1298 WDF_DEVICE_POWER_CAPABILITIES child_power_capabilities;
james@536 1299
james@536 1300 FUNCTION_ENTER();
james@536 1301
james@536 1302 WdfDeviceInitSetDeviceType(child_init, FILE_DEVICE_UNKNOWN);
james@536 1303
james@536 1304 WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&child_pnp_power_callbacks);
james@536 1305 child_pnp_power_callbacks.EvtDeviceD0Entry = XenPciPdo_EvtDeviceD0Entry;
james@538 1306 //child_pnp_power_callbacks.EvtDeviceD0EntryPostInterruptsEnabled = XenPciPdo_EvtDeviceD0EntryPostInterruptsEnabled;
james@536 1307 child_pnp_power_callbacks.EvtDeviceD0Exit = XenPciPdo_EvtDeviceD0Exit;
james@538 1308 //child_pnp_power_callbacks.EvtDeviceD0ExitPreInterruptsDisabled = XenPciPdo_EvtDeviceD0ExitPreInterruptsDisabled;
james@536 1309 child_pnp_power_callbacks.EvtDevicePrepareHardware = XenPciPdo_EvtDevicePrepareHardware;
james@536 1310 child_pnp_power_callbacks.EvtDeviceReleaseHardware = XenPciPdo_EvtDeviceReleaseHardware;
james@538 1311 child_pnp_power_callbacks.EvtDeviceUsageNotification = XenPciPdo_EvtDeviceUsageNotification;
james@536 1312 WdfDeviceInitSetPnpPowerEventCallbacks(child_init, &child_pnp_power_callbacks);
james@538 1313
james@536 1314 KdPrint((__DRIVER_NAME " device = '%s', index = '%d', path = '%s'\n",
james@536 1315 identification->device, identification->index, identification->path));
james@536 1316
james@536 1317 status = WdfDeviceInitAssignWdmIrpPreprocessCallback(child_init, XenPciPdo_EvtDeviceWdmIrpPreprocess_START_DEVICE,
james@536 1318 IRP_MJ_PNP, pnp_minor_functions, ARRAY_SIZE(pnp_minor_functions));
james@536 1319 if (!NT_SUCCESS(status))
james@536 1320 {
james@536 1321 return status;
james@536 1322 }
james@536 1323
james@536 1324 WDF_PDO_EVENT_CALLBACKS_INIT(&pdo_callbacks);
james@536 1325 //pdo_callbacks.EvtDeviceResourcesQuery = XenPciPdo_EvtDeviceResourcesQuery;
james@536 1326 pdo_callbacks.EvtDeviceResourceRequirementsQuery = XenPciPdo_EvtDeviceResourceRequirementsQuery;
james@536 1327 //pdo_callbacks.EvtDeviceEject = XenPciPdo_EvtDeviceEject;
james@536 1328 //pdo_callbacks.EvtDeviceSetLock = XenPciPdo_EvtDeviceSetLock;
james@536 1329 WdfPdoInitSetEventCallbacks(child_init, &pdo_callbacks);
james@536 1330
james@536 1331 RtlUnicodeStringPrintf(&buffer, L"xen\\%S", identification->device);
james@536 1332 status = WdfPdoInitAssignDeviceID(child_init, &buffer);
james@536 1333 if (!NT_SUCCESS(status))
james@536 1334 {
james@536 1335 return status;
james@536 1336 }
james@536 1337 status = WdfPdoInitAddHardwareID(child_init, &buffer);
james@536 1338 if (!NT_SUCCESS(status))
james@536 1339 {
james@536 1340 return status;
james@536 1341 }
james@536 1342 status = WdfPdoInitAddCompatibleID(child_init, &buffer);
james@536 1343 if (!NT_SUCCESS(status))
james@536 1344 {
james@536 1345 return status;
james@536 1346 }
james@536 1347
james@536 1348 RtlUnicodeStringPrintf(&buffer, L"%02d", identification->index);
james@536 1349 status = WdfPdoInitAssignInstanceID(child_init, &buffer);
james@536 1350 if (!NT_SUCCESS(status))
james@536 1351 {
james@536 1352 return status;
james@536 1353 }
james@536 1354
james@536 1355 RtlUnicodeStringPrintf(&buffer, L"Xen %S device #%d", identification->device, identification->index);
james@536 1356 status = WdfPdoInitAddDeviceText(child_init, &buffer, &location, 0x0409);
james@536 1357 if (!NT_SUCCESS(status))
james@536 1358 {
james@536 1359 return status;
james@536 1360 }
james@536 1361 WdfPdoInitSetDefaultLocale(child_init, 0x0409);
james@538 1362
james@538 1363 WdfDeviceInitSetPowerNotPageable(child_init);
james@803 1364
james@803 1365 WdfDeviceInitRegisterPnpStateChangeCallback(child_init, WdfDevStatePnpQueryCanceled, XenPci_EvtDevicePnpStateChange, StateNotificationEnterState);
james@536 1366
james@536 1367 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&child_attributes, XENPCI_PDO_DEVICE_DATA);
james@536 1368 status = WdfDeviceCreate(&child_init, &child_attributes, &child_device);
james@536 1369 if (!NT_SUCCESS(status))
james@536 1370 {
james@536 1371 return status;
james@536 1372 }
james@536 1373
james@536 1374 xppdd = GetXppdd(child_device);
james@536 1375
james@536 1376 xppdd->wdf_device = child_device;
james@536 1377 xppdd->wdf_device_bus_fdo = WdfChildListGetDevice(child_list);
james@536 1378
james@551 1379 xppdd->config_page_mdl = AllocateUncachedPage();
james@551 1380
james@536 1381 xppdd->device_state.magic = XEN_DEVICE_STATE_MAGIC;
james@536 1382 xppdd->device_state.length = sizeof(XENPCI_DEVICE_STATE);
james@536 1383 xppdd->device_state.suspend_resume_state_pdo = SR_STATE_RUNNING;
james@536 1384 xppdd->device_state.suspend_resume_state_fdo = SR_STATE_RUNNING;
james@536 1385 xppdd->device_state.pdo_event_channel = xpdd->pdo_event_channel;
james@536 1386 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFilePaging, TRUE);
james@536 1387 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFileHibernation, TRUE);
james@536 1388 WdfDeviceSetSpecialFileSupport(child_device, WdfSpecialFileDump, TRUE);
james@536 1389
james@536 1390 WDF_DEVICE_PNP_CAPABILITIES_INIT(&child_pnp_capabilities);
james@536 1391 child_pnp_capabilities.LockSupported = WdfFalse;
james@536 1392 child_pnp_capabilities.EjectSupported = WdfTrue;
james@536 1393 child_pnp_capabilities.Removable = WdfTrue;
james@536 1394 child_pnp_capabilities.DockDevice = WdfFalse;
james@536 1395 child_pnp_capabilities.UniqueID = WdfFalse;
james@536 1396 child_pnp_capabilities.SilentInstall = WdfTrue;
james@536 1397 child_pnp_capabilities.SurpriseRemovalOK = WdfTrue;
james@536 1398 child_pnp_capabilities.HardwareDisabled = WdfFalse;
james@536 1399 WdfDeviceSetPnpCapabilities(child_device, &child_pnp_capabilities);
james@536 1400
james@538 1401 WDF_DEVICE_POWER_CAPABILITIES_INIT(&child_power_capabilities);
james@538 1402 child_power_capabilities.DeviceD1 = WdfTrue;
james@538 1403 child_power_capabilities.WakeFromD1 = WdfTrue;
james@538 1404 child_power_capabilities.DeviceWake = PowerDeviceD1;
james@538 1405 child_power_capabilities.DeviceState[PowerSystemWorking] = PowerDeviceD1;
james@538 1406 child_power_capabilities.DeviceState[PowerSystemSleeping1] = PowerDeviceD1;
james@538 1407 child_power_capabilities.DeviceState[PowerSystemSleeping2] = PowerDeviceD2;
james@538 1408 child_power_capabilities.DeviceState[PowerSystemSleeping3] = PowerDeviceD2;
james@538 1409 child_power_capabilities.DeviceState[PowerSystemHibernate] = PowerDeviceD3;
james@538 1410 child_power_capabilities.DeviceState[PowerSystemShutdown] = PowerDeviceD3;
james@538 1411 WdfDeviceSetPowerCapabilities(child_device, &child_power_capabilities);
james@536 1412
james@766 1413 status = WdfFdoQueryForInterface(xpdd->wdf_device, &GUID_BUS_INTERFACE_STANDARD, (PINTERFACE)&bus_interface,
james@766 1414 sizeof(BUS_INTERFACE_STANDARD), 1, NULL);
james@766 1415 if (!NT_SUCCESS(status))
james@766 1416 {
james@766 1417 KdPrint((__DRIVER_NAME " WdfFdoQueryForInterface failed - %08x\n", status));
james@766 1418 return status;
james@766 1419 }
james@536 1420 WDF_QUERY_INTERFACE_CONFIG_INIT(&interface_config, (PINTERFACE)&bus_interface, &GUID_BUS_INTERFACE_STANDARD, NULL);
james@536 1421 status = WdfDeviceAddQueryInterface(child_device, &interface_config);
james@536 1422 if (!NT_SUCCESS(status))
james@536 1423 {
james@766 1424 KdPrint((__DRIVER_NAME " WdfDeviceAddQueryInterface failed - %08x\n", status));
james@536 1425 return status;
james@536 1426 }
james@536 1427
james@536 1428 RtlStringCbCopyA(xppdd->path, ARRAY_SIZE(xppdd->path), identification->path);
james@536 1429 RtlStringCbCopyA(xppdd->device, ARRAY_SIZE(xppdd->device), identification->device);
james@536 1430 xppdd->index = identification->index;
james@536 1431 KeInitializeEvent(&xppdd->backend_state_event, SynchronizationEvent, FALSE);
james@624 1432 ExInitializeFastMutex(&xppdd->backend_state_mutex);
james@536 1433 xppdd->backend_state = XenbusStateUnknown;
james@538 1434 xppdd->frontend_state = XenbusStateUnknown;
james@536 1435 xppdd->backend_path[0] = '\0';
james@536 1436
james@536 1437 FUNCTION_EXIT();
james@536 1438
james@536 1439 return status;
james@536 1440 }
james@536 1441
james@536 1442 static __forceinline VOID
james@536 1443 XenPci_Pdo_ChangeSuspendState(WDFDEVICE device, ULONG new_state)
james@536 1444 {
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
james@536 1448 FUNCTION_ENTER();
james@536 1449 KdPrint((__DRIVER_NAME " setting pdo state to %d\n", new_state));
james@536 1450 xppdd->device_state.suspend_resume_state_pdo = new_state;
james@536 1451 KeMemoryBarrier();
james@536 1452 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xpdd->pdo_event_channel));
james@536 1453 EvtChn_Notify(xpdd, xpdd->pdo_event_channel);
james@536 1454 while(xppdd->device_state.suspend_resume_state_fdo != xppdd->device_state.suspend_resume_state_pdo)
james@536 1455 {
james@536 1456 KdPrint((__DRIVER_NAME " waiting...\n"));
james@536 1457 KeWaitForSingleObject(&xpdd->pdo_suspend_event, Executive, KernelMode, FALSE, NULL);
james@536 1458 }
james@536 1459 KdPrint((__DRIVER_NAME " fdo state set to %d\n", new_state));
james@536 1460 FUNCTION_EXIT();
james@536 1461 }
james@536 1462
james@536 1463 /* called at PASSIVE_LEVEL */
james@536 1464 NTSTATUS
james@536 1465 XenPci_Pdo_Suspend(WDFDEVICE device)
james@536 1466 {
james@536 1467 NTSTATUS status = STATUS_SUCCESS;
james@536 1468 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
james@536 1469 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
james@536 1470 //LARGE_INTEGER wait_time;
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@536 1477
james@536 1478 KdPrint((__DRIVER_NAME " --> " __FUNCTION__ " (%s)\n", xppdd->path));
james@536 1479
james@536 1480 if (xppdd->backend_state == XenbusStateConnected)
james@536 1481 {
james@536 1482 xppdd->restart_on_resume = TRUE;
james@536 1483 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_SUSPENDING);
james@536 1484
james@536 1485 XenPci_ChangeFrontendState(device, XenbusStateClosing, XenbusStateClosing, 30000);
james@536 1486 XenPci_ChangeFrontendState(device, XenbusStateClosed, XenbusStateClosed, 30000);
james@536 1487 XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 30000);
james@536 1488
james@536 1489 if (xppdd->assigned_resources_start != NULL)
james@536 1490 {
james@536 1491 in_ptr = xppdd->assigned_resources_ptr;
james@536 1492 ADD_XEN_INIT_RSP(&in_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
james@536 1493 in_ptr = xppdd->assigned_resources_start;
james@536 1494 while((type = GET_XEN_INIT_RSP(&in_ptr, &setting, &value, &value2)) != XEN_INIT_TYPE_END)
james@536 1495 {
james@536 1496 switch (type)
james@536 1497 {
james@536 1498 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
james@617 1499 case XEN_INIT_TYPE_EVENT_CHANNEL_DPC: /* frontend event channel bound to dpc */
james@536 1500 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
james@536 1501 EvtChn_Unbind(xpdd, PtrToUlong(value));
james@536 1502 EvtChn_Close(xpdd, PtrToUlong(value));
james@536 1503 break;
james@536 1504 }
james@536 1505 }
james@536 1506 }
james@536 1507
james@536 1508 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
james@624 1509 XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateHandler, xppdd);
james@536 1510 }
james@536 1511 else
james@536 1512 {
james@536 1513 xppdd->restart_on_resume = FALSE;
james@536 1514 }
james@536 1515
james@536 1516 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@536 1517
james@536 1518 return status;
james@536 1519 }
james@536 1520
james@536 1521 NTSTATUS
james@536 1522 XenPci_Pdo_Resume(WDFDEVICE device)
james@536 1523 {
james@536 1524 NTSTATUS status = STATUS_SUCCESS;
james@536 1525 PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
james@536 1526 PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
james@536 1527 ULONG old_backend_state;
james@536 1528 PUCHAR src, dst;
james@536 1529
james@536 1530 FUNCTION_ENTER();
james@536 1531 KdPrint((__DRIVER_NAME " path = %s\n", xppdd->path));
james@536 1532
james@536 1533 xppdd->device_state.pdo_event_channel = xpdd->pdo_event_channel;
james@536 1534 old_backend_state = xppdd->backend_state;
james@536 1535
james@536 1536 if (xppdd->restart_on_resume)
james@536 1537 {
james@536 1538 status = XenPci_GetBackendAndAddWatch(device);
james@536 1539
james@536 1540 if (XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 30000) != STATUS_SUCCESS)
james@536 1541 {
james@536 1542 KdPrint((__DRIVER_NAME " Failed to change frontend state to Initialising\n"));
james@536 1543 // this is probably an unrecoverable situation...
james@702 1544 FUNCTION_EXIT();
james@536 1545 return STATUS_UNSUCCESSFUL;
james@536 1546 }
james@536 1547 if (xppdd->assigned_resources_ptr)
james@536 1548 {
james@536 1549 // reset things - feed the 'requested resources' back in
james@536 1550 ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
james@536 1551 src = xppdd->requested_resources_start;
james@536 1552 xppdd->requested_resources_ptr = xppdd->requested_resources_start = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);;
james@536 1553 xppdd->assigned_resources_ptr = xppdd->assigned_resources_start;
james@536 1554
james@536 1555 dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
james@536 1556
james@536 1557 status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
james@536 1558
james@536 1559 MmUnmapIoSpace(dst, xppdd->config_page_length);
james@536 1560 ExFreePoolWithTag(src, XENPCI_POOL_TAG);
james@536 1561 }
james@536 1562 if (XenPci_ChangeFrontendState(device, XenbusStateConnected, XenbusStateConnected, 30000) != STATUS_SUCCESS)
james@536 1563 {
james@536 1564 // this is definitely an unrecoverable situation...
james@536 1565 KdPrint((__DRIVER_NAME " Failed to change frontend state to connected\n"));
james@702 1566 FUNCTION_EXIT();
james@536 1567 return STATUS_UNSUCCESSFUL;
james@536 1568 }
james@536 1569 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_RESUMING);
james@536 1570 XenPci_Pdo_ChangeSuspendState(device, SR_STATE_RUNNING);
james@536 1571 }
james@536 1572
james@536 1573 FUNCTION_EXIT();
james@536 1574
james@536 1575 return STATUS_SUCCESS;
james@536 1576 }