treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / scsi / aacraid / rx.c
blob3dea348bd25d2a42e97cfd960853127453b97e47
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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)
13 * Module Name:
14 * rx.c
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>
32 #include "aacraid.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
43 * been enabled.
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);
68 return IRQ_HANDLED;
70 return IRQ_NONE;
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)) {
81 do {
82 isAif = isFastResponse = isSpecial = 0;
83 if (Index & 0x00000002L) {
84 isAif = 1;
85 if (Index == 0xFFFFFFFEL)
86 isSpecial = 1;
87 Index &= ~0x00000002L;
88 } else {
89 if (Index & 0x00000001L)
90 isFastResponse = 1;
91 Index >>= 2;
93 if (!isSpecial) {
94 if (unlikely(aac_intr_normal(dev,
95 Index, isAif,
96 isFastResponse, NULL))) {
97 rx_writel(dev,
98 MUnit.OutboundQueue,
99 Index);
100 rx_writel(dev,
101 MUnit.ODR,
102 DoorBellAdapterNormRespReady);
105 Index = rx_readl(dev, MUnit.OutboundQueue);
106 } while (Index != 0xFFFFFFFFL);
107 return IRQ_HANDLED;
109 return IRQ_NONE;
113 * aac_rx_disable_interrupt - Disable interrupts
114 * @dev: Adapter
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
124 * @dev: Adapter
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
134 * @dev: Adapter
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
144 * @dev: Adapter
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)
157 unsigned long start;
158 int ok;
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
180 * the interrupt.
182 rx_readb (dev, MUnit.OIMR);
184 * Signal that there is a new synch command
186 rx_writel(dev, InboundDoorbellReg, INBOUNDDOORBELL_0);
188 ok = 0;
189 start = jiffies;
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);
205 ok = 1;
206 break;
209 * Yield the processor in case we are slow
211 msleep(1);
213 if (unlikely(ok != 1)) {
215 * Restore interrupt mask even though we timed out
217 aac_adapter_enable_int(dev);
218 return -ETIMEDOUT;
221 * Pull the synch status from Mailbox 0.
223 if (status)
224 *status = readl(&dev->IndexRegs->Mailbox[0]);
225 if (r1)
226 *r1 = readl(&dev->IndexRegs->Mailbox[1]);
227 if (r2)
228 *r2 = readl(&dev->IndexRegs->Mailbox[2]);
229 if (r3)
230 *r3 = readl(&dev->IndexRegs->Mailbox[3]);
231 if (r4)
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);
241 return 0;
246 * aac_rx_interrupt_adapter - interrupt adapter
247 * @dev: 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
259 * @dev: Adapter
260 * @event: Event to send
262 * Notify the i960 that something it probably cares about has
263 * happened.
266 static void aac_rx_notify_adapter(struct aac_dev *dev, u32 event)
268 switch (event) {
270 case AdapNormCmdQue:
271 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_1);
272 break;
273 case HostNormRespNotFull:
274 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_4);
275 break;
276 case AdapNormRespQue:
277 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_2);
278 break;
279 case HostNormCmdNotFull:
280 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_3);
281 break;
282 case HostShutdown:
283 break;
284 case FastIo:
285 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_6);
286 break;
287 case AdapPrintfDone:
288 rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_5);
289 break;
290 default:
291 BUG();
292 break;
297 * aac_rx_start_adapter - activate adapter
298 * @dev: 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;
307 init = dev->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))
329 return -1;
331 * Check to see if the board panic'd.
333 if (unlikely(status & KERNEL_PANIC)) {
334 char * buffer;
335 struct POSTSTATUS {
336 __le32 Post_Command;
337 __le32 Post_Address;
338 } * post;
339 dma_addr_t paddr, baddr;
340 int ret;
342 if (likely((status & 0xFF000000L) == 0xBC000000L))
343 return (status >> 16) & 0xFF;
344 buffer = dma_alloc_coherent(&dev->pdev->dev, 512, &baddr,
345 GFP_KERNEL);
346 ret = -2;
347 if (unlikely(buffer == NULL))
348 return ret;
349 post = dma_alloc_coherent(&dev->pdev->dev,
350 sizeof(struct POSTSTATUS), &paddr,
351 GFP_KERNEL);
352 if (unlikely(post == NULL)) {
353 dma_free_coherent(&dev->pdev->dev, 512, buffer, baddr);
354 return ret;
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),
363 post, paddr);
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);
369 return ret;
372 * Wait for the adapter to be up and running.
374 if (unlikely(!(status & KERNEL_UP_AND_RUNNING)))
375 return -3;
377 * Everything is OK
379 return 0;
383 * aac_rx_deliver_producer
384 * @fib: fib to issue
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];
392 u32 Index;
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);
402 return 0;
406 * aac_rx_deliver_message
407 * @fib: fib to issue
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];
415 u32 Index;
416 u64 addr;
417 volatile void __iomem *device;
419 unsigned long count = 10000000L; /* 50 seconds */
420 atomic_inc(&q->numpending);
421 for(;;) {
422 Index = rx_readl(dev, MUnit.InboundQueue);
423 if (unlikely(Index == 0xFFFFFFFFL))
424 Index = rx_readl(dev, MUnit.InboundQueue);
425 if (likely(Index != 0xFFFFFFFFL))
426 break;
427 if (--count == 0) {
428 atomic_dec(&q->numpending);
429 return -ETIMEDOUT;
431 udelay(5);
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);
441 return 0;
445 * aac_rx_ioremap
446 * @size: mapping resize request
449 static int aac_rx_ioremap(struct aac_dev * dev, u32 size)
451 if (!size) {
452 iounmap(dev->regs.rx);
453 return 0;
455 dev->base = dev->regs.rx = ioremap(dev->base_start, size);
456 if (dev->base == NULL)
457 return -1;
458 dev->IndexRegs = &dev->regs.rx->IndexRegs;
459 return 0;
462 static int aac_rx_restart_adapter(struct aac_dev *dev, int bled, u8 reset_type)
464 u32 var = 0;
466 if (!(dev->supplement_adapter_info.supported_options2 &
467 AAC_OPTION_MU_RESET) || (bled >= 0) || (bled == -2)) {
468 if (bled)
469 printk(KERN_ERR "%s%d: adapter kernel panic'd %x.\n",
470 dev->name, dev->id, bled);
471 else {
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))
475 bled = -EINVAL;
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))
482 return -EINVAL;
484 if (bled && (var == 0x3803000F)) { /* USE_OTHER_METHOD */
485 rx_writel(dev, MUnit.reserved2, 3);
486 msleep(5000); /* Delay 5 seconds */
487 var = 0x00000001;
489 if (bled && (var != 0x00000001))
490 return -EINVAL;
491 ssleep(5);
492 if (rx_readl(dev, MUnit.OMRx[0]) & KERNEL_PANIC)
493 return -ENODEV;
494 if (startup_timeout < 300)
495 startup_timeout = 300;
496 return 0;
500 * aac_rx_select_comm - Select communications method
501 * @dev: Adapter
502 * @comm: communications method
505 int aac_rx_select_comm(struct aac_dev *dev, int comm)
507 switch (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;
512 break;
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;
517 break;
518 default:
519 return 1;
521 return 0;
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)
535 unsigned long start;
536 unsigned long status;
537 int restart = 0;
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);
543 goto error_iounmap;
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))
567 goto error_iounmap;
568 ++restart;
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);
576 goto error_iounmap;
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);
583 goto error_iounmap;
585 start = jiffies;
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))
591 if ((restart &&
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);
596 goto error_iounmap;
598 if (!restart &&
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)))
606 start = jiffies;
607 ++restart;
609 msleep(1);
611 if (restart && aac_commit)
612 aac_commit = 1;
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
626 * can handle.
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)
634 goto error_iounmap;
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) {
640 if (dev->msi)
641 pci_disable_msi(dev->pdev);
642 printk(KERN_ERR "%s%d: Interrupt unavailable.\n",
643 name, instance);
644 goto error_iounmap;
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);
657 return 0;
659 error_iounmap:
661 return -1;
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);