IpmiInit.c 15 KB

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