2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc.
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
8 * Copyright (c) 2000-2007 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/pci.h>
35 #include <linux/spinlock.h>
36 #include <linux/blkdev.h>
37 #include <linux/delay.h>
38 #include <linux/completion.h>
39 #include <linux/time.h>
40 #include <linux/interrupt.h>
42 #include <scsi/scsi_host.h>
46 static irqreturn_t aac_rx_intr_producer(int irq, void *dev_id)
48 struct aac_dev *dev = dev_id;
49 unsigned long bellbits;
50 u8 intstat = rx_readb(dev, MUnit.OISR);
53 * Read mask and invert because drawbridge is reversed.
54 * This allows us to only service interrupts that have
56 * Check to see if this is our interrupt. If it isn't just return
58 if (likely(intstat & ~(dev->OIMR))) {
59 bellbits = rx_readl(dev, OutboundDoorbellReg);
60 if (unlikely(bellbits & DoorBellPrintfReady)) {
61 aac_printf(dev, readl (&dev->IndexRegs->Mailbox[5]));
62 rx_writel(dev, MUnit.ODR,DoorBellPrintfReady);
63 rx_writel(dev, InboundDoorbellReg,DoorBellPrintfDone);
65 else if (unlikely(bellbits & DoorBellAdapterNormCmdReady)) {
66 rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdReady);
67 aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
69 else if (likely(bellbits & DoorBellAdapterNormRespReady)) {
70 rx_writel(dev, MUnit.ODR,DoorBellAdapterNormRespReady);
71 aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
73 else if (unlikely(bellbits & DoorBellAdapterNormCmdNotFull)) {
74 rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
76 else if (unlikely(bellbits & DoorBellAdapterNormRespNotFull)) {
77 rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
78 rx_writel(dev, MUnit.ODR, DoorBellAdapterNormRespNotFull);
85 static irqreturn_t aac_rx_intr_message(int irq, void *dev_id)
87 struct aac_dev *dev = dev_id;
88 u32 Index = rx_readl(dev, MUnit.OutboundQueue);
89 if (unlikely(Index == 0xFFFFFFFFL))
90 Index = rx_readl(dev, MUnit.OutboundQueue);
91 if (likely(Index != 0xFFFFFFFFL)) {
93 if (unlikely(aac_intr_normal(dev, Index))) {
94 rx_writel(dev, MUnit.OutboundQueue, Index);
95 rx_writel(dev, MUnit.ODR, DoorBellAdapterNormRespReady);
97 Index = rx_readl(dev, MUnit.OutboundQueue);
98 } while (Index != 0xFFFFFFFFL);
105 * aac_rx_disable_interrupt - Disable interrupts
109 static void aac_rx_disable_interrupt(struct aac_dev *dev)
111 rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
115 * aac_rx_enable_interrupt_producer - Enable interrupts
119 static void aac_rx_enable_interrupt_producer(struct aac_dev *dev)
121 rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
125 * aac_rx_enable_interrupt_message - Enable interrupts
129 static void aac_rx_enable_interrupt_message(struct aac_dev *dev)
131 rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xf7);
135 * rx_sync_cmd - send a command and wait
137 * @command: Command to execute
138 * @p1: first parameter
139 * @ret: adapter status
141 * This routine will send a synchronous command to the adapter and wait
142 * for its completion.
145 static int rx_sync_cmd(struct aac_dev *dev, u32 command,
146 u32 p1, u32 p2, u32 p3, u32 p4, u32 p5, u32 p6,
147 u32 *status, u32 * r1, u32 * r2, u32 * r3, u32 * r4)
152 * Write the command into Mailbox 0
154 writel(command, &dev->IndexRegs->Mailbox[0]);
156 * Write the parameters into Mailboxes 1 - 6
158 writel(p1, &dev->IndexRegs->Mailbox[1]);
159 writel(p2, &dev->IndexRegs->Mailbox[2]);
160 writel(p3, &dev->IndexRegs->Mailbox[3]);
161 writel(p4, &dev->IndexRegs->Mailbox[4]);
163 * Clear the synch command doorbell to start on a clean slate.
165 rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
167 * Disable doorbell interrupts
169 rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
171 * Force the completion of the mask register write before issuing
174 rx_readb (dev, MUnit.OIMR);
176 * Signal that there is a new synch command
178 rx_writel(dev, InboundDoorbellReg, INBOUNDDOORBELL_0);
184 * Wait up to 30 seconds
186 while (time_before(jiffies, start+30*HZ))
188 udelay(5); /* Delay 5 microseconds to let Mon960 get info. */
190 * Mon960 will set doorbell0 bit when it has completed the command.
192 if (rx_readl(dev, OutboundDoorbellReg) & OUTBOUNDDOORBELL_0) {
194 * Clear the doorbell.
196 rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
201 * Yield the processor in case we are slow
205 if (unlikely(ok != 1)) {
207 * Restore interrupt mask even though we timed out
209 aac_adapter_enable_int(dev);
213 * Pull the synch status from Mailbox 0.
216 *status = readl(&dev->IndexRegs->Mailbox[0]);
218 *r1 = readl(&dev->IndexRegs->Mailbox[1]);
220 *r2 = readl(&dev->IndexRegs->Mailbox[2]);
222 *r3 = readl(&dev->IndexRegs->Mailbox[3]);
224 *r4 = readl(&dev->IndexRegs->Mailbox[4]);
226 * Clear the synch command doorbell.
228 rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
230 * Restore interrupt mask
232 aac_adapter_enable_int(dev);
238 * aac_rx_interrupt_adapter - interrupt adapter
241 * Send an interrupt to the i960 and breakpoint it.
244 static void aac_rx_interrupt_adapter(struct aac_dev *dev)
246 rx_sync_cmd(dev, BREAKPOINT_REQUEST, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
250 * aac_rx_notify_adapter - send an event to the adapter
252 * @event: Event to send
254 * Notify the i960 that something it probably cares about has
258 static void aac_rx_notify_adapter(struct aac_dev *dev, u32 event)
263 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_1);
265 case HostNormRespNotFull:
266 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_4);
268 case AdapNormRespQue:
269 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_2);
271 case HostNormCmdNotFull:
272 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_3);
277 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_6);
280 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_5);
289 * aac_rx_start_adapter - activate adapter
292 * Start up processing on an i960 based AAC adapter
295 static void aac_rx_start_adapter(struct aac_dev *dev)
297 struct aac_init *init;
300 init->HostElapsedSeconds = cpu_to_le32(get_seconds());
301 // We can only use a 32 bit address here
302 rx_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa,
303 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
307 * aac_rx_check_health
308 * @dev: device to check if healthy
310 * Will attempt to determine if the specified adapter is alive and
311 * capable of handling requests, returning 0 if alive.
313 static int aac_rx_check_health(struct aac_dev *dev)
315 u32 status = rx_readl(dev, MUnit.OMRx[0]);
318 * Check to see if the board failed any self tests.
320 if (unlikely(status & SELF_TEST_FAILED))
323 * Check to see if the board panic'd.
325 if (unlikely(status & KERNEL_PANIC)) {
331 dma_addr_t paddr, baddr;
334 if (likely((status & 0xFF000000L) == 0xBC000000L))
335 return (status >> 16) & 0xFF;
336 buffer = pci_alloc_consistent(dev->pdev, 512, &baddr);
338 if (unlikely(buffer == NULL))
340 post = pci_alloc_consistent(dev->pdev,
341 sizeof(struct POSTSTATUS), &paddr);
342 if (unlikely(post == NULL)) {
343 pci_free_consistent(dev->pdev, 512, buffer, baddr);
346 memset(buffer, 0, 512);
347 post->Post_Command = cpu_to_le32(COMMAND_POST_RESULTS);
348 post->Post_Address = cpu_to_le32(baddr);
349 rx_writel(dev, MUnit.IMRx[0], paddr);
350 rx_sync_cmd(dev, COMMAND_POST_RESULTS, baddr, 0, 0, 0, 0, 0,
351 NULL, NULL, NULL, NULL, NULL);
352 pci_free_consistent(dev->pdev, sizeof(struct POSTSTATUS),
354 if (likely((buffer[0] == '0') && ((buffer[1] == 'x') || (buffer[1] == 'X')))) {
355 ret = (buffer[2] <= '9') ? (buffer[2] - '0') : (buffer[2] - 'A' + 10);
357 ret += (buffer[3] <= '9') ? (buffer[3] - '0') : (buffer[3] - 'A' + 10);
359 pci_free_consistent(dev->pdev, 512, buffer, baddr);
363 * Wait for the adapter to be up and running.
365 if (unlikely(!(status & KERNEL_UP_AND_RUNNING)))
374 * aac_rx_deliver_producer
377 * Will send a fib, returning 0 if successful.
379 int aac_rx_deliver_producer(struct fib * fib)
381 struct aac_dev *dev = fib->dev;
382 struct aac_queue *q = &dev->queues->queue[AdapNormCmdQueue];
383 unsigned long qflags;
385 unsigned long nointr = 0;
387 spin_lock_irqsave(q->lock, qflags);
388 aac_queue_get( dev, &Index, AdapNormCmdQueue, fib->hw_fib_va, 1, fib, &nointr);
391 *(q->headers.producer) = cpu_to_le32(Index + 1);
392 spin_unlock_irqrestore(q->lock, qflags);
393 if (!(nointr & aac_config.irq_mod))
394 aac_adapter_notify(dev, AdapNormCmdQueue);
400 * aac_rx_deliver_message
403 * Will send a fib, returning 0 if successful.
405 static int aac_rx_deliver_message(struct fib * fib)
407 struct aac_dev *dev = fib->dev;
408 struct aac_queue *q = &dev->queues->queue[AdapNormCmdQueue];
409 unsigned long qflags;
412 volatile void __iomem *device;
414 unsigned long count = 10000000L; /* 50 seconds */
415 spin_lock_irqsave(q->lock, qflags);
417 spin_unlock_irqrestore(q->lock, qflags);
419 Index = rx_readl(dev, MUnit.InboundQueue);
420 if (unlikely(Index == 0xFFFFFFFFL))
421 Index = rx_readl(dev, MUnit.InboundQueue);
422 if (likely(Index != 0xFFFFFFFFL))
425 spin_lock_irqsave(q->lock, qflags);
427 spin_unlock_irqrestore(q->lock, qflags);
432 device = dev->base + Index;
433 addr = fib->hw_fib_pa;
434 writel((u32)(addr & 0xffffffff), device);
435 device += sizeof(u32);
436 writel((u32)(addr >> 32), device);
437 device += sizeof(u32);
438 writel(le16_to_cpu(fib->hw_fib_va->header.Size), device);
439 rx_writel(dev, MUnit.InboundQueue, Index);
445 * @size: mapping resize request
448 static int aac_rx_ioremap(struct aac_dev * dev, u32 size)
451 iounmap(dev->regs.rx);
454 dev->base = dev->regs.rx = ioremap(dev->scsi_host_ptr->base, size);
455 if (dev->base == NULL)
457 dev->IndexRegs = &dev->regs.rx->IndexRegs;
461 static int aac_rx_restart_adapter(struct aac_dev *dev, int bled)
465 if (!(dev->supplement_adapter_info.SupportedOptions2 &
466 AAC_OPTION_MU_RESET) || (bled >= 0) || (bled == -2)) {
468 printk(KERN_ERR "%s%d: adapter kernel panic'd %x.\n",
469 dev->name, dev->id, bled);
471 bled = aac_adapter_sync_cmd(dev, IOP_RESET_ALWAYS,
472 0, 0, 0, 0, 0, 0, &var, NULL, NULL, NULL, NULL);
473 if (!bled && (var != 0x00000001) && (var != 0x3803000F))
476 if (bled && (bled != -ETIMEDOUT))
477 bled = aac_adapter_sync_cmd(dev, IOP_RESET,
478 0, 0, 0, 0, 0, 0, &var, NULL, NULL, NULL, NULL);
480 if (bled && (bled != -ETIMEDOUT))
483 if (bled || (var == 0x3803000F)) { /* USE_OTHER_METHOD */
484 rx_writel(dev, MUnit.reserved2, 3);
485 msleep(5000); /* Delay 5 seconds */
488 if (var != 0x00000001)
490 if (rx_readl(dev, MUnit.OMRx[0]) & KERNEL_PANIC)
492 if (startup_timeout < 300)
493 startup_timeout = 300;
498 * aac_rx_select_comm - Select communications method
500 * @comm: communications method
503 int aac_rx_select_comm(struct aac_dev *dev, int comm)
506 case AAC_COMM_PRODUCER:
507 dev->a_ops.adapter_enable_int = aac_rx_enable_interrupt_producer;
508 dev->a_ops.adapter_intr = aac_rx_intr_producer;
509 dev->a_ops.adapter_deliver = aac_rx_deliver_producer;
511 case AAC_COMM_MESSAGE:
512 dev->a_ops.adapter_enable_int = aac_rx_enable_interrupt_message;
513 dev->a_ops.adapter_intr = aac_rx_intr_message;
514 dev->a_ops.adapter_deliver = aac_rx_deliver_message;
523 * aac_rx_init - initialize an i960 based AAC card
524 * @dev: device to configure
526 * Allocate and set up resources for the i960 based AAC variants. The
527 * device_interface in the commregion will be allocated and linked
528 * to the comm region.
531 int _aac_rx_init(struct aac_dev *dev)
534 unsigned long status;
536 int instance = dev->id;
537 const char * name = dev->name;
539 if (aac_adapter_ioremap(dev, dev->base_size)) {
540 printk(KERN_WARNING "%s: unable to map adapter.\n", name);
544 /* Failure to reset here is an option ... */
545 dev->a_ops.adapter_sync_cmd = rx_sync_cmd;
546 dev->a_ops.adapter_enable_int = aac_rx_disable_interrupt;
547 dev->OIMR = status = rx_readb (dev, MUnit.OIMR);
548 if ((((status & 0x0c) != 0x0c) || aac_reset_devices || reset_devices) &&
549 !aac_rx_restart_adapter(dev, 0))
550 /* Make sure the Hardware FIFO is empty */
551 while ((++restart < 512) &&
552 (rx_readl(dev, MUnit.OutboundQueue) != 0xFFFFFFFFL));
554 * Check to see if the board panic'd while booting.
556 status = rx_readl(dev, MUnit.OMRx[0]);
557 if (status & KERNEL_PANIC) {
558 if (aac_rx_restart_adapter(dev, aac_rx_check_health(dev)))
563 * Check to see if the board failed any self tests.
565 status = rx_readl(dev, MUnit.OMRx[0]);
566 if (status & SELF_TEST_FAILED) {
567 printk(KERN_ERR "%s%d: adapter self-test failed.\n", dev->name, instance);
571 * Check to see if the monitor panic'd while booting.
573 if (status & MONITOR_PANIC) {
574 printk(KERN_ERR "%s%d: adapter monitor panic.\n", dev->name, instance);
579 * Wait for the adapter to be up and running. Wait up to 3 minutes
581 while (!((status = rx_readl(dev, MUnit.OMRx[0])) & KERNEL_UP_AND_RUNNING))
584 (status & (KERNEL_PANIC|SELF_TEST_FAILED|MONITOR_PANIC))) ||
585 time_after(jiffies, start+HZ*startup_timeout)) {
586 printk(KERN_ERR "%s%d: adapter kernel failed to start, init status = %lx.\n",
587 dev->name, instance, status);
591 ((status & (KERNEL_PANIC|SELF_TEST_FAILED|MONITOR_PANIC)) ||
592 time_after(jiffies, start + HZ *
593 ((startup_timeout > 60)
594 ? (startup_timeout - 60)
595 : (startup_timeout / 2))))) {
596 if (likely(!aac_rx_restart_adapter(dev, aac_rx_check_health(dev))))
602 if (restart && aac_commit)
605 * Fill in the common function dispatch table.
607 dev->a_ops.adapter_interrupt = aac_rx_interrupt_adapter;
608 dev->a_ops.adapter_disable_int = aac_rx_disable_interrupt;
609 dev->a_ops.adapter_notify = aac_rx_notify_adapter;
610 dev->a_ops.adapter_sync_cmd = rx_sync_cmd;
611 dev->a_ops.adapter_check_health = aac_rx_check_health;
612 dev->a_ops.adapter_restart = aac_rx_restart_adapter;
615 * First clear out all interrupts. Then enable the one's that we
618 aac_adapter_comm(dev, AAC_COMM_PRODUCER);
619 aac_adapter_disable_int(dev);
620 rx_writel(dev, MUnit.ODR, 0xffffffff);
621 aac_adapter_enable_int(dev);
623 if (aac_init_adapter(dev) == NULL)
625 aac_adapter_comm(dev, dev->comm_interface);
626 dev->msi = aac_msi && !pci_enable_msi(dev->pdev);
627 if (request_irq(dev->pdev->irq, dev->a_ops.adapter_intr,
628 IRQF_SHARED|IRQF_DISABLED, "aacraid", dev) < 0) {
630 pci_disable_msi(dev->pdev);
631 printk(KERN_ERR "%s%d: Interrupt unavailable.\n",
635 aac_adapter_enable_int(dev);
637 * Tell the adapter that all is configured, and it can
638 * start accepting requests
640 aac_rx_start_adapter(dev);
649 int aac_rx_init(struct aac_dev *dev)
652 * Fill in the function dispatch table.
654 dev->a_ops.adapter_ioremap = aac_rx_ioremap;
655 dev->a_ops.adapter_comm = aac_rx_select_comm;
657 return _aac_rx_init(dev);