Commit 543f7810 authored by Alan Stern's avatar Alan Stern Committed by Greg Kroah-Hartman

usb-storage: implement "soft" unbinding

This patch (as1092) implements "soft" unbinding for usb-storage.  When
the disconnect routine is called, all commands and reset delays are
allowed to complete normally until after scsi_remove_host() returns.
This means that the commands needed for an orderly shutdown will be
sent through to the device.

Unlike before, the driver will now execute every command that it
accepts.  Hence there's no need for special code to catch unexecuted
commands and fail them.

The new sequence of events when disconnect runs goes as follows:

	If the device is truly unplugged, set the DISCONNECTING
	flag so we won't try to access it any more.

	If the SCSI-scanning thread hasn't started up yet, prevent
	it from doing anything by setting the new DONT_SCAN flag.
	Then wake it up and wait for it to terminate.

	Remove the SCSI host.  This unbinds the upper-level drivers,
	doing an orderly shutdown.  Commands sent to quiesce the
	device will be transmitted normally, unless the device is
	unplugged.

	Set the DISCONNECTING flag so that we won't accept any new
	commands that might get submitted (there aren't supposed to be
	any) and we won't try to access the device for resets.

	Tell the control thread to exit by waking it up with no
	pending command, and wait for it to terminate.

	Go on to do all the other normal stuff: releasing resources,
	freeing memory, and so on.
