2 Copyright (c) 2006, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 Module Name: AtapiPassThru.h
22 #include <Protocol/ScsiPassThru.h>
23 #include <Protocol/PciIo.h>
25 #include <Library/DebugLib.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/BaseLib.h>
28 #include <Library/UefiLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/MemoryAllocationLib.h>
31 #include <Library/UefiBootServicesTableLib.h>
32 #include <IndustryStandard/pci22.h>
37 #define bit(a) (1 << (a))
39 #define MAX_TARGET_ID 4
45 UINT16 Command
; /* when write */
46 UINT16 Status
; /* when read */
50 UINT16 Error
; /* when read */
51 UINT16 Feature
; /* when write */
52 } IDE_ERROR_OR_FEATURE
;
55 UINT16 AltStatus
; /* when read */
56 UINT16 DeviceControl
; /* when write */
57 } IDE_AltStatus_OR_DeviceControl
;
70 // Bit definitions in Programming Interface byte of the Class Code field
71 // in PCI IDE controller's Configuration Space
73 #define IDE_PRIMARY_OPERATING_MODE BIT0
74 #define IDE_PRIMARY_PROGRAMMABLE_INDICATOR BIT1
75 #define IDE_SECONDARY_OPERATING_MODE BIT2
76 #define IDE_SECONDARY_PROGRAMMABLE_INDICATOR BIT3
79 #define ATAPI_MAX_CHANNEL 2
86 IDE_ERROR_OR_FEATURE Reg1
;
92 IDE_CMD_OR_STATUS Reg
;
93 IDE_AltStatus_OR_DeviceControl Alt
;
97 #define ATAPI_SCSI_PASS_THRU_DEV_SIGNATURE EFI_SIGNATURE_32 ('a', 's', 'p', 't')
102 EFI_SCSI_PASS_THRU_PROTOCOL ScsiPassThru
;
103 EFI_SCSI_PASS_THRU_MODE ScsiPassThruMode
;
104 EFI_PCI_IO_PROTOCOL
*PciIo
;
106 // Local Data goes here
108 IDE_BASE_REGISTERS
*IoPort
;
109 IDE_BASE_REGISTERS AtapiIoPortRegisters
[2];
110 CHAR16 ControllerName
[100];
111 CHAR16 ChannelName
[100];
112 UINT32 LatestTargetId
;
114 } ATAPI_SCSI_PASS_THRU_DEV
;
117 // IDE registers' base addresses
120 UINT16 CommandBlockBaseAddr
;
121 UINT16 ControlBlockBaseAddr
;
122 } IDE_REGISTERS_BASE_ADDR
;
124 #define ATAPI_SCSI_PASS_THRU_DEV_FROM_THIS(a) \
126 ATAPI_SCSI_PASS_THRU_DEV, \
128 ATAPI_SCSI_PASS_THRU_DEV_SIGNATURE \
134 extern EFI_DRIVER_BINDING_PROTOCOL gAtapiScsiPassThruDriverBinding
;
135 extern EFI_COMPONENT_NAME_PROTOCOL gAtapiScsiPassThruComponentName
;
136 extern EFI_COMPONENT_NAME2_PROTOCOL gAtapiScsiPassThruComponentName2
;
139 // ATAPI Command op code
141 #define OP_INQUIRY 0x12
142 #define OP_LOAD_UNLOAD_CD 0xa6
143 #define OP_MECHANISM_STATUS 0xbd
144 #define OP_MODE_SELECT_10 0x55
145 #define OP_MODE_SENSE_10 0x5a
146 #define OP_PAUSE_RESUME 0x4b
147 #define OP_PLAY_AUDIO_10 0x45
148 #define OP_PLAY_AUDIO_MSF 0x47
149 #define OP_PLAY_CD 0xbc
150 #define OP_PLAY_CD_MSF 0xb4
151 #define OP_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
152 #define OP_READ_10 0x28
153 #define OP_READ_12 0xa8
154 #define OP_READ_CAPACITY 0x25
155 #define OP_READ_CD 0xbe
156 #define OP_READ_CD_MSF 0xb9
157 #define OP_READ_HEADER 0x44
158 #define OP_READ_SUB_CHANNEL 0x42
159 #define OP_READ_TOC 0x43
160 #define OP_REQUEST_SENSE 0x03
162 #define OP_SEEK_10 0x2b
163 #define OP_SET_CD_SPEED 0xbb
164 #define OP_STOPPLAY_SCAN 0x4e
165 #define OP_START_STOP_UNIT 0x1b
166 #define OP_TEST_UNIT_READY 0x00
168 #define OP_FORMAT_UNIT 0x04
169 #define OP_READ_FORMAT_CAPACITIES 0x23
170 #define OP_VERIFY 0x2f
171 #define OP_WRITE_10 0x2a
172 #define OP_WRITE_12 0xaa
173 #define OP_WRITE_AND_VERIFY 0x2e
178 #define ATAPI_SOFT_RESET_CMD 0x08
189 DATA_DIRECTION Direction
;
192 #define MAX_CHANNEL 2
194 #define ValidCdbLength(Len) ((Len) == 6 || (Len) == 10 || (Len) == 12) ? 1 : 0
197 // IDE registers bit definitions
199 // ATA Err Reg bitmap
201 #define BBK_ERR bit (7) ///< Bad block detected
202 #define UNC_ERR bit (6) ///< Uncorrectable Data
203 #define MC_ERR bit (5) ///< Media Change
204 #define IDNF_ERR bit (4) ///< ID Not Found
205 #define MCR_ERR bit (3) ///< Media Change Requested
206 #define ABRT_ERR bit (2) ///< Aborted Command
207 #define TK0NF_ERR bit (1) ///< Track 0 Not Found
208 #define AMNF_ERR bit (0) ///< Address Mark Not Found
211 // ATAPI Err Reg bitmap
213 #define SENSE_KEY_ERR (bit (7) | bit (6) | bit (5) | bit (4))
214 #define EOM_ERR bit (1) ///< End of Media Detected
215 #define ILI_ERR bit (0) ///< Illegal Length Indication
220 #define LBA_MODE bit (6)
235 #define BSY bit (7) ///< Controller Busy
236 #define DRDY bit (6) ///< Drive Ready
237 #define DWF bit (5) ///< Drive Write Fault
238 #define DSC bit (4) ///< Disk Seek Complete
239 #define DRQ bit (3) ///< Data Request
240 #define CORR bit (2) ///< Corrected Data
241 #define IDX bit (1) ///< Index
242 #define ERR bit (0) ///< Error
243 #define CHECK bit (0) ///< Check bit for ATAPI Status Reg
246 // Device Control Reg
248 #define SRST bit (2) ///< Software Reset
249 #define IEN_L bit (1) ///< Interrupt Enable
252 // ATAPI Feature Register
254 #define OVERLAP bit (1)
258 // ATAPI Interrupt Reason Reson Reg (ATA Sector Count Register)
260 #define RELEASE bit (2)
264 #define PACKET_CMD 0xA0
266 #define DEFAULT_CMD (0xa0)
268 // default content of device control register, disable INT
270 #define DEFAULT_CTL (0x0a)
271 #define MAX_ATAPI_BYTE_COUNT (0xfffe)
274 // function prototype
279 AtapiScsiPassThruDriverBindingSupported (
280 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
281 IN EFI_HANDLE Controller
,
282 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
287 AtapiScsiPassThruDriverBindingStart (
288 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
289 IN EFI_HANDLE Controller
,
290 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
295 AtapiScsiPassThruDriverBindingStop (
296 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
297 IN EFI_HANDLE Controller
,
298 IN UINTN NumberOfChildren
,
299 IN EFI_HANDLE
*ChildHandleBuffer
303 // EFI Component Name Functions
306 Retrieves a Unicode string that is the user readable name of the driver.
308 This function retrieves the user readable name of a driver in the form of a
309 Unicode string. If the driver specified by This has a user readable name in
310 the language specified by Language, then a pointer to the driver name is
311 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
312 by This does not support the language specified by Language,
313 then EFI_UNSUPPORTED is returned.
315 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
316 EFI_COMPONENT_NAME_PROTOCOL instance.
318 @param Language[in] A pointer to a Null-terminated ASCII string
319 array indicating the language. This is the
320 language of the driver name that the caller is
321 requesting, and it must match one of the
322 languages specified in SupportedLanguages. The
323 number of languages supported by a driver is up
324 to the driver writer. Language is specified
325 in RFC 3066 or ISO 639-2 language code format.
327 @param DriverName[out] A pointer to the Unicode string to return.
328 This Unicode string is the name of the
329 driver specified by This in the language
330 specified by Language.
332 @retval EFI_SUCCESS The Unicode string for the Driver specified by
333 This and the language specified by Language was
334 returned in DriverName.
336 @retval EFI_INVALID_PARAMETER Language is NULL.
338 @retval EFI_INVALID_PARAMETER DriverName is NULL.
340 @retval EFI_UNSUPPORTED The driver specified by This does not support
341 the language specified by Language.
346 AtapiScsiPassThruComponentNameGetDriverName (
347 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
349 OUT CHAR16
**DriverName
354 Retrieves a Unicode string that is the user readable name of the controller
355 that is being managed by a driver.
357 This function retrieves the user readable name of the controller specified by
358 ControllerHandle and ChildHandle in the form of a Unicode string. If the
359 driver specified by This has a user readable name in the language specified by
360 Language, then a pointer to the controller name is returned in ControllerName,
361 and EFI_SUCCESS is returned. If the driver specified by This is not currently
362 managing the controller specified by ControllerHandle and ChildHandle,
363 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
364 support the language specified by Language, then EFI_UNSUPPORTED is returned.
366 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
367 EFI_COMPONENT_NAME_PROTOCOL instance.
369 @param ControllerHandle[in] The handle of a controller that the driver
370 specified by This is managing. This handle
371 specifies the controller whose name is to be
374 @param ChildHandle[in] The handle of the child controller to retrieve
375 the name of. This is an optional parameter that
376 may be NULL. It will be NULL for device
377 drivers. It will also be NULL for a bus drivers
378 that wish to retrieve the name of the bus
379 controller. It will not be NULL for a bus
380 driver that wishes to retrieve the name of a
383 @param Language[in] A pointer to a Null-terminated ASCII string
384 array indicating the language. This is the
385 language of the driver name that the caller is
386 requesting, and it must match one of the
387 languages specified in SupportedLanguages. The
388 number of languages supported by a driver is up
389 to the driver writer. Language is specified in
390 RFC 3066 or ISO 639-2 language code format.
392 @param ControllerName[out] A pointer to the Unicode string to return.
393 This Unicode string is the name of the
394 controller specified by ControllerHandle and
395 ChildHandle in the language specified by
396 Language from the point of view of the driver
399 @retval EFI_SUCCESS The Unicode string for the user readable name in
400 the language specified by Language for the
401 driver specified by This was returned in
404 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
406 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
409 @retval EFI_INVALID_PARAMETER Language is NULL.
411 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
413 @retval EFI_UNSUPPORTED The driver specified by This is not currently
414 managing the controller specified by
415 ControllerHandle and ChildHandle.
417 @retval EFI_UNSUPPORTED The driver specified by This does not support
418 the language specified by Language.
423 AtapiScsiPassThruComponentNameGetControllerName (
424 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
425 IN EFI_HANDLE ControllerHandle
,
426 IN EFI_HANDLE ChildHandle OPTIONAL
,
428 OUT CHAR16
**ControllerName
433 AtapiScsiPassThruDriverEntryPoint
438 @todo Add function description
439 @todo ImageHandle - add argument description
440 @todo SystemTable - add argument description
441 @todo add return values
445 AtapiScsiPassThruDriverEntryPoint (
446 IN EFI_HANDLE ImageHandle
,
447 IN EFI_SYSTEM_TABLE
*SystemTable
452 RegisterAtapiScsiPassThru
458 @todo Add function description
459 @todo This add argument description
460 @todo Controller add argument description
461 @todo PciIo add argument description
462 @todo add return values
465 RegisterAtapiScsiPassThru (
466 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
467 IN EFI_HANDLE Controller
,
468 IN EFI_PCI_IO_PROTOCOL
*PciIo
473 AtapiScsiPassThruFunction
481 @todo Add function description
482 @todo This - add argument description
483 @todo Target - add argument description
484 @todo Lun - add argument description
485 @todo Packet - add argument description
486 @todo Event - add argument description
487 @todo add return values
491 AtapiScsiPassThruFunction (
492 IN EFI_SCSI_PASS_THRU_PROTOCOL
*This
,
495 IN OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
496 IN EFI_EVENT Event OPTIONAL
501 AtapiScsiPassThruGetNextDevice
503 TODO: Add function description
505 @param This TODO: add argument description
506 @param Target TODO: add argument description
507 @param Lun TODO: add argument description
509 TODO: add return values
514 AtapiScsiPassThruGetNextDevice (
515 IN EFI_SCSI_PASS_THRU_PROTOCOL
*This
,
516 IN OUT UINT32
*Target
,
522 AtapiScsiPassThruBuildDevicePath
524 TODO: Add function description
526 @param This TODO: add argument description
527 @param Target TODO: add argument description
528 @param Lun TODO: add argument description
529 @param DevicePath TODO: add argument description
531 TODO: add return values
536 AtapiScsiPassThruBuildDevicePath (
537 IN EFI_SCSI_PASS_THRU_PROTOCOL
*This
,
540 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
545 AtapiScsiPassThruGetTargetLun
547 TODO: Add function description
549 @param This TODO: add argument description
550 @param DevicePath TODO: add argument description
551 @param Target TODO: add argument description
552 @param Lun TODO: add argument description
554 TODO: add return values
559 AtapiScsiPassThruGetTargetLun (
560 IN EFI_SCSI_PASS_THRU_PROTOCOL
*This
,
561 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
568 AtapiScsiPassThruResetChannel
570 TODO: Add function description
572 @param This TODO: add argument description
574 TODO: add return values
579 AtapiScsiPassThruResetChannel (
580 IN EFI_SCSI_PASS_THRU_PROTOCOL
*This
585 AtapiScsiPassThruResetTarget
587 TODO: Add function description
589 @param This TODO: add argument description
590 @param Target TODO: add argument description
591 @param Lun TODO: add argument description
593 TODO: add return values
598 AtapiScsiPassThruResetTarget (
599 IN EFI_SCSI_PASS_THRU_PROTOCOL
*This
,
606 CheckSCSIRequestPacket
608 TODO: Add function description
610 @param Packet TODO: add argument description
612 TODO: add return values
616 CheckSCSIRequestPacket (
617 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
622 SubmitBlockingIoCommand
624 TODO: Add function description
626 @param AtapiScsiPrivate TODO: add argument description
627 @param Target TODO: add argument description
628 @param Packet TODO: add argument description
630 TODO: add return values
634 SubmitBlockingIoCommand (
635 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
637 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
644 TODO: Add function description
646 @param Packet - TODO: add argument description
648 @return TODO: add return values
653 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
660 TODO: Add function description
662 @param AtapiScsiPrivate TODO: add argument description
663 @param Target TODO: add argument description
664 @param Timeout TODO: add argument description
665 @param SenseData TODO: add argument description
666 @param SenseDataLength TODO: add argument description
668 TODO: add return values
672 RequestSenseCommand (
673 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
677 UINT8
*SenseDataLength
684 TODO: Add function description
686 @param AtapiScsiPrivate TODO: add argument description
687 @param Target TODO: add argument description
688 @param PacketCommand TODO: add argument description
689 @param Buffer TODO: add argument description
690 @param ByteCount TODO: add argument description
691 @param Direction TODO: add argument description
692 @param TimeOutInMicroSeconds TODO: add argument description
694 TODO: add return values
699 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
701 UINT8
*PacketCommand
,
704 DATA_DIRECTION Direction
,
705 UINT64 TimeOutInMicroSeconds
713 TODO: Add function description
715 @param PciIo TODO: add argument description
716 @param Port TODO: add argument description
718 TODO: add return values
723 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
732 TODO: Add function description
734 @param PciIo TODO: add argument description
735 @param Port TODO: add argument description
737 TODO: add return values
742 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
751 TODO: Add function description
753 @param PciIo TODO: add argument description
754 @param Port TODO: add argument description
755 @param Data TODO: add argument description
757 TODO: add return values
762 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
772 TODO: Add function description
774 @param PciIo TODO: add argument description
775 @param Port TODO: add argument description
776 @param Data TODO: add argument description
778 TODO: add return values
783 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
792 TODO: Add function description
794 @param AtapiScsiPrivate TODO: add argument description
795 @param TimeOutInMicroSeconds TODO: add argument description
797 TODO: add return values
802 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
803 UINT64 TimeOutInMicroSeconds
810 TODO: Add function description
812 @param AtapiScsiPrivate TODO: add argument description
813 @param TimeOutInMicroSeconds TODO: add argument description
815 TODO: add return values
820 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
821 UINT64 TimeOutInMicroSeconds
828 TODO: Add function description
830 @param AtapiScsiPrivate TODO: add argument description
831 @param TimeOutInMicroSeconds TODO: add argument description
833 TODO: add return values
838 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
839 UINT64 TimeOutInMicroSeconds
846 TODO: Add function description
848 @param AtapiScsiPrivate TODO: add argument description
849 @param TimeOutInMicroSeconds TODO: add argument description
851 TODO: add return values
856 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
857 UINT64 TimeOutInMicroSeconds
862 TODO: Add function description
864 @param AtapiScsiPrivate TODO: add argument description
865 @param TimeoutInMicroSeconds TODO: add argument description
867 TODO: add return values
871 StatusWaitForBSYClear (
872 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
873 UINT64 TimeoutInMicroSeconds
878 TODO: Add function description
880 @param AtapiScsiPrivate TODO: add argument description
881 @param TimeoutInMicroSeconds TODO: add argument description
883 TODO: add return values
887 AltStatusWaitForBSYClear (
888 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
889 UINT64 TimeoutInMicroSeconds
894 TODO: Add function description
896 @param AtapiScsiPrivate TODO: add argument description
897 @param TimeoutInMicroSeconds TODO: add argument description
899 TODO: add return values
904 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
905 UINT64 TimeoutInMicroSeconds
910 TODO: Add function description
912 @param AtapiScsiPrivate TODO: add argument description
913 @param TimeoutInMicroSeconds TODO: add argument description
915 TODO: add return values
920 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
921 UINT64 TimeoutInMicroSeconds
926 TODO: Add function description
928 @param AtapiScsiPrivate TODO: add argument description
929 @param Buffer TODO: add argument description
930 @param ByteCount TODO: add argument description
931 @param Direction TODO: add argument description
932 @param TimeOutInMicroSeconds TODO: add argument description
934 TODO: add return values
938 AtapiPassThruPioReadWriteData (
939 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
942 DATA_DIRECTION Direction
,
943 UINT64 TimeOutInMicroSeconds
948 TODO: Add function description
950 @param AtapiScsiPrivate TODO: add argument description
952 TODO: add return values
956 AtapiPassThruCheckErrorStatus (
957 ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
961 GetIdeRegistersBaseAddr (
962 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
963 OUT IDE_REGISTERS_BASE_ADDR
*IdeRegsBaseAddr
968 Get IDE IO port registers' base addresses by mode. In 'Compatibility' mode,
969 use fixed addresses. In Native-PCI mode, get base addresses from BARs in
970 the PCI IDE controller's Configuration Space.
973 PciIo - Pointer to the EFI_PCI_IO_PROTOCOL instance
974 IdeRegsBaseAddr - Pointer to IDE_REGISTERS_BASE_ADDR to
975 receive IDE IO port registers' base addresses
985 InitAtapiIoPortRegisters (
986 IN ATAPI_SCSI_PASS_THRU_DEV
*AtapiScsiPrivate
,
987 IN IDE_REGISTERS_BASE_ADDR
*IdeRegsBaseAddr
993 Initialize each Channel's Base Address of CommandBlock and ControlBlock.
997 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV
998 IdeRegsBaseAddr - The pointer of IDE_REGISTERS_BASE_ADDR