DPDK patches and discussions
 help / color / Atom feed
* [dpdk-dev] [PATCH] usertools: fix bind failed can not regression
@ 2019-11-26  6:49 Hui Wei
  2019-11-26 22:54 ` Thomas Monjalon
  0 siblings, 1 reply; 2+ messages in thread
From: Hui Wei @ 2019-11-26  6:49 UTC (permalink / raw)
  To: dev

nic bind to mlx5_core, enable sriov, nic has one pf and 8 vfs,
bind pf to vfio-pci failed, can not regress to mlx5_core

Signed-off-by: Wei Hui <huiweics@hotmail.com>
---
 usertools/dpdk-devbind.py | 165 ++++++++++++++++++++++----------------
 1 file changed, 94 insertions(+), 71 deletions(-)

diff --git a/usertools/dpdk-devbind.py b/usertools/dpdk-devbind.py
index 7b5cbc12c..59e3960af 100755
--- a/usertools/dpdk-devbind.py
+++ b/usertools/dpdk-devbind.py
@@ -379,6 +379,7 @@ def bind_one(dev_id, driver, force):
     is already bound to a different driver, it will be unbound first'''
     dev = devices[dev_id]
     saved_driver = None  # used to rollback any unbind in case of failure
+    bind_success = False
 
     # prevent disconnection of our ssh session
     if dev["Ssh_if"] and not force:
@@ -386,16 +387,12 @@ def bind_one(dev_id, driver, force):
               "Not modifying" % dev_id, file=sys.stderr)
         return
 
-    # unbind any existing drivers we don't want
+    # does not need to do anything
     if has_driver(dev_id):
         if dev["Driver_str"] == driver:
             print("Notice: %s already bound to driver %s, skipping" %
                   (dev_id, driver), file=sys.stderr)
             return
-        else:
-            saved_driver = dev["Driver_str"]
-            unbind_one(dev_id, force)
-            dev["Driver_str"] = ""  # clear driver string
 
     # For kernels >= 3.15 driver_override can be used to specify the driver
     # for a device rather than relying on the driver to provide a positive
@@ -403,40 +400,45 @@ def bind_one(dev_id, driver, force):
     # the vendor and device ID, adding them to the driver new_id,
     # will erroneously bind other devices too which has the additional burden
     # of unbinding those devices
-    if driver in dpdk_drivers:
-        filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
-        if os.path.exists(filename):
-            try:
-                f = open(filename, "w")
-            except:
-                print("Error: bind failed for %s - Cannot open %s"
-                      % (dev_id, filename), file=sys.stderr)
-                return
-            try:
-                f.write("%s" % driver)
-                f.close()
-            except:
-                print("Error: bind failed for %s - Cannot write driver %s to "
-                      "PCI ID " % (dev_id, driver), file=sys.stderr)
-                return
-        # For kernels < 3.15 use new_id to add PCI id's to the driver
-        else:
-            filename = "/sys/bus/pci/drivers/%s/new_id" % driver
-            try:
-                f = open(filename, "w")
-            except:
-                print("Error: bind failed for %s - Cannot open %s"
-                      % (dev_id, filename), file=sys.stderr)
-                return
-            try:
-                # Convert Device and Vendor Id to int to write to new_id
-                f.write("%04x %04x" % (int(dev["Vendor"],16),
-                        int(dev["Device"], 16)))
-                f.close()
-            except:
-                print("Error: bind failed for %s - Cannot write new PCI ID to "
-                      "driver %s" % (dev_id, driver), file=sys.stderr)
-                return
+    filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
+    if os.path.exists(filename):
+        try:
+            f = open(filename, "w")
+        except:
+            print("Error: bind failed for %s - Cannot open %s"
+                  % (dev_id, filename), file=sys.stderr)
+            return
+        try:
+            f.write("%s" % driver)
+            f.close()
+        except:
+            print("Error: bind failed for %s - Cannot write driver %s to "
+                  "PCI ID " % (dev_id, driver), file=sys.stderr)
+            return
+    # For kernels < 3.15 use new_id to add PCI id's to the driver
+    else:
+        filename = "/sys/bus/pci/drivers/%s/new_id" % driver
+        try:
+            f = open(filename, "w")
+        except:
+            print("Error: bind failed for %s - Cannot open %s"
+                  % (dev_id, filename), file=sys.stderr)
+            return
+        try:
+            # Convert Device and Vendor Id to int to write to new_id
+            f.write("%04x %04x" % (int(dev["Vendor"],16),
+                    int(dev["Device"], 16)))
+            f.close()
+        except:
+            print("Error: bind failed for %s - Cannot write new PCI ID to "
+                  "driver %s" % (dev_id, driver), file=sys.stderr)
+            return
+
+    # unbind any existing drivers we don't want
+    if has_driver(dev_id):
+        saved_driver = dev["Driver_str"]
+        unbind_one(dev_id, force)
+        del dev["Driver_str"]  # clear driver string
 
     # do the bind by writing to /sys
     filename = "/sys/bus/pci/drivers/%s/bind" % driver
@@ -445,42 +447,63 @@ def bind_one(dev_id, driver, force):
     except:
         print("Error: bind failed for %s - Cannot open %s"
               % (dev_id, filename), file=sys.stderr)
-        if saved_driver is not None:  # restore any previous driver
-            bind_one(dev_id, saved_driver, force)
-        return
-    try:
-        f.write(dev_id)
-        f.close()
-    except:
-        # for some reason, closing dev_id after adding a new PCI ID to new_id
-        # results in IOError. however, if the device was successfully bound,
-        # we don't care for any errors and can safely ignore IOError
-        tmp = get_pci_device_details(dev_id, True)
-        if "Driver_str" in tmp and tmp["Driver_str"] == driver:
-            return
-        print("Error: bind failed for %s - Cannot bind to driver %s"
-              % (dev_id, driver), file=sys.stderr)
-        if saved_driver is not None:  # restore any previous driver
-            bind_one(dev_id, saved_driver, force)
-        return
-
-    # For kernels > 3.15 driver_override is used to bind a device to a driver.
-    # Before unbinding it, overwrite driver_override with empty string so that
-    # the device can be bound to any other driver
-    filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
-    if os.path.exists(filename):
-        try:
-            f = open(filename, "w")
-        except:
-            sys.exit("Error: unbind failed for %s - Cannot open %s"
-                  % (dev_id, filename))
+    else:
         try:
-            f.write("\00")
+            f.write(dev_id)
             f.close()
         except:
-            sys.exit("Error: unbind failed for %s - Cannot open %s"
-                  % (dev_id, filename))
+            # for some reason, closing dev_id after adding a new PCI ID to new_id
+            # results in IOError. however, if the device was successfully bound,
+            # we don't care for any errors and can safely ignore IOError
+            tmp = get_pci_device_details(dev_id, True)
+            if "Driver_str" in tmp and tmp["Driver_str"] == driver:
+                return
+            print("Error: bind failed for %s - Cannot bind to driver %s"
+                  % (dev_id, driver), file=sys.stderr)
+        else:
+            bind_success = True
 
+    if False == bind_success:
+        if saved_driver is not None:  # restore any previous driver
+            bind_one(dev_id, saved_driver, force)
+            dev["Driver_str"] = saved_driver
+        else:
+            # At this point, pci device previous does not bind to any driver, or it will
+            # be restored it's previous driver.
+            # For kernels > 3.15 driver_override is used to bind a device to a driver.
+            # Before unbinding it, overwrite driver_override with empty string so that
+            # the device can be bound to any other driver
+            filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
+            if os.path.exists(filename):
+                try:
+                    f = open(filename, "w")
+                except:
+                    sys.exit("Error: restore failed for %s - Cannot open %s"
+                          % (dev_id, filename))
+                try:
+                    f.write("\00")
+                    f.close()
+                except:
+                    sys.exit("Error: restore failed for %s - Cannot write %s"
+                          % (dev_id, filename))
+            # For kernels < 3.15 use remove_id to delete PCI id's from the driver
+            else:
+                filename = "/sys/bus/pci/drivers/%s/remove_id" % driver
+                try:
+                    f = open(filename, "w")
+                except:
+                    print("Error: restore failed for %s - Cannot open %s"
+                          % (dev_id, filename), file=sys.stderr)
+                    return
+                try:
+                    # Convert Device and Vendor Id to int to write to remove_id
+                    f.write("%04x %04x" % (int(dev["Vendor"],16),
+                            int(dev["Device"], 16)))
+                    f.close()
+                except:
+                    print("Error: restore failed for %s - Cannot write new PCI ID to "
+                          "driver %s remove_id" % (dev_id, driver), file=sys.stderr)
+                    return
 
 def unbind_all(dev_list, force=False):
     """Unbind method, takes a list of device locations"""
-- 
2.24.0.windows.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [dpdk-dev] [PATCH] usertools: fix bind failed can not regression
  2019-11-26  6:49 [dpdk-dev] [PATCH] usertools: fix bind failed can not regression Hui Wei
@ 2019-11-26 22:54 ` Thomas Monjalon
  0 siblings, 0 replies; 2+ messages in thread
From: Thomas Monjalon @ 2019-11-26 22:54 UTC (permalink / raw)
  To: Hui Wei; +Cc: dev

26/11/2019 07:49, Hui Wei:
> nic bind to mlx5_core, enable sriov, nic has one pf and 8 vfs,
> bind pf to vfio-pci failed, can not regress to mlx5_core

I don't understand what you try to do.
For info, mlx5 does not bind to VFIO,
but uses a bifurcated driver model.




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-26  6:49 [dpdk-dev] [PATCH] usertools: fix bind failed can not regression Hui Wei
2019-11-26 22:54 ` Thomas Monjalon

DPDK patches and discussions

Archives are clonable:
	git clone --mirror http://inbox.dpdk.org/dev/0 dev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 dev dev/ http://inbox.dpdk.org/dev \
		dev@dpdk.org
	public-inbox-index dev


Newsgroup available over NNTP:
	nntp://inbox.dpdk.org/inbox.dpdk.dev


AGPL code for this site: git clone https://public-inbox.org/ public-inbox