summaryrefslogtreecommitdiffstats
path: root/src/usr/ipmiext/ipmidcmi.C
blob: b3f9d1e22509304edceadc446e8c9e2ba4a306f1 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/ipmiext/ipmidcmi.C $                                  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2019                        */
/* [+] International Business Machines Corp.                              */
/* [+] Maxim Polyakov                                                     */
/*                                                                        */
/*                                                                        */
/* 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 ipmidcmi.C
 * @brief IPMI DCMI command extensions
 */

#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <ipmi/ipmiif.H>
#include <ipmi/ipmisensor.H>
#include <ipmi/ipmi_reasoncodes.H>
extern trace_desc_t * g_trac_ipmi;

#define DCMI_CAP_RESPONSE_DATA_LENGTH 7

namespace SENSOR
{
    enum dcmi_cc
    {
        POWER_LIMIT_ACTIVE     = 0x00,
        POWER_LIMIT_NOT_ACTIVE = 0x80,
    };

    static errlHndl_t getPowerManagementSupportStatus(bool &support)
    {
        errlHndl_t err = NULL;
        IPMI::completion_code cc = IPMI::CC_UNKBAD;

        support = false;

        size_t len = 2;
        uint8_t* data = new uint8_t[len];
        data[0] = 0xDC; // Group Extension Identification
        data[1] = 0x01; // Selector - Supported DCMI Capabilities

        err = IPMI::sendrecv(IPMI::get_dcmi_capability_info(), cc, len, data);
        do
        {
            if (err)
            {
                TRACFCOMP(g_trac_ipmi,
                          "Failed to send DCMI Capabilities Command to BMC");
                break;
            }

            if (cc != IPMI::CC_OK)
            {
                TRACFCOMP(g_trac_ipmi,
                          "Get DCMI Capabilities Command: "
                          "bad completion code from BMC=0x%x",
                          cc);

                /*@
                 * @errortype       ERRL_SEV_INFORMATIONAL
                 * @moduleid        IPMI::MOD_IPMIDCMI
                 * @reasoncode      IPMI::RC_GET_DCMI_CAP_CMD_FAILED
                 * @userdata1       BMC IPMI Completion code.
                 * @devdesc         Request to get DCMI Capabilities information
                 *                  failed
                 * @custdesc        The DCMI Capabilities Info Command retrieve
                 *                  data from the BMC has failed.
                 *
                 */

                err = new ERRORLOG::ErrlEntry(
                      ERRORLOG::ERRL_SEV_INFORMATIONAL,
                      IPMI::MOD_IPMIDCMI,
                      IPMI::RC_GET_DCMI_CAP_CMD_FAILED,
                      static_cast<uint64_t>(cc),
                      0,
                      true);
                break;
            }

            if (len != DCMI_CAP_RESPONSE_DATA_LENGTH)
            {
                TRACFCOMP(g_trac_ipmi,
                          "Get DCMI Capabilities Command: "
                          "invalid data length=%d",
                          len);

                /*@
                 * @errortype       ERRL_SEV_INFORMATIONAL
                 * @moduleid        IPMI::MOD_IPMIDCMI
                 * @reasoncode      IPMI::RC_INVALID_QRESPONSE
                 * @userdata1       Response data length.
                 * @devdesc         Request to get DCMI Capabilities information
                 *                  failed
                 * @custdesc        The DCMI Capabilities Info Command retrieve
                 *                  data with invalid length.
                 *
                 */

                err = new ERRORLOG::ErrlEntry(
                      ERRORLOG::ERRL_SEV_INFORMATIONAL,
                      IPMI::MOD_IPMIDCMI,
                      IPMI::RC_INVALID_QRESPONSE,
                      static_cast<uint64_t>(len),
                      0,
                      true);
                break;
            }

            // from the DCMI spec v1.5, Revision 1.0, August 23, 2011
            // DCMI Capabilities Response Command Format:
            // cc:       byte 1   completion code
            // data:
            // data[0]:  byte 1   0xDC
            // data[1]:  byte 2   Major Version (01h)
            // data[2]:  byte 3   Minor Version (05h)
            // data[3]:  byte 4   Parameter Revision (02h)
            // data[4]:  byte 5   Reserved
            // data[5]:  byte 6   Platform capabilities
            //           [7:1] Reserved
            //           [0]   Power management
            // data[6]:  byte 7   Manageability Access Capabilities
            support = !!(data[5] & 0x1);

        } while(false);

        delete[] data;
        return err;
    }

