Re: [PATCH v1] mptfusion: use generic power management

2020-08-17 Thread Vaibhav Gupta
On Tue, Jul 21, 2020 at 07:54:24PM +0530, Vaibhav Gupta wrote:
> Drivers using legacy power management .suspen()/.resume() callbacks
> have to manage PCI states and device's PM states themselves. They also
> need to take care of standard configuration registers.
> 
> Switch to generic power management framework using a single
> "struct dev_pm_ops" variable to take the unnecessary load from the driver.
> This also avoids the need for the driver to directly call most of the PCI
> helper functions and device power state control functions as through
> the generic framework, PCI Core takes care of the necessary operations,
> and drivers are required to do only device-specific jobs.
> 
> Signed-off-by: Vaibhav Gupta 
> ---
>  drivers/message/fusion/mptbase.c  | 36 +++
>  drivers/message/fusion/mptbase.h  |  7 +++---
>  drivers/message/fusion/mptfc.c|  5 +
>  drivers/message/fusion/mptsas.c   |  5 +
>  drivers/message/fusion/mptscsih.c | 36 +++
>  drivers/message/fusion/mptscsih.h |  7 +++---
>  drivers/message/fusion/mptspi.c   | 26 +-
>  7 files changed, 53 insertions(+), 69 deletions(-)
> 
> diff --git a/drivers/message/fusion/mptbase.c 
> b/drivers/message/fusion/mptbase.c
> index 5216487db4fb..13a839c855a1 100644
> --- a/drivers/message/fusion/mptbase.c
> +++ b/drivers/message/fusion/mptbase.c
> @@ -2139,23 +2139,19 @@ mpt_detach(struct pci_dev *pdev)
>  /**
>   * Power Management
>   */
> -#ifdef CONFIG_PM
>  
> /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
>  /**
>   *   mpt_suspend - Fusion MPT base driver suspend routine.
> - *   @pdev: Pointer to pci_dev structure
> - *   @state: new state to enter
> + *   @dev: Pointer to device structure
>   */
> -int
> -mpt_suspend(struct pci_dev *pdev, pm_message_t state)
> +static int __maybe_unused
> +mpt_suspend(struct device *dev)
>  {
> - u32 device_state;
> + struct pci_dev *pdev = to_pci_dev(dev);
>   MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
>  
> - device_state = pci_choose_state(pdev, state);
>   printk(MYIOC_s_INFO_FMT "pci-suspend: pdev=0x%p, slot=%s, Entering "
> - "operating state [D%d]\n", ioc->name, pdev, pci_name(pdev),
> - device_state);
> + "suspend state\n", ioc->name, pdev, pci_name(pdev));
>  
>   /* put ioc into READY_STATE */
>   if (SendIocReset(ioc, MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET, CAN_SLEEP)) {
> @@ -2174,21 +2170,18 @@ mpt_suspend(struct pci_dev *pdev, pm_message_t state)
>   if (ioc->msi_enable)
>   pci_disable_msi(ioc->pcidev);
>   ioc->pci_irq = -1;
> - pci_save_state(pdev);
> - pci_disable_device(pdev);
> - pci_release_selected_regions(pdev, ioc->bars);
> - pci_set_power_state(pdev, device_state);
>   return 0;
>  }
>  
>  
> /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
>  /**
>   *   mpt_resume - Fusion MPT base driver resume routine.
> - *   @pdev: Pointer to pci_dev structure
> + *   @dev: Pointer to device structure
>   */
> -int
> -mpt_resume(struct pci_dev *pdev)
> +static int __maybe_unused
> +mpt_resume(struct device *dev)
>  {
> + struct pci_dev *pdev = to_pci_dev(dev);
>   MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
>   u32 device_state = pdev->current_state;
>   int recovery_state;
> @@ -2198,9 +2191,6 @@ mpt_resume(struct pci_dev *pdev)
>   "operating state [D%d]\n", ioc->name, pdev, pci_name(pdev),
>   device_state);
>  
> - pci_set_power_state(pdev, PCI_D0);
> - pci_enable_wake(pdev, PCI_D0, 0);
> - pci_restore_state(pdev);
>   ioc->pcidev = pdev;
>   err = mpt_mapresources(ioc);
>   if (err)
> @@ -2256,7 +2246,9 @@ mpt_resume(struct pci_dev *pdev)
>   return 0;
>  
>  }
> -#endif
> +
> +SIMPLE_DEV_PM_OPS(mpt_pm_ops, mpt_suspend, mpt_resume);
> +EXPORT_SYMBOL(mpt_pm_ops);
>  
>  static int
>  mpt_signal_reset(u8 index, MPT_ADAPTER *ioc, int reset_phase)
> @@ -8440,10 +8432,6 @@ mpt_iocstatus_info(MPT_ADAPTER *ioc, u32 ioc_status, 
> MPT_FRAME_HDR *mf)
>  
> /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
>  EXPORT_SYMBOL(mpt_attach);
>  EXPORT_SYMBOL(mpt_detach);
> -#ifdef CONFIG_PM
> -EXPORT_SYMBOL(mpt_resume);
> -EXPORT_SYMBOL(mpt_suspend);
> -#endif
>  EXPORT_SYMBOL(ioc_list);
>  EXPORT_SYMBOL(mpt_register);
>  EXPORT_SYMBOL(mpt_deregister);
> diff --git a/drivers/message/fusion/mptbase.h 
> b/drivers/message/fusion/mptbase.h
> index 813d46311f6a..b4ae350acd6c 100644
> --- a/drivers/message/fusion/mptbase.h
> +++ b/drivers/message/fusion/mptbase.h
> @@ -909,10 +909,9 @@ typedef struct _x_config_parms {
>   */
>  extern intmpt_attach(struct pci_dev *pdev, const struct pci_device_id 
> *id);
>  extern void   mpt_detach(struct pci_dev *pdev);
> -#ifdef CONFIG_PM
> -extern intmpt_suspend(struct pci_dev 

Re: [PATCH v1] mptfusion: use generic power management

2020-07-21 Thread Vaibhav Gupta
This patch is compile-tested only.

--Vaibhav Gupta


[PATCH v1] mptfusion: use generic power management

2020-07-21 Thread Vaibhav Gupta
Drivers using legacy power management .suspen()/.resume() callbacks
have to manage PCI states and device's PM states themselves. They also
need to take care of standard configuration registers.

Switch to generic power management framework using a single
"struct dev_pm_ops" variable to take the unnecessary load from the driver.
This also avoids the need for the driver to directly call most of the PCI
helper functions and device power state control functions as through
the generic framework, PCI Core takes care of the necessary operations,
and drivers are required to do only device-specific jobs.

Signed-off-by: Vaibhav Gupta 
---
 drivers/message/fusion/mptbase.c  | 36 +++
 drivers/message/fusion/mptbase.h  |  7 +++---
 drivers/message/fusion/mptfc.c|  5 +
 drivers/message/fusion/mptsas.c   |  5 +
 drivers/message/fusion/mptscsih.c | 36 +++
 drivers/message/fusion/mptscsih.h |  7 +++---
 drivers/message/fusion/mptspi.c   | 26 +-
 7 files changed, 53 insertions(+), 69 deletions(-)

diff --git a/drivers/message/fusion/mptbase.c b/drivers/message/fusion/mptbase.c
index 5216487db4fb..13a839c855a1 100644
--- a/drivers/message/fusion/mptbase.c
+++ b/drivers/message/fusion/mptbase.c
@@ -2139,23 +2139,19 @@ mpt_detach(struct pci_dev *pdev)
 /**
  * Power Management
  */
-#ifdef CONFIG_PM
 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
 /**
  * mpt_suspend - Fusion MPT base driver suspend routine.
- * @pdev: Pointer to pci_dev structure
- * @state: new state to enter
+ * @dev: Pointer to device structure
  */
-int
-mpt_suspend(struct pci_dev *pdev, pm_message_t state)
+static int __maybe_unused
+mpt_suspend(struct device *dev)
 {
-   u32 device_state;
+   struct pci_dev *pdev = to_pci_dev(dev);
MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
 
-   device_state = pci_choose_state(pdev, state);
printk(MYIOC_s_INFO_FMT "pci-suspend: pdev=0x%p, slot=%s, Entering "
-   "operating state [D%d]\n", ioc->name, pdev, pci_name(pdev),
-   device_state);
+   "suspend state\n", ioc->name, pdev, pci_name(pdev));
 
/* put ioc into READY_STATE */
if (SendIocReset(ioc, MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET, CAN_SLEEP)) {
@@ -2174,21 +2170,18 @@ mpt_suspend(struct pci_dev *pdev, pm_message_t state)
if (ioc->msi_enable)
pci_disable_msi(ioc->pcidev);
ioc->pci_irq = -1;
-   pci_save_state(pdev);
-   pci_disable_device(pdev);
-   pci_release_selected_regions(pdev, ioc->bars);
-   pci_set_power_state(pdev, device_state);
return 0;
 }
 
 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
 /**
  * mpt_resume - Fusion MPT base driver resume routine.
- * @pdev: Pointer to pci_dev structure
+ * @dev: Pointer to device structure
  */
-int
-mpt_resume(struct pci_dev *pdev)
+static int __maybe_unused
+mpt_resume(struct device *dev)
 {
+   struct pci_dev *pdev = to_pci_dev(dev);
MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
u32 device_state = pdev->current_state;
int recovery_state;
@@ -2198,9 +2191,6 @@ mpt_resume(struct pci_dev *pdev)
"operating state [D%d]\n", ioc->name, pdev, pci_name(pdev),
device_state);
 
-   pci_set_power_state(pdev, PCI_D0);
-   pci_enable_wake(pdev, PCI_D0, 0);
-   pci_restore_state(pdev);
ioc->pcidev = pdev;
err = mpt_mapresources(ioc);
if (err)
@@ -2256,7 +2246,9 @@ mpt_resume(struct pci_dev *pdev)
return 0;
 
 }
-#endif
+
+SIMPLE_DEV_PM_OPS(mpt_pm_ops, mpt_suspend, mpt_resume);
+EXPORT_SYMBOL(mpt_pm_ops);
 
 static int
 mpt_signal_reset(u8 index, MPT_ADAPTER *ioc, int reset_phase)
@@ -8440,10 +8432,6 @@ mpt_iocstatus_info(MPT_ADAPTER *ioc, u32 ioc_status, 
MPT_FRAME_HDR *mf)
 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
 EXPORT_SYMBOL(mpt_attach);
 EXPORT_SYMBOL(mpt_detach);
-#ifdef CONFIG_PM
-EXPORT_SYMBOL(mpt_resume);
-EXPORT_SYMBOL(mpt_suspend);
-#endif
 EXPORT_SYMBOL(ioc_list);
 EXPORT_SYMBOL(mpt_register);
 EXPORT_SYMBOL(mpt_deregister);
diff --git a/drivers/message/fusion/mptbase.h b/drivers/message/fusion/mptbase.h
index 813d46311f6a..b4ae350acd6c 100644
--- a/drivers/message/fusion/mptbase.h
+++ b/drivers/message/fusion/mptbase.h
@@ -909,10 +909,9 @@ typedef struct _x_config_parms {
  */
 extern int  mpt_attach(struct pci_dev *pdev, const struct pci_device_id 
*id);
 extern void mpt_detach(struct pci_dev *pdev);
-#ifdef CONFIG_PM
-extern int  mpt_suspend(struct pci_dev *pdev, pm_message_t state);
-extern int  mpt_resume(struct pci_dev *pdev);
-#endif
+
+extern const struct dev_pm_ops mpt_pm_ops;
+
 extern u8   mpt_register(MPT_CALLBACK cbfunc, MPT_DRIVER_CLASS dclass,
char *func_name);