Signed-off-by: default avatarAlan Stern <stern@rowland.harvard.edu>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 9da82bd4
...@@ -127,8 +127,8 @@ static int usb_stor_msg_common(struct us_data *us, int timeout) ...@@ -127,8 +127,8 @@ static int usb_stor_msg_common(struct us_data *us, int timeout)
long timeleft; long timeleft;
int status; int status;
/* don't submit URBs during abort/disconnect processing */ /* don't submit URBs during abort processing */
if (us->dflags & ABORTING_OR_DISCONNECTING) if (test_bit(US_FLIDX_ABORTING, &us->dflags))
return -EIO; return -EIO;
/* set up data structures for the wakeup system */ /* set up data structures for the wakeup system */
...@@ -161,8 +161,8 @@ static int usb_stor_msg_common(struct us_data *us, int timeout) ...@@ -161,8 +161,8 @@ static int usb_stor_msg_common(struct us_data *us, int timeout)
* to cancel it */ * to cancel it */
set_bit(US_FLIDX_URB_ACTIVE, &us->dflags); set_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
/* did an abort/disconnect occur during the submission? */ /* did an abort occur during the submission? */
if (us->dflags & ABORTING_OR_DISCONNECTING) { if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
/* cancel the URB, if it hasn't been cancelled already */ /* cancel the URB, if it hasn't been cancelled already */
if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) { if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
...@@ -419,8 +419,8 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe, ...@@ -419,8 +419,8 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
{ {
int result; int result;
/* don't submit s-g requests during abort/disconnect processing */ /* don't submit s-g requests during abort processing */
if (us->dflags & ABORTING_OR_DISCONNECTING) if (test_bit(US_FLIDX_ABORTING, &us->dflags))
return USB_STOR_XFER_ERROR; return USB_STOR_XFER_ERROR;
/* initialize the scatter-gather request block */ /* initialize the scatter-gather request block */
...@@ -437,8 +437,8 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe, ...@@ -437,8 +437,8 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
* okay to cancel it */ * okay to cancel it */
set_bit(US_FLIDX_SG_ACTIVE, &us->dflags); set_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
/* did an abort/disconnect occur during the submission? */ /* did an abort occur during the submission? */
if (us->dflags & ABORTING_OR_DISCONNECTING) { if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
/* cancel the request, if it hasn't been cancelled already */ /* cancel the request, if it hasn't been cancelled already */
if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) { if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
......
...@@ -320,16 +320,17 @@ static int usb_stor_control_thread(void * __us) ...@@ -320,16 +320,17 @@ static int usb_stor_control_thread(void * __us)
/* lock the device pointers */ /* lock the device pointers */
mutex_lock(&(us->dev_mutex)); mutex_lock(&(us->dev_mutex));
/* if the device has disconnected, we are free to exit */ /* lock access to the state */
if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) { scsi_lock(host);
US_DEBUGP("-- exiting\n");
/* When we are called with no command pending, we're done */
if (us->srb == NULL) {
scsi_unlock(host);
mutex_unlock(&us->dev_mutex); mutex_unlock(&us->dev_mutex);
US_DEBUGP("-- exiting\n");
break; break;
} }
/* lock access to the state */
scsi_lock(host);
/* has the command timed out *already* ? */ /* has the command timed out *already* ? */
if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) { if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
us->srb->result = DID_ABORT << 16; us->srb->result = DID_ABORT << 16;
...@@ -384,12 +385,8 @@ static int usb_stor_control_thread(void * __us) ...@@ -384,12 +385,8 @@ static int usb_stor_control_thread(void * __us)
/* lock access to the state */ /* lock access to the state */
scsi_lock(host); scsi_lock(host);
/* did the command already complete because of a disconnect? */
if (!us->srb)
; /* nothing to do */
/* indicate that the command is done */ /* indicate that the command is done */
else if (us->srb->result != DID_ABORT << 16) { if (us->srb->result != DID_ABORT << 16) {
US_DEBUGP("scsi cmd done, result=0x%x\n", US_DEBUGP("scsi cmd done, result=0x%x\n",
us->srb->result); us->srb->result);
us->srb->scsi_done(us->srb); us->srb->scsi_done(us->srb);
...@@ -820,11 +817,10 @@ static void usb_stor_release_resources(struct us_data *us) ...@@ -820,11 +817,10 @@ static void usb_stor_release_resources(struct us_data *us)
US_DEBUGP("-- %s\n", __func__); US_DEBUGP("-- %s\n", __func__);
/* Tell the control thread to exit. The SCSI host must /* Tell the control thread to exit. The SCSI host must
* already have been removed so it won't try to queue * already have been removed and the DISCONNECTING flag set
* any more commands. * so that we won't accept any more commands.
*/ */
US_DEBUGP("-- sending exit command to thread\n"); US_DEBUGP("-- sending exit command to thread\n");
set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
complete(&us->cmnd_ready); complete(&us->cmnd_ready);
if (us->ctl_thread) if (us->ctl_thread)
kthread_stop(us->ctl_thread); kthread_stop(us->ctl_thread);
...@@ -859,39 +855,36 @@ static void dissociate_dev(struct us_data *us) ...@@ -859,39 +855,36 @@ static void dissociate_dev(struct us_data *us)
usb_set_intfdata(us->pusb_intf, NULL); usb_set_intfdata(us->pusb_intf, NULL);
} }
/* First stage of disconnect processing: stop all commands and remove /* First stage of disconnect processing: stop SCSI scanning,
* the host */ * remove the host, and stop accepting new commands
*/
static void quiesce_and_remove_host(struct us_data *us) static void quiesce_and_remove_host(struct us_data *us)
{ {
struct Scsi_Host *host = us_to_host(us); struct Scsi_Host *host = us_to_host(us);
/* Prevent new USB transfers, stop the current command, and /* If the device is really gone, cut short reset delays */
* interrupt a SCSI-scan or device-reset delay */ if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
scsi_lock(host);
set_bit(US_FLIDX_DISCONNECTING, &us->dflags); set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
scsi_unlock(host);
usb_stor_stop_transport(us);
wake_up(&us->delay_wait);
/* queuecommand won't accept any new commands and the control /* Prevent SCSI-scanning (if it hasn't started yet)
* thread won't execute a previously-queued command. If there * and wait for the SCSI-scanning thread to stop.
* is such a command pending, complete it with an error. */ */
mutex_lock(&us->dev_mutex); set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
if (us->srb) { wake_up(&us->delay_wait);
us->srb->result = DID_NO_CONNECT << 16; wait_for_completion(&us->scanning_done);
scsi_lock(host);
us->srb->scsi_done(us->srb);
us->srb = NULL;
complete(&us->notify); /* in case of an abort */
scsi_unlock(host);
}
mutex_unlock(&us->dev_mutex);
/* Now we own no commands so it's safe to remove the SCSI host */ /* Removing the host will perform an orderly shutdown: caches
* synchronized, disks spun down, etc.
*/
scsi_remove_host(host); scsi_remove_host(host);
/* Wait for the SCSI-scanning thread to stop */ /* Prevent any new commands from being accepted and cut short
wait_for_completion(&us->scanning_done); * reset delays.
*/
scsi_lock(host);
set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
scsi_unlock(host);
wake_up(&us->delay_wait);
} }
/* Second stage of disconnect processing: deallocate all resources */ /* Second stage of disconnect processing: deallocate all resources */
...@@ -919,12 +912,12 @@ static int usb_stor_scan_thread(void * __us) ...@@ -919,12 +912,12 @@ static int usb_stor_scan_thread(void * __us)
printk(KERN_DEBUG "usb-storage: waiting for device " printk(KERN_DEBUG "usb-storage: waiting for device "
"to settle before scanning\n"); "to settle before scanning\n");
wait_event_freezable_timeout(us->delay_wait, wait_event_freezable_timeout(us->delay_wait,
test_bit(US_FLIDX_DISCONNECTING, &us->dflags), test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
delay_use * HZ); delay_use * HZ);
} }
/* If the device is still connected, perform the scanning */ /* If the device is still connected, perform the scanning */
if (!test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) { if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
/* For bulk-only devices, determine the max LUN value */ /* For bulk-only devices, determine the max LUN value */
if (us->protocol == US_PR_BULK && if (us->protocol == US_PR_BULK &&
...@@ -1023,6 +1016,7 @@ static int storage_probe(struct usb_interface *intf, ...@@ -1023,6 +1016,7 @@ static int storage_probe(struct usb_interface *intf,
if (IS_ERR(th)) { if (IS_ERR(th)) {
printk(KERN_WARNING USB_STORAGE printk(KERN_WARNING USB_STORAGE
"Unable to start the device-scanning thread\n"); "Unable to start the device-scanning thread\n");
complete(&us->scanning_done);
quiesce_and_remove_host(us); quiesce_and_remove_host(us);
result = PTR_ERR(th); result = PTR_ERR(th);
goto BadDevice; goto BadDevice;
...@@ -1065,6 +1059,7 @@ static struct usb_driver usb_storage_driver = { ...@@ -1065,6 +1059,7 @@ static struct usb_driver usb_storage_driver = {
.pre_reset = storage_pre_reset, .pre_reset = storage_pre_reset,
.post_reset = storage_post_reset, .post_reset = storage_post_reset,
.id_table = storage_usb_ids, .id_table = storage_usb_ids,
.soft_unbind = 1,
}; };
static int __init usb_stor_init(void) static int __init usb_stor_init(void)
......
...@@ -72,11 +72,9 @@ struct us_unusual_dev { ...@@ -72,11 +72,9 @@ struct us_unusual_dev {
#define US_FLIDX_SG_ACTIVE 1 /* current_sg is in use */ #define US_FLIDX_SG_ACTIVE 1 /* current_sg is in use */
#define US_FLIDX_ABORTING 2 /* abort is in progress */ #define US_FLIDX_ABORTING 2 /* abort is in progress */
#define US_FLIDX_DISCONNECTING 3 /* disconnect in progress */ #define US_FLIDX_DISCONNECTING 3 /* disconnect in progress */
#define ABORTING_OR_DISCONNECTING ((1UL << US_FLIDX_ABORTING) | \
(1UL << US_FLIDX_DISCONNECTING))
#define US_FLIDX_RESETTING 4 /* device reset in progress */ #define US_FLIDX_RESETTING 4 /* device reset in progress */
#define US_FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */ #define US_FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */
#define US_FLIDX_DONT_SCAN 6 /* don't scan (disconnect) */
#define USB_STOR_STRING_LEN 32 #define USB_STOR_STRING_LEN 32
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment