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
;
185 IDE_BUS_DRIVER_PRIVATE_DATA
*IdeBusDriverPrivateData
;
189 // Local variables declaration for IdeControllerInit support
191 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeInit
;
193 BOOLEAN ChannelEnabled
;
196 EFI_IDENTIFY_DATA IdentifyData
;
197 EFI_ATA_COLLECTIVE_MODE
*SupportedModes
;
199 IdeBusDriverPrivateData
= NULL
;
200 SupportedModes
= NULL
;
203 // Perform IdeBus initialization
205 Status
= gBS
->OpenProtocol (
207 &gEfiDevicePathProtocolGuid
,
208 (VOID
**) &ParentDevicePath
,
209 This
->DriverBindingHandle
,
211 EFI_OPEN_PROTOCOL_BY_DRIVER
213 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
218 // Now open the IDE_CONTROLLER_INIT protocol. Step7.1
220 Status
= gBS
->OpenProtocol (
222 &gEfiIdeControllerInitProtocolGuid
,
224 This
->DriverBindingHandle
,
226 EFI_OPEN_PROTOCOL_BY_DRIVER
230 // The following OpenProtocol function with _GET_PROTOCOL attribute and
231 // will not return EFI_ALREADY_STARTED, so save it for now
233 SavedStatus
= Status
;
235 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
236 DEBUG ((EFI_D_ERROR
, "Open Init, Status=%x", Status
));
238 // open protocol is not SUCCESS or not ALREADY_STARTED, error exit
244 // Save Enumall and ChannelCount. Step7.2
246 EnumAll
= IdeInit
->EnumAll
;
247 ChannelCount
= IdeInit
->ChannelCount
;
250 // Consume PCI I/O protocol. Note that the OpenProtocol with _GET_PROTOCOL
251 // attribute will not return EFI_ALREADY_STARTED
253 Status
= gBS
->OpenProtocol (
255 &gEfiPciIoProtocolGuid
,
257 This
->DriverBindingHandle
,
259 EFI_OPEN_PROTOCOL_GET_PROTOCOL
261 if (EFI_ERROR (Status
)) {
262 DEBUG ((EFI_D_ERROR
, "Open PciIo, Status=%x", Status
));
267 // We must check EFI_ALREADY_STARTED because many ATAPI devices are removable
269 if (SavedStatus
!= EFI_ALREADY_STARTED
) {
270 IdeBusDriverPrivateData
= AllocatePool (sizeof (IDE_BUS_DRIVER_PRIVATE_DATA
));
271 if (IdeBusDriverPrivateData
== NULL
) {
272 Status
= EFI_OUT_OF_RESOURCES
;
276 ZeroMem (IdeBusDriverPrivateData
, sizeof (IDE_BUS_DRIVER_PRIVATE_DATA
));
277 Status
= gBS
->InstallMultipleProtocolInterfaces (
280 IdeBusDriverPrivateData
,
283 if (EFI_ERROR (Status
)) {
288 Status
= gBS
->OpenProtocol (
291 (VOID
**) &IdeBusDriverPrivateData
,
292 This
->DriverBindingHandle
,
294 EFI_OPEN_PROTOCOL_GET_PROTOCOL
296 if (EFI_ERROR (Status
)) {
297 IdeBusDriverPrivateData
= NULL
;
302 Status
= PciIo
->Attributes (
304 EfiPciIoAttributeOperationEnable
,
305 EFI_PCI_DEVICE_ENABLE
,
308 if (EFI_ERROR (Status
)) {
313 // Read the environment variable that contains the IDEBus Driver's
314 // Config options that were set by the Driver Configuration Protocol
316 DataSize
= sizeof (ConfigurationOptions
);
317 Status
= gRT
->GetVariable (
318 (CHAR16
*) L
"Configuration",
322 &ConfigurationOptions
324 if (EFI_ERROR (Status
)) {
325 ConfigurationOptions
= 0x0f;
330 // If IdeInit->EnumAll is TRUE, must enumerate all IDE device anyway
332 BeginningIdeChannel
= IdePrimary
;
333 EndIdeChannel
= IdeSecondary
;
334 BeginningIdeDevice
= IdeMaster
;
335 EndIdeDevice
= IdeSlave
;
336 } else if (RemainingDevicePath
== NULL
) {
338 // RemainingDevicePath is NULL, scan IDE bus for each device;
340 BeginningIdeChannel
= IdePrimary
;
341 EndIdeChannel
= IdeSecondary
;
342 BeginningIdeDevice
= IdeMaster
;
344 // default, may be redefined by IdeInit
346 EndIdeDevice
= IdeSlave
;
349 // RemainingDevicePath is not NULL, only scan the specified device.
351 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
352 BeginningIdeChannel
= Node
->Atapi
.PrimarySecondary
;
353 EndIdeChannel
= BeginningIdeChannel
;
354 BeginningIdeDevice
= Node
->Atapi
.SlaveMaster
;
355 EndIdeDevice
= BeginningIdeDevice
;
359 // Obtain IDE IO port registers' base addresses
361 Status
= GetIdeRegistersBaseAddr (PciIo
, IdeRegsBaseAddr
);
362 if (EFI_ERROR (Status
)) {
367 // Report status code: begin IdeBus initialization
369 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
371 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_PC_RESET
),
376 // Strictly follow the enumeration based on IDE_CONTROLLER_INIT protocol
378 for (IdeChannel
= BeginningIdeChannel
; IdeChannel
<= EndIdeChannel
; IdeChannel
++) {
380 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBeforeChannelEnumeration
, IdeChannel
);
383 // now obtain channel information fron IdeControllerInit protocol. Step9
385 Status
= IdeInit
->GetChannelInfo (
391 if (EFI_ERROR (Status
)) {
392 DEBUG ((EFI_D_ERROR
, "[GetChannel, Status=%x]", Status
));
396 if (!ChannelEnabled
) {
400 EndIdeDevice
= (UINT8
) EFI_MIN ((MaxDevices
- 1), EndIdeDevice
);
403 // Now inform the IDE Controller Init Module. Sept10
405 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBeforeChannelReset
, IdeChannel
);
408 // No reset channel function implemented. Sept11
410 IdeInit
->NotifyPhase (IdeInit
, EfiIdeAfterChannelReset
, IdeChannel
);
415 IdeInit
->NotifyPhase (
417 EfiIdeBusBeforeDevicePresenceDetection
,
421 // -- 1st inner loop --- Master/Slave ------------ Step14
423 for (IdeDevice
= BeginningIdeDevice
; IdeDevice
<= EndIdeDevice
; IdeDevice
++) {
425 // Check whether the configuration options allow this device
427 if (!(ConfigurationOptions
& (1 << (IdeChannel
* 2 + IdeDevice
)))) {
432 // The device has been scanned in another Start(), No need to scan it again
433 // for perf optimization.
435 if (IdeBusDriverPrivateData
->HaveScannedDevice
[IdeChannel
* 2 + IdeDevice
]) {
440 // create child handle for the detected device.
442 IdeBlkIoDevice
[IdeChannel
][IdeDevice
] = AllocatePool (sizeof (IDE_BLK_IO_DEV
));
443 if (IdeBlkIoDevice
[IdeChannel
][IdeDevice
] == NULL
) {
447 IdeBlkIoDevicePtr
= IdeBlkIoDevice
[IdeChannel
][IdeDevice
];
449 ZeroMem (IdeBlkIoDevicePtr
, sizeof (IDE_BLK_IO_DEV
));
451 IdeBlkIoDevicePtr
->Signature
= IDE_BLK_IO_DEV_SIGNATURE
;
452 IdeBlkIoDevicePtr
->Channel
= IdeChannel
;
453 IdeBlkIoDevicePtr
->Device
= IdeDevice
;
456 // initialize Block IO interface's Media pointer
458 IdeBlkIoDevicePtr
->BlkIo
.Media
= &IdeBlkIoDevicePtr
->BlkMedia
;
461 // Initialize IDE IO port addresses, including Command Block registers
462 // and Control Block registers
464 IdeBlkIoDevicePtr
->IoPort
= AllocatePool (sizeof (IDE_BASE_REGISTERS
));
465 if (IdeBlkIoDevicePtr
->IoPort
== NULL
) {
469 ZeroMem (IdeBlkIoDevicePtr
->IoPort
, sizeof (IDE_BASE_REGISTERS
));
470 CommandBlockBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].CommandBlockBaseAddr
;
471 ControlBlockBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].ControlBlockBaseAddr
;
473 IdeBlkIoDevicePtr
->IoPort
->Data
= CommandBlockBaseAddr
;
474 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Reg1
) = (UINT16
) (CommandBlockBaseAddr
+ 0x01);
475 IdeBlkIoDevicePtr
->IoPort
->SectorCount
= (UINT16
) (CommandBlockBaseAddr
+ 0x02);
476 IdeBlkIoDevicePtr
->IoPort
->SectorNumber
= (UINT16
) (CommandBlockBaseAddr
+ 0x03);
477 IdeBlkIoDevicePtr
->IoPort
->CylinderLsb
= (UINT16
) (CommandBlockBaseAddr
+ 0x04);
478 IdeBlkIoDevicePtr
->IoPort
->CylinderMsb
= (UINT16
) (CommandBlockBaseAddr
+ 0x05);
479 IdeBlkIoDevicePtr
->IoPort
->Head
= (UINT16
) (CommandBlockBaseAddr
+ 0x06);
480 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Reg
) = (UINT16
) (CommandBlockBaseAddr
+ 0x07);
482 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Alt
) = ControlBlockBaseAddr
;
483 IdeBlkIoDevicePtr
->IoPort
->DriveAddress
= (UINT16
) (ControlBlockBaseAddr
+ 0x01);
485 IdeBlkIoDevicePtr
->IoPort
->MasterSlave
= (UINT16
) ((IdeDevice
== IdeMaster
) ? 1 : 0);
487 IdeBlkIoDevicePtr
->PciIo
= PciIo
;
488 IdeBlkIoDevicePtr
->IdeBusDriverPrivateData
= IdeBusDriverPrivateData
;
489 IdeBlkIoDevicePtr
->IoPort
->BusMasterBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].BusMasterBaseAddr
;
492 // Discover device, now!
494 PERF_START (0, "DiscoverIdeDevice", "IDE", 0);
495 Status
= DiscoverIdeDevice (IdeBlkIoDevicePtr
);
496 PERF_END (0, "DiscoverIdeDevice", "IDE", 0);
498 IdeBusDriverPrivateData
->HaveScannedDevice
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
499 IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
501 if (!EFI_ERROR (Status
)) {
505 ZeroMem (&NewNode
, sizeof (NewNode
));
506 NewNode
.DevPath
.Type
= MESSAGING_DEVICE_PATH
;
507 NewNode
.DevPath
.SubType
= MSG_ATAPI_DP
;
508 SetDevicePathNodeLength (&NewNode
.DevPath
, sizeof (ATAPI_DEVICE_PATH
));
510 NewNode
.Atapi
.PrimarySecondary
= (UINT8
) IdeBlkIoDevicePtr
->Channel
;
511 NewNode
.Atapi
.SlaveMaster
= (UINT8
) IdeBlkIoDevicePtr
->Device
;
512 NewNode
.Atapi
.Lun
= IdeBlkIoDevicePtr
->Lun
;
513 IdeBlkIoDevicePtr
->DevicePath
= AppendDevicePathNode (
517 if (IdeBlkIoDevicePtr
->DevicePath
== NULL
) {
518 ReleaseIdeResources (IdeBlkIoDevicePtr
);
523 // Submit identify data to IDE controller init driver
525 CopyMem (&IdentifyData
, IdeBlkIoDevicePtr
->pIdData
, sizeof (IdentifyData
));
526 // IdentifyData = *IdeBlkIoDevicePtr->pIdData;
527 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
528 IdeInit
->SubmitData (IdeInit
, IdeChannel
, IdeDevice
, &IdentifyData
);
531 // Device detection failed
533 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
534 IdeInit
->SubmitData (IdeInit
, IdeChannel
, IdeDevice
, NULL
);
535 ReleaseIdeResources (IdeBlkIoDevicePtr
);
536 IdeBlkIoDevicePtr
= NULL
;
539 // end of 1st inner loop ---
543 // end of 1st outer loop =========
548 // = 2nd outer loop == Primary/Secondary =================
550 for (IdeChannel
= BeginningIdeChannel
; IdeChannel
<= EndIdeChannel
; IdeChannel
++) {
553 // -- 2nd inner loop --- Master/Slave --------
555 for (IdeDevice
= BeginningIdeDevice
; IdeDevice
<= EndIdeDevice
; IdeDevice
++) {
557 if (IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
]) {
561 if (!IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
]) {
565 Status
= IdeInit
->CalculateMode (
571 if (EFI_ERROR (Status
)) {
572 DEBUG ((EFI_D_ERROR
, "[bStStp20S=%x]", Status
));
576 IdeBlkIoDevicePtr
= IdeBlkIoDevice
[IdeChannel
][IdeDevice
];
579 // Set best supported PIO mode on this IDE device
581 if (SupportedModes
->PioMode
.Mode
<= ATA_PIO_MODE_2
) {
582 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_DEFAULT_PIO
;
584 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_FLOW_PIO
;
587 TransferMode
.ModeNumber
= (UINT8
) (SupportedModes
->PioMode
.Mode
);
589 if (SupportedModes
->ExtModeCount
== 0){
590 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
592 if (EFI_ERROR (Status
)) {
593 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
594 ReleaseIdeResources (IdeBlkIoDevicePtr
);
595 IdeBlkIoDevicePtr
= NULL
;
601 // Set supported DMA mode on this IDE device. Note that UDMA & MDMA cann't
602 // be set together. Only one DMA mode can be set to a device. If setting
603 // DMA mode operation fails, we can continue moving on because we only use
604 // PIO mode at boot time. DMA modes are used by certain kind of OS booting
606 if (SupportedModes
->UdmaMode
.Valid
) {
608 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_UDMA
;
609 TransferMode
.ModeNumber
= (UINT8
) (SupportedModes
->UdmaMode
.Mode
);
610 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
612 if (EFI_ERROR (Status
)) {
613 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
614 ReleaseIdeResources (IdeBlkIoDevicePtr
);
615 IdeBlkIoDevicePtr
= NULL
;
619 EnableInterrupt (IdeBlkIoDevicePtr
);
620 } else if (SupportedModes
->MultiWordDmaMode
.Valid
) {
622 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_MDMA
;
623 TransferMode
.ModeNumber
= (UINT8
) SupportedModes
->MultiWordDmaMode
.Mode
;
624 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
626 if (EFI_ERROR (Status
)) {
627 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
628 ReleaseIdeResources (IdeBlkIoDevicePtr
);
629 IdeBlkIoDevicePtr
= NULL
;
633 EnableInterrupt (IdeBlkIoDevicePtr
);
636 // Init driver parameters
638 DriveParameters
.Sector
= (UINT8
) IdeBlkIoDevicePtr
->pIdData
->AtaData
.sectors_per_track
;
639 DriveParameters
.Heads
= (UINT8
) (IdeBlkIoDevicePtr
->pIdData
->AtaData
.heads
- 1);
640 DriveParameters
.MultipleSector
= (UINT8
) IdeBlkIoDevicePtr
->pIdData
->AtaData
.multi_sector_cmd_max_sct_cnt
;
642 // Set Parameters for the device:
644 // 2) Establish the block count for READ/WRITE MULTIPLE (EXT) command
646 if ((IdeBlkIoDevicePtr
->Type
== IdeHardDisk
) || (IdeBlkIoDevicePtr
->Type
== Ide48bitAddressingHardDisk
)) {
647 Status
= SetDriveParameters (IdeBlkIoDevicePtr
, &DriveParameters
);
651 // Record PIO mode used in private data
653 IdeBlkIoDevicePtr
->PioMode
= SupportedModes
->PioMode
.Mode
;
656 // Set IDE controller Timing Blocks in the PCI Configuration Space
658 IdeInit
->SetTiming (IdeInit
, IdeChannel
, IdeDevice
, SupportedModes
);
661 // Add Component Name for the IDE/ATAPI device that was discovered.
663 IdeBlkIoDevicePtr
->ControllerNameTable
= NULL
;
664 ADD_NAME (IdeBlkIoDevicePtr
);
666 Status
= gBS
->InstallMultipleProtocolInterfaces (
667 &IdeBlkIoDevicePtr
->Handle
,
668 &gEfiDevicePathProtocolGuid
,
669 IdeBlkIoDevicePtr
->DevicePath
,
670 &gEfiBlockIoProtocolGuid
,
671 &IdeBlkIoDevicePtr
->BlkIo
,
672 &gEfiDiskInfoProtocolGuid
,
673 &IdeBlkIoDevicePtr
->DiskInfo
,
677 if (EFI_ERROR (Status
)) {
678 ReleaseIdeResources (IdeBlkIoDevicePtr
);
683 &gEfiPciIoProtocolGuid
,
685 This
->DriverBindingHandle
,
686 IdeBlkIoDevicePtr
->Handle
,
687 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
690 IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
693 // Report status code: device eanbled!
695 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
697 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_ENABLE
),
698 IdeBlkIoDevicePtr
->DevicePath
702 // Create event to clear pending IDE interrupt
704 Status
= gBS
->CreateEvent (
705 EFI_EVENT_SIGNAL_EXIT_BOOT_SERVICES
,
714 // end of 2nd inner loop ----
718 // end of 2nd outer loop ==========
723 // All configurations done! Notify IdeController to do post initialization
724 // work such as saving IDE controller PCI settings for S3 resume
726 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBusPhaseMaximum
, 0);
728 if (SupportedModes
!= NULL
) {
729 gBS
->FreePool (SupportedModes
);
732 PERF_START (0, "Finish IDE detection", "IDE", 1);
733 PERF_END (0, "Finish IDE detection", "IDE", 0);
740 // Report error code: controller error
742 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
743 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
744 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_EC_CONTROLLER_ERROR
),
750 &gEfiIdeControllerInitProtocolGuid
,
751 This
->DriverBindingHandle
,
755 gBS
->UninstallMultipleProtocolInterfaces (
758 IdeBusDriverPrivateData
,
762 if (IdeBusDriverPrivateData
!= NULL
) {
763 gBS
->FreePool (IdeBusDriverPrivateData
);
766 if (SupportedModes
!= NULL
) {
767 gBS
->FreePool (SupportedModes
);
772 &gEfiPciIoProtocolGuid
,
773 This
->DriverBindingHandle
,
779 &gEfiDevicePathProtocolGuid
,
780 This
->DriverBindingHandle
,
789 // ***********************************************************************************
790 // IDEBusDriverBindingStop
791 // ***********************************************************************************
794 Stop this driver on Controller Handle.
796 @param This Protocol instance pointer.
797 @param DeviceHandle Handle of device to stop driver on
798 @param NumberOfChildren Not used
799 @param ChildHandleBuffer Not used
801 @retval EFI_SUCCESS This driver is removed DeviceHandle
802 @retval other This driver was not removed from this device
807 IDEBusDriverBindingStop (
808 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
809 IN EFI_HANDLE Controller
,
810 IN UINTN NumberOfChildren
,
811 IN EFI_HANDLE
*ChildHandleBuffer
813 // TODO: Controller - add argument and description to function comment
814 // TODO: EFI_DEVICE_ERROR - add return value to function comment
817 EFI_PCI_IO_PROTOCOL
*PciIo
;
818 BOOLEAN AllChildrenStopped
;
820 IDE_BUS_DRIVER_PRIVATE_DATA
*IdeBusDriverPrivateData
;
822 IdeBusDriverPrivateData
= NULL
;
824 if (NumberOfChildren
== 0) {
826 Status
= gBS
->OpenProtocol (
828 &gEfiPciIoProtocolGuid
,
830 This
->DriverBindingHandle
,
832 EFI_OPEN_PROTOCOL_GET_PROTOCOL
834 if (!EFI_ERROR (Status
)) {
837 EfiPciIoAttributeOperationDisable
,
838 EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO
| EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO
| EFI_PCI_DEVICE_ENABLE
,
846 (VOID
**) &IdeBusDriverPrivateData
,
847 This
->DriverBindingHandle
,
849 EFI_OPEN_PROTOCOL_GET_PROTOCOL
852 gBS
->UninstallMultipleProtocolInterfaces (
855 IdeBusDriverPrivateData
,
859 if (IdeBusDriverPrivateData
!= NULL
) {
860 gBS
->FreePool (IdeBusDriverPrivateData
);
863 // Close the bus driver
867 &gEfiIdeControllerInitProtocolGuid
,
868 This
->DriverBindingHandle
,
873 &gEfiPciIoProtocolGuid
,
874 This
->DriverBindingHandle
,
879 &gEfiDevicePathProtocolGuid
,
880 This
->DriverBindingHandle
,
887 AllChildrenStopped
= TRUE
;
889 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
891 Status
= DeRegisterIdeDevice (This
, Controller
, ChildHandleBuffer
[Index
]);
893 if (EFI_ERROR (Status
)) {
894 AllChildrenStopped
= FALSE
;
898 if (!AllChildrenStopped
) {
899 return EFI_DEVICE_ERROR
;
906 // ***********************************************************************************
907 // DeRegisterIdeDevice
908 // ***********************************************************************************
911 Deregister an IDE device and free resources
913 @param This Protocol instance pointer.
914 @param Controller Ide device handle
915 @param Handle Handle of device to deregister driver on
921 DeRegisterIdeDevice (
922 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
923 IN EFI_HANDLE Controller
,
926 // TODO: EFI_SUCCESS - add return value to function comment
929 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
930 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
931 EFI_PCI_IO_PROTOCOL
*PciIo
;
934 Status
= gBS
->OpenProtocol (
936 &gEfiBlockIoProtocolGuid
,
938 This
->DriverBindingHandle
,
940 EFI_OPEN_PROTOCOL_GET_PROTOCOL
942 if (EFI_ERROR (Status
)) {
946 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (BlkIo
);
949 // Report Status code: Device disabled
951 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
953 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_DISABLE
),
954 IdeBlkIoDevice
->DevicePath
958 // Close the child handle
960 Status
= gBS
->CloseProtocol (
962 &gEfiPciIoProtocolGuid
,
963 This
->DriverBindingHandle
,
967 Status
= gBS
->UninstallMultipleProtocolInterfaces (
969 &gEfiDevicePathProtocolGuid
,
970 IdeBlkIoDevice
->DevicePath
,
971 &gEfiBlockIoProtocolGuid
,
972 &IdeBlkIoDevice
->BlkIo
,
973 &gEfiDiskInfoProtocolGuid
,
974 &IdeBlkIoDevice
->DiskInfo
,
978 if (EFI_ERROR (Status
)) {
981 &gEfiPciIoProtocolGuid
,
983 This
->DriverBindingHandle
,
985 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
991 // Release allocated resources
993 Index
= IdeBlkIoDevice
->Channel
* 2 + IdeBlkIoDevice
->Device
;
994 IdeBlkIoDevice
->IdeBusDriverPrivateData
->HaveScannedDevice
[Index
] = FALSE
;
996 ReleaseIdeResources (IdeBlkIoDevice
);
1002 // ***********************************************************************************
1004 // ***********************************************************************************
1007 TODO: This - add argument and description to function comment
1008 TODO: ExtendedVerification - add argument and description to function comment
1009 TODO: EFI_DEVICE_ERROR - add return value to function comment
1015 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1016 IN BOOLEAN ExtendedVerification
1019 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1022 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1024 // Requery IDE IO resources in case of the switch of native and legacy modes
1026 ReassignIdeResources (IdeBlkIoDevice
);
1029 // for ATA device, using ATA reset method
1031 if (IdeBlkIoDevice
->Type
== IdeHardDisk
) {
1032 return AtaSoftReset (IdeBlkIoDevice
);
1035 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1036 return EFI_DEVICE_ERROR
;
1040 // for ATAPI device, using ATAPI reset method
1042 Status
= AtapiSoftReset (IdeBlkIoDevice
);
1043 if (ExtendedVerification
) {
1044 Status
= AtaSoftReset (IdeBlkIoDevice
);
1051 Read data from block io device
1053 @param This Protocol instance pointer.
1054 @param MediaId The media ID of the device
1055 @param LBA Starting LBA address to read data
1056 @param BufferSize The size of data to be read
1057 @param Buffer Caller supplied buffer to save data
1059 @return read data status
1064 IDEBlkIoReadBlocks (
1065 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1068 IN UINTN BufferSize
,
1071 // TODO: EFI_DEVICE_ERROR - add return value to function comment
1073 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1075 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1078 // Requery IDE IO resources in case of the switch of native and legacy modes
1080 ReassignIdeResources (IdeBlkIoDevice
);
1083 // For ATA compatible device, use ATA read block's mechanism
1085 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1086 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1087 return AtaBlkIoReadBlocks (
1096 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1097 return EFI_DEVICE_ERROR
;
1101 // for ATAPI device, using ATAPI read block's mechanism
1103 return AtapiBlkIoReadBlocks (
1114 Write data to block io device
1116 @param This Protocol instance pointer.
1117 @param MediaId The media ID of the device
1118 @param LBA Starting LBA address to write data
1119 @param BufferSize The size of data to be written
1120 @param Buffer Caller supplied buffer to save data
1122 @return write data status
1127 IDEBlkIoWriteBlocks (
1128 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1131 IN UINTN BufferSize
,
1134 // TODO: EFI_DEVICE_ERROR - add return value to function comment
1136 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1138 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1140 // Requery IDE IO resources in case of the switch of native and legacy modes
1142 ReassignIdeResources (IdeBlkIoDevice
);
1145 // for ATA device, using ATA write block's mechanism
1147 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1148 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1150 return AtaBlkIoWriteBlocks (
1159 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1160 return EFI_DEVICE_ERROR
;
1164 // for ATAPI device, using ATAPI write block's mechanism
1166 return AtapiBlkIoWriteBlocks (
1176 // ***********************************************************************************
1177 // IDEBlkIoFlushBlocks
1178 // ***********************************************************************************
1181 TODO: This - add argument and description to function comment
1182 TODO: EFI_SUCCESS - add return value to function comment
1186 IDEBlkIoFlushBlocks (
1187 IN EFI_BLOCK_IO_PROTOCOL
*This
1197 Return the results of the Inquiry command to a drive in InquiryData.
1198 Data format of Inquiry data is defined by the Interface GUID.
1200 @param This Protocol instance pointer.
1201 @param InquiryData Results of Inquiry command to device
1202 @param InquiryDataSize Size of InquiryData in bytes.
1204 @retval EFI_SUCCESS InquiryData valid
1205 @retval EFI_NOT_FOUND Device does not support this data class
1206 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
1207 @retval EFI_BUFFER_TOO_SMALL IntquiryDataSize not big enough
1212 IDEDiskInfoInquiry (
1213 IN EFI_DISK_INFO_PROTOCOL
*This
,
1214 IN OUT VOID
*InquiryData
,
1215 IN OUT UINT32
*InquiryDataSize
1218 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1220 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1222 if (*InquiryDataSize
< sizeof (INQUIRY_DATA
)) {
1223 *InquiryDataSize
= sizeof (INQUIRY_DATA
);
1224 return EFI_BUFFER_TOO_SMALL
;
1227 if (IdeBlkIoDevice
->pInquiryData
== NULL
) {
1228 return EFI_NOT_FOUND
;
1231 gBS
->CopyMem (InquiryData
, IdeBlkIoDevice
->pInquiryData
, sizeof (INQUIRY_DATA
));
1232 *InquiryDataSize
= sizeof (INQUIRY_DATA
);
1238 Return the results of the Identify command to a drive in IdentifyData.
1239 Data format of Identify data is defined by the Interface GUID.
1241 @param This Protocol instance pointer.
1242 @param IdentifyData Results of Identify command to device
1243 @param IdentifyDataSize Size of IdentifyData in bytes.
1245 @retval EFI_SUCCESS IdentifyData valid
1246 @retval EFI_NOT_FOUND Device does not support this data class
1247 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
1248 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
1253 IDEDiskInfoIdentify (
1254 IN EFI_DISK_INFO_PROTOCOL
*This
,
1255 IN OUT VOID
*IdentifyData
,
1256 IN OUT UINT32
*IdentifyDataSize
1259 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1261 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1263 if (*IdentifyDataSize
< sizeof (EFI_IDENTIFY_DATA
)) {
1264 *IdentifyDataSize
= sizeof (EFI_IDENTIFY_DATA
);
1265 return EFI_BUFFER_TOO_SMALL
;
1268 if (IdeBlkIoDevice
->pIdData
== NULL
) {
1269 return EFI_NOT_FOUND
;
1272 gBS
->CopyMem (IdentifyData
, IdeBlkIoDevice
->pIdData
, sizeof (EFI_IDENTIFY_DATA
));
1273 *IdentifyDataSize
= sizeof (EFI_IDENTIFY_DATA
);
1279 Return the results of the Request Sense command to a drive in SenseData.
1280 Data format of Sense data is defined by the Interface GUID.
1282 @param This Protocol instance pointer.
1283 @param SenseData Results of Request Sense command to device
1284 @param SenseDataSize Size of SenseData in bytes.
1285 @param SenseDataNumber Type of SenseData
1287 @retval EFI_SUCCESS InquiryData valid
1288 @retval EFI_NOT_FOUND Device does not support this data class
1289 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
1290 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough
1295 IDEDiskInfoSenseData (
1296 IN EFI_DISK_INFO_PROTOCOL
*This
,
1297 IN OUT VOID
*SenseData
,
1298 IN OUT UINT32
*SenseDataSize
,
1299 OUT UINT8
*SenseDataNumber
1302 return EFI_NOT_FOUND
;
1306 Return the results of the Request Sense command to a drive in SenseData.
1307 Data format of Sense data is defined by the Interface GUID.
1309 @param This Protocol instance pointer.
1310 @param IdeChannel Primary or Secondary
1311 @param IdeDevice Master or Slave
1313 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid
1314 @retval EFI_UNSUPPORTED This is not an IDE device
1319 IDEDiskInfoWhichIde (
1320 IN EFI_DISK_INFO_PROTOCOL
*This
,
1321 OUT UINT32
*IdeChannel
,
1322 OUT UINT32
*IdeDevice
1325 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1327 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1328 *IdeChannel
= IdeBlkIoDevice
->Channel
;
1329 *IdeDevice
= IdeBlkIoDevice
->Device
;