patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / scsi / aacraid / sa.c
1 /*
2  *      Adaptec AAC series RAID controller driver
3  *      (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; see the file COPYING.  If not, write to
22  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * Module Name:
25  *  sa.c
26  *
27  * Abstract: Drawbridge specific support functions
28  *
29  */
30
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/types.h>
34 #include <linux/sched.h>
35 #include <linux/pci.h>
36 #include <linux/spinlock.h>
37 #include <linux/slab.h>
38 #include <linux/blkdev.h>
39 #include <linux/delay.h>
40 #include <linux/completion.h>
41 #include <linux/interrupt.h>
42 #include <asm/semaphore.h>
43
44 #include <scsi/scsi_host.h>
45
46 #include "aacraid.h"
47
48 static irqreturn_t aac_sa_intr(int irq, void *dev_id, struct pt_regs *regs)
49 {
50         struct aac_dev *dev = dev_id;
51         unsigned short intstat, mask;
52
53         intstat = sa_readw(dev, DoorbellReg_p);
54         /*
55          *      Read mask and invert because drawbridge is reversed.
56          *      This allows us to only service interrupts that have been enabled.
57          */
58         mask = ~(sa_readw(dev, SaDbCSR.PRISETIRQMASK));
59
60         /* Check to see if this is our interrupt.  If it isn't just return */
61
62         if (intstat & mask) {
63                 if (intstat & PrintfReady) {
64                         aac_printf(dev, le32_to_cpu(sa_readl(dev, Mailbox5)));
65                         sa_writew(dev, DoorbellClrReg_p, PrintfReady); /* clear PrintfReady */
66                         sa_writew(dev, DoorbellReg_s, PrintfDone);
67                 } else if (intstat & DOORBELL_1) {      // dev -> Host Normal Command Ready
68                         aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
69                         sa_writew(dev, DoorbellClrReg_p, DOORBELL_1);
70                 } else if (intstat & DOORBELL_2) {      // dev -> Host Normal Response Ready
71                         aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
72                         sa_writew(dev, DoorbellClrReg_p, DOORBELL_2);
73                 } else if (intstat & DOORBELL_3) {      // dev -> Host Normal Command Not Full
74                         sa_writew(dev, DoorbellClrReg_p, DOORBELL_3);
75                 } else if (intstat & DOORBELL_4) {      // dev -> Host Normal Response Not Full
76                         sa_writew(dev, DoorbellClrReg_p, DOORBELL_4);
77                 }
78                 return IRQ_HANDLED;
79         }
80         return IRQ_NONE;
81 }
82
83 /**
84  *      aac_sa_enable_interrupt -       enable an interrupt event
85  *      @dev: Which adapter to enable.
86  *      @event: Which adapter event.
87  *
88  *      This routine will enable the corresponding adapter event to cause an interrupt on 
89  *      the host.
90  */
91  
92 void aac_sa_enable_interrupt(struct aac_dev *dev, u32 event)
93 {
94         switch (event) {
95
96         case HostNormCmdQue:
97                 sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_1);
98                 break;
99
100         case HostNormRespQue:
101                 sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_2);
102                 break;
103
104         case AdapNormCmdNotFull:
105                 sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_3);
106                 break;
107
108         case AdapNormRespNotFull:
109                 sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_4);
110                 break;
111         }
112 }
113
114 /**
115  *      aac_sa_disable_interrupt        -       disable an interrupt event
116  *      @dev: Which adapter to enable.
117  *      @event: Which adapter event.
118  *
119  *      This routine will enable the corresponding adapter event to cause an interrupt on 
120  *      the host.
121  */
122
123 void aac_sa_disable_interrupt (struct aac_dev *dev, u32 event)
124 {
125         switch (event) {
126
127         case HostNormCmdQue:
128                 sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_1);
129                 break;
130
131         case HostNormRespQue:
132                 sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_2);
133                 break;
134
135         case AdapNormCmdNotFull:
136                 sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_3);
137                 break;
138
139         case AdapNormRespNotFull:
140                 sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_4);
141                 break;
142         }
143 }
144
145 /**
146  *      aac_sa_notify_adapter           -       handle adapter notification
147  *      @dev:   Adapter that notification is for
148  *      @event: Event to notidy
149  *
150  *      Notify the adapter of an event
151  */
152  
153 void aac_sa_notify_adapter(struct aac_dev *dev, u32 event)
154 {
155         switch (event) {
156
157         case AdapNormCmdQue:
158                 sa_writew(dev, DoorbellReg_s,DOORBELL_1);
159                 break;
160         case HostNormRespNotFull:
161                 sa_writew(dev, DoorbellReg_s,DOORBELL_4);
162                 break;
163         case AdapNormRespQue:
164                 sa_writew(dev, DoorbellReg_s,DOORBELL_2);
165                 break;
166         case HostNormCmdNotFull:
167                 sa_writew(dev, DoorbellReg_s,DOORBELL_3);
168                 break;
169         case HostShutdown:
170                 //sa_sync_cmd(dev, HOST_CRASHING, 0, &ret);
171                 break;
172         case FastIo:
173                 sa_writew(dev, DoorbellReg_s,DOORBELL_6);
174                 break;
175         case AdapPrintfDone:
176                 sa_writew(dev, DoorbellReg_s,DOORBELL_5);
177                 break;
178         default:
179                 BUG();
180                 break;
181         }
182 }
183
184
185 /**
186  *      sa_sync_cmd     -       send a command and wait
187  *      @dev: Adapter
188  *      @command: Command to execute
189  *      @p1: first parameter
190  *      @ret: adapter status
191  *
192  *      This routine will send a synchronous comamnd to the adapter and wait 
193  *      for its completion.
194  */
195
196 static int sa_sync_cmd(struct aac_dev *dev, u32 command, u32 p1, u32 *ret)
197 {
198         unsigned long start;
199         int ok;
200         /*
201          *      Write the Command into Mailbox 0
202          */
203         sa_writel(dev, Mailbox0, cpu_to_le32(command));
204         /*
205          *      Write the parameters into Mailboxes 1 - 4
206          */
207         sa_writel(dev, Mailbox1, cpu_to_le32(p1));
208         sa_writel(dev, Mailbox2, 0);
209         sa_writel(dev, Mailbox3, 0);
210         sa_writel(dev, Mailbox4, 0);
211         /*
212          *      Clear the synch command doorbell to start on a clean slate.
213          */
214         sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
215         /*
216          *      Signal that there is a new synch command
217          */
218         sa_writew(dev, DoorbellReg_s, DOORBELL_0);
219
220         ok = 0;
221         start = jiffies;
222
223         while(time_before(jiffies, start+30*HZ))
224         {
225                 /*
226                  *      Delay 5uS so that the monitor gets access
227                  */
228                 udelay(5);
229                 /*
230                  *      Mon110 will set doorbell0 bit when it has 
231                  *      completed the command.
232                  */
233                 if(sa_readw(dev, DoorbellReg_p) & DOORBELL_0)  {
234                         ok = 1;
235                         break;
236                 }
237                 set_current_state(TASK_UNINTERRUPTIBLE);
238                 schedule_timeout(1);
239         }
240
241         if (ok != 1)
242                 return -ETIMEDOUT;
243         /*
244          *      Clear the synch command doorbell.
245          */
246         sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
247         /*
248          *      Pull the synch status from Mailbox 0.
249          */
250         *ret = le32_to_cpu(sa_readl(dev, Mailbox0));
251         return 0;
252 }
253
254 /**
255  *      aac_sa_interrupt_adapter        -       interrupt an adapter
256  *      @dev: Which adapter to enable.
257  *
258  *      Breakpoint an adapter.
259  */
260  
261 static void aac_sa_interrupt_adapter (struct aac_dev *dev)
262 {
263         u32 ret;
264         sa_sync_cmd(dev, BREAKPOINT_REQUEST, 0, &ret);
265 }
266
267 /**
268  *      aac_sa_start_adapter            -       activate adapter
269  *      @dev:   Adapter
270  *
271  *      Start up processing on an ARM based AAC adapter
272  */
273
274 static void aac_sa_start_adapter(struct aac_dev *dev)
275 {
276         u32 ret;
277         struct aac_init *init;
278         /*
279          * Fill in the remaining pieces of the init.
280          */
281         init = dev->init;
282         init->HostElapsedSeconds = cpu_to_le32(jiffies/HZ);
283
284         dprintk(("INIT\n"));
285         /*
286          * Tell the adapter we are back and up and running so it will scan its command
287          * queues and enable our interrupts
288          */
289         dev->irq_mask = (PrintfReady | DOORBELL_1 | DOORBELL_2 | DOORBELL_3 | DOORBELL_4);
290         /*
291          *      First clear out all interrupts.  Then enable the one's that 
292          *      we can handle.
293          */
294         dprintk(("MASK\n"));
295         sa_writew(dev, SaDbCSR.PRISETIRQMASK, cpu_to_le16(0xffff));
296         sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, (PrintfReady | DOORBELL_1 | DOORBELL_2 | DOORBELL_3 | DOORBELL_4));
297         dprintk(("SYNCCMD\n"));
298         /* We can only use a 32 bit address here */
299         sa_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa, &ret);
300 }
301
302 /**
303  *      aac_sa_check_health
304  *      @dev: device to check if healthy
305  *
306  *      Will attempt to determine if the specified adapter is alive and
307  *      capable of handling requests, returning 0 if alive.
308  */
309 static int aac_sa_check_health(struct aac_dev *dev)
310 {
311         long status = sa_readl(dev, Mailbox7);
312
313         /*
314          *      Check to see if the board failed any self tests.
315          */
316         if (status & SELF_TEST_FAILED)
317                 return -1;
318         /*
319          *      Check to see if the board panic'd while booting.
320          */
321         if (status & KERNEL_PANIC)
322                 return -2;
323         /*
324          *      Wait for the adapter to be up and running. Wait up to 3 minutes
325          */
326         if (!(status & KERNEL_UP_AND_RUNNING))
327                 return -3;
328         /*
329          *      Everything is OK
330          */
331         return 0;
332 } /* aac_sa_check_health */
333
334 /**
335  *      aac_sa_init     -       initialize an ARM based AAC card
336  *      @dev: device to configure
337  *      @devnum: adapter number
338  *
339  *      Allocate and set up resources for the ARM based AAC variants. The 
340  *      device_interface in the commregion will be allocated and linked 
341  *      to the comm region.
342  */
343
344 int aac_sa_init(struct aac_dev *dev, unsigned long devnum)
345 {
346         unsigned long start;
347         unsigned long status;
348         int instance;
349         const char *name;
350
351         dev->devnum = devnum;
352
353         dprintk(("PREINST\n"));
354         instance = dev->id;
355         name     = dev->name;
356
357         /*
358          *      Map in the registers from the adapter.
359          */
360         dprintk(("PREMAP\n"));
361
362         if((dev->regs.sa = (struct sa_registers *)ioremap((unsigned long)dev->scsi_host_ptr->base, 8192))==NULL)
363         {       
364                 printk(KERN_WARNING "aacraid: unable to map ARM.\n" );
365                 return -1;
366         }
367         /*
368          *      Check to see if the board failed any self tests.
369          */
370         if (sa_readl(dev, Mailbox7) & SELF_TEST_FAILED) {
371                 printk(KERN_WARNING "%s%d: adapter self-test failed.\n", name, instance);
372                 return -1;
373         }
374         /*
375          *      Check to see if the board panic'd while booting.
376          */
377         if (sa_readl(dev, Mailbox7) & KERNEL_PANIC) {
378                 printk(KERN_WARNING "%s%d: adapter kernel panic'd.\n", name, instance);
379                 return -1;
380         }
381         start = jiffies;
382         /*
383          *      Wait for the adapter to be up and running. Wait up to 3 minutes.
384          */
385         while (!(sa_readl(dev, Mailbox7) & KERNEL_UP_AND_RUNNING)) {
386                 if (time_after(jiffies, start+180*HZ)) {
387                         status = sa_readl(dev, Mailbox7) >> 16;
388                         printk(KERN_WARNING "%s%d: adapter kernel failed to start, init status = %d.\n", name, instance, le32_to_cpu(status));
389                         return -1;
390                 }
391                 set_current_state(TASK_UNINTERRUPTIBLE);
392                 schedule_timeout(1);
393         }
394
395         dprintk(("ATIRQ\n"));
396         if (request_irq(dev->scsi_host_ptr->irq, aac_sa_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev ) < 0) {
397                 printk(KERN_WARNING "%s%d: Interrupt unavailable.\n", name, instance);
398                 return -1;
399         }
400
401         /*
402          *      Fill in the function dispatch table.
403          */
404
405         dev->a_ops.adapter_interrupt = aac_sa_interrupt_adapter;
406         dev->a_ops.adapter_enable_int = aac_sa_enable_interrupt;
407         dev->a_ops.adapter_disable_int = aac_sa_disable_interrupt;
408         dev->a_ops.adapter_notify = aac_sa_notify_adapter;
409         dev->a_ops.adapter_sync_cmd = sa_sync_cmd;
410         dev->a_ops.adapter_check_health = aac_sa_check_health;
411
412         dprintk(("FUNCDONE\n"));
413
414         if(aac_init_adapter(dev) == NULL)
415                 return -1;
416
417         dprintk(("NEWADAPTDONE\n"));
418         /*
419          *      Start any kernel threads needed
420          */
421         dev->thread_pid = kernel_thread((int (*)(void *))aac_command_thread, dev, 0);
422         if (dev->thread_pid < 0) {
423                 printk(KERN_ERR "aacraid: Unable to create command thread.\n");
424                 return -1;
425         }
426
427         /*
428          *      Tell the adapter that all is configure, and it can start 
429          *      accepting requests
430          */
431         dprintk(("STARTING\n"));
432         aac_sa_start_adapter(dev);
433         dprintk(("STARTED\n"));
434         return 0;
435 }
436