make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / scsi / sgiwd93.c
1 /*
2  * sgiwd93.c: SGI WD93 scsi driver.
3  *
4  * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
5  *               1999 Andrew R. Baker (andrewb@uab.edu)
6  *                    - Support for 2nd SCSI controller on Indigo2
7  *               2001 Florian Lohoff (flo@rfc822.org)
8  *                    - Delete HPC scatter gather (Read corruption on 
9  *                      multiple disks)
10  *                    - Cleanup wback cache handling
11  * 
12  * (In all truth, Jed Schimmel wrote all this code.)
13  *
14  */
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/mm.h>
18 #include <linux/blk.h>
19 #include <linux/version.h>
20 #include <linux/delay.h>
21 #include <linux/spinlock.h>
22
23 #include <asm/page.h>
24 #include <asm/pgtable.h>
25 #include <asm/sgialib.h>
26 #include <asm/sgi/sgi.h>
27 #include <asm/sgi/sgimc.h>
28 #include <asm/sgi/sgihpc.h>
29 #include <asm/sgi/sgint23.h>
30 #include <asm/irq.h>
31 #include <asm/io.h>
32
33 #include "scsi.h"
34 #include "hosts.h"
35 #include "wd33c93.h"
36 #include "sgiwd93.h"
37
38 #include <linux/stat.h>
39
40 struct hpc_chunk {
41         struct hpc_dma_desc desc;
42         u32 _padding;   /* align to quadword boundary */
43 };
44
45 struct Scsi_Host *sgiwd93_host = NULL;
46 struct Scsi_Host *sgiwd93_host1 = NULL;
47
48 /* Wuff wuff, wuff, wd33c93.c, wuff wuff, object oriented, bow wow. */
49 static inline void write_wd33c93_count(const wd33c93_regs regs,
50                                       unsigned long value)
51 {
52         *regs.SASR = WD_TRANSFER_COUNT_MSB;
53         mb();
54         *regs.SCMD = ((value >> 16) & 0xff);
55         *regs.SCMD = ((value >>  8) & 0xff);
56         *regs.SCMD = ((value >>  0) & 0xff);
57         mb();
58 }
59
60 static inline unsigned long read_wd33c93_count(const wd33c93_regs regs)
61 {
62         unsigned long value;
63
64         *regs.SASR = WD_TRANSFER_COUNT_MSB;
65         mb();
66         value =  ((*regs.SCMD & 0xff) << 16);
67         value |= ((*regs.SCMD & 0xff) <<  8);
68         value |= ((*regs.SCMD & 0xff) <<  0);
69         mb();
70         return value;
71 }
72
73 /* XXX woof! */
74 static void sgiwd93_intr(int irq, void *dev_id, struct pt_regs *regs)
75 {
76         unsigned long flags;
77
78         spin_lock_irqsave(&io_request_lock, flags);
79         wd33c93_intr((struct Scsi_Host *) dev_id);
80         spin_unlock_irqrestore(&io_request_lock, flags);
81 }
82
83 #undef DEBUG_DMA
84
85 static inline
86 void fill_hpc_entries (struct hpc_chunk **hcp, char *addr, unsigned long len)
87 {
88         unsigned long physaddr;
89         unsigned long count;
90         
91         physaddr = PHYSADDR(addr);
92         while (len) {
93                 /*
94                  * even cntinfo could be up to 16383, without
95                  * magic only 8192 works correctly
96                  */
97                 count = len > 8192 ? 8192 : len;
98                 (*hcp)->desc.pbuf = physaddr;
99                 (*hcp)->desc.cntinfo = count;
100                 (*hcp)++;
101                 len -= count;
102                 physaddr += count;
103         }
104 }
105
106 static int dma_setup(Scsi_Cmnd *cmd, int datainp)
107 {
108         struct WD33C93_hostdata *hdata = (struct WD33C93_hostdata *)cmd->host->hostdata;
109         struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) cmd->host->base;
110         struct hpc_chunk *hcp = (struct hpc_chunk *) hdata->dma_bounce_buffer;
111
112 #ifdef DEBUG_DMA
113         printk("dma_setup: datainp<%d> hcp<%p> ",
114                datainp, hcp);
115 #endif
116
117         hdata->dma_dir = datainp;
118
119         /*
120          * wd33c93 shouldn't pass us bogus dma_setups, but
121          * it does:-( The other wd33c93 drivers deal with
122          * it the same way (which isn't that obvious).
123          * IMHO a better fix would be, not to do these
124          * dma setups in the first place
125          */
126         if (cmd->SCp.ptr == NULL)
127                 return 1;
128
129         fill_hpc_entries (&hcp, cmd->SCp.ptr,cmd->SCp.this_residual);
130
131         /* To make sure, if we trip an HPC bug, that we transfer
132          * every single byte, we tag on an extra zero length dma
133          * descriptor at the end of the chain.
134          */
135         hcp->desc.pbuf = 0;
136         hcp->desc.cntinfo = (HPCDMA_EOX);
137
138 #ifdef DEBUG_DMA
139         printk(" HPCGO\n");
140 #endif
141
142         /* Start up the HPC. */
143         hregs->ndptr = PHYSADDR(hdata->dma_bounce_buffer);
144         if(datainp) {
145                 dma_cache_inv((unsigned long) cmd->SCp.ptr, cmd->SCp.this_residual);
146                 hregs->ctrl = (HPC3_SCTRL_ACTIVE);
147         } else {
148                 dma_cache_wback_inv((unsigned long) cmd->SCp.ptr, cmd->SCp.this_residual);
149                 hregs->ctrl = (HPC3_SCTRL_ACTIVE | HPC3_SCTRL_DIR);
150         }
151
152         return 0;
153 }
154
155 static void dma_stop(struct Scsi_Host *instance, Scsi_Cmnd *SCpnt,
156                      int status)
157 {
158         struct WD33C93_hostdata *hdata = (struct WD33C93_hostdata *)instance->hostdata;
159         struct hpc3_scsiregs *hregs;
160
161         if (!SCpnt)
162                 return;
163
164         hregs = (struct hpc3_scsiregs *) SCpnt->host->base;
165
166 #ifdef DEBUG_DMA
167         printk("dma_stop: status<%d> ", status);
168 #endif
169
170         /* First stop the HPC and flush it's FIFO. */
171         if(hdata->dma_dir) {
172                 hregs->ctrl |= HPC3_SCTRL_FLUSH;
173                 while(hregs->ctrl & HPC3_SCTRL_ACTIVE)
174                         barrier();
175         }
176         hregs->ctrl = 0;
177
178 #ifdef DEBUG_DMA
179         printk("\n");
180 #endif
181 }
182
183 void sgiwd93_reset(unsigned long base)
184 {
185         struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) base;
186
187         hregs->ctrl = HPC3_SCTRL_CRESET;
188         udelay (50);
189         hregs->ctrl = 0;
190 }
191
192 static inline void init_hpc_chain(uchar *buf)
193 {
194         struct hpc_chunk *hcp = (struct hpc_chunk *) buf;
195         unsigned long start, end;
196
197         start = (unsigned long) buf;
198         end = start + PAGE_SIZE;
199         while(start < end) {
200                 hcp->desc.pnext = PHYSADDR((hcp + 1));
201                 hcp->desc.cntinfo = HPCDMA_EOX;
202                 hcp++;
203                 start += sizeof(struct hpc_chunk);
204         };
205         hcp--;
206         hcp->desc.pnext = PHYSADDR(buf);
207
208         /* Force flush to memory */
209         dma_cache_wback_inv((unsigned long) buf, PAGE_SIZE);
210 }
211
212 int __init sgiwd93_detect(Scsi_Host_Template *SGIblows)
213 {
214         static unsigned char called = 0;
215         struct hpc3_scsiregs *hregs = &hpc3c0->scsi_chan0;
216         struct hpc3_scsiregs *hregs1 = &hpc3c0->scsi_chan1;
217         struct WD33C93_hostdata *hdata;
218         struct WD33C93_hostdata *hdata1;
219         wd33c93_regs regs;
220         uchar *buf;
221         
222         if(called)
223                 return 0; /* Should bitch on the console about this... */
224
225         SGIblows->proc_name = "SGIWD93";
226
227         sgiwd93_host = scsi_register(SGIblows, sizeof(struct WD33C93_hostdata));
228         if(sgiwd93_host == NULL)
229                 return 0;
230         sgiwd93_host->base = (unsigned long) hregs;
231         sgiwd93_host->irq = SGI_WD93_0_IRQ;
232
233         buf = (uchar *) get_free_page(GFP_KERNEL);
234         if (!buf) {
235                 printk(KERN_WARNING "sgiwd93: Could not allocate memory for host0 buffer.\n");
236                 scsi_unregister(sgiwd93_host);
237                 return 0;
238         }
239         init_hpc_chain(buf);
240         
241         /* HPC_SCSI_REG0 | 0x03 | KSEG1 */
242         regs.SASR = (unsigned char*) KSEG1ADDR (0x1fbc0003);
243         regs.SCMD = (unsigned char*) KSEG1ADDR (0x1fbc0007);
244         wd33c93_init(sgiwd93_host, regs, dma_setup, dma_stop, WD33C93_FS_16_20);
245
246         hdata = (struct WD33C93_hostdata *)sgiwd93_host->hostdata;
247         hdata->no_sync = 0;
248         hdata->dma_bounce_buffer = (uchar *) (KSEG1ADDR(buf));
249
250         if (request_irq(SGI_WD93_0_IRQ, sgiwd93_intr, 0, "SGI WD93", (void *) sgiwd93_host)) {
251                 printk(KERN_WARNING "sgiwd93: Could not register IRQ %d (for host 0).\n", SGI_WD93_0_IRQ);
252 #ifdef MODULE
253                 wd33c93_release();
254 #endif
255                 free_page((unsigned long)buf);
256                 scsi_unregister(sgiwd93_host);
257                 return 0;
258         }
259         /* set up second controller on the Indigo2 */
260         if(!sgi_guiness) {
261                 sgiwd93_host1 = scsi_register(SGIblows, sizeof(struct WD33C93_hostdata));
262                 if(sgiwd93_host1 != NULL)
263                 {
264                         sgiwd93_host1->base = (unsigned long) hregs1;
265                         sgiwd93_host1->irq = SGI_WD93_1_IRQ;
266         
267                         buf = (uchar *) get_free_page(GFP_KERNEL);
268                         if (!buf) {
269                                 printk(KERN_WARNING "sgiwd93: Could not allocate memory for host1 buffer.\n");
270                                 scsi_unregister(sgiwd93_host1);
271                                 called = 1;
272                                 return 1; /* We registered host0 so return success*/
273                         }
274                         init_hpc_chain(buf);
275
276                         /* HPC_SCSI_REG1 | 0x03 | KSEG1 */
277                         regs.SASR = (unsigned char*) KSEG1ADDR(0x1fbc8003);
278                         regs.SCMD = (unsigned char*) KSEG1ADDR(0x1fbc8007);
279                         wd33c93_init(sgiwd93_host1, regs, dma_setup, dma_stop,
280                                      WD33C93_FS_16_20);
281         
282                         hdata1 = (struct WD33C93_hostdata *)sgiwd93_host1->hostdata;
283                         hdata1->no_sync = 0;
284                         hdata1->dma_bounce_buffer = (uchar *) (KSEG1ADDR(buf));
285         
286                         if (request_irq(SGI_WD93_1_IRQ, sgiwd93_intr, 0, "SGI WD93", (void *) sgiwd93_host1)) {
287                                 printk(KERN_WARNING "sgiwd93: Could not allocate irq %d (for host1).\n", SGI_WD93_1_IRQ);
288 #ifdef MODULE
289                                 wd33c93_release();
290 #endif
291                                 free_page((unsigned long)buf);
292                                 scsi_unregister(sgiwd93_host1);
293                                 /* Fall through since host0 registered OK */
294                         }
295                 }
296         }
297         
298         called = 1;
299
300         return 1; /* Found one. */
301 }
302
303 #define HOSTS_C
304
305 #include "sgiwd93.h"
306
307 static Scsi_Host_Template driver_template = SGIWD93_SCSI;
308
309 #include "scsi_module.c"
310
311 int sgiwd93_release(struct Scsi_Host *instance)
312 {
313 #ifdef MODULE
314         free_irq(SGI_WD93_0_IRQ, sgiwd93_intr);
315         free_page(KSEG0ADDR(hdata->dma_bounce_buffer));
316         wd33c93_release();
317         if(!sgi_guiness) {
318                 free_irq(SGI_WD93_1_IRQ, sgiwd93_intr);
319                 free_page(KSEG0ADDR(hdata1->dma_bounce_buffer));
320                 wd33c93_release();
321         }
322 #endif
323         return 1;
324 }