X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FBus%2FScsi%2FScsiDiskDxe%2FScsiDisk.c;h=6555aeed7d69355df2db54da8db8cec4c6dfe004;hb=eae9e51d98d5eed708c512670b9381d3597a766e;hp=6bfcf03a4b4122fac888bf6880be5ceb091a8640;hpb=ac81789c3b3093a1efffc82333a7c13b8d814222;p=mirror_edk2.git diff --git a/MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c b/MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c index 6bfcf03a4b..6555aeed7d 100644 --- a/MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c +++ b/MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c @@ -2,14 +2,18 @@ SCSI disk driver that layers on every SCSI IO protocol in the system. Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
+Copyright (c) 1985 - 2022, American Megatrends International LLC.
+<<<<<<< HEAD +======= + +>>>>>>> ace365b4e0 (MdeModulePkg/scsi :Coverity scan flags multiple issues in edk2-stable202205) SPDX-License-Identifier: BSD-2-Clause-Patent **/ - #include "ScsiDisk.h" -EFI_DRIVER_BINDING_PROTOCOL gScsiDiskDriverBinding = { +EFI_DRIVER_BINDING_PROTOCOL gScsiDiskDriverBinding = { ScsiDiskDriverBindingSupported, ScsiDiskDriverBindingStart, ScsiDiskDriverBindingStop, @@ -18,7 +22,7 @@ EFI_DRIVER_BINDING_PROTOCOL gScsiDiskDriverBinding = { NULL }; -EFI_DISK_INFO_PROTOCOL gScsiDiskInfoProtocolTemplate = { +EFI_DISK_INFO_PROTOCOL gScsiDiskInfoProtocolTemplate = { EFI_DISK_INFO_SCSI_INTERFACE_GUID, ScsiDiskInfoInquiry, ScsiDiskInfoIdentify, @@ -40,8 +44,8 @@ EFI_DISK_INFO_PROTOCOL gScsiDiskInfoProtocolTemplate = { **/ VOID * AllocateAlignedBuffer ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN UINTN BufferSize + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN UINTN BufferSize ) { return AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), ScsiDiskDevice->ScsiIo->IoAlign); @@ -59,8 +63,8 @@ AllocateAlignedBuffer ( **/ VOID FreeAlignedBuffer ( - IN VOID *Buffer, - IN UINTN BufferSize + IN VOID *Buffer, + IN UINTN BufferSize ) { if (Buffer != NULL) { @@ -68,6 +72,33 @@ FreeAlignedBuffer ( } } +/** + Remove trailing spaces from the string. + + @param String The ASCII string to remove the trailing spaces. + + @retval the new length of the string. +**/ +UINTN +RemoveTrailingSpaces ( + IN OUT CHAR8 *String + ) +{ + UINTN Length; + + Length = AsciiStrLen (String); + if (Length == 0) { + return 0; + } + + while ((Length > 0) && (String[Length-1] == ' ')) { + Length--; + } + + String[Length] = '\0'; + return Length; +} + /** The user Entry Point for module ScsiDisk. @@ -82,12 +113,12 @@ FreeAlignedBuffer ( **/ EFI_STATUS EFIAPI -InitializeScsiDisk( - IN EFI_HANDLE ImageHandle, - IN EFI_SYSTEM_TABLE *SystemTable +InitializeScsiDisk ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable ) { - EFI_STATUS Status; + EFI_STATUS Status; // // Install driver model protocol(s). @@ -102,7 +133,6 @@ InitializeScsiDisk( ); ASSERT_EFI_ERROR (Status); - return Status; } @@ -140,7 +170,7 @@ ScsiDiskDriverBindingSupported ( Status = gBS->OpenProtocol ( Controller, &gEfiScsiIoProtocolGuid, - (VOID **) &ScsiIo, + (VOID **)&ScsiIo, This->DriverBindingHandle, Controller, EFI_OPEN_PROTOCOL_BY_DRIVER @@ -153,7 +183,8 @@ ScsiDiskDriverBindingSupported ( if (!EFI_ERROR (Status)) { if ((DeviceType == EFI_SCSI_TYPE_DISK) || (DeviceType == EFI_SCSI_TYPE_CDROM) || - (DeviceType == EFI_SCSI_TYPE_WLUN)) { + (DeviceType == EFI_SCSI_TYPE_WLUN)) + { Status = EFI_SUCCESS; } else { Status = EFI_UNSUPPORTED; @@ -169,7 +200,6 @@ ScsiDiskDriverBindingSupported ( return Status; } - /** Start this driver on ControllerHandle. @@ -205,10 +235,13 @@ ScsiDiskDriverBindingStart ( UINT8 MaxRetry; BOOLEAN NeedRetry; BOOLEAN MustReadCapacity; + CHAR8 VendorStr[VENDOR_IDENTIFICATION_LENGTH + 1]; + CHAR8 ProductStr[PRODUCT_IDENTIFICATION_LENGTH + 1]; + CHAR16 DeviceStr[VENDOR_IDENTIFICATION_LENGTH + PRODUCT_IDENTIFICATION_LENGTH + 2]; MustReadCapacity = TRUE; - ScsiDiskDevice = (SCSI_DISK_DEV *) AllocateZeroPool (sizeof (SCSI_DISK_DEV)); + ScsiDiskDevice = (SCSI_DISK_DEV *)AllocateZeroPool (sizeof (SCSI_DISK_DEV)); if (ScsiDiskDevice == NULL) { return EFI_OUT_OF_RESOURCES; } @@ -216,7 +249,7 @@ ScsiDiskDriverBindingStart ( Status = gBS->OpenProtocol ( Controller, &gEfiScsiIoProtocolGuid, - (VOID **) &ScsiIo, + (VOID **)&ScsiIo, This->DriverBindingHandle, Controller, EFI_OPEN_PROTOCOL_BY_DRIVER @@ -252,35 +285,36 @@ ScsiDiskDriverBindingStart ( ScsiIo->GetDeviceType (ScsiIo, &(ScsiDiskDevice->DeviceType)); switch (ScsiDiskDevice->DeviceType) { - case EFI_SCSI_TYPE_DISK: - ScsiDiskDevice->BlkIo.Media->BlockSize = 0x200; - MustReadCapacity = TRUE; - break; + case EFI_SCSI_TYPE_DISK: + ScsiDiskDevice->BlkIo.Media->BlockSize = 0x200; + MustReadCapacity = TRUE; + break; - case EFI_SCSI_TYPE_CDROM: - ScsiDiskDevice->BlkIo.Media->BlockSize = 0x800; - ScsiDiskDevice->BlkIo.Media->ReadOnly = TRUE; - MustReadCapacity = FALSE; - break; + case EFI_SCSI_TYPE_CDROM: + ScsiDiskDevice->BlkIo.Media->BlockSize = 0x800; + ScsiDiskDevice->BlkIo.Media->ReadOnly = TRUE; + MustReadCapacity = FALSE; + break; - case EFI_SCSI_TYPE_WLUN: - MustReadCapacity = FALSE; - break; + case EFI_SCSI_TYPE_WLUN: + MustReadCapacity = FALSE; + break; } + // // The Sense Data Array's initial size is 6 // ScsiDiskDevice->SenseDataNumber = 6; - ScsiDiskDevice->SenseData = (EFI_SCSI_SENSE_DATA *) AllocateZeroPool ( - sizeof (EFI_SCSI_SENSE_DATA) * ScsiDiskDevice->SenseDataNumber - ); + ScsiDiskDevice->SenseData = (EFI_SCSI_SENSE_DATA *)AllocateZeroPool ( + sizeof (EFI_SCSI_SENSE_DATA) * ScsiDiskDevice->SenseDataNumber + ); if (ScsiDiskDevice->SenseData == NULL) { gBS->CloseProtocol ( - Controller, - &gEfiScsiIoProtocolGuid, - This->DriverBindingHandle, - Controller - ); + Controller, + &gEfiScsiIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); FreePool (ScsiDiskDevice); return EFI_OUT_OF_RESOURCES; } @@ -307,6 +341,7 @@ ScsiDiskDriverBindingStart ( return EFI_DEVICE_ERROR; } } + // // The second parameter "TRUE" means must // retrieve media capacity @@ -341,6 +376,7 @@ ScsiDiskDriverBindingStart ( DEBUG ((DEBUG_ERROR, "ScsiDisk: Failed to install the Erase Block Protocol! Status = %r\n", Status)); } } + if (DetermineInstallStorageSecurity (ScsiDiskDevice, Controller)) { Status = gBS->InstallProtocolInterface ( &Controller, @@ -352,19 +388,38 @@ ScsiDiskDriverBindingStart ( DEBUG ((DEBUG_ERROR, "ScsiDisk: Failed to install the Storage Security Command Protocol! Status = %r\n", Status)); } } + + CopyMem ( + VendorStr, + &ScsiDiskDevice->InquiryData.Reserved_5_95[VENDOR_IDENTIFICATION_OFFSET], + VENDOR_IDENTIFICATION_LENGTH + ); + VendorStr[VENDOR_IDENTIFICATION_LENGTH] = 0; + RemoveTrailingSpaces (VendorStr); + + CopyMem ( + ProductStr, + &ScsiDiskDevice->InquiryData.Reserved_5_95[PRODUCT_IDENTIFICATION_OFFSET], + PRODUCT_IDENTIFICATION_LENGTH + ); + ProductStr[PRODUCT_IDENTIFICATION_LENGTH] = 0; + RemoveTrailingSpaces (ProductStr); + + UnicodeSPrint (DeviceStr, sizeof (DeviceStr), L"%a %a", VendorStr, ProductStr); + ScsiDiskDevice->ControllerNameTable = NULL; AddUnicodeString2 ( "eng", gScsiDiskComponentName.SupportedLanguages, &ScsiDiskDevice->ControllerNameTable, - L"SCSI Disk Device", + DeviceStr, TRUE ); AddUnicodeString2 ( "en", gScsiDiskComponentName2.SupportedLanguages, &ScsiDiskDevice->ControllerNameTable, - L"SCSI Disk Device", + DeviceStr, FALSE ); return EFI_SUCCESS; @@ -381,10 +436,8 @@ ScsiDiskDriverBindingStart ( Controller ); return Status; - } - /** Stop this driver on ControllerHandle. @@ -407,21 +460,21 @@ ScsiDiskDriverBindingStart ( EFI_STATUS EFIAPI ScsiDiskDriverBindingStop ( - IN EFI_DRIVER_BINDING_PROTOCOL *This, - IN EFI_HANDLE Controller, - IN UINTN NumberOfChildren, - IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL ) { - EFI_BLOCK_IO_PROTOCOL *BlkIo; - EFI_ERASE_BLOCK_PROTOCOL *EraseBlock; - SCSI_DISK_DEV *ScsiDiskDevice; - EFI_STATUS Status; + EFI_BLOCK_IO_PROTOCOL *BlkIo; + EFI_ERASE_BLOCK_PROTOCOL *EraseBlock; + SCSI_DISK_DEV *ScsiDiskDevice; + EFI_STATUS Status; Status = gBS->OpenProtocol ( Controller, &gEfiBlockIoProtocolGuid, - (VOID **) &BlkIo, + (VOID **)&BlkIo, This->DriverBindingHandle, Controller, EFI_OPEN_PROTOCOL_GET_PROTOCOL @@ -435,7 +488,8 @@ ScsiDiskDriverBindingStop ( // // Wait for the BlockIo2 requests queue to become empty // - while (!IsListEmpty (&ScsiDiskDevice->AsyncTaskQueue)); + while (!IsListEmpty (&ScsiDiskDevice->AsyncTaskQueue)) { + } // // If Erase Block Protocol is installed, then uninstall this protocol. @@ -443,7 +497,7 @@ ScsiDiskDriverBindingStop ( Status = gBS->OpenProtocol ( Controller, &gEfiEraseBlockProtocolGuid, - (VOID **) &EraseBlock, + (VOID **)&EraseBlock, This->DriverBindingHandle, Controller, EFI_OPEN_PROTOCOL_GET_PROTOCOL @@ -482,6 +536,7 @@ ScsiDiskDriverBindingStop ( return EFI_SUCCESS; } + // // errors met // @@ -504,19 +559,19 @@ ScsiDiskDriverBindingStop ( EFI_STATUS EFIAPI ScsiDiskReset ( - IN EFI_BLOCK_IO_PROTOCOL *This, - IN BOOLEAN ExtendedVerification + IN EFI_BLOCK_IO_PROTOCOL *This, + IN BOOLEAN ExtendedVerification ) { - EFI_TPL OldTpl; - SCSI_DISK_DEV *ScsiDiskDevice; - EFI_STATUS Status; + EFI_TPL OldTpl; + SCSI_DISK_DEV *ScsiDiskDevice; + EFI_STATUS Status; OldTpl = gBS->RaiseTPL (TPL_CALLBACK); - ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO (This); + ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO (This); - Status = ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo); + Status = ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo); if (EFI_ERROR (Status)) { if (Status == EFI_UNSUPPORTED) { @@ -563,11 +618,11 @@ Done: EFI_STATUS EFIAPI ScsiDiskReadBlocks ( - IN EFI_BLOCK_IO_PROTOCOL *This, - IN UINT32 MediaId, - IN EFI_LBA Lba, - IN UINTN BufferSize, - OUT VOID *Buffer + IN EFI_BLOCK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN EFI_LBA Lba, + IN UINTN BufferSize, + OUT VOID *Buffer ) { SCSI_DISK_DEV *ScsiDiskDevice; @@ -583,8 +638,7 @@ ScsiDiskReadBlocks ( ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO (This); Media = ScsiDiskDevice->BlkIo.Media; - if (!IS_DEVICE_FIXED(ScsiDiskDevice)) { - + if (!IS_DEVICE_FIXED (ScsiDiskDevice)) { Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange); if (EFI_ERROR (Status)) { Status = EFI_DEVICE_ERROR; @@ -593,11 +647,11 @@ ScsiDiskReadBlocks ( if (MediaChange) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiBlockIoProtocolGuid, - &ScsiDiskDevice->BlkIo, - &ScsiDiskDevice->BlkIo - ); + ScsiDiskDevice->Handle, + &gEfiBlockIoProtocolGuid, + &ScsiDiskDevice->BlkIo, + &ScsiDiskDevice->BlkIo + ); gBS->ReinstallProtocolInterface ( ScsiDiskDevice->Handle, &gEfiBlockIo2ProtocolGuid, @@ -612,33 +666,37 @@ ScsiDiskReadBlocks ( &ScsiDiskDevice->EraseBlock ); } + if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiStorageSecurityCommandProtocolGuid, - &ScsiDiskDevice->StorageSecurity, - &ScsiDiskDevice->StorageSecurity - ); + ScsiDiskDevice->Handle, + &gEfiStorageSecurityCommandProtocolGuid, + &ScsiDiskDevice->StorageSecurity, + &ScsiDiskDevice->StorageSecurity + ); } + if (Media->MediaPresent) { Status = EFI_MEDIA_CHANGED; } else { Status = EFI_NO_MEDIA; } + goto Done; } } + // // Get the intrinsic block size // - BlockSize = Media->BlockSize; + BlockSize = Media->BlockSize; if (BlockSize == 0) { Status = EFI_DEVICE_ERROR; goto Done; } - NumberOfBlocks = BufferSize / BlockSize; + NumberOfBlocks = BufferSize / BlockSize; if (!(Media->MediaPresent)) { Status = EFI_NO_MEDIA; @@ -675,7 +733,7 @@ ScsiDiskReadBlocks ( goto Done; } - if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) { + if ((Media->IoAlign > 1) && (((UINTN)Buffer & (Media->IoAlign - 1)) != 0)) { Status = EFI_INVALID_PARAMETER; goto Done; } @@ -704,7 +762,7 @@ Done: @retval EFI_WRITE_PROTECTED The device can not be written to. @retval EFI_DEVICE_ERROR Fail to detect media. @retval EFI_NO_MEDIA Media is not present. - @retval EFI_MEDIA_CHNAGED Media has changed. + @retval EFI_MEDIA_CHANGED Media has changed. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device. @retval EFI_INVALID_PARAMETER Invalid parameter passed in. @@ -712,11 +770,11 @@ Done: EFI_STATUS EFIAPI ScsiDiskWriteBlocks ( - IN EFI_BLOCK_IO_PROTOCOL *This, - IN UINT32 MediaId, - IN EFI_LBA Lba, - IN UINTN BufferSize, - IN VOID *Buffer + IN EFI_BLOCK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN EFI_LBA Lba, + IN UINTN BufferSize, + IN VOID *Buffer ) { SCSI_DISK_DEV *ScsiDiskDevice; @@ -732,8 +790,7 @@ ScsiDiskWriteBlocks ( ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO (This); Media = ScsiDiskDevice->BlkIo.Media; - if (!IS_DEVICE_FIXED(ScsiDiskDevice)) { - + if (!IS_DEVICE_FIXED (ScsiDiskDevice)) { Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange); if (EFI_ERROR (Status)) { Status = EFI_DEVICE_ERROR; @@ -742,11 +799,11 @@ ScsiDiskWriteBlocks ( if (MediaChange) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiBlockIoProtocolGuid, - &ScsiDiskDevice->BlkIo, - &ScsiDiskDevice->BlkIo - ); + ScsiDiskDevice->Handle, + &gEfiBlockIoProtocolGuid, + &ScsiDiskDevice->BlkIo, + &ScsiDiskDevice->BlkIo + ); gBS->ReinstallProtocolInterface ( ScsiDiskDevice->Handle, &gEfiBlockIo2ProtocolGuid, @@ -761,33 +818,37 @@ ScsiDiskWriteBlocks ( &ScsiDiskDevice->EraseBlock ); } + if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiStorageSecurityCommandProtocolGuid, - &ScsiDiskDevice->StorageSecurity, - &ScsiDiskDevice->StorageSecurity - ); + ScsiDiskDevice->Handle, + &gEfiStorageSecurityCommandProtocolGuid, + &ScsiDiskDevice->StorageSecurity, + &ScsiDiskDevice->StorageSecurity + ); } + if (Media->MediaPresent) { Status = EFI_MEDIA_CHANGED; } else { Status = EFI_NO_MEDIA; } + goto Done; } } + // // Get the intrinsic block size // - BlockSize = Media->BlockSize; + BlockSize = Media->BlockSize; if (BlockSize == 0) { Status = EFI_DEVICE_ERROR; goto Done; } - NumberOfBlocks = BufferSize / BlockSize; + NumberOfBlocks = BufferSize / BlockSize; if (!(Media->MediaPresent)) { Status = EFI_NO_MEDIA; @@ -829,10 +890,11 @@ ScsiDiskWriteBlocks ( goto Done; } - if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) { + if ((Media->IoAlign > 1) && (((UINTN)Buffer & (Media->IoAlign - 1)) != 0)) { Status = EFI_INVALID_PARAMETER; goto Done; } + // // if all the parameters are valid, then perform read sectors command // to transfer data from device to host. @@ -857,7 +919,7 @@ Done: EFI_STATUS EFIAPI ScsiDiskFlushBlocks ( - IN EFI_BLOCK_IO_PROTOCOL *This + IN EFI_BLOCK_IO_PROTOCOL *This ) { // @@ -866,7 +928,6 @@ ScsiDiskFlushBlocks ( return EFI_SUCCESS; } - /** Reset SCSI Disk. @@ -886,15 +947,15 @@ ScsiDiskResetEx ( IN BOOLEAN ExtendedVerification ) { - EFI_TPL OldTpl; - SCSI_DISK_DEV *ScsiDiskDevice; - EFI_STATUS Status; + EFI_TPL OldTpl; + SCSI_DISK_DEV *ScsiDiskDevice; + EFI_STATUS Status; OldTpl = gBS->RaiseTPL (TPL_CALLBACK); - ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO2 (This); + ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO2 (This); - Status = ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo); + Status = ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo); if (EFI_ERROR (Status)) { if (Status == EFI_UNSUPPORTED) { @@ -950,12 +1011,12 @@ Done: EFI_STATUS EFIAPI ScsiDiskReadBlocksEx ( - IN EFI_BLOCK_IO2_PROTOCOL *This, - IN UINT32 MediaId, - IN EFI_LBA Lba, - IN OUT EFI_BLOCK_IO2_TOKEN *Token, - IN UINTN BufferSize, - OUT VOID *Buffer + IN EFI_BLOCK_IO2_PROTOCOL *This, + IN UINT32 MediaId, + IN EFI_LBA Lba, + IN OUT EFI_BLOCK_IO2_TOKEN *Token, + IN UINTN BufferSize, + OUT VOID *Buffer ) { SCSI_DISK_DEV *ScsiDiskDevice; @@ -971,8 +1032,7 @@ ScsiDiskReadBlocksEx ( ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO2 (This); Media = ScsiDiskDevice->BlkIo.Media; - if (!IS_DEVICE_FIXED(ScsiDiskDevice)) { - + if (!IS_DEVICE_FIXED (ScsiDiskDevice)) { Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange); if (EFI_ERROR (Status)) { Status = EFI_DEVICE_ERROR; @@ -981,11 +1041,11 @@ ScsiDiskReadBlocksEx ( if (MediaChange) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiBlockIoProtocolGuid, - &ScsiDiskDevice->BlkIo, - &ScsiDiskDevice->BlkIo - ); + ScsiDiskDevice->Handle, + &gEfiBlockIoProtocolGuid, + &ScsiDiskDevice->BlkIo, + &ScsiDiskDevice->BlkIo + ); gBS->ReinstallProtocolInterface ( ScsiDiskDevice->Handle, &gEfiBlockIo2ProtocolGuid, @@ -1000,33 +1060,37 @@ ScsiDiskReadBlocksEx ( &ScsiDiskDevice->EraseBlock ); } + if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiStorageSecurityCommandProtocolGuid, - &ScsiDiskDevice->StorageSecurity, - &ScsiDiskDevice->StorageSecurity - ); + ScsiDiskDevice->Handle, + &gEfiStorageSecurityCommandProtocolGuid, + &ScsiDiskDevice->StorageSecurity, + &ScsiDiskDevice->StorageSecurity + ); } + if (Media->MediaPresent) { Status = EFI_MEDIA_CHANGED; } else { Status = EFI_NO_MEDIA; } + goto Done; } } + // // Get the intrinsic block size // - BlockSize = Media->BlockSize; + BlockSize = Media->BlockSize; if (BlockSize == 0) { Status = EFI_DEVICE_ERROR; goto Done; } - NumberOfBlocks = BufferSize / BlockSize; + NumberOfBlocks = BufferSize / BlockSize; if (!(Media->MediaPresent)) { Status = EFI_NO_MEDIA; @@ -1068,7 +1132,7 @@ ScsiDiskReadBlocksEx ( goto Done; } - if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) { + if ((Media->IoAlign > 1) && (((UINTN)Buffer & (Media->IoAlign - 1)) != 0)) { Status = EFI_INVALID_PARAMETER; goto Done; } @@ -1079,13 +1143,13 @@ ScsiDiskReadBlocksEx ( // if ((Token != NULL) && (Token->Event != NULL)) { Token->TransactionStatus = EFI_SUCCESS; - Status = ScsiDiskAsyncReadSectors ( - ScsiDiskDevice, - Buffer, - Lba, - NumberOfBlocks, - Token - ); + Status = ScsiDiskAsyncReadSectors ( + ScsiDiskDevice, + Buffer, + Lba, + NumberOfBlocks, + Token + ); } else { Status = ScsiDiskReadSectors ( ScsiDiskDevice, @@ -1126,12 +1190,12 @@ Done: EFI_STATUS EFIAPI ScsiDiskWriteBlocksEx ( - IN EFI_BLOCK_IO2_PROTOCOL *This, - IN UINT32 MediaId, - IN EFI_LBA Lba, - IN OUT EFI_BLOCK_IO2_TOKEN *Token, - IN UINTN BufferSize, - IN VOID *Buffer + IN EFI_BLOCK_IO2_PROTOCOL *This, + IN UINT32 MediaId, + IN EFI_LBA Lba, + IN OUT EFI_BLOCK_IO2_TOKEN *Token, + IN UINTN BufferSize, + IN VOID *Buffer ) { SCSI_DISK_DEV *ScsiDiskDevice; @@ -1147,8 +1211,7 @@ ScsiDiskWriteBlocksEx ( ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO2 (This); Media = ScsiDiskDevice->BlkIo.Media; - if (!IS_DEVICE_FIXED(ScsiDiskDevice)) { - + if (!IS_DEVICE_FIXED (ScsiDiskDevice)) { Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange); if (EFI_ERROR (Status)) { Status = EFI_DEVICE_ERROR; @@ -1157,11 +1220,11 @@ ScsiDiskWriteBlocksEx ( if (MediaChange) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiBlockIoProtocolGuid, - &ScsiDiskDevice->BlkIo, - &ScsiDiskDevice->BlkIo - ); + ScsiDiskDevice->Handle, + &gEfiBlockIoProtocolGuid, + &ScsiDiskDevice->BlkIo, + &ScsiDiskDevice->BlkIo + ); gBS->ReinstallProtocolInterface ( ScsiDiskDevice->Handle, &gEfiBlockIo2ProtocolGuid, @@ -1176,33 +1239,37 @@ ScsiDiskWriteBlocksEx ( &ScsiDiskDevice->EraseBlock ); } + if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiStorageSecurityCommandProtocolGuid, - &ScsiDiskDevice->StorageSecurity, - &ScsiDiskDevice->StorageSecurity - ); + ScsiDiskDevice->Handle, + &gEfiStorageSecurityCommandProtocolGuid, + &ScsiDiskDevice->StorageSecurity, + &ScsiDiskDevice->StorageSecurity + ); } + if (Media->MediaPresent) { Status = EFI_MEDIA_CHANGED; } else { Status = EFI_NO_MEDIA; } + goto Done; } } + // // Get the intrinsic block size // - BlockSize = Media->BlockSize; + BlockSize = Media->BlockSize; if (BlockSize == 0) { Status = EFI_DEVICE_ERROR; goto Done; } - NumberOfBlocks = BufferSize / BlockSize; + NumberOfBlocks = BufferSize / BlockSize; if (!(Media->MediaPresent)) { Status = EFI_NO_MEDIA; @@ -1249,7 +1316,7 @@ ScsiDiskWriteBlocksEx ( goto Done; } - if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) { + if ((Media->IoAlign > 1) && (((UINTN)Buffer & (Media->IoAlign - 1)) != 0)) { Status = EFI_INVALID_PARAMETER; goto Done; } @@ -1260,13 +1327,13 @@ ScsiDiskWriteBlocksEx ( // if ((Token != NULL) && (Token->Event != NULL)) { Token->TransactionStatus = EFI_SUCCESS; - Status = ScsiDiskAsyncWriteSectors ( - ScsiDiskDevice, - Buffer, - Lba, - NumberOfBlocks, - Token - ); + Status = ScsiDiskAsyncWriteSectors ( + ScsiDiskDevice, + Buffer, + Lba, + NumberOfBlocks, + Token + ); } else { Status = ScsiDiskWriteSectors ( ScsiDiskDevice, @@ -1313,8 +1380,7 @@ ScsiDiskFlushBlocksEx ( ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO2 (This); Media = ScsiDiskDevice->BlkIo.Media; - if (!IS_DEVICE_FIXED(ScsiDiskDevice)) { - + if (!IS_DEVICE_FIXED (ScsiDiskDevice)) { Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange); if (EFI_ERROR (Status)) { Status = EFI_DEVICE_ERROR; @@ -1323,11 +1389,11 @@ ScsiDiskFlushBlocksEx ( if (MediaChange) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiBlockIoProtocolGuid, - &ScsiDiskDevice->BlkIo, - &ScsiDiskDevice->BlkIo - ); + ScsiDiskDevice->Handle, + &gEfiBlockIoProtocolGuid, + &ScsiDiskDevice->BlkIo, + &ScsiDiskDevice->BlkIo + ); gBS->ReinstallProtocolInterface ( ScsiDiskDevice->Handle, &gEfiBlockIo2ProtocolGuid, @@ -1342,19 +1408,22 @@ ScsiDiskFlushBlocksEx ( &ScsiDiskDevice->EraseBlock ); } + if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiStorageSecurityCommandProtocolGuid, - &ScsiDiskDevice->StorageSecurity, - &ScsiDiskDevice->StorageSecurity - ); + ScsiDiskDevice->Handle, + &gEfiStorageSecurityCommandProtocolGuid, + &ScsiDiskDevice->StorageSecurity, + &ScsiDiskDevice->StorageSecurity + ); } + if (Media->MediaPresent) { Status = EFI_MEDIA_CHANGED; } else { Status = EFI_NO_MEDIA; } + goto Done; } } @@ -1372,7 +1441,8 @@ ScsiDiskFlushBlocksEx ( // // Wait for the BlockIo2 requests queue to become empty // - while (!IsListEmpty (&ScsiDiskDevice->AsyncTaskQueue)); + while (!IsListEmpty (&ScsiDiskDevice->AsyncTaskQueue)) { + } Status = EFI_SUCCESS; @@ -1389,7 +1459,6 @@ Done: return Status; } - /** Internal helper notify function which process the result of an asynchronous SCSI UNMAP Command and signal the event passed from EraseBlocks. @@ -1412,15 +1481,15 @@ ScsiDiskAsyncUnmapNotify ( gBS->CloseEvent (Event); - EraseBlkReq = (SCSI_ERASEBLK_REQUEST *) Context; + EraseBlkReq = (SCSI_ERASEBLK_REQUEST *)Context; CommandPacket = &EraseBlkReq->CommandPacket; Token = EraseBlkReq->Token; Token->TransactionStatus = EFI_SUCCESS; Status = CheckHostAdapterStatus (CommandPacket->HostAdapterStatus); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { DEBUG (( - EFI_D_ERROR, + DEBUG_ERROR, "ScsiDiskAsyncUnmapNotify: Host adapter indicating error status 0x%x.\n", CommandPacket->HostAdapterStatus )); @@ -1430,9 +1499,9 @@ ScsiDiskAsyncUnmapNotify ( } Status = CheckTargetStatus (CommandPacket->TargetStatus); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { DEBUG (( - EFI_D_ERROR, + DEBUG_ERROR, "ScsiDiskAsyncUnmapNotify: Target indicating error status 0x%x.\n", CommandPacket->HostAdapterStatus )); @@ -1465,10 +1534,10 @@ Done: **/ EFI_STATUS ScsiDiskUnmap ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN UINT64 Lba, - IN UINTN Blocks, - IN EFI_ERASE_BLOCK_TOKEN *Token OPTIONAL + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN UINT64 Lba, + IN UINTN Blocks, + IN EFI_ERASE_BLOCK_TOKEN *Token OPTIONAL ) { EFI_SCSI_IO_PROTOCOL *ScsiIo; @@ -1494,7 +1563,7 @@ ScsiDiskUnmap ( AsyncUnmapEvent = NULL; ReturnStatus = EFI_SUCCESS; - if (Blocks / (UINTN) MaxLbaCnt > MaxBlkDespCnt) { + if (Blocks / (UINTN)MaxLbaCnt > MaxBlkDespCnt) { ReturnStatus = EFI_DEVICE_ERROR; goto Done; } @@ -1511,10 +1580,10 @@ ScsiDiskUnmap ( goto Done; } - BlkDespCnt = (UINT32) ((Blocks - 1) / MaxLbaCnt + 1); - UnmapParamListLen = (UINT16) (sizeof (EFI_SCSI_DISK_UNMAP_PARAM_LIST_HEADER) - + BlkDespCnt * sizeof (EFI_SCSI_DISK_UNMAP_BLOCK_DESP)); - UnmapParamList = AllocateZeroPool (UnmapParamListLen); + BlkDespCnt = (UINT32)((Blocks - 1) / MaxLbaCnt + 1); + UnmapParamListLen = (UINT16)(sizeof (EFI_SCSI_DISK_UNMAP_PARAM_LIST_HEADER) + + BlkDespCnt * sizeof (EFI_SCSI_DISK_UNMAP_BLOCK_DESP)); + UnmapParamList = AllocateZeroPool (UnmapParamListLen); if (UnmapParamList == NULL) { ReturnStatus = EFI_DEVICE_ERROR; goto Done; @@ -1528,12 +1597,12 @@ ScsiDiskUnmap ( if (Blocks > MaxLbaCnt) { *(UINT64 *)(&BlkDespPtr->Lba) = SwapBytes64 (Lba); *(UINT32 *)(&BlkDespPtr->BlockNum) = SwapBytes32 (MaxLbaCnt); - Blocks -= MaxLbaCnt; - Lba += MaxLbaCnt; + Blocks -= MaxLbaCnt; + Lba += MaxLbaCnt; } else { *(UINT64 *)(&BlkDespPtr->Lba) = SwapBytes64 (Lba); - *(UINT32 *)(&BlkDespPtr->BlockNum) = SwapBytes32 ((UINT32) Blocks); - Blocks = 0; + *(UINT32 *)(&BlkDespPtr->BlockNum) = SwapBytes32 ((UINT32)Blocks); + Blocks = 0; } BlkDespPtr++; @@ -1563,7 +1632,7 @@ ScsiDiskUnmap ( EraseBlkReq, &AsyncUnmapEvent ); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { ReturnStatus = EFI_DEVICE_ERROR; goto Done; } @@ -1579,7 +1648,7 @@ ScsiDiskUnmap ( CommandPacket, AsyncUnmapEvent ); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { ReturnStatus = EFI_DEVICE_ERROR; OldTpl = gBS->RaiseTPL (TPL_NOTIFY); @@ -1602,7 +1671,7 @@ ScsiDiskUnmap ( CommandPacket, NULL ); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { ReturnStatus = EFI_DEVICE_ERROR; goto Done; } @@ -1612,9 +1681,9 @@ ScsiDiskUnmap ( // Only blocking UNMAP request will reach here. // Status = CheckHostAdapterStatus (CommandPacket->HostAdapterStatus); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { DEBUG (( - EFI_D_ERROR, + DEBUG_ERROR, "ScsiDiskUnmap: Host adapter indicating error status 0x%x.\n", CommandPacket->HostAdapterStatus )); @@ -1624,9 +1693,9 @@ ScsiDiskUnmap ( } Status = CheckTargetStatus (CommandPacket->TargetStatus); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { DEBUG (( - EFI_D_ERROR, + DEBUG_ERROR, "ScsiDiskUnmap: Target indicating error status 0x%x.\n", CommandPacket->HostAdapterStatus )); @@ -1640,6 +1709,7 @@ Done: if (EraseBlkReq->CommandPacket.Cdb != NULL) { FreePool (EraseBlkReq->CommandPacket.Cdb); } + FreePool (EraseBlkReq); } @@ -1684,11 +1754,11 @@ Done: EFI_STATUS EFIAPI ScsiDiskEraseBlocks ( - IN EFI_ERASE_BLOCK_PROTOCOL *This, - IN UINT32 MediaId, - IN EFI_LBA Lba, - IN OUT EFI_ERASE_BLOCK_TOKEN *Token, - IN UINTN Size + IN EFI_ERASE_BLOCK_PROTOCOL *This, + IN UINT32 MediaId, + IN EFI_LBA Lba, + IN OUT EFI_ERASE_BLOCK_TOKEN *Token, + IN UINTN Size ) { SCSI_DISK_DEV *ScsiDiskDevice; @@ -1703,7 +1773,7 @@ ScsiDiskEraseBlocks ( OldTpl = gBS->RaiseTPL (TPL_CALLBACK); ScsiDiskDevice = SCSI_DISK_DEV_FROM_ERASEBLK (This); - if (!IS_DEVICE_FIXED(ScsiDiskDevice)) { + if (!IS_DEVICE_FIXED (ScsiDiskDevice)) { Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange); if (EFI_ERROR (Status)) { Status = EFI_DEVICE_ERROR; @@ -1712,11 +1782,11 @@ ScsiDiskEraseBlocks ( if (MediaChange) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiBlockIoProtocolGuid, - &ScsiDiskDevice->BlkIo, - &ScsiDiskDevice->BlkIo - ); + ScsiDiskDevice->Handle, + &gEfiBlockIoProtocolGuid, + &ScsiDiskDevice->BlkIo, + &ScsiDiskDevice->BlkIo + ); gBS->ReinstallProtocolInterface ( ScsiDiskDevice->Handle, &gEfiBlockIo2ProtocolGuid, @@ -1731,18 +1801,21 @@ ScsiDiskEraseBlocks ( &ScsiDiskDevice->EraseBlock ); } + if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiStorageSecurityCommandProtocolGuid, - &ScsiDiskDevice->StorageSecurity, - &ScsiDiskDevice->StorageSecurity - ); + ScsiDiskDevice->Handle, + &gEfiStorageSecurityCommandProtocolGuid, + &ScsiDiskDevice->StorageSecurity, + &ScsiDiskDevice->StorageSecurity + ); } + Status = EFI_MEDIA_CHANGED; goto Done; } } + // // Get the intrinsic block size // @@ -1768,6 +1841,7 @@ ScsiDiskEraseBlocks ( Token->TransactionStatus = EFI_SUCCESS; gBS->SignalEvent (Token->Event); } + Status = EFI_SUCCESS; goto Done; } @@ -1862,14 +1936,14 @@ Done: EFI_STATUS EFIAPI ScsiDiskReceiveData ( - IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This, - IN UINT32 MediaId OPTIONAL, - IN UINT64 Timeout, - IN UINT8 SecurityProtocolId, - IN UINT16 SecurityProtocolSpecificData, - IN UINTN PayloadBufferSize, - OUT VOID *PayloadBuffer, - OUT UINTN *PayloadTransferSize + IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This, + IN UINT32 MediaId OPTIONAL, + IN UINT64 Timeout, + IN UINT8 SecurityProtocolId, + IN UINT16 SecurityProtocolSpecificData, + IN UINTN PayloadBufferSize, + OUT VOID *PayloadBuffer, + OUT UINTN *PayloadTransferSize ) { SCSI_DISK_DEV *ScsiDiskDevice; @@ -1883,14 +1957,14 @@ ScsiDiskReceiveData ( VOID *AlignedBuffer; BOOLEAN AlignedBufferAllocated; - AlignedBuffer = NULL; - MediaChange = FALSE; - AlignedBufferAllocated = FALSE; - OldTpl = gBS->RaiseTPL (TPL_CALLBACK); - ScsiDiskDevice = SCSI_DISK_DEV_FROM_STORSEC (This); - Media = ScsiDiskDevice->BlkIo.Media; + AlignedBuffer = NULL; + MediaChange = FALSE; + AlignedBufferAllocated = FALSE; + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + ScsiDiskDevice = SCSI_DISK_DEV_FROM_STORSEC (This); + Media = ScsiDiskDevice->BlkIo.Media; - SenseDataLength = (UINT8) (ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); + SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); if (!IS_DEVICE_FIXED (ScsiDiskDevice)) { Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange); @@ -1901,11 +1975,11 @@ ScsiDiskReceiveData ( if (MediaChange) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiBlockIoProtocolGuid, - &ScsiDiskDevice->BlkIo, - &ScsiDiskDevice->BlkIo - ); + ScsiDiskDevice->Handle, + &gEfiBlockIoProtocolGuid, + &ScsiDiskDevice->BlkIo, + &ScsiDiskDevice->BlkIo + ); gBS->ReinstallProtocolInterface ( ScsiDiskDevice->Handle, &gEfiBlockIo2ProtocolGuid, @@ -1920,19 +1994,22 @@ ScsiDiskReceiveData ( &ScsiDiskDevice->EraseBlock ); } + if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiStorageSecurityCommandProtocolGuid, - &ScsiDiskDevice->StorageSecurity, - &ScsiDiskDevice->StorageSecurity - ); + ScsiDiskDevice->Handle, + &gEfiStorageSecurityCommandProtocolGuid, + &ScsiDiskDevice->StorageSecurity, + &ScsiDiskDevice->StorageSecurity + ); } + if (Media->MediaPresent) { Status = EFI_MEDIA_CHANGED; } else { Status = EFI_NO_MEDIA; } + goto Done; } } @@ -1962,6 +2039,7 @@ ScsiDiskReceiveData ( Status = EFI_OUT_OF_RESOURCES; goto Done; } + ZeroMem (AlignedBuffer, PayloadBufferSize); AlignedBufferAllocated = TRUE; } else { @@ -1970,19 +2048,19 @@ ScsiDiskReceiveData ( } Status = ScsiSecurityProtocolInCommand ( - ScsiDiskDevice->ScsiIo, - Timeout, - ScsiDiskDevice->SenseData, - &SenseDataLength, - &HostAdapterStatus, - &TargetStatus, - SecurityProtocolId, - SecurityProtocolSpecificData, - FALSE, - PayloadBufferSize, - AlignedBuffer, - PayloadTransferSize - ); + ScsiDiskDevice->ScsiIo, + Timeout, + ScsiDiskDevice->SenseData, + &SenseDataLength, + &HostAdapterStatus, + &TargetStatus, + SecurityProtocolId, + SecurityProtocolSpecificData, + FALSE, + PayloadBufferSize, + AlignedBuffer, + PayloadTransferSize + ); if (EFI_ERROR (Status)) { goto Done; } @@ -2011,6 +2089,7 @@ Done: ZeroMem (AlignedBuffer, PayloadBufferSize); FreeAlignedBuffer (AlignedBuffer, PayloadBufferSize); } + gBS->RestoreTPL (OldTpl); return Status; } @@ -2073,13 +2152,13 @@ Done: EFI_STATUS EFIAPI ScsiDiskSendData ( - IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This, - IN UINT32 MediaId OPTIONAL, - IN UINT64 Timeout, - IN UINT8 SecurityProtocolId, - IN UINT16 SecurityProtocolSpecificData, - IN UINTN PayloadBufferSize, - OUT VOID *PayloadBuffer + IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This, + IN UINT32 MediaId OPTIONAL, + IN UINT64 Timeout, + IN UINT8 SecurityProtocolId, + IN UINT16 SecurityProtocolSpecificData, + IN UINTN PayloadBufferSize, + OUT VOID *PayloadBuffer ) { SCSI_DISK_DEV *ScsiDiskDevice; @@ -2093,14 +2172,14 @@ ScsiDiskSendData ( VOID *AlignedBuffer; BOOLEAN AlignedBufferAllocated; - AlignedBuffer = NULL; - MediaChange = FALSE; - AlignedBufferAllocated = FALSE; - OldTpl = gBS->RaiseTPL (TPL_CALLBACK); - ScsiDiskDevice = SCSI_DISK_DEV_FROM_STORSEC (This); - Media = ScsiDiskDevice->BlkIo.Media; + AlignedBuffer = NULL; + MediaChange = FALSE; + AlignedBufferAllocated = FALSE; + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + ScsiDiskDevice = SCSI_DISK_DEV_FROM_STORSEC (This); + Media = ScsiDiskDevice->BlkIo.Media; - SenseDataLength = (UINT8) (ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); + SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); if (!IS_DEVICE_FIXED (ScsiDiskDevice)) { Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange); @@ -2111,11 +2190,11 @@ ScsiDiskSendData ( if (MediaChange) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiBlockIoProtocolGuid, - &ScsiDiskDevice->BlkIo, - &ScsiDiskDevice->BlkIo - ); + ScsiDiskDevice->Handle, + &gEfiBlockIoProtocolGuid, + &ScsiDiskDevice->BlkIo, + &ScsiDiskDevice->BlkIo + ); gBS->ReinstallProtocolInterface ( ScsiDiskDevice->Handle, &gEfiBlockIo2ProtocolGuid, @@ -2130,19 +2209,22 @@ ScsiDiskSendData ( &ScsiDiskDevice->EraseBlock ); } + if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) { gBS->ReinstallProtocolInterface ( - ScsiDiskDevice->Handle, - &gEfiStorageSecurityCommandProtocolGuid, - &ScsiDiskDevice->StorageSecurity, - &ScsiDiskDevice->StorageSecurity - ); + ScsiDiskDevice->Handle, + &gEfiStorageSecurityCommandProtocolGuid, + &ScsiDiskDevice->StorageSecurity, + &ScsiDiskDevice->StorageSecurity + ); } + if (Media->MediaPresent) { Status = EFI_MEDIA_CHANGED; } else { Status = EFI_NO_MEDIA; } + goto Done; } } @@ -2177,6 +2259,7 @@ ScsiDiskSendData ( Status = EFI_OUT_OF_RESOURCES; goto Done; } + CopyMem (AlignedBuffer, PayloadBuffer, PayloadBufferSize); AlignedBufferAllocated = TRUE; } else { @@ -2185,18 +2268,18 @@ ScsiDiskSendData ( } Status = ScsiSecurityProtocolOutCommand ( - ScsiDiskDevice->ScsiIo, - Timeout, - ScsiDiskDevice->SenseData, - &SenseDataLength, - &HostAdapterStatus, - &TargetStatus, - SecurityProtocolId, - SecurityProtocolSpecificData, - FALSE, - PayloadBufferSize, - AlignedBuffer - ); + ScsiDiskDevice->ScsiIo, + Timeout, + ScsiDiskDevice->SenseData, + &SenseDataLength, + &HostAdapterStatus, + &TargetStatus, + SecurityProtocolId, + SecurityProtocolSpecificData, + FALSE, + PayloadBufferSize, + AlignedBuffer + ); if (EFI_ERROR (Status)) { goto Done; } @@ -2216,11 +2299,11 @@ Done: ZeroMem (AlignedBuffer, PayloadBufferSize); FreeAlignedBuffer (AlignedBuffer, PayloadBufferSize); } + gBS->RestoreTPL (OldTpl); return Status; } - /** Detect Device and read out capacity ,if error occurs, parse the sense key. @@ -2234,31 +2317,31 @@ Done: **/ EFI_STATUS ScsiDiskDetectMedia ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN BOOLEAN MustReadCapacity, - OUT BOOLEAN *MediaChange + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN BOOLEAN MustReadCapacity, + OUT BOOLEAN *MediaChange ) { - EFI_STATUS Status; - EFI_SCSI_SENSE_DATA *SenseData; - UINTN NumberOfSenseKeys; - BOOLEAN NeedRetry; - BOOLEAN NeedReadCapacity; - UINT8 Retry; - UINT8 MaxRetry; - EFI_BLOCK_IO_MEDIA OldMedia; - UINTN Action; - EFI_EVENT TimeoutEvt; - - Status = EFI_SUCCESS; - SenseData = NULL; - NumberOfSenseKeys = 0; - Retry = 0; - MaxRetry = 3; - Action = ACTION_NO_ACTION; - NeedReadCapacity = FALSE; - *MediaChange = FALSE; - TimeoutEvt = NULL; + EFI_STATUS Status; + EFI_SCSI_SENSE_DATA *SenseData; + UINTN NumberOfSenseKeys; + BOOLEAN NeedRetry; + BOOLEAN NeedReadCapacity; + UINT8 Retry; + UINT8 MaxRetry; + EFI_BLOCK_IO_MEDIA OldMedia; + UINTN Action; + EFI_EVENT TimeoutEvt; + + Status = EFI_SUCCESS; + SenseData = NULL; + NumberOfSenseKeys = 0; + Retry = 0; + MaxRetry = 3; + Action = ACTION_NO_ACTION; + NeedReadCapacity = FALSE; + *MediaChange = FALSE; + TimeoutEvt = NULL; CopyMem (&OldMedia, ScsiDiskDevice->BlkIo.Media, sizeof (OldMedia)); @@ -2273,7 +2356,7 @@ ScsiDiskDetectMedia ( return Status; } - Status = gBS->SetTimer (TimeoutEvt, TimerRelative, EFI_TIMER_PERIOD_SECONDS(120)); + Status = gBS->SetTimer (TimeoutEvt, TimerRelative, EFI_TIMER_PERIOD_SECONDS (120)); if (EFI_ERROR (Status)) { goto EXIT; } @@ -2285,11 +2368,11 @@ ScsiDiskDetectMedia ( // while (EFI_ERROR (gBS->CheckEvent (TimeoutEvt))) { Status = ScsiDiskTestUnitReady ( - ScsiDiskDevice, - &NeedRetry, - &SenseData, - &NumberOfSenseKeys - ); + ScsiDiskDevice, + &NeedRetry, + &SenseData, + &NumberOfSenseKeys + ); if (!EFI_ERROR (Status)) { Status = DetectMediaParsingSenseKeys ( ScsiDiskDevice, @@ -2328,8 +2411,8 @@ ScsiDiskDetectMedia ( // READ_CAPACITY command is not supported by any of the UFS WLUNs. // if (ScsiDiskDevice->DeviceType == EFI_SCSI_TYPE_WLUN) { - NeedReadCapacity = FALSE; - MustReadCapacity = FALSE; + NeedReadCapacity = FALSE; + MustReadCapacity = FALSE; ScsiDiskDevice->BlkIo.Media->MediaPresent = TRUE; } @@ -2391,17 +2474,17 @@ ScsiDiskDetectMedia ( } if (ScsiDiskDevice->BlkIo.Media->ReadOnly != OldMedia.ReadOnly) { - *MediaChange = TRUE; + *MediaChange = TRUE; ScsiDiskDevice->BlkIo.Media->MediaId += 1; } if (ScsiDiskDevice->BlkIo.Media->BlockSize != OldMedia.BlockSize) { - *MediaChange = TRUE; + *MediaChange = TRUE; ScsiDiskDevice->BlkIo.Media->MediaId += 1; } if (ScsiDiskDevice->BlkIo.Media->LastBlock != OldMedia.LastBlock) { - *MediaChange = TRUE; + *MediaChange = TRUE; ScsiDiskDevice->BlkIo.Media->MediaId += 1; } @@ -2425,10 +2508,10 @@ EXIT: if (TimeoutEvt != NULL) { gBS->CloseEvent (TimeoutEvt); } + return Status; } - /** Send out Inquiry command to Device. @@ -2441,40 +2524,40 @@ EXIT: **/ EFI_STATUS ScsiDiskInquiryDevice ( - IN OUT SCSI_DISK_DEV *ScsiDiskDevice, - OUT BOOLEAN *NeedRetry + IN OUT SCSI_DISK_DEV *ScsiDiskDevice, + OUT BOOLEAN *NeedRetry ) { - UINT32 InquiryDataLength; - UINT8 SenseDataLength; - UINT8 HostAdapterStatus; - UINT8 TargetStatus; - EFI_SCSI_SENSE_DATA *SenseDataArray; - UINTN NumberOfSenseKeys; - EFI_STATUS Status; - UINT8 MaxRetry; - UINT8 Index; - EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE *SupportedVpdPages; - EFI_SCSI_BLOCK_LIMITS_VPD_PAGE *BlockLimits; - UINTN PageLength; + UINT32 InquiryDataLength; + UINT8 SenseDataLength; + UINT8 HostAdapterStatus; + UINT8 TargetStatus; + EFI_SCSI_SENSE_DATA *SenseDataArray; + UINTN NumberOfSenseKeys; + EFI_STATUS Status; + UINT8 MaxRetry; + UINT8 Index; + EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE *SupportedVpdPages; + EFI_SCSI_BLOCK_LIMITS_VPD_PAGE *BlockLimits; + UINTN PageLength; InquiryDataLength = sizeof (EFI_SCSI_INQUIRY_DATA); SenseDataLength = 0; Status = ScsiInquiryCommand ( - ScsiDiskDevice->ScsiIo, - SCSI_DISK_TIMEOUT, - NULL, - &SenseDataLength, - &HostAdapterStatus, - &TargetStatus, - (VOID *) &(ScsiDiskDevice->InquiryData), - &InquiryDataLength, - FALSE - ); - // - // no need to check HostAdapterStatus and TargetStatus - // + ScsiDiskDevice->ScsiIo, + SCSI_DISK_TIMEOUT, + NULL, + &SenseDataLength, + &HostAdapterStatus, + &TargetStatus, + (VOID *)&(ScsiDiskDevice->InquiryData), + &InquiryDataLength, + FALSE + ); + // + // no need to check HostAdapterStatus and TargetStatus + // if ((Status == EFI_SUCCESS) || (Status == EFI_WARN_BUFFER_TOO_SMALL)) { ParseInquiryData (ScsiDiskDevice); @@ -2487,41 +2570,49 @@ ScsiDiskInquiryDevice ( *NeedRetry = FALSE; return EFI_DEVICE_ERROR; } + ZeroMem (SupportedVpdPages, sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE)); InquiryDataLength = sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE); SenseDataLength = 0; - Status = ScsiInquiryCommandEx ( - ScsiDiskDevice->ScsiIo, - SCSI_DISK_TIMEOUT, - NULL, - &SenseDataLength, - &HostAdapterStatus, - &TargetStatus, - (VOID *) SupportedVpdPages, - &InquiryDataLength, - TRUE, - EFI_SCSI_PAGE_CODE_SUPPORTED_VPD - ); + Status = ScsiInquiryCommandEx ( + ScsiDiskDevice->ScsiIo, + SCSI_DISK_TIMEOUT, + NULL, + &SenseDataLength, + &HostAdapterStatus, + &TargetStatus, + (VOID *)SupportedVpdPages, + &InquiryDataLength, + TRUE, + EFI_SCSI_PAGE_CODE_SUPPORTED_VPD + ); if (!EFI_ERROR (Status)) { PageLength = (SupportedVpdPages->PageLength2 << 8) - | SupportedVpdPages->PageLength1; + | SupportedVpdPages->PageLength1; // // Sanity checks for coping with broken devices // if (PageLength > sizeof SupportedVpdPages->SupportedVpdPageList) { - DEBUG ((EFI_D_WARN, + DEBUG (( + DEBUG_WARN, "%a: invalid PageLength (%u) in Supported VPD Pages page\n", - __FUNCTION__, (UINT32)PageLength)); + __FUNCTION__, + (UINT32)PageLength + )); PageLength = 0; } if ((PageLength > 0) && (SupportedVpdPages->SupportedVpdPageList[0] != - EFI_SCSI_PAGE_CODE_SUPPORTED_VPD)) { - DEBUG ((EFI_D_WARN, + EFI_SCSI_PAGE_CODE_SUPPORTED_VPD)) + { + DEBUG (( + DEBUG_WARN, "%a: Supported VPD Pages page doesn't start with code 0x%02x\n", - __FUNCTION__, EFI_SCSI_PAGE_CODE_SUPPORTED_VPD)); + __FUNCTION__, + EFI_SCSI_PAGE_CODE_SUPPORTED_VPD + )); PageLength = 0; } @@ -2534,11 +2625,15 @@ ScsiDiskInquiryDevice ( // if ((Index > 0) && (SupportedVpdPages->SupportedVpdPageList[Index] <= - SupportedVpdPages->SupportedVpdPageList[Index - 1])) { - DEBUG ((EFI_D_WARN, + SupportedVpdPages->SupportedVpdPageList[Index - 1])) + { + DEBUG (( + DEBUG_WARN, "%a: non-ascending code in Supported VPD Pages page @ %u\n", - __FUNCTION__, Index)); - Index = 0; + __FUNCTION__, + Index + )); + Index = 0; PageLength = 0; break; } @@ -2558,25 +2653,26 @@ ScsiDiskInquiryDevice ( *NeedRetry = FALSE; return EFI_DEVICE_ERROR; } + ZeroMem (BlockLimits, sizeof (EFI_SCSI_BLOCK_LIMITS_VPD_PAGE)); InquiryDataLength = sizeof (EFI_SCSI_BLOCK_LIMITS_VPD_PAGE); SenseDataLength = 0; - Status = ScsiInquiryCommandEx ( - ScsiDiskDevice->ScsiIo, - SCSI_DISK_TIMEOUT, - NULL, - &SenseDataLength, - &HostAdapterStatus, - &TargetStatus, - (VOID *) BlockLimits, - &InquiryDataLength, - TRUE, - EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD - ); + Status = ScsiInquiryCommandEx ( + ScsiDiskDevice->ScsiIo, + SCSI_DISK_TIMEOUT, + NULL, + &SenseDataLength, + &HostAdapterStatus, + &TargetStatus, + (VOID *)BlockLimits, + &InquiryDataLength, + TRUE, + EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD + ); if (!EFI_ERROR (Status)) { ScsiDiskDevice->BlkIo.Media->OptimalTransferLengthGranularity = (BlockLimits->OptimalTransferLengthGranularity2 << 8) | - BlockLimits->OptimalTransferLengthGranularity1; + BlockLimits->OptimalTransferLengthGranularity1; ScsiDiskDevice->UnmapInfo.MaxLbaCnt = (BlockLimits->MaximumUnmapLbaCount4 << 24) | @@ -2622,15 +2718,14 @@ ScsiDiskInquiryDevice ( if (!EFI_ERROR (Status)) { return EFI_SUCCESS; - } else if (Status == EFI_NOT_READY) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; - } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) { *NeedRetry = FALSE; return EFI_DEVICE_ERROR; } + // // go ahead to check HostAdapterStatus and TargetStatus // (EFI_TIMEOUT, EFI_DEVICE_ERROR) @@ -2641,9 +2736,9 @@ ScsiDiskInquiryDevice ( *NeedRetry = TRUE; return EFI_DEVICE_ERROR; } else if (Status == EFI_DEVICE_ERROR) { - // - // reset the scsi channel - // + // + // reset the scsi channel + // ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo); *NeedRetry = FALSE; return EFI_DEVICE_ERROR; @@ -2657,7 +2752,6 @@ ScsiDiskInquiryDevice ( ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo); *NeedRetry = TRUE; return EFI_DEVICE_ERROR; - } else if (Status == EFI_DEVICE_ERROR) { *NeedRetry = FALSE; return EFI_DEVICE_ERROR; @@ -2671,12 +2765,12 @@ ScsiDiskInquiryDevice ( MaxRetry = 3; for (Index = 0; Index < MaxRetry; Index++) { Status = ScsiDiskRequestSenseKeys ( - ScsiDiskDevice, - NeedRetry, - &SenseDataArray, - &NumberOfSenseKeys, - TRUE - ); + ScsiDiskDevice, + NeedRetry, + &SenseDataArray, + &NumberOfSenseKeys, + TRUE + ); if (!EFI_ERROR (Status)) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; @@ -2686,6 +2780,7 @@ ScsiDiskInquiryDevice ( return EFI_DEVICE_ERROR; } } + // // ScsiDiskRequestSenseKeys() failed after several rounds of retry. // set *NeedRetry = FALSE to avoid the outside caller try again. @@ -2712,10 +2807,10 @@ ScsiDiskInquiryDevice ( **/ EFI_STATUS ScsiDiskTestUnitReady ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - OUT BOOLEAN *NeedRetry, - OUT EFI_SCSI_SENSE_DATA **SenseDataArray, - OUT UINTN *NumberOfSenseKeys + IN SCSI_DISK_DEV *ScsiDiskDevice, + OUT BOOLEAN *NeedRetry, + OUT EFI_SCSI_SENSE_DATA **SenseDataArray, + OUT UINTN *NumberOfSenseKeys ) { EFI_STATUS Status; @@ -2725,31 +2820,31 @@ ScsiDiskTestUnitReady ( UINT8 Index; UINT8 MaxRetry; - SenseDataLength = (UINT8) (ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); - *NumberOfSenseKeys = 0; + SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); + *NumberOfSenseKeys = 0; // // Parameter 3 and 4: do not require sense data, retrieve it when needed. // Status = ScsiTestUnitReadyCommand ( - ScsiDiskDevice->ScsiIo, - SCSI_DISK_TIMEOUT, - ScsiDiskDevice->SenseData, - &SenseDataLength, - &HostAdapterStatus, - &TargetStatus - ); + ScsiDiskDevice->ScsiIo, + SCSI_DISK_TIMEOUT, + ScsiDiskDevice->SenseData, + &SenseDataLength, + &HostAdapterStatus, + &TargetStatus + ); // // no need to check HostAdapterStatus and TargetStatus // if (Status == EFI_NOT_READY) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; - } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) { *NeedRetry = FALSE; return EFI_DEVICE_ERROR; } + // // go ahead to check HostAdapterStatus and TargetStatus(in case of EFI_DEVICE_ERROR) // @@ -2758,7 +2853,6 @@ ScsiDiskTestUnitReady ( if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; - } else if (Status == EFI_DEVICE_ERROR) { // // reset the scsi channel @@ -2776,7 +2870,6 @@ ScsiDiskTestUnitReady ( ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo); *NeedRetry = TRUE; return EFI_DEVICE_ERROR; - } else if (Status == EFI_DEVICE_ERROR) { *NeedRetry = FALSE; return EFI_DEVICE_ERROR; @@ -2791,12 +2884,12 @@ ScsiDiskTestUnitReady ( MaxRetry = 3; for (Index = 0; Index < MaxRetry; Index++) { Status = ScsiDiskRequestSenseKeys ( - ScsiDiskDevice, - NeedRetry, - SenseDataArray, - NumberOfSenseKeys, - FALSE - ); + ScsiDiskDevice, + NeedRetry, + SenseDataArray, + NumberOfSenseKeys, + FALSE + ); if (!EFI_ERROR (Status)) { return EFI_SUCCESS; } @@ -2805,6 +2898,7 @@ ScsiDiskTestUnitReady ( return EFI_DEVICE_ERROR; } } + // // ScsiDiskRequestSenseKeys() failed after several rounds of retry. // set *NeedRetry = FALSE to avoid the outside caller try again. @@ -2827,13 +2921,13 @@ ScsiDiskTestUnitReady ( **/ EFI_STATUS DetectMediaParsingSenseKeys ( - OUT SCSI_DISK_DEV *ScsiDiskDevice, - IN EFI_SCSI_SENSE_DATA *SenseData, - IN UINTN NumberOfSenseKeys, - OUT UINTN *Action + OUT SCSI_DISK_DEV *ScsiDiskDevice, + IN EFI_SCSI_SENSE_DATA *SenseData, + IN UINTN NumberOfSenseKeys, + OUT UINTN *Action ) { - BOOLEAN RetryLater; + BOOLEAN RetryLater; // // Default is to read capacity, unless.. @@ -2844,6 +2938,7 @@ DetectMediaParsingSenseKeys ( if (ScsiDiskDevice->BlkIo.Media->MediaPresent == TRUE) { *Action = ACTION_NO_ACTION; } + return EFI_SUCCESS; } @@ -2854,37 +2949,38 @@ DetectMediaParsingSenseKeys ( if (ScsiDiskDevice->BlkIo.Media->MediaPresent == TRUE) { *Action = ACTION_NO_ACTION; } + return EFI_SUCCESS; } if (ScsiDiskIsNoMedia (SenseData, NumberOfSenseKeys)) { ScsiDiskDevice->BlkIo.Media->MediaPresent = FALSE; ScsiDiskDevice->BlkIo.Media->LastBlock = 0; - *Action = ACTION_NO_ACTION; - DEBUG ((EFI_D_VERBOSE, "ScsiDisk: ScsiDiskIsNoMedia\n")); + *Action = ACTION_NO_ACTION; + DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskIsNoMedia\n")); return EFI_SUCCESS; } if (ScsiDiskIsMediaChange (SenseData, NumberOfSenseKeys)) { ScsiDiskDevice->BlkIo.Media->MediaId++; - DEBUG ((EFI_D_VERBOSE, "ScsiDisk: ScsiDiskIsMediaChange!\n")); + DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskIsMediaChange!\n")); return EFI_SUCCESS; } if (ScsiDiskIsResetBefore (SenseData, NumberOfSenseKeys)) { *Action = ACTION_RETRY_COMMAND_LATER; - DEBUG ((EFI_D_VERBOSE, "ScsiDisk: ScsiDiskIsResetBefore!\n")); + DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskIsResetBefore!\n")); return EFI_SUCCESS; } if (ScsiDiskIsMediaError (SenseData, NumberOfSenseKeys)) { - DEBUG ((EFI_D_VERBOSE, "ScsiDisk: ScsiDiskIsMediaError\n")); + DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskIsMediaError\n")); *Action = ACTION_RETRY_WITH_BACKOFF_ALGO; return EFI_DEVICE_ERROR; } if (ScsiDiskIsHardwareError (SenseData, NumberOfSenseKeys)) { - DEBUG ((EFI_D_VERBOSE, "ScsiDisk: ScsiDiskIsHardwareError\n")); + DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskIsHardwareError\n")); *Action = ACTION_RETRY_WITH_BACKOFF_ALGO; return EFI_DEVICE_ERROR; } @@ -2892,19 +2988,19 @@ DetectMediaParsingSenseKeys ( if (!ScsiDiskIsDriveReady (SenseData, NumberOfSenseKeys, &RetryLater)) { if (RetryLater) { *Action = ACTION_RETRY_COMMAND_LATER; - DEBUG ((EFI_D_VERBOSE, "ScsiDisk: ScsiDiskDriveNotReady!\n")); + DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskDriveNotReady!\n")); return EFI_SUCCESS; } + *Action = ACTION_NO_ACTION; return EFI_DEVICE_ERROR; } *Action = ACTION_RETRY_WITH_BACKOFF_ALGO; - DEBUG ((EFI_D_VERBOSE, "ScsiDisk: Sense Key = 0x%x ASC = 0x%x!\n", SenseData->Sense_Key, SenseData->Addnl_Sense_Code)); + DEBUG ((DEBUG_VERBOSE, "ScsiDisk: Sense Key = 0x%x ASC = 0x%x!\n", SenseData->Sense_Key, SenseData->Addnl_Sense_Code)); return EFI_SUCCESS; } - /** Send read capacity command to device and get the device parameter. @@ -2919,29 +3015,30 @@ DetectMediaParsingSenseKeys ( **/ EFI_STATUS ScsiDiskReadCapacity ( - IN OUT SCSI_DISK_DEV *ScsiDiskDevice, - OUT BOOLEAN *NeedRetry, - OUT EFI_SCSI_SENSE_DATA **SenseDataArray, - OUT UINTN *NumberOfSenseKeys + IN OUT SCSI_DISK_DEV *ScsiDiskDevice, + OUT BOOLEAN *NeedRetry, + OUT EFI_SCSI_SENSE_DATA **SenseDataArray, + OUT UINTN *NumberOfSenseKeys ) { - UINT8 HostAdapterStatus; - UINT8 TargetStatus; - EFI_STATUS CommandStatus; - EFI_STATUS Status; - UINT8 Index; - UINT8 MaxRetry; - UINT8 SenseDataLength; - UINT32 DataLength10; - UINT32 DataLength16; - EFI_SCSI_DISK_CAPACITY_DATA *CapacityData10; - EFI_SCSI_DISK_CAPACITY_DATA16 *CapacityData16; + UINT8 HostAdapterStatus; + UINT8 TargetStatus; + EFI_STATUS CommandStatus; + EFI_STATUS Status; + UINT8 Index; + UINT8 MaxRetry; + UINT8 SenseDataLength; + UINT32 DataLength10; + UINT32 DataLength16; + EFI_SCSI_DISK_CAPACITY_DATA *CapacityData10; + EFI_SCSI_DISK_CAPACITY_DATA16 *CapacityData16; CapacityData10 = AllocateAlignedBuffer (ScsiDiskDevice, sizeof (EFI_SCSI_DISK_CAPACITY_DATA)); if (CapacityData10 == NULL) { *NeedRetry = FALSE; return EFI_DEVICE_ERROR; } + CapacityData16 = AllocateAlignedBuffer (ScsiDiskDevice, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16)); if (CapacityData16 == NULL) { FreeAlignedBuffer (CapacityData10, sizeof (EFI_SCSI_DISK_CAPACITY_DATA)); @@ -2949,14 +3046,14 @@ ScsiDiskReadCapacity ( return EFI_DEVICE_ERROR; } - SenseDataLength = 0; - DataLength10 = sizeof (EFI_SCSI_DISK_CAPACITY_DATA); - DataLength16 = sizeof (EFI_SCSI_DISK_CAPACITY_DATA16); + SenseDataLength = 0; + DataLength10 = sizeof (EFI_SCSI_DISK_CAPACITY_DATA); + DataLength16 = sizeof (EFI_SCSI_DISK_CAPACITY_DATA16); ZeroMem (CapacityData10, sizeof (EFI_SCSI_DISK_CAPACITY_DATA)); ZeroMem (CapacityData16, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16)); - *NumberOfSenseKeys = 0; - *NeedRetry = FALSE; + *NumberOfSenseKeys = 0; + *NeedRetry = FALSE; // // submit Read Capacity(10) Command. If it returns capacity of FFFFFFFFh, @@ -2969,14 +3066,15 @@ ScsiDiskReadCapacity ( &SenseDataLength, &HostAdapterStatus, &TargetStatus, - (VOID *) CapacityData10, + (VOID *)CapacityData10, &DataLength10, FALSE ); ScsiDiskDevice->Cdb16Byte = FALSE; if ((!EFI_ERROR (CommandStatus)) && (CapacityData10->LastLba3 == 0xff) && (CapacityData10->LastLba2 == 0xff) && - (CapacityData10->LastLba1 == 0xff) && (CapacityData10->LastLba0 == 0xff)) { + (CapacityData10->LastLba1 == 0xff) && (CapacityData10->LastLba0 == 0xff)) + { // // use Read Capacity (16), Read (16) and Write (16) next when hard disk size > 2TB // @@ -2992,44 +3090,43 @@ ScsiDiskReadCapacity ( &SenseDataLength, &HostAdapterStatus, &TargetStatus, - (VOID *) CapacityData16, + (VOID *)CapacityData16, &DataLength16, FALSE ); } - // - // no need to check HostAdapterStatus and TargetStatus - // - if (CommandStatus == EFI_SUCCESS) { - GetMediaInfo (ScsiDiskDevice, CapacityData10, CapacityData16); - FreeAlignedBuffer (CapacityData10, sizeof (EFI_SCSI_DISK_CAPACITY_DATA)); - FreeAlignedBuffer (CapacityData16, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16)); - return EFI_SUCCESS; - } - - FreeAlignedBuffer (CapacityData10, sizeof (EFI_SCSI_DISK_CAPACITY_DATA)); - FreeAlignedBuffer (CapacityData16, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16)); - - if (CommandStatus == EFI_NOT_READY) { - *NeedRetry = TRUE; - return EFI_DEVICE_ERROR; - } else if ((CommandStatus == EFI_INVALID_PARAMETER) || (CommandStatus == EFI_UNSUPPORTED)) { - *NeedRetry = FALSE; - return EFI_DEVICE_ERROR; - } - - // - // go ahead to check HostAdapterStatus and TargetStatus - // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL) - // - - Status = CheckHostAdapterStatus (HostAdapterStatus); - if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) { - *NeedRetry = TRUE; - return EFI_DEVICE_ERROR; - - } else if (Status == EFI_DEVICE_ERROR) { + // + // no need to check HostAdapterStatus and TargetStatus + // + if (CommandStatus == EFI_SUCCESS) { + GetMediaInfo (ScsiDiskDevice, CapacityData10, CapacityData16); + FreeAlignedBuffer (CapacityData10, sizeof (EFI_SCSI_DISK_CAPACITY_DATA)); + FreeAlignedBuffer (CapacityData16, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16)); + return EFI_SUCCESS; + } + + FreeAlignedBuffer (CapacityData10, sizeof (EFI_SCSI_DISK_CAPACITY_DATA)); + FreeAlignedBuffer (CapacityData16, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16)); + + if (CommandStatus == EFI_NOT_READY) { + *NeedRetry = TRUE; + return EFI_DEVICE_ERROR; + } else if ((CommandStatus == EFI_INVALID_PARAMETER) || (CommandStatus == EFI_UNSUPPORTED)) { + *NeedRetry = FALSE; + return EFI_DEVICE_ERROR; + } + + // + // go ahead to check HostAdapterStatus and TargetStatus + // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL) + // + + Status = CheckHostAdapterStatus (HostAdapterStatus); + if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) { + *NeedRetry = TRUE; + return EFI_DEVICE_ERROR; + } else if (Status == EFI_DEVICE_ERROR) { // // reset the scsi channel // @@ -3046,7 +3143,6 @@ ScsiDiskReadCapacity ( ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo); *NeedRetry = TRUE; return EFI_DEVICE_ERROR; - } else if (Status == EFI_DEVICE_ERROR) { *NeedRetry = FALSE; return EFI_DEVICE_ERROR; @@ -3059,14 +3155,13 @@ ScsiDiskReadCapacity ( // MaxRetry = 3; for (Index = 0; Index < MaxRetry; Index++) { - Status = ScsiDiskRequestSenseKeys ( - ScsiDiskDevice, - NeedRetry, - SenseDataArray, - NumberOfSenseKeys, - TRUE - ); + ScsiDiskDevice, + NeedRetry, + SenseDataArray, + NumberOfSenseKeys, + TRUE + ); if (!EFI_ERROR (Status)) { return EFI_SUCCESS; } @@ -3075,6 +3170,7 @@ ScsiDiskReadCapacity ( return EFI_DEVICE_ERROR; } } + // // ScsiDiskRequestSenseKeys() failed after several rounds of retry. // set *NeedRetry = FALSE to avoid the outside caller try again. @@ -3096,35 +3192,34 @@ ScsiDiskReadCapacity ( **/ EFI_STATUS CheckHostAdapterStatus ( - IN UINT8 HostAdapterStatus + IN UINT8 HostAdapterStatus ) { switch (HostAdapterStatus) { - case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OK: - return EFI_SUCCESS; + case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OK: + return EFI_SUCCESS; - case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_SELECTION_TIMEOUT: - case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT: - case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT_COMMAND: - return EFI_TIMEOUT; + case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_SELECTION_TIMEOUT: + case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT: + case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT_COMMAND: + return EFI_TIMEOUT; - case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_MESSAGE_REJECT: - case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PARITY_ERROR: - case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_REQUEST_SENSE_FAILED: - case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_DATA_OVERRUN_UNDERRUN: - case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_RESET: - return EFI_NOT_READY; + case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_MESSAGE_REJECT: + case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PARITY_ERROR: + case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_REQUEST_SENSE_FAILED: + case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_DATA_OVERRUN_UNDERRUN: + case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_RESET: + return EFI_NOT_READY; - case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_FREE: - case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR: - return EFI_DEVICE_ERROR; + case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_FREE: + case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR: + return EFI_DEVICE_ERROR; - default: - return EFI_SUCCESS; + default: + return EFI_SUCCESS; } } - /** Check the target status and re-interpret it in EFI_STATUS. @@ -3137,36 +3232,35 @@ CheckHostAdapterStatus ( **/ EFI_STATUS CheckTargetStatus ( - IN UINT8 TargetStatus + IN UINT8 TargetStatus ) { switch (TargetStatus) { - case EFI_EXT_SCSI_STATUS_TARGET_GOOD: - case EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION: - case EFI_EXT_SCSI_STATUS_TARGET_CONDITION_MET: - return EFI_SUCCESS; + case EFI_EXT_SCSI_STATUS_TARGET_GOOD: + case EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION: + case EFI_EXT_SCSI_STATUS_TARGET_CONDITION_MET: + return EFI_SUCCESS; - case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE: - case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE_CONDITION_MET: - case EFI_EXT_SCSI_STATUS_TARGET_BUSY: - case EFI_EXT_SCSI_STATUS_TARGET_TASK_SET_FULL: - return EFI_NOT_READY; + case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE: + case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE_CONDITION_MET: + case EFI_EXT_SCSI_STATUS_TARGET_BUSY: + case EFI_EXT_SCSI_STATUS_TARGET_TASK_SET_FULL: + return EFI_NOT_READY; - case EFI_EXT_SCSI_STATUS_TARGET_RESERVATION_CONFLICT: - return EFI_DEVICE_ERROR; + case EFI_EXT_SCSI_STATUS_TARGET_RESERVATION_CONFLICT: + return EFI_DEVICE_ERROR; - default: - return EFI_SUCCESS; + default: + return EFI_SUCCESS; } } - /** Retrieve all sense keys from the device. When encountering error during the process, if retrieve sense keys before error encountered, it returns the sense keys with return status set to EFI_SUCCESS, - and NeedRetry set to FALSE; otherwize, return the proper return status. + and NeedRetry set to FALSE; otherwise, return the proper return status. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV @param NeedRetry The pointer of flag indicates if need a retry @@ -3180,33 +3274,33 @@ CheckTargetStatus ( **/ EFI_STATUS ScsiDiskRequestSenseKeys ( - IN OUT SCSI_DISK_DEV *ScsiDiskDevice, - OUT BOOLEAN *NeedRetry, - OUT EFI_SCSI_SENSE_DATA **SenseDataArray, - OUT UINTN *NumberOfSenseKeys, - IN BOOLEAN AskResetIfError + IN OUT SCSI_DISK_DEV *ScsiDiskDevice, + OUT BOOLEAN *NeedRetry, + OUT EFI_SCSI_SENSE_DATA **SenseDataArray, + OUT UINTN *NumberOfSenseKeys, + IN BOOLEAN AskResetIfError ) { - EFI_SCSI_SENSE_DATA *PtrSenseData; - UINT8 SenseDataLength; - BOOLEAN SenseReq; - EFI_STATUS Status; - EFI_STATUS FallStatus; - UINT8 HostAdapterStatus; - UINT8 TargetStatus; + EFI_SCSI_SENSE_DATA *PtrSenseData; + UINT8 SenseDataLength; + BOOLEAN SenseReq; + EFI_STATUS Status; + EFI_STATUS FallStatus; + UINT8 HostAdapterStatus; + UINT8 TargetStatus; FallStatus = EFI_SUCCESS; - SenseDataLength = (UINT8) sizeof (EFI_SCSI_SENSE_DATA); + SenseDataLength = (UINT8)sizeof (EFI_SCSI_SENSE_DATA); ZeroMem ( ScsiDiskDevice->SenseData, sizeof (EFI_SCSI_SENSE_DATA) * (ScsiDiskDevice->SenseDataNumber) ); - *NumberOfSenseKeys = 0; - *SenseDataArray = ScsiDiskDevice->SenseData; - Status = EFI_SUCCESS; - PtrSenseData = AllocateAlignedBuffer (ScsiDiskDevice, sizeof (EFI_SCSI_SENSE_DATA)); + *NumberOfSenseKeys = 0; + *SenseDataArray = ScsiDiskDevice->SenseData; + Status = EFI_SUCCESS; + PtrSenseData = AllocateAlignedBuffer (ScsiDiskDevice, sizeof (EFI_SCSI_SENSE_DATA)); if (PtrSenseData == NULL) { return EFI_DEVICE_ERROR; } @@ -3214,36 +3308,33 @@ ScsiDiskRequestSenseKeys ( for (SenseReq = TRUE; SenseReq;) { ZeroMem (PtrSenseData, sizeof (EFI_SCSI_SENSE_DATA)); Status = ScsiRequestSenseCommand ( - ScsiDiskDevice->ScsiIo, - SCSI_DISK_TIMEOUT, - PtrSenseData, - &SenseDataLength, - &HostAdapterStatus, - &TargetStatus - ); - if ((Status == EFI_SUCCESS) || (Status == EFI_WARN_BUFFER_TOO_SMALL)) { - FallStatus = EFI_SUCCESS; - - } else if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) { - *NeedRetry = TRUE; - FallStatus = EFI_DEVICE_ERROR; - - } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) { - *NeedRetry = FALSE; - FallStatus = EFI_DEVICE_ERROR; - - } else if (Status == EFI_DEVICE_ERROR) { - if (AskResetIfError) { - ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo); - } + ScsiDiskDevice->ScsiIo, + SCSI_DISK_TIMEOUT, + PtrSenseData, + &SenseDataLength, + &HostAdapterStatus, + &TargetStatus + ); + if ((Status == EFI_SUCCESS) || (Status == EFI_WARN_BUFFER_TOO_SMALL)) { + FallStatus = EFI_SUCCESS; + } else if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) { + *NeedRetry = TRUE; + FallStatus = EFI_DEVICE_ERROR; + } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) { + *NeedRetry = FALSE; + FallStatus = EFI_DEVICE_ERROR; + } else if (Status == EFI_DEVICE_ERROR) { + if (AskResetIfError) { + ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo); + } - FallStatus = EFI_DEVICE_ERROR; + FallStatus = EFI_DEVICE_ERROR; } if (EFI_ERROR (FallStatus)) { if (*NumberOfSenseKeys != 0) { *NeedRetry = FALSE; - Status = EFI_SUCCESS; + Status = EFI_SUCCESS; goto EXIT; } else { Status = EFI_DEVICE_ERROR; @@ -3259,7 +3350,8 @@ ScsiDiskRequestSenseKeys ( // skip the loop. // if ((PtrSenseData->Sense_Key == EFI_SCSI_SK_NO_SENSE) || - (*NumberOfSenseKeys == ScsiDiskDevice->SenseDataNumber)) { + (*NumberOfSenseKeys == ScsiDiskDevice->SenseDataNumber)) + { SenseReq = FALSE; } } @@ -3269,7 +3361,6 @@ EXIT: return Status; } - /** Get information from media read capacity command. @@ -3285,25 +3376,25 @@ GetMediaInfo ( IN EFI_SCSI_DISK_CAPACITY_DATA16 *Capacity16 ) { - UINT8 *Ptr; + UINT8 *Ptr; if (!ScsiDiskDevice->Cdb16Byte) { - ScsiDiskDevice->BlkIo.Media->LastBlock = ((UINT32) Capacity10->LastLba3 << 24) | - (Capacity10->LastLba2 << 16) | - (Capacity10->LastLba1 << 8) | - Capacity10->LastLba0; + ScsiDiskDevice->BlkIo.Media->LastBlock = ((UINT32)Capacity10->LastLba3 << 24) | + (Capacity10->LastLba2 << 16) | + (Capacity10->LastLba1 << 8) | + Capacity10->LastLba0; ScsiDiskDevice->BlkIo.Media->BlockSize = (Capacity10->BlockSize3 << 24) | (Capacity10->BlockSize2 << 16) | (Capacity10->BlockSize1 << 8) | - Capacity10->BlockSize0; - ScsiDiskDevice->BlkIo.Media->LowestAlignedLba = 0; - ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock = 0; + Capacity10->BlockSize0; + ScsiDiskDevice->BlkIo.Media->LowestAlignedLba = 0; + ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock = 0; if (!ScsiDiskDevice->BlockLimitsVpdSupported) { - ScsiDiskDevice->UnmapInfo.MaxLbaCnt = (UINT32) ScsiDiskDevice->BlkIo.Media->LastBlock; + ScsiDiskDevice->UnmapInfo.MaxLbaCnt = (UINT32)ScsiDiskDevice->BlkIo.Media->LastBlock; } } else { - Ptr = (UINT8*)&ScsiDiskDevice->BlkIo.Media->LastBlock; + Ptr = (UINT8 *)&ScsiDiskDevice->BlkIo.Media->LastBlock; *Ptr++ = Capacity16->LastLba0; *Ptr++ = Capacity16->LastLba1; *Ptr++ = Capacity16->LastLba2; @@ -3316,16 +3407,16 @@ GetMediaInfo ( ScsiDiskDevice->BlkIo.Media->BlockSize = (Capacity16->BlockSize3 << 24) | (Capacity16->BlockSize2 << 16) | (Capacity16->BlockSize1 << 8) | - Capacity16->BlockSize0; + Capacity16->BlockSize0; ScsiDiskDevice->BlkIo.Media->LowestAlignedLba = (Capacity16->LowestAlignLogic2 << 8) | - Capacity16->LowestAlignLogic1; - ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock = (1 << Capacity16->LogicPerPhysical); + Capacity16->LowestAlignLogic1; + ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock = (1 << Capacity16->LogicPerPhysical); if (!ScsiDiskDevice->BlockLimitsVpdSupported) { - if (ScsiDiskDevice->BlkIo.Media->LastBlock > (UINT32) -1) { - ScsiDiskDevice->UnmapInfo.MaxLbaCnt = (UINT32) -1; + if (ScsiDiskDevice->BlkIo.Media->LastBlock > (UINT32)-1) { + ScsiDiskDevice->UnmapInfo.MaxLbaCnt = (UINT32)-1; } else { - ScsiDiskDevice->UnmapInfo.MaxLbaCnt = (UINT32) ScsiDiskDevice->BlkIo.Media->LastBlock; + ScsiDiskDevice->UnmapInfo.MaxLbaCnt = (UINT32)ScsiDiskDevice->BlkIo.Media->LastBlock; } } } @@ -3341,11 +3432,11 @@ GetMediaInfo ( **/ VOID ParseInquiryData ( - IN OUT SCSI_DISK_DEV *ScsiDiskDevice + IN OUT SCSI_DISK_DEV *ScsiDiskDevice ) { - ScsiDiskDevice->FixedDevice = (BOOLEAN) ((ScsiDiskDevice->InquiryData.Rmb == 1) ? 0 : 1); - ScsiDiskDevice->BlkIoMedia.RemovableMedia = (BOOLEAN) (!ScsiDiskDevice->FixedDevice); + ScsiDiskDevice->FixedDevice = (BOOLEAN)((ScsiDiskDevice->InquiryData.Rmb == 1) ? 0 : 1); + ScsiDiskDevice->BlkIoMedia.RemovableMedia = (BOOLEAN)(!ScsiDiskDevice->FixedDevice); } /** @@ -3362,48 +3453,47 @@ ParseInquiryData ( **/ EFI_STATUS ScsiDiskReadSectors ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - OUT VOID *Buffer, - IN EFI_LBA Lba, - IN UINTN NumberOfBlocks + IN SCSI_DISK_DEV *ScsiDiskDevice, + OUT VOID *Buffer, + IN EFI_LBA Lba, + IN UINTN NumberOfBlocks ) { - UINTN BlocksRemaining; - UINT8 *PtrBuffer; - UINT32 BlockSize; - UINT32 ByteCount; - UINT32 MaxBlock; - UINT32 SectorCount; - UINT32 NextSectorCount; - UINT64 Timeout; - EFI_STATUS Status; - UINT8 Index; - UINT8 MaxRetry; - BOOLEAN NeedRetry; + UINTN BlocksRemaining; + UINT8 *PtrBuffer; + UINT32 BlockSize; + UINT32 ByteCount; + UINT32 MaxBlock; + UINT32 SectorCount; + UINT32 NextSectorCount; + UINT64 Timeout; + EFI_STATUS Status; + UINT8 Index; + UINT8 MaxRetry; + BOOLEAN NeedRetry; - Status = EFI_SUCCESS; + Status = EFI_SUCCESS; - BlocksRemaining = NumberOfBlocks; - BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize; + BlocksRemaining = NumberOfBlocks; + BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize; // // limit the data bytes that can be transferred by one Read(10) or Read(16) Command // if (!ScsiDiskDevice->Cdb16Byte) { - MaxBlock = 0xFFFF; + MaxBlock = 0xFFFF; } else { - MaxBlock = 0xFFFFFFFF; + MaxBlock = 0xFFFFFFFF; } PtrBuffer = Buffer; while (BlocksRemaining > 0) { - if (BlocksRemaining <= MaxBlock) { if (!ScsiDiskDevice->Cdb16Byte) { - SectorCount = (UINT16) BlocksRemaining; + SectorCount = (UINT16)BlocksRemaining; } else { - SectorCount = (UINT32) BlocksRemaining; + SectorCount = (UINT32)BlocksRemaining; } } else { SectorCount = MaxBlock; @@ -3438,35 +3528,36 @@ ScsiDiskReadSectors ( // As ScsiDisk and ScsiBus driver are used to manage SCSI or ATAPI devices, we have to use // the lowest transfer rate to calculate the possible maximum timeout value for each operation. // From the above table, we could know 2.1Mbytes per second is lowest one. - // The timout value is rounded up to nearest integar and here an additional 30s is added + // The timeout value is rounded up to nearest integer and here an additional 30s is added // to follow ATA spec in which it mentioned that the device may take up to 30s to respond // commands in the Standby/Idle mode. // - Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31); + Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31); - MaxRetry = 2; + MaxRetry = 2; for (Index = 0; Index < MaxRetry; Index++) { if (!ScsiDiskDevice->Cdb16Byte) { Status = ScsiDiskRead10 ( - ScsiDiskDevice, - &NeedRetry, - Timeout, - PtrBuffer, - &ByteCount, - (UINT32) Lba, - SectorCount - ); + ScsiDiskDevice, + &NeedRetry, + Timeout, + PtrBuffer, + &ByteCount, + (UINT32)Lba, + SectorCount + ); } else { Status = ScsiDiskRead16 ( - ScsiDiskDevice, - &NeedRetry, - Timeout, - PtrBuffer, - &ByteCount, - Lba, - SectorCount - ); + ScsiDiskDevice, + &NeedRetry, + Timeout, + PtrBuffer, + &ByteCount, + Lba, + SectorCount + ); } + if (!EFI_ERROR (Status)) { break; } @@ -3482,7 +3573,7 @@ ScsiDiskReadSectors ( // it is invalid to request more sectors in the CDB than the entire // transfer (ie. ByteCount) can carry. // - // In addition, ByteCount is only expected to go down, or stay unchaged. + // In addition, ByteCount is only expected to go down, or stay unchanged. // Therefore we don't need to update Timeout: the original timeout should // accommodate shorter transfers too. // @@ -3505,8 +3596,8 @@ ScsiDiskReadSectors ( // SectorCount = ByteCount / BlockSize; - Lba += SectorCount; - PtrBuffer = PtrBuffer + SectorCount * BlockSize; + Lba += SectorCount; + PtrBuffer = PtrBuffer + SectorCount * BlockSize; BlocksRemaining -= SectorCount; } @@ -3527,48 +3618,47 @@ ScsiDiskReadSectors ( **/ EFI_STATUS ScsiDiskWriteSectors ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN VOID *Buffer, - IN EFI_LBA Lba, - IN UINTN NumberOfBlocks + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN VOID *Buffer, + IN EFI_LBA Lba, + IN UINTN NumberOfBlocks ) { - UINTN BlocksRemaining; - UINT8 *PtrBuffer; - UINT32 BlockSize; - UINT32 ByteCount; - UINT32 MaxBlock; - UINT32 SectorCount; - UINT32 NextSectorCount; - UINT64 Timeout; - EFI_STATUS Status; - UINT8 Index; - UINT8 MaxRetry; - BOOLEAN NeedRetry; + UINTN BlocksRemaining; + UINT8 *PtrBuffer; + UINT32 BlockSize; + UINT32 ByteCount; + UINT32 MaxBlock; + UINT32 SectorCount; + UINT32 NextSectorCount; + UINT64 Timeout; + EFI_STATUS Status; + UINT8 Index; + UINT8 MaxRetry; + BOOLEAN NeedRetry; - Status = EFI_SUCCESS; + Status = EFI_SUCCESS; - BlocksRemaining = NumberOfBlocks; - BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize; + BlocksRemaining = NumberOfBlocks; + BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize; // // limit the data bytes that can be transferred by one Read(10) or Read(16) Command // if (!ScsiDiskDevice->Cdb16Byte) { - MaxBlock = 0xFFFF; + MaxBlock = 0xFFFF; } else { - MaxBlock = 0xFFFFFFFF; + MaxBlock = 0xFFFFFFFF; } PtrBuffer = Buffer; while (BlocksRemaining > 0) { - if (BlocksRemaining <= MaxBlock) { if (!ScsiDiskDevice->Cdb16Byte) { - SectorCount = (UINT16) BlocksRemaining; + SectorCount = (UINT16)BlocksRemaining; } else { - SectorCount = (UINT32) BlocksRemaining; + SectorCount = (UINT32)BlocksRemaining; } } else { SectorCount = MaxBlock; @@ -3603,34 +3693,35 @@ ScsiDiskWriteSectors ( // As ScsiDisk and ScsiBus driver are used to manage SCSI or ATAPI devices, we have to use // the lowest transfer rate to calculate the possible maximum timeout value for each operation. // From the above table, we could know 2.1Mbytes per second is lowest one. - // The timout value is rounded up to nearest integar and here an additional 30s is added + // The timeout value is rounded up to nearest integer and here an additional 30s is added // to follow ATA spec in which it mentioned that the device may take up to 30s to respond // commands in the Standby/Idle mode. // - Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31); - MaxRetry = 2; + Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31); + MaxRetry = 2; for (Index = 0; Index < MaxRetry; Index++) { if (!ScsiDiskDevice->Cdb16Byte) { Status = ScsiDiskWrite10 ( - ScsiDiskDevice, - &NeedRetry, - Timeout, - PtrBuffer, - &ByteCount, - (UINT32) Lba, - SectorCount - ); + ScsiDiskDevice, + &NeedRetry, + Timeout, + PtrBuffer, + &ByteCount, + (UINT32)Lba, + SectorCount + ); } else { Status = ScsiDiskWrite16 ( - ScsiDiskDevice, - &NeedRetry, - Timeout, - PtrBuffer, - &ByteCount, - Lba, - SectorCount - ); - } + ScsiDiskDevice, + &NeedRetry, + Timeout, + PtrBuffer, + &ByteCount, + Lba, + SectorCount + ); + } + if (!EFI_ERROR (Status)) { break; } @@ -3646,7 +3737,7 @@ ScsiDiskWriteSectors ( // it is invalid to request more sectors in the CDB than the entire // transfer (ie. ByteCount) can carry. // - // In addition, ByteCount is only expected to go down, or stay unchaged. + // In addition, ByteCount is only expected to go down, or stay unchanged. // Therefore we don't need to update Timeout: the original timeout should // accommodate shorter transfers too. // @@ -3663,13 +3754,14 @@ ScsiDiskWriteSectors ( if ((Index == MaxRetry) && (Status != EFI_SUCCESS)) { return EFI_DEVICE_ERROR; } + // // actual transferred sectors // SectorCount = ByteCount / BlockSize; - Lba += SectorCount; - PtrBuffer = PtrBuffer + SectorCount * BlockSize; + Lba += SectorCount; + PtrBuffer = PtrBuffer + SectorCount * BlockSize; BlocksRemaining -= SectorCount; } @@ -3693,23 +3785,23 @@ ScsiDiskWriteSectors ( **/ EFI_STATUS ScsiDiskAsyncReadSectors ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - OUT VOID *Buffer, - IN EFI_LBA Lba, - IN UINTN NumberOfBlocks, - IN EFI_BLOCK_IO2_TOKEN *Token + IN SCSI_DISK_DEV *ScsiDiskDevice, + OUT VOID *Buffer, + IN EFI_LBA Lba, + IN UINTN NumberOfBlocks, + IN EFI_BLOCK_IO2_TOKEN *Token ) { - UINTN BlocksRemaining; - UINT8 *PtrBuffer; - UINT32 BlockSize; - UINT32 ByteCount; - UINT32 MaxBlock; - UINT32 SectorCount; - UINT64 Timeout; - SCSI_BLKIO2_REQUEST *BlkIo2Req; - EFI_STATUS Status; - EFI_TPL OldTpl; + UINTN BlocksRemaining; + UINT8 *PtrBuffer; + UINT32 BlockSize; + UINT32 ByteCount; + UINT32 MaxBlock; + UINT32 SectorCount; + UINT64 Timeout; + SCSI_BLKIO2_REQUEST *BlkIo2Req; + EFI_STATUS Status; + EFI_TPL OldTpl; if ((Token == NULL) || (Token->Event == NULL)) { return EFI_INVALID_PARAMETER; @@ -3720,7 +3812,7 @@ ScsiDiskAsyncReadSectors ( return EFI_OUT_OF_RESOURCES; } - BlkIo2Req->Token = Token; + BlkIo2Req->Token = Token; OldTpl = gBS->RaiseTPL (TPL_NOTIFY); InsertTailList (&ScsiDiskDevice->AsyncTaskQueue, &BlkIo2Req->Link); @@ -3728,30 +3820,29 @@ ScsiDiskAsyncReadSectors ( InitializeListHead (&BlkIo2Req->ScsiRWQueue); - Status = EFI_SUCCESS; + Status = EFI_SUCCESS; - BlocksRemaining = NumberOfBlocks; - BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize; + BlocksRemaining = NumberOfBlocks; + BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize; // // Limit the data bytes that can be transferred by one Read(10) or Read(16) // Command // if (!ScsiDiskDevice->Cdb16Byte) { - MaxBlock = 0xFFFF; + MaxBlock = 0xFFFF; } else { - MaxBlock = 0xFFFFFFFF; + MaxBlock = 0xFFFFFFFF; } PtrBuffer = Buffer; while (BlocksRemaining > 0) { - if (BlocksRemaining <= MaxBlock) { if (!ScsiDiskDevice->Cdb16Byte) { - SectorCount = (UINT16) BlocksRemaining; + SectorCount = (UINT16)BlocksRemaining; } else { - SectorCount = (UINT32) BlocksRemaining; + SectorCount = (UINT32)BlocksRemaining; } } else { SectorCount = MaxBlock; @@ -3787,11 +3878,11 @@ ScsiDiskAsyncReadSectors ( // we have to use the lowest transfer rate to calculate the possible // maximum timeout value for each operation. // From the above table, we could know 2.1Mbytes per second is lowest one. - // The timout value is rounded up to nearest integar and here an additional + // The timeout value is rounded up to nearest integer and here an additional // 30s is added to follow ATA spec in which it mentioned that the device // may take up to 30s to respond commands in the Standby/Idle mode. // - Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31); + Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31); if (!ScsiDiskDevice->Cdb16Byte) { Status = ScsiDiskAsyncRead10 ( @@ -3800,7 +3891,7 @@ ScsiDiskAsyncReadSectors ( 0, PtrBuffer, ByteCount, - (UINT32) Lba, + (UINT32)Lba, SectorCount, BlkIo2Req, Token @@ -3818,6 +3909,7 @@ ScsiDiskAsyncReadSectors ( Token ); } + if (EFI_ERROR (Status)) { // // Some devices will return EFI_DEVICE_ERROR or EFI_TIMEOUT when the data @@ -3868,8 +3960,8 @@ ScsiDiskAsyncReadSectors ( // SectorCount = ByteCount / BlockSize; - Lba += SectorCount; - PtrBuffer = PtrBuffer + SectorCount * BlockSize; + Lba += SectorCount; + PtrBuffer = PtrBuffer + SectorCount * BlockSize; BlocksRemaining -= SectorCount; } @@ -3887,6 +3979,7 @@ Done: gBS->SignalEvent (Token->Event); } + gBS->RestoreTPL (OldTpl); } @@ -3910,23 +4003,23 @@ Done: **/ EFI_STATUS ScsiDiskAsyncWriteSectors ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN VOID *Buffer, - IN EFI_LBA Lba, - IN UINTN NumberOfBlocks, - IN EFI_BLOCK_IO2_TOKEN *Token + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN VOID *Buffer, + IN EFI_LBA Lba, + IN UINTN NumberOfBlocks, + IN EFI_BLOCK_IO2_TOKEN *Token ) { - UINTN BlocksRemaining; - UINT8 *PtrBuffer; - UINT32 BlockSize; - UINT32 ByteCount; - UINT32 MaxBlock; - UINT32 SectorCount; - UINT64 Timeout; - SCSI_BLKIO2_REQUEST *BlkIo2Req; - EFI_STATUS Status; - EFI_TPL OldTpl; + UINTN BlocksRemaining; + UINT8 *PtrBuffer; + UINT32 BlockSize; + UINT32 ByteCount; + UINT32 MaxBlock; + UINT32 SectorCount; + UINT64 Timeout; + SCSI_BLKIO2_REQUEST *BlkIo2Req; + EFI_STATUS Status; + EFI_TPL OldTpl; if ((Token == NULL) || (Token->Event == NULL)) { return EFI_INVALID_PARAMETER; @@ -3937,7 +4030,7 @@ ScsiDiskAsyncWriteSectors ( return EFI_OUT_OF_RESOURCES; } - BlkIo2Req->Token = Token; + BlkIo2Req->Token = Token; OldTpl = gBS->RaiseTPL (TPL_NOTIFY); InsertTailList (&ScsiDiskDevice->AsyncTaskQueue, &BlkIo2Req->Link); @@ -3945,30 +4038,29 @@ ScsiDiskAsyncWriteSectors ( InitializeListHead (&BlkIo2Req->ScsiRWQueue); - Status = EFI_SUCCESS; + Status = EFI_SUCCESS; - BlocksRemaining = NumberOfBlocks; - BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize; + BlocksRemaining = NumberOfBlocks; + BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize; // // Limit the data bytes that can be transferred by one Read(10) or Read(16) // Command // if (!ScsiDiskDevice->Cdb16Byte) { - MaxBlock = 0xFFFF; + MaxBlock = 0xFFFF; } else { - MaxBlock = 0xFFFFFFFF; + MaxBlock = 0xFFFFFFFF; } PtrBuffer = Buffer; while (BlocksRemaining > 0) { - if (BlocksRemaining <= MaxBlock) { if (!ScsiDiskDevice->Cdb16Byte) { - SectorCount = (UINT16) BlocksRemaining; + SectorCount = (UINT16)BlocksRemaining; } else { - SectorCount = (UINT32) BlocksRemaining; + SectorCount = (UINT32)BlocksRemaining; } } else { SectorCount = MaxBlock; @@ -4004,11 +4096,11 @@ ScsiDiskAsyncWriteSectors ( // we have to use the lowest transfer rate to calculate the possible // maximum timeout value for each operation. // From the above table, we could know 2.1Mbytes per second is lowest one. - // The timout value is rounded up to nearest integar and here an additional + // The timeout value is rounded up to nearest integer and here an additional // 30s is added to follow ATA spec in which it mentioned that the device // may take up to 30s to respond commands in the Standby/Idle mode. // - Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31); + Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31); if (!ScsiDiskDevice->Cdb16Byte) { Status = ScsiDiskAsyncWrite10 ( @@ -4017,7 +4109,7 @@ ScsiDiskAsyncWriteSectors ( 0, PtrBuffer, ByteCount, - (UINT32) Lba, + (UINT32)Lba, SectorCount, BlkIo2Req, Token @@ -4035,6 +4127,7 @@ ScsiDiskAsyncWriteSectors ( Token ); } + if (EFI_ERROR (Status)) { // // Some devices will return EFI_DEVICE_ERROR or EFI_TIMEOUT when the data @@ -4085,8 +4178,8 @@ ScsiDiskAsyncWriteSectors ( // SectorCount = ByteCount / BlockSize; - Lba += SectorCount; - PtrBuffer = PtrBuffer + SectorCount * BlockSize; + Lba += SectorCount; + PtrBuffer = PtrBuffer + SectorCount * BlockSize; BlocksRemaining -= SectorCount; } @@ -4104,13 +4197,13 @@ Done: gBS->SignalEvent (Token->Event); } + gBS->RestoreTPL (OldTpl); } return Status; } - /** Submit Read(10) command. @@ -4126,13 +4219,13 @@ Done: **/ EFI_STATUS ScsiDiskRead10 ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - OUT BOOLEAN *NeedRetry, - IN UINT64 Timeout, - OUT UINT8 *DataBuffer, - IN OUT UINT32 *DataLength, - IN UINT32 StartLba, - IN UINT32 SectorCount + IN SCSI_DISK_DEV *ScsiDiskDevice, + OUT BOOLEAN *NeedRetry, + IN UINT64 Timeout, + OUT UINT8 *DataBuffer, + IN OUT UINT32 *DataLength, + IN UINT32 StartLba, + IN UINT32 SectorCount ) { UINT8 SenseDataLength; @@ -4143,31 +4236,31 @@ ScsiDiskRead10 ( UINTN Action; // - // Implement a backoff algorithem to resolve some compatibility issues that + // Implement a backoff algorithm to resolve some compatibility issues that // some SCSI targets or ATAPI devices couldn't correctly response reading/writing // big data in a single operation. - // This algorithem will at first try to execute original request. If the request fails + // This algorithm will at first try to execute original request. If the request fails // with media error sense data or else, it will reduce the transfer length to half and // try again till the operation succeeds or fails with one sector transfer length. // BackOff: - *NeedRetry = FALSE; - Action = ACTION_NO_ACTION; - SenseDataLength = (UINT8) (ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); - ReturnStatus = ScsiRead10Command ( - ScsiDiskDevice->ScsiIo, - Timeout, - ScsiDiskDevice->SenseData, - &SenseDataLength, - &HostAdapterStatus, - &TargetStatus, - DataBuffer, - DataLength, - StartLba, - SectorCount - ); + *NeedRetry = FALSE; + Action = ACTION_NO_ACTION; + SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); + ReturnStatus = ScsiRead10Command ( + ScsiDiskDevice->ScsiIo, + Timeout, + ScsiDiskDevice->SenseData, + &SenseDataLength, + &HostAdapterStatus, + &TargetStatus, + DataBuffer, + DataLength, + StartLba, + SectorCount + ); - if (ReturnStatus == EFI_NOT_READY || ReturnStatus == EFI_BAD_BUFFER_SIZE) { + if ((ReturnStatus == EFI_NOT_READY) || (ReturnStatus == EFI_BAD_BUFFER_SIZE)) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; } else if ((ReturnStatus == EFI_INVALID_PARAMETER) || (ReturnStatus == EFI_UNSUPPORTED)) { @@ -4206,8 +4299,8 @@ BackOff: } if ((TargetStatus == EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION) || (EFI_ERROR (ReturnStatus))) { - DEBUG ((EFI_D_ERROR, "ScsiDiskRead10: Check Condition happened!\n")); - Status = DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action); + DEBUG ((DEBUG_ERROR, "ScsiDiskRead10: Check Condition happened!\n")); + DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action); if (Action == ACTION_RETRY_COMMAND_LATER) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; @@ -4219,11 +4312,12 @@ BackOff: *NeedRetry = FALSE; return EFI_DEVICE_ERROR; } + // // Try again with half length if the sense data shows we need to retry. // SectorCount >>= 1; - *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize; + *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize; goto BackOff; } else { *NeedRetry = FALSE; @@ -4234,7 +4328,6 @@ BackOff: return ReturnStatus; } - /** Submit Write(10) Command. @@ -4251,13 +4344,13 @@ BackOff: **/ EFI_STATUS ScsiDiskWrite10 ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - OUT BOOLEAN *NeedRetry, - IN UINT64 Timeout, - IN UINT8 *DataBuffer, - IN OUT UINT32 *DataLength, - IN UINT32 StartLba, - IN UINT32 SectorCount + IN SCSI_DISK_DEV *ScsiDiskDevice, + OUT BOOLEAN *NeedRetry, + IN UINT64 Timeout, + IN UINT8 *DataBuffer, + IN OUT UINT32 *DataLength, + IN UINT32 StartLba, + IN UINT32 SectorCount ) { EFI_STATUS Status; @@ -4268,30 +4361,30 @@ ScsiDiskWrite10 ( UINTN Action; // - // Implement a backoff algorithem to resolve some compatibility issues that + // Implement a backoff algorithm to resolve some compatibility issues that // some SCSI targets or ATAPI devices couldn't correctly response reading/writing // big data in a single operation. - // This algorithem will at first try to execute original request. If the request fails + // This algorithm will at first try to execute original request. If the request fails // with media error sense data or else, it will reduce the transfer length to half and // try again till the operation succeeds or fails with one sector transfer length. // BackOff: - *NeedRetry = FALSE; - Action = ACTION_NO_ACTION; - SenseDataLength = (UINT8) (ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); - ReturnStatus = ScsiWrite10Command ( - ScsiDiskDevice->ScsiIo, - Timeout, - ScsiDiskDevice->SenseData, - &SenseDataLength, - &HostAdapterStatus, - &TargetStatus, - DataBuffer, - DataLength, - StartLba, - SectorCount - ); - if (ReturnStatus == EFI_NOT_READY || ReturnStatus == EFI_BAD_BUFFER_SIZE) { + *NeedRetry = FALSE; + Action = ACTION_NO_ACTION; + SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); + ReturnStatus = ScsiWrite10Command ( + ScsiDiskDevice->ScsiIo, + Timeout, + ScsiDiskDevice->SenseData, + &SenseDataLength, + &HostAdapterStatus, + &TargetStatus, + DataBuffer, + DataLength, + StartLba, + SectorCount + ); + if ((ReturnStatus == EFI_NOT_READY) || (ReturnStatus == EFI_BAD_BUFFER_SIZE)) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; } else if ((ReturnStatus == EFI_INVALID_PARAMETER) || (ReturnStatus == EFI_UNSUPPORTED)) { @@ -4330,8 +4423,8 @@ BackOff: } if ((TargetStatus == EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION) || (EFI_ERROR (ReturnStatus))) { - DEBUG ((EFI_D_ERROR, "ScsiDiskWrite10: Check Condition happened!\n")); - Status = DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action); + DEBUG ((DEBUG_ERROR, "ScsiDiskWrite10: Check Condition happened!\n")); + DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action); if (Action == ACTION_RETRY_COMMAND_LATER) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; @@ -4343,11 +4436,12 @@ BackOff: *NeedRetry = FALSE; return EFI_DEVICE_ERROR; } + // // Try again with half length if the sense data shows we need to retry. // SectorCount >>= 1; - *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize; + *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize; goto BackOff; } else { *NeedRetry = FALSE; @@ -4358,7 +4452,6 @@ BackOff: return ReturnStatus; } - /** Submit Read(16) command. @@ -4374,13 +4467,13 @@ BackOff: **/ EFI_STATUS ScsiDiskRead16 ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - OUT BOOLEAN *NeedRetry, - IN UINT64 Timeout, - OUT UINT8 *DataBuffer, - IN OUT UINT32 *DataLength, - IN UINT64 StartLba, - IN UINT32 SectorCount + IN SCSI_DISK_DEV *ScsiDiskDevice, + OUT BOOLEAN *NeedRetry, + IN UINT64 Timeout, + OUT UINT8 *DataBuffer, + IN OUT UINT32 *DataLength, + IN UINT64 StartLba, + IN UINT32 SectorCount ) { UINT8 SenseDataLength; @@ -4391,30 +4484,30 @@ ScsiDiskRead16 ( UINTN Action; // - // Implement a backoff algorithem to resolve some compatibility issues that + // Implement a backoff algorithm to resolve some compatibility issues that // some SCSI targets or ATAPI devices couldn't correctly response reading/writing // big data in a single operation. - // This algorithem will at first try to execute original request. If the request fails + // This algorithm will at first try to execute original request. If the request fails // with media error sense data or else, it will reduce the transfer length to half and // try again till the operation succeeds or fails with one sector transfer length. // BackOff: - *NeedRetry = FALSE; - Action = ACTION_NO_ACTION; - SenseDataLength = (UINT8) (ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); - ReturnStatus = ScsiRead16Command ( - ScsiDiskDevice->ScsiIo, - Timeout, - ScsiDiskDevice->SenseData, - &SenseDataLength, - &HostAdapterStatus, - &TargetStatus, - DataBuffer, - DataLength, - StartLba, - SectorCount - ); - if (ReturnStatus == EFI_NOT_READY || ReturnStatus == EFI_BAD_BUFFER_SIZE) { + *NeedRetry = FALSE; + Action = ACTION_NO_ACTION; + SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); + ReturnStatus = ScsiRead16Command ( + ScsiDiskDevice->ScsiIo, + Timeout, + ScsiDiskDevice->SenseData, + &SenseDataLength, + &HostAdapterStatus, + &TargetStatus, + DataBuffer, + DataLength, + StartLba, + SectorCount + ); + if ((ReturnStatus == EFI_NOT_READY) || (ReturnStatus == EFI_BAD_BUFFER_SIZE)) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; } else if ((ReturnStatus == EFI_INVALID_PARAMETER) || (ReturnStatus == EFI_UNSUPPORTED)) { @@ -4453,8 +4546,8 @@ BackOff: } if ((TargetStatus == EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION) || (EFI_ERROR (ReturnStatus))) { - DEBUG ((EFI_D_ERROR, "ScsiDiskRead16: Check Condition happened!\n")); - Status = DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action); + DEBUG ((DEBUG_ERROR, "ScsiDiskRead16: Check Condition happened!\n")); + DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action); if (Action == ACTION_RETRY_COMMAND_LATER) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; @@ -4466,11 +4559,12 @@ BackOff: *NeedRetry = FALSE; return EFI_DEVICE_ERROR; } + // // Try again with half length if the sense data shows we need to retry. // SectorCount >>= 1; - *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize; + *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize; goto BackOff; } else { *NeedRetry = FALSE; @@ -4481,7 +4575,6 @@ BackOff: return ReturnStatus; } - /** Submit Write(16) Command. @@ -4498,13 +4591,13 @@ BackOff: **/ EFI_STATUS ScsiDiskWrite16 ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - OUT BOOLEAN *NeedRetry, - IN UINT64 Timeout, - IN UINT8 *DataBuffer, - IN OUT UINT32 *DataLength, - IN UINT64 StartLba, - IN UINT32 SectorCount + IN SCSI_DISK_DEV *ScsiDiskDevice, + OUT BOOLEAN *NeedRetry, + IN UINT64 Timeout, + IN UINT8 *DataBuffer, + IN OUT UINT32 *DataLength, + IN UINT64 StartLba, + IN UINT32 SectorCount ) { EFI_STATUS Status; @@ -4515,30 +4608,30 @@ ScsiDiskWrite16 ( UINTN Action; // - // Implement a backoff algorithem to resolve some compatibility issues that + // Implement a backoff algorithm to resolve some compatibility issues that // some SCSI targets or ATAPI devices couldn't correctly response reading/writing // big data in a single operation. - // This algorithem will at first try to execute original request. If the request fails + // This algorithm will at first try to execute original request. If the request fails // with media error sense data or else, it will reduce the transfer length to half and // try again till the operation succeeds or fails with one sector transfer length. // BackOff: - *NeedRetry = FALSE; - Action = ACTION_NO_ACTION; - SenseDataLength = (UINT8) (ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); - ReturnStatus = ScsiWrite16Command ( - ScsiDiskDevice->ScsiIo, - Timeout, - ScsiDiskDevice->SenseData, - &SenseDataLength, - &HostAdapterStatus, - &TargetStatus, - DataBuffer, - DataLength, - StartLba, - SectorCount - ); - if (ReturnStatus == EFI_NOT_READY || ReturnStatus == EFI_BAD_BUFFER_SIZE) { + *NeedRetry = FALSE; + Action = ACTION_NO_ACTION; + SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA)); + ReturnStatus = ScsiWrite16Command ( + ScsiDiskDevice->ScsiIo, + Timeout, + ScsiDiskDevice->SenseData, + &SenseDataLength, + &HostAdapterStatus, + &TargetStatus, + DataBuffer, + DataLength, + StartLba, + SectorCount + ); + if ((ReturnStatus == EFI_NOT_READY) || (ReturnStatus == EFI_BAD_BUFFER_SIZE)) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; } else if ((ReturnStatus == EFI_INVALID_PARAMETER) || (ReturnStatus == EFI_UNSUPPORTED)) { @@ -4577,8 +4670,8 @@ BackOff: } if ((TargetStatus == EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION) || (EFI_ERROR (ReturnStatus))) { - DEBUG ((EFI_D_ERROR, "ScsiDiskWrite16: Check Condition happened!\n")); - Status = DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action); + DEBUG ((DEBUG_ERROR, "ScsiDiskWrite16: Check Condition happened!\n")); + DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action); if (Action == ACTION_RETRY_COMMAND_LATER) { *NeedRetry = TRUE; return EFI_DEVICE_ERROR; @@ -4590,11 +4683,12 @@ BackOff: *NeedRetry = FALSE; return EFI_DEVICE_ERROR; } + // // Try again with half length if the sense data shows we need to retry. // SectorCount >>= 1; - *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize; + *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize; goto BackOff; } else { *NeedRetry = FALSE; @@ -4605,7 +4699,6 @@ BackOff: return ReturnStatus; } - /** Internal helper notify function in which determine whether retry of a SCSI Read/Write command is needed and signal the event passed from Block I/O(2) if @@ -4622,23 +4715,23 @@ ScsiDiskNotify ( IN VOID *Context ) { - EFI_STATUS Status; - SCSI_ASYNC_RW_REQUEST *Request; - SCSI_DISK_DEV *ScsiDiskDevice; - EFI_BLOCK_IO2_TOKEN *Token; - UINTN Action; - UINT32 OldDataLength; - UINT32 OldSectorCount; - UINT8 MaxRetry; + EFI_STATUS Status; + SCSI_ASYNC_RW_REQUEST *Request; + SCSI_DISK_DEV *ScsiDiskDevice; + EFI_BLOCK_IO2_TOKEN *Token; + UINTN Action; + UINT32 OldDataLength; + UINT32 OldSectorCount; + UINT8 MaxRetry; gBS->CloseEvent (Event); - Request = (SCSI_ASYNC_RW_REQUEST *) Context; - ScsiDiskDevice = Request->ScsiDiskDevice; - Token = Request->BlkIo2Req->Token; - OldDataLength = Request->DataLength; - OldSectorCount = Request->SectorCount; - MaxRetry = 2; + Request = (SCSI_ASYNC_RW_REQUEST *)Context; + ScsiDiskDevice = Request->ScsiDiskDevice; + Token = Request->BlkIo2Req->Token; + OldDataLength = Request->DataLength; + OldSectorCount = Request->SectorCount; + MaxRetry = 2; // // If previous sub-tasks already fails, no need to process this sub-task. @@ -4686,14 +4779,14 @@ ScsiDiskNotify ( } if (Request->TargetStatus == EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION) { - DEBUG ((EFI_D_ERROR, "ScsiDiskNotify: Check Condition happened!\n")); - - Status = DetectMediaParsingSenseKeys ( - ScsiDiskDevice, - Request->SenseData, - Request->SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), - &Action - ); + DEBUG ((DEBUG_ERROR, "ScsiDiskNotify: Check Condition happened!\n")); + + DetectMediaParsingSenseKeys ( + ScsiDiskDevice, + Request->SenseData, + Request->SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), + &Action + ); if (Action == ACTION_RETRY_COMMAND_LATER) { if (++Request->TimesRetry > MaxRetry) { Token->TransactionStatus = EFI_DEVICE_ERROR; @@ -4710,13 +4803,14 @@ ScsiDiskNotify ( Token->TransactionStatus = EFI_DEVICE_ERROR; goto Exit; } + // // Try again with two half length request if the sense data shows we need // to retry. // Request->SectorCount >>= 1; - Request->DataLength = Request->SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize; - Request->TimesRetry = 0; + Request->DataLength = Request->SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize; + Request->TimesRetry = 0; goto Retry; } else { @@ -4742,7 +4836,7 @@ Retry: Request->TimesRetry, Request->InBuffer, Request->DataLength, - (UINT32) Request->StartLba, + (UINT32)Request->StartLba, Request->SectorCount, Request->BlkIo2Req, Token @@ -4776,7 +4870,7 @@ Retry: 0, Request->InBuffer + Request->SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize, OldDataLength - Request->DataLength, - (UINT32) Request->StartLba + Request->SectorCount, + (UINT32)Request->StartLba + Request->SectorCount, OldSectorCount - Request->SectorCount, Request->BlkIo2Req, Token @@ -4794,6 +4888,7 @@ Retry: Token ); } + if (EFI_ERROR (Status)) { Token->TransactionStatus = EFI_DEVICE_ERROR; goto Exit; @@ -4810,7 +4905,7 @@ Retry: Request->TimesRetry, Request->OutBuffer, Request->DataLength, - (UINT32) Request->StartLba, + (UINT32)Request->StartLba, Request->SectorCount, Request->BlkIo2Req, Token @@ -4844,7 +4939,7 @@ Retry: 0, Request->OutBuffer + Request->SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize, OldDataLength - Request->DataLength, - (UINT32) Request->StartLba + Request->SectorCount, + (UINT32)Request->StartLba + Request->SectorCount, OldSectorCount - Request->SectorCount, Request->BlkIo2Req, Token @@ -4862,6 +4957,7 @@ Retry: Token ); } + if (EFI_ERROR (Status)) { Token->TransactionStatus = EFI_DEVICE_ERROR; goto Exit; @@ -4872,7 +4968,8 @@ Retry: Exit: RemoveEntryList (&Request->Link); if ((IsListEmpty (&Request->BlkIo2Req->ScsiRWQueue)) && - (Request->BlkIo2Req->LastScsiRW)) { + (Request->BlkIo2Req->LastScsiRW)) + { // // The last SCSI R/W command of a BlockIo2 request completes // @@ -4885,7 +4982,6 @@ Exit: FreePool (Request); } - /** Submit Async Read(10) command. @@ -4908,21 +5004,21 @@ Exit: **/ EFI_STATUS ScsiDiskAsyncRead10 ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN UINT64 Timeout, - IN UINT8 TimesRetry, - OUT UINT8 *DataBuffer, - IN UINT32 DataLength, - IN UINT32 StartLba, - IN UINT32 SectorCount, - IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req, - IN EFI_BLOCK_IO2_TOKEN *Token + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN UINT64 Timeout, + IN UINT8 TimesRetry, + OUT UINT8 *DataBuffer, + IN UINT32 DataLength, + IN UINT32 StartLba, + IN UINT32 SectorCount, + IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req, + IN EFI_BLOCK_IO2_TOKEN *Token ) { - EFI_STATUS Status; - SCSI_ASYNC_RW_REQUEST *Request; - EFI_EVENT AsyncIoEvent; - EFI_TPL OldTpl; + EFI_STATUS Status; + SCSI_ASYNC_RW_REQUEST *Request; + EFI_EVENT AsyncIoEvent; + EFI_TPL OldTpl; AsyncIoEvent = NULL; @@ -4935,21 +5031,21 @@ ScsiDiskAsyncRead10 ( InsertTailList (&BlkIo2Req->ScsiRWQueue, &Request->Link); gBS->RestoreTPL (OldTpl); - Request->SenseDataLength = (UINT8) (6 * sizeof (EFI_SCSI_SENSE_DATA)); + Request->SenseDataLength = (UINT8)(6 * sizeof (EFI_SCSI_SENSE_DATA)); Request->SenseData = AllocateZeroPool (Request->SenseDataLength); if (Request->SenseData == NULL) { Status = EFI_OUT_OF_RESOURCES; goto ErrorExit; } - Request->ScsiDiskDevice = ScsiDiskDevice; - Request->Timeout = Timeout; - Request->TimesRetry = TimesRetry; - Request->InBuffer = DataBuffer; - Request->DataLength = DataLength; - Request->StartLba = StartLba; - Request->SectorCount = SectorCount; - Request->BlkIo2Req = BlkIo2Req; + Request->ScsiDiskDevice = ScsiDiskDevice; + Request->Timeout = Timeout; + Request->TimesRetry = TimesRetry; + Request->InBuffer = DataBuffer; + Request->DataLength = DataLength; + Request->StartLba = StartLba; + Request->SectorCount = SectorCount; + Request->BlkIo2Req = BlkIo2Req; // // Create Event @@ -4961,7 +5057,7 @@ ScsiDiskAsyncRead10 ( Request, &AsyncIoEvent ); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { goto ErrorExit; } @@ -4974,11 +5070,11 @@ ScsiDiskAsyncRead10 ( &Request->TargetStatus, Request->InBuffer, &Request->DataLength, - (UINT32) Request->StartLba, + (UINT32)Request->StartLba, Request->SectorCount, AsyncIoEvent ); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { goto ErrorExit; } @@ -5004,7 +5100,6 @@ ErrorExit: return Status; } - /** Submit Async Write(10) command. @@ -5027,21 +5122,21 @@ ErrorExit: **/ EFI_STATUS ScsiDiskAsyncWrite10 ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN UINT64 Timeout, - IN UINT8 TimesRetry, - IN UINT8 *DataBuffer, - IN UINT32 DataLength, - IN UINT32 StartLba, - IN UINT32 SectorCount, - IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req, - IN EFI_BLOCK_IO2_TOKEN *Token + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN UINT64 Timeout, + IN UINT8 TimesRetry, + IN UINT8 *DataBuffer, + IN UINT32 DataLength, + IN UINT32 StartLba, + IN UINT32 SectorCount, + IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req, + IN EFI_BLOCK_IO2_TOKEN *Token ) { - EFI_STATUS Status; - SCSI_ASYNC_RW_REQUEST *Request; - EFI_EVENT AsyncIoEvent; - EFI_TPL OldTpl; + EFI_STATUS Status; + SCSI_ASYNC_RW_REQUEST *Request; + EFI_EVENT AsyncIoEvent; + EFI_TPL OldTpl; AsyncIoEvent = NULL; @@ -5054,21 +5149,21 @@ ScsiDiskAsyncWrite10 ( InsertTailList (&BlkIo2Req->ScsiRWQueue, &Request->Link); gBS->RestoreTPL (OldTpl); - Request->SenseDataLength = (UINT8) (6 * sizeof (EFI_SCSI_SENSE_DATA)); + Request->SenseDataLength = (UINT8)(6 * sizeof (EFI_SCSI_SENSE_DATA)); Request->SenseData = AllocateZeroPool (Request->SenseDataLength); if (Request->SenseData == NULL) { Status = EFI_OUT_OF_RESOURCES; goto ErrorExit; } - Request->ScsiDiskDevice = ScsiDiskDevice; - Request->Timeout = Timeout; - Request->TimesRetry = TimesRetry; - Request->OutBuffer = DataBuffer; - Request->DataLength = DataLength; - Request->StartLba = StartLba; - Request->SectorCount = SectorCount; - Request->BlkIo2Req = BlkIo2Req; + Request->ScsiDiskDevice = ScsiDiskDevice; + Request->Timeout = Timeout; + Request->TimesRetry = TimesRetry; + Request->OutBuffer = DataBuffer; + Request->DataLength = DataLength; + Request->StartLba = StartLba; + Request->SectorCount = SectorCount; + Request->BlkIo2Req = BlkIo2Req; // // Create Event @@ -5080,7 +5175,7 @@ ScsiDiskAsyncWrite10 ( Request, &AsyncIoEvent ); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { goto ErrorExit; } @@ -5093,11 +5188,11 @@ ScsiDiskAsyncWrite10 ( &Request->TargetStatus, Request->OutBuffer, &Request->DataLength, - (UINT32) Request->StartLba, + (UINT32)Request->StartLba, Request->SectorCount, AsyncIoEvent ); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { goto ErrorExit; } @@ -5123,7 +5218,6 @@ ErrorExit: return Status; } - /** Submit Async Read(16) command. @@ -5146,21 +5240,21 @@ ErrorExit: **/ EFI_STATUS ScsiDiskAsyncRead16 ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN UINT64 Timeout, - IN UINT8 TimesRetry, - OUT UINT8 *DataBuffer, - IN UINT32 DataLength, - IN UINT64 StartLba, - IN UINT32 SectorCount, - IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req, - IN EFI_BLOCK_IO2_TOKEN *Token + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN UINT64 Timeout, + IN UINT8 TimesRetry, + OUT UINT8 *DataBuffer, + IN UINT32 DataLength, + IN UINT64 StartLba, + IN UINT32 SectorCount, + IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req, + IN EFI_BLOCK_IO2_TOKEN *Token ) { - EFI_STATUS Status; - SCSI_ASYNC_RW_REQUEST *Request; - EFI_EVENT AsyncIoEvent; - EFI_TPL OldTpl; + EFI_STATUS Status; + SCSI_ASYNC_RW_REQUEST *Request; + EFI_EVENT AsyncIoEvent; + EFI_TPL OldTpl; AsyncIoEvent = NULL; @@ -5173,21 +5267,21 @@ ScsiDiskAsyncRead16 ( InsertTailList (&BlkIo2Req->ScsiRWQueue, &Request->Link); gBS->RestoreTPL (OldTpl); - Request->SenseDataLength = (UINT8) (6 * sizeof (EFI_SCSI_SENSE_DATA)); + Request->SenseDataLength = (UINT8)(6 * sizeof (EFI_SCSI_SENSE_DATA)); Request->SenseData = AllocateZeroPool (Request->SenseDataLength); if (Request->SenseData == NULL) { Status = EFI_OUT_OF_RESOURCES; goto ErrorExit; } - Request->ScsiDiskDevice = ScsiDiskDevice; - Request->Timeout = Timeout; - Request->TimesRetry = TimesRetry; - Request->InBuffer = DataBuffer; - Request->DataLength = DataLength; - Request->StartLba = StartLba; - Request->SectorCount = SectorCount; - Request->BlkIo2Req = BlkIo2Req; + Request->ScsiDiskDevice = ScsiDiskDevice; + Request->Timeout = Timeout; + Request->TimesRetry = TimesRetry; + Request->InBuffer = DataBuffer; + Request->DataLength = DataLength; + Request->StartLba = StartLba; + Request->SectorCount = SectorCount; + Request->BlkIo2Req = BlkIo2Req; // // Create Event @@ -5199,7 +5293,7 @@ ScsiDiskAsyncRead16 ( Request, &AsyncIoEvent ); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { goto ErrorExit; } @@ -5216,7 +5310,7 @@ ScsiDiskAsyncRead16 ( Request->SectorCount, AsyncIoEvent ); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { goto ErrorExit; } @@ -5242,7 +5336,6 @@ ErrorExit: return Status; } - /** Submit Async Write(16) command. @@ -5265,21 +5358,21 @@ ErrorExit: **/ EFI_STATUS ScsiDiskAsyncWrite16 ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN UINT64 Timeout, - IN UINT8 TimesRetry, - IN UINT8 *DataBuffer, - IN UINT32 DataLength, - IN UINT64 StartLba, - IN UINT32 SectorCount, - IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req, - IN EFI_BLOCK_IO2_TOKEN *Token + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN UINT64 Timeout, + IN UINT8 TimesRetry, + IN UINT8 *DataBuffer, + IN UINT32 DataLength, + IN UINT64 StartLba, + IN UINT32 SectorCount, + IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req, + IN EFI_BLOCK_IO2_TOKEN *Token ) { - EFI_STATUS Status; - SCSI_ASYNC_RW_REQUEST *Request; - EFI_EVENT AsyncIoEvent; - EFI_TPL OldTpl; + EFI_STATUS Status; + SCSI_ASYNC_RW_REQUEST *Request; + EFI_EVENT AsyncIoEvent; + EFI_TPL OldTpl; AsyncIoEvent = NULL; @@ -5292,21 +5385,21 @@ ScsiDiskAsyncWrite16 ( InsertTailList (&BlkIo2Req->ScsiRWQueue, &Request->Link); gBS->RestoreTPL (OldTpl); - Request->SenseDataLength = (UINT8) (6 * sizeof (EFI_SCSI_SENSE_DATA)); + Request->SenseDataLength = (UINT8)(6 * sizeof (EFI_SCSI_SENSE_DATA)); Request->SenseData = AllocateZeroPool (Request->SenseDataLength); if (Request->SenseData == NULL) { Status = EFI_OUT_OF_RESOURCES; goto ErrorExit; } - Request->ScsiDiskDevice = ScsiDiskDevice; - Request->Timeout = Timeout; - Request->TimesRetry = TimesRetry; - Request->OutBuffer = DataBuffer; - Request->DataLength = DataLength; - Request->StartLba = StartLba; - Request->SectorCount = SectorCount; - Request->BlkIo2Req = BlkIo2Req; + Request->ScsiDiskDevice = ScsiDiskDevice; + Request->Timeout = Timeout; + Request->TimesRetry = TimesRetry; + Request->OutBuffer = DataBuffer; + Request->DataLength = DataLength; + Request->StartLba = StartLba; + Request->SectorCount = SectorCount; + Request->BlkIo2Req = BlkIo2Req; // // Create Event @@ -5318,7 +5411,7 @@ ScsiDiskAsyncWrite16 ( Request, &AsyncIoEvent ); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { goto ErrorExit; } @@ -5335,7 +5428,7 @@ ScsiDiskAsyncWrite16 ( Request->SectorCount, AsyncIoEvent ); - if (EFI_ERROR(Status)) { + if (EFI_ERROR (Status)) { goto ErrorExit; } @@ -5361,7 +5454,6 @@ ErrorExit: return Status; } - /** Check sense key to find if media presents. @@ -5373,13 +5465,13 @@ ErrorExit: **/ BOOLEAN ScsiDiskIsNoMedia ( - IN EFI_SCSI_SENSE_DATA *SenseData, - IN UINTN SenseCounts + IN EFI_SCSI_SENSE_DATA *SenseData, + IN UINTN SenseCounts ) { - EFI_SCSI_SENSE_DATA *SensePtr; - UINTN Index; - BOOLEAN IsNoMedia; + EFI_SCSI_SENSE_DATA *SensePtr; + UINTN Index; + BOOLEAN IsNoMedia; IsNoMedia = FALSE; SensePtr = SenseData; @@ -5390,16 +5482,17 @@ ScsiDiskIsNoMedia ( // Additional Sense Code is ASC_NO_MEDIA (0x3A) // if ((SensePtr->Sense_Key == EFI_SCSI_SK_NOT_READY) && - (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_NO_MEDIA)) { + (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_NO_MEDIA)) + { IsNoMedia = TRUE; } + SensePtr++; } return IsNoMedia; } - /** Parse sense key. @@ -5412,70 +5505,68 @@ ScsiDiskIsNoMedia ( **/ BOOLEAN ScsiDiskIsMediaError ( - IN EFI_SCSI_SENSE_DATA *SenseData, - IN UINTN SenseCounts + IN EFI_SCSI_SENSE_DATA *SenseData, + IN UINTN SenseCounts ) { - EFI_SCSI_SENSE_DATA *SensePtr; - UINTN Index; - BOOLEAN IsError; + EFI_SCSI_SENSE_DATA *SensePtr; + UINTN Index; + BOOLEAN IsError; - IsError = FALSE; - SensePtr = SenseData; + IsError = FALSE; + SensePtr = SenseData; for (Index = 0; Index < SenseCounts; Index++) { - switch (SensePtr->Sense_Key) { + case EFI_SCSI_SK_MEDIUM_ERROR: + // + // Sense Key is EFI_SCSI_SK_MEDIUM_ERROR (0x3) + // + switch (SensePtr->Addnl_Sense_Code) { + // + // fall through + // + case EFI_SCSI_ASC_MEDIA_ERR1: - case EFI_SCSI_SK_MEDIUM_ERROR: - // - // Sense Key is EFI_SCSI_SK_MEDIUM_ERROR (0x3) - // - switch (SensePtr->Addnl_Sense_Code) { + // + // fall through + // + case EFI_SCSI_ASC_MEDIA_ERR2: - // - // fall through - // - case EFI_SCSI_ASC_MEDIA_ERR1: + // + // fall through + // + case EFI_SCSI_ASC_MEDIA_ERR3: + case EFI_SCSI_ASC_MEDIA_ERR4: + IsError = TRUE; + break; - // - // fall through - // - case EFI_SCSI_ASC_MEDIA_ERR2: + default: + break; + } - // - // fall through - // - case EFI_SCSI_ASC_MEDIA_ERR3: - case EFI_SCSI_ASC_MEDIA_ERR4: - IsError = TRUE; break; - default: - break; - } + case EFI_SCSI_SK_NOT_READY: + // + // Sense Key is EFI_SCSI_SK_NOT_READY (0x2) + // + switch (SensePtr->Addnl_Sense_Code) { + // + // Additional Sense Code is ASC_MEDIA_UPSIDE_DOWN (0x6) + // + case EFI_SCSI_ASC_MEDIA_UPSIDE_DOWN: + IsError = TRUE; + break; - break; + default: + break; + } - case EFI_SCSI_SK_NOT_READY: - // - // Sense Key is EFI_SCSI_SK_NOT_READY (0x2) - // - switch (SensePtr->Addnl_Sense_Code) { - // - // Additional Sense Code is ASC_MEDIA_UPSIDE_DOWN (0x6) - // - case EFI_SCSI_ASC_MEDIA_UPSIDE_DOWN: - IsError = TRUE; break; default: break; - } - break; - - default: - break; } SensePtr++; @@ -5484,7 +5575,6 @@ ScsiDiskIsMediaError ( return IsError; } - /** Check sense key to find if hardware error happens. @@ -5497,19 +5587,18 @@ ScsiDiskIsMediaError ( **/ BOOLEAN ScsiDiskIsHardwareError ( - IN EFI_SCSI_SENSE_DATA *SenseData, - IN UINTN SenseCounts + IN EFI_SCSI_SENSE_DATA *SenseData, + IN UINTN SenseCounts ) { - EFI_SCSI_SENSE_DATA *SensePtr; - UINTN Index; - BOOLEAN IsError; + EFI_SCSI_SENSE_DATA *SensePtr; + UINTN Index; + BOOLEAN IsError; - IsError = FALSE; - SensePtr = SenseData; + IsError = FALSE; + SensePtr = SenseData; for (Index = 0; Index < SenseCounts; Index++) { - // // Sense Key is EFI_SCSI_SK_HARDWARE_ERROR (0x4) // @@ -5523,7 +5612,6 @@ ScsiDiskIsHardwareError ( return IsError; } - /** Check sense key to find if media has changed. @@ -5535,16 +5623,16 @@ ScsiDiskIsHardwareError ( **/ BOOLEAN ScsiDiskIsMediaChange ( - IN EFI_SCSI_SENSE_DATA *SenseData, - IN UINTN SenseCounts + IN EFI_SCSI_SENSE_DATA *SenseData, + IN UINTN SenseCounts ) { - EFI_SCSI_SENSE_DATA *SensePtr; - UINTN Index; - BOOLEAN IsMediaChanged; + EFI_SCSI_SENSE_DATA *SensePtr; + UINTN Index; + BOOLEAN IsMediaChanged; - IsMediaChanged = FALSE; - SensePtr = SenseData; + IsMediaChanged = FALSE; + SensePtr = SenseData; for (Index = 0; Index < SenseCounts; Index++) { // @@ -5552,7 +5640,8 @@ ScsiDiskIsMediaChange ( // Additional sense code is EFI_SCSI_ASC_MEDIA_CHANGE (0x28) // if ((SensePtr->Sense_Key == EFI_SCSI_SK_UNIT_ATTENTION) && - (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_MEDIA_CHANGE)) { + (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_MEDIA_CHANGE)) + { IsMediaChanged = TRUE; } @@ -5574,25 +5663,25 @@ ScsiDiskIsMediaChange ( **/ BOOLEAN ScsiDiskIsResetBefore ( - IN EFI_SCSI_SENSE_DATA *SenseData, - IN UINTN SenseCounts + IN EFI_SCSI_SENSE_DATA *SenseData, + IN UINTN SenseCounts ) { - EFI_SCSI_SENSE_DATA *SensePtr; - UINTN Index; - BOOLEAN IsResetBefore; + EFI_SCSI_SENSE_DATA *SensePtr; + UINTN Index; + BOOLEAN IsResetBefore; IsResetBefore = FALSE; SensePtr = SenseData; for (Index = 0; Index < SenseCounts; Index++) { - // // Sense Key is EFI_SCSI_SK_UNIT_ATTENTION (0x6) // Additional Sense Code is EFI_SCSI_ASC_RESET (0x29) // if ((SensePtr->Sense_Key == EFI_SCSI_SK_UNIT_ATTENTION) && - (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_RESET)) { + (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_RESET)) + { IsResetBefore = TRUE; } @@ -5615,56 +5704,56 @@ ScsiDiskIsResetBefore ( **/ BOOLEAN ScsiDiskIsDriveReady ( - IN EFI_SCSI_SENSE_DATA *SenseData, - IN UINTN SenseCounts, - OUT BOOLEAN *RetryLater + IN EFI_SCSI_SENSE_DATA *SenseData, + IN UINTN SenseCounts, + OUT BOOLEAN *RetryLater ) { - EFI_SCSI_SENSE_DATA *SensePtr; - UINTN Index; - BOOLEAN IsReady; + EFI_SCSI_SENSE_DATA *SensePtr; + UINTN Index; + BOOLEAN IsReady; IsReady = TRUE; *RetryLater = FALSE; SensePtr = SenseData; for (Index = 0; Index < SenseCounts; Index++) { - switch (SensePtr->Sense_Key) { - - case EFI_SCSI_SK_NOT_READY: - // - // Sense Key is EFI_SCSI_SK_NOT_READY (0x2) - // - switch (SensePtr->Addnl_Sense_Code) { - case EFI_SCSI_ASC_NOT_READY: + case EFI_SCSI_SK_NOT_READY: // - // Additional Sense Code is EFI_SCSI_ASC_NOT_READY (0x4) + // Sense Key is EFI_SCSI_SK_NOT_READY (0x2) // - switch (SensePtr->Addnl_Sense_Code_Qualifier) { - case EFI_SCSI_ASCQ_IN_PROGRESS: - // - // Additional Sense Code Qualifier is - // EFI_SCSI_ASCQ_IN_PROGRESS (0x1) - // - IsReady = FALSE; - *RetryLater = TRUE; - break; + switch (SensePtr->Addnl_Sense_Code) { + case EFI_SCSI_ASC_NOT_READY: + // + // Additional Sense Code is EFI_SCSI_ASC_NOT_READY (0x4) + // + switch (SensePtr->Addnl_Sense_Code_Qualifier) { + case EFI_SCSI_ASCQ_IN_PROGRESS: + // + // Additional Sense Code Qualifier is + // EFI_SCSI_ASCQ_IN_PROGRESS (0x1) + // + IsReady = FALSE; + *RetryLater = TRUE; + break; + + default: + IsReady = FALSE; + *RetryLater = FALSE; + break; + } - default: - IsReady = FALSE; - *RetryLater = FALSE; - break; + break; + + default: + break; } + break; default: break; - } - break; - - default: - break; } SensePtr++; @@ -5685,13 +5774,13 @@ ScsiDiskIsDriveReady ( **/ BOOLEAN ScsiDiskHaveSenseKey ( - IN EFI_SCSI_SENSE_DATA *SenseData, - IN UINTN SenseCounts + IN EFI_SCSI_SENSE_DATA *SenseData, + IN UINTN SenseCounts ) { - EFI_SCSI_SENSE_DATA *SensePtr; - UINTN Index; - BOOLEAN HaveSenseKey; + EFI_SCSI_SENSE_DATA *SensePtr; + UINTN Index; + BOOLEAN HaveSenseKey; if (SenseCounts == 0) { HaveSenseKey = FALSE; @@ -5702,12 +5791,12 @@ ScsiDiskHaveSenseKey ( SensePtr = SenseData; for (Index = 0; Index < SenseCounts; Index++) { - // // Sense Key is SK_NO_SENSE (0x0) // if ((SensePtr->Sense_Key == EFI_SCSI_SK_NO_SENSE) && - (Index == 0)) { + (Index == 0)) + { HaveSenseKey = FALSE; } @@ -5725,11 +5814,11 @@ ScsiDiskHaveSenseKey ( **/ VOID ReleaseScsiDiskDeviceResources ( - IN SCSI_DISK_DEV *ScsiDiskDevice + IN SCSI_DISK_DEV *ScsiDiskDevice ) { if (ScsiDiskDevice == NULL) { - return ; + return; } if (ScsiDiskDevice->SenseData != NULL) { @@ -5759,11 +5848,11 @@ ReleaseScsiDiskDeviceResources ( **/ BOOLEAN DetermineInstallBlockIo ( - IN EFI_HANDLE ChildHandle + IN EFI_HANDLE ChildHandle ) { - EFI_SCSI_PASS_THRU_PROTOCOL *ScsiPassThru; - EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtScsiPassThru; + EFI_SCSI_PASS_THRU_PROTOCOL *ScsiPassThru; + EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtScsiPassThru; // // Firstly, check if ExtScsiPassThru Protocol parent handle exists. If existence, @@ -5805,15 +5894,15 @@ DetermineInstallBlockIo ( VOID * EFIAPI GetParentProtocol ( - IN EFI_GUID *ProtocolGuid, - IN EFI_HANDLE ChildHandle + IN EFI_GUID *ProtocolGuid, + IN EFI_HANDLE ChildHandle ) { - UINTN Index; - UINTN HandleCount; - VOID *Interface; - EFI_STATUS Status; - EFI_HANDLE *HandleBuffer; + UINTN Index; + UINTN HandleCount; + VOID *Interface; + EFI_STATUS Status; + EFI_HANDLE *HandleBuffer; // // Retrieve the list of all handles from the handle database @@ -5860,20 +5949,20 @@ GetParentProtocol ( **/ BOOLEAN DetermineInstallEraseBlock ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN EFI_HANDLE ChildHandle + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN EFI_HANDLE ChildHandle ) { - UINT8 HostAdapterStatus; - UINT8 TargetStatus; - EFI_STATUS CommandStatus; - EFI_STATUS Status; - BOOLEAN UfsDevice; - BOOLEAN RetVal; - EFI_DEVICE_PATH_PROTOCOL *DevicePathNode; - UINT8 SenseDataLength; - UINT32 DataLength16; - EFI_SCSI_DISK_CAPACITY_DATA16 *CapacityData16; + UINT8 HostAdapterStatus; + UINT8 TargetStatus; + EFI_STATUS CommandStatus; + EFI_STATUS Status; + BOOLEAN UfsDevice; + BOOLEAN RetVal; + EFI_DEVICE_PATH_PROTOCOL *DevicePathNode; + UINT8 SenseDataLength; + UINT32 DataLength16; + EFI_SCSI_DISK_CAPACITY_DATA16 *CapacityData16; UfsDevice = FALSE; RetVal = TRUE; @@ -5890,7 +5979,7 @@ DetermineInstallEraseBlock ( Status = gBS->HandleProtocol ( ChildHandle, &gEfiDevicePathProtocolGuid, - (VOID **) &DevicePathNode + (VOID **)&DevicePathNode ); // // Device Path protocol must be installed on the device handle. @@ -5902,13 +5991,15 @@ DetermineInstallEraseBlock ( // For now, only support Erase Block Protocol on UFS devices. // if ((DevicePathNode->Type == MESSAGING_DEVICE_PATH) && - (DevicePathNode->SubType == MSG_UFS_DP)) { + (DevicePathNode->SubType == MSG_UFS_DP)) + { UfsDevice = TRUE; break; } DevicePathNode = NextDevicePathNode (DevicePathNode); } + if (!UfsDevice) { RetVal = FALSE; goto Done; @@ -5934,7 +6025,7 @@ DetermineInstallEraseBlock ( &SenseDataLength, &HostAdapterStatus, &TargetStatus, - (VOID *) CapacityData16, + (VOID *)CapacityData16, &DataLength16, FALSE ); @@ -5946,9 +6037,10 @@ DetermineInstallEraseBlock ( // Bits TPE and TPRZ should both be set to enable the erase feature on UFS. // if (((CapacityData16->LowestAlignLogic2 & BIT7) == 0) || - ((CapacityData16->LowestAlignLogic2 & BIT6) == 0)) { + ((CapacityData16->LowestAlignLogic2 & BIT6) == 0)) + { DEBUG (( - EFI_D_VERBOSE, + DEBUG_VERBOSE, "ScsiDisk EraseBlock: Either TPE or TPRZ is not set: 0x%x.\n", CapacityData16->LowestAlignLogic2 )); @@ -5958,7 +6050,7 @@ DetermineInstallEraseBlock ( } } else { DEBUG (( - EFI_D_VERBOSE, + DEBUG_VERBOSE, "ScsiDisk EraseBlock: ReadCapacity16 failed with status %r.\n", CommandStatus )); @@ -5971,9 +6063,10 @@ DetermineInstallEraseBlock ( // Check whether the UFS device server implements the UNMAP command. // if ((ScsiDiskDevice->UnmapInfo.MaxLbaCnt == 0) || - (ScsiDiskDevice->UnmapInfo.MaxBlkDespCnt == 0)) { + (ScsiDiskDevice->UnmapInfo.MaxBlkDespCnt == 0)) + { DEBUG (( - EFI_D_VERBOSE, + DEBUG_VERBOSE, "ScsiDisk EraseBlock: The device server does not implement the UNMAP command.\n" )); @@ -6001,22 +6094,22 @@ Done: **/ BOOLEAN DetermineInstallStorageSecurity ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN EFI_HANDLE ChildHandle + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN EFI_HANDLE ChildHandle ) { - EFI_STATUS Status; - UFS_DEVICE_PATH *UfsDevice; - BOOLEAN RetVal; - EFI_DEVICE_PATH_PROTOCOL *DevicePathNode; + EFI_STATUS Status; + UFS_DEVICE_PATH *UfsDevice; + BOOLEAN RetVal; + EFI_DEVICE_PATH_PROTOCOL *DevicePathNode; - UfsDevice = NULL; - RetVal = TRUE; + UfsDevice = NULL; + RetVal = TRUE; Status = gBS->HandleProtocol ( ChildHandle, &gEfiDevicePathProtocolGuid, - (VOID **) &DevicePathNode + (VOID **)&DevicePathNode ); // // Device Path protocol must be installed on the device handle. @@ -6028,13 +6121,15 @@ DetermineInstallStorageSecurity ( // For now, only support Storage Security Command Protocol on UFS devices. // if ((DevicePathNode->Type == MESSAGING_DEVICE_PATH) && - (DevicePathNode->SubType == MSG_UFS_DP)) { - UfsDevice = (UFS_DEVICE_PATH *) DevicePathNode; + (DevicePathNode->SubType == MSG_UFS_DP)) + { + UfsDevice = (UFS_DEVICE_PATH *)DevicePathNode; break; } DevicePathNode = NextDevicePathNode (DevicePathNode); } + if (UfsDevice == NULL) { RetVal = FALSE; goto Done; @@ -6067,26 +6162,26 @@ Done: EFI_STATUS EFIAPI ScsiDiskInfoInquiry ( - IN EFI_DISK_INFO_PROTOCOL *This, - IN OUT VOID *InquiryData, - IN OUT UINT32 *InquiryDataSize + IN EFI_DISK_INFO_PROTOCOL *This, + IN OUT VOID *InquiryData, + IN OUT UINT32 *InquiryDataSize ) { - EFI_STATUS Status; - SCSI_DISK_DEV *ScsiDiskDevice; + EFI_STATUS Status; + SCSI_DISK_DEV *ScsiDiskDevice; - ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This); + ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This); Status = EFI_BUFFER_TOO_SMALL; if (*InquiryDataSize >= sizeof (ScsiDiskDevice->InquiryData)) { Status = EFI_SUCCESS; CopyMem (InquiryData, &ScsiDiskDevice->InquiryData, sizeof (ScsiDiskDevice->InquiryData)); } + *InquiryDataSize = sizeof (ScsiDiskDevice->InquiryData); return Status; } - /** Provides identify information for the controller type. @@ -6108,13 +6203,13 @@ ScsiDiskInfoInquiry ( EFI_STATUS EFIAPI ScsiDiskInfoIdentify ( - IN EFI_DISK_INFO_PROTOCOL *This, - IN OUT VOID *IdentifyData, - IN OUT UINT32 *IdentifyDataSize + IN EFI_DISK_INFO_PROTOCOL *This, + IN OUT VOID *IdentifyData, + IN OUT UINT32 *IdentifyDataSize ) { - EFI_STATUS Status; - SCSI_DISK_DEV *ScsiDiskDevice; + EFI_STATUS Status; + SCSI_DISK_DEV *ScsiDiskDevice; if (CompareGuid (&This->Interface, &gEfiDiskInfoScsiInterfaceGuid) || CompareGuid (&This->Interface, &gEfiDiskInfoUfsInterfaceGuid)) { // @@ -6123,13 +6218,14 @@ ScsiDiskInfoIdentify ( return EFI_NOT_FOUND; } - ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This); + ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This); Status = EFI_BUFFER_TOO_SMALL; if (*IdentifyDataSize >= sizeof (ScsiDiskDevice->IdentifyData)) { Status = EFI_SUCCESS; CopyMem (IdentifyData, &ScsiDiskDevice->IdentifyData, sizeof (ScsiDiskDevice->IdentifyData)); } + *IdentifyDataSize = sizeof (ScsiDiskDevice->IdentifyData); return Status; } @@ -6154,16 +6250,15 @@ ScsiDiskInfoIdentify ( EFI_STATUS EFIAPI ScsiDiskInfoSenseData ( - IN EFI_DISK_INFO_PROTOCOL *This, - IN OUT VOID *SenseData, - IN OUT UINT32 *SenseDataSize, - OUT UINT8 *SenseDataNumber + IN EFI_DISK_INFO_PROTOCOL *This, + IN OUT VOID *SenseData, + IN OUT UINT32 *SenseDataSize, + OUT UINT8 *SenseDataNumber ) { return EFI_NOT_FOUND; } - /** This function is used by the IDE bus driver to get controller information. @@ -6178,12 +6273,12 @@ ScsiDiskInfoSenseData ( EFI_STATUS EFIAPI ScsiDiskInfoWhichIde ( - IN EFI_DISK_INFO_PROTOCOL *This, - OUT UINT32 *IdeChannel, - OUT UINT32 *IdeDevice + IN EFI_DISK_INFO_PROTOCOL *This, + OUT UINT32 *IdeChannel, + OUT UINT32 *IdeDevice ) { - SCSI_DISK_DEV *ScsiDiskDevice; + SCSI_DISK_DEV *ScsiDiskDevice; if (CompareGuid (&This->Interface, &gEfiDiskInfoScsiInterfaceGuid) || CompareGuid (&This->Interface, &gEfiDiskInfoUfsInterfaceGuid)) { // @@ -6192,14 +6287,13 @@ ScsiDiskInfoWhichIde ( return EFI_UNSUPPORTED; } - ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This); - *IdeChannel = ScsiDiskDevice->Channel; - *IdeDevice = ScsiDiskDevice->Device; + ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This); + *IdeChannel = ScsiDiskDevice->Channel; + *IdeDevice = ScsiDiskDevice->Device; return EFI_SUCCESS; } - /** Issues ATA IDENTIFY DEVICE command to identify ATAPI device. @@ -6215,11 +6309,11 @@ ScsiDiskInfoWhichIde ( **/ EFI_STATUS AtapiIdentifyDevice ( - IN OUT SCSI_DISK_DEV *ScsiDiskDevice + IN OUT SCSI_DISK_DEV *ScsiDiskDevice ) { - EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket; - UINT8 Cdb[6]; + EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket; + UINT8 Cdb[6]; // // Initialize SCSI REQUEST_PACKET and 6-byte Cdb @@ -6227,17 +6321,16 @@ AtapiIdentifyDevice ( ZeroMem (&CommandPacket, sizeof (CommandPacket)); ZeroMem (Cdb, sizeof (Cdb)); - Cdb[0] = ATA_CMD_IDENTIFY_DEVICE; - CommandPacket.Timeout = SCSI_DISK_TIMEOUT; - CommandPacket.Cdb = Cdb; - CommandPacket.CdbLength = (UINT8) sizeof (Cdb); - CommandPacket.InDataBuffer = &ScsiDiskDevice->IdentifyData; + Cdb[0] = ATA_CMD_IDENTIFY_DEVICE; + CommandPacket.Timeout = SCSI_DISK_TIMEOUT; + CommandPacket.Cdb = Cdb; + CommandPacket.CdbLength = (UINT8)sizeof (Cdb); + CommandPacket.InDataBuffer = &ScsiDiskDevice->IdentifyData; CommandPacket.InTransferLength = sizeof (ScsiDiskDevice->IdentifyData); return ScsiDiskDevice->ScsiIo->ExecuteScsiCommand (ScsiDiskDevice->ScsiIo, &CommandPacket, NULL); } - /** Initialize the installation of DiskInfo protocol. @@ -6252,8 +6345,8 @@ AtapiIdentifyDevice ( **/ VOID InitializeInstallDiskInfo ( - IN SCSI_DISK_DEV *ScsiDiskDevice, - IN EFI_HANDLE ChildHandle + IN SCSI_DISK_DEV *ScsiDiskDevice, + IN EFI_HANDLE ChildHandle ) { EFI_STATUS Status; @@ -6263,7 +6356,7 @@ InitializeInstallDiskInfo ( SATA_DEVICE_PATH *SataDevicePath; UINTN IdentifyRetry; - Status = gBS->HandleProtocol (ChildHandle, &gEfiDevicePathProtocolGuid, (VOID **) &DevicePathNode); + Status = gBS->HandleProtocol (ChildHandle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePathNode); // // Device Path protocol must be installed on the device handle. // @@ -6278,9 +6371,9 @@ InitializeInstallDiskInfo ( if ((DevicePathType (DevicePathNode) == HARDWARE_DEVICE_PATH) && (DevicePathSubType (DevicePathNode) == HW_PCI_DP) && (DevicePathType (ChildDevicePathNode) == MESSAGING_DEVICE_PATH) && - ((DevicePathSubType (ChildDevicePathNode) == MSG_ATAPI_DP) || - (DevicePathSubType (ChildDevicePathNode) == MSG_SATA_DP))) { - + ((DevicePathSubType (ChildDevicePathNode) == MSG_ATAPI_DP) || + (DevicePathSubType (ChildDevicePathNode) == MSG_SATA_DP))) + { IdentifyRetry = 3; do { // @@ -6289,13 +6382,13 @@ InitializeInstallDiskInfo ( // Status = AtapiIdentifyDevice (ScsiDiskDevice); if (!EFI_ERROR (Status)) { - if (DevicePathSubType(ChildDevicePathNode) == MSG_ATAPI_DP) { + if (DevicePathSubType (ChildDevicePathNode) == MSG_ATAPI_DP) { // // We find the valid ATAPI device path // - AtapiDevicePath = (ATAPI_DEVICE_PATH *) ChildDevicePathNode; + AtapiDevicePath = (ATAPI_DEVICE_PATH *)ChildDevicePathNode; ScsiDiskDevice->Channel = AtapiDevicePath->PrimarySecondary; - ScsiDiskDevice->Device = AtapiDevicePath->SlaveMaster; + ScsiDiskDevice->Device = AtapiDevicePath->SlaveMaster; // // Update the DiskInfo.Interface to IDE interface GUID for the physical ATAPI device. // @@ -6304,22 +6397,25 @@ InitializeInstallDiskInfo ( // // We find the valid SATA device path // - SataDevicePath = (SATA_DEVICE_PATH *) ChildDevicePathNode; + SataDevicePath = (SATA_DEVICE_PATH *)ChildDevicePathNode; ScsiDiskDevice->Channel = SataDevicePath->HBAPortNumber; - ScsiDiskDevice->Device = SataDevicePath->PortMultiplierPortNumber; + ScsiDiskDevice->Device = SataDevicePath->PortMultiplierPortNumber; // // Update the DiskInfo.Interface to AHCI interface GUID for the physical AHCI device. // CopyGuid (&ScsiDiskDevice->DiskInfo.Interface, &gEfiDiskInfoAhciInterfaceGuid); } + return; } } while (--IdentifyRetry > 0); } else if ((DevicePathType (ChildDevicePathNode) == MESSAGING_DEVICE_PATH) && - (DevicePathSubType (ChildDevicePathNode) == MSG_UFS_DP)) { + (DevicePathSubType (ChildDevicePathNode) == MSG_UFS_DP)) + { CopyGuid (&ScsiDiskDevice->DiskInfo.Interface, &gEfiDiskInfoUfsInterfaceGuid); break; } + DevicePathNode = ChildDevicePathNode; }