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