From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 9412D45CE4; Mon, 11 Nov 2024 08:43:23 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 82C1240E0C; Mon, 11 Nov 2024 08:43:23 +0100 (CET) Received: from zg8tmty3ljk5ljewns4xndka.icoremail.net (zg8tmty3ljk5ljewns4xndka.icoremail.net [167.99.105.149]) by mails.dpdk.org (Postfix) with ESMTP id 2138640E01 for ; Mon, 11 Nov 2024 08:43:21 +0100 (CET) Received: from prodtpl.icoremail.net (unknown [10.12.1.20]) by hzbj-icmmx-7 (Coremail) with SMTP id AQAAfwD3_x+YtTFn6glEBA--.30586S2; Mon, 11 Nov 2024 15:43:20 +0800 (CST) Received: from localhost.localdomain (unknown [218.76.62.144]) by mail (Coremail) with SMTP id AQAAfwDnPXqTtTFnVM1NAA--.31506S3; Mon, 11 Nov 2024 15:43:16 +0800 (CST) From: liwencheng To: dev@dpdk.org Subject: [PATCH v2 3/3] usertools/dpdk-devbind: add bind/unbind for platform device Date: Mon, 11 Nov 2024 07:43:09 +0000 Message-Id: <1731310989-1743449-1-git-send-email-liwencheng@phytium.com.cn> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1730864040-1309652-1-git-send-email-liwencheng@phytium.com.cn> References: <1730864040-1309652-1-git-send-email-liwencheng@phytium.com.cn> X-CM-TRANSID: AQAAfwDnPXqTtTFnVM1NAA--.31506S3 X-CM-SenderInfo: polzv0xfkh0wo6sk53xlxphulrpou0/ Authentication-Results: hzbj-icmmx-7; spf=neutral smtp.mail=liwencheng @phytium.com.cn; X-Coremail-Antispam: 1Uk129KBjvJXoWxXw4furykZrWxuF1xWFy7Awb_yoWrKrWkpr 4rAaySyr17Kr9rKwnxZ3ZrCFyFkws2va45KrnrXw1j93s8ZrZ5trZ7tF1rZ3ZrJr1fZ3Z8 t3Z8XF4vka13ZF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj DUYxn0WfASr-VFAU7a7-sFnT9fnUUIcSsGvfJ3UbIYCTnIWIevJa73UjIFyTuYvj4RJUUU UUUUU X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patch mainly adds functions for bind and unbind platform devices, such as bind_platform_one and unbind_platform_one. Signed-off-by: liwencheng Acked-by: Stephen Hemminger --- usertools/dpdk-devbind.py | 101 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) 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 -- 2.7.4