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_WITH_DEVICE_PATH (
500 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_PRESENCE_DETECT
),
501 IdeBlkIoDevicePtr
->DevicePath
505 // Discover device, now!
507 PERF_START (0, "DiscoverIdeDevice", "IDE", 0);
508 Status
= DiscoverIdeDevice (IdeBlkIoDevicePtr
);
509 PERF_END (0, "DiscoverIdeDevice", "IDE", 0);
511 IdeBusDriverPrivateData
->HaveScannedDevice
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
512 IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
514 if (!EFI_ERROR (Status
)) {
518 ZeroMem (&NewNode
, sizeof (NewNode
));
519 NewNode
.DevPath
.Type
= MESSAGING_DEVICE_PATH
;
520 NewNode
.DevPath
.SubType
= MSG_ATAPI_DP
;
521 SetDevicePathNodeLength (&NewNode
.DevPath
, sizeof (ATAPI_DEVICE_PATH
));
523 NewNode
.Atapi
.PrimarySecondary
= (UINT8
) IdeBlkIoDevicePtr
->Channel
;
524 NewNode
.Atapi
.SlaveMaster
= (UINT8
) IdeBlkIoDevicePtr
->Device
;
525 NewNode
.Atapi
.Lun
= IdeBlkIoDevicePtr
->Lun
;
526 IdeBlkIoDevicePtr
->DevicePath
= AppendDevicePathNode (
530 if (IdeBlkIoDevicePtr
->DevicePath
== NULL
) {
531 ReleaseIdeResources (IdeBlkIoDevicePtr
);
536 // Submit identify data to IDE controller init driver
538 CopyMem (&IdentifyData
, IdeBlkIoDevicePtr
->pIdData
, sizeof (IdentifyData
));
539 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
540 IdeInit
->SubmitData (IdeInit
, IdeChannel
, IdeDevice
, &IdentifyData
);
543 // Device detection failed
545 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
546 IdeInit
->SubmitData (IdeInit
, IdeChannel
, IdeDevice
, NULL
);
547 ReleaseIdeResources (IdeBlkIoDevicePtr
);
548 IdeBlkIoDevicePtr
= NULL
;
551 // end of 1st inner loop ---
555 // end of 1st outer loop =========
560 // = 2nd outer loop == Primary/Secondary =================
562 for (IdeChannel
= BeginningIdeChannel
; IdeChannel
<= EndIdeChannel
; IdeChannel
++) {
565 // -- 2nd inner loop --- Master/Slave --------
567 for (IdeDevice
= BeginningIdeDevice
; IdeDevice
<= EndIdeDevice
; IdeDevice
++) {
569 if (IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
]) {
573 if (!IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
]) {
577 Status
= IdeInit
->CalculateMode (
583 if (EFI_ERROR (Status
)) {
584 DEBUG ((EFI_D_ERROR
, "[bStStp20S=%x]", Status
));
588 IdeBlkIoDevicePtr
= IdeBlkIoDevice
[IdeChannel
][IdeDevice
];
591 // Set best supported PIO mode on this IDE device
593 if (SupportedModes
->PioMode
.Mode
<= ATA_PIO_MODE_2
) {
594 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_DEFAULT_PIO
;
596 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_FLOW_PIO
;
599 TransferMode
.ModeNumber
= (UINT8
) (SupportedModes
->PioMode
.Mode
);
601 if (SupportedModes
->ExtModeCount
== 0){
602 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
604 if (EFI_ERROR (Status
)) {
605 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
606 ReleaseIdeResources (IdeBlkIoDevicePtr
);
607 IdeBlkIoDevicePtr
= NULL
;
613 // Set supported DMA mode on this IDE device. Note that UDMA & MDMA cann't
614 // be set together. Only one DMA mode can be set to a device. If setting
615 // DMA mode operation fails, we can continue moving on because we only use
616 // PIO mode at boot time. DMA modes are used by certain kind of OS booting
618 if (SupportedModes
->UdmaMode
.Valid
) {
620 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_UDMA
;
621 TransferMode
.ModeNumber
= (UINT8
) (SupportedModes
->UdmaMode
.Mode
);
622 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
624 if (EFI_ERROR (Status
)) {
625 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
626 ReleaseIdeResources (IdeBlkIoDevicePtr
);
627 IdeBlkIoDevicePtr
= NULL
;
631 EnableInterrupt (IdeBlkIoDevicePtr
);
632 } else if (SupportedModes
->MultiWordDmaMode
.Valid
) {
634 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_MDMA
;
635 TransferMode
.ModeNumber
= (UINT8
) SupportedModes
->MultiWordDmaMode
.Mode
;
636 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
638 if (EFI_ERROR (Status
)) {
639 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
640 ReleaseIdeResources (IdeBlkIoDevicePtr
);
641 IdeBlkIoDevicePtr
= NULL
;
645 EnableInterrupt (IdeBlkIoDevicePtr
);
648 // Init driver parameters
650 DriveParameters
.Sector
= (UINT8
) IdeBlkIoDevicePtr
->pIdData
->AtaData
.sectors_per_track
;
651 DriveParameters
.Heads
= (UINT8
) (IdeBlkIoDevicePtr
->pIdData
->AtaData
.heads
- 1);
652 DriveParameters
.MultipleSector
= (UINT8
) IdeBlkIoDevicePtr
->pIdData
->AtaData
.multi_sector_cmd_max_sct_cnt
;
654 // Set Parameters for the device:
656 // 2) Establish the block count for READ/WRITE MULTIPLE (EXT) command
658 if ((IdeBlkIoDevicePtr
->Type
== IdeHardDisk
) || (IdeBlkIoDevicePtr
->Type
== Ide48bitAddressingHardDisk
)) {
659 Status
= SetDriveParameters (IdeBlkIoDevicePtr
, &DriveParameters
);
663 // Record PIO mode used in private data
665 IdeBlkIoDevicePtr
->PioMode
= SupportedModes
->PioMode
.Mode
;
668 // Set IDE controller Timing Blocks in the PCI Configuration Space
670 IdeInit
->SetTiming (IdeInit
, IdeChannel
, IdeDevice
, SupportedModes
);
673 // Add Component Name for the IDE/ATAPI device that was discovered.
675 IdeBlkIoDevicePtr
->ControllerNameTable
= NULL
;
676 ADD_NAME (IdeBlkIoDevicePtr
);
678 Status
= gBS
->InstallMultipleProtocolInterfaces (
679 &IdeBlkIoDevicePtr
->Handle
,
680 &gEfiDevicePathProtocolGuid
,
681 IdeBlkIoDevicePtr
->DevicePath
,
682 &gEfiBlockIoProtocolGuid
,
683 &IdeBlkIoDevicePtr
->BlkIo
,
684 &gEfiDiskInfoProtocolGuid
,
685 &IdeBlkIoDevicePtr
->DiskInfo
,
689 if (EFI_ERROR (Status
)) {
690 ReleaseIdeResources (IdeBlkIoDevicePtr
);
695 &gEfiPciIoProtocolGuid
,
697 This
->DriverBindingHandle
,
698 IdeBlkIoDevicePtr
->Handle
,
699 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
702 IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
705 // Report status code: device eanbled!
707 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
709 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_ENABLE
),
710 IdeBlkIoDevicePtr
->DevicePath
714 // Create event to clear pending IDE interrupt
716 Status
= gBS
->CreateEvent (
717 EFI_EVENT_SIGNAL_EXIT_BOOT_SERVICES
,
721 &IdeBlkIoDevicePtr
->ExitBootServiceEvent
725 // end of 2nd inner loop ----
729 // end of 2nd outer loop ==========
734 // All configurations done! Notify IdeController to do post initialization
735 // work such as saving IDE controller PCI settings for S3 resume
737 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBusPhaseMaximum
, 0);
739 if (SupportedModes
!= NULL
) {
740 gBS
->FreePool (SupportedModes
);
743 PERF_START (0, "Finish IDE detection", "IDE", 1);
744 PERF_END (0, "Finish IDE detection", "IDE", 0);
751 // Report error code: controller error
753 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
754 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
755 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_EC_CONTROLLER_ERROR
),
761 &gEfiIdeControllerInitProtocolGuid
,
762 This
->DriverBindingHandle
,
766 gBS
->UninstallMultipleProtocolInterfaces (
769 IdeBusDriverPrivateData
,
773 if (IdeBusDriverPrivateData
!= NULL
) {
774 gBS
->FreePool (IdeBusDriverPrivateData
);
777 if (SupportedModes
!= NULL
) {
778 gBS
->FreePool (SupportedModes
);
783 &gEfiPciIoProtocolGuid
,
784 This
->DriverBindingHandle
,
790 &gEfiDevicePathProtocolGuid
,
791 This
->DriverBindingHandle
,
800 // ***********************************************************************************
801 // IDEBusDriverBindingStop
802 // ***********************************************************************************
805 Stop this driver on Controller Handle.
807 @param This Protocol instance pointer.
808 @param DeviceHandle Handle of device to stop driver on
809 @param NumberOfChildren Not used
810 @param ChildHandleBuffer Not used
812 @retval EFI_SUCCESS This driver is removed DeviceHandle
813 @retval other This driver was not removed from this device
818 IDEBusDriverBindingStop (
819 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
820 IN EFI_HANDLE Controller
,
821 IN UINTN NumberOfChildren
,
822 IN EFI_HANDLE
*ChildHandleBuffer
824 // TODO: Controller - add argument and description to function comment
825 // TODO: EFI_DEVICE_ERROR - add return value to function comment
828 EFI_PCI_IO_PROTOCOL
*PciIo
;
829 BOOLEAN AllChildrenStopped
;
831 IDE_BUS_DRIVER_PRIVATE_DATA
*IdeBusDriverPrivateData
;
833 IdeBusDriverPrivateData
= NULL
;
835 if (NumberOfChildren
== 0) {
837 Status
= gBS
->OpenProtocol (
839 &gEfiPciIoProtocolGuid
,
841 This
->DriverBindingHandle
,
843 EFI_OPEN_PROTOCOL_GET_PROTOCOL
845 if (!EFI_ERROR (Status
)) {
848 EfiPciIoAttributeOperationDisable
,
849 EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO
| EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO
| EFI_PCI_DEVICE_ENABLE
,
857 (VOID
**) &IdeBusDriverPrivateData
,
858 This
->DriverBindingHandle
,
860 EFI_OPEN_PROTOCOL_GET_PROTOCOL
863 gBS
->UninstallMultipleProtocolInterfaces (
866 IdeBusDriverPrivateData
,
870 if (IdeBusDriverPrivateData
!= NULL
) {
871 gBS
->FreePool (IdeBusDriverPrivateData
);
874 // Close the bus driver
878 &gEfiIdeControllerInitProtocolGuid
,
879 This
->DriverBindingHandle
,
884 &gEfiPciIoProtocolGuid
,
885 This
->DriverBindingHandle
,
890 &gEfiDevicePathProtocolGuid
,
891 This
->DriverBindingHandle
,
898 AllChildrenStopped
= TRUE
;
900 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
902 Status
= DeRegisterIdeDevice (This
, Controller
, ChildHandleBuffer
[Index
]);
904 if (EFI_ERROR (Status
)) {
905 AllChildrenStopped
= FALSE
;
909 if (!AllChildrenStopped
) {
910 return EFI_DEVICE_ERROR
;
917 // ***********************************************************************************
918 // DeRegisterIdeDevice
919 // ***********************************************************************************
922 Deregister an IDE device and free resources
924 @param This Protocol instance pointer.
925 @param Controller Ide device handle
926 @param Handle Handle of device to deregister driver on
932 DeRegisterIdeDevice (
933 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
934 IN EFI_HANDLE Controller
,
937 // TODO: EFI_SUCCESS - add return value to function comment
940 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
941 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
942 EFI_PCI_IO_PROTOCOL
*PciIo
;
945 Status
= gBS
->OpenProtocol (
947 &gEfiBlockIoProtocolGuid
,
949 This
->DriverBindingHandle
,
951 EFI_OPEN_PROTOCOL_GET_PROTOCOL
953 if (EFI_ERROR (Status
)) {
957 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (BlkIo
);
960 // Report Status code: Device disabled
962 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
964 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_DISABLE
),
965 IdeBlkIoDevice
->DevicePath
969 // Close the child handle
971 Status
= gBS
->CloseProtocol (
973 &gEfiPciIoProtocolGuid
,
974 This
->DriverBindingHandle
,
978 Status
= gBS
->UninstallMultipleProtocolInterfaces (
980 &gEfiDevicePathProtocolGuid
,
981 IdeBlkIoDevice
->DevicePath
,
982 &gEfiBlockIoProtocolGuid
,
983 &IdeBlkIoDevice
->BlkIo
,
984 &gEfiDiskInfoProtocolGuid
,
985 &IdeBlkIoDevice
->DiskInfo
,
989 if (EFI_ERROR (Status
)) {
992 &gEfiPciIoProtocolGuid
,
994 This
->DriverBindingHandle
,
996 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1002 // Release allocated resources
1004 Index
= IdeBlkIoDevice
->Channel
* 2 + IdeBlkIoDevice
->Device
;
1005 IdeBlkIoDevice
->IdeBusDriverPrivateData
->HaveScannedDevice
[Index
] = FALSE
;
1007 ReleaseIdeResources (IdeBlkIoDevice
);
1013 // ***********************************************************************************
1015 // ***********************************************************************************
1018 TODO: This - add argument and description to function comment
1019 TODO: ExtendedVerification - add argument and description to function comment
1020 TODO: EFI_DEVICE_ERROR - add return value to function comment
1026 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1027 IN BOOLEAN ExtendedVerification
1030 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1033 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1035 // Requery IDE IO resources in case of the switch of native and legacy modes
1037 ReassignIdeResources (IdeBlkIoDevice
);
1040 // for ATA device, using ATA reset method
1042 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1043 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1044 return AtaSoftReset (IdeBlkIoDevice
);
1047 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1048 return EFI_DEVICE_ERROR
;
1052 // for ATAPI device, using ATAPI reset method
1054 Status
= AtapiSoftReset (IdeBlkIoDevice
);
1055 if (ExtendedVerification
) {
1056 Status
= AtaSoftReset (IdeBlkIoDevice
);
1063 Read data from block io device
1065 @param This Protocol instance pointer.
1066 @param MediaId The media ID of the device
1067 @param LBA Starting LBA address to read data
1068 @param BufferSize The size of data to be read
1069 @param Buffer Caller supplied buffer to save data
1071 @return read data status
1076 IDEBlkIoReadBlocks (
1077 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1080 IN UINTN BufferSize
,
1083 // TODO: EFI_DEVICE_ERROR - add return value to function comment
1085 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1087 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1090 // Requery IDE IO resources in case of the switch of native and legacy modes
1092 ReassignIdeResources (IdeBlkIoDevice
);
1095 // For ATA compatible device, use ATA read block's mechanism
1097 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1098 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1099 return AtaBlkIoReadBlocks (
1108 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1109 return EFI_DEVICE_ERROR
;
1113 // for ATAPI device, using ATAPI read block's mechanism
1115 return AtapiBlkIoReadBlocks (
1126 Write data to block io device
1128 @param This Protocol instance pointer.
1129 @param MediaId The media ID of the device
1130 @param LBA Starting LBA address to write data
1131 @param BufferSize The size of data to be written
1132 @param Buffer Caller supplied buffer to save data
1134 @return write data status
1139 IDEBlkIoWriteBlocks (
1140 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1143 IN UINTN BufferSize
,
1146 // TODO: EFI_DEVICE_ERROR - add return value to function comment
1148 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1150 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1152 // Requery IDE IO resources in case of the switch of native and legacy modes
1154 ReassignIdeResources (IdeBlkIoDevice
);
1157 // for ATA device, using ATA write block's mechanism
1159 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1160 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1162 return AtaBlkIoWriteBlocks (
1171 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1172 return EFI_DEVICE_ERROR
;
1176 // for ATAPI device, using ATAPI write block's mechanism
1178 return AtapiBlkIoWriteBlocks (
1188 // ***********************************************************************************
1189 // IDEBlkIoFlushBlocks
1190 // ***********************************************************************************
1193 TODO: This - add argument and description to function comment
1194 TODO: EFI_SUCCESS - add return value to function comment
1198 IDEBlkIoFlushBlocks (
1199 IN EFI_BLOCK_IO_PROTOCOL
*This
1209 Return the results of the Inquiry command to a drive in InquiryData.
1210 Data format of Inquiry data is defined by the Interface GUID.
1212 @param This Protocol instance pointer.
1213 @param InquiryData Results of Inquiry command to device
1214 @param InquiryDataSize Size of InquiryData in bytes.
1216 @retval EFI_SUCCESS InquiryData valid
1217 @retval EFI_NOT_FOUND Device does not support this data class
1218 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
1219 @retval EFI_BUFFER_TOO_SMALL IntquiryDataSize not big enough
1224 IDEDiskInfoInquiry (
1225 IN EFI_DISK_INFO_PROTOCOL
*This
,
1226 IN OUT VOID
*InquiryData
,
1227 IN OUT UINT32
*InquiryDataSize
1230 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1232 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1234 if (*InquiryDataSize
< sizeof (INQUIRY_DATA
)) {
1235 *InquiryDataSize
= sizeof (INQUIRY_DATA
);
1236 return EFI_BUFFER_TOO_SMALL
;
1239 if (IdeBlkIoDevice
->pInquiryData
== NULL
) {
1240 return EFI_NOT_FOUND
;
1243 gBS
->CopyMem (InquiryData
, IdeBlkIoDevice
->pInquiryData
, sizeof (INQUIRY_DATA
));
1244 *InquiryDataSize
= sizeof (INQUIRY_DATA
);
1250 Return the results of the Identify command to a drive in IdentifyData.
1251 Data format of Identify data is defined by the Interface GUID.
1253 @param This Protocol instance pointer.
1254 @param IdentifyData Results of Identify command to device
1255 @param IdentifyDataSize Size of IdentifyData in bytes.
1257 @retval EFI_SUCCESS IdentifyData valid
1258 @retval EFI_NOT_FOUND Device does not support this data class
1259 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
1260 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
1265 IDEDiskInfoIdentify (
1266 IN EFI_DISK_INFO_PROTOCOL
*This
,
1267 IN OUT VOID
*IdentifyData
,
1268 IN OUT UINT32
*IdentifyDataSize
1271 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1273 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1275 if (*IdentifyDataSize
< sizeof (EFI_IDENTIFY_DATA
)) {
1276 *IdentifyDataSize
= sizeof (EFI_IDENTIFY_DATA
);
1277 return EFI_BUFFER_TOO_SMALL
;
1280 if (IdeBlkIoDevice
->pIdData
== NULL
) {
1281 return EFI_NOT_FOUND
;
1284 gBS
->CopyMem (IdentifyData
, IdeBlkIoDevice
->pIdData
, sizeof (EFI_IDENTIFY_DATA
));
1285 *IdentifyDataSize
= sizeof (EFI_IDENTIFY_DATA
);
1291 Return the results of the Request Sense command to a drive in SenseData.
1292 Data format of Sense data is defined by the Interface GUID.
1294 @param This Protocol instance pointer.
1295 @param SenseData Results of Request Sense command to device
1296 @param SenseDataSize Size of SenseData in bytes.
1297 @param SenseDataNumber Type of SenseData
1299 @retval EFI_SUCCESS InquiryData valid
1300 @retval EFI_NOT_FOUND Device does not support this data class
1301 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
1302 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough
1307 IDEDiskInfoSenseData (
1308 IN EFI_DISK_INFO_PROTOCOL
*This
,
1309 IN OUT VOID
*SenseData
,
1310 IN OUT UINT32
*SenseDataSize
,
1311 OUT UINT8
*SenseDataNumber
1314 return EFI_NOT_FOUND
;
1318 Return the results of the Request Sense command to a drive in SenseData.
1319 Data format of Sense data is defined by the Interface GUID.
1321 @param This Protocol instance pointer.
1322 @param IdeChannel Primary or Secondary
1323 @param IdeDevice Master or Slave
1325 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid
1326 @retval EFI_UNSUPPORTED This is not an IDE device
1331 IDEDiskInfoWhichIde (
1332 IN EFI_DISK_INFO_PROTOCOL
*This
,
1333 OUT UINT32
*IdeChannel
,
1334 OUT UINT32
*IdeDevice
1337 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1339 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1340 *IdeChannel
= IdeBlkIoDevice
->Channel
;
1341 *IdeDevice
= IdeBlkIoDevice
->Device
;