diff mbox series

[net-next,v11,09/13] net:ethernet:realtek:rtase: Implement pci_driver suspend and resume function

Message ID 20231115133414.1221480-10-justinlai0215@realtek.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series Add Realtek automotive PCIe driver | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/cc_maintainers success CCed 5 of 5 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 76 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Justin Lai Nov. 15, 2023, 1:34 p.m. UTC
Implement the pci_driver suspend function to enable the device
to sleep, and implement the resume function to enable the device
to resume operation.

Signed-off-by: Justin Lai <justinlai0215@realtek.com>
---
 .../net/ethernet/realtek/rtase/rtase_main.c   | 64 +++++++++++++++++++
 1 file changed, 64 insertions(+)

Comments

Heiner Kallweit Nov. 15, 2023, 3:02 p.m. UTC | #1
On 15.11.2023 14:34, Justin Lai wrote:
> Implement the pci_driver suspend function to enable the device
> to sleep, and implement the resume function to enable the device
> to resume operation.
>
> Signed-off-by: Justin Lai <justinlai0215@realtek.com>
> ---
>  .../net/ethernet/realtek/rtase/rtase_main.c   | 64 +++++++++++++++++++
>  1 file changed, 64 insertions(+)
>
> diff --git a/drivers/net/ethernet/realtek/rtase/rtase_main.c b/drivers/net/ethernet/realtek/rtase/rtase_main.c
> index 12607663dd72..b7679b74cc8a 100644
> --- a/drivers/net/ethernet/realtek/rtase/rtase_main.c
> +++ b/drivers/net/ethernet/realtek/rtase/rtase_main.c
> @@ -2323,12 +2323,76 @@ static void rtase_shutdown(struct pci_dev *pdev)
>  	rtase_reset_interrupt(pdev, tp);
>  }
>
> +#ifdef CONFIG_PM
> +static int rtase_suspend(struct pci_dev *pdev, pm_message_t state)
> +{
> +	struct net_device *dev = pci_get_drvdata(pdev);
> +
> +	if (!netif_running(dev))
> +		goto out;
> +
> +	netif_stop_queue(dev);
> +	netif_carrier_off(dev);
> +	netif_device_detach(dev);
> +	rtase_hw_reset(dev);
> +
> +out:
> +	pci_save_state(pdev);
> +
> +	return 0;
> +}
> +
> +static int rtase_resume(struct pci_dev *pdev)
> +{
> +	struct net_device *dev = pci_get_drvdata(pdev);
> +	struct rtase_private *tp = netdev_priv(dev);
> +	int ret;
> +
> +	pci_set_power_state(pdev, PCI_D0);
> +	pci_restore_state(pdev);
> +	pci_enable_wake(pdev, PCI_D0, 0);
> +

IMO this is done by the PCI core already. See other drivers
and PCI core code.

> +	/* restore last modified mac address */
> +	rtase_rar_set(tp, dev->dev_addr);
> +
> +	if (!netif_running(dev))
> +		goto out;
> +
> +	rtase_wait_for_quiescence(dev);
> +
> +	rtase_tx_clear(tp);
> +	rtase_rx_clear(tp);
> +
> +	ret = rtase_init_ring(dev);
> +	if (ret) {
> +		netdev_err(dev, "unable to init ring\n");
> +		rtase_free_desc(tp);
> +		return -ENOMEM;
> +	}
> +
> +	rtase_hw_config(dev);
> +	/* always link, so start to transmit & receive */
> +	rtase_hw_start(dev);
> +
> +	netif_carrier_on(dev);
> +	netif_wake_queue(dev);
> +	netif_device_attach(dev);
> +
> +out:
> +	return 0;
> +}
> +#endif /* CONFIG_PM */
> +
>  static struct pci_driver rtase_pci_driver = {
>  	.name = KBUILD_MODNAME,
>  	.id_table = rtase_pci_tbl,
>  	.probe = rtase_init_one,
>  	.remove = rtase_remove_one,
>  	.shutdown = rtase_shutdown,
> +#ifdef CONFIG_PM
> +	.suspend = rtase_suspend,
> +	.resume = rtase_resume,

Use pm_sleep_ptr and related macros then you don't need the
conditional compiling.

> +#endif
>  };
>
>  module_pci_driver(rtase_pci_driver);
Justin Lai Nov. 21, 2023, 9:30 a.m. UTC | #2
> On 15.11.2023 14:34, Justin Lai wrote:
> > Implement the pci_driver suspend function to enable the device to
> > sleep, and implement the resume function to enable the device to
> > resume operation.
> >
> > Signed-off-by: Justin Lai <justinlai0215@realtek.com>
> > ---
> >  .../net/ethernet/realtek/rtase/rtase_main.c   | 64 +++++++++++++++++++
> >  1 file changed, 64 insertions(+)
> >
> > diff --git a/drivers/net/ethernet/realtek/rtase/rtase_main.c
> > b/drivers/net/ethernet/realtek/rtase/rtase_main.c
> > index 12607663dd72..b7679b74cc8a 100644
> > --- a/drivers/net/ethernet/realtek/rtase/rtase_main.c
> > +++ b/drivers/net/ethernet/realtek/rtase/rtase_main.c
> > @@ -2323,12 +2323,76 @@ static void rtase_shutdown(struct pci_dev
> *pdev)
> >       rtase_reset_interrupt(pdev, tp);  }
> >
> > +#ifdef CONFIG_PM
> > +static int rtase_suspend(struct pci_dev *pdev, pm_message_t state) {
> > +     struct net_device *dev = pci_get_drvdata(pdev);
> > +
> > +     if (!netif_running(dev))
> > +             goto out;
> > +
> > +     netif_stop_queue(dev);
> > +     netif_carrier_off(dev);
> > +     netif_device_detach(dev);
> > +     rtase_hw_reset(dev);
> > +
> > +out:
> > +     pci_save_state(pdev);
> > +
> > +     return 0;
> > +}
> > +
> > +static int rtase_resume(struct pci_dev *pdev) {
> > +     struct net_device *dev = pci_get_drvdata(pdev);
> > +     struct rtase_private *tp = netdev_priv(dev);
> > +     int ret;
> > +
> > +     pci_set_power_state(pdev, PCI_D0);
> > +     pci_restore_state(pdev);
> > +     pci_enable_wake(pdev, PCI_D0, 0);
> > +
> 
> IMO this is done by the PCI core already. See other drivers and PCI core code.

Thank you, I will check this part.
> 
> > +     /* restore last modified mac address */
> > +     rtase_rar_set(tp, dev->dev_addr);
> > +
> > +     if (!netif_running(dev))
> > +             goto out;
> > +
> > +     rtase_wait_for_quiescence(dev);
> > +
> > +     rtase_tx_clear(tp);
> > +     rtase_rx_clear(tp);
> > +
> > +     ret = rtase_init_ring(dev);
> > +     if (ret) {
> > +             netdev_err(dev, "unable to init ring\n");
> > +             rtase_free_desc(tp);
> > +             return -ENOMEM;
> > +     }
> > +
> > +     rtase_hw_config(dev);
> > +     /* always link, so start to transmit & receive */
> > +     rtase_hw_start(dev);
> > +
> > +     netif_carrier_on(dev);
> > +     netif_wake_queue(dev);
> > +     netif_device_attach(dev);
> > +
> > +out:
> > +     return 0;
> > +}
> > +#endif /* CONFIG_PM */
> > +
> >  static struct pci_driver rtase_pci_driver = {
> >       .name = KBUILD_MODNAME,
> >       .id_table = rtase_pci_tbl,
> >       .probe = rtase_init_one,
> >       .remove = rtase_remove_one,
> >       .shutdown = rtase_shutdown,
> > +#ifdef CONFIG_PM
> > +     .suspend = rtase_suspend,
> > +     .resume = rtase_resume,
> 
> Use pm_sleep_ptr and related macros then you don't need the conditional
> compiling.

Thanks for your suggestion, I will modify this driver.
> 
> > +#endif
> >  };
> >
> >  module_pci_driver(rtase_pci_driver);
diff mbox series

