summaryrefslogtreecommitdiffstats
path: root/src/usr/ipmi/ipmisel.C
blob: de7feec184679c7725f98f4dd988a919920c0b0d (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/ipmi/ipmisel.C $                                      */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2017                        */
/* [+] Google Inc.                                                        */
/* [+] 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 ipmisel.C
 * @brief IPMI system error log transport definition
 */

#include <algorithm>
#include <sys/time.h>
#include <ipmi/ipmisel.H>
#include "ipmiconfig.H"
#include <ipmi/ipmi_reasoncodes.H>
#include <ipmi/ipmisensor.H>
#include <ipmi/ipmiif.H>

#include <sys/task.h>
#include <initservice/taskargs.H>
#include <initservice/initserviceif.H>
#include <targeting/common/commontargeting.H>
#include <targeting/common/util.H>
#include <targeting/common/utilFilter.H>

#include <errl/errlmanager.H>
using   namespace   TARGETING;

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

// local functions
/*
 * @brief Store either Record/Reserve ID from given data
 */
void storeReserveRecord(uint8_t* o_RData, uint8_t* i_data)
{
    o_RData[0] = i_data[0];
    o_RData[1] = i_data[1];
    return;
}

/*
 * @brief Create Partial Add eSEL Header from inputs
 */
void createPartialAddHeader(uint8_t* i_reserveID, uint8_t* i_recordID,
                               uint16_t i_offset, uint8_t i_isLastEntry,
                               uint8_t* o_header)
{
    o_header[0] = i_reserveID[0];
    o_header[1] = i_reserveID[1];
    o_header[2] = i_recordID[0];
    o_header[3] = i_recordID[1];
    o_header[4] = (uint8_t)(i_offset & 0x00FF);
    o_header[5] = (uint8_t)((i_offset & 0xFF00) >> 8);
    o_header[6] = i_isLastEntry;
    return;
}

enum esel_retry
{
    MAX_SEND_COUNT = 4,
    SLEEP_BASE = 2 * NS_PER_MSEC,
};


namespace IPMISEL
{
void sendESEL(uint8_t* i_eselData, uint32_t i_dataSize,
              uint32_t i_eid, std::vector<sel_info_t*>&i_selEventList)
{
    IPMI_TRAC(ENTER_MRK "sendESEL() %d",i_selEventList.size());

#ifdef __HOSTBOOT_RUNTIME
    // HBRT doesn't send a msg, but use the msg structure to pass the data
    msg_t l_msg;
    msg_t *msg = &l_msg;
    memset(msg, 0, sizeof(msg_t));
#else
    msg_t *msg = msg_allocate();
#endif
    msg->type = MSG_SEND_ESEL;
    msg->data[0] = i_eid;
    eselInitData *eselData = 
    new eselInitData(i_selEventList, i_eselData, i_dataSize);

    msg->extra_data = eselData;

#ifdef __HOSTBOOT_RUNTIME
    process_esel(msg);
#else
    // one message queue to the SEL thread
    static msg_q_t mq = Singleton<IpmiSEL>::instance().msgQueue();

    //Send the msg to the sel thread
    int rc = msg_send(mq,msg);
    if(rc)
    {
        IPMI_TRAC(ERR_MRK "Failed (rc=%d) to send message",rc);
        delete eselData;
    }
#endif
    IPMI_TRAC(EXIT_MRK "sendESEL");
    return;
}

/*
 * @brief process esel msg
 */
void process_esel(msg_t *i_msg)
{
    errlHndl_t l_err = NULL;
    IPMI::completion_code l_cc = IPMI::CC_UNKBAD;
    const uint32_t l_eid = i_msg->data[0];
    eselInitData * l_data =
            (eselInitData*)(i_msg->extra_data);
    IPMI_TRAC(ENTER_MRK "process_esel");
    selRecord l_eSel;
    oemSelRecord l_oemSel;

    do
    {
        IPMI_TRAC(ENTER_MRK"sel list size %d", l_data->selInfoList.size());
        std::vector<sel_info_t*>::iterator it;
        for (it = l_data->selInfoList.begin(); it != l_data->selInfoList.end(); 
        ++it)
        {
            sel_info_t *l_sel = *it;
            memset(l_data->oemSel,0,sizeof(oemSelRecord));
            memset(l_data->eSel,0,sizeof(selRecord));
            l_data->selEvent = true;

            //If sensor type is sys event then need to send the oem sel
            //to handle procedure callout        
            if (l_sel->sensorType == TARGETING::SENSOR_TYPE_SYS_EVENT)
            {
                //oem sel data
                l_data->selEvent = false;
                l_oemSel.record_type = 
                record_type_oem_sel_for_procedure_callout;
                l_oemSel.event_data1 = l_sel->eventOffset;
                l_sel->eventOffset = SENSOR::UNDETERMINED_SYSTEM_HW_FAILURE;
                memcpy(l_data->oemSel,&l_oemSel,sizeof(oemSelRecord));
            }

            //sel data
            l_eSel.record_type = record_type_system_event;
            l_eSel.generator_id = generator_id_ami;
            l_eSel.evm_format_version = format_ipmi_version_2_0;
            l_eSel.sensor_type = l_sel->sensorType;
            l_eSel.sensor_number = l_sel->sensorNumber;
            l_eSel.event_dir_type = l_sel->eventDirType;
            l_eSel.event_data1 = l_sel->eventOffset;
            memcpy(l_data->eSel,&l_eSel,sizeof(selRecord));
                

            uint32_t l_send_count = MAX_SEND_COUNT;
            while (l_send_count > 0)
            {
                // try to send the eles to the bmc
                send_esel(l_data, l_err, l_cc);

                // if no error but last completion code was:
                if ((l_err == NULL) &&
                    ((l_cc == IPMI::CC_BADRESV) ||  // lost reservation
                    (l_cc == IPMI::CC_TIMEOUT)))   // timeout
                {
                    // update our count and pause
                    l_send_count--;
                    if (l_send_count)
                    {
                        IPMI_TRAC("process_esel: sleeping; retry_count %d",
                        l_send_count);
                        // sleep 3 times - 2ms, 32ms, 512ms. if we can't get this
                        //  through by then, the system must really be busy...
                        nanosleep(0,
                        SLEEP_BASE << (4 * (MAX_SEND_COUNT - l_send_count - 1)));
                        continue;
                    }
                }
                else
                {
                    //if we enter this, then pel data has logged successfully,
                    //so we don't need to log again, make the size to zero.
                    l_data->dataSize = 0;
                }
                // else it did get sent down OR it didn't because of a bad error
                break;
            } // while
        } // for

    }while(0);

    if(l_err)
    {
#ifdef __HOSTBOOT_RUNTIME
        // HBRT can't commit an error, since this could already be in the
        // errlCommit path since HBRT is single threaded
        IPMI_TRAC(ERR_MRK "DELETING l_err 0x%.8X", l_err->eid());
        delete l_err;
        l_err = NULL;
#else
        l_err->collectTrace(IPMI_COMP_NAME);
        errlCommit(l_err, IPMI_COMP_ID);
#endif
    }
    else if((l_cc == IPMI::CC_OK) &&        // no error
            (l_eid != 0))                   // and it's an errorlog
    {
        // eSEL successfully sent to the BMC - have errlmanager do the ack
        IPMI_TRAC(INFO_MRK "Doing ack for eid 0x%.8X", l_eid);
        ERRORLOG::ErrlManager::errlAckErrorlog(l_eid);
    }

    delete l_data;

    IPMI_TRAC(EXIT_MRK "process_esel");
    return;
} // process_esel

/*
 * @brief Send esel data to bmc
 */
void send_esel(eselInitData * i_data,
            errlHndl_t &o_err, IPMI::completion_code &o_cc)
{
    IPMI_TRAC(ENTER_MRK "send_esel");
    uint8_t* data = NULL;

    size_t len = 0;

    uint8_t sel_recordID[2] = {0,0};
    uint8_t esel_recordID[2] = {0,0};

    do
    {
        const size_t l_eSELlen = i_data->dataSize;

        if (l_eSELlen == 0)
        {
            IPMI_TRAC(INFO_MRK "no eSEL data present, skipping to SEL");

            // sending sensor SELs only, not the eSEL
            o_cc = IPMI::CC_OK;

            break;
        }

        uint8_t reserveID[2] = {0,0};
        // we need to send down the extended sel data (eSEL), which is
        // longer than the protocol buffer, so we need to do a reservation and
        // call the AMI partial_add_esel command multiple times

        // put a reservation on the SEL Device since we're doing a partial_add
        len = 0;
        delete [] data;
        data = NULL;
        o_cc = IPMI::CC_UNKBAD;
        o_err = IPMI::sendrecv(IPMI::reserve_sel(),o_cc,len,data);
        if(o_err)
        {
            IPMI_TRAC(ERR_MRK "error from reserve_sel");
            break;
        }
        if(o_cc != IPMI::CC_OK)
        {
            IPMI_TRAC(ERR_MRK "Failed to reserve_sel, o_cc %02x",o_cc);
            break;
        }
        storeReserveRecord(reserveID,data);

        // first send down the SEL Event Record data
        size_t eSELindex = 0;
        uint8_t l_lastEntry = 0;
        len = PARTIAL_ADD_ESEL_REQ + sizeof(selRecord);
        delete [] data;
        data = new uint8_t[len];

        // fill in the partial_add_esel request (command) data
        createPartialAddHeader(reserveID,esel_recordID,eSELindex,l_lastEntry,data);

        // copy in the SEL event record data
        memcpy(&data[PARTIAL_ADD_ESEL_REQ], i_data->eSel,
                sizeof(selRecord));
        // update to make this what AMI eSEL wants
        data[PARTIAL_ADD_ESEL_REQ + offsetof(selRecord,record_type)] = record_type_ami_esel;
        data[PARTIAL_ADD_ESEL_REQ + offsetof(selRecord,event_data1)] = event_data1_ami;

        o_cc = IPMI::CC_UNKBAD;
        TRACFBIN( g_trac_ipmi, INFO_MRK"1st partial_add_esel:", data, len);
        o_err = IPMI::sendrecv(IPMI::partial_add_esel(),o_cc,len,data);
        if(o_err)
        {
            IPMI_TRAC(ERR_MRK "error from first partial_add_esel");
            break;
        }
        // as long as we continue to get CC_OK, the reserve sel is good.
        // if the reservation is lost (ie, because something else tried to
        // create a SEL) then the BMC just discards all this data. the
        // errorlog will still be in PNOR and won't get ACKed, so it'll get
        // resent on the next IPL.
        if (o_cc != IPMI::CC_OK)
        {
            IPMI_TRAC(ERR_MRK "failed first partial_add_esel, o_cc %02x, eSELindex %02x",
                    o_cc, eSELindex);
            break;
        }
        // BMC returns the recordID, it's always the same (unless
        // there's a major BMC bug...)
        storeReserveRecord(esel_recordID,data);

        // now send down the eSEL data in chunks.
        const size_t l_maxBuffer = IPMI::max_buffer();
        while(eSELindex<l_eSELlen)
        {
            //if the index + the maximum buffer is less than what we still
            //have left in the eSEL, this is not the last entry (data[6] = 0)
            //otherwise, it is and data[6] = 1
            if(eSELindex + (l_maxBuffer - PARTIAL_ADD_ESEL_REQ)
                    < l_eSELlen)
            {
                len = l_maxBuffer;
                l_lastEntry = 0x00;
            }
            else
            {
                len = l_eSELlen - eSELindex + PARTIAL_ADD_ESEL_REQ;
                l_lastEntry = 0x01;
            }
            delete [] data;
            data = new uint8_t[len];

            // fill in the partial_add_esel request (command) data
            createPartialAddHeader(reserveID, esel_recordID,
                    eSELindex + sizeof(selRecord),
                    l_lastEntry, data);

            uint8_t dataCpyLen = len - PARTIAL_ADD_ESEL_REQ;
            memcpy(&data[PARTIAL_ADD_ESEL_REQ],
                    &i_data->eSelExtra[eSELindex],
                    dataCpyLen);

            // update the offset into the data
            eSELindex = eSELindex + dataCpyLen;

            o_cc = IPMI::CC_UNKBAD;
            TRACFBIN( g_trac_ipmi, INFO_MRK"partial_add_esel:", data, len);
            o_err = IPMI::sendrecv(IPMI::partial_add_esel(),o_cc,len,data);
            if(o_err)
            {
                IPMI_TRAC(ERR_MRK "error from partial_add_esel");
                break;
            }
            // as long as we continue to get CC_OK, the reserve sel is good.
            // if the reservation is lost (ie, because something else tried to
            // create a SEL) then the BMC just discards all this data. the
            // errorlog will still be in PNOR and won't get ACKed, so it'll get
            // resent on the next IPL.
            if (o_cc != IPMI::CC_OK)
            {
                IPMI_TRAC(ERR_MRK "failed partial_add_esel, o_cc %02x, eSELindex %02x",
                        o_cc, eSELindex);
                break;
            }
            // BMC returns the recordID, it's always the same (unless
            // there's a major BMC bug...)
            storeReserveRecord(esel_recordID,data);
        } // while eSELindex
        if (o_cc == IPMI::CC_OK)
        {
            memcpy(i_data->eselRecord,esel_recordID,sizeof(esel_recordID));
        }
    }while(0);

    // if eSEL wasn't created due to an error, we don't want to continue
    if ((o_err == NULL) && (o_cc == IPMI::CC_OK))
    {
        // caller wants us to NOT create sensor SEL
        if ((i_data->eSel[offsetof(selRecord,sensor_type)] == SENSOR::INVALID_TYPE) &&
            (i_data->eSel[offsetof(selRecord,sensor_number)] == TARGETING::UTIL::INVALID_IPMI_SENSOR)
            )
        {
            IPMI_TRAC(INFO_MRK "Invalid sensor type/number - NOT sending sensor SELs");
        }
        else
        {
            // if the eSEL wasn't created due to a bad completion code, we will
            // still try to send down a SEL that we create, which will contain
            // the eSEL recordID (if it was successful)
            if (data)
            {
                delete [] data;
            }
            len = sizeof(IPMISEL::selRecord);
            data = new uint8_t[len];


            // send standard SEL event
            if (i_data->selEvent)
            {
                // copy in the SEL event record data
                memcpy(data, i_data->eSel, len);
                // copy the eSEL recordID (if it was created) into the extra data area
                // and mark the event_data1 to indicate this is OEM data
                data[offsetof(selRecord,event_data1)] |= 0xA0;
                data[offsetof(selRecord,event_data2)] = i_data->eselRecord[1];
                data[offsetof(selRecord,event_data3)] = i_data->eselRecord[0];
            }
            else //send OEM SEL event
            {
                // copy in the SEL event record data
                memcpy(data, i_data->oemSel, len);
                data[offsetof(oemSelRecord,event_data5)] = i_data->eselRecord[1];
                data[offsetof(oemSelRecord,event_data6)] =i_data->eselRecord[0];
            }

            // use local cc so that we don't corrupt the esel from above
            IPMI::completion_code l_cc = IPMI::CC_UNKBAD;
            TRACFBIN( g_trac_ipmi, INFO_MRK"add_sel:", data, len);
            o_err = IPMI::sendrecv(IPMI::add_sel(),l_cc,len,data);
            if(o_err)
            {
                IPMI_TRAC(ERR_MRK "error from add_sel");
            }
            else if (l_cc != IPMI::CC_OK)
            {
                IPMI_TRAC(ERR_MRK "failed add_sel, l_cc %02x", l_cc);
            }
            else
            {
                // if CC_OK, then len=2 and data contains the recordID of the new SEL
                storeReserveRecord(sel_recordID,data);
            }
        }
    }

    if (data)
    {
        delete [] data;
    }

    IPMI_TRAC(EXIT_MRK
        "send_esel o_err=%.8X, o_cc=x%.2x, sel recID=x%x%x, esel recID=x%x%x",
        o_err ? o_err->plid() : NULL, o_cc, sel_recordID[1], sel_recordID[0],
        esel_recordID[1], esel_recordID[0]);

    return;
} // send_esel

uint32_t get_sel_time()
{
    uint8_t *data = NULL;
    size_t len = 0;
    errlHndl_t l_err;
    IPMI::completion_code l_cc = IPMI::CC_UNKBAD;

    l_err = IPMI::sendrecv( IPMI::get_sel_time(), l_cc, len, data );
    if( l_err || l_cc != IPMI::CC_OK || len != sizeof(uint32_t) )
    {
        delete l_err;
        delete [] data;
        return 0;
    }

    uint32_t timet = le32toh(*reinterpret_cast<uint32_t*>(data));
    delete [] data;
    return timet;
}

} // IPMISEL

#ifndef __HOSTBOOT_RUNTIME
/**
 * @brief Constructor
 */
IpmiSEL::IpmiSEL(void)
    :iv_msgQ(msg_q_create())
{
    IPMI_TRAC(ENTER_MRK "IpmiSEL ctor");
    task_create(&IpmiSEL::start,NULL);
}

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

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

/**
 * @brief Entry point of the sel ipmi thread
 */
//@todo: RTC 119832
void IpmiSEL::execute(void)
{
    //Mark as an independent daemon so if it crashes we terminate.
    task_detach();

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

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

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

        switch(msg_type)
        {
            case IPMISEL::MSG_SEND_ESEL:
                IPMISEL::process_esel(msg);
                //done with msg
                msg_free(msg);
                break;

            case IPMISEL::MSG_STATE_SHUTDOWN:
                IPMI_TRAC(INFO_MRK "ipmisel shutdown event");

                //Respond that we are done shutting down.
                msg_respond(iv_msgQ, msg);
                break;

            case IPMISEL::MSG_STATE_SHUTDOWN_SEL:
                IPMI_TRAC(INFO_MRK "ipmisel "
                   "shutdown message from ipmirp");
                 msg->type = IPMI::MSG_STATE_SHUTDOWN_SEL;
                //Respond that we are done shutting down.
                msg_respond(iv_msgQ, msg);
                break;
        }
    } // while(1)
    IPMI_TRAC(EXIT_MRK "message loop");
    return;
} // execute
#endif

OpenPOWER on IntegriCloud