diff -u -r /usr/src/linux-1.2.13/drivers/scsi/53c7,8xx.c /usr/src/linux/drivers/scsi/53c7,8xx.c
--- /usr/src/linux-1.2.13/drivers/scsi/53c7,8xx.c	Wed Oct 25 13:23:28 1995
+++ /usr/src/linux/drivers/scsi/53c7,8xx.c	Mon Oct 30 17:33:26 1995
@@ -1,18 +1,55 @@
-/* 
- * Set these options for all host adapters.
- * 	- Memory mapped IO does not work.
- *	- Test 1 does a bus mastering test, which will help
- *	  weed out brain damaged main boards.
- */
-
-
-#define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1)
 /*
- * Define SCSI_MALLOC to use scsi_malloc instead of kmalloc.  Other than
- * preventing deadlock, I'm not sure why we'd want to do this.
+ * PERM_OPTIONS are driver options which will be enabled for all NCR boards
+ * in the system at driver initialization time.
+ *
+ * Don't THINK about touching these in PERM_OPTIONS : 
+ *   OPTION_IO_MAPPED 
+ * 	Memory mapped IO does not work under i86 Linux. 
+ *
+ *   OPTION_DEBUG_TEST1
+ *	Test 1 does bus mastering and interrupt tests, which will help weed 
+ *	out brain damaged main boards.
+ *
+ * These are development kernel changes.  Code for them included in this
+ * driver release may or may not work.  If you turn them on, you should be 
+ * running the latest copy of the development sources from
+ *
+ *	ftp://tsx-11.mit.edu/pub/linux/ALPHA/scsi/53c7,8xx
+ *
+ * and be subcribed to the ncr53c810@colorado.edu mailing list.  To
+ * subscribe, send mail to majordomo@colorado.edu with 
+ *
+ * 	subscribe ncr53c810
+ * 
+ * in the text.
+ *
+ *   OPTION_SYNCHRONOUS
+ *	Enable support for synchronous tranfers.  Target negotiated 
+ *	synchronous transfers will be responded to.  To initiate 
+ *	a synchronous transfer request,  call 
+ *
+ *	    request_synchronous (hostno, target) 
+ *
+ *	from within KGDB.
+ *
+ *   OPTION_ALWAYS_SYNCHRONOUS
+ *	Negotiate for synchronous transfers with every target after
+ *	driver initialization or a SCSI bus reset.  This is a bit dangerous, 
+ *	but more convienient than calling request_synchronous ()
+ *
+ *   OPTION_DISCONNECT
+ *	Enable support for disconnect/reconnect.  To change the 
+ *	default setting on a given host adapter, call
+ *
+ *	    request_disconnect (hostno, allow)
+ *
+ *	where allow is non-zero to allow, 0 to disallow.
  */
 
-#define SCSI_MALLOC
+/* #define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1|OPTION_DISCONNECT|\
+	OPTION_SYNCHRONOUS) */
+
+#define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1|OPTION_DISCONNECT)
 
 /*
  * Sponsored by 
@@ -23,7 +60,7 @@
  * Copyright 1993, 1994, 1995 Drew Eckhardt
  *      Visionary Computing 
  *      (Unix and Linux consulting and custom programming)
- *      drew@Colorado.EDU
+ *      drew@PoohSticks.ORG
  *	+1 (303) 786-7975
  *
  * TolerANT and SCSI SCRIPTS are registered trademarks of NCR Corporation.
@@ -89,20 +126,21 @@
  *	this means offloading the practical maximum amount of processing 
  *	to the SCSI chip.
  * 
- *	On the NCR53c810/820,  this is accomplished by using 
- *		interrupt-on-the-fly signals with the DSA address as a 
- *		parameter when commands complete, and only handling fatal 
- *		errors and SDTR / WDTR 	messages in the host code.
+ *	On the NCR53c810/820/720,  this is accomplished by using 
+ *		interrupt-on-the-fly signals when commands complete, 
+ *		and only handling fatal errors and SDTR / WDTR 	messages 
+ *		in the host code.
  *
- *	On the NCR53c710/720, interrupts are generated as on the NCR53c8x0,
+ *	On the NCR53c710, interrupts are generated as on the NCR53c8x0,
  *		only the lack of a interrupt-on-the-fly facility complicates
- *		things.  
+ *		things.   Also, SCSI ID registers and commands are 
+ *		bit fielded rather than binary encoded.
  *		
- * 	On the NCR53c700 and NCR53c700-66, operations that were done via 
- *		indirect, table mode on the more advanced chips have
- *		been replaced by calls through a jump table which 
+ * 	On the NCR53c700 and NCR53c700-66, operations that are done via 
+ *		indirect, table mode on the more advanced chips must be
+ *	        replaced by calls through a jump table which 
  *		acts as a surrogate for the DSA.  Unfortunately, this 
- * 		means that we must service an interrupt for each 
+ * 		will mean that we must service an interrupt for each 
  *		disconnect/reconnect.
  * 
  * 3.  Eliminating latency by pipelining operations at the different levels.
@@ -115,15 +153,10 @@
  *
  * Architecture : 
  * This driver is built around two queues of commands waiting to 
- * be executed - the Linux issue queue, and the shared Linux/NCR  
- * queue which are manipulated by the NCR53c7xx_queue_command and 
- * NCR53c7x0_intr routines.
- *
- * When the higher level routines pass a SCSI request down to 
- * NCR53c7xx_queue_command, it looks to see if that target/lun 
- * is currently busy. If not, the command is inserted into the 
- * shared Linux/NCR queue, otherwise it is inserted into the Linux 
- * queue.
+ * be executed - the Linux issue queue which gets passed commands by 
+ * queue_command, and the shared Linux/NCR  queue which has commands
+ * transfered to it by the run_process_issue_queue() function which
+ * is called whenever a command completes.
  *
  * As commands are completed, the interrupt routine is triggered,
  * looks for commands in the linked list of completed commands with
@@ -168,7 +201,10 @@
 #include <linux/bios32.h>
 #include <linux/pci.h>
 #include <linux/string.h>
+#include <linux/malloc.h>
 #include <linux/mm.h>
+#include <linux/ioport.h>
+#include <linux/time.h>
 #include "../block/blk.h"
 #include "scsi.h"
 #include "hosts.h"
@@ -176,7 +212,22 @@
 #include "constants.h"
 #include "sd.h"
 
+static void dump_events (struct Scsi_Host *host, int count);
+static Scsi_Cmnd * return_outstanding_commands (struct Scsi_Host *host, 
+    int free, int issue);
+static void hard_reset (struct Scsi_Host *host);
+static void ncr_scsi_reset (struct Scsi_Host *host);
+static void print_lots (struct Scsi_Host *host);
+static void set_synchronous (struct Scsi_Host *host, int target, int sxfer, 
+    int scntl3, int now_connected);
+static int datapath_residual (struct Scsi_Host *host);
+static const char * sbcl_to_phase (int sbcl);
+static void print_progress (Scsi_Cmnd *cmd);
+static void print_queues (struct Scsi_Host *host);
+static void process_issue_queue (void);
+static int shutdown (struct Scsi_Host *host);
 static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int result);
+static int disable (struct Scsi_Host *host);
 static int NCR53c8xx_run_tests (struct Scsi_Host *host);
 static int NCR53c8xx_script_len;
 static int NCR53c8xx_dsa_len;
@@ -185,7 +236,8 @@
 static void intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd 
     *cmd);
 static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd);
-static void print_dsa (struct Scsi_Host *host, unsigned long *dsa);
+static void print_dsa (struct Scsi_Host *host, unsigned long *dsa,
+    const char *prefix);
 static int print_insn (struct Scsi_Host *host, unsigned long *insn,
     char *prefix, int kernel);
 
@@ -195,23 +247,125 @@
     NCR53c7x0_cmd *cmd);
 static void NCR53c8x0_soft_reset (struct Scsi_Host *host);
 
-static int perm_options = PERM_OPTIONS;
+/* INSMOD variables */
+static long long perm_options = PERM_OPTIONS;
+/* 14 = .5s; 15 is max; decreasing divides by two. */
+static int selection_timeout = 14;
+/* Size of event list (per host adapter) */
+static int track_events = 0;
 
 static struct Scsi_Host *first_host = NULL;	/* Head of list of NCR boards */
 static Scsi_Host_Template *the_template = NULL;	
 
+/*
+ * KNOWN BUGS :
+ * - With OPTION_DISCONNECT, on two systems under unknown circumstances,
+ *	we get a PHASE MISMATCH with DSA set to zero (suggests that we 
+ *	are occurring somewhere in the reselection code) where 
+ *	DSP=some value DCMD|DBC=same value.  
+ * 	
+ *	Closer inspection suggests that we may be trying to execute
+ *	some portion of the DSA?
+ * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
+ * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
+ * scsi0 : no current command : unexpected phase MSGIN.
+ *         DSP=0x1c46cc, DCMD|DBC=0x1c46ac, DSA=0x0
+ *         DSPS=0x0, TEMP=0x1c3e70, DMODE=0x80
+ * scsi0 : DSP->
+ * 001c46cc : 0x001c46cc 0x00000000
+ * 001c46d4 : 0x001c5ea0 0x000011f8
+ *
+ *	Changed the print code in the phase_mismatch handler so
+ *	that we call print_lots to try and diagnose this.
+ *
+ * - On one system, we're seeing unexpected disconnects.  Added 
+ * 	print_lots() to the UDC interrupt handling code.
+ */
+
+/* 
+ * Possible future direction of architecture for max performance :
+ *
+ * We're using a single start queue for the NCR chip.  This is 
+ * sub-optimal, because we cannot add a command which would conflict with 
+ * an executing command to this start queue, and therefore must insert the 
+ * next command for a given I/T/L combination after the first has completed;
+ * incurring our interrupt latency between SCSI commands.
+ *
+ * To allow furthur pipelining of the NCR and host CPU operation, we want 
+ * to set things up so that immediately on termination of a command destined 
+ * for a given LUN, we get that LUN busy again.  
+ * 
+ * To do this, we need to add a 32 bit pointer to which is jumped to 
+ * on completion of a command.  If no new command is available, this 
+ * would point to the usual DSA issue queue select routine.
+ *
+ * If one were, it would point to a per-NCR53c7x0_cmd select routine 
+ * which starts execution immediately, inserting the command at the head 
+ * of the start queue if the NCR chip is selected or reselected.
+ *
+ * We would chanage so that we keep a list of outstanding commands 
+ * for each unit, rather than a single running_list.  We'd insert 
+ * a new command into the right running list; if the NCR didn't 
+ * have something running for that yet, we'd put it in the 
+ * start queue as well.  Some magic needs to happen to handle the 
+ * race condition between the first command terminating before the 
+ * new one is written.
+ *
+ * Flexibility change :
+ * 
+ * dsa_schedule assumes that the command at the head of the queue is a 
+ * normal SCSI command, with no flexibility provided for "abnormal" 
+ * (Ie, connect and send an ABORT message; SCSI SEND where we let the 
+ * chip calculate parity over the entire command; target mode) commands.
+ *
+ * dsa_schedule should become a bit more general, and be able to transfer
+ * control to _any_ process, providing the remove from queue code 
+ * as a call back (sort of like we do with other_transfer, etc).
+ *
+ * Potential for profiling : 
+ * Call do_gettimeofday(struct timeval *tv) to get 800ns resolution.
+ */
+
 
 /*
  * TODO : 
- *
- * 1.  Implement single step / trace code?
+ * 1.  Currently, when Icky things happen we do a FATAL().  Instead,
+ *     we want to do an integrity check on the parts of the NCR hostdata
+ *     structure which were initialized at boot time; FATAL() if that 
+ *     fails, and otherwise try to recover.  Keep track of how many
+ *     times this has happened within a single SCSI command; if it 
+ *     gets excessive, then FATAL().
+ *
+ * 2.  Parity checking is currently disabled, and a few things should 
+ *     happen here now that we support synchronous SCSI transfers :
+ *     1.  On soft-reset, we shuld set the EPC (Enable Parity Checking)
+ *	   and AAP (Assert SATN/ on parity error) bits in SCNTL0.
+ *	
+ *     2.  We should enable the parity interrupt in the SIEN0 register.
+ * 
+ *     3.  intr_phase_mismatch() needs to believe that message out is 
+ *	   allways an "acceptable" phase to have a mismatch in.  If 
+ *	   the old phase was MSG_IN, we should send a MESSAGE PARITY 
+ *	   error.  If the old phase was something else, we should send
+ *	   a INITIATOR_DETECTED_ERROR message.  Note that this could
+ *	   cause a RESTORE POINTERS message; so we should handle that 
+ *	   correctly first.  Instead, we should probably do an 
+ *	   initiator_abort.
+ *
+ * 3.  MPEE bit of CTEST4 should be set so we get interrupted if 
+ *     we detect an error.
+ *
+ * 4.  When doing non-fast SCSI, we should allow for the option of setting the 
+ *     EXT bit of STEST2.  This probably goes best with a move to a more 
+ *     general start queue which isn't tied as closely to initiator
+ *     SCSI commands.
  * 
- * 2.  The initial code has been tested on the NCR53c810.  I don't 
+ * 5.  The initial code has been tested on the NCR53c810.  I don't 
  *     have access to NCR53c700, 700-66 (Forex boards), NCR53c710
- *     (NCR Pentium systems), NCR53c720, or NCR53c820 boards to finish
- *     development on those platforms.
+ *     (NCR Pentium systems), NCR53c720, NCR53c820, or NCR53c825 boards to 
+ *     finish development on those platforms.
  *
- *     NCR53c820/720 - need to add wide transfer support, including WDTR 
+ *     NCR53c820/825/720 - need to add wide transfer support, including WDTR 
  *     		negotiation, programming of wide transfer capabilities
  *		on reselection and table indirect selection.
  *
@@ -225,9 +379,10 @@
  *		programmed on selection, so we need to add more code.
  * 
  *     NCR53c700/700-66 - need to add code to refix addresses on 
- *		every nexus change, eliminate all table indirect code.
+ *		every nexus change, eliminate all table indirect code,
+ *		very messy.
  *
- * 3.  The NCR53c7x0 series is very popular on other platforms that 
+ * 6.  The NCR53c7x0 series is very popular on other platforms that 
  *     could be running Linux - ie, some high performance AMIGA SCSI 
  *     boards use it.  
  *	
@@ -265,19 +420,20 @@
  */
 
 /*
- * Use to translate between device IDs of various types.
+ * Use pci_chips_ids to translate in both directions between PCI device ID 
+ * and chip numbers.  
  */
 
-struct pci_chip {
+
+
+static struct {
     unsigned short pci_device_id;
     int chip;
-    int min_revision;
     int max_revision;
-};
-
-static struct pci_chip pci_chip_ids[] = { 
-    {PCI_DEVICE_ID_NCR_53C810, 810, 1, 1}, 
-    {PCI_DEVICE_ID_NCR_53C815, 815, 2, 3},
+    int min_revision;
+} pci_chip_ids[] = { 
+    {PCI_DEVICE_ID_NCR_53C810, 810, 2, 1}, 
+    {PCI_DEVICE_ID_NCR_53C815, 815, 3, 2},
     {PCI_DEVICE_ID_NCR_53C820, 820, -1, -1},
     {PCI_DEVICE_ID_NCR_53C825, 825, -1, -1}
 };
@@ -285,7 +441,14 @@
 #define NPCI_CHIP_IDS (sizeof (pci_chip_ids) / sizeof(pci_chip_ids[0]))
 
 
-/* Forced detection and autoprobe code for various hardware */
+/* 
+ * Forced detection and autoprobe code for various hardware.  Currently, 
+ * entry points for these are not included in init/main.c because if the 
+ * PCI BIOS code isn't working right, you're not going to be able to 
+ * use the hardware anyways; this way we force users to solve their 
+ * problems rather than forcing detection and blaming us when it 
+ * does not work.
+ */
 
 static struct override {
     int chip;	/* 700, 70066, 710, 720, 810, 820 */
@@ -326,7 +489,8 @@
  * 
  */
 
-static void internal_setup(int board, int chip, char *str, int *ints) {
+static void 
+internal_setup(int board, int chip, char *str, int *ints) {
     unsigned char pci;		/* Specifies a PCI override, with bus, device,
 				   function */
 
@@ -384,25 +548,237 @@
 setup_wrapper(820)
 setup_wrapper(825)
 
+/* Template for "preferred" synchronous transfer parameters
+   FIXME: want to replace this with a per-target override. */
+
+static const unsigned char sdtr_message[] = {
+    EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 50 /* *4ns */, 8 /* off */ 
+};
+
+/* Template to request asynchronous transfers */
+
+static const unsigned char async_message[] = {
+    EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 0, 0 /* asynchronous */
+};
+
+/*
+ * Function : struct Scsi_Host *find_host (int host)
+ * 
+ * Purpose : KGDB support function which translates a host number 
+ * 	to a host structure. 
+ *
+ * Inputs : host - number of SCSI host
+ *
+ * Returns : NULL on failure, pointer to host structure on success.
+ */
+
+static struct Scsi_Host *
+find_host (int host) {
+    struct Scsi_Host *h;
+    for (h = first_host; h && h->host_no != host; h = h->next);
+    if (!h) {
+	printk (KERN_ALERT "scsi%d not found\n", host);
+	return NULL;
+    } else if (h->hostt != the_template) {
+	printk (KERN_ALERT "scsi%d is not a NCR board\n", host);
+	return NULL;
+    }
+    return h;
+}
+
+/*
+ * Function : request_synchronous (int host, int target)
+ * 
+ * Purpose : KGDB interface which will allow us to negotiate for 
+ * 	synchronous transfers.  This ill be replaced with a more 
+ * 	integrated function; perhaps a new entry in the scsi_host 
+ *	structure, accessable via an ioctl() or perhaps /proc/scsi.
+ *
+ * Inputs : host - number of SCSI host; target - number of target.
+ *
+ * Returns : 0 when negotiation has been setup for next SCSI command,
+ *	-1 on failure.
+ */
+
+static int
+request_synchronous (int host, int target) {
+    struct Scsi_Host *h;
+    struct NCR53c7x0_hostdata *hostdata;
+    unsigned long flags;
+    if (target < 0 || target > 7) {
+	printk (KERN_ALERT "target %d is bogus\n", target);
+	return -1;
+    }
+    if (!(h = find_host (host)))
+	return -1;
+    else if (h->this_id == target) {
+	printk (KERN_ALERT "target %d is host ID\n", target);
+	return -1;
+    }
+    hostdata = (struct NCR53c7x0_hostdata *)h->hostdata;
+
+    save_flags(flags);
+    cli();
+    if (hostdata->initiate_sdtr & (1 << target)) {
+	restore_flags(flags);
+	printk (KERN_ALERT "target %d allready doing SDTR\n", target);
+	return -1;
+    } 
+    hostdata->initiate_sdtr |= (1 << target);
+    restore_flags(flags);
+    return 0;
+}
+
+/*
+ * Function : request_disconnect (int host, int on_or_off)
+ * 
+ * Purpose : KGDB support function, tells us to allow or disallow 
+ *	disconnections.
+ *
+ * Inputs : host - number of SCSI host; on_or_off - non-zero to allow,
+ *	zero to disallow.
+ *
+ * Returns : 0 on success, *	-1 on failure.
+ */
+
+static int 
+request_disconnect (int host, int on_or_off) {
+    struct Scsi_Host *h;
+    struct NCR53c7x0_hostdata *hostdata;
+    if (!(h = find_host (host)))
+	return -1;
+    hostdata = (struct NCR53c7x0_hostdata *) h->hostdata;
+    if (on_or_off) 
+	hostdata->options |= OPTION_DISCONNECT;
+    else
+	hostdata->options &= ~OPTION_DISCONNECT;
+    return 0;
+}
+
+/*
+ * Function : static void NCR53c7x0_driver_init (struct Scsi_Host *host)
+ *
+ * Purpose : Initialize internal structures, as required on startup, or 
+ *	after a SCSI bus reset.
+ * 
+ * Inputs : host - pointer to this host adapter's structure
+ */
+
+static void 
+NCR53c7x0_driver_init (struct Scsi_Host *host) {
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+	host->hostdata;
+    int i, j;
+    for (i = 0; i < 8; ++i) {
+	hostdata->request_sense[i] = 0;
+    	for (j = 0; j < 8; ++j) 
+	    hostdata->busy[i][j] = 0;
+	set_synchronous (host, i, /* sxfer */ 0, hostdata->saved_scntl3, 0);
+    }
+    hostdata->issue_queue = NULL;
+    hostdata->running_list = hostdata->finished_queue = hostdata->current = NULL;
+    hostdata->issue_dsa_head = NULL;
+    hostdata->issue_dsa_tail = NULL;
+    hostdata->reconnect_dsa_head = NULL;
+    hostdata->addr_reconnect_dsa_head = (unsigned long **)
+	&(hostdata->reconnect_dsa_head);
+    hostdata->expecting_iid = 0;
+    hostdata->expecting_sto = 0;
+    /* FIXME - when we support WIDE target IDs, this needs to change */
+    if (hostdata->options & OPTION_ALWAYS_SYNCHRONOUS) 
+	hostdata->initiate_sdtr |= 0xff; 
+    else
+    	hostdata->initiate_sdtr = 0;
+    hostdata->talked_to = 0;
+    hostdata->idle = 1;
+}
+
+/* 
+ * Function : static int ccf_to_clock (int ccf)
+ *
+ * Purpose :  Return the largest SCSI clock allowable for a given
+ *	clock conversion factor, allowing us to do synchronous periods
+ *	when we don't know what the SCSI clock by taking at least as long as 
+ *	the device says we can.
+ *
+ * Inputs : ccf
+ *
+ * Returns : clock on success, -1 on failure.
+ */
+
+static int 
+ccf_to_clock (int ccf) {
+    switch (ccf) {
+    case 1: return 25000000; 	/* Divide by 1.0 */
+    case 2: return 37500000;	/* Divide by 1.5 */
+    case 3: return 50000000;  	/* Divide by 2.0 */
+    case 0: 			/* Divide by 3.0 */
+    case 4: return 66000000; 
+    default: return -1;
+    }
+}
+
+/* 
+ * Function : static int clock_to_ccf (int clock)
+ *
+ * Purpose :  Return the clock conversion factor for a given SCSI clock.
+ *
+ * Inputs : clock - SCSI clock expressed in Hz.
+ *
+ * Returns : ccf on success, -1 on failure.
+ */
+
+static int 
+clock_to_ccf (int clock) {
+    if (clock < 16666666)
+	return -1;
+    if (clock < 25000000)
+	return 1; 	/* Divide by 1.0 */
+    else if (clock < 37500000)
+	return 2; 	/* Divide by 1.5 */
+    else if (clock < 50000000)
+	return 3;	/* Divide by 2.0 */
+    else if (clock < 66000000)
+	return 4;	/* Divide by 3.0 */
+    else 
+	return -1;
+}
+    
 /* 
  * Function : static int NCR53c7x0_init (struct Scsi_Host *host)
  *
  * Purpose :  initialize the internal structures for a given SCSI host
  *
- * Inputs : host - pointer to this host adapter's structure/ 
+ * Inputs : host - pointer to this host adapter's structure
  *
  * Preconditions : when this function is called, the chip_type 
  * 	field of the hostdata structure MUST have been set.
+ *
+ * Returns : 0 on success, -1 on failure.
  */
 
 static int 
 NCR53c7x0_init (struct Scsi_Host *host) {
     NCR53c7x0_local_declare();
-    /* unsigned char tmp; */
-    int i, j, ccf;
+    int i, ccf, expected_ccf;
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;
     struct Scsi_Host *search;
+    /* 
+     * There are some things which we need to know about in order to provide
+     * a semblance of support.  Print 'em if they aren't what we expect, 
+     * otherwise don't add to the noise.
+     * 
+     * -1 means we don't know what to expect.
+     */
+    int expected_id = -1;
+    int expected_clock = -1;
+    int uninitialized = 0;
+    /* 
+     * FIXME : this is only on Intel boxes.  On other platforms, this
+     * will differ.
+     */
+    int expected_mapping = OPTION_IO_MAPPED;
     NCR53c7x0_local_setup(host);
 
     switch (hostdata->chip) {
@@ -417,7 +793,8 @@
     	hostdata->soft_reset = NCR53c8x0_soft_reset;
 	hostdata->run_tests = NCR53c8xx_run_tests;
 /* Is the SCSI clock ever anything else on these chips? */
-	hostdata->scsi_clock = 40000000;
+	expected_clock = hostdata->scsi_clock = 40000000;
+	expected_id = 7;
     	break;
     default:
 	printk ("scsi%d : chip type of %d is not supported yet, detaching.\n",
@@ -432,29 +809,12 @@
     hostdata->NCR53c7xx_msg_abort = ABORT;
     hostdata->NCR53c7xx_msg_nop = NOP;
 
-    /*
-     * Set up an interrupt handler if we aren't already sharing an IRQ
-     * with another board.
-     */
-
-    for (search = first_host; search && ((search->hostt != the_template) ||
-	(search->irq != host->irq)); search=search->next);
-
-    if (!search) {
-	if (request_irq(host->irq, NCR53c7x0_intr, SA_INTERRUPT, "53c7,8xx")) {
-	    printk("scsi%d : IRQ%d not free, detaching\n", 
-		host->host_no, host->irq);
-	    scsi_unregister (host);
-	    return -1;
-	} 
-    } else {
-	printk("scsi%d : using interrupt handler previously installed for scsi%d\n",
-	    host->host_no, search->host_no);
-    }
-
-    printk ("scsi%d : using %s mapped access\n", host->host_no, 
-	(hostdata->options & OPTION_MEMORY_MAPPED) ? "memory" : 
-	 "io");
+    if (expected_mapping == -1 || 
+	(hostdata->options & (OPTION_MEMORY_MAPPED)) != 
+	(expected_mapping & OPTION_MEMORY_MAPPED))
+	printk ("scsi%d : using %s mapped access\n", host->host_no, 
+	    (hostdata->options & OPTION_MEMORY_MAPPED) ? "memory" : 
+	    "io");
 
     hostdata->dmode = (hostdata->chip == 700 || hostdata->chip == 70066) ? 
 	DMODE_REG_00 : DMODE_REG_10;
@@ -480,9 +840,23 @@
     host->this_id = NCR53c7x0_read8(SCID_REG) & 7;
     hostdata->this_id_mask = 1 << host->this_id;
 #endif
+   
+/*
+ * Note : we should never encounter a board setup for ID0.  So,
+ * 	if we see ID0, assume that it was uninitialized and set it
+ * 	to the industry standard 7.
+ */
+    if (!host->this_id) {
+	printk("scsi%d : initiator ID was set to 0, changing to 7\n",
+	    host->host_no);
+	host->this_id = 7;
+	hostdata->this_id_mask = 1 << 7;
+	uninitialized = 1;
+    };
 
-    printk("scsi%d : using initiator ID %d\n", host->host_no,
-    	host->this_id);
+    if (expected_id == -1 || host->this_id != expected_id)
+    	printk("scsi%d : using initiator ID %d\n", host->host_no,
+    	    host->this_id);
 
     /*
      * Save important registers to allow a soft reset.
@@ -506,12 +880,17 @@
      * On NCR53c700 series chips, DCNTL controls the SCSI clock divisor,
      * on 800 series chips, it allows for a totem-pole IRQ driver.
      */
+
     hostdata->saved_dcntl = NCR53c7x0_read8(DCNTL_REG);
-    
+
+    /* 
+     * DCNTL_800_IRQM controls weather we are using an open drain
+     * driver (reset) or totem pole driver (set).  In all cases, 
+     * it's level active.  I suppose this is an issue when we're trying to 
+     * wire-or the same PCI INTx line?
+     */
     if ((hostdata->chip / 100) == 8)
-	printk ("scsi%d : using %s interrupts\n", host->host_no,
-	    (hostdata->saved_dcntl & DCNTL_800_IRQM) ? "edge triggered" :
-	    "level active");
+	hostdata->saved_dcntl &= ~DCNTL_800_IRQM;
 
     /*
      * DMODE controls DMA burst length, and on 700 series chips,
@@ -521,7 +900,7 @@
 
     /* 
      * Now that burst length and enabled/disabled status is known, 
-     * clue the user in on it.
+     * clue the user in on it.  
      */
    
     if ((hostdata->chip / 100) == 8) {
@@ -544,8 +923,18 @@
      * and normal clock conversion factors.
      */
     if (hostdata->chip / 100 == 8)  {
+	expected_ccf = clock_to_ccf (expected_clock);
 	hostdata->saved_scntl3 = NCR53c7x0_read8(SCNTL3_REG_800);
 	ccf = hostdata->saved_scntl3 & SCNTL3_800_CCF_MASK;
+	if (expected_ccf != -1 && ccf != expected_ccf && !ccf) {
+	    hostdata->saved_scntl3 = (hostdata->saved_scntl3 &
+		~SCNTL3_800_CCF_MASK) | expected_ccf;
+	    if (!uninitialized) {
+		printk ("scsi%d : reset ccf to %d from %d\n", 
+		    host->host_no, expected_ccf, ccf);
+		uninitialized = 1;
+	    }
+	}
     } else
     	ccf = 0;
 
@@ -553,63 +942,26 @@
      * If we don't have a SCSI clock programmed, pick one on the upper
      * bound of that allowed by NCR so that our transfers err on the 
      * slow side, since transfer period must be >= the agreed 
-     * appon period.
+     * upon period.
      */
 
-    if (!hostdata->scsi_clock) 
-	switch(ccf) {
-	case 1: hostdata->scsi_clock = 25000000; break;	/* Divide by 1.0 */
-	case 2: hostdata->scsi_clock = 37500000; break; /* Divide by 1.5 */
-	case 3: hostdata->scsi_clock = 50000000; break; /* Divide by 2.0 */
-	case 0: 					/* Divide by 3.0 */
- 	case 4:	hostdata->scsi_clock = 66000000; break; 
-	default: 
-	    printk ("scsi%d : clock conversion factor %d unknown.\n"
-		    "         synchronous transfers disabled\n",
-		    host->host_no, ccf);
-	    hostdata->options &= ~OPTION_SYNCHRONOUS;
-	    hostdata->scsi_clock = 0; 
-	}
+    if ((!hostdata->scsi_clock) && (hostdata->scsi_clock = ccf_to_clock (ccf)) 
+	== -1) {
+	printk ("scsi%d : clock conversion factor %d unknown.\n"
+		"         synchronous transfers disabled\n",
+		host->host_no, ccf);
+	hostdata->options &= ~OPTION_SYNCHRONOUS;
+	hostdata->scsi_clock = 0; 
+    }
 
-    printk ("scsi%d : using %dMHz SCSI clock\n", host->host_no, 
-	hostdata->scsi_clock / 1000000);
-    /*
-     * Initialize per-target structures, including busy flags and 
-     * synchronous transfer parameters.
-     */
+    if (expected_clock == -1 || hostdata->scsi_clock != expected_clock)
+    	printk ("scsi%d : using %dMHz SCSI clock\n", host->host_no, 
+	    hostdata->scsi_clock / 1000000);
 
-    for (i = 0; i < 8; ++i) {
-    	hostdata->cmd_allocated[i] = 0;
-    	for (j = 0; j < 8; ++j)
-	    hostdata->busy[i][j] = 0;
-	/* 
-	 * NCR53c700 and NCR53c700-66 chips lack the DSA and use a 
-	 * different architecture.  For chips using the DSA architecture,
-	 * initialize the per-target synchronous parameters. 
-	 */
-	if (hostdata->chip != 700 && hostdata->chip != 70066) {
-	    hostdata->sync[i].select_indirect |= (i << 16); 
-	    /* XXX - program SCSI script for immediate return */ 
-	    hostdata->sync[i].script[0] = (DCMD_TYPE_TCI|DCMD_TCI_OP_RETURN) << 24 | 
-		DBC_TCI_TRUE;
-	    switch (hostdata->chip) {
-	    /* Clock divisor */
-    	    case 825:
-	    case 820:
-		/* Fall through to 810 */
-    	    case 815:
-	    case 810:
-		hostdata->sync[i].select_indirect |= (hostdata->saved_scntl3) << 24;
-		break;
-	    default:
-	    }
-    	}
-    }
+    /* FIXME : change when we support WIDE targets 8-15 */
 
-    hostdata->issue_queue = hostdata->running_list = 
-    	hostdata->finished_queue = NULL;
-    hostdata->issue_dsa_head = NULL;
-    hostdata->issue_dsa_tail = NULL;
+    for (i = 0; i < 8; ++i) 
+	hostdata->cmd_allocated[i] = 0;
 
     if (hostdata->init_save_regs)
     	hostdata->init_save_regs (host);
@@ -621,8 +973,6 @@
 	first_host = host;
     }
 
-    hostdata->idle = 1;
-
     /* 
      * Linux SCSI drivers have always been plagued with initialization 
      * problems - some didn't work with the BIOS disabled since they expected
@@ -637,16 +987,54 @@
 
     hostdata->debug_count_limit = -1;
     hostdata->intrs = -1;
-    hostdata->expecting_iid = 0;
-    hostdata->expecting_sto = 0;
+    hostdata->resets = -1;
+    memcpy ((void *) hostdata->synchronous_want, (void *) sdtr_message, 
+	sizeof (hostdata->synchronous_want));
+
+    NCR53c7x0_driver_init (host);
+
+    /*
+     * Set up an interrupt handler if we aren't already sharing an IRQ
+     * with another board.
+     */
+
+    for (search = first_host; search && !(search->hostt == the_template &&
+	search->irq == host->irq && search != host); search=search->next);
+
+    if (!search) {
+	if (request_irq(host->irq, NCR53c7x0_intr, SA_INTERRUPT, "53c7,8xx")) {
+	    printk("scsi%d : IRQ%d not free, detaching\n"
+	           "         You have either a configuration problem, or a\n"
+                   "         broken BIOS.  You may wish to manually assign\n"
+	           "         an interrupt to the NCR board rather than using\n"
+                   "         an automatic setting.\n", 
+		host->host_no, host->irq);
+	    scsi_unregister (host);
+	    return -1;
+	} 
+    } else {
+	printk("scsi%d : using interrupt handler previously installed for scsi%d\n",
+	    host->host_no, search->host_no);
+    }
+
 
     if ((hostdata->run_tests && hostdata->run_tests(host) == -1) ||
         (hostdata->options & OPTION_DEBUG_TESTS_ONLY)) {
     	/* XXX Should disable interrupts, etc. here */
 	scsi_unregister (host);
     	return -1;
-    } else 
-    	return 0;
+    } else {
+	if (host->io_port)  {
+	    host->n_io_port = 128;
+	    request_region (host->io_port, host->n_io_port, "ncr53c7,8xx");
+	}
+    }
+    
+    if (NCR53c7x0_read8 (SBCL_REG) & SBCL_BSY) {
+	printk ("scsi%d : bus wedge, doing SCSI reset\n", host->host_no);
+	hard_reset (host);
+    }
+    return 0;
 }
 
 /* 
@@ -670,7 +1058,8 @@
  *
  */
 
-static int normal_init (Scsi_Host_Template *tpnt, int board, int chip, 
+static int 
+normal_init (Scsi_Host_Template *tpnt, int board, int chip, 
     int base, int io_port, int irq, int dma, int pci_valid, 
     unsigned char pci_bus, unsigned char pci_device_fn, int options) {
     struct Scsi_Host *instance;
@@ -825,6 +1214,30 @@
     hostdata->free->free = NULL;
     hostdata->free->next = NULL;
 
+    hostdata->extra_allocate = 0;
+
+/* 
+ * For diagnostic purposes, we don't really care how fast things blaze.
+ * For profiling, we want to access the 800ns resolution system clock,
+ * using a 'C' call on the host processor.
+ *
+ * Therefore, there's no need for the NCR chip to directly manipulate
+ * this data, and we should put it wherever is most convienient for 
+ * Linux.
+ */
+    if (track_events) 
+	hostdata->events = (struct NCR53c7x0_event *) (track_events ? 
+	    vmalloc (sizeof (struct NCR53c7x0_event) * track_events) : NULL);
+    else
+	hostdata->events = NULL;
+
+    if (hostdata->events) {
+	memset ((void *) hostdata->events, 0, sizeof(struct NCR53c7x0_event) *
+	    track_events);	
+	hostdata->event_size = track_events;
+	hostdata->event_index = 0;
+    } else 
+	hostdata->event_size = 0;
 
     return NCR53c7x0_init(instance);
 }
@@ -851,7 +1264,8 @@
  *
  */
 
-static int pci_init (Scsi_Host_Template *tpnt, int board, int chip, 
+static int 
+pci_init (Scsi_Host_Template *tpnt, int board, int chip, 
     unsigned char bus, unsigned char device_fn, int options) {
     unsigned short vendor_id, device_id, command;
     unsigned long base, io_port; 
@@ -864,7 +1278,7 @@
 	bus, (int) (device_fn & 0xf8) >> 3, 
     	(int) device_fn & 7);
 
-    if (!pcibios_present) {
+    if (!pcibios_present()) {
 	printk("scsi-ncr53c7,8xx : not initializing due to lack of PCI BIOS,\n"
 	       "        try using memory, port, irq override instead.\n");
 	return -1;
@@ -959,6 +1373,12 @@
 	printk ("scsi-ncr53c7,8xx : warning : revision of %d is less than %d.\n",
 	    (int) revision, min_revision);
 
+    if (io_port && check_region (io_port, 128)) {
+	printk ("scsi-ncr53c7,8xx : IO region 0x%lx to 0x%lx is in use\n",
+	    io_port, io_port + 127);
+	return -1;
+    }
+
     return normal_init (tpnt, board, chip, (int) base, (int) io_port, 
 	(int) irq, DMA_NONE, 1, bus, device_fn, options);
 }
@@ -977,7 +1397,8 @@
  *
  */
 
-int NCR53c7xx_detect(Scsi_Host_Template *tpnt) {
+int 
+NCR53c7xx_detect(Scsi_Host_Template *tpnt) {
     int i;
     int current_override;
     int count;			/* Number of boards detected */
@@ -1023,6 +1444,9 @@
 /* NCR53c810 and NCR53c820 script handling code */
 
 #include "53c8xx_d.h"
+#ifdef A_int_debug_sync
+#define DEBUG_SYNC_INTR A_int_debug_sync
+#endif
 static int NCR53c8xx_script_len = sizeof (SCRIPT);
 static int NCR53c8xx_dsa_len = A_dsa_end + Ent_dsa_zero - Ent_dsa_code_template;
 
@@ -1041,12 +1465,11 @@
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;
     unsigned char tmp;
-    int i, ncr_to_memory, memory_to_ncr, ncr_to_ncr;
+    int i, ncr_to_memory, memory_to_ncr;
     unsigned long base;
     NCR53c7x0_local_setup(host);
 
 
-
     /* XXX - NOTE : this code MUST be made endian aware */
     /*  Copy code into buffer that was allocated at detection time.  */
     memcpy ((void *) hostdata->script, (void *) SCRIPT, 
@@ -1069,12 +1492,19 @@
     /* 
      * Fixup absolutes set at boot-time.
      * 
-     * All Absolute variables suffixed with "dsa_" and "int_"
+     * All non-code absolute variables suffixed with "dsa_" and "int_"
      * are constants, and need no fixup provided the assembler has done 
      * it for us (I don't know what the "real" NCR assembler does in 
      * this case, my assembler does the right magic).
      */
 
+    patch_abs_rwri_data (hostdata->script, 0, dsa_save_data_pointer, 
+    	Ent_dsa_code_save_data_pointer - Ent_dsa_zero);
+    patch_abs_rwri_data (hostdata->script, 0, dsa_restore_pointers,
+    	Ent_dsa_code_restore_pointers - Ent_dsa_zero);
+    patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect,
+    	Ent_dsa_code_check_reselect - Ent_dsa_zero);
+
     /*
      * Just for the hell of it, preserve the settings of 
      * Burst Length and Enable Read Line bits from the DMODE 
@@ -1088,14 +1518,12 @@
     	base = (long) host->io_port;
     	memory_to_ncr = tmp|DMODE_800_DIOM;
     	ncr_to_memory = tmp|DMODE_800_SIOM;
-    	ncr_to_ncr = tmp|DMODE_800_DIOM|DMODE_800_SIOM;
     } else {
     	base = (long) host->base;
-    	ncr_to_ncr = memory_to_ncr = ncr_to_memory = tmp;
+	memory_to_ncr = ncr_to_memory = tmp;
     }
 
     patch_abs_32 (hostdata->script, 0, addr_scratch, base + SCRATCHA_REG_800);
-    patch_abs_32 (hostdata->script, 0, addr_sfbr, base + SFBR_REG);
     patch_abs_32 (hostdata->script, 0, addr_temp, base + TEMP_REG);
 
     /*
@@ -1110,21 +1538,56 @@
     patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_memory, tmp);
     patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_ncr, memory_to_ncr);
     patch_abs_rwri_data (hostdata->script, 0, dmode_ncr_to_memory, ncr_to_memory);
-    patch_abs_rwri_data (hostdata->script, 0, dmode_ncr_to_ncr, ncr_to_ncr);
 
     patch_abs_32 (hostdata->script, 0, issue_dsa_head, 
     	(long) &(hostdata->issue_dsa_head));
     patch_abs_32 (hostdata->script, 0, msg_buf, (long) &(hostdata->msg_buf));
     patch_abs_32 (hostdata->script, 0, reconnect_dsa_head, 
     	(long) &(hostdata->reconnect_dsa_head));
+    patch_abs_32 (hostdata->script, 0, addr_reconnect_dsa_head, (long) 
+	&(hostdata->addr_reconnect_dsa_head));
     patch_abs_32 (hostdata->script, 0, reselected_identify, 
     	(long) &(hostdata->reselected_identify));
+/* reselected_tag is currently unused */
+#if 0
     patch_abs_32 (hostdata->script, 0, reselected_tag, 
     	(long) &(hostdata->reselected_tag));
+#endif
 
     patch_abs_32 (hostdata->script, 0, test_dest, (long) &(hostdata->test_dest));
     patch_abs_32 (hostdata->script, 0, test_src, (long) &(hostdata->test_source));
+    patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect, 
+	(unsigned char)(Ent_dsa_code_check_reselect - Ent_dsa_zero));
 
+/* These are for event logging; the ncr_event enum contains the 
+   actual interrupt numbers. */
+#ifdef A_int_EVENT_SELECT
+   patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT, (long) EVENT_SELECT);
+#endif
+#ifdef A_int_EVENT_DISCONNECT
+   patch_abs_32 (hostdata->script, 0, int_EVENT_DISCONNECT, (long) EVENT_DISCONNECT);
+#endif
+#ifdef A_int_EVENT_RESELECT
+   patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT, (long) EVENT_RESELECT);
+#endif
+#ifdef A_int_EVENT_COMPLETE
+   patch_abs_32 (hostdata->script, 0, int_EVENT_COMPLETE, (long) EVENT_COMPLETE);
+#endif
+#ifdef A_int_EVENT_IDLE
+   patch_abs_32 (hostdata->script, 0, int_EVENT_IDLE, (long) EVENT_IDLE);
+#endif
+#ifdef A_int_EVENT_SELECT_FAILED
+   patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT_FAILED, 
+	(long) EVENT_SELECT_FAILED);
+#endif
+#ifdef A_int_EVENT_BEFORE_SELECT
+   patch_abs_32 (hostdata->script, 0, int_EVENT_BEFORE_SELECT,
+	(long) EVENT_BEFORE_SELECT);
+#endif
+#ifdef A_int_EVENT_RESELECT_FAILED
+   patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT_FAILED, 
+	(long) EVENT_RESELECT_FAILED);
+#endif
 
     /*
      * Make sure the NCR and Linux code agree on the location of 
@@ -1137,12 +1600,17 @@
  */
     hostdata->E_accept_message = Ent_accept_message;
     hostdata->E_command_complete = Ent_command_complete;		
+    hostdata->E_cmdout_cmdout = Ent_cmdout_cmdout;
+    hostdata->E_data_transfer = Ent_data_transfer;
     hostdata->E_debug_break = Ent_debug_break;	
     hostdata->E_dsa_code_template = Ent_dsa_code_template;
     hostdata->E_dsa_code_template_end = Ent_dsa_code_template_end;
+    hostdata->E_end_data_transfer = Ent_end_data_transfer;
     hostdata->E_initiator_abort = Ent_initiator_abort;
     hostdata->E_msg_in = Ent_msg_in;
     hostdata->E_other_transfer = Ent_other_transfer;
+    hostdata->E_other_in = Ent_other_in;
+    hostdata->E_other_out = Ent_other_out;
     hostdata->E_reject_message = Ent_reject_message;
     hostdata->E_respond_message = Ent_respond_message;
     hostdata->E_schedule = Ent_schedule;			
@@ -1197,7 +1665,8 @@
  * 
  */
 
-static int NCR53c8xx_run_tests (struct Scsi_Host *host) {
+static int 
+NCR53c8xx_run_tests (struct Scsi_Host *host) {
     NCR53c7x0_local_declare();
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;
@@ -1228,7 +1697,8 @@
 	hostdata->issue_dsa_head = NULL;
     }
 	
-    if (hostdata->options & OPTION_DEBUG_TEST1) {
+    if ((hostdata->options & OPTION_DEBUG_TEST1) && 
+	    hostdata->state != STATE_DISABLED) {
 	hostdata->idle = 0;
 	hostdata->test_running = 1;
 	hostdata->test_completed = -1;
@@ -1288,7 +1758,8 @@
 	hostdata->issue_dsa_head = NULL;
     }
 
-    if (hostdata->options & OPTION_DEBUG_TEST2) {
+    if ((hostdata->options & OPTION_DEBUG_TEST2) && 
+	hostdata->state != STATE_DISABLED) {
 	unsigned long dsa[48];
     	unsigned char identify = IDENTIFY(0, 0);
 	unsigned char cmd[6];
@@ -1380,7 +1851,8 @@
  *	enough to hold the NCR53c8xx dsa.
  */
 
-static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd) {
+static void 
+NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd) {
     Scsi_Cmnd *c = cmd->cmd;
     struct Scsi_Host *host = c->host;
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
@@ -1390,23 +1862,76 @@
     memcpy (cmd->dsa, hostdata->script + (hostdata->E_dsa_code_template / 4),
     	hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template);
 
-    patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(long), 
-    	dsa_temp_jump_resume, ((unsigned long) cmd->dsa) + 
-    	Ent_dsa_jump_resume - Ent_dsa_zero);
-    patch_abs_rwri_data (cmd->dsa, Ent_dsa_code_template / sizeof(long),
+    /* 
+     * Note : within the NCR 'C' code, dsa points to the _start_
+     * of the DSA structure, and _not_ the offset of dsa_zero within
+     * that structure used to facilitate shorter signed offsets
+     * for the 8 bit ALU.
+     * 
+     * The implications of this are that 
+     * 
+     * - 32 bit A_dsa_* absolute values require an additional 
+     * 	 dsa_zero added to their value to be correct, since they are 
+     *   relative to dsa_zero which is in essentially a separate
+     *   space from the code symbols.
+     *
+     * - All other symbols require no special treatment.
+     */
+
+    patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(long),
     	dsa_temp_lun, c->lun);
     patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(long),
-    	dsa_temp_dsa_next, ((unsigned long) cmd->dsa) + A_dsa_next);
+	dsa_temp_addr_next, (unsigned long)&cmd->dsa_next);
+    patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(long),
+    	dsa_temp_next, ((unsigned long) cmd->dsa) + Ent_dsa_zero -
+	Ent_dsa_code_template + A_dsa_next);
     patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(long), 
-    	dsa_temp_sync, hostdata->sync[c->target].select_indirect);
-    patch_abs_rwri_data (cmd->dsa, Ent_dsa_code_template / sizeof(long),
+    	dsa_temp_sync, (long) hostdata->sync[c->target].script);
+    patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(long),
     	dsa_temp_target, c->target);
+    /* XXX - new pointer stuff */
+    patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(long),
+    	dsa_temp_addr_saved_pointer, (unsigned long)&cmd->saved_data_pointer);
+    patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(long),
+    	dsa_temp_addr_saved_residual, (unsigned long)&cmd->saved_residual);
+    patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(long),
+    	dsa_temp_addr_residual, (unsigned long)&cmd->residual);
 }
 
-/*
- * Function : static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int
- *	result)
- *
+/* 
+ * Function : run_process_issue_queue (void)
+ * 
+ * Purpose : insure that the coroutine is running and will process our 
+ * 	request.  process_issue_queue_running is checked/set here (in an 
+ *	inline function) rather than in process_issue_queue itself to reduce 
+ * 	the chances of stack overflow.
+ *
+ */
+
+static volatile int process_issue_queue_running = 0;
+
+static __inline__ void 
+run_process_issue_queue(void) {
+    unsigned long flags;
+    save_flags (flags);
+    cli();
+    if (!process_issue_queue_running) {
+	process_issue_queue_running = 1;
+        process_issue_queue();
+	/* 
+         * process_issue_queue_running is cleared in process_issue_queue 
+	 * once it can't do more work, and process_issue_queue exits with 
+	 * interrupts disabled.
+	 */
+    }
+    restore_flags (flags);
+}
+
+
+/*
+ * Function : static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int
+ *	result)
+ *
  * Purpose : mark SCSI command as finished, OR'ing the host portion 
  *	of the result word into the result field of the corresponding
  *	Scsi_Cmnd structure, and removing it from the internal queues.
@@ -1418,7 +1943,8 @@
  *	the NCR expects to have exclusive access to.
  */
 
-static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int result) {
+static void 
+abnormal_finished (struct NCR53c7x0_cmd *cmd, int result) {
     Scsi_Cmnd *c = cmd->cmd;
     struct Scsi_Host *host = c->host;
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
@@ -1430,12 +1956,13 @@
     save_flags(flags);
     cli();
     for (i = 0; i < 2; ++i) {
-    	for (search = (char *) (i ? hostdata->issue_dsa_head : 
-    	    	hostdata->reconnect_dsa_head), prev = (char **) (i ? 
-    	    	&(hostdata->issue_dsa_head) : &(hostdata->reconnect_dsa_head));
-    	     search && (search + hostdata->dsa_start) != (char *) cmd->dsa;
-    	     prev = (char **) (search + hostdata->dsa_next), 
-    	    	search = *prev);
+    	for (search = (i ? (char *) hostdata->issue_dsa_head : 
+    	    	    (char *) hostdata->reconnect_dsa_head), 
+	    	prev = (char **) (i ? (char **) &(hostdata->issue_dsa_head) : 
+		    (char **) &(hostdata->reconnect_dsa_head));
+    	     	search && (search + hostdata->dsa_start) != (char *) cmd->dsa;
+    	     	prev = (char **) (search + hostdata->dsa_next), 
+    	    	    search = *prev);
 
     	if (search)
     	    *prev = *(char **) (search + hostdata->dsa_next);
@@ -1458,6 +1985,7 @@
     c->scsi_done(c);
 
     restore_flags(flags);
+    run_process_issue_queue();
 }
 
 /* 
@@ -1472,7 +2000,8 @@
  *
  */
 
-static void intr_break (struct Scsi_Host *host, struct 
+static void 
+intr_break (struct Scsi_Host *host, struct 
     NCR53c7x0_cmd *cmd) {
     NCR53c7x0_local_declare();
     struct NCR53c7x0_break *bp;
@@ -1515,45 +2044,133 @@
 
     restore_flags(flags);
 }
+/*
+ * Function : static void print_synchronous (const char *prefix, 
+ *	const unsigned char *msg)
+ * 
+ * Purpose : print a pretty, user and machine parsable representation
+ *	of a SDTR message, including the "real" parameters, data
+ *	clock so we can tell transfer rate at a glance.
+ *
+ * Inputs ; prefix - text to prepend, msg - SDTR message (5 bytes)
+ */
+
+static void
+print_synchronous (const char *prefix, const unsigned char *msg) {
+    if (msg[4]) {
+	int Hz = 1000000000 / (msg[3] * 4);
+	int integer = Hz / 1000000;
+	int fraction = (Hz - (integer * 1000000)) / 10000;
+	printk ("%speriod %dns offset %d %d.%02dMHz %s SCSI%s\n",
+	    prefix, (int) msg[3] * 4, (int) msg[4], integer, fraction,
+	    (((msg[3] * 4) < 200) ? "FAST" : "synchronous"),
+	    (((msg[3] * 4) < 200) ? "-II" : ""));
+    } else 
+	printk ("%sasynchronous SCSI\n", prefix);
+}
 
 /*
- * Function : static int asynchronous (struct Scsi_Host *host, int target)
- *
- * Purpose : reprogram between the selected SCSI Host adapter and target 
- *      (assumed to be currently connected) for asynchronous transfers.
+ * Function : static void set_synchronous (struct Scsi_Host *host, 
+ *	 	int target, int sxfer, int scntl3, int now_connected)
  *
- * Inputs : host - SCSI host structure, target - numeric target ID.
+ * Purpose : reprogram transfers between the selected SCSI initiator and 
+ *	target with the given register values; in the indirect
+ *	select operand, reselection script, and chip registers.
  *
- * Preconditions : the NCR chip should be in one of the halted states
+ * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
+ *	sxfer and scntl3 - NCR registers. now_connected - if non-zero, 
+ *	we should reprogram the registers now too.
  */
-    
-static int asynchronous (struct Scsi_Host *host, int target) {
+
+
+static void
+set_synchronous (struct Scsi_Host *host, int target, int sxfer, int scntl3,
+    int now_connected) {
     NCR53c7x0_local_declare();
-    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *) 
 	host->hostdata;
+    volatile unsigned long *script;
     NCR53c7x0_local_setup(host);
 
-    if ((hostdata->chip / 100) == 8) {
-	hostdata->sync[target].select_indirect = (hostdata->saved_scntl3 << 24)
-	    | (target << 16);
-/* Fill in script here */
-    } else if ((hostdata->chip != 700) && (hostdata->chip != 70066)) {
-	hostdata->sync[target].select_indirect = (1 << (target & 7)) << 16;
-    }
+    /* These are eight bit registers */
+    sxfer &= 0xff;
+    scntl3 &= 0xff;
+
+    hostdata->sync[target].sxfer_sanity = sxfer;
+    hostdata->sync[target].scntl3_sanity = scntl3;
 
 /* 
- * Halted implies connected, when resetting we shouldn't change the 
- * current parameters but must reset all targets to asynchronous.
+ * HARD CODED : synchronous script is EIGHT long words long.  This 
+ * must agree with 53c7.8xx.h
  */
 
-    if (hostdata->state == STATE_HALTED) {
+    if ((hostdata->chip != 700) && (hostdata->chip != 70066)) {
+	hostdata->sync[target].select_indirect = (scntl3 << 24) | 
+	    (target << 16) | (sxfer << 8);
+
+	script = (volatile unsigned long *) hostdata->sync[target].script;
+
+	/* XXX - add NCR53c7x0 code to reprogram SCF bits if we want to */
 	if ((hostdata->chip / 100) == 8) {
-	    NCR53c7x0_write8 (SCNTL3_REG_800, hostdata->saved_scntl3);
+	    script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
+		DCMD_RWRI_OP_MOVE) << 24) |
+		(SCNTL3_REG_800 << 16) | (scntl3 << 8);
+	    script[1] = 0;
+	    script += 2;
+	}
+
+	script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
+	    DCMD_RWRI_OP_MOVE) << 24) |
+		(SXFER_REG << 16) | (sxfer << 8);
+	script[1] = 0;
+	script += 2;
+
+#ifdef DEBUG_SYNC_INTR
+	if (hostdata->options & OPTION_DEBUG_DISCONNECT) {
+	    script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_INT) << 24) | DBC_TCI_TRUE;
+	    script[1] = DEBUG_SYNC_INTR;
+	    script += 2;
 	}
-    /* Offset = 0, transfer period = divide SCLK by 4 */
-	NCR53c7x0_write8 (SXFER_REG, 0);
+#endif
+
+	script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_RETURN) << 24) | DBC_TCI_TRUE;
+	script[1] = 0;
+	script += 2;
     }
-    return 0;
+
+    if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS) 
+	printk ("scsi%d : target %d sync parameters are sxfer=0x%x, scntl3=0x%x\n",
+	host->host_no, target, sxfer, scntl3);
+
+    if (now_connected) {
+	if ((hostdata->chip / 100) == 8) 
+	    NCR53c7x0_write8(SCNTL3_REG_800, scntl3);
+	NCR53c7x0_write8(SXFER_REG, sxfer);
+    }
+}
+
+
+/*
+ * Function : static int asynchronous (struct Scsi_Host *host, int target)
+ *
+ * Purpose : reprogram between the selected SCSI Host adapter and target 
+ *      (assumed to be currently connected) for asynchronous transfers.
+ *
+ * Inputs : host - SCSI host structure, target - numeric target ID.
+ *
+ * Preconditions : the NCR chip should be in one of the halted states
+ */
+    
+static void
+asynchronous (struct Scsi_Host *host, int target) {
+    NCR53c7x0_local_declare();
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+	host->hostdata;
+    NCR53c7x0_local_setup(host);
+    set_synchronous (host, target, /* no offset */ 0, hostdata->saved_scntl3,
+	1);
+    printk ("scsi%d : setting target %d to asynchronous SCSI\n",
+	host->host_no, target);
 }
 
 /* 
@@ -1565,9 +2182,9 @@
 
 /* Table for NCR53c8xx synchronous values */
 static const struct {
-    int div;
-    unsigned char scf;
-    unsigned char tp;
+    int div;		/* Total clock divisor * 10 */
+    unsigned char scf;	/* */
+    unsigned char tp;	/* 4 + tp = xferp divisor */
 } syncs[] = {
 /*	div	scf	tp	div	scf	tp	div	scf	tp */
     {	40,	1,	0}, {	50,	1,	1}, {	60,	1,	2}, 
@@ -1595,60 +2212,50 @@
  */
 
 
-static void synchronous (struct Scsi_Host *host, int target, char *msg) {
+static void 
+synchronous (struct Scsi_Host *host, int target, char *msg) {
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;
     int desire, divisor, i, limit;
-    unsigned long *script;
     unsigned char scntl3, sxfer;
+/* The diagnostic message fits on one line, even with max. width integers */
+    char buf[80];	
    
-/* Scale divisor by 10 to accommodate fractions */ 
+/* Desired transfer clock in Hz */
     desire = 1000000000L / (msg[3] * 4);
-    divisor = desire / (hostdata->scsi_clock / 10);
+/* Scale the available SCSI clock by 10 so we get tenths */
+    divisor = (hostdata->scsi_clock * 10) / desire;
 
+/* NCR chips can handle at most an offset of 8 */
     if (msg[4] > 8)
 	msg[4] = 8;
 
-    printk("scsi%d : optimal synchronous divisor of %d.%01d\n", host->host_no,
-	divisor / 10, divisor % 10);
-
-    limit = (sizeof(syncs) / sizeof(syncs[0])) - 1;
-    for (i = 0; (i < limit) && (divisor < syncs[i + 1].div); ++i);
-
-    printk("scsi%d : selected synchronous divisor of %d.%01d\n", host->host_no,
-	syncs[i].div / 10, syncs[i].div % 10);
-
-    msg[3] = (1000000000 / divisor / 10 / 4);
+    if (hostdata->options & OPTION_DEBUG_SDTR)
+    	printk("scsi%d : optimal synchronous divisor of %d.%01d\n", 
+	    host->host_no, divisor / 10, divisor % 10);
+
+    limit = (sizeof(syncs) / sizeof(syncs[0]) -1);
+    for (i = 0; (i < limit) && (divisor > syncs[i].div); ++i);
+
+    if (hostdata->options & OPTION_DEBUG_SDTR)
+    	printk("scsi%d : selected synchronous divisor of %d.%01d\n", 
+	    host->host_no, syncs[i].div / 10, syncs[i].div % 10);
+
+    msg[3] = ((1000000000L / hostdata->scsi_clock) * syncs[i].div / 10 / 4);
+
+    if (hostdata->options & OPTION_DEBUG_SDTR)
+    	printk("scsi%d : selected synchronous period of %dns\n", host->host_no,
+	    msg[3] * 4);
 
     scntl3 = (hostdata->chip / 100 == 8) ? ((hostdata->saved_scntl3 & 
 	~SCNTL3_800_SCF_MASK) | (syncs[i].scf << SCNTL3_800_SCF_SHIFT)) : 0;
     sxfer = (msg[4] << SXFER_MO_SHIFT) | ((syncs[i].tp) << SXFER_TP_SHIFT);
-
-    if ((hostdata->chip != 700) && (hostdata->chip != 70066)) {
-	hostdata->sync[target].select_indirect = (scntl3 << 24) | (target << 16) | 
-		(sxfer << 8);
-
-	script = (long *) hostdata->sync[target].script;
-
-	/* XXX - add NCR53c7x0 code to reprogram SCF bits if we want to */
-	if ((hostdata->chip / 100) == 8) {
-	    script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
-		DCMD_RWRI_OP_MOVE) << 24) |
-		(SCNTL3_REG_800 << 16) | (scntl3 << 8);
-	    script[1] = 0;
-	    script += 2;
-	}
-
-	script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
-	    DCMD_RWRI_OP_MOVE) << 24) |
-		(SXFER_REG << 16) | (sxfer << 8);
-	script[1] = 0;
-	script += 2;
-
-	script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_RETURN) << 24) | DBC_TCI_TRUE;
-	script[1] = 0;
-	script += 2;
-    }
+    if (hostdata->options & OPTION_DEBUG_SDTR)
+    	printk ("scsi%d : sxfer=0x%x scntl3=0x%x\n", 
+	    host->host_no, (int) sxfer, (int) scntl3);
+    set_synchronous (host, target, sxfer, scntl3, 1);
+    sprintf (buf, "scsi%d : setting target %d to ", host->host_no, target);
+    print_synchronous (buf, msg);
 }
 
 /* 
@@ -1664,9 +2271,11 @@
  *
  */
 
-static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct 
+static int 
+NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct 
     NCR53c7x0_cmd *cmd) {
     NCR53c7x0_local_declare();
+    int print;
     Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;		
@@ -1680,11 +2289,7 @@
 
     switch (dsps) {
     case A_int_msg_1:
-	printk ("scsi%d : message", host->host_no);
-	if (cmd) 
-	    printk (" from target %d lun %d", c->target, c->lun);
-	print_msg ((unsigned char *) hostdata->msg_buf);
-	printk("\n");
+	print = 1;
 	switch (hostdata->msg_buf[0]) {
 	/* 
 	 * Unless we've initiated synchronous negotiation, I don't
@@ -1694,20 +2299,56 @@
 	    hostdata->dsp = hostdata->script + hostdata->E_accept_message /
 		sizeof(long);
 	    hostdata->dsp_changed = 1;
+	    if (cmd && (cmd->flags & CMD_FLAG_SDTR)) {
+		printk ("scsi%d : target %d rejected SDTR\n", host->host_no, 
+		    c->target);
+		cmd->flags &= ~CMD_FLAG_SDTR;
+		asynchronous (host, c->target);
+		print = 0;
+	    } 
 	    break;
 	case INITIATE_RECOVERY:
 	    printk ("scsi%d : extended contingent allegiance not supported yet, rejecting\n",
 		host->host_no);
+	    /* Fall through to default */
+	    hostdata->dsp = hostdata->script + hostdata->E_reject_message /
+		sizeof(long);
+	    hostdata->dsp_changed = 1;
+	    break;
+	default:
+	    printk ("scsi%d : unsupported message, resjecting\n",
+		host->host_no);
 	    hostdata->dsp = hostdata->script + hostdata->E_reject_message /
 		sizeof(long);
 	    hostdata->dsp_changed = 1;
 	}
+	if (print) {
+	    printk ("scsi%d : received message", host->host_no);
+	    if (c) 
+	    	printk (" from target %d lun %d ", c->target, c->lun);
+	    print_msg ((unsigned char *) hostdata->msg_buf);
+	    printk("\n");
+	}
+	
 	return SPECIFIC_INT_NOTHING;
+
+
     case A_int_msg_sdtr:
+/*
+ * At this point, hostdata->msg_buf contains
+ * 0 EXTENDED MESSAGE
+ * 1 length 
+ * 2 SDTR
+ * 3 period * 4ns
+ * 4 offset
+ */
+
 	if (cmd) {
-	    printk ("scsi%d : target %d %s synchronous transfer period %dns, offset%d\n",
-		host->host_no, c->target, (cmd->flags & CMD_FLAG_SDTR) ? "accepting" :
-		"requesting", hostdata->msg_buf[3] * 4, hostdata->msg_buf[4]);
+	    char buf[80];
+	    sprintf (buf, "scsi%d : target %d %s ", host->host_no, c->target,
+		(cmd->flags & CMD_FLAG_SDTR) ? "accepting" : "requesting");
+	    print_synchronous (buf, (unsigned char *) hostdata->msg_buf);
+
 	/* 
 	 * Initiator initiated, won't happen unless synchronous 
 	 * 	transfers are enabled.  If we get a SDTR message in
@@ -1718,8 +2359,11 @@
    	 */
 	    if (cmd->flags & CMD_FLAG_SDTR) {
 		cmd->flags &= ~CMD_FLAG_SDTR; 
-		synchronous (host, c->target, (unsigned char *) 
-		    hostdata->msg_buf);
+		if (hostdata->msg_buf[4]) 
+		    synchronous (host, c->target, (unsigned char *) 
+		    	hostdata->msg_buf);
+		else 
+		    asynchronous (host, c->target);
 		hostdata->dsp = hostdata->script + hostdata->E_accept_message /
 		    sizeof(long);
 		hostdata->dsp_changed = 1;
@@ -1727,37 +2371,23 @@
 	    } else {
 		if (hostdata->options & OPTION_SYNCHRONOUS)  {
 		    cmd->flags |= CMD_FLAG_DID_SDTR;
-		    synchronous (host, c->target, (unsigned char *)
-		    	hostdata->msg_buf);
+		    synchronous (host, c->target, (unsigned char *) 
+			hostdata->msg_buf);
 		} else {
 		    hostdata->msg_buf[4] = 0;		/* 0 offset = async */
+		    asynchronous (host, c->target);
 		}
-
 		patch_dsa_32 (cmd->dsa, dsa_msgout_other, 0, 5);
-		patch_dsa_32 (cmd->dsa, dsa_msgout_other, 1, 
+		patch_dsa_32 (cmd->dsa, dsa_msgout_other, 1, (long) 
 		    hostdata->msg_buf);
 		hostdata->dsp = hostdata->script + 
-		hostdata->E_respond_message / sizeof(long);
+		    hostdata->E_respond_message / sizeof(long);
 		hostdata->dsp_changed = 1;
 	    }
-
-	    if (hostdata->msg_buf[4]) {
-		int Hz = 1000000000 / (hostdata->msg_buf[3] * 4);
-		printk ("scsi%d : setting target %d to %d.%02dMhz %s SCSI%s\n"
-			"         period = %dns, max offset = %d\n",
-			host->host_no, c->target, Hz / 1000000, Hz % 1000000,
-			((hostdata->msg_buf[3] < 200) ? "FAST " : 
-			"synchronous") ,
-			((hostdata->msg_buf[3] < 200) ? "-II" : ""),
-			(int) hostdata->msg_buf[3] * 4, (int) 
-			hostdata->msg_buf[4]);
-	    } else {
-		printk ("scsi%d : setting target %d to asynchronous SCSI\n",
-		    host->host_no, c->target);
-	    }
 	    return SPECIFIC_INT_NOTHING;
 	}
-	/* Fall through to abort */
+	/* Fall through to abort if we couldn't find a cmd, and 
+	   therefore a dsa structure to twiddle */
     case A_int_msg_wdtr:
 	hostdata->dsp = hostdata->script + hostdata->E_reject_message /
 	    sizeof(long);
@@ -1769,14 +2399,15 @@
 	return SPECIFIC_INT_ABORT;
     case A_int_err_selected:
 	printk ("scsi%d : selected by target %d\n", host->host_no,
-	    (int) NCR53c7x0_read8(SSID_REG_800) &7);
+	    (int) NCR53c7x0_read8(SDID_REG_800) &7);
 	hostdata->dsp = hostdata->script + hostdata->E_target_abort / 
     	    sizeof(long);
 	hostdata->dsp_changed = 1;
 	return SPECIFIC_INT_NOTHING;
     case A_int_err_unexpected_reselect:
-	printk ("scsi%d : unexpected reselect by target %d\n", host->host_no,
-	    (int) NCR53c7x0_read8(SSID_REG_800));
+	printk ("scsi%d : unexpected reselect by target %d lun %d\n", 
+	    host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & 7,
+	    hostdata->reselected_identify & 7);
 	hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
     	    sizeof(long);
 	hostdata->dsp_changed = 1;
@@ -1801,19 +2432,12 @@
 	    return SPECIFIC_INT_PANIC;
 	}
 
-/*
- * When a contingent allegiance condition is created, the target 
- * reverts to asynchronous transfers.
- */
-
-	asynchronous (host, c->target);
-
 	/* 
-	 * Use normal one-byte selection message, with no attempts to 
-    	 * reestablish synchronous or wide messages since this may
-    	 * be the crux of our problem.
+	 * FIXME : this uses the normal one-byte selection message.
+	 * 	We may want to renegotiate for synchronous & WIDE transfers
+	 * 	since these could be the crux of our problem.
 	 *
-	 * XXX - once SCSI-II tagged queuing is implemented, we'll
+	 * FIXME : once SCSI-II tagged queuing is implemented, we'll
 	 * 	have to set this up so that the rest of the DSA
 	 *	agrees with this being an untagged queue'd command.
 	 */
@@ -1883,6 +2507,85 @@
 	    printk("scsi%d : test%ld complete\n", host->host_no,
 		hostdata->test_completed);
 	return SPECIFIC_INT_NOTHING;
+#ifdef A_int_debug_reselected_ok
+    case A_int_debug_reselected_ok:
+	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
+    	    	OPTION_DEBUG_DISCONNECT)) {
+	    /* 
+	     * Note - this dsa is not based on location relative to 
+	     * the command structure, but to location relative to the 
+	     * DSA register 
+	     */	
+	    unsigned long *dsa;
+	    dsa = (unsigned long *) NCR53c7x0_read32(DSA_REG);
+
+	    printk("scsi%d : reselected_ok (DSA = 0x%x)\n", 
+		host->host_no, (unsigned) dsa);
+	    printk("scsi%d : resume address is 0x%lx\n",
+		    host->host_no, (unsigned long) cmd->saved_data_pointer);
+	    print_insn (host, hostdata->script + Ent_reselected_ok / 
+    	    	    sizeof(long), "", 1);
+    	    printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
+		host->host_no, NCR53c7x0_read8(SXFER_REG),
+		NCR53c7x0_read8(SCNTL3_REG_800));
+	    if (c) {
+		print_insn (host, (unsigned long *) 
+		    hostdata->sync[c->target].script, "", 1);
+		print_insn (host, (unsigned long *) 
+		    hostdata->sync[c->target].script + 2, "", 1);
+	    }
+	}
+    	return SPECIFIC_INT_RESTART;
+#endif
+#ifdef A_int_debug_reselect_check
+    case A_int_debug_reselect_check:
+	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
+	    unsigned long *dsa;
+#if 0
+	    unsigned long *code;
+#endif
+	    /* 
+	     * Note - this dsa is not based on location relative to 
+	     * the command structure, but to location relative to the 
+	     * DSA register 
+	     */	
+	    dsa = (unsigned long *) NCR53c7x0_read32(DSA_REG);
+	    printk("scsi%d : reselected_check_next (DSA = 0x%x)\n", 
+		host->host_no, (unsigned) dsa);
+	    if (dsa) {
+		printk("scsi%d : resume address is 0x%lx\n",
+		    host->host_no, (unsigned long) cmd->saved_data_pointer);
+#if 0
+		printk("scsi%d : template code :\n", host->host_no);
+		for (code = dsa + (Ent_dsa_code_check_reselect - Ent_dsa_zero) 
+		    / 4; code < (dsa + Ent_dsa_zero / 4); code +=
+		    print_insn (host, code, "", 1));
+#endif
+	    }
+	    print_insn (host, hostdata->script + Ent_reselected_ok / 
+    	    	    sizeof(long), "", 1);
+	}
+    	return SPECIFIC_INT_RESTART;
+#endif
+#ifdef A_int_debug_dsa_schedule
+    case A_int_debug_dsa_schedule:
+	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
+	    unsigned long *dsa;
+	    /* 
+	     * Note - this dsa is not based on location relative to 
+	     * the command structure, but to location relative to the 
+	     * DSA register 
+	     */	
+	    dsa = (unsigned long *) NCR53c7x0_read32(DSA_REG);
+	    printk("scsi%d : dsa_schedule (old DSA = 0x%x)\n", 
+		host->host_no, (unsigned) dsa);
+	    if (dsa) 
+		printk("scsi%d : resume address is 0x%lx (temp was 0x%lx)\n",
+		    host->host_no, (unsigned long) cmd->saved_data_pointer,
+		    NCR53c7x0_read32(TEMP_REG));
+	}
+    	return SPECIFIC_INT_RESTART;
+#endif
 #ifdef A_int_debug_scheduled
     case A_int_debug_scheduled:
 	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
@@ -1903,7 +2606,7 @@
 	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
 	    printk("scsi%d : command sent\n");
 	}
-    return SPECIFIC_INT_RESTART;
+    	return SPECIFIC_INT_RESTART;
 #endif
 #ifdef A_int_debug_dsa_loaded
     case A_int_debug_dsa_loaded:
@@ -1915,12 +2618,14 @@
 #endif
 #ifdef A_int_debug_reselected
     case A_int_debug_reselected:
-	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
+	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
+	    OPTION_DEBUG_DISCONNECT)) {
 	    printk("scsi%d : reselected by target %d lun %d\n",
-		host->host_no, (int) NCR53c7x0_read8(SSID_REG_800), 
+		host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & ~0x80, 
 		(int) hostdata->reselected_identify & 7);
+	    print_queues(host);
 	}
-    return SPECIFIC_INT_RESTART;
+    	return SPECIFIC_INT_RESTART;
 #endif
 #ifdef A_int_debug_head
     case A_int_debug_head:
@@ -1928,12 +2633,173 @@
 	    printk("scsi%d : issue_dsa_head now 0x%lx\n",
 		host->host_no, (unsigned long) hostdata->issue_dsa_head);
 	}
-    return SPECIFIC_INT_RESTART;
+    	return SPECIFIC_INT_RESTART;
+#endif
+#ifdef A_int_debug_disconnect_msg
+    case A_int_debug_disconnect_msg:
+	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
+	    if (c)
+		printk("scsi%d : target %d lun %d disconnecting\n", 
+		    host->host_no, c->target, c->lun);
+	    else
+		printk("scsi%d : unknown target disconnecting\n",
+		    host->host_no);
+	}
+	return SPECIFIC_INT_RESTART;
+#endif
+#ifdef A_int_debug_disconnected
+    case A_int_debug_disconnected:
+	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
+		OPTION_DEBUG_DISCONNECT)) {
+	    printk ("scsi%d : disconnected, new queues are\n", 
+		host->host_no);
+	    print_queues(host);
+#if 0
+    	    printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
+		host->host_no, NCR53c7x0_read8(SXFER_REG),
+		NCR53c7x0_read8(SCNTL3_REG_800));
+#endif
+	    if (c) {
+		print_insn (host, (unsigned long *) 
+		    hostdata->sync[c->target].script, "", 1);
+		print_insn (host, (unsigned long *) 
+		    hostdata->sync[c->target].script + 2, "", 1);
+	    }
+	}
+	return SPECIFIC_INT_RESTART;
+#endif
+#ifdef A_int_debug_panic
+    case A_int_debug_panic:
+	panic("scsi%d : int_debug_panic received\n", host->host_no);
+#endif
+#ifdef A_int_debug_saved
+    case A_int_debug_saved:
+    	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
+    	    OPTION_DEBUG_DISCONNECT)) {
+    	    printk ("scsi%d : saved data pointer 0x%lx\n",
+    	    	host->host_no, (unsigned long) cmd->saved_data_pointer);
+    	    print_progress (c);
+    	}
+    	return SPECIFIC_INT_RESTART;
+#endif
+#ifdef A_int_debug_restored
+    case A_int_debug_restored:
+    	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
+    	    OPTION_DEBUG_DISCONNECT)) {
+    	    if (cmd) {
+		int size;
+    	    	printk ("scsi%d : restored data pointer 0x%lx\n",
+    	    	    host->host_no, (unsigned long) cmd->saved_data_pointer);
+		size = print_insn (host, (unsigned long *) 
+		    cmd->saved_data_pointer, "", 1);
+		size = print_insn (host, (unsigned long *) 
+		    cmd->saved_data_pointer + size, "", 1);
+    	    	print_progress (c);
+	    }
+#if 0
+	    printk ("scsi%d : datapath residual %d\n",
+		host->host_no, datapath_residual (host)) ;
+#endif
+    	}
+    	return SPECIFIC_INT_RESTART;
+#endif
+#ifdef A_int_debug_sync
+    case A_int_debug_sync:
+    	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
+    	    OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
+	    unsigned char sxfer = NCR53c7x0_read8 (SXFER_REG),
+		scntl3 = NCR53c7x0_read8 (SCNTL3_REG_800);
+	    if (c) {
+		if (sxfer != hostdata->sync[c->target].sxfer_sanity ||
+		    scntl3 != hostdata->sync[c->target].scntl3_sanity) {
+		   	printk ("scsi%d :  sync sanity check failed sxfer=0x%x, scntl3=0x%x",
+			    host->host_no, sxfer, scntl3);
+			NCR53c7x0_write8 (SXFER_REG, sxfer);
+			NCR53c7x0_write8 (SCNTL3_REG_800, scntl3);
+		    }
+	    } else 
+    	    	printk ("scsi%d : unknown command sxfer=0x%x, scntl3=0x%x\n",
+		    host->host_no, (int) sxfer, (int) scntl3);
+	}
+    	return SPECIFIC_INT_RESTART;
+#endif
+#ifdef A_int_debug_datain
+	case A_int_debug_datain:
+	    if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
+		OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
+		int size;
+		printk ("scsi%d : In do_datain (%s) sxfer=0x%x, scntl3=0x%x\n"
+			"         datapath residual=%d\n",
+		    host->host_no, sbcl_to_phase (NCR53c7x0_read8 (SBCL_REG)),
+		    (int) NCR53c7x0_read8(SXFER_REG), 
+		    (int) NCR53c7x0_read8(SCNTL3_REG_800),
+		    datapath_residual (host)) ;
+		print_insn (host, (unsigned long *) dsp, "", 1);
+		size = print_insn (host, (unsigned long *) dsp[1], "", 1);
+		print_insn (host, (unsigned long *) dsp[1] + size, "", 1);
+	   } 
+	return SPECIFIC_INT_RESTART;
+#endif
+/* 
+ * FIXME : this needs to change to support WIDE targets.
+ * FIXME : for 7xx support, we need to read SDID_REG_700 and handle 
+ *	the comparison as bitfielded,  not binary.
+ */
+#ifdef A_int_debug_check_dsa
+	case A_int_debug_check_dsa:
+	    if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
+		int sdid = NCR53c7x0_read8 (SDID_REG_800) & 7;
+		char *where = ((unsigned long) dsp - 
+		    NCR53c7x0_insn_size(NCR53c7x0_read8 (DCMD_REG)) ==
+		    	(unsigned long) hostdata->script + 
+		    	Ent_select_check_dsa) ?
+		    "selection" : "reselection";
+		if (c && sdid != c->target) {
+		    printk ("scsi%d : SDID target %d != DSA target %d at %s\n",
+			host->host_no, sdid, c->target, where);
+		    print_lots(host);
+		    dump_events (host, 20);
+		    return SPECIFIC_INT_PANIC;
+		}
+	    }
+	    return SPECIFIC_INT_RESTART;
 #endif
     default:
 	if ((dsps & 0xff000000) == 0x03000000) {
 	     printk ("scsi%d : misc debug interrupt 0x%lx\n",
-		host->host_no, dsps);
+		host->host_no, (unsigned long) dsps);
+	    return SPECIFIC_INT_RESTART;
+	} else if ((dsps & 0xff000000) == 0x05000000) {
+	    if (hostdata->events) {
+		struct NCR53c7x0_event *event;
+		++hostdata->event_index;
+		if (hostdata->event_index >= hostdata->event_size)
+		    hostdata->event_index = 0;
+		event = (struct NCR53c7x0_event *) hostdata->events + 
+		    hostdata->event_index;
+		event->event = (enum ncr_event) dsps;
+		event->dsa = (unsigned long *) NCR53c7x0_read32(DSA_REG);
+	    /* FIXME : this needs to change for the '7xx family */
+		if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) 
+			event->target = NCR53c7x0_read8(SSID_REG_800);
+		else 
+			event->target = 255;
+
+		if (event->event == EVENT_RESELECT)
+		    event->lun = hostdata->reselected_identify & 0xf;
+		else if (c)
+		    event->lun = c->lun;
+		else
+		    event->lun = 255;
+		do_gettimeofday(&(event->time));
+		if (c) {
+		    event->pid = c->pid;
+		    memcpy ((void *) event->cmnd, (void *) c->cmnd, 
+			sizeof (event->cmnd));
+		} else {
+		    event->pid = -1;
+		}
+	    }
 	    return SPECIFIC_INT_RESTART;
 	}
 
@@ -2030,7 +2896,8 @@
 }
 
 
-static int debugger_fn_bl (struct Scsi_Host *host, struct debugger_token *token,
+static int 
+debugger_fn_bl (struct Scsi_Host *host, struct debugger_token *token,
     unsigned long args[]) {
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;
@@ -2068,7 +2935,8 @@
     return 0;
 }
 
-static int debugger_fn_bs (struct Scsi_Host *host, struct debugger_token *token,
+static int 
+debugger_fn_bs (struct Scsi_Host *host, struct debugger_token *token,
     unsigned long args[]) {
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;
@@ -2118,7 +2986,7 @@
 
 #define NDT sizeof(debugger_tokens / sizeof(struct debugger_token))
 
-static struct Scsi_Host * inode_to_host (struct inode *inode) {$
+static struct Scsi_Host * inode_to_host (struct inode *inode) {
     int dev;
     struct Scsi_Host *tmp;
     for (dev = MINOR(inode->rdev), host = first_host;
@@ -2128,7 +2996,8 @@
 }
 
 
-static debugger_user_write (struct inode *inode,struct file *filp,
+static int
+debugger_user_write (struct inode *inode,struct file *filp,
     char *buf,int count) {
     struct Scsi_Host *host;			/* This SCSI host */
     struct NCR53c7x0_hostadata *hostdata;	
@@ -2180,13 +3049,15 @@
     return count;
 } 
 
-static debugger_user_read (struct inode *inode,struct file *filp,
+static int 
+debugger_user_read (struct inode *inode,struct file *filp,
     char *buf,int count) {
     struct Scsi_Host *instance;
     
 }
 
-static debugger_kernel_write (struct Scsi_Host *host, char *buf, size_t
+static int 
+debugger_kernel_write (struct Scsi_Host *host, char *buf, size_t
     buflen) {
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;
@@ -2247,7 +3118,7 @@
 
 
     /* 
-     * Respond to reselection by targets and use our _initiator_ SCSI ID 
+     * Respond to reselection by targets and use our _initiator_ SCSI ID  
      * for arbitration. If notyet, also respond to SCSI selection.
      *
      * XXX - Note : we must reprogram this when reselecting as 
@@ -2266,14 +3137,21 @@
      * and SCSI recommended .5s selection timeout.
      */
 
+#if 0
     NCR53c7x0_write8(STIME0_REG_800, 
-    	((14 << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK) 
+    	((selection_timeout << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK) 
+	| ((15 << STIME0_800_HTH_SHIFT) & STIME0_800_HTH_MASK));
+#else
 /* Disable HTH interrupt */
-#if 0
-	| ((15 << STIME0_800_HTH_SHIFT) & STIME0_800_HTH_MASK)
+    NCR53c7x0_write8(STIME0_REG_800, 
+    	((selection_timeout << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK));
 #endif
-    );
 
+    /*
+     * Enable active negation for happy synchronous transfers.
+     */
+
+    NCR53c7x0_write8(STEST3_REG_800, STEST3_800_TE);
 
 
     /*
@@ -2284,9 +3162,15 @@
     NCR53c7x0_write8(DIEN_REG, DIEN_800_MDPE | DIEN_800_BF |
 		DIEN_ABRT | DIEN_SSI | DIEN_SIR | DIEN_800_IID);
 
-    
+#if 0    
+    NCR53c7x0_write8(SIEN0_REG_800, ((hostdata->options & OPTION_PARITY) ?
+	    SIEN_PAR : 0) | SIEN_RST | SIEN_UDC | SIEN_SGE | SIEN_MA 
+	    |SIEN_800_RESEL);
+#else
     NCR53c7x0_write8(SIEN0_REG_800, ((hostdata->options & OPTION_PARITY) ?
 	    SIEN_PAR : 0) | SIEN_RST | SIEN_UDC | SIEN_SGE | SIEN_MA);
+#endif
+
     NCR53c7x0_write8(SIEN1_REG_800, SIEN1_800_STO | SIEN1_800_HTH);
 
     /* 
@@ -2303,87 +3187,108 @@
     
 }
 
+
 /*
- * Function static struct NCR53c7x0_cmd *create_cmd (Scsi_Cmnd *cmd) 
- *
+ * Function static struct NCR53c7x0_cmd *allocate_cmd (Scsi_Cmnd *cmd)
+ * 
  * Purpose : If we have not already allocated enough NCR53c7x0_cmd
  *	structures to satisfy any allowable number of simultaneous 
  *	commands for this host; do so (using either scsi_malloc()
  *	or kmalloc() depending on configuration), and add them to the 
- *	hostdata free list.  Take the first structure off the free list, 
- *	initialize it based on the Scsi_Cmnd structure passed in, 
- *	including dsa and Linux field initialization, and dsa code relocation.
+ *	free list.
  *
- * Inputs : cmd - SCSI command
+ *	Return the first free NCR53c7x0_cmd structure (which are 
+ * 	reused in a LIFO maner to minimize cache thrashing).
+ * 
  *
- * Returns : NCR53c7x0_cmd structure corresponding to cmd,
+ * Inputs : cmd - SCSI command;  mayMalloc - whether new NCR53c7x0_cmd
+ *          structures may be allocated.
+ *
+ * Returns : NCR53c7x0_cmd structure allocated on behalf of cmd;
  *	NULL on failure.
  */
 
 static struct NCR53c7x0_cmd *
-create_cmd (Scsi_Cmnd *cmd) {
-    NCR53c7x0_local_declare();
+allocate_cmd (Scsi_Cmnd *cmd, int mayMalloc) {
     struct Scsi_Host *host = cmd->host;
-    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
-        host->hostdata;	
-    struct NCR53c7x0_cmd *tmp = NULL; 	/* NCR53c7x0_cmd structure for this command */
-    int datain,  		/* Number of instructions per phase */
-	dataout;
-    int data_transfer_instructions, /* Count of dynamic instructions */
-    	i;			/* Counter */
-    unsigned long *cmd_datain,	/* Address of datain/dataout code */
-	*cmd_dataout;		/* Incremented as we assemble */
-#ifdef notyet
+    struct NCR53c7x0_hostdata *hostdata = 
+	(struct NCR53c7x0_hostdata *) host->hostdata;
     void *real;			/* Real address */
     int size;			/* Size of *tmp */
     int	alignment;		/* Alignment adjustment (0 - 4) */
-#endif
+    int i;
+    struct NCR53c7x0_cmd *tmp = NULL;
     unsigned long flags;
-    NCR53c7x0_local_setup(cmd->host);
 
-/* FIXME : when we start doing multiple simultaneous commands per LUN, 
-   we will need to either
-    	- Do an attach_slave() and detach_slave() the right way (allocate
-    	  memory in attach_slave() as we do in scsi_register).
-    	- Make sure this code works
-    with the former being cleaner.  At the same time, we can also go with
-    a per-device host_scribble, and introduce a NCR53c7x0_device structure
-    to replace the messy fixed length arrays we're starting to use. */
-
-#ifdef notyet
+    if (hostdata->options & OPTION_DEBUG_ALLOCATION)
+	printk ("scsi%d : num_cmds = %d, can_queue = %d, in_scan_scsis = %d\n"
+		"         target = %d, lun = %d, %s\n",
+	    host->host_no, hostdata->num_cmds, host->can_queue, in_scan_scsis,
+	    cmd->target, cmd->lun, (hostdata->cmd_allocated[cmd->target] &
+		(1 << cmd->lun)) ? "allready allocated" : "not allocated");
+	    
 
-    if (hostdata->num_commands < host->can_queue &&
+    if (hostdata->num_cmds < host->can_queue &&
     	!in_scan_scsis && 
-    	!(hostdata->cmd_allocated[cmd->target] & (1 << cmd->lun))) {
-    	for (i = host->hostt->cmd_per_lun - 1; i >= 0  --i) {
-#ifdef SCSI_MALLOC
+	mayMalloc &&
+	(hostdata->extra_allocate ||
+    	    !(hostdata->cmd_allocated[cmd->target] & (1 << cmd->lun)))) {
+	
+    	for (i = !(hostdata->cmd_allocated[cmd->target] & (1 << cmd->lun)) ?
+		(host->hostt->cmd_per_lun - 1) : -1;
+	    (hostdata->extra_allocate || i >= 0) && hostdata->num_cmds 
+		< host->can_queue ;
+	    (hostdata->extra_allocate ? --hostdata->extra_allocate : --i), 
+		++hostdata->num_cmds) {
+
+	    if (!hostdata->extra_allocate) 
+		hostdata->cmd_allocated[cmd->target] |= 1 << cmd->lun;
+
+	    if (hostdata->options & OPTION_SCSI_MALLOC) {
     /* scsi_malloc must allocate with a 512 byte granularity, but always
        returns buffers which are aligned on a 512 boundary */
-	    size = (hostdata->max_cmd_size + 511) / 512 * 512;
-	    tmp = (struct NCR53c7x0_cmd *) scsi_malloc (size);
-	    if (!tmp)
-		break;
-	    tmp->real = (void *) tmp; 
-#else
+	    	size = (hostdata->max_cmd_size + 511) / 512 * 512;
+	    	tmp = (struct NCR53c7x0_cmd *) scsi_malloc (size);
+	    	if (!tmp) {
+		    if (hostdata->options & OPTION_DEBUG_ALLOCATION)
+			printk ("scsi%d : scsi_malloc(%d) failed\n",
+			    host->host_no, size);
+		    break;
+		}
+	    	tmp->real = (void *) tmp; 
+		tmp->free = ((void (*)(void *, int)) scsi_free); 
+	    } else {
     /* kmalloc() can allocate any size, but historically has returned 
        unaligned addresses, so we need to allow for alignment */
-	    size = hostdata->max_cmd_size + 4;
-	    real = kmalloc (size, GFP_ATOMIC);
-	    alignment = 4 - (((unsigned) real) & 3);
-	    tmp = (struct NCR53c7x0_cmd *) (((char *) real) + alignment);
-	    if (!tmp)
-		break;
-	    tmp->real = real;
-#endif /* def SCSI_MALLOC */
-    	    tmp->size = size;			
-	    /* Insert all but last into list */
+		size = hostdata->max_cmd_size + 4;
+		real = kmalloc (size, GFP_ATOMIC);
+		if (!real) {
+		    if (hostdata->options & OPTION_DEBUG_ALLOCATION)
+			printk ("scsi%d : kmalloc(%d) failed\n",
+			    host->host_no, size);
+		    break;
+		}
+		alignment = 4 - (((unsigned) real) & 3);
+		tmp = (struct NCR53c7x0_cmd *) (((char *) real) + alignment);
+		tmp->real = real;
+		tmp->size = size;			
+		tmp->free = ((void (*)(void *, int)) kfree_s); 
+	    }
+    /* Keep the last one out of the free list so we can use it now */
 	    if (i > 0) {
+		save_flags (flags);
+		cli();
 		tmp->next = hostdata->free;
 		hostdata->free = tmp;
+		restore_flags (flags);
 	    }
 	}
+    /* If for some reason, our allocation attempt failed here, we'll
+	try again later */
+	if (i >= 0) 
+	    hostdata->extra_allocate += i + 1;
     }
-#endif /* def notyet */
+
     if (!tmp) {
     	save_flags(flags);
     	cli();
@@ -2391,17 +3296,54 @@
 	if (tmp) {
     	    hostdata->free = tmp->next;
 	    restore_flags(flags);
-    	} else {
-    	    restore_flags(flags);
-    	    return NULL;
     	}
+	restore_flags(flags);
     }
+    if (!tmp && mayMalloc)
+	printk ("scsi%d : can't allocate command for target %d lun %d\n",
+	    host->host_no, cmd->target, cmd->lun);
+    return tmp;
+}
 
 
-    /*
-     * Decide whether we need to generate commands for DATA IN,
-     * DATA OUT, neither, or both based on the SCSI command 
-     */
+/*
+ * Function static struct NCR53c7x0_cmd *create_cmd (Scsi_Cmnd *cmd) 
+ *
+ *
+ * Purpose : allocate a NCR53c7x0_cmd structure, initialize it based on the 
+ * 	Scsi_Cmnd structure passed in cmd, including dsa and Linux field 
+ * 	initialization, and dsa code relocation.
+ *
+ * Inputs : cmd - SCSI command
+ *
+ * Returns : NCR53c7x0_cmd structure corresponding to cmd,
+ *	NULL on failure.
+ */
+
+static struct NCR53c7x0_cmd *
+create_cmd (Scsi_Cmnd *cmd, int mayMalloc) {
+    NCR53c7x0_local_declare();
+    struct Scsi_Host *host = cmd->host;
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+        host->hostdata;	
+    struct NCR53c7x0_cmd *tmp; 	/* NCR53c7x0_cmd structure for this command */
+    int datain,  		/* Number of instructions per phase */
+	dataout;
+    int data_transfer_instructions, /* Count of dynamic instructions */
+    	i;			/* Counter */
+    unsigned long *cmd_datain,	/* Address of datain/dataout code */
+	*cmd_dataout;		/* Incremented as we assemble */
+    unsigned long flags;
+    NCR53c7x0_local_setup(cmd->host);
+
+    if (!(tmp = allocate_cmd (cmd, mayMalloc)))
+	return NULL;
+
+
+    /*
+     * Decide whether we need to generate commands for DATA IN,
+     * DATA OUT, neither, or both based on the SCSI command 
+     */
 
     switch (cmd->cmnd[0]) {
     /* These commands do DATA IN */
@@ -2448,10 +3390,26 @@
 	datain = dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
     }
 
-    /* 
-     * For each data phase implemented, we need a JUMP instruction
-     * to return control to other_transfer.  We also need a MOVE
-     * and a CALL instruction for each scatter/gather segment.
+    /*
+     * New code : so that active pointers work correctly irregardless
+     * 	of where the saved data pointer is at, we want to immediately
+     * 	enter the dynamic code after selection, and on a non-data
+     * 	phase perform a CALL to the non-data phase handler, with
+     * 	returns back to this address.
+     *
+     * 	If a phase mismatch is encountered in the middle of a 
+     * 	Block MOVE instruction, we want to _leave_ that instruction
+     *	unchanged as the current case is, modify a temporary buffer,
+     *	and point the active pointer (TEMP) at that.
+     *
+     * 	Furthurmore, we want to implement a saved data pointer, 
+     * 	set by the SAVE_DATA_POINTERs message.
+     *
+     * 	So, the data transfer segments will change to 
+     *		CALL data_transfer, WHEN NOT data phase
+     *		MOVE x, x, WHEN data phase
+     *		( repeat )
+     *		JUMP other_transfer
      */
 
     data_transfer_instructions = datain + dataout;
@@ -2467,12 +3425,23 @@
 
 
     /*
+     * The saved data pointer is set up so that a RESTORE POINTERS message 
+     * will start the data transfer over at the beggining.
+     */
+
+    tmp->saved_data_pointer = hostdata->script + hostdata->E_data_transfer /
+    	sizeof(unsigned long);
+
+    /*
      * Initialize Linux specific fields.
      */
 
     tmp->cmd = cmd;
     tmp->next = NULL;
     tmp->prev = NULL;
+    tmp->flags = 0;
+    tmp->dsa_next = (unsigned long **)(tmp->dsa + (hostdata->dsa_next - 
+	hostdata->dsa_start) / sizeof(long));
 
     /* 
      * Calculate addresses of dynamic code to fill in DSA
@@ -2500,18 +3469,52 @@
 
     patch_dsa_32(tmp->dsa, dsa_next, 0, NULL);
     patch_dsa_32(tmp->dsa, dsa_cmnd, 0, cmd);
+
+    if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS) 
+	if (hostdata->sync[cmd->target].select_indirect != 
+	    ((hostdata->sync[cmd->target].scntl3_sanity << 24) | 
+		(cmd->target << 16) | 
+		(hostdata->sync[cmd->target].sxfer_sanity << 8))) {
+	    printk ("scsi%d :  sanity check failed select_indirect=0x%lx\n",
+		host->host_no, hostdata->sync[cmd->target].select_indirect);
+	    FATAL(host);
+
+	}
+	
+
+		
+
     patch_dsa_32(tmp->dsa, dsa_select, 0, hostdata->sync[cmd->target].
     	select_indirect);
     /*
      * XXX - we need to figure this size based on whether
      * or not we'll be using any additional messages.
      */
-    patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1);
-#if 0
-    tmp->select[0] = IDENTIFY (1, cmd->lun);
-#else
-    tmp->select[0] = IDENTIFY (0, cmd->lun);
+    if (hostdata->initiate_sdtr & (1 << cmd->target)) {
+	memcpy ((void *) (tmp->select + 1), (void *) sdtr_message, 
+	    sizeof(sdtr_message));
+    	patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1 + sizeof(sdtr_message));
+	tmp->flags |= CMD_FLAG_SDTR;
+	save_flags(flags);
+	cli();
+	hostdata->initiate_sdtr &= ~(1 << cmd->target);
+	restore_flags(flags);
+    
+    }
+#if 1
+    else if (!(hostdata->talked_to & (1 << cmd->target)) && 
+		!(hostdata->options & OPTION_NO_ASYNC)) {
+	memcpy ((void *) (tmp->select + 1), (void *) async_message, 
+	    sizeof(async_message));
+    	patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1 + sizeof(async_message));
+	tmp->flags |= CMD_FLAG_SDTR;
+    } 
 #endif
+    else 
+    	patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1);
+    hostdata->talked_to |= (1 << cmd->target);
+    tmp->select[0] = (hostdata->options & OPTION_DISCONNECT) ? 
+	IDENTIFY (1, cmd->lun) : IDENTIFY (0, cmd->lun);
     patch_dsa_32(tmp->dsa, dsa_msgout, 1, tmp->select);
     patch_dsa_32(tmp->dsa, dsa_cmdout, 0, cmd->cmd_len);
     patch_dsa_32(tmp->dsa, dsa_cmdout, 1, cmd->cmnd);
@@ -2531,21 +3534,23 @@
     patch_dsa_32(tmp->dsa, dsa_status, 1, &cmd->result);
     patch_dsa_32(tmp->dsa, dsa_msgout_other, 0, 1);
     patch_dsa_32(tmp->dsa, dsa_msgout_other, 1, 
-    	&(hostdata->NCR53c7xx_msg_nop));
-
+	&(hostdata->NCR53c7xx_msg_nop));
     
     /*
      * Generate code for zero or more of the DATA IN, DATA OUT phases 
      * in the format 
      *
+     * CALL data_transfer, WHEN NOT phase
      * MOVE first buffer length, first buffer address, WHEN phase
-     * CALL msgin, WHEN MSG_IN 
      * ...
      * MOVE last buffer length, last buffer address, WHEN phase
      * JUMP other_transfer
      */
 
-/* See if we're getting to data transfer */
+/* 
+ * See if we're getting to data transfer by generating an unconditional 
+ * interrupt.
+ */
 #if 0
     if (datain) {
 	cmd_datain[0] = 0x98080000;
@@ -2579,28 +3584,31 @@
 	    cmd->request_bufflen);
 
 	if (datain) {
-	    cmd_datain[0] = ((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I | DCMD_BMI_IO) 
+	    /* CALL other_in, WHEN NOT DATA_IN */  
+	    cmd_datain[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL | 
+		DCMD_TCI_IO) << 24) | 
+		DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE;
+	    cmd_datain[1] = (unsigned long) (hostdata->script + 
+    	    	hostdata->E_other_in / sizeof(long));
+	    /* MOVE count, buf, WHEN DATA_IN */
+	    cmd_datain[2] = ((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I | DCMD_BMI_IO) 
     	    	<< 24) | count;
-	    cmd_datain[1] = buf;
-	    cmd_datain[2] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL | 
-		DCMD_TCI_CD | DCMD_TCI_IO | DCMD_TCI_MSG) << 24) | 
-		DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE | DBC_TCI_TRUE;
-	    cmd_datain[3] = (unsigned long) hostdata->script + 
-    	    	hostdata->E_msg_in;
+	    cmd_datain[3] = buf;
 #if 0
 	    print_insn (host, cmd_datain, "dynamic ", 1);
 	    print_insn (host, cmd_datain + 2, "dynamic ", 1);
 #endif
 	}
 	if (dataout) {
-	    cmd_dataout[0] = ((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I) << 24) 
+	    /* CALL other_out, WHEN NOT DATA_OUT */
+	    cmd_dataout[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL) << 24) | 
+		DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE;
+	    cmd_dataout[1] = (unsigned long) (hostdata->script + 
+    	    	hostdata->E_other_out / sizeof(long));
+	    /* MOVE count, buf, WHEN DATA+OUT */
+	    cmd_dataout[2] = ((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I) << 24) 
 		| count;
-	    cmd_dataout[1] = buf;
-	    cmd_dataout[2] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL | 
-		DCMD_TCI_CD | DCMD_TCI_IO | DCMD_TCI_MSG) << 24) | 
-		DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE | DBC_TCI_TRUE;
-	    cmd_dataout[3] = (unsigned long) hostdata->script + 
-    	    	hostdata->E_msg_in;
+	    cmd_dataout[3] = buf;
 #if 0
 	    print_insn (host, cmd_dataout, "dynamic ", 1);
 	    print_insn (host, cmd_dataout + 2, "dynamic ", 1);
@@ -2631,8 +3639,6 @@
 	cmd_datain += 2;
     }
 #endif
-
-
     if (dataout) {
 	cmd_dataout[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
     	    DBC_TCI_TRUE;
@@ -2643,82 +3649,132 @@
 #endif
 	cmd_dataout += 2;
     }
-
-
     return tmp;
 }
-    
-/* 
+
+/*
  * Function : int NCR53c7xx_queue_command (Scsi_Cmnd *cmd,
- *      void (*done)(Scsi_Cmnd *)) 
+ *      void (*done)(Scsi_Cmnd *))
  *
  * Purpose :  enqueues a SCSI command
  *
  * Inputs : cmd - SCSI command, done - function called on completion, with
  *      a pointer to the command descriptor.
- * 
- * Returns : 0
  *
- * Side effects : 
- *      cmd is added to the per instance issue_queue, with minor 
- *      twiddling done to the host specific fields of cmd.  If the 
- *      main coroutine is not running, it is restarted.
+ * Returns : 0
  *
+ * Side effects :
+ *      cmd is added to the per instance driver issue_queue, with major
+ *      twiddling done to the host specific fields of cmd.  If the
+ *      process_issue_queue corouting isn't running, it is restarted.
+ * 
+ * NOTE : we use the host_scribble field of the Scsi_Cmnd structure to 
+ *	hold our own data, and pervert the ptr field of the SCp field
+ *	to create a linked list.
  */
 
-int NCR53c7xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *)) {
-    NCR53c7x0_local_declare();
-    struct NCR53c7x0_cmd *tmp;
+int
+NCR53c7xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *)) {
     struct Scsi_Host *host = cmd->host;
-    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
-	host->hostdata;
+    struct NCR53c7x0_hostdata *hostdata = 
+	(struct NCR53c7x0_hostdata *) host->hostdata;
     unsigned long flags;
-    unsigned char target_was_busy;
-    NCR53c7x0_local_setup(host);
-    
-    if (((hostdata->options & (OPTION_DEBUG_INIT_ONLY|OPTION_DEBUG_PROBE_ONLY)) ||
-	((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
-	!(hostdata->debug_lun_limit[cmd->target] & (1 << cmd->lun)))) ||
-	cmd->target > 7) {
+    Scsi_Cmnd *tmp;
+
+    cmd->scsi_done = done;
+    cmd->host_scribble = NULL;
+    cmd->SCp.ptr = NULL;
+    cmd->SCp.buffer = NULL;
+
+    save_flags(flags);
+    cli();
+    if ((hostdata->options & (OPTION_DEBUG_INIT_ONLY|OPTION_DEBUG_PROBE_ONLY)) 
+	|| ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
+	    !(hostdata->debug_lun_limit[cmd->target] & (1 << cmd->lun))) 
+    /* 
+     * FIXME : the check on cmd->target > 7 will need to change once we
+     * support WIDE target IDs 8-15 inclusive.  We should also sanity check
+     * here to see if the luser has told us to send a command to ourselves.
+     */
+	|| cmd->target > 7
+	|| hostdata->state == STATE_DISABLED) {
 	printk("scsi%d : disabled target %d lun %d\n", host->host_no,
 	    cmd->target, cmd->lun);
 	cmd->result = (DID_BAD_TARGET << 16);
-	done(cmd);
-	return 0;
-    }
-
-    if (hostdata->options & OPTION_DEBUG_NCOMMANDS_LIMIT) {
-	if (hostdata->debug_count_limit == 0) {
-	    printk("scsi%d : maximum commands exceeded\n", host->host_no);
-	    cmd->result = (DID_BAD_TARGET << 16);
-	    done(cmd);
-	    return 0;
-	} else if (hostdata->debug_count_limit != -1) 
-	    --hostdata->debug_count_limit;
-    }
-   
-    if (hostdata->options & OPTION_DEBUG_READ_ONLY) {
+    } else if ((hostdata->options & OPTION_DEBUG_NCOMMANDS_LIMIT) &&
+	(hostdata->debug_count_limit == 0)) {
+	printk("scsi%d : maximum commands exceeded\n", host->host_no);
+	cmd->result = (DID_BAD_TARGET << 16);
+	cmd->result = (DID_BAD_TARGET << 16);
+    } else if (hostdata->options & OPTION_DEBUG_READ_ONLY) {
 	switch (cmd->cmnd[0]) {
 	case WRITE_6:
 	case WRITE_10:
 	    printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
 		host->host_no);
 	    cmd->result = (DID_BAD_TARGET << 16);
-	    done(cmd);
-	    return 0;
 	}
+    } else {
+    	if ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
+	    hostdata->debug_count_limit != -1) 
+	    --hostdata->debug_count_limit;
+	restore_flags (flags);
+	cmd->result = 0xffff;	/* The NCR will overwrite message
+				       and status with valid data */
+	cmd->host_scribble = (unsigned char *) tmp = create_cmd (cmd, 1);
+    }
+    cli();
+    if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
+	cmd->SCp.ptr = (unsigned char *) hostdata->issue_queue;
+	hostdata->issue_queue = cmd;
+    } else {
+	for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->SCp.ptr; 
+		tmp = (Scsi_Cmnd *) tmp->SCp.ptr);
+	tmp->SCp.ptr = (unsigned char *) cmd;
     }
+    restore_flags (flags);
+    run_process_issue_queue();
+    return 0;
+}
 
-    cmd->scsi_done = done;
-    cmd->result = 0xffff;		/* The NCR will overwrite message
-					   and status with valid data */
-    
-    cmd->host_scribble = (unsigned char *) tmp = create_cmd (cmd);
+/* 
+ * Function : void to_start_queue (struct Scsi_Host *host, 
+ *	struct NCR53c7x0_hostdata * hostdata, Scsi_Cmnd *cmd)
+ *
+ * Purpose : takes a SCSI command which was just removed from the 
+ *	issue queue, and deals with it by inserting it at the tail 
+ *	of the appropriate shared issue queue or by terminating 
+ *	it immediately.
+ *
+ * Inputs : 
+ *	host - SCSI host adater; hostdata - hostdata structure for 
+ *	this adapter; cmd - a pointer to the command; should have 
+ *	the host_scribble field initialized to point to a valid 
+ *	
+ * Side effects : 
+ *      cmd is added to the per instance issue_queue, with minor 
+ *      twiddling done to the host specific fields of cmd.
+ *
+ */
+
+static __inline__ void
+to_start_queue (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
+    struct NCR53c7x0_cmd *cmd) {
+    NCR53c7x0_local_declare();
+    Scsi_Cmnd *tmp = cmd->cmd;
+    unsigned long flags;
+    /* dsa start is negative, so subtraction is used */
+    unsigned char *dsa = ((unsigned char *) cmd->dsa)
+	    - hostdata->dsa_start;	
+    NCR53c7x0_local_setup(host);
+#if 0	
+    printk("scsi%d : new dsa is 0x%x\n", host->host_no, (unsigned) dsa);
+#endif
 
     /*
-     * On NCR53c710 and better chips, we have two issue queues : 
-     * The queue maintained by the Linux driver, and the queue 
-     * maintained by the NCR chip.
+     * On NCR53c710 and better chips, we have two queues : 
+     * The issue queue maintained by the Linux driver, and the 
+     * start queue maintained by the NCR chip.
      * 
      * The Linux queue includes commands which have been generated,
      * but may be unable to execute because the device is busy, 
@@ -2726,89 +3782,70 @@
      * as BUS FREE is detected.
      *
      * NCR53c700 and NCR53c700-66 chips use only the Linux driver
-     * queue. 
-     * 
-     * So, insert into the Linux queue if the device is busy or 
-     * we are running on an old chip, otherwise insert directly into
-     * the NCR queue.
+     * issue queue. 
      */
     
-    /*
-     * REQUEST sense commands need to be executed before all other 
-     * commands since any command will clear the contingent allegiance 
-     * condition that exists and the sense data is only guaranteed to be 
-     * valid while the condition exists.
-     */
-
     save_flags(flags);
     cli();
-
+    
     /* 
-     * Consider a target busy if there are _any_ commands running
-     * on it.  
-     * XXX - Once we do SCSI-II tagged queuing, we want to use 
-     *     a different definition of busy.
+     * Work arround race condition : if an interrupt fired and we 
+     * got disabled forget about this command.
      */
-    	
-    target_was_busy = hostdata->busy[cmd->target][cmd->lun]
-#ifdef LUN_BUSY
-	++
-#endif
-; 
-
-    if (!(hostdata->options & OPTION_700)  &&
-	!target_was_busy) {
-	unsigned char *dsa = ((unsigned char *) tmp->dsa)
-		- hostdata->dsa_start;	
-    	    	/* dsa start is negative, so subtraction is used */
-#if 0	
-	printk("scsi%d : new dsa is 0x%x\n", host->host_no, (unsigned) dsa);
-#endif
 
-    	if (hostdata->running_list)
-    	    hostdata->running_list->prev = tmp;
+    if (hostdata->state == STATE_DISABLED) {
+	printk("scsi%d : driver disabled\n", host->host_no);
+	tmp->result = (DID_BAD_TARGET << 16);
+	cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
+	hostdata->free = cmd;
+	tmp->scsi_done(tmp);
+	restore_flags (flags);
+	return;
+    }
 
-    	tmp->next = hostdata->running_list;
+    ++hostdata->busy[tmp->target][tmp->lun];
 
-	if (!hostdata->running_list)
-	    hostdata->running_list = tmp;
+    if (hostdata->running_list)
+	hostdata->running_list->prev = cmd;
+    cmd->next = hostdata->running_list;
+    hostdata->running_list = cmd;
     	
 
-	if (hostdata->idle) {
-	    hostdata->idle = 0;
-    	    hostdata->state = STATE_RUNNING;
-	    NCR53c7x0_write32 (DSP_REG,  ((unsigned long) hostdata->script) +
-		hostdata->E_schedule);
-	}
+    if (hostdata->idle) {
+	hostdata->idle = 0;
+	hostdata->state = STATE_RUNNING;
+	NCR53c7x0_write32 (DSP_REG,  ((unsigned long) hostdata->script) +
+	    hostdata->E_schedule);
+    }
 
-/* XXX - make function */
-	for (;;) {
-	    /* 
-	     * If the NCR doesn't have any commands waiting in its
-	     * issue queue, then we simply create a new issue queue,
-	     * and signal the NCR that we have more commands.
-	     */
-		
-	    if (!hostdata->issue_dsa_head) {
+/* XXX - make function ?*/
+    for (;;) {
+/* 
+ * If the NCR doesn't have any commands waiting in its  issue queue, then we 
+ * simply create a new issue queue, and signal the NCR that we have more 
+ * commands.
+ */
+	if (!hostdata->issue_dsa_head) {
 #if 0
-		printk ("scsi%d : no issue queue\n", host->host_no);
+	    printk ("scsi%d : no issue queue\n", host->host_no);
+#endif
+	    hostdata->issue_dsa_tail = hostdata->issue_dsa_head = dsa;
+	    NCR53c7x0_write8(hostdata->istat, ISTAT_10_SIGP);
+	    break;
+/*
+ * Otherwise, we blindly perform an atomic write 
+ * to the next pointer of the last command we 
+ * placed in that queue.
+ *
+ * Looks like it doesn't work, but I think it does - 
+ */
+	} else {
+#if 0
+	    printk ("scsi%d : existing issue queue\n", host->host_no);
 #endif
-    	    	hostdata->issue_dsa_tail = hostdata->issue_dsa_head = dsa;
-    	    	NCR53c7x0_write8(hostdata->istat, 
-    	    	    NCR53c7x0_read8(hostdata->istat) | ISTAT_10_SIGP);
-		break;
-	    /*
-	     * Otherwise, we blindly perform an atomic write 
-	     * to the next pointer of the last command we 
-	     * placed in that queue.
-	     *
-	     * Looks like it doesn't work, but I think it does - 
- 	     */
- 	    } else {
-		printk ("scsi%d : existing issue queue\n", host->host_no);
 	    /* XXX - Replace with XCHG or equivalent */
-    	    	hostdata->issue_dsa_tail = *((unsigned char **) 
-    	    	    (hostdata->issue_dsa_tail + hostdata->dsa_next)) = dsa;
+	    hostdata->issue_dsa_tail = *((unsigned char **) 
+		(hostdata->issue_dsa_tail + hostdata->dsa_next)) = dsa;
 	    /*
 	     * After which, one of two things will happen : 
 	     * The NCR will have scheduled a command, either this
@@ -2820,26 +3857,164 @@
 	     * in which case we were too late.  If this happens,
 	     * we restart
 	     */
-		if (hostdata->issue_dsa_head)
-		    break;
-	    }
+	    if (hostdata->issue_dsa_head) 
+		break;
 	}
-/* XXX - end */
-    } else {
-#if 1
-	printk ("scsi%d : using issue_queue instead of issue_dsa_head!\n",
-    	    host->host_no);
-#endif
-	for (tmp = (struct NCR53c7x0_cmd *) hostdata->issue_queue; 
-    	    tmp->next; tmp = (struct NCR53c7x0_cmd *) tmp->next);
-    	tmp->next = tmp;
     }
     restore_flags(flags);
-    return 0;
 }
 
+/*
+ * Function : busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata 
+ *	*hostdata, Scsi_Cmnd *cmd)
+ *
+ * Purpose : decide if we can pass the given SCSI command on to the 
+ *	device in question or not.
+ *  
+ * Returns : non-zero when we're busy, 0 when we aren't.
+ */
+
+static __inline__ int
+busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata, 
+    Scsi_Cmnd *cmd) {
+    /* FIXME : in the future, this needs to accomodate SCSI-II tagged
+       queuing, and we may be able to play with fairness here a bit.
+     */
+    return hostdata->busy[cmd->target][cmd->lun];
+}
+
+/*
+ * Function : process_issue_queue (void)
+ *
+ * Purpose : transfer commands from the issue queue to NCR start queue 
+ *	of each NCR53c7/8xx in the system, avoiding kernel stack 
+ *	overflows when the scsi_done() function is invoked recursively.
+ * 
+ * NOTE : process_issue_queue exits with interrupts *disabled*, so the 
+ *	caller must renable them if it desires.
+ * 
+ * NOTE : process_issue_queue should be called from both 
+ *	NCR53c7x0_queue_command() and from the interrupt handler 
+ *	after command completion in case NCR53c7x0_queue_command()
+ * 	isn't invoked again but we've freed up resources that are
+ *	needed.
+ */
+
+
+static void 
+process_issue_queue (void) {
+  Scsi_Cmnd *tmp, *prev;
+  struct Scsi_Host *host;
+  struct NCR53c7x0_hostdata *hostdata;
+  int done;
+  unsigned long flags;
+  /*
+   * We run (with interrupts disabled) until we're sure that none of 
+     * the host adapters have anything that can be done, at which point 
+     * we set process_issue_queue_running to 0 and exit.
+     *
+     * Interrupts are enabled before doing various other internal 
+     * instructions, after we've decided that we need to run through
+     * the loop again.
+     *
+     */
+  
+  save_flags(flags);
+  do {
+    cli();			/* Freeze request queues */
+    done = 1;
+    for (host = first_host; host && host->hostt == the_template; 
+	 host = host->next) {
+      hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
+      cli();
+      if (hostdata->issue_queue) {
+	if (hostdata->state == STATE_DISABLED) {
+	  tmp = (Scsi_Cmnd *) hostdata->issue_queue;
+	  hostdata->issue_queue = (Scsi_Cmnd *) tmp->SCp.ptr;
+	  tmp->result = (DID_BAD_TARGET << 16);
+	  if (tmp->host_scribble) {
+	    ((struct NCR53c7x0_cmd *)tmp->host_scribble)->next = 
+	      hostdata->free;
+	    hostdata->free = 
+	      (struct NCR53c7x0_cmd *)tmp->host_scribble;
+	    tmp->host_scribble = NULL;
+	  }
+	  tmp->scsi_done (tmp);
+	  done = 0;
+	} else 
+	  for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, 
+	       prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) 
+	       tmp->SCp.ptr) 
+	    if (!tmp->host_scribble || 
+		!busyp (host, hostdata, tmp)) {
+	      if (tmp->host_scribble) {
+		if (hostdata->options & OPTION_DEBUG_QUEUES) 
+		  printk ("scsi%d : moving command for target %d lun %d to start queue\n",
+			  host->host_no, tmp->target, tmp->lun);
+		
+		if (prev)
+		  prev->SCp.ptr = tmp->SCp.ptr;
+		else
+		  hostdata->issue_queue = (Scsi_Cmnd *) 
+		    tmp->SCp.ptr;
+		tmp->SCp.ptr = NULL;
+
+		to_start_queue (host, hostdata, 
+				(struct NCR53c7x0_cmd *)
+				tmp->host_scribble);
+		done = 0;
+	      } else {
+		if (((tmp->result & 0xff) == 0xff) ||
+		    ((tmp->result & 0xff00) == 0xff00)) {
+  /* If we got here, we've found a command which did not have a private
+     data buffer allocated/initialized during the queue_command() routine
+     due to a memory shortage.  Try to grab one now... if we succeed,
+     transfer the command to the run queue.  If not, just keep going.  */
+		  if (NULL != (tmp->host_scribble =
+			       (unsigned char *) create_cmd (tmp, 0))) {
+		    printk ("scsi%d : reused a command struct!\n", host->host_no);
+		    if (prev)
+		      prev->SCp.ptr = tmp->SCp.ptr;
+		    else
+		      hostdata->issue_queue = (Scsi_Cmnd *) 
+			tmp->SCp.ptr;
+		    tmp->SCp.ptr = NULL;
+		    to_start_queue (host, hostdata, 
+				    (struct NCR53c7x0_cmd *)
+				    tmp->host_scribble);
+		    done = 0;
+		  }
+
+#ifdef NOTDEF
+		  printk ("scsi%d : danger Will Robinson!\n",
+			  host->host_no);
+		  tmp->result = DID_ERROR << 16;
+		  disable (host);
+#endif
+		} else {
+		  if (prev)
+		    prev->SCp.ptr = tmp->SCp.ptr;
+		  else
+		    hostdata->issue_queue = (Scsi_Cmnd *) 
+		      tmp->SCp.ptr;
+		  tmp->SCp.ptr = NULL;
+		  tmp->scsi_done(tmp);
+		  done = 0;
+		}
+	      }
+	      /** nope!  dplatt
+		sti();
+		**/
+	      restore_flags(flags);
+	    } /* if target/lun is not busy */
+      } /* if hostdata->issue_queue */
+    } /* for host */
+  } while (!done);
+  process_issue_queue_running = 0;
+}
 
-int fix_pointers (unsigned long dsa) {
+int 
+fix_pointers (unsigned long dsa) {
     return 0;
 }
 
@@ -2854,7 +4029,8 @@
  * 	may be NULL.
  */
 
-static void intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
+static void 
+intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
     NCR53c7x0_local_declare();
     struct NCR53c7x0_hostdata *hostdata = 
     	(struct NCR53c7x0_hostdata *) host->hostdata;
@@ -2915,20 +4091,27 @@
 #endif
 	}
     } 
-    
+
+/*
+ * FIXME : in theory, we can also get a UDC when a STO occurs.
+ */
     if (sstat0_sist0 & SSTAT0_UDC) {
 	fatal = 1;
 	if (cmd) {
 	    printk("scsi%d : target %d lun %d unexpected disconnect\n",
 		host->host_no, cmd->cmd->target, cmd->cmd->lun);
+	    print_lots (host);
 	    abnormal_finished(cmd, DID_ERROR << 16);
-	}
+	} else 
+	     printk("scsi%d : unexpected disconnect (no command)\n",
+		host->host_no);
+
 	hostdata->dsp = hostdata->script + hostdata->E_schedule / 
 	    sizeof(long);
 	hostdata->dsp_changed = 1;
-    /* SCSI PARITY error */
-    } 
+    }
 
+    /* SCSI PARITY error */
     if (sstat0_sist0 & SSTAT0_PAR) {
 	fatal = 1;
 	if (cmd && cmd->cmd) {
@@ -2949,7 +4132,26 @@
     if (sstat0_sist0 & SSTAT0_SGE) {
 	fatal = 1;
 	printk("scsi%d : gross error\n", host->host_no);
-	/* XXX Reduce synchronous transfer rate! */
+	/* Reset SCSI offset */
+	if ((hostdata->chip / 100) == 8)
+	    NCR53c7x0_write8 (STEST2_REG_800, STEST2_800_ROF);
+	
+	/* 
+         * A SCSI gross error may occur when we have 
+	 *
+	 * - A synchronous offset which causes the SCSI FIFO to be overwritten.
+	 *
+	 * - A REQ which causes the maxmimum synchronous offset programmed in 
+	 * 	the SXFER register to be exceeded.
+	 *
+	 * - A phase change with an outstanding synchronous offset.
+	 *
+	 * - Residual data in the synchronous data FIFO, with a transfer
+	 *	other than a synchronous receive is started.$#
+	 */
+		
+
+	/* XXX Should deduce synchronous transfer rate! */
 	hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
     	    sizeof(long);
 	hostdata->dsp_changed = 1;
@@ -2963,7 +4165,11 @@
 	intr_phase_mismatch (host, cmd);
     }
 
-#if 1
+#if 0
+    if (sstat0_sist0 & SIST0_800_RSL) 
+	printk ("scsi%d : Oh no Mr. Bill!\n", host->host_no);
+#endif
+    
 /*
  * If a fatal SCSI interrupt occurs, we must insure that the DMA and
  * SCSI FIFOs were flushed.
@@ -2978,23 +4184,17 @@
 /* XXX - code check for 700/800 chips */
 	if (!(hostdata->dstat & DSTAT_DFE)) {
 	    printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
-#if 0
     	    if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
     	    	NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
     	    	while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
     	    	    DSTAT_DFE));
-    	    } else 
-#endif
-	    {
+    	    } else {
     	    	NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
     	    	while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
     	    }
+	    hostdata->dstat |= DSTAT_DFE;
     	}
-
-	NCR53c7x0_write8 (STEST3_REG_800, STEST3_800_CSF);
-	while (NCR53c7x0_read8 (STEST3_REG_800) & STEST3_800_CSF);
     }
-#endif
 }
 
 /*
@@ -3008,18 +4208,21 @@
  *	this handler.  
  */
 
-static void NCR53c7x0_intr (int irq, struct pt_regs * regs) {
+static void 
+NCR53c7x0_intr (int irq, struct pt_regs * regs) {
     NCR53c7x0_local_declare();
     struct Scsi_Host *host;			/* Host we are looking at */
     unsigned char istat; 			/* Values of interrupt regs */
     struct NCR53c7x0_hostdata *hostdata;	/* host->hostdata */
-    struct NCR53c7x0_cmd *cmd,			/* command which halted */
-	**cmd_prev_ptr;
+    struct NCR53c7x0_cmd *cmd;			/* command which halted */
     unsigned long *dsa;				/* DSA */
     int done = 1;				/* Indicates when handler 
 						   should terminate */
     int interrupted = 0;			/* This HA generated 
 						   an interrupt */
+    int have_intfly;				/* Don't print warning 
+						   messages when we stack
+						   INTFLYs */
     unsigned long flags;				
 
 #ifdef NCR_DEBUG
@@ -3027,27 +4230,23 @@
     size_t buflen;				/* Length of same */
 #endif
 
-#if 0
-    printk("interrupt %d received\n", irq);
-#endif
-
     do {
 	done = 1;
-	for (host = first_host; host; host = hostdata->next ?
-	     hostdata->next : NULL) {
+	for (host = first_host; host; host = host->next) 
+	    if (host->hostt == the_template && host->irq == irq) {
     	    NCR53c7x0_local_setup(host);
 
 	    hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
 	    hostdata->dsp_changed = 0;
 	    interrupted = 0;
-
+	    have_intfly = 0;
 
 	    do {
 		hostdata->dstat_valid = 0;
 		interrupted = 0;
 		/*
 		 * Only read istat once, since reading it again will unstack
-		 * interrupts.
+		 * interrupts?
 		 */
 		istat = NCR53c7x0_read8(hostdata->istat);
 
@@ -3057,7 +4256,8 @@
 		 * the SCSI processor continues running, we can't just look
 		 * at the contents of the DSA register and continue running.
 		 */
-/* XXX - this is getting big, and should move to intr_intfly() */
+/* XXX - this is too big, offends my sense of aesthetics, and should 
+   move to intr_intfly() */
 		if ((hostdata->options & OPTION_INTFLY) && 
 		    ((hostdata->chip / 100) == 8 && (istat & ISTAT_800_INTF))) {
 		    char search_found = 0;	/* Got at least one ? */
@@ -3065,8 +4265,8 @@
 		    interrupted = 1;
 
 		    /* 
-		     * Clear the INTF bit by writing a one.  This reset operation 
-		     * is self-clearing.
+		     * Clear the INTF bit by writing a one.  
+		     * This reset operation is self-clearing.
 		     */
 		    NCR53c7x0_write8(hostdata->istat, istat|ISTAT_800_INTF);
 
@@ -3084,11 +4284,10 @@
 		    save_flags(flags);
 		    cli();
 restart:
-		    for (cmd_prev_ptr = (struct NCR53c7x0_cmd **) 
-			 &(hostdata->running_list), cmd = 
-                         (struct NCR53c7x0_cmd *) hostdata->running_list; cmd ;
-                         cmd_prev_ptr = (struct NCR53c7x0_cmd **) &(cmd->next), 
-    	    	    	 cmd = (struct NCR53c7x0_cmd *) cmd->next) {
+		    for ( cmd = (struct NCR53c7x0_cmd *) hostdata->running_list
+			 ; cmd
+			 ; cmd = (struct NCR53c7x0_cmd *) cmd->next) {
+
 			Scsi_Cmnd *tmp;
 
 			if (!cmd) {
@@ -3114,18 +4313,16 @@
 
 			/* Important - remove from list _before_ done is called */
 			/* XXX - SLL.  Seems like DLL is unnecessary */
-			if (cmd->prev)
-			    cmd->prev->next = cmd->next;
-			if (cmd_prev_ptr)
-			    *cmd_prev_ptr = (struct NCR53c7x0_cmd *) cmd->next;
-
-#ifdef LUN_BUSY
-			/* Check for next command for target, add to issue queue */
-			if (--hostdata->busy[tmp->target][tmp->lun]) {
-			}
-#endif
 
+			if (cmd->next)
+			  cmd->next->prev = cmd->prev;
 
+			if (cmd->prev)
+			    cmd->prev->next = cmd->next;
+			else
+			    hostdata->running_list = cmd->next;
+			  
+			--hostdata->busy[tmp->target][tmp->lun];
     	    	    	cmd->next = hostdata->free;
     	    	    	hostdata->free = cmd;
 
@@ -3146,10 +4343,20 @@
 
 		    }
 		    restore_flags(flags);
-
-		    if (!search_found)  {
+		   
+    /*
+     * I think that we're stacking INTFLY interrupts; taking care of 
+     * all the finished commands on the first one, and then getting
+     * worried when we see the next one.  The magic with have_intfly
+     * should tell if this is the case..
+     */
+		
+		    if (!search_found && !have_intfly)  {
 			printk ("scsi%d : WARNING : INTFLY with no completed commands.\n",
 			    host->host_no);
+		    } else if (!have_intfly)  {
+			have_intfly = 1; 
+    		    	run_process_issue_queue();
 		    }
 		}
 
@@ -3157,6 +4364,12 @@
 		    done = 0;
 		    interrupted = 1;
     	    	    hostdata->state = STATE_HALTED;
+
+		    if (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
+			SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) 
+			printk ("scsi%d : SCSI FIFO not empty\n", 
+			    host->host_no);
+
 		    /*
 		     * NCR53c700 and NCR53c700-66 change the current SCSI
 		     * process, hostdata->current, in the Linux driver so
@@ -3205,23 +4418,20 @@
 			hostdata->dstat_valid = 1;
 		    }
 
-#if 1
 	    /* XXX - code check for 700/800 chips */
 		    if (!(hostdata->dstat & DSTAT_DFE)) {
 			printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
-	    #if 0
 			if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
 			    NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
 			    while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
 				DSTAT_DFE));
 			} else 
-	    #endif
 			{
 			    NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
 			    while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
 			}
+			hostdata->dstat |= DSTAT_DFE;
 		    }
-#endif
 		}
 	    } while (interrupted);
 
@@ -3270,20 +4480,166 @@
 
 static int 
 abort_connected (struct Scsi_Host *host) {
+#ifdef NEW_ABORT
+    NCR53c7x0_local_declare();
+#endif
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;
+/* FIXME : this probably should change for production kernels; at the 
+   least, counter sould move to a per-host structure. */
+    static int counter = 5;
+#ifdef NEW_ABORT
+    int sstat, phase, offset;
+    unsigned long *script;
+    NCR53c7x0_local_setup(host);
+#endif
+
+    if (--counter <= 0) {
+	disable(host);
+	return 0;
+    }
 
-    hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
-	sizeof(long);
-    hostdata->dsp_changed = 1;
     printk ("scsi%d : DANGER : abort_connected() called \n",
 	host->host_no);
-/* XXX - need to flag the command as aborted after the abort_connected
- 	 code runs 
+
+#ifdef NEW_ABORT
+
+/*
+ * New strategy : Rather than using a generic abort routine,
+ * we'll specifically try to source or sink the appropriate
+ * amount of data for the phase we're currently in (taking into 
+ * account the current synchronous offset) 
  */
-    return 0;
-}
 
+    sstat = (NCR53c8x0_read8 ((chip / 100) == 8 ? SSTAT1_REG : SSTAT2_REG);
+    offset = OFFSET (sstat & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
+    phase = sstat & SSTAT2_PHASE_MASK;
+
+/*
+ * SET ATN
+ * MOVE source_or_sink, WHEN CURRENT PHASE 
+ * < repeat for each outstanding byte >
+ * JUMP send_abort_message
+ */
+
+    script = hostdata->abort_script = kmalloc (
+	8  /* instruction size */ * (
+	    1 /* set ATN */,
+	    (!offset ? 1 : offset) /* One transfer per outstanding byte */ +
+	    1 /* send abort message */),
+	GFP_ATOMIC);
+
+
+#else /* def NEW_ABORT */
+    hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
+	    sizeof(long);
+#endif /* def NEW_ABORT */
+    hostdata->dsp_changed = 1;
+
+/* XXX - need to flag the command as aborted after the abort_connected
+ 	 code runs 
+ */
+    return 0;
+}
+
+/*
+ * Function : static int datapath_residual (Scsi_Host *host)
+ *
+ * Purpose : return residual data count of what's in the chip.
+ *
+ * Inputs : host - SCSI host
+ */
+
+static int
+datapath_residual (struct Scsi_Host *host) {
+    NCR53c7x0_local_declare();
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+    	host->hostdata;
+    int count, synchronous, sstat;
+    NCR53c7x0_local_setup(host);
+    /* COMPAT : the 700 and 700-66 need to use DFIFO_00_BO_MASK */
+    count = ((NCR53c7x0_read8 (DFIFO_REG) & DFIFO_10_BO_MASK) -
+	(NCR53c7x0_read32 (DBC_REG) & DFIFO_10_BO_MASK)) & DFIFO_10_BO_MASK;
+    synchronous = NCR53c7x0_read8 (SXFER_REG) & SXFER_MO_MASK;
+    /* COMPAT : DDIR is elsewhere on non-'8xx chips. */
+    if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
+    /* Receive */
+	if (synchronous) 
+	    count += (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ? 
+		SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
+	else
+	    if (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ? 
+		SSTAT0_REG : SSTAT1_REG) & SSTAT1_ILF)
+		++count;
+    } else {
+    /* Send */
+	sstat = ((hostdata->chip / 100) == 8) ?  NCR53c7x0_read8 (SSTAT0_REG) :
+	    NCR53c7x0_read8 (SSTAT1_REG);
+	if (sstat & SSTAT1_OLF)
+	    ++count;
+	if (synchronous && (sstat & SSTAT1_ORF))
+	    ++count;
+    }
+    return count;
+}
+
+/* 
+ * Function : static const char * sbcl_to_phase (int sbcl)_
+ *
+ * Purpose : Convert SBCL register to user-parsable phase representation
+ *
+ * Inputs : sbcl - value of sbcl register
+ */
+
+
+static const char *
+sbcl_to_phase (int sbcl) {
+    switch (sbcl & SBCL_PHASE_MASK) {
+    case SBCL_PHASE_DATAIN:
+	return "DATAIN";
+    case SBCL_PHASE_DATAOUT:
+	return "DATAOUT";
+    case SBCL_PHASE_MSGIN:
+	return "MSGIN";
+    case SBCL_PHASE_MSGOUT:
+	return "MSGOUT";
+    case SBCL_PHASE_CMDOUT:
+	return "CMDOUT";
+    case SBCL_PHASE_STATIN:
+	return "STATUSIN";
+    default:
+	return "unknown";
+    }
+}
+
+/* 
+ * Function : static const char * sstat2_to_phase (int sstat)_
+ *
+ * Purpose : Convert SSTAT2 register to user-parsable phase representation
+ *
+ * Inputs : sstat - value of sstat register
+ */
+
+
+static const char *
+sstat2_to_phase (int sstat) {
+    switch (sstat & SSTAT2_PHASE_MASK) {
+    case SSTAT2_PHASE_DATAIN:
+	return "DATAIN";
+    case SSTAT2_PHASE_DATAOUT:
+	return "DATAOUT";
+    case SSTAT2_PHASE_MSGIN:
+	return "MSGIN";
+    case SSTAT2_PHASE_MSGOUT:
+	return "MSGOUT";
+    case SSTAT2_PHASE_CMDOUT:
+	return "CMDOUT";
+    case SSTAT2_PHASE_STATIN:
+	return "STATUSIN";
+    default:
+	return "unknown";
+    }
+}
 
 /* 
  * Function : static void intr_phase_mismatch (struct Scsi_Host *host, 
@@ -3298,153 +4654,311 @@
  *	is restarted, jumping to the command_complete entry point, or 
  *	patching the address and transfer count of the current instruction 
  *	and calling the msg_in entry point as appropriate.
- *
  */
 
-static void intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd
-    *cmd) {
+static void 
+intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
     NCR53c7x0_local_declare();
     unsigned long dbc_dcmd, *dsp, *dsp_next;
     unsigned char dcmd, sbcl;
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
     	host->hostdata;
-    char *phase;
+    int residual;
+    enum {ACTION_ABORT, ACTION_ABORT_PRINT, ACTION_CONTINUE} action = 
+	ACTION_ABORT_PRINT;
+    const char *where = NULL;
     NCR53c7x0_local_setup(host);
 
-    if (!cmd) {
-	printk ("scsi%d : phase mismatch interrupt occurred with no current command.\n",
-	    host->host_no);
-	abort_connected(host);
-	return;
-    }
-
     /*
      * Corrective action is based on where in the SCSI SCRIPT(tm) the error 
      * occurred, as well as which SCSI phase we are currently in.
      */
-
     dsp_next = (unsigned long *) NCR53c7x0_read32(DSP_REG);
 
+    /* 
+     * Fetch the current instruction, and remove the operands for easier 
+     * interpretation.
+     */
+    dbc_dcmd = NCR53c7x0_read32(DBC_REG);
+    dcmd = (dbc_dcmd & 0xff000000) >> 24;
     /*
-     * Like other processors, the NCR adjusts the DSP pointer before
+     * Like other processors, the NCR adjusts the instruction pointer before
      * instruction decode.  Set the DSP address back to what it should
      * be for this instruction based on its size (2 or 3 longs).
      */
-
-    dbc_dcmd = NCR53c7x0_read32(DBC_REG);
-    dcmd = (dbc_dcmd & 0xff000000) >> 24;
     dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
-    
+
+
+    /*
+     * Read new SCSI phase from the SBCL lines.  Since all of our code uses 
+     * a WHEN conditional instead of an IF conditional, we don't need to 
+     * wait for a new REQ.
+     */
+    sbcl = NCR53c7x0_read8(SBCL_REG) & SBCL_PHASE_MASK;
+
+    if (!cmd) {
+	action = ACTION_ABORT_PRINT;
+	where = "no current command";
     /*
-     * Read new SCSI phase from the SBCL lines.
+     * The way my SCSI SCRIPTS(tm) are architected, recoverable phase
+     * mismatches should only occur where we're doing a multi-byte  
+     * BMI instruction.  Specifically, this means 
+     *
+     *  - select messages (a SCSI-I target may ignore additional messages
+     * 		after the IDENTIFY; any target may reject a SDTR or WDTR)
+     *
+     *  - command out (targets may send a message to signal an error 
+     * 		condition, or go into STATUSIN after they've decided 
+     *		they don't like the command.
      *
-     * Note that since all of our code uses a WHEN conditional instead of an 
-     * IF conditional, we don't need to wait for a valid REQ.
+     *	- reply_message (targets may reject a multi-byte message in the 
+     *		middle)
+     *
+     * 	- data transfer routines (command completion with buffer space
+     *		left, disconnect message, or error message)
      */
-    sbcl = NCR53c7x0_read8(SBCL_REG);
-    switch (sbcl) {
-    case SBCL_PHASE_DATAIN:
-	phase = "DATAIN";
-	break;
-    case SBCL_PHASE_DATAOUT:
-	phase = "DATAOUT";
-	break;
-    case SBCL_PHASE_MSGIN:
-	phase = "MSGIN";
-	break;
-    case SBCL_PHASE_MSGOUT:
-	phase = "MSGOUT";
-	break;
-    case SBCL_PHASE_CMDOUT:
-	phase = "CMDOUT";
-	break;
-    case SBCL_PHASE_STATIN:
-	phase = "STATUSIN";
+    } else if (((dsp >= cmd->data_transfer_start && 
+	dsp < cmd->data_transfer_end)) || dsp == (cmd->residual + 2)) {
+	if ((dcmd & (DCMD_TYPE_MASK|DCMD_BMI_OP_MASK|DCMD_BMI_INDIRECT|
+		DCMD_BMI_MSG|DCMD_BMI_CD)) == (DCMD_TYPE_BMI|
+		DCMD_BMI_OP_MOVE_I)) {
+	    residual = datapath_residual (host);
+	    if (hostdata->options & OPTION_DEBUG_DISCONNECT)
+	    	printk ("scsi%d : handling residual transfer (+ %d bytes from DMA FIFO)\n", 
+		    host->host_no, residual);
+
+	    /*
+	     * The first instruction is a CALL to the alternate handler for 
+	     * this data transfer phase, so we can do calls to 
+	     * munge_msg_restart as we would if control were passed 
+	     * from normal dynamic code.
+	     */
+	    if (dsp != cmd->residual + 2) {
+		cmd->residual[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL |
+			((dcmd & DCMD_BMI_IO) ? DCMD_TCI_IO : 0)) << 24) | 
+		    DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE;
+		cmd->residual[1] =  (unsigned long) (hostdata->script + 
+		    ((dcmd & DCMD_BMI_IO) ?  hostdata->E_other_in :
+		    hostdata->E_other_out) / sizeof(long));
+	    }
+
+	    /*
+	     * The second instruction is the a data transfer block
+	     * move instruction, reflecting the pointer and count at the 
+	     * time of the phase mismatch.
+	     */
+	    cmd->residual[2] = dbc_dcmd + residual;
+	    cmd->residual[3] = NCR53c7x0_read32(DNAD_REG) - residual;
+
+	    /*
+	     * The third and final instruction is a jump to the instruction
+	     * which follows the instruction which had to be 'split'
+	     */
+	    if (dsp != cmd->residual + 2) {
+		cmd->residual[4] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) 
+		    << 24) | DBC_TCI_TRUE;
+		cmd->residual[5] = (unsigned long) dsp_next;
+	    }
+
+	    /*
+	     * For the sake of simplicity, transfer control to the 
+	     * conditional CALL at the start of the residual buffer.
+	     */
+	    hostdata->dsp = cmd->residual;
+	    hostdata->dsp_changed = 1;
+	    action = ACTION_CONTINUE;
+	} else {
+	    where = "non-BMI dynamic DSA code";
+	    action = ACTION_ABORT_PRINT;
+	}
+    } else if (dsp == (hostdata->script + hostdata->E_select_msgout / 4)) {
+	/* Release ATN */
+	NCR53c7x0_write8 (SOCL_REG, 0);
+	switch (sbcl) {
+    /* 
+     * Some devices (SQ555 come to mind) grab the IDENTIFY message
+     * sent on selection, and decide to go into COMMAND OUT phase
+     * rather than accepting the rest of the messages or rejecting
+     * them.  Handle these devices gracefully.
+     */
+	case SBCL_PHASE_CMDOUT:
+	    hostdata->dsp = dsp + 2 /* two _words_ */;
+	    hostdata->dsp_changed = 1;
+	    printk ("scsi%d : target %d ignored SDTR and went into COMMAND OUT\n", 
+		host->host_no, cmd->cmd->target);
+	    cmd->flags &= ~CMD_FLAG_SDTR;
+	    action = ACTION_CONTINUE;
+	    break;
+	case SBCL_PHASE_MSGIN:
+	    NCR53c7x0_write32 (TEMP_REG, (long) dsp + 8);
+	    hostdata->dsp = hostdata->script + hostdata->E_msg_in / 4;
+	    hostdata->dsp_changed = 1;
+	    action = ACTION_CONTINUE;
+	    break;
+	default:
+	    where="select message out";
+	    action = ACTION_ABORT_PRINT;
+	}
+    /*
+     * Some SCSI devices will interpret a command as they read the bytes
+     * off the SCSI bus, and may decide that the command is Bogus before 
+     * they've read the entire commad off the bus.
+     */
+    } else if (dsp == hostdata->script + hostdata->E_cmdout_cmdout / sizeof 
+	(long)) {
+	hostdata->dsp = hostdata->script + hostdata->E_data_transfer /
+	    sizeof (long);
+	hostdata->dsp_changed = 1;
+	action = ACTION_CONTINUE;
+    /* FIXME : we need to handle message reject, etc. within msg_respond. */
+#ifdef notyet
+    } else if (dsp == hostdata->script + hostdata->E_reply_message) {
+	switch (sbcl) {
+    /* Any other phase mismatches abort the currently executing command.  */
+#endif
+    } else {
+	where = "unknown location";
+	action = ACTION_ABORT_PRINT;
+    }
+
+    /* Flush DMA FIFO */
+    if (!hostdata->dstat_valid) {
+	hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
+	hostdata->dstat_valid = 1;
+    }
+    if (!(hostdata->dstat & DSTAT_DFE)) {
+	if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
+	    NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
+	    /* FIXME : what about stacked DMA interrupts? */
+	    while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
+		DSTAT_DFE));
+	} else {
+	    NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
+	    while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
+	}
+	hostdata->dstat |= DSTAT_DFE;
+    }
+
+    switch (action) {
+    case ACTION_ABORT_PRINT:
+	printk("scsi%d : %s : unexpected phase %s.\n",
+	     host->host_no, where ? where : "unknown location", 
+	     sbcl_to_phase(sbcl));
+	print_lots (host);
+    /* Fall through to ACTION_ABORT */
+    case ACTION_ABORT:
+	abort_connected (host);
 	break;
-    default:
-	phase = "unknown";
+    case ACTION_CONTINUE:
 	break;
     }
+}
+
+/*
+ * Function : static void intr_bf (struct Scsi_Host *host, 
+ * 	struct NCR53c7x0_cmd *cmd)
+ *
+ * Purpose : handle BUS FAULT interrupts 
+ *
+ * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
+ * 	may be NULL.
+ */
+
+static void
+intr_bf (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
+    NCR53c7x0_local_declare();
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+	    host->hostdata;
+    unsigned long *dsp,
+	*next_dsp,		/* Current dsp */
+    	*dsa,
+	dbc_dcmd;		/* DCMD (high eight bits) + DBC */
+    unsigned short pci_status;
+    int tmp;
+    unsigned long flags;
+    char *reason = NULL;
+    /* Default behavior is for a silent error, with a retry until we've
+       exhausted retries. */
+    enum {MAYBE, ALWAYS, NEVER} retry = MAYBE;
+    int report = 0;
+    NCR53c7x0_local_setup(host);
 
+    dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
+    next_dsp = (unsigned long *) NCR53c7x0_read32(DSP_REG);
+    dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
+/* FIXME - check chip type  */
+    dsa = (unsigned long *) NCR53c7x0_read32(DSA_REG);
 
     /*
-     * The way the SCSI SCRIPTS(tm) are architected, recoverable phase
-     * mismatches should only occur in the data transfer routines, or
-     * when a command is being aborted.  
+     * Bus faults can be caused by either a Bad Address or 
+     * Target Abort. We should check the Received Target Abort
+     * bit of the PCI status register and Master Abort Bit.
+     *
+     * 	- Master Abort bit indicates that no device claimed
+     *		the address with DEVSEL within five clocks
+     *
+     *	- Target Abort bit indicates that a target claimed it,
+     *		but changed its mind once it saw the byte enables.
+     *
      */
-    if (dsp >= cmd->data_transfer_start & dsp < cmd->data_transfer_end) {
 
-	/*
-	 * There are three instructions used in our data transfer routines with
-	 * a phase conditional on them
-	 *
-	 * 1.  MOVE count, address, WHEN DATA_IN
-	 * 2.  MOVE count, address, WHEN DATA_OUT
-	 * 3.  CALL msg_in, WHEN MSG_IN.
-	 */
-	switch (sbcl & SBCL_PHASE_MASK) {
-	/*
-	 * 1.  STATUS phase : pass control to command_complete as if 
-	 *     a JUMP instruction was executed.  No patches are made.
-	 */
-	case SBCL_PHASE_STATIN:
-	    if (hostdata->options & OPTION_DEBUG_INTR) 
-		printk ("scsi%d : new phase = STATIN\n", host->host_no);
-	    hostdata->dsp = hostdata->script + hostdata->E_command_complete /
-    	    	sizeof(long);
-	    hostdata->dsp_changed = 1;
-	    return;
-	/*
-	 * 2.  MSGIN phase : pass control to msg_in as if a CALL
-	 *     instruction was executed.  Patch current instruction.
-	 */
-/* 
- * XXX - This is buggy.
- */
-	case SBCL_PHASE_MSGIN:
-	    if (hostdata->options & OPTION_DEBUG_INTR) 
-		printk ("scsi%d  : new phase = MSGIN\n", host->host_no);
-	    if ((dcmd & (DCMD_TYPE_MASK|DCMD_BMI_OP_MASK|DCMD_BMI_INDIRECT|
-		    DCMD_BMI_MSG|DCMD_BMI_CD)) == (DCMD_TYPE_BMI|
-		    DCMD_BMI_OP_MOVE_I)) {
-		dsp[0] = dbc_dcmd;
-		dsp[1] = NCR53c7x0_read32(DNAD_REG);
-		NCR53c7x0_write32(TEMP_REG, (unsigned long) dsp);
-		hostdata->dsp = hostdata->script + hostdata->E_msg_in /
-    	    	    sizeof(long);
-		hostdata->dsp_changed = 1;
-	    } else {
-		printk("scsi%d : unexpected MSGIN in dynamic NCR code, dcmd=0x%x.\n",
-		    host->host_no, dcmd);
-		print_insn (host, dsp, "", 1);
-		print_insn (host, dsp_next, "", 1);
-		abort_connected (host);
+    if ((hostdata->chip / 100) == 8) {
+	save_flags (flags);
+	cli();
+	tmp = pcibios_read_config_word (hostdata->pci_bus, 
+	    hostdata->pci_device_fn, PCI_STATUS, &pci_status);
+	restore_flags (flags);
+	if (tmp == PCIBIOS_SUCCESSFUL) {
+	    if (pci_status & PCI_STATUS_REC_TARGET_ABORT) {
+		reason = "PCI target abort";
+		pci_status &= ~PCI_STATUS_REC_TARGET_ABORT;
+	    } else if (pci_status & PCI_STATUS_REC_MASTER_ABORT) {
+		reason = "No device asserted PCI DEVSEL within five bus clocks";
+		pci_status &= ~PCI_STATUS_REC_MASTER_ABORT;
+	    } else if (pci_status & PCI_STATUS_PARITY) {
+		report = 1;   
+		pci_status &= ~PCI_STATUS_PARITY;
 	    }
-	    return;
-	/*
-	 * MSGOUT phase - shouldn't happen, because we haven't 
-	 *		asserted ATN.
-	 * CMDOUT phase - shouldn't happen, since we've already
-	 * 		sent a valid command.
-	 * DATAIN/DATAOUT - other one shouldn't happen, since 
-	 * 		SCSI commands can ONLY have one or the other.
-	 *
-	 * So, we abort the command if one of these things happens.
-	 */
-	default:
-	    printk ("scsi%d : unexpected phase %s in data routine\n",
-		host->host_no, phase);
-	    abort_connected(host);
-	} 
-    /*
-     * Any other phase mismatches abort the currently executing command.
+	} else {
+	    printk ("scsi%d : couldn't read status register : %s\n",
+		host->host_no, pcibios_strerror (tmp));
+	    retry = NEVER;
+	}
+    }
+
+#ifndef notyet
+    report = 1;
+#endif
+    if (report && reason) {
+	printk(KERN_ALERT "scsi%d : BUS FAULT, DSP=0x%lx, DCMD|DBC=0x%lx, DSA=0x%lx\n"
+	       "         DSPS=0x%lx, TEMP=0x%lx, DMODE=0x%x\n"
+	       "         reason = %s\n",
+	     host->host_no, (unsigned long) dsp, dbc_dcmd,
+	     (unsigned long) dsa, NCR53c7x0_read32(DSPS_REG),
+	     NCR53c7x0_read32(TEMP_REG), (int) NCR53c7x0_read8(hostdata->dmode),
+	     reason ? reason : "unknown");
+	print_dsa (host, dsa, KERN_ALERT "");
+	printk(KERN_ALERT "scsi%d : DSP->\n", host->host_no);
+	print_insn(host, dsp, KERN_ALERT "", 1);
+    	print_insn(host, next_dsp, KERN_ALERT "", 1);
+    }
+
+#ifndef notyet
+    retry = NEVER;
+#endif
+
+    /* 
+     * TODO : we should attempt to recover from any spurious bus 
+     * faults.  After X retries, we should figure that things are 
+     * sufficiently wedged, and call NCR53c7xx_reset.
+     *
+     * This code should only get executed once we've decided that we 
+     * cannot retry.
      */
-    } else {
-	printk ("scsi%d : unexpected phase %s at dsp = 0x%x\n",
-	    host->host_no, phase, (unsigned) dsp);
-	print_insn (host, dsp, "", 1);
-	print_insn (host, dsp_next, "", 1);
-	abort_connected(host);
+
+    if (retry == NEVER) {
+    	printk(KERN_ALERT "          mail drew@PoohSticks.ORG\n");
+    	FATAL (host);
     }
 }
 
@@ -3459,7 +4973,8 @@
  * 	may be NULL.
  */
 
-static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
+static void 
+intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
     NCR53c7x0_local_declare();
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;
@@ -3507,10 +5022,10 @@
 	} else 
 #endif
 	{
-	    printk("scsi%d : unexpected abort interrupt at\n" 
+	    printk(KERN_ALERT "scsi%d : unexpected abort interrupt at\n" 
 		   "         ", host->host_no);
-	    print_insn (host, dsp, "s ", 1);
-	    panic(" ");
+	    print_insn (host, dsp, KERN_ALERT "s ", 1);
+	    FATAL (host);
 	}
     }
 
@@ -3531,10 +5046,11 @@
     	    	~DCNTL_SSM) | DCNTL_STD);
 	    restore_flags(flags);
 	} else {
-	    printk("scsi%d : unexpected single step interrupt at\n"
+	    printk(KERN_ALERT "scsi%d : unexpected single step interrupt at\n"
 		   "         ", host->host_no);
-	    print_insn (host, dsp, "", 1);
-	    panic("         mail drew@colorad.edu\n");
+	    print_insn (host, dsp, KERN_ALERT "", 1);
+	    printk(KERN_ALERT "         mail drew@colorado.edu\n");
+    	    FATAL (host);
     	}
     }
 
@@ -3545,7 +5061,7 @@
      * 
      * XXX - we may want to emulate INTFLY here, so we can use 
      *    the same SCSI SCRIPT (tm) for NCR53c710 through NCR53c810  
-     *	  chips once we remove the ADD WITH CARRY instructions.
+     *	  chips.
      */
 
     if (dstat & DSTAT_OPC) {
@@ -3579,40 +5095,26 @@
 		hostdata->expecting_sto = 1;
 	    }
 	} else {
-	    printk("scsi%d : illegal instruction ", host->host_no);
-	    print_insn (host, dsp, "", 1);
-	    printk("scsi%d : DSP=0x%lx, DCMD|DBC=0x%lx, DSA=0x%lx\n"
+	    printk(KERN_ALERT "scsi%d : illegal instruction ", host->host_no);
+	    print_insn (host, dsp, KERN_ALERT "", 1);
+	    printk(KERN_ALERT "scsi%d : DSP=0x%lx, DCMD|DBC=0x%lx, DSA=0x%lx\n"
 	       "         DSPS=0x%lx, TEMP=0x%lx, DMODE=0x%x,\n" 
 	       "	 DNAD=0x%lx\n",
 	     host->host_no, (unsigned long) dsp, dbc_dcmd,
 	     (unsigned long) dsa, NCR53c7x0_read32(DSPS_REG),
 	     NCR53c7x0_read32(TEMP_REG), (int) NCR53c7x0_read8(hostdata->dmode),
 	     NCR53c7x0_read32(DNAD_REG));
-	    panic("         mail drew@Colorado.EDU\n");
+	    printk(KERN_ALERT "         mail drew@PoohSticks.ORG\n");
+    	    FATAL (host);
 	}
     }
 
     /* 
-     * DSTAT_BF are bus fault errors, generated when the chip has 
-     * attempted to access an illegal address.
+     * DSTAT_BF are bus fault errors
      */
     
     if (dstat & DSTAT_800_BF) {
-	printk("scsi%d : BUS FAULT, DSP=0x%lx, DCMD|DBC=0x%lx, DSA=0x%lx\n"
-	       "         DSPS=0x%lx, TEMP=0x%lx, DMODE=0x%x\n", 
-	     host->host_no, (unsigned long) dsp, NCR53c7x0_read32(DBC_REG),
-	     (unsigned long) dsa, NCR53c7x0_read32(DSPS_REG),
-	     NCR53c7x0_read32(TEMP_REG), (int) NCR53c7x0_read8(hostdata->dmode));
-	print_dsa (host, dsa);
-	printk("scsi%d : DSP->\n", host->host_no);
-	print_insn(host, dsp, "", 1);
-	print_insn(host, next_dsp, "", 1);
-#if 0
-	panic("          mail drew@Colorado.EDU\n");
-#else
-	hostdata->idle = 1;
-	hostdata->options |= OPTION_DEBUG_INIT_ONLY;
-#endif
+	intr_bf (host, cmd);
     }
 	
 
@@ -3635,24 +5137,28 @@
 	    abort_connected(host);
 	    break;
 	case SPECIFIC_INT_PANIC:
-	    printk("scsi%d : failure at ", host->host_no);
-	    print_insn (host, dsp, "", 1);
-	    panic("          dstat_sir_intr() returned SPECIFIC_INT_PANIC\n");
+	    printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
+	    print_insn (host, dsp, KERN_ALERT "", 1);
+	    printk(KERN_ALERT "          dstat_sir_intr() returned SPECIFIC_INT_PANIC\n");
+    	    FATAL (host);
 	    break;
 	case SPECIFIC_INT_BREAK:
 	    intr_break (host, cmd);
 	    break;
 	default:
-	    printk("scsi%d : failure at ", host->host_no);
-	    print_insn (host, dsp, "", 1);
-	    panic("          dstat_sir_intr() returned unknown value %d\n", 
+	    printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
+	    print_insn (host, dsp, KERN_ALERT "", 1);
+	    printk(KERN_ALERT"          dstat_sir_intr() returned unknown value %d\n", 
 		tmp);
+    	    FATAL (host);
 	}
     } 
 
-/* All DMA interrupts are fatal.  Flush SCSI queue */
-    NCR53c7x0_write8 (STEST3_REG_800, STEST3_800_CSF);
-    while (NCR53c7x0_read8 (STEST3_REG_800) & STEST3_800_CSF);
+    if ((hostdata->chip / 100) == 8 && (dstat & DSTAT_800_MDPE)) {
+	printk(KERN_ALERT "scsi%d : Master Data Parity Error\n",
+	    host->host_no);
+	FATAL (host);
+    }
 }
 
 /*
@@ -3672,23 +5178,42 @@
  * Returns : size, in longs, of instruction printed.
  */
 
-static int print_insn (struct Scsi_Host *host, unsigned long *insn,
+static int 
+print_insn (struct Scsi_Host *host, unsigned long *insn,
     char *prefix, int kernel) {
     char buf[80], 		/* Temporary buffer and pointer */
 	*tmp;			
     unsigned char dcmd;		/* dcmd register for *insn */
     int size;
 
-    dcmd = (insn[0] >> 24) & 0xff;
-    sprintf(buf, "%s%08lx : 0x%08lx 0x%08lx", (prefix ? prefix : ""), 
-	(unsigned long) insn, insn[0], insn[1]);
-    tmp = buf + strlen(buf);
-    if ((dcmd & DCMD_TYPE_MASK) == DCMD_TYPE_MMI)  {
-	sprintf (tmp, " 0x%08lx\n", insn[2]);
-	size = 3;
+    /* 
+     * Check to see if the instruction pointer is not bogus before 
+     * indirecting through it; avoiding red-zone at start of 
+     * memory.
+     *
+     * FIXME: icky magic needs to happen here on non-intel boxes which
+     * don't have kernel memory mapped in like this.  Might be reasonable
+     * to use vverify()?
+     */
+
+    if ((unsigned long) insn < PAGE_SIZE || 
+	(unsigned long) insn > (high_memory - 8) || 
+	((((dcmd = (insn[0] >> 24) & 0xff) & DCMD_TYPE_MMI) == DCMD_TYPE_MMI) &&
+	(unsigned long) insn > (high_memory - 12))) {
+	size = 0;
+	sprintf (buf, "%s%08lx : address out of range\n",
+	    prefix, (long) insn);
     } else {
-	sprintf (tmp, "\n");
-	size = 2;
+	sprintf(buf, "%s%08lx : 0x%08lx 0x%08lx", (prefix ? prefix : ""), 
+	    (unsigned long) insn, insn[0], insn[1]);
+	tmp = buf + strlen(buf);
+	if ((dcmd & DCMD_TYPE_MASK) == DCMD_TYPE_MMI)  {
+	    sprintf (tmp, " 0x%08lx\n", insn[2]);
+	    size = 3;
+	} else {
+	    sprintf (tmp, "\n");
+	    size = 2;
+	}
     }
 
     if (kernel) 
@@ -3703,6 +5228,28 @@
 }
 
 /*
+ * Function : static const char *ncr_state (int state)
+ *
+ * Purpose : convert state (probably from hostdata->state) to a string
+ *
+ * Inputs : state
+ *
+ * Returns : char * representation of state, "unknown" on error.
+ */
+
+static const char *
+ncr_state (int state) {
+    switch (state) {
+    case STATE_HALTED: return "halted";
+    case STATE_WAITING: return "waiting";
+    case STATE_RUNNING: return "running";
+    case STATE_ABORTING: return "aborting";
+    case STATE_DISABLED: return "disabled";
+    default: return "unknown";
+    }
+}
+
+/*
  * Function : int NCR53c7xx_abort (Scsi_Cmnd *cmd)
  * 
  * Purpose : Abort an errant SCSI command, doing all necessary
@@ -3714,14 +5261,38 @@
  * Returns : 0 on success, -1 on failure.
  */
 
-int NCR53c7xx_abort (Scsi_Cmnd *cmd) {
+int 
+NCR53c7xx_abort (Scsi_Cmnd *cmd) {
+    NCR53c7x0_local_declare();
     struct Scsi_Host *host = cmd->host;
-    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *) 
-	host->hostdata;
+    struct NCR53c7x0_hostdata *hostdata = host ? (struct NCR53c7x0_hostdata *) 
+	host->hostdata : NULL;
     unsigned long flags;
     struct NCR53c7x0_cmd *curr, **prev;
+    Scsi_Cmnd *me, **last;
+#if 0
+    static long cache_pid = -1;
+#endif
+    NCR53c7x0_local_setup(host);
+
+    if (!host) {
+	printk ("Bogus SCSI command pid %ld; no host structure\n",
+	    cmd->pid);
+	return SCSI_ABORT_ERROR;
+    } else if (!hostdata) {
+	printk ("Bogus SCSI host %d; no hostdata\n", host->host_no);
+	return SCSI_ABORT_ERROR;
+    }
+
     save_flags(flags);
     cli();
+#if 0
+    if (cache_pid == cmd->pid) 
+	panic ("scsi%d : bloody fetus %ld\n", host->host_no, cmd->pid);
+    else
+	cache_pid = cmd->pid;
+#endif
+	
 
 /*
  * The command could be hiding in the issue_queue.  This would be very
@@ -3733,23 +5304,24 @@
  * pull the command out of the old queue, and call it aborted.
  */
 
-    for (curr = (struct NCR53c7x0_cmd *) hostdata->issue_queue, 
-         prev = (struct NCR53c7x0_cmd **) &(hostdata->issue_queue);
-	 curr && curr->cmd != cmd; prev = (struct NCR53c7x0_cmd **)
-         &(curr->next), curr = (struct NCR53c7x0_cmd *) curr->next);
-
-    if (curr) {
-	*prev = (struct NCR53c7x0_cmd *) curr->next;
-/* XXX - get rid of DLL ? */
-	if (curr->prev)
-	    curr->prev->next = curr->next;
-
-    	curr->next = hostdata->free;
-    	hostdata->free = curr;
-
-	cmd->result = 0;
+    for (me = (Scsi_Cmnd *) hostdata->issue_queue, 
+         last = (Scsi_Cmnd **) &(hostdata->issue_queue);
+	 me && me != cmd;  last = (Scsi_Cmnd **)&(me->SCp.ptr), 
+	 me = (Scsi_Cmnd *)me->SCp.ptr);
+
+    if (me) {
+	*last = (Scsi_Cmnd *) me->SCp.ptr;
+	if (me->host_scribble) {
+	    ((struct NCR53c7x0_cmd *)me->host_scribble)->next = hostdata->free;
+	    hostdata->free = (struct NCR53c7x0_cmd *) me->host_scribble;
+	    me->host_scribble = NULL;
+	}
+	cmd->result = DID_ABORT << 16;
 	cmd->scsi_done(cmd);
+printk ("scsi%d : found command %ld in Linux issue queue\n", host->host_no,
+	me->pid);
 	restore_flags(flags);
+    	run_process_issue_queue();
 	return SCSI_ABORT_SUCCESS;
     }
 
@@ -3764,32 +5336,54 @@
          &(curr->next), curr = (struct NCR53c7x0_cmd *) curr->next);
 
     if (curr) {
-	restore_flags(flags);
-	printk ("scsi%d : DANGER : command in running list, can not abort.\n",
-	    cmd->host->host_no);
-	return SCSI_ABORT_SNOOZE;
+	if ((cmd->result & 0xff) != 0xff && (cmd->result & 0xff00) != 0xff00) {
+	    if (prev)
+		*prev = (struct NCR53c7x0_cmd *) curr->next;
+	    curr->next = (struct NCR53c7x0_cmd *) hostdata->free;
+	    cmd->host_scribble = NULL;
+	    hostdata->free = curr;
+	    cmd->scsi_done(cmd);
+printk ("scsi%d : found finished command %ld in running list\n", host->host_no,
+    cmd->pid);
+	    restore_flags(flags);
+	    return SCSI_ABORT_NOT_RUNNING;
+	} else {
+	    printk ("scsi%d : DANGER : command running, can not abort.\n",
+		cmd->host->host_no);
+	    restore_flags(flags);
+	    return SCSI_ABORT_BUSY;
+	}
     }
 
-
 /* 
  * And if we couldn't find it in any of our queues, it must have been 
  * a dropped interrupt.
  */
 
     curr = (struct NCR53c7x0_cmd *) cmd->host_scribble;
-    curr->next = hostdata->free;
-    hostdata->free = curr;
+    if (curr) {
+	curr->next = hostdata->free;
+	hostdata->free = curr;
+    }
+    cmd->host_scribble = NULL;
 
     if (((cmd->result & 0xff00) == 0xff00) ||
 	((cmd->result & 0xff) == 0xff)) {
 	printk ("scsi%d : did this command ever run?\n", host->host_no);
+	cmd->result = DID_ABORT << 16;
     } else {
-	printk ("scsi%d : probably lost INTFLY, normal completion\n", 
+	printk ("scsi%d : probably lost INTFLY, normal completion, unbusying lun\n", 
 	    host->host_no);
+	--hostdata->busy[cmd->target][cmd->lun];
     }
-    cmd->scsi_done(cmd);
     restore_flags(flags);
-    return SCSI_ABORT_SNOOZE;
+    cmd->scsi_done(cmd);
+
+/* 
+ * We need to run process_issue_queue since termination of this command 
+ * may allow another queued command to execute first? 
+ */
+    return SCSI_ABORT_NOT_RUNNING;
 }
 
 /*
@@ -3807,42 +5401,71 @@
 NCR53c7xx_reset (Scsi_Cmnd *cmd) {
     NCR53c7x0_local_declare();
     unsigned long flags;
-    int found;
+    int found = 0;
     struct NCR53c7x0_cmd * c;
     Scsi_Cmnd *tmp;
+    /*
+     * When we call scsi_done(), it's going to wake up anything sleeping on the
+     * resources which were in use by the aborted commands, and we'll start to 
+     * get new commands.
+     *
+     * We can't let this happen until after we've re-initialized the driver
+     * structures, and can't reinitilize those structures until after we've 
+     * dealt with their contents.
+     *
+     * So, we need to find all of the commands which were running, stick
+     * them on a linked list of completed commands (we'll use the host_scribble
+     * pointer), do our reinitialization, and then call the done function for
+     * each command.  
+     */
+    Scsi_Cmnd *nuke_list = NULL;
+
     struct Scsi_Host *host = cmd->host;
-    struct NCR53c7x0_hostdata *hostdata = host ? 
-    (struct NCR53c7x0_hostdata *) host->hostdata : NULL;
+    struct NCR53c7x0_hostdata *hostdata = 
+    	(struct NCR53c7x0_hostdata *) host->hostdata;
     NCR53c7x0_local_setup(host);
     save_flags(flags);
+    cli();
     halt (host);
-    NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
-    udelay(25);	/* Minimum amount of time to assert RST */
-    NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
-    for (c = (struct NCR53c7x0_cmd *) hostdata->running_list, found = 0; c; 
-    	c = (struct NCR53c7x0_cmd *) c->next)  {
-	tmp = c->cmd;
-    	c->next = hostdata->free;
-    	hostdata->free = c;
-
-	if (tmp == cmd)
-	    found = 1; 
-    	tmp->result = DID_RESET << 16;
-	tmp->scsi_done(tmp);
-    }
+    print_lots (host);
+    dump_events (host, 30);
+    ncr_scsi_reset (host);
+    for (tmp = nuke_list =  return_outstanding_commands (host, 1 /* free */,
+	0 /* issue */ ); tmp; tmp = (Scsi_Cmnd *) tmp->SCp.buffer)
+	if (tmp == cmd) {
+	    found = 1;
+	    break;
+	}
+	    
+    /* 
+     * If we didn't find the command which caused this reset in our running
+     * list, then we've lost it.  See that it terminates normally anyway.
+     */
     if (!found) {
     	c = (struct NCR53c7x0_cmd *) cmd->host_scribble;
     	if (c) {
     	    c->next = hostdata->free;
     	    hostdata->free = c;
     	}
-    	cmd->result = DID_RESET << 16;
-    	cmd->scsi_done(cmd);
+	cmd->SCp.buffer = (struct scatterlist *) nuke_list;
+	nuke_list = cmd;
     }
-    restore_flags(flags);
 
-    printk ("scsi%d : DANGER : NCR53c7xx_reset is NOP\n",
-	cmd->host->host_no);
+    NCR53c7x0_driver_init (host);
+    hostdata->soft_reset (host);
+    if (hostdata->resets == 0) 
+	disable(host);
+    else if (hostdata->resets != -1)
+	--hostdata->resets;
+/*** WHY???  This can temporarily re-enable even if the caller had disabled!
+    sti();
+***/
+    for (; nuke_list; nuke_list = tmp) {
+	tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
+    	nuke_list->result = DID_RESET << 16;
+	nuke_list->scsi_done (nuke_list);
+    }
+    restore_flags(flags);
     return SCSI_RESET_SUCCESS;
 }
 
@@ -3851,14 +5474,133 @@
  * therefore shares the scsicam_bios_param function.
  */
 
-static void print_dsa (struct Scsi_Host *host, unsigned long *dsa) {
+/*
+ * Function : int insn_to_offset (Scsi_Cmnd *cmd, unsigned long *insn)
+ *
+ * Purpose : convert instructions stored at NCR pointer into data 
+ *	pointer offset.
+ * 
+ * Inputs : cmd - SCSI command; insn - pointer to instruction.  Either current
+ *	DSP, or saved data pointer.
+ *
+ * Returns : offset on success, -1 on failure.
+ */
+
+
+static int 
+insn_to_offset (Scsi_Cmnd *cmd, unsigned long *insn) {
+    struct NCR53c7x0_hostdata *hostdata = 
+	(struct NCR53c7x0_hostdata *) cmd->host->hostdata;
+    struct NCR53c7x0_cmd *ncmd = 
+	(struct NCR53c7x0_cmd *) cmd->host_scribble;
+    int offset = 0, buffers;
+    struct scatterlist *segment;
+    char *ptr;
+    int found = 0;
+
+/*
+ * With the current code implementation, if the insn is inside dynamically 
+ * generated code, the data pointer will be the instruction preceeding 
+ * the next transfer segment.
+ */
+
+    if ((insn >= ncmd->data_transfer_start &&  
+    	    insn < ncmd->data_transfer_end) ||
+    	(insn >= ncmd->residual &&
+    	    insn < (ncmd->residual + 
+    	    	sizeof(ncmd->residual)))) {
+	    ptr = (char *) insn[3];
+
+	    if ((buffers = cmd->use_sg)) {
+    	    	for (offset = 0, 
+		     	segment = (struct scatterlist *) cmd->buffer;
+    	    	     buffers && !((found = ((ptr >= segment->address) && 
+    	    	    	    (ptr < (segment->address + segment->length)))));
+    	    	     --buffers, offset += segment->length, ++segment)
+#if 0
+		    printk("scsi%d: comparing 0x%lx to 0x%lx\n", 
+			cmd->host->host_no, (long) saved, (long)
+			segment->address);
+#else
+		    ;
+#endif
+    	    	    offset += ptr - segment->address;
+    	    } else {
+		found = 1;
+    	    	offset = ptr - (char *) (cmd->request_buffer);
+    	    }
+    } else if ((insn >= hostdata->script + 
+		hostdata->E_data_transfer / sizeof(unsigned long)) &&
+	       (insn <= hostdata->script +
+		hostdata->E_end_data_transfer / sizeof(unsigned long))) {
+    	found = 1;
+	offset = 0;
+    }
+    return found ? offset : -1;
+
+}
+
+
+
+/*
+ * Function : void print_progress (Scsi_Cmnd *cmd) 
+ * 
+ * Purpose : print the current location of the saved data pointer
+ *
+ * Inputs : cmd - command we are interested in
+ *
+ */
+
+static void 
+print_progress (Scsi_Cmnd *cmd) {
+    NCR53c7x0_local_declare();
+    struct NCR53c7x0_cmd *ncmd = 
+	(struct NCR53c7x0_cmd *) cmd->host_scribble;
+    int offset, i;
+    char *where;
+    unsigned long *ptr;
+    NCR53c7x0_local_setup (cmd->host);
+    for (i = 0; i < 2; ++i) {
+	if (!i) {
+	    where = "saved";
+	    ptr = ncmd->saved_data_pointer;
+	} else {
+	    where = "active";
+	    ptr = (unsigned long *) NCR53c7x0_read32 (DSP_REG) -
+		NCR53c7x0_insn_size (NCR53c7x0_read8 (DCMD_REG));
+	} 
+	offset = insn_to_offset (cmd, ptr);
+
+	if (offset != -1) 
+	    printk ("scsi%d : %s data pointer at offset %d\n",
+		cmd->host->host_no, where, offset);
+	else {
+	    int size;
+	    printk ("scsi%d : can't determine %s data pointer offset\n",
+		cmd->host->host_no, where);
+	    size = print_insn (cmd->host, ncmd->saved_data_pointer, "", 1);
+	    print_insn (cmd->host, ncmd->saved_data_pointer + size, "", 1);
+	}
+    }
+}
+
+
+static void 
+print_dsa (struct Scsi_Host *host, unsigned long *dsa,
+    const char *prefix) {
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;
     int i, len;
     char *ptr;
+    Scsi_Cmnd *cmd;
 
-    printk("scsi%d : dsa at 0x%x\n"
+    if (!dsa) {
+	printk("scsi%d : NULL dsa\n", host->host_no);
+	return;
+    }
+    printk("%sscsi%d : dsa at 0x%x\n"
 	    "        + %ld : dsa_msgout length = %lu, data = 0x%lx\n" ,
+    	    prefix ? prefix : "",
     	    host->host_no, (unsigned) dsa, hostdata->dsa_msgout,
     	    dsa[hostdata->dsa_msgout / sizeof(long)],
 	    dsa[hostdata->dsa_msgout / sizeof(long) + 1]);
@@ -3870,18 +5612,148 @@
 	len = print_msg (ptr);
 	printk("\n");
     }
+
+    printk("        + %ld : select_indirect = 0x%lx\n",
+	hostdata->dsa_select, dsa[hostdata->dsa_select / sizeof(long)]);
+    cmd = (Scsi_Cmnd *) dsa[hostdata->dsa_cmnd / sizeof(long)];
+    printk("        + %ld : dsa_cmnd = 0x%lx ", hostdata->dsa_cmnd, (long)
+	cmd);
+    if (cmd) {
+	printk("               target = %d, lun = %d, cmd = ",
+	    cmd->target, cmd->lun);
+	print_command(cmd->cmnd);
+    } else
+	printk("\n");
+    printk("        + %ld : dsa_next = 0x%lx\n", hostdata->dsa_next,
+	dsa[hostdata->dsa_next / sizeof(long)]);
+    if (cmd) { 
+	printk("scsi%d target %d : sxfer_sanity = 0x%x, scntl3_sanity = 0x%x\n"
+	       "                   script : ",
+	    host->host_no, cmd->target,
+	    hostdata->sync[cmd->target].sxfer_sanity,
+	    hostdata->sync[cmd->target].scntl3_sanity);
+	for (i = 0; i < (sizeof(hostdata->sync[cmd->target].script) / 4); ++i)
+	    printk ("0x%lx ", hostdata->sync[cmd->target].script[i]);
+	printk ("\n");
+    	print_progress (cmd);
+    }
 }
+/*
+ * Function : void print_queues (Scsi_Host *host) 
+ * 
+ * Purpose : print the contents of the NCR issue and reconnect queues
+ *
+ * Inputs : host - SCSI host we are interested in
+ *
+ */
+
+static void 
+print_queues (struct Scsi_Host *host) {
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+	host->hostdata;
+    unsigned long *dsa, *next_dsa;
+    int i;
+    Scsi_Cmnd *cmd, *next_cmd;
+    unsigned long flags;
+
+
+    printk ("scsi%d : issue queue\n", host->host_no);
+
+    for (cmd = (Scsi_Cmnd *) hostdata->issue_queue; cmd; cmd = next_cmd) {
+	next_cmd = (Scsi_Cmnd *) cmd->SCp.ptr;
+	save_flags(flags);
+	cli();
+	if (cmd->host_scribble)
+	    print_dsa (host, 
+		((struct NCR53c7x0_cmd *) cmd->host_scribble)->dsa, "");
+	else 
+	    printk ("scsi%d : command %ld for target %d lun %d has no dsa\n",
+		host->host_no, cmd->pid, cmd->target, cmd->lun);
+	restore_flags(flags);
+    }
+	    
+	    
+    for (i = 0; i < 2; ++i) {
+	printk ("scsi%d : %s_dsa_head :\n", host->host_no, 
+	    i ? "reconnect" : "issue");
+/*
+ * FIXME : we need to sanity check this and detect loops, an 
+ * extraordinary number of outstanding commands, etc.  Easiest 
+ * bet is to set a counter to the number of commands we support; 
+ * best is probably to implement some sort of loop detection.
+ */
+
+	for (dsa = (i ? (unsigned long *) hostdata->reconnect_dsa_head :
+	    	(unsigned long *) hostdata->issue_dsa_head); dsa; 
+		dsa = next_dsa) {
+	    save_flags (flags);
+	    cli();
+	    next_dsa = (unsigned long *) dsa[hostdata->dsa_next / sizeof(long)];
+	    print_dsa (host, dsa, "");
+	    restore_flags(flags);
+	}
+	printk ("scsi%d : end %s_dsa_head\n", host->host_no,
+	    i ? "reconnect" : "issue");
+    }
+}
+
+static void
+print_lots (struct Scsi_Host *host) {
+    NCR53c7x0_local_declare();
+    struct NCR53c7x0_hostdata *hostdata = 
+	(struct NCR53c7x0_hostdata *) host->hostdata;
+    unsigned long *dsp_next, *dsp, *dsa, dbc_dcmd;
+    unsigned char dcmd, sbcl;
+    int i, size;
+    NCR53c7x0_local_setup(host);
+
+    if ((dsp_next = (unsigned long *) NCR53c7x0_read32 (DSP_REG))) {
+    	dbc_dcmd = NCR53c7x0_read32(DBC_REG);
+    	dcmd = (dbc_dcmd & 0xff000000) >> 24;
+    	dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
+	dsa = (unsigned long *) NCR53c7x0_read32(DSA_REG);
+	sbcl = NCR53c7x0_read8 (SBCL_REG);
+	    
+	
+	if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) 
+	    printk ("scsi%d : connected (SDID=0x%x, SSID=0x%x)\n",
+		host->host_no, NCR53c7x0_read8 (SDID_REG_800),
+		NCR53c7x0_read8 (SSID_REG_800));
+    	printk ("scsi%d : DCMD|DBC=0x%lx, DSA=0x%lx\n"
+	        "         DSPS=0x%lx, TEMP=0x%lx, DMODE=0x%x\n"
+		"         SXFER=0x%x, SCNTL3=0x%x\n"
+		"         %s%s%sphase=%s, %d bytes in SCSI FIFO\n"
+		"         STEST0=0x%x\n",
+	    host->host_no, dbc_dcmd, (long) dsa, 
+	    NCR53c7x0_read32(DSPS_REG), NCR53c7x0_read32(TEMP_REG), 
+	    (int) NCR53c7x0_read8(hostdata->dmode),
+	    (int) NCR53c7x0_read8(SXFER_REG), 
+	    (int) NCR53c7x0_read8(SCNTL3_REG_800),
+	    (sbcl & SBCL_BSY) ? "BSY " : "",
+	    (sbcl & SBCL_SEL) ? "SEL " : "",
+	    (sbcl & SBCL_REQ) ? "REQ " : "",
+	    sstat2_to_phase(NCR53c7x0_read8 (((hostdata->chip / 100) == 8) ?
+	    	SSTAT1_REG : SSTAT2_REG)),
+	    (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ? 
+		SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT,
+	    NCR53c7x0_read8 (STEST0_REG_800));
+	printk ("scsi%d : DSP 0x%lx ->\n", host->host_no, (long) dsp);
+    	for (i = 6; i > 0; --i, dsp += size)
+	    size = print_insn (host, dsp, "", 1);
+	print_dsa (host, dsa, "");
+	print_queues (host);
+    }
+}
+
 
 /*
  * Function : static int shutdown (struct Scsi_Host *host)
  * 
  * Purpose : does a clean (we hope) shutdown of the NCR SCSI 
  *	chip.  Use prior to dumping core, unloading the NCR driver,
- *	etc.
  * 
  * Returns : 0 on success
  */
-#ifdef MODULE
 static int 
 shutdown (struct Scsi_Host *host) {
     NCR53c7x0_local_declare();
@@ -3891,23 +5763,169 @@
     NCR53c7x0_local_setup(host);
     save_flags (flags);
     cli();
+/* Get in a state where we can reset the SCSI bus */
     halt (host);
+    ncr_scsi_reset (host);
     hostdata->soft_reset(host);
-/* 
- * For now, we take the simplest solution : reset the SCSI bus. Eventually,
- * - If a command is connected, kill it with an ABORT message
- * - If commands are disconnected, connect to each target/LUN and 
- *	do a ABORT, followed by a SOFT reset, followed by a hard 
- *	reset.  
+
+    disable (host);
+    restore_flags (flags);
+    return 0;
+}
+
+/*
+ * Function : void ncr_scsi_reset (struct Scsi_Host *host)
+ *
+ * Purpose : reset the SCSI bus.
  */
+
+static void 
+ncr_scsi_reset (struct Scsi_Host *host) {
+    NCR53c7x0_local_declare();
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+	host->hostdata;
+    unsigned long flags;
+    int sien = 0;
+    NCR53c7x0_local_setup(host);
+    save_flags (flags);
+    cli();
+    if ((hostdata->chip / 100) == 8) {
+	sien = NCR53c7x0_read8(SIEN0_REG_800);
+	NCR53c7x0_write8(SIEN0_REG_800, sien & ~SIEN_RST);
+    }
     NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
     udelay(25);	/* Minimum amount of time to assert RST */
-    NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
+    NCR53c7x0_write8(SCNTL1_REG, 0);
+    if ((hostdata->chip / 100) == 8) {
+	NCR53c7x0_write8(SIEN0_REG_800, sien);
+    }
     restore_flags (flags);
-    return 0;
 }
-#endif
 
+/* 
+ * Function : void hard_reset (struct Scsi_Host *host)
+ *
+ */
+
+static void 
+hard_reset (struct Scsi_Host *host) {
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+	host->hostdata;
+    unsigned long flags;
+    save_flags (flags);
+    cli();
+    ncr_scsi_reset(host);
+    NCR53c7x0_driver_init (host);
+    if (hostdata->soft_reset)
+	hostdata->soft_reset (host);
+    restore_flags(flags);
+}
+
+
+/*
+ * Function : Scsi_Cmnd *return_outstanding_commands (struct Scsi_Host *host,
+ *	int free, int issue)
+ *
+ * Purpose : return a linked list (using the SCp.buffer field as next)
+ *	of Scsi_Cmnd structures that had propogated bellow the 
+ *	Linux issue queue level.  If free is set, free the 
+ * 	NCR53c7x0_cmd structures which are associated with 
+ *	the Scsi_Cmnd structures, and clean up any internal 
+ *	NCR lists that the commands were on.  If issue is set,
+ *	also return commands in the issue queue.
+ *
+ * Returns : linked list of commands.
+ *
+ * NOTE : the caller should insure that the NCR chip is halted
+ *	if the free flag is set. 
+ */
+
+static Scsi_Cmnd *
+return_outstanding_commands (struct Scsi_Host *host, int free, int issue) {
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+	host->hostdata;
+    struct NCR53c7x0_cmd *c;
+    Scsi_Cmnd *list = NULL, *tmp;
+    for (c = (struct NCR53c7x0_cmd *) hostdata->running_list; c; 
+    	c = (struct NCR53c7x0_cmd *) c->next)  {
+	if (c->cmd->SCp.buffer) {
+	    printk ("scsi%d : loop detected in running list!\n", host->host_no);
+	    break;
+	} else {
+	    printk ("The sti() implicit in a printk() prevents hangs\n");
+	    break;
+	}
+
+	c->cmd->SCp.buffer = (struct scatterlist *) list;
+	list = c->cmd;
+	if (free) {
+	    c->prev = NULL;
+    	    c->next = hostdata->free;
+    	    hostdata->free = c;
+	}
+    }
+    if (free) {
+	hostdata->reconnect_dsa_head = NULL;
+	hostdata->issue_dsa_head = NULL;
+	hostdata->issue_dsa_tail = NULL;
+    }
+
+    if (issue) {
+	for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; tmp = tmp->next) {
+	    if (tmp->SCp.buffer) {
+		printk ("scsi%d : loop detected in issue queue!\n", 
+			host->host_no);
+		break;
+	    }
+	    tmp->SCp.buffer = (struct scatterlist *) list;
+	    list = tmp;
+	}
+	if (free)
+	    hostdata->issue_queue = NULL;
+		
+    }
+    return list;
+}
+
+/* 
+ * Function : static int disable (struct Scsi_Host *host)
+ *
+ * Purpose : disables the given NCR host, causing all commands
+ * 	to return a driver error.  Call this so we can unload the
+ * 	module during development and try again.  Eventually, 
+ * 	we should be able to find clean workarrounds for these
+ * 	problems.
+ *
+ * Inputs : host - hostadapter to twiddle
+ *
+ * Returns : 0 on success.
+ */
+
+static int 
+disable (struct Scsi_Host *host) {
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+	host->hostdata;
+    unsigned long flags;
+    Scsi_Cmnd *nuke_list, *tmp;
+    save_flags(flags);
+    cli();
+    if (hostdata->state != STATE_HALTED)
+	halt (host);
+    nuke_list = return_outstanding_commands (host, 1 /* free */, 1 /* issue */);
+    hard_reset (host);
+    hostdata->state = STATE_DISABLED;
+    restore_flags(flags);
+    printk ("scsi%d : nuking commands\n", host->host_no);
+    for (; nuke_list; nuke_list = tmp) {
+	    tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
+	    nuke_list->result = DID_ERROR << 16;
+	    nuke_list->scsi_done(nuke_list);
+    }
+    printk ("scsi%d : done. \n", host->host_no);
+    printk (KERN_ALERT "scsi%d : disabled.  Unload and reload\n",
+    	host->host_no);
+    return 0;
+}
 
 /*
  * Function : static int halt (struct Scsi_Host *host)
@@ -3926,13 +5944,21 @@
     unsigned char istat, tmp;
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 	host->hostdata;
+    int stage;
     NCR53c7x0_local_setup(host);
 
     save_flags(flags);
     cli();
-    NCR53c7x0_write8(hostdata->istat, ISTAT_ABRT);
-    /* Eat interrupts until we find what we're looking for */
-    for (;;) {
+    /* Stage 0 : eat all interrupts
+       Stage 1 : set ABORT
+       Stage 2 : eat all but abort interrupts
+       Stage 3 : eat all interrupts
+     */
+    for (stage = 0;;) {
+	if (stage == 1) {
+	    NCR53c7x0_write8(hostdata->istat, ISTAT_ABRT);
+	    ++stage;
+	}
 	istat = NCR53c7x0_read8 (hostdata->istat);
 	if (istat & ISTAT_SIP) {
 	    if ((hostdata->chip / 100) == 8) {
@@ -3943,23 +5969,104 @@
 		tmp = NCR53c7x0_read8(SSTAT0_REG);
 	    }
 	} else if (istat & ISTAT_DIP) {
-            NCR53c7x0_write8(hostdata->istat, 0);
 	    tmp = NCR53c7x0_read8(DSTAT_REG);
-	    if (tmp & DSTAT_ABRT)
-		break;
-	    else
-		panic("scsi%d: could not halt NCR chip\n", host->host_no);
+	    if (stage == 2) {
+		if (tmp & DSTAT_ABRT) {
+		    NCR53c7x0_write8(hostdata->istat, 0);
+		    ++stage;
+		} else {
+		    printk(KERN_ALERT "scsi%d : could not halt NCR chip\n", 
+			host->host_no);
+		    disable (host);
+	    	}
+    	    }
 	}
+	if (!(istat & (ISTAT_SIP|ISTAT_DIP))) 
+	    if (stage == 0)
+	    	++stage;
+	    else if (stage == 3)
+		break;
     }
     hostdata->state = STATE_HALTED;
     restore_flags(flags);
+#if 0
+    print_lots (host);
+#endif
     return 0;
 }
 
+static const char *
+event_name (int event) {
+    switch (event) {
+    case EVENT_NONE:		return "none";
+    case EVENT_ISSUE_QUEUE:	return "to issue queue";
+    case EVENT_START_QUEUE:	return "to start queue";
+    case EVENT_SELECT:		return "selected";
+    case EVENT_DISCONNECT:	return "disconnected";
+    case EVENT_RESELECT:	return "reselected";
+    case EVENT_COMPLETE:	return "completed";
+    case EVENT_IDLE:		return "idle";
+    case EVENT_SELECT_FAILED:	return "select failed";
+    case EVENT_BEFORE_SELECT:	return "before select";
+    case EVENT_RESELECT_FAILED:	return "reselect failed";
+    default:			return "unknown";
+    }
+}
+
+/*
+ * Function : void dump_events (struct Scsi_Host *host, count)
+ *
+ * Purpose : print last count events which have occurred.
+ */ 
+static void
+dump_events (struct Scsi_Host *host, int count) {
+    struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
+	host->hostdata;
+    struct NCR53c7x0_event event;
+    int i;
+    unsigned long flags;
+    if (hostdata->events) {
+	if (count > hostdata->event_size)
+	    count = hostdata->event_size;
+	for (i = hostdata->event_index; count > 0; 
+	    i = (i ? i - 1 : hostdata->event_size -1), --count) {
+	    save_flags(flags);
+/*
+ * By copying the event we're currently examinging with interrupts
+ * disabled, we can do multiple printk(), etc. operations and 
+ * still be guaranteed that they're happening on the same 
+ * event structure.
+ */
+	    cli();
+#if 0
+	    event = hostdata->events[i];
+#else
+	    memcpy ((void *) &event, (void *) &(hostdata->events[i]),
+		sizeof(event));
+#endif
+
+	    restore_flags(flags);
+	    printk ("scsi%d : %s event %d at %ld secs %ld usecs target %d lun %d\n",
+		host->host_no, event_name (event.event), count,
+		event.time.tv_sec, event.time.tv_usec, event.target, 
+		event.lun);
+	    if (event.dsa) 
+		printk ("         event for dsa 0x%lx\n", (long) event.dsa);
+	    if (event.pid != -1) {
+		printk ("         event for pid %ld ", event.pid);
+		print_command (event.cmnd);
+	    }
+	}
+    }
+}
+
 #ifdef MODULE
-int NCR53c7x0_release(struct Scsi_Host *host) {
+int 
+NCR53c7x0_release(struct Scsi_Host *host) {
+    struct NCR53c7x0_hostdata *hostdata = 
+	(struct NCR53c7x0_hostdata *) host->hostdata;
+    struct NCR53c7x0_cmd *cmd, *tmp;
     shutdown (host);
-/* FIXME : need to recursively free tpnt structure */
     if (host->irq != IRQ_NONE)
 	{
 	    int irq_count;
@@ -3972,6 +6079,20 @@
 	}
     if (host->dma_channel != DMA_NONE)
 	free_dma(host->dma_channel);
+    if (host->io_port)
+	release_region(host->io_port, host->n_io_port);
+    
+    for (cmd = (struct NCR53c7x0_cmd *) hostdata->free; cmd; cmd = tmp, 
+	--hostdata->num_cmds) {
+	tmp = (struct NCR53c7x0_cmd *) cmd->next;
+	if (cmd->free)
+	    cmd->free ((void *) cmd->real, cmd->size);
+    }
+    if (hostdata->num_cmds)
+	printk ("scsi%d : leaked %d NCR53c7x0_cmd structures\n",
+	    host->host_no, hostdata->num_cmds);
+    if (hostdata->events) 
+	vfree ((void *)hostdata->events);
     return 1;
 }
 Scsi_Host_Template driver_template = NCR53c7xx;
diff -u -r /usr/src/linux-1.2.13/drivers/scsi/53c7,8xx.h /usr/src/linux/drivers/scsi/53c7,8xx.h
--- /usr/src/linux-1.2.13/drivers/scsi/53c7,8xx.h	Wed Oct 25 13:23:28 1995
+++ /usr/src/linux/drivers/scsi/53c7,8xx.h	Fri Oct 27 17:29:01 1995
@@ -6,10 +6,10 @@
  *	Hannover, Germany
  *	hm@ix.de	
  *
- * Copyright 1993, Drew Eckhardt
+ * Copyright 1993, 1994, 1995 Drew Eckhardt
  *      Visionary Computing 
  *      (Unix and Linux consulting and custom programming)
- *      drew@Colorado.EDU
+ *      drew@PoohSticks.ORG
  *	+1 (303) 786-7975
  *
  * TolerANT and SCSI SCRIPTS are registered trademarks of NCR Corporation.
@@ -58,11 +58,11 @@
 #define NCR53c7xx_release NULL
 #endif
 
-#define NCR53c7xx {NULL, NULL, "NCR53c{7,8}xx (rel 4)", NCR53c7xx_detect, 	\
+#define NCR53c7xx {NULL, NULL, "NCR53c{7,8}xx (rel 11)", NCR53c7xx_detect,\
     	NULL, /* info */ NULL, /* command, deprecated */ NULL, 		\
 	NCR53c7xx_queue_command, NCR53c7xx_abort, NCR53c7xx_reset,	\
-        NULL /* slave attach */, scsicam_bios_param, /* can queue */ 1, \
-	/* id */ 7, 127 /* old SG_ALL */, /* cmd per lun */ 1 , 	\
+        NULL /* slave attach */, scsicam_bios_param, /* can queue */ 16,\
+	/* id */ 7, 127 /* old SG_ALL */, /* cmd per lun */ 1, 		\
         /* present */ 0, /* unchecked isa dma */ 0, DISABLE_CLUSTERING} 
 #endif /* defined(HOSTS_C) || defined(MODULE) */ 
 
@@ -92,7 +92,7 @@
 #define SCNTL1_DHP_800		0x20	/* Disable halt on parity error or ATN
 					   target mode only */
 #define SCNTL1_CON		0x10	/* Connected */
-#define SCNTL1_RST		0x08	/*  SCSI RST/ */
+#define SCNTL1_RST		0x08	/* SCSI RST/ */
 #define SCNTL1_AESP		0x04	/* Force bad parity */
 #define SCNTL1_SND_700		0x02	/* Start SCSI send */
 #define SCNTL1_IARB_800		0x02	/* Immediate Arbitration, start
@@ -177,15 +177,16 @@
 #define SXFER_TP1		0x20
 #define SXFER_TP0		0x10	/* lsb */
 #define SXFER_TP_MASK		0x70
-#define SXFER_TP_SHIFT		4
+/* FIXME : SXFER_TP_SHIFT == 5 is right for '8xx chips */
+#define SXFER_TP_SHIFT		5
 #define SXFER_TP_4		0x00	/* Divisors */
-#define SXFER_TP_5		0x10
-#define SXFER_TP_6		0x20
-#define SXFER_TP_7		0x30
-#define SXFER_TP_8		0x40
-#define SXFER_TP_9		0x50
-#define SXFER_TP_10		0x60
-#define SXFER_TP_11		0x70
+#define SXFER_TP_5		0x10<<1
+#define SXFER_TP_6		0x20<<1
+#define SXFER_TP_7		0x30<<1
+#define SXFER_TP_8		0x40<<1
+#define SXFER_TP_9		0x50<<1
+#define SXFER_TP_10		0x60<<1
+#define SXFER_TP_11		0x70<<1
 
 #define SXFER_MO3		0x08	/* Max offset msb */
 #define SXFER_MO2		0x04
@@ -313,6 +314,7 @@
 #define DSTAT_800_IID		0x01	/* Same thing, different name */
 
 
+/* NCR53c800 moves this stuff into SIST0 */
 #define SSTAT0_REG		0x0d	/* SCSI status 0 ro */
 #define SIST0_REG_800		0x42	
 #define SSTAT0_MA		0x80	/* ini : phase mismatch,
@@ -328,6 +330,8 @@
 #define SSTAT0_RST		0x02	/* SCSI RST/ received */
 #define SSTAT0_PAR		0x01	/* Parity error */
 
+/* And uses SSTAT0 for what was SSTAT1 */
+
 #define SSTAT1_REG		0x0e	/* SCSI status 1 ro */
 #define SSTAT1_ILF		0x80	/* SIDL full */
 #define SSTAT1_ORF		0x40	/* SODR full */
@@ -344,6 +348,7 @@
 #define SSTAT2_FF1		0x20	
 #define SSTAT2_FF0		0x10
 #define SSTAT2_FF_MASK		0xf0
+#define SSTAT2_FF_SHIFT		4
 
 /* 
  * Latched signals, latched on the leading edge of REQ/ for initiators,
@@ -353,6 +358,13 @@
 #define SSTAT2_MSG		0x04	/* MSG */
 #define SSTAT2_CD		0x02	/* C/D */
 #define SSTAT2_IO		0x01	/* I/O */
+#define SSTAT2_PHASE_CMDOUT	SSTAT2_CD
+#define SSTAT2_PHASE_DATAIN	SSTAT2_IO
+#define SSTAT2_PHASE_DATAOUT	0
+#define SSTAT2_PHASE_MSGIN	(SSTAT2_CD|SSTAT2_IO|SSTAT2_MSG)
+#define SSTAT2_PHASE_MSGOUT	(SSTAT2_CD|SSTAT2_MSG)
+#define SSTAT2_PHASE_STATIN	(SSTAT2_CD|SSTAT2_IO)
+#define SSTAT2_PHASE_MASK	(SSTAT2_CD|SSTAT2_IO|SSTAT2_MSG)
 
 
 /* NCR53c700-66 only */
@@ -898,9 +910,9 @@
 #define OPTION_MEMORY_MAPPED	0x800	/* NCR registers have valid 
 					   memory mapping */
 #define OPTION_IO_MAPPED	0x1000  /* NCR registers have valid
-					   I/O mapping */
-#define OPTION_DEBUG_PROBE_ONLY	0x2000	/* Probe only, don't even init */
-#define OPTION_DEBUG_TESTS_ONLY	0x4000	/* Probe, init, run selected tests */
+					     I/O mapping */
+#define OPTION_DEBUG_PROBE_ONLY	0x2000  /* Probe only, don't even init */
+#define OPTION_DEBUG_TESTS_ONLY	0x4000  /* Probe, init, run selected tests */
 
 #define OPTION_DEBUG_TEST0	0x08000 /* Run test 0 */
 #define OPTION_DEBUG_TEST1	0x10000 /* Run test 1 */
@@ -913,17 +925,38 @@
 #define OPTION_DEBUG_FIXUP 0x400000 /* print fixup values */
 #define OPTION_DEBUG_DSA 0x800000
 #define OPTION_DEBUG_CORRUPTION	0x1000000	/* Detect script corruption */
-
+#define OPTION_DEBUG_SDTR       0x2000000	/* Debug SDTR problem */
+#define OPTION_DEBUG_MISMATCH 	0x4000000 	/* Debug phase mismatches */
+#define OPTION_DISCONNECT	0x8000000	/* Allow disconect */
+#define OPTION_DEBUG_DISCONNECT 0x10000000	
+#define OPTION_ALWAYS_SYNCHRONOUS 0x20000000	/* Negotiate sync. transfers
+						   on power up */
+#define OPTION_SCSI_MALLOC	0x40000000	/* Use scsi_malloc instead of
+						   kmalloc() to allocate NCR
+						   command structures after 
+						   boot */
+#define OPTION_DEBUG_QUEUES	0x80000000	
+#define OPTION_DEBUG_ALLOCATION 0x100000000LL
+#define OPTION_DEBUG_SYNCHRONOUS 0x200000000LL	/* Sanity check SXFER and 
+						   SCNTL3 registers */
+#define OPTION_NO_ASYNC	0x400000000LL		/* Don't automagically send
+						   SDTR for async transfers when
+						   we haven't been told to do
+						   a synchronous transfer. */
 #if !defined(PERM_OPTIONS)
 #define PERM_OPTIONS 0
 #endif
 				
 struct NCR53c7x0_synchronous {
     unsigned long select_indirect;	/* Value used for indirect selection */
-    unsigned long script[6];		/* Size ?? Script used when target is 
+    unsigned long script[8];		/* Size ?? Script used when target is 
 						reselected */
-    unsigned renegotiate:1;		/* Force renegotiation on next   
-					   select */
+    unsigned char synchronous_want[5];	/* Per target desired SDTR */
+/* 
+ * Set_synchronous programs these, select_indirect and current settings after
+ * int_debug_should show a match.
+ */
+    unsigned char sxfer_sanity, scntl3_sanity;
 };
 
 #define CMD_FLAG_SDTR 		1	/* Initiating synchronous 
@@ -931,15 +964,64 @@
 #define CMD_FLAG_WDTR		2	/* Initiating wide transfer
 					   negotiation */
 #define CMD_FLAG_DID_SDTR	4	/* did SDTR */
+#define CMD_FLAG_DID_WDTR	8	/* did WDTR */
 
 struct NCR53c7x0_table_indirect {
     unsigned long count;
     void *address;
 };
 
+enum ncr_event { 
+    EVENT_NONE = 0,
+    EVENT_ISSUE_QUEUE = 0x5000000,	/* Command was added to issue queue */
+    EVENT_START_QUEUE,			/* Command moved to start queue */
+    EVENT_SELECT,			/* Command completed selection */
+    EVENT_DISCONNECT,			/* Command disconnected */
+    EVENT_RESELECT,			/* Command reselected */
+    EVENT_COMPLETE,		        /* Command completed */
+    EVENT_IDLE,
+    EVENT_SELECT_FAILED,
+    EVENT_BEFORE_SELECT,
+    EVENT_RESELECT_FAILED
+};
+
+struct NCR53c7x0_event {
+    enum ncr_event event;	/* What type of event */
+    unsigned char target;
+    unsigned char lun;
+    struct timeval time;	
+    unsigned long *dsa;		/* What's in the DSA register now ? */
+/* 
+ * A few things from that SCSI pid so we know what happened after 
+ * the Scsi_Cmnd structure in question may have disappeared.
+ */
+    unsigned long pid;		/* The SCSI PID which caused this 
+				   event */
+    unsigned char cmnd[12];
+};
+
+/*
+ * Things in the NCR53c7x0_cmd structure are split into two parts :
+ *
+ * 1.  A fixed portion, for things which are not accessed directly by static NCR
+ *	code (ie, are referenced only by the Linux side of the driver,
+ *	or only by dynamically genreated code).  
+ *
+ * 2.  The DSA portion, for things which are accessed directly by static NCR
+ *	code.
+ *
+ * This is a little ugly, but it 
+ * 1.  Avoids conflicts between the NCR code's picture of the structure, and 
+ * 	Linux code's idea of what it looks like.
+ *
+ * 2.  Minimizes the pain in the Linux side of the code needed 
+ * 	to calculate real dsa locations for things, etc.
+ * 
+ */
+
 struct NCR53c7x0_cmd {
     void *real;				/* Real, unaligned address */
-    void (* free)(void *);		/* Command to deallocate; NULL
+    void (* free)(void *, int);		/* Command to deallocate; NULL
 					   for structures allocated with
 					   scsi_register, etc. */
     Scsi_Cmnd *cmd;			/* Associated Scsi_Cmnd 
@@ -952,6 +1034,14 @@
 
     int flags;
 
+/*
+ * SDTR and WIDE messages are an either/or affair
+ * in this message, since we will go into message out and send
+ * _the whole mess_ without dropping out of message out to 
+ * let the target go into message in after sending the first 
+ * message.
+ */
+
     unsigned char select[11];		/* Select message, includes
 					   IDENTIFY
 					   (optional) QUEUE TAG
@@ -969,14 +1059,38 @@
     unsigned long *data_transfer_start;	/* Start of data transfer routines */
     unsigned long *data_transfer_end;	/* Address after end of data transfer o
     	    	    	    	    	   routines */
+/* 
+ * The following three fields were moved from the DSA propper to here
+ * since only dynamically generated NCR code refers to them, meaning
+ * we don't need dsa_* absolutes, and it is simpler to let the 
+ * host code refer to them directly.
+ */
 
-    unsigned long residual[8];		/* Residual data transfer
-					   shadow of data_transfer code.
-
-					   Has instruction with modified
-					   DBC field followed by jump to 
-					   CALL routine following command.
-					 */
+/* 
+ * HARD CODED : residual and saved_residual need to agree with the sizes
+ * used in NCR53c7,8xx.scr.
+ */
+    unsigned long residual[6];		/* Residual data transfer which
+    	    	    	    	    	    allows pointer code to work
+    	    	    	    	    	    right.
+
+    	    	    	    	    	    [0-1] : Conditional call to 
+    	    	    	    	    	    	appropriate other transfer 
+    	    	    	    	    	    	routine.
+    	    	    	    	    	    [2-3] : Residual block transfer
+    	    	    	    	    	    	instruction.
+    	    	    	    	    	    [4-5] : Jump to instruction
+    	    	    	    	    	    	after splice.
+					 */
+    unsigned long saved_residual[6]; 	/* Copy of old residual, so we 
+    	    	    	    	    	    can get another partial 
+    					    transfer and still recover 
+    	    	    	    	    	 */
+    	    	
+    unsigned long * saved_data_pointer;	/* Saved data pointer */
+    unsigned long **dsa_next;		/* _Address_ of dsa_next field  
+					   in this dsa for RISCy 
+					   style constant. */
 	     
     unsigned long dsa[0];		/* Variable length (depending
 					   on host type, number of scatter /
@@ -1003,10 +1117,10 @@
  * Indicates that the NCR was being aborted.
  */
 #define STATE_ABORTING	3
-/* 
- * Indicates that the NCR was successfully aborted. */
+/* Indicates that the NCR was successfully aborted. */
 #define STATE_ABORTED 4
-    
+/* Indicates that the NCR has been disabled due to a fatal error */
+#define STATE_DISABLED 5
 
 /* 
  * Where knowledge of SCSI SCRIPT(tm) specified values are needed 
@@ -1109,9 +1223,9 @@
     long dsa_msgout_other;
     long dsa_write_sync;
     long dsa_write_resume;
-    long dsa_jump_resume;
     long dsa_check_reselect;
     long dsa_status;
+    long dsa_saved_pointer;
 
     /* 
      * Important entry points that generic fixup code needs
@@ -1119,12 +1233,16 @@
      */
 
     long E_accept_message;
+    long E_command_complete;		
+    long E_data_transfer;
     long E_dsa_code_template;
     long E_dsa_code_template_end;
-    long E_command_complete;		
+    long E_end_data_transfer;
     long E_msg_in;
     long E_initiator_abort;
     long E_other_transfer;
+    long E_other_in;
+    long E_other_out;
     long E_target_abort;
     long E_schedule;			
     long E_debug_break;	
@@ -1137,9 +1255,9 @@
     long E_test_2;
     long E_test_3;
     long E_dsa_zero;
-    long E_dsa_jump_resume;
+    long E_cmdout_cmdout;
 
-    int options;			/* Bitfielded set of options enabled */
+    long long options;			/* Bitfielded set of options enabled */
     long test_completed;		/* Test completed */
     int test_running;			/* Test currently running */
     int test_source;
@@ -1164,6 +1282,7 @@
 					 */
 
     volatile int intrs;			/* Number of interrupts */
+    volatile int resets;		/* Number of SCSI resets */
     unsigned char saved_dmode;	
     unsigned char saved_ctest4;
     unsigned char saved_ctest7;
@@ -1212,12 +1331,13 @@
     
     volatile struct NCR53c7x0_synchronous sync[8];
 
-    volatile struct NCR53c7x0_cmd *issue_queue;
+    volatile Scsi_Cmnd *issue_queue;
 						/* waiting to be issued by
 						   Linux driver */
     volatile struct NCR53c7x0_cmd *running_list;	
 						/* commands running, maintained
 						   by Linux driver */
+
     volatile struct NCR53c7x0_cmd *current;	/* currently connected 
 						   nexus, ONLY valid for
 						   NCR53c700/NCR53c700-66
@@ -1234,6 +1354,7 @@
 						   */
     volatile int num_cmds;			/* Number of commands 
 						   allocated */
+    volatile int extra_allocate;
     volatile unsigned char cmd_allocated[8];	/* Have we allocated commands
 						   for this target yet?  If not,
 						   do so ASAP */
@@ -1260,9 +1381,21 @@
     volatile unsigned char msg_buf[16];		/* buffer for messages
 						   other than the command
 						   complete message */
-    volatile unsigned char *reconnect_dsa_head;	
-						/* disconnected commands,
-						   maintained by NCR */
+
+    /* Per-host adapter default synchronous parameters */
+    
+    unsigned char synchronous_want[5];
+
+    /* Bit fielded set of targets we want to speak synchronously with */ 
+    volatile unsigned char initiate_sdtr;	
+    /* Bit fielded list of targets we've talked to. */
+    volatile unsigned char talked_to;
+    /* Array of bit-fielded lun lists that we need to request_sense */
+    volatile unsigned char request_sense[8];
+
+    unsigned long **addr_reconnect_dsa_head;	/* RISCy style constant,
+						   address of following */
+    volatile unsigned long *reconnect_dsa_head;	
     /* Data identifying nexus we are trying to match during reselection */
     volatile unsigned char reselected_identify; /* IDENTIFY message */
     volatile unsigned char reselected_tag;	/* second byte of queue tag 
@@ -1275,6 +1408,16 @@
     char NCR53c7xx_msg_abort;
     char NCR53c7xx_msg_nop;
 
+    volatile int event_size, event_index;
+    volatile struct NCR53c7x0_event *events;
+
+    /* If we need to generate code to kill off the currently connected 
+       command, this is where we do it. Should have a BMI instruction
+       to source or sink the current data, followed by a JUMP
+       to abort_connected */
+
+    unsigned long *abort_script;
+
     int script_count;				/* Size of script in longs */
     unsigned long script[0];			/* Relocated SCSI script */
 
@@ -1335,6 +1478,7 @@
 	  (value) : 							\
 	outl((value), NCR53c7x0_address_io + (address)))
 
+/* Patch arbitrary 32 bit long words in the script */
 #define patch_abs_32(script, offset, symbol, value)			\
     	for (i = 0; i < (sizeof (A_##symbol##_used) / sizeof 		\
     	    (unsigned long)); ++i) {					\
@@ -1346,6 +1490,7 @@
 		(offset)]);						\
     	}
 
+/* Patch read/write instruction immediate field */
 #define patch_abs_rwri_data(script, offset, symbol, value)	        \
     	for (i = 0; i < (sizeof (A_##symbol##_used) / sizeof 		\
     	    (unsigned long)); ++i)					\
@@ -1355,6 +1500,17 @@
     	    	(((value) << DBC_RWRI_IMMEDIATE_SHIFT) &		\
 		 DBC_RWRI_IMMEDIATE_MASK)
 
+/* Patch transfer control instruction data field */
+#define patch_abs_tci_data(script, offset, symbol, value)	        \
+    	for (i = 0; i < (sizeof (A_##symbol##_used) / sizeof 		\
+    	    (unsigned long)); ++i)					\
+    	    (script)[A_##symbol##_used[i] - (offset)] =			\
+	    	((script)[A_##symbol##_used[i] - (offset)] & 		\
+	    	~DBC_TCI_DATA_MASK) | 					\
+    	    	(((value) << DBC_TCI_DATA_SHIFT) &			\
+		 DBC_TCI_DATA_MASK)
+
+/* Patch field in dsa structure (assignment should be +=?) */
 #define patch_dsa_32(dsa, symbol, word, value)				\
 	{								\
 	(dsa)[(hostdata->##symbol - hostdata->dsa_start) / sizeof(long)	\
@@ -1362,10 +1518,10 @@
 	if (hostdata->options & OPTION_DEBUG_DSA)			\
 	    printk("scsi : dsa %s symbol %s(%ld) word %d now 0x%lx\n",	\
 		#dsa, #symbol, (long) hostdata->##symbol, 		\
-		(int) (word), (long) (value));					\
+		(int) (word), (long) (value));				\
 	}
-    
 
+#define FATAL(host) shutdown((host));
 
 #endif /* NCR53c7x0_C */
 #endif /* NCR53c7x0_H */
diff -u -r /usr/src/linux-1.2.13/drivers/scsi/53c7,8xx.scr /usr/src/linux/drivers/scsi/53c7,8xx.scr
--- /usr/src/linux-1.2.13/drivers/scsi/53c7,8xx.scr	Wed Oct 25 13:23:28 1995
+++ /usr/src/linux/drivers/scsi/53c7,8xx.scr	Fri Oct 27 17:29:02 1995
@@ -1,12 +1,14 @@
+#undef DEBUG
+#undef EVENTS
 ; NCR 53c810 driver, main script
 ; Sponsored by 
 ;	iX Multiuser Multitasking Magazine
 ;	hm@ix.de
 ;
-; Copyright 1993, Drew Eckhardt
+; Copyright 1993, 1994, 1995 Drew Eckhardt
 ;      Visionary Computing 
 ;      (Unix and Linux consulting and custom programming)
-;      drew@Colorado.EDU
+;      drew@PoohSticks.ORG
 ;      +1 (303) 786-7975
 ;
 ; TolerANT and SCSI SCRIPTS are registered trademarks of NCR Corporation.
@@ -36,9 +38,8 @@
 ;	the remote debugger should take this into account, and NOT set
 ;	breakpoints in modified instructions.
 ;
-;
 ; Design:
-; The NCR53c7x0 family of SCSI chips are busmasters with an onboard 
+; The NCR53c7,8xx family of SCSI chips are busmasters with an onboard 
 ; microcontroller using a simple instruction set.   
 ;
 ; So, to minimize the effects of interrupt latency, and to maximize 
@@ -76,6 +77,14 @@
 ; - Mailboxes are a mixture of code and data.  This lets us greatly
 ; 	simplify the NCR53c810 code and do things that would otherwise
 ;	not be possible.
+;
+; The saved data pointer is now implemented as follows :
+;
+; 	Control flow has been architected such that if control reaches
+;	munge_save_data_pointer, on a restore pointers message or 
+;	reconnection, a jump to the address formerly in the TEMP register
+;	will allow the SCSI command to resume execution.
+;
 
 ;
 ; Note : the DSA structures must be aligned on 32 bit boundaries,
@@ -84,70 +93,153 @@
 ; NCR registers.
 ;
 
-ABSOLUTE dsa_temp_jump_resume = 0	; Patch to dsa_jump_resume
-    	    	    	    	    	; 	in current dsa
 ABSOLUTE dsa_temp_lun = 0		; Patch to lun for current dsa
-ABSOLUTE dsa_temp_dsa_next = 0		; Patch to dsa next for current dsa
+ABSOLUTE dsa_temp_next = 0		; Patch to dsa next for current dsa
+ABSOLUTE dsa_temp_addr_next = 0		; Patch to address of dsa next address 
+					; 	for current dsa
 ABSOLUTE dsa_temp_sync = 0		; Patch to address of per-target
 					;	sync routine
 ABSOLUTE dsa_temp_target = 0		; Patch to id for current dsa
+ABSOLUTE dsa_temp_addr_saved_pointer = 0; Patch to address of per-command
+					; 	saved data pointer
+ABSOLUTE dsa_temp_addr_residual = 0	; Patch to address of per-command
+					;	current residual code
+ABSOLUTE dsa_temp_addr_saved_residual = 0; Patch to address of per-command
+					; 	saved residual code
 
+;
+; Once a device has initiated reselection, we need to compare it 
+; against the singly linked list of commands which have disconnected
+; and are pending reselection.  These commands are maintained in 
+; an unordered singly linked list of DSA structures, through the
+; DSA pointers at their 'centers' headed by the reconnect_dsa_head
+; pointer.
+; 
+; To avoid complications in removing commands from the list,
+; I minimize the amount of expensive (at eight operations per
+; addition @ 500-600ns each) pointer operations which must
+; be done in the NCR driver by precomputing them on the 
+; host processor during dsa structure generation.
+;
+; The fixed-up per DSA code knows how to recognize the nexus
+; associated with the corresponding SCSI command, and modifies
+; the source and destination pointers for the MOVE MEMORY 
+; instruction which is executed when reselected_ok is called
+; to remove the command from the list.  Similarly, DSA is 
+; loaded with the address of the next DSA structure and
+; reselected_check_next is called if a failure occurs.
+;
+; Perhaps more conscisely, the net effect of the mess is 
+;
+; for (dsa = reconnect_dsa_head, dest = &reconnect_dsa_head, 
+;     src = NULL; dsa; dest = &dsa->next, dsa = dsa->next) {
+; 	src = &dsa->next;
+; 	if (target_id == dsa->id && target_lun == dsa->lun) {
+; 		*dest = *src;
+; 		break;
+;         }	
+; }
+;
+; if (!dsa)
+;           error (int_err_unexpected_reselect);
+; else  
+;     longjmp (dsa->jump_resume, 0);
+;
+; 	
 
 #if (CHIP != 700) && (CHIP != 70066)
+; Define DSA structure used for mailboxes
 ENTRY dsa_code_template
 dsa_code_template:
 
-; Define DSA structure used for mailboxes
-
 ; wrong_dsa loads the DSA register with the value of the dsa_next
 ; field.
 ;
 wrong_dsa:
 ;		Patch the MOVE MEMORY INSTRUCTION such that 
-;		the destination address is that of the OLD next
-;		pointer.
-	MOVE MEMORY 4, dsa_temp_dsa_next, reselected_ok + 8
-
+;		the destination address is the address of the OLD 
+;		next pointer.
+;
+	MOVE MEMORY 4, dsa_temp_addr_next, reselected_ok + 8
 	MOVE dmode_memory_to_ncr TO DMODE	
-	MOVE MEMORY 4, dsa_temp_dsa_next, addr_scratch
+;
+; 	Move the _contents_ of the next pointer into the DSA register as 
+;	the next I_T_L or I_T_L_Q tupple to check against the established
+;	nexus.
+;
+	MOVE MEMORY 4, dsa_temp_next, addr_scratch
 	MOVE dmode_memory_to_memory TO DMODE
 	CALL scratch_to_dsa
 	JUMP reselected_check_next
 
+ABSOLUTE dsa_save_data_pointer = 0
+ENTRY dsa_code_save_data_pointer
+dsa_code_save_data_pointer:
+    	MOVE dmode_ncr_to_memory TO DMODE
+    	MOVE MEMORY 4, addr_temp, dsa_temp_addr_saved_pointer
+    	MOVE dmode_memory_to_memory TO DMODE
+; HARD CODED : 24 bytes needs to agree with 53c7,8xx.h
+    	MOVE MEMORY 24, dsa_temp_addr_residual, dsa_temp_addr_saved_residual
+        CLEAR ACK
+#ifdef DEBUG
+        INT int_debug_saved
+#endif
+    	RETURN
+ABSOLUTE dsa_restore_pointers = 0
+ENTRY dsa_code_restore_pointers
+dsa_code_restore_pointers:
+    	MOVE dmode_memory_to_ncr TO DMODE
+    	MOVE MEMORY 4, dsa_temp_addr_saved_pointer, addr_temp
+    	MOVE dmode_memory_to_memory TO DMODE
+; HARD CODED : 24 bytes needs to agree with 53c7,8xx.h
+    	MOVE MEMORY 24, dsa_temp_addr_saved_residual, dsa_temp_addr_residual
+        CLEAR ACK
+#ifdef DEBUG
+        INT int_debug_restored
+#endif
+    	RETURN
+
 ABSOLUTE dsa_check_reselect = 0
 ; dsa_check_reselect determines weather or not the current target and
 ; lun match the current DSA
 ENTRY dsa_code_check_reselect
 dsa_code_check_reselect:
 	MOVE SSID TO SFBR		; SSID contains 3 bit target ID
-	JUMP REL (wrong_dsa), IF NOT dsa_temp_target, AND MASK 7
+; FIXME : we need to accomodate bit fielded and binary here for '7xx/'8xx chips
+	JUMP REL (wrong_dsa), IF NOT dsa_temp_target, AND MASK 0xf8
+;
+; Hack - move to scratch first, since SFBR is not writeable
+; 	via the CPU and hence a MOVE MEMORY instruction.
+;
 	MOVE dmode_memory_to_ncr TO DMODE
-	MOVE MEMORY 1, reselected_identify, addr_sfbr
-	JUMP REL (wrong_dsa), IF NOT dsa_temp_lun, AND MASK 7
+	MOVE MEMORY 1, reselected_identify, addr_scratch
 	MOVE dmode_memory_to_memory TO DMODE
+	MOVE SCRATCH0 TO SFBR
+; FIXME : we need to accomodate bit fielded and binary here for '7xx/'8xx chips
+	JUMP REL (wrong_dsa), IF NOT dsa_temp_lun, AND MASK 0xf8
 ;		Patch the MOVE MEMORY INSTRUCTION such that
-;		the source address is that of this dsas
+;		the source address is the address of this dsa's
 ;		next pointer.
-	MOVE MEMORY 4, dsa_temp_dsa_next, reselected_ok + 4
+	MOVE MEMORY 4, dsa_temp_addr_next, reselected_ok + 4
 	CALL reselected_ok
 	CALL dsa_temp_sync	
-ENTRY dsa_jump_resume
-dsa_jump_resume:
-	JUMP 0				; Jump to resume address
+; Release ACK on the IDENTIFY message _after_ we've set the synchronous 
+; transfer parameters! 
+	CLEAR ACK
+; Implicitly restore pointers on reselection, so a RETURN
+; will transfer control back to the right spot.
+    	CALL REL (dsa_code_restore_pointers)
+    	RETURN
 ENTRY dsa_zero
 dsa_zero:
-	MOVE dmode_ncr_to_memory TO DMODE			; 8
-	MOVE MEMORY 4, addr_temp, dsa_temp_jump_resume		; 16	
-	MOVE dmode_memory_to_memory TO DMODE			; 28
-	JUMP dsa_schedule					; 36
 ENTRY dsa_code_template_end
 dsa_code_template_end:
 
 ; Perform sanity check for dsa_fields_start == dsa_code_template_end - 
 ; dsa_zero, puke.
 
-ABSOLUTE dsa_fields_start =  36	; Sanity marker
-				; 	pad 12
+ABSOLUTE dsa_fields_start =  0	; Sanity marker
+				; 	pad 48 bytes (fix this RSN)
 ABSOLUTE dsa_next = 48		; len 4 Next DSA
  				; del 4 Previous DSA address
 ABSOLUTE dsa_cmnd = 56		; len 4 Scsi_Cmnd * for this thread.
@@ -168,15 +260,15 @@
 ; Linked lists of DSA structures
 ABSOLUTE issue_dsa_head = 0	; Linked list of DSAs to issue
 ABSOLUTE reconnect_dsa_head = 0	; Link list of DSAs which can reconnect
+ABSOLUTE addr_reconnect_dsa_head = 0 ; Address of variable contataining
+				; address of reconnect_dsa_head
 
 ; These select the source and destination of a MOVE MEMORY instruction
 ABSOLUTE dmode_memory_to_memory = 0x0
 ABSOLUTE dmode_memory_to_ncr = 0x0
 ABSOLUTE dmode_ncr_to_memory = 0x0
-ABSOLUTE dmode_ncr_to_ncr = 0x0
 
 ABSOLUTE addr_scratch = 0x0
-ABSOLUTE addr_sfbr = 0x0
 ABSOLUTE addr_temp = 0x0
 #endif /* CHIP != 700 && CHIP != 70066 */
 
@@ -211,22 +303,55 @@
 ABSOLUTE int_norm_aborted =0x02040000		; Aborted *dsa
 ABSOLUTE int_norm_reset = 0x02050000		; Generated BUS reset.
 ABSOLUTE int_debug_break = 0x03000000		; Break point
+#ifdef DEBUG
 ABSOLUTE int_debug_scheduled = 0x03010000	; new I/O scheduled 
 ABSOLUTE int_debug_idle = 0x03020000		; scheduler is idle
 ABSOLUTE int_debug_dsa_loaded = 0x03030000	; dsa reloaded
 ABSOLUTE int_debug_reselected = 0x03040000	; NCR reselected
 ABSOLUTE int_debug_head = 0x03050000		; issue head overwritten
+ABSOLUTE int_debug_disconnected = 0x03060000	; disconnected
+ABSOLUTE int_debug_disconnect_msg = 0x03070000	; got message to disconnect
+ABSOLUTE int_debug_dsa_schedule = 0x03080000	; in dsa_schedule
+ABSOLUTE int_debug_reselect_check = 0x03090000  ; Check for reselection of DSA
+ABSOLUTE int_debug_reselected_ok = 0x030a0000 	; Reselection accepted
+#endif
+ABSOLUTE int_debug_panic = 0x030b0000		; Panic driver
+#ifdef DEBUG
+ABSOLUTE int_debug_saved = 0x030c0000 		; save/restore pointers
+ABSOLUTE int_debug_restored = 0x030d0000
+ABSOLUTE int_debug_sync = 0x030e0000		; Sanity check synchronous 
+						; parameters. 
+ABSOLUTE int_debug_datain = 0x030f0000		; going into data in phase 
+						; now.
+ABSOLUTE int_debug_check_dsa = 0x03100000	; Sanity check DSA against
+						; SDID.
+#endif
 
 ABSOLUTE int_test_1 = 0x04000000		; Test 1 complete
 ABSOLUTE int_test_2 = 0x04010000		; Test 2 complete
 ABSOLUTE int_test_3 = 0x04020000		; Test 3 complete
+
+
+; These should start with 0x05000000, with low bits incrementing for 
+; each one.
+
+#ifdef EVENTS
+ABSOLUTE int_EVENT_SELECT = 0
+ABSOLUTE int_EVENT_DISCONNECT = 0
+ABSOLUTE int_EVENT_RESELECT = 0
+ABSOLUTE int_EVENT_COMPLETE = 0
+ABSOLUTE int_EVENT_IDLE = 0
+ABSOLUTE int_EVENT_SELECT_FAILED = 0
+ABSOLUTE int_EVENT_BEFORE_SELECT = 0
+ABSOLUTE int_EVENT_RESELECT_FAILED = 0
+#endif
 						
 ABSOLUTE NCR53c7xx_msg_abort = 0	; Pointer to abort message
 ABSOLUTE NCR53c7xx_msg_reject = 0       ; Pointer to reject message
 ABSOLUTE NCR53c7xx_zero	= 0		; long with zero in it, use for source
 ABSOLUTE NCR53c7xx_sink = 0		; long to dump worthless data in
 
-; Pointer to final bytes of multi-byte messages
+; Pointer to message, potentially multi-byte
 ABSOLUTE msg_buf = 0
 
 ; Pointer to holding area for reselection information
@@ -256,15 +381,15 @@
 
 ENTRY dsa_schedule
 dsa_schedule:
+#if 0
+    INT int_debug_dsa_schedule
+#endif
 
 ;
 ; Calculate the address of the next pointer within the DSA 
 ; structure of the command that is currently disconnecting
 ;
     CALL dsa_to_scratch
-; XXX - we need to deal with the NCR53c710, which lacks an add with
-;	carry instruction, by moving around the DSA alignment to avoid
-; 	carry in situations like this.
     MOVE SCRATCH0 + dsa_next TO SCRATCH0
     MOVE SCRATCH1 + 0 TO SCRATCH1 WITH CARRY
     MOVE SCRATCH2 + 0 TO SCRATCH2 WITH CARRY
@@ -283,7 +408,18 @@
     MOVE dmode_ncr_to_memory TO DMODE	
     MOVE MEMORY 4, addr_scratch, reconnect_dsa_head
     MOVE dmode_memory_to_memory TO DMODE
+/* Temporarily, see what happens. */
+#ifndef ORIGINAL
+    MOVE SCNTL2 & 0x7f TO SCNTL2
+    CLEAR ACK
+#endif
     WAIT DISCONNECT
+#ifdef EVENTS
+   INT int_EVENT_DISCONNECT;
+#endif
+#if 0
+    INT int_debug_disconnected
+#endif
 
 ; schedule
 ; PURPOSE : schedule a new I/O once the bus is free by putting the 
@@ -353,8 +489,16 @@
 select:
 
 #if 0
+#ifdef EVENTS
+    INT int_EVENT_BEFORE_SELECT
+#endif
+#endif
+
+#if 0
+#ifdef DEBUG
     INT int_debug_scheduled
 #endif
+#endif
     CLEAR TARGET
 
 ; XXX
@@ -381,6 +525,26 @@
     MOVE 0, 0, WHEN MSGOUT
 #endif
 
+/* In theory, we're doing this right now... */
+#if 1
+;
+; Handle the phase mismatch which may have resulted from the 
+; MOVE FROM dsa_msgout.
+
+    CLEAR ATN
+    CLEAR ACK
+#endif
+
+#ifdef EVENTS
+   INT int_EVENT_SELECT;
+#endif
+
+#ifdef DEBUG
+ENTRY select_check_dsa
+select_check_dsa:
+    INT int_debug_check_dsa
+#endif
+
 #if (CHIP != 700) && (CHIP != 70066)
 
     ; Calculate address of dsa_next field
@@ -448,22 +612,31 @@
 
 ;
 ; data_transfer  
+; other_out
+; other_in
 ; other_transfer
 ;
 ; PURPOSE : handle the main data transfer for a SCSI command in 
-;	two parts.  In the first part, data_transfer, DATA_IN
+;	several parts.  In the first part, data_transfer, DATA_IN
 ;	and DATA_OUT phases are allowed, with the user provided
 ;	code (usually dynamically generated based on the scatter/gather
 ;	list associated with a SCSI command) called to handle these 
 ;	phases.
 ;
+;	After control has passed to one of the user provided 
+;	DATA_IN or DATA_OUT routines, back calls are made to 
+;	other_tranfer_in or other_transfer_out to handle non-DATA IN
+;	and DATA OUT phases respectively, with the state of the active
+;	data pointer being preserved in TEMP.
+;
 ;	On completion, the user code passes control to other_transfer
 ;	which causes DATA_IN and DATA_OUT to result in unexpected_phase
 ;	interrupts so that data overruns may be trapped.
 ;
 ; INPUTS : DSA - SCSI command
 ;
-; CALLS : OK
+; CALLS : OK in data_transfer_start, not ok in other_out and other_in, ok in
+;	other_transfer
 ;
 ; MODIFIES : SCRATCH
 ;
@@ -473,19 +646,22 @@
 ;	an infinite loop.
 ;	
 
+ENTRY data_transfer
 data_transfer:
-    INT int_err_unexpected_phase, WHEN CMD
+    JUMP cmdout_cmdout, WHEN CMD
     CALL msg_in, WHEN MSG_IN
     INT int_err_unexpected_phase, WHEN MSG_OUT
     JUMP do_dataout, WHEN DATA_OUT
     JUMP do_datain, WHEN DATA_IN
     JUMP command_complete, WHEN STATUS
     JUMP data_transfer
+ENTRY end_data_transfer
+end_data_transfer:
 
 ;
-; On NCR53c700 and NCR53c700-66 chips, do_dataout/do_datain are fixed up 
-; whenever the nexus changes so it can point to the correct routine for 
-; that command.
+; FIXME: On NCR53c700 and NCR53c700-66 chips, do_dataout/do_datain 
+; should be fixed up whenever the nexus changes so it can point to the 
+; correct routine for that command.
 ;
 
 #if (CHIP != 700) && (CHIP != 70066)
@@ -514,23 +690,52 @@
     MOVE dmode_ncr_to_memory TO DMODE
     MOVE MEMORY 4, addr_scratch, datain_to_jump + 4
     MOVE dmode_memory_to_memory TO DMODE		
+ENTRY datain_to_jump
 datain_to_jump:
     MOVE MEMORY 4, 0, datain_jump + 4
+#if 0
+    INT int_debug_datain
+#endif
 datain_jump:
     JUMP 0
 #endif /* (CHIP != 700) && (CHIP != 70066) */
 
-;
-; other_transfer is exported because it is referenced by dynamically 
-; generated code.
-;
-ENTRY other_transfer
-other_transfer:
+
+; Note that other_out and other_in loop until a non-data phase
+; is discoverred, so we only execute return statements when we
+; can go on to the next data phase block move statement.
+
+ENTRY other_out
+other_out:
+#if 0
+    INT 0x03ffdead
+#endif
+    INT int_err_unexpected_phase, WHEN CMD
+    JUMP msg_in_restart, WHEN MSG_IN 
+    INT int_err_unexpected_phase, WHEN MSG_OUT
+    INT int_err_unexpected_phase, WHEN DATA_IN
+    JUMP command_complete, WHEN STATUS
+    JUMP other_out, WHEN NOT DATA_OUT
+    RETURN
+
+ENTRY other_in
+other_in:
 #if 0
     INT 0x03ffdead
 #endif
     INT int_err_unexpected_phase, WHEN CMD
-    CALL msg_in, WHEN MSG_IN 
+    JUMP msg_in_restart, WHEN MSG_IN 
+    INT int_err_unexpected_phase, WHEN MSG_OUT
+    INT int_err_unexpected_phase, WHEN DATA_OUT
+    JUMP command_complete, WHEN STATUS
+    JUMP other_in, WHEN NOT DATA_IN
+    RETURN
+
+
+ENTRY other_transfer
+other_transfer:
+    INT int_err_unexpected_phase, WHEN CMD
+    CALL msg_in, WHEN MSG_IN
     INT int_err_unexpected_phase, WHEN MSG_OUT
     INT int_err_unexpected_phase, WHEN DATA_OUT
     INT int_err_unexpected_phase, WHEN DATA_IN
@@ -538,13 +743,17 @@
     JUMP other_transfer
 
 ;
+; msg_in_restart
 ; msg_in
 ; munge_msg
 ;
 ; PURPOSE : process messages from a target.  msg_in is called when the 
 ;	caller hasn't read the first byte of the message.  munge_message
 ;	is called when the caller has read the first byte of the message,
-;	and left it in SFBR.
+;	and left it in SFBR.  msg_in_restart is called when the caller 
+;	hasnt read the first byte of the message, and wishes RETURN
+;	to transfer control back to the address of the conditional
+;	CALL instruction rather than to the instruction after it.
 ;
 ;	Various int_* interrupts are generated when the host system
 ;	needs to intervene, as is the case with SDTR, WDTR, and
@@ -561,9 +770,6 @@
 ;	DISCONNECT messages are handled by moving the command
 ;	to the reconnect_dsa_queue.
 ;
-;	SAVE DATA POINTER and RESTORE DATA POINTERS are currently 
-;	treated as NOPS. 
-;
 ; INPUTS : DSA - SCSI COMMAND, SFBR - first byte of message (munge_msg
 ;	only)
 ;
@@ -576,6 +782,28 @@
 ;	Linux, control is returned to the caller.  Receipt
 ;	of DISCONNECT messages pass control to dsa_schedule.
 ;
+ENTRY msg_in_restart
+msg_in_restart:
+; XXX - hackish
+;
+; Since it's easier to debug changes to the statically 
+; compiled code, rather than the dynamically generated 
+; stuff, such as
+;
+; 	MOVE x, y, WHEN data_phase
+; 	CALL other_z, WHEN NOT data_phase
+; 	MOVE x, y, WHEN data_phase
+;
+; I'd like to have certain routines (notably the message handler)
+; restart on the conditional call rather than the next instruction.
+;
+; So, subtract 8 from the return address
+
+    MOVE TEMP0 + 0xf8 TO TEMP0
+    MOVE TEMP1 + 0xff TO TEMP1 WITH CARRY
+    MOVE TEMP2 + 0xff TO TEMP2 WITH CARRY
+    MOVE TEMP3 + 0xff TO TEMP3 WITH CARRY
+
 ENTRY msg_in
 msg_in:
     MOVE 1, msg_buf, WHEN MSG_IN
@@ -584,50 +812,89 @@
     JUMP munge_extended, IF 0x01		; EXTENDED MESSAGE
     JUMP munge_2, IF 0x20, AND MASK 0xdf	; two byte message
 ;
-; I've seen a handful of broken SCSI devices which fail to issue
-; a SAVE POINTERS message before disconnecting in the middle of 
-; a transfer, assuming that the DATA POINTER will be implicitly 
-; restored.  So, we treat the SAVE DATA POINTER message as a NOP.
-;
-; I've also seen SCSI devices which don't issue a RESTORE DATA
-; POINTER message and assume that thats implicit.
+; XXX - I've seen a handful of broken SCSI devices which fail to issue
+; 	a SAVE POINTERS message before disconnecting in the middle of 
+; 	a transfer, assuming that the DATA POINTER will be implicitly 
+; 	restored.  
+;
+; Historically, I've often done an implicit save when the DISCONNECT
+; message is processed.  We may want to consider having the option of 
+; doing that here. 
 ;
-    JUMP accept_message, IF 0x02		; SAVE DATA POINTER
-    JUMP accept_message, IF 0x03		; RESTORE POINTERS 
+    JUMP munge_save_data_pointer, IF 0x02	; SAVE DATA POINTER
+    JUMP munge_restore_pointers, IF 0x03	; RESTORE POINTERS 
     JUMP munge_disconnect, IF 0x04		; DISCONNECT
     INT int_msg_1, IF 0x07			; MESSAGE REJECT
     INT int_msg_1, IF 0x0f			; INITIATE RECOVERY
+#ifdef EVENTS 
+    INT int_EVENT_SELECT_FAILED 
+#endif
     JUMP reject_message
 
 munge_2:
     JUMP reject_message
+;
+; The SCSI standard allows targets to recover from transient 
+; error conditions by backing up the data pointer with a 
+; RESTORE POINTERS message.  
+;	
+; So, we must save and restore the _residual_ code as well as 
+; the current instruction pointer.  Because of this messiness,
+; it is simpler to put dynamic code in the dsa for this and to
+; just do a simple jump down there. 
+;
 
 munge_save_data_pointer:
-    CLEAR ACK
-    RETURN
+    MOVE DSA0 + dsa_save_data_pointer TO SFBR
+    MOVE SFBR TO SCRATCH0
+    MOVE DSA1 + 0xff TO SFBR WITH CARRY
+    MOVE SFBR TO SCRATCH1
+    MOVE DSA2 + 0xff TO SFBR WITH CARRY 
+    MOVE SFBR TO SCRATCH2
+    MOVE DSA3 + 0xff TO SFBR WITH CARRY
+    MOVE SFBR TO SCRATCH3
 
-munge_disconnect:
-    MOVE SCNTL2 & 0x7f TO SCNTL2
-    CLEAR ACK
+    MOVE dmode_ncr_to_memory TO DMODE
+    MOVE MEMORY 4, addr_scratch, jump_dsa_save + 4
+    MOVE dmode_memory_to_memory TO DMODE
+jump_dsa_save:
+    JUMP 0
 
-#if (CHIP != 700) && (CHIP != 70066)
-; Pass control to the DSA routine.  Note that we can not call
-; dsa_to_scratch here because that would clobber temp, which 
-; we must preserve.
-    MOVE DSA0 TO SFBR
+munge_restore_pointers:
+    MOVE DSA0 + dsa_restore_pointers TO SFBR
     MOVE SFBR TO SCRATCH0
-    MOVE DSA1 TO SFBR
+    MOVE DSA1 + 0xff TO SFBR WITH CARRY
     MOVE SFBR TO SCRATCH1
-    MOVE DSA2 TO SFBR
+    MOVE DSA2 + 0xff TO SFBR WITH CARRY
     MOVE SFBR TO SCRATCH2
-    MOVE DSA3 TO SFBR
+    MOVE DSA3 + 0xff TO SFBR WITH CARRY
     MOVE SFBR TO SCRATCH3
 
     MOVE dmode_ncr_to_memory TO DMODE
-    MOVE MEMORY 4, addr_scratch, jump_to_dsa + 4
+    MOVE MEMORY 4, addr_scratch, jump_dsa_restore + 4
     MOVE dmode_memory_to_memory TO DMODE
-jump_to_dsa:
+jump_dsa_restore:
     JUMP 0
+
+
+munge_disconnect:
+#if 0
+    INT int_debug_disconnect_msg
+#endif
+
+/* 
+ * Before, we overlapped processing with waiting for disconnect, but
+ * debugging was beginning to appear messy.  Temporarily move things
+ * to just before the WAIT DISCONNECT.
+ */
+ 
+#ifdef ORIGINAL
+    MOVE SCNTL2 & 0x7f TO SCNTL2
+    CLEAR ACK
+#endif
+
+#if (CHIP != 700) && (CHIP != 70066)
+    JUMP dsa_schedule
 #else
     WAIT DISCONNECT
     INT int_norm_disconnected
@@ -670,7 +937,7 @@
     RETURN
 
 ENTRY respond_message
-msg_respond:
+respond_message:
     SET ATN
     CLEAR ACK
     MOVE FROM dsa_msgout_other, WHEN MSG_OUT
@@ -739,6 +1006,9 @@
 #endif
     INTFLY
 #endif /* (CHIP != 700) && (CHIP != 70066) */
+#ifdef EVENTS
+    INT int_EVENT_COMPLETE
+#endif
     WAIT DISCONNECT
 #if (CHIP != 700) && (CHIP != 70066)
     JUMP schedule
@@ -781,21 +1051,25 @@
 ;	setting DSP to the target_abort address.
     
 wait_reselect:
+#ifdef EVENTS
+    int int_EVENT_IDLE
+#endif
 #if 0
     int int_debug_idle
 #endif
     WAIT RESELECT wait_reselect_failed
 
 reselected:
+#ifdef EVENTS
+    int int_EVENT_RESELECT
+#endif
+    CLEAR TARGET
+    MOVE dmode_memory_to_memory TO DMODE
     ; Read all data needed to reestablish the nexus - 
     MOVE 1, reselected_identify, WHEN MSG_IN
+    ; We used to CLEAR ACK here.
 #if (CHIP != 700) && (CHIP != 70066)
-    ; Well add a jump to here after some how determining that 
-    ; tagged queueing isn't in use on this device.
-reselected_notag:    
-    MOVE MEMORY 1, NCR53c7xx_zero, reselected_tag
-
-#ifdef DEBUG
+#if 0
     int int_debug_reselected
 #endif
 
@@ -809,10 +1083,13 @@
     ; pointer is the one that will be updated if this DSA is a hit 
     ; and we remove it from the queue.
 
-    MOVE MEMORY 4, reconnect_dsa_head, reselected_ok + 8
+    MOVE MEMORY 4, addr_reconnect_dsa_head, reselected_ok + 8
 
 ENTRY reselected_check_next
 reselected_check_next:
+#if 0
+    INT int_debug_reselect_check
+#endif
     ; Check for a NULL pointer.
     MOVE DSA0 TO SFBR
     JUMP reselected_not_end, IF NOT 0
@@ -825,7 +1102,6 @@
     INT int_err_unexpected_reselect
 
 reselected_not_end:
-    MOVE DSA0 TO SFBR
     ;
     ; XXX the ALU is only eight bits wide, and the assembler
     ; wont do the dirt work for us.  As long as dsa_check_reselect
@@ -838,6 +1114,7 @@
     ; higher than the LSB.
     ;
 
+    MOVE DSA0 TO SFBR
     MOVE SFBR + dsa_check_reselect TO SCRATCH0
     MOVE DSA1 TO SFBR
     MOVE SFBR + 0xff TO SCRATCH1 WITH CARRY
@@ -855,13 +1132,22 @@
 
 ;
 ;
+ENTRY reselected_ok
 reselected_ok:
     MOVE MEMORY 4, 0, 0				; Patched : first word
-						; 	is this successful 
-						; 	dsa_next
-						; Second word is 
-						;	unsuccessful dsa_next
-    CLEAR ACK					; Accept last message
+						; 	is address of 
+						;       successful dsa_next
+						; Second word is last 
+						;	unsuccessful dsa_next,
+						;	starting with 
+						;       dsa_reconnect_head
+    ; We used to CLEAR ACK here.
+#if 0
+    INT int_debug_reselected_ok
+#endif
+#ifdef DEBUG
+    INT int_debug_check_dsa
+#endif
     RETURN					; Return control to where
 #else
     INT int_norm_reselected
@@ -879,26 +1165,41 @@
 ; 
 
 wait_reselect_failed:
+#ifdef EVENTS 
+	INT int_EVENT_RESELECT_FAILED
+#endif
+    MOVE SIST0 & 0x20 TO SFBR
+    JUMP selected, IF 0x20
 ; Reading CTEST2 clears the SIG_P bit in the ISTAT register.
     MOVE CTEST2 & 0x40 TO SFBR	
     JUMP schedule, IF 0x40
-    MOVE SIST0 & 0x20 TO SFBR
-    JUMP selected, IF 0x20
 ; FIXME : Something bogus happened, and we shouldn't fail silently.
+#if 0
     JUMP schedule
+#else
+    INT int_debug_panic
+#endif
+
 
 select_failed:
+#ifdef EVENTS
+  int int_EVENT_SELECT_FAILED
+#endif
+; Otherwise, mask the selected and reselected bits off SIST0
+    MOVE SIST0 & 0x30 TO SFBR
+    JUMP selected, IF 0x20
+    JUMP reselected, IF 0x10 
 ; If SIGP is set, the user just gave us another command, and
 ; we should restart or return to the scheduler.
 ; Reading CTEST2 clears the SIG_P bit in the ISTAT register.
     MOVE CTEST2 & 0x40 TO SFBR	
     JUMP select, IF 0x40
-; Otherwise, mask the selected and reselected bits off SIST0
-    MOVE SIST0 & 0x30 TO SFBR
-    JUMP selected, IF 0x20
-    JUMP reselected, IF 0x10 
 ; FIXME : Something bogus happened, and we shouldn't fail silently.
+#if 0
     JUMP schedule
+#else
+    INT int_debug_panic
+#endif
 
 ;
 ; test_1
@@ -967,30 +1268,39 @@
 ENTRY initiator_abort
 initiator_abort:
     SET ATN
-; In order to abort the currently established nexus, we 
-; need to source/sink up to one byte of data in any SCSI phase, 
-; since the phase cannot change until REQ transitions 
-; false->true
-    JUMP no_eat_cmd, WHEN NOT CMD
+;
+; The SCSI-I specification says that targets may go into MSG out at 
+; their leisure upon receipt of the ATN single.  On all versions of the 
+; specification, we can't change phases until REQ transitions true->false, 
+; so we need to sink/source one byte of data to allow the transition.
+;
+; For the sake of safety, we'll only source one byte of data in all 
+; cases, but to accomodate the SCSI-I dain bramage, we'll sink an  
+; arbitrary number of bytes.
+    JUMP spew_cmd, WHEN CMD
+    JUMP eat_msgin, WHEN MSG_IN
+    JUMP eat_datain, WHEN DATA_IN
+    JUMP eat_status, WHEN STATUS
+    JUMP spew_dataout, WHEN DATA_OUT
+    JUMP sated
+spew_cmd:
     MOVE 1, NCR53c7xx_zero, WHEN CMD
-no_eat_cmd:
-    JUMP no_eat_msg, WHEN NOT MSG_IN
+    JUMP sated
+eat_msgin:
     MOVE 1, NCR53c7xx_sink, WHEN MSG_IN
-no_eat_msg:
-    JUMP no_eat_data, WHEN NOT DATA_IN
-    MOVE 1, NCR53c7xx_sink, WHEN DATA_IN
-no_eat_data:
-    JUMP no_eat_status, WHEN NOT STATUS
+    JUMP eat_msgin, WHEN MSG_IN
+    JUMP sated
+eat_status:
     MOVE 1, NCR53c7xx_sink, WHEN STATUS
-no_eat_status:
-    JUMP no_source_data, WHEN NOT DATA_OUT
+    JUMP eat_status, WHEN STATUS
+    JUMP sated
+eat_datain:
+    MOVE 1, NCR53c7xx_sink, WHEN DATA_IN
+    JUMP eat_datain, WHEN DATA_IN
+    JUMP sated
+spew_dataout:
     MOVE 1, NCR53c7xx_zero, WHEN DATA_OUT
-no_source_data:
-;
-; If DSP points here, and a phase mismatch is encountered, we need to 
-; do a bus reset.
-;
-    
+sated:
     MOVE SCNTL2 & 0x7f TO SCNTL2
     MOVE 1, NCR53c7xx_msg_abort, WHEN MSG_OUT
     WAIT DISCONNECT
diff -u -r /usr/src/linux-1.2.13/drivers/scsi/53c8xx_d.h /usr/src/linux/drivers/scsi/53c8xx_d.h
--- /usr/src/linux-1.2.13/drivers/scsi/53c8xx_d.h	Wed Oct 25 13:23:28 1995
+++ /usr/src/linux/drivers/scsi/53c8xx_d.h	Fri Oct 27 17:29:02 1995
@@ -1,14 +1,16 @@
 unsigned long SCRIPT[] = {
 /*
+
+
 ; NCR 53c810 driver, main script
 ; Sponsored by 
 ;	iX Multiuser Multitasking Magazine
 ;	hm@ix.de
 ;
-; Copyright 1993, Drew Eckhardt
+; Copyright 1993, 1994, 1995 Drew Eckhardt
 ;      Visionary Computing 
 ;      (Unix and Linux consulting and custom programming)
-;      drew@Colorado.EDU
+;      drew@PoohSticks.ORG
 ;      +1 (303) 786-7975
 ;
 ; TolerANT and SCSI SCRIPTS are registered trademarks of NCR Corporation.
@@ -38,9 +40,8 @@
 ;	the remote debugger should take this into account, and NOT set
 ;	breakpoints in modified instructions.
 ;
-;
 ; Design:
-; The NCR53c7x0 family of SCSI chips are busmasters with an onboard 
+; The NCR53c7,8xx family of SCSI chips are busmasters with an onboard 
 ; microcontroller using a simple instruction set.   
 ;
 ; So, to minimize the effects of interrupt latency, and to maximize 
@@ -78,6 +79,14 @@
 ; - Mailboxes are a mixture of code and data.  This lets us greatly
 ; 	simplify the NCR53c810 code and do things that would otherwise
 ;	not be possible.
+;
+; The saved data pointer is now implemented as follows :
+;
+; 	Control flow has been architected such that if control reaches
+;	munge_save_data_pointer, on a restore pointers message or 
+;	reconnection, a jump to the address formerly in the TEMP register
+;	will allow the SCSI command to resume execution.
+;
 
 ;
 ; Note : the DSA structures must be aligned on 32 bit boundaries,
@@ -86,38 +95,87 @@
 ; NCR registers.
 ;
 
-ABSOLUTE dsa_temp_jump_resume = 0	; Patch to dsa_jump_resume
-    	    	    	    	    	; 	in current dsa
 ABSOLUTE dsa_temp_lun = 0		; Patch to lun for current dsa
-ABSOLUTE dsa_temp_dsa_next = 0		; Patch to dsa next for current dsa
+ABSOLUTE dsa_temp_next = 0		; Patch to dsa next for current dsa
+ABSOLUTE dsa_temp_addr_next = 0		; Patch to address of dsa next address 
+					; 	for current dsa
 ABSOLUTE dsa_temp_sync = 0		; Patch to address of per-target
 					;	sync routine
 ABSOLUTE dsa_temp_target = 0		; Patch to id for current dsa
+ABSOLUTE dsa_temp_addr_saved_pointer = 0; Patch to address of per-command
+					; 	saved data pointer
+ABSOLUTE dsa_temp_addr_residual = 0	; Patch to address of per-command
+					;	current residual code
+ABSOLUTE dsa_temp_addr_saved_residual = 0; Patch to address of per-command
+					; 	saved residual code
 
+;
+; Once a device has initiated reselection, we need to compare it 
+; against the singly linked list of commands which have disconnected
+; and are pending reselection.  These commands are maintained in 
+; an unordered singly linked list of DSA structures, through the
+; DSA pointers at their 'centers' headed by the reconnect_dsa_head
+; pointer.
+; 
+; To avoid complications in removing commands from the list,
+; I minimize the amount of expensive (at eight operations per
+; addition @ 500-600ns each) pointer operations which must
+; be done in the NCR driver by precomputing them on the 
+; host processor during dsa structure generation.
+;
+; The fixed-up per DSA code knows how to recognize the nexus
+; associated with the corresponding SCSI command, and modifies
+; the source and destination pointers for the MOVE MEMORY 
+; instruction which is executed when reselected_ok is called
+; to remove the command from the list.  Similarly, DSA is 
+; loaded with the address of the next DSA structure and
+; reselected_check_next is called if a failure occurs.
+;
+; Perhaps more conscisely, the net effect of the mess is 
+;
+; for (dsa = reconnect_dsa_head, dest = &reconnect_dsa_head, 
+;     src = NULL; dsa; dest = &dsa->next, dsa = dsa->next) {
+; 	src = &dsa->next;
+; 	if (target_id == dsa->id && target_lun == dsa->lun) {
+; 		*dest = *src;
+; 		break;
+;         }	
+; }
+;
+; if (!dsa)
+;           error (int_err_unexpected_reselect);
+; else  
+;     longjmp (dsa->jump_resume, 0);
+;
+; 	
 
 
+; Define DSA structure used for mailboxes
 ENTRY dsa_code_template
 dsa_code_template:
 
-; Define DSA structure used for mailboxes
-
 ; wrong_dsa loads the DSA register with the value of the dsa_next
 ; field.
 ;
 wrong_dsa:
 ;		Patch the MOVE MEMORY INSTRUCTION such that 
-;		the destination address is that of the OLD next
-;		pointer.
-	MOVE MEMORY 4, dsa_temp_dsa_next, reselected_ok + 8
+;		the destination address is the address of the OLD 
+;		next pointer.
+;
+	MOVE MEMORY 4, dsa_temp_addr_next, reselected_ok + 8
 
-at 0x00000000 : */	0xc0000004,0x00000000,0x00000660,
+at 0x00000000 : */	0xc0000004,0x00000000,0x000007c4,
 /*
-
 	MOVE dmode_memory_to_ncr TO DMODE	
 
 at 0x00000003 : */	0x78380000,0x00000000,
 /*
-	MOVE MEMORY 4, dsa_temp_dsa_next, addr_scratch
+;
+; 	Move the _contents_ of the next pointer into the DSA register as 
+;	the next I_T_L or I_T_L_Q tupple to check against the established
+;	nexus.
+;
+	MOVE MEMORY 4, dsa_temp_next, addr_scratch
 
 at 0x00000005 : */	0xc0000004,0x00000000,0x00000000,
 /*
@@ -127,11 +185,74 @@
 /*
 	CALL scratch_to_dsa
 
-at 0x0000000a : */	0x88080000,0x00000830,
+at 0x0000000a : */	0x88080000,0x000009cc,
 /*
 	JUMP reselected_check_next
 
-at 0x0000000c : */	0x80080000,0x000005ac,
+at 0x0000000c : */	0x80080000,0x00000710,
+/*
+
+ABSOLUTE dsa_save_data_pointer = 0
+ENTRY dsa_code_save_data_pointer
+dsa_code_save_data_pointer:
+    	MOVE dmode_ncr_to_memory TO DMODE
+
+at 0x0000000e : */	0x78380000,0x00000000,
+/*
+    	MOVE MEMORY 4, addr_temp, dsa_temp_addr_saved_pointer
+
+at 0x00000010 : */	0xc0000004,0x00000000,0x00000000,
+/*
+    	MOVE dmode_memory_to_memory TO DMODE
+
+at 0x00000013 : */	0x78380000,0x00000000,
+/*
+; HARD CODED : 24 bytes needs to agree with 53c7,8xx.h
+    	MOVE MEMORY 24, dsa_temp_addr_residual, dsa_temp_addr_saved_residual
+
+at 0x00000015 : */	0xc0000018,0x00000000,0x00000000,
+/*
+        CLEAR ACK
+
+at 0x00000018 : */	0x60000040,0x00000000,
+/*
+
+
+
+    	RETURN
+
+at 0x0000001a : */	0x90080000,0x00000000,
+/*
+ABSOLUTE dsa_restore_pointers = 0
+ENTRY dsa_code_restore_pointers
+dsa_code_restore_pointers:
+    	MOVE dmode_memory_to_ncr TO DMODE
+
+at 0x0000001c : */	0x78380000,0x00000000,
+/*
+    	MOVE MEMORY 4, dsa_temp_addr_saved_pointer, addr_temp
+
+at 0x0000001e : */	0xc0000004,0x00000000,0x00000000,
+/*
+    	MOVE dmode_memory_to_memory TO DMODE
+
+at 0x00000021 : */	0x78380000,0x00000000,
+/*
+; HARD CODED : 24 bytes needs to agree with 53c7,8xx.h
+    	MOVE MEMORY 24, dsa_temp_addr_saved_residual, dsa_temp_addr_residual
+
+at 0x00000023 : */	0xc0000018,0x00000000,0x00000000,
+/*
+        CLEAR ACK
+
+at 0x00000026 : */	0x60000040,0x00000000,
+/*
+
+
+
+    	RETURN
+
+at 0x00000028 : */	0x90080000,0x00000000,
 /*
 
 ABSOLUTE dsa_check_reselect = 0
@@ -141,75 +262,79 @@
 dsa_code_check_reselect:
 	MOVE SSID TO SFBR		; SSID contains 3 bit target ID
 
-at 0x0000000e : */	0x720a0000,0x00000000,
+at 0x0000002a : */	0x720a0000,0x00000000,
 /*
-	JUMP REL (wrong_dsa), IF NOT dsa_temp_target, AND MASK 7
+; FIXME : we need to accomodate bit fielded and binary here for '7xx/'8xx chips
+	JUMP REL (wrong_dsa), IF NOT dsa_temp_target, AND MASK 0xf8
 
-at 0x00000010 : */	0x80840700,0x00ffffb8,
+at 0x0000002c : */	0x8084f800,0x00ffff48,
 /*
+;
+; Hack - move to scratch first, since SFBR is not writeable
+; 	via the CPU and hence a MOVE MEMORY instruction.
+;
 	MOVE dmode_memory_to_ncr TO DMODE
 
-at 0x00000012 : */	0x78380000,0x00000000,
+at 0x0000002e : */	0x78380000,0x00000000,
 /*
-	MOVE MEMORY 1, reselected_identify, addr_sfbr
+	MOVE MEMORY 1, reselected_identify, addr_scratch
 
-at 0x00000014 : */	0xc0000001,0x00000000,0x00000000,
+at 0x00000030 : */	0xc0000001,0x00000000,0x00000000,
 /*
-	JUMP REL (wrong_dsa), IF NOT dsa_temp_lun, AND MASK 7
+	MOVE dmode_memory_to_memory TO DMODE
 
-at 0x00000017 : */	0x80840700,0x00ffff9c,
+at 0x00000033 : */	0x78380000,0x00000000,
 /*
-	MOVE dmode_memory_to_memory TO DMODE
+	MOVE SCRATCH0 TO SFBR
 
-at 0x00000019 : */	0x78380000,0x00000000,
+at 0x00000035 : */	0x72340000,0x00000000,
+/*
+; FIXME : we need to accomodate bit fielded and binary here for '7xx/'8xx chips
+	JUMP REL (wrong_dsa), IF NOT dsa_temp_lun, AND MASK 0xf8
+
+at 0x00000037 : */	0x8084f800,0x00ffff1c,
 /*
 ;		Patch the MOVE MEMORY INSTRUCTION such that
-;		the source address is that of this dsas
+;		the source address is the address of this dsa's
 ;		next pointer.
-	MOVE MEMORY 4, dsa_temp_dsa_next, reselected_ok + 4
+	MOVE MEMORY 4, dsa_temp_addr_next, reselected_ok + 4
 
-at 0x0000001b : */	0xc0000004,0x00000000,0x0000065c,
+at 0x00000039 : */	0xc0000004,0x00000000,0x000007c0,
 /*
 	CALL reselected_ok
 
-at 0x0000001e : */	0x88080000,0x00000658,
+at 0x0000003c : */	0x88080000,0x000007bc,
 /*
 	CALL dsa_temp_sync	
 
-at 0x00000020 : */	0x88080000,0x00000000,
-/*
-ENTRY dsa_jump_resume
-dsa_jump_resume:
-	JUMP 0				; Jump to resume address
-
-at 0x00000022 : */	0x80080000,0x00000000,
-/*
-ENTRY dsa_zero
-dsa_zero:
-	MOVE dmode_ncr_to_memory TO DMODE			; 8
-
-at 0x00000024 : */	0x78380000,0x00000000,
+at 0x0000003e : */	0x88080000,0x00000000,
 /*
-	MOVE MEMORY 4, addr_temp, dsa_temp_jump_resume		; 16	
+; Release ACK on the IDENTIFY message _after_ we've set the synchronous 
+; transfer parameters! 
+	CLEAR ACK
 
-at 0x00000026 : */	0xc0000004,0x00000000,0x00000000,
+at 0x00000040 : */	0x60000040,0x00000000,
 /*
-	MOVE dmode_memory_to_memory TO DMODE			; 28
+; Implicitly restore pointers on reselection, so a RETURN
+; will transfer control back to the right spot.
+    	CALL REL (dsa_code_restore_pointers)
 
-at 0x00000029 : */	0x78380000,0x00000000,
+at 0x00000042 : */	0x88880000,0x00ffff60,
 /*
-	JUMP dsa_schedule					; 36
+    	RETURN
 
-at 0x0000002b : */	0x80080000,0x000000b4,
+at 0x00000044 : */	0x90080000,0x00000000,
 /*
+ENTRY dsa_zero
+dsa_zero:
 ENTRY dsa_code_template_end
 dsa_code_template_end:
 
 ; Perform sanity check for dsa_fields_start == dsa_code_template_end - 
 ; dsa_zero, puke.
 
-ABSOLUTE dsa_fields_start =  36	; Sanity marker
-				; 	pad 12
+ABSOLUTE dsa_fields_start =  0	; Sanity marker
+				; 	pad 48 bytes (fix this RSN)
 ABSOLUTE dsa_next = 48		; len 4 Next DSA
  				; del 4 Previous DSA address
 ABSOLUTE dsa_cmnd = 56		; len 4 Scsi_Cmnd * for this thread.
@@ -230,15 +355,15 @@
 ; Linked lists of DSA structures
 ABSOLUTE issue_dsa_head = 0	; Linked list of DSAs to issue
 ABSOLUTE reconnect_dsa_head = 0	; Link list of DSAs which can reconnect
+ABSOLUTE addr_reconnect_dsa_head = 0 ; Address of variable contataining
+				; address of reconnect_dsa_head
 
 ; These select the source and destination of a MOVE MEMORY instruction
 ABSOLUTE dmode_memory_to_memory = 0x0
 ABSOLUTE dmode_memory_to_ncr = 0x0
 ABSOLUTE dmode_ncr_to_memory = 0x0
-ABSOLUTE dmode_ncr_to_ncr = 0x0
 
 ABSOLUTE addr_scratch = 0x0
-ABSOLUTE addr_sfbr = 0x0
 ABSOLUTE addr_temp = 0x0
 
 
@@ -273,22 +398,26 @@
 ABSOLUTE int_norm_aborted =0x02040000		; Aborted *dsa
 ABSOLUTE int_norm_reset = 0x02050000		; Generated BUS reset.
 ABSOLUTE int_debug_break = 0x03000000		; Break point
-ABSOLUTE int_debug_scheduled = 0x03010000	; new I/O scheduled 
-ABSOLUTE int_debug_idle = 0x03020000		; scheduler is idle
-ABSOLUTE int_debug_dsa_loaded = 0x03030000	; dsa reloaded
-ABSOLUTE int_debug_reselected = 0x03040000	; NCR reselected
-ABSOLUTE int_debug_head = 0x03050000		; issue head overwritten
+
+ABSOLUTE int_debug_panic = 0x030b0000		; Panic driver
+
 
 ABSOLUTE int_test_1 = 0x04000000		; Test 1 complete
 ABSOLUTE int_test_2 = 0x04010000		; Test 2 complete
 ABSOLUTE int_test_3 = 0x04020000		; Test 3 complete
+
+
+; These should start with 0x05000000, with low bits incrementing for 
+; each one.
+
+
 						
 ABSOLUTE NCR53c7xx_msg_abort = 0	; Pointer to abort message
 ABSOLUTE NCR53c7xx_msg_reject = 0       ; Pointer to reject message
 ABSOLUTE NCR53c7xx_zero	= 0		; long with zero in it, use for source
 ABSOLUTE NCR53c7xx_sink = 0		; long to dump worthless data in
 
-; Pointer to final bytes of multi-byte messages
+; Pointer to message, potentially multi-byte
 ABSOLUTE msg_buf = 0
 
 ; Pointer to holding area for reselection information
@@ -319,76 +448,93 @@
 ENTRY dsa_schedule
 dsa_schedule:
 
+
+
+
 ;
 ; Calculate the address of the next pointer within the DSA 
 ; structure of the command that is currently disconnecting
 ;
     CALL dsa_to_scratch
 
-at 0x0000002d : */	0x88080000,0x000007e8,
+at 0x00000046 : */	0x88080000,0x00000984,
 /*
-; XXX - we need to deal with the NCR53c710, which lacks an add with
-;	carry instruction, by moving around the DSA alignment to avoid
-; 	carry in situations like this.
     MOVE SCRATCH0 + dsa_next TO SCRATCH0
 
-at 0x0000002f : */	0x7e343000,0x00000000,
+at 0x00000048 : */	0x7e343000,0x00000000,
 /*
     MOVE SCRATCH1 + 0 TO SCRATCH1 WITH CARRY
 
-at 0x00000031 : */	0x7f350000,0x00000000,
+at 0x0000004a : */	0x7f350000,0x00000000,
 /*
     MOVE SCRATCH2 + 0 TO SCRATCH2 WITH CARRY
 
-at 0x00000033 : */	0x7f360000,0x00000000,
+at 0x0000004c : */	0x7f360000,0x00000000,
 /*
     MOVE SCRATCH3 + 0 TO SCRATCH3 WITH CARRY
 
-at 0x00000035 : */	0x7f370000,0x00000000,
+at 0x0000004e : */	0x7f370000,0x00000000,
 /*
 
 ; Point the next field of this DSA structure at the current disconnected 
 ; list
     MOVE dmode_ncr_to_memory TO DMODE
 
-at 0x00000037 : */	0x78380000,0x00000000,
+at 0x00000050 : */	0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, addr_scratch, dsa_schedule_insert + 8
 
-at 0x00000039 : */	0xc0000004,0x00000000,0x00000100,
+at 0x00000052 : */	0xc0000004,0x00000000,0x00000164,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x0000003c : */	0x78380000,0x00000000,
+at 0x00000055 : */	0x78380000,0x00000000,
 /*
 dsa_schedule_insert:
     MOVE MEMORY 4, reconnect_dsa_head, 0 
 
-at 0x0000003e : */	0xc0000004,0x00000000,0x00000000,
+at 0x00000057 : */	0xc0000004,0x00000000,0x00000000,
 /*
 
 ; And update the head pointer.
     CALL dsa_to_scratch
 
-at 0x00000041 : */	0x88080000,0x000007e8,
+at 0x0000005a : */	0x88080000,0x00000984,
 /*
     MOVE dmode_ncr_to_memory TO DMODE	
 
-at 0x00000043 : */	0x78380000,0x00000000,
+at 0x0000005c : */	0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, addr_scratch, reconnect_dsa_head
 
-at 0x00000045 : */	0xc0000004,0x00000000,0x00000000,
+at 0x0000005e : */	0xc0000004,0x00000000,0x00000000,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x00000048 : */	0x78380000,0x00000000,
+at 0x00000061 : */	0x78380000,0x00000000,
+/*
+
+
+    MOVE SCNTL2 & 0x7f TO SCNTL2
+
+at 0x00000063 : */	0x7c027f00,0x00000000,
+/*
+    CLEAR ACK
+
+at 0x00000065 : */	0x60000040,0x00000000,
 /*
+
     WAIT DISCONNECT
 
-at 0x0000004a : */	0x48000000,0x00000000,
+at 0x00000067 : */	0x48000000,0x00000000,
 /*
 
+
+
+
+
+
+
 ; schedule
 ; PURPOSE : schedule a new I/O once the bus is free by putting the 
 ;	address of the next DSA structure in the DSA register.
@@ -409,20 +555,20 @@
     ; Point DSA at the current head of the issue queue.
     MOVE dmode_memory_to_ncr TO DMODE
 
-at 0x0000004c : */	0x78380000,0x00000000,
+at 0x00000069 : */	0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, issue_dsa_head, addr_scratch
 
-at 0x0000004e : */	0xc0000004,0x00000000,0x00000000,
+at 0x0000006b : */	0xc0000004,0x00000000,0x00000000,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x00000051 : */	0x78380000,0x00000000,
+at 0x0000006e : */	0x78380000,0x00000000,
 /*
 
     CALL scratch_to_dsa
 
-at 0x00000053 : */	0x88080000,0x00000830,
+at 0x00000070 : */	0x88080000,0x000009cc,
 /*
 
 
@@ -431,35 +577,35 @@
     ; Check for a null pointer.
     MOVE DSA0 TO SFBR
 
-at 0x00000055 : */	0x72100000,0x00000000,
+at 0x00000072 : */	0x72100000,0x00000000,
 /*
     JUMP select, IF NOT 0
 
-at 0x00000057 : */	0x80040000,0x00000194,
+at 0x00000074 : */	0x80040000,0x00000208,
 /*
     MOVE DSA1 TO SFBR
 
-at 0x00000059 : */	0x72110000,0x00000000,
+at 0x00000076 : */	0x72110000,0x00000000,
 /*
     JUMP select, IF NOT 0
 
-at 0x0000005b : */	0x80040000,0x00000194,
+at 0x00000078 : */	0x80040000,0x00000208,
 /*
     MOVE DSA2 TO SFBR
 
-at 0x0000005d : */	0x72120000,0x00000000,
+at 0x0000007a : */	0x72120000,0x00000000,
 /*
     JUMP select, IF NOT 0
 
-at 0x0000005f : */	0x80040000,0x00000194,
+at 0x0000007c : */	0x80040000,0x00000208,
 /*
     MOVE DSA3 TO SFBR
 
-at 0x00000061 : */	0x72130000,0x00000000,
+at 0x0000007e : */	0x72130000,0x00000000,
 /*
     JUMP wait_reselect, IF 0
 
-at 0x00000063 : */	0x800c0000,0x00000560,
+at 0x00000080 : */	0x800c0000,0x000006c0,
 /*
 
     
@@ -495,9 +641,17 @@
 
 
 
+
+
+
+
+
+
+
+
     CLEAR TARGET
 
-at 0x00000065 : */	0x60000200,0x00000000,
+at 0x00000082 : */	0x60000200,0x00000000,
 /*
 
 ; XXX
@@ -514,17 +668,38 @@
 
     SELECT ATN FROM dsa_select, select_failed
 
-at 0x00000067 : */	0x4300003c,0x000006a4,
+at 0x00000084 : */	0x4300003c,0x00000800,
 /*
     JUMP select_msgout, WHEN MSG_OUT
 
-at 0x00000069 : */	0x860b0000,0x000001ac,
+at 0x00000086 : */	0x860b0000,0x00000220,
 /*
 ENTRY select_msgout
 select_msgout:
     MOVE FROM dsa_msgout, WHEN MSG_OUT
 
-at 0x0000006b : */	0x1e000000,0x00000040,
+at 0x00000088 : */	0x1e000000,0x00000040,
+/*
+
+
+
+
+
+
+
+
+
+;
+; Handle the phase mismatch which may have resulted from the 
+; MOVE FROM dsa_msgout.
+
+    CLEAR ATN
+
+at 0x0000008a : */	0x60000008,0x00000000,
+/*
+    CLEAR ACK
+
+at 0x0000008c : */	0x60000040,0x00000000,
 /*
 
 
@@ -535,45 +710,50 @@
 
 
 
+
+
+
+
+
     ; Calculate address of dsa_next field
 
     CALL dsa_to_scratch
 
-at 0x0000006d : */	0x88080000,0x000007e8,
+at 0x0000008e : */	0x88080000,0x00000984,
 /*
 
     MOVE SCRATCH0 + dsa_next TO SCRATCH0
 
-at 0x0000006f : */	0x7e343000,0x00000000,
+at 0x00000090 : */	0x7e343000,0x00000000,
 /*
     MOVE SCRATCH1 + 0 TO SCRATCH1 WITH CARRY
 
-at 0x00000071 : */	0x7f350000,0x00000000,
+at 0x00000092 : */	0x7f350000,0x00000000,
 /*
     MOVE SCRATCH2 + 0 TO SCRATCH2 WITH CARRY
 
-at 0x00000073 : */	0x7f360000,0x00000000,
+at 0x00000094 : */	0x7f360000,0x00000000,
 /*
     MOVE SCRATCH3 + 0 TO SCRATCH3 WITH CARRY
 
-at 0x00000075 : */	0x7f370000,0x00000000,
+at 0x00000096 : */	0x7f370000,0x00000000,
 /*
 
     ; Patch memory to memory move
     move dmode_ncr_to_memory TO DMODE
 
-at 0x00000077 : */	0x78380000,0x00000000,
+at 0x00000098 : */	0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, addr_scratch, issue_remove + 4
 
-at 0x00000079 : */	0xc0000004,0x00000000,0x000001fc,
+at 0x0000009a : */	0xc0000004,0x00000000,0x00000280,
 /*
 
 
     ; And rewrite the issue_dsa_head pointer.
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x0000007c : */	0x78380000,0x00000000,
+at 0x0000009d : */	0x78380000,0x00000000,
 /*
 issue_remove:
 ;	The actual UPDATE of the issue_dsa_head variable is 
@@ -595,7 +775,7 @@
 
     MOVE MEMORY 4, 0, issue_dsa_head
 
-at 0x0000007e : */	0xc0000004,0x00000000,0x00000000,
+at 0x0000009f : */	0xc0000004,0x00000000,0x00000000,
 /*
 
 
@@ -607,27 +787,27 @@
 
     JUMP cmdout, WHEN CMD
 
-at 0x00000081 : */	0x820b0000,0x00000224,
+at 0x000000a2 : */	0x820b0000,0x000002a8,
 /*
     INT int_err_unexpected_phase, WHEN NOT MSG_IN 
 
-at 0x00000083 : */	0x9f030000,0x00000000,
+at 0x000000a4 : */	0x9f030000,0x00000000,
 /*
 
 select_msg_in:
     CALL msg_in, WHEN MSG_IN
 
-at 0x00000085 : */	0x8f0b0000,0x00000354,
+at 0x000000a6 : */	0x8f0b0000,0x00000468,
 /*
     JUMP select_msg_in, WHEN MSG_IN
 
-at 0x00000087 : */	0x870b0000,0x00000214,
+at 0x000000a8 : */	0x870b0000,0x00000298,
 /*
 
 cmdout:
     INT int_err_unexpected_phase, WHEN NOT CMD
 
-at 0x00000089 : */	0x9a030000,0x00000000,
+at 0x000000aa : */	0x9a030000,0x00000000,
 /*
 
 
@@ -637,7 +817,7 @@
 
     MOVE FROM dsa_cmdout, WHEN CMD
 
-at 0x0000008b : */	0x1a000000,0x00000048,
+at 0x000000ac : */	0x1a000000,0x00000048,
 /*
 
 
@@ -645,22 +825,31 @@
 
 ;
 ; data_transfer  
+; other_out
+; other_in
 ; other_transfer
 ;
 ; PURPOSE : handle the main data transfer for a SCSI command in 
-;	two parts.  In the first part, data_transfer, DATA_IN
+;	several parts.  In the first part, data_transfer, DATA_IN
 ;	and DATA_OUT phases are allowed, with the user provided
 ;	code (usually dynamically generated based on the scatter/gather
 ;	list associated with a SCSI command) called to handle these 
 ;	phases.
 ;
+;	After control has passed to one of the user provided 
+;	DATA_IN or DATA_OUT routines, back calls are made to 
+;	other_tranfer_in or other_transfer_out to handle non-DATA IN
+;	and DATA OUT phases respectively, with the state of the active
+;	data pointer being preserved in TEMP.
+;
 ;	On completion, the user code passes control to other_transfer
 ;	which causes DATA_IN and DATA_OUT to result in unexpected_phase
 ;	interrupts so that data overruns may be trapped.
 ;
 ; INPUTS : DSA - SCSI command
 ;
-; CALLS : OK
+; CALLS : OK in data_transfer_start, not ok in other_out and other_in, ok in
+;	other_transfer
 ;
 ; MODIFIES : SCRATCH
 ;
@@ -670,40 +859,43 @@
 ;	an infinite loop.
 ;	
 
+ENTRY data_transfer
 data_transfer:
-    INT int_err_unexpected_phase, WHEN CMD
+    JUMP cmdout_cmdout, WHEN CMD
 
-at 0x0000008d : */	0x9a0b0000,0x00000000,
+at 0x000000ae : */	0x820b0000,0x000002b0,
 /*
     CALL msg_in, WHEN MSG_IN
 
-at 0x0000008f : */	0x8f0b0000,0x00000354,
+at 0x000000b0 : */	0x8f0b0000,0x00000468,
 /*
     INT int_err_unexpected_phase, WHEN MSG_OUT
 
-at 0x00000091 : */	0x9e0b0000,0x00000000,
+at 0x000000b2 : */	0x9e0b0000,0x00000000,
 /*
     JUMP do_dataout, WHEN DATA_OUT
 
-at 0x00000093 : */	0x800b0000,0x0000026c,
+at 0x000000b4 : */	0x800b0000,0x000002f0,
 /*
     JUMP do_datain, WHEN DATA_IN
 
-at 0x00000095 : */	0x810b0000,0x000002c4,
+at 0x000000b6 : */	0x810b0000,0x00000348,
 /*
     JUMP command_complete, WHEN STATUS
 
-at 0x00000097 : */	0x830b0000,0x00000508,
+at 0x000000b8 : */	0x830b0000,0x00000668,
 /*
     JUMP data_transfer
 
-at 0x00000099 : */	0x80080000,0x00000234,
+at 0x000000ba : */	0x80080000,0x000002b8,
 /*
+ENTRY end_data_transfer
+end_data_transfer:
 
 ;
-; On NCR53c700 and NCR53c700-66 chips, do_dataout/do_datain are fixed up 
-; whenever the nexus changes so it can point to the correct routine for 
-; that command.
+; FIXME: On NCR53c700 and NCR53c700-66 chips, do_dataout/do_datain 
+; should be fixed up whenever the nexus changes so it can point to the 
+; correct routine for that command.
 ;
 
 
@@ -711,139 +903,214 @@
 do_dataout:
     CALL dsa_to_scratch
 
-at 0x0000009b : */	0x88080000,0x000007e8,
+at 0x000000bc : */	0x88080000,0x00000984,
 /*
     MOVE SCRATCH0 + dsa_dataout TO SCRATCH0	
 
-at 0x0000009d : */	0x7e345000,0x00000000,
+at 0x000000be : */	0x7e345000,0x00000000,
 /*
     MOVE SCRATCH1 + 0 TO SCRATCH1 WITH CARRY 
 
-at 0x0000009f : */	0x7f350000,0x00000000,
+at 0x000000c0 : */	0x7f350000,0x00000000,
 /*
     MOVE SCRATCH2 + 0 TO SCRATCH2 WITH CARRY 
 
-at 0x000000a1 : */	0x7f360000,0x00000000,
+at 0x000000c2 : */	0x7f360000,0x00000000,
 /*
     MOVE SCRATCH3 + 0 TO SCRATCH3 WITH CARRY 
 
-at 0x000000a3 : */	0x7f370000,0x00000000,
+at 0x000000c4 : */	0x7f370000,0x00000000,
 /*
     MOVE dmode_ncr_to_memory TO DMODE
 
-at 0x000000a5 : */	0x78380000,0x00000000,
+at 0x000000c6 : */	0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, addr_scratch, dataout_to_jump + 4
 
-at 0x000000a7 : */	0xc0000004,0x00000000,0x000002b4,
+at 0x000000c8 : */	0xc0000004,0x00000000,0x00000338,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x000000aa : */	0x78380000,0x00000000,
+at 0x000000cb : */	0x78380000,0x00000000,
 /*
 dataout_to_jump:
     MOVE MEMORY 4, 0, dataout_jump + 4 
 
-at 0x000000ac : */	0xc0000004,0x00000000,0x000002c0,
+at 0x000000cd : */	0xc0000004,0x00000000,0x00000344,
 /*
 dataout_jump:
     JUMP 0
 
-at 0x000000af : */	0x80080000,0x00000000,
+at 0x000000d0 : */	0x80080000,0x00000000,
 /*
 
 ; Nasty jump to dsa->dsain
 do_datain:
     CALL dsa_to_scratch
 
-at 0x000000b1 : */	0x88080000,0x000007e8,
+at 0x000000d2 : */	0x88080000,0x00000984,
 /*
     MOVE SCRATCH0 + dsa_datain TO SCRATCH0	
 
-at 0x000000b3 : */	0x7e345400,0x00000000,
+at 0x000000d4 : */	0x7e345400,0x00000000,
 /*
     MOVE SCRATCH1 + 0 TO SCRATCH1 WITH CARRY 
 
-at 0x000000b5 : */	0x7f350000,0x00000000,
+at 0x000000d6 : */	0x7f350000,0x00000000,
 /*
     MOVE SCRATCH2 + 0 TO SCRATCH2 WITH CARRY 
 
-at 0x000000b7 : */	0x7f360000,0x00000000,
+at 0x000000d8 : */	0x7f360000,0x00000000,
 /*
     MOVE SCRATCH3 + 0 TO SCRATCH3 WITH CARRY 
 
-at 0x000000b9 : */	0x7f370000,0x00000000,
+at 0x000000da : */	0x7f370000,0x00000000,
 /*
     MOVE dmode_ncr_to_memory TO DMODE
 
-at 0x000000bb : */	0x78380000,0x00000000,
+at 0x000000dc : */	0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, addr_scratch, datain_to_jump + 4
 
-at 0x000000bd : */	0xc0000004,0x00000000,0x0000030c,
+at 0x000000de : */	0xc0000004,0x00000000,0x00000390,
 /*
     MOVE dmode_memory_to_memory TO DMODE		
 
-at 0x000000c0 : */	0x78380000,0x00000000,
+at 0x000000e1 : */	0x78380000,0x00000000,
 /*
+ENTRY datain_to_jump
 datain_to_jump:
     MOVE MEMORY 4, 0, datain_jump + 4
 
-at 0x000000c2 : */	0xc0000004,0x00000000,0x00000318,
+at 0x000000e3 : */	0xc0000004,0x00000000,0x0000039c,
 /*
+
+
+
 datain_jump:
     JUMP 0
 
-at 0x000000c5 : */	0x80080000,0x00000000,
+at 0x000000e6 : */	0x80080000,0x00000000,
 /*
 
 
-;
-; other_transfer is exported because it is referenced by dynamically 
-; generated code.
-;
-ENTRY other_transfer
-other_transfer:
+
+; Note that other_out and other_in loop until a non-data phase
+; is discoverred, so we only execute return statements when we
+; can go on to the next data phase block move statement.
+
+ENTRY other_out
+other_out:
+
+
+
+    INT int_err_unexpected_phase, WHEN CMD
+
+at 0x000000e8 : */	0x9a0b0000,0x00000000,
+/*
+    JUMP msg_in_restart, WHEN MSG_IN 
+
+at 0x000000ea : */	0x870b0000,0x00000448,
+/*
+    INT int_err_unexpected_phase, WHEN MSG_OUT
+
+at 0x000000ec : */	0x9e0b0000,0x00000000,
+/*
+    INT int_err_unexpected_phase, WHEN DATA_IN
+
+at 0x000000ee : */	0x990b0000,0x00000000,
+/*
+    JUMP command_complete, WHEN STATUS
+
+at 0x000000f0 : */	0x830b0000,0x00000668,
+/*
+    JUMP other_out, WHEN NOT DATA_OUT
+
+at 0x000000f2 : */	0x80030000,0x000003a0,
+/*
+    RETURN
+
+at 0x000000f4 : */	0x90080000,0x00000000,
+/*
+
+ENTRY other_in
+other_in:
 
 
 
     INT int_err_unexpected_phase, WHEN CMD
 
-at 0x000000c7 : */	0x9a0b0000,0x00000000,
+at 0x000000f6 : */	0x9a0b0000,0x00000000,
 /*
-    CALL msg_in, WHEN MSG_IN 
+    JUMP msg_in_restart, WHEN MSG_IN 
 
-at 0x000000c9 : */	0x8f0b0000,0x00000354,
+at 0x000000f8 : */	0x870b0000,0x00000448,
 /*
     INT int_err_unexpected_phase, WHEN MSG_OUT
 
-at 0x000000cb : */	0x9e0b0000,0x00000000,
+at 0x000000fa : */	0x9e0b0000,0x00000000,
 /*
     INT int_err_unexpected_phase, WHEN DATA_OUT
 
-at 0x000000cd : */	0x980b0000,0x00000000,
+at 0x000000fc : */	0x980b0000,0x00000000,
+/*
+    JUMP command_complete, WHEN STATUS
+
+at 0x000000fe : */	0x830b0000,0x00000668,
+/*
+    JUMP other_in, WHEN NOT DATA_IN
+
+at 0x00000100 : */	0x81030000,0x000003d8,
+/*
+    RETURN
+
+at 0x00000102 : */	0x90080000,0x00000000,
+/*
+
+
+ENTRY other_transfer
+other_transfer:
+    INT int_err_unexpected_phase, WHEN CMD
+
+at 0x00000104 : */	0x9a0b0000,0x00000000,
+/*
+    CALL msg_in, WHEN MSG_IN
+
+at 0x00000106 : */	0x8f0b0000,0x00000468,
+/*
+    INT int_err_unexpected_phase, WHEN MSG_OUT
+
+at 0x00000108 : */	0x9e0b0000,0x00000000,
+/*
+    INT int_err_unexpected_phase, WHEN DATA_OUT
+
+at 0x0000010a : */	0x980b0000,0x00000000,
 /*
     INT int_err_unexpected_phase, WHEN DATA_IN
 
-at 0x000000cf : */	0x990b0000,0x00000000,
+at 0x0000010c : */	0x990b0000,0x00000000,
 /*
     JUMP command_complete, WHEN STATUS
 
-at 0x000000d1 : */	0x830b0000,0x00000508,
+at 0x0000010e : */	0x830b0000,0x00000668,
 /*
     JUMP other_transfer
 
-at 0x000000d3 : */	0x80080000,0x0000031c,
+at 0x00000110 : */	0x80080000,0x00000410,
 /*
 
 ;
+; msg_in_restart
 ; msg_in
 ; munge_msg
 ;
 ; PURPOSE : process messages from a target.  msg_in is called when the 
 ;	caller hasn't read the first byte of the message.  munge_message
 ;	is called when the caller has read the first byte of the message,
-;	and left it in SFBR.
+;	and left it in SFBR.  msg_in_restart is called when the caller 
+;	hasnt read the first byte of the message, and wishes RETURN
+;	to transfer control back to the address of the conditional
+;	CALL instruction rather than to the instruction after it.
 ;
 ;	Various int_* interrupts are generated when the host system
 ;	needs to intervene, as is the case with SDTR, WDTR, and
@@ -860,9 +1127,6 @@
 ;	DISCONNECT messages are handled by moving the command
 ;	to the reconnect_dsa_queue.
 ;
-;	SAVE DATA POINTER and RESTORE DATA POINTERS are currently 
-;	treated as NOPS. 
-;
 ; INPUTS : DSA - SCSI COMMAND, SFBR - first byte of message (munge_msg
 ;	only)
 ;
@@ -875,135 +1139,235 @@
 ;	Linux, control is returned to the caller.  Receipt
 ;	of DISCONNECT messages pass control to dsa_schedule.
 ;
+ENTRY msg_in_restart
+msg_in_restart:
+; XXX - hackish
+;
+; Since it's easier to debug changes to the statically 
+; compiled code, rather than the dynamically generated 
+; stuff, such as
+;
+; 	MOVE x, y, WHEN data_phase
+; 	CALL other_z, WHEN NOT data_phase
+; 	MOVE x, y, WHEN data_phase
+;
+; I'd like to have certain routines (notably the message handler)
+; restart on the conditional call rather than the next instruction.
+;
+; So, subtract 8 from the return address
+
+    MOVE TEMP0 + 0xf8 TO TEMP0
+
+at 0x00000112 : */	0x7e1cf800,0x00000000,
+/*
+    MOVE TEMP1 + 0xff TO TEMP1 WITH CARRY
+
+at 0x00000114 : */	0x7f1dff00,0x00000000,
+/*
+    MOVE TEMP2 + 0xff TO TEMP2 WITH CARRY
+
+at 0x00000116 : */	0x7f1eff00,0x00000000,
+/*
+    MOVE TEMP3 + 0xff TO TEMP3 WITH CARRY
+
+at 0x00000118 : */	0x7f1fff00,0x00000000,
+/*
+
 ENTRY msg_in
 msg_in:
     MOVE 1, msg_buf, WHEN MSG_IN
 
-at 0x000000d5 : */	0x0f000001,0x00000000,
+at 0x0000011a : */	0x0f000001,0x00000000,
 /*
 
 munge_msg:
     JUMP munge_extended, IF 0x01		; EXTENDED MESSAGE
 
-at 0x000000d7 : */	0x800c0001,0x00000428,
+at 0x0000011c : */	0x800c0001,0x00000588,
 /*
     JUMP munge_2, IF 0x20, AND MASK 0xdf	; two byte message
 
-at 0x000000d9 : */	0x800cdf20,0x0000039c,
+at 0x0000011e : */	0x800cdf20,0x000004b0,
 /*
 ;
-; I've seen a handful of broken SCSI devices which fail to issue
-; a SAVE POINTERS message before disconnecting in the middle of 
-; a transfer, assuming that the DATA POINTER will be implicitly 
-; restored.  So, we treat the SAVE DATA POINTER message as a NOP.
+; XXX - I've seen a handful of broken SCSI devices which fail to issue
+; 	a SAVE POINTERS message before disconnecting in the middle of 
+; 	a transfer, assuming that the DATA POINTER will be implicitly 
+; 	restored.  
 ;
-; I've also seen SCSI devices which don't issue a RESTORE DATA
-; POINTER message and assume that thats implicit.
+; Historically, I've often done an implicit save when the DISCONNECT
+; message is processed.  We may want to consider having the option of 
+; doing that here. 
 ;
-    JUMP accept_message, IF 0x02		; SAVE DATA POINTER
+    JUMP munge_save_data_pointer, IF 0x02	; SAVE DATA POINTER
 
-at 0x000000db : */	0x800c0002,0x000004d8,
+at 0x00000120 : */	0x800c0002,0x000004b8,
 /*
-    JUMP accept_message, IF 0x03		; RESTORE POINTERS 
+    JUMP munge_restore_pointers, IF 0x03	; RESTORE POINTERS 
 
-at 0x000000dd : */	0x800c0003,0x000004d8,
+at 0x00000122 : */	0x800c0003,0x0000051c,
 /*
     JUMP munge_disconnect, IF 0x04		; DISCONNECT
 
-at 0x000000df : */	0x800c0004,0x000003b4,
+at 0x00000124 : */	0x800c0004,0x00000580,
 /*
     INT int_msg_1, IF 0x07			; MESSAGE REJECT
 
-at 0x000000e1 : */	0x980c0007,0x01020000,
+at 0x00000126 : */	0x980c0007,0x01020000,
 /*
     INT int_msg_1, IF 0x0f			; INITIATE RECOVERY
 
-at 0x000000e3 : */	0x980c000f,0x01020000,
+at 0x00000128 : */	0x980c000f,0x01020000,
 /*
+
+
+
     JUMP reject_message
 
-at 0x000000e5 : */	0x80080000,0x000004b8,
+at 0x0000012a : */	0x80080000,0x00000618,
 /*
 
 munge_2:
     JUMP reject_message
 
-at 0x000000e7 : */	0x80080000,0x000004b8,
+at 0x0000012c : */	0x80080000,0x00000618,
 /*
+;
+; The SCSI standard allows targets to recover from transient 
+; error conditions by backing up the data pointer with a 
+; RESTORE POINTERS message.  
+;	
+; So, we must save and restore the _residual_ code as well as 
+; the current instruction pointer.  Because of this messiness,
+; it is simpler to put dynamic code in the dsa for this and to
+; just do a simple jump down there. 
+;
 
 munge_save_data_pointer:
-    CLEAR ACK
+    MOVE DSA0 + dsa_save_data_pointer TO SFBR
 
-at 0x000000e9 : */	0x60000040,0x00000000,
+at 0x0000012e : */	0x76100000,0x00000000,
 /*
-    RETURN
+    MOVE SFBR TO SCRATCH0
 
-at 0x000000eb : */	0x90080000,0x00000000,
+at 0x00000130 : */	0x6a340000,0x00000000,
 /*
+    MOVE DSA1 + 0xff TO SFBR WITH CARRY
 
-munge_disconnect:
-    MOVE SCNTL2 & 0x7f TO SCNTL2
+at 0x00000132 : */	0x7711ff00,0x00000000,
+/*
+    MOVE SFBR TO SCRATCH1
 
-at 0x000000ed : */	0x7c027f00,0x00000000,
+at 0x00000134 : */	0x6a350000,0x00000000,
 /*
-    CLEAR ACK
+    MOVE DSA2 + 0xff TO SFBR WITH CARRY 
 
-at 0x000000ef : */	0x60000040,0x00000000,
+at 0x00000136 : */	0x7712ff00,0x00000000,
 /*
+    MOVE SFBR TO SCRATCH2
 
+at 0x00000138 : */	0x6a360000,0x00000000,
+/*
+    MOVE DSA3 + 0xff TO SFBR WITH CARRY
 
-; Pass control to the DSA routine.  Note that we can not call
-; dsa_to_scratch here because that would clobber temp, which 
-; we must preserve.
-    MOVE DSA0 TO SFBR
+at 0x0000013a : */	0x7713ff00,0x00000000,
+/*
+    MOVE SFBR TO SCRATCH3
+
+at 0x0000013c : */	0x6a370000,0x00000000,
+/*
 
-at 0x000000f1 : */	0x72100000,0x00000000,
+    MOVE dmode_ncr_to_memory TO DMODE
+
+at 0x0000013e : */	0x78380000,0x00000000,
+/*
+    MOVE MEMORY 4, addr_scratch, jump_dsa_save + 4
+
+at 0x00000140 : */	0xc0000004,0x00000000,0x00000518,
+/*
+    MOVE dmode_memory_to_memory TO DMODE
+
+at 0x00000143 : */	0x78380000,0x00000000,
+/*
+jump_dsa_save:
+    JUMP 0
+
+at 0x00000145 : */	0x80080000,0x00000000,
+/*
+
+munge_restore_pointers:
+    MOVE DSA0 + dsa_restore_pointers TO SFBR
+
+at 0x00000147 : */	0x76100000,0x00000000,
 /*
     MOVE SFBR TO SCRATCH0
 
-at 0x000000f3 : */	0x6a340000,0x00000000,
+at 0x00000149 : */	0x6a340000,0x00000000,
 /*
-    MOVE DSA1 TO SFBR
+    MOVE DSA1 + 0xff TO SFBR WITH CARRY
 
-at 0x000000f5 : */	0x72110000,0x00000000,
+at 0x0000014b : */	0x7711ff00,0x00000000,
 /*
     MOVE SFBR TO SCRATCH1
 
-at 0x000000f7 : */	0x6a350000,0x00000000,
+at 0x0000014d : */	0x6a350000,0x00000000,
 /*
-    MOVE DSA2 TO SFBR
+    MOVE DSA2 + 0xff TO SFBR WITH CARRY
 
-at 0x000000f9 : */	0x72120000,0x00000000,
+at 0x0000014f : */	0x7712ff00,0x00000000,
 /*
     MOVE SFBR TO SCRATCH2
 
-at 0x000000fb : */	0x6a360000,0x00000000,
+at 0x00000151 : */	0x6a360000,0x00000000,
 /*
-    MOVE DSA3 TO SFBR
+    MOVE DSA3 + 0xff TO SFBR WITH CARRY
 
-at 0x000000fd : */	0x72130000,0x00000000,
+at 0x00000153 : */	0x7713ff00,0x00000000,
 /*
     MOVE SFBR TO SCRATCH3
 
-at 0x000000ff : */	0x6a370000,0x00000000,
+at 0x00000155 : */	0x6a370000,0x00000000,
 /*
 
     MOVE dmode_ncr_to_memory TO DMODE
 
-at 0x00000101 : */	0x78380000,0x00000000,
+at 0x00000157 : */	0x78380000,0x00000000,
 /*
-    MOVE MEMORY 4, addr_scratch, jump_to_dsa + 4
+    MOVE MEMORY 4, addr_scratch, jump_dsa_restore + 4
 
-at 0x00000103 : */	0xc0000004,0x00000000,0x00000424,
+at 0x00000159 : */	0xc0000004,0x00000000,0x0000057c,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x00000106 : */	0x78380000,0x00000000,
+at 0x0000015c : */	0x78380000,0x00000000,
 /*
-jump_to_dsa:
+jump_dsa_restore:
     JUMP 0
 
-at 0x00000108 : */	0x80080000,0x00000000,
+at 0x0000015e : */	0x80080000,0x00000000,
+/*
+
+
+munge_disconnect:
+
+
+
+
+
+
+
+
+
+ 
+
+
+
+
+
+
+    JUMP dsa_schedule
+
+at 0x00000160 : */	0x80080000,0x00000118,
 /*
 
 
@@ -1013,128 +1377,128 @@
 munge_extended:
     CLEAR ACK
 
-at 0x0000010a : */	0x60000040,0x00000000,
+at 0x00000162 : */	0x60000040,0x00000000,
 /*
     INT int_err_unexpected_phase, WHEN NOT MSG_IN
 
-at 0x0000010c : */	0x9f030000,0x00000000,
+at 0x00000164 : */	0x9f030000,0x00000000,
 /*
     MOVE 1, msg_buf + 1, WHEN MSG_IN
 
-at 0x0000010e : */	0x0f000001,0x00000001,
+at 0x00000166 : */	0x0f000001,0x00000001,
 /*
     JUMP munge_extended_2, IF 0x02
 
-at 0x00000110 : */	0x800c0002,0x00000458,
+at 0x00000168 : */	0x800c0002,0x000005b8,
 /*
     JUMP munge_extended_3, IF 0x03 
 
-at 0x00000112 : */	0x800c0003,0x00000488,
+at 0x0000016a : */	0x800c0003,0x000005e8,
 /*
     JUMP reject_message
 
-at 0x00000114 : */	0x80080000,0x000004b8,
+at 0x0000016c : */	0x80080000,0x00000618,
 /*
 
 munge_extended_2:
     CLEAR ACK
 
-at 0x00000116 : */	0x60000040,0x00000000,
+at 0x0000016e : */	0x60000040,0x00000000,
 /*
     MOVE 1, msg_buf + 2, WHEN MSG_IN
 
-at 0x00000118 : */	0x0f000001,0x00000002,
+at 0x00000170 : */	0x0f000001,0x00000002,
 /*
     JUMP reject_message, IF NOT 0x02	; Must be WDTR
 
-at 0x0000011a : */	0x80040002,0x000004b8,
+at 0x00000172 : */	0x80040002,0x00000618,
 /*
     CLEAR ACK
 
-at 0x0000011c : */	0x60000040,0x00000000,
+at 0x00000174 : */	0x60000040,0x00000000,
 /*
     MOVE 1, msg_buf + 3, WHEN MSG_IN
 
-at 0x0000011e : */	0x0f000001,0x00000003,
+at 0x00000176 : */	0x0f000001,0x00000003,
 /*
     INT int_msg_wdtr
 
-at 0x00000120 : */	0x98080000,0x01000000,
+at 0x00000178 : */	0x98080000,0x01000000,
 /*
 
 munge_extended_3:
     CLEAR ACK
 
-at 0x00000122 : */	0x60000040,0x00000000,
+at 0x0000017a : */	0x60000040,0x00000000,
 /*
     MOVE 1, msg_buf + 2, WHEN MSG_IN
 
-at 0x00000124 : */	0x0f000001,0x00000002,
+at 0x0000017c : */	0x0f000001,0x00000002,
 /*
     JUMP reject_message, IF NOT 0x01	; Must be SDTR
 
-at 0x00000126 : */	0x80040001,0x000004b8,
+at 0x0000017e : */	0x80040001,0x00000618,
 /*
     CLEAR ACK
 
-at 0x00000128 : */	0x60000040,0x00000000,
+at 0x00000180 : */	0x60000040,0x00000000,
 /*
     MOVE 2, msg_buf + 3, WHEN MSG_IN
 
-at 0x0000012a : */	0x0f000002,0x00000003,
+at 0x00000182 : */	0x0f000002,0x00000003,
 /*
     INT int_msg_sdtr
 
-at 0x0000012c : */	0x98080000,0x01010000,
+at 0x00000184 : */	0x98080000,0x01010000,
 /*
 
 ENTRY reject_message
 reject_message:
     SET ATN
 
-at 0x0000012e : */	0x58000008,0x00000000,
+at 0x00000186 : */	0x58000008,0x00000000,
 /*
     CLEAR ACK
 
-at 0x00000130 : */	0x60000040,0x00000000,
+at 0x00000188 : */	0x60000040,0x00000000,
 /*
     MOVE 1, NCR53c7xx_msg_reject, WHEN MSG_OUT
 
-at 0x00000132 : */	0x0e000001,0x00000000,
+at 0x0000018a : */	0x0e000001,0x00000000,
 /*
     RETURN
 
-at 0x00000134 : */	0x90080000,0x00000000,
+at 0x0000018c : */	0x90080000,0x00000000,
 /*
 
 ENTRY accept_message
 accept_message:
     CLEAR ACK
 
-at 0x00000136 : */	0x60000040,0x00000000,
+at 0x0000018e : */	0x60000040,0x00000000,
 /*
     RETURN
 
-at 0x00000138 : */	0x90080000,0x00000000,
+at 0x00000190 : */	0x90080000,0x00000000,
 /*
 
 ENTRY respond_message
-msg_respond:
+respond_message:
     SET ATN
 
-at 0x0000013a : */	0x58000008,0x00000000,
+at 0x00000192 : */	0x58000008,0x00000000,
 /*
     CLEAR ACK
 
-at 0x0000013c : */	0x60000040,0x00000000,
+at 0x00000194 : */	0x60000040,0x00000000,
 /*
     MOVE FROM dsa_msgout_other, WHEN MSG_OUT
 
-at 0x0000013e : */	0x1e000000,0x00000068,
+at 0x00000196 : */	0x1e000000,0x00000068,
 /*
     RETURN
 
-at 0x00000140 : */	0x90080000,0x00000000,
+at 0x00000198 : */	0x90080000,0x00000000,
 /*
 
 ;
@@ -1168,33 +1532,33 @@
 command_complete:
     MOVE FROM dsa_status, WHEN STATUS
 
-at 0x00000142 : */	0x1b000000,0x00000060,
+at 0x0000019a : */	0x1b000000,0x00000060,
 /*
 
     MOVE SFBR TO SCRATCH0		; Save status
 
-at 0x00000144 : */	0x6a340000,0x00000000,
+at 0x0000019c : */	0x6a340000,0x00000000,
 /*
 
 ENTRY command_complete_msgin
 command_complete_msgin:
     MOVE FROM dsa_msgin, WHEN MSG_IN
 
-at 0x00000146 : */	0x1f000000,0x00000058,
+at 0x0000019e : */	0x1f000000,0x00000058,
 /*
 ; Indicate that we should be expecting a disconnect
     MOVE SCNTL2 & 0x7f TO SCNTL2
 
-at 0x00000148 : */	0x7c027f00,0x00000000,
+at 0x000001a0 : */	0x7c027f00,0x00000000,
 /*
     CLEAR ACK
 
-at 0x0000014a : */	0x60000040,0x00000000,
+at 0x000001a2 : */	0x60000040,0x00000000,
 /*
 
     MOVE SCRATCH0 TO SFBR			
 
-at 0x0000014c : */	0x72340000,0x00000000,
+at 0x000001a4 : */	0x72340000,0x00000000,
 /*
 
 ;
@@ -1218,26 +1582,29 @@
 
     INTFLY
 
-at 0x0000014e : */	0x98180000,0x00000000,
+at 0x000001a6 : */	0x98180000,0x00000000,
 /*
 
+
+
+
     WAIT DISCONNECT
 
-at 0x00000150 : */	0x48000000,0x00000000,
+at 0x000001a8 : */	0x48000000,0x00000000,
 /*
 
     JUMP schedule
 
-at 0x00000152 : */	0x80080000,0x00000130,
+at 0x000001aa : */	0x80080000,0x000001a4,
 /*
 command_failed:
     WAIT DISCONNECT
 
-at 0x00000154 : */	0x48000000,0x00000000,
+at 0x000001ac : */	0x48000000,0x00000000,
 /*
     INT int_err_check_condition
 
-at 0x00000156 : */	0x98080000,0x00030000,
+at 0x000001ae : */	0x98080000,0x00030000,
 /*
 
 
@@ -1278,25 +1645,32 @@
 
 
 
+
+
+
     WAIT RESELECT wait_reselect_failed
 
-at 0x00000158 : */	0x50000000,0x0000067c,
+at 0x000001b0 : */	0x50000000,0x000007d8,
 /*
 
 reselected:
-    ; Read all data needed to reestablish the nexus - 
-    MOVE 1, reselected_identify, WHEN MSG_IN
 
-at 0x0000015a : */	0x0f000001,0x00000000,
+
+
+    CLEAR TARGET
+
+at 0x000001b2 : */	0x60000200,0x00000000,
 /*
+    MOVE dmode_memory_to_memory TO DMODE
 
-    ; Well add a jump to here after some how determining that 
-    ; tagged queueing isn't in use on this device.
-reselected_notag:    
-    MOVE MEMORY 1, NCR53c7xx_zero, reselected_tag
+at 0x000001b4 : */	0x78380000,0x00000000,
+/*
+    ; Read all data needed to reestablish the nexus - 
+    MOVE 1, reselected_identify, WHEN MSG_IN
 
-at 0x0000015c : */	0xc0000001,0x00000000,0x00000000,
+at 0x000001b6 : */	0x0f000001,0x00000000,
 /*
+    ; We used to CLEAR ACK here.
 
 
 
@@ -1305,75 +1679,74 @@
     ; Point DSA at the current head of the disconnected queue.
     MOVE dmode_memory_to_ncr  TO DMODE
 
-at 0x0000015f : */	0x78380000,0x00000000,
+at 0x000001b8 : */	0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, reconnect_dsa_head, addr_scratch
 
-at 0x00000161 : */	0xc0000004,0x00000000,0x00000000,
+at 0x000001ba : */	0xc0000004,0x00000000,0x00000000,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x00000164 : */	0x78380000,0x00000000,
+at 0x000001bd : */	0x78380000,0x00000000,
 /*
     CALL scratch_to_dsa
 
-at 0x00000166 : */	0x88080000,0x00000830,
+at 0x000001bf : */	0x88080000,0x000009cc,
 /*
 
     ; Fix the update-next pointer so that the reconnect_dsa_head
     ; pointer is the one that will be updated if this DSA is a hit 
     ; and we remove it from the queue.
 
-    MOVE MEMORY 4, reconnect_dsa_head, reselected_ok + 8
+    MOVE MEMORY 4, addr_reconnect_dsa_head, reselected_ok + 8
 
-at 0x00000168 : */	0xc0000004,0x00000000,0x00000660,
+at 0x000001c1 : */	0xc0000004,0x00000000,0x000007c4,
 /*
 
 ENTRY reselected_check_next
 reselected_check_next:
+
+
+
     ; Check for a NULL pointer.
     MOVE DSA0 TO SFBR
 
-at 0x0000016b : */	0x72100000,0x00000000,
+at 0x000001c4 : */	0x72100000,0x00000000,
 /*
     JUMP reselected_not_end, IF NOT 0
 
-at 0x0000016d : */	0x80040000,0x000005f4,
+at 0x000001c6 : */	0x80040000,0x00000758,
 /*
     MOVE DSA1 TO SFBR
 
-at 0x0000016f : */	0x72110000,0x00000000,
+at 0x000001c8 : */	0x72110000,0x00000000,
 /*
     JUMP reselected_not_end, IF NOT 0
 
-at 0x00000171 : */	0x80040000,0x000005f4,
+at 0x000001ca : */	0x80040000,0x00000758,
 /*
     MOVE DSA2 TO SFBR
 
-at 0x00000173 : */	0x72120000,0x00000000,
+at 0x000001cc : */	0x72120000,0x00000000,
 /*
     JUMP reselected_not_end, IF NOT 0
 
-at 0x00000175 : */	0x80040000,0x000005f4,
+at 0x000001ce : */	0x80040000,0x00000758,
 /*
     MOVE DSA3 TO SFBR
 
-at 0x00000177 : */	0x72130000,0x00000000,
+at 0x000001d0 : */	0x72130000,0x00000000,
 /*
     JUMP reselected_not_end, IF NOT 0
 
-at 0x00000179 : */	0x80040000,0x000005f4,
+at 0x000001d2 : */	0x80040000,0x00000758,
 /*
     INT int_err_unexpected_reselect
 
-at 0x0000017b : */	0x98080000,0x00020000,
+at 0x000001d4 : */	0x98080000,0x00020000,
 /*
 
 reselected_not_end:
-    MOVE DSA0 TO SFBR
-
-at 0x0000017d : */	0x72100000,0x00000000,
-/*
     ;
     ; XXX the ALU is only eight bits wide, and the assembler
     ; wont do the dirt work for us.  As long as dsa_check_reselect
@@ -1386,72 +1759,82 @@
     ; higher than the LSB.
     ;
 
+    MOVE DSA0 TO SFBR
+
+at 0x000001d6 : */	0x72100000,0x00000000,
+/*
     MOVE SFBR + dsa_check_reselect TO SCRATCH0
 
-at 0x0000017f : */	0x6e340000,0x00000000,
+at 0x000001d8 : */	0x6e340000,0x00000000,
 /*
     MOVE DSA1 TO SFBR
 
-at 0x00000181 : */	0x72110000,0x00000000,
+at 0x000001da : */	0x72110000,0x00000000,
 /*
     MOVE SFBR + 0xff TO SCRATCH1 WITH CARRY
 
-at 0x00000183 : */	0x6f35ff00,0x00000000,
+at 0x000001dc : */	0x6f35ff00,0x00000000,
 /*
     MOVE DSA2 TO SFBR
 
-at 0x00000185 : */	0x72120000,0x00000000,
+at 0x000001de : */	0x72120000,0x00000000,
 /*
     MOVE SFBR + 0xff TO SCRATCH2 WITH CARRY
 
-at 0x00000187 : */	0x6f36ff00,0x00000000,
+at 0x000001e0 : */	0x6f36ff00,0x00000000,
 /*
     MOVE DSA3 TO SFBR
 
-at 0x00000189 : */	0x72130000,0x00000000,
+at 0x000001e2 : */	0x72130000,0x00000000,
 /*
     MOVE SFBR + 0xff TO SCRATCH3 WITH CARRY
 
-at 0x0000018b : */	0x6f37ff00,0x00000000,
+at 0x000001e4 : */	0x6f37ff00,0x00000000,
 /*
 
     MOVE dmode_ncr_to_memory TO DMODE
 
-at 0x0000018d : */	0x78380000,0x00000000,
+at 0x000001e6 : */	0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, addr_scratch, reselected_check + 4
 
-at 0x0000018f : */	0xc0000004,0x00000000,0x00000654,
+at 0x000001e8 : */	0xc0000004,0x00000000,0x000007b8,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x00000192 : */	0x78380000,0x00000000,
+at 0x000001eb : */	0x78380000,0x00000000,
 /*
 reselected_check:
     JUMP 0
 
-at 0x00000194 : */	0x80080000,0x00000000,
+at 0x000001ed : */	0x80080000,0x00000000,
 /*
 
 
 ;
 ;
+ENTRY reselected_ok
 reselected_ok:
     MOVE MEMORY 4, 0, 0				; Patched : first word
 
-at 0x00000196 : */	0xc0000004,0x00000000,0x00000000,
+at 0x000001ef : */	0xc0000004,0x00000000,0x00000000,
 /*
-						; 	is this successful 
-						; 	dsa_next
-						; Second word is 
-						;	unsuccessful dsa_next
-    CLEAR ACK					; Accept last message
+						; 	is address of 
+						;       successful dsa_next
+						; Second word is last 
+						;	unsuccessful dsa_next,
+						;	starting with 
+						;       dsa_reconnect_head
+    ; We used to CLEAR ACK here.
+
+
+
+
+
 
-at 0x00000199 : */	0x60000040,0x00000000,
-/*
     RETURN					; Return control to where
 
-at 0x0000019b : */	0x90080000,0x00000000,
+at 0x000001f2 : */	0x90080000,0x00000000,
 /*
 
 
@@ -1460,7 +1843,7 @@
 selected:
     INT int_err_selected;
 
-at 0x0000019d : */	0x98080000,0x00010000,
+at 0x000001f4 : */	0x98080000,0x00010000,
 /*
 
 ;
@@ -1472,60 +1855,75 @@
 ; 
 
 wait_reselect_failed:
-; Reading CTEST2 clears the SIG_P bit in the ISTAT register.
-    MOVE CTEST2 & 0x40 TO SFBR	
 
-at 0x0000019f : */	0x741a4000,0x00000000,
-/*
-    JUMP schedule, IF 0x40
 
-at 0x000001a1 : */	0x800c0040,0x00000130,
-/*
+
     MOVE SIST0 & 0x20 TO SFBR
 
-at 0x000001a3 : */	0x74422000,0x00000000,
+at 0x000001f6 : */	0x74422000,0x00000000,
 /*
     JUMP selected, IF 0x20
 
-at 0x000001a5 : */	0x800c0020,0x00000674,
+at 0x000001f8 : */	0x800c0020,0x000007d0,
+/*
+; Reading CTEST2 clears the SIG_P bit in the ISTAT register.
+    MOVE CTEST2 & 0x40 TO SFBR	
+
+at 0x000001fa : */	0x741a4000,0x00000000,
+/*
+    JUMP schedule, IF 0x40
+
+at 0x000001fc : */	0x800c0040,0x000001a4,
 /*
 ; FIXME : Something bogus happened, and we shouldn't fail silently.
-    JUMP schedule
 
-at 0x000001a7 : */	0x80080000,0x00000130,
+
+
+    INT int_debug_panic
+
+at 0x000001fe : */	0x98080000,0x030b0000,
 /*
 
+
+
 select_failed:
-; If SIGP is set, the user just gave us another command, and
-; we should restart or return to the scheduler.
-; Reading CTEST2 clears the SIG_P bit in the ISTAT register.
-    MOVE CTEST2 & 0x40 TO SFBR	
 
-at 0x000001a9 : */	0x741a4000,0x00000000,
-/*
-    JUMP select, IF 0x40
 
-at 0x000001ab : */	0x800c0040,0x00000194,
-/*
+
 ; Otherwise, mask the selected and reselected bits off SIST0
     MOVE SIST0 & 0x30 TO SFBR
 
-at 0x000001ad : */	0x74423000,0x00000000,
+at 0x00000200 : */	0x74423000,0x00000000,
 /*
     JUMP selected, IF 0x20
 
-at 0x000001af : */	0x800c0020,0x00000674,
+at 0x00000202 : */	0x800c0020,0x000007d0,
 /*
     JUMP reselected, IF 0x10 
 
-at 0x000001b1 : */	0x800c0010,0x00000568,
+at 0x00000204 : */	0x800c0010,0x000006c8,
+/*
+; If SIGP is set, the user just gave us another command, and
+; we should restart or return to the scheduler.
+; Reading CTEST2 clears the SIG_P bit in the ISTAT register.
+    MOVE CTEST2 & 0x40 TO SFBR	
+
+at 0x00000206 : */	0x741a4000,0x00000000,
+/*
+    JUMP select, IF 0x40
+
+at 0x00000208 : */	0x800c0040,0x00000208,
 /*
 ; FIXME : Something bogus happened, and we shouldn't fail silently.
-    JUMP schedule
 
-at 0x000001b3 : */	0x80080000,0x00000130,
+
+
+    INT int_debug_panic
+
+at 0x0000020a : */	0x98080000,0x030b0000,
 /*
 
+
 ;
 ; test_1
 ; test_2
@@ -1547,11 +1945,11 @@
 test_1:
     MOVE MEMORY 4, test_src, test_dest
 
-at 0x000001b5 : */	0xc0000004,0x00000000,0x00000000,
+at 0x0000020c : */	0xc0000004,0x00000000,0x00000000,
 /*
     INT int_test_1
 
-at 0x000001b8 : */	0x98080000,0x04000000,
+at 0x0000020f : */	0x98080000,0x04000000,
 /*
 
 ;
@@ -1562,61 +1960,61 @@
 test_2:
     CLEAR TARGET
 
-at 0x000001ba : */	0x60000200,0x00000000,
+at 0x00000211 : */	0x60000200,0x00000000,
 /*
     SELECT ATN FROM 0, test_2_fail
 
-at 0x000001bc : */	0x43000000,0x00000740,
+at 0x00000213 : */	0x43000000,0x0000089c,
 /*
     JUMP test_2_msgout, WHEN MSG_OUT
 
-at 0x000001be : */	0x860b0000,0x00000700,
+at 0x00000215 : */	0x860b0000,0x0000085c,
 /*
 ENTRY test_2_msgout
 test_2_msgout:
     MOVE FROM 8, WHEN MSG_OUT
 
-at 0x000001c0 : */	0x1e000000,0x00000008,
+at 0x00000217 : */	0x1e000000,0x00000008,
 /*
     MOVE FROM 16, WHEN CMD 
 
-at 0x000001c2 : */	0x1a000000,0x00000010,
+at 0x00000219 : */	0x1a000000,0x00000010,
 /*
     MOVE FROM 24, WHEN DATA_IN
 
-at 0x000001c4 : */	0x19000000,0x00000018,
+at 0x0000021b : */	0x19000000,0x00000018,
 /*
     MOVE FROM 32, WHEN STATUS
 
-at 0x000001c6 : */	0x1b000000,0x00000020,
+at 0x0000021d : */	0x1b000000,0x00000020,
 /*
     MOVE FROM 40, WHEN MSG_IN
 
-at 0x000001c8 : */	0x1f000000,0x00000028,
+at 0x0000021f : */	0x1f000000,0x00000028,
 /*
     MOVE SCNTL2 & 0x7f TO SCNTL2
 
-at 0x000001ca : */	0x7c027f00,0x00000000,
+at 0x00000221 : */	0x7c027f00,0x00000000,
 /*
     CLEAR ACK
 
-at 0x000001cc : */	0x60000040,0x00000000,
+at 0x00000223 : */	0x60000040,0x00000000,
 /*
     WAIT DISCONNECT
 
-at 0x000001ce : */	0x48000000,0x00000000,
+at 0x00000225 : */	0x48000000,0x00000000,
 /*
 test_2_fail:
     INT int_test_2
 
-at 0x000001d0 : */	0x98080000,0x04010000,
+at 0x00000227 : */	0x98080000,0x04010000,
 /*
 
 ENTRY debug_break
 debug_break:
     INT int_debug_break
 
-at 0x000001d2 : */	0x98080000,0x03000000,
+at 0x00000229 : */	0x98080000,0x03000000,
 /*
 
 ;
@@ -1632,96 +2030,129 @@
 target_abort:
     SET TARGET
 
-at 0x000001d4 : */	0x58000200,0x00000000,
+at 0x0000022b : */	0x58000200,0x00000000,
 /*
     DISCONNECT
 
-at 0x000001d6 : */	0x48000000,0x00000000,
+at 0x0000022d : */	0x48000000,0x00000000,
 /*
     CLEAR TARGET
 
-at 0x000001d8 : */	0x60000200,0x00000000,
+at 0x0000022f : */	0x60000200,0x00000000,
 /*
     JUMP schedule
 
-at 0x000001da : */	0x80080000,0x00000130,
+at 0x00000231 : */	0x80080000,0x000001a4,
 /*
     
 ENTRY initiator_abort
 initiator_abort:
     SET ATN
 
-at 0x000001dc : */	0x58000008,0x00000000,
+at 0x00000233 : */	0x58000008,0x00000000,
+/*
+;
+; The SCSI-I specification says that targets may go into MSG out at 
+; their leisure upon receipt of the ATN single.  On all versions of the 
+; specification, we can't change phases until REQ transitions true->false, 
+; so we need to sink/source one byte of data to allow the transition.
+;
+; For the sake of safety, we'll only source one byte of data in all 
+; cases, but to accomodate the SCSI-I dain bramage, we'll sink an  
+; arbitrary number of bytes.
+    JUMP spew_cmd, WHEN CMD
+
+at 0x00000235 : */	0x820b0000,0x00000904,
+/*
+    JUMP eat_msgin, WHEN MSG_IN
+
+at 0x00000237 : */	0x870b0000,0x00000914,
+/*
+    JUMP eat_datain, WHEN DATA_IN
+
+at 0x00000239 : */	0x810b0000,0x00000944,
+/*
+    JUMP eat_status, WHEN STATUS
+
+at 0x0000023b : */	0x830b0000,0x0000092c,
+/*
+    JUMP spew_dataout, WHEN DATA_OUT
+
+at 0x0000023d : */	0x800b0000,0x0000095c,
 /*
-; In order to abort the currently established nexus, we 
-; need to source/sink up to one byte of data in any SCSI phase, 
-; since the phase cannot change until REQ transitions 
-; false->true
-    JUMP no_eat_cmd, WHEN NOT CMD
+    JUMP sated
 
-at 0x000001de : */	0x82030000,0x00000788,
+at 0x0000023f : */	0x80080000,0x00000964,
 /*
+spew_cmd:
     MOVE 1, NCR53c7xx_zero, WHEN CMD
 
-at 0x000001e0 : */	0x0a000001,0x00000000,
+at 0x00000241 : */	0x0a000001,0x00000000,
 /*
-no_eat_cmd:
-    JUMP no_eat_msg, WHEN NOT MSG_IN
+    JUMP sated
 
-at 0x000001e2 : */	0x87030000,0x00000798,
+at 0x00000243 : */	0x80080000,0x00000964,
 /*
+eat_msgin:
     MOVE 1, NCR53c7xx_sink, WHEN MSG_IN
 
-at 0x000001e4 : */	0x0f000001,0x00000000,
+at 0x00000245 : */	0x0f000001,0x00000000,
 /*
-no_eat_msg:
-    JUMP no_eat_data, WHEN NOT DATA_IN
+    JUMP eat_msgin, WHEN MSG_IN
 
-at 0x000001e6 : */	0x81030000,0x000007a8,
+at 0x00000247 : */	0x870b0000,0x00000914,
 /*
-    MOVE 1, NCR53c7xx_sink, WHEN DATA_IN
+    JUMP sated
 
-at 0x000001e8 : */	0x09000001,0x00000000,
+at 0x00000249 : */	0x80080000,0x00000964,
 /*
-no_eat_data:
-    JUMP no_eat_status, WHEN NOT STATUS
+eat_status:
+    MOVE 1, NCR53c7xx_sink, WHEN STATUS
 
-at 0x000001ea : */	0x83030000,0x000007b8,
+at 0x0000024b : */	0x0b000001,0x00000000,
 /*
-    MOVE 1, NCR53c7xx_sink, WHEN STATUS
+    JUMP eat_status, WHEN STATUS
 
-at 0x000001ec : */	0x0b000001,0x00000000,
+at 0x0000024d : */	0x830b0000,0x0000092c,
 /*
-no_eat_status:
-    JUMP no_source_data, WHEN NOT DATA_OUT
+    JUMP sated
 
-at 0x000001ee : */	0x80030000,0x000007c8,
+at 0x0000024f : */	0x80080000,0x00000964,
 /*
+eat_datain:
+    MOVE 1, NCR53c7xx_sink, WHEN DATA_IN
+
+at 0x00000251 : */	0x09000001,0x00000000,
+/*
+    JUMP eat_datain, WHEN DATA_IN
+
+at 0x00000253 : */	0x810b0000,0x00000944,
+/*
+    JUMP sated
+
+at 0x00000255 : */	0x80080000,0x00000964,
+/*
+spew_dataout:
     MOVE 1, NCR53c7xx_zero, WHEN DATA_OUT
 
-at 0x000001f0 : */	0x08000001,0x00000000,
+at 0x00000257 : */	0x08000001,0x00000000,
 /*
-no_source_data:
-;
-; If DSP points here, and a phase mismatch is encountered, we need to 
-; do a bus reset.
-;
-    
+sated:
     MOVE SCNTL2 & 0x7f TO SCNTL2
 
-at 0x000001f2 : */	0x7c027f00,0x00000000,
+at 0x00000259 : */	0x7c027f00,0x00000000,
 /*
     MOVE 1, NCR53c7xx_msg_abort, WHEN MSG_OUT
 
-at 0x000001f4 : */	0x0e000001,0x00000000,
+at 0x0000025b : */	0x0e000001,0x00000000,
 /*
     WAIT DISCONNECT
 
-at 0x000001f6 : */	0x48000000,0x00000000,
+at 0x0000025d : */	0x48000000,0x00000000,
 /*
     INT int_norm_aborted
 
-at 0x000001f8 : */	0x98080000,0x02040000,
+at 0x0000025f : */	0x98080000,0x02040000,
 /*
 
 ;
@@ -1742,175 +2173,178 @@
 dsa_to_scratch:
     MOVE DSA0 TO SFBR
 
-at 0x000001fa : */	0x72100000,0x00000000,
+at 0x00000261 : */	0x72100000,0x00000000,
 /*
     MOVE SFBR TO SCRATCH0
 
-at 0x000001fc : */	0x6a340000,0x00000000,
+at 0x00000263 : */	0x6a340000,0x00000000,
 /*
     MOVE DSA1 TO SFBR
 
-at 0x000001fe : */	0x72110000,0x00000000,
+at 0x00000265 : */	0x72110000,0x00000000,
 /*
     MOVE SFBR TO SCRATCH1
 
-at 0x00000200 : */	0x6a350000,0x00000000,
+at 0x00000267 : */	0x6a350000,0x00000000,
 /*
     MOVE DSA2 TO SFBR
 
-at 0x00000202 : */	0x72120000,0x00000000,
+at 0x00000269 : */	0x72120000,0x00000000,
 /*
     MOVE SFBR TO SCRATCH2
 
-at 0x00000204 : */	0x6a360000,0x00000000,
+at 0x0000026b : */	0x6a360000,0x00000000,
 /*
     MOVE DSA3 TO SFBR
 
-at 0x00000206 : */	0x72130000,0x00000000,
+at 0x0000026d : */	0x72130000,0x00000000,
 /*
     MOVE SFBR TO SCRATCH3
 
-at 0x00000208 : */	0x6a370000,0x00000000,
+at 0x0000026f : */	0x6a370000,0x00000000,
 /*
     RETURN
 
-at 0x0000020a : */	0x90080000,0x00000000,
+at 0x00000271 : */	0x90080000,0x00000000,
 /*
 
 scratch_to_dsa:
     MOVE SCRATCH0 TO SFBR
 
-at 0x0000020c : */	0x72340000,0x00000000,
+at 0x00000273 : */	0x72340000,0x00000000,
 /*
     MOVE SFBR TO DSA0
 
-at 0x0000020e : */	0x6a100000,0x00000000,
+at 0x00000275 : */	0x6a100000,0x00000000,
 /*
     MOVE SCRATCH1 TO SFBR
 
-at 0x00000210 : */	0x72350000,0x00000000,
+at 0x00000277 : */	0x72350000,0x00000000,
 /*
     MOVE SFBR TO DSA1
 
-at 0x00000212 : */	0x6a110000,0x00000000,
+at 0x00000279 : */	0x6a110000,0x00000000,
 /*
     MOVE SCRATCH2 TO SFBR
 
-at 0x00000214 : */	0x72360000,0x00000000,
+at 0x0000027b : */	0x72360000,0x00000000,
 /*
     MOVE SFBR TO DSA2
 
-at 0x00000216 : */	0x6a120000,0x00000000,
+at 0x0000027d : */	0x6a120000,0x00000000,
 /*
     MOVE SCRATCH3 TO SFBR
 
-at 0x00000218 : */	0x72370000,0x00000000,
+at 0x0000027f : */	0x72370000,0x00000000,
 /*
     MOVE SFBR TO DSA3
 
-at 0x0000021a : */	0x6a130000,0x00000000,
+at 0x00000281 : */	0x6a130000,0x00000000,
 /*
     RETURN
 
-at 0x0000021c : */	0x90080000,0x00000000,
+at 0x00000283 : */	0x90080000,0x00000000,
 };
 
 #define A_NCR53c7xx_msg_abort	0x00000000
 unsigned long A_NCR53c7xx_msg_abort_used[] = {
-	0x000001f5,
+	0x0000025c,
 };
 
 #define A_NCR53c7xx_msg_reject	0x00000000
 unsigned long A_NCR53c7xx_msg_reject_used[] = {
-	0x00000133,
+	0x0000018b,
 };
 
 #define A_NCR53c7xx_sink	0x00000000
 unsigned long A_NCR53c7xx_sink_used[] = {
-	0x000001e5,
-	0x000001e9,
-	0x000001ed,
+	0x00000246,
+	0x0000024c,
+	0x00000252,
 };
 
 #define A_NCR53c7xx_zero	0x00000000
 unsigned long A_NCR53c7xx_zero_used[] = {
-	0x0000015d,
-	0x000001e1,
-	0x000001f1,
+	0x00000242,
+	0x00000258,
+};
+
+#define A_addr_reconnect_dsa_head	0x00000000
+unsigned long A_addr_reconnect_dsa_head_used[] = {
+	0x000001c2,
 };
 
 #define A_addr_scratch	0x00000000
 unsigned long A_addr_scratch_used[] = {
 	0x00000007,
-	0x0000003a,
-	0x00000046,
-	0x00000050,
-	0x0000007a,
-	0x000000a8,
-	0x000000be,
-	0x00000104,
-	0x00000163,
-	0x00000190,
-};
-
-#define A_addr_sfbr	0x00000000
-unsigned long A_addr_sfbr_used[] = {
-	0x00000016,
+	0x00000032,
+	0x00000053,
+	0x0000005f,
+	0x0000006d,
+	0x0000009b,
+	0x000000c9,
+	0x000000df,
+	0x00000141,
+	0x0000015a,
+	0x000001bc,
+	0x000001e9,
 };
 
 #define A_addr_temp	0x00000000
 unsigned long A_addr_temp_used[] = {
-	0x00000027,
+	0x00000011,
+	0x00000020,
 };
 
 #define A_dmode_memory_to_memory	0x00000000
 unsigned long A_dmode_memory_to_memory_used[] = {
 	0x00000008,
-	0x00000019,
-	0x00000029,
-	0x0000003c,
-	0x00000048,
-	0x00000051,
-	0x0000007c,
-	0x000000aa,
-	0x000000c0,
-	0x00000106,
-	0x00000164,
-	0x00000192,
+	0x00000013,
+	0x00000021,
+	0x00000033,
+	0x00000055,
+	0x00000061,
+	0x0000006e,
+	0x0000009d,
+	0x000000cb,
+	0x000000e1,
+	0x00000143,
+	0x0000015c,
+	0x000001b4,
+	0x000001bd,
+	0x000001eb,
 };
 
 #define A_dmode_memory_to_ncr	0x00000000
 unsigned long A_dmode_memory_to_ncr_used[] = {
 	0x00000003,
-	0x00000012,
-	0x0000004c,
-	0x0000015f,
+	0x0000001c,
+	0x0000002e,
+	0x00000069,
+	0x000001b8,
 };
 
 #define A_dmode_ncr_to_memory	0x00000000
 unsigned long A_dmode_ncr_to_memory_used[] = {
-	0x00000024,
-	0x00000037,
-	0x00000043,
-	0x00000077,
-	0x000000a5,
-	0x000000bb,
-	0x00000101,
-	0x0000018d,
-};
-
-#define A_dmode_ncr_to_ncr	0x00000000
-unsigned long A_dmode_ncr_to_ncr_used[] = {
+	0x0000000e,
+	0x00000050,
+	0x0000005c,
+	0x00000098,
+	0x000000c6,
+	0x000000dc,
+	0x0000013e,
+	0x00000157,
+	0x000001e6,
 };
 
 #define A_dsa_check_reselect	0x00000000
 unsigned long A_dsa_check_reselect_used[] = {
-	0x0000017f,
+	0x000001d8,
 };
 
 #define A_dsa_cmdout	0x00000048
 unsigned long A_dsa_cmdout_used[] = {
-	0x0000008c,
+	0x000000ad,
 };
 
 #define A_dsa_cmnd	0x00000038
@@ -1919,108 +2353,121 @@
 
 #define A_dsa_datain	0x00000054
 unsigned long A_dsa_datain_used[] = {
-	0x000000b3,
+	0x000000d4,
 };
 
 #define A_dsa_dataout	0x00000050
 unsigned long A_dsa_dataout_used[] = {
-	0x0000009d,
+	0x000000be,
 };
 
 #define A_dsa_end	0x00000070
 unsigned long A_dsa_end_used[] = {
 };
 
-#define A_dsa_fields_start	0x00000024
+#define A_dsa_fields_start	0x00000000
 unsigned long A_dsa_fields_start_used[] = {
 };
 
 #define A_dsa_msgin	0x00000058
 unsigned long A_dsa_msgin_used[] = {
-	0x00000147,
+	0x0000019f,
 };
 
 #define A_dsa_msgout	0x00000040
 unsigned long A_dsa_msgout_used[] = {
-	0x0000006c,
+	0x00000089,
 };
 
 #define A_dsa_msgout_other	0x00000068
 unsigned long A_dsa_msgout_other_used[] = {
-	0x0000013f,
+	0x00000197,
 };
 
 #define A_dsa_next	0x00000030
 unsigned long A_dsa_next_used[] = {
-	0x0000002f,
-	0x0000006f,
+	0x00000048,
+	0x00000090,
+};
+
+#define A_dsa_restore_pointers	0x00000000
+unsigned long A_dsa_restore_pointers_used[] = {
+	0x00000147,
+};
+
+#define A_dsa_save_data_pointer	0x00000000
+unsigned long A_dsa_save_data_pointer_used[] = {
+	0x0000012e,
 };
 
 #define A_dsa_select	0x0000003c
 unsigned long A_dsa_select_used[] = {
-	0x00000067,
+	0x00000084,
 };
 
 #define A_dsa_status	0x00000060
 unsigned long A_dsa_status_used[] = {
-	0x00000143,
+	0x0000019b,
 };
 
-#define A_dsa_temp_dsa_next	0x00000000
-unsigned long A_dsa_temp_dsa_next_used[] = {
+#define A_dsa_temp_addr_next	0x00000000
+unsigned long A_dsa_temp_addr_next_used[] = {
 	0x00000001,
-	0x00000006,
-	0x0000001c,
+	0x0000003a,
 };
 
-#define A_dsa_temp_jump_resume	0x00000000
-unsigned long A_dsa_temp_jump_resume_used[] = {
-	0x00000028,
+#define A_dsa_temp_addr_residual	0x00000000
+unsigned long A_dsa_temp_addr_residual_used[] = {
+	0x00000016,
+	0x00000025,
+};
+
+#define A_dsa_temp_addr_saved_pointer	0x00000000
+unsigned long A_dsa_temp_addr_saved_pointer_used[] = {
+	0x00000012,
+	0x0000001f,
+};
+
+#define A_dsa_temp_addr_saved_residual	0x00000000
+unsigned long A_dsa_temp_addr_saved_residual_used[] = {
+	0x00000017,
+	0x00000024,
 };
 
 #define A_dsa_temp_lun	0x00000000
 unsigned long A_dsa_temp_lun_used[] = {
-	0x00000017,
+	0x00000037,
+};
+
+#define A_dsa_temp_next	0x00000000
+unsigned long A_dsa_temp_next_used[] = {
+	0x00000006,
 };
 
 #define A_dsa_temp_sync	0x00000000
 unsigned long A_dsa_temp_sync_used[] = {
-	0x00000021,
+	0x0000003f,
 };
 
 #define A_dsa_temp_target	0x00000000
 unsigned long A_dsa_temp_target_used[] = {
-	0x00000010,
+	0x0000002c,
 };
 
 #define A_int_debug_break	0x03000000
 unsigned long A_int_debug_break_used[] = {
-	0x000001d3,
-};
-
-#define A_int_debug_dsa_loaded	0x03030000
-unsigned long A_int_debug_dsa_loaded_used[] = {
-};
-
-#define A_int_debug_head	0x03050000
-unsigned long A_int_debug_head_used[] = {
-};
-
-#define A_int_debug_idle	0x03020000
-unsigned long A_int_debug_idle_used[] = {
-};
-
-#define A_int_debug_reselected	0x03040000
-unsigned long A_int_debug_reselected_used[] = {
+	0x0000022a,
 };
 
-#define A_int_debug_scheduled	0x03010000
-unsigned long A_int_debug_scheduled_used[] = {
+#define A_int_debug_panic	0x030b0000
+unsigned long A_int_debug_panic_used[] = {
+	0x000001ff,
+	0x0000020b,
 };
 
 #define A_int_err_check_condition	0x00030000
 unsigned long A_int_err_check_condition_used[] = {
-	0x00000157,
+	0x000001af,
 };
 
 #define A_int_err_no_phase	0x00040000
@@ -2029,46 +2476,51 @@
 
 #define A_int_err_selected	0x00010000
 unsigned long A_int_err_selected_used[] = {
-	0x0000019e,
+	0x000001f5,
 };
 
 #define A_int_err_unexpected_phase	0x00000000
 unsigned long A_int_err_unexpected_phase_used[] = {
-	0x00000084,
-	0x0000008a,
-	0x0000008e,
-	0x00000092,
-	0x000000c8,
-	0x000000cc,
-	0x000000ce,
-	0x000000d0,
+	0x000000a5,
+	0x000000ab,
+	0x000000b3,
+	0x000000e9,
+	0x000000ed,
+	0x000000ef,
+	0x000000f7,
+	0x000000fb,
+	0x000000fd,
+	0x00000105,
+	0x00000109,
+	0x0000010b,
 	0x0000010d,
+	0x00000165,
 };
 
 #define A_int_err_unexpected_reselect	0x00020000
 unsigned long A_int_err_unexpected_reselect_used[] = {
-	0x0000017c,
+	0x000001d5,
 };
 
 #define A_int_msg_1	0x01020000
 unsigned long A_int_msg_1_used[] = {
-	0x000000e2,
-	0x000000e4,
+	0x00000127,
+	0x00000129,
 };
 
 #define A_int_msg_sdtr	0x01010000
 unsigned long A_int_msg_sdtr_used[] = {
-	0x0000012d,
+	0x00000185,
 };
 
 #define A_int_msg_wdtr	0x01000000
 unsigned long A_int_msg_wdtr_used[] = {
-	0x00000121,
+	0x00000179,
 };
 
 #define A_int_norm_aborted	0x02040000
 unsigned long A_int_norm_aborted_used[] = {
-	0x000001f9,
+	0x00000260,
 };
 
 #define A_int_norm_command_complete	0x02020000
@@ -2093,12 +2545,12 @@
 
 #define A_int_test_1	0x04000000
 unsigned long A_int_test_1_used[] = {
-	0x000001b9,
+	0x00000210,
 };
 
 #define A_int_test_2	0x04010000
 unsigned long A_int_test_2_used[] = {
-	0x000001d1,
+	0x00000228,
 };
 
 #define A_int_test_3	0x04020000
@@ -2107,147 +2559,167 @@
 
 #define A_issue_dsa_head	0x00000000
 unsigned long A_issue_dsa_head_used[] = {
-	0x0000004f,
-	0x00000080,
+	0x0000006c,
+	0x000000a1,
 };
 
 #define A_msg_buf	0x00000000
 unsigned long A_msg_buf_used[] = {
-	0x000000d6,
-	0x0000010f,
-	0x00000119,
-	0x0000011f,
-	0x00000125,
-	0x0000012b,
+	0x0000011b,
+	0x00000167,
+	0x00000171,
+	0x00000177,
+	0x0000017d,
+	0x00000183,
 };
 
 #define A_reconnect_dsa_head	0x00000000
 unsigned long A_reconnect_dsa_head_used[] = {
-	0x0000003f,
-	0x00000047,
-	0x00000162,
-	0x00000169,
+	0x00000058,
+	0x00000060,
+	0x000001bb,
 };
 
 #define A_reselected_identify	0x00000000
 unsigned long A_reselected_identify_used[] = {
-	0x00000015,
-	0x0000015b,
+	0x00000031,
+	0x000001b7,
 };
 
 #define A_reselected_tag	0x00000000
 unsigned long A_reselected_tag_used[] = {
-	0x0000015e,
 };
 
 #define A_test_dest	0x00000000
 unsigned long A_test_dest_used[] = {
-	0x000001b7,
+	0x0000020e,
 };
 
 #define A_test_src	0x00000000
 unsigned long A_test_src_used[] = {
-	0x000001b6,
+	0x0000020d,
 };
 
-#define Ent_accept_message	0x000004d8
-#define Ent_cmdout_cmdout	0x0000022c
-#define Ent_command_complete	0x00000508
-#define Ent_command_complete_msgin	0x00000518
-#define Ent_debug_break	0x00000748
-#define Ent_dsa_code_check_reselect	0x00000038
+#define Ent_accept_message	0x00000638
+#define Ent_cmdout_cmdout	0x000002b0
+#define Ent_command_complete	0x00000668
+#define Ent_command_complete_msgin	0x00000678
+#define Ent_data_transfer	0x000002b8
+#define Ent_datain_to_jump	0x0000038c
+#define Ent_debug_break	0x000008a4
+#define Ent_dsa_code_check_reselect	0x000000a8
+#define Ent_dsa_code_restore_pointers	0x00000070
+#define Ent_dsa_code_save_data_pointer	0x00000038
 #define Ent_dsa_code_template	0x00000000
-#define Ent_dsa_code_template_end	0x000000b4
-#define Ent_dsa_jump_resume	0x00000088
-#define Ent_dsa_schedule	0x000000b4
-#define Ent_dsa_zero	0x00000090
-#define Ent_initiator_abort	0x00000770
-#define Ent_msg_in	0x00000354
-#define Ent_other_transfer	0x0000031c
-#define Ent_reject_message	0x000004b8
-#define Ent_reselected_check_next	0x000005ac
-#define Ent_respond_message	0x00000000
-#define Ent_schedule	0x00000130
-#define Ent_select	0x00000194
-#define Ent_select_msgout	0x000001ac
-#define Ent_target_abort	0x00000750
-#define Ent_test_1	0x000006d4
-#define Ent_test_2	0x000006e8
-#define Ent_test_2_msgout	0x00000700
+#define Ent_dsa_code_template_end	0x00000118
+#define Ent_dsa_schedule	0x00000118
+#define Ent_dsa_zero	0x00000118
+#define Ent_end_data_transfer	0x000002f0
+#define Ent_initiator_abort	0x000008cc
+#define Ent_msg_in	0x00000468
+#define Ent_msg_in_restart	0x00000448
+#define Ent_other_in	0x000003d8
+#define Ent_other_out	0x000003a0
+#define Ent_other_transfer	0x00000410
+#define Ent_reject_message	0x00000618
+#define Ent_reselected_check_next	0x00000710
+#define Ent_reselected_ok	0x000007bc
+#define Ent_respond_message	0x00000648
+#define Ent_schedule	0x000001a4
+#define Ent_select	0x00000208
+#define Ent_select_msgout	0x00000220
+#define Ent_target_abort	0x000008ac
+#define Ent_test_1	0x00000830
+#define Ent_test_2	0x00000844
+#define Ent_test_2_msgout	0x0000085c
 unsigned long LABELPATCHES[] = {
 	0x00000002,
 	0x0000000b,
 	0x0000000d,
-	0x0000001d,
-	0x0000001f,
-	0x0000002c,
-	0x0000002e,
 	0x0000003b,
-	0x00000042,
+	0x0000003d,
+	0x00000047,
 	0x00000054,
-	0x00000058,
-	0x0000005c,
-	0x00000060,
-	0x00000064,
-	0x00000068,
-	0x0000006a,
-	0x0000006e,
-	0x0000007b,
-	0x00000082,
-	0x00000086,
-	0x00000088,
-	0x00000090,
-	0x00000094,
-	0x00000096,
-	0x00000098,
-	0x0000009a,
+	0x0000005b,
+	0x00000071,
+	0x00000075,
+	0x00000079,
+	0x0000007d,
+	0x00000081,
+	0x00000085,
+	0x00000087,
+	0x0000008f,
 	0x0000009c,
+	0x000000a3,
+	0x000000a7,
 	0x000000a9,
-	0x000000ae,
-	0x000000b2,
-	0x000000bf,
-	0x000000c4,
+	0x000000af,
+	0x000000b1,
+	0x000000b5,
+	0x000000b7,
+	0x000000b9,
+	0x000000bb,
+	0x000000bd,
 	0x000000ca,
-	0x000000d2,
-	0x000000d4,
-	0x000000d8,
-	0x000000da,
-	0x000000dc,
-	0x000000de,
+	0x000000cf,
+	0x000000d3,
 	0x000000e0,
-	0x000000e6,
-	0x000000e8,
-	0x00000105,
+	0x000000e5,
+	0x000000eb,
+	0x000000f1,
+	0x000000f3,
+	0x000000f9,
+	0x000000ff,
+	0x00000101,
+	0x00000107,
+	0x0000010f,
 	0x00000111,
-	0x00000113,
-	0x00000115,
-	0x0000011b,
-	0x00000127,
-	0x00000153,
-	0x00000159,
-	0x00000167,
-	0x0000016a,
-	0x0000016e,
-	0x00000172,
-	0x00000176,
-	0x0000017a,
-	0x00000191,
-	0x000001a2,
-	0x000001a6,
-	0x000001a8,
-	0x000001ac,
-	0x000001b0,
-	0x000001b2,
-	0x000001b4,
-	0x000001bd,
-	0x000001bf,
-	0x000001db,
-	0x000001df,
-	0x000001e3,
-	0x000001e7,
-	0x000001eb,
-	0x000001ef,
+	0x0000011d,
+	0x0000011f,
+	0x00000121,
+	0x00000123,
+	0x00000125,
+	0x0000012b,
+	0x0000012d,
+	0x00000142,
+	0x0000015b,
+	0x00000161,
+	0x00000169,
+	0x0000016b,
+	0x0000016d,
+	0x00000173,
+	0x0000017f,
+	0x000001ab,
+	0x000001b1,
+	0x000001c0,
+	0x000001c3,
+	0x000001c7,
+	0x000001cb,
+	0x000001cf,
+	0x000001d3,
+	0x000001ea,
+	0x000001f9,
+	0x000001fd,
+	0x00000203,
+	0x00000205,
+	0x00000209,
+	0x00000214,
+	0x00000216,
+	0x00000232,
+	0x00000236,
+	0x00000238,
+	0x0000023a,
+	0x0000023c,
+	0x0000023e,
+	0x00000240,
+	0x00000244,
+	0x00000248,
+	0x0000024a,
+	0x0000024e,
+	0x00000250,
+	0x00000254,
+	0x00000256,
 };
 
-unsigned long INSTRUCTIONS	= 0x00000104;
-unsigned long PATCHES	= 0x00000048;
+unsigned long INSTRUCTIONS	= 0x00000136;
+unsigned long PATCHES	= 0x00000056;
diff -u -r /usr/src/linux-1.2.13/drivers/scsi/53c8xx_u.h /usr/src/linux/drivers/scsi/53c8xx_u.h
--- /usr/src/linux-1.2.13/drivers/scsi/53c8xx_u.h	Wed Oct 25 13:23:28 1995
+++ /usr/src/linux/drivers/scsi/53c8xx_u.h	Fri Oct 27 17:29:02 1995
@@ -2,13 +2,12 @@
 #undef A_NCR53c7xx_msg_reject
 #undef A_NCR53c7xx_sink
 #undef A_NCR53c7xx_zero
+#undef A_addr_reconnect_dsa_head
 #undef A_addr_scratch
-#undef A_addr_sfbr
 #undef A_addr_temp
 #undef A_dmode_memory_to_memory
 #undef A_dmode_memory_to_ncr
 #undef A_dmode_ncr_to_memory
-#undef A_dmode_ncr_to_ncr
 #undef A_dsa_check_reselect
 #undef A_dsa_cmdout
 #undef A_dsa_cmnd
@@ -20,19 +19,20 @@
 #undef A_dsa_msgout
 #undef A_dsa_msgout_other
 #undef A_dsa_next
+#undef A_dsa_restore_pointers
+#undef A_dsa_save_data_pointer
 #undef A_dsa_select
 #undef A_dsa_status
-#undef A_dsa_temp_dsa_next
-#undef A_dsa_temp_jump_resume
+#undef A_dsa_temp_addr_next
+#undef A_dsa_temp_addr_residual
+#undef A_dsa_temp_addr_saved_pointer
+#undef A_dsa_temp_addr_saved_residual
 #undef A_dsa_temp_lun
+#undef A_dsa_temp_next
 #undef A_dsa_temp_sync
 #undef A_dsa_temp_target
 #undef A_int_debug_break
-#undef A_int_debug_dsa_loaded
-#undef A_int_debug_head
-#undef A_int_debug_idle
-#undef A_int_debug_reselected
-#undef A_int_debug_scheduled
+#undef A_int_debug_panic
 #undef A_int_err_check_condition
 #undef A_int_err_no_phase
 #undef A_int_err_selected
@@ -61,18 +61,26 @@
 #undef Ent_cmdout_cmdout
 #undef Ent_command_complete
 #undef Ent_command_complete_msgin
+#undef Ent_data_transfer
+#undef Ent_datain_to_jump
 #undef Ent_debug_break
 #undef Ent_dsa_code_check_reselect
+#undef Ent_dsa_code_restore_pointers
+#undef Ent_dsa_code_save_data_pointer
 #undef Ent_dsa_code_template
 #undef Ent_dsa_code_template_end
-#undef Ent_dsa_jump_resume
 #undef Ent_dsa_schedule
 #undef Ent_dsa_zero
+#undef Ent_end_data_transfer
 #undef Ent_initiator_abort
 #undef Ent_msg_in
+#undef Ent_msg_in_restart
+#undef Ent_other_in
+#undef Ent_other_out
 #undef Ent_other_transfer
 #undef Ent_reject_message
 #undef Ent_reselected_check_next
+#undef Ent_reselected_ok
 #undef Ent_respond_message
 #undef Ent_schedule
 #undef Ent_select
