summaryrefslogtreecommitdiffstats
path: root/src/usr/hdat/hdathostslcadata.C
blob: 4599eb7df4ae70fc0a4d018e50bc22626d120dd0 (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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/hdat/hdathostslcadata.C $                             */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016,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                                                     */

#include <devicefw/userif.H>
#include <hdat/hdat.H>
#include <targeting/common/target.H>
#include "hdatutil.H"
#include "hdathostslcadata.H"
#include <sys/mm.h>
#include <sys/mmio.h>
#include <vpd/vpd_if.H>

#ifdef HDAT_DEBUG
#include <iostream>
#endif
#include <targeting/common/util.H>
#include <string.h>
#include <util/align.H>

using namespace TARGETING;
using namespace std;
using namespace VPD;

namespace HDAT
{

extern trace_desc_t *g_trac_hdat;


fru_id_rid_t g_fruIDRidMap[] =
{
    {0x0800 , "BP"},
    {0x1E00 , "EV"},
    {0x1C00 , "SV"},
    {0x1000 , "PF"},
    {0xD000 , "MS"},
    {0x3600 , "PI"},
    {0x0200 , "SP"},
    {0x3100 , "PS"},
    {0x3A00 , "AM"},
    {0x2900 , "CU"},
    {0x2800 , "CE"},
    {0x0300 , "OP"},
    {0x3900 , "RG"},
    {0xA100 , "SA"},
    {0xA200 , "EI"},
    {0xA300 , "EF"},
    {0x2A00 , "CS"},
    {0xD000 , "MS"},
    {0xA000 , "VV"},
    {0x1400 , "RI"},
};


/**
 * @brief This helper routine returns VPD Collected status
 *
 * @pre None
 *
 * @post None
 *
 * @param i_Target   - input parameter - Target of parent FRU
 *
 * @return True  : VPD is collected
 *         False : VPD is not collected
 *
 * @retval HDAT_OTHER_COMP_ERROR
 */
bool getVPDCollectedStatus(TARGETING::Target *i_Target)
{
    bool l_vpdCollectedStatus = true;
//@TODO: RTC 149382: Method to get VPD collected status for Targets

    if((i_Target->getAttr<ATTR_TYPE>() != TYPE_PCI) &&
              (i_Target->getAttr<ATTR_TYPE>() != TYPE_SYS) &&
              (i_Target->getAttr<ATTR_TYPE>() != TYPE_BMC))
    {
        l_vpdCollectedStatus = pvpdPresent(i_Target);
    }
    return l_vpdCollectedStatus;
}

/**
 * @brief This helper routine populates ordinal ids for Procs
 *        and cores
 *
 * @pre None
 *
 * @post None
 *
 * @param None
 *
 * @return None
 *
 * @retval HDAT_OTHER_COMP_ERROR
 */

void hdatPopulateOrdinalId()
{
    TARGETING::Target *l_pSysTarget = NULL;
    (void) TARGETING::targetService().getTopLevelTarget(l_pSysTarget);

    if(l_pSysTarget == NULL)
    {
      HDAT_ERR("hdatGetSystemParamters::Top Level Target not found");
      assert(l_pSysTarget != NULL);
    }

    TARGETING::PredicateCTM l_procFilter(TARGETING::CLASS_CHIP,
                                             TARGETING::TYPE_PROC);

    TARGETING::PredicateHwas l_predHwas;
    l_predHwas.present(true);

    TARGETING::PredicatePostfixExpr l_presentProc;
    l_presentProc.push(&l_procFilter).push(&l_predHwas).And();

    TARGETING::TargetHandleList l_procList;

    //Get all Procs in the system
    TARGETING::targetService().
                  getAssociated(l_procList, l_pSysTarget,
                          TARGETING::TargetService::CHILD,
                          TARGETING::TargetService::ALL, &l_presentProc);

    uint32_t l_procOrdinalId = 0;
    for (TargetHandleList::const_iterator pTarget_it = l_procList.begin();
                pTarget_it != l_procList.end();
                ++pTarget_it)
    {
        TargetHandle_t l_procTarget  = *pTarget_it;
        l_procTarget->setAttr<TARGETING::ATTR_ORDINAL_ID>(l_procOrdinalId++);

        TARGETING::PredicateCTM l_coreFilter(TARGETING::CLASS_UNIT,
                                             TARGETING::TYPE_CORE);

        TARGETING::PredicatePostfixExpr l_presentCore;
        l_presentCore.push(&l_coreFilter).push(&l_predHwas).And();

        TARGETING::TargetHandleList l_coreList;

        //Get all Core in the Proc
        TARGETING::targetService().
                  getAssociated(l_coreList, l_procTarget,
                          TARGETING::TargetService::CHILD,
                          TARGETING::TargetService::ALL, &l_presentCore);

        uint32_t l_coreOrdinalId = 0;
        for (TargetHandleList::const_iterator pcoreTarget_it =
                                                      l_coreList.begin();
                pcoreTarget_it != l_coreList.end();
                ++pcoreTarget_it)
        {
            TargetHandle_t l_coreTarget  = *pcoreTarget_it;
            l_coreTarget->setAttr<TARGETING::ATTR_ORDINAL_ID>
                                                      (l_coreOrdinalId++);
        }
         HDAT_DBG("Added Ordinal IDs for Core : %d",l_coreOrdinalId);
    }
     HDAT_DBG("Added Ordinal IDs Proc: %d ",l_procOrdinalId);
}

/**
 * @brief This routine adds a new SLCA entry for specified target
 *
 * @pre None
 *
 * @post None
 *
 * @param i_Target   - input parameter - Target for which slca entry
 *                                       is added
 *        i_frutype  - input parameter - FRU Type of the Target
 *        i_slcaParentIndex - input parameter - Index of parent FRU
 *        i_LocCodePrefix - input parameter - Location code prefix
 *        o_hdatslca - output parameter - Vector containing slca entries
 *                                        for DIMMs
 *
 * @return Index of Last SLCA entry added
 *
 * @retval HDAT_OTHER_COMP_ERROR
 */
uint16_t hdatAddSLCAEntry( TARGETING::Target *i_Target,
                           HDAT_FRUType_t i_frutype,
                           uint16_t i_slcaparentindex,
                           char *i_LocCodePrefix,
                           vector<HDAT_slcaEntry_t> &o_hdatslca )
{
    //Values for the SLCA's collected and installed fields
    #define HDAT_SLCA_COLLECTED      2
    #define HDAT_SLCA_NOT_COLLECTED  3
    #define HDAT_SLCA_NO_PD_INFO     1
    #define HDAT_SLCA_INSTALLED      2
    #define HDAT_SLCA_NOT_INSTALLED  3
    HDAT_slcaEntry_t l_hdatslcaentry;

    l_hdatslcaentry.fru_index             = o_hdatslca.size();
    l_hdatslcaentry.fru_rid               = g_fruIDRidMap[i_frutype].fru_rid++;

    strncpy(l_hdatslcaentry.fru_id,
                            (const char *)g_fruIDRidMap[i_frutype].fru_id,
                                           sizeof(l_hdatslcaentry.fru_id));

    l_hdatslcaentry.parent_index = i_slcaparentindex;
    l_hdatslcaentry.first_child_rid       = 0xFFFF;
    l_hdatslcaentry.max_location_code_len = 64;

    memset(l_hdatslcaentry.location_code , 0 ,
                                         l_hdatslcaentry.max_location_code_len);

    hdatGetLocationCode(i_Target,i_LocCodePrefix,l_hdatslcaentry.location_code);
    uint32_t l_length = strlen(l_hdatslcaentry.location_code);
    l_hdatslcaentry.location_code[l_length]='\0';
    l_hdatslcaentry.actual_location_code_len = l_length + 1;

    l_hdatslcaentry.number_of_children      = 0x0;
    l_hdatslcaentry.first_child_index       = 0xFFFF;
    l_hdatslcaentry.first_redundant_index   = 0xFFFF;
    l_hdatslcaentry.number_redundant_copies = 0x0;
    l_hdatslcaentry.number_of_children_2B   = 0x0;

    l_hdatslcaentry.installed = HDAT_SLCA_INSTALLED;
    if(getVPDCollectedStatus(i_Target))
    {
        l_hdatslcaentry.collected = HDAT_SLCA_COLLECTED;
    }
    else
    {
        l_hdatslcaentry.collected = HDAT_SLCA_NOT_COLLECTED;
    }

    o_hdatslca.push_back(l_hdatslcaentry);

    if(i_frutype != HDAT_SLCA_FRU_TYPE_EV && i_frutype != HDAT_SLCA_FRU_TYPE_VV)
    {
        if(!i_Target->trySetAttr<TARGETING::ATTR_SLCA_INDEX>
                                                        (o_hdatslca.size() - 1))
        {
            HDAT_ERR("Error while setting SLCA_INDEX attribute for frutype: %d",
                                                                   i_frutype);
        }
        if(!i_Target->trySetAttr<TARGETING::ATTR_SLCA_RID>
                                                     (l_hdatslcaentry.fru_rid))
        {
            HDAT_ERR("Error while setting SLCA_RID attribute for frutype: %d",
                                                                   i_frutype);
        }
    }
    HDAT_DBG("Added SLCA Entry  FI : %s loc_code : %s",
                                              g_fruIDRidMap[i_frutype].fru_id,
                                              l_hdatslcaentry.location_code);
    return(o_hdatslca.size() - 1);
}

/**
 * @brief This routine adds Nodes and its children to SLCA table
 *
 * @pre None
 *
 * @post None
 *
 * @param i_Target   - input parameter - Target of Node FRU being added
 *        i_slcaParentIndex - input parameter - Index of parent FRU
 *        i_LocCodePrefix - input parameter - Initial part of location code
 *        o_hdatslca - output parameter - Vector containing slca entries
 *
 * @return Number of DIMMs added
 *
 */
static void hdatAddNodeToSLCATable(TARGETING::Target *i_Target,
                                 uint16_t i_slcaParentIndex,
                                 char *i_LocCodePrefix,
                         vector<HDAT_slcaEntry_t> &o_hdatslca)
{
    uint16_t l_slcaBPIndex = 0;
    char     l_nodeLocCode[64] = {0};
    uint16_t l_slcaEntryIndex = 0;

    l_slcaEntryIndex = hdatAddSLCAEntry(i_Target, HDAT_SLCA_FRU_TYPE_BP,
                                        i_slcaParentIndex,
                                        i_LocCodePrefix,o_hdatslca);

    if((o_hdatslca[i_slcaParentIndex].first_child_index == 0xFFFF) &&
                                                         (l_slcaEntryIndex))
    {
        o_hdatslca[i_slcaParentIndex].first_child_index = l_slcaEntryIndex;

        o_hdatslca[i_slcaParentIndex].first_child_rid =
                                       o_hdatslca[l_slcaEntryIndex].fru_rid;

        o_hdatslca[i_slcaParentIndex].number_of_children++;
        o_hdatslca[i_slcaParentIndex].number_of_children_2B++;

    }

    l_slcaBPIndex = l_slcaEntryIndex;
    hdatGetLocationCode(i_Target, i_LocCodePrefix, l_nodeLocCode);

    TARGETING::PredicateHwas l_predHwas;
    l_predHwas.present(true);

    TARGETING::PredicateCTM l_procFilter(TARGETING::CLASS_CHIP,
                                             TARGETING::TYPE_PROC);

    TARGETING::PredicateCTM l_memFilter(TARGETING::CLASS_CHIP,
                                             TARGETING::TYPE_MEMBUF);

    TARGETING::PredicateCTM l_pciFilter(TARGETING::CLASS_UNIT,
                                             TARGETING::TYPE_PCI);

    TARGETING::PredicateCTM l_psFilter(TARGETING::CLASS_UNIT,
                                             TARGETING::TYPE_PS);

    TARGETING::PredicateCTM l_vrmFilter(TARGETING::CLASS_UNIT,
                                             TARGETING::TYPE_VRM);

    TARGETING::PredicateCTM l_fanFilter(TARGETING::CLASS_UNIT,
                                             TARGETING::TYPE_FAN);

    TARGETING::PredicateCTM l_uartFilter(TARGETING::CLASS_UNIT,
                                             TARGETING::TYPE_UART);

    TARGETING::PredicateCTM l_usbFilter(TARGETING::CLASS_UNIT,
                                             TARGETING::TYPE_USB);

    TARGETING::PredicateCTM l_ethFilter(TARGETING::CLASS_UNIT,
                                             TARGETING::TYPE_ETH);

    TARGETING::PredicateCTM l_dimmFilter(TARGETING::CLASS_LOGICAL_CARD,
                                             TARGETING::TYPE_DIMM);

    TARGETING::PredicatePostfixExpr l_presentChildren;
    l_presentChildren.push(&l_procFilter).push(&l_memFilter).Or().
                     push(&l_pciFilter).Or().push(&l_psFilter).Or().
                     push(&l_fanFilter).Or().push(&l_uartFilter).Or().
                     push(&l_usbFilter).Or().push(&l_ethFilter).Or().
                     push(&l_vrmFilter).Or().push(&l_dimmFilter).Or().
                     push(&l_predHwas).And();

    TARGETING::TargetHandleList l_childList;

    //Get all children of this node
    TARGETING::targetService().
                  getAssociated(l_childList, i_Target,
                          TARGETING::TargetService::CHILD,
                          TARGETING::TargetService::ALL, &l_presentChildren);

    for (TargetHandleList::const_iterator pTarget_it = l_childList.begin();
                pTarget_it != l_childList.end();
                ++pTarget_it)
    {
        TargetHandle_t l_childTarget  = *pTarget_it;
        TARGETING::ATTR_TYPE_type l_type;
        HDAT_FRUType_t l_hdatFRUType = HDAT_SLCA_FRU_TYPE_UNKNOWN;
        l_slcaEntryIndex = 0;

        if(l_childTarget->tryGetAttr<ATTR_TYPE>(l_type))
        {
            switch(l_type)
            {
                case TYPE_PROC:
                    l_hdatFRUType = HDAT_SLCA_FRU_TYPE_PROC;
                break;

                case TYPE_MEMBUF:
                {
                    ATTR_FRU_ID_type l_childFRUId;
                    ATTR_FRU_ID_type l_encFRUId;
                    TARGETING::TargetHandleList targetList;
                    targetList.clear();
                    getParentAffinityTargets(targetList,l_childTarget,
                               TARGETING::CLASS_ENC,TARGETING::TYPE_NODE);
                    if(!targetList.empty())
                    {
                        TARGETING::Target* l_pNodeTarget = targetList[0];
                        l_encFRUId = l_pNodeTarget->getAttr<ATTR_FRU_ID>();
                        l_childFRUId = l_childTarget->getAttr<ATTR_FRU_ID>();
                        if(l_encFRUId != l_childFRUId)
                        {
                            l_hdatFRUType = HDAT_SLCA_FRU_TYPE_RI;
                        }
                    }
                    else
                    {
                        //target list is empty for type membuf then FRU type
                        //would be set as HDAT_SLCA_FRU_TYPE_UNKNOWN
                        HDAT_ERR("Empty list returned while querying"
                                                  "for parents of membuf");
                    }
                }
                break;

                case TYPE_DIMM:
                    l_hdatFRUType = HDAT_SLCA_FRU_TYPE_DIMM;
                break;

                case TYPE_PCI:
                    l_hdatFRUType = HDAT_SLCA_FRU_TYPE_IOP;
                break;

                case TYPE_PS:
                    l_hdatFRUType = HDAT_SLCA_FRU_TYPE_PS;
                break;

                case TYPE_VRM:
                    l_hdatFRUType = HDAT_SLCA_FRU_TYPE_RG;
                break;

                case TYPE_FAN:
                    l_hdatFRUType = HDAT_SLCA_FRU_TYPE_AM;
                break;

                case TYPE_USB:
                    l_hdatFRUType = HDAT_SLCA_FRU_TYPE_CU;
                break;

                case TYPE_ETH:
                    l_hdatFRUType = HDAT_SLCA_FRU_TYPE_CE;
                break;

                case TYPE_UART:
                    l_hdatFRUType = HDAT_SLCA_FRU_TYPE_CS;
                break;

#ifdef BACKPLANE_EXTENSION_ENABLED
                case TYPE_BX:
                    l_hdatFRUType = HDAT_SLCA_FRU_TYPE_BX;
                    hdatAddNodeToSLCATable(l_childTarget, l_slcaBPIndex,
                                l_nodeLocCode, o_hdatslca);
                break;
#endif

                default:
                    l_hdatFRUType = HDAT_SLCA_FRU_TYPE_UNKNOWN;
                    break;
            }

            if(l_hdatFRUType != HDAT_SLCA_FRU_TYPE_UNKNOWN)
            {
                l_slcaEntryIndex = hdatAddSLCAEntry(l_childTarget,
                                                     l_hdatFRUType,
                                                     l_slcaBPIndex,
                                                     l_nodeLocCode,o_hdatslca);

                o_hdatslca[l_slcaBPIndex].number_of_children++;

                o_hdatslca[l_slcaBPIndex].number_of_children_2B++;

                if((o_hdatslca[l_slcaBPIndex].first_child_index == 0xFFFF) &&
                                                         (l_slcaEntryIndex))
                {
                    o_hdatslca[l_slcaBPIndex].first_child_index =
                                                           l_slcaEntryIndex;

                    o_hdatslca[l_slcaBPIndex].first_child_rid =
                                       o_hdatslca[l_slcaEntryIndex].fru_rid;
                }
            }
        }
        else
        {
            HDAT_ERR("Error reading ATTR_TYPE attribute");
        }
    }
}

/**
 * @brief This routine adds Service Processors  to SLCA table
 *
 * @pre None
 *
 * @post None
 *
 * @param i_Target   - input parameter - Target of SP FRU
 *        i_slcaParentIndex - input parameter - Index of parent FRU
 *        o_hdatslca - output parameter - Vector containing slca entries
 *                                        for Service Processors
 *
 * @return Number of Service Processor FRUs added
 *
 * @retval HDAT_OTHER_COMP_ERROR
 */
static void hdatAddSPToSLCATable(TARGETING::Target *i_Target,
                              uint16_t i_slcaParentIndex,
                              char *i_LocCodePrefix,
                      vector<HDAT_slcaEntry_t> &o_hdatslca)
{
    TARGETING::PredicateCTM l_spPredicate(TARGETING::CLASS_SP,
                                             TARGETING::TYPE_BMC,
                                             TARGETING::MODEL_AST2500);
    TARGETING::PredicateHwas l_predHwas;
    l_predHwas.present(true);

    TARGETING::PredicatePostfixExpr l_presentSP;
    l_presentSP.push(&l_spPredicate).push(&l_predHwas).And();

    //Get all Service processors in the system
    TARGETING::TargetRangeFilter* l_spFilter= new TARGETING::TargetRangeFilter(
                                        TARGETING::targetService().begin(),
                                        TARGETING::targetService().end(),
                                        &l_presentSP);

    // if the predicate is empty, then use old predicate
    if (!(*l_spFilter))
    {
       delete l_spFilter;

       TARGETING::PredicateCTM l_spPredicate(TARGETING::CLASS_CHIP,
                                             TARGETING::TYPE_SP,
                                             TARGETING::MODEL_BMC);

       TARGETING::PredicatePostfixExpr l_presentSP;
       l_presentSP.push(&l_spPredicate).push(&l_predHwas).And();

       l_spFilter = new TARGETING::TargetRangeFilter(
                                     TARGETING::targetService().begin(),
                                     TARGETING::targetService().end(),
                                     &l_presentSP);
    }

    for (;(*l_spFilter);++(*l_spFilter))
    {
        TARGETING::Target *l_spTarget = (*(*l_spFilter));

        hdatAddSLCAEntry(l_spTarget, HDAT_SLCA_FRU_TYPE_SP, i_slcaParentIndex,
                                                  i_LocCodePrefix,o_hdatslca);

        o_hdatslca[i_slcaParentIndex].number_of_children++;
        o_hdatslca[i_slcaParentIndex].number_of_children_2B++;

        HDAT_DBG("Added Service Processor to SLCA");
    }

    delete l_spFilter;
}

/**
 * @brief This routine adds op-panel SLCA entry
 *
 * @pre None
 *
 * @post None
 *
 * @param i_Target   - input parameter - Target of panel FRU
 *        i_slcaParentIndex - input parameter - Index of parent FRU
 *        i_LocCodePrefix - input parameter - Loc Code Prefix
 *        o_hdatslca - output parameter - Vector containing slca entries
 *
 * @return A null error log handle if successful, else the return code pointed
 *         to by errlHndl_t contains one of:
 *
 * @retval HDAT_OTHER_COMP_ERROR
 */
static void hdatAddOpPanelToSLCATable(TARGETING::Target *i_Target,
                                   uint16_t i_slcaParentIndex,
                                   char *i_LocCodePrefix,
                                   vector<HDAT_slcaEntry_t> &o_hdatslca)
{
    TARGETING::PredicateCTM l_opPredicate(TARGETING::CLASS_UNIT,
                                             TARGETING::TYPE_PANEL);

    TARGETING::PredicateHwas l_predHwas;
    l_predHwas.present(true);

    TARGETING::PredicatePostfixExpr l_presentPanel;
    l_presentPanel.push(&l_opPredicate).push(&l_predHwas).And();

    //Get all op-panels in the system
    TARGETING::TargetRangeFilter l_opFilter(
                                        TARGETING::targetService().begin(),
                                        TARGETING::targetService().end(),
                                        &l_presentPanel);

    for (;l_opFilter;++l_opFilter)
    {
        TARGETING::Target *l_opTarget = (*l_opFilter);

        hdatAddSLCAEntry(l_opTarget, HDAT_SLCA_FRU_TYPE_OP, i_slcaParentIndex,
                                                  i_LocCodePrefix,o_hdatslca);

        o_hdatslca[i_slcaParentIndex].number_of_children++;
        o_hdatslca[i_slcaParentIndex].number_of_children_2B++;

        HDAT_DBG("Added OpPanel to SLCA");
    }
}

/**
 * @brief This routine constructs SLCA table for all FRUs
 *
 * @pre None
 *
 * @post None
 *
 * @param o_hdatslcaentries - output parameter - Vector containing slca entries
 *
 * @return A null error log handle if successful, else the return code pointed
 *         to by errlHndl_t contains one of:
 *
 * @retval HDAT_OTHER_COMP_ERROR
 */
errlHndl_t hdatConstructslcaTable(std::vector<HDAT_slcaEntry_t>
                                                          &o_hdatslcaentries)
{

    TARGETING::Target *l_pSystemTarget = NULL;
    (void) TARGETING::targetService().getTopLevelTarget(l_pSystemTarget);

    if(l_pSystemTarget == NULL)
    {
        HDAT_ERR("Error in getting Top Level Target");
        assert(l_pSystemTarget != NULL);
    }

    char l_locCode[64]={0};
    hdatGetLocationCodePrefix(l_locCode);

    uint16_t l_slcaVVIndex = hdatAddSLCAEntry(l_pSystemTarget,
                                              HDAT_SLCA_FRU_TYPE_VV,0,
                                              l_locCode,
                                              o_hdatslcaentries);

    uint16_t l_slcaSVIndex = hdatAddSLCAEntry(l_pSystemTarget,
                                            HDAT_SLCA_FRU_TYPE_SV,l_slcaVVIndex,
                                             l_locCode,
                                             o_hdatslcaentries);

    if((o_hdatslcaentries[0].first_child_index == 0xFFFF) &&
                                                         (l_slcaSVIndex))
    {
        o_hdatslcaentries[0].first_child_index = l_slcaSVIndex;

        o_hdatslcaentries[0].first_child_rid =
                                       o_hdatslcaentries[l_slcaSVIndex].fru_rid;

        o_hdatslcaentries[0].number_of_children++;
        o_hdatslcaentries[0].number_of_children_2B++;

    }

    TARGETING::PredicateCTM l_nodePredicate(TARGETING::CLASS_ENC,
                                             TARGETING::TYPE_NODE);
    TARGETING::PredicateHwas l_predHwas;
    l_predHwas.present(true);

    TARGETING::PredicatePostfixExpr l_presentNode;
    l_presentNode.push(&l_nodePredicate).push(&l_predHwas).And();

    //Get all Nodes
    TARGETING::TargetRangeFilter l_nodeFilter(
                                        TARGETING::targetService().begin(),
                                        TARGETING::targetService().end(),
                                        &l_presentNode);

    TARGETING::Target *l_nodeTarget = (*l_nodeFilter);
    if(l_nodeTarget != NULL)
    {
        l_nodeTarget->setAttr<TARGETING::ATTR_ORDINAL_ID>(0);

        //Add EV here and make SP, BP and OP as children of EV
        uint16_t l_slcaEVIndex = hdatAddSLCAEntry(l_pSystemTarget,
                                           HDAT_SLCA_FRU_TYPE_EV,l_slcaVVIndex,
                                           l_locCode,
                                           o_hdatslcaentries);

        o_hdatslcaentries[l_slcaEVIndex].first_child_index = 0xFFFF;
        o_hdatslcaentries[l_slcaEVIndex].first_child_rid   = 0xFFFF;

        o_hdatslcaentries[l_slcaVVIndex].number_of_children++;
        o_hdatslcaentries[l_slcaVVIndex].number_of_children_2B++;

        hdatAddNodeToSLCATable(l_nodeTarget, l_slcaEVIndex, l_locCode,
                                                    o_hdatslcaentries);

        //Add Service Processor FRUs to SLCA table
        hdatAddSPToSLCATable(l_nodeTarget,l_slcaEVIndex,l_locCode,
                                                          o_hdatslcaentries);

        //Add Op-Panel  FRUs to SLCA table
        hdatAddOpPanelToSLCATable(l_nodeTarget,l_slcaEVIndex,l_locCode,
                                                             o_hdatslcaentries);

        hdatPopulateOrdinalId();
    }
    else
    {
        HDAT_ERR("Error retrieving nodes. There must be alteast one node");
        assert(l_nodeTarget != NULL);
    }
    return NULL;

}

/**
 * @brief This routine sets SLCA structure header parameters
 *
 * @pre None
 *
 * @post None
 *
 * @param o_slcaStruct - output parameter - Structure containing SLCA header
 *        i_nrOfSLCAEntries - input parameter - Number of SLCA entries
 *
 * @return A null error log handle if successful, else the return code pointed
 *         to by errlHndl_t contains one of:
 *
 * @retval HDAT_OTHER_COMP_ERROR
 */
static errlHndl_t hdatSetSLCAStructHdrs(hdatSLCAStruct_t &o_slcaStruct,
                                       uint32_t i_nrOfSLCAEntries)
{
    errlHndl_t l_errlHndl = NULL;

    uint32_t l_hdatslcastructsize = i_nrOfSLCAEntries *
                                                sizeof(HDAT_slcaEntry_t);

    o_slcaStruct.hdatHdr.hdatStructId       = HDAT_HDIF_STRUCT_ID;
    o_slcaStruct.hdatHdr.hdatInstance       = 0;
    o_slcaStruct.hdatHdr.hdatVersion        = HDAT_SLCA_STRUCT_VERSION;
    o_slcaStruct.hdatHdr.hdatSize           = sizeof(hdatSLCAStruct_t) +
                                                        l_hdatslcastructsize;
    o_slcaStruct.hdatHdr.hdatHdrSize        = sizeof(hdatHDIF_t);
    o_slcaStruct.hdatHdr.hdatDataPtrOffset  = sizeof(hdatHDIF_t);
    o_slcaStruct.hdatHdr.hdatDataPtrCnt     = 1;
    o_slcaStruct.hdatHdr.hdatChildStrCnt    = 0;
    o_slcaStruct.hdatHdr.hdatChildStrOffset = 0;

    memcpy(o_slcaStruct.hdatHdr.hdatStructName, HDAT_SLCA_STRUCT_NAME,
                                sizeof(o_slcaStruct.hdatHdr.hdatStructName));

    o_slcaStruct.hdatSLCAIntData[0].hdatOffset =
                                sizeof(hdatHDIF_t)+ sizeof(hdatHDIFDataHdr_t)
                                + sizeof(o_slcaStruct.hdatPadding);

    o_slcaStruct.hdatSLCAIntData[0].hdatSize =
                             sizeof(hdatSLCAArrayHdr_t) + l_hdatslcastructsize;

    memset(o_slcaStruct.hdatPadding, 0 , sizeof(o_slcaStruct.hdatPadding));

    o_slcaStruct.hdatSLCAArrayHdr.hdatOffsetToSLCAArray =
                                            sizeof(hdatSLCAArrayHdr_t);

    o_slcaStruct.hdatSLCAArrayHdr.hdatActualNrEntries = i_nrOfSLCAEntries;

    o_slcaStruct.hdatSLCAArrayHdr.hdatSizeOfEntryAllotted =
                                            sizeof(HDAT_slcaEntry_t);

    o_slcaStruct.hdatSLCAArrayHdr.hdatActualSizeOfEntry =
                                            sizeof(HDAT_slcaEntry_t);

    return l_errlHndl;
}

/**
 * @brief This routine builds the SLCA structure as per HDAT specifications
 *
 * @pre None
 *
 * @post None
 *
 * @param i_msAddr - Mainstore address where SLCA structure is loaded
 *        o_hdatslcaCount - output parameter - Number of SLCA structures
 *        o_hdatslcaSize - output paramster - Size of SLCA created
 *
 * @return errlHndl_t - Error Handle
 *
 * @retval HDAT_OTHER_COMP_ERROR
 */
errlHndl_t hdatBuildSLCA(const HDAT::hdatMsAddr_t &i_msAddr,
                               uint32_t &o_hdatslcaCount,
                               uint32_t &o_hdatslcaSize)

{
    std::vector<HDAT_slcaEntry_t> l_hdatslcaentries;
    hdatSLCAStruct_t l_hdatslcastruct;
    errlHndl_t l_errl = NULL;

    hdatPopulateMTMAndSerialNumber();
    hdatConstructslcaTable(l_hdatslcaentries);

    uint32_t l_hdatslcastructsize = l_hdatslcaentries.size() *
                                                    sizeof(HDAT_slcaEntry_t);

    uint64_t l_base_addr = ((uint64_t) i_msAddr.hi << 32) | i_msAddr.lo;

    //Set SLCA Headers
    hdatSetSLCAStructHdrs(l_hdatslcastruct, l_hdatslcaentries.size());

    // Allocate space for SLCA Header
    void *l_virt_addr_hdr = mm_block_map(reinterpret_cast<void*>
                                               (ALIGN_PAGE_DOWN(l_base_addr)),
                                         (ALIGN_PAGE(sizeof(hdatSLCAStruct_t) +
                                            l_hdatslcastructsize) + PAGESIZE));
    l_virt_addr_hdr = reinterpret_cast<void *>(
                    reinterpret_cast<uint64_t>(l_virt_addr_hdr) +
                    (l_base_addr - ALIGN_PAGE_DOWN(l_base_addr)));

    HDAT_DBG("SLCA hdr addr 0x%016llX SLCA hdr size %d",
                   (uint64_t)l_virt_addr_hdr, sizeof(hdatSLCAStruct_t));

    memcpy(reinterpret_cast<hdatSLCAStruct_t *>(l_virt_addr_hdr),
                                 &l_hdatslcastruct, sizeof(hdatSLCAStruct_t));

    HDAT_slcaEntry_t *l_hdatSLCA = reinterpret_cast<HDAT_slcaEntry_t *>
                       ((uint64_t)l_virt_addr_hdr + sizeof(hdatSLCAStruct_t));

    HDAT_DBG("HDAT SLCA addr 0x%016llX SLCA struct size %d",
                  (uint64_t) l_base_addr, l_hdatslcastructsize);

    HDAT_DBG("HDAT SLCA addr 0x%016llX virtual addr 0x%016llX",
                  (uint64_t) l_hdatSLCA, (uint64_t)l_virt_addr_hdr);

    std::copy(l_hdatslcaentries.begin(), l_hdatslcaentries.end(), l_hdatSLCA);

    HDAT_DBG("HDAT:: Loaded SLCA structures: Size: 0x%X",
                                               sizeof(hdatSLCAStruct_t) +
                                               l_hdatslcastructsize);

    o_hdatslcaSize = sizeof(hdatSLCAStruct_t) + l_hdatslcastructsize;
    o_hdatslcaCount = 1;

    int rc = mm_block_unmap(l_virt_addr_hdr);

    if( rc != 0)
    {
        errlHndl_t l_errl = NULL;
        /*@
         * @errortype
         * @moduleid         HDAT::MOD_SLCA_DESTRUCTOR
         * @reasoncode       HDAT::RC_DEV_MAP_FAIL
         * @devdesc          Unmap a mapped region failed
         * @custdesc         Firmware encountered an internal error.
        */
        hdatBldErrLog(l_errl,
                MOD_PCIA_DESTRUCTOR,
                RC_DEV_MAP_FAIL,
                (uint32_t)((uint64_t)l_virt_addr_hdr >> 32),
                (uint32_t)((uint64_t)l_virt_addr_hdr),0,0,
                ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                HDAT_VERSION1, false);
    }

    return l_errl;
}

/**
 * @brief This routine copies the SLCA from a source address to a destination
 *        address
 *
 * @pre None
 *
 * @post None
 *
 * @param i_msAddrSource - input parameter - Source address of SLCA
 *        i_msAddrDest   - input parameter - Destination address of SLCA
 *        i_slcaSize     - input parameter - Size of SLCA to be copied
 *
 * @return None
 *
 * @retval HDAT_OTHER_COMP_ERROR
 */
void hdatMoveSLCA(const HDAT::hdatMsAddr_t &i_msAddrSource,
                   const HDAT::hdatMsAddr_t &i_msAddrDest,
                   uint32_t i_slcaSize)
{

    uint64_t l_base_addr_source = ((uint64_t) i_msAddrSource.hi << 32) |
                                                             i_msAddrSource.lo;
    uint64_t l_base_addr_dest   = ((uint64_t) i_msAddrDest.hi << 32) |
                                                             i_msAddrDest.lo;

    HDAT_DBG("Move SLCA from 0x%016llX to 0x%016llX with size  %d",
                  (uint64_t) l_base_addr_source, (uint64_t)l_base_addr_dest,
                                                 i_slcaSize);
    // Allocate space for SLCA
    void *l_virt_addr_source = mm_block_map(reinterpret_cast<void*>
                                          (ALIGN_PAGE_DOWN(l_base_addr_source)),
                                          (ALIGN_PAGE(i_slcaSize)+PAGESIZE));

    l_virt_addr_source = reinterpret_cast<void *>(
                    reinterpret_cast<uint64_t>(l_virt_addr_source) +
                    (l_base_addr_source - ALIGN_PAGE_DOWN(l_base_addr_source)));

    // Allocate space for SLCA
    void *l_virt_addr_dest = mm_block_map(reinterpret_cast<void*>
                                          (ALIGN_PAGE_DOWN(l_base_addr_dest)),
                                          (ALIGN_PAGE(i_slcaSize) + PAGESIZE));

    l_virt_addr_dest = reinterpret_cast<void *>(
                    reinterpret_cast<uint64_t>(l_virt_addr_dest) +
                    (l_base_addr_dest - ALIGN_PAGE_DOWN(l_base_addr_dest)));

    memcpy(l_virt_addr_dest , reinterpret_cast<void*>(l_virt_addr_source),
                                                                   i_slcaSize);

    mm_block_unmap(l_virt_addr_source);
    mm_block_unmap(l_virt_addr_dest);
}


}
OpenPOWER on IntegriCloud