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 - 2011, Intel Corporation. All rights reserved.<BR>
8 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>
28 #include <Library/DebugLib.h>
29 #include <Library/UefiDriverEntryPoint.h>
30 #include <Library/BaseLib.h>
31 #include <Library/UefiLib.h>
32 #include <Library/BaseMemoryLib.h>
33 #include <Library/MemoryAllocationLib.h>
34 #include <Library/UefiBootServicesTableLib.h>
35 #include <Library/DevicePathLib.h>
37 #include <IndustryStandard/Atapi.h>
40 // Time out value for ATA pass through protocol
42 #define ATA_TIMEOUT EFI_TIMER_PERIOD_SECONDS (3)
45 // Maximum number of times to retry ATA command
47 #define MAX_RETRY_TIMES 3
50 // The maximum total sectors count in 28 bit addressing mode
52 #define MAX_28BIT_ADDRESSING_CAPACITY 0xfffffff
55 // The maximum ATA transaction sector count in 28 bit addressing mode.
57 #define MAX_28BIT_TRANSFER_BLOCK_NUM 0x100
60 // The maximum ATA transaction sector count in 48 bit addressing mode.
62 #define MAX_48BIT_TRANSFER_BLOCK_NUM 0x10000
65 // The maximum model name in ATA identify data
67 #define MAX_MODEL_NAME_LEN 40
71 // ATA bus data structure for ATA controller
74 EFI_ATA_PASS_THRU_PROTOCOL
*AtaPassThru
;
75 EFI_HANDLE Controller
;
76 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
77 EFI_HANDLE DriverBindingHandle
;
78 } ATA_BUS_DRIVER_DATA
;
80 #define ATA_DEVICE_SIGNATURE SIGNATURE_32 ('A', 'B', 'I', 'D')
83 // ATA device data structure for each child device
89 EFI_BLOCK_IO_PROTOCOL BlockIo
;
90 EFI_BLOCK_IO_MEDIA BlockMedia
;
91 EFI_DISK_INFO_PROTOCOL DiskInfo
;
92 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
94 ATA_BUS_DRIVER_DATA
*AtaBusDriverData
;
96 UINT16 PortMultiplierPort
;
99 // Buffer for the execution of ATA pass through protocol
101 EFI_ATA_PASS_THRU_COMMAND_PACKET Packet
;
102 EFI_ATA_COMMAND_BLOCK Acb
;
103 EFI_ATA_STATUS_BLOCK
*Asb
;
109 // Cached data for ATA identify data
111 ATA_IDENTIFY_DATA
*IdentifyData
;
113 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
114 CHAR16 ModelName
[MAX_MODEL_NAME_LEN
+ 1];
118 #define ATA_DEVICE_FROM_BLOCK_IO(a) CR (a, ATA_DEVICE, BlockIo, ATA_DEVICE_SIGNATURE)
119 #define ATA_DEVICE_FROM_DISK_INFO(a) CR (a, ATA_DEVICE, DiskInfo, ATA_DEVICE_SIGNATURE)
124 extern EFI_DRIVER_BINDING_PROTOCOL gAtaBusDriverBinding
;
125 extern EFI_COMPONENT_NAME_PROTOCOL gAtaBusComponentName
;
126 extern EFI_COMPONENT_NAME2_PROTOCOL gAtaBusComponentName2
;
130 Wrapper for EFI_ATA_PASS_THRU_PROTOCOL.ResetDevice().
132 This function wraps the ResetDevice() invocation for ATA pass through function
135 @param AtaDevice The ATA child device involved for the operation.
137 @return The return status from EFI_ATA_PASS_THRU_PROTOCOL.PassThru().
142 IN ATA_DEVICE
*AtaDevice
147 Discovers whether it is a valid ATA device.
149 This function issues ATA_CMD_IDENTIFY_DRIVE command to the ATA device to identify it.
150 If the command is executed successfully, it then identifies it and initializes
151 the Media information in Block IO protocol interface.
153 @param AtaDevice The ATA child device involved for the operation.
155 @retval EFI_SUCCESS The device is successfully identified and Media information
156 is correctly initialized.
157 @return others Some error occurs when discovering the ATA device.
162 IN OUT ATA_DEVICE
*AtaDevice
167 Read or write a number of blocks from ATA device.
169 This function performs ATA pass through transactions to read/write data from/to
170 ATA device. It may separate the read/write request into several ATA pass through
173 @param AtaDevice The ATA child device involved for the operation.
174 @param Buffer The pointer to the current transaction buffer.
175 @param StartLba The starting logical block address to be accessed.
176 @param NumberOfBlocks The block number or sector count of the transfer.
177 @param IsWrite Indicates whether it is a write operation.
179 @retval EFI_SUCCESS The data transfer is complete successfully.
180 @return others Some error occurs when transferring data.
185 IN OUT ATA_DEVICE
*AtaDevice
,
186 IN OUT UINT8
*Buffer
,
188 IN UINTN NumberOfBlocks
,
193 // Protocol interface prototypes
196 Tests to see if this driver supports a given controller. If a child device is provided,
197 it further tests to see if this driver supports creating a handle for the specified child device.
199 This function checks to see if the driver specified by This supports the device specified by
200 ControllerHandle. Drivers will typically use the device path attached to
201 ControllerHandle and/or the services from the bus I/O abstraction attached to
202 ControllerHandle to determine if the driver supports ControllerHandle. This function
203 may be called many times during platform initialization. In order to reduce boot times, the tests
204 performed by this function must be very small, and take as little time as possible to execute. This
205 function must not change the state of any hardware devices, and this function must be aware that the
206 device specified by ControllerHandle may already be managed by the same driver or a
207 different driver. This function must match its calls to AllocatePages() with FreePages(),
208 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
209 Since ControllerHandle may have been previously started by the same driver, if a protocol is
210 already in the opened state, then it must not be closed with CloseProtocol(). This is required
211 to guarantee the state of ControllerHandle is not modified by this function.
213 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
214 @param[in] ControllerHandle The handle of the controller to test. This handle
215 must support a protocol interface that supplies
216 an I/O abstraction to the driver.
217 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
218 parameter is ignored by device drivers, and is optional for bus
219 drivers. For bus drivers, if this parameter is not NULL, then
220 the bus driver must determine if the bus controller specified
221 by ControllerHandle and the child controller specified
222 by RemainingDevicePath are both supported by this
225 @retval EFI_SUCCESS The device specified by ControllerHandle and
226 RemainingDevicePath is supported by the driver specified by This.
227 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
228 RemainingDevicePath is already being managed by the driver
230 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
231 RemainingDevicePath is already being managed by a different
232 driver or an application that requires exclusive access.
233 Currently not implemented.
234 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
235 RemainingDevicePath is not supported by the driver specified by This.
239 AtaBusDriverBindingSupported (
240 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
241 IN EFI_HANDLE Controller
,
242 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
246 Starts a device controller or a bus controller.
248 The Start() function is designed to be invoked from the EFI boot service ConnectController().
249 As a result, much of the error checking on the parameters to Start() has been moved into this
250 common boot service. It is legal to call Start() from other locations,
251 but the following calling restrictions must be followed or the system behavior will not be deterministic.
252 1. ControllerHandle must be a valid EFI_HANDLE.
253 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
254 EFI_DEVICE_PATH_PROTOCOL.
255 3. Prior to calling Start(), the Supported() function for the driver specified by This must
256 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
258 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
259 @param[in] ControllerHandle The handle of the controller to start. This handle
260 must support a protocol interface that supplies
261 an I/O abstraction to the driver.
262 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
263 parameter is ignored by device drivers, and is optional for bus
264 drivers. For a bus driver, if this parameter is NULL, then handles
265 for all the children of Controller are created by this driver.
266 If this parameter is not NULL and the first Device Path Node is
267 not the End of Device Path Node, then only the handle for the
268 child device specified by the first Device Path Node of
269 RemainingDevicePath is created by this driver.
270 If the first Device Path Node of RemainingDevicePath is
271 the End of Device Path Node, no child handle is created by this
274 @retval EFI_SUCCESS The device was started.
275 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
276 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
277 @retval Others The driver failded to start the device.
282 AtaBusDriverBindingStart (
283 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
284 IN EFI_HANDLE Controller
,
285 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
289 Stops a device controller or a bus controller.
291 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
292 As a result, much of the error checking on the parameters to Stop() has been moved
293 into this common boot service. It is legal to call Stop() from other locations,
294 but the following calling restrictions must be followed or the system behavior will not be deterministic.
295 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
296 same driver's Start() function.
297 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
298 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
299 Start() function, and the Start() function must have called OpenProtocol() on
300 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
302 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
303 @param[in] ControllerHandle A handle to the device being stopped. The handle must
304 support a bus specific I/O protocol for the driver
305 to use to stop the device.
306 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
307 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
308 if NumberOfChildren is 0.
310 @retval EFI_SUCCESS The device was stopped.
311 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
316 AtaBusDriverBindingStop (
317 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
318 IN EFI_HANDLE Controller
,
319 IN UINTN NumberOfChildren
,
320 IN EFI_HANDLE
*ChildHandleBuffer
325 Retrieves a Unicode string that is the user readable name of the driver.
327 This function retrieves the user readable name of a driver in the form of a
328 Unicode string. If the driver specified by This has a user readable name in
329 the language specified by Language, then a pointer to the driver name is
330 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
331 by This does not support the language specified by Language,
332 then EFI_UNSUPPORTED is returned.
334 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
335 EFI_COMPONENT_NAME_PROTOCOL instance.
337 @param Language[in] A pointer to a Null-terminated ASCII string
338 array indicating the language. This is the
339 language of the driver name that the caller is
340 requesting, and it must match one of the
341 languages specified in SupportedLanguages. The
342 number of languages supported by a driver is up
343 to the driver writer. Language is specified
344 in RFC 4646 or ISO 639-2 language code format.
346 @param DriverName[out] A pointer to the Unicode string to return.
347 This Unicode string is the name of the
348 driver specified by This in the language
349 specified by Language.
351 @retval EFI_SUCCESS The Unicode string for the Driver specified by
352 This and the language specified by Language was
353 returned in DriverName.
355 @retval EFI_INVALID_PARAMETER Language is NULL.
357 @retval EFI_INVALID_PARAMETER DriverName is NULL.
359 @retval EFI_UNSUPPORTED The driver specified by This does not support
360 the language specified by Language.
365 AtaBusComponentNameGetDriverName (
366 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
368 OUT CHAR16
**DriverName
373 Retrieves a Unicode string that is the user readable name of the controller
374 that is being managed by a driver.
376 This function retrieves the user readable name of the controller specified by
377 ControllerHandle and ChildHandle in the form of a Unicode string. If the
378 driver specified by This has a user readable name in the language specified by
379 Language, then a pointer to the controller name is returned in ControllerName,
380 and EFI_SUCCESS is returned. If the driver specified by This is not currently
381 managing the controller specified by ControllerHandle and ChildHandle,
382 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
383 support the language specified by Language, then EFI_UNSUPPORTED is returned.
385 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
386 EFI_COMPONENT_NAME_PROTOCOL instance.
388 @param ControllerHandle[in] The handle of a controller that the driver
389 specified by This is managing. This handle
390 specifies the controller whose name is to be
393 @param ChildHandle[in] The handle of the child controller to retrieve
394 the name of. This is an optional parameter that
395 may be NULL. It will be NULL for device
396 drivers. It will also be NULL for a bus drivers
397 that wish to retrieve the name of the bus
398 controller. It will not be NULL for a bus
399 driver that wishes to retrieve the name of a
402 @param Language[in] A pointer to a Null-terminated ASCII string
403 array indicating the language. This is the
404 language of the driver name that the caller is
405 requesting, and it must match one of the
406 languages specified in SupportedLanguages. The
407 number of languages supported by a driver is up
408 to the driver writer. Language is specified in
409 RFC 4646 or ISO 639-2 language code format.
411 @param ControllerName[out] A pointer to the Unicode string to return.
412 This Unicode string is the name of the
413 controller specified by ControllerHandle and
414 ChildHandle in the language specified by
415 Language from the point of view of the driver
418 @retval EFI_SUCCESS The Unicode string for the user readable name in
419 the language specified by Language for the
420 driver specified by This was returned in
423 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
425 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
428 @retval EFI_INVALID_PARAMETER Language is NULL.
430 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
432 @retval EFI_UNSUPPORTED The driver specified by This is not currently
433 managing the controller specified by
434 ControllerHandle and ChildHandle.
436 @retval EFI_UNSUPPORTED The driver specified by This does not support
437 the language specified by Language.
442 AtaBusComponentNameGetControllerName (
443 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
444 IN EFI_HANDLE ControllerHandle
,
445 IN EFI_HANDLE ChildHandle OPTIONAL
,
447 OUT CHAR16
**ControllerName
452 Reset the Block Device.
454 @param This Indicates a pointer to the calling context.
455 @param ExtendedVerification Driver may perform diagnostics on reset.
457 @retval EFI_SUCCESS The device was reset.
458 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
465 IN EFI_BLOCK_IO_PROTOCOL
*This
,
466 IN BOOLEAN ExtendedVerification
471 Read BufferSize bytes from Lba into Buffer.
473 @param This Indicates a pointer to the calling context.
474 @param MediaId Id of the media, changes every time the media is replaced.
475 @param Lba The starting Logical Block Address to read from
476 @param BufferSize Size of Buffer, must be a multiple of device block size.
477 @param Buffer A pointer to the destination buffer for the data. The caller is
478 responsible for either having implicit or explicit ownership of the buffer.
480 @retval EFI_SUCCESS The data was read correctly from the device.
481 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
482 @retval EFI_NO_MEDIA There is no media in the device.
483 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
484 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
485 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
486 or the buffer is not on proper alignment.
491 AtaBlockIoReadBlocks (
492 IN EFI_BLOCK_IO_PROTOCOL
*This
,
501 Write BufferSize bytes from Lba into Buffer.
503 @param This Indicates a pointer to the calling context.
504 @param MediaId The media ID that the write request is for.
505 @param Lba The starting logical block address to be written. The caller is
506 responsible for writing to only legitimate locations.
507 @param BufferSize Size of Buffer, must be a multiple of device block size.
508 @param Buffer A pointer to the source buffer for the data.
510 @retval EFI_SUCCESS The data was written correctly to the device.
511 @retval EFI_WRITE_PROTECTED The device can not be written to.
512 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
513 @retval EFI_NO_MEDIA There is no media in the device.
514 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
515 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
516 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
517 or the buffer is not on proper alignment.
522 AtaBlockIoWriteBlocks (
523 IN EFI_BLOCK_IO_PROTOCOL
*This
,
532 Flush the Block Device.
534 @param This Indicates a pointer to the calling context.
536 @retval EFI_SUCCESS All outstanding data was written to the device
537 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
538 @retval EFI_NO_MEDIA There is no media in the device.
543 AtaBlockIoFlushBlocks (
544 IN EFI_BLOCK_IO_PROTOCOL
*This
549 Provides inquiry information for the controller type.
551 This function is used by the IDE bus driver to get inquiry data. Data format
552 of Identify data is defined by the Interface GUID.
554 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
555 @param[in, out] InquiryData Pointer to a buffer for the inquiry data.
556 @param[in, out] InquiryDataSize Pointer to the value for the inquiry data size.
558 @retval EFI_SUCCESS The command was accepted without any errors.
559 @retval EFI_NOT_FOUND Device does not support this data class
560 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
561 @retval EFI_BUFFER_TOO_SMALL InquiryDataSize not big enough
567 IN EFI_DISK_INFO_PROTOCOL
*This
,
568 IN OUT VOID
*InquiryData
,
569 IN OUT UINT32
*InquiryDataSize
574 Provides identify information for the controller type.
576 This function is used by the IDE bus driver to get identify data. Data format
577 of Identify data is defined by the Interface GUID.
579 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL
581 @param[in, out] IdentifyData Pointer to a buffer for the identify data.
582 @param[in, out] IdentifyDataSize Pointer to the value for the identify data
585 @retval EFI_SUCCESS The command was accepted without any errors.
586 @retval EFI_NOT_FOUND Device does not support this data class
587 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
588 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
593 AtaDiskInfoIdentify (
594 IN EFI_DISK_INFO_PROTOCOL
*This
,
595 IN OUT VOID
*IdentifyData
,
596 IN OUT UINT32
*IdentifyDataSize
601 Provides sense data information for the controller type.
603 This function is used by the IDE bus driver to get sense data.
604 Data format of Sense data is defined by the Interface GUID.
606 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
607 @param[in, out] SenseData Pointer to the SenseData.
608 @param[in, out] SenseDataSize Size of SenseData in bytes.
609 @param[out] SenseDataNumber Pointer to the value for the sense data size.
611 @retval EFI_SUCCESS The command was accepted without any errors.
612 @retval EFI_NOT_FOUND Device does not support this data class.
613 @retval EFI_DEVICE_ERROR Error reading SenseData from device.
614 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough.
619 AtaDiskInfoSenseData (
620 IN EFI_DISK_INFO_PROTOCOL
*This
,
621 IN OUT VOID
*SenseData
,
622 IN OUT UINT32
*SenseDataSize
,
623 OUT UINT8
*SenseDataNumber
628 This function is used by the IDE bus driver to get controller information.
630 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
631 @param[out] IdeChannel Pointer to the Ide Channel number. Primary or secondary.
632 @param[out] IdeDevice Pointer to the Ide Device number. Master or slave.
634 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid.
635 @retval EFI_UNSUPPORTED This is not an IDE device.
640 AtaDiskInfoWhichIde (
641 IN EFI_DISK_INFO_PROTOCOL
*This
,
642 OUT UINT32
*IdeChannel
,
643 OUT UINT32
*IdeDevice