diff mbox

[Lucid] pull-request: mmc: fix all hangs related to mmc/sd card insert/removal during suspend/resume

Message ID 4C8E267C.9000502@canonical.com
State Accepted
Headers show

Commit Message

Lee Jones Sept. 13, 2010, 1:26 p.m. UTC
The following changes since commit 5470af5fd104192214b496a2736fd26200f4650d:
  Steve Conklin (1):
        UBUNTU: [Config] updateconfigs

are available in the git repository at:

  git://kernel.ubuntu.com/lag/ubuntu-lucid.git lp477106

Lee Jones (1):
      mmc: fix all hangs related to mmc/sd card insert/removal during suspend/resume

 drivers/mmc/core/core.c  |   81 +++++++++++++++++++++++++++++++--------------
 drivers/mmc/core/host.c  |    5 +++
 include/linux/mmc/host.h |    3 ++
 3 files changed, 64 insertions(+), 25 deletions(-)

----------------------------------------------------------------------

mmc: fix all hangs related to mmc/sd card insert/removal during suspend/resume
    
Based on a patch-set originally written by Maxim Levitsky.
    
If you don't use CONFIG_MMC_UNSAFE_RESUME, as soon as you attempt to
suspend, the card will be removed, therefore this patch doesn't change the
behavior of this option.
    
However the removal will be done by pm notifier, which runs while
userspace is still not frozen and thus can freely use del_gendisk, without
the risk of deadlock which would happen otherwise.
    
Card detect workqueue is now disabled while userspace is frozen, Therefore
if you do use CONFIG_MMC_UNSAFE_RESUME, and remove the card during
suspend, the removal will be detected as soon as userspace is unfrozen,
again at the moment it is safe to call del_gendisk.
    
Tested with and without CONFIG_MMC_UNSAFE_RESUME with suspend and hibernate.
    
    [akpm@linux-foundation.org: clean up function prototype]
    [akpm@linux-foundation.org: fix CONFIG_PM-n linkage, small cleanups]
    [akpm@linux-foundation.org: coding-style fixes]
    Signed-off-by: Maxim Levitsky <maximlevitsky@gmail.com>
    Cc: David Brownell <david-b@pacbell.net>
    Cc: Alan Stern <stern@rowland.harvard.edu>
    Cc: <linux-mmc@vger.kernel.org>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
    
    [Backported to Ubuntu-2.6.32-25.43 by Lee Jones]
    Signed-off-by: Lee Jones <lee.jones@canonical.com>
    
    BugLink: http://bugs.launchpad.net/bugs/477106

---

Comments

Stefan Bader Sept. 13, 2010, 1:32 p.m. UTC | #1
On 09/13/2010 03:26 PM, Lee Jones wrote:
> The following changes since commit 5470af5fd104192214b496a2736fd26200f4650d:
>   Steve Conklin (1):
>         UBUNTU: [Config] updateconfigs
> 
> are available in the git repository at:
> 
>   git://kernel.ubuntu.com/lag/ubuntu-lucid.git lp477106
> 
> Lee Jones (1):
>       mmc: fix all hangs related to mmc/sd card insert/removal during suspend/resume
> 
>  drivers/mmc/core/core.c  |   81 +++++++++++++++++++++++++++++++--------------
>  drivers/mmc/core/host.c  |    5 +++
>  include/linux/mmc/host.h |    3 ++
>  3 files changed, 64 insertions(+), 25 deletions(-)
> 

Hm, did I not ack this already some time ago? Reading through the mails today I
think to remember a fixup on Maverick to #ifdef the notifier call. Would that
also apply to Lucid?

