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