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-2010 Adaptec, Inc.
9 * 2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 * Abstract: Hardware miniport for Drawbridge specific hardware functions.
32 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/types.h>
35 #include <linux/pci.h>
36 #include <linux/spinlock.h>
37 #include <linux/blkdev.h>
38 #include <linux/delay.h>
39 #include <linux/completion.h>
40 #include <linux/time.h>
41 #include <linux/interrupt.h>
43 #include <scsi/scsi_host.h>
47 static irqreturn_t
aac_rx_intr_producer(int irq
, void *dev_id
)
49 struct aac_dev
*dev
= dev_id
;
50 unsigned long bellbits
;
51 u8 intstat
= rx_readb(dev
, MUnit
.OISR
);
54 * Read mask and invert because drawbridge is reversed.
55 * This allows us to only service interrupts that have
57 * Check to see if this is our interrupt. If it isn't just return
59 if (likely(intstat
& ~(dev
->OIMR
))) {
60 bellbits
= rx_readl(dev
, OutboundDoorbellReg
);
61 if (unlikely(bellbits
& DoorBellPrintfReady
)) {
62 aac_printf(dev
, readl (&dev
->IndexRegs
->Mailbox
[5]));
63 rx_writel(dev
, MUnit
.ODR
,DoorBellPrintfReady
);
64 rx_writel(dev
, InboundDoorbellReg
,DoorBellPrintfDone
);
66 else if (unlikely(bellbits
& DoorBellAdapterNormCmdReady
)) {
67 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormCmdReady
);
68 aac_command_normal(&dev
->queues
->queue
[HostNormCmdQueue
]);
70 else if (likely(bellbits
& DoorBellAdapterNormRespReady
)) {
71 rx_writel(dev
, MUnit
.ODR
,DoorBellAdapterNormRespReady
);
72 aac_response_normal(&dev
->queues
->queue
[HostNormRespQueue
]);
74 else if (unlikely(bellbits
& DoorBellAdapterNormCmdNotFull
)) {
75 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormCmdNotFull
);
77 else if (unlikely(bellbits
& DoorBellAdapterNormRespNotFull
)) {
78 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormCmdNotFull
);
79 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormRespNotFull
);
86 static irqreturn_t
aac_rx_intr_message(int irq
, void *dev_id
)
88 int isAif
, isFastResponse
, isSpecial
;
89 struct aac_dev
*dev
= dev_id
;
90 u32 Index
= rx_readl(dev
, MUnit
.OutboundQueue
);
91 if (unlikely(Index
== 0xFFFFFFFFL
))
92 Index
= rx_readl(dev
, MUnit
.OutboundQueue
);
93 if (likely(Index
!= 0xFFFFFFFFL
)) {
95 isAif
= isFastResponse
= isSpecial
= 0;
96 if (Index
& 0x00000002L
) {
98 if (Index
== 0xFFFFFFFEL
)
100 Index
&= ~0x00000002L
;
102 if (Index
& 0x00000001L
)
107 if (unlikely(aac_intr_normal(dev
,
109 isFastResponse
, NULL
))) {
115 DoorBellAdapterNormRespReady
);
118 Index
= rx_readl(dev
, MUnit
.OutboundQueue
);
119 } while (Index
!= 0xFFFFFFFFL
);
126 * aac_rx_disable_interrupt - Disable interrupts
130 static void aac_rx_disable_interrupt(struct aac_dev
*dev
)
132 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xff);
136 * aac_rx_enable_interrupt_producer - Enable interrupts
140 static void aac_rx_enable_interrupt_producer(struct aac_dev
*dev
)
142 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xfb);
146 * aac_rx_enable_interrupt_message - Enable interrupts
150 static void aac_rx_enable_interrupt_message(struct aac_dev
*dev
)
152 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xf7);
156 * rx_sync_cmd - send a command and wait
158 * @command: Command to execute
159 * @p1: first parameter
160 * @ret: adapter status
162 * This routine will send a synchronous command to the adapter and wait
163 * for its completion.
166 static int rx_sync_cmd(struct aac_dev
*dev
, u32 command
,
167 u32 p1
, u32 p2
, u32 p3
, u32 p4
, u32 p5
, u32 p6
,
168 u32
*status
, u32
* r1
, u32
* r2
, u32
* r3
, u32
* r4
)
173 * Write the command into Mailbox 0
175 writel(command
, &dev
->IndexRegs
->Mailbox
[0]);
177 * Write the parameters into Mailboxes 1 - 6
179 writel(p1
, &dev
->IndexRegs
->Mailbox
[1]);
180 writel(p2
, &dev
->IndexRegs
->Mailbox
[2]);
181 writel(p3
, &dev
->IndexRegs
->Mailbox
[3]);
182 writel(p4
, &dev
->IndexRegs
->Mailbox
[4]);
184 * Clear the synch command doorbell to start on a clean slate.
186 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
188 * Disable doorbell interrupts
190 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xff);
192 * Force the completion of the mask register write before issuing
195 rx_readb (dev
, MUnit
.OIMR
);
197 * Signal that there is a new synch command
199 rx_writel(dev
, InboundDoorbellReg
, INBOUNDDOORBELL_0
);
205 * Wait up to 30 seconds
207 while (time_before(jiffies
, start
+30*HZ
))
209 udelay(5); /* Delay 5 microseconds to let Mon960 get info. */
211 * Mon960 will set doorbell0 bit when it has completed the command.
213 if (rx_readl(dev
, OutboundDoorbellReg
) & OUTBOUNDDOORBELL_0
) {
215 * Clear the doorbell.
217 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
222 * Yield the processor in case we are slow
226 if (unlikely(ok
!= 1)) {
228 * Restore interrupt mask even though we timed out
230 aac_adapter_enable_int(dev
);
234 * Pull the synch status from Mailbox 0.
237 *status
= readl(&dev
->IndexRegs
->Mailbox
[0]);
239 *r1
= readl(&dev
->IndexRegs
->Mailbox
[1]);
241 *r2
= readl(&dev
->IndexRegs
->Mailbox
[2]);
243 *r3
= readl(&dev
->IndexRegs
->Mailbox
[3]);
245 *r4
= readl(&dev
->IndexRegs
->Mailbox
[4]);
247 * Clear the synch command doorbell.
249 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
251 * Restore interrupt mask
253 aac_adapter_enable_int(dev
);
259 * aac_rx_interrupt_adapter - interrupt adapter
262 * Send an interrupt to the i960 and breakpoint it.
265 static void aac_rx_interrupt_adapter(struct aac_dev
*dev
)
267 rx_sync_cmd(dev
, BREAKPOINT_REQUEST
, 0, 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
271 * aac_rx_notify_adapter - send an event to the adapter
273 * @event: Event to send
275 * Notify the i960 that something it probably cares about has
279 static void aac_rx_notify_adapter(struct aac_dev
*dev
, u32 event
)
284 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_1
);
286 case HostNormRespNotFull
:
287 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_4
);
289 case AdapNormRespQue
:
290 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_2
);
292 case HostNormCmdNotFull
:
293 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_3
);
298 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_6
);
301 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_5
);
310 * aac_rx_start_adapter - activate adapter
313 * Start up processing on an i960 based AAC adapter
316 static void aac_rx_start_adapter(struct aac_dev
*dev
)
318 struct aac_init
*init
;
321 init
->HostElapsedSeconds
= cpu_to_le32(get_seconds());
322 // We can only use a 32 bit address here
323 rx_sync_cmd(dev
, INIT_STRUCT_BASE_ADDRESS
, (u32
)(ulong
)dev
->init_pa
,
324 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
328 * aac_rx_check_health
329 * @dev: device to check if healthy
331 * Will attempt to determine if the specified adapter is alive and
332 * capable of handling requests, returning 0 if alive.
334 static int aac_rx_check_health(struct aac_dev
*dev
)
336 u32 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
339 * Check to see if the board failed any self tests.
341 if (unlikely(status
& SELF_TEST_FAILED
))
344 * Check to see if the board panic'd.
346 if (unlikely(status
& KERNEL_PANIC
)) {
352 dma_addr_t paddr
, baddr
;
355 if (likely((status
& 0xFF000000L
) == 0xBC000000L
))
356 return (status
>> 16) & 0xFF;
357 buffer
= pci_alloc_consistent(dev
->pdev
, 512, &baddr
);
359 if (unlikely(buffer
== NULL
))
361 post
= pci_alloc_consistent(dev
->pdev
,
362 sizeof(struct POSTSTATUS
), &paddr
);
363 if (unlikely(post
== NULL
)) {
364 pci_free_consistent(dev
->pdev
, 512, buffer
, baddr
);
367 memset(buffer
, 0, 512);
368 post
->Post_Command
= cpu_to_le32(COMMAND_POST_RESULTS
);
369 post
->Post_Address
= cpu_to_le32(baddr
);
370 rx_writel(dev
, MUnit
.IMRx
[0], paddr
);
371 rx_sync_cmd(dev
, COMMAND_POST_RESULTS
, baddr
, 0, 0, 0, 0, 0,
372 NULL
, NULL
, NULL
, NULL
, NULL
);
373 pci_free_consistent(dev
->pdev
, sizeof(struct POSTSTATUS
),
375 if (likely((buffer
[0] == '0') && ((buffer
[1] == 'x') || (buffer
[1] == 'X')))) {
376 ret
= (hex_to_bin(buffer
[2]) << 4) +
377 hex_to_bin(buffer
[3]);
379 pci_free_consistent(dev
->pdev
, 512, buffer
, baddr
);
383 * Wait for the adapter to be up and running.
385 if (unlikely(!(status
& KERNEL_UP_AND_RUNNING
)))
394 * aac_rx_deliver_producer
397 * Will send a fib, returning 0 if successful.
399 int aac_rx_deliver_producer(struct fib
* fib
)
401 struct aac_dev
*dev
= fib
->dev
;
402 struct aac_queue
*q
= &dev
->queues
->queue
[AdapNormCmdQueue
];
403 unsigned long qflags
;
405 unsigned long nointr
= 0;
407 spin_lock_irqsave(q
->lock
, qflags
);
408 aac_queue_get( dev
, &Index
, AdapNormCmdQueue
, fib
->hw_fib_va
, 1, fib
, &nointr
);
411 *(q
->headers
.producer
) = cpu_to_le32(Index
+ 1);
412 spin_unlock_irqrestore(q
->lock
, qflags
);
413 if (!(nointr
& aac_config
.irq_mod
))
414 aac_adapter_notify(dev
, AdapNormCmdQueue
);
420 * aac_rx_deliver_message
423 * Will send a fib, returning 0 if successful.
425 static int aac_rx_deliver_message(struct fib
* fib
)
427 struct aac_dev
*dev
= fib
->dev
;
428 struct aac_queue
*q
= &dev
->queues
->queue
[AdapNormCmdQueue
];
429 unsigned long qflags
;
432 volatile void __iomem
*device
;
434 unsigned long count
= 10000000L; /* 50 seconds */
435 spin_lock_irqsave(q
->lock
, qflags
);
437 spin_unlock_irqrestore(q
->lock
, qflags
);
439 Index
= rx_readl(dev
, MUnit
.InboundQueue
);
440 if (unlikely(Index
== 0xFFFFFFFFL
))
441 Index
= rx_readl(dev
, MUnit
.InboundQueue
);
442 if (likely(Index
!= 0xFFFFFFFFL
))
445 spin_lock_irqsave(q
->lock
, qflags
);
447 spin_unlock_irqrestore(q
->lock
, qflags
);
452 device
= dev
->base
+ Index
;
453 addr
= fib
->hw_fib_pa
;
454 writel((u32
)(addr
& 0xffffffff), device
);
455 device
+= sizeof(u32
);
456 writel((u32
)(addr
>> 32), device
);
457 device
+= sizeof(u32
);
458 writel(le16_to_cpu(fib
->hw_fib_va
->header
.Size
), device
);
459 rx_writel(dev
, MUnit
.InboundQueue
, Index
);
465 * @size: mapping resize request
468 static int aac_rx_ioremap(struct aac_dev
* dev
, u32 size
)
471 iounmap(dev
->regs
.rx
);
474 dev
->base
= dev
->regs
.rx
= ioremap(dev
->scsi_host_ptr
->base
, size
);
475 if (dev
->base
== NULL
)
477 dev
->IndexRegs
= &dev
->regs
.rx
->IndexRegs
;
481 static int aac_rx_restart_adapter(struct aac_dev
*dev
, int bled
)
485 if (!(dev
->supplement_adapter_info
.SupportedOptions2
&
486 AAC_OPTION_MU_RESET
) || (bled
>= 0) || (bled
== -2)) {
488 printk(KERN_ERR
"%s%d: adapter kernel panic'd %x.\n",
489 dev
->name
, dev
->id
, bled
);
491 bled
= aac_adapter_sync_cmd(dev
, IOP_RESET_ALWAYS
,
492 0, 0, 0, 0, 0, 0, &var
, NULL
, NULL
, NULL
, NULL
);
493 if (!bled
&& (var
!= 0x00000001) && (var
!= 0x3803000F))
496 if (bled
&& (bled
!= -ETIMEDOUT
))
497 bled
= aac_adapter_sync_cmd(dev
, IOP_RESET
,
498 0, 0, 0, 0, 0, 0, &var
, NULL
, NULL
, NULL
, NULL
);
500 if (bled
&& (bled
!= -ETIMEDOUT
))
503 if (bled
|| (var
== 0x3803000F)) { /* USE_OTHER_METHOD */
504 rx_writel(dev
, MUnit
.reserved2
, 3);
505 msleep(5000); /* Delay 5 seconds */
508 if (var
!= 0x00000001)
510 if (rx_readl(dev
, MUnit
.OMRx
[0]) & KERNEL_PANIC
)
512 if (startup_timeout
< 300)
513 startup_timeout
= 300;
518 * aac_rx_select_comm - Select communications method
520 * @comm: communications method
523 int aac_rx_select_comm(struct aac_dev
*dev
, int comm
)
526 case AAC_COMM_PRODUCER
:
527 dev
->a_ops
.adapter_enable_int
= aac_rx_enable_interrupt_producer
;
528 dev
->a_ops
.adapter_intr
= aac_rx_intr_producer
;
529 dev
->a_ops
.adapter_deliver
= aac_rx_deliver_producer
;
531 case AAC_COMM_MESSAGE
:
532 dev
->a_ops
.adapter_enable_int
= aac_rx_enable_interrupt_message
;
533 dev
->a_ops
.adapter_intr
= aac_rx_intr_message
;
534 dev
->a_ops
.adapter_deliver
= aac_rx_deliver_message
;
543 * aac_rx_init - initialize an i960 based AAC card
544 * @dev: device to configure
546 * Allocate and set up resources for the i960 based AAC variants. The
547 * device_interface in the commregion will be allocated and linked
548 * to the comm region.
551 int _aac_rx_init(struct aac_dev
*dev
)
554 unsigned long status
;
556 int instance
= dev
->id
;
557 const char * name
= dev
->name
;
559 if (aac_adapter_ioremap(dev
, dev
->base_size
)) {
560 printk(KERN_WARNING
"%s: unable to map adapter.\n", name
);
564 /* Failure to reset here is an option ... */
565 dev
->a_ops
.adapter_sync_cmd
= rx_sync_cmd
;
566 dev
->a_ops
.adapter_enable_int
= aac_rx_disable_interrupt
;
567 dev
->OIMR
= status
= rx_readb (dev
, MUnit
.OIMR
);
568 if ((((status
& 0x0c) != 0x0c) || aac_reset_devices
|| reset_devices
) &&
569 !aac_rx_restart_adapter(dev
, 0))
570 /* Make sure the Hardware FIFO is empty */
571 while ((++restart
< 512) &&
572 (rx_readl(dev
, MUnit
.OutboundQueue
) != 0xFFFFFFFFL
));
574 * Check to see if the board panic'd while booting.
576 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
577 if (status
& KERNEL_PANIC
) {
578 if (aac_rx_restart_adapter(dev
, aac_rx_check_health(dev
)))
583 * Check to see if the board failed any self tests.
585 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
586 if (status
& SELF_TEST_FAILED
) {
587 printk(KERN_ERR
"%s%d: adapter self-test failed.\n", dev
->name
, instance
);
591 * Check to see if the monitor panic'd while booting.
593 if (status
& MONITOR_PANIC
) {
594 printk(KERN_ERR
"%s%d: adapter monitor panic.\n", dev
->name
, instance
);
599 * Wait for the adapter to be up and running. Wait up to 3 minutes
601 while (!((status
= rx_readl(dev
, MUnit
.OMRx
[0])) & KERNEL_UP_AND_RUNNING
))
604 (status
& (KERNEL_PANIC
|SELF_TEST_FAILED
|MONITOR_PANIC
))) ||
605 time_after(jiffies
, start
+HZ
*startup_timeout
)) {
606 printk(KERN_ERR
"%s%d: adapter kernel failed to start, init status = %lx.\n",
607 dev
->name
, instance
, status
);
611 ((status
& (KERNEL_PANIC
|SELF_TEST_FAILED
|MONITOR_PANIC
)) ||
612 time_after(jiffies
, start
+ HZ
*
613 ((startup_timeout
> 60)
614 ? (startup_timeout
- 60)
615 : (startup_timeout
/ 2))))) {
616 if (likely(!aac_rx_restart_adapter(dev
, aac_rx_check_health(dev
))))
622 if (restart
&& aac_commit
)
625 * Fill in the common function dispatch table.
627 dev
->a_ops
.adapter_interrupt
= aac_rx_interrupt_adapter
;
628 dev
->a_ops
.adapter_disable_int
= aac_rx_disable_interrupt
;
629 dev
->a_ops
.adapter_notify
= aac_rx_notify_adapter
;
630 dev
->a_ops
.adapter_sync_cmd
= rx_sync_cmd
;
631 dev
->a_ops
.adapter_check_health
= aac_rx_check_health
;
632 dev
->a_ops
.adapter_restart
= aac_rx_restart_adapter
;
635 * First clear out all interrupts. Then enable the one's that we
638 aac_adapter_comm(dev
, AAC_COMM_PRODUCER
);
639 aac_adapter_disable_int(dev
);
640 rx_writel(dev
, MUnit
.ODR
, 0xffffffff);
641 aac_adapter_enable_int(dev
);
643 if (aac_init_adapter(dev
) == NULL
)
645 aac_adapter_comm(dev
, dev
->comm_interface
);
646 dev
->msi
= aac_msi
&& !pci_enable_msi(dev
->pdev
);
647 if (request_irq(dev
->pdev
->irq
, dev
->a_ops
.adapter_intr
,
648 IRQF_SHARED
|IRQF_DISABLED
, "aacraid", dev
) < 0) {
650 pci_disable_msi(dev
->pdev
);
651 printk(KERN_ERR
"%s%d: Interrupt unavailable.\n",
655 dev
->dbg_base
= dev
->scsi_host_ptr
->base
;
656 dev
->dbg_base_mapped
= dev
->base
;
657 dev
->dbg_size
= dev
->base_size
;
659 aac_adapter_enable_int(dev
);
661 * Tell the adapter that all is configured, and it can
662 * start accepting requests
664 aac_rx_start_adapter(dev
);
673 int aac_rx_init(struct aac_dev
*dev
)
676 * Fill in the function dispatch table.
678 dev
->a_ops
.adapter_ioremap
= aac_rx_ioremap
;
679 dev
->a_ops
.adapter_comm
= aac_rx_select_comm
;
681 return _aac_rx_init(dev
);