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