[SCSI] 53c700: m68k MVME16x NCR53C710 SCSI
[safe/jmp/linux-2.6] / drivers / scsi / aha152x.c
index 67d78ed..85f2394 100644 (file)
  **************************************************************************/
 
 #include <linux/module.h>
-#include <linux/sched.h>
 #include <asm/irq.h>
-#include <asm/io.h>
+#include <linux/io.h>
 #include <linux/blkdev.h>
 #include <asm/system.h>
+#include <linux/completion.h>
 #include <linux/errno.h>
 #include <linux/string.h>
 #include <linux/wait.h>
 #include <linux/isapnp.h>
 #include <linux/spinlock.h>
 #include <linux/workqueue.h>
-#include <asm/semaphore.h>
+#include <linux/list.h>
 #include <scsi/scsicam.h>
 
 #include "scsi.h"
 #include <scsi/scsi_transport_spi.h>
 #include "aha152x.h"
 
+static LIST_HEAD(aha152x_host_list);
+
 
 /* DEFINES */
 
@@ -423,8 +425,6 @@ MODULE_DEVICE_TABLE(isapnp, id_table);
 
 #endif /* !PCMCIA */
 
-static int registered_count=0;
-static struct Scsi_Host *aha152x_host[2];
 static struct scsi_host_template aha152x_driver_template;
 
 /*
@@ -541,6 +541,7 @@ struct aha152x_hostdata {
 #ifdef __ISAPNP__
        struct pnp_dev *pnpdev;
 #endif
+       struct list_head host_list;
 };
 
 
@@ -550,7 +551,12 @@ struct aha152x_hostdata {
  */
 struct aha152x_scdata {
        Scsi_Cmnd *next;        /* next sc in queue */
-       struct semaphore *sem;  /* semaphore to block on */
+       struct completion *done;/* semaphore to block on */
+       unsigned char cmd_len;
+       unsigned char cmnd[MAX_COMMAND_SIZE];
+       unsigned short use_sg;
+       unsigned request_bufflen;
+       void *request_buffer;
 };
 
 
@@ -602,7 +608,7 @@ struct aha152x_scdata {
 
 #define SCDATA(SCpnt)          ((struct aha152x_scdata *) (SCpnt)->host_scribble)
 #define SCNEXT(SCpnt)          SCDATA(SCpnt)->next
-#define SCSEM(SCpnt)           SCDATA(SCpnt)->sem
+#define SCSEM(SCpnt)           SCDATA(SCpnt)->done
 
 #define SG_ADDRESS(buffer)     ((char *) (page_address((buffer)->page)+(buffer)->offset))
 
@@ -666,7 +672,7 @@ static struct {
 };
 
 /* setup & interrupt */
-static irqreturn_t intr(int irq, void *dev_id, struct pt_regs *);
+static irqreturn_t intr(int irq, void *dev_id);
 static void reset_ports(struct Scsi_Host *shpnt);
 static void aha152x_error(struct Scsi_Host *shpnt, char *msg);
 static void done(struct Scsi_Host *shpnt, int error);
@@ -750,25 +756,9 @@ static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, Scsi_Cmnd *SCp)
        return ptr;
 }
 
-static inline struct Scsi_Host *lookup_irq(int irqno)
-{
-       int i;
-
-       for(i=0; i<ARRAY_SIZE(aha152x_host); i++)
-               if(aha152x_host[i] && aha152x_host[i]->irq==irqno)
-                       return aha152x_host[i];
-
-       return NULL;
-}
-
-static irqreturn_t swintr(int irqno, void *dev_id, struct pt_regs *regs)
+static irqreturn_t swintr(int irqno, void *dev_id)
 {
-       struct Scsi_Host *shpnt = lookup_irq(irqno);
-
-       if (!shpnt) {
-               printk(KERN_ERR "aha152x: catched software interrupt %d for unknown controller.\n", irqno);
-               return IRQ_NONE;
-       }
+       struct Scsi_Host *shpnt = dev_id;
 
        HOSTDATA(shpnt)->swint++;
 
@@ -786,10 +776,11 @@ struct Scsi_Host *aha152x_probe_one(struct aha152x_setup *setup)
                return NULL;
        }
 
-       /* need to have host registered before triggering any interrupt */
-       aha152x_host[registered_count] = shpnt;
-
        memset(HOSTDATA(shpnt), 0, sizeof *HOSTDATA(shpnt));
