IpmiInit.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /** @file
  2. Generic IPMI stack driver
  3. @copyright
  4. Copyright 1999 - 2021 Intel Corporation. <BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <IndustryStandard/Ipmi.h>
  8. #include <SmStatusCodes.h>
  9. #include "IpmiHooks.h"
  10. #include "IpmiBmcCommon.h"
  11. #include "IpmiBmc.h"
  12. #include "IpmiPhysicalLayer.h"
  13. #include <Library/TimerLib.h>
  14. #ifdef FAST_VIDEO_SUPPORT
  15. #include <Protocol/VideoPrint.h>
  16. #endif
  17. #include <Library/UefiRuntimeServicesTableLib.h>
  18. /******************************************************************************
  19. * Local variables
  20. */
  21. IPMI_BMC_INSTANCE_DATA *mIpmiInstance = NULL;
  22. EFI_HANDLE mImageHandle;
  23. //
  24. // Specific test interface
  25. //
  26. VOID
  27. GetDeviceSpecificTestResults (
  28. IN IPMI_BMC_INSTANCE_DATA *IpmiInstance
  29. )
  30. /*++
  31. Routine Description:
  32. This is a BMC specific routine to check the device specific self test results as defined
  33. in the Bensley BMC core specification.
  34. Arguments:
  35. IpmiInstance - Data structure describing BMC variables and used for sending commands
  36. Returns:
  37. VOID
  38. --*/
  39. {
  40. return;
  41. }
  42. EFI_STATUS
  43. GetSelfTest (
  44. IN IPMI_BMC_INSTANCE_DATA *IpmiInstance,
  45. IN EFI_STATUS_CODE_VALUE StatusCodeValue[],
  46. IN OUT UINT8 *ErrorCount
  47. )
  48. /*++
  49. Routine Description:
  50. Execute the Get Self Test results command to determine whether or not the BMC self tests
  51. have passed
  52. Arguments:
  53. IpmiInstance - Data structure describing BMC variables and used for sending commands
  54. StatusCodeValue - An array used to accumulate error codes for later reporting.
  55. ErrorCount - Counter used to keep track of error codes in StatusCodeValue
  56. Returns:
  57. EFI_SUCCESS - BMC Self test results are retrieved and saved into BmcStatus
  58. EFI_DEVICE_ERROR - BMC failed to return self test results.
  59. --*/
  60. {
  61. EFI_STATUS Status;
  62. UINT32 DataSize;
  63. UINT8 Index;
  64. UINT8 *TempPtr;
  65. UINT32 Retries;
  66. BOOLEAN bResultFlag = FALSE;
  67. UINT8 TempData[MAX_TEMP_DATA];
  68. IPMI_SELF_TEST_RESULT_RESPONSE *SelfTestResult;
  69. //
  70. // Get the SELF TEST Results.
  71. //
  72. //
  73. //Note: If BMC PcdIpmiBmcReadyDelayTimer < BMC_KCS_TIMEOUT, it need set Retries as 1. Otherwise it will make SELT failure, caused by below condition (EFI_ERROR(Status) || Retries == 0)
  74. //
  75. if (PcdGet8 (PcdIpmiBmcReadyDelayTimer) < BMC_KCS_TIMEOUT) {
  76. Retries = 1;
  77. } else {
  78. Retries = PcdGet8 (PcdIpmiBmcReadyDelayTimer);
  79. }
  80. DataSize = sizeof (TempData);
  81. SelfTestResult = (IPMI_SELF_TEST_RESULT_RESPONSE *) &TempData[0];
  82. SelfTestResult->Result = 0;
  83. do {
  84. Status = IpmiSendCommand (
  85. &IpmiInstance->IpmiTransport,
  86. IPMI_NETFN_APP,
  87. 0,
  88. IPMI_APP_GET_SELFTEST_RESULTS,
  89. NULL,
  90. 0,
  91. TempData,
  92. &DataSize
  93. );
  94. if (Status == EFI_SUCCESS) {
  95. switch (SelfTestResult->Result) {
  96. case IPMI_APP_SELFTEST_NO_ERROR:
  97. case IPMI_APP_SELFTEST_NOT_IMPLEMENTED:
  98. case IPMI_APP_SELFTEST_ERROR:
  99. case IPMI_APP_SELFTEST_FATAL_HW_ERROR:
  100. bResultFlag = TRUE;
  101. break;
  102. default:
  103. break;
  104. } //switch
  105. if (bResultFlag) {
  106. break;
  107. }
  108. }
  109. MicroSecondDelay (500 * 1000);
  110. } while (--Retries > 0);
  111. //
  112. // If Status indicates a Device error, then the BMC is not responding, so send an error.
  113. //
  114. if (EFI_ERROR (Status) || Retries == 0) {
  115. DEBUG ((EFI_D_ERROR, "\n[IPMI] BMC self-test does not respond (status: %r)!\n\n", Status));
  116. if (*ErrorCount < MAX_SOFT_COUNT) {
  117. StatusCodeValue[*ErrorCount] = EFI_COMPUTING_UNIT_FIRMWARE_PROCESSOR | EFI_CU_FP_EC_COMM_ERROR;
  118. (*ErrorCount)++;
  119. }
  120. IpmiInstance->BmcStatus = BMC_HARDFAIL;
  121. return Status;
  122. } else {
  123. DEBUG ((DEBUG_INFO, "[IPMI] BMC self-test result: %02X-%02X\n", SelfTestResult->Result, SelfTestResult->Param));
  124. //
  125. // Copy the Self test results to Error Status. Data will be copied as long as it
  126. // does not exceed the size of the ErrorStatus variable.
  127. //
  128. for (Index = 0, TempPtr = (UINT8 *) &IpmiInstance->ErrorStatus;
  129. (Index < DataSize) && (Index < sizeof (IpmiInstance->ErrorStatus));
  130. Index++, TempPtr++
  131. ) {
  132. *TempPtr = TempData[Index];
  133. }
  134. //
  135. // Check the IPMI defined self test results.
  136. // Additional Cases are device specific test results.
  137. //
  138. switch (SelfTestResult->Result) {
  139. case IPMI_APP_SELFTEST_NO_ERROR:
  140. case IPMI_APP_SELFTEST_NOT_IMPLEMENTED:
  141. IpmiInstance->BmcStatus = BMC_OK;
  142. break;
  143. case IPMI_APP_SELFTEST_ERROR:
  144. //
  145. // Three of the possible errors result in BMC hard failure; FRU Corruption,
  146. // BootBlock Firmware corruption, and Operational Firmware Corruption. All
  147. // other errors are BMC soft failures.
  148. //
  149. if ((SelfTestResult->Param & (IPMI_APP_SELFTEST_FRU_CORRUPT | IPMI_APP_SELFTEST_FW_BOOTBLOCK_CORRUPT | IPMI_APP_SELFTEST_FW_CORRUPT)) != 0) {
  150. IpmiInstance->BmcStatus = BMC_HARDFAIL;
  151. } else {
  152. IpmiInstance->BmcStatus = BMC_SOFTFAIL;
  153. }
  154. //
  155. // Check if SDR repository is empty and report it if it is.
  156. //
  157. if ((SelfTestResult->Param & IPMI_APP_SELFTEST_SDR_REPOSITORY_EMPTY) != 0) {
  158. if (*ErrorCount < MAX_SOFT_COUNT) {
  159. StatusCodeValue[*ErrorCount] = EFI_COMPUTING_UNIT_FIRMWARE_PROCESSOR | CU_FP_EC_SDR_EMPTY;
  160. (*ErrorCount)++;
  161. }
  162. }
  163. break;
  164. case IPMI_APP_SELFTEST_FATAL_HW_ERROR:
  165. IpmiInstance->BmcStatus = BMC_HARDFAIL;
  166. break;
  167. default:
  168. IpmiInstance->BmcStatus = BMC_HARDFAIL;
  169. //
  170. // Call routine to check device specific failures.
  171. //
  172. GetDeviceSpecificTestResults (IpmiInstance);
  173. }
  174. if (IpmiInstance->BmcStatus == BMC_HARDFAIL) {
  175. if (*ErrorCount < MAX_SOFT_COUNT) {
  176. StatusCodeValue[*ErrorCount] = EFI_COMPUTING_UNIT_FIRMWARE_PROCESSOR | EFI_CU_FP_EC_HARD_FAIL;
  177. (*ErrorCount)++;
  178. }
  179. } else if (IpmiInstance->BmcStatus == BMC_SOFTFAIL) {
  180. if (*ErrorCount < MAX_SOFT_COUNT) {
  181. StatusCodeValue[*ErrorCount] = EFI_COMPUTING_UNIT_FIRMWARE_PROCESSOR | EFI_CU_FP_EC_SOFT_FAIL;
  182. (*ErrorCount)++;
  183. }
  184. }
  185. }
  186. return EFI_SUCCESS;
  187. } // GetSelfTest()
  188. EFI_STATUS
  189. GetDeviceId (
  190. IN IPMI_BMC_INSTANCE_DATA *IpmiInstance,
  191. IN EFI_STATUS_CODE_VALUE StatusCodeValue[ ],
  192. IN OUT UINT8 *ErrorCount
  193. )
  194. /*++
  195. Routine Description:
  196. Execute the Get Device ID command to determine whether or not the BMC is in Force Update
  197. Mode. If it is, then report it to the error manager.
  198. Arguments:
  199. IpmiInstance - Data structure describing BMC variables and used for sending commands
  200. StatusCodeValue - An array used to accumulate error codes for later reporting.
  201. ErrorCount - Counter used to keep track of error codes in StatusCodeValue
  202. Returns:
  203. Status
  204. --*/
  205. {
  206. EFI_STATUS Status;
  207. UINT32 DataSize;
  208. SM_CTRL_INFO *pBmcInfo;
  209. IPMI_MSG_GET_BMC_EXEC_RSP *pBmcExecContext;
  210. UINT32 Retries;
  211. UINT8 TempData[MAX_TEMP_DATA];
  212. #ifdef FAST_VIDEO_SUPPORT
  213. EFI_VIDEOPRINT_PROTOCOL *VideoPrintProtocol;
  214. EFI_STATUS VideoPrintStatus;
  215. #endif
  216. #ifdef FAST_VIDEO_SUPPORT
  217. VideoPrintStatus = gBS->LocateProtocol (
  218. &gEfiVideoPrintProtocolGuid,
  219. NULL,
  220. &VideoPrintProtocol
  221. );
  222. #endif
  223. //
  224. // Set up a loop to retry for up to PcdIpmiBmcReadyDelayTimer seconds. Calculate retries not timeout
  225. // so that in case KCS is not enabled and IpmiSendCommand() returns
  226. // immediately we will not wait all the PcdIpmiBmcReadyDelayTimer seconds.
  227. //
  228. Retries = PcdGet8 (PcdIpmiBmcReadyDelayTimer);
  229. //
  230. // Get the device ID information for the BMC.
  231. //
  232. DataSize = sizeof (TempData);
  233. while (EFI_ERROR (Status = IpmiSendCommand (
  234. &IpmiInstance->IpmiTransport,
  235. IPMI_NETFN_APP, 0,
  236. IPMI_APP_GET_DEVICE_ID,
  237. NULL, 0,
  238. TempData, &DataSize))
  239. ) {
  240. DEBUG ((DEBUG_ERROR, "[IPMI] BMC does not respond by Get BMC DID (status: %r), %d retries left, ResponseData: 0x%lx\n",
  241. Status, Retries, TempData));
  242. if (Retries-- == 0) {
  243. IpmiInstance->BmcStatus = BMC_HARDFAIL;
  244. return Status;
  245. }
  246. //
  247. //Handle the case that BMC FW still not enable KCS channel after AC cycle. just stall 1 second
  248. //
  249. MicroSecondDelay (1*1000*1000);
  250. }
  251. pBmcInfo = (SM_CTRL_INFO*)&TempData[0];
  252. DEBUG ((EFI_D_ERROR, "[IPMI] BMC Device ID: 0x%02X, firmware version: %d.%02X UpdateMode:%x\n", pBmcInfo->DeviceId, pBmcInfo->MajorFirmwareRev, pBmcInfo->MinorFirmwareRev,pBmcInfo->UpdateMode));
  253. //
  254. // In OpenBMC, UpdateMode: the bit 7 of byte 4 in get device id command is used for the BMC status:
  255. // 0 means BMC is ready, 1 means BMC is not ready.
  256. // At the very beginning of BMC power on, the status is 1 means BMC is in booting process and not ready. It is not the flag for force update mode.
  257. //
  258. if (pBmcInfo->UpdateMode == BMC_READY) {
  259. mIpmiInstance->BmcStatus = BMC_OK;
  260. return EFI_SUCCESS;
  261. } else {
  262. DataSize = sizeof (TempData);
  263. Status = IpmiSendCommand (
  264. &IpmiInstance->IpmiTransport,
  265. IPMI_NETFN_FIRMWARE, 0,
  266. IPMI_GET_BMC_EXECUTION_CONTEXT,
  267. NULL, 0,
  268. TempData, &DataSize
  269. );
  270. pBmcExecContext = (IPMI_MSG_GET_BMC_EXEC_RSP*)&TempData[0];
  271. DEBUG ((DEBUG_INFO, "[IPMI] Operational status of BMC: 0x%x\n", pBmcExecContext->CurrentExecutionContext));
  272. if ((pBmcExecContext->CurrentExecutionContext == IPMI_BMC_IN_FORCED_UPDATE_MODE) &&
  273. !EFI_ERROR (Status)) {
  274. DEBUG ((DEBUG_ERROR, "[IPMI] BMC in Forced Update mode, skip waiting for BMC_READY.\n"));
  275. IpmiInstance->BmcStatus = BMC_UPDATE_IN_PROGRESS;
  276. } else {
  277. //
  278. // Updatemode = 1 mean BMC is not ready, continue waiting.
  279. //
  280. while (Retries-- != 0) {
  281. MicroSecondDelay(1*1000*1000); //delay 1 seconds
  282. DEBUG ((EFI_D_ERROR, "[IPMI] UpdateMode Retries: %d \n",Retries));
  283. DataSize = sizeof (TempData);
  284. Status = IpmiSendCommand (
  285. &IpmiInstance->IpmiTransport,
  286. IPMI_NETFN_APP, 0,
  287. IPMI_APP_GET_DEVICE_ID,
  288. NULL, 0,
  289. TempData, &DataSize
  290. );
  291. if (!EFI_ERROR (Status)) {
  292. pBmcInfo = (SM_CTRL_INFO*)&TempData[0];
  293. DEBUG ((DEBUG_ERROR, "[IPMI] UpdateMode Retries: %d pBmcInfo->UpdateMode:%x, Status: %r, Response Data: 0x%lx\n",Retries, pBmcInfo->UpdateMode, Status, TempData));
  294. if (pBmcInfo->UpdateMode == BMC_READY) {
  295. mIpmiInstance->BmcStatus = BMC_OK;
  296. return EFI_SUCCESS;
  297. }
  298. }
  299. }
  300. mIpmiInstance->BmcStatus = BMC_HARDFAIL;
  301. }
  302. }
  303. return Status;
  304. } // GetDeviceId()
  305. /**
  306. This function initializes KCS interface to BMC.
  307. Setup and initialize the BMC for the DXE phase. In order to verify the BMC is functioning
  308. as expected, the BMC Selftest is performed. The results are then checked and any errors are
  309. reported to the error manager. Errors are collected throughout this routine and reported
  310. just prior to installing the driver. If there are more errors than MAX_SOFT_COUNT, then they
  311. will be ignored.
  312. @param[in] ImageHandle - Handle of this driver image
  313. @param[in] SystemTable - Table containing standard EFI services
  314. @retval EFI_SUCCESS - Always success is returned even if KCS does not function
  315. **/
  316. EFI_STATUS
  317. InitializeIpmiKcsPhysicalLayer (
  318. IN EFI_HANDLE ImageHandle,
  319. IN EFI_SYSTEM_TABLE *SystemTable
  320. )
  321. {
  322. EFI_STATUS Status;
  323. UINT8 ErrorCount;
  324. EFI_HANDLE Handle;
  325. UINT8 Index;
  326. EFI_STATUS_CODE_VALUE StatusCodeValue[MAX_SOFT_COUNT];
  327. ErrorCount = 0;
  328. mImageHandle = ImageHandle;
  329. mIpmiInstance = AllocateZeroPool (sizeof (*mIpmiInstance));
  330. if (mIpmiInstance == NULL) {
  331. DEBUG ((EFI_D_ERROR, "ERROR!! Null Pointer returned by AllocateZeroPool ()\n"));
  332. ASSERT_EFI_ERROR (EFI_OUT_OF_RESOURCES);
  333. return EFI_OUT_OF_RESOURCES;
  334. } else {
  335. //
  336. // Calibrate TSC Counter. Stall for 10ms, then multiply the resulting number of
  337. // ticks in that period by 100 to get the number of ticks in a 1 second timeout
  338. //
  339. //
  340. // Initialize the KCS transaction timeout.
  341. //
  342. mIpmiInstance->KcsTimeoutPeriod = (BMC_KCS_TIMEOUT * 1000*1000) / KCS_DELAY_UNIT;
  343. DEBUG ((EFI_D_ERROR, "[IPMI] mIpmiInstance->KcsTimeoutPeriod: 0x%lx\n",mIpmiInstance->KcsTimeoutPeriod));
  344. //
  345. // Initialize IPMI IO Base.
  346. //
  347. mIpmiInstance->IpmiIoBase = PcdGet16 (PcdIpmiIoBaseAddress);
  348. mIpmiInstance->Signature = SM_IPMI_BMC_SIGNATURE;
  349. mIpmiInstance->SlaveAddress = BMC_SLAVE_ADDRESS;
  350. mIpmiInstance->BmcStatus = BMC_NOTREADY;
  351. mIpmiInstance->IpmiTransport.IpmiSubmitCommand = IpmiSendCommand;
  352. mIpmiInstance->IpmiTransport.GetBmcStatus = IpmiGetBmcStatus;
  353. //
  354. // Get the Device ID and check if the system is in Force Update mode.
  355. //
  356. Status = GetDeviceId (
  357. mIpmiInstance,
  358. StatusCodeValue,
  359. &ErrorCount
  360. );
  361. //
  362. // Do not continue initialization if the BMC is in Force Update Mode.
  363. //
  364. if ((mIpmiInstance->BmcStatus != BMC_UPDATE_IN_PROGRESS) &&
  365. (mIpmiInstance->BmcStatus != BMC_HARDFAIL)) {
  366. //
  367. // Get the SELF TEST Results.
  368. //
  369. Status = GetSelfTest (
  370. mIpmiInstance,
  371. StatusCodeValue,
  372. &ErrorCount
  373. );
  374. }
  375. //
  376. // iterate through the errors reporting them to the error manager.
  377. //
  378. for (Index = 0; Index < ErrorCount; Index++) {
  379. ReportStatusCode (
  380. EFI_ERROR_CODE | EFI_ERROR_MAJOR,
  381. StatusCodeValue[Index]
  382. );
  383. }
  384. //
  385. // Now install the Protocol if the BMC is not in a HardFail State and not in Force Update mode
  386. //
  387. if ((mIpmiInstance->BmcStatus != BMC_HARDFAIL) && (mIpmiInstance->BmcStatus != BMC_UPDATE_IN_PROGRESS)) {
  388. Handle = NULL;
  389. Status = gBS->InstallProtocolInterface (
  390. &Handle,
  391. &gIpmiTransportProtocolGuid,
  392. EFI_NATIVE_INTERFACE,
  393. &mIpmiInstance->IpmiTransport
  394. );
  395. ASSERT_EFI_ERROR (Status);
  396. }
  397. return EFI_SUCCESS;
  398. }
  399. } // InitializeIpmiKcsPhysicalLayer()