]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/StatusCode/Dxe/DataHubStatusCodeWorker.c
c14dfc8d33c2d0b4239bffd22e8cdb1810234f69
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / StatusCode / Dxe / DataHubStatusCodeWorker.c
1 /** @file
2 Data Hub status code worker in DXE.
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "DxeStatusCode.h"
16
17 //
18 // Initialize FIFO to cache records.
19 //
20 STATIC
21 LIST_ENTRY mRecordsFifo = INITIALIZE_LIST_HEAD_VARIABLE (mRecordsFifo);
22 STATIC
23 LIST_ENTRY mRecordsBuffer = INITIALIZE_LIST_HEAD_VARIABLE (mRecordsBuffer);
24 STATIC
25 UINT32 mLogDataHubStatus = 0;
26 STATIC
27 EFI_EVENT mLogDataHubEvent;
28 //
29 // Cache data hub protocol.
30 //
31 STATIC
32 EFI_DATA_HUB_PROTOCOL *mDataHubProtocol;
33
34
35 /**
36 Return one DATAHUB_STATUSCODE_RECORD space.
37 The size of free record pool would be extend, if the pool is empty.
38
39
40 @retval NULL Can not allocate free memeory for record.
41 @retval !NULL Point to buffer of record.
42
43 **/
44 STATIC
45 DATA_HUB_STATUS_CODE_DATA_RECORD *
46 AcquireRecordBuffer (
47 VOID
48 )
49 {
50 DATAHUB_STATUSCODE_RECORD *Record;
51 EFI_TPL CurrentTpl;
52 LIST_ENTRY *Node;
53 UINT32 Index;
54
55 CurrentTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
56
57 if (!IsListEmpty (&mRecordsBuffer)) {
58 Node = GetFirstNode (&mRecordsBuffer);
59 RemoveEntryList (Node);
60
61 Record = _CR (Node, DATAHUB_STATUSCODE_RECORD, Node);
62 } else {
63 if (CurrentTpl > TPL_NOTIFY) {
64 //
65 // Memory management should work at <=TPL_NOTIFY
66 //
67 gBS->RestoreTPL (CurrentTpl);
68 return NULL;
69 }
70
71 gBS->RestoreTPL (CurrentTpl);
72 Record = (DATAHUB_STATUSCODE_RECORD *) AllocateZeroPool (sizeof (DATAHUB_STATUSCODE_RECORD) * 16);
73 if (NULL == Record) {
74 return NULL;
75 }
76
77 CurrentTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
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 and
94 release to free record buffer.
95
96 @return !NULL Point to record, which is ready to be logged.
97 @return NULL the FIFO of record is empty.
98
99 **/
100 STATIC
101 DATA_HUB_STATUS_CODE_DATA_RECORD *
102 RetrieveRecord (
103 VOID
104 )
105 {
106 DATA_HUB_STATUS_CODE_DATA_RECORD *RecordData = NULL;
107 DATAHUB_STATUSCODE_RECORD *Record;
108 LIST_ENTRY *Node;
109 EFI_TPL CurrentTpl;
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 (NULL != Record);
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 Records to FIFO.
129
130 @param RecordData Point to the record buffer allocated
131 from AcquireRecordBuffer.
132
133 **/
134 STATIC
135 VOID
136 ReleaseRecord (
137 DATA_HUB_STATUS_CODE_DATA_RECORD *RecordData
138 )
139 {
140 DATAHUB_STATUSCODE_RECORD *Record;
141 EFI_TPL CurrentTpl;
142
143 Record = CR (RecordData, DATAHUB_STATUSCODE_RECORD, Data[0], DATAHUB_STATUS_CODE_SIGNATURE);
144 ASSERT (NULL != Record);
145
146 CurrentTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
147
148 InsertTailList (&mRecordsBuffer, &Record->Node);
149 Record->Signature = 0;
150
151 gBS->RestoreTPL (CurrentTpl);
152 }
153
154
155
156 /**
157 Report status code into DataHub.
158
159 @param CodeType Indicates the type of status code being reported. Type EFI_STATUS_CODE_TYPE is defined in "Related Definitions" below.
160
161 @param Value Describes the current status of a hardware or software entity.
162 This included information about the class and subclass that is used to classify the entity
163 as well as an operation. For progress codes, the operation is the current activity.
164 For error codes, it is the exception. For debug codes, it is not defined at this time.
165 Type EFI_STATUS_CODE_VALUE is defined in "Related Definitions" below.
166 Specific values are discussed in the Intel? Platform Innovation Framework for EFI Status Code Specification.
167
168 @param Instance The enumeration of a hardware or software entity within the system.
169 A system may contain multiple entities that match a class/subclass pairing.
170 The instance differentiates between them. An instance of 0 indicates that instance information is unavailable,
171 not meaningful, or not relevant. Valid instance numbers start with 1.
172
173
174 @param CallerId This optional parameter may be used to identify the caller.
175 This parameter allows the status code driver to apply different rules to different callers.
176 Type EFI_GUID is defined in InstallProtocolInterface() in the EFI 1.10 Specification.
177
178
179 @param Data This optional parameter may be used to pass additional data
180
181 @retval EFI_OUT_OF_RESOURCES Can not acquire record buffer.
182 @retval EFI_DEVICE_ERROR EFI serial device can not work after ExitBootService() is called .
183 @retval EFI_SUCCESS Success to cache status code and signal log data event.
184
185 **/
186 EFI_STATUS
187 DataHubStatusCodeReportWorker (
188 IN EFI_STATUS_CODE_TYPE CodeType,
189 IN EFI_STATUS_CODE_VALUE Value,
190 IN UINT32 Instance,
191 IN EFI_GUID *CallerId,
192 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
193 )
194 {
195 DATA_HUB_STATUS_CODE_DATA_RECORD *Record;
196 UINT32 ErrorLevel;
197 VA_LIST Marker;
198 CHAR8 *Format;
199 UINTN CharCount;
200
201
202 //
203 // Use atom operation to avoid the reentant of report.
204 // If current status is not zero, then the function is reentrancy.
205 //
206 if (1 == InterlockedCompareExchange32 (&mLogDataHubStatus, 0, 0)) {
207 return EFI_DEVICE_ERROR;
208 }
209
210 //
211 // See whether in runtime phase or not.
212 //
213 if (EfiAtRuntime ()) {
214 return EFI_DEVICE_ERROR;
215 }
216
217 Record = AcquireRecordBuffer ();
218 if (Record == NULL) {
219 //
220 // There are no empty record buffer in private buffers
221 //
222 return EFI_OUT_OF_RESOURCES;
223 }
224
225 //
226 // Construct Data Hub Extended Data
227 //
228 Record->CodeType = CodeType;
229 Record->Value = Value;
230 Record->Instance = Instance;
231
232 if (CallerId != NULL) {
233 CopyMem (&Record->CallerId, CallerId, sizeof (EFI_GUID));
234 }
235
236 if (Data != NULL) {
237 if (ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) {
238 CharCount = UnicodeVSPrintAsciiFormat (
239 (CHAR16 *) (Record + 1),
240 EFI_STATUS_CODE_DATA_MAX_SIZE,
241 Format,
242 Marker
243 );
244 //
245 // Change record data type from DebugType to String Type.
246 //
247 CopyGuid (&Record->Data.Type, &gEfiStatusCodeDataTypeDebugGuid);
248 Record->Data.HeaderSize = Data->HeaderSize;
249 Record->Data.Size = (UINT16) ((CharCount + 1) * sizeof (CHAR16));
250 } else {
251 //
252 // Copy status code data header
253 //
254 CopyMem (&Record->Data, Data, sizeof (EFI_STATUS_CODE_DATA));
255
256 if (Data->Size > EFI_STATUS_CODE_DATA_MAX_SIZE) {
257 Record->Data.Size = EFI_STATUS_CODE_DATA_MAX_SIZE;
258 }
259 CopyMem ((VOID *) (Record + 1), Data + 1, Record->Data.Size);
260 }
261 }
262
263 gBS->SignalEvent (mLogDataHubEvent);
264
265 return EFI_SUCCESS;
266 }
267
268
269 /**
270 The Event handler which will be notified to log data in Data Hub.
271
272 @param Event Instance of the EFI_EVENT to signal whenever data is
273 available to be logged in the system.
274 @param Context Context of the event.
275
276 **/
277 STATIC
278 VOID
279 EFIAPI
280 LogDataHubEventCallBack (
281 IN EFI_EVENT Event,
282 IN VOID *Context
283 )
284 {
285 DATA_HUB_STATUS_CODE_DATA_RECORD *Record;
286 UINT32 Size;
287 UINT64 DataRecordClass;
288
289 //
290 // Use atom operation to avoid the reentant of report.
291 // If current status is not zero, then the function is reentrancy.
292 //
293 if (1 == InterlockedCompareExchange32 (&mLogDataHubStatus, 0, 1)) {
294 return;
295 }
296
297 //
298 // Log DataRecord in Data Hub.
299 // Journal records fifo to find all record entry.
300 //
301 while (1) {
302 Record = RetrieveRecord ();
303 if (Record == NULL) {
304 break;
305 }
306 //
307 // Add in the size of the header we added.
308 //
309 Size = sizeof (DATA_HUB_STATUS_CODE_DATA_RECORD) + (UINT32) Record->Data.Size;
310
311 if ((Record->CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) {
312 DataRecordClass = EFI_DATA_RECORD_CLASS_PROGRESS_CODE;
313 } else if ((Record->CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) {
314 DataRecordClass = EFI_DATA_RECORD_CLASS_ERROR;
315 } else if ((Record->CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE) {
316 DataRecordClass = EFI_DATA_RECORD_CLASS_DEBUG;
317 } else {
318 //
319 // Should never get here.
320 //
321 DataRecordClass = EFI_DATA_RECORD_CLASS_DEBUG |
322 EFI_DATA_RECORD_CLASS_ERROR |
323 EFI_DATA_RECORD_CLASS_DATA |
324 EFI_DATA_RECORD_CLASS_PROGRESS_CODE;
325 }
326
327 //
328 // Log DataRecord in Data Hub
329 //
330
331 mDataHubProtocol->LogData (
332 mDataHubProtocol,
333 &gEfiDataHubStatusCodeRecordGuid,
334 &gEfiStatusCodeRuntimeProtocolGuid,
335 DataRecordClass,
336 Record,
337 Size
338 );
339
340 ReleaseRecord (Record);
341 }
342
343 //
344 // Restore the nest status of report
345 //
346 InterlockedCompareExchange32 (&mLogDataHubStatus, 1, 0);
347 }
348
349
350 /**
351 Initialize data hubstatus code.
352 Create a data hub listener.
353
354 @return The function always return EFI_SUCCESS
355
356 **/
357 EFI_STATUS
358 DataHubStatusCodeInitializeWorker (
359 VOID
360 )
361 {
362 EFI_STATUS Status;
363
364 Status = gBS->LocateProtocol (
365 &gEfiDataHubProtocolGuid,
366 NULL,
367 (VOID **) &mDataHubProtocol
368 );
369 ASSERT_EFI_ERROR (Status);
370
371 //
372 // Create a Notify Event to log data in Data Hub
373 //
374 Status = gBS->CreateEvent (
375 EVT_NOTIFY_SIGNAL,
376 TPL_CALLBACK,
377 LogDataHubEventCallBack,
378 NULL,
379 &mLogDataHubEvent
380 );
381
382 ASSERT_EFI_ERROR (Status);
383
384 return EFI_SUCCESS;
385 }
386
387