+       INIT_LIST_HEAD(&HOSTDATA(shpnt)->host_list);
+
+       /* need to have host registered before triggering any interrupt */
+       list_add_tail(&HOSTDATA(shpnt)->host_list, &aha152x_host_list);
 
        shpnt->io_port   = setup->io_port;
        shpnt->n_io_port = IO_RANGE;
@@ -855,7 +846,7 @@ struct Scsi_Host *aha152x_probe_one(struct aha152x_setup *setup)
        SETPORT(SIMODE0, 0);
        SETPORT(SIMODE1, 0);
 
-       if( request_irq(shpnt->irq, swintr, SA_INTERRUPT|SA_SHIRQ, "aha152x", shpnt) ) {
+       if( request_irq(shpnt->irq, swintr, IRQF_DISABLED|IRQF_SHARED, "aha152x", shpnt) ) {
                printk(KERN_ERR "aha152x%d: irq %d busy.\n", shpnt->host_no, shpnt->irq);
                goto out_host_put;
        }
@@ -889,7 +880,7 @@ struct Scsi_Host *aha152x_probe_one(struct aha152x_setup *setup)
        SETPORT(SSTAT0, 0x7f);
        SETPORT(SSTAT1, 0xef);
 
-       if ( request_irq(shpnt->irq, intr, SA_INTERRUPT|SA_SHIRQ, "aha152x", shpnt) ) {
+       if ( request_irq(shpnt->irq, intr, IRQF_DISABLED|IRQF_SHARED, "aha152x", shpnt) ) {
                printk(KERN_ERR "aha152x%d: failed to reassign irq %d.\n", shpnt->host_no, shpnt->irq);
                goto out_host_put;
        }
@@ -902,12 +893,10 @@ struct Scsi_Host *aha152x_probe_one(struct aha152x_setup *setup)
 
        scsi_scan_host(shpnt);
 
-       registered_count++;
-
        return shpnt;
 
 out_host_put:
-       aha152x_host[registered_count]=NULL;
+       list_del(&HOSTDATA(shpnt)->host_list);
        scsi_host_put(shpnt);
 
        return NULL;
@@ -932,6 +921,7 @@ void aha152x_release(struct Scsi_Host *shpnt)
 #endif
 
        scsi_remove_host(shpnt);
+       list_del(&HOSTDATA(shpnt)->host_list);
        scsi_host_put(shpnt);
 }
 
@@ -979,7 +969,8 @@ static int setup_expected_interrupts(struct Scsi_Host *shpnt)
 /* 
  *  Queue a command and setup interrupts for a free bus.
  */
-static int aha152x_internal_queue(Scsi_Cmnd *SCpnt, struct semaphore *sem, int phase, void (*done)(Scsi_Cmnd *))
+static int aha152x_internal_queue(Scsi_Cmnd *SCpnt, struct completion *complete,
+               int phase, void (*done)(Scsi_Cmnd *))
 {
        struct Scsi_Host *shpnt = SCpnt->device->host;
        unsigned long flags;
@@ -1006,15 +997,24 @@ static int aha152x_internal_queue(Scsi_Cmnd *SCpnt, struct semaphore *sem, int p
                        return FAILED;
                }
        } else {
+               struct aha152x_scdata *sc;
+
                SCpnt->host_scribble = kmalloc(sizeof(struct aha152x_scdata), GFP_ATOMIC);
                if(SCpnt->host_scribble==0) {
                        printk(ERR_LEAD "allocation failed\n", CMDINFO(SCpnt));
                        return FAILED;
                }
+
+               sc = SCDATA(SCpnt);
+               memcpy(sc->cmnd, SCpnt->cmnd, sizeof(sc->cmnd));
+               sc->request_buffer  = SCpnt->request_buffer;
+               sc->request_bufflen = SCpnt->request_bufflen;
+               sc->use_sg          = SCpnt->use_sg;
+               sc->cmd_len         = SCpnt->cmd_len;
        }
 
        SCNEXT(SCpnt)           = NULL;
-       SCSEM(SCpnt)            = sem;
+       SCSEM(SCpnt)            = complete;
 
        /* setup scratch area
           SCp.ptr              : buffer pointer
@@ -1085,9 +1085,9 @@ static void reset_done(Scsi_Cmnd *SCpnt)
        DPRINTK(debug_eh, INFO_LEAD "reset_done called\n", CMDINFO(SCpnt));
 #endif
        if(SCSEM(SCpnt)) {
-               up(SCSEM(SCpnt));
+               complete(SCSEM(SCpnt));
        } else {
-               printk(KERN_ERR "aha152x: reset_done w/o semaphore\n");
+               printk(KERN_ERR "aha152x: reset_done w/o completion\n");
        }
 }
 
@@ -1140,21 +1140,6 @@ static int aha152x_abort(Scsi_Cmnd *SCpnt)
        return FAILED;
 }
 
-static void timer_expired(unsigned long p)
-{
-       Scsi_Cmnd        *SCp   = (Scsi_Cmnd *)p;
-       struct semaphore *sem   = SCSEM(SCp);
-       struct Scsi_Host *shpnt = SCp->device->host;
-       unsigned long flags;
-
-       /* remove command from issue queue */
-       DO_LOCK(flags);
-       remove_SC(&ISSUE_SC, SCp);
-       DO_UNLOCK(flags);
-
-       up(sem);
-}
-
 /*
  * Reset a device
  *
@@ -1162,10 +1147,14 @@ static void timer_expired(unsigned long p)
 static int aha152x_device_reset(Scsi_Cmnd * SCpnt)
 {
        struct Scsi_Host *shpnt = SCpnt->device->host;
-       DECLARE_MUTEX_LOCKED(sem);
-       struct timer_list timer;
+       DECLARE_COMPLETION(done);
        int ret, issued, disconnected;
+       unsigned char old_cmd_len = SCpnt->cmd_len;
+       unsigned short old_use_sg = SCpnt->use_sg;
+       void *old_buffer = SCpnt->request_buffer;
+       unsigned old_bufflen = SCpnt->request_bufflen;
        unsigned long flags;
+       unsigned long timeleft;
 
 #if defined(AHA152X_DEBUG)
        if(HOSTDATA(shpnt)->debug & debug_eh) {
@@ -1189,20 +1178,20 @@ static int aha152x_device_reset(Scsi_Cmnd * SCpnt)
        SCpnt->request_buffer  = NULL;
        SCpnt->request_bufflen = 0;
 
-       init_timer(&timer);
-       timer.data     = (unsigned long) SCpnt;
-       timer.expires  = jiffies + 100*HZ;   /* 10s */
-       timer.function = (void (*)(unsigned long)) timer_expired;
+       aha152x_internal_queue(SCpnt, &done, resetting, reset_done);
 
