2 Master header file for ATA Bus Driver.
4 This file defines common data structures, macro definitions and some module
5 internal function header files.
7 Copyright (c) 2009 Intel Corporation. <BR>
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 #include <Protocol/AtaPassThru.h>
24 #include <Protocol/BlockIo.h>
25 #include <Protocol/DiskInfo.h>
26 #include <Protocol/DevicePath.h>
27 #include <Protocol/IdeControllerInit.h>
29 #include <Library/DebugLib.h>
30 #include <Library/UefiDriverEntryPoint.h>
31 #include <Library/BaseLib.h>
32 #include <Library/UefiLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/MemoryAllocationLib.h>
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/DevicePathLib.h>
38 #include <IndustryStandard/Atapi.h>
41 // Time out value for ATA pass through protocol
43 #define ATA_TIMEOUT EFI_TIMER_PERIOD_SECONDS (1)
46 // Maximum number of times to retry ATA command
48 #define MAX_RETRY_TIMES 3
51 // The maximum total sectors count in 28 bit addressing mode
53 #define MAX_28BIT_ADDRESSING_CAPACITY 0xfffffff
56 // The maximum ATA transaction sector count in 28 bit addressing mode.
58 #define MAX_28BIT_TRANSFER_BLOCK_NUM 0x100
61 // The maximum ATA transaction sector count in 48 bit addressing mode.
63 #define MAX_48BIT_TRANSFER_BLOCK_NUM 0x10000
66 // The maximum model name in ATA identify data
68 #define MAX_MODEL_NAME_LEN 40
72 // ATA bus data structure for ATA controller
75 EFI_ATA_PASS_THRU_PROTOCOL
*AtaPassThru
;
76 EFI_HANDLE Controller
;
77 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
78 EFI_HANDLE DriverBindingHandle
;
79 } ATA_BUS_DRIVER_DATA
;
81 #define ATA_DEVICE_SIGNATURE SIGNATURE_32 ('A', 'B', 'I', 'D')
84 // ATA device data structure for each child device
90 EFI_BLOCK_IO_PROTOCOL BlockIo
;
91 EFI_BLOCK_IO_MEDIA BlockMedia
;
92 EFI_DISK_INFO_PROTOCOL DiskInfo
;
93 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
95 ATA_BUS_DRIVER_DATA
*AtaBusDriverData
;
97 UINT16 PortMultiplierPort
;
100 // Buffer for the execution of ATA pass through protocol
102 EFI_ATA_PASS_THRU_COMMAND_PACKET Packet
;
103 EFI_ATA_COMMAND_BLOCK Acb
;
104 EFI_ATA_STATUS_BLOCK
*Asb
;
110 // Cached data for ATA identify data
112 EFI_IDENTIFY_DATA
*IdentifyData
;
114 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
115 CHAR16 ModelName
[MAX_MODEL_NAME_LEN
+ 1];
119 #define ATA_DEVICE_FROM_BLOCK_IO(a) CR (a, ATA_DEVICE, BlockIo, ATA_DEVICE_SIGNATURE)
120 #define ATA_DEVICE_FROM_DISK_INFO(a) CR (a, ATA_DEVICE, DiskInfo, ATA_DEVICE_SIGNATURE)
125 extern EFI_DRIVER_BINDING_PROTOCOL gAtaBusDriverBinding
;
126 extern EFI_COMPONENT_NAME_PROTOCOL gAtaBusComponentName
;
127 extern EFI_COMPONENT_NAME2_PROTOCOL gAtaBusComponentName2
;
131 Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.ResetDevice().
133 This function wraps the ResetDevice() invocation for ATA pass through function
136 @param AtaDevice The ATA child device involved for the operation.
138 @return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
143 IN ATA_DEVICE
*AtaDevice
148 Discovers whether it is a valid ATA device.
150 This function issues ATA_CMD_IDENTIFY_DRIVE command to the ATA device to identify it.
151 If the command is executed successfully, it then identifies it and initializes
152 the Media information in Block IO protocol interface.
154 @param AtaDevice The ATA child device involved for the operation.
156 @retval EFI_SUCCESS The device is successfully identified and Media information
157 is correctly initialized.
158 @return others Some error occurs when discovering the ATA device.
163 IN OUT ATA_DEVICE
*AtaDevice
168 Read or write a number of blocks from ATA device.
170 This function performs ATA pass through transactions to read/write data from/to
171 ATA device. It may separate the read/write request into several ATA pass through
174 @param AtaDevice The ATA child device involved for the operation.
175 @param Buffer The pointer to the current transaction buffer.
176 @param StartLba The starting logical block address to be accessed.
177 @param NumberOfBlocks The block number or sector count of the transfer.
178 @param IsWrite Indicates whether it is a write operation.
180 @retval EFI_SUCCESS The data transfer is complete successfully.
181 @return others Some error occurs when transferring data.
186 IN OUT ATA_DEVICE
*AtaDevice
,
187 IN OUT UINT8
*Buffer
,
189 IN UINTN NumberOfBlocks
,
194 // Protocol interface prototypes
197 Tests to see if this driver supports a given controller. If a child device is provided,
198 it further tests to see if this driver supports creating a handle for the specified child device.
200 This function checks to see if the driver specified by This supports the device specified by
201 ControllerHandle. Drivers will typically use the device path attached to
202 ControllerHandle and/or the services from the bus I/O abstraction attached to
203 ControllerHandle to determine if the driver supports ControllerHandle. This function
204 may be called many times during platform initialization. In order to reduce boot times, the tests
205 performed by this function must be very small, and take as little time as possible to execute. This
206 function must not change the state of any hardware devices, and this function must be aware that the
207 device specified by ControllerHandle may already be managed by the same driver or a
208 different driver. This function must match its calls to AllocatePages() with FreePages(),
209 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
210 Since ControllerHandle may have been previously started by the same driver, if a protocol is
211 already in the opened state, then it must not be closed with CloseProtocol(). This is required
212 to guarantee the state of ControllerHandle is not modified by this function.
214 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
215 @param[in] ControllerHandle The handle of the controller to test. This handle
216 must support a protocol interface that supplies
217 an I/O abstraction to the driver.
218 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
219 parameter is ignored by device drivers, and is optional for bus
220 drivers. For bus drivers, if this parameter is not NULL, then
221 the bus driver must determine if the bus controller specified
222 by ControllerHandle and the child controller specified
223 by RemainingDevicePath are both supported by this
226 @retval EFI_SUCCESS The device specified by ControllerHandle and
227 RemainingDevicePath is supported by the driver specified by This.
228 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
229 RemainingDevicePath is already being managed by the driver
231 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
232 RemainingDevicePath is already being managed by a different
233 driver or an application that requires exclusive access.
234 Currently not implemented.
235 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
236 RemainingDevicePath is not supported by the driver specified by This.
240 AtaBusDriverBindingSupported (
241 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
242 IN EFI_HANDLE Controller
,
243 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
247 Starts a device controller or a bus controller.
249 The Start() function is designed to be invoked from the EFI boot service ConnectController().
250 As a result, much of the error checking on the parameters to Start() has been moved into this
251 common boot service. It is legal to call Start() from other locations,
252 but the following calling restrictions must be followed or the system behavior will not be deterministic.
253 1. ControllerHandle must be a valid EFI_HANDLE.
254 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
255 EFI_DEVICE_PATH_PROTOCOL.
256 3. Prior to calling Start(), the Supported() function for the driver specified by This must
257 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
259 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
260 @param[in] ControllerHandle The handle of the controller to start. This handle
261 must support a protocol interface that supplies
262 an I/O abstraction to the driver.
263 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
264 parameter is ignored by device drivers, and is optional for bus
265 drivers. For a bus driver, if this parameter is NULL, then handles
266 for all the children of Controller are created by this driver.
267 If this parameter is not NULL and the first Device Path Node is
268 not the End of Device Path Node, then only the handle for the
269 child device specified by the first Device Path Node of
270 RemainingDevicePath is created by this driver.
271 If the first Device Path Node of RemainingDevicePath is
272 the End of Device Path Node, no child handle is created by this
275 @retval EFI_SUCCESS The device was started.
276 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
277 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
278 @retval Others The driver failded to start the device.
283 AtaBusDriverBindingStart (
284 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
285 IN EFI_HANDLE Controller
,
286 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
290 Stops a device controller or a bus controller.
292 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
293 As a result, much of the error checking on the parameters to Stop() has been moved
294 into this common boot service. It is legal to call Stop() from other locations,
295 but the following calling restrictions must be followed or the system behavior will not be deterministic.
296 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
297 same driver's Start() function.
298 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
299 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
300 Start() function, and the Start() function must have called OpenProtocol() on
301 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
303 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
304 @param[in] ControllerHandle A handle to the device being stopped. The handle must
305 support a bus specific I/O protocol for the driver
306 to use to stop the device.
307 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
308 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
309 if NumberOfChildren is 0.
311 @retval EFI_SUCCESS The device was stopped.
312 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
317 AtaBusDriverBindingStop (
318 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
319 IN EFI_HANDLE Controller
,
320 IN UINTN NumberOfChildren
,
321 IN EFI_HANDLE
*ChildHandleBuffer
326 Retrieves a Unicode string that is the user readable name of the driver.
328 This function retrieves the user readable name of a driver in the form of a
329 Unicode string. If the driver specified by This has a user readable name in
330 the language specified by Language, then a pointer to the driver name is
331 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
332 by This does not support the language specified by Language,
333 then EFI_UNSUPPORTED is returned.
335 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
336 EFI_COMPONENT_NAME_PROTOCOL instance.
338 @param Language[in] A pointer to a Null-terminated ASCII string
339 array indicating the language. This is the
340 language of the driver name that the caller is
341 requesting, and it must match one of the
342 languages specified in SupportedLanguages. The
343 number of languages supported by a driver is up
344 to the driver writer. Language is specified
345 in RFC 4646 or ISO 639-2 language code format.
347 @param DriverName[out] A pointer to the Unicode string to return.
348 This Unicode string is the name of the
349 driver specified by This in the language
350 specified by Language.
352 @retval EFI_SUCCESS The Unicode string for the Driver specified by
353 This and the language specified by Language was
354 returned in DriverName.
356 @retval EFI_INVALID_PARAMETER Language is NULL.
358 @retval EFI_INVALID_PARAMETER DriverName is NULL.
360 @retval EFI_UNSUPPORTED The driver specified by This does not support
361 the language specified by Language.
366 AtaBusComponentNameGetDriverName (
367 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
369 OUT CHAR16
**DriverName
374 Retrieves a Unicode string that is the user readable name of the controller
375 that is being managed by a driver.
377 This function retrieves the user readable name of the controller specified by
378 ControllerHandle and ChildHandle in the form of a Unicode string. If the
379 driver specified by This has a user readable name in the language specified by
380 Language, then a pointer to the controller name is returned in ControllerName,
381 and EFI_SUCCESS is returned. If the driver specified by This is not currently
382 managing the controller specified by ControllerHandle and ChildHandle,
383 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
384 support the language specified by Language, then EFI_UNSUPPORTED is returned.
386 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
387 EFI_COMPONENT_NAME_PROTOCOL instance.
389 @param ControllerHandle[in] The handle of a controller that the driver
390 specified by This is managing. This handle
391 specifies the controller whose name is to be
394 @param ChildHandle[in] The handle of the child controller to retrieve
395 the name of. This is an optional parameter that
396 may be NULL. It will be NULL for device
397 drivers. It will also be NULL for a bus drivers
398 that wish to retrieve the name of the bus
399 controller. It will not be NULL for a bus
400 driver that wishes to retrieve the name of a
403 @param Language[in] A pointer to a Null-terminated ASCII string
404 array indicating the language. This is the
405 language of the driver name that the caller is
406 requesting, and it must match one of the
407 languages specified in SupportedLanguages. The
408 number of languages supported by a driver is up
409 to the driver writer. Language is specified in
410 RFC 4646 or ISO 639-2 language code format.
412 @param ControllerName[out] A pointer to the Unicode string to return.
413 This Unicode string is the name of the
414 controller specified by ControllerHandle and
415 ChildHandle in the language specified by
416 Language from the point of view of the driver
419 @retval EFI_SUCCESS The Unicode string for the user readable name in
420 the language specified by Language for the
421 driver specified by This was returned in
424 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
426 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
429 @retval EFI_INVALID_PARAMETER Language is NULL.
431 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
433 @retval EFI_UNSUPPORTED The driver specified by This is not currently
434 managing the controller specified by
435 ControllerHandle and ChildHandle.
437 @retval EFI_UNSUPPORTED The driver specified by This does not support
438 the language specified by Language.
443 AtaBusComponentNameGetControllerName (
444 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
445 IN EFI_HANDLE ControllerHandle
,
446 IN EFI_HANDLE ChildHandle OPTIONAL
,
448 OUT CHAR16
**ControllerName
453 Reset the Block Device.
455 @param This Indicates a pointer to the calling context.
456 @param ExtendedVerification Driver may perform diagnostics on reset.
458 @retval EFI_SUCCESS The device was reset.
459 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
466 IN EFI_BLOCK_IO_PROTOCOL
*This
,
467 IN BOOLEAN ExtendedVerification
472 Read BufferSize bytes from Lba into Buffer.
474 @param This Indicates a pointer to the calling context.
475 @param MediaId Id of the media, changes every time the media is replaced.
476 @param Lba The starting Logical Block Address to read from
477 @param BufferSize Size of Buffer, must be a multiple of device block size.
478 @param Buffer A pointer to the destination buffer for the data. The caller is
479 responsible for either having implicit or explicit ownership of the buffer.
481 @retval EFI_SUCCESS The data was read correctly from the device.
482 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
483 @retval EFI_NO_MEDIA There is no media in the device.
484 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
485 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
486 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
487 or the buffer is not on proper alignment.
492 AtaBlockIoReadBlocks (
493 IN EFI_BLOCK_IO_PROTOCOL
*This
,
502 Write BufferSize bytes from Lba into Buffer.
504 @param This Indicates a pointer to the calling context.
505 @param MediaId The media ID that the write request is for.
506 @param Lba The starting logical block address to be written. The caller is
507 responsible for writing to only legitimate locations.
508 @param BufferSize Size of Buffer, must be a multiple of device block size.
509 @param Buffer A pointer to the source buffer for the data.
511 @retval EFI_SUCCESS The data was written correctly to the device.
512 @retval EFI_WRITE_PROTECTED The device can not be written to.
513 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
514 @retval EFI_NO_MEDIA There is no media in the device.
515 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
516 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
517 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
518 or the buffer is not on proper alignment.
523 AtaBlockIoWriteBlocks (
524 IN EFI_BLOCK_IO_PROTOCOL
*This
,
533 Flush the Block Device.
535 @param This Indicates a pointer to the calling context.
537 @retval EFI_SUCCESS All outstanding data was written to the device
538 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
539 @retval EFI_NO_MEDIA There is no media in the device.
544 AtaBlockIoFlushBlocks (
545 IN EFI_BLOCK_IO_PROTOCOL
*This
550 Provides inquiry information for the controller type.
552 This function is used by the IDE bus driver to get inquiry data. Data format
553 of Identify data is defined by the Interface GUID.
555 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
556 @param[in, out] InquiryData Pointer to a buffer for the inquiry data.
557 @param[in, out] InquiryDataSize Pointer to the value for the inquiry data size.
559 @retval EFI_SUCCESS The command was accepted without any errors.
560 @retval EFI_NOT_FOUND Device does not support this data class
561 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
562 @retval EFI_BUFFER_TOO_SMALL InquiryDataSize not big enough
568 IN EFI_DISK_INFO_PROTOCOL
*This
,
569 IN OUT VOID
*InquiryData
,
570 IN OUT UINT32
*InquiryDataSize
575 Provides identify information for the controller type.
577 This function is used by the IDE bus driver to get identify data. Data format
578 of Identify data is defined by the Interface GUID.
580 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL
582 @param[in, out] IdentifyData Pointer to a buffer for the identify data.
583 @param[in, out] IdentifyDataSize Pointer to the value for the identify data
586 @retval EFI_SUCCESS The command was accepted without any errors.
587 @retval EFI_NOT_FOUND Device does not support this data class
588 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
589 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
594 AtaDiskInfoIdentify (
595 IN EFI_DISK_INFO_PROTOCOL
*This
,
596 IN OUT VOID
*IdentifyData
,
597 IN OUT UINT32
*IdentifyDataSize
602 Provides sense data information for the controller type.
604 This function is used by the IDE bus driver to get sense data.
605 Data format of Sense data is defined by the Interface GUID.
607 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
608 @param[in, out] SenseData Pointer to the SenseData.
609 @param[in, out] SenseDataSize Size of SenseData in bytes.
610 @param[out] SenseDataNumber Pointer to the value for the sense data size.
612 @retval EFI_SUCCESS The command was accepted without any errors.
613 @retval EFI_NOT_FOUND Device does not support this data class.
614 @retval EFI_DEVICE_ERROR Error reading SenseData from device.
615 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough.
620 AtaDiskInfoSenseData (
621 IN EFI_DISK_INFO_PROTOCOL
*This
,
622 IN OUT VOID
*SenseData
,
623 IN OUT UINT32
*SenseDataSize
,
624 OUT UINT8
*SenseDataNumber
629 This function is used by the IDE bus driver to get controller information.
631 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
632 @param[out] IdeChannel Pointer to the Ide Channel number. Primary or secondary.
633 @param[out] IdeDevice Pointer to the Ide Device number. Master or slave.
635 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid.
636 @retval EFI_UNSUPPORTED This is not an IDE device.
641 AtaDiskInfoWhichIde (
642 IN EFI_DISK_INFO_PROTOCOL
*This
,
643 OUT UINT32
*IdeChannel
,
644 OUT UINT32
*IdeDevice