Skip to contentSkip to conversation

net: wwan: t7xx: PCIe reset rescan

PATCH Applied [email protected] 4 participants 6 messages Aug 16, 2022 – Sep 9, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 3/5 net: wwan: t7xx: PCIe reset rescan

From: Haijun Liu <[email protected]>

PCI rescan module implements "rescan work queue". In firmware flashing or coredump collection procedure WWAN device is programmed to boot in fastboot mode and a work item is scheduled for removal & detection. The WWAN device is reset using APCI call as part driver removal flow. Work queue rescans pci bus at fixed interval for device detection, later when device is detect work queue exits.

---
 drivers/net/wwan/t7xx/Makefile             |   3 +-
 drivers/net/wwan/t7xx/t7xx_modem_ops.c     |   5 +
 drivers/net/wwan/t7xx/t7xx_pci.c           |  51 ++++++++-
 drivers/net/wwan/t7xx/t7xx_pci.h           |   1 +
 drivers/net/wwan/t7xx/t7xx_pci_rescan.c    | 117 +++++++++++++++++++++
 drivers/net/wwan/t7xx/t7xx_pci_rescan.h    |  29 +++++
 drivers/net/wwan/t7xx/t7xx_port_wwan.c     |   6 ++
 drivers/net/wwan/t7xx/t7xx_state_monitor.c |   2 +
 8 files changed, 212 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/wwan/t7xx/t7xx_pci_rescan.c
 create mode 100644 drivers/net/wwan/t7xx/t7xx_pci_rescan.h
drivers/net/wwan/t7xx/Makefile +2 -1
index dc6a7d682c15..df42764b3df8 100644
@@ -17,4 +17,5 @@ mtk_t7xx-y:=	t7xx_pci.o \
 		t7xx_hif_dpmaif_tx.o \
 		t7xx_hif_dpmaif_rx.o  \
 		t7xx_dpmaif.o \
-		t7xx_netdev.o
+		t7xx_netdev.o \
+		t7xx_pci_rescan.o
drivers/net/wwan/t7xx/t7xx_modem_ops.c +5 -0
index ec2269dfaf2c..fb79d041dbf5 100644
@@ -37,6 +37,7 @@
 #include "t7xx_modem_ops.h"
 #include "t7xx_netdev.h"
 #include "t7xx_pci.h"
+#include "t7xx_pci_rescan.h"
 #include "t7xx_pcie_mac.h"
 #include "t7xx_port.h"
 #include "t7xx_port_proxy.h"
@@ -192,6 +193,10 @@ static irqreturn_t t7xx_rgu_isr_thread(int irq, void *data)
 
 	msleep(RGU_RESET_DELAY_MS);
 	t7xx_reset_device_via_pmic(t7xx_dev);
+
+	if (!t7xx_dev->hp_enable)
+		t7xx_rescan_queue_work(t7xx_dev->pdev);
+
 	return IRQ_HANDLED;
 }
 
drivers/net/wwan/t7xx/t7xx_pci.c +50 -1

71 lines not on this page · raw message

