1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/*++
Module Name:
Alert.c
Abstract:
This file contains functions that handle alerts from the port controller hardware.
Environment:
Kernel-mode Driver Framework
--*/
#include "Driver.h"
#include "alert.tmh"
#ifdef ALLOC_PRAGMA
#pragma alloc_text (PAGE, OnInterruptPassiveIsr)
#pragma alloc_text (PAGE, ProcessAndSendAlerts)
#endif
BOOLEAN
OnInterruptPassiveIsr(
_In_ WDFINTERRUPT PortControllerInterrupt,
_In_ ULONG MessageID
)
/*++
Routine Description:
Per the TCPCI spec, the port controller hardware will drive the Alert pin high
when a hardware event occurs. This routine services such a hardware interrupt at PASSIVE_LEVEL.
The routine determines if an interrupt is an alert from the port controller hardware;
if so, it completes processing of the alert.
Arguments:
Interrupt: A handle to a framework interrupt object.
MessageID: If the device is using message-signaled interrupts (MSIs), this parameter
is the message number that identifies the device's hardware interrupt message.
Otherwise, this value is 0.
Return Value:
TRUE if the function services the hardware interrupt.
Otherwise, this function must return FALSE.
--*/
{
TRACE_FUNC_ENTRY(TRACE_ALERT);
UNREFERENCED_PARAMETER(MessageID);
PAGED_CODE();
NTSTATUS status;
PDEVICE_CONTEXT deviceContext;
ALERT_REGISTER alertRegister;
BOOLEAN interruptRecognized = FALSE;
int numAlertsProcessed = 0;
deviceContext = DeviceGetContext(WdfInterruptGetDevice(PortControllerInterrupt));
// Process the alerts as long as there are bits set in the alert register.
// Set a maximum number of alerts to process in this loop. If the hardware is messed up and we're unable
// to quiesce the interrupt by writing to the alert register, then we don't want to be stuck in an
// infinite loop.
while (numAlertsProcessed <= MAX_ALERTS_TO_PROCESS)
{
status = I2CReadSynchronously(deviceContext,
I2CRequestSourceAlertIsr,
ALERT,
&alertRegister,
sizeof(alertRegister));
if (!NT_SUCCESS(status))
{
goto Exit;
}
// If there are no bits set in the alert register, we should not service this interrupt.
if (alertRegister.AsUInt16 == 0)
{
goto Exit;
}
// Since there are bits set in the alert register, we can safely assume that the
// interrupt is ours to process.
interruptRecognized = TRUE;
ProcessAndSendAlerts(&alertRegister, deviceContext);
++numAlertsProcessed;
}
Exit:
TRACE_FUNC_EXIT(TRACE_ALERT);
return interruptRecognized;
}
void
ProcessAndSendAlerts(
_In_ PALERT_REGISTER AlertRegister,
_In_ PDEVICE_CONTEXT DeviceContext
)
/*++
Routine Description:
Processes the set of hardware alerts that were reported and notifies UcmTcpciCx of the alerts
along with the contents of relevant registers.
Arguments:
AlertRegister: Pointer to alert register contents.
DeviceContext: Device's context space.
--*/
{
TRACE_FUNC_ENTRY(TRACE_ALERT);
PAGED_CODE();
NTSTATUS status;
size_t numAlerts = 0;
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA alertData;
UCMTCPCI_PORT_CONTROLLER_CC_STATUS ccStatus;
UCMTCPCI_PORT_CONTROLLER_POWER_STATUS powerStatus;
UCMTCPCI_PORT_CONTROLLER_FAULT_STATUS faultStatus;
UCMTCPCI_PORT_CONTROLLER_RECEIVE_BUFFER receiveBuffer;
// UcmTcpciCx expects the information on all of the alerts firing presently.
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA hardwareAlerts[MAX_ALERTS];
status = STATUS_SUCCESS;
if (AlertRegister->CCStatus == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertCCStatus;
// We must read the CC status register and send the contents to
// UcmTcpciCx along with the CC status alert.
status = I2CReadSynchronously(DeviceContext,
I2CRequestSourceAlertIsr,
CC_STATUS,
&ccStatus,
sizeof(ccStatus));
if (!NT_SUCCESS(status))
{
goto Exit;
}
alertData.CCStatus = ccStatus;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
}
if (AlertRegister->PowerStatus == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertPowerStatus;
// We must read the power status register and send the contents to
// UcmTcpciCx along with the power status alert.
status = I2CReadSynchronously(DeviceContext,
I2CRequestSourceAlertIsr,
POWER_STATUS,
&powerStatus,
sizeof(powerStatus));
if (!NT_SUCCESS(status))
{
goto Exit;
}
alertData.PowerStatus = powerStatus;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
}
if (AlertRegister->Fault == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertFault;
// We must read the fault status register and send the contents to
// UcmTcpciCx along with the fault alert.
status = I2CReadSynchronously(DeviceContext,
I2CRequestSourceAlertIsr,
FAULT_STATUS,
&faultStatus,
sizeof(faultStatus));
if (!NT_SUCCESS(status))
{
goto Exit;
}
alertData.FaultStatus = faultStatus;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
// Clear FAULT_STATUS Register.
// Mask reserved bit 7 in TCPCI Rev 1.0 Ver 1.0 only, see spec section 4.4.6.3
faultStatus.AsUInt8 &= 0x7F;
status = I2CWriteSynchronously(DeviceContext,
I2CRequestSourceAlertIsr,
FAULT_STATUS,
&faultStatus,
sizeof(faultStatus));
if (!NT_SUCCESS(status))
{
goto Exit;
}
}
if (AlertRegister->ReceiveSOPMessageStatus == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertReceiveSOPMessageStatus;
// We must read the receive buffer register and send the contents to
// UcmTcpciCx along with the receive SOP alert.
status = I2CReadSynchronously(DeviceContext,
I2CRequestSourceAlertIsr,
RECEIVE_BUFFER,
&receiveBuffer,
sizeof(receiveBuffer));
if (!NT_SUCCESS(status))
{
goto Exit;
}
alertData.ReceiveBuffer = &receiveBuffer;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
}
// The remainder of the alert types do not require us to provide any extra
// information to UcmTcpciCx.
if (AlertRegister->ReceivedHardReset == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertReceivedHardReset;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
}
if (AlertRegister->RxBufferOverflow == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertRxBufferOverflow;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
}
if (AlertRegister->TransmitSOPMessageDiscarded == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertTransmitSOPMessageDiscarded;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
}
if (AlertRegister->TransmitSOPMessageFailed == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertTransmitSOPMessageFailed;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
}
if (AlertRegister->TransmitSOPMessageSuccessful == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertTransmitSOPMessageSuccessful;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
}
if (AlertRegister->VbusSinkDisconnectDetected == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertVbusSinkDisconnectDetected;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
}
if (AlertRegister->VbusVoltageAlarmHi == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertVbusVoltageAlarmHi;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
}
if (AlertRegister->VbusVoltageAlarmLo == 1)
{
UCMTCPCI_PORT_CONTROLLER_ALERT_DATA_INIT(&alertData);
alertData.AlertType = UcmTcpciPortControllerAlertVbusVoltageAlarmLo;
hardwareAlerts[numAlerts] = alertData;
++numAlerts;
}
// Only write back non-reserved bits see spec section 4.4.2
// TCPCI Rev 1.0 Ver 1.0: 0x0FFF
// TCPCI Rev 1.0 Ver 1.1: 0x8FFF
AlertRegister->AsUInt16 &= 0x0FFF;
// Quiesce the interrupt by writing back the alert register.
// Per TCPCI spec 4.4.2, the alert is cleared by writing a 1 back to the bit position it is to clear.
status = I2CWriteSynchronously(DeviceContext,
I2CRequestSourceAlertIsr,
ALERT,
AlertRegister,
sizeof(*AlertRegister));
if (!NT_SUCCESS(status))
{
goto Exit;
}
Exit:
if (NT_SUCCESS(status))
{
// Send the list of hardware alerts to UcmTcpciCx.
UcmTcpciPortControllerAlert(DeviceContext->PortController, hardwareAlerts, numAlerts);
}
TRACE_FUNC_EXIT(TRACE_ALERT);
}
|