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
;
188 // Local variables declaration for IdeControllerInit support
190 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeInit
;
192 BOOLEAN ChannelEnabled
;
195 EFI_IDENTIFY_DATA IdentifyData
;
196 EFI_ATA_COLLECTIVE_MODE
*SupportedModes
;
198 IdeBusDriverPrivateData
= NULL
;
199 SupportedModes
= NULL
;
202 // Perform IdeBus initialization
204 Status
= gBS
->OpenProtocol (
206 &gEfiDevicePathProtocolGuid
,
207 (VOID
**) &ParentDevicePath
,
208 This
->DriverBindingHandle
,
210 EFI_OPEN_PROTOCOL_BY_DRIVER
212 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
217 // Now open the IDE_CONTROLLER_INIT protocol. Step7.1
219 Status
= gBS
->OpenProtocol (
221 &gEfiIdeControllerInitProtocolGuid
,
223 This
->DriverBindingHandle
,
225 EFI_OPEN_PROTOCOL_BY_DRIVER
229 // The following OpenProtocol function with _GET_PROTOCOL attribute and
230 // will not return EFI_ALREADY_STARTED, so save it for now
232 SavedStatus
= Status
;
234 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
235 DEBUG ((EFI_D_ERROR
, "Open Init, Status=%x", Status
));
237 // open protocol is not SUCCESS or not ALREADY_STARTED, error exit
243 // Save Enumall and ChannelCount. Step7.2
245 EnumAll
= IdeInit
->EnumAll
;
246 ChannelCount
= IdeInit
->ChannelCount
;
249 // Consume PCI I/O protocol. Note that the OpenProtocol with _GET_PROTOCOL
250 // attribute will not return EFI_ALREADY_STARTED
252 Status
= gBS
->OpenProtocol (
254 &gEfiPciIoProtocolGuid
,
256 This
->DriverBindingHandle
,
258 EFI_OPEN_PROTOCOL_GET_PROTOCOL
260 if (EFI_ERROR (Status
)) {
261 DEBUG ((EFI_D_ERROR
, "Open PciIo, Status=%x", Status
));
266 // We must check EFI_ALREADY_STARTED because many ATAPI devices are removable
268 if (SavedStatus
!= EFI_ALREADY_STARTED
) {
269 IdeBusDriverPrivateData
= AllocatePool (sizeof (IDE_BUS_DRIVER_PRIVATE_DATA
));
270 if (IdeBusDriverPrivateData
== NULL
) {
271 Status
= EFI_OUT_OF_RESOURCES
;
275 ZeroMem (IdeBusDriverPrivateData
, sizeof (IDE_BUS_DRIVER_PRIVATE_DATA
));
276 Status
= gBS
->InstallMultipleProtocolInterfaces (
279 IdeBusDriverPrivateData
,
282 if (EFI_ERROR (Status
)) {
287 Status
= gBS
->OpenProtocol (
290 (VOID
**) &IdeBusDriverPrivateData
,
291 This
->DriverBindingHandle
,
293 EFI_OPEN_PROTOCOL_GET_PROTOCOL
295 if (EFI_ERROR (Status
)) {
296 IdeBusDriverPrivateData
= NULL
;
301 Status
= PciIo
->Attributes (
303 EfiPciIoAttributeOperationEnable
,
304 EFI_PCI_DEVICE_ENABLE
,
307 if (EFI_ERROR (Status
)) {
312 // Read the environment variable that contains the IDEBus Driver's
313 // Config options that were set by the Driver Configuration Protocol
315 DataSize
= sizeof (ConfigurationOptions
);
316 Status
= gRT
->GetVariable (
317 (CHAR16
*) L
"Configuration",
321 &ConfigurationOptions
323 if (EFI_ERROR (Status
)) {
324 ConfigurationOptions
= 0x0f;
329 // If IdeInit->EnumAll is TRUE, must enumerate all IDE device anyway
331 BeginningIdeChannel
= IdePrimary
;
332 EndIdeChannel
= IdeSecondary
;
333 BeginningIdeDevice
= IdeMaster
;
334 EndIdeDevice
= IdeSlave
;
335 } else if (RemainingDevicePath
== NULL
) {
337 // RemainingDevicePath is NULL, scan IDE bus for each device;
339 BeginningIdeChannel
= IdePrimary
;
340 EndIdeChannel
= IdeSecondary
;
341 BeginningIdeDevice
= IdeMaster
;
343 // default, may be redefined by IdeInit
345 EndIdeDevice
= IdeSlave
;
348 // RemainingDevicePath is not NULL, only scan the specified device.
350 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
351 BeginningIdeChannel
= Node
->Atapi
.PrimarySecondary
;
352 EndIdeChannel
= BeginningIdeChannel
;
353 BeginningIdeDevice
= Node
->Atapi
.SlaveMaster
;
354 EndIdeDevice
= BeginningIdeDevice
;
358 // Obtain IDE IO port registers' base addresses
360 Status
= GetIdeRegistersBaseAddr (PciIo
, IdeRegsBaseAddr
);
361 if (EFI_ERROR (Status
)) {
366 // Report status code: begin IdeBus initialization
368 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
370 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_PC_RESET
),
375 // Strictly follow the enumeration based on IDE_CONTROLLER_INIT protocol
377 for (IdeChannel
= BeginningIdeChannel
; IdeChannel
<= EndIdeChannel
; IdeChannel
++) {
379 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBeforeChannelEnumeration
, IdeChannel
);
382 // now obtain channel information fron IdeControllerInit protocol. Step9
384 Status
= IdeInit
->GetChannelInfo (
390 if (EFI_ERROR (Status
)) {
391 DEBUG ((EFI_D_ERROR
, "[GetChannel, Status=%x]", Status
));
395 if (!ChannelEnabled
) {
399 EndIdeDevice
= (UINT8
) EFI_MIN ((MaxDevices
- 1), EndIdeDevice
);
402 // Now inform the IDE Controller Init Module. Sept10
404 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBeforeChannelReset
, IdeChannel
);
407 // No reset channel function implemented. Sept11
409 IdeInit
->NotifyPhase (IdeInit
, EfiIdeAfterChannelReset
, IdeChannel
);
414 IdeInit
->NotifyPhase (
416 EfiIdeBusBeforeDevicePresenceDetection
,
420 // -- 1st inner loop --- Master/Slave ------------ Step14
422 for (IdeDevice
= BeginningIdeDevice
; IdeDevice
<= EndIdeDevice
; IdeDevice
++) {
424 // Check whether the configuration options allow this device
426 if (!(ConfigurationOptions
& (1 << (IdeChannel
* 2 + IdeDevice
)))) {
431 // The device has been scanned in another Start(), No need to scan it again
432 // for perf optimization.
434 if (IdeBusDriverPrivateData
->HaveScannedDevice
[IdeChannel
* 2 + IdeDevice
]) {
439 // create child handle for the detected device.
441 IdeBlkIoDevice
[IdeChannel
][IdeDevice
] = AllocatePool (sizeof (IDE_BLK_IO_DEV
));
442 if (IdeBlkIoDevice
[IdeChannel
][IdeDevice
] == NULL
) {
446 IdeBlkIoDevicePtr
= IdeBlkIoDevice
[IdeChannel
][IdeDevice
];
448 ZeroMem (IdeBlkIoDevicePtr
, sizeof (IDE_BLK_IO_DEV
));
450 IdeBlkIoDevicePtr
->Signature
= IDE_BLK_IO_DEV_SIGNATURE
;
451 IdeBlkIoDevicePtr
->Channel
= IdeChannel
;
452 IdeBlkIoDevicePtr
->Device
= IdeDevice
;
455 // initialize Block IO interface's Media pointer
457 IdeBlkIoDevicePtr
->BlkIo
.Media
= &IdeBlkIoDevicePtr
->BlkMedia
;
460 // Initialize IDE IO port addresses, including Command Block registers
461 // and Control Block registers
463 IdeBlkIoDevicePtr
->IoPort
= AllocatePool (sizeof (IDE_BASE_REGISTERS
));
464 if (IdeBlkIoDevicePtr
->IoPort
== NULL
) {
468 ZeroMem (IdeBlkIoDevicePtr
->IoPort
, sizeof (IDE_BASE_REGISTERS
));
469 CommandBlockBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].CommandBlockBaseAddr
;
470 ControlBlockBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].ControlBlockBaseAddr
;
472 IdeBlkIoDevicePtr
->IoPort
->Data
= CommandBlockBaseAddr
;
473 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Reg1
) = (UINT16
) (CommandBlockBaseAddr
+ 0x01);
474 IdeBlkIoDevicePtr
->IoPort
->SectorCount
= (UINT16
) (CommandBlockBaseAddr
+ 0x02);
475 IdeBlkIoDevicePtr
->IoPort
->SectorNumber
= (UINT16
) (CommandBlockBaseAddr
+ 0x03);
476 IdeBlkIoDevicePtr
->IoPort
->CylinderLsb
= (UINT16
) (CommandBlockBaseAddr
+ 0x04);
477 IdeBlkIoDevicePtr
->IoPort
->CylinderMsb
= (UINT16
) (CommandBlockBaseAddr
+ 0x05);
478 IdeBlkIoDevicePtr
->IoPort
->Head
= (UINT16
) (CommandBlockBaseAddr
+ 0x06);
479 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Reg
) = (UINT16
) (CommandBlockBaseAddr
+ 0x07);
481 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Alt
) = ControlBlockBaseAddr
;
482 IdeBlkIoDevicePtr
->IoPort
->DriveAddress
= (UINT16
) (ControlBlockBaseAddr
+ 0x01);
484 IdeBlkIoDevicePtr
->IoPort
->MasterSlave
= (UINT16
) ((IdeDevice
== IdeMaster
) ? 1 : 0);
486 IdeBlkIoDevicePtr
->PciIo
= PciIo
;
487 IdeBlkIoDevicePtr
->IdeBusDriverPrivateData
= IdeBusDriverPrivateData
;
488 IdeBlkIoDevicePtr
->IoPort
->BusMasterBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].BusMasterBaseAddr
;
491 // Discover device, now!
493 PERF_START (0, "DiscoverIdeDevice", "IDE", 0);
494 Status
= DiscoverIdeDevice (IdeBlkIoDevicePtr
);
495 PERF_END (0, "DiscoverIdeDevice", "IDE", 0);
497 IdeBusDriverPrivateData
->HaveScannedDevice
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
498 IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
500 if (!EFI_ERROR (Status
)) {
504 ZeroMem (&NewNode
, sizeof (NewNode
));
505 NewNode
.DevPath
.Type
= MESSAGING_DEVICE_PATH
;
506 NewNode
.DevPath
.SubType
= MSG_ATAPI_DP
;
507 SetDevicePathNodeLength (&NewNode
.DevPath
, sizeof (ATAPI_DEVICE_PATH
));
509 NewNode
.Atapi
.PrimarySecondary
= (UINT8
) IdeBlkIoDevicePtr
->Channel
;
510 NewNode
.Atapi
.SlaveMaster
= (UINT8
) IdeBlkIoDevicePtr
->Device
;
511 NewNode
.Atapi
.Lun
= IdeBlkIoDevicePtr
->Lun
;
512 IdeBlkIoDevicePtr
->DevicePath
= AppendDevicePathNode (
516 if (IdeBlkIoDevicePtr
->DevicePath
== NULL
) {
517 ReleaseIdeResources (IdeBlkIoDevicePtr
);
522 // Submit identify data to IDE controller init driver
524 CopyMem (&IdentifyData
, IdeBlkIoDevicePtr
->pIdData
, sizeof (IdentifyData
));
525 // IdentifyData = *IdeBlkIoDevicePtr->pIdData;
526 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
527 IdeInit
->SubmitData (IdeInit
, IdeChannel
, IdeDevice
, &IdentifyData
);
530 // Device detection failed
532 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
533 IdeInit
->SubmitData (IdeInit
, IdeChannel
, IdeDevice
, NULL
);
534 ReleaseIdeResources (IdeBlkIoDevicePtr
);
535 IdeBlkIoDevicePtr
= NULL
;
538 // end of 1st inner loop ---
542 // end of 1st outer loop =========
547 // = 2nd outer loop == Primary/Secondary =================
549 for (IdeChannel
= BeginningIdeChannel
; IdeChannel
<= EndIdeChannel
; IdeChannel
++) {
552 // -- 2nd inner loop --- Master/Slave --------
554 for (IdeDevice
= BeginningIdeDevice
; IdeDevice
<= EndIdeDevice
; IdeDevice
++) {
556 if (IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
]) {
560 if (!IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
]) {
564 Status
= IdeInit
->CalculateMode (
570 if (EFI_ERROR (Status
)) {
571 DEBUG ((EFI_D_ERROR
, "[bStStp20S=%x]", Status
));
575 IdeBlkIoDevicePtr
= IdeBlkIoDevice
[IdeChannel
][IdeDevice
];
578 // Set best supported PIO mode on this IDE device
580 if (SupportedModes
->PioMode
.Mode
<= ATA_PIO_MODE_2
) {
581 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_DEFAULT_PIO
;
583 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_FLOW_PIO
;
586 TransferMode
.ModeNumber
= (UINT8
) (SupportedModes
->PioMode
.Mode
);
588 if (SupportedModes
->ExtModeCount
== 0){
589 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
591 if (EFI_ERROR (Status
)) {
592 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
593 ReleaseIdeResources (IdeBlkIoDevicePtr
);
594 IdeBlkIoDevicePtr
= NULL
;
600 // Set supported DMA mode on this IDE device. Note that UDMA & MDMA cann't
601 // be set together. Only one DMA mode can be set to a device. If setting
602 // DMA mode operation fails, we can continue moving on because we only use
603 // PIO mode at boot time. DMA modes are used by certain kind of OS booting
605 if (SupportedModes
->UdmaMode
.Valid
) {
607 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_UDMA
;
608 TransferMode
.ModeNumber
= (UINT8
) (SupportedModes
->UdmaMode
.Mode
);
609 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
611 if (EFI_ERROR (Status
)) {
612 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
613 ReleaseIdeResources (IdeBlkIoDevicePtr
);
614 IdeBlkIoDevicePtr
= NULL
;
618 EnableInterrupt (IdeBlkIoDevicePtr
);
619 } else if (SupportedModes
->MultiWordDmaMode
.Valid
) {
621 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_MDMA
;
622 TransferMode
.ModeNumber
= (UINT8
) SupportedModes
->MultiWordDmaMode
.Mode
;
623 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
625 if (EFI_ERROR (Status
)) {
626 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
627 ReleaseIdeResources (IdeBlkIoDevicePtr
);
628 IdeBlkIoDevicePtr
= NULL
;
632 EnableInterrupt (IdeBlkIoDevicePtr
);
635 // Init driver parameters
637 DriveParameters
.Sector
= (UINT8
) IdeBlkIoDevicePtr
->pIdData
->AtaData
.sectors_per_track
;
638 DriveParameters
.Heads
= (UINT8
) (IdeBlkIoDevicePtr
->pIdData
->AtaData
.heads
- 1);
639 DriveParameters
.MultipleSector
= (UINT8
) IdeBlkIoDevicePtr
->pIdData
->AtaData
.multi_sector_cmd_max_sct_cnt
;
641 // Set Parameters for the device:
643 // 2) Establish the block count for READ/WRITE MULTIPLE (EXT) command
645 if ((IdeBlkIoDevicePtr
->Type
== IdeHardDisk
) || (IdeBlkIoDevicePtr
->Type
== Ide48bitAddressingHardDisk
)) {
646 Status
= SetDriveParameters (IdeBlkIoDevicePtr
, &DriveParameters
);
650 // Record PIO mode used in private data
652 IdeBlkIoDevicePtr
->PioMode
= SupportedModes
->PioMode
.Mode
;
655 // Set IDE controller Timing Blocks in the PCI Configuration Space
657 IdeInit
->SetTiming (IdeInit
, IdeChannel
, IdeDevice
, SupportedModes
);
660 // Add Component Name for the IDE/ATAPI device that was discovered.
662 IdeBlkIoDevicePtr
->ControllerNameTable
= NULL
;
663 ADD_NAME (IdeBlkIoDevicePtr
);
665 Status
= gBS
->InstallMultipleProtocolInterfaces (
666 &IdeBlkIoDevicePtr
->Handle
,
667 &gEfiDevicePathProtocolGuid
,
668 IdeBlkIoDevicePtr
->DevicePath
,
669 &gEfiBlockIoProtocolGuid
,
670 &IdeBlkIoDevicePtr
->BlkIo
,
671 &gEfiDiskInfoProtocolGuid
,
672 &IdeBlkIoDevicePtr
->DiskInfo
,
676 if (EFI_ERROR (Status
)) {
677 ReleaseIdeResources (IdeBlkIoDevicePtr
);
682 &gEfiPciIoProtocolGuid
,
684 This
->DriverBindingHandle
,
685 IdeBlkIoDevicePtr
->Handle
,
686 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
689 IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
692 // Report status code: device eanbled!
694 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
696 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_ENABLE
),
697 IdeBlkIoDevicePtr
->DevicePath
700 // end of 2nd inner loop ----
704 // end of 2nd outer loop ==========
709 // All configurations done! Notify IdeController to do post initialization
710 // work such as saving IDE controller PCI settings for S3 resume
712 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBusPhaseMaximum
, 0);
714 if (SupportedModes
!= NULL
) {
715 gBS
->FreePool (SupportedModes
);
718 PERF_START (0, "Finish IDE detection", "IDE", 1);
719 PERF_END (0, "Finish IDE detection", "IDE", 0);
726 // Report error code: controller error
728 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
729 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
730 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_EC_CONTROLLER_ERROR
),
736 &gEfiIdeControllerInitProtocolGuid
,
737 This
->DriverBindingHandle
,
741 gBS
->UninstallMultipleProtocolInterfaces (
744 IdeBusDriverPrivateData
,
748 if (IdeBusDriverPrivateData
!= NULL
) {
749 gBS
->FreePool (IdeBusDriverPrivateData
);
752 if (SupportedModes
!= NULL
) {
753 gBS
->FreePool (SupportedModes
);
758 &gEfiPciIoProtocolGuid
,
759 This
->DriverBindingHandle
,
765 &gEfiDevicePathProtocolGuid
,
766 This
->DriverBindingHandle
,
775 // ***********************************************************************************
776 // IDEBusDriverBindingStop
777 // ***********************************************************************************
780 Stop this driver on Controller Handle.
782 @param This Protocol instance pointer.
783 @param DeviceHandle Handle of device to stop driver on
784 @param NumberOfChildren Not used
785 @param ChildHandleBuffer Not used
787 @retval EFI_SUCCESS This driver is removed DeviceHandle
788 @retval other This driver was not removed from this device
793 IDEBusDriverBindingStop (
794 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
795 IN EFI_HANDLE Controller
,
796 IN UINTN NumberOfChildren
,
797 IN EFI_HANDLE
*ChildHandleBuffer
799 // TODO: Controller - add argument and description to function comment
800 // TODO: EFI_DEVICE_ERROR - add return value to function comment
803 EFI_PCI_IO_PROTOCOL
*PciIo
;
804 BOOLEAN AllChildrenStopped
;
806 IDE_BUS_DRIVER_PRIVATE_DATA
*IdeBusDriverPrivateData
;
808 IdeBusDriverPrivateData
= NULL
;
810 if (NumberOfChildren
== 0) {
812 Status
= gBS
->OpenProtocol (
814 &gEfiPciIoProtocolGuid
,
816 This
->DriverBindingHandle
,
818 EFI_OPEN_PROTOCOL_GET_PROTOCOL
820 if (!EFI_ERROR (Status
)) {
823 EfiPciIoAttributeOperationDisable
,
824 EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO
| EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO
| EFI_PCI_DEVICE_ENABLE
,
832 (VOID
**) &IdeBusDriverPrivateData
,
833 This
->DriverBindingHandle
,
835 EFI_OPEN_PROTOCOL_GET_PROTOCOL
838 gBS
->UninstallMultipleProtocolInterfaces (
841 IdeBusDriverPrivateData
,
845 if (IdeBusDriverPrivateData
!= NULL
) {
846 gBS
->FreePool (IdeBusDriverPrivateData
);
849 // Close the bus driver
853 &gEfiIdeControllerInitProtocolGuid
,
854 This
->DriverBindingHandle
,
859 &gEfiPciIoProtocolGuid
,
860 This
->DriverBindingHandle
,
865 &gEfiDevicePathProtocolGuid
,
866 This
->DriverBindingHandle
,
873 AllChildrenStopped
= TRUE
;
875 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
877 Status
= DeRegisterIdeDevice (This
, Controller
, ChildHandleBuffer
[Index
]);
879 if (EFI_ERROR (Status
)) {
880 AllChildrenStopped
= FALSE
;
884 if (!AllChildrenStopped
) {
885 return EFI_DEVICE_ERROR
;
892 // ***********************************************************************************
893 // DeRegisterIdeDevice
894 // ***********************************************************************************
897 Deregister an IDE device and free resources
899 @param This Protocol instance pointer.
900 @param Controller Ide device handle
901 @param Handle Handle of device to deregister driver on
907 DeRegisterIdeDevice (
908 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
909 IN EFI_HANDLE Controller
,
912 // TODO: EFI_SUCCESS - add return value to function comment
915 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
916 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
917 EFI_PCI_IO_PROTOCOL
*PciIo
;
920 Status
= gBS
->OpenProtocol (
922 &gEfiBlockIoProtocolGuid
,
924 This
->DriverBindingHandle
,
926 EFI_OPEN_PROTOCOL_GET_PROTOCOL
928 if (EFI_ERROR (Status
)) {
932 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (BlkIo
);
935 // Report Status code: Device disabled
937 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
939 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_DISABLE
),
940 IdeBlkIoDevice
->DevicePath
944 // Close the child handle
946 Status
= gBS
->CloseProtocol (
948 &gEfiPciIoProtocolGuid
,
949 This
->DriverBindingHandle
,
953 Status
= gBS
->UninstallMultipleProtocolInterfaces (
955 &gEfiDevicePathProtocolGuid
,
956 IdeBlkIoDevice
->DevicePath
,
957 &gEfiBlockIoProtocolGuid
,
958 &IdeBlkIoDevice
->BlkIo
,
959 &gEfiDiskInfoProtocolGuid
,
960 &IdeBlkIoDevice
->DiskInfo
,
964 if (EFI_ERROR (Status
)) {
967 &gEfiPciIoProtocolGuid
,
969 This
->DriverBindingHandle
,
971 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
977 // Release allocated resources
979 Index
= IdeBlkIoDevice
->Channel
* 2 + IdeBlkIoDevice
->Device
;
980 IdeBlkIoDevice
->IdeBusDriverPrivateData
->HaveScannedDevice
[Index
] = FALSE
;
982 ReleaseIdeResources (IdeBlkIoDevice
);
988 // ***********************************************************************************
990 // ***********************************************************************************
993 TODO: This - add argument and description to function comment
994 TODO: ExtendedVerification - add argument and description to function comment
995 TODO: EFI_DEVICE_ERROR - add return value to function comment
1001 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1002 IN BOOLEAN ExtendedVerification
1005 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1008 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1010 // Requery IDE IO resources in case of the switch of native and legacy modes
1012 ReassignIdeResources (IdeBlkIoDevice
);
1015 // for ATA device, using ATA reset method
1017 if (IdeBlkIoDevice
->Type
== IdeHardDisk
) {
1018 return AtaSoftReset (IdeBlkIoDevice
);
1021 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1022 return EFI_DEVICE_ERROR
;
1026 // for ATAPI device, using ATAPI reset method
1028 Status
= AtapiSoftReset (IdeBlkIoDevice
);
1029 if (ExtendedVerification
) {
1030 Status
= AtaSoftReset (IdeBlkIoDevice
);
1037 Read data from block io device
1039 @param This Protocol instance pointer.
1040 @param MediaId The media ID of the device
1041 @param LBA Starting LBA address to read data
1042 @param BufferSize The size of data to be read
1043 @param Buffer Caller supplied buffer to save data
1045 @return read data status
1050 IDEBlkIoReadBlocks (
1051 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1054 IN UINTN BufferSize
,
1057 // TODO: EFI_DEVICE_ERROR - add return value to function comment
1059 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1061 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1064 // Requery IDE IO resources in case of the switch of native and legacy modes
1066 ReassignIdeResources (IdeBlkIoDevice
);
1069 // For ATA compatible device, use ATA read block's mechanism
1071 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1072 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1073 return AtaBlkIoReadBlocks (
1082 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1083 return EFI_DEVICE_ERROR
;
1087 // for ATAPI device, using ATAPI read block's mechanism
1089 return AtapiBlkIoReadBlocks (
1100 Write data to block io device
1102 @param This Protocol instance pointer.
1103 @param MediaId The media ID of the device
1104 @param LBA Starting LBA address to write data
1105 @param BufferSize The size of data to be written
1106 @param Buffer Caller supplied buffer to save data
1108 @return write data status
1113 IDEBlkIoWriteBlocks (
1114 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1117 IN UINTN BufferSize
,
1120 // TODO: EFI_DEVICE_ERROR - add return value to function comment
1122 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1124 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1126 // Requery IDE IO resources in case of the switch of native and legacy modes
1128 ReassignIdeResources (IdeBlkIoDevice
);
1131 // for ATA device, using ATA write block's mechanism
1133 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1134 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1136 return AtaBlkIoWriteBlocks (
1145 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1146 return EFI_DEVICE_ERROR
;
1150 // for ATAPI device, using ATAPI write block's mechanism
1152 return AtapiBlkIoWriteBlocks (
1162 // ***********************************************************************************
1163 // IDEBlkIoFlushBlocks
1164 // ***********************************************************************************
1167 TODO: This - add argument and description to function comment
1168 TODO: EFI_SUCCESS - add return value to function comment
1172 IDEBlkIoFlushBlocks (
1173 IN EFI_BLOCK_IO_PROTOCOL
*This
1183 Return the results of the Inquiry command to a drive in InquiryData.
1184 Data format of Inquiry data is defined by the Interface GUID.
1186 @param This Protocol instance pointer.
1187 @param InquiryData Results of Inquiry command to device
1188 @param InquiryDataSize Size of InquiryData in bytes.
1190 @retval EFI_SUCCESS InquiryData valid
1191 @retval EFI_NOT_FOUND Device does not support this data class
1192 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
1193 @retval EFI_BUFFER_TOO_SMALL IntquiryDataSize not big enough
1198 IDEDiskInfoInquiry (
1199 IN EFI_DISK_INFO_PROTOCOL
*This
,
1200 IN OUT VOID
*InquiryData
,
1201 IN OUT UINT32
*InquiryDataSize
1204 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1206 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1208 if (*InquiryDataSize
< sizeof (INQUIRY_DATA
)) {
1209 *InquiryDataSize
= sizeof (INQUIRY_DATA
);
1210 return EFI_BUFFER_TOO_SMALL
;
1213 if (IdeBlkIoDevice
->pInquiryData
== NULL
) {
1214 return EFI_NOT_FOUND
;
1217 gBS
->CopyMem (InquiryData
, IdeBlkIoDevice
->pInquiryData
, sizeof (INQUIRY_DATA
));
1218 *InquiryDataSize
= sizeof (INQUIRY_DATA
);
1224 Return the results of the Identify command to a drive in IdentifyData.
1225 Data format of Identify data is defined by the Interface GUID.
1227 @param This Protocol instance pointer.
1228 @param IdentifyData Results of Identify command to device
1229 @param IdentifyDataSize Size of IdentifyData in bytes.
1231 @retval EFI_SUCCESS IdentifyData valid
1232 @retval EFI_NOT_FOUND Device does not support this data class
1233 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
1234 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
1239 IDEDiskInfoIdentify (
1240 IN EFI_DISK_INFO_PROTOCOL
*This
,
1241 IN OUT VOID
*IdentifyData
,
1242 IN OUT UINT32
*IdentifyDataSize
1245 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1247 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1249 if (*IdentifyDataSize
< sizeof (EFI_IDENTIFY_DATA
)) {
1250 *IdentifyDataSize
= sizeof (EFI_IDENTIFY_DATA
);
1251 return EFI_BUFFER_TOO_SMALL
;
1254 if (IdeBlkIoDevice
->pIdData
== NULL
) {
1255 return EFI_NOT_FOUND
;
1258 gBS
->CopyMem (IdentifyData
, IdeBlkIoDevice
->pIdData
, sizeof (EFI_IDENTIFY_DATA
));
1259 *IdentifyDataSize
= sizeof (EFI_IDENTIFY_DATA
);
1265 Return the results of the Request Sense command to a drive in SenseData.
1266 Data format of Sense data is defined by the Interface GUID.
1268 @param This Protocol instance pointer.
1269 @param SenseData Results of Request Sense command to device
1270 @param SenseDataSize Size of SenseData in bytes.
1271 @param SenseDataNumber Type of SenseData
1273 @retval EFI_SUCCESS InquiryData valid
1274 @retval EFI_NOT_FOUND Device does not support this data class
1275 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
1276 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough
1281 IDEDiskInfoSenseData (
1282 IN EFI_DISK_INFO_PROTOCOL
*This
,
1283 IN OUT VOID
*SenseData
,
1284 IN OUT UINT32
*SenseDataSize
,
1285 OUT UINT8
*SenseDataNumber
1288 return EFI_NOT_FOUND
;
1292 Return the results of the Request Sense command to a drive in SenseData.
1293 Data format of Sense data is defined by the Interface GUID.
1295 @param This Protocol instance pointer.
1296 @param IdeChannel Primary or Secondary
1297 @param IdeDevice Master or Slave
1299 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid
1300 @retval EFI_UNSUPPORTED This is not an IDE device
1305 IDEDiskInfoWhichIde (
1306 IN EFI_DISK_INFO_PROTOCOL
*This
,
1307 OUT UINT32
*IdeChannel
,
1308 OUT UINT32
*IdeDevice
1311 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1313 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1314 *IdeChannel
= IdeBlkIoDevice
->Channel
;
1315 *IdeDevice
= IdeBlkIoDevice
->Device
;