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.
190e1593ecSMatt Spinler  *
200e1593ecSMatt Spinler  * It uses the Repository class's subscription feature to be
210e1593ecSMatt Spinler  * notified about new PELs.
220e1593ecSMatt Spinler  *
230e1593ecSMatt Spinler  * Some PELs do not need to be sent - see enqueueRequired() and
240e1593ecSMatt Spinler  * notifyRequired().
250e1593ecSMatt Spinler  *
260e1593ecSMatt Spinler  * The high level good path flow for sending a single PEL is:
270e1593ecSMatt Spinler  *
280e1593ecSMatt Spinler  * 1) Send the ID and size of the new PEL to the host.
290e1593ecSMatt Spinler  *   - The command response is asynchronous.
300e1593ecSMatt Spinler  *
310e1593ecSMatt Spinler  * 2) The host reads the raw PEL data (outside of this class).
320e1593ecSMatt Spinler  *
330e1593ecSMatt Spinler  * 3) The host sends the PEL to the OS, and then sends an AckPEL
340e1593ecSMatt Spinler  *    PLDM command to the PLDM daemon, who makes a D-Bus method
350e1593ecSMatt Spinler  *    call to this daemon, which calls HostNotifer::ackPEL().
360e1593ecSMatt Spinler  *
370e1593ecSMatt Spinler  *    After this, a PEL never needs to be sent again, but if the
380e1593ecSMatt Spinler  *    host is rebooted before the ack comes it will.
390e1593ecSMatt Spinler  *
400e1593ecSMatt Spinler  * The host firmware has a finite amount of space to store PELs before
410e1593ecSMatt Spinler  * sending to the OS, and it's possible it will fill up.  In this case,
420e1593ecSMatt Spinler  * the AckPEL command will have a special response that will tell the
430e1593ecSMatt Spinler  * PLDM daemon to call  HostReject D-Bus method on this daemon instead
440e1593ecSMatt Spinler  * which will invoke HostNotifier::setHostFull(). This will stop new
450e1593ecSMatt Spinler  * PELs from being sent, and the first PEL that hits this will have
460e1593ecSMatt Spinler  * a timer set to retry again later.
47f60ac27eSMatt Spinler  */
48f60ac27eSMatt Spinler class HostNotifier
49f60ac27eSMatt Spinler {
50f60ac27eSMatt Spinler   public:
51f60ac27eSMatt Spinler     HostNotifier() = delete;
52f60ac27eSMatt Spinler     HostNotifier(const HostNotifier&) = delete;
53f60ac27eSMatt Spinler     HostNotifier& operator=(const HostNotifier&) = delete;
54f60ac27eSMatt Spinler     HostNotifier(HostNotifier&&) = delete;
55f60ac27eSMatt Spinler     HostNotifier& operator=(HostNotifier&&) = delete;
56f60ac27eSMatt Spinler 
57f60ac27eSMatt Spinler     /**
58f60ac27eSMatt Spinler      * @brief Constructor
59f60ac27eSMatt Spinler      *
60f60ac27eSMatt Spinler      * @param[in] repo - The PEL repository object
61f60ac27eSMatt Spinler      * @param[in] dataIface - The data interface object
62f60ac27eSMatt Spinler      * @param[in] hostIface - The host interface object
63f60ac27eSMatt Spinler      */
64f60ac27eSMatt Spinler     HostNotifier(Repository& repo, DataInterfaceBase& dataIface,
65f60ac27eSMatt Spinler                  std::unique_ptr<HostInterface> hostIface);
66f60ac27eSMatt Spinler 
67f60ac27eSMatt Spinler     /**
68f60ac27eSMatt Spinler      * @brief Destructor
69f60ac27eSMatt Spinler      */
70f60ac27eSMatt Spinler     ~HostNotifier();
71f60ac27eSMatt Spinler 
72f60ac27eSMatt Spinler     /**
73f60ac27eSMatt Spinler      * @brief Returns the PEL queue size.
74f60ac27eSMatt Spinler      *
75f60ac27eSMatt Spinler      * For testing.
76f60ac27eSMatt Spinler      *
77f60ac27eSMatt Spinler      * @return size_t - The queue size
78f60ac27eSMatt Spinler      */
79f60ac27eSMatt Spinler     size_t queueSize() const
80f60ac27eSMatt Spinler     {
81f60ac27eSMatt Spinler         return _pelQueue.size();
82f60ac27eSMatt Spinler     }
83f60ac27eSMatt Spinler 
84f60ac27eSMatt Spinler     /**
85f60ac27eSMatt Spinler      * @brief Specifies if the PEL needs to go onto the queue to be
86f60ac27eSMatt Spinler      *        set to the host.
87f60ac27eSMatt Spinler      *
88a943b15bSMatt Spinler      * Only returns false if:
89a943b15bSMatt Spinler      *  - Already acked by the host (or they didn't like it)
90a943b15bSMatt Spinler      *  - Hidden and the HMC already got it
91a943b15bSMatt Spinler      *  - The 'do not report to host' bit is set
92a943b15bSMatt Spinler      *
93f60ac27eSMatt Spinler      * @param[in] id - The PEL ID
94f60ac27eSMatt Spinler      *
95f60ac27eSMatt Spinler      * @return bool - If enqueue is required
96f60ac27eSMatt Spinler      */
97f60ac27eSMatt Spinler     bool enqueueRequired(uint32_t id) const;
98f60ac27eSMatt Spinler 
99f77debb9SMatt Spinler     /**
100f77debb9SMatt Spinler      * @brief If the host still needs to be notified of the PEL
101f77debb9SMatt Spinler      *        at the time of the notification.
102f77debb9SMatt Spinler      *
103f77debb9SMatt Spinler      * Only returns false if:
104f77debb9SMatt Spinler      *  - Already acked by the host
105f77debb9SMatt Spinler      *  - It's hidden, and the HMC already got or will get it.
106f77debb9SMatt Spinler      *
107f77debb9SMatt Spinler      * @param[in] id - The PEL ID
108f77debb9SMatt Spinler      *
109f77debb9SMatt Spinler      * @return bool - If the notify is required
110f77debb9SMatt Spinler      */
111f77debb9SMatt Spinler     bool notifyRequired(uint32_t id) const;
112f77debb9SMatt Spinler 
113cc3b64aeSMatt Spinler     /**
114cc3b64aeSMatt Spinler      * @brief Called when the host sends the 'ack' PLDM command.
115cc3b64aeSMatt Spinler      *
116cc3b64aeSMatt Spinler      * This means the PEL never needs to be sent up again.
117cc3b64aeSMatt Spinler      *
11841293cb8SMatt Spinler      * If the host was previously full, it is also an indication
11941293cb8SMatt Spinler      * it no longer is.
12041293cb8SMatt Spinler      *
121cc3b64aeSMatt Spinler      * @param[in] id - The PEL ID
122cc3b64aeSMatt Spinler      */
123cc3b64aeSMatt Spinler     void ackPEL(uint32_t id);
124cc3b64aeSMatt Spinler 
12541293cb8SMatt Spinler     /**
12641293cb8SMatt Spinler      * @brief Called when the host does not have room for more
12741293cb8SMatt Spinler      *        PELs at this time.
12841293cb8SMatt Spinler      *
12941293cb8SMatt Spinler      * This can happen when an OS isn't running yet, and the
13041293cb8SMatt Spinler      * staging area to hold the PELs before sending them up
13141293cb8SMatt Spinler      * to the OS is full.  This will stop future PEls from being
13241293cb8SMatt Spinler      * sent up, as explained below.
13341293cb8SMatt Spinler      *
13441293cb8SMatt Spinler      * The PEL with this ID will need to be sent again, so its
13541293cb8SMatt Spinler      * state is set back to 'new', and it is removed from the list
13641293cb8SMatt Spinler      * of already sent PELs.
13741293cb8SMatt Spinler      *
13841293cb8SMatt Spinler      * A timer will be started, if it isn't already running, to
13941293cb8SMatt Spinler      * issue another send in the hopes that space has been freed
14041293cb8SMatt Spinler      * up by then (Receiving an ackPEL response is also an
14141293cb8SMatt Spinler      * indication of this if there happened to have been other
14241293cb8SMatt Spinler      * PELs in flight).
14341293cb8SMatt Spinler      *
14441293cb8SMatt Spinler      * @param[in] id - The PEL ID
14541293cb8SMatt Spinler      */
14641293cb8SMatt Spinler     void setHostFull(uint32_t id);
14741293cb8SMatt Spinler 
148a19b6234SMatt Spinler     /**
149a19b6234SMatt Spinler      * @brief Called when the host receives a malformed PEL.
150a19b6234SMatt Spinler      *
151a19b6234SMatt Spinler      * Ideally this will never happen, as the Repository
152a19b6234SMatt Spinler      * class already purges malformed PELs.
153a19b6234SMatt Spinler      *
154a19b6234SMatt Spinler      * The PEL should never be sent up again.
155a19b6234SMatt Spinler      *
156a19b6234SMatt Spinler      * @param[in] id - The PEL ID
157a19b6234SMatt Spinler      */
158a19b6234SMatt Spinler     void setBadPEL(uint32_t id);
159a19b6234SMatt Spinler 
160f60ac27eSMatt Spinler   private:
161f60ac27eSMatt Spinler     /**
162f60ac27eSMatt Spinler      * @brief This function gets called by the Repository class
163f60ac27eSMatt Spinler      *        when a new PEL is added to it.
164f60ac27eSMatt Spinler      *
1657d800a4eSMatt Spinler      * This function puts the PEL on the queue to be sent up if it
1667d800a4eSMatt Spinler      * needs it, and possibly dispatch the send if the conditions call
1677d800a4eSMatt Spinler      * for it.
1687d800a4eSMatt Spinler      *
169f60ac27eSMatt Spinler      * @param[in] pel - The new PEL
170f60ac27eSMatt Spinler      */
171f60ac27eSMatt Spinler     void newLogCallback(const PEL& pel);
172f60ac27eSMatt Spinler 
173f60ac27eSMatt Spinler     /**
174*7cb985ffSMatt Spinler      * @brief This function gets called by the Repository class
175*7cb985ffSMatt Spinler      *        when a PEL is deleted.
176*7cb985ffSMatt Spinler      *
177*7cb985ffSMatt Spinler      * The deleted ID will be removed from the PEL queue and the
178*7cb985ffSMatt Spinler      * sent list.
179*7cb985ffSMatt Spinler      *
180*7cb985ffSMatt Spinler      * @param[in] id - The deleted PEL ID
181*7cb985ffSMatt Spinler      */
182*7cb985ffSMatt Spinler     void deleteLogCallback(uint32_t id);
183*7cb985ffSMatt Spinler 
184*7cb985ffSMatt Spinler     /**
185f60ac27eSMatt Spinler      * @brief This function runs on every existing PEL at startup
186f60ac27eSMatt Spinler      *        and puts the PEL on the queue to send if necessary.
187f60ac27eSMatt Spinler      *
188f60ac27eSMatt Spinler      * @param[in] pel - The PEL
189f60ac27eSMatt Spinler      *
190f60ac27eSMatt Spinler      * @return bool - This is an indicator to the Repository::for_each
191f60ac27eSMatt Spinler      *                function to traverse every PEL.  Always false.
192f60ac27eSMatt Spinler      */
193f60ac27eSMatt Spinler     bool addPELToQueue(const PEL& pel);
194f60ac27eSMatt Spinler 
195f60ac27eSMatt Spinler     /**
196f77debb9SMatt Spinler      * @brief Takes the first PEL from the queue that needs to be
197f77debb9SMatt Spinler      *        sent, and issues the send if conditions are right.
198f60ac27eSMatt Spinler      */
199f60ac27eSMatt Spinler     void doNewLogNotify();
200f60ac27eSMatt Spinler 
201f60ac27eSMatt Spinler     /**
2027d800a4eSMatt Spinler      * @brief Creates the event object to handle sending the PLDM
2037d800a4eSMatt Spinler      *        command from the event loop.
2047d800a4eSMatt Spinler      */
2057d800a4eSMatt Spinler     void scheduleDispatch();
2067d800a4eSMatt Spinler 
2077d800a4eSMatt Spinler     /**
2087d800a4eSMatt Spinler      * @brief Kicks off the PLDM send, but called from the event
2097d800a4eSMatt Spinler      *        loop.
2107d800a4eSMatt Spinler      *
2117d800a4eSMatt Spinler      * @param[in] source - The event source object
2127d800a4eSMatt Spinler      */
2137d800a4eSMatt Spinler     void dispatch(sdeventplus::source::EventBase& source);
2147d800a4eSMatt Spinler 
2157d800a4eSMatt Spinler     /**
216f60ac27eSMatt Spinler      * @brief Called when the host changes state.
217f60ac27eSMatt Spinler      *
2183019c6fbSMatt Spinler      * If the new state is host up and there are PELs to send, it
2193019c6fbSMatt Spinler      * will trigger the first command.  If the new state is off, then
2203019c6fbSMatt Spinler      * it will transfer any PELs that were sent but not acked yet back
2213019c6fbSMatt Spinler      * to the queue to be sent again.
2223019c6fbSMatt Spinler      *
223f60ac27eSMatt Spinler      * @param[in] hostUp - The new host state
224f60ac27eSMatt Spinler      */
225f60ac27eSMatt Spinler     void hostStateChange(bool hostUp);
226f60ac27eSMatt Spinler 
227f60ac27eSMatt Spinler     /**
228f60ac27eSMatt Spinler      * @brief The callback function invoked after the asynchronous
229f60ac27eSMatt Spinler      *        PLDM receive function is complete.
230f60ac27eSMatt Spinler      *
231f869fcf8SMatt Spinler      * If the command was successful, the state of that PEL will
232f869fcf8SMatt Spinler      * be set to 'sent', and the next send will be triggered.
233f869fcf8SMatt Spinler      *
234f869fcf8SMatt Spinler      * If the command failed, a retry timer will be started so it
235f869fcf8SMatt Spinler      * can be sent again.
236f869fcf8SMatt Spinler      *
237f60ac27eSMatt Spinler      * @param[in] status - The response status
238f60ac27eSMatt Spinler      */
239f60ac27eSMatt Spinler     void commandResponse(ResponseStatus status);
240f60ac27eSMatt Spinler 
241f60ac27eSMatt Spinler     /**
242f869fcf8SMatt Spinler      * @brief The function called when the command failure retry
243f869fcf8SMatt Spinler      *        time is up.
244f869fcf8SMatt Spinler      *
245f869fcf8SMatt Spinler      * It will issue a send of the previous PEL and increment the
246f869fcf8SMatt Spinler      * retry count.
247f869fcf8SMatt Spinler      */
248f869fcf8SMatt Spinler     void retryTimerExpired();
249f869fcf8SMatt Spinler 
250f869fcf8SMatt Spinler     /**
25141293cb8SMatt Spinler      * @brief The function called when the 'host full' retry timer
25241293cb8SMatt Spinler      *        expires.
25341293cb8SMatt Spinler      *
25441293cb8SMatt Spinler      * This will re-issue a command to try again with the PEL at
25541293cb8SMatt Spinler      * the front of the queue.
25641293cb8SMatt Spinler      */
25741293cb8SMatt Spinler     void hostFullTimerExpired();
25841293cb8SMatt Spinler 
25941293cb8SMatt Spinler     /**
2603019c6fbSMatt Spinler      * @brief Stops an in progress command
2613019c6fbSMatt Spinler      *
2623019c6fbSMatt Spinler      * In progress meaning after the send but before the response.
2633019c6fbSMatt Spinler      */
2643019c6fbSMatt Spinler     void stopCommand();
2653019c6fbSMatt Spinler 
2663019c6fbSMatt Spinler     /**
267f60ac27eSMatt Spinler      * @brief The PEL repository object
268f60ac27eSMatt Spinler      */
269f60ac27eSMatt Spinler     Repository& _repo;
270f60ac27eSMatt Spinler 
271f60ac27eSMatt Spinler     /**
272f60ac27eSMatt Spinler      * @brief The data interface object
273f60ac27eSMatt Spinler      */
274f60ac27eSMatt Spinler     DataInterfaceBase& _dataIface;
275f60ac27eSMatt Spinler 
276f60ac27eSMatt Spinler     /**
277f60ac27eSMatt Spinler      * @brief Base class pointer for the host command interface
278f60ac27eSMatt Spinler      */
279f60ac27eSMatt Spinler     std::unique_ptr<HostInterface> _hostIface;
280f60ac27eSMatt Spinler 
281f60ac27eSMatt Spinler     /**
282f60ac27eSMatt Spinler      * @brief The list of PEL IDs that need to be sent.
283f60ac27eSMatt Spinler      */
284f60ac27eSMatt Spinler     std::deque<uint32_t> _pelQueue;
285f869fcf8SMatt Spinler 
286f869fcf8SMatt Spinler     /**
287f869fcf8SMatt Spinler      * @brief The list of IDs that were sent, but not acked yet.
288f869fcf8SMatt Spinler      *
289f869fcf8SMatt Spinler      * These move back to _pelQueue on a power off.
290f869fcf8SMatt Spinler      */
291f869fcf8SMatt Spinler     std::vector<uint32_t> _sentPELs;
292f869fcf8SMatt Spinler 
293f869fcf8SMatt Spinler     /**
294f869fcf8SMatt Spinler      * @brief The ID the PEL where the notification has
295f869fcf8SMatt Spinler      *        been kicked off but the asynchronous response
296f869fcf8SMatt Spinler      *        hasn't been received yet.
297f869fcf8SMatt Spinler      */
298f869fcf8SMatt Spinler     uint32_t _inProgressPEL = 0;
299f869fcf8SMatt Spinler 
300f869fcf8SMatt Spinler     /**
301f869fcf8SMatt Spinler      * @brief The command retry count
302f869fcf8SMatt Spinler      */
303f869fcf8SMatt Spinler     size_t _retryCount = 0;
304f869fcf8SMatt Spinler 
305f869fcf8SMatt Spinler     /**
30641293cb8SMatt Spinler      * @brief Indicates if the host has said it is full and does not
30741293cb8SMatt Spinler      *        currently have the space for more PELs.
30841293cb8SMatt Spinler      */
30941293cb8SMatt Spinler     bool _hostFull = false;
31041293cb8SMatt Spinler 
31141293cb8SMatt Spinler     /**
312f869fcf8SMatt Spinler      * @brief The command retry timer.
313f869fcf8SMatt Spinler      */
314f869fcf8SMatt Spinler     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _retryTimer;
3157d800a4eSMatt Spinler 
3167d800a4eSMatt Spinler     /**
31741293cb8SMatt Spinler      * @brief The host full timer, used to retry sending a PEL if the host
31841293cb8SMatt Spinler      *        said it is full.
31941293cb8SMatt Spinler      */
32041293cb8SMatt Spinler     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _hostFullTimer;
32141293cb8SMatt Spinler 
32241293cb8SMatt Spinler     /**
3237d800a4eSMatt Spinler      * @brief The object used to dispatch a new PEL send from the
3247d800a4eSMatt Spinler      *        event loop, so the calling function can be returned from
3257d800a4eSMatt Spinler      *        first.
3267d800a4eSMatt Spinler      */
3277d800a4eSMatt Spinler     std::unique_ptr<sdeventplus::source::Defer> _dispatcher;
328f60ac27eSMatt Spinler };
329f60ac27eSMatt Spinler 
330f60ac27eSMatt Spinler } // namespace openpower::pels
331