2 Header file for Sata Controller driver.
4 Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2018, ARM Ltd. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #ifndef _SATA_CONTROLLER_H_
17 #define _SATA_CONTROLLER_H_
21 #include <IndustryStandard/Pci.h>
23 #include <Protocol/ComponentName.h>
24 #include <Protocol/DriverBinding.h>
25 #include <Protocol/PciIo.h>
26 #include <Protocol/IdeControllerInit.h>
28 #include <Library/BaseLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/DebugLib.h>
31 #include <Library/UefiLib.h>
32 #include <Library/UefiDriverEntryPoint.h>
33 #include <Library/MemoryAllocationLib.h>
34 #include <Library/UefiBootServicesTableLib.h>
37 // Global Variables definitions
39 extern EFI_DRIVER_BINDING_PROTOCOL gSataControllerDriverBinding
;
40 extern EFI_COMPONENT_NAME_PROTOCOL gSataControllerComponentName
;
41 extern EFI_COMPONENT_NAME2_PROTOCOL gSataControllerComponentName2
;
43 #define AHCI_BAR_INDEX 0x05
44 #define R_AHCI_CAP 0x0
45 #define B_AHCI_CAP_NPS (BIT4 | BIT3 | BIT2 | BIT1 | BIT0) // Number of Ports
46 #define B_AHCI_CAP_SPM BIT17 // Supports Port Multiplier
49 /// AHCI each channel can have up to 1 device
51 #define AHCI_MAX_DEVICES 0x01
54 /// AHCI each channel can have 15 devices in the presence of a multiplier
56 #define AHCI_MULTI_MAX_DEVICES 0x0F
59 /// IDE supports 2 channel max
61 #define IDE_MAX_CHANNEL 0x02
64 /// IDE supports 2 devices max
66 #define IDE_MAX_DEVICES 0x02
68 #define SATA_ENUMER_ALL FALSE
71 // Sata Controller driver private data structure
73 #define SATA_CONTROLLER_SIGNATURE SIGNATURE_32('S','A','T','A')
75 typedef struct _EFI_SATA_CONTROLLER_PRIVATE_DATA
{
77 // Standard signature used to identify Sata Controller private data
82 // Protocol instance of IDE_CONTROLLER_INIT produced by this driver
84 EFI_IDE_CONTROLLER_INIT_PROTOCOL IdeInit
;
87 // Copy of protocol pointers used by this driver
89 EFI_PCI_IO_PROTOCOL
*PciIo
;
92 // The number of devices that are supported by this channel
97 // The highest disqulified mode for each attached device,
98 // From ATA/ATAPI spec, if a mode is not supported,
99 // the modes higher than it is also not supported
101 EFI_ATA_COLLECTIVE_MODE
*DisqualifiedModes
;
104 // A copy of EFI_IDENTIFY_DATA data for each attached SATA device and its flag
106 EFI_IDENTIFY_DATA
*IdentifyData
;
107 BOOLEAN
*IdentifyValid
;
110 // Track the state so that the PCI attributes that were modified
111 // can be restored to the original value later.
113 BOOLEAN PciAttributesChanged
;
116 // Copy of the original PCI Attributes
118 UINT64 OriginalPciAttributes
;
119 } EFI_SATA_CONTROLLER_PRIVATE_DATA
;
121 #define SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS(a) CR(a, EFI_SATA_CONTROLLER_PRIVATE_DATA, IdeInit, SATA_CONTROLLER_SIGNATURE)
124 // Driver binding functions declaration
127 Supported function of Driver Binding protocol for this driver.
128 Test to see if this driver supports ControllerHandle.
130 @param This Protocol instance pointer.
131 @param Controller Handle of device to test.
132 @param RemainingDevicePath A pointer to the device path. Should be ignored by
135 @retval EFI_SUCCESS This driver supports this device.
136 @retval EFI_ALREADY_STARTED This driver is already running on this device.
137 @retval other This driver does not support this device.
142 SataControllerSupported (
143 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
144 IN EFI_HANDLE Controller
,
145 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
149 This routine is called right after the .Supported() called and
150 Start this driver on ControllerHandle.
152 @param This Protocol instance pointer.
153 @param Controller Handle of device to bind driver to.
154 @param RemainingDevicePath A pointer to the device path. Should be ignored by
157 @retval EFI_SUCCESS This driver is added to this device.
158 @retval EFI_ALREADY_STARTED This driver is already running on this device.
159 @retval other Some error occurs when binding this driver to this device.
164 SataControllerStart (
165 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
166 IN EFI_HANDLE Controller
,
167 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
171 Stop this driver on ControllerHandle.
173 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
174 @param Controller A handle to the device being stopped.
175 @param NumberOfChildren The number of child device handles in ChildHandleBuffer.
176 @param ChildHandleBuffer An array of child handles to be freed.
178 @retval EFI_SUCCESS This driver is removed from this device.
179 @retval other Some error occurs when removing this driver from this device.
185 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
186 IN EFI_HANDLE Controller
,
187 IN UINTN NumberOfChildren
,
188 IN EFI_HANDLE
*ChildHandleBuffer
192 // IDE controller init functions declaration
195 Returns the information about the specified IDE channel.
197 This function can be used to obtain information about a particular IDE channel.
198 The driver entity uses this information during the enumeration process.
200 If Enabled is set to FALSE, the driver entity will not scan the channel. Note
201 that it will not prevent an operating system driver from scanning the channel.
203 For most of today's controllers, MaxDevices will either be 1 or 2. For SATA
204 controllers, this value will always be 1. SATA configurations can contain SATA
205 port multipliers. SATA port multipliers behave like SATA bridges and can support
206 up to 16 devices on the other side. If a SATA port out of the IDE controller
207 is connected to a port multiplier, MaxDevices will be set to the number of SATA
208 devices that the port multiplier supports. Because today's port multipliers
209 support up to fifteen SATA devices, this number can be as large as fifteen. The IDE
210 bus driver is required to scan for the presence of port multipliers behind an SATA
211 controller and enumerate up to MaxDevices number of devices behind the port
214 In this context, the devices behind a port multiplier constitute a channel.
216 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
217 @param[in] Channel Zero-based channel number.
218 @param[out] Enabled TRUE if this channel is enabled. Disabled channels
219 are not scanned to see if any devices are present.
220 @param[out] MaxDevices The maximum number of IDE devices that the bus driver
221 can expect on this channel. For the ATA/ATAPI
222 specification, version 6, this number will either be
223 one or two. For Serial ATA (SATA) configurations with a
224 port multiplier, this number can be as large as fifteen.
227 @retval EFI_SUCCESS Information was returned without any errors.
228 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
233 IdeInitGetChannelInfo (
234 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
236 OUT BOOLEAN
*Enabled
,
237 OUT UINT8
*MaxDevices
241 The notifications from the driver entity that it is about to enter a certain
242 phase of the IDE channel enumeration process.
244 This function can be used to notify the IDE controller driver to perform
245 specific actions, including any chipset-specific initialization, so that the
246 chipset is ready to enter the next phase. Seven notification points are defined
249 More synchronization points may be added as required in the future.
251 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL
253 @param[in] Phase The phase during enumeration.
254 @param[in] Channel Zero-based channel number.
256 @retval EFI_SUCCESS The notification was accepted without any errors.
257 @retval EFI_UNSUPPORTED Phase is not supported.
258 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
259 @retval EFI_NOT_READY This phase cannot be entered at this time; for
260 example, an attempt was made to enter a Phase
261 without having entered one or more previous
268 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
269 IN EFI_IDE_CONTROLLER_ENUM_PHASE Phase
,
274 Submits the device information to the IDE controller driver.
276 This function is used by the driver entity to pass detailed information about
277 a particular device to the IDE controller driver. The driver entity obtains
278 this information by issuing an ATA or ATAPI IDENTIFY_DEVICE command. IdentifyData
279 is the pointer to the response data buffer. The IdentifyData buffer is owned
280 by the driver entity, and the IDE controller driver must make a local copy
281 of the entire buffer or parts of the buffer as needed. The original IdentifyData
282 buffer pointer may not be valid when
284 - EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() or
285 - EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() is called at a later point.
287 The IDE controller driver may consult various fields of EFI_IDENTIFY_DATA to
288 compute the optimum mode for the device. These fields are not limited to the
289 timing information. For example, an implementation of the IDE controller driver
290 may examine the vendor and type/mode field to match known bad drives.
292 The driver entity may submit drive information in any order, as long as it
293 submits information for all the devices belonging to the enumeration group
294 before EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() is called for any device
295 in that enumeration group. If a device is absent, EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
296 should be called with IdentifyData set to NULL. The IDE controller driver may
297 not have any other mechanism to know whether a device is present or not. Therefore,
298 setting IdentifyData to NULL does not constitute an error condition.
299 EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData() can be called only once for a
300 given (Channel, Device) pair.
302 @param[in] This A pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
303 @param[in] Channel Zero-based channel number.
304 @param[in] Device Zero-based device number on the Channel.
305 @param[in] IdentifyData The device's response to the ATA IDENTIFY_DEVICE command.
307 @retval EFI_SUCCESS The information was accepted without any errors.
308 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
309 @retval EFI_INVALID_PARAMETER Device is invalid.
315 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
318 IN EFI_IDENTIFY_DATA
*IdentifyData
322 Disqualifies specific modes for an IDE device.
324 This function allows the driver entity or other drivers (such as platform
325 drivers) to reject certain timing modes and request the IDE controller driver
326 to recalculate modes. This function allows the driver entity and the IDE
327 controller driver to negotiate the timings on a per-device basis. This function
328 is useful in the case of drives that lie about their capabilities. An example
329 is when the IDE device fails to accept the timing modes that are calculated
330 by the IDE controller driver based on the response to the Identify Drive command.
332 If the driver entity does not want to limit the ATA timing modes and leave that
333 decision to the IDE controller driver, it can either not call this function for
334 the given device or call this function and set the Valid flag to FALSE for all
335 modes that are listed in EFI_ATA_COLLECTIVE_MODE.
337 The driver entity may disqualify modes for a device in any order and any number
340 This function can be called multiple times to invalidate multiple modes of the
341 same type (e.g., Programmed Input/Output [PIO] modes 3 and 4). See the ATA/ATAPI
342 specification for more information on PIO modes.
344 For Serial ATA (SATA) controllers, this member function can be used to disqualify
345 a higher transfer rate mode on a given channel. For example, a platform driver
346 may inform the IDE controller driver to not use second-generation (Gen2) speeds
347 for a certain SATA drive.
349 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
350 @param[in] Channel The zero-based channel number.
351 @param[in] Device The zero-based device number on the Channel.
352 @param[in] BadModes The modes that the device does not support and that
353 should be disqualified.
355 @retval EFI_SUCCESS The modes were accepted without any errors.
356 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
357 @retval EFI_INVALID_PARAMETER Device is invalid.
358 @retval EFI_INVALID_PARAMETER IdentifyData is NULL.
363 IdeInitDisqualifyMode (
364 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
367 IN EFI_ATA_COLLECTIVE_MODE
*BadModes
371 Returns the information about the optimum modes for the specified IDE device.
373 This function is used by the driver entity to obtain the optimum ATA modes for
374 a specific device. The IDE controller driver takes into account the following
375 while calculating the mode:
376 - The IdentifyData inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
377 - The BadModes inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode()
379 The driver entity is required to call EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
380 for all the devices that belong to an enumeration group before calling
381 EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() for any device in the same group.
383 The IDE controller driver will use controller- and possibly platform-specific
384 algorithms to arrive at SupportedModes. The IDE controller may base its
385 decision on user preferences and other considerations as well. This function
386 may be called multiple times because the driver entity may renegotiate the mode
387 with the IDE controller driver using EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode().
389 The driver entity may collect timing information for various devices in any
390 order. The driver entity is responsible for making sure that all the dependencies
391 are satisfied. For example, the SupportedModes information for device A that
392 was previously returned may become stale after a call to
393 EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() for device B.
395 The buffer SupportedModes is allocated by the callee because the caller does
396 not necessarily know the size of the buffer. The type EFI_ATA_COLLECTIVE_MODE
397 is defined in a way that allows for future extensibility and can be of variable
398 length. This memory pool should be deallocated by the caller when it is no
401 The IDE controller driver for a Serial ATA (SATA) controller can use this
402 member function to force a lower speed (first-generation [Gen1] speeds on a
403 second-generation [Gen2]-capable hardware). The IDE controller driver can
404 also allow the driver entity to stay with the speed that has been negotiated
405 by the physical layer.
407 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
408 @param[in] Channel A zero-based channel number.
409 @param[in] Device A zero-based device number on the Channel.
410 @param[out] SupportedModes The optimum modes for the device.
412 @retval EFI_SUCCESS SupportedModes was returned.
413 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
414 @retval EFI_INVALID_PARAMETER Device is invalid.
415 @retval EFI_INVALID_PARAMETER SupportedModes is NULL.
416 @retval EFI_NOT_READY Modes cannot be calculated due to a lack of
417 data. This error may happen if
418 EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
419 and EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyData()
420 were not called for at least one drive in the
421 same enumeration group.
426 IdeInitCalculateMode (
427 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
430 OUT EFI_ATA_COLLECTIVE_MODE
**SupportedModes
434 Commands the IDE controller driver to program the IDE controller hardware
435 so that the specified device can operate at the specified mode.
437 This function is used by the driver entity to instruct the IDE controller
438 driver to program the IDE controller hardware to the specified modes. This
439 function can be called only once for a particular device. For a Serial ATA
440 (SATA) Advanced Host Controller Interface (AHCI) controller, no controller-
441 specific programming may be required.
443 @param[in] This Pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
444 @param[in] Channel Zero-based channel number.
445 @param[in] Device Zero-based device number on the Channel.
446 @param[in] Modes The modes to set.
448 @retval EFI_SUCCESS The command was accepted without any errors.
449 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
450 @retval EFI_INVALID_PARAMETER Device is invalid.
451 @retval EFI_NOT_READY Modes cannot be set at this time due to lack of data.
452 @retval EFI_DEVICE_ERROR Modes cannot be set due to hardware failure.
453 The driver entity should not use this device.
459 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
462 IN EFI_ATA_COLLECTIVE_MODE
*Modes
466 // Forward reference declaration
469 Retrieves a Unicode string that is the user readable name of the UEFI Driver.
471 @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance.
472 @param Language A pointer to a three character ISO 639-2 language identifier.
473 This is the language of the driver name that that the caller
474 is requesting, and it must match one of the languages specified
475 in SupportedLanguages. The number of languages supported by a
476 driver is up to the driver writer.
477 @param DriverName A pointer to the Unicode string to return. This Unicode string
478 is the name of the driver specified by This in the language
479 specified by Language.
481 @retval EFI_SUCCESS The Unicode string for the Driver specified by This
482 and the language specified by Language was returned
484 @retval EFI_INVALID_PARAMETER Language is NULL.
485 @retval EFI_INVALID_PARAMETER DriverName is NULL.
486 @retval EFI_UNSUPPORTED The driver specified by This does not support the
487 language specified by Language.
491 SataControllerComponentNameGetDriverName (
492 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
494 OUT CHAR16
**DriverName
498 Retrieves a Unicode string that is the user readable name of the controller
499 that is being managed by an UEFI Driver.
501 @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance.
502 @param ControllerHandle The handle of a controller that the driver specified by
503 This is managing. This handle specifies the controller
504 whose name is to be returned.
505 @param OPTIONAL ChildHandle The handle of the child controller to retrieve the name
506 of. This is an optional parameter that may be NULL. It
507 will be NULL for device drivers. It will also be NULL
508 for a bus drivers that wish to retrieve the name of the
509 bus controller. It will not be NULL for a bus driver
510 that wishes to retrieve the name of a child controller.
511 @param Language A pointer to a three character ISO 639-2 language
512 identifier. This is the language of the controller name
513 that that the caller is requesting, and it must match one
514 of the languages specified in SupportedLanguages. The
515 number of languages supported by a driver is up to the
517 @param ControllerName A pointer to the Unicode string to return. This Unicode
518 string is the name of the controller specified by
519 ControllerHandle and ChildHandle in the language
520 specified by Language from the point of view of the
521 driver specified by This.
523 @retval EFI_SUCCESS The Unicode string for the user readable name in the
524 language specified by Language for the driver
525 specified by This was returned in DriverName.
526 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
527 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
529 @retval EFI_INVALID_PARAMETER Language is NULL.
530 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
531 @retval EFI_UNSUPPORTED The driver specified by This is not currently
532 managing the controller specified by
533 ControllerHandle and ChildHandle.
534 @retval EFI_UNSUPPORTED The driver specified by This does not support the
535 language specified by Language.
539 SataControllerComponentNameGetControllerName (
540 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
541 IN EFI_HANDLE ControllerHandle
,
542 IN EFI_HANDLE ChildHandle OPTIONAL
,
544 OUT CHAR16
**ControllerName