    // fetch the user defined power limit stored on the BMC
    // using the DCMI Get Power Limit command
    errlHndl_t getUserPowerLimit( uint16_t &o_powerLimit, bool &o_limitActive )
    {
        o_powerLimit = 0;

        // assume the limit has not been activated
        o_limitActive = false;

        errlHndl_t l_err = NULL;

        // Power Management support check
        bool support;
        l_err = getPowerManagementSupportStatus(support);
        if (l_err != NULL)
        {
            // Since the Power Management support information isn`t received,
            // commit this error and still try to read the power limit. If the
            // Power Management is really unsupported, the DCMI Get Power Limit
            // command will return error code in l_cc
            l_err->collectTrace(IPMI_COMP_NAME);
            errlCommit(l_err, IPMI_COMP_ID);

            TRACFCOMP(g_trac_ipmi,
                "Failed to determine if the BMC supports Power Management");

            support = true;
        }

        if (!support)
        {
            TRACFCOMP(g_trac_ipmi,
                      "Power Management is not supported by BMC");
            return NULL;
        }

        // per DCMI spec data size is 3 bytes
        size_t len = 3;

        //create request data buffer
        uint8_t* data = new uint8_t[len];

        IPMI::completion_code cc = IPMI::CC_UNKBAD;

        data[0] = 0xDC;  // Group Extension Identification
        data[1] = 0x00;  // reserved
        data[2] = 0x00;  // reserved

        l_err = IPMI::sendrecv(IPMI::get_power_limit(), cc, len, data);

        SENSOR::dcmi_cc l_cc = static_cast<SENSOR::dcmi_cc>(cc);

        if( l_err == NULL )
        {
            // make sure the completion code is good, then read the bytes
            if( (l_cc == POWER_LIMIT_NOT_ACTIVE) ||
                    (l_cc == POWER_LIMIT_ACTIVE) )
            {
                // from the dcmi spec
                // byte 1   completion code
                // byte 2   0xDC
                // byte 3:4 reserved
                // byte 5   exception actions
                // byte 6:7 power limit
                // data[0] is pointing at byte 2 of the dcmi spec description
                // so our offsets will be off by two below
                o_powerLimit = data[5];
                o_powerLimit = ( o_powerLimit << 8 ) + data[4];

                // fetch the derating factor from the BMC, then apply it to
                // the value returned in the getPowerLimit command

                SENSOR::getSensorReadingData o_sensorData;

                // derating factor is held in the system target
                SENSOR::SensorBase(
                        TARGETING::SENSOR_NAME_DERATING_FACTOR,
                        NULL ).readSensorData( o_sensorData );

                // derate the power limit based on the returned value of the
                // sensor - derating factor is returned as a % value and is
                // stored in the event_status field.
                o_powerLimit = ( static_cast<uint64_t>(o_powerLimit) *
                                            o_sensorData.event_status)/100;

                TRACFCOMP(g_trac_ipmi,"Derating factor = %i",o_sensorData.event_status);
                TRACFCOMP(g_trac_ipmi,"Power limit = 0x%i",o_powerLimit);
                TRACFCOMP(g_trac_ipmi,"Power limit is %s", ((cc) ? "not active": "active"));

                // the completion code also tells us if the limit is active
                if(l_cc == POWER_LIMIT_ACTIVE )
                {
                    o_limitActive = true;
                }
            }
            else
            {
                TRACFCOMP(g_trac_ipmi,"bad completion code from BMC=0x%x",cc);

                /*@
                 * @errortype       ERRL_SEV_UNRECOVERABLE
                 * @moduleid        IPMI::MOD_IPMIDCMI
                 * @reasoncode      IPMI::RC_DCMI_CMD_FAILED
                 * @userdata1       BMC IPMI Completion code.
                 * @devdesc         Request to get power limit information
                 *                  failed
                 * @custdesc        The DCMI command to retrieve the power limit
                 *                  data from the BMC has failed, the user
                 *                  defined power limit cannot be applied.
                 *
                 */

                l_err = new ERRORLOG::ErrlEntry(
                        ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                        IPMI::MOD_IPMIDCMI,
                        IPMI::RC_DCMI_CMD_FAILED,
                        static_cast<uint64_t>(cc),0, true);

                l_err->collectTrace(IPMI_COMP_NAME);

            }

            delete[] data;
        }

        return l_err;
    }

}; // end name space
OpenPOWER on IntegriCloud