]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Universal/StatusCode/DatahubStatusCodeHandlerDxe/DataHubStatusCodeWorker.c
Add datahub status sode handler driver.
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / StatusCode / DatahubStatusCodeHandlerDxe / DataHubStatusCodeWorker.c
CommitLineData
28a94112 1/** @file\r
2 Data Hub status code worker.\r
3\r
4 Copyright (c) 2010, 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 "DatahubStatusCodeHandlerDxe.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\r
193 //\r
194 // Use atom operation to avoid the reentant of report.\r
195 // If current status is not zero, then the function is reentrancy.\r
196 //\r
197 if (InterlockedCompareExchange32 (&mLogDataHubStatus, 0, 0) == 1) {\r
198 return EFI_DEVICE_ERROR;\r
199 }\r
200\r
201 Record = AcquireRecordBuffer ();\r
202 if (Record == NULL) {\r
203 //\r
204 // There are no empty record buffer in private buffers\r
205 //\r
206 return EFI_OUT_OF_RESOURCES;\r
207 }\r
208\r
209 //\r
210 // Construct Data Hub Extended Data\r
211 //\r
212 Record->CodeType = CodeType;\r
213 Record->Value = Value;\r
214 Record->Instance = Instance;\r
215\r
216 if (CallerId != NULL) {\r
217 CopyMem (&Record->CallerId, CallerId, sizeof (EFI_GUID));\r
218 }\r
219\r
220 if (Data != NULL) {\r
221 if (ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) {\r
222 CharCount = UnicodeBSPrintAsciiFormat (\r
223 (CHAR16 *) (Record + 1),\r
224 EFI_STATUS_CODE_DATA_MAX_SIZE,\r
225 Format,\r
226 Marker\r
227 );\r
228 //\r
229 // Change record data type to DebugType.\r
230 //\r
231 CopyGuid (&Record->Data.Type, &gEfiStatusCodeDataTypeDebugGuid);\r
232 Record->Data.HeaderSize = Data->HeaderSize;\r
233 Record->Data.Size = (UINT16) ((CharCount + 1) * sizeof (CHAR16));\r
234 } else {\r
235 //\r
236 // Copy status code data header\r
237 //\r
238 CopyMem (&Record->Data, Data, sizeof (EFI_STATUS_CODE_DATA));\r
239\r
240 if (Data->Size > EFI_STATUS_CODE_DATA_MAX_SIZE) {\r
241 Record->Data.Size = EFI_STATUS_CODE_DATA_MAX_SIZE;\r
242 }\r
243 CopyMem ((VOID *) (Record + 1), Data + 1, Record->Data.Size);\r
244 }\r
245 }\r
246\r
247 gBS->SignalEvent (mLogDataHubEvent);\r
248\r
249 return EFI_SUCCESS;\r
250}\r
251\r
252\r
253/**\r
254 The Event handler which will be notified to log data in Data Hub.\r
255\r
256 @param Event Instance of the EFI_EVENT to signal whenever data is\r
257 available to be logged in the system.\r
258 @param Context Context of the event.\r
259\r
260**/\r
261VOID\r
262EFIAPI\r
263LogDataHubEventCallBack (\r
264 IN EFI_EVENT Event,\r
265 IN VOID *Context\r
266 )\r
267{\r
268 DATA_HUB_STATUS_CODE_DATA_RECORD *Record;\r
269 UINT32 Size;\r
270 UINT64 DataRecordClass;\r
271\r
272 //\r
273 // Use atom operation to avoid the reentant of report.\r
274 // If current status is not zero, then the function is reentrancy.\r
275 //\r
276 if (InterlockedCompareExchange32 (&mLogDataHubStatus, 0, 1) == 1) {\r
277 return;\r
278 }\r
279\r
280 //\r
281 // Log DataRecord in Data Hub.\r
282 // Journal records fifo to find all record entry.\r
283 //\r
284 while (TRUE) {\r
285 //\r
286 // Retrieve record from record FIFO until no more record can be retrieved.\r
287 //\r
288 Record = RetrieveRecord ();\r
289 if (Record == NULL) {\r
290 break;\r
291 }\r
292 //\r
293 // Add in the size of the header we added.\r
294 //\r
295 Size = sizeof (DATA_HUB_STATUS_CODE_DATA_RECORD) + (UINT32) Record->Data.Size;\r
296\r
297 if ((Record->CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) {\r
298 DataRecordClass = EFI_DATA_RECORD_CLASS_PROGRESS_CODE;\r
299 } else if ((Record->CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) {\r
300 DataRecordClass = EFI_DATA_RECORD_CLASS_ERROR;\r
301 } else if ((Record->CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE) {\r
302 DataRecordClass = EFI_DATA_RECORD_CLASS_DEBUG;\r
303 } else {\r
304 //\r
305 // Should never get here.\r
306 //\r
307 DataRecordClass = EFI_DATA_RECORD_CLASS_DEBUG |\r
308 EFI_DATA_RECORD_CLASS_ERROR |\r
309 EFI_DATA_RECORD_CLASS_DATA |\r
310 EFI_DATA_RECORD_CLASS_PROGRESS_CODE;\r
311 }\r
312\r
313 //\r
314 // Log DataRecord in Data Hub\r
315 //\r
316 mDataHubProtocol->LogData (\r
317 mDataHubProtocol,\r
318 &gEfiDataHubStatusCodeRecordGuid,\r
319 &gEfiStatusCodeRuntimeProtocolGuid,\r
320 DataRecordClass,\r
321 Record,\r
322 Size\r
323 );\r
324\r
325 ReleaseRecord (Record);\r
326 }\r
327\r
328 //\r
329 // Restore the nest status of report\r
330 //\r
331 InterlockedCompareExchange32 (&mLogDataHubStatus, 1, 0);\r
332}\r
333\r
334\r
335/**\r
336 Locate Data Hub Protocol and create event for logging data\r
337 as initialization for data hub status code worker.\r
338\r
339 @retval EFI_SUCCESS Initialization is successful.\r
340\r
341**/\r
342EFI_STATUS\r
343DataHubStatusCodeInitializeWorker (\r
344 VOID\r
345 )\r
346{\r
347 EFI_STATUS Status;\r
348\r
349 Status = gBS->LocateProtocol (\r
350 &gEfiDataHubProtocolGuid, \r
351 NULL, \r
352 (VOID **) &mDataHubProtocol\r
353 );\r
354 if (EFI_ERROR (Status)) {\r
355 mDataHubProtocol = NULL;\r
356 return Status;\r
357 }\r
358\r
359 //\r
360 // Create a Notify Event to log data in Data Hub\r
361 //\r
362 Status = gBS->CreateEvent (\r
363 EVT_NOTIFY_SIGNAL,\r
364 TPL_CALLBACK,\r
365 LogDataHubEventCallBack,\r
366 NULL,\r
367 &mLogDataHubEvent\r
368 );\r
369\r
370 ASSERT_EFI_ERROR (Status);\r
371\r
372 return EFI_SUCCESS;\r
373}\r
374\r
375\r