meta-bletchley: add bletchley-net-util

Add bletchley-net-util for link status checking and port registers
dumping.

Test results:

- show link status
```
root@bmc:~# bletchley-net-util --link-st
========================================
Link Status
========================================
Switch:
  Port 10 (BMC): up
  Port 3 (SLED1): down
  Port 2 (SLED2): down
  Port 1 (SLED3): down
  Port 7 (SLED4): up
  Port 6 (SLED5): down
  Port 5 (SLED6): down

Retimer:
  Inner Port (XFI): up
  Outer Port (SFI): up
========================================
```

- show port registers dumping
```
root@bmc:~# bletchley-net-util --port-reg-dump
========================================
Port 10 (BMC)
****************************************
[00]: 0F4D
[01]: 0003
...
[1E]: 0000
[1F]: 005D
========================================

========================================
Port 3 (SLED1)
****************************************
[00]: 100F
[01]: 0003
...
[1E]: 0000
[1F]: 0000
========================================

========================================
Port 2 (SLED2)
****************************************
[00]: 100F
[01]: 0003
...
[1E]: 0000
[1F]: 0000
========================================

========================================
Port 1 (SLED3)
****************************************
[00]: 100F
[01]: 0003
...
[1E]: 0000
[1F]: 0000
========================================

========================================
Port 7 (SLED4)
****************************************
[00]: DE4F
[01]: 0003
...
[1E]: 0000
[1F]: 0166
========================================

========================================
Port 6 (SLED5)
****************************************
[00]: 100F
[01]: 0003
...
[1E]: 0000
[1F]: 0000
========================================

========================================
Port 5 (SLED6)
****************************************
[00]: 100F
[01]: 0003
...
[1E]: 0000
[1F]: 0000
========================================
```

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: Ic4464c0b466b4d6c02246d013fc4d99ca857cbdc
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-tool_0.1.bb b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-tool_0.1.bb
index 3b7ba82..afe3791 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-tool_0.1.bb
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-tool_0.1.bb
@@ -10,6 +10,7 @@
     file://bletchley-system-state-init \
     file://bletchley-system-state-init@.service \
     file://bletchley-usbmux-util \
+    file://bletchley-net-util \
     "
 
 do_install() {
@@ -18,6 +19,7 @@
 
     install -d ${D}${bindir}
     install -m 0755 ${WORKDIR}/bletchley-usbmux-util ${D}${bindir}
+    install -m 0755 ${WORKDIR}/bletchley-net-util ${D}${bindir}
 }
 
 
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-net-util b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-net-util
new file mode 100644
index 0000000..4127b19
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-net-util
@@ -0,0 +1,102 @@
+#!/bin/bash
+
+declare -a PORT_NUM_MAP=(10 3 2 1 7 6 5)
+declare -a PORT_NAME_MAP=(BMC SLED1 SLED2 SLED3 SLED4 SLED5 SLED6)
+SWITCH_MDIO_BUS="1e650000.mdio-1"
+
+INNER_PORT_DEV_ID="4"
+OUTER_PORT_DEV_ID="3"
+RETIMER_MDIO_BUS="1e650018.mdio-1"
+
+get_switch_port_link_st()
+{
+    local port_phy_id=$1
+
+    if PORT_ST_VAL="$(mdio "$SWITCH_MDIO_BUS" phy "$port_phy_id" 0x00)"; then
+        PORT_ST_VAL="${PORT_ST_VAL:2}"
+        if [ "$((16#$PORT_ST_VAL & 16#0800))" -eq "0" ]; then
+            PORT_ST="down"
+        else
+            PORT_ST="up"
+        fi
+    else
+        PORT_ST="mdio failed"
+    fi
+    echo "${PORT_ST}"
+}
+
+get_retimer_port_link_st()
+{
+    local port_dev_id=$1
+
+    if PORT_ST_VAL="$(mdio "$RETIMER_MDIO_BUS" mmd 0:"$port_dev_id" 0x9002)"; then
+        PORT_ST_VAL="${PORT_ST_VAL:2}"
+        if [ "$((16#$PORT_ST_VAL & 16#0004))" -eq "0" ]; then
+            PORT_ST="down"
+        else
+            PORT_ST="up"
+        fi
+    else
+        PORT_ST="mdio failed"
+    fi
+    echo "${PORT_ST}"
+}
+
+get_port_link_st()
+{
+    printf "========================================\n"
+    printf "Link Status\n"
+    printf "========================================\n"
+    printf "Switch:\n"
+    for port_phy in {0..6}
+    do
+        printf "  Port %d (%s): %s\n" "${PORT_NUM_MAP[port_phy]}" "${PORT_NAME_MAP[port_phy]}" "$(get_switch_port_link_st "${PORT_NUM_MAP[port_phy]}")"
+    done
+    printf "\n"
+    printf "Retimer:\n"
+    printf "  Inner Port (XFI): %s\n" "$(get_retimer_port_link_st "${INNER_PORT_DEV_ID}")"
+    printf "  Outer Port (SFI): %s\n" "$(get_retimer_port_link_st "${OUTER_PORT_DEV_ID}")"
+    printf "========================================\n\n"
+}
+
+get_switch_port_reg_dump()
+{
+    for port_phy in {0..6}
+    do
+        printf "========================================\n"
+        printf "Port %d (%s)\n" "${PORT_NUM_MAP[port_phy]}" "${PORT_NAME_MAP[port_phy]}"
+        printf "****************************************\n"
+        for reg_offset in {0..31}
+        do
+            printf "[%02X]: %04X\n" "$reg_offset" "$(mdio "$SWITCH_MDIO_BUS" phy "${PORT_NUM_MAP[port_phy]}" "$reg_offset")"
+        done
+        printf "========================================\n\n"
+    done
+}
+
+print_help()
+{
+    echo "Usage:"
+    echo "  $0 <COMMAND>"
+    echo ""
+    echo "COMMAND:"
+    echo "    --link-st"
+    echo "        show link status"
+    echo ""
+    echo "    --port-reg-dump"
+    echo "        port registers dump"
+    echo ""
+}
+
+ACTION_CMD="$1"
+
+if [[ ${ACTION_CMD} =~ -h|--help ]]; then
+    print_help
+elif [ "${ACTION_CMD}" = "--link-st" ]; then
+    get_port_link_st
+elif [ "${ACTION_CMD}" = "--port-reg-dump" ]; then
+    get_switch_port_reg_dump
+else
+    echo "Unknow command: $ACTION_CMD"
+    print_help
+fi