blob: 882f5fb39b498d81087ed29f65ab99dfdfff5ffc [file] [log] [blame]
Andrew Geisslerb4edc272022-07-19 11:24:45 -04001#!/usr/bin/env perl
Andrew Geissler65ca8e12022-07-16 11:36:14 -04002
Ratan Guptac7366702017-02-22 18:05:44 +05303use strict;
4use warnings;
Ratan Guptac7366702017-02-22 18:05:44 +05305use mrw::Targets;
6use mrw::Inventory;
7use mrw::Util;
8use Getopt::Long; # For parsing command line arguments
9use YAML::Tiny qw(LoadFile);
10
11# Globals
12my $serverwizFile = "";
13my $debug = 0;
Ratan Gupta39747a02017-02-22 18:16:23 +053014my $outputFile = "";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050015my $metaDataFile = "";
Santosh Puranik51041fc2019-10-08 09:45:15 -050016my $skipBrokenMrw = 0;
Ratan Guptac7366702017-02-22 18:05:44 +053017
18# Command line argument parsing
19GetOptions(
20"i=s" => \$serverwizFile, # string
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050021"m=s" => \$metaDataFile, # string
Ratan Gupta39747a02017-02-22 18:16:23 +053022"o=s" => \$outputFile, # string
Santosh Puranik51041fc2019-10-08 09:45:15 -050023"skip-broken-mrw" => \$skipBrokenMrw,
Ratan Guptac7366702017-02-22 18:05:44 +053024"d" => \$debug,
25)
26or printUsage();
27
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050028if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
Ratan Guptac7366702017-02-22 18:05:44 +053029{
30 printUsage();
31}
32
33my $targetObj = Targets->new;
34$targetObj->loadXML($serverwizFile);
35
36#open the mrw xml and the metaData file for the sensor.
37#Fetch the sensorid,sensortype,class,object path from the mrw.
Ratan Gupta39747a02017-02-22 18:16:23 +053038#Get the metadata for that sensor from the metadata file.
39#Merge the data into the outputfile
Ratan Guptac7366702017-02-22 18:05:44 +053040
Ratan Gupta39747a02017-02-22 18:16:23 +053041open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050042my $sensorTypeConfig = LoadFile($metaDataFile);
Ratan Guptac7366702017-02-22 18:05:44 +053043
44my @interestedTypes = keys %{$sensorTypeConfig};
45my %types;
Tom Joseph98be5ac2018-01-24 01:37:30 +053046my %entityDict;
Ratan Guptac7366702017-02-22 18:05:44 +053047
48@types{@interestedTypes} = ();
49
50my @inventory = Inventory::getInventory($targetObj);
51#Process all the targets in the XML
52foreach my $target (sort keys %{$targetObj->getAllTargets()})
53{
54 my $sensorID = '';
55 my $sensorType = '';
56 my $sensorReadingType = '';
57 my $path = '';
58 my $obmcPath = '';
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050059 my $sensorName = '';
Tom Joseph98be5ac2018-01-24 01:37:30 +053060 my $entityID = '';
61 my $entityInstance = '';
Ratan Guptac7366702017-02-22 18:05:44 +053062
63 if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
64
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050065 $sensorName = $targetObj->getInstanceName($target);
66 #not interested in this sensortype
67 next if (not exists $types{$sensorName});
68
Tom Joseph98be5ac2018-01-24 01:37:30 +053069 $entityID = $targetObj->getAttribute($target, "IPMI_ENTITY_ID");
70 if (exists ($entityDict{$entityID}))
71 {
72 $entityDict{$entityID}++;
73 }
74 else
75 {
76 $entityDict{$entityID} = '1';
77 }
78 $entityInstance = $entityDict{$entityID};
79
Ratan Guptac7366702017-02-22 18:05:44 +053080 $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");
81
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050082 $sensorType = hex($targetObj->getAttribute($target,
83 "IPMI_SENSOR_TYPE"));
Ratan Guptac7366702017-02-22 18:05:44 +053084
85 $sensorReadingType = $targetObj->getAttribute($target,
86 "IPMI_SENSOR_READING_TYPE");
87
88 $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
89
Ratan Guptac7366702017-02-22 18:05:44 +053090 #if there is ipmi sensor without sensorid or sensorReadingType or
91 #Instance path then die
92
93 if ($sensorID eq '' or $sensorReadingType eq '' or $path eq '') {
Santosh Puranik51041fc2019-10-08 09:45:15 -050094 next if $skipBrokenMrw;
Ratan Gupta39747a02017-02-22 18:16:23 +053095 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +053096 die("sensor without info for target=$target");
97 }
98
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050099 if (exists $sensorTypeConfig->{$sensorName}{"path"}) {
100 $obmcPath = $sensorTypeConfig->{$sensorName}->{"path"};
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500101 }
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500102 else {
103 #removing the string "instance:" from path
104 $path =~ s/^instance:/\//;
105 $obmcPath = Util::getObmcName(\@inventory,$path);
106 }
107
108 # TODO via openbmc/openbmc#2144 - this fixup shouldn't be needed.
109 $obmcPath = checkOccPathFixup($obmcPath);
Ratan Guptac7366702017-02-22 18:05:44 +0530110
Ratan Guptac7366702017-02-22 18:05:44 +0530111 if (not defined $obmcPath) {
Ratan Gupta39747a02017-02-22 18:16:23 +0530112 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +0530113 die("Unable to get the obmc path for path=$path");
114 }
115
Ratan Gupta39747a02017-02-22 18:16:23 +0530116 print $fh $sensorID.":\n";
117
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500118 my $serviceInterface =
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500119 $sensorTypeConfig->{$sensorName}->{"serviceInterface"};
120 my $readingType = $sensorTypeConfig->{$sensorName}->{"readingType"};
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500121 my $sensorNamePattern =
122 $sensorTypeConfig->{$sensorName}->{"sensorNamePattern"};
Jayanth Othayothaf27cef2018-04-02 06:53:36 -0500123 my $mutability =
124 $sensorTypeConfig->{$sensorName}->{"mutability"};
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500125
126 # store the values in hash
127 my %data;
128 $data{'SENSOR_NAME'} = $sensorName;
129 $data{'SENSOR_TYPE'} = $sensorType;
130 $data{'SERVICE_INTF'} = $serviceInterface;
131 $data{'READING_TYPE'} = $readingType;
132 $data{'SENSOR_NAME_PATTERN'} = $sensorNamePattern;
Jayanth Othayothaf27cef2018-04-02 06:53:36 -0500133 $data{'MUTABILITY'} = $mutability;
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500134 $data{'ENTITY_ID'} = $entityID;
135 $data{'ENTITY_INSTANCE'} = $entityInstance;
136 $data{'FH'} = $fh;
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500137
Tom Joseph98be5ac2018-01-24 01:37:30 +0530138 my $debug = "$sensorID : $sensorName : $sensorType : ";
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500139 $debug .= "$serviceInterface: $readingType : $sensorNamePattern : ";
Jayanth Othayothaf27cef2018-04-02 06:53:36 -0500140 $debug .= "$serviceInterface: $readingType : $mutability : ";
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500141 $debug .= "$entityID : $entityInstance : ";
142 # temperature sensor
143 if($sensorType == 0x01) {
144 my $dbusPath =
145 temperatureSensorPathFixup($sensorName, $target, $obmcPath);
146 if (not defined $dbusPath) {
147 warn("Unsupported sensor $sensorName, Ignoring\n");
148 next;
149 }
150 my $multiplierM = $sensorTypeConfig->{$sensorName}->{"multiplierM"};
151 my $offsetB = $sensorTypeConfig->{$sensorName}->{"offsetB"};
152 my $bExp = $sensorTypeConfig->{$sensorName}->{"bExp"};
153 my $rExp = $sensorTypeConfig->{$sensorName}->{"rExp"};
154 my $unit = $sensorTypeConfig->{$sensorName}->{"unit"};
155 my $scale = $sensorTypeConfig->{$sensorName}->{"scale"};
Marri Devender Rao49b3e642018-03-20 09:25:25 -0500156 # TODO: openbmc/openbmc#3026
157 # Fix IPMI_SENSOR_READING_TYPE for vrm_vdd_temp_sensor
158 if ($sensorName eq "vrm_vdd_temp_sensor") {
159 $sensorReadingType = 1;
160 }
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500161 $data{'MULTIPLIER_M'} = $multiplierM;
162 $data{'OFFSET_B'} = $offsetB;
163 $data{'B_EXP'} = $bExp;
164 $data{'R_EXP'} = $rExp;
165 $data{'UNIT'} = $unit;
166 $data{'SCALE'} = $scale;
167 $data{'PATH'} = $dbusPath;
168 $debug .= "$multiplierM : $offsetB : $bExp : $rExp : $unit : ";
169 $debug .= "$scale : $dbusPath : $obmcPath : ";
170 }
171 else {
172 $debug .= "$obmcPath : ";
173 $data{'PATH'} = $obmcPath;
174 }
175 $data{'SENSOR_READING_TYPE'} = $sensorReadingType;
176 writeToFile(%data);
177 $debug .= "$sensorReadingType\n";
Tom Joseph98be5ac2018-01-24 01:37:30 +0530178 printDebug("$debug");
Ratan Guptac7366702017-02-22 18:05:44 +0530179
180 }
Ratan Guptac7366702017-02-22 18:05:44 +0530181}
Ratan Gupta39747a02017-02-22 18:16:23 +0530182close $fh;
183
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500184# Construct DBus object path for temperature sensors
185sub temperatureSensorPathFixup
186{
187 my ($sensorName, $target, $path) = @_;
188 $path = "/xyz/openbmc_project/sensors/temperature/";
189 if($sensorName eq "cpucore_temp_sensor") {
190 my $core = $targetObj->getTargetParent($target);
191 my $coreNo = $targetObj->getAttribute($core, "IPMI_INSTANCE");
192 my $proc = Util::getEnclosingFru($targetObj, $core);
193 my $procNo = $targetObj->getAttribute($proc, "POSITION");
194 my $size = Util::getSizeOfChildUnitsWithType($targetObj, "CORE", $proc);
195 $coreNo = $coreNo - ($procNo * $size);
196 $path .= "p" . $procNo . "_core" . $coreNo . "_temp";
197 }
198 elsif ($sensorName eq "dimm_temp_sensor") {
199 my $dimm = $targetObj->getTargetParent($target);
200 my $dimmconn = $targetObj->getTargetParent($dimm);
201 my $pos = $targetObj->getAttribute($dimmconn, "POSITION");
202 $path .= "dimm" . $pos . "_temp";
203 }
204 elsif ($sensorName eq "vrm_vdd_temp_sensor") {
205 my $proc = Util::getEnclosingFru($targetObj, $target);
206 my $procNo = $targetObj->getAttribute($proc, "POSITION");
207 $path .= "p" . $procNo . "_vdd_temp";
208 }
209 elsif ($sensorName eq "memory_temp_sensor") {
210 my $gvcard = $targetObj->getTargetParent($target);
211 my $pos = $targetObj->getAttribute($gvcard, "IPMI_INSTANCE");
212 $path .= "gpu" . $pos . "_mem_temp";
213 }
214 elsif ($sensorName eq "gpu_temp_sensor") {
215 my $gvcard = $targetObj->getTargetParent($target);
216 my $pos = $targetObj->getAttribute($gvcard, "IPMI_INSTANCE");
217 $path .= "gpu" . $pos . "_core_temp";
218 }
219 else {
220 return undef;
221 }
222 return $path;
223}
224
Marri Devender Rao17f06792018-03-20 03:15:10 -0500225#Write the interfaces data into the output file
226sub writeInterfaces
227{
228 my ($interfaces, $fh) = @_;
229 print $fh " interfaces:"."\n";
230 #Walk over all the interfces as it needs to be written
231 while (my ($interface,$properties) = each %{$interfaces}) {
232 print $fh " ".$interface.":\n";
233 #walk over all the properties as it needs to be written
234 while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) {
235 #will write property named "Property" first then
236 #other properties.
237 print $fh " ".$dbusProperty.":\n";
238 while (my ($condition,$offsets) = each %{$dbusPropertyValue}) {
239 print $fh " $condition:\n";
240 while (my ($offset,$values) = each %{$offsets}) {
241 print $fh " $offset:\n";
242 while (my ($key,$value) = each %{$values}) {
243 print $fh " $key: ". $value."\n";
244 }
245 }
246 }
247 }
248 }
249}
Ratan Gupta39747a02017-02-22 18:16:23 +0530250
251#Get the metadata for the incoming sensortype from the loaded config file.
252#Write the sensor data into the output file
Ratan Gupta39747a02017-02-22 18:16:23 +0530253sub writeToFile
254{
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500255 my (%data) = @_;
256 my $sensorType = $data{'SENSOR_TYPE'};
257 my $fs = $data{'FH'};
258 print $fh " entityID: ".$data{'ENTITY_ID'}."\n";
259 print $fh " entityInstance: ".$data{'ENTITY_INSTANCE'}."\n";
260 print $fh " sensorType: ".$data{'SENSOR_TYPE'}."\n";
261 print $fh " path: ".$data{'PATH'}."\n";
262 print $fh " sensorReadingType: ".$data{'SENSOR_READING_TYPE'}."\n";
263 print $fh " serviceInterface: ".$data{'SERVICE_INTF'}."\n";
264 print $fh " readingType: ".$data{'READING_TYPE'}."\n";
265 print $fh " sensorNamePattern: ".$data{'SENSOR_NAME_PATTERN'}."\n";
Jayanth Othayothaf27cef2018-04-02 06:53:36 -0500266 print $fh " mutability: ".$data{'MUTABILITY'}."\n";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500267
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500268 # temperature sensor
269 if ($sensorType == 0x01) {
270 print $fh " multiplierM: ".$data{'MULTIPLIER_M'}."\n";
271 print $fh " offsetB: ".$data{'OFFSET_B'}."\n";
272 print $fh " bExp: ".$data{'B_EXP'}."\n";
273 print $fh " rExp: ".$data{'R_EXP'}."\n";
274 print $fh " unit: ".$data{'UNIT'}."\n";
275 print $fh " scale: ".$data{'SCALE'}."\n";
276 }
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500277
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500278 my $sensorName = $data{'SENSOR_NAME'};
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500279 my $interfaces = $sensorTypeConfig->{$sensorName}->{"interfaces"};
Marri Devender Rao17f06792018-03-20 03:15:10 -0500280 writeInterfaces($interfaces, $fh);
Ratan Gupta39747a02017-02-22 18:16:23 +0530281}
Ratan Guptac7366702017-02-22 18:05:44 +0530282
Marri Devender Rao694e8dc2018-03-12 09:21:29 -0500283# Convert MRW OCC inventory path to application d-bus path
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500284sub checkOccPathFixup
285{
286 my ($path) = @_;
287 if ("/system/chassis/motherboard/cpu0/occ" eq $path) {
288 return "/org/open_power/control/occ0";
289 }
Ben Pai3d7698c2020-10-16 10:34:00 +0800290 if ("/system/chassis/motherboard/cpu/occ" eq $path) {
291 return "/org/open_power/control/occ0";
292 }
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500293 if ("/system/chassis/motherboard/cpu1/occ" eq $path) {
294 return "/org/open_power/control/occ1";
295 }
296 return $path;
297}
298
Ratan Guptac7366702017-02-22 18:05:44 +0530299# Usage
300sub printUsage
301{
302 print "
Ratan Gupta39747a02017-02-22 18:16:23 +0530303 $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptac7366702017-02-22 18:05:44 +0530304Options:
Santosh Puranik51041fc2019-10-08 09:45:15 -0500305 --skip-broken-mrw = Skip broken MRW targets
Ratan Guptac7366702017-02-22 18:05:44 +0530306 -d = debug mode
307 \n";
308 exit(1);
309}
310
311# Helper function to put debug statements.
312sub printDebug
313{
314 my $str = shift;
315 print "DEBUG: ", $str, "\n" if $debug;
316}