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