callouts: refactor: merge i2c and fsi callouts

Merge gen_i2c_callouts.pl and gen_proc_fsi_callouts.pl to a single
gen_callouts.pl.

The idea as to have a unified output generated for callouts,
irrespective of bus type. The output will be in YAML, as follows:

<sysfs path> : <Inventory path to be called out>

This will simplify usage for the FRU management code, as well as it
should be easier to support other bus types.

Change-Id: I8793be1207d905af1eb83b5b04383da39efbe02f
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/gen_callouts.pl b/gen_callouts.pl
new file mode 100755
index 0000000..7e0b9e1
--- /dev/null
+++ b/gen_callouts.pl
@@ -0,0 +1,127 @@
+#! /usr/bin/perl
+use strict;
+use warnings;
+
+
+use mrw::Targets;
+use mrw::Inventory;
+use mrw::Util;
+use Getopt::Long;
+
+
+my $mrwFile = "";
+my $outFile = "";
+
+
+GetOptions(
+"m=s" => \$mrwFile,
+"o=s" => \$outFile,
+)
+or printUsage();
+
+
+if (($mrwFile eq "") or ($outFile eq ""))
+{
+    printUsage();
+}
+
+
+# Load system MRW
+my $targets = Targets->new;
+$targets->loadXML($mrwFile);
+
+
+# Load inventory
+my @inventory = Inventory::getInventory($targets);
+
+
+# paths
+my $i2cPath = "/sys/devices/platform/ahb/ahb:apb/1e78a000.i2c/i2c-<port>/i2c-<port>/<port>-00<address>";
+my $fsiMasterPath = "/sys/devices/platform/fsi-master/slave\@00:00";
+my $fsiSlavePath = "/sys/devices/hub\@00/slave\@<link>:00";
+
+
+open(my $fh, '>', $outFile) or die "Could not open file '$outFile' $!";
+
+genI2CCallouts();
+genProcFSICallouts();
+
+close $fh;
+
+
+sub genI2CCallouts
+{
+    my $bmc = Util::getBMCTarget($targets);
+    my $connections = $targets->findConnections($bmc, "I2C");
+    # hash of arrays - {I2C master port : list of connected slave Targets}
+    my %masters;
+
+    for my $i2c (@{$connections->{CONN}})
+    {
+        my $master = $i2c->{SOURCE};
+        my $port = $targets->getAttribute($master,"I2C_PORT");
+        $port = Util::adjustI2CPort($port);
+        my $slave = $i2c->{DEST};
+        push(@{$masters{$port}}, $slave);
+    }
+
+    for my $m (keys %masters)
+    {
+        for my $s(@{$masters{$m}})
+        {
+            my $addr = $targets->getAttribute($s,"I2C_ADDRESS");
+            $addr = Util::adjustI2CAddress(hex($addr));
+            $addr = substr $addr, 2; # strip 0x
+            my $path = $i2cPath;
+            $path =~ s/<port>/$m/g;
+            $path =~ s/<address>/$addr/g;
+            print $fh $path.": ";
+            my $fru = Util::getEnclosingFru($targets, $s);
+            print $fh Util::getObmcName(\@inventory, $fru)."\n";
+        }
+    }
+}
+
+
+sub genProcFSICallouts
+{
+    my @procs;
+    for my $target (keys %{$targets->getAllTargets()})
+    {
+        if ($targets->getType($target) eq "PROC")
+        {
+            push @procs, $target;
+        }
+    }
+
+    for my $proc (@procs)
+    {
+        my $connections = $targets->findConnections($proc, "FSIM");
+        if ("" ne $connections)
+        {
+            # This is a master processor
+            my $path = $fsiMasterPath; # revisit on a multinode system
+            my $fru = Util::getEnclosingFru($targets, $proc);
+            print $fh $path.": ".Util::getObmcName(\@inventory, $fru);
+            for my $fsi (@{$connections->{CONN}})
+            {
+                my $master = $fsi->{SOURCE};
+                my $slave = $fsi->{DEST};
+                my $link = $targets->getAttribute($master, "FSI_LINK");
+                $link = substr $link, 2; # strip 0x
+                my $fru = Util::getEnclosingFru($targets, $slave);
+                $path = $fsiSlavePath;
+                $path =~ s/<link>/$link/g;
+                print $fh "\n".$path.": ".Util::getObmcName(\@inventory, $fru);
+            }
+        }
+    }
+}
+
+
+sub printUsage
+{
+    print "
+    $0 -m [MRW file] -o [Output filename]\n";
+    exit(1);
+}