[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / drivers / scsi / aacraid / rkt.c
blob1b8ed47cfe30be3a7137141ff37da7140a48f336
1 /*
2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
8 * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
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.
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.
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.
24 * Module Name:
25 * rkt.c
27 * Abstract: Hardware miniport for Drawbridge specific hardware functions.
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>
45 #include <scsi/scsi_host.h>
47 #include "aacraid.h"
49 static irqreturn_t aac_rkt_intr(int irq, void *dev_id, struct pt_regs *regs)
51 struct aac_dev *dev = dev_id;
52 unsigned long bellbits;
53 u8 intstat, mask;
54 intstat = rkt_readb(dev, MUnit.OISR);
56 * Read mask and invert because drawbridge is reversed.
57 * This allows us to only service interrupts that have
58 * been enabled.
60 mask = ~(dev->OIMR);
61 /* Check to see if this is our interrupt. If it isn't just return */
62 if (intstat & mask)
64 bellbits = rkt_readl(dev, OutboundDoorbellReg);
65 if (bellbits & DoorBellPrintfReady) {
66 aac_printf(dev, rkt_readl(dev, IndexRegs.Mailbox[5]));
67 rkt_writel(dev, MUnit.ODR,DoorBellPrintfReady);
68 rkt_writel(dev, InboundDoorbellReg,DoorBellPrintfDone);
70 else if (bellbits & DoorBellAdapterNormCmdReady) {
71 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdReady);
72 aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
74 else if (bellbits & DoorBellAdapterNormRespReady) {
75 aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
76 rkt_writel(dev, MUnit.ODR,DoorBellAdapterNormRespReady);
78 else if (bellbits & DoorBellAdapterNormCmdNotFull) {
79 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
81 else if (bellbits & DoorBellAdapterNormRespNotFull) {
82 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
83 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormRespNotFull);
85 return IRQ_HANDLED;
87 return IRQ_NONE;
90 /**
91 * rkt_sync_cmd - send a command and wait
92 * @dev: Adapter
93 * @command: Command to execute
94 * @p1: first parameter
95 * @ret: adapter status
97 * This routine will send a synchronous command to the adapter and wait
98 * for its completion.
101 static int rkt_sync_cmd(struct aac_dev *dev, u32 command, u32 p1, u32 *status)
103 unsigned long start;
104 int ok;
106 * Write the command into Mailbox 0
108 rkt_writel(dev, InboundMailbox0, command);
110 * Write the parameters into Mailboxes 1 - 4
112 rkt_writel(dev, InboundMailbox1, p1);
113 rkt_writel(dev, InboundMailbox2, 0);
114 rkt_writel(dev, InboundMailbox3, 0);
115 rkt_writel(dev, InboundMailbox4, 0);
117 * Clear the synch command doorbell to start on a clean slate.
119 rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
121 * Disable doorbell interrupts
123 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
125 * Force the completion of the mask register write before issuing
126 * the interrupt.
128 rkt_readb (dev, MUnit.OIMR);
130 * Signal that there is a new synch command
132 rkt_writel(dev, InboundDoorbellReg, INBOUNDDOORBELL_0);
134 ok = 0;
135 start = jiffies;
138 * Wait up to 30 seconds
140 while (time_before(jiffies, start+30*HZ))
142 udelay(5); /* Delay 5 microseconds to let Mon960 get info. */
144 * Mon960 will set doorbell0 bit when it has completed the command.
146 if (rkt_readl(dev, OutboundDoorbellReg) & OUTBOUNDDOORBELL_0) {
148 * Clear the doorbell.
150 rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
151 ok = 1;
152 break;
155 * Yield the processor in case we are slow
157 set_current_state(TASK_UNINTERRUPTIBLE);
158 schedule_timeout(1);
160 if (ok != 1) {
162 * Restore interrupt mask even though we timed out
164 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
165 return -ETIMEDOUT;
168 * Pull the synch status from Mailbox 0.
170 if (status)
171 *status = rkt_readl(dev, IndexRegs.Mailbox[0]);
173 * Clear the synch command doorbell.
175 rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
177 * Restore interrupt mask
179 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
180 return 0;
185 * aac_rkt_interrupt_adapter - interrupt adapter
186 * @dev: Adapter
188 * Send an interrupt to the i960 and breakpoint it.
191 static void aac_rkt_interrupt_adapter(struct aac_dev *dev)
193 u32 ret;
194 rkt_sync_cmd(dev, BREAKPOINT_REQUEST, 0, &ret);
198 * aac_rkt_notify_adapter - send an event to the adapter
199 * @dev: Adapter
200 * @event: Event to send
202 * Notify the i960 that something it probably cares about has
203 * happened.
206 static void aac_rkt_notify_adapter(struct aac_dev *dev, u32 event)
208 switch (event) {
210 case AdapNormCmdQue:
211 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_1);
212 break;
213 case HostNormRespNotFull:
214 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_4);
215 break;
216 case AdapNormRespQue:
217 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_2);
218 break;
219 case HostNormCmdNotFull:
220 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_3);
221 break;
222 case HostShutdown:
223 // rkt_sync_cmd(dev, HOST_CRASHING, 0, 0, 0, 0, &ret);
224 break;
225 case FastIo:
226 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_6);
227 break;
228 case AdapPrintfDone:
229 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_5);
230 break;
231 default:
232 BUG();
233 break;
238 * aac_rkt_start_adapter - activate adapter
239 * @dev: Adapter
241 * Start up processing on an i960 based AAC adapter
244 static void aac_rkt_start_adapter(struct aac_dev *dev)
246 u32 status;
247 struct aac_init *init;
249 init = dev->init;
250 init->HostElapsedSeconds = cpu_to_le32(get_seconds());
252 * Tell the adapter we are back and up and running so it will scan
253 * its command queues and enable our interrupts
255 dev->irq_mask = (DoorBellPrintfReady | OUTBOUNDDOORBELL_1 | OUTBOUNDDOORBELL_2 | OUTBOUNDDOORBELL_3 | OUTBOUNDDOORBELL_4);
257 * First clear out all interrupts. Then enable the one's that we
258 * can handle.
260 rkt_writeb(dev, MUnit.OIMR, 0xff);
261 rkt_writel(dev, MUnit.ODR, 0xffffffff);
262 // rkt_writeb(dev, MUnit.OIMR, ~(u8)OUTBOUND_DOORBELL_INTERRUPT_MASK);
263 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
265 // We can only use a 32 bit address here
266 rkt_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa, &status);
270 * aac_rkt_check_health
271 * @dev: device to check if healthy
273 * Will attempt to determine if the specified adapter is alive and
274 * capable of handling requests, returning 0 if alive.
276 static int aac_rkt_check_health(struct aac_dev *dev)
278 u32 status = rkt_readl(dev, MUnit.OMRx[0]);
281 * Check to see if the board failed any self tests.
283 if (status & SELF_TEST_FAILED)
284 return -1;
286 * Check to see if the board panic'd.
288 if (status & KERNEL_PANIC) {
289 char * buffer;
290 struct POSTSTATUS {
291 u32 Post_Command;
292 u32 Post_Address;
293 } * post;
294 dma_addr_t paddr, baddr;
295 int ret;
297 if ((status & 0xFF000000L) == 0xBC000000L)
298 return (status >> 16) & 0xFF;
299 buffer = pci_alloc_consistent(dev->pdev, 512, &baddr);
300 ret = -2;
301 if (buffer == NULL)
302 return ret;
303 post = pci_alloc_consistent(dev->pdev,
304 sizeof(struct POSTSTATUS), &paddr);
305 if (post == NULL) {
306 pci_free_consistent(dev->pdev, 512, buffer, baddr);
307 return ret;
309 memset(buffer, 0, 512);
310 post->Post_Command = cpu_to_le32(COMMAND_POST_RESULTS);
311 post->Post_Address = cpu_to_le32(baddr);
312 rkt_writel(dev, MUnit.IMRx[0], paddr);
313 rkt_sync_cmd(dev, COMMAND_POST_RESULTS, baddr, &status);
314 pci_free_consistent(dev->pdev, sizeof(struct POSTSTATUS),
315 post, paddr);
316 if ((buffer[0] == '0') && (buffer[1] == 'x')) {
317 ret = (buffer[2] <= '9') ? (buffer[2] - '0') : (buffer[2] - 'A' + 10);
318 ret <<= 4;
319 ret += (buffer[3] <= '9') ? (buffer[3] - '0') : (buffer[3] - 'A' + 10);
321 pci_free_consistent(dev->pdev, 512, buffer, baddr);
322 return ret;
325 * Wait for the adapter to be up and running.
327 if (!(status & KERNEL_UP_AND_RUNNING))
328 return -3;
330 * Everything is OK
332 return 0;
336 * aac_rkt_init - initialize an i960 based AAC card
337 * @dev: device to configure
339 * Allocate and set up resources for the i960 based AAC variants. The
340 * device_interface in the commregion will be allocated and linked
341 * to the comm region.
344 int aac_rkt_init(struct aac_dev *dev)
346 unsigned long start;
347 unsigned long status;
348 int instance;
349 const char * name;
351 instance = dev->id;
352 name = dev->name;
355 * Map in the registers from the adapter.
357 if((dev->regs.rkt = ioremap((unsigned long)dev->scsi_host_ptr->base, 8192))==NULL)
359 printk(KERN_WARNING "aacraid: unable to map i960.\n" );
360 goto error_iounmap;
363 * Check to see if the board failed any self tests.
365 if (rkt_readl(dev, MUnit.OMRx[0]) & SELF_TEST_FAILED) {
366 printk(KERN_ERR "%s%d: adapter self-test failed.\n", dev->name, instance);
367 goto error_iounmap;
370 * Check to see if the monitor panic'd while booting.
372 if (rkt_readl(dev, MUnit.OMRx[0]) & MONITOR_PANIC) {
373 printk(KERN_ERR "%s%d: adapter monitor panic.\n", dev->name, instance);
374 goto error_iounmap;
377 * Check to see if the board panic'd while booting.
379 if (rkt_readl(dev, MUnit.OMRx[0]) & KERNEL_PANIC) {
380 printk(KERN_ERR "%s%d: adapter kernel panic'd.\n", dev->name, instance);
381 goto error_iounmap;
383 start = jiffies;
385 * Wait for the adapter to be up and running. Wait up to 3 minutes
387 while (!(rkt_readl(dev, MUnit.OMRx[0]) & KERNEL_UP_AND_RUNNING))
389 if(time_after(jiffies, start+180*HZ))
391 status = rkt_readl(dev, MUnit.OMRx[0]);
392 printk(KERN_ERR "%s%d: adapter kernel failed to start, init status = %lx.\n",
393 dev->name, instance, status);
394 goto error_iounmap;
396 set_current_state(TASK_UNINTERRUPTIBLE);
397 schedule_timeout(1);
399 if (request_irq(dev->scsi_host_ptr->irq, aac_rkt_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev)<0)
401 printk(KERN_ERR "%s%d: Interrupt unavailable.\n", name, instance);
402 goto error_iounmap;
405 * Fill in the function dispatch table.
407 dev->a_ops.adapter_interrupt = aac_rkt_interrupt_adapter;
408 dev->a_ops.adapter_notify = aac_rkt_notify_adapter;
409 dev->a_ops.adapter_sync_cmd = rkt_sync_cmd;
410 dev->a_ops.adapter_check_health = aac_rkt_check_health;
412 if (aac_init_adapter(dev) == NULL)
413 goto error_irq;
415 * Start any kernel threads needed
417 dev->thread_pid = kernel_thread((int (*)(void *))aac_command_thread, dev, 0);
418 if(dev->thread_pid < 0)
420 printk(KERN_ERR "aacraid: Unable to create rkt thread.\n");
421 goto error_kfree;
424 * Tell the adapter that all is configured, and it can start
425 * accepting requests
427 aac_rkt_start_adapter(dev);
428 return 0;
430 error_kfree:
431 kfree(dev->queues);
433 error_irq:
434 free_irq(dev->scsi_host_ptr->irq, (void *)dev);
436 error_iounmap:
437 iounmap(dev->regs.rkt);
439 return -1;