-Stefan
> ----------------------------------------------------------------------
> 
> mmc: fix all hangs related to mmc/sd card insert/removal during suspend/resume
>     
> Based on a patch-set originally written by Maxim Levitsky.
>     
> If you don't use CONFIG_MMC_UNSAFE_RESUME, as soon as you attempt to
> suspend, the card will be removed, therefore this patch doesn't change the
> behavior of this option.
>     
> However the removal will be done by pm notifier, which runs while
> userspace is still not frozen and thus can freely use del_gendisk, without
> the risk of deadlock which would happen otherwise.
>     
> Card detect workqueue is now disabled while userspace is frozen, Therefore
> if you do use CONFIG_MMC_UNSAFE_RESUME, and remove the card during
> suspend, the removal will be detected as soon as userspace is unfrozen,
> again at the moment it is safe to call del_gendisk.
>     
> Tested with and without CONFIG_MMC_UNSAFE_RESUME with suspend and hibernate.
>     
>     [akpm@linux-foundation.org: clean up function prototype]
>     [akpm@linux-foundation.org: fix CONFIG_PM-n linkage, small cleanups]
>     [akpm@linux-foundation.org: coding-style fixes]
>     Signed-off-by: Maxim Levitsky <maximlevitsky@gmail.com>
>     Cc: David Brownell <david-b@pacbell.net>
>     Cc: Alan Stern <stern@rowland.harvard.edu>
>     Cc: <linux-mmc@vger.kernel.org>
>     Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
>     Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
>     
>     [Backported to Ubuntu-2.6.32-25.43 by Lee Jones]
>     Signed-off-by: Lee Jones <lee.jones@canonical.com>
>     
>     BugLink: http://bugs.launchpad.net/bugs/477106
> 
> ---
> 
> diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
> index 30acd52..46f6157 100644
> --- a/drivers/mmc/core/core.c
> +++ b/drivers/mmc/core/core.c
> @@ -1057,6 +1057,17 @@ void mmc_rescan(struct work_struct *work)
>  		container_of(work, struct mmc_host, detect.work);
>  	u32 ocr;
>  	int err;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&host->lock, flags);
> +
> +	if (host->rescan_disable) {
> +		spin_unlock_irqrestore(&host->lock, flags);
> +		return;
> +	}
> +
> +	spin_unlock_irqrestore(&host->lock, flags);
> +
>  
>  	mmc_bus_get(host);
>  
> @@ -1263,18 +1274,6 @@ int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
>  	if (host->bus_ops && !host->bus_dead) {
>  		if (host->bus_ops->suspend)
>  			err = host->bus_ops->suspend(host);
> -		if (err == -ENOSYS || !host->bus_ops->resume) {
> -			/*
> -			 * We simply "remove" the card in this case.
> -			 * It will be redetected on resume.
> -			 */
> -			if (host->bus_ops->remove)
> -				host->bus_ops->remove(host);
> -			mmc_claim_host(host);
> -			mmc_detach_bus(host);
> -			mmc_release_host(host);
> -			err = 0;
> -		}
>  	}
>  	mmc_bus_put(host);
>  
> @@ -1304,28 +1303,60 @@ int mmc_resume_host(struct mmc_host *host)
>  			printk(KERN_WARNING "%s: error %d during resume "
>  					    "(card was removed?)\n",
>  					    mmc_hostname(host), err);
> -			if (host->bus_ops->remove)
> -				host->bus_ops->remove(host);
> -			mmc_claim_host(host);
> -			mmc_detach_bus(host);
> -			mmc_release_host(host);
> -			/* no need to bother upper layers */
>  			err = 0;
>  		}
>  	}
>  	mmc_bus_put(host);
>  
> -	/*
> -	 * We add a slight delay here so that resume can progress
> -	 * in parallel.
> -	 */
> -	mmc_detect_change(host, 1);
> -
>  	return err;
>  }
> -
>  EXPORT_SYMBOL(mmc_resume_host);
>  
> +/* Do the card removal on suspend if card is assumed removeable
> + * Do that in pm notifier while userspace isn't yet frozen, so we will be able
> +   to sync the card.
> +*/
> +int mmc_pm_notify(struct notifier_block *notify_block,
> +					unsigned long mode, void *unused)
> +{
> +	struct mmc_host *host = container_of(
> +		notify_block, struct mmc_host, pm_notify);
> +	unsigned long flags;
> +
> +
> +	switch (mode) {
> +	case PM_HIBERNATION_PREPARE:
> +	case PM_SUSPEND_PREPARE:
> +
> +		spin_lock_irqsave(&host->lock, flags);
> +		host->rescan_disable = 1;
> +		spin_unlock_irqrestore(&host->lock, flags);
> +		cancel_delayed_work_sync(&host->detect);
> +
> +		if (!host->bus_ops || host->bus_ops->suspend)
> +			break;
> +
> +		mmc_claim_host(host);
> +
> +		if (host->bus_ops->remove)
> +			host->bus_ops->remove(host);
> +
> +		mmc_detach_bus(host);
> +		mmc_release_host(host);
> +		break;
> +
> +	case PM_POST_SUSPEND:
> +	case PM_POST_HIBERNATION:
> +
> +		spin_lock_irqsave(&host->lock, flags);
> +		host->rescan_disable = 0;
> +		spin_unlock_irqrestore(&host->lock, flags);
> +		mmc_detect_change(host, 0);
> +
> +	}
> +
> +	return 0;
> +}
>  #endif
>  
>  static int __init mmc_init(void)
> diff --git a/drivers/mmc/core/host.c b/drivers/mmc/core/host.c
> index a268d12..0efe631 100644
> --- a/drivers/mmc/core/host.c
> +++ b/drivers/mmc/core/host.c
> @@ -16,6 +16,8 @@
>  #include <linux/idr.h>
>  #include <linux/pagemap.h>
>  #include <linux/leds.h>
> +#include <linux/slab.h>
> +#include <linux/suspend.h>
>  
>  #include <linux/mmc/host.h>
>  
> @@ -84,6 +86,7 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
>  	init_waitqueue_head(&host->wq);
>  	INIT_DELAYED_WORK(&host->detect, mmc_rescan);
>  	INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);
> +	host->pm_notify.notifier_call = mmc_pm_notify;
>  
>  	/*
>  	 * By default, hosts do not support SGIO or large requests.
> @@ -132,6 +135,7 @@ int mmc_add_host(struct mmc_host *host)
>  #endif
>  
>  	mmc_start_host(host);
> +	register_pm_notifier(&host->pm_notify);
>  
>  	return 0;
>  }
> @@ -148,6 +152,7 @@ EXPORT_SYMBOL(mmc_add_host);
>   */
>  void mmc_remove_host(struct mmc_host *host)
>  {
> +	unregister_pm_notifier(&host->pm_notify);
>  	mmc_stop_host(host);
>  
>  #ifdef CONFIG_DEBUG_FS
> diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h
> index eaf3636..475ca8a 100644
> --- a/include/linux/mmc/host.h
> +++ b/include/linux/mmc/host.h
> @@ -120,6 +120,7 @@ struct mmc_host {
>  	unsigned int		f_min;
>  	unsigned int		f_max;
>  	u32			ocr_avail;
> +	struct notifier_block	pm_notify;
>  
>  #define MMC_VDD_165_195		0x00000080	/* VDD voltage 1.65 - 1.95 */
>  #define MMC_VDD_20_21		0x00000100	/* VDD voltage 2.0 ~ 2.1 */
> @@ -177,6 +178,7 @@ struct mmc_host {
>  
>  	/* Only used with MMC_CAP_DISABLE */
>  	int			enabled;	/* host is enabled */
> +	int			rescan_disable;	/* disable card detection */
>  	int			nesting_cnt;	/* "enable" nesting count */
>  	int			en_dis_recurs;	/* detect recursion */
>  	unsigned int		disable_delay;	/* disable delay in msecs */
> @@ -249,6 +251,7 @@ int mmc_card_can_sleep(struct mmc_host *host);
>  int mmc_host_enable(struct mmc_host *host);
>  int mmc_host_disable(struct mmc_host *host);
>  int mmc_host_lazy_disable(struct mmc_host *host);
> +int mmc_pm_notify(struct notifier_block *notify_block, unsigned long, void *);
>  
>  static inline void mmc_set_disable_delay(struct mmc_host *host,
>  					 unsigned int disable_delay)
>
Brad Figg Sept. 13, 2010, 4:23 p.m. UTC | #2
On 09/13/2010 06:26 AM, Lee Jones wrote:
> The following changes since commit 5470af5fd104192214b496a2736fd26200f4650d:
>    Steve Conklin (1):
>          UBUNTU: [Config] updateconfigs
>
> are available in the git repository at:
>
>    git://kernel.ubuntu.com/lag/ubuntu-lucid.git lp477106
>
> Lee Jones (1):
>        mmc: fix all hangs related to mmc/sd card insert/removal during suspend/resume
>
>   drivers/mmc/core/core.c  |   81 +++++++++++++++++++++++++++++++--------------
>   drivers/mmc/core/host.c  |    5 +++
>   include/linux/mmc/host.h |    3 ++
>   3 files changed, 64 insertions(+), 25 deletions(-)
>
> ----------------------------------------------------------------------
>
> mmc: fix all hangs related to mmc/sd card insert/removal during suspend/resume
>
> Based on a patch-set originally written by Maxim Levitsky.
>
> If you don't use CONFIG_MMC_UNSAFE_RESUME, as soon as you attempt to
> suspend, the card will be removed, therefore this patch doesn't change the
> behavior of this option.
>
> However the removal will be done by pm notifier, which runs while
> userspace is still not frozen and thus can freely use del_gendisk, without
> the risk of deadlock which would happen otherwise.
>
> Card detect workqueue is now disabled while userspace is frozen, Therefore
> if you do use CONFIG_MMC_UNSAFE_RESUME, and remove the card during
> suspend, the removal will be detected as soon as userspace is unfrozen,
> again at the moment it is safe to call del_gendisk.
>
> Tested with and without CONFIG_MMC_UNSAFE_RESUME with suspend and hibernate.
>
>      [akpm@linux-foundation.org: clean up function prototype]
>      [akpm@linux-foundation.org: fix CONFIG_PM-n linkage, small cleanups]
>      [akpm@linux-foundation.org: coding-style fixes]
>      Signed-off-by: Maxim Levitsky<maximlevitsky@gmail.com>
>      Cc: David Brownell<david-b@pacbell.net>
>      Cc: Alan Stern<stern@rowland.harvard.edu>
>      Cc:<linux-mmc@vger.kernel.org>
>      Signed-off-by: Andrew Morton<akpm@linux-foundation.org>
>      Signed-off-by: Linus Torvalds<torvalds@linux-foundation.org>
>
>      [Backported to Ubuntu-2.6.32-25.43 by Lee Jones]
>      Signed-off-by: Lee Jones<lee.jones@canonical.com>
>
>      BugLink: http://bugs.launchpad.net/bugs/477106
>
> ---
>
> diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
> index 30acd52..46f6157 100644
> --- a/drivers/mmc/core/core.c
> +++ b/drivers/mmc/core/core.c
> @@ -1057,6 +1057,17 @@ void mmc_rescan(struct work_struct *work)
>   		container_of(work, struct mmc_host, detect.work);
>   	u32 ocr;
>   	int err;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&host->lock, flags);
> +
> +	if (host->rescan_disable) {
> +		spin_unlock_irqrestore(&host->lock, flags);
> +		return;
> +	}
> +
> +	spin_unlock_irqrestore(&host->lock, flags);
> +
>
>   	mmc_bus_get(host);
>
> @@ -1263,18 +1274,6 @@ int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
>   	if (host->bus_ops&&  !host->bus_dead) {
>   		if (host->bus_ops->suspend)
>   			err = host->bus_ops->suspend(host);
> -		if (err == -ENOSYS || !host->bus_ops->resume) {
> -			/*
> -			 * We simply "remove" the card in this case.
> -			 * It will be redetected on resume.
> -			 */
> -			if (host->bus_ops->remove)
> -				host->bus_ops->remove(host);
> -			mmc_claim_host(host);
> -			mmc_detach_bus(host);
> -			mmc_release_host(host);
> -			err = 0;
> -		}
>   	}
>   	mmc_bus_put(host);
>
> @@ -1304,28 +1303,60 @@ int mmc_resume_host(struct mmc_host *host)
>   			printk(KERN_WARNING "%s: error %d during resume "
>   					    "(card was removed?)\n",
>   					    mmc_hostname(host), err);
> -			if (host->bus_ops->remove)
> -				host->bus_ops->remove(host);
> -			mmc_claim_host(host);
> -			mmc_detach_bus(host);
> -			mmc_release_host(host);
> -			/* no need to bother upper layers */
>   			err = 0;
>   		}
>   	}
>   	mmc_bus_put(host);
>
> -	/*
> -	 * We add a slight delay here so that resume can progress
> -	 * in parallel.
> -	 */
> -	mmc_detect_change(host, 1);
> -
>   	return err;
>   }
> -
>   EXPORT_SYMBOL(mmc_resume_host);
>
> +/* Do the card removal on suspend if card is assumed removeable
> + * Do that in pm notifier while userspace isn't yet frozen, so we will be able
> +   to sync the card.
> +*/
> +int mmc_pm_notify(struct notifier_block *notify_block,
> +					unsigned long mode, void *unused)
> +{
> +	struct mmc_host *host = container_of(
> +		notify_block, struct mmc_host, pm_notify);
> +	unsigned long flags;
> +
> +
> +	switch (mode) {
> +	case PM_HIBERNATION_PREPARE:
> +	case PM_SUSPEND_PREPARE:
> +
> +		spin_lock_irqsave(&host->lock, flags);
> +		host->rescan_disable = 1;
> +		spin_unlock_irqrestore(&host->lock, flags);
> +		cancel_delayed_work_sync(&host->detect);
> +
> +		if (!host->bus_ops || host->bus_ops->suspend)
> +			break;
> +
> +		mmc_claim_host(host);
> +
> +		if (host->bus_ops->remove)
> +			host->bus_ops->remove(host);
> +
> +		mmc_detach_bus(host);
> +		mmc_release_host(host);
> +		break;
> +
> +	case PM_POST_SUSPEND:
> +	case PM_POST_HIBERNATION:
> +
> +		spin_lock_irqsave(&host->lock, flags);
> +		host->rescan_disable = 0;
> +		spin_unlock_irqrestore(&host->lock, flags);
> +		mmc_detect_change(host, 0);
> +
> +	}
> +
> +	return 0;
> +}
>   #endif
>
>   static int __init mmc_init(void)
> diff --git a/drivers/mmc/core/host.c b/drivers/mmc/core/host.c
> index a268d12..0efe631 100644
> --- a/drivers/mmc/core/host.c
> +++ b/drivers/mmc/core/host.c
> @@ -16,6 +16,8 @@
>   #include<linux/idr.h>
>   #include<linux/pagemap.h>
>   #include<linux/leds.h>
> +#include<linux/slab.h>
> +#include<linux/suspend.h>
>
>   #include<linux/mmc/host.h>
>
> @@ -84,6 +86,7 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
>   	init_waitqueue_head(&host->wq);
>   	INIT_DELAYED_WORK(&host->detect, mmc_rescan);
>   	INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);
> +	host->pm_notify.notifier_call = mmc_pm_notify;
>
>   	/*
>   	 * By default, hosts do not support SGIO or large requests.
> @@ -132,6 +135,7 @@ int mmc_add_host(struct mmc_host *host)
>   #endif
>
>   	mmc_start_host(host);
> +	register_pm_notifier(&host->pm_notify);
>
>   	return 0;
>   }
> @@ -148,6 +152,7 @@ EXPORT_SYMBOL(mmc_add_host);
>    */
>   void mmc_remove_host(struct mmc_host *host)
>   {
> +	unregister_pm_notifier(&host->pm_notify);
>   	mmc_stop_host(host);
>
>   #ifdef CONFIG_DEBUG_FS
> diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h
> index eaf3636..475ca8a 100644
> --- a/include/linux/mmc/host.h
> +++ b/include/linux/mmc/host.h
> @@ -120,6 +120,7 @@ struct mmc_host {
>   	unsigned int		f_min;
>   	unsigned int		f_max;
>   	u32			ocr_avail;
> +	struct notifier_block	pm_notify;
>
>   #define MMC_VDD_165_195		0x00000080	/* VDD voltage 1.65 - 1.95 */
>   #define MMC_VDD_20_21		0x00000100	/* VDD voltage 2.0 ~ 2.1 */
> @@ -177,6 +178,7 @@ struct mmc_host {
>
>   	/* Only used with MMC_CAP_DISABLE */
>   	int			enabled;	/* host is enabled */
> +	int			rescan_disable;	/* disable card detection */
>   	int			nesting_cnt;	/* "enable" nesting count */
>   	int			en_dis_recurs;	/* detect recursion */
>   	unsigned int		disable_delay;	/* disable delay in msecs */
> @@ -249,6 +251,7 @@ int mmc_card_can_sleep(struct mmc_host *host);
>   int mmc_host_enable(struct mmc_host *host);
>   int mmc_host_disable(struct mmc_host *host);
>   int mmc_host_lazy_disable(struct mmc_host *host);
> +int mmc_pm_notify(struct notifier_block *notify_block, unsigned long, void *);
>
>   static inline void mmc_set_disable_delay(struct mmc_host *host,
>   					 unsigned int disable_delay)
>

Acked-by: Brad Figg <brad.figg@canonical.com>
Brad Figg Sept. 14, 2010, 4:37 p.m. UTC | #3
On 09/13/2010 06:26 AM, Lee Jones wrote:
> The following changes since commit 5470af5fd104192214b496a2736fd26200f4650d:
>    Steve Conklin (1):
>          UBUNTU: [Config] updateconfigs
>
> are available in the git repository at:
>
>    git://kernel.ubuntu.com/lag/ubuntu-lucid.git lp477106
>
> Lee Jones (1):
>        mmc: fix all hangs related to mmc/sd card insert/removal during suspend/resume
>
>   drivers/mmc/core/core.c  |   81 +++++++++++++++++++++++++++++++--------------
>   drivers/mmc/core/host.c  |    5 +++
>   include/linux/mmc/host.h |    3 ++
>   3 files changed, 64 insertions(+), 25 deletions(-)
>
> ----------------------------------------------------------------------
>
> mmc: fix all hangs related to mmc/sd card insert/removal during suspend/resume
>
> Based on a patch-set originally written by Maxim Levitsky.
>
> If you don't use CONFIG_MMC_UNSAFE_RESUME, as soon as you attempt to
> suspend, the card will be removed, therefore this patch doesn't change the
> behavior of this option.
>
> However the removal will be done by pm notifier, which runs while
> userspace is still not frozen and thus can freely use del_gendisk, without
> the risk of deadlock which would happen otherwise.
>
> Card detect workqueue is now disabled while userspace is frozen, Therefore
> if you do use CONFIG_MMC_UNSAFE_RESUME, and remove the card during
> suspend, the removal will be detected as soon as userspace is unfrozen,
> again at the moment it is safe to call del_gendisk.
>
> Tested with and without CONFIG_MMC_UNSAFE_RESUME with suspend and hibernate.
>
>      [akpm@linux-foundation.org: clean up function prototype]
>      [akpm@linux-foundation.org: fix CONFIG_PM-n linkage, small cleanups]
>      [akpm@linux-foundation.org: coding-style fixes]
>      Signed-off-by: Maxim Levitsky<maximlevitsky@gmail.com>
>      Cc: David Brownell<david-b@pacbell.net>
>      Cc: Alan Stern<stern@rowland.harvard.edu>
>      Cc:<linux-mmc@vger.kernel.org>
>      Signed-off-by: Andrew Morton<akpm@linux-foundation.org>
>      Signed-off-by: Linus Torvalds<torvalds@linux-foundation.org>
>
>      [Backported to Ubuntu-2.6.32-25.43 by Lee Jones]
>      Signed-off-by: Lee Jones<lee.jones@canonical.com>
>
>      BugLink: http://bugs.launchpad.net/bugs/477106
>
> ---
>
> diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
> index 30acd52..46f6157 100644
> --- a/drivers/mmc/core/core.c
> +++ b/drivers/mmc/core/core.c
> @@ -1057,6 +1057,17 @@ void mmc_rescan(struct work_struct *work)
>   		container_of(work, struct mmc_host, detect.work);
>   	u32 ocr;
>   	int err;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&host->lock, flags);
> +
> +	if (host->rescan_disable) {
> +		spin_unlock_irqrestore(&host->lock, flags);
> +		return;
> +	}
> +
> +	spin_unlock_irqrestore(&host->lock, flags);
> +
>
>   	mmc_bus_get(host);
>
> @@ -1263,18 +1274,6 @@ int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
>   	if (host->bus_ops&&  !host->bus_dead) {
>   		if (host->bus_ops->suspend)
>   			err = host->bus_ops->suspend(host);
> -		if (err == -ENOSYS || !host->bus_ops->resume) {
> -			/*
> -			 * We simply "remove" the card in this case.
> -			 * It will be redetected on resume.
> -			 */
> -			if (host->bus_ops->remove)
> -				host->bus_ops->remove(host);
> -			mmc_claim_host(host);
> -			mmc_detach_bus(host);
> -			mmc_release_host(host);
> -			err = 0;
> -		}
>   	}
>   	mmc_bus_put(host);
>
> @@ -1304,28 +1303,60 @@ int mmc_resume_host(struct mmc_host *host)
>   			printk(KERN_WARNING "%s: error %d during resume "
>   					    "(card was removed?)\n",
>   					    mmc_hostname(host), err);
> -			if (host->bus_ops->remove)
> -				host->bus_ops->remove(host);
> -			mmc_claim_host(host);
> -			mmc_detach_bus(host);
> -			mmc_release_host(host);
> -			/* no need to bother upper layers */
>   			err = 0;
>   		}
>   	}
>   	mmc_bus_put(host);
>
> -	/*
> -	 * We add a slight delay here so that resume can progress
> -	 * in parallel.
> -	 */
> -	mmc_detect_change(host, 1);
> -
>   	return err;
>   }
> -
>   EXPORT_SYMBOL(mmc_resume_host);
>
> +/* Do the card removal on suspend if card is assumed removeable
> + * Do that in pm notifier while userspace isn't yet frozen, so we will be able
> +   to sync the card.
> +*/
> +int mmc_pm_notify(struct notifier_block *notify_block,
> +					unsigned long mode, void *unused)
> +{
> +	struct mmc_host *host = container_of(
> +		notify_block, struct mmc_host, pm_notify);
> +	unsigned long flags;
> +
> +
> +	switch (mode) {
> +	case PM_HIBERNATION_PREPARE:
> +	case PM_SUSPEND_PREPARE:
> +
> +		spin_lock_irqsave(&host->lock, flags);
> +		host->rescan_disable = 1;
> +		spin_unlock_irqrestore(&host->lock, flags);
> +		cancel_delayed_work_sync(&host->detect);
> +
> +		if (!host->bus_ops || host->bus_ops->suspend)
> +			break;
> +
> +		mmc_claim_host(host);
> +
> +		if (host->bus_ops->remove)
> +			host->bus_ops->remove(host);
> +
> +		mmc_detach_bus(host);
> +		mmc_release_host(host);
> +		break;
> +
> +	case PM_POST_SUSPEND:
> +	case PM_POST_HIBERNATION:
> +
> +		spin_lock_irqsave(&host->lock, flags);
> +		host->rescan_disable = 0;
> +		spin_unlock_irqrestore(&host->lock, flags);
> +		mmc_detect_change(host, 0);
> +
> +	}
> +
> +	return 0;
> +}
>   #endif
>
>   static int __init mmc_init(void)
> diff --git a/drivers/mmc/core/host.c b/drivers/mmc/core/host.c
> index a268d12..0efe631 100644
> --- a/drivers/mmc/core/host.c
> +++ b/drivers/mmc/core/host.c
> @@ -16,6 +16,8 @@
>   #include<linux/idr.h>
>   #include<linux/pagemap.h>
>   #include<linux/leds.h>
> +#include<linux/slab.h>
> +#include<linux/suspend.h>
>
>   #include<linux/mmc/host.h>
>
> @@ -84,6 +86,7 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
>   	init_waitqueue_head(&host->wq);
>   	INIT_DELAYED_WORK(&host->detect, mmc_rescan);
>   	INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);
> +	host->pm_notify.notifier_call = mmc_pm_notify;
>
>   	/*
>   	 * By default, hosts do not support SGIO or large requests.
> @@ -132,6 +135,7 @@ int mmc_add_host(struct mmc_host *host)
>   #endif
>
>   	mmc_start_host(host);
> +	register_pm_notifier(&host->pm_notify);
>
>   	return 0;
>   }
> @@ -148,6 +152,7 @@ EXPORT_SYMBOL(mmc_add_host);
>    */
>   void mmc_remove_host(struct mmc_host *host)
>   {
> +	unregister_pm_notifier(&host->pm_notify);
>   	mmc_stop_host(host);
>
>   #ifdef CONFIG_DEBUG_FS
> diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h
> index eaf3636..475ca8a 100644
> --- a/include/linux/mmc/host.h
> +++ b/include/linux/mmc/host.h
> @@ -120,6 +120,7 @@ struct mmc_host {
>   	unsigned int		f_min;
>   	unsigned int		f_max;
>   	u32			ocr_avail;
> +	struct notifier_block	pm_notify;
>
>   #define MMC_VDD_165_195		0x00000080	/* VDD voltage 1.65 - 1.95 */
>   #define MMC_VDD_20_21		0x00000100	/* VDD voltage 2.0 ~ 2.1 */
> @@ -177,6 +178,7 @@ struct mmc_host {
>
>   	/* Only used with MMC_CAP_DISABLE */
>   	int			enabled;	/* host is enabled */
> +	int			rescan_disable;	/* disable card detection */
>   	int			nesting_cnt;	/* "enable" nesting count */
>   	int			en_dis_recurs;	/* detect recursion */
>   	unsigned int		disable_delay;	/* disable delay in msecs */
> @@ -249,6 +251,7 @@ int mmc_card_can_sleep(struct mmc_host *host);
>   int mmc_host_enable(struct mmc_host *host);
>   int mmc_host_disable(struct mmc_host *host);
>   int mmc_host_lazy_disable(struct mmc_host *host);
> +int mmc_pm_notify(struct notifier_block *notify_block, unsigned long, void *);
>
>   static inline void mmc_set_disable_delay(struct mmc_host *host,
>   					 unsigned int disable_delay)
>
diff mbox

Patch

diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index 30acd52..46f6157 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -1057,6 +1057,17 @@  void mmc_rescan(struct work_struct *work)
 		container_of(work, struct mmc_host, detect.work);
 	u32 ocr;
 	int err;
+	unsigned long flags;
+
+	spin_lock_irqsave(&host->lock, flags);
+
+	if (host->rescan_disable) {
+		spin_unlock_irqrestore(&host->lock, flags);
+		return;
+	}
+
+	spin_unlock_irqrestore(&host->lock, flags);
+
 
 	mmc_bus_get(host);
 
@@ -1263,18 +1274,6 @@  int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
 	if (host->bus_ops && !host->bus_dead) {
 		if (host->bus_ops->suspend)
 			err = host->bus_ops->suspend(host);
-		if (err == -ENOSYS || !host->bus_ops->resume) {
-			/*
-			 * We simply "remove" the card in this case.
-			 * It will be redetected on resume.
-			 */
-			if (host->bus_ops->remove)
-				host->bus_ops->remove(host);
-			mmc_claim_host(host);
-			mmc_detach_bus(host);
-			mmc_release_host(host);
-			err = 0;
-		}
 	}
 	mmc_bus_put(host);
 
@@ -1304,28 +1303,60 @@  int mmc_resume_host(struct mmc_host *host)
 			printk(KERN_WARNING "%s: error %d during resume "
 					    "(card was removed?)\n",
 					    mmc_hostname(host), err);
-			if (host->bus_ops->remove)
-				host->bus_ops->remove(host);
-			mmc_claim_host(host);
-			mmc_detach_bus(host);
-			mmc_release_host(host);
-			/* no need to bother upper layers */
 			err = 0;
 		}
 	}
 	mmc_bus_put(host);
 
-	/*
-	 * We add a slight delay here so that resume can progress
-	 * in parallel.
-	 */
-	mmc_detect_change(host, 1);
-
 	return err;
 }
