meta-facebook: yosemite4: Control NIC main/aux mode

Support NIC 1 switch to main mode when one of slot1 or slot2 Host's
ACPI power status reach to S3. NIC 2 targets to slot3 and slot4,
NIC 3 targets to slot5 and slot6, etc.

Support NIC 1 switch to aux mode when slot1 and slot2 Host's ACPI
power status are S4. NIC 3 targets to slot3 and slot4,
NIC 3 targets to slot5 and slot6, etc.

Tested:
- Power off corresponding Hosts and check the NIC's power status.
- Power on one of the corresponding Host and check the NIC's power
  status.
  - All NICs have been tested.

Change-Id: I2e76ce3d18f81328789bb9a5d5be4025832b21e7
Signed-off-by: Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor-config.bbappend b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor-config.bbappend
new file mode 100644
index 0000000..860e605
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor-config.bbappend
@@ -0,0 +1 @@
+PHOSPHOR_DBUS_MONITOR_CONFIGS:append = " power-saving-policy"
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor/switch-nic-main-or-aux-mode b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor/switch-nic-main-or-aux-mode
new file mode 100644
index 0000000..d82733e
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor/switch-nic-main-or-aux-mode
@@ -0,0 +1,34 @@
+#!/bin/sh
+set -e
+
+SLOT=$1
+MODE=$2
+GPIO_CHIP=$(basename "/sys/bus/i2c/devices/28-0024/"*gpiochip*)
+
+if [ "$MODE" = "main" ]
+then
+    gpio_val=1
+elif [ "$MODE" = "aux" ]
+then
+    gpio_val=0
+else
+    echo "Mode must be either main or aux instead of \"$MODE\"."
+    exit 255
+fi
+
+if test -z "$SLOT"
+then
+    echo "Slot number(1st parameter) is required."
+    exit 255
+elif [ "$SLOT" -gt 4 ]
+then
+    echo "Slot number must between 1~4."
+    exit 255
+fi
+
+# nic1~4 = gpio pin 4~7
+gpio_pin=$(( SLOT + 3 ))
+
+gpioset "$GPIO_CHIP" $gpio_pin=$gpio_val
+
+exit 0
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor/switch-nic-to-aux-mode@.service b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor/switch-nic-to-aux-mode@.service
new file mode 100644
index 0000000..f618988
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor/switch-nic-to-aux-mode@.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Switch nic %i to aux mode
+
+[Service]
+Type=oneshot
+ExecStart=switch-nic-main-or-aux-mode %i aux
+
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor/switch-nic-to-main-mode@.service b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor/switch-nic-to-main-mode@.service
new file mode 100644
index 0000000..d468591
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor/switch-nic-to-main-mode@.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Switch nic %i to main mode
+
+[Service]
+Type=oneshot
+ExecStart=switch-nic-main-or-aux-mode %i main
+
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor_%.bbappend b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor_%.bbappend
new file mode 100644
index 0000000..4da9e51
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/phosphor-dbus-monitor_%.bbappend
@@ -0,0 +1,17 @@
+FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
+
+inherit obmc-phosphor-systemd
+
+SRC_URI:append = " \
+    file://switch-nic-main-or-aux-mode \
+"
+
+SYSTEMD_SERVICE:${PN}:append = " \
+    switch-nic-to-aux-mode@.service \
+    switch-nic-to-main-mode@.service \
+"
+
+do_install:append() {
+    install -d ${D}${bindir}
+    install -m 0755 ${WORKDIR}/switch-nic-main-or-aux-mode ${D}${bindir}/switch-nic-main-or-aux-mode
+}
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy.bb b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy.bb
new file mode 100644
index 0000000..6855fe0
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy.bb
@@ -0,0 +1,31 @@
+SUMMARY = "Power saving policy configurations for meta-yosemite4 machines"
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+inherit allarch
+inherit phosphor-dbus-monitor
+
+FILESEXTRAPATHS:prepend := "${THISDIR}/${BPN}:"
+
+SRC_URI:append = " \
+    file://nic-1.yaml \
+    file://nic-2.yaml \
+    file://nic-3.yaml \
+    file://nic-4.yaml \
+"
+
+do_install() {
+    install -D ${WORKDIR}/nic-1.yaml ${D}${config_dir}/nic-1.yaml
+    install -D ${WORKDIR}/nic-2.yaml ${D}${config_dir}/nic-2.yaml
+    install -D ${WORKDIR}/nic-3.yaml ${D}${config_dir}/nic-3.yaml
+    install -D ${WORKDIR}/nic-4.yaml ${D}${config_dir}/nic-4.yaml
+}
+
+FILES:${PN}:append = " \
+    ${config_dir}/nic-1.yaml \
+    ${config_dir}/nic-2.yaml \
+    ${config_dir}/nic-3.yaml \
+    ${config_dir}/nic-4.yaml \
+"
+
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-1.yaml b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-1.yaml
new file mode 100644
index 0000000..827e689
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-1.yaml
@@ -0,0 +1,79 @@
+- name: nic 1 current host state path group
+  class: group
+  group: path
+  members:
+      - meta: PATH
+        path: /xyz/openbmc_project/state/host1
+      - meta: PATH
+        path: /xyz/openbmc_project/state/host2
+
+- name: current host state property
+  class: group
+  group: property
+  type: string
+  members:
+      - interface: xyz.openbmc_project.State.Host
+        meta: PROPERTY
+        property: CurrentHostState
+
+- name: nic 1 main mode monitor
+  class: watch
+  watch: property
+  paths: nic 1 current host state path group
+  properties: current host state property
+  callback: check if fulfill nic main mode requirement
+
+- name: nic 1 aux mode monitor
+  class: watch
+  watch: property
+  paths: nic 1 current host state path group
+  properties: current host state property
+  callback: check if fulfill nic aux mode requirement
+
+- name: check if fulfill nic main mode requirement
+  class: condition
+  condition: count
+  paths: nic 1 current host state path group
+  properties: current host state property
+  callback: switch nic 1 to main mode
+  countop: ">="
+  countbound: 1
+  op: "=="
+  bound: xyz.openbmc_project.State.Host.HostState.Running
+
+- name: check if fulfill nic aux mode requirement
+  class: condition
+  condition: count
+  paths: nic 1 current host state path group
+  properties: current host state property
+  callback: switch nic 1 to aux mode
+  countop: "=="
+  countbound: 2
+  op: "=="
+  bound: xyz.openbmc_project.State.Host.HostState.Off
+
+- name: switch nic 1 to main mode
+  class: callback
+  callback: method
+  service: org.freedesktop.systemd1
+  path: /org/freedesktop/systemd1
+  interface: org.freedesktop.systemd1.Manager
+  method: StartUnit
+  args:
+      - value: switch-nic-to-main-mode@1.service
+        type: string
+      - value: replace
+        type: string
+
+- name: switch nic 1 to aux mode
+  class: callback
+  callback: method
+  service: org.freedesktop.systemd1
+  path: /org/freedesktop/systemd1
+  interface: org.freedesktop.systemd1.Manager
+  method: StartUnit
+  args:
+      - value: switch-nic-to-aux-mode@1.service
+        type: string
+      - value: replace
+        type: string
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-2.yaml b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-2.yaml
new file mode 100644
index 0000000..823f71a
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-2.yaml
@@ -0,0 +1,79 @@
+- name: nic 2 current host state path group
+  class: group
+  group: path
+  members:
+      - meta: PATH
+        path: /xyz/openbmc_project/state/host3
+      - meta: PATH
+        path: /xyz/openbmc_project/state/host4
+
+- name: current host state property
+  class: group
+  group: property
+  type: string
+  members:
+      - interface: xyz.openbmc_project.State.Host
+        meta: PROPERTY
+        property: CurrentHostState
+
+- name: nic 2 main mode monitor
+  class: watch
+  watch: property
+  paths: nic 2 current host state path group
+  properties: current host state property
+  callback: check if fulfill nic main mode requirement
+
+- name: nic 2 aux mode monitor
+  class: watch
+  watch: property
+  paths: nic 2 current host state path group
+  properties: current host state property
+  callback: check if fulfill nic aux mode requirement
+
+- name: check if fulfill nic main mode requirement
+  class: condition
+  condition: count
+  paths: nic 2 current host state path group
+  properties: current host state property
+  callback: switch nic 2 to main mode
+  countop: ">="
+  countbound: 1
+  op: "=="
+  bound: xyz.openbmc_project.State.Host.HostState.Running
+
+- name: check if fulfill nic aux mode requirement
+  class: condition
+  condition: count
+  paths: nic 2 current host state path group
+  properties: current host state property
+  callback: switch nic 2 to aux mode
+  countop: "=="
+  countbound: 2
+  op: "=="
+  bound: xyz.openbmc_project.State.Host.HostState.Off
+
+- name: switch nic 2 to main mode
+  class: callback
+  callback: method
+  service: org.freedesktop.systemd1
+  path: /org/freedesktop/systemd1
+  interface: org.freedesktop.systemd1.Manager
+  method: StartUnit
+  args:
+      - value: switch-nic-to-main-mode@2.service
+        type: string
+      - value: replace
+        type: string
+
+- name: switch nic 2 to aux mode
+  class: callback
+  callback: method
+  service: org.freedesktop.systemd1
+  path: /org/freedesktop/systemd1
+  interface: org.freedesktop.systemd1.Manager
+  method: StartUnit
+  args:
+      - value: switch-nic-to-aux-mode@2.service
+        type: string
+      - value: replace
+        type: string
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-3.yaml b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-3.yaml
new file mode 100644
index 0000000..7b207a8
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-3.yaml
@@ -0,0 +1,79 @@
+- name: nic 3 current host state path group
+  class: group
+  group: path
+  members:
+      - meta: PATH
+        path: /xyz/openbmc_project/state/host5
+      - meta: PATH
+        path: /xyz/openbmc_project/state/host6
+
+- name: current host state property
+  class: group
+  group: property
+  type: string
+  members:
+      - interface: xyz.openbmc_project.State.Host
+        meta: PROPERTY
+        property: CurrentHostState
+
+- name: nic 3 main mode monitor
+  class: watch
+  watch: property
+  paths: nic 3 current host state path group
+  properties: current host state property
+  callback: check if fulfill nic main mode requirement
+
+- name: nic 3 aux mode monitor
+  class: watch
+  watch: property
+  paths: nic 3 current host state path group
+  properties: current host state property
+  callback: check if fulfill nic aux mode requirement
+
+- name: check if fulfill nic main mode requirement
+  class: condition
+  condition: count
+  paths: nic 3 current host state path group
+  properties: current host state property
+  callback: switch nic 3 to main mode
+  countop: ">="
+  countbound: 1
+  op: "=="
+  bound: xyz.openbmc_project.State.Host.HostState.Running
+
+- name: check if fulfill nic aux mode requirement
+  class: condition
+  condition: count
+  paths: nic 3 current host state path group
+  properties: current host state property
+  callback: switch nic 3 to aux mode
+  countop: "=="
+  countbound: 2
+  op: "=="
+  bound: xyz.openbmc_project.State.Host.HostState.Off
+
+- name: switch nic 3 to main mode
+  class: callback
+  callback: method
+  service: org.freedesktop.systemd1
+  path: /org/freedesktop/systemd1
+  interface: org.freedesktop.systemd1.Manager
+  method: StartUnit
+  args:
+      - value: switch-nic-to-main-mode@3.service
+        type: string
+      - value: replace
+        type: string
+
+- name: switch nic 3 to aux mode
+  class: callback
+  callback: method
+  service: org.freedesktop.systemd1
+  path: /org/freedesktop/systemd1
+  interface: org.freedesktop.systemd1.Manager
+  method: StartUnit
+  args:
+      - value: switch-nic-to-aux-mode@3.service
+        type: string
+      - value: replace
+        type: string
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-4.yaml b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-4.yaml
new file mode 100644
index 0000000..df8bd42
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/dbus/power-saving-policy/nic-4.yaml
@@ -0,0 +1,79 @@
+- name: nic 4 current host state path group
+  class: group
+  group: path
+  members:
+      - meta: PATH
+        path: /xyz/openbmc_project/state/host7
+      - meta: PATH
+        path: /xyz/openbmc_project/state/host8
+
+- name: current host state property
+  class: group
+  group: property
+  type: string
+  members:
+      - interface: xyz.openbmc_project.State.Host
+        meta: PROPERTY
+        property: CurrentHostState
+
+- name: nic 4 main mode monitor
+  class: watch
+  watch: property
+  paths: nic 4 current host state path group
+  properties: current host state property
+  callback: check if fulfill nic main mode requirement
+
+- name: nic 4 aux mode monitor
+  class: watch
+  watch: property
+  paths: nic 4 current host state path group
+  properties: current host state property
+  callback: check if fulfill nic aux mode requirement
+
+- name: check if fulfill nic main mode requirement
+  class: condition
+  condition: count
+  paths: nic 4 current host state path group
+  properties: current host state property
+  callback: switch nic 4 to main mode
+  countop: ">="
+  countbound: 1
+  op: "=="
+  bound: xyz.openbmc_project.State.Host.HostState.Running
+
+- name: check if fulfill nic aux mode requirement
+  class: condition
+  condition: count
+  paths: nic 4 current host state path group
+  properties: current host state property
+  callback: switch nic 4 to aux mode
+  countop: "=="
+  countbound: 2
+  op: "=="
+  bound: xyz.openbmc_project.State.Host.HostState.Off
+
+- name: switch nic 4 to main mode
+  class: callback
+  callback: method
+  service: org.freedesktop.systemd1
+  path: /org/freedesktop/systemd1
+  interface: org.freedesktop.systemd1.Manager
+  method: StartUnit
+  args:
+      - value: switch-nic-to-main-mode@4.service
+        type: string
+      - value: replace
+        type: string
+
+- name: switch nic 4 to aux mode
+  class: callback
+  callback: method
+  service: org.freedesktop.systemd1
+  path: /org/freedesktop/systemd1
+  interface: org.freedesktop.systemd1.Manager
+  method: StartUnit
+  args:
+      - value: switch-nic-to-aux-mode@4.service
+        type: string
+      - value: replace
+        type: string
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/images/fb-yosemite4-phosphor-image.inc b/meta-facebook/meta-yosemite4/recipes-phosphor/images/fb-yosemite4-phosphor-image.inc
index 79d109c..fe7a367 100644
--- a/meta-facebook/meta-yosemite4/recipes-phosphor/images/fb-yosemite4-phosphor-image.inc
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/images/fb-yosemite4-phosphor-image.inc
@@ -22,3 +22,7 @@
     phosphor-gpio-monitor \
 "
 
+# This enables the feature of dbus-monitor which defined in
+# `meta-phosphor/classes/obmc-phosphor-images.bbclass`
+IMAGE_FEATURES:append = " obmc-dbus-monitor"
+