xref: /linux/drivers/scsi/aacraid/rkt.c (revision 60b2737de1b1ddfdb90f3ba622634eb49d6f3603)
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  *  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 
49 static 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 /**
91  *	rkt_sync_cmd	-	send a command and wait
92  *	@dev: Adapter
93  *	@command: Command to execute
94  *	@p1: first parameter
95  *	@ret: adapter status
96  *
97  *	This routine will send a synchronous command to the adapter and wait
98  *	for its	completion.
99  */
100 
101 static int rkt_sync_cmd(struct aac_dev *dev, u32 command,
102 	u32 p1, u32 p2, u32 p3, u32 p4, u32 p5, u32 p6,
103 	u32 *status, u32 *r1, u32 *r2, u32 *r3, u32 *r4)
104 {
105 	unsigned long start;
106 	int ok;
107 	/*
108 	 *	Write the command into Mailbox 0
109 	 */
110 	rkt_writel(dev, InboundMailbox0, command);
111 	/*
112 	 *	Write the parameters into Mailboxes 1 - 6
113 	 */
114 	rkt_writel(dev, InboundMailbox1, p1);
115 	rkt_writel(dev, InboundMailbox2, p2);
116 	rkt_writel(dev, InboundMailbox3, p3);
117 	rkt_writel(dev, InboundMailbox4, p4);
118 	/*
119 	 *	Clear the synch command doorbell to start on a clean slate.
120 	 */
121 	rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
122 	/*
123 	 *	Disable doorbell interrupts
124 	 */
125 	rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
126 	/*
127 	 *	Force the completion of the mask register write before issuing
128 	 *	the interrupt.
129 	 */
130 	rkt_readb (dev, MUnit.OIMR);
131 	/*
132 	 *	Signal that there is a new synch command
133 	 */
134 	rkt_writel(dev, InboundDoorbellReg, INBOUNDDOORBELL_0);
135 
136 	ok = 0;
137 	start = jiffies;
138 
139 	/*
140 	 *	Wait up to 30 seconds
141 	 */
142 	while (time_before(jiffies, start+30*HZ))
143 	{
144 		udelay(5);	/* Delay 5 microseconds to let Mon960 get info. */
145 		/*
146 		 *	Mon960 will set doorbell0 bit when it has completed the command.
147 		 */
148 		if (rkt_readl(dev, OutboundDoorbellReg) & OUTBOUNDDOORBELL_0) {
149 			/*
150 			 *	Clear the doorbell.
151 			 */
152 			rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
153 			ok = 1;
154 			break;
155 		}
156 		/*
157 		 *	Yield the processor in case we are slow
158 		 */
159 		set_current_state(TASK_UNINTERRUPTIBLE);
160 		schedule_timeout(1);
161 	}
162 	if (ok != 1) {
163 		/*
164 		 *	Restore interrupt mask even though we timed out
165 		 */
166 		rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
167 		return -ETIMEDOUT;
168 	}
169 	/*
170 	 *	Pull the synch status from Mailbox 0.
171 	 */
172 	if (status)
173 		*status = rkt_readl(dev, IndexRegs.Mailbox[0]);
174 	if (r1)
175 		*r1 = rkt_readl(dev, IndexRegs.Mailbox[1]);
176 	if (r2)
177 		*r2 = rkt_readl(dev, IndexRegs.Mailbox[2]);
178 	if (r3)
179 		*r3 = rkt_readl(dev, IndexRegs.Mailbox[3]);
180 	if (r4)
181 		*r4 = rkt_readl(dev, IndexRegs.Mailbox[4]);
182 	/*
183 	 *	Clear the synch command doorbell.
184 	 */
185 	rkt_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
186 	/*
187 	 *	Restore interrupt mask
188 	 */
189 	rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
190 	return 0;
191 
192 }
193 
194 /**
195  *	aac_rkt_interrupt_adapter	-	interrupt adapter
196  *	@dev: Adapter
197  *
198  *	Send an interrupt to the i960 and breakpoint it.
199  */
200 
201 static void aac_rkt_interrupt_adapter(struct aac_dev *dev)
202 {
203 	rkt_sync_cmd(dev, BREAKPOINT_REQUEST, 0, 0, 0, 0, 0, 0,
204 	  NULL, NULL, NULL, NULL, NULL);
205 }
206 
207 /**
208  *	aac_rkt_notify_adapter		-	send an event to the adapter
209  *	@dev: Adapter
210  *	@event: Event to send
211  *
212  *	Notify the i960 that something it probably cares about has
213  *	happened.
214  */
215 
216 static void aac_rkt_notify_adapter(struct aac_dev *dev, u32 event)
217 {
218 	switch (event) {
219 
220 	case AdapNormCmdQue:
221 		rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_1);
222 		break;
223 	case HostNormRespNotFull:
224 		rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_4);
225 		break;
226 	case AdapNormRespQue:
227 		rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_2);
228 		break;
229 	case HostNormCmdNotFull:
230 		rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_3);
231 		break;
232 	case HostShutdown:
233 //		rkt_sync_cmd(dev, HOST_CRASHING, 0, 0, 0, 0, 0, 0,
234 //		  NULL, NULL, NULL, NULL, NULL);
235 		break;
236 	case FastIo:
237 		rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_6);
238 		break;
239 	case AdapPrintfDone:
240 		rkt_writel(dev, MUnit.IDR,INBOUNDDOORBELL_5);
241 		break;
242 	default:
243 		BUG();
244 		break;
245 	}
246 }
247 
248 /**
249  *	aac_rkt_start_adapter		-	activate adapter
250  *	@dev:	Adapter
251  *
252  *	Start up processing on an i960 based AAC adapter
253  */
254 
255 static void aac_rkt_start_adapter(struct aac_dev *dev)
256 {
257 	struct aac_init *init;
258 
259 	init = dev->init;
260 	init->HostElapsedSeconds = cpu_to_le32(get_seconds());
261 	/*
262 	 *	First clear out all interrupts.  Then enable the one's that we
263 	 *	can handle.
264 	 */
265 	rkt_writeb(dev, MUnit.OIMR, 0xff);
266 	rkt_writel(dev, MUnit.ODR, 0xffffffff);
267 //	rkt_writeb(dev, MUnit.OIMR, ~(u8)OUTBOUND_DOORBELL_INTERRUPT_MASK);
268 	rkt_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
269 
270 	// We can only use a 32 bit address here
271 	rkt_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa,
272 	  0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
273 }
274 
275 /**
276  *	aac_rkt_check_health
277  *	@dev: device to check if healthy
278  *
279  *	Will attempt to determine if the specified adapter is alive and
280  *	capable of handling requests, returning 0 if alive.
281  */
282 static int aac_rkt_check_health(struct aac_dev *dev)
283 {
284 	u32 status = rkt_readl(dev, MUnit.OMRx[0]);
285 
286 	/*
287 	 *	Check to see if the board failed any self tests.
288 	 */
289 	if (status & SELF_TEST_FAILED)
290 		return -1;
291 	/*
292 	 *	Check to see if the board panic'd.
293 	 */
294 	if (status & KERNEL_PANIC) {
295 		char * buffer;
296 		struct POSTSTATUS {
297 			__le32 Post_Command;
298 			__le32 Post_Address;
299 		} * post;
300 		dma_addr_t paddr, baddr;
301 		int ret;
302 
303 		if ((status & 0xFF000000L) == 0xBC000000L)
304 			return (status >> 16) & 0xFF;
305 		buffer = pci_alloc_consistent(dev->pdev, 512, &baddr);
306 		ret = -2;
307 		if (buffer == NULL)
308 			return ret;
309 		post = pci_alloc_consistent(dev->pdev,
310 		  sizeof(struct POSTSTATUS), &paddr);
311 		if (post == NULL) {
312 			pci_free_consistent(dev->pdev, 512, buffer, baddr);
313 			return ret;
314 		}
315                 memset(buffer, 0, 512);
316 		post->Post_Command = cpu_to_le32(COMMAND_POST_RESULTS);
317                 post->Post_Address = cpu_to_le32(baddr);
318                 rkt_writel(dev, MUnit.IMRx[0], paddr);
319                 rkt_sync_cmd(dev, COMMAND_POST_RESULTS, baddr, 0, 0, 0, 0, 0,
320 		  NULL, NULL, NULL, NULL, NULL);
321 		pci_free_consistent(dev->pdev, sizeof(struct POSTSTATUS),
322 		  post, paddr);
323                 if ((buffer[0] == '0') && (buffer[1] == 'x')) {
324                         ret = (buffer[2] <= '9') ? (buffer[2] - '0') : (buffer[2] - 'A' + 10);
325                         ret <<= 4;
326                         ret += (buffer[3] <= '9') ? (buffer[3] - '0') : (buffer[3] - 'A' + 10);
327                 }
328 		pci_free_consistent(dev->pdev, 512, buffer, baddr);
329                 return ret;
330         }
331 	/*
332 	 *	Wait for the adapter to be up and running.
333 	 */
334 	if (!(status & KERNEL_UP_AND_RUNNING))
335 		return -3;
336 	/*
337 	 *	Everything is OK
338 	 */
339 	return 0;
340 }
341 
342 /**
343  *	aac_rkt_init	-	initialize an i960 based AAC card
344  *	@dev: device to configure
345  *
346  *	Allocate and set up resources for the i960 based AAC variants. The
347  *	device_interface in the commregion will be allocated and linked
348  *	to the comm region.
349  */
350 
351 int aac_rkt_init(struct aac_dev *dev)
352 {
353 	unsigned long start;
354 	unsigned long status;
355 	int instance;
356 	const char * name;
357 
358 	instance = dev->id;
359 	name     = dev->name;
360 
361 	/*
362 	 *	Map in the registers from the adapter.
363 	 */
364 	if((dev->regs.rkt = ioremap((unsigned long)dev->scsi_host_ptr->base, 8192))==NULL)
365 	{
366 		printk(KERN_WARNING "aacraid: unable to map i960.\n" );
367 		goto error_iounmap;
368 	}
369 	/*
370 	 *	Check to see if the board failed any self tests.
371 	 */
372 	if (rkt_readl(dev, MUnit.OMRx[0]) & SELF_TEST_FAILED) {
373 		printk(KERN_ERR "%s%d: adapter self-test failed.\n", dev->name, instance);
374 		goto error_iounmap;
375 	}
376 	/*
377 	 *	Check to see if the monitor panic'd while booting.
378 	 */
379 	if (rkt_readl(dev, MUnit.OMRx[0]) & MONITOR_PANIC) {
380 		printk(KERN_ERR "%s%d: adapter monitor panic.\n", dev->name, instance);
381 		goto error_iounmap;
382 	}
383 	/*
384 	 *	Check to see if the board panic'd while booting.
385 	 */
386 	if (rkt_readl(dev, MUnit.OMRx[0]) & KERNEL_PANIC) {
387 		printk(KERN_ERR "%s%d: adapter kernel panic'd.\n", dev->name, instance);
388 		goto error_iounmap;
389 	}
390 	start = jiffies;
391 	/*
392 	 *	Wait for the adapter to be up and running. Wait up to 3 minutes
393 	 */
394 	while (!(rkt_readl(dev, MUnit.OMRx[0]) & KERNEL_UP_AND_RUNNING))
395 	{
396 		if(time_after(jiffies, start+180*HZ))
397 		{
398 			status = rkt_readl(dev, MUnit.OMRx[0]);
399 			printk(KERN_ERR "%s%d: adapter kernel failed to start, init status = %lx.\n",
400 					dev->name, instance, status);
401 			goto error_iounmap;
402 		}
403 		set_current_state(TASK_UNINTERRUPTIBLE);
404 		schedule_timeout(1);
405 	}
406 	if (request_irq(dev->scsi_host_ptr->irq, aac_rkt_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev)<0)
407 	{
408 		printk(KERN_ERR "%s%d: Interrupt unavailable.\n", name, instance);
409 		goto error_iounmap;
410 	}
411 	/*
412 	 *	Fill in the function dispatch table.
413 	 */
414 	dev->a_ops.adapter_interrupt = aac_rkt_interrupt_adapter;
415 	dev->a_ops.adapter_notify = aac_rkt_notify_adapter;
416 	dev->a_ops.adapter_sync_cmd = rkt_sync_cmd;
417 	dev->a_ops.adapter_check_health = aac_rkt_check_health;
418 
419 	if (aac_init_adapter(dev) == NULL)
420 		goto error_irq;
421 	/*
422 	 *	Start any kernel threads needed
423 	 */
424 	dev->thread_pid = kernel_thread((int (*)(void *))aac_command_thread, dev, 0);
425 	if(dev->thread_pid < 0)
426 	{
427 		printk(KERN_ERR "aacraid: Unable to create rkt thread.\n");
428 		goto error_kfree;
429 	}
430 	/*
431 	 *	Tell the adapter that all is configured, and it can start
432 	 *	accepting requests
433 	 */
434 	aac_rkt_start_adapter(dev);
435 	return 0;
436 
437 error_kfree:
438 	kfree(dev->queues);
439 
440 error_irq:
441 	free_irq(dev->scsi_host_ptr->irq, (void *)dev);
442 
443 error_iounmap:
444 	iounmap(dev->regs.rkt);
445 
446 	return -1;
447 }
448