vserver 1.9.5.x5
[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/time.h>
42 #include <linux/interrupt.h>
43 #include <asm/semaphore.h>
44
45 #include <scsi/scsi_host.h>
46
47 #include "aacraid.h"
48
49 static irqreturn_t aac_sa_intr(int irq, void *dev_id, struct pt_regs *regs)
50 {
51         struct aac_dev *dev = dev_id;
52         unsigned short intstat, mask;
53
54         intstat = sa_readw(dev, DoorbellReg_p);
55         /*
56          *      Read mask and invert because drawbridge is reversed.
57          *      This allows us to only service interrupts that have been enabled.
58          */
59         mask = ~(sa_readw(dev, SaDbCSR.PRISETIRQMASK));
60
61         /* Check to see if this is our interrupt.  If it isn't just return */
62
63         if (intstat & mask) {
64                 if (intstat & PrintfReady) {
65                         aac_printf(dev, le32_to_cpu(sa_readl(dev, Mailbox5)));
66                         sa_writew(dev, DoorbellClrReg_p, PrintfReady); /* clear PrintfReady */
67                         sa_writew(dev, DoorbellReg_s, PrintfDone);
68                 } else if (intstat & DOORBELL_1) {      // dev -> Host Normal Command Ready
69                         aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
70                         sa_writew(dev, DoorbellClrReg_p, DOORBELL_1);
71                 } else if (intstat & DOORBELL_2) {      // dev -> Host Normal Response Ready
72                         aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
73                         sa_writew(dev, DoorbellClrReg_p, DOORBELL_2);
74                 } else if (intstat & DOORBELL_3) {      // dev -> Host Normal Command Not Full
75                         sa_writew(dev, DoorbellClrReg_p, DOORBELL_3);
76                 } else if (intstat & DOORBELL_4) {      // dev -> Host Normal Response Not Full
77                         sa_writew(dev, DoorbellClrReg_p, DOORBELL_4);
78                 }
79                 return IRQ_HANDLED;
80         }
81         return IRQ_NONE;
82 }
83
84 /**
85  *      aac_sa_notify_adapter           -       handle adapter notification
86  *      @dev:   Adapter that notification is for
87  *      @event: Event to notidy
88  *
89  *      Notify the adapter of an event
90  */
91  
92 void aac_sa_notify_adapter(struct aac_dev *dev, u32 event)
93 {
94         switch (event) {
95
96         case AdapNormCmdQue:
97                 sa_writew(dev, DoorbellReg_s,DOORBELL_1);
98                 break;
99         case HostNormRespNotFull:
100                 sa_writew(dev, DoorbellReg_s,DOORBELL_4);
101                 break;
102         case AdapNormRespQue:
103                 sa_writew(dev, DoorbellReg_s,DOORBELL_2);
104                 break;
105         case HostNormCmdNotFull:
106                 sa_writew(dev, DoorbellReg_s,DOORBELL_3);
107                 break;
108         case HostShutdown:
109                 //sa_sync_cmd(dev, HOST_CRASHING, 0, &ret);
110                 break;
111         case FastIo:
112                 sa_writew(dev, DoorbellReg_s,DOORBELL_6);
113                 break;
114         case AdapPrintfDone:
115                 sa_writew(dev, DoorbellReg_s,DOORBELL_5);
116                 break;
117         default:
118                 BUG();
119                 break;
120         }
121 }
122
123
124 /**
125  *      sa_sync_cmd     -       send a command and wait
126  *      @dev: Adapter
127  *      @command: Command to execute
128  *      @p1: first parameter
129  *      @ret: adapter status
130  *
131  *      This routine will send a synchronous comamnd to the adapter and wait 
132  *      for its completion.
133  */
134
135 static int sa_sync_cmd(struct aac_dev *dev, u32 command, u32 p1, u32 *ret)
136 {
137         unsigned long start;
138         int ok;
139         /*
140          *      Write the Command into Mailbox 0
141          */
142         sa_writel(dev, Mailbox0, cpu_to_le32(command));
143         /*
144          *      Write the parameters into Mailboxes 1 - 4
145          */
146         sa_writel(dev, Mailbox1, cpu_to_le32(p1));
147         sa_writel(dev, Mailbox2, 0);
148         sa_writel(dev, Mailbox3, 0);
149         sa_writel(dev, Mailbox4, 0);
150         /*
151          *      Clear the synch command doorbell to start on a clean slate.
152          */
153         sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
154         /*
155          *      Signal that there is a new synch command
156          */
157         sa_writew(dev, DoorbellReg_s, DOORBELL_0);
158
159         ok = 0;
160         start = jiffies;
161
162         while(time_before(jiffies, start+30*HZ))
163         {
164                 /*
165                  *      Delay 5uS so that the monitor gets access
166                  */
167                 udelay(5);
168                 /*
169                  *      Mon110 will set doorbell0 bit when it has 
170                  *      completed the command.
171                  */
172                 if(sa_readw(dev, DoorbellReg_p) & DOORBELL_0)  {
173                         ok = 1;
174                         break;
175                 }
176                 set_current_state(TASK_UNINTERRUPTIBLE);
177                 schedule_timeout(1);
178         }
179
180         if (ok != 1)
181                 return -ETIMEDOUT;
182         /*
183          *      Clear the synch command doorbell.
184          */
185         sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
186         /*
187          *      Pull the synch status from Mailbox 0.
188          */
189         *ret = le32_to_cpu(sa_readl(dev, Mailbox0));
190         return 0;
191 }
192
193 /**
194  *      aac_sa_interrupt_adapter        -       interrupt an adapter
195  *      @dev: Which adapter to enable.
196  *
197  *      Breakpoint an adapter.
198  */
199  
200 static void aac_sa_interrupt_adapter (struct aac_dev *dev)
201 {
202         u32 ret;
203         sa_sync_cmd(dev, BREAKPOINT_REQUEST, 0, &ret);
204 }
205
206 /**
207  *      aac_sa_start_adapter            -       activate adapter
208  *      @dev:   Adapter
209  *
210  *      Start up processing on an ARM based AAC adapter
211  */
212
213 static void aac_sa_start_adapter(struct aac_dev *dev)
214 {
215         u32 ret;
216         struct aac_init *init;
217         /*
218          * Fill in the remaining pieces of the init.
219          */
220         init = dev->init;
221         init->HostElapsedSeconds = cpu_to_le32(jiffies/HZ);
222
223         dprintk(("INIT\n"));
224         /*
225          * Tell the adapter we are back and up and running so it will scan its command
226          * queues and enable our interrupts
227          */
228         dev->irq_mask = (PrintfReady | DOORBELL_1 | DOORBELL_2 | DOORBELL_3 | DOORBELL_4);
229         /*
230          *      First clear out all interrupts.  Then enable the one's that 
231          *      we can handle.
232          */
233         dprintk(("MASK\n"));
234         sa_writew(dev, SaDbCSR.PRISETIRQMASK, cpu_to_le16(0xffff));
235         sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, (PrintfReady | DOORBELL_1 | DOORBELL_2 | DOORBELL_3 | DOORBELL_4));
236         dprintk(("SYNCCMD\n"));
237         /* We can only use a 32 bit address here */
238         sa_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa, &ret);
239 }
240
241 /**
242  *      aac_sa_check_health
243  *      @dev: device to check if healthy
244  *
245  *      Will attempt to determine if the specified adapter is alive and
246  *      capable of handling requests, returning 0 if alive.
247  */
248 static int aac_sa_check_health(struct aac_dev *dev)
249 {
250         long status = sa_readl(dev, Mailbox7);
251
252         /*
253          *      Check to see if the board failed any self tests.
254          */
255         if (status & SELF_TEST_FAILED)
256                 return -1;
257         /*
258          *      Check to see if the board panic'd while booting.
259          */
260         if (status & KERNEL_PANIC)
261                 return -2;
262         /*
263          *      Wait for the adapter to be up and running. Wait up to 3 minutes
264          */
265         if (!(status & KERNEL_UP_AND_RUNNING))
266                 return -3;
267         /*
268          *      Everything is OK
269          */
270         return 0;
271 }
272
273 /**
274  *      aac_sa_init     -       initialize an ARM based AAC card
275  *      @dev: device to configure
276  *
277  *      Allocate and set up resources for the ARM based AAC variants. The 
278  *      device_interface in the commregion will be allocated and linked 
279  *      to the comm region.
280  */
281
282 int aac_sa_init(struct aac_dev *dev)
283 {
284         unsigned long start;
285         unsigned long status;
286         int instance;
287         const char *name;
288
289         dprintk(("PREINST\n"));
290         instance = dev->id;
291         name     = dev->name;
292
293         /*
294          *      Map in the registers from the adapter.
295          */
296         dprintk(("PREMAP\n"));
297
298         if((dev->regs.sa = ioremap((unsigned long)dev->scsi_host_ptr->base, 8192))==NULL)
299         {       
300                 printk(KERN_WARNING "aacraid: unable to map ARM.\n" );
301                 goto error_iounmap;
302         }
303         /*
304          *      Check to see if the board failed any self tests.
305          */
306         if (sa_readl(dev, Mailbox7) & SELF_TEST_FAILED) {
307                 printk(KERN_WARNING "%s%d: adapter self-test failed.\n", name, instance);
308                 goto error_iounmap;
309         }
310         /*
311          *      Check to see if the board panic'd while booting.
312          */
313         if (sa_readl(dev, Mailbox7) & KERNEL_PANIC) {
314                 printk(KERN_WARNING "%s%d: adapter kernel panic'd.\n", name, instance);
315                 goto error_iounmap;
316         }
317         start = jiffies;
318         /*
319          *      Wait for the adapter to be up and running. Wait up to 3 minutes.
320          */
321         while (!(sa_readl(dev, Mailbox7) & KERNEL_UP_AND_RUNNING)) {
322                 if (time_after(jiffies, start+180*HZ)) {
323                         status = sa_readl(dev, Mailbox7) >> 16;
324                         printk(KERN_WARNING "%s%d: adapter kernel failed to start, init status = %d.\n", name, instance, le32_to_cpu(status));
325                         goto error_iounmap;
326                 }
327                 set_current_state(TASK_UNINTERRUPTIBLE);
328                 schedule_timeout(1);
329         }
330
331         dprintk(("ATIRQ\n"));
332         if (request_irq(dev->scsi_host_ptr->irq, aac_sa_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev ) < 0) {
333                 printk(KERN_WARNING "%s%d: Interrupt unavailable.\n", name, instance);
334                 goto error_iounmap;
335         }
336
337         /*
338          *      Fill in the function dispatch table.
339          */
340
341         dev->a_ops.adapter_interrupt = aac_sa_interrupt_adapter;
342         dev->a_ops.adapter_notify = aac_sa_notify_adapter;
343         dev->a_ops.adapter_sync_cmd = sa_sync_cmd;
344         dev->a_ops.adapter_check_health = aac_sa_check_health;
345
346         dprintk(("FUNCDONE\n"));
347
348         if(aac_init_adapter(dev) == NULL)
349                 goto error_irq;
350
351         dprintk(("NEWADAPTDONE\n"));
352         /*
353          *      Start any kernel threads needed
354          */
355         dev->thread_pid = kernel_thread((int (*)(void *))aac_command_thread, dev, 0);
356         if (dev->thread_pid < 0) {
357                 printk(KERN_ERR "aacraid: Unable to create command thread.\n");
358                 goto error_kfree;
359         }
360
361         /*
362          *      Tell the adapter that all is configure, and it can start 
363          *      accepting requests
364          */
365         dprintk(("STARTING\n"));
366         aac_sa_start_adapter(dev);
367         dprintk(("STARTED\n"));
368         return 0;
369
370
371 error_kfree:
372         kfree(dev->queues);
373
374 error_irq:
375         free_irq(dev->scsi_host_ptr->irq, (void *)dev);
376
377 error_iounmap:
378         iounmap(dev->regs.sa);
379
380         return -1;
381 }
382