drivers/net/wwan/t7xx/t7xx_pci.h +1 -0
index 50b37056ce5a..a87c4cae94ef 100644
@@ -69,6 +69,7 @@ struct t7xx_pci_dev {
 	struct t7xx_modem	*md;
 	struct t7xx_ccmni_ctrl	*ccmni_ctlb;
 	bool			rgu_pci_irq_en;
+	bool			hp_enable;
 
 	/* Low Power Items */
 	struct list_head	md_pm_entities;
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +117 -0

117 lines not on this page · raw message

drivers/net/wwan/t7xx/t7xx_pci_rescan.h new file +29 -0
new file mode 100644
index 000000000000..de4ca1363bb0
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-only
+ *
+ * Copyright (c) 2021, MediaTek Inc.
+ * Copyright (c) 2021-2022, Intel Corporation.
+ */
+
+#ifndef __T7XX_PCI_RESCAN_H__
+#define __T7XX_PCI_RESCAN_H__
+
+#define MTK_RESCAN_WQ "mtk_rescan_wq"
+
+#define DELAY_RESCAN_MTIME 1000
+#define RESCAN_RETRIES 35
+
+struct remove_rescan_context {
+	struct work_struct	 service_task;
+	struct workqueue_struct *pcie_rescan_wq;
+	spinlock_t		dev_lock; /* protects device */
+	struct pci_dev		*dev;
+	int			rescan_done;
+};
+
+void t7xx_pci_dev_rescan(void);
+void t7xx_rescan_queue_work(struct pci_dev *pdev);
+int t7xx_rescan_init(void);
+void t7xx_rescan_deinit(void);
+void t7xx_rescan_done(void);
+
+#endif	/* __T7XX_PCI_RESCAN_H__ */
drivers/net/wwan/t7xx/t7xx_port_wwan.c +6 -0
index e53651ee2005..dfd7fb487fc0 100644
@@ -156,6 +156,12 @@ static void t7xx_port_wwan_md_state_notify(struct t7xx_port *port, unsigned int
 {
 	const struct t7xx_port_conf *port_conf = port->port_conf;
 
+	if (state == MD_STATE_EXCEPTION) {
+		if (port->wwan_port)
+			wwan_port_txoff(port->wwan_port);
+		return;
+	}
+
 	if (state != MD_STATE_READY)
 		return;
 
drivers/net/wwan/t7xx/t7xx_state_monitor.c +2 -0
index c1789a558c9d..9c222809371b 100644
@@ -35,9 +35,11 @@
 #include "t7xx_hif_cldma.h"
 #include "t7xx_mhccif.h"
 #include "t7xx_modem_ops.h"
+#include "t7xx_netdev.h"
 #include "t7xx_pci.h"
 #include "t7xx_pcie_mac.h"
 #include "t7xx_port_proxy.h"
+#include "t7xx_pci_rescan.h"
 #include "t7xx_reg.h"
 #include "t7xx_state_monitor.h"
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 15 lines · On Tue, 16 Aug 2022, [email protected] wrote:

From: Haijun Liu <[email protected]>

PCI rescan module implements "rescan work queue". In firmware flashing or coredump collection procedure WWAN device is programmed to boot in fastboot mode and a work item is scheduled for removal & detection. The WWAN device is reset using APCI call as part driver removal flow. Work queue rescans pci bus at fixed interval for device detection, later when device is detect work queue exits.

Quoted 3 lines

---

+ bool hp_enable;

Never written to.

Quoted 14 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +8 -0
new file mode 100644
index 000000000000..045777d8a843
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2021, MediaTek Inc.
+ * Copyright (c) 2021-2022, Intel Corporation.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ":t7xx:%s: " fmt, __func__
+#define dev_fmt(fmt) "t7xx: " fmt

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 18

Quoted 10 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +10 -0
+
+#include <linux/delay.h>
+#include <linux/pci.h>
+#include <linux/spinlock.h>
+#include <linux/workqueue.h>
+
+#include "t7xx_pci.h"
+#include "t7xx_pci_rescan.h"
+
+static struct remove_rescan_context g_mtk_rescan_context;

Any particular reason for this name?

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 28

Quoted 9 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +9 -0
+void t7xx_pci_dev_rescan(void)
+{
+	struct pci_bus *b = NULL;
+
+	pci_lock_rescan_remove();
+	while ((b = pci_find_next_bus(b)))
+		pci_rescan_bus(b);
+
+	pci_unlock_rescan_remove();

I'd remove the empty line to keep the critical sections grouped together.

Quoted 31 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +31 -0
+}
+
+void t7xx_rescan_done(void)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+	if (g_mtk_rescan_context.rescan_done == 0) {
+		pr_debug("this is a rescan probe\n");
+		g_mtk_rescan_context.rescan_done = 1;
+	} else {
+		pr_debug("this is a init probe\n");
+	}
+	spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+}
+
+static void t7xx_remove_rescan(struct work_struct *work)
+{
+	struct pci_dev *pdev;
+	int num_retries = RESCAN_RETRIES;
+	unsigned long flags;
+
+	spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+	g_mtk_rescan_context.rescan_done = 0;
+	pdev = g_mtk_rescan_context.dev;
+	spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+
+	if (pdev) {
+		pci_stop_and_remove_bus_device_locked(pdev);
+		pr_debug("start remove and rescan flow\n");
+	}

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 69

Quoted 10 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +10 -0
+
+	do {
+		t7xx_pci_dev_rescan();
+		spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+		if (g_mtk_rescan_context.rescan_done) {
+			spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+			break;
+		}
+
+		spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);

Ditto.

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 81

Quoted 12 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +12 -0
+		msleep(DELAY_RESCAN_MTIME);
+	} while (num_retries--);
+}
+
+void t7xx_rescan_queue_work(struct pci_dev *pdev)
+{
+	unsigned long flags;
+
+	dev_info(&pdev->dev, "start queue_mtk_rescan_work\n");
+	spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+	if (!g_mtk_rescan_context.rescan_done) {
+		dev_err(&pdev->dev, "rescan failed because last rescan undone\n");

The meaning of the message is hard to understand.

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 87

Quoted 6 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +6 -0
+		spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+		return;
+	}
+
+	g_mtk_rescan_context.dev = pdev;
+	spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);

Crit section newlines.

Quoted 20 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +20 -0
+	queue_work(g_mtk_rescan_context.pcie_rescan_wq, &g_mtk_rescan_context.service_task);
+}
+
+int t7xx_rescan_init(void)
+{
+	spin_lock_init(&g_mtk_rescan_context.dev_lock);
+	g_mtk_rescan_context.rescan_done = 1;
+	g_mtk_rescan_context.dev = NULL;
+	g_mtk_rescan_context.pcie_rescan_wq = create_singlethread_workqueue(MTK_RESCAN_WQ);
+	if (!g_mtk_rescan_context.pcie_rescan_wq) {
+		pr_err("Failed to create workqueue: %s\n", MTK_RESCAN_WQ);
+		return -ENOMEM;
+	}
+
+	INIT_WORK(&g_mtk_rescan_context.service_task, t7xx_remove_rescan);
+
+	return 0;
+}
+
+void t7xx_rescan_deinit(void)

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 117

