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