summaryrefslogtreecommitdiffstats
path: root/src/usr/mbox/mailboxsp.H
blob: d1db0a88a27f1e404f98f52516bde27783b8ad54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/mbox/mailboxsp.H $                                    */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2012,2018                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#if !defined(__MBOXSP__)
#define __MBOXSP__
/**
 * @file mailboxrp.C
 * @brief Mailbox service provider declariation
 */
#include "mbox_dma_buffer.H"
#include <stdint.h>
#include <builtins.h>
#include <mbox/mboxif.H>
#include <errl/errlentry.H>
#include <sys/msg.h>
#include <sys/sync.h>
#include <list>
#include <map>
#include <util/locked/list.H>
#include <intr/interrupt.H>
#include <limits.h>

extern const char* VFS_ROOT_MSG_MBOX;

namespace TARGETING
{
    class Target;
};

namespace MBOX
{
    /**
     * Messages to the mailbox queue
     */
    enum msgq_msg_t
    {
        MSG_SEND,
        MSG_REGISTER_MSGQ,
        MSG_UNREGISTER_MSGQ,
        MSG_INTR,
        MSG_MBOX_SHUTDOWN,
        MSG_MBOX_SUSPEND,
        MSG_MBOX_RESUME,
        MSG_IPC,
        MSG_LOCAL_IPC,
        MSG_MBOX_ALLOCATE,
        MSG_MBOX_DEALLOCATE,
    };


    class MailboxSp
    {
        public:

            /**
             * Initialize the mailbox service
             * @param[in] i_taskArgs
             */
            static void init(errlHndl_t& o_errl);

            /**
             * Send a message to the mailbox service
             * @param[in] i_q_id, the queue id
             * @param[in/out] io_msg the message to send and if async, the
             * @param[in] i_mbox_msg_type - mailbox message type
             * message returned
             * @return errlHndl_t on error
             */
            static errlHndl_t send(queue_id_t i_q_id,
                                   msg_t * io_msg,
                                   msgq_msg_t i_mbox_msg_type);

            /**
             * Reclaim any DMA buffers owned by the FSP
             * @return errlHndl_t on error
             */
            static errlHndl_t reclaimDmaBfrsFromFsp( void );


        protected:

            /**
             * Default constructor
             */
            MailboxSp();

            /**
             * Destructor
             */
            ~MailboxSp();

            /**
             * Start message handler
             */
            static void* msg_handler(void * unused);

            /**
             * Start poll Task
             */
            // @todo RTC:126643 Remove poller when interrupts avail
            static void* poller(void * unused);

        private:

            /**
             * mailbox message header
             * @note We decided to send this
             */
            struct mbox_msg_t
            {
                /**
                 * @brief Identifier assigned by originating endpoint for correlating
                 * sync message responses.
                 */
                uint32_t msg_id;

                /**
                 * Message queue id.
                 * @see src/include/usr/mbox/mbox_queues.H
                 */
                uint32_t msg_queue_id;

                /**
                 * Message
                 */
                msg_t msg_payload;

                /**
                 * Default constructor
                 */
                mbox_msg_t() : msg_id(0), msg_queue_id(0), msg_payload()
                {
                    memset(&msg_payload,'\0',sizeof(msg_t));
                }
            };

            // Private functions

            /* See init() above */
            errlHndl_t _init();

            /**
             * The mailbox service provider task
             */
            void msgHandler();

            /**
             * The mailbox service poll task
             */
            // @todo RTC:126643 Remove pollTask when interrupts avail
            void pollTask();

            /**
             * Send or queue up the next message to the device driver
             * @param[in] i_mbox_msg, ptr to the mailbox message | NULL
             * @NOTE if the mailbox message ptr is NULL then no new message is
             * added to the queue, but the next message on the queue is sent
             * provided there is one available.
             */
            void send_msg(mbox_msg_t * i_msg = NULL);

            /**
             * Handle a mailbox message received from the device driver
             * @param[in] i_mbox_msg, the mailbox message
             * @return nothing
             * @NOTE will commit an error log on errors.
             */
            void recv_msg(mbox_msg_t & i_mbox_msg);

            /**
             * Register a message queue to receive mailbox messages
             * @param[in] i_queueu_id, The queue identifier
             * @param[in] i_msgQ, The message queue created with msg_q_create()
             *                    @see sys/msg.h
             * @return errlHndl_t on error
             */
            errlHndl_t msgq_register(queue_id_t i_queue_id, msg_q_t i_msgQ);


            /**
             * Un register a message queue from the mailbox service
             * @param[in] i_queue_id, The queue identifier
             * @return The message queue
             */
            msg_q_t msgq_unregister(queue_id_t i_queue_id);

            /**
             * Handle interrupt from Intr presenter
             * @param[in] i_msg, The message
             */
            errlHndl_t handleInterrupt();

            /**
             * Handle potential IPC message
             */
            void handleIPC(queue_id_t i_queue_id, msg_t * i_msg);

            /**
             * Trace the message to the "fast" trace buffer
             * @param[in] i_text, a Description
             * @param[in] i_mbox_msg, the message to trace
             */
            void trace_msg(const char * i_text,
                           const mbox_msg_t & i_mbox_msg) const;

            /**
             * Handle a message to hbmbox from fspmbox
             * @param[in] i_mbox_msg, the mbox message
             */
            void handle_hbmbox_msg(mbox_msg_t & i_mbox_msg);

            /**
             * Handle a response from FSP
             * @param[in] i_mbox_msg, the mbox message
             */
            void handle_hbmbox_resp(mbox_msg_t & i_mbox_msg);

