1 /** @file
2   Header file for Sata Controller driver.
3 
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
9 
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.
12 
13 **/
14 
15 #ifndef _SATA_CONTROLLER_H_
16 #define _SATA_CONTROLLER_H_
17 
18 #include <Uefi.h>
19 #include <Protocol/ComponentName.h>
20 #include <Protocol/DriverBinding.h>
21 #include <Protocol/PciIo.h>
22 #include <Protocol/IdeControllerInit.h>
23 #include <Library/UefiDriverEntryPoint.h>
24 #include <Library/DebugLib.h>
25 #include <Library/UefiLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <IndustryStandard/Pci.h>
31 
32 //
33 // Global Variables definitions
34 //
35 extern EFI_DRIVER_BINDING_PROTOCOL  gSataControllerDriverBinding;
36 extern EFI_COMPONENT_NAME_PROTOCOL  gSataControllerComponentName;
37 extern EFI_COMPONENT_NAME2_PROTOCOL gSataControllerComponentName2;
38 
39 #define AHCI_BAR_INDEX 0x05
40 #define R_AHCI_CAP 0x0
41 #define   B_AHCI_CAP_NPS (BIT4 | BIT3 | BIT2 | BIT1 | BIT0) // Number of Ports
42 #define   B_AHCI_CAP_SPM BIT17 // Supports Port Multiplier
43 
44 ///
45 /// AHCI each channel can have up to 1 device
46 ///
47 #define AHCI_MAX_DEVICES 0x01
48 
49 ///
50 /// AHCI each channel can have 15 devices in the presence of a multiplier
51 ///
52 #define AHCI_MULTI_MAX_DEVICES 0x0F
53 
54 ///
55 /// IDE supports 2 channel max
56 ///
57 #define IDE_MAX_CHANNEL 0x02
58 
59 ///
60 /// IDE supports 2 devices max
61 ///
62 #define IDE_MAX_DEVICES 0x02
63 
64 #define SATA_ENUMER_ALL FALSE
65 
66 //
67 // Sata Controller driver private data structure
68 //
69 
70 #define SATA_CONTROLLER_SIGNATURE SIGNATURE_32('S','A','T','A')
71 
72 typedef struct _EFI_SATA_CONTROLLER_PRIVATE_DATA {
73   //
74   // Standard signature used to identify Sata Controller private data
75   //
76   UINT32                            Signature;
77 
78   //
79   // Protocol instance of IDE_CONTROLLER_INIT produced by this driver
80   //
81   EFI_IDE_CONTROLLER_INIT_PROTOCOL  IdeInit;
82 
83   //
84   // Copy of protocol pointers used by this driver
85   //
86   EFI_PCI_IO_PROTOCOL               *PciIo;
87 
88   //
89   // Original PCI attributes
90   //
91   UINT64                            OriginalPciAttributes;
92 
93   //
94   // The number of devices that are supported by this channel
95   //
96   UINT8                             DeviceCount;
97 
98   //
99   // The highest disqulified mode for each attached device,
100   // From ATA/ATAPI spec, if a mode is not supported,
101   // the modes higher than it is also not supported
102   //
103   EFI_ATA_COLLECTIVE_MODE           *DisqualifiedModes;
104 
105   //
106   // A copy of EFI_IDENTIFY_DATA data for each attached SATA device and its flag
107   //
108   EFI_IDENTIFY_DATA                 *IdentifyData;
109   BOOLEAN                           *IdentifyValid;
110 } EFI_SATA_CONTROLLER_PRIVATE_DATA;
111 
112 #define SATA_CONTROLLER_PRIVATE_DATA_FROM_THIS(a) CR(a, EFI_SATA_CONTROLLER_PRIVATE_DATA, IdeInit, SATA_CONTROLLER_SIGNATURE)
113 
114 //
115 // Driver binding functions declaration
116 //
117 /**
118   Supported function of Driver Binding protocol for this driver.
119   Test to see if this driver supports ControllerHandle.
120 
121   @param This                   Protocol instance pointer.
122   @param Controller             Handle of device to test.
123   @param RemainingDevicePath    A pointer to the device path. Should be ignored by
124                                 device driver.
125 
126   @retval EFI_SUCCESS           This driver supports this device.
127   @retval EFI_ALREADY_STARTED   This driver is already running on this device.
128   @retval other                 This driver does not support this device.
129 
130 **/
131 EFI_STATUS
132 EFIAPI
133 SataControllerSupported (
134   IN EFI_DRIVER_BINDING_PROTOCOL    *This,
135   IN EFI_HANDLE                     Controller,
136   IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
137   )
138 ;
139 
140 /**
141   This routine is called right after the .Supported() called and
142   Start this driver on ControllerHandle.
143 
144   @param This                   Protocol instance pointer.
145   @param Controller             Handle of device to bind driver to.
146   @param RemainingDevicePath    A pointer to the device path. Should be ignored by
147                                 device driver.
148 
149   @retval EFI_SUCCESS           This driver is added to this device.
150   @retval EFI_ALREADY_STARTED   This driver is already running on this device.
151   @retval other                 Some error occurs when binding this driver to this device.
152 
153 **/
154 EFI_STATUS
155 EFIAPI
156 SataControllerStart (
157   IN EFI_DRIVER_BINDING_PROTOCOL    *This,
158   IN EFI_HANDLE                     Controller,
159   IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
160   )
161 ;
162 
163 /**
164   Stop this driver on ControllerHandle.
165 
166   @param This               Protocol instance pointer.
167   @param Controller         Handle of device to stop driver on.
168   @param NumberOfChildren   Not used.
169   @param ChildHandleBuffer  Not used.
170 
171   @retval EFI_SUCCESS   This driver is removed from this device.
172   @retval other         Some error occurs when removing this driver from this device.
173 
174 **/
175 EFI_STATUS
176 EFIAPI
177 SataControllerStop (
178   IN EFI_DRIVER_BINDING_PROTOCOL    *This,
179   IN EFI_HANDLE                     Controller,
180   IN UINTN                          NumberOfChildren,
181   IN EFI_HANDLE                     *ChildHandleBuffer
182   )
183 ;
184 
185 //
186 // IDE controller init functions declaration
187 //
188 /**
189   Returns the information about the specified IDE channel.
190 
191   This function can be used to obtain information about a particular IDE channel.
192   The driver entity uses this information during the enumeration process.
193 
194   If Enabled is set to FALSE, the driver entity will not scan the channel. Note
195   that it will not prevent an operating system driver from scanning the channel.
196 
197   For most of today's controllers, MaxDevices will either be 1 or 2. For SATA
198   controllers, this value will always be 1. SATA configurations can contain SATA
199   port multipliers. SATA port multipliers behave like SATA bridges and can support
200   up to 16 devices on the other side. If a SATA port out of the IDE controller
201   is connected to a port multiplier, MaxDevices will be set to the number of SATA
202   devices that the port multiplier supports. Because today's port multipliers
203   support up to fifteen SATA devices, this number can be as large as fifteen. The IDE
204   bus driver is required to scan for the presence of port multipliers behind an SATA
205   controller and enumerate up to MaxDevices number of devices behind the port
206   multiplier.
207 
208   In this context, the devices behind a port multiplier constitute a channel.
209 
210   @param[in]  This         The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
211   @param[in]  Channel      Zero-based channel number.
212   @param[out] Enabled      TRUE if this channel is enabled.  Disabled channels
213                            are not scanned to see if any devices are present.
214   @param[out] MaxDevices   The maximum number of IDE devices that the bus driver
215                            can expect on this channel.  For the ATA/ATAPI
216                            specification, version 6, this number will either be
217                            one or two. For Serial ATA (SATA) configurations with a
218                            port multiplier, this number can be as large as fifteen.
219 
220   @retval EFI_SUCCESS             Information was returned without any errors.
221   @retval EFI_INVALID_PARAMETER   Channel is invalid (Channel >= ChannelCount).
222 
223 **/
224 EFI_STATUS
225 EFIAPI
226 IdeInitGetChannelInfo (
227   IN EFI_IDE_CONTROLLER_INIT_PROTOCOL   *This,
228   IN UINT8                              Channel,
229   OUT BOOLEAN                           *Enabled,
230   OUT UINT8                             *MaxDevices
231   )
232 ;
233 
234 /**
235   The notifications from the driver entity that it is about to enter a certain
236   phase of the IDE channel enumeration process.
237 
238   This function can be used to notify the IDE controller driver to perform
239   specific actions, including any chipset-specific initialization, so that the
240   chipset is ready to enter the next phase. Seven notification points are defined
241   at this time.
242 
243   More synchronization points may be added as required in the future.
244 
245   @param[in] This      The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
246   @param[in] Phase     The phase during enumeration.
247   @param[in] Channel   Zero-based channel number.
248 
249   @retval EFI_SUCCESS             The notification was accepted without any errors.
250   @retval EFI_UNSUPPORTED         Phase is not supported.
251   @retval EFI_INVALID_PARAMETER   Channel is invalid (Channel >= ChannelCount).
252   @retval EFI_NOT_READY           This phase cannot be entered at this time; for
253                                   example, an attempt was made to enter a Phase
254                                   without having entered one or more previous
255                                   Phase.
256 
257 **/
258 EFI_STATUS
259 EFIAPI
260 IdeInitNotifyPhase (
261   IN EFI_IDE_CONTROLLER_INIT_PROTOCOL   *This,
262   IN EFI_IDE_CONTROLLER_ENUM_PHASE      Phase,
263   IN UINT8                              Channel
264   )
265 ;
266 
267 /**
268   Submits the device information to the IDE controller driver.
269 
270   This function is used by the driver entity to pass detailed information about
271   a particular device to the IDE controller driver. The driver entity obtains
272   this information by issuing an ATA or ATAPI IDENTIFY_DEVICE command. IdentifyData
273   is the pointer to the response data buffer. The IdentifyData buffer is owned
274   by the driver entity, and the IDE controller driver must make a local copy
275   of the entire buffer or parts of the buffer as needed. The original IdentifyData
276   buffer pointer may not be valid when
277 
278     - EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() or
279     - EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() is called at a later point.
280 
281   The IDE controller driver may consult various fields of EFI_IDENTIFY_DATA to
282   compute the optimum mode for the device. These fields are not limited to the
283   timing information. For example, an implementation of the IDE controller driver
284   may examine the vendor and type/mode field to match known bad drives.
285 
286   The driver entity may submit drive information in any order, as long as it
287   submits information for all the devices belonging to the enumeration group
288   before EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() is called for any device
289   in that enumeration group. If a device is absent, EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
290   should be called with IdentifyData set to NULL.  The IDE controller driver may
291   not have any other mechanism to know whether a device is present or not. Therefore,
292   setting IdentifyData to NULL does not constitute an error condition.
293   EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData() can be called only once for a
294   given (Channel, Device) pair.
295 
296   @param[in] This           A pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
297   @param[in] Channel        Zero-based channel number.
298   @param[in] Device         Zero-based device number on the Channel.
299   @param[in] IdentifyData   The device's response to the ATA IDENTIFY_DEVICE command.
300 
301   @retval EFI_SUCCESS             The information was accepted without any errors.
302   @retval EFI_INVALID_PARAMETER   Channel is invalid (Channel >= ChannelCount).
303   @retval EFI_INVALID_PARAMETER   Device is invalid.
304 
305 **/
306 EFI_STATUS
307 EFIAPI
308 IdeInitSubmitData (
309   IN EFI_IDE_CONTROLLER_INIT_PROTOCOL   *This,
310   IN UINT8                              Channel,
311   IN UINT8                              Device,
312   IN EFI_IDENTIFY_DATA                  *IdentifyData
313   )
314 ;
315 
316 /**
317   Disqualifies specific modes for an IDE device.
318 
319   This function allows the driver entity or other drivers (such as platform
320   drivers) to reject certain timing modes and request the IDE controller driver
321   to recalculate modes. This function allows the driver entity and the IDE
322   controller driver to negotiate the timings on a per-device basis. This function
323   is useful in the case of drives that lie about their capabilities. An example
324   is when the IDE device fails to accept the timing modes that are calculated
325   by the IDE controller driver based on the response to the Identify Drive command.
326 
327   If the driver entity does not want to limit the ATA timing modes and leave that
328   decision to the IDE controller driver, it can either not call this function for
329   the given device or call this function and set the Valid flag to FALSE for all
330   modes that are listed in EFI_ATA_COLLECTIVE_MODE.
331 
332   The driver entity may disqualify modes for a device in any order and any number
333   of times.
334 
335   This function can be called multiple times to invalidate multiple modes of the
336   same type (e.g., Programmed Input/Output [PIO] modes 3 and 4). See the ATA/ATAPI
337   specification for more information on PIO modes.
338 
339   For Serial ATA (SATA) controllers, this member function can be used to disqualify
340   a higher transfer rate mode on a given channel. For example, a platform driver
341   may inform the IDE controller driver to not use second-generation (Gen2) speeds
342   for a certain SATA drive.
343 
344   @param[in] This       The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
345   @param[in] Channel    The zero-based channel number.
346   @param[in] Device     The zero-based device number on the Channel.
347   @param[in] BadModes   The modes that the device does not support and that
348                         should be disqualified.
349 
350   @retval EFI_SUCCESS             The modes were accepted without any errors.
351   @retval EFI_INVALID_PARAMETER   Channel is invalid (Channel >= ChannelCount).
352   @retval EFI_INVALID_PARAMETER   Device is invalid.
353   @retval EFI_INVALID_PARAMETER   IdentifyData is NULL.
354 
355 **/
356 EFI_STATUS
357 EFIAPI
358 IdeInitDisqualifyMode (
359   IN EFI_IDE_CONTROLLER_INIT_PROTOCOL   *This,
360   IN UINT8                              Channel,
361   IN UINT8                              Device,
362   IN EFI_ATA_COLLECTIVE_MODE            *BadModes
363   )
364 ;
365 
366 /**
367   Returns the information about the optimum modes for the specified IDE device.
368 
369   This function is used by the driver entity to obtain the optimum ATA modes for
370   a specific device.  The IDE controller driver takes into account the following
371   while calculating the mode:
372     - The IdentifyData inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
373     - The BadModes inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode()
374 
375   The driver entity is required to call EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
376   for all the devices that belong to an enumeration group before calling
377   EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() for any device in the same group.
378 
379   The IDE controller driver will use controller- and possibly platform-specific
380   algorithms to arrive at SupportedModes.  The IDE controller may base its
381   decision on user preferences and other considerations as well. This function
382   may be called multiple times because the driver entity may renegotiate the mode
383   with the IDE controller driver using EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode().
384 
385   The driver entity may collect timing information for various devices in any
386   order. The driver entity is responsible for making sure that all the dependencies
387   are satisfied. For example, the SupportedModes information for device A that
388   was previously returned may become stale after a call to
389   EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() for device B.
390 
391   The buffer SupportedModes is allocated by the callee because the caller does
392   not necessarily know the size of the buffer. The type EFI_ATA_COLLECTIVE_MODE
393   is defined in a way that allows for future extensibility and can be of variable
394   length. This memory pool should be deallocated by the caller when it is no
395   longer necessary.
396 
397   The IDE controller driver for a Serial ATA (SATA) controller can use this
398   member function to force a lower speed (first-generation [Gen1] speeds on a
399   second-generation [Gen2]-capable hardware).  The IDE controller driver can
400   also allow the driver entity to stay with the speed that has been negotiated
401   by the physical layer.
402 
403   @param[in]  This             The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
404   @param[in]  Channel          A zero-based channel number.
405   @param[in]  Device           A zero-based device number on the Channel.
406   @param[out] SupportedModes   The optimum modes for the device.
407 
408   @retval EFI_SUCCESS             SupportedModes was returned.
409   @retval EFI_INVALID_PARAMETER   Channel is invalid (Channel >= ChannelCount).
410   @retval EFI_INVALID_PARAMETER   Device is invalid.
411   @retval EFI_INVALID_PARAMETER   SupportedModes is NULL.
412   @retval EFI_NOT_READY           Modes cannot be calculated due to a lack of
413                                   data.  This error may happen if
414                                   EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
415                                   and EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyData()
416                                   were not called for at least one drive in the
417                                   same enumeration group.
418 
419 **/
420 EFI_STATUS
421 EFIAPI
422 IdeInitCalculateMode (
423   IN EFI_IDE_CONTROLLER_INIT_PROTOCOL   *This,
424   IN UINT8                              Channel,
425   IN UINT8                              Device,
426   OUT EFI_ATA_COLLECTIVE_MODE           **SupportedModes
427   )
428 ;
429 
430 /**
431   Commands the IDE controller driver to program the IDE controller hardware
432   so that the specified device can operate at the specified mode.
433 
434   This function is used by the driver entity to instruct the IDE controller
435   driver to program the IDE controller hardware to the specified modes. This
436   function can be called only once for a particular device. For a Serial ATA
437   (SATA) Advanced Host Controller Interface (AHCI) controller, no controller-
438   specific programming may be required.
439 
440   @param[in] This      Pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
441   @param[in] Channel   Zero-based channel number.
442   @param[in] Device    Zero-based device number on the Channel.
443   @param[in] Modes     The modes to set.
444 
445   @retval EFI_SUCCESS             The command was accepted without any errors.
446   @retval EFI_INVALID_PARAMETER   Channel is invalid (Channel >= ChannelCount).
447   @retval EFI_INVALID_PARAMETER   Device is invalid.
448   @retval EFI_NOT_READY           Modes cannot be set at this time due to lack of data.
449   @retval EFI_DEVICE_ERROR        Modes cannot be set due to hardware failure.
450                                   The driver entity should not use this device.
451 
452 **/
453 EFI_STATUS
454 EFIAPI
455 IdeInitSetTiming (
456   IN EFI_IDE_CONTROLLER_INIT_PROTOCOL   *This,
457   IN UINT8                              Channel,
458   IN UINT8                              Device,
459   IN EFI_ATA_COLLECTIVE_MODE            *Modes
460   )
461 ;
462 
463 //
464 // Forward reference declaration
465 //
466 /**
467   Retrieves a Unicode string that is the user readable name of the UEFI Driver.
468 
469   @param This           A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance.
470   @param Language       A pointer to a three character ISO 639-2 language identifier.
471                         This is the language of the driver name that that the caller
472                         is requesting, and it must match one of the languages specified
473                         in SupportedLanguages.  The number of languages supported by a
474                         driver is up to the driver writer.
475   @param DriverName     A pointer to the Unicode string to return.  This Unicode string
476                         is the name of the driver specified by This in the language
477                         specified by Language.
478 
479   @retval EFI_SUCCESS           The Unicode string for the Driver specified by This
480                                 and the language specified by Language was returned
481                                 in DriverName.
482   @retval EFI_INVALID_PARAMETER Language is NULL.
483   @retval EFI_INVALID_PARAMETER DriverName is NULL.
484   @retval EFI_UNSUPPORTED       The driver specified by This does not support the
485                                 language specified by Language.
486 **/
487 EFI_STATUS
488 EFIAPI
489 SataControllerComponentNameGetDriverName (
490   IN EFI_COMPONENT_NAME_PROTOCOL    *This,
491   IN CHAR8                          *Language,
492   OUT CHAR16                        **DriverName
493   )
494 ;
495 
496 /**
497   Retrieves a Unicode string that is the user readable name of the controller
498   that is being managed by an UEFI Driver.
499 
500   @param This                   A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance.
501   @param ControllerHandle       The handle of a controller that the driver specified by
502                                 This is managing.  This handle specifies the controller
503                                 whose name is to be returned.
504   @param OPTIONAL   ChildHandle The handle of the child controller to retrieve the name
505                                 of.  This is an optional parameter that may be NULL.  It
506                                 will be NULL for device drivers.  It will also be NULL
507                                 for a bus drivers that wish to retrieve the name of the
508                                 bus controller.  It will not be NULL for a bus driver
509                                 that wishes to retrieve the name of a child controller.
510   @param Language               A pointer to a three character ISO 639-2 language
511                                 identifier.  This is the language of the controller name
512                                 that that the caller is requesting, and it must match one
513                                 of the languages specified in SupportedLanguages.  The
514                                 number of languages supported by a driver is up to the
515                                 driver writer.
516   @param ControllerName         A pointer to the Unicode string to return.  This Unicode
517                                 string is the name of the controller specified by
518                                 ControllerHandle and ChildHandle in the language
519                                 specified by Language from the point of view of the
520                                 driver specified by This.
521 
522   @retval EFI_SUCCESS           The Unicode string for the user readable name in the
523                                 language specified by Language for the driver
524                                 specified by This was returned in DriverName.
525   @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
526   @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
527                                 EFI_HANDLE.
528   @retval EFI_INVALID_PARAMETER Language is NULL.
529   @retval EFI_INVALID_PARAMETER ControllerName is NULL.
530   @retval EFI_UNSUPPORTED       The driver specified by This is not currently
531                                 managing the controller specified by
532                                 ControllerHandle and ChildHandle.
533   @retval EFI_UNSUPPORTED       The driver specified by This does not support the
534                                 language specified by Language.
535 **/
536 EFI_STATUS
537 EFIAPI
538 SataControllerComponentNameGetControllerName (
539   IN EFI_COMPONENT_NAME_PROTOCOL    *This,
540   IN EFI_HANDLE                     ControllerHandle,
541   IN EFI_HANDLE                     ChildHandle OPTIONAL,
542   IN CHAR8                          *Language,
543   OUT CHAR16                        **ControllerName
544   )
545 ;
546 
547 #endif
548