xref: /openbmc/linux/drivers/scsi/arm/acornscsi.c (revision ca48739e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/drivers/acorn/scsi/acornscsi.c
4  *
5  *  Acorn SCSI 3 driver
6  *  By R.M.King.
7  *
8  * Abandoned using the Select and Transfer command since there were
9  * some nasty races between our software and the target devices that
10  * were not easy to solve, and the device errata had a lot of entries
11  * for this command, some of them quite nasty...
12  *
13  * Changelog:
14  *  26-Sep-1997	RMK	Re-jigged to use the queue module.
15  *			Re-coded state machine to be based on driver
16  *			state not scsi state.  Should be easier to debug.
17  *			Added acornscsi_release to clean up properly.
18  *			Updated proc/scsi reporting.
19  *  05-Oct-1997	RMK	Implemented writing to SCSI devices.
20  *  06-Oct-1997	RMK	Corrected small (non-serious) bug with the connect/
21  *			reconnect race condition causing a warning message.
22  *  12-Oct-1997	RMK	Added catch for re-entering interrupt routine.
23  *  15-Oct-1997	RMK	Improved handling of commands.
24  *  27-Jun-1998	RMK	Changed asm/delay.h to linux/delay.h.
25  *  13-Dec-1998	RMK	Better abort code and command handling.  Extra state
26  *			transitions added to allow dodgy devices to work.
27  */
28 #define DEBUG_NO_WRITE	1
29 #define DEBUG_QUEUES	2
30 #define DEBUG_DMA	4
31 #define DEBUG_ABORT	8
32 #define DEBUG_DISCON	16
33 #define DEBUG_CONNECT	32
34 #define DEBUG_PHASES	64
35 #define DEBUG_WRITE	128
36 #define DEBUG_LINK	256
37 #define DEBUG_MESSAGES	512
38 #define DEBUG_RESET	1024
39 #define DEBUG_ALL	(DEBUG_RESET|DEBUG_MESSAGES|DEBUG_LINK|DEBUG_WRITE|\
40 			 DEBUG_PHASES|DEBUG_CONNECT|DEBUG_DISCON|DEBUG_ABORT|\
41 			 DEBUG_DMA|DEBUG_QUEUES)
42 
43 /* DRIVER CONFIGURATION
44  *
45  * SCSI-II Tagged queue support.
46  *
47  * I don't have any SCSI devices that support it, so it is totally untested
48  * (except to make sure that it doesn't interfere with any non-tagging
49  * devices).  It is not fully implemented either - what happens when a
50  * tagging device reconnects???
51  *
52  * You can tell if you have a device that supports tagged queueing my
53  * cating (eg) /proc/scsi/acornscsi/0 and see if the SCSI revision is reported
54  * as '2 TAG'.
55  *
56  * Also note that CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE is normally set in the config
57  * scripts, but disabled here.  Once debugged, remove the #undef, otherwise to debug,
58  * comment out the undef.
59  */
60 #undef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
61 /*
62  * SCSI-II Synchronous transfer support.
63  *
64  * Tried and tested...
65  *
66  * SDTR_SIZE	  - maximum number of un-acknowledged bytes (0 = off, 12 = max)
67  * SDTR_PERIOD	  - period of REQ signal (min=125, max=1020)
68  * DEFAULT_PERIOD - default REQ period.
69  */
70 #define SDTR_SIZE	12
71 #define SDTR_PERIOD	125
72 #define DEFAULT_PERIOD	500
73 
74 /*
75  * Debugging information
76  *
77  * DEBUG	  - bit mask from list above
78  * DEBUG_TARGET   - is defined to the target number if you want to debug
79  *		    a specific target. [only recon/write/dma].
80  */
81 #define DEBUG (DEBUG_RESET|DEBUG_WRITE|DEBUG_NO_WRITE)
82 /* only allow writing to SCSI device 0 */
83 #define NO_WRITE 0xFE
84 /*#define DEBUG_TARGET 2*/
85 /*
86  * Select timeout time (in 10ms units)
87  *
88  * This is the timeout used between the start of selection and the WD33C93
89  * chip deciding that the device isn't responding.
90  */
91 #define TIMEOUT_TIME 10
92 /*
93  * Define this if you want to have verbose explanation of SCSI
94  * status/messages.
95  */
96 #undef CONFIG_ACORNSCSI_CONSTANTS
97 /*
98  * Define this if you want to use the on board DMAC [don't remove this option]
99  * If not set, then use PIO mode (not currently supported).
100  */
101 #define USE_DMAC
102 
103 /*
104  * ====================================================================================
105  */
106 
107 #ifdef DEBUG_TARGET
108 #define DBG(cmd,xxx...) \
109   if (cmd->device->id == DEBUG_TARGET) { \
110     xxx; \
111   }
112 #else
113 #define DBG(cmd,xxx...) xxx
114 #endif
115 
116 #include <linux/module.h>
117 #include <linux/kernel.h>
118 #include <linux/string.h>
119 #include <linux/signal.h>
120 #include <linux/errno.h>
121 #include <linux/proc_fs.h>
122 #include <linux/ioport.h>
123 #include <linux/blkdev.h>
124 #include <linux/delay.h>
125 #include <linux/interrupt.h>
126 #include <linux/init.h>
127 #include <linux/bitops.h>
128 #include <linux/stringify.h>
129 #include <linux/io.h>
130 
131 #include <asm/ecard.h>
132 
133 #include "../scsi.h"
134 #include <scsi/scsi_dbg.h>
135 #include <scsi/scsi_host.h>
136 #include <scsi/scsi_transport_spi.h>
137 #include "acornscsi.h"
138 #include "msgqueue.h"
139 #include "scsi.h"
140 
141 #include <scsi/scsicam.h>
142 
143 #define VER_MAJOR 2
144 #define VER_MINOR 0
145 #define VER_PATCH 6
146 
147 #ifdef USE_DMAC
148 /*
149  * DMAC setup parameters
150  */
151 #define INIT_DEVCON0	(DEVCON0_RQL|DEVCON0_EXW|DEVCON0_CMP)
152 #define INIT_DEVCON1	(DEVCON1_BHLD)
153 #define DMAC_READ	(MODECON_READ)
154 #define DMAC_WRITE	(MODECON_WRITE)
155 #define INIT_SBICDMA	(CTRL_DMABURST)
156 
157 #define scsi_xferred	have_data_in
158 
159 /*
160  * Size of on-board DMA buffer
161  */
162 #define DMAC_BUFFER_SIZE	65536
163 #endif
164 
165 #define STATUS_BUFFER_TO_PRINT	24
166 
167 unsigned int sdtr_period = SDTR_PERIOD;
168 unsigned int sdtr_size   = SDTR_SIZE;
169 
170 static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
171 			   unsigned int result);
172 static int acornscsi_reconnect_finish(AS_Host *host);
173 static void acornscsi_dma_cleanup(AS_Host *host);
174 static void acornscsi_abortcmd(AS_Host *host, unsigned char tag);
175 
176 /* ====================================================================================
177  * Miscellaneous
178  */
179 
180 /* Offsets from MEMC base */
181 #define SBIC_REGIDX	0x2000
182 #define SBIC_REGVAL	0x2004
183 #define DMAC_OFFSET	0x3000
184 
185 /* Offsets from FAST IOC base */
186 #define INT_REG		0x2000
187 #define PAGE_REG	0x3000
188 
189 static inline void sbic_arm_write(AS_Host *host, unsigned int reg, unsigned int value)
190 {
191     writeb(reg, host->base + SBIC_REGIDX);
192     writeb(value, host->base + SBIC_REGVAL);
193 }
194 
195 static inline int sbic_arm_read(AS_Host *host, unsigned int reg)
196 {
197     if(reg == SBIC_ASR)
198 	   return readl(host->base + SBIC_REGIDX) & 255;
199     writeb(reg, host->base + SBIC_REGIDX);
200     return readl(host->base + SBIC_REGVAL) & 255;
201 }
202 
203 #define sbic_arm_writenext(host, val)	writeb((val), (host)->base + SBIC_REGVAL)
204 #define sbic_arm_readnext(host) 	readb((host)->base + SBIC_REGVAL)
205 
206 #ifdef USE_DMAC
207 #define dmac_read(host,reg) \
208 	readb((host)->base + DMAC_OFFSET + ((reg) << 2))
209 
210 #define dmac_write(host,reg,value) \
211 	({ writeb((value), (host)->base + DMAC_OFFSET + ((reg) << 2)); })
212 
213 #define dmac_clearintr(host) 	writeb(0, (host)->fast + INT_REG)
214 
215 static inline unsigned int dmac_address(AS_Host *host)
216 {
217     return dmac_read(host, DMAC_TXADRHI) << 16 |
218 	   dmac_read(host, DMAC_TXADRMD) << 8 |
219 	   dmac_read(host, DMAC_TXADRLO);
220 }
221 
222 static
223 void acornscsi_dumpdma(AS_Host *host, char *where)
224 {
225 	unsigned int mode, addr, len;
226 
227 	mode = dmac_read(host, DMAC_MODECON);
228 	addr = dmac_address(host);
229 	len  = dmac_read(host, DMAC_TXCNTHI) << 8 |
230 	       dmac_read(host, DMAC_TXCNTLO);
231 
232 	printk("scsi%d: %s: DMAC %02x @%06x+%04x msk %02x, ",
233 		host->host->host_no, where,
234 		mode, addr, (len + 1) & 0xffff,
235 		dmac_read(host, DMAC_MASKREG));
236 
237 	printk("DMA @%06x, ", host->dma.start_addr);
238 	printk("BH @%p +%04x, ", host->scsi.SCp.ptr,
239 		host->scsi.SCp.this_residual);
240 	printk("DT @+%04x ST @+%04x", host->dma.transferred,
241 		host->scsi.SCp.scsi_xferred);
242 	printk("\n");
243 }
244 #endif
245 
246 static
247 unsigned long acornscsi_sbic_xfcount(AS_Host *host)
248 {
249     unsigned long length;
250 
251     length = sbic_arm_read(host, SBIC_TRANSCNTH) << 16;
252     length |= sbic_arm_readnext(host) << 8;
253     length |= sbic_arm_readnext(host);
254 
255     return length;
256 }
257 
258 static int
259 acornscsi_sbic_wait(AS_Host *host, int stat_mask, int stat, int timeout, char *msg)
260 {
261 	int asr;
262 
263 	do {
264 		asr = sbic_arm_read(host, SBIC_ASR);
265 
266 		if ((asr & stat_mask) == stat)
267 			return 0;
268 
269 		udelay(1);
270 	} while (--timeout);
271 
272 	printk("scsi%d: timeout while %s\n", host->host->host_no, msg);
273 
274 	return -1;
275 }
276 
277 static
278 int acornscsi_sbic_issuecmd(AS_Host *host, int command)
279 {
280     if (acornscsi_sbic_wait(host, ASR_CIP, 0, 1000, "issuing command"))
281 	return -1;
282 
283     sbic_arm_write(host, SBIC_CMND, command);
284 
285     return 0;
286 }
287 
288 static void
289 acornscsi_csdelay(unsigned int cs)
290 {
291     unsigned long target_jiffies, flags;
292 
293     target_jiffies = jiffies + 1 + cs * HZ / 100;
294 
295     local_save_flags(flags);
296     local_irq_enable();
297 
298     while (time_before(jiffies, target_jiffies)) barrier();
299 
300     local_irq_restore(flags);
301 }
302 
303 static
304 void acornscsi_resetcard(AS_Host *host)
305 {
306     unsigned int i, timeout;
307 
308     /* assert reset line */
309     host->card.page_reg = 0x80;
310     writeb(host->card.page_reg, host->fast + PAGE_REG);
311 
312     /* wait 3 cs.  SCSI standard says 25ms. */
313     acornscsi_csdelay(3);
314 
315     host->card.page_reg = 0;
316     writeb(host->card.page_reg, host->fast + PAGE_REG);
317 
318     /*
319      * Should get a reset from the card
320      */
321     timeout = 1000;
322     do {
323 	if (readb(host->fast + INT_REG) & 8)
324 	    break;
325 	udelay(1);
326     } while (--timeout);
327 
328     if (timeout == 0)
329 	printk("scsi%d: timeout while resetting card\n",
330 		host->host->host_no);
331 
332     sbic_arm_read(host, SBIC_ASR);
333     sbic_arm_read(host, SBIC_SSR);
334 
335     /* setup sbic - WD33C93A */
336     sbic_arm_write(host, SBIC_OWNID, OWNID_EAF | host->host->this_id);
337     sbic_arm_write(host, SBIC_CMND, CMND_RESET);
338 
339     /*
340      * Command should cause a reset interrupt
341      */
342     timeout = 1000;
343     do {
344 	if (readb(host->fast + INT_REG) & 8)
345 	    break;
346 	udelay(1);
347     } while (--timeout);
348 
349     if (timeout == 0)
350 	printk("scsi%d: timeout while resetting card\n",
351 		host->host->host_no);
352 
353     sbic_arm_read(host, SBIC_ASR);
354     if (sbic_arm_read(host, SBIC_SSR) != 0x01)
355 	printk(KERN_CRIT "scsi%d: WD33C93A didn't give enhanced reset interrupt\n",
356 		host->host->host_no);
357 
358     sbic_arm_write(host, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
359     sbic_arm_write(host, SBIC_TIMEOUT, TIMEOUT_TIME);
360     sbic_arm_write(host, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
361     sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
362 
363     host->card.page_reg = 0x40;
364     writeb(host->card.page_reg, host->fast + PAGE_REG);
365 
366     /* setup dmac - uPC71071 */
367     dmac_write(host, DMAC_INIT, 0);
368 #ifdef USE_DMAC
369     dmac_write(host, DMAC_INIT, INIT_8BIT);
370     dmac_write(host, DMAC_CHANNEL, CHANNEL_0);
371     dmac_write(host, DMAC_DEVCON0, INIT_DEVCON0);
372     dmac_write(host, DMAC_DEVCON1, INIT_DEVCON1);
373 #endif
374 
375     host->SCpnt = NULL;
376     host->scsi.phase = PHASE_IDLE;
377     host->scsi.disconnectable = 0;
378 
379     memset(host->busyluns, 0, sizeof(host->busyluns));
380 
381     for (i = 0; i < 8; i++) {
382 	host->device[i].sync_state = SYNC_NEGOCIATE;
383 	host->device[i].disconnect_ok = 1;
384     }
385 
386     /* wait 25 cs.  SCSI standard says 250ms. */
387     acornscsi_csdelay(25);
388 }
389 
390 /*=============================================================================================
391  * Utility routines (eg. debug)
392  */
393 #ifdef CONFIG_ACORNSCSI_CONSTANTS
394 static char *acornscsi_interrupttype[] = {
395   "rst",  "suc",  "p/a",  "3",
396   "term", "5",	  "6",	  "7",
397   "serv", "9",	  "a",	  "b",
398   "c",	  "d",	  "e",	  "f"
399 };
400 
401 static signed char acornscsi_map[] = {
402   0,  1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
403  -1,  2, -1, -1,  -1, -1,  3, -1,   4,	5,  6,	7,   8,  9, 10, 11,
404  12, 13, 14, -1,  -1, -1, -1, -1,   4,	5,  6,	7,   8,  9, 10, 11,
405  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
406  15, 16, 17, 18,  19, -1, -1, 20,   4,	5,  6,	7,   8,  9, 10, 11,
407  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
408  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
409  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
410  21, 22, -1, -1,  -1, 23, -1, -1,   4,	5,  6,	7,   8,  9, 10, 11,
411  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
412  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
413  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
414  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
415  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
416  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
417  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1
418 };
419 
420 static char *acornscsi_interruptcode[] = {
421     /* 0 */
422     "reset - normal mode",	/* 00 */
423     "reset - advanced mode",	/* 01 */
424 
425     /* 2 */
426     "sel",			/* 11 */
427     "sel+xfer", 		/* 16 */
428     "data-out", 		/* 18 */
429     "data-in",			/* 19 */
430     "cmd",			/* 1A */
431     "stat",			/* 1B */
432     "??-out",			/* 1C */
433     "??-in",			/* 1D */
434     "msg-out",			/* 1E */
435     "msg-in",			/* 1F */
436 
437     /* 12 */
438     "/ACK asserted",		/* 20 */
439     "save-data-ptr",		/* 21 */
440     "{re}sel",			/* 22 */
441 
442     /* 15 */
443     "inv cmd",			/* 40 */
444     "unexpected disconnect",	/* 41 */
445     "sel timeout",		/* 42 */
446     "P err",			/* 43 */
447     "P err+ATN",		/* 44 */
448     "bad status byte",		/* 47 */
449 
450     /* 21 */
451     "resel, no id",		/* 80 */
452     "resel",			/* 81 */
453     "discon",			/* 85 */
454 };
455 
456 static
457 void print_scsi_status(unsigned int ssr)
458 {
459     if (acornscsi_map[ssr] != -1)
460 	printk("%s:%s",
461 		acornscsi_interrupttype[(ssr >> 4)],
462 		acornscsi_interruptcode[acornscsi_map[ssr]]);
463     else
464 	printk("%X:%X", ssr >> 4, ssr & 0x0f);
465 }
466 #endif
467 
468 static
469 void print_sbic_status(int asr, int ssr, int cmdphase)
470 {
471 #ifdef CONFIG_ACORNSCSI_CONSTANTS
472     printk("sbic: %c%c%c%c%c%c ",
473 	    asr & ASR_INT ? 'I' : 'i',
474 	    asr & ASR_LCI ? 'L' : 'l',
475 	    asr & ASR_BSY ? 'B' : 'b',
476 	    asr & ASR_CIP ? 'C' : 'c',
477 	    asr & ASR_PE  ? 'P' : 'p',
478 	    asr & ASR_DBR ? 'D' : 'd');
479     printk("scsi: ");
480     print_scsi_status(ssr);
481     printk(" ph %02X\n", cmdphase);
482 #else
483     printk("sbic: %02X scsi: %X:%X ph: %02X\n",
484 	    asr, (ssr & 0xf0)>>4, ssr & 0x0f, cmdphase);
485 #endif
486 }
487 
488 static void
489 acornscsi_dumplogline(AS_Host *host, int target, int line)
490 {
491 	unsigned long prev;
492 	signed int ptr;
493 
494 	ptr = host->status_ptr[target] - STATUS_BUFFER_TO_PRINT;
495 	if (ptr < 0)
496 		ptr += STATUS_BUFFER_SIZE;
497 
498 	printk("%c: %3s:", target == 8 ? 'H' : '0' + target,
499 		line == 0 ? "ph" : line == 1 ? "ssr" : "int");
500 
501 	prev = host->status[target][ptr].when;
502 
503 	for (; ptr != host->status_ptr[target]; ptr = (ptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
504 		unsigned long time_diff;
505 
506 		if (!host->status[target][ptr].when)
507 			continue;
508 
509 		switch (line) {
510 		case 0:
511 			printk("%c%02X", host->status[target][ptr].irq ? '-' : ' ',
512 					 host->status[target][ptr].ph);
513 			break;
514 
515 		case 1:
516 			printk(" %02X", host->status[target][ptr].ssr);
517 			break;
518 
519 		case 2:
520 			time_diff = host->status[target][ptr].when - prev;
521 			prev = host->status[target][ptr].when;
522 			if (time_diff == 0)
523 				printk("==^");
524 			else if (time_diff >= 100)
525 				printk("   ");
526 			else
527 				printk(" %02ld", time_diff);
528 			break;
529 		}
530 	}
531 
532 	printk("\n");
533 }
534 
535 static
536 void acornscsi_dumplog(AS_Host *host, int target)
537 {
538     do {
539 	acornscsi_dumplogline(host, target, 0);
540 	acornscsi_dumplogline(host, target, 1);
541 	acornscsi_dumplogline(host, target, 2);
542 
543 	if (target == 8)
544 	    break;
545 
546 	target = 8;
547     } while (1);
548 }
549 
550 static
551 char acornscsi_target(AS_Host *host)
552 {
553 	if (host->SCpnt)
554 		return '0' + host->SCpnt->device->id;
555 	return 'H';
556 }
557 
558 /*
559  * Prototype: cmdtype_t acornscsi_cmdtype(int command)
560  * Purpose  : differentiate READ from WRITE from other commands
561  * Params   : command - command to interpret
562  * Returns  : CMD_READ	- command reads data,
563  *	      CMD_WRITE - command writes data,
564  *	      CMD_MISC	- everything else
565  */
566 static inline
567 cmdtype_t acornscsi_cmdtype(int command)
568 {
569     switch (command) {
570     case WRITE_6:  case WRITE_10:  case WRITE_12:
571 	return CMD_WRITE;
572     case READ_6:   case READ_10:   case READ_12:
573 	return CMD_READ;
574     default:
575 	return CMD_MISC;
576     }
577 }
578 
579 /*
580  * Prototype: int acornscsi_datadirection(int command)
581  * Purpose  : differentiate between commands that have a DATA IN phase
582  *	      and a DATA OUT phase
583  * Params   : command - command to interpret
584  * Returns  : DATADIR_OUT - data out phase expected
585  *	      DATADIR_IN  - data in phase expected
586  */
587 static
588 datadir_t acornscsi_datadirection(int command)
589 {
590     switch (command) {
591     case CHANGE_DEFINITION:	case COMPARE:		case COPY:
592     case COPY_VERIFY:		case LOG_SELECT:	case MODE_SELECT:
593     case MODE_SELECT_10:	case SEND_DIAGNOSTIC:	case WRITE_BUFFER:
594     case FORMAT_UNIT:		case REASSIGN_BLOCKS:	case RESERVE:
595     case SEARCH_EQUAL:		case SEARCH_HIGH:	case SEARCH_LOW:
596     case WRITE_6:		case WRITE_10:		case WRITE_VERIFY:
597     case UPDATE_BLOCK:		case WRITE_LONG:	case WRITE_SAME:
598     case SEARCH_HIGH_12:	case SEARCH_EQUAL_12:	case SEARCH_LOW_12:
599     case WRITE_12:		case WRITE_VERIFY_12:	case SET_WINDOW:
600     case MEDIUM_SCAN:		case SEND_VOLUME_TAG:	case 0xea:
601 	return DATADIR_OUT;
602     default:
603 	return DATADIR_IN;
604     }
605 }
606 
607 /*
608  * Purpose  : provide values for synchronous transfers with 33C93.
609  * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
610  *	Modified by Russell King for 8MHz WD33C93A
611  */
612 static struct sync_xfer_tbl {
613     unsigned int period_ns;
614     unsigned char reg_value;
615 } sync_xfer_table[] = {
616     {	1, 0x20 },    { 249, 0x20 },	{ 374, 0x30 },
617     { 499, 0x40 },    { 624, 0x50 },	{ 749, 0x60 },
618     { 874, 0x70 },    { 999, 0x00 },	{   0,	  0 }
619 };
620 
621 /*
622  * Prototype: int acornscsi_getperiod(unsigned char syncxfer)
623  * Purpose  : period for the synchronous transfer setting
624  * Params   : syncxfer SYNCXFER register value
625  * Returns  : period in ns.
626  */
627 static
628 int acornscsi_getperiod(unsigned char syncxfer)
629 {
630     int i;
631 
632     syncxfer &= 0xf0;
633     if (syncxfer == 0x10)
634 	syncxfer = 0;
635 
636     for (i = 1; sync_xfer_table[i].period_ns; i++)
637 	if (syncxfer == sync_xfer_table[i].reg_value)
638 	    return sync_xfer_table[i].period_ns;
639     return 0;
640 }
641 
642 /*
643  * Prototype: int round_period(unsigned int period)
644  * Purpose  : return index into above table for a required REQ period
645  * Params   : period - time (ns) for REQ
646  * Returns  : table index
647  * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
648  */
649 static inline
650 int round_period(unsigned int period)
651 {
652     int i;
653 
654     for (i = 1; sync_xfer_table[i].period_ns; i++) {
655 	if ((period <= sync_xfer_table[i].period_ns) &&
656 	    (period > sync_xfer_table[i - 1].period_ns))
657 	    return i;
658     }
659     return 7;
660 }
661 
662 /*
663  * Prototype: unsigned char calc_sync_xfer(unsigned int period, unsigned int offset)
664  * Purpose  : calculate value for 33c93s SYNC register
665  * Params   : period - time (ns) for REQ
666  *	      offset - offset in bytes between REQ/ACK
667  * Returns  : value for SYNC register
668  * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
669  */
670 static
671 unsigned char __maybe_unused calc_sync_xfer(unsigned int period,
672 					    unsigned int offset)
673 {
674     return sync_xfer_table[round_period(period)].reg_value |
675 		((offset < SDTR_SIZE) ? offset : SDTR_SIZE);
676 }
677 
678 /* ====================================================================================
679  * Command functions
680  */
681 /*
682  * Function: acornscsi_kick(AS_Host *host)
683  * Purpose : kick next command to interface
684  * Params  : host - host to send command to
685  * Returns : INTR_IDLE if idle, otherwise INTR_PROCESSING
686  * Notes   : interrupts are always disabled!
687  */
688 static
689 intr_ret_t acornscsi_kick(AS_Host *host)
690 {
691     int from_queue = 0;
692     struct scsi_cmnd *SCpnt;
693 
694     /* first check to see if a command is waiting to be executed */
695     SCpnt = host->origSCpnt;
696     host->origSCpnt = NULL;
697 
698     /* retrieve next command */
699     if (!SCpnt) {
700 	SCpnt = queue_remove_exclude(&host->queues.issue, host->busyluns);
701 	if (!SCpnt)
702 	    return INTR_IDLE;
703 
704 	from_queue = 1;
705     }
706 
707     if (host->scsi.disconnectable && host->SCpnt) {
708 	queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
709 	host->scsi.disconnectable = 0;
710 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
711 	DBG(host->SCpnt, printk("scsi%d.%c: moved command to disconnected queue\n",
712 		host->host->host_no, acornscsi_target(host)));
713 #endif
714 	host->SCpnt = NULL;
715     }
716 
717     /*
718      * If we have an interrupt pending, then we may have been reselected.
719      * In this case, we don't want to write to the registers
720      */
721     if (!(sbic_arm_read(host, SBIC_ASR) & (ASR_INT|ASR_BSY|ASR_CIP))) {
722 	sbic_arm_write(host, SBIC_DESTID, SCpnt->device->id);
723 	sbic_arm_write(host, SBIC_CMND, CMND_SELWITHATN);
724     }
725 
726     /*
727      * claim host busy - all of these must happen atomically wrt
728      * our interrupt routine.  Failure means command loss.
729      */
730     host->scsi.phase = PHASE_CONNECTING;
731     host->SCpnt = SCpnt;
732     host->scsi.SCp = SCpnt->SCp;
733     host->dma.xfer_setup = 0;
734     host->dma.xfer_required = 0;
735     host->dma.xfer_done = 0;
736 
737 #if (DEBUG & (DEBUG_ABORT|DEBUG_CONNECT))
738     DBG(SCpnt,printk("scsi%d.%c: starting cmd %02X\n",
739 	    host->host->host_no, '0' + SCpnt->device->id,
740 	    SCpnt->cmnd[0]));
741 #endif
742 
743     if (from_queue) {
744 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
745 	/*
746 	 * tagged queueing - allocate a new tag to this command
747 	 */
748 	if (SCpnt->device->simple_tags) {
749 	    SCpnt->device->current_tag += 1;
750 	    if (SCpnt->device->current_tag == 0)
751 		SCpnt->device->current_tag = 1;
752 	    SCpnt->tag = SCpnt->device->current_tag;
753 	} else
754 #endif
755 	    set_bit(SCpnt->device->id * 8 +
756 		    (u8)(SCpnt->device->lun & 0x07), host->busyluns);
757 
758 	host->stats.removes += 1;
759 
760 	switch (acornscsi_cmdtype(SCpnt->cmnd[0])) {
761 	case CMD_WRITE:
762 	    host->stats.writes += 1;
763 	    break;
764 	case CMD_READ:
765 	    host->stats.reads += 1;
766 	    break;
767 	case CMD_MISC:
768 	    host->stats.miscs += 1;
769 	    break;
770 	}
771     }
772 
773     return INTR_PROCESSING;
774 }
775 
776 /*
777  * Function: void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp, unsigned int result)
778  * Purpose : complete processing for command
779  * Params  : host   - interface that completed
780  *	     result - driver byte of result
781  */
782 static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
783 			   unsigned int result)
784 {
785 	struct scsi_cmnd *SCpnt = *SCpntp;
786 
787     /* clean up */
788     sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
789 
790     host->stats.fins += 1;
791 
792     if (SCpnt) {
793 	*SCpntp = NULL;
794 
795 	acornscsi_dma_cleanup(host);
796 
797 	SCpnt->result = result << 16 | host->scsi.SCp.Message << 8 | host->scsi.SCp.Status;
798 
799 	/*
800 	 * In theory, this should not happen.  In practice, it seems to.
801 	 * Only trigger an error if the device attempts to report all happy
802 	 * but with untransferred buffers...  If we don't do something, then
803 	 * data loss will occur.  Should we check SCpnt->underflow here?
804 	 * It doesn't appear to be set to something meaningful by the higher
805 	 * levels all the time.
806 	 */
807 	if (result == DID_OK) {
808 		int xfer_warn = 0;
809 
810 		if (SCpnt->underflow == 0) {
811 			if (host->scsi.SCp.ptr &&
812 			    acornscsi_cmdtype(SCpnt->cmnd[0]) != CMD_MISC)
813 				xfer_warn = 1;
814 		} else {
815 			if (host->scsi.SCp.scsi_xferred < SCpnt->underflow ||
816 			    host->scsi.SCp.scsi_xferred != host->dma.transferred)
817 				xfer_warn = 1;
818 		}
819 
820 		/* ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.6)
821 		 *  Targets which break data transfers into multiple
822 		 *  connections shall end each successful connection
823 		 *  (except possibly the last) with a SAVE DATA
824 		 *  POINTER - DISCONNECT message sequence.
825 		 *
826 		 * This makes it difficult to ensure that a transfer has
827 		 * completed.  If we reach the end of a transfer during
828 		 * the command, then we can only have finished the transfer.
829 		 * therefore, if we seem to have some data remaining, this
830 		 * is not a problem.
831 		 */
832 		if (host->dma.xfer_done)
833 			xfer_warn = 0;
834 
835 		if (xfer_warn) {
836 		    switch (status_byte(SCpnt->result)) {
837 		    case CHECK_CONDITION:
838 		    case COMMAND_TERMINATED:
839 		    case BUSY:
840 		    case QUEUE_FULL:
841 		    case RESERVATION_CONFLICT:
842 			break;
843 
844 		    default:
845 			scmd_printk(KERN_ERR, SCpnt,
846 				    "incomplete data transfer detected: "
847 				    "result=%08X", SCpnt->result);
848 			scsi_print_command(SCpnt);
849 			acornscsi_dumpdma(host, "done");
850 			acornscsi_dumplog(host, SCpnt->device->id);
851 			set_host_byte(SCpnt, DID_ERROR);
852 		    }
853 		}
854 	}
855 
856 	if (!SCpnt->scsi_done)
857 	    panic("scsi%d.H: null scsi_done function in acornscsi_done", host->host->host_no);
858 
859 	clear_bit(SCpnt->device->id * 8 +
860 		  (u8)(SCpnt->device->lun & 0x7), host->busyluns);
861 
862 	SCpnt->scsi_done(SCpnt);
863     } else
864 	printk("scsi%d: null command in acornscsi_done", host->host->host_no);
865 
866     host->scsi.phase = PHASE_IDLE;
867 }
868 
869 /* ====================================================================================
870  * DMA routines
871  */
872 /*
873  * Purpose  : update SCSI Data Pointer
874  * Notes    : this will only be one SG entry or less
875  */
876 static
877 void acornscsi_data_updateptr(AS_Host *host, struct scsi_pointer *SCp, unsigned int length)
878 {
879     SCp->ptr += length;
880     SCp->this_residual -= length;
881 
882     if (SCp->this_residual == 0 && next_SCp(SCp) == 0)
883 	host->dma.xfer_done = 1;
884 }
885 
886 /*
887  * Prototype: void acornscsi_data_read(AS_Host *host, char *ptr,
888  *				unsigned int start_addr, unsigned int length)
889  * Purpose  : read data from DMA RAM
890  * Params   : host - host to transfer from
891  *	      ptr  - DRAM address
892  *	      start_addr - host mem address
893  *	      length - number of bytes to transfer
894  * Notes    : this will only be one SG entry or less
895  */
896 static
897 void acornscsi_data_read(AS_Host *host, char *ptr,
898 				 unsigned int start_addr, unsigned int length)
899 {
900     extern void __acornscsi_in(void __iomem *, char *buf, int len);
901     unsigned int page, offset, len = length;
902 
903     page = (start_addr >> 12);
904     offset = start_addr & ((1 << 12) - 1);
905 
906     writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
907 
908     while (len > 0) {
909 	unsigned int this_len;
910 
911 	if (len + offset > (1 << 12))
912 	    this_len = (1 << 12) - offset;
913 	else
914 	    this_len = len;
915 
916 	__acornscsi_in(host->base + (offset << 1), ptr, this_len);
917 
918 	offset += this_len;
919 	ptr += this_len;
920 	len -= this_len;
921 
922 	if (offset == (1 << 12)) {
923 	    offset = 0;
924 	    page ++;
925 	    writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
926 	}
927     }
928     writeb(host->card.page_reg, host->fast + PAGE_REG);
929 }
930 
931 /*
932  * Prototype: void acornscsi_data_write(AS_Host *host, char *ptr,
933  *				unsigned int start_addr, unsigned int length)
934  * Purpose  : write data to DMA RAM
935  * Params   : host - host to transfer from
936  *	      ptr  - DRAM address
937  *	      start_addr - host mem address
938  *	      length - number of bytes to transfer
939  * Notes    : this will only be one SG entry or less
940  */
941 static
942 void acornscsi_data_write(AS_Host *host, char *ptr,
943 				 unsigned int start_addr, unsigned int length)
944 {
945     extern void __acornscsi_out(void __iomem *, char *buf, int len);
946     unsigned int page, offset, len = length;
947 
948     page = (start_addr >> 12);
949     offset = start_addr & ((1 << 12) - 1);
950 
951     writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
952 
953     while (len > 0) {
954 	unsigned int this_len;
955 
956 	if (len + offset > (1 << 12))
957 	    this_len = (1 << 12) - offset;
958 	else
959 	    this_len = len;
960 
961 	__acornscsi_out(host->base + (offset << 1), ptr, this_len);
962 
963 	offset += this_len;
964 	ptr += this_len;
965 	len -= this_len;
966 
967 	if (offset == (1 << 12)) {
968 	    offset = 0;
969 	    page ++;
970 	    writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
971 	}
972     }
973     writeb(host->card.page_reg, host->fast + PAGE_REG);
974 }
975 
976 /* =========================================================================================
977  * On-board DMA routines
978  */
979 #ifdef USE_DMAC
980 /*
981  * Prototype: void acornscsi_dmastop(AS_Host *host)
982  * Purpose  : stop all DMA
983  * Params   : host - host on which to stop DMA
984  * Notes    : This is called when leaving DATA IN/OUT phase,
985  *	      or when interface is RESET
986  */
987 static inline
988 void acornscsi_dma_stop(AS_Host *host)
989 {
990     dmac_write(host, DMAC_MASKREG, MASK_ON);
991     dmac_clearintr(host);
992 
993 #if (DEBUG & DEBUG_DMA)
994     DBG(host->SCpnt, acornscsi_dumpdma(host, "stop"));
995 #endif
996 }
997 
998 /*
999  * Function: void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
1000  * Purpose : setup DMA controller for data transfer
1001  * Params  : host - host to setup
1002  *	     direction - data transfer direction
1003  * Notes   : This is called when entering DATA I/O phase, not
1004  *	     while we're in a DATA I/O phase
1005  */
1006 static
1007 void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
1008 {
1009     unsigned int address, length, mode;
1010 
1011     host->dma.direction = direction;
1012 
1013     dmac_write(host, DMAC_MASKREG, MASK_ON);
1014 
1015     if (direction == DMA_OUT) {
1016 #if (DEBUG & DEBUG_NO_WRITE)
1017 	if (NO_WRITE & (1 << host->SCpnt->device->id)) {
1018 	    printk(KERN_CRIT "scsi%d.%c: I can't handle DMA_OUT!\n",
1019 		    host->host->host_no, acornscsi_target(host));
1020 	    return;
1021 	}
1022 #endif
1023 	mode = DMAC_WRITE;
1024     } else
1025 	mode = DMAC_READ;
1026 
1027     /*
1028      * Allocate some buffer space, limited to half the buffer size
1029      */
1030     length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1031     if (length) {
1032 	host->dma.start_addr = address = host->dma.free_addr;
1033 	host->dma.free_addr = (host->dma.free_addr + length) &
1034 				(DMAC_BUFFER_SIZE - 1);
1035 
1036 	/*
1037 	 * Transfer data to DMA memory
1038 	 */
1039 	if (direction == DMA_OUT)
1040 	    acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1041 				length);
1042 
1043 	length -= 1;
1044 	dmac_write(host, DMAC_TXCNTLO, length);
1045 	dmac_write(host, DMAC_TXCNTHI, length >> 8);
1046 	dmac_write(host, DMAC_TXADRLO, address);
1047 	dmac_write(host, DMAC_TXADRMD, address >> 8);
1048 	dmac_write(host, DMAC_TXADRHI, 0);
1049 	dmac_write(host, DMAC_MODECON, mode);
1050 	dmac_write(host, DMAC_MASKREG, MASK_OFF);
1051 
1052 #if (DEBUG & DEBUG_DMA)
1053 	DBG(host->SCpnt, acornscsi_dumpdma(host, "strt"));
1054 #endif
1055 	host->dma.xfer_setup = 1;
1056     }
1057 }
1058 
1059 /*
1060  * Function: void acornscsi_dma_cleanup(AS_Host *host)
1061  * Purpose : ensure that all DMA transfers are up-to-date & host->scsi.SCp is correct
1062  * Params  : host - host to finish
1063  * Notes   : This is called when a command is:
1064  *		terminating, RESTORE_POINTERS, SAVE_POINTERS, DISCONNECT
1065  *	   : This must not return until all transfers are completed.
1066  */
1067 static
1068 void acornscsi_dma_cleanup(AS_Host *host)
1069 {
1070     dmac_write(host, DMAC_MASKREG, MASK_ON);
1071     dmac_clearintr(host);
1072 
1073     /*
1074      * Check for a pending transfer
1075      */
1076     if (host->dma.xfer_required) {
1077 	host->dma.xfer_required = 0;
1078 	if (host->dma.direction == DMA_IN)
1079 	    acornscsi_data_read(host, host->dma.xfer_ptr,
1080 				 host->dma.xfer_start, host->dma.xfer_length);
1081     }
1082 
1083     /*
1084      * Has a transfer been setup?
1085      */
1086     if (host->dma.xfer_setup) {
1087 	unsigned int transferred;
1088 
1089 	host->dma.xfer_setup = 0;
1090 
1091 #if (DEBUG & DEBUG_DMA)
1092 	DBG(host->SCpnt, acornscsi_dumpdma(host, "cupi"));
1093 #endif
1094 
1095 	/*
1096 	 * Calculate number of bytes transferred from DMA.
1097 	 */
1098 	transferred = dmac_address(host) - host->dma.start_addr;
1099 	host->dma.transferred += transferred;
1100 
1101 	if (host->dma.direction == DMA_IN)
1102 	    acornscsi_data_read(host, host->scsi.SCp.ptr,
1103 				 host->dma.start_addr, transferred);
1104 
1105 	/*
1106 	 * Update SCSI pointers
1107 	 */
1108 	acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1109 #if (DEBUG & DEBUG_DMA)
1110 	DBG(host->SCpnt, acornscsi_dumpdma(host, "cupo"));
1111 #endif
1112     }
1113 }
1114 
1115 /*
1116  * Function: void acornscsi_dmacintr(AS_Host *host)
1117  * Purpose : handle interrupts from DMAC device
1118  * Params  : host - host to process
1119  * Notes   : If reading, we schedule the read to main memory &
1120  *	     allow the transfer to continue.
1121  *	   : If writing, we fill the onboard DMA memory from main
1122  *	     memory.
1123  *	   : Called whenever DMAC finished it's current transfer.
1124  */
1125 static
1126 void acornscsi_dma_intr(AS_Host *host)
1127 {
1128     unsigned int address, length, transferred;
1129 
1130 #if (DEBUG & DEBUG_DMA)
1131     DBG(host->SCpnt, acornscsi_dumpdma(host, "inti"));
1132 #endif
1133 
1134     dmac_write(host, DMAC_MASKREG, MASK_ON);
1135     dmac_clearintr(host);
1136 
1137     /*
1138      * Calculate amount transferred via DMA
1139      */
1140     transferred = dmac_address(host) - host->dma.start_addr;
1141     host->dma.transferred += transferred;
1142 
1143     /*
1144      * Schedule DMA transfer off board
1145      */
1146     if (host->dma.direction == DMA_IN) {
1147 	host->dma.xfer_start = host->dma.start_addr;
1148 	host->dma.xfer_length = transferred;
1149 	host->dma.xfer_ptr = host->scsi.SCp.ptr;
1150 	host->dma.xfer_required = 1;
1151     }
1152 
1153     acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1154 
1155     /*
1156      * Allocate some buffer space, limited to half the on-board RAM size
1157      */
1158     length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1159     if (length) {
1160 	host->dma.start_addr = address = host->dma.free_addr;
1161 	host->dma.free_addr = (host->dma.free_addr + length) &
1162 				(DMAC_BUFFER_SIZE - 1);
1163 
1164 	/*
1165 	 * Transfer data to DMA memory
1166 	 */
1167 	if (host->dma.direction == DMA_OUT)
1168 	    acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1169 				length);
1170 
1171 	length -= 1;
1172 	dmac_write(host, DMAC_TXCNTLO, length);
1173 	dmac_write(host, DMAC_TXCNTHI, length >> 8);
1174 	dmac_write(host, DMAC_TXADRLO, address);
1175 	dmac_write(host, DMAC_TXADRMD, address >> 8);
1176 	dmac_write(host, DMAC_TXADRHI, 0);
1177 	dmac_write(host, DMAC_MASKREG, MASK_OFF);
1178 
1179 #if (DEBUG & DEBUG_DMA)
1180 	DBG(host->SCpnt, acornscsi_dumpdma(host, "into"));
1181 #endif
1182     } else {
1183 	host->dma.xfer_setup = 0;
1184 #if 0
1185 	/*
1186 	 * If the interface still wants more, then this is an error.
1187 	 * We give it another byte, but we also attempt to raise an
1188 	 * attention condition.  We continue giving one byte until
1189 	 * the device recognises the attention.
1190 	 */
1191 	if (dmac_read(host, DMAC_STATUS) & STATUS_RQ0) {
1192 	    acornscsi_abortcmd(host, host->SCpnt->tag);
1193 
1194 	    dmac_write(host, DMAC_TXCNTLO, 0);
1195 	    dmac_write(host, DMAC_TXCNTHI, 0);
1196 	    dmac_write(host, DMAC_TXADRLO, 0);
1197 	    dmac_write(host, DMAC_TXADRMD, 0);
1198 	    dmac_write(host, DMAC_TXADRHI, 0);
1199 	    dmac_write(host, DMAC_MASKREG, MASK_OFF);
1200 	}
1201 #endif
1202     }
1203 }
1204 
1205 /*
1206  * Function: void acornscsi_dma_xfer(AS_Host *host)
1207  * Purpose : transfer data between AcornSCSI and memory
1208  * Params  : host - host to process
1209  */
1210 static
1211 void acornscsi_dma_xfer(AS_Host *host)
1212 {
1213     host->dma.xfer_required = 0;
1214 
1215     if (host->dma.direction == DMA_IN)
1216 	acornscsi_data_read(host, host->dma.xfer_ptr,
1217 				host->dma.xfer_start, host->dma.xfer_length);
1218 }
1219 
1220 /*
1221  * Function: void acornscsi_dma_adjust(AS_Host *host)
1222  * Purpose : adjust DMA pointers & count for bytes transferred to
1223  *	     SBIC but not SCSI bus.
1224  * Params  : host - host to adjust DMA count for
1225  */
1226 static
1227 void acornscsi_dma_adjust(AS_Host *host)
1228 {
1229     if (host->dma.xfer_setup) {
1230 	signed long transferred;
1231 #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1232 	DBG(host->SCpnt, acornscsi_dumpdma(host, "adji"));
1233 #endif
1234 	/*
1235 	 * Calculate correct DMA address - DMA is ahead of SCSI bus while
1236 	 * writing.
1237 	 *  host->scsi.SCp.scsi_xferred is the number of bytes
1238 	 *  actually transferred to/from the SCSI bus.
1239 	 *  host->dma.transferred is the number of bytes transferred
1240 	 *  over DMA since host->dma.start_addr was last set.
1241 	 *
1242 	 * real_dma_addr = host->dma.start_addr + host->scsi.SCp.scsi_xferred
1243 	 *		   - host->dma.transferred
1244 	 */
1245 	transferred = host->scsi.SCp.scsi_xferred - host->dma.transferred;
1246 	if (transferred < 0)
1247 	    printk("scsi%d.%c: Ack! DMA write correction %ld < 0!\n",
1248 		    host->host->host_no, acornscsi_target(host), transferred);
1249 	else if (transferred == 0)
1250 	    host->dma.xfer_setup = 0;
1251 	else {
1252 	    transferred += host->dma.start_addr;
1253 	    dmac_write(host, DMAC_TXADRLO, transferred);
1254 	    dmac_write(host, DMAC_TXADRMD, transferred >> 8);
1255 	    dmac_write(host, DMAC_TXADRHI, transferred >> 16);
1256 #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1257 	    DBG(host->SCpnt, acornscsi_dumpdma(host, "adjo"));
1258 #endif
1259 	}
1260     }
1261 }
1262 #endif
1263 
1264 /* =========================================================================================
1265  * Data I/O
1266  */
1267 static int
1268 acornscsi_write_pio(AS_Host *host, char *bytes, int *ptr, int len, unsigned int max_timeout)
1269 {
1270 	unsigned int asr, timeout = max_timeout;
1271 	int my_ptr = *ptr;
1272 
1273 	while (my_ptr < len) {
1274 		asr = sbic_arm_read(host, SBIC_ASR);
1275 
1276 		if (asr & ASR_DBR) {
1277 			timeout = max_timeout;
1278 
1279 			sbic_arm_write(host, SBIC_DATA, bytes[my_ptr++]);
1280 		} else if (asr & ASR_INT)
1281 			break;
1282 		else if (--timeout == 0)
1283 			break;
1284 		udelay(1);
1285 	}
1286 
1287 	*ptr = my_ptr;
1288 
1289 	return (timeout == 0) ? -1 : 0;
1290 }
1291 
1292 /*
1293  * Function: void acornscsi_sendcommand(AS_Host *host)
1294  * Purpose : send a command to a target
1295  * Params  : host - host which is connected to target
1296  */
1297 static void
1298 acornscsi_sendcommand(AS_Host *host)
1299 {
1300 	struct scsi_cmnd *SCpnt = host->SCpnt;
1301 
1302     sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1303     sbic_arm_writenext(host, 0);
1304     sbic_arm_writenext(host, SCpnt->cmd_len - host->scsi.SCp.sent_command);
1305 
1306     acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1307 
1308     if (acornscsi_write_pio(host, SCpnt->cmnd,
1309 	(int *)&host->scsi.SCp.sent_command, SCpnt->cmd_len, 1000000))
1310 	printk("scsi%d: timeout while sending command\n", host->host->host_no);
1311 
1312     host->scsi.phase = PHASE_COMMAND;
1313 }
1314 
1315 static
1316 void acornscsi_sendmessage(AS_Host *host)
1317 {
1318     unsigned int message_length = msgqueue_msglength(&host->scsi.msgs);
1319     unsigned int msgnr;
1320     struct message *msg;
1321 
1322 #if (DEBUG & DEBUG_MESSAGES)
1323     printk("scsi%d.%c: sending message ",
1324 	    host->host->host_no, acornscsi_target(host));
1325 #endif
1326 
1327     switch (message_length) {
1328     case 0:
1329 	acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1330 
1331 	acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 1");
1332 
1333 	sbic_arm_write(host, SBIC_DATA, NOP);
1334 
1335 	host->scsi.last_message = NOP;
1336 #if (DEBUG & DEBUG_MESSAGES)
1337 	printk("NOP");
1338 #endif
1339 	break;
1340 
1341     case 1:
1342 	acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1343 	msg = msgqueue_getmsg(&host->scsi.msgs, 0);
1344 
1345 	acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 2");
1346 
1347 	sbic_arm_write(host, SBIC_DATA, msg->msg[0]);
1348 
1349 	host->scsi.last_message = msg->msg[0];
1350 #if (DEBUG & DEBUG_MESSAGES)
1351 	spi_print_msg(msg->msg);
1352 #endif
1353 	break;
1354 
1355     default:
1356 	/*
1357 	 * ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.14)
1358 	 * 'When a target sends this (MESSAGE_REJECT) message, it
1359 	 *  shall change to MESSAGE IN phase and send this message
1360 	 *  prior to requesting additional message bytes from the
1361 	 *  initiator.  This provides an interlock so that the
1362 	 *  initiator can determine which message byte is rejected.
1363 	 */
1364 	sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1365 	sbic_arm_writenext(host, 0);
1366 	sbic_arm_writenext(host, message_length);
1367 	acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1368 
1369 	msgnr = 0;
1370 	while ((msg = msgqueue_getmsg(&host->scsi.msgs, msgnr++)) != NULL) {
1371 	    unsigned int i;
1372 #if (DEBUG & DEBUG_MESSAGES)
1373 	    spi_print_msg(msg);
1374 #endif
1375 	    i = 0;
1376 	    if (acornscsi_write_pio(host, msg->msg, &i, msg->length, 1000000))
1377 		printk("scsi%d: timeout while sending message\n", host->host->host_no);
1378 
1379 	    host->scsi.last_message = msg->msg[0];
1380 	    if (msg->msg[0] == EXTENDED_MESSAGE)
1381 		host->scsi.last_message |= msg->msg[2] << 8;
1382 
1383 	    if (i != msg->length)
1384 		break;
1385 	}
1386 	break;
1387     }
1388 #if (DEBUG & DEBUG_MESSAGES)
1389     printk("\n");
1390 #endif
1391 }
1392 
1393 /*
1394  * Function: void acornscsi_readstatusbyte(AS_Host *host)
1395  * Purpose : Read status byte from connected target
1396  * Params  : host - host connected to target
1397  */
1398 static
1399 void acornscsi_readstatusbyte(AS_Host *host)
1400 {
1401     acornscsi_sbic_issuecmd(host, CMND_XFERINFO|CMND_SBT);
1402     acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "reading status byte");
1403     host->scsi.SCp.Status = sbic_arm_read(host, SBIC_DATA);
1404 }
1405 
1406 /*
1407  * Function: unsigned char acornscsi_readmessagebyte(AS_Host *host)
1408  * Purpose : Read one message byte from connected target
1409  * Params  : host - host connected to target
1410  */
1411 static
1412 unsigned char acornscsi_readmessagebyte(AS_Host *host)
1413 {
1414     unsigned char message;
1415 
1416     acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1417 
1418     acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "for message byte");
1419 
1420     message = sbic_arm_read(host, SBIC_DATA);
1421 
1422     /* wait for MSGIN-XFER-PAUSED */
1423     acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after message byte");
1424 
1425     sbic_arm_read(host, SBIC_SSR);
1426 
1427     return message;
1428 }
1429 
1430 /*
1431  * Function: void acornscsi_message(AS_Host *host)
1432  * Purpose : Read complete message from connected target & action message
1433  * Params  : host - host connected to target
1434  */
1435 static
1436 void acornscsi_message(AS_Host *host)
1437 {
1438     unsigned char message[16];
1439     unsigned int msgidx = 0, msglen = 1;
1440 
1441     do {
1442 	message[msgidx] = acornscsi_readmessagebyte(host);
1443 
1444 	switch (msgidx) {
1445 	case 0:
1446 	    if (message[0] == EXTENDED_MESSAGE ||
1447 		(message[0] >= 0x20 && message[0] <= 0x2f))
1448 		msglen = 2;
1449 	    break;
1450 
1451 	case 1:
1452 	    if (message[0] == EXTENDED_MESSAGE)
1453 		msglen += message[msgidx];
1454 	    break;
1455 	}
1456 	msgidx += 1;
1457 	if (msgidx < msglen) {
1458 	    acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1459 
1460 	    /* wait for next msg-in */
1461 	    acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after negate ack");
1462 	    sbic_arm_read(host, SBIC_SSR);
1463 	}
1464     } while (msgidx < msglen);
1465 
1466 #if (DEBUG & DEBUG_MESSAGES)
1467     printk("scsi%d.%c: message in: ",
1468 	    host->host->host_no, acornscsi_target(host));
1469     spi_print_msg(message);
1470     printk("\n");
1471 #endif
1472 
1473     if (host->scsi.phase == PHASE_RECONNECTED) {
1474 	/*
1475 	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
1476 	 * 'Whenever a target reconnects to an initiator to continue
1477 	 *  a tagged I/O process, the SIMPLE QUEUE TAG message shall
1478 	 *  be sent immediately following the IDENTIFY message...'
1479 	 */
1480 	if (message[0] == SIMPLE_QUEUE_TAG)
1481 	    host->scsi.reconnected.tag = message[1];
1482 	if (acornscsi_reconnect_finish(host))
1483 	    host->scsi.phase = PHASE_MSGIN;
1484     }
1485 
1486     switch (message[0]) {
1487     case ABORT_TASK_SET:
1488     case ABORT_TASK:
1489     case COMMAND_COMPLETE:
1490 	if (host->scsi.phase != PHASE_STATUSIN) {
1491 	    printk(KERN_ERR "scsi%d.%c: command complete following non-status in phase?\n",
1492 		    host->host->host_no, acornscsi_target(host));
1493 	    acornscsi_dumplog(host, host->SCpnt->device->id);
1494 	}
1495 	host->scsi.phase = PHASE_DONE;
1496 	host->scsi.SCp.Message = message[0];
1497 	break;
1498 
1499     case SAVE_POINTERS:
1500 	/*
1501 	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.20)
1502 	 * 'The SAVE DATA POINTER message is sent from a target to
1503 	 *  direct the initiator to copy the active data pointer to
1504 	 *  the saved data pointer for the current I/O process.
1505 	 */
1506 	acornscsi_dma_cleanup(host);
1507 	host->SCpnt->SCp = host->scsi.SCp;
1508 	host->SCpnt->SCp.sent_command = 0;
1509 	host->scsi.phase = PHASE_MSGIN;
1510 	break;
1511 
1512     case RESTORE_POINTERS:
1513 	/*
1514 	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.19)
1515 	 * 'The RESTORE POINTERS message is sent from a target to
1516 	 *  direct the initiator to copy the most recently saved
1517 	 *  command, data, and status pointers for the I/O process
1518 	 *  to the corresponding active pointers.  The command and
1519 	 *  status pointers shall be restored to the beginning of
1520 	 *  the present command and status areas.'
1521 	 */
1522 	acornscsi_dma_cleanup(host);
1523 	host->scsi.SCp = host->SCpnt->SCp;
1524 	host->scsi.phase = PHASE_MSGIN;
1525 	break;
1526 
1527     case DISCONNECT:
1528 	/*
1529 	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 6.4.2)
1530 	 * 'On those occasions when an error or exception condition occurs
1531 	 *  and the target elects to repeat the information transfer, the
1532 	 *  target may repeat the transfer either issuing a RESTORE POINTERS
1533 	 *  message or by disconnecting without issuing a SAVE POINTERS
1534 	 *  message.  When reconnection is completed, the most recent
1535 	 *  saved pointer values are restored.'
1536 	 */
1537 	acornscsi_dma_cleanup(host);
1538 	host->scsi.phase = PHASE_DISCONNECT;
1539 	break;
1540 
1541     case MESSAGE_REJECT:
1542 #if 0 /* this isn't needed any more */
1543 	/*
1544 	 * If we were negociating sync transfer, we don't yet know if
1545 	 * this REJECT is for the sync transfer or for the tagged queue/wide
1546 	 * transfer.  Re-initiate sync transfer negotiation now, and if
1547 	 * we got a REJECT in response to SDTR, then it'll be set to DONE.
1548 	 */
1549 	if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST)
1550 	    host->device[host->SCpnt->device->id].sync_state = SYNC_NEGOCIATE;
1551 #endif
1552 
1553 	/*
1554 	 * If we have any messages waiting to go out, then assert ATN now
1555 	 */
1556 	if (msgqueue_msglength(&host->scsi.msgs))
1557 	    acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1558 
1559 	switch (host->scsi.last_message) {
1560 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1561 	case HEAD_OF_QUEUE_TAG:
1562 	case ORDERED_QUEUE_TAG:
1563 	case SIMPLE_QUEUE_TAG:
1564 	    /*
1565 	     * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
1566 	     *  If a target does not implement tagged queuing and a queue tag
1567 	     *  message is received, it shall respond with a MESSAGE REJECT
1568 	     *  message and accept the I/O process as if it were untagged.
1569 	     */
1570 	    printk(KERN_NOTICE "scsi%d.%c: disabling tagged queueing\n",
1571 		    host->host->host_no, acornscsi_target(host));
1572 	    host->SCpnt->device->simple_tags = 0;
1573 	    set_bit(host->SCpnt->device->id * 8 +
1574 		    (u8)(host->SCpnt->device->lun & 0x7), host->busyluns);
1575 	    break;
1576 #endif
1577 	case EXTENDED_MESSAGE | (EXTENDED_SDTR << 8):
1578 	    /*
1579 	     * Target can't handle synchronous transfers
1580 	     */
1581 	    printk(KERN_NOTICE "scsi%d.%c: Using asynchronous transfer\n",
1582 		    host->host->host_no, acornscsi_target(host));
1583 	    host->device[host->SCpnt->device->id].sync_xfer = SYNCHTRANSFER_2DBA;
1584 	    host->device[host->SCpnt->device->id].sync_state = SYNC_ASYNCHRONOUS;
1585 	    sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1586 	    break;
1587 
1588 	default:
1589 	    break;
1590 	}
1591 	break;
1592 
1593     case SIMPLE_QUEUE_TAG:
1594 	/* tag queue reconnect... message[1] = queue tag.  Print something to indicate something happened! */
1595 	printk("scsi%d.%c: reconnect queue tag %02X\n",
1596 		host->host->host_no, acornscsi_target(host),
1597 		message[1]);
1598 	break;
1599 
1600     case EXTENDED_MESSAGE:
1601 	switch (message[2]) {
1602 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1603 	case EXTENDED_SDTR:
1604 	    if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST) {
1605 		/*
1606 		 * We requested synchronous transfers.  This isn't quite right...
1607 		 * We can only say if this succeeded if we proceed on to execute the
1608 		 * command from this message.  If we get a MESSAGE PARITY ERROR,
1609 		 * and the target retries fail, then we fallback to asynchronous mode
1610 		 */
1611 		host->device[host->SCpnt->device->id].sync_state = SYNC_COMPLETED;
1612 		printk(KERN_NOTICE "scsi%d.%c: Using synchronous transfer, offset %d, %d ns\n",
1613 			host->host->host_no, acornscsi_target(host),
1614 			message[4], message[3] * 4);
1615 		host->device[host->SCpnt->device->id].sync_xfer =
1616 			calc_sync_xfer(message[3] * 4, message[4]);
1617 	    } else {
1618 		unsigned char period, length;
1619 		/*
1620 		 * Target requested synchronous transfers.  The agreement is only
1621 		 * to be in operation AFTER the target leaves message out phase.
1622 		 */
1623 		acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1624 		period = max_t(unsigned int, message[3], sdtr_period / 4);
1625 		length = min_t(unsigned int, message[4], sdtr_size);
1626 		msgqueue_addmsg(&host->scsi.msgs, 5, EXTENDED_MESSAGE, 3,
1627 				 EXTENDED_SDTR, period, length);
1628 		host->device[host->SCpnt->device->id].sync_xfer =
1629 			calc_sync_xfer(period * 4, length);
1630 	    }
1631 	    sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1632 	    break;
1633 #else
1634 	    /* We do not accept synchronous transfers.  Respond with a
1635 	     * MESSAGE_REJECT.
1636 	     */
1637 #endif
1638 
1639 	case EXTENDED_WDTR:
1640 	    /* The WD33C93A is only 8-bit.  We respond with a MESSAGE_REJECT
1641 	     * to a wide data transfer request.
1642 	     */
1643 	default:
1644 	    acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1645 	    msgqueue_flush(&host->scsi.msgs);
1646 	    msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1647 	    break;
1648 	}
1649 	break;
1650 
1651     default: /* reject message */
1652 	printk(KERN_ERR "scsi%d.%c: unrecognised message %02X, rejecting\n",
1653 		host->host->host_no, acornscsi_target(host),
1654 		message[0]);
1655 	acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1656 	msgqueue_flush(&host->scsi.msgs);
1657 	msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1658 	host->scsi.phase = PHASE_MSGIN;
1659 	break;
1660     }
1661     acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1662 }
1663 
1664 /*
1665  * Function: int acornscsi_buildmessages(AS_Host *host)
1666  * Purpose : build the connection messages for a host
1667  * Params  : host - host to add messages to
1668  */
1669 static
1670 void acornscsi_buildmessages(AS_Host *host)
1671 {
1672 #if 0
1673     /* does the device need resetting? */
1674     if (cmd_reset) {
1675 	msgqueue_addmsg(&host->scsi.msgs, 1, BUS_DEVICE_RESET);
1676 	return;
1677     }
1678 #endif
1679 
1680     msgqueue_addmsg(&host->scsi.msgs, 1,
1681 		     IDENTIFY(host->device[host->SCpnt->device->id].disconnect_ok,
1682 			     host->SCpnt->device->lun));
1683 
1684 #if 0
1685     /* does the device need the current command aborted */
1686     if (cmd_aborted) {
1687 	acornscsi_abortcmd(host->SCpnt->tag);
1688 	return;
1689     }
1690 #endif
1691 
1692 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1693     if (host->SCpnt->tag) {
1694 	unsigned int tag_type;
1695 
1696 	if (host->SCpnt->cmnd[0] == REQUEST_SENSE ||
1697 	    host->SCpnt->cmnd[0] == TEST_UNIT_READY ||
1698 	    host->SCpnt->cmnd[0] == INQUIRY)
1699 	    tag_type = HEAD_OF_QUEUE_TAG;
1700 	else
1701 	    tag_type = SIMPLE_QUEUE_TAG;
1702 	msgqueue_addmsg(&host->scsi.msgs, 2, tag_type, host->SCpnt->tag);
1703     }
1704 #endif
1705 
1706 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1707     if (host->device[host->SCpnt->device->id].sync_state == SYNC_NEGOCIATE) {
1708 	host->device[host->SCpnt->device->id].sync_state = SYNC_SENT_REQUEST;
1709 	msgqueue_addmsg(&host->scsi.msgs, 5,
1710 			 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1711 			 sdtr_period / 4, sdtr_size);
1712     }
1713 #endif
1714 }
1715 
1716 /*
1717  * Function: int acornscsi_starttransfer(AS_Host *host)
1718  * Purpose : transfer data to/from connected target
1719  * Params  : host - host to which target is connected
1720  * Returns : 0 if failure
1721  */
1722 static
1723 int acornscsi_starttransfer(AS_Host *host)
1724 {
1725     int residual;
1726 
1727     if (!host->scsi.SCp.ptr /*&& host->scsi.SCp.this_residual*/) {
1728 	printk(KERN_ERR "scsi%d.%c: null buffer passed to acornscsi_starttransfer\n",
1729 		host->host->host_no, acornscsi_target(host));
1730 	return 0;
1731     }
1732 
1733     residual = scsi_bufflen(host->SCpnt) - host->scsi.SCp.scsi_xferred;
1734 
1735     sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1736     sbic_arm_writenext(host, residual >> 16);
1737     sbic_arm_writenext(host, residual >> 8);
1738     sbic_arm_writenext(host, residual);
1739     acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1740     return 1;
1741 }
1742 
1743 /* =========================================================================================
1744  * Connection & Disconnection
1745  */
1746 /*
1747  * Function : acornscsi_reconnect(AS_Host *host)
1748  * Purpose  : reconnect a previously disconnected command
1749  * Params   : host - host specific data
1750  * Remarks  : SCSI spec says:
1751  *		'The set of active pointers is restored from the set
1752  *		 of saved pointers upon reconnection of the I/O process'
1753  */
1754 static
1755 int acornscsi_reconnect(AS_Host *host)
1756 {
1757     unsigned int target, lun, ok = 0;
1758 
1759     target = sbic_arm_read(host, SBIC_SOURCEID);
1760 
1761     if (!(target & 8))
1762 	printk(KERN_ERR "scsi%d: invalid source id after reselection "
1763 		"- device fault?\n",
1764 		host->host->host_no);
1765 
1766     target &= 7;
1767 
1768     if (host->SCpnt && !host->scsi.disconnectable) {
1769 	printk(KERN_ERR "scsi%d.%d: reconnected while command in "
1770 		"progress to target %d?\n",
1771 		host->host->host_no, target, host->SCpnt->device->id);
1772 	host->SCpnt = NULL;
1773     }
1774 
1775     lun = sbic_arm_read(host, SBIC_DATA) & 7;
1776 
1777     host->scsi.reconnected.target = target;
1778     host->scsi.reconnected.lun = lun;
1779     host->scsi.reconnected.tag = 0;
1780 
1781     if (host->scsi.disconnectable && host->SCpnt &&
1782 	host->SCpnt->device->id == target && host->SCpnt->device->lun == lun)
1783 	ok = 1;
1784 
1785     if (!ok && queue_probetgtlun(&host->queues.disconnected, target, lun))
1786 	ok = 1;
1787 
1788     ADD_STATUS(target, 0x81, host->scsi.phase, 0);
1789 
1790     if (ok) {
1791 	host->scsi.phase = PHASE_RECONNECTED;
1792     } else {
1793 	/* this doesn't seem to work */
1794 	printk(KERN_ERR "scsi%d.%c: reselected with no command "
1795 		"to reconnect with\n",
1796 		host->host->host_no, '0' + target);
1797 	acornscsi_dumplog(host, target);
1798 	acornscsi_abortcmd(host, 0);
1799 	if (host->SCpnt) {
1800 	    queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1801 	    host->SCpnt = NULL;
1802 	}
1803     }
1804     acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1805     return !ok;
1806 }
1807 
1808 /*
1809  * Function: int acornscsi_reconnect_finish(AS_Host *host)
1810  * Purpose : finish reconnecting a command
1811  * Params  : host - host to complete
1812  * Returns : 0 if failed
1813  */
1814 static
1815 int acornscsi_reconnect_finish(AS_Host *host)
1816 {
1817     if (host->scsi.disconnectable && host->SCpnt) {
1818 	host->scsi.disconnectable = 0;
1819 	if (host->SCpnt->device->id  == host->scsi.reconnected.target &&
1820 	    host->SCpnt->device->lun == host->scsi.reconnected.lun &&
1821 	    host->SCpnt->tag         == host->scsi.reconnected.tag) {
1822 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1823 	    DBG(host->SCpnt, printk("scsi%d.%c: reconnected",
1824 		    host->host->host_no, acornscsi_target(host)));
1825 #endif
1826 	} else {
1827 	    queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1828 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1829 	    DBG(host->SCpnt, printk("scsi%d.%c: had to move command "
1830 		    "to disconnected queue\n",
1831 		    host->host->host_no, acornscsi_target(host)));
1832 #endif
1833 	    host->SCpnt = NULL;
1834 	}
1835     }
1836     if (!host->SCpnt) {
1837 	host->SCpnt = queue_remove_tgtluntag(&host->queues.disconnected,
1838 				host->scsi.reconnected.target,
1839 				host->scsi.reconnected.lun,
1840 				host->scsi.reconnected.tag);
1841 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1842 	DBG(host->SCpnt, printk("scsi%d.%c: had to get command",
1843 		host->host->host_no, acornscsi_target(host)));
1844 #endif
1845     }
1846 
1847     if (!host->SCpnt)
1848 	acornscsi_abortcmd(host, host->scsi.reconnected.tag);
1849     else {
1850 	/*
1851 	 * Restore data pointer from SAVED pointers.
1852 	 */
1853 	host->scsi.SCp = host->SCpnt->SCp;
1854 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1855 	printk(", data pointers: [%p, %X]",
1856 		host->scsi.SCp.ptr, host->scsi.SCp.this_residual);
1857 #endif
1858     }
1859 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1860     printk("\n");
1861 #endif
1862 
1863     host->dma.transferred = host->scsi.SCp.scsi_xferred;
1864 
1865     return host->SCpnt != NULL;
1866 }
1867 
1868 /*
1869  * Function: void acornscsi_disconnect_unexpected(AS_Host *host)
1870  * Purpose : handle an unexpected disconnect
1871  * Params  : host - host on which disconnect occurred
1872  */
1873 static
1874 void acornscsi_disconnect_unexpected(AS_Host *host)
1875 {
1876     printk(KERN_ERR "scsi%d.%c: unexpected disconnect\n",
1877 	    host->host->host_no, acornscsi_target(host));
1878 #if (DEBUG & DEBUG_ABORT)
1879     acornscsi_dumplog(host, 8);
1880 #endif
1881 
1882     acornscsi_done(host, &host->SCpnt, DID_ERROR);
1883 }
1884 
1885 /*
1886  * Function: void acornscsi_abortcmd(AS_host *host, unsigned char tag)
1887  * Purpose : abort a currently executing command
1888  * Params  : host - host with connected command to abort
1889  *	     tag  - tag to abort
1890  */
1891 static
1892 void acornscsi_abortcmd(AS_Host *host, unsigned char tag)
1893 {
1894     host->scsi.phase = PHASE_ABORTED;
1895     sbic_arm_write(host, SBIC_CMND, CMND_ASSERTATN);
1896 
1897     msgqueue_flush(&host->scsi.msgs);
1898 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1899     if (tag)
1900 	msgqueue_addmsg(&host->scsi.msgs, 2, ABORT_TAG, tag);
1901     else
1902 #endif
1903 	msgqueue_addmsg(&host->scsi.msgs, 1, ABORT);
1904 }
1905 
1906 /* ==========================================================================================
1907  * Interrupt routines.
1908  */
1909 /*
1910  * Function: int acornscsi_sbicintr(AS_Host *host)
1911  * Purpose : handle interrupts from SCSI device
1912  * Params  : host - host to process
1913  * Returns : INTR_PROCESS if expecting another SBIC interrupt
1914  *	     INTR_IDLE if no interrupt
1915  *	     INTR_NEXT_COMMAND if we have finished processing the command
1916  */
1917 static
1918 intr_ret_t acornscsi_sbicintr(AS_Host *host, int in_irq)
1919 {
1920     unsigned int asr, ssr;
1921 
1922     asr = sbic_arm_read(host, SBIC_ASR);
1923     if (!(asr & ASR_INT))
1924 	return INTR_IDLE;
1925 
1926     ssr = sbic_arm_read(host, SBIC_SSR);
1927 
1928 #if (DEBUG & DEBUG_PHASES)
1929     print_sbic_status(asr, ssr, host->scsi.phase);
1930 #endif
1931 
1932     ADD_STATUS(8, ssr, host->scsi.phase, in_irq);
1933 
1934     if (host->SCpnt && !host->scsi.disconnectable)
1935 	ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
1936 
1937     switch (ssr) {
1938     case 0x00:				/* reset state - not advanced			*/
1939 	printk(KERN_ERR "scsi%d: reset in standard mode but wanted advanced mode.\n",
1940 		host->host->host_no);
1941 	/* setup sbic - WD33C93A */
1942 	sbic_arm_write(host, SBIC_OWNID, OWNID_EAF | host->host->this_id);
1943 	sbic_arm_write(host, SBIC_CMND, CMND_RESET);
1944 	return INTR_IDLE;
1945 
1946     case 0x01:				/* reset state - advanced			*/
1947 	sbic_arm_write(host, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
1948 	sbic_arm_write(host, SBIC_TIMEOUT, TIMEOUT_TIME);
1949 	sbic_arm_write(host, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
1950 	sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
1951 	msgqueue_flush(&host->scsi.msgs);
1952 	return INTR_IDLE;
1953 
1954     case 0x41:				/* unexpected disconnect aborted command	*/
1955 	acornscsi_disconnect_unexpected(host);
1956 	return INTR_NEXT_COMMAND;
1957     }
1958 
1959     switch (host->scsi.phase) {
1960     case PHASE_CONNECTING:		/* STATE: command removed from issue queue	*/
1961 	switch (ssr) {
1962 	case 0x11:			/* -> PHASE_CONNECTED				*/
1963 	    /* BUS FREE -> SELECTION */
1964 	    host->scsi.phase = PHASE_CONNECTED;
1965 	    msgqueue_flush(&host->scsi.msgs);
1966 	    host->dma.transferred = host->scsi.SCp.scsi_xferred;
1967 	    /* 33C93 gives next interrupt indicating bus phase */
1968 	    asr = sbic_arm_read(host, SBIC_ASR);
1969 	    if (!(asr & ASR_INT))
1970 		break;
1971 	    ssr = sbic_arm_read(host, SBIC_SSR);
1972 	    ADD_STATUS(8, ssr, host->scsi.phase, 1);
1973 	    ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, 1);
1974 	    goto connected;
1975 
1976 	case 0x42:			/* select timed out				*/
1977 					/* -> PHASE_IDLE				*/
1978 	    acornscsi_done(host, &host->SCpnt, DID_NO_CONNECT);
1979 	    return INTR_NEXT_COMMAND;
1980 
1981 	case 0x81:			/* -> PHASE_RECONNECTED or PHASE_ABORTED	*/
1982 	    /* BUS FREE -> RESELECTION */
1983 	    host->origSCpnt = host->SCpnt;
1984 	    host->SCpnt = NULL;
1985 	    msgqueue_flush(&host->scsi.msgs);
1986 	    acornscsi_reconnect(host);
1987 	    break;
1988 
1989 	default:
1990 	    printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTING, SSR %02X?\n",
1991 		    host->host->host_no, acornscsi_target(host), ssr);
1992 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
1993 	    acornscsi_abortcmd(host, host->SCpnt->tag);
1994 	}
1995 	return INTR_PROCESSING;
1996 
1997     connected:
1998     case PHASE_CONNECTED:		/* STATE: device selected ok			*/
1999 	switch (ssr) {
2000 #ifdef NONSTANDARD
2001 	case 0x8a:			/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED	*/
2002 	    /* SELECTION -> COMMAND */
2003 	    acornscsi_sendcommand(host);
2004 	    break;
2005 
2006 	case 0x8b:			/* -> PHASE_STATUS				*/
2007 	    /* SELECTION -> STATUS */
2008 	    acornscsi_readstatusbyte(host);
2009 	    host->scsi.phase = PHASE_STATUSIN;
2010 	    break;
2011 #endif
2012 
2013 	case 0x8e:			/* -> PHASE_MSGOUT				*/
2014 	    /* SELECTION ->MESSAGE OUT */
2015 	    host->scsi.phase = PHASE_MSGOUT;
2016 	    acornscsi_buildmessages(host);
2017 	    acornscsi_sendmessage(host);
2018 	    break;
2019 
2020 	/* these should not happen */
2021 	case 0x85:			/* target disconnected				*/
2022 	    acornscsi_done(host, &host->SCpnt, DID_ERROR);
2023 	    break;
2024 
2025 	default:
2026 	    printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTED, SSR %02X?\n",
2027 		    host->host->host_no, acornscsi_target(host), ssr);
2028 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2029 	    acornscsi_abortcmd(host, host->SCpnt->tag);
2030 	}
2031 	return INTR_PROCESSING;
2032 
2033     case PHASE_MSGOUT:			/* STATE: connected & sent IDENTIFY message	*/
2034 	/*
2035 	 * SCSI standard says that MESSAGE OUT phases can be followed by a
2036 	 * DATA phase, STATUS phase, MESSAGE IN phase or COMMAND phase
2037 	 */
2038 	switch (ssr) {
2039 	case 0x8a:			/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED	*/
2040 	case 0x1a:			/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED	*/
2041 	    /* MESSAGE OUT -> COMMAND */
2042 	    acornscsi_sendcommand(host);
2043 	    break;
2044 
2045 	case 0x8b:			/* -> PHASE_STATUS				*/
2046 	case 0x1b:			/* -> PHASE_STATUS				*/
2047 	    /* MESSAGE OUT -> STATUS */
2048 	    acornscsi_readstatusbyte(host);
2049 	    host->scsi.phase = PHASE_STATUSIN;
2050 	    break;
2051 
2052 	case 0x8e:			/* -> PHASE_MSGOUT				*/
2053 	    /* MESSAGE_OUT(MESSAGE_IN) ->MESSAGE OUT */
2054 	    acornscsi_sendmessage(host);
2055 	    break;
2056 
2057 	case 0x4f:			/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2058 	case 0x1f:			/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2059 	    /* MESSAGE OUT -> MESSAGE IN */
2060 	    acornscsi_message(host);
2061 	    break;
2062 
2063 	default:
2064 	    printk(KERN_ERR "scsi%d.%c: PHASE_MSGOUT, SSR %02X?\n",
2065 		    host->host->host_no, acornscsi_target(host), ssr);
2066 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2067 	}
2068 	return INTR_PROCESSING;
2069 
2070     case PHASE_COMMAND: 		/* STATE: connected & command sent		*/
2071 	switch (ssr) {
2072 	case 0x18:			/* -> PHASE_DATAOUT				*/
2073 	    /* COMMAND -> DATA OUT */
2074 	    if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2075 		acornscsi_abortcmd(host, host->SCpnt->tag);
2076 	    acornscsi_dma_setup(host, DMA_OUT);
2077 	    if (!acornscsi_starttransfer(host))
2078 		acornscsi_abortcmd(host, host->SCpnt->tag);
2079 	    host->scsi.phase = PHASE_DATAOUT;
2080 	    return INTR_IDLE;
2081 
2082 	case 0x19:			/* -> PHASE_DATAIN				*/
2083 	    /* COMMAND -> DATA IN */
2084 	    if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2085 		acornscsi_abortcmd(host, host->SCpnt->tag);
2086 	    acornscsi_dma_setup(host, DMA_IN);
2087 	    if (!acornscsi_starttransfer(host))
2088 		acornscsi_abortcmd(host, host->SCpnt->tag);
2089 	    host->scsi.phase = PHASE_DATAIN;
2090 	    return INTR_IDLE;
2091 
2092 	case 0x1b:			/* -> PHASE_STATUS				*/
2093 	    /* COMMAND -> STATUS */
2094 	    acornscsi_readstatusbyte(host);
2095 	    host->scsi.phase = PHASE_STATUSIN;
2096 	    break;
2097 
2098 	case 0x1e:			/* -> PHASE_MSGOUT				*/
2099 	    /* COMMAND -> MESSAGE OUT */
2100 	    acornscsi_sendmessage(host);
2101 	    break;
2102 
2103 	case 0x1f:			/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2104 	    /* COMMAND -> MESSAGE IN */
2105 	    acornscsi_message(host);
2106 	    break;
2107 
2108 	default:
2109 	    printk(KERN_ERR "scsi%d.%c: PHASE_COMMAND, SSR %02X?\n",
2110 		    host->host->host_no, acornscsi_target(host), ssr);
2111 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2112 	}
2113 	return INTR_PROCESSING;
2114 
2115     case PHASE_DISCONNECT:		/* STATE: connected, received DISCONNECT msg	*/
2116 	if (ssr == 0x85) {		/* -> PHASE_IDLE				*/
2117 	    host->scsi.disconnectable = 1;
2118 	    host->scsi.reconnected.tag = 0;
2119 	    host->scsi.phase = PHASE_IDLE;
2120 	    host->stats.disconnects += 1;
2121 	} else {
2122 	    printk(KERN_ERR "scsi%d.%c: PHASE_DISCONNECT, SSR %02X instead of disconnect?\n",
2123 		    host->host->host_no, acornscsi_target(host), ssr);
2124 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2125 	}
2126 	return INTR_NEXT_COMMAND;
2127 
2128     case PHASE_IDLE:			/* STATE: disconnected				*/
2129 	if (ssr == 0x81)		/* -> PHASE_RECONNECTED or PHASE_ABORTED	*/
2130 	    acornscsi_reconnect(host);
2131 	else {
2132 	    printk(KERN_ERR "scsi%d.%c: PHASE_IDLE, SSR %02X while idle?\n",
2133 		    host->host->host_no, acornscsi_target(host), ssr);
2134 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2135 	}
2136 	return INTR_PROCESSING;
2137 
2138     case PHASE_RECONNECTED:		/* STATE: device reconnected to initiator	*/
2139 	/*
2140 	 * Command reconnected - if MESGIN, get message - it may be
2141 	 * the tag.  If not, get command out of disconnected queue
2142 	 */
2143 	/*
2144 	 * If we reconnected and we're not in MESSAGE IN phase after IDENTIFY,
2145 	 * reconnect I_T_L command
2146 	 */
2147 	if (ssr != 0x8f && !acornscsi_reconnect_finish(host))
2148 	    return INTR_IDLE;
2149 	ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
2150 	switch (ssr) {
2151 	case 0x88:			/* data out phase				*/
2152 					/* -> PHASE_DATAOUT				*/
2153 	    /* MESSAGE IN -> DATA OUT */
2154 	    acornscsi_dma_setup(host, DMA_OUT);
2155 	    if (!acornscsi_starttransfer(host))
2156 		acornscsi_abortcmd(host, host->SCpnt->tag);
2157 	    host->scsi.phase = PHASE_DATAOUT;
2158 	    return INTR_IDLE;
2159 
2160 	case 0x89:			/* data in phase				*/
2161 					/* -> PHASE_DATAIN				*/
2162 	    /* MESSAGE IN -> DATA IN */
2163 	    acornscsi_dma_setup(host, DMA_IN);
2164 	    if (!acornscsi_starttransfer(host))
2165 		acornscsi_abortcmd(host, host->SCpnt->tag);
2166 	    host->scsi.phase = PHASE_DATAIN;
2167 	    return INTR_IDLE;
2168 
2169 	case 0x8a:			/* command out					*/
2170 	    /* MESSAGE IN -> COMMAND */
2171 	    acornscsi_sendcommand(host);/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED	*/
2172 	    break;
2173 
2174 	case 0x8b:			/* status in					*/
2175 					/* -> PHASE_STATUSIN				*/
2176 	    /* MESSAGE IN -> STATUS */
2177 	    acornscsi_readstatusbyte(host);
2178 	    host->scsi.phase = PHASE_STATUSIN;
2179 	    break;
2180 
2181 	case 0x8e:			/* message out					*/
2182 					/* -> PHASE_MSGOUT				*/
2183 	    /* MESSAGE IN -> MESSAGE OUT */
2184 	    acornscsi_sendmessage(host);
2185 	    break;
2186 
2187 	case 0x8f:			/* message in					*/
2188 	    acornscsi_message(host);	/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2189 	    break;
2190 
2191 	default:
2192 	    printk(KERN_ERR "scsi%d.%c: PHASE_RECONNECTED, SSR %02X after reconnect?\n",
2193 		    host->host->host_no, acornscsi_target(host), ssr);
2194 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2195 	}
2196 	return INTR_PROCESSING;
2197 
2198     case PHASE_DATAIN:			/* STATE: transferred data in			*/
2199 	/*
2200 	 * This is simple - if we disconnect then the DMA address & count is
2201 	 * correct.
2202 	 */
2203 	switch (ssr) {
2204 	case 0x19:			/* -> PHASE_DATAIN				*/
2205 	case 0x89:			/* -> PHASE_DATAIN				*/
2206 	    acornscsi_abortcmd(host, host->SCpnt->tag);
2207 	    return INTR_IDLE;
2208 
2209 	case 0x1b:			/* -> PHASE_STATUSIN				*/
2210 	case 0x4b:			/* -> PHASE_STATUSIN				*/
2211 	case 0x8b:			/* -> PHASE_STATUSIN				*/
2212 	    /* DATA IN -> STATUS */
2213 	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2214 					  acornscsi_sbic_xfcount(host);
2215 	    acornscsi_dma_stop(host);
2216 	    acornscsi_readstatusbyte(host);
2217 	    host->scsi.phase = PHASE_STATUSIN;
2218 	    break;
2219 
2220 	case 0x1e:			/* -> PHASE_MSGOUT				*/
2221 	case 0x4e:			/* -> PHASE_MSGOUT				*/
2222 	case 0x8e:			/* -> PHASE_MSGOUT				*/
2223 	    /* DATA IN -> MESSAGE OUT */
2224 	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2225 					  acornscsi_sbic_xfcount(host);
2226 	    acornscsi_dma_stop(host);
2227 	    acornscsi_sendmessage(host);
2228 	    break;
2229 
2230 	case 0x1f:			/* message in					*/
2231 	case 0x4f:			/* message in					*/
2232 	case 0x8f:			/* message in					*/
2233 	    /* DATA IN -> MESSAGE IN */
2234 	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2235 					  acornscsi_sbic_xfcount(host);
2236 	    acornscsi_dma_stop(host);
2237 	    acornscsi_message(host);	/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2238 	    break;
2239 
2240 	default:
2241 	    printk(KERN_ERR "scsi%d.%c: PHASE_DATAIN, SSR %02X?\n",
2242 		    host->host->host_no, acornscsi_target(host), ssr);
2243 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2244 	}
2245 	return INTR_PROCESSING;
2246 
2247     case PHASE_DATAOUT: 		/* STATE: transferred data out			*/
2248 	/*
2249 	 * This is more complicated - if we disconnect, the DMA could be 12
2250 	 * bytes ahead of us.  We need to correct this.
2251 	 */
2252 	switch (ssr) {
2253 	case 0x18:			/* -> PHASE_DATAOUT				*/
2254 	case 0x88:			/* -> PHASE_DATAOUT				*/
2255 	    acornscsi_abortcmd(host, host->SCpnt->tag);
2256 	    return INTR_IDLE;
2257 
2258 	case 0x1b:			/* -> PHASE_STATUSIN				*/
2259 	case 0x4b:			/* -> PHASE_STATUSIN				*/
2260 	case 0x8b:			/* -> PHASE_STATUSIN				*/
2261 	    /* DATA OUT -> STATUS */
2262 	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2263 					  acornscsi_sbic_xfcount(host);
2264 	    acornscsi_dma_stop(host);
2265 	    acornscsi_dma_adjust(host);
2266 	    acornscsi_readstatusbyte(host);
2267 	    host->scsi.phase = PHASE_STATUSIN;
2268 	    break;
2269 
2270 	case 0x1e:			/* -> PHASE_MSGOUT				*/
2271 	case 0x4e:			/* -> PHASE_MSGOUT				*/
2272 	case 0x8e:			/* -> PHASE_MSGOUT				*/
2273 	    /* DATA OUT -> MESSAGE OUT */
2274 	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2275 					  acornscsi_sbic_xfcount(host);
2276 	    acornscsi_dma_stop(host);
2277 	    acornscsi_dma_adjust(host);
2278 	    acornscsi_sendmessage(host);
2279 	    break;
2280 
2281 	case 0x1f:			/* message in					*/
2282 	case 0x4f:			/* message in					*/
2283 	case 0x8f:			/* message in					*/
2284 	    /* DATA OUT -> MESSAGE IN */
2285 	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2286 					  acornscsi_sbic_xfcount(host);
2287 	    acornscsi_dma_stop(host);
2288 	    acornscsi_dma_adjust(host);
2289 	    acornscsi_message(host);	/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2290 	    break;
2291 
2292 	default:
2293 	    printk(KERN_ERR "scsi%d.%c: PHASE_DATAOUT, SSR %02X?\n",
2294 		    host->host->host_no, acornscsi_target(host), ssr);
2295 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2296 	}
2297 	return INTR_PROCESSING;
2298 
2299     case PHASE_STATUSIN:		/* STATE: status in complete			*/
2300 	switch (ssr) {
2301 	case 0x1f:			/* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2302 	case 0x8f:			/* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2303 	    /* STATUS -> MESSAGE IN */
2304 	    acornscsi_message(host);
2305 	    break;
2306 
2307 	case 0x1e:			/* -> PHASE_MSGOUT				*/
2308 	case 0x8e:			/* -> PHASE_MSGOUT				*/
2309 	    /* STATUS -> MESSAGE OUT */
2310 	    acornscsi_sendmessage(host);
2311 	    break;
2312 
2313 	default:
2314 	    printk(KERN_ERR "scsi%d.%c: PHASE_STATUSIN, SSR %02X instead of MESSAGE_IN?\n",
2315 		    host->host->host_no, acornscsi_target(host), ssr);
2316 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2317 	}
2318 	return INTR_PROCESSING;
2319 
2320     case PHASE_MSGIN:			/* STATE: message in				*/
2321 	switch (ssr) {
2322 	case 0x1e:			/* -> PHASE_MSGOUT				*/
2323 	case 0x4e:			/* -> PHASE_MSGOUT				*/
2324 	case 0x8e:			/* -> PHASE_MSGOUT				*/
2325 	    /* MESSAGE IN -> MESSAGE OUT */
2326 	    acornscsi_sendmessage(host);
2327 	    break;
2328 
2329 	case 0x1f:			/* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2330 	case 0x2f:
2331 	case 0x4f:
2332 	case 0x8f:
2333 	    acornscsi_message(host);
2334 	    break;
2335 
2336 	case 0x85:
2337 	    printk("scsi%d.%c: strange message in disconnection\n",
2338 		host->host->host_no, acornscsi_target(host));
2339 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2340 	    acornscsi_done(host, &host->SCpnt, DID_ERROR);
2341 	    break;
2342 
2343 	default:
2344 	    printk(KERN_ERR "scsi%d.%c: PHASE_MSGIN, SSR %02X after message in?\n",
2345 		    host->host->host_no, acornscsi_target(host), ssr);
2346 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2347 	}
2348 	return INTR_PROCESSING;
2349 
2350     case PHASE_DONE:			/* STATE: received status & message		*/
2351 	switch (ssr) {
2352 	case 0x85:			/* -> PHASE_IDLE				*/
2353 	    acornscsi_done(host, &host->SCpnt, DID_OK);
2354 	    return INTR_NEXT_COMMAND;
2355 
2356 	case 0x1e:
2357 	case 0x8e:
2358 	    acornscsi_sendmessage(host);
2359 	    break;
2360 
2361 	default:
2362 	    printk(KERN_ERR "scsi%d.%c: PHASE_DONE, SSR %02X instead of disconnect?\n",
2363 		    host->host->host_no, acornscsi_target(host), ssr);
2364 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2365 	}
2366 	return INTR_PROCESSING;
2367 
2368     case PHASE_ABORTED:
2369 	switch (ssr) {
2370 	case 0x85:
2371 	    if (host->SCpnt)
2372 		acornscsi_done(host, &host->SCpnt, DID_ABORT);
2373 	    else {
2374 		clear_bit(host->scsi.reconnected.target * 8 + host->scsi.reconnected.lun,
2375 			  host->busyluns);
2376 		host->scsi.phase = PHASE_IDLE;
2377 	    }
2378 	    return INTR_NEXT_COMMAND;
2379 
2380 	case 0x1e:
2381 	case 0x2e:
2382 	case 0x4e:
2383 	case 0x8e:
2384 	    acornscsi_sendmessage(host);
2385 	    break;
2386 
2387 	default:
2388 	    printk(KERN_ERR "scsi%d.%c: PHASE_ABORTED, SSR %02X?\n",
2389 		    host->host->host_no, acornscsi_target(host), ssr);
2390 	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2391 	}
2392 	return INTR_PROCESSING;
2393 
2394     default:
2395 	printk(KERN_ERR "scsi%d.%c: unknown driver phase %d\n",
2396 		host->host->host_no, acornscsi_target(host), ssr);
2397 	acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2398     }
2399     return INTR_PROCESSING;
2400 }
2401 
2402 /*
2403  * Prototype: void acornscsi_intr(int irq, void *dev_id)
2404  * Purpose  : handle interrupts from Acorn SCSI card
2405  * Params   : irq    - interrupt number
2406  *	      dev_id - device specific data (AS_Host structure)
2407  */
2408 static irqreturn_t
2409 acornscsi_intr(int irq, void *dev_id)
2410 {
2411     AS_Host *host = (AS_Host *)dev_id;
2412     intr_ret_t ret;
2413     int iostatus;
2414     int in_irq = 0;
2415 
2416     do {
2417 	ret = INTR_IDLE;
2418 
2419 	iostatus = readb(host->fast + INT_REG);
2420 
2421 	if (iostatus & 2) {
2422 	    acornscsi_dma_intr(host);
2423 	    iostatus = readb(host->fast + INT_REG);
2424 	}
2425 
2426 	if (iostatus & 8)
2427 	    ret = acornscsi_sbicintr(host, in_irq);
2428 
2429 	/*
2430 	 * If we have a transfer pending, start it.
2431 	 * Only start it if the interface has already started transferring
2432 	 * it's data
2433 	 */
2434 	if (host->dma.xfer_required)
2435 	    acornscsi_dma_xfer(host);
2436 
2437 	if (ret == INTR_NEXT_COMMAND)
2438 	    ret = acornscsi_kick(host);
2439 
2440 	in_irq = 1;
2441     } while (ret != INTR_IDLE);
2442 
2443     return IRQ_HANDLED;
2444 }
2445 
2446 /*=============================================================================================
2447  * Interfaces between interrupt handler and rest of scsi code
2448  */
2449 
2450 /*
2451  * Function : acornscsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2452  * Purpose  : queues a SCSI command
2453  * Params   : cmd  - SCSI command
2454  *	      done - function called on completion, with pointer to command descriptor
2455  * Returns  : 0, or < 0 on error.
2456  */
2457 static int acornscsi_queuecmd_lck(struct scsi_cmnd *SCpnt,
2458 		       void (*done)(struct scsi_cmnd *))
2459 {
2460     AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2461 
2462     if (!done) {
2463 	/* there should be some way of rejecting errors like this without panicing... */
2464 	panic("scsi%d: queuecommand called with NULL done function [cmd=%p]",
2465 		host->host->host_no, SCpnt);
2466 	return -EINVAL;
2467     }
2468 
2469 #if (DEBUG & DEBUG_NO_WRITE)
2470     if (acornscsi_cmdtype(SCpnt->cmnd[0]) == CMD_WRITE && (NO_WRITE & (1 << SCpnt->device->id))) {
2471 	printk(KERN_CRIT "scsi%d.%c: WRITE attempted with NO_WRITE flag set\n",
2472 	    host->host->host_no, '0' + SCpnt->device->id);
2473 	SCpnt->result = DID_NO_CONNECT << 16;
2474 	done(SCpnt);
2475 	return 0;
2476     }
2477 #endif
2478 
2479     SCpnt->scsi_done = done;
2480     SCpnt->host_scribble = NULL;
2481     SCpnt->result = 0;
2482     SCpnt->tag = 0;
2483     SCpnt->SCp.phase = (int)acornscsi_datadirection(SCpnt->cmnd[0]);
2484     SCpnt->SCp.sent_command = 0;
2485     SCpnt->SCp.scsi_xferred = 0;
2486 
2487     init_SCp(SCpnt);
2488 
2489     host->stats.queues += 1;
2490 
2491     {
2492 	unsigned long flags;
2493 
2494 	if (!queue_add_cmd_ordered(&host->queues.issue, SCpnt)) {
2495 	    SCpnt->result = DID_ERROR << 16;
2496 	    done(SCpnt);
2497 	    return 0;
2498 	}
2499 	local_irq_save(flags);
2500 	if (host->scsi.phase == PHASE_IDLE)
2501 	    acornscsi_kick(host);
2502 	local_irq_restore(flags);
2503     }
2504     return 0;
2505 }
2506 
2507 DEF_SCSI_QCMD(acornscsi_queuecmd)
2508 
2509 /*
2510  * Prototype: void acornscsi_reportstatus(struct scsi_cmnd **SCpntp1, struct scsi_cmnd **SCpntp2, int result)
2511  * Purpose  : pass a result to *SCpntp1, and check if *SCpntp1 = *SCpntp2
2512  * Params   : SCpntp1 - pointer to command to return
2513  *	      SCpntp2 - pointer to command to check
2514  *	      result  - result to pass back to mid-level done function
2515  * Returns  : *SCpntp2 = NULL if *SCpntp1 is the same command structure as *SCpntp2.
2516  */
2517 static inline void acornscsi_reportstatus(struct scsi_cmnd **SCpntp1,
2518 					  struct scsi_cmnd **SCpntp2,
2519 					  int result)
2520 {
2521 	struct scsi_cmnd *SCpnt = *SCpntp1;
2522 
2523     if (SCpnt) {
2524 	*SCpntp1 = NULL;
2525 
2526 	SCpnt->result = result;
2527 	SCpnt->scsi_done(SCpnt);
2528     }
2529 
2530     if (SCpnt == *SCpntp2)
2531 	*SCpntp2 = NULL;
2532 }
2533 
2534 enum res_abort { res_not_running, res_success, res_success_clear, res_snooze };
2535 
2536 /*
2537  * Prototype: enum res acornscsi_do_abort(struct scsi_cmnd *SCpnt)
2538  * Purpose  : abort a command on this host
2539  * Params   : SCpnt - command to abort
2540  * Returns  : our abort status
2541  */
2542 static enum res_abort acornscsi_do_abort(AS_Host *host, struct scsi_cmnd *SCpnt)
2543 {
2544 	enum res_abort res = res_not_running;
2545 
2546 	if (queue_remove_cmd(&host->queues.issue, SCpnt)) {
2547 		/*
2548 		 * The command was on the issue queue, and has not been
2549 		 * issued yet.  We can remove the command from the queue,
2550 		 * and acknowledge the abort.  Neither the devices nor the
2551 		 * interface know about the command.
2552 		 */
2553 //#if (DEBUG & DEBUG_ABORT)
2554 		printk("on issue queue ");
2555 //#endif
2556 		res = res_success;
2557 	} else if (queue_remove_cmd(&host->queues.disconnected, SCpnt)) {
2558 		/*
2559 		 * The command was on the disconnected queue.  Simply
2560 		 * acknowledge the abort condition, and when the target
2561 		 * reconnects, we will give it an ABORT message.  The
2562 		 * target should then disconnect, and we will clear
2563 		 * the busylun bit.
2564 		 */
2565 //#if (DEBUG & DEBUG_ABORT)
2566 		printk("on disconnected queue ");
2567 //#endif
2568 		res = res_success;
2569 	} else if (host->SCpnt == SCpnt) {
2570 		unsigned long flags;
2571 
2572 //#if (DEBUG & DEBUG_ABORT)
2573 		printk("executing ");
2574 //#endif
2575 
2576 		local_irq_save(flags);
2577 		switch (host->scsi.phase) {
2578 		/*
2579 		 * If the interface is idle, and the command is 'disconnectable',
2580 		 * then it is the same as on the disconnected queue.  We simply
2581 		 * remove all traces of the command.  When the target reconnects,
2582 		 * we will give it an ABORT message since the command could not
2583 		 * be found.  When the target finally disconnects, we will clear
2584 		 * the busylun bit.
2585 		 */
2586 		case PHASE_IDLE:
2587 			if (host->scsi.disconnectable) {
2588 				host->scsi.disconnectable = 0;
2589 				host->SCpnt = NULL;
2590 				res = res_success;
2591 			}
2592 			break;
2593 
2594 		/*
2595 		 * If the command has connected and done nothing further,
2596 		 * simply force a disconnect.  We also need to clear the
2597 		 * busylun bit.
2598 		 */
2599 		case PHASE_CONNECTED:
2600 			sbic_arm_write(host, SBIC_CMND, CMND_DISCONNECT);
2601 			host->SCpnt = NULL;
2602 			res = res_success_clear;
2603 			break;
2604 
2605 		default:
2606 			acornscsi_abortcmd(host, host->SCpnt->tag);
2607 			res = res_snooze;
2608 		}
2609 		local_irq_restore(flags);
2610 	} else if (host->origSCpnt == SCpnt) {
2611 		/*
2612 		 * The command will be executed next, but a command
2613 		 * is currently using the interface.  This is similar to
2614 		 * being on the issue queue, except the busylun bit has
2615 		 * been set.
2616 		 */
2617 		host->origSCpnt = NULL;
2618 //#if (DEBUG & DEBUG_ABORT)
2619 		printk("waiting for execution ");
2620 //#endif
2621 		res = res_success_clear;
2622 	} else
2623 		printk("unknown ");
2624 
2625 	return res;
2626 }
2627 
2628 /*
2629  * Prototype: int acornscsi_abort(struct scsi_cmnd *SCpnt)
2630  * Purpose  : abort a command on this host
2631  * Params   : SCpnt - command to abort
2632  * Returns  : one of SCSI_ABORT_ macros
2633  */
2634 int acornscsi_abort(struct scsi_cmnd *SCpnt)
2635 {
2636 	AS_Host *host = (AS_Host *) SCpnt->device->host->hostdata;
2637 	int result;
2638 
2639 	host->stats.aborts += 1;
2640 
2641 #if (DEBUG & DEBUG_ABORT)
2642 	{
2643 		int asr, ssr;
2644 		asr = sbic_arm_read(host, SBIC_ASR);
2645 		ssr = sbic_arm_read(host, SBIC_SSR);
2646 
2647 		printk(KERN_WARNING "acornscsi_abort: ");
2648 		print_sbic_status(asr, ssr, host->scsi.phase);
2649 		acornscsi_dumplog(host, SCpnt->device->id);
2650 	}
2651 #endif
2652 
2653 	printk("scsi%d: ", host->host->host_no);
2654 
2655 	switch (acornscsi_do_abort(host, SCpnt)) {
2656 	/*
2657 	 * We managed to find the command and cleared it out.
2658 	 * We do not expect the command to be executing on the
2659 	 * target, but we have set the busylun bit.
2660 	 */
2661 	case res_success_clear:
2662 //#if (DEBUG & DEBUG_ABORT)
2663 		printk("clear ");
2664 //#endif
2665 		clear_bit(SCpnt->device->id * 8 +
2666 			  (u8)(SCpnt->device->lun & 0x7), host->busyluns);
2667 
2668 	/*
2669 	 * We found the command, and cleared it out.  Either
2670 	 * the command is still known to be executing on the
2671 	 * target, or the busylun bit is not set.
2672 	 */
2673 	case res_success:
2674 //#if (DEBUG & DEBUG_ABORT)
2675 		printk("success\n");
2676 //#endif
2677 		result = SUCCESS;
2678 		break;
2679 
2680 	/*
2681 	 * We did find the command, but unfortunately we couldn't
2682 	 * unhook it from ourselves.  Wait some more, and if it
2683 	 * still doesn't complete, reset the interface.
2684 	 */
2685 	case res_snooze:
2686 //#if (DEBUG & DEBUG_ABORT)
2687 		printk("snooze\n");
2688 //#endif
2689 		result = FAILED;
2690 		break;
2691 
2692 	/*
2693 	 * The command could not be found (either because it completed,
2694 	 * or it got dropped.
2695 	 */
2696 	default:
2697 	case res_not_running:
2698 		acornscsi_dumplog(host, SCpnt->device->id);
2699 		result = FAILED;
2700 //#if (DEBUG & DEBUG_ABORT)
2701 		printk("not running\n");
2702 //#endif
2703 		break;
2704 	}
2705 
2706 	return result;
2707 }
2708 
2709 /*
2710  * Prototype: int acornscsi_reset(struct scsi_cmnd *SCpnt)
2711  * Purpose  : reset a command on this host/reset this host
2712  * Params   : SCpnt  - command causing reset
2713  * Returns  : one of SCSI_RESET_ macros
2714  */
2715 int acornscsi_host_reset(struct scsi_cmnd *SCpnt)
2716 {
2717 	AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2718 	struct scsi_cmnd *SCptr;
2719 
2720     host->stats.resets += 1;
2721 
2722 #if (DEBUG & DEBUG_RESET)
2723     {
2724 	int asr, ssr, devidx;
2725 
2726 	asr = sbic_arm_read(host, SBIC_ASR);
2727 	ssr = sbic_arm_read(host, SBIC_SSR);
2728 
2729 	printk(KERN_WARNING "acornscsi_reset: ");
2730 	print_sbic_status(asr, ssr, host->scsi.phase);
2731 	for (devidx = 0; devidx < 9; devidx++)
2732 	    acornscsi_dumplog(host, devidx);
2733     }
2734 #endif
2735 
2736     acornscsi_dma_stop(host);
2737 
2738     /*
2739      * do hard reset.  This resets all devices on this host, and so we
2740      * must set the reset status on all commands.
2741      */
2742     acornscsi_resetcard(host);
2743 
2744     while ((SCptr = queue_remove(&host->queues.disconnected)) != NULL)
2745 	;
2746 
2747     return SUCCESS;
2748 }
2749 
2750 /*==============================================================================================
2751  * initialisation & miscellaneous support
2752  */
2753 
2754 /*
2755  * Function: char *acornscsi_info(struct Scsi_Host *host)
2756  * Purpose : return a string describing this interface
2757  * Params  : host - host to give information on
2758  * Returns : a constant string
2759  */
2760 const
2761 char *acornscsi_info(struct Scsi_Host *host)
2762 {
2763     static char string[100], *p;
2764 
2765     p = string;
2766 
2767     p += sprintf(string, "%s at port %08lX irq %d v%d.%d.%d"
2768 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2769     " SYNC"
2770 #endif
2771 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
2772     " TAG"
2773 #endif
2774 #if (DEBUG & DEBUG_NO_WRITE)
2775     " NOWRITE (" __stringify(NO_WRITE) ")"
2776 #endif
2777 		, host->hostt->name, host->io_port, host->irq,
2778 		VER_MAJOR, VER_MINOR, VER_PATCH);
2779     return string;
2780 }
2781 
2782 static int acornscsi_show_info(struct seq_file *m, struct Scsi_Host *instance)
2783 {
2784     int devidx;
2785     struct scsi_device *scd;
2786     AS_Host *host;
2787 
2788     host  = (AS_Host *)instance->hostdata;
2789 
2790     seq_printf(m, "AcornSCSI driver v%d.%d.%d"
2791 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2792     " SYNC"
2793 #endif
2794 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
2795     " TAG"
2796 #endif
2797 #if (DEBUG & DEBUG_NO_WRITE)
2798     " NOWRITE (" __stringify(NO_WRITE) ")"
2799 #endif
2800 		"\n\n", VER_MAJOR, VER_MINOR, VER_PATCH);
2801 
2802     seq_printf(m,	"SBIC: WD33C93A  Address: %p    IRQ : %d\n",
2803 			host->base + SBIC_REGIDX, host->scsi.irq);
2804 #ifdef USE_DMAC
2805     seq_printf(m,	"DMAC: uPC71071  Address: %p  IRQ : %d\n\n",
2806 			host->base + DMAC_OFFSET, host->scsi.irq);
2807 #endif
2808 
2809     seq_printf(m,	"Statistics:\n"
2810 			"Queued commands: %-10u    Issued commands: %-10u\n"
2811 			"Done commands  : %-10u    Reads          : %-10u\n"
2812 			"Writes         : %-10u    Others         : %-10u\n"
2813 			"Disconnects    : %-10u    Aborts         : %-10u\n"
2814 			"Resets         : %-10u\n\nLast phases:",
2815 			host->stats.queues,		host->stats.removes,
2816 			host->stats.fins,		host->stats.reads,
2817 			host->stats.writes,		host->stats.miscs,
2818 			host->stats.disconnects,	host->stats.aborts,
2819 			host->stats.resets);
2820 
2821     for (devidx = 0; devidx < 9; devidx ++) {
2822 	unsigned int statptr, prev;
2823 
2824 	seq_printf(m, "\n%c:", devidx == 8 ? 'H' : ('0' + devidx));
2825 	statptr = host->status_ptr[devidx] - 10;
2826 
2827 	if ((signed int)statptr < 0)
2828 	    statptr += STATUS_BUFFER_SIZE;
2829 
2830 	prev = host->status[devidx][statptr].when;
2831 
2832 	for (; statptr != host->status_ptr[devidx]; statptr = (statptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
2833 	    if (host->status[devidx][statptr].when) {
2834 		seq_printf(m, "%c%02X:%02X+%2ld",
2835 			host->status[devidx][statptr].irq ? '-' : ' ',
2836 			host->status[devidx][statptr].ph,
2837 			host->status[devidx][statptr].ssr,
2838 			(host->status[devidx][statptr].when - prev) < 100 ?
2839 				(host->status[devidx][statptr].when - prev) : 99);
2840 		prev = host->status[devidx][statptr].when;
2841 	    }
2842 	}
2843     }
2844 
2845     seq_printf(m, "\nAttached devices:\n");
2846 
2847     shost_for_each_device(scd, instance) {
2848 	seq_printf(m, "Device/Lun TaggedQ      Sync\n");
2849 	seq_printf(m, "     %d/%llu   ", scd->id, scd->lun);
2850 	if (scd->tagged_supported)
2851 		seq_printf(m, "%3sabled(%3d) ",
2852 			     scd->simple_tags ? "en" : "dis",
2853 			     scd->current_tag);
2854 	else
2855 		seq_printf(m, "unsupported  ");
2856 
2857 	if (host->device[scd->id].sync_xfer & 15)
2858 		seq_printf(m, "offset %d, %d ns\n",
2859 			     host->device[scd->id].sync_xfer & 15,
2860 			     acornscsi_getperiod(host->device[scd->id].sync_xfer));
2861 	else
2862 		seq_printf(m, "async\n");
2863 
2864     }
2865     return 0;
2866 }
2867 
2868 static struct scsi_host_template acornscsi_template = {
2869 	.module			= THIS_MODULE,
2870 	.show_info		= acornscsi_show_info,
2871 	.name			= "AcornSCSI",
2872 	.info			= acornscsi_info,
2873 	.queuecommand		= acornscsi_queuecmd,
2874 	.eh_abort_handler	= acornscsi_abort,
2875 	.eh_host_reset_handler	= acornscsi_host_reset,
2876 	.can_queue		= 16,
2877 	.this_id		= 7,
2878 	.sg_tablesize		= SG_ALL,
2879 	.cmd_per_lun		= 2,
2880 	.dma_boundary		= PAGE_SIZE - 1,
2881 	.proc_name		= "acornscsi",
2882 };
2883 
2884 static int acornscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
2885 {
2886 	struct Scsi_Host *host;
2887 	AS_Host *ashost;
2888 	int ret;
2889 
2890 	ret = ecard_request_resources(ec);
2891 	if (ret)
2892 		goto out;
2893 
2894 	host = scsi_host_alloc(&acornscsi_template, sizeof(AS_Host));
2895 	if (!host) {
2896 		ret = -ENOMEM;
2897 		goto out_release;
2898 	}
2899 
2900 	ashost = (AS_Host *)host->hostdata;
2901 
2902 	ashost->base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
2903 	ashost->fast = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
2904 	if (!ashost->base || !ashost->fast) {
2905 		ret = -ENOMEM;
2906 		goto out_put;
2907 	}
2908 
2909 	host->irq = ec->irq;
2910 	ashost->host = host;
2911 	ashost->scsi.irq = host->irq;
2912 
2913 	ec->irqaddr	= ashost->fast + INT_REG;
2914 	ec->irqmask	= 0x0a;
2915 
2916 	ret = request_irq(host->irq, acornscsi_intr, 0, "acornscsi", ashost);
2917 	if (ret) {
2918 		printk(KERN_CRIT "scsi%d: IRQ%d not free: %d\n",
2919 			host->host_no, ashost->scsi.irq, ret);
2920 		goto out_put;
2921 	}
2922 
2923 	memset(&ashost->stats, 0, sizeof (ashost->stats));
2924 	queue_initialise(&ashost->queues.issue);
2925 	queue_initialise(&ashost->queues.disconnected);
2926 	msgqueue_initialise(&ashost->scsi.msgs);
2927 
2928 	acornscsi_resetcard(ashost);
2929 
2930 	ret = scsi_add_host(host, &ec->dev);
2931 	if (ret)
2932 		goto out_irq;
2933 
2934 	scsi_scan_host(host);
2935 	goto out;
2936 
2937  out_irq:
2938 	free_irq(host->irq, ashost);
2939 	msgqueue_free(&ashost->scsi.msgs);
2940 	queue_free(&ashost->queues.disconnected);
2941 	queue_free(&ashost->queues.issue);
2942  out_put:
2943 	ecardm_iounmap(ec, ashost->fast);
2944 	ecardm_iounmap(ec, ashost->base);
2945 	scsi_host_put(host);
2946  out_release:
2947 	ecard_release_resources(ec);
2948  out:
2949 	return ret;
2950 }
2951 
2952 static void acornscsi_remove(struct expansion_card *ec)
2953 {
2954 	struct Scsi_Host *host = ecard_get_drvdata(ec);
2955 	AS_Host *ashost = (AS_Host *)host->hostdata;
2956 
2957 	ecard_set_drvdata(ec, NULL);
2958 	scsi_remove_host(host);
2959 
2960 	/*
2961 	 * Put card into RESET state
2962 	 */
2963 	writeb(0x80, ashost->fast + PAGE_REG);
2964 
2965 	free_irq(host->irq, ashost);
2966 
2967 	msgqueue_free(&ashost->scsi.msgs);
2968 	queue_free(&ashost->queues.disconnected);
2969 	queue_free(&ashost->queues.issue);
2970 	ecardm_iounmap(ec, ashost->fast);
2971 	ecardm_iounmap(ec, ashost->base);
2972 	scsi_host_put(host);
2973 	ecard_release_resources(ec);
2974 }
2975 
2976 static const struct ecard_id acornscsi_cids[] = {
2977 	{ MANU_ACORN, PROD_ACORN_SCSI },
2978 	{ 0xffff, 0xffff },
2979 };
2980 
2981 static struct ecard_driver acornscsi_driver = {
2982 	.probe		= acornscsi_probe,
2983 	.remove		= acornscsi_remove,
2984 	.id_table	= acornscsi_cids,
2985 	.drv = {
2986 		.name		= "acornscsi",
2987 	},
2988 };
2989 
2990 static int __init acornscsi_init(void)
2991 {
2992 	return ecard_register_driver(&acornscsi_driver);
2993 }
2994 
2995 static void __exit acornscsi_exit(void)
2996 {
2997 	ecard_remove_driver(&acornscsi_driver);
2998 }
2999 
3000 module_init(acornscsi_init);
3001 module_exit(acornscsi_exit);
3002 
3003 MODULE_AUTHOR("Russell King");
3004 MODULE_DESCRIPTION("AcornSCSI driver");
3005 MODULE_LICENSE("GPL");
3006