xref: /openbmc/phosphor-logging/extensions/openpower-pels/device_callouts.hpp (revision 075c79237505ea3b810a461f5f514e4d520a0c44)
118c42b0fSMatt Spinler #pragma once
218c42b0fSMatt Spinler 
318c42b0fSMatt Spinler #include <nlohmann/json.hpp>
42544b419SPatrick Williams 
52544b419SPatrick Williams #include <filesystem>
618c42b0fSMatt Spinler #include <string>
718c42b0fSMatt Spinler #include <tuple>
818c42b0fSMatt Spinler #include <vector>
918c42b0fSMatt Spinler 
1018c42b0fSMatt Spinler /**
1118c42b0fSMatt Spinler  * @file device_callouts.hpp
1218c42b0fSMatt Spinler  *
1318c42b0fSMatt Spinler  * @brief Looks up FRU callouts, which are D-Bus inventory paths,
1418c42b0fSMatt Spinler  * in JSON for device sysfs paths.
1518c42b0fSMatt Spinler  *
1618c42b0fSMatt Spinler  * The code will extract the search keys from the sysfs path to
1718c42b0fSMatt Spinler  * use to look up the callout list in the JSON.  The callouts will
1818c42b0fSMatt Spinler  * be sorted by priority as defined in th PEL spec.
1918c42b0fSMatt Spinler  *
2018c42b0fSMatt Spinler  * The JSON is normally generated from the MRW, and contains
2118c42b0fSMatt Spinler  * sections for the following types of callouts:
2218c42b0fSMatt Spinler  *   * I2C  (also based on bus/addr as well as the path)
2318c42b0fSMatt Spinler  *   * FSI
2418c42b0fSMatt Spinler  *   * FSI-I2C
2518c42b0fSMatt Spinler  *   * FSI-SPI
2618c42b0fSMatt Spinler  *
2718c42b0fSMatt Spinler  * The JSON looks like:
2818c42b0fSMatt Spinler  *
2918c42b0fSMatt Spinler  * "I2C":
3018c42b0fSMatt Spinler  *   "<bus>":
3118c42b0fSMatt Spinler  *     "<address>":
3218c42b0fSMatt Spinler  *       "Callouts": [
3318c42b0fSMatt Spinler  *          {
3418c42b0fSMatt Spinler  *             "LocationCode": "<location code>",
3518c42b0fSMatt Spinler  *             "Name": "<inventory path>",
3618c42b0fSMatt Spinler  *             "Priority": "<priority=H/M/L>",
3718c42b0fSMatt Spinler  *             "MRU": "<optional MRU name>"
3818c42b0fSMatt Spinler  *          },
3918c42b0fSMatt Spinler  *          ...
4018c42b0fSMatt Spinler  *       ],
4118c42b0fSMatt Spinler  *       "Dest": "<destination MRW target>"
4218c42b0fSMatt Spinler  *
4318c42b0fSMatt Spinler  * "FSI":
4418c42b0fSMatt Spinler  *   "<fsi link>":
4518c42b0fSMatt Spinler  *     "Callouts": [
4618c42b0fSMatt Spinler  *            ...
4718c42b0fSMatt Spinler  *     ],
4818c42b0fSMatt Spinler  *     "Dest": "<destination MRW target>"
4918c42b0fSMatt Spinler  *
5018c42b0fSMatt Spinler  * "FSI-I2C":
5118c42b0fSMatt Spinler  *    "<fsi link>":
5218c42b0fSMatt Spinler  *      "<bus>":
5318c42b0fSMatt Spinler  *        "<address>":
5418c42b0fSMatt Spinler  *            "Callouts": [
5518c42b0fSMatt Spinler  *                   ...
5618c42b0fSMatt Spinler  *             ],
5718c42b0fSMatt Spinler  *            "Dest": "<destination MRW target>"
5818c42b0fSMatt Spinler  *
5918c42b0fSMatt Spinler  * "FSI-SPI":
6018c42b0fSMatt Spinler  *    "<fsi link>":
6118c42b0fSMatt Spinler  *      "<bus>":
6218c42b0fSMatt Spinler  *         "Callouts": [
6318c42b0fSMatt Spinler  *                ...
6418c42b0fSMatt Spinler  *         ],
6518c42b0fSMatt Spinler  *         "Dest": "<destination MRW target>"
6618c42b0fSMatt Spinler  *
6718c42b0fSMatt Spinler  */
6818c42b0fSMatt Spinler 
6918c42b0fSMatt Spinler namespace openpower::pels::device_callouts
7018c42b0fSMatt Spinler {
7118c42b0fSMatt Spinler 
7218c42b0fSMatt Spinler /**
7318c42b0fSMatt Spinler  * @brief Represents a callout in the device JSON.
7418c42b0fSMatt Spinler  *
7518c42b0fSMatt Spinler  * The debug field will only be filled in for the first
7618c42b0fSMatt Spinler  * callout in the list of them and contains additional
7718c42b0fSMatt Spinler  * information about what happened when looking up the
7818c42b0fSMatt Spinler  * callouts that is meant to aid in debug.
7918c42b0fSMatt Spinler  */
8018c42b0fSMatt Spinler struct Callout
8118c42b0fSMatt Spinler {
8218c42b0fSMatt Spinler     std::string priority;
8318c42b0fSMatt Spinler     std::string locationCode;
8418c42b0fSMatt Spinler     std::string name;
8518c42b0fSMatt Spinler     std::string mru;
8618c42b0fSMatt Spinler     std::string debug;
8718c42b0fSMatt Spinler };
8818c42b0fSMatt Spinler 
8918c42b0fSMatt Spinler /**
9018c42b0fSMatt Spinler  * @brief Looks up the callouts in a JSON File to add to a PEL
9118c42b0fSMatt Spinler  *        for when the path between the BMC and the device specified
9218c42b0fSMatt Spinler  *        by the passed in device path needs to be called out.
9318c42b0fSMatt Spinler  *
9418c42b0fSMatt Spinler  * The path is the path used to access the device in sysfs.  It
9518c42b0fSMatt Spinler  * can be either a directory path or a file path.
9618c42b0fSMatt Spinler  *
9718c42b0fSMatt Spinler  * @param[in] devPath - The device path
9818c42b0fSMatt Spinler  * @param[in] compatibleList - The list of compatible names for this
9918c42b0fSMatt Spinler  *                             system.
10018c42b0fSMatt Spinler  * @return std::vector<Callout> - The list of callouts
10118c42b0fSMatt Spinler  */
102*075c7923SPatrick Williams std::vector<Callout> getCallouts(
103*075c7923SPatrick Williams     const std::string& devPath, const std::vector<std::string>& compatibleList);
10418c42b0fSMatt Spinler 
10518c42b0fSMatt Spinler /**
10618c42b0fSMatt Spinler  * @brief Looks up the callouts to add to a PEL for when the path
10718c42b0fSMatt Spinler  *        between the BMC and the device specified by the passed in
10818c42b0fSMatt Spinler  *        I2C bus and address needs to be called out.
10918c42b0fSMatt Spinler  *
11018c42b0fSMatt Spinler  * @param[in] i2cBus - The I2C bus
11118c42b0fSMatt Spinler  * @param[in] i2cAddress - The I2C address
11218c42b0fSMatt Spinler  * @param[in] compatibleList - The list of compatible names for this
11318c42b0fSMatt Spinler  *                             system.
11418c42b0fSMatt Spinler  * @return std::vector<Callout> - The list of callouts
11518c42b0fSMatt Spinler  */
11618c42b0fSMatt Spinler std::vector<Callout>
11718c42b0fSMatt Spinler     getI2CCallouts(size_t i2cBus, uint8_t i2cAddress,
11818c42b0fSMatt Spinler                    const std::vector<std::string>& compatibleList);
11918c42b0fSMatt Spinler 
12018c42b0fSMatt Spinler namespace util
12118c42b0fSMatt Spinler {
12218c42b0fSMatt Spinler 
12318c42b0fSMatt Spinler /**
124a307089cSMatt Spinler  * @brief The different callout path types
125a307089cSMatt Spinler  */
126a307089cSMatt Spinler enum class CalloutType
127a307089cSMatt Spinler {
128a307089cSMatt Spinler     i2c,
129a307089cSMatt Spinler     fsi,
130a307089cSMatt Spinler     fsii2c,
131a307089cSMatt Spinler     fsispi,
132a307089cSMatt Spinler     unknown
133a307089cSMatt Spinler };
134a307089cSMatt Spinler 
135a307089cSMatt Spinler /**
13618c42b0fSMatt Spinler  * @brief Returns the path to the JSON file to look up callouts in.
13718c42b0fSMatt Spinler  *
13818c42b0fSMatt Spinler  * @param[in] compatibleList - The list of compatible names for this
13918c42b0fSMatt Spinler  *                             system.
14018c42b0fSMatt Spinler  *
14118c42b0fSMatt Spinler  * @return path - The path to the file.
14218c42b0fSMatt Spinler  */
14318c42b0fSMatt Spinler std::filesystem::path
14418c42b0fSMatt Spinler     getJSONFilename(const std::vector<std::string>& compatibleList);
14518c42b0fSMatt Spinler 
14618c42b0fSMatt Spinler /**
14718c42b0fSMatt Spinler  * @brief Looks up the callouts in the JSON using the I2C keys.
14818c42b0fSMatt Spinler  *
14918c42b0fSMatt Spinler  * @param[in] i2cBus - The I2C bus
15018c42b0fSMatt Spinler  * @param[in] i2cAddress - The I2C address
15118c42b0fSMatt Spinler  * @param[in] calloutJSON - The JSON containing the callouts
15218c42b0fSMatt Spinler  *
15318c42b0fSMatt Spinler  * @return std::vector<Callout> - The callouts
15418c42b0fSMatt Spinler  */
155*075c7923SPatrick Williams std::vector<device_callouts::Callout> calloutI2C(
156*075c7923SPatrick Williams     size_t i2CBus, uint8_t i2cAddress, const nlohmann::json& calloutJSON);
157a307089cSMatt Spinler 
158a307089cSMatt Spinler /**
159a307089cSMatt Spinler  * @brief Determines the type of the path (FSI, I2C, etc) based
160a307089cSMatt Spinler  *        on tokens in the device path.
161a307089cSMatt Spinler  *
162a307089cSMatt Spinler  * @param[in] devPath - The device path
163a307089cSMatt Spinler  *
164a307089cSMatt Spinler  * @return CalloutType - The callout type
165a307089cSMatt Spinler  */
166a307089cSMatt Spinler CalloutType getCalloutType(const std::string& devPath);
167a307089cSMatt Spinler 
16844c0a643SMatt Spinler /**
16944c0a643SMatt Spinler  * @brief Pulls the fields out of the I2C device path to use as search keys
17044c0a643SMatt Spinler  *        in the JSON.
17144c0a643SMatt Spinler  *
17244c0a643SMatt Spinler  * The keys are the I2C bus and address.
17344c0a643SMatt Spinler  *
17444c0a643SMatt Spinler  * @param[in] devPath - The device path
17544c0a643SMatt Spinler  *
17644c0a643SMatt Spinler  * @return std::tuple<size_t, uint8_t> - The I2C bus and address keys
17744c0a643SMatt Spinler  */
17844c0a643SMatt Spinler std::tuple<size_t, uint8_t> getI2CSearchKeys(const std::string& devPath);
17944c0a643SMatt Spinler 
18044c0a643SMatt Spinler /**
18144c0a643SMatt Spinler  * @brief Pulls the fields out of the FSI device path to use as search keys
18244c0a643SMatt Spinler  *        in the JSON.
18344c0a643SMatt Spinler  *
18444c0a643SMatt Spinler  * The key is the FSI link.  For multi-hop paths, the links are
18544c0a643SMatt Spinler  * separated by '-'s, like "0-1-2".
18644c0a643SMatt Spinler  *
18744c0a643SMatt Spinler  * @param[in] devPath - The device path
18844c0a643SMatt Spinler  *
18944c0a643SMatt Spinler  * @return std::string - The FSI links key
19044c0a643SMatt Spinler  */
19144c0a643SMatt Spinler std::string getFSISearchKeys(const std::string& devPath);
19244c0a643SMatt Spinler 
19344c0a643SMatt Spinler /**
19444c0a643SMatt Spinler  * @brief Pulls the fields out of the FSI-I2C device path to use as
19544c0a643SMatt Spinler  *        search keys in the JSON.
19644c0a643SMatt Spinler  *
19744c0a643SMatt Spinler  * The keys are the FSI link string and the  I2C bus and address.
19844c0a643SMatt Spinler  *
19944c0a643SMatt Spinler  * @param[in] devPath - The device path
20044c0a643SMatt Spinler  *
20144c0a643SMatt Spinler  * @return std::tuple<std::string, std::tuple<size_t, uint8_t>>
20244c0a643SMatt Spinler  *         - The FSI links key along with the I2C bus/address.
20344c0a643SMatt Spinler  */
20444c0a643SMatt Spinler std::tuple<std::string, std::tuple<size_t, uint8_t>>
20544c0a643SMatt Spinler     getFSII2CSearchKeys(const std::string& devPath);
20644c0a643SMatt Spinler 
20744c0a643SMatt Spinler /**
20844c0a643SMatt Spinler  * @brief Pulls the fields out of the SPI device path to use as search keys
20944c0a643SMatt Spinler  *        in the JSON.
21044c0a643SMatt Spinler  *
21144c0a643SMatt Spinler  * The key is the SPI bus number.
21244c0a643SMatt Spinler  *
21344c0a643SMatt Spinler  * @param[in] devPath - The device path
21444c0a643SMatt Spinler  *
21544c0a643SMatt Spinler  * @return size_t - The SPI bus key
21644c0a643SMatt Spinler  */
21744c0a643SMatt Spinler size_t getSPISearchKeys(const std::string& devPath);
21844c0a643SMatt Spinler 
21944c0a643SMatt Spinler /**
22044c0a643SMatt Spinler  * @brief Pulls the fields out of the FSI-SPI device path to use as
22144c0a643SMatt Spinler  *        search keys in the JSON.
22244c0a643SMatt Spinler  *
22344c0a643SMatt Spinler  * The keys are the FSI link string and the SPI bus number.
22444c0a643SMatt Spinler  *
22544c0a643SMatt Spinler  * @param[in] devPath - The device path
22644c0a643SMatt Spinler  *
22744c0a643SMatt Spinler  * @return std::tuple<std::string, size_t>
22844c0a643SMatt Spinler  *         - The FSI links key along with the SPI bus number.
22944c0a643SMatt Spinler  */
23044c0a643SMatt Spinler std::tuple<std::string, size_t> getFSISPISearchKeys(const std::string& devPath);
23144c0a643SMatt Spinler 
23218c42b0fSMatt Spinler } // namespace util
23318c42b0fSMatt Spinler } // namespace openpower::pels::device_callouts
234