2 Private Include file for IdeBus PEIM.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #ifndef _RECOVERY_ATAPI_H_
18 #define _RECOVERY_ATAPI_H_
22 #include <Ppi/BlockIo.h>
23 #include <Ppi/AtaController.h>
25 #include <Library/DebugLib.h>
26 #include <Library/TimerLib.h>
27 #include <Library/PeimEntryPoint.h>
28 #include <Library/PeiServicesLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/IoLib.h>
31 #include <Library/PeiServicesTablePointerLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/PcdLib.h>
36 #include <IndustryStandard/Atapi.h>
38 #define MAX_SENSE_KEY_COUNT 6
39 #define MAX_IDE_CHANNELS 4 // Ide and Sata Primary, Secondary Channel.
40 #define MAX_IDE_DEVICES 8 // Ide, Sata Primary, Secondary and Master, Slave device.
58 UINT16 Command
; /* when write */
59 UINT16 Status
; /* when read */
63 UINT16 Error
; /* when read */
64 UINT16 Feature
; /* when write */
65 } IDE_ERROR_OR_FEATURE
;
68 UINT16 AltStatus
; /* when read */
69 UINT16 DeviceControl
; /* when write */
70 } IDE_ALTSTATUS_OR_DEVICECONTROL
;
77 IDE_ERROR_OR_FEATURE Reg1
;
83 IDE_CMD_OR_STATUS Reg
;
85 IDE_ALTSTATUS_OR_DEVICECONTROL Alt
;
92 EFI_PEI_BLOCK_IO_MEDIA MediaInfo
;
94 } PEI_ATAPI_DEVICE_INFO
;
96 #define ATAPI_BLK_IO_DEV_SIGNATURE SIGNATURE_32 ('a', 'b', 'i', 'o')
100 EFI_PEI_RECOVERY_BLOCK_IO_PPI AtapiBlkIo
;
101 EFI_PEI_PPI_DESCRIPTOR PpiDescriptor
;
102 PEI_ATA_CONTROLLER_PPI
*AtaControllerPpi
;
105 PEI_ATAPI_DEVICE_INFO DeviceInfo
[MAX_IDE_DEVICES
]; //for max 8 device
106 IDE_BASE_REGISTERS IdeIoPortReg
[MAX_IDE_CHANNELS
]; //for max 4 channel.
109 #define PEI_RECOVERY_ATAPI_FROM_BLKIO_THIS(a) CR (a, ATAPI_BLK_IO_DEV, AtapiBlkIo, ATAPI_BLK_IO_DEV_SIGNATURE)
112 #define STALL_1_MILLI_SECOND 1000 // stall 1 ms
113 #define STALL_1_SECONDS 1000 * STALL_1_MILLI_SECOND
116 // Time Out Value For IDE Device Polling
118 // ATATIMEOUT is used for waiting time out for ATA device
120 #define ATATIMEOUT 1000 // 1 second
121 // ATAPITIMEOUT is used for waiting operation
122 // except read and write time out for ATAPI device
124 #define ATAPITIMEOUT 1000 // 1 second
125 // ATAPILONGTIMEOUT is used for waiting read and
126 // write operation timeout for ATAPI device
128 #define CDROMLONGTIMEOUT 2000 // 2 seconds
129 #define ATAPILONGTIMEOUT 5000 // 5 seconds
132 // PEI Recovery Block I/O PPI
136 Gets the count of block I/O devices that one specific block driver detects.
138 This function is used for getting the count of block I/O devices that one
139 specific block driver detects. To the PEI ATAPI driver, it returns the number
140 of all the detected ATAPI devices it detects during the enumeration process.
141 To the PEI legacy floppy driver, it returns the number of all the legacy
142 devices it finds during its enumeration process. If no device is detected,
143 then the function will return zero.
145 @param[in] PeiServices General-purpose services that are available
147 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
149 @param[out] NumberBlockDevices The number of block I/O devices discovered.
151 @retval EFI_SUCCESS Operation performed successfully.
156 AtapiGetNumberOfBlockDevices (
157 IN EFI_PEI_SERVICES
**PeiServices
,
158 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
159 OUT UINTN
*NumberBlockDevices
163 Gets a block device's media information.
165 This function will provide the caller with the specified block device's media
166 information. If the media changes, calling this function will update the media
167 information accordingly.
169 @param[in] PeiServices General-purpose services that are available to every
171 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
172 @param[in] DeviceIndex Specifies the block device to which the function wants
173 to talk. Because the driver that implements Block I/O
174 PPIs will manage multiple block devices, the PPIs that
175 want to talk to a single device must specify the
176 device index that was assigned during the enumeration
177 process. This index is a number from one to
179 @param[out] MediaInfo The media information of the specified block media.
180 The caller is responsible for the ownership of this
183 @retval EFI_SUCCESS Media information about the specified block device
184 was obtained successfully.
185 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
187 @retval Others Other failure occurs.
192 AtapiGetBlockDeviceMediaInfo (
193 IN EFI_PEI_SERVICES
**PeiServices
,
194 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
195 IN UINTN DeviceIndex
,
196 OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
200 Reads the requested number of blocks from the specified block device.
202 The function reads the requested number of blocks from the device. All the
203 blocks are read, or an error is returned. If there is no media in the device,
204 the function returns EFI_NO_MEDIA.
206 @param[in] PeiServices General-purpose services that are available to
208 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
209 @param[in] DeviceIndex Specifies the block device to which the function wants
210 to talk. Because the driver that implements Block I/O
211 PPIs will manage multiple block devices, the PPIs that
212 want to talk to a single device must specify the device
213 index that was assigned during the enumeration process.
214 This index is a number from one to NumberBlockDevices.
215 @param[in] StartLBA The starting logical block address (LBA) to read from
217 @param[in] BufferSize The size of the Buffer in bytes. This number must be
218 a multiple of the intrinsic block size of the device.
219 @param[out] Buffer A pointer to the destination buffer for the data.
220 The caller is responsible for the ownership of the
223 @retval EFI_SUCCESS The data was read correctly from the device.
224 @retval EFI_DEVICE_ERROR The device reported an error while attempting
225 to perform the read operation.
226 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
227 valid, or the buffer is not properly aligned.
228 @retval EFI_NO_MEDIA There is no media in the device.
229 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
230 the intrinsic block size of the device.
236 IN EFI_PEI_SERVICES
**PeiServices
,
237 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
238 IN UINTN DeviceIndex
,
239 IN EFI_PEI_LBA StartLBA
,
245 // Internal functions
249 Enumerate Atapi devices.
251 This function is used to enumerate Atatpi device in Ide channel.
253 @param[in] AtapiBlkIoDev A pointer to atapi block IO device
257 AtapiEnumerateDevices (
258 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
262 Detect Atapi devices.
264 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
265 @param[in] DevicePosition An integer to signify device position.
266 @param[out] MediaInfo The media information of the specified block media.
268 @retval TRUE Atapi device exists in specified position.
269 @retval FALSE Atapi device does not exist in specified position.
273 DiscoverAtapiDevice (
274 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
275 IN UINTN DevicePosition
,
276 OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
280 Detect if an IDE controller exists in specified position.
282 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
283 @param[in] DevicePosition An integer to signify device position.
285 @retval TRUE The Atapi device exists.
286 @retval FALSE The Atapi device does not present.
290 DetectIDEController (
291 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
292 IN UINTN DevicePosition
296 Wait specified time interval to poll for BSY bit clear in the Status Register.
298 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
299 @param[in] IdeIoRegisters A pointer to IDE IO registers.
300 @param[in] TimeoutInMilliSeconds Time specified in milliseconds.
302 @retval EFI_SUCCESS BSY bit is cleared in the specified time interval.
303 @retval EFI_TIMEOUT BSY bit is not cleared in the specified time interval.
308 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
309 IN IDE_BASE_REGISTERS
*IdeIoRegisters
,
310 IN UINTN TimeoutInMilliSeconds
314 Wait specified time interval to poll for DRDY bit set in the Status register.
316 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
317 @param[in] IdeIoRegisters A pointer to IDE IO registers.
318 @param[in] TimeoutInMilliSeconds Time specified in milliseconds.
320 @retval EFI_SUCCESS DRDY bit is set in the specified time interval.
321 @retval EFI_TIMEOUT DRDY bit is not set in the specified time interval.
326 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
327 IN IDE_BASE_REGISTERS
*IdeIoRegisters
,
328 IN UINTN TimeoutInMilliSeconds
332 Wait specified time interval to poll for DRQ bit clear in the Status Register.
334 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
335 @param[in] IdeIoRegisters A pointer to IDE IO registers.
336 @param[in] TimeoutInMilliSeconds Time specified in milliseconds.
338 @retval EFI_SUCCESS DRQ bit is cleared in the specified time interval.
339 @retval EFI_TIMEOUT DRQ bit is not cleared in the specified time interval.
344 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
345 IN IDE_BASE_REGISTERS
*IdeIoRegisters
,
346 IN UINTN TimeoutInMilliSeconds
350 Wait specified time interval to poll for DRQ bit clear in the Alternate Status Register.
352 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
353 @param[in] IdeIoRegisters A pointer to IDE IO registers.
354 @param[in] TimeoutInMilliSeconds Time specified in milliseconds.
356 @retval EFI_SUCCESS DRQ bit is cleared in the specified time interval.
357 @retval EFI_TIMEOUT DRQ bit is not cleared in the specified time interval.
362 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
363 IN IDE_BASE_REGISTERS
*IdeIoRegisters
,
364 IN UINTN TimeoutInMilliSeconds
368 Wait specified time interval to poll for DRQ bit set in the Status Register.
370 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
371 @param[in] IdeIoRegisters A pointer to IDE IO registers.
372 @param[in] TimeoutInMilliSeconds Time specified in milliseconds.
374 @retval EFI_SUCCESS DRQ bit is set in the specified time interval.
375 @retval EFI_TIMEOUT DRQ bit is not set in the specified time interval.
376 @retval EFI_ABORTED Operation Aborted.
381 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
382 IN IDE_BASE_REGISTERS
*IdeIoRegisters
,
383 IN UINTN TimeoutInMilliSeconds
387 Wait specified time interval to poll for DRQ bit set in the Alternate Status Register.
389 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
390 @param[in] IdeIoRegisters A pointer to IDE IO registers.
391 @param[in] TimeoutInMilliSeconds Time specified in milliseconds.
393 @retval EFI_SUCCESS DRQ bit is set in the specified time interval.
394 @retval EFI_TIMEOUT DRQ bit is not set in the specified time interval.
395 @retval EFI_ABORTED Operation Aborted.
400 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
401 IN IDE_BASE_REGISTERS
*IdeIoRegisters
,
402 IN UINTN TimeoutInMilliSeconds
406 Check if there is an error in Status Register.
408 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
409 @param[in] StatusReg The address to IDE IO registers.
411 @retval EFI_SUCCESS Operation success.
412 @retval EFI_DEVICE_ERROR Device error.
417 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
422 Idendify Atapi devices.
424 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
425 @param[in] DevicePosition An integer to signify device position.
427 @retval EFI_SUCCESS Identify successfully.
428 @retval EFI_DEVICE_ERROR Device cannot be identified successfully.
433 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
434 IN UINTN DevicePosition
438 Sends out ATAPI Test Unit Ready Packet Command to the specified device
439 to find out whether device is accessible.
441 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
442 @param[in] DevicePosition An integer to signify device position.
444 @retval EFI_SUCCESS TestUnit command executed successfully.
445 @retval EFI_DEVICE_ERROR Device cannot be executed TestUnit command successfully.
450 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
451 IN UINTN DevicePosition
455 Send out ATAPI commands conforms to the Packet Command with PIO Data In Protocol.
457 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
458 @param[in] DevicePosition An integer to signify device position.
459 @param[in] Packet A pointer to ATAPI command packet.
460 @param[in] Buffer Buffer to contain requested transfer data from device.
461 @param[in] ByteCount Requested transfer data length.
462 @param[in] TimeoutInMilliSeconds Time out value, in unit of milliseconds.
464 @retval EFI_SUCCESS Command executed successfully.
465 @retval EFI_DEVICE_ERROR Device cannot be executed command successfully.
469 AtapiPacketCommandIn (
470 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
471 IN UINTN DevicePosition
,
472 IN ATAPI_PACKET_COMMAND
*Packet
,
475 IN UINTN TimeoutInMilliSeconds
479 Sends out ATAPI Inquiry Packet Command to the specified device.
480 This command will return INQUIRY data of the device.
482 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
483 @param[in] DevicePosition An integer to signify device position.
484 @param[out] MediaInfo The media information of the specified block media.
486 @retval EFI_SUCCESS Command executed successfully.
487 @retval EFI_DEVICE_ERROR Device cannot be executed command successfully.
488 @retval EFI_UNSUPPORTED Unsupported device type.
493 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
494 IN UINTN DevicePosition
,
495 OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
499 Used before read/write blocks from/to ATAPI device media.
500 Since ATAPI device media is removable, it is necessary to detect
501 whether media is present and get current present media's information.
503 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
504 @param[in] DevicePosition An integer to signify device position.
505 @param[in, out] MediaInfo The media information of the specified block media.
507 @retval EFI_SUCCESS Command executed successfully.
508 @retval EFI_DEVICE_ERROR Some device errors happen.
509 @retval EFI_OUT_OF_RESOURCES Can not allocate required resources.
514 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
515 IN UINTN DevicePosition
,
516 IN OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
520 Reset specified Atapi device.
522 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
523 @param[in] DevicePosition An integer to signify device position.
524 @param[in] Extensive If TRUE, use ATA soft reset, otherwise use Atapi soft reset.
526 @retval EFI_SUCCESS Command executed successfully.
527 @retval EFI_DEVICE_ERROR Some device errors happen.
532 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
533 IN UINTN DevicePosition
,
538 Sends out ATAPI Request Sense Packet Command to the specified device.
540 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
541 @param[in] DevicePosition An integer to signify device position.
542 @param[in] SenseBuffers Pointer to sense buffer.
543 @param[in, out] SenseCounts Length of sense buffer.
545 @retval EFI_SUCCESS Command executed successfully.
546 @retval EFI_DEVICE_ERROR Some device errors happen.
551 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
552 IN UINTN DevicePosition
,
553 IN ATAPI_REQUEST_SENSE_DATA
*SenseBuffers
,
554 IN OUT UINT8
*SenseCounts
558 Sends out ATAPI Read Capacity Packet Command to the specified device.
559 This command will return the information regarding the capacity of the
562 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
563 @param[in] DevicePosition An integer to signify device position.
564 @param[in, out] MediaInfo The media information of the specified block media.
566 @retval EFI_SUCCESS Command executed successfully.
567 @retval EFI_DEVICE_ERROR Some device errors happen.
572 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
573 IN UINTN DevicePosition
,
574 IN OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
578 Perform read from disk in block unit.
580 @param[in] AtapiBlkIoDev A pointer to atapi block IO device.
581 @param[in] DevicePosition An integer to signify device position.
582 @param[in] Buffer Buffer to contain read data.
583 @param[in] StartLba Starting LBA address.
584 @param[in] NumberOfBlocks Number of blocks to read.
585 @param[in] BlockSize Size of each block.
587 @retval EFI_SUCCESS Command executed successfully.
588 @retval EFI_DEVICE_ERROR Some device errors happen.
593 IN ATAPI_BLK_IO_DEV
*AtapiBlkIoDev
,
594 IN UINTN DevicePosition
,
596 IN EFI_PEI_LBA StartLba
,
597 IN UINTN NumberOfBlocks
,
602 Check if there is media according to sense data.
604 @param[in] SenseData Pointer to sense data.
605 @param[in] SenseCounts Count of sense data.
607 @retval TRUE No media
608 @retval FALSE Media exists
613 IN ATAPI_REQUEST_SENSE_DATA
*SenseData
,
618 Check if device state is unclear according to sense data.
620 @param[in] SenseData Pointer to sense data.
621 @param[in] SenseCounts Count of sense data.
623 @retval TRUE Device state is unclear
624 @retval FALSE Device state is clear
628 IsDeviceStateUnclear (
629 IN ATAPI_REQUEST_SENSE_DATA
*SenseData
,
634 Check if there is media error according to sense data.
636 @param[in] SenseData Pointer to sense data.
637 @param[in] SenseCounts Count of sense data.
639 @retval TRUE Media error
640 @retval FALSE No media error
645 IN ATAPI_REQUEST_SENSE_DATA
*SenseData
,
650 Check if drive is ready according to sense data.
652 @param[in] SenseData Pointer to sense data.
653 @param[in] SenseCounts Count of sense data.
654 @param[out] NeedRetry Indicate if retry is needed.
656 @retval TRUE Drive ready
657 @retval FALSE Drive not ready
662 IN ATAPI_REQUEST_SENSE_DATA
*SenseData
,
663 IN UINTN SenseCounts
,
664 OUT BOOLEAN
*NeedRetry