1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Adaptec AAC series RAID controller driver
4 * (c) Copyright 2001 Red Hat Inc.
6 * based on the old aacraid driver that is..
7 * Adaptec aacraid device driver for Linux.
9 * Copyright (c) 2000-2010 Adaptec, Inc.
10 * 2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
11 * 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
16 * Abstract: Hardware miniport for Drawbridge specific hardware functions.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/spinlock.h>
24 #include <linux/blkdev.h>
25 #include <linux/delay.h>
26 #include <linux/completion.h>
27 #include <linux/time.h>
28 #include <linux/interrupt.h>
30 #include <scsi/scsi_host.h>
34 static irqreturn_t
aac_rx_intr_producer(int irq
, void *dev_id
)
36 struct aac_dev
*dev
= dev_id
;
37 unsigned long bellbits
;
38 u8 intstat
= rx_readb(dev
, MUnit
.OISR
);
41 * Read mask and invert because drawbridge is reversed.
42 * This allows us to only service interrupts that have
44 * Check to see if this is our interrupt. If it isn't just return
46 if (likely(intstat
& ~(dev
->OIMR
))) {
47 bellbits
= rx_readl(dev
, OutboundDoorbellReg
);
48 if (unlikely(bellbits
& DoorBellPrintfReady
)) {
49 aac_printf(dev
, readl (&dev
->IndexRegs
->Mailbox
[5]));
50 rx_writel(dev
, MUnit
.ODR
,DoorBellPrintfReady
);
51 rx_writel(dev
, InboundDoorbellReg
,DoorBellPrintfDone
);
53 else if (unlikely(bellbits
& DoorBellAdapterNormCmdReady
)) {
54 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormCmdReady
);
55 aac_command_normal(&dev
->queues
->queue
[HostNormCmdQueue
]);
57 else if (likely(bellbits
& DoorBellAdapterNormRespReady
)) {
58 rx_writel(dev
, MUnit
.ODR
,DoorBellAdapterNormRespReady
);
59 aac_response_normal(&dev
->queues
->queue
[HostNormRespQueue
]);
61 else if (unlikely(bellbits
& DoorBellAdapterNormCmdNotFull
)) {
62 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormCmdNotFull
);
64 else if (unlikely(bellbits
& DoorBellAdapterNormRespNotFull
)) {
65 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormCmdNotFull
);
66 rx_writel(dev
, MUnit
.ODR
, DoorBellAdapterNormRespNotFull
);
73 static irqreturn_t
aac_rx_intr_message(int irq
, void *dev_id
)
75 int isAif
, isFastResponse
, isSpecial
;
76 struct aac_dev
*dev
= dev_id
;
77 u32 Index
= rx_readl(dev
, MUnit
.OutboundQueue
);
78 if (unlikely(Index
== 0xFFFFFFFFL
))
79 Index
= rx_readl(dev
, MUnit
.OutboundQueue
);
80 if (likely(Index
!= 0xFFFFFFFFL
)) {
82 isAif
= isFastResponse
= isSpecial
= 0;
83 if (Index
& 0x00000002L
) {
85 if (Index
== 0xFFFFFFFEL
)
87 Index
&= ~0x00000002L
;
89 if (Index
& 0x00000001L
)
94 if (unlikely(aac_intr_normal(dev
,
96 isFastResponse
, NULL
))) {
102 DoorBellAdapterNormRespReady
);
105 Index
= rx_readl(dev
, MUnit
.OutboundQueue
);
106 } while (Index
!= 0xFFFFFFFFL
);
113 * aac_rx_disable_interrupt - Disable interrupts
117 static void aac_rx_disable_interrupt(struct aac_dev
*dev
)
119 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xff);
123 * aac_rx_enable_interrupt_producer - Enable interrupts
127 static void aac_rx_enable_interrupt_producer(struct aac_dev
*dev
)
129 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xfb);
133 * aac_rx_enable_interrupt_message - Enable interrupts
137 static void aac_rx_enable_interrupt_message(struct aac_dev
*dev
)
139 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xf7);
143 * rx_sync_cmd - send a command and wait
145 * @command: Command to execute
146 * @p1: first parameter
147 * @ret: adapter status
149 * This routine will send a synchronous command to the adapter and wait
150 * for its completion.
153 static int rx_sync_cmd(struct aac_dev
*dev
, u32 command
,
154 u32 p1
, u32 p2
, u32 p3
, u32 p4
, u32 p5
, u32 p6
,
155 u32
*status
, u32
* r1
, u32
* r2
, u32
* r3
, u32
* r4
)
160 * Write the command into Mailbox 0
162 writel(command
, &dev
->IndexRegs
->Mailbox
[0]);
164 * Write the parameters into Mailboxes 1 - 6
166 writel(p1
, &dev
->IndexRegs
->Mailbox
[1]);
167 writel(p2
, &dev
->IndexRegs
->Mailbox
[2]);
168 writel(p3
, &dev
->IndexRegs
->Mailbox
[3]);
169 writel(p4
, &dev
->IndexRegs
->Mailbox
[4]);
171 * Clear the synch command doorbell to start on a clean slate.
173 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
175 * Disable doorbell interrupts
177 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xff);
179 * Force the completion of the mask register write before issuing
182 rx_readb (dev
, MUnit
.OIMR
);
184 * Signal that there is a new synch command
186 rx_writel(dev
, InboundDoorbellReg
, INBOUNDDOORBELL_0
);
192 * Wait up to 30 seconds
194 while (time_before(jiffies
, start
+30*HZ
))
196 udelay(5); /* Delay 5 microseconds to let Mon960 get info. */
198 * Mon960 will set doorbell0 bit when it has completed the command.
200 if (rx_readl(dev
, OutboundDoorbellReg
) & OUTBOUNDDOORBELL_0
) {
202 * Clear the doorbell.
204 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
209 * Yield the processor in case we are slow
213 if (unlikely(ok
!= 1)) {
215 * Restore interrupt mask even though we timed out
217 aac_adapter_enable_int(dev
);
221 * Pull the synch status from Mailbox 0.
224 *status
= readl(&dev
->IndexRegs
->Mailbox
[0]);
226 *r1
= readl(&dev
->IndexRegs
->Mailbox
[1]);
228 *r2
= readl(&dev
->IndexRegs
->Mailbox
[2]);
230 *r3
= readl(&dev
->IndexRegs
->Mailbox
[3]);
232 *r4
= readl(&dev
->IndexRegs
->Mailbox
[4]);
234 * Clear the synch command doorbell.
236 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
238 * Restore interrupt mask
240 aac_adapter_enable_int(dev
);
246 * aac_rx_interrupt_adapter - interrupt adapter
249 * Send an interrupt to the i960 and breakpoint it.
252 static void aac_rx_interrupt_adapter(struct aac_dev
*dev
)
254 rx_sync_cmd(dev
, BREAKPOINT_REQUEST
, 0, 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
258 * aac_rx_notify_adapter - send an event to the adapter
260 * @event: Event to send
262 * Notify the i960 that something it probably cares about has
266 static void aac_rx_notify_adapter(struct aac_dev
*dev
, u32 event
)
271 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_1
);
273 case HostNormRespNotFull
:
274 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_4
);
276 case AdapNormRespQue
:
277 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_2
);
279 case HostNormCmdNotFull
:
280 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_3
);
285 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_6
);
288 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_5
);
297 * aac_rx_start_adapter - activate adapter
300 * Start up processing on an i960 based AAC adapter
303 static void aac_rx_start_adapter(struct aac_dev
*dev
)
305 union aac_init
*init
;
308 init
->r7
.host_elapsed_seconds
= cpu_to_le32(ktime_get_real_seconds());
309 // We can only use a 32 bit address here
310 rx_sync_cmd(dev
, INIT_STRUCT_BASE_ADDRESS
, (u32
)(ulong
)dev
->init_pa
,
311 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
315 * aac_rx_check_health
316 * @dev: device to check if healthy
318 * Will attempt to determine if the specified adapter is alive and
319 * capable of handling requests, returning 0 if alive.
321 static int aac_rx_check_health(struct aac_dev
*dev
)
323 u32 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
326 * Check to see if the board failed any self tests.
328 if (unlikely(status
& SELF_TEST_FAILED
))
331 * Check to see if the board panic'd.
333 if (unlikely(status
& KERNEL_PANIC
)) {
339 dma_addr_t paddr
, baddr
;
342 if (likely((status
& 0xFF000000L
) == 0xBC000000L
))
343 return (status
>> 16) & 0xFF;
344 buffer
= dma_alloc_coherent(&dev
->pdev
->dev
, 512, &baddr
,
347 if (unlikely(buffer
== NULL
))
349 post
= dma_alloc_coherent(&dev
->pdev
->dev
,
350 sizeof(struct POSTSTATUS
), &paddr
,
352 if (unlikely(post
== NULL
)) {
353 dma_free_coherent(&dev
->pdev
->dev
, 512, buffer
, baddr
);
356 memset(buffer
, 0, 512);
357 post
->Post_Command
= cpu_to_le32(COMMAND_POST_RESULTS
);
358 post
->Post_Address
= cpu_to_le32(baddr
);
359 rx_writel(dev
, MUnit
.IMRx
[0], paddr
);
360 rx_sync_cmd(dev
, COMMAND_POST_RESULTS
, baddr
, 0, 0, 0, 0, 0,
361 NULL
, NULL
, NULL
, NULL
, NULL
);
362 dma_free_coherent(&dev
->pdev
->dev
, sizeof(struct POSTSTATUS
),
364 if (likely((buffer
[0] == '0') && ((buffer
[1] == 'x') || (buffer
[1] == 'X')))) {
365 ret
= (hex_to_bin(buffer
[2]) << 4) +
366 hex_to_bin(buffer
[3]);
368 dma_free_coherent(&dev
->pdev
->dev
, 512, buffer
, baddr
);
372 * Wait for the adapter to be up and running.
374 if (unlikely(!(status
& KERNEL_UP_AND_RUNNING
)))
383 * aac_rx_deliver_producer
386 * Will send a fib, returning 0 if successful.
388 int aac_rx_deliver_producer(struct fib
* fib
)
390 struct aac_dev
*dev
= fib
->dev
;
391 struct aac_queue
*q
= &dev
->queues
->queue
[AdapNormCmdQueue
];
393 unsigned long nointr
= 0;
395 aac_queue_get( dev
, &Index
, AdapNormCmdQueue
, fib
->hw_fib_va
, 1, fib
, &nointr
);
397 atomic_inc(&q
->numpending
);
398 *(q
->headers
.producer
) = cpu_to_le32(Index
+ 1);
399 if (!(nointr
& aac_config
.irq_mod
))
400 aac_adapter_notify(dev
, AdapNormCmdQueue
);
406 * aac_rx_deliver_message
409 * Will send a fib, returning 0 if successful.
411 static int aac_rx_deliver_message(struct fib
* fib
)
413 struct aac_dev
*dev
= fib
->dev
;
414 struct aac_queue
*q
= &dev
->queues
->queue
[AdapNormCmdQueue
];
417 volatile void __iomem
*device
;
419 unsigned long count
= 10000000L; /* 50 seconds */
420 atomic_inc(&q
->numpending
);
422 Index
= rx_readl(dev
, MUnit
.InboundQueue
);
423 if (unlikely(Index
== 0xFFFFFFFFL
))
424 Index
= rx_readl(dev
, MUnit
.InboundQueue
);
425 if (likely(Index
!= 0xFFFFFFFFL
))
428 atomic_dec(&q
->numpending
);
433 device
= dev
->base
+ Index
;
434 addr
= fib
->hw_fib_pa
;
435 writel((u32
)(addr
& 0xffffffff), device
);
436 device
+= sizeof(u32
);
437 writel((u32
)(addr
>> 32), device
);
438 device
+= sizeof(u32
);
439 writel(le16_to_cpu(fib
->hw_fib_va
->header
.Size
), device
);
440 rx_writel(dev
, MUnit
.InboundQueue
, Index
);
446 * @size: mapping resize request
449 static int aac_rx_ioremap(struct aac_dev
* dev
, u32 size
)
452 iounmap(dev
->regs
.rx
);
455 dev
->base
= dev
->regs
.rx
= ioremap(dev
->base_start
, size
);
456 if (dev
->base
== NULL
)
458 dev
->IndexRegs
= &dev
->regs
.rx
->IndexRegs
;
462 static int aac_rx_restart_adapter(struct aac_dev
*dev
, int bled
, u8 reset_type
)
466 if (!(dev
->supplement_adapter_info
.supported_options2
&
467 AAC_OPTION_MU_RESET
) || (bled
>= 0) || (bled
== -2)) {
469 printk(KERN_ERR
"%s%d: adapter kernel panic'd %x.\n",
470 dev
->name
, dev
->id
, bled
);
472 bled
= aac_adapter_sync_cmd(dev
, IOP_RESET_ALWAYS
,
473 0, 0, 0, 0, 0, 0, &var
, NULL
, NULL
, NULL
, NULL
);
474 if (!bled
&& (var
!= 0x00000001) && (var
!= 0x3803000F))
477 if (bled
&& (bled
!= -ETIMEDOUT
))
478 bled
= aac_adapter_sync_cmd(dev
, IOP_RESET
,
479 0, 0, 0, 0, 0, 0, &var
, NULL
, NULL
, NULL
, NULL
);
481 if (bled
&& (bled
!= -ETIMEDOUT
))
484 if (bled
&& (var
== 0x3803000F)) { /* USE_OTHER_METHOD */
485 rx_writel(dev
, MUnit
.reserved2
, 3);
486 msleep(5000); /* Delay 5 seconds */
489 if (bled
&& (var
!= 0x00000001))
492 if (rx_readl(dev
, MUnit
.OMRx
[0]) & KERNEL_PANIC
)
494 if (startup_timeout
< 300)
495 startup_timeout
= 300;
500 * aac_rx_select_comm - Select communications method
502 * @comm: communications method
505 int aac_rx_select_comm(struct aac_dev
*dev
, int comm
)
508 case AAC_COMM_PRODUCER
:
509 dev
->a_ops
.adapter_enable_int
= aac_rx_enable_interrupt_producer
;
510 dev
->a_ops
.adapter_intr
= aac_rx_intr_producer
;
511 dev
->a_ops
.adapter_deliver
= aac_rx_deliver_producer
;
513 case AAC_COMM_MESSAGE
:
514 dev
->a_ops
.adapter_enable_int
= aac_rx_enable_interrupt_message
;
515 dev
->a_ops
.adapter_intr
= aac_rx_intr_message
;
516 dev
->a_ops
.adapter_deliver
= aac_rx_deliver_message
;
525 * aac_rx_init - initialize an i960 based AAC card
526 * @dev: device to configure
528 * Allocate and set up resources for the i960 based AAC variants. The
529 * device_interface in the commregion will be allocated and linked
530 * to the comm region.
533 int _aac_rx_init(struct aac_dev
*dev
)
536 unsigned long status
;
538 int instance
= dev
->id
;
539 const char * name
= dev
->name
;
541 if (aac_adapter_ioremap(dev
, dev
->base_size
)) {
542 printk(KERN_WARNING
"%s: unable to map adapter.\n", name
);
546 /* Failure to reset here is an option ... */
547 dev
->a_ops
.adapter_sync_cmd
= rx_sync_cmd
;
548 dev
->a_ops
.adapter_enable_int
= aac_rx_disable_interrupt
;
549 dev
->OIMR
= status
= rx_readb (dev
, MUnit
.OIMR
);
551 if (((status
& 0x0c) != 0x0c) || dev
->init_reset
) {
552 dev
->init_reset
= false;
553 if (!aac_rx_restart_adapter(dev
, 0, IOP_HWSOFT_RESET
)) {
554 /* Make sure the Hardware FIFO is empty */
555 while ((++restart
< 512) &&
556 (rx_readl(dev
, MUnit
.OutboundQueue
) != 0xFFFFFFFFL
));
561 * Check to see if the board panic'd while booting.
563 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
564 if (status
& KERNEL_PANIC
) {
565 if (aac_rx_restart_adapter(dev
,
566 aac_rx_check_health(dev
), IOP_HWSOFT_RESET
))
571 * Check to see if the board failed any self tests.
573 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
574 if (status
& SELF_TEST_FAILED
) {
575 printk(KERN_ERR
"%s%d: adapter self-test failed.\n", dev
->name
, instance
);
579 * Check to see if the monitor panic'd while booting.
581 if (status
& MONITOR_PANIC
) {
582 printk(KERN_ERR
"%s%d: adapter monitor panic.\n", dev
->name
, instance
);
587 * Wait for the adapter to be up and running. Wait up to 3 minutes
589 while (!((status
= rx_readl(dev
, MUnit
.OMRx
[0])) & KERNEL_UP_AND_RUNNING
))
592 (status
& (KERNEL_PANIC
|SELF_TEST_FAILED
|MONITOR_PANIC
))) ||
593 time_after(jiffies
, start
+HZ
*startup_timeout
)) {
594 printk(KERN_ERR
"%s%d: adapter kernel failed to start, init status = %lx.\n",
595 dev
->name
, instance
, status
);
599 ((status
& (KERNEL_PANIC
|SELF_TEST_FAILED
|MONITOR_PANIC
)) ||
600 time_after(jiffies
, start
+ HZ
*
601 ((startup_timeout
> 60)
602 ? (startup_timeout
- 60)
603 : (startup_timeout
/ 2))))) {
604 if (likely(!aac_rx_restart_adapter(dev
,
605 aac_rx_check_health(dev
), IOP_HWSOFT_RESET
)))
611 if (restart
&& aac_commit
)
614 * Fill in the common function dispatch table.
616 dev
->a_ops
.adapter_interrupt
= aac_rx_interrupt_adapter
;
617 dev
->a_ops
.adapter_disable_int
= aac_rx_disable_interrupt
;
618 dev
->a_ops
.adapter_notify
= aac_rx_notify_adapter
;
619 dev
->a_ops
.adapter_sync_cmd
= rx_sync_cmd
;
620 dev
->a_ops
.adapter_check_health
= aac_rx_check_health
;
621 dev
->a_ops
.adapter_restart
= aac_rx_restart_adapter
;
622 dev
->a_ops
.adapter_start
= aac_rx_start_adapter
;
625 * First clear out all interrupts. Then enable the one's that we
628 aac_adapter_comm(dev
, AAC_COMM_PRODUCER
);
629 aac_adapter_disable_int(dev
);
630 rx_writel(dev
, MUnit
.ODR
, 0xffffffff);
631 aac_adapter_enable_int(dev
);
633 if (aac_init_adapter(dev
) == NULL
)
635 aac_adapter_comm(dev
, dev
->comm_interface
);
636 dev
->sync_mode
= 0; /* sync. mode not supported */
637 dev
->msi
= aac_msi
&& !pci_enable_msi(dev
->pdev
);
638 if (request_irq(dev
->pdev
->irq
, dev
->a_ops
.adapter_intr
,
639 IRQF_SHARED
, "aacraid", dev
) < 0) {
641 pci_disable_msi(dev
->pdev
);
642 printk(KERN_ERR
"%s%d: Interrupt unavailable.\n",
646 dev
->dbg_base
= dev
->base_start
;
647 dev
->dbg_base_mapped
= dev
->base
;
648 dev
->dbg_size
= dev
->base_size
;
650 aac_adapter_enable_int(dev
);
652 * Tell the adapter that all is configured, and it can
653 * start accepting requests
655 aac_rx_start_adapter(dev
);
664 int aac_rx_init(struct aac_dev
*dev
)
667 * Fill in the function dispatch table.
669 dev
->a_ops
.adapter_ioremap
= aac_rx_ioremap
;
670 dev
->a_ops
.adapter_comm
= aac_rx_select_comm
;
672 return _aac_rx_init(dev
);