blob: 57d347711bf407d3527820f88dc7f91f1b2322e9 [file] [log] [blame]
Feist, Jamesc95cf672019-08-29 16:10:35 -07001/*
2// Copyright (c) 2019 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 "utils.hpp"
18
19#include <boost/algorithm/string/replace.hpp>
20#include <boost/asio/steady_timer.hpp>
James Feist8675a912019-10-16 14:36:58 -070021#include <boost/container/flat_set.hpp>
James Feist0b236ab2019-10-02 09:09:16 -070022#include <filesystem>
James Feist8675a912019-10-16 14:36:58 -070023#include <fstream>
Feist, Jamesc95cf672019-08-29 16:10:35 -070024#include <iostream>
25#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <sdbusplus/bus/match.hpp>
28#include <string>
James Feist45772222019-09-27 10:38:08 -070029#include <utility>
Feist, Jamesc95cf672019-08-29 16:10:35 -070030
31extern "C" {
32#include <i2c/smbus.h>
33#include <linux/i2c-dev.h>
34}
35
36constexpr const char* configType =
37 "xyz.openbmc_project.Configuration.Intel_HSBP_CPLD";
James Feistdb2e0e72019-10-07 16:34:06 -070038constexpr const char* busName = "xyz.openbmc_project.HsbpManager";
Feist, Jamesc95cf672019-08-29 16:10:35 -070039
James Feist45772222019-09-27 10:38:08 -070040constexpr size_t scanRateSeconds = 5;
41constexpr size_t maxDrives = 8; // only 1 byte alloted
42
Feist, Jamesc95cf672019-08-29 16:10:35 -070043boost::asio::io_context io;
44auto conn = std::make_shared<sdbusplus::asio::connection>(io);
45sdbusplus::asio::object_server objServer(conn);
46
James Feist45772222019-09-27 10:38:08 -070047static std::string zeroPad(const uint8_t val)
48{
49 std::ostringstream version;
50 version << std::setw(2) << std::setfill('0') << static_cast<size_t>(val);
51 return version.str();
52}
53
James Feist0b236ab2019-10-02 09:09:16 -070054struct Mux
55{
James Feist8675a912019-10-16 14:36:58 -070056 Mux(size_t busIn, size_t addressIn, size_t channelsIn, size_t indexIn) :
57 bus(busIn), address(addressIn), channels(channelsIn), index(indexIn)
James Feist0b236ab2019-10-02 09:09:16 -070058 {
59 }
60 size_t bus;
61 size_t address;
James Feist8675a912019-10-16 14:36:58 -070062 size_t channels;
63 size_t index;
64
65 // to sort in the flat set
66 bool operator<(const Mux& rhs) const
67 {
68 return index < rhs.index;
69 }
James Feist0b236ab2019-10-02 09:09:16 -070070};
James Feist09dd2312019-10-09 09:29:03 -070071
72enum class BlinkPattern : uint8_t
73{
74 off = 0x0,
75 error = 0x2,
76 terminate = 0x3
77};
78
79struct Led : std::enable_shared_from_this<Led>
80{
81 // led pattern addresses start at 0x10
82 Led(const std::string& path, size_t index, int fd) :
83 address(static_cast<uint8_t>(index + 0x10)), file(fd),
84 ledInterface(objServer.add_interface(path, ledGroup::interface))
85 {
86 if (index >= maxDrives)
87 {
88 throw std::runtime_error("Invalid drive index");
89 }
90
91 if (!set(BlinkPattern::off))
92 {
93 std::cerr << "Cannot initialize LED " << path << "\n";
94 }
95 }
96
97 // this has to be called outside the constructor for shared_from_this to
98 // work
99 void createInterface(void)
100 {
101 std::shared_ptr<Led> self = shared_from_this();
102
103 ledInterface->register_property(
104 ledGroup::asserted, false, [self](const bool req, bool& val) {
105 if (req == val)
106 {
107 return 1;
108 }
James Feist9f6565d2019-10-09 13:15:13 -0700109
110 if (!isPowerOn())
111 {
112 std::cerr << "Can't change blink state when power is off\n";
113 throw std::runtime_error(
114 "Can't change blink state when power is off");
115 }
James Feist09dd2312019-10-09 09:29:03 -0700116 BlinkPattern pattern =
117 req ? BlinkPattern::error : BlinkPattern::terminate;
118 if (!self->set(pattern))
119 {
James Feist9f6565d2019-10-09 13:15:13 -0700120 std::cerr << "Can't change blink pattern\n";
James Feist09dd2312019-10-09 09:29:03 -0700121 throw std::runtime_error("Cannot set blink pattern");
122 }
123 val = req;
124 return 1;
125 });
126 ledInterface->initialize();
127 }
128
129 virtual ~Led()
130 {
131 objServer.remove_interface(ledInterface);
132 }
133
134 bool set(BlinkPattern pattern)
135 {
136 int ret = i2c_smbus_write_byte_data(file, address,
137 static_cast<uint8_t>(pattern));
138 return ret >= 0;
139 }
140
141 uint8_t address;
142 int file;
143 std::shared_ptr<sdbusplus::asio::dbus_interface> ledInterface;
144};
145
James Feist45772222019-09-27 10:38:08 -0700146struct Drive
147{
James Feist42b49c12019-10-29 15:18:43 -0700148 Drive(size_t driveIndex, bool present, bool isOperational, bool nvme,
James Feist244f3232019-09-27 15:15:14 -0700149 bool rebuilding) :
James Feist42b49c12019-10-29 15:18:43 -0700150 isNvme(nvme),
James Feiste8818522019-11-04 13:36:10 -0800151 isPresent(present), index(driveIndex)
James Feist45772222019-09-27 10:38:08 -0700152 {
153 constexpr const char* basePath =
154 "/xyz/openbmc_project/inventory/item/drive/Drive_";
155 itemIface = objServer.add_interface(
156 basePath + std::to_string(driveIndex), inventory::interface);
157 itemIface->register_property("Present", isPresent);
158 itemIface->register_property("PrettyName",
159 "Drive " + std::to_string(driveIndex));
160 itemIface->initialize();
161 operationalIface = objServer.add_interface(
James Feist244f3232019-09-27 15:15:14 -0700162 itemIface->get_object_path(),
James Feist45772222019-09-27 10:38:08 -0700163 "xyz.openbmc_project.State.Decorator.OperationalStatus");
James Feist42b49c12019-10-29 15:18:43 -0700164
165 operationalIface->register_property(
166 "Functional", isOperational,
167 [this](const bool req, bool& property) {
168 if (!isPresent)
169 {
170 return 0;
171 }
172 if (property == req)
173 {
174 return 1;
175 }
176 property = req;
177 if (req)
178 {
179 clearFailed();
180 return 1;
181 }
182 markFailed();
183 return 1;
184 });
185
James Feist45772222019-09-27 10:38:08 -0700186 operationalIface->initialize();
James Feist244f3232019-09-27 15:15:14 -0700187 rebuildingIface = objServer.add_interface(
188 itemIface->get_object_path(), "xyz.openbmc_project.State.Drive");
189 rebuildingIface->register_property("Rebuilding", rebuilding);
190 rebuildingIface->initialize();
James Feist8675a912019-10-16 14:36:58 -0700191 driveIface =
192 objServer.add_interface(itemIface->get_object_path(),
193 "xyz.openbmc_project.Inventory.Item.Drive");
194 driveIface->initialize();
James Feist42b49c12019-10-29 15:18:43 -0700195 associations = objServer.add_interface(itemIface->get_object_path(),
196 association::interface);
197 associations->register_property("Associations",
198 std::vector<Association>{});
199 associations->initialize();
200
201 if (isPresent && (!isOperational || rebuilding))
202 {
203 markFailed();
204 }
James Feist45772222019-09-27 10:38:08 -0700205 }
James Feist09dd2312019-10-09 09:29:03 -0700206 virtual ~Drive()
James Feist45772222019-09-27 10:38:08 -0700207 {
208 objServer.remove_interface(itemIface);
209 objServer.remove_interface(operationalIface);
James Feist244f3232019-09-27 15:15:14 -0700210 objServer.remove_interface(rebuildingIface);
James Feist8675a912019-10-16 14:36:58 -0700211 objServer.remove_interface(assetIface);
James Feistdb2e0e72019-10-07 16:34:06 -0700212 objServer.remove_interface(driveIface);
James Feist42b49c12019-10-29 15:18:43 -0700213 objServer.remove_interface(associations);
James Feist0b236ab2019-10-02 09:09:16 -0700214 }
215
James Feist8675a912019-10-16 14:36:58 -0700216 void createAsset(
217 const boost::container::flat_map<std::string, std::string>& data)
James Feist0b236ab2019-10-02 09:09:16 -0700218 {
James Feist8675a912019-10-16 14:36:58 -0700219 if (assetIface != nullptr)
James Feist0b236ab2019-10-02 09:09:16 -0700220 {
221 return;
222 }
James Feist8675a912019-10-16 14:36:58 -0700223 assetIface = objServer.add_interface(
James Feist0b236ab2019-10-02 09:09:16 -0700224 itemIface->get_object_path(),
James Feist8675a912019-10-16 14:36:58 -0700225 "xyz.openbmc_project.Inventory.Decorator.Asset");
226 for (const auto& [key, value] : data)
James Feistdb2e0e72019-10-07 16:34:06 -0700227 {
James Feist8675a912019-10-16 14:36:58 -0700228 assetIface->register_property(key, value);
James Feistd86629c2020-04-23 10:07:25 -0700229 if (key == "SerialNumber")
230 {
231 serialNumber = value;
Johnathan Mantey7045b4b2020-06-19 09:24:49 -0700232 serialNumberInitialized = true;
James Feistd86629c2020-04-23 10:07:25 -0700233 }
James Feistdb2e0e72019-10-07 16:34:06 -0700234 }
James Feist8675a912019-10-16 14:36:58 -0700235 assetIface->initialize();
James Feistdb2e0e72019-10-07 16:34:06 -0700236 }
237
James Feist42b49c12019-10-29 15:18:43 -0700238 void markFailed(void)
239 {
240 // todo: maybe look this up via mapper
241 constexpr const char* globalInventoryPath =
242 "/xyz/openbmc_project/CallbackManager";
243
244 if (!isPresent)
245 {
246 return;
247 }
248
249 operationalIface->set_property("Functional", false);
250 std::vector<Association> warning = {
251 {"", "warning", globalInventoryPath}};
252 associations->set_property("Associations", warning);
James Feiste8818522019-11-04 13:36:10 -0800253 logDriveError("Drive " + std::to_string(index));
James Feist42b49c12019-10-29 15:18:43 -0700254 }
255
256 void clearFailed(void)
257 {
258 operationalIface->set_property("Functional", true);
259 associations->set_property("Associations", std::vector<Association>{});
260 }
261
James Feistd86629c2020-04-23 10:07:25 -0700262 void setPresent(bool set)
James Feiste8818522019-11-04 13:36:10 -0800263 {
264 // nvme drives get detected by their fru
James Feistd86629c2020-04-23 10:07:25 -0700265 if (set == isPresent)
James Feiste8818522019-11-04 13:36:10 -0800266 {
267 return;
268 }
269 itemIface->set_property("Present", set);
270 isPresent = set;
James Feistd86629c2020-04-23 10:07:25 -0700271 }
272
273 void logPresent()
274 {
Johnathan Mantey7045b4b2020-06-19 09:24:49 -0700275 if (isNvme && !serialNumberInitialized)
James Feiste8818522019-11-04 13:36:10 -0800276 {
Johnathan Mantey7045b4b2020-06-19 09:24:49 -0700277 // wait until NVMe asset is updated to include the serial number
278 // from the NVMe drive
James Feistd86629c2020-04-23 10:07:25 -0700279 return;
James Feiste8818522019-11-04 13:36:10 -0800280 }
Johnathan Mantey7045b4b2020-06-19 09:24:49 -0700281
282 if (!isPresent && loggedPresent)
283 {
284 loggedPresent = false;
285 logDeviceRemoved("Drive", std::to_string(index), serialNumber);
286 serialNumber = "N/A";
287 serialNumberInitialized = false;
288 return;
289 }
290 else if (isPresent && !loggedPresent)
291 {
292 loggedPresent = true;
293 logDeviceAdded("Drive", std::to_string(index), serialNumber);
294 }
James Feiste8818522019-11-04 13:36:10 -0800295 }
296
James Feist45772222019-09-27 10:38:08 -0700297 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface;
298 std::shared_ptr<sdbusplus::asio::dbus_interface> operationalIface;
James Feist244f3232019-09-27 15:15:14 -0700299 std::shared_ptr<sdbusplus::asio::dbus_interface> rebuildingIface;
James Feist8675a912019-10-16 14:36:58 -0700300 std::shared_ptr<sdbusplus::asio::dbus_interface> assetIface;
James Feistdb2e0e72019-10-07 16:34:06 -0700301 std::shared_ptr<sdbusplus::asio::dbus_interface> driveIface;
James Feist42b49c12019-10-29 15:18:43 -0700302 std::shared_ptr<sdbusplus::asio::dbus_interface> associations;
James Feistdb2e0e72019-10-07 16:34:06 -0700303
James Feist45772222019-09-27 10:38:08 -0700304 bool isNvme;
James Feist42b49c12019-10-29 15:18:43 -0700305 bool isPresent;
James Feiste8818522019-11-04 13:36:10 -0800306 size_t index;
James Feistd86629c2020-04-23 10:07:25 -0700307 std::string serialNumber = "N/A";
Johnathan Mantey7045b4b2020-06-19 09:24:49 -0700308 bool serialNumberInitialized = false;
James Feistd86629c2020-04-23 10:07:25 -0700309 bool loggedPresent = false;
James Feist45772222019-09-27 10:38:08 -0700310};
311
James Feistd86629c2020-04-23 10:07:25 -0700312struct Backplane : std::enable_shared_from_this<Backplane>
Feist, Jamesc95cf672019-08-29 16:10:35 -0700313{
314
James Feist45772222019-09-27 10:38:08 -0700315 Backplane(size_t busIn, size_t addressIn, size_t backplaneIndexIn,
316 const std::string& nameIn) :
317 bus(busIn),
318 address(addressIn), backplaneIndex(backplaneIndexIn - 1), name(nameIn),
James Feistd86629c2020-04-23 10:07:25 -0700319 timer(boost::asio::steady_timer(io)),
James Feist8675a912019-10-16 14:36:58 -0700320 muxes(std::make_shared<boost::container::flat_set<Mux>>())
Feist, Jamesc95cf672019-08-29 16:10:35 -0700321 {
322 }
James Feistd0d36f12019-11-21 10:19:44 -0800323 void populateAsset(const std::string& rootPath, const std::string& busname)
324 {
325 conn->async_method_call(
326 [assetIface{assetInterface}, hsbpIface{hsbpItemIface}](
327 const boost::system::error_code ec,
328 const boost::container::flat_map<
329 std::string, std::variant<std::string>>& values) mutable {
330 if (ec)
331 {
332 std::cerr
333 << "Error getting asset tag from HSBP configuration\n";
334
335 return;
336 }
337 assetIface = objServer.add_interface(
338 hsbpIface->get_object_path(), assetTag);
339 for (const auto& [key, value] : values)
340 {
341 const std::string* ptr = std::get_if<std::string>(&value);
342 if (ptr == nullptr)
343 {
344 std::cerr << key << " Invalid type!\n";
345 continue;
346 }
347 assetIface->register_property(key, *ptr);
348 }
349 assetIface->initialize();
350 },
351 busname, rootPath, "org.freedesktop.DBus.Properties", "GetAll",
352 assetTag);
353 }
354
355 void run(const std::string& rootPath, const std::string& busname)
Feist, Jamesc95cf672019-08-29 16:10:35 -0700356 {
James Feist09dd2312019-10-09 09:29:03 -0700357 file = open(("/dev/i2c-" + std::to_string(bus)).c_str(),
358 O_RDWR | O_CLOEXEC);
Feist, Jamesc95cf672019-08-29 16:10:35 -0700359 if (file < 0)
360 {
361 std::cerr << "unable to open bus " << bus << "\n";
362 return;
363 }
364
365 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
366 {
367 std::cerr << "unable to set address to " << address << "\n";
368 return;
369 }
370
James Feist45772222019-09-27 10:38:08 -0700371 if (!getPresent())
372 {
373 std::cerr << "Cannot detect CPLD\n";
374 return;
375 }
376
377 getBootVer(bootVer);
378 getFPGAVer(fpgaVer);
379 getSecurityRev(securityRev);
380 std::string dbusName = boost::replace_all_copy(name, " ", "_");
Feist, Jamesc95cf672019-08-29 16:10:35 -0700381 hsbpItemIface = objServer.add_interface(
James Feist45772222019-09-27 10:38:08 -0700382 "/xyz/openbmc_project/inventory/item/hsbp/" + dbusName,
Feist, Jamesc95cf672019-08-29 16:10:35 -0700383 inventory::interface);
James Feist45772222019-09-27 10:38:08 -0700384 hsbpItemIface->register_property("Present", true);
Feist, Jamesc95cf672019-08-29 16:10:35 -0700385 hsbpItemIface->register_property("PrettyName", name);
386 hsbpItemIface->initialize();
387
James Feistd0d36f12019-11-21 10:19:44 -0800388 storageInterface = objServer.add_interface(
389 hsbpItemIface->get_object_path(),
390 "xyz.openbmc_project.Inventory.Item.StorageController");
391 storageInterface->initialize();
392
James Feist45772222019-09-27 10:38:08 -0700393 versionIface =
James Feiste6db7832020-01-06 14:20:09 -0800394 objServer.add_interface("/xyz/openbmc_project/software/" + dbusName,
James Feist45772222019-09-27 10:38:08 -0700395 "xyz.openbmc_project.Software.Version");
396 versionIface->register_property("Version", zeroPad(bootVer) + "." +
397 zeroPad(fpgaVer) + "." +
398 zeroPad(securityRev));
399 versionIface->register_property(
400 "Purpose",
401 std::string(
402 "xyz.openbmc_project.Software.Version.VersionPurpose.HSBP"));
403 versionIface->initialize();
jayaprakash Mutyala05f8d572020-01-31 15:56:52 +0000404
405 auto activationIface =
406 objServer.add_interface("/xyz/openbmc_project/software/" + dbusName,
407 "xyz.openbmc_project.Software.Activation");
408
409 activationIface->register_property(
410 "Activation",
411 std::string(
412 "xyz.openbmc_project.Software.Activation.Activations.Active"));
413 activationIface->register_property(
414 "RequestedActivation",
415 std::string("xyz.openbmc_project.Software.Activation."
416 "RequestedActivations.None"));
417
418 activationIface->initialize();
419
James Feist45772222019-09-27 10:38:08 -0700420 getPresence(presence);
421 getIFDET(ifdet);
422
James Feistd0d36f12019-11-21 10:19:44 -0800423 populateAsset(rootPath, busname);
424
James Feist45772222019-09-27 10:38:08 -0700425 createDrives();
426
427 runTimer();
428 }
429
430 void runTimer()
431 {
James Feistd86629c2020-04-23 10:07:25 -0700432 timer.expires_after(std::chrono::seconds(scanRateSeconds));
433 timer.async_wait([weak{std::weak_ptr<Backplane>(shared_from_this())}](
434 boost::system::error_code ec) {
435 auto self = weak.lock();
436 if (!self)
437 {
438 return;
439 }
James Feist45772222019-09-27 10:38:08 -0700440 if (ec == boost::asio::error::operation_aborted)
441 {
442 // we're being destroyed
443 return;
444 }
445 else if (ec)
446 {
447 std::cerr << "timer error " << ec.message() << "\n";
448 return;
449 }
James Feist9f6565d2019-10-09 13:15:13 -0700450
451 if (!isPowerOn())
452 {
453 // can't access hsbp when power is off
James Feistd86629c2020-04-23 10:07:25 -0700454 self->runTimer();
James Feist9f6565d2019-10-09 13:15:13 -0700455 return;
456 }
James Feist45772222019-09-27 10:38:08 -0700457
James Feistd86629c2020-04-23 10:07:25 -0700458 self->getPresence(self->presence);
459 self->getIFDET(self->ifdet);
460 self->getFailed(self->failed);
461 self->getRebuild(self->rebuilding);
James Feist45772222019-09-27 10:38:08 -0700462
James Feistd86629c2020-04-23 10:07:25 -0700463 self->updateDrives();
464 self->runTimer();
James Feist45772222019-09-27 10:38:08 -0700465 });
466 }
467
468 void createDrives()
469 {
James Feist45772222019-09-27 10:38:08 -0700470 for (size_t ii = 0; ii < maxDrives; ii++)
Feist, Jamesc95cf672019-08-29 16:10:35 -0700471 {
Johnathan Mantey7045b4b2020-06-19 09:24:49 -0700472 uint8_t driveSlot = (1 << ii);
473 bool isNvme = ((ifdet & driveSlot) && !(presence & driveSlot));
474 bool isPresent = isNvme || (presence & driveSlot);
475 bool isFailed = !isPresent || failed & driveSlot;
476 bool isRebuilding = !isPresent && (rebuilding & driveSlot);
James Feist45772222019-09-27 10:38:08 -0700477
478 // +1 to convert from 0 based to 1 based
479 size_t driveIndex = (backplaneIndex * maxDrives) + ii + 1;
James Feist09dd2312019-10-09 09:29:03 -0700480 Drive& drive = drives.emplace_back(driveIndex, isPresent, !isFailed,
481 isNvme, isRebuilding);
482 std::shared_ptr<Led> led = leds.emplace_back(std::make_shared<Led>(
483 drive.itemIface->get_object_path(), ii, file));
484 led->createInterface();
Feist, Jamesc95cf672019-08-29 16:10:35 -0700485 }
486 }
487
James Feist45772222019-09-27 10:38:08 -0700488 void updateDrives()
Feist, Jamesc95cf672019-08-29 16:10:35 -0700489 {
James Feist42b49c12019-10-29 15:18:43 -0700490 size_t ii = 0;
491
492 for (auto it = drives.begin(); it != drives.end(); it++, ii++)
Feist, Jamesc95cf672019-08-29 16:10:35 -0700493 {
Johnathan Mantey7045b4b2020-06-19 09:24:49 -0700494 uint8_t driveSlot = (1 << ii);
495 bool isNvme = ((ifdet & driveSlot) && !(presence & driveSlot));
496 bool isPresent = isNvme || (presence & driveSlot);
497 bool isFailed = !isPresent || (failed & driveSlot);
498 bool isRebuilding = isPresent && (rebuilding & driveSlot);
James Feist45772222019-09-27 10:38:08 -0700499
James Feist42b49c12019-10-29 15:18:43 -0700500 it->isNvme = isNvme;
James Feistd86629c2020-04-23 10:07:25 -0700501 it->setPresent(isPresent);
Johnathan Mantey7045b4b2020-06-19 09:24:49 -0700502 it->logPresent();
James Feistda0c35f2020-02-03 10:16:13 -0800503
James Feist42b49c12019-10-29 15:18:43 -0700504 it->rebuildingIface->set_property("Rebuilding", isRebuilding);
505 if (isFailed || isRebuilding)
506 {
507 it->markFailed();
508 }
509 else
510 {
511 it->clearFailed();
512 }
Feist, Jamesc95cf672019-08-29 16:10:35 -0700513 }
James Feist45772222019-09-27 10:38:08 -0700514 }
515
516 bool getPresent()
517 {
518 present = i2c_smbus_read_byte(file) >= 0;
Feist, Jamesc95cf672019-08-29 16:10:35 -0700519 return present;
520 }
James Feist45772222019-09-27 10:38:08 -0700521
522 bool getTypeID(uint8_t& val)
523 {
524 constexpr uint8_t addr = 2;
525 int ret = i2c_smbus_read_byte_data(file, addr);
526 if (ret < 0)
527 {
528 std::cerr << "Error " << __FUNCTION__ << "\n";
529 return false;
530 }
531 val = static_cast<uint8_t>(ret);
532 return true;
533 }
534
535 bool getBootVer(uint8_t& val)
536 {
537 constexpr uint8_t addr = 3;
538 int ret = i2c_smbus_read_byte_data(file, addr);
539 if (ret < 0)
540 {
541 std::cerr << "Error " << __FUNCTION__ << "\n";
542 return false;
543 }
544 val = static_cast<uint8_t>(ret);
545 return true;
546 }
547
548 bool getFPGAVer(uint8_t& val)
549 {
550 constexpr uint8_t addr = 4;
551 int ret = i2c_smbus_read_byte_data(file, addr);
552 if (ret < 0)
553 {
554 std::cerr << "Error " << __FUNCTION__ << "\n";
555 return false;
556 }
557 val = static_cast<uint8_t>(ret);
558 return true;
559 }
560
561 bool getSecurityRev(uint8_t& val)
562 {
563 constexpr uint8_t addr = 5;
564 int ret = i2c_smbus_read_byte_data(file, addr);
565 if (ret < 0)
566 {
567 std::cerr << "Error " << __FUNCTION__ << "\n";
568 return false;
569 }
570 val = static_cast<uint8_t>(ret);
571 return true;
572 }
573
574 bool getPresence(uint8_t& val)
575 {
576 // NVMe drives do not assert PRSNTn, and as such do not get reported as
577 // PRESENT in this register
578
579 constexpr uint8_t addr = 8;
580
581 int ret = i2c_smbus_read_byte_data(file, addr);
582 if (ret < 0)
583 {
584 std::cerr << "Error " << __FUNCTION__ << "\n";
585 return false;
586 }
587 // presence is inverted
588 val = static_cast<uint8_t>(~ret);
589 return true;
590 }
591
592 bool getIFDET(uint8_t& val)
593 {
594 // This register is a bitmap of parallel GPIO pins connected to the
595 // IFDETn pin of a drive slot. SATA, SAS, and NVMe drives all assert
596 // IFDETn low when they are inserted into the HSBP.This register, in
597 // combination with the PRESENCE register, are used by the BMC to detect
598 // the presence of NVMe drives.
599
600 constexpr uint8_t addr = 9;
601
602 int ret = i2c_smbus_read_byte_data(file, addr);
603 if (ret < 0)
604 {
605 std::cerr << "Error " << __FUNCTION__ << "\n";
606 return false;
607 }
608 // ifdet is inverted
609 val = static_cast<uint8_t>(~ret);
610 return true;
611 }
612
613 bool getFailed(uint8_t& val)
614 {
615 constexpr uint8_t addr = 0xC;
616 int ret = i2c_smbus_read_byte_data(file, addr);
617 if (ret < 0)
618 {
619 std::cerr << "Error " << __FUNCTION__ << "\n";
620 return false;
621 }
622 val = static_cast<uint8_t>(ret);
623 return true;
624 }
625
626 bool getRebuild(uint8_t& val)
627 {
628 constexpr uint8_t addr = 0xD;
629 int ret = i2c_smbus_read_byte_data(file, addr);
630 if (ret < 0)
631 {
James Feistd86629c2020-04-23 10:07:25 -0700632 std::cerr << "Error " << __FUNCTION__ << " " << strerror(ret)
633 << "\n";
James Feist45772222019-09-27 10:38:08 -0700634 return false;
635 }
636 val = static_cast<uint8_t>(ret);
637 return true;
638 }
639
James Feist09dd2312019-10-09 09:29:03 -0700640 virtual ~Backplane()
Feist, Jamesc95cf672019-08-29 16:10:35 -0700641 {
642 objServer.remove_interface(hsbpItemIface);
James Feist45772222019-09-27 10:38:08 -0700643 objServer.remove_interface(versionIface);
James Feistd86629c2020-04-23 10:07:25 -0700644 timer.cancel();
Feist, Jamesc95cf672019-08-29 16:10:35 -0700645 if (file >= 0)
646 {
647 close(file);
648 }
649 }
650
651 size_t bus;
652 size_t address;
James Feist45772222019-09-27 10:38:08 -0700653 size_t backplaneIndex;
Feist, Jamesc95cf672019-08-29 16:10:35 -0700654 std::string name;
James Feistd86629c2020-04-23 10:07:25 -0700655 boost::asio::steady_timer timer;
James Feist45772222019-09-27 10:38:08 -0700656 bool present = false;
657 uint8_t typeId = 0;
658 uint8_t bootVer = 0;
659 uint8_t fpgaVer = 0;
660 uint8_t securityRev = 0;
661 uint8_t funSupported = 0;
662 uint8_t presence = 0;
663 uint8_t ifdet = 0;
664 uint8_t failed = 0;
James Feist244f3232019-09-27 15:15:14 -0700665 uint8_t rebuilding = 0;
James Feist45772222019-09-27 10:38:08 -0700666
667 int file = -1;
668
Feist, Jamesc95cf672019-08-29 16:10:35 -0700669 std::string type;
670
671 std::shared_ptr<sdbusplus::asio::dbus_interface> hsbpItemIface;
James Feist45772222019-09-27 10:38:08 -0700672 std::shared_ptr<sdbusplus::asio::dbus_interface> versionIface;
James Feistd0d36f12019-11-21 10:19:44 -0800673 std::shared_ptr<sdbusplus::asio::dbus_interface> storageInterface;
674 std::shared_ptr<sdbusplus::asio::dbus_interface> assetInterface;
James Feist45772222019-09-27 10:38:08 -0700675
James Feist42b49c12019-10-29 15:18:43 -0700676 std::list<Drive> drives;
James Feist09dd2312019-10-09 09:29:03 -0700677 std::vector<std::shared_ptr<Led>> leds;
James Feist8675a912019-10-16 14:36:58 -0700678 std::shared_ptr<boost::container::flat_set<Mux>> muxes;
Feist, Jamesc95cf672019-08-29 16:10:35 -0700679};
680
James Feistd86629c2020-04-23 10:07:25 -0700681std::unordered_map<std::string, std::shared_ptr<Backplane>> backplanes;
James Feist42b49c12019-10-29 15:18:43 -0700682std::list<Drive> ownerlessDrives; // drives without a backplane
Feist, Jamesc95cf672019-08-29 16:10:35 -0700683
James Feist8675a912019-10-16 14:36:58 -0700684static size_t getDriveCount()
James Feistdb2e0e72019-10-07 16:34:06 -0700685{
James Feist8675a912019-10-16 14:36:58 -0700686 size_t count = 0;
687 for (const auto& [key, backplane] : backplanes)
James Feistdb2e0e72019-10-07 16:34:06 -0700688 {
James Feistd86629c2020-04-23 10:07:25 -0700689 count += backplane->drives.size();
James Feistdb2e0e72019-10-07 16:34:06 -0700690 }
James Feist8675a912019-10-16 14:36:58 -0700691 return count + ownerlessDrives.size();
James Feistdb2e0e72019-10-07 16:34:06 -0700692}
693
James Feist8675a912019-10-16 14:36:58 -0700694void updateAssets()
James Feist0b236ab2019-10-02 09:09:16 -0700695{
James Feist8675a912019-10-16 14:36:58 -0700696 static constexpr const char* nvmeType =
697 "xyz.openbmc_project.Inventory.Item.NVMe";
James Feist0b236ab2019-10-02 09:09:16 -0700698
699 conn->async_method_call(
700 [](const boost::system::error_code ec, const GetSubTreeType& subtree) {
701 if (ec)
702 {
703 std::cerr << "Error contacting mapper " << ec.message() << "\n";
704 return;
705 }
James Feist8675a912019-10-16 14:36:58 -0700706
707 // drives may get an owner during this, or we might disover more
708 // drives
709 ownerlessDrives.clear();
James Feist0b236ab2019-10-02 09:09:16 -0700710 for (const auto& [path, objDict] : subtree)
711 {
712 if (objDict.empty())
713 {
714 continue;
715 }
716
717 const std::string& owner = objDict.begin()->first;
James Feistdb2e0e72019-10-07 16:34:06 -0700718 // we export this interface too
719 if (owner == busName)
720 {
721 continue;
722 }
James Feist8675a912019-10-16 14:36:58 -0700723 if (std::find(objDict.begin()->second.begin(),
724 objDict.begin()->second.end(),
725 assetTag) == objDict.begin()->second.end())
726 {
727 // no asset tag to associate to
728 continue;
729 }
730
James Feist0b236ab2019-10-02 09:09:16 -0700731 conn->async_method_call(
Johnathan Mantey7045b4b2020-06-19 09:24:49 -0700732 [path](const boost::system::error_code ec2,
733 const boost::container::flat_map<
734 std::string,
735 std::variant<uint64_t, std::string>>& values) {
James Feist0b236ab2019-10-02 09:09:16 -0700736 if (ec2)
737 {
738 std::cerr << "Error Getting Config "
739 << ec2.message() << " " << __FUNCTION__
740 << "\n";
741 return;
742 }
743 auto findBus = values.find("Bus");
James Feist0b236ab2019-10-02 09:09:16 -0700744
James Feist8675a912019-10-16 14:36:58 -0700745 if (findBus == values.end())
James Feist0b236ab2019-10-02 09:09:16 -0700746 {
747 std::cerr << "Illegal interface at " << path
748 << "\n";
749 return;
750 }
751
James Feist8675a912019-10-16 14:36:58 -0700752 // find the mux bus and addr
James Feist0b236ab2019-10-02 09:09:16 -0700753 size_t muxBus = static_cast<size_t>(
754 std::get<uint64_t>(findBus->second));
James Feist0b236ab2019-10-02 09:09:16 -0700755 std::filesystem::path muxPath =
756 "/sys/bus/i2c/devices/i2c-" +
757 std::to_string(muxBus) + "/mux_device";
758 if (!std::filesystem::is_symlink(muxPath))
759 {
760 std::cerr << path << " mux does not exist\n";
761 return;
762 }
763
James Feist8675a912019-10-16 14:36:58 -0700764 // we should be getting something of the form 7-0052
765 // for bus 7 addr 52
James Feist0b236ab2019-10-02 09:09:16 -0700766 std::string fname =
767 std::filesystem::read_symlink(muxPath).filename();
768 auto findDash = fname.find('-');
769
770 if (findDash == std::string::npos ||
771 findDash + 1 >= fname.size())
772 {
773 std::cerr << path << " mux path invalid\n";
774 return;
775 }
776
777 std::string busStr = fname.substr(0, findDash);
778 std::string muxStr = fname.substr(findDash + 1);
779
780 size_t bus = static_cast<size_t>(std::stoi(busStr));
781 size_t addr =
782 static_cast<size_t>(std::stoi(muxStr, nullptr, 16));
James Feist8675a912019-10-16 14:36:58 -0700783 size_t muxIndex = 0;
784
785 // find the channel of the mux the drive is on
786 std::ifstream nameFile("/sys/bus/i2c/devices/i2c-" +
787 std::to_string(muxBus) +
788 "/name");
789 if (!nameFile)
790 {
791 std::cerr << "Unable to open name file of bus "
792 << muxBus << "\n";
793 return;
794 }
795
796 std::string nameStr;
797 std::getline(nameFile, nameStr);
798
799 // file is of the form "i2c-4-mux (chan_id 1)", get chan
800 // assume single digit chan
801 const std::string prefix = "chan_id ";
802 size_t findId = nameStr.find(prefix);
803 if (findId == std::string::npos ||
804 findId + 1 >= nameStr.size())
805 {
806 std::cerr << "Illegal name file on bus " << muxBus
807 << "\n";
808 }
809
810 std::string indexStr =
811 nameStr.substr(findId + prefix.size(), 1);
812
813 size_t driveIndex = std::stoi(indexStr);
814
James Feist0b236ab2019-10-02 09:09:16 -0700815 Backplane* parent = nullptr;
816 for (auto& [name, backplane] : backplanes)
817 {
James Feist8675a912019-10-16 14:36:58 -0700818 muxIndex = 0;
James Feistd86629c2020-04-23 10:07:25 -0700819 for (const Mux& mux : *(backplane->muxes))
James Feist0b236ab2019-10-02 09:09:16 -0700820 {
821 if (bus == mux.bus && addr == mux.address)
822 {
James Feistd86629c2020-04-23 10:07:25 -0700823 parent = backplane.get();
James Feist0b236ab2019-10-02 09:09:16 -0700824 break;
825 }
James Feist8675a912019-10-16 14:36:58 -0700826 muxIndex += mux.channels;
James Feist0b236ab2019-10-02 09:09:16 -0700827 }
828 }
James Feist8675a912019-10-16 14:36:58 -0700829 boost::container::flat_map<std::string, std::string>
830 assetInventory;
831 const std::array<const char*, 4> assetKeys = {
832 "PartNumber", "SerialNumber", "Manufacturer",
833 "Model"};
834 for (const auto& [key, value] : values)
835 {
836 if (std::find(assetKeys.begin(), assetKeys.end(),
837 key) == assetKeys.end())
838 {
839 continue;
840 }
841 assetInventory[key] = std::get<std::string>(value);
842 }
843
844 // assume its a M.2 or something without a hsbp
James Feist0b236ab2019-10-02 09:09:16 -0700845 if (parent == nullptr)
846 {
James Feist8675a912019-10-16 14:36:58 -0700847 auto& drive = ownerlessDrives.emplace_back(
848 getDriveCount() + 1, true, true, true, false);
849 drive.createAsset(assetInventory);
James Feist0b236ab2019-10-02 09:09:16 -0700850 return;
851 }
James Feist8675a912019-10-16 14:36:58 -0700852
853 driveIndex += muxIndex;
854
James Feist0b236ab2019-10-02 09:09:16 -0700855 if (parent->drives.size() <= driveIndex)
856 {
James Feist0b236ab2019-10-02 09:09:16 -0700857 std::cerr << "Illegal drive index at " << path
858 << " " << driveIndex << "\n";
859 return;
860 }
James Feist42b49c12019-10-29 15:18:43 -0700861 auto it = parent->drives.begin();
862 std::advance(it, driveIndex);
James Feist8675a912019-10-16 14:36:58 -0700863
James Feist42b49c12019-10-29 15:18:43 -0700864 it->createAsset(assetInventory);
James Feist0b236ab2019-10-02 09:09:16 -0700865 },
866 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
James Feist8675a912019-10-16 14:36:58 -0700867 "" /*all interface items*/);
James Feist0b236ab2019-10-02 09:09:16 -0700868 }
869 },
870 mapper::busName, mapper::path, mapper::interface, mapper::subtree, "/",
James Feist8675a912019-10-16 14:36:58 -0700871 0, std::array<const char*, 1>{nvmeType});
James Feist0b236ab2019-10-02 09:09:16 -0700872}
873
James Feist8675a912019-10-16 14:36:58 -0700874void populateMuxes(std::shared_ptr<boost::container::flat_set<Mux>> muxes,
James Feist0b236ab2019-10-02 09:09:16 -0700875 std::string& rootPath)
876{
877 const static std::array<const std::string, 4> muxTypes = {
878 "xyz.openbmc_project.Configuration.PCA9543Mux",
879 "xyz.openbmc_project.Configuration.PCA9544Mux",
880 "xyz.openbmc_project.Configuration.PCA9545Mux",
881 "xyz.openbmc_project.Configuration.PCA9546Mux"};
882 conn->async_method_call(
883 [muxes](const boost::system::error_code ec,
884 const GetSubTreeType& subtree) {
885 if (ec)
886 {
887 std::cerr << "Error contacting mapper " << ec.message() << "\n";
888 return;
889 }
890 std::shared_ptr<std::function<void()>> callback =
891 std::make_shared<std::function<void()>>(
James Feist8675a912019-10-16 14:36:58 -0700892 []() { updateAssets(); });
893 size_t index = 0; // as we use a flat map, these are sorted
James Feist0b236ab2019-10-02 09:09:16 -0700894 for (const auto& [path, objDict] : subtree)
895 {
896 if (objDict.empty() || objDict.begin()->second.empty())
897 {
898 continue;
899 }
900
901 const std::string& owner = objDict.begin()->first;
902 const std::vector<std::string>& interfaces =
903 objDict.begin()->second;
904
905 const std::string* interface = nullptr;
906 for (const std::string& iface : interfaces)
907 {
908 if (std::find(muxTypes.begin(), muxTypes.end(), iface) !=
909 muxTypes.end())
910 {
911 interface = &iface;
912 break;
913 }
914 }
915 if (interface == nullptr)
916 {
917 std::cerr << "Cannot get mux type\n";
918 continue;
919 }
920
921 conn->async_method_call(
James Feist8675a912019-10-16 14:36:58 -0700922 [path, muxes, callback, index](
James Feist0b236ab2019-10-02 09:09:16 -0700923 const boost::system::error_code ec2,
924 const boost::container::flat_map<
James Feist8675a912019-10-16 14:36:58 -0700925 std::string,
926 std::variant<uint64_t, std::vector<std::string>>>&
927 values) {
James Feist0b236ab2019-10-02 09:09:16 -0700928 if (ec2)
929 {
930 std::cerr << "Error Getting Config "
931 << ec2.message() << " " << __FUNCTION__
932 << "\n";
933 return;
934 }
935 auto findBus = values.find("Bus");
936 auto findAddress = values.find("Address");
James Feist8675a912019-10-16 14:36:58 -0700937 auto findChannelNames = values.find("ChannelNames");
James Feist0b236ab2019-10-02 09:09:16 -0700938 if (findBus == values.end() ||
939 findAddress == values.end())
940 {
941 std::cerr << "Illegal configuration at " << path
942 << "\n";
943 return;
944 }
945 size_t bus = static_cast<size_t>(
946 std::get<uint64_t>(findBus->second));
947 size_t address = static_cast<size_t>(
948 std::get<uint64_t>(findAddress->second));
James Feist8675a912019-10-16 14:36:58 -0700949 std::vector<std::string> channels =
950 std::get<std::vector<std::string>>(
951 findChannelNames->second);
952 muxes->emplace(bus, address, channels.size(), index);
James Feist0b236ab2019-10-02 09:09:16 -0700953 if (callback.use_count() == 1)
954 {
955 (*callback)();
956 }
957 },
958 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
959 *interface);
James Feist8675a912019-10-16 14:36:58 -0700960 index++;
James Feist0b236ab2019-10-02 09:09:16 -0700961 }
962 },
963 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
964 rootPath, 1, muxTypes);
965}
966
Feist, Jamesc95cf672019-08-29 16:10:35 -0700967void populate()
968{
969 conn->async_method_call(
970 [](const boost::system::error_code ec, const GetSubTreeType& subtree) {
971 if (ec)
972 {
973 std::cerr << "Error contacting mapper " << ec.message() << "\n";
974 return;
975 }
976 for (const auto& [path, objDict] : subtree)
977 {
978 if (objDict.empty())
979 {
980 continue;
981 }
982
983 const std::string& owner = objDict.begin()->first;
984 conn->async_method_call(
James Feistd0d36f12019-11-21 10:19:44 -0800985 [path, owner](const boost::system::error_code ec2,
986 const boost::container::flat_map<
987 std::string, BasicVariantType>& resp) {
Feist, Jamesc95cf672019-08-29 16:10:35 -0700988 if (ec2)
989 {
990 std::cerr << "Error Getting Config "
991 << ec2.message() << "\n";
992 return;
993 }
994 backplanes.clear();
995 std::optional<size_t> bus;
996 std::optional<size_t> address;
James Feist45772222019-09-27 10:38:08 -0700997 std::optional<size_t> backplaneIndex;
Feist, Jamesc95cf672019-08-29 16:10:35 -0700998 std::optional<std::string> name;
999 for (const auto& [key, value] : resp)
1000 {
1001 if (key == "Bus")
1002 {
1003 bus = std::get<uint64_t>(value);
1004 }
1005 else if (key == "Address")
1006 {
1007 address = std::get<uint64_t>(value);
1008 }
James Feist45772222019-09-27 10:38:08 -07001009 else if (key == "Index")
1010 {
1011 backplaneIndex = std::get<uint64_t>(value);
1012 }
Feist, Jamesc95cf672019-08-29 16:10:35 -07001013 else if (key == "Name")
1014 {
1015 name = std::get<std::string>(value);
1016 }
1017 }
James Feist45772222019-09-27 10:38:08 -07001018 if (!bus || !address || !name || !backplaneIndex)
Feist, Jamesc95cf672019-08-29 16:10:35 -07001019 {
1020 std::cerr << "Illegal configuration at " << path
1021 << "\n";
1022 return;
1023 }
James Feist0b236ab2019-10-02 09:09:16 -07001024 std::string parentPath =
1025 std::filesystem::path(path).parent_path();
Feist, Jamesc95cf672019-08-29 16:10:35 -07001026 const auto& [backplane, status] = backplanes.emplace(
James Feistd86629c2020-04-23 10:07:25 -07001027 *name, std::make_shared<Backplane>(
1028 *bus, *address, *backplaneIndex, *name));
1029 backplane->second->run(parentPath, owner);
1030 populateMuxes(backplane->second->muxes, parentPath);
Feist, Jamesc95cf672019-08-29 16:10:35 -07001031 },
1032 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1033 configType);
1034 }
1035 },
1036 mapper::busName, mapper::path, mapper::interface, mapper::subtree, "/",
1037 0, std::array<const char*, 1>{configType});
1038}
1039
1040int main()
1041{
1042 boost::asio::steady_timer callbackTimer(io);
1043
James Feistdb2e0e72019-10-07 16:34:06 -07001044 conn->request_name(busName);
Feist, Jamesc95cf672019-08-29 16:10:35 -07001045
1046 sdbusplus::bus::match::match match(
1047 *conn,
1048 "type='signal',member='PropertiesChanged',arg0='" +
1049 std::string(configType) + "'",
1050 [&callbackTimer](sdbusplus::message::message&) {
1051 callbackTimer.expires_after(std::chrono::seconds(2));
1052 callbackTimer.async_wait([](const boost::system::error_code ec) {
1053 if (ec == boost::asio::error::operation_aborted)
1054 {
1055 // timer was restarted
1056 return;
1057 }
1058 else if (ec)
1059 {
1060 std::cerr << "Timer error" << ec.message() << "\n";
1061 return;
1062 }
1063 populate();
1064 });
1065 });
1066
James Feist0b236ab2019-10-02 09:09:16 -07001067 sdbusplus::bus::match::match drive(
1068 *conn,
1069 "type='signal',member='PropertiesChanged',arg0='xyz.openbmc_project."
James Feist8675a912019-10-16 14:36:58 -07001070 "Inventory.Item.NVMe'",
James Feistdb2e0e72019-10-07 16:34:06 -07001071 [&callbackTimer](sdbusplus::message::message& message) {
James Feist0b236ab2019-10-02 09:09:16 -07001072 callbackTimer.expires_after(std::chrono::seconds(2));
James Feistdb2e0e72019-10-07 16:34:06 -07001073 if (message.get_sender() == conn->get_unique_name())
1074 {
1075 return;
1076 }
James Feist0b236ab2019-10-02 09:09:16 -07001077 callbackTimer.async_wait([](const boost::system::error_code ec) {
1078 if (ec == boost::asio::error::operation_aborted)
1079 {
1080 // timer was restarted
1081 return;
1082 }
1083 else if (ec)
1084 {
1085 std::cerr << "Timer error" << ec.message() << "\n";
1086 return;
1087 }
James Feistd86629c2020-04-23 10:07:25 -07001088 updateAssets();
James Feist0b236ab2019-10-02 09:09:16 -07001089 });
1090 });
1091
James Feist42b49c12019-10-29 15:18:43 -07001092 auto iface =
1093 objServer.add_interface("/xyz/openbmc_project/inventory/item/storage",
1094 "xyz.openbmc_project.inventory.item.storage");
1095
Feist, Jamesc95cf672019-08-29 16:10:35 -07001096 io.post([]() { populate(); });
James Feist9f6565d2019-10-09 13:15:13 -07001097 setupPowerMatch(conn);
Feist, Jamesc95cf672019-08-29 16:10:35 -07001098 io.run();
1099}