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)
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.
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>
49 static irqreturn_t aac_rx_intr_producer(int irq, void *dev_id)
51 struct aac_dev *dev = dev_id;
52 unsigned long bellbits;
53 u8 intstat = rx_readb(dev, MUnit.OISR);
56 * Read mask and invert because drawbridge is reversed.
57 * This allows us to only service interrupts that have
59 * Check to see if this is our interrupt. If it isn't just return
61 if (intstat & ~(dev->OIMR)) {
62 bellbits = rx_readl(dev, OutboundDoorbellReg);
63 if (bellbits & DoorBellPrintfReady) {
64 aac_printf(dev, readl (&dev->IndexRegs->Mailbox[5]));
65 rx_writel(dev, MUnit.ODR,DoorBellPrintfReady);
66 rx_writel(dev, InboundDoorbellReg,DoorBellPrintfDone);
68 else if (bellbits & DoorBellAdapterNormCmdReady) {
69 rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdReady);
70 aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
72 else if (bellbits & DoorBellAdapterNormRespReady) {
73 rx_writel(dev, MUnit.ODR,DoorBellAdapterNormRespReady);
74 aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
76 else if (bellbits & DoorBellAdapterNormCmdNotFull) {
77 rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
79 else if (bellbits & DoorBellAdapterNormRespNotFull) {
80 rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
81 rx_writel(dev, MUnit.ODR, DoorBellAdapterNormRespNotFull);
88 static irqreturn_t aac_rx_intr_message(int irq, void *dev_id)
90 struct aac_dev *dev = dev_id;
91 u32 Index = rx_readl(dev, MUnit.OutboundQueue);
92 if (Index == 0xFFFFFFFFL)
93 Index = rx_readl(dev, MUnit.OutboundQueue);
94 if (Index != 0xFFFFFFFFL) {
96 if (aac_intr_normal(dev, Index)) {
97 rx_writel(dev, MUnit.OutboundQueue, Index);
98 rx_writel(dev, MUnit.ODR, DoorBellAdapterNormRespReady);
100 Index = rx_readl(dev, MUnit.OutboundQueue);
101 } while (Index != 0xFFFFFFFFL);
108 * aac_rx_disable_interrupt - Disable interrupts
112 static void aac_rx_disable_interrupt(struct aac_dev *dev)
114 rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
118 * aac_rx_enable_interrupt_producer - Enable interrupts
122 static void aac_rx_enable_interrupt_producer(struct aac_dev *dev)
124 rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
128 * aac_rx_enable_interrupt_message - Enable interrupts
132 static void aac_rx_enable_interrupt_message(struct aac_dev *dev)
134 rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xf7);
138 * rx_sync_cmd - send a command and wait
140 * @command: Command to execute
141 * @p1: first parameter
142 * @ret: adapter status
144 * This routine will send a synchronous command to the adapter and wait
145 * for its completion.
148 static int rx_sync_cmd(struct aac_dev *dev, u32 command,
149 u32 p1, u32 p2, u32 p3, u32 p4, u32 p5, u32 p6,
150 u32 *status, u32 * r1, u32 * r2, u32 * r3, u32 * r4)
155 * Write the command into Mailbox 0
157 writel(command, &dev->IndexRegs->Mailbox[0]);
159 * Write the parameters into Mailboxes 1 - 6
161 writel(p1, &dev->IndexRegs->Mailbox[1]);
162 writel(p2, &dev->IndexRegs->Mailbox[2]);
163 writel(p3, &dev->IndexRegs->Mailbox[3]);
164 writel(p4, &dev->IndexRegs->Mailbox[4]);
166 * Clear the synch command doorbell to start on a clean slate.
168 rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
170 * Disable doorbell interrupts
172 rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
174 * Force the completion of the mask register write before issuing
177 rx_readb (dev, MUnit.OIMR);
179 * Signal that there is a new synch command
181 rx_writel(dev, InboundDoorbellReg, INBOUNDDOORBELL_0);
187 * Wait up to 30 seconds
189 while (time_before(jiffies, start+30*HZ))
191 udelay(5); /* Delay 5 microseconds to let Mon960 get info. */
193 * Mon960 will set doorbell0 bit when it has completed the command.
195 if (rx_readl(dev, OutboundDoorbellReg) & OUTBOUNDDOORBELL_0) {
197 * Clear the doorbell.
199 rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
204 * Yield the processor in case we are slow
210 * Restore interrupt mask even though we timed out
212 aac_adapter_enable_int(dev);
216 * Pull the synch status from Mailbox 0.
219 *status = readl(&dev->IndexRegs->Mailbox[0]);
221 *r1 = readl(&dev->IndexRegs->Mailbox[1]);
223 *r2 = readl(&dev->IndexRegs->Mailbox[2]);
225 *r3 = readl(&dev->IndexRegs->Mailbox[3]);
227 *r4 = readl(&dev->IndexRegs->Mailbox[4]);
229 * Clear the synch command doorbell.
231 rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
233 * Restore interrupt mask
235 aac_adapter_enable_int(dev);
241 * aac_rx_interrupt_adapter - interrupt adapter
244 * Send an interrupt to the i960 and breakpoint it.
247 static void aac_rx_interrupt_adapter(struct aac_dev *dev)
249 rx_sync_cmd(dev, BREAKPOINT_REQUEST, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
253 * aac_rx_notify_adapter - send an event to the adapter
255 * @event: Event to send
257 * Notify the i960 that something it probably cares about has
261 static void aac_rx_notify_adapter(struct aac_dev *dev, u32 event)
266 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_1);
268 case HostNormRespNotFull:
269 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_4);
271 case AdapNormRespQue:
272 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_2);
274 case HostNormCmdNotFull:
275 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_3);
280 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_6);
283 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_5);
292 * aac_rx_start_adapter - activate adapter
295 * Start up processing on an i960 based AAC adapter
298 void aac_rx_start_adapter(struct aac_dev *dev)
300 struct aac_init *init;
303 init->HostElapsedSeconds = cpu_to_le32(get_seconds());
304 // We can only use a 32 bit address here
305 rx_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa,
306 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
310 * aac_rx_check_health
311 * @dev: device to check if healthy
313 * Will attempt to determine if the specified adapter is alive and
314 * capable of handling requests, returning 0 if alive.
316 static int aac_rx_check_health(struct aac_dev *dev)
318 u32 status = rx_readl(dev, MUnit.OMRx[0]);
321 * Check to see if the board failed any self tests.
323 if (status & SELF_TEST_FAILED)
326 * Check to see if the board panic'd.
328 if (status & KERNEL_PANIC) {
334 dma_addr_t paddr, baddr;
337 if ((status & 0xFF000000L) == 0xBC000000L)
338 return (status >> 16) & 0xFF;
339 buffer = pci_alloc_consistent(dev->pdev, 512, &baddr);
343 post = pci_alloc_consistent(dev->pdev,
344 sizeof(struct POSTSTATUS), &paddr);
346 pci_free_consistent(dev->pdev, 512, buffer, baddr);
349 memset(buffer, 0, 512);
350 post->Post_Command = cpu_to_le32(COMMAND_POST_RESULTS);
351 post->Post_Address = cpu_to_le32(baddr);
352 rx_writel(dev, MUnit.IMRx[0], paddr);
353 rx_sync_cmd(dev, COMMAND_POST_RESULTS, baddr, 0, 0, 0, 0, 0,
354 NULL, NULL, NULL, NULL, NULL);
355 pci_free_consistent(dev->pdev, sizeof(struct POSTSTATUS),
357 if ((buffer[0] == '0') && ((buffer[1] == 'x') || (buffer[1] == 'X'))) {
358 ret = (buffer[2] <= '9') ? (buffer[2] - '0') : (buffer[2] - 'A' + 10);
360 ret += (buffer[3] <= '9') ? (buffer[3] - '0') : (buffer[3] - 'A' + 10);
362 pci_free_consistent(dev->pdev, 512, buffer, baddr);
366 * Wait for the adapter to be up and running.
368 if (!(status & KERNEL_UP_AND_RUNNING))
377 * aac_rx_deliver_producer
380 * Will send a fib, returning 0 if successful.
382 static int aac_rx_deliver_producer(struct fib * fib)
384 struct aac_dev *dev = fib->dev;
385 struct aac_queue *q = &dev->queues->queue[AdapNormCmdQueue];
386 unsigned long qflags;
388 unsigned long nointr = 0;
390 spin_lock_irqsave(q->lock, qflags);
391 aac_queue_get( dev, &Index, AdapNormCmdQueue, fib->hw_fib, 1, fib, &nointr);
394 *(q->headers.producer) = cpu_to_le32(Index + 1);
395 spin_unlock_irqrestore(q->lock, qflags);
396 if (!(nointr & aac_config.irq_mod))
397 aac_adapter_notify(dev, AdapNormCmdQueue);
403 * aac_rx_deliver_message
406 * Will send a fib, returning 0 if successful.
408 static int aac_rx_deliver_message(struct fib * fib)
410 struct aac_dev *dev = fib->dev;
411 struct aac_queue *q = &dev->queues->queue[AdapNormCmdQueue];
412 unsigned long qflags;
415 volatile void __iomem *device;
417 unsigned long count = 10000000L; /* 50 seconds */
418 spin_lock_irqsave(q->lock, qflags);
420 spin_unlock_irqrestore(q->lock, qflags);
422 Index = rx_readl(dev, MUnit.InboundQueue);
423 if (Index == 0xFFFFFFFFL)
424 Index = rx_readl(dev, MUnit.InboundQueue);
425 if (Index != 0xFFFFFFFFL)
428 spin_lock_irqsave(q->lock, qflags);
430 spin_unlock_irqrestore(q->lock, qflags);
435 device = dev->base + Index;
436 addr = fib->hw_fib_pa;
437 writel((u32)(addr & 0xffffffff), device);
438 device += sizeof(u32);
439 writel((u32)(addr >> 32), device);
440 device += sizeof(u32);
441 writel(le16_to_cpu(fib->hw_fib->header.Size), device);
442 rx_writel(dev, MUnit.InboundQueue, Index);
448 * @size: mapping resize request
451 static int aac_rx_ioremap(struct aac_dev * dev, u32 size)
454 iounmap(dev->regs.rx);
457 dev->base = dev->regs.rx = ioremap(dev->scsi_host_ptr->base, size);
458 if (dev->base == NULL)
460 dev->IndexRegs = &dev->regs.rx->IndexRegs;
464 static int aac_rx_restart_adapter(struct aac_dev *dev)
468 printk(KERN_ERR "%s%d: adapter kernel panic'd.\n",
471 if (aac_rx_check_health(dev) <= 0)
473 if (rx_sync_cmd(dev, IOP_RESET, 0, 0, 0, 0, 0, 0,
474 &var, NULL, NULL, NULL, NULL))
476 if (var != 0x00000001)
478 if (rx_readl(dev, MUnit.OMRx[0]) & KERNEL_PANIC)
484 * aac_rx_select_comm - Select communications method
486 * @comm: communications method
489 int aac_rx_select_comm(struct aac_dev *dev, int comm)
492 case AAC_COMM_PRODUCER:
493 dev->a_ops.adapter_enable_int = aac_rx_enable_interrupt_producer;
494 dev->a_ops.adapter_intr = aac_rx_intr_producer;
495 dev->a_ops.adapter_deliver = aac_rx_deliver_producer;
497 case AAC_COMM_MESSAGE:
498 dev->a_ops.adapter_enable_int = aac_rx_enable_interrupt_message;
499 dev->a_ops.adapter_intr = aac_rx_intr_message;
500 dev->a_ops.adapter_deliver = aac_rx_deliver_message;
509 * aac_rx_init - initialize an i960 based AAC card
510 * @dev: device to configure
512 * Allocate and set up resources for the i960 based AAC variants. The
513 * device_interface in the commregion will be allocated and linked
514 * to the comm region.
517 int _aac_rx_init(struct aac_dev *dev)
520 unsigned long status;
527 if (aac_adapter_ioremap(dev, dev->base_size)) {
528 printk(KERN_WARNING "%s: unable to map adapter.\n", name);
533 * Check to see if the board panic'd while booting.
535 status = rx_readl(dev, MUnit.OMRx[0]);
536 if (status & KERNEL_PANIC)
537 if (aac_rx_restart_adapter(dev))
540 * Check to see if the board failed any self tests.
542 status = rx_readl(dev, MUnit.OMRx[0]);
543 if (status & SELF_TEST_FAILED) {
544 printk(KERN_ERR "%s%d: adapter self-test failed.\n", dev->name, instance);
548 * Check to see if the monitor panic'd while booting.
550 if (status & MONITOR_PANIC) {
551 printk(KERN_ERR "%s%d: adapter monitor panic.\n", dev->name, instance);
556 * Wait for the adapter to be up and running. Wait up to 3 minutes
558 while (!((status = rx_readl(dev, MUnit.OMRx[0])) & KERNEL_UP_AND_RUNNING))
560 if(time_after(jiffies, start+startup_timeout*HZ))
562 printk(KERN_ERR "%s%d: adapter kernel failed to start, init status = %lx.\n",
563 dev->name, instance, status);
569 * Fill in the common function dispatch table.
571 dev->a_ops.adapter_interrupt = aac_rx_interrupt_adapter;
572 dev->a_ops.adapter_disable_int = aac_rx_disable_interrupt;
573 dev->a_ops.adapter_notify = aac_rx_notify_adapter;
574 dev->a_ops.adapter_sync_cmd = rx_sync_cmd;
575 dev->a_ops.adapter_check_health = aac_rx_check_health;
578 * First clear out all interrupts. Then enable the one's that we
581 aac_adapter_comm(dev, AAC_COMM_PRODUCER);
582 aac_adapter_disable_int(dev);
583 rx_writel(dev, MUnit.ODR, 0xffffffff);
584 aac_adapter_enable_int(dev);
586 if (aac_init_adapter(dev) == NULL)
588 aac_adapter_comm(dev, dev->comm_interface);
589 if (request_irq(dev->scsi_host_ptr->irq, dev->a_ops.adapter_intr,
590 IRQF_SHARED|IRQF_DISABLED, "aacraid", dev) < 0) {
591 printk(KERN_ERR "%s%d: Interrupt unavailable.\n",
595 aac_adapter_enable_int(dev);
597 * Tell the adapter that all is configured, and it can
598 * start accepting requests
600 aac_rx_start_adapter(dev);
609 int aac_rx_init(struct aac_dev *dev)
612 * Fill in the function dispatch table.
614 dev->a_ops.adapter_ioremap = aac_rx_ioremap;
615 dev->a_ops.adapter_comm = aac_rx_select_comm;
617 return _aac_rx_init(dev);