IpmiInit.c 15 KB

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