2 Data Hub status code worker.
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "DatahubStatusCodeHandlerDxe.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.
174 DataHubStatusCodeReportWorker (
175 IN EFI_STATUS_CODE_TYPE CodeType
,
176 IN EFI_STATUS_CODE_VALUE Value
,
178 IN EFI_GUID
*CallerId
,
179 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
182 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
;
196 Record
= AcquireRecordBuffer ();
197 if (Record
== NULL
) {
199 // There are no empty record buffer in private buffers
201 return EFI_OUT_OF_RESOURCES
;
205 // Construct Data Hub Extended Data
207 Record
->CodeType
= CodeType
;
208 Record
->Value
= Value
;
209 Record
->Instance
= Instance
;
211 if (CallerId
!= NULL
) {
212 CopyMem (&Record
->CallerId
, CallerId
, sizeof (EFI_GUID
));
216 if (ReportStatusCodeExtractDebugInfo (Data
, &ErrorLevel
, &Marker
, &Format
)) {
217 CharCount
= UnicodeBSPrintAsciiFormat (
218 (CHAR16
*) (Record
+ 1),
219 EFI_STATUS_CODE_DATA_MAX_SIZE
,
224 // Change record data type to DebugType.
226 CopyGuid (&Record
->Data
.Type
, &gEfiStatusCodeDataTypeDebugGuid
);
227 Record
->Data
.HeaderSize
= Data
->HeaderSize
;
228 Record
->Data
.Size
= (UINT16
) ((CharCount
+ 1) * sizeof (CHAR16
));
231 // Copy status code data header
233 CopyMem (&Record
->Data
, Data
, sizeof (EFI_STATUS_CODE_DATA
));
235 if (Data
->Size
> EFI_STATUS_CODE_DATA_MAX_SIZE
) {
236 Record
->Data
.Size
= EFI_STATUS_CODE_DATA_MAX_SIZE
;
238 CopyMem ((VOID
*) (Record
+ 1), Data
+ 1, Record
->Data
.Size
);
242 gBS
->SignalEvent (mLogDataHubEvent
);
249 The Event handler which will be notified to log data in Data Hub.
251 @param Event Instance of the EFI_EVENT to signal whenever data is
252 available to be logged in the system.
253 @param Context Context of the event.
258 LogDataHubEventCallBack (
263 DATA_HUB_STATUS_CODE_DATA_RECORD
*Record
;
265 UINT64 DataRecordClass
;
268 // Use atom operation to avoid the reentant of report.
269 // If current status is not zero, then the function is reentrancy.
271 if (InterlockedCompareExchange32 (&mLogDataHubStatus
, 0, 1) == 1) {
276 // Log DataRecord in Data Hub.
277 // Journal records fifo to find all record entry.
281 // Retrieve record from record FIFO until no more record can be retrieved.
283 Record
= RetrieveRecord ();
284 if (Record
== NULL
) {
288 // Add in the size of the header we added.
290 Size
= sizeof (DATA_HUB_STATUS_CODE_DATA_RECORD
) + (UINT32
) Record
->Data
.Size
;
292 if ((Record
->CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) {
293 DataRecordClass
= EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
294 } else if ((Record
->CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) {
295 DataRecordClass
= EFI_DATA_RECORD_CLASS_ERROR
;
296 } else if ((Record
->CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
) {
297 DataRecordClass
= EFI_DATA_RECORD_CLASS_DEBUG
;
300 // Should never get here.
302 DataRecordClass
= EFI_DATA_RECORD_CLASS_DEBUG
|
303 EFI_DATA_RECORD_CLASS_ERROR
|
304 EFI_DATA_RECORD_CLASS_DATA
|
305 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
309 // Log DataRecord in Data Hub
311 mDataHubProtocol
->LogData (
313 &gEfiDataHubStatusCodeRecordGuid
,
314 &gEfiStatusCodeRuntimeProtocolGuid
,
320 ReleaseRecord (Record
);
324 // Restore the nest status of report
326 InterlockedCompareExchange32 (&mLogDataHubStatus
, 1, 0);
331 Locate Data Hub Protocol and create event for logging data
332 as initialization for data hub status code worker.
334 @retval EFI_SUCCESS Initialization is successful.
338 DataHubStatusCodeInitializeWorker (
344 Status
= gBS
->LocateProtocol (
345 &gEfiDataHubProtocolGuid
,
347 (VOID
**) &mDataHubProtocol
349 if (EFI_ERROR (Status
)) {
350 mDataHubProtocol
= NULL
;
355 // Create a Notify Event to log data in Data Hub
357 Status
= gBS
->CreateEvent (
360 LogDataHubEventCallBack
,
365 ASSERT_EFI_ERROR (Status
);