win-pvdrivers

changeset 53:94d0f2823b42

manual merge
author Andy Grover <andy.grover@oracle.com>
date Mon Dec 17 14:19:39 2007 -0800 (2007-12-17)
parents d69f904fcf28 4f37f90fc704
children e170a016cea8
files common/include/evtchn_public.h common/include/gnttbl_public.h common/include/xenbus_public.h xenpci/evtchn.c xenpci/xenbus.c xenpci/xenpci.c xenvbd/xenvbd.c xenvbd/xenvbd.h
line diff
     1.1 --- a/common/include/evtchn_public.h	Fri Dec 14 13:30:56 2007 -0800
     1.2 +++ b/common/include/evtchn_public.h	Mon Dec 17 14:19:39 2007 -0800
     1.3 @@ -17,6 +17,9 @@ along with this program; if not, write t
     1.4  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     1.5  */
     1.6  
     1.7 +#if !defined(_EVTCHN_PUBLIC_H_)
     1.8 +#define _EVTCHN_PUBLIC_H_
     1.9 +
    1.10  DEFINE_GUID( GUID_XEN_IFACE_EVTCHN, 0xD2D20756, 0xDE69, 0x4447, 0x8A, 0x7D, 0x98, 0x37, 0x19, 0x7D, 0x61, 0x66);
    1.11  //{D2D20756-DE69-4447-8A7D-9837197D6166}
    1.12  
    1.13 @@ -49,3 +52,5 @@ typedef struct _XENBUS_IFACE_EVTCHN {
    1.14    PXEN_EVTCHN_ALLOCUNBOUND AllocUnbound;
    1.15  
    1.16  } XEN_IFACE_EVTCHN, *PXEN_IFACE_EVTCHN;
    1.17 +
    1.18 +#endif
    1.19 \ No newline at end of file
     2.1 --- a/common/include/gnttbl_public.h	Fri Dec 14 13:30:56 2007 -0800
     2.2 +++ b/common/include/gnttbl_public.h	Mon Dec 17 14:19:39 2007 -0800
     2.3 @@ -17,6 +17,9 @@ along with this program; if not, write t
     2.4  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     2.5  */
     2.6  
     2.7 +#if !defined(_GNTTBL_PUBLIC_H_)
     2.8 +#define _GNTTBL_PUBLIC_H_
     2.9 +
    2.10  // {6A71ACF8-0F6D-4022-BA60-19986EBEEA73}
    2.11  DEFINE_GUID(GUID_XEN_IFACE_GNTTBL, 0x6a71acf8, 0xf6d, 0x4022, 0xba, 0x60, 0x19, 0x98, 0x6e, 0xbe, 0xea, 0x73);
    2.12  
    2.13 @@ -31,3 +34,5 @@ typedef struct _XEN_IFACE_GNTTBL {
    2.14    PXEN_GNTTBL_GRANTACCESS GrantAccess;
    2.15    PXEN_GNTTBL_ENDACCESS EndAccess;
    2.16  } XEN_IFACE_GNTTBL, *PXEN_IFACE_GNTTBL;
    2.17 +
    2.18 +#endif
    2.19 \ No newline at end of file
     3.1 --- a/common/include/xen_public.h	Fri Dec 14 13:30:56 2007 -0800
     3.2 +++ b/common/include/xen_public.h	Mon Dec 17 14:19:39 2007 -0800
     3.3 @@ -1,6 +1,10 @@
     3.4  #if !defined(_XEN_PUBLIC_H_)
     3.5  #define _XEN_PUBLIC_H_
     3.6  
     3.7 +#include <evtchn_public.h>
     3.8 +#include <xenbus_public.h>
     3.9 +#include <gnttbl_public.h>
    3.10 +
    3.11  DEFINE_GUID( GUID_XEN_IFACE_XEN, 0x5C568AC5, 0x9DDF, 0x4FA5, 0xA9, 0x4A, 0x39, 0xD6, 0x70, 0x77, 0x81, 0x9C);
    3.12  //{5C568AC5-9DDF-4FA5-A94A-39D67077819C}
    3.13  
    3.14 @@ -18,10 +22,20 @@ typedef struct _XEN_IFACE_XEN {
    3.15  
    3.16  } XEN_IFACE_XEN, *PXEN_IFACE_XEN;
    3.17  
    3.18 +#define XEN_DATA_MAGIC 0x12345678
    3.19  
    3.20  typedef struct {
    3.21 +  ULONG Magic;
    3.22    char BasePath[128];
    3.23    PXENBUS_WATCH_CALLBACK WatchHandler;
    3.24 +  PVOID WatchContext;
    3.25 +  XEN_IFACE_EVTCHN EvtChnInterface;
    3.26 +  XEN_IFACE_XENBUS XenBusInterface;
    3.27 +  //XEN_IFACE_XEN XenInterface;
    3.28 +  XEN_IFACE_GNTTBL GntTblInterface;
    3.29 +  BOOLEAN AutoEnumerate;
    3.30 +  CM_PARTIAL_RESOURCE_DESCRIPTOR InterruptRaw;
    3.31 +  CM_PARTIAL_RESOURCE_DESCRIPTOR InterruptTranslated;
    3.32  } XENPCI_XEN_DEVICE_DATA, *PXENPCI_XEN_DEVICE_DATA;
    3.33  
    3.34  WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_XEN_DEVICE_DATA, GetXenDeviceData);
     4.1 --- a/common/include/xenbus_public.h	Fri Dec 14 13:30:56 2007 -0800
     4.2 +++ b/common/include/xenbus_public.h	Mon Dec 17 14:19:39 2007 -0800
     4.3 @@ -17,6 +17,9 @@ along with this program; if not, write t
     4.4  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     4.5  */
     4.6  
     4.7 +#if !defined(_XENBUS_PUBLIC_H_)
     4.8 +#define _XENBUS_PUBLIC_H_
     4.9 +
    4.10  DEFINE_GUID( GUID_XEN_IFACE_XENBUS, 0x9CA4D816, 0x0E5E, 0x4f9a, 0x8F, 0x59, 0x94, 0x4C, 0xED, 0x82, 0x78, 0x11);
    4.11  //{9CA4D816-0E5E-4f9a-8F59-944CED827811}
    4.12  
    4.13 @@ -52,3 +55,5 @@ typedef struct _XEN_IFACE_XENBUS {
    4.14    PXEN_XENBUS_ADDWATCH AddWatch;
    4.15    PXEN_XENBUS_REMWATCH RemWatch;
    4.16  } XEN_IFACE_XENBUS, *PXEN_IFACE_XENBUS;
    4.17 +
    4.18 +#endif
    4.19 \ No newline at end of file
     5.1 --- a/dirs	Fri Dec 14 13:30:56 2007 -0800
     5.2 +++ b/dirs	Mon Dec 17 14:19:39 2007 -0800
     5.3 @@ -1,1 +1,1 @@
     5.4 -DIRS=xenpci xenhide xenvbd
     5.5 \ No newline at end of file
     5.6 +DIRS=xenpci xenhide xenvbd xenaddresource
     5.7 \ No newline at end of file
     6.1 --- a/xenhide/sources	Fri Dec 14 13:30:56 2007 -0800
     6.2 +++ b/xenhide/sources	Mon Dec 17 14:19:39 2007 -0800
     6.3 @@ -1,14 +1,13 @@
     6.4  TARGETNAME=XENHIDE
     6.5  TARGETTYPE=DRIVER
     6.6 -TARGETPATH=..\Target
     6.7 -
     6.8 +TARGETPATH=..\Target\$(DDK_TARGET_OS)
     6.9 +VERSION=0.4.0.0
    6.10 +KMDF_VERSION=1
    6.11  MSC_WARNING_LEVEL=/W4
    6.12 -
    6.13 +INF_NAME=xenhide
    6.14  INCLUDES = ..\common\include;..\common\include\public
    6.15 -
    6.16 -NO_BINPLACE=1
    6.17 -KMDF_VERSION=1
    6.18  TARGETLIBS=$(DDK_LIB_PATH)\ntstrsafe.lib
    6.19 -
    6.20 +MISCFILES=$(TARGETPATH)\$(INF_NAME).inf
    6.21 +NTTARGETFILES=
    6.22  SOURCES=xenhide.c
    6.23  
     7.1 --- a/xenpci/evtchn.c	Fri Dec 14 13:30:56 2007 -0800
     7.2 +++ b/xenpci/evtchn.c	Mon Dec 17 14:19:39 2007 -0800
     7.3 @@ -68,7 +68,8 @@ EvtChn_Interrupt(WDFINTERRUPT Interrupt,
     7.4  
     7.5    //KdPrint((__DRIVER_NAME "     I-\n"));
     7.6  
     7.7 -  return TRUE;
     7.8 +//  return TRUE;
     7.9 +  return FALSE;
    7.10  }
    7.11  
    7.12  NTSTATUS
    7.13 @@ -77,7 +78,7 @@ EvtChn_Bind(PVOID Context, evtchn_port_t
    7.14    WDFDEVICE Device = Context;
    7.15    PXENPCI_DEVICE_DATA xpdd = GetDeviceData(Device);
    7.16  
    7.17 -  KdPrint((__DRIVER_NAME " --> EvtChn_Bind\n"));
    7.18 +  KdPrint((__DRIVER_NAME " --> EvtChn_Bind (ServiceRoutine = %08X, ServiceContext = %08x)\n", ServiceRoutine, ServiceContext));
    7.19  
    7.20    if(xpdd->ev_actions[Port].ServiceRoutine != NULL)
    7.21    {
     8.1 --- a/xenpci/sources	Fri Dec 14 13:30:56 2007 -0800
     8.2 +++ b/xenpci/sources	Mon Dec 17 14:19:39 2007 -0800
     8.3 @@ -1,14 +1,12 @@
     8.4  TARGETNAME=XENPCI
     8.5  TARGETTYPE=DRIVER
     8.6 -TARGETPATH=..\Target
     8.7 -
     8.8 +TARGETPATH=..\Target\$(DDK_TARGET_OS)
     8.9 +VERSION=0.4.0.12
    8.10 +KMDF_VERSION=1
    8.11  MSC_WARNING_LEVEL=/W4
    8.12 -
    8.13 +INF_NAME=xenpci
    8.14  INCLUDES = ..\common\include;..\common\include\public
    8.15 -
    8.16 -NO_BINPLACE=1
    8.17 -KMDF_VERSION=1
    8.18  TARGETLIBS=$(DDK_LIB_PATH)\ntstrsafe.lib
    8.19 -
    8.20 +MISCFILES=$(TARGETPATH)\$(INF_NAME).inf
    8.21 +NTTARGETFILES=
    8.22  SOURCES=xenpci.c xenbus.c gnttbl.c evtchn.c
    8.23 -
     9.1 --- a/xenpci/xenbus.c	Fri Dec 14 13:30:56 2007 -0800
     9.2 +++ b/xenpci/xenbus.c	Mon Dec 17 14:19:39 2007 -0800
     9.3 @@ -379,15 +379,25 @@ XenBus_Close(WDFDEVICE Device)
     9.4    KdPrint((__DRIVER_NAME "     Waiting for threads to die\n"));
     9.5    ObReferenceObjectByHandle(xpdd->XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, &WaitArray[0], NULL);
     9.6    ObReferenceObjectByHandle(xpdd->XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, &WaitArray[1], NULL);
     9.7 -  KeWaitForMultipleObjects(2, WaitArray, WaitAll, Executive, KernelMode, FALSE, NULL, WaitBlockArray);
     9.8 +  //KeWaitForMultipleObjects(1, &WaitArray[0], WaitAny, Executive, KernelMode, FALSE, NULL, WaitBlockArray);
     9.9 +  KdPrint((__DRIVER_NAME "     Waiting for ReadThread\n"));
    9.10 +  KeWaitForSingleObject(WaitArray[0], Executive, KernelMode, FALSE, NULL);
    9.11 +  KdPrint((__DRIVER_NAME "     Waiting for WatchThread\n"));
    9.12 +  //KeWaitForMultipleObjects(1, &WaitArray[1], WaitAny, Executive, KernelMode, FALSE, NULL, WaitBlockArray);
    9.13 +  KeWaitForSingleObject(WaitArray[1], Executive, KernelMode, FALSE, NULL);
    9.14    KdPrint((__DRIVER_NAME "     Threads are dead\n"));
    9.15  
    9.16    xpdd->XenBus_ShuttingDown = FALSE;
    9.17  
    9.18 -  ObDereferenceObject(WaitArray[0]);
    9.19 -  ObDereferenceObject(WaitArray[1]);
    9.20 +//  ObDereferenceObject(WaitArray[0]);
    9.21 +//  ObDereferenceObject(WaitArray[1]);
    9.22 +
    9.23 +  KdPrint((__DRIVER_NAME "     A\n"));
    9.24  
    9.25    ZwClose(xpdd->XenBus_WatchThreadHandle);
    9.26 +
    9.27 +  KdPrint((__DRIVER_NAME "     B\n"));
    9.28 +
    9.29    ZwClose(xpdd->XenBus_ReadThreadHandle);
    9.30  
    9.31    KdPrint((__DRIVER_NAME " <-- XenBus_Close\n"));
    10.1 --- a/xenpci/xenpci.c	Fri Dec 14 13:30:56 2007 -0800
    10.2 +++ b/xenpci/xenpci.c	Mon Dec 17 14:19:39 2007 -0800
    10.3 @@ -348,12 +348,12 @@ XenPCI_AddDevice(
    10.4    }
    10.5  
    10.6    busInfo.BusTypeGuid = GUID_XENPCI_DEVCLASS;
    10.7 -  busInfo.LegacyBusType = PNPBus;
    10.8 +  busInfo.LegacyBusType = Internal; //PNPBus;
    10.9    busInfo.BusNumber = 0;
   10.10  
   10.11    WdfDeviceSetBusInformationForChildren(Device, &busInfo);
   10.12  
   10.13 -  WDF_INTERRUPT_CONFIG_INIT(&interruptConfig, EvtChn_Interrupt, NULL);
   10.14 +  WDF_INTERRUPT_CONFIG_INIT(&interruptConfig, EvtChn_Interrupt, NULL); //EvtChn_InterruptDpc);
   10.15    interruptConfig.EvtInterruptEnable = XenPCI_InterruptEnable;
   10.16    interruptConfig.EvtInterruptDisable = XenPCI_InterruptDisable;
   10.17    status = WdfInterruptCreate(Device, &interruptConfig, WDF_NO_OBJECT_ATTRIBUTES, &xpdd->XenInterrupt);
   10.18 @@ -407,8 +407,6 @@ XenPCI_PrepareHardware(
   10.19      switch (descriptor->Type) {
   10.20      case CmResourceTypePort:
   10.21        //KdPrint((__DRIVER_NAME "     I/O mapped CSR: (%x) Length: (%d)\n", descriptor->u.Port.Start.LowPart, descriptor->u.Port.Length));
   10.22 -//      xpdd->IoBaseAddress = ULongToPtr(descriptor->u.Port.Start.LowPart);
   10.23 -//      xpdd->IoRange = descriptor->u.Port.Length;
   10.24        break;
   10.25      case CmResourceTypeMemory:
   10.26        KdPrint((__DRIVER_NAME "     Memory mapped CSR:(%x:%x) Length:(%d)\n", descriptor->u.Memory.Start.LowPart, descriptor->u.Memory.Start.HighPart, descriptor->u.Memory.Length));
   10.27 @@ -418,6 +416,8 @@ XenPCI_PrepareHardware(
   10.28        break;
   10.29      case CmResourceTypeInterrupt:
   10.30        //KdPrint((__DRIVER_NAME "     Interrupt level: 0x%0x, Vector: 0x%0x\n", descriptor->u.Interrupt.Level, descriptor->u.Interrupt.Vector));
   10.31 +      memcpy(&InterruptRaw, WdfCmResourceListGetDescriptor(ResourceList, i), sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
   10.32 +      memcpy(&InterruptTranslated, WdfCmResourceListGetDescriptor(ResourceListTranslated, i), sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
   10.33        break;
   10.34      case CmResourceTypeDevicePrivate:
   10.35        //KdPrint((__DRIVER_NAME "     Private Data: 0x%02x 0x%02x 0x%02x\n", descriptor->u.DevicePrivate.Data[0], descriptor->u.DevicePrivate.Data[1], descriptor->u.DevicePrivate.Data[2] ));
   10.36 @@ -641,10 +641,6 @@ XenPCI_ChildListCreateDevice(
   10.37    NTSTATUS status;
   10.38    WDFDEVICE ChildDevice = NULL;
   10.39    PXENPCI_IDENTIFICATION_DESCRIPTION XenIdentificationDesc;
   10.40 -  XEN_IFACE_EVTCHN EvtChnInterface;
   10.41 -  XEN_IFACE_XENBUS XenBusInterface;
   10.42 -  //XEN_IFACE_XEN XenInterface;
   10.43 -  XEN_IFACE_GNTTBL GntTblInterface;
   10.44    DECLARE_UNICODE_STRING_SIZE(buffer, 20);
   10.45    WDF_OBJECT_ATTRIBUTES PdoAttributes;
   10.46    DECLARE_CONST_UNICODE_STRING(DeviceLocation, L"Xen Bus");
   10.47 @@ -692,73 +688,72 @@ XenPCI_ChildListCreateDevice(
   10.48    WdfDeviceSetSpecialFileSupport(ChildDevice, WdfSpecialFileDump, TRUE);
   10.49  
   10.50    ChildDeviceData = GetXenDeviceData(ChildDevice);
   10.51 +  ChildDeviceData->Magic = XEN_DATA_MAGIC;
   10.52 +  ChildDeviceData->AutoEnumerate = AutoEnumerate;
   10.53 +  ChildDeviceData->WatchHandler = NULL;
   10.54    strncpy(ChildDeviceData->BasePath, XenIdentificationDesc->Path, 128);
   10.55 -
   10.56 -  RtlZeroMemory(&EvtChnInterface, sizeof(EvtChnInterface));
   10.57 -  EvtChnInterface.InterfaceHeader.Size = sizeof(EvtChnInterface);
   10.58 -  EvtChnInterface.InterfaceHeader.Version = 1;
   10.59 -  EvtChnInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
   10.60 -  EvtChnInterface.InterfaceHeader.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
   10.61 -  EvtChnInterface.InterfaceHeader.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
   10.62 -  EvtChnInterface.Bind = EvtChn_Bind;
   10.63 -  EvtChnInterface.Unbind = EvtChn_Unbind;
   10.64 -  EvtChnInterface.Mask = EvtChn_Mask;
   10.65 -  EvtChnInterface.Unmask = EvtChn_Unmask;
   10.66 -  EvtChnInterface.Notify = EvtChn_Notify;
   10.67 -  EvtChnInterface.AllocUnbound = EvtChn_AllocUnbound;
   10.68 -  WDF_QUERY_INTERFACE_CONFIG_INIT(&qiConfig, (PINTERFACE)&EvtChnInterface, &GUID_XEN_IFACE_EVTCHN, NULL);
   10.69 +  memcpy(&ChildDeviceData->InterruptRaw, &InterruptRaw, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
   10.70 +  memcpy(&ChildDeviceData->InterruptTranslated, &InterruptTranslated, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
   10.71 +  
   10.72 +  ChildDeviceData->EvtChnInterface.InterfaceHeader.Size = sizeof(ChildDeviceData->EvtChnInterface);
   10.73 +  ChildDeviceData->EvtChnInterface.InterfaceHeader.Version = 1;
   10.74 +  ChildDeviceData->EvtChnInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
   10.75 +  ChildDeviceData->EvtChnInterface.InterfaceHeader.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
   10.76 +  ChildDeviceData->EvtChnInterface.InterfaceHeader.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
   10.77 +  ChildDeviceData->EvtChnInterface.Bind = EvtChn_Bind;
   10.78 +  ChildDeviceData->EvtChnInterface.Unbind = EvtChn_Unbind;
   10.79 +  ChildDeviceData->EvtChnInterface.Mask = EvtChn_Mask;
   10.80 +  ChildDeviceData->EvtChnInterface.Unmask = EvtChn_Unmask;
   10.81 +  ChildDeviceData->EvtChnInterface.Notify = EvtChn_Notify;
   10.82 +  ChildDeviceData->EvtChnInterface.AllocUnbound = EvtChn_AllocUnbound;
   10.83 +  WDF_QUERY_INTERFACE_CONFIG_INIT(&qiConfig, (PINTERFACE)&ChildDeviceData->EvtChnInterface, &GUID_XEN_IFACE_EVTCHN, NULL);
   10.84    status = WdfDeviceAddQueryInterface(ChildDevice, &qiConfig);
   10.85    if (!NT_SUCCESS(status))
   10.86    {
   10.87      return status;
   10.88    }
   10.89  
   10.90 -  /* nobody uses this yet, maybe it isn't needed? */
   10.91 -#if 0
   10.92 -  RtlZeroMemory(&XenInterface, sizeof(XenInterface));
   10.93 -  XenInterface.InterfaceHeader.Size = sizeof(XenInterface);
   10.94 -  XenInterface.InterfaceHeader.Version = 1;
   10.95 -  XenInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
   10.96 -  XenInterface.InterfaceHeader.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
   10.97 -  XenInterface.InterfaceHeader.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
   10.98 -  XenInterface.AllocMMIO = XenPCI_AllocMMIO;
   10.99 -  WDF_QUERY_INTERFACE_CONFIG_INIT(&qiConfig, (PINTERFACE)&XenInterface, &GUID_XEN_IFACE_XEN, NULL);
  10.100 +/*
  10.101 +  ChildDeviceData->XenInterface.InterfaceHeader.Size = sizeof(ChildDeviceData->XenInterface);
  10.102 +  ChildDeviceData->XenInterface.InterfaceHeader.Version = 1;
  10.103 +  ChildDeviceData->XenInterface.InterfaceHeader.Context = NULL;
  10.104 +  ChildDeviceData->XenInterface.InterfaceHeader.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
  10.105 +  ChildDeviceData->XenInterface.InterfaceHeader.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
  10.106 +  ChildDeviceData->XenInterface.AllocMMIO = XenPCI_AllocMMIO;
  10.107 +  WDF_QUERY_INTERFACE_CONFIG_INIT(&qiConfig, (PINTERFACE)&ChildDeviceData->XenInterface, &GUID_XEN_IFACE_XEN, NULL);
  10.108    status = WdfDeviceAddQueryInterface(ChildDevice, &qiConfig);
  10.109    if (!NT_SUCCESS(status)) {
  10.110      return status;
  10.111    }
  10.112 -#endif
  10.113 +*/
  10.114  
  10.115 -  RtlZeroMemory(&GntTblInterface, sizeof(GntTblInterface));
  10.116 -  GntTblInterface.InterfaceHeader.Size = sizeof(GntTblInterface);
  10.117 -  GntTblInterface.InterfaceHeader.Version = 1;
  10.118 -  GntTblInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
  10.119 -  GntTblInterface.InterfaceHeader.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
  10.120 -  GntTblInterface.InterfaceHeader.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
  10.121 -  GntTblInterface.GrantAccess = GntTbl_GrantAccess;
  10.122 -  GntTblInterface.EndAccess = GntTbl_EndAccess;
  10.123 -  WDF_QUERY_INTERFACE_CONFIG_INIT(&qiConfig, (PINTERFACE)&GntTblInterface, &GUID_XEN_IFACE_GNTTBL, NULL);
  10.124 +  ChildDeviceData->GntTblInterface.InterfaceHeader.Size = sizeof(ChildDeviceData->GntTblInterface);
  10.125 +  ChildDeviceData->GntTblInterface.InterfaceHeader.Version = 1;
  10.126 +  ChildDeviceData->GntTblInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
  10.127 +  ChildDeviceData->GntTblInterface.InterfaceHeader.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
  10.128 +  ChildDeviceData->GntTblInterface.InterfaceHeader.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
  10.129 +  ChildDeviceData->GntTblInterface.GrantAccess = GntTbl_GrantAccess;
  10.130 +  ChildDeviceData->GntTblInterface.EndAccess = GntTbl_EndAccess;
  10.131 +  WDF_QUERY_INTERFACE_CONFIG_INIT(&qiConfig, (PINTERFACE)&ChildDeviceData->GntTblInterface, &GUID_XEN_IFACE_GNTTBL, NULL);
  10.132    status = WdfDeviceAddQueryInterface(ChildDevice, &qiConfig);
  10.133    if (!NT_SUCCESS(status)) {
  10.134      return status;
  10.135    }
  10.136  
  10.137 -  RtlZeroMemory(&XenBusInterface, sizeof(XenBusInterface));
  10.138 -
  10.139 -  XenBusInterface.InterfaceHeader.Size = sizeof(XenBusInterface);
  10.140 -  XenBusInterface.InterfaceHeader.Version = 1;
  10.141 -  XenBusInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
  10.142 +  ChildDeviceData->XenBusInterface.InterfaceHeader.Size = sizeof(ChildDeviceData->XenBusInterface);
  10.143 +  ChildDeviceData->XenBusInterface.InterfaceHeader.Version = 1;
  10.144 +  ChildDeviceData->XenBusInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
  10.145    //XenBusInterface.InterfaceHeader.Context = ExAllocatePoolWithTag(NonPagedPool, (strlen(XenIdentificationDesc->Path) + 1), XENPCI_POOL_TAG);
  10.146    //strcpy(XenBusInterface.InterfaceHeader.Context, XenIdentificationDesc->Path);
  10.147 -  XenBusInterface.Read = XenBus_Read;
  10.148 -  XenBusInterface.Write = XenBus_Write;
  10.149 -  XenBusInterface.Printf = XenBus_Printf;
  10.150 -  XenBusInterface.StartTransaction = XenBus_StartTransaction;
  10.151 -  XenBusInterface.EndTransaction = XenBus_EndTransaction;
  10.152 -  XenBusInterface.List = XenBus_List;
  10.153 -  XenBusInterface.AddWatch = XenBus_AddWatch;
  10.154 -  XenBusInterface.RemWatch = XenBus_RemWatch;
  10.155 -  WDF_QUERY_INTERFACE_CONFIG_INIT(&qiConfig, (PINTERFACE)&XenBusInterface, &GUID_XEN_IFACE_XENBUS, NULL);
  10.156 +  ChildDeviceData->XenBusInterface.Read = XenBus_Read;
  10.157 +  ChildDeviceData->XenBusInterface.Write = XenBus_Write;
  10.158 +  ChildDeviceData->XenBusInterface.Printf = XenBus_Printf;
  10.159 +  ChildDeviceData->XenBusInterface.StartTransaction = XenBus_StartTransaction;
  10.160 +  ChildDeviceData->XenBusInterface.EndTransaction = XenBus_EndTransaction;
  10.161 +  ChildDeviceData->XenBusInterface.List = XenBus_List;
  10.162 +  ChildDeviceData->XenBusInterface.AddWatch = XenBus_AddWatch;
  10.163 +  ChildDeviceData->XenBusInterface.RemWatch = XenBus_RemWatch;
  10.164 +  WDF_QUERY_INTERFACE_CONFIG_INIT(&qiConfig, (PINTERFACE)&ChildDeviceData->XenBusInterface, &GUID_XEN_IFACE_XENBUS, NULL);
  10.165    status = WdfDeviceAddQueryInterface(ChildDevice, &qiConfig);
  10.166    if (!NT_SUCCESS(status)) {
  10.167      return status;
  10.168 @@ -822,7 +817,7 @@ XenPCI_XenBusWatchHandler(char *Path, PV
  10.169          {
  10.170            //KdPrint((__DRIVER_NAME "     Child Path = %s (Match - WatchHandler = %08x)\n", ChildDeviceData->BasePath, ChildDeviceData->WatchHandler));
  10.171            if (ChildDeviceData->WatchHandler != NULL)
  10.172 -            ChildDeviceData->WatchHandler(Path, NULL);
  10.173 +            ChildDeviceData->WatchHandler(Path, ChildDeviceData->WatchContext);
  10.174          }
  10.175          else
  10.176          {
    11.1 --- a/xenvbd/sources	Fri Dec 14 13:30:56 2007 -0800
    11.2 +++ b/xenvbd/sources	Mon Dec 17 14:19:39 2007 -0800
    11.3 @@ -1,13 +1,12 @@
    11.4  TARGETNAME=XENVBD
    11.5  TARGETTYPE=DRIVER
    11.6 -TARGETPATH=..\Target
    11.7 -
    11.8 +TARGETPATH=..\Target\$(DDK_TARGET_OS)
    11.9 +VERSION=0.4.0.122
   11.10 +KMDF_VERSION=1
   11.11  MSC_WARNING_LEVEL=/W4
   11.12 -
   11.13 +INF_NAME=xenvbd
   11.14  INCLUDES = ..\common\include;..\common\include\public
   11.15 -
   11.16 -NO_BINPLACE=1
   11.17 -KMDF_VERSION=1
   11.18 -TARGETLIBS=$(DDK_LIB_PATH)\ntstrsafe.lib $(DDK_LIB_PATH)\scsiport.lib
   11.19 -
   11.20 +TARGETLIBS=$(TARGETLIBS) $(DDK_LIB_PATH)\ntstrsafe.lib $(DDK_LIB_PATH)\scsiport.lib
   11.21 +MISCFILES=..\Target\$(DDK_TARGET_OS)\$(INF_NAME).inf
   11.22 +NTTARGETFILES=
   11.23  SOURCES=xenvbd.c
    12.1 --- a/xenvbd/xenvbd.c	Fri Dec 14 13:30:56 2007 -0800
    12.2 +++ b/xenvbd/xenvbd.c	Mon Dec 17 14:19:39 2007 -0800
    12.3 @@ -1,482 +1,102 @@
    12.4  #include "xenvbd.h"
    12.5  #include <io/blkif.h>
    12.6 -#include <srb.h>
    12.7  #include <scsi.h>
    12.8  #include <ntddscsi.h>
    12.9  #include <ntdddisk.h>
   12.10  #include <stdlib.h>
   12.11  #include <xen_public.h>
   12.12 -#include <gnttbl_public.h>
   12.13  #include <io/xenbus.h>
   12.14 -#include <ntddft.h>
   12.15  
   12.16  #define wmb() KeMemoryBarrier()
   12.17  #define mb() KeMemoryBarrier()
   12.18  
   12.19 +#define BUF_PAGES_PER_SRB 11
   12.20 +
   12.21  DRIVER_INITIALIZE DriverEntry;
   12.22  
   12.23 -static NTSTATUS
   12.24 -XenVbd_AddDevice(WDFDRIVER Driver, PWDFDEVICE_INIT DeviceInit);
   12.25 -static NTSTATUS
   12.26 -XenVbd_PrepareHardware(WDFDEVICE hDevice, WDFCMRESLIST Resources, WDFCMRESLIST ResourcesTranslated);
   12.27 -static NTSTATUS
   12.28 -XenVbd_ReleaseHardware(WDFDEVICE Device, WDFCMRESLIST ResourcesTranslated);
   12.29 -static NTSTATUS
   12.30 -XenVbd_D0Entry(WDFDEVICE Device, WDF_POWER_DEVICE_STATE PreviousState);
   12.31 -static NTSTATUS
   12.32 -XenVbd_D0EntryPostInterruptsEnabled(WDFDEVICE Device, WDF_POWER_DEVICE_STATE PreviousState);
   12.33 -static NTSTATUS
   12.34 -XenVbd_D0Exit(WDFDEVICE Device, WDF_POWER_DEVICE_STATE TargetState);
   12.35 -static NTSTATUS
   12.36 -XenVbd_DeviceUsageNotification(WDFDEVICE Device, WDF_SPECIAL_FILE_TYPE NotificationType, BOOLEAN IsInNotificationPath);
   12.37 +static ULONG
   12.38 +XenVbd_HwScsiFindAdapter(PVOID DeviceExtension, PVOID HwContext, PVOID BusInformation, PCHAR ArgumentString, PPORT_CONFIGURATION_INFORMATION ConfigInfo, PBOOLEAN Again);
   12.39 +static BOOLEAN
   12.40 +XenVbd_HwScsiInitialize(PVOID DeviceExtension);
   12.41 +static BOOLEAN
   12.42 +XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK Srb);
   12.43 +static BOOLEAN
   12.44 +XenVbd_HwScsiInterrupt(PVOID DeviceExtension);
   12.45 +static BOOLEAN
   12.46 +XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId);
   12.47 +static BOOLEAN
   12.48 +XenVbd_HwScsiAdapterState(PVOID DeviceExtension, PVOID Context, BOOLEAN SaveState);
   12.49 +static SCSI_ADAPTER_CONTROL_STATUS
   12.50 +XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters);
   12.51  
   12.52 -static VOID
   12.53 -XenVbd_IoDefault(WDFQUEUE Queue, WDFREQUEST Request);
   12.54 -static VOID
   12.55 -XenVbd_IoRead(WDFQUEUE Queue, WDFREQUEST Request, size_t  Length);
   12.56 -static VOID
   12.57 -XenVbd_IoWrite(WDFQUEUE Queue, WDFREQUEST Request, size_t  Length);
   12.58 -static VOID
   12.59 -XenVbd_IoDeviceControl(WDFQUEUE Queue, WDFREQUEST Request, IN size_t  OutputBufferLength, size_t  InputBufferLength, ULONG IoControlCode);
   12.60 -
   12.61 -static NTSTATUS
   12.62 -XenVbd_ChildListCreateDevice(WDFCHILDLIST ChildList, PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER IdentificationDescription, PWDFDEVICE_INIT ChildInit);
   12.63 -//static NTSTATUS
   12.64 -//XenVbd_DeviceResourceRequirementsQuery(WDFDEVICE Device, WDFIORESREQLIST IoResourceRequirementsList);
   12.65 -static NTSTATUS
   12.66 -XenVbd_Child_PreprocessWdmIrpSCSI(WDFDEVICE Device, PIRP Irp);
   12.67 -static NTSTATUS
   12.68 -XenVbd_Child_PreprocessWdmIrpDEVICE_CONTROL(WDFDEVICE Device, PIRP Irp);
   12.69 -static NTSTATUS
   12.70 -XenVbd_Child_PreprocessWdmIrpSomethingSomething(WDFDEVICE Device, PIRP Irp);
   12.71 -
   12.72 -static VOID
   12.73 -XenVbd_HotPlugHandler(char *Path, PVOID Data);
   12.74 +//static XEN_IFACE_EVTCHN EvtChnInterface;
   12.75 +//static XEN_IFACE_XENBUS XenBusInterface;
   12.76  
   12.77  #ifdef ALLOC_PRAGMA
   12.78  #pragma alloc_text (INIT, DriverEntry)
   12.79 -#pragma alloc_text (PAGE, XenVbd_AddDevice)
   12.80  #endif
   12.81  
   12.82 -#pragma warning(disable: 4127) // disable conditional expression is constant
   12.83 -
   12.84 -LIST_ENTRY DeviceListHead;
   12.85 -XEN_IFACE_EVTCHN EvtChnInterface;
   12.86 -XEN_IFACE_XENBUS XenBusInterface;
   12.87 -XEN_IFACE_XEN XenInterface;
   12.88 -XEN_IFACE_GNTTBL GntTblInterface;
   12.89 -
   12.90  static BOOLEAN AutoEnumerate;
   12.91  
   12.92  NTSTATUS
   12.93  DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
   12.94  {
   12.95 -  WDF_DRIVER_CONFIG config;
   12.96 -  ULONG status;
   12.97 -  UNICODE_STRING RegKeyName;
   12.98 -  UNICODE_STRING RegValueName;
   12.99 -  HANDLE RegHandle;
  12.100 -  OBJECT_ATTRIBUTES RegObjectAttributes;
  12.101 -  char Buf[200];
  12.102 -  ULONG BufLen = 200;
  12.103 -  PKEY_VALUE_PARTIAL_INFORMATION KeyPartialValue;
  12.104 -  int State = 0;
  12.105 -  int StartPos = 0;
  12.106 -  WCHAR *SystemStartOptions;
  12.107 -  size_t SystemStartOptionsLen;
  12.108 -  size_t i;
  12.109 +  ULONG Status;
  12.110 +  HW_INITIALIZATION_DATA HwInitializationData;
  12.111  
  12.112    KdPrint((__DRIVER_NAME " --> DriverEntry\n"));
  12.113  
  12.114 -  WDF_DRIVER_CONFIG_INIT(&config, XenVbd_AddDevice);
  12.115 -  status = WdfDriverCreate(
  12.116 -                      DriverObject,
  12.117 -                      RegistryPath,
  12.118 -                      WDF_NO_OBJECT_ATTRIBUTES,
  12.119 -                      &config,
  12.120 -                      WDF_NO_HANDLE);
  12.121 -  if(!NT_SUCCESS(status))
  12.122 -  {
  12.123 -    KdPrint((__DRIVER_NAME " WdfDriverCreate failed with status 0x%08x\n", status));
  12.124 -  }
  12.125 +  RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
  12.126  
  12.127 -  RtlInitUnicodeString(&RegKeyName, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control");
  12.128 -  InitializeObjectAttributes(&RegObjectAttributes, &RegKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);
  12.129 -  status = ZwOpenKey(&RegHandle, KEY_READ, &RegObjectAttributes);
  12.130 -  if(!NT_SUCCESS(status))
  12.131 -  {
  12.132 -    KdPrint((__DRIVER_NAME "     ZwOpenKey returned %08x\n", status));
  12.133 -  }
  12.134 +  HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
  12.135 +  HwInitializationData.AdapterInterfaceType = Internal; //PNPBus;
  12.136 +  HwInitializationData.HwInitialize = XenVbd_HwScsiInitialize;
  12.137 +  HwInitializationData.HwStartIo = XenVbd_HwScsiStartIo;
  12.138 +  HwInitializationData.HwInterrupt = XenVbd_HwScsiInterrupt;
  12.139 +  HwInitializationData.HwFindAdapter = XenVbd_HwScsiFindAdapter;
  12.140 +  HwInitializationData.HwResetBus = XenVbd_HwScsiResetBus;
  12.141 +  HwInitializationData.HwDmaStarted = NULL;
  12.142 +  HwInitializationData.HwAdapterState = NULL;
  12.143 +  HwInitializationData.DeviceExtensionSize = sizeof(XENVBD_DEVICE_DATA);
  12.144 +  HwInitializationData.SpecificLuExtensionSize = 0;
  12.145 +  HwInitializationData.SrbExtensionSize = 0;
  12.146 +  HwInitializationData.NumberOfAccessRanges = 1;
  12.147  
  12.148 -  RtlInitUnicodeString(&RegValueName, L"SystemStartOptions");
  12.149 -  status = ZwQueryValueKey(RegHandle, &RegValueName, KeyValuePartialInformation, Buf, BufLen, &BufLen);
  12.150 -  if(!NT_SUCCESS(status))
  12.151 -  {
  12.152 -    KdPrint((__DRIVER_NAME "     ZwQueryKeyValue returned %08x\n", status));
  12.153 -  }
  12.154 -  //KdPrint((__DRIVER_NAME "     BufLen = %d\n", BufLen));
  12.155 -  KeyPartialValue = (PKEY_VALUE_PARTIAL_INFORMATION)Buf;
  12.156 -  KdPrint((__DRIVER_NAME "     Buf = %ws\n", KeyPartialValue->Data));
  12.157 -  SystemStartOptions = (WCHAR *)KeyPartialValue->Data;
  12.158 +  //HwInitializationData.MapBuffers = FALSE;
  12.159 +  HwInitializationData.MapBuffers = TRUE;
  12.160  
  12.161 -  AutoEnumerate = FALSE;
  12.162 +  HwInitializationData.NeedPhysicalAddresses = FALSE;
  12.163 +//  HwInitializationData.NeedPhysicalAddresses = TRUE;
  12.164  
  12.165 -  RtlStringCbLengthW(SystemStartOptions, KeyPartialValue->DataLength, &SystemStartOptionsLen);
  12.166 +  HwInitializationData.TaggedQueuing = TRUE; //FALSE;
  12.167 +  HwInitializationData.AutoRequestSense = FALSE;
  12.168 +  HwInitializationData.MultipleRequestPerLu = FALSE;
  12.169 +  HwInitializationData.ReceiveEvent = FALSE; // check this
  12.170 +  HwInitializationData.VendorIdLength = 0;
  12.171 +  HwInitializationData.VendorId = NULL;
  12.172 +  HwInitializationData.DeviceIdLength = 0;
  12.173 +  HwInitializationData.DeviceId = NULL;
  12.174 +  HwInitializationData.HwAdapterControl = XenVbd_HwScsiAdapterControl;
  12.175  
  12.176 -  for (i = 0; i <= SystemStartOptionsLen/2; i++)
  12.177 +  Status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
  12.178 +
  12.179 +  if(!NT_SUCCESS(Status))
  12.180    {
  12.181 -    //KdPrint((__DRIVER_NAME "     pos = %d, state = %d, char = '%wc' (%d)\n", i, State, SystemStartOptions[i], SystemStartOptions[i]));
  12.182 -    
  12.183 -    switch (State)
  12.184 -    {
  12.185 -    case 0:
  12.186 -      if (SystemStartOptions[i] == L'G')
  12.187 -      {
  12.188 -        StartPos = i;
  12.189 -        State = 2;
  12.190 -      } else if (SystemStartOptions[i] != L' ')
  12.191 -      {
  12.192 -        State = 1;
  12.193 -      }
  12.194 -      break;
  12.195 -    case 1:
  12.196 -      if (SystemStartOptions[i] == L' ')
  12.197 -        State = 0;
  12.198 -      break;
  12.199 -    case 2:
  12.200 -      if (SystemStartOptions[i] == L'P')
  12.201 -        State = 3;
  12.202 -      else
  12.203 -        State = 0;
  12.204 -      break;
  12.205 -    case 3:
  12.206 -      if (SystemStartOptions[i] == L'L')
  12.207 -        State = 4;
  12.208 -      else
  12.209 -        State = 0;
  12.210 -      break;
  12.211 -    case 4:
  12.212 -      if (SystemStartOptions[i] == L'P')
  12.213 -        State = 5;
  12.214 -      else
  12.215 -        State = 0;
  12.216 -      break;
  12.217 -    case 5:
  12.218 -      if (SystemStartOptions[i] == L'V')
  12.219 -        State = 6;
  12.220 -      else
  12.221 -        State = 0;
  12.222 -      break;
  12.223 -    case 6:
  12.224 -      if (SystemStartOptions[i] == L' ' || SystemStartOptions[i] == 0)
  12.225 -        AutoEnumerate = TRUE;
  12.226 -      State = 0;
  12.227 -      break;
  12.228 -    }
  12.229 +    KdPrint((__DRIVER_NAME " ScsiPortInitialize failed with status 0x%08x\n", Status));
  12.230    }
  12.231  
  12.232 -  KdPrint((__DRIVER_NAME "     AutoEnumerate = %d\n", AutoEnumerate));
  12.233 -
  12.234    KdPrint((__DRIVER_NAME " <-- DriverEntry\n"));
  12.235  
  12.236 -  return status;
  12.237 -}
  12.238 -
  12.239 -static WDFDEVICE GlobalDevice;
  12.240 -static PDEVICE_OBJECT Pdo;
  12.241 -
  12.242 -static NTSTATUS
  12.243 -XenVbd_AddDevice(WDFDRIVER Driver, PWDFDEVICE_INIT DeviceInit)
  12.244 -{
  12.245 -  WDF_CHILD_LIST_CONFIG ChildListConfig;
  12.246 -  NTSTATUS status;
  12.247 -  WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
  12.248 -
  12.249 -  UNREFERENCED_PARAMETER(Driver);
  12.250 -
  12.251 -  KdPrint((__DRIVER_NAME " --> DeviceAdd\n"));
  12.252 -
  12.253 -  Pdo = WdfFdoInitWdmGetPhysicalDevice(DeviceInit);
  12.254 -
  12.255 -  //WdfDeviceInitSetDeviceType(DeviceInit, FILE_DEVICE_BUS_EXTENDER);
  12.256 -  WDF_CHILD_LIST_CONFIG_INIT(&ChildListConfig, sizeof(XENVBD_DEVICE_IDENTIFICATION_DESCRIPTION), XenVbd_ChildListCreateDevice);
  12.257 -  WdfFdoInitSetDefaultChildListConfig(DeviceInit, &ChildListConfig, WDF_NO_OBJECT_ATTRIBUTES);
  12.258 -
  12.259 -  WdfDeviceInitSetDeviceType(DeviceInit, FILE_DEVICE_CONTROLLER);
  12.260 -  //WdfDeviceInitSetIoType(DeviceInit, WdfDeviceIoBuffered);
  12.261 -  WdfDeviceInitSetExclusive(DeviceInit, FALSE);
  12.262 -  
  12.263 -  WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
  12.264 -  pnpPowerCallbacks.EvtDevicePrepareHardware = XenVbd_PrepareHardware;
  12.265 -  pnpPowerCallbacks.EvtDeviceReleaseHardware = XenVbd_ReleaseHardware;
  12.266 -  pnpPowerCallbacks.EvtDeviceD0Entry = XenVbd_D0Entry;
  12.267 -  pnpPowerCallbacks.EvtDeviceD0EntryPostInterruptsEnabled = XenVbd_D0EntryPostInterruptsEnabled;
  12.268 -  pnpPowerCallbacks.EvtDeviceD0Exit = XenVbd_D0Exit;
  12.269 -  pnpPowerCallbacks.EvtDeviceUsageNotification = XenVbd_DeviceUsageNotification;
  12.270 -  WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);
  12.271 -
  12.272 -  /*initialize storage for the device context*/
  12.273 -  //WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, XENVBD_DEVICE_DATA);
  12.274 -
  12.275 -  //WdfDeviceInitSetPowerNotPageable(DeviceInit);
  12.276 -
  12.277 -  /*create a device instance.*/
  12.278 -  status = WdfDeviceCreate(&DeviceInit, WDF_NO_OBJECT_ATTRIBUTES, &GlobalDevice);  
  12.279 -  if(!NT_SUCCESS(status))
  12.280 -  {
  12.281 -    KdPrint((__DRIVER_NAME "WdfDeviceCreate failed with status 0x%08x\n", status));
  12.282 -    return status;
  12.283 -  }
  12.284 -
  12.285 -  WdfDeviceSetSpecialFileSupport(GlobalDevice, WdfSpecialFilePaging, TRUE);
  12.286 -  WdfDeviceSetSpecialFileSupport(GlobalDevice, WdfSpecialFileHibernation, TRUE);
  12.287 -  WdfDeviceSetSpecialFileSupport(GlobalDevice, WdfSpecialFileDump, TRUE);
  12.288 -  
  12.289 -  status = STATUS_SUCCESS;
  12.290 -
  12.291 -  KdPrint((__DRIVER_NAME " <-- DeviceAdd\n"));
  12.292 -  return status;
  12.293 -}
  12.294 -
  12.295 -static NTSTATUS
  12.296 -XenVbd_PrepareHardware(
  12.297 -  IN WDFDEVICE    Device,
  12.298 -  IN WDFCMRESLIST ResourceList,
  12.299 -  IN WDFCMRESLIST ResourceListTranslated)
  12.300 -{
  12.301 -  NTSTATUS status = STATUS_SUCCESS;
  12.302 -
  12.303 -  UNREFERENCED_PARAMETER(ResourceList);
  12.304 -  UNREFERENCED_PARAMETER(ResourceListTranslated);
  12.305 -
  12.306 -  //KdPrint((__DRIVER_NAME " --> EvtDevicePrepareHardware\n"));
  12.307 -
  12.308 -  status = WdfFdoQueryForInterface(Device, &GUID_XEN_IFACE_EVTCHN, (PINTERFACE) &EvtChnInterface, sizeof(XEN_IFACE_EVTCHN), 1, NULL);
  12.309 -  if(!NT_SUCCESS(status))
  12.310 -  {
  12.311 -    KdPrint((__DRIVER_NAME "     WdfFdoQueryForInterface (EvtChn) failed with status 0x%08x\n", status));
  12.312 -  }
  12.313 -
  12.314 -  status = WdfFdoQueryForInterface(Device, &GUID_XEN_IFACE_XENBUS, (PINTERFACE)&XenBusInterface, sizeof(XEN_IFACE_XENBUS), 1, NULL);
  12.315 -  if(!NT_SUCCESS(status))
  12.316 -  {
  12.317 -    KdPrint((__DRIVER_NAME "     WdfFdoQueryForInterface (XenBus) failed with status 0x%08x\n", status));
  12.318 -  }
  12.319 -
  12.320 -#if 0 // not used yet
  12.321 -  status = WdfFdoQueryForInterface(Device, &GUID_XEN_IFACE_XEN, (PINTERFACE)&XenInterface, sizeof(XEN_IFACE_XEN), 1, NULL);
  12.322 -  if(!NT_SUCCESS(status))
  12.323 -  {
  12.324 -    KdPrint((__DRIVER_NAME "     WdfFdoQueryForInterface (Xen) failed with status 0x%08x\n", status));
  12.325 -  }
  12.326 -#endif
  12.327 -
  12.328 -  status = WdfFdoQueryForInterface(Device, &GUID_XEN_IFACE_GNTTBL, (PINTERFACE)&GntTblInterface, sizeof(XEN_IFACE_GNTTBL), 1, NULL);
  12.329 -  if(!NT_SUCCESS(status))
  12.330 -  {
  12.331 -    KdPrint((__DRIVER_NAME "     WdfFdoQueryForInterface (GntTbl) failed with status 0x%08x\n", status));
  12.332 -  }
  12.333 -  
  12.334 -  //KdPrint((__DRIVER_NAME " <-- EvtDevicePrepareHardware\n"));
  12.335 -
  12.336 -  InitializeListHead(&DeviceListHead);
  12.337 -
  12.338 -  return status;
  12.339 -}
  12.340 -
  12.341 -static NTSTATUS
  12.342 -XenVbd_ReleaseHardware(WDFDEVICE Device, WDFCMRESLIST ResourcesTranslated)
  12.343 -{
  12.344 -  UNREFERENCED_PARAMETER(Device);
  12.345 -  UNREFERENCED_PARAMETER(ResourcesTranslated);
  12.346 -
  12.347 -  // release interfaces here...
  12.348 -
  12.349 -  //XenVbd_Close();
  12.350 -
  12.351 -  return STATUS_SUCCESS;
  12.352 -}
  12.353 -
  12.354 -static NTSTATUS
  12.355 -XenVbd_D0Entry(
  12.356 -    IN WDFDEVICE  Device,
  12.357 -    IN WDF_POWER_DEVICE_STATE PreviousState
  12.358 -    )
  12.359 -{
  12.360 -  NTSTATUS status = STATUS_SUCCESS;
  12.361 -
  12.362 -  UNREFERENCED_PARAMETER(Device);
  12.363 -  UNREFERENCED_PARAMETER(PreviousState);
  12.364 -
  12.365 -  //KdPrint((__DRIVER_NAME " --> EvtDeviceD0Entry\n"));
  12.366 -
  12.367 -  //KdPrint((__DRIVER_NAME " <-- EvtDeviceD0Entry\n"));
  12.368 -
  12.369 -  return status;
  12.370 +  return Status;
  12.371  }
  12.372  
  12.373 -static LONG EnumeratedDevices;
  12.374 -static KEVENT WaitDevicesEvent;
  12.375 -
  12.376 -static NTSTATUS
  12.377 -XenVbd_D0EntryPostInterruptsEnabled(WDFDEVICE Device, WDF_POWER_DEVICE_STATE PreviousState)
  12.378 -{
  12.379 -  //OBJECT_ATTRIBUTES oa;
  12.380 -  NTSTATUS status = STATUS_SUCCESS;
  12.381 -  //HANDLE nothing;
  12.382 -  //char *response;
  12.383 -  PXENPCI_XEN_DEVICE_DATA PdoDeviceData;
  12.384 -  char **VbdDevices;
  12.385 -  char *msg;
  12.386 -  char buffer[128];
  12.387 -  int i;
  12.388 -  LARGE_INTEGER WaitTimeout;
  12.389 -
  12.390 -  UNREFERENCED_PARAMETER(Device);
  12.391 -  UNREFERENCED_PARAMETER(PreviousState);
  12.392 -
  12.393 -  //KdPrint((__DRIVER_NAME " --> EvtDeviceD0EntryPostInterruptsEnabled\n"));
  12.394 -
  12.395 -  PdoDeviceData = (PXENPCI_XEN_DEVICE_DATA)Pdo->DeviceExtension; //GetXenDeviceData(Device);
  12.396 -
  12.397 -  //KdPrint((__DRIVER_NAME "     BasePath = %s\n", PdoDeviceData->BasePath));
  12.398 -  PdoDeviceData->WatchHandler = XenVbd_HotPlugHandler;
  12.399 -
  12.400 -  EnumeratedDevices = 0;
  12.401 -  KeInitializeEvent(&WaitDevicesEvent, SynchronizationEvent, FALSE);  
  12.402 -
  12.403 -  // TODO: Should probably do this in an EvtChildListScanForChildren
  12.404 -  if (AutoEnumerate)
  12.405 -  {
  12.406 -    msg = XenBusInterface.List(XenBusInterface.InterfaceHeader.Context,
  12.407 -      XBT_NIL, "device/vbd", &VbdDevices);
  12.408 -    if (!msg) {
  12.409 -      for (i = 0; VbdDevices[i]; i++)
  12.410 -      {
  12.411 -        KdPrint((__DRIVER_NAME "     found existing vbd device %s\n", VbdDevices[i]));
  12.412 -        RtlStringCbPrintfA(buffer, ARRAY_SIZE(buffer), "device/vbd/%s/state", VbdDevices[i]);
  12.413 -        XenVbd_HotPlugHandler(buffer, NULL);
  12.414 -        //ExFreePoolWithTag(bdDevices[i], XENPCI_POOL_TAG);
  12.415 -      }
  12.416 -      KdPrint((__DRIVER_NAME "     Waiting for devices to be enumerated\n"));
  12.417 -      while (EnumeratedDevices != i)
  12.418 -      {
  12.419 -        WaitTimeout.QuadPart = -600000000;
  12.420 -        if (KeWaitForSingleObject(&WaitDevicesEvent, Executive, KernelMode, FALSE, &WaitTimeout) == STATUS_TIMEOUT)
  12.421 -        {
  12.422 -          KdPrint((__DRIVER_NAME "     Wait timed out\n"));
  12.423 -          break;
  12.424 -        }
  12.425 -        KdPrint((__DRIVER_NAME "     %d out of %d devices enumerated\n", EnumeratedDevices, i));
  12.426 -      }  
  12.427 -    }
  12.428 -  }
  12.429 -
  12.430 -  //KdPrint((__DRIVER_NAME " <-- EvtDeviceD0EntryPostInterruptsEnabled\n"));
  12.431 -
  12.432 -  return status;
  12.433 -}
  12.434 -
  12.435 -static NTSTATUS
  12.436 -XenVbd_D0Exit(
  12.437 -    IN WDFDEVICE Device,
  12.438 -    IN WDF_POWER_DEVICE_STATE  TargetState
  12.439 -    )
  12.440 -{
  12.441 -  NTSTATUS status = STATUS_SUCCESS;
  12.442 -  //char *response;
  12.443 -
  12.444 -  UNREFERENCED_PARAMETER(Device);
  12.445 -  UNREFERENCED_PARAMETER(TargetState);
  12.446 -
  12.447 -  //KdPrint((__DRIVER_NAME " --> EvtDeviceD0Exit\n"));
  12.448 -
  12.449 -  //response = XenBusInterface.RemWatch(XBT_NIL, XenBusInterface.InterfaceHeader.Context, XenVbd_HotPlugHandler, NULL);
  12.450 -
  12.451 -  //KdPrint((__DRIVER_NAME " <-- EvtDeviceD0Exit\n"));
  12.452 -
  12.453 -  return status;
  12.454 -}
  12.455 -
  12.456 -static NTSTATUS
  12.457 -XenVbd_DeviceUsageNotification(
  12.458 -  WDFDEVICE Device,
  12.459 -  WDF_SPECIAL_FILE_TYPE NotificationType,
  12.460 -  BOOLEAN IsInNotificationPath)
  12.461 -{
  12.462 -  UNREFERENCED_PARAMETER(Device);
  12.463 -
  12.464 -//  KdPrint((__DRIVER_NAME " --> DeviceUsageNotification\n"));
  12.465 -
  12.466 -  switch (NotificationType)
  12.467 -  {
  12.468 -  case WdfSpecialFilePaging:
  12.469 -    KdPrint((__DRIVER_NAME "     NotificationType = WdfSpecialFilePaging, Using = %d\n", IsInNotificationPath));
  12.470 -    break;
  12.471 -  case WdfSpecialFileHibernation:
  12.472 -    KdPrint((__DRIVER_NAME "     NotificationType = WdfSpecialFileHibernation, Using = %d\n", IsInNotificationPath));
  12.473 -    break;
  12.474 -  case WdfSpecialFileDump:
  12.475 -    KdPrint((__DRIVER_NAME "     NotificationType = WdfSpecialFileDump, Using = %d\n", IsInNotificationPath));
  12.476 -    break;
  12.477 -  default:
  12.478 -    KdPrint((__DRIVER_NAME "     NotificationType = %d, Using = %d\n", NotificationType, IsInNotificationPath));
  12.479 -    break;
  12.480 -  }
  12.481 -//  KdPrint((__DRIVER_NAME " <-- DeviceUsageNotification\n"));
  12.482 -
  12.483 -  return TRUE;
  12.484 -}
  12.485 -
  12.486 -static VOID 
  12.487 -XenVbd_IoDefault(
  12.488 -    IN WDFQUEUE  Queue,
  12.489 -    IN WDFREQUEST  Request
  12.490 -    )
  12.491 -{
  12.492 -  UNREFERENCED_PARAMETER(Queue);
  12.493 -
  12.494 -  //KdPrint((__DRIVER_NAME " --> EvtDeviceIoDefault\n"));
  12.495 -
  12.496 -  WdfRequestComplete(Request, STATUS_NOT_IMPLEMENTED);
  12.497 -
  12.498 -  //KdPrint((__DRIVER_NAME " <-- EvtDeviceIoDefault\n"));
  12.499 -}
  12.500 -
  12.501 -static __inline uint64_t
  12.502 -GET_ID_FROM_FREELIST(PXENVBD_CHILD_DEVICE_DATA ChildDeviceData)
  12.503 -{
  12.504 -  uint64_t free;
  12.505 -
  12.506 -  free = ChildDeviceData->shadow_free;
  12.507 -
  12.508 -  //KdPrint((__DRIVER_NAME "     A free = %d\n", free));
  12.509 -  
  12.510 -  ChildDeviceData->shadow_free = ChildDeviceData->shadow[free].req.id;
  12.511 -
  12.512 -  //KdPrint((__DRIVER_NAME "     A shadow_free now = %d\n", ChildDeviceData->shadow_free));
  12.513 -
  12.514 -  ChildDeviceData->shadow[free].req.id = 0x0fffffee; /* debug */
  12.515 -  return free;
  12.516 -}
  12.517 -
  12.518 -static __inline VOID
  12.519 -ADD_ID_TO_FREELIST(PXENVBD_CHILD_DEVICE_DATA ChildDeviceData, uint64_t Id)
  12.520 -{
  12.521 -  ChildDeviceData->shadow[Id].req.id  = ChildDeviceData->shadow_free;
  12.522 -  ChildDeviceData->shadow[Id].Irp = NULL;
  12.523 -  ChildDeviceData->shadow_free = Id;
  12.524 -}
  12.525 -
  12.526 -static VOID
  12.527 -XenVbd_PutIrpOnRing(WDFDEVICE Device, PIRP Irp);
  12.528 -
  12.529  static PMDL
  12.530  AllocatePages(int Pages)
  12.531  {
  12.532    PMDL Mdl;
  12.533    PVOID Buf;
  12.534  
  12.535 +  //KdPrint((__DRIVER_NAME " --- AllocatePages IRQL = %d\n", KeGetCurrentIrql()));
  12.536    Buf = ExAllocatePoolWithTag(NonPagedPool, Pages * PAGE_SIZE, XENVBD_POOL_TAG);
  12.537    if (Buf == NULL)
  12.538    {
  12.539 @@ -486,6 +106,7 @@ AllocatePages(int Pages)
  12.540    if (Mdl == NULL)
  12.541    {
  12.542      KdPrint((__DRIVER_NAME "     AllocatePages Failed at IoAllocateMdl\n"));
  12.543 +>>>>>>> /tmp/xenvbd.c~other.LRcZCH
  12.544    }
  12.545    MmBuildMdlForNonPagedPool(Mdl);
  12.546    
  12.547 @@ -502,158 +123,197 @@ static VOID
  12.548  FreePages(PMDL Mdl)
  12.549  {
  12.550    PVOID Buf = MmGetMdlVirtualAddress(Mdl);
  12.551 +  //KdPrint((__DRIVER_NAME " --- FreePages IRQL = %d\n", KeGetCurrentIrql()));
  12.552    //KdPrint((__DRIVER_NAME "     FreePages Failed at IoAllocateMdl\n"));
  12.553    //KdPrint((__DRIVER_NAME "     FreePages Buf = %08x\n", Buf));
  12.554    IoFreeMdl(Mdl);
  12.555    ExFreePoolWithTag(Buf, XENVBD_POOL_TAG);
  12.556  }
  12.557  
  12.558 +static ULONG
  12.559 +XenVbd_HwScsiFindAdapter(PVOID DeviceExtension, PVOID HwContext, PVOID BusInformation, PCHAR ArgumentString, PPORT_CONFIGURATION_INFORMATION ConfigInfo, PBOOLEAN Again)
  12.560 +{
  12.561 +  ULONG Status = SP_RETURN_FOUND;
  12.562 +  ULONG i;
  12.563 +  PACCESS_RANGE AccessRange;
  12.564 +  PXENVBD_DEVICE_DATA DeviceData = (PXENVBD_DEVICE_DATA)DeviceExtension;
  12.565 +
  12.566 +  KeInitializeSpinLock(&DeviceData->Lock);
  12.567 +  KdPrint((__DRIVER_NAME " --> HwScsiFindAdapter\n"));
  12.568 +  KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
  12.569 +
  12.570 +  KdPrint((__DRIVER_NAME "     BusInterruptLevel = %d\n", ConfigInfo->BusInterruptLevel));
  12.571 +  KdPrint((__DRIVER_NAME "     BusInterruptVector = %d\n", ConfigInfo->BusInterruptVector));
  12.572 +
  12.573 +  KdPrint((__DRIVER_NAME "     AccessRanges = %d\n", ConfigInfo->NumberOfAccessRanges));
  12.574 +
  12.575 +  for (i = 0; i < ConfigInfo->NumberOfAccessRanges; i++)
  12.576 +  {
  12.577 +    AccessRange = &(*(ConfigInfo->AccessRanges))[i];
  12.578 +    KdPrint((__DRIVER_NAME "     AccessRange %2d: RangeStart = %08x, RangeLength = %08x, RangeInMemory = %d\n", i, AccessRange->RangeStart.LowPart, AccessRange->RangeLength, AccessRange->RangeInMemory));
  12.579 +    switch (i)
  12.580 +    {
  12.581 +    case 0:
  12.582 +      DeviceData->XenDeviceData = (PXENPCI_XEN_DEVICE_DATA)AccessRange->RangeStart.QuadPart;
  12.583 +      KdPrint((__DRIVER_NAME "     Mapped to virtual address %08x\n", DeviceData->XenDeviceData));
  12.584 +      KdPrint((__DRIVER_NAME "     Magic = %08x\n", DeviceData->XenDeviceData->Magic));
  12.585 +      if (DeviceData->XenDeviceData->Magic == XEN_DATA_MAGIC)
  12.586 +      {
  12.587 +      }
  12.588 +      break;
  12.589 +    default:
  12.590 +      break;
  12.591 +    }
  12.592 +  }
  12.593 +
  12.594 +  ConfigInfo->NumberOfBuses = SCSI_BUSES;
  12.595 +  ConfigInfo->MaximumTransferLength = BUF_PAGES_PER_SRB * PAGE_SIZE;
  12.596 +  ConfigInfo->NumberOfPhysicalBreaks = BUF_PAGES_PER_SRB - 1; //11 - 1;
  12.597 +  ConfigInfo->ScatterGather = TRUE;
  12.598 +  ConfigInfo->Master = FALSE;
  12.599 +  ConfigInfo->AlignmentMask = 0;
  12.600 +  ConfigInfo->MaximumNumberOfLogicalUnits = 1;
  12.601 +  ConfigInfo->MaximumNumberOfTargets = SCSI_TARGETS_PER_BUS;
  12.602 +  //ConfigInfo->TaggedQueueing = TRUE;
  12.603 +
  12.604 +  *Again = FALSE;
  12.605 +
  12.606 +  KdPrint((__DRIVER_NAME " <-- HwScsiFindAdapter\n"));  
  12.607 +
  12.608 +  return Status;
  12.609 +}
  12.610 +
  12.611 +static __inline uint64_t
  12.612 +GET_ID_FROM_FREELIST(PXENVBD_TARGET_DATA TargetData)
  12.613 +{
  12.614 +  uint64_t free;
  12.615 +  free = TargetData->shadow_free;
  12.616 +  TargetData->shadow_free = TargetData->shadow[free].req.id;
  12.617 +  TargetData->shadow[free].req.id = 0x0fffffee; /* debug */
  12.618 +  return free;
  12.619 +}
  12.620 +
  12.621 +static __inline VOID
  12.622 +ADD_ID_TO_FREELIST(PXENVBD_TARGET_DATA TargetData, uint64_t Id)
  12.623 +{
  12.624 +  TargetData->shadow[Id].req.id  = TargetData->shadow_free;
  12.625 +  TargetData->shadow[Id].Srb = NULL;
  12.626 +  TargetData->shadow_free = Id;
  12.627 +}
  12.628 +
  12.629 +//static HANDLE XenVbd_ScsiPortThreadHandle;
  12.630 +//static KEVENT XenVbd_ScsiPortThreadEvent;
  12.631 +
  12.632  static VOID
  12.633 -XenVbd_DpcThreadProc(WDFDPC Dpc)
  12.634 +XenVbd_Interrupt(PKINTERRUPT Interrupt, PVOID DeviceExtension)
  12.635  {
  12.636 -  PIRP Irp;
  12.637 +  PXENVBD_TARGET_DATA TargetData = (PXENVBD_TARGET_DATA)DeviceExtension;
  12.638 +
  12.639 +  TargetData->PendingInterrupt = TRUE;
  12.640 +  return;
  12.641 +}
  12.642 +
  12.643 +static VOID
  12.644 +XenVbd_HwScsiInterruptTarget(PVOID DeviceExtension)
  12.645 +{
  12.646 +  PXENVBD_TARGET_DATA TargetData = (PXENVBD_TARGET_DATA)DeviceExtension;
  12.647 +  PSCSI_REQUEST_BLOCK Srb;
  12.648    RING_IDX i, rp;
  12.649    int j;
  12.650    blkif_response_t *rep;
  12.651 -  PXENVBD_CHILD_DEVICE_DATA ChildDeviceData;
  12.652 -  PSCSI_REQUEST_BLOCK Srb;
  12.653 -  PIO_STACK_LOCATION IrpSp;
  12.654    char *DataBuffer;
  12.655    int more_to_do;
  12.656 -  int IrpCount;
  12.657 -  PIRP Irps[100];
  12.658    int BlockCount;
  12.659 -  KIRQL KIrql;
  12.660 -  WDFDEVICE ChildDevice;
  12.661 -  XenVbd_ListEntry *ListEntry;
  12.662 +  KIRQL Irql;
  12.663    int notify;
  12.664 -
  12.665 -  //!!!IRQL_DISPATCH!!!
  12.666 +  KAPC_STATE ApcState;
  12.667 +  PIRP Irp;
  12.668 +  PXENVBD_DEVICE_DATA DeviceData;
  12.669  
  12.670 -  //KdPrint((__DRIVER_NAME " --> XenVbd_DpcThreadProc\n"));
  12.671 -
  12.672 -  ChildDevice = WdfDpcGetParentObject(Dpc);
  12.673 +//  KdPrint((__DRIVER_NAME " --> HwScsiInterruptTarget\n"));
  12.674  
  12.675 -  ChildDeviceData = *GetChildDeviceData(ChildDevice);
  12.676 -
  12.677 -  IrpCount = 0;
  12.678 +  DeviceData = (PXENVBD_DEVICE_DATA)TargetData->DeviceData;
  12.679    more_to_do = TRUE;
  12.680 -  KeAcquireSpinLock(&ChildDeviceData->Lock, &KIrql);
  12.681 -
  12.682 -  ChildDeviceData->IrpAddedToRingAtLastDpc = ChildDeviceData->IrpAddedToRing;
  12.683  
  12.684    while (more_to_do)
  12.685    {
  12.686 -    rp = ChildDeviceData->Ring.sring->rsp_prod;
  12.687 +    rp = TargetData->Ring.sring->rsp_prod;
  12.688      KeMemoryBarrier();
  12.689 -    for (i = ChildDeviceData->Ring.rsp_cons; i != rp; i++)
  12.690 +    for (i = TargetData->Ring.rsp_cons; i != rp; i++)
  12.691      {
  12.692 -      rep = RING_GET_RESPONSE(&ChildDeviceData->Ring, i);
  12.693 -      ChildDeviceData->IrpRemovedFromRing++;
  12.694 -      Irp = ChildDeviceData->shadow[rep->id].Irp;
  12.695 -      IrpSp = IoGetCurrentIrpStackLocation(Irp);
  12.696 -      Srb = IrpSp->Parameters.Scsi.Srb;
  12.697 +      rep = RING_GET_RESPONSE(&TargetData->Ring, i);
  12.698 +      Srb = TargetData->shadow[rep->id].Srb;
  12.699 +      BlockCount = (Srb->Cdb[7] << 8) | Srb->Cdb[8];
  12.700  
  12.701 -      if (rep->status != BLKIF_RSP_OKAY)
  12.702 -      {
  12.703 -        KdPrint((__DRIVER_NAME "     Xen Operation returned error in DpcThreadProc\n"));
  12.704 -        KdPrint((__DRIVER_NAME "       operation = %d, nr_segments = %d, sector_number = %d\n", ChildDeviceData->shadow[rep->id].req.operation, ChildDeviceData->shadow[rep->id].req.nr_segments, ChildDeviceData->shadow[rep->id].req.sector_number));
  12.705 -        for (j = 0; j < ChildDeviceData->shadow[rep->id].req.nr_segments; j++)
  12.706 -        {
  12.707 -          KdPrint((__DRIVER_NAME "       gref[%d] = %d\n", j, ChildDeviceData->shadow[rep->id].req.seg[j].gref));
  12.708 -        }
  12.709 -        KdPrint((__DRIVER_NAME "       MmGetMdlByteOffset = %d\n", MmGetMdlByteOffset(Irp->MdlAddress)));
  12.710 -      }
  12.711 -      for (j = 0; j < ChildDeviceData->shadow[rep->id].req.nr_segments; j++)
  12.712 +      if (rep->status == BLKIF_RSP_OKAY)
  12.713 +        Srb->SrbStatus = SRB_STATUS_SUCCESS;
  12.714 +      else
  12.715        {
  12.716 -        GntTblInterface.EndAccess(GntTblInterface.InterfaceHeader.Context,
  12.717 -          ChildDeviceData->shadow[rep->id].req.seg[j].gref);
  12.718 -      }
  12.719 -      BlockCount = (Srb->Cdb[7] << 8) | Srb->Cdb[8];
  12.720 -      if (ChildDeviceData->shadow[rep->id].Buf != NULL)
  12.721 -      {
  12.722 +        KdPrint((__DRIVER_NAME "     Xen Operation returned error\n"));
  12.723          if (Srb->Cdb[0] == SCSIOP_READ)
  12.724 -        {
  12.725 -          DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
  12.726 -          if (DataBuffer == NULL)
  12.727 -            KdPrint((__DRIVER_NAME "     MmGetSystemAddressForMdlSafe Failed in DpcThreadProc\n"));
  12.728 -          memcpy(DataBuffer, ChildDeviceData->shadow[rep->id].Buf, BlockCount * ChildDeviceData->BytesPerSector);
  12.729 -        }
  12.730 -        FreePages(ChildDeviceData->shadow[rep->id].Mdl);
  12.731 +          KdPrint((__DRIVER_NAME "     Operation = Read\n"));
  12.732 +        else
  12.733 +          KdPrint((__DRIVER_NAME "     Operation = Write\n"));     
  12.734 +        KdPrint((__DRIVER_NAME "     Sector = %08X, Count = %d\n", TargetData->shadow[rep->id].req.sector_number, BlockCount));
  12.735 +        Srb->SrbStatus = SRB_STATUS_ERROR;
  12.736        }
  12.737 -      Srb->SrbStatus = SRB_STATUS_SUCCESS;
  12.738 -      Srb->ScsiStatus = 0;
  12.739 -      Irp->IoStatus.Status = STATUS_SUCCESS;
  12.740 -      Irp->IoStatus.Information = BlockCount * ChildDeviceData->BytesPerSector;
  12.741 +      for (j = 0; j < TargetData->shadow[rep->id].req.nr_segments; j++)
  12.742 +        DeviceData->XenDeviceData->GntTblInterface.EndAccess(TargetData->shadow[rep->id].req.seg[j].gref);
  12.743 +      if (Srb->Cdb[0] == SCSIOP_READ)
  12.744 +        memcpy(Srb->DataBuffer, TargetData->shadow[rep->id].Buf, BlockCount * TargetData->BytesPerSector);
  12.745  
  12.746 -      Irps[IrpCount++] = Irp;
  12.747 +      ScsiPortNotification(RequestComplete, DeviceData, Srb);
  12.748  
  12.749 -      ADD_ID_TO_FREELIST(ChildDeviceData, rep->id);
  12.750 +      ADD_ID_TO_FREELIST(TargetData, rep->id);
  12.751      }
  12.752  
  12.753 -    ChildDeviceData->Ring.rsp_cons = i;
  12.754 -    if (i != ChildDeviceData->Ring.req_prod_pvt)
  12.755 +    TargetData->Ring.rsp_cons = i;
  12.756 +    if (i != TargetData->Ring.req_prod_pvt)
  12.757      {
  12.758 -      RING_FINAL_CHECK_FOR_RESPONSES(&ChildDeviceData->Ring, more_to_do);
  12.759 +      RING_FINAL_CHECK_FOR_RESPONSES(&TargetData->Ring, more_to_do);
  12.760      }
  12.761      else
  12.762      {
  12.763 -      ChildDeviceData->Ring.sring->rsp_event = i + 1;
  12.764 +      TargetData->Ring.sring->rsp_event = i + 1;
  12.765        more_to_do = FALSE;
  12.766      }
  12.767    }
  12.768  
  12.769 -  notify = 0;
  12.770 -  while (!RING_FULL(&ChildDeviceData->Ring) && (ListEntry = (XenVbd_ListEntry *)RemoveHeadList(&ChildDeviceData->IrpListHead)) != (XenVbd_ListEntry *)&ChildDeviceData->IrpListHead)
  12.771 -  {
  12.772 -    ChildDeviceData->IrpRemovedFromList++;
  12.773 -    XenVbd_PutIrpOnRing(ChildDevice, ListEntry->Irp);
  12.774 -    ExFreePoolWithTag(ListEntry, XENVBD_POOL_TAG);
  12.775 -    notify = 1;
  12.776 -  }
  12.777 -  if (notify)
  12.778 -  {
  12.779 -    RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&ChildDeviceData->Ring, notify);
  12.780 -    if (notify)
  12.781 -      EvtChnInterface.Notify(EvtChnInterface.InterfaceHeader.Context,
  12.782 -        ChildDeviceData->EventChannel);
  12.783 -  }
  12.784 -  KeReleaseSpinLock(&ChildDeviceData->Lock, KIrql);
  12.785 +//  KdPrint((__DRIVER_NAME " <-- HwScsiInterruptTarget\n"));
  12.786  
  12.787 -  for (j = 0; j < IrpCount; j++)
  12.788 -  {
  12.789 -    IoCompleteRequest(Irps[j], IO_NO_INCREMENT);
  12.790 -    ChildDeviceData->IrpCompleted++;
  12.791 -  }
  12.792 -
  12.793 -  //KdPrint((__DRIVER_NAME " <-- XenVbd_DpcThreadProc\n"));
  12.794 -  //KdPrint((__DRIVER_NAME " <-- XenVbd_DpcThreadProc (AddedToList = %d, RemovedFromList = %d, AddedToRing = %d, AddedToRingAtLastNotify = %d, AddedToRingAtLastInterrupt = %d, AddedToRingAtLastDpc = %d, RemovedFromRing = %d, IrpCompleted = %d)\n", ChildDeviceData->IrpAddedToList, ChildDeviceData->IrpRemovedFromList, ChildDeviceData->IrpAddedToRing, ChildDeviceData->IrpAddedToRingAtLastNotify, ChildDeviceData->IrpAddedToRingAtLastInterrupt, ChildDeviceData->IrpAddedToRingAtLastDpc, ChildDeviceData->IrpRemovedFromRing, ChildDeviceData->IrpCompleted));
  12.795 +  return FALSE;
  12.796  }
  12.797  
  12.798  static BOOLEAN
  12.799 -XenVbd_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext)
  12.800 +XenVbd_HwScsiInterrupt(PVOID DeviceExtension)
  12.801  {
  12.802 -  BOOLEAN RetVal;
  12.803 -  PXENVBD_CHILD_DEVICE_DATA ChildDeviceData;
  12.804 -
  12.805 -  UNREFERENCED_PARAMETER(Interrupt);
  12.806 -  // !!!RUNS AT DIRQL!!!
  12.807 +  PXENVBD_DEVICE_DATA DeviceData;
  12.808 +  PXENVBD_TARGET_DATA TargetData;
  12.809 +  int i, j;
  12.810  
  12.811 -  //KdPrint((__DRIVER_NAME " --> XenVbd_Interrupt\n"));
  12.812 +//  KdPrint((__DRIVER_NAME " --> HwScsiInterrupt\n"));
  12.813  
  12.814 -  ChildDeviceData = (PXENVBD_CHILD_DEVICE_DATA)ServiceContext;
  12.815 -  ChildDeviceData->IrpAddedToRingAtLastInterrupt = ChildDeviceData->IrpAddedToRing;
  12.816 -  RetVal = WdfDpcEnqueue(ChildDeviceData->Dpc);
  12.817 +  DeviceData = (PXENVBD_DEVICE_DATA)DeviceExtension;
  12.818  
  12.819 -  //KdPrint((__DRIVER_NAME " <-- XenVbd_Interrupt (RetVal = %d)\n", RetVal));  
  12.820 -
  12.821 -  return STATUS_SUCCESS;
  12.822 +  KeMemoryBarrier();
  12.823 +  for (i = 0; i < SCSI_BUSES; i++)
  12.824 +  {
  12.825 +    for (j = 0; j < SCSI_TARGETS_PER_BUS; j++)
  12.826 +    {
  12.827 +      TargetData = &DeviceData->BusData[i].TargetData[j];
  12.828 +      if (TargetData->PendingInterrupt)
  12.829 +        XenVbd_HwScsiInterruptTarget(TargetData);
  12.830 +      TargetData->PendingInterrupt = FALSE;
  12.831 +    }
  12.832 +  }
  12.833 +//  KdPrint((__DRIVER_NAME " <-- HwScsiInterrupt\n"));
  12.834  }
  12.835  
  12.836  static VOID
  12.837  XenVbd_BackEndStateHandler(char *Path, PVOID Data)
  12.838  {
  12.839 -  PXENVBD_CHILD_DEVICE_DATA DeviceData = Data;
  12.840 +  PXENVBD_TARGET_DATA TargetData;
  12.841 +  PXENVBD_DEVICE_DATA DeviceData;
  12.842    char TmpPath[128];
  12.843    char *Value;
  12.844    int NewState;
  12.845 @@ -661,14 +321,20 @@ XenVbd_BackEndStateHandler(char *Path, P
  12.846    grant_ref_t ref;
  12.847    blkif_sring_t *SharedRing;
  12.848    ULONG PFN;
  12.849 -  XENVBD_DEVICE_IDENTIFICATION_DESCRIPTION Description;
  12.850 +  //XENVBD_DEVICE_IDENTIFICATION_DESCRIPTION Description;
  12.851 +  KIRQL OldIrql;
  12.852    NTSTATUS status;
  12.853 +  int i;
  12.854  
  12.855 -  XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
  12.856 -    XBT_NIL, Path, &Value);
  12.857 +  KdPrint((__DRIVER_NAME " --> BackEndStateHandler\n"));
  12.858 +  KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
  12.859 +
  12.860 +  TargetData = (PXENVBD_TARGET_DATA)Data;
  12.861 +  DeviceData = (PXENVBD_DEVICE_DATA)TargetData->DeviceData;
  12.862 +
  12.863 +  DeviceData->XenDeviceData->XenBusInterface.Read(XBT_NIL, Path, &Value);
  12.864 +
  12.865    NewState = atoi(Value);
  12.866 -  ExFreePool(Value);
  12.867 -
  12.868    switch (NewState)
  12.869    {
  12.870    case XenbusStateUnknown:
  12.871 @@ -682,127 +348,115 @@ XenVbd_BackEndStateHandler(char *Path, P
  12.872    case XenbusStateInitWait:
  12.873      KdPrint((__DRIVER_NAME "     Backend State Changed to InitWait\n"));  
  12.874  
  12.875 -    DeviceData->EventChannel = EvtChnInterface.AllocUnbound(
  12.876 -      EvtChnInterface.InterfaceHeader.Context, 0);
  12.877 -    EvtChnInterface.Bind(EvtChnInterface.InterfaceHeader.Context,
  12.878 -      DeviceData->EventChannel, XenVbd_Interrupt, DeviceData);
  12.879 +    TargetData->EventChannel = DeviceData->XenDeviceData->EvtChnInterface.AllocUnbound(0);
  12.880 +    DeviceData->XenDeviceData->EvtChnInterface.Bind(TargetData->EventChannel, XenVbd_Interrupt, TargetData);
  12.881      Mdl = AllocatePage();
  12.882      PFN = *MmGetMdlPfnArray(Mdl);
  12.883      SharedRing = (blkif_sring_t *)MmGetMdlVirtualAddress(Mdl);
  12.884      SHARED_RING_INIT(SharedRing);
  12.885 -    FRONT_RING_INIT(&DeviceData->Ring, SharedRing, PAGE_SIZE);
  12.886 -    ref = GntTblInterface.GrantAccess(GntTblInterface.InterfaceHeader.Context,
  12.887 -      0, PFN, FALSE);
  12.888 -
  12.889 -    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
  12.890 -    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/ring-ref");
  12.891 -    XenBusInterface.Printf(XenBusInterface.InterfaceHeader.Context,
  12.892 -      XBT_NIL, TmpPath, "%d", ref);
  12.893 +    FRONT_RING_INIT(&TargetData->Ring, SharedRing, PAGE_SIZE);
  12.894 +    ref = DeviceData->XenDeviceData->GntTblInterface.GrantAccess(0, PFN, FALSE);
  12.895  
  12.896 -    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
  12.897 -    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/event-channel");
  12.898 -    XenBusInterface.Printf(XenBusInterface.InterfaceHeader.Context,
  12.899 -      XBT_NIL, TmpPath, "%d", DeviceData->EventChannel);
  12.900 +    TargetData->shadow = ExAllocatePoolWithTag(NonPagedPool, sizeof(blkif_shadow_t) * BLK_RING_SIZE, XENVBD_POOL_TAG);
  12.901 +
  12.902 +    memset(TargetData->shadow, 0, sizeof(blkif_shadow_t) * BLK_RING_SIZE);
  12.903 +    for (i = 0; i < BLK_RING_SIZE; i++)
  12.904 +    {
  12.905 +      TargetData->shadow[i].req.id = i + 1;
  12.906 +      TargetData->shadow[i].Mdl = AllocatePages(BUF_PAGES_PER_SRB); // stupid that we have to do this!
  12.907 +      TargetData->shadow[i].Buf = MmGetMdlVirtualAddress(TargetData->shadow[i].Mdl);
  12.908 +    }
  12.909 +    TargetData->shadow_free = 0;
  12.910 +    TargetData->shadow[BLK_RING_SIZE - 1].req.id = 0x0fffffff;
  12.911 +
  12.912 +    RtlStringCbCopyA(TmpPath, 128, TargetData->Path);
  12.913 +    RtlStringCbCatA(TmpPath, 128, "/ring-ref");
  12.914 +    DeviceData->XenDeviceData->XenBusInterface.Printf(XBT_NIL, TmpPath, "%d", ref);
  12.915 +
  12.916 +    RtlStringCbCopyA(TmpPath, 128, TargetData->Path);
  12.917 +    RtlStringCbCatA(TmpPath, 128, "/event-channel");
  12.918 +    DeviceData->XenDeviceData->XenBusInterface.Printf(XBT_NIL, TmpPath, "%d", TargetData->EventChannel);
  12.919    
  12.920 -    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
  12.921 -    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/state");
  12.922 -    XenBusInterface.Printf(XenBusInterface.InterfaceHeader.Context,
  12.923 -      XBT_NIL, TmpPath, "%d", XenbusStateInitialised);
  12.924 +    RtlStringCbCopyA(TmpPath, 128, TargetData->Path);
  12.925 +    RtlStringCbCatA(TmpPath, 128, "/state");
  12.926 +    DeviceData->XenDeviceData->XenBusInterface.Printf(XBT_NIL, TmpPath, "%d", XenbusStateInitialised);
  12.927  
  12.928      KdPrint((__DRIVER_NAME "     Set Frontend state to Initialised\n"));
  12.929      break;
  12.930  
  12.931    case XenbusStateInitialised:
  12.932      KdPrint((__DRIVER_NAME "     Backend State Changed to Initialised\n"));
  12.933 -    // create the device
  12.934      break;
  12.935  
  12.936    case XenbusStateConnected:
  12.937      KdPrint((__DRIVER_NAME "     Backend State Changed to Connected\n"));  
  12.938  
  12.939 -    WDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER_INIT(&Description.Header, sizeof(Description));
  12.940 -
  12.941 -    Description.DeviceData = DeviceData;
  12.942 -
  12.943 -    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
  12.944 -    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/device-type");
  12.945 -    XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
  12.946 -      XBT_NIL, TmpPath, &Value);
  12.947 +    RtlStringCbCopyA(TmpPath, 128, TargetData->Path);
  12.948 +    RtlStringCbCatA(TmpPath, 128, "/device-type");
  12.949 +    DeviceData->XenDeviceData->XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
  12.950      if (strcmp(Value, "disk") == 0)
  12.951      {
  12.952        KdPrint((__DRIVER_NAME "     DeviceType = Disk\n"));    
  12.953 -      DeviceData->DeviceType = XENVBD_DEVICETYPE_DISK;
  12.954 +      TargetData->DeviceType = XENVBD_DEVICETYPE_DISK;
  12.955      }
  12.956      else if (strcmp(Value, "cdrom") == 0)
  12.957      {
  12.958        KdPrint((__DRIVER_NAME "     DeviceType = CDROM\n"));    
  12.959 -      DeviceData->DeviceType = XENVBD_DEVICETYPE_CDROM;
  12.960 +      TargetData->DeviceType = XENVBD_DEVICETYPE_CDROM;
  12.961      }
  12.962      else
  12.963      {
  12.964        KdPrint((__DRIVER_NAME "     DeviceType = %s (This probably won't work!)\n", Value));
  12.965 +      TargetData->DeviceType = XENVBD_DEVICETYPE_UNKNOWN;
  12.966      }
  12.967  
  12.968 -    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->BackendPath);
  12.969 -    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/type"); // should probably check that this is 'phy' or 'file' or at least not ''
  12.970 -    XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
  12.971 -      XBT_NIL, TmpPath, &Value);
  12.972 +    RtlStringCbCopyA(TmpPath, 128, TargetData->BackendPath);
  12.973 +    RtlStringCbCatA(TmpPath, 128, "/type"); // should probably check that this is 'phy' or 'file' or at least not ''
  12.974 +    DeviceData->XenDeviceData->XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
  12.975      KdPrint((__DRIVER_NAME "     Backend Type = %s\n", Value));
  12.976      ExFreePool(Value);
  12.977  
  12.978 -    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->BackendPath);
  12.979 -    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/mode"); // should store this...
  12.980 -    XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
  12.981 -      XBT_NIL, TmpPath, &Value);
  12.982 +    RtlStringCbCopyA(TmpPath, 128, TargetData->BackendPath);
  12.983 +    RtlStringCbCatA(TmpPath, 128, "/mode"); // should store this...
  12.984 +    DeviceData->XenDeviceData->XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
  12.985      KdPrint((__DRIVER_NAME "     Backend Mode = %s\n", Value));
  12.986      ExFreePool(Value);
  12.987  
  12.988 -    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->BackendPath);
  12.989 -    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/sector-size");
  12.990 -    XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
  12.991 -      XBT_NIL, TmpPath, &Value);
  12.992 +    RtlStringCbCopyA(TmpPath, 128, TargetData->BackendPath);
  12.993 +    RtlStringCbCatA(TmpPath, 128, "/sector-size");
  12.994 +    DeviceData->XenDeviceData->XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
  12.995      // should complain if Value == NULL
  12.996 -    DeviceData->BytesPerSector = atoi(Value);
  12.997 -    ExFreePool(Value);
  12.998 +    TargetData->BytesPerSector = atoi(Value);
  12.999  
 12.1000 -    KdPrint((__DRIVER_NAME "     BytesPerSector = %d\n", DeviceData->BytesPerSector));    
 12.1001 +    KdPrint((__DRIVER_NAME "     BytesPerSector = %d\n", TargetData->BytesPerSector));    
 12.1002  
 12.1003 -    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->BackendPath);
 12.1004 -    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/sectors");
 12.1005 -    XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
 12.1006 -      XBT_NIL, TmpPath, &Value);
 12.1007 +    RtlStringCbCopyA(TmpPath, 128, TargetData->BackendPath);
 12.1008 +    RtlStringCbCatA(TmpPath, 128, "/sectors");
 12.1009 +    DeviceData->XenDeviceData->XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
 12.1010      // should complain if Value == NULL
 12.1011 -    DeviceData->TotalSectors = (ULONGLONG)atol(Value);
 12.1012 -    ExFreePool(Value);
 12.1013 +    TargetData->TotalSectors = (ULONGLONG)atol(Value);
 12.1014  
 12.1015 -    KdPrint((__DRIVER_NAME "     TotalSectors = %d\n", DeviceData->TotalSectors));    
 12.1016 +    KdPrint((__DRIVER_NAME "     TotalSectors = %d\n", TargetData->TotalSectors));    
 12.1017  
 12.1018      // should probably use the partition table (if one exists) here for the sectorspertrack and trackspercylinder values
 12.1019 -    DeviceData->Geometry.MediaType = FixedMedia;
 12.1020 -    DeviceData->Geometry.BytesPerSector = DeviceData->BytesPerSector;
 12.1021 -    DeviceData->Geometry.SectorsPerTrack = 63;
 12.1022 -    DeviceData->Geometry.TracksPerCylinder = 255;
 12.1023 -    DeviceData->Geometry.Cylinders.QuadPart = DeviceData->TotalSectors / DeviceData->Geometry.SectorsPerTrack / DeviceData->Geometry.TracksPerCylinder;
 12.1024 -    KdPrint((__DRIVER_NAME "     Geometry C/H/S = %d/%d/%d\n", DeviceData->Geometry.Cylinders.LowPart, DeviceData->Geometry.TracksPerCylinder, DeviceData->Geometry.SectorsPerTrack));
 12.1025 -
 12.1026 -    // if we detected something wrong, we should not enumarate the device and should instead initiate a close
 12.1027 +    TargetData->Geometry.MediaType = FixedMedia;
 12.1028 +    TargetData->Geometry.BytesPerSector = TargetData->BytesPerSector;
 12.1029 +    TargetData->Geometry.SectorsPerTrack = 63;
 12.1030 +    TargetData->Geometry.TracksPerCylinder = 255;
 12.1031 +    TargetData->Geometry.Cylinders.QuadPart = TargetData->TotalSectors / TargetData->Geometry.SectorsPerTrack / TargetData->Geometry.TracksPerCylinder;
 12.1032 +    KdPrint((__DRIVER_NAME "     Geometry C/H/S = %d/%d/%d\n", TargetData->Geometry.Cylinders.LowPart, TargetData->Geometry.TracksPerCylinder, TargetData->Geometry.SectorsPerTrack));
 12.1033 +    
 12.1034 +// now ask windows to rescan the scsi bus...
 12.1035 +    DeviceData->BusChangePending = 1;
 12.1036  
 12.1037 -    status = WdfChildListAddOrUpdateChildDescriptionAsPresent(WdfFdoGetDefaultChildList(GlobalDevice), &Description.Header, NULL);
 12.1038 -    if (!NT_SUCCESS(status))
 12.1039 -    {
 12.1040 -      KdPrint((__DRIVER_NAME "     WdfChildListAddOrUpdateChildDescriptionAsPresent failed %08x\n", status));
 12.1041 -    } 
 12.1042 -
 12.1043 -    RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
 12.1044 -    RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/state");
 12.1045 -    XenBusInterface.Printf(XenBusInterface.InterfaceHeader.Context,
 12.1046 -      XBT_NIL, TmpPath, "%d", XenbusStateConnected);
 12.1047 +    RtlStringCbCopyA(TmpPath, 128, TargetData->Path);
 12.1048 +    RtlStringCbCatA(TmpPath, 128, "/state");
 12.1049 +    DeviceData->XenDeviceData->XenBusInterface.Printf(XBT_NIL, TmpPath, "%d", XenbusStateConnected);
 12.1050  
 12.1051      KdPrint((__DRIVER_NAME "     Set Frontend state to Connected\n"));
 12.1052 -    InterlockedIncrement(&EnumeratedDevices);
 12.1053 -    KdPrint((__DRIVER_NAME "     Added a device, notifying\n"));
 12.1054 -    
 12.1055 -    KeSetEvent(&WaitDevicesEvent, 1, FALSE);
 12.1056 +    InterlockedIncrement(&DeviceData->EnumeratedDevices);
 12.1057 +    KdPrint((__DRIVER_NAME "     Added a device, notifying\n"));  
 12.1058 +    KeSetEvent(&DeviceData->WaitDevicesEvent, 1, FALSE);
 12.1059  
 12.1060      break;
 12.1061  
 12.1062 @@ -818,350 +472,188 @@ XenVbd_BackEndStateHandler(char *Path, P
 12.1063      KdPrint((__DRIVER_NAME "     Backend State Changed to Undefined = %d\n", NewState));
 12.1064      break;
 12.1065    }
 12.1066 +
 12.1067 +  KdPrint((__DRIVER_NAME " <-- BackEndStateHandler\n"));
 12.1068  }
 12.1069  
 12.1070  static VOID
 12.1071 -XenVbd_HotPlugHandler(char *Path, PVOID Data)
 12.1072 +XenVbd_WatchHandler(char *Path, PVOID DeviceExtension)
 12.1073  {
 12.1074 -  PXENVBD_CHILD_DEVICE_DATA DeviceData;
 12.1075 +  PXENVBD_DEVICE_DATA DeviceData = (PXENVBD_DEVICE_DATA)DeviceExtension;
 12.1076    char **Bits;
 12.1077    int Count;
 12.1078    char TmpPath[128];
 12.1079    char *Value;
 12.1080 -
 12.1081 -  UNREFERENCED_PARAMETER(Data);  
 12.1082 +  int CurrentBus, CurrentTarget;
 12.1083 +  PXENVBD_TARGET_DATA TargetData, VacantTarget;
 12.1084 +  KIRQL OldIrql;
 12.1085 +  int i, j;
 12.1086  
 12.1087 -  //KdPrint((__DRIVER_NAME " --> HotPlugHandler\n"));
 12.1088 +  KdPrint((__DRIVER_NAME " --> WatchHandler (DeviceData = %08x)\n", DeviceData));
 12.1089  
 12.1090 -  //KdPrint((__DRIVER_NAME "     Path = %s\n", Path));
 12.1091 +  KdPrint((__DRIVER_NAME "     Path = %s\n", Path));
 12.1092  
 12.1093    Bits = SplitString(Path, '/', 4, &Count);
 12.1094    if (Count != 4)
 12.1095    {
 12.1096 -    KdPrint((__FUNCTION__ ": Count = %d, not 4!\n", Count));
 12.1097 -    goto cleanup;
 12.1098 -  }
 12.1099 +  case 0:
 12.1100 +  case 1:
 12.1101 +  case 2:
 12.1102 +    break; // should never happen
 12.1103 +  case 3:
 12.1104 +    break;
 12.1105 +  case 4:
 12.1106 +    if (strcmp(Bits[3], "state") != 0) // we only care when the state appears
 12.1107 +      break;
 12.1108  
 12.1109 -  if (strcmp(Bits[3], "state") != 0) // we only care when the state appears
 12.1110 -    goto cleanup;
 12.1111 +    KeAcquireSpinLock(&DeviceData->Lock, &OldIrql);
 12.1112  
 12.1113 -  /* ignore already known devices */
 12.1114 -  for (DeviceData = (PXENVBD_CHILD_DEVICE_DATA)DeviceListHead.Flink; DeviceData != (PXENVBD_CHILD_DEVICE_DATA)&DeviceListHead; DeviceData = (PXENVBD_CHILD_DEVICE_DATA)DeviceData->Entry.Flink)
 12.1115 +    for (VacantTarget = NULL,i = 0; i < SCSI_BUSES * SCSI_TARGETS_PER_BUS; i++)
 12.1116 +    {
 12.1117 +      CurrentBus = i / SCSI_TARGETS_PER_BUS;
 12.1118 +      CurrentTarget = i % SCSI_TARGETS_PER_BUS;
 12.1119 +      if (CurrentTarget == 7) // don't use 7 - it would be for the controller
 12.1120 +        continue;
 12.1121 +      TargetData = &DeviceData->BusData[CurrentBus].TargetData[CurrentTarget];
 12.1122 +      if (TargetData->Present && strncmp(TargetData->Path, Path, strlen(TargetData->Path)) == 0 && Path[strlen(TargetData->Path)] == '/')
 12.1123 +        break; // already exists
 12.1124 +      else if (!TargetData->Present && VacantTarget == NULL)
 12.1125 +        VacantTarget = TargetData;
 12.1126 +    }
 12.1127 +    if (i == SCSI_BUSES * SCSI_TARGETS_PER_BUS && VacantTarget != NULL)
 12.1128 +    {
 12.1129 +      VacantTarget->Present = 1;
 12.1130 +      KeReleaseSpinLock(&DeviceData->Lock, OldIrql);
 12.1131 +
 12.1132 +      RtlStringCbCopyA(VacantTarget->Path, 128, Bits[0]);
 12.1133 +      RtlStringCbCatA(VacantTarget->Path, 128, "/");
 12.1134 +      RtlStringCbCatA(VacantTarget->Path, 128, Bits[1]);
 12.1135 +      RtlStringCbCatA(VacantTarget->Path, 128, "/");
 12.1136 +      RtlStringCbCatA(VacantTarget->Path, 128, Bits[2]);
 12.1137 +
 12.1138 +      VacantTarget->DeviceIndex = atoi(Bits[2]);
 12.1139 +
 12.1140 +      RtlStringCbCopyA(TmpPath, 128, VacantTarget->Path);
 12.1141 +      RtlStringCbCatA(TmpPath, 128, "/backend");
 12.1142 +      DeviceData->XenDeviceData->XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
 12.1143 +      if (Value == NULL)
 12.1144 +        KdPrint((__DRIVER_NAME "     Read Failed\n"));
 12.1145 +      else
 12.1146 +        RtlStringCbCopyA(VacantTarget->BackendPath, 128, Value);
 12.1147 +      RtlStringCbCopyA(TmpPath, 128, VacantTarget->BackendPath);
 12.1148 +      RtlStringCbCatA(TmpPath, 128, "/state");
 12.1149 +
 12.1150 +      DeviceData->XenDeviceData->XenBusInterface.AddWatch(XBT_NIL, TmpPath, XenVbd_BackEndStateHandler, VacantTarget);
 12.1151 +    }
 12.1152 +    else
 12.1153 +      KeReleaseSpinLock(&DeviceData->Lock, OldIrql);
 12.1154 +    break;
 12.1155 +  }
 12.1156 +  
 12.1157 +  FreeSplitString(Bits, Count);
 12.1158 +
 12.1159 +  KdPrint((__DRIVER_NAME " <-- WatchHandler\n"));  
 12.1160 +
 12.1161 +  return;
 12.1162 +}
 12.1163 +
 12.1164 +static VOID 
 12.1165 +XenVbd_CheckBusChangedTimer(PVOID DeviceExtension)
 12.1166 +{
 12.1167 +  PXENVBD_DEVICE_DATA DeviceData = (PXENVBD_DEVICE_DATA)DeviceExtension;
 12.1168 +
 12.1169 +  if (DeviceData->BusChangePending)
 12.1170    {
 12.1171 -    if (strncmp(DeviceData->Path, Path, strlen(DeviceData->Path)) == 0 && Path[strlen(DeviceData->Path)] == '/')
 12.1172 +    ScsiPortNotification(BusChangeDetected, DeviceData, 0);
 12.1173 +    DeviceData->BusChangePending = 0;
 12.1174 +  }
 12.1175 +  ScsiPortNotification(RequestTimerCall, DeviceExtension, XenVbd_CheckBusChangedTimer, 1000000);
 12.1176 +}
 12.1177 +
 12.1178 +static BOOLEAN
 12.1179 +XenVbd_HwScsiInitialize(PVOID DeviceExtension)
 12.1180 +{
 12.1181 +  PXENVBD_DEVICE_DATA DeviceData = (PXENVBD_DEVICE_DATA)DeviceExtension;
 12.1182 +  unsigned int i, j;
 12.1183 +  NTSTATUS Status;
 12.1184 +  char **VbdDevices;
 12.1185 +  char *msg;
 12.1186 +  char buffer[128];
 12.1187 +  LARGE_INTEGER WaitTimeout;
 12.1188 +
 12.1189 +  KdPrint((__DRIVER_NAME " --> HwScsiInitialize\n"));
 12.1190 +  KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
 12.1191 +
 12.1192 +  for (i = 0; i < SCSI_BUSES; i++)
 12.1193 +  {
 12.1194 +    for (j = 0; j < SCSI_TARGETS_PER_BUS; j++)
 12.1195      {
 12.1196 -      goto cleanup;
 12.1197 +      DeviceData->BusData[i].TargetData[j].Present = 0;
 12.1198 +      DeviceData->BusData[i].TargetData[j].DeviceData = DeviceData;
 12.1199      }
 12.1200    }
 12.1201  
 12.1202 -  /* new device found, alloc and init dev extension */
 12.1203 -  DeviceData = ExAllocatePoolWithTag(NonPagedPool,
 12.1204 -    sizeof(XENVBD_CHILD_DEVICE_DATA), XENVBD_POOL_TAG);
 12.1205 -  memset(DeviceData, 0, sizeof(XENVBD_CHILD_DEVICE_DATA));
 12.1206 -
 12.1207 -  //KdPrint((__DRIVER_NAME "     Allocated ChildDeviceData = %08x\n", DeviceData));
 12.1208 -  
 12.1209 -  InsertTailList(&DeviceListHead, &DeviceData->Entry);
 12.1210 -  RtlStringCbPrintfA(DeviceData->Path, ARRAY_SIZE(DeviceData->Path),
 12.1211 -    "%s/%s/%s", Bits[0], Bits[1], Bits[2]);
 12.1212 -  DeviceData->DeviceIndex = atoi(Bits[2]);
 12.1213 -
 12.1214 -  /* Get backend path */
 12.1215 -  RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->Path);
 12.1216 -  RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/backend");
 12.1217 -  XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
 12.1218 -    XBT_NIL, TmpPath, &Value);
 12.1219 -  if (!Value)
 12.1220 -  {
 12.1221 -    KdPrint((__DRIVER_NAME "     Read Failed\n"));
 12.1222 -  }
 12.1223 -  else
 12.1224 -  {
 12.1225 -    RtlStringCbCopyA(DeviceData->BackendPath,
 12.1226 -      ARRAY_SIZE(DeviceData->BackendPath), Value);
 12.1227 -  }
 12.1228 -  ExFreePool(Value);
 12.1229 -
 12.1230 -  /* Add watch on backend state */
 12.1231 -  RtlStringCbCopyA(TmpPath, ARRAY_SIZE(TmpPath), DeviceData->BackendPath);
 12.1232 -  RtlStringCbCatA(TmpPath, ARRAY_SIZE(TmpPath), "/state");
 12.1233 -  XenBusInterface.AddWatch(XenBusInterface.InterfaceHeader.Context,
 12.1234 -    XBT_NIL, TmpPath, XenVbd_BackEndStateHandler, DeviceData);
 12.1235 -
 12.1236 -cleanup:
 12.1237 -  FreeSplitString(Bits, Count);
 12.1238 -
 12.1239 -  //KdPrint((__DRIVER_NAME " <-- HotPlugHandler\n"));  
 12.1240 -}
 12.1241 -
 12.1242 -static NTSTATUS
 12.1243 -XenVbd_ChildListCreateDevice(WDFCHILDLIST ChildList, PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER IdentificationDescription, PWDFDEVICE_INIT ChildInit)
 12.1244 -{
 12.1245 -  NTSTATUS status;
 12.1246 -  WDFDEVICE ChildDevice;
 12.1247 -  PXENVBD_DEVICE_IDENTIFICATION_DESCRIPTION XenVbdIdentificationDesc;
 12.1248 -  DECLARE_UNICODE_STRING_SIZE(buffer, 50);
 12.1249 -  WDF_OBJECT_ATTRIBUTES PdoAttributes;
 12.1250 -  DECLARE_CONST_UNICODE_STRING(DeviceLocation, L"Xen Bus");
 12.1251 -  PXENVBD_CHILD_DEVICE_DATA ChildDeviceData;
 12.1252 -  unsigned int i;
 12.1253 -  WDF_DPC_CONFIG DpcConfig;
 12.1254 -  WDF_OBJECT_ATTRIBUTES DpcObjectAttributes;
 12.1255 -  WDF_DEVICE_STATE DeviceState;
 12.1256 -  //UCHAR ScsiMinors[1] = { IRP_MN_SCSI_CLASS };
 12.1257 -
 12.1258 -  UNREFERENCED_PARAMETER(ChildList);
 12.1259 -
 12.1260 -  KdPrint((__DRIVER_NAME " --> ChildListCreateDevice\n"));
 12.1261 -
 12.1262 -  XenVbdIdentificationDesc = CONTAINING_RECORD(IdentificationDescription, XENVBD_DEVICE_IDENTIFICATION_DESCRIPTION, Header);
 12.1263 -
 12.1264 -  ChildDeviceData = XenVbdIdentificationDesc->DeviceData;
 12.1265 -
 12.1266 -  // Capabilities = CM_DEVCAP_UNIQUEID
 12.1267 -  // Devnode Flages = DN_NEED_RESTART??? DN_DISABLEABLE???
 12.1268 -
 12.1269 -  switch (ChildDeviceData->DeviceType)
 12.1270 -  {
 12.1271 -  case XENVBD_DEVICETYPE_DISK:
 12.1272 -    WdfDeviceInitSetDeviceType(ChildInit, FILE_DEVICE_DISK);
 12.1273 +  DeviceData->XenDeviceData->WatchContext = DeviceExtension;
 12.1274 +  KeMemoryBarrier();
 12.1275 +  DeviceData->XenDeviceData->WatchHandler = XenVbd_WatchHandler;
 12.1276  
 12.1277 -    status = RtlUnicodeStringPrintf(&buffer, L"XEN\\Disk\0");
 12.1278 -    //status = RtlUnicodeStringPrintf(&buffer, L"XEN\\Disk&Ven_James&Prod_James&Rev_1.00\0");
 12.1279 -    status = WdfPdoInitAssignDeviceID(ChildInit, &buffer);
 12.1280 -
 12.1281 -    status = RtlUnicodeStringPrintf(&buffer, L"%02d\0", ChildDeviceData->DeviceIndex);
 12.1282 -    status = WdfPdoInitAssignInstanceID(ChildInit, &buffer);
 12.1283 -
 12.1284 -    status = RtlUnicodeStringPrintf(&buffer, L"GenDisk\0");
 12.1285 -    status = WdfPdoInitAddCompatibleID(ChildInit, &buffer);
 12.1286 -    status = WdfPdoInitAddHardwareID(ChildInit, &buffer);
 12.1287 -
 12.1288 -    status = RtlUnicodeStringPrintf(&buffer, L"Xen PV Disk (%d)", ChildDeviceData->DeviceIndex);
 12.1289 -    status = WdfPdoInitAddDeviceText(ChildInit, &buffer, &DeviceLocation, 0x409);
 12.1290 -    break;
 12.1291 -  case XENVBD_DEVICETYPE_CDROM:
 12.1292 -    WdfDeviceInitSetDeviceType(ChildInit, FILE_DEVICE_MASS_STORAGE);
 12.1293 -    WdfDeviceInitSetCharacteristics(ChildInit, FILE_READ_ONLY_DEVICE|FILE_REMOVABLE_MEDIA, TRUE);
 12.1294 -
 12.1295 -    status = RtlUnicodeStringPrintf(&buffer, L"XEN\\CDROM\0");
 12.1296 -    status = WdfPdoInitAssignDeviceID(ChildInit, &buffer);
 12.1297 -
 12.1298 -    status = RtlUnicodeStringPrintf(&buffer, L"%02d\0", ChildDeviceData->DeviceIndex);
 12.1299 -    status = WdfPdoInitAssignInstanceID(ChildInit, &buffer);
 12.1300 -
 12.1301 -    status = RtlUnicodeStringPrintf(&buffer, L"GenCdRom\0");
 12.1302 -    status = WdfPdoInitAddCompatibleID(ChildInit, &buffer);
 12.1303 -    status = WdfPdoInitAddHardwareID(ChildInit, &buffer);
 12.1304 -
 12.1305 -    status = RtlUnicodeStringPrintf(&buffer, L"Xen PV CDROM (%d)\0", ChildDeviceData->DeviceIndex);
 12.1306 -    status = WdfPdoInitAddDeviceText(ChildInit, &buffer, &DeviceLocation, 0x409);
 12.1307 -    break;
 12.1308 -  default:
 12.1309 -    // wtf?
 12.1310 -    break;
 12.1311 -  }
 12.1312 -
 12.1313 -  WdfPdoInitSetDefaultLocale(ChildInit, 0x409);
 12.1314 -  
 12.1315 -  WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&PdoAttributes, PXENVBD_CHILD_DEVICE_DATA);
 12.1316 -
 12.1317 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSCSI, IRP_MJ_SCSI, NULL, 0);
 12.1318 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpDEVICE_CONTROL, IRP_MJ_DEVICE_CONTROL, NULL, 0);
 12.1319 -/*
 12.1320 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_CLEANUP, NULL, 0);
 12.1321 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_CLOSE, NULL, 0);
 12.1322 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_CREATE, NULL, 0);
 12.1323 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_DIRECTORY_CONTROL, NULL, 0);
 12.1324 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_FILE_SYSTEM_CONTROL, NULL, 0);
 12.1325 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_FLUSH_BUFFERS, NULL, 0);
 12.1326 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_LOCK_CONTROL, NULL, 0);
 12.1327 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_POWER, NULL, 0);
 12.1328 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_QUERY_EA, NULL, 0);
 12.1329 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_QUERY_INFORMATION, NULL, 0);
 12.1330 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_QUERY_SECURITY, NULL, 0);
 12.1331 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_QUERY_VOLUME_INFORMATION, NULL, 0);
 12.1332 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_READ, NULL, 0);
 12.1333 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_SET_INFORMATION, NULL, 0);
 12.1334 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_SET_SECURITY, NULL, 0);
 12.1335 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_SET_VOLUME_INFORMATION, NULL, 0);
 12.1336 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_SHUTDOWN, NULL, 0);
 12.1337 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_SYSTEM_CONTROL, NULL, 0);
 12.1338 -  WdfDeviceInitAssignWdmIrpPreprocessCallback(ChildInit, XenVbd_Child_PreprocessWdmIrpSomethingSomething, IRP_MJ_WRITE, NULL, 0);
 12.1339 -*/
 12.1340 -  WdfDeviceInitSetIoType(ChildInit, WdfDeviceIoDirect);
 12.1341 -
 12.1342 -  //WdfDeviceInitSetPowerNotPageable(ChildInit);
 12.1343 -
 12.1344 -  status = WdfDeviceCreate(&ChildInit, &PdoAttributes, &ChildDevice);
 12.1345 -  if (!NT_SUCCESS(status))
 12.1346 -  {
 12.1347 -    KdPrint((__DRIVER_NAME "     WdfDeviceCreate status = %08X\n", status));
 12.1348 -  }
 12.1349 -
 12.1350 -  switch (ChildDeviceData->DeviceType)
 12.1351 +  KeInitializeEvent(&DeviceData->WaitDevicesEvent, SynchronizationEvent, FALSE);  
 12.1352 +  DeviceData->EnumeratedDevices = 0;
 12.1353 +  if (DeviceData->XenDeviceData->AutoEnumerate)
 12.1354    {
 12.1355 -  case XENVBD_DEVICETYPE_DISK:
 12.1356 -    WDF_DEVICE_STATE_INIT(&DeviceState);
 12.1357 -    DeviceState.NotDisableable = WdfTrue;
 12.1358 -    WdfDeviceSetDeviceState(ChildDevice, &DeviceState);
 12.1359 -    WdfDeviceSetSpecialFileSupport(ChildDevice, WdfSpecialFilePaging, TRUE);
 12.1360 -    WdfDeviceSetSpecialFileSupport(ChildDevice, WdfSpecialFileHibernation, TRUE);
 12.1361 -    WdfDeviceSetSpecialFileSupport(ChildDevice, WdfSpecialFileDump, TRUE);
 12.1362 -    break;
 12.1363 -  case XENVBD_DEVICETYPE_CDROM:
 12.1364 -    break;
 12.1365 +    msg = DeviceData->XenDeviceData->XenBusInterface.List(XBT_NIL, "device/vbd", &VbdDevices);
 12.1366 +    if (!msg) {
 12.1367 +      for (i = 0; VbdDevices[i]; i++)
 12.1368 +      {
 12.1369 +        KdPrint((__DRIVER_NAME "     found existing vbd device %s\n", VbdDevices[i]));
 12.1370 +        RtlStringCbPrintfA(buffer, ARRAY_SIZE(buffer), "device/vbd/%s/state", VbdDevices[i]);
 12.1371 +        XenVbd_WatchHandler(buffer, DeviceData);
 12.1372 +//        WaitTimeout.QuadPart = -600000000;
 12.1373 +        KeWaitForSingleObject(&DeviceData->WaitDevicesEvent, Executive, KernelMode, FALSE, NULL);
 12.1374 +        KdPrint((__DRIVER_NAME "     %d devices enumerated\n", DeviceData->EnumeratedDevices));
 12.1375 +      }  
 12.1376 +    }
 12.1377 +/*
 12.1378 +      for (i = 0; VbdDevices[i]; i++)
 12.1379 +      {
 12.1380 +        KdPrint((__DRIVER_NAME "     found existing vbd device %s\n", VbdDevices[i]));
 12.1381 +        RtlStringCbPrintfA(buffer, ARRAY_SIZE(buffer), "device/vbd/%s/state", VbdDevices[i]);
 12.1382 +        XenVbd_WatchHandler(buffer, DeviceData);
 12.1383 +        //ExFreePoolWithTag(bdDevices[i], XENPCI_POOL_TAG);
 12.1384 +      }
 12.1385 +      KdPrint((__DRIVER_NAME "     Waiting for %d devices to be enumerated\n", i));
 12.1386 +      while (DeviceData->EnumeratedDevices < i)
 12.1387 +      {
 12.1388 +        WaitTimeout.QuadPart = -600000000;
 12.1389 +        if (KeWaitForSingleObject(&DeviceData->WaitDevicesEvent, Executive, KernelMode, FALSE, &WaitTimeout) == STATUS_TIMEOUT)
 12.1390 +        {
 12.1391 +          KdPrint((__DRIVER_NAME "     Wait timed out\n"));
 12.1392 +          break;
 12.1393 +        }
 12.1394 +        KdPrint((__DRIVER_NAME "     %d out of %d devices enumerated\n", DeviceData->EnumeratedDevices, i));
 12.1395 +      }  
 12.1396 +    }
 12.1397 +*/
 12.1398 +    ScsiPortNotification(BusChangeDetected, DeviceData, 0);
 12.1399 +    DeviceData->BusChangePending = 0;
 12.1400    }
 12.1401 -  *GetChildDeviceData(ChildDevice) = ChildDeviceData;
 12.1402 -
 12.1403 -  ChildDeviceData->FastPathUsed = 0;
 12.1404 -  ChildDeviceData->SlowPathUsed = 0;
 12.1405 -  ChildDeviceData->IrpAddedToList = 0;
 12.1406 -  ChildDeviceData->IrpRemovedFromList = 0;
 12.1407 -  ChildDeviceData->IrpAddedToRing = 0;
 12.1408 -  ChildDeviceData->IrpAddedToRingAtLastNotify = 0;
 12.1409 -  ChildDeviceData->IrpAddedToRingAtLastInterrupt = 0;
 12.1410 -  ChildDeviceData->IrpAddedToRingAtLastDpc = 0;
 12.1411 -  ChildDeviceData->IrpRemovedFromRing = 0;
 12.1412 -  ChildDeviceData->IrpCompleted = 0;
 12.1413 -
 12.1414 -  ChildDeviceData->Device = ChildDevice;
 12.1415 -  
 12.1416 -  KeInitializeSpinLock(&ChildDeviceData->Lock);
 12.1417 -  KeInitializeSpinLock(&ChildDeviceData->IrpListLock);
 12.1418 -  InitializeListHead(&ChildDeviceData->IrpListHead);
 12.1419 -
 12.1420 -  ChildDeviceData->shadow = ExAllocatePoolWithTag(NonPagedPool, sizeof(blkif_shadow_t) * BLK_RING_SIZE, XENVBD_POOL_TAG);
 12.1421 -  memset(ChildDeviceData->shadow, 0, sizeof(blkif_shadow_t) * BLK_RING_SIZE);
 12.1422 -  //KdPrint((__DRIVER_NAME "     Allocated shadow = %08x\n", ChildDeviceData->shadow));
 12.1423 -  for (i = 0; i < BLK_RING_SIZE; i++)
 12.1424 -    ChildDeviceData->shadow[i].req.id = i + 1;
 12.1425 -  ChildDeviceData->shadow_free = 0;
 12.1426 -  ChildDeviceData->shadow[BLK_RING_SIZE - 1].req.id = 0x0fffffff;
 12.1427 -
 12.1428 -  WDF_DPC_CONFIG_INIT(&DpcConfig, XenVbd_DpcThreadProc);
 12.1429 -  WDF_OBJECT_ATTRIBUTES_INIT(&DpcObjectAttributes);
 12.1430 -  DpcObjectAttributes.ParentObject = ChildDevice;
 12.1431 -  WdfDpcCreate(&DpcConfig, &DpcObjectAttributes, &ChildDeviceData->Dpc);
 12.1432 -
 12.1433 -  KdPrint((__DRIVER_NAME " <-- ChildListCreateDevice (status = %08x)\n", status));
 12.1434 -
 12.1435 -  return status;
 12.1436 -}
 12.1437 -
 12.1438 -
 12.1439 -// if the list isn't empty, then we should always add the request to the end of the list and then try and clear the list onto the ring
 12.1440 -
 12.1441 -// Call with device lock held
 12.1442 -static VOID
 12.1443 -XenVbd_PutIrpOnRing(WDFDEVICE Device, PIRP Irp)
 12.1444 -{
 12.1445 -  char *DataBuffer;
 12.1446 -  PSCSI_REQUEST_BLOCK Srb;
 12.1447 -  PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
 12.1448 -  PXENVBD_CHILD_DEVICE_DATA ChildDeviceData;
 12.1449 -  blkif_request_t *req;
 12.1450 -  int i;
 12.1451 -  ULONG j;
 12.1452 -  int BlockCount;
 12.1453 -  UINT8 sect_offset;
 12.1454 -
 12.1455 -  ChildDeviceData = *GetChildDeviceData(Device);
 12.1456 -
 12.1457 -//  if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.1458 -//    KdPrint((__DRIVER_NAME " --> PutIrpOnRing\n"));
 12.1459 -
 12.1460 -  if (RING_FULL(&ChildDeviceData->Ring))
 12.1461 -  {
 12.1462 -    KdPrint((__DRIVER_NAME "     RING IS FULL - EXPECT BADNESS\n"));
 12.1463 -  }
 12.1464 +  ScsiPortNotification(RequestTimerCall, DeviceExtension, XenVbd_CheckBusChangedTimer, 1000000);
 12.1465  
 12.1466 -  req = RING_GET_REQUEST(&ChildDeviceData->Ring, ChildDeviceData->Ring.req_prod_pvt);
 12.1467 -
 12.1468 -  //KdPrint((__DRIVER_NAME "     req = %08x\n", req));
 12.1469 -
 12.1470 -  Srb = irpSp->Parameters.Scsi.Srb;
 12.1471 -
 12.1472 -  req->sector_number = (Srb->Cdb[2] << 24) | (Srb->Cdb[3] << 16) | (Srb->Cdb[4] << 8) | Srb->Cdb[5];
 12.1473 -  BlockCount = (Srb->Cdb[7] << 8) | Srb->Cdb[8];
 12.1474 -
 12.1475 -  req->id = GET_ID_FROM_FREELIST(ChildDeviceData);
 12.1476 -
 12.1477 -  if (req->id == 0x0fffffff)
 12.1478 -  {
 12.1479 -    KdPrint((__DRIVER_NAME "     Something is horribly wrong in PutIrpOnRing\n"));
 12.1480 -  }
 12.1481 -
 12.1482 -  //KdPrint((__DRIVER_NAME "     id = %d\n", req->id));
 12.1483 -
 12.1484 -  req->handle = 0;
 12.1485 -  req->operation = (Srb->Cdb[0] == SCSIOP_READ)?BLKIF_OP_READ:BLKIF_OP_WRITE;
 12.1486 -  ChildDeviceData->shadow[req->id].Irp = Irp;
 12.1487 +  KdPrint((__DRIVER_NAME " <-- HwScsiInitialize\n"));
 12.1488  
 12.1489 -  if ((MmGetMdlByteOffset(Irp->MdlAddress) & 0x1ff) == 0) // 0x1ff shouldn't be hardcoded...
 12.1490 -  {
 12.1491 -    // fast path - zero copy
 12.1492 -    ChildDeviceData->shadow[req->id].Mdl = Irp->MdlAddress;
 12.1493 -    ChildDeviceData->shadow[req->id].Buf = NULL; // we don't need the virtual address
 12.1494 -    ChildDeviceData->FastPathUsed++;
 12.1495 -  }
 12.1496 -  else
 12.1497 -  {
 12.1498 -    // slow path - copy to bounce buffer
 12.1499 -    ChildDeviceData->shadow[req->id].Mdl = AllocatePages((BlockCount * ChildDeviceData->BytesPerSector + PAGE_SIZE - 1) / PAGE_SIZE);
 12.1500 -    ChildDeviceData->shadow[req->id].Buf = MmGetMdlVirtualAddress(ChildDeviceData->shadow[req->id].Mdl);
 12.1501 -    if (ChildDeviceData->shadow[req->id].Buf == NULL)
 12.1502 -    {
 12.1503 -      KdPrint((__DRIVER_NAME "     MmGetMdlVirtualAddress returned NULL in PutIrpOnRing\n"));
 12.1504 -    }
 12.1505 -    ChildDeviceData->SlowPathUsed++;
 12.1506 -  }
 12.1507 -
 12.1508 -//  if (((ChildDeviceData->FastPathUsed + ChildDeviceData->SlowPathUsed) & 0x2FF) == 0)
 12.1509 -//  {
 12.1510 -//    KdPrint((__DRIVER_NAME "     Fast Path = %d, Slow Path = %d\n", ChildDeviceData->FastPathUsed, ChildDeviceData->SlowPathUsed));
 12.1511 -//    KdPrint((__DRIVER_NAME "     AddedToList = %d, RemovedFromList = %d, AddedToRing = %d, AddedToRingAtLastNotify = %d, AddedToRingAtLastInterrupt = %d, AddedToRingAtLastDpc = %d, RemovedFromRing = %d, IrpCompleted = %d\n", ChildDeviceData->IrpAddedToList, ChildDeviceData->IrpRemovedFromList, ChildDeviceData->IrpAddedToRing, ChildDeviceData->IrpAddedToRingAtLastNotify, ChildDeviceData->IrpAddedToRingAtLastInterrupt, ChildDeviceData->IrpAddedToRingAtLastDpc, ChildDeviceData->IrpRemovedFromRing, ChildDeviceData->IrpCompleted));
 12.1512 -//  }
 12.1513 -
 12.1514 -  sect_offset = (UINT8)(MmGetMdlByteOffset(ChildDeviceData->shadow[req->id].Mdl) >> 9);
 12.1515 -  for (i = 0, req->nr_segments = 0; i < BlockCount; req->nr_segments++)
 12.1516 -  {
 12.1517 -    req->seg[req->nr_segments].gref = GntTblInterface.GrantAccess(
 12.1518 -      GntTblInterface.InterfaceHeader.Context,
 12.1519 -      0,
 12.1520 -      MmGetMdlPfnArray(ChildDeviceData->shadow[req->id].Mdl)[req->nr_segments],
 12.1521 -      FALSE);
 12.1522 -    req->seg[req->nr_segments].first_sect = sect_offset;
 12.1523 -    for (j = sect_offset; i < BlockCount && j < PAGE_SIZE / ChildDeviceData->BytesPerSector; j++, i++)
 12.1524 -      req->seg[req->nr_segments].last_sect = (uint8_t)j;
 12.1525 -    sect_offset = 0;
 12.1526 -  }
 12.1527 -  if (Srb->Cdb[0] == SCSIOP_WRITE && ChildDeviceData->shadow[req->id].Buf != NULL)
 12.1528 -  {
 12.1529 -    DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
 12.1530 -    if (DataBuffer == NULL)
 12.1531 -      KdPrint((__DRIVER_NAME "     MmGetSystemAddressForMdlSafe failed in PutIrpOnRing\n"));
 12.1532 -    memcpy(ChildDeviceData->shadow[req->id].Buf, DataBuffer, BlockCount * ChildDeviceData->BytesPerSector);
 12.1533 -  }
 12.1534 -  ChildDeviceData->shadow[req->id].req = *req;
 12.1535 -
 12.1536 -  ChildDeviceData->Ring.req_prod_pvt++;
 12.1537 -
 12.1538 -  ChildDeviceData->IrpAddedToRing++;
 12.1539 -
 12.1540 -//  if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.1541 -//    KdPrint((__DRIVER_NAME " <-- PutIrpOnRing\n"));
 12.1542 +  return TRUE;
 12.1543  }
 12.1544  
 12.1545  static ULONG
 12.1546 -XenVBD_FillModePage(PXENVBD_CHILD_DEVICE_DATA ChildDeviceData, UCHAR PageCode, PUCHAR DataBuffer, ULONG BufferLength, PULONG Offset)
 12.1547 +XenVbd_FillModePage(PXENVBD_DEVICE_DATA DeviceData, UCHAR PageCode, PUCHAR DataBuffer, ULONG BufferLength, PULONG Offset)
 12.1548  {
 12.1549    PMODE_RIGID_GEOMETRY_PAGE ModeRigidGeometry;
 12.1550  
 12.1551    switch (PageCode)
 12.1552    {
 12.1553 +/*
 12.1554    case MODE_PAGE_RIGID_GEOMETRY:
 12.1555 -    if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_DISK)
 12.1556 +    if (DeviceData->ScsiData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.1557      {
 12.1558      KdPrint((__DRIVER_NAME "     MODE_PAGE_RIGID_GEOMETRY\n"));
 12.1559      if (*Offset + sizeof(MODE_RIGID_GEOMETRY_PAGE) > BufferLength)
 12.1560 @@ -1171,16 +663,17 @@ XenVBD_FillModePage(PXENVBD_CHILD_DEVICE
 12.1561      ModeRigidGeometry->PageCode = PageCode;
 12.1562      ModeRigidGeometry->PageSavable = 0;
 12.1563      ModeRigidGeometry->PageLength = sizeof(MODE_RIGID_GEOMETRY_PAGE);
 12.1564 -    ModeRigidGeometry->NumberOfCylinders[0] = (UCHAR)((ChildDeviceData->Geometry.Cylinders.LowPart >> 16) & 0xFF);
 12.1565 -    ModeRigidGeometry->NumberOfCylinders[1] = (UCHAR)((ChildDeviceData->Geometry.Cylinders.LowPart >> 8) & 0xFF);
 12.1566 -    ModeRigidGeometry->NumberOfCylinders[2] = (UCHAR)((ChildDeviceData->Geometry.Cylinders.LowPart >> 0) & 0xFF);
 12.1567 -    ModeRigidGeometry->NumberOfHeads = (UCHAR)ChildDeviceData->Geometry.TracksPerCylinder;
 12.1568 +    ModeRigidGeometry->NumberOfCylinders[0] = (DeviceData->Geometry.Cylinders.LowPart >> 16) & 0xFF;
 12.1569 +    ModeRigidGeometry->NumberOfCylinders[1] = (DeviceData->Geometry.Cylinders.LowPart >> 8) & 0xFF;
 12.1570 +    ModeRigidGeometry->NumberOfCylinders[2] = (DeviceData->Geometry.Cylinders.LowPart >> 0) & 0xFF;
 12.1571 +    ModeRigidGeometry->NumberOfHeads = DeviceData->Geometry.TracksPerCylinder;
 12.1572      //ModeRigidGeometry->LandZoneCyclinder = 0;
 12.1573      ModeRigidGeometry->RoataionRate[0] = 0x05;
 12.1574      ModeRigidGeometry->RoataionRate[0] = 0x39;
 12.1575      *Offset += sizeof(MODE_RIGID_GEOMETRY_PAGE);
 12.1576      }
 12.1577      break;
 12.1578 +*/
 12.1579    case MODE_PAGE_FAULT_REPORTING:
 12.1580      break;
 12.1581    default:
 12.1582 @@ -1189,416 +682,306 @@ XenVBD_FillModePage(PXENVBD_CHILD_DEVICE
 12.1583    return 0;
 12.1584  }
 12.1585  
 12.1586 -static NTSTATUS
 12.1587 -XenVbd_Child_PreprocessWdmIrpDEVICE_CONTROL(WDFDEVICE Device, PIRP Irp)
 12.1588 +// Call with device lock held
 12.1589 +static VOID
 12.1590 +XenVbd_PutSrbOnRing(PXENVBD_TARGET_DATA TargetData, PSCSI_REQUEST_BLOCK Srb)
 12.1591  {
 12.1592 -  NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
 12.1593 -  PIO_STACK_LOCATION IrpStack;
 12.1594 -  PSCSI_ADDRESS ScsiAddress;
 12.1595 -  PXENVBD_CHILD_DEVICE_DATA ChildDeviceData;  
 12.1596 -  PSTORAGE_PROPERTY_QUERY Spq;
 12.1597 -  PSTORAGE_ADAPTER_DESCRIPTOR Sad;
 12.1598 -  PSTORAGE_DEVICE_DESCRIPTOR Sdd;
 12.1599 -  PSTORAGE_DEVICE_ID_DESCRIPTOR Sdid;
 12.1600 -  PSTORAGE_IDENTIFIER Si;
 12.1601 -//  ULONG Information;
 12.1602 -  int StructEndOffset;
 12.1603 +  //PUCHAR DataBuffer;
 12.1604 +  blkif_request_t *req;
 12.1605 +  int i;
 12.1606 +  int j;
 12.1607 +  int BlockCount;
 12.1608 +  int sect_offset;
 12.1609 +  PVOID CurrentVirtual;
 12.1610 +  ULONG CurrentLength;
 12.1611 +  ULONG SegmentLength;
 12.1612 +  SCSI_PHYSICAL_ADDRESS PageAddress;
 12.1613 +  int Iterations;
 12.1614 +  PXENVBD_DEVICE_DATA DeviceData = (PXENVBD_DEVICE_DATA)TargetData->DeviceData;
 12.1615  
 12.1616 -  KdPrint((__DRIVER_NAME " --> PreprocessWdmIrpDEVICE_CONTROL\n"));
 12.1617 +// can use SRB_STATUS_BUSY to push the SRB back to windows...
 12.1618  
 12.1619 -  ChildDeviceData = *GetChildDeviceData(Device);
 12.1620 +//  KdPrint((__DRIVER_NAME " --> PutSrbOnRing\n"));
 12.1621  
 12.1622 -  IrpStack = IoGetCurrentIrpStackLocation(Irp);
 12.1623 -
 12.1624 -  switch (IrpStack->Parameters.DeviceIoControl.IoControlCode)
 12.1625 +  if (RING_FULL(&TargetData->Ring))
 12.1626    {
 12.1627 -  case IOCTL_SCSI_GET_ADDRESS:
 12.1628 -    KdPrint((__DRIVER_NAME "     IOCTL_SCSI_GET_ADDRESS\n"));
 12.1629 -    Irp->IoStatus.Information = sizeof(SCSI_ADDRESS);
 12.1630 -    if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength >= sizeof(SCSI_ADDRESS))
 12.1631 -    {
 12.1632 -      ScsiAddress = (PSCSI_ADDRESS)Irp->AssociatedIrp.SystemBuffer;
 12.1633 -      ScsiAddress->Length = sizeof(SCSI_ADDRESS);
 12.1634 -      ScsiAddress->PortNumber = 0;
 12.1635 -      ScsiAddress->PathId = 0;
 12.1636 -      ScsiAddress->TargetId = 0;
 12.1637 -      ScsiAddress->Lun = 0;
 12.1638 -      Status = STATUS_SUCCESS;
 12.1639 -      Irp->IoStatus.Status = Status;
 12.1640 -      IoCompleteRequest(Irp, IO_NO_INCREMENT);      
 12.1641 -    }
 12.1642 -    else
 12.1643 -    {
 12.1644 -      KdPrint((__DRIVER_NAME "     (Buffer size too small @ %d\n", IrpStack->Parameters.DeviceIoControl.OutputBufferLength));    
 12.1645 -    }
 12.1646 -    break;
 12.1647 -  case IOCTL_STORAGE_QUERY_PROPERTY:
 12.1648 -    KdPrint((__DRIVER_NAME "     IOCTL_STORAGE_QUERY_PROPERTY\n"));    
 12.1649 -    Spq = (PSTORAGE_PROPERTY_QUERY)Irp->AssociatedIrp.SystemBuffer;
 12.1650 -    if (Spq->PropertyId == StorageAdapterProperty && Spq->QueryType == PropertyStandardQuery)
 12.1651 -    {
 12.1652 -      KdPrint((__DRIVER_NAME "     PropertyId = StorageAdapterProperty, QueryType = PropertyStandardQuery\n"));
 12.1653 -      Irp->IoStatus.Information = 0;
 12.1654 -      if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength >= 8)
 12.1655 -      {
 12.1656 -        Irp->IoStatus.Information = 8;
 12.1657 -        Sad = (PSTORAGE_ADAPTER_DESCRIPTOR)Irp->AssociatedIrp.SystemBuffer;
 12.1658 -        Sad->Version = 1;
 12.1659 -        Sad->Size = sizeof(STORAGE_ADAPTER_DESCRIPTOR);
 12.1660 -        if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength >= Sad->Size)
 12.1661 -        {
 12.1662 -          Irp->IoStatus.Information = Sad->Size;
 12.1663 -          Sad->MaximumTransferLength = 45056;
 12.1664 -          Sad->MaximumPhysicalPages = 11;
 12.1665 -          Sad->AlignmentMask = 0;
 12.1666 -          Sad->AdapterUsesPio = FALSE;
 12.1667 -          Sad->AdapterScansDown = FALSE;
 12.1668 -          Sad->CommandQueueing = FALSE;
 12.1669 -          Sad->AcceleratedTransfer = FALSE;
 12.1670 -          Sad->BusType = BusTypeScsi;
 12.1671 -          Sad->BusMajorVersion = 0;
 12.1672 -          Sad->BusMinorVersion = 0;
 12.1673 -        }
 12.1674 -      }
 12.1675 -      Status = STATUS_SUCCESS;
 12.1676 -      Irp->IoStatus.Status = Status;
 12.1677 -      IoCompleteRequest(Irp, IO_NO_INCREMENT);      
 12.1678 -    }
 12.1679 -    else if (Spq->PropertyId == StorageDeviceProperty && Spq->QueryType == PropertyStandardQuery)
 12.1680 -    {
 12.1681 -      KdPrint((__DRIVER_NAME "     PropertyId = StorageDeviceProperty, QueryType = PropertyStandardQuery\n"));
 12.1682 -      Irp->IoStatus.Information = 0;
 12.1683 -      if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength >= 8)
 12.1684 -      {
 12.1685 -        Irp->IoStatus.Information = 8;
 12.1686 -        Sdd = (PSTORAGE_DEVICE_DESCRIPTOR)Irp->AssociatedIrp.SystemBuffer;
 12.1687 -        Sdd->Version = 1;
 12.1688 -        Sdd->Size = &Sdd->RawDeviceProperties[36] - (PUCHAR)Sdd + 1;
 12.1689 -        // 0       0        1         2       3
 12.1690 -        // 0       7        5         4       1
 12.1691 -        //"VENDOR\0PRODUCT\0Revision\0Serial\0"
 12.1692 -        if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength >= Sdd->Size)
 12.1693 -        {
 12.1694 -          Irp->IoStatus.Information = Sdd->Size;
 12.1695 -          switch (ChildDeviceData->DeviceType)
 12.1696 -          { 
 12.1697 -          case XENVBD_DEVICETYPE_DISK:
 12.1698 -            Sdd->DeviceType = DIRECT_ACCESS_DEVICE;
 12.1699 -            Sdd->DeviceTypeModifier = 0x00;
 12.1700 -            Sdd->RemovableMedia = FALSE;
 12.1701 -            break;
 12.1702 -          case XENVBD_DEVICETYPE_CDROM:
 12.1703 -            Sdd->DeviceType = READ_ONLY_DIRECT_ACCESS_DEVICE;
 12.1704 -            Sdd->DeviceTypeModifier = 0x00;
 12.1705 -            Sdd->RemovableMedia = TRUE;
 12.1706 -            break;
 12.1707 -          default:
 12.1708 -            // wtf
 12.1709 -            break;
 12.1710 -          }
 12.1711 -          Sdd->CommandQueueing = FALSE;
 12.1712 -          StructEndOffset = Sdd->RawDeviceProperties - (PUCHAR)Sdd;
 12.1713 -          Sdd->VendorIdOffset = StructEndOffset + 0;
 12.1714 -          Sdd->ProductIdOffset = StructEndOffset + 7;
 12.1715 -          Sdd->ProductRevisionOffset = StructEndOffset + 15;
 12.1716 -          Sdd->SerialNumberOffset = StructEndOffset + 24;
 12.1717 -          Sdd->BusType = BusTypeScsi;
 12.1718 -          Sdd->RawPropertiesLength = 36;
 12.1719 -          memcpy(Sdd->RawDeviceProperties, "VENDOR\0PRODUCT\0Revision\0Serial99999\0", 36);
 12.1720 -        }
 12.1721 -      }
 12.1722 -      Status = STATUS_SUCCESS;
 12.1723 -      Irp->IoStatus.Status = Status;
 12.1724 -      IoCompleteRequest(Irp, IO_NO_INCREMENT);      
 12.1725 -    }
 12.1726 -    else if (Spq->PropertyId == StorageDeviceIdProperty && Spq->QueryType == PropertyStandardQuery)
 12.1727 -    {
 12.1728 -      KdPrint((__DRIVER_NAME "     PropertyId = StorageDeviceIdProperty, QueryType = PropertyStandardQuery\n"));
 12.1729 -      Irp->IoStatus.Information = 0;
 12.1730 -      if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength >= 8)
 12.1731 -      {
 12.1732 -        Irp->IoStatus.Information = 8;
 12.1733 -        Sdid = (PSTORAGE_DEVICE_ID_DESCRIPTOR)Irp->AssociatedIrp.SystemBuffer;
 12.1734 -        Sdid->Version = 1;
 12.1735 -        Si = (PSTORAGE_IDENTIFIER)Sdid->Identifiers;
 12.1736 -        Sdid->Size = &Si->Identifier[8] - (PUCHAR)Sdid + 1;
 12.1737 -        if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength >= Sdid->Size)
 12.1738 -        {
 12.1739 -          Irp->IoStatus.Information = Sdid->Size;
 12.1740 -          Sdid->NumberOfIdentifiers = 1;
 12.1741 -          Si->CodeSet = StorageIdCodeSetAscii;
 12.1742 -          Si->Type = StorageIdTypeScsiNameString;
 12.1743 -          //Si->CodeSet = StorageIdCodeSetBinary;
 12.1744 -          //Si->Type = StorageIdTypeEUI64;
 12.1745 -          Si->IdentifierSize = 9;
 12.1746 -          Si->NextOffset = 0;
 12.1747 -          Si->Association = StorageIdAssocPort;
 12.1748 -          Si->Identifier[0] = 'S';
 12.1749 -          Si->Identifier[1] = 'e';
 12.1750 -          Si->Identifier[2] = 'r';
 12.1751 -          Si->Identifier[3] = 'i';
 12.1752 -          Si->Identifier[4] = 'a';
 12.1753 -          Si->Identifier[5] = 'l';
 12.1754 -          Si->Identifier[6] = '9';
 12.1755 -          Si->Identifier[7] = '9';
 12.1756 -          Si->Identifier[6] = '9';
 12.1757 -          Si->Identifier[7] = '9';
 12.1758 -          Si->Identifier[8] = '9';
 12.1759 -          //Si->Identifier[8] = 0;
 12.1760 -        }
 12.1761 -      }
 12.1762 -      Status = STATUS_SUCCESS;
 12.1763 -      Irp->IoStatus.Status = Status;
 12.1764 -      IoCompleteRequest(Irp, IO_NO_INCREMENT);      
 12.1765 -    }
 12.1766 -    else
 12.1767 -    {
 12.1768 -      switch (Spq->PropertyId)
 12.1769 -      {
 12.1770 -      case StorageDeviceProperty:
 12.1771 -        KdPrint((__DRIVER_NAME "     StorageDeviceProperty\n"));
 12.1772 -        break;        
 12.1773 -      case StorageAccessAlignmentProperty:
 12.1774 -        KdPrint((__DRIVER_NAME "     StorageAccessAlignmentProperty\n"));
 12.1775 -        break;
 12.1776 -      case StorageAdapterProperty:
 12.1777 -        KdPrint((__DRIVER_NAME "     StorageAdapterProperty\n"));
 12.1778 -        break;
 12.1779 -      case StorageDeviceIdProperty:
 12.1780 -        KdPrint((__DRIVER_NAME "     StorageDeviceIdProperty\n"));
 12.1781 -        break;
 12.1782 -      case StorageDeviceUniqueIdProperty:
 12.1783 -        KdPrint((__DRIVER_NAME "     StorageDeviceUniqueIdProperty\n"));
 12.1784 -        break;
 12.1785 -      case StorageDeviceWriteCacheProperty:
 12.1786 -        KdPrint((__DRIVER_NAME "     StorageDeviceWriteCacheProperty\n"));
 12.1787 -        break;
 12.1788 -      default:
 12.1789 -        KdPrint((__DRIVER_NAME "     Unknown Property %08x\n", Spq->PropertyId));
 12.1790 -        break;
 12.1791 -      }
 12.1792 -      switch (Spq->QueryType)
 12.1793 -      {
 12.1794 -      case PropertyStandardQuery:
 12.1795 -        KdPrint((__DRIVER_NAME "     PropertyStandardQuery\n"));
 12.1796 -        break;        
 12.1797 -      case PropertyExistsQuery:
 12.1798 -        KdPrint((__DRIVER_NAME "     PropertyExistsQuery\n"));
 12.1799 -        break;        
 12.1800 -      default:
 12.1801 -        KdPrint((__DRIVER_NAME "     Unknown Query %08x\n", Spq->QueryType));
 12.1802 -        break;
 12.1803 -      }
 12.1804 -      Status = STATUS_NOT_IMPLEMENTED;
 12.1805 -      Irp->IoStatus.Status = Status;
 12.1806 -      IoCompleteRequest(Irp, IO_NO_INCREMENT);      
 12.1807 -    }
 12.1808 -    break;
 12.1809 -  // http://www.osronline.com/article.cfm?article=229
 12.1810 -  // 0x00560030 device = 0x56, Function = 0x00c = 
 12.1811 -  case IOCTL_DISK_GET_DRIVE_GEOMETRY:
 12.1812 -    KdPrint((__DRIVER_NAME "     IOCTL_DISK_GET_DRIVE_GEOMETRY\n"));
 12.1813 -    memcpy(Irp->AssociatedIrp.SystemBuffer, &ChildDeviceData->Geometry, sizeof(DISK_GEOMETRY));
 12.1814 -    Status = STATUS_SUCCESS;
 12.1815 -    Irp->IoStatus.Status = Status;
 12.1816 -    IoCompleteRequest(Irp, IO_NO_INCREMENT);      
 12.1817 -    break;
 12.1818 -  case FT_BALANCED_READ_MODE: // just pretend we know what this is...
 12.1819 -    KdPrint((__DRIVER_NAME "     FT_BALANCED_READ_MODE\n"));
 12.1820 -    Status = STATUS_SUCCESS;
 12.1821 -    Irp->IoStatus.Status = Status;
 12.1822 -    IoCompleteRequest(Irp, IO_NO_INCREMENT);      
 12.1823 -    break;
 12.1824 -  default:
 12.1825 -    KdPrint((__DRIVER_NAME "     Control Code = %08x\n", IrpStack->Parameters.DeviceIoControl.IoControlCode));
 12.1826 -    Status = STATUS_NOT_IMPLEMENTED;
 12.1827 -    Irp->IoStatus.Status = Status;
 12.1828 -    IoCompleteRequest(Irp, IO_NO_INCREMENT);      
 12.1829 +    KdPrint((__DRIVER_NAME "     RING IS FULL - EXPECT BADNESS\n"));
 12.1830 +    // TODO: Fail badly here
 12.1831    }
 12.1832  
 12.1833 -  KdPrint((__DRIVER_NAME " <-- PreprocessWdmIrpDEVICE_CONTROL\n"));
 12.1834 +  req = RING_GET_REQUEST(&TargetData->Ring, TargetData->Ring.req_prod_pvt);
 12.1835  
 12.1836 -  return Status;
 12.1837 +  req->sector_number = (Srb->Cdb[2] << 24) | (Srb->Cdb[3] << 16) | (Srb->Cdb[4] << 8) | Srb->Cdb[5];
 12.1838 +  BlockCount = (Srb->Cdb[7] << 8) | Srb->Cdb[8];
 12.1839 +
 12.1840 +  req->id = GET_ID_FROM_FREELIST(TargetData);
 12.1841 +
 12.1842 +  if (req->id == 0x0fffffff)
 12.1843 +  {
 12.1844 +    KdPrint((__DRIVER_NAME "     Something is horribly wrong in PutSrbOnRing\n"));
 12.1845 +  }
 12.1846 +
 12.1847 +  req->handle = 0;
 12.1848 +  req->operation = (Srb->Cdb[0] == SCSIOP_READ)?BLKIF_OP_READ:BLKIF_OP_WRITE;
 12.1849 +  TargetData->shadow[req->id].Srb = Srb;
 12.1850 +
 12.1851 +//  KdPrint((__DRIVER_NAME "     DataBuffer = %08X\n", Srb->DataBuffer));
 12.1852 +//  KdPrint((__DRIVER_NAME "     BlockCount = %08X\n", BlockCount));
 12.1853 +
 12.1854 +  req->nr_segments = (BlockCount * TargetData->BytesPerSector + PAGE_SIZE - 1) / PAGE_SIZE;
 12.1855 +//  KdPrint((__DRIVER_NAME "     req->nr_segments = %08X\n", req->nr_segments));
 12.1856 +
 12.1857 +  for (i = 0; i < req->nr_segments; i++)
 12.1858 +  {
 12.1859 +    req->seg[i].gref = DeviceData->XenDeviceData->GntTblInterface.GrantAccess(0, MmGetMdlPfnArray(TargetData->shadow[req->id].Mdl)[i], FALSE);
 12.1860 +    req->seg[i].first_sect = 0;
 12.1861 +    if (i == req->nr_segments - 1)
 12.1862 +      req->seg[i].last_sect = (BlockCount - 1) % (PAGE_SIZE / TargetData->BytesPerSector);
 12.1863 +    else
 12.1864 +      req->seg[i].last_sect = PAGE_SIZE / TargetData->BytesPerSector - 1;
 12.1865 +//    KdPrint((__DRIVER_NAME "     Page %d, first_sect = %d, last_sect = %d\n", i, req->seg[i].first_sect, req->seg[i].last_sect));
 12.1866 +  }
 12.1867 +  if (Srb->Cdb[0] == SCSIOP_WRITE)
 12.1868 +    memcpy(TargetData->shadow[req->id].Buf, Srb->DataBuffer, BlockCount * TargetData->BytesPerSector);
 12.1869 +  TargetData->shadow[req->id].req = *req;
 12.1870 +  TargetData->Ring.req_prod_pvt++;
 12.1871 +
 12.1872 +//  KdPrint((__DRIVER_NAME " <-- PutSrbOnRing\n"));
 12.1873  }
 12.1874  
 12.1875 -static NTSTATUS
 12.1876 -XenVbd_Child_PreprocessWdmIrpSomethingSomething(WDFDEVICE Device, PIRP Irp)
 12.1877 -{
 12.1878 -  NTSTATUS Status;
 12.1879 -  PIO_STACK_LOCATION IrpStack;
 12.1880 -
 12.1881 -  KdPrint((__DRIVER_NAME " --> XenVbd_Child_PreprocessWdmIrpSomethingSomething\n"));
 12.1882 -
 12.1883 -  IrpStack = IoGetCurrentIrpStackLocation(Irp);
 12.1884 -  KdPrint((__DRIVER_NAME "     Major = %02X, Minor = %02X\n", IrpStack->MajorFunction, IrpStack->MinorFunction));
 12.1885 -
 12.1886 -  IoSkipCurrentIrpStackLocation(Irp);
 12.1887 -
 12.1888 -  Status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
 12.1889 -
 12.1890 -  KdPrint((__DRIVER_NAME " <-- XenVbd_Child_PreprocessWdmIrpSomethingSomething\n"));
 12.1891 -
 12.1892 -  return Status;
 12.1893 -}
 12.1894 -
 12.1895 -static NTSTATUS
 12.1896 -XenVbd_Child_PreprocessWdmIrpSCSI(WDFDEVICE Device, PIRP Irp)
 12.1897 +static BOOLEAN
 12.1898 +XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK Srb)
 12.1899  {
 12.1900    PUCHAR DataBuffer;
 12.1901 -  NTSTATUS status = STATUS_SUCCESS;
 12.1902 -  PSCSI_REQUEST_BLOCK Srb;
 12.1903 -  PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
 12.1904 -  PXENVBD_CHILD_DEVICE_DATA ChildDeviceData;
 12.1905 -  KIRQL KIrql;
 12.1906 -  XenVbd_ListEntry *ListEntry;
 12.1907 -  int notify;
 12.1908    PCDB cdb;
 12.1909 -  //PUCHAR Ptr;
 12.1910 -  ULONG i;
 12.1911 -
 12.1912 -  ChildDeviceData = *GetChildDeviceData(Device);
 12.1913 +  PXENVBD_DEVICE_DATA DeviceData = (PXENVBD_DEVICE_DATA)DeviceExtension;
 12.1914 +  PXENVBD_TARGET_DATA TargetData;
 12.1915 +  unsigned int i;
 12.1916 +  KIRQL KIrql;
 12.1917 +  int notify;
 12.1918 +  SCSI_PHYSICAL_ADDRESS ScsiPhysicalAddress;
 12.1919 +  ULONG Length;
 12.1920  
 12.1921 -//  if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.1922 -//    KdPrint((__DRIVER_NAME " --> WdmIrpPreprocessSCSI\n"));
 12.1923 +//  KdPrint((__DRIVER_NAME " --> HwScsiStartIo PathId = %d, TargetId = %d, Lun = %d\n", Srb->PathId, Srb->TargetId, Srb->Lun));
 12.1924 +//  KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
 12.1925  
 12.1926 -  //KdPrint((__DRIVER_NAME "     SCSI Minor = %02X\n", irpSp->MinorFunction));
 12.1927 +  if (Srb->PathId >= SCSI_BUSES || Srb->TargetId >= SCSI_TARGETS_PER_BUS)
 12.1928 +  {
 12.1929 +    Srb->SrbStatus = SRB_STATUS_NO_DEVICE;
 12.1930 +    ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.1931 +    ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.1932 +    KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Out of bounds)\n"));
 12.1933 +    return TRUE;
 12.1934 +  }
 12.1935  
 12.1936 -  Srb = irpSp->Parameters.Scsi.Srb;
 12.1937 +  TargetData = &DeviceData->BusData[Srb->PathId].TargetData[Srb->TargetId];
 12.1938 +
 12.1939 +  if (!TargetData->Present)
 12.1940 +  {
 12.1941 +    Srb->SrbStatus = SRB_STATUS_NO_DEVICE;
 12.1942 +    ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.1943 +    ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.1944 +    KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Not Present)\n"));
 12.1945 +    return TRUE;
 12.1946 +  }
 12.1947  
 12.1948    switch (Srb->Function)
 12.1949    {
 12.1950    case SRB_FUNCTION_EXECUTE_SCSI:
 12.1951      cdb = (PCDB)Srb->Cdb;
 12.1952 -//    if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.1953 -//      KdPrint((__DRIVER_NAME "     SRB_FUNCTION_EXECUTE_SCSI\n"));
 12.1954 -    switch(cdb->CDB6GENERIC.OperationCode) //Srb->Cdb[0])
 12.1955 +//    KdPrint((__DRIVER_NAME "     SRB_FUNCTION_EXECUTE_SCSI\n"));
 12.1956 +    switch(cdb->CDB6GENERIC.OperationCode)
 12.1957      {
 12.1958      case SCSIOP_TEST_UNIT_READY:
 12.1959 -//      if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.1960 -//        KdPrint((__DRIVER_NAME "     Command = TEST_UNIT_READY\n"));
 12.1961 +      KdPrint((__DRIVER_NAME "     Command = TEST_UNIT_READY\n"));
 12.1962        Srb->SrbStatus = SRB_STATUS_SUCCESS;
 12.1963        Srb->ScsiStatus = 0;
 12.1964 -      status = STATUS_SUCCESS;
 12.1965 -      Irp->IoStatus.Status = status;
 12.1966 -      Irp->IoStatus.Information = 0;
 12.1967 -      IoCompleteRequest(Irp, IO_NO_INCREMENT);
 12.1968 +      ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.1969 +      ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.1970        break;
 12.1971      case SCSIOP_INQUIRY:
 12.1972        KdPrint((__DRIVER_NAME "     Command = INQUIRY\n"));
 12.1973        KdPrint((__DRIVER_NAME "     (LUN = %d, EVPD = %d, Page Code = %02X)\n", Srb->Cdb[1] >> 5, Srb->Cdb[1] & 1, Srb->Cdb[2]));
 12.1974 -      if ((Srb->Cdb[1] & 1) == 0)
 12.1975 +      KdPrint((__DRIVER_NAME "     (Length = %d)\n", Srb->DataTransferLength));
 12.1976 +      KdPrint((__DRIVER_NAME "     (Srb->Databuffer = %08x)\n", Srb->DataBuffer));
 12.1977 +//      KdPrint((__DRIVER_NAME "     PhysicalAddress.LowPart = %08x\n", ScsiPortGetPhysicalAddress(DeviceData, Srb, Srb->DataBuffer, &Length).LowPart));
 12.1978 +//      DataBuffer = ScsiPortGetVirtualAddress(DeviceData, ScsiPortGetPhysicalAddress(DeviceData, Srb, Srb->DataBuffer, &Length));
 12.1979 +//      KdPrint((__DRIVER_NAME "     (Databuffer = %08x)\n", DataBuffer));
 12.1980 +//      break;
 12.1981 +      DataBuffer = Srb->DataBuffer;
 12.1982 +      RtlZeroMemory(DataBuffer, Srb->DataTransferLength);
 12.1983 +      Srb->SrbStatus = SRB_STATUS_SUCCESS;
 12.1984 +      switch (TargetData->DeviceType)
 12.1985        {
 12.1986 -//        DataBuffer = Srb->DataBuffer;
 12.1987 -        DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
 12.1988 -        RtlZeroMemory(DataBuffer, Srb->DataTransferLength);
 12.1989 -
 12.1990 -        DataBuffer[0] = 0x00; // disk
 12.1991 -        DataBuffer[1] = 0x00; // not removable
 12.1992 -        memcpy(DataBuffer + 8, "James   ", 8); // vendor id
 12.1993 -        memcpy(DataBuffer + 16, "XenVBD          ", 8); // product id
 12.1994 -        memcpy(DataBuffer + 32, "000", 8); // product revision level
 12.1995 -        Srb->SrbStatus = SRB_STATUS_SUCCESS;
 12.1996 +      case XENVBD_DEVICETYPE_DISK:
 12.1997 +        if ((Srb->Cdb[1] & 1) == 0)
 12.1998 +        {
 12.1999 +          DataBuffer[0] = DIRECT_ACCESS_DEVICE;
 12.2000 +          DataBuffer[1] = 0x00; // not removable
 12.2001 +          DataBuffer[3] = 32;
 12.2002 +          memcpy(DataBuffer + 8, "XEN     ", 8); // vendor id
 12.2003 +          memcpy(DataBuffer + 16, "PV VBD          ", 16); // product id
 12.2004 +          memcpy(DataBuffer + 32, "0000", 4); // product revision level
 12.2005 +        }
 12.2006 +        else
 12.2007 +        {
 12.2008 +          switch (Srb->Cdb[2])
 12.2009 +          {
 12.2010 +          case 0x00:
 12.2011 +            DataBuffer[0] = DIRECT_ACCESS_DEVICE;
 12.2012 +            DataBuffer[1] = 0x00;
 12.2013 +            DataBuffer[2] = 0x00;
 12.2014 +            DataBuffer[3] = 2;
 12.2015 +            DataBuffer[4] = 0x00;
 12.2016 +            DataBuffer[5] = 0x80;
 12.2017 +            break;
 12.2018 +          case 0x80:
 12.2019 +            DataBuffer[0] = DIRECT_ACCESS_DEVICE;
 12.2020 +            DataBuffer[1] = 0x80;
 12.2021 +            DataBuffer[2] = 0x00;
 12.2022 +            DataBuffer[3] = 8;
 12.2023 +            DataBuffer[4] = 0x31;
 12.2024 +            DataBuffer[5] = 0x32;
 12.2025 +            DataBuffer[6] = 0x33;
 12.2026 +            DataBuffer[7] = 0x34;
 12.2027 +            DataBuffer[8] = 0x35;
 12.2028 +            DataBuffer[9] = 0x36;
 12.2029 +            DataBuffer[10] = 0x37;
 12.2030 +            DataBuffer[11] = 0x38;
 12.2031 +            break;
 12.2032 +          default:
 12.2033 +            KdPrint((__DRIVER_NAME "     Unknown Page %02x requested\n", Srb->Cdb[2]));
 12.2034 +            Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
 12.2035 +            break;
 12.2036 +          }
 12.2037 +        }
 12.2038 +        break;
 12.2039 +      case XENVBD_DEVICETYPE_CDROM:
 12.2040 +        if ((Srb->Cdb[1] & 1) == 0)
 12.2041 +        {
 12.2042 +          DataBuffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
 12.2043 +          DataBuffer[1] = 0x01; // removable
 12.2044 +          DataBuffer[3] = 32;
 12.2045 +          memcpy(DataBuffer + 8, "XEN     ", 8); // vendor id
 12.2046 +          memcpy(DataBuffer + 16, "PV VBD          ", 16); // product id
 12.2047 +          memcpy(DataBuffer + 32, "0000", 4); // product revision level
 12.2048 +        }
 12.2049 +        else
 12.2050 +        {
 12.2051 +          switch (Srb->Cdb[2])
 12.2052 +          {
 12.2053 +          case 0x00:
 12.2054 +            DataBuffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
 12.2055 +            DataBuffer[1] = 0x00;
 12.2056 +            DataBuffer[2] = 0x00;
 12.2057 +            DataBuffer[3] = 2;
 12.2058 +            DataBuffer[4] = 0x00;
 12.2059 +            DataBuffer[5] = 0x80;
 12.2060 +            break;
 12.2061 +          case 0x80:
 12.2062 +            DataBuffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
 12.2063 +            DataBuffer[1] = 0x80;
 12.2064 +            DataBuffer[2] = 0x00;
 12.2065 +            DataBuffer[3] = 8;
 12.2066 +            DataBuffer[4] = 0x31;
 12.2067 +            DataBuffer[5] = 0x32;
 12.2068 +            DataBuffer[6] = 0x33;
 12.2069 +            DataBuffer[7] = 0x34;
 12.2070 +            DataBuffer[8] = 0x35;
 12.2071 +            DataBuffer[9] = 0x36;
 12.2072 +            DataBuffer[10] = 0x37;
 12.2073 +            DataBuffer[11] = 0x38;
 12.2074 +            break;
 12.2075 +          default:
 12.2076 +            KdPrint((__DRIVER_NAME "     Unknown Page %02x requested\n", Srb->Cdb[2]));
 12.2077 +            Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
 12.2078 +            break;
 12.2079 +          }
 12.2080 +        }
 12.2081 +        break;
 12.2082 +      default:
 12.2083 +        KdPrint((__DRIVER_NAME "     Unknown DeviceType %02x requested\n", TargetData->DeviceType));
 12.2084 +        Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
 12.2085 +        break;
 12.2086        }
 12.2087 -      else
 12.2088 -      {
 12.2089 -        KdPrint((__DRIVER_NAME "     Command = INQUIRY (LUN = %d, EVPD = %d, Page Code = %02X)\n", Srb->Cdb[1] >> 5, Srb->Cdb[1] & 1, Srb->Cdb[2]));
 12.2090 -        Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
 12.2091 -      }
 12.2092 -      Srb->ScsiStatus = 0;
 12.2093 -      status = STATUS_SUCCESS;
 12.2094 -      Irp->IoStatus.Status = status;
 12.2095 -      Irp->IoStatus.Information = 0;
 12.2096 -      IoCompleteRequest(Irp, IO_NO_INCREMENT);
 12.2097 +      ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.2098 +      ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.2099        break;
 12.2100      case SCSIOP_READ_CAPACITY:
 12.2101 -//      if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.2102 -//        KdPrint((__DRIVER_NAME "     Command = READ_CAPACITY\n"));
 12.2103 -//      DataBuffer = Srb->DataBuffer;
 12.2104 -      DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
 12.2105 -      DataBuffer[0] = (unsigned char)(ChildDeviceData->TotalSectors >> 24) & 0xff;
 12.2106 -      DataBuffer[1] = (unsigned char)(ChildDeviceData->TotalSectors >> 16) & 0xff;
 12.2107 -      DataBuffer[2] = (unsigned char)(ChildDeviceData->TotalSectors >> 8) & 0xff;
 12.2108 -      DataBuffer[3] = (unsigned char)(ChildDeviceData->TotalSectors >> 0) & 0xff;
 12.2109 -      DataBuffer[4] = (unsigned char)(ChildDeviceData->BytesPerSector >> 24) & 0xff;
 12.2110 -      DataBuffer[5] = (unsigned char)(ChildDeviceData->BytesPerSector >> 16) & 0xff;
 12.2111 -      DataBuffer[6] = (unsigned char)(ChildDeviceData->BytesPerSector >> 8) & 0xff;
 12.2112 -      DataBuffer[7] = (unsigned char)(ChildDeviceData->BytesPerSector >> 0) & 0xff;
 12.2113 +      KdPrint((__DRIVER_NAME "     Command = READ_CAPACITY\n"));
 12.2114 +      DataBuffer = Srb->DataBuffer;
 12.2115 +      RtlZeroMemory(DataBuffer, Srb->DataTransferLength);
 12.2116 +      DataBuffer[0] = (unsigned char)((TargetData->TotalSectors - 1) >> 24) & 0xff;
 12.2117 +      DataBuffer[1] = (unsigned char)((TargetData->TotalSectors - 1) >> 16) & 0xff;
 12.2118 +      DataBuffer[2] = (unsigned char)((TargetData->TotalSectors - 1) >> 8) & 0xff;
 12.2119 +      DataBuffer[3] = (unsigned char)((TargetData->TotalSectors - 1) >> 0) & 0xff;
 12.2120 +      DataBuffer[4] = (unsigned char)(TargetData->BytesPerSector >> 24) & 0xff;
 12.2121 +      DataBuffer[5] = (unsigned char)(TargetData->BytesPerSector >> 16) & 0xff;
 12.2122 +      DataBuffer[6] = (unsigned char)(TargetData->BytesPerSector >> 8) & 0xff;
 12.2123 +      DataBuffer[7] = (unsigned char)(TargetData->BytesPerSector >> 0) & 0xff;
 12.2124        Srb->ScsiStatus = 0;
 12.2125        Srb->SrbStatus = SRB_STATUS_SUCCESS;
 12.2126 -      status = STATUS_SUCCESS;
 12.2127 -      Irp->IoStatus.Status = status;
 12.2128 -      Irp->IoStatus.Information = 0;
 12.2129 -      IoCompleteRequest(Irp, IO_NO_INCREMENT);
 12.2130 +      ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.2131 +      ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.2132        break;
 12.2133      case SCSIOP_MODE_SENSE:
 12.2134        KdPrint((__DRIVER_NAME "     Command = MODE_SENSE (DBD = %d, PC = %d, Page Code = %02x)\n", Srb->Cdb[1] & 0x10, Srb->Cdb[2] & 0xC0, Srb->Cdb[2] & 0x3F));
 12.2135        KdPrint((__DRIVER_NAME "     Length = %d\n", Srb->DataTransferLength));
 12.2136  
 12.2137 -      status = STATUS_SUCCESS; 
 12.2138 -      DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
 12.2139 -//      DataBuffer = Srb->DataBuffer;
 12.2140 +      Srb->ScsiStatus = 0;
 12.2141 +      Srb->SrbStatus = SRB_STATUS_SUCCESS;
 12.2142 +      Srb->DataTransferLength = 0;
 12.2143 +      DataBuffer = Srb->DataBuffer;
 12.2144        RtlZeroMemory(DataBuffer, Srb->DataTransferLength);
 12.2145 -      switch(cdb->MODE_SENSE.PageCode) //Srb->Cdb[2] & 0x3F)
 12.2146 +      switch(cdb->MODE_SENSE.PageCode)
 12.2147        {
 12.2148        case MODE_SENSE_RETURN_ALL:
 12.2149 -        Irp->IoStatus.Information = 0;
 12.2150          //Ptr = (UCHAR *)Srb->DataBuffer;
 12.2151          for (i = 0; i < MODE_SENSE_RETURN_ALL; i++)
 12.2152          {
 12.2153 -          if (XenVBD_FillModePage(ChildDeviceData, cdb->MODE_SENSE.PageCode, DataBuffer, cdb->MODE_SENSE.AllocationLength, &Irp->IoStatus.Information))
 12.2154 +          if (XenVbd_FillModePage(DeviceData, cdb->MODE_SENSE.PageCode, DataBuffer, cdb->MODE_SENSE.AllocationLength, &Srb->DataTransferLength))
 12.2155            {
 12.2156              break;
 12.2157            }
 12.2158          }
 12.2159          break;
 12.2160        default:
 12.2161 -        XenVBD_FillModePage(ChildDeviceData, cdb->MODE_SENSE.PageCode, DataBuffer, cdb->MODE_SENSE.AllocationLength, &Irp->IoStatus.Information);
 12.2162 +        XenVbd_FillModePage(DeviceData, cdb->MODE_SENSE.PageCode, DataBuffer, cdb->MODE_SENSE.AllocationLength, &Srb->DataTransferLength);
 12.2163          break;
 12.2164        }
 12.2165 -      Srb->DataTransferLength = Irp->IoStatus.Information;
 12.2166 -//      Srb->ScsiStatus = 0;
 12.2167 -      Srb->SrbStatus = SRB_STATUS_SUCCESS;
 12.2168 -      Irp->IoStatus.Status = status;
 12.2169 -      IoCompleteRequest(Irp, IO_NO_INCREMENT);
 12.2170 +      ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.2171 +      ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.2172        break;
 12.2173      case SCSIOP_READ:
 12.2174      case SCSIOP_WRITE:
 12.2175 -//      if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.2176 -//        KdPrint((__DRIVER_NAME "     Command = READ/WRITE\n"));
 12.2177 -
 12.2178 -      IoMarkIrpPending(Irp);
 12.2179 -
 12.2180 -      //KdPrint((__DRIVER_NAME "     Irp Acquiring Lock\n"));
 12.2181 -      KeAcquireSpinLock(&ChildDeviceData->Lock, &KIrql);
 12.2182 -      //KdPrint((__DRIVER_NAME "A    Got It\n"));
 12.2183 -
 12.2184 -      if (RING_FULL(&ChildDeviceData->Ring))
 12.2185 +//      KdPrint((__DRIVER_NAME "     Command = READ/WRITE\n"));
 12.2186 +/*
 12.2187 +      for (i = 0; i < 10; i++)
 12.2188        {
 12.2189 -        //KdPrint((__DRIVER_NAME "A    Inserting into list\n"));
 12.2190 -        ListEntry = (XenVbd_ListEntry *)ExAllocatePoolWithTag(NonPagedPool, sizeof(XenVbd_ListEntry), XENVBD_POOL_TAG);
 12.2191 -        //KdPrint((__DRIVER_NAME "     Allocate ListEntry = %08x\n", ListEntry));
 12.2192 -        if (ListEntry == NULL)
 12.2193 -        {
 12.2194 -          KdPrint((__DRIVER_NAME "     CANNOT ALLOCATE MEMORY FOR ListEntry!!!\n"));
 12.2195 -        }
 12.2196 -        ListEntry->Irp = Irp;
 12.2197 -        InsertTailList(&ChildDeviceData->IrpListHead, &ListEntry->Entry);
 12.2198 -        ChildDeviceData->IrpAddedToList++;
 12.2199 +        KdPrint((__DRIVER_NAME "     %02x: %02x\n", i, Srb->Cdb[i]));
 12.2200        }
 12.2201 -      else
 12.2202 -      {
 12.2203 -        XenVbd_PutIrpOnRing(Device, Irp);
 12.2204 -        RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&ChildDeviceData->Ring, notify);
 12.2205 -        if (notify)
 12.2206 -          EvtChnInterface.Notify(EvtChnInterface.InterfaceHeader.Context,
 12.2207 -            ChildDeviceData->EventChannel);
 12.2208 -        //KdPrint((__DRIVER_NAME "     WdmIrpPreprocessSCSI (AddedToList = %d, RemovedFromList = %d, AddedToRing = %d, AddedToRingAtLastNotify = %d, AddedToRingAtLastInterrupt = %d, AddedToRingAtLastDpc = %d, RemovedFromRing = %d, IrpCompleted = %d)\n", ChildDeviceData->IrpAddedToList, ChildDeviceData->IrpRemovedFromList, ChildDeviceData->IrpAddedToRing, ChildDeviceData->IrpAddedToRingAtLastNotify, ChildDeviceData->IrpAddedToRingAtLastInterrupt, ChildDeviceData->IrpAddedToRingAtLastDpc, ChildDeviceData->IrpRemovedFromRing, ChildDeviceData->IrpCompleted));
 12.2209 -      }
 12.2210 -      KeReleaseSpinLock(&ChildDeviceData->Lock, KIrql);
 12.2211 -      status = STATUS_PENDING;
 12.2212 +*/
 12.2213 +      //KeAcquireSpinLock(&DeviceData->Lock, &KIrql);
 12.2214 +
 12.2215 +      XenVbd_PutSrbOnRing(TargetData, Srb);
 12.2216 +      RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&TargetData->Ring, notify);
 12.2217 +      if (notify)
 12.2218 +        DeviceData->XenDeviceData->EvtChnInterface.Notify(TargetData->EventChannel);
 12.2219 +      //KeReleaseSpinLock(&DeviceData->Lock, KIrql);
 12.2220 +//      Srb->SrbStatus = SRB_STATUS_SUCCESS;
 12.2221 +//      ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.2222 +      if (!RING_FULL(&TargetData->Ring))
 12.2223 +        ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.2224 +      break;
 12.2225 +    case SCSIOP_REPORT_LUNS:
 12.2226 +      KdPrint((__DRIVER_NAME "     Command = REPORT_LUNS\n"));
 12.2227 +      Srb->SrbStatus = SRB_STATUS_SUCCESS; //SRB_STATUS_INVALID_REQUEST;
 12.2228 +      ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.2229 +      ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.2230        break;
 12.2231      case SCSIOP_READ_TOC:
 12.2232 -      DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
 12.2233 +      DataBuffer = Srb->DataBuffer;
 12.2234 +//      DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
 12.2235  /*
 12.2236  #define READ_TOC_FORMAT_TOC         0x00
 12.2237  #define READ_TOC_FORMAT_SESSION     0x01
 12.2238 @@ -1638,73 +1021,108 @@ XenVbd_Child_PreprocessWdmIrpSCSI(WDFDEV
 12.2239          Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
 12.2240          break;
 12.2241        }
 12.2242 -      Irp->IoStatus.Status = status;
 12.2243 -      IoCompleteRequest(Irp, IO_NO_INCREMENT);
 12.2244 +      ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.2245 +      ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.2246        break;
 12.2247 -/*
 12.2248 -    case SCSIOP_GET_CONFIGURATION:
 12.2249 -#define SCSI_GET_CONFIGURATION_REQUEST_TYPE_ALL     0x0
 12.2250 -#define SCSI_GET_CONFIGURATION_REQUEST_TYPE_CURRENT 0x1
 12.2251 -#define SCSI_GET_CONFIGURATION_REQUEST_TYPE_ONE     0x2
 12.2252 -
 12.2253 -        UCHAR OperationCode;       // 0x46 - SCSIOP_GET_CONFIGURATION
 12.2254 -        UCHAR RequestType : 2;     // SCSI_GET_CONFIGURATION_REQUEST_TYPE_*
 12.2255 -        UCHAR Reserved1   : 6;     // includes obsolete LUN field
 12.2256 -        UCHAR StartingFeature[2];
 12.2257 -        UCHAR Reserved2[3];
 12.2258 -        UCHAR AllocationLength[2];
 12.2259 -        UCHAR Control;
 12.2260 -      break;
 12.2261 -*/     
 12.2262      default:
 12.2263        KdPrint((__DRIVER_NAME "     Unhandled EXECUTE_SCSI Command = %02X\n", Srb->Cdb[0]));
 12.2264        Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
 12.2265 -      Irp->IoStatus.Status = status;
 12.2266 -      IoCompleteRequest(Irp, IO_NO_INCREMENT);
 12.2267 +      ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.2268 +      ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.2269        break;
 12.2270      }
 12.2271 -    //status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
 12.2272      break;
 12.2273    case SRB_FUNCTION_CLAIM_DEVICE:
 12.2274 -//    if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.2275 -//      KdPrint((__DRIVER_NAME "     SRB_FUNCTION_CLAIM_DEVICE\n"));
 12.2276 -    ObReferenceObject(WdfDeviceWdmGetDeviceObject(Device));
 12.2277 -    Srb->DataBuffer = WdfDeviceWdmGetDeviceObject(Device);
 12.2278 -//    Srb->DataBuffer = WdfDeviceWdmGetAttachedDevice(Device);
 12.2279 +    KdPrint((__DRIVER_NAME "     SRB_FUNCTION_CLAIM_DEVICE\n"));
 12.2280 +//    ObReferenceObject(WdfDeviceWdmGetDeviceObject(Device));
 12.2281 +//    Srb->DataBuffer = WdfDeviceWdmGetDeviceObject(Device);
 12.2282      Srb->SrbStatus = SRB_STATUS_SUCCESS;
 12.2283 -    status = STATUS_SUCCESS;
 12.2284 -    Irp->IoStatus.Status = status;
 12.2285 -    Irp->IoStatus.Information = 0;
 12.2286 -    IoCompleteRequest(Irp, IO_NO_INCREMENT);
 12.2287 +    ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.2288 +    ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.2289      break;
 12.2290    case SRB_FUNCTION_IO_CONTROL:
 12.2291 -//    if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.2292 -//      KdPrint((__DRIVER_NAME "     SRB_FUNCTION_IO_CONTROL\n"));
 12.2293 -    //status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
 12.2294 +    KdPrint((__DRIVER_NAME "     SRB_FUNCTION_IO_CONTROL\n"));
 12.2295      Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
 12.2296 -    status = STATUS_NOT_IMPLEMENTED;
 12.2297 -    //Irp->IoStatus.Status = status;
 12.2298 -    //Irp->IoStatus.Information = 0;
 12.2299 -    IoCompleteRequest(Irp, IO_NO_INCREMENT);
 12.2300 +    ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.2301 +    ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.2302      break;
 12.2303    case SRB_FUNCTION_FLUSH:
 12.2304 -//    if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.2305 -//      KdPrint((__DRIVER_NAME "     SRB_FUNCTION_FLUSH\n"));
 12.2306 -    Srb->SrbStatus = SRB_STATUS_SUCCESS;
 12.2307 -    status = STATUS_SUCCESS;
 12.2308 -    Irp->IoStatus.Status = status;
 12.2309 -    Irp->IoStatus.Information = 0;
 12.2310 -    IoCompleteRequest(Irp, IO_NO_INCREMENT);
 12.2311 +    KdPrint((__DRIVER_NAME "     SRB_FUNCTION_FLUSH\n"));
 12.2312 +    Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
 12.2313 +    ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.2314 +    ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.2315      break;
 12.2316    default:
 12.2317      KdPrint((__DRIVER_NAME "     Unhandled Srb->Function = %08X\n", Srb->Function));
 12.2318 -    status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
 12.2319 +    Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
 12.2320 +    ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
 12.2321 +    ScsiPortNotification(NextRequest, DeviceExtension, NULL);
 12.2322      break;
 12.2323    }
 12.2324  
 12.2325 -//  if (ChildDeviceData->DeviceType == XENVBD_DEVICETYPE_CDROM)
 12.2326 -//    KdPrint((__DRIVER_NAME " <-- WdmIrpPreprocessSCSI (AddedToList = %d, RemovedFromList = %d, AddedToRing = %d, AddedToRingAtLastNotify = %d, AddedToRingAtLastInterrupt = %d, RemovedFromRing = %d)\n", ChildDeviceData->IrpAddedToList, ChildDeviceData->IrpRemovedFromList, ChildDeviceData->IrpAddedToRing, ChildDeviceData->IrpAddedToRingAtLastNotify, ChildDeviceData->IrpAddedToRingAtLastInterrupt, ChildDeviceData->IrpCompleted));
 12.2327 -  //KdPrint((__DRIVER_NAME " <-- WdmIrpPreprocessSCSI\n"));
 12.2328 +//  KdPrint((__DRIVER_NAME " <-- HwScsiStartIo\n"));
 12.2329  
 12.2330 -  return status;
 12.2331 +  return TRUE;
 12.2332  }
 12.2333 +
 12.2334 +static BOOLEAN
 12.2335 +XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId)
 12.2336 +{
 12.2337 +  KdPrint((__DRIVER_NAME " --> HwScsiResetBus\n"));
 12.2338 +  KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
 12.2339 +
 12.2340 +  KdPrint((__DRIVER_NAME " <-- HwScsiResetBus\n"));
 12.2341 +
 12.2342 +  return TRUE;
 12.2343 +}
 12.2344 +
 12.2345 +
 12.2346 +static BOOLEAN
 12.2347 +XenVbd_HwScsiAdapterState(PVOID DeviceExtension, PVOID Context, BOOLEAN SaveState)
 12.2348 +{
 12.2349 +  KdPrint((__DRIVER_NAME " --> HwScsiAdapterState\n"));
 12.2350 +  KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
 12.2351 +
 12.2352 +  KdPrint((__DRIVER_NAME " <-- HwScsiAdapterState\n"));
 12.2353 +
 12.2354 +  return TRUE;
 12.2355 +}
 12.2356 +
 12.2357 +static SCSI_ADAPTER_CONTROL_STATUS
 12.2358 +XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters)
 12.2359 +{
 12.2360 +  SCSI_ADAPTER_CONTROL_STATUS Status = ScsiAdapterControlSuccess;
 12.2361 +  PSCSI_SUPPORTED_CONTROL_TYPE_LIST SupportedControlTypeList;
 12.2362 +
 12.2363 +  KdPrint((__DRIVER_NAME " --> HwScsiAdapterControl\n"));
 12.2364 +  KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
 12.2365 +
 12.2366 +  switch (ControlType)
 12.2367 +  {
 12.2368 +  case ScsiQuerySupportedControlTypes:
 12.2369 +    SupportedControlTypeList = (PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters;
 12.2370 +    KdPrint((__DRIVER_NAME "     ScsiQuerySupportedControlTypes (Max = %d)\n", SupportedControlTypeList->MaxControlType));
 12.2371 +    SupportedControlTypeList->SupportedTypeList[ScsiQuerySupportedControlTypes] = TRUE;
 12.2372 +    SupportedControlTypeList->SupportedTypeList[ScsiStopAdapter] = TRUE;
 12.2373 +    break;
 12.2374 +  case ScsiStopAdapter:
 12.2375 +    KdPrint((__DRIVER_NAME "     ScsiStopAdapter\n"));
 12.2376 +    break;
 12.2377 +  case ScsiRestartAdapter:
 12.2378 +    KdPrint((__DRIVER_NAME "     ScsiRestartAdapter\n"));
 12.2379 +    break;
 12.2380 +  case ScsiSetBootConfig:
 12.2381 +    KdPrint((__DRIVER_NAME "     ScsiSetBootConfig\n"));
 12.2382 +    break;
 12.2383 +  case ScsiSetRunningConfig:
 12.2384 +    KdPrint((__DRIVER_NAME "     ScsiSetRunningConfig\n"));
 12.2385 +    break;
 12.2386 +  default:
 12.2387 +    KdPrint((__DRIVER_NAME "     UNKNOWN\n"));
 12.2388 +    break;
 12.2389 +  }
 12.2390 +
 12.2391 +  KdPrint((__DRIVER_NAME " <-- HwScsiAdapterControl\n"));
 12.2392 +
 12.2393 +  return Status;
 12.2394 +}
    13.1 --- a/xenvbd/xenvbd.h	Fri Dec 14 13:30:56 2007 -0800
    13.2 +++ b/xenvbd/xenvbd.h	Mon Dec 17 14:19:39 2007 -0800
    13.3 @@ -1,11 +1,13 @@
    13.4 -#if !defined(_XENBUS_H_)
    13.5 -#define _XENBUS_H_
    13.6 +#if !defined(_XENVBD_H_)
    13.7 +#define _XENVBD_H_
    13.8  
    13.9 +#include <ntifs.h>
   13.10  #include <ntddk.h>
   13.11  #include <wdm.h>
   13.12  #include <wdf.h>
   13.13  #include <initguid.h>
   13.14  #include <ntdddisk.h>
   13.15 +#include <srb.h>
   13.16  
   13.17  #define NTSTRSAFE_LIB
   13.18  #include <ntstrsafe.h>
   13.19 @@ -16,100 +18,77 @@
   13.20  #include <event_channel.h>
   13.21  #include <hvm/params.h>
   13.22  #include <hvm/hvm_op.h>
   13.23 +#include <xen_public.h>
   13.24  #include <evtchn_public.h>
   13.25  #include <xenbus_public.h>
   13.26 +#include <gnttbl_public.h>
   13.27  #include <io/ring.h>
   13.28  #include <io/blkif.h>
   13.29  #define __DRIVER_NAME "XenVbd"
   13.30 -#define XENVBD_POOL_TAG (ULONG) 'XenP'
   13.31 +#define XENVBD_POOL_TAG (ULONG) 'XVBD'
   13.32  
   13.33  #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
   13.34  #define BLK_RING_SIZE __RING_SIZE((blkif_sring_t *)0, PAGE_SIZE)
   13.35  
   13.36  typedef struct {
   13.37    blkif_request_t req;
   13.38 -  //int Id;
   13.39 -  PIRP Irp;
   13.40 +  PSCSI_REQUEST_BLOCK Srb;
   13.41    PMDL Mdl;
   13.42    VOID *Buf;
   13.43 -  //int nr_segments;
   13.44 -  //unsigned long gref[BLKIF_MAX_SEGMENTS_PER_REQUEST];
   13.45  } blkif_shadow_t;
   13.46  
   13.47 -typedef struct {
   13.48 -  LIST_ENTRY Entry;
   13.49 -  PIRP Irp;
   13.50 -} XenVbd_ListEntry;
   13.51 +//#include "scsidata.h"
   13.52  
   13.53 -/*
   13.54 -typedef struct _XENVBD_QUEUE_DATA {
   13.55 -    XENVBD_CHILD_DEVICE_DATA DeviceData;
   13.56 -} XENVBD_QUEUE_DATA, *PXENVBD_QUEUE_DATA;
   13.57  
   13.58 -WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENVBD_QUEUE_DATA, GetQueueData)
   13.59 -*/
   13.60 -
   13.61 -//typedef unsigned long xenbus_transaction_t;
   13.62 -//typedef uint32_t XENSTORE_RING_IDX;
   13.63 -
   13.64 -extern XEN_IFACE_EVTCHN EvtChnInterface;
   13.65 -extern XEN_IFACE_XENBUS XenBusInterface;
   13.66 +#define SCSI_BUSES 4
   13.67 +#define SCSI_TARGETS_PER_BUS 16
   13.68  
   13.69  typedef enum {
   13.70    XENVBD_DEVICETYPE_UNKNOWN,
   13.71    XENVBD_DEVICETYPE_DISK,
   13.72 -  XENVBD_DEVICETYPE_CDROM
   13.73 +  XENVBD_DEVICETYPE_CDROM,
   13.74 +  XENVBD_DEVICETYPE_CONTROLLER // Not yet used
   13.75  } XENVBD_DEVICETYPE;
   13.76  
   13.77 -typedef struct _XENVBD_CHILD_DEVICE_DATA {
   13.78 -  LIST_ENTRY Entry;
   13.79 -  KSPIN_LOCK Lock;
   13.80 -  WDFQUEUE IoDefaultQueue;
   13.81 -  WDFDEVICE Device;
   13.82 -  char Path[128];
   13.83 -  char BackendPath[128];
   13.84 -  ULONG DeviceIndex;
   13.85 +struct
   13.86 +{
   13.87 +  int Present;
   13.88 +  BOOLEAN PendingInterrupt;
   13.89 +  PVOID DeviceData; // how can we create a forward definition for this???
   13.90    evtchn_port_t EventChannel;
   13.91    //blkif_sring_t *SharedRing;
   13.92 +  blkif_shadow_t *shadow;
   13.93 +  uint64_t shadow_free;
   13.94    ULONG RingBufPFN;
   13.95    int BackendState;
   13.96    int FrontendState;
   13.97 +  char Path[128];
   13.98 +  int DeviceIndex;
   13.99 +  char BackendPath[128];
  13.100    blkif_front_ring_t Ring;
  13.101 -  blkif_shadow_t *shadow;
  13.102 -  uint64_t shadow_free;
  13.103 -
  13.104 -  LIST_ENTRY IrpListHead;
  13.105 -  KSPIN_LOCK IrpListLock;
  13.106 -
  13.107 -  WDFDPC Dpc;
  13.108 -
  13.109 +  XENVBD_DEVICETYPE DeviceType;
  13.110 +  DISK_GEOMETRY Geometry;
  13.111    ULONG BytesPerSector;
  13.112 -  ULONGLONG TotalSectors;
  13.113 -  DISK_GEOMETRY Geometry;
  13.114 -  XENVBD_DEVICETYPE DeviceType;
  13.115 +  ULONGLONG TotalSectors; 
  13.116 +} typedef XENVBD_TARGET_DATA, *PXENVBD_TARGET_DATA;
  13.117  
  13.118 -  int IrpAddedToList;
  13.119 -  int IrpRemovedFromList;
  13.120 -  int IrpAddedToRing;
  13.121 -  int IrpAddedToRingAtLastNotify;
  13.122 -  int IrpAddedToRingAtLastInterrupt;
  13.123 -  int IrpAddedToRingAtLastDpc;
  13.124 -  int IrpRemovedFromRing;
  13.125 -  int IrpCompleted;
  13.126 +struct
  13.127 +{
  13.128 +  XENVBD_TARGET_DATA TargetData[SCSI_TARGETS_PER_BUS];
  13.129 +} typedef XENVBD_BUS_DATA, *PXENVBD_BUS_DATA;
  13.130  
  13.131 -  int FastPathUsed;
  13.132 -  int SlowPathUsed;
  13.133 -
  13.134 -} XENVBD_CHILD_DEVICE_DATA, *PXENVBD_CHILD_DEVICE_DATA;
  13.135 -
  13.136 -WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(PXENVBD_CHILD_DEVICE_DATA, GetChildDeviceData);
  13.137 +struct
  13.138 +{
  13.139 +  PXENPCI_XEN_DEVICE_DATA XenDeviceData;
  13.140 +  XENVBD_BUS_DATA BusData[SCSI_BUSES];
  13.141  
  13.142 -typedef struct _XENVBD_DEVICE_IDENTIFICATION_DESCRIPTION
  13.143 -{
  13.144 -  WDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER Header;
  13.145 -  PXENVBD_CHILD_DEVICE_DATA DeviceData;
  13.146 -  //ULONG DeviceIndex;
  13.147 -  //char Path[128];
  13.148 -} XENVBD_DEVICE_IDENTIFICATION_DESCRIPTION, *PXENVBD_DEVICE_IDENTIFICATION_DESCRIPTION;
  13.149 +  KSPIN_LOCK Lock;
  13.150 +
  13.151 +  int BusChangePending;
  13.152 +
  13.153 +  int EnumeratedDevices;
  13.154 +  KEVENT WaitDevicesEvent;
  13.155 +
  13.156 +} typedef XENVBD_DEVICE_DATA, *PXENVBD_DEVICE_DATA;
  13.157  
  13.158  #endif