]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Ata/AtaBusDxe/AtaPassThruExecute.c
1. Add definition for SSP(StorageSecurityCommand Protocol)
[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
6 through command and protocol. \r
7 \r
490b5ea1 8 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 9 This program and the accompanying materials\r
ad86a50a 10 are licensed and made available under the terms and conditions of the BSD License\r
11 which accompanies this distribution. The full text of the license may be found at\r
12 http://opensource.org/licenses/bsd-license.php\r
13\r
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17\r
18**/\r
19\r
20#include "AtaBus.h"\r
21\r
22//\r
23// Look up table (UdmaValid, IsWrite) for EFI_ATA_PASS_THRU_CMD_PROTOCOL\r
24//\r
25EFI_ATA_PASS_THRU_CMD_PROTOCOL mAtaPassThruCmdProtocols[][2] = {\r
26 {\r
27 EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN,\r
28 EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT\r
29 },\r
30 {\r
31 EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN,\r
32 EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT,\r
33 }\r
34};\r
35\r
36//\r
37// Look up table (UdmaValid, Lba48Bit, IsIsWrite) for ATA_CMD\r
38//\r
39UINT8 mAtaCommands[][2][2] = {\r
40 {\r
41 {\r
42 ATA_CMD_READ_SECTORS, // 28-bit LBA; PIO read\r
43 ATA_CMD_WRITE_SECTORS // 28-bit LBA; PIO write\r
44 },\r
45 {\r
46 ATA_CMD_READ_SECTORS_EXT, // 48-bit LBA; PIO read\r
47 ATA_CMD_WRITE_SECTORS_EXT // 48-bit LBA; PIO write\r
48 }\r
49 },\r
50 {\r
51 {\r
52 ATA_CMD_READ_DMA, // 28-bit LBA; DMA read\r
53 ATA_CMD_WRITE_DMA // 28-bit LBA; DMA write\r
54 },\r
55 {\r
56 ATA_CMD_READ_DMA_EXT, // 48-bit LBA; DMA read\r
57 ATA_CMD_WRITE_DMA_EXT // 48-bit LBA; DMA write\r
58 }\r
59 }\r
60};\r
61\r
62//\r
63// Look up table (Lba48Bit) for maximum transfer block number\r
64//\r
65UINTN mMaxTransferBlockNumber[] = {\r
66 MAX_28BIT_TRANSFER_BLOCK_NUM,\r
67 MAX_48BIT_TRANSFER_BLOCK_NUM\r
68};\r
69\r
70\r
71/**\r
72 Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.PassThru().\r
73\r
74 This function wraps the PassThru() invocation for ATA pass through function\r
75 for an ATA device. It assembles the ATA pass through command packet for ATA\r
76 transaction.\r
77\r
490b5ea1 78 @param[in, out] AtaDevice The ATA child device involved for the operation.\r
79 @param[in, out] TaskPacket Pointer to a Pass Thru Command Packet. Optional, \r
80 if it is NULL, blocking mode, and use the packet\r
81 in AtaDevice. If it is not NULL, non blocking mode,\r
82 and pass down this Packet.\r
83 @param[in] Event If Event is NULL, then blocking I/O is performed.\r
84 If Event is not NULL and non-blocking I/O is\r
85 supported,then non-blocking I/O is performed,\r
86 and Event will be signaled when the write\r
87 request is completed.\r
ad86a50a 88\r
89 @return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().\r
90\r
91**/\r
92EFI_STATUS\r
93AtaDevicePassThru (\r
490b5ea1 94 IN OUT ATA_DEVICE *AtaDevice,\r
95 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *TaskPacket, OPTIONAL\r
96 IN OUT EFI_EVENT Event OPTIONAL\r
ad86a50a 97 )\r
98{\r
99 EFI_STATUS Status;\r
100 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;\r
101 EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;\r
102\r
103 //\r
490b5ea1 104 // Assemble packet. If it is non blocking mode, the Ata driver should keep each \r
105 // subtask and clean them when the event is signaled.\r
ad86a50a 106 //\r
490b5ea1 107 if (TaskPacket != NULL) {\r
108 Packet = TaskPacket;\r
109 Packet->Asb = AllocateAlignedBuffer (AtaDevice, sizeof (*AtaDevice->Asb));\r
110 CopyMem (Packet->Asb, AtaDevice->Asb, sizeof (*AtaDevice->Asb));\r
111 Packet->Acb = AllocateCopyPool(sizeof (EFI_ATA_COMMAND_BLOCK), &AtaDevice->Acb);\r
112 } else {\r
113 Packet = &AtaDevice->Packet;\r
114 Packet->Asb = AtaDevice->Asb;\r
115 Packet->Acb = &AtaDevice->Acb;\r
116 }\r
ad86a50a 117\r
118 AtaPassThru = AtaDevice->AtaBusDriverData->AtaPassThru;\r
119\r
120 Status = AtaPassThru->PassThru (\r
121 AtaPassThru,\r
122 AtaDevice->Port,\r
123 AtaDevice->PortMultiplierPort,\r
124 Packet,\r
490b5ea1 125 Event\r
ad86a50a 126 );\r
127 //\r
128 // Ensure ATA pass through caller and callee have the same\r
129 // interpretation of ATA pass through protocol. \r
130 //\r
131 ASSERT (Status != EFI_INVALID_PARAMETER);\r
132 ASSERT (Status != EFI_BAD_BUFFER_SIZE);\r
133\r
134 return Status;\r
135}\r
136\r
137\r
138/**\r
139 Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.ResetDevice().\r
140\r
141 This function wraps the ResetDevice() invocation for ATA pass through function\r
142 for an ATA device. \r
143\r
144 @param AtaDevice The ATA child device involved for the operation.\r
145\r
146 @return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().\r
147\r
148**/\r
149EFI_STATUS\r
150ResetAtaDevice (\r
151 IN ATA_DEVICE *AtaDevice\r
152 )\r
153{\r
154 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;\r
155 \r
156 AtaPassThru = AtaDevice->AtaBusDriverData->AtaPassThru;\r
157 \r
158 return AtaPassThru->ResetDevice (\r
159 AtaPassThru,\r
160 AtaDevice->Port,\r
161 AtaDevice->PortMultiplierPort\r
162 );\r
163}\r
164\r
165\r
166/**\r
167 Prints ATA model name to ATA device structure.\r
168\r
169 This function converts ATA device model name from ATA identify data \r
170 to a string in ATA device structure. It needs to change the character\r
171 order in the original model name string.\r
172\r
173 @param AtaDevice The ATA child device involved for the operation.\r
174\r
175**/\r
176VOID\r
177PrintAtaModelName (\r
178 IN OUT ATA_DEVICE *AtaDevice\r
179 )\r
180{\r
181 UINTN Index;\r
182 CHAR8 *Source;\r
183 CHAR16 *Destination;\r
184\r
6ea8e37b 185 Source = AtaDevice->IdentifyData->ModelName;\r
ad86a50a 186 Destination = AtaDevice->ModelName;\r
187\r
188 //\r
189 // Swap the byte order in the original module name.\r
190 //\r
191 for (Index = 0; Index < MAX_MODEL_NAME_LEN; Index += 2) {\r
192 Destination[Index] = Source[Index + 1];\r
193 Destination[Index + 1] = Source[Index];\r
194 }\r
195 AtaDevice->ModelName[MAX_MODEL_NAME_LEN] = L'\0';\r
196}\r
197\r
198\r
199/**\r
200 Gets ATA device Capacity according to ATA 6.\r
201\r
202 This function returns the capacity of the ATA device if it follows\r
203 ATA 6 to support 48 bit addressing.\r
204\r
205 @param AtaDevice The ATA child device involved for the operation.\r
206\r
207 @return The capacity of the ATA device or 0 if the device does not support\r
208 48-bit addressing defined in ATA 6.\r
209\r
210**/\r
211EFI_LBA\r
212GetAtapi6Capacity (\r
213 IN ATA_DEVICE *AtaDevice\r
214 )\r
215{\r
216 EFI_LBA Capacity;\r
217 EFI_LBA TmpLba;\r
218 UINTN Index;\r
6ea8e37b 219 ATA_IDENTIFY_DATA *IdentifyData;\r
ad86a50a 220\r
6ea8e37b 221 IdentifyData = AtaDevice->IdentifyData;\r
222 if ((IdentifyData->command_set_supported_83 & BIT10) == 0) {\r
ad86a50a 223 //\r
224 // The device doesn't support 48 bit addressing\r
225 //\r
226 return 0;\r
227 }\r
228\r
229 //\r
230 // 48 bit address feature set is supported, get maximum capacity\r
231 //\r
232 Capacity = 0;\r
233 for (Index = 0; Index < 4; Index++) {\r
234 //\r
235 // Lower byte goes first: word[100] is the lowest word, word[103] is highest\r
236 //\r
6ea8e37b 237 TmpLba = IdentifyData->maximum_lba_for_48bit_addressing[Index];\r
ad86a50a 238 Capacity |= LShiftU64 (TmpLba, 16 * Index);\r
239 }\r
240\r
241 return Capacity;\r
242}\r
243\r
244\r
245/**\r
246 Identifies ATA device via the Identify data.\r
247\r
248 This function identifies the ATA device and initializes the Media information in \r
249 Block IO protocol interface.\r
250\r
251 @param AtaDevice The ATA child device involved for the operation.\r
252\r
253 @retval EFI_UNSUPPORTED The device is not a valid ATA device (hard disk).\r
254 @retval EFI_SUCCESS The device is successfully identified and Media information\r
255 is correctly initialized.\r
256\r
257**/\r
258EFI_STATUS\r
259IdentifyAtaDevice (\r
260 IN OUT ATA_DEVICE *AtaDevice\r
261 )\r
262{\r
6ea8e37b 263 ATA_IDENTIFY_DATA *IdentifyData;\r
ad86a50a 264 EFI_BLOCK_IO_MEDIA *BlockMedia;\r
265 EFI_LBA Capacity;\r
266 UINT16 PhyLogicSectorSupport;\r
267 UINT16 UdmaMode;\r
268\r
6ea8e37b 269 IdentifyData = AtaDevice->IdentifyData;\r
ad86a50a 270\r
271 if ((IdentifyData->config & BIT15) != 0) {\r
272 //\r
273 // This is not an hard disk\r
274 //\r
275 return EFI_UNSUPPORTED;\r
276 }\r
277\r
490b5ea1 278 DEBUG ((EFI_D_INFO, "AtaBus - Identify Device (%x %x)\n", (UINTN)AtaDevice->Port, (UINTN)AtaDevice->PortMultiplierPort));\r
279\r
ad86a50a 280 //\r
281 // Check whether the WORD 88 (supported UltraDMA by drive) is valid\r
282 //\r
283 if ((IdentifyData->field_validity & BIT2) != 0) {\r
284 UdmaMode = IdentifyData->ultra_dma_mode;\r
285 if ((UdmaMode & (BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6)) != 0) {\r
286 //\r
287 // If BIT0~BIT6 is selected, then UDMA is supported\r
288 //\r
289 AtaDevice->UdmaValid = TRUE;\r
290 }\r
291 }\r
292\r
293 Capacity = GetAtapi6Capacity (AtaDevice);\r
294 if (Capacity > MAX_28BIT_ADDRESSING_CAPACITY) {\r
295 //\r
296 // Capacity exceeds 120GB. 48-bit addressing is really needed\r
297 //\r
298 AtaDevice->Lba48Bit = TRUE;\r
299 } else {\r
300 //\r
301 // This is a hard disk <= 120GB capacity, treat it as normal hard disk\r
302 //\r
303 Capacity = ((UINT32)IdentifyData->user_addressable_sectors_hi << 16) | IdentifyData->user_addressable_sectors_lo;\r
304 AtaDevice->Lba48Bit = FALSE;\r
305 }\r
306\r
307 //\r
308 // Block Media Information:\r
309 //\r
310 BlockMedia = &AtaDevice->BlockMedia;\r
311 BlockMedia->LastBlock = Capacity - 1;\r
907c1a00 312 BlockMedia->IoAlign = AtaDevice->AtaBusDriverData->AtaPassThru->Mode->IoAlign;\r
ad86a50a 313 //\r
314 // Check whether Long Physical Sector Feature is supported\r
315 //\r
316 PhyLogicSectorSupport = IdentifyData->phy_logic_sector_support;\r
317 if ((PhyLogicSectorSupport & (BIT14 | BIT15)) == BIT14) {\r
318 //\r
319 // Check whether one physical block contains multiple physical blocks\r
320 //\r
321 if ((PhyLogicSectorSupport & BIT13) != 0) {\r
322 BlockMedia->LogicalBlocksPerPhysicalBlock = (UINT32) (1 << (PhyLogicSectorSupport & 0x000f));\r
323 //\r
324 // Check lowest alignment of logical blocks within physical block\r
325 //\r
326 if ((IdentifyData->alignment_logic_in_phy_blocks & (BIT14 | BIT15)) == BIT14) {\r
c61f9362 327 BlockMedia->LowestAlignedLba = (EFI_LBA) ((BlockMedia->LogicalBlocksPerPhysicalBlock - ((UINT32)IdentifyData->alignment_logic_in_phy_blocks & 0x3fff)) %\r
328 BlockMedia->LogicalBlocksPerPhysicalBlock);\r
ad86a50a 329 }\r
330 }\r
331 //\r
332 // Check logical block size\r
333 //\r
334 if ((PhyLogicSectorSupport & BIT12) != 0) {\r
335 BlockMedia->BlockSize = (UINT32) (((IdentifyData->logic_sector_size_hi << 16) | IdentifyData->logic_sector_size_lo) * sizeof (UINT16));\r
336 }\r
337 AtaDevice->BlockIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION2;\r
338 }\r
339 //\r
340 // Get ATA model name from identify data structure. \r
341 //\r
490b5ea1 342 PrintAtaModelName (AtaDevice);\r
ad86a50a 343\r
344 return EFI_SUCCESS;\r
345}\r
346\r
347\r
348/**\r
349 Discovers whether it is a valid ATA device.\r
350\r
351 This function issues ATA_CMD_IDENTIFY_DRIVE command to the ATA device to identify it.\r
352 If the command is executed successfully, it then identifies it and initializes\r
353 the Media information in Block IO protocol interface.\r
354\r
355 @param AtaDevice The ATA child device involved for the operation.\r
356\r
357 @retval EFI_SUCCESS The device is successfully identified and Media information\r
358 is correctly initialized.\r
359 @return others Some error occurs when discovering the ATA device. \r
360\r
361**/\r
362EFI_STATUS\r
363DiscoverAtaDevice (\r
364 IN OUT ATA_DEVICE *AtaDevice\r
365 )\r
366{\r
367 EFI_STATUS Status;\r
368 EFI_ATA_COMMAND_BLOCK *Acb;\r
369 EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;\r
370 UINTN Retry;\r
371\r
372 //\r
373 // Prepare for ATA command block.\r
374 //\r
375 Acb = ZeroMem (&AtaDevice->Acb, sizeof (*Acb));\r
376 Acb->AtaCommand = ATA_CMD_IDENTIFY_DRIVE;\r
490b5ea1 377 Acb->AtaDeviceHead = (UINT8) (BIT7 | BIT6 | BIT5 | (AtaDevice->PortMultiplierPort << 4));\r
ad86a50a 378\r
379 //\r
380 // Prepare for ATA pass through packet.\r
381 //\r
382 Packet = ZeroMem (&AtaDevice->Packet, sizeof (*Packet));\r
383 Packet->InDataBuffer = AtaDevice->IdentifyData;\r
384 Packet->InTransferLength = sizeof (*AtaDevice->IdentifyData);\r
385 Packet->Protocol = EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN;\r
490b5ea1 386 Packet->Length = EFI_ATA_PASS_THRU_LENGTH_BYTES | EFI_ATA_PASS_THRU_LENGTH_SECTOR_COUNT;\r
387 Packet->Timeout = ATA_TIMEOUT;\r
ad86a50a 388\r
389 Retry = MAX_RETRY_TIMES;\r
390 do {\r
490b5ea1 391 Status = AtaDevicePassThru (AtaDevice, NULL, NULL);\r
ad86a50a 392 if (!EFI_ERROR (Status)) {\r
393 //\r
394 // The command is issued successfully\r
395 //\r
396 Status = IdentifyAtaDevice (AtaDevice);\r
397 if (!EFI_ERROR (Status)) {\r
398 return Status;\r
399 }\r
400 }\r
401 } while (Retry-- > 0);\r
402\r
403 return Status;\r
404}\r
405\r
406/**\r
407 Transfer data from ATA device.\r
408\r
409 This function performs one ATA pass through transaction to transfer data from/to\r
410 ATA device. It chooses the appropriate ATA command and protocol to invoke PassThru\r
411 interface of ATA pass through.\r
412\r
490b5ea1 413 @param[in, out] AtaDevice The ATA child device involved for the operation.\r
414 @param[in, out] TaskPacket Pointer to a Pass Thru Command Packet. Optional, \r
415 if it is NULL, blocking mode, and use the packet\r
416 in AtaDevice. If it is not NULL, non blocking mode,\r
417 and pass down this Packet.\r
418 @param[in, out] Buffer The pointer to the current transaction buffer.\r
419 @param[in] StartLba The starting logical block address to be accessed.\r
420 @param[in] TransferLength The block number or sector count of the transfer.\r
421 @param[in] IsWrite Indicates whether it is a write operation.\r
422 @param[in] Event If Event is NULL, then blocking I/O is performed.\r
423 If Event is not NULL and non-blocking I/O is\r
424 supported,then non-blocking I/O is performed,\r
425 and Event will be signaled when the write\r
426 request is completed.\r
ad86a50a 427\r
428 @retval EFI_SUCCESS The data transfer is complete successfully.\r
429 @return others Some error occurs when transferring data. \r
430\r
431**/\r
432EFI_STATUS\r
433TransferAtaDevice (\r
490b5ea1 434 IN OUT ATA_DEVICE *AtaDevice,\r
435 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *TaskPacket, OPTIONAL\r
436 IN OUT VOID *Buffer,\r
437 IN EFI_LBA StartLba,\r
438 IN UINT32 TransferLength,\r
439 IN BOOLEAN IsWrite, \r
440 IN EFI_EVENT Event OPTIONAL\r
ad86a50a 441 )\r
442{\r
443 EFI_ATA_COMMAND_BLOCK *Acb;\r
444 EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet;\r
445\r
423401f9 446 //\r
61d4f8f9 447 // Ensure AtaDevice->UdmaValid, AtaDevice->Lba48Bit and IsWrite are valid boolean values \r
423401f9 448 //\r
61d4f8f9 449 ASSERT ((UINTN) AtaDevice->UdmaValid < 2);\r
423401f9 450 ASSERT ((UINTN) AtaDevice->Lba48Bit < 2);\r
451 ASSERT ((UINTN) IsWrite < 2);\r
ad86a50a 452 //\r
453 // Prepare for ATA command block.\r
454 //\r
455 Acb = ZeroMem (&AtaDevice->Acb, sizeof (*Acb));\r
456 Acb->AtaCommand = mAtaCommands[AtaDevice->UdmaValid][AtaDevice->Lba48Bit][IsWrite];\r
457 Acb->AtaSectorNumber = (UINT8) StartLba;\r
458 Acb->AtaCylinderLow = (UINT8) RShiftU64 (StartLba, 8);\r
459 Acb->AtaCylinderHigh = (UINT8) RShiftU64 (StartLba, 16);\r
490b5ea1 460 Acb->AtaDeviceHead = (UINT8) (BIT7 | BIT6 | BIT5 | (AtaDevice->PortMultiplierPort << 4));\r
423401f9 461 Acb->AtaSectorCount = (UINT8) TransferLength;\r
ad86a50a 462 if (AtaDevice->Lba48Bit) {\r
463 Acb->AtaSectorNumberExp = (UINT8) RShiftU64 (StartLba, 24);\r
423401f9 464 Acb->AtaCylinderLowExp = (UINT8) RShiftU64 (StartLba, 32);\r
465 Acb->AtaCylinderHighExp = (UINT8) RShiftU64 (StartLba, 40);\r
466 Acb->AtaSectorCountExp = (UINT8) (TransferLength >> 8);\r
ad86a50a 467 } else {\r
468 Acb->AtaDeviceHead = (UINT8) (Acb->AtaDeviceHead | RShiftU64 (StartLba, 24));\r
469 }\r
ad86a50a 470\r
471 //\r
472 // Prepare for ATA pass through packet.\r
473 //\r
490b5ea1 474 if (TaskPacket != NULL) {\r
475 Packet = ZeroMem (TaskPacket, sizeof (*Packet));\r
476 } else {\r
477 Packet = ZeroMem (&AtaDevice->Packet, sizeof (*Packet));\r
478 }\r
479\r
ad86a50a 480 if (IsWrite) {\r
481 Packet->OutDataBuffer = Buffer;\r
482 Packet->OutTransferLength = TransferLength;\r
483 } else {\r
484 Packet->InDataBuffer = Buffer;\r
485 Packet->InTransferLength = TransferLength;\r
486 }\r
490b5ea1 487\r
ad86a50a 488 Packet->Protocol = mAtaPassThruCmdProtocols[AtaDevice->UdmaValid][IsWrite];\r
489 Packet->Length = EFI_ATA_PASS_THRU_LENGTH_SECTOR_COUNT;\r
490b5ea1 490 Packet->Timeout = ATA_TIMEOUT;\r
491\r
492 return AtaDevicePassThru (AtaDevice, TaskPacket, Event);\r
493}\r
494\r
495/**\r
496 Free SubTask. \r
497\r
498 @param[in, out] Task Pointer to task to be freed.\r
499\r
500**/\r
501VOID\r
502EFIAPI \r
503FreeAtaSubTask (\r
504 IN ATA_BUS_ASYN_TASK *Task\r
505 )\r
506{\r
507 if (Task->Packet.Asb != NULL) {\r
508 FreeAlignedBuffer (Task->Packet.Asb, sizeof (Task->Packet.Asb));\r
509 }\r
510 if (Task->Packet.Acb != NULL) {\r
511 FreePool (Task->Packet.Acb);\r
512 }\r
513\r
514 FreePool (Task);\r
515}\r
516\r
517/**\r
518 Call back funtion when the event is signaled.\r
519\r
520 @param[in] Event The Event this notify function registered to.\r
521 @param[in] Context Pointer to the context data registerd to the\r
522 Event.\r
523\r
524**/\r
525VOID\r
526EFIAPI \r
527AtaNonBlockingCallBack (\r
528 IN EFI_EVENT Event,\r
529 IN VOID *Context\r
530 )\r
531{\r
532 ATA_BUS_ASYN_TASK *Task;\r
533\r
534 Task = (ATA_BUS_ASYN_TASK *) Context;\r
535 gBS->CloseEvent (Event);\r
536\r
537 //\r
538 // Check the command status.\r
539 // If there is error during the sub task source allocation, the error status\r
540 // should be returned to the caller directly, so here the Task->Token may already\r
541 // be deleted by the caller and no need to update the status.\r
542 //\r
543 if ((!(*Task->IsError)) && (Task->Packet.Asb->AtaStatus & 0x01) == 0x01) {\r
544 Task->Token->TransactionStatus = EFI_DEVICE_ERROR;\r
545 }\r
546 DEBUG ((\r
547 DEBUG_INFO, \r
548 "NON-BLOCKING EVENT FINISHED!- STATUS = %r\n", \r
549 Task->Token->TransactionStatus\r
550 ));\r
551\r
552 //\r
553 // Reduce the SubEventCount, till it comes to zero.\r
554 //\r
555 (*Task->UnsignalledEventCount) --;\r
556 DEBUG ((DEBUG_INFO, "UnsignalledEventCount = %x\n", *Task->UnsignalledEventCount));\r
557\r
558 //\r
559 // Remove the SubTask from the Task list.\r
560 //\r
561 RemoveEntryList (&Task->TaskEntry);\r
562 if ((*Task->UnsignalledEventCount) == 0) {\r
563 //\r
564 // All Sub tasks are done, then signal the upper layyer event.\r
565 // Except there is error during the sub task source allocation.\r
566 //\r
567 if (!(*Task->IsError)) {\r
568 gBS->SignalEvent (Task->Token->Event);\r
569 DEBUG ((DEBUG_INFO, "Signal Up Level Event UnsignalledEventCount = %x!\n", *Task->UnsignalledEventCount));\r
570 }\r
571 \r
572 FreePool (Task->UnsignalledEventCount);\r
573 FreePool (Task->IsError);\r
574 }\r
ad86a50a 575\r
490b5ea1 576 DEBUG ((\r
577 DEBUG_INFO, \r
578 "PACKET INFO: Write=%s, Lenght=%x, LowCylinder=%x, HighCylinder=%x,SectionNumber=%x",\r
579 Task->Packet.OutDataBuffer != NULL ? L"YES" : L"NO",\r
580 Task->Packet.OutDataBuffer != NULL ? Task->Packet.OutTransferLength : Task->Packet.InTransferLength,\r
581 Task->Packet.Acb->AtaCylinderLow,\r
582 Task->Packet.Acb->AtaCylinderHigh,\r
583 Task->Packet.Acb->AtaSectorCount\r
584 ));\r
585\r
586 //\r
587 // Free the buffer of SubTask.\r
588 //\r
589 FreeAtaSubTask (Task);\r
ad86a50a 590}\r
591\r
592/**\r
593 Read or write a number of blocks from ATA device.\r
594\r
595 This function performs ATA pass through transactions to read/write data from/to\r
596 ATA device. It may separate the read/write request into several ATA pass through\r
597 transactions.\r
598\r
490b5ea1 599 @param[in, out] AtaDevice The ATA child device involved for the operation.\r
600 @param[in, out] Buffer The pointer to the current transaction buffer.\r
601 @param[in] StartLba The starting logical block address to be accessed.\r
602 @param[in] NumberOfBlocks The block number or sector count of the transfer.\r
603 @param[in] IsWrite Indicates whether it is a write operation.\r
604 @param[in, out] Token A pointer to the token associated with the transaction.\r
ad86a50a 605\r
606 @retval EFI_SUCCESS The data transfer is complete successfully.\r
607 @return others Some error occurs when transferring data. \r
608\r
609**/\r
610EFI_STATUS \r
611AccessAtaDevice(\r
612 IN OUT ATA_DEVICE *AtaDevice,\r
613 IN OUT UINT8 *Buffer,\r
614 IN EFI_LBA StartLba,\r
615 IN UINTN NumberOfBlocks,\r
490b5ea1 616 IN BOOLEAN IsWrite,\r
617 IN OUT EFI_BLOCK_IO2_TOKEN *Token\r
ad86a50a 618 )\r
619{\r
620 EFI_STATUS Status;\r
621 UINTN MaxTransferBlockNumber;\r
622 UINTN TransferBlockNumber;\r
623 UINTN BlockSize;\r
490b5ea1 624 UINTN *EventCount;\r
625 UINTN TempCount;\r
626 ATA_BUS_ASYN_TASK *Task;\r
627 EFI_EVENT SubEvent;\r
628 UINTN Index;\r
629 BOOLEAN *IsError;\r
630 EFI_TPL OldTpl;\r
631\r
632 SubEvent = NULL;\r
633 TempCount = 0;\r
634 Status = EFI_SUCCESS;\r
635\r
636 EventCount = AllocateZeroPool (sizeof (UINTN));\r
637 if (EventCount == NULL) {\r
638 return EFI_OUT_OF_RESOURCES;\r
639 }\r
640 \r
641 IsError = AllocateZeroPool (sizeof (BOOLEAN));\r
642 if (IsError == NULL) {\r
643 goto EXIT;\r
644 }\r
645 *IsError = FALSE;\r
646\r
647 //\r
648 // Initial the return status for Non Blocking.\r
649 //\r
650 if (Token != NULL && Token->Event != NULL) {\r
651 Token->TransactionStatus = EFI_SUCCESS;\r
652 }\r
423401f9 653 //\r
654 // Ensure AtaDevice->Lba48Bit is a valid boolean value \r
655 //\r
656 ASSERT ((UINTN) AtaDevice->Lba48Bit < 2);\r
ad86a50a 657 MaxTransferBlockNumber = mMaxTransferBlockNumber[AtaDevice->Lba48Bit];\r
490b5ea1 658 BlockSize = AtaDevice->BlockMedia.BlockSize;\r
659\r
660 TempCount = (NumberOfBlocks + MaxTransferBlockNumber - 1) / MaxTransferBlockNumber;\r
661 *EventCount = TempCount;\r
662 Index = 0;\r
663\r
ad86a50a 664 do {\r
665 if (NumberOfBlocks > MaxTransferBlockNumber) {\r
666 TransferBlockNumber = MaxTransferBlockNumber;\r
490b5ea1 667 NumberOfBlocks -= MaxTransferBlockNumber;\r
ad86a50a 668 } else {\r
669 TransferBlockNumber = NumberOfBlocks;\r
670 NumberOfBlocks = 0;\r
671 }\r
672\r
490b5ea1 673 //\r
674 // Create sub event for the sub Ata task. Non-Blocking Mode.\r
675 //\r
676 if (Token != NULL && Token->Event != NULL) {\r
677 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
678 Task = AllocateZeroPool (sizeof (ATA_BUS_ASYN_TASK));\r
679 if (Task == NULL) {\r
680 //\r
681 // If resource allocation fail, reduce the total sub event counts.\r
682 //\r
683 *EventCount = (*EventCount) - (TempCount - Index);\r
684 *IsError = TRUE;\r
685 Token->TransactionStatus = EFI_OUT_OF_RESOURCES;\r
686 Status = EFI_OUT_OF_RESOURCES;\r
687\r
688 gBS->RestoreTPL (OldTpl);\r
689 goto EXIT;\r
690 }\r
691\r
692 Task->UnsignalledEventCount = EventCount;\r
693 Task->Token = Token;\r
694 Task->IsError = IsError;\r
695\r
696 InsertTailList (&AtaDevice->AtaTaskList, &Task->TaskEntry);\r
697\r
698 Status = gBS->CreateEvent (\r
699 EVT_NOTIFY_SIGNAL,\r
700 TPL_NOTIFY,\r
701 AtaNonBlockingCallBack,\r
702 Task,\r
703 &SubEvent\r
704 );\r
705 //\r
706 // If resource allocation fail, the un-signalled event count should equal to\r
707 // the original one minus the unassigned subtasks number.\r
708 //\r
709 if (EFI_ERROR (Status)) {\r
710 *EventCount = (*EventCount) - (TempCount - Index);\r
711 *IsError = TRUE;\r
712 gBS->RestoreTPL (OldTpl);\r
713 goto EXIT;\r
714 }\r
715 Index++;\r
716 gBS->RestoreTPL (OldTpl); \r
717\r
718 DEBUG ((EFI_D_INFO, "NON-BLOCKING SET EVENT START: WRITE = %d\n", IsWrite));\r
719 Status = TransferAtaDevice (AtaDevice, &Task->Packet, Buffer, StartLba, (UINT32) TransferBlockNumber, IsWrite, SubEvent);\r
720 DEBUG ((\r
721 EFI_D_INFO,\r
722 "NON-BLOCKING SET EVENT END:StartLba=%x, TransferBlockNumbers=%x, Status=%r\n",\r
723 StartLba,\r
724 TransferBlockNumber,\r
725 Status\r
726 ));\r
727 }else {\r
728 //\r
729 // Blocking Mode.\r
730 //\r
731 DEBUG ((EFI_D_INFO, "BLOCKING BLOCK I/O START: WRITE = %d\n", IsWrite));\r
732 Status = TransferAtaDevice (AtaDevice, NULL, Buffer, StartLba, (UINT32) TransferBlockNumber, IsWrite, NULL);\r
733 DEBUG ((\r
734 EFI_D_INFO,\r
735 "BLOCKING BLOCK I/O FINISHE - StartLba = %x; TransferBlockNumbers = %x, status = %r\n", \r
736 StartLba,\r
737 TransferBlockNumber,\r
738 Status\r
739 ));\r
740 }\r
741\r
ad86a50a 742 if (EFI_ERROR (Status)) {\r
490b5ea1 743 goto EXIT;\r
ad86a50a 744 }\r
490b5ea1 745\r
ad86a50a 746 StartLba += TransferBlockNumber;\r
747 Buffer += TransferBlockNumber * BlockSize;\r
748 } while (NumberOfBlocks > 0);\r
749\r
490b5ea1 750EXIT:\r
751\r
752 if (*EventCount == 0) {\r
753 FreePool (EventCount);\r
754 FreePool (IsError);\r
755 }\r
756\r
ad86a50a 757 return Status;\r
758}\r