summaryrefslogtreecommitdiffstats
path: root/src/usr/ipmi/ipmisel.C
blob: 6ae3e4b6399c5246a087f0685139bc86fc564aea (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/ipmi/ipmisel.C $                                      */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2015                        */
/* [+] 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 <sys/task.h>
#include <initservice/taskargs.H>
#include <initservice/initserviceif.H>

#include <errl/errlmanager.H>

//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,
              uint8_t i_eventDirType, uint8_t i_eventOffset,
              uint8_t i_sensorType, uint8_t i_sensorNumber)
{
    IPMI_TRAC(ENTER_MRK "sendESEL()");

#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;

    // create the sel record of information
    selRecord l_sel;
    l_sel.record_type = record_type_system_event;
    l_sel.generator_id = generator_id_ami;
    l_sel.evm_format_version = format_ipmi_version_2_0;
    l_sel.sensor_type = i_sensorType;
    l_sel.sensor_number = i_sensorNumber;
    l_sel.event_dir_type = i_eventDirType;
    l_sel.event_data1 = i_eventOffset;

    eselInitData *eselData =
        new eselInitData(&l_sel, 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;
} // sendESEL

/*
 * @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");

    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 it did get sent down OR it didn't because of a bad error
        break;

    } // while

    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;

        // TODO RTC: 123419 investigate adding a 'iv_isIpmiEnabled' flag and setting
        // that to false somehow, so that we can commit error logs in this path
        // and future calls wont try to send them down.
#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
    {
#ifdef __HOSTBOOT_RUNTIME
        // TODO RTC: 123419 story to add storing errlog to PNOR in HBRT if IPMI
#else
        // eSEL successfully sent to the BMC - send an ack to the errlmanager
        IPMI_TRAC(INFO_MRK "Sending ack for eid 0x%.8X", l_eid);
        ERRORLOG::ErrlManager::errlAckErrorlog(l_eid);
#endif
    }

    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 esel_recordID[2] = {0,0};
    uint8_t sel_recordID[2] = {0,0};

    do{
        const size_t l_eSELlen = i_data->dataSize;
        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
    }while(0);

    // if eSEL wasn't created due to an error, we don't want to continue
    if(o_err == NULL)
    {
        // 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)
        delete [] data;
        len = sizeof(IPMISEL::selRecord);
        data = new uint8_t[len];

        // copy in the SEL event record data
        memcpy(data, i_data->eSel, sizeof(IPMISEL::selRecord));
        // copy the eSEL recordID (if it was created) into the extra data area
        data[offsetof(selRecord,event_data2)] = esel_recordID[1];
        data[offsetof(selRecord,event_data3)] = esel_recordID[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);
        }
    }

    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

} // 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();

    // 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, IPMISEL::MSG_STATE_SHUTDOWN,
                                       INITSERVICE::MBOX_PRIORITY);

    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;
        }
    } // while(1)
    IPMI_TRAC(EXIT_MRK "message loop");
    return;
} // execute
#endif

OpenPOWER on IntegriCloud