/** @file
Header file for AHCI mode of ATA host controller.
Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "AtaAtapiPassThru.h"
/**
read a one-byte data from a IDE port.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure
@param Port The IDE Port number
@return the one-byte data read from IDE port
**/
)
{
Data = 0;
//
// perform 1-byte data read from register
//
1,
&Data
);
return Data;
}
/**
write a 1-byte data to a specific IDE port.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure
@param Port The IDE port to be writen
@param Data The data to write to the port
**/
)
{
//
// perform 1-byte data write to register
//
1,
&Data
);
}
/**
write a 1-word data to a specific IDE port.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure
@param Port The IDE port to be writen
@param Data The data to write to the port
**/
)
{
//
// perform 1-word data write to register
//
1,
&Data
);
}
/**
write a 2-word data to a specific IDE port.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure
@param Port The IDE port to be writen
@param Data The data to write to the port
**/
)
{
//
// perform 2-word data write to register
//
1,
&Data
);
}
/**
Write multiple words of data to the IDE data port.
Call the IO abstraction once to do the complete read,
not one word at a time
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure
@param Port IO port to read
@param Count No. of UINT16's to read
@param Buffer Pointer to the data buffer for read
**/
)
{
//
// perform UINT16 data write to the FIFO
//
);
}
/**
Reads multiple words of data from the IDE data port.
Call the IO abstraction once to do the complete read,
not one word at a time
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure
@param Port IO port to read
@param Count Number of UINT16's to read
@param Buffer Pointer to the data buffer for read
**/
)
{
//
// Perform UINT16 data read from FIFO
//
);
}
/**
This function is used to analyze the Status Register and print out
some debug information and if there is ERR bit set in the Status
Register, the Error Register's value is also be parsed and print out.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
**/
)
{
if (AtaStatusBlock != NULL) {
//
// Dump the content of all ATA registers.
//
}
DEBUG_CODE_BEGIN ();
DEBUG ((EFI_D_ERROR, "CheckRegisterStatus()-- %02x : Error : Write Fault\n", StatusBlock.AtaStatus));
}
DEBUG ((EFI_D_ERROR, "CheckRegisterStatus()-- %02x : Error : Corrected Data\n", StatusBlock.AtaStatus));
}
DEBUG ((EFI_D_ERROR, "CheckRegisterStatus()-- %02x : Error : Bad Block Detected\n", StatusBlock.AtaError));
}
DEBUG ((EFI_D_ERROR, "CheckRegisterStatus()-- %02x : Error : Uncorrectable Data\n", StatusBlock.AtaError));
}
DEBUG ((EFI_D_ERROR, "CheckRegisterStatus()-- %02x : Error : Media Change\n", StatusBlock.AtaError));
}
}
DEBUG ((EFI_D_ERROR, "CheckRegisterStatus()-- %02x : Error : Track 0 Not Found\n", StatusBlock.AtaError));
}
DEBUG ((EFI_D_ERROR, "CheckRegisterStatus()-- %02x : Error : Address Mark Not Found\n", StatusBlock.AtaError));
}
}
DEBUG_CODE_END ();
}
/**
This function is used to analyze the Status Register at the condition that BSY is zero.
if there is ERR bit set in the Status Register, then return error.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@retval EFI_SUCCESS No err information in the Status Register.
@retval EFI_DEVICE_ERROR Any err information in the Status Register.
**/
)
{
if ((StatusRegister & ATA_STSREG_BSY) == 0) {
return EFI_SUCCESS;
} else {
return EFI_DEVICE_ERROR;
}
}
return EFI_SUCCESS;
}
/**
This function is used to poll for the DRQ bit clear in the Status
Register. DRQ is cleared when the device is finished transferring data.
So this function is called after data transfer is finished.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param Timeout The time to complete the command, uses 100ns as a unit.
@retval EFI_SUCCESS DRQ bit clear within the time out.
@retval EFI_TIMEOUT DRQ bit not clear within the time out.
@note
Read Status Register will clear interrupt status.
**/
DRQClear (
)
{
do {
//
// Wait for BSY == 0, then judge if DRQ is clear
//
if ((StatusRegister & ATA_STSREG_BSY) == 0) {
return EFI_DEVICE_ERROR;
} else {
return EFI_SUCCESS;
}
}
//
// Stall for 100 microseconds.
//
MicroSecondDelay (100);
Delay--;
} while (Delay > 0);
return EFI_TIMEOUT;
}
/**
This function is used to poll for the DRQ bit clear in the Alternate
Status Register. DRQ is cleared when the device is finished
transferring data. So this function is called after data transfer
is finished.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param Timeout The time to complete the command, uses 100ns as a unit.
@retval EFI_SUCCESS DRQ bit clear within the time out.
@retval EFI_TIMEOUT DRQ bit not clear within the time out.
@note Read Alternate Status Register will not clear interrupt status.
**/
)
{
do {
//
// Wait for BSY == 0, then judge if DRQ is clear
//
if ((AltRegister & ATA_STSREG_BSY) == 0) {
return EFI_DEVICE_ERROR;
} else {
return EFI_SUCCESS;
}
}
//
// Stall for 100 microseconds.
//
MicroSecondDelay (100);
Delay--;
} while (Delay > 0);
return EFI_TIMEOUT;
}
/**
This function is used to poll for the DRQ bit set in the
Status Register.
DRQ is set when the device is ready to transfer data. So this function
is called after the command is sent to the device and before required
data is transferred.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param Timeout The time to complete the command, uses 100ns as a unit.
@retval EFI_SUCCESS DRQ bit set within the time out.
@retval EFI_TIMEOUT DRQ bit not set within the time out.
@retval EFI_ABORTED DRQ bit not set caused by the command abort.
@note Read Status Register will clear interrupt status.
**/
DRQReady (
)
{
do {
//
// Read Status Register will clear interrupt
//
//
// Wait for BSY == 0, then judge if DRQ is clear or ERR is set
//
if ((StatusRegister & ATA_STSREG_BSY) == 0) {
return EFI_ABORTED;
}
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
} else {
return EFI_NOT_READY;
}
}
//
// Stall for 100 microseconds.
//
MicroSecondDelay (100);
Delay--;
} while (Delay > 0);
return EFI_TIMEOUT;
}
/**
This function is used to poll for the DRQ bit set in the Alternate Status Register.
DRQ is set when the device is ready to transfer data. So this function is called after
the command is sent to the device and before required data is transferred.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param Timeout The time to complete the command, uses 100ns as a unit.
@retval EFI_SUCCESS DRQ bit set within the time out.
@retval EFI_TIMEOUT DRQ bit not set within the time out.
@retval EFI_ABORTED DRQ bit not set caused by the command abort.
@note Read Alternate Status Register will not clear interrupt status.
**/
)
{
do {
//
// Read Alternate Status Register will not clear interrupt status
//
//
// Wait for BSY == 0, then judge if DRQ is clear or ERR is set
//
if ((AltRegister & ATA_STSREG_BSY) == 0) {
return EFI_ABORTED;
}
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
} else {
return EFI_NOT_READY;
}
}
//
// Stall for 100 microseconds.
//
MicroSecondDelay (100);
Delay--;
} while (Delay > 0);
return EFI_TIMEOUT;
}
/**
This function is used to poll for the DRDY bit set in the Status Register. DRDY
bit is set when the device is ready to accept command. Most ATA commands must be
sent after DRDY set except the ATAPI Packet Command.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param Timeout The time to complete the command, uses 100ns as a unit.
@retval EFI_SUCCESS DRDY bit set within the time out.
@retval EFI_TIMEOUT DRDY bit not set within the time out.
@note Read Status Register will clear interrupt status.
**/
)
{
do {
//
// Wait for BSY == 0, then judge if DRDY is set or ERR is set
//
if ((StatusRegister & ATA_STSREG_BSY) == 0) {
return EFI_ABORTED;
}
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
} else {
return EFI_DEVICE_ERROR;
}
}
//
// Stall for 100 microseconds.
//
MicroSecondDelay (100);
Delay--;
} while (Delay > 0);
return EFI_TIMEOUT;
}
/**
This function is used to poll for the DRDY bit set in the Alternate Status Register.
DRDY bit is set when the device is ready to accept command. Most ATA commands must
be sent after DRDY set except the ATAPI Packet Command.
@param PciIo A pointer to EFI_PCI_IO_PROTOCOL data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param Timeout The time to complete the command, uses 100ns as a unit.
@retval EFI_SUCCESS DRDY bit set within the time out.
@retval EFI_TIMEOUT DRDY bit not set within the time out.
@note Read Alternate Status Register will clear interrupt status.
**/
)
{
do {
//
// Wait for BSY == 0, then judge if DRDY is set or ERR is set
//
if ((AltRegister & ATA_STSREG_BSY) == 0) {
return EFI_ABORTED;
}
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
} else {
return EFI_DEVICE_ERROR;
}
}
//
// Stall for 100 microseconds.
//
MicroSecondDelay (100);
Delay--;
} while (Delay > 0);
return EFI_TIMEOUT;
}
/**
This function is used to poll for the BSY bit clear in the Status Register. BSY
is clear when the device is not busy. Every command must be sent after device is not busy.
@param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param Timeout The time to complete the command, uses 100ns as a unit.
@retval EFI_SUCCESS BSY bit clear within the time out.
@retval EFI_TIMEOUT BSY bit not clear within the time out.
@note Read Status Register will clear interrupt status.
**/
)
{
do {
return EFI_SUCCESS;
}
//
// Stall for 100 microseconds.
//
MicroSecondDelay (100);
Delay--;
} while (Delay > 0);
return EFI_TIMEOUT;
}
/**
This function is used to poll for the BSY bit clear in the Status Register. BSY
is clear when the device is not busy. Every command must be sent after device is not busy.
@param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param Timeout The time to complete the command, uses 100ns as a unit.
@retval EFI_SUCCESS BSY bit clear within the time out.
@retval EFI_TIMEOUT BSY bit not clear within the time out.
@note Read Status Register will clear interrupt status.
**/
)
{
do {
return EFI_SUCCESS;
}
//
// Stall for 100 microseconds.
//
MicroSecondDelay (100);
Delay--;
} while (Delay > 0);
return EFI_TIMEOUT;
}
/**
Get IDE i/o port registers' base addresses by mode.
In 'Compatibility' mode, use fixed addresses.
In Native-PCI mode, get base addresses from BARs in the PCI IDE controller's
Configuration Space.
The steps to get IDE i/o port registers' base addresses for each channel
as follows:
1. Examine the Programming Interface byte of the Class Code fields in PCI IDE
controller's Configuration Space to determine the operating mode.
2. a) In 'Compatibility' mode, use fixed addresses shown in the Table 1 below.
___________________________________________
| | Command Block | Control Block |
| Channel | Registers | Registers |
|___________|_______________|_______________|
| Primary | 1F0h - 1F7h | 3F6h - 3F7h |
|___________|_______________|_______________|
| Secondary | 170h - 177h | 376h - 377h |
|___________|_______________|_______________|
Table 1. Compatibility resource mappings
b) In Native-PCI mode, IDE registers are mapped into IO space using the BARs
in IDE controller's PCI Configuration Space, shown in the Table 2 below.
___________________________________________________
| | Command Block | Control Block |
| Channel | Registers | Registers |
|___________|___________________|___________________|
| Primary | BAR at offset 0x10| BAR at offset 0x14|
|___________|___________________|___________________|
| Secondary | BAR at offset 0x18| BAR at offset 0x1C|
|___________|___________________|___________________|
Table 2. BARs for Register Mapping
@param[in] PciIo Pointer to the EFI_PCI_IO_PROTOCOL instance
@param[in, out] IdeRegisters Pointer to EFI_IDE_REGISTERS which is used to
store the IDE i/o port registers' base addresses
@retval EFI_UNSUPPORTED Return this value when the BARs is not IO type
@retval EFI_SUCCESS Get the Base address successfully
@retval Other Read the pci configureation data error
**/
)
{
return EFI_INVALID_PARAMETER;
}
0,
sizeof (PciData),
);
return Status;
}
CommandBlockBaseAddr = 0x1f0;
ControlBlockBaseAddr = 0x3f6;
} else {
//
// The BARs should be of IO type
//
return EFI_UNSUPPORTED;
}
}
//
// Calculate IDE primary channel I/O register base address.
//
CommandBlockBaseAddr = 0x170;
ControlBlockBaseAddr = 0x376;
} else {
//
// The BARs should be of IO type
//
return EFI_UNSUPPORTED;
}
}
//
// Calculate IDE secondary channel I/O register base address.
//
return EFI_SUCCESS;
}
/**
This function is used to implement the Soft Reset on the specified device. But,
the ATA Soft Reset mechanism is so strong a reset method that it will force
resetting on both devices connected to the same cable.
It is called by IdeBlkIoReset(), a interface function of Block
I/O protocol.
This function can also be used by the ATAPI device to perform reset when
ATAPI Reset command is failed.
@param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param Timeout The time to complete the command, uses 100ns as a unit.
@retval EFI_SUCCESS Soft reset completes successfully.
@retval EFI_DEVICE_ERROR Any step during the reset process is failed.
@note The registers initial values after ATA soft reset are different
to the ATA device and ATAPI device.
**/
)
{
DeviceControl = 0;
//
// disable Interrupt and set SRST bit to initiate soft reset
//
//
// SRST should assert for at least 5 us, we use 10 us for
// better compatibility
//
MicroSecondDelay (10);
//
// Enable interrupt to support UDMA, and clear SRST bit
//
DeviceControl = 0;
//
// Wait for at least 10 ms to check BSY status, we use 10 ms
// for better compatibility
//
MicroSecondDelay (10000);
//
// slave device needs at most 31ms to clear BSY
//
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
}
/**
Send ATA Ext command into device with NON_DATA protocol.
@param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param AtaCommandBlock A pointer to EFI_ATA_COMMAND_BLOCK data structure.
@param Timeout The time to complete the command, uses 100ns as a unit.
@retval EFI_SUCCESS Reading succeed
@retval EFI_DEVICE_ERROR Error executing commands on this device.
**/
)
{
return EFI_DEVICE_ERROR;
}
//
// Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
//
//
// set all the command parameters
// Before write to all the following registers, BSY and DRQ must be 0.
//
return EFI_DEVICE_ERROR;
}
//
// Fill the feature register, which is a two-byte FIFO. Need write twice.
//
//
// Fill the sector count register, which is a two-byte FIFO. Need write twice.
//
//
// Fill the start LBA registers, which are also two-byte FIFO
//
//
// Send command via Command Register
//
//
// Stall at least 400 microseconds.
//
MicroSecondDelay (400);
return EFI_SUCCESS;
}
/**
This function is used to send out ATA commands conforms to the PIO Data In Protocol.
@param[in] PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data
structure.
@param[in] IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param[in, out] Buffer A pointer to the source buffer for the data.
@param[in] ByteCount The length of the data.
@param[in] Read Flag used to determine the data transfer direction.
Read equals 1, means data transferred from device
to host;Read equals 0, means data transferred
from host to device.
@param[in] AtaCommandBlock A pointer to EFI_ATA_COMMAND_BLOCK data structure.
@param[in, out] AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
@param[in] Timeout The time to complete the command, uses 100ns as a unit.
@param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
used by non-blocking mode.
@retval EFI_SUCCESS send out the ATA command and device send required data successfully.
@retval EFI_DEVICE_ERROR command sent failed.
**/
)
{
return EFI_INVALID_PARAMETER;
}
//
// Issue ATA command
//
goto Exit;
}
//
// According to PIO data in protocol, host can perform a series of reads to
// the data register after each time device set DRQ ready;
// The data size of "a series of read" is command specific.
// For most ATA command, data size received from device will not exceed
// 1 sector, hence the data size for "a series of read" can be the whole data
// size of one command request.
// For ATA command such as Read Sector command, the data size of one ATA
// command request is often larger than 1 sector, according to the
// Read Sector command, the data size of "a series of read" is exactly 1
// sector.
// Here for simplification reason, we specify the data size for
// "a series of read" to 1 sector (256 words) if data size of one ATA command
// request is larger than 256 words.
//
Increment = 256;
//
// used to record bytes of currently transfered data
//
WordCount = 0;
//
// Poll DRQ bit set, data transfer can be performed only when DRQ is ready
//
goto Exit;
}
//
// Get the byte count for one series of read
//
}
if (Read) {
);
} else {
);
}
goto Exit;
}
}
goto Exit;
}
Exit:
//
// Dump All Ide registers to ATA_STATUS_BLOCK
//
//
// Not support the Non-blocking now,just do the blocking process.
//
return Status;
}
/**
Send ATA command into device with NON_DATA protocol
@param[in] PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE
data structure.
@param[in] IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param[in] AtaCommandBlock A pointer to EFI_ATA_COMMAND_BLOCK data
structure.
@param[in, out] AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
@param[in] Timeout The time to complete the command, uses 100ns as a unit.
@param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
used by non-blocking mode.
@retval EFI_SUCCESS Reading succeed
@retval EFI_ABORTED Command failed
@retval EFI_DEVICE_ERROR Device status error.
**/
)
{
return EFI_INVALID_PARAMETER;
}
//
// Issue ATA command
//
goto Exit;
}
//
// Wait for command completion
//
goto Exit;
}
goto Exit;
}
Exit:
//
// Dump All Ide registers to ATA_STATUS_BLOCK
//
//
// Not support the Non-blocking now,just do the blocking process.
//
return Status;
}
/**
Wait for memory to be set.
@param[in] PciIo The PCI IO protocol instance.
@param[in] IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@retval EFI_DEVICE_ERROR The memory is not set.
@retval EFI_TIMEOUT The memory setting is time out.
@retval EFI_SUCCESS The memory is correct set.
**/
)
{
Timeout = 2000;
while (TRUE) {
break;
}
break;
}
if ((RegisterValue & BMIS_INTERRUPT) != 0) {
break;
}
//
// Stall for 1 milliseconds.
//
MicroSecondDelay (1000);
Timeout--;
}
return Status;
}
/**
Check if the memory to be set.
@param[in] PciIo The PCI IO protocol instance.
@param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
used by non-blocking mode.
@param[in] IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@retval EFI_DEVICE_ERROR The memory setting met a issue.
@retval EFI_NOT_READY The memory is not set.
@retval EFI_TIMEOUT The memory setting is time out.
@retval EFI_SUCCESS The memory is correct set.
**/
)
{
Task->RetryTimes--;
return EFI_DEVICE_ERROR;
}
if ((RegisterValue & BMIS_ERROR) != 0) {
return EFI_DEVICE_ERROR;
}
if ((RegisterValue & BMIS_INTERRUPT) != 0) {
return EFI_SUCCESS;
}
if (Task->RetryTimes == 0) {
return EFI_TIMEOUT;
} else {
//
// The memory is not set.
//
return EFI_NOT_READY;
}
}
/**
Perform an ATA Udma operation (Read, ReadExt, Write, WriteExt).
@param[in] Instance A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data
structure.
@param[in] IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param[in] Read Flag used to determine the data transfer
direction. Read equals 1, means data transferred
from device to host;Read equals 0, means data
transferred from host to device.
@param[in] DataBuffer A pointer to the source buffer for the data.
@param[in] DataLength The length of the data.
@param[in] AtaCommandBlock A pointer to EFI_ATA_COMMAND_BLOCK data structure.
@param[in, out] AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
@param[in] Timeout The time to complete the command, uses 100ns as a unit.
@param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
used by non-blocking mode.
@retval EFI_SUCCESS the operation is successful.
@retval EFI_OUT_OF_RESOURCES Build PRD table failed
@retval EFI_UNSUPPORTED Unknown channel or operations command
@retval EFI_DEVICE_ERROR Ata command execute failed
**/
)
{
PrdBaseAddr = NULL;
PrdTableMap = NULL;
PageCount = 0;
if ((PciIo == NULL) || (IdeRegisters == NULL) || (DataBuffer == NULL) || (AtaCommandBlock == NULL)) {
return EFI_INVALID_PARAMETER;
}
//
// Before starting the Blocking BlockIO operation, push to finish all non-blocking
// BlockIO tasks.
// Delay 1ms to simulate the blocking time out checking.
//
//
// Stall for 1 milliseconds.
//
MicroSecondDelay (1000);
}
//
// The data buffer should be even alignment
//
return EFI_INVALID_PARAMETER;
}
//
// Set relevant IO Port address.
//
//
// For Blocking mode, start the command.
// For non-blocking mode, when the command is not started, start it, otherwise
// go to check the status.
//
//
// Calculate the number of PRD entry.
// Every entry in PRD table can specify a 64K memory region.
//
//
// Make sure that the memory region of PRD table is not cross 64K boundary
//
if (PrdTableSize > 0x10000) {
return EFI_INVALID_PARAMETER;
}
//
// Allocate buffer for PRD table initialization.
//
(VOID **)&PrdBaseAddr,
0
);
return EFI_OUT_OF_RESOURCES;
}
);
//
// If the data length actually mapped is not equal to the requested amount,
// it means the DMA operation may be broken into several discontinuous smaller chunks.
// Can't handle this case.
//
return EFI_OUT_OF_RESOURCES;
}
//
// Map the host address of DataBuffer to DMA master address.
//
if (Read) {
} else {
}
);
return EFI_OUT_OF_RESOURCES;
}
//
// According to Ata spec, it requires the buffer address and size to be even.
//
//
// Fill the PRD table with appropriate bus master address of data buffer and data length.
//
while (ByteRemaining != 0) {
if (ByteRemaining <= 0x10000) {
break;
}
ByteRemaining -= 0x10000;
BufferMapAddress += 0x10000;
}
//
// Start to enable the DMA operation
//
//
// Enable interrupt to support UDMA
//
DeviceControl = 0;
//
// Read BMIS register and clear ERROR and INTR bit
//
//
// Set the base address to BMID register
//
//
// Set BMIC register to identify the operation direction
//
if (Read) {
} else {
}
//
// Max transfer number of sectors for one command is 65536(32Mbyte),
// it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
// So set the variable Count to 2000, for about 2 second Timeout time.
//
}
//
// Issue ATA command
//
goto Exit;
}
goto Exit;
}
//
// Set START bit of BMIC register
//
}
//
// Check the INTERRUPT and ERROR bit of BMIS
// Max transfer number of sectors for one command is 65536(32Mbyte),
// it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
// So set the variable Count to 2000, for about 2 second Timeout time.
//
} else {
}
//
// For blocking mode, clear registers and free buffers.
// For non blocking mode, when the related registers have been set or time
// out, or a error has been happened, it needs to clear the register and free
// buffer.
//
//
// Read BMIS register and clear ERROR and INTR bit
//
//
// Read Status Register of IDE device to clear interrupt
//
//
// Clear START bit of BMIC register
//
//
// Disable interrupt of Select device
//
//
// Stall for 10 milliseconds.
//
MicroSecondDelay (10000);
}
Exit:
//
// Free all allocated resource
//
} else {
}
//
// Dump All Ide registers to ATA_STATUS_BLOCK
//
}
return Status;
}
/**
This function reads the pending data in the device.
@param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@retval EFI_SUCCESS Successfully read.
@retval EFI_NOT_READY The BSY is set avoiding reading.
**/
)
{
return EFI_NOT_READY;
}
1,
);
}
}
return EFI_SUCCESS;
}
/**
This function is called by AtaPacketCommandExecute().
It is used to transfer data between host and device. The data direction is specified
by the fourth parameter.
@param PciIo A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param IdeRegisters A pointer to EFI_IDE_REGISTERS data structure.
@param Buffer Buffer contained data transferred between host and device.
@param ByteCount Data size in byte unit of the buffer.
@param Read Flag used to determine the data transfer direction.
Read equals 1, means data transferred from device to host;
Read equals 0, means data transferred from host to device.
@param Timeout Timeout value for wait DRQ ready before each data stream's transfer
, uses 100ns as a unit.
@retval EFI_SUCCESS data is transferred successfully.
@retval EFI_DEVICE_ERROR the device failed to transfer data.
**/
)
{
//
// No data transfer is premitted.
//
if (ByteCount == 0) {
return EFI_SUCCESS;
}
//
// ActuralWordCount means the word count of data really transferred.
//
ActualWordCount = 0;
while (ActualWordCount < RequiredWordCount) {
//
// before each data transfer stream, the host should poll DRQ bit ready,
// to see whether indicates device is ready to transfer data.
//
}
//
// get current data transfer size from Cylinder Registers.
//
WordCount /= 2;
if (Read) {
);
} else {
);
}
//
// read status register to check whether error happens.
//
return EFI_DEVICE_ERROR;
}
}
if (Read) {
//
// In the case where the drive wants to send more data than we need to read,
// the DRQ bit will be set and cause delays from DRQClear2().
// We need to read data from the drive until it clears DRQ so we can move on.
//
}
//
// read status register to check whether error happens.
//
return EFI_DEVICE_ERROR;
}
//
// After data transfer is completed, normally, DRQ bit should clear.
//
return EFI_DEVICE_ERROR;
}
return Status;
}
/**
Sumbit ATAPI request sense command.
@param[in] PciIo Pointer to the EFI_PCI_IO_PROTOCOL instance
@param[in] IdeRegisters Pointer to EFI_IDE_REGISTERS which is used to
store the IDE i/o port registers' base addresses
@param[in] Channel The channel number of device.
@param[in] Device The device number of device.
@param[in] SenseData A pointer to store sense data.
@param[in] SenseDataLength The sense data length.
@param[in] Timeout The timeout value to execute this cmd, uses 100ns as a unit.
@retval EFI_SUCCESS Send out the ATAPI packet command successfully.
@retval EFI_DEVICE_ERROR The device failed to send data.
**/
)
{
Cdb[0] = ATA_CMD_REQUEST_SENSE;
return Status;
}
/**
This function is used to send out ATAPI commands conforms to the Packet Command
with PIO Data In Protocol.
@param[in] PciIo Pointer to the EFI_PCI_IO_PROTOCOL instance
@param[in] IdeRegisters Pointer to EFI_IDE_REGISTERS which is used to
store the IDE i/o port registers' base addresses
@param[in] Channel The channel number of device.
@param[in] Device The device number of device.
@param[in] Packet A pointer to EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET data structure.
@retval EFI_SUCCESS send out the ATAPI packet command successfully
and device sends data successfully.
@retval EFI_DEVICE_ERROR the device failed to send data.
**/
)
{
//
// Fill ATAPI Command Packet according to CDB.
// For Atapi cmd, its length should be less than or equal to 12 bytes.
//
return EFI_INVALID_PARAMETER;
}
//
// No OVL; No DMA
//
//
// set the transfersize to ATAPI_MAX_BYTE_COUNT to let the device
// determine how many data should be transferred.
//
//
// Disable interrupt
//
//
// Issue ATA PACKET command firstly
//
return Status;
}
return Status;
}
//
// Send out ATAPI command packet
//
//
// Stall for 10 microseconds.
//
MicroSecondDelay (10);
}
//
//
TRUE,
);
} else {
);
}
if (!EFI_ERROR (PacketCommandStatus)) {
return PacketCommandStatus;
}
//
// Return SenseData if PacketCommandStatus matches
// the following return codes.
//
if ((PacketCommandStatus == EFI_BAD_BUFFER_SIZE) ||
(PacketCommandStatus == EFI_DEVICE_ERROR) ||
(PacketCommandStatus == EFI_TIMEOUT)) {
//
// avoid submit request sense command continuously.
//
return PacketCommandStatus;
}
);
}
return PacketCommandStatus;
}
/**
Set the calculated Best transfer mode to a detected device.
@param Instance A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param Channel The channel number of device.
@param Device The device number of device.
@param TransferMode A pointer to EFI_ATA_TRANSFER_MODE data structure.
@param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
@retval EFI_SUCCESS Set transfer mode successfully.
@retval EFI_DEVICE_ERROR Set transfer mode failed.
@retval EFI_OUT_OF_RESOURCES Allocate memory failed.
**/
)
{
//
// Send SET FEATURE command (sub command 0x03) to set pio mode.
//
);
return Status;
}
/**
Set drive parameters for devices not support PACKETS command.
@param Instance A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param Channel The channel number of device.
@param Device The device number of device.
@param DriveParameters A pointer to EFI_ATA_DRIVE_PARMS data structure.
@param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
@retval EFI_SUCCESS Set drive parameter successfully.
@retval EFI_DEVICE_ERROR Set drive parameter failed.
@retval EFI_OUT_OF_RESOURCES Allocate memory failed.
**/
)
{
//
// Send Init drive parameters
//
);
//
// Send Set Multiple parameters
//
);
return Status;
}
/**
Send SMART Return Status command to check if the execution of SMART cmd is successful or not.
@param Instance A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param Channel The channel number of device.
@param Device The device number of device.
@param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
@retval EFI_SUCCESS Successfully get the return status of S.M.A.R.T command execution.
@retval Others Fail to get return status data.
**/
)
{
//
// Send S.M.A.R.T Read Return Status command to device
//
);
return EFI_DEVICE_ERROR;
}
//
// The threshold exceeded condition is not detected by the device
//
//
// The threshold exceeded condition is detected by the device
//
}
return EFI_SUCCESS;
}
/**
Enable SMART command of the disk if supported.
@param Instance A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param Channel The channel number of device.
@param Device The device number of device.
@param IdentifyData A pointer to data buffer which is used to contain IDENTIFY data.
@param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
**/
)
{
//
// Detect if the device supports S.M.A.R.T.
//
//
// S.M.A.R.T is not supported by the device
//
} else {
//
// Check if the feature is enabled. If not, then enable S.M.A.R.T.
//
//
// Send S.M.A.R.T Enable command to device
//
);
//
// Send S.M.A.R.T AutoSave command to device
//
);
);
}
}
}
}
return ;
}
/**
Sends out an ATA Identify Command to the specified device.
This function is called by DiscoverIdeDevice() during its device
identification. It sends out the ATA Identify Command to the
specified device. Only ATA device responses to this command. If
the command succeeds, it returns the Identify data structure which
contains information about the device. This function extracts the
information it needs to fill the IDE_BLK_IO_DEV data structure,
including device type, media block size, media capacity, and etc.
@param Instance A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param Channel The channel number of device.
@param Device The device number of device.
@param Buffer A pointer to data buffer which is used to contain IDENTIFY data.
@param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
@retval EFI_SUCCESS Identify ATA device successfully.
@retval EFI_DEVICE_ERROR ATA Identify Device Command failed or device is not ATA device.
@retval EFI_OUT_OF_RESOURCES Allocate memory failed.
**/
)
{
sizeof (EFI_IDENTIFY_DATA),
TRUE,
);
return Status;
}
/**
This function is called by DiscoverIdeDevice() during its device
identification.
Its main purpose is to get enough information for the device media
to fill in the Media data structure of the Block I/O Protocol interface.
There are 5 steps to reach such objective:
1. Sends out the ATAPI Identify Command to the specified device.
Only ATAPI device responses to this command. If the command succeeds,
it returns the Identify data structure which filled with information
about the device. Since the ATAPI device contains removable media,
the only meaningful information is the device module name.
2. Sends out ATAPI Inquiry Packet Command to the specified device.
This command will return inquiry data of the device, which contains
the device type information.
3. Allocate sense data space for future use. We don't detect the media
presence here to improvement boot performance, especially when CD
media is present. The media detection will be performed just before
@param Instance A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param Channel The channel number of device.
@param Device The device number of device.
@param Buffer A pointer to data buffer which is used to contain IDENTIFY data.
@param AtaStatusBlock A pointer to EFI_ATA_STATUS_BLOCK data structure.
@retval EFI_SUCCESS Identify ATAPI device successfully.
@retval EFI_DEVICE_ERROR ATA Identify Packet Device Command failed or device type
is not supported by this IDE driver.
@retval EFI_OUT_OF_RESOURCES Allocate memory failed.
**/
)
{
//
// Send ATAPI Identify Command to get IDENTIFY data.
//
sizeof (EFI_IDENTIFY_DATA),
TRUE,
);
return Status;
}
/**
This function is used for detect whether the IDE device exists in the
specified Channel as the specified Device Number.
There is two IDE channels: one is Primary Channel, the other is
Secondary Channel.(Channel is the logical name for the physical "Cable".)
Different channel has different register group.
On each IDE channel, at most two IDE devices attach,
one is called Device 0 (Master device), the other is called Device 1
(Slave device). The devices on the same channel co-use the same register
group, so before sending out a command for a specified device via command
register, it is a must to select the current device to accept the command
@param Instance A pointer to ATA_ATAPI_PASS_THRU_INSTANCE data structure.
@param IdeChannel The channel number of device.
@retval EFI_SUCCESS successfully detects device.
@retval other any failure during detection process will return this value.
**/
)
{
//
// Send ATA Device Execut Diagnostic command.
// This command should work no matter DRDY is ready or not
//
DEBUG((EFI_D_ERROR, "New detecting method: Send Execute Diagnostic Command: WaitForBSYClear: Status: %d\n", Status));
continue;
}
//
// Select Master or Slave device to get the return signature for ATA DEVICE DIAGNOSTIC cmd.
//
//
// Stall for 1 milliseconds.
//
MicroSecondDelay (1000);
//
//
} else {
continue;
}
//
// Send IDENTIFY cmd to the device to test if it is really attached.
//
if (DeviceType == EfiIdeHarddisk) {
//
// if identifying ata device is failure, then try to send identify packet cmd.
//
}
} else {
//
// if identifying atapi device is failure, then try to send identify cmd.
//
}
}
//
// No device is found at this port
//
continue;
}
//
// If the device is a hard disk, then try to enable S.M.A.R.T feature
//
if (DeviceType == EfiIdeHarddisk) {
&Buffer,
);
}
//
// Submit identify data to IDE controller init driver
//
//
// Now start to config ide device parameter and transfer mode.
//
);
continue;
}
//
// Set best supported PIO mode on this IDE device
//
} else {
}
if (SupportedModes->ExtModeCount == 0){
continue;
}
}
//
// Set supported DMA mode on this IDE device. Note that UDMA & MDMA cann't
// be set together. Only one DMA mode can be set to a device. If setting
// DMA mode operation fails, we can continue moving on because we only use
// PIO mode at boot time. DMA modes are used by certain kind of OS booting
//
continue;
}
continue;
}
}
//
// Set Parameters for the device:
// 1) Init
//
if (DeviceType == EfiIdeHarddisk) {
//
// Init driver parameters
//
DriveParameters.MultipleSector = (UINT8) ((ATA5_IDENTIFY_DATA *)(&Buffer.AtaData))->multi_sector_cmd_max_sct_cnt;
}
//
// Set IDE controller Timing Blocks in the PCI Configuration Space
//
//
// IDE controller and IDE device timing is configured successfully.
// Now insert the device into device list.
//
continue;
}
if (DeviceType == EfiIdeHarddisk) {
}
}
return EFI_SUCCESS;
}
/**
Initialize ATA host controller at IDE mode.
The function is designed to initialize ATA host controller.
@param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
**/
)
{
//
// Obtain IDE IO port registers' base addresses
//
goto ErrorExit;
}
//
// now obtain channel information fron IdeControllerInit protocol.
//
);
continue;
}
if (!ChannelEnabled) {
continue;
}
//
// Now inform the IDE Controller Init Module.
//
//
// No reset channel function implemented.
//
//
// Now inform the IDE Controller Init Module.
//
//
// Detect all attached ATA devices and set the transfer mode for each device.
//
}
//
// All configurations done! Notify IdeController to do post initialization
// work such as saving IDE controller PCI settings for S3 resume
//
return Status;
}