Quoted 10 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +10 -0
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+	g_mtk_rescan_context.rescan_done = 0;
+	g_mtk_rescan_context.dev = NULL;
+	spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+	cancel_work_sync(&g_mtk_rescan_context.service_task);
+	destroy_workqueue(g_mtk_rescan_context.pcie_rescan_wq);
+}

In general, I felt this whole file was very heavy to read compared with the other parts of t7xx code. Maybe it will get better if g_mtk_rescan_context becomes shorter and re-newlining is done to better indicate the critical sections but we'll see.

Quoted 12 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.h new file +6 -0
new file mode 100644
index 000000000000..de4ca1363bb0
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-only
+ *
+ * Copyright (c) 2021, MediaTek Inc.
+ * Copyright (c) 2021-2022, Intel Corporation.
+ */
+

drivers/net/wwan/t7xx/t7xx_pci_rescan.h line 16

Quoted 10 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.h new file +10 -0
+#ifndef __T7XX_PCI_RESCAN_H__
+#define __T7XX_PCI_RESCAN_H__
+
+#define MTK_RESCAN_WQ "mtk_rescan_wq"
+
+#define DELAY_RESCAN_MTIME 1000
+#define RESCAN_RETRIES 35
+
+struct remove_rescan_context {
+	struct work_struct	 service_task;

Extra whitespace.

drivers/net/wwan/t7xx/t7xx_pci_rescan.h line 18

Quoted 2 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.h new file +2 -0
+	struct workqueue_struct *pcie_rescan_wq;
+	spinlock_t		dev_lock; /* protects device */

Perhaps use a tab before the comment instead to make some room.

Quoted 3 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.h new file +3 -0
+	struct pci_dev		*dev;
+	int			rescan_done;
+};
Signature · 1 line
 i.
maintainer
#2 lore raw
In reply to [email protected]
Quoted 8 lines · On Tue, Aug 16, 2022 at 7:12 AM <[email protected]> wrote:

From: Haijun Liu <[email protected]>

PCI rescan module implements "rescan work queue". In firmware flashing or coredump collection procedure WWAN device is programmed to boot in fastboot mode and a work item is scheduled for removal & detection. The WWAN device is reset using APCI call as part driver removal flow. Work queue rescans pci bus at fixed interval for device detection, later when device is detect work queue exits.

[skipped]

drivers/net/wwan/t7xx/t7xx_pci.c static int t7xx_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 723

