1 #pragma once 2 3 #include <filesystem> 4 #include <string> 5 #include <vector> 6 7 namespace phosphor 8 { 9 namespace pmbus 10 { 11 12 namespace fs = std::filesystem; 13 14 // The file name Linux uses to capture the READ_VIN from pmbus. 15 constexpr auto READ_VIN = "in1_input"; 16 17 namespace in_input 18 { 19 // VIN thresholds in Volts 20 constexpr auto VIN_VOLTAGE_MIN = 20; 21 constexpr auto VIN_VOLTAGE_110_THRESHOLD = 160; 22 23 // VIN actual values in Volts 24 // VIN_VOLTAGE_0: VIN < VIN_VOLTAGE_MIN 25 // VIN_VOLTAGE_110: VIN_VOLTAGE_MIN < VIN < VIN_VOLTAGE_110_THRESHOLD 26 // VIN_VOLTAGE_220: VIN_VOLTAGE_110_THRESHOLD < VIN 27 constexpr auto VIN_VOLTAGE_0 = 0; 28 constexpr auto VIN_VOLTAGE_110 = 110; 29 constexpr auto VIN_VOLTAGE_220 = 220; 30 } // namespace in_input 31 32 // The file name Linux uses to capture the STATUS_WORD from pmbus. 33 constexpr auto STATUS_WORD = "status0"; 34 35 // The file name Linux uses to capture the STATUS_INPUT from pmbus. 36 constexpr auto STATUS_INPUT = "status0_input"; 37 38 // Voltage out status. 39 // Overvoltage fault or warning, Undervoltage fault or warning, maximum or 40 // minimum warning, .... 41 // Uses Page substitution 42 constexpr auto STATUS_VOUT = "statusP_vout"; 43 44 namespace status_vout 45 { 46 // Mask of bits that are only warnings 47 constexpr auto WARNING_MASK = 0x6A; 48 } // namespace status_vout 49 50 // Current output status bits. 51 constexpr auto STATUS_IOUT = "status0_iout"; 52 53 // Manufacturing specific status bits 54 constexpr auto STATUS_MFR = "status0_mfr"; 55 56 // Reports on the status of any fans installed in position 1 and 2. 57 constexpr auto STATUS_FANS_1_2 = "status0_fans12"; 58 59 // Reports on temperature faults or warnings. Overtemperature fault, 60 // overtemperature warning, undertemperature warning, undertemperature fault. 61 constexpr auto STATUS_TEMPERATURE = "status0_temp"; 62 63 // Reports on the communication, memory, logic fault(s). 64 constexpr auto STATUS_CML = "status0_cml"; 65 66 namespace status_word 67 { 68 constexpr auto VOUT_FAULT = 0x8000; 69 70 // The IBM CFF power supply driver does map this bit to power1_alarm in the 71 // hwmon space, but since the other bits that need to be checked do not have 72 // a similar mapping, the code will just read STATUS_WORD and use bit masking 73 // to see if the INPUT FAULT OR WARNING bit is on. 74 constexpr auto INPUT_FAULT_WARN = 0x2000; 75 76 // The bit mask representing the MFRSPECIFIC fault, bit 4 of STATUS_WORD high 77 // byte. A manufacturer specific fault or warning has occurred. 78 constexpr auto MFR_SPECIFIC_FAULT = 0x1000; 79 80 // The bit mask representing the POWER_GOOD Negated bit of the STATUS_WORD. 81 constexpr auto POWER_GOOD_NEGATED = 0x0800; 82 83 // The bit mask representing the FAN FAULT or WARNING bit of the STATUS_WORD. 84 // Bit 2 of the high byte of STATUS_WORD. 85 constexpr auto FAN_FAULT = 0x0400; 86 87 // The bit mask representing the UNITI_IS_OFF bit of the STATUS_WORD. 88 constexpr auto UNIT_IS_OFF = 0x0040; 89 90 // Bit 5 of the STATUS_BYTE, or lower byte of STATUS_WORD is used to indicate 91 // an output overvoltage fault. 92 constexpr auto VOUT_OV_FAULT = 0x0020; 93 94 // The bit mask representing that an output overcurrent fault has occurred. 95 constexpr auto IOUT_OC_FAULT = 0x0010; 96 97 // The IBM CFF power supply driver does map this bit to in1_alarm, however, 98 // since a number of the other bits are not mapped that way for STATUS_WORD, 99 // this code will just read the entire STATUS_WORD and use bit masking to find 100 // out if that fault is on. 101 constexpr auto VIN_UV_FAULT = 0x0008; 102 103 // The bit mask representing the TEMPERATURE FAULT or WARNING bit of the 104 // STATUS_WORD. Bit 2 of the low byte (STATUS_BYTE). 105 constexpr auto TEMPERATURE_FAULT_WARN = 0x0004; 106 107 // The bit mask representing the CML (Communication, Memory, and/or Logic) fault 108 // bit of the STATUS_WORD. Bit 1 of the low byte (STATUS_BYTE). 109 constexpr auto CML_FAULT = 0x0002; 110 } // namespace status_word 111 112 namespace status_vout 113 { 114 // The IBM CFF power supply driver maps MFR's OV_FAULT and VAUX_FAULT to this 115 // bit. 116 constexpr auto OV_FAULT = 0x80; 117 118 // The IBM CFF power supply driver maps MFR's UV_FAULT to this bit. 119 constexpr auto UV_FAULT = 0x10; 120 } // namespace status_vout 121 122 namespace status_temperature 123 { 124 // Overtemperature Fault 125 constexpr auto OT_FAULT = 0x80; 126 } // namespace status_temperature 127 128 constexpr auto ON_OFF_CONFIG = "on_off_config"; 129 130 // From PMBus Specification Part II Revsion 1.2: 131 // The ON_OFF_CONFIG command configures the combination of CONTROL pin input 132 // and serial bus commands needed to turn the unit on and off. This includes how 133 // the unit responds when power is applied. 134 // Bits [7:5] - 000 - Reserved 135 // Bit 4 - 1 - Unit does not power up until commanded by the CONTROL pin and 136 // OPERATION command (as programmed in bits [3:0]). 137 // Bit 3 - 0 - Unit ignores the on/off portion of the OPERATION command from 138 // serial bus. 139 // Bit 2 - 1 - Unit requires the CONTROL pin to be asserted to start the unit. 140 // Bit 1 - 0 - Polarity of the CONTROL pin. Active low (Pull pin low to start 141 // the unit). 142 // Bit 0 - 1 - Turn off the output and stop transferring energy to the output as 143 // fast as possible. 144 constexpr auto ON_OFF_CONFIG_CONTROL_PIN_ONLY = 0x15; 145 146 /** 147 * Where the access should be done 148 */ 149 enum class Type 150 { 151 Base, // base device directory 152 Hwmon, // hwmon directory 153 Debug, // pmbus debug directory 154 DeviceDebug, // device debug directory 155 HwmonDeviceDebug // hwmon device debug directory 156 }; 157 158 /** 159 * @class PMBusBase 160 * 161 * This is a base class for PMBus to assist with unit testing via mocking. 162 */ 163 class PMBusBase 164 { 165 public: 166 virtual ~PMBusBase() = default; 167 168 virtual uint64_t read(const std::string& name, Type type) = 0; 169 virtual std::string readString(const std::string& name, Type type) = 0; 170 virtual void writeBinary(const std::string& name, std::vector<uint8_t> data, 171 Type type) = 0; 172 virtual void findHwmonDir() = 0; 173 virtual const fs::path& path() const = 0; 174 }; 175 176 /** 177 * Wrapper function for PMBus 178 * 179 * @param[in] bus - I2C bus 180 * @param[in] address - I2C address (as a 2-byte string, e.g. 0069) 181 * 182 * @return PMBusBase pointer 183 */ 184 std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus, 185 const std::string& address); 186 187 /** 188 * @class PMBus 189 * 190 * This class is an interface to communicating with PMBus devices 191 * by reading and writing sysfs files. 192 * 193 * Based on the Type parameter, the accesses can either be done 194 * in the base device directory (the one passed into the constructor), 195 * or in the hwmon directory for the device. 196 */ 197 class PMBus : public PMBusBase 198 { 199 public: 200 PMBus() = delete; 201 virtual ~PMBus() = default; 202 PMBus(const PMBus&) = default; 203 PMBus& operator=(const PMBus&) = default; 204 PMBus(PMBus&&) = default; 205 PMBus& operator=(PMBus&&) = default; 206 207 /** 208 * Constructor 209 * 210 * @param[in] path - path to the sysfs directory 211 */ 212 PMBus(const std::string& path) : basePath(path) 213 { 214 findHwmonDir(); 215 } 216 217 /** 218 * Constructor 219 * 220 * This version is required when DeviceDebug 221 * access will be used. 222 * 223 * @param[in] path - path to the sysfs directory 224 * @param[in] driverName - the device driver name 225 * @param[in] instance - chip instance number 226 */ 227 PMBus(const std::string& path, const std::string& driverName, 228 size_t instance) : 229 basePath(path), 230 driverName(driverName), instance(instance) 231 { 232 findHwmonDir(); 233 } 234 235 /** 236 * Wrapper function for PMBus 237 * 238 * @param[in] bus - I2C bus 239 * @param[in] address - I2C address (as a 2-byte string, e.g. 0069) 240 * 241 * @return PMBusBase pointer 242 */ 243 static std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus, 244 const std::string& address); 245 246 /** 247 * Reads a file in sysfs that represents a single bit, 248 * therefore doing a PMBus read. 249 * 250 * @param[in] name - path concatenated to 251 * basePath to read 252 * @param[in] type - Path type 253 * 254 * @return bool - false if result was 0, else true 255 */ 256 bool readBit(const std::string& name, Type type); 257 258 /** 259 * Reads a file in sysfs that represents a single bit, 260 * where the page number passed in is substituted 261 * into the name in place of the 'P' character in it. 262 * 263 * @param[in] name - path concatenated to 264 * basePath to read 265 * @param[in] page - page number 266 * @param[in] type - Path type 267 * 268 * @return bool - false if result was 0, else true 269 */ 270 bool readBitInPage(const std::string& name, size_t page, Type type); 271 /** 272 * Checks if the file for the given name and type exists. 273 * 274 * @param[in] name - path concatenated to basePath to read 275 * @param[in] type - Path type 276 * 277 * @return bool - True if file exists, false if it does not. 278 */ 279 bool exists(const std::string& name, Type type); 280 281 /** 282 * Read byte(s) from file in sysfs. 283 * 284 * @param[in] name - path concatenated to basePath to read 285 * @param[in] type - Path type 286 * 287 * @return uint64_t - Up to 8 bytes of data read from file. 288 */ 289 uint64_t read(const std::string& name, Type type) override; 290 291 /** 292 * Read a string from file in sysfs. 293 * 294 * @param[in] name - path concatenated to basePath to read 295 * @param[in] type - Path type 296 * 297 * @return string - The data read from the file. 298 */ 299 std::string readString(const std::string& name, Type type) override; 300 301 /** 302 * Read data from a binary file in sysfs. 303 * 304 * @param[in] name - path concatenated to basePath to read 305 * @param[in] type - Path type 306 * @param[in] length - length of data to read, in bytes 307 * 308 * @return vector<uint8_t> - The data read from the file. 309 */ 310 std::vector<uint8_t> readBinary(const std::string& name, Type type, 311 size_t length); 312 313 /** 314 * Writes an integer value to the file, therefore doing 315 * a PMBus write. 316 * 317 * @param[in] name - path concatenated to 318 * basePath to write 319 * @param[in] value - the value to write 320 * @param[in] type - Path type 321 */ 322 void write(const std::string& name, int value, Type type); 323 324 /** 325 * Writes binary data to a file in sysfs. 326 * 327 * @param[in] name - path concatenated to basePath to write 328 * @param[in] data - The data to write to the file 329 * @param[in] type - Path type 330 */ 331 void writeBinary(const std::string& name, std::vector<uint8_t> data, 332 Type type) override; 333 334 /** 335 * Returns the sysfs base path of this device 336 */ 337 const fs::path& path() const override 338 { 339 return basePath; 340 } 341 342 /** 343 * Replaces the 'P' in the string passed in with 344 * the page number passed in. 345 * 346 * For example: 347 * insertPageNum("inP_enable", 42) 348 * returns "in42_enable" 349 * 350 * @param[in] templateName - the name string, with a 'P' in it 351 * @param[in] page - the page number to insert where the P was 352 * 353 * @return string - the new string with the page number in it 354 */ 355 static std::string insertPageNum(const std::string& templateName, 356 size_t page); 357 358 /** 359 * Finds the path relative to basePath to the hwmon directory 360 * for the device and stores it in hwmonRelPath. 361 */ 362 void findHwmonDir() override; 363 364 /** 365 * Returns the path to use for the passed in type. 366 * 367 * @param[in] type - Path type 368 * 369 * @return fs::path - the full path 370 */ 371 fs::path getPath(Type type); 372 373 private: 374 /** 375 * Returns the device name 376 * 377 * This is found in the 'name' file in basePath. 378 * 379 * @return string - the device name 380 */ 381 std::string getDeviceName(); 382 383 /** 384 * The sysfs device path 385 */ 386 fs::path basePath; 387 388 /** 389 * The directory name under the basePath hwmon directory 390 */ 391 fs::path hwmonDir; 392 393 /** 394 * The device driver name. Used for finding the device 395 * debug directory. Not required if that directory 396 * isn't used. 397 */ 398 std::string driverName; 399 400 /** 401 * The device instance number. 402 * 403 * Used in conjunction with the driver name for finding 404 * the debug directory. Not required if that directory 405 * isn't used. 406 */ 407 size_t instance = 0; 408 409 /** 410 * The pmbus debug path with status files 411 */ 412 const fs::path debugPath = "/sys/kernel/debug/"; 413 }; 414 415 } // namespace pmbus 416 } // namespace phosphor 417