Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 1 | /* |
| 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 Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 21 | #include <boost/container/flat_set.hpp> |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 22 | #include <filesystem> |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 23 | #include <fstream> |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 24 | #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 Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 29 | #include <utility> |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 30 | |
| 31 | extern "C" { |
| 32 | #include <i2c/smbus.h> |
| 33 | #include <linux/i2c-dev.h> |
| 34 | } |
| 35 | |
| 36 | constexpr const char* configType = |
| 37 | "xyz.openbmc_project.Configuration.Intel_HSBP_CPLD"; |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 38 | constexpr const char* busName = "xyz.openbmc_project.HsbpManager"; |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 39 | |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 40 | constexpr size_t scanRateSeconds = 5; |
| 41 | constexpr size_t maxDrives = 8; // only 1 byte alloted |
| 42 | |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 43 | boost::asio::io_context io; |
| 44 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 45 | sdbusplus::asio::object_server objServer(conn); |
| 46 | |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 47 | static 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 Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 54 | struct Mux |
| 55 | { |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 56 | Mux(size_t busIn, size_t addressIn, size_t channelsIn, size_t indexIn) : |
| 57 | bus(busIn), address(addressIn), channels(channelsIn), index(indexIn) |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 58 | { |
| 59 | } |
| 60 | size_t bus; |
| 61 | size_t address; |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 62 | 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 Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 70 | }; |
James Feist | 09dd231 | 2019-10-09 09:29:03 -0700 | [diff] [blame] | 71 | |
| 72 | enum class BlinkPattern : uint8_t |
| 73 | { |
| 74 | off = 0x0, |
| 75 | error = 0x2, |
| 76 | terminate = 0x3 |
| 77 | }; |
| 78 | |
| 79 | struct 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 Feist | 9f6565d | 2019-10-09 13:15:13 -0700 | [diff] [blame] | 109 | |
| 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 Feist | 09dd231 | 2019-10-09 09:29:03 -0700 | [diff] [blame] | 116 | BlinkPattern pattern = |
| 117 | req ? BlinkPattern::error : BlinkPattern::terminate; |
| 118 | if (!self->set(pattern)) |
| 119 | { |
James Feist | 9f6565d | 2019-10-09 13:15:13 -0700 | [diff] [blame] | 120 | std::cerr << "Can't change blink pattern\n"; |
James Feist | 09dd231 | 2019-10-09 09:29:03 -0700 | [diff] [blame] | 121 | 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 Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 146 | struct Drive |
| 147 | { |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 148 | Drive(size_t driveIndex, bool present, bool isOperational, bool nvme, |
James Feist | 244f323 | 2019-09-27 15:15:14 -0700 | [diff] [blame] | 149 | bool rebuilding) : |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 150 | isNvme(nvme), |
James Feist | e881852 | 2019-11-04 13:36:10 -0800 | [diff] [blame] | 151 | isPresent(present), index(driveIndex) |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 152 | { |
| 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 Feist | 244f323 | 2019-09-27 15:15:14 -0700 | [diff] [blame] | 162 | itemIface->get_object_path(), |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 163 | "xyz.openbmc_project.State.Decorator.OperationalStatus"); |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 164 | |
| 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 Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 186 | operationalIface->initialize(); |
James Feist | 244f323 | 2019-09-27 15:15:14 -0700 | [diff] [blame] | 187 | rebuildingIface = objServer.add_interface( |
| 188 | itemIface->get_object_path(), "xyz.openbmc_project.State.Drive"); |
| 189 | rebuildingIface->register_property("Rebuilding", rebuilding); |
| 190 | rebuildingIface->initialize(); |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 191 | driveIface = |
| 192 | objServer.add_interface(itemIface->get_object_path(), |
| 193 | "xyz.openbmc_project.Inventory.Item.Drive"); |
| 194 | driveIface->initialize(); |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 195 | 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 Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 205 | } |
James Feist | 09dd231 | 2019-10-09 09:29:03 -0700 | [diff] [blame] | 206 | virtual ~Drive() |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 207 | { |
| 208 | objServer.remove_interface(itemIface); |
| 209 | objServer.remove_interface(operationalIface); |
James Feist | 244f323 | 2019-09-27 15:15:14 -0700 | [diff] [blame] | 210 | objServer.remove_interface(rebuildingIface); |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 211 | objServer.remove_interface(assetIface); |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 212 | objServer.remove_interface(driveIface); |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 213 | objServer.remove_interface(associations); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 214 | } |
| 215 | |
James Feist | c66735b | 2020-07-17 13:51:21 -0700 | [diff] [blame] | 216 | void removeAsset() |
| 217 | { |
| 218 | objServer.remove_interface(assetIface); |
| 219 | assetIface = nullptr; |
| 220 | } |
| 221 | |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 222 | void createAsset( |
| 223 | const boost::container::flat_map<std::string, std::string>& data) |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 224 | { |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 225 | if (assetIface != nullptr) |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 226 | { |
| 227 | return; |
| 228 | } |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 229 | assetIface = objServer.add_interface( |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 230 | itemIface->get_object_path(), |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 231 | "xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 232 | for (const auto& [key, value] : data) |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 233 | { |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 234 | assetIface->register_property(key, value); |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 235 | if (key == "SerialNumber") |
| 236 | { |
| 237 | serialNumber = value; |
Johnathan Mantey | 7045b4b | 2020-06-19 09:24:49 -0700 | [diff] [blame] | 238 | serialNumberInitialized = true; |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 239 | } |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 240 | } |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 241 | assetIface->initialize(); |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 242 | } |
| 243 | |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 244 | void markFailed(void) |
| 245 | { |
| 246 | // todo: maybe look this up via mapper |
| 247 | constexpr const char* globalInventoryPath = |
| 248 | "/xyz/openbmc_project/CallbackManager"; |
| 249 | |
| 250 | if (!isPresent) |
| 251 | { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | operationalIface->set_property("Functional", false); |
| 256 | std::vector<Association> warning = { |
| 257 | {"", "warning", globalInventoryPath}}; |
| 258 | associations->set_property("Associations", warning); |
James Feist | e881852 | 2019-11-04 13:36:10 -0800 | [diff] [blame] | 259 | logDriveError("Drive " + std::to_string(index)); |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void clearFailed(void) |
| 263 | { |
| 264 | operationalIface->set_property("Functional", true); |
| 265 | associations->set_property("Associations", std::vector<Association>{}); |
| 266 | } |
| 267 | |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 268 | void setPresent(bool set) |
James Feist | e881852 | 2019-11-04 13:36:10 -0800 | [diff] [blame] | 269 | { |
| 270 | // nvme drives get detected by their fru |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 271 | if (set == isPresent) |
James Feist | e881852 | 2019-11-04 13:36:10 -0800 | [diff] [blame] | 272 | { |
| 273 | return; |
| 274 | } |
| 275 | itemIface->set_property("Present", set); |
| 276 | isPresent = set; |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void logPresent() |
| 280 | { |
Johnathan Mantey | 7045b4b | 2020-06-19 09:24:49 -0700 | [diff] [blame] | 281 | if (isNvme && !serialNumberInitialized) |
James Feist | e881852 | 2019-11-04 13:36:10 -0800 | [diff] [blame] | 282 | { |
Johnathan Mantey | 7045b4b | 2020-06-19 09:24:49 -0700 | [diff] [blame] | 283 | // wait until NVMe asset is updated to include the serial number |
| 284 | // from the NVMe drive |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 285 | return; |
James Feist | e881852 | 2019-11-04 13:36:10 -0800 | [diff] [blame] | 286 | } |
Johnathan Mantey | 7045b4b | 2020-06-19 09:24:49 -0700 | [diff] [blame] | 287 | |
| 288 | if (!isPresent && loggedPresent) |
| 289 | { |
| 290 | loggedPresent = false; |
| 291 | logDeviceRemoved("Drive", std::to_string(index), serialNumber); |
| 292 | serialNumber = "N/A"; |
| 293 | serialNumberInitialized = false; |
James Feist | c66735b | 2020-07-17 13:51:21 -0700 | [diff] [blame] | 294 | removeAsset(); |
Johnathan Mantey | 7045b4b | 2020-06-19 09:24:49 -0700 | [diff] [blame] | 295 | } |
| 296 | else if (isPresent && !loggedPresent) |
| 297 | { |
| 298 | loggedPresent = true; |
| 299 | logDeviceAdded("Drive", std::to_string(index), serialNumber); |
| 300 | } |
James Feist | e881852 | 2019-11-04 13:36:10 -0800 | [diff] [blame] | 301 | } |
| 302 | |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 303 | std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface; |
| 304 | std::shared_ptr<sdbusplus::asio::dbus_interface> operationalIface; |
James Feist | 244f323 | 2019-09-27 15:15:14 -0700 | [diff] [blame] | 305 | std::shared_ptr<sdbusplus::asio::dbus_interface> rebuildingIface; |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 306 | std::shared_ptr<sdbusplus::asio::dbus_interface> assetIface; |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 307 | std::shared_ptr<sdbusplus::asio::dbus_interface> driveIface; |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 308 | std::shared_ptr<sdbusplus::asio::dbus_interface> associations; |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 309 | |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 310 | bool isNvme; |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 311 | bool isPresent; |
James Feist | e881852 | 2019-11-04 13:36:10 -0800 | [diff] [blame] | 312 | size_t index; |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 313 | std::string serialNumber = "N/A"; |
Johnathan Mantey | 7045b4b | 2020-06-19 09:24:49 -0700 | [diff] [blame] | 314 | bool serialNumberInitialized = false; |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 315 | bool loggedPresent = false; |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 316 | }; |
| 317 | |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 318 | struct Backplane : std::enable_shared_from_this<Backplane> |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 319 | { |
| 320 | |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 321 | Backplane(size_t busIn, size_t addressIn, size_t backplaneIndexIn, |
| 322 | const std::string& nameIn) : |
| 323 | bus(busIn), |
| 324 | address(addressIn), backplaneIndex(backplaneIndexIn - 1), name(nameIn), |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 325 | timer(boost::asio::steady_timer(io)), |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 326 | muxes(std::make_shared<boost::container::flat_set<Mux>>()) |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 327 | { |
| 328 | } |
James Feist | d0d36f1 | 2019-11-21 10:19:44 -0800 | [diff] [blame] | 329 | void populateAsset(const std::string& rootPath, const std::string& busname) |
| 330 | { |
| 331 | conn->async_method_call( |
| 332 | [assetIface{assetInterface}, hsbpIface{hsbpItemIface}]( |
| 333 | const boost::system::error_code ec, |
| 334 | const boost::container::flat_map< |
| 335 | std::string, std::variant<std::string>>& values) mutable { |
| 336 | if (ec) |
| 337 | { |
| 338 | std::cerr |
| 339 | << "Error getting asset tag from HSBP configuration\n"; |
| 340 | |
| 341 | return; |
| 342 | } |
| 343 | assetIface = objServer.add_interface( |
| 344 | hsbpIface->get_object_path(), assetTag); |
| 345 | for (const auto& [key, value] : values) |
| 346 | { |
| 347 | const std::string* ptr = std::get_if<std::string>(&value); |
| 348 | if (ptr == nullptr) |
| 349 | { |
| 350 | std::cerr << key << " Invalid type!\n"; |
| 351 | continue; |
| 352 | } |
| 353 | assetIface->register_property(key, *ptr); |
| 354 | } |
| 355 | assetIface->initialize(); |
| 356 | }, |
| 357 | busname, rootPath, "org.freedesktop.DBus.Properties", "GetAll", |
| 358 | assetTag); |
| 359 | } |
| 360 | |
| 361 | void run(const std::string& rootPath, const std::string& busname) |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 362 | { |
James Feist | 09dd231 | 2019-10-09 09:29:03 -0700 | [diff] [blame] | 363 | file = open(("/dev/i2c-" + std::to_string(bus)).c_str(), |
| 364 | O_RDWR | O_CLOEXEC); |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 365 | if (file < 0) |
| 366 | { |
| 367 | std::cerr << "unable to open bus " << bus << "\n"; |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | if (ioctl(file, I2C_SLAVE_FORCE, address) < 0) |
| 372 | { |
| 373 | std::cerr << "unable to set address to " << address << "\n"; |
| 374 | return; |
| 375 | } |
| 376 | |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 377 | if (!getPresent()) |
| 378 | { |
| 379 | std::cerr << "Cannot detect CPLD\n"; |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | getBootVer(bootVer); |
| 384 | getFPGAVer(fpgaVer); |
| 385 | getSecurityRev(securityRev); |
| 386 | std::string dbusName = boost::replace_all_copy(name, " ", "_"); |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 387 | hsbpItemIface = objServer.add_interface( |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 388 | "/xyz/openbmc_project/inventory/item/hsbp/" + dbusName, |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 389 | inventory::interface); |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 390 | hsbpItemIface->register_property("Present", true); |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 391 | hsbpItemIface->register_property("PrettyName", name); |
| 392 | hsbpItemIface->initialize(); |
| 393 | |
James Feist | d0d36f1 | 2019-11-21 10:19:44 -0800 | [diff] [blame] | 394 | storageInterface = objServer.add_interface( |
| 395 | hsbpItemIface->get_object_path(), |
| 396 | "xyz.openbmc_project.Inventory.Item.StorageController"); |
| 397 | storageInterface->initialize(); |
| 398 | |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 399 | versionIface = |
James Feist | e6db783 | 2020-01-06 14:20:09 -0800 | [diff] [blame] | 400 | objServer.add_interface("/xyz/openbmc_project/software/" + dbusName, |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 401 | "xyz.openbmc_project.Software.Version"); |
| 402 | versionIface->register_property("Version", zeroPad(bootVer) + "." + |
| 403 | zeroPad(fpgaVer) + "." + |
| 404 | zeroPad(securityRev)); |
| 405 | versionIface->register_property( |
| 406 | "Purpose", |
| 407 | std::string( |
| 408 | "xyz.openbmc_project.Software.Version.VersionPurpose.HSBP")); |
| 409 | versionIface->initialize(); |
jayaprakash Mutyala | 05f8d57 | 2020-01-31 15:56:52 +0000 | [diff] [blame] | 410 | |
| 411 | auto activationIface = |
| 412 | objServer.add_interface("/xyz/openbmc_project/software/" + dbusName, |
| 413 | "xyz.openbmc_project.Software.Activation"); |
| 414 | |
| 415 | activationIface->register_property( |
| 416 | "Activation", |
| 417 | std::string( |
| 418 | "xyz.openbmc_project.Software.Activation.Activations.Active")); |
| 419 | activationIface->register_property( |
| 420 | "RequestedActivation", |
| 421 | std::string("xyz.openbmc_project.Software.Activation." |
| 422 | "RequestedActivations.None")); |
| 423 | |
| 424 | activationIface->initialize(); |
| 425 | |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 426 | getPresence(presence); |
| 427 | getIFDET(ifdet); |
| 428 | |
James Feist | d0d36f1 | 2019-11-21 10:19:44 -0800 | [diff] [blame] | 429 | populateAsset(rootPath, busname); |
| 430 | |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 431 | createDrives(); |
| 432 | |
| 433 | runTimer(); |
| 434 | } |
| 435 | |
| 436 | void runTimer() |
| 437 | { |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 438 | timer.expires_after(std::chrono::seconds(scanRateSeconds)); |
| 439 | timer.async_wait([weak{std::weak_ptr<Backplane>(shared_from_this())}]( |
| 440 | boost::system::error_code ec) { |
| 441 | auto self = weak.lock(); |
| 442 | if (!self) |
| 443 | { |
| 444 | return; |
| 445 | } |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 446 | if (ec == boost::asio::error::operation_aborted) |
| 447 | { |
| 448 | // we're being destroyed |
| 449 | return; |
| 450 | } |
| 451 | else if (ec) |
| 452 | { |
| 453 | std::cerr << "timer error " << ec.message() << "\n"; |
| 454 | return; |
| 455 | } |
James Feist | 9f6565d | 2019-10-09 13:15:13 -0700 | [diff] [blame] | 456 | |
| 457 | if (!isPowerOn()) |
| 458 | { |
| 459 | // can't access hsbp when power is off |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 460 | self->runTimer(); |
James Feist | 9f6565d | 2019-10-09 13:15:13 -0700 | [diff] [blame] | 461 | return; |
| 462 | } |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 463 | |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 464 | self->getPresence(self->presence); |
| 465 | self->getIFDET(self->ifdet); |
| 466 | self->getFailed(self->failed); |
| 467 | self->getRebuild(self->rebuilding); |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 468 | |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 469 | self->updateDrives(); |
| 470 | self->runTimer(); |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 471 | }); |
| 472 | } |
| 473 | |
| 474 | void createDrives() |
| 475 | { |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 476 | for (size_t ii = 0; ii < maxDrives; ii++) |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 477 | { |
Johnathan Mantey | 7045b4b | 2020-06-19 09:24:49 -0700 | [diff] [blame] | 478 | uint8_t driveSlot = (1 << ii); |
| 479 | bool isNvme = ((ifdet & driveSlot) && !(presence & driveSlot)); |
| 480 | bool isPresent = isNvme || (presence & driveSlot); |
| 481 | bool isFailed = !isPresent || failed & driveSlot; |
| 482 | bool isRebuilding = !isPresent && (rebuilding & driveSlot); |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 483 | |
| 484 | // +1 to convert from 0 based to 1 based |
| 485 | size_t driveIndex = (backplaneIndex * maxDrives) + ii + 1; |
James Feist | 09dd231 | 2019-10-09 09:29:03 -0700 | [diff] [blame] | 486 | Drive& drive = drives.emplace_back(driveIndex, isPresent, !isFailed, |
| 487 | isNvme, isRebuilding); |
| 488 | std::shared_ptr<Led> led = leds.emplace_back(std::make_shared<Led>( |
| 489 | drive.itemIface->get_object_path(), ii, file)); |
| 490 | led->createInterface(); |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 494 | void updateDrives() |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 495 | { |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 496 | size_t ii = 0; |
| 497 | |
| 498 | for (auto it = drives.begin(); it != drives.end(); it++, ii++) |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 499 | { |
Johnathan Mantey | 7045b4b | 2020-06-19 09:24:49 -0700 | [diff] [blame] | 500 | uint8_t driveSlot = (1 << ii); |
| 501 | bool isNvme = ((ifdet & driveSlot) && !(presence & driveSlot)); |
| 502 | bool isPresent = isNvme || (presence & driveSlot); |
| 503 | bool isFailed = !isPresent || (failed & driveSlot); |
| 504 | bool isRebuilding = isPresent && (rebuilding & driveSlot); |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 505 | |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 506 | it->isNvme = isNvme; |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 507 | it->setPresent(isPresent); |
Johnathan Mantey | 7045b4b | 2020-06-19 09:24:49 -0700 | [diff] [blame] | 508 | it->logPresent(); |
James Feist | da0c35f | 2020-02-03 10:16:13 -0800 | [diff] [blame] | 509 | |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 510 | it->rebuildingIface->set_property("Rebuilding", isRebuilding); |
| 511 | if (isFailed || isRebuilding) |
| 512 | { |
| 513 | it->markFailed(); |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | it->clearFailed(); |
| 518 | } |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 519 | } |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | bool getPresent() |
| 523 | { |
| 524 | present = i2c_smbus_read_byte(file) >= 0; |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 525 | return present; |
| 526 | } |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 527 | |
| 528 | bool getTypeID(uint8_t& val) |
| 529 | { |
| 530 | constexpr uint8_t addr = 2; |
| 531 | int ret = i2c_smbus_read_byte_data(file, addr); |
| 532 | if (ret < 0) |
| 533 | { |
| 534 | std::cerr << "Error " << __FUNCTION__ << "\n"; |
| 535 | return false; |
| 536 | } |
| 537 | val = static_cast<uint8_t>(ret); |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | bool getBootVer(uint8_t& val) |
| 542 | { |
| 543 | constexpr uint8_t addr = 3; |
| 544 | int ret = i2c_smbus_read_byte_data(file, addr); |
| 545 | if (ret < 0) |
| 546 | { |
| 547 | std::cerr << "Error " << __FUNCTION__ << "\n"; |
| 548 | return false; |
| 549 | } |
| 550 | val = static_cast<uint8_t>(ret); |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | bool getFPGAVer(uint8_t& val) |
| 555 | { |
| 556 | constexpr uint8_t addr = 4; |
| 557 | int ret = i2c_smbus_read_byte_data(file, addr); |
| 558 | if (ret < 0) |
| 559 | { |
| 560 | std::cerr << "Error " << __FUNCTION__ << "\n"; |
| 561 | return false; |
| 562 | } |
| 563 | val = static_cast<uint8_t>(ret); |
| 564 | return true; |
| 565 | } |
| 566 | |
| 567 | bool getSecurityRev(uint8_t& val) |
| 568 | { |
| 569 | constexpr uint8_t addr = 5; |
| 570 | int ret = i2c_smbus_read_byte_data(file, addr); |
| 571 | if (ret < 0) |
| 572 | { |
| 573 | std::cerr << "Error " << __FUNCTION__ << "\n"; |
| 574 | return false; |
| 575 | } |
| 576 | val = static_cast<uint8_t>(ret); |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | bool getPresence(uint8_t& val) |
| 581 | { |
| 582 | // NVMe drives do not assert PRSNTn, and as such do not get reported as |
| 583 | // PRESENT in this register |
| 584 | |
| 585 | constexpr uint8_t addr = 8; |
| 586 | |
| 587 | int ret = i2c_smbus_read_byte_data(file, addr); |
| 588 | if (ret < 0) |
| 589 | { |
| 590 | std::cerr << "Error " << __FUNCTION__ << "\n"; |
| 591 | return false; |
| 592 | } |
| 593 | // presence is inverted |
| 594 | val = static_cast<uint8_t>(~ret); |
| 595 | return true; |
| 596 | } |
| 597 | |
| 598 | bool getIFDET(uint8_t& val) |
| 599 | { |
| 600 | // This register is a bitmap of parallel GPIO pins connected to the |
| 601 | // IFDETn pin of a drive slot. SATA, SAS, and NVMe drives all assert |
| 602 | // IFDETn low when they are inserted into the HSBP.This register, in |
| 603 | // combination with the PRESENCE register, are used by the BMC to detect |
| 604 | // the presence of NVMe drives. |
| 605 | |
| 606 | constexpr uint8_t addr = 9; |
| 607 | |
| 608 | int ret = i2c_smbus_read_byte_data(file, addr); |
| 609 | if (ret < 0) |
| 610 | { |
| 611 | std::cerr << "Error " << __FUNCTION__ << "\n"; |
| 612 | return false; |
| 613 | } |
| 614 | // ifdet is inverted |
| 615 | val = static_cast<uint8_t>(~ret); |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | bool getFailed(uint8_t& val) |
| 620 | { |
| 621 | constexpr uint8_t addr = 0xC; |
| 622 | int ret = i2c_smbus_read_byte_data(file, addr); |
| 623 | if (ret < 0) |
| 624 | { |
| 625 | std::cerr << "Error " << __FUNCTION__ << "\n"; |
| 626 | return false; |
| 627 | } |
| 628 | val = static_cast<uint8_t>(ret); |
| 629 | return true; |
| 630 | } |
| 631 | |
| 632 | bool getRebuild(uint8_t& val) |
| 633 | { |
| 634 | constexpr uint8_t addr = 0xD; |
| 635 | int ret = i2c_smbus_read_byte_data(file, addr); |
| 636 | if (ret < 0) |
| 637 | { |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 638 | std::cerr << "Error " << __FUNCTION__ << " " << strerror(ret) |
| 639 | << "\n"; |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 640 | return false; |
| 641 | } |
| 642 | val = static_cast<uint8_t>(ret); |
| 643 | return true; |
| 644 | } |
| 645 | |
James Feist | 09dd231 | 2019-10-09 09:29:03 -0700 | [diff] [blame] | 646 | virtual ~Backplane() |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 647 | { |
| 648 | objServer.remove_interface(hsbpItemIface); |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 649 | objServer.remove_interface(versionIface); |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 650 | timer.cancel(); |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 651 | if (file >= 0) |
| 652 | { |
| 653 | close(file); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | size_t bus; |
| 658 | size_t address; |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 659 | size_t backplaneIndex; |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 660 | std::string name; |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 661 | boost::asio::steady_timer timer; |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 662 | bool present = false; |
| 663 | uint8_t typeId = 0; |
| 664 | uint8_t bootVer = 0; |
| 665 | uint8_t fpgaVer = 0; |
| 666 | uint8_t securityRev = 0; |
| 667 | uint8_t funSupported = 0; |
| 668 | uint8_t presence = 0; |
| 669 | uint8_t ifdet = 0; |
| 670 | uint8_t failed = 0; |
James Feist | 244f323 | 2019-09-27 15:15:14 -0700 | [diff] [blame] | 671 | uint8_t rebuilding = 0; |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 672 | |
| 673 | int file = -1; |
| 674 | |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 675 | std::string type; |
| 676 | |
| 677 | std::shared_ptr<sdbusplus::asio::dbus_interface> hsbpItemIface; |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 678 | std::shared_ptr<sdbusplus::asio::dbus_interface> versionIface; |
James Feist | d0d36f1 | 2019-11-21 10:19:44 -0800 | [diff] [blame] | 679 | std::shared_ptr<sdbusplus::asio::dbus_interface> storageInterface; |
| 680 | std::shared_ptr<sdbusplus::asio::dbus_interface> assetInterface; |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 681 | |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 682 | std::list<Drive> drives; |
James Feist | 09dd231 | 2019-10-09 09:29:03 -0700 | [diff] [blame] | 683 | std::vector<std::shared_ptr<Led>> leds; |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 684 | std::shared_ptr<boost::container::flat_set<Mux>> muxes; |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 685 | }; |
| 686 | |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 687 | std::unordered_map<std::string, std::shared_ptr<Backplane>> backplanes; |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 688 | std::list<Drive> ownerlessDrives; // drives without a backplane |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 689 | |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 690 | static size_t getDriveCount() |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 691 | { |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 692 | size_t count = 0; |
| 693 | for (const auto& [key, backplane] : backplanes) |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 694 | { |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 695 | count += backplane->drives.size(); |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 696 | } |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 697 | return count + ownerlessDrives.size(); |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 698 | } |
| 699 | |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 700 | void updateAssets() |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 701 | { |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 702 | static constexpr const char* nvmeType = |
| 703 | "xyz.openbmc_project.Inventory.Item.NVMe"; |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 704 | |
| 705 | conn->async_method_call( |
| 706 | [](const boost::system::error_code ec, const GetSubTreeType& subtree) { |
| 707 | if (ec) |
| 708 | { |
| 709 | std::cerr << "Error contacting mapper " << ec.message() << "\n"; |
| 710 | return; |
| 711 | } |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 712 | |
| 713 | // drives may get an owner during this, or we might disover more |
| 714 | // drives |
| 715 | ownerlessDrives.clear(); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 716 | for (const auto& [path, objDict] : subtree) |
| 717 | { |
| 718 | if (objDict.empty()) |
| 719 | { |
| 720 | continue; |
| 721 | } |
| 722 | |
| 723 | const std::string& owner = objDict.begin()->first; |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 724 | // we export this interface too |
| 725 | if (owner == busName) |
| 726 | { |
| 727 | continue; |
| 728 | } |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 729 | if (std::find(objDict.begin()->second.begin(), |
| 730 | objDict.begin()->second.end(), |
| 731 | assetTag) == objDict.begin()->second.end()) |
| 732 | { |
| 733 | // no asset tag to associate to |
| 734 | continue; |
| 735 | } |
| 736 | |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 737 | conn->async_method_call( |
Johnathan Mantey | 7045b4b | 2020-06-19 09:24:49 -0700 | [diff] [blame] | 738 | [path](const boost::system::error_code ec2, |
| 739 | const boost::container::flat_map< |
| 740 | std::string, |
| 741 | std::variant<uint64_t, std::string>>& values) { |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 742 | if (ec2) |
| 743 | { |
| 744 | std::cerr << "Error Getting Config " |
| 745 | << ec2.message() << " " << __FUNCTION__ |
| 746 | << "\n"; |
| 747 | return; |
| 748 | } |
| 749 | auto findBus = values.find("Bus"); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 750 | |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 751 | if (findBus == values.end()) |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 752 | { |
| 753 | std::cerr << "Illegal interface at " << path |
| 754 | << "\n"; |
| 755 | return; |
| 756 | } |
| 757 | |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 758 | // find the mux bus and addr |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 759 | size_t muxBus = static_cast<size_t>( |
| 760 | std::get<uint64_t>(findBus->second)); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 761 | std::filesystem::path muxPath = |
| 762 | "/sys/bus/i2c/devices/i2c-" + |
| 763 | std::to_string(muxBus) + "/mux_device"; |
| 764 | if (!std::filesystem::is_symlink(muxPath)) |
| 765 | { |
| 766 | std::cerr << path << " mux does not exist\n"; |
| 767 | return; |
| 768 | } |
| 769 | |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 770 | // we should be getting something of the form 7-0052 |
| 771 | // for bus 7 addr 52 |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 772 | std::string fname = |
| 773 | std::filesystem::read_symlink(muxPath).filename(); |
| 774 | auto findDash = fname.find('-'); |
| 775 | |
| 776 | if (findDash == std::string::npos || |
| 777 | findDash + 1 >= fname.size()) |
| 778 | { |
| 779 | std::cerr << path << " mux path invalid\n"; |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | std::string busStr = fname.substr(0, findDash); |
| 784 | std::string muxStr = fname.substr(findDash + 1); |
| 785 | |
| 786 | size_t bus = static_cast<size_t>(std::stoi(busStr)); |
| 787 | size_t addr = |
| 788 | static_cast<size_t>(std::stoi(muxStr, nullptr, 16)); |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 789 | size_t muxIndex = 0; |
| 790 | |
| 791 | // find the channel of the mux the drive is on |
| 792 | std::ifstream nameFile("/sys/bus/i2c/devices/i2c-" + |
| 793 | std::to_string(muxBus) + |
| 794 | "/name"); |
| 795 | if (!nameFile) |
| 796 | { |
| 797 | std::cerr << "Unable to open name file of bus " |
| 798 | << muxBus << "\n"; |
| 799 | return; |
| 800 | } |
| 801 | |
| 802 | std::string nameStr; |
| 803 | std::getline(nameFile, nameStr); |
| 804 | |
| 805 | // file is of the form "i2c-4-mux (chan_id 1)", get chan |
| 806 | // assume single digit chan |
| 807 | const std::string prefix = "chan_id "; |
| 808 | size_t findId = nameStr.find(prefix); |
| 809 | if (findId == std::string::npos || |
| 810 | findId + 1 >= nameStr.size()) |
| 811 | { |
| 812 | std::cerr << "Illegal name file on bus " << muxBus |
| 813 | << "\n"; |
| 814 | } |
| 815 | |
| 816 | std::string indexStr = |
| 817 | nameStr.substr(findId + prefix.size(), 1); |
| 818 | |
| 819 | size_t driveIndex = std::stoi(indexStr); |
| 820 | |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 821 | Backplane* parent = nullptr; |
| 822 | for (auto& [name, backplane] : backplanes) |
| 823 | { |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 824 | muxIndex = 0; |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 825 | for (const Mux& mux : *(backplane->muxes)) |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 826 | { |
| 827 | if (bus == mux.bus && addr == mux.address) |
| 828 | { |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 829 | parent = backplane.get(); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 830 | break; |
| 831 | } |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 832 | muxIndex += mux.channels; |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 833 | } |
| 834 | } |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 835 | boost::container::flat_map<std::string, std::string> |
| 836 | assetInventory; |
| 837 | const std::array<const char*, 4> assetKeys = { |
| 838 | "PartNumber", "SerialNumber", "Manufacturer", |
| 839 | "Model"}; |
| 840 | for (const auto& [key, value] : values) |
| 841 | { |
| 842 | if (std::find(assetKeys.begin(), assetKeys.end(), |
| 843 | key) == assetKeys.end()) |
| 844 | { |
| 845 | continue; |
| 846 | } |
| 847 | assetInventory[key] = std::get<std::string>(value); |
| 848 | } |
| 849 | |
| 850 | // assume its a M.2 or something without a hsbp |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 851 | if (parent == nullptr) |
| 852 | { |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 853 | auto& drive = ownerlessDrives.emplace_back( |
| 854 | getDriveCount() + 1, true, true, true, false); |
| 855 | drive.createAsset(assetInventory); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 856 | return; |
| 857 | } |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 858 | |
| 859 | driveIndex += muxIndex; |
| 860 | |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 861 | if (parent->drives.size() <= driveIndex) |
| 862 | { |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 863 | std::cerr << "Illegal drive index at " << path |
| 864 | << " " << driveIndex << "\n"; |
| 865 | return; |
| 866 | } |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 867 | auto it = parent->drives.begin(); |
| 868 | std::advance(it, driveIndex); |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 869 | |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 870 | it->createAsset(assetInventory); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 871 | }, |
| 872 | owner, path, "org.freedesktop.DBus.Properties", "GetAll", |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 873 | "" /*all interface items*/); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 874 | } |
| 875 | }, |
| 876 | mapper::busName, mapper::path, mapper::interface, mapper::subtree, "/", |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 877 | 0, std::array<const char*, 1>{nvmeType}); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 878 | } |
| 879 | |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 880 | void populateMuxes(std::shared_ptr<boost::container::flat_set<Mux>> muxes, |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 881 | std::string& rootPath) |
| 882 | { |
| 883 | const static std::array<const std::string, 4> muxTypes = { |
| 884 | "xyz.openbmc_project.Configuration.PCA9543Mux", |
| 885 | "xyz.openbmc_project.Configuration.PCA9544Mux", |
| 886 | "xyz.openbmc_project.Configuration.PCA9545Mux", |
| 887 | "xyz.openbmc_project.Configuration.PCA9546Mux"}; |
| 888 | conn->async_method_call( |
| 889 | [muxes](const boost::system::error_code ec, |
| 890 | const GetSubTreeType& subtree) { |
| 891 | if (ec) |
| 892 | { |
| 893 | std::cerr << "Error contacting mapper " << ec.message() << "\n"; |
| 894 | return; |
| 895 | } |
| 896 | std::shared_ptr<std::function<void()>> callback = |
| 897 | std::make_shared<std::function<void()>>( |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 898 | []() { updateAssets(); }); |
| 899 | size_t index = 0; // as we use a flat map, these are sorted |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 900 | for (const auto& [path, objDict] : subtree) |
| 901 | { |
| 902 | if (objDict.empty() || objDict.begin()->second.empty()) |
| 903 | { |
| 904 | continue; |
| 905 | } |
| 906 | |
| 907 | const std::string& owner = objDict.begin()->first; |
| 908 | const std::vector<std::string>& interfaces = |
| 909 | objDict.begin()->second; |
| 910 | |
| 911 | const std::string* interface = nullptr; |
| 912 | for (const std::string& iface : interfaces) |
| 913 | { |
| 914 | if (std::find(muxTypes.begin(), muxTypes.end(), iface) != |
| 915 | muxTypes.end()) |
| 916 | { |
| 917 | interface = &iface; |
| 918 | break; |
| 919 | } |
| 920 | } |
| 921 | if (interface == nullptr) |
| 922 | { |
| 923 | std::cerr << "Cannot get mux type\n"; |
| 924 | continue; |
| 925 | } |
| 926 | |
| 927 | conn->async_method_call( |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 928 | [path, muxes, callback, index]( |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 929 | const boost::system::error_code ec2, |
| 930 | const boost::container::flat_map< |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 931 | std::string, |
| 932 | std::variant<uint64_t, std::vector<std::string>>>& |
| 933 | values) { |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 934 | if (ec2) |
| 935 | { |
| 936 | std::cerr << "Error Getting Config " |
| 937 | << ec2.message() << " " << __FUNCTION__ |
| 938 | << "\n"; |
| 939 | return; |
| 940 | } |
| 941 | auto findBus = values.find("Bus"); |
| 942 | auto findAddress = values.find("Address"); |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 943 | auto findChannelNames = values.find("ChannelNames"); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 944 | if (findBus == values.end() || |
| 945 | findAddress == values.end()) |
| 946 | { |
| 947 | std::cerr << "Illegal configuration at " << path |
| 948 | << "\n"; |
| 949 | return; |
| 950 | } |
| 951 | size_t bus = static_cast<size_t>( |
| 952 | std::get<uint64_t>(findBus->second)); |
| 953 | size_t address = static_cast<size_t>( |
| 954 | std::get<uint64_t>(findAddress->second)); |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 955 | std::vector<std::string> channels = |
| 956 | std::get<std::vector<std::string>>( |
| 957 | findChannelNames->second); |
| 958 | muxes->emplace(bus, address, channels.size(), index); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 959 | if (callback.use_count() == 1) |
| 960 | { |
| 961 | (*callback)(); |
| 962 | } |
| 963 | }, |
| 964 | owner, path, "org.freedesktop.DBus.Properties", "GetAll", |
| 965 | *interface); |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 966 | index++; |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 967 | } |
| 968 | }, |
| 969 | mapper::busName, mapper::path, mapper::interface, mapper::subtree, |
| 970 | rootPath, 1, muxTypes); |
| 971 | } |
| 972 | |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 973 | void populate() |
| 974 | { |
| 975 | conn->async_method_call( |
| 976 | [](const boost::system::error_code ec, const GetSubTreeType& subtree) { |
| 977 | if (ec) |
| 978 | { |
| 979 | std::cerr << "Error contacting mapper " << ec.message() << "\n"; |
| 980 | return; |
| 981 | } |
| 982 | for (const auto& [path, objDict] : subtree) |
| 983 | { |
| 984 | if (objDict.empty()) |
| 985 | { |
| 986 | continue; |
| 987 | } |
| 988 | |
| 989 | const std::string& owner = objDict.begin()->first; |
| 990 | conn->async_method_call( |
James Feist | d0d36f1 | 2019-11-21 10:19:44 -0800 | [diff] [blame] | 991 | [path, owner](const boost::system::error_code ec2, |
| 992 | const boost::container::flat_map< |
| 993 | std::string, BasicVariantType>& resp) { |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 994 | if (ec2) |
| 995 | { |
| 996 | std::cerr << "Error Getting Config " |
| 997 | << ec2.message() << "\n"; |
| 998 | return; |
| 999 | } |
| 1000 | backplanes.clear(); |
| 1001 | std::optional<size_t> bus; |
| 1002 | std::optional<size_t> address; |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 1003 | std::optional<size_t> backplaneIndex; |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 1004 | std::optional<std::string> name; |
| 1005 | for (const auto& [key, value] : resp) |
| 1006 | { |
| 1007 | if (key == "Bus") |
| 1008 | { |
| 1009 | bus = std::get<uint64_t>(value); |
| 1010 | } |
| 1011 | else if (key == "Address") |
| 1012 | { |
| 1013 | address = std::get<uint64_t>(value); |
| 1014 | } |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 1015 | else if (key == "Index") |
| 1016 | { |
| 1017 | backplaneIndex = std::get<uint64_t>(value); |
| 1018 | } |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 1019 | else if (key == "Name") |
| 1020 | { |
| 1021 | name = std::get<std::string>(value); |
| 1022 | } |
| 1023 | } |
James Feist | 4577222 | 2019-09-27 10:38:08 -0700 | [diff] [blame] | 1024 | if (!bus || !address || !name || !backplaneIndex) |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 1025 | { |
| 1026 | std::cerr << "Illegal configuration at " << path |
| 1027 | << "\n"; |
| 1028 | return; |
| 1029 | } |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 1030 | std::string parentPath = |
| 1031 | std::filesystem::path(path).parent_path(); |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 1032 | const auto& [backplane, status] = backplanes.emplace( |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 1033 | *name, std::make_shared<Backplane>( |
| 1034 | *bus, *address, *backplaneIndex, *name)); |
| 1035 | backplane->second->run(parentPath, owner); |
| 1036 | populateMuxes(backplane->second->muxes, parentPath); |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 1037 | }, |
| 1038 | owner, path, "org.freedesktop.DBus.Properties", "GetAll", |
| 1039 | configType); |
| 1040 | } |
| 1041 | }, |
| 1042 | mapper::busName, mapper::path, mapper::interface, mapper::subtree, "/", |
| 1043 | 0, std::array<const char*, 1>{configType}); |
| 1044 | } |
| 1045 | |
| 1046 | int main() |
| 1047 | { |
| 1048 | boost::asio::steady_timer callbackTimer(io); |
| 1049 | |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 1050 | conn->request_name(busName); |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 1051 | |
| 1052 | sdbusplus::bus::match::match match( |
| 1053 | *conn, |
| 1054 | "type='signal',member='PropertiesChanged',arg0='" + |
| 1055 | std::string(configType) + "'", |
| 1056 | [&callbackTimer](sdbusplus::message::message&) { |
| 1057 | callbackTimer.expires_after(std::chrono::seconds(2)); |
| 1058 | callbackTimer.async_wait([](const boost::system::error_code ec) { |
| 1059 | if (ec == boost::asio::error::operation_aborted) |
| 1060 | { |
| 1061 | // timer was restarted |
| 1062 | return; |
| 1063 | } |
| 1064 | else if (ec) |
| 1065 | { |
| 1066 | std::cerr << "Timer error" << ec.message() << "\n"; |
| 1067 | return; |
| 1068 | } |
| 1069 | populate(); |
| 1070 | }); |
| 1071 | }); |
| 1072 | |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 1073 | sdbusplus::bus::match::match drive( |
| 1074 | *conn, |
| 1075 | "type='signal',member='PropertiesChanged',arg0='xyz.openbmc_project." |
James Feist | 8675a91 | 2019-10-16 14:36:58 -0700 | [diff] [blame] | 1076 | "Inventory.Item.NVMe'", |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 1077 | [&callbackTimer](sdbusplus::message::message& message) { |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 1078 | callbackTimer.expires_after(std::chrono::seconds(2)); |
James Feist | db2e0e7 | 2019-10-07 16:34:06 -0700 | [diff] [blame] | 1079 | if (message.get_sender() == conn->get_unique_name()) |
| 1080 | { |
| 1081 | return; |
| 1082 | } |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 1083 | callbackTimer.async_wait([](const boost::system::error_code ec) { |
| 1084 | if (ec == boost::asio::error::operation_aborted) |
| 1085 | { |
| 1086 | // timer was restarted |
| 1087 | return; |
| 1088 | } |
| 1089 | else if (ec) |
| 1090 | { |
| 1091 | std::cerr << "Timer error" << ec.message() << "\n"; |
| 1092 | return; |
| 1093 | } |
James Feist | d86629c | 2020-04-23 10:07:25 -0700 | [diff] [blame] | 1094 | updateAssets(); |
James Feist | 0b236ab | 2019-10-02 09:09:16 -0700 | [diff] [blame] | 1095 | }); |
| 1096 | }); |
| 1097 | |
James Feist | 42b49c1 | 2019-10-29 15:18:43 -0700 | [diff] [blame] | 1098 | auto iface = |
| 1099 | objServer.add_interface("/xyz/openbmc_project/inventory/item/storage", |
| 1100 | "xyz.openbmc_project.inventory.item.storage"); |
| 1101 | |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 1102 | io.post([]() { populate(); }); |
James Feist | 9f6565d | 2019-10-09 13:15:13 -0700 | [diff] [blame] | 1103 | setupPowerMatch(conn); |
Feist, James | c95cf67 | 2019-08-29 16:10:35 -0700 | [diff] [blame] | 1104 | io.run(); |
| 1105 | } |