2 Copyright (c) 2006, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 @par Revision Reference:
12 This module is modified from DXE\IDE module for Ide Contriller Init support
18 #define PCI_CLASS_MASS_STORAGE 0x01
19 #define PCI_SUB_CLASS_IDE 0x01
23 // IDE Bus Driver Binding Protocol Instance
25 EFI_DRIVER_BINDING_PROTOCOL gIDEBusDriverBinding
= {
26 IDEBusDriverBindingSupported
,
27 IDEBusDriverBindingStart
,
28 IDEBusDriverBindingStop
,
35 // ***********************************************************************************
36 // IDEBusDriverBindingSupported
37 // ***********************************************************************************
40 Register Driver Binding protocol for this driver.
42 @param[in] This -- A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
43 @param[in] ControllerHandle -- The handle of the controller to test.
44 @param[in] RemainingDevicePath -- A pointer to the remaining portion of a device path.
46 @retval EFI_SUCCESS Driver loaded.
47 @retval other Driver not loaded.
52 IDEBusDriverBindingSupported (
53 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
54 IN EFI_HANDLE Controller
,
55 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
57 // TODO: Controller - add argument and description to function comment
58 // TODO: EFI_UNSUPPORTED - add return value to function comment
61 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
63 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeInit
;
65 if (RemainingDevicePath
!= NULL
) {
66 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
67 if (Node
->DevPath
.Type
!= MESSAGING_DEVICE_PATH
||
68 Node
->DevPath
.SubType
!= MSG_ATAPI_DP
||
69 DevicePathNodeLength(&Node
->DevPath
) != sizeof(ATAPI_DEVICE_PATH
)) {
70 return EFI_UNSUPPORTED
;
75 // Open the IO Abstraction(s) needed to perform the supported test
77 Status
= gBS
->OpenProtocol (
79 &gEfiDevicePathProtocolGuid
,
80 (VOID
**) &ParentDevicePath
,
81 This
->DriverBindingHandle
,
83 EFI_OPEN_PROTOCOL_BY_DRIVER
85 if (Status
== EFI_ALREADY_STARTED
) {
89 if (EFI_ERROR (Status
)) {
94 // Clsoe protocol, don't use device path protocol in the .Support() function
98 &gEfiDevicePathProtocolGuid
,
99 This
->DriverBindingHandle
,
104 // Verify the Ide Controller Init Protocol, which installed by the
105 // IdeController module.
106 // Note 1: PciIo protocol has been opened BY_DRIVER by ide_init, so We can't
107 // open BY_DRIVER here) That's why we don't check pciio protocol
108 // Note 2: ide_init driver check ide controller's pci config space, so we dont
109 // check here any more to save code size
111 Status
= gBS
->OpenProtocol (
113 &gEfiIdeControllerInitProtocolGuid
,
115 This
->DriverBindingHandle
,
117 EFI_OPEN_PROTOCOL_BY_DRIVER
120 if (Status
== EFI_ALREADY_STARTED
) {
125 // If protocols were opened normally, closed it
129 &gEfiIdeControllerInitProtocolGuid
,
130 This
->DriverBindingHandle
,
138 // ***********************************************************************************
139 // IDEBusDriverBindingStart
140 // ***********************************************************************************
143 Start this driver on Controller by detecting all disks and installing
144 BlockIo protocol on them.
146 @param This Protocol instance pointer.
147 @param Controller Handle of device to bind driver to.
148 @param RemainingDevicePath Not used, always produce all possible children.
150 @retval EFI_SUCCESS This driver is added to ControllerHandle.
151 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
152 @retval other This driver does not support this device.
157 IDEBusDriverBindingStart (
158 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
159 IN EFI_HANDLE Controller
,
160 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
164 EFI_STATUS SavedStatus
;
165 EFI_PCI_IO_PROTOCOL
*PciIo
;
166 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
169 UINT8 BeginningIdeChannel
;
172 UINT8 BeginningIdeDevice
;
174 IDE_BLK_IO_DEV
*IdeBlkIoDevice
[IdeMaxChannel
][IdeMaxDevice
];
175 IDE_BLK_IO_DEV
*IdeBlkIoDevicePtr
;
176 IDE_REGISTERS_BASE_ADDR IdeRegsBaseAddr
[IdeMaxChannel
];
177 ATA_TRANSFER_MODE TransferMode
;
178 ATA_DRIVE_PARMS DriveParameters
;
179 EFI_DEV_PATH NewNode
;
180 UINT8 ConfigurationOptions
;
181 UINT16 CommandBlockBaseAddr
;
182 UINT16 ControlBlockBaseAddr
;
184 IDE_BUS_DRIVER_PRIVATE_DATA
*IdeBusDriverPrivateData
;
187 // Local variables declaration for IdeControllerInit support
189 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeInit
;
191 BOOLEAN ChannelEnabled
;
194 EFI_IDENTIFY_DATA IdentifyData
;
195 EFI_ATA_COLLECTIVE_MODE
*SupportedModes
;
197 IdeBusDriverPrivateData
= NULL
;
198 SupportedModes
= NULL
;
201 // Perform IdeBus initialization
203 Status
= gBS
->OpenProtocol (
205 &gEfiDevicePathProtocolGuid
,
206 (VOID
**) &ParentDevicePath
,
207 This
->DriverBindingHandle
,
209 EFI_OPEN_PROTOCOL_BY_DRIVER
211 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
216 // Now open the IDE_CONTROLLER_INIT protocol. Step7.1
218 Status
= gBS
->OpenProtocol (
220 &gEfiIdeControllerInitProtocolGuid
,
222 This
->DriverBindingHandle
,
224 EFI_OPEN_PROTOCOL_BY_DRIVER
228 // The following OpenProtocol function with _GET_PROTOCOL attribute and
229 // will not return EFI_ALREADY_STARTED, so save it for now
231 SavedStatus
= Status
;
233 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
234 DEBUG ((EFI_D_ERROR
, "Open Init, Status=%x", Status
));
236 // open protocol is not SUCCESS or not ALREADY_STARTED, error exit
242 // Save Enumall and ChannelCount. Step7.2
244 EnumAll
= IdeInit
->EnumAll
;
245 ChannelCount
= IdeInit
->ChannelCount
;
248 // Consume PCI I/O protocol. Note that the OpenProtocol with _GET_PROTOCOL
249 // attribute will not return EFI_ALREADY_STARTED
251 Status
= gBS
->OpenProtocol (
253 &gEfiPciIoProtocolGuid
,
255 This
->DriverBindingHandle
,
257 EFI_OPEN_PROTOCOL_GET_PROTOCOL
259 if (EFI_ERROR (Status
)) {
260 DEBUG ((EFI_D_ERROR
, "Open PciIo, Status=%x", Status
));
265 // We must check EFI_ALREADY_STARTED because many ATAPI devices are removable
267 if (SavedStatus
!= EFI_ALREADY_STARTED
) {
268 IdeBusDriverPrivateData
= AllocatePool (sizeof (IDE_BUS_DRIVER_PRIVATE_DATA
));
269 if (IdeBusDriverPrivateData
== NULL
) {
270 Status
= EFI_OUT_OF_RESOURCES
;
274 ZeroMem (IdeBusDriverPrivateData
, sizeof (IDE_BUS_DRIVER_PRIVATE_DATA
));
275 Status
= gBS
->InstallMultipleProtocolInterfaces (
278 IdeBusDriverPrivateData
,
281 if (EFI_ERROR (Status
)) {
286 Status
= gBS
->OpenProtocol (
289 (VOID
**) &IdeBusDriverPrivateData
,
290 This
->DriverBindingHandle
,
292 EFI_OPEN_PROTOCOL_GET_PROTOCOL
294 if (EFI_ERROR (Status
)) {
295 IdeBusDriverPrivateData
= NULL
;
300 Status
= PciIo
->Attributes (
302 EfiPciIoAttributeOperationEnable
,
303 EFI_PCI_DEVICE_ENABLE
,
306 if (EFI_ERROR (Status
)) {
311 // Read the environment variable that contains the IDEBus Driver's
312 // Config options that were set by the Driver Configuration Protocol
314 DataSize
= sizeof (ConfigurationOptions
);
315 Status
= gRT
->GetVariable (
316 (CHAR16
*) L
"Configuration",
320 &ConfigurationOptions
322 if (EFI_ERROR (Status
)) {
323 ConfigurationOptions
= 0x0f;
328 // If IdeInit->EnumAll is TRUE, must enumerate all IDE device anyway
330 BeginningIdeChannel
= IdePrimary
;
331 EndIdeChannel
= IdeSecondary
;
332 BeginningIdeDevice
= IdeMaster
;
333 EndIdeDevice
= IdeSlave
;
334 } else if (RemainingDevicePath
== NULL
) {
336 // RemainingDevicePath is NULL, scan IDE bus for each device;
338 BeginningIdeChannel
= IdePrimary
;
339 EndIdeChannel
= IdeSecondary
;
340 BeginningIdeDevice
= IdeMaster
;
342 // default, may be redefined by IdeInit
344 EndIdeDevice
= IdeSlave
;
347 // RemainingDevicePath is not NULL, only scan the specified device.
349 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
350 BeginningIdeChannel
= Node
->Atapi
.PrimarySecondary
;
351 EndIdeChannel
= BeginningIdeChannel
;
352 BeginningIdeDevice
= Node
->Atapi
.SlaveMaster
;
353 EndIdeDevice
= BeginningIdeDevice
;
357 // Obtain IDE IO port registers' base addresses
359 Status
= GetIdeRegistersBaseAddr (PciIo
, IdeRegsBaseAddr
);
360 if (EFI_ERROR (Status
)) {
365 // Report status code: begin IdeBus initialization
367 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
369 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_PC_RESET
),
374 // Strictly follow the enumeration based on IDE_CONTROLLER_INIT protocol
376 for (IdeChannel
= BeginningIdeChannel
; IdeChannel
<= EndIdeChannel
; IdeChannel
++) {
378 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBeforeChannelEnumeration
, IdeChannel
);
381 // now obtain channel information fron IdeControllerInit protocol. Step9
383 Status
= IdeInit
->GetChannelInfo (
389 if (EFI_ERROR (Status
)) {
390 DEBUG ((EFI_D_ERROR
, "[GetChannel, Status=%x]", Status
));
394 if (!ChannelEnabled
) {
398 EndIdeDevice
= (UINT8
) EFI_MIN ((MaxDevices
- 1), EndIdeDevice
);
401 // Now inform the IDE Controller Init Module. Sept10
403 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBeforeChannelReset
, IdeChannel
);
406 // No reset channel function implemented. Sept11
408 IdeInit
->NotifyPhase (IdeInit
, EfiIdeAfterChannelReset
, IdeChannel
);
413 IdeInit
->NotifyPhase (
415 EfiIdeBusBeforeDevicePresenceDetection
,
420 // Prepare to detect IDE device of this channel
422 InitializeIDEChannelData ();
425 // -- 1st inner loop --- Master/Slave ------------ Step14
427 for (IdeDevice
= BeginningIdeDevice
; IdeDevice
<= EndIdeDevice
; IdeDevice
++) {
429 // Check whether the configuration options allow this device
431 if (!(ConfigurationOptions
& (1 << (IdeChannel
* 2 + IdeDevice
)))) {
436 // The device has been scanned in another Start(), No need to scan it again
437 // for perf optimization.
439 if (IdeBusDriverPrivateData
->HaveScannedDevice
[IdeChannel
* 2 + IdeDevice
]) {
444 // create child handle for the detected device.
446 IdeBlkIoDevice
[IdeChannel
][IdeDevice
] = AllocatePool (sizeof (IDE_BLK_IO_DEV
));
447 if (IdeBlkIoDevice
[IdeChannel
][IdeDevice
] == NULL
) {
451 IdeBlkIoDevicePtr
= IdeBlkIoDevice
[IdeChannel
][IdeDevice
];
453 ZeroMem (IdeBlkIoDevicePtr
, sizeof (IDE_BLK_IO_DEV
));
455 IdeBlkIoDevicePtr
->Signature
= IDE_BLK_IO_DEV_SIGNATURE
;
456 IdeBlkIoDevicePtr
->Channel
= IdeChannel
;
457 IdeBlkIoDevicePtr
->Device
= IdeDevice
;
460 // initialize Block IO interface's Media pointer
462 IdeBlkIoDevicePtr
->BlkIo
.Media
= &IdeBlkIoDevicePtr
->BlkMedia
;
465 // Initialize IDE IO port addresses, including Command Block registers
466 // and Control Block registers
468 IdeBlkIoDevicePtr
->IoPort
= AllocatePool (sizeof (IDE_BASE_REGISTERS
));
469 if (IdeBlkIoDevicePtr
->IoPort
== NULL
) {
473 ZeroMem (IdeBlkIoDevicePtr
->IoPort
, sizeof (IDE_BASE_REGISTERS
));
474 CommandBlockBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].CommandBlockBaseAddr
;
475 ControlBlockBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].ControlBlockBaseAddr
;
477 IdeBlkIoDevicePtr
->IoPort
->Data
= CommandBlockBaseAddr
;
478 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Reg1
) = (UINT16
) (CommandBlockBaseAddr
+ 0x01);
479 IdeBlkIoDevicePtr
->IoPort
->SectorCount
= (UINT16
) (CommandBlockBaseAddr
+ 0x02);
480 IdeBlkIoDevicePtr
->IoPort
->SectorNumber
= (UINT16
) (CommandBlockBaseAddr
+ 0x03);
481 IdeBlkIoDevicePtr
->IoPort
->CylinderLsb
= (UINT16
) (CommandBlockBaseAddr
+ 0x04);
482 IdeBlkIoDevicePtr
->IoPort
->CylinderMsb
= (UINT16
) (CommandBlockBaseAddr
+ 0x05);
483 IdeBlkIoDevicePtr
->IoPort
->Head
= (UINT16
) (CommandBlockBaseAddr
+ 0x06);
484 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Reg
) = (UINT16
) (CommandBlockBaseAddr
+ 0x07);
486 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Alt
) = ControlBlockBaseAddr
;
487 IdeBlkIoDevicePtr
->IoPort
->DriveAddress
= (UINT16
) (ControlBlockBaseAddr
+ 0x01);
489 IdeBlkIoDevicePtr
->IoPort
->MasterSlave
= (UINT16
) ((IdeDevice
== IdeMaster
) ? 1 : 0);
491 IdeBlkIoDevicePtr
->PciIo
= PciIo
;
492 IdeBlkIoDevicePtr
->IdeBusDriverPrivateData
= IdeBusDriverPrivateData
;
493 IdeBlkIoDevicePtr
->IoPort
->BusMasterBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].BusMasterBaseAddr
;
496 // Report Status code: is about to detect IDE drive
498 REPORT_STATUS_CODE_EX (
500 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_PRESENCE_DETECT
),
509 // Discover device, now!
511 PERF_START (0, "DiscoverIdeDevice", "IDE", 0);
512 Status
= DiscoverIdeDevice (IdeBlkIoDevicePtr
);
513 PERF_END (0, "DiscoverIdeDevice", "IDE", 0);
515 IdeBusDriverPrivateData
->HaveScannedDevice
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
516 IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
518 if (!EFI_ERROR (Status
)) {
522 ZeroMem (&NewNode
, sizeof (NewNode
));
523 NewNode
.DevPath
.Type
= MESSAGING_DEVICE_PATH
;
524 NewNode
.DevPath
.SubType
= MSG_ATAPI_DP
;
525 SetDevicePathNodeLength (&NewNode
.DevPath
, sizeof (ATAPI_DEVICE_PATH
));
527 NewNode
.Atapi
.PrimarySecondary
= (UINT8
) IdeBlkIoDevicePtr
->Channel
;
528 NewNode
.Atapi
.SlaveMaster
= (UINT8
) IdeBlkIoDevicePtr
->Device
;
529 NewNode
.Atapi
.Lun
= IdeBlkIoDevicePtr
->Lun
;
530 IdeBlkIoDevicePtr
->DevicePath
= AppendDevicePathNode (
534 if (IdeBlkIoDevicePtr
->DevicePath
== NULL
) {
535 ReleaseIdeResources (IdeBlkIoDevicePtr
);
540 // Submit identify data to IDE controller init driver
542 CopyMem (&IdentifyData
, IdeBlkIoDevicePtr
->pIdData
, sizeof (IdentifyData
));
543 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
544 IdeInit
->SubmitData (IdeInit
, IdeChannel
, IdeDevice
, &IdentifyData
);
547 // Device detection failed
549 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
550 IdeInit
->SubmitData (IdeInit
, IdeChannel
, IdeDevice
, NULL
);
551 ReleaseIdeResources (IdeBlkIoDevicePtr
);
552 IdeBlkIoDevicePtr
= NULL
;
555 // end of 1st inner loop ---
559 // end of 1st outer loop =========
564 // = 2nd outer loop == Primary/Secondary =================
566 for (IdeChannel
= BeginningIdeChannel
; IdeChannel
<= EndIdeChannel
; IdeChannel
++) {
569 // -- 2nd inner loop --- Master/Slave --------
571 for (IdeDevice
= BeginningIdeDevice
; IdeDevice
<= EndIdeDevice
; IdeDevice
++) {
573 if (IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
]) {
577 if (!IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
]) {
581 Status
= IdeInit
->CalculateMode (
587 if (EFI_ERROR (Status
)) {
588 DEBUG ((EFI_D_ERROR
, "[bStStp20S=%x]", Status
));
592 IdeBlkIoDevicePtr
= IdeBlkIoDevice
[IdeChannel
][IdeDevice
];
595 // Set best supported PIO mode on this IDE device
597 if (SupportedModes
->PioMode
.Mode
<= ATA_PIO_MODE_2
) {
598 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_DEFAULT_PIO
;
600 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_FLOW_PIO
;
603 TransferMode
.ModeNumber
= (UINT8
) (SupportedModes
->PioMode
.Mode
);
605 if (SupportedModes
->ExtModeCount
== 0){
606 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
608 if (EFI_ERROR (Status
)) {
609 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
610 ReleaseIdeResources (IdeBlkIoDevicePtr
);
611 IdeBlkIoDevicePtr
= NULL
;
617 // Set supported DMA mode on this IDE device. Note that UDMA & MDMA cann't
618 // be set together. Only one DMA mode can be set to a device. If setting
619 // DMA mode operation fails, we can continue moving on because we only use
620 // PIO mode at boot time. DMA modes are used by certain kind of OS booting
622 if (SupportedModes
->UdmaMode
.Valid
) {
624 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_UDMA
;
625 TransferMode
.ModeNumber
= (UINT8
) (SupportedModes
->UdmaMode
.Mode
);
626 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
628 if (EFI_ERROR (Status
)) {
629 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
630 ReleaseIdeResources (IdeBlkIoDevicePtr
);
631 IdeBlkIoDevicePtr
= NULL
;
635 EnableInterrupt (IdeBlkIoDevicePtr
);
636 } else if (SupportedModes
->MultiWordDmaMode
.Valid
) {
638 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_MDMA
;
639 TransferMode
.ModeNumber
= (UINT8
) SupportedModes
->MultiWordDmaMode
.Mode
;
640 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
642 if (EFI_ERROR (Status
)) {
643 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
644 ReleaseIdeResources (IdeBlkIoDevicePtr
);
645 IdeBlkIoDevicePtr
= NULL
;
649 EnableInterrupt (IdeBlkIoDevicePtr
);
652 // Init driver parameters
654 DriveParameters
.Sector
= (UINT8
) IdeBlkIoDevicePtr
->pIdData
->AtaData
.sectors_per_track
;
655 DriveParameters
.Heads
= (UINT8
) (IdeBlkIoDevicePtr
->pIdData
->AtaData
.heads
- 1);
656 DriveParameters
.MultipleSector
= (UINT8
) IdeBlkIoDevicePtr
->pIdData
->AtaData
.multi_sector_cmd_max_sct_cnt
;
658 // Set Parameters for the device:
660 // 2) Establish the block count for READ/WRITE MULTIPLE (EXT) command
662 if ((IdeBlkIoDevicePtr
->Type
== IdeHardDisk
) || (IdeBlkIoDevicePtr
->Type
== Ide48bitAddressingHardDisk
)) {
663 Status
= SetDriveParameters (IdeBlkIoDevicePtr
, &DriveParameters
);
667 // Record PIO mode used in private data
669 IdeBlkIoDevicePtr
->PioMode
= SupportedModes
->PioMode
.Mode
;
672 // Set IDE controller Timing Blocks in the PCI Configuration Space
674 IdeInit
->SetTiming (IdeInit
, IdeChannel
, IdeDevice
, SupportedModes
);
677 // Add Component Name for the IDE/ATAPI device that was discovered.
679 IdeBlkIoDevicePtr
->ControllerNameTable
= NULL
;
680 ADD_NAME (IdeBlkIoDevicePtr
);
682 Status
= gBS
->InstallMultipleProtocolInterfaces (
683 &IdeBlkIoDevicePtr
->Handle
,
684 &gEfiDevicePathProtocolGuid
,
685 IdeBlkIoDevicePtr
->DevicePath
,
686 &gEfiBlockIoProtocolGuid
,
687 &IdeBlkIoDevicePtr
->BlkIo
,
688 &gEfiDiskInfoProtocolGuid
,
689 &IdeBlkIoDevicePtr
->DiskInfo
,
693 if (EFI_ERROR (Status
)) {
694 ReleaseIdeResources (IdeBlkIoDevicePtr
);
699 &gEfiPciIoProtocolGuid
,
701 This
->DriverBindingHandle
,
702 IdeBlkIoDevicePtr
->Handle
,
703 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
706 IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
709 // Report status code: device eanbled!
711 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
713 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_ENABLE
),
714 IdeBlkIoDevicePtr
->DevicePath
718 // Create event to clear pending IDE interrupt
720 Status
= gBS
->CreateEvent (
721 EFI_EVENT_SIGNAL_EXIT_BOOT_SERVICES
,
725 &IdeBlkIoDevicePtr
->ExitBootServiceEvent
729 // end of 2nd inner loop ----
733 // end of 2nd outer loop ==========
738 // All configurations done! Notify IdeController to do post initialization
739 // work such as saving IDE controller PCI settings for S3 resume
741 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBusPhaseMaximum
, 0);
743 if (SupportedModes
!= NULL
) {
744 gBS
->FreePool (SupportedModes
);
747 PERF_START (0, "Finish IDE detection", "IDE", 1);
748 PERF_END (0, "Finish IDE detection", "IDE", 0);
755 // Report error code: controller error
757 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
758 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
759 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_EC_CONTROLLER_ERROR
),
765 &gEfiIdeControllerInitProtocolGuid
,
766 This
->DriverBindingHandle
,
770 gBS
->UninstallMultipleProtocolInterfaces (
773 IdeBusDriverPrivateData
,
777 if (IdeBusDriverPrivateData
!= NULL
) {
778 gBS
->FreePool (IdeBusDriverPrivateData
);
781 if (SupportedModes
!= NULL
) {
782 gBS
->FreePool (SupportedModes
);
787 &gEfiPciIoProtocolGuid
,
788 This
->DriverBindingHandle
,
794 &gEfiDevicePathProtocolGuid
,
795 This
->DriverBindingHandle
,
804 // ***********************************************************************************
805 // IDEBusDriverBindingStop
806 // ***********************************************************************************
809 Stop this driver on Controller Handle.
811 @param This Protocol instance pointer.
812 @param DeviceHandle Handle of device to stop driver on
813 @param NumberOfChildren Not used
814 @param ChildHandleBuffer Not used
816 @retval EFI_SUCCESS This driver is removed DeviceHandle
817 @retval other This driver was not removed from this device
822 IDEBusDriverBindingStop (
823 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
824 IN EFI_HANDLE Controller
,
825 IN UINTN NumberOfChildren
,
826 IN EFI_HANDLE
*ChildHandleBuffer
828 // TODO: Controller - add argument and description to function comment
829 // TODO: EFI_DEVICE_ERROR - add return value to function comment
832 EFI_PCI_IO_PROTOCOL
*PciIo
;
833 BOOLEAN AllChildrenStopped
;
835 IDE_BUS_DRIVER_PRIVATE_DATA
*IdeBusDriverPrivateData
;
837 IdeBusDriverPrivateData
= NULL
;
839 if (NumberOfChildren
== 0) {
841 Status
= gBS
->OpenProtocol (
843 &gEfiPciIoProtocolGuid
,
845 This
->DriverBindingHandle
,
847 EFI_OPEN_PROTOCOL_GET_PROTOCOL
849 if (!EFI_ERROR (Status
)) {
852 EfiPciIoAttributeOperationDisable
,
853 EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO
| EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO
| EFI_PCI_DEVICE_ENABLE
,
861 (VOID
**) &IdeBusDriverPrivateData
,
862 This
->DriverBindingHandle
,
864 EFI_OPEN_PROTOCOL_GET_PROTOCOL
867 gBS
->UninstallMultipleProtocolInterfaces (
870 IdeBusDriverPrivateData
,
874 if (IdeBusDriverPrivateData
!= NULL
) {
875 gBS
->FreePool (IdeBusDriverPrivateData
);
878 // Close the bus driver
882 &gEfiIdeControllerInitProtocolGuid
,
883 This
->DriverBindingHandle
,
888 &gEfiPciIoProtocolGuid
,
889 This
->DriverBindingHandle
,
894 &gEfiDevicePathProtocolGuid
,
895 This
->DriverBindingHandle
,
902 AllChildrenStopped
= TRUE
;
904 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
906 Status
= DeRegisterIdeDevice (This
, Controller
, ChildHandleBuffer
[Index
]);
908 if (EFI_ERROR (Status
)) {
909 AllChildrenStopped
= FALSE
;
913 if (!AllChildrenStopped
) {
914 return EFI_DEVICE_ERROR
;
921 // ***********************************************************************************
922 // DeRegisterIdeDevice
923 // ***********************************************************************************
926 Deregister an IDE device and free resources
928 @param This Protocol instance pointer.
929 @param Controller Ide device handle
930 @param Handle Handle of device to deregister driver on
936 DeRegisterIdeDevice (
937 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
938 IN EFI_HANDLE Controller
,
941 // TODO: EFI_SUCCESS - add return value to function comment
944 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
945 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
946 EFI_PCI_IO_PROTOCOL
*PciIo
;
949 Status
= gBS
->OpenProtocol (
951 &gEfiBlockIoProtocolGuid
,
953 This
->DriverBindingHandle
,
955 EFI_OPEN_PROTOCOL_GET_PROTOCOL
957 if (EFI_ERROR (Status
)) {
961 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (BlkIo
);
964 // Report Status code: Device disabled
966 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
968 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_DISABLE
),
969 IdeBlkIoDevice
->DevicePath
973 // Close the child handle
975 Status
= gBS
->CloseProtocol (
977 &gEfiPciIoProtocolGuid
,
978 This
->DriverBindingHandle
,
982 Status
= gBS
->UninstallMultipleProtocolInterfaces (
984 &gEfiDevicePathProtocolGuid
,
985 IdeBlkIoDevice
->DevicePath
,
986 &gEfiBlockIoProtocolGuid
,
987 &IdeBlkIoDevice
->BlkIo
,
988 &gEfiDiskInfoProtocolGuid
,
989 &IdeBlkIoDevice
->DiskInfo
,
993 if (EFI_ERROR (Status
)) {
996 &gEfiPciIoProtocolGuid
,
998 This
->DriverBindingHandle
,
1000 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1006 // Release allocated resources
1008 Index
= IdeBlkIoDevice
->Channel
* 2 + IdeBlkIoDevice
->Device
;
1009 IdeBlkIoDevice
->IdeBusDriverPrivateData
->HaveScannedDevice
[Index
] = FALSE
;
1011 ReleaseIdeResources (IdeBlkIoDevice
);
1017 // ***********************************************************************************
1019 // ***********************************************************************************
1022 TODO: This - add argument and description to function comment
1023 TODO: ExtendedVerification - add argument and description to function comment
1024 TODO: EFI_DEVICE_ERROR - add return value to function comment
1030 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1031 IN BOOLEAN ExtendedVerification
1034 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1037 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1039 // Requery IDE IO resources in case of the switch of native and legacy modes
1041 ReassignIdeResources (IdeBlkIoDevice
);
1044 // for ATA device, using ATA reset method
1046 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1047 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1048 return AtaSoftReset (IdeBlkIoDevice
);
1051 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1052 return EFI_DEVICE_ERROR
;
1056 // for ATAPI device, using ATAPI reset method
1058 Status
= AtapiSoftReset (IdeBlkIoDevice
);
1059 if (ExtendedVerification
) {
1060 Status
= AtaSoftReset (IdeBlkIoDevice
);
1067 Read data from block io device
1069 @param This Protocol instance pointer.
1070 @param MediaId The media ID of the device
1071 @param LBA Starting LBA address to read data
1072 @param BufferSize The size of data to be read
1073 @param Buffer Caller supplied buffer to save data
1075 @return read data status
1080 IDEBlkIoReadBlocks (
1081 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1084 IN UINTN BufferSize
,
1087 // TODO: EFI_DEVICE_ERROR - add return value to function comment
1089 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1091 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1094 // Requery IDE IO resources in case of the switch of native and legacy modes
1096 ReassignIdeResources (IdeBlkIoDevice
);
1099 // For ATA compatible device, use ATA read block's mechanism
1101 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1102 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1103 return AtaBlkIoReadBlocks (
1112 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1113 return EFI_DEVICE_ERROR
;
1117 // for ATAPI device, using ATAPI read block's mechanism
1119 return AtapiBlkIoReadBlocks (
1130 Write data to block io device
1132 @param This Protocol instance pointer.
1133 @param MediaId The media ID of the device
1134 @param LBA Starting LBA address to write data
1135 @param BufferSize The size of data to be written
1136 @param Buffer Caller supplied buffer to save data
1138 @return write data status
1143 IDEBlkIoWriteBlocks (
1144 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1147 IN UINTN BufferSize
,
1150 // TODO: EFI_DEVICE_ERROR - add return value to function comment
1152 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1154 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1156 // Requery IDE IO resources in case of the switch of native and legacy modes
1158 ReassignIdeResources (IdeBlkIoDevice
);
1161 // for ATA device, using ATA write block's mechanism
1163 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1164 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1166 return AtaBlkIoWriteBlocks (
1175 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1176 return EFI_DEVICE_ERROR
;
1180 // for ATAPI device, using ATAPI write block's mechanism
1182 return AtapiBlkIoWriteBlocks (
1192 // ***********************************************************************************
1193 // IDEBlkIoFlushBlocks
1194 // ***********************************************************************************
1197 TODO: This - add argument and description to function comment
1198 TODO: EFI_SUCCESS - add return value to function comment
1202 IDEBlkIoFlushBlocks (
1203 IN EFI_BLOCK_IO_PROTOCOL
*This
1213 Return the results of the Inquiry command to a drive in InquiryData.
1214 Data format of Inquiry data is defined by the Interface GUID.
1216 @param This Protocol instance pointer.
1217 @param InquiryData Results of Inquiry command to device
1218 @param InquiryDataSize Size of InquiryData in bytes.
1220 @retval EFI_SUCCESS InquiryData valid
1221 @retval EFI_NOT_FOUND Device does not support this data class
1222 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
1223 @retval EFI_BUFFER_TOO_SMALL IntquiryDataSize not big enough
1228 IDEDiskInfoInquiry (
1229 IN EFI_DISK_INFO_PROTOCOL
*This
,
1230 IN OUT VOID
*InquiryData
,
1231 IN OUT UINT32
*InquiryDataSize
1234 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1236 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1238 if (*InquiryDataSize
< sizeof (INQUIRY_DATA
)) {
1239 *InquiryDataSize
= sizeof (INQUIRY_DATA
);
1240 return EFI_BUFFER_TOO_SMALL
;
1243 if (IdeBlkIoDevice
->pInquiryData
== NULL
) {
1244 return EFI_NOT_FOUND
;
1247 gBS
->CopyMem (InquiryData
, IdeBlkIoDevice
->pInquiryData
, sizeof (INQUIRY_DATA
));
1248 *InquiryDataSize
= sizeof (INQUIRY_DATA
);
1254 Return the results of the Identify command to a drive in IdentifyData.
1255 Data format of Identify data is defined by the Interface GUID.
1257 @param This Protocol instance pointer.
1258 @param IdentifyData Results of Identify command to device
1259 @param IdentifyDataSize Size of IdentifyData in bytes.
1261 @retval EFI_SUCCESS IdentifyData valid
1262 @retval EFI_NOT_FOUND Device does not support this data class
1263 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
1264 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
1269 IDEDiskInfoIdentify (
1270 IN EFI_DISK_INFO_PROTOCOL
*This
,
1271 IN OUT VOID
*IdentifyData
,
1272 IN OUT UINT32
*IdentifyDataSize
1275 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1277 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1279 if (*IdentifyDataSize
< sizeof (EFI_IDENTIFY_DATA
)) {
1280 *IdentifyDataSize
= sizeof (EFI_IDENTIFY_DATA
);
1281 return EFI_BUFFER_TOO_SMALL
;
1284 if (IdeBlkIoDevice
->pIdData
== NULL
) {
1285 return EFI_NOT_FOUND
;
1288 gBS
->CopyMem (IdentifyData
, IdeBlkIoDevice
->pIdData
, sizeof (EFI_IDENTIFY_DATA
));
1289 *IdentifyDataSize
= sizeof (EFI_IDENTIFY_DATA
);
1295 Return the results of the Request Sense command to a drive in SenseData.
1296 Data format of Sense data is defined by the Interface GUID.
1298 @param This Protocol instance pointer.
1299 @param SenseData Results of Request Sense command to device
1300 @param SenseDataSize Size of SenseData in bytes.
1301 @param SenseDataNumber Type of SenseData
1303 @retval EFI_SUCCESS InquiryData valid
1304 @retval EFI_NOT_FOUND Device does not support this data class
1305 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
1306 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough
1311 IDEDiskInfoSenseData (
1312 IN EFI_DISK_INFO_PROTOCOL
*This
,
1313 IN OUT VOID
*SenseData
,
1314 IN OUT UINT32
*SenseDataSize
,
1315 OUT UINT8
*SenseDataNumber
1318 return EFI_NOT_FOUND
;
1322 Return the results of the Request Sense command to a drive in SenseData.
1323 Data format of Sense data is defined by the Interface GUID.
1325 @param This Protocol instance pointer.
1326 @param IdeChannel Primary or Secondary
1327 @param IdeDevice Master or Slave
1329 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid
1330 @retval EFI_UNSUPPORTED This is not an IDE device
1335 IDEDiskInfoWhichIde (
1336 IN EFI_DISK_INFO_PROTOCOL
*This
,
1337 OUT UINT32
*IdeChannel
,
1338 OUT UINT32
*IdeDevice
1341 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1343 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1344 *IdeChannel
= IdeBlkIoDevice
->Channel
;
1345 *IdeDevice
= IdeBlkIoDevice
->Device
;