win-pvdrivers

changeset 558:bbac251831a8

Implemented GetScatterGatherList
Implemented a way to limit the maximum size of the list
author James Harper <james.harper@bendigoit.com.au>
date Sun Apr 12 19:14:39 2009 +1000 (2009-04-12)
parents 9b044a670859
children b0b8be2d30c0
files xenpci/xenpci_pdo.c
line diff
     1.1 --- a/xenpci/xenpci_pdo.c	Sun Apr 12 19:13:23 2009 +1000
     1.2 +++ b/xenpci/xenpci_pdo.c	Sun Apr 12 19:14:39 2009 +1000
     1.3 @@ -33,6 +33,7 @@ typedef struct {
     1.4    PVOID aligned_buffer;
     1.5    PVOID unaligned_buffer;
     1.6    ULONG copy_length;
     1.7 +  BOOLEAN allocated_by_me;
     1.8  } sg_extra_t;
     1.9  
    1.10  typedef struct {
    1.11 @@ -325,16 +326,23 @@ XenPci_DOP_MapTransfer(
    1.12    
    1.13    if (xen_dma_adapter->dma_extension)
    1.14    {
    1.15 -    if (xen_dma_adapter->dma_extension->need_virtual_address(device_object->CurrentIrp))
    1.16 +    if (xen_dma_adapter->dma_extension->need_virtual_address && xen_dma_adapter->dma_extension->need_virtual_address(device_object->CurrentIrp))
    1.17      {
    1.18        map_register->map_type = MAP_TYPE_VIRTUAL;
    1.19      }
    1.20      else
    1.21      {
    1.22 -      ULONG alignment = xen_dma_adapter->dma_extension->get_alignment(device_object->CurrentIrp);
    1.23 -      if ((MmGetMdlByteOffset(mdl) & (alignment - 1)) || (MmGetMdlByteCount(mdl) & (alignment - 1)))
    1.24 +      if (xen_dma_adapter->dma_extension->get_alignment)
    1.25        {
    1.26 -        map_register->map_type = MAP_TYPE_REMAPPED;
    1.27 +        ULONG alignment = xen_dma_adapter->dma_extension->get_alignment(device_object->CurrentIrp);
    1.28 +        if ((MmGetMdlByteOffset(mdl) & (alignment - 1)) || (MmGetMdlByteCount(mdl) & (alignment - 1)))
    1.29 +        {
    1.30 +          map_register->map_type = MAP_TYPE_REMAPPED;
    1.31 +        }
    1.32 +        else
    1.33 +        {
    1.34 +          map_register->map_type = MAP_TYPE_MDL;
    1.35 +        }
    1.36        }
    1.37        else
    1.38        {
    1.39 @@ -424,35 +432,10 @@ XenPci_DOP_ReadDmaCounter(
    1.40    return 0;
    1.41  }
    1.42  
    1.43 -static NTSTATUS
    1.44 -XenPci_DOP_GetScatterGatherList(
    1.45 -  PDMA_ADAPTER DmaAdapter,
    1.46 -  PDEVICE_OBJECT DeviceObject,
    1.47 -  PMDL Mdl,
    1.48 -  PVOID CurrentVa,
    1.49 -  ULONG Length,
    1.50 -  PDRIVER_LIST_CONTROL ExecutionRoutine,
    1.51 -  PVOID Context,
    1.52 -  BOOLEAN WriteToDevice)
    1.53 -{
    1.54 -  UNREFERENCED_PARAMETER(DmaAdapter);
    1.55 -  UNREFERENCED_PARAMETER(DeviceObject);
    1.56 -  UNREFERENCED_PARAMETER(Mdl);
    1.57 -  UNREFERENCED_PARAMETER(CurrentVa);
    1.58 -  UNREFERENCED_PARAMETER(Length);
    1.59 -  UNREFERENCED_PARAMETER(ExecutionRoutine);
    1.60 -  UNREFERENCED_PARAMETER(Context);
    1.61 -  UNREFERENCED_PARAMETER(WriteToDevice);
    1.62 -
    1.63 -  FUNCTION_ENTER();
    1.64 -  FUNCTION_EXIT();
    1.65 -  return STATUS_UNSUCCESSFUL;
    1.66 -}
    1.67 -
    1.68  static VOID
    1.69  XenPci_DOP_PutScatterGatherList(
    1.70      IN PDMA_ADAPTER DmaAdapter,
    1.71 -    IN PSCATTER_GATHER_LIST ScatterGather,
    1.72 +    IN PSCATTER_GATHER_LIST sg_list,
    1.73      IN BOOLEAN WriteToDevice
    1.74      )
    1.75  {
    1.76 @@ -468,35 +451,37 @@ XenPci_DOP_PutScatterGatherList(
    1.77    xen_dma_adapter = (xen_dma_adapter_t *)DmaAdapter;
    1.78    xpdd = GetXpdd(xen_dma_adapter->xppdd->wdf_device_bus_fdo);
    1.79    
    1.80 -  sg_extra = (sg_extra_t *)((PUCHAR)ScatterGather + FIELD_OFFSET(SCATTER_GATHER_LIST, Elements) +
    1.81 -    (sizeof(SCATTER_GATHER_ELEMENT)) * ScatterGather->NumberOfElements);
    1.82 +  sg_extra = (sg_extra_t *)((PUCHAR)sg_list + FIELD_OFFSET(SCATTER_GATHER_LIST, Elements) +
    1.83 +    (sizeof(SCATTER_GATHER_ELEMENT)) * sg_list->NumberOfElements);
    1.84  
    1.85    switch (sg_extra->map_type)
    1.86    {
    1.87    case MAP_TYPE_REMAPPED:
    1.88 -    for (i = 0; i < ScatterGather->NumberOfElements; i++)
    1.89 +    for (i = 0; i < sg_list->NumberOfElements; i++)
    1.90      {
    1.91        grant_ref_t gref;
    1.92 -      gref = (grant_ref_t)(ScatterGather->Elements[i].Address.QuadPart >> PAGE_SHIFT);
    1.93 +      gref = (grant_ref_t)(sg_list->Elements[i].Address.QuadPart >> PAGE_SHIFT);
    1.94        GntTbl_EndAccess(xpdd, gref, FALSE);
    1.95 -      ScatterGather->Elements[i].Address.QuadPart = -1;
    1.96 +      sg_list->Elements[i].Address.QuadPart = -1;
    1.97      }
    1.98      if (!WriteToDevice)
    1.99        memcpy(sg_extra->unaligned_buffer, sg_extra->aligned_buffer, sg_extra->copy_length);
   1.100      ExFreePoolWithTag(sg_extra->aligned_buffer, XENPCI_POOL_TAG);
   1.101      break;
   1.102    case MAP_TYPE_MDL:
   1.103 -    for (i = 0; i < ScatterGather->NumberOfElements; i++)
   1.104 +    for (i = 0; i < sg_list->NumberOfElements; i++)
   1.105      {
   1.106        grant_ref_t gref;
   1.107 -      gref = (grant_ref_t)(ScatterGather->Elements[i].Address.QuadPart >> PAGE_SHIFT);
   1.108 +      gref = (grant_ref_t)(sg_list->Elements[i].Address.QuadPart >> PAGE_SHIFT);
   1.109        GntTbl_EndAccess(xpdd, gref, FALSE);
   1.110 -      ScatterGather->Elements[i].Address.QuadPart = -1;
   1.111 +      sg_list->Elements[i].Address.QuadPart = -1;
   1.112      }
   1.113      break;
   1.114    case MAP_TYPE_VIRTUAL:
   1.115      break;
   1.116    }
   1.117 +  if (sg_extra->allocated_by_me)
   1.118 +    ExFreePoolWithTag(sg_list, XENPCI_POOL_TAG);
   1.119    //FUNCTION_EXIT();
   1.120  }
   1.121  
   1.122 @@ -510,17 +495,18 @@ XenPci_DOP_CalculateScatterGatherList(
   1.123    PULONG NumberOfMapRegisters
   1.124    )
   1.125  {
   1.126 +  xen_dma_adapter_t *xen_dma_adapter;
   1.127    ULONG elements;
   1.128    PMDL curr_mdl;
   1.129 -  
   1.130 -  UNREFERENCED_PARAMETER(DmaAdapter);
   1.131 -  UNREFERENCED_PARAMETER(Mdl);
   1.132 -  
   1.133 +    
   1.134    FUNCTION_ENTER();
   1.135    
   1.136    KdPrint((__DRIVER_NAME "     Mdl = %p\n", Mdl));
   1.137    KdPrint((__DRIVER_NAME "     CurrentVa = %p\n", CurrentVa));
   1.138    KdPrint((__DRIVER_NAME "     Length = %d\n", Length));
   1.139 +
   1.140 +  xen_dma_adapter = (xen_dma_adapter_t *)DmaAdapter;
   1.141 +
   1.142    if (Mdl)
   1.143    {
   1.144      for (curr_mdl = Mdl, elements = 0; curr_mdl; curr_mdl = curr_mdl->Next)
   1.145 @@ -528,33 +514,42 @@ XenPci_DOP_CalculateScatterGatherList(
   1.146    }
   1.147    else
   1.148    {
   1.149 -    elements = ADDRESS_AND_SIZE_TO_SPAN_PAGES(0, Length) + 1;
   1.150 +    elements = ADDRESS_AND_SIZE_TO_SPAN_PAGES(0, Length); // + 1;
   1.151 +  }
   1.152 +
   1.153 +  if (elements > xen_dma_adapter->adapter_object.MapRegistersPerChannel)
   1.154 +  {
   1.155 +    KdPrint((__DRIVER_NAME "     elements = %d - too many\n", elements));
   1.156 +    if (NumberOfMapRegisters)
   1.157 +      *NumberOfMapRegisters = 0;
   1.158 +    *ScatterGatherListSize = 0;
   1.159 +    
   1.160 +    return STATUS_INSUFFICIENT_RESOURCES;
   1.161    }
   1.162    
   1.163    *ScatterGatherListSize = FIELD_OFFSET(SCATTER_GATHER_LIST, Elements)
   1.164      + sizeof(SCATTER_GATHER_ELEMENT) * elements
   1.165      + sizeof(sg_extra_t);
   1.166    if (NumberOfMapRegisters)
   1.167 -    *NumberOfMapRegisters = 1;
   1.168 +    *NumberOfMapRegisters = elements;
   1.169  
   1.170 -  KdPrint((__DRIVER_NAME "     ScatterGatherListSize = %d\n", *ScatterGatherListSize));
   1.171 +  KdPrint((__DRIVER_NAME "     ScatterGatherListSize = %d, NumberOfMapRegisters = %d\n", *ScatterGatherListSize, elements));
   1.172  
   1.173    FUNCTION_EXIT();
   1.174    return STATUS_SUCCESS;
   1.175  }
   1.176  
   1.177  static NTSTATUS
   1.178 -XenPci_DOP_BuildScatterGatherList(
   1.179 +XenPci_DOP_BuildScatterGatherListButDontExecute(
   1.180    IN PDMA_ADAPTER DmaAdapter,
   1.181    IN PDEVICE_OBJECT DeviceObject,
   1.182    IN PMDL Mdl,
   1.183    IN PVOID CurrentVa,
   1.184    IN ULONG Length,
   1.185 -  IN PDRIVER_LIST_CONTROL ExecutionRoutine,
   1.186 -  IN PVOID Context,
   1.187    IN BOOLEAN WriteToDevice,
   1.188    IN PVOID ScatterGatherBuffer,
   1.189 -  IN ULONG ScatterGatherBufferLength)
   1.190 +  IN ULONG ScatterGatherBufferLength,
   1.191 +  BOOLEAN allocated_by_me)
   1.192  {
   1.193    ULONG i;
   1.194    PSCATTER_GATHER_LIST sglist = ScatterGatherBuffer;
   1.195 @@ -588,7 +583,7 @@ XenPci_DOP_BuildScatterGatherList(
   1.196    
   1.197    if (xen_dma_adapter->dma_extension)
   1.198    {
   1.199 -    if (xen_dma_adapter->dma_extension->need_virtual_address(DeviceObject->CurrentIrp))
   1.200 +    if (xen_dma_adapter->dma_extension->need_virtual_address && xen_dma_adapter->dma_extension->need_virtual_address(DeviceObject->CurrentIrp))
   1.201      {
   1.202        ASSERT(!Mdl->Next); /* can only virtual a single buffer */
   1.203        map_type = MAP_TYPE_VIRTUAL;
   1.204 @@ -596,25 +591,32 @@ XenPci_DOP_BuildScatterGatherList(
   1.205      }
   1.206      else
   1.207      {
   1.208 -      ULONG alignment = xen_dma_adapter->dma_extension->get_alignment(DeviceObject->CurrentIrp);
   1.209 -      if ((MmGetMdlByteOffset(Mdl) & (alignment - 1)) || (MmGetMdlByteCount(Mdl) & (alignment - 1)))
   1.210 +      if (xen_dma_adapter->dma_extension->get_alignment)
   1.211        {
   1.212 -        ASSERT(!Mdl->Next); /* can only remap a single buffer for now - will need to check all Mdl's in the future */
   1.213 -        map_type = MAP_TYPE_REMAPPED;
   1.214 -        sglist->NumberOfElements = ADDRESS_AND_SIZE_TO_SPAN_PAGES(NULL, Length);
   1.215 +        ULONG alignment = xen_dma_adapter->dma_extension->get_alignment(DeviceObject->CurrentIrp);
   1.216 +        if ((MmGetMdlByteOffset(Mdl) & (alignment - 1)) || (MmGetMdlByteCount(Mdl) & (alignment - 1)))
   1.217 +        {
   1.218 +          ASSERT(!Mdl->Next); /* can only remap a single buffer for now - will need to check all Mdl's in the future */
   1.219 +          map_type = MAP_TYPE_REMAPPED;
   1.220 +          sglist->NumberOfElements = ADDRESS_AND_SIZE_TO_SPAN_PAGES(NULL, Length);
   1.221 +        }
   1.222 +        else
   1.223 +        {
   1.224 +          map_type = MAP_TYPE_MDL;
   1.225 +        }
   1.226        }
   1.227        else
   1.228        {
   1.229          map_type = MAP_TYPE_MDL;
   1.230 -        for (curr_mdl = Mdl, sglist->NumberOfElements = 0; curr_mdl; curr_mdl = curr_mdl->Next)
   1.231 -          sglist->NumberOfElements += ADDRESS_AND_SIZE_TO_SPAN_PAGES(
   1.232 -            MmGetMdlVirtualAddress(curr_mdl), MmGetMdlByteCount(curr_mdl));
   1.233        }
   1.234      }
   1.235    }
   1.236    else
   1.237    {
   1.238      map_type = MAP_TYPE_MDL;
   1.239 +  }
   1.240 +  if (map_type == MAP_TYPE_MDL)
   1.241 +  {
   1.242      for (curr_mdl = Mdl, sglist->NumberOfElements = 0; curr_mdl; curr_mdl = curr_mdl->Next)
   1.243        sglist->NumberOfElements += ADDRESS_AND_SIZE_TO_SPAN_PAGES(
   1.244          MmGetMdlVirtualAddress(curr_mdl), MmGetMdlByteCount(curr_mdl));
   1.245 @@ -629,6 +631,8 @@ XenPci_DOP_BuildScatterGatherList(
   1.246    
   1.247    sg_extra = (sg_extra_t *)((PUCHAR)sglist + FIELD_OFFSET(SCATTER_GATHER_LIST, Elements) +
   1.248      (sizeof(SCATTER_GATHER_ELEMENT)) * sglist->NumberOfElements);
   1.249 +
   1.250 +  sg_extra->allocated_by_me = allocated_by_me;
   1.251    
   1.252    sg_extra->map_type = map_type;
   1.253    switch (map_type)
   1.254 @@ -697,12 +701,75 @@ XenPci_DOP_BuildScatterGatherList(
   1.255      KdPrint((__DRIVER_NAME "     map_type = %d\n", map_type));
   1.256      break;
   1.257    }
   1.258 +  return STATUS_SUCCESS;
   1.259 +}
   1.260  
   1.261 -  ExecutionRoutine(DeviceObject, DeviceObject->CurrentIrp, ScatterGatherBuffer, Context);
   1.262 +static NTSTATUS
   1.263 +XenPci_DOP_BuildScatterGatherList(
   1.264 +  IN PDMA_ADAPTER DmaAdapter,
   1.265 +  IN PDEVICE_OBJECT DeviceObject,
   1.266 +  IN PMDL Mdl,
   1.267 +  IN PVOID CurrentVa,
   1.268 +  IN ULONG Length,
   1.269 +  IN PDRIVER_LIST_CONTROL ExecutionRoutine,
   1.270 +  IN PVOID Context,
   1.271 +  IN BOOLEAN WriteToDevice,
   1.272 +  IN PVOID ScatterGatherBuffer,
   1.273 +  IN ULONG ScatterGatherBufferLength)
   1.274 +{
   1.275 +  NTSTATUS status;
   1.276 +  
   1.277 +  status = XenPci_DOP_BuildScatterGatherListButDontExecute(DmaAdapter, DeviceObject, Mdl, CurrentVa, Length, WriteToDevice, ScatterGatherBuffer, ScatterGatherBufferLength, FALSE);
   1.278 +  
   1.279 +  if (NT_SUCCESS(status))
   1.280 +    ExecutionRoutine(DeviceObject, DeviceObject->CurrentIrp, ScatterGatherBuffer, Context);
   1.281  
   1.282    //FUNCTION_EXIT();
   1.283    
   1.284 -  return STATUS_SUCCESS;
   1.285 +  return status;
   1.286 +}
   1.287 +
   1.288 +static NTSTATUS
   1.289 +XenPci_DOP_GetScatterGatherList(
   1.290 +  PDMA_ADAPTER DmaAdapter,
   1.291 +  PDEVICE_OBJECT DeviceObject,
   1.292 +  PMDL Mdl,
   1.293 +  PVOID CurrentVa,
   1.294 +  ULONG Length,
   1.295 +  PDRIVER_LIST_CONTROL ExecutionRoutine,
   1.296 +  PVOID Context,
   1.297 +  BOOLEAN WriteToDevice)
   1.298 +{
   1.299 +  NTSTATUS status;
   1.300 +  ULONG list_size;
   1.301 +  ULONG map_registers;
   1.302 +  PSCATTER_GATHER_LIST sg_list;
   1.303 +  
   1.304 +  FUNCTION_ENTER();
   1.305 +
   1.306 +  status = XenPci_DOP_CalculateScatterGatherList(DmaAdapter, Mdl, CurrentVa, Length, &list_size, &map_registers);
   1.307 +  if (!NT_SUCCESS(status))
   1.308 +  {
   1.309 +    FUNCTION_EXIT();
   1.310 +    return status;
   1.311 +  }
   1.312 +
   1.313 +  sg_list = ExAllocatePoolWithTag(NonPagedPool, list_size, XENPCI_POOL_TAG);
   1.314 +  if (!sg_list)
   1.315 +  {
   1.316 +    KdPrint((__DRIVER_NAME "     Cannot allocate memory for sg_list\n"));
   1.317 +    FUNCTION_EXIT();
   1.318 +    return STATUS_INSUFFICIENT_RESOURCES;
   1.319 +  }
   1.320 +    
   1.321 +  status = XenPci_DOP_BuildScatterGatherListButDontExecute(DmaAdapter, DeviceObject, Mdl, CurrentVa, Length, WriteToDevice, sg_list, list_size, TRUE);
   1.322 +  
   1.323 +  if (NT_SUCCESS(status))
   1.324 +    ExecutionRoutine(DeviceObject, DeviceObject->CurrentIrp, sg_list, Context);
   1.325 +  
   1.326 +  FUNCTION_EXIT();
   1.327 +  
   1.328 +  return status;
   1.329  }
   1.330  
   1.331  static NTSTATUS
   1.332 @@ -778,10 +845,47 @@ Windows accessed beyond the end of the s
   1.333      return NULL;
   1.334    }
   1.335      
   1.336 +  xen_dma_adapter->xppdd = context;
   1.337 +  xen_dma_adapter->dma_extension = NULL;
   1.338 +
   1.339 +  KdPrint((__DRIVER_NAME "     About to call IoGetAttachedDeviceReference\n"));
   1.340 +  curr = IoGetAttachedDeviceReference(WdfDeviceWdmGetDeviceObject(xen_dma_adapter->xppdd->wdf_device));
   1.341 +  KdPrint((__DRIVER_NAME "     Before start of loop - curr = %p\n", curr));
   1.342 +  while (curr != NULL)
   1.343 +  {
   1.344 +    fdo_driver_object = curr->DriverObject;
   1.345 +    if (fdo_driver_object)
   1.346 +    {
   1.347 +      ObReferenceObject(fdo_driver_object);
   1.348 +      fdo_driver_extension = IoGetDriverObjectExtension(fdo_driver_object, UlongToPtr(XEN_DMA_DRIVER_EXTENSION_MAGIC));
   1.349 +      if (fdo_driver_extension)
   1.350 +      {
   1.351 +        xen_dma_adapter->dma_extension_driver = fdo_driver_object; /* so we can dereference it on putdmaadapter */
   1.352 +        xen_dma_adapter->dma_extension = (dma_driver_extension_t *)fdo_driver_extension;
   1.353 +        ObDereferenceObject(curr);
   1.354 +        break;
   1.355 +      }
   1.356 +      else
   1.357 +      {
   1.358 +        ObDereferenceObject(fdo_driver_object);
   1.359 +      }
   1.360 +    }
   1.361 +    prev = curr;
   1.362 +    curr = IoGetLowerDeviceObject(curr);
   1.363 +    ObDereferenceObject(prev);
   1.364 +  }
   1.365 +  KdPrint((__DRIVER_NAME "     End of loop\n"));
   1.366  
   1.367    xen_dma_adapter->adapter_object.DmaHeader.Size = sizeof(X_ADAPTER_OBJECT); //xen_dma_adapter_t);
   1.368    xen_dma_adapter->adapter_object.MasterAdapter = NULL;
   1.369 -  xen_dma_adapter->adapter_object.MapRegistersPerChannel = 1024;
   1.370 +  if (xen_dma_adapter->dma_extension && xen_dma_adapter->dma_extension->max_sg_elements)
   1.371 +  {
   1.372 +    xen_dma_adapter->adapter_object.MapRegistersPerChannel = xen_dma_adapter->dma_extension->max_sg_elements;
   1.373 +  }
   1.374 +  else
   1.375 +  {
   1.376 +    xen_dma_adapter->adapter_object.MapRegistersPerChannel = 256;
   1.377 +  }
   1.378    xen_dma_adapter->adapter_object.AdapterBaseVa = NULL;
   1.379    xen_dma_adapter->adapter_object.MapRegisterBase = NULL;
   1.380    xen_dma_adapter->adapter_object.NumberOfMapRegisters = 0;
   1.381 @@ -834,38 +938,8 @@ Windows accessed beyond the end of the s
   1.382      xen_dma_adapter->adapter_object.DmaHeader.DmaOperations->BuildScatterGatherList = XenPci_DOP_BuildScatterGatherList;
   1.383      xen_dma_adapter->adapter_object.DmaHeader.DmaOperations->BuildMdlFromScatterGatherList = XenPci_DOP_BuildMdlFromScatterGatherList;
   1.384    }
   1.385 -  xen_dma_adapter->xppdd = context;
   1.386 -  xen_dma_adapter->dma_extension = NULL;
   1.387  
   1.388 -  KdPrint((__DRIVER_NAME "     About to call IoGetAttachedDeviceReference\n"));
   1.389 -  curr = IoGetAttachedDeviceReference(WdfDeviceWdmGetDeviceObject(xen_dma_adapter->xppdd->wdf_device));
   1.390 -  KdPrint((__DRIVER_NAME "     Before start of loop - curr = %p\n", curr));
   1.391 -  while (curr != NULL)
   1.392 -  {
   1.393 -    fdo_driver_object = curr->DriverObject;
   1.394 -    if (fdo_driver_object)
   1.395 -    {
   1.396 -      ObReferenceObject(fdo_driver_object);
   1.397 -      fdo_driver_extension = IoGetDriverObjectExtension(fdo_driver_object, UlongToPtr(XEN_DMA_DRIVER_EXTENSION_MAGIC));
   1.398 -      if (fdo_driver_extension)
   1.399 -      {
   1.400 -        xen_dma_adapter->dma_extension_driver = fdo_driver_object; /* so we can dereference it on putdmaadapter */
   1.401 -        xen_dma_adapter->dma_extension = (dma_driver_extension_t *)fdo_driver_extension;
   1.402 -        ObDereferenceObject(curr);
   1.403 -        break;
   1.404 -      }
   1.405 -      else
   1.406 -      {
   1.407 -        ObDereferenceObject(fdo_driver_object);
   1.408 -      }
   1.409 -    }
   1.410 -    prev = curr;
   1.411 -    curr = IoGetLowerDeviceObject(curr);
   1.412 -    ObDereferenceObject(prev);
   1.413 -  }
   1.414 -  KdPrint((__DRIVER_NAME "     End of loop\n"));
   1.415 -
   1.416 -  *number_of_map_registers = 1024; /* why not... */
   1.417 +  *number_of_map_registers = xen_dma_adapter->adapter_object.MapRegistersPerChannel; //1024; /* why not... */
   1.418  
   1.419    FUNCTION_EXIT();
   1.420  
   1.421 @@ -2359,2349 +2433,3 @@ XenPci_Pdo_Resume(WDFDEVICE device)
   1.422  
   1.423    return STATUS_SUCCESS;
   1.424  } 
   1.425 -
   1.426 -#if 0
   1.427 -NTSTATUS
   1.428 -XenPci_Power_Pdo(PDEVICE_OBJECT device_object, PIRP irp)
   1.429 -{
   1.430 -  NTSTATUS status;
   1.431 -  PIO_STACK_LOCATION stack;
   1.432 -  POWER_STATE_TYPE power_type;
   1.433 -  POWER_STATE power_state;
   1.434 -  //PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
   1.435 -  //PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.436 -
   1.437 -  UNREFERENCED_PARAMETER(device_object);
   1.438 -  
   1.439 -  //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
   1.440 -
   1.441 -  stack = IoGetCurrentIrpStackLocation(irp);
   1.442 -  power_type = stack->Parameters.Power.Type;
   1.443 -  power_state = stack->Parameters.Power.State;
   1.444 -  
   1.445 -  switch (stack->MinorFunction)
   1.446 -  {
   1.447 -  case IRP_MN_POWER_SEQUENCE:
   1.448 -    //KdPrint((__DRIVER_NAME "     IRP_MN_POWER_SEQUENCE\n"));
   1.449 -    status = STATUS_NOT_SUPPORTED;
   1.450 -    break;
   1.451 -  case IRP_MN_QUERY_POWER:
   1.452 -    //KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_POWER\n"));
   1.453 -    status = STATUS_SUCCESS;
   1.454 -    break;
   1.455 -  case IRP_MN_SET_POWER:
   1.456 -    //KdPrint((__DRIVER_NAME "     IRP_MN_SET_POWER\n"));
   1.457 -    switch (power_type) {
   1.458 -    case DevicePowerState:
   1.459 -      PoSetPowerState(device_object, power_type, power_state);
   1.460 -      status = STATUS_SUCCESS;
   1.461 -      break;
   1.462 -    case SystemPowerState:
   1.463 -      status = STATUS_SUCCESS;
   1.464 -      break;
   1.465 -    default:
   1.466 -      status = STATUS_NOT_SUPPORTED;
   1.467 -      break;
   1.468 -    }    
   1.469 -    break;
   1.470 -  case IRP_MN_WAIT_WAKE:
   1.471 -    //KdPrint((__DRIVER_NAME "     IRP_MN_WAIT_WAKE\n"));
   1.472 -    status = STATUS_NOT_SUPPORTED;
   1.473 -    break;
   1.474 -  default:
   1.475 -    //KdPrint((__DRIVER_NAME "     Unknown IRP_MN_%d\n", stack->MinorFunction));
   1.476 -    status = STATUS_NOT_SUPPORTED;
   1.477 -    break;
   1.478 -  }
   1.479 -  if (status != STATUS_NOT_SUPPORTED) {
   1.480 -    irp->IoStatus.Status = status;
   1.481 -  }
   1.482 -
   1.483 -  PoStartNextPowerIrp(irp);
   1.484 -  status = irp->IoStatus.Status;
   1.485 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
   1.486 -  
   1.487 -  //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
   1.488 -
   1.489 -  return status;
   1.490 -}
   1.491 -
   1.492 -/*
   1.493 -Called at PASSIVE_LEVEL(?)
   1.494 -Called during restore
   1.495 -*/
   1.496 -
   1.497 -static ULONG
   1.498 -XenPci_ReadBackendState(PXENPCI_PDO_DEVICE_DATA xppdd)
   1.499 -{
   1.500 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.501 -  char path[128];
   1.502 -  char *value;
   1.503 -  char *err;
   1.504 -  ULONG backend_state;
   1.505 -  
   1.506 -  RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
   1.507 -  err = XenBus_Read(xpdd, XBT_NIL, path, &value);
   1.508 -  if (err)
   1.509 -  {
   1.510 -    XenPci_FreeMem(err);
   1.511 -    return XenbusStateUnknown;
   1.512 -  }
   1.513 -  else
   1.514 -  {
   1.515 -    backend_state = atoi(value);
   1.516 -    XenPci_FreeMem(value);
   1.517 -    return backend_state;
   1.518 -  }
   1.519 -}
   1.520 -
   1.521 -static VOID
   1.522 -XenPci_BackEndStateHandler(char *path, PVOID context)
   1.523 -{
   1.524 -  PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)context;
   1.525 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.526 -  ULONG new_backend_state;
   1.527 -
   1.528 -#if !DBG
   1.529 -  UNREFERENCED_PARAMETER(path);
   1.530 -#endif
   1.531 -  
   1.532 -  //  KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
   1.533 -
   1.534 -  /* check that path == device/id/state */
   1.535 -  //RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->path);
   1.536 -  new_backend_state = XenPci_ReadBackendState(xppdd);
   1.537 -  if (new_backend_state == XenbusStateUnknown)
   1.538 -  {
   1.539 -    if (xpdd->suspend_state != SUSPEND_STATE_NONE)
   1.540 -      return;
   1.541 -    KdPrint(("Failed to read %s, assuming closed\n", path));
   1.542 -    new_backend_state = XenbusStateClosed;
   1.543 -  }
   1.544 -
   1.545 -  if (xppdd->backend_state == new_backend_state)
   1.546 -  {
   1.547 -    KdPrint((__DRIVER_NAME "     state unchanged\n"));
   1.548 -    //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
   1.549 -    return;
   1.550 -  }
   1.551 -
   1.552 -  xppdd->backend_state = new_backend_state;
   1.553 -
   1.554 -  switch (xppdd->backend_state)
   1.555 -  {
   1.556 -  case XenbusStateUnknown:
   1.557 -    KdPrint((__DRIVER_NAME "     Backend State Changed to Unknown (%s)\n", path));  
   1.558 -    break;
   1.559 -
   1.560 -  case XenbusStateInitialising:
   1.561 -    KdPrint((__DRIVER_NAME "     Backend State Changed to Initialising (%s)\n", path));  
   1.562 -    break;
   1.563 -
   1.564 -  case XenbusStateInitWait:
   1.565 -    KdPrint((__DRIVER_NAME "     Backend State Changed to InitWait (%s)\n", path));  
   1.566 -    break;
   1.567 -
   1.568 -  case XenbusStateInitialised:
   1.569 -    KdPrint((__DRIVER_NAME "     Backend State Changed to Initialised (%s)\n", path));  
   1.570 -    break;
   1.571 -
   1.572 -  case XenbusStateConnected:
   1.573 -    KdPrint((__DRIVER_NAME "     Backend State Changed to Connected (%s)\n", path));    
   1.574 -    break;
   1.575 -
   1.576 -  case XenbusStateClosing:
   1.577 -    KdPrint((__DRIVER_NAME "     Backend State Changed to Closing (%s)\n", path));  
   1.578 -    if (xpdd->suspend_state == SUSPEND_STATE_NONE)
   1.579 -    {
   1.580 -      if (xppdd->common.device_usage_paging
   1.581 -        || xppdd->common.device_usage_dump
   1.582 -        || xppdd->common.device_usage_hibernation)
   1.583 -      {
   1.584 -        KdPrint((__DRIVER_NAME "     Not closing device because it is in use\n"));
   1.585 -        /* in use by page file, dump file, or hiber file - can't close */
   1.586 -        /* we should probably re-check if the device usage changes in the future */
   1.587 -      }
   1.588 -      else
   1.589 -      {
   1.590 -        if (xppdd->common.current_pnp_state == Started)
   1.591 -        {
   1.592 -          KdPrint((__DRIVER_NAME "     Sending RequestDeviceEject\n"));
   1.593 -          IoRequestDeviceEject(xppdd->common.pdo);
   1.594 -        }
   1.595 -        else
   1.596 -        {
   1.597 -          KdPrint((__DRIVER_NAME "     Not closing device because it is not started\n"));
   1.598 -        }
   1.599 -      }
   1.600 -    }
   1.601 -    break;
   1.602 -
   1.603 -  case XenbusStateClosed:
   1.604 -    KdPrint((__DRIVER_NAME "     Backend State Changed to Closed (%s)\n", path));  
   1.605 -    break;
   1.606 -
   1.607 -  default:
   1.608 -    KdPrint((__DRIVER_NAME "     Backend State Changed to Undefined = %d (%s)\n", xppdd->backend_state, path));
   1.609 -    break;
   1.610 -  }
   1.611 -
   1.612 -  KeSetEvent(&xppdd->backend_state_event, 1, FALSE);
   1.613 -
   1.614 -//  KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
   1.615 -
   1.616 -  return;
   1.617 -}
   1.618 -
   1.619 -struct dummy_sring {
   1.620 -    RING_IDX req_prod, req_event;
   1.621 -    RING_IDX rsp_prod, rsp_event;
   1.622 -    uint8_t  pad[48];
   1.623 -};
   1.624 -
   1.625 -/*
   1.626 -Called at PASSIVE_LEVEL
   1.627 -Called during restore
   1.628 -*/
   1.629 -
   1.630 -static NTSTATUS
   1.631 -XenPci_ChangeFrontendState(PXENPCI_PDO_DEVICE_DATA xppdd, ULONG frontend_state_set, ULONG backend_state_response, ULONG maximum_wait_ms)
   1.632 -{
   1.633 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.634 -  LARGE_INTEGER timeout;
   1.635 -  ULONG remaining;
   1.636 -  ULONG thiswait;
   1.637 -  char path[128];
   1.638 -  
   1.639 -  //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
   1.640 -
   1.641 -  RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->path);
   1.642 -  XenBus_Printf(xpdd, XBT_NIL, path, "%d", frontend_state_set);
   1.643 -
   1.644 -  remaining = maximum_wait_ms;
   1.645 -
   1.646 -  while (xppdd->backend_state != backend_state_response)
   1.647 -  {
   1.648 -    thiswait = min((LONG)remaining, 1000); // 1 second or remaining time, whichever is less
   1.649 -    timeout.QuadPart = (LONGLONG)-1 * thiswait * 1000 * 10;
   1.650 -    if (KeWaitForSingleObject(&xppdd->backend_state_event, Executive, KernelMode, FALSE, &timeout) == STATUS_TIMEOUT)
   1.651 -    {
   1.652 -      remaining -= thiswait;
   1.653 -      if (remaining == 0)
   1.654 -      {
   1.655 -        KdPrint((__DRIVER_NAME "     Timed out waiting for %d!\n", backend_state_response));
   1.656 -        return STATUS_UNSUCCESSFUL;
   1.657 -      }
   1.658 -      KdPrint((__DRIVER_NAME "     Still waiting for %d (currently %d)...\n", backend_state_response, xppdd->backend_state));
   1.659 -    }
   1.660 -  }
   1.661 -  //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
   1.662 -  return STATUS_SUCCESS;
   1.663 -}
   1.664 -
   1.665 -static VOID
   1.666 -DUMP_CURRENT_PNP_STATE(PXENPCI_PDO_DEVICE_DATA xppdd)
   1.667 -{
   1.668 -  switch (xppdd->common.current_pnp_state)
   1.669 -  {
   1.670 -  case Unknown:
   1.671 -    KdPrint((__DRIVER_NAME "     pnp_state = Unknown\n"));
   1.672 -    break;
   1.673 -  case NotStarted:
   1.674 -    KdPrint((__DRIVER_NAME "     pnp_state = NotStarted\n"));
   1.675 -    break;
   1.676 -  case Started:
   1.677 -    KdPrint((__DRIVER_NAME "     pnp_state = Started\n"));
   1.678 -    break;
   1.679 -  case StopPending:
   1.680 -    KdPrint((__DRIVER_NAME "     pnp_state = StopPending\n"));
   1.681 -    break;
   1.682 -  case Stopped:
   1.683 -    KdPrint((__DRIVER_NAME "     pnp_state = Stopped\n"));
   1.684 -    break;
   1.685 -  case RemovePending:
   1.686 -    KdPrint((__DRIVER_NAME "     pnp_state = RemovePending\n"));
   1.687 -    break;
   1.688 -  case SurpriseRemovePending:
   1.689 -    KdPrint((__DRIVER_NAME "     pnp_state = SurpriseRemovePending\n"));
   1.690 -    break;
   1.691 -  case Removed:
   1.692 -    KdPrint((__DRIVER_NAME "     pnp_state = Removed\n"));
   1.693 -    break;
   1.694 -  default:
   1.695 -    KdPrint((__DRIVER_NAME "     pnp_state = ???\n"));
   1.696 -    break;
   1.697 -  }
   1.698 -}
   1.699 -
   1.700 -static NTSTATUS
   1.701 -XenPci_EvtChn_Bind(PVOID Context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
   1.702 -{
   1.703 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.704 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.705 -  
   1.706 -  return EvtChn_Bind(xpdd, Port, ServiceRoutine, ServiceContext);
   1.707 -}
   1.708 -
   1.709 -static NTSTATUS
   1.710 -XenPci_EvtChn_BindDpc(PVOID Context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
   1.711 -{
   1.712 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.713 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.714 -  
   1.715 -  return EvtChn_BindDpc(xpdd, Port, ServiceRoutine, ServiceContext);
   1.716 -}
   1.717 -
   1.718 -static NTSTATUS
   1.719 -XenPci_EvtChn_Unbind(PVOID Context, evtchn_port_t Port)
   1.720 -{
   1.721 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.722 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.723 -  
   1.724 -  return EvtChn_Unbind(xpdd, Port);
   1.725 -}
   1.726 -
   1.727 -static NTSTATUS
   1.728 -XenPci_EvtChn_Mask(PVOID Context, evtchn_port_t Port)
   1.729 -{
   1.730 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.731 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.732 -  
   1.733 -  return EvtChn_Mask(xpdd, Port);
   1.734 -}
   1.735 -
   1.736 -static NTSTATUS
   1.737 -XenPci_EvtChn_Unmask(PVOID Context, evtchn_port_t Port)
   1.738 -{
   1.739 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.740 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.741 -  
   1.742 -  return EvtChn_Unmask(xpdd, Port);
   1.743 -}
   1.744 -
   1.745 -static NTSTATUS
   1.746 -XenPci_EvtChn_Notify(PVOID Context, evtchn_port_t Port)
   1.747 -{
   1.748 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.749 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.750 -  
   1.751 -  return EvtChn_Notify(xpdd, Port);
   1.752 -}
   1.753 -
   1.754 -static BOOLEAN
   1.755 -XenPci_EvtChn_AckEvent(PVOID context, evtchn_port_t port)
   1.756 -{
   1.757 -  PXENPCI_PDO_DEVICE_DATA xppdd = context;
   1.758 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.759 -  
   1.760 -  return EvtChn_AckEvent(xpdd, port);
   1.761 -}
   1.762 -
   1.763 -static BOOLEAN
   1.764 -XenPci_EvtChn_Sync(PVOID context, PKSYNCHRONIZE_ROUTINE sync_routine, PVOID sync_context)
   1.765 -{
   1.766 -  PXENPCI_PDO_DEVICE_DATA xppdd = context;
   1.767 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.768 -  
   1.769 -  return KeSynchronizeExecution(xpdd->interrupt, sync_routine, sync_context);
   1.770 -}
   1.771 -
   1.772 -static grant_ref_t
   1.773 -XenPci_GntTbl_GrantAccess(PVOID Context, domid_t domid, uint32_t frame, int readonly, grant_ref_t ref)
   1.774 -{
   1.775 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.776 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.777 -  
   1.778 -  return GntTbl_GrantAccess(xpdd, domid, frame, readonly, ref);
   1.779 -}
   1.780 -
   1.781 -static BOOLEAN
   1.782 -XenPci_GntTbl_EndAccess(PVOID Context, grant_ref_t ref, BOOLEAN keepref)
   1.783 -{
   1.784 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.785 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.786 -  
   1.787 -  return GntTbl_EndAccess(xpdd, ref, keepref);
   1.788 -}
   1.789 -
   1.790 -static VOID
   1.791 -XenPci_GntTbl_PutRef(PVOID Context, grant_ref_t ref)
   1.792 -{
   1.793 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.794 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.795 -  
   1.796 -  GntTbl_PutRef(xpdd, ref);
   1.797 -}
   1.798 -
   1.799 -static grant_ref_t
   1.800 -XenPci_GntTbl_GetRef(PVOID Context)
   1.801 -{
   1.802 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.803 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.804 -  
   1.805 -  return GntTbl_GetRef(xpdd);
   1.806 -}
   1.807 -
   1.808 -PCHAR
   1.809 -XenPci_XenBus_Read(PVOID Context, xenbus_transaction_t xbt, char *path, char **value)
   1.810 -{
   1.811 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.812 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.813 -  return XenBus_Read(xpdd, xbt, path, value);
   1.814 -}
   1.815 -
   1.816 -PCHAR
   1.817 -XenPci_XenBus_Write(PVOID Context, xenbus_transaction_t xbt, char *path, char *value)
   1.818 -{
   1.819 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.820 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.821 -  return XenBus_Write(xpdd, xbt, path, value);
   1.822 -}
   1.823 -
   1.824 -PCHAR
   1.825 -XenPci_XenBus_Printf(PVOID Context, xenbus_transaction_t xbt, char *path, char *fmt, ...)
   1.826 -{
   1.827 -  //PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.828 -  //PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.829 -  //return XenBus_Printf(xpdd, xbt, path, value);
   1.830 -  UNREFERENCED_PARAMETER(Context);
   1.831 -  UNREFERENCED_PARAMETER(xbt);
   1.832 -  UNREFERENCED_PARAMETER(path);
   1.833 -  UNREFERENCED_PARAMETER(fmt);
   1.834 -  return NULL;
   1.835 -}
   1.836 -
   1.837 -PCHAR
   1.838 -XenPci_XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
   1.839 -{
   1.840 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.841 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.842 -  return XenBus_StartTransaction(xpdd, xbt);
   1.843 -}
   1.844 -
   1.845 -PCHAR
   1.846 -XenPci_XenBus_EndTransaction(PVOID Context, xenbus_transaction_t xbt, int abort, int *retry)
   1.847 -{
   1.848 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.849 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.850 -  return XenBus_EndTransaction(xpdd, xbt, abort, retry);
   1.851 -}
   1.852 -
   1.853 -PCHAR
   1.854 -XenPci_XenBus_List(PVOID Context, xenbus_transaction_t xbt, char *prefix, char ***contents)
   1.855 -{
   1.856 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.857 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.858 -  return XenBus_List(xpdd, xbt, prefix, contents);
   1.859 -}
   1.860 -
   1.861 -PCHAR
   1.862 -XenPci_XenBus_AddWatch(PVOID Context, xenbus_transaction_t xbt, char *path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
   1.863 -{
   1.864 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.865 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.866 -  PCHAR retval;
   1.867 -  
   1.868 -  FUNCTION_ENTER();
   1.869 -  retval = XenBus_AddWatch(xpdd, xbt, path, ServiceRoutine, ServiceContext);
   1.870 -  if (retval == NULL)
   1.871 -  {
   1.872 -    KdPrint((__DRIVER_NAME "     XenPci_XenBus_AddWatch - %s = NULL\n", path));
   1.873 -  }
   1.874 -  else
   1.875 -  {
   1.876 -    KdPrint((__DRIVER_NAME "     XenPci_XenBus_AddWatch - %s = %s\n", path, retval));
   1.877 -  }
   1.878 -  FUNCTION_EXIT();
   1.879 -  return retval;
   1.880 -}
   1.881 -
   1.882 -PCHAR
   1.883 -XenPci_XenBus_RemWatch(PVOID Context, xenbus_transaction_t xbt, char *path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
   1.884 -{
   1.885 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.886 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.887 -  return XenBus_RemWatch(xpdd, xbt, path, ServiceRoutine, ServiceContext);
   1.888 -}
   1.889 -
   1.890 -static NTSTATUS
   1.891 -XenPci_XenShutdownDevice(PVOID Context)
   1.892 -{
   1.893 -  PXENPCI_PDO_DEVICE_DATA xppdd = Context;
   1.894 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.895 -  PUCHAR in_ptr;
   1.896 -  ULONG i;
   1.897 -  UCHAR type;
   1.898 -  PVOID setting;
   1.899 -  PVOID value;
   1.900 -
   1.901 -  FUNCTION_ENTER();
   1.902 -
   1.903 -  if (xppdd->backend_state == XenbusStateConnected)
   1.904 -  {
   1.905 -    XenPci_ChangeFrontendState(xppdd, XenbusStateClosing, XenbusStateClosing, 30000);
   1.906 -    if (xppdd->backend_state == XenbusStateClosing)
   1.907 -      XenPci_ChangeFrontendState(xppdd, XenbusStateClosed, XenbusStateClosed, 30000);
   1.908 -    if (xppdd->backend_state == XenbusStateClosed)
   1.909 -      XenPci_ChangeFrontendState(xppdd, XenbusStateInitialising, XenbusStateInitWait, 30000);
   1.910 -  }
   1.911 -  else
   1.912 -  {
   1.913 -    if (xppdd->backend_state == XenbusStateClosing)
   1.914 -      XenPci_ChangeFrontendState(xppdd, XenbusStateClosed, XenbusStateClosed, 30000);
   1.915 -  }
   1.916 -
   1.917 -  if (xppdd->assigned_resources_start != NULL)
   1.918 -  {
   1.919 -    ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
   1.920 -    in_ptr = xppdd->assigned_resources_start;
   1.921 -    while((type = GET_XEN_INIT_RSP(&in_ptr, &setting, &value, &value2)) != XEN_INIT_TYPE_END)
   1.922 -    {
   1.923 -      switch (type)
   1.924 -      {
   1.925 -      case XEN_INIT_TYPE_RING: /* frontend ring */
   1.926 -        FreePages(value);
   1.927 -        break;
   1.928 -      case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
   1.929 -      case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
   1.930 -        EvtChn_Unbind(xpdd, PtrToUlong(value));
   1.931 -        EvtChn_Close(xpdd, PtrToUlong(value));
   1.932 -        break;
   1.933 -      case XEN_INIT_TYPE_GRANT_ENTRIES:
   1.934 -        for (i = 0; i < PtrToUlong(setting); i++)
   1.935 -          GntTbl_EndAccess(xpdd, ((grant_ref_t *)value)[i], FALSE);
   1.936 -        break;
   1.937 -      }
   1.938 -    }
   1.939 -    ExFreePoolWithTag(xppdd->assigned_resources_start, XENPCI_POOL_TAG);
   1.940 -    xppdd->assigned_resources_start = NULL;
   1.941 -  }
   1.942 -
   1.943 -  FUNCTION_EXIT();
   1.944 -
   1.945 -  return STATUS_SUCCESS;
   1.946 -}
   1.947 -
   1.948 -static NTSTATUS
   1.949 -XenPci_XenConfigDevice(PVOID context);
   1.950 -
   1.951 -static NTSTATUS
   1.952 -XenPci_XenConfigDeviceSpecifyBuffers(PVOID context, PUCHAR src, PUCHAR dst)
   1.953 -{
   1.954 -  PXENPCI_PDO_DEVICE_DATA xppdd = context;
   1.955 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
   1.956 -  NTSTATUS status = STATUS_SUCCESS;
   1.957 -  ULONG i;
   1.958 -  char path[128];
   1.959 -  PCHAR setting, value, value2;
   1.960 -  PCHAR res;
   1.961 -  PVOID address;
   1.962 -  UCHAR type;
   1.963 -  PUCHAR in_ptr; //, in_start;
   1.964 -  PUCHAR out_ptr; //, out_start;
   1.965 -  XENPCI_VECTORS vectors;
   1.966 -  ULONG event_channel;
   1.967 -  ULONG run_type = 0;
   1.968 -  PMDL ring;
   1.969 -  grant_ref_t gref;
   1.970 -  BOOLEAN done_xenbus_init = FALSE;
   1.971 - 
   1.972 -  FUNCTION_ENTER();
   1.973 -
   1.974 -  in_ptr = src;
   1.975 -  out_ptr = dst;
   1.976 -  
   1.977 -  // always add vectors
   1.978 -  vectors.magic = XEN_DATA_MAGIC;
   1.979 -  vectors.length = sizeof(XENPCI_VECTORS);
   1.980 -  vectors.context = xppdd;
   1.981 -  vectors.EvtChn_Bind = XenPci_EvtChn_Bind;
   1.982 -  vectors.EvtChn_BindDpc = XenPci_EvtChn_BindDpc;
   1.983 -  vectors.EvtChn_Unbind = XenPci_EvtChn_Unbind;
   1.984 -  vectors.EvtChn_Mask = XenPci_EvtChn_Mask;
   1.985 -  vectors.EvtChn_Unmask = XenPci_EvtChn_Unmask;
   1.986 -  vectors.EvtChn_Notify = XenPci_EvtChn_Notify;
   1.987 -  vectors.EvtChn_AckEvent = XenPci_EvtChn_AckEvent;
   1.988 -  vectors.EvtChn_Sync = XenPci_EvtChn_Sync;
   1.989 -  vectors.GntTbl_GetRef = XenPci_GntTbl_GetRef;
   1.990 -  vectors.GntTbl_PutRef = XenPci_GntTbl_PutRef;
   1.991 -  vectors.GntTbl_GrantAccess = XenPci_GntTbl_GrantAccess;
   1.992 -  vectors.GntTbl_EndAccess = XenPci_GntTbl_EndAccess;
   1.993 -  vectors.XenPci_XenConfigDevice = XenPci_XenConfigDevice;
   1.994 -  vectors.XenPci_XenShutdownDevice = XenPci_XenShutdownDevice;
   1.995 -  strncpy(vectors.path, xppdd->path, 128);
   1.996 -  strncpy(vectors.backend_path, xppdd->backend_path, 128);
   1.997 -  vectors.pdo_event_channel = xpdd->pdo_event_channel;
   1.998 -  vectors.XenBus_Read = XenPci_XenBus_Read;
   1.999 -  vectors.XenBus_Write = XenPci_XenBus_Write;
  1.1000 -  vectors.XenBus_Printf = XenPci_XenBus_Printf;
  1.1001 -  vectors.XenBus_StartTransaction = XenPci_XenBus_StartTransaction;
  1.1002 -  vectors.XenBus_EndTransaction = XenPci_XenBus_EndTransaction;
  1.1003 -  vectors.XenBus_List = XenPci_XenBus_List;
  1.1004 -  vectors.XenBus_AddWatch = XenPci_XenBus_AddWatch;
  1.1005 -  vectors.XenBus_RemWatch = XenPci_XenBus_RemWatch;
  1.1006 -
  1.1007 -  if (qemu_filtered)
  1.1008 -    ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_ACTIVE, NULL, NULL, NULL);
  1.1009 -
  1.1010 -  ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_QEMU_PROTOCOL_VERSION, NULL, UlongToPtr(qemu_protocol_version), NULL);
  1.1011 -  
  1.1012 -  ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_VECTORS, NULL, &vectors, NULL);
  1.1013 -  ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_STATE_PTR, NULL, &xppdd->device_state, NULL);
  1.1014 -
  1.1015 -  // first pass, possibly before state == Connected
  1.1016 -  while((type = GET_XEN_INIT_REQ(&in_ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
  1.1017 -  {
  1.1018 -  
  1.1019 -    if (!done_xenbus_init)
  1.1020 -    {
  1.1021 -      if (XenPci_ChangeFrontendState(xppdd, XenbusStateInitialising, XenbusStateInitWait, 30000) != STATUS_SUCCESS)
  1.1022 -      {
  1.1023 -        status = STATUS_UNSUCCESSFUL;
  1.1024 -        goto error;
  1.1025 -      }
  1.1026 -      done_xenbus_init = TRUE;
  1.1027 -    }
  1.1028 -    
  1.1029 -    ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, type, setting, value, value2);
  1.1030 -
  1.1031 -    switch (type)
  1.1032 -    {
  1.1033 -    case XEN_INIT_TYPE_RUN:
  1.1034 -      run_type++;
  1.1035 -      break;
  1.1036 -    case XEN_INIT_TYPE_WRITE_STRING: /* frontend setting = value */
  1.1037 -      //KdPrint((__DRIVER_NAME "     XEN_INIT_TYPE_WRITE_STRING - %s = %s\n", setting, value));
  1.1038 -      RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
  1.1039 -      XenBus_Printf(xpdd, XBT_NIL, path, "%s", value);
  1.1040 -      break;
  1.1041 -    case XEN_INIT_TYPE_RING: /* frontend ring */
  1.1042 -      /* we only allocate and do the SHARED_RING_INIT here */
  1.1043 -      if ((ring = AllocatePage()) != 0)
  1.1044 -      {
  1.1045 -        address = MmGetMdlVirtualAddress(ring);
  1.1046 -        //KdPrint((__DRIVER_NAME "     XEN_INIT_TYPE_RING - %s = %p\n", setting, address));
  1.1047 -        SHARED_RING_INIT((struct dummy_sring *)address);
  1.1048 -        if ((gref = GntTbl_GrantAccess(
  1.1049 -          xpdd, 0, (ULONG)*MmGetMdlPfnArray(ring), FALSE, INVALID_GRANT_REF)) != INVALID_GRANT_REF)
  1.1050 -        {
  1.1051 -          RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
  1.1052 -          XenBus_Printf(xpdd, XBT_NIL, path, "%d", gref);
  1.1053 -          ADD_XEN_INIT_RSP(&out_ptr, type, setting, address, NULL);
  1.1054 -          ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, type, setting, ring, NULL);
  1.1055 -          // add the grant entry too so it gets freed automatically
  1.1056 -          __ADD_XEN_INIT_UCHAR(&xppdd->assigned_resources_ptr, XEN_INIT_TYPE_GRANT_ENTRIES);
  1.1057 -          __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, 1);
  1.1058 -          __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, gref);
  1.1059 -        }
  1.1060 -        else
  1.1061 -        {
  1.1062 -          FreePages(ring);
  1.1063 -          status = STATUS_UNSUCCESSFUL;
  1.1064 -          goto error;
  1.1065 -        }
  1.1066 -      }
  1.1067 -      else
  1.1068 -      {
  1.1069 -        status = STATUS_UNSUCCESSFUL;
  1.1070 -        goto error;
  1.1071 -      }
  1.1072 -      break;
  1.1073 -    case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
  1.1074 -    case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
  1.1075 -      if ((event_channel = EvtChn_AllocUnbound(xpdd, 0)) != 0)
  1.1076 -      {
  1.1077 -        //KdPrint((__DRIVER_NAME "     XEN_INIT_TYPE_EVENT_CHANNEL - %s = %d\n", setting, event_channel));
  1.1078 -        RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
  1.1079 -        XenBus_Printf(xpdd, XBT_NIL, path, "%d", event_channel);
  1.1080 -        ADD_XEN_INIT_RSP(&out_ptr, type, setting, UlongToPtr(event_channel), NULL);
  1.1081 -        ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, type, setting, UlongToPtr(event_channel), NULL);
  1.1082 -        if (type == XEN_INIT_TYPE_EVENT_CHANNEL_IRQ)
  1.1083 -          EvtChn_BindIrq(xpdd, event_channel, xppdd->irq_vector, path);
  1.1084 -      }
  1.1085 -      else
  1.1086 -      {
  1.1087 -        status = STATUS_UNSUCCESSFUL;
  1.1088 -        goto error;
  1.1089 -      }
  1.1090 -      break;
  1.1091 -    }
  1.1092 -  }
  1.1093 -  if (!NT_SUCCESS(status))
  1.1094 -  {
  1.1095 -    goto error;
  1.1096 -  }
  1.1097 -  // If XEN_INIT_TYPE_RUN was specified more than once then we skip XenbusStateInitialised here and go straight to XenbusStateConnected at the end
  1.1098 -  if (run_type == 1)
  1.1099 -  {
  1.1100 -    if (XenPci_ChangeFrontendState(xppdd, XenbusStateInitialised, XenbusStateConnected, 30000) != STATUS_SUCCESS)
  1.1101 -    {
  1.1102 -      status = STATUS_UNSUCCESSFUL;
  1.1103 -      goto error;
  1.1104 -    }
  1.1105 -  }
  1.1106 -
  1.1107 -  // second pass, possibly after state == Connected
  1.1108 -  in_ptr = src;
  1.1109 -  while((type = GET_XEN_INIT_REQ(&in_ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
  1.1110 -  {
  1.1111 -    switch(type)
  1.1112 -    {
  1.1113 -    case XEN_INIT_TYPE_READ_STRING_BACK:
  1.1114 -    case XEN_INIT_TYPE_READ_STRING_FRONT:
  1.1115 -      if (type == XEN_INIT_TYPE_READ_STRING_FRONT)
  1.1116 -        RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
  1.1117 -      else
  1.1118 -        RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->backend_path, setting);
  1.1119 -      res = XenBus_Read(xpdd, XBT_NIL, path, &value);
  1.1120 -      if (res)
  1.1121 -      {
  1.1122 -        //KdPrint((__DRIVER_NAME "     XEN_INIT_TYPE_READ_STRING - %s = <failed>\n", setting));
  1.1123 -        XenPci_FreeMem(res);
  1.1124 -        ADD_XEN_INIT_RSP(&out_ptr, type, setting, NULL, NULL);
  1.1125 -      }
  1.1126 -      else
  1.1127 -      {
  1.1128 -        //KdPrint((__DRIVER_NAME "     XEN_INIT_TYPE_READ_STRING - %s = %s\n", setting, value));
  1.1129 -        ADD_XEN_INIT_RSP(&out_ptr, type, setting, value, NULL);
  1.1130 -        XenPci_FreeMem(value);
  1.1131 -      }
  1.1132 -      break;
  1.1133 -    case XEN_INIT_TYPE_VECTORS:
  1.1134 -      // this is always done so ignore the request
  1.1135 -      break;
  1.1136 -    case XEN_INIT_TYPE_GRANT_ENTRIES:
  1.1137 -      //KdPrint((__DRIVER_NAME "     XEN_INIT_TYPE_GRANT_ENTRIES - %d\n", PtrToUlong(value)));
  1.1138 -      __ADD_XEN_INIT_UCHAR(&out_ptr, type);
  1.1139 -      __ADD_XEN_INIT_UCHAR(&xppdd->assigned_resources_ptr, type);
  1.1140 -      __ADD_XEN_INIT_ULONG(&out_ptr, PtrToUlong(value));
  1.1141 -      __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, PtrToUlong(value));
  1.1142 -      for (i = 0; i < PtrToUlong(value); i++)
  1.1143 -      {
  1.1144 -        gref = GntTbl_GetRef(xpdd);
  1.1145 -        __ADD_XEN_INIT_ULONG(&out_ptr, gref);
  1.1146 -        __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, gref);
  1.1147 -      }
  1.1148 -      break;
  1.1149 -    }
  1.1150 -  }
  1.1151 -  ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_END, NULL, NULL);
  1.1152 -
  1.1153 -  if (run_type)
  1.1154 -  {
  1.1155 -    if (XenPci_ChangeFrontendState(xppdd, XenbusStateConnected, XenbusStateConnected, 30000) != STATUS_SUCCESS)
  1.1156 -    {
  1.1157 -      status = STATUS_UNSUCCESSFUL;
  1.1158 -      goto error;
  1.1159 -    }
  1.1160 -  }
  1.1161 -  FUNCTION_EXIT();
  1.1162 -  return status;
  1.1163 -  
  1.1164 -error:
  1.1165 -  XenPci_ChangeFrontendState(xppdd, XenbusStateInitialising, XenbusStateInitWait, 30000);
  1.1166 -  FUNCTION_EXIT_STATUS(status);
  1.1167 -
  1.1168 -  return status;
  1.1169 -}
  1.1170 -
  1.1171 -static NTSTATUS
  1.1172 -XenPci_XenConfigDevice(PVOID context)
  1.1173 -{
  1.1174 -  NTSTATUS status;
  1.1175 -  PUCHAR src, dst;
  1.1176 -  PXENPCI_PDO_DEVICE_DATA xppdd = context;  
  1.1177 -
  1.1178 -  src = ExAllocatePoolWithTag(NonPagedPool, xppdd->config_page_length, XENPCI_POOL_TAG);
  1.1179 -  dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
  1.1180 -  memcpy(src, dst, xppdd->config_page_length);
  1.1181 -  
  1.1182 -  status = XenPci_XenConfigDeviceSpecifyBuffers(xppdd, src, dst);
  1.1183 -
  1.1184 -  MmUnmapIoSpace(dst, xppdd->config_page_length);
  1.1185 -  ExFreePoolWithTag(src, XENPCI_POOL_TAG);
  1.1186 -  
  1.1187 -  return status;
  1.1188 -}
  1.1189 -
  1.1190 -static NTSTATUS
  1.1191 -XenPci_GetBackendAndAddWatch(PDEVICE_OBJECT device_object)
  1.1192 -{
  1.1193 -  PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
  1.1194 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
  1.1195 -  char path[128];
  1.1196 -  PCHAR res;
  1.1197 -  PCHAR value;
  1.1198 -
  1.1199 -  /* Get backend path */
  1.1200 -  RtlStringCbPrintfA(path, ARRAY_SIZE(path),
  1.1201 -    "%s/backend", xppdd->path);
  1.1202 -  res = XenBus_Read(xpdd, XBT_NIL, path, &value);
  1.1203 -  if (res)
  1.1204 -  {
  1.1205 -    KdPrint((__DRIVER_NAME "    Failed to read backend path\n"));
  1.1206 -    XenPci_FreeMem(res);
  1.1207 -    return STATUS_UNSUCCESSFUL;
  1.1208 -  }
  1.1209 -  RtlStringCbCopyA(xppdd->backend_path, ARRAY_SIZE(xppdd->backend_path), value);
  1.1210 -  XenPci_FreeMem(value);
  1.1211 -
  1.1212 -  /* Add watch on backend state */
  1.1213 -  RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
  1.1214 -  XenBus_AddWatch(xpdd, XBT_NIL, path, XenPci_BackEndStateHandler, xppdd);
  1.1215 -  
  1.1216 -  return STATUS_SUCCESS;
  1.1217 -}
  1.1218 -
  1.1219 -NTSTATUS
  1.1220 -XenPci_Pdo_Resume(PDEVICE_OBJECT device_object)
  1.1221 -{
  1.1222 -  NTSTATUS status;
  1.1223 -  PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
  1.1224 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
  1.1225 -  ULONG old_backend_state;
  1.1226 -  PUCHAR src, dst;
  1.1227 -
  1.1228 -  FUNCTION_ENTER();
  1.1229 -
  1.1230 -  old_backend_state = xppdd->backend_state;
  1.1231 -
  1.1232 -  if (xppdd->restart_on_resume)
  1.1233 -  {  
  1.1234 -    status = XenPci_GetBackendAndAddWatch(device_object);
  1.1235 -  
  1.1236 -    if (XenPci_ChangeFrontendState(xppdd, XenbusStateInitialising, XenbusStateInitWait, 30000) != STATUS_SUCCESS)
  1.1237 -    {
  1.1238 -      KdPrint((__DRIVER_NAME "     Failed to change frontend state to Initialising\n"));
  1.1239 -      // this is probably an unrecoverable situation...
  1.1240 -      FUNCTION_ERROR_EXIT();
  1.1241 -      return STATUS_UNSUCCESSFUL;
  1.1242 -    }
  1.1243 -    if (xppdd->assigned_resources_ptr)
  1.1244 -    {
  1.1245 -      // reset things - feed the 'requested resources' back in
  1.1246 -      ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL);
  1.1247 -      src = xppdd->requested_resources_start;
  1.1248 -      xppdd->requested_resources_ptr = xppdd->requested_resources_start = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);;
  1.1249 -      xppdd->assigned_resources_ptr = xppdd->assigned_resources_start;
  1.1250 -      
  1.1251 -      dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
  1.1252 -      
  1.1253 -      status = XenPci_XenConfigDeviceSpecifyBuffers(xppdd, src, dst);
  1.1254 -
  1.1255 -      MmUnmapIoSpace(dst, xppdd->config_page_length);
  1.1256 -      ExFreePoolWithTag(src, XENPCI_POOL_TAG);
  1.1257 -    }
  1.1258 -    if (XenPci_ChangeFrontendState(xppdd, XenbusStateConnected, XenbusStateConnected, 30000) != STATUS_SUCCESS)
  1.1259 -    {
  1.1260 -      // this is definitely an unrecoverable situation...
  1.1261 -      KdPrint((__DRIVER_NAME "     Failed to change frontend state to connected\n"));
  1.1262 -      FUNCTION_ERROR_EXIT();
  1.1263 -      return STATUS_UNSUCCESSFUL;
  1.1264 -    }
  1.1265 -  }
  1.1266 -  else
  1.1267 -  {
  1.1268 -    KdPrint((__DRIVER_NAME "     Not resuming - current_pnp_state = %d, old_backend_state = %d\n", xppdd->common.current_pnp_state, old_backend_state));
  1.1269 -  }
  1.1270 -  KeMemoryBarrier();
  1.1271 -  xppdd->device_state.resume_state = RESUME_STATE_FRONTEND_RESUME;
  1.1272 -  KeMemoryBarrier();
  1.1273 -  EvtChn_Notify(xpdd, xpdd->pdo_event_channel);  
  1.1274 -
  1.1275 -  FUNCTION_EXIT();
  1.1276 -
  1.1277 -  return STATUS_SUCCESS;
  1.1278 -} 
  1.1279 -
  1.1280 -/* called at PASSIVE_LEVEL */
  1.1281 -NTSTATUS
  1.1282 -XenPci_Pdo_Suspend(PDEVICE_OBJECT device_object)
  1.1283 -{
  1.1284 -  NTSTATUS status = STATUS_SUCCESS;
  1.1285 -  PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
  1.1286 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
  1.1287 -  LARGE_INTEGER wait_time;
  1.1288 -  char path[128];
  1.1289 -  PUCHAR in_ptr;
  1.1290 -  UCHAR type;
  1.1291 -  PVOID setting;
  1.1292 -  PVOID value;
  1.1293 -
  1.1294 -  KdPrint((__DRIVER_NAME " --> " __FUNCTION__ " (%s)\n", xppdd->path));
  1.1295 -
  1.1296 -  if (xppdd->backend_state == XenbusStateConnected)
  1.1297 -  {
  1.1298 -    xppdd->restart_on_resume = TRUE;
  1.1299 -    xppdd->device_state.resume_state_ack = RESUME_STATE_RUNNING;
  1.1300 -    KeMemoryBarrier();
  1.1301 -    xppdd->device_state.resume_state = RESUME_STATE_SUSPENDING;
  1.1302 -    KeMemoryBarrier();
  1.1303 -    EvtChn_Notify(xpdd, xpdd->pdo_event_channel);    
  1.1304 -    while(xppdd->device_state.resume_state_ack != RESUME_STATE_SUSPENDING)
  1.1305 -    {
  1.1306 -      KdPrint((__DRIVER_NAME "     Starting delay - resume_state = %d, resume_state_ack = %d\n", xppdd->device_state.resume_state, xppdd->device_state.resume_state_ack));
  1.1307 -      wait_time.QuadPart = 100 * (-1 * 10 * 1000);
  1.1308 -      KeDelayExecutionThread(KernelMode, FALSE, &wait_time);
  1.1309 -      KdPrint((__DRIVER_NAME "     Done with delay\n"));
  1.1310 -    }
  1.1311 -    KdPrint((__DRIVER_NAME "     resume_state acknowledged\n"));
  1.1312 -
  1.1313 -    XenPci_ChangeFrontendState(xppdd, XenbusStateClosing, XenbusStateClosing, 30000);
  1.1314 -    XenPci_ChangeFrontendState(xppdd, XenbusStateClosed, XenbusStateClosed, 30000);
  1.1315 -    XenPci_ChangeFrontendState(xppdd, XenbusStateInitialising, XenbusStateInitWait, 30000);
  1.1316 -
  1.1317 -    if (xppdd->assigned_resources_start != NULL)
  1.1318 -    {
  1.1319 -      in_ptr = xppdd->assigned_resources_ptr;
  1.1320 -      ADD_XEN_INIT_RSP(&in_ptr, XEN_INIT_TYPE_END, NULL, NULL);
  1.1321 -      in_ptr = xppdd->assigned_resources_start;
  1.1322 -      while((type = GET_XEN_INIT_RSP(&in_ptr, &setting, &value)) != XEN_INIT_TYPE_END)
  1.1323 -      {
  1.1324 -        switch (type)
  1.1325 -        {
  1.1326 -        case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
  1.1327 -        case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel bound to irq */
  1.1328 -          EvtChn_Close(xpdd, PtrToUlong(value));
  1.1329 -          break;
  1.1330 -        }
  1.1331 -      }
  1.1332 -    }
  1.1333 -
  1.1334 -    RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
  1.1335 -    XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackEndStateHandler, xppdd);  
  1.1336 -  }
  1.1337 -  else
  1.1338 -  {
  1.1339 -    xppdd->restart_on_resume = FALSE;
  1.1340 -  }
  1.1341 -
  1.1342 -  KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
  1.1343 -  
  1.1344 -  return status;
  1.1345 -}
  1.1346 -
  1.1347 -VOID
  1.1348 -XenPci_DumpPdoConfig(PDEVICE_OBJECT device_object)
  1.1349 -{
  1.1350 -  PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
  1.1351 -
  1.1352 -#if !DBG
  1.1353 -  UNREFERENCED_PARAMETER(xppdd);
  1.1354 -#endif
  1.1355 -
  1.1356 -  KdPrint((__DRIVER_NAME "     path = %s\n", xppdd->path));
  1.1357 -  KdPrint((__DRIVER_NAME "     backend_path = %s\n", xppdd->backend_path));
  1.1358 -  KdPrint((__DRIVER_NAME "     irq_number = %d\n", xppdd->irq_number));
  1.1359 -  KdPrint((__DRIVER_NAME "     irq_level = %d\n", xppdd->irq_level));
  1.1360 -  KdPrint((__DRIVER_NAME "     irq_vector = %x\n", xppdd->irq_vector));
  1.1361 -}
  1.1362 -
  1.1363 -static PMDL
  1.1364 -XenConfig_MakeConfigPage(PDEVICE_OBJECT device_object, PMDL mdl)
  1.1365 -{
  1.1366 -  //PXENCONFIG_DEVICE_DATA xcdd = (PXENCONFIG_DEVICE_DATA)device_object->DeviceExtension;
  1.1367 -  //PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
  1.1368 -  //PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
  1.1369 -  PMDL mdl;
  1.1370 -  PUCHAR ptr;
  1.1371 -  PDEVICE_OBJECT curr, prev;
  1.1372 -  PDRIVER_OBJECT fdo_driver_object;
  1.1373 -  PUCHAR fdo_driver_extension;
  1.1374 -  
  1.1375 -  ptr = MmGetMdlVirtualAddress(mdl);
  1.1376 -  curr = IoGetAttachedDeviceReference(device_object);
  1.1377 -  while (curr != NULL)
  1.1378 -  {
  1.1379 -    fdo_driver_object = curr->DriverObject;
  1.1380 -    KdPrint((__DRIVER_NAME "     fdo_driver_object = %p\n", fdo_driver_object));
  1.1381 -    if (fdo_driver_object)
  1.1382 -    {
  1.1383 -      fdo_driver_extension = IoGetDriverObjectExtension(fdo_driver_object, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC));
  1.1384 -      KdPrint((__DRIVER_NAME "     fdo_driver_extension = %p\n", fdo_driver_extension));
  1.1385 -      if (fdo_driver_extension)
  1.1386 -      {
  1.1387 -        memcpy(ptr, fdo_driver_extension, PAGE_SIZE);
  1.1388 -        ObDereferenceObject(curr);
  1.1389 -        break;
  1.1390 -      }
  1.1391 -    }
  1.1392 -    prev = curr;
  1.1393 -    curr = IoGetLowerDeviceObject(curr);
  1.1394 -    ObDereferenceObject(prev);
  1.1395 -  }
  1.1396 -  return mdl;
  1.1397 -}
  1.1398 -
  1.1399 -static NTSTATUS
  1.1400 -XenPci_Pnp_StartDevice(PDEVICE_OBJECT device_object, PIRP irp)
  1.1401 -{
  1.1402 -  NTSTATUS status = STATUS_SUCCESS;
  1.1403 -  PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
  1.1404 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
  1.1405 -  PIO_STACK_LOCATION stack;
  1.1406 -  PCM_PARTIAL_RESOURCE_LIST prl;
  1.1407 -  PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
  1.1408 -  ULONG i;
  1.1409 -  char path[128];
  1.1410 -  PMDL mdl;
  1.1411 - 
  1.1412 -  FUNCTION_ENTER();
  1.1413 -  KdPrint((__DRIVER_NAME "     %s\n", xppdd->path));
  1.1414 -
  1.1415 -  DUMP_CURRENT_PNP_STATE(xppdd);
  1.1416 -  
  1.1417 -  stack = IoGetCurrentIrpStackLocation(irp);
  1.1418 -
  1.1419 -  status = XenPci_GetBackendAndAddWatch(device_object);
  1.1420 -  if (!NT_SUCCESS(status)) {
  1.1421 -    FUNCTION_ERROR_EXIT();
  1.1422 -    return status;
  1.1423 -  }
  1.1424 -
  1.1425 -  mdl = XenConfig_MakeConfigPage(device_object);
  1.1426 -  
  1.1427 -  prl = &stack->Parameters.StartDevice.AllocatedResources->List[0].PartialResourceList;
  1.1428 -  for (i = 0; i < prl->Count; i++)
  1.1429 -  {
  1.1430 -    prd = & prl->PartialDescriptors[i];
  1.1431 -    switch (prd->Type)
  1.1432 -    {
  1.1433 -#if 0    
  1.1434 -    case CmResourceTypeInterrupt:
  1.1435 -      KdPrint((__DRIVER_NAME "     CmResourceTypeInterrupt\n"));
  1.1436 -      KdPrint((__DRIVER_NAME "     irq_number = %02x\n", prd->u.Interrupt.Vector));
  1.1437 -      xppdd->irq_number = prd->u.Interrupt.Vector;
  1.1438 -      break;
  1.1439 -#endif
  1.1440 -    case CmResourceTypeMemory:
  1.1441 -      if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart && prd->u.Memory.Length == 0)
  1.1442 -      {
  1.1443 -        prd->u.Memory.Start.QuadPart = MmGetMdlPfnArray(mdl)[0] << PAGE_SHIFT;
  1.1444 -        prd->u.Memory.Length = MmGetMdlByteCount(mdl);
  1.1445 -      }
  1.1446 -      else if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart + 1 && prd->u.Memory.Length == 0)
  1.1447 -      {
  1.1448 -        RtlZeroMemory(prd, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
  1.1449 -        prd->Type = CmResourceTypeInterrupt;
  1.1450 -        prd->ShareDisposition = CmResourceShareShared;
  1.1451 -        prd->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
  1.1452 -        prd->u.Interrupt.Level = xpdd->irq_number;
  1.1453 -        prd->u.Interrupt.Vector = xpdd->irq_number;
  1.1454 -        prd->u.Interrupt.Affinity = (KAFFINITY)-1;
  1.1455 -        xppdd->irq_number = xpdd->irq_number;
  1.1456 -      }
  1.1457 -      break;
  1.1458 -    }
  1.1459 -  }
  1.1460 -
  1.1461 -  prl = &stack->Parameters.StartDevice.AllocatedResourcesTranslated->List[0].PartialResourceList;
  1.1462 -  for (i = 0; i < prl->Count; i++)
  1.1463 -  {
  1.1464 -    prd = & prl->PartialDescriptors[i];
  1.1465 -    switch (prd->Type)
  1.1466 -    {
  1.1467 -#if 0
  1.1468 -    case CmResourceTypeInterrupt:
  1.1469 -      KdPrint((__DRIVER_NAME "     CmResourceTypeInterrupt (%d)\n", i));
  1.1470 -      KdPrint((__DRIVER_NAME "     irq_vector = %02x\n", prd->u.Interrupt.Vector));
  1.1471 -      KdPrint((__DRIVER_NAME "     irq_level = %d\n", prd->u.Interrupt.Level));
  1.1472 -      xppdd->irq_vector = prd->u.Interrupt.Vector;
  1.1473 -      xppdd->irq_level = (KIRQL)prd->u.Interrupt.Level;
  1.1474 -      break;
  1.1475 -#endif
  1.1476 -    case CmResourceTypeMemory:
  1.1477 -      KdPrint((__DRIVER_NAME "     CmResourceTypeMemory (%d)\n", i));
  1.1478 -      KdPrint((__DRIVER_NAME "     Start = %08x, Length = %d\n", prd->u.Memory.Start.LowPart, prd->u.Memory.Length));
  1.1479 -      if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart)
  1.1480 -      {
  1.1481 -        if (prd->u.Memory.Length == 0)
  1.1482 -        {
  1.1483 -          KdPrint((__DRIVER_NAME "     pfn[0] = %08x\n", (ULONG)MmGetMdlPfnArray(mdl)[0]));
  1.1484 -          prd->u.Memory.Start.QuadPart = (ULONGLONG)MmGetMdlPfnArray(mdl)[0] << PAGE_SHIFT;
  1.1485 -          prd->u.Memory.Length = MmGetMdlByteCount(mdl);
  1.1486 -          KdPrint((__DRIVER_NAME "     New Start = %08x%08x, Length = %d\n", prd->u.Memory.Start.HighPart, prd->u.Memory.Start.LowPart, prd->u.Memory.Length));
  1.1487 -        }
  1.1488 -        xppdd->config_page_phys = prd->u.Memory.Start;
  1.1489 -        xppdd->config_page_length = prd->u.Memory.Length;
  1.1490 -        xppdd->requested_resources_start = xppdd->requested_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
  1.1491 -        xppdd->assigned_resources_start = xppdd->assigned_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
  1.1492 -        
  1.1493 -        status = XenPci_XenConfigDevice(xppdd);
  1.1494 -        if (!NT_SUCCESS(status))
  1.1495 -        {
  1.1496 -          RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
  1.1497 -          XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackEndStateHandler, xppdd);
  1.1498 -          FUNCTION_ERROR_EXIT();
  1.1499 -          return status;
  1.1500 -        }
  1.1501 -      }
  1.1502 -      else if (prd->u.Memory.Start.QuadPart == xpdd->platform_mmio_addr.QuadPart + 1 && prd->u.Memory.Length == 0)
  1.1503 -      {
  1.1504 -        RtlZeroMemory(prd, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
  1.1505 -        prd->Type = CmResourceTypeInterrupt;
  1.1506 -        prd->ShareDisposition = CmResourceShareShared;
  1.1507 -        prd->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
  1.1508 -        prd->u.Interrupt.Level = xpdd->irq_level;
  1.1509 -        prd->u.Interrupt.Vector = xpdd->irq_vector;
  1.1510 -        prd->u.Interrupt.Affinity = (KAFFINITY)-1;
  1.1511 -        xppdd->irq_vector = xpdd->irq_vector;
  1.1512 -        xppdd->irq_level = xpdd->irq_level;
  1.1513 -      }
  1.1514 -      break;
  1.1515 -    }
  1.1516 -  }
  1.1517 -
  1.1518 -  SET_PNP_STATE(&xppdd->common, Started);
  1.1519 -  
  1.1520 -  FUNCTION_EXIT();
  1.1521 -
  1.1522 -  return STATUS_SUCCESS;
  1.1523 -}
  1.1524 -
  1.1525 -static NTSTATUS
  1.1526 -XenPci_Pnp_RemoveDevice(PDEVICE_OBJECT device_object, PIRP irp)
  1.1527 -{
  1.1528 -  NTSTATUS status = STATUS_SUCCESS;
  1.1529 -  PXENPCI_PDO_DEVICE_DATA xppdd = device_object->DeviceExtension;
  1.1530 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
  1.1531 -  char path[128];
  1.1532 -
  1.1533 -  UNREFERENCED_PARAMETER(irp);
  1.1534 -
  1.1535 -  FUNCTION_ENTER();
  1.1536 -
  1.1537 -  DUMP_CURRENT_PNP_STATE(xppdd);
  1.1538 -
  1.1539 -  if (xppdd->common.current_pnp_state != Removed)
  1.1540 -  {
  1.1541 -    status = XenPci_XenShutdownDevice(xppdd);
  1.1542 -    /* Remove watch on backend state */
  1.1543 -    RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
  1.1544 -    XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackEndStateHandler, xppdd);
  1.1545 -    SET_PNP_STATE(&xppdd->common, Removed);
  1.1546 -    IoInvalidateDeviceRelations(xppdd->bus_pdo, BusRelations);
  1.1547 -  }
  1.1548 -  if (xppdd->reported_missing)
  1.1549 -  {
  1.1550 -    IoDeleteDevice(xppdd->common.pdo);
  1.1551 -  }
  1.1552 -  
  1.1553 -  FUNCTION_EXIT_STATUS(status);
  1.1554 -
  1.1555 -  return status;
  1.1556 -}
  1.1557 -
  1.1558 -static NTSTATUS
  1.1559 -XenPci_QueryResourceRequirements(PDEVICE_OBJECT device_object, PIRP irp)
  1.1560 -{
  1.1561 -  PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
  1.1562 -  PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
  1.1563 -  PIO_RESOURCE_REQUIREMENTS_LIST irrl;
  1.1564 -  PIO_RESOURCE_DESCRIPTOR ird;
  1.1565 -  ULONG length;
  1.1566 -
  1.1567 -  UNREFERENCED_PARAMETER(device_object);
  1.1568 -
  1.1569 -  FUNCTION_ENTER();
  1.1570 -  
  1.1571 -  length = FIELD_OFFSET(IO_RESOURCE_REQUIREMENTS_LIST, List) +
  1.1572 -    FIELD_OFFSET(IO_RESOURCE_LIST, Descriptors) +
  1.1573 -    sizeof(IO_RESOURCE_DESCRIPTOR) * 2;
  1.1574 -  irrl = ExAllocatePoolWithTag(NonPagedPool,
  1.1575 -    length,
  1.1576 -    XENPCI_POOL_TAG);
  1.1577 -  
  1.1578 -  irrl->ListSize = length;
  1.1579 -  irrl->InterfaceType = PNPBus; //Internal;
  1.1580 -  irrl->BusNumber = 0;
  1.1581 -  irrl->SlotNumber = 0;
  1.1582 -  irrl->AlternativeLists = 1;
  1.1583 -  irrl->List[0].Version = 1;
  1.1584 -  irrl->List[0].Revision = 1;
  1.1585 -  irrl->List[0].Count = 0;
  1.1586 -
  1.1587 -  #if 0
  1.1588 -  ird = &irrl->List[0].Descriptors[irrl->List[0].Count++];
  1.1589 -  ird->Option = 0;
  1.1590 -  ird->Type = CmResourceTypeInterrupt;
  1.1591 -  ird->ShareDisposition = CmResourceShareShared;
  1.1592 -  ird->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
  1.1593 -  KdPrint((__DRIVER_NAME "      irq type = %s\n", (xpdd->irq_mode == Latched)?"Latched":"Level"));
  1.1594 -  ird->u.Interrupt.MinimumVector = xpdd->irq_number;
  1.1595 -  ird->u.Interrupt.MaximumVector = xpdd->irq_number;
  1.1596 -  #endif
  1.1597 -  
  1.1598 -  ird = &irrl->List[0].Descriptors[irrl->List[0].Count++];
  1.1599 -  ird->Option = 0;
  1.1600 -  ird->Type = CmResourceTypeMemory;
  1.1601 -  ird->ShareDisposition = CmResourceShareShared;
  1.1602 -  ird->Flags = CM_RESOURCE_MEMORY_READ_WRITE | CM_RESOURCE_MEMORY_CACHEABLE;
  1.1603 -  ird->u.Memory.MinimumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart;
  1.1604 -  ird->u.Memory.MaximumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart;
  1.1605 -  ird->u.Memory.Length = 0;
  1.1606 -  ird->u.Memory.Alignment = PAGE_SIZE;
  1.1607 -
  1.1608 -  ird = &irrl->List[0].Descriptors[irrl->List[0].Count++];
  1.1609 -  ird->Option = 0;
  1.1610 -  ird->Type = CmResourceTypeMemory;
  1.1611 -  ird->ShareDisposition = CmResourceShareShared;
  1.1612 -  ird->Flags = CM_RESOURCE_MEMORY_READ_WRITE | CM_RESOURCE_MEMORY_CACHEABLE;
  1.1613 -  ird->u.Memory.MinimumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart + 1;
  1.1614 -  ird->u.Memory.MaximumAddress.QuadPart = xpdd->platform_mmio_addr.QuadPart + 1;
  1.1615 -  ird->u.Memory.Length = 0;
  1.1616 -  ird->u.Memory.Alignment = PAGE_SIZE;
  1.1617 -
  1.1618 -  irp->IoStatus.Information = (ULONG_PTR)irrl;
  1.1619 -
  1.1620 -  FUNCTION_EXIT();
  1.1621 -  
  1.1622 -  return STATUS_SUCCESS;
  1.1623 -}
  1.1624 -
  1.1625 -static NTSTATUS
  1.1626 -XenPci_Pnp_QueryTargetRelations(PDEVICE_OBJECT device_object, PIRP irp)
  1.1627 -{
  1.1628 -  PDEVICE_RELATIONS dr;
  1.1629 -  PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
  1.1630 -  
  1.1631 -  dr = (PDEVICE_RELATIONS)ExAllocatePoolWithTag (PagedPool, sizeof(DEVICE_RELATIONS), XENPCI_POOL_TAG);
  1.1632 -  dr->Count = 1;
  1.1633 -  dr->Objects[0] = xppdd->common.pdo;
  1.1634 -  ObReferenceObject(xppdd->common.pdo);
  1.1635 -  irp->IoStatus.Information = (ULONG_PTR)dr;
  1.1636 -  
  1.1637 -  return STATUS_SUCCESS;
  1.1638 -}
  1.1639 -
  1.1640 -static NTSTATUS
  1.1641 -XenPci_Pnp_QueryCapabilities(PDEVICE_OBJECT device_object, PIRP irp)
  1.1642 -{
  1.1643 -  PIO_STACK_LOCATION stack;
  1.1644 -  PDEVICE_CAPABILITIES dc;
  1.1645 -
  1.1646 -  UNREFERENCED_PARAMETER(device_object);
  1.1647 -  
  1.1648 -  stack = IoGetCurrentIrpStackLocation(irp);
  1.1649 -  dc = stack->Parameters.DeviceCapabilities.Capabilities;
  1.1650 -  dc->LockSupported = FALSE;
  1.1651 -  dc->EjectSupported = TRUE;
  1.1652 -  dc->Removable = TRUE;
  1.1653 -  dc->DockDevice = FALSE;
  1.1654 -  dc->UniqueID = FALSE;
  1.1655 -  dc->SilentInstall = TRUE; //FALSE;
  1.1656 -  dc->RawDeviceOK = FALSE;
  1.1657 -  dc->SurpriseRemovalOK = TRUE;
  1.1658 -  dc->HardwareDisabled = FALSE;
  1.1659 -  dc->NoDisplayInUI = FALSE;
  1.1660 -  dc->DeviceWake = PowerDeviceUnspecified;
  1.1661 -  dc->D1Latency = 0;
  1.1662 -  dc->D2Latency = 0;
  1.1663 -  dc->D3Latency = 0;
  1.1664 -  /* we are really supposed to get the DeviceState entries from the parent... */
  1.1665 -  dc->DeviceState[PowerSystemWorking] = PowerDeviceD0;
  1.1666 -  dc->DeviceState[PowerSystemSleeping1] = PowerDeviceUnspecified;
  1.1667 -  dc->DeviceState[PowerSystemSleeping2] = PowerDeviceUnspecified;
  1.1668 -  dc->DeviceState[PowerSystemSleeping3] = PowerDeviceUnspecified;
  1.1669 -  dc->DeviceState[PowerSystemHibernate] = PowerDeviceD3;
  1.1670 -  dc->DeviceState[PowerSystemShutdown] = PowerDeviceD3;
  1.1671 -  return STATUS_SUCCESS;
  1.1672 -}
  1.1673 -
  1.1674 -static VOID
  1.1675 -XenPci_IS_InterfaceReference(PVOID context)
  1.1676 -{
  1.1677 -  UNREFERENCED_PARAMETER(context);
  1.1678 -  FUNCTION_ENTER();
  1.1679 -  FUNCTION_EXIT();
  1.1680 -}
  1.1681 -
  1.1682 -static VOID
  1.1683 -XenPci_IS_InterfaceDereference(PVOID context)
  1.1684 -{
  1.1685 -  UNREFERENCED_PARAMETER(context);
  1.1686 -  FUNCTION_ENTER();
  1.1687 -  FUNCTION_EXIT();
  1.1688 -}
  1.1689 -
  1.1690 -static BOOLEAN
  1.1691 -XenPci_BIS_TranslateBusAddress(PVOID context, PHYSICAL_ADDRESS bus_address, ULONG length, PULONG address_space, PPHYSICAL_ADDRESS translated_address)
  1.1692 -{
  1.1693 -  UNREFERENCED_PARAMETER(context);
  1.1694 -  UNREFERENCED_PARAMETER(length);
  1.1695 -  /* actually this isn't right - should look up the gref for the physical address and work backwards from that */
  1.1696 -  FUNCTION_ENTER();
  1.1697 -  if (*address_space != 0)
  1.1698 -  {
  1.1699 -    KdPrint((__DRIVER_NAME "      Cannot map I/O space\n"));
  1.1700 -    FUNCTION_EXIT();
  1.1701 -    return FALSE;
  1.1702 -  }
  1.1703 -  *translated_address = bus_address;
  1.1704 -  FUNCTION_EXIT();
  1.1705 -  return TRUE;
  1.1706 -}
  1.1707 -
  1.1708 -static VOID
  1.1709 -XenPci_DOP_PutDmaAdapter(PDMA_ADAPTER dma_adapter)
  1.1710 -{
  1.1711 -  UNREFERENCED_PARAMETER(dma_adapter);
  1.1712 -  
  1.1713 -  FUNCTION_ENTER();
  1.1714 -  // decrement ref count
  1.1715 -  FUNCTION_EXIT();
  1.1716 -
  1.1717 -  return;
  1.1718 -}
  1.1719 -
  1.1720 -static PVOID
  1.1721 -XenPci_DOP_AllocateCommonBuffer(
  1.1722 -  PDMA_ADAPTER DmaAdapter,
  1.1723 -  ULONG Length,
  1.1724 -  PPHYSICAL_ADDRESS LogicalAddress,
  1.1725 -  BOOLEAN CacheEnabled
  1.1726 -)
  1.1727 -{
  1.1728 -  xen_dma_adapter_t *xen_dma_adapter;
  1.1729 -  PXENPCI_DEVICE_DATA xpdd;
  1.1730 -  PVOID buffer;
  1.1731 -  PFN_NUMBER pfn;
  1.1732 -  grant_ref_t gref;
  1.1733 -  
  1.1734 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.1735 -  UNREFERENCED_PARAMETER(CacheEnabled);
  1.1736 -  
  1.1737 -  //FUNCTION_ENTER();
  1.1738 -
  1.1739 -  xen_dma_adapter = (xen_dma_adapter_t *)DmaAdapter;
  1.1740 -  xpdd = xen_dma_adapter->xppdd->bus_fdo->DeviceExtension;
  1.1741 -
  1.1742 -  //KdPrint((__DRIVER_NAME "     Length = %d\n", Length));
  1.1743 -  
  1.1744 -  buffer = ExAllocatePoolWithTag(NonPagedPool, Length, XENPCI_POOL_TAG);
  1.1745 -
  1.1746 -  pfn = (PFN_NUMBER)(MmGetPhysicalAddress(buffer).QuadPart >> PAGE_SHIFT);
  1.1747 -  ASSERT(pfn);
  1.1748 -  gref = (grant_ref_t)GntTbl_GrantAccess(xpdd, 0, pfn, FALSE, INVALID_GRANT_REF);
  1.1749 -  ASSERT(gref);
  1.1750 -  LogicalAddress->QuadPart = (gref << PAGE_SHIFT) | (PtrToUlong(buffer) & (PAGE_SIZE - 1));
  1.1751 -  
  1.1752 -  //FUNCTION_EXIT();
  1.1753 -  return buffer;
  1.1754 -}
  1.1755 -
  1.1756 -static VOID
  1.1757 -XenPci_DOP_FreeCommonBuffer(
  1.1758 -  PDMA_ADAPTER DmaAdapter,
  1.1759 -  ULONG Length,
  1.1760 -  PHYSICAL_ADDRESS LogicalAddress,
  1.1761 -  PVOID VirtualAddress,
  1.1762 -  BOOLEAN CacheEnabled
  1.1763 -)
  1.1764 -{
  1.1765 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.1766 -  UNREFERENCED_PARAMETER(Length);
  1.1767 -  UNREFERENCED_PARAMETER(LogicalAddress);
  1.1768 -  UNREFERENCED_PARAMETER(CacheEnabled);
  1.1769 -
  1.1770 -  FUNCTION_ENTER();
  1.1771 -  ExFreePoolWithTag(VirtualAddress, XENPCI_POOL_TAG);
  1.1772 -  // TODO: free the grant ref here
  1.1773 -  FUNCTION_EXIT();
  1.1774 -}
  1.1775 -
  1.1776 -static NTSTATUS
  1.1777 -XenPci_DOP_AllocateAdapterChannel(
  1.1778 -    IN PDMA_ADAPTER  DmaAdapter,
  1.1779 -    IN PDEVICE_OBJECT  DeviceObject,
  1.1780 -    IN ULONG  NumberOfMapRegisters,
  1.1781 -    IN PDRIVER_CONTROL  ExecutionRoutine,
  1.1782 -    IN PVOID  Context
  1.1783 -    )
  1.1784 -{
  1.1785 -  IO_ALLOCATION_ACTION action;
  1.1786 -  
  1.1787 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.1788 -  UNREFERENCED_PARAMETER(NumberOfMapRegisters);
  1.1789 -  
  1.1790 -  FUNCTION_ENTER();
  1.1791 -  action = ExecutionRoutine(DeviceObject, DeviceObject->CurrentIrp, UlongToPtr(64), Context);
  1.1792 -  
  1.1793 -  switch (action)
  1.1794 -  {
  1.1795 -  case KeepObject:
  1.1796 -    KdPrint((__DRIVER_NAME "     KeepObject\n"));
  1.1797 -    break;
  1.1798 -  case DeallocateObject:
  1.1799 -    KdPrint((__DRIVER_NAME "     DeallocateObject\n"));
  1.1800 -    break;
  1.1801 -  case DeallocateObjectKeepRegisters:
  1.1802 -    KdPrint((__DRIVER_NAME "     DeallocateObjectKeepRegisters\n"));
  1.1803 -    break;
  1.1804 -  default:
  1.1805 -    KdPrint((__DRIVER_NAME "     Unknown action %d\n", action));
  1.1806 -    break;
  1.1807 -  }
  1.1808 -  FUNCTION_EXIT();
  1.1809 -  return STATUS_SUCCESS;
  1.1810 -}
  1.1811 -
  1.1812 -static BOOLEAN
  1.1813 -XenPci_DOP_FlushAdapterBuffers(
  1.1814 -  PDMA_ADAPTER DmaAdapter,
  1.1815 -  PMDL Mdl,
  1.1816 -  PVOID MapRegisterBase,
  1.1817 -  PVOID CurrentVa,
  1.1818 -  ULONG Length,
  1.1819 -  BOOLEAN WriteToDevice)
  1.1820 -{
  1.1821 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.1822 -  UNREFERENCED_PARAMETER( Mdl);
  1.1823 -  UNREFERENCED_PARAMETER(MapRegisterBase);
  1.1824 -  UNREFERENCED_PARAMETER(CurrentVa);
  1.1825 -  UNREFERENCED_PARAMETER(Length);
  1.1826 -  UNREFERENCED_PARAMETER(WriteToDevice);
  1.1827 -
  1.1828 -  FUNCTION_ENTER();
  1.1829 -  FUNCTION_EXIT();
  1.1830 -  return TRUE;
  1.1831 -}
  1.1832 -
  1.1833 -static VOID
  1.1834 -XenPci_DOP_FreeAdapterChannel(
  1.1835 -    IN PDMA_ADAPTER  DmaAdapter
  1.1836 -    )
  1.1837 -{
  1.1838 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.1839 -
  1.1840 -  FUNCTION_ENTER();
  1.1841 -  FUNCTION_EXIT();
  1.1842 -}
  1.1843 -
  1.1844 -static VOID
  1.1845 -XenPci_DOP_FreeMapRegisters(
  1.1846 -  PDMA_ADAPTER DmaAdapter,
  1.1847 -  PVOID MapRegisterBase,
  1.1848 -  ULONG NumberOfMapRegisters)
  1.1849 -{
  1.1850 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.1851 -  UNREFERENCED_PARAMETER(MapRegisterBase);
  1.1852 -  UNREFERENCED_PARAMETER(NumberOfMapRegisters);
  1.1853 -
  1.1854 -  FUNCTION_ENTER();
  1.1855 -  FUNCTION_EXIT();
  1.1856 -}
  1.1857 -
  1.1858 -static PHYSICAL_ADDRESS
  1.1859 -XenPci_DOP_MapTransfer(
  1.1860 -    PDMA_ADAPTER DmaAdapter,
  1.1861 -    PMDL Mdl,
  1.1862 -    PVOID MapRegisterBase,
  1.1863 -    PVOID CurrentVa,
  1.1864 -    PULONG Length,
  1.1865 -    BOOLEAN WriteToDevice)
  1.1866 -{
  1.1867 -  PHYSICAL_ADDRESS physical;
  1.1868 -  
  1.1869 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.1870 -  UNREFERENCED_PARAMETER(Mdl);
  1.1871 -  UNREFERENCED_PARAMETER(MapRegisterBase);
  1.1872 -  UNREFERENCED_PARAMETER(CurrentVa);
  1.1873 -  UNREFERENCED_PARAMETER(Length);
  1.1874 -  UNREFERENCED_PARAMETER(WriteToDevice);
  1.1875 -
  1.1876 -  FUNCTION_ENTER();
  1.1877 -  
  1.1878 -  physical.QuadPart = 0;
  1.1879 -  
  1.1880 -  FUNCTION_EXIT();
  1.1881 -  return physical;
  1.1882 -}
  1.1883 -
  1.1884 -static ULONG
  1.1885 -XenPci_DOP_GetDmaAlignment(
  1.1886 -  PDMA_ADAPTER DmaAdapter)
  1.1887 -{
  1.1888 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.1889 -
  1.1890 -  FUNCTION_ENTER();
  1.1891 -  FUNCTION_EXIT();
  1.1892 -  return 0;
  1.1893 -}
  1.1894 -
  1.1895 -static ULONG
  1.1896 -XenPci_DOP_ReadDmaCounter(
  1.1897 -  PDMA_ADAPTER DmaAdapter)
  1.1898 -{
  1.1899 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.1900 -
  1.1901 -  FUNCTION_ENTER();
  1.1902 -  FUNCTION_EXIT();
  1.1903 -  return 0;
  1.1904 -}
  1.1905 -
  1.1906 -static NTSTATUS
  1.1907 -XenPci_DOP_GetScatterGatherList(
  1.1908 -  PDMA_ADAPTER DmaAdapter,
  1.1909 -  PDEVICE_OBJECT DeviceObject,
  1.1910 -  PMDL Mdl,
  1.1911 -  PVOID CurrentVa,
  1.1912 -  ULONG Length,
  1.1913 -  PDRIVER_LIST_CONTROL ExecutionRoutine,
  1.1914 -  PVOID Context,
  1.1915 -  BOOLEAN WriteToDevice)
  1.1916 -{
  1.1917 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.1918 -  UNREFERENCED_PARAMETER(DeviceObject);
  1.1919 -  UNREFERENCED_PARAMETER(Mdl);
  1.1920 -  UNREFERENCED_PARAMETER(CurrentVa);
  1.1921 -  UNREFERENCED_PARAMETER(Length);
  1.1922 -  UNREFERENCED_PARAMETER(ExecutionRoutine);
  1.1923 -  UNREFERENCED_PARAMETER(Context);
  1.1924 -  UNREFERENCED_PARAMETER(WriteToDevice);
  1.1925 -
  1.1926 -  FUNCTION_ENTER();
  1.1927 -  FUNCTION_EXIT();
  1.1928 -  return STATUS_SUCCESS;
  1.1929 -}
  1.1930 -
  1.1931 -#define MAP_TYPE_VIRTUAL  1
  1.1932 -#define MAP_TYPE_MDL      2
  1.1933 -#define MAP_TYPE_REMAPPED 3
  1.1934 -
  1.1935 -typedef struct {
  1.1936 -  ULONG map_type;
  1.1937 -  PVOID aligned_buffer;
  1.1938 -  PVOID unaligned_buffer;
  1.1939 -  ULONG copy_length;
  1.1940 -} sg_extra_t;
  1.1941 -
  1.1942 -static VOID
  1.1943 -XenPci_DOP_PutScatterGatherList(
  1.1944 -    IN PDMA_ADAPTER DmaAdapter,
  1.1945 -    IN PSCATTER_GATHER_LIST ScatterGather,
  1.1946 -    IN BOOLEAN WriteToDevice
  1.1947 -    )
  1.1948 -{
  1.1949 -  xen_dma_adapter_t *xen_dma_adapter;
  1.1950 -  PXENPCI_DEVICE_DATA xpdd;
  1.1951 -  ULONG i;
  1.1952 -  sg_extra_t *sg_extra;
  1.1953 -
  1.1954 -  UNREFERENCED_PARAMETER(WriteToDevice);
  1.1955 -  
  1.1956 -  //FUNCTION_ENTER();
  1.1957 -
  1.1958 -  xen_dma_adapter = (xen_dma_adapter_t *)DmaAdapter;
  1.1959 -  xpdd = xen_dma_adapter->xppdd->bus_fdo->DeviceExtension;
  1.1960 -  
  1.1961 -  sg_extra = (sg_extra_t *)((PUCHAR)ScatterGather + FIELD_OFFSET(SCATTER_GATHER_LIST, Elements) +
  1.1962 -    (sizeof(SCATTER_GATHER_ELEMENT)) * ScatterGather->NumberOfElements);
  1.1963 -
  1.1964 -  switch (sg_extra->map_type)
  1.1965 -  {
  1.1966 -  case MAP_TYPE_REMAPPED:
  1.1967 -    for (i = 0; i < ScatterGather->NumberOfElements; i++)
  1.1968 -    {
  1.1969 -      grant_ref_t gref;
  1.1970 -      gref = (grant_ref_t)(ScatterGather->Elements[i].Address.QuadPart >> PAGE_SHIFT);
  1.1971 -      GntTbl_EndAccess(xpdd, gref, FALSE);
  1.1972 -      ScatterGather->Elements[i].Address.QuadPart = -1;
  1.1973 -    }
  1.1974 -    if (!WriteToDevice)
  1.1975 -      memcpy(sg_extra->unaligned_buffer, sg_extra->aligned_buffer, sg_extra->copy_length);
  1.1976 -    ExFreePoolWithTag(sg_extra->aligned_buffer, XENPCI_POOL_TAG);
  1.1977 -    break;
  1.1978 -  case MAP_TYPE_MDL:
  1.1979 -    for (i = 0; i < ScatterGather->NumberOfElements; i++)
  1.1980 -    {
  1.1981 -      grant_ref_t gref;
  1.1982 -      gref = (grant_ref_t)(ScatterGather->Elements[i].Address.QuadPart >> PAGE_SHIFT);
  1.1983 -      GntTbl_EndAccess(xpdd, gref, FALSE);
  1.1984 -      ScatterGather->Elements[i].Address.QuadPart = -1;
  1.1985 -    }
  1.1986 -    break;
  1.1987 -  case MAP_TYPE_VIRTUAL:
  1.1988 -    break;
  1.1989 -  }
  1.1990 -  //FUNCTION_EXIT();
  1.1991 -}
  1.1992 -
  1.1993 -static NTSTATUS
  1.1994 -XenPci_DOP_CalculateScatterGatherList(
  1.1995 -  PDMA_ADAPTER DmaAdapter,
  1.1996 -  PMDL Mdl,
  1.1997 -  PVOID CurrentVa,
  1.1998 -  ULONG Length,
  1.1999 -  PULONG ScatterGatherListSize,
  1.2000 -  PULONG NumberOfMapRegisters
  1.2001 -  )
  1.2002 -{
  1.2003 -  ULONG elements;
  1.2004 -  PMDL curr_mdl;
  1.2005 -  
  1.2006 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.2007 -  UNREFERENCED_PARAMETER(Mdl);
  1.2008 -  
  1.2009 -  FUNCTION_ENTER();
  1.2010 -  
  1.2011 -  KdPrint((__DRIVER_NAME "     Mdl = %p\n", Mdl));
  1.2012 -  KdPrint((__DRIVER_NAME "     CurrentVa = %p\n", CurrentVa));
  1.2013 -  KdPrint((__DRIVER_NAME "     Length = %d\n", Length));
  1.2014 -  if (Mdl)
  1.2015 -  {
  1.2016 -    for (curr_mdl = Mdl, elements = 0; curr_mdl; curr_mdl = curr_mdl->Next)
  1.2017 -      elements += ADDRESS_AND_SIZE_TO_SPAN_PAGES(CurrentVa, Length);
  1.2018 -  }
  1.2019 -  else
  1.2020 -  {
  1.2021 -    elements = ADDRESS_AND_SIZE_TO_SPAN_PAGES(0, Length) + 1;
  1.2022 -  }
  1.2023 -  
  1.2024 -  *ScatterGatherListSize = FIELD_OFFSET(SCATTER_GATHER_LIST, Elements)
  1.2025 -    + sizeof(SCATTER_GATHER_ELEMENT) * elements
  1.2026 -    + sizeof(sg_extra_t);
  1.2027 -  if (NumberOfMapRegisters)
  1.2028 -    *NumberOfMapRegisters = 1;
  1.2029 -
  1.2030 -  KdPrint((__DRIVER_NAME "     ScatterGatherListSize = %d\n", *ScatterGatherListSize));
  1.2031 -
  1.2032 -  FUNCTION_EXIT();
  1.2033 -  return STATUS_SUCCESS;
  1.2034 -}
  1.2035 -
  1.2036 -static NTSTATUS
  1.2037 -XenPci_DOP_BuildScatterGatherList(
  1.2038 -  IN PDMA_ADAPTER DmaAdapter,
  1.2039 -  IN PDEVICE_OBJECT DeviceObject,
  1.2040 -  IN PMDL Mdl,
  1.2041 -  IN PVOID CurrentVa,
  1.2042 -  IN ULONG Length,
  1.2043 -  IN PDRIVER_LIST_CONTROL ExecutionRoutine,
  1.2044 -  IN PVOID Context,
  1.2045 -  IN BOOLEAN WriteToDevice,
  1.2046 -  IN PVOID ScatterGatherBuffer,
  1.2047 -  IN ULONG ScatterGatherBufferLength)
  1.2048 -{
  1.2049 -  ULONG i;
  1.2050 -  PSCATTER_GATHER_LIST sglist = ScatterGatherBuffer;
  1.2051 -  PUCHAR ptr;
  1.2052 -  ULONG remaining = Length;
  1.2053 -  ULONG total_remaining;
  1.2054 -  xen_dma_adapter_t *xen_dma_adapter;
  1.2055 -  PXENPCI_DEVICE_DATA xpdd;
  1.2056 -  sg_extra_t *sg_extra;
  1.2057 -  PMDL curr_mdl;
  1.2058 -  ULONG map_type;
  1.2059 -  ULONG sg_element;
  1.2060 -  ULONG offset;
  1.2061 -  PFN_NUMBER pfn;
  1.2062 -  grant_ref_t gref;
  1.2063 -  
  1.2064 -  UNREFERENCED_PARAMETER(WriteToDevice);
  1.2065 -
  1.2066 -  //FUNCTION_ENTER();
  1.2067 -
  1.2068 -  xen_dma_adapter = (xen_dma_adapter_t *)DmaAdapter;
  1.2069 -  xpdd = xen_dma_adapter->xppdd->bus_fdo->DeviceExtension;
  1.2070 -
  1.2071 -  ASSERT(Mdl);
  1.2072 -  if (xen_dma_adapter->dma_extension)
  1.2073 -  {
  1.2074 -    if (xen_dma_adapter->dma_extension->need_virtual_address(DeviceObject->CurrentIrp))
  1.2075 -    {
  1.2076 -      ASSERT(!Mdl->Next); /* can only virtual a single buffer */
  1.2077 -      map_type = MAP_TYPE_VIRTUAL;
  1.2078 -      sglist->NumberOfElements = 1;
  1.2079 -    }
  1.2080 -    else
  1.2081 -    {
  1.2082 -      ULONG alignment = xen_dma_adapter->dma_extension->get_alignment(DeviceObject->CurrentIrp);
  1.2083 -      if (PtrToUlong(CurrentVa) & (alignment - 1))
  1.2084 -      {
  1.2085 -        ASSERT(!Mdl->Next); /* can only remap a single buffer */
  1.2086 -        map_type = MAP_TYPE_REMAPPED;
  1.2087 -        sglist->NumberOfElements = ADDRESS_AND_SIZE_TO_SPAN_PAGES(NULL, Length);
  1.2088 -      }
  1.2089 -      else
  1.2090 -      {
  1.2091 -        map_type = MAP_TYPE_MDL;
  1.2092 -        for (curr_mdl = Mdl, sglist->NumberOfElements = 0; curr_mdl; curr_mdl = curr_mdl->Next)
  1.2093 -          sglist->NumberOfElements += ADDRESS_AND_SIZE_TO_SPAN_PAGES(
  1.2094 -            MmGetMdlVirtualAddress(curr_mdl), MmGetMdlByteCount(curr_mdl));
  1.2095 -      }
  1.2096 -    }
  1.2097 -  }
  1.2098 -  else
  1.2099 -  {
  1.2100 -    map_type = MAP_TYPE_MDL;
  1.2101 -    for (curr_mdl = Mdl, sglist->NumberOfElements = 0; curr_mdl; curr_mdl = curr_mdl->Next)
  1.2102 -      sglist->NumberOfElements += ADDRESS_AND_SIZE_TO_SPAN_PAGES(
  1.2103 -        MmGetMdlVirtualAddress(curr_mdl), MmGetMdlByteCount(curr_mdl));
  1.2104 -  }
  1.2105 -  if (ScatterGatherBufferLength < FIELD_OFFSET(SCATTER_GATHER_LIST, Elements) +
  1.2106 -    sizeof(SCATTER_GATHER_ELEMENT) * sglist->NumberOfElements + sizeof(sg_extra_t))
  1.2107 -  {
  1.2108 -    return STATUS_BUFFER_TOO_SMALL;
  1.2109 -  }
  1.2110 -  
  1.2111 -  sg_extra = (sg_extra_t *)((PUCHAR)sglist + FIELD_OFFSET(SCATTER_GATHER_LIST, Elements) +
  1.2112 -    (sizeof(SCATTER_GATHER_ELEMENT)) * sglist->NumberOfElements);
  1.2113 -  
  1.2114 -  sg_extra->map_type = map_type;
  1.2115 -  switch (map_type)
  1.2116 -  {
  1.2117 -  case MAP_TYPE_MDL:
  1.2118 -    KdPrint((__DRIVER_NAME "     MAP_TYPE_MDL - %p\n", CurrentVa));
  1.2119 -    total_remaining = Length;
  1.2120 -    for (sg_element = 0, curr_mdl = Mdl; curr_mdl; curr_mdl = curr_mdl->Next)
  1.2121 -    {
  1.2122 -      remaining = MmGetMdlByteCount(curr_mdl);
  1.2123 -      if (!MmGetMdlByteOffset(Mdl) && (PtrToUlong(CurrentVa) & (PAGE_SIZE - 1)))
  1.2124 -        offset = (PtrToUlong(CurrentVa) & (PAGE_SIZE - 1));
  1.2125 -      else
  1.2126 -        offset = MmGetMdlByteOffset(curr_mdl);
  1.2127 -      for (i = 0; i < ADDRESS_AND_SIZE_TO_SPAN_PAGES(MmGetMdlVirtualAddress(curr_mdl), MmGetMdlByteCount(curr_mdl)); i++)
  1.2128 -      {
  1.2129 -//KdPrint((__DRIVER_NAME "     element = %d\n", sg_element));
  1.2130 -//KdPrint((__DRIVER_NAME "     remaining = %d\n", remaining));
  1.2131 -        pfn = MmGetMdlPfnArray(curr_mdl)[i];
  1.2132 -        ASSERT(pfn);
  1.2133 -        gref = (grant_ref_t)GntTbl_GrantAccess(xpdd, 0, pfn, FALSE, INVALID_GRANT_REF);
  1.2134 -        ASSERT(gref != INVALID_GRANT_REF);
  1.2135 -        sglist->Elements[sg_element].Address.QuadPart = (LONGLONG)(gref << PAGE_SHIFT) | offset;
  1.2136 -        sglist->Elements[sg_element].Length = min(min(PAGE_SIZE - offset, remaining), total_remaining);
  1.2137 -        total_remaining -= sglist->Elements[sg_element].Length;
  1.2138 -        remaining -= sglist->Elements[sg_element].Length;
  1.2139 -        offset = 0;
  1.2140 -        sg_element++;
  1.2141 -      }
  1.2142 -    }
  1.2143 -    break;
  1.2144 -  case MAP_TYPE_REMAPPED:
  1.2145 -    sg_extra->aligned_buffer = ExAllocatePoolWithTag(NonPagedPool, max(Length, PAGE_SIZE), XENPCI_POOL_TAG);
  1.2146 -    ASSERT(sg_extra->aligned_buffer); /* lazy */
  1.2147 -    KdPrint((__DRIVER_NAME "     MAP_TYPE_REMAPPED - %p -> %p\n", CurrentVa, sg_extra->aligned_buffer));
  1.2148 -    sg_extra->unaligned_buffer = MmGetSystemAddressForMdlSafe(Mdl, NormalPagePriority);
  1.2149 -    ASSERT(sg_extra->unaligned_buffer); /* lazy */
  1.2150 -    if (!MmGetMdlByteOffset(Mdl) && (PtrToUlong(CurrentVa) & (PAGE_SIZE - 1)))
  1.2151 -      sg_extra->unaligned_buffer = (PUCHAR)sg_extra->unaligned_buffer + (PtrToUlong(CurrentVa) & (PAGE_SIZE - 1));
  1.2152 -    sg_extra->copy_length = Length;
  1.2153 -    if (WriteToDevice)
  1.2154 -      memcpy(sg_extra->aligned_buffer, sg_extra->unaligned_buffer, sg_extra->copy_length);
  1.2155 -    for (sg_element = 0, remaining = Length; 
  1.2156 -      sg_element < ADDRESS_AND_SIZE_TO_SPAN_PAGES(sg_extra->aligned_buffer, Length); sg_element++)
  1.2157 -    {
  1.2158 -      pfn = (PFN_NUMBER)(MmGetPhysicalAddress((PUCHAR)sg_extra->aligned_buffer + (sg_element << PAGE_SHIFT)).QuadPart >> PAGE_SHIFT);
  1.2159 -      ASSERT(pfn);
  1.2160 -      gref = (grant_ref_t)GntTbl_GrantAccess(xpdd, 0, pfn, FALSE, INVALID_GRANT_REF);
  1.2161 -      ASSERT(gref);
  1.2162 -      sglist->Elements[sg_element].Address.QuadPart = (ULONGLONG)gref << PAGE_SHIFT;
  1.2163 -      sglist->Elements[sg_element].Length = min(PAGE_SIZE, remaining);
  1.2164 -      remaining -= sglist->Elements[sg_element].Length;
  1.2165 -    }
  1.2166 -    break;
  1.2167 -  case MAP_TYPE_VIRTUAL:
  1.2168 -    KdPrint((__DRIVER_NAME "     MAP_TYPE_VIRTUAL\n"));
  1.2169 -    ptr = MmGetSystemAddressForMdlSafe(Mdl, NormalPagePriority);
  1.2170 -    ASSERT(ptr); /* lazy */
  1.2171 -    if (!MmGetMdlByteOffset(Mdl) && (PtrToUlong(CurrentVa) & (PAGE_SIZE - 1)))
  1.2172 -      ptr += (PtrToUlong(CurrentVa) & (PAGE_SIZE - 1));
  1.2173 -    sglist->Elements[0].Address.QuadPart = (ULONGLONG)ptr;
  1.2174 -    sglist->Elements[0].Length = Length;
  1.2175 -    break;
  1.2176 -  }
  1.2177 -#if 0
  1.2178 -  KdPrint((__DRIVER_NAME "     Mdl = %p, CurrentVa = %p, Mdl->Va = %p, Offset = %d, Length = %d\n", 
  1.2179 -    Mdl, CurrentVa, MmGetMdlVirtualAddress(Mdl), MmGetMdlByteOffset(Mdl), Length));
  1.2180 -  for (i = 0; i < sglist->NumberOfElements; i++)
  1.2181 -  {
  1.2182 -    KdPrint((__DRIVER_NAME "     sge[%d]->Address = %08x%08x, Length = %d\n", i, sglist->Elements[i].Address.HighPart,
  1.2183 -      sglist->Elements[i].Address.LowPart, sglist->Elements[i].Length));
  1.2184 -  }
  1.2185 -#endif
  1.2186 -  ExecutionRoutine(DeviceObject, DeviceObject->CurrentIrp, ScatterGatherBuffer, Context);
  1.2187 -
  1.2188 -  //FUNCTION_EXIT();
  1.2189 -  
  1.2190 -  return STATUS_SUCCESS;
  1.2191 -}
  1.2192 -
  1.2193 -static NTSTATUS
  1.2194 -XenPci_DOP_BuildMdlFromScatterGatherList(
  1.2195 -  PDMA_ADAPTER DmaAdapter,
  1.2196 -  PSCATTER_GATHER_LIST ScatterGather,
  1.2197 -  PMDL OriginalMdl,
  1.2198 -  PMDL *TargetMdl)
  1.2199 -{
  1.2200 -  UNREFERENCED_PARAMETER(DmaAdapter);
  1.2201 -  UNREFERENCED_PARAMETER(ScatterGather);
  1.2202 -  UNREFERENCED_PARAMETER(OriginalMdl);
  1.2203 -  UNREFERENCED_PARAMETER(TargetMdl);
  1.2204 -
  1.2205 -  FUNCTION_ENTER();
  1.2206 -  FUNCTION_EXIT();
  1.2207 -  return STATUS_UNSUCCESSFUL;
  1.2208 -}
  1.2209 -
  1.2210 -static PDMA_ADAPTER
  1.2211 -XenPci_BIS_GetDmaAdapter(PVOID context, PDEVICE_DESCRIPTION device_description, PULONG number_of_map_registers)
  1.2212 -{
  1.2213 -  xen_dma_adapter_t *xen_dma_adapter;
  1.2214 -  PDEVICE_OBJECT curr, prev;
  1.2215 -  PDRIVER_OBJECT fdo_driver_object;
  1.2216 -  PVOID fdo_driver_extension;
  1.2217 -  
  1.2218 -  UNREFERENCED_PARAMETER(device_description);
  1.2219 -  
  1.2220 -  FUNCTION_ENTER();
  1.2221 -
  1.2222 -  KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
  1.2223 -  KdPrint((__DRIVER_NAME "     Device Description = %p:\n", device_description));
  1.2224 -  KdPrint((__DRIVER_NAME "      Version  = %d\n", device_description->Version));
  1.2225 -  KdPrint((__DRIVER_NAME "      Master = %d\n", device_description->Master));
  1.2226 -  KdPrint((__DRIVER_NAME "      ScatterGather = %d\n", device_description->ScatterGather));
  1.2227 -  KdPrint((__DRIVER_NAME "      DemandMode = %d\n", device_description->DemandMode));
  1.2228 -  KdPrint((__DRIVER_NAME "      AutoInitialize = %d\n", device_description->AutoInitialize));
  1.2229 -  KdPrint((__DRIVER_NAME "      Dma32BitAddresses = %d\n", device_description->Dma32BitAddresses));
  1.2230 -  KdPrint((__DRIVER_NAME "      IgnoreCount = %d\n", device_description->IgnoreCount));
  1.2231 -  KdPrint((__DRIVER_NAME "      Dma64BitAddresses = %d\n", device_description->Dma64BitAddresses));
  1.2232 -  KdPrint((__DRIVER_NAME "      BusNumber = %d\n", device_description->BusNumber));
  1.2233 -  KdPrint((__DRIVER_NAME "      DmaChannel = %d\n", device_description->DmaChannel));
  1.2234 -  KdPrint((__DRIVER_NAME "      InterfaceType = %d\n", device_description->InterfaceType));
  1.2235 -  KdPrint((__DRIVER_NAME "      DmaWidth = %d\n", device_description->DmaWidth));
  1.2236 -  KdPrint((__DRIVER_NAME "      DmaSpeed = %d\n", device_description->DmaSpeed));
  1.2237 -  KdPrint((__DRIVER_NAME "      MaximumLength = %d\n", device_description->MaximumLength));
  1.2238 -  KdPrint((__DRIVER_NAME "      DmaPort = %d\n", device_description->DmaPort));
  1.2239 -  
  1.2240 -/*
  1.2241 -we have to allocate PAGE_SIZE bytes here because Windows thinks this is
  1.2242 -actually an ADAPTER_OBJECT, and then the verifier crashes because
  1.2243 -Windows accessed beyond the end of the structure :(
  1.2244 -*/
  1.2245 -  xen_dma_adapter = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
  1.2246 -  RtlZeroMemory(xen_dma_adapter, PAGE_SIZE);
  1.2247 -  xen_dma_adapter->dma_adapter.Version = 2;
  1.2248 -  xen_dma_adapter->dma_adapter.Size = sizeof(DMA_ADAPTER); //xen_dma_adapter_t);
  1.2249 -  xen_dma_adapter->dma_adapter.DmaOperations = ExAllocatePoolWithTag(NonPagedPool, sizeof(DMA_OPERATIONS), XENPCI_POOL_TAG);
  1.2250 -  //xen_dma_adapter->dma_adapter.DmaOperations = &xen_dma_adapter->dma_operations;
  1.2251 -  xen_dma_adapter->dma_adapter.DmaOperations->Size = sizeof(DMA_OPERATIONS);
  1.2252 -  xen_dma_adapter->dma_adapter.DmaOperations->PutDmaAdapter = XenPci_DOP_PutDmaAdapter;
  1.2253 -  xen_dma_adapter->dma_adapter.DmaOperations->AllocateCommonBuffer = XenPci_DOP_AllocateCommonBuffer;
  1.2254 -  xen_dma_adapter->dma_adapter.DmaOperations->FreeCommonBuffer = XenPci_DOP_FreeCommonBuffer;
  1.2255 -  xen_dma_adapter->dma_adapter.DmaOperations->AllocateAdapterChannel = XenPci_DOP_AllocateAdapterChannel;
  1.2256 -  xen_dma_adapter->dma_adapter.DmaOperations->FlushAdapterBuffers = XenPci_DOP_FlushAdapterBuffers;
  1.2257 -  xen_dma_adapter->dma_adapter.DmaOperations->FreeAdapterChannel = XenPci_DOP_FreeAdapterChannel;
  1.2258 -  xen_dma_adapter->dma_adapter.DmaOperations->FreeMapRegisters = XenPci_DOP_FreeMapRegisters;
  1.2259 -  xen_dma_adapter->dma_adapter.DmaOperations->MapTransfer = XenPci_DOP_MapTransfer;
  1.2260 -  xen_dma_adapter->dma_adapter.DmaOperations->GetDmaAlignment = XenPci_DOP_GetDmaAlignment;
  1.2261 -  xen_dma_adapter->dma_adapter.DmaOperations->ReadDmaCounter = XenPci_DOP_ReadDmaCounter;
  1.2262 -  xen_dma_adapter->dma_adapter.DmaOperations->GetScatterGatherList = XenPci_DOP_GetScatterGatherList;
  1.2263 -  xen_dma_adapter->dma_adapter.DmaOperations->PutScatterGatherList = XenPci_DOP_PutScatterGatherList;
  1.2264 -  xen_dma_adapter->dma_adapter.DmaOperations->CalculateScatterGatherList = XenPci_DOP_CalculateScatterGatherList;
  1.2265 -  xen_dma_adapter->dma_adapter.DmaOperations->BuildScatterGatherList = XenPci_DOP_BuildScatterGatherList;
  1.2266 -  xen_dma_adapter->dma_adapter.DmaOperations->BuildMdlFromScatterGatherList = XenPci_DOP_BuildMdlFromScatterGatherList;
  1.2267 -  xen_dma_adapter->xppdd = context;
  1.2268 -  xen_dma_adapter->dma_extension = NULL;
  1.2269 -
  1.2270 -  KdPrint((__DRIVER_NAME "     About to call IoGetAttachedDeviceReference\n"));
  1.2271 -  curr = IoGetAttachedDeviceReference(xen_dma_adapter->xppdd->common.pdo);
  1.2272 -  KdPrint((__DRIVER_NAME "     Before start of loop - curr = %p\n", curr));
  1.2273 -  while (curr != NULL)
  1.2274 -  {
  1.2275 -    fdo_driver_object = curr->DriverObject;
  1.2276 -    KdPrint((__DRIVER_NAME "     fdo_driver_object = %p\n", fdo_driver_object));
  1.2277 -    if (fdo_driver_object)
  1.2278 -    {
  1.2279 -      fdo_driver_extension = IoGetDriverObjectExtension(fdo_driver_object, UlongToPtr(XEN_DMA_DRIVER_EXTENSION_MAGIC));
  1.2280 -      if (fdo_driver_extension)
  1.2281 -      {
  1.2282 -        xen_dma_adapter->dma_extension = (dma_driver_extension_t *)fdo_driver_extension;
  1.2283 -        ObDereferenceObject(curr);
  1.2284 -        break;
  1.2285 -      }
  1.2286 -    }
  1.2287 -    prev = curr;
  1.2288 -    curr = IoGetLowerDeviceObject(curr);
  1.2289 -    ObDereferenceObject(prev);
  1.2290 -  }
  1.2291 -  KdPrint((__DRIVER_NAME "     End of loop\n"));
  1.2292 -
  1.2293 -  *number_of_map_registers = 1024; //1024; /* why not... */
  1.2294 -
  1.2295 -  FUNCTION_EXIT();
  1.2296 -
  1.2297 -  return &xen_dma_adapter->dma_adapter;
  1.2298 -}
  1.2299 -
  1.2300 -static ULONG
  1.2301 -XenPci_BIS_SetBusData(PVOID context, ULONG data_type, PVOID buffer, ULONG offset, ULONG length)
  1.2302 -{
  1.2303 -  UNREFERENCED_PARAMETER(context);
  1.2304 -  UNREFERENCED_PARAMETER(data_type);
  1.2305 -  UNREFERENCED_PARAMETER(buffer);
  1.2306 -  UNREFERENCED_PARAMETER(offset);
  1.2307 -  UNREFERENCED_PARAMETER(length);
  1.2308 -  
  1.2309 -  FUNCTION_ENTER();
  1.2310 -  FUNCTION_EXIT();
  1.2311 -  return 0;
  1.2312 -}
  1.2313 -
  1.2314 -static ULONG
  1.2315 -XenPci_BIS_GetBusData(PVOID context, ULONG data_type, PVOID buffer, ULONG offset, ULONG length)
  1.2316 -{
  1.2317 -  UNREFERENCED_PARAMETER(context);
  1.2318 -  UNREFERENCED_PARAMETER(data_type);
  1.2319 -  UNREFERENCED_PARAMETER(buffer);
  1.2320 -  UNREFERENCED_PARAMETER(offset);
  1.2321 -  UNREFERENCED_PARAMETER(length);
  1.2322 -  
  1.2323 -  FUNCTION_ENTER();
  1.2324 -  FUNCTION_EXIT();
  1.2325 -  return 0;
  1.2326 -}
  1.2327 -
  1.2328 -static NTSTATUS
  1.2329 -XenPci_QueryInterface(PDEVICE_OBJECT device_object, PIRP irp)
  1.2330 -{
  1.2331 -  NTSTATUS status;
  1.2332 -  PIO_STACK_LOCATION stack;
  1.2333 -  PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
  1.2334 -  PBUS_INTERFACE_STANDARD bis;
  1.2335 -  PGUID guid;
  1.2336 -
  1.2337 -  FUNCTION_ENTER();
  1.2338 -
  1.2339 -  stack = IoGetCurrentIrpStackLocation(irp);
  1.2340 -
  1.2341 -  if (memcmp(stack->Parameters.QueryInterface.InterfaceType, &GUID_BUS_INTERFACE_STANDARD, sizeof(GUID_BUS_INTERFACE_STANDARD)) == 0)
  1.2342 -  {
  1.2343 -    KdPrint((__DRIVER_NAME "      GUID_BUS_INTERFACE_STANDARD\n"));
  1.2344 -    if (stack->Parameters.QueryInterface.Size < sizeof(BUS_INTERFACE_STANDARD))
  1.2345 -    {
  1.2346 -      KdPrint((__DRIVER_NAME "      buffer too small\n"));
  1.2347 -      status = STATUS_INVALID_PARAMETER;
  1.2348 -      FUNCTION_EXIT();
  1.2349 -      return status;
  1.2350 -    }
  1.2351 -    if (stack->Parameters.QueryInterface.Version != 1)
  1.2352 -    {
  1.2353 -      KdPrint((__DRIVER_NAME "      incorrect version %d\n", stack->Parameters.QueryInterface.Version));
  1.2354 -      status = STATUS_INVALID_PARAMETER;
  1.2355 -      FUNCTION_EXIT();
  1.2356 -      return status;
  1.2357 -    }
  1.2358 -    bis = (PBUS_INTERFACE_STANDARD)stack->Parameters.QueryInterface.Interface;
  1.2359 -    bis->Size = sizeof(BUS_INTERFACE_STANDARD);
  1.2360 -    bis->Version = 1; //BUS_INTERFACE_STANDARD_VERSION;
  1.2361 -    bis->Context = xppdd;
  1.2362 -    bis->InterfaceReference = XenPci_IS_InterfaceReference;
  1.2363 -    bis->InterfaceDereference = XenPci_IS_InterfaceReference;
  1.2364 -    bis->TranslateBusAddress = XenPci_BIS_TranslateBusAddress;
  1.2365 -    bis->GetDmaAdapter = XenPci_BIS_GetDmaAdapter;
  1.2366 -    bis->SetBusData = XenPci_BIS_SetBusData;
  1.2367 -    bis->GetBusData = XenPci_BIS_GetBusData;
  1.2368 -    status = STATUS_SUCCESS;
  1.2369 -    FUNCTION_EXIT();
  1.2370 -    return status;
  1.2371 -  }
  1.2372 -  else
  1.2373 -  {
  1.2374 -    guid = (PGUID)stack->Parameters.QueryInterface.InterfaceType;
  1.2375 -    KdPrint((__DRIVER_NAME "      Unknown GUID %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
  1.2376 -    guid->Data1, (ULONG)guid->Data2, (ULONG)guid->Data3, (ULONG)guid->Data4[0], (ULONG)guid->Data4[1],
  1.2377 -    (ULONG)guid->Data4[2], (ULONG)guid->Data4[3], (ULONG)guid->Data4[4], (ULONG)guid->Data4[5],
  1.2378 -    (ULONG)guid->Data4[6], (ULONG)guid->Data4[7]));
  1.2379 -    status = irp->IoStatus.Status;
  1.2380 -    FUNCTION_EXIT();
  1.2381 -    return status;
  1.2382 -  }
  1.2383 -}
  1.2384 -
  1.2385 -NTSTATUS
  1.2386 -XenPci_Pnp_Pdo(PDEVICE_OBJECT device_object, PIRP irp)
  1.2387 -{
  1.2388 -  NTSTATUS status;
  1.2389 -  PIO_STACK_LOCATION stack;
  1.2390 -  PXENPCI_PDO_DEVICE_DATA xppdd = (PXENPCI_PDO_DEVICE_DATA)device_object->DeviceExtension;
  1.2391 -  //PXENPCI_DEVICE_DATA xpdd = xppdd->bus_fdo->DeviceExtension;
  1.2392 -  LPWSTR buffer;
  1.2393 -  WCHAR widebuf[256];
  1.2394 -  unsigned int i;
  1.2395 -  PPNP_BUS_INFORMATION pbi;
  1.2396 -  ULONG *usage_type;
  1.2397 -
  1.2398 -  //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
  1.2399 -
  1.2400 -  stack = IoGetCurrentIrpStackLocation(irp);
  1.2401 -
  1.2402 -  switch (stack->MinorFunction)
  1.2403 -  {
  1.2404 -  case IRP_MN_START_DEVICE:
  1.2405 -    KdPrint((__DRIVER_NAME "     IRP_MN_START_DEVICE (status = %08x)\n", irp->IoStatus.Status));
  1.2406 -    status = XenPci_Pnp_StartDevice(device_object, irp);
  1.2407 -    break;
  1.2408 -    
  1.2409 -  case IRP_MN_QUERY_STOP_DEVICE:
  1.2410 -    KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_STOP_DEVICE (status = %08x)\n", irp->IoStatus.Status));
  1.2411 -    SET_PNP_STATE(&xppdd->common, StopPending);
  1.2412 -    status = STATUS_SUCCESS;
  1.2413 -    break;
  1.2414 -
  1.2415 -  case IRP_MN_STOP_DEVICE:
  1.2416 -    KdPrint((__DRIVER_NAME "     IRP_MN_STOP_DEVICE (status = %08x)\n", irp->IoStatus.Status));
  1.2417 -    SET_PNP_STATE(&xppdd->common, Stopped);
  1.2418 -    status = STATUS_SUCCESS;
  1.2419 -    break;
  1.2420 -
  1.2421 -  case IRP_MN_CANCEL_STOP_DEVICE:
  1.2422 -    KdPrint((__DRIVER_NAME "     IRP_MN_CANCEL_STOP_DEVICE (status = %08x)\n", irp->IoStatus.Status));
  1.2423 -    REVERT_PNP_STATE(&xppdd->common);
  1.2424 -    status = STATUS_SUCCESS;
  1.2425 -    break;
  1.2426 -
  1.2427 -  case IRP_MN_QUERY_REMOVE_DEVICE:
  1.2428 -    KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_REMOVE_DEVICE (status = %08x)\n", irp->IoStatus.Status));
  1.2429 -    SET_PNP_STATE(&xppdd->common, RemovePending);
  1.2430 -    status = STATUS_SUCCESS;
  1.2431 -    break;
  1.2432 -
  1.2433 -  case IRP_MN_REMOVE_DEVICE:
  1.2434 -    KdPrint((__DRIVER_NAME "     IRP_MN_REMOVE_DEVICE (status = %08x)\n", irp->IoStatus.Status));
  1.2435 -    status = XenPci_Pnp_RemoveDevice(device_object, irp);
  1.2436 -    break;
  1.2437 -
  1.2438 -  case IRP_MN_CANCEL_REMOVE_DEVICE:
  1.2439 -    KdPrint((__DRIVER_NAME "     IRP_MN_CANCEL_REMOVE_DEVICE (status = %08x)\n", irp->IoStatus.Status));
  1.2440 -    REVERT_PNP_STATE(&xppdd->common);
  1.2441 -    status = STATUS_SUCCESS;
  1.2442 -    break;
  1.2443 -
  1.2444 -  case IRP_MN_SURPRISE_REMOVAL:
  1.2445 -    KdPrint((__DRIVER_NAME "     IRP_MN_SURPRISE_REMOVAL (status = %08x)\n", irp->IoStatus.Status));
  1.2446 -    SET_PNP_STATE(&xppdd->common, SurpriseRemovePending);
  1.2447 -    status = STATUS_SUCCESS;
  1.2448 -    break;
  1.2449 -
  1.2450 -  case IRP_MN_DEVICE_USAGE_NOTIFICATION:
  1.2451 -    KdPrint((__DRIVER_NAME "     IRP_MN_DEVICE_USAGE_NOTIFICATION (status = %08x)\n", irp->IoStatus.Status));
  1.2452 -    
  1.2453 -    usage_type = NULL;
  1.2454 -    switch (stack->Parameters.UsageNotification.Type)
  1.2455 -    {
  1.2456 -    case DeviceUsageTypePaging:
  1.2457 -      KdPrint((__DRIVER_NAME "     type = DeviceUsageTypePaging\n"));
  1.2458 -      usage_type = &xppdd->common.device_usage_paging;
  1.2459 -      break;
  1.2460 -    case DeviceUsageTypeDumpFile:
  1.2461 -      KdPrint((__DRIVER_NAME "     type = DeviceUsageTypeDumpFile\n"));
  1.2462 -      usage_type = &xppdd->common.device_usage_dump;
  1.2463 -      break;
  1.2464 -    case DeviceUsageTypeHibernation:
  1.2465 -      KdPrint((__DRIVER_NAME "     type = DeviceUsageTypeHibernation\n"));
  1.2466 -      usage_type = &xppdd->common.device_usage_hibernation;
  1.2467 -      break;
  1.2468 -    default:
  1.2469 -      KdPrint((__DRIVER_NAME " Unknown usage type %x\n",
  1.2470 -        stack->Parameters.UsageNotification.Type));
  1.2471 -      break;
  1.2472 -    }
  1.2473 -    KdPrint((__DRIVER_NAME "     inpath = %d\n", stack->Parameters.UsageNotification.InPath));
  1.2474 -    if (usage_type)
  1.2475 -    {
  1.2476 -      if (stack->Parameters.UsageNotification.InPath)
  1.2477 -        (*usage_type)++;
  1.2478 -      else
  1.2479 -        (*usage_type)--;
  1.2480 -    }        
  1.2481 -    status = STATUS_SUCCESS;
  1.2482 -    break;
  1.2483 -
  1.2484 -  case IRP_MN_QUERY_ID:
  1.2485 -    KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_ID (status = %08x)\n", irp->IoStatus.Status));
  1.2486 -    switch (stack->Parameters.QueryId.IdType)
  1.2487 -    {
  1.2488 -    case BusQueryDeviceID: /* REG_SZ */
  1.2489 -      KdPrint((__DRIVER_NAME "     BusQueryDeviceID\n"));
  1.2490 -      buffer = ExAllocatePoolWithTag(PagedPool, 512, XENPCI_POOL_TAG);
  1.2491 -      for (i = 0; i < strlen(xppdd->device); i++)
  1.2492 -        widebuf[i] = xppdd->device[i];
  1.2493 -      widebuf[i] = 0;
  1.2494 -      RtlStringCbPrintfW(buffer, 512, L"Xen\\%ws", widebuf);
  1.2495 -      KdPrint((__DRIVER_NAME "     %ls\n", buffer));
  1.2496 -      irp->IoStatus.Information = (ULONG_PTR)buffer;
  1.2497 -      status = STATUS_SUCCESS;
  1.2498 -      break;
  1.2499 -    case BusQueryHardwareIDs: /* REG_MULTI_SZ */
  1.2500 -      KdPrint((__DRIVER_NAME "     BusQueryHardwareIDs\n"));
  1.2501 -      buffer = ExAllocatePoolWithTag(PagedPool, 512, XENPCI_POOL_TAG);
  1.2502 -      for (i = 0; i < strlen(xppdd->device); i++)
  1.2503 -        widebuf[i] = xppdd->device[i];
  1.2504 -      widebuf[i] = 0;
  1.2505 -      RtlStringCbPrintfW(buffer, 512, L"Xen\\%ws", widebuf);
  1.2506 -      for (i = 0; buffer[i] != 0; i++);
  1.2507 -      buffer[i + 1] = 0;      
  1.2508 -//      for (i = 0; i < 256; i++)
  1.2509 -//        KdPrint((__DRIVER_NAME "     %04X: %04X %wc\n", i, buffer[i], buffer[i]));
  1.2510 -      KdPrint((__DRIVER_NAME "     %ls\n", buffer));
  1.2511 -      irp->IoStatus.Information = (ULONG_PTR)buffer;
  1.2512 -      status = STATUS_SUCCESS;
  1.2513 -      break;
  1.2514 -    case BusQueryCompatibleIDs: /* REG_MULTI_SZ */
  1.2515 -      KdPrint((__DRIVER_NAME "     BusQueryCompatibleIDs\n"));
  1.2516 -      buffer = ExAllocatePoolWithTag(PagedPool, 512, XENPCI_POOL_TAG);
  1.2517 -      for (i = 0; i < strlen(xppdd->device); i++)
  1.2518 -        widebuf[i] = xppdd->device[i];
  1.2519 -      widebuf[i] = 0;
  1.2520 -      RtlStringCbPrintfW(buffer, 512, L"Xen\\%ws", widebuf);
  1.2521 -      for (i = 0; buffer[i] != 0; i++);
  1.2522 -      buffer[i + 1] = 0;
  1.2523 -      KdPrint((__DRIVER_NAME "     %ls\n", buffer));
  1.2524 -      irp->IoStatus.Information = (ULONG_PTR)buffer;
  1.2525 -      status = STATUS_SUCCESS;
  1.2526 -      break;
  1.2527 -    case BusQueryInstanceID: /* REG_SZ */
  1.2528 -      KdPrint((__DRIVER_NAME "     BusQueryInstanceID\n"));
  1.2529 -      buffer = ExAllocatePoolWithTag(PagedPool, 512, XENPCI_POOL_TAG);
  1.2530 -      RtlStringCbPrintfW(buffer, 512, L"%02d", xppdd->index);
  1.2531 -      KdPrint((__DRIVER_NAME "     %ls\n", buffer));
  1.2532 -      irp->IoStatus.Information = (ULONG_PTR)buffer;
  1.2533 -      status = STATUS_SUCCESS;
  1.2534 -      break;
  1.2535 -    default:
  1.2536 -      KdPrint((__DRIVER_NAME "     Unhandled IdType = %d\n", stack->Parameters.QueryId.IdType));
  1.2537 -      irp->IoStatus.Information = 0;
  1.2538 -      status = STATUS_NOT_SUPPORTED;
  1.2539 -      break;
  1.2540 -    }
  1.2541 -    break;
  1.2542 -    
  1.2543 -  case IRP_MN_QUERY_DEVICE_TEXT:
  1.2544 -    KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_DEVICE_TEXT (status = %08x)\n", irp->IoStatus.Status));
  1.2545 -    switch (stack->Parameters.QueryDeviceText.DeviceTextType)
  1.2546 -    {
  1.2547 -    case DeviceTextDescription:
  1.2548 -      KdPrint((__DRIVER_NAME "     DeviceTextDescription\n"));
  1.2549 -      buffer = ExAllocatePoolWithTag(PagedPool, 512, XENPCI_POOL_TAG);
  1.2550 -      for (i = 0; i < strlen(xppdd->device); i++)
  1.2551 -        widebuf[i] = xppdd->device[i];
  1.2552 -      widebuf[i] = 0;
  1.2553 -      RtlStringCbPrintfW(buffer, 512, L"Xen %ws device #%d", widebuf, xppdd->index);
  1.2554 -      KdPrint((__DRIVER_NAME "     %ls\n", buffer));
  1.2555 -      irp->IoStatus.Information = (ULONG_PTR)buffer;
  1.2556 -      status = STATUS_SUCCESS;
  1.2557 -      break;
  1.2558 -    case DeviceTextLocationInformation:
  1.2559 -      KdPrint((__DRIVER_NAME "     DeviceTextLocationInformation\n"));
  1.2560 -      buffer = ExAllocatePoolWithTag(PagedPool, 512, XENPCI_POOL_TAG);
  1.2561 -      RtlStringCbPrintfW(buffer, 512, L"Xen Bus");
  1.2562 -      KdPrint((__DRIVER_NAME "     %ls\n", buffer));
  1.2563 -      irp->IoStatus.Information = (ULONG_PTR)buffer;
  1.2564 -      status = STATUS_SUCCESS;
  1.2565 -      break;
  1.2566 -    default:
  1.2567 -      KdPrint((__DRIVER_NAME "     Unhandled IdType = %d\n", stack->Parameters.QueryDeviceText.DeviceTextType));
  1.2568 -      irp->IoStatus.Information = 0;
  1.2569 -      status = STATUS_NOT_SUPPORTED;
  1.2570 -      break;
  1.2571 -    }
  1.2572 -    break;
  1.2573 -    
  1.2574 -  case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
  1.2575 -    KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_RESOURCE_REQUIREMENTS (status = %08x)\n", irp->IoStatus.Status));
  1.2576 -    status = XenPci_QueryResourceRequirements(device_object, irp);
  1.2577 -    break;
  1.2578 -
  1.2579 -  case IRP_MN_QUERY_CAPABILITIES:
  1.2580 -    KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_CAPABILITIES (status = %08x)\n", irp->IoStatus.Status));
  1.2581 -    status = XenPci_Pnp_QueryCapabilities(device_object, irp);
  1.2582 -    break;
  1.2583 -
  1.2584 -  case IRP_MN_QUERY_BUS_INFORMATION:
  1.2585 -    KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_BUS_INFORMATION (status = %08x)\n", irp->IoStatus.Status));
  1.2586 -    pbi = (PPNP_BUS_INFORMATION)ExAllocatePoolWithTag(PagedPool, sizeof(PNP_BUS_INFORMATION), XENPCI_POOL_TAG);
  1.2587 -    pbi->BusTypeGuid = GUID_BUS_TYPE_XEN;
  1.2588 -    pbi->LegacyBusType = PNPBus; //Internal;
  1.2589 -    pbi->BusNumber = 0;
  1.2590 -    irp->IoStatus.Information = (ULONG_PTR)pbi;
  1.2591 -    status = STATUS_SUCCESS;
  1.2592 -    break;
  1.2593 -
  1.2594 -  case IRP_MN_QUERY_INTERFACE:
  1.2595 -    status = XenPci_QueryInterface(device_object, irp);
  1.2596 -    break;
  1.2597 -    
  1.2598 -  case IRP_MN_QUERY_RESOURCES:
  1.2599 -    KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_RESOURCES (status = %08x)\n", irp->IoStatus.Status));
  1.2600 -    status = irp->IoStatus.Status;
  1.2601 -#if 0    
  1.2602 -    crl = (PCM_RESOURCE_LIST)ExAllocatePoolWithTag(PagedPool, sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) * 2, XENPCI_POOL_TAG);
  1.2603 -    crl->Count = 1;
  1.2604 -    crl->List[0].InterfaceType = PNPBus;
  1.2605 -    crl->List[0].BusNumber = 0;
  1.2606 -    crl->List[0].PartialResourceList.Version = 1;
  1.2607 -    crl->List[0].PartialResourceList.Revision = 1;
  1.2608 -    crl->List[0].PartialResourceList.Count = 0;
  1.2609 -
  1.2610 -    prd = &crl->List[0].PartialResourceList.PartialDescriptors[crl->List[0].PartialResourceList.Count++];
  1.2611 -    prd->Type = CmResourceTypeInterrupt;
  1.2612 -    prd->ShareDisposition = CmResourceShareShared;
  1.2613 -    prd->Flags = (xpdd->irq_mode == Latched)?CM_RESOURCE_INTERRUPT_LATCHED:CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
  1.2614 -    prd->u.Interrupt.Level = xpdd->irq_number;
  1.2615 -    prd->u.Interrupt.Vector = xpdd->irq_number;
  1.2616 -    prd->u.Interrupt.Affinity = (KAFFINITY)-1;
  1.2617 -    
  1.2618 -    prd = &crl->List[0].PartialResourceList.PartialDescriptors[crl->List[0].PartialResourceList.Count++];
  1.2619 -    prd->Type = CmResourceTypeMemory;
  1.2620 -    prd->ShareDisposition = CmResourceShareShared;
  1.2621 -    prd->Flags = CM_RESOURCE_MEMORY_READ_WRITE | CM_RESOURCE_MEMORY_CACHEABLE;
  1.2622 -    prd->u.Memory.Start = xpdd->platform_mmio_addr;
  1.2623 -    prd->u.Memory.Length = 0;
  1.2624 -    
  1.2625 -    irp->IoStatus.Information = (ULONG_PTR)crl;
  1.2626 -    status = STATUS_SUCCESS;
  1.2627 -#endif
  1.2628 -    break;    
  1.2629 -    
  1.2630 -  case IRP_MN_QUERY_PNP_DEVICE_STATE:
  1.2631 -    KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_PNP_DEVICE_STATE (status = %08x)\n", irp->IoStatus.Status));
  1.2632 -    irp->IoStatus.Information = 0;
  1.2633 -    status = STATUS_SUCCESS;
  1.2634 -    break;
  1.2635 -  
  1.2636 -  case IRP_MN_QUERY_DEVICE_RELATIONS:
  1.2637 -    KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_DEVICE_RELATIONS (status = %08x)\n", irp->IoStatus.Status));
  1.2638 -    switch (stack->Parameters.QueryDeviceRelations.Type)
  1.2639 -    {
  1.2640 -    case TargetDeviceRelation:
  1.2641 -      KdPrint((__DRIVER_NAME "     BusRelations\n"));
  1.2642 -      status = XenPci_Pnp_QueryTargetRelations(device_object, irp);
  1.2643 -      break;  
  1.2644 -    default:
  1.2645 -      status = irp->IoStatus.Status;
  1.2646 -      break;
  1.2647 -    }
  1.2648 -    break;
  1.2649 -
  1.2650 -  case IRP_MN_EJECT:
  1.2651 -    KdPrint((__DRIVER_NAME "     IRP_MN_EJECT\n"));
  1.2652 -    status = STATUS_SUCCESS;
  1.2653 -    break;
  1.2654 -      
  1.2655 -  default:
  1.2656 -    //KdPrint((__DRIVER_NAME "     Unhandled Minor = %d, Status = %08x\n", stack->MinorFunction, irp->IoStatus.Status));
  1.2657 -    status = irp->IoStatus.Status;
  1.2658 -    break;
  1.2659 -  }
  1.2660 -
  1.2661 -  irp->IoStatus.Status = status;
  1.2662 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
  1.2663 -
  1.2664 -  //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__"\n"));
  1.2665 -
  1.2666 -  return status;
  1.2667 -}
  1.2668 -
  1.2669 -NTSTATUS
  1.2670 -XenPci_Irp_Create_Pdo(PDEVICE_OBJECT device_object, PIRP irp)
  1.2671 -{
  1.2672 -  NTSTATUS status;
  1.2673 -
  1.2674 -  UNREFERENCED_PARAMETER(device_object);
  1.2675 -
  1.2676 -  FUNCTION_ENTER();
  1.2677 -
  1.2678 -  status = irp->IoStatus.Status;
  1.2679 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
  1.2680 -
  1.2681 -  FUNCTION_EXIT();
  1.2682 -
  1.2683 -  return status;
  1.2684 -}
  1.2685 -
  1.2686 -NTSTATUS
  1.2687 -XenPci_Irp_Close_Pdo(PDEVICE_OBJECT device_object, PIRP irp)
  1.2688 -{
  1.2689 -  NTSTATUS status;
  1.2690 -
  1.2691 -  UNREFERENCED_PARAMETER(device_object);
  1.2692 -
  1.2693 -  FUNCTION_ENTER();
  1.2694 -
  1.2695 -  status = irp->IoStatus.Status;
  1.2696 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
  1.2697 -
  1.2698 -  FUNCTION_EXIT();
  1.2699 -
  1.2700 -  return status;
  1.2701 -}
  1.2702 -
  1.2703 -NTSTATUS
  1.2704 -XenPci_Irp_Read_Pdo(PDEVICE_OBJECT device_object, PIRP irp)
  1.2705 -{
  1.2706 -  NTSTATUS status;
  1.2707 -
  1.2708 -  UNREFERENCED_PARAMETER(device_object);
  1.2709 -
  1.2710 -  FUNCTION_ENTER();
  1.2711 -
  1.2712 -  status = irp->IoStatus.Status;
  1.2713 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
  1.2714 -
  1.2715 -  FUNCTION_EXIT();
  1.2716 -
  1.2717 -  return status;
  1.2718 -}
  1.2719 -
  1.2720 -NTSTATUS
  1.2721 -XenPci_Irp_Write_Pdo(PDEVICE_OBJECT device_object, PIRP irp)
  1.2722 -{
  1.2723 -  NTSTATUS status;
  1.2724 -
  1.2725 -  UNREFERENCED_PARAMETER(device_object);
  1.2726 -
  1.2727 -  FUNCTION_ENTER();
  1.2728 -
  1.2729 -  status = irp->IoStatus.Status;
  1.2730 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
  1.2731 -
  1.2732 -  FUNCTION_EXIT();
  1.2733 -
  1.2734 -  return status;
  1.2735 -}
  1.2736 -
  1.2737 -NTSTATUS
  1.2738 -XenPci_Irp_Cleanup_Pdo(PDEVICE_OBJECT device_object, PIRP irp)
  1.2739 -{
  1.2740 -  NTSTATUS status;
  1.2741 -
  1.2742 -  UNREFERENCED_PARAMETER(device_object);
  1.2743 -
  1.2744 -  FUNCTION_ENTER();
  1.2745 -  
  1.2746 -  status = irp->IoStatus.Status;
  1.2747 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
  1.2748 -  
  1.2749 -  FUNCTION_EXIT();
  1.2750 -
  1.2751 -  return status;
  1.2752 -}
  1.2753 -
  1.2754 -DDKAPI NTSTATUS
  1.2755 -XenPci_SystemControl_Pdo(PDEVICE_OBJECT device_object, PIRP irp)
  1.2756 -{
  1.2757 -  NTSTATUS status;
  1.2758 -
  1.2759 -  UNREFERENCED_PARAMETER(device_object);
  1.2760 -
  1.2761 -  FUNCTION_ENTER();
  1.2762 -  
  1.2763 -  status = irp->IoStatus.Status;
  1.2764 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
  1.2765 -  
  1.2766 -  FUNCTION_EXIT();
  1.2767 -
  1.2768 -  return status;
  1.2769 -}
  1.2770 -#endif