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