blob: 6f1a1780efce4e8e4a4a2f82564e81f498ad299f [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 * sa.c
26 *
27 * Abstract: Drawbridge specific support functions
28 *
29 */
30
31#include <linux/kernel.h>
32#include <linux/init.h>
33#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/pci.h>
35#include <linux/spinlock.h>
36#include <linux/slab.h>
37#include <linux/blkdev.h>
38#include <linux/delay.h>
39#include <linux/completion.h>
40#include <linux/time.h>
41#include <linux/interrupt.h>
42#include <asm/semaphore.h>
43
44#include <scsi/scsi_host.h>
45
46#include "aacraid.h"
47
David Howells7d12e782006-10-05 14:55:46 +010048static irqreturn_t aac_sa_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct aac_dev *dev = dev_id;
51 unsigned short intstat, mask;
52
53 intstat = sa_readw(dev, DoorbellReg_p);
54 /*
55 * Read mask and invert because drawbridge is reversed.
56 * This allows us to only service interrupts that have been enabled.
57 */
58 mask = ~(sa_readw(dev, SaDbCSR.PRISETIRQMASK));
59
60 /* Check to see if this is our interrupt. If it isn't just return */
61
62 if (intstat & mask) {
63 if (intstat & PrintfReady) {
64 aac_printf(dev, sa_readl(dev, Mailbox5));
65 sa_writew(dev, DoorbellClrReg_p, PrintfReady); /* clear PrintfReady */
66 sa_writew(dev, DoorbellReg_s, PrintfDone);
67 } else if (intstat & DOORBELL_1) { // dev -> Host Normal Command Ready
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 sa_writew(dev, DoorbellClrReg_p, DOORBELL_1);
Mark Haverkampdf3b7662006-05-10 09:13:11 -070069 aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 } else if (intstat & DOORBELL_2) { // dev -> Host Normal Response Ready
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 sa_writew(dev, DoorbellClrReg_p, DOORBELL_2);
Mark Haverkampdf3b7662006-05-10 09:13:11 -070072 aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 } else if (intstat & DOORBELL_3) { // dev -> Host Normal Command Not Full
74 sa_writew(dev, DoorbellClrReg_p, DOORBELL_3);
75 } else if (intstat & DOORBELL_4) { // dev -> Host Normal Response Not Full
76 sa_writew(dev, DoorbellClrReg_p, DOORBELL_4);
77 }
78 return IRQ_HANDLED;
79 }
80 return IRQ_NONE;
81}
82
83/**
Mark Haverkampbd1aac82005-08-03 15:39:01 -070084 * aac_sa_disable_interrupt - disable interrupt
85 * @dev: Which adapter to enable.
86 */
87
88static void aac_sa_disable_interrupt (struct aac_dev *dev)
89{
90 sa_writew(dev, SaDbCSR.PRISETIRQMASK, 0xffff);
91}
92
93/**
Mark Haverkamp28713322007-01-23 14:59:20 -080094 * aac_sa_enable_interrupt - enable interrupt
95 * @dev: Which adapter to enable.
96 */
97
98static void aac_sa_enable_interrupt (struct aac_dev *dev)
99{
100 sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, (PrintfReady | DOORBELL_1 |
101 DOORBELL_2 | DOORBELL_3 | DOORBELL_4));
102}
103
104/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * aac_sa_notify_adapter - handle adapter notification
106 * @dev: Adapter that notification is for
107 * @event: Event to notidy
108 *
109 * Notify the adapter of an event
110 */
111
Adrian Bunk 48338692005-04-25 19:45:58 -0700112static void aac_sa_notify_adapter(struct aac_dev *dev, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 switch (event) {
115
116 case AdapNormCmdQue:
117 sa_writew(dev, DoorbellReg_s,DOORBELL_1);
118 break;
119 case HostNormRespNotFull:
120 sa_writew(dev, DoorbellReg_s,DOORBELL_4);
121 break;
122 case AdapNormRespQue:
123 sa_writew(dev, DoorbellReg_s,DOORBELL_2);
124 break;
125 case HostNormCmdNotFull:
126 sa_writew(dev, DoorbellReg_s,DOORBELL_3);
127 break;
128 case HostShutdown:
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700129 /*
130 sa_sync_cmd(dev, HOST_CRASHING, 0, 0, 0, 0, 0, 0,
131 NULL, NULL, NULL, NULL, NULL);
132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 break;
134 case FastIo:
135 sa_writew(dev, DoorbellReg_s,DOORBELL_6);
136 break;
137 case AdapPrintfDone:
138 sa_writew(dev, DoorbellReg_s,DOORBELL_5);
139 break;
140 default:
141 BUG();
142 break;
143 }
144}
145
146
147/**
148 * sa_sync_cmd - send a command and wait
149 * @dev: Adapter
150 * @command: Command to execute
151 * @p1: first parameter
152 * @ret: adapter status
153 *
154 * This routine will send a synchronous command to the adapter and wait
155 * for its completion.
156 */
157
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700158static int sa_sync_cmd(struct aac_dev *dev, u32 command,
159 u32 p1, u32 p2, u32 p3, u32 p4, u32 p5, u32 p6,
160 u32 *ret, u32 *r1, u32 *r2, u32 *r3, u32 *r4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 unsigned long start;
163 int ok;
164 /*
165 * Write the Command into Mailbox 0
166 */
167 sa_writel(dev, Mailbox0, command);
168 /*
169 * Write the parameters into Mailboxes 1 - 4
170 */
171 sa_writel(dev, Mailbox1, p1);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700172 sa_writel(dev, Mailbox2, p2);
173 sa_writel(dev, Mailbox3, p3);
174 sa_writel(dev, Mailbox4, p4);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /*
177 * Clear the synch command doorbell to start on a clean slate.
178 */
179 sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
180 /*
181 * Signal that there is a new synch command
182 */
183 sa_writew(dev, DoorbellReg_s, DOORBELL_0);
184
185 ok = 0;
186 start = jiffies;
187
188 while(time_before(jiffies, start+30*HZ))
189 {
190 /*
191 * Delay 5uS so that the monitor gets access
192 */
193 udelay(5);
194 /*
195 * Mon110 will set doorbell0 bit when it has
196 * completed the command.
197 */
198 if(sa_readw(dev, DoorbellReg_p) & DOORBELL_0) {
199 ok = 1;
200 break;
201 }
Mark Haverkamp1241f352006-03-27 09:44:23 -0800202 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
205 if (ok != 1)
206 return -ETIMEDOUT;
207 /*
208 * Clear the synch command doorbell.
209 */
210 sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
211 /*
212 * Pull the synch status from Mailbox 0.
213 */
214 if (ret)
215 *ret = sa_readl(dev, Mailbox0);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700216 if (r1)
217 *r1 = sa_readl(dev, Mailbox1);
218 if (r2)
219 *r2 = sa_readl(dev, Mailbox2);
220 if (r3)
221 *r3 = sa_readl(dev, Mailbox3);
222 if (r4)
223 *r4 = sa_readl(dev, Mailbox4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
225}
226
227/**
228 * aac_sa_interrupt_adapter - interrupt an adapter
229 * @dev: Which adapter to enable.
230 *
231 * Breakpoint an adapter.
232 */
233
234static void aac_sa_interrupt_adapter (struct aac_dev *dev)
235{
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700236 sa_sync_cmd(dev, BREAKPOINT_REQUEST, 0, 0, 0, 0, 0, 0,
Mark Haverkampbd1aac82005-08-03 15:39:01 -0700237 NULL, NULL, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240/**
241 * aac_sa_start_adapter - activate adapter
242 * @dev: Adapter
243 *
244 * Start up processing on an ARM based AAC adapter
245 */
246
247static void aac_sa_start_adapter(struct aac_dev *dev)
248{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct aac_init *init;
250 /*
251 * Fill in the remaining pieces of the init.
252 */
253 init = dev->init;
254 init->HostElapsedSeconds = cpu_to_le32(get_seconds());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* We can only use a 32 bit address here */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700256 sa_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS,
257 (u32)(ulong)dev->init_pa, 0, 0, 0, 0, 0,
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700258 NULL, NULL, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/**
262 * aac_sa_check_health
263 * @dev: device to check if healthy
264 *
265 * Will attempt to determine if the specified adapter is alive and
266 * capable of handling requests, returning 0 if alive.
267 */
268static int aac_sa_check_health(struct aac_dev *dev)
269{
270 long status = sa_readl(dev, Mailbox7);
271
272 /*
273 * Check to see if the board failed any self tests.
274 */
275 if (status & SELF_TEST_FAILED)
276 return -1;
277 /*
278 * Check to see if the board panic'd while booting.
279 */
280 if (status & KERNEL_PANIC)
281 return -2;
282 /*
283 * Wait for the adapter to be up and running. Wait up to 3 minutes
284 */
285 if (!(status & KERNEL_UP_AND_RUNNING))
286 return -3;
287 /*
288 * Everything is OK
289 */
290 return 0;
291}
292
293/**
Mark Haverkamp76a7f8f2006-09-19 09:00:02 -0700294 * aac_sa_ioremap
295 * @size: mapping resize request
296 *
297 */
298static int aac_sa_ioremap(struct aac_dev * dev, u32 size)
299{
300 if (!size) {
301 iounmap(dev->regs.sa);
302 return 0;
303 }
304 dev->base = dev->regs.sa = ioremap(dev->scsi_host_ptr->base, size);
305 return (dev->base == NULL) ? -1 : 0;
306}
307
308/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * aac_sa_init - initialize an ARM based AAC card
310 * @dev: device to configure
311 *
312 * Allocate and set up resources for the ARM based AAC variants. The
313 * device_interface in the commregion will be allocated and linked
314 * to the comm region.
315 */
316
317int aac_sa_init(struct aac_dev *dev)
318{
319 unsigned long start;
320 unsigned long status;
321 int instance;
322 const char *name;
323
324 instance = dev->id;
325 name = dev->name;
326
Mark Haverkamp76a7f8f2006-09-19 09:00:02 -0700327 if (aac_sa_ioremap(dev, dev->base_size)) {
328 printk(KERN_WARNING "%s: unable to map adapter.\n", name);
329 goto error_iounmap;
330 }
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * Check to see if the board failed any self tests.
334 */
335 if (sa_readl(dev, Mailbox7) & SELF_TEST_FAILED) {
336 printk(KERN_WARNING "%s%d: adapter self-test failed.\n", name, instance);
337 goto error_iounmap;
338 }
339 /*
340 * Check to see if the board panic'd while booting.
341 */
342 if (sa_readl(dev, Mailbox7) & KERNEL_PANIC) {
343 printk(KERN_WARNING "%s%d: adapter kernel panic'd.\n", name, instance);
344 goto error_iounmap;
345 }
346 start = jiffies;
347 /*
348 * Wait for the adapter to be up and running. Wait up to 3 minutes.
349 */
350 while (!(sa_readl(dev, Mailbox7) & KERNEL_UP_AND_RUNNING)) {
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700351 if (time_after(jiffies, start+startup_timeout*HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 status = sa_readl(dev, Mailbox7);
353 printk(KERN_WARNING "%s%d: adapter kernel failed to start, init status = %lx.\n",
354 name, instance, status);
355 goto error_iounmap;
356 }
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700357 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /*
361 * Fill in the function dispatch table.
362 */
363
364 dev->a_ops.adapter_interrupt = aac_sa_interrupt_adapter;
Mark Haverkampbd1aac82005-08-03 15:39:01 -0700365 dev->a_ops.adapter_disable_int = aac_sa_disable_interrupt;
Mark Haverkamp28713322007-01-23 14:59:20 -0800366 dev->a_ops.adapter_enable_int = aac_sa_enable_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 dev->a_ops.adapter_notify = aac_sa_notify_adapter;
368 dev->a_ops.adapter_sync_cmd = sa_sync_cmd;
369 dev->a_ops.adapter_check_health = aac_sa_check_health;
Mark Haverkamp28713322007-01-23 14:59:20 -0800370 dev->a_ops.adapter_intr = aac_sa_intr;
Mark Haverkamp76a7f8f2006-09-19 09:00:02 -0700371 dev->a_ops.adapter_ioremap = aac_sa_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Mark Haverkampbd1aac82005-08-03 15:39:01 -0700373 /*
374 * First clear out all interrupts. Then enable the one's that
375 * we can handle.
376 */
Mark Haverkamp28713322007-01-23 14:59:20 -0800377 aac_adapter_disable_int(dev);
378 aac_adapter_enable_int(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if(aac_init_adapter(dev) == NULL)
381 goto error_irq;
Mark Haverkamp28713322007-01-23 14:59:20 -0800382 if (request_irq(dev->scsi_host_ptr->irq, dev->a_ops.adapter_intr,
383 IRQF_SHARED|IRQF_DISABLED,
384 "aacraid", (void *)dev ) < 0) {
385 printk(KERN_WARNING "%s%d: Interrupt unavailable.\n",
386 name, instance);
387 goto error_iounmap;
388 }
389 aac_adapter_enable_int(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * Tell the adapter that all is configure, and it can start
393 * accepting requests
394 */
395 aac_sa_start_adapter(dev);
396 return 0;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398error_irq:
Mark Haverkamp28713322007-01-23 14:59:20 -0800399 aac_sa_disable_interrupt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 free_irq(dev->scsi_host_ptr->irq, (void *)dev);
401
402error_iounmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 return -1;
405}
406