ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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/blkdev.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
50 /* XXX woof! */
51 static void sgiwd93_intr(int irq, void *dev_id, struct pt_regs *regs)
52 {
53         unsigned long flags;
54         struct Scsi_Host *dev = dev_id;
55         
56         spin_lock_irqsave(dev->host_lock, flags);
57         wd33c93_intr((struct Scsi_Host *) dev_id);
58         spin_unlock_irqrestore(dev->host_lock, flags);
59 }
60
61 #undef DEBUG_DMA
62
63 static inline
64 void fill_hpc_entries (struct hpc_chunk **hcp, char *addr, unsigned long len)
65 {
66         unsigned long physaddr;
67         unsigned long count;
68         
69         physaddr = PHYSADDR(addr);
70         while (len) {
71                 /*
72                  * even cntinfo could be up to 16383, without
73                  * magic only 8192 works correctly
74                  */
75                 count = len > 8192 ? 8192 : len;
76                 (*hcp)->desc.pbuf = physaddr;
77                 (*hcp)->desc.cntinfo = count;
78                 (*hcp)++;
79                 len -= count;
80                 physaddr += count;
81         }
82 }
83
84 static int dma_setup(Scsi_Cmnd *cmd, int datainp)
85 {
86         struct WD33C93_hostdata *hdata = (struct WD33C93_hostdata *)cmd->host->hostdata;
87         struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) cmd->host->base;
88         struct hpc_chunk *hcp = (struct hpc_chunk *) hdata->dma_bounce_buffer;
89
90 #ifdef DEBUG_DMA
91         printk("dma_setup: datainp<%d> hcp<%p> ",
92                datainp, hcp);
93 #endif
94
95         hdata->dma_dir = datainp;
96
97         /*
98          * wd33c93 shouldn't pass us bogus dma_setups, but
99          * it does:-( The other wd33c93 drivers deal with
100          * it the same way (which isn't that obvious).
101          * IMHO a better fix would be, not to do these
102          * dma setups in the first place
103          */
104         if (cmd->SCp.ptr == NULL)
105                 return 1;
106
107         fill_hpc_entries (&hcp, cmd->SCp.ptr,cmd->SCp.this_residual);
108
109         /* To make sure, if we trip an HPC bug, that we transfer
110          * every single byte, we tag on an extra zero length dma
111          * descriptor at the end of the chain.
112          */
113         hcp->desc.pbuf = 0;
114         hcp->desc.cntinfo = (HPCDMA_EOX);
115
116 #ifdef DEBUG_DMA
117         printk(" HPCGO\n");
118 #endif
119
120         /* Start up the HPC. */
121         hregs->ndptr = PHYSADDR(hdata->dma_bounce_buffer);
122         if(datainp) {
123                 dma_cache_inv((unsigned long) cmd->SCp.ptr, cmd->SCp.this_residual);
124                 hregs->ctrl = (HPC3_SCTRL_ACTIVE);
125         } else {
126                 dma_cache_wback_inv((unsigned long) cmd->SCp.ptr, cmd->SCp.this_residual);
127                 hregs->ctrl = (HPC3_SCTRL_ACTIVE | HPC3_SCTRL_DIR);
128         }
129
130         return 0;
131 }
132
133 static void dma_stop(struct Scsi_Host *instance, Scsi_Cmnd *SCpnt,
134                      int status)
135 {
136         struct WD33C93_hostdata *hdata = (struct WD33C93_hostdata *)instance->hostdata;
137         struct hpc3_scsiregs *hregs;
138
139         if (!SCpnt)
140                 return;
141
142         hregs = (struct hpc3_scsiregs *) SCpnt->host->base;
143
144 #ifdef DEBUG_DMA
145         printk("dma_stop: status<%d> ", status);
146 #endif
147
148         /* First stop the HPC and flush it's FIFO. */
149         if(hdata->dma_dir) {
150                 hregs->ctrl |= HPC3_SCTRL_FLUSH;
151                 while(hregs->ctrl & HPC3_SCTRL_ACTIVE)
152                         barrier();
153         }
154         hregs->ctrl = 0;
155
156 #ifdef DEBUG_DMA
157         printk("\n");
158 #endif
159 }
160
161 void sgiwd93_reset(unsigned long base)
162 {
163         struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) base;
164
165         hregs->ctrl = HPC3_SCTRL_CRESET;
166         udelay (50);
167         hregs->ctrl = 0;
168 }
169
170 static inline void init_hpc_chain(uchar *buf)
171 {
172         struct hpc_chunk *hcp = (struct hpc_chunk *) buf;
173         unsigned long start, end;
174
175         start = (unsigned long) buf;
176         end = start + PAGE_SIZE;
177         while(start < end) {
178                 hcp->desc.pnext = PHYSADDR((hcp + 1));
179                 hcp->desc.cntinfo = HPCDMA_EOX;
180                 hcp++;
181                 start += sizeof(struct hpc_chunk);
182         };
183         hcp--;
184         hcp->desc.pnext = PHYSADDR(buf);
185
186         /* Force flush to memory */
187         dma_cache_wback_inv((unsigned long) buf, PAGE_SIZE);
188 }
189
190 int __init sgiwd93_detect(Scsi_Host_Template *SGIblows)
191 {
192         static unsigned char called = 0;
193         struct hpc3_scsiregs *hregs = &hpc3c0->scsi_chan0;
194         struct hpc3_scsiregs *hregs1 = &hpc3c0->scsi_chan1;
195         struct WD33C93_hostdata *hdata;
196         struct WD33C93_hostdata *hdata1;
197         wd33c93_regs regs;
198         uchar *buf;
199         
200         if(called)
201                 return 0; /* Should bitch on the console about this... */
202
203         SGIblows->proc_name = "SGIWD93";
204
205         sgiwd93_host = scsi_register(SGIblows, sizeof(struct WD33C93_hostdata));
206         if(sgiwd93_host == NULL)
207                 return 0;
208         sgiwd93_host->base = (unsigned long) hregs;
209         sgiwd93_host->irq = SGI_WD93_0_IRQ;
210
211         buf = (uchar *) get_zeroed_page(GFP_KERNEL);
212         if (!buf) {
213                 printk(KERN_WARNING "sgiwd93: Could not allocate memory for host0 buffer.\n");
214                 scsi_unregister(sgiwd93_host);
215                 return 0;
216         }
217         init_hpc_chain(buf);
218         
219         /* HPC_SCSI_REG0 | 0x03 | KSEG1 */
220         regs.SASR = (unsigned char*) KSEG1ADDR (0x1fbc0003);
221         regs.SCMD = (unsigned char*) KSEG1ADDR (0x1fbc0007);
222         wd33c93_init(sgiwd93_host, regs, dma_setup, dma_stop, WD33C93_FS_16_20);
223
224         hdata = (struct WD33C93_hostdata *)sgiwd93_host->hostdata;
225         hdata->no_sync = 0;
226         hdata->dma_bounce_buffer = (uchar *) (KSEG1ADDR(buf));
227
228         if (request_irq(SGI_WD93_0_IRQ, sgiwd93_intr, 0, "SGI WD93", (void *) sgiwd93_host)) {
229                 printk(KERN_WARNING "sgiwd93: Could not register IRQ %d (for host 0).\n", SGI_WD93_0_IRQ);
230                 wd33c93_release();
231                 free_page((unsigned long)buf);
232                 scsi_unregister(sgiwd93_host);
233                 return 0;
234         }
235         /* set up second controller on the Indigo2 */
236         if(!sgi_guiness) {
237                 sgiwd93_host1 = scsi_register(SGIblows, sizeof(struct WD33C93_hostdata));
238                 if(sgiwd93_host1 != NULL)
239                 {
240                         sgiwd93_host1->base = (unsigned long) hregs1;
241                         sgiwd93_host1->irq = SGI_WD93_1_IRQ;
242         
243                         buf = (uchar *) get_zeroed_page(GFP_KERNEL);
244                         if (!buf) {
245                                 printk(KERN_WARNING "sgiwd93: Could not allocate memory for host1 buffer.\n");
246                                 scsi_unregister(sgiwd93_host1);
247                                 called = 1;
248                                 return 1; /* We registered host0 so return success*/
249                         }
250                         init_hpc_chain(buf);
251
252                         /* HPC_SCSI_REG1 | 0x03 | KSEG1 */
253                         regs.SASR = (unsigned char*) KSEG1ADDR(0x1fbc8003);
254                         regs.SCMD = (unsigned char*) KSEG1ADDR(0x1fbc8007);
255                         wd33c93_init(sgiwd93_host1, regs, dma_setup, dma_stop,
256                                      WD33C93_FS_16_20);
257         
258                         hdata1 = (struct WD33C93_hostdata *)sgiwd93_host1->hostdata;
259                         hdata1->no_sync = 0;
260                         hdata1->dma_bounce_buffer = (uchar *) (KSEG1ADDR(buf));
261         
262                         if (request_irq(SGI_WD93_1_IRQ, sgiwd93_intr, 0, "SGI WD93", (void *) sgiwd93_host1)) {
263                                 printk(KERN_WARNING "sgiwd93: Could not allocate irq %d (for host1).\n", SGI_WD93_1_IRQ);
264                                 wd33c93_release();
265                                 free_page((unsigned long)buf);
266                                 scsi_unregister(sgiwd93_host1);
267                                 /* Fall through since host0 registered OK */
268                         }
269                 }
270         }
271         
272         called = 1;
273
274         return 1; /* Found one. */
275 }
276
277 int sgiwd93_release(struct Scsi_Host *instance)
278 {
279         free_irq(SGI_WD93_0_IRQ, sgiwd93_intr);
280         free_page(KSEG0ADDR(hdata->dma_bounce_buffer));
281         wd33c93_release();
282         if(!sgi_guiness) {
283                 free_irq(SGI_WD93_1_IRQ, sgiwd93_intr);
284                 free_page(KSEG0ADDR(hdata1->dma_bounce_buffer));
285                 wd33c93_release();
286         }
287         return 1;
288 }
289
290 static Scsi_Host_Template driver_template = {
291         .proc_name           = "SGIWD93",
292         .name                = "SGI WD93",
293         .detect              = sgiwd93_detect,
294         .release             = sgiwd93_release,
295         .queuecommand        = wd33c93_queuecommand,
296         .abort               = wd33c93_abort,
297         .reset               = wd33c93_reset,
298         .can_queue           = CAN_QUEUE,
299         .this_id             = 7,
300         .sg_tablesize        = SG_ALL,
301         .cmd_per_lun         = CMD_PER_LUN,
302         .use_clustering      = DISABLE_CLUSTERING,
303 };
304 #include "scsi_module.c"