blob: a511c0f40c379cb1d85a390800567bf58af16b83 [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 = "";
Santosh Puranik51041fc2019-10-08 09:45:15 -050015my $skipBrokenMrw = 0;
Ratan Guptac7366702017-02-22 18:05:44 +053016
17# Command line argument parsing
18GetOptions(
19"i=s" => \$serverwizFile, # string
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050020"m=s" => \$metaDataFile, # string
Ratan Gupta39747a02017-02-22 18:16:23 +053021"o=s" => \$outputFile, # string
Santosh Puranik51041fc2019-10-08 09:45:15 -050022"skip-broken-mrw" => \$skipBrokenMrw,
Ratan Guptac7366702017-02-22 18:05:44 +053023"d" => \$debug,
24)
25or printUsage();
26
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050027if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
Ratan Guptac7366702017-02-22 18:05:44 +053028{
29 printUsage();
30}
31
32my $targetObj = Targets->new;
33$targetObj->loadXML($serverwizFile);
34
35#open the mrw xml and the metaData file for the sensor.
36#Fetch the sensorid,sensortype,class,object path from the mrw.
Ratan Gupta39747a02017-02-22 18:16:23 +053037#Get the metadata for that sensor from the metadata file.
38#Merge the data into the outputfile
Ratan Guptac7366702017-02-22 18:05:44 +053039
Ratan Gupta39747a02017-02-22 18:16:23 +053040open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050041my $sensorTypeConfig = LoadFile($metaDataFile);
Ratan Guptac7366702017-02-22 18:05:44 +053042
43my @interestedTypes = keys %{$sensorTypeConfig};
44my %types;
Tom Joseph98be5ac2018-01-24 01:37:30 +053045my %entityDict;
Ratan Guptac7366702017-02-22 18:05:44 +053046
47@types{@interestedTypes} = ();
48
49my @inventory = Inventory::getInventory($targetObj);
50#Process all the targets in the XML
51foreach my $target (sort keys %{$targetObj->getAllTargets()})
52{
53 my $sensorID = '';
54 my $sensorType = '';
55 my $sensorReadingType = '';
56 my $path = '';
57 my $obmcPath = '';
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050058 my $sensorName = '';
Tom Joseph98be5ac2018-01-24 01:37:30 +053059 my $entityID = '';
60 my $entityInstance = '';
Ratan Guptac7366702017-02-22 18:05:44 +053061
62 if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
63
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050064 $sensorName = $targetObj->getInstanceName($target);
65 #not interested in this sensortype
66 next if (not exists $types{$sensorName});
67
Tom Joseph98be5ac2018-01-24 01:37:30 +053068 $entityID = $targetObj->getAttribute($target, "IPMI_ENTITY_ID");
69 if (exists ($entityDict{$entityID}))
70 {
71 $entityDict{$entityID}++;
72 }
73 else
74 {
75 $entityDict{$entityID} = '1';
76 }
77 $entityInstance = $entityDict{$entityID};
78
Ratan Guptac7366702017-02-22 18:05:44 +053079 $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");
80
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050081 $sensorType = hex($targetObj->getAttribute($target,
82 "IPMI_SENSOR_TYPE"));
Ratan Guptac7366702017-02-22 18:05:44 +053083
84 $sensorReadingType = $targetObj->getAttribute($target,
85 "IPMI_SENSOR_READING_TYPE");
86
87 $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
88
Ratan Guptac7366702017-02-22 18:05:44 +053089 #if there is ipmi sensor without sensorid or sensorReadingType or
90 #Instance path then die
91
92 if ($sensorID eq '' or $sensorReadingType eq '' or $path eq '') {
Santosh Puranik51041fc2019-10-08 09:45:15 -050093 next if $skipBrokenMrw;
Ratan Gupta39747a02017-02-22 18:16:23 +053094 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +053095 die("sensor without info for target=$target");
96 }
97
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050098 if (exists $sensorTypeConfig->{$sensorName}{"path"}) {
99 $obmcPath = $sensorTypeConfig->{$sensorName}->{"path"};
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500100 }
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500101 else {
102 #removing the string "instance:" from path
103 $path =~ s/^instance:/\//;
104 $obmcPath = Util::getObmcName(\@inventory,$path);
105 }
106
107 # TODO via openbmc/openbmc#2144 - this fixup shouldn't be needed.
108 $obmcPath = checkOccPathFixup($obmcPath);
Ratan Guptac7366702017-02-22 18:05:44 +0530109
Ratan Guptac7366702017-02-22 18:05:44 +0530110 if (not defined $obmcPath) {
Ratan Gupta39747a02017-02-22 18:16:23 +0530111 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +0530112 die("Unable to get the obmc path for path=$path");
113 }
114
Ratan Gupta39747a02017-02-22 18:16:23 +0530115 print $fh $sensorID.":\n";
116
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500117 my $serviceInterface =
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500118 $sensorTypeConfig->{$sensorName}->{"serviceInterface"};
119 my $readingType = $sensorTypeConfig->{$sensorName}->{"readingType"};
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500120 my $sensorNamePattern =
121 $sensorTypeConfig->{$sensorName}->{"sensorNamePattern"};
Jayanth Othayothaf27cef2018-04-02 06:53:36 -0500122 my $mutability =
123 $sensorTypeConfig->{$sensorName}->{"mutability"};
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500124
125 # store the values in hash
126 my %data;
127 $data{'SENSOR_NAME'} = $sensorName;
128 $data{'SENSOR_TYPE'} = $sensorType;
129 $data{'SERVICE_INTF'} = $serviceInterface;
130 $data{'READING_TYPE'} = $readingType;
131 $data{'SENSOR_NAME_PATTERN'} = $sensorNamePattern;
Jayanth Othayothaf27cef2018-04-02 06:53:36 -0500132 $data{'MUTABILITY'} = $mutability;
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500133 $data{'ENTITY_ID'} = $entityID;
134 $data{'ENTITY_INSTANCE'} = $entityInstance;
135 $data{'FH'} = $fh;
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500136
Tom Joseph98be5ac2018-01-24 01:37:30 +0530137 my $debug = "$sensorID : $sensorName : $sensorType : ";
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500138 $debug .= "$serviceInterface: $readingType : $sensorNamePattern : ";
Jayanth Othayothaf27cef2018-04-02 06:53:36 -0500139 $debug .= "$serviceInterface: $readingType : $mutability : ";
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500140 $debug .= "$entityID : $entityInstance : ";
141 # temperature sensor
142 if($sensorType == 0x01) {
143 my $dbusPath =
144 temperatureSensorPathFixup($sensorName, $target, $obmcPath);
145 if (not defined $dbusPath) {
146 warn("Unsupported sensor $sensorName, Ignoring\n");
147 next;
148 }
149 my $multiplierM = $sensorTypeConfig->{$sensorName}->{"multiplierM"};
150 my $offsetB = $sensorTypeConfig->{$sensorName}->{"offsetB"};
151 my $bExp = $sensorTypeConfig->{$sensorName}->{"bExp"};
152 my $rExp = $sensorTypeConfig->{$sensorName}->{"rExp"};
153 my $unit = $sensorTypeConfig->{$sensorName}->{"unit"};
154 my $scale = $sensorTypeConfig->{$sensorName}->{"scale"};
Marri Devender Rao49b3e642018-03-20 09:25:25 -0500155 # TODO: openbmc/openbmc#3026
156 # Fix IPMI_SENSOR_READING_TYPE for vrm_vdd_temp_sensor
157 if ($sensorName eq "vrm_vdd_temp_sensor") {
158 $sensorReadingType = 1;
159 }
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500160 $data{'MULTIPLIER_M'} = $multiplierM;
161 $data{'OFFSET_B'} = $offsetB;
162 $data{'B_EXP'} = $bExp;
163 $data{'R_EXP'} = $rExp;
164 $data{'UNIT'} = $unit;
165 $data{'SCALE'} = $scale;
166 $data{'PATH'} = $dbusPath;
167 $debug .= "$multiplierM : $offsetB : $bExp : $rExp : $unit : ";
168 $debug .= "$scale : $dbusPath : $obmcPath : ";
169 }
170 else {
171 $debug .= "$obmcPath : ";
172 $data{'PATH'} = $obmcPath;
173 }
174 $data{'SENSOR_READING_TYPE'} = $sensorReadingType;
175 writeToFile(%data);
176 $debug .= "$sensorReadingType\n";
Tom Joseph98be5ac2018-01-24 01:37:30 +0530177 printDebug("$debug");
Ratan Guptac7366702017-02-22 18:05:44 +0530178
179 }
Ratan Guptac7366702017-02-22 18:05:44 +0530180}
Ratan Gupta39747a02017-02-22 18:16:23 +0530181close $fh;
182
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500183# Construct DBus object path for temperature sensors
184sub temperatureSensorPathFixup
185{
186 my ($sensorName, $target, $path) = @_;
187 $path = "/xyz/openbmc_project/sensors/temperature/";
188 if($sensorName eq "cpucore_temp_sensor") {
189 my $core = $targetObj->getTargetParent($target);
190 my $coreNo = $targetObj->getAttribute($core, "IPMI_INSTANCE");
191 my $proc = Util::getEnclosingFru($targetObj, $core);
192 my $procNo = $targetObj->getAttribute($proc, "POSITION");
193 my $size = Util::getSizeOfChildUnitsWithType($targetObj, "CORE", $proc);
194 $coreNo = $coreNo - ($procNo * $size);
195 $path .= "p" . $procNo . "_core" . $coreNo . "_temp";
196 }
197 elsif ($sensorName eq "dimm_temp_sensor") {
198 my $dimm = $targetObj->getTargetParent($target);
199 my $dimmconn = $targetObj->getTargetParent($dimm);
200 my $pos = $targetObj->getAttribute($dimmconn, "POSITION");
201 $path .= "dimm" . $pos . "_temp";
202 }
203 elsif ($sensorName eq "vrm_vdd_temp_sensor") {
204 my $proc = Util::getEnclosingFru($targetObj, $target);
205 my $procNo = $targetObj->getAttribute($proc, "POSITION");
206 $path .= "p" . $procNo . "_vdd_temp";
207 }
208 elsif ($sensorName eq "memory_temp_sensor") {
209 my $gvcard = $targetObj->getTargetParent($target);
210 my $pos = $targetObj->getAttribute($gvcard, "IPMI_INSTANCE");
211 $path .= "gpu" . $pos . "_mem_temp";
212 }
213 elsif ($sensorName eq "gpu_temp_sensor") {
214 my $gvcard = $targetObj->getTargetParent($target);
215 my $pos = $targetObj->getAttribute($gvcard, "IPMI_INSTANCE");
216 $path .= "gpu" . $pos . "_core_temp";
217 }
218 else {
219 return undef;
220 }
221 return $path;
222}
223
Marri Devender Rao17f06792018-03-20 03:15:10 -0500224#Write the interfaces data into the output file
225sub writeInterfaces
226{
227 my ($interfaces, $fh) = @_;
228 print $fh " interfaces:"."\n";
229 #Walk over all the interfces as it needs to be written
230 while (my ($interface,$properties) = each %{$interfaces}) {
231 print $fh " ".$interface.":\n";
232 #walk over all the properties as it needs to be written
233 while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) {
234 #will write property named "Property" first then
235 #other properties.
236 print $fh " ".$dbusProperty.":\n";
237 while (my ($condition,$offsets) = each %{$dbusPropertyValue}) {
238 print $fh " $condition:\n";
239 while (my ($offset,$values) = each %{$offsets}) {
240 print $fh " $offset:\n";
241 while (my ($key,$value) = each %{$values}) {
242 print $fh " $key: ". $value."\n";
243 }
244 }
245 }
246 }
247 }
248}
Ratan Gupta39747a02017-02-22 18:16:23 +0530249
250#Get the metadata for the incoming sensortype from the loaded config file.
251#Write the sensor data into the output file
Ratan Gupta39747a02017-02-22 18:16:23 +0530252sub writeToFile
253{
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500254 my (%data) = @_;
255 my $sensorType = $data{'SENSOR_TYPE'};
256 my $fs = $data{'FH'};
257 print $fh " entityID: ".$data{'ENTITY_ID'}."\n";
258 print $fh " entityInstance: ".$data{'ENTITY_INSTANCE'}."\n";
259 print $fh " sensorType: ".$data{'SENSOR_TYPE'}."\n";
260 print $fh " path: ".$data{'PATH'}."\n";
261 print $fh " sensorReadingType: ".$data{'SENSOR_READING_TYPE'}."\n";
262 print $fh " serviceInterface: ".$data{'SERVICE_INTF'}."\n";
263 print $fh " readingType: ".$data{'READING_TYPE'}."\n";
264 print $fh " sensorNamePattern: ".$data{'SENSOR_NAME_PATTERN'}."\n";
Jayanth Othayothaf27cef2018-04-02 06:53:36 -0500265 print $fh " mutability: ".$data{'MUTABILITY'}."\n";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500266
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500267 # temperature sensor
268 if ($sensorType == 0x01) {
269 print $fh " multiplierM: ".$data{'MULTIPLIER_M'}."\n";
270 print $fh " offsetB: ".$data{'OFFSET_B'}."\n";
271 print $fh " bExp: ".$data{'B_EXP'}."\n";
272 print $fh " rExp: ".$data{'R_EXP'}."\n";
273 print $fh " unit: ".$data{'UNIT'}."\n";
274 print $fh " scale: ".$data{'SCALE'}."\n";
275 }
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500276
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500277 my $sensorName = $data{'SENSOR_NAME'};
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500278 my $interfaces = $sensorTypeConfig->{$sensorName}->{"interfaces"};
Marri Devender Rao17f06792018-03-20 03:15:10 -0500279 writeInterfaces($interfaces, $fh);
Ratan Gupta39747a02017-02-22 18:16:23 +0530280}
Ratan Guptac7366702017-02-22 18:05:44 +0530281
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500282# Convert MRW OCC inventory path to application d-bus path
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500283sub checkOccPathFixup
284{
285 my ($path) = @_;
286 if ("/system/chassis/motherboard/cpu0/occ" eq $path) {
287 return "/org/open_power/control/occ0";
288 }
Ben Pai3d7698c2020-10-16 10:34:00 +0800289 if ("/system/chassis/motherboard/cpu/occ" eq $path) {
290 return "/org/open_power/control/occ0";
291 }
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500292 if ("/system/chassis/motherboard/cpu1/occ" eq $path) {
293 return "/org/open_power/control/occ1";
294 }
295 return $path;
296}
297
Ratan Guptac7366702017-02-22 18:05:44 +0530298# Usage
299sub printUsage
300{
301 print "
Ratan Gupta39747a02017-02-22 18:16:23 +0530302 $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptac7366702017-02-22 18:05:44 +0530303Options:
Santosh Puranik51041fc2019-10-08 09:45:15 -0500304 --skip-broken-mrw = Skip broken MRW targets
Ratan Guptac7366702017-02-22 18:05:44 +0530305 -d = debug mode
306 \n";
307 exit(1);
308}
309
310# Helper function to put debug statements.
311sub printDebug
312{
313 my $str = shift;
314 print "DEBUG: ", $str, "\n" if $debug;
315}