summaryrefslogtreecommitdiffstats
path: root/src/usr/diag/prdf/common/framework/service/prdfTargetServices.C
blob: f0341d83083d7545d01c66b4c9b2cbd3384ecc47 (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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/diag/prdf/common/framework/service/prdfTargetServices.C $ */
/*                                                                        */
/* IBM CONFIDENTIAL                                                       */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2012,2013              */
/*                                                                        */
/* 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                                                     */

/**
 * @file prdfTargetServices.C
 * @brief PRD wrapper of targetting code
 */

//------------------------------------------------------------------------------
//  Includes
//------------------------------------------------------------------------------

#include <prdfTargetServices.H>

#include <prdfGlobal.H>
#include <prdfErrlUtil.H>
#include <prdfTrace.H>
#include <algorithm>
#include <fapi.H>
#include <targeting/common/targetservice.H>

using namespace TARGETING;

//------------------------------------------------------------------------------

namespace PRDF
{

namespace PlatServices
{

//##############################################################################
//##
//##                 Target Manipulation Utility Functions
//##
//##############################################################################

// FIXME: This function is using type PRDF::HUID. I think it should now be using
//        TARGETING::HUID_ATTR. Also, will need equivalent to
//        PRDF::INVALID_HUID. I think HWSV has HWSV_INVALID_HUID, but I don't
//        think that exists in Hostboot. Need a common interface before making
//        changes.
TARGETING::TargetHandle_t getTarget( HUID i_huid )
{
    TargetHandle_t o_target = NULL;

    // FIXME: This is an incredibly inefficient linear search. It is recommended
    //        that the common targeting code provide an interface for us so that
    //        all users can call the potentially optimized function. There is a
    //        function available in HWSV (hwsvTargetUtil.H) but not in Hostboot.
    //        Sadly, the HWSV code does this exact linear search.
    TargetService & l_targetService = targetService();
    for ( TargetIterator l_targetPtr = l_targetService.begin();
          l_targetPtr != l_targetService.end(); ++l_targetPtr )
    {
        if ( i_huid == (l_targetPtr->getAttr<ATTR_HUID>()) )
        {
            o_target = (*l_targetPtr);
            break;
        }
    }

    if ( NULL == o_target )
    {
        PRDF_ERR( "[getTarget] i_huid: 0x%08x failed", i_huid );
    }

    return o_target;
}

//------------------------------------------------------------------------------

TARGETING::TargetHandle_t getTarget( const TARGETING::EntityPath & i_path )
{
    TargetHandle_t o_target = targetService().toTarget( i_path );
    if ( NULL == o_target )
    {
        PRDF_ERR( "[getTarget] Failed: i_path = " ); i_path.dump();
    }

    return o_target;
}

//------------------------------------------------------------------------------

int32_t getEntityPath( TARGETING::TargetHandle_t i_target,
                       TARGETING::EntityPath & o_path,
                       TARGETING::EntityPath::PATH_TYPE i_pathType )
{
    int32_t o_rc = FAIL;

    do
    {
        if ( NULL == i_target ) break;

        if ( EntityPath::PATH_NA != i_pathType )
            o_path.setType( i_pathType );

        ATTRIBUTE_ID attr = ATTR_NA;
        switch ( o_path.type() )
        {
            case EntityPath::PATH_AFFINITY: attr = ATTR_AFFINITY_PATH; break;
            case EntityPath::PATH_PHYSICAL: attr = ATTR_PHYS_PATH;     break;
            case EntityPath::PATH_POWER:    attr = ATTR_POWER_PATH;    break;
            default: ;
        }
        if ( ATTR_NA == attr )
        {
            PRDF_ERR( "[getEntityPath] Unsupported EntityPath type %d",
                      o_path.type() );
            break;
        }

        if ( !targetService().tryGetPath(attr, i_target, o_path) )
        {
            PRDF_ERR( "[getEntityPath] Failed to get path %d", attr );
            break;
        }

        o_rc = SUCCESS;

    } while (0);

    if ( SUCCESS != o_rc )
    {
        PRDF_ERR( "[getEntityPath] Failed: i_target=0x%08x",
                  getHuid(i_target) );
    }

    return o_rc;
}

//------------------------------------------------------------------------------

HUID getHuid( TARGETING::TargetHandle_t i_target )
{
    HUID o_huid = INVALID_HUID;

    do
    {
        if ( NULL == i_target ) break; // return INVALID_HUID

        // TODO: get_huid() (src/include/usr/targeting/common/util.H) can be
        //       called to fetch HUID however this feature is not yet available
        //       in FSP yet.
        if ( !i_target->tryGetAttr<ATTR_HUID>(o_huid) )
        {
            PRDF_ERR( "[getHuid] Failed to get ATTR_HUID" );
            o_huid = INVALID_HUID; // Just in case.
        }

    } while (0);

    return o_huid;
}

//------------------------------------------------------------------------------

bool isFunctional( TARGETING::TargetHandle_t i_target )
{
    bool o_funcState = false;

    do
    {
        if ( NULL == i_target )
        {
            PRDF_ERR( "[isFunctional] i_target is NULL" );
            break;
        }

        HwasState l_funcState;
        if ( !i_target->tryGetAttr<ATTR_HWAS_STATE>(l_funcState) )
        {
            PRDF_ERR( "[isFunctional] Failed to get ATTR_HWAS_STATE" );
            break;
        }

        if ( l_funcState.functional ) o_funcState =true;
    } while (0);

    return o_funcState;
}

//------------------------------------------------------------------------------

TARGETING::TYPE getTargetType( TARGETING::TargetHandle_t i_target )
{
    TYPE o_type = TYPE_LAST_IN_RANGE;

    if ( NULL != i_target )
    {
        if ( !i_target->tryGetAttr<ATTR_TYPE>(o_type) )
        {
            PRDF_ERR( "[getTargetType] Failed to get ATTR_TYPE" );
            o_type = TYPE_LAST_IN_RANGE; // Just in case
        }
    }

    if ( TYPE_LAST_IN_RANGE == o_type )
    {
        PRDF_ERR( "[getTargetType] Failed: i_target=0x%08x",
                  getHuid(i_target) );
    }

    return o_type;
}

//------------------------------------------------------------------------------

TARGETING::CLASS getTargetClass( TARGETING::TargetHandle_t i_target )
{
    CLASS o_class = CLASS_NA;

    if ( NULL != i_target )
    {
        if ( !i_target->tryGetAttr<ATTR_CLASS>(o_class) )
        {
            PRDF_ERR( "[getTargetClass] Failed to get ATTR_CLASS" );
            o_class = CLASS_NA; // Just in case
        }
    }

    if ( CLASS_NA == o_class )
    {
        PRDF_ERR( "[getTargetClass] Failed: i_target=0x%08x",
                  getHuid(i_target) );
    }

    return o_class;
}

//------------------------------------------------------------------------------

void printTargetInfo( TARGETING::TargetHandle_t i_target )
{
    EntityPath l_path ( EntityPath::PATH_PHYSICAL );
    int32_t l_rc = getEntityPath( i_target, l_path );
    if ( SUCCESS == l_rc )
    {
        PRDF_DTRAC( "PRDCONFIG: HUID=0x%08x path=", getHuid(i_target) );
        l_path.dump();
    }
    else
    {
        PRDF_ERR( "[printTargetInfo] Failed: i_target=0x%08x",
                  getHuid(i_target) );
    }
}

//------------------------------------------------------------------------------

uint8_t getChipLevel( TARGETING::TargetHandle_t i_target )
{
    uint8_t o_chipLvl = 0;

    do
    {
        if ( NULL == i_target ) break;

        TargetHandle_t l_parentTarget = getParentChip( i_target );
        if ( NULL == l_parentTarget ) break;

        if ( !l_parentTarget->tryGetAttr<ATTR_EC>(o_chipLvl) )
        {
            PRDF_ERR( "[getChipLevel] Failed to get ATTR_EC" );
            o_chipLvl = 0xff; // Just in case
        }

    } while (0);

    if ( 0 == o_chipLvl )
    {
        PRDF_ERR( "[getChipLevel] Failed: i_target=0x%08x", getHuid(i_target) );
    }

    return o_chipLvl;
}

//------------------------------------------------------------------------------

/* TODO: getChipId() may be available in an attribute, but this design has not
 *       been solidified. Instead, we may need to query for 'reason' attributes
 *       to determine the reason we need to do the checks. Since we don't have
 *       any immediate need for these functions (no workarounds as of yet), we
 *       will leave them commented out in the code.
uint8_t getChipId( TARGETING::TargetHandle_t i_target )
{
    // Returns chip ID enum (i.e. P7, P7+, etc.)
    return 0;
}
*/

//##############################################################################
//##
//##                       getConnected() support functions
//##
//##############################################################################

// This is a helper function for getConnected(). It will return the association
// type (CHILD_BY_AFFINITY or PARENT_BY_AFFINITY) between a target and
// destination target type. The function only characterizes parent or child
// relationships. It does not do any peer-to-peer relationships. The function
// will return non-SUCCESS if a relationship is not supported.

struct conn_t
{
    TYPE from : 8;
    TYPE to : 8;
    TargetService::ASSOCIATION_TYPE type : 8;

    static uint32_t getSortOrder( TYPE type )
    {
        // Can't trust that the order of the TYPE enum does not change so create
        // our own sorting order.

        uint32_t order = 0;

        switch ( type )
        {
            case TYPE_SYS:    order =  0; break;
            case TYPE_NODE:   order =  1; break;
            case TYPE_PROC:   order =  2; break;
            case TYPE_EX:     order =  3; break;
            case TYPE_XBUS:   order =  4; break;
            case TYPE_ABUS:   order =  5; break;
            case TYPE_PCI:    order =  6; break;
            case TYPE_MCS:    order =  7; break;
            case TYPE_MEMBUF: order =  8; break;
            case TYPE_L4:     order =  9; break;
            case TYPE_MBA:    order =  10; break;
            case TYPE_DIMM:   order =  11; break;
            default: ;
        }

        return order;
    }

    bool operator<( const conn_t & r )
    {
        uint32_t thisOrder = getSortOrder(this->from);
        uint32_t thatOrder = getSortOrder(r.from);

        if ( thisOrder == thatOrder )
            return ( getSortOrder(this->to) < getSortOrder(r.to) );
        else
            return ( thisOrder < thatOrder );
    }

};

int32_t getAssociationType( TARGETING::TargetHandle_t i_target,
                            TARGETING::TYPE i_connType,
                            TARGETING::TargetService::ASSOCIATION_TYPE & o_type)
{
    #define PRDF_FUNC "[PlatServices::getAssociationType] "

    int32_t o_rc = SUCCESS;

    static conn_t lookups[] =
    {
        // This table must be sorted based on the < operator of struct conn_t.
        { TYPE_SYS,    TYPE_NODE,   TargetService::CHILD_BY_AFFINITY },
        { TYPE_NODE,   TYPE_SYS,    TargetService::PARENT_BY_AFFINITY },
        { TYPE_NODE,   TYPE_PROC,   TargetService::CHILD_BY_AFFINITY },
        { TYPE_NODE,   TYPE_EX,     TargetService::CHILD_BY_AFFINITY },
        { TYPE_NODE,   TYPE_XBUS,   TargetService::CHILD_BY_AFFINITY },
        { TYPE_NODE,   TYPE_ABUS,   TargetService::CHILD_BY_AFFINITY },
        { TYPE_NODE,   TYPE_PCI,    TargetService::CHILD_BY_AFFINITY },
        { TYPE_NODE,   TYPE_MCS,    TargetService::CHILD_BY_AFFINITY },
        { TYPE_NODE,   TYPE_MEMBUF, TargetService::CHILD_BY_AFFINITY },
        { TYPE_NODE,   TYPE_L4,     TargetService::CHILD_BY_AFFINITY },
        { TYPE_NODE,   TYPE_MBA,    TargetService::CHILD_BY_AFFINITY },
        { TYPE_NODE,   TYPE_DIMM,   TargetService::CHILD_BY_AFFINITY },

        { TYPE_PROC,   TYPE_NODE,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_PROC,   TYPE_EX,     TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_XBUS,   TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_ABUS,   TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_PCI,    TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_MCS,    TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_MEMBUF, TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_L4,     TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_MBA,    TargetService::CHILD_BY_AFFINITY  },
        { TYPE_PROC,   TYPE_DIMM,   TargetService::CHILD_BY_AFFINITY  },

        { TYPE_EX,     TYPE_NODE,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_EX,     TYPE_PROC,   TargetService::PARENT_BY_AFFINITY },

        { TYPE_XBUS,   TYPE_NODE,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_XBUS,   TYPE_PROC,   TargetService::PARENT_BY_AFFINITY },

        { TYPE_ABUS,   TYPE_NODE,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_ABUS,   TYPE_PROC,   TargetService::PARENT_BY_AFFINITY },

        { TYPE_PCI,    TYPE_NODE,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_PCI,    TYPE_PROC,   TargetService::PARENT_BY_AFFINITY },

        { TYPE_MCS,    TYPE_NODE,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_MCS,    TYPE_PROC,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_MCS,    TYPE_MEMBUF, TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MCS,    TYPE_L4,     TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MCS,    TYPE_MBA,    TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MCS,    TYPE_DIMM,   TargetService::CHILD_BY_AFFINITY  },

        { TYPE_MEMBUF, TYPE_NODE,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_MEMBUF, TYPE_PROC,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_MEMBUF, TYPE_MCS,    TargetService::PARENT_BY_AFFINITY },
        { TYPE_MEMBUF, TYPE_L4,     TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MEMBUF, TYPE_MBA,    TargetService::CHILD_BY_AFFINITY  },
        { TYPE_MEMBUF, TYPE_DIMM,   TargetService::CHILD_BY_AFFINITY  },

        { TYPE_L4,     TYPE_NODE,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_L4,     TYPE_PROC,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_L4,     TYPE_MCS,    TargetService::PARENT_BY_AFFINITY },
        { TYPE_L4,     TYPE_MEMBUF, TargetService::PARENT_BY_AFFINITY },

        { TYPE_MBA,    TYPE_NODE,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_MBA,    TYPE_PROC,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_MBA,    TYPE_MCS,    TargetService::PARENT_BY_AFFINITY },
        { TYPE_MBA,    TYPE_MEMBUF, TargetService::PARENT_BY_AFFINITY },
        { TYPE_MBA,    TYPE_DIMM,   TargetService::CHILD_BY_AFFINITY  },

        { TYPE_DIMM,   TYPE_NODE,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_DIMM,   TYPE_PROC,   TargetService::PARENT_BY_AFFINITY },
        { TYPE_DIMM,   TYPE_MCS,    TargetService::PARENT_BY_AFFINITY },
        { TYPE_DIMM,   TYPE_MEMBUF, TargetService::PARENT_BY_AFFINITY },
        { TYPE_DIMM,   TYPE_MBA,    TargetService::PARENT_BY_AFFINITY },
    };

    do
    {
        if ( NULL == i_target )
        {
            PRDF_ERR( PRDF_FUNC"Given target is null" );
            o_rc = FAIL; break;
        }

        const size_t sz_lookups = sizeof(lookups) / sizeof(conn_t);

        TYPE type = getTargetType(i_target);

        conn_t match = { type, i_connType, TargetService::CHILD_BY_AFFINITY };

        conn_t * it = std::lower_bound( lookups, lookups + sz_lookups, match );

        if ( (it == lookups + sz_lookups) || // off the end
             (type != it->from) || (i_connType != it->to) ) // not equals
        {
            PRDF_ERR( PRDF_FUNC"Look-up failed: i_target=0x%08x i_connType=%d",
                      getHuid(i_target), i_connType );
            o_rc = FAIL; break;
        }

        o_type = it->type;

    } while (0);

    return o_rc;

    #undef PRDF_FUNC
}

// Helper function for the various getConnected() functions.
TargetHandleList getConnAssoc( TargetHandle_t i_target, TYPE i_connType,
                               TargetService::ASSOCIATION_TYPE i_assocType )
{
    #define PRDF_FUNC "[PlatServices::getConnAssoc] "

    TargetHandleList o_list; // Default empty list

    do
    {
        if ( NULL == i_target )
        {
            PRDF_ERR( PRDF_FUNC"Given target is null" );
            break;
        }

        // Match any class, specified type, and functional.
        PredicateCTM predType( CLASS_NA, i_connType );
        PredicateIsFunctional predFunc;
        PredicatePostfixExpr predAnd;
        predAnd.push(&predType).push(&predFunc).And();

        targetService().getAssociated( o_list, i_target, i_assocType,
                                       TargetService::ALL, &predAnd );

    } while(0);

    return o_list;

    #undef PRDF_FUNC
}

//------------------------------------------------------------------------------

TargetHandleList getConnected( TargetHandle_t i_target, TYPE i_connType )
{
    TargetHandleList o_list; // Default empty list

    do
    {
        if ( i_connType == getTargetType(i_target) )
        {
            o_list.push_back( i_target );
            break;
        }

        TargetService::ASSOCIATION_TYPE assocType;
        int32_t l_rc = getAssociationType( i_target, i_connType, assocType );
        if ( SUCCESS != l_rc ) break;

        o_list = getConnAssoc( i_target, i_connType, assocType );

    } while(0);

    return o_list;
}

//------------------------------------------------------------------------------

TargetHandle_t getConnectedParent( TargetHandle_t i_target, TYPE i_connType )
{
    #define PRDF_FUNC "[PlatServices::getConnectedParent] "

    TargetHandle_t o_parent = NULL;

    do
    {
        if ( i_connType == getTargetType(i_target) )
        {
            o_parent = i_target;
            break;
        }

        TargetService::ASSOCIATION_TYPE assocType;
        int32_t l_rc = getAssociationType( i_target, i_connType, assocType );
        if ( SUCCESS != l_rc ) break;

        if ( TargetService::PARENT_BY_AFFINITY != assocType )
        {
            PRDF_ERR( PRDF_FUNC"Unsupported parent connection: i_target=0x%08x "
                      "i_connType=%d", getHuid(i_target), i_connType );
            break;
        }

        TargetHandleList list = getConnAssoc( i_target, i_connType, assocType );
        if ( 1 != list.size() ) // Should be one and only one parent
        {
            PRDF_ERR( PRDF_FUNC"Could not find parent: i_target=0x%08x "
                      "i_connType=%d", getHuid(i_target), i_connType );
            break;
        }

        o_parent = list[0];

    } while(0);

    return o_parent;

    #undef PRDF_FUNC
}

//------------------------------------------------------------------------------

TargetHandle_t getConnectedChild( TargetHandle_t i_target, TYPE i_connType,
                                  uint32_t i_position )
{
    #define PRDF_FUNC "[PlatServices::getConnectedChild] "

    TargetHandle_t o_child = NULL;

    do
    {
        TargetService::ASSOCIATION_TYPE assocType;
        int32_t l_rc = getAssociationType( i_target, i_connType, assocType );
        if ( SUCCESS != l_rc ) break;

        if ( TargetService::CHILD_BY_AFFINITY != assocType )
        {
            PRDF_ERR( PRDF_FUNC"Unsupported child connection: i_target=0x%08x "
                    "i_connType=%d", getHuid(i_target), i_connType );
            break;
        }

        // SPECIAL CASE: The MEMBUF position number is relative to the PROC,
        //      not the MCS. This means the MEMBUF position number is the same
        //      as the position number of the attached MCS. In many cases, we
        //      want to get the MEMBUF connected to the MCS, but don't have
        //      knowledge of the MCS's position number (espeically in the rule
        //      code. So the following will change the desired position number
        //      to the MCS position number for MCS->MEMBUF connections only.
        if ( TYPE_MCS == getTargetType(i_target) && TYPE_MEMBUF == i_connType )
            i_position = getTargetPosition(i_target);

        TargetHandleList list = getConnAssoc( i_target, i_connType, assocType );
        for ( TargetHandleList::iterator i = list.begin();
              i != list.end(); ++i )
        {
            if ( i_position == getTargetPosition(*i) )
            {
                o_child = *i;
                break;
            }
        }

    } while(0);

    return o_child;

    #undef PRDF_FUNC
}

//------------------------------------------------------------------------------

TargetHandle_t getConnectedPeerTarget( TargetHandle_t i_target )
{
    #define PRDF_FUNC "[PlatServices::getConnectedPeerTarget] "

    TargetHandle_t o_target = NULL;

    do
    {
        if ( NULL == i_target )
        {
            PRDF_ERR( PRDF_FUNC"Given target is NULL" );
            break;
        }

        TYPE type = getTargetType( i_target );
        if ( TYPE_XBUS != type && TYPE_ABUS != type )
        {
            PRDF_ERR( PRDF_FUNC"Target type not supported: i_target=0x%08x "
                      "type=0x%x", getHuid(i_target), type );
            break;
        }

        o_target = i_target->getAttr<ATTR_PEER_TARGET>();

    } while(0);

    return o_target;

    #undef PRDF_FUNC
}

//------------------------------------------------------------------------------

TargetHandle_t getConnectedPeerProc( TargetHandle_t i_procTarget,
                                     TYPE i_busType, uint32_t i_busPos )
{
    #define PRDF_FUNC "[PlatServices::getConnectedPeerProc] "

    TargetHandle_t o_target = NULL;

    do
    {
        if ( NULL == i_procTarget || TYPE_PROC != getTargetType(i_procTarget) )
        {
            PRDF_ERR( PRDF_FUNC"Given target is not of TYPE_PROC: "
                      "i_procTarget=0x%08x", getHuid(i_procTarget) );
            break;
        }

        if ( !( ((TYPE_XBUS == i_busType) && (MAX_XBUS_PER_PROC > i_busPos)) ||
                ((TYPE_ABUS == i_busType) && (MAX_ABUS_PER_PROC > i_busPos)) ) )
            break;

        // Starting PROC -> starting XBUS/ABUS.
        TargetHandle_t busTarget = getConnectedChild( i_procTarget, i_busType,
                                                      i_busPos );
        if ( NULL == busTarget ) break;

        // Starting XBUS/ABUS -> ATTR_PEER_TARGET -> destination XBUS/ABUS.
        TargetHandle_t destTarget = getConnectedPeerTarget( busTarget );
        if ( NULL == destTarget ) break;

        // Destination XBUS/ABUS -> destination PROC.
        o_target = getConnectedParent( destTarget, TYPE_PROC );

    } while(0);

    return o_target;

    #undef PRDF_FUNC
}

//------------------------------------------------------------------------------

TARGETING::TargetHandle_t getSystemTarget()
{
    TargetHandle_t sysTarget = NULL;
    targetService().getTopLevelTarget( sysTarget );

    if ( NULL == sysTarget )
    {
        PRDF_ERR( "[getSystemTarget] Failed" );
    }

    return sysTarget;
}

//------------------------------------------------------------------------------

TARGETING::TargetHandle_t getParentChip( TARGETING::TargetHandle_t i_target )
{
    TargetHandle_t o_chipTarget = NULL;

    CLASS l_class = getTargetClass( i_target );
    switch ( l_class )
    {
        case CLASS_CHIP:
            o_chipTarget = i_target;
            break;

        case CLASS_UNIT:
        {
            TargetHandleList l_list;
            PredicateCTM l_predClass( CLASS_CHIP );
            targetService().getAssociated( l_list, i_target,
                                           TargetService::PARENT,
                                           TargetService::ALL,
                                           &l_predClass );
            if ( 1 == l_list.size() )
            {
                o_chipTarget = l_list[0];
            }
            else
            {
                PRDF_ERR( "[getParentChip] Could not find parent chip" );
            }
            break;
        }

        default:
            PRDF_ERR( "[getParentChip] Unsupported class: %d", l_class );
    }

    if ( NULL == o_chipTarget )
    {
        PRDF_ERR( "[getParentChip] Failed: i_target=0x%08x",
                  getHuid(i_target) );
    }

    return o_chipTarget;
}

//------------------------------------------------------------------------------

TARGETING::TargetHandleList getFunctionalTargetList( TARGETING::TYPE i_type )
{
    TargetHandleList o_list; // Default empty list.

    TargetService & l_targetService = targetService();

    // Match any class, specified type, and functional.
    PredicateCTM l_predType( CLASS_NA, i_type );
    PredicateIsFunctional l_predFunc;
    PredicatePostfixExpr l_predAnd;
    l_predAnd.push(&l_predType).push(&l_predFunc).And();

    // Defining a filter to get a list of all targets of i_type.
    TargetRangeFilter l_filter( l_targetService.begin(), l_targetService.end(),
                                &l_predAnd );
    for( ; l_filter; ++l_filter )
    {
        // Adding functional target to the vector.
        o_list.push_back( *l_filter );
    }

    return o_list;
}

//------------------------------------------------------------------------------

// FIXME: In the past, this was a wrapper for a GARD interface. Need to make
//        sure that we have the equivelant functionality.
bool checkLastFuncCore( TARGETING::TargetHandle_t i_coreTarget )
{
    bool o_lastCore = false;

    // TODO: Possibly support TYPE_EX, TYPE_L2, and TYPE_L3 as target input.

    TargetHandleList l_list = getFunctionalTargetList( TYPE_CORE );
    if ( 1 == l_list.size() && l_list[0] == i_coreTarget )
        o_lastCore = true;

    return o_lastCore;
}

//##############################################################################
//##
//##                       Target position support code
//##
//##############################################################################

uint32_t getTargetPosition( TARGETING::TargetHandle_t i_target )
{
    #define PRDF_FUNC "[PlatServices::getTargetPosition] "

    uint32_t o_pos = INVALID_POSITION_BOUND;

    CLASS l_class = getTargetClass( i_target );
    switch ( l_class )
    {
        case CLASS_CHIP:
        {
            TYPE l_type = getTargetType( i_target );
            switch ( l_type )
            {
                case TYPE_PROC:
                {
                    uint16_t tmpPos = 0;
                    if ( !i_target->tryGetAttr<ATTR_POSITION>(tmpPos) )
                    {
                        PRDF_ERR( PRDF_FUNC"Failed to get ATTR_POSITION" );
                    }
                    else
                        o_pos = (uint32_t)tmpPos;
                    break;
                }

                case TYPE_MEMBUF:
                    o_pos = getMemChnl( i_target );
                    break;

                default:
                    PRDF_ERR( PRDF_FUNC"Unsupported type: %d", l_type );
            }
            break;
        }

        case CLASS_UNIT:
        {
            uint8_t tmpPos = 0;
            if ( !i_target->tryGetAttr<ATTR_CHIP_UNIT>(tmpPos) )
            {
                PRDF_ERR( PRDF_FUNC"Failed to get ATTR_CHIP_UNIT" );
            }
            else
                o_pos = (uint32_t)tmpPos;
            break;
        }

        case CLASS_ENC:
            o_pos = getNodePosition( i_target );
            break;

        default:
            PRDF_ERR( PRDF_FUNC"Unsupported class: %d", l_class );
    }

    if ( INVALID_POSITION_BOUND == o_pos )
    {
        PRDF_ERR( PRDF_FUNC"Failed: target=0x%08x", getHuid(i_target) );
    }

    #undef PRDF_FUNC

    return o_pos;
}

//------------------------------------------------------------------------------

uint32_t getNodePosition( TARGETING::TargetHandle_t i_target )
{
    uint32_t o_pos = INVALID_POSITION_BOUND;

    do
    {
        // Get the node handle.
        TargetHandle_t node = getConnectedParent( i_target, TYPE_NODE );
        if ( NULL == node )
        {
            PRDF_ERR( "[getNodePosition] Failed to get node target" );
            break;
        }

        // FIXME: While this code works, it is preferred to use the POSITION
        //        attribute of the node. Currently, this attribute does not
        //        exist but it will, eventually. (RTC WI expected from Nick
        //        Bofferding)
        EntityPath l_path ( EntityPath::PATH_PHYSICAL );
        int32_t l_rc = getEntityPath( node, l_path );
        if ( SUCCESS != l_rc ) break;

        o_pos = l_path[l_path.size()-1].instance;

    } while (0);

    if ( INVALID_POSITION_BOUND == o_pos )
    {
        PRDF_ERR( "[getNodePosition] Failed: target=0x%08x",
                  getHuid(i_target) );
    }

    return o_pos;
}

//##############################################################################
//##
//##                        Memory specific functions
//##
//##############################################################################

int32_t getMasterRanks( TargetHandle_t i_memTarget,
                        uint32_t i_portSlct, uint32_t i_dimmSlct,
                        std::vector<uint32_t> & o_ranks )
{
    #define PRDF_FUNC "PlatServices::getMasterRanks] "

    int32_t o_rc = FAIL;

    do
    {
        if ( NULL == i_memTarget ) break;

        if ( (MAX_PORT_PER_MBA  <= i_portSlct) ||
             (MAX_DIMM_PER_PORT <= i_dimmSlct) )
            break;

        TargetHandle_t mbaTarget = getConnectedParent( i_memTarget, TYPE_MBA );
        if ( NULL == mbaTarget )
        {
            PRDF_ERR( PRDF_FUNC"getConnectedParent() failed" );
            break;
        }

        uint8_t rankInfo[MAX_PORT_PER_MBA][MAX_DIMM_PER_PORT];
        if( !mbaTarget->tryGetAttr<ATTR_EFF_DIMM_RANKS_CONFIGED>(rankInfo) )
        {
            PRDF_ERR( PRDF_FUNC"Failed to get attribute" );
            break;
        }

        uint8_t rankMask = rankInfo[i_portSlct][i_dimmSlct];
        if ( 0 == (rankMask & 0xf0) )
        {
            PRDF_ERR( PRDF_FUNC"Attribute value invalid: 0x%02x", rankMask );
            break;
        }

        for ( uint32_t rank = 0; rank < 4; rank++ )
        {
            if ( 0 != (rankMask & (0x80 >> rank)) )
            {
                o_ranks.push_back(rank);
            }
        }

        o_rc = SUCCESS;

    } while (0);

    if ( SUCCESS != o_rc )
    {
        PRDF_ERR( PRDF_FUNC"Failed: i_memTarget=0x%08x i_portSlct=%d "
                  "i_dimmSlct=%d",
                  getHuid(i_memTarget), i_portSlct, i_dimmSlct );
    }

    return o_rc;

    #undef PRDF_FUNC
}

//------------------------------------------------------------------------------

uint32_t getMemChnl( TARGETING::TargetHandle_t i_memTarget )
{
    #define PRDF_FUNC "[PlatServices::getMemChnl] "

    uint32_t o_chnl = INVALID_POSITION_BOUND; // Intentially set to
                                              // INVALID_POSITION_BOUND for call
                                              // from getTargetPosition().

    do
    {
        if ( NULL == i_memTarget ) break;

        TargetHandle_t mcsTarget = getConnectedParent( i_memTarget, TYPE_MCS );
        if ( NULL == mcsTarget )
        {
            PRDF_ERR( PRDF_FUNC"getConnectedParent() failed" );
            break;
        }

        o_chnl = getTargetPosition( mcsTarget );

    } while (0);

    if ( MAX_MCS_PER_PROC <= o_chnl ) // Real MCS position check.
    {
        PRDF_ERR( PRDF_FUNC"Failed: i_memTarget=0x%08x",
                  getHuid(i_memTarget) );
    }

    return o_chnl;

    #undef PRDF_FUNC
}

//------------------------------------------------------------------------------

int32_t isMembufOnDimm( TARGETING::TargetHandle_t i_memTarget,
                        bool & o_isBuffered )
{
    int32_t o_rc = FAIL;

    o_isBuffered = false;

    do
    {
        // The DIMMs in an node should either all be buffered or all not. So
        // we can check the attribute from ANY MBA.
        TargetHandleList list = getConnected( i_memTarget, TYPE_MBA );
        if ( 0 == list.size() )
        {
            PRDF_ERR( "[isMembufOnDimm] Couldn't find an MBA target" );
            break;
        }

        TargetHandle_t mbaTarget = list[0];

        o_isBuffered = mbaTarget->getAttr<ATTR_EFF_CUSTOM_DIMM>();

        o_rc = SUCCESS;

    } while (0);

    if ( SUCCESS != o_rc )
    {
        PRDF_ERR( "[isMembufOnDimm] Failed: i_memTarget=0x%08x",
                  getHuid(i_memTarget) );
    }

    return o_rc;
}

//------------------------------------------------------------------------------

int32_t getMbaPort( TARGETING::TargetHandle_t i_dimmTarget, uint8_t & o_port )
{
    return i_dimmTarget->tryGetAttr<ATTR_MBA_PORT>(o_port) ? SUCCESS : FAIL;
}

//------------------------------------------------------------------------------

int32_t getMbaDimm( TARGETING::TargetHandle_t i_dimmTarget, uint8_t & o_dimm )
{
    return i_dimmTarget->tryGetAttr<ATTR_MBA_DIMM>(o_dimm) ? SUCCESS : FAIL;
}

//------------------------------------------------------------------------------

bool isDramWidthX4( TargetHandle_t i_mba )
{
    return ( fapi::ENUM_ATTR_EFF_DRAM_WIDTH_X4 ==
             i_mba->getAttr<ATTR_EFF_DRAM_WIDTH>() );
}

//##############################################################################
//##
//##                        Clock specific functions
//##
//##############################################################################

// FIXME: RTC: 51628 will address clock target issue
bool areClocksOn(TARGETING::TargetHandle_t i_pGivenTarget)
{
    bool o_clocksOn = false;

    #ifdef __HOSTBOOT_MODULE

    o_clocksOn = true;

    #else

    if ( NULL != i_pGivenTarget )
    {
        errlHndl_t errl = NULL;
        //errl =HWSV::hwsvClockQueryOn(i_pGivenTarget,
        //                  HWSV::NO_MODE, o_clocksOn);
        if ( NULL != errl )
        {
            PRDF_ERR( "[areClocksOn] In areClocksOn failed" );
            PRDF_COMMIT_ERRL(errl, ERRL_ACTION_REPORT);
        }
    }
    else
    {
        PRDF_ERR( "[areClocksOn] given target is null" );
    }

    #endif

    return o_clocksOn;
}

//------------------------------------------------------------------------------

// FIXME: RTC: 51628 will address clock target issue
TARGETING::TargetHandle_t getClockId(TARGETING::TargetHandle_t
                            i_pGivenTarget,
                            TARGETING ::TYPE targetype)
{
    TargetHandleList l_clockCardlist;
    TargetHandle_t o_pClockCardHandle = NULL;

    return o_pClockCardHandle;
}

//------------------------------------------------------------------------------

// FIXME: RTC: 51628 will address clock target issue
TARGETING::TargetHandle_t getClockMux(TARGETING::TargetHandle_t
                            i_pGivenTarget)
{
    //Modeling info of card and Clock mux is required
    // PredicateCTM l_ClockMux(CLASS_UNIT,TYPE_CLOCK_MUX);
    //defined for compilation
    PredicateCTM l_ClockMux(CLASS_UNIT);
    TargetHandle_t o_ptargetClockMux = NULL;
    #if 0
    do
    {
        if(NULL != i_pGivenTarget)
        {
            TargetHandleList l_list;
            if(TYPE_PROC==(i_pGivenTarget->getAttr<ATTR_TYPE>()))
            {
                    targetService().getAssociated(l_list,
                                        i_pGivenTarget,
                                        TargetService::CHILD_BY_AFFINITY,
                                        TargetService::ALL,
                                        &l_ClockMux);
            }
            else
            {
                //TODO: If given target is not a proc  how to query all mux units
                //      which relation  to be used
            }

            if (l_list.size() > 0)
            {
                // Pick out first item
                o_ptargetClockMux = l_list[0];
            }
        }
        else
        {
            PRDF_ERR("[getClockMux] given target is NULL");
        }
    }while(0);
    #endif
    return o_ptargetClockMux;
}

//##############################################################################
//##                     MNFG Policy Flag Functions
//##############################################################################

// Helper function to access the state of manufacturing policy flags.
bool isMnfgFlagSet( uint32_t i_flag )
{
    bool o_rc = false;
    ATTR_MNFG_FLAGS_type l_attrValue = 0;
    TargetHandle_t l_pTopTarget= NULL;
    targetService().getTopLevelTarget(l_pTopTarget);
    if(l_pTopTarget)
    {
        l_attrValue = l_pTopTarget->getAttr<ATTR_MNFG_FLAGS>();
        o_rc = l_attrValue & i_flag;
    }
    else
    {
        PRDF_ERR("[isMnfgFlagSet] error finding l_pTopTarget");
    }

    //PRDF_TRAC("[isMnfgFlagSet] MNFG Flags: 0x%016llX, i_flag: "
    //          "0x%08X, o_rc: %d", l_attrValue, i_flag, o_rc);

    return o_rc;
}

//------------------------------------------------------------------------------

bool mfgMode()
{ return isMnfgFlagSet( MNFG_FLAG_BIT_MNFG_THRESHOLDS      ); }

bool hdatAvpMode()
{ return isMnfgFlagSet( MNFG_FLAG_BIT_MNFG_HDAT_AVP_ENABLE ); }

bool mnfgTerminate()
{ return isMnfgFlagSet( MNFG_FLAG_BIT_MNFG_SRC_TERM        ); }

bool areDramRepairsDisabled()
{ return isMnfgFlagSet( MNFG_FLAG_BIT_MNFG_DISABLE_DRAM_REPAIRS ); }

bool mnfgSpareDramDeploy()
{ return isMnfgFlagSet( MNFG_FLAG_BIT_MNFG_TEST_DRAM_REPAIRS ); }

} // end namespace PlatServices

} // end namespace PRDF

OpenPOWER on IntegriCloud