]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Pci/IdeBusDxe/Ata.c
IntelFrameworkModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Pci / IdeBusDxe / Ata.c
1 /** @file
2 This file contains all helper functions on the ATA command
3
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 @par Revision Reference:
8 2002-6: Add Atapi6 enhancement, support >120GB hard disk, including
9 update - ATAIdentity() func
10 update - AtaBlockIoReadBlocks() func
11 update - AtaBlockIoWriteBlocks() func
12 add - AtaAtapi6Identify() func
13 add - AtaReadSectorsExt() func
14 add - AtaWriteSectorsExt() func
15 add - AtaPioDataInExt() func
16 add - AtaPioDataOutExt() func
17
18 **/
19
20 #include "IdeBus.h"
21 /**
22 This function is called by ATAIdentify() to identity whether this disk
23 supports ATA/ATAPI6 48bit addressing, ie support >120G capacity
24
25 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record
26 all the information of the IDE device.
27
28 @retval EFI_SUCCESS The disk specified by IdeDev is a Atapi6 supported one and
29 48-bit addressing must be used
30 @retval EFI_UNSUPPORTED The disk dosn't not support Atapi6 or it supports but the
31 capacity is below 120G, 48bit addressing is not needed
32 @retval EFI_DEVICE_ERROR The identify data in IdeDev is incorrect
33 @retval EFI_INVALID_PARAMETER The identify data in IdeDev is NULL.
34
35 @note This function must be called after DEVICE_IDENTITY command has been
36 successfully returned
37
38 **/
39 EFI_STATUS
40 AtaAtapi6Identify (
41 IN IDE_BLK_IO_DEV *IdeDev
42 )
43 {
44 UINT8 Index;
45 EFI_LBA TmpLba;
46 EFI_LBA Capacity;
47 EFI_IDENTIFY_DATA *Atapi6IdentifyStruct;
48
49 if (IdeDev->IdData == NULL) {
50 return EFI_INVALID_PARAMETER;
51 }
52
53 Atapi6IdentifyStruct = IdeDev->IdData;
54
55 if ((Atapi6IdentifyStruct->AtapiData.cmd_set_support_83 & (BIT15 | BIT14)) != 0x4000) {
56 //
57 // Per ATA-6 spec, word83: bit15 is zero and bit14 is one
58 //
59 return EFI_DEVICE_ERROR;
60 }
61
62 if ((Atapi6IdentifyStruct->AtapiData.cmd_set_support_83 & BIT10) == 0) {
63 //
64 // The device dosn't support 48 bit addressing
65 //
66 return EFI_UNSUPPORTED;
67 }
68
69 //
70 // 48 bit address feature set is supported, get maximum capacity
71 //
72 Capacity = Atapi6IdentifyStruct->AtaData.maximum_lba_for_48bit_addressing[0];
73 for (Index = 1; Index < 4; Index++) {
74 //
75 // Lower byte goes first: word[100] is the lowest word, word[103] is highest
76 //
77 TmpLba = Atapi6IdentifyStruct->AtaData.maximum_lba_for_48bit_addressing[Index];
78 Capacity |= LShiftU64 (TmpLba, 16 * Index);
79 }
80
81 if (Capacity > MAX_28BIT_ADDRESSING_CAPACITY) {
82 //
83 // Capacity exceeds 120GB. 48-bit addressing is really needed
84 //
85 IdeDev->Type = Ide48bitAddressingHardDisk;
86
87 //
88 // Fill block media information:Media->LogicalPartition ,
89 // Media->WriteCaching will be filledin the DiscoverIdeDevcie() function.
90 //
91 IdeDev->BlkIo.Media->IoAlign = 4;
92 IdeDev->BlkIo.Media->MediaId = 1;
93 IdeDev->BlkIo.Media->RemovableMedia = FALSE;
94 IdeDev->BlkIo.Media->MediaPresent = TRUE;
95 IdeDev->BlkIo.Media->ReadOnly = FALSE;
96 IdeDev->BlkIo.Media->BlockSize = 0x200;
97 IdeDev->BlkIo.Media->LastBlock = Capacity - 1;
98
99 return EFI_SUCCESS;
100 }
101
102 return EFI_UNSUPPORTED;
103 }
104 /**
105 Enable SMART of the disk if supported
106
107 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure,used to record
108 all the information of the IDE device.
109 **/
110 VOID
111 AtaSMARTSupport (
112 IN IDE_BLK_IO_DEV *IdeDev
113 )
114 {
115 EFI_STATUS Status;
116 BOOLEAN SMARTSupported;
117 UINT8 Device;
118 EFI_IDENTIFY_DATA *TmpAtaIdentifyPointer;
119 UINT8 DeviceSelect;
120 UINT8 LBAMid;
121 UINT8 LBAHigh;
122
123 //
124 // Detect if the device supports S.M.A.R.T.
125 //
126 if ((IdeDev->IdData->AtaData.command_set_supported_83 & 0xc000) != 0x4000) {
127 //
128 // Data in word 82 is not valid (bit15 shall be zero and bit14 shall be to one)
129 //
130 return ;
131 } else {
132 if ((IdeDev->IdData->AtaData.command_set_supported_82 & 0x0001) != 0x0001) {
133 //
134 // S.M.A.R.T is not supported by the device
135 //
136 SMARTSupported = FALSE;
137 } else {
138 SMARTSupported = TRUE;
139 }
140 }
141
142 if (!SMARTSupported) {
143 //
144 // Report nonsupport status code
145 //
146 REPORT_STATUS_CODE (
147 EFI_ERROR_CODE | EFI_ERROR_MINOR,
148 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_NOTSUPPORTED)
149 );
150 } else {
151 //
152 // Enable this feature
153 //
154 REPORT_STATUS_CODE (
155 EFI_PROGRESS_CODE,
156 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_ENABLE)
157 );
158
159 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0);
160 Status = AtaNonDataCommandIn (
161 IdeDev,
162 ATA_CMD_SMART,
163 Device,
164 ATA_SMART_ENABLE_OPERATION,
165 0,
166 0,
167 ATA_CONSTANT_4F,
168 ATA_CONSTANT_C2
169 );
170 //
171 // Detect if this feature is enabled
172 //
173 TmpAtaIdentifyPointer = (EFI_IDENTIFY_DATA *) AllocateZeroPool (sizeof (EFI_IDENTIFY_DATA));
174 if (TmpAtaIdentifyPointer == NULL) {
175 return;
176 }
177
178 DeviceSelect = (UINT8) ((IdeDev->Device) << 4);
179 Status = AtaPioDataIn (
180 IdeDev,
181 (VOID *) TmpAtaIdentifyPointer,
182 sizeof (EFI_IDENTIFY_DATA),
183 ATA_CMD_IDENTIFY_DRIVE,
184 DeviceSelect,
185 0,
186 0,
187 0,
188 0
189 );
190 if (EFI_ERROR (Status)) {
191 gBS->FreePool (TmpAtaIdentifyPointer);
192 return ;
193 }
194
195 //
196 // Check if the feature is enabled
197 //
198 if ((TmpAtaIdentifyPointer->AtaData.command_set_feature_enb_85 & 0x0001) == 0x0001) {
199 //
200 // Read status data
201 //
202 AtaNonDataCommandIn (
203 IdeDev,
204 ATA_CMD_SMART,
205 Device,
206 ATA_SMART_RETURN_STATUS,
207 0,
208 0,
209 ATA_CONSTANT_4F,
210 ATA_CONSTANT_C2
211 );
212 LBAMid = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb);
213 LBAHigh = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb);
214
215 if ((LBAMid == 0x4f) && (LBAHigh == 0xc2)) {
216 //
217 // The threshold exceeded condition is not detected by the device
218 //
219 REPORT_STATUS_CODE (
220 EFI_PROGRESS_CODE,
221 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_UNDERTHRESHOLD)
222 );
223
224 } else if ((LBAMid == 0xf4) && (LBAHigh == 0x2c)) {
225 //
226 // The threshold exceeded condition is detected by the device
227 //
228 REPORT_STATUS_CODE (
229 EFI_PROGRESS_CODE,
230 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_OVERTHRESHOLD)
231 );
232 }
233
234 } else {
235 //
236 // Report disabled status code
237 //
238 REPORT_STATUS_CODE (
239 EFI_ERROR_CODE | EFI_ERROR_MINOR,
240 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_DISABLED)
241 );
242 }
243
244 gBS->FreePool (TmpAtaIdentifyPointer);
245 }
246
247 return ;
248 }
249 /**
250 Sends out an ATA Identify Command to the specified device.
251
252 This function is called by DiscoverIdeDevice() during its device
253 identification. It sends out the ATA Identify Command to the
254 specified device. Only ATA device responses to this command. If
255 the command succeeds, it returns the Identify data structure which
256 contains information about the device. This function extracts the
257 information it needs to fill the IDE_BLK_IO_DEV data structure,
258 including device type, media block size, media capacity, and etc.
259
260 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure,used to record
261 all the information of the IDE device.
262
263 @retval EFI_SUCCESS Identify ATA device successfully.
264 @retval EFI_DEVICE_ERROR ATA Identify Device Command failed or device is not ATA device.
265 @note parameter IdeDev will be updated in this function.
266
267 **/
268 EFI_STATUS
269 ATAIdentify (
270 IN IDE_BLK_IO_DEV *IdeDev
271 )
272 {
273 EFI_STATUS Status;
274 EFI_IDENTIFY_DATA *AtaIdentifyPointer;
275 UINT32 Capacity;
276 UINT8 DeviceSelect;
277 UINTN Retry;
278
279 //
280 // AtaIdentifyPointer is used for accommodating returned IDENTIFY data of
281 // the ATA Identify command
282 //
283 AtaIdentifyPointer = (EFI_IDENTIFY_DATA *) AllocateZeroPool (sizeof (EFI_IDENTIFY_DATA));
284 if (AtaIdentifyPointer == NULL) {
285 return EFI_OUT_OF_RESOURCES;
286 }
287
288 //
289 // use ATA PIO Data In protocol to send ATA Identify command
290 // and receive data from device
291 //
292 DeviceSelect = (UINT8) ((IdeDev->Device) << 4);
293
294
295 Retry = 3;
296 while (Retry > 0) {
297 Status = AtaPioDataIn (
298 IdeDev,
299 (VOID *) AtaIdentifyPointer,
300 sizeof (EFI_IDENTIFY_DATA),
301 ATA_CMD_IDENTIFY_DRIVE,
302 DeviceSelect,
303 0,
304 0,
305 0,
306 0
307 );
308 //
309 // If ATA Identify command succeeds, then according to the received
310 // IDENTIFY data,
311 // identify the device type ( ATA or not ).
312 // If ATA device, fill the information in IdeDev.
313 // If not ATA device, return IDE_DEVICE_ERROR
314 //
315 if (!EFI_ERROR (Status)) {
316
317 IdeDev->IdData = AtaIdentifyPointer;
318
319 //
320 // Print ATA Module Name
321 //
322 PrintAtaModuleName (IdeDev);
323
324 //
325 // bit 15 of pAtaIdentify->config is used to identify whether device is
326 // ATA device or ATAPI device.
327 // if 0, means ATA device; if 1, means ATAPI device.
328 //
329 if ((AtaIdentifyPointer->AtaData.config & 0x8000) == 0x00) {
330 //
331 // Detect if support S.M.A.R.T. If yes, enable it as default
332 //
333 AtaSMARTSupport (IdeDev);
334
335 //
336 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device)
337 //
338 Status = AtaAtapi6Identify (IdeDev);
339 if (!EFI_ERROR (Status)) {
340 //
341 // It's a disk with >120GB capacity, initialized in AtaAtapi6Identify()
342 //
343 return EFI_SUCCESS;
344 } else if (Status == EFI_DEVICE_ERROR) {
345 //
346 // Some disk with big capacity (>200GB) is slow when being identified
347 // and will return all zero for word83.
348 // We try twice at first. If it fails, we do a SoftRest and try again.
349 //
350 Retry--;
351 if (Retry == 1) {
352 //
353 // Do a SoftRest before the third attempt.
354 //
355 AtaSoftReset (IdeDev);
356 }
357 continue;
358 }
359 //
360 // This is a hard disk <= 120GB capacity, treat it as normal hard disk
361 //
362 IdeDev->Type = IdeHardDisk;
363
364 //
365 // Block Media Information:
366 // Media->LogicalPartition , Media->WriteCaching will be filled
367 // in the DiscoverIdeDevcie() function.
368 //
369 IdeDev->BlkIo.Media->IoAlign = 4;
370 IdeDev->BlkIo.Media->MediaId = 1;
371 IdeDev->BlkIo.Media->RemovableMedia = FALSE;
372 IdeDev->BlkIo.Media->MediaPresent = TRUE;
373 IdeDev->BlkIo.Media->ReadOnly = FALSE;
374 IdeDev->BlkIo.Media->BlockSize = 0x200;
375
376 //
377 // Calculate device capacity
378 //
379 Capacity = ((UINT32)AtaIdentifyPointer->AtaData.user_addressable_sectors_hi << 16) |
380 AtaIdentifyPointer->AtaData.user_addressable_sectors_lo ;
381 IdeDev->BlkIo.Media->LastBlock = Capacity - 1;
382
383 return EFI_SUCCESS;
384 }
385
386 }
387 break;
388 }
389
390 gBS->FreePool (AtaIdentifyPointer);
391 //
392 // Make sure the pIdData will not be freed again.
393 //
394 IdeDev->IdData = NULL;
395
396 return EFI_DEVICE_ERROR;
397 }
398
399 /**
400 This function is a helper function used to change the char order in a string. It
401 is designed specially for the PrintAtaModuleName() function. After the IDE device
402 is detected, the IDE driver gets the device module name by sending ATA command
403 called ATA Identify Command or ATAPI Identify Command to the specified IDE device.
404 The module name returned is a string of ASCII characters: the first character is bit8--bit15
405 of the first word, the second character is BIT0--bit7 of the first word and so on. Thus
406 the string can not be print directly before it is preprocessed by this func to change
407 the order of characters in each word in the string.
408
409 @param Destination Indicates the destination string.
410 @param Source Indicates the source string.
411 @param Size the length of the string
412 **/
413 VOID
414 SwapStringChars (
415 IN CHAR8 *Destination,
416 IN CHAR8 *Source,
417 IN UINT32 Size
418 )
419 {
420 UINT32 Index;
421 CHAR8 Temp;
422
423 for (Index = 0; Index < Size; Index += 2) {
424
425 Temp = Source[Index + 1];
426 Destination[Index + 1] = Source[Index];
427 Destination[Index] = Temp;
428 }
429 }
430 /**
431 This function is called by ATAIdentify() or ATAPIIdentify() to print device's module name.
432
433 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record
434 all the information of the IDE device.
435 **/
436 VOID
437 PrintAtaModuleName (
438 IN IDE_BLK_IO_DEV *IdeDev
439 )
440 {
441 if (IdeDev->IdData == NULL) {
442 return ;
443 }
444
445 SwapStringChars (IdeDev->ModelName, IdeDev->IdData->AtaData.ModelName, 40);
446 IdeDev->ModelName[40] = 0x00;
447 }
448
449 /**
450 This function is used to send out ATA commands conforms to the PIO Data In Protocol.
451
452 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record
453 all the information of the IDE device.
454 @param Buffer buffer contained data transferred from device to host.
455 @param ByteCount data size in byte unit of the buffer.
456 @param AtaCommand value of the Command Register
457 @param Head value of the Head/Device Register
458 @param SectorCount value of the Sector Count Register
459 @param SectorNumber value of the Sector Number Register
460 @param CylinderLsb value of the low byte of the Cylinder Register
461 @param CylinderMsb value of the high byte of the Cylinder Register
462
463 @retval EFI_SUCCESS send out the ATA command and device send required data successfully.
464 @retval EFI_DEVICE_ERROR command sent failed.
465
466 **/
467 EFI_STATUS
468 AtaPioDataIn (
469 IN IDE_BLK_IO_DEV *IdeDev,
470 IN VOID *Buffer,
471 IN UINT32 ByteCount,
472 IN UINT8 AtaCommand,
473 IN UINT8 Head,
474 IN UINT8 SectorCount,
475 IN UINT8 SectorNumber,
476 IN UINT8 CylinderLsb,
477 IN UINT8 CylinderMsb
478 )
479 {
480 UINTN WordCount;
481 UINTN Increment;
482 UINT16 *Buffer16;
483 EFI_STATUS Status;
484
485 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
486 if (EFI_ERROR (Status)) {
487 return EFI_DEVICE_ERROR;
488 }
489
490 //
491 // e0:1110,0000-- bit7 and bit5 are reserved bits.
492 // bit6 set means LBA mode
493 //
494 IDEWritePortB (
495 IdeDev->PciIo,
496 IdeDev->IoPort->Head,
497 (UINT8) ((IdeDev->Device << 4) | 0xe0 | Head)
498 );
499
500 //
501 // All ATAPI device's ATA commands can be issued regardless of the
502 // state of the DRDY
503 //
504 if (IdeDev->Type == IdeHardDisk) {
505
506 Status = DRDYReady (IdeDev, ATATIMEOUT);
507 if (EFI_ERROR (Status)) {
508 return EFI_DEVICE_ERROR;
509 }
510 }
511 //
512 // set all the command parameters
513 // Before write to all the following registers, BSY and DRQ must be 0.
514 //
515 Status = DRQClear2 (IdeDev, ATATIMEOUT);
516 if (EFI_ERROR (Status)) {
517 return EFI_DEVICE_ERROR;
518 }
519
520 if (AtaCommand == ATA_CMD_SET_FEATURES) {
521 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03);
522 }
523
524 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount);
525 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, SectorNumber);
526 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, CylinderLsb);
527 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, CylinderMsb);
528
529 //
530 // send command via Command Register
531 //
532 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
533
534 Buffer16 = (UINT16 *) Buffer;
535
536 //
537 // According to PIO data in protocol, host can perform a series of reads to
538 // the data register after each time device set DRQ ready;
539 // The data size of "a series of read" is command specific.
540 // For most ATA command, data size received from device will not exceed
541 // 1 sector, hence the data size for "a series of read" can be the whole data
542 // size of one command request.
543 // For ATA command such as Read Sector command, the data size of one ATA
544 // command request is often larger than 1 sector, according to the
545 // Read Sector command, the data size of "a series of read" is exactly 1
546 // sector.
547 // Here for simplification reason, we specify the data size for
548 // "a series of read" to 1 sector (256 words) if data size of one ATA command
549 // request is larger than 256 words.
550 //
551 Increment = 256;
552
553 //
554 // used to record bytes of currently transfered data
555 //
556 WordCount = 0;
557
558 while (WordCount < ByteCount / 2) {
559 //
560 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
561 //
562 Status = DRQReady2 (IdeDev, ATATIMEOUT);
563 if (EFI_ERROR (Status)) {
564 return EFI_DEVICE_ERROR;
565 }
566
567 Status = CheckErrorStatus (IdeDev);
568 if (EFI_ERROR (Status)) {
569 return EFI_DEVICE_ERROR;
570 }
571
572 //
573 // Get the byte count for one series of read
574 //
575 if ((WordCount + Increment) > ByteCount / 2) {
576 Increment = ByteCount / 2 - WordCount;
577 }
578
579 IDEReadPortWMultiple (
580 IdeDev->PciIo,
581 IdeDev->IoPort->Data,
582 Increment,
583 Buffer16
584 );
585
586 WordCount += Increment;
587 Buffer16 += Increment;
588
589 }
590
591 DRQClear (IdeDev, ATATIMEOUT);
592
593 return CheckErrorStatus (IdeDev);
594 }
595
596 /**
597 This function is used to send out ATA commands conforms to the
598 PIO Data Out Protocol.
599
600 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
601 to record all the information of the IDE device.
602 @param *Buffer buffer contained data transferred from host to device.
603 @param ByteCount data size in byte unit of the buffer.
604 @param AtaCommand value of the Command Register
605 @param Head value of the Head/Device Register
606 @param SectorCount value of the Sector Count Register
607 @param SectorNumber value of the Sector Number Register
608 @param CylinderLsb value of the low byte of the Cylinder Register
609 @param CylinderMsb value of the high byte of the Cylinder Register
610
611 @retval EFI_SUCCESS send out the ATA command and device received required
612 data successfully.
613 @retval EFI_DEVICE_ERROR command sent failed.
614
615 **/
616 EFI_STATUS
617 AtaPioDataOut (
618 IN IDE_BLK_IO_DEV *IdeDev,
619 IN VOID *Buffer,
620 IN UINT32 ByteCount,
621 IN UINT8 AtaCommand,
622 IN UINT8 Head,
623 IN UINT8 SectorCount,
624 IN UINT8 SectorNumber,
625 IN UINT8 CylinderLsb,
626 IN UINT8 CylinderMsb
627 )
628 {
629 UINTN WordCount;
630 UINTN Increment;
631 UINT16 *Buffer16;
632 EFI_STATUS Status;
633
634 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
635 if (EFI_ERROR (Status)) {
636 return EFI_DEVICE_ERROR;
637 }
638
639 //
640 // select device via Head/Device register.
641 // Before write Head/Device register, BSY and DRQ must be 0.
642 //
643 Status = DRQClear2 (IdeDev, ATATIMEOUT);
644 if (EFI_ERROR (Status)) {
645 return EFI_DEVICE_ERROR;
646 }
647
648 //
649 // e0:1110,0000-- bit7 and bit5 are reserved bits.
650 // bit6 set means LBA mode
651 //
652 IDEWritePortB (
653 IdeDev->PciIo,
654 IdeDev->IoPort->Head,
655 (UINT8) ((IdeDev->Device << 4) | 0xe0 | Head)
656 );
657
658 Status = DRDYReady (IdeDev, ATATIMEOUT);
659 if (EFI_ERROR (Status)) {
660 return EFI_DEVICE_ERROR;
661 }
662
663 //
664 // set all the command parameters
665 // Before write to all the following registers, BSY and DRQ must be 0.
666 //
667 Status = DRQClear2 (IdeDev, ATATIMEOUT);
668 if (EFI_ERROR (Status)) {
669 return EFI_DEVICE_ERROR;
670 }
671
672 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount);
673 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, SectorNumber);
674 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, CylinderLsb);
675 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, CylinderMsb);
676
677 //
678 // send command via Command Register
679 //
680 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
681
682 Buffer16 = (UINT16 *) Buffer;
683
684 //
685 // According to PIO data out protocol, host can perform a series of
686 // writes to the data register after each time device set DRQ ready;
687 // The data size of "a series of read" is command specific.
688 // For most ATA command, data size written to device will not exceed 1 sector,
689 // hence the data size for "a series of write" can be the data size of one
690 // command request.
691 // For ATA command such as Write Sector command, the data size of one
692 // ATA command request is often larger than 1 sector, according to the
693 // Write Sector command, the data size of "a series of read" is exactly
694 // 1 sector.
695 // Here for simplification reason, we specify the data size for
696 // "a series of write" to 1 sector (256 words) if data size of one ATA command
697 // request is larger than 256 words.
698 //
699 Increment = 256;
700 WordCount = 0;
701
702 while (WordCount < ByteCount / 2) {
703
704 //
705 // DRQReady2-- read Alternate Status Register to determine the DRQ bit
706 // data transfer can be performed only when DRQ is ready.
707 //
708 Status = DRQReady2 (IdeDev, ATATIMEOUT);
709 if (EFI_ERROR (Status)) {
710 return EFI_DEVICE_ERROR;
711 }
712
713 Status = CheckErrorStatus (IdeDev);
714 if (EFI_ERROR (Status)) {
715 return EFI_DEVICE_ERROR;
716 }
717
718 //
719 // Check the remaining byte count is less than 512 bytes
720 //
721 if ((WordCount + Increment) > ByteCount / 2) {
722 Increment = ByteCount / 2 - WordCount;
723 }
724 //
725 // perform a series of write without check DRQ ready
726 //
727
728 IDEWritePortWMultiple (
729 IdeDev->PciIo,
730 IdeDev->IoPort->Data,
731 Increment,
732 Buffer16
733 );
734 WordCount += Increment;
735 Buffer16 += Increment;
736
737 }
738
739 DRQClear (IdeDev, ATATIMEOUT);
740
741 return CheckErrorStatus (IdeDev);
742 }
743
744 /**
745 This function is used to analyze the Status Register and print out
746 some debug information and if there is ERR bit set in the Status
747 Register, the Error Register's value is also be parsed and print out.
748
749 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to
750 record all the information of the IDE device.
751
752 @retval EFI_SUCCESS No err information in the Status Register.
753 @retval EFI_DEVICE_ERROR Any err information in the Status Register.
754
755 **/
756 EFI_STATUS
757 CheckErrorStatus (
758 IN IDE_BLK_IO_DEV *IdeDev
759 )
760 {
761 UINT8 StatusRegister;
762 UINT8 ErrorRegister;
763
764 StatusRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Status);
765
766 DEBUG_CODE_BEGIN ();
767
768 if ((StatusRegister & ATA_STSREG_DWF) != 0) {
769 DEBUG (
770 (EFI_D_BLKIO,
771 "CheckErrorStatus()-- %02x : Error : Write Fault\n",
772 StatusRegister)
773 );
774 }
775
776 if ((StatusRegister & ATA_STSREG_CORR) != 0) {
777 DEBUG (
778 (EFI_D_BLKIO,
779 "CheckErrorStatus()-- %02x : Error : Corrected Data\n",
780 StatusRegister)
781 );
782 }
783
784 if ((StatusRegister & ATA_STSREG_ERR) != 0) {
785 ErrorRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Error);
786
787 if ((ErrorRegister & ATA_ERRREG_BBK) != 0) {
788 DEBUG (
789 (EFI_D_BLKIO,
790 "CheckErrorStatus()-- %02x : Error : Bad Block Detected\n",
791 ErrorRegister)
792 );
793 }
794
795 if ((ErrorRegister & ATA_ERRREG_UNC) != 0) {
796 DEBUG (
797 (EFI_D_BLKIO,
798 "CheckErrorStatus()-- %02x : Error : Uncorrectable Data\n",
799 ErrorRegister)
800 );
801 }
802
803 if ((ErrorRegister & ATA_ERRREG_MC) != 0) {
804 DEBUG (
805 (EFI_D_BLKIO,
806 "CheckErrorStatus()-- %02x : Error : Media Change\n",
807 ErrorRegister)
808 );
809 }
810
811 if ((ErrorRegister & ATA_ERRREG_ABRT) != 0) {
812 DEBUG (
813 (EFI_D_BLKIO,
814 "CheckErrorStatus()-- %02x : Error : Abort\n",
815 ErrorRegister)
816 );
817 }
818
819 if ((ErrorRegister & ATA_ERRREG_TK0NF) != 0) {
820 DEBUG (
821 (EFI_D_BLKIO,
822 "CheckErrorStatus()-- %02x : Error : Track 0 Not Found\n",
823 ErrorRegister)
824 );
825 }
826
827 if ((ErrorRegister & ATA_ERRREG_AMNF) != 0) {
828 DEBUG (
829 (EFI_D_BLKIO,
830 "CheckErrorStatus()-- %02x : Error : Address Mark Not Found\n",
831 ErrorRegister)
832 );
833 }
834 }
835
836 DEBUG_CODE_END ();
837
838 if ((StatusRegister & (ATA_STSREG_ERR | ATA_STSREG_DWF | ATA_STSREG_CORR)) == 0) {
839 return EFI_SUCCESS;
840 }
841
842 return EFI_DEVICE_ERROR;
843
844 }
845
846 /**
847 This function is called by the AtaBlkIoReadBlocks() to perform reading from
848 media in block unit.
849
850 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record
851 all the information of the IDE device.
852 @param DataBuffer A pointer to the destination buffer for the data.
853 @param Lba The starting logical block address to read from on the device media.
854 @param NumberOfBlocks The number of transfer data blocks.
855
856 @return status is fully dependent on the return status of AtaPioDataIn() function.
857
858 **/
859 EFI_STATUS
860 AtaReadSectors (
861 IN IDE_BLK_IO_DEV *IdeDev,
862 IN VOID *DataBuffer,
863 IN EFI_LBA Lba,
864 IN UINTN NumberOfBlocks
865 )
866 {
867 EFI_STATUS Status;
868 UINTN BlocksRemaining;
869 UINT32 Lba32;
870 UINT8 Lba0;
871 UINT8 Lba1;
872 UINT8 Lba2;
873 UINT8 Lba3;
874 UINT8 AtaCommand;
875 UINT8 SectorCount8;
876 UINT16 SectorCount;
877 UINTN ByteCount;
878 VOID *Buffer;
879
880 Buffer = DataBuffer;
881
882 //
883 // Using ATA Read Sector(s) command (opcode=0x20) with PIO DATA IN protocol
884 //
885 AtaCommand = ATA_CMD_READ_SECTORS;
886
887
888 BlocksRemaining = NumberOfBlocks;
889
890 Lba32 = (UINT32) Lba;
891
892 Status = EFI_SUCCESS;
893
894 while (BlocksRemaining > 0) {
895
896 //
897 // in ATA-3 spec, LBA is in 28 bit width
898 //
899 Lba0 = (UINT8) Lba32;
900 Lba1 = (UINT8) (Lba32 >> 8);
901 Lba2 = (UINT8) (Lba32 >> 16);
902 //
903 // low 4 bit of Lba3 stands for LBA bit24~bit27.
904 //
905 Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f);
906
907 if (BlocksRemaining >= 0x100) {
908
909 //
910 // SectorCount8 is sent to Sector Count register, 0x00 means 256
911 // sectors to be read
912 //
913 SectorCount8 = 0x00;
914 //
915 // SectorCount is used to record the number of sectors to be read
916 //
917 SectorCount = 256;
918 } else {
919
920 SectorCount8 = (UINT8) BlocksRemaining;
921 SectorCount = (UINT16) BlocksRemaining;
922 }
923
924 //
925 // ByteCount is the number of bytes that will be read
926 //
927 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
928
929 //
930 // call AtaPioDataIn() to send Read Sector Command and receive data read
931 //
932 Status = AtaPioDataIn (
933 IdeDev,
934 Buffer,
935 (UINT32) ByteCount,
936 AtaCommand,
937 Lba3,
938 SectorCount8,
939 Lba0,
940 Lba1,
941 Lba2
942 );
943 if (EFI_ERROR (Status)) {
944 return Status;
945 }
946
947 Lba32 += SectorCount;
948 Buffer = ((UINT8 *) Buffer + ByteCount);
949 BlocksRemaining -= SectorCount;
950 }
951
952 return Status;
953 }
954
955 /**
956 This function is called by the AtaBlkIoWriteBlocks() to perform writing onto
957 media in block unit.
958
959 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure,used to record
960 all the information of the IDE device.
961 @param BufferData A pointer to the source buffer for the data.
962 @param Lba The starting logical block address to write onto the device media.
963 @param NumberOfBlocks The number of transfer data blocks.
964
965 @return status is fully dependent on the return status of AtaPioDataIn() function.
966
967 **/
968 EFI_STATUS
969 AtaWriteSectors (
970 IN IDE_BLK_IO_DEV *IdeDev,
971 IN VOID *BufferData,
972 IN EFI_LBA Lba,
973 IN UINTN NumberOfBlocks
974 )
975 {
976 EFI_STATUS Status;
977 UINTN BlocksRemaining;
978 UINT32 Lba32;
979 UINT8 Lba0;
980 UINT8 Lba1;
981 UINT8 Lba2;
982 UINT8 Lba3;
983 UINT8 AtaCommand;
984 UINT8 SectorCount8;
985 UINT16 SectorCount;
986 UINTN ByteCount;
987 VOID *Buffer;
988
989 Buffer = BufferData;
990
991 //
992 // Using Write Sector(s) command (opcode=0x30) with PIO DATA OUT protocol
993 //
994 AtaCommand = ATA_CMD_WRITE_SECTORS;
995
996 BlocksRemaining = NumberOfBlocks;
997
998 Lba32 = (UINT32) Lba;
999
1000 Status = EFI_SUCCESS;
1001
1002 while (BlocksRemaining > 0) {
1003
1004 Lba0 = (UINT8) Lba32;
1005 Lba1 = (UINT8) (Lba32 >> 8);
1006 Lba2 = (UINT8) (Lba32 >> 16);
1007 Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f);
1008
1009 if (BlocksRemaining >= 0x100) {
1010
1011 //
1012 // SectorCount8 is sent to Sector Count register, 0x00 means 256 sectors
1013 // to be written
1014 //
1015 SectorCount8 = 0x00;
1016 //
1017 // SectorCount is used to record the number of sectors to be written
1018 //
1019 SectorCount = 256;
1020 } else {
1021
1022 SectorCount8 = (UINT8) BlocksRemaining;
1023 SectorCount = (UINT16) BlocksRemaining;
1024 }
1025
1026 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
1027
1028 Status = AtaPioDataOut (
1029 IdeDev,
1030 Buffer,
1031 (UINT32) ByteCount,
1032 AtaCommand,
1033 Lba3,
1034 SectorCount8,
1035 Lba0,
1036 Lba1,
1037 Lba2
1038 );
1039 if (EFI_ERROR (Status)) {
1040 return Status;
1041 }
1042
1043 Lba32 += SectorCount;
1044 Buffer = ((UINT8 *) Buffer + ByteCount);
1045 BlocksRemaining -= SectorCount;
1046 }
1047
1048 return Status;
1049 }
1050 /**
1051 This function is used to implement the Soft Reset on the specified device. But,
1052 the ATA Soft Reset mechanism is so strong a reset method that it will force
1053 resetting on both devices connected to the same cable.
1054
1055 It is called by IdeBlkIoReset(), a interface function of Block
1056 I/O protocol.
1057
1058 This function can also be used by the ATAPI device to perform reset when
1059 ATAPI Reset command is failed.
1060
1061 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record
1062 all the information of the IDE device.
1063 @retval EFI_SUCCESS Soft reset completes successfully.
1064 @retval EFI_DEVICE_ERROR Any step during the reset process is failed.
1065
1066 @note The registers initial values after ATA soft reset are different
1067 to the ATA device and ATAPI device.
1068 **/
1069 EFI_STATUS
1070 AtaSoftReset (
1071 IN IDE_BLK_IO_DEV *IdeDev
1072 )
1073 {
1074
1075 UINT8 DeviceControl;
1076
1077 DeviceControl = 0;
1078 //
1079 // set SRST bit to initiate soft reset
1080 //
1081 DeviceControl |= ATA_CTLREG_SRST;
1082
1083 //
1084 // disable Interrupt
1085 //
1086 DeviceControl |= BIT1;
1087
1088 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
1089
1090 //
1091 // SRST should assert for at least 5 us, we use 10 us for
1092 // better compatibility
1093 //
1094 gBS->Stall (10);
1095
1096 //
1097 // Enable interrupt to support UDMA, and clear SRST bit
1098 //
1099 DeviceControl = 0;
1100 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
1101
1102 //
1103 // Wait for at least 2 ms to check BSY status, we use 10 ms
1104 // for better compatibility
1105 //
1106 gBS->Stall(10000);
1107 //
1108 // slave device needs at most 31s to clear BSY
1109 //
1110 if (WaitForBSYClear (IdeDev, 31000) == EFI_TIMEOUT) {
1111 return EFI_DEVICE_ERROR;
1112 }
1113
1114 return EFI_SUCCESS;
1115 }
1116 /**
1117 This function is used to send out ATA commands conforms to the PIO Data In
1118 Protocol, supporting ATA/ATAPI-6 standard
1119
1120 Comparing with ATA-3 data in protocol, we have two differents here:
1121 1. Do NOT wait for DRQ clear before sending command into IDE device.(the
1122 wait will frequently fail... cause writing function return error)
1123
1124 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly
1125 slow down writing performance by 100 times!)
1126
1127 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
1128 to record all the information of the IDE device.
1129 @param Buffer buffer contained data transferred from device to host.
1130 @param ByteCount data size in byte unit of the buffer.
1131 @param AtaCommand value of the Command Register
1132 @param StartLba the start LBA of this transaction
1133 @param SectorCount the count of sectors to be transfered
1134
1135 @retval EFI_SUCCESS send out the ATA command and device send required data successfully.
1136 @retval EFI_DEVICE_ERROR command sent failed.
1137
1138 **/
1139 EFI_STATUS
1140 AtaPioDataInExt (
1141 IN IDE_BLK_IO_DEV *IdeDev,
1142 IN OUT VOID *Buffer,
1143 IN UINT32 ByteCount,
1144 IN UINT8 AtaCommand,
1145 IN EFI_LBA StartLba,
1146 IN UINT16 SectorCount
1147 )
1148 {
1149 UINT8 DevSel;
1150 UINT8 SectorCount8;
1151 UINT8 LbaLow;
1152 UINT8 LbaMid;
1153 UINT8 LbaHigh;
1154 UINTN WordCount;
1155 UINTN Increment;
1156 UINT16 *Buffer16;
1157 EFI_STATUS Status;
1158
1159 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1160 if (EFI_ERROR (Status)) {
1161 return EFI_DEVICE_ERROR;
1162 }
1163
1164 //
1165 // Select device, set bit6 as 1 to indicate LBA mode is used
1166 //
1167 DevSel = (UINT8) (IdeDev->Device << 4);
1168 DevSel |= 0x40;
1169 IDEWritePortB (
1170 IdeDev->PciIo,
1171 IdeDev->IoPort->Head,
1172 DevSel
1173 );
1174
1175 //
1176 // Wait for DRDY singnal asserting. ATAPI device needn't wait
1177 //
1178 if ( (IdeDev->Type == IdeHardDisk) ||
1179 (IdeDev->Type == Ide48bitAddressingHardDisk)) {
1180
1181 Status = DRDYReady (IdeDev, ATATIMEOUT);
1182 if (EFI_ERROR (Status)) {
1183 return EFI_DEVICE_ERROR;
1184 }
1185 }
1186
1187 //
1188 // Fill feature register if needed
1189 //
1190 if (AtaCommand == ATA_CMD_SET_FEATURES) {
1191 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03);
1192 }
1193
1194 //
1195 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1196 //
1197 SectorCount8 = (UINT8) (SectorCount >> 8);
1198 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1199
1200 SectorCount8 = (UINT8) SectorCount;
1201 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1202
1203 //
1204 // Fill the start LBA registers, which are also two-byte FIFO
1205 //
1206 LbaLow = (UINT8) RShiftU64 (StartLba, 24);
1207 LbaMid = (UINT8) RShiftU64 (StartLba, 32);
1208 LbaHigh = (UINT8) RShiftU64 (StartLba, 40);
1209 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1210 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1211 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1212
1213 LbaLow = (UINT8) StartLba;
1214 LbaMid = (UINT8) RShiftU64 (StartLba, 8);
1215 LbaHigh = (UINT8) RShiftU64 (StartLba, 16);
1216 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1217 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1218 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1219
1220 //
1221 // Send command via Command Register, invoking the processing of this command
1222 //
1223 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
1224
1225 Buffer16 = (UINT16 *) Buffer;
1226
1227 //
1228 // According to PIO data in protocol, host can perform a series of reads to
1229 // the data register after each time device set DRQ ready;
1230 //
1231
1232 //
1233 // 256 words
1234 //
1235 Increment = 256;
1236
1237 //
1238 // used to record bytes of currently transfered data
1239 //
1240 WordCount = 0;
1241
1242 while (WordCount < ByteCount / 2) {
1243 //
1244 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
1245 //
1246 Status = DRQReady2 (IdeDev, ATATIMEOUT);
1247 if (EFI_ERROR (Status)) {
1248 return EFI_DEVICE_ERROR;
1249 }
1250
1251 Status = CheckErrorStatus (IdeDev);
1252 if (EFI_ERROR (Status)) {
1253 return EFI_DEVICE_ERROR;
1254 }
1255
1256 //
1257 // Get the byte count for one series of read
1258 //
1259 if ((WordCount + Increment) > ByteCount / 2) {
1260 Increment = ByteCount / 2 - WordCount;
1261 }
1262
1263 IDEReadPortWMultiple (
1264 IdeDev->PciIo,
1265 IdeDev->IoPort->Data,
1266 Increment,
1267 Buffer16
1268 );
1269
1270 WordCount += Increment;
1271 Buffer16 += Increment;
1272
1273 }
1274
1275 return CheckErrorStatus (IdeDev);
1276 }
1277 /**
1278 Send ATA Ext command into device with NON_DATA protocol.
1279
1280 @param IdeDev Standard IDE device private data structure
1281 @param AtaCommand The ATA command to be sent
1282 @param Device The value in Device register
1283 @param Feature The value in Feature register
1284 @param SectorCount The value in SectorCount register
1285 @param LbaAddress The LBA address in 48-bit mode
1286
1287 @retval EFI_SUCCESS Reading succeed
1288 @retval EFI_DEVICE_ERROR Error executing commands on this device.
1289
1290 **/
1291 EFI_STATUS
1292 AtaCommandIssueExt (
1293 IN IDE_BLK_IO_DEV *IdeDev,
1294 IN UINT8 AtaCommand,
1295 IN UINT8 Device,
1296 IN UINT16 Feature,
1297 IN UINT16 SectorCount,
1298 IN EFI_LBA LbaAddress
1299 )
1300 {
1301 EFI_STATUS Status;
1302 UINT8 SectorCount8;
1303 UINT8 Feature8;
1304 UINT8 LbaLow;
1305 UINT8 LbaMid;
1306 UINT8 LbaHigh;
1307
1308 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1309 if (EFI_ERROR (Status)) {
1310 return EFI_DEVICE_ERROR;
1311 }
1312
1313 //
1314 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
1315 //
1316 IDEWritePortB (
1317 IdeDev->PciIo,
1318 IdeDev->IoPort->Head,
1319 (UINT8) ((IdeDev->Device << 4) | 0xe0)
1320 );
1321
1322 //
1323 // ATA commands for ATA device must be issued when DRDY is set
1324 //
1325 Status = DRDYReady (IdeDev, ATATIMEOUT);
1326 if (EFI_ERROR (Status)) {
1327 return EFI_DEVICE_ERROR;
1328 }
1329
1330 //
1331 // Pass parameter into device register block
1332 //
1333 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
1334
1335 //
1336 // Fill the feature register, which is a two-byte FIFO. Need write twice.
1337 //
1338 Feature8 = (UINT8) (Feature >> 8);
1339 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
1340
1341 Feature8 = (UINT8) Feature;
1342 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
1343
1344 //
1345 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1346 //
1347 SectorCount8 = (UINT8) (SectorCount >> 8);
1348 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1349
1350 SectorCount8 = (UINT8) SectorCount;
1351 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1352
1353 //
1354 // Fill the start LBA registers, which are also two-byte FIFO
1355 //
1356 LbaLow = (UINT8) RShiftU64 (LbaAddress, 24);
1357 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1358 LbaLow = (UINT8) LbaAddress;
1359 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1360
1361 LbaMid = (UINT8) RShiftU64 (LbaAddress, 32);
1362 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1363 LbaMid = (UINT8) RShiftU64 (LbaAddress, 8);
1364 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1365
1366 LbaHigh = (UINT8) RShiftU64 (LbaAddress, 40);
1367 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1368 LbaHigh = (UINT8) RShiftU64 (LbaAddress, 16);
1369 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1370
1371 //
1372 // Work around for Segate 160G disk writing
1373 //
1374 gBS->Stall (1800);
1375
1376 //
1377 // Send command via Command Register
1378 //
1379 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
1380
1381 //
1382 // Stall at least 400ns
1383 //
1384 gBS->Stall (100);
1385
1386 return EFI_SUCCESS;
1387 }
1388 /**
1389 Send ATA Ext command into device with NON_DATA protocol
1390
1391 @param IdeDev Standard IDE device private data structure
1392 @param AtaCommand The ATA command to be sent
1393 @param Device The value in Device register
1394 @param Feature The value in Feature register
1395 @param SectorCount The value in SectorCount register
1396 @param LbaAddress The LBA address in 48-bit mode
1397
1398 @retval EFI_SUCCESS Reading succeed
1399 @retval EFI_DEVICE_ERROR Error executing commands on this device.
1400
1401 **/
1402 EFI_STATUS
1403 AtaCommandIssue (
1404 IN IDE_BLK_IO_DEV *IdeDev,
1405 IN UINT8 AtaCommand,
1406 IN UINT8 Device,
1407 IN UINT16 Feature,
1408 IN UINT16 SectorCount,
1409 IN EFI_LBA LbaAddress
1410 )
1411 {
1412 EFI_STATUS Status;
1413 UINT8 SectorCount8;
1414 UINT8 Feature8;
1415 UINT8 Lba0;
1416 UINT8 Lba1;
1417 UINT8 Lba2;
1418 UINT8 Lba3;
1419
1420 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1421 if (EFI_ERROR (Status)) {
1422 return EFI_DEVICE_ERROR;
1423 }
1424
1425 //
1426 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
1427 //
1428 IDEWritePortB (
1429 IdeDev->PciIo,
1430 IdeDev->IoPort->Head,
1431 (UINT8) ((IdeDev->Device << 4) | 0xe0)
1432 );
1433
1434 //
1435 // ATA commands for ATA device must be issued when DRDY is set
1436 //
1437 Status = DRDYReady (IdeDev, ATATIMEOUT);
1438 if (EFI_ERROR (Status)) {
1439 return EFI_DEVICE_ERROR;
1440 }
1441
1442 Lba0 = (UINT8) LbaAddress;
1443 Lba1 = (UINT8) RShiftU64 (LbaAddress, 8);
1444 Lba2 = (UINT8) RShiftU64 (LbaAddress, 16);
1445 Lba3 = (UINT8) RShiftU64 (LbaAddress, 24);
1446 Device = (UINT8) (Device | Lba3);
1447
1448 //
1449 // Pass parameter into device register block
1450 //
1451 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
1452
1453 //
1454 // Fill the feature register, which is a two-byte FIFO. Need write twice.
1455 //
1456 Feature8 = (UINT8) Feature;
1457 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
1458
1459 //
1460 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1461 //
1462 SectorCount8 = (UINT8) SectorCount;
1463 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1464
1465 //
1466 // Fill the start LBA registers, which are also two-byte FIFO
1467 //
1468
1469 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, Lba0);
1470 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, Lba1);
1471 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, Lba2);
1472
1473 //
1474 // Send command via Command Register
1475 //
1476 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
1477
1478 //
1479 // Stall at least 400ns
1480 //
1481 gBS->Stall (100);
1482
1483 return EFI_SUCCESS;
1484 }
1485 /**
1486 Perform an ATA Udma operation (Read, ReadExt, Write, WriteExt).
1487
1488 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
1489 to record all the information of the IDE device.
1490 @param DataBuffer A pointer to the source buffer for the data.
1491 @param StartLba The starting logical block address to write to
1492 on the device media.
1493 @param NumberOfBlocks The number of transfer data blocks.
1494 @param UdmaOp The perform operations could be AtaUdmaReadOp, AtaUdmaReadExOp,
1495 AtaUdmaWriteOp, AtaUdmaWriteExOp
1496
1497 @retval EFI_SUCCESS the operation is successful.
1498 @retval EFI_OUT_OF_RESOURCES Build PRD table failed
1499 @retval EFI_UNSUPPORTED Unknown channel or operations command
1500 @retval EFI_DEVICE_ERROR Ata command execute failed
1501
1502 **/
1503 EFI_STATUS
1504 DoAtaUdma (
1505 IN IDE_BLK_IO_DEV *IdeDev,
1506 IN VOID *DataBuffer,
1507 IN EFI_LBA StartLba,
1508 IN UINTN NumberOfBlocks,
1509 IN ATA_UDMA_OPERATION UdmaOp
1510 )
1511 {
1512 IDE_DMA_PRD *PrdAddr;
1513 IDE_DMA_PRD *UsedPrdAddr;
1514 IDE_DMA_PRD *TempPrdAddr;
1515 UINT8 RegisterValue;
1516 UINT8 Device;
1517 UINT64 IoPortForBmic;
1518 UINT64 IoPortForBmis;
1519 UINT64 IoPortForBmid;
1520 EFI_STATUS Status;
1521 UINTN PrdTableNum;
1522 UINTN ByteCount;
1523 UINTN ByteAvailable;
1524 UINT8 *PrdBuffer;
1525 UINTN RemainBlockNum;
1526 UINT8 DeviceControl;
1527 UINT32 Count;
1528 UINTN PageCount;
1529 VOID *Map;
1530 VOID *MemPage;
1531 EFI_PHYSICAL_ADDRESS DeviceAddress;
1532 UINTN MaxDmaCommandSectors;
1533 EFI_PCI_IO_PROTOCOL_OPERATION PciIoProtocolOp;
1534 UINT8 AtaCommand;
1535
1536 switch (UdmaOp) {
1537 case AtaUdmaReadOp:
1538 MaxDmaCommandSectors = ATAPI_MAX_DMA_CMD_SECTORS;
1539 PciIoProtocolOp = EfiPciIoOperationBusMasterWrite;
1540 AtaCommand = ATA_CMD_READ_DMA;
1541 break;
1542 case AtaUdmaReadExtOp:
1543 MaxDmaCommandSectors = ATAPI_MAX_DMA_EXT_CMD_SECTORS;
1544 PciIoProtocolOp = EfiPciIoOperationBusMasterWrite;
1545 AtaCommand = ATA_CMD_READ_DMA_EXT;
1546 break;
1547 case AtaUdmaWriteOp:
1548 MaxDmaCommandSectors = ATAPI_MAX_DMA_CMD_SECTORS;
1549 PciIoProtocolOp = EfiPciIoOperationBusMasterRead;
1550 AtaCommand = ATA_CMD_WRITE_DMA;
1551 break;
1552 case AtaUdmaWriteExtOp:
1553 MaxDmaCommandSectors = ATAPI_MAX_DMA_EXT_CMD_SECTORS;
1554 PciIoProtocolOp = EfiPciIoOperationBusMasterRead;
1555 AtaCommand = ATA_CMD_WRITE_DMA_EXT;
1556 break;
1557 default:
1558 return EFI_UNSUPPORTED;
1559 break;
1560 }
1561
1562 //
1563 // Select device
1564 //
1565 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0);
1566 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
1567
1568 //
1569 // Enable interrupt to support UDMA
1570 //
1571 DeviceControl = 0;
1572 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
1573
1574 if (IdePrimary == IdeDev->Channel) {
1575 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICP_OFFSET;
1576 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISP_OFFSET;
1577 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDP_OFFSET;
1578 } else {
1579 if (IdeSecondary == IdeDev->Channel) {
1580 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICS_OFFSET;
1581 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISS_OFFSET;
1582 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDS_OFFSET;
1583 } else {
1584 return EFI_UNSUPPORTED;
1585 }
1586 }
1587
1588 //
1589 // Read BMIS register and clear ERROR and INTR bit
1590 //
1591 IdeDev->PciIo->Io.Read (
1592 IdeDev->PciIo,
1593 EfiPciIoWidthUint8,
1594 EFI_PCI_IO_PASS_THROUGH_BAR,
1595 IoPortForBmis,
1596 1,
1597 &RegisterValue
1598 );
1599
1600 RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR);
1601
1602 IdeDev->PciIo->Io.Write (
1603 IdeDev->PciIo,
1604 EfiPciIoWidthUint8,
1605 EFI_PCI_IO_PASS_THROUGH_BAR,
1606 IoPortForBmis,
1607 1,
1608 &RegisterValue
1609 );
1610
1611 Status = EFI_SUCCESS;
1612
1613 RemainBlockNum = NumberOfBlocks;
1614 while (RemainBlockNum > 0) {
1615
1616 if (RemainBlockNum >= MaxDmaCommandSectors) {
1617 //
1618 // SectorCount is used to record the number of sectors to be read
1619 // Max 65536 sectors can be transfered at a time.
1620 //
1621 NumberOfBlocks = MaxDmaCommandSectors;
1622 RemainBlockNum -= MaxDmaCommandSectors;
1623 } else {
1624 NumberOfBlocks = (UINT16) RemainBlockNum;
1625 RemainBlockNum = 0;
1626 }
1627
1628 //
1629 // Calculate the number of PRD table to make sure the memory region
1630 // not cross 64K boundary
1631 //
1632 ByteCount = NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
1633 PrdTableNum = ((ByteCount >> 16) + 1) + 1;
1634
1635 //
1636 // Build PRD table
1637 //
1638 PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD));
1639 Status = IdeDev->PciIo->AllocateBuffer (
1640 IdeDev->PciIo,
1641 AllocateAnyPages,
1642 EfiBootServicesData,
1643 PageCount,
1644 &MemPage,
1645 0
1646 );
1647 if (EFI_ERROR (Status)) {
1648 return EFI_OUT_OF_RESOURCES;
1649 }
1650 ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount));
1651
1652 PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage);
1653 //
1654 // To make sure PRD is allocated in one 64K page
1655 //
1656 if (((UINTN) PrdAddr & 0x0FFFF) > (((UINTN) PrdAddr + PrdTableNum * sizeof (IDE_DMA_PRD) - 1) & 0x0FFFF)) {
1657 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x10000) & 0xFFFF0000);
1658 } else {
1659 if ((UINTN) PrdAddr & 0x03) {
1660 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x04) & 0xFFFFFFFC);
1661 } else {
1662 UsedPrdAddr = PrdAddr;
1663 }
1664 }
1665
1666 //
1667 // Build the PRD table
1668 //
1669 Status = IdeDev->PciIo->Map (
1670 IdeDev->PciIo,
1671 PciIoProtocolOp,
1672 DataBuffer,
1673 &ByteCount,
1674 &DeviceAddress,
1675 &Map
1676 );
1677 if (EFI_ERROR (Status)) {
1678 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage);
1679 return EFI_OUT_OF_RESOURCES;
1680 }
1681 PrdBuffer = (VOID *) ((UINTN) DeviceAddress);
1682 TempPrdAddr = UsedPrdAddr;
1683 while (TRUE) {
1684
1685 ByteAvailable = 0x10000 - ((UINTN) PrdBuffer & 0xFFFF);
1686
1687 if (ByteCount <= ByteAvailable) {
1688 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
1689 TempPrdAddr->ByteCount = (UINT16) ByteCount;
1690 TempPrdAddr->EndOfTable = 0x8000;
1691 break;
1692 }
1693
1694 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
1695 TempPrdAddr->ByteCount = (UINT16) ByteAvailable;
1696
1697 ByteCount -= ByteAvailable;
1698 PrdBuffer += ByteAvailable;
1699 TempPrdAddr++;
1700 }
1701
1702 //
1703 // Set the base address to BMID register
1704 //
1705 IdeDev->PciIo->Io.Write (
1706 IdeDev->PciIo,
1707 EfiPciIoWidthUint32,
1708 EFI_PCI_IO_PASS_THROUGH_BAR,
1709 IoPortForBmid,
1710 1,
1711 &UsedPrdAddr
1712 );
1713
1714 //
1715 // Set BMIC register to identify the operation direction
1716 //
1717 IdeDev->PciIo->Io.Read (
1718 IdeDev->PciIo,
1719 EfiPciIoWidthUint8,
1720 EFI_PCI_IO_PASS_THROUGH_BAR,
1721 IoPortForBmic,
1722 1,
1723 &RegisterValue
1724 );
1725
1726 if (UdmaOp == AtaUdmaReadExtOp || UdmaOp == AtaUdmaReadOp) {
1727 RegisterValue |= BMIC_NREAD;
1728 } else {
1729 RegisterValue &= ~((UINT8) BMIC_NREAD);
1730 }
1731
1732 IdeDev->PciIo->Io.Write (
1733 IdeDev->PciIo,
1734 EfiPciIoWidthUint8,
1735 EFI_PCI_IO_PASS_THROUGH_BAR,
1736 IoPortForBmic,
1737 1,
1738 &RegisterValue
1739 );
1740
1741 if (UdmaOp == AtaUdmaWriteExtOp || UdmaOp == AtaUdmaReadExtOp) {
1742 Status = AtaCommandIssueExt (
1743 IdeDev,
1744 AtaCommand,
1745 Device,
1746 0,
1747 (UINT16) NumberOfBlocks,
1748 StartLba
1749 );
1750 } else {
1751 Status = AtaCommandIssue (
1752 IdeDev,
1753 AtaCommand,
1754 Device,
1755 0,
1756 (UINT16) NumberOfBlocks,
1757 StartLba
1758 );
1759 }
1760
1761 if (EFI_ERROR (Status)) {
1762 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage);
1763 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
1764 return EFI_DEVICE_ERROR;
1765 }
1766
1767 //
1768 // Set START bit of BMIC register
1769 //
1770 IdeDev->PciIo->Io.Read (
1771 IdeDev->PciIo,
1772 EfiPciIoWidthUint8,
1773 EFI_PCI_IO_PASS_THROUGH_BAR,
1774 IoPortForBmic,
1775 1,
1776 &RegisterValue
1777 );
1778
1779 RegisterValue |= BMIC_START;
1780
1781 IdeDev->PciIo->Io.Write (
1782 IdeDev->PciIo,
1783 EfiPciIoWidthUint8,
1784 EFI_PCI_IO_PASS_THROUGH_BAR,
1785 IoPortForBmic,
1786 1,
1787 &RegisterValue
1788 );
1789
1790 //
1791 // Check the INTERRUPT and ERROR bit of BMIS
1792 // Max transfer number of sectors for one command is 65536(32Mbyte),
1793 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
1794 // So set the variable Count to 2000, for about 2 second timeout time.
1795 //
1796 Status = EFI_SUCCESS;
1797 Count = 2000;
1798 while (TRUE) {
1799
1800 IdeDev->PciIo->Io.Read (
1801 IdeDev->PciIo,
1802 EfiPciIoWidthUint8,
1803 EFI_PCI_IO_PASS_THROUGH_BAR,
1804 IoPortForBmis,
1805 1,
1806 &RegisterValue
1807 );
1808 if (((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) != 0) || (Count == 0)) {
1809 if (((RegisterValue & BMIS_ERROR) != 0) || (Count == 0)) {
1810 Status = EFI_DEVICE_ERROR;
1811 break;
1812 }
1813 break;
1814 }
1815
1816 gBS->Stall (1000);
1817 Count --;
1818 }
1819
1820 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage);
1821 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
1822 //
1823 // Read BMIS register and clear ERROR and INTR bit
1824 //
1825 IdeDev->PciIo->Io.Read (
1826 IdeDev->PciIo,
1827 EfiPciIoWidthUint8,
1828 EFI_PCI_IO_PASS_THROUGH_BAR,
1829 IoPortForBmis,
1830 1,
1831 &RegisterValue
1832 );
1833
1834 RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR);
1835
1836 IdeDev->PciIo->Io.Write (
1837 IdeDev->PciIo,
1838 EfiPciIoWidthUint8,
1839 EFI_PCI_IO_PASS_THROUGH_BAR,
1840 IoPortForBmis,
1841 1,
1842 &RegisterValue
1843 );
1844 //
1845 // Read Status Register of IDE device to clear interrupt
1846 //
1847 RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status);
1848 //
1849 // Clear START bit of BMIC register
1850 //
1851 IdeDev->PciIo->Io.Read (
1852 IdeDev->PciIo,
1853 EfiPciIoWidthUint8,
1854 EFI_PCI_IO_PASS_THROUGH_BAR,
1855 IoPortForBmic,
1856 1,
1857 &RegisterValue
1858 );
1859
1860 RegisterValue &= ~((UINT8) BMIC_START);
1861
1862 IdeDev->PciIo->Io.Write (
1863 IdeDev->PciIo,
1864 EfiPciIoWidthUint8,
1865 EFI_PCI_IO_PASS_THROUGH_BAR,
1866 IoPortForBmic,
1867 1,
1868 &RegisterValue
1869 );
1870
1871 if ((RegisterValue & BMIS_ERROR) != 0) {
1872 return EFI_DEVICE_ERROR;
1873 }
1874
1875 if (EFI_ERROR (Status)) {
1876 break;
1877 }
1878 DataBuffer = (UINT8 *) DataBuffer + NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
1879 StartLba += NumberOfBlocks;
1880 }
1881
1882 //
1883 // Disable interrupt of Select device
1884 //
1885 IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl);
1886 DeviceControl |= ATA_CTLREG_IEN_L;
1887 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
1888
1889 return Status;
1890 }
1891
1892
1893 /**
1894 This function is called by the AtaBlkIoReadBlocks() to perform reading from
1895 media in block unit. The function has been enhanced to support >120GB access
1896 and transfer at most 65536 blocks per command
1897
1898 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record
1899 all the information of the IDE device.
1900 @param DataBuffer A pointer to the destination buffer for the data.
1901 @param StartLba The starting logical block address to read from on the device media.
1902 @param NumberOfBlocks The number of transfer data blocks.
1903
1904 @return status depends on the function DoAtaUdma() returns.
1905 **/
1906 EFI_STATUS
1907 AtaUdmaReadExt (
1908 IN IDE_BLK_IO_DEV *IdeDev,
1909 IN VOID *DataBuffer,
1910 IN EFI_LBA StartLba,
1911 IN UINTN NumberOfBlocks
1912 )
1913 {
1914 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaReadExtOp);
1915 }
1916 /**
1917 This function is called by the AtaBlkIoReadBlocks() to perform
1918 reading from media in block unit. The function has been enhanced to
1919 support >120GB access and transfer at most 65536 blocks per command
1920
1921 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record
1922 all the information of the IDE device.
1923 @param DataBuffer A pointer to the destination buffer for the data.
1924 @param StartLba The starting logical block address to read from
1925 on the device media.
1926 @param NumberOfBlocks The number of transfer data blocks.
1927
1928 @return status depends on the function DoAtaUdma() returns.
1929 **/
1930 EFI_STATUS
1931 AtaUdmaRead (
1932 IN IDE_BLK_IO_DEV *IdeDev,
1933 IN VOID *DataBuffer,
1934 IN EFI_LBA StartLba,
1935 IN UINTN NumberOfBlocks
1936 )
1937 {
1938 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaReadOp);
1939 }
1940
1941 /**
1942 This function is called by the AtaBlkIoReadBlocks() to perform
1943 reading from media in block unit. The function has been enhanced to
1944 support >120GB access and transfer at most 65536 blocks per command
1945
1946 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record
1947 all the information of the IDE device.
1948 @param DataBuffer A pointer to the destination buffer for the data.
1949 @param StartLba The starting logical block address to read from on the device media.
1950 @param NumberOfBlocks The number of transfer data blocks.
1951
1952 @return status is fully dependent on the return status of AtaPioDataInExt() function.
1953 **/
1954 EFI_STATUS
1955 AtaReadSectorsExt (
1956 IN IDE_BLK_IO_DEV *IdeDev,
1957 IN VOID *DataBuffer,
1958 IN EFI_LBA StartLba,
1959 IN UINTN NumberOfBlocks
1960 )
1961 {
1962 EFI_STATUS Status;
1963 UINTN BlocksRemaining;
1964 EFI_LBA Lba64;
1965 UINT8 AtaCommand;
1966 UINT16 SectorCount;
1967 UINT32 ByteCount;
1968 VOID *Buffer;
1969
1970 //
1971 // Using ATA "Read Sectors Ext" command(opcode=0x24) with PIO DATA IN protocol
1972 //
1973 AtaCommand = ATA_CMD_READ_SECTORS_EXT;
1974 Buffer = DataBuffer;
1975 BlocksRemaining = NumberOfBlocks;
1976 Lba64 = StartLba;
1977 Status = EFI_SUCCESS;
1978
1979 while (BlocksRemaining > 0) {
1980
1981 if (BlocksRemaining >= 0x10000) {
1982 //
1983 // SectorCount is used to record the number of sectors to be read
1984 // Max 65536 sectors can be transfered at a time.
1985 //
1986 SectorCount = 0xffff;
1987 } else {
1988 SectorCount = (UINT16) BlocksRemaining;
1989 }
1990
1991 //
1992 // ByteCount is the number of bytes that will be read
1993 //
1994 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
1995
1996 //
1997 // call AtaPioDataInExt() to send Read Sector Command and receive data read
1998 //
1999 Status = AtaPioDataInExt (
2000 IdeDev,
2001 Buffer,
2002 ByteCount,
2003 AtaCommand,
2004 Lba64,
2005 SectorCount
2006 );
2007 if (EFI_ERROR (Status)) {
2008 return Status;
2009 }
2010
2011 Lba64 += SectorCount;
2012 Buffer = ((UINT8 *) Buffer + ByteCount);
2013 BlocksRemaining -= SectorCount;
2014 }
2015
2016 return Status;
2017 }
2018 /**
2019 This function is the ATA implementation for ReadBlocks in the
2020 Block I/O Protocol interface.
2021
2022 @param IdeBlkIoDevice Indicates the calling context.
2023 @param MediaId The media id that the read request is for.
2024 @param Lba The starting logical block address to read from on the device.
2025 @param BufferSize The size of the Buffer in bytes. This must be a multiple
2026 of the intrinsic block size of the device.
2027
2028 @param Buffer A pointer to the destination buffer for the data. The caller
2029 is responsible for either having implicit or explicit ownership
2030 of the memory that data is read into.
2031
2032 @retval EFI_SUCCESS Read Blocks successfully.
2033 @retval EFI_DEVICE_ERROR Read Blocks failed.
2034 @retval EFI_NO_MEDIA There is no media in the device.
2035 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media.
2036 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
2037 intrinsic block size of the device.
2038 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
2039 or the data buffer is not valid.
2040
2041 @note If Read Block error because of device error, this function will call
2042 AtaSoftReset() function to reset device.
2043
2044 **/
2045 EFI_STATUS
2046 AtaBlkIoReadBlocks (
2047 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
2048 IN UINT32 MediaId,
2049 IN EFI_LBA Lba,
2050 IN UINTN BufferSize,
2051 OUT VOID *Buffer
2052 )
2053 {
2054 EFI_BLOCK_IO_MEDIA *Media;
2055 UINTN BlockSize;
2056 UINTN NumberOfBlocks;
2057 EFI_STATUS Status;
2058
2059 if (Buffer == NULL) {
2060 return EFI_INVALID_PARAMETER;
2061 }
2062
2063 if (BufferSize == 0) {
2064 return EFI_SUCCESS;
2065 }
2066
2067 Status = EFI_SUCCESS;
2068
2069 //
2070 // Get the intrinsic block size
2071 //
2072 Media = IdeBlkIoDevice->BlkIo.Media;
2073 BlockSize = Media->BlockSize;
2074
2075 NumberOfBlocks = BufferSize / BlockSize;
2076
2077 if (MediaId != Media->MediaId) {
2078 return EFI_MEDIA_CHANGED;
2079 }
2080
2081 if (BufferSize % BlockSize != 0) {
2082 return EFI_BAD_BUFFER_SIZE;
2083 }
2084
2085 if (!(Media->MediaPresent)) {
2086 return EFI_NO_MEDIA;
2087 }
2088
2089 if (Lba > Media->LastBlock) {
2090 return EFI_INVALID_PARAMETER;
2091 }
2092
2093 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
2094 return EFI_INVALID_PARAMETER;
2095 }
2096
2097 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
2098 return EFI_INVALID_PARAMETER;
2099 }
2100
2101 Status = EFI_SUCCESS;
2102 if (IdeBlkIoDevice->Type == Ide48bitAddressingHardDisk) {
2103 //
2104 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 read block mechanism
2105 //
2106 if (IdeBlkIoDevice->UdmaMode.Valid) {
2107 Status = AtaUdmaReadExt (IdeBlkIoDevice, Buffer, Lba, NumberOfBlocks);
2108 } else {
2109 Status = AtaReadSectorsExt (IdeBlkIoDevice, Buffer, Lba, NumberOfBlocks);
2110 }
2111 } else {
2112 //
2113 // For ATA-3 compatible device, use ATA-3 read block mechanism
2114 //
2115 if (IdeBlkIoDevice->UdmaMode.Valid) {
2116 Status = AtaUdmaRead (IdeBlkIoDevice, Buffer, Lba, NumberOfBlocks);
2117 } else {
2118 Status = AtaReadSectors (IdeBlkIoDevice, Buffer, Lba, NumberOfBlocks);
2119 }
2120 }
2121
2122 if (EFI_ERROR (Status)) {
2123 AtaSoftReset (IdeBlkIoDevice);
2124 return EFI_DEVICE_ERROR;
2125 }
2126
2127 return EFI_SUCCESS;
2128
2129 }
2130 /**
2131 This function is used to send out ATA commands conforms to the
2132 PIO Data Out Protocol, supporting ATA/ATAPI-6 standard
2133
2134 Comparing with ATA-3 data out protocol, we have two differents here:<BR>
2135 1. Do NOT wait for DRQ clear before sending command into IDE device.(the
2136 wait will frequently fail... cause writing function return error)
2137
2138 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly
2139 slow down writing performance by 100 times!)
2140
2141 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2142 to record all the information of the IDE device.
2143 @param Buffer buffer contained data transferred from host to device.
2144 @param ByteCount data size in byte unit of the buffer.
2145 @param AtaCommand value of the Command Register
2146 @param StartLba the start LBA of this transaction
2147 @param SectorCount the count of sectors to be transfered
2148
2149 @retval EFI_SUCCESS send out the ATA command and device receive required
2150 data successfully.
2151 @retval EFI_DEVICE_ERROR command sent failed.
2152
2153 **/
2154 EFI_STATUS
2155 AtaPioDataOutExt (
2156 IN IDE_BLK_IO_DEV *IdeDev,
2157 IN VOID *Buffer,
2158 IN UINT32 ByteCount,
2159 IN UINT8 AtaCommand,
2160 IN EFI_LBA StartLba,
2161 IN UINT16 SectorCount
2162 )
2163 {
2164 UINT8 DevSel;
2165 UINT8 SectorCount8;
2166 UINT8 LbaLow;
2167 UINT8 LbaMid;
2168 UINT8 LbaHigh;
2169 UINTN WordCount;
2170 UINTN Increment;
2171 UINT16 *Buffer16;
2172 EFI_STATUS Status;
2173
2174 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
2175 if (EFI_ERROR (Status)) {
2176 return EFI_DEVICE_ERROR;
2177 }
2178
2179 //
2180 // Select device. Set bit6 as 1 to indicate LBA mode is used
2181 //
2182 DevSel = (UINT8) (IdeDev->Device << 4);
2183 DevSel |= 0x40;
2184 IDEWritePortB (
2185 IdeDev->PciIo,
2186 IdeDev->IoPort->Head,
2187 DevSel
2188 );
2189
2190 //
2191 // Wait for DRDY singnal asserting.
2192 //
2193 Status = DRDYReady (IdeDev, ATATIMEOUT);
2194 if (EFI_ERROR (Status)) {
2195 return EFI_DEVICE_ERROR;
2196 }
2197
2198 //
2199 // Fill feature register if needed
2200 //
2201 if (AtaCommand == ATA_CMD_SET_FEATURES) {
2202 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03);
2203 }
2204
2205 //
2206 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
2207 //
2208 SectorCount8 = (UINT8) (SectorCount >> 8);
2209 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
2210
2211 SectorCount8 = (UINT8) SectorCount;
2212 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
2213
2214 //
2215 // Fill the start LBA registers, which are also two-byte FIFO
2216 //
2217 LbaLow = (UINT8) RShiftU64 (StartLba, 24);
2218 LbaMid = (UINT8) RShiftU64 (StartLba, 32);
2219 LbaHigh = (UINT8) RShiftU64 (StartLba, 40);
2220 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
2221 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
2222 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
2223
2224 LbaLow = (UINT8) StartLba;
2225 LbaMid = (UINT8) RShiftU64 (StartLba, 8);
2226 LbaHigh = (UINT8) RShiftU64 (StartLba, 16);
2227 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
2228 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
2229 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
2230
2231 //
2232 // Send command via Command Register, invoking the processing of this command
2233 //
2234 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
2235
2236 Buffer16 = (UINT16 *) Buffer;
2237
2238 //
2239 // According to PIO Data Out protocol, host can perform a series of writes to
2240 // the data register after each time device set DRQ ready;
2241 //
2242 Increment = 256;
2243
2244 //
2245 // used to record bytes of currently transfered data
2246 //
2247 WordCount = 0;
2248
2249 while (WordCount < ByteCount / 2) {
2250 //
2251 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
2252 //
2253 Status = DRQReady2 (IdeDev, ATATIMEOUT);
2254 if (EFI_ERROR (Status)) {
2255 return EFI_DEVICE_ERROR;
2256 }
2257
2258 Status = CheckErrorStatus (IdeDev);
2259 if (EFI_ERROR (Status)) {
2260 return EFI_DEVICE_ERROR;
2261 }
2262
2263 //
2264 // Write data into device by one series of writing to data register
2265 //
2266 if ((WordCount + Increment) > ByteCount / 2) {
2267 Increment = ByteCount / 2 - WordCount;
2268 }
2269
2270 IDEWritePortWMultiple (
2271 IdeDev->PciIo,
2272 IdeDev->IoPort->Data,
2273 Increment,
2274 Buffer16
2275 );
2276
2277 WordCount += Increment;
2278 Buffer16 += Increment;
2279
2280 }
2281 return CheckErrorStatus (IdeDev);
2282 }
2283 /**
2284 This function is called by the AtaBlkIoWriteBlocks() to perform
2285 writing to media in block unit. The function has been enhanced to
2286 support >120GB access and transfer at most 65536 blocks per command
2287
2288 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2289 to record all the information of the IDE device.
2290 @param DataBuffer A pointer to the source buffer for the data.
2291 @param StartLba The starting logical block address to write to
2292 on the device media.
2293 @param NumberOfBlocks The number of transfer data blocks.
2294
2295 @return status depends on the function DoAtaUdma() returns.
2296 **/
2297 EFI_STATUS
2298 AtaUdmaWriteExt (
2299 IN IDE_BLK_IO_DEV *IdeDev,
2300 IN VOID *DataBuffer,
2301 IN EFI_LBA StartLba,
2302 IN UINTN NumberOfBlocks
2303 )
2304 {
2305 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaWriteExtOp);
2306 }
2307
2308 /**
2309 This function is called by the AtaBlkIoWriteBlocks() to perform
2310 writing to media in block unit.
2311
2312 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2313 to record all the information of the IDE device.
2314 @param DataBuffer A pointer to the source buffer for the data.
2315 @param StartLba The starting logical block address to write to
2316 on the device media.
2317 @param NumberOfBlocks The number of transfer data blocks.
2318
2319 @return status depends on the function DoAtaUdma() returns.
2320 **/
2321 EFI_STATUS
2322 AtaUdmaWrite (
2323 IN IDE_BLK_IO_DEV *IdeDev,
2324 IN VOID *DataBuffer,
2325 IN EFI_LBA StartLba,
2326 IN UINTN NumberOfBlocks
2327 )
2328 {
2329 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaWriteOp);
2330 }
2331 /**
2332 This function is called by the AtaBlkIoWriteBlocks() to perform
2333 writing onto media in block unit. The function has been enhanced to
2334 support >120GB access and transfer at most 65536 blocks per command
2335
2336 @param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure,used
2337 to record all the information of the IDE device.
2338 @param DataBuffer A pointer to the source buffer for the data.
2339 @param StartLba The starting logical block address to write onto the device
2340 media.
2341 @param NumberOfBlocks The number of transfer data blocks.
2342
2343 @return status is fully dependent on the return status of AtaPioDataOutExt() function.
2344 **/
2345 EFI_STATUS
2346 AtaWriteSectorsExt (
2347 IN IDE_BLK_IO_DEV *IdeDev,
2348 IN VOID *DataBuffer,
2349 IN EFI_LBA StartLba,
2350 IN UINTN NumberOfBlocks
2351 )
2352 {
2353 EFI_STATUS Status;
2354 EFI_LBA Lba64;
2355 UINTN BlocksRemaining;
2356 UINT8 AtaCommand;
2357 UINT16 SectorCount;
2358 UINT32 ByteCount;
2359 VOID *Buffer;
2360
2361 //
2362 // Using ATA "Write Sectors Ext" cmd(opcode=0x24) with PIO DATA OUT protocol
2363 //
2364 AtaCommand = ATA_CMD_WRITE_SECTORS_EXT;
2365 Lba64 = StartLba;
2366 Buffer = DataBuffer;
2367 BlocksRemaining = NumberOfBlocks;
2368
2369 Status = EFI_SUCCESS;
2370
2371 while (BlocksRemaining > 0) {
2372
2373 if (BlocksRemaining >= 0x10000) {
2374 //
2375 // SectorCount is used to record the number of sectors to be written.
2376 // Max 65536 sectors can be transfered at a time.
2377 //
2378 SectorCount = 0xffff;
2379 } else {
2380 SectorCount = (UINT16) BlocksRemaining;
2381 }
2382
2383 //
2384 // ByteCount is the number of bytes that will be written
2385 //
2386 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
2387
2388 //
2389 // Call AtaPioDataOutExt() to send "Write Sectors Ext" Command
2390 //
2391 Status = AtaPioDataOutExt (
2392 IdeDev,
2393 Buffer,
2394 ByteCount,
2395 AtaCommand,
2396 Lba64,
2397 SectorCount
2398 );
2399 if (EFI_ERROR (Status)) {
2400 return Status;
2401 }
2402
2403 Lba64 += SectorCount;
2404 Buffer = ((UINT8 *) Buffer + ByteCount);
2405 BlocksRemaining -= SectorCount;
2406 }
2407
2408 return Status;
2409 }
2410 /**
2411 This function is the ATA implementation for WriteBlocks in the
2412 Block I/O Protocol interface.
2413
2414 @param IdeBlkIoDevice Indicates the calling context.
2415 @param MediaId The media id that the write request is for.
2416 @param Lba The starting logical block address to write onto the device.
2417 @param BufferSize The size of the Buffer in bytes. This must be a multiple
2418 of the intrinsic block size of the device.
2419 @param Buffer A pointer to the source buffer for the data.The caller
2420 is responsible for either having implicit or explicit
2421 ownership of the memory that data is written from.
2422
2423 @retval EFI_SUCCESS Write Blocks successfully.
2424 @retval EFI_DEVICE_ERROR Write Blocks failed.
2425 @retval EFI_NO_MEDIA There is no media in the device.
2426 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media.
2427
2428 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
2429 intrinsic block size of the device.
2430 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
2431 or the data buffer is not valid.
2432
2433 @note If Write Block error because of device error, this function will call
2434 AtaSoftReset() function to reset device.
2435 **/
2436 EFI_STATUS
2437 AtaBlkIoWriteBlocks (
2438 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
2439 IN UINT32 MediaId,
2440 IN EFI_LBA Lba,
2441 IN UINTN BufferSize,
2442 OUT VOID *Buffer
2443 )
2444 {
2445
2446 EFI_BLOCK_IO_MEDIA *Media;
2447 UINTN BlockSize;
2448 UINTN NumberOfBlocks;
2449 EFI_STATUS Status;
2450
2451 if (Buffer == NULL) {
2452 return EFI_INVALID_PARAMETER;
2453 }
2454
2455 if (BufferSize == 0) {
2456 return EFI_SUCCESS;
2457 }
2458
2459 Status = EFI_SUCCESS;
2460
2461 //
2462 // Get the intrinsic block size
2463 //
2464 Media = IdeBlkIoDevice->BlkIo.Media;
2465 BlockSize = Media->BlockSize;
2466 NumberOfBlocks = BufferSize / BlockSize;
2467
2468 if (MediaId != Media->MediaId) {
2469 return EFI_MEDIA_CHANGED;
2470 }
2471
2472 if (BufferSize % BlockSize != 0) {
2473 return EFI_BAD_BUFFER_SIZE;
2474 }
2475
2476 if (Lba > Media->LastBlock) {
2477 return EFI_INVALID_PARAMETER;
2478 }
2479
2480 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
2481 return EFI_INVALID_PARAMETER;
2482 }
2483
2484 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
2485 return EFI_INVALID_PARAMETER;
2486 }
2487
2488 Status = EFI_SUCCESS;
2489 if (IdeBlkIoDevice->Type == Ide48bitAddressingHardDisk) {
2490 //
2491 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 write block mechanism
2492 //
2493 if (IdeBlkIoDevice->UdmaMode.Valid) {
2494 Status = AtaUdmaWriteExt (IdeBlkIoDevice, Buffer, Lba, NumberOfBlocks);
2495 } else {
2496 Status = AtaWriteSectorsExt (IdeBlkIoDevice, Buffer, Lba, NumberOfBlocks);
2497 }
2498 } else {
2499 //
2500 // For ATA-3 compatible device, use ATA-3 write block mechanism
2501 //
2502 if (IdeBlkIoDevice->UdmaMode.Valid) {
2503 Status = AtaUdmaWrite (IdeBlkIoDevice, Buffer, Lba, NumberOfBlocks);
2504 } else {
2505 Status = AtaWriteSectors (IdeBlkIoDevice, Buffer, Lba, NumberOfBlocks);
2506 }
2507 }
2508
2509 if (EFI_ERROR (Status)) {
2510 AtaSoftReset (IdeBlkIoDevice);
2511 return EFI_DEVICE_ERROR;
2512 }
2513
2514 return EFI_SUCCESS;
2515 }
2516 /**
2517 Enable Long Physical Sector Feature for ATA device.
2518
2519 @param IdeDev The IDE device data
2520
2521 @retval EFI_SUCCESS The ATA device supports Long Physical Sector feature
2522 and corresponding fields in BlockIo structure is updated.
2523 @retval EFI_UNSUPPORTED The device is not ATA device or Long Physical Sector
2524 feature is not supported.
2525 **/
2526 EFI_STATUS
2527 AtaEnableLongPhysicalSector (
2528 IN IDE_BLK_IO_DEV *IdeDev
2529 )
2530 {
2531 EFI_ATA_IDENTIFY_DATA *AtaIdentifyData;
2532 UINT16 PhyLogicSectorSupport;
2533
2534 ASSERT (IdeDev->IdData != NULL);
2535 //
2536 // Only valid for ATA device
2537 //
2538 AtaIdentifyData = (EFI_ATA_IDENTIFY_DATA *) &IdeDev->IdData->AtaData;
2539 if ((AtaIdentifyData->config & 0x8000) != 0) {
2540 return EFI_UNSUPPORTED;
2541 }
2542 PhyLogicSectorSupport = AtaIdentifyData->phy_logic_sector_support;
2543 //
2544 // Check whether Long Physical Sector Feature is supported
2545 //
2546 if ((PhyLogicSectorSupport & 0xc000) == 0x4000) {
2547 IdeDev->BlkIo.Media->LogicalBlocksPerPhysicalBlock = 1;
2548 IdeDev->BlkIo.Media->LowestAlignedLba = 0;
2549 //
2550 // Check whether one physical block contains multiple physical blocks
2551 //
2552 if ((PhyLogicSectorSupport & 0x2000) != 0) {
2553 IdeDev->BlkIo.Media->LogicalBlocksPerPhysicalBlock =
2554 (UINT32) (1 << (PhyLogicSectorSupport & 0x000f));
2555 //
2556 // Check lowest alignment of logical blocks within physical block
2557 //
2558 if ((AtaIdentifyData->alignment_logic_in_phy_blocks & 0xc000) == 0x4000) {
2559 IdeDev->BlkIo.Media->LowestAlignedLba =
2560 (EFI_LBA) ((IdeDev->BlkIo.Media->LogicalBlocksPerPhysicalBlock - ((UINT32)AtaIdentifyData->alignment_logic_in_phy_blocks & 0x3fff)) %
2561 IdeDev->BlkIo.Media->LogicalBlocksPerPhysicalBlock);
2562 }
2563 }
2564 //
2565 // Check logical block size
2566 //
2567 IdeDev->BlkIo.Media->BlockSize = 0x200;
2568 if ((PhyLogicSectorSupport & 0x1000) != 0) {
2569 IdeDev->BlkIo.Media->BlockSize = (UINT32) (
2570 ((AtaIdentifyData->logic_sector_size_hi << 16) |
2571 AtaIdentifyData->logic_sector_size_lo) * sizeof (UINT16)
2572 );
2573 }
2574 return EFI_SUCCESS;
2575 } else {
2576 return EFI_UNSUPPORTED;
2577 }
2578 }
2579 /**
2580 Send ATA command into device with NON_DATA protocol
2581
2582 @param IdeDev Standard IDE device private data structure
2583 @param AtaCommand The ATA command to be sent
2584 @param Device The value in Device register
2585 @param Feature The value in Feature register
2586 @param SectorCount The value in SectorCount register
2587 @param LbaLow The value in LBA_LOW register
2588 @param LbaMiddle The value in LBA_MIDDLE register
2589 @param LbaHigh The value in LBA_HIGH register
2590
2591 @retval EFI_SUCCESS Reading succeed
2592 @retval EFI_ABORTED Command failed
2593 @retval EFI_DEVICE_ERROR Device status error.
2594
2595 **/
2596 EFI_STATUS
2597 AtaNonDataCommandIn (
2598 IN IDE_BLK_IO_DEV *IdeDev,
2599 IN UINT8 AtaCommand,
2600 IN UINT8 Device,
2601 IN UINT8 Feature,
2602 IN UINT8 SectorCount,
2603 IN UINT8 LbaLow,
2604 IN UINT8 LbaMiddle,
2605 IN UINT8 LbaHigh
2606 )
2607 {
2608 EFI_STATUS Status;
2609 UINT8 StatusRegister;
2610
2611 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
2612 if (EFI_ERROR (Status)) {
2613 return EFI_DEVICE_ERROR;
2614 }
2615
2616 //
2617 // Select device (bit4), set Lba mode(bit6) (use 0xe0 for compatibility)
2618 //
2619 IDEWritePortB (
2620 IdeDev->PciIo,
2621 IdeDev->IoPort->Head,
2622 (UINT8) ((IdeDev->Device << 4) | 0xe0)
2623 );
2624
2625 //
2626 // ATA commands for ATA device must be issued when DRDY is set
2627 //
2628 Status = DRDYReady (IdeDev, ATATIMEOUT);
2629 if (EFI_ERROR (Status)) {
2630 return EFI_DEVICE_ERROR;
2631 }
2632
2633 //
2634 // Pass parameter into device register block
2635 //
2636 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
2637 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature);
2638 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount);
2639 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
2640 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMiddle);
2641 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
2642
2643 //
2644 // Send command via Command Register
2645 //
2646 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
2647
2648 //
2649 // Wait for command completion
2650 // For ATAPI_SMART_CMD, we may need more timeout to let device
2651 // adjust internal states.
2652 //
2653 if (AtaCommand == ATA_CMD_SMART) {
2654 Status = WaitForBSYClear (IdeDev, ATASMARTTIMEOUT);
2655 } else {
2656 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
2657 }
2658 if (EFI_ERROR (Status)) {
2659 return EFI_DEVICE_ERROR;
2660 }
2661
2662 StatusRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Status);
2663 if ((StatusRegister & ATA_STSREG_ERR) == ATA_STSREG_ERR) {
2664 //
2665 // Failed to execute command, abort operation
2666 //
2667 return EFI_ABORTED;
2668 }
2669
2670 return EFI_SUCCESS;
2671 }
2672
2673
2674