2009-12-22 08:35:49 +01:00
|
|
|
/** @file
|
|
|
|
This file implements ATA pass through transaction for ATA bus driver.
|
|
|
|
|
|
|
|
This file implements the low level execution of ATA pass through transaction.
|
|
|
|
It transforms the high level identity, read/write, reset command to ATA pass
|
2011-05-04 04:38:43 +02:00
|
|
|
through command and protocol.
|
|
|
|
|
2011-05-19 11:40:42 +02:00
|
|
|
NOTE: This file also implements the StorageSecurityCommandProtocol(SSP). For input
|
2012-03-16 04:31:47 +01:00
|
|
|
parameter SecurityProtocolSpecificData, ATA spec has no explicitly definition
|
|
|
|
for Security Protocol Specific layout. This implementation uses big endian for
|
2011-05-04 04:38:43 +02:00
|
|
|
Cylinder register.
|
2012-03-16 04:31:47 +01:00
|
|
|
|
2013-06-18 05:36:11 +02:00
|
|
|
Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
|
2010-04-24 11:49:11 +02:00
|
|
|
This program and the accompanying materials
|
2009-12-22 08:35:49 +01:00
|
|
|
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
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
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 "AtaBus.h"
|
|
|
|
|
2011-05-04 04:38:43 +02:00
|
|
|
#define ATA_CMD_TRUST_NON_DATA 0x5B
|
|
|
|
#define ATA_CMD_TRUST_RECEIVE 0x5C
|
|
|
|
#define ATA_CMD_TRUST_RECEIVE_DMA 0x5D
|
|
|
|
#define ATA_CMD_TRUST_SEND 0x5E
|
|
|
|
#define ATA_CMD_TRUST_SEND_DMA 0x5F
|
|
|
|
|
2009-12-22 08:35:49 +01:00
|
|
|
//
|
|
|
|
// Look up table (UdmaValid, IsWrite) for EFI_ATA_PASS_THRU_CMD_PROTOCOL
|
|
|
|
//
|
|
|
|
EFI_ATA_PASS_THRU_CMD_PROTOCOL mAtaPassThruCmdProtocols[][2] = {
|
|
|
|
{
|
|
|
|
EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN,
|
|
|
|
EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN,
|
|
|
|
EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Look up table (UdmaValid, Lba48Bit, IsIsWrite) for ATA_CMD
|
|
|
|
//
|
|
|
|
UINT8 mAtaCommands[][2][2] = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ATA_CMD_READ_SECTORS, // 28-bit LBA; PIO read
|
|
|
|
ATA_CMD_WRITE_SECTORS // 28-bit LBA; PIO write
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ATA_CMD_READ_SECTORS_EXT, // 48-bit LBA; PIO read
|
|
|
|
ATA_CMD_WRITE_SECTORS_EXT // 48-bit LBA; PIO write
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ATA_CMD_READ_DMA, // 28-bit LBA; DMA read
|
|
|
|
ATA_CMD_WRITE_DMA // 28-bit LBA; DMA write
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ATA_CMD_READ_DMA_EXT, // 48-bit LBA; DMA read
|
|
|
|
ATA_CMD_WRITE_DMA_EXT // 48-bit LBA; DMA write
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-04 04:38:43 +02:00
|
|
|
//
|
|
|
|
// Look up table (UdmaValid, IsTrustSend) for ATA_CMD
|
|
|
|
//
|
2012-03-16 04:31:47 +01:00
|
|
|
UINT8 mAtaTrustCommands[2][2] = {
|
2011-05-04 04:38:43 +02:00
|
|
|
{
|
|
|
|
ATA_CMD_TRUST_RECEIVE, // PIO read
|
|
|
|
ATA_CMD_TRUST_SEND // PIO write
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ATA_CMD_TRUST_RECEIVE_DMA, // DMA read
|
|
|
|
ATA_CMD_TRUST_SEND_DMA // DMA write
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-12-22 08:35:49 +01:00
|
|
|
//
|
|
|
|
// Look up table (Lba48Bit) for maximum transfer block number
|
|
|
|
//
|
|
|
|
UINTN mMaxTransferBlockNumber[] = {
|
|
|
|
MAX_28BIT_TRANSFER_BLOCK_NUM,
|
|
|
|
MAX_48BIT_TRANSFER_BLOCK_NUM
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
|
|
|
|
|
|
|
|
This function wraps the PassThru() invocation for ATA pass through function
|
|
|
|
for an ATA device. It assembles the ATA pass through command packet for ATA
|
|
|
|
transaction.
|
|
|
|
|
2011-05-03 12:31:41 +02:00
|
|
|
@param[in, out] AtaDevice The ATA child device involved for the operation.
|
2012-03-16 04:31:47 +01:00
|
|
|
@param[in, out] TaskPacket Pointer to a Pass Thru Command Packet. Optional,
|
2011-05-03 12:31:41 +02:00
|
|
|
if it is NULL, blocking mode, and use the packet
|
|
|
|
in AtaDevice. If it is not NULL, non blocking mode,
|
|
|
|
and pass down this Packet.
|
2011-05-19 08:12:58 +02:00
|
|
|
@param[in, out] Event If Event is NULL, then blocking I/O is performed.
|
2011-05-03 12:31:41 +02:00
|
|
|
If Event is not NULL and non-blocking I/O is
|
|
|
|
supported,then non-blocking I/O is performed,
|
|
|
|
and Event will be signaled when the write
|
|
|
|
request is completed.
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
@return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
AtaDevicePassThru (
|
2011-05-03 12:31:41 +02:00
|
|
|
IN OUT ATA_DEVICE *AtaDevice,
|
|
|
|
IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *TaskPacket, OPTIONAL
|
|
|
|
IN OUT EFI_EVENT Event OPTIONAL
|
2009-12-22 08:35:49 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
|
|
|
|
EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;
|
|
|
|
|
|
|
|
//
|
2012-03-16 04:31:47 +01:00
|
|
|
// Assemble packet. If it is non blocking mode, the Ata driver should keep each
|
2011-05-03 12:31:41 +02:00
|
|
|
// subtask and clean them when the event is signaled.
|
2009-12-22 08:35:49 +01:00
|
|
|
//
|
2011-05-03 12:31:41 +02:00
|
|
|
if (TaskPacket != NULL) {
|
|
|
|
Packet = TaskPacket;
|
2011-05-19 11:40:42 +02:00
|
|
|
Packet->Asb = AllocateAlignedBuffer (AtaDevice, sizeof (EFI_ATA_STATUS_BLOCK));
|
2012-10-16 11:27:49 +02:00
|
|
|
if (Packet->Asb == NULL) {
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2011-05-19 11:40:42 +02:00
|
|
|
CopyMem (Packet->Asb, AtaDevice->Asb, sizeof (EFI_ATA_STATUS_BLOCK));
|
|
|
|
Packet->Acb = AllocateCopyPool (sizeof (EFI_ATA_COMMAND_BLOCK), &AtaDevice->Acb);
|
2011-05-03 12:31:41 +02:00
|
|
|
} else {
|
|
|
|
Packet = &AtaDevice->Packet;
|
|
|
|
Packet->Asb = AtaDevice->Asb;
|
|
|
|
Packet->Acb = &AtaDevice->Acb;
|
|
|
|
}
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
AtaPassThru = AtaDevice->AtaBusDriverData->AtaPassThru;
|
|
|
|
|
|
|
|
Status = AtaPassThru->PassThru (
|
|
|
|
AtaPassThru,
|
|
|
|
AtaDevice->Port,
|
|
|
|
AtaDevice->PortMultiplierPort,
|
|
|
|
Packet,
|
2011-05-03 12:31:41 +02:00
|
|
|
Event
|
2009-12-22 08:35:49 +01:00
|
|
|
);
|
|
|
|
//
|
|
|
|
// Ensure ATA pass through caller and callee have the same
|
2012-03-16 04:31:47 +01:00
|
|
|
// interpretation of ATA pass through protocol.
|
2009-12-22 08:35:49 +01:00
|
|
|
//
|
|
|
|
ASSERT (Status != EFI_INVALID_PARAMETER);
|
|
|
|
ASSERT (Status != EFI_BAD_BUFFER_SIZE);
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.ResetDevice().
|
|
|
|
|
|
|
|
This function wraps the ResetDevice() invocation for ATA pass through function
|
2012-03-16 04:31:47 +01:00
|
|
|
for an ATA device.
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
@param AtaDevice The ATA child device involved for the operation.
|
|
|
|
|
|
|
|
@return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
ResetAtaDevice (
|
|
|
|
IN ATA_DEVICE *AtaDevice
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
|
2012-03-16 04:31:47 +01:00
|
|
|
|
2009-12-22 08:35:49 +01:00
|
|
|
AtaPassThru = AtaDevice->AtaBusDriverData->AtaPassThru;
|
2012-03-16 04:31:47 +01:00
|
|
|
|
2012-10-30 05:23:40 +01:00
|
|
|
//
|
|
|
|
// Report Status Code to indicate reset happens
|
|
|
|
//
|
|
|
|
REPORT_STATUS_CODE_WITH_DEVICE_PATH (
|
|
|
|
EFI_PROGRESS_CODE,
|
|
|
|
(EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_RESET),
|
|
|
|
AtaDevice->AtaBusDriverData->ParentDevicePath
|
|
|
|
);
|
|
|
|
|
2009-12-22 08:35:49 +01:00
|
|
|
return AtaPassThru->ResetDevice (
|
|
|
|
AtaPassThru,
|
|
|
|
AtaDevice->Port,
|
|
|
|
AtaDevice->PortMultiplierPort
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Prints ATA model name to ATA device structure.
|
|
|
|
|
2012-03-16 04:31:47 +01:00
|
|
|
This function converts ATA device model name from ATA identify data
|
2009-12-22 08:35:49 +01:00
|
|
|
to a string in ATA device structure. It needs to change the character
|
|
|
|
order in the original model name string.
|
|
|
|
|
|
|
|
@param AtaDevice The ATA child device involved for the operation.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
PrintAtaModelName (
|
|
|
|
IN OUT ATA_DEVICE *AtaDevice
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINTN Index;
|
|
|
|
CHAR8 *Source;
|
|
|
|
CHAR16 *Destination;
|
|
|
|
|
2010-01-26 06:21:52 +01:00
|
|
|
Source = AtaDevice->IdentifyData->ModelName;
|
2009-12-22 08:35:49 +01:00
|
|
|
Destination = AtaDevice->ModelName;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Swap the byte order in the original module name.
|
|
|
|
//
|
|
|
|
for (Index = 0; Index < MAX_MODEL_NAME_LEN; Index += 2) {
|
|
|
|
Destination[Index] = Source[Index + 1];
|
|
|
|
Destination[Index + 1] = Source[Index];
|
|
|
|
}
|
|
|
|
AtaDevice->ModelName[MAX_MODEL_NAME_LEN] = L'\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Gets ATA device Capacity according to ATA 6.
|
|
|
|
|
|
|
|
This function returns the capacity of the ATA device if it follows
|
|
|
|
ATA 6 to support 48 bit addressing.
|
|
|
|
|
|
|
|
@param AtaDevice The ATA child device involved for the operation.
|
|
|
|
|
|
|
|
@return The capacity of the ATA device or 0 if the device does not support
|
|
|
|
48-bit addressing defined in ATA 6.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_LBA
|
|
|
|
GetAtapi6Capacity (
|
|
|
|
IN ATA_DEVICE *AtaDevice
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_LBA Capacity;
|
|
|
|
EFI_LBA TmpLba;
|
|
|
|
UINTN Index;
|
2010-01-26 06:21:52 +01:00
|
|
|
ATA_IDENTIFY_DATA *IdentifyData;
|
2009-12-22 08:35:49 +01:00
|
|
|
|
2010-01-26 06:21:52 +01:00
|
|
|
IdentifyData = AtaDevice->IdentifyData;
|
|
|
|
if ((IdentifyData->command_set_supported_83 & BIT10) == 0) {
|
2009-12-22 08:35:49 +01:00
|
|
|
//
|
|
|
|
// The device doesn't support 48 bit addressing
|
|
|
|
//
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 48 bit address feature set is supported, get maximum capacity
|
|
|
|
//
|
|
|
|
Capacity = 0;
|
|
|
|
for (Index = 0; Index < 4; Index++) {
|
|
|
|
//
|
|
|
|
// Lower byte goes first: word[100] is the lowest word, word[103] is highest
|
|
|
|
//
|
2010-01-26 06:21:52 +01:00
|
|
|
TmpLba = IdentifyData->maximum_lba_for_48bit_addressing[Index];
|
2009-12-22 08:35:49 +01:00
|
|
|
Capacity |= LShiftU64 (TmpLba, 16 * Index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Identifies ATA device via the Identify data.
|
|
|
|
|
2012-03-16 04:31:47 +01:00
|
|
|
This function identifies the ATA device and initializes the Media information in
|
2009-12-22 08:35:49 +01:00
|
|
|
Block IO protocol interface.
|
|
|
|
|
|
|
|
@param AtaDevice The ATA child device involved for the operation.
|
|
|
|
|
|
|
|
@retval EFI_UNSUPPORTED The device is not a valid ATA device (hard disk).
|
|
|
|
@retval EFI_SUCCESS The device is successfully identified and Media information
|
|
|
|
is correctly initialized.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
IdentifyAtaDevice (
|
|
|
|
IN OUT ATA_DEVICE *AtaDevice
|
|
|
|
)
|
|
|
|
{
|
2010-01-26 06:21:52 +01:00
|
|
|
ATA_IDENTIFY_DATA *IdentifyData;
|
2009-12-22 08:35:49 +01:00
|
|
|
EFI_BLOCK_IO_MEDIA *BlockMedia;
|
|
|
|
EFI_LBA Capacity;
|
|
|
|
UINT16 PhyLogicSectorSupport;
|
|
|
|
UINT16 UdmaMode;
|
|
|
|
|
2010-01-26 06:21:52 +01:00
|
|
|
IdentifyData = AtaDevice->IdentifyData;
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
if ((IdentifyData->config & BIT15) != 0) {
|
|
|
|
//
|
|
|
|
// This is not an hard disk
|
|
|
|
//
|
|
|
|
return EFI_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
2011-09-01 08:37:55 +02:00
|
|
|
DEBUG ((EFI_D_INFO, "AtaBus - Identify Device: Port %x PortMultiplierPort %x\n", AtaDevice->Port, AtaDevice->PortMultiplierPort));
|
2011-05-03 12:31:41 +02:00
|
|
|
|
2009-12-22 08:35:49 +01:00
|
|
|
//
|
|
|
|
// Check whether the WORD 88 (supported UltraDMA by drive) is valid
|
|
|
|
//
|
|
|
|
if ((IdentifyData->field_validity & BIT2) != 0) {
|
|
|
|
UdmaMode = IdentifyData->ultra_dma_mode;
|
|
|
|
if ((UdmaMode & (BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6)) != 0) {
|
|
|
|
//
|
|
|
|
// If BIT0~BIT6 is selected, then UDMA is supported
|
|
|
|
//
|
|
|
|
AtaDevice->UdmaValid = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Capacity = GetAtapi6Capacity (AtaDevice);
|
|
|
|
if (Capacity > MAX_28BIT_ADDRESSING_CAPACITY) {
|
|
|
|
//
|
|
|
|
// Capacity exceeds 120GB. 48-bit addressing is really needed
|
|
|
|
//
|
|
|
|
AtaDevice->Lba48Bit = TRUE;
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// This is a hard disk <= 120GB capacity, treat it as normal hard disk
|
|
|
|
//
|
|
|
|
Capacity = ((UINT32)IdentifyData->user_addressable_sectors_hi << 16) | IdentifyData->user_addressable_sectors_lo;
|
|
|
|
AtaDevice->Lba48Bit = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Block Media Information:
|
|
|
|
//
|
|
|
|
BlockMedia = &AtaDevice->BlockMedia;
|
|
|
|
BlockMedia->LastBlock = Capacity - 1;
|
2009-12-31 06:28:14 +01:00
|
|
|
BlockMedia->IoAlign = AtaDevice->AtaBusDriverData->AtaPassThru->Mode->IoAlign;
|
2009-12-22 08:35:49 +01:00
|
|
|
//
|
|
|
|
// Check whether Long Physical Sector Feature is supported
|
|
|
|
//
|
|
|
|
PhyLogicSectorSupport = IdentifyData->phy_logic_sector_support;
|
|
|
|
if ((PhyLogicSectorSupport & (BIT14 | BIT15)) == BIT14) {
|
|
|
|
//
|
|
|
|
// Check whether one physical block contains multiple physical blocks
|
|
|
|
//
|
|
|
|
if ((PhyLogicSectorSupport & BIT13) != 0) {
|
|
|
|
BlockMedia->LogicalBlocksPerPhysicalBlock = (UINT32) (1 << (PhyLogicSectorSupport & 0x000f));
|
|
|
|
//
|
|
|
|
// Check lowest alignment of logical blocks within physical block
|
|
|
|
//
|
|
|
|
if ((IdentifyData->alignment_logic_in_phy_blocks & (BIT14 | BIT15)) == BIT14) {
|
2010-03-17 12:06:21 +01:00
|
|
|
BlockMedia->LowestAlignedLba = (EFI_LBA) ((BlockMedia->LogicalBlocksPerPhysicalBlock - ((UINT32)IdentifyData->alignment_logic_in_phy_blocks & 0x3fff)) %
|
|
|
|
BlockMedia->LogicalBlocksPerPhysicalBlock);
|
2009-12-22 08:35:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Check logical block size
|
|
|
|
//
|
|
|
|
if ((PhyLogicSectorSupport & BIT12) != 0) {
|
|
|
|
BlockMedia->BlockSize = (UINT32) (((IdentifyData->logic_sector_size_hi << 16) | IdentifyData->logic_sector_size_lo) * sizeof (UINT16));
|
|
|
|
}
|
|
|
|
AtaDevice->BlockIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION2;
|
|
|
|
}
|
|
|
|
//
|
2012-03-16 04:31:47 +01:00
|
|
|
// Get ATA model name from identify data structure.
|
2009-12-22 08:35:49 +01:00
|
|
|
//
|
2011-05-03 12:31:41 +02:00
|
|
|
PrintAtaModelName (AtaDevice);
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Discovers whether it is a valid ATA device.
|
|
|
|
|
|
|
|
This function issues ATA_CMD_IDENTIFY_DRIVE command to the ATA device to identify it.
|
|
|
|
If the command is executed successfully, it then identifies it and initializes
|
|
|
|
the Media information in Block IO protocol interface.
|
|
|
|
|
|
|
|
@param AtaDevice The ATA child device involved for the operation.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS The device is successfully identified and Media information
|
|
|
|
is correctly initialized.
|
2012-03-16 04:31:47 +01:00
|
|
|
@return others Some error occurs when discovering the ATA device.
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
DiscoverAtaDevice (
|
|
|
|
IN OUT ATA_DEVICE *AtaDevice
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
EFI_ATA_COMMAND_BLOCK *Acb;
|
|
|
|
EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;
|
|
|
|
UINTN Retry;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Prepare for ATA command block.
|
|
|
|
//
|
2011-05-19 11:40:42 +02:00
|
|
|
Acb = ZeroMem (&AtaDevice->Acb, sizeof (EFI_ATA_COMMAND_BLOCK));
|
2009-12-22 08:35:49 +01:00
|
|
|
Acb->AtaCommand = ATA_CMD_IDENTIFY_DRIVE;
|
2011-05-03 12:31:41 +02:00
|
|
|
Acb->AtaDeviceHead = (UINT8) (BIT7 | BIT6 | BIT5 | (AtaDevice->PortMultiplierPort << 4));
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Prepare for ATA pass through packet.
|
|
|
|
//
|
2011-05-19 11:40:42 +02:00
|
|
|
Packet = ZeroMem (&AtaDevice->Packet, sizeof (EFI_ATA_PASS_THRU_COMMAND_PACKET));
|
2009-12-22 08:35:49 +01:00
|
|
|
Packet->InDataBuffer = AtaDevice->IdentifyData;
|
2011-05-19 11:40:42 +02:00
|
|
|
Packet->InTransferLength = sizeof (ATA_IDENTIFY_DATA);
|
2009-12-22 08:35:49 +01:00
|
|
|
Packet->Protocol = EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN;
|
2011-05-03 12:31:41 +02:00
|
|
|
Packet->Length = EFI_ATA_PASS_THRU_LENGTH_BYTES | EFI_ATA_PASS_THRU_LENGTH_SECTOR_COUNT;
|
|
|
|
Packet->Timeout = ATA_TIMEOUT;
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
Retry = MAX_RETRY_TIMES;
|
|
|
|
do {
|
2011-05-03 12:31:41 +02:00
|
|
|
Status = AtaDevicePassThru (AtaDevice, NULL, NULL);
|
2009-12-22 08:35:49 +01:00
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
//
|
|
|
|
// The command is issued successfully
|
|
|
|
//
|
|
|
|
Status = IdentifyAtaDevice (AtaDevice);
|
2012-10-31 10:33:40 +01:00
|
|
|
return Status;
|
2009-12-22 08:35:49 +01:00
|
|
|
}
|
|
|
|
} while (Retry-- > 0);
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Transfer data from ATA device.
|
|
|
|
|
|
|
|
This function performs one ATA pass through transaction to transfer data from/to
|
|
|
|
ATA device. It chooses the appropriate ATA command and protocol to invoke PassThru
|
|
|
|
interface of ATA pass through.
|
|
|
|
|
2011-05-03 12:31:41 +02:00
|
|
|
@param[in, out] AtaDevice The ATA child device involved for the operation.
|
2012-03-16 04:31:47 +01:00
|
|
|
@param[in, out] TaskPacket Pointer to a Pass Thru Command Packet. Optional,
|
2011-05-03 12:31:41 +02:00
|
|
|
if it is NULL, blocking mode, and use the packet
|
|
|
|
in AtaDevice. If it is not NULL, non blocking mode,
|
|
|
|
and pass down this Packet.
|
|
|
|
@param[in, out] Buffer The pointer to the current transaction buffer.
|
|
|
|
@param[in] StartLba The starting logical block address to be accessed.
|
|
|
|
@param[in] TransferLength The block number or sector count of the transfer.
|
|
|
|
@param[in] IsWrite Indicates whether it is a write operation.
|
|
|
|
@param[in] Event If Event is NULL, then blocking I/O is performed.
|
|
|
|
If Event is not NULL and non-blocking I/O is
|
|
|
|
supported,then non-blocking I/O is performed,
|
|
|
|
and Event will be signaled when the write
|
|
|
|
request is completed.
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
@retval EFI_SUCCESS The data transfer is complete successfully.
|
2012-03-16 04:31:47 +01:00
|
|
|
@return others Some error occurs when transferring data.
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
TransferAtaDevice (
|
2011-05-03 12:31:41 +02:00
|
|
|
IN OUT ATA_DEVICE *AtaDevice,
|
|
|
|
IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *TaskPacket, OPTIONAL
|
|
|
|
IN OUT VOID *Buffer,
|
|
|
|
IN EFI_LBA StartLba,
|
|
|
|
IN UINT32 TransferLength,
|
2012-03-16 04:31:47 +01:00
|
|
|
IN BOOLEAN IsWrite,
|
2011-05-03 12:31:41 +02:00
|
|
|
IN EFI_EVENT Event OPTIONAL
|
2009-12-22 08:35:49 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_ATA_COMMAND_BLOCK *Acb;
|
|
|
|
EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;
|
|
|
|
|
2009-12-24 09:31:31 +01:00
|
|
|
//
|
2012-03-16 04:31:47 +01:00
|
|
|
// Ensure AtaDevice->UdmaValid, AtaDevice->Lba48Bit and IsWrite are valid boolean values
|
2009-12-24 09:31:31 +01:00
|
|
|
//
|
2009-12-25 05:28:31 +01:00
|
|
|
ASSERT ((UINTN) AtaDevice->UdmaValid < 2);
|
2009-12-24 09:31:31 +01:00
|
|
|
ASSERT ((UINTN) AtaDevice->Lba48Bit < 2);
|
|
|
|
ASSERT ((UINTN) IsWrite < 2);
|
2009-12-22 08:35:49 +01:00
|
|
|
//
|
|
|
|
// Prepare for ATA command block.
|
|
|
|
//
|
2011-05-19 11:40:42 +02:00
|
|
|
Acb = ZeroMem (&AtaDevice->Acb, sizeof (EFI_ATA_COMMAND_BLOCK));
|
2009-12-22 08:35:49 +01:00
|
|
|
Acb->AtaCommand = mAtaCommands[AtaDevice->UdmaValid][AtaDevice->Lba48Bit][IsWrite];
|
|
|
|
Acb->AtaSectorNumber = (UINT8) StartLba;
|
|
|
|
Acb->AtaCylinderLow = (UINT8) RShiftU64 (StartLba, 8);
|
|
|
|
Acb->AtaCylinderHigh = (UINT8) RShiftU64 (StartLba, 16);
|
2011-05-03 12:31:41 +02:00
|
|
|
Acb->AtaDeviceHead = (UINT8) (BIT7 | BIT6 | BIT5 | (AtaDevice->PortMultiplierPort << 4));
|
2009-12-24 09:31:31 +01:00
|
|
|
Acb->AtaSectorCount = (UINT8) TransferLength;
|
2009-12-22 08:35:49 +01:00
|
|
|
if (AtaDevice->Lba48Bit) {
|
|
|
|
Acb->AtaSectorNumberExp = (UINT8) RShiftU64 (StartLba, 24);
|
2009-12-24 09:31:31 +01:00
|
|
|
Acb->AtaCylinderLowExp = (UINT8) RShiftU64 (StartLba, 32);
|
|
|
|
Acb->AtaCylinderHighExp = (UINT8) RShiftU64 (StartLba, 40);
|
|
|
|
Acb->AtaSectorCountExp = (UINT8) (TransferLength >> 8);
|
2009-12-22 08:35:49 +01:00
|
|
|
} else {
|
|
|
|
Acb->AtaDeviceHead = (UINT8) (Acb->AtaDeviceHead | RShiftU64 (StartLba, 24));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Prepare for ATA pass through packet.
|
|
|
|
//
|
2011-05-03 12:31:41 +02:00
|
|
|
if (TaskPacket != NULL) {
|
2011-05-19 11:40:42 +02:00
|
|
|
Packet = ZeroMem (TaskPacket, sizeof (EFI_ATA_PASS_THRU_COMMAND_PACKET));
|
2011-05-03 12:31:41 +02:00
|
|
|
} else {
|
2011-05-19 11:40:42 +02:00
|
|
|
Packet = ZeroMem (&AtaDevice->Packet, sizeof (EFI_ATA_PASS_THRU_COMMAND_PACKET));
|
2011-05-03 12:31:41 +02:00
|
|
|
}
|
|
|
|
|
2009-12-22 08:35:49 +01:00
|
|
|
if (IsWrite) {
|
|
|
|
Packet->OutDataBuffer = Buffer;
|
|
|
|
Packet->OutTransferLength = TransferLength;
|
|
|
|
} else {
|
|
|
|
Packet->InDataBuffer = Buffer;
|
|
|
|
Packet->InTransferLength = TransferLength;
|
|
|
|
}
|
2011-05-03 12:31:41 +02:00
|
|
|
|
2009-12-22 08:35:49 +01:00
|
|
|
Packet->Protocol = mAtaPassThruCmdProtocols[AtaDevice->UdmaValid][IsWrite];
|
|
|
|
Packet->Length = EFI_ATA_PASS_THRU_LENGTH_SECTOR_COUNT;
|
2013-01-04 07:03:52 +01:00
|
|
|
//
|
|
|
|
// |------------------------|-----------------|------------------------|-----------------|
|
|
|
|
// | ATA PIO Transfer Mode | Transfer Rate | ATA DMA Transfer Mode | Transfer Rate |
|
|
|
|
// |------------------------|-----------------|------------------------|-----------------|
|
|
|
|
// | PIO Mode 0 | 3.3Mbytes/sec | Single-word DMA Mode 0 | 2.1Mbytes/sec |
|
|
|
|
// |------------------------|-----------------|------------------------|-----------------|
|
|
|
|
// | PIO Mode 1 | 5.2Mbytes/sec | Single-word DMA Mode 1 | 4.2Mbytes/sec |
|
|
|
|
// |------------------------|-----------------|------------------------|-----------------|
|
|
|
|
// | PIO Mode 2 | 8.3Mbytes/sec | Single-word DMA Mode 2 | 8.4Mbytes/sec |
|
|
|
|
// |------------------------|-----------------|------------------------|-----------------|
|
|
|
|
// | PIO Mode 3 | 11.1Mbytes/sec | Multi-word DMA Mode 0 | 4.2Mbytes/sec |
|
|
|
|
// |------------------------|-----------------|------------------------|-----------------|
|
|
|
|
// | PIO Mode 4 | 16.6Mbytes/sec | Multi-word DMA Mode 1 | 13.3Mbytes/sec |
|
|
|
|
// |------------------------|-----------------|------------------------|-----------------|
|
|
|
|
//
|
|
|
|
// As AtaBus is used to manage ATA devices, we have to use the lowest transfer rate to
|
|
|
|
// calculate the possible maximum timeout value for each read/write operation.
|
2013-06-18 05:36:11 +02:00
|
|
|
// The timout value is rounded up to nearest integar and here an additional 30s is added
|
|
|
|
// to follow ATA spec in which it mentioned that the device may take up to 30s to respond
|
|
|
|
// commands in the Standby/Idle mode.
|
2013-01-04 07:03:52 +01:00
|
|
|
//
|
|
|
|
if (AtaDevice->UdmaValid) {
|
|
|
|
//
|
|
|
|
// Calculate the maximum timeout value for DMA read/write operation.
|
|
|
|
//
|
2013-06-18 05:36:11 +02:00
|
|
|
Packet->Timeout = EFI_TIMER_PERIOD_SECONDS (DivU64x32 (MultU64x32 (TransferLength, AtaDevice->BlockMedia.BlockSize), 2100000) + 31);
|
2013-01-04 07:03:52 +01:00
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Calculate the maximum timeout value for PIO read/write operation
|
|
|
|
//
|
2013-06-18 05:36:11 +02:00
|
|
|
Packet->Timeout = EFI_TIMER_PERIOD_SECONDS (DivU64x32 (MultU64x32 (TransferLength, AtaDevice->BlockMedia.BlockSize), 3300000) + 31);
|
2013-01-04 07:03:52 +01:00
|
|
|
}
|
2011-05-03 12:31:41 +02:00
|
|
|
|
|
|
|
return AtaDevicePassThru (AtaDevice, TaskPacket, Event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-16 04:31:47 +01:00
|
|
|
Free SubTask.
|
2011-05-03 12:31:41 +02:00
|
|
|
|
|
|
|
@param[in, out] Task Pointer to task to be freed.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
2012-03-16 04:31:47 +01:00
|
|
|
EFIAPI
|
2011-05-03 12:31:41 +02:00
|
|
|
FreeAtaSubTask (
|
2012-03-16 04:31:47 +01:00
|
|
|
IN OUT ATA_BUS_ASYN_SUB_TASK *Task
|
2011-05-03 12:31:41 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (Task->Packet.Asb != NULL) {
|
2011-05-19 11:40:42 +02:00
|
|
|
FreeAlignedBuffer (Task->Packet.Asb, sizeof (EFI_ATA_STATUS_BLOCK));
|
2011-05-03 12:31:41 +02:00
|
|
|
}
|
|
|
|
if (Task->Packet.Acb != NULL) {
|
|
|
|
FreePool (Task->Packet.Acb);
|
|
|
|
}
|
|
|
|
|
|
|
|
FreePool (Task);
|
|
|
|
}
|
|
|
|
|
2013-10-10 07:57:50 +02:00
|
|
|
/**
|
|
|
|
Terminate any in-flight non-blocking I/O requests by signaling an EFI_ABORTED
|
|
|
|
in the TransactionStatus member of the EFI_BLOCK_IO2_TOKEN for the non-blocking
|
|
|
|
I/O. After that it is safe to free any Token or Buffer data structures that
|
|
|
|
were allocated to initiate the non-blockingI/O requests that were in-flight for
|
|
|
|
this device.
|
|
|
|
|
|
|
|
@param[in] AtaDevice The ATA child device involved for the operation.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
AtaTerminateNonBlockingTask (
|
|
|
|
IN ATA_DEVICE *AtaDevice
|
|
|
|
)
|
|
|
|
{
|
|
|
|
BOOLEAN SubTaskEmpty;
|
|
|
|
EFI_TPL OldTpl;
|
|
|
|
ATA_BUS_ASYN_TASK *AtaTask;
|
|
|
|
LIST_ENTRY *Entry;
|
|
|
|
LIST_ENTRY *List;
|
|
|
|
|
|
|
|
OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
|
|
|
|
//
|
|
|
|
// Abort all executing tasks from now.
|
|
|
|
//
|
|
|
|
AtaDevice->Abort = TRUE;
|
|
|
|
|
|
|
|
List = &AtaDevice->AtaTaskList;
|
|
|
|
for (Entry = GetFirstNode (List); !IsNull (List, Entry);) {
|
|
|
|
AtaTask = ATA_ASYN_TASK_FROM_ENTRY (Entry);
|
|
|
|
AtaTask->Token->TransactionStatus = EFI_ABORTED;
|
|
|
|
gBS->SignalEvent (AtaTask->Token->Event);
|
|
|
|
|
|
|
|
Entry = RemoveEntryList (Entry);
|
|
|
|
FreePool (AtaTask);
|
|
|
|
}
|
|
|
|
gBS->RestoreTPL (OldTpl);
|
|
|
|
|
|
|
|
do {
|
|
|
|
OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
|
|
|
|
//
|
|
|
|
// Wait for executing subtasks done.
|
|
|
|
//
|
|
|
|
SubTaskEmpty = IsListEmpty (&AtaDevice->AtaSubTaskList);
|
|
|
|
gBS->RestoreTPL (OldTpl);
|
|
|
|
} while (!SubTaskEmpty);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Aborting operation has been done. From now on, don't need to abort normal operation.
|
|
|
|
//
|
|
|
|
OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
|
|
|
|
AtaDevice->Abort = FALSE;
|
|
|
|
gBS->RestoreTPL (OldTpl);
|
|
|
|
}
|
|
|
|
|
2011-05-03 12:31:41 +02:00
|
|
|
/**
|
|
|
|
Call back funtion when the event is signaled.
|
|
|
|
|
|
|
|
@param[in] Event The Event this notify function registered to.
|
2011-05-19 11:40:42 +02:00
|
|
|
@param[in] Context Pointer to the context data registered to the
|
2011-05-03 12:31:41 +02:00
|
|
|
Event.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
2012-03-16 04:31:47 +01:00
|
|
|
EFIAPI
|
2011-05-03 12:31:41 +02:00
|
|
|
AtaNonBlockingCallBack (
|
|
|
|
IN EFI_EVENT Event,
|
|
|
|
IN VOID *Context
|
|
|
|
)
|
|
|
|
{
|
2012-03-16 04:31:47 +01:00
|
|
|
ATA_BUS_ASYN_SUB_TASK *Task;
|
|
|
|
ATA_BUS_ASYN_TASK *AtaTask;
|
|
|
|
ATA_DEVICE *AtaDevice;
|
|
|
|
LIST_ENTRY *Entry;
|
|
|
|
EFI_STATUS Status;
|
2011-05-03 12:31:41 +02:00
|
|
|
|
2012-03-16 04:31:47 +01:00
|
|
|
Task = (ATA_BUS_ASYN_SUB_TASK *) Context;
|
2011-05-03 12:31:41 +02:00
|
|
|
gBS->CloseEvent (Event);
|
|
|
|
|
2012-03-16 04:31:47 +01:00
|
|
|
AtaDevice = Task->AtaDevice;
|
|
|
|
|
2011-05-03 12:31:41 +02:00
|
|
|
//
|
|
|
|
// Check the command status.
|
|
|
|
// If there is error during the sub task source allocation, the error status
|
|
|
|
// should be returned to the caller directly, so here the Task->Token may already
|
|
|
|
// be deleted by the caller and no need to update the status.
|
|
|
|
//
|
2011-05-19 11:40:42 +02:00
|
|
|
if ((!(*Task->IsError)) && ((Task->Packet.Asb->AtaStatus & 0x01) == 0x01)) {
|
2011-05-03 12:31:41 +02:00
|
|
|
Task->Token->TransactionStatus = EFI_DEVICE_ERROR;
|
|
|
|
}
|
2013-10-10 07:57:50 +02:00
|
|
|
|
|
|
|
if (AtaDevice->Abort) {
|
|
|
|
Task->Token->TransactionStatus = EFI_ABORTED;
|
|
|
|
}
|
|
|
|
|
2011-05-03 12:31:41 +02:00
|
|
|
DEBUG ((
|
2011-09-01 08:37:55 +02:00
|
|
|
EFI_D_BLKIO,
|
|
|
|
"NON-BLOCKING EVENT FINISHED!- STATUS = %r\n",
|
2011-05-03 12:31:41 +02:00
|
|
|
Task->Token->TransactionStatus
|
|
|
|
));
|
|
|
|
|
|
|
|
//
|
|
|
|
// Reduce the SubEventCount, till it comes to zero.
|
|
|
|
//
|
|
|
|
(*Task->UnsignalledEventCount) --;
|
2011-09-01 08:37:55 +02:00
|
|
|
DEBUG ((EFI_D_BLKIO, "UnsignalledEventCount = %d\n", *Task->UnsignalledEventCount));
|
2011-05-03 12:31:41 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Remove the SubTask from the Task list.
|
|
|
|
//
|
|
|
|
RemoveEntryList (&Task->TaskEntry);
|
|
|
|
if ((*Task->UnsignalledEventCount) == 0) {
|
|
|
|
//
|
2011-05-19 11:40:42 +02:00
|
|
|
// All Sub tasks are done, then signal the upper layer event.
|
2011-05-03 12:31:41 +02:00
|
|
|
// Except there is error during the sub task source allocation.
|
|
|
|
//
|
|
|
|
if (!(*Task->IsError)) {
|
|
|
|
gBS->SignalEvent (Task->Token->Event);
|
2011-09-01 08:37:55 +02:00
|
|
|
DEBUG ((EFI_D_BLKIO, "Signal the upper layer event!\n"));
|
2011-05-03 12:31:41 +02:00
|
|
|
}
|
2012-03-16 04:31:47 +01:00
|
|
|
|
2011-05-03 12:31:41 +02:00
|
|
|
FreePool (Task->UnsignalledEventCount);
|
|
|
|
FreePool (Task->IsError);
|
2012-03-16 04:31:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Finish all subtasks and move to the next task in AtaTaskList.
|
|
|
|
//
|
|
|
|
if (!IsListEmpty (&AtaDevice->AtaTaskList)) {
|
|
|
|
Entry = GetFirstNode (&AtaDevice->AtaTaskList);
|
2013-10-10 07:57:50 +02:00
|
|
|
AtaTask = ATA_ASYN_TASK_FROM_ENTRY (Entry);
|
2012-03-16 04:31:47 +01:00
|
|
|
DEBUG ((EFI_D_BLKIO, "Start to embark a new Ata Task\n"));
|
|
|
|
DEBUG ((EFI_D_BLKIO, "AtaTask->NumberOfBlocks = %x; AtaTask->Token=%x\n", AtaTask->NumberOfBlocks, AtaTask->Token));
|
|
|
|
Status = AccessAtaDevice (
|
|
|
|
AtaTask->AtaDevice,
|
|
|
|
AtaTask->Buffer,
|
|
|
|
AtaTask->StartLba,
|
|
|
|
AtaTask->NumberOfBlocks,
|
|
|
|
AtaTask->IsWrite,
|
|
|
|
AtaTask->Token
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
AtaTask->Token->TransactionStatus = Status;
|
|
|
|
gBS->SignalEvent (AtaTask->Token->Event);
|
|
|
|
}
|
|
|
|
RemoveEntryList (Entry);
|
|
|
|
FreePool (AtaTask);
|
|
|
|
}
|
2011-05-03 12:31:41 +02:00
|
|
|
}
|
2009-12-22 08:35:49 +01:00
|
|
|
|
2011-05-03 12:31:41 +02:00
|
|
|
DEBUG ((
|
2011-09-01 08:37:55 +02:00
|
|
|
EFI_D_BLKIO,
|
|
|
|
"PACKET INFO: Write=%s, Length=%x, LowCylinder=%x, HighCylinder=%x, SectionNumber=%x\n",
|
2011-05-03 12:31:41 +02:00
|
|
|
Task->Packet.OutDataBuffer != NULL ? L"YES" : L"NO",
|
|
|
|
Task->Packet.OutDataBuffer != NULL ? Task->Packet.OutTransferLength : Task->Packet.InTransferLength,
|
|
|
|
Task->Packet.Acb->AtaCylinderLow,
|
|
|
|
Task->Packet.Acb->AtaCylinderHigh,
|
|
|
|
Task->Packet.Acb->AtaSectorCount
|
|
|
|
));
|
|
|
|
|
|
|
|
//
|
|
|
|
// Free the buffer of SubTask.
|
|
|
|
//
|
|
|
|
FreeAtaSubTask (Task);
|
2009-12-22 08:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Read or write a number of blocks from ATA device.
|
|
|
|
|
|
|
|
This function performs ATA pass through transactions to read/write data from/to
|
|
|
|
ATA device. It may separate the read/write request into several ATA pass through
|
|
|
|
transactions.
|
|
|
|
|
2011-05-03 12:31:41 +02:00
|
|
|
@param[in, out] AtaDevice The ATA child device involved for the operation.
|
|
|
|
@param[in, out] Buffer The pointer to the current transaction buffer.
|
|
|
|
@param[in] StartLba The starting logical block address to be accessed.
|
|
|
|
@param[in] NumberOfBlocks The block number or sector count of the transfer.
|
|
|
|
@param[in] IsWrite Indicates whether it is a write operation.
|
|
|
|
@param[in, out] Token A pointer to the token associated with the transaction.
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
@retval EFI_SUCCESS The data transfer is complete successfully.
|
2012-03-16 04:31:47 +01:00
|
|
|
@return others Some error occurs when transferring data.
|
2009-12-22 08:35:49 +01:00
|
|
|
|
|
|
|
**/
|
2012-03-16 04:31:47 +01:00
|
|
|
EFI_STATUS
|
2009-12-22 08:35:49 +01:00
|
|
|
AccessAtaDevice(
|
|
|
|
IN OUT ATA_DEVICE *AtaDevice,
|
|
|
|
IN OUT UINT8 *Buffer,
|
|
|
|
IN EFI_LBA StartLba,
|
|
|
|
IN UINTN NumberOfBlocks,
|
2011-05-03 12:31:41 +02:00
|
|
|
IN BOOLEAN IsWrite,
|
|
|
|
IN OUT EFI_BLOCK_IO2_TOKEN *Token
|
2009-12-22 08:35:49 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINTN MaxTransferBlockNumber;
|
|
|
|
UINTN TransferBlockNumber;
|
|
|
|
UINTN BlockSize;
|
2012-03-16 04:31:47 +01:00
|
|
|
ATA_BUS_ASYN_SUB_TASK *SubTask;
|
2011-05-03 12:31:41 +02:00
|
|
|
UINTN *EventCount;
|
|
|
|
UINTN TempCount;
|
2012-03-16 04:31:47 +01:00
|
|
|
ATA_BUS_ASYN_TASK *AtaTask;
|
2011-05-03 12:31:41 +02:00
|
|
|
EFI_EVENT SubEvent;
|
|
|
|
UINTN Index;
|
|
|
|
BOOLEAN *IsError;
|
|
|
|
EFI_TPL OldTpl;
|
|
|
|
|
2011-05-19 11:40:42 +02:00
|
|
|
TempCount = 0;
|
|
|
|
Status = EFI_SUCCESS;
|
|
|
|
EventCount = NULL;
|
|
|
|
IsError = NULL;
|
|
|
|
Index = 0;
|
2012-03-16 04:31:47 +01:00
|
|
|
SubTask = NULL;
|
2011-05-19 11:40:42 +02:00
|
|
|
SubEvent = NULL;
|
2012-03-16 04:31:47 +01:00
|
|
|
AtaTask = NULL;
|
2013-10-10 07:57:50 +02:00
|
|
|
|
2009-12-24 09:31:31 +01:00
|
|
|
//
|
2012-03-16 04:31:47 +01:00
|
|
|
// Ensure AtaDevice->Lba48Bit is a valid boolean value
|
2009-12-24 09:31:31 +01:00
|
|
|
//
|
|
|
|
ASSERT ((UINTN) AtaDevice->Lba48Bit < 2);
|
2009-12-22 08:35:49 +01:00
|
|
|
MaxTransferBlockNumber = mMaxTransferBlockNumber[AtaDevice->Lba48Bit];
|
2011-05-03 12:31:41 +02:00
|
|
|
BlockSize = AtaDevice->BlockMedia.BlockSize;
|
|
|
|
|
2011-05-19 11:40:42 +02:00
|
|
|
//
|
|
|
|
// Initial the return status and shared account for Non Blocking.
|
|
|
|
//
|
|
|
|
if ((Token != NULL) && (Token->Event != NULL)) {
|
2012-03-16 04:31:47 +01:00
|
|
|
OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
|
2013-10-10 07:57:50 +02:00
|
|
|
|
2012-03-16 04:31:47 +01:00
|
|
|
if (!IsListEmpty (&AtaDevice->AtaSubTaskList)) {
|
|
|
|
AtaTask = AllocateZeroPool (sizeof (ATA_BUS_ASYN_TASK));
|
|
|
|
if (AtaTask == NULL) {
|
|
|
|
gBS->RestoreTPL (OldTpl);
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
AtaTask->AtaDevice = AtaDevice;
|
|
|
|
AtaTask->Buffer = Buffer;
|
|
|
|
AtaTask->IsWrite = IsWrite;
|
|
|
|
AtaTask->NumberOfBlocks = NumberOfBlocks;
|
|
|
|
AtaTask->Signature = ATA_TASK_SIGNATURE;
|
|
|
|
AtaTask->StartLba = StartLba;
|
|
|
|
AtaTask->Token = Token;
|
|
|
|
|
|
|
|
InsertTailList (&AtaDevice->AtaTaskList, &AtaTask->TaskEntry);
|
|
|
|
gBS->RestoreTPL (OldTpl);
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
gBS->RestoreTPL (OldTpl);
|
2011-05-19 11:40:42 +02:00
|
|
|
|
2012-03-16 04:31:47 +01:00
|
|
|
Token->TransactionStatus = EFI_SUCCESS;
|
2011-05-19 11:40:42 +02:00
|
|
|
EventCount = AllocateZeroPool (sizeof (UINTN));
|
|
|
|
if (EventCount == NULL) {
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
2012-03-16 04:31:47 +01:00
|
|
|
|
2011-05-19 11:40:42 +02:00
|
|
|
IsError = AllocateZeroPool (sizeof (BOOLEAN));
|
|
|
|
if (IsError == NULL) {
|
|
|
|
FreePool (EventCount);
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
2012-03-16 04:31:47 +01:00
|
|
|
DEBUG ((EFI_D_BLKIO, "Allocation IsError Addr=%x\n", IsError));
|
2011-05-19 11:40:42 +02:00
|
|
|
*IsError = FALSE;
|
|
|
|
TempCount = (NumberOfBlocks + MaxTransferBlockNumber - 1) / MaxTransferBlockNumber;
|
|
|
|
*EventCount = TempCount;
|
2012-03-16 04:31:47 +01:00
|
|
|
DEBUG ((EFI_D_BLKIO, "AccessAtaDevice, NumberOfBlocks=%x\n", NumberOfBlocks));
|
|
|
|
DEBUG ((EFI_D_BLKIO, "AccessAtaDevice, MaxTransferBlockNumber=%x\n", MaxTransferBlockNumber));
|
|
|
|
DEBUG ((EFI_D_BLKIO, "AccessAtaDevice, EventCount=%x\n", TempCount));
|
2013-10-10 07:57:50 +02:00
|
|
|
} else {
|
2012-03-16 04:31:47 +01:00
|
|
|
while (!IsListEmpty (&AtaDevice->AtaTaskList) || !IsListEmpty (&AtaDevice->AtaSubTaskList)) {
|
|
|
|
//
|
|
|
|
// Stall for 100us.
|
|
|
|
//
|
|
|
|
MicroSecondDelay (100);
|
|
|
|
}
|
2011-05-19 11:40:42 +02:00
|
|
|
}
|
2011-05-03 12:31:41 +02:00
|
|
|
|
2009-12-22 08:35:49 +01:00
|
|
|
do {
|
|
|
|
if (NumberOfBlocks > MaxTransferBlockNumber) {
|
|
|
|
TransferBlockNumber = MaxTransferBlockNumber;
|
2011-05-03 12:31:41 +02:00
|
|
|
NumberOfBlocks -= MaxTransferBlockNumber;
|
2009-12-22 08:35:49 +01:00
|
|
|
} else {
|
|
|
|
TransferBlockNumber = NumberOfBlocks;
|
2011-05-19 11:40:42 +02:00
|
|
|
NumberOfBlocks = 0;
|
2009-12-22 08:35:49 +01:00
|
|
|
}
|
|
|
|
|
2011-05-03 12:31:41 +02:00
|
|
|
//
|
2011-05-19 11:40:42 +02:00
|
|
|
// Create sub event for the sub ata task. Non-blocking mode.
|
2011-05-03 12:31:41 +02:00
|
|
|
//
|
2011-05-19 11:40:42 +02:00
|
|
|
if ((Token != NULL) && (Token->Event != NULL)) {
|
2012-03-16 04:31:47 +01:00
|
|
|
SubTask = NULL;
|
2011-05-19 11:40:42 +02:00
|
|
|
SubEvent = NULL;
|
|
|
|
|
2012-03-16 04:31:47 +01:00
|
|
|
SubTask = AllocateZeroPool (sizeof (ATA_BUS_ASYN_SUB_TASK));
|
|
|
|
if (SubTask == NULL) {
|
2011-05-19 11:40:42 +02:00
|
|
|
Status = EFI_OUT_OF_RESOURCES;
|
2011-05-03 12:31:41 +02:00
|
|
|
goto EXIT;
|
|
|
|
}
|
|
|
|
|
2011-05-19 11:40:42 +02:00
|
|
|
OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
|
2012-03-16 04:31:47 +01:00
|
|
|
SubTask->UnsignalledEventCount = EventCount;
|
|
|
|
SubTask->Signature = ATA_SUB_TASK_SIGNATURE;
|
|
|
|
SubTask->AtaDevice = AtaDevice;
|
|
|
|
SubTask->Token = Token;
|
|
|
|
SubTask->IsError = IsError;
|
|
|
|
InsertTailList (&AtaDevice->AtaSubTaskList, &SubTask->TaskEntry);
|
|
|
|
gBS->RestoreTPL (OldTpl);
|
2011-05-03 12:31:41 +02:00
|
|
|
|
|
|
|
Status = gBS->CreateEvent (
|
|
|
|
EVT_NOTIFY_SIGNAL,
|
|
|
|
TPL_NOTIFY,
|
|
|
|
AtaNonBlockingCallBack,
|
2012-03-16 04:31:47 +01:00
|
|
|
SubTask,
|
2011-05-03 12:31:41 +02:00
|
|
|
&SubEvent
|
|
|
|
);
|
|
|
|
//
|
|
|
|
// If resource allocation fail, the un-signalled event count should equal to
|
|
|
|
// the original one minus the unassigned subtasks number.
|
|
|
|
//
|
|
|
|
if (EFI_ERROR (Status)) {
|
2011-05-19 11:40:42 +02:00
|
|
|
Status = EFI_OUT_OF_RESOURCES;
|
2011-05-03 12:31:41 +02:00
|
|
|
goto EXIT;
|
|
|
|
}
|
|
|
|
|
2012-03-16 04:31:47 +01:00
|
|
|
Status = TransferAtaDevice (AtaDevice, &SubTask->Packet, Buffer, StartLba, (UINT32) TransferBlockNumber, IsWrite, SubEvent);
|
2011-05-19 11:40:42 +02:00
|
|
|
} else {
|
2011-05-03 12:31:41 +02:00
|
|
|
//
|
|
|
|
// Blocking Mode.
|
|
|
|
//
|
2012-03-16 04:31:47 +01:00
|
|
|
DEBUG ((EFI_D_BLKIO, "Blocking AccessAtaDevice, TransferBlockNumber=%x; StartLba = %x\n", TransferBlockNumber, StartLba));
|
2011-05-03 12:31:41 +02:00
|
|
|
Status = TransferAtaDevice (AtaDevice, NULL, Buffer, StartLba, (UINT32) TransferBlockNumber, IsWrite, NULL);
|
|
|
|
}
|
|
|
|
|
2009-12-22 08:35:49 +01:00
|
|
|
if (EFI_ERROR (Status)) {
|
2011-05-03 12:31:41 +02:00
|
|
|
goto EXIT;
|
2009-12-22 08:35:49 +01:00
|
|
|
}
|
2011-05-03 12:31:41 +02:00
|
|
|
|
2011-05-19 11:40:42 +02:00
|
|
|
Index++;
|
2009-12-22 08:35:49 +01:00
|
|
|
StartLba += TransferBlockNumber;
|
|
|
|
Buffer += TransferBlockNumber * BlockSize;
|
|
|
|
} while (NumberOfBlocks > 0);
|
|
|
|
|
2011-05-03 12:31:41 +02:00
|
|
|
EXIT:
|
2011-05-19 11:40:42 +02:00
|
|
|
if ((Token != NULL) && (Token->Event != NULL)) {
|
|
|
|
//
|
|
|
|
// Release resource at non-blocking mode.
|
|
|
|
//
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
|
|
|
|
Token->TransactionStatus = Status;
|
|
|
|
*EventCount = (*EventCount) - (TempCount - Index);
|
|
|
|
*IsError = TRUE;
|
2012-03-16 04:31:47 +01:00
|
|
|
|
2011-05-19 11:40:42 +02:00
|
|
|
if (*EventCount == 0) {
|
|
|
|
FreePool (EventCount);
|
|
|
|
FreePool (IsError);
|
|
|
|
}
|
2012-03-16 04:31:47 +01:00
|
|
|
|
|
|
|
if (SubTask != NULL) {
|
|
|
|
RemoveEntryList (&SubTask->TaskEntry);
|
|
|
|
FreeAtaSubTask (SubTask);
|
2011-05-19 11:40:42 +02:00
|
|
|
}
|
2011-05-03 12:31:41 +02:00
|
|
|
|
2011-05-19 11:40:42 +02:00
|
|
|
if (SubEvent != NULL) {
|
2012-03-16 04:31:47 +01:00
|
|
|
gBS->CloseEvent (SubEvent);
|
2011-05-19 11:40:42 +02:00
|
|
|
}
|
|
|
|
gBS->RestoreTPL (OldTpl);
|
|
|
|
}
|
2012-03-16 04:31:47 +01:00
|
|
|
}
|
2011-05-03 12:31:41 +02:00
|
|
|
|
2009-12-22 08:35:49 +01:00
|
|
|
return Status;
|
|
|
|
}
|
2011-05-04 04:38:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Trust transfer data from/to ATA device.
|
|
|
|
|
|
|
|
This function performs one ATA pass through transaction to do a trust transfer from/to
|
|
|
|
ATA device. It chooses the appropriate ATA command and protocol to invoke PassThru
|
|
|
|
interface of ATA pass through.
|
|
|
|
|
|
|
|
@param AtaDevice The ATA child device involved for the operation.
|
|
|
|
@param Buffer The pointer to the current transaction buffer.
|
|
|
|
@param SecurityProtocolId The value of the "Security Protocol" parameter of
|
|
|
|
the security protocol command to be sent.
|
|
|
|
@param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
|
|
|
|
of the security protocol command to be sent.
|
|
|
|
@param TransferLength The block number or sector count of the transfer.
|
|
|
|
@param IsTrustSend Indicates whether it is a trust send operation or not.
|
|
|
|
@param Timeout The timeout, in 100ns units, to use for the execution
|
|
|
|
of the security protocol command. A Timeout value of 0
|
|
|
|
means that this function will wait indefinitely for the
|
|
|
|
security protocol command to execute. If Timeout is greater
|
|
|
|
than zero, then this function will return EFI_TIMEOUT
|
|
|
|
if the time required to execute the receive data command
|
|
|
|
is greater than Timeout.
|
2011-05-19 08:12:58 +02:00
|
|
|
@param TransferLengthOut A pointer to a buffer to store the size in bytes of the data
|
|
|
|
written to the buffer. Ignore it when IsTrustSend is TRUE.
|
2011-05-04 04:38:43 +02:00
|
|
|
|
|
|
|
@retval EFI_SUCCESS The data transfer is complete successfully.
|
2012-03-16 04:31:47 +01:00
|
|
|
@return others Some error occurs when transferring data.
|
2011-05-04 04:38:43 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
TrustTransferAtaDevice (
|
|
|
|
IN OUT ATA_DEVICE *AtaDevice,
|
|
|
|
IN OUT VOID *Buffer,
|
|
|
|
IN UINT8 SecurityProtocolId,
|
|
|
|
IN UINT16 SecurityProtocolSpecificData,
|
|
|
|
IN UINTN TransferLength,
|
|
|
|
IN BOOLEAN IsTrustSend,
|
|
|
|
IN UINT64 Timeout,
|
|
|
|
OUT UINTN *TransferLengthOut
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_ATA_COMMAND_BLOCK *Acb;
|
|
|
|
EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
VOID *NewBuffer;
|
|
|
|
EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
|
|
|
|
|
|
|
|
//
|
2012-03-16 04:31:47 +01:00
|
|
|
// Ensure AtaDevice->UdmaValid and IsTrustSend are valid boolean values
|
2011-05-04 04:38:43 +02:00
|
|
|
//
|
|
|
|
ASSERT ((UINTN) AtaDevice->UdmaValid < 2);
|
|
|
|
ASSERT ((UINTN) IsTrustSend < 2);
|
|
|
|
//
|
|
|
|
// Prepare for ATA command block.
|
|
|
|
//
|
2011-05-19 11:40:42 +02:00
|
|
|
Acb = ZeroMem (&AtaDevice->Acb, sizeof (EFI_ATA_COMMAND_BLOCK));
|
2011-05-04 04:38:43 +02:00
|
|
|
if (TransferLength == 0) {
|
|
|
|
Acb->AtaCommand = ATA_CMD_TRUST_NON_DATA;
|
|
|
|
} else {
|
|
|
|
Acb->AtaCommand = mAtaTrustCommands[AtaDevice->UdmaValid][IsTrustSend];
|
|
|
|
}
|
|
|
|
Acb->AtaFeatures = SecurityProtocolId;
|
|
|
|
Acb->AtaSectorCount = (UINT8) (TransferLength / 512);
|
|
|
|
Acb->AtaSectorNumber = (UINT8) ((TransferLength / 512) >> 8);
|
|
|
|
//
|
2012-03-16 04:31:47 +01:00
|
|
|
// NOTE: ATA Spec has no explicitly definition for Security Protocol Specific layout.
|
|
|
|
// Here use big endian for Cylinder register.
|
2011-05-04 04:38:43 +02:00
|
|
|
//
|
|
|
|
Acb->AtaCylinderHigh = (UINT8) SecurityProtocolSpecificData;
|
|
|
|
Acb->AtaCylinderLow = (UINT8) (SecurityProtocolSpecificData >> 8);
|
2012-03-16 04:31:47 +01:00
|
|
|
Acb->AtaDeviceHead = (UINT8) (BIT7 | BIT6 | BIT5 | (AtaDevice->PortMultiplierPort << 4));
|
2011-05-04 04:38:43 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Prepare for ATA pass through packet.
|
|
|
|
//
|
2011-05-19 11:40:42 +02:00
|
|
|
Packet = ZeroMem (&AtaDevice->Packet, sizeof (EFI_ATA_PASS_THRU_COMMAND_PACKET));
|
2011-05-04 04:38:43 +02:00
|
|
|
if (TransferLength == 0) {
|
|
|
|
Packet->InTransferLength = 0;
|
|
|
|
Packet->OutTransferLength = 0;
|
|
|
|
Packet->Protocol = EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA;
|
|
|
|
} else if (IsTrustSend) {
|
|
|
|
//
|
|
|
|
// Check the alignment of the incoming buffer prior to invoking underlying ATA PassThru
|
|
|
|
//
|
|
|
|
AtaPassThru = AtaDevice->AtaBusDriverData->AtaPassThru;
|
|
|
|
if ((AtaPassThru->Mode->IoAlign > 1) && !IS_ALIGNED (Buffer, AtaPassThru->Mode->IoAlign)) {
|
|
|
|
NewBuffer = AllocateAlignedBuffer (AtaDevice, TransferLength);
|
2012-10-16 11:27:49 +02:00
|
|
|
if (NewBuffer == NULL) {
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2011-05-04 04:38:43 +02:00
|
|
|
CopyMem (NewBuffer, Buffer, TransferLength);
|
|
|
|
FreePool (Buffer);
|
|
|
|
Buffer = NewBuffer;
|
2012-03-16 04:31:47 +01:00
|
|
|
}
|
2011-05-04 04:38:43 +02:00
|
|
|
Packet->OutDataBuffer = Buffer;
|
|
|
|
Packet->OutTransferLength = (UINT32) TransferLength;
|
|
|
|
Packet->Protocol = mAtaPassThruCmdProtocols[AtaDevice->UdmaValid][IsTrustSend];
|
|
|
|
} else {
|
|
|
|
Packet->InDataBuffer = Buffer;
|
|
|
|
Packet->InTransferLength = (UINT32) TransferLength;
|
|
|
|
Packet->Protocol = mAtaPassThruCmdProtocols[AtaDevice->UdmaValid][IsTrustSend];
|
|
|
|
}
|
|
|
|
Packet->Length = EFI_ATA_PASS_THRU_LENGTH_BYTES;
|
|
|
|
Packet->Timeout = Timeout;
|
|
|
|
|
|
|
|
Status = AtaDevicePassThru (AtaDevice, NULL, NULL);
|
|
|
|
if (TransferLengthOut != NULL) {
|
|
|
|
if (! IsTrustSend) {
|
|
|
|
*TransferLengthOut = Packet->InTransferLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status;
|
|
|
|
}
|