2 This file implement UEFI driver for IDE Bus which includes device identification,
3 Child device(Disk, CDROM, etc) enumeration and child handler installation, and
6 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 @par Revision Reference:
16 This module is modified from DXE\IDE module for Ide Contriller Init support
22 #define PCI_CLASS_MASS_STORAGE 0x01
23 #define PCI_SUB_CLASS_IDE 0x01
27 // IDE Bus Driver Binding Protocol Instance
29 EFI_DRIVER_BINDING_PROTOCOL gIDEBusDriverBinding
= {
30 IDEBusDriverBindingSupported
,
31 IDEBusDriverBindingStart
,
32 IDEBusDriverBindingStop
,
38 Deregister an IDE device and free resources
40 @param This Protocol instance pointer.
41 @param Controller Ide device handle
42 @param Handle Handle of device to deregister driver on
44 @retval EFI_SUCCESS Deregiter a specific IDE device successfully
50 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
51 IN EFI_HANDLE Controller
,
56 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
57 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
58 EFI_PCI_IO_PROTOCOL
*PciIo
;
61 Status
= gBS
->OpenProtocol (
63 &gEfiBlockIoProtocolGuid
,
65 This
->DriverBindingHandle
,
67 EFI_OPEN_PROTOCOL_GET_PROTOCOL
69 if (EFI_ERROR (Status
)) {
73 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (BlkIo
);
76 // Report Status code: Device disabled
78 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
80 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_DISABLE
),
81 IdeBlkIoDevice
->DevicePath
85 // Close the child handle
87 Status
= gBS
->CloseProtocol (
89 &gEfiPciIoProtocolGuid
,
90 This
->DriverBindingHandle
,
94 Status
= gBS
->UninstallMultipleProtocolInterfaces (
96 &gEfiDevicePathProtocolGuid
,
97 IdeBlkIoDevice
->DevicePath
,
98 &gEfiBlockIoProtocolGuid
,
99 &IdeBlkIoDevice
->BlkIo
,
100 &gEfiDiskInfoProtocolGuid
,
101 &IdeBlkIoDevice
->DiskInfo
,
105 if (EFI_ERROR (Status
)) {
108 &gEfiPciIoProtocolGuid
,
110 This
->DriverBindingHandle
,
112 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
118 // Release allocated resources
120 Index
= IdeBlkIoDevice
->Channel
* 2 + IdeBlkIoDevice
->Device
;
121 if (Index
< MAX_IDE_DEVICE
) {
122 IdeBlkIoDevice
->IdeBusDriverPrivateData
->HaveScannedDevice
[Index
] = FALSE
;
124 ReleaseIdeResources (IdeBlkIoDevice
);
129 Supported function of Driver Binding protocol for this driver.
131 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
132 @param ControllerHandle The handle of the controller to test.
133 @param RemainingDevicePath A pointer to the remaining portion of a device path.
135 @retval EFI_SUCCESS Driver loaded.
136 @retval other Driver not loaded.
141 IDEBusDriverBindingSupported (
142 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
143 IN EFI_HANDLE Controller
,
144 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
148 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
150 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeInit
;
151 EFI_PCI_IO_PROTOCOL
*PciIo
;
154 if (RemainingDevicePath
!= NULL
) {
155 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
157 // Check if RemainingDevicePath is the End of Device Path Node,
158 // if yes, go on checking other conditions
160 if (!IsDevicePathEnd (Node
)) {
162 // If RemainingDevicePath isn't the End of Device Path Node,
163 // check its validation
165 if (Node
->DevPath
.Type
!= MESSAGING_DEVICE_PATH
||
166 Node
->DevPath
.SubType
!= MSG_ATAPI_DP
||
167 DevicePathNodeLength(&Node
->DevPath
) != sizeof(ATAPI_DEVICE_PATH
)) {
168 return EFI_UNSUPPORTED
;
174 // Verify the Ide Controller Init Protocol, which installed by the
175 // IdeController module.
177 Status
= gBS
->OpenProtocol (
179 &gEfiIdeControllerInitProtocolGuid
,
181 This
->DriverBindingHandle
,
183 EFI_OPEN_PROTOCOL_BY_DRIVER
186 if (Status
== EFI_ALREADY_STARTED
) {
190 if (EFI_ERROR (Status
)) {
195 // Close the I/O Abstraction(s) used to perform the supported test
199 &gEfiIdeControllerInitProtocolGuid
,
200 This
->DriverBindingHandle
,
205 // Open the EFI Device Path protocol needed to perform the supported test
207 Status
= gBS
->OpenProtocol (
209 &gEfiDevicePathProtocolGuid
,
210 (VOID
**) &ParentDevicePath
,
211 This
->DriverBindingHandle
,
213 EFI_OPEN_PROTOCOL_BY_DRIVER
215 if (Status
== EFI_ALREADY_STARTED
) {
220 // Close protocol, don't use device path protocol in the Support() function
224 &gEfiDevicePathProtocolGuid
,
225 This
->DriverBindingHandle
,
230 // Get the EfiPciIoProtocol
232 Status
= gBS
->OpenProtocol (
234 &gEfiPciIoProtocolGuid
,
236 This
->DriverBindingHandle
,
238 EFI_OPEN_PROTOCOL_GET_PROTOCOL
241 if (EFI_ERROR (Status
)) {
246 // Now further check the PCI header: Base class (offset 0x0B) and
247 // Sub Class (offset 0x0A). This controller should be an IDE controller
249 Status
= PciIo
->Pci
.Read (
257 if (!EFI_ERROR (Status
)) {
259 // Examine if it is IDE mode by class code
261 if ((PciData
.Hdr
.ClassCode
[2] != PCI_CLASS_MASS_STORAGE
) || (PciData
.Hdr
.ClassCode
[1] != PCI_SUB_CLASS_IDE
)) {
262 Status
= EFI_UNSUPPORTED
;
264 Status
= EFI_SUCCESS
;
273 Start function of Driver binding protocol which start this driver on Controller
274 by detecting all disks and installing BlockIo protocol on them.
276 @param This Protocol instance pointer.
277 @param Controller Handle of device to bind driver to.
278 @param RemainingDevicePath produce all possible children.
280 @retval EFI_SUCCESS This driver is added to ControllerHandle.
281 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
282 @retval other This driver does not support this device.
287 IDEBusDriverBindingStart (
288 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
289 IN EFI_HANDLE Controller
,
290 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
294 EFI_STATUS SavedStatus
;
295 EFI_PCI_IO_PROTOCOL
*PciIo
;
296 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
299 UINT8 BeginningIdeChannel
;
302 UINT8 BeginningIdeDevice
;
304 IDE_BLK_IO_DEV
*IdeBlkIoDevice
[IdeMaxChannel
][IdeMaxDevice
];
305 IDE_BLK_IO_DEV
*IdeBlkIoDevicePtr
;
306 IDE_REGISTERS_BASE_ADDR IdeRegsBaseAddr
[IdeMaxChannel
];
307 ATA_TRANSFER_MODE TransferMode
;
308 ATA_DRIVE_PARMS DriveParameters
;
309 EFI_DEV_PATH NewNode
;
310 UINT8 ConfigurationOptions
;
311 UINT16 CommandBlockBaseAddr
;
312 UINT16 ControlBlockBaseAddr
;
314 IDE_BUS_DRIVER_PRIVATE_DATA
*IdeBusDriverPrivateData
;
318 // Local variables declaration for IdeControllerInit support
320 EFI_IDE_CONTROLLER_INIT_PROTOCOL
*IdeInit
;
322 BOOLEAN ChannelEnabled
;
324 EFI_IDENTIFY_DATA IdentifyData
;
325 EFI_ATA_COLLECTIVE_MODE
*SupportedModes
;
327 IdeBusDriverPrivateData
= NULL
;
328 SupportedModes
= NULL
;
331 // Perform IdeBus initialization
333 Status
= gBS
->OpenProtocol (
335 &gEfiDevicePathProtocolGuid
,
336 (VOID
**) &ParentDevicePath
,
337 This
->DriverBindingHandle
,
339 EFI_OPEN_PROTOCOL_BY_DRIVER
341 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
346 // Now open the IDE_CONTROLLER_INIT protocol. Step7.1
348 Status
= gBS
->OpenProtocol (
350 &gEfiIdeControllerInitProtocolGuid
,
352 This
->DriverBindingHandle
,
354 EFI_OPEN_PROTOCOL_BY_DRIVER
358 // The following OpenProtocol function with _GET_PROTOCOL attribute and
359 // will not return EFI_ALREADY_STARTED, so save it for now
361 SavedStatus
= Status
;
363 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
364 DEBUG ((EFI_D_ERROR
, "Open Init, Status=%x", Status
));
366 // open protocol is not SUCCESS or not ALREADY_STARTED, error exit
372 // Save Enumall. Step7.2
374 EnumAll
= IdeInit
->EnumAll
;
377 // Consume PCI I/O protocol. Note that the OpenProtocol with _GET_PROTOCOL
378 // attribute will not return EFI_ALREADY_STARTED
380 Status
= gBS
->OpenProtocol (
382 &gEfiPciIoProtocolGuid
,
384 This
->DriverBindingHandle
,
386 EFI_OPEN_PROTOCOL_GET_PROTOCOL
388 if (EFI_ERROR (Status
)) {
389 DEBUG ((EFI_D_ERROR
, "Open PciIo, Status=%x", Status
));
394 // We must check EFI_ALREADY_STARTED because many ATAPI devices are removable
396 if (SavedStatus
!= EFI_ALREADY_STARTED
) {
397 IdeBusDriverPrivateData
= AllocatePool (sizeof (IDE_BUS_DRIVER_PRIVATE_DATA
));
398 if (IdeBusDriverPrivateData
== NULL
) {
399 Status
= EFI_OUT_OF_RESOURCES
;
403 ZeroMem (IdeBusDriverPrivateData
, sizeof (IDE_BUS_DRIVER_PRIVATE_DATA
));
404 Status
= gBS
->InstallMultipleProtocolInterfaces (
407 IdeBusDriverPrivateData
,
410 if (EFI_ERROR (Status
)) {
415 Status
= gBS
->OpenProtocol (
418 (VOID
**) &IdeBusDriverPrivateData
,
419 This
->DriverBindingHandle
,
421 EFI_OPEN_PROTOCOL_GET_PROTOCOL
423 if (EFI_ERROR (Status
)) {
424 IdeBusDriverPrivateData
= NULL
;
429 Status
= PciIo
->Attributes (
431 EfiPciIoAttributeOperationSupported
,
435 if (!EFI_ERROR (Status
)) {
436 Supports
&= EFI_PCI_DEVICE_ENABLE
;
437 Status
= PciIo
->Attributes (
439 EfiPciIoAttributeOperationEnable
,
445 if (EFI_ERROR (Status
)) {
450 // Read the environment variable that contains the IDEBus Driver's
451 // Config options that were set by the Driver Configuration Protocol
453 DataSize
= sizeof (ConfigurationOptions
);
454 Status
= gRT
->GetVariable (
455 (CHAR16
*) L
"Configuration",
459 &ConfigurationOptions
461 if (EFI_ERROR (Status
)) {
462 ConfigurationOptions
= 0x0f;
465 if (EnumAll
|| RemainingDevicePath
== NULL
) {
467 // If IdeInit->EnumAll is TRUE or RemainingDevicePath is NULL,
468 // must enumerate all IDE devices anyway
470 BeginningIdeChannel
= IdePrimary
;
471 EndIdeChannel
= IdeSecondary
;
472 BeginningIdeDevice
= IdeMaster
;
473 EndIdeDevice
= IdeSlave
;
475 } else if (!IsDevicePathEnd (RemainingDevicePath
)) {
477 // If RemainingDevicePath isn't the End of Device Path Node,
478 // only scan the specified device by RemainingDevicePath
480 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
481 BeginningIdeChannel
= Node
->Atapi
.PrimarySecondary
;
482 EndIdeChannel
= BeginningIdeChannel
;
483 BeginningIdeDevice
= Node
->Atapi
.SlaveMaster
;
484 EndIdeDevice
= BeginningIdeDevice
;
485 if (BeginningIdeChannel
>= IdeMaxChannel
|| EndIdeChannel
>= IdeMaxChannel
) {
486 Status
= EFI_INVALID_PARAMETER
;
489 if (BeginningIdeDevice
>= IdeMaxDevice
|| EndIdeDevice
>= IdeMaxDevice
) {
490 Status
= EFI_INVALID_PARAMETER
;
496 // If RemainingDevicePath is the End of Device Path Node,
497 // skip enumerate any device and return EFI_SUCESSS
499 BeginningIdeChannel
= IdeMaxChannel
;
500 EndIdeChannel
= IdeMaxChannel
- 1;
501 BeginningIdeDevice
= IdeMaxDevice
;
502 EndIdeDevice
= IdeMaxDevice
- 1;
506 // Obtain IDE IO port registers' base addresses
508 Status
= GetIdeRegistersBaseAddr (PciIo
, IdeRegsBaseAddr
);
509 if (EFI_ERROR (Status
)) {
514 // Report status code: begin IdeBus initialization
516 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
518 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_PC_RESET
),
523 // Strictly follow the enumeration based on IDE_CONTROLLER_INIT protocol
525 for (IdeChannel
= BeginningIdeChannel
; IdeChannel
<= EndIdeChannel
; IdeChannel
++) {
527 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBeforeChannelEnumeration
, IdeChannel
);
530 // now obtain channel information fron IdeControllerInit protocol. Step9
532 Status
= IdeInit
->GetChannelInfo (
538 if (EFI_ERROR (Status
)) {
539 DEBUG ((EFI_D_ERROR
, "[GetChannel, Status=%x]", Status
));
543 if (!ChannelEnabled
) {
547 EndIdeDevice
= (UINT8
) MIN ((MaxDevices
- 1), EndIdeDevice
);
548 ASSERT (EndIdeDevice
< IdeMaxDevice
);
550 // Now inform the IDE Controller Init Module. Sept10
552 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBeforeChannelReset
, IdeChannel
);
555 // No reset channel function implemented. Sept11
557 IdeInit
->NotifyPhase (IdeInit
, EfiIdeAfterChannelReset
, IdeChannel
);
562 IdeInit
->NotifyPhase (
564 EfiIdeBusBeforeDevicePresenceDetection
,
569 // Prepare to detect IDE device of this channel
571 InitializeIDEChannelData ();
574 // -- 1st inner loop --- Master/Slave ------------ Step14
576 for (IdeDevice
= BeginningIdeDevice
; IdeDevice
<= EndIdeDevice
; IdeDevice
++) {
578 // Check whether the configuration options allow this device
580 if ((ConfigurationOptions
& (1 << (IdeChannel
* 2 + IdeDevice
))) == 0) {
585 // The device has been scanned in another Start(), No need to scan it again
586 // for perf optimization.
588 if (IdeBusDriverPrivateData
->HaveScannedDevice
[IdeChannel
* 2 + IdeDevice
]) {
593 // create child handle for the detected device.
595 IdeBlkIoDevice
[IdeChannel
][IdeDevice
] = AllocatePool (sizeof (IDE_BLK_IO_DEV
));
596 if (IdeBlkIoDevice
[IdeChannel
][IdeDevice
] == NULL
) {
600 IdeBlkIoDevicePtr
= IdeBlkIoDevice
[IdeChannel
][IdeDevice
];
602 ZeroMem (IdeBlkIoDevicePtr
, sizeof (IDE_BLK_IO_DEV
));
604 IdeBlkIoDevicePtr
->Signature
= IDE_BLK_IO_DEV_SIGNATURE
;
605 IdeBlkIoDevicePtr
->Channel
= (EFI_IDE_CHANNEL
) IdeChannel
;
606 IdeBlkIoDevicePtr
->Device
= (EFI_IDE_DEVICE
) IdeDevice
;
609 // initialize Block IO interface's Media pointer
611 IdeBlkIoDevicePtr
->BlkIo
.Media
= &IdeBlkIoDevicePtr
->BlkMedia
;
614 // Initialize IDE IO port addresses, including Command Block registers
615 // and Control Block registers
617 IdeBlkIoDevicePtr
->IoPort
= AllocatePool (sizeof (IDE_BASE_REGISTERS
));
618 if (IdeBlkIoDevicePtr
->IoPort
== NULL
) {
622 ZeroMem (IdeBlkIoDevicePtr
->IoPort
, sizeof (IDE_BASE_REGISTERS
));
623 CommandBlockBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].CommandBlockBaseAddr
;
624 ControlBlockBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].ControlBlockBaseAddr
;
626 IdeBlkIoDevicePtr
->IoPort
->Data
= CommandBlockBaseAddr
;
627 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Reg1
) = (UINT16
) (CommandBlockBaseAddr
+ 0x01);
628 IdeBlkIoDevicePtr
->IoPort
->SectorCount
= (UINT16
) (CommandBlockBaseAddr
+ 0x02);
629 IdeBlkIoDevicePtr
->IoPort
->SectorNumber
= (UINT16
) (CommandBlockBaseAddr
+ 0x03);
630 IdeBlkIoDevicePtr
->IoPort
->CylinderLsb
= (UINT16
) (CommandBlockBaseAddr
+ 0x04);
631 IdeBlkIoDevicePtr
->IoPort
->CylinderMsb
= (UINT16
) (CommandBlockBaseAddr
+ 0x05);
632 IdeBlkIoDevicePtr
->IoPort
->Head
= (UINT16
) (CommandBlockBaseAddr
+ 0x06);
633 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Reg
) = (UINT16
) (CommandBlockBaseAddr
+ 0x07);
635 (*(UINT16
*) &IdeBlkIoDevicePtr
->IoPort
->Alt
) = ControlBlockBaseAddr
;
636 IdeBlkIoDevicePtr
->IoPort
->DriveAddress
= (UINT16
) (ControlBlockBaseAddr
+ 0x01);
638 IdeBlkIoDevicePtr
->IoPort
->MasterSlave
= (UINT16
) ((IdeDevice
== IdeMaster
) ? 1 : 0);
640 IdeBlkIoDevicePtr
->PciIo
= PciIo
;
641 IdeBlkIoDevicePtr
->IdeBusDriverPrivateData
= IdeBusDriverPrivateData
;
642 IdeBlkIoDevicePtr
->IoPort
->BusMasterBaseAddr
= IdeRegsBaseAddr
[IdeChannel
].BusMasterBaseAddr
;
645 // Report Status code: is about to detect IDE drive
647 REPORT_STATUS_CODE_EX (
649 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_PRESENCE_DETECT
),
658 // Discover device, now!
660 PERF_START (NULL
, "DiscoverIdeDevice", "IDE", 0);
661 Status
= DiscoverIdeDevice (IdeBlkIoDevicePtr
);
662 PERF_END (NULL
, "DiscoverIdeDevice", "IDE", 0);
664 IdeBusDriverPrivateData
->HaveScannedDevice
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
665 IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
667 if (!EFI_ERROR (Status
)) {
671 ZeroMem (&NewNode
, sizeof (NewNode
));
672 NewNode
.DevPath
.Type
= MESSAGING_DEVICE_PATH
;
673 NewNode
.DevPath
.SubType
= MSG_ATAPI_DP
;
674 SetDevicePathNodeLength (&NewNode
.DevPath
, sizeof (ATAPI_DEVICE_PATH
));
676 NewNode
.Atapi
.PrimarySecondary
= (UINT8
) IdeBlkIoDevicePtr
->Channel
;
677 NewNode
.Atapi
.SlaveMaster
= (UINT8
) IdeBlkIoDevicePtr
->Device
;
678 NewNode
.Atapi
.Lun
= IdeBlkIoDevicePtr
->Lun
;
679 IdeBlkIoDevicePtr
->DevicePath
= AppendDevicePathNode (
683 if (IdeBlkIoDevicePtr
->DevicePath
== NULL
) {
684 ReleaseIdeResources (IdeBlkIoDevicePtr
);
689 // Submit identify data to IDE controller init driver
691 CopyMem (&IdentifyData
, IdeBlkIoDevicePtr
->IdData
, sizeof (IdentifyData
));
692 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
693 IdeInit
->SubmitData (IdeInit
, IdeChannel
, IdeDevice
, &IdentifyData
);
696 // Device detection failed
698 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
699 IdeInit
->SubmitData (IdeInit
, IdeChannel
, IdeDevice
, NULL
);
700 ReleaseIdeResources (IdeBlkIoDevicePtr
);
701 IdeBlkIoDevicePtr
= NULL
;
704 // end of 1st inner loop ---
708 // end of 1st outer loop =========
713 // = 2nd outer loop == Primary/Secondary =================
715 for (IdeChannel
= BeginningIdeChannel
; IdeChannel
<= EndIdeChannel
; IdeChannel
++) {
718 // -- 2nd inner loop --- Master/Slave --------
720 for (IdeDevice
= BeginningIdeDevice
; IdeDevice
<= EndIdeDevice
; IdeDevice
++) {
722 if (IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
]) {
726 if (!IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
]) {
730 Status
= IdeInit
->CalculateMode (
736 if (EFI_ERROR (Status
)) {
737 DEBUG ((EFI_D_ERROR
, "[bStStp20S=%x]", Status
));
741 IdeBlkIoDevicePtr
= IdeBlkIoDevice
[IdeChannel
][IdeDevice
];
744 // Set best supported PIO mode on this IDE device
746 if (SupportedModes
->PioMode
.Mode
<= AtaPioMode2
) {
747 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_DEFAULT_PIO
;
749 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_FLOW_PIO
;
752 TransferMode
.ModeNumber
= (UINT8
) (SupportedModes
->PioMode
.Mode
);
754 if (SupportedModes
->ExtModeCount
== 0){
755 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
757 if (EFI_ERROR (Status
)) {
758 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
759 ReleaseIdeResources (IdeBlkIoDevicePtr
);
760 IdeBlkIoDevicePtr
= NULL
;
766 // Set supported DMA mode on this IDE device. Note that UDMA & MDMA cann't
767 // be set together. Only one DMA mode can be set to a device. If setting
768 // DMA mode operation fails, we can continue moving on because we only use
769 // PIO mode at boot time. DMA modes are used by certain kind of OS booting
771 if (SupportedModes
->UdmaMode
.Valid
) {
773 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_UDMA
;
774 TransferMode
.ModeNumber
= (UINT8
) (SupportedModes
->UdmaMode
.Mode
);
775 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
777 if (EFI_ERROR (Status
)) {
778 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
779 ReleaseIdeResources (IdeBlkIoDevicePtr
);
780 IdeBlkIoDevicePtr
= NULL
;
786 IdeBlkIoDevicePtr
->UdmaMode
.Valid
= TRUE
;
787 IdeBlkIoDevicePtr
->UdmaMode
.Mode
= SupportedModes
->UdmaMode
.Mode
;
788 EnableInterrupt (IdeBlkIoDevicePtr
);
789 } else if (SupportedModes
->MultiWordDmaMode
.Valid
) {
791 TransferMode
.ModeCategory
= ATA_MODE_CATEGORY_MDMA
;
792 TransferMode
.ModeNumber
= (UINT8
) SupportedModes
->MultiWordDmaMode
.Mode
;
793 Status
= SetDeviceTransferMode (IdeBlkIoDevicePtr
, &TransferMode
);
795 if (EFI_ERROR (Status
)) {
796 IdeBusDriverPrivateData
->DeviceFound
[IdeChannel
* 2 + IdeDevice
] = FALSE
;
797 ReleaseIdeResources (IdeBlkIoDevicePtr
);
798 IdeBlkIoDevicePtr
= NULL
;
802 EnableInterrupt (IdeBlkIoDevicePtr
);
805 // Init driver parameters
807 DriveParameters
.Sector
= (UINT8
) ((ATA5_IDENTIFY_DATA
*) IdeBlkIoDevicePtr
->IdData
)->sectors_per_track
;
808 DriveParameters
.Heads
= (UINT8
) (((ATA5_IDENTIFY_DATA
*) IdeBlkIoDevicePtr
->IdData
)->heads
- 1);
809 DriveParameters
.MultipleSector
= (UINT8
) IdeBlkIoDevicePtr
->IdData
->AtaData
.multi_sector_cmd_max_sct_cnt
;
811 // Set Parameters for the device:
813 // 2) Establish the block count for READ/WRITE MULTIPLE (EXT) command
815 if ((IdeBlkIoDevicePtr
->Type
== IdeHardDisk
) || (IdeBlkIoDevicePtr
->Type
== Ide48bitAddressingHardDisk
)) {
816 Status
= SetDriveParameters (IdeBlkIoDevicePtr
, &DriveParameters
);
820 // Record PIO mode used in private data
822 IdeBlkIoDevicePtr
->PioMode
= (ATA_PIO_MODE
) SupportedModes
->PioMode
.Mode
;
825 // Set IDE controller Timing Blocks in the PCI Configuration Space
827 IdeInit
->SetTiming (IdeInit
, IdeChannel
, IdeDevice
, SupportedModes
);
830 // Add Component Name for the IDE/ATAPI device that was discovered.
832 IdeBlkIoDevicePtr
->ControllerNameTable
= NULL
;
833 ADD_IDE_ATAPI_NAME (IdeBlkIoDevicePtr
);
835 Status
= gBS
->InstallMultipleProtocolInterfaces (
836 &IdeBlkIoDevicePtr
->Handle
,
837 &gEfiDevicePathProtocolGuid
,
838 IdeBlkIoDevicePtr
->DevicePath
,
839 &gEfiBlockIoProtocolGuid
,
840 &IdeBlkIoDevicePtr
->BlkIo
,
841 &gEfiDiskInfoProtocolGuid
,
842 &IdeBlkIoDevicePtr
->DiskInfo
,
846 if (EFI_ERROR (Status
)) {
847 ReleaseIdeResources (IdeBlkIoDevicePtr
);
852 &gEfiPciIoProtocolGuid
,
854 This
->DriverBindingHandle
,
855 IdeBlkIoDevicePtr
->Handle
,
856 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
859 IdeBusDriverPrivateData
->DeviceProcessed
[IdeChannel
* 2 + IdeDevice
] = TRUE
;
862 // Report status code: device eanbled!
864 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
866 (EFI_IO_BUS_ATA_ATAPI
| EFI_P_PC_ENABLE
),
867 IdeBlkIoDevicePtr
->DevicePath
871 // Create event to clear pending IDE interrupt
873 Status
= gBS
->CreateEventEx (
878 &gEfiEventExitBootServicesGuid
,
879 &IdeBlkIoDevicePtr
->ExitBootServiceEvent
883 // end of 2nd inner loop ----
887 // end of 2nd outer loop ==========
892 // All configurations done! Notify IdeController to do post initialization
893 // work such as saving IDE controller PCI settings for S3 resume
895 IdeInit
->NotifyPhase (IdeInit
, EfiIdeBusPhaseMaximum
, 0);
897 if (SupportedModes
!= NULL
) {
898 FreePool (SupportedModes
);
901 PERF_START (NULL
, "Finish IDE detection", "IDE", 1);
902 PERF_END (NULL
, "Finish IDE detection", "IDE", 0);
909 // Report error code: controller error
911 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
912 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
913 (EFI_IO_BUS_ATA_ATAPI
| EFI_IOB_EC_CONTROLLER_ERROR
),
919 &gEfiIdeControllerInitProtocolGuid
,
920 This
->DriverBindingHandle
,
924 gBS
->UninstallMultipleProtocolInterfaces (
927 IdeBusDriverPrivateData
,
931 if (IdeBusDriverPrivateData
!= NULL
) {
932 gBS
->FreePool (IdeBusDriverPrivateData
);
935 if (SupportedModes
!= NULL
) {
936 gBS
->FreePool (SupportedModes
);
941 &gEfiPciIoProtocolGuid
,
942 This
->DriverBindingHandle
,
948 &gEfiDevicePathProtocolGuid
,
949 This
->DriverBindingHandle
,
957 Stop function of Driver Binding Protocol which is to stop the driver on Controller Handle and all
958 child handle attached to the controller handle if there are.
960 @param This Protocol instance pointer.
961 @param Controller Handle of device to stop driver on
962 @param NumberOfChildren Not used
963 @param ChildHandleBuffer Not used
965 @retval EFI_SUCCESS This driver is removed DeviceHandle
966 @retval other This driver was not removed from this device
971 IDEBusDriverBindingStop (
972 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
973 IN EFI_HANDLE Controller
,
974 IN UINTN NumberOfChildren
,
975 IN EFI_HANDLE
*ChildHandleBuffer
979 EFI_PCI_IO_PROTOCOL
*PciIo
;
980 BOOLEAN AllChildrenStopped
;
982 IDE_BUS_DRIVER_PRIVATE_DATA
*IdeBusDriverPrivateData
;
985 IdeBusDriverPrivateData
= NULL
;
987 if (NumberOfChildren
== 0) {
989 Status
= gBS
->OpenProtocol (
991 &gEfiPciIoProtocolGuid
,
993 This
->DriverBindingHandle
,
995 EFI_OPEN_PROTOCOL_GET_PROTOCOL
997 if (!EFI_ERROR (Status
)) {
998 Status
= PciIo
->Attributes (
1000 EfiPciIoAttributeOperationSupported
,
1004 if (!EFI_ERROR (Status
)) {
1005 Supports
&= EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO
| EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO
| EFI_PCI_DEVICE_ENABLE
;
1008 EfiPciIoAttributeOperationDisable
,
1018 (VOID
**) &IdeBusDriverPrivateData
,
1019 This
->DriverBindingHandle
,
1021 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1024 gBS
->UninstallMultipleProtocolInterfaces (
1027 IdeBusDriverPrivateData
,
1031 if (IdeBusDriverPrivateData
!= NULL
) {
1032 gBS
->FreePool (IdeBusDriverPrivateData
);
1035 // Close the bus driver
1037 gBS
->CloseProtocol (
1039 &gEfiIdeControllerInitProtocolGuid
,
1040 This
->DriverBindingHandle
,
1043 gBS
->CloseProtocol (
1045 &gEfiPciIoProtocolGuid
,
1046 This
->DriverBindingHandle
,
1049 gBS
->CloseProtocol (
1051 &gEfiDevicePathProtocolGuid
,
1052 This
->DriverBindingHandle
,
1059 AllChildrenStopped
= TRUE
;
1061 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
1063 Status
= DeRegisterIdeDevice (This
, Controller
, ChildHandleBuffer
[Index
]);
1065 if (EFI_ERROR (Status
)) {
1066 AllChildrenStopped
= FALSE
;
1070 if (!AllChildrenStopped
) {
1071 return EFI_DEVICE_ERROR
;
1078 issue ATA or ATAPI command to reset a block IO device.
1079 @param This Block IO protocol instance pointer.
1080 @param ExtendedVerification If FALSE,for ATAPI device, driver will only invoke ATAPI reset method
1081 If TRUE, for ATAPI device, driver need invoke ATA reset method after
1082 invoke ATAPI reset method
1084 @retval EFI_DEVICE_ERROR When the device is neighther ATA device or ATAPI device.
1085 @retval EFI_SUCCESS The device reset successfully
1091 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1092 IN BOOLEAN ExtendedVerification
1095 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1099 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1101 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1103 // Requery IDE IO resources in case of the switch of native and legacy modes
1105 ReassignIdeResources (IdeBlkIoDevice
);
1108 // for ATA device, using ATA reset method
1110 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1111 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1112 Status
= AtaSoftReset (IdeBlkIoDevice
);
1116 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1117 Status
= EFI_DEVICE_ERROR
;
1122 // for ATAPI device, using ATAPI reset method
1124 Status
= AtapiSoftReset (IdeBlkIoDevice
);
1125 if (ExtendedVerification
) {
1126 Status
= AtaSoftReset (IdeBlkIoDevice
);
1130 gBS
->RestoreTPL (OldTpl
);
1135 Read data from a block IO device
1137 @param This Block IO protocol instance pointer.
1138 @param MediaId The media ID of the device
1139 @param Lba Starting LBA address to read data
1140 @param BufferSize The size of data to be read
1141 @param Buffer Caller supplied buffer to save data
1143 @retval EFI_DEVICE_ERROR unknown device type
1144 @retval other read data status.
1149 IDEBlkIoReadBlocks (
1150 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1153 IN UINTN BufferSize
,
1157 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1161 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1163 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1166 // Requery IDE IO resources in case of the switch of native and legacy modes
1168 ReassignIdeResources (IdeBlkIoDevice
);
1171 // For ATA compatible device, use ATA read block's mechanism
1173 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1174 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1175 Status
= AtaBlkIoReadBlocks (
1185 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1186 Status
= EFI_DEVICE_ERROR
;
1191 // for ATAPI device, using ATAPI read block's mechanism
1193 Status
= AtapiBlkIoReadBlocks (
1202 gBS
->RestoreTPL (OldTpl
);
1208 Write data to block io device.
1210 @param This Protocol instance pointer.
1211 @param MediaId The media ID of the device
1212 @param Lba Starting LBA address to write data
1213 @param BufferSize The size of data to be written
1214 @param Buffer Caller supplied buffer to save data
1216 @retval EFI_DEVICE_ERROR unknown device type
1217 @retval other write data status
1222 IDEBlkIoWriteBlocks (
1223 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1226 IN UINTN BufferSize
,
1230 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1234 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1236 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_THIS (This
);
1238 // Requery IDE IO resources in case of the switch of native and legacy modes
1240 ReassignIdeResources (IdeBlkIoDevice
);
1243 // for ATA device, using ATA write block's mechanism
1245 if (IdeBlkIoDevice
->Type
== IdeHardDisk
||
1246 IdeBlkIoDevice
->Type
== Ide48bitAddressingHardDisk
) {
1248 Status
= AtaBlkIoWriteBlocks (
1258 if (IdeBlkIoDevice
->Type
== IdeUnknown
) {
1259 Status
= EFI_DEVICE_ERROR
;
1264 // for ATAPI device, using ATAPI write block's mechanism
1266 Status
= AtapiBlkIoWriteBlocks (
1275 gBS
->RestoreTPL (OldTpl
);
1279 Flushes all modified data to a physical block devices
1281 @param This Indicates a pointer to the calling context which to sepcify a
1282 sepcific block device
1284 @retval EFI_SUCCESS Always return success.
1288 IDEBlkIoFlushBlocks (
1289 IN EFI_BLOCK_IO_PROTOCOL
*This
1299 This function is used by the IDE bus driver to get inquiry data.
1300 Data format of Identify data is defined by the Interface GUID.
1302 @param This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
1303 @param InquiryData Pointer to a buffer for the inquiry data.
1304 @param InquiryDataSize Pointer to the value for the inquiry data size.
1306 @retval EFI_SUCCESS The command was accepted without any errors.
1307 @retval EFI_NOT_FOUND Device does not support this data class
1308 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
1309 @retval EFI_BUFFER_TOO_SMALL IntquiryDataSize not big enough
1314 IDEDiskInfoInquiry (
1315 IN EFI_DISK_INFO_PROTOCOL
*This
,
1316 IN OUT VOID
*InquiryData
,
1317 IN OUT UINT32
*InquiryDataSize
1320 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1322 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1324 if (*InquiryDataSize
< sizeof (ATAPI_INQUIRY_DATA
)) {
1325 *InquiryDataSize
= sizeof (ATAPI_INQUIRY_DATA
);
1326 return EFI_BUFFER_TOO_SMALL
;
1329 if (IdeBlkIoDevice
->InquiryData
== NULL
) {
1330 return EFI_NOT_FOUND
;
1333 gBS
->CopyMem (InquiryData
, IdeBlkIoDevice
->InquiryData
, sizeof (ATAPI_INQUIRY_DATA
));
1334 *InquiryDataSize
= sizeof (ATAPI_INQUIRY_DATA
);
1340 This function is used by the IDE bus driver to get identify data.
1341 Data format of Identify data is defined by the Interface GUID.
1343 @param This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
1344 @param IdentifyData Pointer to a buffer for the identify data.
1345 @param IdentifyDataSize Pointer to the value for the identify data size.
1347 @retval EFI_SUCCESS The command was accepted without any errors.
1348 @retval EFI_NOT_FOUND Device does not support this data class
1349 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
1350 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
1355 IDEDiskInfoIdentify (
1356 IN EFI_DISK_INFO_PROTOCOL
*This
,
1357 IN OUT VOID
*IdentifyData
,
1358 IN OUT UINT32
*IdentifyDataSize
1361 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1363 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1365 if (*IdentifyDataSize
< sizeof (EFI_IDENTIFY_DATA
)) {
1366 *IdentifyDataSize
= sizeof (EFI_IDENTIFY_DATA
);
1367 return EFI_BUFFER_TOO_SMALL
;
1370 if (IdeBlkIoDevice
->IdData
== NULL
) {
1371 return EFI_NOT_FOUND
;
1374 gBS
->CopyMem (IdentifyData
, IdeBlkIoDevice
->IdData
, sizeof (EFI_IDENTIFY_DATA
));
1375 *IdentifyDataSize
= sizeof (EFI_IDENTIFY_DATA
);
1381 This function is used by the IDE bus driver to get sense data.
1382 Data format of Sense data is defined by the Interface GUID.
1384 @param This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
1385 @param SenseData Pointer to the SenseData.
1386 @param SenseDataSize Size of SenseData in bytes.
1387 @param SenseDataNumber Pointer to the value for the identify data size.
1389 @retval EFI_SUCCESS The command was accepted without any errors.
1390 @retval EFI_NOT_FOUND Device does not support this data class
1391 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
1392 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough
1397 IDEDiskInfoSenseData (
1398 IN EFI_DISK_INFO_PROTOCOL
*This
,
1399 IN OUT VOID
*SenseData
,
1400 IN OUT UINT32
*SenseDataSize
,
1401 OUT UINT8
*SenseDataNumber
1404 return EFI_NOT_FOUND
;
1408 This function is used by the IDE bus driver to get controller information.
1410 @param This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
1411 @param IdeChannel Pointer to the Ide Channel number. Primary or secondary.
1412 @param IdeDevice Pointer to the Ide Device number. Master or slave.
1414 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid
1415 @retval EFI_UNSUPPORTED This is not an IDE device
1420 IDEDiskInfoWhichIde (
1421 IN EFI_DISK_INFO_PROTOCOL
*This
,
1422 OUT UINT32
*IdeChannel
,
1423 OUT UINT32
*IdeDevice
1426 IDE_BLK_IO_DEV
*IdeBlkIoDevice
;
1428 IdeBlkIoDevice
= IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS (This
);
1429 *IdeChannel
= IdeBlkIoDevice
->Channel
;
1430 *IdeDevice
= IdeBlkIoDevice
->Device
;
1436 The is an event(generally the event is exitBootService event) call back function.
1437 Clear pending IDE interrupt before OS loader/kernel take control of the IDE device.
1439 @param Event Pointer to this event
1440 @param Context Event hanlder private data
1451 UINT64 IoPortForBmis
;
1452 UINT8 RegisterValue
;
1453 IDE_BLK_IO_DEV
*IdeDev
;
1458 IdeDev
= (IDE_BLK_IO_DEV
*) Context
;
1461 // Obtain IDE IO port registers' base addresses
1463 Status
= ReassignIdeResources (IdeDev
);
1464 if (EFI_ERROR (Status
)) {
1469 // Check whether interrupt is pending
1473 // Reset IDE device to force it de-assert interrupt pin
1474 // Note: this will reset all devices on this IDE channel
1476 Status
= AtaSoftReset (IdeDev
);
1477 if (EFI_ERROR (Status
)) {
1482 // Get base address of IDE Bus Master Status Regsiter
1484 if (IdePrimary
== IdeDev
->Channel
) {
1485 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISP_OFFSET
;
1487 if (IdeSecondary
== IdeDev
->Channel
) {
1488 IoPortForBmis
= IdeDev
->IoPort
->BusMasterBaseAddr
+ BMISS_OFFSET
;
1494 // Read BMIS register and clear ERROR and INTR bit
1496 IdeDev
->PciIo
->Io
.Read (
1499 EFI_PCI_IO_PASS_THROUGH_BAR
,
1505 RegisterValue
|= (BMIS_INTERRUPT
| BMIS_ERROR
);
1507 IdeDev
->PciIo
->Io
.Write (
1510 EFI_PCI_IO_PASS_THROUGH_BAR
,
1517 // Select the other device on this channel to ensure this device to release the interrupt pin
1519 if (IdeDev
->Device
== 0) {
1520 RegisterValue
= (1 << 4) | 0xe0;
1522 RegisterValue
= (0 << 4) | 0xe0;
1526 IdeDev
->IoPort
->Head
,
1533 The user Entry Point for module IdeBus. The user code starts with this function.
1535 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1536 @param[in] SystemTable A pointer to the EFI System Table.
1538 @retval EFI_SUCCESS The entry point is executed successfully.
1539 @retval other Some error occurs when executing this entry point.
1545 IN EFI_HANDLE ImageHandle
,
1546 IN EFI_SYSTEM_TABLE
*SystemTable
1552 // Install driver model protocol(s).
1554 Status
= EfiLibInstallAllDriverProtocols2 (
1557 &gIDEBusDriverBinding
,
1559 &gIDEBusComponentName
,
1560 &gIDEBusComponentName2
,
1563 &gIDEBusDriverDiagnostics
,
1564 &gIDEBusDriverDiagnostics2
1566 ASSERT_EFI_ERROR (Status
);