Matt Spinler | 8df7be8 | 2017-01-09 15:42:05 -0600 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | |
| 3 | #Creates a configuration file for each hwmon sensor in the MRW |
| 4 | #for use by phosphor-hwmon. These configuration files contain |
| 5 | #labels and thresholds for the hwmon features for that sensor. |
| 6 | |
| 7 | use strict; |
| 8 | use warnings; |
| 9 | |
| 10 | use mrw::Targets; |
| 11 | use mrw::Util; |
| 12 | use Getopt::Long; |
| 13 | |
| 14 | use constant { |
| 15 | I2C_TYPE => "i2c" |
| 16 | }; |
| 17 | |
| 18 | my $serverwizFile; |
| 19 | my @hwmon; |
| 20 | |
| 21 | GetOptions("x=s" => \$serverwizFile) or printUsage(); |
| 22 | |
| 23 | if (not defined $serverwizFile) { |
| 24 | printUsage(); |
| 25 | } |
| 26 | |
| 27 | my $g_targetObj = Targets->new; |
| 28 | $g_targetObj->loadXML($serverwizFile); |
| 29 | |
| 30 | my $bmc = Util::getBMCTarget($g_targetObj); |
| 31 | |
| 32 | getI2CSensors($bmc, \@hwmon); |
| 33 | |
| 34 | exit 0; |
| 35 | |
| 36 | |
| 37 | #Returns an array of hashes that represent hwmon enabled I2C sensors. |
| 38 | sub getI2CSensors |
| 39 | { |
| 40 | my ($bmc, $hwmon) = @_; |
| 41 | my $connections = $g_targetObj->findConnections($bmc, "I2C"); |
| 42 | |
| 43 | return if ($connections eq ""); |
| 44 | |
| 45 | for my $i2c (@{$connections->{CONN}}) { |
| 46 | |
| 47 | my $chip = $i2c->{DEST_PARENT}; |
| 48 | my @hwmonUnits = Util::getChildUnitsWithTargetType($g_targetObj, |
| 49 | "unit-hwmon-feature", |
| 50 | $chip); |
| 51 | |
| 52 | #If chip didn't have hwmon units, it isn't hwmon enabled. |
| 53 | next unless (scalar @hwmonUnits > 0); |
| 54 | |
| 55 | my %entry; |
| 56 | $entry{type} = I2C_TYPE; |
| 57 | $entry{name} = lc $g_targetObj->getInstanceName($chip); |
Matt Spinler | c79b21f | 2017-01-09 15:45:14 -0600 | [diff] [blame^] | 58 | getHwmonAttributes(\@hwmonUnits, \%entry); |
| 59 | getI2CAttributes($i2c, \%entry); |
Matt Spinler | 8df7be8 | 2017-01-09 15:42:05 -0600 | [diff] [blame] | 60 | |
| 61 | push @$hwmon, { %entry }; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
Matt Spinler | c79b21f | 2017-01-09 15:45:14 -0600 | [diff] [blame^] | 66 | #Reads the hwmon related attributes from the HWMON_FEATURE |
| 67 | #complex attribute and adds them to the hash. |
| 68 | sub getHwmonAttributes |
| 69 | { |
| 70 | my ($units, $entry) = @_; |
| 71 | my %hwmonFeatures; |
| 72 | |
| 73 | for my $unit (@$units) { |
| 74 | |
| 75 | #The hwmon name, like 'in1', 'temp1', 'fan1', etc |
| 76 | my $hwmon = $g_targetObj->getAttributeField($unit, |
| 77 | "HWMON_FEATURE", |
| 78 | "HWMON_NAME"); |
| 79 | |
| 80 | #The useful name for this feature, like 'ambient' |
| 81 | my $name = $g_targetObj->getAttributeField($unit, |
| 82 | "HWMON_FEATURE", |
| 83 | "DESCRIPTIVE_NAME"); |
| 84 | $hwmonFeatures{$hwmon}{label} = $name; |
| 85 | |
| 86 | #Thresholds are optional, ignore if NA |
| 87 | my $warnHigh = $g_targetObj->getAttributeField($unit, |
| 88 | "HWMON_FEATURE", |
| 89 | "WARN_HIGH"); |
| 90 | if (($warnHigh ne "") && ($warnHigh ne "NA")) { |
| 91 | $hwmonFeatures{$hwmon}{warnhigh} = $warnHigh; |
| 92 | } |
| 93 | |
| 94 | my $warnLow = $g_targetObj->getAttributeField($unit, |
| 95 | "HWMON_FEATURE", |
| 96 | "WARN_LOW"); |
| 97 | if (($warnLow ne "") && ($warnLow ne "NA")) { |
| 98 | $hwmonFeatures{$hwmon}{warnlow} = $warnLow; |
| 99 | } |
| 100 | |
| 101 | my $critHigh = $g_targetObj->getAttributeField($unit, |
| 102 | "HWMON_FEATURE", |
| 103 | "CRIT_HIGH"); |
| 104 | if (($critHigh ne "") && ($critHigh ne "NA")) { |
| 105 | $hwmonFeatures{$hwmon}{crithigh} = $critHigh; |
| 106 | } |
| 107 | |
| 108 | my $critLow = $g_targetObj->getAttributeField($unit, |
| 109 | "HWMON_FEATURE", |
| 110 | "CRIT_LOW"); |
| 111 | if (($critLow ne "") && ($critHigh ne "NA")) { |
| 112 | $hwmonFeatures{$hwmon}{critlow} = $critLow; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | $entry->{hwmon} = { %hwmonFeatures }; |
| 117 | } |
| 118 | |
| 119 | |
Matt Spinler | 8df7be8 | 2017-01-09 15:42:05 -0600 | [diff] [blame] | 120 | #Reads the I2C attributes for the chip and adds them to the hash. |
| 121 | #This includes the i2C address, and register base address and |
| 122 | #offset for the I2C bus the chip is on. |
| 123 | sub getI2CAttributes |
| 124 | { |
| 125 | my ($i2c, $entry) = @_; |
| 126 | |
| 127 | #The address comes from the destination unit, and needs |
| 128 | #to be the 7 bit value in hex without the 0x. |
| 129 | my $addr = $g_targetObj->getAttribute($i2c->{DEST}, "I2C_ADDRESS"); |
| 130 | $addr = hex($addr) >> 1; |
| 131 | $entry->{addr} = sprintf("%x", $addr); |
| 132 | |
| 133 | #The reg base address and offset may be optional depending on |
| 134 | #the BMC chip type. We'll check later if it's required but missing. |
| 135 | if (!$g_targetObj->isBadAttribute($i2c->{SOURCE}, "REG_BASE_ADDRESS")) { |
| 136 | my $addr = $g_targetObj->getAttribute($i2c->{SOURCE}, |
| 137 | "REG_BASE_ADDRESS"); |
| 138 | $entry->{regBaseAddress} = sprintf("%x", hex($addr)); |
| 139 | } |
| 140 | |
| 141 | if (!$g_targetObj->isBadAttribute($i2c->{SOURCE}, "REG_OFFSET")) { |
| 142 | my $offset = $g_targetObj->getAttribute($i2c->{SOURCE}, |
| 143 | "REG_OFFSET"); |
| 144 | $entry->{regOffset} = sprintf("%x", hex($offset)); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | sub printUsage |
| 150 | { |
| 151 | print "$0 -x [XML filename]\n"; |
| 152 | exit(1); |
| 153 | } |