summaryrefslogtreecommitdiffstats
path: root/src/usr/hwpf/hwp/mvpd_accessors/getMBvpdSlopeInterceptData.C
blob: bd30909ed1ec6df4abccc1100f959fa748efd167 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/hwpf/hwp/mvpd_accessors/getMBvpdSlopeInterceptData.C $ */
/*                                                                        */
/* IBM CONFIDENTIAL                                                       */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2013,2014              */
/*                                                                        */
/* p1                                                                     */
/*                                                                        */
/* Object Code Only (OCO) source materials                                */
/* Licensed Internal Code Source Materials                                */
/* IBM HostBoot Licensed Internal Code                                    */
/*                                                                        */
/* The source code for this program is not published or otherwise         */
/* divested of its trade secrets, irrespective of what has been           */
/* deposited with the U.S. Copyright Office.                              */
/*                                                                        */
/* Origin: 30                                                             */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
// $Id: getMBvpdSlopeInterceptData.C,v 1.4 2014/02/12 22:12:00 mjjones Exp $
/**
 *  @file getMBvpdSlopeInterceptData.C
 *
 *  @brief get master and supplier power slope and intercept data
 *         from MBvpd MV and MW keywords
 *
 */

#include    <stdint.h>

//  fapi support
#include    <fapi.H>
#include    <getMBvpdSlopeInterceptData.H>

