meta-ibm:ncsi: ensure eth speeds set to 1 gigabit

IBM has run into some issues recently with certain network
configurations where eth1 is not getting set to 1 gigabit. This can
cause issues in scenarios where a network environment is running on 1
gigabit minimum-speed network switches.

Removing link-local support from eth1 seemed to aggravated this.
link-local is only on eth0 to avoid subnet issues. But without
link-local, the systemd call, systemd-networkd-wait-online, does not
always find an active eth1 so it never runs the command to force eth1 up
to 1 gigabit.

As far as we can tell, there's no appropriate mapping within
systemd-networkd-wait-online for it to know when the NCSI stack is
appropriately up on a network interface (allowing the 1 gigabit speed to
be set). Therefore the solution taken in this commit is to just keep
retrying for a period of time.

Tested:
- Verified new script ran and set eth ports as expected

Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
Change-Id: I6737e208b59830262f51a8e30c78a97fc698e4fd
diff --git a/meta-ibm/recipes-phosphor/network/network/ncsi-linkspeed@.service b/meta-ibm/recipes-phosphor/network/network/ncsi-linkspeed@.service
index 00bce85..26737b9 100644
--- a/meta-ibm/recipes-phosphor/network/network/ncsi-linkspeed@.service
+++ b/meta-ibm/recipes-phosphor/network/network/ncsi-linkspeed@.service
@@ -3,10 +3,9 @@
 After=network.target
 
 [Service]
+TimeoutSec=90s
 Restart=no
-ExecCondition=/lib/systemd/systemd-networkd-wait-online --timeout 30 -i %i
-# package 0, channel 0, oem command, see Intel I210 datasheet section 10.6.3.10.1
-ExecStart=/usr/libexec/ncsi-netlink-ifindex %i -p 0 -c 0 -o 00000157200001
+ExecStart=/usr/libexec/ncsi-wait-and-set-speed %i
 SyslogIdentifier=nsci-linkspeed
 Type=oneshot
 
diff --git a/meta-ibm/recipes-phosphor/network/network/ncsi-wait-and-set-speed b/meta-ibm/recipes-phosphor/network/network/ncsi-wait-and-set-speed
new file mode 100644
index 0000000..e435831
--- /dev/null
+++ b/meta-ibm/recipes-phosphor/network/network/ncsi-wait-and-set-speed
@@ -0,0 +1,37 @@
+#!/bin/bash
+set -e
+
+# This script tries to periodically set the passed in network interface to
+# 1 gigabit. It will run until it is successful or it hits its max retries.
+# This script is necessary because there is no clear indication from the kernel
+# NCSI stack when it is in a proper state for this speed setting to succeed
+
+# This script expects 1 parameter
+# - Network interface to configure (i.e. eth0, eth1, ...)
+
+if [ $# -ne 1 ]; then
+    echo "Required network interface not provided"
+    exit 1
+fi
+
+netIface=$1
+
+# 60s total: 12 tries with 5s sleeps
+for i in {1..12}
+do
+    echo "attempt number $i: setting $netIface to 1 gigabit"
+    rc=0
+    # package 0, channel 0, oem command, see Intel I210 datasheet section 10.6.3.10.1
+    /usr/libexec/ncsi-netlink-ifindex "$netIface" -p 0 -c 0 -o 00000157200001 || rc=$?
+    if [ $rc -ne 0 ]; then
+        echo "error code is $rc setting $netIface to 1 gigabit, sleep and retry"
+        sleep 5
+    else
+        echo "success setting $netIface to 1 gigabit"
+        exit 0
+    fi
+
+done
+
+echo "ERROR: all retry attempts exhausted, unable to configure $netIface to 1 gigabit"
+exit 1
\ No newline at end of file
diff --git a/meta-ibm/recipes-phosphor/network/phosphor-network_%.bbappend b/meta-ibm/recipes-phosphor/network/phosphor-network_%.bbappend
index be233f8..eac0503 100644
--- a/meta-ibm/recipes-phosphor/network/phosphor-network_%.bbappend
+++ b/meta-ibm/recipes-phosphor/network/phosphor-network_%.bbappend
@@ -2,6 +2,8 @@
 
 inherit obmc-phosphor-systemd
 
+RDEPENDS:${PN} += "bash"
+
 OBMC_NETWORK_INTERFACES ?= "eth0"
 OBMC_NETWORK_INTERFACES:append:p10bmc = " eth1"
 
@@ -9,6 +11,7 @@
 LINKSPEED_TMPL = "ncsi-linkspeed@.service"
 
 SRC_URI += " file://ncsi-netlink-ifindex"
+SRC_URI += " file://ncsi-wait-and-set-speed"
 SRC_URI:append:ibm-ac-server = " file://${FAILOVER_TMPL}"
 SRC_URI:append:p10bmc = " file://${LINKSPEED_TMPL}"
 
@@ -24,6 +27,7 @@
 SYSTEMD_LINK:${PN}:append:p10bmc = "${@compose_list(d, 'LINKSPEED_FMT', 'OBMC_NETWORK_INTERFACES')}"
 
 FILES:${PN} += "${libexecdir}/ncsi-netlink-ifindex"
+FILES:${PN} += "${libexecdir}/ncsi-wait-and-set-speed"
 FILES:${PN} += "${datadir}/network/*.json"
 
 PACKAGECONFIG:append = " sync-mac"
@@ -37,6 +41,7 @@
 do_install:append() {
     install -d ${D}${libexecdir}
     install -m 0755 ${WORKDIR}/ncsi-netlink-ifindex ${D}${libexecdir}
+    install -m 0755 ${WORKDIR}/ncsi-wait-and-set-speed ${D}${libexecdir}
 }
 
 SRC_URI:append:p10bmc = " file://inventory-object-map.json"