win-pvdrivers

changeset 889:4b8b40cda748

Add reset handling to storport vbd driver
author James Harper <james.harper@bendigoit.com.au>
date Thu Mar 17 00:04:24 2011 +1100 (2011-03-17)
parents 33b9eccb6301
children aacde7adc8b0
files xenvbd/xenvbd_storport.c xenvbd/xenvbd_storport.h
line diff
     1.1 --- a/xenvbd/xenvbd_storport.c	Thu Mar 17 00:04:06 2011 +1100
     1.2 +++ b/xenvbd/xenvbd_storport.c	Thu Mar 17 00:04:24 2011 +1100
     1.3 @@ -68,8 +68,9 @@ get_shadow_from_freelist(PXENVBD_DEVICE_
     1.4  static VOID
     1.5  put_shadow_on_freelist(PXENVBD_DEVICE_DATA xvdd, blkif_shadow_t *shadow)
     1.6  {
     1.7 -  xvdd->shadow_free_list[xvdd->shadow_free] = (USHORT)shadow->req.id;
     1.8 +  xvdd->shadow_free_list[xvdd->shadow_free] = (USHORT)(shadow->req.id & SHADOW_ID_ID_MASK);
     1.9    shadow->srb = NULL;
    1.10 +  shadow->reset = FALSE;
    1.11    xvdd->shadow_free++;
    1.12  }
    1.13  
    1.14 @@ -348,6 +349,9 @@ XenVbd_InitFromConfig(PXENVBD_DEVICE_DAT
    1.15      for (i = 0; i < SHADOW_ENTRIES; i++)
    1.16      {
    1.17        xvdd->shadows[i].req.id = i;
    1.18 +      /* make sure leftover real requests's are never confused with dump mode requests */
    1.19 +      if (dump_mode)
    1.20 +        xvdd->shadows[i].req.id |= SHADOW_ID_DUMP_FLAG;
    1.21        put_shadow_on_freelist(xvdd, &xvdd->shadows[i]);
    1.22      }
    1.23    }
    1.24 @@ -550,6 +554,7 @@ XenVbd_PutQueuedSrbsOnRing(PXENVBD_DEVIC
    1.25      shadow->srb = srb;
    1.26      shadow->length = 0;
    1.27      shadow->system_address = system_address;
    1.28 +    shadow->reset = FALSE;
    1.29  
    1.30      if (!dump_mode)
    1.31      {
    1.32 @@ -1135,69 +1140,68 @@ XenVbd_HandleEventSynchronised(PVOID Dev
    1.33          xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
    1.34          break;
    1.35        case RING_DETECT_STATE_COMPLETE:
    1.36 -        shadow = &xvdd->shadows[rep->id];
    1.37 -        srb = shadow->srb;
    1.38 -        ASSERT(srb);
    1.39 -        srb_entry = srb->SrbExtension;
    1.40 -        ASSERT(srb_entry);
    1.41 -        //block_count = decode_cdb_length(srb);
    1.42 -        //block_count *= xvdd->bytes_per_sector / 512;
    1.43 -        /* a few errors occur in dump mode because Xen refuses to allow us to map pages we are using for other stuff. Just ignore them */
    1.44 -        if (rep->status == BLKIF_RSP_OKAY || (dump_mode &&  dump_mode_errors++ < DUMP_MODE_ERROR_LIMIT))
    1.45 -          srb->SrbStatus = SRB_STATUS_SUCCESS;
    1.46 +        shadow = &xvdd->shadows[rep->id & SHADOW_ID_ID_MASK];
    1.47 +        if (shadow->reset)
    1.48 +        {
    1.49 +          KdPrint((__DRIVER_NAME "     discarding reset shadow\n"));
    1.50 +          for (j = 0; j < shadow->req.nr_segments; j++)
    1.51 +          {
    1.52 +            xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
    1.53 +              shadow->req.seg[j].gref, FALSE, xvdd->grant_tag);
    1.54 +          }
    1.55 +        }
    1.56 +        else if (dump_mode && !(rep->id & SHADOW_ID_DUMP_FLAG))
    1.57 +        {
    1.58 +          KdPrint((__DRIVER_NAME "     discarding stale (non-dump-mode) shadow\n"));
    1.59 +        }
    1.60          else
    1.61          {
    1.62 -          KdPrint((__DRIVER_NAME "     Xen Operation returned error\n"));
    1.63 -          if (decode_cdb_is_read(srb))
    1.64 -            KdPrint((__DRIVER_NAME "     Operation = Read\n"));
    1.65 +          srb = shadow->srb;
    1.66 +          ASSERT(srb);
    1.67 +          srb_entry = srb->SrbExtension;
    1.68 +          ASSERT(srb_entry);
    1.69 +          /* a few errors occur in dump mode because Xen refuses to allow us to map pages we are using for other stuff. Just ignore them */
    1.70 +          if (rep->status == BLKIF_RSP_OKAY || (dump_mode &&  dump_mode_errors++ < DUMP_MODE_ERROR_LIMIT))
    1.71 +            srb->SrbStatus = SRB_STATUS_SUCCESS;
    1.72            else
    1.73 -            KdPrint((__DRIVER_NAME "     Operation = Write\n"));
    1.74 -          if (!dump_mode)
    1.75            {
    1.76 -            KdPrint((__DRIVER_NAME "     Sector = %08X, Count = %d\n", (ULONG)shadow->req.sector_number, shadow->length / 512));
    1.77 -            KdPrint((__DRIVER_NAME "     DataBuffer = %p\n", srb->DataBuffer));
    1.78 -            KdPrint((__DRIVER_NAME "     Physical = %08x%08x\n", MmGetPhysicalAddress(shadow->system_address).HighPart, MmGetPhysicalAddress(shadow->system_address).LowPart));
    1.79 -            KdPrint((__DRIVER_NAME "     PFN = %08x\n", (ULONG)(MmGetPhysicalAddress(shadow->system_address).QuadPart >> PAGE_SHIFT)));
    1.80 -
    1.81 -            for (j = 0; j < shadow->req.nr_segments; j++)
    1.82 +            KdPrint((__DRIVER_NAME "     Xen Operation returned error\n"));
    1.83 +            if (decode_cdb_is_read(srb))
    1.84 +              KdPrint((__DRIVER_NAME "     Operation = Read\n"));
    1.85 +            else
    1.86 +              KdPrint((__DRIVER_NAME "     Operation = Write\n"));
    1.87 +            srb_entry->error = TRUE;
    1.88 +          }
    1.89 +          if (shadow->aligned_buffer_in_use)
    1.90 +          {
    1.91 +            ASSERT(xvdd->aligned_buffer_in_use);
    1.92 +            xvdd->aligned_buffer_in_use = FALSE;
    1.93 +            if (srb->SrbStatus == SRB_STATUS_SUCCESS && decode_cdb_is_read(srb))
    1.94 +              memcpy((PUCHAR)shadow->system_address, xvdd->aligned_buffer, shadow->length);
    1.95 +          }
    1.96 +          for (j = 0; j < shadow->req.nr_segments; j++)
    1.97 +          {
    1.98 +            xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
    1.99 +              shadow->req.seg[j].gref, FALSE, xvdd->grant_tag);
   1.100 +          }
   1.101 +          srb_entry->outstanding_requests--;
   1.102 +          if (!srb_entry->outstanding_requests && srb_entry->offset == srb_entry->length)
   1.103 +          {
   1.104 +            if (srb_entry->error)
   1.105              {
   1.106 -              KdPrint((__DRIVER_NAME "     gref = %d\n", shadow->req.seg[j].gref));
   1.107 -              KdPrint((__DRIVER_NAME "     first_sect = %d\n", shadow->req.seg[j].first_sect));
   1.108 -              KdPrint((__DRIVER_NAME "     last_sect = %d\n", shadow->req.seg[j].last_sect));
   1.109 -            }
   1.110 +              srb->SrbStatus = SRB_STATUS_ERROR;
   1.111 +              srb->ScsiStatus = 0x02;
   1.112 +              xvdd->last_sense_key = SCSI_SENSE_MEDIUM_ERROR;
   1.113 +              xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
   1.114 +              XenVbd_MakeAutoSense(xvdd, srb);
   1.115 +            }        
   1.116 +            StorPortNotification(RequestComplete, xvdd, srb);
   1.117            }
   1.118 -          srb_entry->error = TRUE;
   1.119 -        }
   1.120 -        if (shadow->aligned_buffer_in_use)
   1.121 -        {
   1.122 -          ASSERT(xvdd->aligned_buffer_in_use);
   1.123 -          xvdd->aligned_buffer_in_use = FALSE;
   1.124 -          if (decode_cdb_is_read(srb))
   1.125 -            memcpy((PUCHAR)shadow->system_address, xvdd->aligned_buffer, shadow->length);
   1.126 -        }
   1.127 -        
   1.128 -        for (j = 0; j < shadow->req.nr_segments; j++)
   1.129 -        {
   1.130 -          xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
   1.131 -            shadow->req.seg[j].gref, FALSE, xvdd->grant_tag);
   1.132          }
   1.133          shadow->aligned_buffer_in_use = FALSE;
   1.134          shadow->srb = NULL;
   1.135 +        shadow->reset = FALSE;
   1.136          put_shadow_on_freelist(xvdd, shadow);
   1.137 -        //if (dump_mode) KdPrint((__DRIVER_NAME "     srb = %p\n", srb));
   1.138 -        srb_entry->outstanding_requests--;
   1.139 -        if (!srb_entry->outstanding_requests && srb_entry->offset == srb_entry->length)
   1.140 -        {
   1.141 -          if (srb_entry->error)
   1.142 -          {
   1.143 -            srb->SrbStatus = SRB_STATUS_ERROR;
   1.144 -            srb->ScsiStatus = 0x02;
   1.145 -            xvdd->last_sense_key = SCSI_SENSE_MEDIUM_ERROR;
   1.146 -            xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
   1.147 -            XenVbd_MakeAutoSense(xvdd, srb);
   1.148 -          }        
   1.149 -          StorPortNotification(RequestComplete, xvdd, srb);
   1.150 -        }
   1.151          break;
   1.152        }
   1.153      }
   1.154 @@ -1261,6 +1265,81 @@ XenVbd_HwStorInterrupt(PVOID DeviceExten
   1.155  }
   1.156  
   1.157  static BOOLEAN
   1.158 +XenVbd_HwStorResetBus(PVOID DeviceExtension, ULONG PathId)
   1.159 +{
   1.160 +  PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
   1.161 +  //srb_list_entry_t *srb_entry;
   1.162 +  int i;
   1.163 +  /* need to make sure that each SRB is only reset once */
   1.164 +  LIST_ENTRY srb_reset_list;
   1.165 +  PLIST_ENTRY list_entry;
   1.166 +  //STOR_LOCK_HANDLE lock_handle;
   1.167 +
   1.168 +  UNREFERENCED_PARAMETER(PathId);
   1.169 +
   1.170 +  FUNCTION_ENTER();
   1.171 +
   1.172 +  /* It appears that the StartIo spinlock is already held at this point */
   1.173 +
   1.174 +  KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
   1.175 +
   1.176 +  if (xvdd->ring_detect_state == RING_DETECT_STATE_COMPLETE && xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
   1.177 +  {
   1.178 +    xvdd->aligned_buffer_in_use = FALSE;
   1.179 +    
   1.180 +    InitializeListHead(&srb_reset_list);
   1.181 +    
   1.182 +    while((list_entry = RemoveHeadList(&xvdd->srb_list)) != &xvdd->srb_list)
   1.183 +    {
   1.184 +      srb_list_entry_t *srb_entry = CONTAINING_RECORD(list_entry, srb_list_entry_t, list_entry);
   1.185 +      KdPrint((__DRIVER_NAME "     adding queued SRB %p to reset list\n", srb_entry->srb));
   1.186 +      InsertTailList(&srb_reset_list, list_entry);
   1.187 +    }
   1.188 +    
   1.189 +    for (i = 0; i < MAX_SHADOW_ENTRIES; i++)
   1.190 +    {
   1.191 +      if (xvdd->shadows[i].srb)
   1.192 +      {
   1.193 +        srb_list_entry_t *srb_entry = xvdd->shadows[i].srb->SrbExtension;
   1.194 +        for (list_entry = srb_reset_list.Flink; list_entry != &srb_reset_list; list_entry = list_entry->Flink)
   1.195 +        {
   1.196 +          if (list_entry == &srb_entry->list_entry)
   1.197 +            break;
   1.198 +        }
   1.199 +        if (list_entry == &srb_reset_list)
   1.200 +        {
   1.201 +          KdPrint((__DRIVER_NAME "     adding in-flight SRB %p to reset list\n", srb_entry->srb));
   1.202 +          InsertTailList(&srb_reset_list, &srb_entry->list_entry);
   1.203 +        }
   1.204 +        /* set reset here so that the interrupt won't do anything with the srb but will dispose of the shadow entry correctly */
   1.205 +        xvdd->shadows[i].reset = TRUE;
   1.206 +        xvdd->shadows[i].srb = NULL;
   1.207 +        xvdd->shadows[i].aligned_buffer_in_use = FALSE;
   1.208 +      }
   1.209 +    }
   1.210 +
   1.211 +    while((list_entry = RemoveHeadList(&srb_reset_list)) != &srb_reset_list)
   1.212 +    {
   1.213 +      srb_list_entry_t *srb_entry = CONTAINING_RECORD(list_entry, srb_list_entry_t, list_entry);
   1.214 +      srb_entry->srb->SrbStatus = SRB_STATUS_BUS_RESET;
   1.215 +      KdPrint((__DRIVER_NAME "     completing SRB %p with status SRB_STATUS_BUS_RESET\n", srb_entry->srb));
   1.216 +      StorPortNotification(RequestComplete, xvdd, srb_entry->srb);
   1.217 +    }
   1.218 +
   1.219 +    /* send a notify to Dom0 just in case it was missed for some reason (which should _never_ happen) */
   1.220 +    xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
   1.221 +  
   1.222 +    StorPortNotification(NextRequest, DeviceExtension);
   1.223 +  }
   1.224 +
   1.225 +  //StorPortReleaseSpinLock(DeviceExtension, &lock_handle);
   1.226 +
   1.227 +  FUNCTION_EXIT();
   1.228 +
   1.229 +  return TRUE;
   1.230 +}
   1.231 +
   1.232 +static BOOLEAN
   1.233  XenVbd_HwStorStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK srb)
   1.234  {
   1.235    PUCHAR data_buffer;
   1.236 @@ -1802,6 +1881,15 @@ XenVbd_HwStorStartIo(PVOID DeviceExtensi
   1.237      //srb->SrbStatus = SRB_STATUS_SUCCESS;
   1.238      //StorPortNotification(RequestComplete, DeviceExtension, srb);
   1.239      break;
   1.240 +  case SRB_FUNCTION_RESET_BUS:
   1.241 +  case SRB_FUNCTION_RESET_DEVICE:
   1.242 +  case SRB_FUNCTION_RESET_LOGICAL_UNIT:
   1.243 +    /* the path doesn't matter here - only ever one device*/
   1.244 +    XenVbd_HwStorResetBus(DeviceExtension, 0);
   1.245 +    srb->SrbStatus = SRB_STATUS_SUCCESS;
   1.246 +    StorPortNotification(RequestComplete, DeviceExtension, srb);    
   1.247 +    break;
   1.248 +  
   1.249    default:
   1.250      KdPrint((__DRIVER_NAME "     Unhandled srb->Function = %08X\n", srb->Function));
   1.251      srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
   1.252 @@ -1814,24 +1902,6 @@ XenVbd_HwStorStartIo(PVOID DeviceExtensi
   1.253    return TRUE;
   1.254  }
   1.255  
   1.256 -static BOOLEAN
   1.257 -XenVbd_HwStorResetBus(PVOID DeviceExtension, ULONG PathId)
   1.258 -{
   1.259 -  //PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
   1.260 -
   1.261 -  UNREFERENCED_PARAMETER(DeviceExtension);
   1.262 -  UNREFERENCED_PARAMETER(PathId);
   1.263 -
   1.264 -  FUNCTION_ENTER();
   1.265 -
   1.266 -  KdPrint((__DRIVER_NAME "     IRQL = %d\n", KeGetCurrentIrql()));
   1.267 -
   1.268 -  FUNCTION_EXIT();
   1.269 -
   1.270 -
   1.271 -  return TRUE;
   1.272 -}
   1.273 -
   1.274  static SCSI_ADAPTER_CONTROL_STATUS
   1.275  XenVbd_HwStorAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters)
   1.276  {
   1.277 @@ -1900,8 +1970,6 @@ DriverEntry(PDRIVER_OBJECT DriverObject,
   1.278    ULONG status;
   1.279    VIRTUAL_HW_INITIALIZATION_DATA VHwInitializationData;
   1.280    HW_INITIALIZATION_DATA HwInitializationData;
   1.281 -  //PVOID driver_extension;
   1.282 -  //PUCHAR ptr;
   1.283    OBJECT_ATTRIBUTES oa;
   1.284    HANDLE service_handle;
   1.285    UNICODE_STRING param_name;
     2.1 --- a/xenvbd/xenvbd_storport.h	Thu Mar 17 00:04:06 2011 +1100
     2.2 +++ b/xenvbd/xenvbd_storport.h	Thu Mar 17 00:04:24 2011 +1100
     2.3 @@ -89,9 +89,16 @@ typedef struct {
     2.4    PVOID system_address;
     2.5    ULONG length;
     2.6    BOOLEAN aligned_buffer_in_use;
     2.7 +  BOOLEAN reset;
     2.8 +  #if DBG && NTDDI_VERSION >= NTDDI_WINXP
     2.9 +  LARGE_INTEGER ring_submit_time;
    2.10 +  #endif
    2.11  } blkif_shadow_t;
    2.12  
    2.13 -#define MAX_SHADOW_ENTRIES 64
    2.14 +#define MAX_SHADOW_ENTRIES  64
    2.15 +#define SHADOW_ID_ID_MASK   0x03FF /* maximum of 1024 requests - currently use a maximum of 64 though */
    2.16 +#define SHADOW_ID_DUMP_FLAG 0x8000 /* indicates the request was generated by dump mode */
    2.17 +
    2.18  #define SHADOW_ENTRIES min(MAX_SHADOW_ENTRIES, min(BLK_RING_SIZE, BLK_OTHER_RING_SIZE))
    2.19  
    2.20  typedef enum {