ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / scsi / aacraid / rkt.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  *  rkt.c
26  *
27  * Abstract: Hardware miniport for Drawbridge specific hardware 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_rkt_intr(int irq, void *dev_id, struct pt_regs *regs)
49 {
50         struct aac_dev *dev = dev_id;
51         unsigned long bellbits;
52         u8 intstat, mask;
53         intstat = rkt_readb(dev, MUnit.OISR);
54         /*
55          *      Read mask and invert because drawbridge is reversed.
56          *      This allows us to only service interrupts that have 
57          *      been enabled.
58          */
59         mask = ~(dev->OIMR);
60         /* Check to see if this is our interrupt.  If it isn't just return */
61         if (intstat & mask) 
62         {
63                 bellbits = rkt_readl(dev, OutboundDoorbellReg);
64                 if (bellbits & DoorBellPrintfReady) {
65                         aac_printf(dev, le32_to_cpu(rkt_readl (dev, IndexRegs.Mailbox[5])));
66                         rkt_writel(dev, MUnit.ODR,DoorBellPrintfReady);
67                         rkt_writel(dev, InboundDoorbellReg,DoorBellPrintfDone);
68                 }
69                 else if (bellbits & DoorBellAdapterNormCmdReady) {
70                         aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
71                         rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdReady);
72                 }
73                 else if (bellbits & DoorBellAdapterNormRespReady) {
74                         aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
75                         rkt_writel(dev, MUnit.ODR,DoorBellAdapterNormRespReady);
76                 }
77                 else if (bellbits & DoorBellAdapterNormCmdNotFull) {
78                         rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
79                 }
80                 else if (bellbits & DoorBellAdapterNormRespNotFull) {
81                         rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
82                         rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormRespNotFull);
83                 }
84                 return IRQ_HANDLED;
85         }
86         return IRQ_NONE;
87 }
88
89 /**
90  *      aac_rkt_enable_interrupt        -       Enable event reporting
91  *      @dev: Adapter
92  *      @event: Event to enable
93  *
94  *      Enable event reporting from the i960 for a given event.
95  */
96  
97 static void aac_rkt_enable_interrupt(struct aac_dev * dev, u32 event)
98 {
99         switch (event) {
100
101         case HostNormCmdQue:
102                 dev->irq_mask &= ~(OUTBOUNDDOORBELL_1);
103                 break;
104
105         case HostNormRespQue:
106                 dev->irq_mask &= ~(OUTBOUNDDOORBELL_2);
107                 break;
108
109         case AdapNormCmdNotFull:
110                 dev->irq_mask &= ~(OUTBOUNDDOORBELL_3);
111                 break;
112
113         case AdapNormRespNotFull:
114                 dev->irq_mask &= ~(OUTBOUNDDOORBELL_4);
115                 break;
116         }
117 }
118
119 /**
120  *      aac_rkt_disable_interrupt       -       Disable event reporting
121  *      @dev: Adapter
122  *      @event: Event to enable
123  *
124  *      Disable event reporting from the i960 for a given event.
125  */
126
127 static void aac_rkt_disable_interrupt(struct aac_dev *dev, u32 event)
128 {
129         switch (event) {
130
131         case HostNormCmdQue:
132                 dev->irq_mask |= (OUTBOUNDDOORBELL_1);
133                 break;
134
135         case HostNormRespQue:
136                 dev->irq_mask |= (OUTBOUNDDOORBELL_2);
137                 break;
138
139         case AdapNormCmdNotFull:
140                 dev->irq_mask |= (OUTBOUNDDOORBELL_3);
141                 break;
142
143         case AdapNormRespNotFull:
144                 dev->irq_mask |= (OUTBOUNDDOORBELL_4);
145                 break;
146         }
147 }
148
149 /**
150  *      rkt_sync_cmd    -       send a command and wait
151  *      @dev: Adapter
152  *      @command: Command to execute
153  *      @p1: first parameter
154  *      @ret: adapter status
155  *
156  *      This routine will send a synchronous comamnd to the adapter and wait 
157  *      for its completion.
158  */
159
160 static int rkt_sync_cmd(struct aac_dev *dev, u32 command, u32 p1, u32 *status)
161 {
162         unsigned long start;
163         int ok;
164         /*
165          *      Write the command into Mailbox 0
166          */
167         rkt_writel(dev, InboundMailbox0, cpu_to_le32(command));
168         /*
169          *      Write the parameters into Mailboxes 1 - 4
170          */
171         rkt_writel(dev, InboundMailbox1, cpu_to_le32(p1));
172         rkt_writel(dev, InboundMailbox2, 0);
173         rkt_writel(dev, InboundMailbox3, 0);
174         rkt_writel(dev, InboundMailbox4, 0);
175         /*
176          *      Clear the synch command doorbell to start on a clean slate.
177          */
178         rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
179         /*
180          *      Disable doorbell interrupts
181          */
182         rkt_writeb(dev, MUnit.OIMR, dev->OIMR |= 0x04);
183         /*
184          *      Force the completion of the mask register write before issuing
185          *      the interrupt.
186          */
187         rkt_readb (dev, MUnit.OIMR);
188         /*
189          *      Signal that there is a new synch command
190          */
191         rkt_writel(dev, InboundDoorbellReg, INBOUNDDOORBELL_0);
192
193         ok = 0;
194         start = jiffies;
195
196         /*
197          *      Wait up to 30 seconds
198          */
199         while (time_before(jiffies, start+30*HZ)) 
200         {
201                 udelay(5);      /* Delay 5 microseconds to let Mon960 get info. */
202                 /*
203                  *      Mon960 will set doorbell0 bit when it has completed the command.
204                  */
205                 if (rkt_readl(dev, OutboundDoorbellReg) & OUTBOUNDDOORBELL_0) {
206                         /*
207                          *      Clear the doorbell.
208                          */
209                         rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
210                         ok = 1;
211                         break;
212                 }
213                 /*
214                  *      Yield the processor in case we are slow 
215                  */
216                 set_current_state(TASK_UNINTERRUPTIBLE);
217                 schedule_timeout(1);
218         }
219         if (ok != 1) {
220                 /*
221                  *      Restore interrupt mask even though we timed out
222                  */
223                 rkt_writeb(dev, MUnit.OIMR, dev->OIMR &= 0xfb);
224                 return -ETIMEDOUT;
225         }
226         /*
227          *      Pull the synch status from Mailbox 0.
228          */
229         *status = le32_to_cpu(rkt_readl(dev, IndexRegs.Mailbox[0]));
230         /*
231          *      Clear the synch command doorbell.
232          */
233         rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
234         /*
235          *      Restore interrupt mask
236          */
237         rkt_writeb(dev, MUnit.OIMR, dev->OIMR &= 0xfb);
238         return 0;
239
240 }
241
242 /**
243  *      aac_rkt_interrupt_adapter       -       interrupt adapter
244  *      @dev: Adapter
245  *
246  *      Send an interrupt to the i960 and breakpoint it.
247  */
248
249 static void aac_rkt_interrupt_adapter(struct aac_dev *dev)
250 {
251         u32 ret;
252         rkt_sync_cmd(dev, BREAKPOINT_REQUEST, 0, &ret);
253 }
254
255 /**
256  *      aac_rkt_notify_adapter          -       send an event to the adapter
257  *      @dev: Adapter
258  *      @event: Event to send
259  *
260  *      Notify the i960 that something it probably cares about has
261  *      happened.
262  */
263
264 static void aac_rkt_notify_adapter(struct aac_dev *dev, u32 event)
265 {
266         switch (event) {
267
268         case AdapNormCmdQue:
269                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_1);
270                 break;
271         case HostNormRespNotFull:
272                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_4);
273                 break;
274         case AdapNormRespQue:
275                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_2);
276                 break;
277         case HostNormCmdNotFull:
278                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_3);
279                 break;
280         case HostShutdown:
281 //              rkt_sync_cmd(dev, HOST_CRASHING, 0, 0, 0, 0, &ret);
282                 break;
283         case FastIo:
284                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_6);
285                 break;
286         case AdapPrintfDone:
287                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_5);
288                 break;
289         default:
290                 BUG();
291                 break;
292         }
293 }
294
295 /**
296  *      aac_rkt_start_adapter           -       activate adapter
297  *      @dev:   Adapter
298  *
299  *      Start up processing on an i960 based AAC adapter
300  */
301
302 static void aac_rkt_start_adapter(struct aac_dev *dev)
303 {
304         u32 status;
305         struct aac_init *init;
306
307         init = dev->init;
308         init->HostElapsedSeconds = cpu_to_le32(jiffies/HZ);
309         /*
310          *      Tell the adapter we are back and up and running so it will scan
311          *      its command queues and enable our interrupts
312          */
313         dev->irq_mask = (DoorBellPrintfReady | OUTBOUNDDOORBELL_1 | OUTBOUNDDOORBELL_2 | OUTBOUNDDOORBELL_3 | OUTBOUNDDOORBELL_4);
314         /*
315          *      First clear out all interrupts.  Then enable the one's that we
316          *      can handle.
317          */
318         rkt_writeb(dev, MUnit.OIMR, 0xff);
319         rkt_writel(dev, MUnit.ODR, 0xffffffff);
320 //      rkt_writeb(dev, MUnit.OIMR, ~(u8)OUTBOUND_DOORBELL_INTERRUPT_MASK);
321         rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
322
323         // We can only use a 32 bit address here
324         rkt_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa, &status);
325 }
326
327 /**
328  *      aac_rkt_check_health
329  *      @dev: device to check if healthy
330  *
331  *      Will attempt to determine if the specified adapter is alive and
332  *      capable of handling requests, returning 0 if alive.
333  */
334 static int aac_rkt_check_health(struct aac_dev *dev)
335 {
336         long status = rkt_readl(dev, IndexRegs.Mailbox[7]);
337
338         /*
339          *      Check to see if the board failed any self tests.
340          */
341         if (status & SELF_TEST_FAILED)
342                 return -1;
343         /*
344          *      Check to see if the board panic'd while booting.
345          */
346         if (status & KERNEL_PANIC)
347                 return -2;
348         /*
349          *      Wait for the adapter to be up and running. Wait up to 3 minutes
350          */
351         if (!(status & KERNEL_UP_AND_RUNNING))
352                 return -3;
353         /*
354          *      Everything is OK
355          */
356         return 0;
357 } /* aac_rkt_check_health */
358
359 /**
360  *      aac_rkt_init    -       initialize an i960 based AAC card
361  *      @dev: device to configure
362  *      @devnum: adapter number
363  *
364  *      Allocate and set up resources for the i960 based AAC variants. The 
365  *      device_interface in the commregion will be allocated and linked 
366  *      to the comm region.
367  */
368
369 int aac_rkt_init(struct aac_dev *dev, unsigned long num)
370 {
371         unsigned long start;
372         unsigned long status;
373         int instance;
374         const char * name;
375
376         dev->devnum = num;
377         instance = dev->id;
378         name     = dev->name;
379
380         /*
381          *      Map in the registers from the adapter.
382          */
383         if((dev->regs.rkt = (struct rkt_registers *)ioremap((unsigned long)dev->scsi_host_ptr->base, 8192))==NULL)
384         {       
385                 printk(KERN_WARNING "aacraid: unable to map i960.\n" );
386                 return -1;
387         }
388         /*
389          *      Check to see if the board failed any self tests.
390          */
391         if (rkt_readl(dev, IndexRegs.Mailbox[7]) & SELF_TEST_FAILED) {
392                 printk(KERN_ERR "%s%d: adapter self-test failed.\n", dev->name, instance);
393                 return -1;
394         }
395         /*
396          *      Check to see if the board panic'd while booting.
397          */
398         if (rkt_readl(dev, IndexRegs.Mailbox[7]) & KERNEL_PANIC) {
399                 printk(KERN_ERR "%s%d: adapter kernel panic'd.\n", dev->name, instance);
400                 return -1;
401         }
402         start = jiffies;
403         /*
404          *      Wait for the adapter to be up and running. Wait up to 3 minutes
405          */
406         while (!(rkt_readl(dev, IndexRegs.Mailbox[7]) & KERNEL_UP_AND_RUNNING)) 
407         {
408                 if(time_after(jiffies, start+180*HZ))
409                 {
410                         status = rkt_readl(dev, IndexRegs.Mailbox[7]) >> 16;
411                         printk(KERN_ERR "%s%d: adapter kernel failed to start, init status = %ld.\n", dev->name, instance, status);
412                         return -1;
413                 }
414                 set_current_state(TASK_UNINTERRUPTIBLE);
415                 schedule_timeout(1);
416         }
417         if (request_irq(dev->scsi_host_ptr->irq, aac_rkt_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev)<0) 
418         {
419                 printk(KERN_ERR "%s%d: Interrupt unavailable.\n", name, instance);
420                 return -1;
421         }
422         /*
423          *      Fill in the function dispatch table.
424          */
425         dev->a_ops.adapter_interrupt = aac_rkt_interrupt_adapter;
426         dev->a_ops.adapter_enable_int = aac_rkt_enable_interrupt;
427         dev->a_ops.adapter_disable_int = aac_rkt_disable_interrupt;
428         dev->a_ops.adapter_notify = aac_rkt_notify_adapter;
429         dev->a_ops.adapter_sync_cmd = rkt_sync_cmd;
430         dev->a_ops.adapter_check_health = aac_rkt_check_health;
431
432         if (aac_init_adapter(dev) == NULL)
433                 return -1;
434         /*
435          *      Start any kernel threads needed
436          */
437         dev->thread_pid = kernel_thread((int (*)(void *))aac_command_thread, dev, 0);
438         if(dev->thread_pid < 0)
439         {
440                 printk(KERN_ERR "aacraid: Unable to create rkt thread.\n");
441                 return -1;
442         }       
443         /*
444          *      Tell the adapter that all is configured, and it can start
445          *      accepting requests
446          */
447         aac_rkt_start_adapter(dev);
448         return 0;
449 }