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