win-pvdrivers

changeset 803:bbc6c94b9621 0.11.0.218

Fix backend state change. Can now detach devices even when in use
Cleanups.
author James Harper <james.harper@bendigoit.com.au>
date Sat Jun 26 23:18:40 2010 +1000 (2010-06-26)
parents 05bab5b47a93
children 6ea80e94e8cf
files xennet/xennet.c xenpci/xenbus.c xenpci/xenpci.h xenpci/xenpci_fdo.c xenpci/xenpci_pdo.c xenscsi/xenscsi.c xenusb/xenusb.h xenusb/xenusb_fdo.c xenvbd/xenvbd.c xenvbd/xenvbd.h
line diff
     1.1 --- a/xennet/xennet.c	Sat Jun 26 23:17:31 2010 +1000
     1.2 +++ b/xennet/xennet.c	Sat Jun 26 23:18:40 2010 +1000
     1.3 @@ -644,8 +644,8 @@ XenNet_Init(
     1.4  
     1.5    ptr = xi->config_page;
     1.6    // two XEN_INIT_TYPE_RUNs means go straight to XenbusStateConnected - skip XenbusStateInitialised
     1.7 -  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
     1.8 -  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
     1.9 +  //ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
    1.10 +  //ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
    1.11    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "tx-ring-ref", NULL, NULL);
    1.12    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "rx-ring-ref", NULL, NULL);
    1.13    #pragma warning(suppress:4054)
    1.14 @@ -661,6 +661,24 @@ XenNet_Init(
    1.15    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-sg", buf, NULL);
    1.16    RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !!xi->config_gso);
    1.17    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-gso-tcpv4", buf, NULL);
    1.18 +  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT, NULL, NULL, NULL);
    1.19 +  __ADD_XEN_INIT_UCHAR(&ptr, 0); /* no pre-connect required */
    1.20 +  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT, NULL, NULL, NULL);
    1.21 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
    1.22 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
    1.23 +  __ADD_XEN_INIT_UCHAR(&ptr, 20);
    1.24 +  __ADD_XEN_INIT_UCHAR(&ptr, 0);
    1.25 +  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN, NULL, NULL, NULL);
    1.26 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
    1.27 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
    1.28 +  __ADD_XEN_INIT_UCHAR(&ptr, 50);
    1.29 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
    1.30 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
    1.31 +  __ADD_XEN_INIT_UCHAR(&ptr, 50);
    1.32 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitialising);
    1.33 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait);
    1.34 +  __ADD_XEN_INIT_UCHAR(&ptr, 50);
    1.35 +  __ADD_XEN_INIT_UCHAR(&ptr, 0);
    1.36    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
    1.37    
    1.38    status = xi->vectors.XenPci_XenConfigDevice(xi->vectors.context);
     2.1 --- a/xenpci/xenbus.c	Sat Jun 26 23:17:31 2010 +1000
     2.2 +++ b/xenpci/xenbus.c	Sat Jun 26 23:18:40 2010 +1000
     2.3 @@ -210,8 +210,6 @@ XenBus_Write(
     2.4    struct xsd_sockmsg *rep;
     2.5    char *msg;
     2.6  
     2.7 -  //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
     2.8 -
     2.9    ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
    2.10  
    2.11    rep = xenbus_format_msg_reply(xpdd, XS_WRITE, xbt, req, ARRAY_SIZE(req));
    2.12 @@ -220,8 +218,6 @@ XenBus_Write(
    2.13      return msg;
    2.14    ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
    2.15  
    2.16 -  //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
    2.17 -
    2.18    return NULL;
    2.19  }
    2.20  
     3.1 --- a/xenpci/xenpci.h	Sat Jun 26 23:17:31 2010 +1000
     3.2 +++ b/xenpci/xenpci.h	Sat Jun 26 23:18:40 2010 +1000
     3.3 @@ -217,6 +217,12 @@ typedef struct {
     3.4  
     3.5  WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_DEVICE_DATA, GetXpdd)
     3.6  
     3.7 +typedef struct {
     3.8 +  UCHAR front_target;
     3.9 +  UCHAR back_expected;
    3.10 +  UCHAR wait; /* units = 100ms */
    3.11 +} XENPCI_STATE_MAP_ELEMENT, *PXENPCI_STATE_MAP_ELEMENT;
    3.12 +
    3.13  typedef struct {  
    3.14    WDFDEVICE wdf_device;
    3.15    WDFDEVICE wdf_device_bus_fdo;
    3.16 @@ -242,8 +248,16 @@ typedef struct {
    3.17    PUCHAR assigned_resources_ptr;
    3.18    XENPCI_DEVICE_STATE device_state;
    3.19    BOOLEAN restart_on_resume;
    3.20 +  BOOLEAN backend_initiated_remove;
    3.21 +  BOOLEAN do_not_enumerate;
    3.22    
    3.23 -  BOOLEAN hiber_usage_kludge;  
    3.24 +  XENPCI_STATE_MAP_ELEMENT xb_pre_connect_map[5];
    3.25 +  XENPCI_STATE_MAP_ELEMENT xb_post_connect_map[5];
    3.26 +  XENPCI_STATE_MAP_ELEMENT xb_shutdown_map[5];
    3.27 +  
    3.28 +  
    3.29 +  
    3.30 +  BOOLEAN hiber_usage_kludge;
    3.31  } XENPCI_PDO_DEVICE_DATA, *PXENPCI_PDO_DEVICE_DATA;
    3.32  
    3.33  WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_PDO_DEVICE_DATA, GetXppdd)
    3.34 @@ -398,6 +412,8 @@ XenPci_PatchKernel(PXENPCI_DEVICE_DATA x
    3.35  
    3.36  NTSTATUS
    3.37  XenPci_HookDbgPrint();
    3.38 +NTSTATUS
    3.39 +XenPci_UnHookDbgPrint();
    3.40  
    3.41  struct xsd_sockmsg *
    3.42  XenBus_Raw(PXENPCI_DEVICE_DATA xpdd, struct xsd_sockmsg *msg);
     4.1 --- a/xenpci/xenpci_fdo.c	Sat Jun 26 23:17:31 2010 +1000
     4.2 +++ b/xenpci/xenpci_fdo.c	Sat Jun 26 23:18:40 2010 +1000
     4.3 @@ -973,6 +973,8 @@ XenPci_EvtChildListScanForChildren(WDFCH
     4.4    CHAR path[128];
     4.5    XENPCI_PDO_IDENTIFICATION_DESCRIPTION child_description;
     4.6    PVOID entry;
     4.7 +  WDFDEVICE child_device;
     4.8 +  WDF_CHILD_RETRIEVE_INFO retrieve_info;
     4.9    
    4.10    FUNCTION_ENTER();
    4.11  
    4.12 @@ -1009,6 +1011,44 @@ XenPci_EvtChildListScanForChildren(WDFCH
    4.13            RtlStringCbCopyA(child_description.path, ARRAY_SIZE(child_description.path), path);
    4.14            RtlStringCbCopyA(child_description.device, ARRAY_SIZE(child_description.device), devices[i]);
    4.15            child_description.index = atoi(instances[j]);
    4.16 +          WDF_CHILD_RETRIEVE_INFO_INIT(&retrieve_info, &child_description.header);
    4.17 +          child_device = WdfChildListRetrievePdo(child_list, &retrieve_info);
    4.18 +          if (child_device)
    4.19 +          {
    4.20 +            PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(child_device);
    4.21 +            char *err;
    4.22 +            char *value;
    4.23 +            char backend_state_path[128];
    4.24 +            
    4.25 +            if (xppdd->do_not_enumerate)
    4.26 +            {
    4.27 +              RtlStringCbPrintfA(backend_state_path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
    4.28 +            
    4.29 +              err = XenBus_Read(xpdd, XBT_NIL, backend_state_path, &value);
    4.30 +              if (err)
    4.31 +              {
    4.32 +                XenPci_FreeMem(err);
    4.33 +                xppdd->backend_state = XenbusStateUnknown;
    4.34 +              }
    4.35 +              else
    4.36 +              {
    4.37 +                xppdd->backend_state = atoi(value);
    4.38 +                XenPci_FreeMem(value);
    4.39 +              }
    4.40 +              if (xppdd->backend_state == XenbusStateClosing || xppdd->backend_state == XenbusStateClosed)
    4.41 +              {
    4.42 +                KdPrint((__DRIVER_NAME "     Surprise removing %s due to backend initiated remove\n", path));
    4.43 +                XenPci_FreeMem(instances[j]);
    4.44 +                continue;
    4.45 +              }
    4.46 +              else
    4.47 +              {
    4.48 +                /* I guess we are being added again ... */
    4.49 +                xppdd->backend_initiated_remove = FALSE;
    4.50 +                xppdd->do_not_enumerate = FALSE;
    4.51 +              }
    4.52 +            }
    4.53 +          }
    4.54            status = WdfChildListAddOrUpdateChildDescriptionAsPresent(child_list, &child_description.header, NULL);
    4.55            if (!NT_SUCCESS(status))
    4.56            {
     5.1 --- a/xenpci/xenpci_pdo.c	Sat Jun 26 23:17:31 2010 +1000
     5.2 +++ b/xenpci/xenpci_pdo.c	Sat Jun 26 23:18:40 2010 +1000
     5.3 @@ -33,6 +33,7 @@ static EVT_WDF_DEVICE_RELEASE_HARDWARE X
     5.4  static EVT_WDF_DEVICE_USAGE_NOTIFICATION XenPciPdo_EvtDeviceUsageNotification;
     5.5  static EVT_WDFDEVICE_WDM_IRP_PREPROCESS XenPciPdo_EvtDeviceWdmIrpPreprocess_START_DEVICE;
     5.6  static EVT_WDF_DEVICE_RESOURCE_REQUIREMENTS_QUERY XenPciPdo_EvtDeviceResourceRequirementsQuery;
     5.7 +static EVT_WDF_DEVICE_PNP_STATE_CHANGE_NOTIFICATION  EvtDevicePnpStateChange;
     5.8  
     5.9  /*
    5.10  Called at PASSIVE_LEVEL(?)
    5.11 @@ -107,7 +108,7 @@ XenPci_UpdateBackendState(PVOID context)
    5.12      ExReleaseFastMutex(&xppdd->backend_state_mutex);
    5.13      return;
    5.14    }
    5.15 -
    5.16 +  
    5.17    xppdd->backend_state = new_backend_state;
    5.18  
    5.19    switch (xppdd->backend_state)
    5.20 @@ -136,6 +137,7 @@ XenPci_UpdateBackendState(PVOID context)
    5.21      KdPrint((__DRIVER_NAME "     Backend State Changed to Closing\n"));
    5.22      if (xppdd->frontend_state != XenbusStateClosing)
    5.23      {
    5.24 +      xppdd->backend_initiated_remove = TRUE;
    5.25        KdPrint((__DRIVER_NAME "     Requesting eject\n"));
    5.26        WdfPdoRequestEject(device);
    5.27      }
    5.28 @@ -514,6 +516,26 @@ XenPci_ChangeFrontendState(WDFDEVICE dev
    5.29  }
    5.30  
    5.31  static NTSTATUS
    5.32 +XenPci_ChangeFrontendStateMap(WDFDEVICE device, PXENPCI_STATE_MAP_ELEMENT map)
    5.33 +{
    5.34 +  NTSTATUS status;
    5.35 +  
    5.36 +  FUNCTION_ENTER();
    5.37 +  while(map->front_target)
    5.38 +  {
    5.39 +    //KdPrint((__DRIVER_NAME "     Changing state to %d expecting %d\n", (ULONG)map->front_target, (ULONG)map->back_expected));
    5.40 +    if (!NT_SUCCESS(status = XenPci_ChangeFrontendState(device, (ULONG)map->front_target, (ULONG)map->back_expected, (ULONG)map->wait * 100)))
    5.41 +    {
    5.42 +      FUNCTION_EXIT();
    5.43 +      return status;
    5.44 +    }
    5.45 +    map++;
    5.46 +  }
    5.47 +  FUNCTION_EXIT();
    5.48 +  return STATUS_SUCCESS;
    5.49 +}
    5.50 +
    5.51 +static NTSTATUS
    5.52  XenPci_XenConfigDevice(WDFDEVICE device);
    5.53  
    5.54  static NTSTATUS
    5.55 @@ -533,11 +555,7 @@ XenPci_XenShutdownDevice(PVOID context)
    5.56  
    5.57    if (xppdd->backend_state == XenbusStateConnected)
    5.58    {
    5.59 -    XenPci_ChangeFrontendState(device, XenbusStateClosing, XenbusStateClosing, 30000);
    5.60 -    if (xppdd->backend_state == XenbusStateClosing)
    5.61 -      XenPci_ChangeFrontendState(device, XenbusStateClosed, XenbusStateClosed, 30000);
    5.62 -    if (xppdd->backend_state == XenbusStateClosed)
    5.63 -      XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 30000);
    5.64 +    XenPci_ChangeFrontendStateMap(device, xppdd->xb_shutdown_map);
    5.65    }
    5.66    else
    5.67    {
    5.68 @@ -599,14 +617,17 @@ XenPci_XenConfigDeviceSpecifyBuffers(WDF
    5.69    PUCHAR out_ptr;
    5.70    XENPCI_VECTORS vectors;
    5.71    ULONG event_channel;
    5.72 -  ULONG run_type = 0;
    5.73    PMDL ring;
    5.74    grant_ref_t gref;
    5.75 -  BOOLEAN done_xenbus_init = FALSE;
    5.76    PVOID value2;
    5.77 +  int map_no;
    5.78   
    5.79    FUNCTION_ENTER();
    5.80  
    5.81 +  xppdd->xb_pre_connect_map[0].front_target = 0;
    5.82 +  xppdd->xb_post_connect_map[0].front_target = 0;
    5.83 +  xppdd->xb_shutdown_map[0].front_target = 0;
    5.84 +  
    5.85    in_ptr = src;
    5.86    out_ptr = dst;
    5.87    
    5.88 @@ -645,27 +666,16 @@ XenPci_XenConfigDeviceSpecifyBuffers(WDF
    5.89    ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_VECTORS, NULL, &vectors, NULL);
    5.90    ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_STATE_PTR, NULL, &xppdd->device_state, NULL);
    5.91    
    5.92 +  // need to make sure we never get here while backend state is connected or closing...
    5.93    // first pass, possibly before state == Connected
    5.94    in_ptr = src;
    5.95    while((type = GET_XEN_INIT_REQ(&in_ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
    5.96    {
    5.97 -    if (!done_xenbus_init)
    5.98 -    {
    5.99 -      if (XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 2000) != STATUS_SUCCESS)
   5.100 -      {
   5.101 -        status = STATUS_UNSUCCESSFUL;
   5.102 -        goto error;
   5.103 -      }
   5.104 -      done_xenbus_init = TRUE;
   5.105 -    }
   5.106 -    
   5.107 +//KdPrint((__DRIVER_NAME "     in_ptr = %p, type = %d\n", in_ptr, type));
   5.108      ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, type, setting, value, value2);
   5.109  
   5.110      switch (type)
   5.111      {
   5.112 -    case XEN_INIT_TYPE_RUN:
   5.113 -      run_type++;
   5.114 -      break;
   5.115      case XEN_INIT_TYPE_WRITE_STRING: /* frontend setting = value */
   5.116        //KdPrint((__DRIVER_NAME "     XEN_INIT_TYPE_WRITE_STRING - %s = %s\n", setting, value));
   5.117        RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
   5.118 @@ -676,12 +686,13 @@ XenPci_XenConfigDeviceSpecifyBuffers(WDF
   5.119        if ((ring = AllocatePage()) != 0)
   5.120        {
   5.121          address = MmGetMdlVirtualAddress(ring);
   5.122 -        //KdPrint((__DRIVER_NAME "     XEN_INIT_TYPE_RING - %s = %p\n", setting, address));
   5.123 +        KdPrint((__DRIVER_NAME "     XEN_INIT_TYPE_RING - %s = %p\n", setting, address));
   5.124          SHARED_RING_INIT((struct dummy_sring *)address);
   5.125          if ((gref = GntTbl_GrantAccess(
   5.126            xpdd, 0, (ULONG)*MmGetMdlPfnArray(ring), FALSE, INVALID_GRANT_REF, (ULONG)'XPDO')) != INVALID_GRANT_REF)
   5.127          {
   5.128            RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/%s", xppdd->path, setting);
   5.129 +          KdPrint((__DRIVER_NAME "     XEN_INIT_TYPE_RING - %s = %d\n", setting, gref));
   5.130            XenBus_Printf(xpdd, XBT_NIL, path, "%d", gref);
   5.131            ADD_XEN_INIT_RSP(&out_ptr, type, setting, address, NULL);
   5.132            ADD_XEN_INIT_RSP(&xppdd->assigned_resources_ptr, type, setting, ring, NULL);
   5.133 @@ -734,20 +745,56 @@ XenPci_XenConfigDeviceSpecifyBuffers(WDF
   5.134          goto error;
   5.135        }
   5.136        break;
   5.137 +    case XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT:
   5.138 +      map_no = 0;
   5.139 +      while ((xppdd->xb_pre_connect_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
   5.140 +      {
   5.141 +         xppdd->xb_pre_connect_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
   5.142 +         xppdd->xb_pre_connect_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
   5.143 +         __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].front_target);
   5.144 +         __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].back_expected);
   5.145 +         __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_pre_connect_map[map_no].wait);
   5.146 +         map_no++;
   5.147 +      }
   5.148 +      __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
   5.149 +      break;
   5.150 +    case XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT:
   5.151 +      map_no = 0;
   5.152 +      while ((xppdd->xb_post_connect_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
   5.153 +      {
   5.154 +         xppdd->xb_post_connect_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
   5.155 +         xppdd->xb_post_connect_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
   5.156 +         __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].front_target);
   5.157 +         __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].back_expected);
   5.158 +         __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_post_connect_map[map_no].wait);
   5.159 +         map_no++;
   5.160 +      }
   5.161 +      __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
   5.162 +      break;
   5.163 +    case XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN:
   5.164 +      map_no = 0;
   5.165 +      while ((xppdd->xb_shutdown_map[map_no].front_target = __GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
   5.166 +      {
   5.167 +         xppdd->xb_shutdown_map[map_no].back_expected = __GET_XEN_INIT_UCHAR(&in_ptr);
   5.168 +         xppdd->xb_shutdown_map[map_no].wait = __GET_XEN_INIT_UCHAR(&in_ptr);
   5.169 +         __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].front_target);
   5.170 +         __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].back_expected);
   5.171 +         __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, xppdd->xb_shutdown_map[map_no].wait);
   5.172 +         map_no++;
   5.173 +      }
   5.174 +      __ADD_XEN_INIT_UCHAR(&xppdd->requested_resources_ptr, 0);
   5.175 +      break;
   5.176      }
   5.177    }
   5.178    if (!NT_SUCCESS(status))
   5.179    {
   5.180      goto error;
   5.181    }
   5.182 -  // If XEN_INIT_TYPE_RUN was specified more than once then we skip XenbusStateInitialised here and go straight to XenbusStateConnected at the end
   5.183 -  if (run_type == 1)
   5.184 +
   5.185 +  if (XenPci_ChangeFrontendStateMap(device, xppdd->xb_pre_connect_map) != STATUS_SUCCESS)
   5.186    {
   5.187 -    if (XenPci_ChangeFrontendState(device, XenbusStateInitialised, XenbusStateConnected, 2000) != STATUS_SUCCESS)
   5.188 -    {
   5.189 -      status = STATUS_UNSUCCESSFUL;
   5.190 -      goto error;
   5.191 -    }
   5.192 +    status = STATUS_UNSUCCESSFUL;
   5.193 +    goto error;
   5.194    }
   5.195  
   5.196    // second pass, possibly after state == Connected
   5.197 @@ -792,6 +839,16 @@ XenPci_XenConfigDeviceSpecifyBuffers(WDF
   5.198          __ADD_XEN_INIT_ULONG(&xppdd->assigned_resources_ptr, gref);
   5.199        }
   5.200        break;
   5.201 +    /* just need to eat these */
   5.202 +    case XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT:
   5.203 +    case XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT:
   5.204 +    case XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN:
   5.205 +      while ((__GET_XEN_INIT_UCHAR(&in_ptr)) != 0)
   5.206 +      {
   5.207 +         __GET_XEN_INIT_UCHAR(&in_ptr);
   5.208 +         __GET_XEN_INIT_UCHAR(&in_ptr);
   5.209 +      }
   5.210 +      break;
   5.211      }
   5.212    }
   5.213    if (qemu_hide_flags_value)
   5.214 @@ -821,18 +878,16 @@ XenPci_XenConfigDeviceSpecifyBuffers(WDF
   5.215    
   5.216    ADD_XEN_INIT_RSP(&out_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
   5.217  
   5.218 -  if (run_type)
   5.219 +  if (XenPci_ChangeFrontendStateMap(device, xppdd->xb_post_connect_map) != STATUS_SUCCESS)
   5.220    {
   5.221 -    if (XenPci_ChangeFrontendState(device, XenbusStateConnected, XenbusStateConnected, 2000) != STATUS_SUCCESS)
   5.222 -    {
   5.223 -      status = STATUS_UNSUCCESSFUL;
   5.224 -      goto error;
   5.225 -    }
   5.226 +    status = STATUS_UNSUCCESSFUL;
   5.227 +    goto error;
   5.228    }
   5.229    FUNCTION_EXIT();
   5.230    return status;
   5.231  
   5.232  error:
   5.233 +  KdPrint((__DRIVER_NAME "     Error\n"));
   5.234    XenPci_ChangeFrontendState(device, XenbusStateInitialising, XenbusStateInitWait, 2000);
   5.235    FUNCTION_EXIT_STATUS(status);
   5.236    return status;
   5.237 @@ -1198,6 +1253,30 @@ XenPciPdo_EvtDeviceUsageNotification(WDF
   5.238    FUNCTION_EXIT();
   5.239  }
   5.240  
   5.241 +static NTSTATUS
   5.242 +XenPci_EvtDevicePnpStateChange(WDFDEVICE device, PCWDF_DEVICE_PNP_NOTIFICATION_DATA notification_data)
   5.243 +{
   5.244 +  PXENPCI_PDO_DEVICE_DATA xppdd = GetXppdd(device);
   5.245 +  
   5.246 +  //FUNCTION_ENTER();
   5.247 +  
   5.248 +  if (xppdd->backend_initiated_remove
   5.249 +    && notification_data->Type == StateNotificationEnterState
   5.250 +    && notification_data->Data.EnterState.CurrentState == WdfDevStatePnpQueryRemovePending 
   5.251 +    && notification_data->Data.EnterState.NewState == WdfDevStatePnpQueryCanceled)
   5.252 +  {
   5.253 +    PXENPCI_DEVICE_DATA xpdd = GetXpdd(xppdd->wdf_device_bus_fdo);
   5.254 +    
   5.255 +    KdPrint((__DRIVER_NAME "     Eject failed, doing surprise removal\n"));
   5.256 +    xppdd->do_not_enumerate = TRUE;
   5.257 +    XenPci_EvtChildListScanForChildren(xpdd->child_list);
   5.258 +  }
   5.259 +  
   5.260 +  //FUNCTION_EXIT();
   5.261 +  
   5.262 +  return STATUS_SUCCESS;
   5.263 +}
   5.264 +
   5.265  NTSTATUS
   5.266  XenPci_EvtChildListCreateDevice(WDFCHILDLIST child_list,
   5.267    PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER identification_header,
   5.268 @@ -1283,6 +1362,8 @@ XenPci_EvtChildListCreateDevice(WDFCHILD
   5.269    WdfPdoInitSetDefaultLocale(child_init, 0x0409);
   5.270  
   5.271    WdfDeviceInitSetPowerNotPageable(child_init);
   5.272 +
   5.273 +  WdfDeviceInitRegisterPnpStateChangeCallback(child_init, WdfDevStatePnpQueryCanceled, XenPci_EvtDevicePnpStateChange, StateNotificationEnterState);
   5.274    
   5.275    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&child_attributes, XENPCI_PDO_DEVICE_DATA);
   5.276    status = WdfDeviceCreate(&child_init, &child_attributes, &child_device);
     6.1 --- a/xenscsi/xenscsi.c	Sat Jun 26 23:17:31 2010 +1000
     6.2 +++ b/xenscsi/xenscsi.c	Sat Jun 26 23:18:40 2010 +1000
     6.3 @@ -417,7 +417,7 @@ XenScsi_HwScsiFindAdapter(PVOID DeviceEx
     6.4    UNREFERENCED_PARAMETER(ArgumentString);
     6.5    UNREFERENCED_PARAMETER(Reserved3);
     6.6  
     6.7 -  KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));  
     6.8 +  FUNCTION_ENTER();
     6.9    KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
    6.10    
    6.11    xsdd->scsiport_paused = TRUE; /* wait for initial scan */
    6.12 @@ -504,16 +504,13 @@ XenScsi_HwScsiFindAdapter(PVOID DeviceEx
    6.13        break;
    6.14      }
    6.15    }
    6.16 -#if 0
    6.17 -  if (xsdd->device_type == XENSCSI_DEVICETYPE_UNKNOWN
    6.18 -    || sring == NULL
    6.19 -    || xsdd->event_channel == 0)
    6.20 +
    6.21 +  if (sring == NULL || xsdd->event_channel == 0)
    6.22    {
    6.23      KdPrint((__DRIVER_NAME "     Missing settings\n"));
    6.24      KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
    6.25      return SP_RETURN_BAD_CONFIG;
    6.26    }
    6.27 -#endif
    6.28    
    6.29    ConfigInfo->ScatterGather = TRUE;
    6.30    ConfigInfo->NumberOfPhysicalBreaks = VSCSIIF_SG_TABLESIZE - 1;
    6.31 @@ -555,7 +552,7 @@ XenScsi_HwScsiFindAdapter(PVOID DeviceEx
    6.32      xsdd->vectors.XenBus_AddWatch(xsdd->vectors.context, XBT_NIL, path,
    6.33        XenScsi_DevWatch, xsdd);
    6.34    }
    6.35 -  KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
    6.36 +  FUNCTION_EXIT();
    6.37  
    6.38    return SP_RETURN_FOUND;
    6.39  }
    6.40 @@ -687,7 +684,7 @@ XenScsi_HwScsiStartIo(PVOID DeviceExtens
    6.41    {
    6.42      Srb->SrbStatus = SRB_STATUS_NO_DEVICE;
    6.43      ScsiPortNotification(RequestComplete, DeviceExtension, Srb);
    6.44 -    KdPrint((__DRIVER_NAME "     Out of bounds\n"));
    6.45 +    //KdPrint((__DRIVER_NAME "     Out of bounds\n"));
    6.46      ScsiPortNotification(NextRequest, DeviceExtension);
    6.47      //FUNCTION_EXIT();
    6.48      return TRUE;
    6.49 @@ -829,10 +826,30 @@ DriverEntry(PDRIVER_OBJECT DriverObject,
    6.50  
    6.51    IoAllocateDriverObjectExtension(DriverObject, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC), PAGE_SIZE, &driver_extension);
    6.52    ptr = driver_extension;
    6.53 -  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
    6.54    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "ring-ref", NULL, NULL);
    6.55    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_IRQ, "event-channel", NULL, NULL);
    6.56    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_GRANT_ENTRIES, NULL, UlongToPtr(144), NULL);
    6.57 +  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT, NULL, NULL, NULL);
    6.58 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitialised);
    6.59 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
    6.60 +  __ADD_XEN_INIT_UCHAR(&ptr, 20);
    6.61 +  __ADD_XEN_INIT_UCHAR(&ptr, 0);
    6.62 +  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT, NULL, NULL, NULL);
    6.63 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
    6.64 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
    6.65 +  __ADD_XEN_INIT_UCHAR(&ptr, 20);
    6.66 +  __ADD_XEN_INIT_UCHAR(&ptr, 0);
    6.67 +  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN, NULL, NULL, NULL);
    6.68 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
    6.69 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
    6.70 +  __ADD_XEN_INIT_UCHAR(&ptr, 50);
    6.71 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
    6.72 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
    6.73 +  __ADD_XEN_INIT_UCHAR(&ptr, 50);
    6.74 +  //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait); //ialising);
    6.75 +  //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait);
    6.76 +  //__ADD_XEN_INIT_UCHAR(&ptr, 50);
    6.77 +  __ADD_XEN_INIT_UCHAR(&ptr, 0);
    6.78    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
    6.79    /* RegistryPath == NULL when we are invoked as a crash dump driver */
    6.80    if (!RegistryPath)
     7.1 --- a/xenusb/xenusb.h	Sat Jun 26 23:17:31 2010 +1000
     7.2 +++ b/xenusb/xenusb.h	Sat Jun 26 23:18:40 2010 +1000
     7.3 @@ -44,6 +44,7 @@ Foundation, Inc., 51 Franklin Street, Fi
     7.4  #include <xen_windows.h>
     7.5  #include <io/ring.h>
     7.6  #include <io/usbif.h>
     7.7 +#include <io/xenbus.h>
     7.8  #include <usb.h>
     7.9  #include <usbioctl.h>
    7.10  #include <usbdlib.h>
     8.1 --- a/xenusb/xenusb_fdo.c	Sat Jun 26 23:17:31 2010 +1000
     8.2 +++ b/xenusb/xenusb_fdo.c	Sat Jun 26 23:18:40 2010 +1000
     8.3 @@ -600,12 +600,30 @@ f78e2ddc 8088e092 8088037e 00000001 0000
     8.4    status = XenUsb_StartXenbusInit(xudd);
     8.5  
     8.6    ptr = xudd->config_page;
     8.7 -  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
     8.8 -  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
     8.9 +  //ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
    8.10 +  //ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
    8.11    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "urb-ring-ref", NULL, NULL);
    8.12    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "conn-ring-ref", NULL, NULL);
    8.13    #pragma warning(suppress:4054)
    8.14    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_DPC, "event-channel", (PVOID)XenUsb_HandleEvent, xudd);
    8.15 +  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT, NULL, NULL, NULL);
    8.16 +  __ADD_XEN_INIT_UCHAR(&ptr, 0); /* no pre-connect required */
    8.17 +  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT, NULL, NULL, NULL);
    8.18 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
    8.19 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
    8.20 +  __ADD_XEN_INIT_UCHAR(&ptr, 20);
    8.21 +  __ADD_XEN_INIT_UCHAR(&ptr, 0);
    8.22 +  ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN, NULL, NULL, NULL);
    8.23 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
    8.24 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
    8.25 +  __ADD_XEN_INIT_UCHAR(&ptr, 50);
    8.26 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
    8.27 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
    8.28 +  __ADD_XEN_INIT_UCHAR(&ptr, 50);
    8.29 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitialising);
    8.30 +  __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait);
    8.31 +  __ADD_XEN_INIT_UCHAR(&ptr, 50);
    8.32 +  __ADD_XEN_INIT_UCHAR(&ptr, 0);
    8.33    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
    8.34    status = xudd->vectors.XenPci_XenConfigDevice(xudd->vectors.context);
    8.35  
     9.1 --- a/xenvbd/xenvbd.c	Sat Jun 26 23:17:31 2010 +1000
     9.2 +++ b/xenvbd/xenvbd.c	Sat Jun 26 23:18:40 2010 +1000
     9.3 @@ -1622,7 +1622,7 @@ DriverEntry(PDRIVER_OBJECT DriverObject,
     9.4    {
     9.5      IoAllocateDriverObjectExtension(DriverObject, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC), PAGE_SIZE, &driver_extension);
     9.6      ptr = driver_extension;
     9.7 -    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
     9.8 +    //ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
     9.9      ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "ring-ref", NULL, NULL);
    9.10      ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_IRQ, "event-channel", NULL, NULL);
    9.11      ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_FRONT, "device-type", NULL, NULL);
    9.12 @@ -1630,6 +1630,28 @@ DriverEntry(PDRIVER_OBJECT DriverObject,
    9.13      ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sectors", NULL, NULL);
    9.14      ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sector-size", NULL, NULL);
    9.15      ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_GRANT_ENTRIES, NULL, ULongToPtr(BLKIF_MAX_SEGMENTS_PER_REQUEST), NULL); /* for use in crash dump */
    9.16 +    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT, NULL, NULL, NULL);
    9.17 +    __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
    9.18 +    __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
    9.19 +    __ADD_XEN_INIT_UCHAR(&ptr, 20);
    9.20 +    __ADD_XEN_INIT_UCHAR(&ptr, 0);
    9.21 +    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT, NULL, NULL, NULL);
    9.22 +    //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
    9.23 +    //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
    9.24 +    //__ADD_XEN_INIT_UCHAR(&ptr, 20);
    9.25 +    __ADD_XEN_INIT_UCHAR(&ptr, 0);
    9.26 +    ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN, NULL, NULL, NULL);
    9.27 +    __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
    9.28 +    __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
    9.29 +    __ADD_XEN_INIT_UCHAR(&ptr, 50);
    9.30 +    __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
    9.31 +    __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
    9.32 +    __ADD_XEN_INIT_UCHAR(&ptr, 50);
    9.33 +    __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitialising);
    9.34 +    __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait);
    9.35 +    __ADD_XEN_INIT_UCHAR(&ptr, 50);
    9.36 +    __ADD_XEN_INIT_UCHAR(&ptr, 0);
    9.37 +    
    9.38      ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
    9.39  
    9.40      InitializeObjectAttributes(&oa, RegistryPath, OBJ_CASE_INSENSITIVE, NULL, NULL);
    10.1 --- a/xenvbd/xenvbd.h	Sat Jun 26 23:17:31 2010 +1000
    10.2 +++ b/xenvbd/xenvbd.h	Sat Jun 26 23:18:40 2010 +1000
    10.3 @@ -43,6 +43,7 @@ Foundation, Inc., 51 Franklin Street, Fi
    10.4  #include <xen_public.h>
    10.5  #include <io/ring.h>
    10.6  #include <io/blkif.h>
    10.7 +#include <io/xenbus.h>
    10.8  
    10.9  #define XENVBD_POOL_TAG (ULONG) 'XVBD'
   10.10