summaryrefslogtreecommitdiffstats
path: root/gen_ipmi_sensor.pl
blob: f9752906df3b0a3f539b4b2808f15ccfd0591067 (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
#! /usr/bin/perl
use strict;
use warnings;
use mrw::Targets;
use mrw::Inventory;
use mrw::Util;
use Getopt::Long; # For parsing command line arguments
use YAML::Tiny qw(LoadFile);

# Globals
my $serverwizFile  = "";
my $debug           = 0;
my $outputFile     = "";
my $metaDataFile   = "";

# Command line argument parsing
GetOptions(
"i=s" => \$serverwizFile,    # string
"m=s" => \$metaDataFile,     # string
"o=s" => \$outputFile,       # string
"d"   => \$debug,
)
or printUsage();

if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
{
    printUsage();
}

my $targetObj = Targets->new;
$targetObj->loadXML($serverwizFile);

#open the mrw xml and the metaData file for the sensor.
#Fetch the sensorid,sensortype,class,object path from the mrw.
#Get the metadata for that sensor from the metadata file.
#Merge the data into the outputfile

open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
my $sensorTypeConfig = LoadFile($metaDataFile);

my @interestedTypes = keys %{$sensorTypeConfig};
my %types;
my %entityDict;

@types{@interestedTypes} = ();

my @inventory = Inventory::getInventory($targetObj);
#Process all the targets in the XML
foreach my $target (sort keys %{$targetObj->getAllTargets()})
{
    my $sensorID = '';
    my $sensorType = '';
    my $sensorReadingType = '';
    my $path = '';
    my $obmcPath = '';
    my $sensorName = '';
    my $entityID = '';
    my $entityInstance = '';

    if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {

        $sensorName = $targetObj->getInstanceName($target);
        #not interested in this sensortype
        next if (not exists $types{$sensorName});

        $entityID = $targetObj->getAttribute($target, "IPMI_ENTITY_ID");
        if (exists ($entityDict{$entityID}))
        {
            $entityDict{$entityID}++;
        }
        else
        {
            $entityDict{$entityID} = '1';
        }
        $entityInstance = $entityDict{$entityID};

        $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");

        $sensorType = hex($targetObj->getAttribute($target,
                             "IPMI_SENSOR_TYPE"));

        $sensorReadingType = $targetObj->getAttribute($target,
                             "IPMI_SENSOR_READING_TYPE");

        $path = $targetObj->getAttribute($target, "INSTANCE_PATH");

        #if there is ipmi sensor without sensorid or sensorReadingType or
        #Instance path then die

        if ($sensorID eq '' or $sensorReadingType eq '' or $path eq '') {
            close $fh;
            die("sensor without info for target=$target");
        }

        if (exists $sensorTypeConfig->{$sensorName}{"path"}) {
            $obmcPath = $sensorTypeConfig->{$sensorName}->{"path"};
        }
        else {
            #removing the string "instance:" from path
            $path =~ s/^instance:/\//;
            $obmcPath = Util::getObmcName(\@inventory,$path);
        }

        # TODO via openbmc/openbmc#2144 - this fixup shouldn't be needed.
        $obmcPath = checkOccPathFixup($obmcPath);

        if (not defined $obmcPath) {
            close $fh;
            die("Unable to get the obmc path for path=$path");
        }

        print $fh $sensorID.":\n";

        my $serviceInterface =
            $sensorTypeConfig->{$sensorName}->{"serviceInterface"};
        my $readingType = $sensorTypeConfig->{$sensorName}->{"readingType"};
        my $sensorNamePattern =
                $sensorTypeConfig->{$sensorName}->{"sensorNamePattern"};

        # store the values in hash
        my %data;
        $data{'SENSOR_NAME'} = $sensorName;
        $data{'SENSOR_TYPE'} = $sensorType;
        $data{'SERVICE_INTF'} = $serviceInterface;
        $data{'READING_TYPE'} = $readingType;
        $data{'SENSOR_NAME_PATTERN'} = $sensorNamePattern;
        $data{'ENTITY_ID'} = $entityID;
        $data{'ENTITY_INSTANCE'} = $entityInstance;
        $data{'FH'} = $fh;

        my $debug = "$sensorID : $sensorName : $sensorType : ";
        $debug .= "$serviceInterface: $readingType : $sensorNamePattern : ";
        $debug .= "$entityID : $entityInstance : ";
        # temperature sensor
        if($sensorType == 0x01) {
            my $dbusPath =
                temperatureSensorPathFixup($sensorName, $target, $obmcPath);
            if (not defined $dbusPath) {
                warn("Unsupported sensor $sensorName, Ignoring\n");
                next;
            }
            my $multiplierM = $sensorTypeConfig->{$sensorName}->{"multiplierM"};
            my $offsetB = $sensorTypeConfig->{$sensorName}->{"offsetB"};
            my $bExp = $sensorTypeConfig->{$sensorName}->{"bExp"};
            my $rExp = $sensorTypeConfig->{$sensorName}->{"rExp"};
            my $unit = $sensorTypeConfig->{$sensorName}->{"unit"};
            my $scale = $sensorTypeConfig->{$sensorName}->{"scale"};
            # TODO: openbmc/openbmc#3026
            # Fix IPMI_SENSOR_READING_TYPE for vrm_vdd_temp_sensor
            if ($sensorName eq "vrm_vdd_temp_sensor") {
                $sensorReadingType = 1;
            }
            $data{'MULTIPLIER_M'} = $multiplierM;
            $data{'OFFSET_B'} = $offsetB;
            $data{'B_EXP'} = $bExp;
            $data{'R_EXP'} = $rExp;
            $data{'UNIT'} = $unit;
            $data{'SCALE'} = $scale;
            $data{'PATH'} = $dbusPath;
            $debug .= "$multiplierM : $offsetB : $bExp : $rExp : $unit : ";
            $debug .= "$scale : $dbusPath : $obmcPath : ";
        }
        else {
            $debug .= "$obmcPath : ";
            $data{'PATH'} = $obmcPath;
        }
        $data{'SENSOR_READING_TYPE'} = $sensorReadingType;
        writeToFile(%data);
        $debug .= "$sensorReadingType\n";
        printDebug("$debug");

    }
}
close $fh;

# Construct DBus object path for temperature sensors
sub temperatureSensorPathFixup
{
    my ($sensorName, $target, $path) = @_;
    $path = "/xyz/openbmc_project/sensors/temperature/";
    if($sensorName eq "cpucore_temp_sensor") {
        my $core = $targetObj->getTargetParent($target);
        my $coreNo = $targetObj->getAttribute($core, "IPMI_INSTANCE");
        my $proc = Util::getEnclosingFru($targetObj, $core);
        my $procNo = $targetObj->getAttribute($proc, "POSITION");
        my $size = Util::getSizeOfChildUnitsWithType($targetObj, "CORE", $proc);
        $coreNo = $coreNo - ($procNo * $size);
        $path .= "p" . $procNo . "_core" . $coreNo . "_temp";
    }
    elsif ($sensorName eq "dimm_temp_sensor") {
        my $dimm = $targetObj->getTargetParent($target);
        my $dimmconn = $targetObj->getTargetParent($dimm);
        my $pos = $targetObj->getAttribute($dimmconn, "POSITION");
        $path .= "dimm" . $pos . "_temp";
    }
    elsif ($sensorName eq "vrm_vdd_temp_sensor") {
        my $proc = Util::getEnclosingFru($targetObj, $target);
        my $procNo = $targetObj->getAttribute($proc, "POSITION");
        $path .= "p" . $procNo . "_vdd_temp";
    }
    elsif ($sensorName eq "memory_temp_sensor") {
        my $gvcard = $targetObj->getTargetParent($target);
        my $pos = $targetObj->getAttribute($gvcard, "IPMI_INSTANCE");
        $path .= "gpu" . $pos . "_mem_temp";
    }
    elsif ($sensorName eq "gpu_temp_sensor") {
        my $gvcard = $targetObj->getTargetParent($target);
        my $pos = $targetObj->getAttribute($gvcard, "IPMI_INSTANCE");
        $path .= "gpu" . $pos . "_core_temp";
    }
    else {
        return undef;
    }
    return $path;
}

#Write the interfaces data into the output file
sub writeInterfaces
{
    my ($interfaces, $fh) = @_;
    print $fh "  interfaces:"."\n";
    #Walk over all the interfces as it needs to be written
    while (my ($interface,$properties) = each %{$interfaces}) {
        print $fh "    ".$interface.":\n";
        #walk over all the properties as it needs to be written
        while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) {
            #will write property named "Property" first then
            #other properties.
            print $fh "      ".$dbusProperty.":\n";
            while (my ($condition,$offsets) = each %{$dbusPropertyValue}) {
                print $fh "          $condition:\n";
                while (my ($offset,$values) = each %{$offsets}) {
                    print $fh "            $offset:\n";
                    while (my ($key,$value) = each %{$values})  {
                        print $fh "              $key: ". $value."\n";
                    }
                }
            }
        }
    }
}

