blob: 557287a0b80bdbfde17a349efb9aa7666cd7d7ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
49static irqreturn_t aac_rkt_intr(int irq, void *dev_id, struct pt_regs *regs)
50{
51 struct aac_dev *dev = dev_id;
52 unsigned long bellbits;
53 u8 intstat, mask;
54 intstat = rkt_readb(dev, MUnit.OISR);
55 /*
56 * Read mask and invert because drawbridge is reversed.
57 * This allows us to only service interrupts that have
58 * been enabled.
59 */
60 mask = ~(dev->OIMR);
61 /* Check to see if this is our interrupt. If it isn't just return */
62 if (intstat & mask)
63 {
64 bellbits = rkt_readl(dev, OutboundDoorbellReg);
65 if (bellbits & DoorBellPrintfReady) {
66 aac_printf(dev, rkt_readl(dev, IndexRegs.Mailbox[5]));
67 rkt_writel(dev, MUnit.ODR,DoorBellPrintfReady);
68 rkt_writel(dev, InboundDoorbellReg,DoorBellPrintfDone);
69 }
70 else if (bellbits & DoorBellAdapterNormCmdReady) {
71 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdReady);
72 aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
73 }
74 else if (bellbits & DoorBellAdapterNormRespReady) {
75 aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
76 rkt_writel(dev, MUnit.ODR,DoorBellAdapterNormRespReady);
77 }
78 else if (bellbits & DoorBellAdapterNormCmdNotFull) {
79 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
80 }
81 else if (bellbits & DoorBellAdapterNormRespNotFull) {
82 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
83 rkt_writel(dev, MUnit.ODR, DoorBellAdapterNormRespNotFull);
84 }
85 return IRQ_HANDLED;
86 }
87 return IRQ_NONE;
88}
89
90/**
Mark Haverkampbd1aac82005-08-03 15:39:01 -070091 * aac_rkt_disable_interrupt - Disable interrupts
92 * @dev: Adapter
93 */
94
95static void aac_rkt_disable_interrupt(struct aac_dev *dev)
96{
97 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
98}
99
100/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * rkt_sync_cmd - send a command and wait
102 * @dev: Adapter
103 * @command: Command to execute
104 * @p1: first parameter
105 * @ret: adapter status
106 *
107 * This routine will send a synchronous command to the adapter and wait
108 * for its completion.
109 */
110
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700111static int rkt_sync_cmd(struct aac_dev *dev, u32 command,
112 u32 p1, u32 p2, u32 p3, u32 p4, u32 p5, u32 p6,
113 u32 *status, u32 *r1, u32 *r2, u32 *r3, u32 *r4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 unsigned long start;
116 int ok;
117 /*
118 * Write the command into Mailbox 0
119 */
120 rkt_writel(dev, InboundMailbox0, command);
121 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700122 * Write the parameters into Mailboxes 1 - 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
124 rkt_writel(dev, InboundMailbox1, p1);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700125 rkt_writel(dev, InboundMailbox2, p2);
126 rkt_writel(dev, InboundMailbox3, p3);
127 rkt_writel(dev, InboundMailbox4, p4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /*
129 * Clear the synch command doorbell to start on a clean slate.
130 */
131 rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
132 /*
133 * Disable doorbell interrupts
134 */
135 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
136 /*
137 * Force the completion of the mask register write before issuing
138 * the interrupt.
139 */
140 rkt_readb (dev, MUnit.OIMR);
141 /*
142 * Signal that there is a new synch command
143 */
144 rkt_writel(dev, InboundDoorbellReg, INBOUNDDOORBELL_0);
145
146 ok = 0;
147 start = jiffies;
148
149 /*
150 * Wait up to 30 seconds
151 */
152 while (time_before(jiffies, start+30*HZ))
153 {
154 udelay(5); /* Delay 5 microseconds to let Mon960 get info. */
155 /*
156 * Mon960 will set doorbell0 bit when it has completed the command.
157 */
158 if (rkt_readl(dev, OutboundDoorbellReg) & OUTBOUNDDOORBELL_0) {
159 /*
160 * Clear the doorbell.
161 */
162 rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
163 ok = 1;
164 break;
165 }
166 /*
167 * Yield the processor in case we are slow
168 */
169 set_current_state(TASK_UNINTERRUPTIBLE);
170 schedule_timeout(1);
171 }
172 if (ok != 1) {
173 /*
174 * Restore interrupt mask even though we timed out
175 */
176 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
177 return -ETIMEDOUT;
178 }
179 /*
180 * Pull the synch status from Mailbox 0.
181 */
182 if (status)
183 *status = rkt_readl(dev, IndexRegs.Mailbox[0]);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700184 if (r1)
185 *r1 = rkt_readl(dev, IndexRegs.Mailbox[1]);
186 if (r2)
187 *r2 = rkt_readl(dev, IndexRegs.Mailbox[2]);
188 if (r3)
189 *r3 = rkt_readl(dev, IndexRegs.Mailbox[3]);
190 if (r4)
191 *r4 = rkt_readl(dev, IndexRegs.Mailbox[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * Clear the synch command doorbell.
194 */
195 rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
196 /*
197 * Restore interrupt mask
198 */
199 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
200 return 0;
201
202}
203
204/**
205 * aac_rkt_interrupt_adapter - interrupt adapter
206 * @dev: Adapter
207 *
208 * Send an interrupt to the i960 and breakpoint it.
209 */
210
211static void aac_rkt_interrupt_adapter(struct aac_dev *dev)
212{
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700213 rkt_sync_cmd(dev, BREAKPOINT_REQUEST, 0, 0, 0, 0, 0, 0,
214 NULL, NULL, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/**
218 * aac_rkt_notify_adapter - send an event to the adapter
219 * @dev: Adapter
220 * @event: Event to send
221 *
222 * Notify the i960 that something it probably cares about has
223 * happened.
224 */
225
226static void aac_rkt_notify_adapter(struct aac_dev *dev, u32 event)
227{
228 switch (event) {
229
230 case AdapNormCmdQue:
231 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_1);
232 break;
233 case HostNormRespNotFull:
234 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_4);
235 break;
236 case AdapNormRespQue:
237 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_2);
238 break;
239 case HostNormCmdNotFull:
240 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_3);
241 break;
242 case HostShutdown:
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700243// rkt_sync_cmd(dev, HOST_CRASHING, 0, 0, 0, 0, 0, 0,
244// NULL, NULL, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246 case FastIo:
247 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_6);
248 break;
249 case AdapPrintfDone:
250 rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_5);
251 break;
252 default:
253 BUG();
254 break;
255 }
256}
257
258/**
259 * aac_rkt_start_adapter - activate adapter
260 * @dev: Adapter
261 *
262 * Start up processing on an i960 based AAC adapter
263 */
264
265static void aac_rkt_start_adapter(struct aac_dev *dev)
266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct aac_init *init;
268
269 init = dev->init;
270 init->HostElapsedSeconds = cpu_to_le32(get_seconds());
271 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * First clear out all interrupts. Then enable the one's that we
273 * can handle.
274 */
275 rkt_writeb(dev, MUnit.OIMR, 0xff);
276 rkt_writel(dev, MUnit.ODR, 0xffffffff);
277// rkt_writeb(dev, MUnit.OIMR, ~(u8)OUTBOUND_DOORBELL_INTERRUPT_MASK);
278 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
279
280 // We can only use a 32 bit address here
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700281 rkt_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa,
282 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/**
286 * aac_rkt_check_health
287 * @dev: device to check if healthy
288 *
289 * Will attempt to determine if the specified adapter is alive and
290 * capable of handling requests, returning 0 if alive.
291 */
292static int aac_rkt_check_health(struct aac_dev *dev)
293{
294 u32 status = rkt_readl(dev, MUnit.OMRx[0]);
295
296 /*
297 * Check to see if the board failed any self tests.
298 */
299 if (status & SELF_TEST_FAILED)
300 return -1;
301 /*
302 * Check to see if the board panic'd.
303 */
304 if (status & KERNEL_PANIC) {
305 char * buffer;
306 struct POSTSTATUS {
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700307 __le32 Post_Command;
308 __le32 Post_Address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 } * post;
310 dma_addr_t paddr, baddr;
311 int ret;
312
313 if ((status & 0xFF000000L) == 0xBC000000L)
314 return (status >> 16) & 0xFF;
315 buffer = pci_alloc_consistent(dev->pdev, 512, &baddr);
316 ret = -2;
317 if (buffer == NULL)
318 return ret;
319 post = pci_alloc_consistent(dev->pdev,
320 sizeof(struct POSTSTATUS), &paddr);
321 if (post == NULL) {
322 pci_free_consistent(dev->pdev, 512, buffer, baddr);
323 return ret;
324 }
325 memset(buffer, 0, 512);
326 post->Post_Command = cpu_to_le32(COMMAND_POST_RESULTS);
327 post->Post_Address = cpu_to_le32(baddr);
328 rkt_writel(dev, MUnit.IMRx[0], paddr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700329 rkt_sync_cmd(dev, COMMAND_POST_RESULTS, baddr, 0, 0, 0, 0, 0,
330 NULL, NULL, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 pci_free_consistent(dev->pdev, sizeof(struct POSTSTATUS),
332 post, paddr);
333 if ((buffer[0] == '0') && (buffer[1] == 'x')) {
334 ret = (buffer[2] <= '9') ? (buffer[2] - '0') : (buffer[2] - 'A' + 10);
335 ret <<= 4;
336 ret += (buffer[3] <= '9') ? (buffer[3] - '0') : (buffer[3] - 'A' + 10);
337 }
338 pci_free_consistent(dev->pdev, 512, buffer, baddr);
339 return ret;
340 }
341 /*
342 * Wait for the adapter to be up and running.
343 */
344 if (!(status & KERNEL_UP_AND_RUNNING))
345 return -3;
346 /*
347 * Everything is OK
348 */
349 return 0;
350}
351
352/**
353 * aac_rkt_init - initialize an i960 based AAC card
354 * @dev: device to configure
355 *
356 * Allocate and set up resources for the i960 based AAC variants. The
357 * device_interface in the commregion will be allocated and linked
358 * to the comm region.
359 */
360
361int aac_rkt_init(struct aac_dev *dev)
362{
363 unsigned long start;
364 unsigned long status;
365 int instance;
366 const char * name;
367
368 instance = dev->id;
369 name = dev->name;
370
371 /*
372 * Map in the registers from the adapter.
373 */
374 if((dev->regs.rkt = ioremap((unsigned long)dev->scsi_host_ptr->base, 8192))==NULL)
375 {
376 printk(KERN_WARNING "aacraid: unable to map i960.\n" );
377 goto error_iounmap;
378 }
379 /*
380 * Check to see if the board failed any self tests.
381 */
382 if (rkt_readl(dev, MUnit.OMRx[0]) & SELF_TEST_FAILED) {
383 printk(KERN_ERR "%s%d: adapter self-test failed.\n", dev->name, instance);
384 goto error_iounmap;
385 }
386 /*
387 * Check to see if the monitor panic'd while booting.
388 */
389 if (rkt_readl(dev, MUnit.OMRx[0]) & MONITOR_PANIC) {
390 printk(KERN_ERR "%s%d: adapter monitor panic.\n", dev->name, instance);
391 goto error_iounmap;
392 }
393 /*
394 * Check to see if the board panic'd while booting.
395 */
396 if (rkt_readl(dev, MUnit.OMRx[0]) & KERNEL_PANIC) {
397 printk(KERN_ERR "%s%d: adapter kernel panic'd.\n", dev->name, instance);
398 goto error_iounmap;
399 }
400 start = jiffies;
401 /*
402 * Wait for the adapter to be up and running. Wait up to 3 minutes
403 */
404 while (!(rkt_readl(dev, MUnit.OMRx[0]) & KERNEL_UP_AND_RUNNING))
405 {
406 if(time_after(jiffies, start+180*HZ))
407 {
408 status = rkt_readl(dev, MUnit.OMRx[0]);
409 printk(KERN_ERR "%s%d: adapter kernel failed to start, init status = %lx.\n",
410 dev->name, instance, status);
411 goto error_iounmap;
412 }
413 set_current_state(TASK_UNINTERRUPTIBLE);
414 schedule_timeout(1);
415 }
416 if (request_irq(dev->scsi_host_ptr->irq, aac_rkt_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev)<0)
417 {
418 printk(KERN_ERR "%s%d: Interrupt unavailable.\n", name, instance);
419 goto error_iounmap;
420 }
421 /*
422 * Fill in the function dispatch table.
423 */
424 dev->a_ops.adapter_interrupt = aac_rkt_interrupt_adapter;
Mark Haverkampbd1aac82005-08-03 15:39:01 -0700425 dev->a_ops.adapter_disable_int = aac_rkt_disable_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 dev->a_ops.adapter_notify = aac_rkt_notify_adapter;
427 dev->a_ops.adapter_sync_cmd = rkt_sync_cmd;
428 dev->a_ops.adapter_check_health = aac_rkt_check_health;
429
Mark Haverkampbd1aac82005-08-03 15:39:01 -0700430 /*
431 * First clear out all interrupts. Then enable the one's that we
432 * can handle.
433 */
434 rkt_writeb(dev, MUnit.OIMR, 0xff);
435 rkt_writel(dev, MUnit.ODR, 0xffffffff);
436 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (aac_init_adapter(dev) == NULL)
439 goto error_irq;
440 /*
441 * Start any kernel threads needed
442 */
443 dev->thread_pid = kernel_thread((int (*)(void *))aac_command_thread, dev, 0);
444 if(dev->thread_pid < 0)
445 {
446 printk(KERN_ERR "aacraid: Unable to create rkt thread.\n");
447 goto error_kfree;
448 }
449 /*
450 * Tell the adapter that all is configured, and it can start
451 * accepting requests
452 */
453 aac_rkt_start_adapter(dev);
454 return 0;
455
456error_kfree:
457 kfree(dev->queues);
458
459error_irq:
Mark Haverkampbd1aac82005-08-03 15:39:01 -0700460 rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 free_irq(dev->scsi_host_ptr->irq, (void *)dev);
462
463error_iounmap:
464 iounmap(dev->regs.rkt);
465
466 return -1;
467}