mbox series

[v4,0/3] fix runtime PM for SD card readers

Message ID 20210628133412.1172068-1-martin.kepplinger@puri.sm
Headers show
Series fix runtime PM for SD card readers | expand

Message

Martin Kepplinger June 28, 2021, 1:34 p.m. UTC
hi,

(According to Alan Stern, "as far as I know, all") SD card readers send
MEDIA_CHANGED unit attention notification on (runtime) resume. We cannot
use runtime PM with these devices as I/O always fails in that case.

This fixes runtime PM for SD cardreaders. I'd appreciate any feedback.

To enable runtime PM for an SD cardreader number 0:0:0:0, do:
    
echo 0 > /sys/module/block/parameters/events_dfl_poll_msecs
echo 1000 > /sys/bus/scsi/devices/0:0:0:0/power/autosuspend_delay_ms
echo auto > /sys/bus/scsi/devices/0:0:0:0/power/control

thank you,
                           martin

revision history
----------------
v4: (thank you Bart and Alan)
* send SENSE REQUEST in sd instead of adding a global scsi error flag.

v3: (thank you Bart)
 * create a new BLIST entry to mark affected devices instead of the
   sysfs module parameter for sd only. still, only sd implements handling
   the flag for now.
 * cc linux-pm list
https://lore.kernel.org/linux-scsi/20210328102531.1114535-1-martin.kepplinger@puri.sm/

v2:
https://lore.kernel.org/linux-scsi/20210112093329.3639-1-martin.kepplinger@puri.sm/
 * move module parameter to sd
 * add Documentation
v1:
https://lore.kernel.org/linux-scsi/20210111152029.28426-1-martin.kepplinger@puri.sm/T/
For the full background, the discussion started in June 2020 here:
https://lore.kernel.org/linux-scsi/20200623111018.31954-1-martin.kepplinger@puri.sm/


Martin Kepplinger (3):
  scsi: devinfo: add new flag BLIST_MEDIA_CHANGE
  scsi: sd: send REQUEST SENSE for BLIST_MEDIA_CHANGE devices in
    runtime_resume()
  scsi: devinfo: add BLIST_MEDIA_CHANGE for Ultra HS-SD/MMC usb
    cardreaders

 drivers/scsi/scsi_devinfo.c |  1 +
 drivers/scsi/sd.c           | 37 ++++++++++++++++++++++++++++++++++++-
 include/scsi/scsi_devinfo.h |  6 +++---
 3 files changed, 40 insertions(+), 4 deletions(-)

Comments

Bart Van Assche June 30, 2021, 4:18 a.m. UTC | #1
On 6/28/21 6:34 AM, Martin Kepplinger wrote:
> +static int sd_resume_runtime(struct device *dev)

> +{

> +	struct scsi_disk *sdkp = dev_get_drvdata(dev);

> +	struct scsi_device *sdp;

> +	int timeout, retries, res;

> +	struct scsi_sense_hdr my_sshdr;


Since the sense data is ignored, consider removing the "my_sshdr"
declaration and passing NULL as sense pointer to scsi_execute().

> +	if (!sdkp)	/* E.g.: runtime resume at the start of sd_probe() */

> +		return 0;


Are you sure that this code is necessary? There is an
scsi_autopm_get_device(sdp) call at the start of sd_probe() and
scsi_autopm_put_device(sdp) call at the end of sd_probe(). In other
words, no runtime suspend will happen between the
device_initialize(&sdkp->dev) call in sd_probe() and the
dev_set_drvdata(dev, sdkp) call in the same function.

> +	if (sdp->sdev_bflags & BLIST_MEDIA_CHANGE) {

> +		for (retries = 3; retries > 0; --retries) {

> +			unsigned char cmd[10] = { 0 };

> +

> +			cmd[0] = REQUEST_SENSE;


Please define the CDB as follows:

	static const u8 cmd[10] = { REQUEST_SENSE };

> +			/*

> +			 * Leave the rest of the command zero to indicate

> +			 * flush everything.

> +			 */


Shouldn't this comment appear above the CDB definition? Also, what does
"flush everything" mean? According to SPC sense data is discarded from
the device while processing REQUEST SENSE, no matter what the value of
the ALLOCATION LENGTH parameter in that command is. From SPC-6: "the
REQUEST SENSE command with any allocation length clears the sense data."

> +			res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL,

> +					   &my_sshdr, timeout,

> +					   sdkp->max_retries, 0, RQF_PM, NULL);


Only one level of retries please. Can sdkp->max_retries be changed into 1?

Thanks,

Bart.
Bart Van Assche June 30, 2021, 4:20 a.m. UTC | #2
On 6/28/21 6:34 AM, Martin Kepplinger wrote:
> These cardreader device issues a MEDIA CHANGE unit attention not only
> when actually a medium changed but also simply when resuming from suspend.
> 
> Setting the BLIST_MEDIA_CHANGE flag enables using runtime pm for SD cardreaders.

"device issues" -> "devices issue"? Anyway:

Reviewed-by: Bart Van Assche <bvanassche@acm.org>