-       aha152x_internal_queue(SCpnt, &sem, resetting, reset_done);
-       add_timer(&timer);
-       down(&sem);
-       del_timer(&timer);
-       
-       SCpnt->cmd_len         = SCpnt->old_cmd_len;
-       SCpnt->use_sg          = SCpnt->old_use_sg;
-       SCpnt->request_buffer  = SCpnt->buffer;
-               SCpnt->request_bufflen = SCpnt->bufflen;
+       timeleft = wait_for_completion_timeout(&done, 100*HZ);
+       if (!timeleft) {
+               /* remove command from issue queue */
+               DO_LOCK(flags);
+               remove_SC(&ISSUE_SC, SCpnt);
+               DO_UNLOCK(flags);
+       }
+
+       SCpnt->cmd_len         = old_cmd_len;
+       SCpnt->use_sg          = old_use_sg;
+       SCpnt->request_buffer  = old_buffer;
+               SCpnt->request_bufflen = old_bufflen;
 
        DO_LOCK(flags);
 
@@ -1260,16 +1249,15 @@ static void free_hard_reset_SCs(struct Scsi_Host *shpnt, Scsi_Cmnd **SCs)
  * Reset the bus
  *
  */
-static int aha152x_bus_reset(Scsi_Cmnd *SCpnt)
+static int aha152x_bus_reset_host(struct Scsi_Host *shpnt)
 {
-       struct Scsi_Host *shpnt = SCpnt->device->host;
        unsigned long flags;
 
        DO_LOCK(flags);
 
 #if defined(AHA152X_DEBUG)
        if(HOSTDATA(shpnt)->debug & debug_eh) {
-               printk(DEBUG_LEAD "aha152x_bus_reset(%p)", CMDINFO(SCpnt), SCpnt);
+               printk(KERN_DEBUG "scsi%d: bus reset", shpnt->host_no);
                show_queues(shpnt);
        }
 #endif
@@ -1277,14 +1265,14 @@ static int aha152x_bus_reset(Scsi_Cmnd *SCpnt)
        free_hard_reset_SCs(shpnt, &ISSUE_SC);
        free_hard_reset_SCs(shpnt, &DISCONNECTED_SC);
 
-       DPRINTK(debug_eh, DEBUG_LEAD "resetting bus\n", CMDINFO(SCpnt));
+       DPRINTK(debug_eh, KERN_DEBUG "scsi%d: resetting bus\n", shpnt->host_no);
 
        SETPORT(SCSISEQ, SCSIRSTO);
        mdelay(256);
        SETPORT(SCSISEQ, 0);
        mdelay(DELAY);
 
-       DPRINTK(debug_eh, DEBUG_LEAD "bus resetted\n", CMDINFO(SCpnt));
+       DPRINTK(debug_eh, KERN_DEBUG "scsi%d: bus resetted\n", shpnt->host_no);
 
        setup_expected_interrupts(shpnt);
        if(HOSTDATA(shpnt)->commands==0)
@@ -1295,6 +1283,14 @@ static int aha152x_bus_reset(Scsi_Cmnd *SCpnt)
        return SUCCESS;
 }
 
