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 * @p2: second parameter
148 * @p3: third parameter
149 * @p4: forth parameter
150 * @p5: fifth parameter
151 * @p6: sixth parameter
152 * @status: adapter status
153 * @r1: first return value
154 * @r2: second return value
155 * @r3: third return value
156 * @r4: forth return value
158 * This routine will send a synchronous command to the adapter and wait
159 * for its completion.
162 static int rx_sync_cmd(struct aac_dev
*dev
, u32 command
,
163 u32 p1
, u32 p2
, u32 p3
, u32 p4
, u32 p5
, u32 p6
,
164 u32
*status
, u32
* r1
, u32
* r2
, u32
* r3
, u32
* r4
)
169 * Write the command into Mailbox 0
171 writel(command
, &dev
->IndexRegs
->Mailbox
[0]);
173 * Write the parameters into Mailboxes 1 - 6
175 writel(p1
, &dev
->IndexRegs
->Mailbox
[1]);
176 writel(p2
, &dev
->IndexRegs
->Mailbox
[2]);
177 writel(p3
, &dev
->IndexRegs
->Mailbox
[3]);
178 writel(p4
, &dev
->IndexRegs
->Mailbox
[4]);
180 * Clear the synch command doorbell to start on a clean slate.
182 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
184 * Disable doorbell interrupts
186 rx_writeb(dev
, MUnit
.OIMR
, dev
->OIMR
= 0xff);
188 * Force the completion of the mask register write before issuing
191 rx_readb (dev
, MUnit
.OIMR
);
193 * Signal that there is a new synch command
195 rx_writel(dev
, InboundDoorbellReg
, INBOUNDDOORBELL_0
);
201 * Wait up to 30 seconds
203 while (time_before(jiffies
, start
+30*HZ
))
205 udelay(5); /* Delay 5 microseconds to let Mon960 get info. */
207 * Mon960 will set doorbell0 bit when it has completed the command.
209 if (rx_readl(dev
, OutboundDoorbellReg
) & OUTBOUNDDOORBELL_0
) {
211 * Clear the doorbell.
213 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
218 * Yield the processor in case we are slow
222 if (unlikely(ok
!= 1)) {
224 * Restore interrupt mask even though we timed out
226 aac_adapter_enable_int(dev
);
230 * Pull the synch status from Mailbox 0.
233 *status
= readl(&dev
->IndexRegs
->Mailbox
[0]);
235 *r1
= readl(&dev
->IndexRegs
->Mailbox
[1]);
237 *r2
= readl(&dev
->IndexRegs
->Mailbox
[2]);
239 *r3
= readl(&dev
->IndexRegs
->Mailbox
[3]);
241 *r4
= readl(&dev
->IndexRegs
->Mailbox
[4]);
243 * Clear the synch command doorbell.
245 rx_writel(dev
, OutboundDoorbellReg
, OUTBOUNDDOORBELL_0
);
247 * Restore interrupt mask
249 aac_adapter_enable_int(dev
);
255 * aac_rx_interrupt_adapter - interrupt adapter
258 * Send an interrupt to the i960 and breakpoint it.
261 static void aac_rx_interrupt_adapter(struct aac_dev
*dev
)
263 rx_sync_cmd(dev
, BREAKPOINT_REQUEST
, 0, 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
267 * aac_rx_notify_adapter - send an event to the adapter
269 * @event: Event to send
271 * Notify the i960 that something it probably cares about has
275 static void aac_rx_notify_adapter(struct aac_dev
*dev
, u32 event
)
280 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_1
);
282 case HostNormRespNotFull
:
283 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_4
);
285 case AdapNormRespQue
:
286 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_2
);
288 case HostNormCmdNotFull
:
289 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_3
);
294 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_6
);
297 rx_writel(dev
, MUnit
.IDR
,INBOUNDDOORBELL_5
);
306 * aac_rx_start_adapter - activate adapter
309 * Start up processing on an i960 based AAC adapter
312 static void aac_rx_start_adapter(struct aac_dev
*dev
)
314 union aac_init
*init
;
317 init
->r7
.host_elapsed_seconds
= cpu_to_le32(ktime_get_real_seconds());
318 // We can only use a 32 bit address here
319 rx_sync_cmd(dev
, INIT_STRUCT_BASE_ADDRESS
, (u32
)(ulong
)dev
->init_pa
,
320 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
324 * aac_rx_check_health
325 * @dev: device to check if healthy
327 * Will attempt to determine if the specified adapter is alive and
328 * capable of handling requests, returning 0 if alive.
330 static int aac_rx_check_health(struct aac_dev
*dev
)
332 u32 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
335 * Check to see if the board failed any self tests.
337 if (unlikely(status
& SELF_TEST_FAILED
))
340 * Check to see if the board panic'd.
342 if (unlikely(status
& KERNEL_PANIC
)) {
348 dma_addr_t paddr
, baddr
;
351 if (likely((status
& 0xFF000000L
) == 0xBC000000L
))
352 return (status
>> 16) & 0xFF;
353 buffer
= dma_alloc_coherent(&dev
->pdev
->dev
, 512, &baddr
,
356 if (unlikely(buffer
== NULL
))
358 post
= dma_alloc_coherent(&dev
->pdev
->dev
,
359 sizeof(struct POSTSTATUS
), &paddr
,
361 if (unlikely(post
== NULL
)) {
362 dma_free_coherent(&dev
->pdev
->dev
, 512, buffer
, baddr
);
365 memset(buffer
, 0, 512);
366 post
->Post_Command
= cpu_to_le32(COMMAND_POST_RESULTS
);
367 post
->Post_Address
= cpu_to_le32(baddr
);
368 rx_writel(dev
, MUnit
.IMRx
[0], paddr
);
369 rx_sync_cmd(dev
, COMMAND_POST_RESULTS
, baddr
, 0, 0, 0, 0, 0,
370 NULL
, NULL
, NULL
, NULL
, NULL
);
371 dma_free_coherent(&dev
->pdev
->dev
, sizeof(struct POSTSTATUS
),
373 if (likely((buffer
[0] == '0') && ((buffer
[1] == 'x') || (buffer
[1] == 'X')))) {
374 ret
= (hex_to_bin(buffer
[2]) << 4) +
375 hex_to_bin(buffer
[3]);
377 dma_free_coherent(&dev
->pdev
->dev
, 512, buffer
, baddr
);
381 * Wait for the adapter to be up and running.
383 if (unlikely(!(status
& KERNEL_UP_AND_RUNNING
)))
392 * aac_rx_deliver_producer
395 * Will send a fib, returning 0 if successful.
397 int aac_rx_deliver_producer(struct fib
* fib
)
399 struct aac_dev
*dev
= fib
->dev
;
400 struct aac_queue
*q
= &dev
->queues
->queue
[AdapNormCmdQueue
];
402 unsigned long nointr
= 0;
404 aac_queue_get( dev
, &Index
, AdapNormCmdQueue
, fib
->hw_fib_va
, 1, fib
, &nointr
);
406 atomic_inc(&q
->numpending
);
407 *(q
->headers
.producer
) = cpu_to_le32(Index
+ 1);
408 if (!(nointr
& aac_config
.irq_mod
))
409 aac_adapter_notify(dev
, AdapNormCmdQueue
);
415 * aac_rx_deliver_message
418 * Will send a fib, returning 0 if successful.
420 static int aac_rx_deliver_message(struct fib
* fib
)
422 struct aac_dev
*dev
= fib
->dev
;
423 struct aac_queue
*q
= &dev
->queues
->queue
[AdapNormCmdQueue
];
426 volatile void __iomem
*device
;
428 unsigned long count
= 10000000L; /* 50 seconds */
429 atomic_inc(&q
->numpending
);
431 Index
= rx_readl(dev
, MUnit
.InboundQueue
);
432 if (unlikely(Index
== 0xFFFFFFFFL
))
433 Index
= rx_readl(dev
, MUnit
.InboundQueue
);
434 if (likely(Index
!= 0xFFFFFFFFL
))
437 atomic_dec(&q
->numpending
);
442 device
= dev
->base
+ Index
;
443 addr
= fib
->hw_fib_pa
;
444 writel((u32
)(addr
& 0xffffffff), device
);
445 device
+= sizeof(u32
);
446 writel((u32
)(addr
>> 32), device
);
447 device
+= sizeof(u32
);
448 writel(le16_to_cpu(fib
->hw_fib_va
->header
.Size
), device
);
449 rx_writel(dev
, MUnit
.InboundQueue
, Index
);
456 * @size: mapping resize request
459 static int aac_rx_ioremap(struct aac_dev
* dev
, u32 size
)
462 iounmap(dev
->regs
.rx
);
465 dev
->base
= dev
->regs
.rx
= ioremap(dev
->base_start
, size
);
466 if (dev
->base
== NULL
)
468 dev
->IndexRegs
= &dev
->regs
.rx
->IndexRegs
;
472 static int aac_rx_restart_adapter(struct aac_dev
*dev
, int bled
, u8 reset_type
)
476 if (!(dev
->supplement_adapter_info
.supported_options2
&
477 AAC_OPTION_MU_RESET
) || (bled
>= 0) || (bled
== -2)) {
479 printk(KERN_ERR
"%s%d: adapter kernel panic'd %x.\n",
480 dev
->name
, dev
->id
, bled
);
482 bled
= aac_adapter_sync_cmd(dev
, IOP_RESET_ALWAYS
,
483 0, 0, 0, 0, 0, 0, &var
, NULL
, NULL
, NULL
, NULL
);
484 if (!bled
&& (var
!= 0x00000001) && (var
!= 0x3803000F))
487 if (bled
&& (bled
!= -ETIMEDOUT
))
488 bled
= aac_adapter_sync_cmd(dev
, IOP_RESET
,
489 0, 0, 0, 0, 0, 0, &var
, NULL
, NULL
, NULL
, NULL
);
491 if (bled
&& (bled
!= -ETIMEDOUT
))
494 if (bled
&& (var
== 0x3803000F)) { /* USE_OTHER_METHOD */
495 rx_writel(dev
, MUnit
.reserved2
, 3);
496 msleep(5000); /* Delay 5 seconds */
499 if (bled
&& (var
!= 0x00000001))
502 if (rx_readl(dev
, MUnit
.OMRx
[0]) & KERNEL_PANIC
)
504 if (startup_timeout
< 300)
505 startup_timeout
= 300;
510 * aac_rx_select_comm - Select communications method
512 * @comm: communications method
515 int aac_rx_select_comm(struct aac_dev
*dev
, int comm
)
518 case AAC_COMM_PRODUCER
:
519 dev
->a_ops
.adapter_enable_int
= aac_rx_enable_interrupt_producer
;
520 dev
->a_ops
.adapter_intr
= aac_rx_intr_producer
;
521 dev
->a_ops
.adapter_deliver
= aac_rx_deliver_producer
;
523 case AAC_COMM_MESSAGE
:
524 dev
->a_ops
.adapter_enable_int
= aac_rx_enable_interrupt_message
;
525 dev
->a_ops
.adapter_intr
= aac_rx_intr_message
;
526 dev
->a_ops
.adapter_deliver
= aac_rx_deliver_message
;
535 * aac_rx_init - initialize an i960 based AAC card
536 * @dev: device to configure
538 * Allocate and set up resources for the i960 based AAC variants. The
539 * device_interface in the commregion will be allocated and linked
540 * to the comm region.
543 int _aac_rx_init(struct aac_dev
*dev
)
546 unsigned long status
;
548 int instance
= dev
->id
;
549 const char * name
= dev
->name
;
551 if (aac_adapter_ioremap(dev
, dev
->base_size
)) {
552 printk(KERN_WARNING
"%s: unable to map adapter.\n", name
);
556 /* Failure to reset here is an option ... */
557 dev
->a_ops
.adapter_sync_cmd
= rx_sync_cmd
;
558 dev
->a_ops
.adapter_enable_int
= aac_rx_disable_interrupt
;
559 dev
->OIMR
= status
= rx_readb (dev
, MUnit
.OIMR
);
561 if (((status
& 0x0c) != 0x0c) || dev
->init_reset
) {
562 dev
->init_reset
= false;
563 if (!aac_rx_restart_adapter(dev
, 0, IOP_HWSOFT_RESET
)) {
564 /* Make sure the Hardware FIFO is empty */
565 while ((++restart
< 512) &&
566 (rx_readl(dev
, MUnit
.OutboundQueue
) != 0xFFFFFFFFL
));
571 * Check to see if the board panic'd while booting.
573 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
574 if (status
& KERNEL_PANIC
) {
575 if (aac_rx_restart_adapter(dev
,
576 aac_rx_check_health(dev
), IOP_HWSOFT_RESET
))
581 * Check to see if the board failed any self tests.
583 status
= rx_readl(dev
, MUnit
.OMRx
[0]);
584 if (status
& SELF_TEST_FAILED
) {
585 printk(KERN_ERR
"%s%d: adapter self-test failed.\n", dev
->name
, instance
);
589 * Check to see if the monitor panic'd while booting.
591 if (status
& MONITOR_PANIC
) {
592 printk(KERN_ERR
"%s%d: adapter monitor panic.\n", dev
->name
, instance
);
597 * Wait for the adapter to be up and running. Wait up to 3 minutes
599 while (!((status
= rx_readl(dev
, MUnit
.OMRx
[0])) & KERNEL_UP_AND_RUNNING
))
602 (status
& (KERNEL_PANIC
|SELF_TEST_FAILED
|MONITOR_PANIC
))) ||
603 time_after(jiffies
, start
+HZ
*startup_timeout
)) {
604 printk(KERN_ERR
"%s%d: adapter kernel failed to start, init status = %lx.\n",
605 dev
->name
, instance
, status
);
609 ((status
& (KERNEL_PANIC
|SELF_TEST_FAILED
|MONITOR_PANIC
)) ||
610 time_after(jiffies
, start
+ HZ
*
611 ((startup_timeout
> 60)
612 ? (startup_timeout
- 60)
613 : (startup_timeout
/ 2))))) {
614 if (likely(!aac_rx_restart_adapter(dev
,
615 aac_rx_check_health(dev
), IOP_HWSOFT_RESET
)))
621 if (restart
&& aac_commit
)
624 * Fill in the common function dispatch table.
626 dev
->a_ops
.adapter_interrupt
= aac_rx_interrupt_adapter
;
627 dev
->a_ops
.adapter_disable_int
= aac_rx_disable_interrupt
;
628 dev
->a_ops
.adapter_notify
= aac_rx_notify_adapter
;
629 dev
->a_ops
.adapter_sync_cmd
= rx_sync_cmd
;
630 dev
->a_ops
.adapter_check_health
= aac_rx_check_health
;
631 dev
->a_ops
.adapter_restart
= aac_rx_restart_adapter
;
632 dev
->a_ops
.adapter_start
= aac_rx_start_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
->sync_mode
= 0; /* sync. mode not supported */
647 dev
->msi
= aac_msi
&& !pci_enable_msi(dev
->pdev
);
648 if (request_irq(dev
->pdev
->irq
, dev
->a_ops
.adapter_intr
,
649 IRQF_SHARED
, "aacraid", dev
) < 0) {
651 pci_disable_msi(dev
->pdev
);
652 printk(KERN_ERR
"%s%d: Interrupt unavailable.\n",
656 dev
->dbg_base
= dev
->base_start
;
657 dev
->dbg_base_mapped
= dev
->base
;
658 dev
->dbg_size
= dev
->base_size
;
660 aac_adapter_enable_int(dev
);
662 * Tell the adapter that all is configured, and it can
663 * start accepting requests
665 aac_rx_start_adapter(dev
);
674 int aac_rx_init(struct aac_dev
*dev
)
677 * Fill in the function dispatch table.
679 dev
->a_ops
.adapter_ioremap
= aac_rx_ioremap
;
680 dev
->a_ops
.adapter_comm
= aac_rx_select_comm
;
682 return _aac_rx_init(dev
);