2 SCSI disk driver that layers on every SCSI IO protocol in the system.
4 Copyright (c) 2006 - 2010, 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 The user Entry Point for module ScsiDisk.
38 The user code starts with this function.
40 @param ImageHandle The firmware allocated handle for the EFI image.
41 @param SystemTable A pointer to the EFI System Table.
43 @retval EFI_SUCCESS The entry point is executed successfully.
44 @retval other Some error occurs when executing this entry point.
50 IN EFI_HANDLE ImageHandle
,
51 IN EFI_SYSTEM_TABLE
*SystemTable
57 // Install driver model protocol(s).
59 Status
= EfiLibInstallDriverBindingComponentName2 (
62 &gScsiDiskDriverBinding
,
64 &gScsiDiskComponentName
,
65 &gScsiDiskComponentName2
67 ASSERT_EFI_ERROR (Status
);
74 Test to see if this driver supports ControllerHandle.
76 This service is called by the EFI boot service ConnectController(). In order
77 to make drivers as small as possible, there are a few calling restrictions for
78 this service. ConnectController() must follow these calling restrictions.
79 If any other agent wishes to call Supported() it must also follow these
82 @param This Protocol instance pointer.
83 @param ControllerHandle Handle of device to test
84 @param RemainingDevicePath Optional parameter use to pick a specific child
87 @retval EFI_SUCCESS This driver supports this device
88 @retval EFI_ALREADY_STARTED This driver is already running on this device
89 @retval other This driver does not support this device
94 ScsiDiskDriverBindingSupported (
95 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
96 IN EFI_HANDLE Controller
,
97 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
101 EFI_SCSI_IO_PROTOCOL
*ScsiIo
;
104 Status
= gBS
->OpenProtocol (
106 &gEfiScsiIoProtocolGuid
,
108 This
->DriverBindingHandle
,
110 EFI_OPEN_PROTOCOL_BY_DRIVER
112 if (EFI_ERROR (Status
)) {
116 Status
= ScsiIo
->GetDeviceType (ScsiIo
, &DeviceType
);
117 if (!EFI_ERROR (Status
)) {
118 if ((DeviceType
== EFI_SCSI_TYPE_DISK
) || (DeviceType
== EFI_SCSI_TYPE_CDROM
)) {
119 Status
= EFI_SUCCESS
;
121 Status
= EFI_UNSUPPORTED
;
127 &gEfiScsiIoProtocolGuid
,
128 This
->DriverBindingHandle
,
136 Start this driver on ControllerHandle.
138 This service is called by the EFI boot service ConnectController(). In order
139 to make drivers as small as possible, there are a few calling restrictions for
140 this service. ConnectController() must follow these calling restrictions. If
141 any other agent wishes to call Start() it must also follow these calling
144 @param This Protocol instance pointer.
145 @param ControllerHandle Handle of device to bind driver to
146 @param RemainingDevicePath Optional parameter use to pick a specific child
149 @retval EFI_SUCCESS This driver is added to ControllerHandle
150 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
151 @retval other This driver does not support this device
156 ScsiDiskDriverBindingStart (
157 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
158 IN EFI_HANDLE Controller
,
159 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
163 EFI_SCSI_IO_PROTOCOL
*ScsiIo
;
164 SCSI_DISK_DEV
*ScsiDiskDevice
;
170 ScsiDiskDevice
= (SCSI_DISK_DEV
*) AllocateZeroPool (sizeof (SCSI_DISK_DEV
));
171 if (ScsiDiskDevice
== NULL
) {
172 return EFI_OUT_OF_RESOURCES
;
175 Status
= gBS
->OpenProtocol (
177 &gEfiScsiIoProtocolGuid
,
179 This
->DriverBindingHandle
,
181 EFI_OPEN_PROTOCOL_BY_DRIVER
183 if (EFI_ERROR (Status
)) {
184 FreePool (ScsiDiskDevice
);
188 ScsiDiskDevice
->Signature
= SCSI_DISK_DEV_SIGNATURE
;
189 ScsiDiskDevice
->ScsiIo
= ScsiIo
;
190 ScsiDiskDevice
->BlkIo
.Media
= &ScsiDiskDevice
->BlkIoMedia
;
191 ScsiDiskDevice
->BlkIo
.Reset
= ScsiDiskReset
;
192 ScsiDiskDevice
->BlkIo
.ReadBlocks
= ScsiDiskReadBlocks
;
193 ScsiDiskDevice
->BlkIo
.WriteBlocks
= ScsiDiskWriteBlocks
;
194 ScsiDiskDevice
->BlkIo
.FlushBlocks
= ScsiDiskFlushBlocks
;
195 ScsiDiskDevice
->Handle
= Controller
;
197 ScsiIo
->GetDeviceType (ScsiIo
, &(ScsiDiskDevice
->DeviceType
));
198 switch (ScsiDiskDevice
->DeviceType
) {
199 case EFI_SCSI_TYPE_DISK
:
200 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x200;
203 case EFI_SCSI_TYPE_CDROM
:
204 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x800;
208 // The Sense Data Array's initial size is 6
210 ScsiDiskDevice
->SenseDataNumber
= 6;
211 ScsiDiskDevice
->SenseData
= (EFI_SCSI_SENSE_DATA
*) AllocateZeroPool (
212 sizeof (EFI_SCSI_SENSE_DATA
) * ScsiDiskDevice
->SenseDataNumber
214 if (ScsiDiskDevice
->SenseData
== NULL
) {
217 &gEfiScsiIoProtocolGuid
,
218 This
->DriverBindingHandle
,
221 FreePool (ScsiDiskDevice
);
222 return EFI_OUT_OF_RESOURCES
;
226 // Retrieve device information
229 for (Index
= 0; Index
< MaxRetry
; Index
++) {
230 Status
= ScsiDiskInquiryDevice (ScsiDiskDevice
, &NeedRetry
);
231 if (!EFI_ERROR (Status
)) {
236 FreePool (ScsiDiskDevice
->SenseData
);
239 &gEfiScsiIoProtocolGuid
,
240 This
->DriverBindingHandle
,
243 FreePool (ScsiDiskDevice
);
244 return EFI_DEVICE_ERROR
;
248 // The second parameter "TRUE" means must
249 // retrieve media capacity
251 Status
= ScsiDiskDetectMedia (ScsiDiskDevice
, TRUE
, &Temp
);
252 if (!EFI_ERROR (Status
)) {
254 // Determine if Block IO should be produced on this controller handle
256 if (DetermineInstallBlockIo(Controller
)) {
257 InitializeInstallDiskInfo(ScsiDiskDevice
, Controller
);
258 Status
= gBS
->InstallMultipleProtocolInterfaces (
260 &gEfiBlockIoProtocolGuid
,
261 &ScsiDiskDevice
->BlkIo
,
262 &gEfiDiskInfoProtocolGuid
,
263 &ScsiDiskDevice
->DiskInfo
,
266 if (!EFI_ERROR(Status
)) {
267 ScsiDiskDevice
->ControllerNameTable
= NULL
;
270 gScsiDiskComponentName
.SupportedLanguages
,
271 &ScsiDiskDevice
->ControllerNameTable
,
277 gScsiDiskComponentName2
.SupportedLanguages
,
278 &ScsiDiskDevice
->ControllerNameTable
,
287 gBS
->FreePool (ScsiDiskDevice
->SenseData
);
288 gBS
->FreePool (ScsiDiskDevice
);
291 &gEfiScsiIoProtocolGuid
,
292 This
->DriverBindingHandle
,
301 Stop this driver on ControllerHandle.
303 This service is called by the EFI boot service DisconnectController().
304 In order to make drivers as small as possible, there are a few calling
305 restrictions for this service. DisconnectController() must follow these
306 calling restrictions. If any other agent wishes to call Stop() it must
307 also follow these calling restrictions.
309 @param This Protocol instance pointer.
310 @param ControllerHandle Handle of device to stop driver on
311 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
312 children is zero stop the entire bus driver.
313 @param ChildHandleBuffer List of Child Handles to Stop.
315 @retval EFI_SUCCESS This driver is removed ControllerHandle
316 @retval other This driver was not removed from this device
321 ScsiDiskDriverBindingStop (
322 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
323 IN EFI_HANDLE Controller
,
324 IN UINTN NumberOfChildren
,
325 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
328 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
329 SCSI_DISK_DEV
*ScsiDiskDevice
;
332 Status
= gBS
->OpenProtocol (
334 &gEfiBlockIoProtocolGuid
,
336 This
->DriverBindingHandle
,
338 EFI_OPEN_PROTOCOL_GET_PROTOCOL
340 if (EFI_ERROR (Status
)) {
344 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (BlkIo
);
345 Status
= gBS
->UninstallMultipleProtocolInterfaces (
347 &gEfiBlockIoProtocolGuid
,
348 &ScsiDiskDevice
->BlkIo
,
349 &gEfiDiskInfoProtocolGuid
,
350 &ScsiDiskDevice
->DiskInfo
,
353 if (!EFI_ERROR (Status
)) {
356 &gEfiScsiIoProtocolGuid
,
357 This
->DriverBindingHandle
,
361 ReleaseScsiDiskDeviceResources (ScsiDiskDevice
);
375 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
376 @param ExtendedVerification The flag about if extend verificate
378 @retval EFI_SUCCESS The device was reset.
379 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
381 @return EFI_STATUS is returned from EFI_SCSI_IO_PROTOCOL.ResetDevice().
387 IN EFI_BLOCK_IO_PROTOCOL
*This
,
388 IN BOOLEAN ExtendedVerification
392 SCSI_DISK_DEV
*ScsiDiskDevice
;
395 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
397 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (This
);
399 Status
= ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
401 if (!ExtendedVerification
) {
405 Status
= ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
408 gBS
->RestoreTPL (OldTpl
);
413 The function is to Read Block from SCSI Disk.
415 @param This The pointer of EFI_BLOCK_IO_PROTOCOL.
416 @param MediaId The Id of Media detected
417 @param Lba The logic block address
418 @param BufferSize The size of Buffer
419 @param Buffer The buffer to fill the read out data
421 @retval EFI_SUCCESS Successfully to read out block.
422 @retval EFI_DEVICE_ERROR Fail to detect media.
423 @retval EFI_NO_MEDIA Media is not present.
424 @retval EFI_MEDIA_CHANGED Media has changed.
425 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
426 @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
432 IN EFI_BLOCK_IO_PROTOCOL
*This
,
439 SCSI_DISK_DEV
*ScsiDiskDevice
;
440 EFI_BLOCK_IO_MEDIA
*Media
;
443 UINTN NumberOfBlocks
;
448 if (Buffer
== NULL
) {
449 return EFI_INVALID_PARAMETER
;
452 if (BufferSize
== 0) {
456 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
458 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (This
);
460 if (!IS_DEVICE_FIXED(ScsiDiskDevice
)) {
462 Status
= ScsiDiskDetectMedia (ScsiDiskDevice
, FALSE
, &MediaChange
);
463 if (EFI_ERROR (Status
)) {
464 Status
= EFI_DEVICE_ERROR
;
469 gBS
->ReinstallProtocolInterface (
470 ScsiDiskDevice
->Handle
,
471 &gEfiBlockIoProtocolGuid
,
472 &ScsiDiskDevice
->BlkIo
,
473 &ScsiDiskDevice
->BlkIo
478 // Get the intrinsic block size
480 Media
= ScsiDiskDevice
->BlkIo
.Media
;
481 BlockSize
= Media
->BlockSize
;
483 NumberOfBlocks
= BufferSize
/ BlockSize
;
485 if (!(Media
->MediaPresent
)) {
486 Status
= EFI_NO_MEDIA
;
490 if (MediaId
!= Media
->MediaId
) {
491 Status
= EFI_MEDIA_CHANGED
;
495 if (BufferSize
% BlockSize
!= 0) {
496 Status
= EFI_BAD_BUFFER_SIZE
;
500 if (Lba
> Media
->LastBlock
) {
501 Status
= EFI_INVALID_PARAMETER
;
505 if ((Lba
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
506 Status
= EFI_INVALID_PARAMETER
;
510 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
511 Status
= EFI_INVALID_PARAMETER
;
516 // If all the parameters are valid, then perform read sectors command
517 // to transfer data from device to host.
519 Status
= ScsiDiskReadSectors (ScsiDiskDevice
, Buffer
, Lba
, NumberOfBlocks
);
522 gBS
->RestoreTPL (OldTpl
);
527 The function is to Write Block to SCSI Disk.
529 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
530 @param MediaId The Id of Media detected
531 @param Lba The logic block address
532 @param BufferSize The size of Buffer
533 @param Buffer The buffer to fill the read out data
535 @retval EFI_SUCCESS Successfully to read out block.
536 @retval EFI_WRITE_PROTECTED The device can not be written to.
537 @retval EFI_DEVICE_ERROR Fail to detect media.
538 @retval EFI_NO_MEDIA Media is not present.
539 @retval EFI_MEDIA_CHNAGED Media has changed.
540 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
541 @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
546 ScsiDiskWriteBlocks (
547 IN EFI_BLOCK_IO_PROTOCOL
*This
,
554 SCSI_DISK_DEV
*ScsiDiskDevice
;
555 EFI_BLOCK_IO_MEDIA
*Media
;
558 UINTN NumberOfBlocks
;
563 if (Buffer
== NULL
) {
564 return EFI_INVALID_PARAMETER
;
567 if (BufferSize
== 0) {
571 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
573 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (This
);
575 if (!IS_DEVICE_FIXED(ScsiDiskDevice
)) {
577 Status
= ScsiDiskDetectMedia (ScsiDiskDevice
, FALSE
, &MediaChange
);
578 if (EFI_ERROR (Status
)) {
579 Status
= EFI_DEVICE_ERROR
;
584 gBS
->ReinstallProtocolInterface (
585 ScsiDiskDevice
->Handle
,
586 &gEfiBlockIoProtocolGuid
,
587 &ScsiDiskDevice
->BlkIo
,
588 &ScsiDiskDevice
->BlkIo
593 // Get the intrinsic block size
595 Media
= ScsiDiskDevice
->BlkIo
.Media
;
596 BlockSize
= Media
->BlockSize
;
598 NumberOfBlocks
= BufferSize
/ BlockSize
;
600 if (!(Media
->MediaPresent
)) {
601 Status
= EFI_NO_MEDIA
;
605 if (MediaId
!= Media
->MediaId
) {
606 Status
= EFI_MEDIA_CHANGED
;
610 if (BufferSize
% BlockSize
!= 0) {
611 Status
= EFI_BAD_BUFFER_SIZE
;
615 if (Lba
> Media
->LastBlock
) {
616 Status
= EFI_INVALID_PARAMETER
;
620 if ((Lba
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
621 Status
= EFI_INVALID_PARAMETER
;
625 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
626 Status
= EFI_INVALID_PARAMETER
;
630 // if all the parameters are valid, then perform read sectors command
631 // to transfer data from device to host.
633 Status
= ScsiDiskWriteSectors (ScsiDiskDevice
, Buffer
, Lba
, NumberOfBlocks
);
636 gBS
->RestoreTPL (OldTpl
);
643 EFI_SUCCESS is returned directly.
645 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
647 @retval EFI_SUCCESS All outstanding data was written to the device
652 ScsiDiskFlushBlocks (
653 IN EFI_BLOCK_IO_PROTOCOL
*This
664 Detect Device and read out capacity ,if error occurs, parse the sense key.
666 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
667 @param MustReadCapacity The flag about reading device capacity
668 @param MediaChange The pointer of flag indicates if media has changed
670 @retval EFI_DEVICE_ERROR Indicates that error occurs
671 @retval EFI_SUCCESS Successfully to detect media
675 ScsiDiskDetectMedia (
676 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
677 IN BOOLEAN MustReadCapacity
,
678 OUT BOOLEAN
*MediaChange
682 EFI_STATUS ReadCapacityStatus
;
683 EFI_SCSI_SENSE_DATA
*SenseData
;
684 UINTN NumberOfSenseKeys
;
686 BOOLEAN NeedReadCapacity
;
689 EFI_BLOCK_IO_MEDIA OldMedia
;
692 Status
= EFI_SUCCESS
;
693 ReadCapacityStatus
= EFI_SUCCESS
;
695 NumberOfSenseKeys
= 0;
696 NeedReadCapacity
= FALSE
;
697 CopyMem (&OldMedia
, ScsiDiskDevice
->BlkIo
.Media
, sizeof (OldMedia
));
698 *MediaChange
= FALSE
;
701 for (Index
= 0; Index
< MaxRetry
; Index
++) {
702 Status
= ScsiDiskTestUnitReady (
708 if (!EFI_ERROR (Status
)) {
717 if ((Index
== MaxRetry
) && EFI_ERROR (Status
)) {
718 return EFI_DEVICE_ERROR
;
721 Status
= DetectMediaParsingSenseKeys (
727 if (EFI_ERROR (Status
)) {
731 // ACTION_NO_ACTION: need not read capacity
732 // other action code: need read capacity
734 if (Action
== ACTION_NO_ACTION
) {
735 NeedReadCapacity
= FALSE
;
737 NeedReadCapacity
= TRUE
;
741 // either NeedReadCapacity is TRUE, or MustReadCapacity is TRUE,
742 // retrieve capacity via Read Capacity command
744 if (NeedReadCapacity
|| MustReadCapacity
) {
746 // retrieve media information
749 for (Index
= 0; Index
< MaxRetry
; Index
++) {
751 ReadCapacityStatus
= ScsiDiskReadCapacity (
757 if (EFI_ERROR (ReadCapacityStatus
) && !NeedRetry
) {
758 return EFI_DEVICE_ERROR
;
761 // analyze sense key to action
763 Status
= DetectMediaParsingSenseKeys (
770 // if Status is error, it may indicate crisis error,
771 // so return without retry.
773 if (EFI_ERROR (Status
)) {
778 case ACTION_NO_ACTION
:
785 case ACTION_RETRY_COMMAND_LATER
:
787 // retry the ReadCapacity later and continuously, until the condition
788 // no longer emerges.
789 // stall time is 100000us, or say 0.1 second.
797 // other cases, just retry the command
803 if ((Index
== MaxRetry
) && EFI_ERROR (ReadCapacityStatus
)) {
804 return EFI_DEVICE_ERROR
;
808 if (ScsiDiskDevice
->BlkIo
.Media
->MediaId
!= OldMedia
.MediaId
) {
810 // Media change information got from the device
815 if (ScsiDiskDevice
->BlkIo
.Media
->ReadOnly
!= OldMedia
.ReadOnly
) {
817 ScsiDiskDevice
->BlkIo
.Media
->MediaId
+= 1;
820 if (ScsiDiskDevice
->BlkIo
.Media
->BlockSize
!= OldMedia
.BlockSize
) {
822 ScsiDiskDevice
->BlkIo
.Media
->MediaId
+= 1;
825 if (ScsiDiskDevice
->BlkIo
.Media
->LastBlock
!= OldMedia
.LastBlock
) {
827 ScsiDiskDevice
->BlkIo
.Media
->MediaId
+= 1;
830 if (ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
!= OldMedia
.MediaPresent
) {
831 if (ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
) {
833 // when change from no media to media present, reset the MediaId to 1.
835 ScsiDiskDevice
->BlkIo
.Media
->MediaId
= 1;
838 // when no media, reset the MediaId to zero.
840 ScsiDiskDevice
->BlkIo
.Media
->MediaId
= 0;
851 Send out Inquiry command to Device.
853 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
854 @param NeedRetry Indicates if needs try again when error happens
856 @retval EFI_DEVICE_ERROR Indicates that error occurs
857 @retval EFI_SUCCESS Successfully to detect media
861 ScsiDiskInquiryDevice (
862 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
863 OUT BOOLEAN
*NeedRetry
866 UINT32 InquiryDataLength
;
867 UINT8 SenseDataLength
;
868 UINT8 HostAdapterStatus
;
870 EFI_SCSI_SENSE_DATA
*SenseDataArray
;
871 UINTN NumberOfSenseKeys
;
876 InquiryDataLength
= sizeof (EFI_SCSI_INQUIRY_DATA
);
879 Status
= ScsiInquiryCommand (
880 ScsiDiskDevice
->ScsiIo
,
881 EFI_TIMER_PERIOD_SECONDS (1),
886 (VOID
*) &(ScsiDiskDevice
->InquiryData
),
891 // no need to check HostAdapterStatus and TargetStatus
893 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_WARN_BUFFER_TOO_SMALL
)) {
894 ParseInquiryData (ScsiDiskDevice
);
897 } else if (Status
== EFI_NOT_READY
) {
899 return EFI_DEVICE_ERROR
;
901 } else if ((Status
== EFI_INVALID_PARAMETER
) || (Status
== EFI_UNSUPPORTED
)) {
903 return EFI_DEVICE_ERROR
;
906 // go ahead to check HostAdapterStatus and TargetStatus
907 // (EFI_TIMEOUT, EFI_DEVICE_ERROR)
910 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
911 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
913 return EFI_DEVICE_ERROR
;
914 } else if (Status
== EFI_DEVICE_ERROR
) {
916 // reset the scsi channel
918 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
920 return EFI_DEVICE_ERROR
;
923 Status
= CheckTargetStatus (TargetStatus
);
924 if (Status
== EFI_NOT_READY
) {
926 // reset the scsi device
928 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
930 return EFI_DEVICE_ERROR
;
932 } else if (Status
== EFI_DEVICE_ERROR
) {
934 return EFI_DEVICE_ERROR
;
938 // if goes here, meant ScsiInquiryCommand() failed.
939 // if ScsiDiskRequestSenseKeys() succeeds at last,
940 // better retry ScsiInquiryCommand(). (by setting *NeedRetry = TRUE)
943 for (Index
= 0; Index
< MaxRetry
; Index
++) {
944 Status
= ScsiDiskRequestSenseKeys (
951 if (!EFI_ERROR (Status
)) {
953 return EFI_DEVICE_ERROR
;
957 return EFI_DEVICE_ERROR
;
961 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
962 // set *NeedRetry = FALSE to avoid the outside caller try again.
965 return EFI_DEVICE_ERROR
;
971 When Test Unit Ready command succeeds, retrieve Sense Keys via Request Sense;
972 When Test Unit Ready command encounters any error caused by host adapter or
973 target, return error without retrieving Sense Keys.
975 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
976 @param NeedRetry The pointer of flag indicates try again
977 @param SenseDataArray The pointer of an array of sense data
978 @param NumberOfSenseKeys The pointer of the number of sense data array
980 @retval EFI_DEVICE_ERROR Indicates that error occurs
981 @retval EFI_SUCCESS Successfully to test unit
985 ScsiDiskTestUnitReady (
986 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
987 OUT BOOLEAN
*NeedRetry
,
988 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
989 OUT UINTN
*NumberOfSenseKeys
993 UINT8 SenseDataLength
;
994 UINT8 HostAdapterStatus
;
1000 *NumberOfSenseKeys
= 0;
1003 // Parameter 3 and 4: do not require sense data, retrieve it when needed.
1005 Status
= ScsiTestUnitReadyCommand (
1006 ScsiDiskDevice
->ScsiIo
,
1007 EFI_TIMER_PERIOD_SECONDS (1),
1014 // no need to check HostAdapterStatus and TargetStatus
1016 if (Status
== EFI_NOT_READY
) {
1018 return EFI_DEVICE_ERROR
;
1020 } else if ((Status
== EFI_INVALID_PARAMETER
) || (Status
== EFI_UNSUPPORTED
)) {
1022 return EFI_DEVICE_ERROR
;
1025 // go ahead to check HostAdapterStatus and TargetStatus(in case of EFI_DEVICE_ERROR)
1028 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
1029 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
1031 return EFI_DEVICE_ERROR
;
1033 } else if (Status
== EFI_DEVICE_ERROR
) {
1035 // reset the scsi channel
1037 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
1039 return EFI_DEVICE_ERROR
;
1042 Status
= CheckTargetStatus (TargetStatus
);
1043 if (Status
== EFI_NOT_READY
) {
1045 // reset the scsi device
1047 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
1049 return EFI_DEVICE_ERROR
;
1051 } else if (Status
== EFI_DEVICE_ERROR
) {
1053 return EFI_DEVICE_ERROR
;
1057 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1058 Status
= ScsiDiskRequestSenseKeys (
1065 if (!EFI_ERROR (Status
)) {
1070 return EFI_DEVICE_ERROR
;
1074 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
1075 // set *NeedRetry = FALSE to avoid the outside caller try again.
1078 return EFI_DEVICE_ERROR
;
1082 Parsing Sense Keys which got from request sense command.
1084 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1085 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
1086 @param NumberOfSenseKeys The number of sense key
1087 @param Action The pointer of action which indicates what is need to do next
1089 @retval EFI_DEVICE_ERROR Indicates that error occurs
1090 @retval EFI_SUCCESS Successfully to complete the parsing
1094 DetectMediaParsingSenseKeys (
1095 OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1096 IN EFI_SCSI_SENSE_DATA
*SenseData
,
1097 IN UINTN NumberOfSenseKeys
,
1104 // Default is to read capacity, unless..
1106 *Action
= ACTION_READ_CAPACITY
;
1108 if (NumberOfSenseKeys
== 0) {
1109 *Action
= ACTION_NO_ACTION
;
1113 if (!ScsiDiskHaveSenseKey (SenseData
, NumberOfSenseKeys
)) {
1115 // No Sense Key returned from last submitted command
1117 *Action
= ACTION_NO_ACTION
;
1121 if (ScsiDiskIsNoMedia (SenseData
, NumberOfSenseKeys
)) {
1122 ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
= FALSE
;
1123 ScsiDiskDevice
->BlkIo
.Media
->LastBlock
= 0;
1124 *Action
= ACTION_NO_ACTION
;
1128 if (ScsiDiskIsMediaChange (SenseData
, NumberOfSenseKeys
)) {
1129 ScsiDiskDevice
->BlkIo
.Media
->MediaId
++;
1133 if (ScsiDiskIsMediaError (SenseData
, NumberOfSenseKeys
)) {
1134 ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
= FALSE
;
1135 ScsiDiskDevice
->BlkIo
.Media
->LastBlock
= 0;
1136 return EFI_DEVICE_ERROR
;
1139 if (ScsiDiskIsHardwareError (SenseData
, NumberOfSenseKeys
)) {
1140 return EFI_DEVICE_ERROR
;
1143 if (!ScsiDiskIsDriveReady (SenseData
, NumberOfSenseKeys
, &RetryLater
)) {
1145 *Action
= ACTION_RETRY_COMMAND_LATER
;
1149 return EFI_DEVICE_ERROR
;
1157 Send read capacity command to device and get the device parameter.
1159 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1160 @param NeedRetry The pointer of flag indicates if need a retry
1161 @param SenseDataArray The pointer of an array of sense data
1162 @param NumberOfSenseKeys The number of sense key
1164 @retval EFI_DEVICE_ERROR Indicates that error occurs
1165 @retval EFI_SUCCESS Successfully to read capacity
1169 ScsiDiskReadCapacity (
1170 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1171 OUT BOOLEAN
*NeedRetry
,
1172 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
1173 OUT UINTN
*NumberOfSenseKeys
1176 UINT8 HostAdapterStatus
;
1178 EFI_STATUS CommandStatus
;
1182 UINT8 SenseDataLength
;
1184 UINT32 DataLength10
;
1185 UINT32 DataLength16
;
1186 EFI_SCSI_DISK_CAPACITY_DATA CapacityData10
;
1187 EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData16
;
1190 SenseDataLength
= 0;
1191 DataLength10
= sizeof (EFI_SCSI_DISK_CAPACITY_DATA
);
1192 DataLength16
= sizeof (EFI_SCSI_DISK_CAPACITY_DATA16
);
1193 ZeroMem (&CapacityData10
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA
));
1194 ZeroMem (&CapacityData16
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16
));
1196 *NumberOfSenseKeys
= 0;
1198 ScsiVersion
= (UINT8
)(ScsiDiskDevice
->InquiryData
.Version
& 0x03);
1200 if (ScsiVersion
< SCSI_COMMAND_VERSION_3
) {
1202 // submit Read Capacity(10) Command. in this call,not request sense data
1204 CommandStatus
= ScsiReadCapacityCommand (
1205 ScsiDiskDevice
->ScsiIo
,
1206 EFI_TIMER_PERIOD_SECONDS(1),
1211 (VOID
*) &CapacityData10
,
1217 // submit Read Capacity(16) Command to get parameter LogicalBlocksPerPhysicalBlock
1218 // and LowestAlignedLba
1220 CommandStatus
= ScsiReadCapacity16Command (
1221 ScsiDiskDevice
->ScsiIo
,
1222 EFI_TIMER_PERIOD_SECONDS (1),
1227 (VOID
*) &CapacityData16
,
1233 // no need to check HostAdapterStatus and TargetStatus
1235 if (CommandStatus
== EFI_SUCCESS
) {
1236 GetMediaInfo (ScsiDiskDevice
, &CapacityData10
,&CapacityData16
);
1239 } else if (CommandStatus
== EFI_NOT_READY
) {
1241 return EFI_DEVICE_ERROR
;
1243 } else if ((CommandStatus
== EFI_INVALID_PARAMETER
) || (CommandStatus
== EFI_UNSUPPORTED
)) {
1245 return EFI_DEVICE_ERROR
;
1248 // go ahead to check HostAdapterStatus and TargetStatus
1249 // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
1252 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
1253 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
1255 return EFI_DEVICE_ERROR
;
1257 } else if (Status
== EFI_DEVICE_ERROR
) {
1259 // reset the scsi channel
1261 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
1263 return EFI_DEVICE_ERROR
;
1266 Status
= CheckTargetStatus (TargetStatus
);
1267 if (Status
== EFI_NOT_READY
) {
1269 // reset the scsi device
1271 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
1273 return EFI_DEVICE_ERROR
;
1275 } else if (Status
== EFI_DEVICE_ERROR
) {
1277 return EFI_DEVICE_ERROR
;
1281 // if goes here, meant ScsiReadCapacityCommand() failed.
1282 // if ScsiDiskRequestSenseKeys() succeeds at last,
1283 // better retry ScsiReadCapacityCommand(). (by setting *NeedRetry = TRUE)
1286 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1288 Status
= ScsiDiskRequestSenseKeys (
1295 if (!EFI_ERROR (Status
)) {
1297 return EFI_DEVICE_ERROR
;
1301 return EFI_DEVICE_ERROR
;
1305 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
1306 // set *NeedRetry = FALSE to avoid the outside caller try again.
1309 return EFI_DEVICE_ERROR
;
1313 Check the HostAdapter status and re-interpret it in EFI_STATUS.
1315 @param HostAdapterStatus Host Adapter status
1317 @retval EFI_SUCCESS Host adapter is OK.
1318 @retval EFI_TIMEOUT Timeout.
1319 @retval EFI_NOT_READY Adapter NOT ready.
1320 @retval EFI_DEVICE_ERROR Adapter device error.
1324 CheckHostAdapterStatus (
1325 IN UINT8 HostAdapterStatus
1328 switch (HostAdapterStatus
) {
1329 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OK
:
1332 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_SELECTION_TIMEOUT
:
1333 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT
:
1334 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT_COMMAND
:
1337 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_MESSAGE_REJECT
:
1338 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PARITY_ERROR
:
1339 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_REQUEST_SENSE_FAILED
:
1340 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_DATA_OVERRUN_UNDERRUN
:
1341 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_RESET
:
1342 return EFI_NOT_READY
;
1344 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_FREE
:
1345 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR
:
1346 return EFI_DEVICE_ERROR
;
1355 Check the target status and re-interpret it in EFI_STATUS.
1357 @param TargetStatus Target status
1359 @retval EFI_NOT_READY Device is NOT ready.
1360 @retval EFI_DEVICE_ERROR
1366 IN UINT8 TargetStatus
1369 switch (TargetStatus
) {
1370 case EFI_EXT_SCSI_STATUS_TARGET_GOOD
:
1371 case EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION
:
1372 case EFI_EXT_SCSI_STATUS_TARGET_CONDITION_MET
:
1375 case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE
:
1376 case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE_CONDITION_MET
:
1377 case EFI_EXT_SCSI_STATUS_TARGET_BUSY
:
1378 case EFI_EXT_SCSI_STATUS_TARGET_TASK_SET_FULL
:
1379 return EFI_NOT_READY
;
1381 case EFI_EXT_SCSI_STATUS_TARGET_RESERVATION_CONFLICT
:
1382 return EFI_DEVICE_ERROR
;
1392 Retrieve all sense keys from the device.
1394 When encountering error during the process, if retrieve sense keys before
1395 error encountered, it returns the sense keys with return status set to EFI_SUCCESS,
1396 and NeedRetry set to FALSE; otherwize, return the proper return status.
1398 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1399 @param NeedRetry The pointer of flag indicates if need a retry
1400 @param SenseDataArray The pointer of an array of sense data
1401 @param NumberOfSenseKeys The number of sense key
1402 @param AskResetIfError The flag indicates if need reset when error occurs
1404 @retval EFI_DEVICE_ERROR Indicates that error occurs
1405 @retval EFI_SUCCESS Successfully to request sense key
1409 ScsiDiskRequestSenseKeys (
1410 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1411 OUT BOOLEAN
*NeedRetry
,
1412 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
1413 OUT UINTN
*NumberOfSenseKeys
,
1414 IN BOOLEAN AskResetIfError
1417 EFI_SCSI_SENSE_DATA
*PtrSenseData
;
1418 UINT8 SenseDataLength
;
1421 EFI_STATUS FallStatus
;
1422 UINT8 HostAdapterStatus
;
1425 FallStatus
= EFI_SUCCESS
;
1426 SenseDataLength
= sizeof (EFI_SCSI_SENSE_DATA
);
1429 ScsiDiskDevice
->SenseData
,
1430 sizeof (EFI_SCSI_SENSE_DATA
) * (ScsiDiskDevice
->SenseDataNumber
)
1433 *NumberOfSenseKeys
= 0;
1434 *SenseDataArray
= ScsiDiskDevice
->SenseData
;
1435 PtrSenseData
= ScsiDiskDevice
->SenseData
;
1437 for (SenseReq
= TRUE
; SenseReq
;) {
1438 Status
= ScsiRequestSenseCommand (
1439 ScsiDiskDevice
->ScsiIo
,
1440 EFI_TIMER_PERIOD_SECONDS (2),
1446 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_WARN_BUFFER_TOO_SMALL
)) {
1447 FallStatus
= EFI_SUCCESS
;
1449 } else if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
1451 FallStatus
= EFI_DEVICE_ERROR
;
1453 } else if ((Status
== EFI_INVALID_PARAMETER
) || (Status
== EFI_UNSUPPORTED
)) {
1455 FallStatus
= EFI_DEVICE_ERROR
;
1457 } else if (Status
== EFI_DEVICE_ERROR
) {
1458 if (AskResetIfError
) {
1459 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
1462 FallStatus
= EFI_DEVICE_ERROR
;
1465 if (EFI_ERROR (FallStatus
)) {
1466 if (*NumberOfSenseKeys
!= 0) {
1470 return EFI_DEVICE_ERROR
;
1474 (*NumberOfSenseKeys
) += 1;
1477 // no more sense key or number of sense keys exceeds predefined,
1480 if ((PtrSenseData
->Sense_Key
== EFI_SCSI_SK_NO_SENSE
) ||
1481 (*NumberOfSenseKeys
== ScsiDiskDevice
->SenseDataNumber
)) {
1491 Get information from media read capacity command.
1493 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1494 @param Capacity10 The pointer of EFI_SCSI_DISK_CAPACITY_DATA
1495 @param Capacity16 The pointer of EFI_SCSI_DISK_CAPACITY_DATA16
1500 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1501 IN EFI_SCSI_DISK_CAPACITY_DATA
*Capacity10
,
1502 IN EFI_SCSI_DISK_CAPACITY_DATA16
*Capacity16
1508 ScsiVersion
= (UINT8
)(ScsiDiskDevice
->InquiryData
.Version
& 0x03);
1509 ScsiDiskDevice
->BlkIo
.Media
->LowestAlignedLba
= 0;
1510 ScsiDiskDevice
->BlkIo
.Media
->LogicalBlocksPerPhysicalBlock
= 1;
1513 if (ScsiVersion
< SCSI_COMMAND_VERSION_3
) {
1514 ScsiDiskDevice
->BlkIo
.Media
->LastBlock
= (Capacity10
->LastLba3
<< 24) |
1515 (Capacity10
->LastLba2
<< 16) |
1516 (Capacity10
->LastLba1
<< 8) |
1517 Capacity10
->LastLba0
;
1519 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= (Capacity10
->BlockSize3
<< 24) |
1520 (Capacity10
->BlockSize2
<< 16) |
1521 (Capacity10
->BlockSize1
<< 8) |
1522 Capacity10
->BlockSize0
;
1523 ScsiDiskDevice
->BlkIo
.Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION
;
1526 Ptr
= (UINT8
*)&ScsiDiskDevice
->BlkIo
.Media
->LastBlock
;
1527 *Ptr
++ = Capacity16
->LastLba0
;
1528 *Ptr
++ = Capacity16
->LastLba1
;
1529 *Ptr
++ = Capacity16
->LastLba2
;
1530 *Ptr
++ = Capacity16
->LastLba3
;
1531 *Ptr
++ = Capacity16
->LastLba4
;
1532 *Ptr
++ = Capacity16
->LastLba5
;
1533 *Ptr
++ = Capacity16
->LastLba6
;
1534 *Ptr
= Capacity16
->LastLba7
;
1536 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= (Capacity16
->BlockSize3
<< 24) |
1537 (Capacity16
->BlockSize2
<< 16) |
1538 (Capacity16
->BlockSize1
<< 8) |
1539 Capacity16
->BlockSize0
;
1541 ScsiDiskDevice
->BlkIo
.Media
->LowestAlignedLba
= (Capacity16
->LowestAlignLogic2
<< 8)|(Capacity16
->LowestAlignLogic1
);
1542 ScsiDiskDevice
->BlkIo
.Media
->LogicalBlocksPerPhysicalBlock
= Capacity16
->LogicPerPhysical
;
1543 ScsiDiskDevice
->BlkIo
.Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION2
;
1547 ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
= TRUE
;
1549 if (ScsiDiskDevice
->DeviceType
== EFI_SCSI_TYPE_DISK
) {
1550 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x200;
1553 if (ScsiDiskDevice
->DeviceType
== EFI_SCSI_TYPE_CDROM
) {
1554 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x800;
1561 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1566 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
1569 ScsiDiskDevice
->FixedDevice
= (BOOLEAN
) ((ScsiDiskDevice
->InquiryData
.Rmb
== 1) ? 0 : 1);
1570 ScsiDiskDevice
->BlkIoMedia
.RemovableMedia
= (BOOLEAN
) (!ScsiDiskDevice
->FixedDevice
);
1574 Read sector from SCSI Disk.
1576 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1577 @param Buffer The buffer to fill in the read out data
1578 @param Lba Logic block address
1579 @param NumberOfBlocks The number of blocks to read
1581 @retval EFI_DEVICE_ERROR Indicates a device error.
1582 @retval EFI_SUCCESS Operation is successful.
1586 ScsiDiskReadSectors (
1587 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1590 IN UINTN NumberOfBlocks
1593 UINTN BlocksRemaining
;
1605 EFI_SCSI_SENSE_DATA
*SenseData
;
1606 UINTN NumberOfSenseKeys
;
1609 NumberOfSenseKeys
= 0;
1611 Status
= EFI_SUCCESS
;
1613 BlocksRemaining
= NumberOfBlocks
;
1614 BlockSize
= ScsiDiskDevice
->BlkIo
.Media
->BlockSize
;
1616 // limit the data bytes that can be transferred by one Read(10) Command
1621 Lba32
= (UINT32
) Lba
;
1623 while (BlocksRemaining
> 0) {
1625 if (BlocksRemaining
<= MaxBlock
) {
1627 SectorCount
= (UINT16
) BlocksRemaining
;
1630 SectorCount
= MaxBlock
;
1633 ByteCount
= SectorCount
* BlockSize
;
1634 Timeout
= EFI_TIMER_PERIOD_SECONDS (2);
1637 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1639 Status
= ScsiDiskRead10 (
1650 if (!EFI_ERROR (Status
)) {
1655 return EFI_DEVICE_ERROR
;
1660 if ((Index
== MaxRetry
) && (Status
!= EFI_SUCCESS
)) {
1661 return EFI_DEVICE_ERROR
;
1665 // actual transferred sectors
1667 SectorCount
= ByteCount
/ BlockSize
;
1669 Lba32
+= SectorCount
;
1670 PtrBuffer
= PtrBuffer
+ SectorCount
* BlockSize
;
1671 BlocksRemaining
-= SectorCount
;
1678 Write sector to SCSI Disk.
1680 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1681 @param Buffer The buffer of data to be written into SCSI Disk
1682 @param Lba Logic block address
1683 @param NumberOfBlocks The number of blocks to read
1685 @retval EFI_DEVICE_ERROR Indicates a device error.
1686 @retval EFI_SUCCESS Operation is successful.
1690 ScsiDiskWriteSectors (
1691 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1694 IN UINTN NumberOfBlocks
1697 UINTN BlocksRemaining
;
1709 EFI_SCSI_SENSE_DATA
*SenseData
;
1710 UINTN NumberOfSenseKeys
;
1713 NumberOfSenseKeys
= 0;
1715 Status
= EFI_SUCCESS
;
1717 BlocksRemaining
= NumberOfBlocks
;
1718 BlockSize
= ScsiDiskDevice
->BlkIo
.Media
->BlockSize
;
1720 // limit the data bytes that can be transferred by one Write(10) Command
1725 Lba32
= (UINT32
) Lba
;
1727 while (BlocksRemaining
> 0) {
1729 if (BlocksRemaining
<= MaxBlock
) {
1731 SectorCount
= (UINT16
) BlocksRemaining
;
1734 SectorCount
= MaxBlock
;
1737 ByteCount
= SectorCount
* BlockSize
;
1738 Timeout
= EFI_TIMER_PERIOD_SECONDS (2);
1740 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1741 Status
= ScsiDiskWrite10 (
1752 if (!EFI_ERROR (Status
)) {
1757 return EFI_DEVICE_ERROR
;
1761 if ((Index
== MaxRetry
) && (Status
!= EFI_SUCCESS
)) {
1762 return EFI_DEVICE_ERROR
;
1765 // actual transferred sectors
1767 SectorCount
= ByteCount
/ BlockSize
;
1769 Lba32
+= SectorCount
;
1770 PtrBuffer
= PtrBuffer
+ SectorCount
* BlockSize
;
1771 BlocksRemaining
-= SectorCount
;
1779 Submit Read command.
1781 @param ScsiDiskDevice The pointer of ScsiDiskDevice
1782 @param NeedRetry The pointer of flag indicates if needs retry if error happens
1783 @param SenseDataArray NOT used yet in this function
1784 @param NumberOfSenseKeys The number of sense key
1785 @param Timeout The time to complete the command
1786 @param DataBuffer The buffer to fill with the read out data
1787 @param DataLength The length of buffer
1788 @param StartLba The start logic block address
1789 @param SectorSize The size of sector
1791 @return EFI_STATUS is returned by calling ScsiRead10Command().
1795 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1796 OUT BOOLEAN
*NeedRetry
,
1797 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
, OPTIONAL
1798 OUT UINTN
*NumberOfSenseKeys
,
1800 OUT UINT8
*DataBuffer
,
1801 IN OUT UINT32
*DataLength
,
1803 IN UINT32 SectorSize
1806 UINT8 SenseDataLength
;
1808 UINT8 HostAdapterStatus
;
1812 *NumberOfSenseKeys
= 0;
1813 SenseDataLength
= 0;
1814 Status
= ScsiRead10Command (
1815 ScsiDiskDevice
->ScsiIo
,
1831 Submit Write Command.
1833 @param ScsiDiskDevice The pointer of ScsiDiskDevice
1834 @param NeedRetry The pointer of flag indicates if needs retry if error happens
1835 @param SenseDataArray NOT used yet in this function
1836 @param NumberOfSenseKeys The number of sense key
1837 @param Timeout The time to complete the command
1838 @param DataBuffer The buffer to fill with the read out data
1839 @param DataLength The length of buffer
1840 @param StartLba The start logic block address
1841 @param SectorSize The size of sector
1843 @return EFI_STATUS is returned by calling ScsiWrite10Command().
1848 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1849 OUT BOOLEAN
*NeedRetry
,
1850 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
, OPTIONAL
1851 OUT UINTN
*NumberOfSenseKeys
,
1853 IN UINT8
*DataBuffer
,
1854 IN OUT UINT32
*DataLength
,
1856 IN UINT32 SectorSize
1860 UINT8 SenseDataLength
;
1861 UINT8 HostAdapterStatus
;
1865 *NumberOfSenseKeys
= 0;
1866 SenseDataLength
= 0;
1867 Status
= ScsiWrite10Command (
1868 ScsiDiskDevice
->ScsiIo
,
1884 Check sense key to find if media presents.
1886 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
1887 @param SenseCounts The number of sense key
1889 @retval TRUE NOT any media
1890 @retval FALSE Media presents
1894 IN EFI_SCSI_SENSE_DATA
*SenseData
,
1895 IN UINTN SenseCounts
1898 EFI_SCSI_SENSE_DATA
*SensePtr
;
1903 SensePtr
= SenseData
;
1905 for (Index
= 0; Index
< SenseCounts
; Index
++) {
1907 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2),
1908 // Additional Sense Code is ASC_NO_MEDIA (0x3A)
1910 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_NOT_READY
) &&
1911 (SensePtr
->Addnl_Sense_Code
== EFI_SCSI_ASC_NO_MEDIA
)) {
1924 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
1925 @param SenseCounts The number of sense key
1928 @retval FALSE NOT error
1932 ScsiDiskIsMediaError (
1933 IN EFI_SCSI_SENSE_DATA
*SenseData
,
1934 IN UINTN SenseCounts
1937 EFI_SCSI_SENSE_DATA
*SensePtr
;
1942 SensePtr
= SenseData
;
1944 for (Index
= 0; Index
< SenseCounts
; Index
++) {
1946 switch (SensePtr
->Sense_Key
) {
1948 case EFI_SCSI_SK_MEDIUM_ERROR
:
1950 // Sense Key is EFI_SCSI_SK_MEDIUM_ERROR (0x3)
1952 switch (SensePtr
->Addnl_Sense_Code
) {
1957 case EFI_SCSI_ASC_MEDIA_ERR1
:
1962 case EFI_SCSI_ASC_MEDIA_ERR2
:
1967 case EFI_SCSI_ASC_MEDIA_ERR3
:
1968 case EFI_SCSI_ASC_MEDIA_ERR4
:
1978 case EFI_SCSI_SK_NOT_READY
:
1980 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2)
1982 switch (SensePtr
->Addnl_Sense_Code
) {
1984 // Additional Sense Code is ASC_MEDIA_UPSIDE_DOWN (0x6)
1986 case EFI_SCSI_ASC_MEDIA_UPSIDE_DOWN
:
2007 Check sense key to find if hardware error happens.
2009 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2010 @param SenseCounts The number of sense key
2012 @retval TRUE Hardware error exits.
2013 @retval FALSE NO error.
2017 ScsiDiskIsHardwareError (
2018 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2019 IN UINTN SenseCounts
2022 EFI_SCSI_SENSE_DATA
*SensePtr
;
2027 SensePtr
= SenseData
;
2029 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2032 // Sense Key is EFI_SCSI_SK_HARDWARE_ERROR (0x4)
2034 if (SensePtr
->Sense_Key
== EFI_SCSI_SK_HARDWARE_ERROR
) {
2046 Check sense key to find if media has changed.
2048 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2049 @param SenseCounts The number of sense key
2051 @retval TRUE Media is changed.
2052 @retval FALSE Media is NOT changed.
2055 ScsiDiskIsMediaChange (
2056 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2057 IN UINTN SenseCounts
2060 EFI_SCSI_SENSE_DATA
*SensePtr
;
2062 BOOLEAN IsMediaChanged
;
2064 IsMediaChanged
= FALSE
;
2065 SensePtr
= SenseData
;
2067 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2069 // Sense Key is EFI_SCSI_SK_UNIT_ATTENTION (0x6),
2070 // Additional sense code is EFI_SCSI_ASC_MEDIA_CHANGE (0x28)
2072 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_UNIT_ATTENTION
) &&
2073 (SensePtr
->Addnl_Sense_Code
== EFI_SCSI_ASC_MEDIA_CHANGE
)) {
2074 IsMediaChanged
= TRUE
;
2080 return IsMediaChanged
;
2084 Check sense key to find if reset happens.
2086 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2087 @param SenseCounts The number of sense key
2089 @retval TRUE It is reset before.
2090 @retval FALSE It is NOT reset before.
2094 ScsiDiskIsResetBefore (
2095 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2096 IN UINTN SenseCounts
2099 EFI_SCSI_SENSE_DATA
*SensePtr
;
2101 BOOLEAN IsResetBefore
;
2103 IsResetBefore
= FALSE
;
2104 SensePtr
= SenseData
;
2106 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2109 // Sense Key is EFI_SCSI_SK_UNIT_ATTENTION (0x6)
2110 // Additional Sense Code is EFI_SCSI_ASC_RESET (0x29)
2112 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_UNIT_ATTENTION
) &&
2113 (SensePtr
->Addnl_Sense_Code
== EFI_SCSI_ASC_RESET
)) {
2114 IsResetBefore
= TRUE
;
2120 return IsResetBefore
;
2124 Check sense key to find if the drive is ready.
2126 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2127 @param SenseCounts The number of sense key
2128 @param RetryLater The flag means if need a retry
2130 @retval TRUE Drive is ready.
2131 @retval FALSE Drive is NOT ready.
2135 ScsiDiskIsDriveReady (
2136 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2137 IN UINTN SenseCounts
,
2138 OUT BOOLEAN
*RetryLater
2141 EFI_SCSI_SENSE_DATA
*SensePtr
;
2146 *RetryLater
= FALSE
;
2147 SensePtr
= SenseData
;
2149 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2151 switch (SensePtr
->Sense_Key
) {
2153 case EFI_SCSI_SK_NOT_READY
:
2155 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2)
2157 switch (SensePtr
->Addnl_Sense_Code
) {
2158 case EFI_SCSI_ASC_NOT_READY
:
2160 // Additional Sense Code is EFI_SCSI_ASC_NOT_READY (0x4)
2162 switch (SensePtr
->Addnl_Sense_Code_Qualifier
) {
2163 case EFI_SCSI_ASCQ_IN_PROGRESS
:
2165 // Additional Sense Code Qualifier is
2166 // EFI_SCSI_ASCQ_IN_PROGRESS (0x1)
2174 *RetryLater
= FALSE
;
2195 Check sense key to find if it has sense key.
2197 @param SenseData - The pointer of EFI_SCSI_SENSE_DATA
2198 @param SenseCounts - The number of sense key
2200 @retval TRUE It has sense key.
2201 @retval FALSE It has NOT any sense key.
2205 ScsiDiskHaveSenseKey (
2206 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2207 IN UINTN SenseCounts
2210 EFI_SCSI_SENSE_DATA
*SensePtr
;
2212 BOOLEAN HaveSenseKey
;
2214 if (SenseCounts
== 0) {
2215 HaveSenseKey
= FALSE
;
2217 HaveSenseKey
= TRUE
;
2220 SensePtr
= SenseData
;
2222 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2225 // Sense Key is SK_NO_SENSE (0x0)
2227 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_NO_SENSE
) &&
2229 HaveSenseKey
= FALSE
;
2235 return HaveSenseKey
;
2239 Release resource about disk device.
2241 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
2245 ReleaseScsiDiskDeviceResources (
2246 IN SCSI_DISK_DEV
*ScsiDiskDevice
2249 if (ScsiDiskDevice
== NULL
) {
2253 if (ScsiDiskDevice
->SenseData
!= NULL
) {
2254 FreePool (ScsiDiskDevice
->SenseData
);
2255 ScsiDiskDevice
->SenseData
= NULL
;
2258 if (ScsiDiskDevice
->ControllerNameTable
!= NULL
) {
2259 FreeUnicodeStringTable (ScsiDiskDevice
->ControllerNameTable
);
2260 ScsiDiskDevice
->ControllerNameTable
= NULL
;
2263 FreePool (ScsiDiskDevice
);
2265 ScsiDiskDevice
= NULL
;
2269 Determine if Block Io should be produced.
2272 @param ChildHandle Child Handle to retrieve Parent information.
2274 @retval TRUE Should produce Block Io.
2275 @retval FALSE Should not produce Block Io.
2279 DetermineInstallBlockIo (
2280 IN EFI_HANDLE ChildHandle
2283 EFI_SCSI_PASS_THRU_PROTOCOL
*ScsiPassThru
;
2284 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtScsiPassThru
;
2287 // Firstly, check if ExtScsiPassThru Protocol parent handle exists. If existence,
2288 // check its attribute, logic or physical.
2290 ExtScsiPassThru
= (EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*)GetParentProtocol (&gEfiExtScsiPassThruProtocolGuid
, ChildHandle
);
2291 if (ExtScsiPassThru
!= NULL
) {
2292 if ((ExtScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
) != 0) {
2298 // Secondly, check if ScsiPassThru Protocol parent handle exists. If existence,
2299 // check its attribute, logic or physical.
2301 ScsiPassThru
= (EFI_SCSI_PASS_THRU_PROTOCOL
*)GetParentProtocol (&gEfiScsiPassThruProtocolGuid
, ChildHandle
);
2302 if (ScsiPassThru
!= NULL
) {
2303 if ((ScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
) != 0) {
2312 Search protocol database and check to see if the protocol
2313 specified by ProtocolGuid is present on a ControllerHandle and opened by
2314 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
2315 If the ControllerHandle is found, then the protocol specified by ProtocolGuid
2316 will be opened on it.
2319 @param ProtocolGuid ProtocolGuid pointer.
2320 @param ChildHandle Child Handle to retrieve Parent information.
2326 IN EFI_GUID
*ProtocolGuid
,
2327 IN EFI_HANDLE ChildHandle
2334 EFI_HANDLE
*HandleBuffer
;
2337 // Retrieve the list of all handles from the handle database
2339 Status
= gBS
->LocateHandleBuffer (
2347 if (EFI_ERROR (Status
)) {
2352 // Iterate to find who is parent handle that is opened with ProtocolGuid by ChildHandle
2354 for (Index
= 0; Index
< HandleCount
; Index
++) {
2355 Status
= EfiTestChildHandle (HandleBuffer
[Index
], ChildHandle
, ProtocolGuid
);
2356 if (!EFI_ERROR (Status
)) {
2357 Status
= gBS
->HandleProtocol (HandleBuffer
[Index
], ProtocolGuid
, (VOID
**)&Interface
);
2358 if (!EFI_ERROR (Status
)) {
2359 gBS
->FreePool (HandleBuffer
);
2365 gBS
->FreePool (HandleBuffer
);
2370 Provides inquiry information for the controller type.
2372 This function is used by the IDE bus driver to get inquiry data. Data format
2373 of Identify data is defined by the Interface GUID.
2375 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
2376 @param[in, out] InquiryData Pointer to a buffer for the inquiry data.
2377 @param[in, out] InquiryDataSize Pointer to the value for the inquiry data size.
2379 @retval EFI_SUCCESS The command was accepted without any errors.
2380 @retval EFI_NOT_FOUND Device does not support this data class
2381 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
2382 @retval EFI_BUFFER_TOO_SMALL InquiryDataSize not big enough
2387 ScsiDiskInfoInquiry (
2388 IN EFI_DISK_INFO_PROTOCOL
*This
,
2389 IN OUT VOID
*InquiryData
,
2390 IN OUT UINT32
*InquiryDataSize
2394 SCSI_DISK_DEV
*ScsiDiskDevice
;
2396 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_DISKINFO (This
);
2398 Status
= EFI_BUFFER_TOO_SMALL
;
2399 if (*InquiryDataSize
>= sizeof (ScsiDiskDevice
->InquiryData
)) {
2400 Status
= EFI_SUCCESS
;
2401 CopyMem (InquiryData
, &ScsiDiskDevice
->InquiryData
, sizeof (ScsiDiskDevice
->InquiryData
));
2403 *InquiryDataSize
= sizeof (ScsiDiskDevice
->InquiryData
);
2409 Provides identify information for the controller type.
2411 This function is used by the IDE bus driver to get identify data. Data format
2412 of Identify data is defined by the Interface GUID.
2414 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL
2416 @param[in, out] IdentifyData Pointer to a buffer for the identify data.
2417 @param[in, out] IdentifyDataSize Pointer to the value for the identify data
2420 @retval EFI_SUCCESS The command was accepted without any errors.
2421 @retval EFI_NOT_FOUND Device does not support this data class
2422 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
2423 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
2428 ScsiDiskInfoIdentify (
2429 IN EFI_DISK_INFO_PROTOCOL
*This
,
2430 IN OUT VOID
*IdentifyData
,
2431 IN OUT UINT32
*IdentifyDataSize
2435 SCSI_DISK_DEV
*ScsiDiskDevice
;
2437 if (CompareGuid (&This
->Interface
, &gEfiDiskInfoScsiInterfaceGuid
)) {
2439 // Physical SCSI bus does not support this data class.
2441 return EFI_NOT_FOUND
;
2444 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_DISKINFO (This
);
2446 Status
= EFI_BUFFER_TOO_SMALL
;
2447 if (*IdentifyDataSize
>= sizeof (ScsiDiskDevice
->IdentifyData
)) {
2448 Status
= EFI_SUCCESS
;
2449 CopyMem (IdentifyData
, &ScsiDiskDevice
->IdentifyData
, sizeof (ScsiDiskDevice
->IdentifyData
));
2451 *IdentifyDataSize
= sizeof (ScsiDiskDevice
->IdentifyData
);
2456 Provides sense data information for the controller type.
2458 This function is used by the IDE bus driver to get sense data.
2459 Data format of Sense data is defined by the Interface GUID.
2461 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
2462 @param[in, out] SenseData Pointer to the SenseData.
2463 @param[in, out] SenseDataSize Size of SenseData in bytes.
2464 @param[out] SenseDataNumber Pointer to the value for the sense data size.
2466 @retval EFI_SUCCESS The command was accepted without any errors.
2467 @retval EFI_NOT_FOUND Device does not support this data class.
2468 @retval EFI_DEVICE_ERROR Error reading SenseData from device.
2469 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough.
2474 ScsiDiskInfoSenseData (
2475 IN EFI_DISK_INFO_PROTOCOL
*This
,
2476 IN OUT VOID
*SenseData
,
2477 IN OUT UINT32
*SenseDataSize
,
2478 OUT UINT8
*SenseDataNumber
2481 return EFI_NOT_FOUND
;
2486 This function is used by the IDE bus driver to get controller information.
2488 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
2489 @param[out] IdeChannel Pointer to the Ide Channel number. Primary or secondary.
2490 @param[out] IdeDevice Pointer to the Ide Device number. Master or slave.
2492 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid.
2493 @retval EFI_UNSUPPORTED This is not an IDE device.
2498 ScsiDiskInfoWhichIde (
2499 IN EFI_DISK_INFO_PROTOCOL
*This
,
2500 OUT UINT32
*IdeChannel
,
2501 OUT UINT32
*IdeDevice
2504 SCSI_DISK_DEV
*ScsiDiskDevice
;
2506 if (CompareGuid (&This
->Interface
, &gEfiDiskInfoScsiInterfaceGuid
)) {
2508 // This is not an IDE physical device.
2510 return EFI_UNSUPPORTED
;
2513 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_DISKINFO (This
);
2514 *IdeChannel
= ScsiDiskDevice
->Channel
;
2515 *IdeDevice
= ScsiDiskDevice
->Device
;
2522 Issues ATA IDENTIFY DEVICE command to identify ATAPI device.
2524 This function tries to fill 512-byte ATAPI_IDENTIFY_DATA for ATAPI device to
2525 implement Identify() interface for DiskInfo protocol. The ATA command is sent
2526 via SCSI Request Packet.
2528 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
2530 @retval EFI_SUCCESS The ATAPI device identify data were retrieved successfully.
2531 @retval others Some error occurred during the identification that ATAPI device.
2535 AtapiIdentifyDevice (
2536 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
2539 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket
;
2543 // Initialize SCSI REQUEST_PACKET and 6-byte Cdb
2545 ZeroMem (&CommandPacket
, sizeof (CommandPacket
));
2546 ZeroMem (Cdb
, sizeof (Cdb
));
2548 Cdb
[0] = ATA_CMD_IDENTIFY_DEVICE
;
2549 CommandPacket
.Timeout
= EFI_TIMER_PERIOD_SECONDS (1);
2550 CommandPacket
.Cdb
= Cdb
;
2551 CommandPacket
.CdbLength
= sizeof (Cdb
);
2552 CommandPacket
.InDataBuffer
= &ScsiDiskDevice
->IdentifyData
;
2553 CommandPacket
.InTransferLength
= sizeof (ScsiDiskDevice
->IdentifyData
);
2555 return ScsiDiskDevice
->ScsiIo
->ExecuteScsiCommand (ScsiDiskDevice
->ScsiIo
, &CommandPacket
, NULL
);
2560 Initialize the installation of DiskInfo protocol.
2562 This function prepares for the installation of DiskInfo protocol on the child handle.
2563 By default, it installs DiskInfo protocol with SCSI interface GUID. If it further
2564 detects that the physical device is an ATAPI/AHCI device, it then updates interface GUID
2565 to be IDE/AHCI interface GUID.
2567 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV.
2568 @param ChildHandle Child handle to install DiskInfo protocol.
2572 InitializeInstallDiskInfo (
2573 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
2574 IN EFI_HANDLE ChildHandle
2578 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
2579 EFI_DEVICE_PATH_PROTOCOL
*ChildDevicePathNode
;
2580 ATAPI_DEVICE_PATH
*AtapiDevicePath
;
2581 SATA_DEVICE_PATH
*SataDevicePath
;
2582 UINTN IdentifyRetry
;
2584 Status
= gBS
->HandleProtocol (ChildHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**) &DevicePathNode
);
2586 // Device Path protocol must be installed on the device handle.
2588 ASSERT_EFI_ERROR (Status
);
2590 // Copy the DiskInfo protocol template.
2592 CopyMem (&ScsiDiskDevice
->DiskInfo
, &gScsiDiskInfoProtocolTemplate
, sizeof (gScsiDiskInfoProtocolTemplate
));
2594 while (!IsDevicePathEnd (DevicePathNode
)) {
2595 ChildDevicePathNode
= NextDevicePathNode (DevicePathNode
);
2596 if ((DevicePathType (DevicePathNode
) == HARDWARE_DEVICE_PATH
) &&
2597 (DevicePathSubType (DevicePathNode
) == HW_PCI_DP
) &&
2598 (DevicePathType (ChildDevicePathNode
) == MESSAGING_DEVICE_PATH
) &&
2599 ((DevicePathSubType (ChildDevicePathNode
) == MSG_ATAPI_DP
) ||
2600 (DevicePathSubType (ChildDevicePathNode
) == MSG_SATA_DP
))) {
2605 // Issue ATA Identify Device Command via SCSI command, which is required to publish DiskInfo protocol
2606 // with IDE/AHCI interface GUID.
2608 Status
= AtapiIdentifyDevice (ScsiDiskDevice
);
2609 if (!EFI_ERROR (Status
)) {
2610 if (DevicePathSubType(ChildDevicePathNode
) == MSG_ATAPI_DP
) {
2612 // We find the valid ATAPI device path
2614 AtapiDevicePath
= (ATAPI_DEVICE_PATH
*) ChildDevicePathNode
;
2615 ScsiDiskDevice
->Channel
= AtapiDevicePath
->PrimarySecondary
;
2616 ScsiDiskDevice
->Device
= AtapiDevicePath
->SlaveMaster
;
2618 // Update the DiskInfo.Interface to IDE interface GUID for the physical ATAPI device.
2620 CopyGuid (&ScsiDiskDevice
->DiskInfo
.Interface
, &gEfiDiskInfoIdeInterfaceGuid
);
2623 // We find the valid SATA device path
2625 SataDevicePath
= (SATA_DEVICE_PATH
*) ChildDevicePathNode
;
2626 ScsiDiskDevice
->Channel
= SataDevicePath
->HBAPortNumber
;
2627 ScsiDiskDevice
->Device
= SataDevicePath
->PortMultiplierPortNumber
;
2629 // Update the DiskInfo.Interface to AHCI interface GUID for the physical AHCI device.
2631 CopyGuid (&ScsiDiskDevice
->DiskInfo
.Interface
, &gEfiDiskInfoAhciInterfaceGuid
);
2635 } while (--IdentifyRetry
> 0);
2637 DevicePathNode
= ChildDevicePathNode
;