[SCSI] qla2xxx: Simplify interrupt handler locking.
[linux-2.6] / drivers / scsi / a3000.c
1 #include <linux/types.h>
2 #include <linux/mm.h>
3 #include <linux/blkdev.h>
4 #include <linux/ioport.h>
5 #include <linux/init.h>
6 #include <linux/spinlock.h>
7 #include <linux/interrupt.h>
8
9 #include <asm/setup.h>
10 #include <asm/page.h>
11 #include <asm/pgtable.h>
12 #include <asm/amigaints.h>
13 #include <asm/amigahw.h>
14 #include <asm/irq.h>
15
16 #include "scsi.h"
17 #include <scsi/scsi_host.h>
18 #include "wd33c93.h"
19 #include "a3000.h"
20
21 #include<linux/stat.h>
22
23 #define DMA(ptr) ((a3000_scsiregs *)((ptr)->base))
24 #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata))
25
26 static struct Scsi_Host *a3000_host = NULL;
27
28 static irqreturn_t a3000_intr (int irq, void *dummy)
29 {
30         unsigned long flags;
31         unsigned int status = DMA(a3000_host)->ISTR;
32
33         if (!(status & ISTR_INT_P))
34                 return IRQ_NONE;
35         if (status & ISTR_INTS)
36         {
37                 spin_lock_irqsave(a3000_host->host_lock, flags);
38                 wd33c93_intr (a3000_host);
39                 spin_unlock_irqrestore(a3000_host->host_lock, flags);
40                 return IRQ_HANDLED;
41         }
42         printk("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
43         return IRQ_NONE;
44 }
45
46 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
47 {
48     unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
49     unsigned long addr = virt_to_bus(cmd->SCp.ptr);
50
51     /*
52      * if the physical address has the wrong alignment, or if
53      * physical address is bad, or if it is a write and at the
54      * end of a physical memory chunk, then allocate a bounce
55      * buffer
56      */
57     if (addr & A3000_XFER_MASK)
58     {
59         HDATA(a3000_host)->dma_bounce_len = (cmd->SCp.this_residual + 511)
60             & ~0x1ff;
61         HDATA(a3000_host)->dma_bounce_buffer =
62             kmalloc (HDATA(a3000_host)->dma_bounce_len, GFP_KERNEL);
63         
64         /* can't allocate memory; use PIO */
65         if (!HDATA(a3000_host)->dma_bounce_buffer) {
66             HDATA(a3000_host)->dma_bounce_len = 0;
67             return 1;
68         }
69
70         if (!dir_in) {
71             /* copy to bounce buffer for a write */
72             memcpy (HDATA(a3000_host)->dma_bounce_buffer,
73                 cmd->SCp.ptr, cmd->SCp.this_residual);
74         }
75
76         addr = virt_to_bus(HDATA(a3000_host)->dma_bounce_buffer);
77     }
78
79     /* setup dma direction */
80     if (!dir_in)
81         cntr |= CNTR_DDIR;
82
83     /* remember direction */
84     HDATA(a3000_host)->dma_dir = dir_in;
85
86     DMA(a3000_host)->CNTR = cntr;
87
88     /* setup DMA *physical* address */
89     DMA(a3000_host)->ACR = addr;
90
91     if (dir_in)
92         /* invalidate any cache */
93         cache_clear (addr, cmd->SCp.this_residual);
94     else
95         /* push any dirty cache */
96         cache_push (addr, cmd->SCp.this_residual);
97
98     /* start DMA */
99     mb();                       /* make sure setup is completed */
100     DMA(a3000_host)->ST_DMA = 1;
101     mb();                       /* make sure DMA has started before next IO */
102
103     /* return success */
104     return 0;
105 }
106
107 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
108                      int status)
109 {
110     /* disable SCSI interrupts */
111     unsigned short cntr = CNTR_PDMD;
112
113     if (!HDATA(instance)->dma_dir)
114         cntr |= CNTR_DDIR;
115
116     DMA(instance)->CNTR = cntr;
117     mb();                       /* make sure CNTR is updated before next IO */
118
119     /* flush if we were reading */
120     if (HDATA(instance)->dma_dir) {
121         DMA(instance)->FLUSH = 1;
122         mb();                   /* don't allow prefetch */
123         while (!(DMA(instance)->ISTR & ISTR_FE_FLG))
124             barrier();
125         mb();                   /* no IO until FLUSH is done */
126     }
127
128     /* clear a possible interrupt */
129     /* I think that this CINT is only necessary if you are
130      * using the terminal count features.   HM 7 Mar 1994
131      */
132     DMA(instance)->CINT = 1;
133
134     /* stop DMA */
135     DMA(instance)->SP_DMA = 1;
136     mb();                       /* make sure DMA is stopped before next IO */
137
138     /* restore the CONTROL bits (minus the direction flag) */
139     DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
140     mb();                       /* make sure CNTR is updated before next IO */
141
142     /* copy from a bounce buffer, if necessary */
143     if (status && HDATA(instance)->dma_bounce_buffer) {
144         if (SCpnt) {
145             if (HDATA(instance)->dma_dir && SCpnt)
146                 memcpy (SCpnt->SCp.ptr,
147                         HDATA(instance)->dma_bounce_buffer,
148                         SCpnt->SCp.this_residual);
149             kfree (HDATA(instance)->dma_bounce_buffer);
150             HDATA(instance)->dma_bounce_buffer = NULL;
151             HDATA(instance)->dma_bounce_len = 0;
152         } else {
153             kfree (HDATA(instance)->dma_bounce_buffer);
154             HDATA(instance)->dma_bounce_buffer = NULL;
155             HDATA(instance)->dma_bounce_len = 0;
156         }
157     }
158 }
159
160 int __init a3000_detect(struct scsi_host_template *tpnt)
161 {
162     wd33c93_regs regs;
163
164     if  (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(A3000_SCSI))
165         return 0;
166     if (!request_mem_region(0xDD0000, 256, "wd33c93"))
167         return 0;
168
169     tpnt->proc_name = "A3000";
170     tpnt->proc_info = &wd33c93_proc_info;
171
172     a3000_host = scsi_register (tpnt, sizeof(struct WD33C93_hostdata));
173     if (a3000_host == NULL)
174         goto fail_register;
175
176     a3000_host->base = ZTWO_VADDR(0xDD0000);
177     a3000_host->irq = IRQ_AMIGA_PORTS;
178     DMA(a3000_host)->DAWR = DAWR_A3000;
179     regs.SASR = &(DMA(a3000_host)->SASR);
180     regs.SCMD = &(DMA(a3000_host)->SCMD);
181     HDATA(a3000_host)->no_sync = 0xff;
182     HDATA(a3000_host)->fast = 0;
183     HDATA(a3000_host)->dma_mode = CTRL_DMA;
184     wd33c93_init(a3000_host, regs, dma_setup, dma_stop, WD33C93_FS_12_15);
185     if (request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED, "A3000 SCSI",
186                     a3000_intr))
187         goto fail_irq;
188     DMA(a3000_host)->CNTR = CNTR_PDMD | CNTR_INTEN;
189
190     return 1;
191
192 fail_irq:
193     wd33c93_release();
194     scsi_unregister(a3000_host);
195 fail_register:
196     release_mem_region(0xDD0000, 256);
197     return 0;
198 }
199
200 static int a3000_bus_reset(struct scsi_cmnd *cmd)
201 {
202         /* FIXME perform bus-specific reset */
203         
204         /* FIXME 2: kill this entire function, which should
205            cause mid-layer to call wd33c93_host_reset anyway? */
206
207         spin_lock_irq(cmd->device->host->host_lock);
208         wd33c93_host_reset(cmd);
209         spin_unlock_irq(cmd->device->host->host_lock);
210
211         return SUCCESS;
212 }
213
214 #define HOSTS_C
215
216 static struct scsi_host_template driver_template = {
217         .proc_name              = "A3000",
218         .name                   = "Amiga 3000 built-in SCSI",
219         .detect                 = a3000_detect,
220         .release                = a3000_release,
221         .queuecommand           = wd33c93_queuecommand,
222         .eh_abort_handler       = wd33c93_abort,
223         .eh_bus_reset_handler   = a3000_bus_reset,
224         .eh_host_reset_handler  = wd33c93_host_reset,
225         .can_queue              = CAN_QUEUE,
226         .this_id                = 7,
227         .sg_tablesize           = SG_ALL,
228         .cmd_per_lun            = CMD_PER_LUN,
229         .use_clustering         = ENABLE_CLUSTERING
230 };
231
232
233 #include "scsi_module.c"
234
235 int a3000_release(struct Scsi_Host *instance)
236 {
237     wd33c93_release();
238     DMA(instance)->CNTR = 0;
239     release_mem_region(0xDD0000, 256);
240     free_irq(IRQ_AMIGA_PORTS, a3000_intr);
241     return 1;
242 }
243
244 MODULE_LICENSE("GPL");