]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/StatusCode/DatahubStatusCodeHandlerDxe/DataHubStatusCodeWorker.c
IntelFrameworkModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / StatusCode / DatahubStatusCodeHandlerDxe / DataHubStatusCodeWorker.c
1 /** @file
2 Data Hub status code worker.
3
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "DatahubStatusCodeHandlerDxe.h"
10
11 //
12 // Initialize FIFO to cache records.
13 //
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;
18 //
19 // Cache data hub protocol.
20 //
21 EFI_DATA_HUB_PROTOCOL *mDataHubProtocol = NULL;
22
23
24 /**
25 Retrieve one record of from free record buffer. This record is removed from
26 free record buffer.
27
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.
30
31 @return Pointer to the free record.
32 NULL means failure to allocate new memeory for free record buffer.
33
34 **/
35 DATA_HUB_STATUS_CODE_DATA_RECORD *
36 AcquireRecordBuffer (
37 VOID
38 )
39 {
40 DATAHUB_STATUSCODE_RECORD *Record;
41 EFI_TPL CurrentTpl;
42 LIST_ENTRY *Node;
43 UINT32 Index;
44
45 CurrentTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
46
47 if (!IsListEmpty (&mRecordsBuffer)) {
48 //
49 // Strip one entry from free record buffer.
50 //
51 Node = GetFirstNode (&mRecordsBuffer);
52 RemoveEntryList (Node);
53
54 Record = BASE_CR (Node, DATAHUB_STATUSCODE_RECORD, Node);
55 } else {
56 if (CurrentTpl > TPL_NOTIFY) {
57 //
58 // Memory management should work at <=TPL_NOTIFY
59 //
60 gBS->RestoreTPL (CurrentTpl);
61 return NULL;
62 }
63
64 //
65 // If free record buffer is exhausted, then allocate 16 new records for it.
66 //
67 gBS->RestoreTPL (CurrentTpl);
68 Record = (DATAHUB_STATUSCODE_RECORD *) AllocateZeroPool (sizeof (DATAHUB_STATUSCODE_RECORD) * 16);
69 if (Record == NULL) {
70 return NULL;
71 }
72
73 CurrentTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
74 //
75 // Here we only insert 15 new records to the free record buffer, for the first record
76 // will be returned immediately.
77 //
78 for (Index = 1; Index < 16; Index++) {
79 InsertTailList (&mRecordsBuffer, &Record[Index].Node);
80 }
81 }
82
83 Record->Signature = DATAHUB_STATUS_CODE_SIGNATURE;
84 InsertTailList (&mRecordsFifo, &Record->Node);
85
86 gBS->RestoreTPL (CurrentTpl);
87
88 return (DATA_HUB_STATUS_CODE_DATA_RECORD *) (Record->Data);
89 }
90
91
92 /**
93 Retrieve one record from Records FIFO. The record would be removed from FIFO.
94
95 @return Point to record, which is ready to be logged.
96 NULL means the FIFO of record is empty.
97
98 **/
99 DATA_HUB_STATUS_CODE_DATA_RECORD *
100 RetrieveRecord (
101 VOID
102 )
103 {
104 DATA_HUB_STATUS_CODE_DATA_RECORD *RecordData;
105 DATAHUB_STATUSCODE_RECORD *Record;
106 LIST_ENTRY *Node;
107 EFI_TPL CurrentTpl;
108
109 RecordData = NULL;
110
111 CurrentTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
112
113 if (!IsListEmpty (&mRecordsFifo)) {
114 Node = GetFirstNode (&mRecordsFifo);
115 Record = CR (Node, DATAHUB_STATUSCODE_RECORD, Node, DATAHUB_STATUS_CODE_SIGNATURE);
116 ASSERT (Record != NULL);
117
118 RemoveEntryList (&Record->Node);
119 RecordData = (DATA_HUB_STATUS_CODE_DATA_RECORD *) Record->Data;
120 }
121
122 gBS->RestoreTPL (CurrentTpl);
123
124 return RecordData;
125 }
126
127 /**
128 Release given record and return it to free record buffer.
129
130 @param RecordData Pointer to the record to release.
131
132 **/
133 VOID
134 ReleaseRecord (
135 DATA_HUB_STATUS_CODE_DATA_RECORD *RecordData
136 )
137 {
138 DATAHUB_STATUSCODE_RECORD *Record;
139 EFI_TPL CurrentTpl;
140
141 Record = CR (RecordData, DATAHUB_STATUSCODE_RECORD, Data[0], DATAHUB_STATUS_CODE_SIGNATURE);
142 ASSERT (Record != NULL);
143
144 CurrentTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
145
146 InsertTailList (&mRecordsBuffer, &Record->Node);
147 Record->Signature = 0;
148
149 gBS->RestoreTPL (CurrentTpl);
150 }
151
152 /**
153 Report status code into DataHub.
154
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
163 different callers.
164 @param Data This optional parameter may be used to pass additional data.
165
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.
170
171 **/
172 EFI_STATUS
173 EFIAPI
174 DataHubStatusCodeReportWorker (
175 IN EFI_STATUS_CODE_TYPE CodeType,
176 IN EFI_STATUS_CODE_VALUE Value,
177 IN UINT32 Instance,
178 IN EFI_GUID *CallerId,
179 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
180 )
181 {
182 DATA_HUB_STATUS_CODE_DATA_RECORD *Record;
183 UINT32 ErrorLevel;
184 BASE_LIST Marker;
185 CHAR8 *Format;
186 UINTN CharCount;
187
188 //
189 // Use atom operation to avoid the reentant of report.
190 // If current status is not zero, then the function is reentrancy.
191 //
192 if (InterlockedCompareExchange32 (&mLogDataHubStatus, 0, 0) == 1) {
193 return EFI_DEVICE_ERROR;
194 }
195
196 Record = AcquireRecordBuffer ();
197 if (Record == NULL) {
198 //
199 // There are no empty record buffer in private buffers
200 //
201 return EFI_OUT_OF_RESOURCES;
202 }
203
204 //
205 // Construct Data Hub Extended Data
206 //
207 Record->CodeType = CodeType;
208 Record->Value = Value;
209 Record->Instance = Instance;
210
211 if (CallerId != NULL) {
212 CopyMem (&Record->CallerId, CallerId, sizeof (EFI_GUID));
213 }
214
215 if (Data != NULL) {
216 if (ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) {
217 CharCount = UnicodeBSPrintAsciiFormat (
218 (CHAR16 *) (Record + 1),
219 EFI_STATUS_CODE_DATA_MAX_SIZE,
220 Format,
221 Marker
222 );
223 //
224 // Change record data type to DebugType.
225 //
226 CopyGuid (&Record->Data.Type, &gEfiStatusCodeDataTypeDebugGuid);
227 Record->Data.HeaderSize = Data->HeaderSize;
228 Record->Data.Size = (UINT16) ((CharCount + 1) * sizeof (CHAR16));
229 } else {
230 //
231 // Copy status code data header
232 //
233 CopyMem (&Record->Data, Data, sizeof (EFI_STATUS_CODE_DATA));
234
235 if (Data->Size > EFI_STATUS_CODE_DATA_MAX_SIZE) {
236 Record->Data.Size = EFI_STATUS_CODE_DATA_MAX_SIZE;
237 }
238 CopyMem ((VOID *) (Record + 1), Data + 1, Record->Data.Size);
239 }
240 }
241
242 gBS->SignalEvent (mLogDataHubEvent);
243
244 return EFI_SUCCESS;
245 }
246
247
248 /**
249 The Event handler which will be notified to log data in Data Hub.
250
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.
254
255 **/
256 VOID
257 EFIAPI
258 LogDataHubEventCallBack (
259 IN EFI_EVENT Event,
260 IN VOID *Context
261 )
262 {
263 DATA_HUB_STATUS_CODE_DATA_RECORD *Record;
264 UINT32 Size;
265 UINT64 DataRecordClass;
266
267 //
268 // Use atom operation to avoid the reentant of report.
269 // If current status is not zero, then the function is reentrancy.
270 //
271 if (InterlockedCompareExchange32 (&mLogDataHubStatus, 0, 1) == 1) {
272 return;
273 }
274
275 //
276 // Log DataRecord in Data Hub.
277 // Journal records fifo to find all record entry.
278 //
279 while (TRUE) {
280 //
281 // Retrieve record from record FIFO until no more record can be retrieved.
282 //
283 Record = RetrieveRecord ();
284 if (Record == NULL) {
285 break;
286 }
287 //
288 // Add in the size of the header we added.
289 //
290 Size = sizeof (DATA_HUB_STATUS_CODE_DATA_RECORD) + (UINT32) Record->Data.Size;
291
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;
298 } else {
299 //
300 // Should never get here.
301 //
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;
306 }
307
308 //
309 // Log DataRecord in Data Hub
310 //
311 mDataHubProtocol->LogData (
312 mDataHubProtocol,
313 &gEfiDataHubStatusCodeRecordGuid,
314 &gEfiStatusCodeRuntimeProtocolGuid,
315 DataRecordClass,
316 Record,
317 Size
318 );
319
320 ReleaseRecord (Record);
321 }
322
323 //
324 // Restore the nest status of report
325 //
326 InterlockedCompareExchange32 (&mLogDataHubStatus, 1, 0);
327 }
328
329
330 /**
331 Locate Data Hub Protocol and create event for logging data
332 as initialization for data hub status code worker.
333
334 @retval EFI_SUCCESS Initialization is successful.
335
336 **/
337 EFI_STATUS
338 DataHubStatusCodeInitializeWorker (
339 VOID
340 )
341 {
342 EFI_STATUS Status;
343
344 Status = gBS->LocateProtocol (
345 &gEfiDataHubProtocolGuid,
346 NULL,
347 (VOID **) &mDataHubProtocol
348 );
349 if (EFI_ERROR (Status)) {
350 mDataHubProtocol = NULL;
351 return Status;
352 }
353
354 //
355 // Create a Notify Event to log data in Data Hub
356 //
357 Status = gBS->CreateEvent (
358 EVT_NOTIFY_SIGNAL,
359 TPL_CALLBACK,
360 LogDataHubEventCallBack,
361 NULL,
362 &mLogDataHubEvent
363 );
364
365 ASSERT_EFI_ERROR (Status);
366
367 return EFI_SUCCESS;
368 }
369
370