]> git.proxmox.com Git - mirror_edk2.git/blob - PcAtChipsetPkg/Bus/Pci/IdeControllerDxe/IdeController.c
PcAtChipsetPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / PcAtChipsetPkg / Bus / Pci / IdeControllerDxe / IdeController.c
1 /** @file
2 This driver module produces IDE_CONTROLLER_INIT protocol and will be used by
3 IDE Bus driver to support platform dependent timing information. This driver
4 is responsible for early initialization of IDE controller.
5
6 Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "IdeController.h"
12
13 ///
14 /// EFI_DRIVER_BINDING_PROTOCOL instance
15 ///
16 EFI_DRIVER_BINDING_PROTOCOL gIdeControllerDriverBinding = {
17 IdeControllerSupported,
18 IdeControllerStart,
19 IdeControllerStop,
20 0xa,
21 NULL,
22 NULL
23 };
24
25 ///
26 /// EFI_IDE_CONTROLLER_PROVATE_DATA Template
27 ///
28 EFI_IDE_CONTROLLER_INIT_PROTOCOL gEfiIdeControllerInit = {
29 IdeInitGetChannelInfo,
30 IdeInitNotifyPhase,
31 IdeInitSubmitData,
32 IdeInitDisqualifyMode,
33 IdeInitCalculateMode,
34 IdeInitSetTiming,
35 ICH_IDE_ENUMER_ALL,
36 ICH_IDE_MAX_CHANNEL
37 };
38
39 ///
40 /// EFI_ATA_COLLECTIVE_MODE Template
41 ///
42 EFI_ATA_COLLECTIVE_MODE gEfiAtaCollectiveModeTemplate = {
43 {
44 TRUE, ///< PioMode.Valid
45 0 ///< PioMode.Mode
46 },
47 {
48 TRUE, ///< SingleWordDmaMode.Valid
49 0
50 },
51 {
52 FALSE, ///< MultiWordDmaMode.Valid
53 0
54 },
55 {
56 TRUE, ///< UdmaMode.Valid
57 0 ///< UdmaMode.Mode
58 }
59 };
60
61 /**
62 Chipset Ide Driver EntryPoint function. It follows the standard EFI driver model.
63 It's called by StartImage() of DXE Core.
64
65 @param ImageHandle While the driver image loaded be the ImageLoader(),
66 an image handle is assigned to this driver binary,
67 all activities of the driver is tied to this ImageHandle
68 @param SystemTable A pointer to the system table, for all BS(Boo Services) and
69 RT(Runtime Services)
70
71 @return EFI_STATUS Status of EfiLibInstallDriverBindingComponentName2().
72 **/
73 EFI_STATUS
74 EFIAPI
75 InitializeIdeControllerDriver (
76 IN EFI_HANDLE ImageHandle,
77 IN EFI_SYSTEM_TABLE *SystemTable
78 )
79 {
80 EFI_STATUS Status;
81
82 //
83 // Install driver model protocol(s).
84 //
85 Status = EfiLibInstallDriverBindingComponentName2 (
86 ImageHandle,
87 SystemTable,
88 &gIdeControllerDriverBinding,
89 ImageHandle,
90 &gIdeControllerComponentName,
91 &gIdeControllerComponentName2
92 );
93 ASSERT_EFI_ERROR (Status);
94
95 return Status;
96 }
97
98 /**
99 Register Driver Binding protocol for this driver.
100
101 @param This A pointer points to the Binding Protocol instance
102 @param Controller The handle of controller to be tested.
103 @param RemainingDevicePath A pointer to the device path. Ignored by device
104 driver but used by bus driver
105
106 @retval EFI_SUCCESS Driver loaded.
107 @retval !EFI_SUCCESS Driver not loaded.
108 **/
109 EFI_STATUS
110 EFIAPI
111 IdeControllerSupported (
112 IN EFI_DRIVER_BINDING_PROTOCOL *This,
113 IN EFI_HANDLE Controller,
114 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
115 )
116 {
117 EFI_STATUS Status;
118 EFI_PCI_IO_PROTOCOL *PciIo;
119 UINT8 PciClass;
120 UINT8 PciSubClass;
121
122 //
123 // Attempt to Open PCI I/O Protocol
124 //
125 Status = gBS->OpenProtocol (
126 Controller,
127 &gEfiPciIoProtocolGuid,
128 (VOID **) &PciIo,
129 This->DriverBindingHandle,
130 Controller,
131 EFI_OPEN_PROTOCOL_BY_DRIVER
132 );
133 if (EFI_ERROR (Status)) {
134 return Status;
135 }
136
137 //
138 // Now further check the PCI header: Base class (offset 0x0B) and
139 // Sub Class (offset 0x0A). This controller should be an Ide controller
140 //
141 Status = PciIo->Pci.Read (
142 PciIo,
143 EfiPciIoWidthUint8,
144 PCI_CLASSCODE_OFFSET + 2,
145 1,
146 &PciClass
147 );
148 if (EFI_ERROR (Status)) {
149 goto Done;
150 }
151
152 Status = PciIo->Pci.Read (
153 PciIo,
154 EfiPciIoWidthUint8,
155 PCI_CLASSCODE_OFFSET + 1,
156 1,
157 &PciSubClass
158 );
159 if (EFI_ERROR (Status)) {
160 goto Done;
161 }
162
163 //
164 // Examine Ide PCI Configuration table fields
165 //
166 if ((PciClass != PCI_CLASS_MASS_STORAGE) || (PciSubClass != PCI_CLASS_MASS_STORAGE_IDE)) {
167 Status = EFI_UNSUPPORTED;
168 }
169
170 Done:
171 gBS->CloseProtocol (
172 Controller,
173 &gEfiPciIoProtocolGuid,
174 This->DriverBindingHandle,
175 Controller
176 );
177
178 return Status;
179 }
180
181 /**
182 This routine is called right after the .Supported() called and return
183 EFI_SUCCESS. Notes: The supported protocols are checked but the Protocols
184 are closed.
185
186 @param This A pointer points to the Binding Protocol instance
187 @param Controller The handle of controller to be tested. Parameter
188 passed by the caller
189 @param RemainingDevicePath A pointer to the device path. Should be ignored by
190 device driver
191
192 @return EFI_STATUS Status of InstallMultipleProtocolInterfaces()
193 **/
194 EFI_STATUS
195 EFIAPI
196 IdeControllerStart (
197 IN EFI_DRIVER_BINDING_PROTOCOL *This,
198 IN EFI_HANDLE Controller,
199 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
200 )
201 {
202 EFI_STATUS Status;
203 EFI_PCI_IO_PROTOCOL *PciIo;
204
205 //
206 // Now test and open the EfiPciIoProtocol
207 //
208 Status = gBS->OpenProtocol (
209 Controller,
210 &gEfiPciIoProtocolGuid,
211 (VOID **) &PciIo,
212 This->DriverBindingHandle,
213 Controller,
214 EFI_OPEN_PROTOCOL_BY_DRIVER
215 );
216 //
217 // Status == EFI_SUCCESS - A normal execution flow, SUCCESS and the program proceeds.
218 // Status == ALREADY_STARTED - A non-zero Status code returned. It indicates
219 // that the protocol has been opened and should be treated as a
220 // normal condition and the program proceeds. The Protocol will not
221 // opened 'again' by this call.
222 // Status != ALREADY_STARTED - Error status, terminate program execution
223 //
224 if (EFI_ERROR (Status)) {
225 return Status;
226 }
227
228 //
229 // Install IDE_CONTROLLER_INIT protocol
230 //
231 return gBS->InstallMultipleProtocolInterfaces (
232 &Controller,
233 &gEfiIdeControllerInitProtocolGuid, &gEfiIdeControllerInit,
234 NULL
235 );
236 }
237
238 /**
239 Stop this driver on Controller Handle.
240
241 @param This Protocol instance pointer.
242 @param Controller Handle of device to stop driver on
243 @param NumberOfChildren Not used
244 @param ChildHandleBuffer Not used
245
246 @retval EFI_SUCCESS This driver is removed DeviceHandle
247 @retval !EFI_SUCCESS This driver was not removed from this device
248 **/
249 EFI_STATUS
250 EFIAPI
251 IdeControllerStop (
252 IN EFI_DRIVER_BINDING_PROTOCOL *This,
253 IN EFI_HANDLE Controller,
254 IN UINTN NumberOfChildren,
255 IN EFI_HANDLE *ChildHandleBuffer
256 )
257 {
258 EFI_STATUS Status;
259 EFI_IDE_CONTROLLER_INIT_PROTOCOL *IdeControllerInit;
260
261 //
262 // Open the produced protocol
263 //
264 Status = gBS->OpenProtocol (
265 Controller,
266 &gEfiIdeControllerInitProtocolGuid,
267 (VOID **) &IdeControllerInit,
268 This->DriverBindingHandle,
269 Controller,
270 EFI_OPEN_PROTOCOL_GET_PROTOCOL
271 );
272 if (EFI_ERROR (Status)) {
273 return EFI_UNSUPPORTED;
274 }
275
276 //
277 // Make sure the protocol was produced by this driver
278 //
279 if (IdeControllerInit != &gEfiIdeControllerInit) {
280 return EFI_UNSUPPORTED;
281 }
282
283 //
284 // Uninstall the IDE Controller Init Protocol
285 //
286 Status = gBS->UninstallMultipleProtocolInterfaces (
287 Controller,
288 &gEfiIdeControllerInitProtocolGuid, &gEfiIdeControllerInit,
289 NULL
290 );
291 if (EFI_ERROR (Status)) {
292 return Status;
293 }
294
295 //
296 // Close protocols opened by Ide controller driver
297 //
298 return gBS->CloseProtocol (
299 Controller,
300 &gEfiPciIoProtocolGuid,
301 This->DriverBindingHandle,
302 Controller
303 );
304 }
305
306 //
307 // Interface functions of IDE_CONTROLLER_INIT protocol
308 //
309 /**
310 Returns the information about the specified IDE channel.
311
312 This function can be used to obtain information about a particular IDE channel.
313 The driver entity uses this information during the enumeration process.
314
315 If Enabled is set to FALSE, the driver entity will not scan the channel. Note
316 that it will not prevent an operating system driver from scanning the channel.
317
318 For most of today's controllers, MaxDevices will either be 1 or 2. For SATA
319 controllers, this value will always be 1. SATA configurations can contain SATA
320 port multipliers. SATA port multipliers behave like SATA bridges and can support
321 up to 16 devices on the other side. If a SATA port out of the IDE controller
322 is connected to a port multiplier, MaxDevices will be set to the number of SATA
323 devices that the port multiplier supports. Because today's port multipliers
324 support up to fifteen SATA devices, this number can be as large as fifteen. The IDE
325 bus driver is required to scan for the presence of port multipliers behind an SATA
326 controller and enumerate up to MaxDevices number of devices behind the port
327 multiplier.
328
329 In this context, the devices behind a port multiplier constitute a channel.
330
331 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
332 @param[in] Channel Zero-based channel number.
333 @param[out] Enabled TRUE if this channel is enabled. Disabled channels
334 are not scanned to see if any devices are present.
335 @param[out] MaxDevices The maximum number of IDE devices that the bus driver
336 can expect on this channel. For the ATA/ATAPI
337 specification, version 6, this number will either be
338 one or two. For Serial ATA (SATA) configurations with a
339 port multiplier, this number can be as large as fifteen.
340
341 @retval EFI_SUCCESS Information was returned without any errors.
342 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
343
344 **/
345 EFI_STATUS
346 EFIAPI
347 IdeInitGetChannelInfo (
348 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
349 IN UINT8 Channel,
350 OUT BOOLEAN *Enabled,
351 OUT UINT8 *MaxDevices
352 )
353 {
354 //
355 // Channel number (0 based, either 0 or 1)
356 //
357 if (Channel < ICH_IDE_MAX_CHANNEL) {
358 *Enabled = TRUE;
359 *MaxDevices = ICH_IDE_MAX_DEVICES;
360 return EFI_SUCCESS;
361 }
362
363 *Enabled = FALSE;
364 return EFI_INVALID_PARAMETER;
365 }
366
367 /**
368 The notifications from the driver entity that it is about to enter a certain
369 phase of the IDE channel enumeration process.
370
371 This function can be used to notify the IDE controller driver to perform
372 specific actions, including any chipset-specific initialization, so that the
373 chipset is ready to enter the next phase. Seven notification points are defined
374 at this time.
375
376 More synchronization points may be added as required in the future.
377
378 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
379 @param[in] Phase The phase during enumeration.
380 @param[in] Channel Zero-based channel number.
381
382 @retval EFI_SUCCESS The notification was accepted without any errors.
383 @retval EFI_UNSUPPORTED Phase is not supported.
384 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
385 @retval EFI_NOT_READY This phase cannot be entered at this time; for
386 example, an attempt was made to enter a Phase
387 without having entered one or more previous
388 Phase.
389
390 **/
391 EFI_STATUS
392 EFIAPI
393 IdeInitNotifyPhase (
394 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
395 IN EFI_IDE_CONTROLLER_ENUM_PHASE Phase,
396 IN UINT8 Channel
397 )
398 {
399 return EFI_SUCCESS;
400 }
401
402 /**
403 Submits the device information to the IDE controller driver.
404
405 This function is used by the driver entity to pass detailed information about
406 a particular device to the IDE controller driver. The driver entity obtains
407 this information by issuing an ATA or ATAPI IDENTIFY_DEVICE command. IdentifyData
408 is the pointer to the response data buffer. The IdentifyData buffer is owned
409 by the driver entity, and the IDE controller driver must make a local copy
410 of the entire buffer or parts of the buffer as needed. The original IdentifyData
411 buffer pointer may not be valid when
412
413 - EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() or
414 - EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() is called at a later point.
415
416 The IDE controller driver may consult various fields of EFI_IDENTIFY_DATA to
417 compute the optimum mode for the device. These fields are not limited to the
418 timing information. For example, an implementation of the IDE controller driver
419 may examine the vendor and type/mode field to match known bad drives.
420
421 The driver entity may submit drive information in any order, as long as it
422 submits information for all the devices belonging to the enumeration group
423 before EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() is called for any device
424 in that enumeration group. If a device is absent, EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
425 should be called with IdentifyData set to NULL. The IDE controller driver may
426 not have any other mechanism to know whether a device is present or not. Therefore,
427 setting IdentifyData to NULL does not constitute an error condition.
428 EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData() can be called only once for a
429 given (Channel, Device) pair.
430
431 @param[in] This A pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
432 @param[in] Channel Zero-based channel number.
433 @param[in] Device Zero-based device number on the Channel.
434 @param[in] IdentifyData The device's response to the ATA IDENTIFY_DEVICE command.
435
436 @retval EFI_SUCCESS The information was accepted without any errors.
437 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
438 @retval EFI_INVALID_PARAMETER Device is invalid.
439
440 **/
441 EFI_STATUS
442 EFIAPI
443 IdeInitSubmitData (
444 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
445 IN UINT8 Channel,
446 IN UINT8 Device,
447 IN EFI_IDENTIFY_DATA *IdentifyData
448 )
449 {
450 return EFI_SUCCESS;
451 }
452
453 /**
454 Disqualifies specific modes for an IDE device.
455
456 This function allows the driver entity or other drivers (such as platform
457 drivers) to reject certain timing modes and request the IDE controller driver
458 to recalculate modes. This function allows the driver entity and the IDE
459 controller driver to negotiate the timings on a per-device basis. This function
460 is useful in the case of drives that lie about their capabilities. An example
461 is when the IDE device fails to accept the timing modes that are calculated
462 by the IDE controller driver based on the response to the Identify Drive command.
463
464 If the driver entity does not want to limit the ATA timing modes and leave that
465 decision to the IDE controller driver, it can either not call this function for
466 the given device or call this function and set the Valid flag to FALSE for all
467 modes that are listed in EFI_ATA_COLLECTIVE_MODE.
468
469 The driver entity may disqualify modes for a device in any order and any number
470 of times.
471
472 This function can be called multiple times to invalidate multiple modes of the
473 same type (e.g., Programmed Input/Output [PIO] modes 3 and 4). See the ATA/ATAPI
474 specification for more information on PIO modes.
475
476 For Serial ATA (SATA) controllers, this member function can be used to disqualify
477 a higher transfer rate mode on a given channel. For example, a platform driver
478 may inform the IDE controller driver to not use second-generation (Gen2) speeds
479 for a certain SATA drive.
480
481 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
482 @param[in] Channel The zero-based channel number.
483 @param[in] Device The zero-based device number on the Channel.
484 @param[in] BadModes The modes that the device does not support and that
485 should be disqualified.
486
487 @retval EFI_SUCCESS The modes were accepted without any errors.
488 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
489 @retval EFI_INVALID_PARAMETER Device is invalid.
490 @retval EFI_INVALID_PARAMETER IdentifyData is NULL.
491
492 **/
493 EFI_STATUS
494 EFIAPI
495 IdeInitDisqualifyMode (
496 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
497 IN UINT8 Channel,
498 IN UINT8 Device,
499 IN EFI_ATA_COLLECTIVE_MODE *BadModes
500 )
501 {
502 return EFI_SUCCESS;
503 }
504
505 /**
506 Returns the information about the optimum modes for the specified IDE device.
507
508 This function is used by the driver entity to obtain the optimum ATA modes for
509 a specific device. The IDE controller driver takes into account the following
510 while calculating the mode:
511 - The IdentifyData inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
512 - The BadModes inputs to EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode()
513
514 The driver entity is required to call EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
515 for all the devices that belong to an enumeration group before calling
516 EFI_IDE_CONTROLLER_INIT_PROTOCOL.CalculateMode() for any device in the same group.
517
518 The IDE controller driver will use controller- and possibly platform-specific
519 algorithms to arrive at SupportedModes. The IDE controller may base its
520 decision on user preferences and other considerations as well. This function
521 may be called multiple times because the driver entity may renegotiate the mode
522 with the IDE controller driver using EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode().
523
524 The driver entity may collect timing information for various devices in any
525 order. The driver entity is responsible for making sure that all the dependencies
526 are satisfied. For example, the SupportedModes information for device A that
527 was previously returned may become stale after a call to
528 EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyMode() for device B.
529
530 The buffer SupportedModes is allocated by the callee because the caller does
531 not necessarily know the size of the buffer. The type EFI_ATA_COLLECTIVE_MODE
532 is defined in a way that allows for future extensibility and can be of variable
533 length. This memory pool should be deallocated by the caller when it is no
534 longer necessary.
535
536 The IDE controller driver for a Serial ATA (SATA) controller can use this
537 member function to force a lower speed (first-generation [Gen1] speeds on a
538 second-generation [Gen2]-capable hardware). The IDE controller driver can
539 also allow the driver entity to stay with the speed that has been negotiated
540 by the physical layer.
541
542 @param[in] This The pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
543 @param[in] Channel A zero-based channel number.
544 @param[in] Device A zero-based device number on the Channel.
545 @param[out] SupportedModes The optimum modes for the device.
546
547 @retval EFI_SUCCESS SupportedModes was returned.
548 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
549 @retval EFI_INVALID_PARAMETER Device is invalid.
550 @retval EFI_INVALID_PARAMETER SupportedModes is NULL.
551 @retval EFI_NOT_READY Modes cannot be calculated due to a lack of
552 data. This error may happen if
553 EFI_IDE_CONTROLLER_INIT_PROTOCOL.SubmitData()
554 and EFI_IDE_CONTROLLER_INIT_PROTOCOL.DisqualifyData()
555 were not called for at least one drive in the
556 same enumeration group.
557
558 **/
559 EFI_STATUS
560 EFIAPI
561 IdeInitCalculateMode (
562 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
563 IN UINT8 Channel,
564 IN UINT8 Device,
565 OUT EFI_ATA_COLLECTIVE_MODE **SupportedModes
566 )
567 {
568 if (Channel >= ICH_IDE_MAX_CHANNEL || Device >= ICH_IDE_MAX_DEVICES) {
569 return EFI_INVALID_PARAMETER;
570 }
571
572 *SupportedModes = AllocateCopyPool (sizeof (EFI_ATA_COLLECTIVE_MODE), &gEfiAtaCollectiveModeTemplate);
573 if (*SupportedModes == NULL) {
574 return EFI_OUT_OF_RESOURCES;
575 }
576
577 return EFI_SUCCESS;
578 }
579
580 /**
581 Commands the IDE controller driver to program the IDE controller hardware
582 so that the specified device can operate at the specified mode.
583
584 This function is used by the driver entity to instruct the IDE controller
585 driver to program the IDE controller hardware to the specified modes. This
586 function can be called only once for a particular device. For a Serial ATA
587 (SATA) Advanced Host Controller Interface (AHCI) controller, no controller-
588 specific programming may be required.
589
590 @param[in] This Pointer to the EFI_IDE_CONTROLLER_INIT_PROTOCOL instance.
591 @param[in] Channel Zero-based channel number.
592 @param[in] Device Zero-based device number on the Channel.
593 @param[in] Modes The modes to set.
594
595 @retval EFI_SUCCESS The command was accepted without any errors.
596 @retval EFI_INVALID_PARAMETER Channel is invalid (Channel >= ChannelCount).
597 @retval EFI_INVALID_PARAMETER Device is invalid.
598 @retval EFI_NOT_READY Modes cannot be set at this time due to lack of data.
599 @retval EFI_DEVICE_ERROR Modes cannot be set due to hardware failure.
600 The driver entity should not use this device.
601
602 **/
603 EFI_STATUS
604 EFIAPI
605 IdeInitSetTiming (
606 IN EFI_IDE_CONTROLLER_INIT_PROTOCOL *This,
607 IN UINT8 Channel,
608 IN UINT8 Device,
609 IN EFI_ATA_COLLECTIVE_MODE *Modes
610 )
611 {
612 return EFI_SUCCESS;
613 }