summaryrefslogtreecommitdiffstats
path: root/src/usr/ipmi/ipmirp.C
blob: 747e552ec7237da16ec3d9b3be11306a7dfde2ba (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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/ipmi/ipmirp.C $                                       */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2012,2014                        */
/* [+] 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                                                     */
/**
 * @file ipmirp.C
 * @brief IPMI resource provider definition
 */

#include "ipmirp.H"
#include "ipmiconfig.H"
#include "ipmidd.H"
#include <ipmi/ipmi_reasoncodes.H>
#include <devicefw/driverif.H>
#include <devicefw/userif.H>

#include <config.h>
#include <sys/task.h>
#include <initservice/taskargs.H>
#include <initservice/initserviceif.H>
#include <sys/vfs.h>

#include <targeting/common/commontargeting.H>
#include <kernel/ipc.H>
#include <arch/ppc.H>
#include <errl/errlmanager.H>
#include <sys/time.h>
#include <sys/misc.h>
#include <errno.h>

// Defined in ipmidd.C
extern trace_desc_t * g_trac_ipmi;
#define IPMI_TRAC(printf_string,args...) \
    TRACFCOMP(g_trac_ipmi,"rp: "printf_string,##args)

/**
 * setup _start and handle barrier
 */
TASK_ENTRY_MACRO( IpmiRP::daemonProcess );

/**
 * @brief  Constructor
 */
IpmiRP::IpmiRP(void):
    iv_msgQ(msg_q_create()),
    iv_sendq(),
    iv_timeoutq(),
    iv_respondq(),
    iv_bmc_timeout(IPMI::g_bmc_timeout),
    iv_outstanding_req(IPMI::g_outstanding_req),
    iv_xmit_buffer_size(IPMI::g_xmit_buffer_size),
    iv_recv_buffer_size(IPMI::g_recv_buffer_size),
    iv_retries(IPMI::g_retries),
    iv_shutdown_msg(NULL),
    iv_shutdown_now(false)
{
    mutex_init(&iv_mutex);
    sync_cond_init(&iv_cv);
}

/**
 * @brief  Destructor
 */
IpmiRP::~IpmiRP(void)
{
    msg_q_destroy(iv_msgQ);
}

void* IpmiRP::start(void* unused)
{
    Singleton<IpmiRP>::instance().execute();
    return NULL;
}

void* IpmiRP::timeout_thread(void* unused)
{
    Singleton<IpmiRP>::instance().timeoutThread();
    return NULL;
}

void* IpmiRP::get_capabilities(void* unused)
{
    Singleton<IpmiRP>::instance().getInterfaceCapabilities();
    return NULL;
}

void IpmiRP::daemonProcess(errlHndl_t& o_errl)
{
    task_create(&IpmiRP::start, NULL);
}

/**
 * @brief Return the transport header size
 */
size_t getXportHeaderSize(void)
{
    // Get the header size from the physical transport.
    IPMI::Message* msg = IPMI::Message::factory();
    size_t xport_size = msg->header_size();
    delete msg;
    return xport_size;
}

/**
 * @brief Return the maximum data size to allocate
 */
inline size_t IpmiRP::maxBuffer(void)
{
    // If the interface isn't up (say we're sending the
    // get-capabilities command, or some such) we use the
    // defaults setup in the ctor.

    static size_t  xport_header_size = getXportHeaderSize();

    mutex_lock(&iv_mutex);

    // Given the max buffer information from the get-capabilities
    // command, subtract off the physical layers header size.

    // iv_xmit_buffer_size can change - it'll be one thing for
    // the default when the RP is created, and possibly another
    // when the get-capabilities command returns.
    // an additional 1 is subtracted based on issues seen with AMI
    size_t mbs = iv_xmit_buffer_size - xport_header_size - 1;

    mutex_unlock(&iv_mutex);

    return mbs;
}

/**
 * @brief Start routine of the time-out handler
 */
void IpmiRP::timeoutThread(void)
{
    // Mark as an independent daemon so if it crashes we terminate.
    task_detach();

    IPMI_TRAC(ENTER_MRK "time out thread");

    // If there's something on the queue we want to grab it's timeout time
    // and wait. Note the response queue is "sorted" as we send messages in
    // order. So, the first message on the queue is the one who's timeout
    // is going to come first.
    while (true)
    {
        mutex_lock(&iv_mutex);
        while ((iv_timeoutq.size() == 0) && !iv_shutdown_now)
        {
            sync_cond_wait(&iv_cv, &iv_mutex);
        }

        // shutting down...
        if (iv_shutdown_now)
        {
            break; // return and terminate thread
        }

        msg_t*& msq_msg = iv_timeoutq.front();
        IPMI::Message* msg = static_cast<IPMI::Message*>(msq_msg->extra_data);

        // The diffence between the timeout of the first message in the
        // queue and the current time is the time we wait for a timeout
        timespec_t tmp_time;
        clock_gettime(CLOCK_MONOTONIC, &tmp_time);

        uint64_t now = (NS_PER_SEC * tmp_time.tv_sec) + tmp_time.tv_nsec;
        uint64_t timeout = (NS_PER_SEC * msg->iv_timeout.tv_sec) +
            msg->iv_timeout.tv_nsec;

        if (now >= timeout)
        {
            IPMI_TRAC("timeout: %x:%x", msg->iv_netfun, msg->iv_cmd);

            // This little bugger timed out. Get him off the timeoutq
            iv_timeoutq.pop_front();

            // Get him off the responseq, and reply back to the waiter that
            // there was a timeout
            response(msg, IPMI::CC_TIMEOUT);

            // Tell the resource provider to check for any pending messages
            msg_t* msg_idleMsg = msg_allocate();
            msg_idleMsg->type = IPMI::MSG_STATE_IDLE;
            msg_send(iv_msgQ, msg_idleMsg);

            mutex_unlock(&iv_mutex);
        }
        else
        {
            mutex_unlock(&iv_mutex);
            nanosleep( 0, timeout - now );
        }
    }
    IPMI_TRAC(EXIT_MRK "time out thread");

    return;
}

/**
 * @brief Get the BMC interface capabilities
 */
void IpmiRP::getInterfaceCapabilities(void)
{
    // Mark as an independent daemon so if it crashes we terminate.
    task_detach();

    // Queue up a get-capabilties message. Anything that queues up behind us
    // (I guess it could queue up in front of us too ...) will use the defaults.

    IPMI::completion_code cc = IPMI::CC_UNKBAD;
    size_t len = 0;
    uint8_t* data = NULL;
    errlHndl_t err = IPMI::sendrecv(IPMI::get_capabilities(), cc, len, data);

    do {
        // If we have a problem, we can't "turn on" the IPMI stack.
        if (err)
        {
            IPMI_TRAC("get_capabilties returned an error, using defaults");
            err->collectTrace(IPMI_COMP_NAME);
            errlCommit(err, IPMI_COMP_ID);
            break;
        }

        // If we get back a funky completion code, we'll use the defaults.
        if (cc != IPMI::CC_OK)
        {
            IPMI_TRAC("get_capabilities not ok %d, using defaults", cc);
            break;
        }

        // If we didn't get back what we expected, use the defaults
        if (len != 5)
        {
            IPMI_TRAC("get_capabilities length %d; using defaults", len);
            break;
        }

        // Protect the members as we're on another thread.
        mutex_lock(&iv_mutex);

        iv_outstanding_req = data[0];
        iv_xmit_buffer_size = data[1];
        iv_recv_buffer_size = data[2];
        iv_bmc_timeout = data[3];
        iv_retries = data[4];

        IPMI_TRAC("get_capabilities: requests %d, in buf %d, "
                  "out buf %d, timeout %d, retries %d",
                  iv_outstanding_req, iv_xmit_buffer_size,
                  iv_recv_buffer_size, iv_bmc_timeout, iv_retries);

        mutex_unlock(&iv_mutex);

    } while(false);

    delete[] data;

    return;
}

/**
 * @brief  Entry point of the resource provider
 */
void IpmiRP::execute(void)
{
    bool l_shutdown_pending = false;

    // Mark as an independent daemon so if it crashes we terminate.
    task_detach();

    // Register shutdown events with init service.
    //      Done at the "end" of shutdown processesing.
    //      This will flush out any IPMI messages which were sent as
    //      part of the shutdown processing. We chose MBOX priority
    //      as we don't want to accidentally get this message after
    //      interrupt processing has stopped in case we need intr to
    //      finish flushing the pipe.
    INITSERVICE::registerShutdownEvent(iv_msgQ, IPMI::MSG_STATE_SHUTDOWN,
                                       INITSERVICE::MBOX_PRIORITY);

    // Start the thread that waits for timeouts
    task_create( &IpmiRP::timeout_thread, NULL );

    // Queue and wait for a message for the interface capabilities
    task_create( &IpmiRP::get_capabilities, NULL);

    while (true)
    {
        msg_t* msg = msg_wait(iv_msgQ);

        const IPMI::msg_type msg_type =
            static_cast<IPMI::msg_type>(msg->type);

        // Invert the "default" by checking here. This allows the compiler
        // to warn us if the enum has an unhandled case but still catch
        // runtime errors where msg_type is set out of bounds.
        assert(msg_type <= IPMI::MSG_LAST_TYPE,
               "msg_type %d not handled", msg_type);

        switch(msg_type)
        {
            // Messages we're told to send.
            // Push the message on the queue, and the idle() at the
            // bottom of this loop will start the transmit process.
            // Be sure to push_back to ensure ordering of transmission.
        case IPMI::MSG_STATE_SEND:
            if (!l_shutdown_pending)
            {
                iv_sendq.push_back(msg);
            }
            // shutting down, do not accept new messages
            else
            {
                IPMI_TRAC(WARN_MRK "IPMI shutdown pending. Message dropped");
                IPMI::Message* ipmi_msg =
                            static_cast<IPMI::Message*>(msg->extra_data);
                response(ipmi_msg, IPMI::CC_BADSTATE);
                msg_free(msg);
            }
            break;

            // State changes from the IPMI hardware. These are async
            // messages so we get rid of them here.
        case IPMI::MSG_STATE_IDLE:
            msg_free(msg);
            // No-op - we do it at the bottom of the loop.
            break;

            // Handle a response (B2H_ATN)
        case IPMI::MSG_STATE_RESP:
            msg_free(msg);
            response();
            break;

            // Handle an event (SMS_ATN)
        case IPMI::MSG_STATE_EVNT:
            IPMI_TRAC(ERR_MRK "msg loop: unexpected ipmi sms");
            msg_free(msg);
            // TODO: RTC 116600 Handle SMS messages
            break;

            // Accept no more messages. Anything in the sendq is sent and
            // we wait for the reply from the BMC.
        case IPMI::MSG_STATE_SHUTDOWN:
            IPMI_TRAC(INFO_MRK "ipmi begin shutdown");
            l_shutdown_pending = true;    // Stop incoming new messages
            iv_shutdown_msg = msg;        // Reply to this message
            break;
        };

        // There's a good chance the interface will be idle right after
        // the operation we just performed. Since we need to poll for the
        // idle state, calling idle after processing a message may decrease
        // the latency of waiting for idle. The worst case is that we find
        // the interface busy and go back to waiting. Note: there's no need
        // to keep calling idle if there are old elements on the sendq;
        // we'll need to wait for the interface to indicate we're idle.
        if ((IPMI::MSG_STATE_SEND != msg_type) || (iv_sendq.size() == 1))
        {
            idle();
        }

        // Once quiesced, shutdown and reply to shutdown msg
        if (l_shutdown_pending && iv_respondq.empty() && iv_sendq.empty())
        {
            shutdownNow();
            break; // exit loop and terminate task
        }
    }

    IPMI_TRAC(EXIT_MRK "message loop");
    return;
}

///
/// @brief  Go in to the idle state
///
void IpmiRP::idle(void)
{
    // Check to see if we have many outstanding requests. If so, don't send
    // any more messages. Note the eagain mechanism still works even though
    // we're not sending messages as eventually we'll get enough responses
    // to shorten the response queue and since the message loop calls us
    // to transmit even for the reception of a message, the driver will
    // eventually reset egagains. If responses timeout, we end up here as
    // the response queue processing sends an idle message when anything is
    // removed.
    if (iv_outstanding_req > iv_respondq.size())
    {
        // If the interface is idle, we can write anything we need to write.
        for (IPMI::send_q_t::iterator i = iv_sendq.begin();
             i != iv_sendq.end();)
        {
            // If we have a problem transmitting a message, then we just stop
            // here and wait for the next time the interface transitions to idle
            // Note that there are two failure cases: the first is that there is
            // a problem transmitting. In this case we told the other end of the
            // message queue, and so the life of this message is over. The other
            // case is that the interface turned out to be busy in which case
            // this message can sit on the queue and it'll be next.

            IPMI::Message* msg = static_cast<IPMI::Message*>((*i)->extra_data);

            // If there was an i/o error, we do nothing - leave this message on
            // the queue. Don't touch msg after xmit returns. If the message was
            // sent, and it was async, msg has been destroyed.
            if (msg->xmit())
            {
                break;
            }
            i  = iv_sendq.erase(i);
        }
    }
    return;
}

///
/// @brief Handle a response to a message we sent
///
void IpmiRP::response(void)
{
    IPMI::Message* rcv_buf = IPMI::Message::factory();

    do
    {
        // Go down to the device and read. Fills in iv_key.
        errlHndl_t err = rcv_buf->recv();

        if (err)
        {
            // Not much we're going to do here, so lets commit the error and
            // the original request will timeout.
            err->collectTrace(IPMI_COMP_NAME);
            errlCommit(err, IPMI_COMP_ID);
            break;
        }

        mutex_lock(&iv_mutex);
        response(rcv_buf);
        mutex_unlock(&iv_mutex);

    } while (false);

    delete rcv_buf;
    return;
}

///
/// @brief Handle a response to a message we want to change
///
void IpmiRP::response(IPMI::Message* i_msg, IPMI::completion_code i_cc)
{
    // The third bit in the netfun indicates this is a reply.
    static const uint8_t reply_bit = 0x04;

    i_msg->iv_cc = i_cc;
    i_msg->iv_netfun |= reply_bit; // update the netfun
    i_msg->iv_len = 0;             // sending no data
    i_msg->iv_data = NULL;
    response(i_msg);
    return;
}

///
/// @brief Handle a response to a message we have
///
void IpmiRP::response(IPMI::Message* i_msg)
{
    do {

        // Look for a message with this seq number waiting for a
        // repsonse. If there isn't a message looking for this response,
        // that's an error. Async messages should also be on this queue,
        // even though the caller has long gone on to other things.
        IPMI::respond_q_t::iterator itr = iv_respondq.find(i_msg->iv_key);
        if (itr == iv_respondq.end())
        {
            IPMI_TRAC(ERR_MRK "message not found on the response queue: "
                      "%d %x:%x", i_msg->iv_key, i_msg->iv_netfun,
                      i_msg->iv_cmd);

            /* @errorlog tag
             * @errortype       ERRL_SEV_UNRECOVERABLE
             * @moduleid        IPMI::MOD_IPMISRV_REPLY
             * @reasoncode      IPMI::RC_WAITER_NOT_FOUND
             * @userdata1       the network function/lun
             * @userdata2       the command which was in error
             * @devdesc         there was no matching message on
             *                  the response queue
             * @custdesc        Unexpected IPMI message from the BMC
             */
            errlHndl_t err = new ERRORLOG::ErrlEntry(
                ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                IPMI::MOD_IPMISRV_REPLY,
                IPMI::RC_WAITER_NOT_FOUND,
                i_msg->iv_netfun, i_msg->iv_cmd, true);

            err->collectTrace(IPMI_COMP_NAME);
            errlCommit(err, IPMI_COMP_ID);

            delete[] i_msg->iv_data;
            break;
        }

        msg_t* original_msg = itr->second;

        // Get us off the response queue, and the timeout queue.
        iv_respondq.erase(itr);
        iv_timeoutq.remove(original_msg);

        // Hand the allocated buffer over to the original message's
        // ipmi_msg_t It will be responsible for de-allocating it
        // when it's dtor is called.
        IPMI::Message* ipmi_msg =
            static_cast<IPMI::Message*>(original_msg->extra_data);

        // Hand ownership of the data to the original requestor
        ipmi_msg->iv_data   = i_msg->iv_data;
        ipmi_msg->iv_len    = i_msg->iv_len;
        ipmi_msg->iv_cc     = i_msg->iv_cc;
        ipmi_msg->iv_netfun = i_msg->iv_netfun;

        // The subclasses know how to handle the response from here.
        // For example, sync messages will respond and async will delete
        ipmi_msg->response(iv_msgQ);

    } while(false);

    return;
}

///
/// @brief Queue a message on to the response queue
///
void IpmiRP::queueForResponse(IPMI::Message& i_msg)
{
    // Figure out when this fellow's timeout should occur. If we
    // have a problem from clock_gettime we have a bug, not an error.
    clock_gettime(CLOCK_MONOTONIC, &(i_msg.iv_timeout));

    // Lock before accessing the timeout (and the queues, etc.)
    mutex_lock(&iv_mutex);

    // BMC request-to-response times are always seconds, 1 - 30.
    // And I don't think we care about roll over here.
    i_msg.iv_timeout.tv_sec += iv_bmc_timeout;

    // Put this message on the response queue so we can find it later
    // for a response and on the timeout queue so if it times out
    // we can find it there. Note all message will all have the same
    // timeout - mostly. Every message sent before the BMC tells us
    // the timeout (at least one message) will have the shortest possible
    // timeout. The BMC might lengthen the timeout, but can not shorten
    // it. All messages after that will have the same timeout. So the
    // timeout queue is "sorted."

    iv_respondq[i_msg.iv_seq] = i_msg.iv_msg;
    iv_timeoutq.push_back(i_msg.iv_msg);

    // If we put a message in an empty timeout queue (we know this as
    // there's only one message in the queue now) signal the timeout thread
    if (iv_timeoutq.size() == 1)
    {
        sync_cond_signal(&iv_cv);
    }

    mutex_unlock(&iv_mutex);
    return;
}

///
/// @brief handle shutdown.
/// Queued messages to send have been sent and all responses complete.
/// Now that we are quiesced, deallocate resources and respond to the
/// shutdown message
///
void IpmiRP::shutdownNow(void)
{
    IPMI_TRAC(INFO_MRK "ipmi shut down now");

    mutex_lock(&iv_mutex);
    iv_shutdown_now = true; // Shutdown underway

    // Wake up Time out thread to terminate.
    sync_cond_signal(&iv_cv);
    mutex_unlock(&iv_mutex);

    // TODO: RTC 116600 unRegisterMsgQ for interrupts

    // Shut down device driver
    Singleton<IpmiDD>::instance().handleShutdown();

    // reply back to shutdown requester that we are shutdown
    msg_respond(iv_msgQ, iv_shutdown_msg);
}


namespace IPMI
{
    ///
    /// @brief  Synchronus message send
    ///
    errlHndl_t sendrecv(const IPMI::command_t& i_cmd,
                        IPMI::completion_code& o_completion_code,
                        size_t& io_len, uint8_t*& io_data)
    {
        errlHndl_t err;
        static msg_q_t mq = Singleton<IpmiRP>::instance().msgQueue();

        IPMI::Message* ipmi_msg = IPMI::Message::factory(i_cmd, io_len, io_data,
                                                         IPMI::TYPE_SYNC);

        // I think if the buffer is too large this is a programming error.
        assert(io_len <= max_buffer());

        IPMI_TRAC("queuing sync %x:%x", ipmi_msg->iv_netfun, ipmi_msg->iv_cmd);
        int rc = msg_sendrecv(mq, ipmi_msg->iv_msg);

        // If the kernel didn't give a hassle about he message, check to see if
        // there was an error reported back from the other end of the queue. If
        // this message made it to the other end of the queue, then our memory
        // (io_data) is in the proper state.
        if (rc == 0) {
            err = ipmi_msg->iv_errl;
        }

        // Otherwise, lets make an errl out of our return code
        else
        {
            /* @errorlog tag
             * @errortype       ERRL_SEV_UNRECOVERABLE
             * @moduleid        IPMI::MOD_IPMISRV_SEND
             * @reasoncode      IPMI::RC_INVALID_SENDRECV
             * @userdata1       rc from msq_sendrecv()
             * @devdesc         msg_sendrecv() failed
             * @custdesc        Firmware error during system boot
             */
            err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                          IPMI::MOD_IPMISRV_SEND,
                                          IPMI::RC_INVALID_SENDRECV,
                                          rc,
                                          0,
                                          true);
            err->collectTrace(IPMI_COMP_NAME);

            // ... and clean up the memory for the caller
            delete[] io_data;
        }

        // The length and the data are the response, if there was one. All of
        // there are pretty much invalid if there was an error.
        io_len = ipmi_msg->iv_len;
        io_data = ipmi_msg->iv_data;
        o_completion_code = static_cast<IPMI::completion_code>(ipmi_msg->iv_cc);
        delete ipmi_msg;

        return err;
    }

    ///
    /// @brief  Asynchronus message send
    ///
    errlHndl_t send(const IPMI::command_t& i_cmd,
                    const size_t i_len, uint8_t* i_data)
    {
        static msg_q_t mq = Singleton<IpmiRP>::instance().msgQueue();
        errlHndl_t err = NULL;

        // We don't delete this message, the message will self destruct
        // after it's been transmitted. Note it could be placed on the send
        // queue and we are none the wiser - so we can't delete it.
        IPMI::Message* ipmi_msg = IPMI::Message::factory(i_cmd, i_len, i_data,
                                                         IPMI::TYPE_ASYNC);

        // I think if the buffer is too large this is a programming error.
        assert(i_len <= max_buffer());

        IPMI_TRAC("queuing async %x:%x", ipmi_msg->iv_netfun, ipmi_msg->iv_cmd);
        int rc = msg_send(mq, ipmi_msg->iv_msg);

        if (rc)
        {
            /* @errorlog tag
             * @errortype       ERRL_SEV_UNRECOVERABLE
             * @moduleid        IPMI::MOD_IPMISRV_SEND
             * @reasoncode      IPMI::RC_INVALID_SEND
             * @userdata1       rc from msq_send()
             * @devdesc         msg_send() failed
             * @custdesc        Firmware error during system boot
             */
            err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                          IPMI::MOD_IPMISRV_SEND,
                                          IPMI::RC_INVALID_SEND,
                                          rc,
                                          0,
                                          true);
            err->collectTrace(IPMI_COMP_NAME);

            // ... and clean up the memory for the caller
            delete[] i_data;
        }

        return err;
    }

    ///
    /// @brief  Maximum buffer for data (max xport - header)
    ///
    size_t max_buffer(void)
    {
        return Singleton<IpmiRP>::instance().maxBuffer();
    }

    ///
    /// @brief Synchronously send an event
    ///
    errlHndl_t send_event(const uint8_t i_sensor_type,
                          const uint8_t i_sensor_number,
                          const bool i_assertion,
                          const uint8_t i_type,
                          completion_code& o_completion_code,
                          const size_t i_len,
                          uint8_t* i_data)
    {
        static const size_t event_header = 5;

        // Sanity check
        assert((i_len > 0) && (i_len < 4),
               "event request i_len incorrect %d", i_len);
        assert(i_type < 0x80, "event request i_type out of range %x", i_type);

        size_t len = event_header + i_len;
        uint8_t* data = new uint8_t[len];
        IPMI::completion_code cc = IPMI::CC_OK;

        data[0] = 0x01;     // More or less fixed, see table 5.4
        data[1] = 0x04;     // Fixed in the IPMI spec table 29.5
        data[2] = i_sensor_type;
        data[3] = i_sensor_number;
        data[4] = (i_assertion ? 0x80 : 0x00) + i_type;
        for (size_t i = 0; i < i_len; i++)
        {
            data[event_header + i] = i_data[i];
        }

        // We're done with i_data, but the caller deletes it. Note there's
        // no response data to an event - so there's nothing to copy over,
        // no reference to i_data, nothing.

        errlHndl_t err = sendrecv(IPMI::platform_event(), cc, len, data);

        o_completion_code = cc;
        delete[] data;
        return err;
    }

};
OpenPOWER on IntegriCloud