2 Copyright (c) 2006 - 2008, 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.
16 This function is used to get the current status of the media residing
17 in the LS-120 drive or ZIP drive. The media status is returned in the
21 pointer pointing to IDE_BLK_IO_DEV data structure, used
22 to record all the information of the IDE device.
25 The media status is achieved successfully and the media
28 @retval EFI_DEVICE_ERROR
29 Get Media Status Command is failed.
32 There is no media in the drive.
34 @retval EFI_WRITE_PROTECTED
35 The media is writing protected.
38 This function must be called after the LS120EnableMediaStatus()
39 with second parameter set to TRUE
40 (means enable media status notification) is called.
46 IN IDE_BLK_IO_DEV
*IdeDev
53 // Poll Alternate Register for BSY clear within timeout.
55 EfiStatus
= WaitForBSYClear2 (IdeDev
, ATATIMEOUT
);
56 if (EFI_ERROR (EfiStatus
)) {
57 return EFI_DEVICE_ERROR
;
61 // Select device via Device/Head Register.
63 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4 | 0xe0);
64 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, DeviceSelect
);
67 // Poll Alternate Register for DRDY set within timeout.
68 // After device is selected, DRDY set indicates the device is ready to
71 EfiStatus
= DRDYReady2 (IdeDev
, ATATIMEOUT
);
72 if (EFI_ERROR (EfiStatus
)) {
73 return EFI_DEVICE_ERROR
;
77 // Get Media Status Command is sent
79 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, 0xDA);
82 // BSY bit will clear after command is complete.
84 EfiStatus
= WaitForBSYClear2 (IdeDev
, ATATIMEOUT
);
85 if (EFI_ERROR (EfiStatus
)) {
86 return EFI_DEVICE_ERROR
;
90 // the media status is returned by the command in the ERROR register
92 StatusValue
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
94 if (StatusValue
& BIT1
) {
98 if (StatusValue
& BIT6
) {
99 return EFI_WRITE_PROTECTED
;
106 This function is used to send Enable Media Status Notification Command
107 or Disable Media Status Notification Command.
110 pointer pointing to IDE_BLK_IO_DEV data structure, used
111 to record all the information of the IDE device.
114 a flag that indicates whether enable or disable media
118 If command completes successfully.
120 @retval EFI_DEVICE_ERROR
126 LS120EnableMediaStatus (
127 IN IDE_BLK_IO_DEV
*IdeDev
,
135 // Poll Alternate Register for BSY clear within timeout.
137 Status
= WaitForBSYClear2 (IdeDev
, ATATIMEOUT
);
138 if (EFI_ERROR (Status
)) {
139 return EFI_DEVICE_ERROR
;
143 // Select device via Device/Head Register.
145 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4 | 0xe0);
146 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, DeviceSelect
);
149 // Poll Alternate Register for DRDY set within timeout.
150 // After device is selected, DRDY set indicates the device is ready to
153 Status
= DRDYReady2 (IdeDev
, ATATIMEOUT
);
154 if (EFI_ERROR (Status
)) {
155 return EFI_DEVICE_ERROR
;
160 // 0x95: Enable media status notification
162 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, 0x95);
165 // 0x31: Disable media status notification
167 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, 0x31);
170 // Set Feature Command is sent
172 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, 0xEF);
175 // BSY bit will clear after command is complete.
177 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
178 if (EFI_ERROR (Status
)) {
179 return EFI_DEVICE_ERROR
;
186 This function is called by DiscoverIdeDevice() during its device
189 Its main purpose is to get enough information for the device media
190 to fill in the Media data structure of the Block I/O Protocol interface.
192 There are 5 steps to reach such objective:
194 1. Sends out the ATAPI Identify Command to the specified device.
195 Only ATAPI device responses to this command. If the command succeeds,
196 it returns the Identify data structure which filled with information
197 about the device. Since the ATAPI device contains removable media,
198 the only meaningful information is the device module name.
200 2. Sends out ATAPI Inquiry Packet Command to the specified device.
201 This command will return inquiry data of the device, which contains
202 the device type information.
204 3. Allocate sense data space for future use. We don't detect the media
205 presence here to improvement boot performance, especially when CD
206 media is present. The media detection will be performed just before
207 each BLK_IO read/write
210 pointer pointing to IDE_BLK_IO_DEV data structure, used
211 to record all the information of the IDE device.
214 Identify ATAPI device successfully.
216 @retval EFI_DEVICE_ERROR
217 ATAPI Identify Device Command failed or device type
218 is not supported by this IDE driver.
221 Parameter "IdeDev" will be updated in this function.
223 TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
224 TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
228 IN IDE_BLK_IO_DEV
*IdeDev
231 EFI_IDENTIFY_DATA
*AtapiIdentifyPointer
;
238 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4);
240 AtapiIdentifyPointer
= AllocatePool (sizeof (EFI_IDENTIFY_DATA
));
241 if (AtapiIdentifyPointer
== NULL
) {
242 return EFI_OUT_OF_RESOURCES
;
245 // Send ATAPI Identify Command to get IDENTIFY data.
247 Status
= AtaPioDataIn (
249 (VOID
*) AtapiIdentifyPointer
,
250 sizeof (EFI_IDENTIFY_DATA
),
251 ATA_CMD_IDENTIFY_DEVICE
,
259 if (EFI_ERROR (Status
)) {
260 gBS
->FreePool (AtapiIdentifyPointer
);
261 return EFI_DEVICE_ERROR
;
264 IdeDev
->pIdData
= AtapiIdentifyPointer
;
265 PrintAtaModuleName (IdeDev
);
268 // Send ATAPI Inquiry Packet Command to get INQUIRY data.
270 Status
= AtapiInquiry (IdeDev
);
271 if (EFI_ERROR (Status
)) {
272 gBS
->FreePool (IdeDev
->pIdData
);
274 // Make sure the pIdData will not be freed again.
276 IdeDev
->pIdData
= NULL
;
277 return EFI_DEVICE_ERROR
;
280 // Get media removable info from INQUIRY data.
282 IdeDev
->BlkIo
.Media
->RemovableMedia
= (UINT8
) ((IdeDev
->pInquiryData
->RMB
& 0x80) == 0x80);
285 // Identify device type via INQUIRY data.
287 switch (IdeDev
->pInquiryData
->peripheral_type
& 0x1f) {
295 // device is LS120 or ZIP drive.
297 IdeDev
->Type
= IdeMagnetic
;
299 IdeDev
->BlkIo
.Media
->MediaId
= 0;
301 // Give initial value
303 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
305 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
306 IdeDev
->BlkIo
.Media
->BlockSize
= 0x200;
314 IdeDev
->Type
= IdeCdRom
;
315 IdeDev
->BlkIo
.Media
->MediaId
= 0;
317 // Give initial value
319 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
321 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
322 IdeDev
->BlkIo
.Media
->BlockSize
= 0x800;
323 IdeDev
->BlkIo
.Media
->ReadOnly
= TRUE
;
342 IdeDev
->Type
= IdeUnknown
;
343 gBS
->FreePool (IdeDev
->pIdData
);
344 gBS
->FreePool (IdeDev
->pInquiryData
);
346 // Make sure the pIdData and pInquiryData will not be freed again.
348 IdeDev
->pIdData
= NULL
;
349 IdeDev
->pInquiryData
= NULL
;
350 return EFI_DEVICE_ERROR
;
354 // original sense data numbers
356 IdeDev
->SenseDataNumber
= 20;
358 IdeDev
->SenseData
= AllocatePool (IdeDev
->SenseDataNumber
* sizeof (ATAPI_REQUEST_SENSE_DATA
));
359 if (IdeDev
->SenseData
== NULL
) {
360 gBS
->FreePool (IdeDev
->pIdData
);
361 gBS
->FreePool (IdeDev
->pInquiryData
);
363 // Make sure the pIdData and pInquiryData will not be freed again.
365 IdeDev
->pIdData
= NULL
;
366 IdeDev
->pInquiryData
= NULL
;
367 return EFI_OUT_OF_RESOURCES
;
374 Sends out ATAPI Inquiry Packet Command to the specified device.
375 This command will return INQUIRY data of the device.
378 pointer pointing to IDE_BLK_IO_DEV data structure, used
379 to record all the information of the IDE device.
382 Inquiry command completes successfully.
384 @retval EFI_DEVICE_ERROR
385 Inquiry command failed.
388 Parameter "IdeDev" will be updated in this function.
393 IN IDE_BLK_IO_DEV
*IdeDev
396 ATAPI_PACKET_COMMAND Packet
;
398 ATAPI_INQUIRY_DATA
*InquiryData
;
401 // prepare command packet for the ATAPI Inquiry Packet Command.
403 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
404 Packet
.Inquiry
.opcode
= ATA_CMD_INQUIRY
;
405 Packet
.Inquiry
.page_code
= 0;
406 Packet
.Inquiry
.allocation_length
= sizeof (ATAPI_INQUIRY_DATA
);
408 InquiryData
= AllocatePool (sizeof (ATAPI_INQUIRY_DATA
));
409 if (InquiryData
== NULL
) {
410 return EFI_DEVICE_ERROR
;
414 // Send command packet and get requested Inquiry data.
416 Status
= AtapiPacketCommandIn (
419 (UINT16
*) InquiryData
,
420 sizeof (ATAPI_INQUIRY_DATA
),
423 if (EFI_ERROR (Status
)) {
424 gBS
->FreePool (InquiryData
);
425 return EFI_DEVICE_ERROR
;
428 IdeDev
->pInquiryData
= InquiryData
;
434 This function is used to send out ATAPI commands conforms to the
435 Packet Command with PIO Data In Protocol.
438 pointer pointing to IDE_BLK_IO_DEV data structure, used
439 to record all the information of the IDE device.
442 pointer pointing to ATAPI_PACKET_COMMAND data structure
443 which contains the contents of the command.
446 buffer contained data transferred from device to host.
449 data size in byte unit of the buffer.
452 this parameter is used to specify the timeout
453 value for the PioReadWriteData() function.
456 send out the ATAPI packet command successfully
457 and device sends data successfully.
459 @retval EFI_DEVICE_ERROR
460 the device failed to send data.
464 AtapiPacketCommandIn (
465 IN IDE_BLK_IO_DEV
*IdeDev
,
466 IN ATAPI_PACKET_COMMAND
*Packet
,
472 UINT16
*CommandIndex
;
477 // Set all the command parameters by fill related registers.
478 // Before write to all the following registers, BSY and DRQ must be 0.
480 Status
= DRQClear2 (IdeDev
, ATAPITIMEOUT
);
481 if (EFI_ERROR (Status
)) {
486 // Select device via Device/Head Register.
490 IdeDev
->IoPort
->Head
,
491 (UINT8
) ((IdeDev
->Device
<< 4) | ATA_DEFAULT_CMD
) // DEFAULT_CMD: 0xa0 (1010,0000)
497 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, 0x00);
500 // set the transfersize to ATAPI_MAX_BYTE_COUNT to let the device
501 // determine how many data should be transferred.
505 IdeDev
->IoPort
->CylinderLsb
,
506 (UINT8
) (ATAPI_MAX_BYTE_COUNT
& 0x00ff)
510 IdeDev
->IoPort
->CylinderMsb
,
511 (UINT8
) (ATAPI_MAX_BYTE_COUNT
>> 8)
515 // ATA_DEFAULT_CTL:0x0a (0000,1010)
518 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, ATA_DEFAULT_CTL
);
521 // Send Packet command to inform device
522 // that the following data bytes are command packet.
524 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, ATA_CMD_PACKET
);
526 Status
= DRQReady (IdeDev
, ATAPITIMEOUT
);
527 if (EFI_ERROR (Status
)) {
532 // Send out command packet
534 CommandIndex
= Packet
->Data16
;
535 for (Count
= 0; Count
< 6; Count
++, CommandIndex
++) {
537 IDEWritePortW (IdeDev
->PciIo
, IdeDev
->IoPort
->Data
, *CommandIndex
);
542 // call PioReadWriteData() function to get
543 // requested transfer data form device.
545 return PioReadWriteData (IdeDev
, Buffer
, ByteCount
, 1, TimeOut
);
549 This function is used to send out ATAPI commands conforms to the
550 Packet Command with PIO Data Out Protocol.
553 pointer pointing to IDE_BLK_IO_DEV data structure, used
554 to record all the information of the IDE device.
557 pointer pointing to ATAPI_PACKET_COMMAND data structure
558 which contains the contents of the command.
561 buffer contained data transferred from host to device.
564 data size in byte unit of the buffer.
567 this parameter is used to specify the timeout
568 value for the PioReadWriteData() function.
571 send out the ATAPI packet command successfully
572 and device received data successfully.
574 @retval EFI_DEVICE_ERROR
575 the device failed to send data.
579 AtapiPacketCommandOut (
580 IN IDE_BLK_IO_DEV
*IdeDev
,
581 IN ATAPI_PACKET_COMMAND
*Packet
,
587 UINT16
*CommandIndex
;
592 // set all the command parameters
593 // Before write to all the following registers, BSY and DRQ must be 0.
595 Status
= DRQClear2 (IdeDev
, ATAPITIMEOUT
);
596 if (EFI_ERROR (Status
)) {
601 // Select device via Device/Head Register.
605 IdeDev
->IoPort
->Head
,
606 (UINT8
) ((IdeDev
->Device
<< 4) | ATA_DEFAULT_CMD
) // ATA_DEFAULT_CMD: 0xa0 (1010,0000)
612 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, 0x00);
615 // set the transfersize to ATAPI_MAX_BYTE_COUNT to
616 // let the device determine how many data should be transferred.
620 IdeDev
->IoPort
->CylinderLsb
,
621 (UINT8
) (ATAPI_MAX_BYTE_COUNT
& 0x00ff)
625 IdeDev
->IoPort
->CylinderMsb
,
626 (UINT8
) (ATAPI_MAX_BYTE_COUNT
>> 8)
630 // DEFAULT_CTL:0x0a (0000,1010)
633 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, ATA_DEFAULT_CTL
);
636 // Send Packet command to inform device
637 // that the following data bytes are command packet.
639 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, ATA_CMD_PACKET
);
641 Status
= DRQReady2 (IdeDev
, ATAPITIMEOUT
);
642 if (EFI_ERROR (Status
)) {
647 // Send out command packet
649 CommandIndex
= Packet
->Data16
;
650 for (Count
= 0; Count
< 6; Count
++, CommandIndex
++) {
651 IDEWritePortW (IdeDev
->PciIo
, IdeDev
->IoPort
->Data
, *CommandIndex
);
656 // call PioReadWriteData() function to send requested transfer data to device.
658 return PioReadWriteData (IdeDev
, Buffer
, ByteCount
, 0, TimeOut
);
662 This function is called by either AtapiPacketCommandIn() or
663 AtapiPacketCommandOut(). It is used to transfer data between
664 host and device. The data direction is specified by the fourth
668 pointer pointing to IDE_BLK_IO_DEV data structure, used
669 to record all the information of the IDE device.
672 buffer contained data transferred between host and device.
675 data size in byte unit of the buffer.
678 flag used to determine the data transfer direction.
679 Read equals 1, means data transferred from device to host;
680 Read equals 0, means data transferred from host to device.
683 timeout value for wait DRQ ready before each data
687 data is transferred successfully.
689 @retval EFI_DEVICE_ERROR
690 the device failed to transfer data.
695 IN IDE_BLK_IO_DEV
*IdeDev
,
703 // required transfer data in word unit.
705 UINT32 RequiredWordCount
;
708 // actual transfer data in word unit.
710 UINT32 ActualWordCount
;
716 // No data transfer is premitted.
718 if (ByteCount
== 0) {
722 // for performance, we assert the ByteCount is an even number
723 // which is actually a resonable assumption
724 ASSERT((ByteCount
%2) == 0);
727 RequiredWordCount
= ByteCount
/ 2;
729 // ActuralWordCount means the word count of data really transferred.
733 while (ActualWordCount
< RequiredWordCount
) {
736 // before each data transfer stream, the host should poll DRQ bit ready,
737 // to see whether indicates device is ready to transfer data.
739 Status
= DRQReady2 (IdeDev
, TimeOut
);
740 if (EFI_ERROR (Status
)) {
741 return CheckErrorStatus (IdeDev
);
745 // read Status Register will clear interrupt
747 IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
750 // get current data transfer size from Cylinder Registers.
752 WordCount
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
) << 8;
753 WordCount
= WordCount
| IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
);
754 WordCount
= WordCount
& 0xffff;
757 WordCount
= MIN (WordCount
, (RequiredWordCount
- ActualWordCount
));
760 IDEReadPortWMultiple (
762 IdeDev
->IoPort
->Data
,
767 IDEWritePortWMultiple (
769 IdeDev
->IoPort
->Data
,
775 PtrBuffer
+= WordCount
;
776 ActualWordCount
+= WordCount
;
781 // In the case where the drive wants to send more data than we need to read,
782 // the DRQ bit will be set and cause delays from DRQClear2().
783 // We need to read data from the drive until it clears DRQ so we can move on.
785 AtapiReadPendingData (IdeDev
);
789 // After data transfer is completed, normally, DRQ bit should clear.
791 Status
= DRQClear2 (IdeDev
, ATAPITIMEOUT
);
792 if (EFI_ERROR (Status
)) {
793 return EFI_DEVICE_ERROR
;
797 // read status register to check whether error happens.
799 return CheckErrorStatus (IdeDev
);
803 Sends out ATAPI Test Unit Ready Packet Command to the specified device
804 to find out whether device is accessible.
806 @param[in] *IdeDev Pointer pointing to IDE_BLK_IO_DEV data structure, used
807 to record all the information of the IDE device.
808 @param[in] *SenseCount Sense count for this packet command
810 @retval EFI_SUCCESS Device is accessible.
811 @retval EFI_DEVICE_ERROR Device is not accessible.
816 IN IDE_BLK_IO_DEV
*IdeDev
,
817 OUT UINTN
*SenseCount
820 ATAPI_PACKET_COMMAND Packet
;
826 // fill command packet
828 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
829 Packet
.TestUnitReady
.opcode
= ATA_CMD_TEST_UNIT_READY
;
832 // send command packet
834 Status
= AtapiPacketCommandIn (IdeDev
, &Packet
, NULL
, 0, ATAPITIMEOUT
);
835 if (EFI_ERROR (Status
)) {
839 Status
= AtapiRequestSense (IdeDev
, SenseCount
);
840 if (EFI_ERROR (Status
)) {
849 Sends out ATAPI Request Sense Packet Command to the specified device.
850 This command will return all the current Sense data in the device.
851 This function will pack all the Sense data in one single buffer.
854 pointer pointing to IDE_BLK_IO_DEV data structure, used
855 to record all the information of the IDE device.
857 @param[out] **SenseBuffers
858 allocated in this function, and freed by the calling function.
859 This buffer is used to accommodate all the sense data returned
863 record the unit size of the sense data block in the SenseBuffers,
865 @param[out] *BufNumbers
866 record the number of units in the SenseBuffers.
869 Request Sense command completes successfully.
871 @retval EFI_DEVICE_ERROR
872 Request Sense command failed.
877 IN IDE_BLK_IO_DEV
*IdeDev
,
878 OUT UINTN
*SenseCounts
882 ATAPI_REQUEST_SENSE_DATA
*Sense
;
884 BOOLEAN FetchSenseData
;
885 ATAPI_PACKET_COMMAND Packet
;
889 ZeroMem (IdeDev
->SenseData
, sizeof (ATAPI_REQUEST_SENSE_DATA
) * (IdeDev
->SenseDataNumber
));
891 // fill command packet for Request Sense Packet Command
893 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
894 Packet
.RequestSence
.opcode
= ATA_CMD_REQUEST_SENSE
;
895 Packet
.RequestSence
.allocation_length
= sizeof (ATAPI_REQUEST_SENSE_DATA
);
898 // initialize pointer
900 Ptr
= (UINT16
*) IdeDev
->SenseData
;
902 // request sense data from device continuously until no sense data
903 // exists in the device.
905 for (FetchSenseData
= TRUE
; FetchSenseData
;) {
907 Sense
= (ATAPI_REQUEST_SENSE_DATA
*) Ptr
;
910 // send out Request Sense Packet Command and get one Sense data form device
912 Status
= AtapiPacketCommandIn (
916 sizeof (ATAPI_REQUEST_SENSE_DATA
),
920 // failed to get Sense data
922 if (EFI_ERROR (Status
)) {
923 if (*SenseCounts
== 0) {
924 return EFI_DEVICE_ERROR
;
932 // We limit MAX sense data count to 20 in order to avoid dead loop. Some
933 // incompatible ATAPI devices don't retrive NO_SENSE when there is no media.
934 // In this case, dead loop occurs if we don't have a gatekeeper. 20 is
935 // supposed to be large enough for any ATAPI device.
937 if ((Sense
->sense_key
!= ATA_SK_NO_SENSE
) && ((*SenseCounts
) < 20)) {
939 // Ptr is word-based pointer
941 Ptr
+= (sizeof (ATAPI_REQUEST_SENSE_DATA
) + 1) >> 1;
945 // when no sense key, skip out the loop
947 FetchSenseData
= FALSE
;
955 Sends out ATAPI Read Capacity Packet Command to the specified device.
956 This command will return the information regarding the capacity of the
959 Current device status will impact device's response to the Read Capacity
960 Command. For example, if the device once reset, the Read Capacity
961 Command will fail. The Sense data record the current device status, so
962 if the Read Capacity Command failed, the Sense data must be requested
963 and be analyzed to determine if the Read Capacity Command should retry.
965 @param[in] *IdeDev Pointer pointing to IDE_BLK_IO_DEV data structure, used
966 to record all the information of the IDE device.
967 @param[in] SenseCount Sense count for this packet command
969 @retval EFI_SUCCESS Read Capacity Command finally completes successfully.
970 @retval EFI_DEVICE_ERROR Read Capacity Command failed because of device error.
972 @note Parameter "IdeDev" will be updated in this function.
974 TODO: EFI_NOT_READY - add return value to function comment
978 IN IDE_BLK_IO_DEV
*IdeDev
,
979 OUT UINTN
*SenseCount
983 // status returned by Read Capacity Packet Command
986 EFI_STATUS SenseStatus
;
987 ATAPI_PACKET_COMMAND Packet
;
990 // used for capacity data returned from ATAPI device
992 ATAPI_READ_CAPACITY_DATA Data
;
993 ATAPI_READ_FORMAT_CAPACITY_DATA FormatData
;
997 ZeroMem (&Data
, sizeof (Data
));
998 ZeroMem (&FormatData
, sizeof (FormatData
));
1000 if (IdeDev
->Type
== IdeCdRom
) {
1002 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1003 Packet
.Inquiry
.opcode
= ATA_CMD_READ_CAPACITY
;
1004 Status
= AtapiPacketCommandIn (
1008 sizeof (ATAPI_READ_CAPACITY_DATA
),
1014 // Type == IdeMagnetic
1016 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1017 Packet
.ReadFormatCapacity
.opcode
= ATA_CMD_READ_FORMAT_CAPACITY
;
1018 Packet
.ReadFormatCapacity
.allocation_length_lo
= 12;
1019 Status
= AtapiPacketCommandIn (
1022 (UINT16
*) &FormatData
,
1023 sizeof (ATAPI_READ_FORMAT_CAPACITY_DATA
),
1028 if (Status
== EFI_TIMEOUT
) {
1033 SenseStatus
= AtapiRequestSense (IdeDev
, SenseCount
);
1035 if (!EFI_ERROR (SenseStatus
)) {
1037 if (!EFI_ERROR (Status
)) {
1039 if (IdeDev
->Type
== IdeCdRom
) {
1041 IdeDev
->BlkIo
.Media
->LastBlock
= (Data
.LastLba3
<< 24) |
1042 (Data
.LastLba2
<< 16) |
1043 (Data
.LastLba1
<< 8) |
1046 if (IdeDev
->BlkIo
.Media
->LastBlock
!= 0) {
1048 IdeDev
->BlkIo
.Media
->BlockSize
= (Data
.BlockSize3
<< 24) |
1049 (Data
.BlockSize2
<< 16) |
1050 (Data
.BlockSize1
<< 8) |
1053 IdeDev
->BlkIo
.Media
->MediaPresent
= TRUE
;
1055 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1056 return EFI_DEVICE_ERROR
;
1059 IdeDev
->BlkIo
.Media
->ReadOnly
= TRUE
;
1062 // Because the user data portion in the sector of the Data CD supported
1065 IdeDev
->BlkIo
.Media
->BlockSize
= 0x800;
1068 if (IdeDev
->Type
== IdeMagnetic
) {
1070 if (FormatData
.DesCode
== 3) {
1071 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1072 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1075 IdeDev
->BlkIo
.Media
->LastBlock
= (FormatData
.LastLba3
<< 24) |
1076 (FormatData
.LastLba2
<< 16) |
1077 (FormatData
.LastLba1
<< 8) |
1078 FormatData
.LastLba0
;
1079 if (IdeDev
->BlkIo
.Media
->LastBlock
!= 0) {
1080 IdeDev
->BlkIo
.Media
->LastBlock
--;
1082 IdeDev
->BlkIo
.Media
->BlockSize
= (FormatData
.BlockSize2
<< 16) |
1083 (FormatData
.BlockSize1
<< 8) |
1084 FormatData
.BlockSize0
;
1086 IdeDev
->BlkIo
.Media
->MediaPresent
= TRUE
;
1088 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1090 // Return EFI_NOT_READY operation succeeds but returned capacity is 0
1092 return EFI_NOT_READY
;
1095 IdeDev
->BlkIo
.Media
->BlockSize
= 0x200;
1105 return EFI_DEVICE_ERROR
;
1110 Used before read/write blocks from/to ATAPI device media.
1111 Since ATAPI device media is removable, it is necessary to detect
1112 whether media is present and get current present media's
1113 information, and if media has been changed, Block I/O Protocol
1114 need to be reinstalled.
1117 pointer pointing to IDE_BLK_IO_DEV data structure, used
1118 to record all the information of the IDE device.
1120 @param[out] *MediaChange
1121 return value that indicates if the media of the device has been
1125 media found successfully.
1127 @retval EFI_DEVICE_ERROR
1128 any error encounters during media detection.
1130 @retval EFI_NO_MEDIA
1134 parameter IdeDev may be updated in this function.
1139 IN IDE_BLK_IO_DEV
*IdeDev
,
1140 OUT BOOLEAN
*MediaChange
1144 EFI_STATUS CleanStateStatus
;
1145 EFI_BLOCK_IO_MEDIA OldMediaInfo
;
1147 UINTN RetryNotReady
;
1149 SENSE_RESULT SResult
;
1150 BOOLEAN WriteProtected
;
1152 CopyMem (&OldMediaInfo
, IdeDev
->BlkIo
.Media
, sizeof (EFI_BLOCK_IO_MEDIA
));
1153 *MediaChange
= FALSE
;
1155 // Retry for SenseDeviceNotReadyNeedRetry.
1156 // Each retry takes 1s and we limit the upper boundary to
1157 // 120 times about 2 min.
1159 RetryNotReady
= 120;
1162 // Do Test Unit Ready
1169 while (RetryTimes
!= 0) {
1171 Status
= AtapiTestUnitReady (IdeDev
, &SenseCount
);
1173 if (EFI_ERROR (Status
)) {
1175 // Test Unit Ready error without sense data.
1176 // For some devices, this means there's extra data
1177 // that has not been read, so we read these extra
1178 // data out before going on.
1180 CleanStateStatus
= AtapiReadPendingData (IdeDev
);
1181 if (EFI_ERROR (CleanStateStatus
)) {
1183 // Busy wait failed, try again
1188 // Try again without counting down RetryTimes
1193 ParseSenseData (IdeDev
, SenseCount
, &SResult
);
1196 case SenseNoSenseKey
:
1197 if (IdeDev
->BlkIo
.Media
->MediaPresent
) {
1201 // Media present but the internal structure need refreshed.
1202 // Try Read Capacity
1208 case SenseDeviceNotReadyNeedRetry
:
1209 if (--RetryNotReady
== 0) {
1210 return EFI_DEVICE_ERROR
;
1212 gBS
->Stall (1000 * STALL_1_MILLI_SECOND
);
1217 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1218 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1222 case SenseDeviceNotReadyNoRetry
:
1223 case SenseMediaError
:
1224 return EFI_DEVICE_ERROR
;
1226 case SenseMediaChange
:
1227 IdeDev
->BlkIo
.Media
->MediaId
++;
1238 return EFI_DEVICE_ERROR
;
1246 while (RetryTimes
!= 0) {
1248 Status
= AtapiReadCapacity (IdeDev
, &SenseCount
);
1250 if (EFI_ERROR (Status
)) {
1255 ParseSenseData (IdeDev
, SenseCount
, &SResult
);
1258 case SenseNoSenseKey
:
1262 case SenseDeviceNotReadyNeedRetry
:
1264 // We use Test Unit Ready to retry which
1271 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1272 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1276 case SenseDeviceNotReadyNoRetry
:
1277 case SenseMediaError
:
1278 return EFI_DEVICE_ERROR
;
1280 case SenseMediaChange
:
1281 IdeDev
->BlkIo
.Media
->MediaId
++;
1292 return EFI_DEVICE_ERROR
;
1296 // the following code is to check the write-protected for LS120 media
1298 if ((IdeDev
->BlkIo
.Media
->MediaPresent
) && (IdeDev
->Type
== IdeMagnetic
)) {
1300 Status
= IsLS120orZipWriteProtected (IdeDev
, &WriteProtected
);
1301 if (!EFI_ERROR (Status
)) {
1303 if (WriteProtected
) {
1305 IdeDev
->BlkIo
.Media
->ReadOnly
= TRUE
;
1308 IdeDev
->BlkIo
.Media
->ReadOnly
= FALSE
;
1314 if (IdeDev
->BlkIo
.Media
->MediaId
!= OldMediaInfo
.MediaId
) {
1316 // Media change information got from the device
1318 *MediaChange
= TRUE
;
1321 if (IdeDev
->BlkIo
.Media
->ReadOnly
!= OldMediaInfo
.ReadOnly
) {
1322 *MediaChange
= TRUE
;
1323 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1326 if (IdeDev
->BlkIo
.Media
->BlockSize
!= OldMediaInfo
.BlockSize
) {
1327 *MediaChange
= TRUE
;
1328 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1331 if (IdeDev
->BlkIo
.Media
->LastBlock
!= OldMediaInfo
.LastBlock
) {
1332 *MediaChange
= TRUE
;
1333 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1336 if (IdeDev
->BlkIo
.Media
->MediaPresent
!= OldMediaInfo
.MediaPresent
) {
1337 if (IdeDev
->BlkIo
.Media
->MediaPresent
) {
1339 // when change from no media to media present, reset the MediaId to 1.
1341 IdeDev
->BlkIo
.Media
->MediaId
= 1;
1344 // when no media, reset the MediaId to zero.
1346 IdeDev
->BlkIo
.Media
->MediaId
= 0;
1349 *MediaChange
= TRUE
;
1353 // if any change on current existing media,
1354 // the Block I/O protocol need to be reinstalled.
1357 gBS
->ReinstallProtocolInterface (
1359 &gEfiBlockIoProtocolGuid
,
1365 if (IdeDev
->BlkIo
.Media
->MediaPresent
) {
1368 return EFI_NO_MEDIA
;
1373 This function is called by the AtapiBlkIoReadBlocks() to perform
1374 read from media in block unit.
1376 The main command used to access media here is READ(10) Command.
1377 READ(10) Command requests that the ATAPI device media transfer
1378 specified data to the host. Data is transferred in block(sector)
1379 unit. The maximum number of blocks that can be transferred once is
1380 65536. This is the main difference between READ(10) and READ(12)
1381 Command. The maximum number of blocks in READ(12) is 2 power 32.
1384 pointer pointing to IDE_BLK_IO_DEV data structure, used
1385 to record all the information of the IDE device.
1388 A pointer to the destination buffer for the data.
1391 The starting logical block address to read from
1392 on the device media.
1394 @param[in] NumberOfBlocks
1395 The number of transfer data blocks.
1397 @return status is fully dependent on the return status
1398 of AtapiPacketCommandIn() function.
1403 IN IDE_BLK_IO_DEV
*IdeDev
,
1406 IN UINTN NumberOfBlocks
1410 ATAPI_PACKET_COMMAND Packet
;
1411 ATAPI_READ10_CMD
*Read10Packet
;
1413 UINTN BlocksRemaining
;
1423 // fill command packet for Read(10) command
1425 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1426 Read10Packet
= &Packet
.Read10
;
1427 Lba32
= (UINT32
) Lba
;
1430 BlockSize
= IdeDev
->BlkIo
.Media
->BlockSize
;
1433 // limit the data bytes that can be transferred by one Read(10) Command
1437 BlocksRemaining
= NumberOfBlocks
;
1439 Status
= EFI_SUCCESS
;
1440 while (BlocksRemaining
> 0) {
1442 if (BlocksRemaining
<= MaxBlock
) {
1444 SectorCount
= (UINT16
) BlocksRemaining
;
1447 SectorCount
= MaxBlock
;
1451 // fill the Packet data structure
1454 Read10Packet
->opcode
= ATA_CMD_READ_10
;
1457 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1458 // Lba0 is MSB, Lba3 is LSB
1460 Read10Packet
->Lba3
= (UINT8
) (Lba32
& 0xff);
1461 Read10Packet
->Lba2
= (UINT8
) (Lba32
>> 8);
1462 Read10Packet
->Lba1
= (UINT8
) (Lba32
>> 16);
1463 Read10Packet
->Lba0
= (UINT8
) (Lba32
>> 24);
1466 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1467 // TranLen0 is MSB, TranLen is LSB
1469 Read10Packet
->TranLen1
= (UINT8
) (SectorCount
& 0xff);
1470 Read10Packet
->TranLen0
= (UINT8
) (SectorCount
>> 8);
1472 ByteCount
= SectorCount
* BlockSize
;
1474 if (IdeDev
->Type
== IdeCdRom
) {
1475 TimeOut
= CDROMLONGTIMEOUT
;
1477 TimeOut
= ATAPILONGTIMEOUT
;
1480 Status
= AtapiPacketCommandIn (
1483 (UINT16
*) PtrBuffer
,
1487 if (EFI_ERROR (Status
)) {
1491 Lba32
+= SectorCount
;
1492 PtrBuffer
= (UINT8
*) PtrBuffer
+ SectorCount
* BlockSize
;
1493 BlocksRemaining
-= SectorCount
;
1500 This function is called by the AtapiBlkIoWriteBlocks() to perform
1501 write onto media in block unit.
1502 The main command used to access media here is Write(10) Command.
1503 Write(10) Command requests that the ATAPI device media transfer
1504 specified data to the host. Data is transferred in block (sector)
1505 unit. The maximum number of blocks that can be transferred once is
1509 pointer pointing to IDE_BLK_IO_DEV data structure, used
1510 to record all the information of the IDE device.
1513 A pointer to the source buffer for the data.
1516 The starting logical block address to write onto
1519 @param[in] NumberOfBlocks
1520 The number of transfer data blocks.
1522 @return status is fully dependent on the return status
1523 of AtapiPacketCommandOut() function.
1528 IN IDE_BLK_IO_DEV
*IdeDev
,
1531 IN UINTN NumberOfBlocks
1535 ATAPI_PACKET_COMMAND Packet
;
1536 ATAPI_READ10_CMD
*Read10Packet
;
1539 UINTN BlocksRemaining
;
1548 // fill command packet for Write(10) command
1549 // Write(10) command packet has the same data structure as
1550 // Read(10) command packet,
1551 // so here use the Read10Packet data structure
1552 // for the Write(10) command packet.
1554 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1555 Read10Packet
= &Packet
.Read10
;
1557 Lba32
= (UINT32
) Lba
;
1560 BlockSize
= IdeDev
->BlkIo
.Media
->BlockSize
;
1563 // limit the data bytes that can be transferred by one Read(10) Command
1565 MaxBlock
= (UINT16
) (65536 / BlockSize
);
1567 BlocksRemaining
= NumberOfBlocks
;
1569 Status
= EFI_SUCCESS
;
1570 while (BlocksRemaining
> 0) {
1572 if (BlocksRemaining
>= MaxBlock
) {
1573 SectorCount
= MaxBlock
;
1575 SectorCount
= (UINT16
) BlocksRemaining
;
1579 // Command code is WRITE_10.
1581 Read10Packet
->opcode
= ATA_CMD_WRITE_10
;
1584 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1585 // Lba0 is MSB, Lba3 is LSB
1587 Read10Packet
->Lba3
= (UINT8
) (Lba32
& 0xff);
1588 Read10Packet
->Lba2
= (UINT8
) (Lba32
>> 8);
1589 Read10Packet
->Lba1
= (UINT8
) (Lba32
>> 16);
1590 Read10Packet
->Lba0
= (UINT8
) (Lba32
>> 24);
1593 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1594 // TranLen0 is MSB, TranLen is LSB
1596 Read10Packet
->TranLen1
= (UINT8
) (SectorCount
& 0xff);
1597 Read10Packet
->TranLen0
= (UINT8
) (SectorCount
>> 8);
1599 ByteCount
= SectorCount
* BlockSize
;
1601 Status
= AtapiPacketCommandOut (
1604 (UINT16
*) PtrBuffer
,
1608 if (EFI_ERROR (Status
)) {
1612 Lba32
+= SectorCount
;
1613 PtrBuffer
= ((UINT8
*) PtrBuffer
+ SectorCount
* BlockSize
);
1614 BlocksRemaining
-= SectorCount
;
1621 This function is used to implement the Soft Reset on the specified
1622 ATAPI device. Different from the AtaSoftReset(), here reset is a ATA
1623 Soft Reset Command special for ATAPI device, and it only take effects
1624 on the specified ATAPI device, not on the whole IDE bus.
1625 Since the ATAPI soft reset is needed when device is in exceptional
1626 condition (such as BSY bit is always set ), I think the Soft Reset
1627 command should be sent without waiting for the BSY clear and DRDY
1629 This function is called by IdeBlkIoReset(),
1630 a interface function of Block I/O protocol.
1633 pointer pointing to IDE_BLK_IO_DEV data structure, used
1634 to record all the information of the IDE device.
1637 Soft reset completes successfully.
1639 @retval EFI_DEVICE_ERROR
1640 Any step during the reset process is failed.
1645 IN IDE_BLK_IO_DEV
*IdeDev
1653 // for ATAPI device, no need to wait DRDY ready after device selecting.
1654 // (bit7 and bit5 are both set to 1 for backward compatibility)
1656 DeviceSelect
= (UINT8
) (((BIT7
| BIT5
) | (IdeDev
->Device
<< 4)));
1657 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, DeviceSelect
);
1659 Command
= ATA_CMD_SOFT_RESET
;
1660 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, Command
);
1663 // BSY cleared is the only status return to the host by the device
1664 // when reset is completed.
1665 // slave device needs at most 31s to clear BSY
1667 Status
= WaitForBSYClear (IdeDev
, 31000);
1668 if (EFI_ERROR (Status
)) {
1669 return EFI_DEVICE_ERROR
;
1673 // stall 5 seconds to make the device status stable
1675 gBS
->Stall (5000000);
1681 This function is the ATAPI implementation for ReadBlocks in the
1682 Block I/O Protocol interface.
1684 @param[in] *IdeBlkIoDev
1685 Indicates the calling context.
1688 The media id that the read request is for.
1691 The starting logical block address to read from
1694 @param[in] BufferSize
1695 The size of the Buffer in bytes. This must be a
1696 multiple of the intrinsic block size of the device.
1699 A pointer to the destination buffer for the data.
1700 The caller is responsible for either having implicit
1701 or explicit ownership of the memory that data is read into.
1704 Read Blocks successfully.
1706 @retval EFI_DEVICE_ERROR
1709 @retval EFI_NO_MEDIA
1710 There is no media in the device.
1712 @retval EFI_MEDIA_CHANGED
1713 The MediaId is not for the current media.
1715 @retval EFI_BAD_BUFFER_SIZE
1716 The BufferSize parameter is not a multiple of the
1717 intrinsic block size of the device.
1719 @retval EFI_INVALID_PARAMETER
1720 The read request contains LBAs that are not valid,
1721 or the data buffer is not valid.
1725 AtapiBlkIoReadBlocks (
1726 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
,
1729 IN UINTN BufferSize
,
1733 EFI_BLOCK_IO_MEDIA
*Media
;
1735 UINTN NumberOfBlocks
;
1738 BOOLEAN MediaChange
;
1740 if (Buffer
== NULL
) {
1741 return EFI_INVALID_PARAMETER
;
1744 if (BufferSize
== 0) {
1749 // ATAPI device media is removable, so it is a must
1750 // to detect media first before read operation
1752 MediaChange
= FALSE
;
1753 Status
= AtapiDetectMedia (IdeBlkIoDevice
, &MediaChange
);
1754 if (EFI_ERROR (Status
)) {
1756 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1757 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1758 IdeBlkIoDevice
->Cache
= NULL
;
1764 // Get the intrinsic block size
1766 Media
= IdeBlkIoDevice
->BlkIo
.Media
;
1767 BlockSize
= Media
->BlockSize
;
1769 NumberOfBlocks
= BufferSize
/ BlockSize
;
1771 if (!(Media
->MediaPresent
)) {
1773 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1774 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1775 IdeBlkIoDevice
->Cache
= NULL
;
1777 return EFI_NO_MEDIA
;
1781 if ((MediaId
!= Media
->MediaId
) || MediaChange
) {
1783 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1784 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1785 IdeBlkIoDevice
->Cache
= NULL
;
1787 return EFI_MEDIA_CHANGED
;
1790 if (BufferSize
% BlockSize
!= 0) {
1791 return EFI_BAD_BUFFER_SIZE
;
1794 if (LBA
> Media
->LastBlock
) {
1795 return EFI_INVALID_PARAMETER
;
1798 if ((LBA
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
1799 return EFI_INVALID_PARAMETER
;
1802 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
1803 return EFI_INVALID_PARAMETER
;
1807 // if all the parameters are valid, then perform read sectors command
1808 // to transfer data from device to host.
1810 Status
= AtapiReadSectors (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1811 if (EFI_ERROR (Status
)) {
1812 return EFI_DEVICE_ERROR
;
1816 // Read blocks succeeded
1820 // save the first block to the cache for performance
1822 if (LBA
== 0 && !IdeBlkIoDevice
->Cache
) {
1823 IdeBlkIoDevice
->Cache
= AllocatePool (BlockSize
);
1824 if (IdeBlkIoDevice
!= NULL
) {
1825 CopyMem ((UINT8
*) IdeBlkIoDevice
->Cache
, (UINT8
*) Buffer
, BlockSize
);
1834 This function is the ATAPI implementation for WriteBlocks in the
1835 Block I/O Protocol interface.
1838 Indicates the calling context.
1841 The media id that the write request is for.
1844 The starting logical block address to write onto
1847 @param[in] BufferSize
1848 The size of the Buffer in bytes. This must be a
1849 multiple of the intrinsic block size of the device.
1852 A pointer to the source buffer for the data.
1853 The caller is responsible for either having implicit
1854 or explicit ownership of the memory that data is
1858 Write Blocks successfully.
1860 @retval EFI_DEVICE_ERROR
1861 Write Blocks failed.
1863 @retval EFI_NO_MEDIA
1864 There is no media in the device.
1866 @retval EFI_MEDIA_CHANGE
1867 The MediaId is not for the current media.
1869 @retval EFI_BAD_BUFFER_SIZE
1870 The BufferSize parameter is not a multiple of the
1871 intrinsic block size of the device.
1873 @retval EFI_INVALID_PARAMETER
1874 The write request contains LBAs that are not valid,
1875 or the data buffer is not valid.
1877 TODO: EFI_MEDIA_CHANGED - add return value to function comment
1878 TODO: EFI_WRITE_PROTECTED - add return value to function comment
1881 AtapiBlkIoWriteBlocks (
1882 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
,
1885 IN UINTN BufferSize
,
1890 EFI_BLOCK_IO_MEDIA
*Media
;
1892 UINTN NumberOfBlocks
;
1894 BOOLEAN MediaChange
;
1896 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1897 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1898 IdeBlkIoDevice
->Cache
= NULL
;
1901 if (Buffer
== NULL
) {
1902 return EFI_INVALID_PARAMETER
;
1905 if (BufferSize
== 0) {
1910 // ATAPI device media is removable,
1911 // so it is a must to detect media first before write operation
1913 MediaChange
= FALSE
;
1914 Status
= AtapiDetectMedia (IdeBlkIoDevice
, &MediaChange
);
1915 if (EFI_ERROR (Status
)) {
1917 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1918 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1919 IdeBlkIoDevice
->Cache
= NULL
;
1925 // Get the intrinsic block size
1927 Media
= IdeBlkIoDevice
->BlkIo
.Media
;
1928 BlockSize
= Media
->BlockSize
;
1929 NumberOfBlocks
= BufferSize
/ BlockSize
;
1931 if (!(Media
->MediaPresent
)) {
1933 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1934 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1935 IdeBlkIoDevice
->Cache
= NULL
;
1937 return EFI_NO_MEDIA
;
1940 if ((MediaId
!= Media
->MediaId
) || MediaChange
) {
1942 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1943 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1944 IdeBlkIoDevice
->Cache
= NULL
;
1946 return EFI_MEDIA_CHANGED
;
1949 if (Media
->ReadOnly
) {
1950 return EFI_WRITE_PROTECTED
;
1953 if (BufferSize
% BlockSize
!= 0) {
1954 return EFI_BAD_BUFFER_SIZE
;
1957 if (LBA
> Media
->LastBlock
) {
1958 return EFI_INVALID_PARAMETER
;
1961 if ((LBA
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
1962 return EFI_INVALID_PARAMETER
;
1965 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
1966 return EFI_INVALID_PARAMETER
;
1970 // if all the parameters are valid,
1971 // then perform write sectors command to transfer data from host to device.
1973 Status
= AtapiWriteSectors (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1974 if (EFI_ERROR (Status
)) {
1975 return EFI_DEVICE_ERROR
;
1983 This function is used to parse sense data. Only the first
1984 sense data is honoured.
1986 @param[in] IdeDev Indicates the calling context.
1987 @param[in] SenseCount Count of sense data.
1988 @param[out] Result The parsed result.
1990 @retval EFI_SUCCESS Successfully parsed.
1991 @retval EFI_INVALID_PARAMETER Count of sense data is zero.
1996 IN IDE_BLK_IO_DEV
*IdeDev
,
1997 IN UINTN SenseCount
,
1998 OUT SENSE_RESULT
*Result
2001 ATAPI_REQUEST_SENSE_DATA
*SenseData
;
2003 if (SenseCount
== 0) {
2004 return EFI_INVALID_PARAMETER
;
2008 // Only use the first sense data
2010 SenseData
= IdeDev
->SenseData
;
2011 *Result
= SenseOtherSense
;
2013 switch (SenseData
->sense_key
) {
2014 case ATA_SK_NO_SENSE
:
2015 *Result
= SenseNoSenseKey
;
2017 case ATA_SK_NOT_READY
:
2018 switch (SenseData
->addnl_sense_code
) {
2019 case ATA_ASC_NO_MEDIA
:
2020 *Result
= SenseNoMedia
;
2022 case ATA_ASC_MEDIA_UPSIDE_DOWN
:
2023 *Result
= SenseMediaError
;
2025 case ATA_ASC_NOT_READY
:
2026 if (SenseData
->addnl_sense_code_qualifier
== ATA_ASCQ_IN_PROGRESS
) {
2027 *Result
= SenseDeviceNotReadyNeedRetry
;
2029 *Result
= SenseDeviceNotReadyNoRetry
;
2034 case ATA_SK_UNIT_ATTENTION
:
2035 if (SenseData
->addnl_sense_code
== ATA_ASC_MEDIA_CHANGE
) {
2036 *Result
= SenseMediaChange
;
2039 case ATA_SK_MEDIUM_ERROR
:
2040 switch (SenseData
->addnl_sense_code
) {
2041 case ATA_ASC_MEDIA_ERR1
:
2042 case ATA_ASC_MEDIA_ERR2
:
2043 case ATA_ASC_MEDIA_ERR3
:
2044 case ATA_ASC_MEDIA_ERR4
:
2045 *Result
= SenseMediaError
;
2057 This function reads the pending data in the device.
2059 @param[in] IdeDev Indicates the calling context.
2061 @retval EFI_SUCCESS Successfully read.
2062 @retval EFI_NOT_READY The BSY is set avoiding reading.
2066 AtapiReadPendingData (
2067 IN IDE_BLK_IO_DEV
*IdeDev
2071 UINT16 TempWordBuffer
;
2073 AltRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
);
2074 if ((AltRegister
& ATA_STSREG_BSY
) == ATA_STSREG_BSY
) {
2075 return EFI_NOT_READY
;
2077 if ((AltRegister
& (ATA_STSREG_BSY
| ATA_STSREG_DRQ
)) == ATA_STSREG_DRQ
) {
2078 TempWordBuffer
= IDEReadPortB (IdeDev
->PciIo
,IdeDev
->IoPort
->Alt
.AltStatus
);
2079 while ((TempWordBuffer
& (ATA_STSREG_BSY
| ATA_STSREG_DRQ
)) == ATA_STSREG_DRQ
) {
2080 IDEReadPortWMultiple (
2082 IdeDev
->IoPort
->Data
,
2086 TempWordBuffer
= IDEReadPortB (IdeDev
->PciIo
,IdeDev
->IoPort
->Alt
.AltStatus
);
2093 TODO: Add function description
2095 @param IdeDev TODO: add argument description
2096 @param WriteProtected TODO: add argument description
2098 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2099 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2100 @retval EFI_SUCCESS TODO: Add description for return value
2104 IsLS120orZipWriteProtected (
2105 IN IDE_BLK_IO_DEV
*IdeDev
,
2106 OUT BOOLEAN
*WriteProtected
2111 *WriteProtected
= FALSE
;
2113 Status
= LS120EnableMediaStatus (IdeDev
, TRUE
);
2114 if (EFI_ERROR (Status
)) {
2115 return EFI_DEVICE_ERROR
;
2119 // the Get Media Status Command is only valid
2120 // if a Set Features/Enable Media Status Command has been priviously issued.
2122 if (LS120GetMediaStatus (IdeDev
) == EFI_WRITE_PROTECTED
) {
2124 *WriteProtected
= TRUE
;
2127 *WriteProtected
= FALSE
;
2131 // After Get Media Status Command completes,
2132 // Set Features/Disable Media Command should be sent.
2134 Status
= LS120EnableMediaStatus (IdeDev
, FALSE
);
2135 if (EFI_ERROR (Status
)) {
2136 return EFI_DEVICE_ERROR
;