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