Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 1 | #! /usr/bin/perl |
| 2 | use strict; |
| 3 | use warnings; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 4 | use mrw::Targets; |
| 5 | use mrw::Inventory; |
| 6 | use mrw::Util; |
| 7 | use Getopt::Long; # For parsing command line arguments |
| 8 | use YAML::Tiny qw(LoadFile); |
| 9 | |
| 10 | # Globals |
| 11 | my $serverwizFile = ""; |
| 12 | my $debug = 0; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 13 | my $outputFile = ""; |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 14 | my $metaDataFile = ""; |
Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame] | 15 | my $skipBrokenMrw = 0; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 16 | |
| 17 | # Command line argument parsing |
| 18 | GetOptions( |
| 19 | "i=s" => \$serverwizFile, # string |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 20 | "m=s" => \$metaDataFile, # string |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 21 | "o=s" => \$outputFile, # string |
Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame] | 22 | "skip-broken-mrw" => \$skipBrokenMrw, |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 23 | "d" => \$debug, |
| 24 | ) |
| 25 | or printUsage(); |
| 26 | |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 27 | if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq "")) |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 28 | { |
| 29 | printUsage(); |
| 30 | } |
| 31 | |
| 32 | my $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 Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 37 | #Get the metadata for that sensor from the metadata file. |
| 38 | #Merge the data into the outputfile |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 39 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 40 | open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!"; |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 41 | my $sensorTypeConfig = LoadFile($metaDataFile); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 42 | |
| 43 | my @interestedTypes = keys %{$sensorTypeConfig}; |
| 44 | my %types; |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 45 | my %entityDict; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 46 | |
| 47 | @types{@interestedTypes} = (); |
| 48 | |
| 49 | my @inventory = Inventory::getInventory($targetObj); |
| 50 | #Process all the targets in the XML |
| 51 | foreach my $target (sort keys %{$targetObj->getAllTargets()}) |
| 52 | { |
| 53 | my $sensorID = ''; |
| 54 | my $sensorType = ''; |
| 55 | my $sensorReadingType = ''; |
| 56 | my $path = ''; |
| 57 | my $obmcPath = ''; |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 58 | my $sensorName = ''; |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 59 | my $entityID = ''; |
| 60 | my $entityInstance = ''; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 61 | |
| 62 | if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") { |
| 63 | |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 64 | $sensorName = $targetObj->getInstanceName($target); |
| 65 | #not interested in this sensortype |
| 66 | next if (not exists $types{$sensorName}); |
| 67 | |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 68 | $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 Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 79 | $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID"); |
| 80 | |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 81 | $sensorType = hex($targetObj->getAttribute($target, |
| 82 | "IPMI_SENSOR_TYPE")); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 83 | |
| 84 | $sensorReadingType = $targetObj->getAttribute($target, |
| 85 | "IPMI_SENSOR_READING_TYPE"); |
| 86 | |
| 87 | $path = $targetObj->getAttribute($target, "INSTANCE_PATH"); |
| 88 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 89 | #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 Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame] | 93 | next if $skipBrokenMrw; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 94 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 95 | die("sensor without info for target=$target"); |
| 96 | } |
| 97 | |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 98 | if (exists $sensorTypeConfig->{$sensorName}{"path"}) { |
| 99 | $obmcPath = $sensorTypeConfig->{$sensorName}->{"path"}; |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 100 | } |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 101 | 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 Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 109 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 110 | if (not defined $obmcPath) { |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 111 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 112 | die("Unable to get the obmc path for path=$path"); |
| 113 | } |
| 114 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 115 | print $fh $sensorID.":\n"; |
| 116 | |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 117 | my $serviceInterface = |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 118 | $sensorTypeConfig->{$sensorName}->{"serviceInterface"}; |
| 119 | my $readingType = $sensorTypeConfig->{$sensorName}->{"readingType"}; |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 120 | my $sensorNamePattern = |
| 121 | $sensorTypeConfig->{$sensorName}->{"sensorNamePattern"}; |
Jayanth Othayoth | af27cef | 2018-04-02 06:53:36 -0500 | [diff] [blame] | 122 | my $mutability = |
| 123 | $sensorTypeConfig->{$sensorName}->{"mutability"}; |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 124 | |
| 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 Othayoth | af27cef | 2018-04-02 06:53:36 -0500 | [diff] [blame] | 132 | $data{'MUTABILITY'} = $mutability; |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 133 | $data{'ENTITY_ID'} = $entityID; |
| 134 | $data{'ENTITY_INSTANCE'} = $entityInstance; |
| 135 | $data{'FH'} = $fh; |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 136 | |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 137 | my $debug = "$sensorID : $sensorName : $sensorType : "; |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 138 | $debug .= "$serviceInterface: $readingType : $sensorNamePattern : "; |
Jayanth Othayoth | af27cef | 2018-04-02 06:53:36 -0500 | [diff] [blame] | 139 | $debug .= "$serviceInterface: $readingType : $mutability : "; |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 140 | $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 Rao | 49b3e64 | 2018-03-20 09:25:25 -0500 | [diff] [blame] | 155 | # 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 Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 160 | $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 Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 177 | printDebug("$debug"); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 178 | |
| 179 | } |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 180 | } |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 181 | close $fh; |
| 182 | |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 183 | # Construct DBus object path for temperature sensors |
| 184 | sub 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 Rao | 17f0679 | 2018-03-20 03:15:10 -0500 | [diff] [blame] | 224 | #Write the interfaces data into the output file |
| 225 | sub 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 Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 249 | |
| 250 | #Get the metadata for the incoming sensortype from the loaded config file. |
| 251 | #Write the sensor data into the output file |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 252 | sub writeToFile |
| 253 | { |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 254 | 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 Othayoth | af27cef | 2018-04-02 06:53:36 -0500 | [diff] [blame] | 265 | print $fh " mutability: ".$data{'MUTABILITY'}."\n"; |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 266 | |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 267 | # 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 Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 276 | |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 277 | my $sensorName = $data{'SENSOR_NAME'}; |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 278 | my $interfaces = $sensorTypeConfig->{$sensorName}->{"interfaces"}; |
Marri Devender Rao | 17f0679 | 2018-03-20 03:15:10 -0500 | [diff] [blame] | 279 | writeInterfaces($interfaces, $fh); |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 280 | } |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 281 | |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 282 | # Convert MRW OCC inventory path to application d-bus path |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 283 | sub checkOccPathFixup |
| 284 | { |
| 285 | my ($path) = @_; |
| 286 | if ("/system/chassis/motherboard/cpu0/occ" eq $path) { |
| 287 | return "/org/open_power/control/occ0"; |
| 288 | } |
Ben Pai | 3d7698c | 2020-10-16 10:34:00 +0800 | [diff] [blame] | 289 | if ("/system/chassis/motherboard/cpu/occ" eq $path) { |
| 290 | return "/org/open_power/control/occ0"; |
| 291 | } |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 292 | if ("/system/chassis/motherboard/cpu1/occ" eq $path) { |
| 293 | return "/org/open_power/control/occ1"; |
| 294 | } |
| 295 | return $path; |
| 296 | } |
| 297 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 298 | # Usage |
| 299 | sub printUsage |
| 300 | { |
| 301 | print " |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 302 | $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS] |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 303 | Options: |
Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame] | 304 | --skip-broken-mrw = Skip broken MRW targets |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 305 | -d = debug mode |
| 306 | \n"; |
| 307 | exit(1); |
| 308 | } |
| 309 | |
| 310 | # Helper function to put debug statements. |
| 311 | sub printDebug |
| 312 | { |
| 313 | my $str = shift; |
| 314 | print "DEBUG: ", $str, "\n" if $debug; |
| 315 | } |