2 This driver module produces IDE_CONTROLLER_INIT protocol for Sata Controllers.
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "SataController.h"
18 /// EFI_DRIVER_BINDING_PROTOCOL instance
20 EFI_DRIVER_BINDING_PROTOCOL gSataControllerDriverBinding
= {
21 SataControllerSupported
,
30 Read AHCI Operation register.
32 @param PciIo The PCI IO protocol instance.
33 @param Offset The operation register offset.
35 @return The register content read.
41 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
47 ASSERT (PciIo
!= NULL
);
64 Write AHCI Operation register.
66 @param PciIo The PCI IO protocol instance.
67 @param Offset The operation register offset.
68 @param Data The data used to write down.
74 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
79 ASSERT (PciIo
!= NULL
);
94 This function is used to calculate the best PIO mode supported by specific IDE device
96 @param IdentifyData The identify data of specific IDE device.
97 @param DisPioMode Disqualified PIO modes collection.
98 @param SelectedMode Available PIO modes collection.
100 @retval EFI_SUCCESS Best PIO modes are returned.
101 @retval EFI_UNSUPPORTED The device doesn't support PIO mode,
102 or all supported modes have been disqualified.
105 CalculateBestPioMode (
106 IN EFI_IDENTIFY_DATA
*IdentifyData
,
107 IN UINT16
*DisPioMode OPTIONAL
,
108 OUT UINT16
*SelectedMode
112 UINT16 AdvancedPioMode
;
115 UINT16 MinimumPioCycleTime
;
119 PioMode
= (UINT8
) (((ATA5_IDENTIFY_DATA
*) (&(IdentifyData
->AtaData
)))->pio_cycle_timing
>> 8);
122 // See whether Identify Data word 64 - 70 are valid
124 if ((IdentifyData
->AtaData
.field_validity
& 0x02) == 0x02) {
126 AdvancedPioMode
= IdentifyData
->AtaData
.advanced_pio_modes
;
127 DEBUG ((EFI_D_INFO
, "CalculateBestPioMode: AdvancedPioMode = %x\n", AdvancedPioMode
));
129 for (Index
= 0; Index
< 8; Index
++) {
130 if ((AdvancedPioMode
& 0x01) != 0) {
134 AdvancedPioMode
>>= 1;
138 // If Temp is modified, mean the advanced_pio_modes is not zero;
139 // if Temp is not modified, mean there is no advanced PIO mode supported,
140 // the best PIO Mode is the value in pio_cycle_timing.
143 AdvancedPioMode
= (UINT16
) (Temp
+ 3);
145 AdvancedPioMode
= PioMode
;
149 // Limit the PIO mode to at most PIO4.
151 PioMode
= (UINT16
) MIN (AdvancedPioMode
, 4);
153 MinimumPioCycleTime
= IdentifyData
->AtaData
.min_pio_cycle_time_with_flow_control
;
155 if (MinimumPioCycleTime
<= 120) {
156 PioMode
= (UINT16
) MIN (4, PioMode
);
157 } else if (MinimumPioCycleTime
<= 180) {
158 PioMode
= (UINT16
) MIN (3, PioMode
);
159 } else if (MinimumPioCycleTime
<= 240) {
160 PioMode
= (UINT16
) MIN (2, PioMode
);
166 // Degrade the PIO mode if the mode has been disqualified
168 if (DisPioMode
!= NULL
) {
169 if (*DisPioMode
< 2) {
170 return EFI_UNSUPPORTED
; // no mode below ATA_PIO_MODE_BELOW_2
173 if (PioMode
>= *DisPioMode
) {
174 PioMode
= (UINT16
) (*DisPioMode
- 1);
179 *SelectedMode
= 1; // ATA_PIO_MODE_BELOW_2;
181 *SelectedMode
= PioMode
; // ATA_PIO_MODE_2 to ATA_PIO_MODE_4;
186 // Identify Data word 64 - 70 are not valid
187 // Degrade the PIO mode if the mode has been disqualified
189 if (DisPioMode
!= NULL
) {
190 if (*DisPioMode
< 2) {
191 return EFI_UNSUPPORTED
; // no mode below ATA_PIO_MODE_BELOW_2
194 if (PioMode
== *DisPioMode
) {
200 *SelectedMode
= 1; // ATA_PIO_MODE_BELOW_2;
202 *SelectedMode
= 2; // ATA_PIO_MODE_2;
211 This function is used to calculate the best UDMA mode supported by specific IDE device
213 @param IdentifyData The identify data of specific IDE device.
214 @param DisUDmaMode Disqualified UDMA modes collection.
215 @param SelectedMode Available UDMA modes collection.
217 @retval EFI_SUCCESS Best UDMA modes are returned.
218 @retval EFI_UNSUPPORTED The device doesn't support UDMA mode,
219 or all supported modes have been disqualified.
222 CalculateBestUdmaMode (
223 IN EFI_IDENTIFY_DATA
*IdentifyData
,
224 IN UINT16
*DisUDmaMode OPTIONAL
,
225 OUT UINT16
*SelectedMode
229 UINT16 DeviceUDmaMode
;
234 // Check whether the WORD 88 (supported UltraDMA by drive) is valid
236 if ((IdentifyData
->AtaData
.field_validity
& 0x04) == 0x00) {
237 return EFI_UNSUPPORTED
;
240 DeviceUDmaMode
= IdentifyData
->AtaData
.ultra_dma_mode
;
241 DEBUG ((EFI_D_INFO
, "CalculateBestUdmaMode: DeviceUDmaMode = %x\n", DeviceUDmaMode
));
242 DeviceUDmaMode
&= 0x3f;
243 TempMode
= 0; // initialize it to UDMA-0
245 while ((DeviceUDmaMode
>>= 1) != 0) {
250 // Degrade the UDMA mode if the mode has been disqualified
252 if (DisUDmaMode
!= NULL
) {
253 if (*DisUDmaMode
== 0) {
255 return EFI_UNSUPPORTED
; // no mode below ATA_UDMA_MODE_0
258 if (TempMode
>= *DisUDmaMode
) {
259 TempMode
= (UINT16
) (*DisUDmaMode
- 1);
264 // Possible returned mode is between ATA_UDMA_MODE_0 and ATA_UDMA_MODE_5
266 *SelectedMode
= TempMode
;
272 The Entry Point of module. It follows the standard UEFI driver model.
274 @param[in] ImageHandle The firmware allocated handle for the EFI image.
275 @param[in] SystemTable A pointer to the EFI System Table.
277 @retval EFI_SUCCESS The entry point is executed successfully.
278 @retval other Some error occurs when executing this entry point.
283 InitializeSataControllerDriver (
284 IN EFI_HANDLE ImageHandle
,
285 IN EFI_SYSTEM_TABLE
*SystemTable
291 // Install driver model protocol(s).
293 Status
= EfiLibInstallDriverBindingComponentName2 (
296 &gSataControllerDriverBinding
,
298 &gSataControllerComponentName
,
299 &gSataControllerComponentName2
301 ASSERT_EFI_ERROR (Status
);
307 Supported function of Driver Binding protocol for this driver.
308 Test to see if this driver supports ControllerHandle.
310 @param This Protocol instance pointer.
311 @param Controller Handle of device to test.
312 @param RemainingDevicePath A pointer to the device path.
313 it should be ignored by device driver.
315 @retval EFI_SUCCESS This driver supports this device.
316 @retval EFI_ALREADY_STARTED This driver is already running on this device.
317 @retval other This driver does not support this device.
322 SataControllerSupported (
323 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
324 IN EFI_HANDLE Controller
,
325 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
329 EFI_PCI_IO_PROTOCOL
*PciIo
;
333 // Attempt to open PCI I/O Protocol
335 Status
= gBS
->OpenProtocol (
337 &gEfiPciIoProtocolGuid
,
339 This
->DriverBindingHandle
,
341 EFI_OPEN_PROTOCOL_GET_PROTOCOL
343 if (EFI_ERROR (Status
)) {
348 // Now further check the PCI header: Base Class (offset 0x0B) and
349 // Sub Class (offset 0x0A). This controller should be an SATA controller
351 Status
= PciIo
->Pci
.Read (
354 PCI_CLASSCODE_OFFSET
,
355 sizeof (PciData
.Hdr
.ClassCode
),
356 PciData
.Hdr
.ClassCode
358 if (EFI_ERROR (Status
)) {
359 return EFI_UNSUPPORTED
;
362 if (IS_PCI_IDE (&PciData
) || IS_PCI_SATADPA (&PciData
)) {
366 return EFI_UNSUPPORTED
;
370 This routine is called right after the .Supported() called and
371 Start this driver on ControllerHandle.
373 @param This Protocol instance pointer.
374 @param Controller Handle of device to bind driver to.
375 @param RemainingDevicePath A pointer to the device path.
376 it should be ignored by device driver.
378 @retval EFI_SUCCESS This driver is added to this device.
379 @retval EFI_ALREADY_STARTED This driver is already running on this device.
380 @retval other Some error occurs when binding this driver to this device.
385 SataControllerStart (
386 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
387 IN EFI_HANDLE Controller
,
388 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
392 EFI_PCI_IO_PROTOCOL
*PciIo
;
394 EFI_SATA_CONTROLLER_PRIVATE_DATA
*SataPrivateData
;
396 UINTN ChannelDeviceCount
;
398 DEBUG ((EFI_D_INFO
, "SataControllerStart START\n"));
400 SataPrivateData
= NULL
;
403 // Now test and open PCI I/O Protocol
405 Status
= gBS
->OpenProtocol (
407 &gEfiPciIoProtocolGuid
,
409 This
->DriverBindingHandle
,
411 EFI_OPEN_PROTOCOL_BY_DRIVER
413 if (EFI_ERROR (Status
)) {
418 // Allocate Sata Private Data structure
420 SataPrivateData
= AllocateZeroPool (sizeof (EFI_SATA_CONTROLLER_PRIVATE_DATA
));
421 if (SataPrivateData
== NULL
) {
422 Status
= EFI_OUT_OF_RESOURCES
;
427 // Initialize Sata Private Data
429 SataPrivateData
->Signature
= SATA_CONTROLLER_SIGNATURE
;
430 SataPrivateData
->PciIo
= PciIo
;
431 SataPrivateData
->IdeInit
.GetChannelInfo
= IdeInitGetChannelInfo
;
432 SataPrivateData
->IdeInit
.NotifyPhase
= IdeInitNotifyPhase
;
433 SataPrivateData
->IdeInit
.SubmitData
= IdeInitSubmitData
;
434 SataPrivateData
->IdeInit
.DisqualifyMode
= IdeInitDisqualifyMode
;
435 SataPrivateData
->IdeInit
.CalculateMode
= IdeInitCalculateMode
;
436 SataPrivateData
->IdeInit
.SetTiming
= IdeInitSetTiming
;
437 SataPrivateData
->IdeInit
.EnumAll
= SATA_ENUMER_ALL
;
439 Status
= PciIo
->Pci
.Read (
442 PCI_CLASSCODE_OFFSET
,
443 sizeof (PciData
.Hdr
.ClassCode
),
444 PciData
.Hdr
.ClassCode
446 if (EFI_ERROR (Status
)) {
447 goto FreeSataPrivateData
;
450 if (IS_PCI_IDE (&PciData
)) {
451 SataPrivateData
->IdeInit
.ChannelCount
= IDE_MAX_CHANNEL
;
452 SataPrivateData
->DeviceCount
= IDE_MAX_DEVICES
;
453 } else if (IS_PCI_SATADPA (&PciData
)) {
455 // Read Host Capability Register(CAP) to get Number of Ports(NPS) and Supports Port Multiplier(SPM)
456 // NPS is 0's based value indicating the maximum number of ports supported by the HBA silicon.
457 // A maximum of 32 ports can be supported. A value of '0h', indicating one port, is the minimum requirement.
459 Data32
= AhciReadReg (PciIo
, R_AHCI_CAP
);
460 SataPrivateData
->IdeInit
.ChannelCount
= (UINT8
) ((Data32
& B_AHCI_CAP_NPS
) + 1);
461 SataPrivateData
->DeviceCount
= AHCI_MAX_DEVICES
;
462 if ((Data32
& B_AHCI_CAP_SPM
) == B_AHCI_CAP_SPM
) {
463 SataPrivateData
->DeviceCount
= AHCI_MULTI_MAX_DEVICES
;
467 ChannelDeviceCount
= (UINTN
) (SataPrivateData
->IdeInit
.ChannelCount
) * (UINTN
) (SataPrivateData
->DeviceCount
);
468 SataPrivateData
->DisqualifiedModes
= AllocateZeroPool ((sizeof (EFI_ATA_COLLECTIVE_MODE
)) * ChannelDeviceCount
);
469 if (SataPrivateData
->DisqualifiedModes
== NULL
) {
470 Status
= EFI_OUT_OF_RESOURCES
;
471 goto FreeSataPrivateData
;
474 SataPrivateData
->IdentifyData
= AllocateZeroPool ((sizeof (EFI_IDENTIFY_DATA
)) * ChannelDeviceCount
);
475 if (SataPrivateData
->IdentifyData
== NULL
) {
476 Status
= EFI_OUT_OF_RESOURCES
;
477 goto FreeDisqualifiedModes
;
480 SataPrivateData
->IdentifyValid
= AllocateZeroPool ((sizeof (BOOLEAN
)) * ChannelDeviceCount
);
481 if (SataPrivateData
->IdentifyValid
== NULL
) {
482 Status
= EFI_OUT_OF_RESOURCES
;
483 goto FreeIdentifyData
;
487 // Install IDE Controller Init Protocol to this instance
489 Status
= gBS
->InstallMultipleProtocolInterfaces (
491 &gEfiIdeControllerInitProtocolGuid
,
492 &(SataPrivateData
->IdeInit
),
496 if (EFI_ERROR (Status
)) {
497 goto FreeIdentifyValid
;
500 DEBUG ((EFI_D_INFO
, "SataControllerStart END status = %r\n", Status
));
504 FreePool (SataPrivateData
->IdentifyValid
);
507 FreePool (SataPrivateData
->IdentifyData
);
509 FreeDisqualifiedModes
:
510 FreePool (SataPrivateData
->DisqualifiedModes
);
513 FreePool (SataPrivateData
);
518 &gEfiPciIoProtocolGuid
,
519 This
->DriverBindingHandle
,
524 DEBUG ((EFI_D_ERROR
, "SataControllerStart error return status = %r\n", Status
));
529 Stop this driver on ControllerHandle.
531 @param This Protocol instance pointer.
532 @param Controller Handle of device to stop driver on.
533 @param NumberOfChildren Not used.
534 @param ChildHandleBuffer Not used.
536 @retval EFI_SUCCESS This driver is removed from this device.
537 @retval other Some error occurs when removing this driver from this device.
543 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
544 IN EFI_HANDLE Controller
,
545 IN UINTN NumberOfChildren
,
546 IN EFI_HANDLE
*ChildHandleBuffer
550 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeInit
;
551 EFI_SATA_CONTROLLER_PRIVATE_DATA
*SataPrivateData
;
554 // Open the produced protocol
556 Status
= gBS
->OpenProtocol (
558 &gEfiIdeControllerInitProtocolGuid
,
560 This
->DriverBindingHandle
,
562 EFI_OPEN_PROTOCOL_GET_PROTOCOL
564 if (EFI_ERROR (Status
)) {
565 return EFI_UNSUPPORTED
;
568 SataPrivateData
= SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (IdeInit
);
569 ASSERT (SataPrivateData
!= NULL
);
572 // Uninstall the IDE Controller Init Protocol from this instance
574 Status
= gBS
->UninstallMultipleProtocolInterfaces (
576 &gEfiIdeControllerInitProtocolGuid
,
577 &(SataPrivateData
->IdeInit
),
580 if (EFI_ERROR (Status
)) {
584 if (SataPrivateData
!= NULL
) {
585 if (SataPrivateData
->DisqualifiedModes
!= NULL
) {
586 FreePool (SataPrivateData
->DisqualifiedModes
);
588 if (SataPrivateData
->IdentifyData
!= NULL
) {
589 FreePool (SataPrivateData
->IdentifyData
);
591 if (SataPrivateData
->IdentifyValid
!= NULL
) {
592 FreePool (SataPrivateData
->IdentifyValid
);
594 FreePool (SataPrivateData
);
598 // Close protocols opened by Sata Controller driver
600 return gBS
->CloseProtocol (
602 &gEfiPciIoProtocolGuid
,
603 This
->DriverBindingHandle
,
609 Calculate the flat array subscript of a (Channel, Device) pair.
611 @param[in] SataPrivateData The private data structure corresponding to the
612 SATA controller that attaches the device for
613 which the flat array subscript is being
616 @param[in] Channel The channel (ie. port) number on the SATA
617 controller that the device is attached to.
619 @param[in] Device The device number on the channel.
621 @return The flat array subscript suitable for indexing DisqualifiedModes,
622 IdentifyData, and IdentifyValid.
627 IN CONST EFI_SATA_CONTROLLER_PRIVATE_DATA
*SataPrivateData
,
632 ASSERT (SataPrivateData
!= NULL
);
633 ASSERT (Channel
< SataPrivateData
->IdeInit
.ChannelCount
);
634 ASSERT (Device
< SataPrivateData
->DeviceCount
);
636 return Channel
* SataPrivateData
->DeviceCount
+ Device
;
640 // Interface functions of IDE_CONTROLLER_INIT protocol
643 Returns the information about the specified IDE channel.
645 This function can be used to obtain information about a particular IDE channel.
646 The driver entity uses this information during the enumeration process.
648 If Enabled is set to FALSE, the driver entity will not scan the channel. Note
649 that it will not prevent an operating system driver from scanning the channel.
651 For most of today's controllers, MaxDevices will either be 1 or 2. For SATA
652 controllers, this value will always be 1. SATA configurations can contain SATA
653 port multipliers. SATA port multipliers behave like SATA bridges and can support
654 up to 16 devices on the other side. If a SATA port out of the IDE controller
655 is connected to a port multiplier, MaxDevices will be set to the number of SATA
656 devices that the port multiplier supports. Because today's port multipliers
657 support up to fifteen SATA devices, this number can be as large as fifteen. The IDE
658 bus driver is required to scan for the presence of port multipliers behind an SATA
659 controller and enumerate up to MaxDevices number of devices behind the port
662 In this context, the devices behind a port multiplier constitute a channel.
664 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
665 @param[in] Channel Zero-based channel number.
666 @param[out] Enabled TRUE if this channel is enabled. Disabled channels
667 are not scanned to see if any devices are present.
668 @param[out] MaxDevices The maximum number of IDE devices that the bus driver
669 can expect on this channel. For the ATA/ATAPI
670 specification, version 6, this number will either be
671 one or two. For Serial ATA (SATA) configurations with a
672 port multiplier, this number can be as large as fifteen.
674 @retval EFI_SUCCESS Information was returned without any errors.
675 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
680 IdeInitGetChannelInfo (
681 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
683 OUT BOOLEAN
*Enabled
,
684 OUT UINT8
*MaxDevices
687 EFI_SATA_CONTROLLER_PRIVATE_DATA
*SataPrivateData
;
688 SataPrivateData
= SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This
);
689 ASSERT (SataPrivateData
!= NULL
);
691 if (Channel
< This
->ChannelCount
) {
693 *MaxDevices
= SataPrivateData
->DeviceCount
;
698 return EFI_INVALID_PARAMETER
;
702 The notifications from the driver entity that it is about to enter a certain
703 phase of the IDE channel enumeration process.
705 This function can be used to notify the IDE controller driver to perform
706 specific actions, including any chipset-specific initialization, so that the
707 chipset is ready to enter the next phase. Seven notification points are defined
710 More synchronization points may be added as required in the future.
712 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
713 @param[in] Phase The phase during enumeration.
714 @param[in] Channel Zero-based channel number.
716 @retval EFI_SUCCESS The notification was accepted without any errors.
717 @retval EFI_UNSUPPORTED Phase is not supported.
718 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
719 @retval EFI_NOT_READY This phase cannot be entered at this time; for
720 example, an attempt was made to enter a Phase
721 without having entered one or more previous
728 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
729 IN EFI_IDE_CONTROLLER_ENUM_PHASE Phase
,
737 Submits the device information to the IDE controller driver.
739 This function is used by the driver entity to pass detailed information about
740 a particular device to the IDE controller driver. The driver entity obtains
741 this information by issuing an ATA or ATAPI IDENTIFY_DEVICE command. IdentifyData
742 is the pointer to the response data buffer. The IdentifyData buffer is owned
743 by the driver entity, and the IDE controller driver must make a local copy
744 of the entire buffer or parts of the buffer as needed. The original IdentifyData
745 buffer pointer may not be valid when
747 - EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() or
748 - EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() is called at a later point.
750 The IDE controller driver may consult various fields of EFI_IDENTIFY_DATA to
751 compute the optimum mode for the device. These fields are not limited to the
752 timing information. For example, an implementation of the IDE controller driver
753 may examine the vendor and type/mode field to match known bad drives.
755 The driver entity may submit drive information in any order, as long as it
756 submits information for all the devices belonging to the enumeration group
757 before EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() is called for any device
758 in that enumeration group. If a device is absent, EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
759 should be called with IdentifyData set to NULL. The IDE controller driver may
760 not have any other mechanism to know whether a device is present or not. Therefore,
761 setting IdentifyData to NULL does not constitute an error condition.
762 EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData() can be called only once for a
763 given (Channel, Device) pair.
765 @param[in] This A pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
766 @param[in] Channel Zero-based channel number.
767 @param[in] Device Zero-based device number on the Channel.
768 @param[in] IdentifyData The device's response to the ATA IDENTIFY_DEVICE command.
770 @retval EFI_SUCCESS The information was accepted without any errors.
771 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
772 @retval EFI_INVALID_PARAMETER Device is invalid.
778 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
781 IN EFI_IDENTIFY_DATA
*IdentifyData
784 EFI_SATA_CONTROLLER_PRIVATE_DATA
*SataPrivateData
;
787 SataPrivateData
= SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This
);
788 ASSERT (SataPrivateData
!= NULL
);
790 if ((Channel
>= This
->ChannelCount
) || (Device
>= SataPrivateData
->DeviceCount
)) {
791 return EFI_INVALID_PARAMETER
;
794 DeviceIndex
= FlatDeviceIndex (SataPrivateData
, Channel
, Device
);
797 // Make a local copy of device's IdentifyData and mark the valid flag
799 if (IdentifyData
!= NULL
) {
801 &(SataPrivateData
->IdentifyData
[DeviceIndex
]),
803 sizeof (EFI_IDENTIFY_DATA
)
806 SataPrivateData
->IdentifyValid
[DeviceIndex
] = TRUE
;
808 SataPrivateData
->IdentifyValid
[DeviceIndex
] = FALSE
;
815 Disqualifies specific modes for an IDE device.
817 This function allows the driver entity or other drivers (such as platform
818 drivers) to reject certain timing modes and request the IDE controller driver
819 to recalculate modes. This function allows the driver entity and the IDE
820 controller driver to negotiate the timings on a per-device basis. This function
821 is useful in the case of drives that lie about their capabilities. An example
822 is when the IDE device fails to accept the timing modes that are calculated
823 by the IDE controller driver based on the response to the Identify Drive command.
825 If the driver entity does not want to limit the ATA timing modes and leave that
826 decision to the IDE controller driver, it can either not call this function for
827 the given device or call this function and set the Valid flag to FALSE for all
828 modes that are listed in EFI_ATA_COLLECTIVE_MODE.
830 The driver entity may disqualify modes for a device in any order and any number
833 This function can be called multiple times to invalidate multiple modes of the
834 same type (e.g., Programmed Input/Output [PIO] modes 3 and 4). See the ATA/ATAPI
835 specification for more information on PIO modes.
837 For Serial ATA (SATA) controllers, this member function can be used to disqualify
838 a higher transfer rate mode on a given channel. For example, a platform driver
839 may inform the IDE controller driver to not use second-generation (Gen2) speeds
840 for a certain SATA drive.
842 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
843 @param[in] Channel The zero-based channel number.
844 @param[in] Device The zero-based device number on the Channel.
845 @param[in] BadModes The modes that the device does not support and that
846 should be disqualified.
848 @retval EFI_SUCCESS The modes were accepted without any errors.
849 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
850 @retval EFI_INVALID_PARAMETER Device is invalid.
851 @retval EFI_INVALID_PARAMETER IdentifyData is NULL.
856 IdeInitDisqualifyMode (
857 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
860 IN EFI_ATA_COLLECTIVE_MODE
*BadModes
863 EFI_SATA_CONTROLLER_PRIVATE_DATA
*SataPrivateData
;
866 SataPrivateData
= SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This
);
867 ASSERT (SataPrivateData
!= NULL
);
869 if ((Channel
>= This
->ChannelCount
) || (BadModes
== NULL
) || (Device
>= SataPrivateData
->DeviceCount
)) {
870 return EFI_INVALID_PARAMETER
;
873 DeviceIndex
= FlatDeviceIndex (SataPrivateData
, Channel
, Device
);
876 // Record the disqualified modes per channel per device. From ATA/ATAPI spec,
877 // if a mode is not supported, the modes higher than it is also not supported.
880 &(SataPrivateData
->DisqualifiedModes
[DeviceIndex
]),
882 sizeof (EFI_ATA_COLLECTIVE_MODE
)
889 Returns the information about the optimum modes for the specified IDE device.
891 This function is used by the driver entity to obtain the optimum ATA modes for
892 a specific device. The IDE controller driver takes into account the following
893 while calculating the mode:
894 - The IdentifyData inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
895 - The BadModes inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode()
897 The driver entity is required to call EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
898 for all the devices that belong to an enumeration group before calling
899 EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() for any device in the same group.
901 The IDE controller driver will use controller- and possibly platform-specific
902 algorithms to arrive at SupportedModes. The IDE controller may base its
903 decision on user preferences and other considerations as well. This function
904 may be called multiple times because the driver entity may renegotiate the mode
905 with the IDE controller driver using EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode().
907 The driver entity may collect timing information for various devices in any
908 order. The driver entity is responsible for making sure that all the dependencies
909 are satisfied. For example, the SupportedModes information for device A that
910 was previously returned may become stale after a call to
911 EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() for device B.
913 The buffer SupportedModes is allocated by the callee because the caller does
914 not necessarily know the size of the buffer. The type EFI_ATA_COLLECTIVE_MODE
915 is defined in a way that allows for future extensibility and can be of variable
916 length. This memory pool should be deallocated by the caller when it is no
919 The IDE controller driver for a Serial ATA (SATA) controller can use this
920 member function to force a lower speed (first-generation [Gen1] speeds on a
921 second-generation [Gen2]-capable hardware). The IDE controller driver can
922 also allow the driver entity to stay with the speed that has been negotiated
923 by the physical layer.
925 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
926 @param[in] Channel A zero-based channel number.
927 @param[in] Device A zero-based device number on the Channel.
928 @param[out] SupportedModes The optimum modes for the device.
930 @retval EFI_SUCCESS SupportedModes was returned.
931 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
932 @retval EFI_INVALID_PARAMETER Device is invalid.
933 @retval EFI_INVALID_PARAMETER SupportedModes is NULL.
934 @retval EFI_NOT_READY Modes cannot be calculated due to a lack of
935 data. This error may happen if
936 EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
937 and EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyData()
938 were not called for at least one drive in the
939 same enumeration group.
944 IdeInitCalculateMode (
945 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
948 OUT EFI_ATA_COLLECTIVE_MODE
**SupportedModes
951 EFI_SATA_CONTROLLER_PRIVATE_DATA
*SataPrivateData
;
952 EFI_IDENTIFY_DATA
*IdentifyData
;
953 BOOLEAN IdentifyValid
;
954 EFI_ATA_COLLECTIVE_MODE
*DisqualifiedModes
;
959 SataPrivateData
= SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS (This
);
960 ASSERT (SataPrivateData
!= NULL
);
962 if ((Channel
>= This
->ChannelCount
) || (SupportedModes
== NULL
) || (Device
>= SataPrivateData
->DeviceCount
)) {
963 return EFI_INVALID_PARAMETER
;
966 *SupportedModes
= AllocateZeroPool (sizeof (EFI_ATA_COLLECTIVE_MODE
));
967 if (*SupportedModes
== NULL
) {
968 ASSERT (*SupportedModes
!= NULL
);
969 return EFI_OUT_OF_RESOURCES
;
972 DeviceIndex
= FlatDeviceIndex (SataPrivateData
, Channel
, Device
);
974 IdentifyData
= &(SataPrivateData
->IdentifyData
[DeviceIndex
]);
975 IdentifyValid
= SataPrivateData
->IdentifyValid
[DeviceIndex
];
976 DisqualifiedModes
= &(SataPrivateData
->DisqualifiedModes
[DeviceIndex
]);
979 // Make sure we've got the valid identify data of the device from SubmitData()
981 if (!IdentifyValid
) {
982 FreePool (*SupportedModes
);
983 return EFI_NOT_READY
;
986 Status
= CalculateBestPioMode (
988 (DisqualifiedModes
->PioMode
.Valid
? ((UINT16
*) &(DisqualifiedModes
->PioMode
.Mode
)) : NULL
),
991 if (!EFI_ERROR (Status
)) {
992 (*SupportedModes
)->PioMode
.Valid
= TRUE
;
993 (*SupportedModes
)->PioMode
.Mode
= SelectedMode
;
996 (*SupportedModes
)->PioMode
.Valid
= FALSE
;
998 DEBUG ((EFI_D_INFO
, "IdeInitCalculateMode: PioMode = %x\n", (*SupportedModes
)->PioMode
.Mode
));
1000 Status
= CalculateBestUdmaMode (
1002 (DisqualifiedModes
->UdmaMode
.Valid
? ((UINT16
*) &(DisqualifiedModes
->UdmaMode
.Mode
)) : NULL
),
1006 if (!EFI_ERROR (Status
)) {
1007 (*SupportedModes
)->UdmaMode
.Valid
= TRUE
;
1008 (*SupportedModes
)->UdmaMode
.Mode
= SelectedMode
;
1011 (*SupportedModes
)->UdmaMode
.Valid
= FALSE
;
1013 DEBUG ((EFI_D_INFO
, "IdeInitCalculateMode: UdmaMode = %x\n", (*SupportedModes
)->UdmaMode
.Mode
));
1016 // The modes other than PIO and UDMA are not supported
1022 Commands the IDE controller driver to program the IDE controller hardware
1023 so that the specified device can operate at the specified mode.
1025 This function is used by the driver entity to instruct the IDE controller
1026 driver to program the IDE controller hardware to the specified modes. This
1027 function can be called only once for a particular device. For a Serial ATA
1028 (SATA) Advanced Host Controller Interface (AHCI) controller, no controller-
1029 specific programming may be required.
1031 @param[in] This Pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
1032 @param[in] Channel Zero-based channel number.
1033 @param[in] Device Zero-based device number on the Channel.
1034 @param[in] Modes The modes to set.
1036 @retval EFI_SUCCESS The command was accepted without any errors.
1037 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
1038 @retval EFI_INVALID_PARAMETER Device is invalid.
1039 @retval EFI_NOT_READY Modes cannot be set at this time due to lack of data.
1040 @retval EFI_DEVICE_ERROR Modes cannot be set due to hardware failure.
1041 The driver entity should not use this device.
1047 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL
*This
,
1050 IN EFI_ATA_COLLECTIVE_MODE
*Modes