fc4c73c2a6a9ec81b4759b5fd8691f72759c7857
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / scsi / aacraid / rkt.c
1 /*
2  *      Adaptec AAC series RAID controller driver
3  *      (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; see the file COPYING.  If not, write to
22  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * Module Name:
25  *  rkt.c
26  *
27  * Abstract: Hardware miniport for Drawbridge specific hardware functions.
28  *
29  */
30
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/types.h>
34 #include <linux/sched.h>
35 #include <linux/pci.h>
36 #include <linux/spinlock.h>
37 #include <linux/slab.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>
43 #include <asm/semaphore.h>
44
45 #include <scsi/scsi_host.h>
46
47 #include "aacraid.h"
48
49 static irqreturn_t aac_rkt_intr(int irq, void *dev_id, struct pt_regs *regs)
50 {
51         struct aac_dev *dev = dev_id;
52
53         if (dev->new_comm_interface) {
54                 u32 Index = rkt_readl(dev, MUnit.OutboundQueue);
55                 if (Index == 0xFFFFFFFFL)
56                         Index = rkt_readl(dev, MUnit.OutboundQueue);
57                 if (Index != 0xFFFFFFFFL) {
58                         do {
59                                 if (aac_intr_normal(dev, Index)) {
60                                         rkt_writel(dev, MUnit.OutboundQueue, Index);
61                                         rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormRespReady);
62                                 }
63                                 Index = rkt_readl(dev, MUnit.OutboundQueue);
64                         } while (Index != 0xFFFFFFFFL);
65                         return IRQ_HANDLED;
66                 }
67         } else {
68                 unsigned long bellbits;
69                 u8 intstat;
70                 intstat = rkt_readb(dev, MUnit.OISR);
71                 /*
72                  *      Read mask and invert because drawbridge is reversed.
73                  *      This allows us to only service interrupts that have 
74                  *      been enabled.
75                  *      Check to see if this is our interrupt.  If it isn't just return
76                  */
77                 if (intstat & ~(dev->OIMR))
78                 {
79                         bellbits = rkt_readl(dev, OutboundDoorbellReg);
80                         if (bellbits & DoorBellPrintfReady) {
81                                 aac_printf(dev, rkt_readl (dev, IndexRegs.Mailbox[5]));
82                                 rkt_writel(dev, MUnit.ODR,DoorBellPrintfReady);
83                                 rkt_writel(dev, InboundDoorbellReg,DoorBellPrintfDone);
84                         }
85                         else if (bellbits & DoorBellAdapterNormCmdReady) {
86                                 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdReady);
87                                 aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
88 //                              rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdReady);
89                         }
90                         else if (bellbits & DoorBellAdapterNormRespReady) {
91                                 rkt_writel(dev, MUnit.ODR,DoorBellAdapterNormRespReady);
92                                 aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
93                         }
94                         else if (bellbits & DoorBellAdapterNormCmdNotFull) {
95                                 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
96                         }
97                         else if (bellbits & DoorBellAdapterNormRespNotFull) {
98                                 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
99                                 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormRespNotFull);
100                         }
101                         return IRQ_HANDLED;
102                 }
103         }
104         return IRQ_NONE;
105 }
106
107 /**
108  *      aac_rkt_disable_interrupt       -       Disable interrupts
109  *      @dev: Adapter
110  */
111
112 static void aac_rkt_disable_interrupt(struct aac_dev *dev)
113 {
114         rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
115 }
116
117 /**
118  *      rkt_sync_cmd    -       send a command and wait
119  *      @dev: Adapter
120  *      @command: Command to execute
121  *      @p1: first parameter
122  *      @ret: adapter status
123  *
124  *      This routine will send a synchronous command to the adapter and wait 
125  *      for its completion.
126  */
127
128 static int rkt_sync_cmd(struct aac_dev *dev, u32 command,
129         u32 p1, u32 p2, u32 p3, u32 p4, u32 p5, u32 p6,
130         u32 *status, u32 *r1, u32 *r2, u32 *r3, u32 *r4)
131 {
132         unsigned long start;
133         int ok;
134         /*
135          *      Write the command into Mailbox 0
136          */
137         rkt_writel(dev, InboundMailbox0, command);
138         /*
139          *      Write the parameters into Mailboxes 1 - 6
140          */
141         rkt_writel(dev, InboundMailbox1, p1);
142         rkt_writel(dev, InboundMailbox2, p2);
143         rkt_writel(dev, InboundMailbox3, p3);
144         rkt_writel(dev, InboundMailbox4, p4);
145         /*
146          *      Clear the synch command doorbell to start on a clean slate.
147          */
148         rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
149         /*
150          *      Disable doorbell interrupts
151          */
152         rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
153         /*
154          *      Force the completion of the mask register write before issuing
155          *      the interrupt.
156          */
157         rkt_readb (dev, MUnit.OIMR);
158         /*
159          *      Signal that there is a new synch command
160          */
161         rkt_writel(dev, InboundDoorbellReg, INBOUNDDOORBELL_0);
162
163         ok = 0;
164         start = jiffies;
165
166         /*
167          *      Wait up to 30 seconds
168          */
169         while (time_before(jiffies, start+30*HZ)) 
170         {
171                 udelay(5);      /* Delay 5 microseconds to let Mon960 get info. */
172                 /*
173                  *      Mon960 will set doorbell0 bit when it has completed the command.
174                  */
175                 if (rkt_readl(dev, OutboundDoorbellReg) & OUTBOUNDDOORBELL_0) {
176                         /*
177                          *      Clear the doorbell.
178                          */
179                         rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
180                         ok = 1;
181                         break;
182                 }
183                 /*
184                  *      Yield the processor in case we are slow 
185                  */
186                 set_current_state(TASK_UNINTERRUPTIBLE);
187                 schedule_timeout(1);
188         }
189         if (ok != 1) {
190                 /*
191                  *      Restore interrupt mask even though we timed out
192                  */
193                 if (dev->new_comm_interface)
194                         rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xf7);
195                 else
196                         rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
197                 return -ETIMEDOUT;
198         }
199         /*
200          *      Pull the synch status from Mailbox 0.
201          */
202         if (status)
203                 *status = rkt_readl(dev, IndexRegs.Mailbox[0]);
204         if (r1)
205                 *r1 = rkt_readl(dev, IndexRegs.Mailbox[1]);
206         if (r2)
207                 *r2 = rkt_readl(dev, IndexRegs.Mailbox[2]);
208         if (r3)
209                 *r3 = rkt_readl(dev, IndexRegs.Mailbox[3]);
210         if (r4)
211                 *r4 = rkt_readl(dev, IndexRegs.Mailbox[4]);
212         /*
213          *      Clear the synch command doorbell.
214          */
215         rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
216         /*
217          *      Restore interrupt mask
218          */
219         if (dev->new_comm_interface)
220                 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xf7);
221         else
222                 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
223         return 0;
224
225 }
226
227 /**
228  *      aac_rkt_interrupt_adapter       -       interrupt adapter
229  *      @dev: Adapter
230  *
231  *      Send an interrupt to the i960 and breakpoint it.
232  */
233
234 static void aac_rkt_interrupt_adapter(struct aac_dev *dev)
235 {
236         rkt_sync_cmd(dev, BREAKPOINT_REQUEST, 0, 0, 0, 0, 0, 0,
237           NULL, NULL, NULL, NULL, NULL);
238 }
239
240 /**
241  *      aac_rkt_notify_adapter          -       send an event to the adapter
242  *      @dev: Adapter
243  *      @event: Event to send
244  *
245  *      Notify the i960 that something it probably cares about has
246  *      happened.
247  */
248
249 static void aac_rkt_notify_adapter(struct aac_dev *dev, u32 event)
250 {
251         switch (event) {
252
253         case AdapNormCmdQue:
254                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_1);
255                 break;
256         case HostNormRespNotFull:
257                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_4);
258                 break;
259         case AdapNormRespQue:
260                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_2);
261                 break;
262         case HostNormCmdNotFull:
263                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_3);
264                 break;
265         case HostShutdown:
266 //              rkt_sync_cmd(dev, HOST_CRASHING, 0, 0, 0, 0, 0, 0,
267 //                NULL, NULL, NULL, NULL, NULL);
268                 break;
269         case FastIo:
270                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_6);
271                 break;
272         case AdapPrintfDone:
273                 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_5);
274                 break;
275         default:
276                 BUG();
277                 break;
278         }
279 }
280
281 /**
282  *      aac_rkt_start_adapter           -       activate adapter
283  *      @dev:   Adapter
284  *
285  *      Start up processing on an i960 based AAC adapter
286  */
287
288 static void aac_rkt_start_adapter(struct aac_dev *dev)
289 {
290         struct aac_init *init;
291
292         init = dev->init;
293         init->HostElapsedSeconds = cpu_to_le32(get_seconds());
294         // We can only use a 32 bit address here
295         rkt_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa,
296           0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
297 }
298
299 /**
300  *      aac_rkt_check_health
301  *      @dev: device to check if healthy
302  *
303  *      Will attempt to determine if the specified adapter is alive and
304  *      capable of handling requests, returning 0 if alive.
305  */
306 static int aac_rkt_check_health(struct aac_dev *dev)
307 {
308         u32 status = rkt_readl(dev, MUnit.OMRx[0]);
309
310         /*
311          *      Check to see if the board failed any self tests.
312          */
313         if (status & SELF_TEST_FAILED)
314                 return -1;
315         /*
316          *      Check to see if the board panic'd.
317          */
318         if (status & KERNEL_PANIC) {
319                 char * buffer;
320                 struct POSTSTATUS {
321                         __le32 Post_Command;
322                         __le32 Post_Address;
323                 } * post;
324                 dma_addr_t paddr, baddr;
325                 int ret;
326
327                 if ((status & 0xFF000000L) == 0xBC000000L)
328                         return (status >> 16) & 0xFF;
329                 buffer = pci_alloc_consistent(dev->pdev, 512, &baddr);
330                 ret = -2;
331                 if (buffer == NULL)
332                         return ret;
333                 post = pci_alloc_consistent(dev->pdev,
334                   sizeof(struct POSTSTATUS), &paddr);
335                 if (post == NULL) {
336                         pci_free_consistent(dev->pdev, 512, buffer, baddr);
337                         return ret;
338                 }
339                 memset(buffer, 0, 512);
340                 post->Post_Command = cpu_to_le32(COMMAND_POST_RESULTS);
341                 post->Post_Address = cpu_to_le32(baddr);
342                 rkt_writel(dev, MUnit.IMRx[0], paddr);
343                 rkt_sync_cmd(dev, COMMAND_POST_RESULTS, baddr, 0, 0, 0, 0, 0,
344                   NULL, NULL, NULL, NULL, NULL);
345                 pci_free_consistent(dev->pdev, sizeof(struct POSTSTATUS),
346                   post, paddr);
347                 if ((buffer[0] == '0') && (buffer[1] == 'x')) {
348                         ret = (buffer[2] <= '9') ? (buffer[2] - '0') : (buffer[2] - 'A' + 10);
349                         ret <<= 4;
350                         ret += (buffer[3] <= '9') ? (buffer[3] - '0') : (buffer[3] - 'A' + 10);
351                 }
352                 pci_free_consistent(dev->pdev, 512, buffer, baddr);
353                 return ret;
354         }
355         /*
356          *      Wait for the adapter to be up and running.
357          */
358         if (!(status & KERNEL_UP_AND_RUNNING))
359                 return -3;
360         /*
361          *      Everything is OK
362          */
363         return 0;
364 }
365
366 /**
367  *      aac_rkt_send
368  *      @fib: fib to issue
369  *
370  *      Will send a fib, returning 0 if successful.
371  */
372 static int aac_rkt_send(struct fib * fib)
373 {
374         u64 addr = fib->hw_fib_pa;
375         struct aac_dev *dev = fib->dev;
376         volatile void __iomem *device = dev->regs.rkt;
377         u32 Index;
378
379         dprintk((KERN_DEBUG "%p->aac_rkt_send(%p->%llx)\n", dev, fib, addr));
380         Index = rkt_readl(dev, MUnit.InboundQueue);
381         if (Index == 0xFFFFFFFFL)
382                 Index = rkt_readl(dev, MUnit.InboundQueue);
383         dprintk((KERN_DEBUG "Index = 0x%x\n", Index));
384         if (Index == 0xFFFFFFFFL)
385                 return Index;
386         device += Index;
387         dprintk((KERN_DEBUG "entry = %x %x %u\n", (u32)(addr & 0xffffffff),
388           (u32)(addr >> 32), (u32)le16_to_cpu(fib->hw_fib->header.Size)));
389         writel((u32)(addr & 0xffffffff), device);
390         device += sizeof(u32);
391         writel((u32)(addr >> 32), device);
392         device += sizeof(u32);
393         writel(le16_to_cpu(fib->hw_fib->header.Size), device);
394         rkt_writel(dev, MUnit.InboundQueue, Index);
395         dprintk((KERN_DEBUG "aac_rkt_send - return 0\n"));
396         return 0;
397 }
398
399 /**
400  *      aac_rkt_init    -       initialize an i960 based AAC card
401  *      @dev: device to configure
402  *
403  *      Allocate and set up resources for the i960 based AAC variants. The 
404  *      device_interface in the commregion will be allocated and linked 
405  *      to the comm region.
406  */
407
408 int aac_rkt_init(struct aac_dev *dev)
409 {
410         unsigned long start;
411         unsigned long status;
412         int instance;
413         const char * name;
414
415         instance = dev->id;
416         name     = dev->name;
417
418         /*
419          *      Check to see if the board panic'd while booting.
420          */
421         /*
422          *      Check to see if the board failed any self tests.
423          */
424         if (rkt_readl(dev, MUnit.OMRx[0]) & SELF_TEST_FAILED) {
425                 printk(KERN_ERR "%s%d: adapter self-test failed.\n", dev->name, instance);
426                 goto error_iounmap;
427         }
428         /*
429          *      Check to see if the monitor panic'd while booting.
430          */
431         if (rkt_readl(dev, MUnit.OMRx[0]) & MONITOR_PANIC) {
432                 printk(KERN_ERR "%s%d: adapter monitor panic.\n", dev->name, instance);
433                 goto error_iounmap;
434         }
435         /*
436          *      Check to see if the board panic'd while booting.
437          */
438         if (rkt_readl(dev, MUnit.OMRx[0]) & KERNEL_PANIC) {
439                 printk(KERN_ERR "%s%d: adapter kernel panic'd.\n", dev->name, instance);
440                 goto error_iounmap;
441         }
442         start = jiffies;
443         /*
444          *      Wait for the adapter to be up and running. Wait up to 3 minutes
445          */
446         while (!(rkt_readl(dev, MUnit.OMRx[0]) & KERNEL_UP_AND_RUNNING))
447         {
448                 if(time_after(jiffies, start+180*HZ))
449                 {
450                         status = rkt_readl(dev, MUnit.OMRx[0]);
451                         printk(KERN_ERR "%s%d: adapter kernel failed to start, init status = %lx.\n", 
452                                         dev->name, instance, status);
453                         goto error_iounmap;
454                 }
455                 set_current_state(TASK_UNINTERRUPTIBLE);
456                 schedule_timeout(1);
457         }
458         if (request_irq(dev->scsi_host_ptr->irq, aac_rkt_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev)<0) 
459         {
460                 printk(KERN_ERR "%s%d: Interrupt unavailable.\n", name, instance);
461                 goto error_iounmap;
462         }
463         /*
464          *      Fill in the function dispatch table.
465          */
466         dev->a_ops.adapter_interrupt = aac_rkt_interrupt_adapter;
467         dev->a_ops.adapter_disable_int = aac_rkt_disable_interrupt;
468         dev->a_ops.adapter_notify = aac_rkt_notify_adapter;
469         dev->a_ops.adapter_sync_cmd = rkt_sync_cmd;
470         dev->a_ops.adapter_check_health = aac_rkt_check_health;
471         dev->a_ops.adapter_send = aac_rkt_send;
472
473         /*
474          *      First clear out all interrupts.  Then enable the one's that we
475          *      can handle.
476          */
477         rkt_writeb(dev, MUnit.OIMR, 0xff);
478         rkt_writel(dev, MUnit.ODR, 0xffffffff);
479         rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
480
481         if (aac_init_adapter(dev) == NULL)
482                 goto error_irq;
483         if (dev->new_comm_interface) {
484                 /*
485                  * FIB Setup has already been done, but we can minimize the
486                  * damage by at least ensuring the OS never issues more
487                  * commands than we can handle. The Rocket adapters currently
488                  * can only handle 246 commands and 8 AIFs at the same time,
489                  * and in fact do notify us accordingly if we negotiate the
490                  * FIB size. The problem that causes us to add this check is
491                  * to ensure that we do not overdo it with the adapter when a
492                  * hard coded FIB override is being utilized. This special
493                  * case warrants this half baked, but convenient, check here.
494                  */
495                 if (dev->scsi_host_ptr->can_queue > (246 - AAC_NUM_MGT_FIB)) {
496                         dev->init->MaxIoCommands = cpu_to_le32(246);
497                         dev->scsi_host_ptr->can_queue = 246 - AAC_NUM_MGT_FIB;
498                 }
499                 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xf7);
500         }
501         /*
502          *      Tell the adapter that all is configured, and it can start
503          *      accepting requests
504          */
505         aac_rkt_start_adapter(dev);
506         return 0;
507
508 error_irq:
509         rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
510         free_irq(dev->scsi_host_ptr->irq, (void *)dev);
511
512 error_iounmap:
513
514         return -1;
515 }