+/*
+ * Reset the bus
+ *
+ */
+static int aha152x_bus_reset(Scsi_Cmnd *SCpnt)
+{
+       return aha152x_bus_reset_host(SCpnt->device->host);
+}
 
 /*
  *  Restore default values to the AIC-6260 registers and reset the fifos
@@ -1337,23 +1333,28 @@ static void reset_ports(struct Scsi_Host *shpnt)
  * Reset the host (bus and controller)
  *
  */
-int aha152x_host_reset(Scsi_Cmnd * SCpnt)
+int aha152x_host_reset_host(struct Scsi_Host *shpnt)
 {
-#if defined(AHA152X_DEBUG)
-       struct Scsi_Host *shpnt = SCpnt->device->host;
-#endif
-
-       DPRINTK(debug_eh, DEBUG_LEAD "aha152x_host_reset(%p)\n", CMDINFO(SCpnt), SCpnt);
+       DPRINTK(debug_eh, KERN_DEBUG "scsi%d: host reset\n", shpnt->host_no);
 
-       aha152x_bus_reset(SCpnt);
+       aha152x_bus_reset_host(shpnt);
 
-       DPRINTK(debug_eh, DEBUG_LEAD "resetting ports\n", CMDINFO(SCpnt));
-       reset_ports(SCpnt->device->host);
+       DPRINTK(debug_eh, KERN_DEBUG "scsi%d: resetting ports\n", shpnt->host_no);
+       reset_ports(shpnt);
 
        return SUCCESS;
 }
 
 /*
+ * Reset the host (bus and controller)
+ * 
+ */
+static int aha152x_host_reset(Scsi_Cmnd *SCpnt)
+{
+       return aha152x_host_reset_host(SCpnt->device->host);
+}
+
+/*
  * Return the "logical geometry"
  *
  */
@@ -1427,26 +1428,25 @@ static struct work_struct aha152x_tq;
  * Run service completions on the card with interrupts enabled.
  *
  */
