xref: /linux/drivers/scsi/aacraid/sa.c (revision 858259cf7d1c443c836a2022b78cb281f0a9b95e)
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  *  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>
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_sa_intr(int irq, void *dev_id, struct pt_regs *regs)
50 {
51 	struct aac_dev *dev = dev_id;
52 	unsigned short intstat, mask;
53 
54 	intstat = sa_readw(dev, DoorbellReg_p);
55 	/*
56 	 *	Read mask and invert because drawbridge is reversed.
57 	 *	This allows us to only service interrupts that have been enabled.
58 	 */
59 	mask = ~(sa_readw(dev, SaDbCSR.PRISETIRQMASK));
60 
61 	/* Check to see if this is our interrupt.  If it isn't just return */
62 
63 	if (intstat & mask) {
64 		if (intstat & PrintfReady) {
65 			aac_printf(dev, sa_readl(dev, Mailbox5));
66 			sa_writew(dev, DoorbellClrReg_p, PrintfReady); /* clear PrintfReady */
67 			sa_writew(dev, DoorbellReg_s, PrintfDone);
68 		} else if (intstat & DOORBELL_1) {	// dev -> Host Normal Command Ready
69 			aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
70 			sa_writew(dev, DoorbellClrReg_p, DOORBELL_1);
71 		} else if (intstat & DOORBELL_2) {	// dev -> Host Normal Response Ready
72 			aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
73 			sa_writew(dev, DoorbellClrReg_p, DOORBELL_2);
74 		} else if (intstat & DOORBELL_3) {	// dev -> Host Normal Command Not Full
75 			sa_writew(dev, DoorbellClrReg_p, DOORBELL_3);
76 		} else if (intstat & DOORBELL_4) {	// dev -> Host Normal Response Not Full
77 			sa_writew(dev, DoorbellClrReg_p, DOORBELL_4);
78 		}
79 		return IRQ_HANDLED;
80 	}
81 	return IRQ_NONE;
82 }
83 
84 /**
85  *	aac_sa_disable_interrupt	-	disable interrupt
86  *	@dev: Which adapter to enable.
87  */
88 
89 static void aac_sa_disable_interrupt (struct aac_dev *dev)
90 {
91 	sa_writew(dev, SaDbCSR.PRISETIRQMASK, 0xffff);
92 }
93 
94 /**
95  *	aac_sa_notify_adapter		-	handle adapter notification
96  *	@dev:	Adapter that notification is for
97  *	@event:	Event to notidy
98  *
99  *	Notify the adapter of an event
100  */
101 
102 static void aac_sa_notify_adapter(struct aac_dev *dev, u32 event)
103 {
104 	switch (event) {
105 
106 	case AdapNormCmdQue:
107 		sa_writew(dev, DoorbellReg_s,DOORBELL_1);
108 		break;
109 	case HostNormRespNotFull:
110 		sa_writew(dev, DoorbellReg_s,DOORBELL_4);
111 		break;
112 	case AdapNormRespQue:
113 		sa_writew(dev, DoorbellReg_s,DOORBELL_2);
114 		break;
115 	case HostNormCmdNotFull:
116 		sa_writew(dev, DoorbellReg_s,DOORBELL_3);
117 		break;
118 	case HostShutdown:
119 		/*
120 		sa_sync_cmd(dev, HOST_CRASHING, 0, 0, 0, 0, 0, 0,
121 		NULL, NULL, NULL, NULL, NULL);
122 		*/
123 		break;
124 	case FastIo:
125 		sa_writew(dev, DoorbellReg_s,DOORBELL_6);
126 		break;
127 	case AdapPrintfDone:
128 		sa_writew(dev, DoorbellReg_s,DOORBELL_5);
129 		break;
130 	default:
131 		BUG();
132 		break;
133 	}
134 }
135 
136 
137 /**
138  *	sa_sync_cmd	-	send a command and wait
139  *	@dev: Adapter
140  *	@command: Command to execute
141  *	@p1: first parameter
142  *	@ret: adapter status
143  *
144  *	This routine will send a synchronous command to the adapter and wait
145  *	for its	completion.
146  */
147 
148 static int sa_sync_cmd(struct aac_dev *dev, u32 command,
149 		u32 p1, u32 p2, u32 p3, u32 p4, u32 p5, u32 p6,
150 		u32 *ret, u32 *r1, u32 *r2, u32 *r3, u32 *r4)
151 {
152 	unsigned long start;
153  	int ok;
154 	/*
155 	 *	Write the Command into Mailbox 0
156 	 */
157 	sa_writel(dev, Mailbox0, command);
158 	/*
159 	 *	Write the parameters into Mailboxes 1 - 4
160 	 */
161 	sa_writel(dev, Mailbox1, p1);
162 	sa_writel(dev, Mailbox2, p2);
163 	sa_writel(dev, Mailbox3, p3);
164 	sa_writel(dev, Mailbox4, p4);
165 
166 	/*
167 	 *	Clear the synch command doorbell to start on a clean slate.
168 	 */
169 	sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
170 	/*
171 	 *	Signal that there is a new synch command
172 	 */
173 	sa_writew(dev, DoorbellReg_s, DOORBELL_0);
174 
175 	ok = 0;
176 	start = jiffies;
177 
178 	while(time_before(jiffies, start+30*HZ))
179 	{
180 		/*
181 		 *	Delay 5uS so that the monitor gets access
182 		 */
183 		udelay(5);
184 		/*
185 		 *	Mon110 will set doorbell0 bit when it has
186 		 *	completed the command.
187 		 */
188 		if(sa_readw(dev, DoorbellReg_p) & DOORBELL_0)  {
189 			ok = 1;
190 			break;
191 		}
192 		set_current_state(TASK_UNINTERRUPTIBLE);
193 		schedule_timeout(1);
194 	}
195 
196 	if (ok != 1)
197 		return -ETIMEDOUT;
198 	/*
199 	 *	Clear the synch command doorbell.
200 	 */
201 	sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
202 	/*
203 	 *	Pull the synch status from Mailbox 0.
204 	 */
205 	if (ret)
206 		*ret = sa_readl(dev, Mailbox0);
207 	if (r1)
208 		*r1 = sa_readl(dev, Mailbox1);
209 	if (r2)
210 		*r2 = sa_readl(dev, Mailbox2);
211 	if (r3)
212 		*r3 = sa_readl(dev, Mailbox3);
213 	if (r4)
214 		*r4 = sa_readl(dev, Mailbox4);
215 	return 0;
216 }
217 
218 /**
219  *	aac_sa_interrupt_adapter	-	interrupt an adapter
220  *	@dev: Which adapter to enable.
221  *
222  *	Breakpoint an adapter.
223  */
224 
225 static void aac_sa_interrupt_adapter (struct aac_dev *dev)
226 {
227 	sa_sync_cmd(dev, BREAKPOINT_REQUEST, 0, 0, 0, 0, 0, 0,
228 			NULL, NULL, NULL, NULL, NULL);
229 }
230 
231 /**
232  *	aac_sa_start_adapter		-	activate adapter
233  *	@dev:	Adapter
234  *
235  *	Start up processing on an ARM based AAC adapter
236  */
237 
238 static void aac_sa_start_adapter(struct aac_dev *dev)
239 {
240 	struct aac_init *init;
241 	/*
242 	 * Fill in the remaining pieces of the init.
243 	 */
244 	init = dev->init;
245 	init->HostElapsedSeconds = cpu_to_le32(get_seconds());
246 	/* We can only use a 32 bit address here */
247 	sa_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS,
248 			(u32)(ulong)dev->init_pa, 0, 0, 0, 0, 0,
249 			NULL, NULL, NULL, NULL, NULL);
250 }
251 
252 /**
253  *	aac_sa_check_health
254  *	@dev: device to check if healthy
255  *
256  *	Will attempt to determine if the specified adapter is alive and
257  *	capable of handling requests, returning 0 if alive.
258  */
259 static int aac_sa_check_health(struct aac_dev *dev)
260 {
261 	long status = sa_readl(dev, Mailbox7);
262 
263 	/*
264 	 *	Check to see if the board failed any self tests.
265 	 */
266 	if (status & SELF_TEST_FAILED)
267 		return -1;
268 	/*
269 	 *	Check to see if the board panic'd while booting.
270 	 */
271 	if (status & KERNEL_PANIC)
272 		return -2;
273 	/*
274 	 *	Wait for the adapter to be up and running. Wait up to 3 minutes
275 	 */
276 	if (!(status & KERNEL_UP_AND_RUNNING))
277 		return -3;
278 	/*
279 	 *	Everything is OK
280 	 */
281 	return 0;
282 }
283 
284 /**
285  *	aac_sa_init	-	initialize an ARM based AAC card
286  *	@dev: device to configure
287  *
288  *	Allocate and set up resources for the ARM based AAC variants. The
289  *	device_interface in the commregion will be allocated and linked
290  *	to the comm region.
291  */
292 
293 int aac_sa_init(struct aac_dev *dev)
294 {
295 	unsigned long start;
296 	unsigned long status;
297 	int instance;
298 	const char *name;
299 
300 	instance = dev->id;
301 	name     = dev->name;
302 
303 	/*
304 	 *	Check to see if the board failed any self tests.
305 	 */
306 	if (sa_readl(dev, Mailbox7) & SELF_TEST_FAILED) {
307 		printk(KERN_WARNING "%s%d: adapter self-test failed.\n", name, instance);
308 		goto error_iounmap;
309 	}
310 	/*
311 	 *	Check to see if the board panic'd while booting.
312 	 */
313 	if (sa_readl(dev, Mailbox7) & KERNEL_PANIC) {
314 		printk(KERN_WARNING "%s%d: adapter kernel panic'd.\n", name, instance);
315 		goto error_iounmap;
316 	}
317 	start = jiffies;
318 	/*
319 	 *	Wait for the adapter to be up and running. Wait up to 3 minutes.
320 	 */
321 	while (!(sa_readl(dev, Mailbox7) & KERNEL_UP_AND_RUNNING)) {
322 		if (time_after(jiffies, start+180*HZ)) {
323 			status = sa_readl(dev, Mailbox7);
324 			printk(KERN_WARNING "%s%d: adapter kernel failed to start, init status = %lx.\n",
325 					name, instance, status);
326 			goto error_iounmap;
327 		}
328 		set_current_state(TASK_UNINTERRUPTIBLE);
329 		schedule_timeout(1);
330 	}
331 
332 	if (request_irq(dev->scsi_host_ptr->irq, aac_sa_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev ) < 0) {
333 		printk(KERN_WARNING "%s%d: Interrupt unavailable.\n", name, instance);
334 		goto error_iounmap;
335 	}
336 
337 	/*
338 	 *	Fill in the function dispatch table.
339 	 */
340 
341 	dev->a_ops.adapter_interrupt = aac_sa_interrupt_adapter;
342 	dev->a_ops.adapter_disable_int = aac_sa_disable_interrupt;
343 	dev->a_ops.adapter_notify = aac_sa_notify_adapter;
344 	dev->a_ops.adapter_sync_cmd = sa_sync_cmd;
345 	dev->a_ops.adapter_check_health = aac_sa_check_health;
346 
347 	/*
348 	 *	First clear out all interrupts.  Then enable the one's that
349 	 *	we can handle.
350 	 */
351 	sa_writew(dev, SaDbCSR.PRISETIRQMASK, 0xffff);
352 	sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, (PrintfReady | DOORBELL_1 |
353 				DOORBELL_2 | DOORBELL_3 | DOORBELL_4));
354 
355 	if(aac_init_adapter(dev) == NULL)
356 		goto error_irq;
357 
358 	/*
359 	 *	Tell the adapter that all is configure, and it can start
360 	 *	accepting requests
361 	 */
362 	aac_sa_start_adapter(dev);
363 	return 0;
364 
365 error_irq:
366 	sa_writew(dev, SaDbCSR.PRISETIRQMASK, 0xffff);
367 	free_irq(dev->scsi_host_ptr->irq, (void *)dev);
368 
369 error_iounmap:
370 
371 	return -1;
372 }
373 
374