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/config.h>
32 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/types.h>
35 #include <linux/sched.h>
36 #include <linux/pci.h>
37 #include <linux/spinlock.h>
38 #include <linux/slab.h>
39 #include <linux/blk.h>
40 #include <linux/delay.h>
41 #include <linux/completion.h>
42 #include <asm/semaphore.h>
43 #include "scsi.h"
44 #include "hosts.h"
45 
46 #include "aacraid.h"
47 
aac_sa_intr(int irq,void * dev_id,struct pt_regs * regs)48 static void aac_sa_intr(int irq, void *dev_id, struct pt_regs *regs)
49 {
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, le32_to_cpu(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
68 			aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
69 			sa_writew(dev, DoorbellClrReg_p, DOORBELL_1);
70 		} else if (intstat & DOORBELL_2) {	// dev -> Host Normal Response Ready
71 			aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
72 			sa_writew(dev, DoorbellClrReg_p, DOORBELL_2);
73 		} 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 	}
79 }
80 
81 /**
82  *	aac_sa_enable_interrupt	-	enable an interrupt event
83  *	@dev: Which adapter to enable.
84  *	@event: Which adapter event.
85  *
86  *	This routine will enable the corresponding adapter event to cause an interrupt on
87  * 	the host.
88  */
89 
aac_sa_enable_interrupt(struct aac_dev * dev,u32 event)90 void aac_sa_enable_interrupt(struct aac_dev *dev, u32 event)
91 {
92 	switch (event) {
93 
94 	case HostNormCmdQue:
95 		sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_1);
96 		break;
97 
98 	case HostNormRespQue:
99 		sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_2);
100 		break;
101 
102 	case AdapNormCmdNotFull:
103 		sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_3);
104 		break;
105 
106 	case AdapNormRespNotFull:
107 		sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, DOORBELL_4);
108 		break;
109 	}
110 }
111 
112 /**
113  *	aac_sa_disable_interrupt	-	disable an interrupt event
114  *	@dev: Which adapter to enable.
115  *	@event: Which adapter event.
116  *
117  *	This routine will enable the corresponding adapter event to cause an interrupt on
118  * 	the host.
119  */
120 
aac_sa_disable_interrupt(struct aac_dev * dev,u32 event)121 void aac_sa_disable_interrupt (struct aac_dev *dev, u32 event)
122 {
123 	switch (event) {
124 
125 	case HostNormCmdQue:
126 		sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_1);
127 		break;
128 
129 	case HostNormRespQue:
130 		sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_2);
131 		break;
132 
133 	case AdapNormCmdNotFull:
134 		sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_3);
135 		break;
136 
137 	case AdapNormRespNotFull:
138 		sa_writew(dev, SaDbCSR.PRISETIRQMASK, DOORBELL_4);
139 		break;
140 	}
141 }
142 
143 /**
144  *	aac_sa_notify_adapter		-	handle adapter notification
145  *	@dev:	Adapter that notification is for
146  *	@event:	Event to notidy
147  *
148  *	Notify the adapter of an event
149  */
150 
aac_sa_notify_adapter(struct aac_dev * dev,u32 event)151 void aac_sa_notify_adapter(struct aac_dev *dev, u32 event)
152 {
153 	switch (event) {
154 
155 	case AdapNormCmdQue:
156 		sa_writew(dev, DoorbellReg_s,DOORBELL_1);
157 		break;
158 	case HostNormRespNotFull:
159 		sa_writew(dev, DoorbellReg_s,DOORBELL_4);
160 		break;
161 	case AdapNormRespQue:
162 		sa_writew(dev, DoorbellReg_s,DOORBELL_2);
163 		break;
164 	case HostNormCmdNotFull:
165 		sa_writew(dev, DoorbellReg_s,DOORBELL_3);
166 		break;
167 	case HostShutdown:
168 		//sa_sync_cmd(dev, HOST_CRASHING, 0, &ret);
169 		break;
170 	case FastIo:
171 		sa_writew(dev, DoorbellReg_s,DOORBELL_6);
172 		break;
173 	case AdapPrintfDone:
174 		sa_writew(dev, DoorbellReg_s,DOORBELL_5);
175 		break;
176 	default:
177 		BUG();
178 		break;
179 	}
180 }
181 
182 
183 /**
184  *	sa_sync_cmd	-	send a command and wait
185  *	@dev: Adapter
186  *	@command: Command to execute
187  *	@p1: first parameter
188  *	@ret: adapter status
189  *
190  *	This routine will send a synchronous comamnd to the adapter and wait
191  *	for its	completion.
192  */
193 
sa_sync_cmd(struct aac_dev * dev,u32 command,u32 p1,u32 * ret)194 static int sa_sync_cmd(struct aac_dev *dev, u32 command, u32 p1, u32 *ret)
195 {
196 	unsigned long start;
197  	int ok;
198 	/*
199 	 *	Write the Command into Mailbox 0
200 	 */
201 	sa_writel(dev, Mailbox0, cpu_to_le32(command));
202 	/*
203 	 *	Write the parameters into Mailboxes 1 - 4
204 	 */
205 	sa_writel(dev, Mailbox1, cpu_to_le32(p1));
206 	sa_writel(dev, Mailbox2, 0);
207 	sa_writel(dev, Mailbox3, 0);
208 	sa_writel(dev, Mailbox4, 0);
209 	/*
210 	 *	Clear the synch command doorbell to start on a clean slate.
211 	 */
212 	sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
213 	/*
214 	 *	Signal that there is a new synch command
215 	 */
216 	sa_writew(dev, DoorbellReg_s, DOORBELL_0);
217 
218 	ok = 0;
219 	start = jiffies;
220 
221 	while(time_before(jiffies, start+30*HZ))
222 	{
223 		/*
224 		 *	Delay 5uS so that the monitor gets access
225 		 */
226 		udelay(5);
227 		/*
228 		 *	Mon110 will set doorbell0 bit when it has
229 		 *	completed the command.
230 		 */
231 		if(sa_readw(dev, DoorbellReg_p) & DOORBELL_0)  {
232 			ok = 1;
233 			break;
234 		}
235 		set_current_state(TASK_UNINTERRUPTIBLE);
236 		schedule_timeout(1);
237 	}
238 
239 	if (ok != 1)
240 		return -ETIMEDOUT;
241 	/*
242 	 *	Clear the synch command doorbell.
243 	 */
244 	sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
245 	/*
246 	 *	Pull the synch status from Mailbox 0.
247 	 */
248 	*ret = le32_to_cpu(sa_readl(dev, Mailbox0));
249 	return 0;
250 }
251 
252 /**
253  *	aac_sa_interrupt_adapter	-	interrupt an adapter
254  *	@dev: Which adapter to enable.
255  *
256  *	Breakpoint an adapter.
257  */
258 
aac_sa_interrupt_adapter(struct aac_dev * dev)259 static void aac_sa_interrupt_adapter (struct aac_dev *dev)
260 {
261 	u32 ret;
262 	sa_sync_cmd(dev, BREAKPOINT_REQUEST, 0, &ret);
263 }
264 
265 /**
266  *	aac_sa_start_adapter		-	activate adapter
267  *	@dev:	Adapter
268  *
269  *	Start up processing on an ARM based AAC adapter
270  */
271 
aac_sa_start_adapter(struct aac_dev * dev)272 static void aac_sa_start_adapter(struct aac_dev *dev)
273 {
274 	u32 ret;
275 	struct aac_init *init;
276 	/*
277 	 * Fill in the remaining pieces of the init.
278 	 */
279 	init = dev->init;
280 	init->HostElapsedSeconds = cpu_to_le32(jiffies/HZ);
281 
282 	dprintk(("INIT\n"));
283 	/*
284 	 * Tell the adapter we are back and up and running so it will scan its command
285 	 * queues and enable our interrupts
286 	 */
287 	dev->irq_mask =	(PrintfReady | DOORBELL_1 | DOORBELL_2 | DOORBELL_3 | DOORBELL_4);
288 	/*
289 	 *	First clear out all interrupts.  Then enable the one's that
290 	 *	we can handle.
291 	 */
292 	dprintk(("MASK\n"));
293 	sa_writew(dev, SaDbCSR.PRISETIRQMASK, cpu_to_le16(0xffff));
294 	sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, (PrintfReady | DOORBELL_1 | DOORBELL_2 | DOORBELL_3 | DOORBELL_4));
295 	dprintk(("SYNCCMD\n"));
296 	/* We can only use a 32 bit address here */
297 	sa_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa, &ret);
298 }
299 
300 /**
301  *	aac_sa_init	-	initialize an ARM based AAC card
302  *	@dev: device to configure
303  *	@devnum: adapter number
304  *
305  *	Allocate and set up resources for the ARM based AAC variants. The
306  *	device_interface in the commregion will be allocated and linked
307  *	to the comm region.
308  */
309 
aac_sa_init(struct aac_dev * dev,unsigned long devnum)310 int aac_sa_init(struct aac_dev *dev, unsigned long devnum)
311 {
312 	unsigned long start;
313 	unsigned long status;
314 	int instance;
315 	const char *name;
316 
317 	dev->devnum = devnum;
318 
319 	dprintk(("PREINST\n"));
320 	instance = dev->id;
321 	name     = dev->name;
322 
323 	/*
324 	 *	Map in the registers from the adapter.
325 	 */
326 	dprintk(("PREMAP\n"));
327 
328 	if((dev->regs.sa = (struct sa_registers *)ioremap((unsigned long)dev->scsi_host_ptr->base, 8192))==NULL)
329 	{
330 		printk(KERN_WARNING "aacraid: unable to map ARM.\n" );
331 		return -1;
332 	}
333 	/*
334 	 *	Check to see if the board failed any self tests.
335 	 */
336 	if (sa_readl(dev, Mailbox7) & SELF_TEST_FAILED) {
337 		printk(KERN_WARNING "%s%d: adapter self-test failed.\n", name, instance);
338 		return -1;
339 	}
340 	/*
341 	 *	Check to see if the board panic'd while booting.
342 	 */
343 	if (sa_readl(dev, Mailbox7) & KERNEL_PANIC) {
344 		printk(KERN_WARNING "%s%d: adapter kernel panic'd.\n", name, instance);
345 		return -1;
346 	}
347 	start = jiffies;
348 	/*
349 	 *	Wait for the adapter to be up and running. Wait up to 3 minutes.
350 	 */
351 	while (!(sa_readl(dev, Mailbox7) & KERNEL_UP_AND_RUNNING)) {
352 		if (time_after(jiffies, start+180*HZ)) {
353 			status = sa_readl(dev, Mailbox7) >> 16;
354 			printk(KERN_WARNING "%s%d: adapter kernel failed to start, init status = %d.\n", name, instance, le32_to_cpu(status));
355 			return -1;
356 		}
357 		set_current_state(TASK_UNINTERRUPTIBLE);
358 		schedule_timeout(1);
359 	}
360 
361 	dprintk(("ATIRQ\n"));
362 	if (request_irq(dev->scsi_host_ptr->irq, aac_sa_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev ) < 0) {
363 		printk(KERN_WARNING "%s%d: Interrupt unavailable.\n", name, instance);
364 		return -1;
365 	}
366 
367 	/*
368 	 *	Fill in the function dispatch table.
369 	 */
370 
371 	dev->a_ops.adapter_interrupt = aac_sa_interrupt_adapter;
372 	dev->a_ops.adapter_enable_int = aac_sa_enable_interrupt;
373 	dev->a_ops.adapter_disable_int = aac_sa_disable_interrupt;
374 	dev->a_ops.adapter_notify = aac_sa_notify_adapter;
375 	dev->a_ops.adapter_sync_cmd = sa_sync_cmd;
376 
377 	dprintk(("FUNCDONE\n"));
378 
379 	if(aac_init_adapter(dev) == NULL)
380 		return -1;
381 
382 	dprintk(("NEWADAPTDONE\n"));
383 	/*
384 	 *	Start any kernel threads needed
385 	 */
386 	dev->thread_pid = kernel_thread((int (*)(void *))aac_command_thread, dev, 0);
387 	if (dev->thread_pid < 0) {
388 	     printk(KERN_ERR "aacraid: Unable to create command thread.\n");
389 	     return -1;
390 	}
391 
392 	/*
393 	 *	Tell the adapter that all is configure, and it can start
394 	 *	accepting requests
395 	 */
396 	dprintk(("STARTING\n"));
397 	aac_sa_start_adapter(dev);
398 	dprintk(("STARTED\n"));
399 	return 0;
400 }
401 
402