blob: 990b31012fa4ac679e91091933d831956211d07f [file] [log] [blame]
Cheng C Yang8c3fab62019-12-19 00:51:06 +08001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
17#include "dimm.hpp"
18
19#include "mdrv2.hpp"
20
kasunath634ec6a2022-07-25 15:34:17 -070021#include <boost/algorithm/string.hpp>
kasunath2eca4fe2022-08-17 17:30:07 -070022#include <phosphor-logging/elog-errors.hpp>
kasunath634ec6a2022-07-25 15:34:17 -070023
Cheng C Yang8c3fab62019-12-19 00:51:06 +080024namespace phosphor
25{
26namespace smbios
27{
28
29using DeviceType =
30 sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::DeviceType;
31
kasunath2eca4fe2022-08-17 17:30:07 -070032using EccType =
33 sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::Ecc;
34
Cheng C Yang8c3fab62019-12-19 00:51:06 +080035static constexpr uint16_t maxOldDimmSize = 0x7fff;
36void Dimm::memoryInfoUpdate(void)
37{
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080038 uint8_t* dataIn = storage;
Cheng C Yang8c3fab62019-12-19 00:51:06 +080039
40 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
41
42 if (dataIn == nullptr)
43 {
44 return;
45 }
46 for (uint8_t index = 0; index < dimmNum; index++)
47 {
48 dataIn = smbiosNextPtr(dataIn);
49 if (dataIn == nullptr)
50 {
51 return;
52 }
53 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
54 if (dataIn == nullptr)
55 {
56 return;
57 }
58 }
59
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080060 auto memoryInfo = reinterpret_cast<struct MemoryInfo*>(dataIn);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080061
62 memoryDataWidth(memoryInfo->dataWidth);
63
64 if (memoryInfo->size == maxOldDimmSize)
65 {
66 dimmSizeExt(memoryInfo->extendedSize);
67 }
68 else
69 {
70 dimmSize(memoryInfo->size);
71 }
72
73 dimmDeviceLocator(memoryInfo->deviceLocator, memoryInfo->length, dataIn);
74 dimmType(memoryInfo->memoryType);
75 dimmTypeDetail(memoryInfo->typeDetail);
76 maxMemorySpeedInMhz(memoryInfo->speed);
77 dimmManufacturer(memoryInfo->manufacturer, memoryInfo->length, dataIn);
78 dimmSerialNum(memoryInfo->serialNum, memoryInfo->length, dataIn);
79 dimmPartNum(memoryInfo->partNum, memoryInfo->length, dataIn);
80 memoryAttributes(memoryInfo->attributes);
81 memoryConfiguredSpeedInMhz(memoryInfo->confClockSpeed);
82
kasunath2eca4fe2022-08-17 17:30:07 -070083 updateEccType(memoryInfo->phyArrayHandle);
84
Jie Yange7cf3192021-08-20 11:21:43 -070085 if (!motherboardPath.empty())
86 {
87 std::vector<std::tuple<std::string, std::string, std::string>> assocs;
88 assocs.emplace_back("chassis", "memories", motherboardPath);
89 association::associations(assocs);
90 }
91
Cheng C Yang8c3fab62019-12-19 00:51:06 +080092 return;
93}
94
kasunath2eca4fe2022-08-17 17:30:07 -070095void Dimm::updateEccType(uint16_t exPhyArrayHandle)
96{
97 uint8_t* dataIn = storage;
98
99 while (dataIn != nullptr)
100 {
101 dataIn = getSMBIOSTypePtr(dataIn, physicalMemoryArrayType);
102 if (dataIn == nullptr)
103 {
104 phosphor::logging::log<phosphor::logging::level::ERR>(
105 "Failed to get SMBIOS table type-16 data.");
106 return;
107 }
108
109 auto info = reinterpret_cast<struct PhysicalMemoryArrayInfo*>(dataIn);
110 if (info->handle == exPhyArrayHandle)
111 {
112 std::map<uint8_t, EccType>::const_iterator it =
113 dimmEccTypeMap.find(info->memoryErrorCorrection);
114 if (it == dimmEccTypeMap.end())
115 {
116 ecc(EccType::NoECC);
117 }
118 else
119 {
120 ecc(it->second);
121 }
122 return;
123 }
124
125 dataIn = smbiosNextPtr(dataIn);
126 }
127 phosphor::logging::log<phosphor::logging::level::ERR>(
128 "Failed find the corresponding SMBIOS table type-16 data for dimm:",
129 phosphor::logging::entry("DIMM:%d", dimmNum));
130}
131
132EccType Dimm::ecc(EccType value)
133{
134 return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::ecc(
135 value);
136}
137
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800138uint16_t Dimm::memoryDataWidth(uint16_t value)
139{
140 return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
141 memoryDataWidth(value);
142}
143
144static constexpr uint16_t baseNewVersionDimmSize = 0x8000;
145static constexpr uint16_t dimmSizeUnit = 1024;
146void Dimm::dimmSize(const uint16_t size)
147{
Jason M. Billse7770992021-05-14 13:24:33 -0700148 size_t result = size & maxOldDimmSize;
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800149 if (0 == (size & baseNewVersionDimmSize))
150 {
151 result = result * dimmSizeUnit;
152 }
153 memorySizeInKB(result);
154}
155
Jason M. Billse7770992021-05-14 13:24:33 -0700156void Dimm::dimmSizeExt(size_t size)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800157{
158 size = size * dimmSizeUnit;
159 memorySizeInKB(size);
160}
161
Jason M. Billse7770992021-05-14 13:24:33 -0700162size_t Dimm::memorySizeInKB(size_t value)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800163{
164 return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
165 memorySizeInKB(value);
166}
167
168void Dimm::dimmDeviceLocator(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800169 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800170{
171 std::string result = positionToString(positionNum, structLen, dataIn);
172
173 memoryDeviceLocator(result);
Jie Yang31720392021-07-22 21:45:45 -0700174
175 locationCode(result);
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800176}
177
178std::string Dimm::memoryDeviceLocator(std::string value)
179{
180 return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
181 memoryDeviceLocator(value);
182}
183
184void Dimm::dimmType(const uint8_t type)
185{
186 std::map<uint8_t, DeviceType>::const_iterator it = dimmTypeTable.find(type);
187 if (it == dimmTypeTable.end())
188 {
189 memoryType(DeviceType::Unknown);
190 }
191 else
192 {
193 memoryType(it->second);
194 }
195}
196
197DeviceType Dimm::memoryType(DeviceType value)
198{
199 return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
200 memoryType(value);
201}
202
203void Dimm::dimmTypeDetail(uint16_t detail)
204{
205 std::string result;
206 for (uint8_t index = 0; index < (8 * sizeof(detail)); index++)
207 {
208 if (detail & 0x01)
209 {
210 result += detailTable[index];
211 }
212 detail >>= 1;
213 }
214 memoryTypeDetail(result);
215}
216
217std::string Dimm::memoryTypeDetail(std::string value)
218{
219 return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
220 memoryTypeDetail(value);
221}
222
223uint16_t Dimm::maxMemorySpeedInMhz(uint16_t value)
224{
225 return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
226 maxMemorySpeedInMhz(value);
227}
228
229void Dimm::dimmManufacturer(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800230 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800231{
232 std::string result = positionToString(positionNum, structLen, dataIn);
233
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530234 bool val = true;
235 if (result == "NO DIMM")
236 {
237 val = false;
238
239 // No dimm presence so making manufacturer value as "" (instead of
240 // NO DIMM - as there won't be any manufacturer for DIMM which is not
241 // present).
242 result = "";
243 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800244 manufacturer(result);
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530245 present(val);
Tim Leedc469c72021-07-20 10:55:58 +0800246 functional(val);
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800247}
248
249std::string Dimm::manufacturer(std::string value)
250{
251 return sdbusplus::xyz::openbmc_project::Inventory::Decorator::server::
252 Asset::manufacturer(value);
253}
254
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530255bool Dimm::present(bool value)
256{
257 return sdbusplus::xyz::openbmc_project::Inventory::server::Item::present(
258 value);
259}
260
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800261void Dimm::dimmSerialNum(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800262 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800263{
264 std::string result = positionToString(positionNum, structLen, dataIn);
265
266 serialNumber(result);
267}
268
269std::string Dimm::serialNumber(std::string value)
270{
271 return sdbusplus::xyz::openbmc_project::Inventory::Decorator::server::
272 Asset::serialNumber(value);
273}
274
275void Dimm::dimmPartNum(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800276 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800277{
278 std::string result = positionToString(positionNum, structLen, dataIn);
279
kasunath634ec6a2022-07-25 15:34:17 -0700280 // Part number could contain spaces at the end. Eg: "abcd123 ". Since its
281 // unnecessary, we should remove them.
282 boost::algorithm::trim_right(result);
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800283 partNumber(result);
284}
285
286std::string Dimm::partNumber(std::string value)
287{
288 return sdbusplus::xyz::openbmc_project::Inventory::Decorator::server::
289 Asset::partNumber(value);
290}
291
Jie Yang31720392021-07-22 21:45:45 -0700292std::string Dimm::locationCode(std::string value)
293{
294 return sdbusplus::xyz::openbmc_project::Inventory::Decorator::server::
295 LocationCode::locationCode(value);
296}
297
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800298uint8_t Dimm::memoryAttributes(uint8_t value)
299{
300 return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
301 memoryAttributes(value);
302}
303
304uint16_t Dimm::memoryConfiguredSpeedInMhz(uint16_t value)
305{
306 return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
307 memoryConfiguredSpeedInMhz(value);
308}
309
Tim Leedc469c72021-07-20 10:55:58 +0800310bool Dimm::functional(bool value)
311{
312 return sdbusplus::xyz::openbmc_project::State::Decorator::server::
313 OperationalStatus::functional(value);
314}
315
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800316} // namespace smbios
317} // namespace phosphor