Quoted 13 lines
drivers/net/wwan/t7xx/t7xx_pci.c +3 -0
index 871f2a27a398..2f5c6fbe601e 100644
@@ -715,8 +716,11 @@ static int t7xx_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
                return ret;
        }
 
+       t7xx_rescan_done();
        t7xx_pcie_mac_set_int(t7xx_dev, MHCCIF_INT);
        t7xx_pcie_mac_interrupts_en(t7xx_dev);
+       if (!t7xx_dev->hp_enable)
+               pci_ignore_hotplug(pdev);

pci_ignore_hotplug() also disables hotplug events for a parent bridge. Is that how this call was intended?

Quoted 3 lines
drivers/net/wwan/t7xx/t7xx_pci.c +0 -0
        return 0;
 }

[skipped]

Quoted 13 lines
drivers/net/wwan/t7xx/t7xx_pci.c +13 -0
+static void __exit t7xx_pci_cleanup(void)
+{
+       int remove_flag = 0;
+       struct device *dev;
+
+       dev = driver_find_device(&t7xx_pci_driver.driver, NULL, NULL, t7xx_always_match);
+       if (dev) {
+               pr_debug("unregister t7xx PCIe driver while device is still exist.\n");
+               put_device(dev);
+               remove_flag = 1;
+       } else {
+               pr_debug("no t7xx PCIe driver found.\n");
+       }

drivers/net/wwan/t7xx/t7xx_pci.c static struct pci_driver t7xx_pci_driver = { · line 803

Quoted 10 lines
drivers/net/wwan/t7xx/t7xx_pci.c +10 -0
+
+       pci_lock_rescan_remove();
+       pci_unregister_driver(&t7xx_pci_driver);
+       pci_unlock_rescan_remove();
+       t7xx_rescan_deinit();
+
+       if (remove_flag) {
+               pr_debug("remove t7xx PCI device\n");
+               pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
+       }

What is the purpose of these operations? Should not the PCI core do this for us on the driver unregister?

Quoted 6 lines
drivers/net/wwan/t7xx/t7xx_pci.c +3 -0
+}
+
+module_exit(t7xx_pci_cleanup);
 
 MODULE_AUTHOR("MediaTek Inc");
 MODULE_DESCRIPTION("MediaTek PCIe 5G WWAN modem T7xx driver");

[skipped]

Quoted 5 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +0 -0
new file mode 100644
index 000000000000..045777d8a843

[skipped]

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 26

Quoted 7 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +7 -0
+void t7xx_pci_dev_rescan(void)
+{
+       struct pci_bus *b = NULL;
+
+       pci_lock_rescan_remove();
+       while ((b = pci_find_next_bus(b)))
+               pci_rescan_bus(b);

The driver does not need to rescan all buses. The device should appear on the same bus, so the driver just needs to rescan a single and already known bus.

Quoted 3 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +3 -0
+
+       pci_unlock_rescan_remove();
+}

[skipped]

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 57

Quoted 13 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +13 -0
+static void t7xx_remove_rescan(struct work_struct *work)
+{
+       struct pci_dev *pdev;
+       int num_retries = RESCAN_RETRIES;
+       unsigned long flags;
+
+       spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+       g_mtk_rescan_context.rescan_done = 0;
+       pdev = g_mtk_rescan_context.dev;
+       spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+
+       if (pdev) {
+               pci_stop_and_remove_bus_device_locked(pdev);

What is the purpose of removing the device then trying to find it by rescanning the bus? Would not it be easier to save a PCI device configuration, reset the device, and then restore the configuration?

The DEVLINK_CMD_RELOAD description states that this command performs (see include/uapi/linux/devlink.h):

Hot driver reload, makes configuration changes take place. The devlink instance is not released during the process.

And the devlink_reload() function in net/core/devlink.c is able to survive the devlink structure memory freeing only by accident. But the PCI device removing should do exactly that: call the device removing callback, which will release the devlink instance.

Quoted 15 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +15 -0
+               pr_debug("start remove and rescan flow\n");
+       }
+
+       do {
+               t7xx_pci_dev_rescan();
+               spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+               if (g_mtk_rescan_context.rescan_done) {
+                       spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+                       break;
+               }
+
+               spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+               msleep(DELAY_RESCAN_MTIME);
+       } while (num_retries--);
+}

[skipped]

Quoted 5 lines
drivers/net/wwan/t7xx/t7xx_port_wwan.c +0 -0
index e53651ee2005..dfd7fb487fc0 100644

drivers/net/wwan/t7xx/t7xx_port_wwan.c static void t7xx_port_wwan_md_state_notify(struct t7xx_port *port, unsigned int · line 164

Quoted 9 lines
drivers/net/wwan/t7xx/t7xx_port_wwan.c +6 -0
@@ -156,6 +156,12 @@ static void t7xx_port_wwan_md_state_notify(struct t7xx_port *port, unsigned int
 {
        const struct t7xx_port_conf *port_conf = port->port_conf;
 
+       if (state == MD_STATE_EXCEPTION) {
+               if (port->wwan_port)
+                       wwan_port_txoff(port->wwan_port);
+               return;
+       }
+

Looks unrelated to the patch description. Does this hunk really belong to the patch?

drivers/net/wwan/t7xx/t7xx_port_wwan.c static void t7xx_port_wwan_md_state_notify(struct t7xx_port *port, unsigned int · line 167

Quoted 3 lines
drivers/net/wwan/t7xx/t7xx_port_wwan.c +0 -0
        if (state != MD_STATE_READY)
                return;
 

--
Sergey

#3 lore raw
Quoted 20 lines · Sergey Ryazanov
Quoted 8 lines · On Tue, Aug 16, 2022 at 7:12 AM <[email protected]> wrote:

From: Haijun Liu <[email protected]>

PCI rescan module implements "rescan work queue". In firmware flashing or coredump collection procedure WWAN device is programmed to boot in fastboot mode and a work item is scheduled for removal & detection. The WWAN device is reset using APCI call as part driver removal flow. Work queue rescans pci bus at fixed interval for device detection, later when device is detect work queue exits.

[skipped]

Quoted 8 lines
drivers/net/wwan/t7xx/t7xx_pci.c +0 -0
index 871f2a27a398..2f5c6fbe601e 100644
@@ -715,8 +716,11 @@ static int t7xx_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
                 return ret;
         }
 
Quoted 8 lines

drivers/net/wwan/t7xx/t7xx_pci.c static int t7xx_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 723

Quoted 5 lines
drivers/net/wwan/t7xx/t7xx_pci.c +3 -0
+       t7xx_rescan_done();
         t7xx_pcie_mac_set_int(t7xx_dev, MHCCIF_INT);
         t7xx_pcie_mac_interrupts_en(t7xx_dev);
+       if (!t7xx_dev->hp_enable)
+               pci_ignore_hotplug(pdev);

pci_ignore_hotplug() also disables hotplug events for a parent bridge. Is that how this call was intended?

I am checking on it. Will get back.

Quoted 25 lines
Quoted 4 lines
drivers/net/wwan/t7xx/t7xx_pci.c +0 -0
         return 0;
  }

[skipped]

Quoted 18 lines
drivers/net/wwan/t7xx/t7xx_pci.c +18 -0
+static void __exit t7xx_pci_cleanup(void)
+{
+       int remove_flag = 0;
+       struct device *dev;
+
+       dev = driver_find_device(&t7xx_pci_driver.driver, NULL, NULL, t7xx_always_match);
+       if (dev) {
+               pr_debug("unregister t7xx PCIe driver while device is still exist.\n");
+               put_device(dev);
+               remove_flag = 1;
+       } else {
+               pr_debug("no t7xx PCIe driver found.\n");
+       }
+
+       pci_lock_rescan_remove();
+       pci_unregister_driver(&t7xx_pci_driver);
+       pci_unlock_rescan_remove();
+       t7xx_rescan_deinit();
Quoted 8 lines

drivers/net/wwan/t7xx/t7xx_pci.c static struct pci_driver t7xx_pci_driver = { · line 803

Quoted 5 lines
drivers/net/wwan/t7xx/t7xx_pci.c +5 -0
+
+       if (remove_flag) {
+               pr_debug("remove t7xx PCI device\n");
+               pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
+       }

What is the purpose of these operations? Should not the PCI core do this for us on the driver unregister?

In removal flow the device need to be reset else the next insmod
would result in device handshake failure. So in driver removal flow
the device is reset and current dev is removed using pci_stop_and_remove_bus_device_locked().

If hotplug is disabled on PCI Express Root Port, the dev removal procedure will not happen on device reset. So driver is explicitly calling pci_stop_and_remove_bus_device_locked().

Quoted 18 lines
Quoted 7 lines
drivers/net/wwan/t7xx/t7xx_pci.c +3 -0
+}
+
+module_exit(t7xx_pci_cleanup);
 
  MODULE_AUTHOR("MediaTek Inc");
  MODULE_DESCRIPTION("MediaTek PCIe 5G WWAN modem T7xx driver");

[skipped]

Quoted 5 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +0 -0
new file mode 100644
index 000000000000..045777d8a843

[skipped]

Quoted 11 lines

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 26

Quoted 7 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +7 -0
+void t7xx_pci_dev_rescan(void)
+{
+       struct pci_bus *b = NULL;
+
+       pci_lock_rescan_remove();
+       while ((b = pci_find_next_bus(b)))
+               pci_rescan_bus(b);

The driver does not need to rescan all buses. The device should appear on the same bus, so the driver just needs to rescan a single and already known bus.

On device reset the dev is removed so scanning all buses.

Quoted 16 lines
Quoted 4 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +3 -0
+
+       pci_unlock_rescan_remove();
+}

[skipped]

Quoted 9 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +9 -0
+static void t7xx_remove_rescan(struct work_struct *work)
+{
+       struct pci_dev *pdev;
+       int num_retries = RESCAN_RETRIES;
+       unsigned long flags;
+
+       spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+       g_mtk_rescan_context.rescan_done = 0;
+       pdev = g_mtk_rescan_context.dev;
Quoted 8 lines

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 57

Quoted 4 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +4 -0
+       spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+
+       if (pdev) {
+               pci_stop_and_remove_bus_device_locked(pdev);

What is the purpose of removing the device then trying to find it by rescanning the bus? Would not it be easier to save a PCI device configuration, reset the device, and then restore the configuration?

If hotplug is disabled, the device is not removed on reset. So in this case driver need to handle the device removal and rescan.

Quoted 36 lines

The DEVLINK_CMD_RELOAD description states that this command performs (see include/uapi/linux/devlink.h):

Hot driver reload, makes configuration changes take place. The devlink instance is not released during the process.

And the devlink_reload() function in net/core/devlink.c is able to survive the devlink structure memory freeing only by accident. But the PCI device removing should do exactly that: call the device removing callback, which will release the devlink instance. >

Quoted 15 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +15 -0
+               pr_debug("start remove and rescan flow\n");
+       }
+
+       do {
+               t7xx_pci_dev_rescan();
+               spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+               if (g_mtk_rescan_context.rescan_done) {
+                       spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+                       break;
+               }
+
+               spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+               msleep(DELAY_RESCAN_MTIME);
+       } while (num_retries--);
+}

[skipped]

Quoted 8 lines
drivers/net/wwan/t7xx/t7xx_port_wwan.c +0 -0
index e53651ee2005..dfd7fb487fc0 100644
@@ -156,6 +156,12 @@ static void t7xx_port_wwan_md_state_notify(struct t7xx_port *port, unsigned int
  {
         const struct t7xx_port_conf *port_conf = port->port_conf;
 
Quoted 9 lines

drivers/net/wwan/t7xx/t7xx_port_wwan.c static void t7xx_port_wwan_md_state_notify(struct t7xx_port *port, unsigned int · line 164

Quoted 6 lines
drivers/net/wwan/t7xx/t7xx_port_wwan.c +6 -0
+       if (state == MD_STATE_EXCEPTION) {
+               if (port->wwan_port)
+                       wwan_port_txoff(port->wwan_port);
+               return;
+       }
+

Looks unrelated to the patch description. Does this hunk really belong to the patch?

Will drop this.

Quoted 7 lines

drivers/net/wwan/t7xx/t7xx_port_wwan.c static void t7xx_port_wwan_md_state_notify(struct t7xx_port *port, unsigned int · line 167

Quoted 4 lines
drivers/net/wwan/t7xx/t7xx_port_wwan.c +0 -0
         if (state != MD_STATE_READY)
                 return;
 

--
Sergey

Signature · 1 line
Chetan
maintainer
#4 lore raw
Quoted 39 lines · M Chetan
Quoted 29 lines · Sergey Ryazanov
Quoted 8 lines · On Tue, Aug 16, 2022 at 7:12 AM <[email protected]> wrote:

From: Haijun Liu <[email protected]>

PCI rescan module implements "rescan work queue". In firmware flashing or coredump collection procedure WWAN device is programmed to boot in fastboot mode and a work item is scheduled for removal & detection. The WWAN device is reset using APCI call as part driver removal flow. Work queue rescans pci bus at fixed interval for device detection, later when device is detect work queue exits.

[skipped]

Quoted 5 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +0 -0
new file mode 100644
index 000000000000..045777d8a843

[skipped]

Quoted 9 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +9 -0
+static void t7xx_remove_rescan(struct work_struct *work)
+{
+       struct pci_dev *pdev;
+       int num_retries = RESCAN_RETRIES;
+       unsigned long flags;
+
+       spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+       g_mtk_rescan_context.rescan_done = 0;
+       pdev = g_mtk_rescan_context.dev;
Quoted 8 lines

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 57

Quoted 4 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +4 -0
+       spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+
+       if (pdev) {
+               pci_stop_and_remove_bus_device_locked(pdev);

What is the purpose of removing the device then trying to find it by rescanning the bus? Would not it be easier to save a PCI device configuration, reset the device, and then restore the configuration?

Quoted 2 lines

If hotplug is disabled, the device is not removed on reset. So in this case driver need to handle the device removal and rescan.

I still can not understand this part and need a clue. Why should the driver disable the hotplug?

And is there a more gentle way to reset the firmware without the device object removing?

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +15 -0
+               pr_debug("start remove and rescan flow\n");
+       }
+
+       do {
+               t7xx_pci_dev_rescan();
+               spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+               if (g_mtk_rescan_context.rescan_done) {
+                       spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+                       break;
+               }
+
+               spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+               msleep(DELAY_RESCAN_MTIME);
+       } while (num_retries--);
+}
Signature · 1 line
Sergey
#5 lore raw
Quoted 41 lines · Sergey Ryazanov
Quoted 39 lines · M Chetan
Quoted 37 lines · Sergey Ryazanov
Quoted 8 lines · On Tue, Aug 16, 2022 at 7:12 AM <[email protected]> wrote:

From: Haijun Liu <[email protected]>

PCI rescan module implements "rescan work queue". In firmware flashing or coredump collection procedure WWAN device is programmed to boot in fastboot mode and a work item is scheduled for removal & detection. The WWAN device is reset using APCI call as part driver removal flow. Work queue rescans pci bus at fixed interval for device detection, later when device is detect work queue exits.

[skipped]

Quoted 5 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +0 -0
new file mode 100644
index 000000000000..045777d8a843

[skipped]

drivers/net/wwan/t7xx/t7xx_pci_rescan.c line 57

Quoted 13 lines
drivers/net/wwan/t7xx/t7xx_pci_rescan.c new file +13 -0
+static void t7xx_remove_rescan(struct work_struct *work)
+{
+       struct pci_dev *pdev;
+       int num_retries = RESCAN_RETRIES;
+       unsigned long flags;
+
+       spin_lock_irqsave(&g_mtk_rescan_context.dev_lock, flags);
+       g_mtk_rescan_context.rescan_done = 0;
+       pdev = g_mtk_rescan_context.dev;
+       spin_unlock_irqrestore(&g_mtk_rescan_context.dev_lock, flags);
+
+       if (pdev) {
+               pci_stop_and_remove_bus_device_locked(pdev);

What is the purpose of removing the device then trying to find it by rescanning the bus? Would not it be easier to save a PCI device configuration, reset the device, and then restore the configuration?

Quoted 5 lines
Quoted 2 lines

If hotplug is disabled, the device is not removed on reset. So in this case driver need to handle the device removal and rescan.

I still can not understand this part and need a clue. Why should the driver disable the hotplug?

This is a platform configuration, it could be set to enable/disable. We can find this option in BIOS settings.

Quoted 2 lines

And is there a more gentle way to reset the firmware without the device object removing?

Device reset causes WWAN device to fall off the BUS. If device had not fallen off the bus then we could have reused.

Without these changes, we need to manually execute device remove & rescan commands.

Could you please suggest how can we proceed here ?

Signature · 1 line
Chetan