summaryrefslogtreecommitdiffstats
path: root/Inventory.pm
blob: 680c97e63f833e658bc90e00b3386efbad4f2d37 (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
package Inventory;

use strict;
use warnings;

#Target types to always include in the inventory if present
my %TYPES = (SYS => 1, NODE => 1, PROC => 1,
             BMC => 1, GPU => 1, CORE => 1, OCC => 1, TPM => 1);

#RU_TYPES of cards to include
#FRU = field replaceable unit, CRU = customer replaceable unit
my %RU_TYPES = (FRU => 1, CRU => 1);

#Chips that are modeled as modules (card-chip together)
my %MODULE_TYPES = (PROC => 1, GPU => 1);

#Returns an array of hashes that represents the inventory
#for a system.  The hash elements are:
#TARGET:  The MRW target of the item
#OBMC_NAME: The OpenBMC name for the item.  This is usually
#           a simplified version of the target.
sub getInventory
{
    my $targetObj = shift;
    my @inventory;

    if (ref($targetObj) ne "Targets") {
        die "Invalid Targets object passed to getInventory\n";
    }

    findItems($targetObj, \@inventory);

    pruneModuleCards($targetObj, \@inventory);

    makeOBMCNames($targetObj, \@inventory);

    return @inventory;
}


#Finds the inventory targets in the MRW.
#It selects them if the target's type is in %TYPES
#or the target's RU_TYPE is in %RU_TYPES.
#This will pick up FRUs and other chips like the BMC and processor.
sub findItems
{
    my ($targetObj, $inventory) = @_;

    for my $target (sort keys %{$targetObj->getAllTargets()}) {
        my $type = "";
        my $ruType = "";

        if (!$targetObj->isBadAttribute($target, "TYPE")) {
            $type = $targetObj->getAttribute($target, "TYPE");
        }

        if (!$targetObj->isBadAttribute($target, "RU_TYPE")) {
            $ruType = $targetObj->getAttribute($target, "RU_TYPE");
        }

        if ((exists $TYPES{$type}) || (exists $RU_TYPES{$ruType})) {
            my %item;
            $item{TARGET} = $target;
            $item{OBMC_NAME} = $target; #Will fixup later
            push @$inventory, { %item };
        }
    }
}


#Removes entries from the inventory for the card target of a module.
#Needed because processors and GPUs are modeled as a package which
#is a card-chip instance that plugs into a connector on the
#backplane/processor card.  Since we already include the chip target
#in the inventory (that's how we can identify what it is), we don't
#need the entry for the card target.
#
#For example, we'll already have .../module-0/proc-0 so we don't
#need a separate .../module-0 entry.
sub pruneModuleCards
{
    my ($targetObj, $inventory) = @_;
    my @toRemove;

    #Find the parent (a card) of items of type %type
    for my $item (@$inventory) {

        if (exists $MODULE_TYPES{$targetObj->getType($item->{TARGET})}) {
            my $card = $targetObj->getTargetParent($item->{TARGET});
            push @toRemove, $card;
        }
    }

    #Remove these parent cards
    for my $c (@toRemove) {
        for my $i (0 .. (scalar @$inventory) - 1) {
            if ($c eq $inventory->[$i]{TARGET}) {
                splice(@$inventory, $i, 1);
                last;
            }
        }
    }
}


#Makes the OpenBMC name for the targets in the inventory.
#Removes unnecessary segments of the path name, renames
#some segments to match standard conventions, and numbers
#segments based on their position attribute.
sub makeOBMCNames
{
    my ($targetObj, $inventory) = @_;

    #Remove connector segments from the OBMC_NAME
    removeConnectors($targetObj, $inventory);

    #Don't need the card instance of a PROC/GPU module
    removeModuleFromPath($targetObj, $inventory);

    #Don't need card segments for non-FRUs
    removeNonFRUCardSegments($targetObj, $inventory);

    #Don't need to show the middle units between proc & core
    removeIntermediateUnits($targetObj, $inventory);

    #Certain parts have standard naming
    renameSegmentWithType("PROC", "cpu", $targetObj, $inventory);
    renameSegmentWithType("SYS", "system", $targetObj, $inventory);
    renameSegmentWithType("NODE", "chassis", $targetObj, $inventory);

    #Make sure the motherboard is called 'motherboard' in the OBMC_NAME.
    #The only way to identify it is with its TARGET_TYPE,
    #which is different than the regular type.
    renameSegmentWithTargetType("card-motherboard", "motherboard",
                                $targetObj, $inventory);

    #Don't need instance numbers unless there are more than 1 present
    removeInstNumIfOneInstPresent($inventory);

    #We want card1, not card-1
    removeHyphensFromInstanceNum($inventory);

    pointChassisAtMotherboard($targetObj, $inventory);
}


#Removes non-FRU cards in the middle of a hierarchy from OBMC_NAME.
#For example, .../motherboard/fanriser-0/fan-0 ->
# .../motherboard/fan-0 when fanriser-0 isn't a FRU.
sub removeNonFRUCardSegments
{
    my ($targetObj, $inventory) = @_;

    for my $item (@$inventory) {

        #Split the target into segments, then start
        #adding segments in to make new targets so we can
        #make API calls on the segment instances.
        my @segments = split('/', $item->{TARGET});
        my $target = "";
        for my $s (@segments) {
            next if (length($s) == 0);

            $target .= "/$s";

            my $class = $targetObj->getAttribute($target, "CLASS");
            next if ($class ne "CARD");

            my $ruType = $targetObj->getAttribute($target, "RU_TYPE");

            #If this segment is a card but not a FRU,
            #remove it from the path.
            if (not exists $RU_TYPES{$ruType}) {
                my $segment = $targetObj->getInstanceName($target);
                $item->{OBMC_NAME} =~ s/\b$segment-\d+\b\///;
            }
        }
    }
}


#Removes connectors from the OBMC_NAME element.  Also
#takes the POSITION value of the connector and adds it
#to the card segment that plugs into the connector.
#For example:
#  /motherboard/card-conn-5/card-0 ->
#  /motherobard/card-5
sub removeConnectors
{
    my ($targetObj, $inventory) = @_;

    #Find the connectors embedded in the segments
    for my $item (@$inventory) {

        #Split the target into segments, then start
        #adding segments in to make new targets
        my @segments = split('/', $item->{TARGET});
        my $target = "";
        for my $s (@segments) {
            next if (length($s) == 0);

            $target .= "/$s";
            my $class = $targetObj->getAttribute($target, "CLASS");
            next unless ($class eq "CONNECTOR");

            my ($segment) = $target =~ /\b(\w+-\d+)$/;
            my $pos = $targetObj->getAttribute($target, "POSITION");

            #change /connector-11/card-2/ to /card-11/
            $item->{OBMC_NAME} =~ s/\b$segment\/(\w+)-\d+/$1-$pos/;

        }
    }
}


#Units, typically cores, can be subunits of other subunits of
#their parent chip.  We can remove all of these intermediate
#units.  For example, cpu0/someunit1/someunit2/core3 ->
#cpu0/core3.
sub removeIntermediateUnits
{
    my ($targetObj, $inventory) = @_;

    for my $item (@$inventory) {

        my $class = $targetObj->getAttribute($item->{TARGET}, "CLASS");
        next unless ($class eq "UNIT");

        my $parent = $targetObj->getTargetParent($item->{TARGET});

        #Remove all of these intermediate units until we find
        #something that isn't a unit (most likely a chip).
        while ($targetObj->getAttribute($parent, "CLASS") eq "UNIT") {

            my $name = $targetObj->getInstanceName($parent);
            $item->{OBMC_NAME} =~ s/$name(-)*(\d+)*\///;

            $parent = $targetObj->getTargetParent($parent);
        }
    }
}


#Renames segments of the paths to the name passed in
#based on the type of the segment.
#For example:
# renameSegmentWithType("PROC", "cpu", ...);
# With a target of:
#   motherboard-0/myp9proc-5/core-3
# Where target motherboard-0/myp9proc-5 has a type
# of PROC, gives:
#   motherboard-0/cpu-5/core-3
sub renameSegmentWithType
{
    my ($type, $newSegment, $targetObj, $inventory) = @_;

    #Find the targets for all the segments, and
    #if their type matches what we're looking for, then
    #save it so we can rename them later.
    for my $item (@$inventory) {

        my @segments = split('/', $item->{TARGET});
        my $target = "";

        for my $s (@segments) {
            next if (length($s) == 0);

            $target .= "/$s";
            my $curType = $targetObj->getType($target);
            next unless ($curType eq $type);

            my $oldSegment = $targetObj->getInstanceName($target);
            $item->{OBMC_NAME} =~ s/$oldSegment/$newSegment/;
        }
    }
}


#Removes the card portion of a module from OBMC_NAME.
#For example, .../motherboard-0/module-1/proc-0 ->
#.../motherboard-0/proc-1.
#This needs to be revisited if multi-processor modules
#ever come into plan.
sub removeModuleFromPath
{
    my ($targetObj, $inventory) = @_;
    my %chipNames;

    #Find the names of the chips on the modules
    for my $item (@$inventory) {
        if (exists $MODULE_TYPES{$targetObj->getType($item->{TARGET})}) {
            $chipNames{$targetObj->getInstanceName($item->{TARGET})} = 1;
        }
    }

    #Now convert module-A/name-B to name-A
    #Note that the -B isn't always present
    for my $item (@$inventory) {

        for my $name (keys %chipNames) {
            $item->{OBMC_NAME} =~ s/\w+-(\d+)\/$name(-\d+)*/$name-$1/;
        }
    }
}


#The same as renameSegmentWithType, but finds the segment
#to rename by calling Targets::getTargetType() on it
#instead of Targets::getType().
sub renameSegmentWithTargetType
{
    my ($type, $newSegment, $targetObj, $inventory) = @_;

    for my $item (@$inventory) {

        my @segments = split('/', $item->{TARGET});
        my $target = "";

        for my $s (@segments) {
            next if (length($s) == 0);

            $target .= "/$s";
            my $curType = $targetObj->getTargetType($target);
            next unless ($curType eq $type);

            my $oldSegment = $targetObj->getInstanceName($target);
            $item->{OBMC_NAME} =~ s/$oldSegment/$newSegment/;
        }
    }
}


#FRU management code needs the node/chassis item to point
#to the motherboard and not /system/chassis.  Can revisit this
#for multi-chassis systems if they ever show up.
sub pointChassisAtMotherboard
{
    my ($targetObj, $inventory) = @_;
    my $newName = undef;

    for my $item (@$inventory) {
        my $type = $targetObj->getTargetType($item->{TARGET});
        if ($type eq "card-motherboard") {
            $newName = $item->{OBMC_NAME};
            last;
        }
    }

    for my $item (@$inventory) {
        if ($targetObj->getType($item->{TARGET}) eq "NODE") {
            if (defined $newName) {
                $item->{OBMC_NAME} = $newName;
            }
            last;
        }
    }
}


#Removes the instance number from the OBMC_NAME segments
#where only 1 of those segments exists because numbering isn't
#necessary to distinguish them.
sub removeInstNumIfOneInstPresent
{
    my ($inventory) = @_;
    my %instanceHash;

    for my $item (@$inventory) {
        #Look at all the segments, keeping track if we've
        #seen a particular segment with the same instance before.
        my @segments = split('/', $item->{OBMC_NAME});
        for my $segment (@segments) {
            my ($s, $inst) = $segment =~ /(\w+)-(\d+)/;
            if (defined $s) {
                if (not exists $instanceHash{$s}) {
                    $instanceHash{$s}{inst} = $inst;
                }
                else {
                    if ($instanceHash{$s}{inst} ne $inst) {
                        $instanceHash{$s}{keep} = 1;
                    }
                }
            }
        }
    }

    #Remove the instanc numbers we don't need to keep.
    for my $segment (keys %instanceHash) {
        if (not exists $instanceHash{$segment}{keep}) {
            for my $item (@$inventory) {
               $item->{OBMC_NAME} =~ s/$segment-\d+/$segment/;
            }
        }
    }
}


#Removes the '-' from between the segment name and instance.
sub removeHyphensFromInstanceNum
{
    my ($inventory) = @_;

    for my $item (@$inventory) {
        $item->{OBMC_NAME} =~ s/-(\d+)\b/$1/g;
    }
}

1;

=head1 NAME

Inventory

=head1 DESCRIPTION

Retrieves the OpenBMC inventory from the MRW.

The inventory contains:

=over 4

=item * The system target

=item * The chassis target(s)  (Called a 'node' in the MRW.)

=item * All targets of class CARD or CHIP that are FRUs.

=item * All targets of type PROC

=item * All targets of type CORE

=item * All targets of type BMC

=item * All targets of type GPU

=back

=head2 Notes:

The processor and GPU chips are usually modeled in the MRW as a
card->chip package that would plug into a connector on the motherboard
or other parent card.  So, even if both card and chip are marked as a FRU,
there will only be 1 entry in the inventory for both, and the MRW
target associated with it will be for the chip and not the card.

In addition, this intermediate card will be removed from the path name:
  /system/chassis/motheboard/cpu and not
  /system/chassis/motherboard/cpucard/cpu

=head2 Inventory naming conventions

The inventory names returned in the OBMC_NAME hash element will follow
the conventions listed below.  An example of an inventory name is:
/system/chassis/motherboard/cpu5

=over 4

=item * If there is only 1 instance of any segment in the system, then
        it won't have an instance number, otherwise there will be one.

=item * The root of the name is '/system'.

=item * After system is 'chassis', of which there can be 1 or more.

=item * The name is based on the MRW card plugging order, and not what
        the system looks like from the outside.  For example, a power
        supply that plugs into a motherboard (maybe via a non-fru riser
        or cable, see the item below), would be:
        /system/chassis/motherboard/psu2 and not
        /system/chassis/psu2.

=item * If a card is not a FRU so isn't in the inventory itself, then it
        won't show up in the name of any child cards that are FRUs.
        For example, if fan-riser isn't a FRU, it would be
        /system/chassis/motherboard/fan3 and not
        /system/chassis/motherboard/fan-riser/fan3.

=item * The MRW models connectors between cards, but these never show up
        in the inventory name.

=item * If there is a motherboard, it is always called 'motherboard'.

=item * Processors, GPUs, and BMCs are always called: 'cpu', 'gpu' and
        'bmc' respectively.

=back

=head1 METHODS

=over 4

=item getInventory (C<TargetsObj>)

Returns an array of hashes representing inventory items.

The Hash contains:

* TARGET: The MRW target of the item, for example:

    /sys-0/node-0/motherboard-0/proc_socket-0/module-0/p9_proc_m

* OBMC_NAME: The OpenBMC name of the item, for example:

   /system/chassis/motherboard/cpu2

=back

=cut
OpenPOWER on IntegriCloud