e1000: Fixes for packet split related issues
[safe/jmp/linux-2.6] / drivers / scsi / scsi_error.c
index 4f312da..ad53421 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/string.h>
 #include <linux/slab.h>
 #include <linux/kernel.h>
+#include <linux/kthread.h>
 #include <linux/interrupt.h>
 #include <linux/blkdev.h>
 #include <linux/delay.h>
@@ -49,7 +50,7 @@
 void scsi_eh_wakeup(struct Scsi_Host *shost)
 {
        if (shost->host_busy == shost->host_failed) {
-               up(shost->eh_wait);
+               wake_up_process(shost->ehandler);
                SCSI_LOG_ERROR_RECOVERY(5,
                                printk("Waking error handler thread\n"));
        }
@@ -67,19 +68,24 @@ int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
 {
        struct Scsi_Host *shost = scmd->device->host;
        unsigned long flags;
+       int ret = 0;
 
-       if (shost->eh_wait == NULL)
+       if (!shost->ehandler)
                return 0;
 
        spin_lock_irqsave(shost->host_lock, flags);
+       if (scsi_host_set_state(shost, SHOST_RECOVERY))
+               if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
+                       goto out_unlock;
 
-       scsi_eh_eflags_set(scmd, eh_flag);
+       ret = 1;
+       scmd->eh_eflags |= eh_flag;
        list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
-       set_bit(SHOST_RECOVERY, &shost->shost_state);
        shost->host_failed++;
        scsi_eh_wakeup(shost);
+ out_unlock:
        spin_unlock_irqrestore(shost->host_lock, flags);
-       return 1;
+       return ret;
 }
 
 /**
@@ -115,7 +121,6 @@ void scsi_add_timer(struct scsi_cmnd *scmd, int timeout,
 
        add_timer(&scmd->eh_timeout);
 }
-EXPORT_SYMBOL(scsi_add_timer);
 
 /**
  * scsi_delete_timer - Delete/cancel timer for a given function.
@@ -143,7 +148,6 @@ int scsi_delete_timer(struct scsi_cmnd *scmd)
 
        return rtn;
 }
-EXPORT_SYMBOL(scsi_delete_timer);
 
 /**
  * scsi_times_out - Timeout function for normal scsi commands.
@@ -177,8 +181,8 @@ void scsi_times_out(struct scsi_cmnd *scmd)
                }
 
        if (unlikely(!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))) {
-               panic("Error handler thread not present at %p %p %s %d",
-                     scmd, scmd->device->host, __FILE__, __LINE__);
+               scmd->result |= DID_TIME_OUT << 16;
+               __scsi_done(scmd);
        }
 }
 
@@ -197,7 +201,7 @@ int scsi_block_when_processing_errors(struct scsi_device *sdev)
 {
        int online;
 
-       wait_event(sdev->host->host_wait, (!test_bit(SHOST_RECOVERY, &sdev->host->shost_state)));
+       wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
 
        online = scsi_device_online(sdev);
 
@@ -228,8 +232,7 @@ static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
                list_for_each_entry(scmd, work_q, eh_entry) {
                        if (scmd->device == sdev) {
                                ++total_failures;
-                               if (scsi_eh_eflags_chk(scmd,
-                                                      SCSI_EH_CANCEL_CMD))
+                               if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
                                        ++cmd_cancel;
                                else 
                                        ++cmd_failed;
@@ -425,7 +428,7 @@ static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
  **/
 static void scsi_eh_times_out(struct scsi_cmnd *scmd)
 {
-       scsi_eh_eflags_set(scmd, SCSI_EH_REC_TIMEOUT);
+       scmd->eh_eflags |= SCSI_EH_REC_TIMEOUT;
        SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd:%p\n", __FUNCTION__,
                                          scmd));
 
@@ -504,8 +507,8 @@ static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, int timeout)
         * see if timeout.  if so, tell the host to forget about it.
         * in other words, we don't want a callback any more.
         */