Patch

diff --git a/drivers/net/ethernet/realtek/rtase/rtase_main.c b/drivers/net/ethernet/realtek/rtase/rtase_main.c
index 12607663dd72..b7679b74cc8a 100644
--- a/drivers/net/ethernet/realtek/rtase/rtase_main.c
+++ b/drivers/net/ethernet/realtek/rtase/rtase_main.c
@@ -2323,12 +2323,76 @@  static void rtase_shutdown(struct pci_dev *pdev)
 	rtase_reset_interrupt(pdev, tp);
 }
 
+#ifdef CONFIG_PM
+static int rtase_suspend(struct pci_dev *pdev, pm_message_t state)
+{
+	struct net_device *dev = pci_get_drvdata(pdev);
+
+	if (!netif_running(dev))
+		goto out;
+
+	netif_stop_queue(dev);
+	netif_carrier_off(dev);
+	netif_device_detach(dev);
+	rtase_hw_reset(dev);
+
+out:
+	pci_save_state(pdev);
+
+	return 0;
+}
+
+static int rtase_resume(struct pci_dev *pdev)
+{
+	struct net_device *dev = pci_get_drvdata(pdev);
+	struct rtase_private *tp = netdev_priv(dev);
+	int ret;
+
+	pci_set_power_state(pdev, PCI_D0);
+	pci_restore_state(pdev);
+	pci_enable_wake(pdev, PCI_D0, 0);
+
+	/* restore last modified mac address */
+	rtase_rar_set(tp, dev->dev_addr);
+
+	if (!netif_running(dev))
+		goto out;
+
+	rtase_wait_for_quiescence(dev);
+
+	rtase_tx_clear(tp);
+	rtase_rx_clear(tp);
+
+	ret = rtase_init_ring(dev);
+	if (ret) {
+		netdev_err(dev, "unable to init ring\n");
+		rtase_free_desc(tp);
+		return -ENOMEM;
+	}
+
+	rtase_hw_config(dev);
+	/* always link, so start to transmit & receive */
+	rtase_hw_start(dev);
+
+	netif_carrier_on(dev);
+	netif_wake_queue(dev);
+	netif_device_attach(dev);
+
+out:
+	return 0;
+}
+#endif /* CONFIG_PM */
+
 static struct pci_driver rtase_pci_driver = {
 	.name = KBUILD_MODNAME,
 	.id_table = rtase_pci_tbl,
 	.probe = rtase_init_one,
 	.remove = rtase_remove_one,
 	.shutdown = rtase_shutdown,
+#ifdef CONFIG_PM
+	.suspend = rtase_suspend,
+	.resume = rtase_resume,
+#endif
 };
 
 module_pci_driver(rtase_pci_driver);