ecbff1
From 2a73ec2f11e5dde5754260d0ce99778f35ec92cc Mon Sep 17 00:00:00 2001
ecbff1
From: Jan Synacek <jsynacek@redhat.com>
ecbff1
Date: Wed, 17 Jan 2018 10:08:08 +0100
ecbff1
Subject: [PATCH] udev: net_id add support for platform bus (ACPI, mostly
ecbff1
 arm64) devices
ecbff1
ecbff1
(cherry picked from commit c20e6de897b2378bc3f936e1e265d2d2e2450a73)
ecbff1
ecbff1
Note: There is RHEL-only code in the patch. After some discussion,
ecbff1
we only want to rename Hisilicon Network Subsystem (HNS) devices.
ecbff1
ecbff1
Resolves: #1529633
ecbff1
---
ecbff1
 src/udev/udev-builtin-net_id.c | 72 ++++++++++++++++++++++++++++++++++++++++++
ecbff1
 1 file changed, 72 insertions(+)
ecbff1
ecbff1
diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c
ecbff1
index 19e1f2631..69cee5a72 100644
ecbff1
--- a/src/udev/udev-builtin-net_id.c
ecbff1
+++ b/src/udev/udev-builtin-net_id.c
ecbff1
@@ -42,6 +42,7 @@
ecbff1
  *                                         -- PCI geographical location
ecbff1
  *   [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
ecbff1
  *                                         -- USB port number chain
ecbff1
+ *   a<vendor><model>i<instance>           -- Platform bus ACPI instance id
ecbff1
  *
ecbff1
  * All multi-function PCI devices will carry the [f<function>] number in the
ecbff1
  * device name, including the function 0 device.
ecbff1
@@ -100,6 +101,7 @@
ecbff1
 
ecbff1
 #include "udev.h"
ecbff1
 #include "fileio.h"
ecbff1
+#include "def.h"
ecbff1
 
ecbff1
 #define ONBOARD_INDEX_MAX (16*1024-1)
ecbff1
 
ecbff1
@@ -110,6 +112,7 @@ enum netname_type{
ecbff1
         NET_BCMA,
ecbff1
         NET_VIRTIO,
ecbff1
         NET_CCWGROUP,
ecbff1
+        NET_PLATFORM,
ecbff1
 };
ecbff1
 
ecbff1
 struct netnames {
ecbff1
@@ -127,6 +130,7 @@ struct netnames {
ecbff1
         char usb_ports[IFNAMSIZ];
ecbff1
         char bcma_core[IFNAMSIZ];
ecbff1
         char ccw_group[IFNAMSIZ];
ecbff1
+        char platform_path[IFNAMSIZ];
ecbff1
 };
ecbff1
 
ecbff1
 /* retrieve on-board index number and label from firmware */
ecbff1
@@ -288,6 +292,64 @@ out:
ecbff1
         return err;
ecbff1
 }
ecbff1
 
ecbff1
+#define _PLATFORM_TEST "/sys/devices/platform/vvvvPPPP"
ecbff1
+#define _PLATFORM_PATTERN4 "/sys/devices/platform/%4s%4x:%2x/net/eth%u"
ecbff1
+#define _PLATFORM_PATTERN3 "/sys/devices/platform/%3s%4x:%2x/net/eth%u"
ecbff1
+
ecbff1
+static int names_platform(struct udev_device *dev, struct netnames *names, bool test) {
ecbff1
+        struct udev_device *parent;
ecbff1
+        char vendor[5];
ecbff1
+        unsigned model, instance, ethid;
ecbff1
+        const char *syspath, *pattern, *validchars;
ecbff1
+
ecbff1
+        /* check if our direct parent is a platform device with no other bus in-between */
ecbff1
+        parent = udev_device_get_parent(dev);
ecbff1
+        if (!parent)
ecbff1
+                return -ENOENT;
ecbff1
+
ecbff1
+        if (!streq_ptr("platform", udev_device_get_subsystem(parent)))
ecbff1
+                 return -ENOENT;
ecbff1
+
ecbff1
+        syspath = udev_device_get_syspath(dev);
ecbff1
+
ecbff1
+        /* syspath is too short, to have a valid ACPI instance */
ecbff1
+        if (strlen(syspath) < sizeof _PLATFORM_TEST)
ecbff1
+                return -EINVAL;
ecbff1
+
ecbff1
+        /* Vendor ID can be either PNP ID (3 chars A-Z) or ACPI ID (4 chars A-Z and numerals) */
ecbff1
+        if (syspath[sizeof _PLATFORM_TEST - 1] == ':') {
ecbff1
+                pattern = _PLATFORM_PATTERN4;
ecbff1
+                validchars = UPPERCASE_LETTERS DIGITS;
ecbff1
+        } else {
ecbff1
+                pattern = _PLATFORM_PATTERN3;
ecbff1
+                validchars = UPPERCASE_LETTERS;
ecbff1
+        }
ecbff1
+
ecbff1
+        /* RHEL-only! */
ecbff1
+        /* We only want to rename HNS cards */
ecbff1
+        if (!startswith(syspath, "/sys/devices/platform/HISI"))
ecbff1
+                return -ENOENT;
ecbff1
+
ecbff1
+        /* Platform devices are named after ACPI table match, and instance id
ecbff1
+         * eg. "/sys/devices/platform/HISI00C2:00");
ecbff1
+         * The Vendor (3 or 4 char), followed by hexdecimal model number : instance id.
ecbff1
+         */
ecbff1
+#pragma GCC diagnostic push
ecbff1
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
ecbff1
+        if (sscanf(syspath, pattern, vendor, &model, &instance, &ethid) != 4)
ecbff1
+                return -EINVAL;
ecbff1
+#pragma GCC diagnostic pop
ecbff1
+
ecbff1
+        if (!in_charset(vendor, validchars))
ecbff1
+                return -ENOENT;
ecbff1
+
ecbff1
+        ascii_strlower(vendor);
ecbff1
+
ecbff1
+        xsprintf(names->platform_path, "a%s%xi%u", vendor, model, instance);
ecbff1
+        names->type = NET_PLATFORM;
ecbff1
+        return 0;
ecbff1
+}
ecbff1
+
ecbff1
 static int names_pci(struct udev_device *dev, struct netnames *names) {
ecbff1
         struct udev_device *parent;
ecbff1
         static int do_virtio = -1;
ecbff1
@@ -555,6 +617,16 @@ static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool
ecbff1
                 goto out;
ecbff1
         }
ecbff1
 
ecbff1
+        /* get ACPI path names for ARM64 platform devices */
ecbff1
+        err = names_platform(dev, &names, test);
ecbff1
+        if (err >= 0 && names.type == NET_PLATFORM) {
ecbff1
+                char str[IFNAMSIZ];
ecbff1
+
ecbff1
+                if (snprintf(str, sizeof(str), "%s%s", prefix, names.platform_path) < (int)sizeof(str))
ecbff1
+                        udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
ecbff1
+                goto out;
ecbff1
+        }
ecbff1
+
ecbff1
         /* get PCI based path names, we compose only PCI based paths */
ecbff1
         err = names_pci(dev, &names);
ecbff1
         if (err < 0)