2 SCSI disk driver that layers on every SCSI IO protocol in the system.
4 Copyright (c) 2006 - 2011, 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
;
169 BOOLEAN MustReadCapacity
;
171 MustReadCapacity
= TRUE
;
173 ScsiDiskDevice
= (SCSI_DISK_DEV
*) AllocateZeroPool (sizeof (SCSI_DISK_DEV
));
174 if (ScsiDiskDevice
== NULL
) {
175 return EFI_OUT_OF_RESOURCES
;
178 Status
= gBS
->OpenProtocol (
180 &gEfiScsiIoProtocolGuid
,
182 This
->DriverBindingHandle
,
184 EFI_OPEN_PROTOCOL_BY_DRIVER
186 if (EFI_ERROR (Status
)) {
187 FreePool (ScsiDiskDevice
);
191 ScsiDiskDevice
->Signature
= SCSI_DISK_DEV_SIGNATURE
;
192 ScsiDiskDevice
->ScsiIo
= ScsiIo
;
193 ScsiDiskDevice
->BlkIo
.Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
194 ScsiDiskDevice
->BlkIo
.Media
= &ScsiDiskDevice
->BlkIoMedia
;
195 ScsiDiskDevice
->BlkIo
.Reset
= ScsiDiskReset
;
196 ScsiDiskDevice
->BlkIo
.ReadBlocks
= ScsiDiskReadBlocks
;
197 ScsiDiskDevice
->BlkIo
.WriteBlocks
= ScsiDiskWriteBlocks
;
198 ScsiDiskDevice
->BlkIo
.FlushBlocks
= ScsiDiskFlushBlocks
;
199 ScsiDiskDevice
->Handle
= Controller
;
201 ScsiIo
->GetDeviceType (ScsiIo
, &(ScsiDiskDevice
->DeviceType
));
202 switch (ScsiDiskDevice
->DeviceType
) {
203 case EFI_SCSI_TYPE_DISK
:
204 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x200;
205 MustReadCapacity
= TRUE
;
208 case EFI_SCSI_TYPE_CDROM
:
209 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x800;
210 MustReadCapacity
= FALSE
;
214 // The Sense Data Array's initial size is 6
216 ScsiDiskDevice
->SenseDataNumber
= 6;
217 ScsiDiskDevice
->SenseData
= (EFI_SCSI_SENSE_DATA
*) AllocateZeroPool (
218 sizeof (EFI_SCSI_SENSE_DATA
) * ScsiDiskDevice
->SenseDataNumber
220 if (ScsiDiskDevice
->SenseData
== NULL
) {
223 &gEfiScsiIoProtocolGuid
,
224 This
->DriverBindingHandle
,
227 FreePool (ScsiDiskDevice
);
228 return EFI_OUT_OF_RESOURCES
;
232 // Retrieve device information
235 for (Index
= 0; Index
< MaxRetry
; Index
++) {
236 Status
= ScsiDiskInquiryDevice (ScsiDiskDevice
, &NeedRetry
);
237 if (!EFI_ERROR (Status
)) {
242 FreePool (ScsiDiskDevice
->SenseData
);
245 &gEfiScsiIoProtocolGuid
,
246 This
->DriverBindingHandle
,
249 FreePool (ScsiDiskDevice
);
250 return EFI_DEVICE_ERROR
;
254 // The second parameter "TRUE" means must
255 // retrieve media capacity
257 Status
= ScsiDiskDetectMedia (ScsiDiskDevice
, MustReadCapacity
, &Temp
);
258 if (!EFI_ERROR (Status
)) {
260 // Determine if Block IO should be produced on this controller handle
262 if (DetermineInstallBlockIo(Controller
)) {
263 InitializeInstallDiskInfo(ScsiDiskDevice
, Controller
);
264 Status
= gBS
->InstallMultipleProtocolInterfaces (
266 &gEfiBlockIoProtocolGuid
,
267 &ScsiDiskDevice
->BlkIo
,
268 &gEfiDiskInfoProtocolGuid
,
269 &ScsiDiskDevice
->DiskInfo
,
272 if (!EFI_ERROR(Status
)) {
273 ScsiDiskDevice
->ControllerNameTable
= NULL
;
276 gScsiDiskComponentName
.SupportedLanguages
,
277 &ScsiDiskDevice
->ControllerNameTable
,
283 gScsiDiskComponentName2
.SupportedLanguages
,
284 &ScsiDiskDevice
->ControllerNameTable
,
293 gBS
->FreePool (ScsiDiskDevice
->SenseData
);
294 gBS
->FreePool (ScsiDiskDevice
);
297 &gEfiScsiIoProtocolGuid
,
298 This
->DriverBindingHandle
,
307 Stop this driver on ControllerHandle.
309 This service is called by the EFI boot service DisconnectController().
310 In order to make drivers as small as possible, there are a few calling
311 restrictions for this service. DisconnectController() must follow these
312 calling restrictions. If any other agent wishes to call Stop() it must
313 also follow these calling restrictions.
315 @param This Protocol instance pointer.
316 @param ControllerHandle Handle of device to stop driver on
317 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
318 children is zero stop the entire bus driver.
319 @param ChildHandleBuffer List of Child Handles to Stop.
321 @retval EFI_SUCCESS This driver is removed ControllerHandle
322 @retval other This driver was not removed from this device
327 ScsiDiskDriverBindingStop (
328 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
329 IN EFI_HANDLE Controller
,
330 IN UINTN NumberOfChildren
,
331 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
334 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
335 SCSI_DISK_DEV
*ScsiDiskDevice
;
338 Status
= gBS
->OpenProtocol (
340 &gEfiBlockIoProtocolGuid
,
342 This
->DriverBindingHandle
,
344 EFI_OPEN_PROTOCOL_GET_PROTOCOL
346 if (EFI_ERROR (Status
)) {
350 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (BlkIo
);
351 Status
= gBS
->UninstallMultipleProtocolInterfaces (
353 &gEfiBlockIoProtocolGuid
,
354 &ScsiDiskDevice
->BlkIo
,
355 &gEfiDiskInfoProtocolGuid
,
356 &ScsiDiskDevice
->DiskInfo
,
359 if (!EFI_ERROR (Status
)) {
362 &gEfiScsiIoProtocolGuid
,
363 This
->DriverBindingHandle
,
367 ReleaseScsiDiskDeviceResources (ScsiDiskDevice
);
381 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
382 @param ExtendedVerification The flag about if extend verificate
384 @retval EFI_SUCCESS The device was reset.
385 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
387 @return EFI_STATUS is returned from EFI_SCSI_IO_PROTOCOL.ResetDevice().
393 IN EFI_BLOCK_IO_PROTOCOL
*This
,
394 IN BOOLEAN ExtendedVerification
398 SCSI_DISK_DEV
*ScsiDiskDevice
;
401 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
403 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (This
);
405 Status
= ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
407 if (EFI_ERROR (Status
)) {
408 Status
= EFI_DEVICE_ERROR
;
412 if (!ExtendedVerification
) {
416 Status
= ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
418 if (EFI_ERROR (Status
)) {
419 Status
= EFI_DEVICE_ERROR
;
424 gBS
->RestoreTPL (OldTpl
);
429 The function is to Read Block from SCSI Disk.
431 @param This The pointer of EFI_BLOCK_IO_PROTOCOL.
432 @param MediaId The Id of Media detected
433 @param Lba The logic block address
434 @param BufferSize The size of Buffer
435 @param Buffer The buffer to fill the read out data
437 @retval EFI_SUCCESS Successfully to read out block.
438 @retval EFI_DEVICE_ERROR Fail to detect media.
439 @retval EFI_NO_MEDIA Media is not present.
440 @retval EFI_MEDIA_CHANGED Media has changed.
441 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
442 @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
448 IN EFI_BLOCK_IO_PROTOCOL
*This
,
455 SCSI_DISK_DEV
*ScsiDiskDevice
;
456 EFI_BLOCK_IO_MEDIA
*Media
;
459 UINTN NumberOfBlocks
;
464 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
465 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (This
);
467 if (!IS_DEVICE_FIXED(ScsiDiskDevice
)) {
469 Status
= ScsiDiskDetectMedia (ScsiDiskDevice
, FALSE
, &MediaChange
);
470 if (EFI_ERROR (Status
)) {
471 Status
= EFI_DEVICE_ERROR
;
476 gBS
->ReinstallProtocolInterface (
477 ScsiDiskDevice
->Handle
,
478 &gEfiBlockIoProtocolGuid
,
479 &ScsiDiskDevice
->BlkIo
,
480 &ScsiDiskDevice
->BlkIo
485 // Get the intrinsic block size
487 Media
= ScsiDiskDevice
->BlkIo
.Media
;
488 BlockSize
= Media
->BlockSize
;
490 NumberOfBlocks
= BufferSize
/ BlockSize
;
492 if (!(Media
->MediaPresent
)) {
493 Status
= EFI_NO_MEDIA
;
497 if (MediaId
!= Media
->MediaId
) {
498 Status
= EFI_MEDIA_CHANGED
;
502 if (Buffer
== NULL
) {
503 Status
= EFI_INVALID_PARAMETER
;
507 if (BufferSize
== 0) {
508 Status
= EFI_SUCCESS
;
512 if (BufferSize
% BlockSize
!= 0) {
513 Status
= EFI_BAD_BUFFER_SIZE
;
517 if (Lba
> Media
->LastBlock
) {
518 Status
= EFI_INVALID_PARAMETER
;
522 if ((Lba
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
523 Status
= EFI_INVALID_PARAMETER
;
527 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
528 Status
= EFI_INVALID_PARAMETER
;
533 // If all the parameters are valid, then perform read sectors command
534 // to transfer data from device to host.
536 Status
= ScsiDiskReadSectors (ScsiDiskDevice
, Buffer
, Lba
, NumberOfBlocks
);
539 gBS
->RestoreTPL (OldTpl
);
544 The function is to Write Block to SCSI Disk.
546 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
547 @param MediaId The Id of Media detected
548 @param Lba The logic block address
549 @param BufferSize The size of Buffer
550 @param Buffer The buffer to fill the read out data
552 @retval EFI_SUCCESS Successfully to read out block.
553 @retval EFI_WRITE_PROTECTED The device can not be written to.
554 @retval EFI_DEVICE_ERROR Fail to detect media.
555 @retval EFI_NO_MEDIA Media is not present.
556 @retval EFI_MEDIA_CHNAGED Media has changed.
557 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
558 @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
563 ScsiDiskWriteBlocks (
564 IN EFI_BLOCK_IO_PROTOCOL
*This
,
571 SCSI_DISK_DEV
*ScsiDiskDevice
;
572 EFI_BLOCK_IO_MEDIA
*Media
;
575 UINTN NumberOfBlocks
;
580 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
581 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_THIS (This
);
583 if (!IS_DEVICE_FIXED(ScsiDiskDevice
)) {
585 Status
= ScsiDiskDetectMedia (ScsiDiskDevice
, FALSE
, &MediaChange
);
586 if (EFI_ERROR (Status
)) {
587 Status
= EFI_DEVICE_ERROR
;
592 gBS
->ReinstallProtocolInterface (
593 ScsiDiskDevice
->Handle
,
594 &gEfiBlockIoProtocolGuid
,
595 &ScsiDiskDevice
->BlkIo
,
596 &ScsiDiskDevice
->BlkIo
601 // Get the intrinsic block size
603 Media
= ScsiDiskDevice
->BlkIo
.Media
;
604 BlockSize
= Media
->BlockSize
;
606 NumberOfBlocks
= BufferSize
/ BlockSize
;
608 if (!(Media
->MediaPresent
)) {
609 Status
= EFI_NO_MEDIA
;
613 if (MediaId
!= Media
->MediaId
) {
614 Status
= EFI_MEDIA_CHANGED
;
618 if (BufferSize
== 0) {
619 Status
= EFI_SUCCESS
;
623 if (Buffer
== NULL
) {
624 Status
= EFI_INVALID_PARAMETER
;
628 if (BufferSize
% BlockSize
!= 0) {
629 Status
= EFI_BAD_BUFFER_SIZE
;
633 if (Lba
> Media
->LastBlock
) {
634 Status
= EFI_INVALID_PARAMETER
;
638 if ((Lba
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
639 Status
= EFI_INVALID_PARAMETER
;
643 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
644 Status
= EFI_INVALID_PARAMETER
;
648 // if all the parameters are valid, then perform read sectors command
649 // to transfer data from device to host.
651 Status
= ScsiDiskWriteSectors (ScsiDiskDevice
, Buffer
, Lba
, NumberOfBlocks
);
654 gBS
->RestoreTPL (OldTpl
);
661 EFI_SUCCESS is returned directly.
663 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
665 @retval EFI_SUCCESS All outstanding data was written to the device
670 ScsiDiskFlushBlocks (
671 IN EFI_BLOCK_IO_PROTOCOL
*This
682 Detect Device and read out capacity ,if error occurs, parse the sense key.
684 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
685 @param MustReadCapacity The flag about reading device capacity
686 @param MediaChange The pointer of flag indicates if media has changed
688 @retval EFI_DEVICE_ERROR Indicates that error occurs
689 @retval EFI_SUCCESS Successfully to detect media
693 ScsiDiskDetectMedia (
694 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
695 IN BOOLEAN MustReadCapacity
,
696 OUT BOOLEAN
*MediaChange
700 EFI_SCSI_SENSE_DATA
*SenseData
;
701 UINTN NumberOfSenseKeys
;
703 BOOLEAN NeedReadCapacity
;
706 EFI_BLOCK_IO_MEDIA OldMedia
;
708 EFI_EVENT TimeoutEvt
;
710 Status
= EFI_SUCCESS
;
712 NumberOfSenseKeys
= 0;
715 Action
= ACTION_NO_ACTION
;
716 NeedReadCapacity
= FALSE
;
717 *MediaChange
= FALSE
;
720 CopyMem (&OldMedia
, ScsiDiskDevice
->BlkIo
.Media
, sizeof (OldMedia
));
722 Status
= gBS
->CreateEvent (
729 if (EFI_ERROR (Status
)) {
733 Status
= gBS
->SetTimer (TimeoutEvt
, TimerRelative
, EFI_TIMER_PERIOD_SECONDS(120));
734 if (EFI_ERROR (Status
)) {
739 // Sending Test_Unit cmd to poll device status.
740 // If the sense data shows the drive is not ready or reset before, we need poll the device status again.
741 // We limit the upper boundary to 120 seconds.
743 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvt
))) {
744 Status
= ScsiDiskTestUnitReady (
750 if (!EFI_ERROR (Status
)) {
751 Status
= DetectMediaParsingSenseKeys (
757 if (EFI_ERROR (Status
)) {
759 } else if (Action
== ACTION_RETRY_COMMAND_LATER
) {
766 if (!NeedRetry
|| (Retry
>= MaxRetry
)) {
772 if (EFI_ERROR (Status
)) {
777 // ACTION_NO_ACTION: need not read capacity
778 // other action code: need read capacity
780 if (Action
== ACTION_READ_CAPACITY
) {
781 NeedReadCapacity
= TRUE
;
785 // either NeedReadCapacity is TRUE, or MustReadCapacity is TRUE,
786 // retrieve capacity via Read Capacity command
788 if (NeedReadCapacity
|| MustReadCapacity
) {
790 // retrieve media information
792 for (Retry
= 0; Retry
< MaxRetry
; Retry
++) {
793 Status
= ScsiDiskReadCapacity (
799 if (!EFI_ERROR (Status
)) {
801 // analyze sense key to action
803 Status
= DetectMediaParsingSenseKeys (
809 if (EFI_ERROR (Status
)) {
811 // if Status is error, it may indicate crisis error,
812 // so return without retry.
815 } else if (Action
== ACTION_RETRY_COMMAND_LATER
) {
823 if (!NeedRetry
|| (Retry
>= MaxRetry
)) {
829 if (EFI_ERROR (Status
)) {
834 if (ScsiDiskDevice
->BlkIo
.Media
->MediaId
!= OldMedia
.MediaId
) {
836 // Media change information got from the device
841 if (ScsiDiskDevice
->BlkIo
.Media
->ReadOnly
!= OldMedia
.ReadOnly
) {
843 ScsiDiskDevice
->BlkIo
.Media
->MediaId
+= 1;
846 if (ScsiDiskDevice
->BlkIo
.Media
->BlockSize
!= OldMedia
.BlockSize
) {
848 ScsiDiskDevice
->BlkIo
.Media
->MediaId
+= 1;
851 if (ScsiDiskDevice
->BlkIo
.Media
->LastBlock
!= OldMedia
.LastBlock
) {
853 ScsiDiskDevice
->BlkIo
.Media
->MediaId
+= 1;
856 if (ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
!= OldMedia
.MediaPresent
) {
857 if (ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
) {
859 // when change from no media to media present, reset the MediaId to 1.
861 ScsiDiskDevice
->BlkIo
.Media
->MediaId
= 1;
864 // when no media, reset the MediaId to zero.
866 ScsiDiskDevice
->BlkIo
.Media
->MediaId
= 0;
873 if (TimeoutEvt
!= NULL
) {
874 gBS
->CloseEvent (TimeoutEvt
);
881 Send out Inquiry command to Device.
883 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
884 @param NeedRetry Indicates if needs try again when error happens
886 @retval EFI_DEVICE_ERROR Indicates that error occurs
887 @retval EFI_SUCCESS Successfully to detect media
891 ScsiDiskInquiryDevice (
892 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
893 OUT BOOLEAN
*NeedRetry
896 UINT32 InquiryDataLength
;
897 UINT8 SenseDataLength
;
898 UINT8 HostAdapterStatus
;
900 EFI_SCSI_SENSE_DATA
*SenseDataArray
;
901 UINTN NumberOfSenseKeys
;
905 EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE SupportedVpdPages
;
906 EFI_SCSI_BLOCK_LIMITS_VPD_PAGE BlockLimits
;
909 InquiryDataLength
= sizeof (EFI_SCSI_INQUIRY_DATA
);
912 Status
= ScsiInquiryCommand (
913 ScsiDiskDevice
->ScsiIo
,
914 EFI_TIMER_PERIOD_SECONDS (1),
919 (VOID
*) &(ScsiDiskDevice
->InquiryData
),
924 // no need to check HostAdapterStatus and TargetStatus
926 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_WARN_BUFFER_TOO_SMALL
)) {
927 ParseInquiryData (ScsiDiskDevice
);
929 if (ScsiDiskDevice
->DeviceType
== EFI_SCSI_TYPE_DISK
) {
931 // Check whether the device supports Block Limits VPD page (0xB0)
933 ZeroMem (&SupportedVpdPages
, sizeof (SupportedVpdPages
));
934 InquiryDataLength
= sizeof (SupportedVpdPages
);
936 Status
= ScsiInquiryCommandEx (
937 ScsiDiskDevice
->ScsiIo
,
938 EFI_TIMER_PERIOD_SECONDS (1),
943 (VOID
*) &SupportedVpdPages
,
946 EFI_SCSI_PAGE_CODE_SUPPORTED_VPD
948 if (!EFI_ERROR (Status
)) {
949 PageLength
= (SupportedVpdPages
.PageLength2
<< 8)
950 | SupportedVpdPages
.PageLength1
;
951 for (Index
= 0; Index
< PageLength
; Index
++) {
952 if (SupportedVpdPages
.SupportedVpdPageList
[Index
] == EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD
) {
958 // Query the Block Limits VPD page
960 if (Index
< PageLength
) {
961 ZeroMem (&BlockLimits
, sizeof (BlockLimits
));
962 InquiryDataLength
= sizeof (BlockLimits
);
964 Status
= ScsiInquiryCommandEx (
965 ScsiDiskDevice
->ScsiIo
,
966 EFI_TIMER_PERIOD_SECONDS (1),
971 (VOID
*) &BlockLimits
,
974 EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD
976 if (!EFI_ERROR (Status
)) {
977 ScsiDiskDevice
->BlkIo
.Media
->OptimalTransferLengthGranularity
=
978 (BlockLimits
.OptimalTransferLengthGranularity2
<< 8) |
979 BlockLimits
.OptimalTransferLengthGranularity1
;
986 if (!EFI_ERROR (Status
)) {
989 } else if (Status
== EFI_NOT_READY
) {
991 return EFI_DEVICE_ERROR
;
993 } else if ((Status
== EFI_INVALID_PARAMETER
) || (Status
== EFI_UNSUPPORTED
)) {
995 return EFI_DEVICE_ERROR
;
998 // go ahead to check HostAdapterStatus and TargetStatus
999 // (EFI_TIMEOUT, EFI_DEVICE_ERROR)
1002 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
1003 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
1005 return EFI_DEVICE_ERROR
;
1006 } else if (Status
== EFI_DEVICE_ERROR
) {
1008 // reset the scsi channel
1010 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
1012 return EFI_DEVICE_ERROR
;
1015 Status
= CheckTargetStatus (TargetStatus
);
1016 if (Status
== EFI_NOT_READY
) {
1018 // reset the scsi device
1020 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
1022 return EFI_DEVICE_ERROR
;
1024 } else if (Status
== EFI_DEVICE_ERROR
) {
1026 return EFI_DEVICE_ERROR
;
1030 // if goes here, meant ScsiInquiryCommand() failed.
1031 // if ScsiDiskRequestSenseKeys() succeeds at last,
1032 // better retry ScsiInquiryCommand(). (by setting *NeedRetry = TRUE)
1035 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1036 Status
= ScsiDiskRequestSenseKeys (
1043 if (!EFI_ERROR (Status
)) {
1045 return EFI_DEVICE_ERROR
;
1049 return EFI_DEVICE_ERROR
;
1053 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
1054 // set *NeedRetry = FALSE to avoid the outside caller try again.
1057 return EFI_DEVICE_ERROR
;
1063 When Test Unit Ready command succeeds, retrieve Sense Keys via Request Sense;
1064 When Test Unit Ready command encounters any error caused by host adapter or
1065 target, return error without retrieving Sense Keys.
1067 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1068 @param NeedRetry The pointer of flag indicates try again
1069 @param SenseDataArray The pointer of an array of sense data
1070 @param NumberOfSenseKeys The pointer of the number of sense data array
1072 @retval EFI_DEVICE_ERROR Indicates that error occurs
1073 @retval EFI_SUCCESS Successfully to test unit
1077 ScsiDiskTestUnitReady (
1078 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1079 OUT BOOLEAN
*NeedRetry
,
1080 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
1081 OUT UINTN
*NumberOfSenseKeys
1085 UINT8 SenseDataLength
;
1086 UINT8 HostAdapterStatus
;
1091 SenseDataLength
= 0;
1092 *NumberOfSenseKeys
= 0;
1095 // Parameter 3 and 4: do not require sense data, retrieve it when needed.
1097 Status
= ScsiTestUnitReadyCommand (
1098 ScsiDiskDevice
->ScsiIo
,
1099 EFI_TIMER_PERIOD_SECONDS (1),
1106 // no need to check HostAdapterStatus and TargetStatus
1108 if (Status
== EFI_NOT_READY
) {
1110 return EFI_DEVICE_ERROR
;
1112 } else if ((Status
== EFI_INVALID_PARAMETER
) || (Status
== EFI_UNSUPPORTED
)) {
1114 return EFI_DEVICE_ERROR
;
1117 // go ahead to check HostAdapterStatus and TargetStatus(in case of EFI_DEVICE_ERROR)
1120 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
1121 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
1123 return EFI_DEVICE_ERROR
;
1125 } else if (Status
== EFI_DEVICE_ERROR
) {
1127 // reset the scsi channel
1129 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
1131 return EFI_DEVICE_ERROR
;
1134 Status
= CheckTargetStatus (TargetStatus
);
1135 if (Status
== EFI_NOT_READY
) {
1137 // reset the scsi device
1139 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
1141 return EFI_DEVICE_ERROR
;
1143 } else if (Status
== EFI_DEVICE_ERROR
) {
1145 return EFI_DEVICE_ERROR
;
1149 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1150 Status
= ScsiDiskRequestSenseKeys (
1157 if (!EFI_ERROR (Status
)) {
1162 return EFI_DEVICE_ERROR
;
1166 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
1167 // set *NeedRetry = FALSE to avoid the outside caller try again.
1170 return EFI_DEVICE_ERROR
;
1174 Parsing Sense Keys which got from request sense command.
1176 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1177 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
1178 @param NumberOfSenseKeys The number of sense key
1179 @param Action The pointer of action which indicates what is need to do next
1181 @retval EFI_DEVICE_ERROR Indicates that error occurs
1182 @retval EFI_SUCCESS Successfully to complete the parsing
1186 DetectMediaParsingSenseKeys (
1187 OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1188 IN EFI_SCSI_SENSE_DATA
*SenseData
,
1189 IN UINTN NumberOfSenseKeys
,
1196 // Default is to read capacity, unless..
1198 *Action
= ACTION_READ_CAPACITY
;
1200 if (NumberOfSenseKeys
== 0) {
1201 if (ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
== TRUE
) {
1202 *Action
= ACTION_NO_ACTION
;
1207 if (!ScsiDiskHaveSenseKey (SenseData
, NumberOfSenseKeys
)) {
1209 // No Sense Key returned from last submitted command
1211 if (ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
== TRUE
) {
1212 *Action
= ACTION_NO_ACTION
;
1217 if (ScsiDiskIsNoMedia (SenseData
, NumberOfSenseKeys
)) {
1218 ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
= FALSE
;
1219 ScsiDiskDevice
->BlkIo
.Media
->LastBlock
= 0;
1220 *Action
= ACTION_NO_ACTION
;
1224 if (ScsiDiskIsMediaChange (SenseData
, NumberOfSenseKeys
)) {
1225 ScsiDiskDevice
->BlkIo
.Media
->MediaId
++;
1229 if (ScsiDiskIsResetBefore (SenseData
, NumberOfSenseKeys
)) {
1230 *Action
= ACTION_RETRY_COMMAND_LATER
;
1234 if (ScsiDiskIsMediaError (SenseData
, NumberOfSenseKeys
)) {
1235 ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
= FALSE
;
1236 ScsiDiskDevice
->BlkIo
.Media
->LastBlock
= 0;
1237 *Action
= ACTION_NO_ACTION
;
1238 return EFI_DEVICE_ERROR
;
1241 if (ScsiDiskIsHardwareError (SenseData
, NumberOfSenseKeys
)) {
1242 *Action
= ACTION_NO_ACTION
;
1243 return EFI_DEVICE_ERROR
;
1246 if (!ScsiDiskIsDriveReady (SenseData
, NumberOfSenseKeys
, &RetryLater
)) {
1248 *Action
= ACTION_RETRY_COMMAND_LATER
;
1251 *Action
= ACTION_NO_ACTION
;
1252 return EFI_DEVICE_ERROR
;
1260 Send read capacity command to device and get the device parameter.
1262 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1263 @param NeedRetry The pointer of flag indicates if need a retry
1264 @param SenseDataArray The pointer of an array of sense data
1265 @param NumberOfSenseKeys The number of sense key
1267 @retval EFI_DEVICE_ERROR Indicates that error occurs
1268 @retval EFI_SUCCESS Successfully to read capacity
1272 ScsiDiskReadCapacity (
1273 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1274 OUT BOOLEAN
*NeedRetry
,
1275 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
1276 OUT UINTN
*NumberOfSenseKeys
1279 UINT8 HostAdapterStatus
;
1281 EFI_STATUS CommandStatus
;
1285 UINT8 SenseDataLength
;
1286 UINT32 DataLength10
;
1287 UINT32 DataLength16
;
1288 EFI_SCSI_DISK_CAPACITY_DATA CapacityData10
;
1289 EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData16
;
1292 SenseDataLength
= 0;
1293 DataLength10
= sizeof (EFI_SCSI_DISK_CAPACITY_DATA
);
1294 DataLength16
= sizeof (EFI_SCSI_DISK_CAPACITY_DATA16
);
1295 ZeroMem (&CapacityData10
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA
));
1296 ZeroMem (&CapacityData16
, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16
));
1298 *NumberOfSenseKeys
= 0;
1302 // submit Read Capacity(10) Command. If it returns capacity of FFFFFFFFh,
1303 // 16 byte command should be used to access large hard disk >2TB
1305 CommandStatus
= ScsiReadCapacityCommand (
1306 ScsiDiskDevice
->ScsiIo
,
1307 EFI_TIMER_PERIOD_SECONDS(1),
1312 (VOID
*) &CapacityData10
,
1317 ScsiDiskDevice
->Cdb16Byte
= FALSE
;
1318 if ((!EFI_ERROR (CommandStatus
)) && (CapacityData10
.LastLba3
== 0xff) && (CapacityData10
.LastLba2
== 0xff) &&
1319 (CapacityData10
.LastLba1
== 0xff) && (CapacityData10
.LastLba0
== 0xff)) {
1321 // use Read Capacity (16), Read (16) and Write (16) next when hard disk size > 2TB
1323 ScsiDiskDevice
->Cdb16Byte
= TRUE
;
1325 // submit Read Capacity(16) Command to get parameter LogicalBlocksPerPhysicalBlock
1326 // and LowestAlignedLba
1328 CommandStatus
= ScsiReadCapacity16Command (
1329 ScsiDiskDevice
->ScsiIo
,
1330 EFI_TIMER_PERIOD_SECONDS (1),
1335 (VOID
*) &CapacityData16
,
1342 // no need to check HostAdapterStatus and TargetStatus
1344 if (CommandStatus
== EFI_SUCCESS
) {
1345 GetMediaInfo (ScsiDiskDevice
, &CapacityData10
,&CapacityData16
);
1348 } else if (CommandStatus
== EFI_NOT_READY
) {
1350 return EFI_DEVICE_ERROR
;
1352 } else if ((CommandStatus
== EFI_INVALID_PARAMETER
) || (CommandStatus
== EFI_UNSUPPORTED
)) {
1354 return EFI_DEVICE_ERROR
;
1357 // go ahead to check HostAdapterStatus and TargetStatus
1358 // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
1361 Status
= CheckHostAdapterStatus (HostAdapterStatus
);
1362 if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
1364 return EFI_DEVICE_ERROR
;
1366 } else if (Status
== EFI_DEVICE_ERROR
) {
1368 // reset the scsi channel
1370 ScsiDiskDevice
->ScsiIo
->ResetBus (ScsiDiskDevice
->ScsiIo
);
1372 return EFI_DEVICE_ERROR
;
1375 Status
= CheckTargetStatus (TargetStatus
);
1376 if (Status
== EFI_NOT_READY
) {
1378 // reset the scsi device
1380 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
1382 return EFI_DEVICE_ERROR
;
1384 } else if (Status
== EFI_DEVICE_ERROR
) {
1386 return EFI_DEVICE_ERROR
;
1390 // if goes here, meant ScsiReadCapacityCommand() failed.
1391 // if ScsiDiskRequestSenseKeys() succeeds at last,
1392 // better retry ScsiReadCapacityCommand(). (by setting *NeedRetry = TRUE)
1395 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1397 Status
= ScsiDiskRequestSenseKeys (
1404 if (!EFI_ERROR (Status
)) {
1406 return EFI_DEVICE_ERROR
;
1410 return EFI_DEVICE_ERROR
;
1414 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
1415 // set *NeedRetry = FALSE to avoid the outside caller try again.
1418 return EFI_DEVICE_ERROR
;
1422 Check the HostAdapter status and re-interpret it in EFI_STATUS.
1424 @param HostAdapterStatus Host Adapter status
1426 @retval EFI_SUCCESS Host adapter is OK.
1427 @retval EFI_TIMEOUT Timeout.
1428 @retval EFI_NOT_READY Adapter NOT ready.
1429 @retval EFI_DEVICE_ERROR Adapter device error.
1433 CheckHostAdapterStatus (
1434 IN UINT8 HostAdapterStatus
1437 switch (HostAdapterStatus
) {
1438 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OK
:
1441 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_SELECTION_TIMEOUT
:
1442 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT
:
1443 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT_COMMAND
:
1446 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_MESSAGE_REJECT
:
1447 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PARITY_ERROR
:
1448 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_REQUEST_SENSE_FAILED
:
1449 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_DATA_OVERRUN_UNDERRUN
:
1450 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_RESET
:
1451 return EFI_NOT_READY
;
1453 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_FREE
:
1454 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR
:
1455 return EFI_DEVICE_ERROR
;
1464 Check the target status and re-interpret it in EFI_STATUS.
1466 @param TargetStatus Target status
1468 @retval EFI_NOT_READY Device is NOT ready.
1469 @retval EFI_DEVICE_ERROR
1475 IN UINT8 TargetStatus
1478 switch (TargetStatus
) {
1479 case EFI_EXT_SCSI_STATUS_TARGET_GOOD
:
1480 case EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION
:
1481 case EFI_EXT_SCSI_STATUS_TARGET_CONDITION_MET
:
1484 case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE
:
1485 case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE_CONDITION_MET
:
1486 case EFI_EXT_SCSI_STATUS_TARGET_BUSY
:
1487 case EFI_EXT_SCSI_STATUS_TARGET_TASK_SET_FULL
:
1488 return EFI_NOT_READY
;
1490 case EFI_EXT_SCSI_STATUS_TARGET_RESERVATION_CONFLICT
:
1491 return EFI_DEVICE_ERROR
;
1501 Retrieve all sense keys from the device.
1503 When encountering error during the process, if retrieve sense keys before
1504 error encountered, it returns the sense keys with return status set to EFI_SUCCESS,
1505 and NeedRetry set to FALSE; otherwize, return the proper return status.
1507 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1508 @param NeedRetry The pointer of flag indicates if need a retry
1509 @param SenseDataArray The pointer of an array of sense data
1510 @param NumberOfSenseKeys The number of sense key
1511 @param AskResetIfError The flag indicates if need reset when error occurs
1513 @retval EFI_DEVICE_ERROR Indicates that error occurs
1514 @retval EFI_SUCCESS Successfully to request sense key
1518 ScsiDiskRequestSenseKeys (
1519 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1520 OUT BOOLEAN
*NeedRetry
,
1521 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
1522 OUT UINTN
*NumberOfSenseKeys
,
1523 IN BOOLEAN AskResetIfError
1526 EFI_SCSI_SENSE_DATA
*PtrSenseData
;
1527 UINT8 SenseDataLength
;
1530 EFI_STATUS FallStatus
;
1531 UINT8 HostAdapterStatus
;
1534 FallStatus
= EFI_SUCCESS
;
1535 SenseDataLength
= (UINT8
) sizeof (EFI_SCSI_SENSE_DATA
);
1538 ScsiDiskDevice
->SenseData
,
1539 sizeof (EFI_SCSI_SENSE_DATA
) * (ScsiDiskDevice
->SenseDataNumber
)
1542 *NumberOfSenseKeys
= 0;
1543 *SenseDataArray
= ScsiDiskDevice
->SenseData
;
1544 PtrSenseData
= ScsiDiskDevice
->SenseData
;
1546 for (SenseReq
= TRUE
; SenseReq
;) {
1547 Status
= ScsiRequestSenseCommand (
1548 ScsiDiskDevice
->ScsiIo
,
1549 EFI_TIMER_PERIOD_SECONDS (2),
1555 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_WARN_BUFFER_TOO_SMALL
)) {
1556 FallStatus
= EFI_SUCCESS
;
1558 } else if ((Status
== EFI_TIMEOUT
) || (Status
== EFI_NOT_READY
)) {
1560 FallStatus
= EFI_DEVICE_ERROR
;
1562 } else if ((Status
== EFI_INVALID_PARAMETER
) || (Status
== EFI_UNSUPPORTED
)) {
1564 FallStatus
= EFI_DEVICE_ERROR
;
1566 } else if (Status
== EFI_DEVICE_ERROR
) {
1567 if (AskResetIfError
) {
1568 ScsiDiskDevice
->ScsiIo
->ResetDevice (ScsiDiskDevice
->ScsiIo
);
1571 FallStatus
= EFI_DEVICE_ERROR
;
1574 if (EFI_ERROR (FallStatus
)) {
1575 if (*NumberOfSenseKeys
!= 0) {
1579 return EFI_DEVICE_ERROR
;
1583 (*NumberOfSenseKeys
) += 1;
1586 // no more sense key or number of sense keys exceeds predefined,
1589 if ((PtrSenseData
->Sense_Key
== EFI_SCSI_SK_NO_SENSE
) ||
1590 (*NumberOfSenseKeys
== ScsiDiskDevice
->SenseDataNumber
)) {
1600 Get information from media read capacity command.
1602 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1603 @param Capacity10 The pointer of EFI_SCSI_DISK_CAPACITY_DATA
1604 @param Capacity16 The pointer of EFI_SCSI_DISK_CAPACITY_DATA16
1609 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
1610 IN EFI_SCSI_DISK_CAPACITY_DATA
*Capacity10
,
1611 IN EFI_SCSI_DISK_CAPACITY_DATA16
*Capacity16
1616 if (!ScsiDiskDevice
->Cdb16Byte
) {
1617 ScsiDiskDevice
->BlkIo
.Media
->LastBlock
= (Capacity10
->LastLba3
<< 24) |
1618 (Capacity10
->LastLba2
<< 16) |
1619 (Capacity10
->LastLba1
<< 8) |
1620 Capacity10
->LastLba0
;
1622 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= (Capacity10
->BlockSize3
<< 24) |
1623 (Capacity10
->BlockSize2
<< 16) |
1624 (Capacity10
->BlockSize1
<< 8) |
1625 Capacity10
->BlockSize0
;
1626 ScsiDiskDevice
->BlkIo
.Media
->LowestAlignedLba
= 0;
1627 ScsiDiskDevice
->BlkIo
.Media
->LogicalBlocksPerPhysicalBlock
= 0;
1629 Ptr
= (UINT8
*)&ScsiDiskDevice
->BlkIo
.Media
->LastBlock
;
1630 *Ptr
++ = Capacity16
->LastLba0
;
1631 *Ptr
++ = Capacity16
->LastLba1
;
1632 *Ptr
++ = Capacity16
->LastLba2
;
1633 *Ptr
++ = Capacity16
->LastLba3
;
1634 *Ptr
++ = Capacity16
->LastLba4
;
1635 *Ptr
++ = Capacity16
->LastLba5
;
1636 *Ptr
++ = Capacity16
->LastLba6
;
1637 *Ptr
= Capacity16
->LastLba7
;
1639 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= (Capacity16
->BlockSize3
<< 24) |
1640 (Capacity16
->BlockSize2
<< 16) |
1641 (Capacity16
->BlockSize1
<< 8) |
1642 Capacity16
->BlockSize0
;
1644 ScsiDiskDevice
->BlkIo
.Media
->LowestAlignedLba
= (Capacity16
->LowestAlignLogic2
<< 8) |
1645 Capacity16
->LowestAlignLogic1
;
1646 ScsiDiskDevice
->BlkIo
.Media
->LogicalBlocksPerPhysicalBlock
= (1 << Capacity16
->LogicPerPhysical
);
1649 ScsiDiskDevice
->BlkIo
.Media
->MediaPresent
= TRUE
;
1651 if (ScsiDiskDevice
->DeviceType
== EFI_SCSI_TYPE_DISK
) {
1652 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x200;
1655 if (ScsiDiskDevice
->DeviceType
== EFI_SCSI_TYPE_CDROM
) {
1656 ScsiDiskDevice
->BlkIo
.Media
->BlockSize
= 0x800;
1663 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1668 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
1671 ScsiDiskDevice
->FixedDevice
= (BOOLEAN
) ((ScsiDiskDevice
->InquiryData
.Rmb
== 1) ? 0 : 1);
1672 ScsiDiskDevice
->BlkIoMedia
.RemovableMedia
= (BOOLEAN
) (!ScsiDiskDevice
->FixedDevice
);
1676 Read sector from SCSI Disk.
1678 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1679 @param Buffer The buffer to fill in the read out data
1680 @param Lba Logic block address
1681 @param NumberOfBlocks The number of blocks to read
1683 @retval EFI_DEVICE_ERROR Indicates a device error.
1684 @retval EFI_SUCCESS Operation is successful.
1688 ScsiDiskReadSectors (
1689 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1692 IN UINTN NumberOfBlocks
1695 UINTN BlocksRemaining
;
1706 EFI_SCSI_SENSE_DATA
*SenseData
;
1707 UINTN NumberOfSenseKeys
;
1710 NumberOfSenseKeys
= 0;
1712 Status
= EFI_SUCCESS
;
1714 BlocksRemaining
= NumberOfBlocks
;
1715 BlockSize
= ScsiDiskDevice
->BlkIo
.Media
->BlockSize
;
1718 // limit the data bytes that can be transferred by one Read(10) or Read(16) Command
1720 if (!ScsiDiskDevice
->Cdb16Byte
) {
1723 MaxBlock
= 0xFFFFFFFF;
1728 while (BlocksRemaining
> 0) {
1730 if (BlocksRemaining
<= MaxBlock
) {
1731 if (!ScsiDiskDevice
->Cdb16Byte
) {
1732 SectorCount
= (UINT16
) BlocksRemaining
;
1734 SectorCount
= (UINT32
) BlocksRemaining
;
1737 SectorCount
= MaxBlock
;
1740 ByteCount
= SectorCount
* BlockSize
;
1741 Timeout
= EFI_TIMER_PERIOD_SECONDS (2);
1744 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1745 if (!ScsiDiskDevice
->Cdb16Byte
) {
1746 Status
= ScsiDiskRead10 (
1758 Status
= ScsiDiskRead16 (
1770 if (!EFI_ERROR (Status
)) {
1775 return EFI_DEVICE_ERROR
;
1780 if ((Index
== MaxRetry
) && (Status
!= EFI_SUCCESS
)) {
1781 return EFI_DEVICE_ERROR
;
1785 // actual transferred sectors
1787 SectorCount
= ByteCount
/ BlockSize
;
1790 PtrBuffer
= PtrBuffer
+ SectorCount
* BlockSize
;
1791 BlocksRemaining
-= SectorCount
;
1798 Write sector to SCSI Disk.
1800 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1801 @param Buffer The buffer of data to be written into SCSI Disk
1802 @param Lba Logic block address
1803 @param NumberOfBlocks The number of blocks to read
1805 @retval EFI_DEVICE_ERROR Indicates a device error.
1806 @retval EFI_SUCCESS Operation is successful.
1810 ScsiDiskWriteSectors (
1811 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1814 IN UINTN NumberOfBlocks
1817 UINTN BlocksRemaining
;
1828 EFI_SCSI_SENSE_DATA
*SenseData
;
1829 UINTN NumberOfSenseKeys
;
1832 NumberOfSenseKeys
= 0;
1834 Status
= EFI_SUCCESS
;
1836 BlocksRemaining
= NumberOfBlocks
;
1837 BlockSize
= ScsiDiskDevice
->BlkIo
.Media
->BlockSize
;
1840 // limit the data bytes that can be transferred by one Read(10) or Read(16) Command
1842 if (!ScsiDiskDevice
->Cdb16Byte
) {
1845 MaxBlock
= 0xFFFFFFFF;
1850 while (BlocksRemaining
> 0) {
1852 if (BlocksRemaining
<= MaxBlock
) {
1853 if (!ScsiDiskDevice
->Cdb16Byte
) {
1854 SectorCount
= (UINT16
) BlocksRemaining
;
1856 SectorCount
= (UINT32
) BlocksRemaining
;
1859 SectorCount
= MaxBlock
;
1862 ByteCount
= SectorCount
* BlockSize
;
1863 Timeout
= EFI_TIMER_PERIOD_SECONDS (2);
1865 for (Index
= 0; Index
< MaxRetry
; Index
++) {
1866 if (!ScsiDiskDevice
->Cdb16Byte
) {
1867 Status
= ScsiDiskWrite10 (
1879 Status
= ScsiDiskWrite16 (
1891 if (!EFI_ERROR (Status
)) {
1896 return EFI_DEVICE_ERROR
;
1900 if ((Index
== MaxRetry
) && (Status
!= EFI_SUCCESS
)) {
1901 return EFI_DEVICE_ERROR
;
1904 // actual transferred sectors
1906 SectorCount
= ByteCount
/ BlockSize
;
1909 PtrBuffer
= PtrBuffer
+ SectorCount
* BlockSize
;
1910 BlocksRemaining
-= SectorCount
;
1918 Submit Read(10) command.
1920 @param ScsiDiskDevice The pointer of ScsiDiskDevice
1921 @param NeedRetry The pointer of flag indicates if needs retry if error happens
1922 @param SenseDataArray NOT used yet in this function
1923 @param NumberOfSenseKeys The number of sense key
1924 @param Timeout The time to complete the command
1925 @param DataBuffer The buffer to fill with the read out data
1926 @param DataLength The length of buffer
1927 @param StartLba The start logic block address
1928 @param SectorSize The size of sector
1930 @return EFI_STATUS is returned by calling ScsiRead10Command().
1934 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1935 OUT BOOLEAN
*NeedRetry
,
1936 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
, OPTIONAL
1937 OUT UINTN
*NumberOfSenseKeys
,
1939 OUT UINT8
*DataBuffer
,
1940 IN OUT UINT32
*DataLength
,
1942 IN UINT32 SectorSize
1945 UINT8 SenseDataLength
;
1947 UINT8 HostAdapterStatus
;
1951 *NumberOfSenseKeys
= 0;
1952 SenseDataLength
= 0;
1953 Status
= ScsiRead10Command (
1954 ScsiDiskDevice
->ScsiIo
,
1970 Submit Write(10) Command.
1972 @param ScsiDiskDevice The pointer of ScsiDiskDevice
1973 @param NeedRetry The pointer of flag indicates if needs retry if error happens
1974 @param SenseDataArray NOT used yet in this function
1975 @param NumberOfSenseKeys The number of sense key
1976 @param Timeout The time to complete the command
1977 @param DataBuffer The buffer to fill with the read out data
1978 @param DataLength The length of buffer
1979 @param StartLba The start logic block address
1980 @param SectorSize The size of sector
1982 @return EFI_STATUS is returned by calling ScsiWrite10Command().
1987 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
1988 OUT BOOLEAN
*NeedRetry
,
1989 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
, OPTIONAL
1990 OUT UINTN
*NumberOfSenseKeys
,
1992 IN UINT8
*DataBuffer
,
1993 IN OUT UINT32
*DataLength
,
1995 IN UINT32 SectorSize
1999 UINT8 SenseDataLength
;
2000 UINT8 HostAdapterStatus
;
2004 *NumberOfSenseKeys
= 0;
2005 SenseDataLength
= 0;
2006 Status
= ScsiWrite10Command (
2007 ScsiDiskDevice
->ScsiIo
,
2023 Submit Read(16) command.
2025 @param ScsiDiskDevice The pointer of ScsiDiskDevice
2026 @param NeedRetry The pointer of flag indicates if needs retry if error happens
2027 @param SenseDataArray NOT used yet in this function
2028 @param NumberOfSenseKeys The number of sense key
2029 @param Timeout The time to complete the command
2030 @param DataBuffer The buffer to fill with the read out data
2031 @param DataLength The length of buffer
2032 @param StartLba The start logic block address
2033 @param SectorSize The size of sector
2035 @return EFI_STATUS is returned by calling ScsiRead10Command().
2039 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
2040 OUT BOOLEAN
*NeedRetry
,
2041 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
, OPTIONAL
2042 OUT UINTN
*NumberOfSenseKeys
,
2044 OUT UINT8
*DataBuffer
,
2045 IN OUT UINT32
*DataLength
,
2047 IN UINT32 SectorSize
2050 UINT8 SenseDataLength
;
2052 UINT8 HostAdapterStatus
;
2056 *NumberOfSenseKeys
= 0;
2057 SenseDataLength
= 0;
2058 Status
= ScsiRead16Command (
2059 ScsiDiskDevice
->ScsiIo
,
2075 Submit Write(16) Command.
2077 @param ScsiDiskDevice The pointer of ScsiDiskDevice
2078 @param NeedRetry The pointer of flag indicates if needs retry if error happens
2079 @param SenseDataArray NOT used yet in this function
2080 @param NumberOfSenseKeys The number of sense key
2081 @param Timeout The time to complete the command
2082 @param DataBuffer The buffer to fill with the read out data
2083 @param DataLength The length of buffer
2084 @param StartLba The start logic block address
2085 @param SectorSize The size of sector
2087 @return EFI_STATUS is returned by calling ScsiWrite10Command().
2092 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
2093 OUT BOOLEAN
*NeedRetry
,
2094 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
, OPTIONAL
2095 OUT UINTN
*NumberOfSenseKeys
,
2097 IN UINT8
*DataBuffer
,
2098 IN OUT UINT32
*DataLength
,
2100 IN UINT32 SectorSize
2104 UINT8 SenseDataLength
;
2105 UINT8 HostAdapterStatus
;
2109 *NumberOfSenseKeys
= 0;
2110 SenseDataLength
= 0;
2111 Status
= ScsiWrite16Command (
2112 ScsiDiskDevice
->ScsiIo
,
2128 Check sense key to find if media presents.
2130 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2131 @param SenseCounts The number of sense key
2133 @retval TRUE NOT any media
2134 @retval FALSE Media presents
2138 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2139 IN UINTN SenseCounts
2142 EFI_SCSI_SENSE_DATA
*SensePtr
;
2147 SensePtr
= SenseData
;
2149 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2151 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2),
2152 // Additional Sense Code is ASC_NO_MEDIA (0x3A)
2154 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_NOT_READY
) &&
2155 (SensePtr
->Addnl_Sense_Code
== EFI_SCSI_ASC_NO_MEDIA
)) {
2168 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2169 @param SenseCounts The number of sense key
2172 @retval FALSE NOT error
2176 ScsiDiskIsMediaError (
2177 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2178 IN UINTN SenseCounts
2181 EFI_SCSI_SENSE_DATA
*SensePtr
;
2186 SensePtr
= SenseData
;
2188 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2190 switch (SensePtr
->Sense_Key
) {
2192 case EFI_SCSI_SK_MEDIUM_ERROR
:
2194 // Sense Key is EFI_SCSI_SK_MEDIUM_ERROR (0x3)
2196 switch (SensePtr
->Addnl_Sense_Code
) {
2201 case EFI_SCSI_ASC_MEDIA_ERR1
:
2206 case EFI_SCSI_ASC_MEDIA_ERR2
:
2211 case EFI_SCSI_ASC_MEDIA_ERR3
:
2212 case EFI_SCSI_ASC_MEDIA_ERR4
:
2222 case EFI_SCSI_SK_NOT_READY
:
2224 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2)
2226 switch (SensePtr
->Addnl_Sense_Code
) {
2228 // Additional Sense Code is ASC_MEDIA_UPSIDE_DOWN (0x6)
2230 case EFI_SCSI_ASC_MEDIA_UPSIDE_DOWN
:
2251 Check sense key to find if hardware error happens.
2253 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2254 @param SenseCounts The number of sense key
2256 @retval TRUE Hardware error exits.
2257 @retval FALSE NO error.
2261 ScsiDiskIsHardwareError (
2262 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2263 IN UINTN SenseCounts
2266 EFI_SCSI_SENSE_DATA
*SensePtr
;
2271 SensePtr
= SenseData
;
2273 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2276 // Sense Key is EFI_SCSI_SK_HARDWARE_ERROR (0x4)
2278 if (SensePtr
->Sense_Key
== EFI_SCSI_SK_HARDWARE_ERROR
) {
2290 Check sense key to find if media has changed.
2292 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2293 @param SenseCounts The number of sense key
2295 @retval TRUE Media is changed.
2296 @retval FALSE Media is NOT changed.
2299 ScsiDiskIsMediaChange (
2300 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2301 IN UINTN SenseCounts
2304 EFI_SCSI_SENSE_DATA
*SensePtr
;
2306 BOOLEAN IsMediaChanged
;
2308 IsMediaChanged
= FALSE
;
2309 SensePtr
= SenseData
;
2311 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2313 // Sense Key is EFI_SCSI_SK_UNIT_ATTENTION (0x6),
2314 // Additional sense code is EFI_SCSI_ASC_MEDIA_CHANGE (0x28)
2316 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_UNIT_ATTENTION
) &&
2317 (SensePtr
->Addnl_Sense_Code
== EFI_SCSI_ASC_MEDIA_CHANGE
)) {
2318 IsMediaChanged
= TRUE
;
2324 return IsMediaChanged
;
2328 Check sense key to find if reset happens.
2330 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2331 @param SenseCounts The number of sense key
2333 @retval TRUE It is reset before.
2334 @retval FALSE It is NOT reset before.
2338 ScsiDiskIsResetBefore (
2339 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2340 IN UINTN SenseCounts
2343 EFI_SCSI_SENSE_DATA
*SensePtr
;
2345 BOOLEAN IsResetBefore
;
2347 IsResetBefore
= FALSE
;
2348 SensePtr
= SenseData
;
2350 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2353 // Sense Key is EFI_SCSI_SK_UNIT_ATTENTION (0x6)
2354 // Additional Sense Code is EFI_SCSI_ASC_RESET (0x29)
2356 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_UNIT_ATTENTION
) &&
2357 (SensePtr
->Addnl_Sense_Code
== EFI_SCSI_ASC_RESET
)) {
2358 IsResetBefore
= TRUE
;
2364 return IsResetBefore
;
2368 Check sense key to find if the drive is ready.
2370 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2371 @param SenseCounts The number of sense key
2372 @param RetryLater The flag means if need a retry
2374 @retval TRUE Drive is ready.
2375 @retval FALSE Drive is NOT ready.
2379 ScsiDiskIsDriveReady (
2380 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2381 IN UINTN SenseCounts
,
2382 OUT BOOLEAN
*RetryLater
2385 EFI_SCSI_SENSE_DATA
*SensePtr
;
2390 *RetryLater
= FALSE
;
2391 SensePtr
= SenseData
;
2393 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2395 switch (SensePtr
->Sense_Key
) {
2397 case EFI_SCSI_SK_NOT_READY
:
2399 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2)
2401 switch (SensePtr
->Addnl_Sense_Code
) {
2402 case EFI_SCSI_ASC_NOT_READY
:
2404 // Additional Sense Code is EFI_SCSI_ASC_NOT_READY (0x4)
2406 switch (SensePtr
->Addnl_Sense_Code_Qualifier
) {
2407 case EFI_SCSI_ASCQ_IN_PROGRESS
:
2409 // Additional Sense Code Qualifier is
2410 // EFI_SCSI_ASCQ_IN_PROGRESS (0x1)
2418 *RetryLater
= FALSE
;
2439 Check sense key to find if it has sense key.
2441 @param SenseData - The pointer of EFI_SCSI_SENSE_DATA
2442 @param SenseCounts - The number of sense key
2444 @retval TRUE It has sense key.
2445 @retval FALSE It has NOT any sense key.
2449 ScsiDiskHaveSenseKey (
2450 IN EFI_SCSI_SENSE_DATA
*SenseData
,
2451 IN UINTN SenseCounts
2454 EFI_SCSI_SENSE_DATA
*SensePtr
;
2456 BOOLEAN HaveSenseKey
;
2458 if (SenseCounts
== 0) {
2459 HaveSenseKey
= FALSE
;
2461 HaveSenseKey
= TRUE
;
2464 SensePtr
= SenseData
;
2466 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2469 // Sense Key is SK_NO_SENSE (0x0)
2471 if ((SensePtr
->Sense_Key
== EFI_SCSI_SK_NO_SENSE
) &&
2473 HaveSenseKey
= FALSE
;
2479 return HaveSenseKey
;
2483 Release resource about disk device.
2485 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
2489 ReleaseScsiDiskDeviceResources (
2490 IN SCSI_DISK_DEV
*ScsiDiskDevice
2493 if (ScsiDiskDevice
== NULL
) {
2497 if (ScsiDiskDevice
->SenseData
!= NULL
) {
2498 FreePool (ScsiDiskDevice
->SenseData
);
2499 ScsiDiskDevice
->SenseData
= NULL
;
2502 if (ScsiDiskDevice
->ControllerNameTable
!= NULL
) {
2503 FreeUnicodeStringTable (ScsiDiskDevice
->ControllerNameTable
);
2504 ScsiDiskDevice
->ControllerNameTable
= NULL
;
2507 FreePool (ScsiDiskDevice
);
2509 ScsiDiskDevice
= NULL
;
2513 Determine if Block Io should be produced.
2516 @param ChildHandle Child Handle to retrieve Parent information.
2518 @retval TRUE Should produce Block Io.
2519 @retval FALSE Should not produce Block Io.
2523 DetermineInstallBlockIo (
2524 IN EFI_HANDLE ChildHandle
2527 EFI_SCSI_PASS_THRU_PROTOCOL
*ScsiPassThru
;
2528 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtScsiPassThru
;
2531 // Firstly, check if ExtScsiPassThru Protocol parent handle exists. If existence,
2532 // check its attribute, logic or physical.
2534 ExtScsiPassThru
= (EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*)GetParentProtocol (&gEfiExtScsiPassThruProtocolGuid
, ChildHandle
);
2535 if (ExtScsiPassThru
!= NULL
) {
2536 if ((ExtScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
) != 0) {
2542 // Secondly, check if ScsiPassThru Protocol parent handle exists. If existence,
2543 // check its attribute, logic or physical.
2545 ScsiPassThru
= (EFI_SCSI_PASS_THRU_PROTOCOL
*)GetParentProtocol (&gEfiScsiPassThruProtocolGuid
, ChildHandle
);
2546 if (ScsiPassThru
!= NULL
) {
2547 if ((ScsiPassThru
->Mode
->Attributes
& EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
) != 0) {
2556 Search protocol database and check to see if the protocol
2557 specified by ProtocolGuid is present on a ControllerHandle and opened by
2558 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
2559 If the ControllerHandle is found, then the protocol specified by ProtocolGuid
2560 will be opened on it.
2563 @param ProtocolGuid ProtocolGuid pointer.
2564 @param ChildHandle Child Handle to retrieve Parent information.
2570 IN EFI_GUID
*ProtocolGuid
,
2571 IN EFI_HANDLE ChildHandle
2578 EFI_HANDLE
*HandleBuffer
;
2581 // Retrieve the list of all handles from the handle database
2583 Status
= gBS
->LocateHandleBuffer (
2591 if (EFI_ERROR (Status
)) {
2596 // Iterate to find who is parent handle that is opened with ProtocolGuid by ChildHandle
2598 for (Index
= 0; Index
< HandleCount
; Index
++) {
2599 Status
= EfiTestChildHandle (HandleBuffer
[Index
], ChildHandle
, ProtocolGuid
);
2600 if (!EFI_ERROR (Status
)) {
2601 Status
= gBS
->HandleProtocol (HandleBuffer
[Index
], ProtocolGuid
, (VOID
**)&Interface
);
2602 if (!EFI_ERROR (Status
)) {
2603 gBS
->FreePool (HandleBuffer
);
2609 gBS
->FreePool (HandleBuffer
);
2614 Provides inquiry information for the controller type.
2616 This function is used by the IDE bus driver to get inquiry data. Data format
2617 of Identify data is defined by the Interface GUID.
2619 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
2620 @param[in, out] InquiryData Pointer to a buffer for the inquiry data.
2621 @param[in, out] InquiryDataSize Pointer to the value for the inquiry data size.
2623 @retval EFI_SUCCESS The command was accepted without any errors.
2624 @retval EFI_NOT_FOUND Device does not support this data class
2625 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
2626 @retval EFI_BUFFER_TOO_SMALL InquiryDataSize not big enough
2631 ScsiDiskInfoInquiry (
2632 IN EFI_DISK_INFO_PROTOCOL
*This
,
2633 IN OUT VOID
*InquiryData
,
2634 IN OUT UINT32
*InquiryDataSize
2638 SCSI_DISK_DEV
*ScsiDiskDevice
;
2640 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_DISKINFO (This
);
2642 Status
= EFI_BUFFER_TOO_SMALL
;
2643 if (*InquiryDataSize
>= sizeof (ScsiDiskDevice
->InquiryData
)) {
2644 Status
= EFI_SUCCESS
;
2645 CopyMem (InquiryData
, &ScsiDiskDevice
->InquiryData
, sizeof (ScsiDiskDevice
->InquiryData
));
2647 *InquiryDataSize
= sizeof (ScsiDiskDevice
->InquiryData
);
2653 Provides identify information for the controller type.
2655 This function is used by the IDE bus driver to get identify data. Data format
2656 of Identify data is defined by the Interface GUID.
2658 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL
2660 @param[in, out] IdentifyData Pointer to a buffer for the identify data.
2661 @param[in, out] IdentifyDataSize Pointer to the value for the identify data
2664 @retval EFI_SUCCESS The command was accepted without any errors.
2665 @retval EFI_NOT_FOUND Device does not support this data class
2666 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
2667 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
2672 ScsiDiskInfoIdentify (
2673 IN EFI_DISK_INFO_PROTOCOL
*This
,
2674 IN OUT VOID
*IdentifyData
,
2675 IN OUT UINT32
*IdentifyDataSize
2679 SCSI_DISK_DEV
*ScsiDiskDevice
;
2681 if (CompareGuid (&This
->Interface
, &gEfiDiskInfoScsiInterfaceGuid
)) {
2683 // Physical SCSI bus does not support this data class.
2685 return EFI_NOT_FOUND
;
2688 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_DISKINFO (This
);
2690 Status
= EFI_BUFFER_TOO_SMALL
;
2691 if (*IdentifyDataSize
>= sizeof (ScsiDiskDevice
->IdentifyData
)) {
2692 Status
= EFI_SUCCESS
;
2693 CopyMem (IdentifyData
, &ScsiDiskDevice
->IdentifyData
, sizeof (ScsiDiskDevice
->IdentifyData
));
2695 *IdentifyDataSize
= sizeof (ScsiDiskDevice
->IdentifyData
);
2700 Provides sense data information for the controller type.
2702 This function is used by the IDE bus driver to get sense data.
2703 Data format of Sense data is defined by the Interface GUID.
2705 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
2706 @param[in, out] SenseData Pointer to the SenseData.
2707 @param[in, out] SenseDataSize Size of SenseData in bytes.
2708 @param[out] SenseDataNumber Pointer to the value for the sense data size.
2710 @retval EFI_SUCCESS The command was accepted without any errors.
2711 @retval EFI_NOT_FOUND Device does not support this data class.
2712 @retval EFI_DEVICE_ERROR Error reading SenseData from device.
2713 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough.
2718 ScsiDiskInfoSenseData (
2719 IN EFI_DISK_INFO_PROTOCOL
*This
,
2720 IN OUT VOID
*SenseData
,
2721 IN OUT UINT32
*SenseDataSize
,
2722 OUT UINT8
*SenseDataNumber
2725 return EFI_NOT_FOUND
;
2730 This function is used by the IDE bus driver to get controller information.
2732 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
2733 @param[out] IdeChannel Pointer to the Ide Channel number. Primary or secondary.
2734 @param[out] IdeDevice Pointer to the Ide Device number. Master or slave.
2736 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid.
2737 @retval EFI_UNSUPPORTED This is not an IDE device.
2742 ScsiDiskInfoWhichIde (
2743 IN EFI_DISK_INFO_PROTOCOL
*This
,
2744 OUT UINT32
*IdeChannel
,
2745 OUT UINT32
*IdeDevice
2748 SCSI_DISK_DEV
*ScsiDiskDevice
;
2750 if (CompareGuid (&This
->Interface
, &gEfiDiskInfoScsiInterfaceGuid
)) {
2752 // This is not an IDE physical device.
2754 return EFI_UNSUPPORTED
;
2757 ScsiDiskDevice
= SCSI_DISK_DEV_FROM_DISKINFO (This
);
2758 *IdeChannel
= ScsiDiskDevice
->Channel
;
2759 *IdeDevice
= ScsiDiskDevice
->Device
;
2766 Issues ATA IDENTIFY DEVICE command to identify ATAPI device.
2768 This function tries to fill 512-byte ATAPI_IDENTIFY_DATA for ATAPI device to
2769 implement Identify() interface for DiskInfo protocol. The ATA command is sent
2770 via SCSI Request Packet.
2772 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
2774 @retval EFI_SUCCESS The ATAPI device identify data were retrieved successfully.
2775 @retval others Some error occurred during the identification that ATAPI device.
2779 AtapiIdentifyDevice (
2780 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
2783 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket
;
2787 // Initialize SCSI REQUEST_PACKET and 6-byte Cdb
2789 ZeroMem (&CommandPacket
, sizeof (CommandPacket
));
2790 ZeroMem (Cdb
, sizeof (Cdb
));
2792 Cdb
[0] = ATA_CMD_IDENTIFY_DEVICE
;
2793 CommandPacket
.Timeout
= EFI_TIMER_PERIOD_SECONDS (1);
2794 CommandPacket
.Cdb
= Cdb
;
2795 CommandPacket
.CdbLength
= (UINT8
) sizeof (Cdb
);
2796 CommandPacket
.InDataBuffer
= &ScsiDiskDevice
->IdentifyData
;
2797 CommandPacket
.InTransferLength
= sizeof (ScsiDiskDevice
->IdentifyData
);
2799 return ScsiDiskDevice
->ScsiIo
->ExecuteScsiCommand (ScsiDiskDevice
->ScsiIo
, &CommandPacket
, NULL
);
2804 Initialize the installation of DiskInfo protocol.
2806 This function prepares for the installation of DiskInfo protocol on the child handle.
2807 By default, it installs DiskInfo protocol with SCSI interface GUID. If it further
2808 detects that the physical device is an ATAPI/AHCI device, it then updates interface GUID
2809 to be IDE/AHCI interface GUID.
2811 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV.
2812 @param ChildHandle Child handle to install DiskInfo protocol.
2816 InitializeInstallDiskInfo (
2817 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
2818 IN EFI_HANDLE ChildHandle
2822 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
2823 EFI_DEVICE_PATH_PROTOCOL
*ChildDevicePathNode
;
2824 ATAPI_DEVICE_PATH
*AtapiDevicePath
;
2825 SATA_DEVICE_PATH
*SataDevicePath
;
2826 UINTN IdentifyRetry
;
2828 Status
= gBS
->HandleProtocol (ChildHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**) &DevicePathNode
);
2830 // Device Path protocol must be installed on the device handle.
2832 ASSERT_EFI_ERROR (Status
);
2834 // Copy the DiskInfo protocol template.
2836 CopyMem (&ScsiDiskDevice
->DiskInfo
, &gScsiDiskInfoProtocolTemplate
, sizeof (gScsiDiskInfoProtocolTemplate
));
2838 while (!IsDevicePathEnd (DevicePathNode
)) {
2839 ChildDevicePathNode
= NextDevicePathNode (DevicePathNode
);
2840 if ((DevicePathType (DevicePathNode
) == HARDWARE_DEVICE_PATH
) &&
2841 (DevicePathSubType (DevicePathNode
) == HW_PCI_DP
) &&
2842 (DevicePathType (ChildDevicePathNode
) == MESSAGING_DEVICE_PATH
) &&
2843 ((DevicePathSubType (ChildDevicePathNode
) == MSG_ATAPI_DP
) ||
2844 (DevicePathSubType (ChildDevicePathNode
) == MSG_SATA_DP
))) {
2849 // Issue ATA Identify Device Command via SCSI command, which is required to publish DiskInfo protocol
2850 // with IDE/AHCI interface GUID.
2852 Status
= AtapiIdentifyDevice (ScsiDiskDevice
);
2853 if (!EFI_ERROR (Status
)) {
2854 if (DevicePathSubType(ChildDevicePathNode
) == MSG_ATAPI_DP
) {
2856 // We find the valid ATAPI device path
2858 AtapiDevicePath
= (ATAPI_DEVICE_PATH
*) ChildDevicePathNode
;
2859 ScsiDiskDevice
->Channel
= AtapiDevicePath
->PrimarySecondary
;
2860 ScsiDiskDevice
->Device
= AtapiDevicePath
->SlaveMaster
;
2862 // Update the DiskInfo.Interface to IDE interface GUID for the physical ATAPI device.
2864 CopyGuid (&ScsiDiskDevice
->DiskInfo
.Interface
, &gEfiDiskInfoIdeInterfaceGuid
);
2867 // We find the valid SATA device path
2869 SataDevicePath
= (SATA_DEVICE_PATH
*) ChildDevicePathNode
;
2870 ScsiDiskDevice
->Channel
= SataDevicePath
->HBAPortNumber
;
2871 ScsiDiskDevice
->Device
= SataDevicePath
->PortMultiplierPortNumber
;
2873 // Update the DiskInfo.Interface to AHCI interface GUID for the physical AHCI device.
2875 CopyGuid (&ScsiDiskDevice
->DiskInfo
.Interface
, &gEfiDiskInfoAhciInterfaceGuid
);
2879 } while (--IdentifyRetry
> 0);
2881 DevicePathNode
= ChildDevicePathNode
;