#Get the metadata for the incoming sensortype from the loaded config file.
#Write the sensor data into the output file
sub writeToFile
{
    my (%data) = @_;
    my $sensorType = $data{'SENSOR_TYPE'};
    my $fs = $data{'FH'};
    print $fh "  entityID: ".$data{'ENTITY_ID'}."\n";
    print $fh "  entityInstance: ".$data{'ENTITY_INSTANCE'}."\n";
    print $fh "  sensorType: ".$data{'SENSOR_TYPE'}."\n";
    print $fh "  path: ".$data{'PATH'}."\n";
    print $fh "  sensorReadingType: ".$data{'SENSOR_READING_TYPE'}."\n";
    print $fh "  serviceInterface: ".$data{'SERVICE_INTF'}."\n";
    print $fh "  readingType: ".$data{'READING_TYPE'}."\n";
    print $fh "  sensorNamePattern: ".$data{'SENSOR_NAME_PATTERN'}."\n";

    # temperature sensor
    if ($sensorType == 0x01) {
        print $fh "  multiplierM: ".$data{'MULTIPLIER_M'}."\n";
        print $fh "  offsetB: ".$data{'OFFSET_B'}."\n";
        print $fh "  bExp: ".$data{'B_EXP'}."\n";
        print $fh "  rExp: ".$data{'R_EXP'}."\n";
        print $fh "  unit: ".$data{'UNIT'}."\n";
        print $fh "  scale: ".$data{'SCALE'}."\n";
    }

    my $sensorName = $data{'SENSOR_NAME'};
    my $interfaces = $sensorTypeConfig->{$sensorName}->{"interfaces"};
    writeInterfaces($interfaces, $fh);
}

# Convert MRW OCC inventory path to application d-bus path
sub checkOccPathFixup
{
    my ($path) = @_;
    if ("/system/chassis/motherboard/cpu0/occ" eq $path) {
        return "/org/open_power/control/occ0";
    }
    if ("/system/chassis/motherboard/cpu1/occ" eq $path) {
        return "/org/open_power/control/occ1";
    }
    return $path;
}

# Usage
sub printUsage
{
    print "
    $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
Options:
    -d = debug mode
        \n";
    exit(1);
}

# Helper function to put debug statements.
sub printDebug
{
    my $str = shift;
    print "DEBUG: ", $str, "\n" if $debug;
}
OpenPOWER on IntegriCloud