blob: d55bba7b2f6d3481b95261aab6b64ba18df84dfc [file] [log] [blame]
William A. Kennington III7d6fa422021-02-08 17:04:02 -08001#pragma once
2#include "ncsi_sockio.h"
3#include "net_config.h"
4#include "net_iface.h"
5#include "platforms/nemora/portable/ncsi_client.h"
6#include "platforms/nemora/portable/ncsi_fsm.h"
7#include "platforms/nemora/portable/net_types.h"
8
9#include <optional>
10
11namespace ncsi
12{
13
14typedef ncsi_response_type_t (*ncsi_simple_poll_f)(ncsi_state_t*,
15 network_debug_t*,
16 ncsi_buf_t*, mac_addr_t*,
17 uint32_t, uint16_t);
18
19// This class encapsulates three state machines:
20// * L2 -- performs basic NC-SI setup, reads NIC MAC addr
21// * L3/4 -- once network is configured on the interface,
22// sets up NC-SI filter in the NIC.
23// * Test -- runs several basic NC-SI link tests, like
24// ECHO Request/Reply, checks filter setup etc.
25// Also, reads hostless/host-based flag from the NIC, see
26// ncsi_fsm.c:is_nic_hostless() for details.
27class StateMachine
28{
29 public:
30 StateMachine();
31
32 void set_sockio(net::SockIO* sock_io);
33
34 void set_net_config(net::ConfigBase* net_config);
35
36 // NC-SI State Machine's main function.
37 // max_rounds = 0 means run forever.
38 void run(int max_rounds = 0);
39
40 // How often Test FSM re-runs, in seconds.
41 void set_retest_delay(unsigned int delay);
42
43 private:
44 // Reset the state machine
45 void reset();
46
47 // Poll L2 state machine. Each call advances it by one step.
48 // Its implementation is taken directly from EC.
49 size_t poll_l2_config();
50
51 // This function is used to poll both L3/4 and Test state machine,
52 // depending on the function passed in as an argument.
53 size_t poll_simple(ncsi_simple_poll_f poll_func);
54
55 // Helper function for printing NC-SI error to stdout.
56 void report_ncsi_error(ncsi_response_type_t response_type);
57
58 int receive_ncsi();
59
60 // Helper function for advancing the test FSM.
61 void run_test_fsm(size_t* tx_len);
62
63 // Clear the state and reset all state machines.
64 void clear_state();
65
66 // In current implementation this is the same as clear state,
67 // except that it also increments the failure counter.
68 void fail();
69
70 // Return true if the test state machine finished successfully.
71 bool is_test_done() const
72 {
73 return ncsi_state_.test_state == NCSI_STATE_TEST_END;
74 }
75
76 // Max number of times a state machine is going to retry a command.
77 static constexpr auto MAX_TRIES = 5;
78
79 // How long (in seconds) to wait before re-running NC-SI test state
80 // machine.
81 unsigned int retest_delay_s_ = 1;
82
83 // The last known state of the link on the NIC
84 std::optional<bool> link_up_;
85
86 // The last known hostless mode of the NIC
87 std::optional<bool> hostless_;
88
89 // net_config_ is used to query and set network configuration.
90 // The StateMachine does not own the pointer and it is the
91 // responsibility of the client to make sure that it outlives the
92 // StateMachine.
93 net::ConfigBase* net_config_ = nullptr;
94
95 // sock_io_ is used to read and write NC-SI packets.
96 // The StateMachine does not own the pointer. It is the responsibility
97 // of the client to make sure that sock_io_ outlives the StateMachine.
98 net::SockIO* sock_io_ = nullptr;
99
100 // Both ncsi_state_ and network_debug_ parameters represent the state of
101 // the NC-SI state machine. The names and definitions are taken directly
102 // from EC.
103 ncsi_state_t ncsi_state_;
104 network_debug_t network_debug_;
105
106 // Depending on the state ncsi_buf_ represents either the NC-SI packet
107 // received from the NIC or NC-SI packet that was (or about to be)
108 // sent to the NIC.
109 ncsi_buf_t ncsi_buf_;
110};
111
112} // namespace ncsi