VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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_enable_interrupt -       enable an interrupt event
86  *      @dev: Which adapter to enable.
87  *      @event: Which adapter event.
88  *
89  *      This routine will enable the corresponding adapter event to cause an interrupt on 
90  *      the host.
91  */
92  
93 void aac_sa_enable_interrupt(struct aac_dev *dev, u32 event)
94 {
95         switch (event) {
96
97         case HostNormCmdQue:
98                 sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_1);
99                 break;
100
101         case HostNormRespQue:
102                 sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_2);
103                 break;
104
105         case AdapNormCmdNotFull:
106                 sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_3);
107                 break;
108
109         case AdapNormRespNotFull:
110                 sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_4);
111                 break;
112         }
113 }
114
115 /**
116  *      aac_sa_disable_interrupt        -       disable an interrupt event
117  *      @dev: Which adapter to enable.
118  *      @event: Which adapter event.
119  *
120  *      This routine will enable the corresponding adapter event to cause an interrupt on 
121  *      the host.
122  */
123
124 void aac_sa_disable_interrupt (struct aac_dev *dev, u32 event)
125 {
126         switch (event) {
127
128         case HostNormCmdQue:
129                 sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_1);
130                 break;
131
132         case HostNormRespQue:
133                 sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_2);
134                 break;
135
136         case AdapNormCmdNotFull:
137                 sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_3);
138                 break;
139
140         case AdapNormRespNotFull:
141                 sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_4);
142                 break;
143         }
144 }
145
146 /**
147  *      aac_sa_notify_adapter           -       handle adapter notification
148  *      @dev:   Adapter that notification is for
149  *      @event: Event to notidy
150  *
151  *      Notify the adapter of an event
152  */
153  
154 void aac_sa_notify_adapter(struct aac_dev *dev, u32 event)
155 {
156         switch (event) {
157
158         case AdapNormCmdQue:
159                 sa_writew(dev, DoorbellReg_s,DOORBELL_1);
160                 break;
161         case HostNormRespNotFull:
162                 sa_writew(dev, DoorbellReg_s,DOORBELL_4);
163                 break;
164         case AdapNormRespQue:
165                 sa_writew(dev, DoorbellReg_s,DOORBELL_2);
166                 break;
167         case HostNormCmdNotFull:
168                 sa_writew(dev, DoorbellReg_s,DOORBELL_3);
169                 break;
170         case HostShutdown:
171                 //sa_sync_cmd(dev, HOST_CRASHING, 0, &ret);
172                 break;
173         case FastIo:
174                 sa_writew(dev, DoorbellReg_s,DOORBELL_6);
175                 break;
176         case AdapPrintfDone:
177                 sa_writew(dev, DoorbellReg_s,DOORBELL_5);
178                 break;
179         default:
180                 BUG();
181                 break;
182         }
183 }
184
185
186 /**
187  *      sa_sync_cmd     -       send a command and wait
188  *      @dev: Adapter
189  *      @command: Command to execute
190  *      @p1: first parameter
191  *      @ret: adapter status
192  *
193  *      This routine will send a synchronous comamnd to the adapter and wait 
194  *      for its completion.
195  */
196
197 static int sa_sync_cmd(struct aac_dev *dev, u32 command, u32 p1, u32 *ret)
198 {
199         unsigned long start;
200         int ok;
201         /*
202          *      Write the Command into Mailbox 0
203          */
204         sa_writel(dev, Mailbox0, cpu_to_le32(command));
205         /*
206          *      Write the parameters into Mailboxes 1 - 4
207          */
208         sa_writel(dev, Mailbox1, cpu_to_le32(p1));
209         sa_writel(dev, Mailbox2, 0);
210         sa_writel(dev, Mailbox3, 0);
211         sa_writel(dev, Mailbox4, 0);
212         /*
213          *      Clear the synch command doorbell to start on a clean slate.
214          */
215         sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
216         /*
217          *      Signal that there is a new synch command
218          */
219         sa_writew(dev, DoorbellReg_s, DOORBELL_0);
220
221         ok = 0;
222         start = jiffies;
223
224         while(time_before(jiffies, start+30*HZ))
225         {
226                 /*
227                  *      Delay 5uS so that the monitor gets access
228                  */
229                 udelay(5);
230                 /*
231                  *      Mon110 will set doorbell0 bit when it has 
232                  *      completed the command.
233                  */
234                 if(sa_readw(dev, DoorbellReg_p) & DOORBELL_0)  {
235                         ok = 1;
236                         break;
237                 }
238                 set_current_state(TASK_UNINTERRUPTIBLE);
239                 schedule_timeout(1);
240         }
241
242         if (ok != 1)
243                 return -ETIMEDOUT;
244         /*
245          *      Clear the synch command doorbell.
246          */
247         sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
248         /*
249          *      Pull the synch status from Mailbox 0.
250          */
251         *ret = le32_to_cpu(sa_readl(dev, Mailbox0));
252         return 0;
253 }
254
255 /**
256  *      aac_sa_interrupt_adapter        -       interrupt an adapter
257  *      @dev: Which adapter to enable.
258  *
259  *      Breakpoint an adapter.
260  */
261  
262 static void aac_sa_interrupt_adapter (struct aac_dev *dev)
263 {
264         u32 ret;
265         sa_sync_cmd(dev, BREAKPOINT_REQUEST, 0, &ret);
266 }
267
268 /**
269  *      aac_sa_start_adapter            -       activate adapter
270  *      @dev:   Adapter
271  *
272  *      Start up processing on an ARM based AAC adapter
273  */
274
275 static void aac_sa_start_adapter(struct aac_dev *dev)
276 {
277         u32 ret;
278         struct aac_init *init;
279         /*
280          * Fill in the remaining pieces of the init.
281          */
282         init = dev->init;
283         init->HostElapsedSeconds = cpu_to_le32(jiffies/HZ);
284
285         dprintk(("INIT\n"));
286         /*
287          * Tell the adapter we are back and up and running so it will scan its command
288          * queues and enable our interrupts
289          */
290         dev->irq_mask = (PrintfReady | DOORBELL_1 | DOORBELL_2 | DOORBELL_3 | DOORBELL_4);
291         /*
292          *      First clear out all interrupts.  Then enable the one's that 
293          *      we can handle.
294          */
295         dprintk(("MASK\n"));
296         sa_writew(dev, SaDbCSR.PRISETIRQMASK, cpu_to_le16(0xffff));
297         sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, (PrintfReady | DOORBELL_1 | DOORBELL_2 | DOORBELL_3 | DOORBELL_4));
298         dprintk(("SYNCCMD\n"));
299         /* We can only use a 32 bit address here */
300         sa_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa, &ret);
301 }
302
303 /**
304  *      aac_sa_check_health
305  *      @dev: device to check if healthy
306  *
307  *      Will attempt to determine if the specified adapter is alive and
308  *      capable of handling requests, returning 0 if alive.
309  */
310 static int aac_sa_check_health(struct aac_dev *dev)
311 {
312         long status = sa_readl(dev, Mailbox7);
313
314         /*
315          *      Check to see if the board failed any self tests.
316          */
317         if (status & SELF_TEST_FAILED)
318                 return -1;
319         /*
320          *      Check to see if the board panic'd while booting.
321          */
322         if (status & KERNEL_PANIC)
323                 return -2;
324         /*
325          *      Wait for the adapter to be up and running. Wait up to 3 minutes
326          */
327         if (!(status & KERNEL_UP_AND_RUNNING))
328                 return -3;
329         /*
330          *      Everything is OK
331          */
332         return 0;
333 }
334
335 /**
336  *      aac_sa_init     -       initialize an ARM based AAC card
337  *      @dev: device to configure
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)
345 {
346         unsigned long start;
347         unsigned long status;
348         int instance;
349         const char *name;
350
351         dprintk(("PREINST\n"));
352         instance = dev->id;
353         name     = dev->name;
354
355         /*
356          *      Map in the registers from the adapter.
357          */
358         dprintk(("PREMAP\n"));
359
360         if((dev->regs.sa = (struct sa_registers *)ioremap((unsigned long)dev->scsi_host_ptr->base, 8192))==NULL)
361         {       
362                 printk(KERN_WARNING "aacraid: unable to map ARM.\n" );
363                 goto error_iounmap;
364         }
365         /*
366          *      Check to see if the board failed any self tests.
367          */
368         if (sa_readl(dev, Mailbox7) & SELF_TEST_FAILED) {
369                 printk(KERN_WARNING "%s%d: adapter self-test failed.\n", name, instance);
370                 goto error_iounmap;
371         }
372         /*
373          *      Check to see if the board panic'd while booting.
374          */
375         if (sa_readl(dev, Mailbox7) & KERNEL_PANIC) {
376                 printk(KERN_WARNING "%s%d: adapter kernel panic'd.\n", name, instance);
377                 goto error_iounmap;
378         }
379         start = jiffies;
380         /*
381          *      Wait for the adapter to be up and running. Wait up to 3 minutes.
382          */
383         while (!(sa_readl(dev, Mailbox7) & KERNEL_UP_AND_RUNNING)) {
384                 if (time_after(jiffies, start+180*HZ)) {
385                         status = sa_readl(dev, Mailbox7) >> 16;
386                         printk(KERN_WARNING "%s%d: adapter kernel failed to start, init status = %d.\n", name, instance, le32_to_cpu(status));
387                         goto error_iounmap;
388                 }
389                 set_current_state(TASK_UNINTERRUPTIBLE);
390                 schedule_timeout(1);
391         }
392
393         dprintk(("ATIRQ\n"));
394         if (request_irq(dev->scsi_host_ptr->irq, aac_sa_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev ) < 0) {
395                 printk(KERN_WARNING "%s%d: Interrupt unavailable.\n", name, instance);
396                 goto error_iounmap;
397         }
398
399         /*
400          *      Fill in the function dispatch table.
401          */
402
403         dev->a_ops.adapter_interrupt = aac_sa_interrupt_adapter;
404         dev->a_ops.adapter_enable_int = aac_sa_enable_interrupt;
405         dev->a_ops.adapter_disable_int = aac_sa_disable_interrupt;
406         dev->a_ops.adapter_notify = aac_sa_notify_adapter;
407         dev->a_ops.adapter_sync_cmd = sa_sync_cmd;
408         dev->a_ops.adapter_check_health = aac_sa_check_health;
409
410         dprintk(("FUNCDONE\n"));
411
412         if(aac_init_adapter(dev) == NULL)
413                 goto error_irq;
414
415         dprintk(("NEWADAPTDONE\n"));
416         /*
417          *      Start any kernel threads needed
418          */
419         dev->thread_pid = kernel_thread((int (*)(void *))aac_command_thread, dev, 0);
420         if (dev->thread_pid < 0) {
421                 printk(KERN_ERR "aacraid: Unable to create command thread.\n");
422                 goto error_kfree;
423         }
424
425         /*
426          *      Tell the adapter that all is configure, and it can start 
427          *      accepting requests
428          */
429         dprintk(("STARTING\n"));
430         aac_sa_start_adapter(dev);
431         dprintk(("STARTED\n"));
432         return 0;
433
434
435 error_kfree:
436         kfree(dev->queues);
437
438 error_irq:
439         free_irq(dev->scsi_host_ptr->irq, (void *)dev);
440
441 error_iounmap:
442         iounmap(dev->regs.sa);
443
444         return -1;
445 }
446