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 if ((Resources
) && !(PCI_IS_ROOT_FDO(DeviceExtension
)))
94 /* These resources would only be for non-root FDOs, unhandled for now */
95 ASSERT(Resources
->Count
== 1);
96 UNIMPLEMENTED_DBGBREAK();
99 /* Initialize the arbiter for this FDO */
100 Status
= PciInitializeArbiterRanges(DeviceExtension
, Resources
);
101 if (!NT_SUCCESS(Status
))
103 /* Cancel the transition if this failed */
104 PciCancelStateTransition(DeviceExtension
, PciStarted
);
108 /* Again, check for boot-provided resources for non-root FDO */
109 if ((Resources
) && !(PCI_IS_ROOT_FDO(DeviceExtension
)))
111 /* Unhandled for now */
112 ASSERT(Resources
->Count
== 1);
113 UNIMPLEMENTED_DBGBREAK();
116 /* Commit the transition to the started state */
117 PciCommitStateTransition(DeviceExtension
, PciStarted
);
118 return STATUS_SUCCESS
;
123 PciFdoIrpQueryRemoveDevice(IN PIRP Irp
,
124 IN PIO_STACK_LOCATION IoStackLocation
,
125 IN PPCI_FDO_EXTENSION DeviceExtension
)
128 return STATUS_NOT_SUPPORTED
;
133 PciFdoIrpRemoveDevice(IN PIRP Irp
,
134 IN PIO_STACK_LOCATION IoStackLocation
,
135 IN PPCI_FDO_EXTENSION DeviceExtension
)
137 UNIMPLEMENTED_DBGBREAK();
138 return STATUS_NOT_SUPPORTED
;
143 PciFdoIrpCancelRemoveDevice(IN PIRP Irp
,
144 IN PIO_STACK_LOCATION IoStackLocation
,
145 IN PPCI_FDO_EXTENSION DeviceExtension
)
147 UNIMPLEMENTED_DBGBREAK();
148 return STATUS_NOT_SUPPORTED
;
153 PciFdoIrpStopDevice(IN PIRP Irp
,
154 IN PIO_STACK_LOCATION IoStackLocation
,
155 IN PPCI_FDO_EXTENSION DeviceExtension
)
157 UNIMPLEMENTED_DBGBREAK();
158 return STATUS_NOT_SUPPORTED
;
163 PciFdoIrpQueryStopDevice(IN PIRP Irp
,
164 IN PIO_STACK_LOCATION IoStackLocation
,
165 IN PPCI_FDO_EXTENSION DeviceExtension
)
167 UNIMPLEMENTED_DBGBREAK();
168 return STATUS_NOT_SUPPORTED
;
173 PciFdoIrpCancelStopDevice(IN PIRP Irp
,
174 IN PIO_STACK_LOCATION IoStackLocation
,
175 IN PPCI_FDO_EXTENSION DeviceExtension
)
177 UNIMPLEMENTED_DBGBREAK();
178 return STATUS_NOT_SUPPORTED
;
183 PciFdoIrpQueryDeviceRelations(IN PIRP Irp
,
184 IN PIO_STACK_LOCATION IoStackLocation
,
185 IN PPCI_FDO_EXTENSION DeviceExtension
)
190 /* Are bus relations being queried? */
191 if (IoStackLocation
->Parameters
.QueryDeviceRelations
.Type
!= BusRelations
)
193 /* The FDO is a bus, so only bus relations can be obtained */
194 Status
= STATUS_NOT_SUPPORTED
;
198 /* Scan the PCI bus and build the device relations for the caller */
199 Status
= PciQueryDeviceRelations(DeviceExtension
,
201 &Irp
->IoStatus
.Information
);
204 /* Return the enumeration status back */
210 PciFdoIrpQueryInterface(IN PIRP Irp
,
211 IN PIO_STACK_LOCATION IoStackLocation
,
212 IN PPCI_FDO_EXTENSION DeviceExtension
)
216 ASSERT(DeviceExtension
->ExtensionType
== PciFdoExtensionType
);
218 /* Deleted extensions don't respond to IRPs */
219 if (DeviceExtension
->DeviceState
== PciDeleted
)
221 /* Hand it back to try to deal with it */
222 return PciPassIrpFromFdoToPdo(DeviceExtension
, Irp
);
225 /* Query our driver for this interface */
226 Status
= PciQueryInterface(DeviceExtension
,
227 IoStackLocation
->Parameters
.QueryInterface
.
229 IoStackLocation
->Parameters
.QueryInterface
.
231 IoStackLocation
->Parameters
.QueryInterface
.
233 IoStackLocation
->Parameters
.QueryInterface
.
234 InterfaceSpecificData
,
235 IoStackLocation
->Parameters
.QueryInterface
.
238 if (NT_SUCCESS(Status
))
240 /* We found it, let the PDO handle it */
241 Irp
->IoStatus
.Status
= Status
;
242 return PciPassIrpFromFdoToPdo(DeviceExtension
, Irp
);
244 else if (Status
== STATUS_NOT_SUPPORTED
)
246 /* Otherwise, we can't handle it, let someone else down the stack try */
247 Status
= PciCallDownIrpStack(DeviceExtension
, Irp
);
248 if (Status
== STATUS_NOT_SUPPORTED
)
250 /* They can't either, try a last-resort interface lookup */
251 Status
= PciQueryInterface(DeviceExtension
,
252 IoStackLocation
->Parameters
.QueryInterface
.
254 IoStackLocation
->Parameters
.QueryInterface
.
256 IoStackLocation
->Parameters
.QueryInterface
.
258 IoStackLocation
->Parameters
.QueryInterface
.
259 InterfaceSpecificData
,
260 IoStackLocation
->Parameters
.QueryInterface
.
266 /* Has anyone claimed this interface yet? */
267 if (Status
== STATUS_NOT_SUPPORTED
)
269 /* No, return the original IRP status */
270 Status
= Irp
->IoStatus
.Status
;
274 /* Yes, set the new IRP status */
275 Irp
->IoStatus
.Status
= Status
;
278 /* Complete this IRP */
279 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
285 PciFdoIrpQueryCapabilities(IN PIRP Irp
,
286 IN PIO_STACK_LOCATION IoStackLocation
,
287 IN PPCI_FDO_EXTENSION DeviceExtension
)
289 PDEVICE_CAPABILITIES Capabilities
;
291 ASSERT_FDO(DeviceExtension
);
293 /* Get the capabilities */
294 Capabilities
= IoStackLocation
->Parameters
.DeviceCapabilities
.Capabilities
;
296 /* Inherit wake levels and power mappings from the higher-up capabilities */
297 DeviceExtension
->PowerState
.SystemWakeLevel
= Capabilities
->SystemWake
;
298 DeviceExtension
->PowerState
.DeviceWakeLevel
= Capabilities
->DeviceWake
;
299 RtlCopyMemory(DeviceExtension
->PowerState
.SystemStateMapping
,
300 Capabilities
->DeviceState
,
301 sizeof(DeviceExtension
->PowerState
.SystemStateMapping
));
303 /* Dump the capabilities and return success */
304 PciDebugDumpQueryCapabilities(Capabilities
);
305 return STATUS_SUCCESS
;
310 PciFdoIrpDeviceUsageNotification(IN PIRP Irp
,
311 IN PIO_STACK_LOCATION IoStackLocation
,
312 IN PPCI_FDO_EXTENSION DeviceExtension
)
314 UNIMPLEMENTED_DBGBREAK();
315 return STATUS_NOT_SUPPORTED
;
320 PciFdoIrpSurpriseRemoval(IN PIRP Irp
,
321 IN PIO_STACK_LOCATION IoStackLocation
,
322 IN PPCI_FDO_EXTENSION DeviceExtension
)
324 UNIMPLEMENTED_DBGBREAK();
325 return STATUS_NOT_SUPPORTED
;
330 PciFdoIrpQueryLegacyBusInformation(IN PIRP Irp
,
331 IN PIO_STACK_LOCATION IoStackLocation
,
332 IN PPCI_FDO_EXTENSION DeviceExtension
)
334 UNIMPLEMENTED_DBGBREAK();
335 return STATUS_NOT_SUPPORTED
;
340 PciGetHotPlugParameters(IN PPCI_FDO_EXTENSION FdoExtension
)
342 ACPI_EVAL_INPUT_BUFFER InputBuffer
;
343 PACPI_EVAL_OUTPUT_BUFFER OutputBuffer
;
348 /* We should receive 4 parameters, per the HPP specification */
349 Length
= sizeof(ACPI_EVAL_OUTPUT_BUFFER
) + 4 * sizeof(ACPI_METHOD_ARGUMENT
);
351 /* Allocate the buffer to hold the parameters */
352 OutputBuffer
= ExAllocatePoolWithTag(PagedPool
, Length
, PCI_POOL_TAG
);
353 if (!OutputBuffer
) return;
355 /* Initialize the output and input buffers. The method is _HPP */
356 RtlZeroMemory(OutputBuffer
, Length
);
357 *(PULONG
)InputBuffer
.MethodName
= 'PPH_';
358 InputBuffer
.Signature
= ACPI_EVAL_INPUT_BUFFER_SIGNATURE
;
361 /* Send the IOCTL to the ACPI driver */
362 Status
= PciSendIoctl(FdoExtension
->PhysicalDeviceObject
,
363 IOCTL_ACPI_EVAL_METHOD
,
368 if (!NT_SUCCESS(Status
))
370 /* The method failed, check if we can salvage data from parent */
371 if (!PCI_IS_ROOT_FDO(FdoExtension
))
373 /* Copy the root bus' hot plug parameters */
374 FdoExtension
->HotPlugParameters
= FdoExtension
->ParentFdoExtension
->HotPlugParameters
;
377 /* Nothing more to do on this path */
381 /* ACPI sent back some data. 4 parameters are expected in the output */
382 if (OutputBuffer
->Count
!= 4) break;
384 /* HotPlug PCI Support not yet implemented */
385 UNIMPLEMENTED_DBGBREAK();
388 /* Free the buffer and return */
389 ExFreePoolWithTag(OutputBuffer
, 0);
394 PciInitializeFdoExtensionCommonFields(PPCI_FDO_EXTENSION FdoExtension
,
395 IN PDEVICE_OBJECT DeviceObject
,
396 IN PDEVICE_OBJECT PhysicalDeviceObject
)
398 /* Initialize the extension */
399 RtlZeroMemory(FdoExtension
, sizeof(PCI_FDO_EXTENSION
));
401 /* Setup the common fields */
402 FdoExtension
->PhysicalDeviceObject
= PhysicalDeviceObject
;
403 FdoExtension
->FunctionalDeviceObject
= DeviceObject
;
404 FdoExtension
->ExtensionType
= PciFdoExtensionType
;
405 FdoExtension
->PowerState
.CurrentSystemState
= PowerSystemWorking
;
406 FdoExtension
->PowerState
.CurrentDeviceState
= PowerDeviceD0
;
407 FdoExtension
->IrpDispatchTable
= &PciFdoDispatchTable
;
409 /* Initialize the extension locks */
410 KeInitializeEvent(&FdoExtension
->SecondaryExtLock
, SynchronizationEvent
, TRUE
);
411 KeInitializeEvent(&FdoExtension
->ChildListLock
, SynchronizationEvent
, TRUE
);
413 /* Initialize the default state */
414 PciInitializeState(FdoExtension
);
419 PciAddDevice(IN PDRIVER_OBJECT DriverObject
,
420 IN PDEVICE_OBJECT PhysicalDeviceObject
)
422 PCM_RESOURCE_LIST Descriptor
;
423 PDEVICE_OBJECT AttachedTo
;
424 PPCI_FDO_EXTENSION FdoExtension
;
425 PPCI_FDO_EXTENSION ParentExtension
;
426 PPCI_PDO_EXTENSION PdoExtension
;
427 PDEVICE_OBJECT DeviceObject
;
428 UCHAR Buffer
[sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + sizeof(ULONG
)];
429 PKEY_VALUE_PARTIAL_INFORMATION ValueInfo
= (PKEY_VALUE_PARTIAL_INFORMATION
)Buffer
;
432 UNICODE_STRING ValueName
;
435 DPRINT1("PCI - AddDevice (a new bus). PDO: %p (Driver: %wZ)\n",
436 PhysicalDeviceObject
, &PhysicalDeviceObject
->DriverObject
->DriverName
);
438 /* Zero out variables so failure path knows what to do */
444 /* Check if there's already a device extension for this bus */
445 ParentExtension
= PciFindParentPciFdoExtension(PhysicalDeviceObject
,
449 /* Make sure we find a real PDO */
450 PdoExtension
= PhysicalDeviceObject
->DeviceExtension
;
451 ASSERT_PDO(PdoExtension
);
453 /* Make sure it's a PCI-to-PCI bridge */
454 if ((PdoExtension
->BaseClass
!= PCI_CLASS_BRIDGE_DEV
) ||
455 (PdoExtension
->SubClass
!= PCI_SUBCLASS_BR_PCI_TO_PCI
))
457 /* This should never happen */
458 DPRINT1("PCI - PciAddDevice for Non-Root/Non-PCI-PCI bridge,\n"
459 " Class %02x, SubClass %02x, will not add.\n",
460 PdoExtension
->BaseClass
,
461 PdoExtension
->SubClass
);
462 ASSERT((PdoExtension
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) &&
463 (PdoExtension
->SubClass
== PCI_SUBCLASS_BR_PCI_TO_PCI
));
465 /* Enter the failure path */
466 Status
= STATUS_INVALID_DEVICE_REQUEST
;
470 /* Subordinate bus on the bridge */
471 DPRINT1("PCI - AddDevice (new bus is child of bus 0x%x).\n",
472 ParentExtension
->BaseBus
);
474 /* Make sure PCI bus numbers are configured */
475 if (!PciAreBusNumbersConfigured(PdoExtension
))
477 /* This is a critical failure */
478 DPRINT1("PCI - Bus numbers not configured for bridge (0x%x.0x%x.0x%x)\n",
479 ParentExtension
->BaseBus
,
480 PdoExtension
->Slot
.u
.bits
.DeviceNumber
,
481 PdoExtension
->Slot
.u
.bits
.FunctionNumber
);
483 /* Enter the failure path */
484 Status
= STATUS_INVALID_DEVICE_REQUEST
;
489 /* Create the FDO for the bus */
490 Status
= IoCreateDevice(DriverObject
,
491 sizeof(PCI_FDO_EXTENSION
),
493 FILE_DEVICE_BUS_EXTENDER
,
497 if (!NT_SUCCESS(Status
)) break;
499 /* Initialize the extension for the FDO */
500 FdoExtension
= DeviceObject
->DeviceExtension
;
501 PciInitializeFdoExtensionCommonFields(DeviceObject
->DeviceExtension
,
503 PhysicalDeviceObject
);
505 /* Attach to the root PDO */
506 Status
= STATUS_NO_SUCH_DEVICE
;
507 AttachedTo
= IoAttachDeviceToDeviceStack(DeviceObject
,
508 PhysicalDeviceObject
);
509 ASSERT(AttachedTo
!= NULL
);
510 if (!AttachedTo
) break;
511 FdoExtension
->AttachedDeviceObject
= AttachedTo
;
513 /* Check if this is a child bus, or the root */
516 /* The child inherits root data */
517 FdoExtension
->BaseBus
= PdoExtension
->Dependent
.type1
.SecondaryBus
;
518 FdoExtension
->BusRootFdoExtension
= ParentExtension
->BusRootFdoExtension
;
519 PdoExtension
->BridgeFdoExtension
= FdoExtension
;
520 FdoExtension
->ParentFdoExtension
= ParentExtension
;
524 /* Query the boot configuration */
525 Status
= PciGetDeviceProperty(PhysicalDeviceObject
,
526 DevicePropertyBootConfiguration
,
527 (PVOID
*)&Descriptor
);
528 if (!NT_SUCCESS(Status
))
530 /* No configuration has been set */
535 /* Root PDO in ReactOS does not assign boot resources */
536 UNIMPLEMENTED_DBGBREAK("Encountered during setup\n");
542 /* Root PDO in ReactOS does not assign boot resources */
543 UNIMPLEMENTED_DBGBREAK();
547 /* Default configuration isn't the normal path on Windows */
548 if (PciBreakOnDefault
)
550 /* If a second bus is found and there's still no data, crash */
551 KeBugCheckEx(PCI_BUS_DRIVER_INTERNAL
,
553 (ULONG_PTR
)DeviceObject
,
558 /* Warn that a default configuration will be used, and set bus 0 */
559 DPRINT1("PCI Will use default configuration.\n");
560 PciBreakOnDefault
= TRUE
;
561 FdoExtension
->BaseBus
= 0;
564 /* This is the root bus */
565 FdoExtension
->BusRootFdoExtension
= FdoExtension
;
568 /* Get the HAL or ACPI Bus Handler Callbacks for Configuration Access */
569 Status
= PciGetConfigHandlers(FdoExtension
);
570 if (!NT_SUCCESS(Status
)) break;
572 /* Initialize all the supported PCI arbiters */
573 Status
= PciInitializeArbiters(FdoExtension
);
574 if (!NT_SUCCESS(Status
)) break;
576 /* This is a real FDO, insert it into the list */
577 FdoExtension
->Fake
= FALSE
;
578 PciInsertEntryAtTail(&PciFdoExtensionListHead
,
582 /* Open the device registry key so that we can query the errata flags */
583 IoOpenDeviceRegistryKey(DeviceObject
,
584 PLUGPLAY_REGKEY_DEVICE
,
588 /* Open the value that contains errata flags for this bus instance */
589 RtlInitUnicodeString(&ValueName
, L
"HackFlags");
590 Status
= ZwQueryValueKey(KeyHandle
,
592 KeyValuePartialInformation
,
597 if (NT_SUCCESS(Status
))
599 /* Make sure the data is of expected type and size */
600 if ((ValueInfo
->Type
== REG_DWORD
) &&
601 (ValueInfo
->DataLength
== sizeof(ULONG
)))
603 /* Read the flags for this bus */
604 FdoExtension
->BusHackFlags
= *(PULONG
)&ValueInfo
->Data
;
608 /* Query ACPI for PCI HotPlug Support */
609 PciGetHotPlugParameters(FdoExtension
);
611 /* The Bus FDO is now initialized */
612 DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
613 return STATUS_SUCCESS
;
616 /* This is the failure path */
617 ASSERT(!NT_SUCCESS(Status
));
619 /* Check if the FDO extension exists */
620 if (FdoExtension
) DPRINT1("Should destroy secondaries\n");
622 /* Delete device objects */
623 if (AttachedTo
) IoDetachDevice(AttachedTo
);
624 if (DeviceObject
) IoDeleteDevice(DeviceObject
);