summaryrefslogtreecommitdiff
path: root/Silicon/Hisilicon/Drivers/Smbios/AddSmbiosType9/AddSmbiosType9.c
blob: 8d8dacd3e028351c5f2f1eeeecfc4127a00e125d (plain)
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
/** @file
*
*  Copyright (c) 2015, Hisilicon Limited. All rights reserved.
*  Copyright (c) 2015, Linaro Limited. All rights reserved.
*
*  This program and the accompanying materials
*  are licensed and made available under the terms and conditions of the BSD License
*  which accompanies this distribution.  The full text of the license may be found at
*  http://opensource.org/licenses/bsd-license.php
*
*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*
**/

#include "AddSmbiosType9.h"

extern SMBIOS_TABLE_TYPE9 gPcieSlotInfo[];
extern UINT8 OemGetPcieSlotNumber ();

REPORT_PCIEDIDVID2BMC  PcieDeviceToReport_2P[PCIEDEVICE_REPORT_MAX] = {
      {67,0,0,0},
      {225,0,0,3},
      {0xFFFF,0xFFFF,0xFFFF,0xFFFF},
      {0xFFFF,0xFFFF,0xFFFF,0xFFFF}
};
VOID
EFIAPI
UpdateSmbiosType9Info(
  IN OUT SMBIOS_TABLE_TYPE9             *Type9Record
)
{
    EFI_STATUS                         Status;
    UINTN                              HandleIndex;
    EFI_HANDLE                        *HandleBuffer;
    UINTN                              HandleCount;
    EFI_PCI_IO_PROTOCOL               *PciIo;
    UINTN                              SegmentNumber;
    UINTN                              BusNumber;
    UINTN                              DeviceNumber;
    UINTN                              FunctionNumber;
    UINTN                              Index;
    REPORT_PCIEDIDVID2BMC              ReportPcieDidVid[PCIEDEVICE_REPORT_MAX];
    if(OemIsMpBoot()){
        (VOID)CopyMem((VOID *)ReportPcieDidVid,(VOID *)PcieDeviceToReport_2P,sizeof(PcieDeviceToReport_2P));
    } else {
        (VOID)CopyMem((VOID *)ReportPcieDidVid,(VOID *)PcieDeviceToReport,sizeof(PcieDeviceToReport));
    }
    Status = gBS->LocateHandleBuffer (
                                      ByProtocol,
                                      &gEfiPciIoProtocolGuid,
                                      NULL,
                                      &HandleCount,
                                      &HandleBuffer
                                      );
    if(EFI_ERROR(Status)) {
        DEBUG((EFI_D_ERROR, " Locate gEfiPciIoProtocol Failed.\n"));
        gBS->FreePool ((VOID *)HandleBuffer);
        return;
    }
    for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
        Status = gBS->HandleProtocol (
                                        HandleBuffer[HandleIndex],
                                        &gEfiPciIoProtocolGuid,
                                        (VOID **)&PciIo
                                        );
        if (EFI_ERROR (Status)) {
            DEBUG((EFI_D_ERROR, "[%a]:[%dL] Status : %r\n", __FUNCTION__, __LINE__, Status));
            continue;
        }
        (VOID)PciIo->GetLocation(PciIo, &SegmentNumber, &BusNumber, &DeviceNumber, &FunctionNumber);
        for(Index = 0; Index < sizeof(ReportPcieDidVid) / sizeof(REPORT_PCIEDIDVID2BMC); Index++){
            if (Type9Record->SlotID == ReportPcieDidVid[Index].Slot + 1) {
                if((BusNumber == ReportPcieDidVid[Index].Bus) && (DeviceNumber == ReportPcieDidVid[Index].Device)) {
                    DEBUG((EFI_D_ERROR,"PCIe device plot in slot Seg %d  bdf %d %d %d\r\n",SegmentNumber,BusNumber,DeviceNumber,FunctionNumber));
                    Type9Record->SegmentGroupNum   = SegmentNumber;
                    Type9Record->BusNum            = BusNumber;
                    Type9Record->DevFuncNum        = (DeviceNumber << 3) | FunctionNumber;
                    Type9Record->CurrentUsage      = SlotUsageInUse;
                    break;
                }
            }
        }
    }
    gBS->FreePool ((VOID *)HandleBuffer);
    return;
}
EFI_STATUS
EFIAPI
AddSmbiosType9Entry (
  IN EFI_HANDLE            ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable
  )
{
    EFI_STATUS                          Status;
    EFI_SMBIOS_TYPE                     SmbiosType;
    EFI_SMBIOS_HANDLE                   SmbiosHandle;
    EFI_SMBIOS_PROTOCOL                 *Smbios;
    EFI_SMBIOS_TABLE_HEADER             *Record;
    SMBIOS_TABLE_TYPE9                  *Type9Record;
    SMBIOS_TABLE_TYPE9                  *SmbiosRecord = NULL;
    CHAR8                              *OptionalStrStart;

    UINT8                               SmbiosAddType9Number;
    UINT8                               Index;

    CHAR16                              *SlotDesignation = NULL;
    UINTN                               SlotDesignationStrLen;

    Status = gBS->LocateProtocol (
                  &gEfiSmbiosProtocolGuid,
                  NULL,
                  (VOID **) &Smbios
                  );
    if (EFI_ERROR (Status)) {
        DEBUG((EFI_D_ERROR, "[%a]:[%dL] LocateProtocol Failed. Status : %r\n", __FUNCTION__, __LINE__, Status));
        return Status;
    }

    do {
        SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;
        SmbiosType = EFI_SMBIOS_TYPE_SYSTEM_SLOTS;
        Status = Smbios->GetNext (Smbios, &SmbiosHandle, &SmbiosType, &Record, NULL);
        if (!EFI_ERROR(Status)) {
            Status = Smbios->Remove (Smbios, SmbiosHandle);
            if (EFI_ERROR(Status)) {
                DEBUG((EFI_D_ERROR, "[%a]:[%dL] Remove System Slot Failed. Status : %r\n", __FUNCTION__, __LINE__, Status));
                break;
            }
        }
    } while (SmbiosHandle != SMBIOS_HANDLE_PI_RESERVED);

    SmbiosAddType9Number = OemGetPcieSlotNumber();

    for (Index = 0; Index < SmbiosAddType9Number; Index++)
    {
        if (gPcieSlotInfo[Index].Hdr.Type != EFI_SMBIOS_TYPE_SYSTEM_SLOTS)
        {
            continue;
        }

        Type9Record = &gPcieSlotInfo[Index];

        UpdateSmbiosType9Info (Type9Record);
        SlotDesignation = AllocateZeroPool ((sizeof (CHAR16)) * SMBIOS_STRING_MAX_LENGTH);
        if (NULL == SlotDesignation)
        {
            Status = EFI_OUT_OF_RESOURCES;
            DEBUG((EFI_D_ERROR, "[%a]:[%dL] AllocateZeroPool Failed. Status : %r\n", __FUNCTION__, __LINE__, Status));

            goto Exit;
        }

        SlotDesignationStrLen = UnicodeSPrint (SlotDesignation, SMBIOS_STRING_MAX_LENGTH - 1, L"PCIE Slot%d", Type9Record->SlotID);

        //
        // Two zeros following the last string.
        //
        SmbiosRecord = AllocateZeroPool(sizeof (SMBIOS_TABLE_TYPE9) + SlotDesignationStrLen + 1 + 1);
        if(NULL == SmbiosRecord)
        {
            Status = EFI_OUT_OF_RESOURCES;
            DEBUG((EFI_D_ERROR, "[%a]:[%dL] AllocateZeroPool Failed. Status : %r\n", __FUNCTION__, __LINE__, Status));

            goto Exit;
        }

        (VOID)CopyMem(SmbiosRecord, Type9Record, sizeof (SMBIOS_TABLE_TYPE9));

        SmbiosRecord->Hdr.Length = sizeof (SMBIOS_TABLE_TYPE9);

        OptionalStrStart = (CHAR8 *)(SmbiosRecord + 1);
        UnicodeStrToAsciiStr(SlotDesignation, OptionalStrStart);

        //
        // Now we have got the full smbios record, call smbios protocol to add this record.
        //
        SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;
        Status = Smbios->Add (Smbios, NULL, &SmbiosHandle, (EFI_SMBIOS_TABLE_HEADER *)SmbiosRecord);
        if(EFI_ERROR(Status))
        {
            DEBUG((EFI_D_ERROR, "[%a]:[%dL] Smbios Type09 Table Log Failed! %r \n", __FUNCTION__, __LINE__, Status));
            goto Exit;
        }

        FreePool(SmbiosRecord);
        FreePool(SlotDesignation);
    }

    return EFI_SUCCESS;

Exit:
    if(SmbiosRecord != NULL)
    {
        FreePool(SmbiosRecord);
    }

    if(SlotDesignation != NULL)
    {
        FreePool(SlotDesignation);
    }

    return Status;
}