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 = ""; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 15 | |
| 16 | # Command line argument parsing |
| 17 | GetOptions( |
| 18 | "i=s" => \$serverwizFile, # string |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 19 | "m=s" => \$metaDataFile, # string |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 20 | "o=s" => \$outputFile, # string |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 21 | "d" => \$debug, |
| 22 | ) |
| 23 | or printUsage(); |
| 24 | |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 25 | if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq "")) |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 26 | { |
| 27 | printUsage(); |
| 28 | } |
| 29 | |
| 30 | my $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 Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 35 | #Get the metadata for that sensor from the metadata file. |
| 36 | #Merge the data into the outputfile |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 37 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 38 | open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!"; |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 39 | my $sensorTypeConfig = LoadFile($metaDataFile); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 40 | |
| 41 | my @interestedTypes = keys %{$sensorTypeConfig}; |
| 42 | my %types; |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 43 | my %entityDict; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 44 | |
| 45 | @types{@interestedTypes} = (); |
| 46 | |
| 47 | my @inventory = Inventory::getInventory($targetObj); |
| 48 | #Process all the targets in the XML |
| 49 | foreach my $target (sort keys %{$targetObj->getAllTargets()}) |
| 50 | { |
| 51 | my $sensorID = ''; |
| 52 | my $sensorType = ''; |
| 53 | my $sensorReadingType = ''; |
| 54 | my $path = ''; |
| 55 | my $obmcPath = ''; |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 56 | my $sensorName = ''; |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 57 | my $entityID = ''; |
| 58 | my $entityInstance = ''; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 59 | |
| 60 | if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") { |
| 61 | |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 62 | $sensorName = $targetObj->getInstanceName($target); |
| 63 | #not interested in this sensortype |
| 64 | next if (not exists $types{$sensorName}); |
| 65 | |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 66 | $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 Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 77 | $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID"); |
| 78 | |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 79 | $sensorType = hex($targetObj->getAttribute($target, |
| 80 | "IPMI_SENSOR_TYPE")); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 81 | |
| 82 | $sensorReadingType = $targetObj->getAttribute($target, |
| 83 | "IPMI_SENSOR_READING_TYPE"); |
| 84 | |
| 85 | $path = $targetObj->getAttribute($target, "INSTANCE_PATH"); |
| 86 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 87 | #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 Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 91 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 92 | die("sensor without info for target=$target"); |
| 93 | } |
| 94 | |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 95 | if (exists $sensorTypeConfig->{$sensorName}{"path"}) { |
| 96 | $obmcPath = $sensorTypeConfig->{$sensorName}->{"path"}; |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 97 | } |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 98 | 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 Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 106 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 107 | if (not defined $obmcPath) { |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 108 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 109 | die("Unable to get the obmc path for path=$path"); |
| 110 | } |
| 111 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 112 | print $fh $sensorID.":\n"; |
| 113 | |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 114 | my $serviceInterface = |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 115 | $sensorTypeConfig->{$sensorName}->{"serviceInterface"}; |
| 116 | my $readingType = $sensorTypeConfig->{$sensorName}->{"readingType"}; |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 117 | my $sensorNamePattern = |
| 118 | $sensorTypeConfig->{$sensorName}->{"sensorNamePattern"}; |
Jayanth Othayoth | af27cef | 2018-04-02 06:53:36 -0500 | [diff] [blame^] | 119 | my $mutability = |
| 120 | $sensorTypeConfig->{$sensorName}->{"mutability"}; |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 121 | |
| 122 | # store the values in hash |
| 123 | my %data; |
| 124 | $data{'SENSOR_NAME'} = $sensorName; |
| 125 | $data{'SENSOR_TYPE'} = $sensorType; |
| 126 | $data{'SERVICE_INTF'} = $serviceInterface; |
| 127 | $data{'READING_TYPE'} = $readingType; |
| 128 | $data{'SENSOR_NAME_PATTERN'} = $sensorNamePattern; |
Jayanth Othayoth | af27cef | 2018-04-02 06:53:36 -0500 | [diff] [blame^] | 129 | $data{'MUTABILITY'} = $mutability; |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 130 | $data{'ENTITY_ID'} = $entityID; |
| 131 | $data{'ENTITY_INSTANCE'} = $entityInstance; |
| 132 | $data{'FH'} = $fh; |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 133 | |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 134 | my $debug = "$sensorID : $sensorName : $sensorType : "; |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 135 | $debug .= "$serviceInterface: $readingType : $sensorNamePattern : "; |
Jayanth Othayoth | af27cef | 2018-04-02 06:53:36 -0500 | [diff] [blame^] | 136 | $debug .= "$serviceInterface: $readingType : $mutability : "; |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 137 | $debug .= "$entityID : $entityInstance : "; |
| 138 | # temperature sensor |
| 139 | if($sensorType == 0x01) { |
| 140 | my $dbusPath = |
| 141 | temperatureSensorPathFixup($sensorName, $target, $obmcPath); |
| 142 | if (not defined $dbusPath) { |
| 143 | warn("Unsupported sensor $sensorName, Ignoring\n"); |
| 144 | next; |
| 145 | } |
| 146 | my $multiplierM = $sensorTypeConfig->{$sensorName}->{"multiplierM"}; |
| 147 | my $offsetB = $sensorTypeConfig->{$sensorName}->{"offsetB"}; |
| 148 | my $bExp = $sensorTypeConfig->{$sensorName}->{"bExp"}; |
| 149 | my $rExp = $sensorTypeConfig->{$sensorName}->{"rExp"}; |
| 150 | my $unit = $sensorTypeConfig->{$sensorName}->{"unit"}; |
| 151 | my $scale = $sensorTypeConfig->{$sensorName}->{"scale"}; |
Marri Devender Rao | 49b3e64 | 2018-03-20 09:25:25 -0500 | [diff] [blame] | 152 | # TODO: openbmc/openbmc#3026 |
| 153 | # Fix IPMI_SENSOR_READING_TYPE for vrm_vdd_temp_sensor |
| 154 | if ($sensorName eq "vrm_vdd_temp_sensor") { |
| 155 | $sensorReadingType = 1; |
| 156 | } |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 157 | $data{'MULTIPLIER_M'} = $multiplierM; |
| 158 | $data{'OFFSET_B'} = $offsetB; |
| 159 | $data{'B_EXP'} = $bExp; |
| 160 | $data{'R_EXP'} = $rExp; |
| 161 | $data{'UNIT'} = $unit; |
| 162 | $data{'SCALE'} = $scale; |
| 163 | $data{'PATH'} = $dbusPath; |
| 164 | $debug .= "$multiplierM : $offsetB : $bExp : $rExp : $unit : "; |
| 165 | $debug .= "$scale : $dbusPath : $obmcPath : "; |
| 166 | } |
| 167 | else { |
| 168 | $debug .= "$obmcPath : "; |
| 169 | $data{'PATH'} = $obmcPath; |
| 170 | } |
| 171 | $data{'SENSOR_READING_TYPE'} = $sensorReadingType; |
| 172 | writeToFile(%data); |
| 173 | $debug .= "$sensorReadingType\n"; |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 174 | printDebug("$debug"); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 175 | |
| 176 | } |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 177 | } |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 178 | close $fh; |
| 179 | |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 180 | # Construct DBus object path for temperature sensors |
| 181 | sub temperatureSensorPathFixup |
| 182 | { |
| 183 | my ($sensorName, $target, $path) = @_; |
| 184 | $path = "/xyz/openbmc_project/sensors/temperature/"; |
| 185 | if($sensorName eq "cpucore_temp_sensor") { |
| 186 | my $core = $targetObj->getTargetParent($target); |
| 187 | my $coreNo = $targetObj->getAttribute($core, "IPMI_INSTANCE"); |
| 188 | my $proc = Util::getEnclosingFru($targetObj, $core); |
| 189 | my $procNo = $targetObj->getAttribute($proc, "POSITION"); |
| 190 | my $size = Util::getSizeOfChildUnitsWithType($targetObj, "CORE", $proc); |
| 191 | $coreNo = $coreNo - ($procNo * $size); |
| 192 | $path .= "p" . $procNo . "_core" . $coreNo . "_temp"; |
| 193 | } |
| 194 | elsif ($sensorName eq "dimm_temp_sensor") { |
| 195 | my $dimm = $targetObj->getTargetParent($target); |
| 196 | my $dimmconn = $targetObj->getTargetParent($dimm); |
| 197 | my $pos = $targetObj->getAttribute($dimmconn, "POSITION"); |
| 198 | $path .= "dimm" . $pos . "_temp"; |
| 199 | } |
| 200 | elsif ($sensorName eq "vrm_vdd_temp_sensor") { |
| 201 | my $proc = Util::getEnclosingFru($targetObj, $target); |
| 202 | my $procNo = $targetObj->getAttribute($proc, "POSITION"); |
| 203 | $path .= "p" . $procNo . "_vdd_temp"; |
| 204 | } |
| 205 | elsif ($sensorName eq "memory_temp_sensor") { |
| 206 | my $gvcard = $targetObj->getTargetParent($target); |
| 207 | my $pos = $targetObj->getAttribute($gvcard, "IPMI_INSTANCE"); |
| 208 | $path .= "gpu" . $pos . "_mem_temp"; |
| 209 | } |
| 210 | elsif ($sensorName eq "gpu_temp_sensor") { |
| 211 | my $gvcard = $targetObj->getTargetParent($target); |
| 212 | my $pos = $targetObj->getAttribute($gvcard, "IPMI_INSTANCE"); |
| 213 | $path .= "gpu" . $pos . "_core_temp"; |
| 214 | } |
| 215 | else { |
| 216 | return undef; |
| 217 | } |
| 218 | return $path; |
| 219 | } |
| 220 | |
Marri Devender Rao | 17f0679 | 2018-03-20 03:15:10 -0500 | [diff] [blame] | 221 | #Write the interfaces data into the output file |
| 222 | sub writeInterfaces |
| 223 | { |
| 224 | my ($interfaces, $fh) = @_; |
| 225 | print $fh " interfaces:"."\n"; |
| 226 | #Walk over all the interfces as it needs to be written |
| 227 | while (my ($interface,$properties) = each %{$interfaces}) { |
| 228 | print $fh " ".$interface.":\n"; |
| 229 | #walk over all the properties as it needs to be written |
| 230 | while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) { |
| 231 | #will write property named "Property" first then |
| 232 | #other properties. |
| 233 | print $fh " ".$dbusProperty.":\n"; |
| 234 | while (my ($condition,$offsets) = each %{$dbusPropertyValue}) { |
| 235 | print $fh " $condition:\n"; |
| 236 | while (my ($offset,$values) = each %{$offsets}) { |
| 237 | print $fh " $offset:\n"; |
| 238 | while (my ($key,$value) = each %{$values}) { |
| 239 | print $fh " $key: ". $value."\n"; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 246 | |
| 247 | #Get the metadata for the incoming sensortype from the loaded config file. |
| 248 | #Write the sensor data into the output file |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 249 | sub writeToFile |
| 250 | { |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 251 | my (%data) = @_; |
| 252 | my $sensorType = $data{'SENSOR_TYPE'}; |
| 253 | my $fs = $data{'FH'}; |
| 254 | print $fh " entityID: ".$data{'ENTITY_ID'}."\n"; |
| 255 | print $fh " entityInstance: ".$data{'ENTITY_INSTANCE'}."\n"; |
| 256 | print $fh " sensorType: ".$data{'SENSOR_TYPE'}."\n"; |
| 257 | print $fh " path: ".$data{'PATH'}."\n"; |
| 258 | print $fh " sensorReadingType: ".$data{'SENSOR_READING_TYPE'}."\n"; |
| 259 | print $fh " serviceInterface: ".$data{'SERVICE_INTF'}."\n"; |
| 260 | print $fh " readingType: ".$data{'READING_TYPE'}."\n"; |
| 261 | print $fh " sensorNamePattern: ".$data{'SENSOR_NAME_PATTERN'}."\n"; |
Jayanth Othayoth | af27cef | 2018-04-02 06:53:36 -0500 | [diff] [blame^] | 262 | print $fh " mutability: ".$data{'MUTABILITY'}."\n"; |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 263 | |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 264 | # temperature sensor |
| 265 | if ($sensorType == 0x01) { |
| 266 | print $fh " multiplierM: ".$data{'MULTIPLIER_M'}."\n"; |
| 267 | print $fh " offsetB: ".$data{'OFFSET_B'}."\n"; |
| 268 | print $fh " bExp: ".$data{'B_EXP'}."\n"; |
| 269 | print $fh " rExp: ".$data{'R_EXP'}."\n"; |
| 270 | print $fh " unit: ".$data{'UNIT'}."\n"; |
| 271 | print $fh " scale: ".$data{'SCALE'}."\n"; |
| 272 | } |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 273 | |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 274 | my $sensorName = $data{'SENSOR_NAME'}; |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 275 | my $interfaces = $sensorTypeConfig->{$sensorName}->{"interfaces"}; |
Marri Devender Rao | 17f0679 | 2018-03-20 03:15:10 -0500 | [diff] [blame] | 276 | writeInterfaces($interfaces, $fh); |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 277 | } |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 278 | |
Marri Devender Rao | 694e8dc | 2018-03-12 09:21:29 -0500 | [diff] [blame] | 279 | # Convert MRW OCC inventory path to application d-bus path |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 280 | sub checkOccPathFixup |
| 281 | { |
| 282 | my ($path) = @_; |
| 283 | if ("/system/chassis/motherboard/cpu0/occ" eq $path) { |
| 284 | return "/org/open_power/control/occ0"; |
| 285 | } |
| 286 | if ("/system/chassis/motherboard/cpu1/occ" eq $path) { |
| 287 | return "/org/open_power/control/occ1"; |
| 288 | } |
| 289 | return $path; |
| 290 | } |
| 291 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 292 | # Usage |
| 293 | sub printUsage |
| 294 | { |
| 295 | print " |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 296 | $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS] |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 297 | Options: |
| 298 | -d = debug mode |
| 299 | \n"; |
| 300 | exit(1); |
| 301 | } |
| 302 | |
| 303 | # Helper function to put debug statements. |
| 304 | sub printDebug |
| 305 | { |
| 306 | my $str = shift; |
| 307 | print "DEBUG: ", $str, "\n" if $debug; |
| 308 | } |