]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Ata/AtaBusDxe/AtaPassThruExecute.c
2c79f46ed9cc57a8132c1461b89ad815a018f84c
[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 NOTE: This file also implements the StorageSecurityCommandProtocol(SSP). For input
9 parameter SecurityProtocolSpecificData, ATA spec has no explicitly definition
10 for Security Protocol Specific layout. This implementation uses big endian for
11 Cylinder register.
12
13 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
14 This program and the accompanying materials
15 are licensed and made available under the terms and conditions of the BSD License
16 which accompanies this distribution. The full text of the license may be found at
17 http://opensource.org/licenses/bsd-license.php
18
19 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
20 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21
22
23 **/
24
25 #include "AtaBus.h"
26
27 #define ATA_CMD_TRUST_NON_DATA 0x5B
28 #define ATA_CMD_TRUST_RECEIVE 0x5C
29 #define ATA_CMD_TRUST_RECEIVE_DMA 0x5D
30 #define ATA_CMD_TRUST_SEND 0x5E
31 #define ATA_CMD_TRUST_SEND_DMA 0x5F
32
33 //
34 // Look up table (UdmaValid, IsWrite) for EFI_ATA_PASS_THRU_CMD_PROTOCOL
35 //
36 EFI_ATA_PASS_THRU_CMD_PROTOCOL mAtaPassThruCmdProtocols[][2] = {
37 {
38 EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN,
39 EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT
40 },
41 {
42 EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN,
43 EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT,
44 }
45 };
46
47 //
48 // Look up table (UdmaValid, Lba48Bit, IsIsWrite) for ATA_CMD
49 //
50 UINT8 mAtaCommands[][2][2] = {
51 {
52 {
53 ATA_CMD_READ_SECTORS, // 28-bit LBA; PIO read
54 ATA_CMD_WRITE_SECTORS // 28-bit LBA; PIO write
55 },
56 {
57 ATA_CMD_READ_SECTORS_EXT, // 48-bit LBA; PIO read
58 ATA_CMD_WRITE_SECTORS_EXT // 48-bit LBA; PIO write
59 }
60 },
61 {
62 {
63 ATA_CMD_READ_DMA, // 28-bit LBA; DMA read
64 ATA_CMD_WRITE_DMA // 28-bit LBA; DMA write
65 },
66 {
67 ATA_CMD_READ_DMA_EXT, // 48-bit LBA; DMA read
68 ATA_CMD_WRITE_DMA_EXT // 48-bit LBA; DMA write
69 }
70 }
71 };
72
73 //
74 // Look up table (UdmaValid, IsTrustSend) for ATA_CMD
75 //
76 UINT8 mAtaTrustCommands[2][2] = {
77 {
78 ATA_CMD_TRUST_RECEIVE, // PIO read
79 ATA_CMD_TRUST_SEND // PIO write
80 },
81 {
82 ATA_CMD_TRUST_RECEIVE_DMA, // DMA read
83 ATA_CMD_TRUST_SEND_DMA // DMA write
84 }
85 };
86
87
88 //
89 // Look up table (Lba48Bit) for maximum transfer block number
90 //
91 UINTN mMaxTransferBlockNumber[] = {
92 MAX_28BIT_TRANSFER_BLOCK_NUM,
93 MAX_48BIT_TRANSFER_BLOCK_NUM
94 };
95
96
97 /**
98 Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
99
100 This function wraps the PassThru() invocation for ATA pass through function
101 for an ATA device. It assembles the ATA pass through command packet for ATA
102 transaction.
103
104 @param[in, out] AtaDevice The ATA child device involved for the operation.
105 @param[in, out] TaskPacket Pointer to a Pass Thru Command Packet. Optional,
106 if it is NULL, blocking mode, and use the packet
107 in AtaDevice. If it is not NULL, non blocking mode,
108 and pass down this Packet.
109 @param[in, out] Event If Event is NULL, then blocking I/O is performed.
110 If Event is not NULL and non-blocking I/O is
111 supported,then non-blocking I/O is performed,
112 and Event will be signaled when the write
113 request is completed.
114
115 @return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
116
117 **/
118 EFI_STATUS
119 AtaDevicePassThru (
120 IN OUT ATA_DEVICE *AtaDevice,
121 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *TaskPacket, OPTIONAL
122 IN OUT EFI_EVENT Event OPTIONAL
123 )
124 {
125 EFI_STATUS Status;
126 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
127 EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;
128
129 //
130 // Assemble packet. If it is non blocking mode, the Ata driver should keep each
131 // subtask and clean them when the event is signaled.
132 //
133 if (TaskPacket != NULL) {
134 Packet = TaskPacket;
135 Packet->Asb = AllocateAlignedBuffer (AtaDevice, sizeof (EFI_ATA_STATUS_BLOCK));
136 if (Packet->Asb == NULL) {
137 return EFI_OUT_OF_RESOURCES;
138 }
139
140 CopyMem (Packet->Asb, AtaDevice->Asb, sizeof (EFI_ATA_STATUS_BLOCK));
141 Packet->Acb = AllocateCopyPool (sizeof (EFI_ATA_COMMAND_BLOCK), &AtaDevice->Acb);
142 } else {
143 Packet = &AtaDevice->Packet;
144 Packet->Asb = AtaDevice->Asb;
145 Packet->Acb = &AtaDevice->Acb;
146 }
147
148 AtaPassThru = AtaDevice->AtaBusDriverData->AtaPassThru;
149
150 Status = AtaPassThru->PassThru (
151 AtaPassThru,
152 AtaDevice->Port,
153 AtaDevice->PortMultiplierPort,
154 Packet,
155 Event
156 );
157 //
158 // Ensure ATA pass through caller and callee have the same
159 // interpretation of ATA pass through protocol.
160 //
161 ASSERT (Status != EFI_INVALID_PARAMETER);
162 ASSERT (Status != EFI_BAD_BUFFER_SIZE);
163
164 return Status;
165 }
166
167
168 /**
169 Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.ResetDevice().
170
171 This function wraps the ResetDevice() invocation for ATA pass through function
172 for an ATA device.
173
174 @param AtaDevice The ATA child device involved for the operation.
175
176 @return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
177
178 **/
179 EFI_STATUS
180 ResetAtaDevice (
181 IN ATA_DEVICE *AtaDevice
182 )
183 {
184 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
185
186 AtaPassThru = AtaDevice->AtaBusDriverData->AtaPassThru;
187
188 //
189 // Report Status Code to indicate reset happens
190 //
191 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
192 EFI_PROGRESS_CODE,
193 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_RESET),
194 AtaDevice->AtaBusDriverData->ParentDevicePath
195 );
196
197 return AtaPassThru->ResetDevice (
198 AtaPassThru,
199 AtaDevice->Port,
200 AtaDevice->PortMultiplierPort
201 );
202 }
203
204
205 /**
206 Prints ATA model name to ATA device structure.
207
208 This function converts ATA device model name from ATA identify data
209 to a string in ATA device structure. It needs to change the character
210 order in the original model name string.
211
212 @param AtaDevice The ATA child device involved for the operation.
213
214 **/
215 VOID
216 PrintAtaModelName (
217 IN OUT ATA_DEVICE *AtaDevice
218 )
219 {
220 UINTN Index;
221 CHAR8 *Source;
222 CHAR16 *Destination;
223
224 Source = AtaDevice->IdentifyData->ModelName;
225 Destination = AtaDevice->ModelName;
226
227 //
228 // Swap the byte order in the original module name.
229 //
230 for (Index = 0; Index < MAX_MODEL_NAME_LEN; Index += 2) {
231 Destination[Index] = Source[Index + 1];
232 Destination[Index + 1] = Source[Index];
233 }
234 AtaDevice->ModelName[MAX_MODEL_NAME_LEN] = L'\0';
235 }
236
237
238 /**
239 Gets ATA device Capacity according to ATA 6.
240
241 This function returns the capacity of the ATA device if it follows
242 ATA 6 to support 48 bit addressing.
243
244 @param AtaDevice The ATA child device involved for the operation.
245
246 @return The capacity of the ATA device or 0 if the device does not support
247 48-bit addressing defined in ATA 6.
248
249 **/
250 EFI_LBA
251 GetAtapi6Capacity (
252 IN ATA_DEVICE *AtaDevice
253 )
254 {
255 EFI_LBA Capacity;
256 EFI_LBA TmpLba;
257 UINTN Index;
258 ATA_IDENTIFY_DATA *IdentifyData;
259
260 IdentifyData = AtaDevice->IdentifyData;
261 if ((IdentifyData->command_set_supported_83 & BIT10) == 0) {
262 //
263 // The device doesn't support 48 bit addressing
264 //
265 return 0;
266 }
267
268 //
269 // 48 bit address feature set is supported, get maximum capacity
270 //
271 Capacity = 0;
272 for (Index = 0; Index < 4; Index++) {
273 //
274 // Lower byte goes first: word[100] is the lowest word, word[103] is highest
275 //
276 TmpLba = IdentifyData->maximum_lba_for_48bit_addressing[Index];
277 Capacity |= LShiftU64 (TmpLba, 16 * Index);
278 }
279
280 return Capacity;
281 }
282
283
284 /**
285 Identifies ATA device via the Identify data.
286
287 This function identifies the ATA device and initializes the Media information in
288 Block IO protocol interface.
289
290 @param AtaDevice The ATA child device involved for the operation.
291
292 @retval EFI_UNSUPPORTED The device is not a valid ATA device (hard disk).
293 @retval EFI_SUCCESS The device is successfully identified and Media information
294 is correctly initialized.
295
296 **/
297 EFI_STATUS
298 IdentifyAtaDevice (
299 IN OUT ATA_DEVICE *AtaDevice
300 )
301 {
302 ATA_IDENTIFY_DATA *IdentifyData;
303 EFI_BLOCK_IO_MEDIA *BlockMedia;
304 EFI_LBA Capacity;
305 UINT16 PhyLogicSectorSupport;
306 UINT16 UdmaMode;
307
308 IdentifyData = AtaDevice->IdentifyData;
309
310 if ((IdentifyData->config & BIT15) != 0) {
311 //
312 // This is not an hard disk
313 //
314 return EFI_UNSUPPORTED;
315 }
316
317 DEBUG ((EFI_D_INFO, "AtaBus - Identify Device: Port %x PortMultiplierPort %x\n", AtaDevice->Port, AtaDevice->PortMultiplierPort));
318
319 //
320 // Check whether the WORD 88 (supported UltraDMA by drive) is valid
321 //
322 if ((IdentifyData->field_validity & BIT2) != 0) {
323 UdmaMode = IdentifyData->ultra_dma_mode;
324 if ((UdmaMode & (BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6)) != 0) {
325 //
326 // If BIT0~BIT6 is selected, then UDMA is supported
327 //
328 AtaDevice->UdmaValid = TRUE;
329 }
330 }
331
332 Capacity = GetAtapi6Capacity (AtaDevice);
333 if (Capacity > MAX_28BIT_ADDRESSING_CAPACITY) {
334 //
335 // Capacity exceeds 120GB. 48-bit addressing is really needed
336 //
337 AtaDevice->Lba48Bit = TRUE;
338 } else {
339 //
340 // This is a hard disk <= 120GB capacity, treat it as normal hard disk
341 //
342 Capacity = ((UINT32)IdentifyData->user_addressable_sectors_hi << 16) | IdentifyData->user_addressable_sectors_lo;
343 AtaDevice->Lba48Bit = FALSE;
344 }
345
346 //
347 // Block Media Information:
348 //
349 BlockMedia = &AtaDevice->BlockMedia;
350 BlockMedia->LastBlock = Capacity - 1;
351 BlockMedia->IoAlign = AtaDevice->AtaBusDriverData->AtaPassThru->Mode->IoAlign;
352 //
353 // Check whether Long Physical Sector Feature is supported
354 //
355 PhyLogicSectorSupport = IdentifyData->phy_logic_sector_support;
356 if ((PhyLogicSectorSupport & (BIT14 | BIT15)) == BIT14) {
357 //
358 // Check whether one physical block contains multiple physical blocks
359 //
360 if ((PhyLogicSectorSupport & BIT13) != 0) {
361 BlockMedia->LogicalBlocksPerPhysicalBlock = (UINT32) (1 << (PhyLogicSectorSupport & 0x000f));
362 //
363 // Check lowest alignment of logical blocks within physical block
364 //
365 if ((IdentifyData->alignment_logic_in_phy_blocks & (BIT14 | BIT15)) == BIT14) {
366 BlockMedia->LowestAlignedLba = (EFI_LBA) ((BlockMedia->LogicalBlocksPerPhysicalBlock - ((UINT32)IdentifyData->alignment_logic_in_phy_blocks & 0x3fff)) %
367 BlockMedia->LogicalBlocksPerPhysicalBlock);
368 }
369 }
370 //
371 // Check logical block size
372 //
373 if ((PhyLogicSectorSupport & BIT12) != 0) {
374 BlockMedia->BlockSize = (UINT32) (((IdentifyData->logic_sector_size_hi << 16) | IdentifyData->logic_sector_size_lo) * sizeof (UINT16));
375 }
376 AtaDevice->BlockIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION2;
377 }
378 //
379 // Get ATA model name from identify data structure.
380 //
381 PrintAtaModelName (AtaDevice);
382
383 return EFI_SUCCESS;
384 }
385
386
387 /**
388 Discovers whether it is a valid ATA device.
389
390 This function issues ATA_CMD_IDENTIFY_DRIVE command to the ATA device to identify it.
391 If the command is executed successfully, it then identifies it and initializes
392 the Media information in Block IO protocol interface.
393
394 @param AtaDevice The ATA child device involved for the operation.
395
396 @retval EFI_SUCCESS The device is successfully identified and Media information
397 is correctly initialized.
398 @return others Some error occurs when discovering the ATA device.
399
400 **/
401 EFI_STATUS
402 DiscoverAtaDevice (
403 IN OUT ATA_DEVICE *AtaDevice
404 )
405 {
406 EFI_STATUS Status;
407 EFI_ATA_COMMAND_BLOCK *Acb;
408 EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;
409 UINTN Retry;
410
411 //
412 // Prepare for ATA command block.
413 //
414 Acb = ZeroMem (&AtaDevice->Acb, sizeof (EFI_ATA_COMMAND_BLOCK));
415 Acb->AtaCommand = ATA_CMD_IDENTIFY_DRIVE;
416 Acb->AtaDeviceHead = (UINT8) (BIT7 | BIT6 | BIT5 | (AtaDevice->PortMultiplierPort << 4));
417
418 //
419 // Prepare for ATA pass through packet.
420 //
421 Packet = ZeroMem (&AtaDevice->Packet, sizeof (EFI_ATA_PASS_THRU_COMMAND_PACKET));
422 Packet->InDataBuffer = AtaDevice->IdentifyData;
423 Packet->InTransferLength = sizeof (ATA_IDENTIFY_DATA);
424 Packet->Protocol = EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN;
425 Packet->Length = EFI_ATA_PASS_THRU_LENGTH_BYTES | EFI_ATA_PASS_THRU_LENGTH_SECTOR_COUNT;
426 Packet->Timeout = ATA_TIMEOUT;
427
428 Retry = MAX_RETRY_TIMES;
429 do {
430 Status = AtaDevicePassThru (AtaDevice, NULL, NULL);
431 if (!EFI_ERROR (Status)) {
432 //
433 // The command is issued successfully
434 //
435 Status = IdentifyAtaDevice (AtaDevice);
436 return Status;
437 }
438 } while (Retry-- > 0);
439
440 return Status;
441 }
442
443 /**
444 Transfer data from ATA device.
445
446 This function performs one ATA pass through transaction to transfer data from/to
447 ATA device. It chooses the appropriate ATA command and protocol to invoke PassThru
448 interface of ATA pass through.
449
450 @param[in, out] AtaDevice The ATA child device involved for the operation.
451 @param[in, out] TaskPacket Pointer to a Pass Thru Command Packet. Optional,
452 if it is NULL, blocking mode, and use the packet
453 in AtaDevice. If it is not NULL, non blocking mode,
454 and pass down this Packet.
455 @param[in, out] Buffer The pointer to the current transaction buffer.
456 @param[in] StartLba The starting logical block address to be accessed.
457 @param[in] TransferLength The block number or sector count of the transfer.
458 @param[in] IsWrite Indicates whether it is a write operation.
459 @param[in] Event If Event is NULL, then blocking I/O is performed.
460 If Event is not NULL and non-blocking I/O is
461 supported,then non-blocking I/O is performed,
462 and Event will be signaled when the write
463 request is completed.
464
465 @retval EFI_SUCCESS The data transfer is complete successfully.
466 @return others Some error occurs when transferring data.
467
468 **/
469 EFI_STATUS
470 TransferAtaDevice (
471 IN OUT ATA_DEVICE *AtaDevice,
472 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *TaskPacket, OPTIONAL
473 IN OUT VOID *Buffer,
474 IN EFI_LBA StartLba,
475 IN UINT32 TransferLength,
476 IN BOOLEAN IsWrite,
477 IN EFI_EVENT Event OPTIONAL
478 )
479 {
480 EFI_ATA_COMMAND_BLOCK *Acb;
481 EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;
482
483 //
484 // Ensure AtaDevice->UdmaValid, AtaDevice->Lba48Bit and IsWrite are valid boolean values
485 //
486 ASSERT ((UINTN) AtaDevice->UdmaValid < 2);
487 ASSERT ((UINTN) AtaDevice->Lba48Bit < 2);
488 ASSERT ((UINTN) IsWrite < 2);
489 //
490 // Prepare for ATA command block.
491 //
492 Acb = ZeroMem (&AtaDevice->Acb, sizeof (EFI_ATA_COMMAND_BLOCK));
493 Acb->AtaCommand = mAtaCommands[AtaDevice->UdmaValid][AtaDevice->Lba48Bit][IsWrite];
494 Acb->AtaSectorNumber = (UINT8) StartLba;
495 Acb->AtaCylinderLow = (UINT8) RShiftU64 (StartLba, 8);
496 Acb->AtaCylinderHigh = (UINT8) RShiftU64 (StartLba, 16);
497 Acb->AtaDeviceHead = (UINT8) (BIT7 | BIT6 | BIT5 | (AtaDevice->PortMultiplierPort << 4));
498 Acb->AtaSectorCount = (UINT8) TransferLength;
499 if (AtaDevice->Lba48Bit) {
500 Acb->AtaSectorNumberExp = (UINT8) RShiftU64 (StartLba, 24);
501 Acb->AtaCylinderLowExp = (UINT8) RShiftU64 (StartLba, 32);
502 Acb->AtaCylinderHighExp = (UINT8) RShiftU64 (StartLba, 40);
503 Acb->AtaSectorCountExp = (UINT8) (TransferLength >> 8);
504 } else {
505 Acb->AtaDeviceHead = (UINT8) (Acb->AtaDeviceHead | RShiftU64 (StartLba, 24));
506 }
507
508 //
509 // Prepare for ATA pass through packet.
510 //
511 if (TaskPacket != NULL) {
512 Packet = ZeroMem (TaskPacket, sizeof (EFI_ATA_PASS_THRU_COMMAND_PACKET));
513 } else {
514 Packet = ZeroMem (&AtaDevice->Packet, sizeof (EFI_ATA_PASS_THRU_COMMAND_PACKET));
515 }
516
517 if (IsWrite) {
518 Packet->OutDataBuffer = Buffer;
519 Packet->OutTransferLength = TransferLength;
520 } else {
521 Packet->InDataBuffer = Buffer;
522 Packet->InTransferLength = TransferLength;
523 }
524
525 Packet->Protocol = mAtaPassThruCmdProtocols[AtaDevice->UdmaValid][IsWrite];
526 Packet->Length = EFI_ATA_PASS_THRU_LENGTH_SECTOR_COUNT;
527 //
528 // |------------------------|-----------------|------------------------|-----------------|
529 // | ATA PIO Transfer Mode | Transfer Rate | ATA DMA Transfer Mode | Transfer Rate |
530 // |------------------------|-----------------|------------------------|-----------------|
531 // | PIO Mode 0 | 3.3Mbytes/sec | Single-word DMA Mode 0 | 2.1Mbytes/sec |
532 // |------------------------|-----------------|------------------------|-----------------|
533 // | PIO Mode 1 | 5.2Mbytes/sec | Single-word DMA Mode 1 | 4.2Mbytes/sec |
534 // |------------------------|-----------------|------------------------|-----------------|
535 // | PIO Mode 2 | 8.3Mbytes/sec | Single-word DMA Mode 2 | 8.4Mbytes/sec |
536 // |------------------------|-----------------|------------------------|-----------------|
537 // | PIO Mode 3 | 11.1Mbytes/sec | Multi-word DMA Mode 0 | 4.2Mbytes/sec |
538 // |------------------------|-----------------|------------------------|-----------------|
539 // | PIO Mode 4 | 16.6Mbytes/sec | Multi-word DMA Mode 1 | 13.3Mbytes/sec |
540 // |------------------------|-----------------|------------------------|-----------------|
541 //
542 // As AtaBus is used to manage ATA devices, we have to use the lowest transfer rate to
543 // calculate the possible maximum timeout value for each read/write operation.
544 //
545 if (AtaDevice->UdmaValid) {
546 //
547 // Calculate the maximum timeout value for DMA read/write operation.
548 //
549 Packet->Timeout = EFI_TIMER_PERIOD_SECONDS ((TransferLength * AtaDevice->BlockMedia.BlockSize) / 2100000 + 1);
550 } else {
551 //
552 // Calculate the maximum timeout value for PIO read/write operation
553 //
554 Packet->Timeout = EFI_TIMER_PERIOD_SECONDS ((TransferLength * AtaDevice->BlockMedia.BlockSize) / 3300000 + 1);
555 }
556
557
558 return AtaDevicePassThru (AtaDevice, TaskPacket, Event);
559 }
560
561 /**
562 Free SubTask.
563
564 @param[in, out] Task Pointer to task to be freed.
565
566 **/
567 VOID
568 EFIAPI
569 FreeAtaSubTask (
570 IN OUT ATA_BUS_ASYN_SUB_TASK *Task
571 )
572 {
573 if (Task->Packet.Asb != NULL) {
574 FreeAlignedBuffer (Task->Packet.Asb, sizeof (EFI_ATA_STATUS_BLOCK));
575 }
576 if (Task->Packet.Acb != NULL) {
577 FreePool (Task->Packet.Acb);
578 }
579
580 FreePool (Task);
581 }
582
583 /**
584 Call back funtion when the event is signaled.
585
586 @param[in] Event The Event this notify function registered to.
587 @param[in] Context Pointer to the context data registered to the
588 Event.
589
590 **/
591 VOID
592 EFIAPI
593 AtaNonBlockingCallBack (
594 IN EFI_EVENT Event,
595 IN VOID *Context
596 )
597 {
598 ATA_BUS_ASYN_SUB_TASK *Task;
599 ATA_BUS_ASYN_TASK *AtaTask;
600 ATA_DEVICE *AtaDevice;
601 LIST_ENTRY *Entry;
602 EFI_STATUS Status;
603
604 Task = (ATA_BUS_ASYN_SUB_TASK *) Context;
605 gBS->CloseEvent (Event);
606
607 AtaDevice = Task->AtaDevice;
608
609 //
610 // Check the command status.
611 // If there is error during the sub task source allocation, the error status
612 // should be returned to the caller directly, so here the Task->Token may already
613 // be deleted by the caller and no need to update the status.
614 //
615 if ((!(*Task->IsError)) && ((Task->Packet.Asb->AtaStatus & 0x01) == 0x01)) {
616 Task->Token->TransactionStatus = EFI_DEVICE_ERROR;
617 }
618 DEBUG ((
619 EFI_D_BLKIO,
620 "NON-BLOCKING EVENT FINISHED!- STATUS = %r\n",
621 Task->Token->TransactionStatus
622 ));
623
624 //
625 // Reduce the SubEventCount, till it comes to zero.
626 //
627 (*Task->UnsignalledEventCount) --;
628 DEBUG ((EFI_D_BLKIO, "UnsignalledEventCount = %d\n", *Task->UnsignalledEventCount));
629
630 //
631 // Remove the SubTask from the Task list.
632 //
633 RemoveEntryList (&Task->TaskEntry);
634 if ((*Task->UnsignalledEventCount) == 0) {
635 //
636 // All Sub tasks are done, then signal the upper layer event.
637 // Except there is error during the sub task source allocation.
638 //
639 if (!(*Task->IsError)) {
640 gBS->SignalEvent (Task->Token->Event);
641 DEBUG ((EFI_D_BLKIO, "Signal the upper layer event!\n"));
642 }
643
644 FreePool (Task->UnsignalledEventCount);
645 FreePool (Task->IsError);
646
647
648 //
649 // Finish all subtasks and move to the next task in AtaTaskList.
650 //
651 if (!IsListEmpty (&AtaDevice->AtaTaskList)) {
652 Entry = GetFirstNode (&AtaDevice->AtaTaskList);
653 AtaTask = ATA_AYNS_TASK_FROM_ENTRY (Entry);
654 DEBUG ((EFI_D_BLKIO, "Start to embark a new Ata Task\n"));
655 DEBUG ((EFI_D_BLKIO, "AtaTask->NumberOfBlocks = %x; AtaTask->Token=%x\n", AtaTask->NumberOfBlocks, AtaTask->Token));
656 Status = AccessAtaDevice (
657 AtaTask->AtaDevice,
658 AtaTask->Buffer,
659 AtaTask->StartLba,
660 AtaTask->NumberOfBlocks,
661 AtaTask->IsWrite,
662 AtaTask->Token
663 );
664 if (EFI_ERROR (Status)) {
665 AtaTask->Token->TransactionStatus = Status;
666 gBS->SignalEvent (AtaTask->Token->Event);
667 }
668 RemoveEntryList (Entry);
669 FreePool (AtaTask);
670 }
671 }
672
673 DEBUG ((
674 EFI_D_BLKIO,
675 "PACKET INFO: Write=%s, Length=%x, LowCylinder=%x, HighCylinder=%x, SectionNumber=%x\n",
676 Task->Packet.OutDataBuffer != NULL ? L"YES" : L"NO",
677 Task->Packet.OutDataBuffer != NULL ? Task->Packet.OutTransferLength : Task->Packet.InTransferLength,
678 Task->Packet.Acb->AtaCylinderLow,
679 Task->Packet.Acb->AtaCylinderHigh,
680 Task->Packet.Acb->AtaSectorCount
681 ));
682
683 //
684 // Free the buffer of SubTask.
685 //
686 FreeAtaSubTask (Task);
687 }
688
689 /**
690 Read or write a number of blocks from ATA device.
691
692 This function performs ATA pass through transactions to read/write data from/to
693 ATA device. It may separate the read/write request into several ATA pass through
694 transactions.
695
696 @param[in, out] AtaDevice The ATA child device involved for the operation.
697 @param[in, out] Buffer The pointer to the current transaction buffer.
698 @param[in] StartLba The starting logical block address to be accessed.
699 @param[in] NumberOfBlocks The block number or sector count of the transfer.
700 @param[in] IsWrite Indicates whether it is a write operation.
701 @param[in, out] Token A pointer to the token associated with the transaction.
702
703 @retval EFI_SUCCESS The data transfer is complete successfully.
704 @return others Some error occurs when transferring data.
705
706 **/
707 EFI_STATUS
708 AccessAtaDevice(
709 IN OUT ATA_DEVICE *AtaDevice,
710 IN OUT UINT8 *Buffer,
711 IN EFI_LBA StartLba,
712 IN UINTN NumberOfBlocks,
713 IN BOOLEAN IsWrite,
714 IN OUT EFI_BLOCK_IO2_TOKEN *Token
715 )
716 {
717 EFI_STATUS Status;
718 UINTN MaxTransferBlockNumber;
719 UINTN TransferBlockNumber;
720 UINTN BlockSize;
721 ATA_BUS_ASYN_SUB_TASK *SubTask;
722 UINTN *EventCount;
723 UINTN TempCount;
724 ATA_BUS_ASYN_TASK *AtaTask;
725 EFI_EVENT SubEvent;
726 UINTN Index;
727 BOOLEAN *IsError;
728 EFI_TPL OldTpl;
729
730 TempCount = 0;
731 Status = EFI_SUCCESS;
732 EventCount = NULL;
733 IsError = NULL;
734 Index = 0;
735 SubTask = NULL;
736 SubEvent = NULL;
737 AtaTask = NULL;
738
739 //
740 // Ensure AtaDevice->Lba48Bit is a valid boolean value
741 //
742 ASSERT ((UINTN) AtaDevice->Lba48Bit < 2);
743 MaxTransferBlockNumber = mMaxTransferBlockNumber[AtaDevice->Lba48Bit];
744 BlockSize = AtaDevice->BlockMedia.BlockSize;
745
746 //
747 // Initial the return status and shared account for Non Blocking.
748 //
749 if ((Token != NULL) && (Token->Event != NULL)) {
750 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
751 if (!IsListEmpty (&AtaDevice->AtaSubTaskList)) {
752 AtaTask = AllocateZeroPool (sizeof (ATA_BUS_ASYN_TASK));
753 if (AtaTask == NULL) {
754 gBS->RestoreTPL (OldTpl);
755 return EFI_OUT_OF_RESOURCES;
756 }
757 AtaTask->AtaDevice = AtaDevice;
758 AtaTask->Buffer = Buffer;
759 AtaTask->IsWrite = IsWrite;
760 AtaTask->NumberOfBlocks = NumberOfBlocks;
761 AtaTask->Signature = ATA_TASK_SIGNATURE;
762 AtaTask->StartLba = StartLba;
763 AtaTask->Token = Token;
764
765 InsertTailList (&AtaDevice->AtaTaskList, &AtaTask->TaskEntry);
766 gBS->RestoreTPL (OldTpl);
767 return EFI_SUCCESS;
768 }
769 gBS->RestoreTPL (OldTpl);
770
771 Token->TransactionStatus = EFI_SUCCESS;
772 EventCount = AllocateZeroPool (sizeof (UINTN));
773 if (EventCount == NULL) {
774 return EFI_OUT_OF_RESOURCES;
775 }
776
777 IsError = AllocateZeroPool (sizeof (BOOLEAN));
778 if (IsError == NULL) {
779 FreePool (EventCount);
780 return EFI_OUT_OF_RESOURCES;
781 }
782 DEBUG ((EFI_D_BLKIO, "Allocation IsError Addr=%x\n", IsError));
783 *IsError = FALSE;
784 TempCount = (NumberOfBlocks + MaxTransferBlockNumber - 1) / MaxTransferBlockNumber;
785 *EventCount = TempCount;
786 DEBUG ((EFI_D_BLKIO, "AccessAtaDevice, NumberOfBlocks=%x\n", NumberOfBlocks));
787 DEBUG ((EFI_D_BLKIO, "AccessAtaDevice, MaxTransferBlockNumber=%x\n", MaxTransferBlockNumber));
788 DEBUG ((EFI_D_BLKIO, "AccessAtaDevice, EventCount=%x\n", TempCount));
789 }else {
790 while (!IsListEmpty (&AtaDevice->AtaTaskList) || !IsListEmpty (&AtaDevice->AtaSubTaskList)) {
791 //
792 // Stall for 100us.
793 //
794 MicroSecondDelay (100);
795 }
796 }
797
798 do {
799 if (NumberOfBlocks > MaxTransferBlockNumber) {
800 TransferBlockNumber = MaxTransferBlockNumber;
801 NumberOfBlocks -= MaxTransferBlockNumber;
802 } else {
803 TransferBlockNumber = NumberOfBlocks;
804 NumberOfBlocks = 0;
805 }
806
807 //
808 // Create sub event for the sub ata task. Non-blocking mode.
809 //
810 if ((Token != NULL) && (Token->Event != NULL)) {
811 SubTask = NULL;
812 SubEvent = NULL;
813
814 SubTask = AllocateZeroPool (sizeof (ATA_BUS_ASYN_SUB_TASK));
815 if (SubTask == NULL) {
816 Status = EFI_OUT_OF_RESOURCES;
817 goto EXIT;
818 }
819
820 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
821 SubTask->UnsignalledEventCount = EventCount;
822 SubTask->Signature = ATA_SUB_TASK_SIGNATURE;
823 SubTask->AtaDevice = AtaDevice;
824 SubTask->Token = Token;
825 SubTask->IsError = IsError;
826 InsertTailList (&AtaDevice->AtaSubTaskList, &SubTask->TaskEntry);
827 gBS->RestoreTPL (OldTpl);
828
829 Status = gBS->CreateEvent (
830 EVT_NOTIFY_SIGNAL,
831 TPL_NOTIFY,
832 AtaNonBlockingCallBack,
833 SubTask,
834 &SubEvent
835 );
836 //
837 // If resource allocation fail, the un-signalled event count should equal to
838 // the original one minus the unassigned subtasks number.
839 //
840 if (EFI_ERROR (Status)) {
841 Status = EFI_OUT_OF_RESOURCES;
842 goto EXIT;
843 }
844
845 Status = TransferAtaDevice (AtaDevice, &SubTask->Packet, Buffer, StartLba, (UINT32) TransferBlockNumber, IsWrite, SubEvent);
846 } else {
847 //
848 // Blocking Mode.
849 //
850 DEBUG ((EFI_D_BLKIO, "Blocking AccessAtaDevice, TransferBlockNumber=%x; StartLba = %x\n", TransferBlockNumber, StartLba));
851 Status = TransferAtaDevice (AtaDevice, NULL, Buffer, StartLba, (UINT32) TransferBlockNumber, IsWrite, NULL);
852 }
853
854 if (EFI_ERROR (Status)) {
855 goto EXIT;
856 }
857
858 Index++;
859 StartLba += TransferBlockNumber;
860 Buffer += TransferBlockNumber * BlockSize;
861 } while (NumberOfBlocks > 0);
862
863 EXIT:
864 if ((Token != NULL) && (Token->Event != NULL)) {
865 //
866 // Release resource at non-blocking mode.
867 //
868 if (EFI_ERROR (Status)) {
869 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
870 Token->TransactionStatus = Status;
871 *EventCount = (*EventCount) - (TempCount - Index);
872 *IsError = TRUE;
873
874 if (*EventCount == 0) {
875 FreePool (EventCount);
876 FreePool (IsError);
877 }
878
879 if (SubTask != NULL) {
880 RemoveEntryList (&SubTask->TaskEntry);
881 FreeAtaSubTask (SubTask);
882 }
883
884 if (SubEvent != NULL) {
885 gBS->CloseEvent (SubEvent);
886 }
887 gBS->RestoreTPL (OldTpl);
888 }
889 }
890
891 return Status;
892 }
893
894 /**
895 Trust transfer data from/to ATA device.
896
897 This function performs one ATA pass through transaction to do a trust transfer from/to
898 ATA device. It chooses the appropriate ATA command and protocol to invoke PassThru
899 interface of ATA pass through.
900
901 @param AtaDevice The ATA child device involved for the operation.
902 @param Buffer The pointer to the current transaction buffer.
903 @param SecurityProtocolId The value of the "Security Protocol" parameter of
904 the security protocol command to be sent.
905 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
906 of the security protocol command to be sent.
907 @param TransferLength The block number or sector count of the transfer.
908 @param IsTrustSend Indicates whether it is a trust send operation or not.
909 @param Timeout The timeout, in 100ns units, to use for the execution
910 of the security protocol command. A Timeout value of 0
911 means that this function will wait indefinitely for the
912 security protocol command to execute. If Timeout is greater
913 than zero, then this function will return EFI_TIMEOUT
914 if the time required to execute the receive data command
915 is greater than Timeout.
916 @param TransferLengthOut A pointer to a buffer to store the size in bytes of the data
917 written to the buffer. Ignore it when IsTrustSend is TRUE.
918
919 @retval EFI_SUCCESS The data transfer is complete successfully.
920 @return others Some error occurs when transferring data.
921
922 **/
923 EFI_STATUS
924 EFIAPI
925 TrustTransferAtaDevice (
926 IN OUT ATA_DEVICE *AtaDevice,
927 IN OUT VOID *Buffer,
928 IN UINT8 SecurityProtocolId,
929 IN UINT16 SecurityProtocolSpecificData,
930 IN UINTN TransferLength,
931 IN BOOLEAN IsTrustSend,
932 IN UINT64 Timeout,
933 OUT UINTN *TransferLengthOut
934 )
935 {
936 EFI_ATA_COMMAND_BLOCK *Acb;
937 EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;
938 EFI_STATUS Status;
939 VOID *NewBuffer;
940 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
941
942 //
943 // Ensure AtaDevice->UdmaValid and IsTrustSend are valid boolean values
944 //
945 ASSERT ((UINTN) AtaDevice->UdmaValid < 2);
946 ASSERT ((UINTN) IsTrustSend < 2);
947 //
948 // Prepare for ATA command block.
949 //
950 Acb = ZeroMem (&AtaDevice->Acb, sizeof (EFI_ATA_COMMAND_BLOCK));
951 if (TransferLength == 0) {
952 Acb->AtaCommand = ATA_CMD_TRUST_NON_DATA;
953 } else {
954 Acb->AtaCommand = mAtaTrustCommands[AtaDevice->UdmaValid][IsTrustSend];
955 }
956 Acb->AtaFeatures = SecurityProtocolId;
957 Acb->AtaSectorCount = (UINT8) (TransferLength / 512);
958 Acb->AtaSectorNumber = (UINT8) ((TransferLength / 512) >> 8);
959 //
960 // NOTE: ATA Spec has no explicitly definition for Security Protocol Specific layout.
961 // Here use big endian for Cylinder register.
962 //
963 Acb->AtaCylinderHigh = (UINT8) SecurityProtocolSpecificData;
964 Acb->AtaCylinderLow = (UINT8) (SecurityProtocolSpecificData >> 8);
965 Acb->AtaDeviceHead = (UINT8) (BIT7 | BIT6 | BIT5 | (AtaDevice->PortMultiplierPort << 4));
966
967 //
968 // Prepare for ATA pass through packet.
969 //
970 Packet = ZeroMem (&AtaDevice->Packet, sizeof (EFI_ATA_PASS_THRU_COMMAND_PACKET));
971 if (TransferLength == 0) {
972 Packet->InTransferLength = 0;
973 Packet->OutTransferLength = 0;
974 Packet->Protocol = EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA;
975 } else if (IsTrustSend) {
976 //
977 // Check the alignment of the incoming buffer prior to invoking underlying ATA PassThru
978 //
979 AtaPassThru = AtaDevice->AtaBusDriverData->AtaPassThru;
980 if ((AtaPassThru->Mode->IoAlign > 1) && !IS_ALIGNED (Buffer, AtaPassThru->Mode->IoAlign)) {
981 NewBuffer = AllocateAlignedBuffer (AtaDevice, TransferLength);
982 if (NewBuffer == NULL) {
983 return EFI_OUT_OF_RESOURCES;
984 }
985
986 CopyMem (NewBuffer, Buffer, TransferLength);
987 FreePool (Buffer);
988 Buffer = NewBuffer;
989 }
990 Packet->OutDataBuffer = Buffer;
991 Packet->OutTransferLength = (UINT32) TransferLength;
992 Packet->Protocol = mAtaPassThruCmdProtocols[AtaDevice->UdmaValid][IsTrustSend];
993 } else {
994 Packet->InDataBuffer = Buffer;
995 Packet->InTransferLength = (UINT32) TransferLength;
996 Packet->Protocol = mAtaPassThruCmdProtocols[AtaDevice->UdmaValid][IsTrustSend];
997 }
998 Packet->Length = EFI_ATA_PASS_THRU_LENGTH_BYTES;
999 Packet->Timeout = Timeout;
1000
1001 Status = AtaDevicePassThru (AtaDevice, NULL, NULL);
1002 if (TransferLengthOut != NULL) {
1003 if (! IsTrustSend) {
1004 *TransferLengthOut = Packet->InTransferLength;
1005 }
1006 }
1007 return Status;
1008 }