blob: 250c096895b749207f5e6e44f62e93cee3fd8228 [file] [log] [blame]
Eddie Jamesfa1f5c02020-09-17 15:12:46 -05001#pragma once
2
Jeremy Kerr7f7c0852024-08-08 11:32:55 +08003#include <stdint.h>
4
5#include <optional>
Eddie Jamesfa1f5c02020-09-17 15:12:46 -05006#include <span>
Jeremy Kerr8a76d892024-07-26 17:19:57 +08007#include <string>
Jeremy Kerr7f7c0852024-08-08 11:32:55 +08008#include <vector>
Eddie Jamesfa1f5c02020-09-17 15:12:46 -05009
Ratan Guptabbe45792018-03-23 00:22:55 +053010namespace phosphor
11{
12namespace network
13{
14namespace ncsi
15{
16
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053017constexpr auto DEFAULT_VALUE = -1;
18constexpr auto NONE = 0;
Jeremy Kerrb7885242024-09-16 12:43:36 +080019constexpr uint8_t CHANNEL_ID_NONE = 0x1f;
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053020
Jeremy Kerr7f7c0852024-08-08 11:32:55 +080021struct 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
32struct PackageInfo
33{
34 uint32_t id;
35 bool forced;
36 std::vector<ChannelInfo> channels;
37};
38
39struct InterfaceInfo
40{
41 std::vector<PackageInfo> packages;
42};
43
Jeremy Kerrb7885242024-09-16 12:43:36 +080044struct 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
59struct 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
Jeremy Kerr8d9af022024-07-26 16:47:16 +080073struct Interface
74{
Jeremy Kerrbc22f812024-07-29 17:43:35 +080075 /* @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.
Jeremy Kerr147086d2024-08-27 13:46:38 +080084 * @returns the NCSI response message to this command, or no value on error.
Jeremy Kerrbc22f812024-07-29 17:43:35 +080085 */
Jeremy Kerr2d0b48d2024-09-16 13:03:26 +080086 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 */
96 virtual ~Interface() {};
97};
98
99std::string to_string(Interface& interface);
100
101struct NetlinkInterface : Interface
102{
103 /* implementations for Interface */
Jeremy Kerrb7885242024-09-16 12:43:36 +0800104 std::optional<NCSIResponse> sendCommand(NCSICommand& cmd);
Jeremy Kerr2d0b48d2024-09-16 13:03:26 +0800105 std::string toString();
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800106
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
Jeremy Kerr7f7c0852024-08-08 11:32:55 +0800128 * 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.
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800133 */
Jeremy Kerr7f7c0852024-08-08 11:32:55 +0800134 std::optional<InterfaceInfo> getInfo(int package);
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800135
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
Jeremy Kerr2d0b48d2024-09-16 13:03:26 +0800151 NetlinkInterface(int ifindex);
152
Jeremy Kerr8d9af022024-07-26 16:47:16 +0800153 int ifindex;
154};
155
Gunnar Mills57d9c502018-09-14 14:42:34 -0500156} // namespace ncsi
157} // namespace network
158} // namespace phosphor