Re: [PATCH v4 10/12] bus: mhi: core: Separate system error and power down handling

2020-11-15 Thread Manivannan Sadhasivam
On Mon, Nov 09, 2020 at 12:47:29PM -0800, Bhaumik Bhatt wrote:
> Currently, there exist a set of if...else statements in the
> mhi_pm_disable_transition() function which make handling system
> error and disable transitions differently complex. To make that
> cleaner and facilitate differences in behavior, separate these
> two transitions for MHI host.
> 
> Signed-off-by: Bhaumik Bhatt 

Reviewed-by: Manivannan Sadhasivam 

Thanks,
Mani

> ---
>  drivers/bus/mhi/core/pm.c | 159 
> +++---
>  1 file changed, 137 insertions(+), 22 deletions(-)
> 
> diff --git a/drivers/bus/mhi/core/pm.c b/drivers/bus/mhi/core/pm.c
> index 1d04e401..347ae7d 100644
> --- a/drivers/bus/mhi/core/pm.c
> +++ b/drivers/bus/mhi/core/pm.c
> @@ -444,7 +444,7 @@ static int mhi_pm_mission_mode_transition(struct 
> mhi_controller *mhi_cntrl)
>   return ret;
>  }
>  
> -/* Handle SYS_ERR and Shutdown transitions */
> +/* Handle shutdown transitions */
>  static void mhi_pm_disable_transition(struct mhi_controller *mhi_cntrl,
> enum mhi_pm_state transition_state)
>  {
> @@ -460,10 +460,6 @@ static void mhi_pm_disable_transition(struct 
> mhi_controller *mhi_cntrl,
>   to_mhi_pm_state_str(mhi_cntrl->pm_state),
>   to_mhi_pm_state_str(transition_state));
>  
> - /* We must notify MHI control driver so it can clean up first */
> - if (transition_state == MHI_PM_SYS_ERR_PROCESS)
> - mhi_cntrl->status_cb(mhi_cntrl, MHI_CB_SYS_ERROR);
> -
>   mutex_lock(_cntrl->pm_mutex);
>   write_lock_irq(_cntrl->pm_lock);
>   prev_state = mhi_cntrl->pm_state;
> @@ -502,11 +498,8 @@ static void mhi_pm_disable_transition(struct 
> mhi_controller *mhi_cntrl,
>   MHICTRL_RESET_SHIFT,
>   _reset) ||
>   !in_reset, timeout);
> - if ((!ret || in_reset) && cur_state == MHI_PM_SYS_ERR_PROCESS) {
> + if (!ret || in_reset)
>   dev_err(dev, "Device failed to exit MHI Reset state\n");
> - mutex_unlock(_cntrl->pm_mutex);
> - return;
> - }
>  
>   /*
>* Device will clear BHI_INTVEC as a part of RESET processing,
> @@ -566,19 +559,142 @@ static void mhi_pm_disable_transition(struct 
> mhi_controller *mhi_cntrl,
>   er_ctxt->wp = er_ctxt->rbase;
>   }
>  
> - if (cur_state == MHI_PM_SYS_ERR_PROCESS) {
> - mhi_ready_state_transition(mhi_cntrl);
> - } else {
> - /* Move to disable state */
> - write_lock_irq(_cntrl->pm_lock);
> - cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_DISABLE);
> - write_unlock_irq(_cntrl->pm_lock);
> - if (unlikely(cur_state != MHI_PM_DISABLE))
> - dev_err(dev, "Error moving from PM state: %s to: %s\n",
> - to_mhi_pm_state_str(cur_state),
> - to_mhi_pm_state_str(MHI_PM_DISABLE));
> + /* Move to disable state */
> + write_lock_irq(_cntrl->pm_lock);
> + cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_DISABLE);
> + write_unlock_irq(_cntrl->pm_lock);
> + if (unlikely(cur_state != MHI_PM_DISABLE))
> + dev_err(dev, "Error moving from PM state: %s to: %s\n",
> + to_mhi_pm_state_str(cur_state),
> + to_mhi_pm_state_str(MHI_PM_DISABLE));
> +
> + dev_dbg(dev, "Exiting with PM state: %s, MHI state: %s\n",
> + to_mhi_pm_state_str(mhi_cntrl->pm_state),
> + TO_MHI_STATE_STR(mhi_cntrl->dev_state));
> +
> + mutex_unlock(_cntrl->pm_mutex);
> +}
> +
> +/* Handle system error transitions */
> +static void mhi_pm_sys_error_transition(struct mhi_controller *mhi_cntrl)
> +{
> + enum mhi_pm_state cur_state, prev_state;
> + struct mhi_event *mhi_event;
> + struct mhi_cmd_ctxt *cmd_ctxt;
> + struct mhi_cmd *mhi_cmd;
> + struct mhi_event_ctxt *er_ctxt;
> + struct device *dev = _cntrl->mhi_dev->dev;
> + int ret, i;
> +
> + dev_dbg(dev, "Transitioning from PM state: %s to: %s\n",
> + to_mhi_pm_state_str(mhi_cntrl->pm_state),
> + to_mhi_pm_state_str(MHI_PM_SYS_ERR_PROCESS));
> +
> + /* We must notify MHI control driver so it can clean up first */
> + mhi_cntrl->status_cb(mhi_cntrl, MHI_CB_SYS_ERROR);
> +
> + mutex_lock(_cntrl->pm_mutex);
> + write_lock_irq(_cntrl->pm_lock);
> + prev_state = mhi_cntrl->pm_state;
> + cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_SYS_ERR_PROCESS);
> + write_unlock_irq(_cntrl->pm_lock);
> +
> + if (cur_state != MHI_PM_SYS_ERR_PROCESS) {
> + dev_err(dev, "Failed to transition from PM state: %s to: %s\n",
> + to_mhi_pm_state_str(cur_state),
> +   

[PATCH v4 10/12] bus: mhi: core: Separate system error and power down handling

2020-11-09 Thread Bhaumik Bhatt
Currently, there exist a set of if...else statements in the
mhi_pm_disable_transition() function which make handling system
error and disable transitions differently complex. To make that
cleaner and facilitate differences in behavior, separate these
two transitions for MHI host.

Signed-off-by: Bhaumik Bhatt 
---
 drivers/bus/mhi/core/pm.c | 159 +++---
 1 file changed, 137 insertions(+), 22 deletions(-)

diff --git a/drivers/bus/mhi/core/pm.c b/drivers/bus/mhi/core/pm.c
index 1d04e401..347ae7d 100644
--- a/drivers/bus/mhi/core/pm.c
+++ b/drivers/bus/mhi/core/pm.c
@@ -444,7 +444,7 @@ static int mhi_pm_mission_mode_transition(struct 
mhi_controller *mhi_cntrl)
return ret;
 }
 
-/* Handle SYS_ERR and Shutdown transitions */
+/* Handle shutdown transitions */
 static void mhi_pm_disable_transition(struct mhi_controller *mhi_cntrl,
  enum mhi_pm_state transition_state)
 {
@@ -460,10 +460,6 @@ static void mhi_pm_disable_transition(struct 
mhi_controller *mhi_cntrl,
to_mhi_pm_state_str(mhi_cntrl->pm_state),
to_mhi_pm_state_str(transition_state));
 
-   /* We must notify MHI control driver so it can clean up first */
-   if (transition_state == MHI_PM_SYS_ERR_PROCESS)
-   mhi_cntrl->status_cb(mhi_cntrl, MHI_CB_SYS_ERROR);
-
mutex_lock(_cntrl->pm_mutex);
write_lock_irq(_cntrl->pm_lock);
prev_state = mhi_cntrl->pm_state;
@@ -502,11 +498,8 @@ static void mhi_pm_disable_transition(struct 
mhi_controller *mhi_cntrl,
MHICTRL_RESET_SHIFT,
_reset) ||
!in_reset, timeout);
-   if ((!ret || in_reset) && cur_state == MHI_PM_SYS_ERR_PROCESS) {
+   if (!ret || in_reset)
dev_err(dev, "Device failed to exit MHI Reset state\n");
-   mutex_unlock(_cntrl->pm_mutex);
-   return;
-   }
 
/*
 * Device will clear BHI_INTVEC as a part of RESET processing,
@@ -566,19 +559,142 @@ static void mhi_pm_disable_transition(struct 
mhi_controller *mhi_cntrl,
er_ctxt->wp = er_ctxt->rbase;
}
 
-   if (cur_state == MHI_PM_SYS_ERR_PROCESS) {
-   mhi_ready_state_transition(mhi_cntrl);
-   } else {
-   /* Move to disable state */
-   write_lock_irq(_cntrl->pm_lock);
-   cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_DISABLE);
-   write_unlock_irq(_cntrl->pm_lock);
-   if (unlikely(cur_state != MHI_PM_DISABLE))
-   dev_err(dev, "Error moving from PM state: %s to: %s\n",
-   to_mhi_pm_state_str(cur_state),
-   to_mhi_pm_state_str(MHI_PM_DISABLE));
+   /* Move to disable state */
+   write_lock_irq(_cntrl->pm_lock);
+   cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_DISABLE);
+   write_unlock_irq(_cntrl->pm_lock);
+   if (unlikely(cur_state != MHI_PM_DISABLE))
+   dev_err(dev, "Error moving from PM state: %s to: %s\n",
+   to_mhi_pm_state_str(cur_state),
+   to_mhi_pm_state_str(MHI_PM_DISABLE));
+
+   dev_dbg(dev, "Exiting with PM state: %s, MHI state: %s\n",
+   to_mhi_pm_state_str(mhi_cntrl->pm_state),
+   TO_MHI_STATE_STR(mhi_cntrl->dev_state));
+
+   mutex_unlock(_cntrl->pm_mutex);
+}
+
+/* Handle system error transitions */
+static void mhi_pm_sys_error_transition(struct mhi_controller *mhi_cntrl)
+{
+   enum mhi_pm_state cur_state, prev_state;
+   struct mhi_event *mhi_event;
+   struct mhi_cmd_ctxt *cmd_ctxt;
+   struct mhi_cmd *mhi_cmd;
+   struct mhi_event_ctxt *er_ctxt;
+   struct device *dev = _cntrl->mhi_dev->dev;
+   int ret, i;
+
+   dev_dbg(dev, "Transitioning from PM state: %s to: %s\n",
+   to_mhi_pm_state_str(mhi_cntrl->pm_state),
+   to_mhi_pm_state_str(MHI_PM_SYS_ERR_PROCESS));
+
+   /* We must notify MHI control driver so it can clean up first */
+   mhi_cntrl->status_cb(mhi_cntrl, MHI_CB_SYS_ERROR);
+
+   mutex_lock(_cntrl->pm_mutex);
+   write_lock_irq(_cntrl->pm_lock);
+   prev_state = mhi_cntrl->pm_state;
+   cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_SYS_ERR_PROCESS);
+   write_unlock_irq(_cntrl->pm_lock);
+
+   if (cur_state != MHI_PM_SYS_ERR_PROCESS) {
+   dev_err(dev, "Failed to transition from PM state: %s to: %s\n",
+   to_mhi_pm_state_str(cur_state),
+   to_mhi_pm_state_str(MHI_PM_SYS_ERR_PROCESS));
+   goto exit_sys_error_transition;
+   }
+
+   mhi_cntrl->ee = MHI_EE_DISABLE_TRANSITION;
+   mhi_cntrl->dev_state =