-static void run(void)
+static void run(struct work_struct *work)
 {
-       int i;
-       for (i = 0; i<ARRAY_SIZE(aha152x_host); i++) {
-               struct Scsi_Host *shpnt = aha152x_host[i];
-               if (shpnt && HOSTDATA(shpnt)->service) {
-                       HOSTDATA(shpnt)->service=0;
-                       is_complete(shpnt);
-               }
+       struct aha152x_hostdata *hd;
+
+       list_for_each_entry(hd, &aha152x_host_list, host_list) {
+               struct Scsi_Host *shost = container_of((void *)hd, struct Scsi_Host, hostdata);
+
+               is_complete(shost);
        }
 }
 
 /*
- *    Interrupts handler
+ * Interrupt handler
  *
  */
-
-static irqreturn_t intr(int irqno, void *dev_id, struct pt_regs *regs)
+static irqreturn_t intr(int irqno, void *dev_id)
 {
-       struct Scsi_Host *shpnt = lookup_irq(irqno);
+       struct Scsi_Host *shpnt = (struct Scsi_Host *)dev_id;
+       unsigned long flags;
        unsigned char rev, dmacntrl0;
 
        if (!shpnt) {
@@ -1472,23 +1472,23 @@ static irqreturn_t intr(int irqno, void *dev_id, struct pt_regs *regs)
        if ((rev == 0xFF) && (dmacntrl0 == 0xFF))
                return IRQ_NONE;
 
+       if( TESTLO(DMASTAT, INTSTAT) )
+               return IRQ_NONE;        
+
        /* no more interrupts from the controller, while we're busy.
           INTEN is restored by the BH handler */
        CLRBITS(DMACNTRL0, INTEN);
 
-#if 0
-       /* check if there is already something to be
-           serviced; should not happen */
-       if(HOSTDATA(shpnt)->service) {
-               printk(KERN_ERR "aha152x%d: lost interrupt (%d)\n", HOSTNO, HOSTDATA(shpnt)->service);
-               show_queues(shpnt);
+       DO_LOCK(flags);
+       if( HOSTDATA(shpnt)->service==0 ) {
+               HOSTDATA(shpnt)->service=1;
+
+               /* Poke the BH handler */
+               INIT_WORK(&aha152x_tq, run);
+               schedule_work(&aha152x_tq);
        }
-#endif
-       
-       /* Poke the BH handler */
-       HOSTDATA(shpnt)->service++;
-       INIT_WORK(&aha152x_tq, (void *) run, NULL);
-       schedule_work(&aha152x_tq);
+       DO_UNLOCK(flags);
+
        return IRQ_HANDLED;
 }
 
@@ -1557,6 +1557,9 @@ static void busfree_run(struct Scsi_Host *shpnt)
 #endif
 
                if(DONE_SC->SCp.phase & check_condition) {
+                       struct scsi_cmnd *cmd = HOSTDATA(shpnt)->done_SC;
+                       struct aha152x_scdata *sc = SCDATA(cmd);
+
 #if 0
                        if(HOSTDATA(shpnt)->debug & debug_eh) {
                                printk(ERR_LEAD "received sense: ", CMDINFO(DONE_SC));
@@ -1565,13 +1568,13 @@ static void busfree_run(struct Scsi_Host *shpnt)
 #endif
 
                        /* restore old command */
-                       memcpy((void *) DONE_SC->cmnd, (void *) DONE_SC->data_cmnd, sizeof(DONE_SC->data_cmnd));
-                       DONE_SC->request_buffer  = DONE_SC->buffer;
-                       DONE_SC->request_bufflen = DONE_SC->bufflen;
-                       DONE_SC->use_sg          = DONE_SC->old_use_sg;
-                       DONE_SC->cmd_len         = DONE_SC->old_cmd_len;
+                       memcpy(cmd->cmnd, sc->cmnd, sizeof(sc->cmnd));
+                       cmd->request_buffer  = sc->request_buffer;
+                       cmd->request_bufflen = sc->request_bufflen;
+                       cmd->use_sg          = sc->use_sg;
+                       cmd->cmd_len         = sc->cmd_len;
 
-                       DONE_SC->SCp.Status = 0x02;
+                       cmd->SCp.Status = 0x02;
 
                        HOSTDATA(shpnt)->commands--;
                        if (!HOSTDATA(shpnt)->commands)
@@ -2522,7 +2525,18 @@ static void is_complete(struct Scsi_Host *shpnt)
        unsigned long flags;
        int pending;
 
+       if(!shpnt)
+               return;
+
        DO_LOCK(flags);
+
+       if( HOSTDATA(shpnt)->service==0 )  {
+               DO_UNLOCK(flags);
+               return;
+       }
+
+       HOSTDATA(shpnt)->service = 0;
+
        if(HOSTDATA(shpnt)->in_intr) {
                DO_UNLOCK(flags);
                /* aha152x_error never returns.. */
@@ -3913,16 +3927,17 @@ static int __init aha152x_init(void)
 #endif
        }
 
-       return registered_count>0;
+       return 1;
 }
 
 static void __exit aha152x_exit(void)
 {
-       int i;
+       struct aha152x_hostdata *hd;
+
+       list_for_each_entry(hd, &aha152x_host_list, host_list) {
+               struct Scsi_Host *shost = container_of((void *)hd, struct Scsi_Host, hostdata);
 
-       for(i=0; i<ARRAY_SIZE(setup); i++) {
-               aha152x_release(aha152x_host[i]);
-               aha152x_host[i]=NULL;
+               aha152x_release(shost);
        }
 }