hc
2024-03-25 edb30157bad0c0001c32b854271ace01d3b9a16a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/** @file
  Generic SMM IPMI stack driver
 
  @copyright
  Copyright 1999 - 2021 Intel Corporation. <BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent
**/
 
//
// Statements that include other files
//
#include <IndustryStandard/Ipmi.h>
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/SmmLib.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/SmmServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/IpmiBaseLib.h>
#include <SmStatusCodes.h>
#include "IpmiHooks.h"
#include "IpmiBmcCommon.h"
#include "IpmiBmc.h"
#include <Library/TimerLib.h>
 
IPMI_BMC_INSTANCE_DATA             *mIpmiInstance;
EFI_HANDLE                         mImageHandle;
 
 
EFI_STATUS
GetDeviceId (
  IN      IPMI_BMC_INSTANCE_DATA       *IpmiInstance,
  IN      EFI_STATUS_CODE_VALUE        StatusCodeValue[ ],
  IN OUT  UINT8                        *ErrorCount
  )
/*++
 
Routine Description:
  Execute the Get Device ID command to determine whether or not the BMC is in Force Update
  Mode.  If it is, then report it to the error manager.
 
Arguments:
  IpmiInstance    - Data structure describing BMC variables and used for sending commands
  StatusCodeValue - An array used to accumulate error codes for later reporting.
  ErrorCount      - Counter used to keep track of error codes in StatusCodeValue
 
Returns:
  Status
 
--*/
{
  EFI_STATUS               Status;
  UINT32                   DataSize;
  SM_CTRL_INFO             *ControllerInfo;
  UINT8                    TimeOut;
  UINT8                    Retries;
 
  TimeOut = 0;
  Retries = PcdGet8 (PcdIpmiBmcReadyDelayTimer);
 
  do {
    //
    // Get the device ID information for the BMC.
    //
    DataSize = MAX_TEMP_DATA;
    Status = IpmiSendCommand (
               &IpmiInstance->IpmiTransport,
               IPMI_NETFN_APP,
               0,
               IPMI_APP_GET_DEVICE_ID,
               NULL,
               0,
               IpmiInstance->TempData,
               &DataSize
               );
    if (Status == EFI_SUCCESS) {
      DEBUG ((EFI_D_INFO, "IPMI: SendCommand success!\n"));
      break;
    } else {
      //
      // Display message and retry.
      //
      DEBUG (
        (EFI_D_ERROR | EFI_D_INFO,
         "IPMI: Waiting for BMC (KCS 0x%x)...\n",
         IpmiInstance->IpmiIoBase)
        );
      MicroSecondDelay (500 * 1000);
      TimeOut++;
    }
  } while (TimeOut < Retries);
 
  //
  // If there is no error then proceed to check the data returned by the BMC
  //
  if (!EFI_ERROR (Status)) {
    ControllerInfo = (SM_CTRL_INFO *) IpmiInstance->TempData;
    //
    // If the controller is in Update Mode and the maximum number of errors has not been exceeded, then
    // save the error code to the StatusCode array and increment the counter.  Set the BMC Status to indicate
    // the BMC is in force update mode.
    //
    if (ControllerInfo->UpdateMode != 0) {
      IpmiInstance->BmcStatus = BMC_UPDATE_IN_PROGRESS;
      if (*ErrorCount < MAX_SOFT_COUNT) {
        StatusCodeValue[*ErrorCount] = EFI_COMPUTING_UNIT_FIRMWARE_PROCESSOR | CU_FP_EC_FORCE_UPDATE_MODE;
        (*ErrorCount)++;
      }
    }
  }
 
  return Status;
}
 
EFI_STATUS
SmmInitializeIpmiKcsPhysicalLayer (
  IN EFI_HANDLE             ImageHandle,
  IN EFI_SYSTEM_TABLE       *SystemTable
  )
/*++
 
Routine Description:
  Setup and initialize the BMC for the DXE phase.  In order to verify the BMC is functioning
  as expected, the BMC Selftest is performed.  The results are then checked and any errors are
  reported to the error manager.  Errors are collected throughout this routine and reported
  just prior to installing the driver.  If there are more errors than MAX_SOFT_COUNT, then they
  will be ignored.
 
Arguments:
  ImageHandle - Handle of this driver image
  SystemTable - Table containing standard EFI services
 
Returns:
  EFI_SUCCESS - Successful driver initialization
 
--*/
{
  EFI_STATUS                       Status;
  UINT8                            ErrorCount;
  EFI_HANDLE                       Handle;
  EFI_STATUS_CODE_VALUE            StatusCodeValue[MAX_SOFT_COUNT];
 
  DEBUG ((DEBUG_INFO,"SmmInitializeIpmiKcsPhysicalLayer entry \n"));
  ErrorCount = 0;
  mImageHandle = ImageHandle;
 
  mIpmiInstance = AllocateZeroPool (sizeof (IPMI_BMC_INSTANCE_DATA));
  ASSERT (mIpmiInstance != NULL);
  if (mIpmiInstance == NULL) {
    DEBUG ((EFI_D_ERROR, "ERROR!! Null Pointer returned by AllocateZeroPool ()\n"));
    ASSERT_EFI_ERROR (EFI_OUT_OF_RESOURCES);
    return EFI_OUT_OF_RESOURCES;
  } else {
 
    //
    // Initialize the KCS transaction timeout. Assume delay unit is 1000 us.
    //
    mIpmiInstance->KcsTimeoutPeriod = (BMC_KCS_TIMEOUT * 1000*1000) / KCS_DELAY_UNIT;
 
    //
    // Initialize IPMI IO Base, we still use SMS IO base to get device ID and Seltest result since SMM IF may have different cmds supported
    //
    mIpmiInstance->IpmiIoBase                       = PcdGet16 (PcdIpmiSmmIoBaseAddress);
    mIpmiInstance->Signature                        = SM_IPMI_BMC_SIGNATURE;
    mIpmiInstance->SlaveAddress                     = BMC_SLAVE_ADDRESS;
    mIpmiInstance->BmcStatus                        = BMC_NOTREADY;
    mIpmiInstance->IpmiTransport.IpmiSubmitCommand  = IpmiSendCommand;
    mIpmiInstance->IpmiTransport.GetBmcStatus       = IpmiGetBmcStatus;
 
    DEBUG ((DEBUG_INFO,"IPMI: Waiting for Getting BMC DID in SMM \n"));
    //
    // Get the Device ID and check if the system is in Force Update mode.
    //
    // Just obey the Spec..
    // If we want to improve performance, we're going to comment it.
    //
    Status = GetDeviceId (
               mIpmiInstance,
               StatusCodeValue,
               &ErrorCount
               );
    ASSERT_EFI_ERROR (Status);
    Handle = NULL;
    Status = gSmst->SmmInstallProtocolInterface (
                      &Handle,
                      &gSmmIpmiTransportProtocolGuid,
                      EFI_NATIVE_INTERFACE,
                      &mIpmiInstance->IpmiTransport
                      );
    ASSERT_EFI_ERROR (Status);
 
    DEBUG ((DEBUG_INFO,"SmmInitializeIpmiKcsPhysicalLayer exit \n"));
 
    return EFI_SUCCESS;
  }
}
 
EFI_STATUS
InitializeSmmGenericIpmi (
  IN EFI_HANDLE             ImageHandle,
  IN EFI_SYSTEM_TABLE       *SystemTable
  )
{
  SmmInitializeIpmiKcsPhysicalLayer (ImageHandle, SystemTable);
  return EFI_SUCCESS;
}