patch-2.1.88 linux/drivers/acorn/scsi/acornscsi.c

Next file: linux/drivers/acorn/scsi/acornscsi.h
Previous file: linux/drivers/acorn/scsi/acornscsi-io.S
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.1.87/linux/drivers/acorn/scsi/acornscsi.c linux/drivers/acorn/scsi/acornscsi.c
@@ -0,0 +1,2870 @@
+/*
+ * linux/arch/arm/drivers/scsi/acornscsi.c
+ *
+ *  Acorn SCSI 3 driver
+ *  By R.M.King.
+ *
+ * Abandoned using the Select and Transfer command since there were
+ * some nasty races between our software and the target devices that
+ * were not easy to solve, and the device errata had a lot of entries
+ * for this command, some of them quite nasty...
+ *
+ * Changelog:
+ *  26-Sep-1997	RMK	Re-jigged to use the queue module.
+ *			Re-coded state machine to be based on driver
+ *			state not scsi state.  Should be easier to debug.
+ *			Added acornscsi_release to clean up properly.
+ *			Updated proc/scsi reporting.
+ *  05-Oct-1997	RMK	Implemented writing to SCSI devices.
+ *  06-Oct-1997	RMK	Corrected small (non-serious) bug with the connect/
+ *			reconnect race condition causing a warning message.
+ *  12-Oct-1997	RMK	Added catch for re-entering interrupt routine.
+ *  15-Oct-1997	RMK	Improved handling of commands.
+ */
+#define DEBUG_NO_WRITE	1
+#define DEBUG_QUEUES	2
+#define DEBUG_DMA	4
+#define DEBUG_ABORT	8
+#define DEBUG_DISCON	16
+#define DEBUG_CONNECT	32
+#define DEBUG_PHASES	64
+#define DEBUG_WRITE	128
+#define DEBUG_LINK	256
+#define DEBUG_MESSAGES	512
+#define DEBUG_RESET	1024
+#define DEBUG_ALL	(DEBUG_RESET|DEBUG_MESSAGES|DEBUG_LINK|DEBUG_WRITE|\
+			 DEBUG_PHASES|DEBUG_CONNECT|DEBUG_DISCON|DEBUG_ABORT|\
+			 DEBUG_DMA|DEBUG_QUEUES|DEBUG_NO_WRITE)
+
+/* DRIVER CONFIGURATION
+ *
+ * SCSI-II Tagged queue support.
+ *
+ * I don't have any SCSI devices that support it, so it is totally untested
+ * (except to make sure that it doesn't interfere with any non-tagging
+ * devices).  It is not fully implemented either - what happens when a
+ * tagging device reconnects???
+ *
+ * You can tell if you have a device that supports tagged queueing my
+ * cating (eg) /proc/scsi/acornscsi/0 and see if the SCSI revision is reported
+ * as '2 TAG'.
+ *
+ * Also note that CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE is normally set in the config
+ * scripts, but disabled here.  Once debugged, remove the #undef, otherwise to debug,
+ * comment out the undef.
+ */
+#undef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
+/*
+ * SCSI-II Linked command support.
+ *
+ * The higher level code doesn't support linked commands yet, and so the option
+ * is undef'd here.
+ */
+#undef CONFIG_SCSI_ACORNSCSI_LINK
+/*
+ * SCSI-II Synchronous transfer support.
+ *
+ * Tried and tested...
+ *
+ * SDTR_SIZE	  - maximum number of un-acknowledged bytes (0 = off, 12 = max)
+ * SDTR_PERIOD	  - period of REQ signal (min=125, max=1020)
+ * DEFAULT_PERIOD - default REQ period.
+ */
+#define SDTR_SIZE	12
+#define SDTR_PERIOD	125
+#define DEFAULT_PERIOD	500
+
+/*
+ * Debugging information
+ *
+ * DEBUG	  - bit mask from list above
+ * DEBUG_TARGET   - is defined to the target number if you want to debug
+ *		    a specific target. [only recon/write/dma].
+ */
+#define DEBUG (DEBUG_RESET|DEBUG_WRITE|DEBUG_NO_WRITE)
+/* only allow writing to SCSI device 0 */
+#define NO_WRITE 0xFE
+/*#define DEBUG_TARGET 2*/
+/*
+ * Select timeout time (in 10ms units)
+ *
+ * This is the timeout used between the start of selection and the WD33C93
+ * chip deciding that the device isn't responding.
+ */
+#define TIMEOUT_TIME 10
+/*
+ * Define this if you want to have verbose explaination of SCSI
+ * status/messages.
+ */
+#undef CONFIG_ACORNSCSI_CONSTANTS
+/*
+ * Define this if you want to use the on board DMAC [don't remove this option]
+ * If not set, then use PIO mode (not currently supported).
+ */
+#define USE_DMAC
+/*
+ * List of devices that the driver will recognise
+ */
+#define ACORNSCSI_LIST { MANU_ACORN, PROD_ACORN_SCSI }
+/*
+ * ====================================================================================
+ */
+
+#ifdef DEBUG_TARGET
+#define DBG(cmd,xxx...) \
+  if (cmd->target == DEBUG_TARGET) { \
+    xxx; \
+  }
+#else
+#define DBG(cmd,xxx...) xxx
+#endif
+
+#ifndef STRINGIFY
+#define STRINGIFY(x) #x
+#endif
+#define STR(x) STRINGIFY(x)
+#define NO_WRITE_STR STR(NO_WRITE)
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/sched.h>
+#include <linux/string.h>
+#include <linux/signal.h>
+#include <linux/errno.h>
+#include <linux/proc_fs.h>
+#include <linux/stat.h>
+#include <linux/ioport.h>
+#include <linux/blk.h>
+#include <asm/bitops.h>
+#include <asm/delay.h>
+#include <asm/system.h>
+#include <asm/io.h>
+#include <asm/ecard.h>
+
+#include "../../scsi/scsi.h"
+#include "../../scsi/hosts.h"
+#include "../../scsi/constants.h"
+#include "acornscsi.h"
+
+#define VER_MAJOR 2
+#define VER_MINOR 0
+#define VER_PATCH 5
+
+#ifndef ABORT_TAG
+#define ABORT_TAG 0xd
+#else
+#error "Yippee!  ABORT TAG is now defined!  Remove this error!"
+#endif
+
+#ifndef NO_IRQ
+#define NO_IRQ 255
+#endif
+
+#ifdef CONFIG_SCSI_ACORNSCSI_LINK
+#error SCSI2 LINKed commands not supported (yet)!
+#endif
+
+#ifdef USE_DMAC
+/*
+ * DMAC setup parameters
+ */ 
+#define INIT_DEVCON0	(DEVCON0_RQL|DEVCON0_EXW|DEVCON0_CMP)
+#define INIT_DEVCON1	(DEVCON1_BHLD)
+#define DMAC_READ	(MODECON_READ)
+#define DMAC_WRITE	(MODECON_WRITE)
+#define INIT_SBICDMA	(CTRL_DMABURST)
+
+/*
+ * Size of on-board DMA buffer
+ */
+#define DMAC_BUFFER_SIZE	65536
+#endif
+
+/*
+ * This is used to dump the previous states of the SBIC
+ */
+static struct status_entry {
+	unsigned long	when;
+	unsigned char	ssr;
+	unsigned char	ph;
+	unsigned char	irq;
+	unsigned char	unused;
+} status[9][16];
+static unsigned char status_ptr[9];
+
+#define ADD_STATUS(_q,_ssr,_ph,_irq) \
+({							\
+	status[(_q)][status_ptr[(_q)]].when = jiffies;	\
+	status[(_q)][status_ptr[(_q)]].ssr  = (_ssr);	\
+	status[(_q)][status_ptr[(_q)]].ph   = (_ph);	\
+	status[(_q)][status_ptr[(_q)]].irq  = (_irq);	\
+	status_ptr[(_q)] = (status_ptr[(_q)] + 1) & 15;	\
+})
+
+unsigned int sdtr_period = SDTR_PERIOD;
+unsigned int sdtr_size   = SDTR_SIZE;
+
+static struct proc_dir_entry proc_scsi_acornscsi = {
+	PROC_SCSI_EATA, 9, "acornscsi", S_IFDIR | S_IRUGO | S_IXUGO, 2
+};
+
+static void acornscsi_done (AS_Host *host, Scsi_Cmnd **SCpntp, unsigned int result);
+static int acornscsi_reconnect_finish (AS_Host *host);
+static void acornscsi_dma_cleanup (AS_Host *host);
+static void acornscsi_abortcmd (AS_Host *host, unsigned char tag);
+
+/* ====================================================================================
+ * Miscellaneous
+ */
+
+static inline void
+sbic_arm_write (unsigned int io_port, int reg, int value)
+{
+    outb_t (reg, io_port);
+    outb_t (value, io_port + 4);
+}
+
+#define sbic_arm_writenext(io,val) \
+	outb_t ((val), (io) + 4)
+
+static inline
+int sbic_arm_read (unsigned int io_port, int reg)
+{
+    if(reg == ASR)
+	   return inl_t(io_port) & 255;
+    outb_t (reg, io_port);
+    return inl_t(io_port + 4) & 255;
+}
+
+#define sbic_arm_readnext(io) \
+	inb_t((io) + 4)
+
+#ifdef USE_DMAC
+#define dmac_read(io_port,reg) \
+	inb ((io_port) + (reg))
+
+#define dmac_write(io_port,reg,value) \
+	({ outb ((value), (io_port) + (reg)); })
+
+#define dmac_clearintr(io_port) \
+	({ outb (0, (io_port)); })
+
+static inline
+unsigned int dmac_address (unsigned int io_port)
+{
+    return dmac_read (io_port, TXADRHI) << 16 |
+	   dmac_read (io_port, TXADRMD) << 8 |
+	   dmac_read (io_port, TXADRLO);
+}
+#endif
+
+static
+unsigned long acornscsi_sbic_xfcount (AS_Host *host)
+{
+    unsigned long length;
+
+    length = sbic_arm_read (host->scsi.io_port, TRANSCNTH) << 16;
+    length |= sbic_arm_readnext (host->scsi.io_port) << 8;
+    length |= sbic_arm_readnext (host->scsi.io_port);
+
+    return length;
+}
+
+static
+int acornscsi_sbic_issuecmd (AS_Host *host, int command)
+{
+    int asr;
+
+    do {
+	asr = sbic_arm_read (host->scsi.io_port, ASR);
+    } while (asr & ASR_CIP);
+
+    sbic_arm_write (host->scsi.io_port, CMND, command);
+
+    return 0;
+}
+
+static void
+acornscsi_csdelay (unsigned int cs)
+{
+    unsigned long target_jiffies, flags;
+
+    target_jiffies = jiffies + 1 + cs * HZ / 100;
+
+    save_flags (flags);
+    sti ();
+
+    while (jiffies < target_jiffies) barrier();
+
+    restore_flags (flags);
+}
+
+static
+void acornscsi_resetcard (AS_Host *host)
+{
+    unsigned int i;
+
+    /* assert reset line */
+    host->card.page_reg = 0x80;
+    outb (host->card.page_reg, host->card.io_page);
+
+    /* wait 3 cs.  SCSI standard says 25ms. */
+    acornscsi_csdelay (3);
+
+    host->card.page_reg = 0;
+    outb (host->card.page_reg, host->card.io_page);
+
+    /*
+     * Should get a reset from the card
+     */
+    while (!(inb (host->card.io_intr) & 8));
+    sbic_arm_read (host->scsi.io_port, ASR);
+    sbic_arm_read (host->scsi.io_port, SSR);
+
+    /* setup sbic - WD33C93A */
+    sbic_arm_write (host->scsi.io_port, OWNID, OWNID_EAF | host->host->this_id);
+    sbic_arm_write (host->scsi.io_port, CMND, CMND_RESET);
+
+    /*
+     * Command should cause a reset interrupt
+     */
+    while (!(inb (host->card.io_intr) & 8));
+    sbic_arm_read (host->scsi.io_port, ASR);
+    if (sbic_arm_read (host->scsi.io_port, SSR) != 0x01)
+	printk (KERN_CRIT "scsi%d: WD33C93A didn't give enhanced reset interrupt\n",
+		host->host->host_no);
+
+    sbic_arm_write (host->scsi.io_port, CTRL, INIT_SBICDMA | CTRL_IDI);
+    sbic_arm_write (host->scsi.io_port, TIMEOUT, TIMEOUT_TIME);
+    sbic_arm_write (host->scsi.io_port, SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
+    sbic_arm_write (host->scsi.io_port, SOURCEID, SOURCEID_ER | SOURCEID_DSP);
+
+    host->card.page_reg = 0x40;
+    outb (host->card.page_reg, host->card.io_page);
+
+#ifdef USE_DMAC
+    /* setup dmac - uPC71071 */
+    dmac_write (host->dma.io_port, INIT, 0);
+    dmac_write (host->dma.io_port, INIT, INIT_8BIT);
+    dmac_write (host->dma.io_port, CHANNEL, CHANNEL_0);
+    dmac_write (host->dma.io_port, DEVCON0, INIT_DEVCON0);
+    dmac_write (host->dma.io_port, DEVCON1, INIT_DEVCON1);
+#else
+    dmac_write (host->dma.io_port, INIT, 0);
+#endif
+
+    host->SCpnt = NULL;
+    host->scsi.phase = PHASE_IDLE;
+    host->scsi.disconnectable = 0;
+
+    for (i = 0; i < 8; i++) {
+	host->busyluns[i] = 0;
+	host->device[i].sync_state = SYNC_NEGOCIATE;
+	host->device[i].disconnect_ok = 1;
+    }
+
+    /* wait 25 cs.  SCSI standard says 250ms. */
+    acornscsi_csdelay (25);
+}
+
+/*=============================================================================================
+ * Utility routines (eg. debug)
+ */
+#ifdef CONFIG_ACORNSCSI_CONSTANTS
+static char *acornscsi_interrupttype[] = {
+  "rst",  "suc",  "p/a",  "3",
+  "term", "5",	  "6",	  "7",
+  "serv", "9",	  "a",	  "b",
+  "c",	  "d",	  "e",	  "f"
+};
+
+static signed char acornscsi_map[] = {
+  0,  1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
+ -1,  2, -1, -1,  -1, -1,  3, -1,   4,	5,  6,	7,   8,  9, 10, 11,
+ 12, 13, 14, -1,  -1, -1, -1, -1,   4,	5,  6,	7,   8,  9, 10, 11,
+ -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
+ 15, 16, 17, 18,  19, -1, -1, 20,   4,	5,  6,	7,   8,  9, 10, 11,
+ -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
+ -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
+ -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
+ 21, 22, -1, -1,  -1, 23, -1, -1,   4,	5,  6,	7,   8,  9, 10, 11,
+ -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
+ -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
+ -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
+ -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
+ -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
+ -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
+ -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1
+};      
+
+static char *acornscsi_interruptcode[] = {
+    /* 0 */
+    "reset - normal mode",	/* 00 */
+    "reset - advanced mode",	/* 01 */
+
+    /* 2 */
+    "sel",			/* 11 */
+    "sel+xfer", 		/* 16 */
+    "data-out", 		/* 18 */
+    "data-in",			/* 19 */
+    "cmd",			/* 1A */
+    "stat",			/* 1B */
+    "??-out",			/* 1C */
+    "??-in",			/* 1D */
+    "msg-out",			/* 1E */
+    "msg-in",			/* 1F */
+
+    /* 12 */
+    "/ACK asserted",		/* 20 */
+    "save-data-ptr",		/* 21 */
+    "{re}sel",			/* 22 */
+
+    /* 15 */
+    "inv cmd",			/* 40 */
+    "unexpected disconnect",	/* 41 */
+    "sel timeout",		/* 42 */
+    "P err",			/* 43 */
+    "P err+ATN",		/* 44 */
+    "bad status byte",		/* 47 */
+
+    /* 21 */
+    "resel, no id",		/* 80 */
+    "resel",			/* 81 */
+    "discon",			/* 85 */
+};
+
+static
+void print_scsi_status (unsigned int ssr)
+{
+    if (acornscsi_map[ssr] != -1)
+	printk ("%s:%s",
+		acornscsi_interrupttype[(ssr >> 4)],
+		acornscsi_interruptcode[acornscsi_map[ssr]]);
+    else
+	printk ("%X:%X", ssr >> 4, ssr & 0x0f);    
+}    
+#endif
+
+static
+void print_sbic_status (int asr, int ssr, int cmdphase)
+{
+#ifdef CONFIG_ACORNSCSI_CONSTANTS
+    printk ("sbic: %c%c%c%c%c%c ",
+	    asr & ASR_INT ? 'I' : 'i',
+	    asr & ASR_LCI ? 'L' : 'l',
+	    asr & ASR_BSY ? 'B' : 'b',
+	    asr & ASR_CIP ? 'C' : 'c',
+	    asr & ASR_PE  ? 'P' : 'p',
+	    asr & ASR_DBR ? 'D' : 'd');
+    printk ("scsi: ");
+    print_scsi_status (ssr);
+    printk (" ph %02X\n", cmdphase);
+#else
+    printk ("sbic: %02X scsi: %X:%X ph: %02X\n",
+	    asr, (ssr & 0xf0)>>4, ssr & 0x0f, cmdphase);
+#endif
+}
+
+static
+void acornscsi_dumplog (AS_Host *host, int target)
+{
+    unsigned int prev;
+    do {
+	signed int statptr;
+
+	printk ("%c:", target == 8 ? 'H' : ('0' + target));
+	statptr = status_ptr[target] - 10;
+
+	if (statptr < 0)
+	    statptr += 16;
+
+	prev = status[target][statptr].when;
+
+	for (; statptr != status_ptr[target]; statptr = (statptr + 1) & 15) {
+	    if (status[target][statptr].when) {
+#ifdef CONFIG_ACORNSCSI_CONSTANTS
+		printk ("%c%02X:S=",
+			status[target][statptr].irq ? '-' : ' ',
+			status[target][statptr].ph);
+		print_scsi_status (status[target][statptr].ssr);
+#else
+		printk ("%c%02X:%02X",
+			status[target][statptr].irq ? '-' : ' ',
+			status[target][statptr].ph,
+			status[target][statptr].ssr);
+#endif
+		printk ("+%02ld",
+			(status[target][statptr].when - prev) < 100 ?
+				(status[target][statptr].when - prev) : 99);
+		prev = status[target][statptr].when;
+	    }
+	}
+	printk ("\n");
+	if (target == 8)
+	    break;
+	target = 8;
+    } while (1);
+}
+
+static
+char acornscsi_target (AS_Host *host)
+{
+	if (host->SCpnt)
+		return '0' + host->SCpnt->target;
+	return 'H';
+}
+
+#ifdef USE_DMAC
+static
+void acornscsi_dumpdma (AS_Host *host, char *where)
+{
+	unsigned int mode, addr, len;
+
+	mode = dmac_read (host->dma.io_port, MODECON);
+	addr = dmac_address (host->dma.io_port);
+	len  = dmac_read (host->dma.io_port, TXCNTHI) << 8 |
+	       dmac_read (host->dma.io_port, TXCNTLO);
+
+	printk ("scsi%d: %s: DMAC %02x @%06x+%04x msk %02x, ",
+		host->host->host_no, where,
+		mode, addr, (len + 1) & 0xffff,
+		dmac_read (host->dma.io_port, MASKREG));
+
+	printk ("DMA @%06x, ", host->dma.start_addr);
+	printk ("BH @%p +%04x, ", host->scsi.SCp.ptr,
+		host->scsi.SCp.this_residual);
+	printk ("DT @+%04x ST @+%04x", host->dma.transferred,
+		host->scsi.SCp.have_data_in);
+	printk ("\n");
+}
+#endif
+
+/*
+ * Prototype: cmdtype_t acornscsi_cmdtype (int command)
+ * Purpose  : differentiate READ from WRITE from other commands
+ * Params   : command - command to interpret
+ * Returns  : CMD_READ	- command reads data,
+ *	      CMD_WRITE - command writes data,
+ *	      CMD_MISC	- everything else
+ */
+static inline
+cmdtype_t acornscsi_cmdtype (int command)
+{
+    switch (command) {
+    case WRITE_6:  case WRITE_10:  case WRITE_12:
+	return CMD_WRITE;
+    case READ_6:   case READ_10:   case READ_12:
+	return CMD_READ;
+    default:
+	return CMD_MISC;
+    }
+}
+
+/*
+ * Prototype: int acornscsi_datadirection (int command)
+ * Purpose  : differentiate between commands that have a DATA IN phase
+ *	      and a DATA OUT phase
+ * Params   : command - command to interpret
+ * Returns  : DATADIR_OUT - data out phase expected
+ *	      DATADIR_IN  - data in phase expected
+ */
+static
+datadir_t acornscsi_datadirection (int command)
+{
+    switch (command) {
+    case CHANGE_DEFINITION:	case COMPARE:		case COPY:
+    case COPY_VERIFY:		case LOG_SELECT:	case MODE_SELECT:
+    case MODE_SELECT_10:	case SEND_DIAGNOSTIC:	case WRITE_BUFFER:
+    case FORMAT_UNIT:		case REASSIGN_BLOCKS:	case RESERVE:
+    case SEARCH_EQUAL:		case SEARCH_HIGH:	case SEARCH_LOW:
+    case WRITE_6:		case WRITE_10:		case WRITE_VERIFY:
+    case UPDATE_BLOCK:		case WRITE_LONG:	case WRITE_SAME:
+    case SEARCH_HIGH_12:	case SEARCH_EQUAL_12:	case SEARCH_LOW_12:
+    case WRITE_12:		case WRITE_VERIFY_12:	case SET_WINDOW:
+    case MEDIUM_SCAN:		case SEND_VOLUME_TAG:	case 0xea:
+	return DATADIR_OUT;
+    default:
+	return DATADIR_IN;
+    }
+}
+
+/*
+ * Purpose  : provide values for synchronous transfers with 33C93.
+ * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
+ *	Modified by Russell King for 8MHz WD33C93A
+ */
+static struct sync_xfer_tbl {
+    unsigned int period_ns;
+    unsigned char reg_value;
+} sync_xfer_table[] = {
+    {	1, 0x20 },    { 249, 0x20 },	{ 374, 0x30 },
+    { 499, 0x40 },    { 624, 0x50 },	{ 749, 0x60 },
+    { 874, 0x70 },    { 999, 0x00 },	{   0,	  0 }
+};
+
+/*
+ * Prototype: int acornscsi_getperiod (unsigned char syncxfer)
+ * Purpose  : period for the synchronous transfer setting
+ * Params   : syncxfer SYNCXFER register value
+ * Returns  : period in ns.
+ */
+static
+int acornscsi_getperiod (unsigned char syncxfer)
+{
+    int i;
+
+    syncxfer &= 0xf0;
+    if (syncxfer == 0x10)
+	syncxfer = 0;
+
+    for (i = 1; sync_xfer_table[i].period_ns; i++)
+	if (syncxfer == sync_xfer_table[i].reg_value)
+	    return sync_xfer_table[i].period_ns;
+    return 0;
+}
+
+/*
+ * Prototype: int round_period (unsigned int period)
+ * Purpose  : return index into above table for a required REQ period
+ * Params   : period - time (ns) for REQ
+ * Returns  : table index
+ * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
+ */
+static inline
+int round_period (unsigned int period)
+{
+    int i;
+
+    for (i = 1; sync_xfer_table[i].period_ns; i++) {
+	if ((period <= sync_xfer_table[i].period_ns) &&
+	    (period > sync_xfer_table[i - 1].period_ns))
+	    return i;
+    }
+    return 7;
+}
+
+/*
+ * Prototype: unsigned char calc_sync_xfer (unsigned int period, unsigned int offset)
+ * Purpose  : calculate value for 33c93s SYNC register
+ * Params   : period - time (ns) for REQ
+ *	      offset - offset in bytes between REQ/ACK
+ * Returns  : value for SYNC register
+ * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
+ */
+static
+unsigned char calc_sync_xfer (unsigned int period, unsigned int offset)
+{
+    return sync_xfer_table[round_period(period)].reg_value |
+		((offset < SDTR_SIZE) ? offset : SDTR_SIZE);
+}
+
+/* ====================================================================================
+ * Command functions
+ */
+/*
+ * Function: acornscsi_kick (AS_Host *host)
+ * Purpose : kick next command to interface
+ * Params  : host - host to send command to
+ * Returns : INTR_IDLE if idle, otherwise INTR_PROCESSING
+ * Notes   : interrupts are always disabled!
+ */
+static
+intr_ret_t acornscsi_kick (AS_Host *host)
+{
+    int from_queue = 0;
+    Scsi_Cmnd *SCpnt;
+
+    /* first check to see if a command is waiting to be executed */
+    SCpnt = host->origSCpnt;
+    host->origSCpnt = NULL;
+
+    /* retrieve next command */
+    if (!SCpnt) {
+	SCpnt = queue_remove_exclude (&host->queues.issue, host->busyluns);
+	if (!SCpnt)
+	    return INTR_IDLE;
+
+	from_queue = 1;
+    }
+
+    if (host->scsi.disconnectable && host->SCpnt) {
+	queue_add_cmd_tail (&host->queues.disconnected, host->SCpnt);
+	host->scsi.disconnectable = 0;
+#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
+	DBG(host->SCpnt, printk ("scsi%d.%c: moved command to disconnected queue\n",
+		host->host->host_no, acornscsi_target (host)));
+#endif
+	host->SCpnt = NULL;
+    }
+
+    /*
+     * If we have an interrupt pending, then we may have been reselected.
+     * In this case, we don't want to write to the registers
+     */
+    if (!(sbic_arm_read (host->scsi.io_port, ASR) & (ASR_INT|ASR_BSY|ASR_CIP))) {
+	sbic_arm_write (host->scsi.io_port, DESTID, SCpnt->target);
+	sbic_arm_write (host->scsi.io_port, CMND, CMND_SELWITHATN);
+    }
+
+    /*
+     * claim host busy - all of these must happen atomically wrt
+     * our interrupt routine.  Failure means command loss.
+     */
+    host->scsi.phase = PHASE_CONNECTING;
+    host->SCpnt = SCpnt;
+    host->scsi.SCp = SCpnt->SCp;
+    host->dma.xfer_setup = 0;
+    host->dma.xfer_required = 0;
+
+#if (DEBUG & (DEBUG_ABORT|DEBUG_CONNECT))
+    DBG(SCpnt,printk ("scsi%d.%c: starting cmd %02X\n",
+	    host->host->host_no, '0' + SCpnt->target,
+	    SCpnt->cmnd[0]));
+#endif
+
+    if (from_queue) {
+#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
+	/*
+	 * tagged queueing - allocate a new tag to this command
+	 */
+	if (SCpnt->device->tagged_queue) {
+	    SCpnt->device->current_tag += 1;
+	    if (SCpnt->device->current_tag == 0)
+		SCpnt->device->current_tag = 1;
+	    SCpnt->tag = SCpnt->device->current_tag;
+	} else
+#endif
+	    set_bit (SCpnt->target * 8 + SCpnt->lun, host->busyluns);
+
+	host->stats.removes += 1;
+
+	switch (acornscsi_cmdtype (SCpnt->cmnd[0])) {
+	case CMD_WRITE:
+	    host->stats.writes += 1;
+	    break;
+	case CMD_READ:
+	    host->stats.reads += 1;
+	    break;
+	case CMD_MISC:
+	    host->stats.miscs += 1;
+	    break;
+	}
+    }
+
+    return INTR_PROCESSING;
+}    
+
+/*
+ * Function: void acornscsi_done (AS_Host *host, Scsi_Cmnd **SCpntp, unsigned int result)
+ * Purpose : complete processing for command
+ * Params  : host   - interface that completed
+ *	     result - driver byte of result
+ */
+static
+void acornscsi_done (AS_Host *host, Scsi_Cmnd **SCpntp, unsigned int result)
+{
+    Scsi_Cmnd *SCpnt = *SCpntp;
+
+    /* clean up */
+    sbic_arm_write (host->scsi.io_port, SOURCEID, SOURCEID_ER | SOURCEID_DSP);
+
+    host->stats.fins += 1;
+
+    if (SCpnt) {
+	*SCpntp = NULL;
+
+	acornscsi_dma_cleanup (host);
+
+	SCpnt->result = result << 16 | host->scsi.SCp.Message << 8 | host->scsi.SCp.Status;
+
+	/*
+	 * In theory, this should not happen.  In practice, it seems to.
+	 * Only trigger an error if the device attempts to report all happy
+	 * but with untransferred buffers...  If we don't do something, then
+	 * data loss will occur.  Should we check SCpnt->underflow here?
+	 * It doesn't appear to be set to something meaningful by the higher
+	 * levels all the time.
+	 */
+	if (host->scsi.SCp.ptr && result == DID_OK &&
+		acornscsi_cmdtype (SCpnt->cmnd[0]) != CMD_MISC) {
+	    switch (status_byte (SCpnt->result)) {
+	    case CHECK_CONDITION:
+	    case COMMAND_TERMINATED:
+	    case BUSY:
+	    case QUEUE_FULL:
+	    case RESERVATION_CONFLICT:
+		break;
+
+	    default:
+		printk (KERN_ERR "scsi%d.H: incomplete data transfer detected: result=%08X command=",
+			host->host->host_no, SCpnt->result);
+		print_command (SCpnt->cmnd);
+		acornscsi_dumpdma (host, "done");
+	 	acornscsi_dumplog (host, SCpnt->target);
+		SCpnt->result &= 0xffff;
+		SCpnt->result |= DID_ERROR << 16;
+	    }
+	}
+
+	if (!SCpnt->scsi_done)
+	    panic ("scsi%d.H: null scsi_done function in acornscsi_done", host->host->host_no);
+
+	clear_bit (SCpnt->target * 8 + SCpnt->lun, host->busyluns);
+
+	SCpnt->scsi_done (SCpnt);
+    } else
+	printk ("scsi%d: null command in acornscsi_done", host->host->host_no);
+
+    host->scsi.phase = PHASE_IDLE;
+}
+
+/* ====================================================================================
+ * DMA routines
+ */
+/*
+ * Purpose  : update SCSI Data Pointer
+ * Notes    : this will only be one SG entry or less
+ */
+static
+void acornscsi_data_updateptr (AS_Host *host, Scsi_Pointer *SCp, unsigned int length)
+{
+    SCp->ptr += length;
+    SCp->this_residual -= length;
+
+    if (!SCp->this_residual) {
+	if (SCp->buffers_residual) {
+	    SCp->buffer++;
+	    SCp->buffers_residual--;
+	    SCp->ptr = (char *)SCp->buffer->address;
+	    SCp->this_residual = SCp->buffer->length;
+	} else
+	    SCp->ptr = NULL;
+    }
+}
+
+/*
+ * Prototype: void acornscsi_data_read (AS_Host *host, char *ptr,
+ *				unsigned int start_addr, unsigned int length)
+ * Purpose  : read data from DMA RAM
+ * Params   : host - host to transfer from
+ *	      ptr  - DRAM address
+ *	      start_addr - host mem address
+ *	      length - number of bytes to transfer
+ * Notes    : this will only be one SG entry or less
+ */
+static
+void acornscsi_data_read (AS_Host *host, char *ptr,
+				 unsigned int start_addr, unsigned int length)
+{
+    extern void __acornscsi_in (int port, char *buf, int len);
+    unsigned int page, offset, len = length;
+
+    page = (start_addr >> 12);
+    offset = start_addr & ((1 << 12) - 1);
+
+    outb ((page & 0x3f) | host->card.page_reg, host->card.io_page);
+
+    while (len > 0) {
+	unsigned int this_len;
+
+	if (len + offset > (1 << 12))
+	    this_len = (1 << 12) - offset;
+	else
+	    this_len = len;
+
+	__acornscsi_in (host->card.io_ram + (offset << 1), ptr, this_len);
+
+	offset += this_len;
+	ptr += this_len;
+	len -= this_len;
+
+	if (offset == (1 << 12)) {
+	    offset = 0;
+	    page ++;
+	    outb ((page & 0x3f) | host->card.page_reg, host->card.io_page);
+	}
+    }
+    outb (host->card.page_reg, host->card.io_page);
+}
+
+/*
+ * Prototype: void acornscsi_data_write (AS_Host *host, char *ptr,
+ *				unsigned int start_addr, unsigned int length)
+ * Purpose  : write data to DMA RAM
+ * Params   : host - host to transfer from
+ *	      ptr  - DRAM address
+ *	      start_addr - host mem address
+ *	      length - number of bytes to transfer
+ * Notes    : this will only be one SG entry or less
+ */
+static
+void acornscsi_data_write (AS_Host *host, char *ptr,
+				 unsigned int start_addr, unsigned int length)
+{
+    extern void __acornscsi_out (int port, char *buf, int len);
+    unsigned int page, offset, len = length;
+
+    page = (start_addr >> 12);
+    offset = start_addr & ((1 << 12) - 1);
+
+    outb ((page & 0x3f) | host->card.page_reg, host->card.io_page);
+
+    while (len > 0) {
+	unsigned int this_len;
+
+	if (len + offset > (1 << 12))
+	    this_len = (1 << 12) - offset;
+	else
+	    this_len = len;
+
+	__acornscsi_out (host->card.io_ram + (offset << 1), ptr, this_len);
+
+	offset += this_len;
+	ptr += this_len;
+	len -= this_len;
+
+	if (offset == (1 << 12)) {
+	    offset = 0;
+	    page ++;
+	    outb ((page & 0x3f) | host->card.page_reg, host->card.io_page);
+	}
+    }
+    outb (host->card.page_reg, host->card.io_page);
+}
+
+/* =========================================================================================
+ * On-board DMA routines
+ */
+#ifdef USE_DMAC
+/*
+ * Prototype: void acornscsi_dmastop (AS_Host *host)
+ * Purpose  : stop all DMA
+ * Params   : host - host on which to stop DMA
+ * Notes    : This is called when leaving DATA IN/OUT phase,
+ *	      or when interface is RESET
+ */
+static inline
+void acornscsi_dma_stop (AS_Host *host)
+{
+    dmac_write (host->dma.io_port, MASKREG, MASK_ON);
+    dmac_clearintr (host->dma.io_intr_clear);
+
+#if (DEBUG & DEBUG_DMA)
+    DBG(host->SCpnt, acornscsi_dumpdma (host, "stop"));
+#endif
+}
+
+/*
+ * Function: void acornscsi_dma_setup (AS_Host *host, dmadir_t direction)
+ * Purpose : setup DMA controller for data transfer
+ * Params  : host - host to setup
+ *	     direction - data transfer direction
+ * Notes   : This is called when entering DATA I/O phase, not
+ *	     while we're in a DATA I/O phase
+ */
+static
+void acornscsi_dma_setup (AS_Host *host, dmadir_t direction)
+{
+    unsigned int address, length, mode;
+
+    host->dma.direction = direction;
+
+    dmac_write (host->dma.io_port, MASKREG, MASK_ON);
+
+    if (direction == DMA_OUT) {
+#if (DEBUG & DEBUG_NO_WRITE)
+	if (NO_WRITE & (1 << host->SCpnt->target)) {
+	    printk (KERN_CRIT "scsi%d.%c: I can't handle DMA_OUT!\n",
+		    host->host->host_no, acornscsi_target (host));
+	    return;
+	}
+#endif
+	mode = DMAC_WRITE;
+    } else
+	mode = DMAC_READ;
+
+    /*
+     * Allocate some buffer space, limited to half the buffer size
+     */
+    length = min (host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
+    if (length) {
+	host->dma.start_addr = address = host->dma.free_addr;
+	host->dma.free_addr = (host->dma.free_addr + length) &
+				(DMAC_BUFFER_SIZE - 1);
+
+	/*
+	 * Transfer data to DMA memory
+	 */
+	if (direction == DMA_OUT)
+	    acornscsi_data_write (host, host->scsi.SCp.ptr, host->dma.start_addr,
+				length);
+
+	length -= 1;
+	dmac_write (host->dma.io_port, TXCNTLO, length);
+	dmac_write (host->dma.io_port, TXCNTHI, length >> 8);
+	dmac_write (host->dma.io_port, TXADRLO, address);
+	dmac_write (host->dma.io_port, TXADRMD, address >> 8);
+	dmac_write (host->dma.io_port, TXADRHI, 0);
+	dmac_write (host->dma.io_port, MODECON, mode);
+	dmac_write (host->dma.io_port, MASKREG, MASK_OFF);
+
+#if (DEBUG & DEBUG_DMA)
+	DBG(host->SCpnt, acornscsi_dumpdma (host, "strt"));
+#endif
+	host->dma.xfer_setup = 1;
+    }
+}
+
+/*
+ * Function: void acornscsi_dma_cleanup (AS_Host *host)
+ * Purpose : ensure that all DMA transfers are up-to-date & host->scsi.SCp is correct
+ * Params  : host - host to finish
+ * Notes   : This is called when a command is:
+ *		terminating, RESTORE_POINTERS, SAVE_POINTERS, DISCONECT
+ *	   : This must not return until all transfers are completed.
+ */
+static
+void acornscsi_dma_cleanup (AS_Host *host)
+{
+    dmac_write (host->dma.io_port, MASKREG, MASK_ON);
+    dmac_clearintr (host->dma.io_intr_clear);
+
+    /*
+     * Check for a pending transfer
+     */
+    if (host->dma.xfer_required) {
+	host->dma.xfer_required = 0;
+	if (host->dma.direction == DMA_IN)
+	    acornscsi_data_read (host, host->dma.xfer_ptr,
+				 host->dma.xfer_start, host->dma.xfer_length);
+    }
+
+    /*
+     * Has a transfer been setup?
+     */
+    if (host->dma.xfer_setup) {
+	unsigned int transferred;
+
+	host->dma.xfer_setup = 0;
+
+#if (DEBUG & DEBUG_DMA)
+	DBG(host->SCpnt, acornscsi_dumpdma (host, "clup"));
+#endif
+
+	/*
+	 * Calculate number of bytes transferred from DMA.
+	 */
+	transferred = dmac_address (host->dma.io_port) - host->dma.start_addr;
+	host->dma.transferred += transferred;
+
+	if (host->dma.direction == DMA_IN)
+	    acornscsi_data_read (host, host->scsi.SCp.ptr,
+				 host->dma.start_addr, transferred);
+
+	/*
+	 * Update SCSI pointers
+	 */
+	acornscsi_data_updateptr (host, &host->scsi.SCp, transferred);
+    }
+}
+
+/*
+ * Function: void acornscsi_dmacintr (AS_Host *host)
+ * Purpose : handle interrupts from DMAC device
+ * Params  : host - host to process
+ * Notes   : If reading, we schedule the read to main memory &
+ *	     allow the transfer to continue.
+ *	   : If writing, we fill the onboard DMA memory from main
+ *	     memory.
+ *	   : Called whenever DMAC finished it's current transfer.
+ */
+static
+void acornscsi_dma_intr (AS_Host *host)
+{
+    unsigned int address, length, transferred;
+
+#if (DEBUG & DEBUG_DMA)
+    DBG(host->SCpnt, acornscsi_dumpdma (host, "inti"));
+#endif
+
+    dmac_write (host->dma.io_port, MASKREG, MASK_ON);
+    dmac_clearintr (host->dma.io_intr_clear);
+
+    /*
+     * Calculate amount transferred via DMA
+     */
+    transferred = dmac_address (host->dma.io_port) - host->dma.start_addr;
+    host->dma.transferred += transferred;
+
+    /*
+     * Schedule DMA transfer off board
+     */
+    if (host->dma.direction == DMA_IN) {
+	host->dma.xfer_start = host->dma.start_addr;
+	host->dma.xfer_length = transferred;
+	host->dma.xfer_ptr = host->scsi.SCp.ptr;
+	host->dma.xfer_required = 1;
+    }
+
+    acornscsi_data_updateptr (host, &host->scsi.SCp, transferred);
+
+    /*
+     * Allocate some buffer space, limited to half the on-board RAM size
+     */
+    length = min (host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
+    if (length) {
+	host->dma.start_addr = address = host->dma.free_addr;
+	host->dma.free_addr = (host->dma.free_addr + length) &
+				(DMAC_BUFFER_SIZE - 1);
+
+	/*
+	 * Transfer data to DMA memory
+	 */
+	if (host->dma.direction == DMA_OUT)
+	    acornscsi_data_write (host, host->scsi.SCp.ptr, host->dma.start_addr,
+				length);
+
+	length -= 1;
+	dmac_write (host->dma.io_port, TXCNTLO, length);
+	dmac_write (host->dma.io_port, TXCNTHI, length >> 8);
+	dmac_write (host->dma.io_port, TXADRLO, address);
+	dmac_write (host->dma.io_port, TXADRMD, address >> 8);
+	dmac_write (host->dma.io_port, TXADRHI, 0);
+	dmac_write (host->dma.io_port, MASKREG, MASK_OFF);
+
+#if (DEBUG & DEBUG_DMA)
+	DBG(host->SCpnt, acornscsi_dumpdma (host, "into"));
+#endif
+    } else {
+	host->dma.xfer_setup = 0;
+#if 0
+	/*
+	 * If the interface still wants more, then this is an error.
+	 * We give it another byte, but we also attempt to raise an
+	 * attention condition.  We continue giving one byte until
+	 * the device recognises the attention.
+	 */
+	if (dmac_read (host->dma.io_port, STATUS) & STATUS_RQ0) {
+	    acornscsi_abortcmd (host, host->SCpnt->tag);
+
+	    dmac_write (host->dma.io_port, TXCNTLO, 0);
+	    dmac_write (host->dma.io_port, TXCNTHI, 0);
+	    dmac_write (host->dma.io_port, TXADRLO, 0);
+	    dmac_write (host->dma.io_port, TXADRMD, 0);
+	    dmac_write (host->dma.io_port, TXADRHI, 0);
+	    dmac_write (host->dma.io_port, MASKREG, MASK_OFF);
+	}
+#endif
+    }
+}
+
+/*
+ * Function: void acornscsi_dma_xfer (AS_Host *host)
+ * Purpose : transfer data between AcornSCSI and memory
+ * Params  : host - host to process
+ */
+static
+void acornscsi_dma_xfer (AS_Host *host)
+{
+    host->dma.xfer_required = 0;
+
+    if (host->dma.direction == DMA_IN)
+	acornscsi_data_read (host, host->dma.xfer_ptr,
+				host->dma.xfer_start, host->dma.xfer_length);
+}
+
+/*
+ * Function: void acornscsi_dma_adjust (AS_Host *host)
+ * Purpose : adjust DMA pointers & count for bytes transfered to
+ *	     SBIC but not SCSI bus.
+ * Params  : host - host to adjust DMA count for
+ */
+static
+void acornscsi_dma_adjust (AS_Host *host)
+{
+    if (host->dma.xfer_setup) {
+	signed long transferred;
+#if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
+	DBG(host->SCpnt, acornscsi_dumpdma (host, "adji"));
+#endif
+	/*
+	 * Calculate correct DMA address - DMA is ahead of SCSI bus while
+	 * writing.
+	 *  host->scsi.SCp.have_data_in is the number of bytes
+	 *  actually transferred to/from the SCSI bus.
+	 *  host->dma.transferred is the number of bytes transferred
+	 *  over DMA since host->dma.start_addr was last set.
+	 *
+	 * real_dma_addr = host->dma.start_addr + host->scsi.SCp.have_data_in
+	 *		   - host->dma.transferred
+	 */
+	transferred = host->scsi.SCp.have_data_in - host->dma.transferred;
+	if (transferred < 0)
+	    printk ("scsi%d.%c: Ack! DMA write correction %ld < 0!\n",
+		    host->host->host_no, acornscsi_target (host), transferred);
+	else if (transferred == 0)
+	    host->dma.xfer_setup = 0;
+	else {
+	    transferred += host->dma.start_addr;
+	    dmac_write (host->dma.io_port, TXADRLO, transferred);
+	    dmac_write (host->dma.io_port, TXADRMD, transferred >> 8);
+	    dmac_write (host->dma.io_port, TXADRHI, transferred >> 16);
+#if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
+	    DBG(host->SCpnt, acornscsi_dumpdma (host, "adjo"));
+#endif
+	}
+    }
+}
+#endif
+
+/* =========================================================================================
+ * Data I/O
+ */
+/*
+ * Function: void acornscsi_sendcommand (AS_Host *host)
+ * Purpose : send a command to a target
+ * Params  : host - host which is connected to target
+ */
+static
+void acornscsi_sendcommand (AS_Host *host)
+{
+    Scsi_Cmnd *SCpnt = host->SCpnt;
+    unsigned int asr;
+    unsigned char *cmdptr, *cmdend;
+
+    sbic_arm_write (host->scsi.io_port, TRANSCNTH, 0);
+    sbic_arm_writenext (host->scsi.io_port, 0);
+    sbic_arm_writenext (host->scsi.io_port, SCpnt->cmd_len - host->scsi.SCp.sent_command);
+    acornscsi_sbic_issuecmd (host, CMND_XFERINFO);
+
+    cmdptr = SCpnt->cmnd + host->scsi.SCp.sent_command;
+    cmdend = SCpnt->cmnd + SCpnt->cmd_len;
+
+    while (cmdptr < cmdend) {
+	asr = sbic_arm_read (host->scsi.io_port, ASR);
+	if (asr & ASR_DBR)
+	    sbic_arm_write (host->scsi.io_port, DATA, *cmdptr++);
+	else if (asr & ASR_INT)
+	    break;
+    }
+    if (cmdptr >= cmdend)
+	host->scsi.SCp.sent_command = cmdptr - SCpnt->cmnd;
+    host->scsi.phase = PHASE_COMMAND;
+}
+
+static
+void acornscsi_sendmessage (AS_Host *host)
+{
+    unsigned int message_length = msgqueue_msglength (&host->scsi.msgs);
+    int msglen;
+    char *msg;
+
+#if (DEBUG & DEBUG_MESSAGES)
+    printk ("scsi%d.%c: sending message ",
+	    host->host->host_no, acornscsi_target (host));
+#endif
+
+    switch (message_length) {
+    case 0:
+	acornscsi_sbic_issuecmd (host, CMND_XFERINFO | CMND_SBT);
+	while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_DBR) == 0);
+	sbic_arm_write (host->scsi.io_port, DATA, NOP);
+	host->scsi.last_message = NOP;
+#if (DEBUG & DEBUG_MESSAGES)
+	printk ("NOP");
+#endif
+	break;
+
+    case 1:
+	acornscsi_sbic_issuecmd (host, CMND_XFERINFO | CMND_SBT);
+	msg = msgqueue_getnextmsg (&host->scsi.msgs, &msglen);
+	while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_DBR) == 0);
+	sbic_arm_write (host->scsi.io_port, DATA, msg[0]);
+	host->scsi.last_message = msg[0];
+#if (DEBUG & DEBUG_MESSAGES)
+	print_msg (msg);
+#endif
+	break;
+
+    default:
+	/*
+	 * ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.14)
+	 * 'When a target sends this (MESSAGE_REJECT) message, it
+	 *  shall change to MESSAGE IN phase and send this message
+	 *  prior to requesting additional message bytes from the
+	 *  initiator.  This provides an interlock so that the
+	 *  initiator can determine which message byte is rejected.
+	 */
+	sbic_arm_write (host->scsi.io_port, TRANSCNTH, 0);
+	sbic_arm_writenext (host->scsi.io_port, 0);
+	sbic_arm_writenext (host->scsi.io_port, message_length);
+	acornscsi_sbic_issuecmd (host, CMND_XFERINFO);
+
+	while ((msg = msgqueue_getnextmsg (&host->scsi.msgs, &msglen)) != NULL) {
+	    unsigned int asr, i;
+#if (DEBUG & DEBUG_MESSAGES)
+	    print_msg (msg);
+#endif
+	    for (i = 0; i < msglen;) {
+		asr = sbic_arm_read (host->scsi.io_port, ASR);
+		if (asr & ASR_DBR)
+		    sbic_arm_write (host->scsi.io_port, DATA, msg[i++]);
+		if (asr & ASR_INT)
+		    break;
+	    }
+	    host->scsi.last_message = msg[0];
+	    if (msg[0] == EXTENDED_MESSAGE)
+		host->scsi.last_message |= msg[2] << 8;
+	    if (asr & ASR_INT)
+		break;
+	}
+	break;
+    }
+#if (DEBUG & DEBUG_MESSAGES)
+    printk ("\n");
+#endif
+}
+
+/*
+ * Function: void acornscsi_readstatusbyte (AS_Host *host)
+ * Purpose : Read status byte from connected target
+ * Params  : host - host connected to target
+ */
+static
+void acornscsi_readstatusbyte (AS_Host *host)
+{
+    acornscsi_sbic_issuecmd (host, CMND_XFERINFO|CMND_SBT);
+    while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_DBR) == 0);
+
+    host->scsi.SCp.Status = sbic_arm_read (host->scsi.io_port, DATA);
+}
+
+/*
+ * Function: unsigned char acornscsi_readmessagebyte (AS_Host *host)
+ * Purpose : Read one message byte from connected target
+ * Params  : host - host connected to target
+ */
+static
+unsigned char acornscsi_readmessagebyte (AS_Host *host)
+{
+    unsigned char message;
+
+    acornscsi_sbic_issuecmd (host, CMND_XFERINFO | CMND_SBT);
+    while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_DBR) == 0);
+
+    message = sbic_arm_read (host->scsi.io_port, DATA);
+
+    /* wait for MSGIN-XFER-PAUSED */
+    while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_INT) == 0);
+    sbic_arm_read (host->scsi.io_port, SSR);
+
+    return message;
+}
+
+/*
+ * Function: void acornscsi_message (AS_Host *host)
+ * Purpose : Read complete message from connected target & action message
+ * Params  : host - host connected to target
+ */
+static
+void acornscsi_message (AS_Host *host)
+{
+    unsigned char message[16];
+    unsigned int msgidx = 0, msglen = 1;
+
+    do {
+	message[msgidx] = acornscsi_readmessagebyte (host);
+
+	switch (msgidx) {
+	case 0:
+	    if (message[0] == EXTENDED_MESSAGE ||
+		(message[0] >= 0x20 && message[0] <= 0x2f))
+		msglen = 2;
+	    break;
+
+	case 1:
+	    if (message[0] == EXTENDED_MESSAGE)
+		msglen += message[msgidx];
+	    break;
+	}
+	msgidx += 1;
+	if (msgidx < msglen) {
+	    acornscsi_sbic_issuecmd (host, CMND_NEGATEACK);
+
+	    /* wait for next msg-in */
+	    while ((sbic_arm_read (host->scsi.io_port, ASR) & ASR_INT) == 0);
+	    sbic_arm_read (host->scsi.io_port, SSR);
+	}
+    } while (msgidx < msglen);
+
+#if (DEBUG & DEBUG_MESSAGES)
+    printk (KERN_DEBUG "scsi%d.%c: message in: ",
+	    host->host->host_no, acornscsi_target (host));
+    print_msg (message);
+    printk ("\n");
+#endif
+
+    if (host->scsi.phase == PHASE_RECONNECTED) {
+	/*
+	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
+	 * 'Whenever a target reconnects to an initiator to continue
+	 *  a tagged I/O process, the SIMPLE QUEUE TAG message shall
+	 *  be sent immediately following the IDENTIFY message...'
+	 */
+	if (message[0] == SIMPLE_QUEUE_TAG)
+	    host->scsi.reconnected.tag = message[1];
+	if (acornscsi_reconnect_finish (host))
+	    host->scsi.phase = PHASE_MSGIN;
+    }
+
+    switch (message[0]) {
+    case ABORT:
+    case ABORT_TAG:
+    case COMMAND_COMPLETE:
+	if (host->scsi.phase != PHASE_STATUSIN)
+	    printk (KERN_ERR "scsi%d.%c: command complete following non-status in phase?\n",
+		    host->host->host_no, acornscsi_target (host));
+	host->scsi.phase = PHASE_DONE;
+	host->scsi.SCp.Message = message[0];
+	break;
+
+    case SAVE_POINTERS:
+	/*
+	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.20)
+	 * 'The SAVE DATA POINTER message is sent from a target to
+	 *  direct the initiator to copy the active data pointer to
+	 *  the saved data pointer for the current I/O process.
+	 */
+	acornscsi_dma_cleanup (host);
+	host->SCpnt->SCp = host->scsi.SCp;
+	host->SCpnt->SCp.sent_command = 0;
+	host->scsi.phase = PHASE_MSGIN;
+	break;
+
+    case RESTORE_POINTERS:
+	/*
+	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.19)
+	 * 'The RESTORE POINTERS message is sent from a target to
+	 *  direct the initiator to copy the most recently saved
+	 *  command, data, and status pointers for the I/O process
+	 *  to the corresponding active pointers.  The command and
+	 *  status pointers shall be restored to the beginning of
+	 *  the present command and status areas.'
+	 */
+	acornscsi_dma_cleanup (host);
+	host->scsi.SCp = host->SCpnt->SCp;
+	host->scsi.phase = PHASE_MSGIN;
+	break;
+
+    case DISCONNECT:
+	/*
+	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 6.4.2)
+	 * 'On those occasions when an error or exception condition occurs
+	 *  and the target elects to repeat the information transfer, the
+	 *  target may repeat the transfer either issuing a RESTORE POINTERS
+	 *  message or by disconnecting without issuing a SAVE POINTERS
+	 *  message.  When reconnection is completed, the most recent
+	 *  saved pointer values are restored.'
+	 */
+	acornscsi_dma_cleanup (host);
+	host->scsi.phase = PHASE_DISCONNECT;
+	break;
+
+    case MESSAGE_REJECT:
+#if 0 /* this isn't needed any more */
+	/*
+	 * If we were negociating sync transfer, we don't yet know if
+	 * this REJECT is for the sync transfer or for the tagged queue/wide
+	 * transfer.  Re-initiate sync transfer negociation now, and if
+	 * we got a REJECT in response to SDTR, then it'll be set to DONE.
+	 */
+	if (host->device[host->SCpnt->target].sync_state == SYNC_SENT_REQUEST)
+	    host->device[host->SCpnt->target].sync_state = SYNC_NEGOCIATE;
+#endif
+
+	/*
+	 * If we have any messages waiting to go out, then assert ATN now
+	 */
+	if (msgqueue_msglength (&host->scsi.msgs))
+	    acornscsi_sbic_issuecmd (host, CMND_ASSERTATN);
+
+	switch (host->scsi.last_message) {
+#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
+	case HEAD_OF_QUEUE_TAG:
+	case ORDERED_QUEUE_TAG:
+	case SIMPLE_QUEUE_TAG:
+	    /*
+	     * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
+	     *  If a target does not implement tagged queuing and a queue tag
+	     *  message is received, it shall respond with a MESSAGE REJECT
+	     *  message and accept the I/O process as if it were untagged.
+	     */
+	    printk (KERN_NOTICE "scsi%d.%c: disabling tagged queueing\n",
+		    host->host->host_no, acornscsi_target (host));
+	    host->SCpnt->device->tagged_queue = 0;
+	    set_bit (host->SCpnt->target * 8 + host->SCpnt->lun, &host->busyluns);
+	    break;
+#endif
+	case EXTENDED_MESSAGE | (EXTENDED_SDTR << 8):
+	    /*
+	     * Target can't handle synchronous transfers
+	     */
+	    printk (KERN_NOTICE "scsi%d.%c: Using asynchronous transfer\n",
+		    host->host->host_no, acornscsi_target (host));
+	    host->device[host->SCpnt->target].sync_xfer = SYNCHTRANSFER_2DBA;
+	    host->device[host->SCpnt->target].sync_state = SYNC_ASYNCHRONOUS;
+	    sbic_arm_write (host->scsi.io_port, SYNCHTRANSFER, host->device[host->SCpnt->target].sync_xfer);
+	    break;
+
+	default:
+	    break;
+	}
+	break;
+
+    case QUEUE_FULL:
+	/* TODO: target queue is full */
+	break;
+
+    case SIMPLE_QUEUE_TAG:
+	/* tag queue reconnect... message[1] = queue tag.  Print something to indicate something happened! */
+	printk ("scsi%d.%c: reconnect queue tag %02X\n",
+		host->host->host_no, acornscsi_target (host),
+		message[1]);
+	break;
+
+    case EXTENDED_MESSAGE:
+	switch (message[2]) {
+#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
+	case EXTENDED_SDTR:
+	    if (host->device[host->SCpnt->target].sync_state == SYNC_SENT_REQUEST) {
+		/*
+		 * We requested synchronous transfers.  This isn't quite right...
+		 * We can only say if this succeeded if we proceed on to execute the
+		 * command from this message.  If we get a MESSAGE PARITY ERROR,
+		 * and the target retries fail, then we fallback to asynchronous mode
+		 */
+		host->device[host->SCpnt->target].sync_state = SYNC_COMPLETED;
+		printk (KERN_NOTICE "scsi%d.%c: Using synchronous transfer, offset %d, %d ns\n",
+			host->host->host_no, acornscsi_target(host),
+			message[4], message[3] * 4);
+		host->device[host->SCpnt->target].sync_xfer =
+			calc_sync_xfer (message[3] * 4, message[4]);
+	    } else {
+		unsigned char period, length;
+		/*
+		 * Target requested synchronous transfers.  The agreement is only
+		 * to be in operation AFTER the target leaves message out phase.
+		 */
+		acornscsi_sbic_issuecmd (host, CMND_ASSERTATN);
+		period = max (message[3], sdtr_period / 4);
+		length = min (message[4], sdtr_size);
+		msgqueue_addmsg (&host->scsi.msgs, 5, EXTENDED_MESSAGE, 3,
+				 EXTENDED_SDTR, period, length);
+		host->device[host->SCpnt->target].sync_xfer =
+			calc_sync_xfer (period * 4, length);
+	    }
+	    sbic_arm_write (host->scsi.io_port, SYNCHTRANSFER, host->device[host->SCpnt->target].sync_xfer);
+	    break;
+#else
+	    /* We do not accept synchronous transfers.  Respond with a
+	     * MESSAGE_REJECT.
+	     */
+#endif
+
+	case EXTENDED_WDTR:
+	    /* The WD33C93A is only 8-bit.  We respond with a MESSAGE_REJECT
+	     * to a wide data transfer request.
+	     */
+	default:
+	    acornscsi_sbic_issuecmd (host, CMND_ASSERTATN);
+	    msgqueue_flush (&host->scsi.msgs);
+	    msgqueue_addmsg (&host->scsi.msgs, 1, MESSAGE_REJECT);
+	    break;
+	}
+	break;
+
+#ifdef CONFIG_SCSI_ACORNSCSI_LINK
+    case LINKED_CMD_COMPLETE:
+    case LINKED_FLG_CMD_COMPLETE:
+	/*
+	 * We don't support linked commands yet
+	 */
+	if (0) {
+#if (DEBUG & DEBUG_LINK)
+	    printk (KERN_DEBUG "scsi%d.%c: lun %d tag %d linked command complete\n",
+		    host->host->host_no, acornscsi_target(host), host->SCpnt->tag);
+#endif
+	    /*
+	     * A linked command should only terminate with one of these messages
+	     * if there are more linked commands available.
+	     */
+	    if (!host->SCpnt->next_link) {
+		printk (KERN_WARNING "scsi%d.%c: lun %d tag %d linked command complete, but no next_link\n",
+			instance->host_no, acornscsi_target (host), host->SCpnt->tag);
+		acornscsi_sbic_issuecmd (host, CMND_ASSERTATN);
+		msgqueue_addmsg (&host->scsi.msgs, 1, ABORT);
+	    } else {
+		Scsi_Cmnd *SCpnt = host->SCpnt;
+
+		acornscsi_dma_cleanup (host);
+
+		host->SCpnt = host->SCpnt->next_link;
+		host->SCpnt->tag = SCpnt->tag;
+		SCpnt->result = DID_OK | host->scsi.SCp.Message << 8 | host->Scsi.SCp.Status;
+		SCpnt->done (SCpnt);
+
+		/* initialise host->SCpnt->SCp */
+	    }
+	    break;
+	}
+#endif
+
+    default: /* reject message */
+	printk (KERN_ERR "scsi%d.%c: unrecognised message %02X, rejecting\n",
+		host->host->host_no, acornscsi_target (host),
+		message[0]);
+	acornscsi_sbic_issuecmd (host, CMND_ASSERTATN);
+	msgqueue_flush (&host->scsi.msgs);
+	msgqueue_addmsg (&host->scsi.msgs, 1, MESSAGE_REJECT);
+	host->scsi.phase = PHASE_MSGIN;
+	break;
+    }
+    acornscsi_sbic_issuecmd (host, CMND_NEGATEACK);
+}
+
+/*
+ * Function: int acornscsi_buildmessages (AS_Host *host)
+ * Purpose : build the connection messages for a host
+ * Params  : host - host to add messages to
+ */
+static
+void acornscsi_buildmessages (AS_Host *host)
+{
+#if 0
+    /* does the device need resetting? */
+    if (cmd_reset) {
+	msgqueue_addmsg (&host->scsi.msgs, 1, BUS_DEVICE_RESET);
+	return;
+    }
+#endif
+
+    msgqueue_addmsg (&host->scsi.msgs, 1,
+		     IDENTIFY(host->device[host->SCpnt->target].disconnect_ok,
+			     host->SCpnt->lun));
+
+#if 0
+    /* does the device need the current command aborted */
+    if (cmd_aborted) {
+	acornscsi_abortcmd (host->SCpnt->tag);
+	return;
+    }
+#endif
+
+#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
+    if (host->SCpnt->tag) {
+	unsigned int tag_type;
+
+	if (host->SCpnt->cmnd[0] == REQUEST_SENSE ||
+	    host->SCpnt->cmnd[0] == TEST_UNIT_READY ||
+	    host->SCpnt->cmnd[0] == INQUIRY)
+	    tag_type = HEAD_OF_QUEUE_TAG;
+	else
+	    tag_type = SIMPLE_QUEUE_TAG;
+	msgqueue_addmsg (&host->scsi.msgs, 2, tag_type, host->SCpnt->tag);
+    }
+#endif
+
+#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
+    if (host->device[host->SCpnt->target].sync_state == SYNC_NEGOCIATE) {
+	host->device[host->SCpnt->target].sync_state = SYNC_SENT_REQUEST;
+	msgqueue_addmsg (&host->scsi.msgs, 5,
+			 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
+			 sdtr_period / 4, sdtr_size);
+    }
+#endif
+}
+
+/*
+ * Function: int acornscsi_starttransfer (AS_Host *host)
+ * Purpose : transfer data to/from connected target
+ * Params  : host - host to which target is connected
+ * Returns : 0 if failure
+ */
+static
+int acornscsi_starttransfer (AS_Host *host)
+{
+    int residual;
+
+    if (!host->scsi.SCp.ptr /*&& host->scsi.SCp.this_residual*/) {
+	printk (KERN_ERR "scsi%d.%c: null buffer passed to acornscsi_starttransfer\n",
+		host->host->host_no, acornscsi_target (host));
+	return 0;
+    }
+
+    residual = host->SCpnt->request_bufflen - host->scsi.SCp.have_data_in;
+
+    sbic_arm_write (host->scsi.io_port, SYNCHTRANSFER, host->device[host->SCpnt->target].sync_xfer);
+    sbic_arm_writenext (host->scsi.io_port, residual >> 16);
+    sbic_arm_writenext (host->scsi.io_port, residual >> 8);
+    sbic_arm_writenext (host->scsi.io_port, residual);
+    acornscsi_sbic_issuecmd (host, CMND_XFERINFO);
+    return 1;
+}
+
+/* =========================================================================================
+ * Connection & Disconnection
+ */
+/*
+ * Function : acornscsi_reconnect (AS_Host *host)
+ * Purpose  : reconnect a previously disconnected command
+ * Params   : host - host specific data
+ * Remarks  : SCSI spec says:
+ *		'The set of active pointers is restored from the set
+ *		 of saved pointers upon reconnection of the I/O process'
+ */
+static
+int acornscsi_reconnect (AS_Host *host)
+{
+    unsigned int target, lun, ok = 0;
+
+    target = sbic_arm_read (host->scsi.io_port, SOURCEID);
+
+    if (!(target & 8))
+	printk (KERN_ERR "scsi%d: invalid source id after reselection "
+		"- device fault?\n",
+		host->host->host_no);
+
+    target &= 7;
+
+    if (host->SCpnt && !host->scsi.disconnectable) {
+	printk (KERN_ERR "scsi%d.%d: reconnected while command in "
+		"progress to target %d?\n",
+		host->host->host_no, target, host->SCpnt->target);
+	host->SCpnt = NULL;
+    }
+
+    lun = sbic_arm_read (host->scsi.io_port, DATA) & 7;
+
+    host->scsi.reconnected.target = target;
+    host->scsi.reconnected.lun = lun;
+    host->scsi.reconnected.tag = 0;
+
+    if (host->scsi.disconnectable && host->SCpnt &&
+	host->SCpnt->target == target && host->SCpnt->lun == lun)
+	ok = 1;
+
+    if (!ok && queue_probetgtlun (&host->queues.disconnected, target, lun))
+	ok = 1;
+
+    ADD_STATUS(target, 0x81, host->scsi.phase, 0);
+
+    if (ok) {
+	host->scsi.phase = PHASE_RECONNECTED;
+    } else {
+	/* this doesn't seem to work */
+	printk (KERN_ERR "scsi%d.%c: reselected with no command "
+		"to reconnect with\n",
+		host->host->host_no, '0' + target);
+	acornscsi_dumplog (host, target);
+	acornscsi_sbic_issuecmd (host, CMND_ASSERTATN);
+	msgqueue_addmsg (&host->scsi.msgs, 1, ABORT);
+	host->scsi.phase = PHASE_ABORTED;
+    }
+    acornscsi_sbic_issuecmd (host, CMND_NEGATEACK);
+    return !ok;
+}
+
+/*
+ * Function: int acornscsi_reconect_finish (AS_Host *host)
+ * Purpose : finish reconnecting a command
+ * Params  : host - host to complete
+ * Returns : 0 if failed
+ */
+static
+int acornscsi_reconnect_finish (AS_Host *host)
+{
+    if (host->scsi.disconnectable && host->SCpnt) {
+	host->scsi.disconnectable = 0;
+	if (host->SCpnt->target == host->scsi.reconnected.target &&
+	    host->SCpnt->lun	== host->scsi.reconnected.lun &&
+	    host->SCpnt->tag	== host->scsi.reconnected.tag) {
+#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
+	    DBG(host->SCpnt, printk ("scsi%d.%c: reconnected",
+		    host->host->host_no, acornscsi_target (host)));
+#endif
+	} else {
+	    queue_add_cmd_tail (&host->queues.disconnected, host->SCpnt);
+#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
+	    DBG(host->SCpnt, printk ("scsi%d.%c: had to move command "
+		    "to disconnected queue\n",
+		    host->host->host_no, acornscsi_target (host)));
+#endif
+	    host->SCpnt = NULL;
+	}
+    }
+    if (!host->SCpnt) {
+	host->SCpnt = queue_remove_tgtluntag (&host->queues.disconnected,
+				host->scsi.reconnected.target,
+				host->scsi.reconnected.lun,
+				host->scsi.reconnected.tag);
+#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
+	DBG(host->SCpnt, printk ("scsi%d.%c: had to get command",
+		host->host->host_no, acornscsi_target (host)));
+#endif
+    }
+
+    if (!host->SCpnt) {
+	acornscsi_abortcmd (host, host->scsi.reconnected.tag);
+	host->scsi.phase = PHASE_ABORTED;
+    } else {
+	/*
+	 * Restore data pointer from SAVED pointers.
+	 */
+	host->scsi.SCp = host->SCpnt->SCp;
+#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
+	printk (", data pointers: [%p, %X]",
+		host->scsi.SCp.ptr, host->scsi.SCp.this_residual);
+#endif
+    }
+#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
+    printk ("\n");
+#endif
+
+    host->dma.transferred = host->scsi.SCp.have_data_in;
+
+    return host->SCpnt != NULL;
+}
+
+/*
+ * Function: void acornscsi_disconnect_unexpected (AS_Host *host)
+ * Purpose : handle an unexpected disconnect
+ * Params  : host - host on which disconnect occurred
+ */
+static
+void acornscsi_disconnect_unexpected (AS_Host *host)
+{
+    printk (KERN_ERR "scsi%d.%c: unexpected disconnect\n",
+	    host->host->host_no, acornscsi_target (host));
+#if (DEBUG & DEBUG_ABORT)
+    acornscsi_dumplog (host, 8);
+#endif
+
+    acornscsi_done (host, &host->SCpnt, DID_ABORT);
+}
+
+/*
+ * Function: void acornscsi_abortcmd (AS_host *host, unsigned char tag)
+ * Purpose : abort a currently executing command
+ * Params  : host - host with connected command to abort
+ *	     tag  - tag to abort
+ */
+static
+void acornscsi_abortcmd (AS_Host *host, unsigned char tag)
+{
+    sbic_arm_write (host->scsi.io_port, CMND, CMND_ASSERTATN);
+
+    msgqueue_flush (&host->scsi.msgs);
+#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
+    if (tag)
+	msgqueue_addmsg (&host->scsi.msgs, 2, ABORT_TAG, tag);
+    else
+#endif
+	msgqueue_addmsg (&host->scsi.msgs, 1, ABORT);
+}
+
+/* ==========================================================================================
+ * Interrupt routines.
+ */
+/*
+ * Function: int acornscsi_sbicintr (AS_Host *host)
+ * Purpose : handle interrupts from SCSI device
+ * Params  : host - host to process
+ * Returns : INTR_PROCESS if expecting another SBIC interrupt
+ *	     INTR_IDLE if no interrupt
+ *	     INTR_NEXT_COMMAND if we have finished processing the command
+ */
+static
+intr_ret_t acornscsi_sbicintr (AS_Host *host, int in_irq)
+{
+    unsigned int asr, ssr;
+
+    asr = sbic_arm_read (host->scsi.io_port, ASR);
+    if (!(asr & ASR_INT))
+	return INTR_IDLE;
+
+    ssr = sbic_arm_read (host->scsi.io_port, SSR);
+
+#if (DEBUG & DEBUG_PHASES)
+    print_sbic_status(asr, ssr, host->scsi.phase);
+#endif
+
+    ADD_STATUS(8, ssr, host->scsi.phase, in_irq);
+
+    if (host->SCpnt && !host->scsi.disconnectable)
+	ADD_STATUS(host->SCpnt->target, ssr, host->scsi.phase, in_irq);
+
+    switch (ssr) {
+    case 0x00:				/* reset state - not advanced			*/
+	printk (KERN_ERR "scsi%d: reset in standard mode but wanted advanced mode.\n",
+		host->host->host_no);
+	/* setup sbic - WD33C93A */
+	sbic_arm_write (host->scsi.io_port, OWNID, OWNID_EAF | host->host->this_id);
+	sbic_arm_write (host->scsi.io_port, CMND, CMND_RESET);
+	return INTR_IDLE;
+
+    case 0x01:				/* reset state - advanced			*/
+	sbic_arm_write (host->scsi.io_port, CTRL, INIT_SBICDMA | CTRL_IDI);
+	sbic_arm_write (host->scsi.io_port, TIMEOUT, TIMEOUT_TIME);
+	sbic_arm_write (host->scsi.io_port, SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
+	sbic_arm_write (host->scsi.io_port, SOURCEID, SOURCEID_ER | SOURCEID_DSP);
+	msgqueue_flush (&host->scsi.msgs);
+	return INTR_IDLE;
+
+    case 0x41:				/* unexpected disconnect aborted command	*/
+	acornscsi_disconnect_unexpected (host);
+	return INTR_NEXT_COMMAND;
+    }
+
+    switch (host->scsi.phase) {
+    case PHASE_CONNECTING:		/* STATE: command removed from issue queue	*/
+	switch (ssr) {
+	case 0x11:			/* -> PHASE_CONNECTED				*/
+	    /* BUS FREE -> SELECTION */
+	    host->scsi.phase = PHASE_CONNECTED;
+	    msgqueue_flush (&host->scsi.msgs);
+	    host->dma.transferred = host->scsi.SCp.have_data_in;
+	    /* 33C93 gives next interrupt indicating bus phase */
+	    asr = sbic_arm_read (host->scsi.io_port, ASR);
+	    if (!(asr & ASR_INT))
+		break;
+	    ssr = sbic_arm_read (host->scsi.io_port, SSR);
+	    ADD_STATUS(8, ssr, host->scsi.phase, 1);
+	    ADD_STATUS(host->SCpnt->target, ssr, host->scsi.phase, 1);
+	    goto connected;
+	    
+	case 0x42:			/* select timed out				*/
+					/* -> PHASE_IDLE				*/
+	    acornscsi_done (host, &host->SCpnt, DID_NO_CONNECT);
+	    return INTR_NEXT_COMMAND;
+
+	case 0x81:			/* -> PHASE_RECONNECTED or PHASE_ABORTED	*/
+	    /* BUS FREE -> RESELECTION */
+	    host->origSCpnt = host->SCpnt;
+	    host->SCpnt = NULL;
+	    msgqueue_flush (&host->scsi.msgs);
+	    acornscsi_reconnect (host);
+	    break;
+
+	default:
+	    printk (KERN_ERR "scsi%d.%c: PHASE_CONNECTING, SSR %02X?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	    acornscsi_abortcmd (host, host->SCpnt->tag);
+	}
+	return INTR_PROCESSING;
+
+    connected:
+    case PHASE_CONNECTED:		/* STATE: device selected ok			*/
+	switch (ssr) {
+#ifdef NONSTANDARD
+	case 0x8a:			/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED	*/
+	    /* SELECTION -> COMMAND */
+	    acornscsi_sendcommand (host);
+	    break;
+
+	case 0x8b:			/* -> PHASE_STATUS				*/
+	    /* SELECTION -> STATUS */
+	    acornscsi_readstatusbyte (host);
+	    host->scsi.phase = PHASE_STATUSIN;
+	    break;
+#endif
+
+	case 0x8e:			/* -> PHASE_MSGOUT				*/
+	    /* SELECTION ->MESSAGE OUT */
+	    host->scsi.phase = PHASE_MSGOUT;
+	    acornscsi_buildmessages (host);
+	    acornscsi_sendmessage (host);
+	    break;
+
+	/* these should not happen */
+	case 0x85:			/* target disconnected				*/
+	    acornscsi_done (host, &host->SCpnt, DID_ERROR);
+	    break;
+
+	default:
+	    printk (KERN_ERR "scsi%d.%c: PHASE_CONNECTED, SSR %02X?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	    acornscsi_abortcmd (host, host->SCpnt->tag);
+	}
+	return INTR_PROCESSING;
+
+    case PHASE_MSGOUT:			/* STATE: connected & sent IDENTIFY message	*/
+	/*
+	 * SCSI standard says th at a MESSAGE OUT phases can be followed by a DATA phase
+	 */
+	switch (ssr) {
+	case 0x8a:
+	case 0x1a:			/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED	*/
+	    /* MESSAGE OUT -> COMMAND */
+	    acornscsi_sendcommand (host);
+	    break;
+
+	case 0x1b:			/* -> PHASE_STATUS				*/
+	    /* MESSAGE OUT -> STATUS */
+	    acornscsi_readstatusbyte (host);
+	    host->scsi.phase = PHASE_STATUSIN;
+	    break;
+
+	case 0x8e:			/* -> PHASE_MSGOUT				*/
+	    /* MESSAGE_OUT(MESSAGE_IN) ->MESSAGE OUT */
+	    acornscsi_sendmessage (host);
+	    break;
+
+        case 0x4f:
+	case 0x1f:			/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
+	    /* MESSAGE OUT -> MESSAGE IN */
+	    acornscsi_message (host);
+	    break;
+
+	default:
+	    printk (KERN_ERR "scsi%d.%c: PHASE_MSGOUT, SSR %02X?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	}
+	return INTR_PROCESSING;
+
+    case PHASE_COMMAND: 		/* STATE: connected & command sent		*/
+	switch (ssr) {
+	case 0x18:			/* -> PHASE_DATAOUT				*/
+	    /* COMMAND -> DATA OUT */
+	    if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
+		acornscsi_abortcmd (host, host->SCpnt->tag);
+	    acornscsi_dma_setup (host, DMA_OUT);
+	    if (!acornscsi_starttransfer (host))
+		acornscsi_abortcmd (host, host->SCpnt->tag);
+	    host->scsi.phase = PHASE_DATAOUT;
+	    return INTR_IDLE;
+
+	case 0x19:			/* -> PHASE_DATAIN				*/
+	    /* COMMAND -> DATA IN */
+	    if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
+		acornscsi_abortcmd (host, host->SCpnt->tag);
+	    acornscsi_dma_setup (host, DMA_IN);
+	    if (!acornscsi_starttransfer (host))
+		acornscsi_abortcmd (host, host->SCpnt->tag);
+	    host->scsi.phase = PHASE_DATAIN;
+	    return INTR_IDLE;
+
+	case 0x1b:			/* -> PHASE_STATUS				*/
+	    /* COMMAND -> STATUS */
+	    acornscsi_readstatusbyte (host);
+	    host->scsi.phase = PHASE_STATUSIN;
+	    break;
+
+	case 0x1e:			/* -> PHASE_MSGOUT				*/
+	    /* COMMAND -> MESSAGE OUT */
+	    acornscsi_sendmessage (host);
+	    break;
+
+	case 0x1f:			/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
+	    /* COMMAND -> MESSAGE IN */
+	    acornscsi_message (host);
+	    break;
+
+	default:
+	    printk (KERN_ERR "scsi%d.%c: PHASE_COMMAND, SSR %02X?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	}
+	return INTR_PROCESSING;
+
+    case PHASE_DISCONNECT:		/* STATE: connected, received DISCONNECT msg	*/
+	if (ssr == 0x85) {		/* -> PHASE_IDLE				*/
+	    host->scsi.disconnectable = 1;
+	    host->scsi.reconnected.tag = 0;
+	    host->scsi.phase = PHASE_IDLE;
+	    host->stats.disconnects += 1;
+	} else {
+	    printk (KERN_ERR "scsi%d.%c: PHASE_DISCONNECT, SSR %02X instead of disconnect?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	}
+	return INTR_NEXT_COMMAND;
+
+    case PHASE_IDLE:			/* STATE: disconnected				*/
+	if (ssr == 0x81)		/* -> PHASE_RECONNECTED or PHASE_ABORTED	*/
+	    acornscsi_reconnect (host);
+	else {
+	    printk (KERN_ERR "scsi%d.%c: PHASE_IDLE, SSR %02X while idle?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	}
+	return INTR_PROCESSING;
+
+    case PHASE_RECONNECTED:		/* STATE: device reconnected to initiator	*/
+	/*
+	 * Command reconnected - if MESGIN, get message - it may be
+	 * the tag.  If not, get command out of disconnected queue
+	 */
+	/*
+	 * If we reconnected and we're not in MESSAGE IN phase after IDENTIFY,
+	 * reconnect I_T_L command
+	 */
+	if (ssr != 0x8f && !acornscsi_reconnect_finish (host))
+	    return INTR_IDLE;
+	ADD_STATUS(host->SCpnt->target, ssr, host->scsi.phase, in_irq);
+	switch (ssr) {
+	case 0x88:			/* data out phase				*/
+					/* -> PHASE_DATAOUT				*/
+	    /* MESSAGE IN -> DATA OUT */
+	    acornscsi_dma_setup (host, DMA_OUT);
+	    if (!acornscsi_starttransfer (host))
+		acornscsi_abortcmd (host, host->SCpnt->tag);
+	    host->scsi.phase = PHASE_DATAOUT;
+	    return INTR_IDLE;
+
+	case 0x89:			/* data in phase				*/
+					/* -> PHASE_DATAIN				*/
+	    /* MESSAGE IN -> DATA IN */
+	    acornscsi_dma_setup (host, DMA_IN);
+	    if (!acornscsi_starttransfer (host))
+		acornscsi_abortcmd (host, host->SCpnt->tag);
+	    host->scsi.phase = PHASE_DATAIN;
+	    return INTR_IDLE;
+
+	case 0x8a:			/* command out					*/
+	    /* MESSAGE IN -> COMMAND */
+	    acornscsi_sendcommand (host);/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED	*/
+	    break;
+
+	case 0x8b:			/* status in					*/
+					/* -> PHASE_STATUSIN				*/
+	    /* MESSAGE IN -> STATUS */
+	    acornscsi_readstatusbyte (host);
+	    host->scsi.phase = PHASE_STATUSIN;
+	    break;
+
+	case 0x8e:			/* message out					*/
+					/* -> PHASE_MSGOUT				*/
+	    /* MESSAGE IN -> MESSAGE OUT */
+	    acornscsi_sendmessage (host);
+	    break;
+
+	case 0x8f:			/* message in					*/
+	    acornscsi_message (host);	/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
+	    break;
+
+	default:
+	    printk (KERN_ERR "scsi%d.%c: PHASE_RECONNECTED, SSR %02X after reconnect?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	}
+	return INTR_PROCESSING;
+
+    case PHASE_DATAIN:			/* STATE: transferred data in			*/
+	/*
+	 * This is simple - if we disconnect then the DMA address & count is
+	 * correct.
+	 */
+	switch (ssr) {
+	case 0x19:			/* -> PHASE_DATAIN				*/
+	    acornscsi_abortcmd (host, host->SCpnt->tag);
+	    return INTR_IDLE;
+
+	case 0x4b:			/* -> PHASE_STATUSIN				*/
+	case 0x1b:			/* -> PHASE_STATUSIN				*/
+	    /* DATA IN -> STATUS */
+	    host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
+					  acornscsi_sbic_xfcount (host);
+	    acornscsi_dma_stop (host);
+	    acornscsi_readstatusbyte (host);
+	    host->scsi.phase = PHASE_STATUSIN;
+	    break;
+
+	case 0x1e:			/* -> PHASE_MSGOUT				*/
+	case 0x4e:			/* -> PHASE_MSGOUT				*/
+	    /* DATA IN -> MESSAGE OUT */
+	    host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
+					  acornscsi_sbic_xfcount (host);
+	    acornscsi_dma_stop (host);
+	    acornscsi_sendmessage (host);
+	    break;
+
+	case 0x1f:			/* message in					*/
+	case 0x4f:			/* message in					*/
+	    /* DATA IN -> MESSAGE IN */
+	    host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
+					  acornscsi_sbic_xfcount (host);
+	    acornscsi_dma_stop (host);
+	    acornscsi_message (host);	/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
+	    break;
+
+	default:
+	    printk (KERN_ERR "scsi%d.%c: PHASE_DATAIN, SSR %02X?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	}
+	return INTR_PROCESSING;
+
+    case PHASE_DATAOUT: 		/* STATE: transferred data out			*/
+	/*
+	 * This is more complicated - if we disconnect, the DMA could be 12
+	 * bytes ahead of us.  We need to correct this.
+	 */
+	switch (ssr) {
+	case 0x18:			/* -> PHASE_DATAOUT				*/
+	    acornscsi_abortcmd (host, host->SCpnt->tag);
+	    return INTR_IDLE;
+
+	case 0x4b:			/* -> PHASE_STATUSIN				*/
+	case 0x1b:			/* -> PHASE_STATUSIN				*/
+	    /* DATA OUT -> STATUS */
+	    host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
+					  acornscsi_sbic_xfcount (host);
+	    acornscsi_dma_stop (host);
+	    acornscsi_dma_adjust (host);
+	    acornscsi_readstatusbyte (host);
+	    host->scsi.phase = PHASE_STATUSIN;
+	    break;
+
+	case 0x1e:			/* -> PHASE_MSGOUT				*/
+	case 0x4e:			/* -> PHASE_MSGOUT				*/
+	    /* DATA OUT -> MESSAGE OUT */
+	    host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
+					  acornscsi_sbic_xfcount (host);
+	    acornscsi_dma_stop (host);
+	    acornscsi_dma_adjust (host);
+	    acornscsi_sendmessage (host);
+	    break;
+
+	case 0x1f:			/* message in					*/
+	case 0x4f:			/* message in					*/
+	    /* DATA OUT -> MESSAGE IN */
+	    host->scsi.SCp.have_data_in = host->SCpnt->request_bufflen -
+					  acornscsi_sbic_xfcount (host);
+	    acornscsi_dma_stop (host);
+	    acornscsi_dma_adjust (host);
+	    acornscsi_message (host);	/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
+	    break;
+
+	default:
+	    printk (KERN_ERR "scsi%d.%c: PHASE_DATAOUT, SSR %02X?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	}
+	return INTR_PROCESSING;
+
+    case PHASE_STATUSIN:		/* STATE: status in complete			*/
+	if (ssr == 0x1f)		/* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
+	    /* STATUS -> MESSAGE IN */
+	    acornscsi_message (host);
+	else if (ssr == 0x1e)		/* -> PHASE_MSGOUT				*/
+	    /* STATUS -> MESSAGE OUT */
+	    acornscsi_sendmessage (host);
+	else {
+	    printk (KERN_ERR "scsi%d.%c: PHASE_STATUSIN, SSR %02X instead of MESSAGE_IN?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	}
+	return INTR_PROCESSING;
+
+    case PHASE_MSGIN:			/* STATE: message in				*/
+	switch (ssr) {
+	case 0x1e:			/* -> PHASE_MSGOUT				*/
+	case 0x4e:			/* -> PHASE_MSGOUT				*/
+	    /* MESSAGE IN -> MESSAGE OUT */
+	    acornscsi_sendmessage (host);
+	    break;
+
+	case 0x1f:			/* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
+	case 0x2f:
+	case 0x4f:
+	case 0x8f:
+	    acornscsi_message (host);
+	    break;
+
+	default:
+	    printk (KERN_ERR "scsi%d.%c: PHASE_MSGIN, SSR %02X after message in?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	}
+	return INTR_PROCESSING;
+
+    case PHASE_DONE:			/* STATE: received status & message		*/
+	switch (ssr) {
+	case 0x85:			/* -> PHASE_IDLE				*/
+	    acornscsi_done (host, &host->SCpnt, DID_OK);
+	    return INTR_NEXT_COMMAND;
+
+	case 0x8e:
+	    acornscsi_sendmessage (host);
+	    break;
+
+	default:
+	    printk (KERN_ERR "scsi%d.%c: PHASE_DONE, SSR %02X instead of disconnect?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	}
+	return INTR_PROCESSING;
+
+    case PHASE_ABORTED:
+	switch (ssr) {
+	case 0x85:
+	    acornscsi_done (host, &host->SCpnt, DID_ABORT);
+	    return INTR_NEXT_COMMAND;
+
+	case 0x1e:
+	case 0x2e:
+	case 0x4e:
+	case 0x8e:
+	    acornscsi_sendmessage (host);
+	    break;
+
+	default:
+	    printk (KERN_ERR "scsi%d.%c: PHASE_ABORTED, SSR %02X?\n",
+		    host->host->host_no, acornscsi_target (host), ssr);
+	    acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+	}
+	return INTR_PROCESSING;
+
+    default:
+	printk (KERN_ERR "scsi%d.%c: unknown driver phase %d\n",
+		host->host->host_no, acornscsi_target (host), ssr);
+	acornscsi_dumplog (host, host->SCpnt ? host->SCpnt->target : 8);
+    }
+    return INTR_PROCESSING;
+}
+
+/*
+ * Prototype: void acornscsi_intr (int irq, void *dev_id, struct pt_regs *regs)
+ * Purpose  : handle interrupts from Acorn SCSI card
+ * Params   : irq    - interrupt number
+ *	      dev_id - device specific data (AS_Host structure)
+ *	      regs   - processor registers when interrupt occurred
+ */
+static
+void acornscsi_intr (int irq, void *dev_id, struct pt_regs *regs)
+{
+    AS_Host *host = (AS_Host *)dev_id;
+    intr_ret_t ret;
+    int iostatus;
+    int in_irq = 0;
+
+    if (host->scsi.interrupt)
+	printk ("scsi%d: interrupt re-entered\n", host->host->host_no);
+    host->scsi.interrupt = 1;
+
+    do {
+	ret = INTR_IDLE;
+
+	iostatus = inb (host->card.io_intr);
+
+	if (iostatus & 2) {
+	    acornscsi_dma_intr (host);
+	    iostatus = inb (host->card.io_intr);
+	}
+
+	if (iostatus & 8)
+	    ret = acornscsi_sbicintr (host, in_irq);
+
+	/*
+	 * If we have a transfer pending, start it.
+	 * Only start it if the interface has already started transferring
+	 * it's data
+	 */
+	if (host->dma.xfer_required)
+	    acornscsi_dma_xfer (host);
+
+	if (ret == INTR_NEXT_COMMAND)
+	    ret = acornscsi_kick (host);
+
+	in_irq = 1;
+    } while (ret != INTR_IDLE);
+
+    host->scsi.interrupt = 0;
+}
+
+/*=============================================================================================
+ * Interfaces between interrupt handler and rest of scsi code
+ */
+
+/*
+ * Function : acornscsi_queuecmd (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
+ * Purpose  : queues a SCSI command
+ * Params   : cmd  - SCSI command
+ *	      done - function called on completion, with pointer to command descriptor
+ * Returns  : 0, or < 0 on error.
+ */
+int acornscsi_queuecmd (Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
+{
+    AS_Host *host = (AS_Host *)SCpnt->host->hostdata;
+
+    if (!done) {
+	/* there should be some way of rejecting errors like this without panicing... */
+	panic ("scsi%d: queuecommand called with NULL done function [cmd=%p]",
+		SCpnt->host->host_no, SCpnt);
+	return -EINVAL;
+    }
+
+#if (DEBUG & DEBUG_NO_WRITE)
+    if (acornscsi_cmdtype (SCpnt->cmnd[0]) == CMD_WRITE && (NO_WRITE & (1 << SCpnt->target))) {
+	printk (KERN_CRIT "scsi%d.%c: WRITE attempted with NO_WRITE flag set\n",
+	    SCpnt->host->host_no, '0' + SCpnt->target);
+	SCpnt->result = DID_NO_CONNECT << 16;
+	done (SCpnt);
+	return 0;
+    }
+#endif
+
+    SCpnt->scsi_done = done;
+    SCpnt->host_scribble = NULL;
+    SCpnt->result = 0;
+    SCpnt->tag = 0;
+    SCpnt->SCp.phase = (int)acornscsi_datadirection (SCpnt->cmnd[0]);
+    SCpnt->SCp.sent_command = 0;
+    SCpnt->SCp.have_data_in = 0;
+    SCpnt->SCp.Status = 0;
+    SCpnt->SCp.Message = 0;
+
+    if (SCpnt->use_sg) {
+	SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->buffer;
+	SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
+	SCpnt->SCp.ptr = (char *) SCpnt->SCp.buffer->address;
+	SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
+    } else {
+	SCpnt->SCp.buffer = NULL;
+	SCpnt->SCp.buffers_residual = 0;
+	SCpnt->SCp.ptr = (char *) SCpnt->request_buffer;
+	SCpnt->SCp.this_residual = SCpnt->request_bufflen;
+    }
+
+    host->stats.queues += 1;
+
+    {
+	unsigned long flags;
+
+	if (!queue_add_cmd_ordered (&host->queues.issue, SCpnt)) {
+	    SCpnt->result = DID_ERROR << 16;
+	    done (SCpnt);
+	    return 0;
+	}
+	save_flags_cli (flags);
+	if (host->scsi.phase == PHASE_IDLE)
+	    acornscsi_kick (host);
+	restore_flags (flags);
+    }
+    return 0;
+}
+
+/*
+ * Prototype: void acornscsi_reportstatus (Scsi_Cmnd **SCpntp1, Scsi_Cmnd **SCpntp2, int result)
+ * Purpose  : pass a result to *SCpntp1, and check if *SCpntp1 = *SCpntp2
+ * Params   : SCpntp1 - pointer to command to return
+ *	      SCpntp2 - pointer to command to check
+ *	      result  - result to pass back to mid-level done function
+ * Returns  : *SCpntp2 = NULL if *SCpntp1 is the same command structure as *SCpntp2.
+ */
+static inline
+void acornscsi_reportstatus (Scsi_Cmnd **SCpntp1, Scsi_Cmnd **SCpntp2, int result)
+{
+    Scsi_Cmnd *SCpnt = *SCpntp1;
+
+    if (SCpnt) {
+	*SCpntp1 = NULL;
+
+	SCpnt->result = result;
+	SCpnt->scsi_done (SCpnt);
+    }
+
+    if (SCpnt == *SCpntp2)
+	*SCpntp2 = NULL;
+}
+
+/*
+ * Prototype: int acornscsi_abort (Scsi_Cmnd *SCpnt)
+ * Purpose  : abort a command on this host
+ * Params   : SCpnt - command to abort
+ * Returns  : one of SCSI_ABORT_ macros
+ */
+int acornscsi_abort (Scsi_Cmnd *SCpnt)
+{
+    AS_Host *host = (AS_Host *) SCpnt->host->hostdata;
+    int result = SCSI_ABORT_NOT_RUNNING;
+
+    host->stats.aborts += 1;
+
+#if (DEBUG & DEBUG_ABORT)
+    {
+	int asr, ssr;
+	asr = sbic_arm_read (host->scsi.io_port, ASR);
+	ssr = sbic_arm_read (host->scsi.io_port, SSR);
+
+	printk (KERN_WARNING "acornscsi_abort: ");
+	print_sbic_status(asr, ssr, host->scsi.phase);
+	acornscsi_dumplog (host, SCpnt->target);
+    }
+#endif
+
+    if (queue_removecmd (&host->queues.issue, SCpnt)) {
+	SCpnt->result = DID_ABORT << 16;
+	SCpnt->scsi_done (SCpnt);
+#if (DEBUG & DEBUG_ABORT)
+	printk ("scsi%d: command on issue queue\n", host->host->host_no);
+#endif
+	result = SCSI_ABORT_SUCCESS;
+    } else if (queue_cmdonqueue (&host->queues.disconnected, SCpnt)) {
+	printk ("scsi%d: command on disconnected queue\n", host->host->host_no);
+	result = SCSI_ABORT_SNOOZE;
+    } else if (host->SCpnt == SCpnt) {
+	acornscsi_abortcmd (host, host->SCpnt->tag);
+	printk ("scsi%d: command executing\n", host->host->host_no);
+	result = SCSI_ABORT_SNOOZE;
+    } else if (host->origSCpnt == SCpnt) {
+	host->origSCpnt = NULL;
+	SCpnt->result = DID_ABORT << 16;
+	SCpnt->scsi_done (SCpnt);
+#if (DEBUG & DEBUG_ABORT)
+	printk ("scsi%d: command waiting for execution\n", host->host->host_no);
+#endif
+	result = SCSI_ABORT_SUCCESS;
+    }
+
+    if (result == SCSI_ABORT_NOT_RUNNING) {
+	printk ("scsi%d: abort(): command not running\n", host->host->host_no);
+	acornscsi_dumplog (host, SCpnt->target);
+#if (DEBUG & DEBUG_ABORT)
+	result = SCSI_ABORT_SNOOZE;
+#endif
+    }
+    return result;
+}
+
+/*
+ * Prototype: int acornscsi_reset (Scsi_Cmnd *SCpnt, unsigned int reset_flags)
+ * Purpose  : reset a command on this host/reset this host
+ * Params   : SCpnt  - command causing reset
+ *	      result - what type of reset to perform
+ * Returns  : one of SCSI_RESET_ macros
+ */
+int acornscsi_reset (Scsi_Cmnd *SCpnt, unsigned int reset_flags)
+{
+    AS_Host *host = (AS_Host *)SCpnt->host->hostdata;
+    Scsi_Cmnd *SCptr;
+    
+    host->stats.resets += 1;
+
+#if (DEBUG & DEBUG_RESET)
+    {
+	int asr, ssr;
+
+	asr = sbic_arm_read (host->scsi.io_port, ASR);
+	ssr = sbic_arm_read (host->scsi.io_port, SSR);
+
+	printk (KERN_WARNING "acornscsi_reset: ");
+	print_sbic_status(asr, ssr, host->scsi.phase);
+	acornscsi_dumplog (host, SCpnt->target);
+    }
+#endif
+
+    acornscsi_dma_stop (host);
+
+    SCptr = host->SCpnt;
+
+    /*
+     * do hard reset.  This resets all devices on this host, and so we
+     * must set the reset status on all commands.
+     */
+    acornscsi_resetcard (host);
+
+    /*
+     * report reset on commands current connected/disconnected
+     */
+    acornscsi_reportstatus (&host->SCpnt, &SCptr, DID_RESET);
+
+    while ((SCptr = queue_remove (&host->queues.disconnected)) != NULL)
+	acornscsi_reportstatus (&SCptr, &SCpnt, DID_RESET);
+
+    if (SCpnt) {
+	SCpnt->result = DID_RESET << 16;
+	SCpnt->scsi_done (SCpnt);
+    }
+while (1);
+    return SCSI_RESET_BUS_RESET | SCSI_RESET_HOST_RESET | SCSI_RESET_SUCCESS;
+}
+
+/*==============================================================================================
+ * initialisation & miscellaneous support
+ */
+static struct expansion_card *ecs[MAX_ECARDS];
+
+/*
+ * Prototype: void acornscsi_init (AS_Host *host)
+ * Purpose  : initialise the AS_Host structure for one interface & setup hardware
+ * Params   : host - host to setup
+ */
+static
+void acornscsi_init (AS_Host *host)
+{
+    memset (&host->stats, 0, sizeof (host->stats));
+    queue_initialise (&host->queues.issue);
+    queue_initialise (&host->queues.disconnected);
+    msgqueue_initialise (&host->scsi.msgs);
+
+    acornscsi_resetcard (host);
+}
+
+int acornscsi_detect(Scsi_Host_Template * tpnt)
+{
+    static const card_ids acornscsi_cids[] = { ACORNSCSI_LIST, { 0xffff, 0xffff } };
+    int i, count = 0;
+    struct Scsi_Host *instance;
+    AS_Host *host;
+
+    tpnt->proc_dir = &proc_scsi_acornscsi;
+
+    for (i = 0; i < MAX_ECARDS; i++)
+	ecs[i] = NULL;
+
+    ecard_startfind ();
+
+    while(1) {
+	ecs[count] = ecard_find(0, acornscsi_cids);
+	if (!ecs[count])
+	    break;
+
+	if (ecs[count]->irq == 0xff) {
+	    printk ("scsi: WD33C93 does not have IRQ enabled - ignoring\n");
+	    continue;
+	}
+
+	ecard_claim(ecs[count]); /* Must claim here - card produces irq on reset */
+
+	instance = scsi_register (tpnt, sizeof(AS_Host));
+	host = (AS_Host *)instance->hostdata;
+
+	instance->io_port = ecard_address (ecs[count], ECARD_MEMC, 0);
+	instance->irq = ecs[count]->irq;
+
+	host->host		= instance;
+	host->scsi.io_port	= ioaddr (instance->io_port + 0x800);
+	host->scsi.irq		= instance->irq;
+	host->card.io_intr	= POD_SPACE(instance->io_port) + 0x800;
+	host->card.io_page	= POD_SPACE(instance->io_port) + 0xc00;
+	host->card.io_ram	= ioaddr (instance->io_port);
+	host->dma.io_port	= instance->io_port + 0xc00;
+	host->dma.io_intr_clear = POD_SPACE(instance->io_port) + 0x800;
+
+	request_region (instance->io_port + 0x800,  2, "acornscsi(sbic)");
+	request_region (host->card.io_intr,  1, "acornscsi(intr)");
+	request_region (host->card.io_page,  1, "acornscsi(page)");
+#ifdef USE_DMAC
+	request_region (host->dma.io_port, 256, "acornscsi(dmac)");
+#endif
+	request_region (instance->io_port, 2048, "acornscsi(ram)");
+
+	if (request_irq(host->scsi.irq, acornscsi_intr, SA_INTERRUPT, "acornscsi", host)) {
+	    printk(KERN_CRIT "scsi%d: IRQ%d not free, interrupts disabled\n",
+		instance->host_no, host->scsi.irq);
+	    host->scsi.irq = NO_IRQ;
+	}
+
+	acornscsi_init (host);
+
+	++count;
+    }
+    return count;
+}
+
+/*
+ * Function: int acornscsi_release (struct Scsi_Host *host)
+ * Purpose : release all resources used by this adapter
+ * Params  : host - driver structure to release
+ * Returns : nothing of any consequence
+ */
+int acornscsi_release (struct Scsi_Host *instance)
+{
+    AS_Host *host = (AS_Host *)instance->hostdata;
+    int i;
+
+    /*
+     * Put card into RESET state
+     */
+    outb (0x80, host->card.io_page);
+
+    if (host->scsi.irq != NO_IRQ)
+	free_irq (host->scsi.irq, host);
+
+    release_region (instance->io_port + 0x800, 2);
+    release_region (host->card.io_intr, 1);
+    release_region (host->card.io_page, 1);
+    release_region (host->dma.io_port, 256);
+    release_region (instance->io_port, 2048);
+
+    for (i = 0; i < MAX_ECARDS; i++)
+	if (ecs[i] && instance->io_port == ecard_address (ecs[i], ECARD_MEMC, 0))
+	    ecard_release (ecs[i]);
+
+    msgqueue_free (&host->scsi.msgs);
+    queue_free (&host->queues.disconnected);
+    queue_free (&host->queues.issue);
+
+    return 0;
+}
+
+/*
+ * Function: char *acornscsi_info (struct Scsi_Host *host)
+ * Purpose : return a string describing this interface
+ * Params  : host - host to give information on
+ * Returns : a constant string
+ */
+const
+char *acornscsi_info(struct Scsi_Host *host)
+{
+    static char string[100], *p;
+
+    p = string;
+    
+    p += sprintf (string, "%s at port %X irq %d v%d.%d.%d"
+#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
+    " SYNC"
+#endif
+#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
+    " TAG"
+#endif
+#ifdef CONFIG_SCSI_ACORNSCSI_LINK
+    " LINK"
+#endif
+#if (DEBUG & DEBUG_NO_WRITE)
+    " NOWRITE ("NO_WRITE_STR")"
+#endif
+		, host->hostt->name, host->io_port, host->irq,
+		VER_MAJOR, VER_MINOR, VER_PATCH);
+    return string;
+}
+
+int acornscsi_proc_info(char *buffer, char **start, off_t offset,
+			int length, int host_no, int inout)
+{
+    int pos, begin = 0, devidx;
+    struct Scsi_Host *instance = scsi_hostlist;
+    Scsi_Device *scd;
+    AS_Host *host;
+    char *p = buffer;
+
+    for (instance = scsi_hostlist;
+	    instance && instance->host_no != host_no;
+		instance = instance->next);
+
+    if (inout == 1 || !instance)
+	return -EINVAL;
+
+    host  = (AS_Host *)instance->hostdata;
+    
+    p += sprintf (p, "AcornSCSI driver v%d.%d.%d"
+#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
+    " SYNC"
+#endif
+#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
+    " TAG"
+#endif
+#ifdef CONFIG_SCSI_ACORNSCSI_LINK
+    " LINK"
+#endif
+#if (DEBUG & DEBUG_NO_WRITE)
+    " NOWRITE ("NO_WRITE_STR")"
+#endif
+		"\n\n", VER_MAJOR, VER_MINOR, VER_PATCH);
+
+    p += sprintf (p,	"SBIC: WD33C93A  Address: %08X  IRQ : %d\n",
+			host->scsi.io_port, host->scsi.irq);
+#ifdef USE_DMAC
+    p += sprintf (p,	"DMAC: uPC71071  Address: %08X  IRQ : %d\n\n",
+			host->dma.io_port, host->scsi.irq);
+#endif
+
+    p += sprintf (p,	"Statistics:\n",
+			"Queued commands: %-10d    Issued commands: %-10d\n"
+			"Done commands  : %-10d    Reads          : %-10d\n"
+			"Writes         : %-10d    Others         : %-10d\n"
+			"Disconnects    : %-10d    Aborts         : %-10d\n"
+			"Resets         : %-10d\n\nLast phases:",
+			host->stats.queues,		host->stats.removes,
+			host->stats.fins,		host->stats.reads,
+			host->stats.writes,		host->stats.miscs,
+			host->stats.disconnects,	host->stats.aborts,
+			host->stats.resets);
+
+    for (devidx = 0; devidx < 9; devidx ++) {
+	unsigned int statptr, prev;
+
+	p += sprintf (p, "\n%c:", devidx == 8 ? 'H' : ('0' + devidx));
+	statptr = status_ptr[devidx] - 10;
+
+	if ((signed int)statptr < 0)
+	    statptr += 16;
+
+	prev = status[devidx][statptr].when;
+
+	for (; statptr != status_ptr[devidx]; statptr = (statptr + 1) & 15) {
+	    if (status[devidx][statptr].when) {
+		p += sprintf (p, "%c%02X:%02X+%2ld",
+			status[devidx][statptr].irq ? '-' : ' ',
+			status[devidx][statptr].ph,
+			status[devidx][statptr].ssr,
+			(status[devidx][statptr].when - prev) < 100 ?
+				(status[devidx][statptr].when - prev) : 99);
+		prev = status[devidx][statptr].when;
+	    }
+	}
+    }
+
+    p += sprintf (p, "\nAttached devices:%s\n", instance->host_queue ? "" : " none");
+
+    for (scd = instance->host_queue; scd; scd = scd->next) {
+	int len;
+
+	proc_print_scsidevice (scd, p, &len, 0);
+	p += len;
+
+	p += sprintf (p, "Extensions: ");
+
+	if (scd->tagged_supported)
+	    p += sprintf (p, "TAG %sabled [%d] ",
+			  scd->tagged_queue ? "en" : "dis", scd->current_tag);
+	p += sprintf (p, "\nTransfers: ");
+	if (host->device[scd->id].sync_xfer & 15)
+	    p += sprintf (p, "sync, offset %d, %d ns\n",
+			  host->device[scd->id].sync_xfer & 15,
+			  acornscsi_getperiod (host->device[scd->id].sync_xfer));
+	else
+	    p += sprintf (p, "async\n");
+
+	pos = p - buffer;
+	if (pos + begin < offset) {
+	    begin += pos;
+	    p = buffer;
+	}
+	pos = p - buffer;
+	if (pos + begin > offset + length)
+	    break;
+    }
+
+    pos = p - buffer;
+
+    *start = buffer + (offset - begin);
+    pos -= offset - begin;
+
+    if (pos > length)
+	pos = length;
+
+    return pos;
+}
+
+#ifdef MODULE
+
+Scsi_Host_Template driver_template = ACORNSCSI_3;
+
+#include "../../scsi/scsi_module.c"
+#endif

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen, slshen@lbl.gov