blob: fa09062bd95d999e0275fd637d9af1d81a24709e [file] [log] [blame]
Matt Spinler8df7be82017-01-09 15:42:05 -06001#!/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
7use strict;
8use warnings;
9
10use mrw::Targets;
11use mrw::Util;
12use Getopt::Long;
13
14use constant {
15 I2C_TYPE => "i2c"
16};
17
18my $serverwizFile;
19my @hwmon;
20
21GetOptions("x=s" => \$serverwizFile) or printUsage();
22
23if (not defined $serverwizFile) {
24 printUsage();
25}
26
27my $g_targetObj = Targets->new;
28$g_targetObj->loadXML($serverwizFile);
29
30my $bmc = Util::getBMCTarget($g_targetObj);
31
32getI2CSensors($bmc, \@hwmon);
33
34exit 0;
35
36
37#Returns an array of hashes that represent hwmon enabled I2C sensors.
38sub 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 Spinlerc79b21f2017-01-09 15:45:14 -060058 getHwmonAttributes(\@hwmonUnits, \%entry);
59 getI2CAttributes($i2c, \%entry);
Matt Spinler8df7be82017-01-09 15:42:05 -060060
61 push @$hwmon, { %entry };
62 }
63}
64
65
Matt Spinlerc79b21f2017-01-09 15:45:14 -060066#Reads the hwmon related attributes from the HWMON_FEATURE
67#complex attribute and adds them to the hash.
68sub 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 Spinler8df7be82017-01-09 15:42:05 -0600120#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.
123sub 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
149sub printUsage
150{
151 print "$0 -x [XML filename]\n";
152 exit(1);
153}