win-pvdrivers

changeset 49:4f37f90fc704

Appears to be working now...
author James Harper <james.harper@bendigoit.com.au>
date Sun Dec 16 17:44:31 2007 +1100 (2007-12-16)
parents a70cbf588756
children 94d0f2823b42 70ab0938d3db
files xenvbd/sources xenvbd/xenvbd.c xenvbd/xenvbd.h
line diff
     1.1 --- a/xenvbd/sources	Sat Dec 15 15:42:55 2007 +1100
     1.2 +++ b/xenvbd/sources	Sun Dec 16 17:44:31 2007 +1100
     1.3 @@ -1,7 +1,7 @@
     1.4  TARGETNAME=XENVBD
     1.5  TARGETTYPE=DRIVER
     1.6  TARGETPATH=..\Target\$(DDK_TARGET_OS)
     1.7 -VERSION=0.4.0.117
     1.8 +VERSION=0.4.0.122
     1.9  KMDF_VERSION=1
    1.10  MSC_WARNING_LEVEL=/W4
    1.11  INF_NAME=xenvbd
     2.1 --- a/xenvbd/xenvbd.c	Sat Dec 15 15:42:55 2007 +1100
     2.2 +++ b/xenvbd/xenvbd.c	Sun Dec 16 17:44:31 2007 +1100
     2.3 @@ -96,7 +96,7 @@ AllocatePages(int Pages)
     2.4    PMDL Mdl;
     2.5    PVOID Buf;
     2.6  
     2.7 -  KdPrint((__DRIVER_NAME " --- AllocatePages IRQL = %d\n", KeGetCurrentIrql()));
     2.8 +  //KdPrint((__DRIVER_NAME " --- AllocatePages IRQL = %d\n", KeGetCurrentIrql()));
     2.9    Buf = ExAllocatePoolWithTag(NonPagedPool, Pages * PAGE_SIZE, XENVBD_POOL_TAG);
    2.10    if (Buf == NULL)
    2.11    {
    2.12 @@ -122,7 +122,7 @@ static VOID
    2.13  FreePages(PMDL Mdl)
    2.14  {
    2.15    PVOID Buf = MmGetMdlVirtualAddress(Mdl);
    2.16 -  KdPrint((__DRIVER_NAME " --- FreePages IRQL = %d\n", KeGetCurrentIrql()));
    2.17 +  //KdPrint((__DRIVER_NAME " --- FreePages IRQL = %d\n", KeGetCurrentIrql()));
    2.18    //KdPrint((__DRIVER_NAME "     FreePages Failed at IoAllocateMdl\n"));
    2.19    //KdPrint((__DRIVER_NAME "     FreePages Buf = %08x\n", Buf));
    2.20    IoFreeMdl(Mdl);
    2.21 @@ -137,6 +137,7 @@ XenVbd_HwScsiFindAdapter(PVOID DeviceExt
    2.22    PACCESS_RANGE AccessRange;
    2.23    PXENVBD_DEVICE_DATA DeviceData = (PXENVBD_DEVICE_DATA)DeviceExtension;
    2.24  
    2.25 +  KeInitializeSpinLock(&DeviceData->Lock);
    2.26    KdPrint((__DRIVER_NAME " --> HwScsiFindAdapter\n"));
    2.27    KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
    2.28  
    2.29 @@ -152,8 +153,6 @@ XenVbd_HwScsiFindAdapter(PVOID DeviceExt
    2.30      switch (i)
    2.31      {
    2.32      case 0:
    2.33 -      //DeviceData->ScsiData = ScsiPortGetDeviceBase(DeviceExtension, ConfigInfo->AdapterInterfaceType, ConfigInfo->SystemIoBusNumber, AccessRange->RangeStart, PAGE_SIZE, FALSE);
    2.34 -      //DeviceData->ScsiData = MmMapIoSpace(AccessRange->RangeStart, PAGE_SIZE, MmCached);
    2.35        DeviceData->XenDeviceData = (PXENPCI_XEN_DEVICE_DATA)AccessRange->RangeStart.QuadPart;
    2.36        KdPrint((__DRIVER_NAME "     Mapped to virtual address %08x\n", DeviceData->XenDeviceData));
    2.37        KdPrint((__DRIVER_NAME "     Magic = %08x\n", DeviceData->XenDeviceData->Magic));
    2.38 @@ -243,24 +242,25 @@ XenVbd_HwScsiInterruptTarget(PVOID Devic
    2.39      {
    2.40        rep = RING_GET_RESPONSE(&TargetData->Ring, i);
    2.41        Srb = TargetData->shadow[rep->id].Srb;
    2.42 +      BlockCount = (Srb->Cdb[7] << 8) | Srb->Cdb[8];
    2.43  
    2.44 -//      KdPrint((__DRIVER_NAME "     Response Id = %d\n", (int)rep->id));
    2.45 -
    2.46 -      if (rep->status != BLKIF_RSP_OKAY)
    2.47 +      if (rep->status == BLKIF_RSP_OKAY)
    2.48 +        Srb->SrbStatus = SRB_STATUS_SUCCESS;
    2.49 +      else
    2.50        {
    2.51          KdPrint((__DRIVER_NAME "     Xen Operation returned error\n"));
    2.52 +        if (Srb->Cdb[0] == SCSIOP_READ)
    2.53 +          KdPrint((__DRIVER_NAME "     Operation = Read\n"));
    2.54 +        else
    2.55 +          KdPrint((__DRIVER_NAME "     Operation = Write\n"));     
    2.56 +        KdPrint((__DRIVER_NAME "     Sector = %08X, Count = %d\n", TargetData->shadow[rep->id].req.sector_number, BlockCount));
    2.57 +        Srb->SrbStatus = SRB_STATUS_ERROR;
    2.58        }
    2.59        for (j = 0; j < TargetData->shadow[rep->id].req.nr_segments; j++)
    2.60 -      {
    2.61          DeviceData->XenDeviceData->GntTblInterface.EndAccess(TargetData->shadow[rep->id].req.seg[j].gref);
    2.62 -      }
    2.63 -      BlockCount = (Srb->Cdb[7] << 8) | Srb->Cdb[8];
    2.64        if (Srb->Cdb[0] == SCSIOP_READ)
    2.65          memcpy(Srb->DataBuffer, TargetData->shadow[rep->id].Buf, BlockCount * TargetData->BytesPerSector);
    2.66  
    2.67 -      //FreePages(TargetData->shadow[rep->id].Mdl);
    2.68 -      Srb->SrbStatus = SRB_STATUS_SUCCESS;
    2.69 -
    2.70        ScsiPortNotification(RequestComplete, DeviceData, Srb);
    2.71  
    2.72        ADD_ID_TO_FREELIST(TargetData, rep->id);
    2.73 @@ -326,6 +326,7 @@ XenVbd_BackEndStateHandler(char *Path, P
    2.74    int i;
    2.75  
    2.76    KdPrint((__DRIVER_NAME " --> BackEndStateHandler\n"));
    2.77 +  KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
    2.78  
    2.79    TargetData = (PXENVBD_TARGET_DATA)Data;
    2.80    DeviceData = (PXENVBD_DEVICE_DATA)TargetData->DeviceData;
    2.81 @@ -445,24 +446,6 @@ XenVbd_BackEndStateHandler(char *Path, P
    2.82  // now ask windows to rescan the scsi bus...
    2.83      DeviceData->BusChangePending = 1;
    2.84  
    2.85 -/*
    2.86 -    KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
    2.87 -    OldIrql = KeRaiseIrqlToDpcLevel();
    2.88 -    KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
    2.89 -    ScsiPortNotification(BusChangeDetected, TargetData->DeviceData, 0);
    2.90 -    KeLowerIrql(OldIrql);
    2.91 -    KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
    2.92 -*/
    2.93 -/*
    2.94 -    // if we detected something wrong, we should not enumerate the device and should instead initiate a close
    2.95 -
    2.96 -    status = WdfChildListAddOrUpdateChildDescriptionAsPresent(WdfFdoGetDefaultChildList(GlobalDevice), &Description.Header, NULL);
    2.97 -    if (!NT_SUCCESS(status))
    2.98 -    {
    2.99 -      KdPrint((__DRIVER_NAME "     WdfChildListAddOrUpdateChildDescriptionAsPresent failed %08x\n", status));
   2.100 -    } 
   2.101 -*/
   2.102 -
   2.103      RtlStringCbCopyA(TmpPath, 128, TargetData->Path);
   2.104      RtlStringCbCatA(TmpPath, 128, "/state");
   2.105      DeviceData->XenDeviceData->XenBusInterface.Printf(XBT_NIL, TmpPath, "%d", XenbusStateConnected);
   2.106 @@ -498,8 +481,9 @@ XenVbd_WatchHandler(char *Path, PVOID De
   2.107    int Count;
   2.108    char TmpPath[128];
   2.109    char *Value;
   2.110 -  int VacantBus, VacantTarget;
   2.111 -  PXENVBD_TARGET_DATA TargetData;
   2.112 +  int CurrentBus, CurrentTarget;
   2.113 +  PXENVBD_TARGET_DATA TargetData, VacantTarget;
   2.114 +  KIRQL OldIrql;
   2.115    int i, j;
   2.116  
   2.117    KdPrint((__DRIVER_NAME " --> WatchHandler (DeviceData = %08x)\n", DeviceData));
   2.118 @@ -518,79 +502,48 @@ XenVbd_WatchHandler(char *Path, PVOID De
   2.119    case 4:
   2.120      if (strcmp(Bits[3], "state") != 0) // we only care when the state appears
   2.121        break;
   2.122 -//    for (DeviceData = (PXENVBD_DEVICE_DATA)DeviceListHead.Flink; DeviceData != (PXENVBD_CHILD_DEVICE_DATA)&DeviceListHead; DeviceData = (PXENVBD_CHILD_DEVICE_DATA)DeviceData->Entry.Flink)
   2.123 -//    {
   2.124 -//    VacantBus = -1;
   2.125 -//    VacantTarget = -1;
   2.126 -    TargetData = NULL;
   2.127 -//    VacantTarget = NULL;
   2.128 -    
   2.129 -    for (i = 0; i < SCSI_BUSES; i++)
   2.130 -    {
   2.131 -      for (j = 0; j < SCSI_TARGETS_PER_BUS; j++)
   2.132 -      {
   2.133 -        if (j == 7) // don't use 7 - it would be for the controller
   2.134 -          continue;
   2.135 -        TargetData = &DeviceData->BusData[i].TargetData[j];
   2.136 -        if (TargetData->Present && strncmp(TargetData->Path, Path, strlen(TargetData->Path)) == 0 && Path[strlen(TargetData->Path)] == '/')
   2.137 -        {
   2.138 -          FreeSplitString(Bits, Count);
   2.139 -          KdPrint((__DRIVER_NAME " <-- WatchHandler (Already exists)\n"));
   2.140 -          return;
   2.141 -        }
   2.142 -      }
   2.143 -    }
   2.144 -    for (i = 0; i < SCSI_BUSES; i++)
   2.145 +
   2.146 +    KeAcquireSpinLock(&DeviceData->Lock, &OldIrql);
   2.147 +
   2.148 +    for (VacantTarget = NULL,i = 0; i < SCSI_BUSES * SCSI_TARGETS_PER_BUS; i++)
   2.149      {
   2.150 -      for (j = 0; j < SCSI_TARGETS_PER_BUS; j++)
   2.151 -      {
   2.152 -        if (j == 7) // don't use 7 - it would be for the controller
   2.153 -          continue;
   2.154 -        TargetData = &DeviceData->BusData[i].TargetData[j];
   2.155 -        if (!TargetData->Present)
   2.156 -        {
   2.157 -KdPrint((__DRIVER_NAME "     A\n"));    
   2.158 -          TargetData->Present = 1;
   2.159 -          RtlStringCbCopyA(TargetData->Path, 128, Bits[0]);
   2.160 -          RtlStringCbCatA(TargetData->Path, 128, "/");
   2.161 -          RtlStringCbCatA(TargetData->Path, 128, Bits[1]);
   2.162 -          RtlStringCbCatA(TargetData->Path, 128, "/");
   2.163 -          RtlStringCbCatA(TargetData->Path, 128, Bits[2]);
   2.164 -
   2.165 -KdPrint((__DRIVER_NAME "     B\n"));    
   2.166 -
   2.167 -          TargetData->DeviceIndex = atoi(Bits[2]);
   2.168 -
   2.169 -KdPrint((__DRIVER_NAME "     C\n"));
   2.170 +      CurrentBus = i / SCSI_TARGETS_PER_BUS;
   2.171 +      CurrentTarget = i % SCSI_TARGETS_PER_BUS;
   2.172 +      if (CurrentTarget == 7) // don't use 7 - it would be for the controller
   2.173 +        continue;
   2.174 +      TargetData = &DeviceData->BusData[CurrentBus].TargetData[CurrentTarget];
   2.175 +      if (TargetData->Present && strncmp(TargetData->Path, Path, strlen(TargetData->Path)) == 0 && Path[strlen(TargetData->Path)] == '/')
   2.176 +        break; // already exists
   2.177 +      else if (!TargetData->Present && VacantTarget == NULL)
   2.178 +        VacantTarget = TargetData;
   2.179 +    }
   2.180 +    if (i == SCSI_BUSES * SCSI_TARGETS_PER_BUS && VacantTarget != NULL)
   2.181 +    {
   2.182 +      VacantTarget->Present = 1;
   2.183 +      KeReleaseSpinLock(&DeviceData->Lock, OldIrql);
   2.184  
   2.185 -          RtlStringCbCopyA(TmpPath, 128, TargetData->Path);
   2.186 -          RtlStringCbCatA(TmpPath, 128, "/backend");
   2.187 -          DeviceData->XenDeviceData->XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
   2.188 -          if (Value == NULL)
   2.189 -          {
   2.190 -            KdPrint((__DRIVER_NAME "     Read Failed\n"));
   2.191 -          }
   2.192 -          else
   2.193 -          {
   2.194 -            RtlStringCbCopyA(TargetData->BackendPath, 128, Value);
   2.195 -          }
   2.196 -          RtlStringCbCopyA(TmpPath, 128, TargetData->BackendPath);
   2.197 -          RtlStringCbCatA(TmpPath, 128, "/state");
   2.198 +      RtlStringCbCopyA(VacantTarget->Path, 128, Bits[0]);
   2.199 +      RtlStringCbCatA(VacantTarget->Path, 128, "/");
   2.200 +      RtlStringCbCatA(VacantTarget->Path, 128, Bits[1]);
   2.201 +      RtlStringCbCatA(VacantTarget->Path, 128, "/");
   2.202 +      RtlStringCbCatA(VacantTarget->Path, 128, Bits[2]);
   2.203  
   2.204 -KdPrint((__DRIVER_NAME "     D\n"));
   2.205 -
   2.206 -          DeviceData->XenDeviceData->XenBusInterface.AddWatch(XBT_NIL, TmpPath, XenVbd_BackEndStateHandler, TargetData);
   2.207 -
   2.208 -KdPrint((__DRIVER_NAME "     E\n"));
   2.209 +      VacantTarget->DeviceIndex = atoi(Bits[2]);
   2.210  
   2.211 -          FreeSplitString(Bits, Count);
   2.212 -
   2.213 -          KdPrint((__DRIVER_NAME " <-- WatchHandler (Added a new target)\n"));
   2.214 +      RtlStringCbCopyA(TmpPath, 128, VacantTarget->Path);
   2.215 +      RtlStringCbCatA(TmpPath, 128, "/backend");
   2.216 +      DeviceData->XenDeviceData->XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
   2.217 +      if (Value == NULL)
   2.218 +        KdPrint((__DRIVER_NAME "     Read Failed\n"));
   2.219 +      else
   2.220 +        RtlStringCbCopyA(VacantTarget->BackendPath, 128, Value);
   2.221 +      RtlStringCbCopyA(TmpPath, 128, VacantTarget->BackendPath);
   2.222 +      RtlStringCbCatA(TmpPath, 128, "/state");
   2.223  
   2.224 -          return;
   2.225 -        }
   2.226 -      }
   2.227 +      DeviceData->XenDeviceData->XenBusInterface.AddWatch(XBT_NIL, TmpPath, XenVbd_BackEndStateHandler, VacantTarget);
   2.228      }
   2.229 +    else
   2.230 +      KeReleaseSpinLock(&DeviceData->Lock, OldIrql);
   2.231      break;
   2.232    }
   2.233    
   2.234 @@ -652,6 +605,17 @@ XenVbd_HwScsiInitialize(PVOID DeviceExte
   2.235          KdPrint((__DRIVER_NAME "     found existing vbd device %s\n", VbdDevices[i]));
   2.236          RtlStringCbPrintfA(buffer, ARRAY_SIZE(buffer), "device/vbd/%s/state", VbdDevices[i]);
   2.237          XenVbd_WatchHandler(buffer, DeviceData);
   2.238 +//        WaitTimeout.QuadPart = -600000000;
   2.239 +        KeWaitForSingleObject(&DeviceData->WaitDevicesEvent, Executive, KernelMode, FALSE, NULL);
   2.240 +        KdPrint((__DRIVER_NAME "     %d devices enumerated\n", DeviceData->EnumeratedDevices));
   2.241 +      }  
   2.242 +    }
   2.243 +/*
   2.244 +      for (i = 0; VbdDevices[i]; i++)
   2.245 +      {
   2.246 +        KdPrint((__DRIVER_NAME "     found existing vbd device %s\n", VbdDevices[i]));
   2.247 +        RtlStringCbPrintfA(buffer, ARRAY_SIZE(buffer), "device/vbd/%s/state", VbdDevices[i]);
   2.248 +        XenVbd_WatchHandler(buffer, DeviceData);
   2.249          //ExFreePoolWithTag(bdDevices[i], XENPCI_POOL_TAG);
   2.250        }
   2.251        KdPrint((__DRIVER_NAME "     Waiting for %d devices to be enumerated\n", i));
   2.252 @@ -666,6 +630,7 @@ XenVbd_HwScsiInitialize(PVOID DeviceExte
   2.253          KdPrint((__DRIVER_NAME "     %d out of %d devices enumerated\n", DeviceData->EnumeratedDevices, i));
   2.254        }  
   2.255      }
   2.256 +*/
   2.257      ScsiPortNotification(BusChangeDetected, DeviceData, 0);
   2.258      DeviceData->BusChangePending = 0;
   2.259    }
   2.260 @@ -943,10 +908,10 @@ XenVbd_HwScsiStartIo(PVOID DeviceExtensi
   2.261        KdPrint((__DRIVER_NAME "     Command = READ_CAPACITY\n"));
   2.262        DataBuffer = Srb->DataBuffer;
   2.263        RtlZeroMemory(DataBuffer, Srb->DataTransferLength);
   2.264 -      DataBuffer[0] = (unsigned char)(TargetData->TotalSectors >> 24) & 0xff;
   2.265 -      DataBuffer[1] = (unsigned char)(TargetData->TotalSectors >> 16) & 0xff;
   2.266 -      DataBuffer[2] = (unsigned char)(TargetData->TotalSectors >> 8) & 0xff;
   2.267 -      DataBuffer[3] = (unsigned char)(TargetData->TotalSectors >> 0) & 0xff;
   2.268 +      DataBuffer[0] = (unsigned char)((TargetData->TotalSectors - 1) >> 24) & 0xff;
   2.269 +      DataBuffer[1] = (unsigned char)((TargetData->TotalSectors - 1) >> 16) & 0xff;
   2.270 +      DataBuffer[2] = (unsigned char)((TargetData->TotalSectors - 1) >> 8) & 0xff;
   2.271 +      DataBuffer[3] = (unsigned char)((TargetData->TotalSectors - 1) >> 0) & 0xff;
   2.272        DataBuffer[4] = (unsigned char)(TargetData->BytesPerSector >> 24) & 0xff;
   2.273        DataBuffer[5] = (unsigned char)(TargetData->BytesPerSector >> 16) & 0xff;
   2.274        DataBuffer[6] = (unsigned char)(TargetData->BytesPerSector >> 8) & 0xff;
   2.275 @@ -963,8 +928,6 @@ XenVbd_HwScsiStartIo(PVOID DeviceExtensi
   2.276        Srb->ScsiStatus = 0;
   2.277        Srb->SrbStatus = SRB_STATUS_SUCCESS;
   2.278        Srb->DataTransferLength = 0;
   2.279 -//      ScsiPhysicalAddress = ScsiPortGetPhysicalAddress(DeviceData, Srb, Srb->DataBuffer, &Srb->DataTransferLength);
   2.280 -//      DataBuffer = ScsiPortGetVirtualAddress(DeviceData, ScsiPhysicalAddress);
   2.281        DataBuffer = Srb->DataBuffer;
   2.282        RtlZeroMemory(DataBuffer, Srb->DataTransferLength);
   2.283        switch(cdb->MODE_SENSE.PageCode)
     3.1 --- a/xenvbd/xenvbd.h	Sat Dec 15 15:42:55 2007 +1100
     3.2 +++ b/xenvbd/xenvbd.h	Sun Dec 16 17:44:31 2007 +1100
     3.3 @@ -82,8 +82,7 @@ struct
     3.4    PXENPCI_XEN_DEVICE_DATA XenDeviceData;
     3.5    XENVBD_BUS_DATA BusData[SCSI_BUSES];
     3.6  
     3.7 -  int FastPathUsed;
     3.8 -  int SlowPathUsed;
     3.9 +  KSPIN_LOCK Lock;
    3.10  
    3.11    int BusChangePending;
    3.12