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