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  *  rx.c
26  *
27  * Abstract: Hardware miniport for Drawbridge specific hardware 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_rx_intr(int irq,void * dev_id,struct pt_regs * regs)48 static void aac_rx_intr(int irq, void *dev_id, struct pt_regs *regs)
49 {
50 	struct aac_dev *dev = dev_id;
51 	unsigned long bellbits;
52 	u8 intstat, mask;
53 	intstat = rx_readb(dev, MUnit.OISR);
54 	/*
55 	 *	Read mask and invert because drawbridge is reversed.
56 	 *	This allows us to only service interrupts that have
57 	 *	been enabled.
58 	 */
59 	mask = ~(rx_readb(dev, MUnit.OIMR));
60 	/* Check to see if this is our interrupt.  If it isn't just return */
61 	if (intstat & mask)
62 	{
63 		bellbits = rx_readl(dev, OutboundDoorbellReg);
64 		if (bellbits & DoorBellPrintfReady) {
65 			aac_printf(dev, le32_to_cpu(rx_readl (dev, IndexRegs.Mailbox[5])));
66 			rx_writel(dev, MUnit.ODR,DoorBellPrintfReady);
67 			rx_writel(dev, InboundDoorbellReg,DoorBellPrintfDone);
68 		}
69 		else if (bellbits & DoorBellAdapterNormCmdReady) {
70 			aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
71 			rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdReady);
72 		}
73 		else if (bellbits & DoorBellAdapterNormRespReady) {
74 			aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
75 			rx_writel(dev, MUnit.ODR,DoorBellAdapterNormRespReady);
76 		}
77 		else if (bellbits & DoorBellAdapterNormCmdNotFull) {
78 			rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
79 		}
80 		else if (bellbits & DoorBellAdapterNormRespNotFull) {
81 			rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
82 			rx_writel(dev, MUnit.ODR, DoorBellAdapterNormRespNotFull);
83 		}
84 	}
85 }
86 
87 /**
88  *	aac_rx_enable_interrupt	-	Enable event reporting
89  *	@dev: Adapter
90  *	@event: Event to enable
91  *
92  *	Enable event reporting from the i960 for a given event.
93  */
94 
aac_rx_enable_interrupt(struct aac_dev * dev,u32 event)95 static void aac_rx_enable_interrupt(struct aac_dev * dev, u32 event)
96 {
97 	switch (event) {
98 
99 	case HostNormCmdQue:
100 		dev->irq_mask &= ~(OUTBOUNDDOORBELL_1);
101 		break;
102 
103 	case HostNormRespQue:
104 		dev->irq_mask &= ~(OUTBOUNDDOORBELL_2);
105 		break;
106 
107 	case AdapNormCmdNotFull:
108 		dev->irq_mask &= ~(OUTBOUNDDOORBELL_3);
109 		break;
110 
111 	case AdapNormRespNotFull:
112 		dev->irq_mask &= ~(OUTBOUNDDOORBELL_4);
113 		break;
114 	}
115 }
116 
117 /**
118  *	aac_rx_disable_interrupt	-	Disable event reporting
119  *	@dev: Adapter
120  *	@event: Event to enable
121  *
122  *	Disable event reporting from the i960 for a given event.
123  */
124 
aac_rx_disable_interrupt(struct aac_dev * dev,u32 event)125 static void aac_rx_disable_interrupt(struct aac_dev *dev, u32 event)
126 {
127 	switch (event) {
128 
129 	case HostNormCmdQue:
130 		dev->irq_mask |= (OUTBOUNDDOORBELL_1);
131 		break;
132 
133 	case HostNormRespQue:
134 		dev->irq_mask |= (OUTBOUNDDOORBELL_2);
135 		break;
136 
137 	case AdapNormCmdNotFull:
138 		dev->irq_mask |= (OUTBOUNDDOORBELL_3);
139 		break;
140 
141 	case AdapNormRespNotFull:
142 		dev->irq_mask |= (OUTBOUNDDOORBELL_4);
143 		break;
144 	}
145 }
146 
147 /**
148  *	rx_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 comamnd to the adapter and wait
155  *	for its	completion.
156  */
157 
rx_sync_cmd(struct aac_dev * dev,u32 command,u32 p1,u32 * status)158 static int rx_sync_cmd(struct aac_dev *dev, u32 command, u32 p1, u32 *status)
159 {
160 	unsigned long start;
161 	int ok;
162 	/*
163 	 *	Write the command into Mailbox 0
164 	 */
165 	rx_writel(dev, InboundMailbox0, cpu_to_le32(command));
166 	/*
167 	 *	Write the parameters into Mailboxes 1 - 4
168 	 */
169 	rx_writel(dev, InboundMailbox1, cpu_to_le32(p1));
170 	rx_writel(dev, InboundMailbox2, 0);
171 	rx_writel(dev, InboundMailbox3, 0);
172 	rx_writel(dev, InboundMailbox4, 0);
173 	/*
174 	 *	Clear the synch command doorbell to start on a clean slate.
175 	 */
176 	rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
177 	/*
178 	 *	Disable doorbell interrupts
179 	 */
180 	rx_writeb(dev, MUnit.OIMR, rx_readb(dev, MUnit.OIMR) | 0x04);
181 	/*
182 	 *	Force the completion of the mask register write before issuing
183 	 *	the interrupt.
184 	 */
185 	rx_readb (dev, MUnit.OIMR);
186 	/*
187 	 *	Signal that there is a new synch command
188 	 */
189 	rx_writel(dev, InboundDoorbellReg, INBOUNDDOORBELL_0);
190 
191 	ok = 0;
192 	start = jiffies;
193 
194 	/*
195 	 *	Wait up to 30 seconds
196 	 */
197 	while (time_before(jiffies, start+30*HZ))
198 	{
199 		udelay(5);	/* Delay 5 microseconds to let Mon960 get info. */
200 		/*
201 		 *	Mon960 will set doorbell0 bit when it has completed the command.
202 		 */
203 		if (rx_readl(dev, OutboundDoorbellReg) & OUTBOUNDDOORBELL_0) {
204 			/*
205 			 *	Clear the doorbell.
206 			 */
207 			rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
208 			ok = 1;
209 			break;
210 		}
211 		/*
212 		 *	Yield the processor in case we are slow
213 		 */
214 		set_current_state(TASK_UNINTERRUPTIBLE);
215 		schedule_timeout(1);
216 	}
217 	if (ok != 1) {
218 		/*
219 		 *	Restore interrupt mask even though we timed out
220 		 */
221 		rx_writeb(dev, MUnit.OIMR, rx_readl(dev, MUnit.OIMR) & 0xfb);
222 		return -ETIMEDOUT;
223 	}
224 	/*
225 	 *	Pull the synch status from Mailbox 0.
226 	 */
227 	*status = le32_to_cpu(rx_readl(dev, IndexRegs.Mailbox[0]));
228 	/*
229 	 *	Clear the synch command doorbell.
230 	 */
231 	rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
232 	/*
233 	 *	Restore interrupt mask
234 	 */
235 	rx_writeb(dev, MUnit.OIMR, rx_readl(dev, MUnit.OIMR) & 0xfb);
236 	return 0;
237 
238 }
239 
240 /**
241  *	aac_rx_interrupt_adapter	-	interrupt adapter
242  *	@dev: Adapter
243  *
244  *	Send an interrupt to the i960 and breakpoint it.
245  */
246 
aac_rx_interrupt_adapter(struct aac_dev * dev)247 static void aac_rx_interrupt_adapter(struct aac_dev *dev)
248 {
249 	u32 ret;
250 	rx_sync_cmd(dev, BREAKPOINT_REQUEST, 0, &ret);
251 }
252 
253 /**
254  *	aac_rx_notify_adapter		-	send an event to the adapter
255  *	@dev: Adapter
256  *	@event: Event to send
257  *
258  *	Notify the i960 that something it probably cares about has
259  *	happened.
260  */
261 
aac_rx_notify_adapter(struct aac_dev * dev,u32 event)262 static void aac_rx_notify_adapter(struct aac_dev *dev, u32 event)
263 {
264 	switch (event) {
265 
266 	case AdapNormCmdQue:
267 		rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_1);
268 		break;
269 	case HostNormRespNotFull:
270 		rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_4);
271 		break;
272 	case AdapNormRespQue:
273 		rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_2);
274 		break;
275 	case HostNormCmdNotFull:
276 		rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_3);
277 		break;
278 	case HostShutdown:
279 //		rx_sync_cmd(dev, HOST_CRASHING, 0, 0, 0, 0, &ret);
280 		break;
281 	case FastIo:
282 		rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_6);
283 		break;
284 	case AdapPrintfDone:
285 		rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_5);
286 		break;
287 	default:
288 		BUG();
289 		break;
290 	}
291 }
292 
293 /**
294  *	aac_rx_start_adapter		-	activate adapter
295  *	@dev:	Adapter
296  *
297  *	Start up processing on an i960 based AAC adapter
298  */
299 
aac_rx_start_adapter(struct aac_dev * dev)300 static void aac_rx_start_adapter(struct aac_dev *dev)
301 {
302 	u32 status;
303 	struct aac_init *init;
304 
305 	init = dev->init;
306 	init->HostElapsedSeconds = cpu_to_le32(jiffies/HZ);
307 	/*
308 	 *	Tell the adapter we are back and up and running so it will scan
309 	 *	its command queues and enable our interrupts
310 	 */
311 	dev->irq_mask = (DoorBellPrintfReady | OUTBOUNDDOORBELL_1 | OUTBOUNDDOORBELL_2 | OUTBOUNDDOORBELL_3 | OUTBOUNDDOORBELL_4);
312 	/*
313 	 *	First clear out all interrupts.  Then enable the one's that we
314 	 *	can handle.
315 	 */
316 	rx_writeb(dev, MUnit.OIMR, 0xff);
317 	rx_writel(dev, MUnit.ODR, 0xffffffff);
318 //	rx_writeb(dev, MUnit.OIMR, ~(u8)OUTBOUND_DOORBELL_INTERRUPT_MASK);
319 	rx_writeb(dev, MUnit.OIMR, 0xfb);
320 
321 	// We can only use a 32 bit address here
322 	rx_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa, &status);
323 }
324 
325 /**
326  *	aac_rx_init	-	initialize an i960 based AAC card
327  *	@dev: device to configure
328  *	@devnum: adapter number
329  *
330  *	Allocate and set up resources for the i960 based AAC variants. The
331  *	device_interface in the commregion will be allocated and linked
332  *	to the comm region.
333  */
334 
aac_rx_init(struct aac_dev * dev,unsigned long num)335 int aac_rx_init(struct aac_dev *dev, unsigned long num)
336 {
337 	unsigned long start;
338 	unsigned long status;
339 	int instance;
340 	const char * name;
341 
342 	dev->devnum = num;
343 	instance = dev->id;
344 	name     = dev->name;
345 
346 	/*
347 	 *	Map in the registers from the adapter.
348 	 */
349 	if((dev->regs.rx = (struct rx_registers *)ioremap((unsigned long)dev->scsi_host_ptr->base, 8192))==NULL)
350 	{
351 		printk(KERN_WARNING "aacraid: unable to map i960.\n" );
352 		return -1;
353 	}
354 	/*
355 	 *	Check to see if the board failed any self tests.
356 	 */
357 	if (rx_readl(dev, IndexRegs.Mailbox[7]) & SELF_TEST_FAILED) {
358 		printk(KERN_ERR "%s%d: adapter self-test failed.\n", dev->name, instance);
359 		return -1;
360 	}
361 	/*
362 	 *	Check to see if the board panic'd while booting.
363 	 */
364 	if (rx_readl(dev, IndexRegs.Mailbox[7]) & KERNEL_PANIC) {
365 		printk(KERN_ERR "%s%d: adapter kernel panic'd.\n", dev->name, instance);
366 		return -1;
367 	}
368 	start = jiffies;
369 	/*
370 	 *	Wait for the adapter to be up and running. Wait up to 3 minutes
371 	 */
372 	while (!(rx_readl(dev, IndexRegs.Mailbox[7]) & KERNEL_UP_AND_RUNNING))
373 	{
374 		if(time_after(jiffies, start+180*HZ))
375 		{
376 			status = rx_readl(dev, IndexRegs.Mailbox[7]) >> 16;
377 			printk(KERN_ERR "%s%d: adapter kernel failed to start, init status = %ld.\n", dev->name, instance, status);
378 			return -1;
379 		}
380 		set_current_state(TASK_UNINTERRUPTIBLE);
381 		schedule_timeout(1);
382 	}
383 	if (request_irq(dev->scsi_host_ptr->irq, aac_rx_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev)<0)
384 	{
385 		printk(KERN_ERR "%s%d: Interrupt unavailable.\n", name, instance);
386 		return -1;
387 	}
388 	/*
389 	 *	Fill in the function dispatch table.
390 	 */
391 	dev->a_ops.adapter_interrupt = aac_rx_interrupt_adapter;
392 	dev->a_ops.adapter_enable_int = aac_rx_enable_interrupt;
393 	dev->a_ops.adapter_disable_int = aac_rx_disable_interrupt;
394 	dev->a_ops.adapter_notify = aac_rx_notify_adapter;
395 	dev->a_ops.adapter_sync_cmd = rx_sync_cmd;
396 
397 	if (aac_init_adapter(dev) == NULL)
398 		return -1;
399 	/*
400 	 *	Start any kernel threads needed
401 	 */
402 	dev->thread_pid = kernel_thread((int (*)(void *))aac_command_thread, dev, 0);
403 	if(dev->thread_pid < 0)
404 	{
405 		printk(KERN_ERR "aacraid: Unable to create rx thread.\n");
406 		return -1;
407 	}
408 	/*
409 	 *	Tell the adapter that all is configured, and it can start
410 	 *	accepting requests
411 	 */
412 	aac_rx_start_adapter(dev);
413 	return 0;
414 }
415