extern "C"
{
using namespace fapi;

// local function to get master power slope and intercept data
fapi::ReturnCode getMBvpdMasterData(
                              const fapi::Target   &i_mbTarget,
                              const fapi::MBvpdSlopeIntercept i_attr,
                              uint32_t & o_val);

// local function to get supplier power slope and intercept data
fapi::ReturnCode getMBvpdSupplierData(
                              const fapi::Target   &i_mbTarget,
                              const fapi::MBvpdSlopeIntercept i_attr,
                              uint32_t & o_val);

/**
 * @brief get power slope and intercept data from cvpd record VSPD
 *        keyword MW and MV
 * @param[in]  i_mbTarget   -   mb target
 * @param[in]  i_attr       -   enumerator to select requested value
 * @param[out] o_val        -   master/supplier slope/intercept value
 *
 * @return fapi::ReturnCode -   FAPI_RC_SUCCESS if success,
 *                              relevant error code for failure.
 */

fapi::ReturnCode getMBvpdSlopeInterceptData(
                              const fapi::Target   &i_mbTarget,
                              const fapi::MBvpdSlopeIntercept i_attr,
                              uint32_t & o_val)
{
    fapi::ReturnCode l_fapirc;

    FAPI_DBG("getMBvpdSlopeInterceptData: entry ");

    // get master values from MW keyword or supplier values from MV keyword
    switch (i_attr)
    {
       case MASTER_POWER_SLOPE:
       case MASTER_POWER_INTERCEPT:
           l_fapirc = getMBvpdMasterData(i_mbTarget, i_attr, o_val);
           break;
       case SUPPLIER_POWER_SLOPE:
       case SUPPLIER_POWER_INTERCEPT:
           l_fapirc = getMBvpdSupplierData(i_mbTarget, i_attr, o_val);
           break;
       default: // Unlikely, but needs to be caught
           FAPI_ERR("getMBvpdSlopeInterceptData: invalid attribute ID 0x%02x",
                       i_attr);
           const fapi::MBvpdSlopeIntercept & ATTR_ID = i_attr;
           FAPI_SET_HWP_ERROR(l_fapirc, RC_MBVPD_INVALID_ATTRIBUTE_ID);
    }

    FAPI_DBG("getMBvpdSlopeInterceptData: exit rc=0x%08x",
               static_cast<uint32_t>(l_fapirc));
    return  l_fapirc;
}

// local function to get master power slope and intercept data
//
// the master power slope and intercept are in the MW keyword
//
fapi::ReturnCode getMBvpdMasterData(
                              const fapi::Target   &i_mbTarget,
                              const fapi::MBvpdSlopeIntercept i_attr,
                              uint32_t & o_val)
{
    fapi::ReturnCode l_fapirc;

    //MW keyword layout
    struct mw_keyword
    {
        uint8_t     MWKeywordVersion;
        uint8_t     masterPowerSlope_MSB;     //big endian order
        uint8_t     masterPowerSlope_LSB;
        uint8_t     masterPowerIntercept_MSB; //big endian order
        uint8_t     masterPowerIntercept_LSB;
        uint8_t     reserved[4];
        uint8_t     tempSensorPrimaryLayout;
        uint8_t     tempSensorSecondaryLayout;
    };
    const uint32_t MW_KEYWORD_SIZE = sizeof(mw_keyword);  // keyword size

    mw_keyword * l_pMwBuffer = NULL; // MBvpd MW keyword buffer
    uint32_t  l_MwBufsize = sizeof(mw_keyword);

    FAPI_DBG("getMBvpdMasterData: entry ");

    do {

        l_pMwBuffer = new mw_keyword;

        // Read the MW keyword field
        l_fapirc = fapiGetMBvpdField(fapi::MBVPD_RECORD_VSPD,
                                     fapi::MBVPD_KEYWORD_MW,
                                     i_mbTarget,
                                     reinterpret_cast<uint8_t *>(l_pMwBuffer),
                                     l_MwBufsize);
        if (l_fapirc)
        {
            FAPI_ERR("getMBvpdMasterData: Read of MV keyword failed");
            break;  //  break out with fapirc
        }

        // Check that sufficient MW keyword was returned.
        if (l_MwBufsize < MW_KEYWORD_SIZE )
        {
            FAPI_ERR("getMBvpdMasterData:"
                     " less MW keyword returned than expected %d < %d",
                       l_MwBufsize, MW_KEYWORD_SIZE);
            const uint32_t & KEYWORD = fapi::MBVPD_KEYWORD_MW;
            const uint32_t & RETURNED_SIZE = l_MwBufsize;
            const fapi::Target & CHIP_TARGET = i_mbTarget;
            FAPI_SET_HWP_ERROR(l_fapirc, RC_MBVPD_INSUFFICIENT_VPD_RETURNED );
            break;  //  break out with fapirc
        }

        // Return requested value
        switch (i_attr)
        {
           case MASTER_POWER_SLOPE:  //get each byte to perserve endian
               o_val = l_pMwBuffer->masterPowerSlope_LSB;
               o_val |= (l_pMwBuffer->masterPowerSlope_MSB << 8);
               break;
           case MASTER_POWER_INTERCEPT:  //get each byte to perserve endian
               o_val = l_pMwBuffer->masterPowerIntercept_LSB;
               o_val |= (l_pMwBuffer->masterPowerIntercept_MSB << 8);
               break;
       default: //i_attr value was checked before call so should not get here
               break;
       }

    } while (0);

    delete l_pMwBuffer;
    l_pMwBuffer = NULL;

    FAPI_DBG("getMBvpdMasterData: exit rc=0x%08x",
               static_cast<uint32_t>(l_fapirc));

    return  l_fapirc;
}

// local function to get supplier power slope and intercept data
//
// Read the #I keyword to get the module ID of this CDIMM
// Then read the #MV keyword which has all the vendor supplied info
// and search the list for the module ID found in the #I keyword
//
fapi::ReturnCode getMBvpdSupplierData(
                              const fapi::Target   &i_mbTarget,
                              const fapi::MBvpdSlopeIntercept i_attr,
                              uint32_t & o_val)
{

    //#I keyword layout
    const uint32_t  PDI_DDR3_KEYWORD_SIZE = 256;
    const uint32_t  PDI_DDR4_KEYWORD_SIZE = 512; // assumed size for DDR4
    const uint8_t   SPD_DDR3 = 0xB;
    const uint8_t   SPD_DDR4 = 0xC;
    struct pdI_keyword
    {
        union
        {
            struct // common
            {
                uint8_t   filler1[2];
                uint8_t   mem_type;
            } common;
            struct // DDR3 layout of #I
            {
                uint8_t   filler1[117]; // other fields and reserved bytes
                uint8_t   moduleID_MSB; // at offset 117. Big endian order
                uint8_t   moduleID_LSB; // VPD data CCIN_31E1_v.5.3.ods
                uint8_t   filler2[PDI_DDR3_KEYWORD_SIZE-117-2]; //trailing space
            } ddr3;
            struct // DDR4 layout of #I
            {
                uint8_t   filler1[320]; // other fields and reserved bytes
                uint8_t   moduleID_MSB; // at offset 320. Big endian order
                uint8_t   moduleID_LSB; //
                uint8_t   filler2[PDI_DDR4_KEYWORD_SIZE-320-2]; //trailing space
            } ddr4;
        } pdI;
    };

    //MV keyword layout
    struct mv_vendorInfo
    {
        uint8_t   supplierID_MSB;              // Big endian order
        uint8_t   supplierID_LSB;
        uint8_t   supplierPowerSlope_MSB;      // Big endian order
        uint8_t   supplierPowerSlope_LSB;
        uint8_t   supplierPowerIntercept_MSB;  // Big endian order
        uint8_t   supplierPowerIntercept_LSB;
        uint8_t   reserved[4];
    };
    struct mv_keyword //variable length. Structure is size of 1 entry.
    {
        uint8_t   version;
        uint8_t   numEntries;
        mv_vendorInfo firstVendorInfo;
       // variable number of vendor supplied entries
    };

    fapi::ReturnCode l_fapirc;
    pdI_keyword  *  l_pPdIBuffer = NULL;   // MBvpd #I keyword buffer
    uint32_t        l_pdIBufsize = sizeof(pdI_keyword);
    uint8_t         l_moduleID_LSB = 0;    // module ID to look for
    uint8_t         l_moduleID_MSB = 0;
    mv_keyword   *  l_pMvBuffer = NULL;    // MBvpd MV keyword buffer
    uint32_t        l_mvBufsize = 0;       // variable length
    mv_vendorInfo * l_pVendorInfo = NULL;
    uint32_t        l_offset = 0;
    bool            l_found = false;

    do {

        l_pPdIBuffer = new pdI_keyword;

        // Read the #I keyword field to get the Module ID
        l_fapirc = fapiGetMBvpdField(fapi::MBVPD_RECORD_VSPD,
                                     fapi::MBVPD_KEYWORD_PDI,
                                     i_mbTarget,
                                     reinterpret_cast<uint8_t *>(l_pPdIBuffer),
                                     l_pdIBufsize);
        if (l_fapirc)
        {
            FAPI_ERR("getMBvpdSupplierData: Read of pdI keyword failed");
            break;  //  break out with fapirc
        }

        FAPI_DBG("getMBvpdSupplierData: #I mem type=0x%02x ",
            l_pPdIBuffer->pdI.common.mem_type);

        // check for DDR3 or DDR4
        if (SPD_DDR3 == l_pPdIBuffer->pdI.common.mem_type )
        {
            // Check that sufficient #I was returned.
            if (l_pdIBufsize < PDI_DDR3_KEYWORD_SIZE )
            {
                FAPI_ERR("getMBvpdSupplierData:"
                         " less DDR3 #I keyword returned than expected %d < %d",
                           l_pdIBufsize, PDI_DDR3_KEYWORD_SIZE);
                const uint32_t & KEYWORD = fapi::MBVPD_KEYWORD_PDI;
                const uint32_t & RETURNED_SIZE = l_pdIBufsize;
                const fapi::Target & CHIP_TARGET = i_mbTarget;
                FAPI_SET_HWP_ERROR(l_fapirc,RC_MBVPD_INSUFFICIENT_VPD_RETURNED);
                break;  //  break out with fapirc
            }

            // grab module ID
            l_moduleID_LSB = l_pPdIBuffer->pdI.ddr3.moduleID_LSB;
            l_moduleID_MSB = l_pPdIBuffer->pdI.ddr3.moduleID_MSB;
        }
        else if (SPD_DDR4 == l_pPdIBuffer->pdI.common.mem_type )
        {
            // Check that sufficient #I was returned.
            if (l_pdIBufsize < PDI_DDR4_KEYWORD_SIZE )
            {
                FAPI_ERR("getMBvpdSupplierData:"
                         " less DDR4 #I keyword returned than expected %d < %d",
                           l_pdIBufsize, PDI_DDR4_KEYWORD_SIZE);
                const uint32_t & KEYWORD = fapi::MBVPD_KEYWORD_PDI;
                const uint32_t & RETURNED_SIZE = l_pdIBufsize;
                const fapi::Target & CHIP_TARGET = i_mbTarget;
                FAPI_SET_HWP_ERROR(l_fapirc,RC_MBVPD_INSUFFICIENT_VPD_RETURNED);
                break;  //  break out with fapirc
            }

            // grab module ID
            l_moduleID_LSB = l_pPdIBuffer->pdI.ddr4.moduleID_LSB;
            l_moduleID_MSB = l_pPdIBuffer->pdI.ddr4.moduleID_MSB;
        }
        else
        {
            FAPI_ERR("getMBvpdSupplierData:"
                     " unexpected memory type in #I");
            const uint8_t & MEM_TYPE = l_pPdIBuffer->pdI.common.mem_type;
            const fapi::Target & MEMBUF_TARGET = i_mbTarget;
            FAPI_SET_HWP_ERROR(l_fapirc,RC_MBVPD_UNEXPECTED_MEM_TYPE);
            break;  //  break out with fapirc
        }

        // Done with #I buffer. Error paths free buffer at end.
        delete l_pPdIBuffer;
        l_pPdIBuffer = NULL;

        FAPI_DBG("getMBvpdSupplierData: #I moduleID=0x%08x ",
            l_moduleID_LSB+(l_moduleID_MSB<<8));

        // see how big the MV keyword is as it is variable length
        l_fapirc = fapiGetMBvpdField(fapi::MBVPD_RECORD_VSPD,
                                     fapi::MBVPD_KEYWORD_MV,
                                     i_mbTarget,
                                     NULL, //pass NULL buff pointer to get size
                                     l_mvBufsize);
        if (l_fapirc)
        {
             FAPI_ERR("getMBvpdSupplierData: Read of MV keyword failed");
             break;  //  break out with fapirc
        }

        // read MV keyword
        l_pMvBuffer = (mv_keyword *)new uint8_t[l_mvBufsize];

        l_fapirc = fapiGetMBvpdField(fapi::MBVPD_RECORD_VSPD,
                                     fapi::MBVPD_KEYWORD_MV,
                                     i_mbTarget,
                                     reinterpret_cast<uint8_t *>(l_pMvBuffer),
                                     l_mvBufsize);
        if (l_fapirc)
        {
            FAPI_ERR("getMBvpdSupplierData: Read of MV keyword failed");
            break;  //  break out with fapirc
        }

        // Check that sufficient MV was returned to get at least the count.
        l_pVendorInfo = &(l_pMvBuffer->firstVendorInfo);
        l_offset = (uint8_t *)l_pVendorInfo - (uint8_t *)l_pMvBuffer;

        if (l_mvBufsize < l_offset )
        {
            FAPI_ERR("getMBvpdSupplierData:"
                     " less MV keyword returned than expected %d < %d",
                       l_mvBufsize, l_offset);
            const uint32_t & KEYWORD = fapi::MBVPD_KEYWORD_MV;
            const uint32_t & RETURNED_SIZE = l_mvBufsize;
            const fapi::Target & CHIP_TARGET = i_mbTarget;
            FAPI_SET_HWP_ERROR(l_fapirc, RC_MBVPD_INSUFFICIENT_VPD_RETURNED );
            break;  //  break out with fapirc
        }

        // look for matching module ID
        for (uint32_t l_count=0;l_count < l_pMvBuffer->numEntries;l_count++)
        {
            // shouldn't run past end of buffer, checking to be sure
            if (l_offset + sizeof (mv_vendorInfo) > l_mvBufsize)
            {
                break;
            }

            FAPI_DBG("getMBvpdSupplierData: cnt=%d this supplier ID= 0x%08x ",
              l_count,
              l_pVendorInfo->supplierID_LSB+(l_pVendorInfo->supplierID_MSB<<8));

            if ((l_pVendorInfo->supplierID_LSB == l_moduleID_LSB ) &&
                (l_pVendorInfo->supplierID_MSB == l_moduleID_MSB ))
            {
                l_found = true;
                break;
            }
            l_offset += sizeof (mv_vendorInfo);
            l_pVendorInfo++;
        }

        // If not found, see if first supplier should be used
        // and there is one (unlikely that there is not one).
        if ( ! l_found &&
             0 < l_pMvBuffer->numEntries &&
             sizeof (mv_keyword) <= l_mvBufsize)
        {
            uint8_t l_checkUseFirstSupplier = 0;
            l_fapirc = FAPI_ATTR_GET(
                       ATTR_CENTAUR_EC_USE_FIRST_SUPPLIER_FOR_INVALID_MODULE_ID,
                       &i_mbTarget,
                       l_checkUseFirstSupplier);
            if (l_fapirc)
            {
                FAPI_ERR("getMBvpdSupplierData:"
                         " get attr use first supplier failed");
                break;  //  break out with fapirc
            }
            FAPI_DBG("getMBvpdSupplierData: attr use first supplier = 0x%02x",
                      l_checkUseFirstSupplier);
            if ( l_checkUseFirstSupplier)
            {
                l_pVendorInfo = &(l_pMvBuffer->firstVendorInfo);
                l_found = true;
            }
        }

        // Return requested value if found
        if ( l_found )
        {
            switch (i_attr)
            {
               case SUPPLIER_POWER_SLOPE:  //get each byte to perserve endian
                   o_val = l_pVendorInfo->supplierPowerSlope_LSB;
                   o_val |= (l_pVendorInfo->supplierPowerSlope_MSB << 8);
                   break;
               case SUPPLIER_POWER_INTERCEPT: //get each byte to perserve endian
                   o_val = l_pVendorInfo->supplierPowerIntercept_LSB;
                   o_val |= (l_pVendorInfo->supplierPowerIntercept_MSB << 8);
                   break;
           default: //i_attr value was checked already so should not get here
                   break;
            }
       }
       else
       {
           FAPI_ERR("getMBvpdSupplierData:"
                    " supplier ID not found 0x%04x",
                      l_moduleID_LSB+(l_moduleID_MSB<<8));
           const uint32_t & MODULE_ID = l_moduleID_LSB+(l_moduleID_MSB<<8);
           const fapi::Target & MEMBUF_TARGET = i_mbTarget;
           FAPI_SET_HWP_ERROR(l_fapirc, RC_MBVPD_SUPPLIER_ID_NOT_IN_MV_VPD );
       }

    } while (0);

    delete l_pPdIBuffer;
    l_pPdIBuffer = NULL;
    delete l_pMvBuffer;
    l_pMvBuffer = NULL;

    FAPI_DBG("getMBvpdSupplierData: exit rc=0x%08x",
               static_cast<uint32_t>(l_fapirc));

    return  l_fapirc;
}


}   // extern "C"
OpenPOWER on IntegriCloud