aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorAlan Stern <stern@rowland.harvard.edu>2008-05-08 11:55:59 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2008-07-21 18:15:54 -0400
commit543f7810fba2a62e412efa9473ad08167b691f09 (patch)
tree4e29d5b264b44ed544ea4fa6aec3389c504e9b68 /drivers
parent9da82bd4649334817ef0e752a69eb99051645dad (diff)
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: Alan Stern <stern@rowland.harvard.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/usb/storage/transport.c16
-rw-r--r--drivers/usb/storage/usb.c77
-rw-r--r--drivers/usb/storage/usb.h4
3 files changed, 45 insertions, 52 deletions
diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
index 2f88bb958bad..94138df557b9 100644
--- a/drivers/usb/storage/transport.c
+++ b/drivers/usb/storage/transport.c
@@ -127,8 +127,8 @@ static int usb_stor_msg_common(struct us_data *us, int timeout)
127 long timeleft; 127 long timeleft;
128 int status; 128 int status;
129 129
130 /* don't submit URBs during abort/disconnect processing */ 130 /* don't submit URBs during abort processing */
131 if (us->dflags & ABORTING_OR_DISCONNECTING) 131 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
132 return -EIO; 132 return -EIO;
133 133
134 /* set up data structures for the wakeup system */ 134 /* 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 * to cancel it */ 161 * to cancel it */
162 set_bit(US_FLIDX_URB_ACTIVE, &us->dflags); 162 set_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
163 163
164 /* did an abort/disconnect occur during the submission? */ 164 /* did an abort occur during the submission? */
165 if (us->dflags & ABORTING_OR_DISCONNECTING) { 165 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
166 166
167 /* cancel the URB, if it hasn't been cancelled already */ 167 /* cancel the URB, if it hasn't been cancelled already */
168 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) { 168 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{ 419{
420 int result; 420 int result;
421 421
422 /* don't submit s-g requests during abort/disconnect processing */ 422 /* don't submit s-g requests during abort processing */
423 if (us->dflags & ABORTING_OR_DISCONNECTING) 423 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
424 return USB_STOR_XFER_ERROR; 424 return USB_STOR_XFER_ERROR;
425 425
426 /* initialize the scatter-gather request block */ 426 /* 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 * okay to cancel it */ 437 * okay to cancel it */
438 set_bit(US_FLIDX_SG_ACTIVE, &us->dflags); 438 set_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
439 439
440 /* did an abort/disconnect occur during the submission? */ 440 /* did an abort occur during the submission? */
441 if (us->dflags & ABORTING_OR_DISCONNECTING) { 441 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
442 442
443 /* cancel the request, if it hasn't been cancelled already */ 443 /* cancel the request, if it hasn't been cancelled already */
444 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) { 444 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index 6b14f8d253f1..6bfd99dd57aa 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -320,16 +320,17 @@ static int usb_stor_control_thread(void * __us)
320 /* lock the device pointers */ 320 /* lock the device pointers */
321 mutex_lock(&(us->dev_mutex)); 321 mutex_lock(&(us->dev_mutex));
322 322
323 /* if the device has disconnected, we are free to exit */ 323 /* lock access to the state */
324 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) { 324 scsi_lock(host);
325 US_DEBUGP("-- exiting\n"); 325
326 /* When we are called with no command pending, we're done */
327 if (us->srb == NULL) {
328 scsi_unlock(host);
326 mutex_unlock(&us->dev_mutex); 329 mutex_unlock(&us->dev_mutex);
330 US_DEBUGP("-- exiting\n");
327 break; 331 break;
328 } 332 }
329 333
330 /* lock access to the state */
331 scsi_lock(host);
332
333 /* has the command timed out *already* ? */ 334 /* has the command timed out *already* ? */
334 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) { 335 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
335 us->srb->result = DID_ABORT << 16; 336 us->srb->result = DID_ABORT << 16;
@@ -384,12 +385,8 @@ static int usb_stor_control_thread(void * __us)
384 /* lock access to the state */ 385 /* lock access to the state */
385 scsi_lock(host); 386 scsi_lock(host);
386 387
387 /* did the command already complete because of a disconnect? */
388 if (!us->srb)
389 ; /* nothing to do */
390
391 /* indicate that the command is done */ 388 /* indicate that the command is done */
392 else if (us->srb->result != DID_ABORT << 16) { 389 if (us->srb->result != DID_ABORT << 16) {
393 US_DEBUGP("scsi cmd done, result=0x%x\n", 390 US_DEBUGP("scsi cmd done, result=0x%x\n",
394 us->srb->result); 391 us->srb->result);
395 us->srb->scsi_done(us->srb); 392 us->srb->scsi_done(us->srb);
@@ -820,11 +817,10 @@ static void usb_stor_release_resources(struct us_data *us)
820 US_DEBUGP("-- %s\n", __func__); 817 US_DEBUGP("-- %s\n", __func__);
821 818
822 /* Tell the control thread to exit. The SCSI host must 819 /* Tell the control thread to exit. The SCSI host must
823 * already have been removed so it won't try to queue 820 * already have been removed and the DISCONNECTING flag set
824 * any more commands. 821 * so that we won't accept any more commands.
825 */ 822 */
826 US_DEBUGP("-- sending exit command to thread\n"); 823 US_DEBUGP("-- sending exit command to thread\n");
827 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
828 complete(&us->cmnd_ready); 824 complete(&us->cmnd_ready);
829 if (us->ctl_thread) 825 if (us->ctl_thread)
830 kthread_stop(us->ctl_thread); 826 kthread_stop(us->ctl_thread);
@@ -859,39 +855,36 @@ static void dissociate_dev(struct us_data *us)
859 usb_set_intfdata(us->pusb_intf, NULL); 855 usb_set_intfdata(us->pusb_intf, NULL);
860} 856}
861 857
862/* First stage of disconnect processing: stop all commands and remove 858/* First stage of disconnect processing: stop SCSI scanning,
863 * the host */ 859 * remove the host, and stop accepting new commands
860 */
864static void quiesce_and_remove_host(struct us_data *us) 861static void quiesce_and_remove_host(struct us_data *us)
865{ 862{
866 struct Scsi_Host *host = us_to_host(us); 863 struct Scsi_Host *host = us_to_host(us);
867 864
868 /* Prevent new USB transfers, stop the current command, and 865 /* If the device is really gone, cut short reset delays */
869 * interrupt a SCSI-scan or device-reset delay */ 866 if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
870 scsi_lock(host); 867 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
871 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
872 scsi_unlock(host);
873 usb_stor_stop_transport(us);
874 wake_up(&us->delay_wait);
875 868
876 /* queuecommand won't accept any new commands and the control 869 /* Prevent SCSI-scanning (if it hasn't started yet)
877 * thread won't execute a previously-queued command. If there 870 * and wait for the SCSI-scanning thread to stop.
878 * is such a command pending, complete it with an error. */ 871 */
879 mutex_lock(&us->dev_mutex); 872 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
880 if (us->srb) { 873 wake_up(&us->delay_wait);
881 us->srb->result = DID_NO_CONNECT << 16; 874 wait_for_completion(&us->scanning_done);
882 scsi_lock(host);
883 us->srb->scsi_done(us->srb);
884 us->srb = NULL;
885 complete(&us->notify); /* in case of an abort */
886 scsi_unlock(host);
887 }
888 mutex_unlock(&us->dev_mutex);
889 875
890 /* Now we own no commands so it's safe to remove the SCSI host */ 876 /* Removing the host will perform an orderly shutdown: caches
877 * synchronized, disks spun down, etc.
878 */
891 scsi_remove_host(host); 879 scsi_remove_host(host);
892 880
893 /* Wait for the SCSI-scanning thread to stop */ 881 /* Prevent any new commands from being accepted and cut short
894 wait_for_completion(&us->scanning_done); 882 * reset delays.
883 */
884 scsi_lock(host);
885 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
886 scsi_unlock(host);
887 wake_up(&us->delay_wait);
895} 888}
896 889
897/* Second stage of disconnect processing: deallocate all resources */ 890/* Second stage of disconnect processing: deallocate all resources */
@@ -919,12 +912,12 @@ static int usb_stor_scan_thread(void * __us)
919 printk(KERN_DEBUG "usb-storage: waiting for device " 912 printk(KERN_DEBUG "usb-storage: waiting for device "
920 "to settle before scanning\n"); 913 "to settle before scanning\n");
921 wait_event_freezable_timeout(us->delay_wait, 914 wait_event_freezable_timeout(us->delay_wait,
922 test_bit(US_FLIDX_DISCONNECTING, &us->dflags), 915 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
923 delay_use * HZ); 916 delay_use * HZ);
924 } 917 }
925 918
926 /* If the device is still connected, perform the scanning */ 919 /* If the device is still connected, perform the scanning */
927 if (!test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) { 920 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
928 921
929 /* For bulk-only devices, determine the max LUN value */ 922 /* For bulk-only devices, determine the max LUN value */
930 if (us->protocol == US_PR_BULK && 923 if (us->protocol == US_PR_BULK &&
@@ -1023,6 +1016,7 @@ static int storage_probe(struct usb_interface *intf,
1023 if (IS_ERR(th)) { 1016 if (IS_ERR(th)) {
1024 printk(KERN_WARNING USB_STORAGE 1017 printk(KERN_WARNING USB_STORAGE
1025 "Unable to start the device-scanning thread\n"); 1018 "Unable to start the device-scanning thread\n");
1019 complete(&us->scanning_done);
1026 quiesce_and_remove_host(us); 1020 quiesce_and_remove_host(us);
1027 result = PTR_ERR(th); 1021 result = PTR_ERR(th);
1028 goto BadDevice; 1022 goto BadDevice;
@@ -1065,6 +1059,7 @@ static struct usb_driver usb_storage_driver = {
1065 .pre_reset = storage_pre_reset, 1059 .pre_reset = storage_pre_reset,
1066 .post_reset = storage_post_reset, 1060 .post_reset = storage_post_reset,
1067 .id_table = storage_usb_ids, 1061 .id_table = storage_usb_ids,
1062 .soft_unbind = 1,
1068}; 1063};
1069 1064
1070static int __init usb_stor_init(void) 1065static int __init usb_stor_init(void)
diff --git a/drivers/usb/storage/usb.h b/drivers/usb/storage/usb.h
index 8da96da5875d..47906dc620db 100644
--- a/drivers/usb/storage/usb.h
+++ b/drivers/usb/storage/usb.h
@@ -72,11 +72,9 @@ struct us_unusual_dev {
72#define US_FLIDX_SG_ACTIVE 1 /* current_sg is in use */ 72#define US_FLIDX_SG_ACTIVE 1 /* current_sg is in use */
73#define US_FLIDX_ABORTING 2 /* abort is in progress */ 73#define US_FLIDX_ABORTING 2 /* abort is in progress */
74#define US_FLIDX_DISCONNECTING 3 /* disconnect in progress */ 74#define US_FLIDX_DISCONNECTING 3 /* disconnect in progress */
75#define ABORTING_OR_DISCONNECTING ((1UL << US_FLIDX_ABORTING) | \
76 (1UL << US_FLIDX_DISCONNECTING))
77#define US_FLIDX_RESETTING 4 /* device reset in progress */ 75#define US_FLIDX_RESETTING 4 /* device reset in progress */
78#define US_FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */ 76#define US_FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */
79 77#define US_FLIDX_DONT_SCAN 6 /* don't scan (disconnect) */
80 78
81#define USB_STOR_STRING_LEN 32 79#define USB_STOR_STRING_LEN 32
82 80