[v2,3/3] usertools/dpdk-devbind: add bind/unbind for platform device

Message ID 1730864040-1309652-1-git-send-email-liwencheng@phytium.com.cn (mailing list archive)
State Changes Requested, archived
Delegated to: Stephen Hemminger
Headers
Series [v2,1/3] net/macb: add new driver |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/github-robot: build success github build: passed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing fail Testing issues
ci/iol-compile-arm64-testing fail Testing issues
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-sample-apps-testing success Testing PASS

Commit Message

Wencheng Li Nov. 6, 2024, 3:34 a.m. UTC
This patch mainly adds functions for bind and unbind platform devices,
such as bind_platform_one and unbind_platform_one.

Signed-off-by: liwencheng <liwencheng@phytium.com.cn>
---
 usertools/dpdk-devbind.py | 101 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 99 insertions(+), 2 deletions(-)
  

Patch

diff --git a/usertools/dpdk-devbind.py b/usertools/dpdk-devbind.py
index 80c35f9..4b65b55 100755
--- a/usertools/dpdk-devbind.py
+++ b/usertools/dpdk-devbind.py
@@ -147,10 +147,32 @@  def module_is_loaded(module):
     return module in loaded_modules
 
 
+def get_platform_devices():
+    global platform_devices
+
+    platform_device_path = "/sys/bus/platform/devices/"
+    platform_devices = os.listdir(platform_device_path)
+
+
+def devices_are_platform(devs):
+    all_devices_are_platform = True
+
+    get_platform_devices()
+    for d in devs:
+        if d not in platform_devices:
+            all_devices_are_platform = False
+            break
+
+    return all_devices_are_platform
+
+
 def check_modules():
     '''Checks that igb_uio is loaded'''
     global dpdk_drivers
 
+    if devices_are_platform(args):
+        return
+
     # list of supported modules
     mods = [{"Name": driver, "Found": False} for driver in dpdk_drivers]
 
@@ -321,11 +343,38 @@  def dev_id_from_dev_name(dev_name):
     for d in devices.keys():
         if dev_name in devices[d]["Interface"].split(","):
             return devices[d]["Slot"]
+
+    # Check if it is a platform device
+    if dev_name in platform_devices:
+        return dev_name
+
     # if nothing else matches - error
     raise ValueError("Unknown device: %s. "
                      "Please specify device in \"bus:slot.func\" format" % dev_name)
 
 
+def unbind_platform_one(dev_name):
+    filename = "/sys/bus/platform/devices/%s/driver" % dev_name
+
+    if exists(filename):
+        try:
+            f = open(os.path.join(filename, "unbind"), "w")
+        except OSError as err:
+            sys.exit("Error: unbind failed for %s - Cannot open %s: %s" %
+                     (dev_name, os.path.join(filename, "unbind"), err))
+        f.write(dev_name)
+        f.close()
+        filename = "/sys/bus/platform/devices/%s/driver_override" % dev_name
+        try:
+            f = open(filename, "w")
+        except OSError as err:
+            sys.exit("Error: unbind failed for %s - Cannot open %s: %s" %
+                     (dev_name, filename, err))
+        f.write("")
+        f.close()
+        print("Successfully unbind platform device %s" % dev_name)
+
+
 def unbind_one(dev_id, force):
     '''Unbind the device identified by "dev_id" from its current driver'''
     dev = devices[dev_id]
@@ -351,6 +400,48 @@  def unbind_one(dev_id, force):
     f.close()
 
 
+def bind_platform_one(dev_name, driver):
+    filename = "/sys/bus/platform/drivers/%s" % driver
+
+    if not exists(filename):
+        print("The driver %s is not loaded" % driver)
+        return
+    # unbind any existing drivers we don't want
+    filename = "/sys/bus/platform/devices/%s/driver" % dev_name
+    if exists(filename):
+        unbind_platform_one(dev_name)
+    # driver_override can be used to specify the driver
+    filename = "/sys/bus/platform/devices/%s/driver_override" % dev_name
+    if exists(filename):
+        try:
+            f = open(filename, "w")
+        except OSError as err:
+            sys.exit("Error: unbind failed for %s - Cannot open %s: %s"
+                     % (dev_name, filename, err))
+        try:
+            f.write(driver)
+            f.close()
+        except OSError as err:
+            sys.exit("Error: unbind failed for %s - Cannot write %s: %s"
+                     % (dev_name, filename, err))
+    # do the bind by writing to /sys
+    filename = "/sys/bus/platform/drivers/%s/bind" % driver
+    try:
+        f = open(filename, "w")
+    except OSError as err:
+        print("Error: bind failed for %s - Cannot open %s: %s"
+              % (dev_name, filename, err), file=sys.stderr)
+        return
+    try:
+        f.write(dev_name)
+        f.close()
+    except OSError as err:
+        print("Error: bind failed for %s - Cannot bind to driver %s: %s"
+              % (dev_name, driver, err), file=sys.stderr)
+        return
+    print("Successfully bind platform device %s to driver %s" % (dev_name, driver))
+
+
 def bind_one(dev_id, driver, force):
     '''Bind the device given by "dev_id" to the driver "driver". If the device
     is already bound to a different driver, it will be unbound first'''
@@ -475,7 +566,10 @@  def unbind_all(dev_list, force=False):
         sys.exit(1)
 
     for d in dev_list:
-        unbind_one(d, force)
+        if d in platform_devices:
+            unbind_platform_one(d)
+        else:
+            unbind_one(d, force)
 
 
 def has_iommu():
@@ -537,7 +631,10 @@  def bind_all(dev_list, driver, force=False):
         check_noiommu_mode()
 
     for d in dev_list:
-        bind_one(d, driver, force)
+        if d in platform_devices:
+            bind_platform_one(d, driver)
+        else:
+            bind_one(d, driver, force)
 
     # For kernels < 3.15 when binding devices to a generic driver
     # (i.e. one that doesn't have a PCI ID table) using new_id, some devices