2 Data Hub status code worker.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "StatusCodeRuntimeDxe.h"
12 // Initialize FIFO to cache records.
14 LIST_ENTRY mRecordsFifo
= INITIALIZE_LIST_HEAD_VARIABLE (mRecordsFifo
);
15 LIST_ENTRY mRecordsBuffer
= INITIALIZE_LIST_HEAD_VARIABLE (mRecordsBuffer
);
16 UINT32 mLogDataHubStatus
= 0;
17 EFI_EVENT mLogDataHubEvent
;
19 // Cache data hub protocol.
21 EFI_DATA_HUB_PROTOCOL
*mDataHubProtocol
= NULL
;
25 Retrieve one record of from free record buffer. This record is removed from
28 This function retrieves one record from free record buffer.
29 If the pool has been exhausted, then new memory would be allocated for it.
31 @return Pointer to the free record.
32 NULL means failure to allocate new memeory for free record buffer.
35 DATA_HUB_STATUS_CODE_DATA_RECORD
*
40 DATAHUB_STATUSCODE_RECORD
*Record
;
45 CurrentTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
47 if (!IsListEmpty (&mRecordsBuffer
)) {
49 // Strip one entry from free record buffer.
51 Node
= GetFirstNode (&mRecordsBuffer
);
52 RemoveEntryList (Node
);
54 Record
= BASE_CR (Node
, DATAHUB_STATUSCODE_RECORD
, Node
);
56 if (CurrentTpl
> TPL_NOTIFY
) {
58 // Memory management should work at <=TPL_NOTIFY
60 gBS
->RestoreTPL (CurrentTpl
);
65 // If free record buffer is exhausted, then allocate 16 new records for it.
67 gBS
->RestoreTPL (CurrentTpl
);
68 Record
= (DATAHUB_STATUSCODE_RECORD
*) AllocateZeroPool (sizeof (DATAHUB_STATUSCODE_RECORD
) * 16);
73 CurrentTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
75 // Here we only insert 15 new records to the free record buffer, for the first record
76 // will be returned immediately.
78 for (Index
= 1; Index
< 16; Index
++) {
79 InsertTailList (&mRecordsBuffer
, &Record
[Index
].Node
);
83 Record
->Signature
= DATAHUB_STATUS_CODE_SIGNATURE
;
84 InsertTailList (&mRecordsFifo
, &Record
->Node
);
86 gBS
->RestoreTPL (CurrentTpl
);
88 return (DATA_HUB_STATUS_CODE_DATA_RECORD
*) (Record
->Data
);
93 Retrieve one record from Records FIFO. The record would be removed from FIFO.
95 @return Point to record, which is ready to be logged.
96 NULL means the FIFO of record is empty.
99 DATA_HUB_STATUS_CODE_DATA_RECORD
*
104 DATA_HUB_STATUS_CODE_DATA_RECORD
*RecordData
;
105 DATAHUB_STATUSCODE_RECORD
*Record
;
111 CurrentTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
113 if (!IsListEmpty (&mRecordsFifo
)) {
114 Node
= GetFirstNode (&mRecordsFifo
);
115 Record
= CR (Node
, DATAHUB_STATUSCODE_RECORD
, Node
, DATAHUB_STATUS_CODE_SIGNATURE
);
116 ASSERT (Record
!= NULL
);
118 RemoveEntryList (&Record
->Node
);
119 RecordData
= (DATA_HUB_STATUS_CODE_DATA_RECORD
*) Record
->Data
;
122 gBS
->RestoreTPL (CurrentTpl
);
128 Release given record and return it to free record buffer.
130 @param RecordData Pointer to the record to release.
135 DATA_HUB_STATUS_CODE_DATA_RECORD
*RecordData
138 DATAHUB_STATUSCODE_RECORD
*Record
;
141 Record
= CR (RecordData
, DATAHUB_STATUSCODE_RECORD
, Data
[0], DATAHUB_STATUS_CODE_SIGNATURE
);
142 ASSERT (Record
!= NULL
);
144 CurrentTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
146 InsertTailList (&mRecordsBuffer
, &Record
->Node
);
147 Record
->Signature
= 0;
149 gBS
->RestoreTPL (CurrentTpl
);
153 Report status code into DataHub.
155 @param CodeType Indicates the type of status code being reported.
156 @param Value Describes the current status of a hardware or software entity.
157 This included information about the class and subclass that is used to
158 classify the entity as well as an operation.
159 @param Instance The enumeration of a hardware or software entity within
160 the system. Valid instance numbers start with 1.
161 @param CallerId This optional parameter may be used to identify the caller.
162 This parameter allows the status code driver to apply different rules to
164 @param Data This optional parameter may be used to pass additional data.
166 @retval EFI_SUCCESS The function completed successfully.
167 @retval EFI_DEVICE_ERROR Function is reentered.
168 @retval EFI_DEVICE_ERROR Function is called at runtime.
169 @retval EFI_OUT_OF_RESOURCES Fail to allocate memory for free record buffer.
173 DataHubStatusCodeReportWorker (
174 IN EFI_STATUS_CODE_TYPE CodeType
,
175 IN EFI_STATUS_CODE_VALUE Value
,
177 IN EFI_GUID
*CallerId
,
178 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
181 DATA_HUB_STATUS_CODE_DATA_RECORD
*Record
;
189 // Use atom operation to avoid the reentant of report.
190 // If current status is not zero, then the function is reentrancy.
192 if (InterlockedCompareExchange32 (&mLogDataHubStatus
, 0, 0) == 1) {
193 return EFI_DEVICE_ERROR
;
197 // See whether in runtime phase or not.
199 if (EfiAtRuntime ()) {
200 return EFI_DEVICE_ERROR
;
203 if (mDataHubProtocol
== NULL
) {
204 Status
= DataHubStatusCodeInitializeWorker ();
205 if (EFI_ERROR (Status
)) {
210 Record
= AcquireRecordBuffer ();
211 if (Record
== NULL
) {
213 // There are no empty record buffer in private buffers
215 return EFI_OUT_OF_RESOURCES
;
219 // Construct Data Hub Extended Data
221 Record
->CodeType
= CodeType
;
222 Record
->Value
= Value
;
223 Record
->Instance
= Instance
;
225 if (CallerId
!= NULL
) {
226 CopyMem (&Record
->CallerId
, CallerId
, sizeof (EFI_GUID
));
230 if (ReportStatusCodeExtractDebugInfo (Data
, &ErrorLevel
, &Marker
, &Format
)) {
231 CharCount
= UnicodeBSPrintAsciiFormat (
232 (CHAR16
*) (Record
+ 1),
233 EFI_STATUS_CODE_DATA_MAX_SIZE
,
238 // Change record data type to DebugType.
240 CopyGuid (&Record
->Data
.Type
, &gEfiStatusCodeDataTypeDebugGuid
);
241 Record
->Data
.HeaderSize
= Data
->HeaderSize
;
242 Record
->Data
.Size
= (UINT16
) ((CharCount
+ 1) * sizeof (CHAR16
));
245 // Copy status code data header
247 CopyMem (&Record
->Data
, Data
, sizeof (EFI_STATUS_CODE_DATA
));
249 if (Data
->Size
> EFI_STATUS_CODE_DATA_MAX_SIZE
) {
250 Record
->Data
.Size
= EFI_STATUS_CODE_DATA_MAX_SIZE
;
252 CopyMem ((VOID
*) (Record
+ 1), Data
+ 1, Record
->Data
.Size
);
256 gBS
->SignalEvent (mLogDataHubEvent
);
263 The Event handler which will be notified to log data in Data Hub.
265 @param Event Instance of the EFI_EVENT to signal whenever data is
266 available to be logged in the system.
267 @param Context Context of the event.
272 LogDataHubEventCallBack (
277 DATA_HUB_STATUS_CODE_DATA_RECORD
*Record
;
279 UINT64 DataRecordClass
;
282 // Use atom operation to avoid the reentant of report.
283 // If current status is not zero, then the function is reentrancy.
285 if (InterlockedCompareExchange32 (&mLogDataHubStatus
, 0, 1) == 1) {
290 // Log DataRecord in Data Hub.
291 // Journal records fifo to find all record entry.
295 // Retrieve record from record FIFO until no more record can be retrieved.
297 Record
= RetrieveRecord ();
298 if (Record
== NULL
) {
302 // Add in the size of the header we added.
304 Size
= sizeof (DATA_HUB_STATUS_CODE_DATA_RECORD
) + (UINT32
) Record
->Data
.Size
;
306 if ((Record
->CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) {
307 DataRecordClass
= EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
308 } else if ((Record
->CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) {
309 DataRecordClass
= EFI_DATA_RECORD_CLASS_ERROR
;
310 } else if ((Record
->CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
) {
311 DataRecordClass
= EFI_DATA_RECORD_CLASS_DEBUG
;
314 // Should never get here.
316 DataRecordClass
= EFI_DATA_RECORD_CLASS_DEBUG
|
317 EFI_DATA_RECORD_CLASS_ERROR
|
318 EFI_DATA_RECORD_CLASS_DATA
|
319 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
323 // Log DataRecord in Data Hub
325 mDataHubProtocol
->LogData (
327 &gEfiDataHubStatusCodeRecordGuid
,
328 &gEfiStatusCodeRuntimeProtocolGuid
,
334 ReleaseRecord (Record
);
338 // Restore the nest status of report
340 InterlockedCompareExchange32 (&mLogDataHubStatus
, 1, 0);
345 Locate Data Hub Protocol and create event for logging data
346 as initialization for data hub status code worker.
348 @retval EFI_SUCCESS Initialization is successful.
352 DataHubStatusCodeInitializeWorker (
358 Status
= gBS
->LocateProtocol (
359 &gEfiDataHubProtocolGuid
,
361 (VOID
**) &mDataHubProtocol
363 if (EFI_ERROR (Status
)) {
364 mDataHubProtocol
= NULL
;
369 // Create a Notify Event to log data in Data Hub
371 Status
= gBS
->CreateEvent (
374 LogDataHubEventCallBack
,
379 ASSERT_EFI_ERROR (Status
);