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-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10 * 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Abstract: Hardware miniport for Drawbridge specific hardware functions.
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/spinlock.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>
44 #include <scsi/scsi_host.h>
48 static irqreturn_t
aac_rx_intr_producer(int irq
, void *dev_id
)
50 struct aac_dev
*dev
= dev_id
;
51 unsigned long bellbits
;
52 u8 intstat
= rx_readb(dev
, MUnit
.OISR
);
55 * Read mask and invert because drawbridge is reversed.
56 * This allows us to only service interrupts that have
58 * Check to see if this is our interrupt. If it isn't just return
60 if (likely(intstat
& ~(dev
->OIMR
))) {
61 bellbits
= rx_readl(dev
, OutboundDoorbellReg
);
62 if (unlikely(bellbits
& DoorBellPrintfReady
)) {
63 aac_printf(dev
, readl (&dev
->IndexRegs
->Mailbox
[5]));
64 rx_writel(dev
, MUnit
.ODR
,DoorBellPrintfReady
);
65 rx_writel(dev
, InboundDoorbellReg
,DoorBellPrintfDone
);
67 else if (unlikely(bellbits
& DoorBellAdapterNormCmdReady
)) {
68 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormCmdReady
);
69 aac_command_normal(&dev
->queues
->queue
[HostNormCmdQueue
]);
71 else if (likely(bellbits
& DoorBellAdapterNormRespReady
)) {
72 rx_writel(dev
, MUnit
.ODR
,DoorBellAdapterNormRespReady
);
73 aac_response_normal(&dev
->queues
->queue
[HostNormRespQueue
]);
75 else if (unlikely(bellbits
& DoorBellAdapterNormCmdNotFull
)) {
76 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormCmdNotFull
);
78 else if (unlikely(bellbits
& DoorBellAdapterNormRespNotFull
)) {
79 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormCmdNotFull
);
80 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormRespNotFull
);
87 static irqreturn_t
aac_rx_intr_message(int irq
, void *dev_id
)
89 int isAif
, isFastResponse
, isSpecial
;
90 struct aac_dev
*dev
= dev_id
;
91 u32 Index
= rx_readl(dev
, MUnit
.OutboundQueue
);
92 if (unlikely(Index
== 0xFFFFFFFFL
))
93 Index
= rx_readl(dev
, MUnit
.OutboundQueue
);
94 if (likely(Index
!= 0xFFFFFFFFL
)) {
96 isAif
= isFastResponse
= isSpecial
= 0;
97 if (Index
& 0x00000002L
) {
99 if (Index
== 0xFFFFFFFEL
)
101 Index
&= ~0x00000002L
;
103 if (Index
& 0x00000001L
)
108 if (unlikely(aac_intr_normal(dev
,
110 isFastResponse
, NULL
))) {
116 DoorBellAdapterNormRespReady
);
119 Index
= rx_readl(dev
, MUnit
.OutboundQueue
);
120 } while (Index
!= 0xFFFFFFFFL
);
127 * aac_rx_disable_interrupt - Disable interrupts
131 static void aac_rx_disable_interrupt(struct aac_dev
*dev
)
133 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xff);
137 * aac_rx_enable_interrupt_producer - Enable interrupts
141 static void aac_rx_enable_interrupt_producer(struct aac_dev
*dev
)
143 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xfb);
147 * aac_rx_enable_interrupt_message - Enable interrupts
151 static void aac_rx_enable_interrupt_message(struct aac_dev
*dev
)
153 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xf7);
157 * rx_sync_cmd - send a command and wait
159 * @command: Command to execute
160 * @p1: first parameter
161 * @ret: adapter status
163 * This routine will send a synchronous command to the adapter and wait
164 * for its completion.
167 static int rx_sync_cmd(struct aac_dev
*dev
, u32 command
,
168 u32 p1
, u32 p2
, u32 p3
, u32 p4
, u32 p5
, u32 p6
,
169 u32
*status
, u32
* r1
, u32
* r2
, u32
* r3
, u32
* r4
)
174 * Write the command into Mailbox 0
176 writel(command
, &dev
->IndexRegs
->Mailbox
[0]);
178 * Write the parameters into Mailboxes 1 - 6
180 writel(p1
, &dev
->IndexRegs
->Mailbox
[1]);
181 writel(p2
, &dev
->IndexRegs
->Mailbox
[2]);
182 writel(p3
, &dev
->IndexRegs
->Mailbox
[3]);
183 writel(p4
, &dev
->IndexRegs
->Mailbox
[4]);
185 * Clear the synch command doorbell to start on a clean slate.
187 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
189 * Disable doorbell interrupts
191 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xff);
193 * Force the completion of the mask register write before issuing
196 rx_readb (dev
, MUnit
.OIMR
);
198 * Signal that there is a new synch command
200 rx_writel(dev
, InboundDoorbellReg
, INBOUNDDOORBELL_0
);
206 * Wait up to 30 seconds
208 while (time_before(jiffies
, start
+30*HZ
))
210 udelay(5); /* Delay 5 microseconds to let Mon960 get info. */
212 * Mon960 will set doorbell0 bit when it has completed the command.
214 if (rx_readl(dev
, OutboundDoorbellReg
) & OUTBOUNDDOORBELL_0
) {
216 * Clear the doorbell.
218 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
223 * Yield the processor in case we are slow
227 if (unlikely(ok
!= 1)) {
229 * Restore interrupt mask even though we timed out
231 aac_adapter_enable_int(dev
);
235 * Pull the synch status from Mailbox 0.
238 *status
= readl(&dev
->IndexRegs
->Mailbox
[0]);
240 *r1
= readl(&dev
->IndexRegs
->Mailbox
[1]);
242 *r2
= readl(&dev
->IndexRegs
->Mailbox
[2]);
244 *r3
= readl(&dev
->IndexRegs
->Mailbox
[3]);
246 *r4
= readl(&dev
->IndexRegs
->Mailbox
[4]);
248 * Clear the synch command doorbell.
250 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
252 * Restore interrupt mask
254 aac_adapter_enable_int(dev
);
260 * aac_rx_interrupt_adapter - interrupt adapter
263 * Send an interrupt to the i960 and breakpoint it.
266 static void aac_rx_interrupt_adapter(struct aac_dev
*dev
)
268 rx_sync_cmd(dev
, BREAKPOINT_REQUEST
, 0, 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
272 * aac_rx_notify_adapter - send an event to the adapter
274 * @event: Event to send
276 * Notify the i960 that something it probably cares about has
280 static void aac_rx_notify_adapter(struct aac_dev
*dev
, u32 event
)
285 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_1
);
287 case HostNormRespNotFull
:
288 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_4
);
290 case AdapNormRespQue
:
291 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_2
);
293 case HostNormCmdNotFull
:
294 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_3
);
299 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_6
);
302 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_5
);
311 * aac_rx_start_adapter - activate adapter
314 * Start up processing on an i960 based AAC adapter
317 static void aac_rx_start_adapter(struct aac_dev
*dev
)
319 union aac_init
*init
;
322 init
->r7
.host_elapsed_seconds
= cpu_to_le32(get_seconds());
323 // We can only use a 32 bit address here
324 rx_sync_cmd(dev
, INIT_STRUCT_BASE_ADDRESS
, (u32
)(ulong
)dev
->init_pa
,
325 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
329 * aac_rx_check_health
330 * @dev: device to check if healthy
332 * Will attempt to determine if the specified adapter is alive and
333 * capable of handling requests, returning 0 if alive.
335 static int aac_rx_check_health(struct aac_dev
*dev
)
337 u32 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
340 * Check to see if the board failed any self tests.
342 if (unlikely(status
& SELF_TEST_FAILED
))
345 * Check to see if the board panic'd.
347 if (unlikely(status
& KERNEL_PANIC
)) {
353 dma_addr_t paddr
, baddr
;
356 if (likely((status
& 0xFF000000L
) == 0xBC000000L
))
357 return (status
>> 16) & 0xFF;
358 buffer
= dma_alloc_coherent(&dev
->pdev
->dev
, 512, &baddr
,
361 if (unlikely(buffer
== NULL
))
363 post
= dma_alloc_coherent(&dev
->pdev
->dev
,
364 sizeof(struct POSTSTATUS
), &paddr
,
366 if (unlikely(post
== NULL
)) {
367 dma_free_coherent(&dev
->pdev
->dev
, 512, buffer
, baddr
);
370 memset(buffer
, 0, 512);
371 post
->Post_Command
= cpu_to_le32(COMMAND_POST_RESULTS
);
372 post
->Post_Address
= cpu_to_le32(baddr
);
373 rx_writel(dev
, MUnit
.IMRx
[0], paddr
);
374 rx_sync_cmd(dev
, COMMAND_POST_RESULTS
, baddr
, 0, 0, 0, 0, 0,
375 NULL
, NULL
, NULL
, NULL
, NULL
);
376 dma_free_coherent(&dev
->pdev
->dev
, sizeof(struct POSTSTATUS
),
378 if (likely((buffer
[0] == '0') && ((buffer
[1] == 'x') || (buffer
[1] == 'X')))) {
379 ret
= (hex_to_bin(buffer
[2]) << 4) +
380 hex_to_bin(buffer
[3]);
382 dma_free_coherent(&dev
->pdev
->dev
, 512, buffer
, baddr
);
386 * Wait for the adapter to be up and running.
388 if (unlikely(!(status
& KERNEL_UP_AND_RUNNING
)))
397 * aac_rx_deliver_producer
400 * Will send a fib, returning 0 if successful.
402 int aac_rx_deliver_producer(struct fib
* fib
)
404 struct aac_dev
*dev
= fib
->dev
;
405 struct aac_queue
*q
= &dev
->queues
->queue
[AdapNormCmdQueue
];
407 unsigned long nointr
= 0;
409 aac_queue_get( dev
, &Index
, AdapNormCmdQueue
, fib
->hw_fib_va
, 1, fib
, &nointr
);
411 atomic_inc(&q
->numpending
);
412 *(q
->headers
.producer
) = cpu_to_le32(Index
+ 1);
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
];
431 volatile void __iomem
*device
;
433 unsigned long count
= 10000000L; /* 50 seconds */
434 atomic_inc(&q
->numpending
);
436 Index
= rx_readl(dev
, MUnit
.InboundQueue
);
437 if (unlikely(Index
== 0xFFFFFFFFL
))
438 Index
= rx_readl(dev
, MUnit
.InboundQueue
);
439 if (likely(Index
!= 0xFFFFFFFFL
))
442 atomic_dec(&q
->numpending
);
447 device
= dev
->base
+ Index
;
448 addr
= fib
->hw_fib_pa
;
449 writel((u32
)(addr
& 0xffffffff), device
);
450 device
+= sizeof(u32
);
451 writel((u32
)(addr
>> 32), device
);
452 device
+= sizeof(u32
);
453 writel(le16_to_cpu(fib
->hw_fib_va
->header
.Size
), device
);
454 rx_writel(dev
, MUnit
.InboundQueue
, Index
);
460 * @size: mapping resize request
463 static int aac_rx_ioremap(struct aac_dev
* dev
, u32 size
)
466 iounmap(dev
->regs
.rx
);
469 dev
->base
= dev
->regs
.rx
= ioremap(dev
->base_start
, size
);
470 if (dev
->base
== NULL
)
472 dev
->IndexRegs
= &dev
->regs
.rx
->IndexRegs
;
476 static int aac_rx_restart_adapter(struct aac_dev
*dev
, int bled
, u8 reset_type
)
480 if (!(dev
->supplement_adapter_info
.supported_options2
&
481 AAC_OPTION_MU_RESET
) || (bled
>= 0) || (bled
== -2)) {
483 printk(KERN_ERR
"%s%d: adapter kernel panic'd %x.\n",
484 dev
->name
, dev
->id
, bled
);
486 bled
= aac_adapter_sync_cmd(dev
, IOP_RESET_ALWAYS
,
487 0, 0, 0, 0, 0, 0, &var
, NULL
, NULL
, NULL
, NULL
);
488 if (!bled
&& (var
!= 0x00000001) && (var
!= 0x3803000F))
491 if (bled
&& (bled
!= -ETIMEDOUT
))
492 bled
= aac_adapter_sync_cmd(dev
, IOP_RESET
,
493 0, 0, 0, 0, 0, 0, &var
, NULL
, NULL
, NULL
, NULL
);
495 if (bled
&& (bled
!= -ETIMEDOUT
))
498 if (bled
&& (var
== 0x3803000F)) { /* USE_OTHER_METHOD */
499 rx_writel(dev
, MUnit
.reserved2
, 3);
500 msleep(5000); /* Delay 5 seconds */
503 if (bled
&& (var
!= 0x00000001))
506 if (rx_readl(dev
, MUnit
.OMRx
[0]) & KERNEL_PANIC
)
508 if (startup_timeout
< 300)
509 startup_timeout
= 300;
514 * aac_rx_select_comm - Select communications method
516 * @comm: communications method
519 int aac_rx_select_comm(struct aac_dev
*dev
, int comm
)
522 case AAC_COMM_PRODUCER
:
523 dev
->a_ops
.adapter_enable_int
= aac_rx_enable_interrupt_producer
;
524 dev
->a_ops
.adapter_intr
= aac_rx_intr_producer
;
525 dev
->a_ops
.adapter_deliver
= aac_rx_deliver_producer
;
527 case AAC_COMM_MESSAGE
:
528 dev
->a_ops
.adapter_enable_int
= aac_rx_enable_interrupt_message
;
529 dev
->a_ops
.adapter_intr
= aac_rx_intr_message
;
530 dev
->a_ops
.adapter_deliver
= aac_rx_deliver_message
;
539 * aac_rx_init - initialize an i960 based AAC card
540 * @dev: device to configure
542 * Allocate and set up resources for the i960 based AAC variants. The
543 * device_interface in the commregion will be allocated and linked
544 * to the comm region.
547 int _aac_rx_init(struct aac_dev
*dev
)
550 unsigned long status
;
552 int instance
= dev
->id
;
553 const char * name
= dev
->name
;
555 if (aac_adapter_ioremap(dev
, dev
->base_size
)) {
556 printk(KERN_WARNING
"%s: unable to map adapter.\n", name
);
560 /* Failure to reset here is an option ... */
561 dev
->a_ops
.adapter_sync_cmd
= rx_sync_cmd
;
562 dev
->a_ops
.adapter_enable_int
= aac_rx_disable_interrupt
;
563 dev
->OIMR
= status
= rx_readb (dev
, MUnit
.OIMR
);
565 if (((status
& 0x0c) != 0x0c) || dev
->init_reset
) {
566 dev
->init_reset
= false;
567 if (!aac_rx_restart_adapter(dev
, 0, IOP_HWSOFT_RESET
)) {
568 /* Make sure the Hardware FIFO is empty */
569 while ((++restart
< 512) &&
570 (rx_readl(dev
, MUnit
.OutboundQueue
) != 0xFFFFFFFFL
));
575 * Check to see if the board panic'd while booting.
577 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
578 if (status
& KERNEL_PANIC
) {
579 if (aac_rx_restart_adapter(dev
,
580 aac_rx_check_health(dev
), IOP_HWSOFT_RESET
))
585 * Check to see if the board failed any self tests.
587 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
588 if (status
& SELF_TEST_FAILED
) {
589 printk(KERN_ERR
"%s%d: adapter self-test failed.\n", dev
->name
, instance
);
593 * Check to see if the monitor panic'd while booting.
595 if (status
& MONITOR_PANIC
) {
596 printk(KERN_ERR
"%s%d: adapter monitor panic.\n", dev
->name
, instance
);
601 * Wait for the adapter to be up and running. Wait up to 3 minutes
603 while (!((status
= rx_readl(dev
, MUnit
.OMRx
[0])) & KERNEL_UP_AND_RUNNING
))
606 (status
& (KERNEL_PANIC
|SELF_TEST_FAILED
|MONITOR_PANIC
))) ||
607 time_after(jiffies
, start
+HZ
*startup_timeout
)) {
608 printk(KERN_ERR
"%s%d: adapter kernel failed to start, init status = %lx.\n",
609 dev
->name
, instance
, status
);
613 ((status
& (KERNEL_PANIC
|SELF_TEST_FAILED
|MONITOR_PANIC
)) ||
614 time_after(jiffies
, start
+ HZ
*
615 ((startup_timeout
> 60)
616 ? (startup_timeout
- 60)
617 : (startup_timeout
/ 2))))) {
618 if (likely(!aac_rx_restart_adapter(dev
,
619 aac_rx_check_health(dev
), IOP_HWSOFT_RESET
)))
625 if (restart
&& aac_commit
)
628 * Fill in the common function dispatch table.
630 dev
->a_ops
.adapter_interrupt
= aac_rx_interrupt_adapter
;
631 dev
->a_ops
.adapter_disable_int
= aac_rx_disable_interrupt
;
632 dev
->a_ops
.adapter_notify
= aac_rx_notify_adapter
;
633 dev
->a_ops
.adapter_sync_cmd
= rx_sync_cmd
;
634 dev
->a_ops
.adapter_check_health
= aac_rx_check_health
;
635 dev
->a_ops
.adapter_restart
= aac_rx_restart_adapter
;
636 dev
->a_ops
.adapter_start
= aac_rx_start_adapter
;
639 * First clear out all interrupts. Then enable the one's that we
642 aac_adapter_comm(dev
, AAC_COMM_PRODUCER
);
643 aac_adapter_disable_int(dev
);
644 rx_writel(dev
, MUnit
.ODR
, 0xffffffff);
645 aac_adapter_enable_int(dev
);
647 if (aac_init_adapter(dev
) == NULL
)
649 aac_adapter_comm(dev
, dev
->comm_interface
);
650 dev
->sync_mode
= 0; /* sync. mode not supported */
651 dev
->msi
= aac_msi
&& !pci_enable_msi(dev
->pdev
);
652 if (request_irq(dev
->pdev
->irq
, dev
->a_ops
.adapter_intr
,
653 IRQF_SHARED
, "aacraid", dev
) < 0) {
655 pci_disable_msi(dev
->pdev
);
656 printk(KERN_ERR
"%s%d: Interrupt unavailable.\n",
660 dev
->dbg_base
= dev
->base_start
;
661 dev
->dbg_base_mapped
= dev
->base
;
662 dev
->dbg_size
= dev
->base_size
;
664 aac_adapter_enable_int(dev
);
666 * Tell the adapter that all is configured, and it can
667 * start accepting requests
669 aac_rx_start_adapter(dev
);
678 int aac_rx_init(struct aac_dev
*dev
)
681 * Fill in the function dispatch table.
683 dev
->a_ops
.adapter_ioremap
= aac_rx_ioremap
;
684 dev
->a_ops
.adapter_comm
= aac_rx_select_comm
;
686 return _aac_rx_init(dev
);