meta-ampere: usbnet: implement using usb-ctrl

Change to implement virtual USB Ethernet using phosphor-misc's usb-ctrl.

Tested:
1. Boot BMC to Linux. Login and check if usb0 ethernet interface exists.
2. Power ON the Host. Check if usb0 automatically sets its IP to
192.168.0.10
3. Restart the ampere-usbnet.service service. Check if no error happens.

Signed-off-by: Thang Q. Nguyen <thang@os.amperecomputing.com>
Change-Id: I4de722a691a112db851b6dda0a7821b9f78b4d72
diff --git a/meta-ampere/meta-common/recipes-ac01/usbnet/ampere-usbnet.bb b/meta-ampere/meta-common/recipes-ac01/usbnet/ampere-usbnet.bb
deleted file mode 100644
index 3045fa5..0000000
--- a/meta-ampere/meta-common/recipes-ac01/usbnet/ampere-usbnet.bb
+++ /dev/null
@@ -1,30 +0,0 @@
-SUMMARY = "Ampere Computing LLC Add Ethernet over USB gadget device"
-DESCRIPTION = "Add Ethernet over USB gadget device for Ampere systems"
-PR = "r1"
-
-LICENSE = "Apache-2.0"
-S = "${WORKDIR}"
-
-LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
-
-inherit systemd
-inherit obmc-phosphor-systemd
-
-DEPENDS = "systemd"
-RDEPENDS:${PN} = "bash"
-
-SYSTEMD_PACKAGES = "${PN}"
-SYSTEMD_SERVICE:${PN} = " \
-        ampere_add_usbnet_gadget.service \
-        "
-
-SRC_URI += "file://00-bmc-usb0.network"
-SRC_URI += "file://ampere_add_usbnet_gadget.sh"
-
-do_install:append() {
-        install -d ${D}${sbindir}
-        install -d ${D}/etc/systemd/network
-        install -m 744 ${WORKDIR}/ampere_add_usbnet_gadget.sh ${D}${sbindir}/
-        install -m 644 ${WORKDIR}/00-bmc-usb0.network \
-                       ${D}/etc/systemd/network/
-}
diff --git a/meta-ampere/meta-common/recipes-ac01/usbnet/ampere-usbnet/ampere_add_usbnet_gadget.service b/meta-ampere/meta-common/recipes-ac01/usbnet/ampere-usbnet/ampere_add_usbnet_gadget.service
deleted file mode 100644
index 428263a..0000000
--- a/meta-ampere/meta-common/recipes-ac01/usbnet/ampere-usbnet/ampere_add_usbnet_gadget.service
+++ /dev/null
@@ -1,13 +0,0 @@
-[Unit]
-Description=Ampere adds Ethernet over USB gadget device
-Wants=systemd-networkd.service
-Before=systemd-networkd.service
-After=phosphor-ipmi-host.service
-
-[Service]
-ExecStart=/usr/bin/env ampere_add_usbnet_gadget.sh
-SyslogIdentifier=ampere_add_usbnet_gadget.sh
-Type=oneshot
-
-[Install]
-WantedBy=systemd-networkd.service
diff --git a/meta-ampere/meta-common/recipes-ac01/usbnet/ampere-usbnet/ampere_add_usbnet_gadget.sh b/meta-ampere/meta-common/recipes-ac01/usbnet/ampere-usbnet/ampere_add_usbnet_gadget.sh
deleted file mode 100644
index c4c2c27..0000000
--- a/meta-ampere/meta-common/recipes-ac01/usbnet/ampere-usbnet/ampere_add_usbnet_gadget.sh
+++ /dev/null
@@ -1,93 +0,0 @@
-#!/bin/sh
-
-# Add an Ethernet over USB gadget device and connect to a port of Aspeed USB
-# virtual hub. If can't find any free port on virtual hub, exit with failure.
-# If can't find the virtual hub, exit with failure.
-
-# Author: Thinh Hung Pham <thinh.pham@amperecomputing.com>
-# Signed-off-by: Chanh Nguyen <chnguyen@amperecomputing.com>
-
-UDC_SYSPATH=/sys/class/udc
-VHUB_DEVICE=1e6a0000.usb-vhub:p
-GADGET_CONFIG_SYSPATH=/sys/kernel/config/usb_gadget
-USBNET=usbnet
-# The number of port on AST2500 USB virtual hub
-NUM_PORT_USB_HUB=5
-# idVendor = 0x1d6b: Linux Foundation
-VENDORID=0x1d6b
-# idProduct = 0x0103: NCM (Ethernet) Gadget
-PRODUCTID=0x0103
-# Language code = 0x409: English – United States
-LANGUAGEID=0x409
-SERIALNUMBER=cafecafe
-MANUFACTURER=Aspeed
-FUNCTION=ecm.usb0
-
-if [ ! -d ${GADGET_CONFIG_SYSPATH} ]; then
-	# GADGET_CONFIG_SYSPATH is not exist
-	# Return 1 so that systemd knows the service failed to start
-	echo "ERROR: ${GADGET_CONFIG_SYSPATH} : doesn't exist!"
-	exit 1
-fi
-
-find_free_vhub_port(){
-	for ((i=1;i<=${NUM_PORT_USB_HUB};i++))
-	do
-		state=$(cat ${UDC_SYSPATH}/${VHUB_DEVICE}${i}/state)
-		func=$(cat ${UDC_SYSPATH}/${VHUB_DEVICE}${i}/function)
-		if [ "${state}" == "not attached" -a "${func}" == "" ]; then
-			FREEUDC=${VHUB_DEVICE}${i}
-			break
-		fi
-	done
-	if [ ${i} -eq 6 ]; then
-		# Can't find a free port
-		# Return 1 so that systemd knows the service failed to start
-		echo "ERROR: Can't find a free port !"
-		exit 1
-	fi
-}
-
-if [ -d ${GADGET_CONFIG_SYSPATH}/${USBNET} ]; then
-	cd ${GADGET_CONFIG_SYSPATH}/${USBNET}
-else
-	# Create the gadget
-	mkdir ${GADGET_CONFIG_SYSPATH}/${USBNET}
-	cd ${GADGET_CONFIG_SYSPATH}/${USBNET}
-
-	# Configure the gadget
-	echo ${VENDORID} > idVendor
-	echo ${PRODUCTID} > idProduct
-	mkdir strings/${LANGUAGEID}
-	echo ${SERIALNUMBER} > strings/${LANGUAGEID}/serialnumber
-	echo ${MANUFACTURER} > strings/${LANGUAGEID}/manufacturer
-	echo ${USBNET} > strings/${LANGUAGEID}/product
-
-	# Create the configuration
-	mkdir configs/c.1
-	mkdir configs/c.1/strings/${LANGUAGEID}
-
-	# Create the function
-	mkdir functions/${FUNCTION}
-
-	# Associate the function with its configuration
-	ln -s functions/${FUNCTION} configs/c.1
-fi
-
-# Find an available virtual hub port
-find_free_vhub_port
-
-# Enable the gadget
-echo ${FREEUDC} > UDC
-
-if [[ $? -ne 0 ]]; then
-	# End
-	cd - > /dev/null
-	# Virtual HUB is not available
-	# Return 1 so that systemd knows the service failed to start
-	exit 1
-fi
-
-# End
-cd - > /dev/null
-
diff --git a/meta-ampere/meta-common/recipes-phosphor/network/ampere-usbnet.bb b/meta-ampere/meta-common/recipes-phosphor/network/ampere-usbnet.bb
new file mode 100644
index 0000000..d1dff35
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-phosphor/network/ampere-usbnet.bb
@@ -0,0 +1,28 @@
+SUMMARY = "Enable USB ethernet"
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+DEPENDS += "systemd"
+RDEPENDS:${PN} += "libsystemd bash"
+
+inherit allarch systemd
+
+SRC_URI += "file://ampere-usbnet.service \
+            file://ampere_usbnet.sh \
+            file://00-bmc-usb0.network"
+
+do_install() {
+    install -d ${D}${systemd_unitdir}/system/
+    install -m 0644 ${WORKDIR}/ampere-usbnet.service ${D}${systemd_unitdir}/system
+
+    install -d ${D}${sysconfdir_native}/systemd/network/
+    install -m 0644 ${WORKDIR}/00-bmc-usb0.network ${D}${sysconfdir_native}/systemd/network
+
+    install -d ${D}/${sbindir}
+    install -m 755 ${WORKDIR}/ampere_usbnet.sh ${D}/${sbindir}
+}
+
+NATIVE_SYSTEMD_SUPPORT = "1"
+SYSTEMD_PACKAGES = "${PN}"
+SYSTEMD_SERVICE:${PN} = "ampere-usbnet.service"
diff --git a/meta-ampere/meta-common/recipes-ac01/usbnet/ampere-usbnet/00-bmc-usb0.network b/meta-ampere/meta-common/recipes-phosphor/network/ampere-usbnet/00-bmc-usb0.network
similarity index 100%
rename from meta-ampere/meta-common/recipes-ac01/usbnet/ampere-usbnet/00-bmc-usb0.network
rename to meta-ampere/meta-common/recipes-phosphor/network/ampere-usbnet/00-bmc-usb0.network
diff --git a/meta-ampere/meta-common/recipes-phosphor/network/ampere-usbnet/ampere-usbnet.service b/meta-ampere/meta-common/recipes-phosphor/network/ampere-usbnet/ampere-usbnet.service
new file mode 100644
index 0000000..3499543
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-phosphor/network/ampere-usbnet/ampere-usbnet.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Ampere Ethernet over USB gadget device
+After=phosphor-ipmi-host.service
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=/usr/sbin/ampere_usbnet.sh
+ExecStop=/usr/bin/usb-ctrl ecm usbnet off
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-ampere/meta-common/recipes-phosphor/network/ampere-usbnet/ampere_usbnet.sh b/meta-ampere/meta-common/recipes-phosphor/network/ampere-usbnet/ampere_usbnet.sh
new file mode 100644
index 0000000..fd2dce6
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-phosphor/network/ampere-usbnet/ampere_usbnet.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+ENV_MAC_ADDR=$(fw_printenv eth1addr)
+if [ -z "$ENV_MAC_ADDR" ]; then
+	ENV_MAC_ADDR=$(fw_printenv ethaddr)
+fi
+
+MAC_ADDR=$(echo "$ENV_MAC_ADDR" | cut -d "=" -f 2)
+
+if [ -n "$MAC_ADDR" ]; then
+	# Generate MAC Address from eth1addr using locally administered MAC
+	# https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local_(U/L_bit
+	SUBMAC=$(echo "$MAC_ADDR" | cut -d ":" -f 2-6)
+	/usr/bin/usb-ctrl ecm usbnet on "06:$SUBMAC" "02:$SUBMAC"
+else
+	/usr/bin/usb-ctrl ecm usbnet on
+fi
+
+# Use NCM (Ethernet) Gadget instead of FunctionFS Gadget
+echo 0x0103 > /sys/kernel/config/usb_gadget/usbnet/idProduct
+echo "OpenBMC usbnet Device" > /sys/kernel/config/usb_gadget/usbnet/strings/0x409/product
diff --git a/meta-ampere/meta-jade/recipes-phosphor/packagegroups/packagegroup-obmc-apps.bbappend b/meta-ampere/meta-jade/recipes-phosphor/packagegroups/packagegroup-obmc-apps.bbappend
index 7136be6..56db568 100644
--- a/meta-ampere/meta-jade/recipes-phosphor/packagegroups/packagegroup-obmc-apps.bbappend
+++ b/meta-ampere/meta-jade/recipes-phosphor/packagegroups/packagegroup-obmc-apps.bbappend
@@ -2,6 +2,7 @@
                                        webui-vue \
                                        phosphor-image-signing \
                                        phosphor-virtual-sensor \
+                                       phosphor-misc-usb-ctrl \
                                      "
 
 RDEPENDS:${PN}-inventory:append:mtjade = " \