blob: 14a498449986423252cadd0d09cdb7867234c4b3 [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
Jayaprakash Mutyalaa1ff2442023-06-15 07:10:07 +000024#include <regex>
25
Cheng C Yang8c3fab62019-12-19 00:51:06 +080026namespace phosphor
27{
28namespace smbios
29{
30
John Edward Broadbentefd41542022-12-13 16:39:18 -080031#ifdef DIMM_ONLY_LOCATOR
32bool onlyDimmLocationCode = true;
33#else
34bool onlyDimmLocationCode = false;
35#endif
36
Cheng C Yang8c3fab62019-12-19 00:51:06 +080037using DeviceType =
Jason M. Bills33ae81f2023-04-26 09:06:08 -070038 sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::DeviceType;
Cheng C Yang8c3fab62019-12-19 00:51:06 +080039
kasunath2eca4fe2022-08-17 17:30:07 -070040using EccType =
Jason M. Bills33ae81f2023-04-26 09:06:08 -070041 sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::Ecc;
kasunath2eca4fe2022-08-17 17:30:07 -070042
Cheng C Yang8c3fab62019-12-19 00:51:06 +080043static constexpr uint16_t maxOldDimmSize = 0x7fff;
Brandon Kim5a122a62023-05-04 04:25:03 +000044void Dimm::memoryInfoUpdate(uint8_t* smbiosTableStorage,
45 const std::string& motherboard)
Cheng C Yang8c3fab62019-12-19 00:51:06 +080046{
Brandon Kim5a122a62023-05-04 04:25:03 +000047 storage = smbiosTableStorage;
48 motherboardPath = motherboard;
49
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080050 uint8_t* dataIn = storage;
Cheng C Yang8c3fab62019-12-19 00:51:06 +080051
52 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
53
54 if (dataIn == nullptr)
55 {
56 return;
57 }
58 for (uint8_t index = 0; index < dimmNum; index++)
59 {
60 dataIn = smbiosNextPtr(dataIn);
61 if (dataIn == nullptr)
62 {
63 return;
64 }
65 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
66 if (dataIn == nullptr)
67 {
68 return;
69 }
70 }
71
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080072 auto memoryInfo = reinterpret_cast<struct MemoryInfo*>(dataIn);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080073
74 memoryDataWidth(memoryInfo->dataWidth);
Joseph Fu410bbc22023-07-12 19:01:04 +080075 memoryTotalWidth(memoryInfo->totalWidth);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080076
77 if (memoryInfo->size == maxOldDimmSize)
78 {
79 dimmSizeExt(memoryInfo->extendedSize);
80 }
81 else
82 {
83 dimmSize(memoryInfo->size);
84 }
Tom Tung39cc3682023-03-15 04:44:04 +080085 // If the size is 0, no memory device is installed in the socket.
86 const auto isDimmPresent = memoryInfo->size > 0;
87 present(isDimmPresent);
88 functional(isDimmPresent);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080089
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +000090 dimmDeviceLocator(memoryInfo->bankLocator, memoryInfo->deviceLocator,
91 memoryInfo->length, dataIn);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080092 dimmType(memoryInfo->memoryType);
93 dimmTypeDetail(memoryInfo->typeDetail);
94 maxMemorySpeedInMhz(memoryInfo->speed);
95 dimmManufacturer(memoryInfo->manufacturer, memoryInfo->length, dataIn);
96 dimmSerialNum(memoryInfo->serialNum, memoryInfo->length, dataIn);
97 dimmPartNum(memoryInfo->partNum, memoryInfo->length, dataIn);
98 memoryAttributes(memoryInfo->attributes);
Jayaprakash Mutyalaa1ff2442023-06-15 07:10:07 +000099 dimmMedia(memoryInfo->memoryTechnology);
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800100 memoryConfiguredSpeedInMhz(memoryInfo->confClockSpeed);
101
kasunath2eca4fe2022-08-17 17:30:07 -0700102 updateEccType(memoryInfo->phyArrayHandle);
103
Jie Yange7cf3192021-08-20 11:21:43 -0700104 if (!motherboardPath.empty())
105 {
106 std::vector<std::tuple<std::string, std::string, std::string>> assocs;
107 assocs.emplace_back("chassis", "memories", motherboardPath);
108 association::associations(assocs);
109 }
110
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800111 return;
112}
113
kasunath2eca4fe2022-08-17 17:30:07 -0700114void Dimm::updateEccType(uint16_t exPhyArrayHandle)
115{
116 uint8_t* dataIn = storage;
117
118 while (dataIn != nullptr)
119 {
120 dataIn = getSMBIOSTypePtr(dataIn, physicalMemoryArrayType);
121 if (dataIn == nullptr)
122 {
123 phosphor::logging::log<phosphor::logging::level::ERR>(
124 "Failed to get SMBIOS table type-16 data.");
125 return;
126 }
127
128 auto info = reinterpret_cast<struct PhysicalMemoryArrayInfo*>(dataIn);
129 if (info->handle == exPhyArrayHandle)
130 {
131 std::map<uint8_t, EccType>::const_iterator it =
132 dimmEccTypeMap.find(info->memoryErrorCorrection);
133 if (it == dimmEccTypeMap.end())
134 {
135 ecc(EccType::NoECC);
136 }
137 else
138 {
139 ecc(it->second);
140 }
141 return;
142 }
143
144 dataIn = smbiosNextPtr(dataIn);
145 }
146 phosphor::logging::log<phosphor::logging::level::ERR>(
147 "Failed find the corresponding SMBIOS table type-16 data for dimm:",
148 phosphor::logging::entry("DIMM:%d", dimmNum));
149}
150
151EccType Dimm::ecc(EccType value)
152{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700153 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::ecc(
kasunath2eca4fe2022-08-17 17:30:07 -0700154 value);
155}
156
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800157uint16_t Dimm::memoryDataWidth(uint16_t value)
158{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700159 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800160 memoryDataWidth(value);
161}
162
Joseph Fu410bbc22023-07-12 19:01:04 +0800163uint16_t Dimm::memoryTotalWidth(uint16_t value)
164{
165 return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
166 memoryTotalWidth(value);
167}
168
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800169static constexpr uint16_t baseNewVersionDimmSize = 0x8000;
170static constexpr uint16_t dimmSizeUnit = 1024;
171void Dimm::dimmSize(const uint16_t size)
172{
Joseph Fu0b1d9422023-03-31 17:47:12 +0800173 uint32_t result = size & maxOldDimmSize;
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800174 if (0 == (size & baseNewVersionDimmSize))
175 {
176 result = result * dimmSizeUnit;
177 }
178 memorySizeInKB(result);
179}
180
Joseph Fu0b1d9422023-03-31 17:47:12 +0800181void Dimm::dimmSizeExt(uint32_t size)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800182{
183 size = size * dimmSizeUnit;
184 memorySizeInKB(size);
185}
186
Jason M. Billse7770992021-05-14 13:24:33 -0700187size_t Dimm::memorySizeInKB(size_t value)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800188{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700189 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800190 memorySizeInKB(value);
191}
192
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000193void Dimm::dimmDeviceLocator(const uint8_t bankLocatorPositionNum,
194 const uint8_t deviceLocatorPositionNum,
195 const uint8_t structLen, uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800196{
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500197 std::string deviceLocator = positionToString(deviceLocatorPositionNum,
198 structLen, dataIn);
199 std::string bankLocator = positionToString(bankLocatorPositionNum,
200 structLen, dataIn);
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000201
202 std::string result;
John Edward Broadbentefd41542022-12-13 16:39:18 -0800203 if (bankLocator.empty() || onlyDimmLocationCode)
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000204 {
John Edward Broadbentefd41542022-12-13 16:39:18 -0800205 result = deviceLocator;
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000206 }
207 else
208 {
John Edward Broadbentefd41542022-12-13 16:39:18 -0800209 result = bankLocator + " " + deviceLocator;
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000210 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800211
212 memoryDeviceLocator(result);
Jie Yang31720392021-07-22 21:45:45 -0700213
214 locationCode(result);
Jayaprakash Mutyalaa1ff2442023-06-15 07:10:07 +0000215 const std::string substrCpu = "CPU";
216 auto cpuPos = deviceLocator.find(substrCpu);
217
218 if (cpuPos != std::string::npos)
219 {
220 std::string socketString =
221 deviceLocator.substr(cpuPos + substrCpu.length(), 1);
222 try
223 {
224 uint8_t socketNum =
225 static_cast<uint8_t>(std::stoi(socketString) + 1);
226 socket(socketNum);
227 }
228 catch (const sdbusplus::exception_t& ex)
229 {
230 phosphor::logging::log<phosphor::logging::level::ERR>(
231 "std::stoi operation failed ",
232 phosphor::logging::entry("ERROR=%s", ex.what()));
233 }
234 }
235
236 const std::string substrDimm = "DIMM";
237 auto dimmPos = deviceLocator.find(substrDimm);
238
239 if (dimmPos != std::string::npos)
240 {
241 std::string slotString =
242 deviceLocator.substr(dimmPos + substrDimm.length() + 1);
243 /* slotString is extracted from substrDimm (DIMM_A) if slotString is
244 * single alphabet like A, B , C.. then assign ASCII value of slotString
245 * to slot */
246 if ((std::regex_match(slotString, std::regex("^[A-Za-z]+$"))) &&
247 (slotString.length() == 1))
248 {
249 slot(static_cast<uint8_t>(toupper(slotString[0])));
250 }
251 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800252}
253
254std::string Dimm::memoryDeviceLocator(std::string value)
255{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700256 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800257 memoryDeviceLocator(value);
258}
259
260void Dimm::dimmType(const uint8_t type)
261{
262 std::map<uint8_t, DeviceType>::const_iterator it = dimmTypeTable.find(type);
263 if (it == dimmTypeTable.end())
264 {
265 memoryType(DeviceType::Unknown);
266 }
267 else
268 {
269 memoryType(it->second);
270 }
271}
272
273DeviceType Dimm::memoryType(DeviceType value)
274{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700275 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800276 memoryType(value);
277}
278
Jayaprakash Mutyalaa1ff2442023-06-15 07:10:07 +0000279void Dimm::dimmMedia(const uint8_t type)
280{
281 std::map<uint8_t, MemoryTechType>::const_iterator it =
282 dimmMemoryTechTypeMap.find(type);
283 if (it == dimmMemoryTechTypeMap.end())
284 {
285 memoryMedia(MemoryTechType::Unknown);
286 }
287 else
288 {
289 memoryMedia(it->second);
290 }
291}
292
293MemoryTechType Dimm::memoryMedia(MemoryTechType value)
294{
295 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
296 memoryMedia(value);
297}
298
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800299void Dimm::dimmTypeDetail(uint16_t detail)
300{
301 std::string result;
302 for (uint8_t index = 0; index < (8 * sizeof(detail)); index++)
303 {
304 if (detail & 0x01)
305 {
306 result += detailTable[index];
307 }
308 detail >>= 1;
309 }
310 memoryTypeDetail(result);
311}
312
313std::string Dimm::memoryTypeDetail(std::string value)
314{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700315 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800316 memoryTypeDetail(value);
317}
318
319uint16_t Dimm::maxMemorySpeedInMhz(uint16_t value)
320{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700321 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800322 maxMemorySpeedInMhz(value);
323}
324
325void Dimm::dimmManufacturer(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800326 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800327{
328 std::string result = positionToString(positionNum, structLen, dataIn);
329
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530330 if (result == "NO DIMM")
331 {
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530332 // No dimm presence so making manufacturer value as "" (instead of
333 // NO DIMM - as there won't be any manufacturer for DIMM which is not
334 // present).
335 result = "";
336 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800337 manufacturer(result);
338}
339
340std::string Dimm::manufacturer(std::string value)
341{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700342 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800343 Asset::manufacturer(value);
344}
345
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530346bool Dimm::present(bool value)
347{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700348 return sdbusplus::server::xyz::openbmc_project::inventory::Item::present(
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530349 value);
350}
351
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800352void Dimm::dimmSerialNum(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800353 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800354{
355 std::string result = positionToString(positionNum, structLen, dataIn);
356
357 serialNumber(result);
358}
359
360std::string Dimm::serialNumber(std::string value)
361{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700362 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800363 Asset::serialNumber(value);
364}
365
366void Dimm::dimmPartNum(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800367 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800368{
369 std::string result = positionToString(positionNum, structLen, dataIn);
370
kasunath634ec6a2022-07-25 15:34:17 -0700371 // Part number could contain spaces at the end. Eg: "abcd123 ". Since its
372 // unnecessary, we should remove them.
373 boost::algorithm::trim_right(result);
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800374 partNumber(result);
375}
376
377std::string Dimm::partNumber(std::string value)
378{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700379 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800380 Asset::partNumber(value);
381}
382
Jie Yang31720392021-07-22 21:45:45 -0700383std::string Dimm::locationCode(std::string value)
384{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700385 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Jie Yang31720392021-07-22 21:45:45 -0700386 LocationCode::locationCode(value);
387}
388
George Liu036374a2023-06-15 08:47:46 +0800389size_t Dimm::memoryAttributes(size_t value)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800390{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700391 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800392 memoryAttributes(value);
393}
394
Jayaprakash Mutyalaa1ff2442023-06-15 07:10:07 +0000395uint8_t Dimm::slot(uint8_t value)
396{
397 return sdbusplus::server::xyz::openbmc_project::inventory::item::dimm::
398 MemoryLocation::slot(value);
399}
400
401uint8_t Dimm::socket(uint8_t value)
402{
403 return sdbusplus::server::xyz::openbmc_project::inventory::item::dimm::
404 MemoryLocation::socket(value);
405}
406
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800407uint16_t Dimm::memoryConfiguredSpeedInMhz(uint16_t value)
408{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700409 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800410 memoryConfiguredSpeedInMhz(value);
411}
412
Tim Leedc469c72021-07-20 10:55:58 +0800413bool Dimm::functional(bool value)
414{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700415 return sdbusplus::server::xyz::openbmc_project::state::decorator::
Tim Leedc469c72021-07-20 10:55:58 +0800416 OperationalStatus::functional(value);
417}
418
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800419} // namespace smbios
420} // namespace phosphor