2 * PROJECT: ReactOS PCI Bus Driver
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: drivers/bus/pci/pci/state.c
5 * PURPOSE: Bus/Device State Support
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 /* GLOBALS ********************************************************************/
17 PCHAR PciTransitionText
[PciMaxObjectState
+ 1] =
24 "PciSynchronizedOperation",
28 NTSTATUS PnpStateCancelArray
[PciMaxObjectState
] =
30 STATUS_INVALID_DEVICE_REQUEST
,
32 STATUS_INVALID_DEVICE_STATE
,
33 STATUS_INVALID_DEVICE_STATE
,
38 NTSTATUS PnpStateTransitionArray
[PciMaxObjectState
* PciMaxObjectState
] =
40 STATUS_SUCCESS
, // Not Started -> Not Started
41 STATUS_SUCCESS
, // Started -> Not Started
42 STATUS_FAIL_CHECK
, // Deleted -> Not Started
43 STATUS_SUCCESS
, // Stopped -> Not Started
44 STATUS_FAIL_CHECK
, // Surprise Removed -> Not Started
45 STATUS_FAIL_CHECK
, // Synchronized Operation -> Not Started
47 STATUS_SUCCESS
, // Not Started -> Started
48 STATUS_FAIL_CHECK
, // Started -> Started
49 STATUS_FAIL_CHECK
, // Deleted -> Started
50 STATUS_SUCCESS
, // Stopped -> Started
51 STATUS_FAIL_CHECK
, // Surprise Removed -> Started
52 STATUS_FAIL_CHECK
, // Synchronized Operation -> Started
54 STATUS_SUCCESS
, // Not Started -> Deleted
55 STATUS_SUCCESS
, // Started -> Deleted
56 STATUS_FAIL_CHECK
, // Deleted -> Deleted
57 STATUS_FAIL_CHECK
, // Stopped -> Deleted
58 STATUS_SUCCESS
, // Surprise Removed -> Deleted
59 STATUS_FAIL_CHECK
, // Synchronized Operation -> Deleted
61 STATUS_INVALID_DEVICE_REQUEST
, // Not Started -> Stopped
62 STATUS_SUCCESS
, // Started -> Stopped
63 STATUS_FAIL_CHECK
, // Deleted -> Stopped
64 STATUS_FAIL_CHECK
, // Stopped -> Stopped
65 STATUS_FAIL_CHECK
, // Surprise Removed -> Stopped
66 STATUS_FAIL_CHECK
, // Synchronized Operation -> Stopped
68 STATUS_SUCCESS
, // Not Started -> Surprise Removed
69 STATUS_SUCCESS
, // Started -> Surprise Removed
70 STATUS_FAIL_CHECK
, // Deleted -> Surprise Removed
71 STATUS_SUCCESS
, // Stopped -> Surprise Removed
72 STATUS_FAIL_CHECK
, // Surprise Removed -> Surprise Removed
73 STATUS_FAIL_CHECK
, // Synchronized Operation -> Surprise Removed
75 STATUS_SUCCESS
, // Not Started -> Synchronized Operation
76 STATUS_SUCCESS
, // Started -> Synchronized Operation
77 STATUS_INVALID_DEVICE_STATE
, // Deleted -> Synchronized Operation
78 STATUS_SUCCESS
, // Stopped -> Synchronized Operation
79 STATUS_INVALID_DEVICE_STATE
, // Surprise Removed -> Synchronized Operation
80 STATUS_FAIL_CHECK
// Synchronized Operation -> Synchronized Operation
83 /* FUNCTIONS ******************************************************************/
87 PciInitializeState(IN PPCI_FDO_EXTENSION DeviceExtension
)
89 /* Set the initial state */
90 DeviceExtension
->DeviceState
= PciNotStarted
;
91 DeviceExtension
->TentativeNextState
= PciNotStarted
;
96 PciBeginStateTransition(IN PPCI_FDO_EXTENSION DeviceExtension
,
97 IN PCI_STATE NewState
)
99 PCI_STATE CurrentState
;
101 DPRINT1("PCI Request to begin transition of Extension %p to %s ->",
103 PciTransitionText
[NewState
]);
105 /* Assert the device isn't already in a pending transition */
106 ASSERT(DeviceExtension
->TentativeNextState
== DeviceExtension
->DeviceState
);
108 /* Assert this is a valid state */
109 CurrentState
= DeviceExtension
->DeviceState
;
110 ASSERT(CurrentState
< PciMaxObjectState
);
111 ASSERT(NewState
< PciMaxObjectState
);
113 /* Lookup if this state transition is valid */
114 Status
= PnpStateTransitionArray
[CurrentState
+ 6 * NewState
];
115 if (Status
== STATUS_FAIL_CHECK
)
117 /* Invalid transition (logical fault) */
118 DPRINT1("ERROR\nPCI: Error trying to enter state \"%s\" "
119 "from state \"%s\"\n",
120 PciTransitionText
[NewState
],
121 PciTransitionText
[CurrentState
]);
124 else if (Status
== STATUS_INVALID_DEVICE_REQUEST
)
126 /* Invalid transition (illegal request) */
127 DPRINT1("ERROR\nPCI: Illegal request to try to enter state \"%s\" "
128 "from state \"%s\", rejecting",
129 PciTransitionText
[NewState
],
130 PciTransitionText
[CurrentState
]);
133 /* New state must be different from current, unless request is at fault */
134 ASSERT((NewState
!= DeviceExtension
->DeviceState
) || (!NT_SUCCESS(Status
)));
136 /* Enter the new state if successful, and return state status */
137 if (NT_SUCCESS(Status
)) DeviceExtension
->TentativeNextState
= NewState
;
138 DbgPrint("%x\n", Status
);
144 PciCancelStateTransition(IN PPCI_FDO_EXTENSION DeviceExtension
,
145 IN PCI_STATE StateNotEntered
)
148 DPRINT1("PCI Request to cancel transition of Extension %p to %s ->",
150 PciTransitionText
[StateNotEntered
]);
152 /* The next state can't be the state the device is already in */
153 if (DeviceExtension
->TentativeNextState
== DeviceExtension
->DeviceState
)
155 /* It's too late since the state was already committed */
156 ASSERT(StateNotEntered
< PciMaxObjectState
);
157 ASSERT(PnpStateCancelArray
[StateNotEntered
] != STATUS_FAIL_CHECK
);
160 Status
= STATUS_INVALID_DEVICE_STATE
;
161 DbgPrint("%x\n", Status
);
165 /* The device hasn't yet entered the state, so it's still possible to cancel */
166 ASSERT(DeviceExtension
->TentativeNextState
== StateNotEntered
);
167 DeviceExtension
->TentativeNextState
= DeviceExtension
->DeviceState
;
170 Status
= STATUS_SUCCESS
;
171 DbgPrint("%x\n", Status
);
174 /* Return the cancel state */
180 PciCommitStateTransition(IN PPCI_FDO_EXTENSION DeviceExtension
,
181 IN PCI_STATE NewState
)
183 DPRINT1("PCI Commit transition of Extension %p to %s\n",
184 DeviceExtension
, PciTransitionText
[NewState
]);
186 /* Make sure this is a valid commit */
187 ASSERT(NewState
!= PciSynchronizedOperation
);
188 ASSERT(DeviceExtension
->TentativeNextState
== NewState
);
190 /* Enter the new state */
191 DeviceExtension
->DeviceState
= NewState
;