blob: f9752906df3b0a3f539b4b2808f15ccfd0591067 [file] [log] [blame]
Ratan Guptac7366702017-02-22 18:05:44 +05301#! /usr/bin/perl
2use strict;
3use warnings;
Ratan Guptac7366702017-02-22 18:05:44 +05304use mrw::Targets;
5use mrw::Inventory;
6use mrw::Util;
7use Getopt::Long; # For parsing command line arguments
8use YAML::Tiny qw(LoadFile);
9
10# Globals
11my $serverwizFile = "";
12my $debug = 0;
Ratan Gupta39747a02017-02-22 18:16:23 +053013my $outputFile = "";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050014my $metaDataFile = "";
Ratan Guptac7366702017-02-22 18:05:44 +053015
16# Command line argument parsing
17GetOptions(
18"i=s" => \$serverwizFile, # string
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050019"m=s" => \$metaDataFile, # string
Ratan Gupta39747a02017-02-22 18:16:23 +053020"o=s" => \$outputFile, # string
Ratan Guptac7366702017-02-22 18:05:44 +053021"d" => \$debug,
22)
23or printUsage();
24
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050025if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
Ratan Guptac7366702017-02-22 18:05:44 +053026{
27 printUsage();
28}
29
30my $targetObj = Targets->new;
31$targetObj->loadXML($serverwizFile);
32
33#open the mrw xml and the metaData file for the sensor.
34#Fetch the sensorid,sensortype,class,object path from the mrw.
Ratan Gupta39747a02017-02-22 18:16:23 +053035#Get the metadata for that sensor from the metadata file.
36#Merge the data into the outputfile
Ratan Guptac7366702017-02-22 18:05:44 +053037
Ratan Gupta39747a02017-02-22 18:16:23 +053038open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050039my $sensorTypeConfig = LoadFile($metaDataFile);
Ratan Guptac7366702017-02-22 18:05:44 +053040
41my @interestedTypes = keys %{$sensorTypeConfig};
42my %types;
Tom Joseph98be5ac2018-01-24 01:37:30 +053043my %entityDict;
Ratan Guptac7366702017-02-22 18:05:44 +053044
45@types{@interestedTypes} = ();
46
47my @inventory = Inventory::getInventory($targetObj);
48#Process all the targets in the XML
49foreach my $target (sort keys %{$targetObj->getAllTargets()})
50{
51 my $sensorID = '';
52 my $sensorType = '';
53 my $sensorReadingType = '';
54 my $path = '';
55 my $obmcPath = '';
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050056 my $sensorName = '';
Tom Joseph98be5ac2018-01-24 01:37:30 +053057 my $entityID = '';
58 my $entityInstance = '';
Ratan Guptac7366702017-02-22 18:05:44 +053059
60 if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
61
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050062 $sensorName = $targetObj->getInstanceName($target);
63 #not interested in this sensortype
64 next if (not exists $types{$sensorName});
65
Tom Joseph98be5ac2018-01-24 01:37:30 +053066 $entityID = $targetObj->getAttribute($target, "IPMI_ENTITY_ID");
67 if (exists ($entityDict{$entityID}))
68 {
69 $entityDict{$entityID}++;
70 }
71 else
72 {
73 $entityDict{$entityID} = '1';
74 }
75 $entityInstance = $entityDict{$entityID};
76
Ratan Guptac7366702017-02-22 18:05:44 +053077 $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");
78
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050079 $sensorType = hex($targetObj->getAttribute($target,
80 "IPMI_SENSOR_TYPE"));
Ratan Guptac7366702017-02-22 18:05:44 +053081
82 $sensorReadingType = $targetObj->getAttribute($target,
83 "IPMI_SENSOR_READING_TYPE");
84
85 $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
86
Ratan Guptac7366702017-02-22 18:05:44 +053087 #if there is ipmi sensor without sensorid or sensorReadingType or
88 #Instance path then die
89
90 if ($sensorID eq '' or $sensorReadingType eq '' or $path eq '') {
Ratan Gupta39747a02017-02-22 18:16:23 +053091 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +053092 die("sensor without info for target=$target");
93 }
94
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050095 if (exists $sensorTypeConfig->{$sensorName}{"path"}) {
96 $obmcPath = $sensorTypeConfig->{$sensorName}->{"path"};
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -050097 }
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050098 else {
99 #removing the string "instance:" from path
100 $path =~ s/^instance:/\//;
101 $obmcPath = Util::getObmcName(\@inventory,$path);
102 }
103
104 # TODO via openbmc/openbmc#2144 - this fixup shouldn't be needed.
105 $obmcPath = checkOccPathFixup($obmcPath);
Ratan Guptac7366702017-02-22 18:05:44 +0530106
Ratan Guptac7366702017-02-22 18:05:44 +0530107 if (not defined $obmcPath) {
Ratan Gupta39747a02017-02-22 18:16:23 +0530108 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +0530109 die("Unable to get the obmc path for path=$path");
110 }
111
Ratan Gupta39747a02017-02-22 18:16:23 +0530112 print $fh $sensorID.":\n";
113
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500114 my $serviceInterface =
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500115 $sensorTypeConfig->{$sensorName}->{"serviceInterface"};
116 my $readingType = $sensorTypeConfig->{$sensorName}->{"readingType"};
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500117 my $sensorNamePattern =
118 $sensorTypeConfig->{$sensorName}->{"sensorNamePattern"};
119
120 # store the values in hash
121 my %data;
122 $data{'SENSOR_NAME'} = $sensorName;
123 $data{'SENSOR_TYPE'} = $sensorType;
124 $data{'SERVICE_INTF'} = $serviceInterface;
125 $data{'READING_TYPE'} = $readingType;
126 $data{'SENSOR_NAME_PATTERN'} = $sensorNamePattern;
127 $data{'ENTITY_ID'} = $entityID;
128 $data{'ENTITY_INSTANCE'} = $entityInstance;
129 $data{'FH'} = $fh;
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500130
Tom Joseph98be5ac2018-01-24 01:37:30 +0530131 my $debug = "$sensorID : $sensorName : $sensorType : ";
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500132 $debug .= "$serviceInterface: $readingType : $sensorNamePattern : ";
133 $debug .= "$entityID : $entityInstance : ";
134 # temperature sensor
135 if($sensorType == 0x01) {
136 my $dbusPath =
137 temperatureSensorPathFixup($sensorName, $target, $obmcPath);
138 if (not defined $dbusPath) {
139 warn("Unsupported sensor $sensorName, Ignoring\n");
140 next;
141 }
142 my $multiplierM = $sensorTypeConfig->{$sensorName}->{"multiplierM"};
143 my $offsetB = $sensorTypeConfig->{$sensorName}->{"offsetB"};
144 my $bExp = $sensorTypeConfig->{$sensorName}->{"bExp"};
145 my $rExp = $sensorTypeConfig->{$sensorName}->{"rExp"};
146 my $unit = $sensorTypeConfig->{$sensorName}->{"unit"};
147 my $scale = $sensorTypeConfig->{$sensorName}->{"scale"};
Marri Devender Rao49b3e642018-03-20 09:25:25 -0500148 # TODO: openbmc/openbmc#3026
149 # Fix IPMI_SENSOR_READING_TYPE for vrm_vdd_temp_sensor
150 if ($sensorName eq "vrm_vdd_temp_sensor") {
151 $sensorReadingType = 1;
152 }
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500153 $data{'MULTIPLIER_M'} = $multiplierM;
154 $data{'OFFSET_B'} = $offsetB;
155 $data{'B_EXP'} = $bExp;
156 $data{'R_EXP'} = $rExp;
157 $data{'UNIT'} = $unit;
158 $data{'SCALE'} = $scale;
159 $data{'PATH'} = $dbusPath;
160 $debug .= "$multiplierM : $offsetB : $bExp : $rExp : $unit : ";
161 $debug .= "$scale : $dbusPath : $obmcPath : ";
162 }
163 else {
164 $debug .= "$obmcPath : ";
165 $data{'PATH'} = $obmcPath;
166 }
167 $data{'SENSOR_READING_TYPE'} = $sensorReadingType;
168 writeToFile(%data);
169 $debug .= "$sensorReadingType\n";
Tom Joseph98be5ac2018-01-24 01:37:30 +0530170 printDebug("$debug");
Ratan Guptac7366702017-02-22 18:05:44 +0530171
172 }
Ratan Guptac7366702017-02-22 18:05:44 +0530173}
Ratan Gupta39747a02017-02-22 18:16:23 +0530174close $fh;
175
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500176# Construct DBus object path for temperature sensors
177sub temperatureSensorPathFixup
178{
179 my ($sensorName, $target, $path) = @_;
180 $path = "/xyz/openbmc_project/sensors/temperature/";
181 if($sensorName eq "cpucore_temp_sensor") {
182 my $core = $targetObj->getTargetParent($target);
183 my $coreNo = $targetObj->getAttribute($core, "IPMI_INSTANCE");
184 my $proc = Util::getEnclosingFru($targetObj, $core);
185 my $procNo = $targetObj->getAttribute($proc, "POSITION");
186 my $size = Util::getSizeOfChildUnitsWithType($targetObj, "CORE", $proc);
187 $coreNo = $coreNo - ($procNo * $size);
188 $path .= "p" . $procNo . "_core" . $coreNo . "_temp";
189 }
190 elsif ($sensorName eq "dimm_temp_sensor") {
191 my $dimm = $targetObj->getTargetParent($target);
192 my $dimmconn = $targetObj->getTargetParent($dimm);
193 my $pos = $targetObj->getAttribute($dimmconn, "POSITION");
194 $path .= "dimm" . $pos . "_temp";
195 }
196 elsif ($sensorName eq "vrm_vdd_temp_sensor") {
197 my $proc = Util::getEnclosingFru($targetObj, $target);
198 my $procNo = $targetObj->getAttribute($proc, "POSITION");
199 $path .= "p" . $procNo . "_vdd_temp";
200 }
201 elsif ($sensorName eq "memory_temp_sensor") {
202 my $gvcard = $targetObj->getTargetParent($target);
203 my $pos = $targetObj->getAttribute($gvcard, "IPMI_INSTANCE");
204 $path .= "gpu" . $pos . "_mem_temp";
205 }
206 elsif ($sensorName eq "gpu_temp_sensor") {
207 my $gvcard = $targetObj->getTargetParent($target);
208 my $pos = $targetObj->getAttribute($gvcard, "IPMI_INSTANCE");
209 $path .= "gpu" . $pos . "_core_temp";
210 }
211 else {
212 return undef;
213 }
214 return $path;
215}
216
Marri Devender Rao17f06792018-03-20 03:15:10 -0500217#Write the interfaces data into the output file
218sub writeInterfaces
219{
220 my ($interfaces, $fh) = @_;
221 print $fh " interfaces:"."\n";
222 #Walk over all the interfces as it needs to be written
223 while (my ($interface,$properties) = each %{$interfaces}) {
224 print $fh " ".$interface.":\n";
225 #walk over all the properties as it needs to be written
226 while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) {
227 #will write property named "Property" first then
228 #other properties.
229 print $fh " ".$dbusProperty.":\n";
230 while (my ($condition,$offsets) = each %{$dbusPropertyValue}) {
231 print $fh " $condition:\n";
232 while (my ($offset,$values) = each %{$offsets}) {
233 print $fh " $offset:\n";
234 while (my ($key,$value) = each %{$values}) {
235 print $fh " $key: ". $value."\n";
236 }
237 }
238 }
239 }
240 }
241}
Ratan Gupta39747a02017-02-22 18:16:23 +0530242
243#Get the metadata for the incoming sensortype from the loaded config file.
244#Write the sensor data into the output file
Ratan Gupta39747a02017-02-22 18:16:23 +0530245sub writeToFile
246{
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500247 my (%data) = @_;
248 my $sensorType = $data{'SENSOR_TYPE'};
249 my $fs = $data{'FH'};
250 print $fh " entityID: ".$data{'ENTITY_ID'}."\n";
251 print $fh " entityInstance: ".$data{'ENTITY_INSTANCE'}."\n";
252 print $fh " sensorType: ".$data{'SENSOR_TYPE'}."\n";
253 print $fh " path: ".$data{'PATH'}."\n";
254 print $fh " sensorReadingType: ".$data{'SENSOR_READING_TYPE'}."\n";
255 print $fh " serviceInterface: ".$data{'SERVICE_INTF'}."\n";
256 print $fh " readingType: ".$data{'READING_TYPE'}."\n";
257 print $fh " sensorNamePattern: ".$data{'SENSOR_NAME_PATTERN'}."\n";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500258
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500259 # temperature sensor
260 if ($sensorType == 0x01) {
261 print $fh " multiplierM: ".$data{'MULTIPLIER_M'}."\n";
262 print $fh " offsetB: ".$data{'OFFSET_B'}."\n";
263 print $fh " bExp: ".$data{'B_EXP'}."\n";
264 print $fh " rExp: ".$data{'R_EXP'}."\n";
265 print $fh " unit: ".$data{'UNIT'}."\n";
266 print $fh " scale: ".$data{'SCALE'}."\n";
267 }
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500268
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500269 my $sensorName = $data{'SENSOR_NAME'};
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500270 my $interfaces = $sensorTypeConfig->{$sensorName}->{"interfaces"};
Marri Devender Rao17f06792018-03-20 03:15:10 -0500271 writeInterfaces($interfaces, $fh);
Ratan Gupta39747a02017-02-22 18:16:23 +0530272}
Ratan Guptac7366702017-02-22 18:05:44 +0530273
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500274# Convert MRW OCC inventory path to application d-bus path
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500275sub checkOccPathFixup
276{
277 my ($path) = @_;
278 if ("/system/chassis/motherboard/cpu0/occ" eq $path) {
279 return "/org/open_power/control/occ0";
280 }
281 if ("/system/chassis/motherboard/cpu1/occ" eq $path) {
282 return "/org/open_power/control/occ1";
283 }
284 return $path;
285}
286
Ratan Guptac7366702017-02-22 18:05:44 +0530287# Usage
288sub printUsage
289{
290 print "
Ratan Gupta39747a02017-02-22 18:16:23 +0530291 $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptac7366702017-02-22 18:05:44 +0530292Options:
293 -d = debug mode
294 \n";
295 exit(1);
296}
297
298# Helper function to put debug statements.
299sub printDebug
300{
301 my $str = shift;
302 print "DEBUG: ", $str, "\n" if $debug;
303}