[SCSI] qla2xxx: Correct regression in DMA-mask setting prior to allocations.
[linux-2.6] / drivers / scsi / a2091.c
1 #include <linux/types.h>
2 #include <linux/mm.h>
3 #include <linux/blkdev.h>
4 #include <linux/init.h>
5 #include <linux/interrupt.h>
6
7 #include <asm/setup.h>
8 #include <asm/page.h>
9 #include <asm/pgtable.h>
10 #include <asm/amigaints.h>
11 #include <asm/amigahw.h>
12 #include <linux/zorro.h>
13 #include <asm/irq.h>
14 #include <linux/spinlock.h>
15
16 #include "scsi.h"
17 #include <scsi/scsi_host.h>
18 #include "wd33c93.h"
19 #include "a2091.h"
20
21 #include<linux/stat.h>
22
23 #define DMA(ptr) ((a2091_scsiregs *)((ptr)->base))
24 #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata))
25
26 static irqreturn_t a2091_intr (int irq, void *_instance)
27 {
28     unsigned long flags;
29     unsigned int status;
30     struct Scsi_Host *instance = (struct Scsi_Host *)_instance;
31
32     status = DMA(instance)->ISTR;
33     if (!(status & (ISTR_INT_F|ISTR_INT_P)) || !(status & ISTR_INTS))
34         return IRQ_NONE;
35
36     spin_lock_irqsave(instance->host_lock, flags);
37     wd33c93_intr(instance);
38     spin_unlock_irqrestore(instance->host_lock, flags);
39     return IRQ_HANDLED;
40 }
41
42 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
43 {
44     unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
45     unsigned long addr = virt_to_bus(cmd->SCp.ptr);
46     struct Scsi_Host *instance = cmd->device->host;
47
48     /* don't allow DMA if the physical address is bad */
49     if (addr & A2091_XFER_MASK)
50     {
51         HDATA(instance)->dma_bounce_len = (cmd->SCp.this_residual + 511)
52             & ~0x1ff;
53         HDATA(instance)->dma_bounce_buffer =
54             kmalloc (HDATA(instance)->dma_bounce_len, GFP_KERNEL);
55         
56         /* can't allocate memory; use PIO */
57         if (!HDATA(instance)->dma_bounce_buffer) {
58             HDATA(instance)->dma_bounce_len = 0;
59             return 1;
60         }
61
62         /* get the physical address of the bounce buffer */
63         addr = virt_to_bus(HDATA(instance)->dma_bounce_buffer);
64
65         /* the bounce buffer may not be in the first 16M of physmem */
66         if (addr & A2091_XFER_MASK) {
67             /* we could use chipmem... maybe later */
68             kfree (HDATA(instance)->dma_bounce_buffer);
69             HDATA(instance)->dma_bounce_buffer = NULL;
70             HDATA(instance)->dma_bounce_len = 0;
71             return 1;
72         }
73
74         if (!dir_in) {
75                 /* copy to bounce buffer for a write */
76                 memcpy (HDATA(instance)->dma_bounce_buffer,
77                         cmd->SCp.ptr, cmd->SCp.this_residual);
78         }
79     }
80
81     /* setup dma direction */
82     if (!dir_in)
83         cntr |= CNTR_DDIR;
84
85     /* remember direction */
86     HDATA(cmd->device->host)->dma_dir = dir_in;
87
88     DMA(cmd->device->host)->CNTR = cntr;
89
90     /* setup DMA *physical* address */
91     DMA(cmd->device->host)->ACR = addr;
92
93     if (dir_in){
94         /* invalidate any cache */
95         cache_clear (addr, cmd->SCp.this_residual);
96     }else{
97         /* push any dirty cache */
98         cache_push (addr, cmd->SCp.this_residual);
99       }
100     /* start DMA */
101     DMA(cmd->device->host)->ST_DMA = 1;
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     /* disable SCSI interrupts */
117     DMA(instance)->CNTR = cntr;
118
119     /* flush if we were reading */
120     if (HDATA(instance)->dma_dir) {
121         DMA(instance)->FLUSH = 1;
122         while (!(DMA(instance)->ISTR & ISTR_FE_FLG))
123             ;
124     }
125
126     /* clear a possible interrupt */
127     DMA(instance)->CINT = 1;
128
129     /* stop DMA */
130     DMA(instance)->SP_DMA = 1;
131
132     /* restore the CONTROL bits (minus the direction flag) */
133     DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
134
135     /* copy from a bounce buffer, if necessary */
136     if (status && HDATA(instance)->dma_bounce_buffer) {
137         if( HDATA(instance)->dma_dir )
138                 memcpy (SCpnt->SCp.ptr, 
139                         HDATA(instance)->dma_bounce_buffer,
140                         SCpnt->SCp.this_residual);
141         kfree (HDATA(instance)->dma_bounce_buffer);
142         HDATA(instance)->dma_bounce_buffer = NULL;
143         HDATA(instance)->dma_bounce_len = 0;
144     }
145 }
146
147 int __init a2091_detect(struct scsi_host_template *tpnt)
148 {
149     static unsigned char called = 0;
150     struct Scsi_Host *instance;
151     unsigned long address;
152     struct zorro_dev *z = NULL;
153     wd33c93_regs regs;
154     int num_a2091 = 0;
155
156     if (!MACH_IS_AMIGA || called)
157         return 0;
158     called = 1;
159
160     tpnt->proc_name = "A2091";
161     tpnt->proc_info = &wd33c93_proc_info;
162
163     while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
164         if (z->id != ZORRO_PROD_CBM_A590_A2091_1 &&
165             z->id != ZORRO_PROD_CBM_A590_A2091_2)
166             continue;
167         address = z->resource.start;
168         if (!request_mem_region(address, 256, "wd33c93"))
169             continue;
170
171         instance = scsi_register (tpnt, sizeof (struct WD33C93_hostdata));
172         if (instance == NULL)
173             goto release;
174         instance->base = ZTWO_VADDR(address);
175         instance->irq = IRQ_AMIGA_PORTS;
176         instance->unique_id = z->slotaddr;
177         DMA(instance)->DAWR = DAWR_A2091;
178         regs.SASR = &(DMA(instance)->SASR);
179         regs.SCMD = &(DMA(instance)->SCMD);
180         HDATA(instance)->no_sync = 0xff;
181         HDATA(instance)->fast = 0;
182         HDATA(instance)->dma_mode = CTRL_DMA;
183         wd33c93_init(instance, regs, dma_setup, dma_stop, WD33C93_FS_8_10);
184         if (request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED, "A2091 SCSI",
185                         instance))
186             goto unregister;
187         DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
188         num_a2091++;
189         continue;
190
191 unregister:
192         scsi_unregister(instance);
193         wd33c93_release();
194 release:
195         release_mem_region(address, 256);
196     }
197
198     return num_a2091;
199 }
200
201 static int a2091_bus_reset(struct scsi_cmnd *cmd)
202 {
203         /* FIXME perform bus-specific reset */
204
205         /* FIXME 2: kill this function, and let midlayer fall back
206            to the same action, calling wd33c93_host_reset() */
207
208         spin_lock_irq(cmd->device->host->host_lock);
209         wd33c93_host_reset(cmd);
210         spin_unlock_irq(cmd->device->host->host_lock);
211
212         return SUCCESS;
213 }
214
215 #define HOSTS_C
216
217 static struct scsi_host_template driver_template = {
218         .proc_name              = "A2901",
219         .name                   = "Commodore A2091/A590 SCSI",
220         .detect                 = a2091_detect,
221         .release                = a2091_release,
222         .queuecommand           = wd33c93_queuecommand,
223         .eh_abort_handler       = wd33c93_abort,
224         .eh_bus_reset_handler   = a2091_bus_reset,
225         .eh_host_reset_handler  = wd33c93_host_reset,
226         .can_queue              = CAN_QUEUE,
227         .this_id                = 7,
228         .sg_tablesize           = SG_ALL,
229         .cmd_per_lun            = CMD_PER_LUN,
230         .use_clustering         = DISABLE_CLUSTERING
231 };
232
233
234 #include "scsi_module.c"
235
236 int a2091_release(struct Scsi_Host *instance)
237 {
238 #ifdef MODULE
239         DMA(instance)->CNTR = 0;
240         release_mem_region(ZTWO_PADDR(instance->base), 256);
241         free_irq(IRQ_AMIGA_PORTS, instance);
242         wd33c93_release();
243 #endif
244         return 1;
245 }
246
247 MODULE_LICENSE("GPL");