-       if (scsi_eh_eflags_chk(scmd, SCSI_EH_REC_TIMEOUT)) {
-               scsi_eh_eflags_clr(scmd,  SCSI_EH_REC_TIMEOUT);
+       if (scmd->eh_eflags & SCSI_EH_REC_TIMEOUT) {
+               scmd->eh_eflags &= ~SCSI_EH_REC_TIMEOUT;
 
                /*
                 * as far as the low level driver is
@@ -630,7 +633,7 @@ static void scsi_eh_finish_cmd(struct scsi_cmnd *scmd,
                               struct list_head *done_q)
 {
        scmd->device->host->host_failed--;
-       scsi_eh_eflags_clr_all(scmd);
+       scmd->eh_eflags = 0;
 
        /*
         * set this back so that the upper level can correctly free up
@@ -663,13 +666,11 @@ static void scsi_eh_finish_cmd(struct scsi_cmnd *scmd,
 static int scsi_eh_get_sense(struct list_head *work_q,
                             struct list_head *done_q)
 {
-       struct list_head *lh, *lh_sf;
-       struct scsi_cmnd *scmd;
+       struct scsi_cmnd *scmd, *next;
        int rtn;
 
-       list_for_each_safe(lh, lh_sf, work_q) {
-               scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
-               if (scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD) ||
+       list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
+               if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
                    SCSI_SENSE_VALID(scmd))
                        continue;
 
@@ -778,9 +779,11 @@ retry_tur:
                __FUNCTION__, scmd, rtn));
        if (rtn == SUCCESS)
                return 0;
-       else if (rtn == NEEDS_RETRY)
+       else if (rtn == NEEDS_RETRY) {
                if (retry_cnt--)
                        goto retry_tur;
+               return 0;
+       }
        return 1;
 }
 
@@ -799,20 +802,18 @@ retry_tur:
 static int scsi_eh_abort_cmds(struct list_head *work_q,
                              struct list_head *done_q)
 {
-       struct list_head *lh, *lh_sf;
-       struct scsi_cmnd *scmd;
+       struct scsi_cmnd *scmd, *next;
        int rtn;
 
-       list_for_each_safe(lh, lh_sf, work_q) {
-               scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
-               if (!scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD))
+       list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
+               if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
                        continue;
                SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
                                                  "0x%p\n", current->comm,
                                                  scmd));
                rtn = scsi_try_to_abort_cmd(scmd);
                if (rtn == SUCCESS) {
-                       scsi_eh_eflags_clr(scmd,  SCSI_EH_CANCEL_CMD);
+                       scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
                        if (!scsi_device_online(scmd->device) ||
                            !scsi_eh_tur(scmd)) {
                                scsi_eh_finish_cmd(scmd, done_q);
@@ -919,8 +920,7 @@ static int scsi_eh_stu(struct Scsi_Host *shost,
                              struct list_head *work_q,
                              struct list_head *done_q)
 {
-       struct list_head *lh, *lh_sf;
-       struct scsi_cmnd *scmd, *stu_scmd;
+       struct scsi_cmnd *scmd, *stu_scmd, *next;
        struct scsi_device *sdev;
 
        shost_for_each_device(sdev, shost) {
@@ -941,8 +941,8 @@ static int scsi_eh_stu(struct Scsi_Host *shost,
                if (!scsi_eh_try_stu(stu_scmd)) {
                        if (!scsi_device_online(sdev) ||
                            !scsi_eh_tur(stu_scmd)) {
-                               list_for_each_safe(lh, lh_sf, work_q) {
-                                       scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
+                               list_for_each_entry_safe(scmd, next,
+                                                         work_q, eh_entry) {
                                        if (scmd->device == sdev)
                                                scsi_eh_finish_cmd(scmd, done_q);
                                }
@@ -973,8 +973,7 @@ static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
                                    struct list_head *work_q,
                                    struct list_head *done_q)
 {
-       struct list_head *lh, *lh_sf;
-       struct scsi_cmnd *scmd, *bdr_scmd;
+       struct scsi_cmnd *scmd, *bdr_scmd, *next;
        struct scsi_device *sdev;
        int rtn;
 
@@ -996,11 +995,8 @@ static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
                if (rtn == SUCCESS) {
                        if (!scsi_device_online(sdev) ||
                            !scsi_eh_tur(bdr_scmd)) {
-                               list_for_each_safe(lh, lh_sf,
-                                                  work_q) {
-                                       scmd = list_entry(lh, struct
-                                                         scsi_cmnd,
-                                                         eh_entry);
+                               list_for_each_entry_safe(scmd, next,
+                                                        work_q, eh_entry) {
                                        if (scmd->device == sdev)
                                                scsi_eh_finish_cmd(scmd,
                                                                   done_q);
@@ -1083,9 +1079,7 @@ static int scsi_eh_bus_reset(struct Scsi_Host *shost,
                             struct list_head *work_q,
                             struct list_head *done_q)
 {
-       struct list_head *lh, *lh_sf;
-       struct scsi_cmnd *scmd;
-       struct scsi_cmnd *chan_scmd;
+       struct scsi_cmnd *scmd, *chan_scmd, *next;
        unsigned int channel;
        int rtn;
 
@@ -1116,9 +1110,7 @@ static int scsi_eh_bus_reset(struct Scsi_Host *shost,
                                                  channel));
                rtn = scsi_try_bus_reset(chan_scmd);
                if (rtn == SUCCESS) {
-                       list_for_each_safe(lh, lh_sf, work_q) {
-                               scmd = list_entry(lh, struct scsi_cmnd,
-                                                 eh_entry);
+                       list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
                                if (channel == scmd->device->channel)
                                        if (!scsi_device_online(scmd->device) ||
                                            !scsi_eh_tur(scmd))
@@ -1143,9 +1135,8 @@ static int scsi_eh_bus_reset(struct Scsi_Host *shost,
 static int scsi_eh_host_reset(struct list_head *work_q,
                              struct list_head *done_q)
 {
+       struct scsi_cmnd *scmd, *next;
        int rtn;
-       struct list_head *lh, *lh_sf;
-       struct scsi_cmnd *scmd;
 
        if (!list_empty(work_q)) {
                scmd = list_entry(work_q->next,
@@ -1156,8 +1147,7 @@ static int scsi_eh_host_reset(struct list_head *work_q,
 
                rtn = scsi_try_host_reset(scmd);
                if (rtn == SUCCESS) {
-                       list_for_each_safe(lh, lh_sf, work_q) {
-                               scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
+                       list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
                                if (!scsi_device_online(scmd->device) ||
                                    (!scsi_eh_try_stu(scmd) && !scsi_eh_tur(scmd)) ||
                                    !scsi_eh_tur(scmd))
@@ -1181,11 +1171,9 @@ static int scsi_eh_host_reset(struct list_head *work_q,
 static void scsi_eh_offline_sdevs(struct list_head *work_q,
                                  struct list_head *done_q)
 {
-       struct list_head *lh, *lh_sf;
-       struct scsi_cmnd *scmd;
+       struct scsi_cmnd *scmd, *next;
 
-       list_for_each_safe(lh, lh_sf, work_q) {
-               scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
+       list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
                printk(KERN_INFO "scsi: Device offlined - not"
                                " ready after error recovery: host"
                                " %d channel %d id %d lun %d\n",
@@ -1194,7 +1182,7 @@ static void scsi_eh_offline_sdevs(struct list_head *work_q,
                                scmd->device->id,
                                scmd->device->lun);
                scsi_device_set_state(scmd->device, SDEV_OFFLINE);
-               if (scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD)) {
+               if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
                        /*
                         * FIXME: Handle lost cmds.
                         */
@@ -1457,6 +1445,7 @@ static void scsi_eh_lock_door(struct scsi_device *sdev)
 static void scsi_restart_operations(struct Scsi_Host *shost)
 {
        struct scsi_device *sdev;
+       unsigned long flags;
 
        /*
         * If the door was locked, we need to insert a door lock request
@@ -1476,7 +1465,11 @@ static void scsi_restart_operations(struct Scsi_Host *shost)
        SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
                                          __FUNCTION__));
 
-       clear_bit(SHOST_RECOVERY, &shost->shost_state);
+       spin_lock_irqsave(shost->host_lock, flags);
+       if (scsi_host_set_state(shost, SHOST_RUNNING))
+               if (scsi_host_set_state(shost, SHOST_CANCEL))
+                       BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
+       spin_unlock_irqrestore(shost->host_lock, flags);
 
        wake_up(&shost->host_wait);
 
@@ -1513,12 +1506,10 @@ static void scsi_eh_ready_devs(struct Scsi_Host *shost,
  **/
 static void scsi_eh_flush_done_q(struct list_head *done_q)
 {
-       struct list_head *lh, *lh_sf;
-       struct scsi_cmnd *scmd;
+       struct scsi_cmnd *scmd, *next;
 
-       list_for_each_safe(lh, lh_sf, done_q) {
-               scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
-               list_del_init(lh);
+       list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
+               list_del_init(&scmd->eh_entry);
                if (scsi_device_online(scmd->device) &&
                    !blk_noretry_request(scmd->request) &&
                    (++scmd->retries < scmd->allowed)) {
@@ -1600,50 +1591,31 @@ int scsi_error_handler(void *data)
 {
        struct Scsi_Host *shost = (struct Scsi_Host *) data;
        int rtn;
-       DECLARE_MUTEX_LOCKED(sem);
-
-       /*
-        *    Flush resources
-        */
-
-       daemonize("scsi_eh_%d", shost->host_no);
 
        current->flags |= PF_NOFREEZE;
 
-       shost->eh_wait = &sem;
-       shost->ehandler = current;
-
+       
        /*
-        * Wake up the thread that created us.
+        * Note - we always use TASK_INTERRUPTIBLE even if the module
+        * was loaded as part of the kernel.  The reason is that
+        * UNINTERRUPTIBLE would cause this thread to be counted in
+        * the load average as a running process, and an interruptible
+        * wait doesn't.
         */
-       SCSI_LOG_ERROR_RECOVERY(3, printk("Wake up parent of"
-                                         " scsi_eh_%d\n",shost->host_no));
-
-       complete(shost->eh_notify);
-
-       while (1) {
-               /*
-                * If we get a signal, it means we are supposed to go
-                * away and die.  This typically happens if the user is
-                * trying to unload a module.
-                */
-               SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler"
-                                                 " scsi_eh_%d"
-                                                 " sleeping\n",shost->host_no));
-
-               /*
-                * Note - we always use down_interruptible with the semaphore
-                * even if the module was loaded as part of the kernel.  The
-                * reason is that down() will cause this thread to be counted
-                * in the load average as a running process, and down
-                * interruptible doesn't.  Given that we need to allow this
-                * thread to die if the driver was loaded as a module, using
-                * semaphores isn't unreasonable.
-                */
-               down_interruptible(&sem);
-               if (shost->eh_kill)
-                       break;
+       set_current_state(TASK_INTERRUPTIBLE);
+       while (!kthread_should_stop()) {
+               if (shost->host_failed == 0 ||
+                   shost->host_failed != shost->host_busy) {
+                       SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler"
+                                                         " scsi_eh_%d"
+                                                         " sleeping\n",
+                                                         shost->host_no));
+                       schedule();
+                       set_current_state(TASK_INTERRUPTIBLE);
+                       continue;
+               }
 
+               __set_current_state(TASK_RUNNING);
                SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler"
                                                  " scsi_eh_%d waking"
                                                  " up\n",shost->host_no));
@@ -1670,7 +1642,7 @@ int scsi_error_handler(void *data)
                 * which are still online.
                 */
                scsi_restart_operations(shost);
-
+               set_current_state(TASK_INTERRUPTIBLE);
        }
 
        SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler scsi_eh_%d"
@@ -1679,23 +1651,7 @@ int scsi_error_handler(void *data)
        /*
         * Make sure that nobody tries to wake us up again.
         */
-       shost->eh_wait = NULL;
-
-       /*
-        * Knock this down too.  From this point on, the host is flying
-        * without a pilot.  If this is because the module is being unloaded,
-        * that's fine.  If the user sent a signal to this thing, we are
-        * potentially in real danger.
-        */
-       shost->eh_active = 0;
        shost->ehandler = NULL;
-
-       /*
-        * If anyone is waiting for us to exit (i.e. someone trying to unload
-        * a driver), then wake up that process to let them know we are on
-        * the way out the door.
-        */
-       complete_and_exit(shost->eh_notify, 0);
        return 0;
 }
 
@@ -1866,12 +1822,16 @@ EXPORT_SYMBOL(scsi_reset_provider);
 int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
                          struct scsi_sense_hdr *sshdr)
 {
-       if (!sense_buffer || !sb_len || (sense_buffer[0] & 0x70) != 0x70)
+       if (!sense_buffer || !sb_len)
                return 0;
 
        memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
 
        sshdr->response_code = (sense_buffer[0] & 0x7f);
+
+       if (!scsi_sense_valid(sshdr))
+               return 0;
+
        if (sshdr->response_code >= 0x72) {
                /*
                 * descriptor format