-
 EXPORT_SYMBOL(mmc_resume_host);
 
+/* Do the card removal on suspend if card is assumed removeable
+ * Do that in pm notifier while userspace isn't yet frozen, so we will be able
+   to sync the card.
+*/
+int mmc_pm_notify(struct notifier_block *notify_block,
+					unsigned long mode, void *unused)
+{
+	struct mmc_host *host = container_of(
+		notify_block, struct mmc_host, pm_notify);
+	unsigned long flags;
+
+
+	switch (mode) {
+	case PM_HIBERNATION_PREPARE:
+	case PM_SUSPEND_PREPARE:
+
+		spin_lock_irqsave(&host->lock, flags);
+		host->rescan_disable = 1;
+		spin_unlock_irqrestore(&host->lock, flags);
+		cancel_delayed_work_sync(&host->detect);
+
+		if (!host->bus_ops || host->bus_ops->suspend)
+			break;
+
+		mmc_claim_host(host);
+
+		if (host->bus_ops->remove)
+			host->bus_ops->remove(host);
+
+		mmc_detach_bus(host);
+		mmc_release_host(host);
+		break;
+
+	case PM_POST_SUSPEND:
+	case PM_POST_HIBERNATION:
+
+		spin_lock_irqsave(&host->lock, flags);
+		host->rescan_disable = 0;
+		spin_unlock_irqrestore(&host->lock, flags);
+		mmc_detect_change(host, 0);
+
+	}
+
+	return 0;
+}
 #endif
 
 static int __init mmc_init(void)
