2 * PROJECT: ReactOS PCI Bus Driver
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: drivers/bus/pci/fdo.c
5 * PURPOSE: FDO Device Management
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 /* GLOBALS ********************************************************************/
17 SINGLE_LIST_ENTRY PciFdoExtensionListHead
;
18 BOOLEAN PciBreakOnDefault
;
20 PCI_MN_DISPATCH_TABLE PciFdoDispatchPowerTable
[] =
22 {IRP_DISPATCH
, (PCI_DISPATCH_FUNCTION
)PciFdoWaitWake
},
23 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
24 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoSetPowerState
},
25 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpQueryPower
},
26 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
}
29 PCI_MN_DISPATCH_TABLE PciFdoDispatchPnpTable
[] =
31 {IRP_UPWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpStartDevice
},
32 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpQueryRemoveDevice
},
33 {IRP_DISPATCH
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpRemoveDevice
},
34 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpCancelRemoveDevice
},
35 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpStopDevice
},
36 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpQueryStopDevice
},
37 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpCancelStopDevice
},
38 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpQueryDeviceRelations
},
39 {IRP_DISPATCH
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpQueryInterface
},
40 {IRP_UPWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpQueryCapabilities
},
41 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
42 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
43 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
44 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
45 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
46 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
47 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
48 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
49 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
50 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
51 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
52 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
},
53 {IRP_UPWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpDeviceUsageNotification
},
54 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpSurpriseRemoval
},
55 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciFdoIrpQueryLegacyBusInformation
},
56 {IRP_DOWNWARD
, (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
}
59 PCI_MJ_DISPATCH_TABLE PciFdoDispatchTable
=
61 IRP_MN_QUERY_LEGACY_BUS_INFORMATION
,
62 PciFdoDispatchPnpTable
,
64 PciFdoDispatchPowerTable
,
66 (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
,
68 (PCI_DISPATCH_FUNCTION
)PciIrpNotSupported
71 /* FUNCTIONS ******************************************************************/
75 PciFdoIrpStartDevice(IN PIRP Irp
,
76 IN PIO_STACK_LOCATION IoStackLocation
,
77 IN PPCI_FDO_EXTENSION DeviceExtension
)
80 PCM_RESOURCE_LIST Resources
;
83 /* The device stack must be starting the FDO in a success path */
84 if (!NT_SUCCESS(Irp
->IoStatus
.Status
)) return STATUS_NOT_SUPPORTED
;
86 /* Attempt to switch the state machine to the started state */
87 Status
= PciBeginStateTransition(DeviceExtension
, PciStarted
);
88 if (!NT_SUCCESS(Status
)) return Status
;
90 /* Check for any boot-provided resources */
91 Resources
= IoStackLocation
->Parameters
.StartDevice
.AllocatedResources
;
92 DPRINT1("Resources: %p\n", Resources
);
93 if ((Resources
) && !(PCI_IS_ROOT_FDO(DeviceExtension
)))
95 /* These resources would only be for non-root FDOs, unhandled for now */
96 ASSERT(Resources
->Count
== 1);
101 /* Initialize the arbiter for this FDO */
102 Status
= PciInitializeArbiterRanges(DeviceExtension
, Resources
);
103 if (!NT_SUCCESS(Status
))
105 /* Cancel the transition if this failed */
106 PciCancelStateTransition(DeviceExtension
, PciStarted
);
110 /* Again, check for boot-provided resources for non-root FDO */
111 if ((Resources
) && !(PCI_IS_ROOT_FDO(DeviceExtension
)))
113 /* Unhandled for now */
114 ASSERT(Resources
->Count
== 1);
119 /* Commit the transition to the started state */
120 PciCommitStateTransition(DeviceExtension
, PciStarted
);
121 return STATUS_SUCCESS
;
126 PciFdoIrpQueryRemoveDevice(IN PIRP Irp
,
127 IN PIO_STACK_LOCATION IoStackLocation
,
128 IN PPCI_FDO_EXTENSION DeviceExtension
)
132 return STATUS_NOT_SUPPORTED
;
137 PciFdoIrpRemoveDevice(IN PIRP Irp
,
138 IN PIO_STACK_LOCATION IoStackLocation
,
139 IN PPCI_FDO_EXTENSION DeviceExtension
)
143 return STATUS_NOT_SUPPORTED
;
148 PciFdoIrpCancelRemoveDevice(IN PIRP Irp
,
149 IN PIO_STACK_LOCATION IoStackLocation
,
150 IN PPCI_FDO_EXTENSION DeviceExtension
)
154 return STATUS_NOT_SUPPORTED
;
159 PciFdoIrpStopDevice(IN PIRP Irp
,
160 IN PIO_STACK_LOCATION IoStackLocation
,
161 IN PPCI_FDO_EXTENSION DeviceExtension
)
165 return STATUS_NOT_SUPPORTED
;
170 PciFdoIrpQueryStopDevice(IN PIRP Irp
,
171 IN PIO_STACK_LOCATION IoStackLocation
,
172 IN PPCI_FDO_EXTENSION DeviceExtension
)
176 return STATUS_NOT_SUPPORTED
;
181 PciFdoIrpCancelStopDevice(IN PIRP Irp
,
182 IN PIO_STACK_LOCATION IoStackLocation
,
183 IN PPCI_FDO_EXTENSION DeviceExtension
)
187 return STATUS_NOT_SUPPORTED
;
192 PciFdoIrpQueryDeviceRelations(IN PIRP Irp
,
193 IN PIO_STACK_LOCATION IoStackLocation
,
194 IN PPCI_FDO_EXTENSION DeviceExtension
)
199 /* Are bus relations being queried? */
200 if (IoStackLocation
->Parameters
.QueryDeviceRelations
.Type
!= BusRelations
)
202 /* The FDO is a bus, so only bus relations can be obtained */
203 Status
= STATUS_NOT_SUPPORTED
;
207 /* Scan the PCI bus and build the device relations for the caller */
208 Status
= PciQueryDeviceRelations(DeviceExtension
,
210 &Irp
->IoStatus
.Information
);
213 /* Return the enumeration status back */
219 PciFdoIrpQueryInterface(IN PIRP Irp
,
220 IN PIO_STACK_LOCATION IoStackLocation
,
221 IN PPCI_FDO_EXTENSION DeviceExtension
)
225 ASSERT(DeviceExtension
->ExtensionType
== PciFdoExtensionType
);
227 /* Deleted extensions don't respond to IRPs */
228 if (DeviceExtension
->DeviceState
== PciDeleted
)
230 /* Hand it bacO try to deal with it */
231 return PciPassIrpFromFdoToPdo(DeviceExtension
, Irp
);
234 /* Query our driver for this interface */
235 Status
= PciQueryInterface(DeviceExtension
,
236 IoStackLocation
->Parameters
.QueryInterface
.
238 IoStackLocation
->Parameters
.QueryInterface
.
240 IoStackLocation
->Parameters
.QueryInterface
.
242 IoStackLocation
->Parameters
.QueryInterface
.
243 InterfaceSpecificData
,
244 IoStackLocation
->Parameters
.QueryInterface
.
247 if (NT_SUCCESS(Status
))
249 /* We found it, let the PDO handle it */
250 Irp
->IoStatus
.Status
= Status
;
251 return PciPassIrpFromFdoToPdo(DeviceExtension
, Irp
);
253 else if (Status
== STATUS_NOT_SUPPORTED
)
255 /* Otherwise, we can't handle it, let someone else down the stack try */
256 Status
= PciCallDownIrpStack(DeviceExtension
, Irp
);
257 if (Status
== STATUS_NOT_SUPPORTED
)
259 /* They can't either, try a last-resort interface lookup */
260 Status
= PciQueryInterface(DeviceExtension
,
261 IoStackLocation
->Parameters
.QueryInterface
.
263 IoStackLocation
->Parameters
.QueryInterface
.
265 IoStackLocation
->Parameters
.QueryInterface
.
267 IoStackLocation
->Parameters
.QueryInterface
.
268 InterfaceSpecificData
,
269 IoStackLocation
->Parameters
.QueryInterface
.
275 /* Has anyone claimed this interface yet? */
276 if (Status
== STATUS_NOT_SUPPORTED
)
278 /* No, return the original IRP status */
279 Status
= Irp
->IoStatus
.Status
;
283 /* Yes, set the new IRP status */
284 Irp
->IoStatus
.Status
= Status
;
287 /* Complete this IRP */
288 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
294 PciFdoIrpQueryCapabilities(IN PIRP Irp
,
295 IN PIO_STACK_LOCATION IoStackLocation
,
296 IN PPCI_FDO_EXTENSION DeviceExtension
)
298 PDEVICE_CAPABILITIES Capabilities
;
300 ASSERT_FDO(DeviceExtension
);
302 /* Get the capabilities */
303 Capabilities
= IoStackLocation
->Parameters
.DeviceCapabilities
.Capabilities
;
305 /* Inherit wake levels and power mappings from the higher-up capabilities */
306 DeviceExtension
->PowerState
.SystemWakeLevel
= Capabilities
->SystemWake
;
307 DeviceExtension
->PowerState
.DeviceWakeLevel
= Capabilities
->DeviceWake
;
308 RtlCopyMemory(DeviceExtension
->PowerState
.SystemStateMapping
,
309 Capabilities
->DeviceState
,
310 sizeof(DeviceExtension
->PowerState
.SystemStateMapping
));
312 /* Dump the capabilities and return success */
313 PciDebugDumpQueryCapabilities(Capabilities
);
314 return STATUS_SUCCESS
;
319 PciFdoIrpDeviceUsageNotification(IN PIRP Irp
,
320 IN PIO_STACK_LOCATION IoStackLocation
,
321 IN PPCI_FDO_EXTENSION DeviceExtension
)
325 return STATUS_NOT_SUPPORTED
;
330 PciFdoIrpSurpriseRemoval(IN PIRP Irp
,
331 IN PIO_STACK_LOCATION IoStackLocation
,
332 IN PPCI_FDO_EXTENSION DeviceExtension
)
336 return STATUS_NOT_SUPPORTED
;
341 PciFdoIrpQueryLegacyBusInformation(IN PIRP Irp
,
342 IN PIO_STACK_LOCATION IoStackLocation
,
343 IN PPCI_FDO_EXTENSION DeviceExtension
)
347 return STATUS_NOT_SUPPORTED
;
352 PciGetHotPlugParameters(IN PPCI_FDO_EXTENSION FdoExtension
)
354 ACPI_EVAL_INPUT_BUFFER InputBuffer
;
355 PACPI_EVAL_OUTPUT_BUFFER OutputBuffer
;
360 /* We should receive 4 parameters, per the HPP specification */
361 Length
= sizeof(ACPI_EVAL_OUTPUT_BUFFER
) + 4 * sizeof(ACPI_METHOD_ARGUMENT
);
363 /* Allocate the buffer to hold the parameters */
364 OutputBuffer
= ExAllocatePoolWithTag(PagedPool
, Length
, PCI_POOL_TAG
);
365 if (!OutputBuffer
) return;
367 /* Initialize the output and input buffers. The method is _HPP */
368 RtlZeroMemory(OutputBuffer
, Length
);
369 *(PULONG
)InputBuffer
.MethodName
= 'PPH_';
370 InputBuffer
.Signature
= ACPI_EVAL_INPUT_BUFFER_SIGNATURE
;
373 /* Send the IOCTL to the ACPI driver */
374 Status
= PciSendIoctl(FdoExtension
->PhysicalDeviceObject
,
375 IOCTL_ACPI_EVAL_METHOD
,
380 if (!NT_SUCCESS(Status
))
382 /* The method failed, check if we can salvage data from parent */
383 if (!PCI_IS_ROOT_FDO(FdoExtension
))
385 /* Copy the root bus' hot plug parameters */
386 FdoExtension
->HotPlugParameters
= FdoExtension
->ParentFdoExtension
->HotPlugParameters
;
389 /* Nothing more to do on this path */
393 /* ACPI sent back some data. 4 parameters are expected in the output */
394 if (OutputBuffer
->Count
!= 4) break;
396 /* HotPlug PCI Support not yet implemented */
401 /* Free the buffer and return */
402 ExFreePoolWithTag(OutputBuffer
, 0);
407 PciInitializeFdoExtensionCommonFields(PPCI_FDO_EXTENSION FdoExtension
,
408 IN PDEVICE_OBJECT DeviceObject
,
409 IN PDEVICE_OBJECT PhysicalDeviceObject
)
411 /* Initialize the extension */
412 RtlZeroMemory(FdoExtension
, sizeof(PCI_FDO_EXTENSION
));
414 /* Setup the common fields */
415 FdoExtension
->PhysicalDeviceObject
= PhysicalDeviceObject
;
416 FdoExtension
->FunctionalDeviceObject
= DeviceObject
;
417 FdoExtension
->ExtensionType
= PciFdoExtensionType
;
418 FdoExtension
->PowerState
.CurrentSystemState
= PowerSystemWorking
;
419 FdoExtension
->PowerState
.CurrentDeviceState
= PowerDeviceD0
;
420 FdoExtension
->IrpDispatchTable
= &PciFdoDispatchTable
;
422 /* Initialize the extension locks */
423 KeInitializeEvent(&FdoExtension
->SecondaryExtLock
, SynchronizationEvent
, TRUE
);
424 KeInitializeEvent(&FdoExtension
->ChildListLock
, SynchronizationEvent
, TRUE
);
426 /* Initialize the default state */
427 PciInitializeState(FdoExtension
);
432 PciAddDevice(IN PDRIVER_OBJECT DriverObject
,
433 IN PDEVICE_OBJECT PhysicalDeviceObject
)
435 PCM_RESOURCE_LIST Descriptor
;
436 PDEVICE_OBJECT AttachedTo
;
437 PPCI_FDO_EXTENSION FdoExtension
;
438 PPCI_FDO_EXTENSION ParentExtension
;
439 PDEVICE_OBJECT DeviceObject
;
440 UCHAR Buffer
[sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + sizeof(ULONG
)];
441 PKEY_VALUE_PARTIAL_INFORMATION ValueInfo
= (PKEY_VALUE_PARTIAL_INFORMATION
)Buffer
;
444 UNICODE_STRING ValueName
;
447 DPRINT1("PCI - AddDevice (a new bus). PDO: %p (Driver: %wZ)\n",
448 PhysicalDeviceObject
, &PhysicalDeviceObject
->DriverObject
->DriverName
);
450 /* Zero out variables so failure path knows what to do */
454 /* Check if there's already a device extension for this bus */
455 ParentExtension
= PciFindParentPciFdoExtension(PhysicalDeviceObject
,
459 /* More than one PCI bus, this is not expected yet */
464 /* Create the FDO for the bus */
465 Status
= IoCreateDevice(DriverObject
,
466 sizeof(PCI_FDO_EXTENSION
),
468 FILE_DEVICE_BUS_EXTENDER
,
472 if (!NT_SUCCESS(Status
)) break;
474 /* Initialize the extension for the FDO */
475 FdoExtension
= DeviceObject
->DeviceExtension
;
476 PciInitializeFdoExtensionCommonFields(DeviceObject
->DeviceExtension
,
478 PhysicalDeviceObject
);
480 /* Attach to the root PDO */
481 Status
= STATUS_NO_SUCH_DEVICE
;
482 AttachedTo
= IoAttachDeviceToDeviceStack(DeviceObject
,
483 PhysicalDeviceObject
);
484 ASSERT(AttachedTo
!= NULL
);
485 if (!AttachedTo
) break;
486 FdoExtension
->AttachedDeviceObject
= AttachedTo
;
489 /* More than one PCI bus, this is not expected yet */
495 /* Query the boot configuration */
496 Status
= PciGetDeviceProperty(PhysicalDeviceObject
,
497 DevicePropertyBootConfiguration
,
498 (PVOID
*)&Descriptor
);
499 if (!NT_SUCCESS(Status
))
501 /* No configuration has been set */
506 /* Root PDO in ReactOS does not assign boot resources */
513 /* Root PDO in ReactOS does not assign boot resources */
519 /* Default configuration isn't the normal path on Windows */
520 if (PciBreakOnDefault
)
522 /* If a second bus is found and there's still no data, crash */
524 KeBugCheckEx(PCI_BUS_DRIVER_INTERNAL
,
526 (ULONG_PTR
)DeviceObject
,
530 DPRINT1("Windows would crash!\n");
534 /* Warn that a default configuration will be used, and set bus 0 */
535 DPRINT1("PCI Will use default configuration.\n");
536 PciBreakOnDefault
= TRUE
;
537 FdoExtension
->BaseBus
= 0;
540 /* This is the root bus */
541 FdoExtension
->BusRootFdoExtension
= FdoExtension
;
544 /* Get the HAL or ACPI Bus Handler Callbacks for Configuration Access */
545 Status
= PciGetConfigHandlers(FdoExtension
);
546 if (!NT_SUCCESS(Status
)) break;
548 /* Initialize all the supported PCI arbiters */
549 Status
= PciInitializeArbiters(FdoExtension
);
550 if (!NT_SUCCESS(Status
)) break;
552 /* This is a real FDO, insert it into the list */
553 FdoExtension
->Fake
= FALSE
;
554 PciInsertEntryAtTail(&PciFdoExtensionListHead
,
558 /* Open the device registry key so that we can query the errata flags */
559 IoOpenDeviceRegistryKey(DeviceObject
,
560 PLUGPLAY_REGKEY_DEVICE
,
564 /* Open the value that contains errata flags for this bus instance */
565 RtlInitUnicodeString(&ValueName
, L
"HackFlags");
566 Status
= ZwQueryValueKey(KeyHandle
,
568 KeyValuePartialInformation
,
573 if (NT_SUCCESS(Status
))
575 /* Make sure the data is of expected type and size */
576 if ((ValueInfo
->Type
== REG_DWORD
) &&
577 (ValueInfo
->DataLength
== sizeof(ULONG
)))
579 /* Read the flags for this bus */
580 FdoExtension
->BusHackFlags
= *(PULONG
)&ValueInfo
->Data
;
584 /* Query ACPI for PCI HotPlug Support */
585 PciGetHotPlugParameters(FdoExtension
);
587 /* The Bus FDO is now initialized */
588 DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
589 DPRINT1("PCI Root FDO Added: %p %p\n", DeviceObject
, FdoExtension
);
590 return STATUS_SUCCESS
;
593 /* This is the failure path */
594 ASSERT(!NT_SUCCESS(Status
));
595 if (AttachedTo
) IoDetachDevice(AttachedTo
);
596 if (DeviceObject
) IoDeleteDevice(DeviceObject
);