2 SCSI disk driver that layers on every SCSI IO protocol in the system.
4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 EFI_DRIVER_BINDING_PROTOCOL gScsiDiskDriverBinding
= {
19 ScsiDiskDriverBindingSupported
,
20 ScsiDiskDriverBindingStart
,
21 ScsiDiskDriverBindingStop
,
27 EFI_DISK_INFO_PROTOCOL gScsiDiskInfoProtocolTemplate
= {
28 EFI_DISK_INFO_SCSI_INTERFACE_GUID
,
31 ScsiDiskInfoSenseData
,
36 Allocates an aligned buffer for SCSI disk.
38 This function allocates an aligned buffer for the SCSI disk to perform
39 SCSI IO operations. The alignment requirement is from SCSI IO interface.
41 @param ScsiDiskDevice The SCSI disk involved for the operation.
42 @param BufferSize The request buffer size.
44 @return A pointer to the aligned buffer or NULL if the allocation fails.
48 AllocateAlignedBuffer (
49 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
53 return AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize
), ScsiDiskDevice
->ScsiIo
->IoAlign
);
57 Frees an aligned buffer for SCSI disk.
59 This function frees an aligned buffer for the SCSI disk to perform
62 @param Buffer The aligned buffer to be freed.
63 @param BufferSize The request buffer size.
73 FreeAlignedPages (Buffer
, EFI_SIZE_TO_PAGES (BufferSize
));
78 The user Entry Point for module ScsiDisk.
80 The user code starts with this function.
82 @param ImageHandle The firmware allocated handle for the EFI image.
83 @param SystemTable A pointer to the EFI System Table.
85 @retval EFI_SUCCESS The entry point is executed successfully.
86 @retval other Some error occurs when executing this entry point.
92 IN EFI_HANDLE ImageHandle
,
93 IN EFI_SYSTEM_TABLE
*SystemTable
99 // Install driver model protocol(s).
101 Status
= EfiLibInstallDriverBindingComponentName2 (
104 &gScsiDiskDriverBinding
,
106 &gScsiDiskComponentName
,
107 &gScsiDiskComponentName2
109 ASSERT_EFI_ERROR (Status
);
116 Test to see if this driver supports ControllerHandle.
118 This service is called by the EFI boot service ConnectController(). In order
119 to make drivers as small as possible, there are a few calling restrictions for
120 this service. ConnectController() must follow these calling restrictions.
121 If any other agent wishes to call Supported() it must also follow these
122 calling restrictions.
124 @param This Protocol instance pointer.
125 @param ControllerHandle Handle of device to test
126 @param RemainingDevicePath Optional parameter use to pick a specific child
129 @retval EFI_SUCCESS This driver supports this device
130 @retval EFI_ALREADY_STARTED This driver is already running on this device
131 @retval other This driver does not support this device
136 ScsiDiskDriverBindingSupported (
137 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
138 IN EFI_HANDLE Controller
,
139 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
143 EFI_SCSI_IO_PROTOCOL
*ScsiIo
;
146 Status
= gBS
->OpenProtocol (
148 &gEfiScsiIoProtocolGuid
,
150 This
->DriverBindingHandle
,
152 EFI_OPEN_PROTOCOL_BY_DRIVER
154 if (EFI_ERROR (Status
)) {
158 Status
= ScsiIo
->GetDeviceType (ScsiIo
, &DeviceType
);
159 if (!EFI_ERROR (Status
)) {
160 if ((DeviceType
== EFI_SCSI_TYPE_DISK
) || (DeviceType
== EFI_SCSI_TYPE_CDROM
)) {
161 Status
= EFI_SUCCESS
;
163 Status
= EFI_UNSUPPORTED
;
169 &gEfiScsiIoProtocolGuid
,
170 This
->DriverBindingHandle
,
178 Start this driver on ControllerHandle.
180 This service is called by the EFI boot service ConnectController(). In order
181 to make drivers as small as possible, there are a few calling restrictions for
182 this service. ConnectController() must follow these calling restrictions. If
183 any other agent wishes to call Start() it must also follow these calling
186 @param This Protocol instance pointer.
187 @param ControllerHandle Handle of device to bind driver to
188 @param RemainingDevicePath Optional parameter use to pick a specific child
191 @retval EFI_SUCCESS This driver is added to ControllerHandle
192 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
193 @retval other This driver does not support this device
198 ScsiDiskDriverBindingStart (
199 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
200 IN EFI_HANDLE Controller
,
201 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
205 EFI_SCSI_IO_PROTOCOL
*ScsiIo
;
206 SCSI_DISK_DEV
*ScsiDiskDevice
;
211 BOOLEAN MustReadCapacity
;
213 MustReadCapacity
= TRUE
;
215 ScsiDiskDevice
= (SCSI_DISK_DEV
*) AllocateZeroPool (sizeof (SCSI_DISK_DEV
));
216 if (ScsiDiskDevice
== NULL
) {
217 return EFI_OUT_OF_RESOURCES
;
220 Status
= gBS
->OpenProtocol (
222 &gEfiScsiIoProtocolGuid
,
224 This
->DriverBindingHandle
,
226 EFI_OPEN_PROTOCOL_BY_DRIVER
228 if (EFI_ERROR (Status
)) {
229 FreePool (ScsiDiskDevice
);
233 ScsiDiskDevice
->Signature
= SCSI_DISK_DEV_SIGNATURE
;
234 ScsiDiskDevice
->ScsiIo
= ScsiIo
;
235 ScsiDiskDevice
->BlkIo
.Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
236 ScsiDiskDevice
->BlkIo
.Media
= &ScsiDiskDevice
->BlkIoMedia
;
237 ScsiDiskDevice
->BlkIo
.Reset
= ScsiDiskReset
;
238 ScsiDiskDevice
->BlkIo
.ReadBlocks
= ScsiDiskReadBlocks
;
239 ScsiDiskDevice
->BlkIo
.WriteBlocks
= ScsiDiskWriteBlocks
;
240 ScsiDiskDevice
->BlkIo
.FlushBlocks
= ScsiDiskFlushBlocks
;
241 ScsiDiskDevice
->Handle
= Controller
;
243 ScsiIo
->GetDeviceType (ScsiIo
, &(ScsiDiskDevice
->DeviceType
));
244 switch (ScsiDiskDevice
->DeviceType
) {
245 case EFI_SCSI_TYPE_DISK
:
246 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x200;
247 MustReadCapacity
= TRUE
;
250 case EFI_SCSI_TYPE_CDROM
:
251 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x800;
252 MustReadCapacity
= FALSE
;
256 // The Sense Data Array's initial size is 6
258 ScsiDiskDevice
->SenseDataNumber
= 6;
259 ScsiDiskDevice
->SenseData
= (EFI_SCSI_SENSE_DATA
*) AllocateZeroPool (
260 sizeof (EFI_SCSI_SENSE_DATA
) * ScsiDiskDevice
->SenseDataNumber
262 if (ScsiDiskDevice
->SenseData
== NULL
) {
265 &gEfiScsiIoProtocolGuid
,
266 This
->DriverBindingHandle
,
269 FreePool (ScsiDiskDevice
);
270 return EFI_OUT_OF_RESOURCES
;
274 // Retrieve device information
277 for (Index
= 0; Index
< MaxRetry
; Index
++) {
278 Status
= ScsiDiskInquiryDevice (ScsiDiskDevice
, &NeedRetry
);
279 if (!EFI_ERROR (Status
)) {
284 FreePool (ScsiDiskDevice
->SenseData
);
287 &gEfiScsiIoProtocolGuid
,
288 This
->DriverBindingHandle
,
291 FreePool (ScsiDiskDevice
);
292 return EFI_DEVICE_ERROR
;
296 // The second parameter "TRUE" means must
297 // retrieve media capacity
299 Status
= ScsiDiskDetectMedia (ScsiDiskDevice
, MustReadCapacity
, &Temp
);
300 if (!EFI_ERROR (Status
)) {
302 // Determine if Block IO should be produced on this controller handle
304 if (DetermineInstallBlockIo(Controller
)) {
305 InitializeInstallDiskInfo(ScsiDiskDevice
, Controller
);
306 Status
= gBS
->InstallMultipleProtocolInterfaces (
308 &gEfiBlockIoProtocolGuid
,
309 &ScsiDiskDevice
->BlkIo
,
310 &gEfiDiskInfoProtocolGuid
,
311 &ScsiDiskDevice
->DiskInfo
,
314 if (!EFI_ERROR(Status
)) {
315 ScsiDiskDevice
->ControllerNameTable
= NULL
;
318 gScsiDiskComponentName
.SupportedLanguages
,
319 &ScsiDiskDevice
->ControllerNameTable
,
325 gScsiDiskComponentName2
.SupportedLanguages
,
326 &ScsiDiskDevice
->ControllerNameTable
,
335 gBS
->FreePool (ScsiDiskDevice
->SenseData
);
336 gBS
->FreePool (ScsiDiskDevice
);
339 &gEfiScsiIoProtocolGuid
,
340 This
->DriverBindingHandle
,
349 Stop this driver on ControllerHandle.
351 This service is called by the EFI boot service DisconnectController().
352 In order to make drivers as small as possible, there are a few calling
353 restrictions for this service. DisconnectController() must follow these
354 calling restrictions. If any other agent wishes to call Stop() it must
355 also follow these calling restrictions.
357 @param This Protocol instance pointer.
358 @param ControllerHandle Handle of device to stop driver on
359 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
360 children is zero stop the entire bus driver.
361 @param ChildHandleBuffer List of Child Handles to Stop.
363 @retval EFI_SUCCESS This driver is removed ControllerHandle
364 @retval other This driver was not removed from this device
369 ScsiDiskDriverBindingStop (
370 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
371 IN EFI_HANDLE Controller
,
372 IN UINTN NumberOfChildren
,
373 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
376 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
377 SCSI_DISK_DEV
*ScsiDiskDevice
;
380 Status
= gBS
->OpenProtocol (
382 &gEfiBlockIoProtocolGuid
,
384 This
->DriverBindingHandle
,
386 EFI_OPEN_PROTOCOL_GET_PROTOCOL
388 if (EFI_ERROR (Status
)) {
392 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (BlkIo
);
393 Status
= gBS
->UninstallMultipleProtocolInterfaces (
395 &gEfiBlockIoProtocolGuid
,
396 &ScsiDiskDevice
->BlkIo
,
397 &gEfiDiskInfoProtocolGuid
,
398 &ScsiDiskDevice
->DiskInfo
,
401 if (!EFI_ERROR (Status
)) {
404 &gEfiScsiIoProtocolGuid
,
405 This
->DriverBindingHandle
,
409 ReleaseScsiDiskDeviceResources (ScsiDiskDevice
);
423 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
424 @param ExtendedVerification The flag about if extend verificate
426 @retval EFI_SUCCESS The device was reset.
427 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
429 @return EFI_STATUS is returned from EFI_SCSI_IO_PROTOCOL.ResetDevice().
435 IN EFI_BLOCK_IO_PROTOCOL
*This
,
436 IN BOOLEAN ExtendedVerification
440 SCSI_DISK_DEV
*ScsiDiskDevice
;
443 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
445 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (This
);
447 Status
= ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
449 if (EFI_ERROR (Status
)) {
450 Status
= EFI_DEVICE_ERROR
;
454 if (!ExtendedVerification
) {
458 Status
= ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
460 if (EFI_ERROR (Status
)) {
461 Status
= EFI_DEVICE_ERROR
;
466 gBS
->RestoreTPL (OldTpl
);
471 The function is to Read Block from SCSI Disk.
473 @param This The pointer of EFI_BLOCK_IO_PROTOCOL.
474 @param MediaId The Id of Media detected
475 @param Lba The logic block address
476 @param BufferSize The size of Buffer
477 @param Buffer The buffer to fill the read out data
479 @retval EFI_SUCCESS Successfully to read out block.
480 @retval EFI_DEVICE_ERROR Fail to detect media.
481 @retval EFI_NO_MEDIA Media is not present.
482 @retval EFI_MEDIA_CHANGED Media has changed.
483 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
484 @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
490 IN EFI_BLOCK_IO_PROTOCOL
*This
,
497 SCSI_DISK_DEV
*ScsiDiskDevice
;
498 EFI_BLOCK_IO_MEDIA
*Media
;
501 UINTN NumberOfBlocks
;
506 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
507 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (This
);
509 if (!IS_DEVICE_FIXED(ScsiDiskDevice
)) {
511 Status
= ScsiDiskDetectMedia (ScsiDiskDevice
, FALSE
, &MediaChange
);
512 if (EFI_ERROR (Status
)) {
513 Status
= EFI_DEVICE_ERROR
;
518 gBS
->ReinstallProtocolInterface (
519 ScsiDiskDevice
->Handle
,
520 &gEfiBlockIoProtocolGuid
,
521 &ScsiDiskDevice
->BlkIo
,
522 &ScsiDiskDevice
->BlkIo
524 Status
= EFI_MEDIA_CHANGED
;
529 // Get the intrinsic block size
531 Media
= ScsiDiskDevice
->BlkIo
.Media
;
532 BlockSize
= Media
->BlockSize
;
534 NumberOfBlocks
= BufferSize
/ BlockSize
;
536 if (!(Media
->MediaPresent
)) {
537 Status
= EFI_NO_MEDIA
;
541 if (MediaId
!= Media
->MediaId
) {
542 Status
= EFI_MEDIA_CHANGED
;
546 if (Buffer
== NULL
) {
547 Status
= EFI_INVALID_PARAMETER
;
551 if (BufferSize
== 0) {
552 Status
= EFI_SUCCESS
;
556 if (BufferSize
% BlockSize
!= 0) {
557 Status
= EFI_BAD_BUFFER_SIZE
;
561 if (Lba
> Media
->LastBlock
) {
562 Status
= EFI_INVALID_PARAMETER
;
566 if ((Lba
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
567 Status
= EFI_INVALID_PARAMETER
;
571 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
572 Status
= EFI_INVALID_PARAMETER
;
577 // If all the parameters are valid, then perform read sectors command
578 // to transfer data from device to host.
580 Status
= ScsiDiskReadSectors (ScsiDiskDevice
, Buffer
, Lba
, NumberOfBlocks
);
583 gBS
->RestoreTPL (OldTpl
);
588 The function is to Write Block to SCSI Disk.
590 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
591 @param MediaId The Id of Media detected
592 @param Lba The logic block address
593 @param BufferSize The size of Buffer
594 @param Buffer The buffer to fill the read out data
596 @retval EFI_SUCCESS Successfully to read out block.
597 @retval EFI_WRITE_PROTECTED The device can not be written to.
598 @retval EFI_DEVICE_ERROR Fail to detect media.
599 @retval EFI_NO_MEDIA Media is not present.
600 @retval EFI_MEDIA_CHNAGED Media has changed.
601 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
602 @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
607 ScsiDiskWriteBlocks (
608 IN EFI_BLOCK_IO_PROTOCOL
*This
,
615 SCSI_DISK_DEV
*ScsiDiskDevice
;
616 EFI_BLOCK_IO_MEDIA
*Media
;
619 UINTN NumberOfBlocks
;
624 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
625 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (This
);
627 if (!IS_DEVICE_FIXED(ScsiDiskDevice
)) {
629 Status
= ScsiDiskDetectMedia (ScsiDiskDevice
, FALSE
, &MediaChange
);
630 if (EFI_ERROR (Status
)) {
631 Status
= EFI_DEVICE_ERROR
;
636 gBS
->ReinstallProtocolInterface (
637 ScsiDiskDevice
->Handle
,
638 &gEfiBlockIoProtocolGuid
,
639 &ScsiDiskDevice
->BlkIo
,
640 &ScsiDiskDevice
->BlkIo
642 Status
= EFI_MEDIA_CHANGED
;
647 // Get the intrinsic block size
649 Media
= ScsiDiskDevice
->BlkIo
.Media
;
650 BlockSize
= Media
->BlockSize
;
652 NumberOfBlocks
= BufferSize
/ BlockSize
;
654 if (!(Media
->MediaPresent
)) {
655 Status
= EFI_NO_MEDIA
;
659 if (MediaId
!= Media
->MediaId
) {
660 Status
= EFI_MEDIA_CHANGED
;
664 if (BufferSize
== 0) {
665 Status
= EFI_SUCCESS
;
669 if (Buffer
== NULL
) {
670 Status
= EFI_INVALID_PARAMETER
;
674 if (BufferSize
% BlockSize
!= 0) {
675 Status
= EFI_BAD_BUFFER_SIZE
;
679 if (Lba
> Media
->LastBlock
) {
680 Status
= EFI_INVALID_PARAMETER
;
684 if ((Lba
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
685 Status
= EFI_INVALID_PARAMETER
;
689 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
690 Status
= EFI_INVALID_PARAMETER
;
694 // if all the parameters are valid, then perform read sectors command
695 // to transfer data from device to host.
697 Status
= ScsiDiskWriteSectors (ScsiDiskDevice
, Buffer
, Lba
, NumberOfBlocks
);
700 gBS
->RestoreTPL (OldTpl
);
707 EFI_SUCCESS is returned directly.
709 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
711 @retval EFI_SUCCESS All outstanding data was written to the device
716 ScsiDiskFlushBlocks (
717 IN EFI_BLOCK_IO_PROTOCOL
*This
728 Detect Device and read out capacity ,if error occurs, parse the sense key.
730 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
731 @param MustReadCapacity The flag about reading device capacity
732 @param MediaChange The pointer of flag indicates if media has changed
734 @retval EFI_DEVICE_ERROR Indicates that error occurs
735 @retval EFI_SUCCESS Successfully to detect media
739 ScsiDiskDetectMedia (
740 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
741 IN BOOLEAN MustReadCapacity
,
742 OUT BOOLEAN
*MediaChange
746 EFI_SCSI_SENSE_DATA
*SenseData
;
747 UINTN NumberOfSenseKeys
;
749 BOOLEAN NeedReadCapacity
;
752 EFI_BLOCK_IO_MEDIA OldMedia
;
754 EFI_EVENT TimeoutEvt
;
756 Status
= EFI_SUCCESS
;
758 NumberOfSenseKeys
= 0;
761 Action
= ACTION_NO_ACTION
;
762 NeedReadCapacity
= FALSE
;
763 *MediaChange
= FALSE
;
766 CopyMem (&OldMedia
, ScsiDiskDevice
->BlkIo
.Media
, sizeof (OldMedia
));
768 Status
= gBS
->CreateEvent (
775 if (EFI_ERROR (Status
)) {
779 Status
= gBS
->SetTimer (TimeoutEvt
, TimerRelative
, EFI_TIMER_PERIOD_SECONDS(120));
780 if (EFI_ERROR (Status
)) {
785 // Sending Test_Unit cmd to poll device status.
786 // If the sense data shows the drive is not ready or reset before, we need poll the device status again.
787 // We limit the upper boundary to 120 seconds.
789 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvt
))) {
790 Status
= ScsiDiskTestUnitReady (
796 if (!EFI_ERROR (Status
)) {
797 Status
= DetectMediaParsingSenseKeys (
803 if (EFI_ERROR (Status
)) {
805 } else if (Action
== ACTION_RETRY_COMMAND_LATER
) {
812 if (!NeedRetry
|| (Retry
>= MaxRetry
)) {
818 if (EFI_ERROR (Status
)) {
823 // ACTION_NO_ACTION: need not read capacity
824 // other action code: need read capacity
826 if (Action
== ACTION_READ_CAPACITY
) {
827 NeedReadCapacity
= TRUE
;
831 // either NeedReadCapacity is TRUE, or MustReadCapacity is TRUE,
832 // retrieve capacity via Read Capacity command
834 if (NeedReadCapacity
|| MustReadCapacity
) {
836 // retrieve media information
838 for (Retry
= 0; Retry
< MaxRetry
; Retry
++) {
839 Status
= ScsiDiskReadCapacity (
845 if (!EFI_ERROR (Status
)) {
847 // analyze sense key to action
849 Status
= DetectMediaParsingSenseKeys (
855 if (EFI_ERROR (Status
)) {
857 // if Status is error, it may indicate crisis error,
858 // so return without retry.
861 } else if (Action
== ACTION_RETRY_COMMAND_LATER
) {
869 if (!NeedRetry
|| (Retry
>= MaxRetry
)) {
875 if (EFI_ERROR (Status
)) {
880 if (ScsiDiskDevice
->BlkIo
.Media
->MediaId
!= OldMedia
.MediaId
) {
882 // Media change information got from the device
887 if (ScsiDiskDevice
->BlkIo
.Media
->ReadOnly
!= OldMedia
.ReadOnly
) {
889 ScsiDiskDevice
->BlkIo
.Media
->MediaId
+= 1;
892 if (ScsiDiskDevice
->BlkIo
.Media
->BlockSize
!= OldMedia
.BlockSize
) {
894 ScsiDiskDevice
->BlkIo
.Media
->MediaId
+= 1;
897 if (ScsiDiskDevice
->BlkIo
.Media
->LastBlock
!= OldMedia
.LastBlock
) {
899 ScsiDiskDevice
->BlkIo
.Media
->MediaId
+= 1;
902 if (ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
!= OldMedia
.MediaPresent
) {
903 if (ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
) {
905 // when change from no media to media present, reset the MediaId to 1.
907 ScsiDiskDevice
->BlkIo
.Media
->MediaId
= 1;
910 // when no media, reset the MediaId to zero.
912 ScsiDiskDevice
->BlkIo
.Media
->MediaId
= 0;
919 if (TimeoutEvt
!= NULL
) {
920 gBS
->CloseEvent (TimeoutEvt
);
927 Send out Inquiry command to Device.
929 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
930 @param NeedRetry Indicates if needs try again when error happens
932 @retval EFI_DEVICE_ERROR Indicates that error occurs
933 @retval EFI_SUCCESS Successfully to detect media
937 ScsiDiskInquiryDevice (
938 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
939 OUT BOOLEAN
*NeedRetry
942 UINT32 InquiryDataLength
;
943 UINT8 SenseDataLength
;
944 UINT8 HostAdapterStatus
;
946 EFI_SCSI_SENSE_DATA
*SenseDataArray
;
947 UINTN NumberOfSenseKeys
;
951 EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE
*SupportedVpdPages
;
952 EFI_SCSI_BLOCK_LIMITS_VPD_PAGE
*BlockLimits
;
955 InquiryDataLength
= sizeof (EFI_SCSI_INQUIRY_DATA
);
958 Status
= ScsiInquiryCommand (
959 ScsiDiskDevice
->ScsiIo
,
965 (VOID
*) &(ScsiDiskDevice
->InquiryData
),
970 // no need to check HostAdapterStatus and TargetStatus
972 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_WARN_BUFFER_TOO_SMALL
)) {
973 ParseInquiryData (ScsiDiskDevice
);
975 if (ScsiDiskDevice
->DeviceType
== EFI_SCSI_TYPE_DISK
) {
977 // Check whether the device supports Block Limits VPD page (0xB0)
979 SupportedVpdPages
= AllocateAlignedBuffer (ScsiDiskDevice
, sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE
));
980 if (SupportedVpdPages
== NULL
) {
982 return EFI_DEVICE_ERROR
;
984 ZeroMem (SupportedVpdPages
, sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE
));
985 InquiryDataLength
= sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE
);
987 Status
= ScsiInquiryCommandEx (
988 ScsiDiskDevice
->ScsiIo
,
994 (VOID
*) SupportedVpdPages
,
997 EFI_SCSI_PAGE_CODE_SUPPORTED_VPD
999 if (!EFI_ERROR (Status
)) {
1000 PageLength
= (SupportedVpdPages
->PageLength2
<< 8)
1001 | SupportedVpdPages
->PageLength1
;
1002 for (Index
= 0; Index
< PageLength
; Index
++) {
1003 if (SupportedVpdPages
->SupportedVpdPageList
[Index
] == EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD
) {
1009 // Query the Block Limits VPD page
1011 if (Index
< PageLength
) {
1012 BlockLimits
= AllocateAlignedBuffer (ScsiDiskDevice
, sizeof (EFI_SCSI_BLOCK_LIMITS_VPD_PAGE
));
1013 if (BlockLimits
== NULL
) {
1014 FreeAlignedBuffer (SupportedVpdPages
, sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE
));
1016 return EFI_DEVICE_ERROR
;
1018 ZeroMem (BlockLimits
, sizeof (EFI_SCSI_BLOCK_LIMITS_VPD_PAGE
));
1019 InquiryDataLength
= sizeof (EFI_SCSI_BLOCK_LIMITS_VPD_PAGE
);
1020 SenseDataLength
= 0;
1021 Status
= ScsiInquiryCommandEx (
1022 ScsiDiskDevice
->ScsiIo
,
1028 (VOID
*) BlockLimits
,
1031 EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD
1033 if (!EFI_ERROR (Status
)) {
1034 ScsiDiskDevice
->BlkIo
.Media
->OptimalTransferLengthGranularity
=
1035 (BlockLimits
->OptimalTransferLengthGranularity2
<< 8) |
1036 BlockLimits
->OptimalTransferLengthGranularity1
;
1039 FreeAlignedBuffer (BlockLimits
, sizeof (EFI_SCSI_BLOCK_LIMITS_VPD_PAGE
));
1043 FreeAlignedBuffer (SupportedVpdPages
, sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE
));
1047 if (!EFI_ERROR (Status
)) {
1050 } else if (Status
== EFI_NOT_READY
) {
1052 return EFI_DEVICE_ERROR
;
1054 } else if ((Status
== EFI_INVALID_PARAMETER
) || (Status
== EFI_UNSUPPORTED
)) {
1056 return EFI_DEVICE_ERROR
;
1059 // go ahead to check HostAdapterStatus and TargetStatus
1060 // (EFI_TIMEOUT, EFI_DEVICE_ERROR)
1063 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
1064 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
1066 return EFI_DEVICE_ERROR
;
1067 } else if (Status
== EFI_DEVICE_ERROR
) {
1069 // reset the scsi channel
1071 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
1073 return EFI_DEVICE_ERROR
;
1076 Status
= CheckTargetStatus (TargetStatus
);
1077 if (Status
== EFI_NOT_READY
) {
1079 // reset the scsi device
1081 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
1083 return EFI_DEVICE_ERROR
;
1085 } else if (Status
== EFI_DEVICE_ERROR
) {
1087 return EFI_DEVICE_ERROR
;
1091 // if goes here, meant ScsiInquiryCommand() failed.
1092 // if ScsiDiskRequestSenseKeys() succeeds at last,
1093 // better retry ScsiInquiryCommand(). (by setting *NeedRetry = TRUE)
1096 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1097 Status
= ScsiDiskRequestSenseKeys (
1104 if (!EFI_ERROR (Status
)) {
1106 return EFI_DEVICE_ERROR
;
1110 return EFI_DEVICE_ERROR
;
1114 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
1115 // set *NeedRetry = FALSE to avoid the outside caller try again.
1118 return EFI_DEVICE_ERROR
;
1124 When Test Unit Ready command succeeds, retrieve Sense Keys via Request Sense;
1125 When Test Unit Ready command encounters any error caused by host adapter or
1126 target, return error without retrieving Sense Keys.
1128 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1129 @param NeedRetry The pointer of flag indicates try again
1130 @param SenseDataArray The pointer of an array of sense data
1131 @param NumberOfSenseKeys The pointer of the number of sense data array
1133 @retval EFI_DEVICE_ERROR Indicates that error occurs
1134 @retval EFI_SUCCESS Successfully to test unit
1138 ScsiDiskTestUnitReady (
1139 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1140 OUT BOOLEAN
*NeedRetry
,
1141 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
1142 OUT UINTN
*NumberOfSenseKeys
1146 UINT8 SenseDataLength
;
1147 UINT8 HostAdapterStatus
;
1152 SenseDataLength
= 0;
1153 *NumberOfSenseKeys
= 0;
1156 // Parameter 3 and 4: do not require sense data, retrieve it when needed.
1158 Status
= ScsiTestUnitReadyCommand (
1159 ScsiDiskDevice
->ScsiIo
,
1167 // no need to check HostAdapterStatus and TargetStatus
1169 if (Status
== EFI_NOT_READY
) {
1171 return EFI_DEVICE_ERROR
;
1173 } else if ((Status
== EFI_INVALID_PARAMETER
) || (Status
== EFI_UNSUPPORTED
)) {
1175 return EFI_DEVICE_ERROR
;
1178 // go ahead to check HostAdapterStatus and TargetStatus(in case of EFI_DEVICE_ERROR)
1181 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
1182 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
1184 return EFI_DEVICE_ERROR
;
1186 } else if (Status
== EFI_DEVICE_ERROR
) {
1188 // reset the scsi channel
1190 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
1192 return EFI_DEVICE_ERROR
;
1195 Status
= CheckTargetStatus (TargetStatus
);
1196 if (Status
== EFI_NOT_READY
) {
1198 // reset the scsi device
1200 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
1202 return EFI_DEVICE_ERROR
;
1204 } else if (Status
== EFI_DEVICE_ERROR
) {
1206 return EFI_DEVICE_ERROR
;
1210 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1211 Status
= ScsiDiskRequestSenseKeys (
1218 if (!EFI_ERROR (Status
)) {
1223 return EFI_DEVICE_ERROR
;
1227 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
1228 // set *NeedRetry = FALSE to avoid the outside caller try again.
1231 return EFI_DEVICE_ERROR
;
1235 Parsing Sense Keys which got from request sense command.
1237 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1238 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
1239 @param NumberOfSenseKeys The number of sense key
1240 @param Action The pointer of action which indicates what is need to do next
1242 @retval EFI_DEVICE_ERROR Indicates that error occurs
1243 @retval EFI_SUCCESS Successfully to complete the parsing
1247 DetectMediaParsingSenseKeys (
1248 OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1249 IN EFI_SCSI_SENSE_DATA
*SenseData
,
1250 IN UINTN NumberOfSenseKeys
,
1257 // Default is to read capacity, unless..
1259 *Action
= ACTION_READ_CAPACITY
;
1261 if (NumberOfSenseKeys
== 0) {
1262 if (ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
== TRUE
) {
1263 *Action
= ACTION_NO_ACTION
;
1268 if (!ScsiDiskHaveSenseKey (SenseData
, NumberOfSenseKeys
)) {
1270 // No Sense Key returned from last submitted command
1272 if (ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
== TRUE
) {
1273 *Action
= ACTION_NO_ACTION
;
1278 if (ScsiDiskIsNoMedia (SenseData
, NumberOfSenseKeys
)) {
1279 ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
= FALSE
;
1280 ScsiDiskDevice
->BlkIo
.Media
->LastBlock
= 0;
1281 *Action
= ACTION_NO_ACTION
;
1282 DEBUG ((EFI_D_VERBOSE
, "ScsiDisk: ScsiDiskIsNoMedia\n"));
1286 if (ScsiDiskIsMediaChange (SenseData
, NumberOfSenseKeys
)) {
1287 ScsiDiskDevice
->BlkIo
.Media
->MediaId
++;
1288 DEBUG ((EFI_D_VERBOSE
, "ScsiDisk: ScsiDiskIsMediaChange!\n"));
1292 if (ScsiDiskIsResetBefore (SenseData
, NumberOfSenseKeys
)) {
1293 *Action
= ACTION_RETRY_COMMAND_LATER
;
1294 DEBUG ((EFI_D_VERBOSE
, "ScsiDisk: ScsiDiskIsResetBefore!\n"));
1298 if (ScsiDiskIsMediaError (SenseData
, NumberOfSenseKeys
)) {
1299 DEBUG ((EFI_D_VERBOSE
, "ScsiDisk: ScsiDiskIsMediaError\n"));
1300 *Action
= ACTION_RETRY_WITH_BACKOFF_ALGO
;
1301 return EFI_DEVICE_ERROR
;
1304 if (ScsiDiskIsHardwareError (SenseData
, NumberOfSenseKeys
)) {
1305 DEBUG ((EFI_D_VERBOSE
, "ScsiDisk: ScsiDiskIsHardwareError\n"));
1306 *Action
= ACTION_RETRY_WITH_BACKOFF_ALGO
;
1307 return EFI_DEVICE_ERROR
;
1310 if (!ScsiDiskIsDriveReady (SenseData
, NumberOfSenseKeys
, &RetryLater
)) {
1312 *Action
= ACTION_RETRY_COMMAND_LATER
;
1313 DEBUG ((EFI_D_VERBOSE
, "ScsiDisk: ScsiDiskDriveNotReady!\n"));
1316 *Action
= ACTION_NO_ACTION
;
1317 return EFI_DEVICE_ERROR
;
1320 *Action
= ACTION_RETRY_WITH_BACKOFF_ALGO
;
1321 DEBUG ((EFI_D_VERBOSE
, "ScsiDisk: Sense Key = 0x%x ASC = 0x%x!\n", SenseData
->Sense_Key
, SenseData
->Addnl_Sense_Code
));
1327 Send read capacity command to device and get the device parameter.
1329 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1330 @param NeedRetry The pointer of flag indicates if need a retry
1331 @param SenseDataArray The pointer of an array of sense data
1332 @param NumberOfSenseKeys The number of sense key
1334 @retval EFI_DEVICE_ERROR Indicates that error occurs
1335 @retval EFI_SUCCESS Successfully to read capacity or sense data is received.
1339 ScsiDiskReadCapacity (
1340 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1341 OUT BOOLEAN
*NeedRetry
,
1342 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
1343 OUT UINTN
*NumberOfSenseKeys
1346 UINT8 HostAdapterStatus
;
1348 EFI_STATUS CommandStatus
;
1352 UINT8 SenseDataLength
;
1353 UINT32 DataLength10
;
1354 UINT32 DataLength16
;
1355 EFI_SCSI_DISK_CAPACITY_DATA
*CapacityData10
;
1356 EFI_SCSI_DISK_CAPACITY_DATA16
*CapacityData16
;
1358 CapacityData10
= AllocateAlignedBuffer (ScsiDiskDevice
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA
));
1359 if (CapacityData10
== NULL
) {
1361 return EFI_DEVICE_ERROR
;
1363 CapacityData16
= AllocateAlignedBuffer (ScsiDiskDevice
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16
));
1364 if (CapacityData16
== NULL
) {
1365 FreeAlignedBuffer (CapacityData10
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA
));
1367 return EFI_DEVICE_ERROR
;
1370 SenseDataLength
= 0;
1371 DataLength10
= sizeof (EFI_SCSI_DISK_CAPACITY_DATA
);
1372 DataLength16
= sizeof (EFI_SCSI_DISK_CAPACITY_DATA16
);
1373 ZeroMem (CapacityData10
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA
));
1374 ZeroMem (CapacityData16
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16
));
1376 *NumberOfSenseKeys
= 0;
1380 // submit Read Capacity(10) Command. If it returns capacity of FFFFFFFFh,
1381 // 16 byte command should be used to access large hard disk >2TB
1383 CommandStatus
= ScsiReadCapacityCommand (
1384 ScsiDiskDevice
->ScsiIo
,
1390 (VOID
*) CapacityData10
,
1395 ScsiDiskDevice
->Cdb16Byte
= FALSE
;
1396 if ((!EFI_ERROR (CommandStatus
)) && (CapacityData10
->LastLba3
== 0xff) && (CapacityData10
->LastLba2
== 0xff) &&
1397 (CapacityData10
->LastLba1
== 0xff) && (CapacityData10
->LastLba0
== 0xff)) {
1399 // use Read Capacity (16), Read (16) and Write (16) next when hard disk size > 2TB
1401 ScsiDiskDevice
->Cdb16Byte
= TRUE
;
1403 // submit Read Capacity(16) Command to get parameter LogicalBlocksPerPhysicalBlock
1404 // and LowestAlignedLba
1406 CommandStatus
= ScsiReadCapacity16Command (
1407 ScsiDiskDevice
->ScsiIo
,
1413 (VOID
*) CapacityData16
,
1420 // no need to check HostAdapterStatus and TargetStatus
1422 if (CommandStatus
== EFI_SUCCESS
) {
1423 GetMediaInfo (ScsiDiskDevice
, CapacityData10
, CapacityData16
);
1424 FreeAlignedBuffer (CapacityData10
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA
));
1425 FreeAlignedBuffer (CapacityData16
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16
));
1429 FreeAlignedBuffer (CapacityData10
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA
));
1430 FreeAlignedBuffer (CapacityData16
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16
));
1432 if (CommandStatus
== EFI_NOT_READY
) {
1434 return EFI_DEVICE_ERROR
;
1435 } else if ((CommandStatus
== EFI_INVALID_PARAMETER
) || (CommandStatus
== EFI_UNSUPPORTED
)) {
1437 return EFI_DEVICE_ERROR
;
1441 // go ahead to check HostAdapterStatus and TargetStatus
1442 // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
1445 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
1446 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
1448 return EFI_DEVICE_ERROR
;
1450 } else if (Status
== EFI_DEVICE_ERROR
) {
1452 // reset the scsi channel
1454 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
1456 return EFI_DEVICE_ERROR
;
1459 Status
= CheckTargetStatus (TargetStatus
);
1460 if (Status
== EFI_NOT_READY
) {
1462 // reset the scsi device
1464 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
1466 return EFI_DEVICE_ERROR
;
1468 } else if (Status
== EFI_DEVICE_ERROR
) {
1470 return EFI_DEVICE_ERROR
;
1474 // if goes here, meant ScsiReadCapacityCommand() failed.
1475 // if ScsiDiskRequestSenseKeys() succeeds at last,
1476 // better retry ScsiReadCapacityCommand(). (by setting *NeedRetry = TRUE)
1479 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1481 Status
= ScsiDiskRequestSenseKeys (
1488 if (!EFI_ERROR (Status
)) {
1493 return EFI_DEVICE_ERROR
;
1497 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
1498 // set *NeedRetry = FALSE to avoid the outside caller try again.
1501 return EFI_DEVICE_ERROR
;
1505 Check the HostAdapter status and re-interpret it in EFI_STATUS.
1507 @param HostAdapterStatus Host Adapter status
1509 @retval EFI_SUCCESS Host adapter is OK.
1510 @retval EFI_TIMEOUT Timeout.
1511 @retval EFI_NOT_READY Adapter NOT ready.
1512 @retval EFI_DEVICE_ERROR Adapter device error.
1516 CheckHostAdapterStatus (
1517 IN UINT8 HostAdapterStatus
1520 switch (HostAdapterStatus
) {
1521 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OK
:
1524 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_SELECTION_TIMEOUT
:
1525 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT
:
1526 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT_COMMAND
:
1529 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_MESSAGE_REJECT
:
1530 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PARITY_ERROR
:
1531 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_REQUEST_SENSE_FAILED
:
1532 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_DATA_OVERRUN_UNDERRUN
:
1533 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_RESET
:
1534 return EFI_NOT_READY
;
1536 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_FREE
:
1537 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR
:
1538 return EFI_DEVICE_ERROR
;
1547 Check the target status and re-interpret it in EFI_STATUS.
1549 @param TargetStatus Target status
1551 @retval EFI_NOT_READY Device is NOT ready.
1552 @retval EFI_DEVICE_ERROR
1558 IN UINT8 TargetStatus
1561 switch (TargetStatus
) {
1562 case EFI_EXT_SCSI_STATUS_TARGET_GOOD
:
1563 case EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION
:
1564 case EFI_EXT_SCSI_STATUS_TARGET_CONDITION_MET
:
1567 case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE
:
1568 case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE_CONDITION_MET
:
1569 case EFI_EXT_SCSI_STATUS_TARGET_BUSY
:
1570 case EFI_EXT_SCSI_STATUS_TARGET_TASK_SET_FULL
:
1571 return EFI_NOT_READY
;
1573 case EFI_EXT_SCSI_STATUS_TARGET_RESERVATION_CONFLICT
:
1574 return EFI_DEVICE_ERROR
;
1583 Retrieve all sense keys from the device.
1585 When encountering error during the process, if retrieve sense keys before
1586 error encountered, it returns the sense keys with return status set to EFI_SUCCESS,
1587 and NeedRetry set to FALSE; otherwize, return the proper return status.
1589 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1590 @param NeedRetry The pointer of flag indicates if need a retry
1591 @param SenseDataArray The pointer of an array of sense data
1592 @param NumberOfSenseKeys The number of sense key
1593 @param AskResetIfError The flag indicates if need reset when error occurs
1595 @retval EFI_DEVICE_ERROR Indicates that error occurs
1596 @retval EFI_SUCCESS Successfully to request sense key
1600 ScsiDiskRequestSenseKeys (
1601 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1602 OUT BOOLEAN
*NeedRetry
,
1603 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
1604 OUT UINTN
*NumberOfSenseKeys
,
1605 IN BOOLEAN AskResetIfError
1608 EFI_SCSI_SENSE_DATA
*PtrSenseData
;
1609 UINT8 SenseDataLength
;
1612 EFI_STATUS FallStatus
;
1613 UINT8 HostAdapterStatus
;
1616 FallStatus
= EFI_SUCCESS
;
1617 SenseDataLength
= (UINT8
) sizeof (EFI_SCSI_SENSE_DATA
);
1620 ScsiDiskDevice
->SenseData
,
1621 sizeof (EFI_SCSI_SENSE_DATA
) * (ScsiDiskDevice
->SenseDataNumber
)
1624 *NumberOfSenseKeys
= 0;
1625 *SenseDataArray
= ScsiDiskDevice
->SenseData
;
1626 Status
= EFI_SUCCESS
;
1627 PtrSenseData
= AllocateAlignedBuffer (ScsiDiskDevice
, sizeof (EFI_SCSI_SENSE_DATA
));
1628 if (PtrSenseData
== NULL
) {
1629 return EFI_DEVICE_ERROR
;
1632 for (SenseReq
= TRUE
; SenseReq
;) {
1633 ZeroMem (PtrSenseData
, sizeof (EFI_SCSI_SENSE_DATA
));
1634 Status
= ScsiRequestSenseCommand (
1635 ScsiDiskDevice
->ScsiIo
,
1642 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_WARN_BUFFER_TOO_SMALL
)) {
1643 FallStatus
= EFI_SUCCESS
;
1645 } else if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
1647 FallStatus
= EFI_DEVICE_ERROR
;
1649 } else if ((Status
== EFI_INVALID_PARAMETER
) || (Status
== EFI_UNSUPPORTED
)) {
1651 FallStatus
= EFI_DEVICE_ERROR
;
1653 } else if (Status
== EFI_DEVICE_ERROR
) {
1654 if (AskResetIfError
) {
1655 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
1658 FallStatus
= EFI_DEVICE_ERROR
;
1661 if (EFI_ERROR (FallStatus
)) {
1662 if (*NumberOfSenseKeys
!= 0) {
1664 Status
= EFI_SUCCESS
;
1667 Status
= EFI_DEVICE_ERROR
;
1672 CopyMem (ScsiDiskDevice
->SenseData
+ *NumberOfSenseKeys
, PtrSenseData
, SenseDataLength
);
1673 (*NumberOfSenseKeys
) += 1;
1676 // no more sense key or number of sense keys exceeds predefined,
1679 if ((PtrSenseData
->Sense_Key
== EFI_SCSI_SK_NO_SENSE
) ||
1680 (*NumberOfSenseKeys
== ScsiDiskDevice
->SenseDataNumber
)) {
1686 FreeAlignedBuffer (PtrSenseData
, sizeof (EFI_SCSI_SENSE_DATA
));
1692 Get information from media read capacity command.
1694 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1695 @param Capacity10 The pointer of EFI_SCSI_DISK_CAPACITY_DATA
1696 @param Capacity16 The pointer of EFI_SCSI_DISK_CAPACITY_DATA16
1701 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1702 IN EFI_SCSI_DISK_CAPACITY_DATA
*Capacity10
,
1703 IN EFI_SCSI_DISK_CAPACITY_DATA16
*Capacity16
1708 if (!ScsiDiskDevice
->Cdb16Byte
) {
1709 ScsiDiskDevice
->BlkIo
.Media
->LastBlock
= (Capacity10
->LastLba3
<< 24) |
1710 (Capacity10
->LastLba2
<< 16) |
1711 (Capacity10
->LastLba1
<< 8) |
1712 Capacity10
->LastLba0
;
1714 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= (Capacity10
->BlockSize3
<< 24) |
1715 (Capacity10
->BlockSize2
<< 16) |
1716 (Capacity10
->BlockSize1
<< 8) |
1717 Capacity10
->BlockSize0
;
1718 ScsiDiskDevice
->BlkIo
.Media
->LowestAlignedLba
= 0;
1719 ScsiDiskDevice
->BlkIo
.Media
->LogicalBlocksPerPhysicalBlock
= 0;
1721 Ptr
= (UINT8
*)&ScsiDiskDevice
->BlkIo
.Media
->LastBlock
;
1722 *Ptr
++ = Capacity16
->LastLba0
;
1723 *Ptr
++ = Capacity16
->LastLba1
;
1724 *Ptr
++ = Capacity16
->LastLba2
;
1725 *Ptr
++ = Capacity16
->LastLba3
;
1726 *Ptr
++ = Capacity16
->LastLba4
;
1727 *Ptr
++ = Capacity16
->LastLba5
;
1728 *Ptr
++ = Capacity16
->LastLba6
;
1729 *Ptr
= Capacity16
->LastLba7
;
1731 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= (Capacity16
->BlockSize3
<< 24) |
1732 (Capacity16
->BlockSize2
<< 16) |
1733 (Capacity16
->BlockSize1
<< 8) |
1734 Capacity16
->BlockSize0
;
1736 ScsiDiskDevice
->BlkIo
.Media
->LowestAlignedLba
= (Capacity16
->LowestAlignLogic2
<< 8) |
1737 Capacity16
->LowestAlignLogic1
;
1738 ScsiDiskDevice
->BlkIo
.Media
->LogicalBlocksPerPhysicalBlock
= (1 << Capacity16
->LogicPerPhysical
);
1741 ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
= TRUE
;
1743 if (ScsiDiskDevice
->DeviceType
== EFI_SCSI_TYPE_DISK
) {
1744 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x200;
1747 if (ScsiDiskDevice
->DeviceType
== EFI_SCSI_TYPE_CDROM
) {
1748 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x800;
1755 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1760 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
1763 ScsiDiskDevice
->FixedDevice
= (BOOLEAN
) ((ScsiDiskDevice
->InquiryData
.Rmb
== 1) ? 0 : 1);
1764 ScsiDiskDevice
->BlkIoMedia
.RemovableMedia
= (BOOLEAN
) (!ScsiDiskDevice
->FixedDevice
);
1768 Read sector from SCSI Disk.
1770 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1771 @param Buffer The buffer to fill in the read out data
1772 @param Lba Logic block address
1773 @param NumberOfBlocks The number of blocks to read
1775 @retval EFI_DEVICE_ERROR Indicates a device error.
1776 @retval EFI_SUCCESS Operation is successful.
1780 ScsiDiskReadSectors (
1781 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1784 IN UINTN NumberOfBlocks
1787 UINTN BlocksRemaining
;
1799 Status
= EFI_SUCCESS
;
1801 BlocksRemaining
= NumberOfBlocks
;
1802 BlockSize
= ScsiDiskDevice
->BlkIo
.Media
->BlockSize
;
1805 // limit the data bytes that can be transferred by one Read(10) or Read(16) Command
1807 if (!ScsiDiskDevice
->Cdb16Byte
) {
1810 MaxBlock
= 0xFFFFFFFF;
1815 while (BlocksRemaining
> 0) {
1817 if (BlocksRemaining
<= MaxBlock
) {
1818 if (!ScsiDiskDevice
->Cdb16Byte
) {
1819 SectorCount
= (UINT16
) BlocksRemaining
;
1821 SectorCount
= (UINT32
) BlocksRemaining
;
1824 SectorCount
= MaxBlock
;
1827 ByteCount
= SectorCount
* BlockSize
;
1829 // |------------------------|-----------------|------------------|-----------------|
1830 // | ATA Transfer Mode | Transfer Rate | SCSI Interface | Transfer Rate |
1831 // |------------------------|-----------------|------------------|-----------------|
1832 // | PIO Mode 0 | 3.3Mbytes/sec | SCSI-1 | 5Mbytes/sec |
1833 // |------------------------|-----------------|------------------|-----------------|
1834 // | PIO Mode 1 | 5.2Mbytes/sec | Fast SCSI | 10Mbytes/sec |
1835 // |------------------------|-----------------|------------------|-----------------|
1836 // | PIO Mode 2 | 8.3Mbytes/sec | Fast-Wide SCSI | 20Mbytes/sec |
1837 // |------------------------|-----------------|------------------|-----------------|
1838 // | PIO Mode 3 | 11.1Mbytes/sec | Ultra SCSI | 20Mbytes/sec |
1839 // |------------------------|-----------------|------------------|-----------------|
1840 // | PIO Mode 4 | 16.6Mbytes/sec | Ultra Wide SCSI | 40Mbytes/sec |
1841 // |------------------------|-----------------|------------------|-----------------|
1842 // | Single-word DMA Mode 0 | 2.1Mbytes/sec | Ultra2 SCSI | 40Mbytes/sec |
1843 // |------------------------|-----------------|------------------|-----------------|
1844 // | Single-word DMA Mode 1 | 4.2Mbytes/sec | Ultra2 Wide SCSI | 80Mbytes/sec |
1845 // |------------------------|-----------------|------------------|-----------------|
1846 // | Single-word DMA Mode 2 | 8.4Mbytes/sec | Ultra3 SCSI | 160Mbytes/sec |
1847 // |------------------------|-----------------|------------------|-----------------|
1848 // | Multi-word DMA Mode 0 | 4.2Mbytes/sec | Ultra-320 SCSI | 320Mbytes/sec |
1849 // |------------------------|-----------------|------------------|-----------------|
1850 // | Multi-word DMA Mode 1 | 13.3Mbytes/sec | Ultra-640 SCSI | 640Mbytes/sec |
1851 // |------------------------|-----------------|------------------|-----------------|
1853 // As ScsiDisk and ScsiBus driver are used to manage SCSI or ATAPI devices, we have to use
1854 // the lowest transfer rate to calculate the possible maximum timeout value for each operation.
1855 // From the above table, we could know 2.1Mbytes per second is lowest one.
1856 // The timout value is rounded up to nearest integar and here an additional 30s is added
1857 // to follow ATA spec in which it mentioned that the device may take up to 30s to respond
1858 // commands in the Standby/Idle mode.
1860 Timeout
= EFI_TIMER_PERIOD_SECONDS (ByteCount
/ 2100000 + 31);
1863 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1864 if (!ScsiDiskDevice
->Cdb16Byte
) {
1865 Status
= ScsiDiskRead10 (
1875 Status
= ScsiDiskRead16 (
1885 if (!EFI_ERROR (Status
)) {
1890 return EFI_DEVICE_ERROR
;
1895 if ((Index
== MaxRetry
) && (Status
!= EFI_SUCCESS
)) {
1896 return EFI_DEVICE_ERROR
;
1900 // actual transferred sectors
1902 SectorCount
= ByteCount
/ BlockSize
;
1905 PtrBuffer
= PtrBuffer
+ SectorCount
* BlockSize
;
1906 BlocksRemaining
-= SectorCount
;
1913 Write sector to SCSI Disk.
1915 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1916 @param Buffer The buffer of data to be written into SCSI Disk
1917 @param Lba Logic block address
1918 @param NumberOfBlocks The number of blocks to read
1920 @retval EFI_DEVICE_ERROR Indicates a device error.
1921 @retval EFI_SUCCESS Operation is successful.
1925 ScsiDiskWriteSectors (
1926 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1929 IN UINTN NumberOfBlocks
1932 UINTN BlocksRemaining
;
1944 Status
= EFI_SUCCESS
;
1946 BlocksRemaining
= NumberOfBlocks
;
1947 BlockSize
= ScsiDiskDevice
->BlkIo
.Media
->BlockSize
;
1950 // limit the data bytes that can be transferred by one Read(10) or Read(16) Command
1952 if (!ScsiDiskDevice
->Cdb16Byte
) {
1955 MaxBlock
= 0xFFFFFFFF;
1960 while (BlocksRemaining
> 0) {
1962 if (BlocksRemaining
<= MaxBlock
) {
1963 if (!ScsiDiskDevice
->Cdb16Byte
) {
1964 SectorCount
= (UINT16
) BlocksRemaining
;
1966 SectorCount
= (UINT32
) BlocksRemaining
;
1969 SectorCount
= MaxBlock
;
1972 ByteCount
= SectorCount
* BlockSize
;
1974 // |------------------------|-----------------|------------------|-----------------|
1975 // | ATA Transfer Mode | Transfer Rate | SCSI Interface | Transfer Rate |
1976 // |------------------------|-----------------|------------------|-----------------|
1977 // | PIO Mode 0 | 3.3Mbytes/sec | SCSI-1 | 5Mbytes/sec |
1978 // |------------------------|-----------------|------------------|-----------------|
1979 // | PIO Mode 1 | 5.2Mbytes/sec | Fast SCSI | 10Mbytes/sec |
1980 // |------------------------|-----------------|------------------|-----------------|
1981 // | PIO Mode 2 | 8.3Mbytes/sec | Fast-Wide SCSI | 20Mbytes/sec |
1982 // |------------------------|-----------------|------------------|-----------------|
1983 // | PIO Mode 3 | 11.1Mbytes/sec | Ultra SCSI | 20Mbytes/sec |
1984 // |------------------------|-----------------|------------------|-----------------|
1985 // | PIO Mode 4 | 16.6Mbytes/sec | Ultra Wide SCSI | 40Mbytes/sec |
1986 // |------------------------|-----------------|------------------|-----------------|
1987 // | Single-word DMA Mode 0 | 2.1Mbytes/sec | Ultra2 SCSI | 40Mbytes/sec |
1988 // |------------------------|-----------------|------------------|-----------------|
1989 // | Single-word DMA Mode 1 | 4.2Mbytes/sec | Ultra2 Wide SCSI | 80Mbytes/sec |
1990 // |------------------------|-----------------|------------------|-----------------|
1991 // | Single-word DMA Mode 2 | 8.4Mbytes/sec | Ultra3 SCSI | 160Mbytes/sec |
1992 // |------------------------|-----------------|------------------|-----------------|
1993 // | Multi-word DMA Mode 0 | 4.2Mbytes/sec | Ultra-320 SCSI | 320Mbytes/sec |
1994 // |------------------------|-----------------|------------------|-----------------|
1995 // | Multi-word DMA Mode 1 | 13.3Mbytes/sec | Ultra-640 SCSI | 640Mbytes/sec |
1996 // |------------------------|-----------------|------------------|-----------------|
1998 // As ScsiDisk and ScsiBus driver are used to manage SCSI or ATAPI devices, we have to use
1999 // the lowest transfer rate to calculate the possible maximum timeout value for each operation.
2000 // From the above table, we could know 2.1Mbytes per second is lowest one.
2001 // The timout value is rounded up to nearest integar and here an additional 30s is added
2002 // to follow ATA spec in which it mentioned that the device may take up to 30s to respond
2003 // commands in the Standby/Idle mode.
2005 Timeout
= EFI_TIMER_PERIOD_SECONDS (ByteCount
/ 2100000 + 31);
2007 for (Index
= 0; Index
< MaxRetry
; Index
++) {
2008 if (!ScsiDiskDevice
->Cdb16Byte
) {
2009 Status
= ScsiDiskWrite10 (
2019 Status
= ScsiDiskWrite16 (
2029 if (!EFI_ERROR (Status
)) {
2034 return EFI_DEVICE_ERROR
;
2038 if ((Index
== MaxRetry
) && (Status
!= EFI_SUCCESS
)) {
2039 return EFI_DEVICE_ERROR
;
2042 // actual transferred sectors
2044 SectorCount
= ByteCount
/ BlockSize
;
2047 PtrBuffer
= PtrBuffer
+ SectorCount
* BlockSize
;
2048 BlocksRemaining
-= SectorCount
;
2056 Submit Read(10) command.
2058 @param ScsiDiskDevice The pointer of ScsiDiskDevice
2059 @param NeedRetry The pointer of flag indicates if needs retry if error happens
2060 @param Timeout The time to complete the command
2061 @param DataBuffer The buffer to fill with the read out data
2062 @param DataLength The length of buffer
2063 @param StartLba The start logic block address
2064 @param SectorCount The number of blocks to read
2066 @return EFI_STATUS is returned by calling ScsiRead10Command().
2070 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
2071 OUT BOOLEAN
*NeedRetry
,
2073 OUT UINT8
*DataBuffer
,
2074 IN OUT UINT32
*DataLength
,
2076 IN UINT32 SectorCount
2079 UINT8 SenseDataLength
;
2081 EFI_STATUS ReturnStatus
;
2082 UINT8 HostAdapterStatus
;
2087 // Implement a backoff algorithem to resolve some compatibility issues that
2088 // some SCSI targets or ATAPI devices couldn't correctly response reading/writing
2089 // big data in a single operation.
2090 // This algorithem will at first try to execute original request. If the request fails
2091 // with media error sense data or else, it will reduce the transfer length to half and
2092 // try again till the operation succeeds or fails with one sector transfer length.
2096 Action
= ACTION_NO_ACTION
;
2097 SenseDataLength
= (UINT8
) (ScsiDiskDevice
->SenseDataNumber
* sizeof (EFI_SCSI_SENSE_DATA
));
2098 ReturnStatus
= ScsiRead10Command (
2099 ScsiDiskDevice
->ScsiIo
,
2101 ScsiDiskDevice
->SenseData
,
2111 if (ReturnStatus
== EFI_NOT_READY
) {
2113 return EFI_DEVICE_ERROR
;
2114 } else if ((ReturnStatus
== EFI_INVALID_PARAMETER
) || (ReturnStatus
== EFI_UNSUPPORTED
)) {
2116 return ReturnStatus
;
2120 // go ahead to check HostAdapterStatus and TargetStatus
2121 // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
2123 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
2124 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
2126 return EFI_DEVICE_ERROR
;
2127 } else if (Status
== EFI_DEVICE_ERROR
) {
2129 // reset the scsi channel
2131 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
2133 return EFI_DEVICE_ERROR
;
2136 Status
= CheckTargetStatus (TargetStatus
);
2137 if (Status
== EFI_NOT_READY
) {
2139 // reset the scsi device
2141 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
2143 return EFI_DEVICE_ERROR
;
2144 } else if (Status
== EFI_DEVICE_ERROR
) {
2146 return EFI_DEVICE_ERROR
;
2149 if ((TargetStatus
== EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION
) || (EFI_ERROR (ReturnStatus
))) {
2150 DEBUG ((EFI_D_ERROR
, "ScsiDiskRead10: Check Condition happened!\n"));
2151 Status
= DetectMediaParsingSenseKeys (ScsiDiskDevice
, ScsiDiskDevice
->SenseData
, SenseDataLength
/ sizeof (EFI_SCSI_SENSE_DATA
), &Action
);
2152 if (Action
== ACTION_RETRY_COMMAND_LATER
) {
2154 return EFI_DEVICE_ERROR
;
2155 } else if (Action
== ACTION_RETRY_WITH_BACKOFF_ALGO
) {
2156 if (SectorCount
<= 1) {
2158 // Jump out if the operation still fails with one sector transfer length.
2161 return EFI_DEVICE_ERROR
;
2164 // Try again with half length if the sense data shows we need to retry.
2167 *DataLength
= SectorCount
* ScsiDiskDevice
->BlkIo
.Media
->BlockSize
;
2171 return EFI_DEVICE_ERROR
;
2175 return ReturnStatus
;
2180 Submit Write(10) Command.
2182 @param ScsiDiskDevice The pointer of ScsiDiskDevice
2183 @param NeedRetry The pointer of flag indicates if needs retry if error happens
2184 @param Timeout The time to complete the command
2185 @param DataBuffer The buffer to fill with the read out data
2186 @param DataLength The length of buffer
2187 @param StartLba The start logic block address
2188 @param SectorCount The number of blocks to write
2190 @return EFI_STATUS is returned by calling ScsiWrite10Command().
2195 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
2196 OUT BOOLEAN
*NeedRetry
,
2198 IN UINT8
*DataBuffer
,
2199 IN OUT UINT32
*DataLength
,
2201 IN UINT32 SectorCount
2205 EFI_STATUS ReturnStatus
;
2206 UINT8 SenseDataLength
;
2207 UINT8 HostAdapterStatus
;
2212 // Implement a backoff algorithem to resolve some compatibility issues that
2213 // some SCSI targets or ATAPI devices couldn't correctly response reading/writing
2214 // big data in a single operation.
2215 // This algorithem will at first try to execute original request. If the request fails
2216 // with media error sense data or else, it will reduce the transfer length to half and
2217 // try again till the operation succeeds or fails with one sector transfer length.
2221 Action
= ACTION_NO_ACTION
;
2222 SenseDataLength
= (UINT8
) (ScsiDiskDevice
->SenseDataNumber
* sizeof (EFI_SCSI_SENSE_DATA
));
2223 ReturnStatus
= ScsiWrite10Command (
2224 ScsiDiskDevice
->ScsiIo
,
2226 ScsiDiskDevice
->SenseData
,
2235 if (ReturnStatus
== EFI_NOT_READY
) {
2237 return EFI_DEVICE_ERROR
;
2238 } else if ((ReturnStatus
== EFI_INVALID_PARAMETER
) || (ReturnStatus
== EFI_UNSUPPORTED
)) {
2240 return ReturnStatus
;
2244 // go ahead to check HostAdapterStatus and TargetStatus
2245 // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
2247 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
2248 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
2250 return EFI_DEVICE_ERROR
;
2251 } else if (Status
== EFI_DEVICE_ERROR
) {
2253 // reset the scsi channel
2255 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
2257 return EFI_DEVICE_ERROR
;
2260 Status
= CheckTargetStatus (TargetStatus
);
2261 if (Status
== EFI_NOT_READY
) {
2263 // reset the scsi device
2265 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
2267 return EFI_DEVICE_ERROR
;
2268 } else if (Status
== EFI_DEVICE_ERROR
) {
2270 return EFI_DEVICE_ERROR
;
2273 if ((TargetStatus
== EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION
) || (EFI_ERROR (ReturnStatus
))) {
2274 DEBUG ((EFI_D_ERROR
, "ScsiDiskWrite10: Check Condition happened!\n"));
2275 Status
= DetectMediaParsingSenseKeys (ScsiDiskDevice
, ScsiDiskDevice
->SenseData
, SenseDataLength
/ sizeof (EFI_SCSI_SENSE_DATA
), &Action
);
2276 if (Action
== ACTION_RETRY_COMMAND_LATER
) {
2278 return EFI_DEVICE_ERROR
;
2279 } else if (Action
== ACTION_RETRY_WITH_BACKOFF_ALGO
) {
2280 if (SectorCount
<= 1) {
2282 // Jump out if the operation still fails with one sector transfer length.
2285 return EFI_DEVICE_ERROR
;
2288 // Try again with half length if the sense data shows we need to retry.
2291 *DataLength
= SectorCount
* ScsiDiskDevice
->BlkIo
.Media
->BlockSize
;
2295 return EFI_DEVICE_ERROR
;
2299 return ReturnStatus
;
2304 Submit Read(16) command.
2306 @param ScsiDiskDevice The pointer of ScsiDiskDevice
2307 @param NeedRetry The pointer of flag indicates if needs retry if error happens
2308 @param Timeout The time to complete the command
2309 @param DataBuffer The buffer to fill with the read out data
2310 @param DataLength The length of buffer
2311 @param StartLba The start logic block address
2312 @param SectorCount The number of blocks to read
2314 @return EFI_STATUS is returned by calling ScsiRead16Command().
2318 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
2319 OUT BOOLEAN
*NeedRetry
,
2321 OUT UINT8
*DataBuffer
,
2322 IN OUT UINT32
*DataLength
,
2324 IN UINT32 SectorCount
2327 UINT8 SenseDataLength
;
2329 EFI_STATUS ReturnStatus
;
2330 UINT8 HostAdapterStatus
;
2335 // Implement a backoff algorithem to resolve some compatibility issues that
2336 // some SCSI targets or ATAPI devices couldn't correctly response reading/writing
2337 // big data in a single operation.
2338 // This algorithem will at first try to execute original request. If the request fails
2339 // with media error sense data or else, it will reduce the transfer length to half and
2340 // try again till the operation succeeds or fails with one sector transfer length.
2344 Action
= ACTION_NO_ACTION
;
2345 SenseDataLength
= (UINT8
) (ScsiDiskDevice
->SenseDataNumber
* sizeof (EFI_SCSI_SENSE_DATA
));
2346 ReturnStatus
= ScsiRead16Command (
2347 ScsiDiskDevice
->ScsiIo
,
2349 ScsiDiskDevice
->SenseData
,
2358 if (ReturnStatus
== EFI_NOT_READY
) {
2360 return EFI_DEVICE_ERROR
;
2361 } else if ((ReturnStatus
== EFI_INVALID_PARAMETER
) || (ReturnStatus
== EFI_UNSUPPORTED
)) {
2363 return ReturnStatus
;
2367 // go ahead to check HostAdapterStatus and TargetStatus
2368 // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
2370 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
2371 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
2373 return EFI_DEVICE_ERROR
;
2374 } else if (Status
== EFI_DEVICE_ERROR
) {
2376 // reset the scsi channel
2378 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
2380 return EFI_DEVICE_ERROR
;
2383 Status
= CheckTargetStatus (TargetStatus
);
2384 if (Status
== EFI_NOT_READY
) {
2386 // reset the scsi device
2388 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
2390 return EFI_DEVICE_ERROR
;
2391 } else if (Status
== EFI_DEVICE_ERROR
) {
2393 return EFI_DEVICE_ERROR
;
2396 if ((TargetStatus
== EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION
) || (EFI_ERROR (ReturnStatus
))) {
2397 DEBUG ((EFI_D_ERROR
, "ScsiDiskRead16: Check Condition happened!\n"));
2398 Status
= DetectMediaParsingSenseKeys (ScsiDiskDevice
, ScsiDiskDevice
->SenseData
, SenseDataLength
/ sizeof (EFI_SCSI_SENSE_DATA
), &Action
);
2399 if (Action
== ACTION_RETRY_COMMAND_LATER
) {
2401 return EFI_DEVICE_ERROR
;
2402 } else if (Action
== ACTION_RETRY_WITH_BACKOFF_ALGO
) {
2403 if (SectorCount
<= 1) {
2405 // Jump out if the operation still fails with one sector transfer length.
2408 return EFI_DEVICE_ERROR
;
2411 // Try again with half length if the sense data shows we need to retry.
2414 *DataLength
= SectorCount
* ScsiDiskDevice
->BlkIo
.Media
->BlockSize
;
2418 return EFI_DEVICE_ERROR
;
2422 return ReturnStatus
;
2427 Submit Write(16) Command.
2429 @param ScsiDiskDevice The pointer of ScsiDiskDevice
2430 @param NeedRetry The pointer of flag indicates if needs retry if error happens
2431 @param Timeout The time to complete the command
2432 @param DataBuffer The buffer to fill with the read out data
2433 @param DataLength The length of buffer
2434 @param StartLba The start logic block address
2435 @param SectorCount The number of blocks to write
2437 @return EFI_STATUS is returned by calling ScsiWrite16Command().
2442 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
2443 OUT BOOLEAN
*NeedRetry
,
2445 IN UINT8
*DataBuffer
,
2446 IN OUT UINT32
*DataLength
,
2448 IN UINT32 SectorCount
2452 EFI_STATUS ReturnStatus
;
2453 UINT8 SenseDataLength
;
2454 UINT8 HostAdapterStatus
;
2459 // Implement a backoff algorithem to resolve some compatibility issues that
2460 // some SCSI targets or ATAPI devices couldn't correctly response reading/writing
2461 // big data in a single operation.
2462 // This algorithem will at first try to execute original request. If the request fails
2463 // with media error sense data or else, it will reduce the transfer length to half and
2464 // try again till the operation succeeds or fails with one sector transfer length.
2468 Action
= ACTION_NO_ACTION
;
2469 SenseDataLength
= (UINT8
) (ScsiDiskDevice
->SenseDataNumber
* sizeof (EFI_SCSI_SENSE_DATA
));
2470 ReturnStatus
= ScsiWrite16Command (
2471 ScsiDiskDevice
->ScsiIo
,
2473 ScsiDiskDevice
->SenseData
,
2482 if (ReturnStatus
== EFI_NOT_READY
) {
2484 return EFI_DEVICE_ERROR
;
2485 } else if ((ReturnStatus
== EFI_INVALID_PARAMETER
) || (ReturnStatus
== EFI_UNSUPPORTED
)) {
2487 return ReturnStatus
;
2491 // go ahead to check HostAdapterStatus and TargetStatus
2492 // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
2494 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
2495 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
2497 return EFI_DEVICE_ERROR
;
2498 } else if (Status
== EFI_DEVICE_ERROR
) {
2500 // reset the scsi channel
2502 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
2504 return EFI_DEVICE_ERROR
;
2507 Status
= CheckTargetStatus (TargetStatus
);
2508 if (Status
== EFI_NOT_READY
) {
2510 // reset the scsi device
2512 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
2514 return EFI_DEVICE_ERROR
;
2515 } else if (Status
== EFI_DEVICE_ERROR
) {
2517 return EFI_DEVICE_ERROR
;
2520 if ((TargetStatus
== EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION
) || (EFI_ERROR (ReturnStatus
))) {
2521 DEBUG ((EFI_D_ERROR
, "ScsiDiskWrite16: Check Condition happened!\n"));
2522 Status
= DetectMediaParsingSenseKeys (ScsiDiskDevice
, ScsiDiskDevice
->SenseData
, SenseDataLength
/ sizeof (EFI_SCSI_SENSE_DATA
), &Action
);
2523 if (Action
== ACTION_RETRY_COMMAND_LATER
) {
2525 return EFI_DEVICE_ERROR
;
2526 } else if (Action
== ACTION_RETRY_WITH_BACKOFF_ALGO
) {
2527 if (SectorCount
<= 1) {
2529 // Jump out if the operation still fails with one sector transfer length.
2532 return EFI_DEVICE_ERROR
;
2535 // Try again with half length if the sense data shows we need to retry.
2538 *DataLength
= SectorCount
* ScsiDiskDevice
->BlkIo
.Media
->BlockSize
;
2542 return EFI_DEVICE_ERROR
;
2546 return ReturnStatus
;
2551 Check sense key to find if media presents.
2553 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2554 @param SenseCounts The number of sense key
2556 @retval TRUE NOT any media
2557 @retval FALSE Media presents
2561 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2562 IN UINTN SenseCounts
2565 EFI_SCSI_SENSE_DATA
*SensePtr
;
2570 SensePtr
= SenseData
;
2572 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2574 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2),
2575 // Additional Sense Code is ASC_NO_MEDIA (0x3A)
2577 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_NOT_READY
) &&
2578 (SensePtr
->Addnl_Sense_Code
== EFI_SCSI_ASC_NO_MEDIA
)) {
2591 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2592 @param SenseCounts The number of sense key
2595 @retval FALSE NOT error
2599 ScsiDiskIsMediaError (
2600 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2601 IN UINTN SenseCounts
2604 EFI_SCSI_SENSE_DATA
*SensePtr
;
2609 SensePtr
= SenseData
;
2611 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2613 switch (SensePtr
->Sense_Key
) {
2615 case EFI_SCSI_SK_MEDIUM_ERROR
:
2617 // Sense Key is EFI_SCSI_SK_MEDIUM_ERROR (0x3)
2619 switch (SensePtr
->Addnl_Sense_Code
) {
2624 case EFI_SCSI_ASC_MEDIA_ERR1
:
2629 case EFI_SCSI_ASC_MEDIA_ERR2
:
2634 case EFI_SCSI_ASC_MEDIA_ERR3
:
2635 case EFI_SCSI_ASC_MEDIA_ERR4
:
2645 case EFI_SCSI_SK_NOT_READY
:
2647 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2)
2649 switch (SensePtr
->Addnl_Sense_Code
) {
2651 // Additional Sense Code is ASC_MEDIA_UPSIDE_DOWN (0x6)
2653 case EFI_SCSI_ASC_MEDIA_UPSIDE_DOWN
:
2674 Check sense key to find if hardware error happens.
2676 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2677 @param SenseCounts The number of sense key
2679 @retval TRUE Hardware error exits.
2680 @retval FALSE NO error.
2684 ScsiDiskIsHardwareError (
2685 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2686 IN UINTN SenseCounts
2689 EFI_SCSI_SENSE_DATA
*SensePtr
;
2694 SensePtr
= SenseData
;
2696 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2699 // Sense Key is EFI_SCSI_SK_HARDWARE_ERROR (0x4)
2701 if (SensePtr
->Sense_Key
== EFI_SCSI_SK_HARDWARE_ERROR
) {
2713 Check sense key to find if media has changed.
2715 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2716 @param SenseCounts The number of sense key
2718 @retval TRUE Media is changed.
2719 @retval FALSE Media is NOT changed.
2722 ScsiDiskIsMediaChange (
2723 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2724 IN UINTN SenseCounts
2727 EFI_SCSI_SENSE_DATA
*SensePtr
;
2729 BOOLEAN IsMediaChanged
;
2731 IsMediaChanged
= FALSE
;
2732 SensePtr
= SenseData
;
2734 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2736 // Sense Key is EFI_SCSI_SK_UNIT_ATTENTION (0x6),
2737 // Additional sense code is EFI_SCSI_ASC_MEDIA_CHANGE (0x28)
2739 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_UNIT_ATTENTION
) &&
2740 (SensePtr
->Addnl_Sense_Code
== EFI_SCSI_ASC_MEDIA_CHANGE
)) {
2741 IsMediaChanged
= TRUE
;
2747 return IsMediaChanged
;
2751 Check sense key to find if reset happens.
2753 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2754 @param SenseCounts The number of sense key
2756 @retval TRUE It is reset before.
2757 @retval FALSE It is NOT reset before.
2761 ScsiDiskIsResetBefore (
2762 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2763 IN UINTN SenseCounts
2766 EFI_SCSI_SENSE_DATA
*SensePtr
;
2768 BOOLEAN IsResetBefore
;
2770 IsResetBefore
= FALSE
;
2771 SensePtr
= SenseData
;
2773 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2776 // Sense Key is EFI_SCSI_SK_UNIT_ATTENTION (0x6)
2777 // Additional Sense Code is EFI_SCSI_ASC_RESET (0x29)
2779 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_UNIT_ATTENTION
) &&
2780 (SensePtr
->Addnl_Sense_Code
== EFI_SCSI_ASC_RESET
)) {
2781 IsResetBefore
= TRUE
;
2787 return IsResetBefore
;
2791 Check sense key to find if the drive is ready.
2793 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2794 @param SenseCounts The number of sense key
2795 @param RetryLater The flag means if need a retry
2797 @retval TRUE Drive is ready.
2798 @retval FALSE Drive is NOT ready.
2802 ScsiDiskIsDriveReady (
2803 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2804 IN UINTN SenseCounts
,
2805 OUT BOOLEAN
*RetryLater
2808 EFI_SCSI_SENSE_DATA
*SensePtr
;
2813 *RetryLater
= FALSE
;
2814 SensePtr
= SenseData
;
2816 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2818 switch (SensePtr
->Sense_Key
) {
2820 case EFI_SCSI_SK_NOT_READY
:
2822 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2)
2824 switch (SensePtr
->Addnl_Sense_Code
) {
2825 case EFI_SCSI_ASC_NOT_READY
:
2827 // Additional Sense Code is EFI_SCSI_ASC_NOT_READY (0x4)
2829 switch (SensePtr
->Addnl_Sense_Code_Qualifier
) {
2830 case EFI_SCSI_ASCQ_IN_PROGRESS
:
2832 // Additional Sense Code Qualifier is
2833 // EFI_SCSI_ASCQ_IN_PROGRESS (0x1)
2841 *RetryLater
= FALSE
;
2862 Check sense key to find if it has sense key.
2864 @param SenseData - The pointer of EFI_SCSI_SENSE_DATA
2865 @param SenseCounts - The number of sense key
2867 @retval TRUE It has sense key.
2868 @retval FALSE It has NOT any sense key.
2872 ScsiDiskHaveSenseKey (
2873 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2874 IN UINTN SenseCounts
2877 EFI_SCSI_SENSE_DATA
*SensePtr
;
2879 BOOLEAN HaveSenseKey
;
2881 if (SenseCounts
== 0) {
2882 HaveSenseKey
= FALSE
;
2884 HaveSenseKey
= TRUE
;
2887 SensePtr
= SenseData
;
2889 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2892 // Sense Key is SK_NO_SENSE (0x0)
2894 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_NO_SENSE
) &&
2896 HaveSenseKey
= FALSE
;
2902 return HaveSenseKey
;
2906 Release resource about disk device.
2908 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
2912 ReleaseScsiDiskDeviceResources (
2913 IN SCSI_DISK_DEV
*ScsiDiskDevice
2916 if (ScsiDiskDevice
== NULL
) {
2920 if (ScsiDiskDevice
->SenseData
!= NULL
) {
2921 FreePool (ScsiDiskDevice
->SenseData
);
2922 ScsiDiskDevice
->SenseData
= NULL
;
2925 if (ScsiDiskDevice
->ControllerNameTable
!= NULL
) {
2926 FreeUnicodeStringTable (ScsiDiskDevice
->ControllerNameTable
);
2927 ScsiDiskDevice
->ControllerNameTable
= NULL
;
2930 FreePool (ScsiDiskDevice
);
2932 ScsiDiskDevice
= NULL
;
2936 Determine if Block Io should be produced.
2939 @param ChildHandle Child Handle to retrieve Parent information.
2941 @retval TRUE Should produce Block Io.
2942 @retval FALSE Should not produce Block Io.
2946 DetermineInstallBlockIo (
2947 IN EFI_HANDLE ChildHandle
2950 EFI_SCSI_PASS_THRU_PROTOCOL
*ScsiPassThru
;
2951 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtScsiPassThru
;
2954 // Firstly, check if ExtScsiPassThru Protocol parent handle exists. If existence,
2955 // check its attribute, logic or physical.
2957 ExtScsiPassThru
= (EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*)GetParentProtocol (&gEfiExtScsiPassThruProtocolGuid
, ChildHandle
);
2958 if (ExtScsiPassThru
!= NULL
) {
2959 if ((ExtScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
) != 0) {
2965 // Secondly, check if ScsiPassThru Protocol parent handle exists. If existence,
2966 // check its attribute, logic or physical.
2968 ScsiPassThru
= (EFI_SCSI_PASS_THRU_PROTOCOL
*)GetParentProtocol (&gEfiScsiPassThruProtocolGuid
, ChildHandle
);
2969 if (ScsiPassThru
!= NULL
) {
2970 if ((ScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
) != 0) {
2979 Search protocol database and check to see if the protocol
2980 specified by ProtocolGuid is present on a ControllerHandle and opened by
2981 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
2982 If the ControllerHandle is found, then the protocol specified by ProtocolGuid
2983 will be opened on it.
2986 @param ProtocolGuid ProtocolGuid pointer.
2987 @param ChildHandle Child Handle to retrieve Parent information.
2993 IN EFI_GUID
*ProtocolGuid
,
2994 IN EFI_HANDLE ChildHandle
3001 EFI_HANDLE
*HandleBuffer
;
3004 // Retrieve the list of all handles from the handle database
3006 Status
= gBS
->LocateHandleBuffer (
3014 if (EFI_ERROR (Status
)) {
3019 // Iterate to find who is parent handle that is opened with ProtocolGuid by ChildHandle
3021 for (Index
= 0; Index
< HandleCount
; Index
++) {
3022 Status
= EfiTestChildHandle (HandleBuffer
[Index
], ChildHandle
, ProtocolGuid
);
3023 if (!EFI_ERROR (Status
)) {
3024 Status
= gBS
->HandleProtocol (HandleBuffer
[Index
], ProtocolGuid
, (VOID
**)&Interface
);
3025 if (!EFI_ERROR (Status
)) {
3026 gBS
->FreePool (HandleBuffer
);
3032 gBS
->FreePool (HandleBuffer
);
3037 Provides inquiry information for the controller type.
3039 This function is used by the IDE bus driver to get inquiry data. Data format
3040 of Identify data is defined by the Interface GUID.
3042 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
3043 @param[in, out] InquiryData Pointer to a buffer for the inquiry data.
3044 @param[in, out] InquiryDataSize Pointer to the value for the inquiry data size.
3046 @retval EFI_SUCCESS The command was accepted without any errors.
3047 @retval EFI_NOT_FOUND Device does not support this data class
3048 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
3049 @retval EFI_BUFFER_TOO_SMALL InquiryDataSize not big enough
3054 ScsiDiskInfoInquiry (
3055 IN EFI_DISK_INFO_PROTOCOL
*This
,
3056 IN OUT VOID
*InquiryData
,
3057 IN OUT UINT32
*InquiryDataSize
3061 SCSI_DISK_DEV
*ScsiDiskDevice
;
3063 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_DISKINFO (This
);
3065 Status
= EFI_BUFFER_TOO_SMALL
;
3066 if (*InquiryDataSize
>= sizeof (ScsiDiskDevice
->InquiryData
)) {
3067 Status
= EFI_SUCCESS
;
3068 CopyMem (InquiryData
, &ScsiDiskDevice
->InquiryData
, sizeof (ScsiDiskDevice
->InquiryData
));
3070 *InquiryDataSize
= sizeof (ScsiDiskDevice
->InquiryData
);
3076 Provides identify information for the controller type.
3078 This function is used by the IDE bus driver to get identify data. Data format
3079 of Identify data is defined by the Interface GUID.
3081 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL
3083 @param[in, out] IdentifyData Pointer to a buffer for the identify data.
3084 @param[in, out] IdentifyDataSize Pointer to the value for the identify data
3087 @retval EFI_SUCCESS The command was accepted without any errors.
3088 @retval EFI_NOT_FOUND Device does not support this data class
3089 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
3090 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
3095 ScsiDiskInfoIdentify (
3096 IN EFI_DISK_INFO_PROTOCOL
*This
,
3097 IN OUT VOID
*IdentifyData
,
3098 IN OUT UINT32
*IdentifyDataSize
3102 SCSI_DISK_DEV
*ScsiDiskDevice
;
3104 if (CompareGuid (&This
->Interface
, &gEfiDiskInfoScsiInterfaceGuid
)) {
3106 // Physical SCSI bus does not support this data class.
3108 return EFI_NOT_FOUND
;
3111 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_DISKINFO (This
);
3113 Status
= EFI_BUFFER_TOO_SMALL
;
3114 if (*IdentifyDataSize
>= sizeof (ScsiDiskDevice
->IdentifyData
)) {
3115 Status
= EFI_SUCCESS
;
3116 CopyMem (IdentifyData
, &ScsiDiskDevice
->IdentifyData
, sizeof (ScsiDiskDevice
->IdentifyData
));
3118 *IdentifyDataSize
= sizeof (ScsiDiskDevice
->IdentifyData
);
3123 Provides sense data information for the controller type.
3125 This function is used by the IDE bus driver to get sense data.
3126 Data format of Sense data is defined by the Interface GUID.
3128 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
3129 @param[in, out] SenseData Pointer to the SenseData.
3130 @param[in, out] SenseDataSize Size of SenseData in bytes.
3131 @param[out] SenseDataNumber Pointer to the value for the sense data size.
3133 @retval EFI_SUCCESS The command was accepted without any errors.
3134 @retval EFI_NOT_FOUND Device does not support this data class.
3135 @retval EFI_DEVICE_ERROR Error reading SenseData from device.
3136 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough.
3141 ScsiDiskInfoSenseData (
3142 IN EFI_DISK_INFO_PROTOCOL
*This
,
3143 IN OUT VOID
*SenseData
,
3144 IN OUT UINT32
*SenseDataSize
,
3145 OUT UINT8
*SenseDataNumber
3148 return EFI_NOT_FOUND
;
3153 This function is used by the IDE bus driver to get controller information.
3155 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
3156 @param[out] IdeChannel Pointer to the Ide Channel number. Primary or secondary.
3157 @param[out] IdeDevice Pointer to the Ide Device number. Master or slave.
3159 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid.
3160 @retval EFI_UNSUPPORTED This is not an IDE device.
3165 ScsiDiskInfoWhichIde (
3166 IN EFI_DISK_INFO_PROTOCOL
*This
,
3167 OUT UINT32
*IdeChannel
,
3168 OUT UINT32
*IdeDevice
3171 SCSI_DISK_DEV
*ScsiDiskDevice
;
3173 if (CompareGuid (&This
->Interface
, &gEfiDiskInfoScsiInterfaceGuid
)) {
3175 // This is not an IDE physical device.
3177 return EFI_UNSUPPORTED
;
3180 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_DISKINFO (This
);
3181 *IdeChannel
= ScsiDiskDevice
->Channel
;
3182 *IdeDevice
= ScsiDiskDevice
->Device
;
3189 Issues ATA IDENTIFY DEVICE command to identify ATAPI device.
3191 This function tries to fill 512-byte ATAPI_IDENTIFY_DATA for ATAPI device to
3192 implement Identify() interface for DiskInfo protocol. The ATA command is sent
3193 via SCSI Request Packet.
3195 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
3197 @retval EFI_SUCCESS The ATAPI device identify data were retrieved successfully.
3198 @retval others Some error occurred during the identification that ATAPI device.
3202 AtapiIdentifyDevice (
3203 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
3206 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket
;
3210 // Initialize SCSI REQUEST_PACKET and 6-byte Cdb
3212 ZeroMem (&CommandPacket
, sizeof (CommandPacket
));
3213 ZeroMem (Cdb
, sizeof (Cdb
));
3215 Cdb
[0] = ATA_CMD_IDENTIFY_DEVICE
;
3216 CommandPacket
.Timeout
= SCSI_DISK_TIMEOUT
;
3217 CommandPacket
.Cdb
= Cdb
;
3218 CommandPacket
.CdbLength
= (UINT8
) sizeof (Cdb
);
3219 CommandPacket
.InDataBuffer
= &ScsiDiskDevice
->IdentifyData
;
3220 CommandPacket
.InTransferLength
= sizeof (ScsiDiskDevice
->IdentifyData
);
3222 return ScsiDiskDevice
->ScsiIo
->ExecuteScsiCommand (ScsiDiskDevice
->ScsiIo
, &CommandPacket
, NULL
);
3227 Initialize the installation of DiskInfo protocol.
3229 This function prepares for the installation of DiskInfo protocol on the child handle.
3230 By default, it installs DiskInfo protocol with SCSI interface GUID. If it further
3231 detects that the physical device is an ATAPI/AHCI device, it then updates interface GUID
3232 to be IDE/AHCI interface GUID.
3234 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV.
3235 @param ChildHandle Child handle to install DiskInfo protocol.
3239 InitializeInstallDiskInfo (
3240 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
3241 IN EFI_HANDLE ChildHandle
3245 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
3246 EFI_DEVICE_PATH_PROTOCOL
*ChildDevicePathNode
;
3247 ATAPI_DEVICE_PATH
*AtapiDevicePath
;
3248 SATA_DEVICE_PATH
*SataDevicePath
;
3249 UINTN IdentifyRetry
;
3251 Status
= gBS
->HandleProtocol (ChildHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**) &DevicePathNode
);
3253 // Device Path protocol must be installed on the device handle.
3255 ASSERT_EFI_ERROR (Status
);
3257 // Copy the DiskInfo protocol template.
3259 CopyMem (&ScsiDiskDevice
->DiskInfo
, &gScsiDiskInfoProtocolTemplate
, sizeof (gScsiDiskInfoProtocolTemplate
));
3261 while (!IsDevicePathEnd (DevicePathNode
)) {
3262 ChildDevicePathNode
= NextDevicePathNode (DevicePathNode
);
3263 if ((DevicePathType (DevicePathNode
) == HARDWARE_DEVICE_PATH
) &&
3264 (DevicePathSubType (DevicePathNode
) == HW_PCI_DP
) &&
3265 (DevicePathType (ChildDevicePathNode
) == MESSAGING_DEVICE_PATH
) &&
3266 ((DevicePathSubType (ChildDevicePathNode
) == MSG_ATAPI_DP
) ||
3267 (DevicePathSubType (ChildDevicePathNode
) == MSG_SATA_DP
))) {
3272 // Issue ATA Identify Device Command via SCSI command, which is required to publish DiskInfo protocol
3273 // with IDE/AHCI interface GUID.
3275 Status
= AtapiIdentifyDevice (ScsiDiskDevice
);
3276 if (!EFI_ERROR (Status
)) {
3277 if (DevicePathSubType(ChildDevicePathNode
) == MSG_ATAPI_DP
) {
3279 // We find the valid ATAPI device path
3281 AtapiDevicePath
= (ATAPI_DEVICE_PATH
*) ChildDevicePathNode
;
3282 ScsiDiskDevice
->Channel
= AtapiDevicePath
->PrimarySecondary
;
3283 ScsiDiskDevice
->Device
= AtapiDevicePath
->SlaveMaster
;
3285 // Update the DiskInfo.Interface to IDE interface GUID for the physical ATAPI device.
3287 CopyGuid (&ScsiDiskDevice
->DiskInfo
.Interface
, &gEfiDiskInfoIdeInterfaceGuid
);
3290 // We find the valid SATA device path
3292 SataDevicePath
= (SATA_DEVICE_PATH
*) ChildDevicePathNode
;
3293 ScsiDiskDevice
->Channel
= SataDevicePath
->HBAPortNumber
;
3294 ScsiDiskDevice
->Device
= SataDevicePath
->PortMultiplierPortNumber
;
3296 // Update the DiskInfo.Interface to AHCI interface GUID for the physical AHCI device.
3298 CopyGuid (&ScsiDiskDevice
->DiskInfo
.Interface
, &gEfiDiskInfoAhciInterfaceGuid
);
3302 } while (--IdentifyRetry
> 0);
3304 DevicePathNode
= ChildDevicePathNode
;