diff --git a/drivers/mmc/core/host.c b/drivers/mmc/core/host.c
index a268d12..0efe631 100644
--- a/drivers/mmc/core/host.c
+++ b/drivers/mmc/core/host.c
@@ -16,6 +16,8 @@ 
 #include <linux/idr.h>
 #include <linux/pagemap.h>
 #include <linux/leds.h>
+#include <linux/slab.h>
+#include <linux/suspend.h>
 
 #include <linux/mmc/host.h>
 
@@ -84,6 +86,7 @@  struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
 	init_waitqueue_head(&host->wq);
 	INIT_DELAYED_WORK(&host->detect, mmc_rescan);
 	INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);
+	host->pm_notify.notifier_call = mmc_pm_notify;
 
 	/*
 	 * By default, hosts do not support SGIO or large requests.
@@ -132,6 +135,7 @@  int mmc_add_host(struct mmc_host *host)
 #endif
 
 	mmc_start_host(host);
+	register_pm_notifier(&host->pm_notify);
 
 	return 0;
 }
@@ -148,6 +152,7 @@  EXPORT_SYMBOL(mmc_add_host);
  */
 void mmc_remove_host(struct mmc_host *host)
 {
+	unregister_pm_notifier(&host->pm_notify);
 	mmc_stop_host(host);
 
 #ifdef CONFIG_DEBUG_FS
diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h
index eaf3636..475ca8a 100644
--- a/include/linux/mmc/host.h
+++ b/include/linux/mmc/host.h
@@ -120,6 +120,7 @@  struct mmc_host {
 	unsigned int		f_min;
 	unsigned int		f_max;
 	u32			ocr_avail;
+	struct notifier_block	pm_notify;
 
 #define MMC_VDD_165_195		0x00000080	/* VDD voltage 1.65 - 1.95 */
 #define MMC_VDD_20_21		0x00000100	/* VDD voltage 2.0 ~ 2.1 */
@@ -177,6 +178,7 @@  struct mmc_host {
 
 	/* Only used with MMC_CAP_DISABLE */
 	int			enabled;	/* host is enabled */
+	int			rescan_disable;	/* disable card detection */
 	int			nesting_cnt;	/* "enable" nesting count */
 	int			en_dis_recurs;	/* detect recursion */
 	unsigned int		disable_delay;	/* disable delay in msecs */
@@ -249,6 +251,7 @@  int mmc_card_can_sleep(struct mmc_host *host);
 int mmc_host_enable(struct mmc_host *host);
 int mmc_host_disable(struct mmc_host *host);
 int mmc_host_lazy_disable(struct mmc_host *host);
+int mmc_pm_notify(struct notifier_block *notify_block, unsigned long, void *);
 
 static inline void mmc_set_disable_delay(struct mmc_host *host,
 					 unsigned int disable_delay)