2 Header file for SCSI Disk Driver.
4 Copyright (c) 2004 - 2008, Intel Corporation. <BR>
5 All rights reserved. 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.
22 #include <Protocol/ScsiIo.h>
23 #include <Protocol/ComponentName.h>
24 #include <Protocol/BlockIo.h>
25 #include <Protocol/DriverBinding.h>
26 #include <Protocol/ScsiPassThruExt.h>
28 #include <Library/DebugLib.h>
29 #include <Library/UefiDriverEntryPoint.h>
30 #include <Library/UefiLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/UefiScsiLib.h>
34 #include <Library/UefiBootServicesTableLib.h>
36 #include <IndustryStandard/Scsi.h>
38 #define IS_DEVICE_FIXED(a) (a)->FixedDevice ? 1 : 0
40 #define SCSI_DISK_DEV_SIGNATURE SIGNATURE_32 ('s', 'c', 'd', 'k')
47 EFI_BLOCK_IO_PROTOCOL BlkIo
;
48 EFI_BLOCK_IO_MEDIA BlkIoMedia
;
49 EFI_SCSI_IO_PROTOCOL
*ScsiIo
;
54 EFI_SCSI_SENSE_DATA
*SenseData
;
55 UINTN SenseDataNumber
;
56 EFI_SCSI_INQUIRY_DATA InquiryData
;
58 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
62 #define SCSI_DISK_DEV_FROM_THIS(a) CR (a, SCSI_DISK_DEV, BlkIo, SCSI_DISK_DEV_SIGNATURE)
67 extern EFI_DRIVER_BINDING_PROTOCOL gScsiDiskDriverBinding
;
68 extern EFI_COMPONENT_NAME_PROTOCOL gScsiDiskComponentName
;
69 extern EFI_COMPONENT_NAME2_PROTOCOL gScsiDiskComponentName2
;
71 // action code used in detect media process
73 #define ACTION_NO_ACTION 0x00
74 #define ACTION_READ_CAPACITY 0x01
75 #define ACTION_RETRY_COMMAND_LATER 0x02
78 Test to see if this driver supports ControllerHandle.
80 This service is called by the EFI boot service ConnectController(). In order
81 to make drivers as small as possible, there are a few calling restrictions for
82 this service. ConnectController() must follow these calling restrictions.
83 If any other agent wishes to call Supported() it must also follow these
86 @param This Protocol instance pointer.
87 @param ControllerHandle Handle of device to test
88 @param RemainingDevicePath Optional parameter use to pick a specific child
91 @retval EFI_SUCCESS This driver supports this device
92 @retval EFI_ALREADY_STARTED This driver is already running on this device
93 @retval other This driver does not support this device
98 ScsiDiskDriverBindingSupported (
99 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
100 IN EFI_HANDLE Controller
,
101 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
105 Start this driver on ControllerHandle.
107 This service is called by the EFI boot service ConnectController(). In order
108 to make drivers as small as possible, there are a few calling restrictions for
109 this service. ConnectController() must follow these calling restrictions. If
110 any other agent wishes to call Start() it must also follow these calling
113 @param This Protocol instance pointer.
114 @param ControllerHandle Handle of device to bind driver to
115 @param RemainingDevicePath Optional parameter use to pick a specific child
118 @retval EFI_SUCCESS This driver is added to ControllerHandle
119 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
120 @retval other This driver does not support this device
125 ScsiDiskDriverBindingStart (
126 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
127 IN EFI_HANDLE Controller
,
128 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
132 Stop this driver on ControllerHandle.
134 This service is called by the EFI boot service DisconnectController().
135 In order to make drivers as small as possible, there are a few calling
136 restrictions for this service. DisconnectController() must follow these
137 calling restrictions. If any other agent wishes to call Stop() it must
138 also follow these calling restrictions.
140 @param This Protocol instance pointer.
141 @param ControllerHandle Handle of device to stop driver on
142 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
143 children is zero stop the entire bus driver.
144 @param ChildHandleBuffer List of Child Handles to Stop.
146 @retval EFI_SUCCESS This driver is removed ControllerHandle
147 @retval other This driver was not removed from this device
152 ScsiDiskDriverBindingStop (
153 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
154 IN EFI_HANDLE Controller
,
155 IN UINTN NumberOfChildren
,
156 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
160 // EFI Component Name Functions
163 Retrieves a Unicode string that is the user readable name of the driver.
165 This function retrieves the user readable name of a driver in the form of a
166 Unicode string. If the driver specified by This has a user readable name in
167 the language specified by Language, then a pointer to the driver name is
168 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
169 by This does not support the language specified by Language,
170 then EFI_UNSUPPORTED is returned.
172 @param This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
173 EFI_COMPONENT_NAME_PROTOCOL instance.
175 @param Language A pointer to a Null-terminated ASCII string
176 array indicating the language. This is the
177 language of the driver name that the caller is
178 requesting, and it must match one of the
179 languages specified in SupportedLanguages. The
180 number of languages supported by a driver is up
181 to the driver writer. Language is specified
182 in RFC 3066 or ISO 639-2 language code format.
184 @param DriverName A pointer to the Unicode string to return.
185 This Unicode string is the name of the
186 driver specified by This in the language
187 specified by Language.
189 @retval EFI_SUCCESS The Unicode string for the Driver specified by
190 This and the language specified by Language was
191 returned in DriverName.
193 @retval EFI_INVALID_PARAMETER Language is NULL.
195 @retval EFI_INVALID_PARAMETER DriverName is NULL.
197 @retval EFI_UNSUPPORTED The driver specified by This does not support
198 the language specified by Language.
203 ScsiDiskComponentNameGetDriverName (
204 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
206 OUT CHAR16
**DriverName
211 Retrieves a Unicode string that is the user readable name of the controller
212 that is being managed by a driver.
214 This function retrieves the user readable name of the controller specified by
215 ControllerHandle and ChildHandle in the form of a Unicode string. If the
216 driver specified by This has a user readable name in the language specified by
217 Language, then a pointer to the controller name is returned in ControllerName,
218 and EFI_SUCCESS is returned. If the driver specified by This is not currently
219 managing the controller specified by ControllerHandle and ChildHandle,
220 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
221 support the language specified by Language, then EFI_UNSUPPORTED is returned.
223 @param This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
224 EFI_COMPONENT_NAME_PROTOCOL instance.
226 @param ControllerHandle The handle of a controller that the driver
227 specified by This is managing. This handle
228 specifies the controller whose name is to be
231 @param ChildHandle The handle of the child controller to retrieve
232 the name of. This is an optional parameter that
233 may be NULL. It will be NULL for device
234 drivers. It will also be NULL for a bus drivers
235 that wish to retrieve the name of the bus
236 controller. It will not be NULL for a bus
237 driver that wishes to retrieve the name of a
240 @param Language A pointer to a Null-terminated ASCII string
241 array indicating the language. This is the
242 language of the driver name that the caller is
243 requesting, and it must match one of the
244 languages specified in SupportedLanguages. The
245 number of languages supported by a driver is up
246 to the driver writer. Language is specified in
247 RFC 3066 or ISO 639-2 language code format.
249 @param ControllerName A pointer to the Unicode string to return.
250 This Unicode string is the name of the
251 controller specified by ControllerHandle and
252 ChildHandle in the language specified by
253 Language from the point of view of the driver
256 @retval EFI_SUCCESS The Unicode string for the user readable name in
257 the language specified by Language for the
258 driver specified by This was returned in
261 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
263 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
266 @retval EFI_INVALID_PARAMETER Language is NULL.
268 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
270 @retval EFI_UNSUPPORTED The driver specified by This is not currently
271 managing the controller specified by
272 ControllerHandle and ChildHandle.
274 @retval EFI_UNSUPPORTED The driver specified by This does not support
275 the language specified by Language.
280 ScsiDiskComponentNameGetControllerName (
281 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
282 IN EFI_HANDLE ControllerHandle
,
283 IN EFI_HANDLE ChildHandle OPTIONAL
,
285 OUT CHAR16
**ControllerName
292 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
293 @param ExtendedVerification The flag about if extend verificate
295 @retval EFI_SUCCESS The device was reset.
296 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
298 @return EFI_STATUS is retured from EFI_SCSI_IO_PROTOCOL.ResetDevice().
304 IN EFI_BLOCK_IO_PROTOCOL
*This
,
305 IN BOOLEAN ExtendedVerification
310 The function is to Read Block from SCSI Disk.
312 @param This The pointer of EFI_BLOCK_IO_PROTOCOL.
313 @param MediaId The Id of Media detected
314 @param Lba The logic block address
315 @param BufferSize The size of Buffer
316 @param Buffer The buffer to fill the read out data
318 @retval EFI_SUCCESS Successfully to read out block.
319 @retval EFI_DEVICE_ERROR Fail to detect media.
320 @retval EFI_NO_MEDIA Media is not present.
321 @retval EFI_MEDIA_CHANGED Media has changed.
322 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
323 @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
329 IN EFI_BLOCK_IO_PROTOCOL
*This
,
338 The function is to Write Block to SCSI Disk.
340 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
341 @param MediaId The Id of Media detected
342 @param Lba The logic block address
343 @param BufferSize The size of Buffer
344 @param Buffer The buffer to fill the read out data
346 @retval EFI_SUCCESS Successfully to read out block.
347 @retval EFI_WRITE_PROTECTED The device can not be written to.
348 @retval EFI_DEVICE_ERROR Fail to detect media.
349 @retval EFI_NO_MEDIA Media is not present.
350 @retval EFI_MEDIA_CHNAGED Media has changed.
351 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
352 @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
357 ScsiDiskWriteBlocks (
358 IN EFI_BLOCK_IO_PROTOCOL
*This
,
369 EFI_SUCCESS is returned directly.
371 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
373 @retval EFI_SUCCESS All outstanding data was written to the device
378 ScsiDiskFlushBlocks (
379 IN EFI_BLOCK_IO_PROTOCOL
*This
383 Dectect Device and read out capacity ,if error occurs, parse the sense key.
385 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
386 @param MustReadCapacity The flag about reading device capacity
387 @param MediaChange The pointer of flag indicates if media has changed
389 @retval EFI_DEVICE_ERROR Indicates that error occurs
390 @retval EFI_SUCCESS Successfully to detect media
394 ScsiDiskDetectMedia (
395 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
396 IN BOOLEAN MustReadCapacity
,
397 OUT BOOLEAN
*MediaChange
403 When Test Unit Ready command succeeds, retrieve Sense Keys via Request Sense;
404 When Test Unit Ready command encounters any error caused by host adapter or
405 target, return error without retrieving Sense Keys.
407 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
408 @param NeedRetry The pointer of flag indicates try again
409 @param SenseDataArray The pointer of an array of sense data
410 @param NumberOfSenseKeys The pointer of the number of sense data array
412 @retval EFI_DEVICE_ERROR Indicates that error occurs
413 @retval EFI_SUCCESS Successfully to test unit
417 ScsiDiskTestUnitReady (
418 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
419 OUT BOOLEAN
*NeedRetry
,
420 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
421 OUT UINTN
*NumberOfSenseKeys
426 Parsing Sense Keys which got from request sense command.
428 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
429 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
430 @param NumberOfSenseKeys The number of sense key
431 @param Action The pointer of action which indicates what is need to do next
433 @retval EFI_DEVICE_ERROR Indicates that error occurs
434 @retval EFI_SUCCESS Successfully to complete the parsing
438 DetectMediaParsingSenseKeys (
439 OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
440 IN EFI_SCSI_SENSE_DATA
*SenseData
,
441 IN UINTN NumberOfSenseKeys
,
447 Send read capacity command to device and get the device parameter.
449 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
450 @param NeedRetry The pointer of flag indicates if need a retry
451 @param SenseDataArray The pointer of an array of sense data
452 @param NumberOfSenseKeys The number of sense key
454 @retval EFI_DEVICE_ERROR Indicates that error occurs
455 @retval EFI_SUCCESS Successfully to read capacity
459 ScsiDiskReadCapacity (
460 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
461 OUT BOOLEAN
*NeedRetry
,
462 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
463 OUT UINTN
*NumberOfSenseKeys
467 Check the HostAdapter status and re-interpret it in EFI_STATUS.
469 @param HostAdapterStatus Host Adapter status
471 @retval EFI_SUCCESS Host adapter is OK.
472 @retval EFI_TIMEOUT Timeout.
473 @retval EFI_NOT_READY Adapter NOT ready.
474 @retval EFI_DEVICE_ERROR Adapter device error.
478 CheckHostAdapterStatus (
479 IN UINT8 HostAdapterStatus
484 Check the target status and re-interpret it in EFI_STATUS.
486 @param TargetStatus Target status
488 @retval EFI_NOT_READY Device is NOT ready.
489 @retval EFI_DEVICE_ERROR
495 IN UINT8 TargetStatus
499 Retrieve all sense keys from the device.
501 When encountering error during the process, if retrieve sense keys before
502 error encounterred, it returns the sense keys with return status set to EFI_SUCCESS,
503 and NeedRetry set to FALSE; otherwize, return the proper return status.
505 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
506 @param NeedRetry The pointer of flag indicates if need a retry
507 @param SenseDataArray The pointer of an array of sense data
508 @param NumberOfSenseKeys The number of sense key
509 @param AskResetIfError The flag indicates if need reset when error occurs
511 @retval EFI_DEVICE_ERROR Indicates that error occurs
512 @retval EFI_SUCCESS Successfully to request sense key
516 ScsiDiskRequestSenseKeys (
517 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
518 OUT BOOLEAN
*NeedRetry
,
519 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
,
520 OUT UINTN
*NumberOfSenseKeys
,
521 IN BOOLEAN AskResetIfError
525 Send out Inquiry command to Device.
527 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
528 @param NeedRetry Indicates if needs try again when error happens
530 @retval EFI_DEVICE_ERROR Indicates that error occurs
531 @retval EFI_SUCCESS Successfully to detect media
535 ScsiDiskInquiryDevice (
536 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
537 OUT BOOLEAN
*NeedRetry
543 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
548 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
552 Read sector from SCSI Disk.
554 @param ScsiDiskDevice The poiniter of SCSI_DISK_DEV
555 @param Buffer The buffer to fill in the read out data
556 @param Lba Logic block address
557 @param NumberOfBlocks The number of blocks to read
559 @retval EFI_DEVICE_ERROR Indicates a device error.
560 @retval EFI_SUCCESS Operation is successful.
564 ScsiDiskReadSectors (
565 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
568 IN UINTN NumberOfBlocks
572 Write sector to SCSI Disk.
574 @param ScsiDiskDevice The poiniter of SCSI_DISK_DEV
575 @param Buffer The buffer of data to be written into SCSI Disk
576 @param Lba Logic block address
577 @param NumberOfBlocks The number of blocks to read
579 @retval EFI_DEVICE_ERROR Indicates a device error.
580 @retval EFI_SUCCESS Operation is successful.
584 ScsiDiskWriteSectors (
585 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
588 IN UINTN NumberOfBlocks
592 Sumbmit Read command.
594 @param ScsiDiskDevice The pointer of ScsiDiskDevice
595 @param NeedRetry The pointer of flag indicates if needs retry if error happens
596 @param SenseDataArray NOT used yet in this function
597 @param NumberOfSenseKeys The number of sense key
598 @param Timeout The time to complete the command
599 @param DataBuffer The buffer to fill with the read out data
600 @param DataLength The length of buffer
601 @param StartLba The start logic block address
602 @param SectorSize The size of sector
604 @return EFI_STATUS is returned by calling ScsiRead10Command().
608 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
609 OUT BOOLEAN
*NeedRetry
,
610 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
, OPTIONAL
611 OUT UINTN
*NumberOfSenseKeys
,
613 OUT UINT8
*DataBuffer
,
614 IN OUT UINT32
*DataLength
,
620 Submit Write Command.
622 @param ScsiDiskDevice The pointer of ScsiDiskDevice
623 @param NeedRetry The pointer of flag indicates if needs retry if error happens
624 @param SenseDataArray NOT used yet in this function
625 @param NumberOfSenseKeys The number of sense key
626 @param Timeout The time to complete the command
627 @param DataBuffer The buffer to fill with the read out data
628 @param DataLength The length of buffer
629 @param StartLba The start logic block address
630 @param SectorSize The size of sector
632 @return EFI_STATUS is returned by calling ScsiWrite10Command().
637 IN SCSI_DISK_DEV
*ScsiDiskDevice
,
638 OUT BOOLEAN
*NeedRetry
,
639 OUT EFI_SCSI_SENSE_DATA
**SenseDataArray
, OPTIONAL
640 OUT UINTN
*NumberOfSenseKeys
,
642 IN UINT8
*DataBuffer
,
643 IN OUT UINT32
*DataLength
,
649 Get information from media read capacity command.
651 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
652 @param Capacity The pointer of EFI_SCSI_DISK_CAPACITY_DATA
657 IN OUT SCSI_DISK_DEV
*ScsiDiskDevice
,
658 IN EFI_SCSI_DISK_CAPACITY_DATA
*Capacity
662 Check sense key to find if media presents.
664 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
665 @param SenseCounts The number of sense key
667 @retval TRUE NOT any media
668 @retval FALSE Media presents
672 IN EFI_SCSI_SENSE_DATA
*SenseData
,
679 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
680 @param SenseCounts The number of sense key
683 @retval FALSE NOT error
687 ScsiDiskIsMediaError (
688 IN EFI_SCSI_SENSE_DATA
*SenseData
,
693 Check sense key to find if hardware error happens.
695 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
696 @param SenseCounts The number of sense key
698 @retval TRUE Hardware error exits.
699 @retval FALSE NO error.
703 ScsiDiskIsHardwareError (
704 IN EFI_SCSI_SENSE_DATA
*SenseData
,
709 Check sense key to find if media has changed.
711 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
712 @param SenseCounts The number of sense key
714 @retval TRUE Media is changed.
715 @retval FALSE Medit is NOT changed.
718 ScsiDiskIsMediaChange (
719 IN EFI_SCSI_SENSE_DATA
*SenseData
,
724 Check sense key to find if reset happens.
726 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
727 @param SenseCounts The number of sense key
729 @retval TRUE It is reset before.
730 @retval FALSE It is NOT reset before.
734 ScsiDiskIsResetBefore (
735 IN EFI_SCSI_SENSE_DATA
*SenseData
,
740 Check sense key to find if the drive is ready.
742 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
743 @param SenseCounts The number of sense key
744 @param RetryLater The flag means if need a retry
746 @retval TRUE Drive is ready.
747 @retval FALSE Drive is NOT ready.
751 ScsiDiskIsDriveReady (
752 IN EFI_SCSI_SENSE_DATA
*SenseData
,
753 IN UINTN SenseCounts
,
754 OUT BOOLEAN
*RetryLater
758 Check sense key to find if it has sense key.
760 @param SenseData - The pointer of EFI_SCSI_SENSE_DATA
761 @param SenseCounts - The number of sense key
763 @retval TRUE It has sense key.
764 @retval FALSE It has NOT any sense key.
768 ScsiDiskHaveSenseKey (
769 IN EFI_SCSI_SENSE_DATA
*SenseData
,
774 Release resource about disk device.
776 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
780 ReleaseScsiDiskDeviceResources (
781 IN SCSI_DISK_DEV
*ScsiDiskDevice