]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Ata/AtaBusDxe/AtaPassThruExecute.c
Add new UEFI driver AtaBusDxe:
[mirror_edk2.git] / MdeModulePkg / Bus / Ata / AtaBusDxe / AtaPassThruExecute.c
1 /** @file
2 This file implements ATA pass through transaction for ATA bus driver.
3
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.
7
8 Copyright (c) 2009 Intel Corporation. <BR>
9 All rights reserved. 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
13
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.
16
17
18 **/
19
20 #include "AtaBus.h"
21
22 //
23 // Look up table (UdmaValid, IsWrite) for EFI_ATA_PASS_THRU_CMD_PROTOCOL
24 //
25 EFI_ATA_PASS_THRU_CMD_PROTOCOL mAtaPassThruCmdProtocols[][2] = {
26 {
27 EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN,
28 EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
29 },
30 {
31 EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN,
32 EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT,
33 }
34 };
35
36 //
37 // Look up table (UdmaValid, Lba48Bit, IsIsWrite) for ATA_CMD
38 //
39 UINT8 mAtaCommands[][2][2] = {
40 {
41 {
42 ATA_CMD_READ_SECTORS, // 28-bit LBA; PIO read
43 ATA_CMD_WRITE_SECTORS // 28-bit LBA; PIO write
44 },
45 {
46 ATA_CMD_READ_SECTORS_EXT, // 48-bit LBA; PIO read
47 ATA_CMD_WRITE_SECTORS_EXT // 48-bit LBA; PIO write
48 }
49 },
50 {
51 {
52 ATA_CMD_READ_DMA, // 28-bit LBA; DMA read
53 ATA_CMD_WRITE_DMA // 28-bit LBA; DMA write
54 },
55 {
56 ATA_CMD_READ_DMA_EXT, // 48-bit LBA; DMA read
57 ATA_CMD_WRITE_DMA_EXT // 48-bit LBA; DMA write
58 }
59 }
60 };
61
62 //
63 // Look up table (Lba48Bit) for maximum transfer block number
64 //
65 UINTN mMaxTransferBlockNumber[] = {
66 MAX_28BIT_TRANSFER_BLOCK_NUM,
67 MAX_48BIT_TRANSFER_BLOCK_NUM
68 };
69
70
71 /**
72 Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
73
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
76 transaction.
77
78 @param AtaDevice The ATA child device involved for the operation.
79
80 @return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
81
82 **/
83 EFI_STATUS
84 AtaDevicePassThru (
85 IN OUT ATA_DEVICE *AtaDevice
86 )
87 {
88 EFI_STATUS Status;
89 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
90 EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;
91
92 //
93 // Assemble packet
94 //
95 Packet = &AtaDevice->Packet;
96 Packet->Asb = AtaDevice->Asb;
97 Packet->Acb = &AtaDevice->Acb;
98 Packet->Timeout = ATA_TIMEOUT;
99
100 AtaPassThru = AtaDevice->AtaBusDriverData->AtaPassThru;
101
102 Status = AtaPassThru->PassThru (
103 AtaPassThru,
104 AtaDevice->Port,
105 AtaDevice->PortMultiplierPort,
106 Packet,
107 NULL
108 );
109 //
110 // Ensure ATA pass through caller and callee have the same
111 // interpretation of ATA pass through protocol.
112 //
113 ASSERT (Status != EFI_INVALID_PARAMETER);
114 ASSERT (Status != EFI_BAD_BUFFER_SIZE);
115
116 return Status;
117 }
118
119
120 /**
121 Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.ResetDevice().
122
123 This function wraps the ResetDevice() invocation for ATA pass through function
124 for an ATA device.
125
126 @param AtaDevice The ATA child device involved for the operation.
127
128 @return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
129
130 **/
131 EFI_STATUS
132 ResetAtaDevice (
133 IN ATA_DEVICE *AtaDevice
134 )
135 {
136 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
137
138 AtaPassThru = AtaDevice->AtaBusDriverData->AtaPassThru;
139
140 return AtaPassThru->ResetDevice (
141 AtaPassThru,
142 AtaDevice->Port,
143 AtaDevice->PortMultiplierPort
144 );
145 }
146
147
148 /**
149 Prints ATA model name to ATA device structure.
150
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.
154
155 @param AtaDevice The ATA child device involved for the operation.
156
157 **/
158 VOID
159 PrintAtaModelName (
160 IN OUT ATA_DEVICE *AtaDevice
161 )
162 {
163 UINTN Index;
164 CHAR8 *Source;
165 CHAR16 *Destination;
166
167 Source = AtaDevice->IdentifyData->AtaData.ModelName;
168 Destination = AtaDevice->ModelName;
169
170 //
171 // Swap the byte order in the original module name.
172 //
173 for (Index = 0; Index < MAX_MODEL_NAME_LEN; Index += 2) {
174 Destination[Index] = Source[Index + 1];
175 Destination[Index + 1] = Source[Index];
176 }
177 AtaDevice->ModelName[MAX_MODEL_NAME_LEN] = L'\0';
178 }
179
180
181 /**
182 Gets ATA device Capacity according to ATA 6.
183
184 This function returns the capacity of the ATA device if it follows
185 ATA 6 to support 48 bit addressing.
186
187 @param AtaDevice The ATA child device involved for the operation.
188
189 @return The capacity of the ATA device or 0 if the device does not support
190 48-bit addressing defined in ATA 6.
191
192 **/
193 EFI_LBA
194 GetAtapi6Capacity (
195 IN ATA_DEVICE *AtaDevice
196 )
197 {
198 EFI_LBA Capacity;
199 EFI_LBA TmpLba;
200 UINTN Index;
201 ATAPI_IDENTIFY_DATA *IdentifyData;
202
203 IdentifyData = (ATAPI_IDENTIFY_DATA *) AtaDevice->IdentifyData;
204 if ((IdentifyData->cmd_set_support_83 & BIT10) == 0) {
205 //
206 // The device doesn't support 48 bit addressing
207 //
208 return 0;
209 }
210
211 //
212 // 48 bit address feature set is supported, get maximum capacity
213 //
214 Capacity = 0;
215 for (Index = 0; Index < 4; Index++) {
216 //
217 // Lower byte goes first: word[100] is the lowest word, word[103] is highest
218 //
219 TmpLba = IdentifyData->max_user_lba_for_48bit_addr[Index];
220 Capacity |= LShiftU64 (TmpLba, 16 * Index);
221 }
222
223 return Capacity;
224 }
225
226
227 /**
228 Identifies ATA device via the Identify data.
229
230 This function identifies the ATA device and initializes the Media information in
231 Block IO protocol interface.
232
233 @param AtaDevice The ATA child device involved for the operation.
234
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.
238
239 **/
240 EFI_STATUS
241 IdentifyAtaDevice (
242 IN OUT ATA_DEVICE *AtaDevice
243 )
244 {
245 EFI_ATA_IDENTIFY_DATA *IdentifyData;
246 EFI_BLOCK_IO_MEDIA *BlockMedia;
247 EFI_LBA Capacity;
248 UINT16 PhyLogicSectorSupport;
249 UINT16 UdmaMode;
250
251 IdentifyData = &AtaDevice->IdentifyData->AtaData;
252
253 if ((IdentifyData->config & BIT15) != 0) {
254 //
255 // This is not an hard disk
256 //
257 return EFI_UNSUPPORTED;
258 }
259
260 //
261 // Check whether the WORD 88 (supported UltraDMA by drive) is valid
262 //
263 if ((IdentifyData->field_validity & BIT2) != 0) {
264 UdmaMode = IdentifyData->ultra_dma_mode;
265 if ((UdmaMode & (BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6)) != 0) {
266 //
267 // If BIT0~BIT6 is selected, then UDMA is supported
268 //
269 AtaDevice->UdmaValid = TRUE;
270 }
271 }
272
273 Capacity = GetAtapi6Capacity (AtaDevice);
274 if (Capacity > MAX_28BIT_ADDRESSING_CAPACITY) {
275 //
276 // Capacity exceeds 120GB. 48-bit addressing is really needed
277 //
278 AtaDevice->Lba48Bit = TRUE;
279 } else {
280 //
281 // This is a hard disk <= 120GB capacity, treat it as normal hard disk
282 //
283 Capacity = ((UINT32)IdentifyData->user_addressable_sectors_hi << 16) | IdentifyData->user_addressable_sectors_lo;
284 AtaDevice->Lba48Bit = FALSE;
285 }
286
287 //
288 // Block Media Information:
289 //
290 BlockMedia = &AtaDevice->BlockMedia;
291 BlockMedia->LastBlock = Capacity - 1;
292 //
293 // Check whether Long Physical Sector Feature is supported
294 //
295 PhyLogicSectorSupport = IdentifyData->phy_logic_sector_support;
296 if ((PhyLogicSectorSupport & (BIT14 | BIT15)) == BIT14) {
297 //
298 // Check whether one physical block contains multiple physical blocks
299 //
300 if ((PhyLogicSectorSupport & BIT13) != 0) {
301 BlockMedia->LogicalBlocksPerPhysicalBlock = (UINT32) (1 << (PhyLogicSectorSupport & 0x000f));
302 //
303 // Check lowest alignment of logical blocks within physical block
304 //
305 if ((IdentifyData->alignment_logic_in_phy_blocks & (BIT14 | BIT15)) == BIT14) {
306 BlockMedia->LowestAlignedLba = (EFI_LBA) (IdentifyData->alignment_logic_in_phy_blocks & 0x3fff);
307 }
308 }
309 //
310 // Check logical block size
311 //
312 if ((PhyLogicSectorSupport & BIT12) != 0) {
313 BlockMedia->BlockSize = (UINT32) (((IdentifyData->logic_sector_size_hi << 16) | IdentifyData->logic_sector_size_lo) * sizeof (UINT16));
314 }
315 AtaDevice->BlockIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION2;
316 }
317 //
318 // Get ATA model name from identify data structure.
319 //
320 PrintAtaModelName (AtaDevice);
321
322 return EFI_SUCCESS;
323 }
324
325
326 /**
327 Discovers whether it is a valid ATA device.
328
329 This function issues ATA_CMD_IDENTIFY_DRIVE command to the ATA device to identify it.
330 If the command is executed successfully, it then identifies it and initializes
331 the Media information in Block IO protocol interface.
332
333 @param AtaDevice The ATA child device involved for the operation.
334
335 @retval EFI_SUCCESS The device is successfully identified and Media information
336 is correctly initialized.
337 @return others Some error occurs when discovering the ATA device.
338
339 **/
340 EFI_STATUS
341 DiscoverAtaDevice (
342 IN OUT ATA_DEVICE *AtaDevice
343 )
344 {
345 EFI_STATUS Status;
346 EFI_ATA_COMMAND_BLOCK *Acb;
347 EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;
348 UINTN Retry;
349
350 //
351 // Prepare for ATA command block.
352 //
353 Acb = ZeroMem (&AtaDevice->Acb, sizeof (*Acb));
354 Acb->AtaCommand = ATA_CMD_IDENTIFY_DRIVE;
355
356 //
357 // Prepare for ATA pass through packet.
358 //
359 Packet = ZeroMem (&AtaDevice->Packet, sizeof (*Packet));
360 Packet->InDataBuffer = AtaDevice->IdentifyData;
361 Packet->InTransferLength = sizeof (*AtaDevice->IdentifyData);
362 Packet->Protocol = EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN;
363 Packet->Length = EFI_ATA_PASS_THRU_LENGTH_BYTES | EFI_ATA_PASS_THRU_LENGTH_SECTOR_COUNT;
364
365 Retry = MAX_RETRY_TIMES;
366 do {
367 Status = AtaDevicePassThru (AtaDevice);
368 if (!EFI_ERROR (Status)) {
369 //
370 // The command is issued successfully
371 //
372 Status = IdentifyAtaDevice (AtaDevice);
373 if (!EFI_ERROR (Status)) {
374 return Status;
375 }
376 }
377 } while (Retry-- > 0);
378
379 return Status;
380 }
381
382 /**
383 Transfer data from ATA device.
384
385 This function performs one ATA pass through transaction to transfer data from/to
386 ATA device. It chooses the appropriate ATA command and protocol to invoke PassThru
387 interface of ATA pass through.
388
389 @param AtaDevice The ATA child device involved for the operation.
390 @param Buffer The pointer to the current transaction buffer.
391 @param StartLba The starting logical block address to be accessed.
392 @param TransferLength The block number or sector count of the transfer.
393 @param IsWrite Indicates whether it is a write operation.
394
395 @retval EFI_SUCCESS The data transfer is complete successfully.
396 @return others Some error occurs when transferring data.
397
398 **/
399 EFI_STATUS
400 TransferAtaDevice (
401 IN OUT ATA_DEVICE *AtaDevice,
402 IN OUT VOID *Buffer,
403 IN EFI_LBA StartLba,
404 IN UINT32 TransferLength,
405 IN BOOLEAN IsWrite
406 )
407 {
408 EFI_ATA_COMMAND_BLOCK *Acb;
409 EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;
410
411 //
412 // Prepare for ATA command block.
413 //
414 Acb = ZeroMem (&AtaDevice->Acb, sizeof (*Acb));
415 Acb->AtaCommand = mAtaCommands[AtaDevice->UdmaValid][AtaDevice->Lba48Bit][IsWrite];
416 Acb->AtaSectorNumber = (UINT8) StartLba;
417 Acb->AtaCylinderLow = (UINT8) RShiftU64 (StartLba, 8);
418 Acb->AtaCylinderHigh = (UINT8) RShiftU64 (StartLba, 16);
419 Acb->AtaDeviceHead = (UINT8) (BIT7 | BIT6 | BIT5 | (AtaDevice->PortMultiplierPort << 4));
420 if (AtaDevice->Lba48Bit) {
421 Acb->AtaSectorNumberExp = (UINT8) RShiftU64 (StartLba, 24);
422 } else {
423 Acb->AtaDeviceHead = (UINT8) (Acb->AtaDeviceHead | RShiftU64 (StartLba, 24));
424 }
425 Acb->AtaCylinderLowExp = (UINT8) RShiftU64 (StartLba, 32);
426 Acb->AtaCylinderHighExp = (UINT8) RShiftU64 (StartLba, 40);
427 Acb->AtaSectorCount = (UINT8) TransferLength;
428 Acb->AtaSectorCountExp = (UINT8) (TransferLength >> 8);
429
430 //
431 // Prepare for ATA pass through packet.
432 //
433 Packet = ZeroMem (&AtaDevice->Packet, sizeof (*Packet));
434 if (IsWrite) {
435 Packet->OutDataBuffer = Buffer;
436 Packet->OutTransferLength = TransferLength;
437 } else {
438 Packet->InDataBuffer = Buffer;
439 Packet->InTransferLength = TransferLength;
440 }
441 Packet->Protocol = mAtaPassThruCmdProtocols[AtaDevice->UdmaValid][IsWrite];
442 Packet->Length = EFI_ATA_PASS_THRU_LENGTH_SECTOR_COUNT;
443
444 return AtaDevicePassThru (AtaDevice);
445 }
446
447 /**
448 Read or write a number of blocks from ATA device.
449
450 This function performs ATA pass through transactions to read/write data from/to
451 ATA device. It may separate the read/write request into several ATA pass through
452 transactions.
453
454 @param AtaDevice The ATA child device involved for the operation.
455 @param Buffer The pointer to the current transaction buffer.
456 @param StartLba The starting logical block address to be accessed.
457 @param NumberOfBlocks The block number or sector count of the transfer.
458 @param IsWrite Indicates whether it is a write operation.
459
460 @retval EFI_SUCCESS The data transfer is complete successfully.
461 @return others Some error occurs when transferring data.
462
463 **/
464 EFI_STATUS
465 AccessAtaDevice(
466 IN OUT ATA_DEVICE *AtaDevice,
467 IN OUT UINT8 *Buffer,
468 IN EFI_LBA StartLba,
469 IN UINTN NumberOfBlocks,
470 IN BOOLEAN IsWrite
471 )
472 {
473 EFI_STATUS Status;
474 UINTN MaxTransferBlockNumber;
475 UINTN TransferBlockNumber;
476 UINTN BlockSize;
477
478 MaxTransferBlockNumber = mMaxTransferBlockNumber[AtaDevice->Lba48Bit];
479 BlockSize = AtaDevice->BlockMedia.BlockSize;
480 do {
481 if (NumberOfBlocks > MaxTransferBlockNumber) {
482 TransferBlockNumber = MaxTransferBlockNumber;
483 NumberOfBlocks -= MaxTransferBlockNumber;
484 } else {
485 TransferBlockNumber = NumberOfBlocks;
486 NumberOfBlocks = 0;
487 }
488
489 Status = TransferAtaDevice (AtaDevice, Buffer, StartLba, (UINT32) TransferBlockNumber, IsWrite);
490 if (EFI_ERROR (Status)) {
491 return Status;
492 }
493 StartLba += TransferBlockNumber;
494 Buffer += TransferBlockNumber * BlockSize;
495 } while (NumberOfBlocks > 0);
496
497 return Status;
498 }