1f60ac27eSMatt Spinler #pragma once
2f60ac27eSMatt Spinler 
3f60ac27eSMatt Spinler #include "host_interface.hpp"
4f60ac27eSMatt Spinler #include "pel.hpp"
5f60ac27eSMatt Spinler #include "repository.hpp"
6f60ac27eSMatt Spinler 
7f60ac27eSMatt Spinler #include <deque>
8f869fcf8SMatt Spinler #include <sdeventplus/clock.hpp>
97d800a4eSMatt Spinler #include <sdeventplus/source/event.hpp>
10f869fcf8SMatt Spinler #include <sdeventplus/utility/timer.hpp>
11f60ac27eSMatt Spinler 
12f60ac27eSMatt Spinler namespace openpower::pels
13f60ac27eSMatt Spinler {
14f60ac27eSMatt Spinler 
15f60ac27eSMatt Spinler /**
16f60ac27eSMatt Spinler  * @class HostNotifier
17f60ac27eSMatt Spinler  *
18f60ac27eSMatt Spinler  * This class handles notifying the host firmware of new PELs.
19f60ac27eSMatt Spinler  */
20f60ac27eSMatt Spinler class HostNotifier
21f60ac27eSMatt Spinler {
22f60ac27eSMatt Spinler   public:
23f60ac27eSMatt Spinler     HostNotifier() = delete;
24f60ac27eSMatt Spinler     HostNotifier(const HostNotifier&) = delete;
25f60ac27eSMatt Spinler     HostNotifier& operator=(const HostNotifier&) = delete;
26f60ac27eSMatt Spinler     HostNotifier(HostNotifier&&) = delete;
27f60ac27eSMatt Spinler     HostNotifier& operator=(HostNotifier&&) = delete;
28f60ac27eSMatt Spinler 
29f60ac27eSMatt Spinler     /**
30f60ac27eSMatt Spinler      * @brief Constructor
31f60ac27eSMatt Spinler      *
32f60ac27eSMatt Spinler      * @param[in] repo - The PEL repository object
33f60ac27eSMatt Spinler      * @param[in] dataIface - The data interface object
34f60ac27eSMatt Spinler      * @param[in] hostIface - The host interface object
35f60ac27eSMatt Spinler      */
36f60ac27eSMatt Spinler     HostNotifier(Repository& repo, DataInterfaceBase& dataIface,
37f60ac27eSMatt Spinler                  std::unique_ptr<HostInterface> hostIface);
38f60ac27eSMatt Spinler 
39f60ac27eSMatt Spinler     /**
40f60ac27eSMatt Spinler      * @brief Destructor
41f60ac27eSMatt Spinler      */
42f60ac27eSMatt Spinler     ~HostNotifier();
43f60ac27eSMatt Spinler 
44f60ac27eSMatt Spinler     /**
45f60ac27eSMatt Spinler      * @brief Returns the PEL queue size.
46f60ac27eSMatt Spinler      *
47f60ac27eSMatt Spinler      * For testing.
48f60ac27eSMatt Spinler      *
49f60ac27eSMatt Spinler      * @return size_t - The queue size
50f60ac27eSMatt Spinler      */
51f60ac27eSMatt Spinler     size_t queueSize() const
52f60ac27eSMatt Spinler     {
53f60ac27eSMatt Spinler         return _pelQueue.size();
54f60ac27eSMatt Spinler     }
55f60ac27eSMatt Spinler 
56f60ac27eSMatt Spinler     /**
57f60ac27eSMatt Spinler      * @brief Specifies if the PEL needs to go onto the queue to be
58f60ac27eSMatt Spinler      *        set to the host.
59f60ac27eSMatt Spinler      *
60a943b15bSMatt Spinler      * Only returns false if:
61a943b15bSMatt Spinler      *  - Already acked by the host (or they didn't like it)
62a943b15bSMatt Spinler      *  - Hidden and the HMC already got it
63a943b15bSMatt Spinler      *  - The 'do not report to host' bit is set
64a943b15bSMatt Spinler      *
65f60ac27eSMatt Spinler      * @param[in] id - The PEL ID
66f60ac27eSMatt Spinler      *
67f60ac27eSMatt Spinler      * @return bool - If enqueue is required
68f60ac27eSMatt Spinler      */
69f60ac27eSMatt Spinler     bool enqueueRequired(uint32_t id) const;
70f60ac27eSMatt Spinler 
71f77debb9SMatt Spinler     /**
72f77debb9SMatt Spinler      * @brief If the host still needs to be notified of the PEL
73f77debb9SMatt Spinler      *        at the time of the notification.
74f77debb9SMatt Spinler      *
75f77debb9SMatt Spinler      * Only returns false if:
76f77debb9SMatt Spinler      *  - Already acked by the host
77f77debb9SMatt Spinler      *  - It's hidden, and the HMC already got or will get it.
78f77debb9SMatt Spinler      *
79f77debb9SMatt Spinler      * @param[in] id - The PEL ID
80f77debb9SMatt Spinler      *
81f77debb9SMatt Spinler      * @return bool - If the notify is required
82f77debb9SMatt Spinler      */
83f77debb9SMatt Spinler     bool notifyRequired(uint32_t id) const;
84f77debb9SMatt Spinler 
85cc3b64aeSMatt Spinler     /**
86cc3b64aeSMatt Spinler      * @brief Called when the host sends the 'ack' PLDM command.
87cc3b64aeSMatt Spinler      *
88cc3b64aeSMatt Spinler      * This means the PEL never needs to be sent up again.
89cc3b64aeSMatt Spinler      *
90*41293cb8SMatt Spinler      * If the host was previously full, it is also an indication
91*41293cb8SMatt Spinler      * it no longer is.
92*41293cb8SMatt Spinler      *
93cc3b64aeSMatt Spinler      * @param[in] id - The PEL ID
94cc3b64aeSMatt Spinler      */
95cc3b64aeSMatt Spinler     void ackPEL(uint32_t id);
96cc3b64aeSMatt Spinler 
97*41293cb8SMatt Spinler     /**
98*41293cb8SMatt Spinler      * @brief Called when the host does not have room for more
99*41293cb8SMatt Spinler      *        PELs at this time.
100*41293cb8SMatt Spinler      *
101*41293cb8SMatt Spinler      * This can happen when an OS isn't running yet, and the
102*41293cb8SMatt Spinler      * staging area to hold the PELs before sending them up
103*41293cb8SMatt Spinler      * to the OS is full.  This will stop future PEls from being
104*41293cb8SMatt Spinler      * sent up, as explained below.
105*41293cb8SMatt Spinler      *
106*41293cb8SMatt Spinler      * The PEL with this ID will need to be sent again, so its
107*41293cb8SMatt Spinler      * state is set back to 'new', and it is removed from the list
108*41293cb8SMatt Spinler      * of already sent PELs.
109*41293cb8SMatt Spinler      *
110*41293cb8SMatt Spinler      * A timer will be started, if it isn't already running, to
111*41293cb8SMatt Spinler      * issue another send in the hopes that space has been freed
112*41293cb8SMatt Spinler      * up by then (Receiving an ackPEL response is also an
113*41293cb8SMatt Spinler      * indication of this if there happened to have been other
114*41293cb8SMatt Spinler      * PELs in flight).
115*41293cb8SMatt Spinler      *
116*41293cb8SMatt Spinler      * @param[in] id - The PEL ID
117*41293cb8SMatt Spinler      */
118*41293cb8SMatt Spinler     void setHostFull(uint32_t id);
119*41293cb8SMatt Spinler 
120f60ac27eSMatt Spinler   private:
121f60ac27eSMatt Spinler     /**
122f60ac27eSMatt Spinler      * @brief This function gets called by the Repository class
123f60ac27eSMatt Spinler      *        when a new PEL is added to it.
124f60ac27eSMatt Spinler      *
1257d800a4eSMatt Spinler      * This function puts the PEL on the queue to be sent up if it
1267d800a4eSMatt Spinler      * needs it, and possibly dispatch the send if the conditions call
1277d800a4eSMatt Spinler      * for it.
1287d800a4eSMatt Spinler      *
129f60ac27eSMatt Spinler      * @param[in] pel - The new PEL
130f60ac27eSMatt Spinler      */
131f60ac27eSMatt Spinler     void newLogCallback(const PEL& pel);
132f60ac27eSMatt Spinler 
133f60ac27eSMatt Spinler     /**
134f60ac27eSMatt Spinler      * @brief This function runs on every existing PEL at startup
135f60ac27eSMatt Spinler      *        and puts the PEL on the queue to send if necessary.
136f60ac27eSMatt Spinler      *
137f60ac27eSMatt Spinler      * @param[in] pel - The PEL
138f60ac27eSMatt Spinler      *
139f60ac27eSMatt Spinler      * @return bool - This is an indicator to the Repository::for_each
140f60ac27eSMatt Spinler      *                function to traverse every PEL.  Always false.
141f60ac27eSMatt Spinler      */
142f60ac27eSMatt Spinler     bool addPELToQueue(const PEL& pel);
143f60ac27eSMatt Spinler 
144f60ac27eSMatt Spinler     /**
145f77debb9SMatt Spinler      * @brief Takes the first PEL from the queue that needs to be
146f77debb9SMatt Spinler      *        sent, and issues the send if conditions are right.
147f60ac27eSMatt Spinler      */
148f60ac27eSMatt Spinler     void doNewLogNotify();
149f60ac27eSMatt Spinler 
150f60ac27eSMatt Spinler     /**
1517d800a4eSMatt Spinler      * @brief Creates the event object to handle sending the PLDM
1527d800a4eSMatt Spinler      *        command from the event loop.
1537d800a4eSMatt Spinler      */
1547d800a4eSMatt Spinler     void scheduleDispatch();
1557d800a4eSMatt Spinler 
1567d800a4eSMatt Spinler     /**
1577d800a4eSMatt Spinler      * @brief Kicks off the PLDM send, but called from the event
1587d800a4eSMatt Spinler      *        loop.
1597d800a4eSMatt Spinler      *
1607d800a4eSMatt Spinler      * @param[in] source - The event source object
1617d800a4eSMatt Spinler      */
1627d800a4eSMatt Spinler     void dispatch(sdeventplus::source::EventBase& source);
1637d800a4eSMatt Spinler 
1647d800a4eSMatt Spinler     /**
165f60ac27eSMatt Spinler      * @brief Called when the host changes state.
166f60ac27eSMatt Spinler      *
1673019c6fbSMatt Spinler      * If the new state is host up and there are PELs to send, it
1683019c6fbSMatt Spinler      * will trigger the first command.  If the new state is off, then
1693019c6fbSMatt Spinler      * it will transfer any PELs that were sent but not acked yet back
1703019c6fbSMatt Spinler      * to the queue to be sent again.
1713019c6fbSMatt Spinler      *
172f60ac27eSMatt Spinler      * @param[in] hostUp - The new host state
173f60ac27eSMatt Spinler      */
174f60ac27eSMatt Spinler     void hostStateChange(bool hostUp);
175f60ac27eSMatt Spinler 
176f60ac27eSMatt Spinler     /**
177f60ac27eSMatt Spinler      * @brief The callback function invoked after the asynchronous
178f60ac27eSMatt Spinler      *        PLDM receive function is complete.
179f60ac27eSMatt Spinler      *
180f869fcf8SMatt Spinler      * If the command was successful, the state of that PEL will
181f869fcf8SMatt Spinler      * be set to 'sent', and the next send will be triggered.
182f869fcf8SMatt Spinler      *
183f869fcf8SMatt Spinler      * If the command failed, a retry timer will be started so it
184f869fcf8SMatt Spinler      * can be sent again.
185f869fcf8SMatt Spinler      *
186f60ac27eSMatt Spinler      * @param[in] status - The response status
187f60ac27eSMatt Spinler      */
188f60ac27eSMatt Spinler     void commandResponse(ResponseStatus status);
189f60ac27eSMatt Spinler 
190f60ac27eSMatt Spinler     /**
191f869fcf8SMatt Spinler      * @brief The function called when the command failure retry
192f869fcf8SMatt Spinler      *        time is up.
193f869fcf8SMatt Spinler      *
194f869fcf8SMatt Spinler      * It will issue a send of the previous PEL and increment the
195f869fcf8SMatt Spinler      * retry count.
196f869fcf8SMatt Spinler      */
197f869fcf8SMatt Spinler     void retryTimerExpired();
198f869fcf8SMatt Spinler 
199f869fcf8SMatt Spinler     /**
200*41293cb8SMatt Spinler      * @brief The function called when the 'host full' retry timer
201*41293cb8SMatt Spinler      *        expires.
202*41293cb8SMatt Spinler      *
203*41293cb8SMatt Spinler      * This will re-issue a command to try again with the PEL at
204*41293cb8SMatt Spinler      * the front of the queue.
205*41293cb8SMatt Spinler      */
206*41293cb8SMatt Spinler     void hostFullTimerExpired();
207*41293cb8SMatt Spinler 
208*41293cb8SMatt Spinler     /**
2093019c6fbSMatt Spinler      * @brief Stops an in progress command
2103019c6fbSMatt Spinler      *
2113019c6fbSMatt Spinler      * In progress meaning after the send but before the response.
2123019c6fbSMatt Spinler      */
2133019c6fbSMatt Spinler     void stopCommand();
2143019c6fbSMatt Spinler 
2153019c6fbSMatt Spinler     /**
216f60ac27eSMatt Spinler      * @brief The PEL repository object
217f60ac27eSMatt Spinler      */
218f60ac27eSMatt Spinler     Repository& _repo;
219f60ac27eSMatt Spinler 
220f60ac27eSMatt Spinler     /**
221f60ac27eSMatt Spinler      * @brief The data interface object
222f60ac27eSMatt Spinler      */
223f60ac27eSMatt Spinler     DataInterfaceBase& _dataIface;
224f60ac27eSMatt Spinler 
225f60ac27eSMatt Spinler     /**
226f60ac27eSMatt Spinler      * @brief Base class pointer for the host command interface
227f60ac27eSMatt Spinler      */
228f60ac27eSMatt Spinler     std::unique_ptr<HostInterface> _hostIface;
229f60ac27eSMatt Spinler 
230f60ac27eSMatt Spinler     /**
231f60ac27eSMatt Spinler      * @brief The list of PEL IDs that need to be sent.
232f60ac27eSMatt Spinler      */
233f60ac27eSMatt Spinler     std::deque<uint32_t> _pelQueue;
234f869fcf8SMatt Spinler 
235f869fcf8SMatt Spinler     /**
236f869fcf8SMatt Spinler      * @brief The list of IDs that were sent, but not acked yet.
237f869fcf8SMatt Spinler      *
238f869fcf8SMatt Spinler      * These move back to _pelQueue on a power off.
239f869fcf8SMatt Spinler      */
240f869fcf8SMatt Spinler     std::vector<uint32_t> _sentPELs;
241f869fcf8SMatt Spinler 
242f869fcf8SMatt Spinler     /**
243f869fcf8SMatt Spinler      * @brief The ID the PEL where the notification has
244f869fcf8SMatt Spinler      *        been kicked off but the asynchronous response
245f869fcf8SMatt Spinler      *        hasn't been received yet.
246f869fcf8SMatt Spinler      */
247f869fcf8SMatt Spinler     uint32_t _inProgressPEL = 0;
248f869fcf8SMatt Spinler 
249f869fcf8SMatt Spinler     /**
250f869fcf8SMatt Spinler      * @brief The command retry count
251f869fcf8SMatt Spinler      */
252f869fcf8SMatt Spinler     size_t _retryCount = 0;
253f869fcf8SMatt Spinler 
254f869fcf8SMatt Spinler     /**
255*41293cb8SMatt Spinler      * @brief Indicates if the host has said it is full and does not
256*41293cb8SMatt Spinler      *        currently have the space for more PELs.
257*41293cb8SMatt Spinler      */
258*41293cb8SMatt Spinler     bool _hostFull = false;
259*41293cb8SMatt Spinler 
260*41293cb8SMatt Spinler     /**
261f869fcf8SMatt Spinler      * @brief The command retry timer.
262f869fcf8SMatt Spinler      */
263f869fcf8SMatt Spinler     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _retryTimer;
2647d800a4eSMatt Spinler 
2657d800a4eSMatt Spinler     /**
266*41293cb8SMatt Spinler      * @brief The host full timer, used to retry sending a PEL if the host
267*41293cb8SMatt Spinler      *        said it is full.
268*41293cb8SMatt Spinler      */
269*41293cb8SMatt Spinler     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _hostFullTimer;
270*41293cb8SMatt Spinler 
271*41293cb8SMatt Spinler     /**
2727d800a4eSMatt Spinler      * @brief The object used to dispatch a new PEL send from the
2737d800a4eSMatt Spinler      *        event loop, so the calling function can be returned from
2747d800a4eSMatt Spinler      *        first.
2757d800a4eSMatt Spinler      */
2767d800a4eSMatt Spinler     std::unique_ptr<sdeventplus::source::Defer> _dispatcher;
277f60ac27eSMatt Spinler };
278f60ac27eSMatt Spinler 
279f60ac27eSMatt Spinler } // namespace openpower::pels
280