2 This file implements ATA pass through transaction for ATA bus driver.
4 This file implements the low level execution of ATA pass through transaction.
5 It transforms the high level identity, read/write, reset command to ATA pass
6 through command and protocol.
8 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 // Look up table (UdmaValid, IsWrite) for EFI_ATA_PASS_THRU_CMD_PROTOCOL
25 EFI_ATA_PASS_THRU_CMD_PROTOCOL mAtaPassThruCmdProtocols
[][2] = {
27 EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
,
28 EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
31 EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN
,
32 EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT
,
37 // Look up table (UdmaValid, Lba48Bit, IsIsWrite) for ATA_CMD
39 UINT8 mAtaCommands
[][2][2] = {
42 ATA_CMD_READ_SECTORS
, // 28-bit LBA; PIO read
43 ATA_CMD_WRITE_SECTORS
// 28-bit LBA; PIO write
46 ATA_CMD_READ_SECTORS_EXT
, // 48-bit LBA; PIO read
47 ATA_CMD_WRITE_SECTORS_EXT
// 48-bit LBA; PIO write
52 ATA_CMD_READ_DMA
, // 28-bit LBA; DMA read
53 ATA_CMD_WRITE_DMA
// 28-bit LBA; DMA write
56 ATA_CMD_READ_DMA_EXT
, // 48-bit LBA; DMA read
57 ATA_CMD_WRITE_DMA_EXT
// 48-bit LBA; DMA write
63 // Look up table (Lba48Bit) for maximum transfer block number
65 UINTN mMaxTransferBlockNumber
[] = {
66 MAX_28BIT_TRANSFER_BLOCK_NUM
,
67 MAX_48BIT_TRANSFER_BLOCK_NUM
72 Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
74 This function wraps the PassThru() invocation for ATA pass through function
75 for an ATA device. It assembles the ATA pass through command packet for ATA
78 @param AtaDevice The ATA child device involved for the operation.
80 @return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
85 IN OUT ATA_DEVICE
*AtaDevice
89 EFI_ATA_PASS_THRU_PROTOCOL
*AtaPassThru
;
90 EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
;
95 Packet
= &AtaDevice
->Packet
;
96 Packet
->Asb
= AtaDevice
->Asb
;
97 Packet
->Acb
= &AtaDevice
->Acb
;
98 Packet
->Timeout
= ATA_TIMEOUT
;
100 AtaPassThru
= AtaDevice
->AtaBusDriverData
->AtaPassThru
;
102 Status
= AtaPassThru
->PassThru (
105 AtaDevice
->PortMultiplierPort
,
110 // Ensure ATA pass through caller and callee have the same
111 // interpretation of ATA pass through protocol.
113 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
114 ASSERT (Status
!= EFI_BAD_BUFFER_SIZE
);
121 Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.ResetDevice().
123 This function wraps the ResetDevice() invocation for ATA pass through function
126 @param AtaDevice The ATA child device involved for the operation.
128 @return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
133 IN ATA_DEVICE
*AtaDevice
136 EFI_ATA_PASS_THRU_PROTOCOL
*AtaPassThru
;
138 AtaPassThru
= AtaDevice
->AtaBusDriverData
->AtaPassThru
;
140 return AtaPassThru
->ResetDevice (
143 AtaDevice
->PortMultiplierPort
149 Prints ATA model name to ATA device structure.
151 This function converts ATA device model name from ATA identify data
152 to a string in ATA device structure. It needs to change the character
153 order in the original model name string.
155 @param AtaDevice The ATA child device involved for the operation.
160 IN OUT ATA_DEVICE
*AtaDevice
167 Source
= AtaDevice
->IdentifyData
->ModelName
;
168 Destination
= AtaDevice
->ModelName
;
171 // Swap the byte order in the original module name.
173 for (Index
= 0; Index
< MAX_MODEL_NAME_LEN
; Index
+= 2) {
174 Destination
[Index
] = Source
[Index
+ 1];
175 Destination
[Index
+ 1] = Source
[Index
];
177 AtaDevice
->ModelName
[MAX_MODEL_NAME_LEN
] = L
'\0';
182 Gets ATA device Capacity according to ATA 6.
184 This function returns the capacity of the ATA device if it follows
185 ATA 6 to support 48 bit addressing.
187 @param AtaDevice The ATA child device involved for the operation.
189 @return The capacity of the ATA device or 0 if the device does not support
190 48-bit addressing defined in ATA 6.
195 IN ATA_DEVICE
*AtaDevice
201 ATA_IDENTIFY_DATA
*IdentifyData
;
203 IdentifyData
= AtaDevice
->IdentifyData
;
204 if ((IdentifyData
->command_set_supported_83
& BIT10
) == 0) {
206 // The device doesn't support 48 bit addressing
212 // 48 bit address feature set is supported, get maximum capacity
215 for (Index
= 0; Index
< 4; Index
++) {
217 // Lower byte goes first: word[100] is the lowest word, word[103] is highest
219 TmpLba
= IdentifyData
->maximum_lba_for_48bit_addressing
[Index
];
220 Capacity
|= LShiftU64 (TmpLba
, 16 * Index
);
228 Identifies ATA device via the Identify data.
230 This function identifies the ATA device and initializes the Media information in
231 Block IO protocol interface.
233 @param AtaDevice The ATA child device involved for the operation.
235 @retval EFI_UNSUPPORTED The device is not a valid ATA device (hard disk).
236 @retval EFI_SUCCESS The device is successfully identified and Media information
237 is correctly initialized.
242 IN OUT ATA_DEVICE
*AtaDevice
245 ATA_IDENTIFY_DATA
*IdentifyData
;
246 EFI_BLOCK_IO_MEDIA
*BlockMedia
;
248 UINT16 PhyLogicSectorSupport
;
251 IdentifyData
= AtaDevice
->IdentifyData
;
253 if ((IdentifyData
->config
& BIT15
) != 0) {
255 // This is not an hard disk
257 return EFI_UNSUPPORTED
;
261 // Check whether the WORD 88 (supported UltraDMA by drive) is valid
263 if ((IdentifyData
->field_validity
& BIT2
) != 0) {
264 UdmaMode
= IdentifyData
->ultra_dma_mode
;
265 if ((UdmaMode
& (BIT0
| BIT1
| BIT2
| BIT3
| BIT4
| BIT5
| BIT6
)) != 0) {
267 // If BIT0~BIT6 is selected, then UDMA is supported
269 AtaDevice
->UdmaValid
= TRUE
;
273 Capacity
= GetAtapi6Capacity (AtaDevice
);
274 if (Capacity
> MAX_28BIT_ADDRESSING_CAPACITY
) {
276 // Capacity exceeds 120GB. 48-bit addressing is really needed
278 AtaDevice
->Lba48Bit
= TRUE
;
281 // This is a hard disk <= 120GB capacity, treat it as normal hard disk
283 Capacity
= ((UINT32
)IdentifyData
->user_addressable_sectors_hi
<< 16) | IdentifyData
->user_addressable_sectors_lo
;
284 AtaDevice
->Lba48Bit
= FALSE
;
288 // Block Media Information:
290 BlockMedia
= &AtaDevice
->BlockMedia
;
291 BlockMedia
->LastBlock
= Capacity
- 1;
292 BlockMedia
->IoAlign
= AtaDevice
->AtaBusDriverData
->AtaPassThru
->Mode
->IoAlign
;
294 // Check whether Long Physical Sector Feature is supported
296 PhyLogicSectorSupport
= IdentifyData
->phy_logic_sector_support
;
297 if ((PhyLogicSectorSupport
& (BIT14
| BIT15
)) == BIT14
) {
299 // Check whether one physical block contains multiple physical blocks
301 if ((PhyLogicSectorSupport
& BIT13
) != 0) {
302 BlockMedia
->LogicalBlocksPerPhysicalBlock
= (UINT32
) (1 << (PhyLogicSectorSupport
& 0x000f));
304 // Check lowest alignment of logical blocks within physical block
306 if ((IdentifyData
->alignment_logic_in_phy_blocks
& (BIT14
| BIT15
)) == BIT14
) {
307 BlockMedia
->LowestAlignedLba
= (EFI_LBA
) ((BlockMedia
->LogicalBlocksPerPhysicalBlock
- ((UINT32
)IdentifyData
->alignment_logic_in_phy_blocks
& 0x3fff)) %
308 BlockMedia
->LogicalBlocksPerPhysicalBlock
);
312 // Check logical block size
314 if ((PhyLogicSectorSupport
& BIT12
) != 0) {
315 BlockMedia
->BlockSize
= (UINT32
) (((IdentifyData
->logic_sector_size_hi
<< 16) | IdentifyData
->logic_sector_size_lo
) * sizeof (UINT16
));
317 AtaDevice
->BlockIo
.Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION2
;
320 // Get ATA model name from identify data structure.
322 PrintAtaModelName (AtaDevice
);
329 Discovers whether it is a valid ATA device.
331 This function issues ATA_CMD_IDENTIFY_DRIVE command to the ATA device to identify it.
332 If the command is executed successfully, it then identifies it and initializes
333 the Media information in Block IO protocol interface.
335 @param AtaDevice The ATA child device involved for the operation.
337 @retval EFI_SUCCESS The device is successfully identified and Media information
338 is correctly initialized.
339 @return others Some error occurs when discovering the ATA device.
344 IN OUT ATA_DEVICE
*AtaDevice
348 EFI_ATA_COMMAND_BLOCK
*Acb
;
349 EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
;
353 // Prepare for ATA command block.
355 Acb
= ZeroMem (&AtaDevice
->Acb
, sizeof (*Acb
));
356 Acb
->AtaCommand
= ATA_CMD_IDENTIFY_DRIVE
;
357 Acb
->AtaDeviceHead
= (UINT8
) (BIT7
| BIT6
| BIT5
| (AtaDevice
->PortMultiplierPort
<< 4));
360 // Prepare for ATA pass through packet.
362 Packet
= ZeroMem (&AtaDevice
->Packet
, sizeof (*Packet
));
363 Packet
->InDataBuffer
= AtaDevice
->IdentifyData
;
364 Packet
->InTransferLength
= sizeof (*AtaDevice
->IdentifyData
);
365 Packet
->Protocol
= EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN
;
366 Packet
->Length
= EFI_ATA_PASS_THRU_LENGTH_BYTES
| EFI_ATA_PASS_THRU_LENGTH_SECTOR_COUNT
;
368 Retry
= MAX_RETRY_TIMES
;
370 Status
= AtaDevicePassThru (AtaDevice
);
371 if (!EFI_ERROR (Status
)) {
373 // The command is issued successfully
375 Status
= IdentifyAtaDevice (AtaDevice
);
376 if (!EFI_ERROR (Status
)) {
380 } while (Retry
-- > 0);
386 Transfer data from ATA device.
388 This function performs one ATA pass through transaction to transfer data from/to
389 ATA device. It chooses the appropriate ATA command and protocol to invoke PassThru
390 interface of ATA pass through.
392 @param AtaDevice The ATA child device involved for the operation.
393 @param Buffer The pointer to the current transaction buffer.
394 @param StartLba The starting logical block address to be accessed.
395 @param TransferLength The block number or sector count of the transfer.
396 @param IsWrite Indicates whether it is a write operation.
398 @retval EFI_SUCCESS The data transfer is complete successfully.
399 @return others Some error occurs when transferring data.
404 IN OUT ATA_DEVICE
*AtaDevice
,
407 IN UINT32 TransferLength
,
411 EFI_ATA_COMMAND_BLOCK
*Acb
;
412 EFI_ATA_PASS_THRU_COMMAND_PACKET
*Packet
;
415 // Ensure AtaDevice->UdmaValid, AtaDevice->Lba48Bit and IsWrite are valid boolean values
417 ASSERT ((UINTN
) AtaDevice
->UdmaValid
< 2);
418 ASSERT ((UINTN
) AtaDevice
->Lba48Bit
< 2);
419 ASSERT ((UINTN
) IsWrite
< 2);
421 // Prepare for ATA command block.
423 Acb
= ZeroMem (&AtaDevice
->Acb
, sizeof (*Acb
));
424 Acb
->AtaCommand
= mAtaCommands
[AtaDevice
->UdmaValid
][AtaDevice
->Lba48Bit
][IsWrite
];
425 Acb
->AtaSectorNumber
= (UINT8
) StartLba
;
426 Acb
->AtaCylinderLow
= (UINT8
) RShiftU64 (StartLba
, 8);
427 Acb
->AtaCylinderHigh
= (UINT8
) RShiftU64 (StartLba
, 16);
428 Acb
->AtaDeviceHead
= (UINT8
) (BIT7
| BIT6
| BIT5
| (AtaDevice
->PortMultiplierPort
<< 4));
429 Acb
->AtaSectorCount
= (UINT8
) TransferLength
;
430 if (AtaDevice
->Lba48Bit
) {
431 Acb
->AtaSectorNumberExp
= (UINT8
) RShiftU64 (StartLba
, 24);
432 Acb
->AtaCylinderLowExp
= (UINT8
) RShiftU64 (StartLba
, 32);
433 Acb
->AtaCylinderHighExp
= (UINT8
) RShiftU64 (StartLba
, 40);
434 Acb
->AtaSectorCountExp
= (UINT8
) (TransferLength
>> 8);
436 Acb
->AtaDeviceHead
= (UINT8
) (Acb
->AtaDeviceHead
| RShiftU64 (StartLba
, 24));
440 // Prepare for ATA pass through packet.
442 Packet
= ZeroMem (&AtaDevice
->Packet
, sizeof (*Packet
));
444 Packet
->OutDataBuffer
= Buffer
;
445 Packet
->OutTransferLength
= TransferLength
;
447 Packet
->InDataBuffer
= Buffer
;
448 Packet
->InTransferLength
= TransferLength
;
450 Packet
->Protocol
= mAtaPassThruCmdProtocols
[AtaDevice
->UdmaValid
][IsWrite
];
451 Packet
->Length
= EFI_ATA_PASS_THRU_LENGTH_SECTOR_COUNT
;
453 return AtaDevicePassThru (AtaDevice
);
457 Read or write a number of blocks from ATA device.
459 This function performs ATA pass through transactions to read/write data from/to
460 ATA device. It may separate the read/write request into several ATA pass through
463 @param AtaDevice The ATA child device involved for the operation.
464 @param Buffer The pointer to the current transaction buffer.
465 @param StartLba The starting logical block address to be accessed.
466 @param NumberOfBlocks The block number or sector count of the transfer.
467 @param IsWrite Indicates whether it is a write operation.
469 @retval EFI_SUCCESS The data transfer is complete successfully.
470 @return others Some error occurs when transferring data.
475 IN OUT ATA_DEVICE
*AtaDevice
,
476 IN OUT UINT8
*Buffer
,
478 IN UINTN NumberOfBlocks
,
483 UINTN MaxTransferBlockNumber
;
484 UINTN TransferBlockNumber
;
488 // Ensure AtaDevice->Lba48Bit is a valid boolean value
490 ASSERT ((UINTN
) AtaDevice
->Lba48Bit
< 2);
491 MaxTransferBlockNumber
= mMaxTransferBlockNumber
[AtaDevice
->Lba48Bit
];
492 BlockSize
= AtaDevice
->BlockMedia
.BlockSize
;
494 if (NumberOfBlocks
> MaxTransferBlockNumber
) {
495 TransferBlockNumber
= MaxTransferBlockNumber
;
496 NumberOfBlocks
-= MaxTransferBlockNumber
;
498 TransferBlockNumber
= NumberOfBlocks
;
502 Status
= TransferAtaDevice (AtaDevice
, Buffer
, StartLba
, (UINT32
) TransferBlockNumber
, IsWrite
);
503 if (EFI_ERROR (Status
)) {
506 StartLba
+= TransferBlockNumber
;
507 Buffer
+= TransferBlockNumber
* BlockSize
;
508 } while (NumberOfBlocks
> 0);