Add config interface start (PciReadWriteConfigSpace, PciReadSlotConfig)
[reactos.git] / reactos / drivers / bus / pcix / fdo.c
1 /*
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
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <pci.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 /* GLOBALS ********************************************************************/
16
17 SINGLE_LIST_ENTRY PciFdoExtensionListHead;
18 BOOLEAN PciBreakOnDefault;
19
20 PCI_MN_DISPATCH_TABLE PciFdoDispatchPowerTable[] =
21 {
22 {IRP_DISPATCH, PciFdoWaitWake},
23 {IRP_DOWNWARD, PciIrpNotSupported},
24 {IRP_DOWNWARD, PciFdoSetPowerState},
25 {IRP_DOWNWARD, PciFdoIrpQueryPower},
26 {IRP_DOWNWARD, PciIrpNotSupported}
27 };
28
29 PCI_MN_DISPATCH_TABLE PciFdoDispatchPnpTable[] =
30 {
31 {IRP_UPWARD, PciFdoIrpStartDevice},
32 {IRP_DOWNWARD, PciFdoIrpQueryRemoveDevice},
33 {IRP_DISPATCH, PciFdoIrpRemoveDevice},
34 {IRP_DOWNWARD, PciFdoIrpCancelRemoveDevice},
35 {IRP_DOWNWARD, PciFdoIrpStopDevice},
36 {IRP_DOWNWARD, PciFdoIrpQueryStopDevice},
37 {IRP_DOWNWARD, PciFdoIrpCancelStopDevice},
38 {IRP_DOWNWARD, PciFdoIrpQueryDeviceRelations},
39 {IRP_DISPATCH, PciFdoIrpQueryInterface},
40 {IRP_UPWARD, PciFdoIrpQueryCapabilities},
41 {IRP_DOWNWARD, PciIrpNotSupported},
42 {IRP_DOWNWARD, PciIrpNotSupported},
43 {IRP_DOWNWARD, PciIrpNotSupported},
44 {IRP_DOWNWARD, PciIrpNotSupported},
45 {IRP_DOWNWARD, PciIrpNotSupported},
46 {IRP_DOWNWARD, PciIrpNotSupported},
47 {IRP_DOWNWARD, PciIrpNotSupported},
48 {IRP_DOWNWARD, PciIrpNotSupported},
49 {IRP_DOWNWARD, PciIrpNotSupported},
50 {IRP_DOWNWARD, PciIrpNotSupported},
51 {IRP_DOWNWARD, PciIrpNotSupported},
52 {IRP_DOWNWARD, PciIrpNotSupported},
53 {IRP_UPWARD, PciFdoIrpDeviceUsageNotification},
54 {IRP_DOWNWARD, PciFdoIrpSurpriseRemoval},
55 {IRP_DOWNWARD, PciFdoIrpQueryLegacyBusInformation},
56 {IRP_DOWNWARD, PciIrpNotSupported}
57 };
58
59 PCI_MJ_DISPATCH_TABLE PciFdoDispatchTable =
60 {
61 IRP_MN_QUERY_LEGACY_BUS_INFORMATION,
62 PciFdoDispatchPnpTable,
63 IRP_MN_QUERY_POWER,
64 PciFdoDispatchPowerTable,
65 IRP_DOWNWARD,
66 PciIrpNotSupported,
67 IRP_DOWNWARD,
68 PciIrpNotSupported
69 };
70
71 /* FUNCTIONS ******************************************************************/
72
73 NTSTATUS
74 NTAPI
75 PciFdoIrpStartDevice(IN PIRP Irp,
76 IN PIO_STACK_LOCATION IoStackLocation,
77 IN PPCI_FDO_EXTENSION DeviceExtension)
78 {
79 NTSTATUS Status;
80 PCM_RESOURCE_LIST Resources;
81 PAGED_CODE();
82
83 /* The device stack must be starting the FDO in a success path */
84 if (!NT_SUCCESS(Irp->IoStatus.Status)) return STATUS_NOT_SUPPORTED;
85
86 /* Attempt to switch the state machine to the started state */
87 Status = PciBeginStateTransition(DeviceExtension, PciStarted);
88 if (!NT_SUCCESS(Status)) return Status;
89
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)))
94 {
95 /* These resources would only be for non-root FDOs, unhandled for now */
96 ASSERT(Resources->Count == 1);
97 UNIMPLEMENTED;
98 while (TRUE);
99 }
100
101 /* Initialize the arbiter for this FDO */
102 Status = PciInitializeArbiterRanges(DeviceExtension, Resources);
103 if (!NT_SUCCESS(Status))
104 {
105 /* Cancel the transition if this failed */
106 PciCancelStateTransition(DeviceExtension, PciStarted);
107 return Status;
108 }
109
110 /* Again, check for boot-provided resources for non-root FDO */
111 if ((Resources) && !(PCI_IS_ROOT_FDO(DeviceExtension)))
112 {
113 /* Unhandled for now */
114 ASSERT(Resources->Count == 1);
115 UNIMPLEMENTED;
116 while (TRUE);
117 }
118
119 /* Commit the transition to the started state */
120 PciCommitStateTransition(DeviceExtension, PciStarted);
121 return STATUS_SUCCESS;
122 }
123
124 NTSTATUS
125 NTAPI
126 PciFdoIrpQueryRemoveDevice(IN PIRP Irp,
127 IN PIO_STACK_LOCATION IoStackLocation,
128 IN PPCI_FDO_EXTENSION DeviceExtension)
129 {
130 UNIMPLEMENTED;
131 while (TRUE);
132 return STATUS_NOT_SUPPORTED;
133 }
134
135 NTSTATUS
136 NTAPI
137 PciFdoIrpRemoveDevice(IN PIRP Irp,
138 IN PIO_STACK_LOCATION IoStackLocation,
139 IN PPCI_FDO_EXTENSION DeviceExtension)
140 {
141 UNIMPLEMENTED;
142 while (TRUE);
143 return STATUS_NOT_SUPPORTED;
144 }
145
146 NTSTATUS
147 NTAPI
148 PciFdoIrpCancelRemoveDevice(IN PIRP Irp,
149 IN PIO_STACK_LOCATION IoStackLocation,
150 IN PPCI_FDO_EXTENSION DeviceExtension)
151 {
152 UNIMPLEMENTED;
153 while (TRUE);
154 return STATUS_NOT_SUPPORTED;
155 }
156
157 NTSTATUS
158 NTAPI
159 PciFdoIrpStopDevice(IN PIRP Irp,
160 IN PIO_STACK_LOCATION IoStackLocation,
161 IN PPCI_FDO_EXTENSION DeviceExtension)
162 {
163 UNIMPLEMENTED;
164 while (TRUE);
165 return STATUS_NOT_SUPPORTED;
166 }
167
168 NTSTATUS
169 NTAPI
170 PciFdoIrpQueryStopDevice(IN PIRP Irp,
171 IN PIO_STACK_LOCATION IoStackLocation,
172 IN PPCI_FDO_EXTENSION DeviceExtension)
173 {
174 UNIMPLEMENTED;
175 while (TRUE);
176 return STATUS_NOT_SUPPORTED;
177 }
178
179 NTSTATUS
180 NTAPI
181 PciFdoIrpCancelStopDevice(IN PIRP Irp,
182 IN PIO_STACK_LOCATION IoStackLocation,
183 IN PPCI_FDO_EXTENSION DeviceExtension)
184 {
185 UNIMPLEMENTED;
186 while (TRUE);
187 return STATUS_NOT_SUPPORTED;
188 }
189
190 NTSTATUS
191 NTAPI
192 PciFdoIrpQueryDeviceRelations(IN PIRP Irp,
193 IN PIO_STACK_LOCATION IoStackLocation,
194 IN PPCI_FDO_EXTENSION DeviceExtension)
195 {
196 NTSTATUS Status;
197 PAGED_CODE();
198
199 /* Are bus relations being queried? */
200 if (IoStackLocation->Parameters.QueryDeviceRelations.Type != BusRelations)
201 {
202 /* The FDO is a bus, so only bus relations can be obtained */
203 Status = STATUS_NOT_SUPPORTED;
204 }
205 else
206 {
207 /* Scan the PCI bus and build the device relations for the caller */
208 Status = PciQueryDeviceRelations(DeviceExtension,
209 (PDEVICE_RELATIONS*)
210 &Irp->IoStatus.Information);
211 }
212
213 /* Return the enumeration status back */
214 return Status;
215 }
216
217 NTSTATUS
218 NTAPI
219 PciFdoIrpQueryInterface(IN PIRP Irp,
220 IN PIO_STACK_LOCATION IoStackLocation,
221 IN PPCI_FDO_EXTENSION DeviceExtension)
222 {
223 NTSTATUS Status;
224 PAGED_CODE();
225 ASSERT(DeviceExtension->ExtensionType == PciFdoExtensionType);
226
227 /* Deleted extensions don't respond to IRPs */
228 if (DeviceExtension->DeviceState == PciDeleted)
229 {
230 /* Hand it bacO try to deal with it */
231 return PciPassIrpFromFdoToPdo(DeviceExtension, Irp);
232 }
233
234 /* Query our driver for this interface */
235 Status = PciQueryInterface(DeviceExtension,
236 IoStackLocation->Parameters.QueryInterface.
237 InterfaceType,
238 IoStackLocation->Parameters.QueryInterface.
239 Size,
240 IoStackLocation->Parameters.QueryInterface.
241 Version,
242 IoStackLocation->Parameters.QueryInterface.
243 InterfaceSpecificData,
244 IoStackLocation->Parameters.QueryInterface.
245 Interface,
246 FALSE);
247 if (NT_SUCCESS(Status))
248 {
249 /* We found it, let the PDO handle it */
250 Irp->IoStatus.Status = Status;
251 return PciPassIrpFromFdoToPdo(DeviceExtension, Irp);
252 }
253 else if (Status == STATUS_NOT_SUPPORTED)
254 {
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)
258 {
259 /* They can't either, try a last-resort interface lookup */
260 Status = PciQueryInterface(DeviceExtension,
261 IoStackLocation->Parameters.QueryInterface.
262 InterfaceType,
263 IoStackLocation->Parameters.QueryInterface.
264 Size,
265 IoStackLocation->Parameters.QueryInterface.
266 Version,
267 IoStackLocation->Parameters.QueryInterface.
268 InterfaceSpecificData,
269 IoStackLocation->Parameters.QueryInterface.
270 Interface,
271 TRUE);
272 }
273 }
274
275 /* Has anyone claimed this interface yet? */
276 if (Status == STATUS_NOT_SUPPORTED)
277 {
278 /* No, return the original IRP status */
279 Status = Irp->IoStatus.Status;
280 }
281 else
282 {
283 /* Yes, set the new IRP status */
284 Irp->IoStatus.Status = Status;
285 }
286
287 /* Complete this IRP */
288 IoCompleteRequest(Irp, IO_NO_INCREMENT);
289 return Status;
290 }
291
292 NTSTATUS
293 NTAPI
294 PciFdoIrpQueryCapabilities(IN PIRP Irp,
295 IN PIO_STACK_LOCATION IoStackLocation,
296 IN PPCI_FDO_EXTENSION DeviceExtension)
297 {
298 UNIMPLEMENTED;
299 while (TRUE);
300 return STATUS_NOT_SUPPORTED;
301 }
302
303 NTSTATUS
304 NTAPI
305 PciFdoIrpDeviceUsageNotification(IN PIRP Irp,
306 IN PIO_STACK_LOCATION IoStackLocation,
307 IN PPCI_FDO_EXTENSION DeviceExtension)
308 {
309 UNIMPLEMENTED;
310 while (TRUE);
311 return STATUS_NOT_SUPPORTED;
312 }
313
314 NTSTATUS
315 NTAPI
316 PciFdoIrpSurpriseRemoval(IN PIRP Irp,
317 IN PIO_STACK_LOCATION IoStackLocation,
318 IN PPCI_FDO_EXTENSION DeviceExtension)
319 {
320 UNIMPLEMENTED;
321 while (TRUE);
322 return STATUS_NOT_SUPPORTED;
323 }
324
325 NTSTATUS
326 NTAPI
327 PciFdoIrpQueryLegacyBusInformation(IN PIRP Irp,
328 IN PIO_STACK_LOCATION IoStackLocation,
329 IN PPCI_FDO_EXTENSION DeviceExtension)
330 {
331 UNIMPLEMENTED;
332 while (TRUE);
333 return STATUS_NOT_SUPPORTED;
334 }
335
336 VOID
337 NTAPI
338 PciGetHotPlugParameters(IN PPCI_FDO_EXTENSION FdoExtension)
339 {
340 ACPI_EVAL_INPUT_BUFFER InputBuffer;
341 PACPI_EVAL_OUTPUT_BUFFER OutputBuffer;
342 ULONG Length;
343 NTSTATUS Status;
344 PAGED_CODE();
345
346 /* We should receive 4 parameters, per the HPP specification */
347 Length = sizeof(ACPI_EVAL_OUTPUT_BUFFER) + 4 * sizeof(ACPI_METHOD_ARGUMENT);
348
349 /* Allocate the buffer to hold the parameters */
350 OutputBuffer = ExAllocatePoolWithTag(PagedPool, Length, PCI_POOL_TAG);
351 if (!OutputBuffer) return;
352
353 /* Initialize the output and input buffers. The method is _HPP */
354 RtlZeroMemory(OutputBuffer, Length);
355 *(PULONG)InputBuffer.MethodName = 'PPH_';
356 InputBuffer.Signature = ACPI_EVAL_INPUT_BUFFER_SIGNATURE;
357 do
358 {
359 /* Send the IOCTL to the ACPI driver */
360 Status = PciSendIoctl(FdoExtension->PhysicalDeviceObject,
361 IOCTL_ACPI_EVAL_METHOD,
362 &InputBuffer,
363 sizeof(InputBuffer),
364 OutputBuffer,
365 Length);
366 if (!NT_SUCCESS(Status))
367 {
368 /* The method failed, check if we can salvage data from parent */
369 if (!PCI_IS_ROOT_FDO(FdoExtension))
370 {
371 /* Copy the root bus' hot plug parameters */
372 FdoExtension->HotPlugParameters = FdoExtension->ParentFdoExtension->HotPlugParameters;
373 }
374
375 /* Nothing more to do on this path */
376 break;
377 }
378
379 /* ACPI sent back some data. 4 parameters are expected in the output */
380 if (OutputBuffer->Count != 4) break;
381
382 /* HotPlug PCI Support not yet implemented */
383 UNIMPLEMENTED;
384 while (TRUE);
385 } while (FALSE);
386
387 /* Free the buffer and return */
388 ExFreePoolWithTag(OutputBuffer, 0);
389 }
390
391 VOID
392 NTAPI
393 PciInitializeFdoExtensionCommonFields(PPCI_FDO_EXTENSION FdoExtension,
394 IN PDEVICE_OBJECT DeviceObject,
395 IN PDEVICE_OBJECT PhysicalDeviceObject)
396 {
397 /* Initialize the extension */
398 RtlZeroMemory(FdoExtension, sizeof(PCI_FDO_EXTENSION));
399
400 /* Setup the common fields */
401 FdoExtension->PhysicalDeviceObject = PhysicalDeviceObject;
402 FdoExtension->FunctionalDeviceObject = DeviceObject;
403 FdoExtension->ExtensionType = PciFdoExtensionType;
404 FdoExtension->PowerState.CurrentSystemState = PowerSystemWorking;
405 FdoExtension->PowerState.CurrentDeviceState = PowerDeviceD0;
406 FdoExtension->IrpDispatchTable = &PciFdoDispatchTable;
407
408 /* Initialize the extension locks */
409 KeInitializeEvent(&FdoExtension->SecondaryExtLock, SynchronizationEvent, TRUE);
410 KeInitializeEvent(&FdoExtension->ChildListLock, SynchronizationEvent, TRUE);
411
412 /* Initialize the default state */
413 PciInitializeState(FdoExtension);
414 }
415
416 NTSTATUS
417 NTAPI
418 PciAddDevice(IN PDRIVER_OBJECT DriverObject,
419 IN PDEVICE_OBJECT PhysicalDeviceObject)
420 {
421 PCM_RESOURCE_LIST Descriptor;
422 PDEVICE_OBJECT AttachedTo;
423 PPCI_FDO_EXTENSION FdoExtension;
424 PPCI_FDO_EXTENSION ParentExtension;
425 PDEVICE_OBJECT DeviceObject;
426 UCHAR Buffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG)];
427 PKEY_VALUE_PARTIAL_INFORMATION ValueInfo = (PKEY_VALUE_PARTIAL_INFORMATION)Buffer;
428 NTSTATUS Status;
429 HANDLE KeyHandle;
430 UNICODE_STRING ValueName;
431 ULONG ResultLength;
432 PAGED_CODE();
433 DPRINT1("PCI - AddDevice (a new bus). PDO: %p (Driver: %wZ)\n",
434 PhysicalDeviceObject, &PhysicalDeviceObject->DriverObject->DriverName);
435
436 /* Zero out variables so failure path knows what to do */
437 AttachedTo = NULL;
438 do
439 {
440 /* Check if there's already a device extension for this bus */
441 ParentExtension = PciFindParentPciFdoExtension(PhysicalDeviceObject,
442 &PciGlobalLock);
443 if (ParentExtension)
444 {
445 /* More than one PCI bus, this is not expected yet */
446 UNIMPLEMENTED;
447 while (TRUE);
448 }
449
450 /* Create the FDO for the bus */
451 Status = IoCreateDevice(DriverObject,
452 sizeof(PCI_FDO_EXTENSION),
453 NULL,
454 FILE_DEVICE_BUS_EXTENDER,
455 0,
456 0,
457 &DeviceObject);
458 if (!NT_SUCCESS(Status)) break;
459
460 /* Initialize the extension for the FDO */
461 FdoExtension = DeviceObject->DeviceExtension;
462 PciInitializeFdoExtensionCommonFields(DeviceObject->DeviceExtension,
463 DeviceObject,
464 PhysicalDeviceObject);
465
466 /* Attach to the root PDO */
467 Status = STATUS_NO_SUCH_DEVICE;
468 AttachedTo = IoAttachDeviceToDeviceStack(DeviceObject,
469 PhysicalDeviceObject);
470 ASSERT(AttachedTo != NULL);
471 if (!AttachedTo) break;
472 FdoExtension->AttachedDeviceObject = AttachedTo;
473 if (ParentExtension)
474 {
475 /* More than one PCI bus, this is not expected yet */
476 UNIMPLEMENTED;
477 while (TRUE);
478 }
479 else
480 {
481 /* Query the boot configuration */
482 Status = PciGetDeviceProperty(PhysicalDeviceObject,
483 DevicePropertyBootConfiguration,
484 (PVOID*)&Descriptor);
485 if (!NT_SUCCESS(Status))
486 {
487 /* No configuration has been set */
488 Descriptor = NULL;
489 }
490 else
491 {
492 /* Root PDO in ReactOS does not assign boot resources */
493 UNIMPLEMENTED;
494 while (TRUE);
495 }
496
497 if (Descriptor)
498 {
499 /* Root PDO in ReactOS does not assign boot resources */
500 UNIMPLEMENTED;
501 while (TRUE);
502 }
503 else
504 {
505 /* Default configuration isn't the normal path on Windows */
506 if (PciBreakOnDefault)
507 {
508 /* If a second bus is found and there's still no data, crash */
509 KeBugCheckEx(PCI_BUS_DRIVER_INTERNAL,
510 0xDEAD0010u,
511 (ULONG_PTR)DeviceObject,
512 0,
513 0);
514 }
515
516 /* Warn that a default configuration will be used, and set bus 0 */
517 DPRINT1("PCI Will use default configuration.\n");
518 PciBreakOnDefault = TRUE;
519 FdoExtension->BaseBus = 0;
520 }
521
522 /* This is the root bus */
523 FdoExtension->BusRootFdoExtension = FdoExtension;
524 }
525
526 /* Get the HAL or ACPI Bus Handler Callbacks for Configuration Access */
527 Status = PciGetConfigHandlers(FdoExtension);
528 if (!NT_SUCCESS(Status)) break;
529
530 /* Initialize all the supported PCI arbiters */
531 Status = PciInitializeArbiters(FdoExtension);
532 if (!NT_SUCCESS(Status)) break;
533
534 /* This is a real FDO, insert it into the list */
535 FdoExtension->Fake = FALSE;
536 PciInsertEntryAtTail(&PciFdoExtensionListHead,
537 FdoExtension,
538 &PciGlobalLock);
539
540 /* Open the device registry key so that we can query the errata flags */
541 IoOpenDeviceRegistryKey(DeviceObject,
542 PLUGPLAY_REGKEY_DEVICE,
543 KEY_ALL_ACCESS,
544 &KeyHandle),
545
546 /* Open the value that contains errata flags for this bus instance */
547 RtlInitUnicodeString(&ValueName, L"HackFlags");
548 Status = ZwQueryValueKey(KeyHandle,
549 &ValueName,
550 KeyValuePartialInformation,
551 ValueInfo,
552 sizeof(Buffer),
553 &ResultLength);
554 ZwClose(KeyHandle);
555 if (NT_SUCCESS(Status))
556 {
557 /* Make sure the data is of expected type and size */
558 if ((ValueInfo->Type == REG_DWORD) &&
559 (ValueInfo->DataLength == sizeof(ULONG)))
560 {
561 /* Read the flags for this bus */
562 FdoExtension->BusHackFlags = *(PULONG)&ValueInfo->Data;
563 }
564 }
565
566 /* Query ACPI for PCI HotPlug Support */
567 PciGetHotPlugParameters(FdoExtension);
568
569 /* The Bus FDO is now initialized */
570 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
571 DPRINT1("PCI Root FDO Added: %p %p\n", DeviceObject, FdoExtension);
572 return STATUS_SUCCESS;
573 } while (FALSE);
574
575 /* This is the failure path */
576 ASSERT(!NT_SUCCESS(Status));
577 if (AttachedTo) IoDetachDevice(AttachedTo);
578 if (DeviceObject) IoDeleteDevice(DeviceObject);
579 return Status;
580 }
581
582 /* EOF */