2 Copyright (c) 2006, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 @par Revision Reference:
12 2002-6: Add Atapi6 enhancement, support >120GB hard disk, including
13 update - ATAIdentity() func
14 update - AtaBlockIoReadBlocks() func
15 update - AtaBlockIoWriteBlocks() func
16 add - AtaAtapi6Identify() func
17 add - AtaReadSectorsExt() func
18 add - AtaWriteSectorsExt() func
19 add - AtaPioDataInExt() func
20 add - AtaPioDataOutExt() func
29 IN IDE_BLK_IO_DEV
*IdeDev
,
30 IN OUT VOID
*DataBuffer
,
32 IN UINTN NumberOfBlocks
37 IN IDE_BLK_IO_DEV
*IdeDev
,
40 IN UINTN NumberOfBlocks
45 IN IDE_BLK_IO_DEV
*IdeDev
,
55 IN IDE_BLK_IO_DEV
*IdeDev
,
64 Sends out an ATA Identify Command to the specified device.
66 This function is called by DiscoverIdeDevice() during its device
67 identification. It sends out the ATA Identify Command to the
68 specified device. Only ATA device responses to this command. If
69 the command succeeds, it returns the Identify data structure which
70 contains information about the device. This function extracts the
71 information it needs to fill the IDE_BLK_IO_DEV data structure,
72 including device type, media block size, media capacity, and etc.
75 pointer pointing to IDE_BLK_IO_DEV data structure,used
76 to record all the information of the IDE device.
78 @retval EFI_SUCCESS Identify ATA device successfully.
80 @retval EFI_DEVICE_ERROR ATA Identify Device Command failed or
81 device is not ATA device.
84 parameter IdeDev will be updated in this function.
89 IN IDE_BLK_IO_DEV
*IdeDev
93 EFI_IDENTIFY_DATA
*AtaIdentifyPointer
;
98 // AtaIdentifyPointer is used for accommodating returned IDENTIFY data of
99 // the ATA Identify command
101 AtaIdentifyPointer
= (EFI_IDENTIFY_DATA
*) AllocateZeroPool (sizeof (EFI_IDENTIFY_DATA
));
104 // use ATA PIO Data In protocol to send ATA Identify command
105 // and receive data from device
108 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4);
109 Status
= AtaPioDataIn (
111 (VOID
*) AtaIdentifyPointer
,
112 sizeof (EFI_IDENTIFY_DATA
),
121 // If ATA Identify command succeeds, then according to the received
123 // identify the device type ( ATA or not ).
124 // If ATA device, fill the information in IdeDev.
125 // If not ATA device, return IDE_DEVICE_ERROR
127 if (!EFI_ERROR (Status
)) {
129 IdeDev
->pIdData
= AtaIdentifyPointer
;
132 // Print ATA Module Name
134 PrintAtaModuleName (IdeDev
);
137 // bit 15 of pAtaIdentify->config is used to identify whether device is
138 // ATA device or ATAPI device.
139 // if 0, means ATA device; if 1, means ATAPI device.
141 if ((AtaIdentifyPointer
->AtaData
.config
& 0x8000) == 0x00) {
143 // Detect if support S.M.A.R.T. If yes, enable it as default
145 AtaSMARTSupport (IdeDev
);
148 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device)
150 Status
= AtaAtapi6Identify (IdeDev
);
151 if (!EFI_ERROR (Status
)) {
153 // It's a disk with >120GB capacity, initialized in AtaAtapi6Identify()
158 // This is a hard disk <= 120GB capacity, treat it as normal hard disk
160 IdeDev
->Type
= IdeHardDisk
;
163 // Block Media Information:
164 // Media->LogicalPartition , Media->WriteCaching will be filled
165 // in the DiscoverIdeDevcie() function.
167 IdeDev
->BlkIo
.Media
->IoAlign
= 4;
168 IdeDev
->BlkIo
.Media
->MediaId
= 1;
169 IdeDev
->BlkIo
.Media
->RemovableMedia
= FALSE
;
170 IdeDev
->BlkIo
.Media
->MediaPresent
= TRUE
;
171 IdeDev
->BlkIo
.Media
->ReadOnly
= FALSE
;
172 IdeDev
->BlkIo
.Media
->BlockSize
= 0x200;
175 // Calculate device capacity
177 Capacity
= ((UINT32
)AtaIdentifyPointer
->AtaData
.user_addressable_sectors_hi
<< 16) |
178 AtaIdentifyPointer
->AtaData
.user_addressable_sectors_lo
;
179 IdeDev
->BlkIo
.Media
->LastBlock
= Capacity
- 1;
186 gBS
->FreePool (AtaIdentifyPointer
);
188 // Make sure the pIdData will not be freed again.
190 IdeDev
->pIdData
= NULL
;
192 return EFI_DEVICE_ERROR
;
197 This function is called by ATAIdentify() to identity whether this disk
198 supports ATA/ATAPI6 48bit addressing, ie support >120G capacity
201 pointer pointing to IDE_BLK_IO_DEV data structure, used
202 to record all the information of the IDE device.
204 @retval EFI_SUCCESS The disk specified by IdeDev is a Atapi6 supported one
205 and 48-bit addressing must be used
207 @retval EFI_UNSUPPORTED The disk dosn't not support Atapi6 or it supports but
208 the capacity is below 120G, 48bit addressing is not
212 This function must be called after DEVICE_IDENTITY command has been
213 successfully returned
218 IN IDE_BLK_IO_DEV
*IdeDev
224 EFI_IDENTIFY_DATA
*Atapi6IdentifyStruct
;
226 if (IdeDev
->pIdData
== NULL
) {
227 return EFI_UNSUPPORTED
;
230 Atapi6IdentifyStruct
= IdeDev
->pIdData
;
232 if ((Atapi6IdentifyStruct
->AtapiData
.cmd_set_support_83
& bit10
) == 0) {
234 // The device dosn't support 48 bit addressing
236 return EFI_UNSUPPORTED
;
240 // 48 bit address feature set is supported, get maximum capacity
242 Capacity
= Atapi6IdentifyStruct
->AtapiData
.max_user_lba_for_48bit_addr
[0];
243 for (Index
= 1; Index
< 4; Index
++) {
245 // Lower byte goes first: word[100] is the lowest word, word[103] is highest
247 TmpLba
= Atapi6IdentifyStruct
->AtapiData
.max_user_lba_for_48bit_addr
[Index
];
248 Capacity
|= LShiftU64 (TmpLba
, 16 * Index
);
251 if (Capacity
> MAX_28BIT_ADDRESSING_CAPACITY
) {
253 // Capacity exceeds 120GB. 48-bit addressing is really needed
255 IdeDev
->Type
= Ide48bitAddressingHardDisk
;
258 // Fill block media information:Media->LogicalPartition ,
259 // Media->WriteCaching will be filledin the DiscoverIdeDevcie() function.
261 IdeDev
->BlkIo
.Media
->IoAlign
= 4;
262 IdeDev
->BlkIo
.Media
->MediaId
= 1;
263 IdeDev
->BlkIo
.Media
->RemovableMedia
= FALSE
;
264 IdeDev
->BlkIo
.Media
->MediaPresent
= TRUE
;
265 IdeDev
->BlkIo
.Media
->ReadOnly
= FALSE
;
266 IdeDev
->BlkIo
.Media
->BlockSize
= 0x200;
267 IdeDev
->BlkIo
.Media
->LastBlock
= Capacity
- 1;
272 return EFI_UNSUPPORTED
;
276 This function is called by ATAIdentify() or ATAPIIdentify()
277 to print device's module name.
280 pointer pointing to IDE_BLK_IO_DEV data structure, used
281 to record all the information of the IDE device.
286 IN IDE_BLK_IO_DEV
*IdeDev
289 if (IdeDev
->pIdData
== NULL
) {
293 SwapStringChars (IdeDev
->ModelName
, IdeDev
->pIdData
->AtaData
.ModelName
, 40);
294 IdeDev
->ModelName
[40] = 0x00;
298 This function is used to send out ATA commands conforms to the
299 PIO Data In Protocol.
302 pointer pointing to IDE_BLK_IO_DEV data structure, used
303 to record all the information of the IDE device.
306 buffer contained data transferred from device to host.
309 data size in byte unit of the buffer.
311 @param[in] AtaCommand
312 value of the Command Register
315 value of the Head/Device Register
317 @param[in] SectorCount
318 value of the Sector Count Register
320 @param[in] SectorNumber
321 value of the Sector Number Register
323 @param[in] CylinderLsb
324 value of the low byte of the Cylinder Register
326 @param[in] CylinderMsb
327 value of the high byte of the Cylinder Register
329 @retval EFI_SUCCESS send out the ATA command and device send required
332 @retval EFI_DEVICE_ERROR command sent failed.
337 IN IDE_BLK_IO_DEV
*IdeDev
,
342 IN UINT8 SectorCount
,
343 IN UINT8 SectorNumber
,
344 IN UINT8 CylinderLsb
,
353 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
354 if (EFI_ERROR (Status
)) {
355 return EFI_DEVICE_ERROR
;
359 // e0:1110,0000-- bit7 and bit5 are reserved bits.
360 // bit6 set means LBA mode
364 IdeDev
->IoPort
->Head
,
365 (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0 | Head
)
369 // All ATAPI device's ATA commands can be issued regardless of the
372 if (IdeDev
->Type
== IdeHardDisk
) {
374 Status
= DRDYReady (IdeDev
, ATATIMEOUT
);
375 if (EFI_ERROR (Status
)) {
376 return EFI_DEVICE_ERROR
;
380 // set all the command parameters
381 // Before write to all the following registers, BSY and DRQ must be 0.
383 Status
= DRQClear2 (IdeDev
, ATATIMEOUT
);
384 if (EFI_ERROR (Status
)) {
385 return EFI_DEVICE_ERROR
;
388 if (AtaCommand
== SET_FEATURES_CMD
) {
389 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, 0x03);
392 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount
);
393 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, SectorNumber
);
394 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, CylinderLsb
);
395 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, CylinderMsb
);
398 // send command via Command Register
400 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, AtaCommand
);
402 Buffer16
= (UINT16
*) Buffer
;
405 // According to PIO data in protocol, host can perform a series of reads to
406 // the data register after each time device set DRQ ready;
407 // The data size of "a series of read" is command specific.
408 // For most ATA command, data size received from device will not exceed
409 // 1 sector, hence the data size for "a series of read" can be the whole data
410 // size of one command request.
411 // For ATA command such as Read Sector command, the data size of one ATA
412 // command request is often larger than 1 sector, according to the
413 // Read Sector command, the data size of "a series of read" is exactly 1
415 // Here for simplification reason, we specify the data size for
416 // "a series of read" to 1 sector (256 words) if data size of one ATA command
417 // request is larger than 256 words.
422 // used to record bytes of currently transfered data
426 while (WordCount
< ByteCount
/ 2) {
428 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
430 Status
= DRQReady2 (IdeDev
, ATATIMEOUT
);
431 if (EFI_ERROR (Status
)) {
432 return EFI_DEVICE_ERROR
;
435 Status
= CheckErrorStatus (IdeDev
);
436 if (EFI_ERROR (Status
)) {
437 return EFI_DEVICE_ERROR
;
441 // Get the byte count for one series of read
443 if ((WordCount
+ Increment
) > ByteCount
/ 2) {
444 Increment
= ByteCount
/ 2 - WordCount
;
447 IDEReadPortWMultiple (
449 IdeDev
->IoPort
->Data
,
454 WordCount
+= Increment
;
455 Buffer16
+= Increment
;
459 DRQClear (IdeDev
, ATATIMEOUT
);
461 return CheckErrorStatus (IdeDev
);
465 This function is used to send out ATA commands conforms to the
466 PIO Data Out Protocol.
469 pointer pointing to IDE_BLK_IO_DEV data structure, used
470 to record all the information of the IDE device.
472 @param *Buffer buffer contained data transferred from host to device.
473 @param ByteCount data size in byte unit of the buffer.
474 @param AtaCommand value of the Command Register
475 @param Head value of the Head/Device Register
476 @param SectorCount value of the Sector Count Register
477 @param SectorNumber value of the Sector Number Register
478 @param CylinderLsb value of the low byte of the Cylinder Register
479 @param CylinderMsb value of the high byte of the Cylinder Register
481 @retval EFI_SUCCESS send out the ATA command and device received required
484 @retval EFI_DEVICE_ERROR command sent failed.
489 IN IDE_BLK_IO_DEV
*IdeDev
,
494 IN UINT8 SectorCount
,
495 IN UINT8 SectorNumber
,
496 IN UINT8 CylinderLsb
,
505 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
506 if (EFI_ERROR (Status
)) {
507 return EFI_DEVICE_ERROR
;
511 // select device via Head/Device register.
512 // Before write Head/Device register, BSY and DRQ must be 0.
514 Status
= DRQClear2 (IdeDev
, ATATIMEOUT
);
515 if (EFI_ERROR (Status
)) {
516 return EFI_DEVICE_ERROR
;
520 // e0:1110,0000-- bit7 and bit5 are reserved bits.
521 // bit6 set means LBA mode
525 IdeDev
->IoPort
->Head
,
526 (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0 | Head
)
529 Status
= DRDYReady (IdeDev
, ATATIMEOUT
);
530 if (EFI_ERROR (Status
)) {
531 return EFI_DEVICE_ERROR
;
535 // set all the command parameters
536 // Before write to all the following registers, BSY and DRQ must be 0.
538 Status
= DRQClear2 (IdeDev
, ATATIMEOUT
);
539 if (EFI_ERROR (Status
)) {
540 return EFI_DEVICE_ERROR
;
543 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount
);
544 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, SectorNumber
);
545 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, CylinderLsb
);
546 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, CylinderMsb
);
549 // send command via Command Register
551 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, AtaCommand
);
553 Buffer16
= (UINT16
*) Buffer
;
556 // According to PIO data out protocol, host can perform a series of
557 // writes to the data register after each time device set DRQ ready;
558 // The data size of "a series of read" is command specific.
559 // For most ATA command, data size written to device will not exceed 1 sector,
560 // hence the data size for "a series of write" can be the data size of one
562 // For ATA command such as Write Sector command, the data size of one
563 // ATA command request is often larger than 1 sector, according to the
564 // Write Sector command, the data size of "a series of read" is exactly
566 // Here for simplification reason, we specify the data size for
567 // "a series of write" to 1 sector (256 words) if data size of one ATA command
568 // request is larger than 256 words.
573 while (WordCount
< ByteCount
/ 2) {
576 // DRQReady2-- read Alternate Status Register to determine the DRQ bit
577 // data transfer can be performed only when DRQ is ready.
579 Status
= DRQReady2 (IdeDev
, ATATIMEOUT
);
580 if (EFI_ERROR (Status
)) {
581 return EFI_DEVICE_ERROR
;
584 Status
= CheckErrorStatus (IdeDev
);
585 if (EFI_ERROR (Status
)) {
586 return EFI_DEVICE_ERROR
;
590 // Check the remaining byte count is less than 512 bytes
592 if ((WordCount
+ Increment
) > ByteCount
/ 2) {
593 Increment
= ByteCount
/ 2 - WordCount
;
596 // perform a series of write without check DRQ ready
599 IDEWritePortWMultiple (
601 IdeDev
->IoPort
->Data
,
605 WordCount
+= Increment
;
606 Buffer16
+= Increment
;
610 DRQClear (IdeDev
, ATATIMEOUT
);
612 return CheckErrorStatus (IdeDev
);
616 This function is used to analyze the Status Register and print out
617 some debug information and if there is ERR bit set in the Status
618 Register, the Error Register's value is also be parsed and print out.
621 pointer pointing to IDE_BLK_IO_DEV data structure, used
622 to record all the information of the IDE device.
624 @retval EFI_SUCCESS No err information in the Status Register.
625 @retval EFI_DEVICE_ERROR Any err information in the Status Register.
630 IN IDE_BLK_IO_DEV
*IdeDev
633 UINT8 StatusRegister
;
641 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
645 if (StatusRegister
& DWF
) {
648 "CheckErrorStatus()-- %02x : Error : Write Fault\n",
653 if (StatusRegister
& CORR
) {
656 "CheckErrorStatus()-- %02x : Error : Corrected Data\n",
661 if (StatusRegister
& ERR
) {
662 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
664 if (ErrorRegister
& BBK_ERR
) {
667 "CheckErrorStatus()-- %02x : Error : Bad Block Detected\n",
672 if (ErrorRegister
& UNC_ERR
) {
675 "CheckErrorStatus()-- %02x : Error : Uncorrectable Data\n",
680 if (ErrorRegister
& MC_ERR
) {
683 "CheckErrorStatus()-- %02x : Error : Media Change\n",
688 if (ErrorRegister
& ABRT_ERR
) {
691 "CheckErrorStatus()-- %02x : Error : Abort\n",
696 if (ErrorRegister
& TK0NF_ERR
) {
699 "CheckErrorStatus()-- %02x : Error : Track 0 Not Found\n",
704 if (ErrorRegister
& AMNF_ERR
) {
707 "CheckErrorStatus()-- %02x : Error : Address Mark Not Found\n",
715 if ((StatusRegister
& (ERR
| DWF
| CORR
)) == 0) {
719 return EFI_DEVICE_ERROR
;
724 This function is called by the AtaBlkIoReadBlocks() to perform
725 reading from media in block unit.
728 pointer pointing to IDE_BLK_IO_DEV data structure, used
729 to record all the information of the IDE device.
731 @param[in] *DataBuffer
732 A pointer to the destination buffer for the data.
735 The starting logical block address to read from
738 @param[in] NumberOfBlocks
739 The number of transfer data blocks.
741 @return return status is fully dependent on the return status
742 of AtaPioDataIn() function.
747 IN IDE_BLK_IO_DEV
*IdeDev
,
750 IN UINTN NumberOfBlocks
754 UINTN BlocksRemaining
;
769 // Using ATA Read Sector(s) command (opcode=0x20) with PIO DATA IN protocol
771 AtaCommand
= READ_SECTORS_CMD
;
774 BlocksRemaining
= NumberOfBlocks
;
776 Lba32
= (UINT32
) Lba
;
778 Status
= EFI_SUCCESS
;
780 while (BlocksRemaining
> 0) {
783 // in ATA-3 spec, LBA is in 28 bit width
785 Lba0
= (UINT8
) Lba32
;
786 Lba1
= (UINT8
) (Lba32
>> 8);
787 Lba2
= (UINT8
) (Lba32
>> 16);
789 // low 4 bit of Lba3 stands for LBA bit24~bit27.
791 Lba3
= (UINT8
) ((Lba32
>> 24) & 0x0f);
793 if (BlocksRemaining
>= 0x100) {
796 // SectorCount8 is sent to Sector Count register, 0x00 means 256
797 // sectors to be read
801 // SectorCount is used to record the number of sectors to be read
806 SectorCount8
= (UINT8
) BlocksRemaining
;
807 SectorCount
= (UINT16
) BlocksRemaining
;
811 // ByteCount is the number of bytes that will be read
813 ByteCount
= SectorCount
* (IdeDev
->BlkIo
.Media
->BlockSize
);
816 // call AtaPioDataIn() to send Read Sector Command and receive data read
818 Status
= AtaPioDataIn (
829 if (EFI_ERROR (Status
)) {
833 Lba32
+= SectorCount
;
834 Buffer
= ((UINT8
*) Buffer
+ ByteCount
);
835 BlocksRemaining
-= SectorCount
;
842 This function is called by the AtaBlkIoWriteBlocks() to perform
843 writing onto media in block unit.
846 pointer pointing to IDE_BLK_IO_DEV data structure,used
847 to record all the information of the IDE device.
849 @param[in] *BufferData
850 A pointer to the source buffer for the data.
853 The starting logical block address to write onto
856 @param[in] NumberOfBlocks
857 The number of transfer data blocks.
859 @return return status is fully dependent on the return status
860 of AtaPioDataOut() function.
865 IN IDE_BLK_IO_DEV
*IdeDev
,
868 IN UINTN NumberOfBlocks
872 UINTN BlocksRemaining
;
887 // Using Write Sector(s) command (opcode=0x30) with PIO DATA OUT protocol
889 AtaCommand
= WRITE_SECTORS_CMD
;
891 BlocksRemaining
= NumberOfBlocks
;
893 Lba32
= (UINT32
) Lba
;
895 Status
= EFI_SUCCESS
;
897 while (BlocksRemaining
> 0) {
899 Lba0
= (UINT8
) Lba32
;
900 Lba1
= (UINT8
) (Lba32
>> 8);
901 Lba2
= (UINT8
) (Lba32
>> 16);
902 Lba3
= (UINT8
) ((Lba32
>> 24) & 0x0f);
904 if (BlocksRemaining
>= 0x100) {
907 // SectorCount8 is sent to Sector Count register, 0x00 means 256 sectors
912 // SectorCount is used to record the number of sectors to be written
917 SectorCount8
= (UINT8
) BlocksRemaining
;
918 SectorCount
= (UINT16
) BlocksRemaining
;
921 ByteCount
= SectorCount
* (IdeDev
->BlkIo
.Media
->BlockSize
);
923 Status
= AtaPioDataOut (
934 if (EFI_ERROR (Status
)) {
938 Lba32
+= SectorCount
;
939 Buffer
= ((UINT8
*) Buffer
+ ByteCount
);
940 BlocksRemaining
-= SectorCount
;
947 This function is used to implement the Soft Reset on the specified
948 device. But, the ATA Soft Reset mechanism is so strong a reset method
949 that it will force resetting on both devices connected to the
952 It is called by IdeBlkIoReset(), a interface function of Block
955 This function can also be used by the ATAPI device to perform reset when
956 ATAPI Reset command is failed.
959 pointer pointing to IDE_BLK_IO_DEV data structure, used
960 to record all the information of the IDE device.
962 @retval EFI_SUCCESS Soft reset completes successfully.
963 @retval EFI_DEVICE_ERROR Any step during the reset process is failed.
966 The registers initial values after ATA soft reset are different
967 to the ATA device and ATAPI device.
972 IN IDE_BLK_IO_DEV
*IdeDev
980 // set SRST bit to initiate soft reset
982 DeviceControl
|= SRST
;
987 DeviceControl
|= bit1
;
989 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
992 // SRST should assert for at least 5 us, we use 10 us for
993 // better compatibility
998 // Enable interrupt to support UDMA, and clear SRST bit
1001 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
1004 // Wait for at least 2 ms to check BSY status, we use 10 ms
1005 // for better compatibility
1009 // slave device needs at most 31s to clear BSY
1011 if (WaitForBSYClear (IdeDev
, 31000) == EFI_TIMEOUT
) {
1012 return EFI_DEVICE_ERROR
;
1019 This function is the ATA implementation for ReadBlocks in the
1020 Block I/O Protocol interface.
1022 @param[in] *IdeBlkIoDevice
1023 Indicates the calling context.
1026 The media id that the read request is for.
1029 The starting logical block address to read from
1032 @param[in] BufferSize
1033 The size of the Buffer in bytes. This must be a
1034 multiple of the intrinsic block size of the device.
1037 A pointer to the destination buffer for the data.
1038 The caller is responsible for either having implicit
1039 or explicit ownership of the memory that data is read into.
1041 @retval EFI_SUCCESS Read Blocks successfully.
1042 @retval EFI_DEVICE_ERROR Read Blocks failed.
1043 @retval EFI_NO_MEDIA There is no media in the device.
1044 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media.
1046 @retval EFI_BAD_BUFFER_SIZE
1047 The BufferSize parameter is not a multiple of the
1048 intrinsic block size of the device.
1050 @retval EFI_INVALID_PARAMETER
1051 The read request contains LBAs that are not valid,
1052 or the data buffer is not valid.
1055 If Read Block error because of device error, this function will call
1056 AtaSoftReset() function to reset device.
1060 AtaBlkIoReadBlocks (
1061 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
,
1064 IN UINTN BufferSize
,
1068 EFI_BLOCK_IO_MEDIA
*Media
;
1070 UINTN NumberOfBlocks
;
1073 if (Buffer
== NULL
) {
1074 return EFI_INVALID_PARAMETER
;
1077 if (BufferSize
== 0) {
1081 Status
= EFI_SUCCESS
;
1084 // Get the intrinsic block size
1086 Media
= IdeBlkIoDevice
->BlkIo
.Media
;
1087 BlockSize
= Media
->BlockSize
;
1089 NumberOfBlocks
= BufferSize
/ BlockSize
;
1091 if (MediaId
!= Media
->MediaId
) {
1092 return EFI_MEDIA_CHANGED
;
1095 if (BufferSize
% BlockSize
!= 0) {
1096 return EFI_BAD_BUFFER_SIZE
;
1099 if (!(Media
->MediaPresent
)) {
1100 return EFI_NO_MEDIA
;
1103 if (LBA
> Media
->LastBlock
) {
1104 return EFI_INVALID_PARAMETER
;
1107 if ((LBA
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
1108 return EFI_INVALID_PARAMETER
;
1111 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
1112 return EFI_INVALID_PARAMETER
;
1115 if (IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1117 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 read block mechanism
1119 Status
= AtaUdmaReadExt (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1120 if (EFI_ERROR (Status
)) {
1121 Status
= AtaReadSectorsExt (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1125 // For ATA-3 compatible device, use ATA-3 read block mechanism
1127 Status
= AtaUdmaRead (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1128 if (EFI_ERROR (Status
)) {
1129 Status
= AtaReadSectors (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1133 if (EFI_ERROR (Status
)) {
1134 AtaSoftReset (IdeBlkIoDevice
);
1135 return EFI_DEVICE_ERROR
;
1143 This function is the ATA implementation for WriteBlocks in the
1144 Block I/O Protocol interface.
1146 @param[in] *IdeBlkIoDevice
1147 Indicates the calling context.
1150 The media id that the write request is for.
1153 The starting logical block address to write onto
1156 @param[in] BufferSize
1157 The size of the Buffer in bytes. This must be a
1158 multiple of the intrinsic block size of the device.
1161 A pointer to the source buffer for the data.
1162 The caller is responsible for either having implicit
1163 or explicit ownership of the memory that data is
1166 @retval EFI_SUCCESS Write Blocks successfully.
1167 @retval EFI_DEVICE_ERROR Write Blocks failed.
1168 @retval EFI_NO_MEDIA There is no media in the device.
1169 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media.
1171 @retval EFI_BAD_BUFFER_SIZE
1172 The BufferSize parameter is not a multiple of the
1173 intrinsic block size of the device.
1175 @retval EFI_INVALID_PARAMETER
1176 The write request contains LBAs that are not valid,
1177 or the data buffer is not valid.
1180 If Write Block error because of device error, this function will call
1181 AtaSoftReset() function to reset device.
1185 AtaBlkIoWriteBlocks (
1186 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
,
1189 IN UINTN BufferSize
,
1194 EFI_BLOCK_IO_MEDIA
*Media
;
1196 UINTN NumberOfBlocks
;
1199 if (Buffer
== NULL
) {
1200 return EFI_INVALID_PARAMETER
;
1203 if (BufferSize
== 0) {
1207 Status
= EFI_SUCCESS
;
1210 // Get the intrinsic block size
1212 Media
= IdeBlkIoDevice
->BlkIo
.Media
;
1213 BlockSize
= Media
->BlockSize
;
1214 NumberOfBlocks
= BufferSize
/ BlockSize
;
1216 if (MediaId
!= Media
->MediaId
) {
1217 return EFI_MEDIA_CHANGED
;
1220 if (BufferSize
% BlockSize
!= 0) {
1221 return EFI_BAD_BUFFER_SIZE
;
1224 if (LBA
> Media
->LastBlock
) {
1225 return EFI_INVALID_PARAMETER
;
1228 if ((LBA
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
1229 return EFI_INVALID_PARAMETER
;
1232 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
1233 return EFI_INVALID_PARAMETER
;
1236 if (IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1238 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 write block mechanism
1240 Status
= AtaUdmaWriteExt (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1241 if (EFI_ERROR (Status
)) {
1242 Status
= AtaWriteSectorsExt (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1246 // For ATA-3 compatible device, use ATA-3 write block mechanism
1248 Status
= AtaUdmaWrite (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1249 if (EFI_ERROR (Status
)) {
1250 Status
= AtaWriteSectors (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1254 if (EFI_ERROR (Status
)) {
1255 AtaSoftReset (IdeBlkIoDevice
);
1256 return EFI_DEVICE_ERROR
;
1263 This function is called by the AtaBlkIoReadBlocks() to perform
1264 reading from media in block unit. The function has been enhanced to
1265 support >120GB access and transfer at most 65536 blocks per command
1268 pointer pointing to IDE_BLK_IO_DEV data structure, used
1269 to record all the information of the IDE device.
1271 @param[in] *DataBuffer A pointer to the destination buffer for the data.
1272 @param[in] StartLba The starting logical block address to read from
1273 on the device media.
1274 @param[in] NumberOfBlocks The number of transfer data blocks.
1276 @return return status is fully dependent on the return status
1277 of AtaPioDataInExt() function.
1282 IN IDE_BLK_IO_DEV
*IdeDev
,
1283 IN VOID
*DataBuffer
,
1284 IN EFI_LBA StartLba
,
1285 IN UINTN NumberOfBlocks
1289 UINTN BlocksRemaining
;
1297 // Using ATA "Read Sectors Ext" command(opcode=0x24) with PIO DATA IN protocol
1299 AtaCommand
= READ_SECTORS_EXT_CMD
;
1300 Buffer
= DataBuffer
;
1301 BlocksRemaining
= NumberOfBlocks
;
1303 Status
= EFI_SUCCESS
;
1305 while (BlocksRemaining
> 0) {
1307 if (BlocksRemaining
>= 0x10000) {
1309 // SectorCount is used to record the number of sectors to be read
1310 // Max 65536 sectors can be transfered at a time.
1312 SectorCount
= 0xffff;
1314 SectorCount
= (UINT16
) BlocksRemaining
;
1318 // ByteCount is the number of bytes that will be read
1320 ByteCount
= SectorCount
* (IdeDev
->BlkIo
.Media
->BlockSize
);
1323 // call AtaPioDataInExt() to send Read Sector Command and receive data read
1325 Status
= AtaPioDataInExt (
1333 if (EFI_ERROR (Status
)) {
1337 Lba64
+= SectorCount
;
1338 Buffer
= ((UINT8
*) Buffer
+ ByteCount
);
1339 BlocksRemaining
-= SectorCount
;
1346 This function is called by the AtaBlkIoWriteBlocks() to perform
1347 writing onto media in block unit. The function has been enhanced to
1348 support >120GB access and transfer at most 65536 blocks per command
1351 pointer pointing to IDE_BLK_IO_DEV data structure,used
1352 to record all the information of the IDE device.
1354 @param[in] *DataBuffer
1355 A pointer to the source buffer for the data.
1358 The starting logical block address to write onto
1361 @param[in] NumberOfBlocks
1362 The number of transfer data blocks.
1364 @return status is fully dependent on the return status
1365 of AtaPioDataOutExt() function.
1369 AtaWriteSectorsExt (
1370 IN IDE_BLK_IO_DEV
*IdeDev
,
1371 IN VOID
*DataBuffer
,
1372 IN EFI_LBA StartLba
,
1373 IN UINTN NumberOfBlocks
1378 UINTN BlocksRemaining
;
1385 // Using ATA "Write Sectors Ext" cmd(opcode=0x24) with PIO DATA OUT protocol
1387 AtaCommand
= WRITE_SECTORS_EXT_CMD
;
1389 Buffer
= DataBuffer
;
1390 BlocksRemaining
= NumberOfBlocks
;
1392 Status
= EFI_SUCCESS
;
1394 while (BlocksRemaining
> 0) {
1396 if (BlocksRemaining
>= 0x10000) {
1398 // SectorCount is used to record the number of sectors to be written.
1399 // Max 65536 sectors can be transfered at a time.
1401 SectorCount
= 0xffff;
1403 SectorCount
= (UINT16
) BlocksRemaining
;
1407 // ByteCount is the number of bytes that will be written
1409 ByteCount
= SectorCount
* (IdeDev
->BlkIo
.Media
->BlockSize
);
1412 // Call AtaPioDataOutExt() to send "Write Sectors Ext" Command
1414 Status
= AtaPioDataOutExt (
1422 if (EFI_ERROR (Status
)) {
1426 Lba64
+= SectorCount
;
1427 Buffer
= ((UINT8
*) Buffer
+ ByteCount
);
1428 BlocksRemaining
-= SectorCount
;
1435 This function is used to send out ATA commands conforms to the
1436 PIO Data In Protocol, supporting ATA/ATAPI-6 standard
1438 Comparing with ATA-3 data in protocol, we have two differents here:<BR>
1439 1. Do NOT wait for DRQ clear before sending command into IDE device.(the
1440 wait will frequently fail... cause writing function return error)
1442 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly
1443 slow down writing performance by 100 times!)
1445 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
1446 to record all the information of the IDE device.
1448 @param[in,out] *Buffer buffer contained data transferred from device to host.
1449 @param[in] ByteCount data size in byte unit of the buffer.
1450 @param[in] AtaCommand value of the Command Register
1451 @param[in] StartLba the start LBA of this transaction
1452 @param[in] SectorCount the count of sectors to be transfered
1454 @retval EFI_SUCCESS send out the ATA command and device send required
1457 @retval EFI_DEVICE_ERROR command sent failed.
1462 IN IDE_BLK_IO_DEV
*IdeDev
,
1463 IN OUT VOID
*Buffer
,
1464 IN UINT32 ByteCount
,
1465 IN UINT8 AtaCommand
,
1466 IN EFI_LBA StartLba
,
1467 IN UINT16 SectorCount
1480 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
1481 if (EFI_ERROR (Status
)) {
1482 return EFI_DEVICE_ERROR
;
1486 // Select device, set bit6 as 1 to indicate LBA mode is used
1488 DevSel
= (UINT8
) (IdeDev
->Device
<< 4);
1492 IdeDev
->IoPort
->Head
,
1497 // Wait for DRDY singnal asserting. ATAPI device needn't wait
1499 if ( (IdeDev
->Type
== IdeHardDisk
) ||
1500 (IdeDev
->Type
== Ide48bitAddressingHardDisk
)) {
1502 Status
= DRDYReady (IdeDev
, ATATIMEOUT
);
1503 if (EFI_ERROR (Status
)) {
1504 return EFI_DEVICE_ERROR
;
1509 // Fill feature register if needed
1511 if (AtaCommand
== SET_FEATURES_CMD
) {
1512 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, 0x03);
1516 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1518 SectorCount8
= (UINT8
) (SectorCount
>> 8);
1519 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount8
);
1521 SectorCount8
= (UINT8
) SectorCount
;
1522 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount8
);
1525 // Fill the start LBA registers, which are also two-byte FIFO
1527 LbaLow
= (UINT8
) RShiftU64 (StartLba
, 24);
1528 LbaMid
= (UINT8
) RShiftU64 (StartLba
, 32);
1529 LbaHigh
= (UINT8
) RShiftU64 (StartLba
, 40);
1530 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1531 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMid
);
1532 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
1534 LbaLow
= (UINT8
) StartLba
;
1535 LbaMid
= (UINT8
) RShiftU64 (StartLba
, 8);
1536 LbaHigh
= (UINT8
) RShiftU64 (StartLba
, 16);
1537 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1538 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMid
);
1539 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
1542 // Send command via Command Register, invoking the processing of this command
1544 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, AtaCommand
);
1546 Buffer16
= (UINT16
*) Buffer
;
1549 // According to PIO data in protocol, host can perform a series of reads to
1550 // the data register after each time device set DRQ ready;
1559 // used to record bytes of currently transfered data
1563 while (WordCount
< ByteCount
/ 2) {
1565 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
1567 Status
= DRQReady2 (IdeDev
, ATATIMEOUT
);
1568 if (EFI_ERROR (Status
)) {
1569 return EFI_DEVICE_ERROR
;
1572 Status
= CheckErrorStatus (IdeDev
);
1573 if (EFI_ERROR (Status
)) {
1574 return EFI_DEVICE_ERROR
;
1578 // Get the byte count for one series of read
1580 if ((WordCount
+ Increment
) > ByteCount
/ 2) {
1581 Increment
= ByteCount
/ 2 - WordCount
;
1584 IDEReadPortWMultiple (
1586 IdeDev
->IoPort
->Data
,
1591 WordCount
+= Increment
;
1592 Buffer16
+= Increment
;
1596 return CheckErrorStatus (IdeDev
);
1600 This function is used to send out ATA commands conforms to the
1601 PIO Data Out Protocol, supporting ATA/ATAPI-6 standard
1603 Comparing with ATA-3 data out protocol, we have two differents here:<BR>
1604 1. Do NOT wait for DRQ clear before sending command into IDE device.(the
1605 wait will frequently fail... cause writing function return error)
1607 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly
1608 slow down writing performance by 100 times!)
1611 pointer pointing to IDE_BLK_IO_DEV data structure, used
1612 to record all the information of the IDE device.
1614 @param[in] *Buffer buffer contained data transferred from host to device.
1615 @param[in] ByteCount data size in byte unit of the buffer.
1616 @param[in] AtaCommand value of the Command Register
1617 @param[in] StartLba the start LBA of this transaction
1618 @param[in] SectorCount the count of sectors to be transfered
1620 @retval EFI_SUCCESS send out the ATA command and device receive required
1623 @retval EFI_DEVICE_ERROR command sent failed.
1628 IN IDE_BLK_IO_DEV
*IdeDev
,
1630 IN UINT32 ByteCount
,
1631 IN UINT8 AtaCommand
,
1632 IN EFI_LBA StartLba
,
1633 IN UINT16 SectorCount
1646 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
1647 if (EFI_ERROR (Status
)) {
1648 return EFI_DEVICE_ERROR
;
1652 // Select device. Set bit6 as 1 to indicate LBA mode is used
1654 DevSel
= (UINT8
) (IdeDev
->Device
<< 4);
1658 IdeDev
->IoPort
->Head
,
1663 // Wait for DRDY singnal asserting.
1665 Status
= DRDYReady (IdeDev
, ATATIMEOUT
);
1666 if (EFI_ERROR (Status
)) {
1667 return EFI_DEVICE_ERROR
;
1671 // Fill feature register if needed
1673 if (AtaCommand
== SET_FEATURES_CMD
) {
1674 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, 0x03);
1678 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1680 SectorCount8
= (UINT8
) (SectorCount
>> 8);
1681 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount8
);
1683 SectorCount8
= (UINT8
) SectorCount
;
1684 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount8
);
1687 // Fill the start LBA registers, which are also two-byte FIFO
1689 LbaLow
= (UINT8
) RShiftU64 (StartLba
, 24);
1690 LbaMid
= (UINT8
) RShiftU64 (StartLba
, 32);
1691 LbaHigh
= (UINT8
) RShiftU64 (StartLba
, 40);
1692 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1693 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMid
);
1694 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
1696 LbaLow
= (UINT8
) StartLba
;
1697 LbaMid
= (UINT8
) RShiftU64 (StartLba
, 8);
1698 LbaHigh
= (UINT8
) RShiftU64 (StartLba
, 16);
1699 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1700 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMid
);
1701 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
1704 // Send command via Command Register, invoking the processing of this command
1706 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, AtaCommand
);
1708 Buffer16
= (UINT16
*) Buffer
;
1711 // According to PIO Data Out protocol, host can perform a series of writes to
1712 // the data register after each time device set DRQ ready;
1717 // used to record bytes of currently transfered data
1721 while (WordCount
< ByteCount
/ 2) {
1723 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
1725 Status
= DRQReady2 (IdeDev
, ATATIMEOUT
);
1726 if (EFI_ERROR (Status
)) {
1727 return EFI_DEVICE_ERROR
;
1730 Status
= CheckErrorStatus (IdeDev
);
1731 if (EFI_ERROR (Status
)) {
1732 return EFI_DEVICE_ERROR
;
1736 // Write data into device by one series of writing to data register
1738 if ((WordCount
+ Increment
) > ByteCount
/ 2) {
1739 Increment
= ByteCount
/ 2 - WordCount
;
1742 IDEWritePortWMultiple (
1744 IdeDev
->IoPort
->Data
,
1749 WordCount
+= Increment
;
1750 Buffer16
+= Increment
;
1757 return CheckErrorStatus (IdeDev
);
1762 Enable SMART of the disk if supported
1765 pointer pointing to IDE_BLK_IO_DEV data structure,used
1766 to record all the information of the IDE device.
1771 IN IDE_BLK_IO_DEV
*IdeDev
1775 BOOLEAN SMARTSupported
;
1777 EFI_IDENTIFY_DATA
*TmpAtaIdentifyPointer
;
1783 // Detect if the device supports S.M.A.R.T.
1785 if ((IdeDev
->pIdData
->AtaData
.command_set_supported_83
& 0xc000) != 0x4000) {
1787 // Data in word 82 is not valid (bit15 shall be zero and bit14 shall be to one)
1791 if ((IdeDev
->pIdData
->AtaData
.command_set_supported_82
& 0x0001) != 0x0001) {
1793 // S.M.A.R.T is not supported by the device
1795 SMARTSupported
= FALSE
;
1797 SMARTSupported
= TRUE
;
1801 if (!SMARTSupported
) {
1803 // Report nonsupport status code
1805 REPORT_STATUS_CODE (
1806 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1807 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_ATA_BUS_SMART_NOTSUPPORTED
)
1811 // Enable this feature
1813 REPORT_STATUS_CODE (
1815 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_ATA_BUS_SMART_ENABLE
)
1818 Device
= (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0);
1819 Status
= AtaNonDataCommandIn (
1823 ATA_SMART_ENABLE_OPERATION
,
1830 // Detect if this feature is enabled
1832 TmpAtaIdentifyPointer
= (EFI_IDENTIFY_DATA
*) AllocateZeroPool (sizeof (EFI_IDENTIFY_DATA
));
1834 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4);
1835 Status
= AtaPioDataIn (
1837 (VOID
*) TmpAtaIdentifyPointer
,
1838 sizeof (EFI_IDENTIFY_DATA
),
1846 if (EFI_ERROR (Status
)) {
1847 gBS
->FreePool (TmpAtaIdentifyPointer
);
1852 // Check if the feature is enabled
1854 if ((TmpAtaIdentifyPointer
->AtaData
.command_set_feature_enb_85
& 0x0001) == 0x0001) {
1858 AtaNonDataCommandIn (
1862 ATA_SMART_RETURN_STATUS
,
1868 LBAMid
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
);
1869 LBAHigh
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
);
1871 if ((LBAMid
== 0x4f) && (LBAHigh
== 0xc2)) {
1873 // The threshold exceeded condition is not detected by the device
1875 REPORT_STATUS_CODE (
1877 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_ATA_BUS_SMART_UNDERTHRESHOLD
)
1880 } else if ((LBAMid
== 0xf4) && (LBAHigh
== 0x2c)) {
1882 // The threshold exceeded condition is detected by the device
1884 REPORT_STATUS_CODE (
1886 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_ATA_BUS_SMART_OVERTHRESHOLD
)
1892 // Report disabled status code
1894 REPORT_STATUS_CODE (
1895 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1896 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_ATA_BUS_SMART_DISABLED
)
1900 gBS
->FreePool (TmpAtaIdentifyPointer
);
1907 Send ATA Ext command into device with NON_DATA protocol
1909 @param IdeDev Standard IDE device private data structure
1910 @param AtaCommand The ATA command to be sent
1911 @param Device The value in Device register
1912 @param Feature The value in Feature register
1913 @param SectorCount The value in SectorCount register
1914 @param LbaAddress The LBA address in 48-bit mode
1916 @retval EFI_SUCCESS Reading succeed
1917 @retval EFI_DEVICE_ERROR Error executing commands on this device
1921 AtaCommandIssueExt (
1922 IN IDE_BLK_IO_DEV
*IdeDev
,
1923 IN UINT8 AtaCommand
,
1926 IN UINT16 SectorCount
,
1927 IN EFI_LBA LbaAddress
1937 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
1938 if (EFI_ERROR (Status
)) {
1939 return EFI_DEVICE_ERROR
;
1943 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
1947 IdeDev
->IoPort
->Head
,
1948 (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0)
1952 // ATA commands for ATA device must be issued when DRDY is set
1954 Status
= DRDYReady (IdeDev
, ATATIMEOUT
);
1955 if (EFI_ERROR (Status
)) {
1956 return EFI_DEVICE_ERROR
;
1960 // Pass parameter into device register block
1962 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
1965 // Fill the feature register, which is a two-byte FIFO. Need write twice.
1967 Feature8
= (UINT8
) (Feature
>> 8);
1968 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, Feature8
);
1970 Feature8
= (UINT8
) Feature
;
1971 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, Feature8
);
1974 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1976 SectorCount8
= (UINT8
) (SectorCount
>> 8);
1977 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount8
);
1979 SectorCount8
= (UINT8
) SectorCount
;
1980 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount8
);
1983 // Fill the start LBA registers, which are also two-byte FIFO
1985 LbaLow
= (UINT8
) RShiftU64 (LbaAddress
, 24);
1986 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1987 LbaLow
= (UINT8
) LbaAddress
;
1988 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1990 LbaMid
= (UINT8
) RShiftU64 (LbaAddress
, 32);
1991 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMid
);
1992 LbaMid
= (UINT8
) RShiftU64 (LbaAddress
, 8);
1993 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMid
);
1995 LbaHigh
= (UINT8
) RShiftU64 (LbaAddress
, 40);
1996 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
1997 LbaHigh
= (UINT8
) RShiftU64 (LbaAddress
, 16);
1998 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
2001 // Work around for Segate 160G disk writing
2006 // Send command via Command Register
2008 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, AtaCommand
);
2011 // Stall at least 400ns
2019 Send ATA Ext command into device with NON_DATA protocol
2021 @param IdeDev Standard IDE device private data structure
2022 @param AtaCommand The ATA command to be sent
2023 @param Device The value in Device register
2024 @param Feature The value in Feature register
2025 @param SectorCount The value in SectorCount register
2026 @param LbaAddress The LBA address in 48-bit mode
2028 @retval EFI_SUCCESS Reading succeed
2029 @retval EFI_DEVICE_ERROR Error executing commands on this device
2034 IN IDE_BLK_IO_DEV
*IdeDev
,
2035 IN UINT8 AtaCommand
,
2038 IN UINT16 SectorCount
,
2039 IN EFI_LBA LbaAddress
2050 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
2051 if (EFI_ERROR (Status
)) {
2052 return EFI_DEVICE_ERROR
;
2056 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
2060 IdeDev
->IoPort
->Head
,
2061 (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0)
2065 // ATA commands for ATA device must be issued when DRDY is set
2067 Status
= DRDYReady (IdeDev
, ATATIMEOUT
);
2068 if (EFI_ERROR (Status
)) {
2069 return EFI_DEVICE_ERROR
;
2072 Lba0
= (UINT8
) LbaAddress
;
2073 Lba1
= (UINT8
) RShiftU64 (LbaAddress
, 8);
2074 Lba2
= (UINT8
) RShiftU64 (LbaAddress
, 16);
2075 Lba3
= (UINT8
) RShiftU64 (LbaAddress
, 24);
2079 // Pass parameter into device register block
2081 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
2084 // Fill the feature register, which is a two-byte FIFO. Need write twice.
2086 Feature8
= (UINT8
) Feature
;
2087 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, Feature8
);
2090 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
2092 SectorCount8
= (UINT8
) SectorCount
;
2093 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount8
);
2096 // Fill the start LBA registers, which are also two-byte FIFO
2099 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, Lba0
);
2100 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, Lba1
);
2101 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, Lba2
);
2104 // Send command via Command Register
2106 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, AtaCommand
);
2109 // Stall at least 400ns
2117 This function is called by the AtaBlkIoReadBlocks() to perform
2118 reading from media in block unit. The function has been enhanced to
2119 support >120GB access and transfer at most 65536 blocks per command
2121 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2122 to record all the information of the IDE device.
2124 @param[in] *DataBuffer A pointer to the destination buffer for the data.
2126 @param[in] StartLba The starting logical block address to read from
2127 on the device media.
2129 @param[in] NumberOfBlocks The number of transfer data blocks.
2131 @return The device status of UDMA operation. If the operation is
2132 successful, return EFI_SUCCESS.
2134 TODO: EFI_UNSUPPORTED - add return value to function comment
2135 TODO: EFI_DEVICE_ERROR - add return value to function comment
2136 TODO: EFI_DEVICE_ERROR - add return value to function comment
2137 TODO: EFI_DEVICE_ERROR - add return value to function comment
2141 IN IDE_BLK_IO_DEV
*IdeDev
,
2142 IN VOID
*DataBuffer
,
2143 IN EFI_LBA StartLba
,
2144 IN UINTN NumberOfBlocks
2147 IDE_DMA_PRD
*PrdAddr
;
2148 IDE_DMA_PRD
*UsedPrdAddr
;
2149 IDE_DMA_PRD
*TempPrdAddr
;
2150 UINT8 RegisterValue
;
2152 UINT64 IoPortForBmic
;
2153 UINT64 IoPortForBmis
;
2154 UINT64 IoPortForBmid
;
2158 UINTN ByteAvailable
;
2160 UINTN RemainBlockNum
;
2161 UINT8 DeviceControl
;
2166 EFI_PHYSICAL_ADDRESS DeviceAddress
;
2169 // Channel and device differential. Select device.
2171 Device
= (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0);
2174 // Enable interrupt to support UDMA and Select device
2177 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
2179 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
2181 if (IdePrimary
== IdeDev
->Channel
) {
2182 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICP_OFFSET
;
2183 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISP_OFFSET
;
2184 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDP_OFFSET
;
2186 if (IdeSecondary
== IdeDev
->Channel
) {
2187 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICS_OFFSET
;
2188 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISS_OFFSET
;
2189 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDS_OFFSET
;
2191 return EFI_UNSUPPORTED
;
2195 RemainBlockNum
= NumberOfBlocks
;
2196 while (RemainBlockNum
> 0) {
2198 if (RemainBlockNum
>= MAX_DMA_EXT_COMMAND_SECTORS
) {
2200 // SectorCount is used to record the number of sectors to be read
2201 // Max 65536 sectors can be transfered at a time.
2203 NumberOfBlocks
= MAX_DMA_EXT_COMMAND_SECTORS
;
2204 RemainBlockNum
-= MAX_DMA_EXT_COMMAND_SECTORS
;
2206 NumberOfBlocks
= (UINT16
) RemainBlockNum
;
2211 // Calculate the number of PRD table to make sure the memory region
2212 // not cross 64K boundary
2214 ByteCount
= NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
2215 PrdTableNum
= ((ByteCount
>> 16) + 1) + 1;
2220 PageCount
= EFI_SIZE_TO_PAGES (2 * PrdTableNum
* sizeof (IDE_DMA_PRD
));
2221 Status
= IdeDev
->PciIo
->AllocateBuffer (
2224 EfiBootServicesData
,
2229 if (EFI_ERROR (Status
)) {
2230 return EFI_OUT_OF_RESOURCES
;
2232 ZeroMem ((VOID
*) ((UINTN
) MemPage
), EFI_PAGES_TO_SIZE (PageCount
));
2234 PrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) MemPage
);
2237 // To make sure PRD is allocated in one 64K page
2239 if (((UINTN
) PrdAddr
& 0x0FFFF) > (((UINTN
) PrdAddr
+ PrdTableNum
* sizeof (IDE_DMA_PRD
) - 1) & 0x0FFFF)) {
2240 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x10000) & 0xFFFF0000);
2242 if ((UINTN
) PrdAddr
& 0x03) {
2243 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x04) & 0xFFFFFFFC);
2245 UsedPrdAddr
= PrdAddr
;
2250 // Build the PRD table
2252 Status
= IdeDev
->PciIo
->Map (
2254 EfiPciIoOperationBusMasterWrite
,
2260 if (EFI_ERROR (Status
)) {
2261 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
2262 return EFI_OUT_OF_RESOURCES
;
2264 PrdBuffer
= (VOID
*) ((UINTN
) DeviceAddress
);
2265 TempPrdAddr
= UsedPrdAddr
;
2268 ByteAvailable
= 0x10000 - ((UINTN
) PrdBuffer
& 0xFFFF);
2270 if (ByteCount
<= ByteAvailable
) {
2271 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
2272 TempPrdAddr
->ByteCount
= (UINT16
) ByteCount
;
2273 TempPrdAddr
->EndOfTable
= 0x8000;
2277 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
2278 TempPrdAddr
->ByteCount
= (UINT16
) ByteAvailable
;
2280 ByteCount
-= ByteAvailable
;
2281 PrdBuffer
+= ByteAvailable
;
2286 // Set the base address to BMID register
2288 IdeDev
->PciIo
->Io
.Write (
2290 EfiPciIoWidthUint32
,
2291 EFI_PCI_IO_PASS_THROUGH_BAR
,
2298 // Set BMIC register to identify the operation direction
2300 IdeDev
->PciIo
->Io
.Read (
2303 EFI_PCI_IO_PASS_THROUGH_BAR
,
2309 RegisterValue
|= BMIC_nREAD
;
2311 IdeDev
->PciIo
->Io
.Write (
2314 EFI_PCI_IO_PASS_THROUGH_BAR
,
2321 // Read BMIS register and clear ERROR and INTR bit
2323 IdeDev
->PciIo
->Io
.Read (
2326 EFI_PCI_IO_PASS_THROUGH_BAR
,
2332 RegisterValue
|= BMIS_INTERRUPT
| BMIS_ERROR
;
2334 IdeDev
->PciIo
->Io
.Write (
2337 EFI_PCI_IO_PASS_THROUGH_BAR
,
2344 // Issue READ DMA EXT command
2346 Status
= AtaCommandIssueExt (
2351 (UINT16
) NumberOfBlocks
,
2354 if (EFI_ERROR (Status
)) {
2355 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
2356 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2357 return EFI_DEVICE_ERROR
;
2361 // Set START bit of BMIC register
2363 IdeDev
->PciIo
->Io
.Read (
2366 EFI_PCI_IO_PASS_THROUGH_BAR
,
2372 RegisterValue
|= BMIC_START
;
2374 IdeDev
->PciIo
->Io
.Write (
2377 EFI_PCI_IO_PASS_THROUGH_BAR
,
2384 // Check the INTERRUPT and ERROR bit of BMIS
2385 // Max transfer number of sectors for one command is 65536(32Mbyte),
2386 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
2387 // So set the variable Count to 2000, for about 2 second timeout time.
2392 IdeDev
->PciIo
->Io
.Read (
2395 EFI_PCI_IO_PASS_THROUGH_BAR
,
2400 if ((RegisterValue
& (BMIS_INTERRUPT
| BMIS_ERROR
)) || (Count
== 0)) {
2401 if ((RegisterValue
& BMIS_ERROR
) || (Count
== 0)) {
2403 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
2405 IdeDev
->PciIo
->Io
.Read (
2408 EFI_PCI_IO_PASS_THROUGH_BAR
,
2414 RegisterValue
&= ~((UINT8
)BMIC_START
);
2416 IdeDev
->PciIo
->Io
.Write (
2419 EFI_PCI_IO_PASS_THROUGH_BAR
,
2424 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
2425 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2426 return EFI_DEVICE_ERROR
;
2435 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
2436 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2438 // Read Status Register of IDE device to clear interrupt
2440 RegisterValue
= IDEReadPortB(IdeDev
->PciIo
,IdeDev
->IoPort
->Reg
.Status
);
2442 // Clear START bit of BMIC register
2444 IdeDev
->PciIo
->Io
.Read (
2447 EFI_PCI_IO_PASS_THROUGH_BAR
,
2453 RegisterValue
&= ~((UINT8
) BMIC_START
);
2455 IdeDev
->PciIo
->Io
.Write (
2458 EFI_PCI_IO_PASS_THROUGH_BAR
,
2464 if (RegisterValue
& BMIS_ERROR
) {
2465 return EFI_DEVICE_ERROR
;
2468 DataBuffer
= (UINT8
*) DataBuffer
+ NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
2469 StartLba
+= NumberOfBlocks
;
2473 // Disable interrupt of Select device
2475 IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
);
2476 DeviceControl
|= IEN_L
;
2477 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
2483 This function is called by the AtaBlkIoReadBlocks() to perform
2484 reading from media in block unit. The function has been enhanced to
2485 support >120GB access and transfer at most 65536 blocks per command
2488 pointer pointing to IDE_BLK_IO_DEV data structure, used
2489 to record all the information of the IDE device.
2491 @param[in] *DataBuffer A pointer to the destination buffer for the data.
2492 @param[in] StartLba The starting logical block address to read from
2493 on the device media.
2494 @param[in] NumberOfBlocks The number of transfer data blocks.
2496 @return The device status of UDMA operation. If the operation is
2497 successful, return EFI_SUCCESS.
2499 TODO: EFI_UNSUPPORTED - add return value to function comment
2500 TODO: EFI_DEVICE_ERROR - add return value to function comment
2501 TODO: EFI_DEVICE_ERROR - add return value to function comment
2502 TODO: EFI_DEVICE_ERROR - add return value to function comment
2506 IN IDE_BLK_IO_DEV
*IdeDev
,
2507 IN VOID
*DataBuffer
,
2508 IN EFI_LBA StartLba
,
2509 IN UINTN NumberOfBlocks
2512 IDE_DMA_PRD
*PrdAddr
;
2513 IDE_DMA_PRD
*UsedPrdAddr
;
2514 IDE_DMA_PRD
*TempPrdAddr
;
2515 UINT8 RegisterValue
;
2517 UINT64 IoPortForBmic
;
2518 UINT64 IoPortForBmis
;
2519 UINT64 IoPortForBmid
;
2523 UINTN ByteAvailable
;
2525 UINTN RemainBlockNum
;
2526 UINT8 DeviceControl
;
2531 EFI_PHYSICAL_ADDRESS DeviceAddress
;
2534 // Channel and device differential
2536 Device
= (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0);
2539 // Enable interrupt to support UDMA and Select device
2542 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
2544 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
2546 if (IdePrimary
== IdeDev
->Channel
) {
2547 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICP_OFFSET
;
2548 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISP_OFFSET
;
2549 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDP_OFFSET
;
2551 if (IdeSecondary
== IdeDev
->Channel
) {
2552 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICS_OFFSET
;
2553 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISS_OFFSET
;
2554 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDS_OFFSET
;
2556 return EFI_UNSUPPORTED
;
2560 RemainBlockNum
= NumberOfBlocks
;
2561 while (RemainBlockNum
> 0) {
2563 if (RemainBlockNum
>= MAX_DMA_COMMAND_SECTORS
) {
2565 // SectorCount is used to record the number of sectors to be read
2566 // Max 256 sectors can be transfered at a time.
2568 NumberOfBlocks
= MAX_DMA_COMMAND_SECTORS
;
2569 RemainBlockNum
-= MAX_DMA_COMMAND_SECTORS
;
2571 NumberOfBlocks
= (UINT16
) RemainBlockNum
;
2576 // Calculate the number of PRD table to make sure the memory region
2577 // not cross 64K boundary
2579 ByteCount
= NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
2580 PrdTableNum
= ((ByteCount
>> 16) + 1) + 1;
2585 PageCount
= EFI_SIZE_TO_PAGES (2 * PrdTableNum
* sizeof (IDE_DMA_PRD
));
2586 Status
= IdeDev
->PciIo
->AllocateBuffer (
2589 EfiBootServicesData
,
2594 if (EFI_ERROR (Status
)) {
2595 return EFI_OUT_OF_RESOURCES
;
2597 ZeroMem ((VOID
*) ((UINTN
) MemPage
), EFI_PAGES_TO_SIZE (PageCount
));
2599 PrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) MemPage
);
2601 // To make sure PRD is allocated in one 64K page
2603 if (((UINTN
) PrdAddr
& 0x0FFFF) > (((UINTN
) PrdAddr
+ PrdTableNum
* sizeof (IDE_DMA_PRD
) - 1) & 0x0FFFF)) {
2604 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x10000) & 0xFFFF0000);
2606 if ((UINTN
) PrdAddr
& 0x03) {
2607 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x04) & 0xFFFFFFFC);
2609 UsedPrdAddr
= PrdAddr
;
2614 // Build the PRD table
2616 Status
= IdeDev
->PciIo
->Map (
2618 EfiPciIoOperationBusMasterWrite
,
2624 if (EFI_ERROR (Status
)) {
2625 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
2626 return EFI_OUT_OF_RESOURCES
;
2628 PrdBuffer
= (UINT8
*) ((UINTN
) DeviceAddress
);
2629 TempPrdAddr
= UsedPrdAddr
;
2632 ByteAvailable
= 0x10000 - ((UINTN
) PrdBuffer
& 0xFFFF);
2634 if (ByteCount
<= ByteAvailable
) {
2635 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
2636 TempPrdAddr
->ByteCount
= (UINT16
) ByteCount
;
2637 TempPrdAddr
->EndOfTable
= 0x8000;
2641 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
2642 TempPrdAddr
->ByteCount
= (UINT16
) ByteAvailable
;
2644 ByteCount
-= ByteAvailable
;
2645 PrdBuffer
+= ByteAvailable
;
2650 // Set the base address to BMID register
2652 IdeDev
->PciIo
->Io
.Write (
2654 EfiPciIoWidthUint32
,
2655 EFI_PCI_IO_PASS_THROUGH_BAR
,
2662 // Set BMIC register to identify the operation direction
2664 IdeDev
->PciIo
->Io
.Read (
2667 EFI_PCI_IO_PASS_THROUGH_BAR
,
2673 RegisterValue
|= BMIC_nREAD
;
2675 IdeDev
->PciIo
->Io
.Write (
2678 EFI_PCI_IO_PASS_THROUGH_BAR
,
2685 // Read BMIS register and clear ERROR and INTR bit
2687 IdeDev
->PciIo
->Io
.Read (
2690 EFI_PCI_IO_PASS_THROUGH_BAR
,
2696 RegisterValue
|= (BMIS_INTERRUPT
| BMIS_ERROR
);
2698 IdeDev
->PciIo
->Io
.Write (
2701 EFI_PCI_IO_PASS_THROUGH_BAR
,
2708 // Issue READ DMA command
2710 Status
= AtaCommandIssue (
2715 (UINT16
) NumberOfBlocks
,
2718 if (EFI_ERROR (Status
)) {
2719 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
2720 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2721 return EFI_DEVICE_ERROR
;
2725 // Set START bit of BMIC register
2727 IdeDev
->PciIo
->Io
.Read (
2730 EFI_PCI_IO_PASS_THROUGH_BAR
,
2736 RegisterValue
|= BMIC_START
;
2738 IdeDev
->PciIo
->Io
.Write (
2741 EFI_PCI_IO_PASS_THROUGH_BAR
,
2748 // Check the INTERRUPT and ERROR bit of BMIS
2749 // Max transfer number of sectors for one command is 65536(32Mbyte),
2750 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
2751 // So set the variable Count to 2000, for about 2 second timeout time.
2756 IdeDev
->PciIo
->Io
.Read (
2759 EFI_PCI_IO_PASS_THROUGH_BAR
,
2764 if ((RegisterValue
& (BMIS_INTERRUPT
| BMIS_ERROR
)) || (Count
== 0)) {
2765 if ((RegisterValue
& BMIS_ERROR
) || (Count
== 0)) {
2767 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
2769 IdeDev
->PciIo
->Io
.Read (
2772 EFI_PCI_IO_PASS_THROUGH_BAR
,
2778 RegisterValue
&= ~((UINT8
)BMIC_START
);
2780 IdeDev
->PciIo
->Io
.Write (
2783 EFI_PCI_IO_PASS_THROUGH_BAR
,
2788 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
2789 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2790 return EFI_DEVICE_ERROR
;
2799 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
2800 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2802 // Read Status Register of IDE device to clear interrupt
2804 RegisterValue
= IDEReadPortB(IdeDev
->PciIo
,IdeDev
->IoPort
->Reg
.Status
);
2806 // Clear START bit of BMIC register
2808 IdeDev
->PciIo
->Io
.Read (
2811 EFI_PCI_IO_PASS_THROUGH_BAR
,
2817 RegisterValue
&= ~((UINT8
) BMIC_START
);
2819 IdeDev
->PciIo
->Io
.Write (
2822 EFI_PCI_IO_PASS_THROUGH_BAR
,
2828 if (RegisterValue
& BMIS_ERROR
) {
2829 return EFI_DEVICE_ERROR
;
2832 DataBuffer
= (UINT8
*) DataBuffer
+ NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
2833 StartLba
+= NumberOfBlocks
;
2837 // Disable interrupt of Select device
2839 IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
);
2840 DeviceControl
|= IEN_L
;
2841 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
2847 This function is called by the AtaBlkIoWriteBlocks() to perform
2848 writing to media in block unit. The function has been enhanced to
2849 support >120GB access and transfer at most 65536 blocks per command
2851 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2852 to record all the information of the IDE device.
2854 @param[in] *DataBuffer A pointer to the source buffer for the data.
2856 @param[in] StartLba The starting logical block address to write to
2857 on the device media.
2859 @param[in] NumberOfBlocks The number of transfer data blocks.
2861 @return The device status of UDMA operation. If the operation is
2862 successful, return EFI_SUCCESS.
2864 TODO: EFI_UNSUPPORTED - add return value to function comment
2865 TODO: EFI_DEVICE_ERROR - add return value to function comment
2866 TODO: EFI_DEVICE_ERROR - add return value to function comment
2870 IN IDE_BLK_IO_DEV
*IdeDev
,
2871 IN VOID
*DataBuffer
,
2872 IN EFI_LBA StartLba
,
2873 IN UINTN NumberOfBlocks
2876 IDE_DMA_PRD
*PrdAddr
;
2877 IDE_DMA_PRD
*UsedPrdAddr
;
2878 IDE_DMA_PRD
*TempPrdAddr
;
2879 UINT8 RegisterValue
;
2881 UINT64 IoPortForBmic
;
2882 UINT64 IoPortForBmis
;
2883 UINT64 IoPortForBmid
;
2887 UINTN ByteAvailable
;
2889 UINTN RemainBlockNum
;
2890 UINT8 DeviceControl
;
2895 EFI_PHYSICAL_ADDRESS DeviceAddress
;
2898 // Channel and device differential
2900 Device
= (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0);
2903 // Enable interrupt to support UDMA and Select device
2906 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
2908 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
2910 if (IdePrimary
== IdeDev
->Channel
) {
2911 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICP_OFFSET
;
2912 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISP_OFFSET
;
2913 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDP_OFFSET
;
2915 if (IdeSecondary
== IdeDev
->Channel
) {
2916 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICS_OFFSET
;
2917 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISS_OFFSET
;
2918 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDS_OFFSET
;
2920 return EFI_UNSUPPORTED
;
2924 RemainBlockNum
= NumberOfBlocks
;
2925 while (RemainBlockNum
> 0) {
2927 if (RemainBlockNum
>= MAX_DMA_EXT_COMMAND_SECTORS
) {
2929 // SectorCount is used to record the number of sectors to be read
2930 // Max 65536 sectors can be transfered at a time.
2932 NumberOfBlocks
= MAX_DMA_EXT_COMMAND_SECTORS
;
2933 RemainBlockNum
-= MAX_DMA_EXT_COMMAND_SECTORS
;
2935 NumberOfBlocks
= (UINT16
) RemainBlockNum
;
2940 // Calculate the number of PRD table to make sure the memory region
2941 // not cross 64K boundary
2943 ByteCount
= NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
2944 PrdTableNum
= ((ByteCount
>> 16) + 1) + 1;
2949 PageCount
= EFI_SIZE_TO_PAGES (2 * PrdTableNum
* sizeof (IDE_DMA_PRD
));
2950 Status
= IdeDev
->PciIo
->AllocateBuffer (
2953 EfiBootServicesData
,
2958 if (EFI_ERROR (Status
)) {
2959 return EFI_OUT_OF_RESOURCES
;
2961 ZeroMem ((VOID
*) ((UINTN
) MemPage
), EFI_PAGES_TO_SIZE (PageCount
));
2963 PrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) MemPage
);
2965 // To make sure PRD is allocated in one 64K page
2967 if (((UINTN
) PrdAddr
& 0x0FFFF) > (((UINTN
) PrdAddr
+ PrdTableNum
* sizeof (IDE_DMA_PRD
) - 1) & 0x0FFFF)) {
2968 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x10000) & 0xFFFF0000);
2970 if ((UINTN
) PrdAddr
& 0x03) {
2971 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x04) & 0xFFFFFFFC);
2973 UsedPrdAddr
= PrdAddr
;
2978 // Build the PRD table
2980 Status
= IdeDev
->PciIo
->Map (
2982 EfiPciIoOperationBusMasterRead
,
2988 if (EFI_ERROR (Status
)) {
2989 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
2990 return EFI_OUT_OF_RESOURCES
;
2992 PrdBuffer
= (UINT8
*) ((UINTN
) DeviceAddress
);
2993 TempPrdAddr
= UsedPrdAddr
;
2996 ByteAvailable
= 0x10000 - ((UINTN
) PrdBuffer
& 0xFFFF);
2998 if (ByteCount
<= ByteAvailable
) {
2999 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
3000 TempPrdAddr
->ByteCount
= (UINT16
) ByteCount
;
3001 TempPrdAddr
->EndOfTable
= 0x8000;
3005 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
3006 TempPrdAddr
->ByteCount
= (UINT16
) ByteAvailable
;
3008 ByteCount
-= ByteAvailable
;
3009 PrdBuffer
+= ByteAvailable
;
3014 // Set the base address to BMID register
3016 IdeDev
->PciIo
->Io
.Write (
3018 EfiPciIoWidthUint32
,
3019 EFI_PCI_IO_PASS_THROUGH_BAR
,
3026 // Set BMIC register to identify the operation direction
3028 IdeDev
->PciIo
->Io
.Read (
3031 EFI_PCI_IO_PASS_THROUGH_BAR
,
3039 RegisterValue
&= ~((UINT8
) BMIC_nREAD
);
3041 IdeDev
->PciIo
->Io
.Write (
3044 EFI_PCI_IO_PASS_THROUGH_BAR
,
3051 // Read BMIS register and clear ERROR and INTR bit
3053 IdeDev
->PciIo
->Io
.Read (
3056 EFI_PCI_IO_PASS_THROUGH_BAR
,
3062 RegisterValue
|= (BMIS_INTERRUPT
| BMIS_ERROR
);
3064 IdeDev
->PciIo
->Io
.Write (
3067 EFI_PCI_IO_PASS_THROUGH_BAR
,
3074 // Issue WRITE DMA EXT command
3076 Status
= AtaCommandIssueExt (
3081 (UINT16
) NumberOfBlocks
,
3084 if (EFI_ERROR (Status
)) {
3085 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
3086 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3087 return EFI_DEVICE_ERROR
;
3091 // Set START bit of BMIC register
3093 IdeDev
->PciIo
->Io
.Read (
3096 EFI_PCI_IO_PASS_THROUGH_BAR
,
3102 RegisterValue
|= BMIC_START
;
3104 IdeDev
->PciIo
->Io
.Write (
3107 EFI_PCI_IO_PASS_THROUGH_BAR
,
3114 // Check the INTERRUPT and ERROR bit of BMIS
3115 // Max transfer number of sectors for one command is 65536(32Mbyte),
3116 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
3117 // So set the variable Count to 2000, for about 2 second timeout time.
3122 IdeDev
->PciIo
->Io
.Read (
3125 EFI_PCI_IO_PASS_THROUGH_BAR
,
3130 if ((RegisterValue
& (BMIS_INTERRUPT
| BMIS_ERROR
)) || (Count
== 0)) {
3131 if ((RegisterValue
& BMIS_ERROR
) || (Count
== 0)) {
3133 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
3135 IdeDev
->PciIo
->Io
.Read (
3138 EFI_PCI_IO_PASS_THROUGH_BAR
,
3144 RegisterValue
&= ~((UINT8
)BMIC_START
);
3146 IdeDev
->PciIo
->Io
.Write (
3149 EFI_PCI_IO_PASS_THROUGH_BAR
,
3154 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
3155 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3156 return EFI_DEVICE_ERROR
;
3165 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
3166 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3168 // Read Status Register of IDE device to clear interrupt
3170 RegisterValue
= IDEReadPortB(IdeDev
->PciIo
,IdeDev
->IoPort
->Reg
.Status
);
3172 // Clear START bit of BMIC register
3174 IdeDev
->PciIo
->Io
.Read (
3177 EFI_PCI_IO_PASS_THROUGH_BAR
,
3183 RegisterValue
&= ~((UINT8
) BMIC_START
);
3185 IdeDev
->PciIo
->Io
.Write (
3188 EFI_PCI_IO_PASS_THROUGH_BAR
,
3194 DataBuffer
= (UINT8
*) DataBuffer
+ NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
3195 StartLba
+= NumberOfBlocks
;
3199 // Disable interrupt of Select device
3201 IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
);
3202 DeviceControl
|= IEN_L
;
3203 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
3209 This function is called by the AtaBlkIoWriteBlocks() to perform
3210 writing to media in block unit. The function has been enhanced to
3211 support >120GB access and transfer at most 65536 blocks per command
3214 pointer pointing to IDE_BLK_IO_DEV data structure, used
3215 to record all the information of the IDE device.
3217 @param[in] *DataBuffer
3218 A pointer to the source buffer for the data.
3221 The starting logical block address to write to
3222 on the device media.
3224 @param[in] NumberOfBlocks
3225 The number of transfer data blocks.
3227 @return The device status of UDMA operation. If the operation is
3228 successful, return EFI_SUCCESS.
3230 TODO: EFI_UNSUPPORTED - add return value to function comment
3231 TODO: EFI_DEVICE_ERROR - add return value to function comment
3232 TODO: EFI_DEVICE_ERROR - add return value to function comment
3236 IN IDE_BLK_IO_DEV
*IdeDev
,
3237 IN VOID
*DataBuffer
,
3238 IN EFI_LBA StartLba
,
3239 IN UINTN NumberOfBlocks
3242 IDE_DMA_PRD
*PrdAddr
;
3243 IDE_DMA_PRD
*UsedPrdAddr
;
3244 IDE_DMA_PRD
*TempPrdAddr
;
3245 UINT8 RegisterValue
;
3247 UINT64 IoPortForBmic
;
3248 UINT64 IoPortForBmis
;
3249 UINT64 IoPortForBmid
;
3253 UINTN ByteAvailable
;
3255 UINTN RemainBlockNum
;
3256 UINT8 DeviceControl
;
3261 EFI_PHYSICAL_ADDRESS DeviceAddress
;
3264 // Channel and device differential
3266 Device
= (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0);
3269 // Enable interrupt to support UDMA
3272 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
3274 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
3276 if (IdePrimary
== IdeDev
->Channel
) {
3277 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICP_OFFSET
;
3278 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISP_OFFSET
;
3279 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDP_OFFSET
;
3281 if (IdeSecondary
== IdeDev
->Channel
) {
3282 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICS_OFFSET
;
3283 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISS_OFFSET
;
3284 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDS_OFFSET
;
3286 return EFI_UNSUPPORTED
;
3290 RemainBlockNum
= NumberOfBlocks
;
3291 while (RemainBlockNum
> 0) {
3293 if (RemainBlockNum
>= MAX_DMA_COMMAND_SECTORS
) {
3295 // SectorCount is used to record the number of sectors to be read
3296 // Max 256 sectors can be transfered at a time.
3298 NumberOfBlocks
= MAX_DMA_COMMAND_SECTORS
;
3299 RemainBlockNum
-= MAX_DMA_COMMAND_SECTORS
;
3301 NumberOfBlocks
= (UINT16
) RemainBlockNum
;
3306 // Calculate the number of PRD table to make sure the memory region
3307 // not cross 64K boundary
3309 ByteCount
= NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
3310 PrdTableNum
= ((ByteCount
>> 16) + 1) + 1;
3315 PageCount
= EFI_SIZE_TO_PAGES (2 * PrdTableNum
* sizeof (IDE_DMA_PRD
));
3316 Status
= IdeDev
->PciIo
->AllocateBuffer (
3319 EfiBootServicesData
,
3324 if (EFI_ERROR (Status
)) {
3325 return EFI_OUT_OF_RESOURCES
;
3327 ZeroMem ((VOID
*) ((UINTN
) MemPage
), EFI_PAGES_TO_SIZE (PageCount
));
3329 PrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) MemPage
);
3332 // To make sure PRD is allocated in one 64K page
3334 if (((UINTN
) PrdAddr
& 0x0FFFF) > (((UINTN
) PrdAddr
+ PrdTableNum
* sizeof (IDE_DMA_PRD
) - 1) & 0x0FFFF)) {
3335 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x10000) & 0xFFFF0000);
3337 if ((UINTN
) PrdAddr
& 0x03) {
3338 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x04) & 0xFFFFFFFC);
3340 UsedPrdAddr
= PrdAddr
;
3345 // Build the PRD table
3347 Status
= IdeDev
->PciIo
->Map (
3349 EfiPciIoOperationBusMasterRead
,
3355 if (EFI_ERROR (Status
)) {
3356 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
3357 return EFI_OUT_OF_RESOURCES
;
3359 PrdBuffer
= (UINT8
*) ((UINTN
) DeviceAddress
);
3360 TempPrdAddr
= UsedPrdAddr
;
3363 ByteAvailable
= 0x10000 - ((UINTN
) PrdBuffer
& 0xFFFF);
3365 if (ByteCount
<= ByteAvailable
) {
3366 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
3367 TempPrdAddr
->ByteCount
= (UINT16
) ByteCount
;
3368 TempPrdAddr
->EndOfTable
= 0x8000;
3372 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
3373 TempPrdAddr
->ByteCount
= (UINT16
) ByteAvailable
;
3375 ByteCount
-= ByteAvailable
;
3376 PrdBuffer
+= ByteAvailable
;
3381 // Set the base address to BMID register
3383 IdeDev
->PciIo
->Io
.Write (
3385 EfiPciIoWidthUint32
,
3386 EFI_PCI_IO_PASS_THROUGH_BAR
,
3393 // Set BMIC register to identify the operation direction
3395 IdeDev
->PciIo
->Io
.Read (
3398 EFI_PCI_IO_PASS_THROUGH_BAR
,
3406 RegisterValue
&= ~((UINT8
) BMIC_nREAD
);
3408 IdeDev
->PciIo
->Io
.Write (
3411 EFI_PCI_IO_PASS_THROUGH_BAR
,
3418 // Read BMIS register and clear ERROR and INTR bit
3420 IdeDev
->PciIo
->Io
.Read (
3423 EFI_PCI_IO_PASS_THROUGH_BAR
,
3429 RegisterValue
|= (BMIS_INTERRUPT
| BMIS_ERROR
);
3431 IdeDev
->PciIo
->Io
.Write (
3434 EFI_PCI_IO_PASS_THROUGH_BAR
,
3441 // Issue WRITE DMA command
3443 Status
= AtaCommandIssue (
3448 (UINT16
) NumberOfBlocks
,
3451 if (EFI_ERROR (Status
)) {
3452 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
3453 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3454 return EFI_DEVICE_ERROR
;
3458 // Set START bit of BMIC register
3460 IdeDev
->PciIo
->Io
.Read (
3463 EFI_PCI_IO_PASS_THROUGH_BAR
,
3469 RegisterValue
|= BMIC_START
;
3471 IdeDev
->PciIo
->Io
.Write (
3474 EFI_PCI_IO_PASS_THROUGH_BAR
,
3481 // Check the INTERRUPT and ERROR bit of BMIS
3482 // Max transfer number of sectors for one command is 65536(32Mbyte),
3483 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
3484 // So set the variable Count to 2000, for about 2 second timeout time.
3489 IdeDev
->PciIo
->Io
.Read (
3492 EFI_PCI_IO_PASS_THROUGH_BAR
,
3497 if ((RegisterValue
& (BMIS_INTERRUPT
| BMIS_ERROR
)) || (Count
== 0)) {
3498 if ((RegisterValue
& BMIS_ERROR
) || (Count
== 0)) {
3500 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
3502 IdeDev
->PciIo
->Io
.Read (
3505 EFI_PCI_IO_PASS_THROUGH_BAR
,
3511 RegisterValue
&= ~((UINT8
)BMIC_START
);
3513 IdeDev
->PciIo
->Io
.Write (
3516 EFI_PCI_IO_PASS_THROUGH_BAR
,
3521 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
3522 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3523 return EFI_DEVICE_ERROR
;
3532 IdeDev
->PciIo
->FreeBuffer (IdeDev
->PciIo
, PageCount
, MemPage
);
3533 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3536 // Read Status Register of IDE device to clear interrupt
3538 RegisterValue
= IDEReadPortB(IdeDev
->PciIo
,IdeDev
->IoPort
->Reg
.Status
);
3540 // Clear START bit of BMIC register
3542 IdeDev
->PciIo
->Io
.Read (
3545 EFI_PCI_IO_PASS_THROUGH_BAR
,
3551 RegisterValue
&= ~((UINT8
) BMIC_START
);
3553 IdeDev
->PciIo
->Io
.Write (
3556 EFI_PCI_IO_PASS_THROUGH_BAR
,
3562 DataBuffer
= (UINT8
*) DataBuffer
+ NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
3563 StartLba
+= NumberOfBlocks
;
3567 // Disable interrupt of Select device
3569 IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
);
3570 DeviceControl
|= IEN_L
;
3571 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);