summaryrefslogtreecommitdiffstats
path: root/src/usr/fapi2/plat_vpd_access.C
blob: 164062c9d9ca80da0ee73653c2e306fbbc9db87f (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/fapi2/plat_vpd_access.C $                             */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2012,2017                        */
/* [+] 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 plat_vpd_access.C
 *
 *  @brief Implements the GetVpd function
 */

#include <stdint.h>
#include <fapi2.H>
#include <vpd_access_defs.H>
#include <vpd_access.H>
#include <p9_get_mem_vpd_keyword.H>
#include <attribute_service.H>
#include <vpd/dvpdenums.H>
#include <vpd/memd_vpdenums.H>
#include <errl/errlmanager.H>
//The following can be uncommented for unit testing
//#undef FAPI_DBG
//#define FAPI_DBG(args...) FAPI_INF(args)

namespace fapi2
{

fapi2::ReturnCode platGetVPD(
                        const fapi2::Target<fapi2::TARGET_TYPE_MCS>& i_target,
                        VPDInfo<fapi2::TARGET_TYPE_MCS>& io_vpd_info,
                        uint8_t* o_blob)
{
    fapi2::ReturnCode l_rc = fapi2::FAPI2_RC_SUCCESS;
    errlHndl_t l_errl = nullptr;
    keywordInfo_t l_keywordInfo;
    uint8_t* l_dvpd_vm = nullptr;
    uint32_t* l_full_dvpdVM = nullptr;

    // Assume that all memory keywords (MR,MT,J0..JZ,X0...XZ) are all the
    // same size of 255. This avoids going through the decode and asking
    // the vpd DD the size of the keyword.
    const size_t VPD_KEYWORD_SIZE = 255;

    FAPI_DBG("platGetVPD: enter");

    do
    {
        // null blob pointer requests blob size
        if ( nullptr == o_blob) // just return size
        {
            // use a generic max blob size (DQ and CK need less)
            io_vpd_info.iv_size = VPD_KEYWORD_SIZE;
            FAPI_DBG("platGetVPD: return blob size of %d",
                 io_vpd_info.iv_size);
            break; //return success
        }

        //Make sure passed blob buffer is big enough
        if (VPD_KEYWORD_SIZE > io_vpd_info.iv_size)
        {
            FAPI_ERR("platGetVPD: blob size of %d too small, should be %d",
                     io_vpd_info.iv_size,
                     VPD_KEYWORD_SIZE);
            /*@
            * @errortype
            * @moduleid          fapi2::MOD_FAPI2_PLAT_GET_VPD
            * @reasoncode        fapi2::RC_BUFFER_TOO_SMALL
            * @userdata1         Buffer size
            * @userdata2         Expected size
            * @devdesc           Passed buffer too small
            * @custdesc          Firmware Error
            */
            l_errl = new ERRORLOG::ErrlEntry(
                             ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                             fapi2::MOD_FAPI2_PLAT_GET_VPD,
                             fapi2::RC_BUFFER_TOO_SMALL,
                             io_vpd_info.iv_size,
                             VPD_KEYWORD_SIZE,
                             true); //software callout
            l_rc.setPlatDataPtr(reinterpret_cast<void *> (l_errl));
            break; //return with error
        }

        // Get targeting MCS target
        TARGETING::Target * l_pMcsTarget = nullptr;
        l_errl = fapi2::platAttrSvc::getTargetingTarget(i_target, l_pMcsTarget);
        if (l_errl)
        {
            FAPI_ERR("platGetVPD: Error from getTargetingTarget");
            l_rc.setPlatDataPtr(reinterpret_cast<void *> (l_errl));
            break; //return with error
        }

        //Validate type and check for override
        uint64_t  l_mapKeyword  = 0;
        uint64_t  l_keywordEnum = 0; //set this now to be used later
        if ( fapi2::MT == io_vpd_info.iv_vpd_type )
        {
            if (1 ==
               l_pMcsTarget->getAttr<TARGETING::ATTR_VPD_OVERRIDE_MT_ENABLE>() )
            {
                uint8_t l_override[VPD_KEYWORD_SIZE]={0};
                assert(l_pMcsTarget->tryGetAttr<TARGETING::ATTR_VPD_OVERRIDE_MT>
                                         (l_override),
                       "platGetVPD: getAttr ATTR_VPD_OVERRIDE_MT failed");
                FAPI_DBG("platGetVPD: return MT override attr");
                memcpy(o_blob,l_override,VPD_KEYWORD_SIZE);
                break; //return with overriden keyword
            }

            // not overriden, continue
            l_mapKeyword = DVPD::MT;
            l_keywordEnum = DVPD::X0;
        }
        else if ( fapi2::MR == io_vpd_info.iv_vpd_type )
        {
            if (1==
               l_pMcsTarget->getAttr<TARGETING::ATTR_VPD_OVERRIDE_MR_ENABLE>() )
            {
                uint8_t l_override[VPD_KEYWORD_SIZE]={0};
                assert(l_pMcsTarget->tryGetAttr<TARGETING::ATTR_VPD_OVERRIDE_MR>
                                         (l_override),
                       "platGetVPD: getAttr ATTR_VPD_OVERRIDE_MR failed");
                FAPI_DBG("platGetVPD: return MR override attr");
                memcpy(o_blob,l_override,VPD_KEYWORD_SIZE);
                break; //return with overriden keyword
            }

            // not overriden, continue
            l_mapKeyword = DVPD::MR;
            l_keywordEnum = DVPD::J0;
        }
        else if ( fapi2::DQ == io_vpd_info.iv_vpd_type )
        {
            if (1==
               l_pMcsTarget->getAttr<TARGETING::ATTR_VPD_OVERRIDE_DQ_ENABLE>() )
            {
                ATTR_VPD_OVERRIDE_DQ_Type l_override={0};
                assert(l_pMcsTarget->tryGetAttr<TARGETING::ATTR_VPD_OVERRIDE_DQ>
                                         (l_override),
                       "platGetVPD: getAttr ATTR_VPD_OVERRIDE_DQ failed");
                FAPI_DBG("platGetVPD: return DQ override attr");
                memcpy(o_blob,l_override,sizeof(ATTR_VPD_OVERRIDE_DQ_Type));
                break; //return with overriden keyword
            }

            // not overriden, continue
            l_mapKeyword = DVPD::Q0;
            l_keywordEnum = DVPD::Q0;
        }
        else if ( fapi2::CK == io_vpd_info.iv_vpd_type )
        {
            if (1==
               l_pMcsTarget->getAttr<TARGETING::ATTR_VPD_OVERRIDE_CK_ENABLE>() )
            {
                ATTR_VPD_OVERRIDE_CK_Type l_override={0};
                assert(l_pMcsTarget->tryGetAttr<TARGETING::ATTR_VPD_OVERRIDE_CK>
                                         (l_override),
                       "platGetVPD: getAttr ATTR_VPD_OVERRIDE_CK failed");
                FAPI_DBG("platGetVPD: return CK override attr");
                memcpy(o_blob,l_override,sizeof(ATTR_VPD_OVERRIDE_CK_Type));
                break; //return with overriden keyword
            }
            l_mapKeyword = DVPD::CK;
            l_keywordEnum = DVPD::CK;
        }
        else
        {
            FAPI_ERR("platGetVPD: invalid type = %d",
                     io_vpd_info.iv_vpd_type);
            /*@
            * @errortype
            * @moduleid          fapi2::MOD_FAPI2_PLAT_GET_VPD
            * @reasoncode        fapi2::RC_INVALID_TYPE
            * @userdata1         Vpd type
            * @userdata2         HUID of MCS target
            * @devdesc           MR and MT types supported
            * @custdesc          Firmware Error
            */
            l_errl = new ERRORLOG::ErrlEntry(
                             ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                             fapi2::MOD_FAPI2_PLAT_GET_VPD,
                             fapi2::RC_BUFFER_TOO_SMALL,
                             io_vpd_info.iv_vpd_type,
                             TARGETING::get_huid(l_pMcsTarget),
                             true); //software callout
            l_rc.setPlatDataPtr(reinterpret_cast<void *> (l_errl));
            break; //return with error
        }

        // The PNOR::MEMD section potentially contains multiple copies of the
        // MEMD VPD.  This next section searches through the PNOR copy for a
        // valid copy if it exists and uses that instead of the DVPD section.

        //Read mapping keyword
        size_t l_buffSize = VPD_KEYWORD_SIZE;
        uint8_t * l_pMapping = new uint8_t[VPD_KEYWORD_SIZE]();

        // MEMD Processing
        PNOR::SectionInfo_t l_memd_info;
        l_errl = PNOR::getSectionInfo(PNOR::MEMD,l_memd_info);
        bool l_memd_found = false;
        MemdHeader_t l_header;

        if( l_errl )
        {
            FAPI_INF("platGetVPD: Optional MEMD section not found.");
            delete l_errl;
            l_errl = NULL;
        }
        else
        {
            do
            {
                const uint8_t * vaddr = reinterpret_cast<const uint8_t *>(
                                l_memd_info.vaddr);
                // Get and process the header
                memcpy(&l_header, vaddr, sizeof(l_header));

                size_t theSize = 0;

                // Get the VM keyword size
                l_errl = deviceRead( l_pMcsTarget, nullptr,
                                 theSize, DEVICE_DVPD_ADDRESS(
                                 DVPD::MEMD, DVPD::VM));

                if( l_errl )
                {
                    FAPI_ERR("platGetVPD: ERROR getting MEMD VM size");
                    l_rc.setPlatDataPtr(reinterpret_cast<void *>( l_errl ));
                    break;
                }

                // Get the VM keyword from the real VPD
                l_dvpd_vm = static_cast<uint8_t*>(malloc( theSize ));

                l_errl = deviceRead( l_pMcsTarget, l_dvpd_vm,
                                    theSize, DEVICE_DVPD_ADDRESS(
                                            DVPD::MEMD, DVPD::VM));
                if( l_errl )
                {
                    FAPI_ERR("platGetVPD: ERROR getting DVPD VM keyword");
                    l_rc.setPlatDataPtr(reinterpret_cast<void *>( l_errl ));
                    break;
                }
                l_full_dvpdVM = (reinterpret_cast<uint32_t*>(l_dvpd_vm));

                l_memd_found = find_memd_in_pnor(l_dvpd_vm,
                                l_header, l_pMcsTarget, theSize);
                uint64_t l_memd_offset_bytes = l_pMcsTarget->getAttr<
                        TARGETING::ATTR_MEMD_OFFSET>();

                if(l_memd_found)
                {
                    FAPI_INF("platGetVPD: Matching MEMD data was found in the "
                             "PNOR section, VM value is %llx. Reading in at "
                             "offset %llx",l_full_dvpdVM[0], l_memd_offset_bytes);
                    l_errl = deviceRead((TARGETING::Target *)l_pMcsTarget,
                                     l_pMapping,
                                     l_buffSize,
                                     DEVICE_MEMD_VPD_ADDRESS(MEMD_VPD::MEMD,
                                         l_mapKeyword) + l_memd_offset_bytes );
                    break;
                }else
                {
                    FAPI_INF("platGetVPD: Matching MEMD data was not found in "
                             "the PNOR section, DVPD VM value is %llx",
                             l_full_dvpdVM[0]);
                }

            }while(0);

        }
        if(l_errl)
        {
            FAPI_ERR("find_memd_in_pnor: ERROR getting the PNOR MEMD information");
            l_rc.setPlatDataPtr(reinterpret_cast<void *>(l_errl));
            break;
        }

        if( !(l_memd_found) )
        {
            FAPI_INF("platGetVPD: MEMD data was not found in the PNOR "
                     "section. Using EEPROM. VM value is: %llx",
                     l_full_dvpdVM[0]);

            l_errl = deviceRead((TARGETING::Target *)l_pMcsTarget,
                                l_pMapping,
                                l_buffSize,
                                DEVICE_DVPD_ADDRESS(DVPD::MEMD,
                                                l_mapKeyword));
            if (l_errl)
            {
                delete l_pMapping;
                l_pMapping = nullptr;
                FAPI_ERR("platGetVPD: ERROR reading mapping keyword");
                l_rc.setPlatDataPtr(reinterpret_cast<void *> (l_errl));
                break; //return with error
            }
        }

        // Find vpd keyword name based on VPDInfo
        FAPI_EXEC_HWP(l_rc,
                  p9_get_mem_vpd_keyword,
                  i_target,
                  io_vpd_info,
                  l_pMapping,
                  VPD_KEYWORD_SIZE,
                  l_keywordInfo);

        if (l_rc)
        {
            delete l_pMapping;
            FAPI_ERR("platGetVPD: ERROR returned from p9_get_mem_vpd_keyword");
            break; //return with error
        }
        FAPI_DBG("platGetVPD: keyword name =  %s",
                 l_keywordInfo.kwName);

        // Skip grabbing CK keyword data again and just use the index provided
        // by the hwp to get the specific data for this particular mcs target
        if (l_mapKeyword == DVPD::CK )
        {
            l_buffSize = l_keywordInfo.kwBlobSize;

            // Just a safety check so you don't copy out of bounds
            if (l_buffSize <= VPD_KEYWORD_SIZE)
            {
                // o_blob was already checked for nullptr
                // copy blob of l_buffSize past the 4 byte header
                // (each indexed section is l_buffSize bytes)
                memcpy(o_blob, l_pMapping + l_keywordInfo.kwBlobIndex, l_buffSize);
            }
            else
            {
                memcpy(o_blob,
                    l_pMapping + l_keywordInfo.kwBlobIndex,
                    VPD_KEYWORD_SIZE);
            }
            delete l_pMapping;
        }
        else
        {
            delete l_pMapping;

            //Convert keyword name to keyword enumeration.
            //ascii 0..9 runs from 0x30 to 0x39.
            //The conversion assumes the input is valid (0..9,A..Z)
            //and that the enumeration is in order and consecutive.
            if ( '0' == (l_keywordInfo.kwName[1] & 0xf0)) //it is a digit (0..9)
            {
                l_keywordEnum += (l_keywordInfo.kwName[1] - '0');
            }
            else //it is a char (A..Z)
            {
                l_keywordEnum += (l_keywordInfo.kwName[1] - 'A') + 10;
            }

            //Read vpd blob
            l_buffSize = l_keywordInfo.kwBlobSize;
            if(l_memd_found)
            {
                l_errl = deviceRead((TARGETING::Target *)l_pMcsTarget,
                                    o_blob,
                                    l_buffSize,
                                    DEVICE_MEMD_VPD_ADDRESS(MEMD_VPD::MEMD,
                                                l_keywordEnum));

            }
            else
            {
                l_errl = deviceRead((TARGETING::Target *)l_pMcsTarget,
                                o_blob,
                                l_buffSize,
                                DEVICE_DVPD_ADDRESS(DVPD::MEMD,
                                                    l_keywordEnum));
            }

            if (l_errl)
            {
                FAPI_ERR("platGetVPD: ERROR reading keyword");
                l_rc.setPlatDataPtr(reinterpret_cast<void *> (l_errl));
                break; //return with error
            }

            //Confirm all expected data was returned
            if (l_keywordInfo.kwBlobSize > l_buffSize)
            {
                FAPI_ERR("platGetVPD: insufficient vpd returned"
                         " for keyword %d;"
                         " %d returned, %d expected",
                         l_keywordEnum,
                         l_buffSize,
                         io_vpd_info.iv_size);
                /*@
                * @errortype
                * @moduleid          fapi2::MOD_FAPI2_PLAT_GET_VPD
                * @reasoncode        fapi2::RC_RETURNED_VPD_TOO_SMALL
                * @userdata1[0:31]   Returned vpd in bytes
                * @userdata1[32:64]  Expected number of vpd bytes
                * @userdata2         Keyword
                * @devdesc           Less than expected number of bytes returned.
                * @custdesc          Firmware Error
                */
                l_errl = new ERRORLOG::ErrlEntry(
                                 ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                 fapi2::MOD_FAPI2_PLAT_GET_VPD,
                                 fapi2::RC_RETURNED_VPD_TOO_SMALL,
                                 TWO_UINT32_TO_UINT64(
                                                      l_buffSize,
                                                      l_keywordInfo.kwBlobSize),
                                 l_keywordEnum);
                l_errl->addHwCallout( l_pMcsTarget,
                                 HWAS::SRCI_PRIORITY_LOW,
                                 HWAS::NO_DECONFIG,
                                 HWAS::GARD_NULL );

                l_rc.setPlatDataPtr(reinterpret_cast<void *> (l_errl));
                break; //return with error
            }
        }
    }
    while (0);

    free(l_dvpd_vm);
    l_dvpd_vm = nullptr;
    l_full_dvpdVM = nullptr;

    FAPI_DBG("platGetVPD: exit");

    return l_rc;
}

bool find_memd_in_pnor(uint8_t* i_eepromVM, MemdHeader_t i_header,
                       TARGETING::Target * i_target, size_t i_vm_size)
{
    errlHndl_t l_errl = nullptr;

    bool memdFoundInPnor = false;
    bool l_valid_memd = ((i_header.eyecatch == MEMD_VALID_HEADER) &
                    (i_header.header_version == MEMD_VALID_HEADER_VERSION) &
                    (i_header.memd_version == MEMD_VALID_MEMD_VERSION) );

    uint8_t* l_memd_vm = nullptr;
    uint32_t l_memd_iteration = 0;
    bool l_retValue = false;

    if(l_valid_memd && !memdFoundInPnor)
    {
        FAPI_INF("find_memd_in_pnor: MEMD is valid!");
        // Reset memd offset before we start the first iteration
        i_target->setAttr<TARGETING::ATTR_MEMD_OFFSET>(0);

        //do-while loop (needs to be executed at least once)
        //    while VM keywods don't match, and we haven't gone
        //    through all the MEMD copies
        do
        {
            // Get the VM keyword from the copy in PNOR
            l_memd_vm = static_cast<uint8_t*>(malloc( i_vm_size ));

            uint64_t l_memd_offset = i_target->getAttr<
                        TARGETING::ATTR_MEMD_OFFSET>();

            FAPI_INF("find_memd_in_pnor: attempting to read MEMD VM keyword");
            l_errl = deviceRead( i_target, l_memd_vm, i_vm_size,
                            DEVICE_MEMD_VPD_ADDRESS(
                                MEMD_VPD::MEMD, MEMD_VPD::VM));
            if(l_errl)
            {
                FAPI_ERR("find_memd_in_pnor: ERROR getting MEMD VM keyword");
                errlCommit(l_errl, FAPI2_COMP_ID);
                break;
            }

            assert(i_vm_size != 0);

            // Compare the last nibbles
            if( !((i_eepromVM[i_vm_size-1] & 0x0F) ==
                  (l_memd_vm[i_vm_size-1] & 0x0F)) )
            {
                // VM's don't match, we need to keep looking
                FAPI_INF("find_memd_in_pnor: DVPD and MEMD VM's last nibble"
                         " don't match: %llx and %llx",
                         (reinterpret_cast<uint32_t*>(i_eepromVM))[0],
                         (reinterpret_cast<uint32_t*>(l_memd_vm))[0] );
                i_target->setAttr<TARGETING::ATTR_MEMD_OFFSET
                        >(l_memd_offset +
                          (i_header.expected_size_kb * 1000));
            }
            else
            {
                FAPI_INF("find_memd_in_pnor: Matching MEMD data was found in "
                         "the PNOR section.  VM value is: %llx. Offset "
                         "is %llx, DVPD VM is %llx",
                         (reinterpret_cast<uint32_t*>(l_memd_vm))[0],
                         l_memd_offset,
                         (reinterpret_cast<uint32_t*>(i_eepromVM))[0]);
                memdFoundInPnor = true;
                l_retValue = true;
                break;
            }

            l_memd_iteration = l_memd_iteration + 1;
            free(l_memd_vm);
            l_memd_vm = nullptr;

        }while(l_memd_iteration < i_header.expected_num);

        free(l_memd_vm);
        l_memd_vm = nullptr;
    }

    return l_retValue;

}



} // namespace
OpenPOWER on IntegriCloud