            /**
             * Handle a new message from HB to FSP
             * @param[in] i_msg, The carrier message
             */
            void handleNewMessage(msg_t* i_msg);

            /**
             * Handle any unclaimed messages from FSP to HB
             * @post iv_pending.size() == 0;
             */
            void handleUnclaimed();

            /**
             * Handle shutdown
             * @pre quiesced() == true
             */
            void handleShutdown();

            /**
             * Perform the final actions needed to suspend the mailbox sp
             * @pre quiesced() == true
             */
            void suspend();

            /**
             * Resume the mailbox from the suspend state
             */
            void resume();

            /**
             * Send message to FSP indicating an invalid or undeliverable
             * message queue.
             * @param[in] i_mbox_msg.  The invalid message
             */
            void invalidMsgResponder(mbox_msg_t & i_mbox_msg);

            /**
             * Handle an allocate message
             * @param[in] The allocate message
             */
            void handleAllocate(msg_t* i_msg);

            /**
             * Unallocate storage
             * @param[in] Pointer to allocated storage
             * @note If i_ptr is not owned by mbox then
             * storage is just freed
             */
            void deallocate(void * i_ptr);

            /**
             * Query Quiesced
             * @returns [true|false]
             */
            bool quiesced();

            /**
             * Reclaim any DMA buffers owned by the FSP
             * @return errlHndl_t on error
             */
            errlHndl_t _reclaimDmaBfrsFromFsp( void );

            /**
             * Send a Reclaim DMA Buffers message to FSP
             * @param[in] i_mbox_msg.  Message Buffer to use,
             */
            void sendReclaimDmaBfrsMsg(mbox_msg_t & i_mbox_msg);

            /**
             * Send a Reclaim DMA Buffers message to FSP
              */
            void sendReclaimDmaBfrsMsg( void );

            /**
             * Start the watchdogTimeoutTaskWorker and print
             * out an error if it crashes
             */
            static void * watchdogTimeoutTask(void * i_mailboxSp);

            /**
             * Used to start a timer , if the timer expires then
             * Hostboot will print out a bunch of MBOX and INTR
             * error information to the SLOW buffer. This is used
             * to collect debug information in the case where we
             * are hanging, waiting for a response to an mailbox msg
             */
            static void * watchdogTimeoutTaskWorker(void * i_mailboxSp);

            /**
             * Determine if a Reclaim Bfr message is outstanding
             * @return [true - Msg active | false - no msg active]
             */
            ALWAYS_INLINE
            bool isDmaReqBfrMsgOutstanding( void ) const
            {
                return ( iv_reclaim_sent_cnt > iv_reclaim_rsp_cnt) ;
            }


            enum
            {
                MAX_RETRY_COUNT = 3,
                MAX_ALLOCATION = 64 * KILOBYTE,
            };

            /**
             * Information needed for a resonse to a sync message
             */
            struct msg_respond_t
            {
                public:
                    msg_respond_t *     next;
                    msg_respond_t *     prev;
                    msg_t *             key;    //!< ptr to original msg

                    //These fields are only used in sync message from FSP to HB
                    uint32_t            msg_id;         //!< The message_id
                    uint32_t            msg_queue_id;   //!< The msg queue id

                    /**
                     * Ctor
                     */
                    msg_respond_t(msg_t * i_msg) :
                        next(NULL),
                        prev(NULL),
                        key(i_msg),
                        msg_id(0),
                        msg_queue_id(0) {}
            };

            typedef std::list<mbox_msg_t> send_q_t;
            typedef Util::Locked::List<msg_respond_t, msg_t *> respond_q_t;
            typedef std::map<queue_id_t,msg_q_t> registry_t;
            typedef std::list<msg_t *> msg_list_t;

            typedef std::pair<uint64_t, uint64_t> addr_size_t;
            typedef std::list<addr_size_t> addr_list_t;

            msg_q_t     iv_msgQ;        //!< mailbox message queue
            send_q_t    iv_sendq;       //!< msg to send queue
            mutable mutex_t iv_sendq_mutex; // SMP protection
            mbox_msg_t  iv_msg_to_send; //!< message being sent
            respond_q_t iv_respondq;    //!< msg respond pending list
            registry_t  iv_registry;    //!< Registered queue
            DmaBuffer   iv_dmaBuffer;   //!< DMA buffer manager
            send_q_t    iv_pendingq;    //!< Pending for queue registration
            tid_t       iv_dmaRequestWatchdog; //!< TID of dma buffer request watchdog

            TARGETING::Target * iv_trgt;//!< mailbox device driver target
            msg_t *     iv_shutdown_msg;//!< Message to shutdown mbox
            msg_t *     iv_suspend_msg; //!< Message to suspend mbox
            bool        iv_rts;         //!< ready to send flag
            bool        iv_dma_pend;    //!< Request pending (on Hw)
                                        //!<   for more DMA bufs
            bool        iv_disabled;    //!< Mailboxsp shut off (rejects new)
            bool        iv_suspended;   //!< Mailbox is suspended (queues new)
            bool        iv_suspend_intr;//!< Disable HW interrupts on suspend
            bool        iv_allow_blk_resp;//!< Disable HW interrupts on suspend
            uint64_t    iv_sum_alloc;   //!< Total extra_data storage allocated
            msg_list_t  iv_pend_alloc;  //!< Pending memory allocations
            addr_list_t iv_allocAddrs;  //!< memory addresses allocated by mbox
            int         iv_reclaim_sent_cnt;//!< num of reclaim bfr msgs sent
            int         iv_reclaim_rsp_cnt; //!< num of reclaim responses rcvd

    };
};

#endif
OpenPOWER on IntegriCloud