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