xref: /openbmc/phosphor-networkd/src/ncsi_util.hpp (revision 2d0b48dac602196e1f4d2f3d47830f6b88b5186f)
1 #pragma once
2 
3 #include <stdint.h>
4 
5 #include <optional>
6 #include <span>
7 #include <string>
8 #include <vector>
9 
10 namespace phosphor
11 {
12 namespace network
13 {
14 namespace ncsi
15 {
16 
17 constexpr auto DEFAULT_VALUE = -1;
18 constexpr auto NONE = 0;
19 constexpr uint8_t CHANNEL_ID_NONE = 0x1f;
20 
21 struct ChannelInfo
22 {
23     uint32_t id;
24     bool active;
25     bool forced;
26     uint32_t version_major, version_minor;
27     std::string version;
28     uint32_t link_state;
29     std::vector<uint16_t> vlan_ids;
30 };
31 
32 struct PackageInfo
33 {
34     uint32_t id;
35     bool forced;
36     std::vector<ChannelInfo> channels;
37 };
38 
39 struct InterfaceInfo
40 {
41     std::vector<PackageInfo> packages;
42 };
43 
44 struct NCSICommand
45 {
46     /* constructs a message; the payload span is copied into the internal
47      * command vector */
48     NCSICommand(uint8_t opcode, uint8_t package, std::optional<uint8_t> channel,
49                 std::span<unsigned char> payload);
50 
51     uint8_t getChannel();
52 
53     uint8_t opcode;
54     uint8_t package;
55     std::optional<uint8_t> channel;
56     std::vector<unsigned char> payload;
57 };
58 
59 struct NCSIResponse
60 {
61     uint8_t opcode;
62     uint8_t response, reason;
63     std::span<unsigned char> payload;
64     std::vector<unsigned char> full_payload;
65 
66     /* Given an incoming response with full_payload set, check that we have
67      * enough data for a correct response, and populate the rest of the struct
68      * to suit
69      */
70     int parseFullPayload();
71 };
72 
73 struct Interface
74 {
75     /* @brief  This function will ask underlying NCSI driver
76      *         to send an OEM command (command type 0x50) with
77      *         the specified payload as the OEM data.
78      *         This function talks with the NCSI driver over
79      *         netlink messages.
80      * @param[in] package - NCSI Package.
81      * @param[in] channel - Channel number with in the package.
82      * @param[in] opcode  - NCSI Send Command sub-operation
83      * @param[in] payload - OEM data to send.
84      * @returns the NCSI response message to this command, or no value on error.
85      */
86     virtual std::optional<NCSIResponse> sendCommand(NCSICommand& cmd) = 0;
87 
88     /**
89      * @brief Create a string representation of this interface
90      *
91      * @returns a string containing an interface identifier, for logging
92      */
93     virtual std::string toString() = 0;
94 
95     /* virtual destructor for vtable */
~Interfacephosphor::network::ncsi::Interface96     virtual ~Interface() {};
97 };
98 
99 std::string to_string(Interface& interface);
100 
101 struct NetlinkInterface : Interface
102 {
103     /* implementations for Interface */
104     std::optional<NCSIResponse> sendCommand(NCSICommand& cmd);
105     std::string toString();
106 
107     /* @brief  This function will ask underlying NCSI driver
108      *         to set a specific  package or package/channel
109      *         combination as the preferred choice.
110      *         This function talks with the NCSI driver over
111      *         netlink messages.
112      * @param[in] package - NCSI Package.
113      * @param[in] channel - Channel number with in the package.
114      * @returns 0 on success and negative value for failure.
115      */
116     int setChannel(int package, int channel);
117 
118     /* @brief  This function will ask underlying NCSI driver
119      *         to clear any preferred setting from the interface.
120      *         This function talks with the NCSI driver over
121      *         netlink messages.
122      * @returns 0 on success and negative value for failure.
123      */
124     int clearInterface();
125 
126     /* @brief  This function is used to dump all the info
127      *         of the package and the channels underlying
128      *         the package, or all packages if DEFAULT_VALUE
129      *         is passed
130      * @param[in] package - NCSI Package
131      * @returns an InterfaceInfo with package data the specified pacakge,
132      *          or all packages if none is specified.
133      */
134     std::optional<InterfaceInfo> getInfo(int package);
135 
136     /* @brief  This function assigns a mask controlling responses to AEN from a
137      * package.
138      * @param[in] mask - A 32-bit mask integer
139      * @returns 0 on success and negative value for failure.
140      */
141     int setPackageMask(unsigned int mask);
142 
143     /* @brief  This function sets the AEN mask for the channels inside the
144      * selected package.
145      * @param[in] package - NCSI Package.
146      * @param[in] mask - A 32-bit mask integer
147      * @returns 0 on success and negative value for failure.
148      */
149     int setChannelMask(int package, unsigned int mask);
150 
151     NetlinkInterface(int ifindex);
152 
153     int ifindex;
154 };
155 
156 } // namespace ncsi
157 } // namespace network
158 } // namespace phosphor
159