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
;
2165 EFI_PHYSICAL_ADDRESS MemPage
;
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 MemPage
= 0xFFFFFFFF;
2221 PageCount
= EFI_SIZE_TO_PAGES (2 * PrdTableNum
* sizeof (IDE_DMA_PRD
));
2222 Status
= gBS
->AllocatePages (
2224 EfiBootServicesData
,
2228 if (EFI_ERROR (Status
)) {
2229 return EFI_OUT_OF_RESOURCES
;
2231 ZeroMem ((VOID
*) ((UINTN
) MemPage
), EFI_PAGES_TO_SIZE (PageCount
));
2233 PrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) MemPage
);
2236 // To make sure PRD is allocated in one 64K page
2238 if (((UINTN
) PrdAddr
& 0x0FFFF) > (((UINTN
) PrdAddr
+ PrdTableNum
* sizeof (IDE_DMA_PRD
) - 1) & 0x0FFFF)) {
2239 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x10000) & 0xFFFF0000);
2241 if ((UINTN
) PrdAddr
& 0x03) {
2242 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x04) & 0xFFFFFFFC);
2244 UsedPrdAddr
= PrdAddr
;
2249 // Build the PRD table
2251 Status
= IdeDev
->PciIo
->Map (
2253 EfiPciIoOperationBusMasterWrite
,
2259 if (EFI_ERROR (Status
)) {
2260 gBS
->FreePages (MemPage
, PageCount
);
2261 return EFI_OUT_OF_RESOURCES
;
2263 PrdBuffer
= (VOID
*) ((UINTN
) DeviceAddress
);
2264 TempPrdAddr
= UsedPrdAddr
;
2267 ByteAvailable
= 0x10000 - ((UINTN
) PrdBuffer
& 0xFFFF);
2269 if (ByteCount
<= ByteAvailable
) {
2270 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
2271 TempPrdAddr
->ByteCount
= (UINT16
) ByteCount
;
2272 TempPrdAddr
->EndOfTable
= 0x8000;
2276 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
2277 TempPrdAddr
->ByteCount
= (UINT16
) ByteAvailable
;
2279 ByteCount
-= ByteAvailable
;
2280 PrdBuffer
+= ByteAvailable
;
2285 // Set the base address to BMID register
2287 IdeDev
->PciIo
->Io
.Write (
2289 EfiPciIoWidthUint32
,
2290 EFI_PCI_IO_PASS_THROUGH_BAR
,
2297 // Set BMIC register to identify the operation direction
2299 IdeDev
->PciIo
->Io
.Read (
2302 EFI_PCI_IO_PASS_THROUGH_BAR
,
2308 RegisterValue
|= BMIC_nREAD
;
2310 IdeDev
->PciIo
->Io
.Write (
2313 EFI_PCI_IO_PASS_THROUGH_BAR
,
2320 // Read BMIS register and clear ERROR and INTR bit
2322 IdeDev
->PciIo
->Io
.Read (
2325 EFI_PCI_IO_PASS_THROUGH_BAR
,
2331 RegisterValue
|= BMIS_INTERRUPT
| BMIS_ERROR
;
2333 IdeDev
->PciIo
->Io
.Write (
2336 EFI_PCI_IO_PASS_THROUGH_BAR
,
2343 // Issue READ DMA EXT command
2345 Status
= AtaCommandIssueExt (
2350 (UINT16
) NumberOfBlocks
,
2353 if (EFI_ERROR (Status
)) {
2354 gBS
->FreePages (MemPage
, PageCount
);
2355 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2356 return EFI_DEVICE_ERROR
;
2360 // Set START bit of BMIC register
2362 IdeDev
->PciIo
->Io
.Read (
2365 EFI_PCI_IO_PASS_THROUGH_BAR
,
2371 RegisterValue
|= BMIC_START
;
2373 IdeDev
->PciIo
->Io
.Write (
2376 EFI_PCI_IO_PASS_THROUGH_BAR
,
2383 // Check the INTERRUPT and ERROR bit of BMIS
2384 // Max transfer number of sectors for one command is 65536(32Mbyte),
2385 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
2386 // So set the variable Count to 2000, for about 2 second timeout time.
2391 IdeDev
->PciIo
->Io
.Read (
2394 EFI_PCI_IO_PASS_THROUGH_BAR
,
2399 if ((RegisterValue
& (BMIS_INTERRUPT
| BMIS_ERROR
)) || (Count
== 0)) {
2400 if ((RegisterValue
& BMIS_ERROR
) || (Count
== 0)) {
2402 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
2404 IdeDev
->PciIo
->Io
.Read (
2407 EFI_PCI_IO_PASS_THROUGH_BAR
,
2413 RegisterValue
&= ~((UINT8
)BMIC_START
);
2415 IdeDev
->PciIo
->Io
.Write (
2418 EFI_PCI_IO_PASS_THROUGH_BAR
,
2423 gBS
->FreePages (MemPage
, PageCount
);
2424 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2425 return EFI_DEVICE_ERROR
;
2434 gBS
->FreePages (MemPage
, PageCount
);
2435 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2437 // Read Status Register of IDE device to clear interrupt
2439 RegisterValue
= IDEReadPortB(IdeDev
->PciIo
,IdeDev
->IoPort
->Reg
.Status
);
2441 // Clear START bit of BMIC register
2443 IdeDev
->PciIo
->Io
.Read (
2446 EFI_PCI_IO_PASS_THROUGH_BAR
,
2452 RegisterValue
&= ~((UINT8
) BMIC_START
);
2454 IdeDev
->PciIo
->Io
.Write (
2457 EFI_PCI_IO_PASS_THROUGH_BAR
,
2463 if (RegisterValue
& BMIS_ERROR
) {
2464 return EFI_DEVICE_ERROR
;
2467 DataBuffer
= (UINT8
*) DataBuffer
+ NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
2468 StartLba
+= NumberOfBlocks
;
2472 // Disable interrupt of Select device
2474 IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
);
2475 DeviceControl
|= IEN_L
;
2476 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
2482 This function is called by the AtaBlkIoReadBlocks() to perform
2483 reading from media in block unit. The function has been enhanced to
2484 support >120GB access and transfer at most 65536 blocks per command
2487 pointer pointing to IDE_BLK_IO_DEV data structure, used
2488 to record all the information of the IDE device.
2490 @param[in] *DataBuffer A pointer to the destination buffer for the data.
2491 @param[in] StartLba The starting logical block address to read from
2492 on the device media.
2493 @param[in] NumberOfBlocks The number of transfer data blocks.
2495 @return The device status of UDMA operation. If the operation is
2496 successful, return EFI_SUCCESS.
2498 TODO: EFI_UNSUPPORTED - add return value to function comment
2499 TODO: EFI_DEVICE_ERROR - 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
2505 IN IDE_BLK_IO_DEV
*IdeDev
,
2506 IN VOID
*DataBuffer
,
2507 IN EFI_LBA StartLba
,
2508 IN UINTN NumberOfBlocks
2511 IDE_DMA_PRD
*PrdAddr
;
2512 IDE_DMA_PRD
*UsedPrdAddr
;
2513 IDE_DMA_PRD
*TempPrdAddr
;
2514 UINT8 RegisterValue
;
2516 UINT64 IoPortForBmic
;
2517 UINT64 IoPortForBmis
;
2518 UINT64 IoPortForBmid
;
2522 UINTN ByteAvailable
;
2524 UINTN RemainBlockNum
;
2525 UINT8 DeviceControl
;
2529 EFI_PHYSICAL_ADDRESS MemPage
;
2530 EFI_PHYSICAL_ADDRESS DeviceAddress
;
2533 // Channel and device differential
2535 Device
= (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0);
2538 // Enable interrupt to support UDMA and Select device
2541 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
2543 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
2545 if (IdePrimary
== IdeDev
->Channel
) {
2546 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICP_OFFSET
;
2547 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISP_OFFSET
;
2548 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDP_OFFSET
;
2550 if (IdeSecondary
== IdeDev
->Channel
) {
2551 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICS_OFFSET
;
2552 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISS_OFFSET
;
2553 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDS_OFFSET
;
2555 return EFI_UNSUPPORTED
;
2559 RemainBlockNum
= NumberOfBlocks
;
2560 while (RemainBlockNum
> 0) {
2562 if (RemainBlockNum
>= MAX_DMA_COMMAND_SECTORS
) {
2564 // SectorCount is used to record the number of sectors to be read
2565 // Max 256 sectors can be transfered at a time.
2567 NumberOfBlocks
= MAX_DMA_COMMAND_SECTORS
;
2568 RemainBlockNum
-= MAX_DMA_COMMAND_SECTORS
;
2570 NumberOfBlocks
= (UINT16
) RemainBlockNum
;
2575 // Calculate the number of PRD table to make sure the memory region
2576 // not cross 64K boundary
2578 ByteCount
= NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
2579 PrdTableNum
= ((ByteCount
>> 16) + 1) + 1;
2584 MemPage
= 0xFFFFFFFF;
2585 PageCount
= EFI_SIZE_TO_PAGES (2 * PrdTableNum
* sizeof (IDE_DMA_PRD
));
2586 Status
= gBS
->AllocatePages (
2588 EfiBootServicesData
,
2592 if (EFI_ERROR (Status
)) {
2593 return EFI_OUT_OF_RESOURCES
;
2595 ZeroMem ((VOID
*) ((UINTN
) MemPage
), EFI_PAGES_TO_SIZE (PageCount
));
2597 PrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) MemPage
);
2599 // To make sure PRD is allocated in one 64K page
2601 if (((UINTN
) PrdAddr
& 0x0FFFF) > (((UINTN
) PrdAddr
+ PrdTableNum
* sizeof (IDE_DMA_PRD
) - 1) & 0x0FFFF)) {
2602 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x10000) & 0xFFFF0000);
2604 if ((UINTN
) PrdAddr
& 0x03) {
2605 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x04) & 0xFFFFFFFC);
2607 UsedPrdAddr
= PrdAddr
;
2612 // Build the PRD table
2614 Status
= IdeDev
->PciIo
->Map (
2616 EfiPciIoOperationBusMasterWrite
,
2622 if (EFI_ERROR (Status
)) {
2623 gBS
->FreePages (MemPage
, PageCount
);
2624 return EFI_OUT_OF_RESOURCES
;
2626 PrdBuffer
= (UINT8
*) ((UINTN
) DeviceAddress
);
2627 TempPrdAddr
= UsedPrdAddr
;
2630 ByteAvailable
= 0x10000 - ((UINTN
) PrdBuffer
& 0xFFFF);
2632 if (ByteCount
<= ByteAvailable
) {
2633 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
2634 TempPrdAddr
->ByteCount
= (UINT16
) ByteCount
;
2635 TempPrdAddr
->EndOfTable
= 0x8000;
2639 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
2640 TempPrdAddr
->ByteCount
= (UINT16
) ByteAvailable
;
2642 ByteCount
-= ByteAvailable
;
2643 PrdBuffer
+= ByteAvailable
;
2648 // Set the base address to BMID register
2650 IdeDev
->PciIo
->Io
.Write (
2652 EfiPciIoWidthUint32
,
2653 EFI_PCI_IO_PASS_THROUGH_BAR
,
2660 // Set BMIC register to identify the operation direction
2662 IdeDev
->PciIo
->Io
.Read (
2665 EFI_PCI_IO_PASS_THROUGH_BAR
,
2671 RegisterValue
|= BMIC_nREAD
;
2673 IdeDev
->PciIo
->Io
.Write (
2676 EFI_PCI_IO_PASS_THROUGH_BAR
,
2683 // Read BMIS register and clear ERROR and INTR bit
2685 IdeDev
->PciIo
->Io
.Read (
2688 EFI_PCI_IO_PASS_THROUGH_BAR
,
2694 RegisterValue
|= (BMIS_INTERRUPT
| BMIS_ERROR
);
2696 IdeDev
->PciIo
->Io
.Write (
2699 EFI_PCI_IO_PASS_THROUGH_BAR
,
2706 // Issue READ DMA command
2708 Status
= AtaCommandIssue (
2713 (UINT16
) NumberOfBlocks
,
2716 if (EFI_ERROR (Status
)) {
2717 gBS
->FreePages (MemPage
, PageCount
);
2718 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2719 return EFI_DEVICE_ERROR
;
2723 // Set START bit of BMIC register
2725 IdeDev
->PciIo
->Io
.Read (
2728 EFI_PCI_IO_PASS_THROUGH_BAR
,
2734 RegisterValue
|= BMIC_START
;
2736 IdeDev
->PciIo
->Io
.Write (
2739 EFI_PCI_IO_PASS_THROUGH_BAR
,
2746 // Check the INTERRUPT and ERROR bit of BMIS
2747 // Max transfer number of sectors for one command is 65536(32Mbyte),
2748 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
2749 // So set the variable Count to 2000, for about 2 second timeout time.
2754 IdeDev
->PciIo
->Io
.Read (
2757 EFI_PCI_IO_PASS_THROUGH_BAR
,
2762 if ((RegisterValue
& (BMIS_INTERRUPT
| BMIS_ERROR
)) || (Count
== 0)) {
2763 if ((RegisterValue
& BMIS_ERROR
) || (Count
== 0)) {
2765 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
2767 IdeDev
->PciIo
->Io
.Read (
2770 EFI_PCI_IO_PASS_THROUGH_BAR
,
2776 RegisterValue
&= ~((UINT8
)BMIC_START
);
2778 IdeDev
->PciIo
->Io
.Write (
2781 EFI_PCI_IO_PASS_THROUGH_BAR
,
2786 gBS
->FreePages (MemPage
, PageCount
);
2787 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2788 return EFI_DEVICE_ERROR
;
2797 gBS
->FreePages (MemPage
, PageCount
);
2798 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
2800 // Read Status Register of IDE device to clear interrupt
2802 RegisterValue
= IDEReadPortB(IdeDev
->PciIo
,IdeDev
->IoPort
->Reg
.Status
);
2804 // Clear START bit of BMIC register
2806 IdeDev
->PciIo
->Io
.Read (
2809 EFI_PCI_IO_PASS_THROUGH_BAR
,
2815 RegisterValue
&= ~((UINT8
) BMIC_START
);
2817 IdeDev
->PciIo
->Io
.Write (
2820 EFI_PCI_IO_PASS_THROUGH_BAR
,
2826 if (RegisterValue
& BMIS_ERROR
) {
2827 return EFI_DEVICE_ERROR
;
2830 DataBuffer
= (UINT8
*) DataBuffer
+ NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
2831 StartLba
+= NumberOfBlocks
;
2835 // Disable interrupt of Select device
2837 IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
);
2838 DeviceControl
|= IEN_L
;
2839 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
2845 This function is called by the AtaBlkIoWriteBlocks() to perform
2846 writing to media in block unit. The function has been enhanced to
2847 support >120GB access and transfer at most 65536 blocks per command
2849 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2850 to record all the information of the IDE device.
2852 @param[in] *DataBuffer A pointer to the source buffer for the data.
2854 @param[in] StartLba The starting logical block address to write to
2855 on the device media.
2857 @param[in] NumberOfBlocks The number of transfer data blocks.
2859 @return The device status of UDMA operation. If the operation is
2860 successful, return EFI_SUCCESS.
2862 TODO: EFI_UNSUPPORTED - add return value to function comment
2863 TODO: EFI_DEVICE_ERROR - add return value to function comment
2864 TODO: EFI_DEVICE_ERROR - add return value to function comment
2868 IN IDE_BLK_IO_DEV
*IdeDev
,
2869 IN VOID
*DataBuffer
,
2870 IN EFI_LBA StartLba
,
2871 IN UINTN NumberOfBlocks
2874 IDE_DMA_PRD
*PrdAddr
;
2875 IDE_DMA_PRD
*UsedPrdAddr
;
2876 IDE_DMA_PRD
*TempPrdAddr
;
2877 UINT8 RegisterValue
;
2879 UINT64 IoPortForBmic
;
2880 UINT64 IoPortForBmis
;
2881 UINT64 IoPortForBmid
;
2885 UINTN ByteAvailable
;
2887 UINTN RemainBlockNum
;
2888 UINT8 DeviceControl
;
2892 EFI_PHYSICAL_ADDRESS MemPage
;
2893 EFI_PHYSICAL_ADDRESS DeviceAddress
;
2896 // Channel and device differential
2898 Device
= (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0);
2901 // Enable interrupt to support UDMA and Select device
2904 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
2906 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
2908 if (IdePrimary
== IdeDev
->Channel
) {
2909 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICP_OFFSET
;
2910 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISP_OFFSET
;
2911 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDP_OFFSET
;
2913 if (IdeSecondary
== IdeDev
->Channel
) {
2914 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICS_OFFSET
;
2915 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISS_OFFSET
;
2916 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDS_OFFSET
;
2918 return EFI_UNSUPPORTED
;
2922 RemainBlockNum
= NumberOfBlocks
;
2923 while (RemainBlockNum
> 0) {
2925 if (RemainBlockNum
>= MAX_DMA_EXT_COMMAND_SECTORS
) {
2927 // SectorCount is used to record the number of sectors to be read
2928 // Max 65536 sectors can be transfered at a time.
2930 NumberOfBlocks
= MAX_DMA_EXT_COMMAND_SECTORS
;
2931 RemainBlockNum
-= MAX_DMA_EXT_COMMAND_SECTORS
;
2933 NumberOfBlocks
= (UINT16
) RemainBlockNum
;
2938 // Calculate the number of PRD table to make sure the memory region
2939 // not cross 64K boundary
2941 ByteCount
= NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
2942 PrdTableNum
= ((ByteCount
>> 16) + 1) + 1;
2947 MemPage
= 0xFFFFFFFF;
2948 PageCount
= EFI_SIZE_TO_PAGES (2 * PrdTableNum
* sizeof (IDE_DMA_PRD
));
2949 Status
= gBS
->AllocatePages (
2951 EfiBootServicesData
,
2955 if (EFI_ERROR (Status
)) {
2956 return EFI_OUT_OF_RESOURCES
;
2958 ZeroMem ((VOID
*) ((UINTN
) MemPage
), EFI_PAGES_TO_SIZE (PageCount
));
2960 PrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) MemPage
);
2962 // To make sure PRD is allocated in one 64K page
2964 if (((UINTN
) PrdAddr
& 0x0FFFF) > (((UINTN
) PrdAddr
+ PrdTableNum
* sizeof (IDE_DMA_PRD
) - 1) & 0x0FFFF)) {
2965 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x10000) & 0xFFFF0000);
2967 if ((UINTN
) PrdAddr
& 0x03) {
2968 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x04) & 0xFFFFFFFC);
2970 UsedPrdAddr
= PrdAddr
;
2975 // Build the PRD table
2977 Status
= IdeDev
->PciIo
->Map (
2979 EfiPciIoOperationBusMasterRead
,
2985 if (EFI_ERROR (Status
)) {
2986 gBS
->FreePages (MemPage
, PageCount
);
2987 return EFI_OUT_OF_RESOURCES
;
2989 PrdBuffer
= (UINT8
*) ((UINTN
) DeviceAddress
);
2990 TempPrdAddr
= UsedPrdAddr
;
2993 ByteAvailable
= 0x10000 - ((UINTN
) PrdBuffer
& 0xFFFF);
2995 if (ByteCount
<= ByteAvailable
) {
2996 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
2997 TempPrdAddr
->ByteCount
= (UINT16
) ByteCount
;
2998 TempPrdAddr
->EndOfTable
= 0x8000;
3002 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
3003 TempPrdAddr
->ByteCount
= (UINT16
) ByteAvailable
;
3005 ByteCount
-= ByteAvailable
;
3006 PrdBuffer
+= ByteAvailable
;
3011 // Set the base address to BMID register
3013 IdeDev
->PciIo
->Io
.Write (
3015 EfiPciIoWidthUint32
,
3016 EFI_PCI_IO_PASS_THROUGH_BAR
,
3023 // Set BMIC register to identify the operation direction
3025 IdeDev
->PciIo
->Io
.Read (
3028 EFI_PCI_IO_PASS_THROUGH_BAR
,
3036 RegisterValue
&= ~((UINT8
) BMIC_nREAD
);
3038 IdeDev
->PciIo
->Io
.Write (
3041 EFI_PCI_IO_PASS_THROUGH_BAR
,
3048 // Read BMIS register and clear ERROR and INTR bit
3050 IdeDev
->PciIo
->Io
.Read (
3053 EFI_PCI_IO_PASS_THROUGH_BAR
,
3059 RegisterValue
|= (BMIS_INTERRUPT
| BMIS_ERROR
);
3061 IdeDev
->PciIo
->Io
.Write (
3064 EFI_PCI_IO_PASS_THROUGH_BAR
,
3071 // Issue WRITE DMA EXT command
3073 Status
= AtaCommandIssueExt (
3078 (UINT16
) NumberOfBlocks
,
3081 if (EFI_ERROR (Status
)) {
3082 gBS
->FreePages (MemPage
, PageCount
);
3083 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3084 return EFI_DEVICE_ERROR
;
3088 // Set START bit of BMIC register
3090 IdeDev
->PciIo
->Io
.Read (
3093 EFI_PCI_IO_PASS_THROUGH_BAR
,
3099 RegisterValue
|= BMIC_START
;
3101 IdeDev
->PciIo
->Io
.Write (
3104 EFI_PCI_IO_PASS_THROUGH_BAR
,
3111 // Check the INTERRUPT and ERROR bit of BMIS
3112 // Max transfer number of sectors for one command is 65536(32Mbyte),
3113 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
3114 // So set the variable Count to 2000, for about 2 second timeout time.
3119 IdeDev
->PciIo
->Io
.Read (
3122 EFI_PCI_IO_PASS_THROUGH_BAR
,
3127 if ((RegisterValue
& (BMIS_INTERRUPT
| BMIS_ERROR
)) || (Count
== 0)) {
3128 if ((RegisterValue
& BMIS_ERROR
) || (Count
== 0)) {
3130 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
3132 IdeDev
->PciIo
->Io
.Read (
3135 EFI_PCI_IO_PASS_THROUGH_BAR
,
3141 RegisterValue
&= ~((UINT8
)BMIC_START
);
3143 IdeDev
->PciIo
->Io
.Write (
3146 EFI_PCI_IO_PASS_THROUGH_BAR
,
3151 gBS
->FreePages (MemPage
, PageCount
);
3152 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3153 return EFI_DEVICE_ERROR
;
3162 gBS
->FreePages (MemPage
, PageCount
);
3163 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3165 // Read Status Register of IDE device to clear interrupt
3167 RegisterValue
= IDEReadPortB(IdeDev
->PciIo
,IdeDev
->IoPort
->Reg
.Status
);
3169 // Clear START bit of BMIC register
3171 IdeDev
->PciIo
->Io
.Read (
3174 EFI_PCI_IO_PASS_THROUGH_BAR
,
3180 RegisterValue
&= ~((UINT8
) BMIC_START
);
3182 IdeDev
->PciIo
->Io
.Write (
3185 EFI_PCI_IO_PASS_THROUGH_BAR
,
3191 DataBuffer
= (UINT8
*) DataBuffer
+ NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
3192 StartLba
+= NumberOfBlocks
;
3196 // Disable interrupt of Select device
3198 IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
);
3199 DeviceControl
|= IEN_L
;
3200 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
3206 This function is called by the AtaBlkIoWriteBlocks() to perform
3207 writing to media in block unit. The function has been enhanced to
3208 support >120GB access and transfer at most 65536 blocks per command
3211 pointer pointing to IDE_BLK_IO_DEV data structure, used
3212 to record all the information of the IDE device.
3214 @param[in] *DataBuffer
3215 A pointer to the source buffer for the data.
3218 The starting logical block address to write to
3219 on the device media.
3221 @param[in] NumberOfBlocks
3222 The number of transfer data blocks.
3224 @return The device status of UDMA operation. If the operation is
3225 successful, return EFI_SUCCESS.
3227 TODO: EFI_UNSUPPORTED - add return value to function comment
3228 TODO: EFI_DEVICE_ERROR - add return value to function comment
3229 TODO: EFI_DEVICE_ERROR - add return value to function comment
3233 IN IDE_BLK_IO_DEV
*IdeDev
,
3234 IN VOID
*DataBuffer
,
3235 IN EFI_LBA StartLba
,
3236 IN UINTN NumberOfBlocks
3239 IDE_DMA_PRD
*PrdAddr
;
3240 IDE_DMA_PRD
*UsedPrdAddr
;
3241 IDE_DMA_PRD
*TempPrdAddr
;
3242 UINT8 RegisterValue
;
3244 UINT64 IoPortForBmic
;
3245 UINT64 IoPortForBmis
;
3246 UINT64 IoPortForBmid
;
3250 UINTN ByteAvailable
;
3252 UINTN RemainBlockNum
;
3253 UINT8 DeviceControl
;
3257 EFI_PHYSICAL_ADDRESS MemPage
;
3258 EFI_PHYSICAL_ADDRESS DeviceAddress
;
3261 // Channel and device differential
3263 Device
= (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0);
3266 // Enable interrupt to support UDMA
3269 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);
3271 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
3273 if (IdePrimary
== IdeDev
->Channel
) {
3274 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICP_OFFSET
;
3275 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISP_OFFSET
;
3276 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDP_OFFSET
;
3278 if (IdeSecondary
== IdeDev
->Channel
) {
3279 IoPortForBmic
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMICS_OFFSET
;
3280 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISS_OFFSET
;
3281 IoPortForBmid
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMIDS_OFFSET
;
3283 return EFI_UNSUPPORTED
;
3287 RemainBlockNum
= NumberOfBlocks
;
3288 while (RemainBlockNum
> 0) {
3290 if (RemainBlockNum
>= MAX_DMA_COMMAND_SECTORS
) {
3292 // SectorCount is used to record the number of sectors to be read
3293 // Max 256 sectors can be transfered at a time.
3295 NumberOfBlocks
= MAX_DMA_COMMAND_SECTORS
;
3296 RemainBlockNum
-= MAX_DMA_COMMAND_SECTORS
;
3298 NumberOfBlocks
= (UINT16
) RemainBlockNum
;
3303 // Calculate the number of PRD table to make sure the memory region
3304 // not cross 64K boundary
3306 ByteCount
= NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
3307 PrdTableNum
= ((ByteCount
>> 16) + 1) + 1;
3312 MemPage
= 0xFFFFFFFF;
3313 PageCount
= EFI_SIZE_TO_PAGES (2 * PrdTableNum
* sizeof (IDE_DMA_PRD
));
3314 Status
= gBS
->AllocatePages (
3316 EfiBootServicesData
,
3320 if (EFI_ERROR (Status
)) {
3321 return EFI_OUT_OF_RESOURCES
;
3323 ZeroMem ((VOID
*) ((UINTN
) MemPage
), EFI_PAGES_TO_SIZE (PageCount
));
3325 PrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) MemPage
);
3328 // To make sure PRD is allocated in one 64K page
3330 if (((UINTN
) PrdAddr
& 0x0FFFF) > (((UINTN
) PrdAddr
+ PrdTableNum
* sizeof (IDE_DMA_PRD
) - 1) & 0x0FFFF)) {
3331 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x10000) & 0xFFFF0000);
3333 if ((UINTN
) PrdAddr
& 0x03) {
3334 UsedPrdAddr
= (IDE_DMA_PRD
*) ((UINTN
) ((UINT8
*) PrdAddr
+ 0x04) & 0xFFFFFFFC);
3336 UsedPrdAddr
= PrdAddr
;
3341 // Build the PRD table
3343 Status
= IdeDev
->PciIo
->Map (
3345 EfiPciIoOperationBusMasterRead
,
3351 if (EFI_ERROR (Status
)) {
3352 gBS
->FreePages (MemPage
, PageCount
);
3353 return EFI_OUT_OF_RESOURCES
;
3355 PrdBuffer
= (UINT8
*) ((UINTN
) DeviceAddress
);
3356 TempPrdAddr
= UsedPrdAddr
;
3359 ByteAvailable
= 0x10000 - ((UINTN
) PrdBuffer
& 0xFFFF);
3361 if (ByteCount
<= ByteAvailable
) {
3362 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
3363 TempPrdAddr
->ByteCount
= (UINT16
) ByteCount
;
3364 TempPrdAddr
->EndOfTable
= 0x8000;
3368 TempPrdAddr
->RegionBaseAddr
= (UINT32
) ((UINTN
) PrdBuffer
);
3369 TempPrdAddr
->ByteCount
= (UINT16
) ByteAvailable
;
3371 ByteCount
-= ByteAvailable
;
3372 PrdBuffer
+= ByteAvailable
;
3377 // Set the base address to BMID register
3379 IdeDev
->PciIo
->Io
.Write (
3381 EfiPciIoWidthUint32
,
3382 EFI_PCI_IO_PASS_THROUGH_BAR
,
3389 // Set BMIC register to identify the operation direction
3391 IdeDev
->PciIo
->Io
.Read (
3394 EFI_PCI_IO_PASS_THROUGH_BAR
,
3402 RegisterValue
&= ~((UINT8
) BMIC_nREAD
);
3404 IdeDev
->PciIo
->Io
.Write (
3407 EFI_PCI_IO_PASS_THROUGH_BAR
,
3414 // Read BMIS register and clear ERROR and INTR bit
3416 IdeDev
->PciIo
->Io
.Read (
3419 EFI_PCI_IO_PASS_THROUGH_BAR
,
3425 RegisterValue
|= (BMIS_INTERRUPT
| BMIS_ERROR
);
3427 IdeDev
->PciIo
->Io
.Write (
3430 EFI_PCI_IO_PASS_THROUGH_BAR
,
3437 // Issue WRITE DMA command
3439 Status
= AtaCommandIssue (
3444 (UINT16
) NumberOfBlocks
,
3447 if (EFI_ERROR (Status
)) {
3448 gBS
->FreePages (MemPage
, PageCount
);
3449 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3450 return EFI_DEVICE_ERROR
;
3454 // Set START bit of BMIC register
3456 IdeDev
->PciIo
->Io
.Read (
3459 EFI_PCI_IO_PASS_THROUGH_BAR
,
3465 RegisterValue
|= BMIC_START
;
3467 IdeDev
->PciIo
->Io
.Write (
3470 EFI_PCI_IO_PASS_THROUGH_BAR
,
3477 // Check the INTERRUPT and ERROR bit of BMIS
3478 // Max transfer number of sectors for one command is 65536(32Mbyte),
3479 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
3480 // So set the variable Count to 2000, for about 2 second timeout time.
3485 IdeDev
->PciIo
->Io
.Read (
3488 EFI_PCI_IO_PASS_THROUGH_BAR
,
3493 if ((RegisterValue
& (BMIS_INTERRUPT
| BMIS_ERROR
)) || (Count
== 0)) {
3494 if ((RegisterValue
& BMIS_ERROR
) || (Count
== 0)) {
3496 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
3498 IdeDev
->PciIo
->Io
.Read (
3501 EFI_PCI_IO_PASS_THROUGH_BAR
,
3507 RegisterValue
&= ~((UINT8
)BMIC_START
);
3509 IdeDev
->PciIo
->Io
.Write (
3512 EFI_PCI_IO_PASS_THROUGH_BAR
,
3517 gBS
->FreePages (MemPage
, PageCount
);
3518 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3519 return EFI_DEVICE_ERROR
;
3528 gBS
->FreePages (MemPage
, PageCount
);
3529 IdeDev
->PciIo
->Unmap (IdeDev
->PciIo
, Map
);
3532 // Read Status Register of IDE device to clear interrupt
3534 RegisterValue
= IDEReadPortB(IdeDev
->PciIo
,IdeDev
->IoPort
->Reg
.Status
);
3536 // Clear START bit of BMIC register
3538 IdeDev
->PciIo
->Io
.Read (
3541 EFI_PCI_IO_PASS_THROUGH_BAR
,
3547 RegisterValue
&= ~((UINT8
) BMIC_START
);
3549 IdeDev
->PciIo
->Io
.Write (
3552 EFI_PCI_IO_PASS_THROUGH_BAR
,
3558 DataBuffer
= (UINT8
*) DataBuffer
+ NumberOfBlocks
* IdeDev
->BlkIo
.Media
->BlockSize
;
3559 StartLba
+= NumberOfBlocks
;
3563 // Disable interrupt of Select device
3565 IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
);
3566 DeviceControl
|= IEN_L
;
3567 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);