]> git.neil.brown.name Git - history.git/commitdiff
Import 1.3.49 1.3.49
authorLinus Torvalds <torvalds@linuxfoundation.org>
Fri, 23 Nov 2007 20:10:26 +0000 (15:10 -0500)
committerLinus Torvalds <torvalds@linuxfoundation.org>
Fri, 23 Nov 2007 20:10:26 +0000 (15:10 -0500)
27 files changed:
CREDITS
Documentation/networking/z8530drv.txt
Makefile
arch/i386/kernel/setup.c
arch/i386/kernel/smp.c
drivers/char/README.scc
drivers/char/scc.c
drivers/char/scc_config.h [deleted file]
drivers/net/3c503.c
drivers/net/3c505.c
drivers/pci/pci.c
drivers/scsi/53c7,8xx.c
drivers/scsi/53c7,8xx.h
drivers/scsi/53c7,8xx.scr
drivers/scsi/53c8xx_d.h
drivers/scsi/53c8xx_u.h
drivers/scsi/aha152x.c
drivers/scsi/aha152x.h
drivers/scsi/aic7xxx.c
drivers/scsi/aic7xxx_proc.c
fs/buffer.c
fs/dquot.c
fs/nfs/nfsroot.c
include/linux/pci.h
include/linux/scc.h
kernel/ksyms.c
mm/memory.c

diff --git a/CREDITS b/CREDITS
index 4b6ae7063def4f70035aa7ae758c968f2c97d018..33cb25067653ff84b339302142e20c4dcba83eeb 100644 (file)
--- a/CREDITS
+++ b/CREDITS
@@ -483,6 +483,14 @@ S: Hoefbladhof 27
 S: 2215 DV Voorhout
 S: The Netherlands
 
+N: Karl Keyte
+E: kkeyte@koft.rhein-main.de
+E: kkeyte@esoc.esa.de
+D: Disk usage statistics and modifications to line printer driver
+S: Erbacher Strasse 6
+S: D-64283 Darmstadt
+S: Germany
+
 N: Olaf Kirch
 E: okir@monad.swb.de
 D: Author of the Linux Network Administrators' Guide
index 60ee0c454f5ecb776f89428d7257ea7365a20d15..016521ae913895dcc13776f8744dafbbff45e414 100644 (file)
@@ -1,58 +1,19 @@
-// 950913: note -- I will upload the new version 1.9a to ftp.ucsd.edu
-//         as soon as possible...
-//
-// ******
-// ****** The driver has a  n e w  MAJOR number (34) now! ******
-// ******
-//
-// please remake /dev/sc*:
-//
-// mknod /dev/scc0 c 34 0
-// mknod /dev/scc1 c 34 1
-// mknod /dev/scc2 c 34 2
-// mknod /dev/scc3 c 34 3
-//
-// (and so on...)
-//
-// If you want to use the old device naming scheme use:
-//
-// ln -f /dev/scc0 /dev/sc1
-// ln -f /dev/scc1 /dev/sc2
-// ln -f /dev/scc2 /dev/sc3
-// ln -f /dev/scc3 /dev/sc4
-//
-// (you get the idea...)
-//
-// -dl1bke-
-
-
 This is a subset of the documentation. To use this driver you MUST have the
 full package from:
 
 Internet:
 =========
 
-ftp.ucsd.edu:/hamradio/packet/tcpip/incoming/z8530drv-1.9.dl1bke.tar.gz
+ftp.ucsd.edu:/hamradio/packet/tcpip/incoming/z8530drv-2.0.dl1bke.real.tar.gz
 
 [
   if you can't find it there, try:
-  .../tcpip/linux/z8530drv-1.9.dl1bke.tar.gz
+  .../tcpip/linux/z8530drv-2.0.dl1bke.tar.gz
 
 ]
 
 and various mirrors (i.e. nic.switch.ch)
 
-AX.25 BBS
-=========
-
-UNIX @ DB0ACH.#NRW.DEU.EU, subject: Z8530D19.Pxx/Pyy
-
-(AX.25 call: DB0ACH-8)
-
-and various BBS that received the file through AUTO7P or 7PSERV
-with the filename Z8530D19.TGZ
-
-
 ---------------------------------------------------------------------------
 
 
@@ -60,179 +21,198 @@ with the filename Z8530D19.TGZ
 
    ********************************************************************
 
-        (c) 1994 by Joerg Reuter DL1BKE
+        (c) 1993,1995 by Joerg Reuter DL1BKE
 
-        portions (c) 1994 Hans Alblas PE1AYX
-        and      (c) 1993 Guido ten Dolle PE1NNZ
+        portions (c) 1993 Guido ten Dolle PE1NNZ
 
         for the complete copyright notice see >> Copying.Z8530DRV <<
 
    ******************************************************************** 
 
 
-0. Installation of the package
-==============================
-
-Run SCC-Install. If one (or more) of the patches fails PLEASE consult 
-chapter 2 (and READ IT of course!)
-
-
-
-1. Initialization and attachment of the channels
-================================================
+1. Initialization of the driver
+===============================
 
 To use the driver, 3 steps must be performed:
 
-     1. Global initialization of the driver in the kernel
-     2. Setup of parameters with sccinit
-     2. Attachment of each channel in the packet software
-
-The global initialization is needed to reset all SCCs and to 
-install a common interrupt handler.  Also, the hardware addresses 
-of the chips are defined in this step.  In the second step, each 
-channel is set up for the intended use.
-
-
-
-1.1. Initialization
-===================
-
-Initialization of the hardware is performed by setting the defines and
-variables in the file "/linux/drivers/char/scc_config.h". You can change 
-a number of parameters.
+     1. if compiled as module: loading the module
+     2. Setup of hardware, MODEM and KISS parameters with sccinit
+     3. Attachment of each channel in the packet software
 
 
+1.1 Loading the module
+======================
 
-################################################################################################
-# For OptoSCC card e.g:
-#
-
-int     Nchips       = 2        ; /* number of chips */
-io_port Vector_Latch = 0x168    ; /* addr. of INTACK-Latch (0 for poll mode)
-*/
-int     Ivec         = 9        ; /* interrupt vector */
-long    Clock        = 4915200  ; /* frequency of the scc clock */
-char    Pclk         = 1        ; /* use PCLK (1) or RTxC (0) */
-char    Board        = PA0HZP   ; /* what type of SCC card do you use? */
-int     Option       = 0        ; /* command for extra hardware */
-io_port Special_Port = 0        ; /* port address for special hardware */
-                                  /* (for EAGLE, PC100, PRIMUS, DRSI) */
-
-                        /*      ^  never remove the semicolon !! */
-
-
-/*                      Channel    A      B         Chip        */
-/*                               ============     ========      */
-/* Control ports:                                               */
-
-io_port SCC_ctrl[MAXSCC * 2] =  {0x152, 0x150,  /* ...one...    */
-                                 0x156, 0x154,  /* ...two...    */
-                                     0,     0,  /* ...three...  */
-                                     0,     0}; /* ...four...   */
-                       
+(If you're going to compile the driver as a part of the kernel image,
+ skip this chapter and continue with 1.2)
 
-/* Data ports:                                                 */
+Before you can use a module, you'll have to load it with
 
-io_port SCC_data[MAXSCC * 2] =  {0x153, 0x151, /* ...one...    */
-                                0x157, 0x155,  /* ...two...    */
-                                    0,     0,  /* ...three...  */
-                                    0,     0}; /* ...four...   */
+       insmod scc.o
 
+please read 'man insmod' that comes with modutils.
 
-/* set to '1' if you have and want ESCC chip (8580/85180/85280) support */
-
-/*                                           Chip      */
-/*                                         ========    */
-int SCC_Enhanced[MAXSCC] =     {0,     /* ...one...    */
-                                0,     /* ...two...    */
-                                0,     /* ...three...  */
-                                0};    /* ...four...   */
-
-/* some useful #defines. You might need them or not */
-
-#define VERBOSE_BOOTMSG 1
-#undef  SCC_DELAY              /* perhaps a 486DX2 is a *bit* too fast */
-#undef  SCC_LDELAY             /* slow it even a bit more down */
-#undef  DONT_CHECK             /* don't look if the SCCs you specified are available */
+You should include the insmod in one of the /etc/rc.d/rc.* files,
+and don't forget to insert a call of sccinit after that. It
+will read  your
 
 
-/*********** END OF CONFIGURATION PARAMETERS ********************************************/
+1.2. /etc/z8530drv.rc
+=====================
 
+To setup all parameters you must run /sbin/sccinit from one
+of your rc.*-files. This has to be done BEFORE the start of
+NET or axattach. Sccinit reads the file /etc/z8530drv.rc
+and sets the hardware, MODEM and KISS parameters. A sample file is
+delivered with this package. Change it to your needs.
 
+The file itself consists of two main sections.
 
+1.2.1 configuration of hardware parameters
+==========================================
 
-################################################################################################
-# For Baycom (U)SCC card e.g:
-#
+The hardware setup section defines the following parameters for each
+Z8530:
 
-int     Nchips      = 2        ; /* number of chips */
-io_port Vector_Latch = 0       ; /* addr. of INTACK-Latch (0 for poll mode) */
-int     Ivec        = 7        ; /* interrupt vector */
-long    Clock       = 4915200  ; /* frequency of the scc clock */
-char   Board        = BAYCOM   ; /* what type of SCC card do you use? */
-int    Option       = 0        ; /* command for extra hardware */
-io_port Special_Port = 0       ; /* port address for special hardware */
-                                 /* (for EAGLE, PC100, PRIMUS, DRSI) */
+chip    1
+data_a  0x300                   # data port A
+ctrl_a  0x304                   # control port A
+data_b  0x301                   # data port B
+ctrl_b  0x305                   # control port B
+irq     5                       # IRQ No. 5
+pclock  4915200                 # clock
+board   BAYCOM                  # hardware type
+escc    no                      # enhanced SCC chip? (8580/85180/85280)
+vector  0                       # latch for interrupt vector
+special no                      # address of special function register
+option  0                       # option to set via sfr
 
-                       /*      ^  never remove the semicolon !! */
-                       
 
+chip   - this is just a delimiter to make sccinit a bit simplier to
+         program. A parameter has no effect.
 
-/*                     Channel    A      B         Chip        */
-/*                              ============     ========      */
-/* Control ports:                                              */
+data_a  - the address of the data port A of this Z8530 (needed)
+ctrl_a  - the address of the control port A (needed)
+data_b  - the address of the data port B (needed)
+ctrl_b  - the address of the control port B (needed)
 
-io_port SCC_ctrl[MAXSCC * 2] =         {0x304, 0x305,  /* ...one...    */
-                                0x306, 0x307,  /* ...two...    */
-                                    0,     0,  /* ...three...  */
-                                    0,     0}; /* ...four...   */
+irq     - the used IRQ for this chip. Different chips can use different
+          IRQs or the same. If they share an interrupt, it needs to be
+         specified within one chip-definition only.
 
-/* Data ports:                                                 */
+pclock  - the clock at the PCLK pin of the Z8530 (option, 4915200 is
+          default), measured in Hertz
 
-io_port SCC_data[MAXSCC * 2] =  {0x300, 0x301, /* ...one...    */
-                                0x302, 0x303,  /* ...two...    */
-                                    0,     0,  /* ...three...  */
-                                    0,     0}; /* ...four...   */
+board   - the "type" of the board:
 
+          SCC type                 value
+          ---------------------------------
+          PA0HZP SCC card          PA0HZP
+          EAGLE card               EAGLE
+          PC100 card               PC100
+          PRIMUS-PC (DG9BL) card   PRIMUS
+          BayCom (U)SCC card       BAYCOM
 
-/* set to '1' if you have and want ESCC chip (8580/85180/85280) support */
+escc    - if you want support for ESCC chips (8580, 85180, 85280), set
+          this to "yes" (option, defaults to "no")
 
-/*                                           Chip      */
-/*                                         ========    */
-int SCC_Enhanced[MAXSCC] =     {0,     /* ...one...    */
-                                0,     /* ...two...    */
-                                0,     /* ...three...  */
-                                0};    /* ...four...   */
+vector  - address of the vector latch (aka "intack port") for PA0HZP
+          cards. There can be only one vector latch for all chips!
+         (option, defaults to 0)
 
-/* some useful #defines. You might need them or not */
+special - address of the special function register on several cards.
+          (option, defaults to 0)
 
-#define VERBOSE_BOOTMSG 1
-#undef  SCC_DELAY              /* perhaps a 486DX2 is a *bit* too fast */
-#undef  SCC_LDELAY             /* slow it even a bit more down */
-#undef  DONT_CHECK             /* don't look if the SCCs you specified are available */
+option  - The value you write into that register (option, default is 0)
 
-After you changed a parameter you have to recompile a new kernel image file.
+You can specify up to four chips (8 channels). If this is not enough,
+just change
 
-The channel number ranges from 0 to (2 * Nchips) - 1,
-where Nchips is the number of chips.
+       #define MAXSCC 4
 
-The crystal clock is specified as 4.9152 MHz.  Other frequencies 
-can be used, and this parameter should be adjusted accordingly.
+to a higher value.
 
+Example for the BayCom USCC:
+----------------------------
 
-You can define your scc type with Board
+chip    1
+data_a  0x300                   # data port A
+ctrl_a  0x304                   # control port A
+data_b  0x301                   # data port B
+ctrl_b  0x305                   # control port B
+irq     5                       # IRQ No. 5 (#)
+board   BAYCOM                  # hardware type (*)
+#
+# SCC chip 2
+#
+chip    2
+data_a  0x302
+ctrl_a  0x306
+data_b  0x303
+ctrl_b  0x307
+board   BAYCOM
+
+An example for a PA0HZP card:
+-----------------------------
+
+chip 1
+data_a 0x153
+data_b 0x151
+ctrl_a 0x152
+ctrl_b 0x150
+irq 9
+pclock 4915200
+board PA0HZP
+vector 0x168
+escc no
+#
+#
+#
+chip 2
+data_a 0x157
+data_b 0x155
+ctrl_a 0x156
+ctrl_b 0x154
+irq 9
+pclock 4915200
+board PA0HZP
+vector 0x168
+escc no
+
+A DRSI would should probably work with this:
+--------------------------------------------
+(actually: two DRSI cards...)
+
+chip 1
+data_a 0x303
+data_b 0x301
+ctrl_a 0x302
+ctrl_b 0x300
+irq 7
+pclock 4915200
+board DRSI
+escc no
+#
+#
+#
+chip 2
+data_a 0x313
+data_b 0x311
+ctrl_a 0x312
+ctrl_b 0x310
+irq 7
+pclock 4915200
+board DRSI
+escc no
 
-   SCC type                 value
-   ---------------------------------
-   PA0HZP SCC card          PA0HZP
-   EAGLE card               EAGLE
-   PC100 card               PC100
-   PRIMUS-PC (DG9BL) card   PRIMUS
-   BayCom (U)SCC card       BAYCOM
+Note that you cannot use the on-board baudrate generator off DRSI
+cards. Use "mode dpll" for clock source (see below).
 
+This is based on information provided by Mike Bilow (and verified
+by Paul Helay)
 
-NOTE:
-=====
+The utility "gencfg"
+--------------------
 
 If you only know the parameters for the PE1CHL driver for DOS,
 run gencfg. It will generate the correct port addresses (I hope).
@@ -242,8 +222,7 @@ not appear. Example:
 
 gencfg 2 0x150 4 2 0 1 0x168 9 4915200 
 
-will print a short form of scc_config.h for the OptoSCC to stdout. 
-("short" <=> few comments).
+will print a skeleton z8530drv.rc for the OptoSCC to stdout.
 
 gencfg 2 0x300 2 4 5 -4 0 7 4915200 0x10
 
@@ -251,24 +230,19 @@ does the same for the BayCom USCC card. I my opinion it is much easier
 to edit scc_config.h... 
 
 
-1.2 initializing the driver on bootup
-=====================================
+1.2.2 channel configuration
+===========================
 
+The channel definition is divided into three sub sections for each
+channel:
 
-To setup a number parameters you must run /sbin/sccinit from one
-of your rc.*-files. This has to be done BEFORE the start of
-NET or the ax25attach. Sccinit reads the file /etc/z8530drv.rc
-and sets the MODEM and KISS parameters. A sample file is
-delivered with this package. Change it to your needs:
-
-Each channel definition is divided into three sections. An
-example for /dev/scc0:
+An example for /dev/scc0:
 
 # DEVICE
 
-device /dev/scc0               # the device for the following params
+device /dev/scc0       # the device for the following params
 
-# MODEM
+# MODEM / BUFFERS
 
 speed 1200             # the default baudrate
 clock dpll             # clock source: 
@@ -279,6 +253,15 @@ clock dpll         # clock source:
 mode nrzi              # HDLC encoding mode
                        #       nrzi = 1k2 MODEM, G3RUH 9k6 MODEM
                        #       nrz  = DF9IC 9k6 MODEM
+                       #
+rxbuffers 8            # number of rx buffers allocated
+                       #               (option, default is 4)
+txbuffers 16           # number of tx buffers allocatd
+                       #               (option, default is 16)
+bufsize        384             # size of buffers. Note that this must include
+                       # the AX.25 header, not only the data field!
+                       # (optional, defaults to 384)
+
 # KISS (Layer 1)
 
 txdelay 36              # (see chapter 1.4)
@@ -311,8 +294,11 @@ Sccparam program or through KISS. Just to avoid securety holes...
     maxkey expires --- of course without sending anything (useful).
 
 
-1.3. Attach commands
-====================
+2. Attachment of a channel by your AX.25 software
+=================================================
+
+2.1 KA9Q NOS derivates
+======================
 
 When the linux has startup, the SCC driver has been initialized,
 you can attach the channels in your packet software. This is done
@@ -328,12 +314,12 @@ Example Wampes:
 # NOTE: Interfacename and the device must be the same!!
 # Usage: attach asy 0 0 slip|vjslip|ax25ui|ax25i|nrs|kissui <label> 0 <mtu> <speed> [ip_addr]
 #
-attach asy 0 0 kissi scc0 256 256 1200    # Attach SCC channel 1 in 1200 baud
-attach asy 0 0 kissi scc1 256 256 1200    # Attach SCC channel 2 in 1200 baud
+attach asy 0 0 kissi  scc0 256 256 1200   # Attach SCC channel 1 in 1200 baud
+attach asy 0 0 kissi  scc1 256 256 1200   # Attach SCC channel 2 in 1200 baud
 attach asy 0 0 kissui scc2 256 256 38400  # Attach SCC channel 3 in 38400 baud
 attach asy 0 0 kissui scc3 256 256 9600   # Attach SCC channel 4 in 9600 baud
-#                ^
-#                 for WAMPES 921229 use here: ax25
+#              ^^^^
+#              for WAMPES 921229 use here: ax25
 #
 
 Example JNOS:
@@ -341,10 +327,10 @@ Example JNOS:
 ############################################
 # JNOS device attach
 #
-#attach asy scc0 0 ax25 scc0 256 256 1200
-#attach asy scc1 0 ax25 scc1 256 256 1200
-#attach asy scc2 0 ax25 scc2 256 256 300
-#attach asy scc3 0 ax25 scc3 256 256 4800
+attach asy scc0 0 ax25 scc0 256 256 1200
+attach asy scc1 0 ax25 scc1 256 256 1200
+attach asy scc2 0 ax25 scc2 256 256 300
+attach asy scc3 0 ax25 scc3 256 256 4800
 #
 #
 
@@ -363,10 +349,22 @@ Such a divider is not necessary for normal CSMA packet radio
 operation, but interrupt overhead is slightly reduced if you 
 still install it.  
 
+2.2 Kernel AX.25
+================
 
+Well, as said before: The driver emulates a KISS TNC, so you
+can simply run
+
+       axattach -s 1200 /dev/scc0 DL1BKE
+
+to establish the link between kernel AX.25 and z8530drv.
 
-1.4. Displaying SCC Parameters:
-===============================
+
+3. Adjustment and Display of parameters
+=======================================
+
+3.1 Displaying SCC Parameters:
+==============================
 
 Once a SCC channel has been attached, the parameter settings and 
 some statistic information can be shown using the param program:
@@ -397,15 +395,15 @@ HDLC                  Z8530           Interrupts         Queues
 -----------------------------------------------------------------------
 Sent       :     273  RxOver :     0  RxInts :   125074  RxQueue :    0
 Received   :    1095  TxUnder:     0  TxInts :     4684  TxQueue :    0
-RxErrors   :    1591                  ExInts :    11776
-KissErrors :       0                  SpInts :     1503  NoSpace :    0
+RxErrors   :    1591                  ExInts :    11776  NoSpace :    0
+KissErrors :       0                  SpInts :     1503
 Tx State   :    idle
 
 Memory allocated:
 
-Total  :    1
-RxAlloc:    0
-TxAlloc:    1
+Buffer size:     384
+rx buffers :       4
+tx buffers :       8
 
 
 The status info shown is:
@@ -432,7 +430,7 @@ power of you computer. If "Space" errors occur, specify a higher
 number of buffers in the "scc.h" file.
 
 
-1.5 Setting Parameters
+3.2 Setting Parameters
 ======================
 
 
@@ -630,15 +628,9 @@ slip:
 
 
 
-2. Problems
+4. Problems 
 ===========
 
-
-[..]
-
-2.5 Other problems
-==================
-
 If you have tx-problems with your BayCom USCC card please check
 the manufacturer of the 8530. SGS chips have a slightly
 different timing. Try Zilog... I have no information if this
index 79a7e0f4d54bb6930e76c22ebfbd660af2e9d480..d718ed4939053e28d888fd8c02bccdbf69fd2a1a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 VERSION = 1
 PATCHLEVEL = 3
-SUBLEVEL = 48
+SUBLEVEL = 49
 
 ARCH = i386
 
index a6d63cdcf3f4f6bc7a2099f1425694717fd65c8e..27da8a53a21f0f79f28b1a16aef3b99c7a58e848 100644 (file)
@@ -175,7 +175,7 @@ void setup_arch(char **cmdline_p,
 static const char * i486model(unsigned int nr)
 {
        static const char *model[] = {
-               "0", "DX","SX","DX/2","4","SX/2","6","DX/2-WB","DX/4"
+               "0", "DX","SX","DX/2","4","SX/2","6","DX/2-WB","DX/4","DX/4-WB"
        };
        if (nr < sizeof(model)/sizeof(char *))
                return model[nr];
index 2c5f9d272f26acb6e16e44f16f379aea31171454..3289e9f27ae17d45ea2daf43064068ca29acebaf 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/delay.h>
 #include <asm/i82489.h>
 #include <linux/smp.h>
+#include <asm/pgtable.h>
 #include <asm/bitops.h>
 #include <asm/smp.h>
 
index 3d2db4ab35420b96b6643810e96a0b1124c9bb08..71b0c5e4148c985ac5bbabce47a99fcbb94a12af 100644 (file)
@@ -1,30 +1,3 @@
-// 950913: note -- I will upload the new version 1.9a to ftp.ucsd.edu
-//         as soon as possible... 
-//
-// ******
-// ****** The driver has a  n e w  MAJOR number (34) now! ******
-// ******
-//
-// please remake /dev/sc*:
-//
-// mknod /dev/scc0 c 34 0
-// mknod /dev/scc1 c 34 1
-// mknod /dev/scc2 c 34 2
-// mknod /dev/scc3 c 34 3
-//
-// (and so on...)
-//
-// If you want to use the old device naming scheme use:
-//
-// ln -f /dev/scc0 /dev/sc1
-// ln -f /dev/scc1 /dev/sc2
-// ln -f /dev/scc2 /dev/sc3
-// ln -f /dev/scc3 /dev/sc4
-//
-// (you get the idea...)
-//
-// -dl1bke-
-
 
 You will find subset of the documentation in
 
@@ -33,11 +6,11 @@ You will find subset of the documentation in
 
 To use this driver you MUST have the full package from
 
-ftp.ucsd.edu:/hamradio/packet/tcpip/incoming/z8530drv-1.9a.dl1bke.tar.gz
+ftp.ucsd.edu:/hamradio/packet/tcpip/incoming/z8530drv-2.01.dl1bke.real.tar.gz
 
 [
   if you can't find it there, try:
-  .../tcpip/linux/z8530drv-1.9a.dl1bke.tar.gz
+  .../tcpip/linux/z8530drv-2.01.dl1bke.tar.gz
 
 ]
 
index c92318f59d3a1a58732d9225028e4591d6ddb1d9..6981c45664fbdd1a487d67e1b01cec2ead7bc215 100644 (file)
@@ -1,6 +1,6 @@
-#define RCS_ID "$Id: scc.c,v 1.26 1995/09/07 14:46:19 jreuter Exp jreuter $"
+#define RCS_ID "$Id: scc.c,v 1.41 1995/12/17 22:36:40 jreuter Exp jreuter $"
 
-#define BANNER "Z8530 SCC driver v1.9.dl1bke (beta) by dl1bke\n"
+#define BANNER "Z8530 SCC driver version 2.01.dl1bke (alpha) by DL1BKE\n"
 
 /*
 
 
    ********************************************************************
 
-       (c) 1993 - 1995 by Joerg Reuter DL1BKE
+       Copyright (c) 1993, 1995 Joerg Reuter DL1BKE
 
-       portions (c) 1994 Hans Alblas PE1AYX 
-       and      (c) 1993 Guido ten Dolle PE1NNZ
+       portions (c) 1993 Guido ten Dolle PE1NNZ
 
    ********************************************************************
    
-   The driver and the programs in this archive are UNDER CONSTRUCTION.
+   The driver and the programs in the archive are UNDER CONSTRUCTION.
    The code is likely to fail, and so your kernel could --- even 
    a whole network. 
 
@@ -60,8 +59,8 @@
                Internet: jreuter@lykos.tng.oche.de
                
                
-   History of z8530drv:
-   --------------------
+   Incomplete history of z8530drv:
+   -------------------------------
 
    940913      - started to write the driver, rescued most of my own
                  code (and Hans Alblas' memory buffer pool concept) from 
 
    950131      - changed copyright notice to GPL without limitations.
    
-   950228      - (hopefully) fixed the reason for kernel panics in
-                  chk_rcv_queue() [stupid error]
-                  
-   950304       - fixed underrun/zcount handling
+     .
+     .
+     .
+      
+   950922      - using kernel timer chain
    
-   950305      - the driver registers port addresses now
-   
-   950314      - fixed underrun interrupt handling again
+   951002      - splitting timer routine, adding support for throttle/
+                 unthrottle calls, removed typo in kiss decoder,
+                 corrected termios settings.
+                 
+   951011      - at last found one (the?) problem which caused the 
+                 driver to mess up buffers on heavy load pe1ayx was
+                 complaining about. Quite simple to reproduce:
+                 set a 9k6 port into audio loopback, add a route
+                 to 44.99.99.99 on that route and do a 
+                 ping -f 44.99.99.99
+                 
+   951013      - it still does not survive a flood ping...
    
-   950512      - (hope to have) fixed hidden re-entrance problem
-                 in scc_timer()
+   951107      - axattach w/o "-s" option (or setting an invalid 
+                 baudrate) does not produce "division by zero" faults 
+                 anymore.
+                 
+   951114      - rewrote memory management, took first steps to allow
+                 compilation as a module. Well, it looks like a whole
+                 new driver now. BTW: It  d o e s  survive the flood
+                 ping at last...
+                 
+   951116      - scc_config.h is gone -- the driver will be initialized
+                 through ioctl-commands. Use sccinit.c for this purpose.
                  
-   950824      - received frames will be sent to the application
-                 faster, clean-up of z8530_init()
+   951117      - Well --- what should I say: You can compile it as a
+                 module now. And I solved the problem with slip.c...
+                 
+   951120      - most ioctl() routines may be called w/o suser()
+                 permissions, check if you've set the permissions of
+                 /dev/scc* right! NOT 0666, you know it's evil ;-)
+                 
+   951217      - found silly bug in init_channel(), some bugfixes
+                 for the "standalone" module
+
 
    Thanks to:
    ----------
    PE1CHL Rob  - for a lot of good ideas from his SCC driver for DOS
    PE1NNZ Guido - for his port of the original driver to Linux
    KA9Q   Phil  - from whom we stole the mbuf-structure
-   PA3AYX Hans  - who rewrote parts of the memory management and some 
-                 minor, but nevertheless useful changes
+   PA3AYX Hans  - for some useful changes
    DL8MBT Flori - for support
    DG0FT  Rene  - for the BayCom USCC support
    PA3AOU Harry - for ESCC testing, information supply and support
    
-   PE1KOX Rob, DG1RTF Thomas, ON5QK Roland, 
+   PE1KOX Rob, DG1RTF Thomas, ON5QK Roland, G4XYW Andy, Linus,
+   EI9GL Paul,
    
    and all who sent me bug reports and ideas... 
    
    NB -- if you find errors, change something, please let me know
         first before you distribute it... And please don't touch
         the version number. Just replace my callsign in
-        "v1.9.dl1bke" with your own. Just to avoid confusion...
+        "v2.01.dl1bke" with your own. Just to avoid confusion...
         
    If you want to add your modification to the linux distribution
    please (!) contact me first.
   
 */
 
+/* ----------------------------------------------------------------------- */
+
+#define DEBUG_BUFFERS  /* keep defined unless it is really stable... */
+
+#undef  SCC_DELAY      /* perhaps a 486DX2 is a *bit* too fast */
+#undef  SCC_LDELAY 5   /* slow it even a bit more down */
+#undef  DONT_CHECK     /* don't look if the SCCs you specified are available */
+
+#define MAXSCC          4       /* number of max. supported chips */
+#define RXBUFFERS       8       /* default number of RX buffers */
+#define TXBUFFERS       8       /* default number of TX buffers */
+#define BUFSIZE         384     /* must not exceed 4096-sizeof(mbuf) */
+#define TPS             25      /* scc_tx_timer():  Ticks Per Second */
+
+#define DEFAULT_CLOCK  4915200 /* default pclock if nothing is specified */
+
+/* ----------------------------------------------------------------------- */
+
+#include <linux/module.h>
 #include <linux/errno.h>
 #include <linux/signal.h>
 #include <linux/sched.h>
 #include <linux/fcntl.h>
 #include <linux/ptrace.h>
 #include <linux/malloc.h>
-#include <linux/scc.h>
 #include <linux/delay.h>
-#include "scc_config.h"
+#include <linux/scc.h>
 
 #include <asm/system.h>
 #include <asm/io.h>
 #include <time.h>
 #include <linux/kernel.h>
 
+#ifdef MODULE
+int init_module(void);
+void cleanup_module(void);
+#endif
+
 #ifndef Z8530_MAJOR
 #define Z8530_MAJOR 34
 #endif
 
 int scc_init(void);
 
+static struct mbuf * scc_enqueue_buffer(struct mbuf **queue, struct mbuf * buffer);
+static struct mbuf * scc_dequeue_buffer(struct mbuf **queue);
+static void alloc_buffer_pool(struct scc_channel *scc);
+static void free_buffer_pool(struct scc_channel *scc);
+static struct mbuf * scc_get_buffer(struct scc_channel *scc, char type);
+
 int scc_open(struct tty_struct *tty, struct file *filp);
 static void scc_close(struct tty_struct *tty, struct file *filp);
 int scc_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
@@ -166,6 +221,7 @@ static int scc_chars_in_buffer(struct tty_struct *tty);
 static void scc_flush_buffer(struct tty_struct *tty);
 static int scc_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
 static void scc_set_termios(struct tty_struct *tty, struct termios *old_termios);
+static void scc_set_ldisc(struct tty_struct *tty);
 static void scc_throttle(struct tty_struct *tty);
 static void scc_unthrottle(struct tty_struct *tty);
 static void scc_start(struct tty_struct *tty);
@@ -183,9 +239,9 @@ static void scc_exint(register struct scc_channel *scc);
 static void scc_rxint(register struct scc_channel *scc);
 static void scc_spint(register struct scc_channel *scc);
 static void scc_isr(int irq, struct pt_regs *regs);
-static void scc_timer(void);
+static void scc_tx_timer(unsigned long);
+static void scc_rx_timer(unsigned long);
 static void scc_init_timer(struct scc_channel *scc);
-static void scc_rx_timer(void);
 
 /* from serial.c */
 
@@ -199,15 +255,21 @@ static int scc_refcount;
 static struct tty_struct *scc_table[2*MAXSCC];
 static struct termios scc_termios[2 * MAXSCC];
 static struct termios scc_termios_locked[2 * MAXSCC];
+
+struct irqflags { unsigned char used : 1; } Ivec[16];
        
 struct scc_channel SCC_Info[2 * MAXSCC];         /* information per channel */
+io_port SCC_ctrl[2 * MAXSCC];                   /* Control ports */
 
 unsigned char Random = 0;              /* random number for p-persist */
 unsigned char Driver_Initialized = 0;
-static struct sccbuf *sccfreelist[MAX_IBUFS] = {0};
-static int allocated_ibufs = 0;
+int Nchips = 0;
+io_port Vector_Latch = 0;
 
-static struct rx_timer_CB rx_timer_cb;
+struct semaphore scc_sem = MUTEX;
+unsigned char scc_wbuf[BUFSIZE];
+
+static struct termios scc_std_termios;
 
 /* ******************************************************************** */
 /* *                   Port Access Functions                         * */
@@ -219,9 +281,9 @@ InReg(register io_port port, register unsigned char reg)
 #ifdef SCC_LDELAY
        register unsigned char r;
        Outb(port, reg);
-       udelay(5);
+       udelay(SCC_LDELAY);
        r=Inb(port);
-       udelay(5);
+       udelay(SCC_LDELAY);
        return r;
 #else
        Outb(port, reg);
@@ -233,8 +295,8 @@ static inline void
 OutReg(register io_port port, register unsigned char reg, register unsigned char val)
 {
 #ifdef SCC_LDELAY
-       Outb(port, reg); udelay(5);
-       Outb(port, val); udelay(5);
+       Outb(port, reg); udelay(SCC_LDELAY);
+       Outb(port, val); udelay(SCC_LDELAY);
 #else
        Outb(port, reg);
        Outb(port, val);
@@ -263,243 +325,379 @@ cl(register struct scc_channel *scc, register unsigned char reg, register unsign
 /* *                   Memory Buffer Management                        */
 /* ******************************************************************** */
 
-/* mbuf concept lent from KA9Q. Tnx PE1AYX for the buffer pool concept */
-/* (sorry, do you have any better ideas?) */
+/*
+ * The new buffer scheme uses a ring chain of buffers. This has the
+ * advantage to access both, first and last element of the list, very
+ * fast. It has the disadvantage to mess things up double if something
+ * is wrong.
+ *
+ * Every scc channel has its own buffer pool now with an adjustable
+ * number of buffers for transmit and receive buffers. The buffer
+ * size remains the same for each AX.25 frame, but is (as a semi-
+ * undocumented feature) adjustable within a range of 512 and 4096
+ * to benefit experiments with higher frame lengths. If you need
+ * larger frames... Well, you'd better choose a whole new concept
+ * for that, like a DMA capable I/O card and a block device driver...
+ *
+ */
 
+/* ------ Management of buffer queues ------ */
 
-/* allocate memory for the interrupt buffer pool */
 
-void scc_alloc_buffer_pool(void)
-{
-       int i;
-       struct sccbuf *sccb;
-       struct mbuf   *bp;
-       
-       for (i = 0 ; i < MAX_IBUFS ; i++)
-       {
-               sccb = (struct sccbuf *)kmalloc(sizeof(struct sccbuf), GFP_ATOMIC);
-               bp = (struct mbuf *)kmalloc(sizeof(struct mbuf), GFP_ATOMIC);
-               
-               if ( !(sccb && bp) )
-               {
-                       allocated_ibufs = --i;
-                       
-                       if (allocated_ibufs < 0)
-                               panic("scc_alloc_buffer_pool() - can't get buffer space");
-                       else
-                               printk("Warning: scc_alloc_buffer_pool() - allocated only %i buffers\n",i);
-                               
-                       return;
-               }
-               
-               sccfreelist[i] = sccb;
-               sccfreelist[i]->bp = bp;
-               memset(sccfreelist[i]->bp ,0,sizeof(struct mbuf));
-               sccfreelist[i]->inuse = 0;
-               sccfreelist[i]->bp->type = 0;
-               sccfreelist[i]->bp->refcnt = 0;
-               sccfreelist[i]->bp->size = BUFSIZE;
-       }
-       allocated_ibufs = MAX_IBUFS;
-}
+/* Insert a buffer at the "end" of the chain */
 
-unsigned int scc_count_used_buffers(unsigned int * rx, unsigned int * tx)
+static struct mbuf * 
+scc_enqueue_buffer(struct mbuf **queue, struct mbuf * buffer)
 {
-       unsigned int i, used = 0;
+       unsigned long flags;
+       struct mbuf * anchor;
        
-       if (rx) *rx = 0;
-       if (tx) *tx = 0;
+       save_flags(flags); cli();               /* do not disturb! */
+
+#ifdef DEBUG_BUFFERS                   
+       if (queue == NULLBUFP)                  /* called with illegal parameters, notify the user */
+
+       {
+               printk("z8530drv: Pointer to queue anchor is NULL pointer [enq]\n");
+               restore_flags(flags);
+               return NULLBUF;
+       }
        
-       for (i = 0 ; i < allocated_ibufs ; i++)
+       if (buffer == NULLBUF)
        {
-               if (sccfreelist[i]->inuse)
-               {
-                       switch (sccfreelist[i]->bp->type)
-                       {
-                               case BT_RECEIVE:
-                                       if (rx) (*rx)++; break;
-                               case BT_TRANSMIT:
-                                       if (tx) (*tx)++; break;
-                       }
-                       
-                       used++;
-               }
+               printk("z8530drv: can't enqueue a NULL pointer\n");
+               restore_flags(flags);
+               return NULLBUF;
+       }
+#endif
+
+       anchor = *queue;                        /* the anchor is the "start" of the chain */
+
+       if (anchor == NULLBUF)                  /* found an empty list */
+       {
+               *queue = buffer;                /* new anchor */
+               buffer->next = buffer->prev = NULLBUF;
+       } else  
+       if (anchor->prev == NULLBUF)            /* list has one member only */
+       {
+#ifdef DEBUG_BUFFERS
+               if (anchor->prev != NULLBUF)    /* oops?! */
+                       printk("weird --- anchor->prev is NULL but not anchor->next [enq]\n");
+#endif                 
+               anchor->next = anchor->prev = buffer;
+               buffer->next = buffer->prev = anchor;
+       } else
+#ifdef DEBUG_BUFFERS   
+       if (anchor->next == NULLBUF)            /* this has to be an error. Okay, make the best out of it */
+       {
+               printk("z8530drv: weird --- anchor->next is NULL but not anchor->prev [enq]\n");
+               anchor->next = anchor->prev = buffer;
+               buffer->next = buffer->prev = anchor;
+       } else
+#endif
+       {                                       /* every other case */
+               buffer->prev = anchor->prev;    /* self explaining, isn't it? */
+               buffer->next = anchor;
+               anchor->prev->next = buffer;
+               anchor->prev = buffer;
        }
        
-       return used;
+       restore_flags(flags);
+       return *queue;                          /* return "start" of chain */
 }
 
 
-/* Allocate mbuf */
-struct mbuf *
-scc_get_buffer(char type)
+
+/* Remove a buffer from the "start" of the chain an return it */
+
+static struct mbuf * 
+scc_dequeue_buffer(struct mbuf **queue)
 {
-       int i;
        unsigned long flags;
+       struct mbuf *buffer;
+       
+       save_flags(flags); cli();
+
+#ifdef DEBUG_BUFFERS                   
+       if (queue == NULLBUFP)                  /* called with illegal parameter */
 
-       save_flags(flags); cli();       /* just to be sure */
+       {
+               printk("z8530drv: Pointer to queue anchor is NULL pointer [deq]\n");
+               restore_flags(flags);
+               return NULLBUF;
+       }
+#endif
 
-       for (i = 0 ; i < allocated_ibufs ; i++)
+       buffer = *queue;                        /* head of the chain */
+       
+       if (buffer != NULLBUF)                  /* not an empty list? */
        {
-               if(sccfreelist[i]->inuse == 0)
+               if (buffer->prev != NULLBUF)    /* not last buffer? */
                {
-                       sccfreelist[i]->inuse = 1;
-                       sccfreelist[i]->bp->type = type;
-                       sccfreelist[i]->bp->next = NULLBUF;
-                       sccfreelist[i]->bp->anext = NULLBUF;
-                       sccfreelist[i]->bp->dup = NULLBUF;
-                       sccfreelist[i]->bp->size = BUFSIZE;
-                       sccfreelist[i]->bp->refcnt = 1;
-                       sccfreelist[i]->bp->cnt = 0;
-                       sccfreelist[i]->bp->in_use = 0;
+#ifdef DEBUG_BUFFERS
+                       if (buffer->next == NULLBUF)
+                       {                       /* what?! */
+                               printk("z8530drv: weird --- buffer->next is NULL but not buffer->prev [deq]\n");
+                       } else
+#endif
+                       if (buffer->prev->next == buffer->prev->prev)
+                       {                       /* only one buffer remaining... */
+                               buffer->next->prev = NULLBUF;
+                               buffer->next->next = NULLBUF;
+                       } else {                /* the one remaining situation... */
+                               buffer->next->prev = buffer->prev;
+                               buffer->prev->next = buffer->next;
+                       }
+               } 
+#ifdef DEBUG_BUFFERS
+               else if (buffer->next != NULLBUF)
+                       printk("z8530drv: weird --- buffer->prev is NULL but not buffer->next [deq]\n");
+#endif
+               *queue = buffer->next;          /* new head of chain */
                
-                       restore_flags(flags);
-                       return sccfreelist[i]->bp;
-               }
-       }
+               buffer->next = NULLBUF;         /* for securety only... */
+               buffer->prev = NULLBUF;
+               buffer->rw_ptr = buffer->data;
+       } 
        
-       printk("\nSCC scc_get_buffer(): buffer pool empty\n");  /* should never happen */
        restore_flags(flags);
-       return NULLBUF;
+       return buffer;                          /* give it away... */
 }
 
+/* ------ buffer pool management ------ */
 
-/* Decrement the reference pointer in an mbuf. If it goes to zero,
- * free all resources associated with mbuf.
- * Return pointer to next mbuf in packet chain
- */
-struct mbuf *
-scc_return_buffer(register struct mbuf *bp, char type)
+
+/* allocate buffer pool        for a channel */
+
+static void 
+alloc_buffer_pool(struct scc_channel *scc)
 {
-       struct mbuf *bpnext;
-       int i;
-       unsigned long flags;
+       int k;
+       struct mbuf * bptr;
+       int  buflen;
        
-       if(!bp)
-               return NULLBUF;
-               
-       save_flags(flags); cli();
-       bpnext = bp->next;
+       buflen = sizeof(struct mbuf) + scc->stat.bufsize;
+       
+       if (scc->stat.bufsize <  336) scc->stat.bufsize = 336;
+       if (buflen > 4096)
+       {
+               scc->stat.bufsize = 4096-sizeof(struct mbuf);
+               buflen = 4096;
+       }
+       
+       if (scc->stat.rxbuffers < 4)  scc->stat.rxbuffers = 4;
+       if (scc->stat.txbuffers < 4)  scc->stat.txbuffers = 4;
        
-       if (bp->dup)
+
+       
+       /* allocate receive buffers for this channel */
+       
+       for (k = 0; k < scc->stat.rxbuffers ; k++)
        {
-               for(i = 0 ; i < allocated_ibufs ; i++)
+               /* allocate memory for the struct and the buffer */
+               
+               bptr = (struct mbuf *) kmalloc(buflen, GFP_ATOMIC);
+               
+               /* should not happen, but who knows? */
+               
+               if (bptr == NULLBUF)
                {
-                       if(sccfreelist[i]->bp == bp->dup)
-                       {
-                             if (sccfreelist[i]->bp->type != type)
-                             {
-                                  printk("scc_return_buffer(bp->dup, %i): wrong buffer type %i",
-                                         type,sccfreelist[i]->bp->type);
-                             }
-                             
-                             sccfreelist[i]->bp->cnt = 0;
-                             sccfreelist[i]->bp->refcnt = 0;
-                             sccfreelist[i]->bp->in_use = 0;
-                             sccfreelist[i]->inuse = 0;
-                             bp->dup = NULLBUF;
-                       }
+                       printk("z8530drv: %s: can't allocate memory for rx buffer pool", kdevname(scc->tty->device));
+                       break;
                }
+               
+               /* clear memory */
+               
+               memset(bptr, 0, buflen);
+               
+               /* initialize structure */
+               
+               bptr->rw_ptr = bptr->data;
+               
+               /* and append the buffer to the pool */
+               
+               scc_enqueue_buffer(&scc->rx_buffer_pool, bptr);
        }
        
-       /* Decrement reference count. If it has gone to zero, free it. */
-       if(--bp->refcnt <= 0)
+       /* now do the same for the transmit buffers */
+       
+       for (k = 0; k < scc->stat.txbuffers ; k++)
        {
-               for(i = 0 ; i < allocated_ibufs ; i++)
+               bptr = (struct mbuf *) kmalloc(buflen, GFP_ATOMIC);
+               
+               if (bptr == NULLBUF)
                {
-                       if(sccfreelist[i]->bp == bp)
-                       {
-                               if (sccfreelist[i]->bp->type != type)
-                               {
-                                       printk("scc_return_buffer(bp, %i): wrong buffer type %i",
-                                               type,sccfreelist[i]->bp->type);
-                               }                               
-                            
-                               sccfreelist[i]->bp->cnt = 0;
-                               sccfreelist[i]->bp->refcnt = 0;
-                               sccfreelist[i]->inuse = 0;
-                               restore_flags(flags);
-                               return bpnext;
-                       }
+                       printk("z8530drv: %s: can't allocate memory for tx buffer pool", kdevname(scc->tty->device));
+                       break;
                }
-       }
                
-       printk("\nscc_return_buffer(): bogus pointer %p\n",bp);
-       restore_flags(flags);
-       return bpnext;
-} 
-
-
-/* Free packet (a chain of mbufs). Return pointer to next packet on queue,
- * if any
- */
-struct mbuf *
-scc_free_chain(register struct mbuf *bp, char type)
-{
-       register struct mbuf *abp;
-       unsigned long flags;
-
-       if(!bp) 
-               return NULLBUF;
+               memset(bptr, 0, buflen);
                
-       save_flags(flags); cli();       
-       
-       abp = bp->anext;
-       while (bp) bp = scc_return_buffer(bp, type);
+               bptr->rw_ptr = bptr->data;
                
-       restore_flags(flags);
-       return abp;
+               scc_enqueue_buffer(&scc->tx_buffer_pool, bptr);
+       }
 }
 
 
-/* Append mbuf to end of mbuf chain */
-void
-scc_append_to_chain(struct mbuf **bph,struct mbuf *bp)
+/* remove buffer pool */
+
+static void 
+free_buffer_pool(struct scc_channel *scc)
 {
-       register struct mbuf *p;
+       struct mbuf * bptr;
        unsigned long flags;
-
-       if(bph == NULLBUFP || bp == NULLBUF)
-               return;
+       int cnt;
        
+       /* this one is a bit tricky and probably dangerous. */
+               
        save_flags(flags); cli();
        
-       if(*bph == NULLBUF)
+       /* esp. to free the buffers currently in use by ISR */
+       
+       bptr = scc->rx_bp;
+       if (bptr != NULLBUF)
        {
-               /* First one on chain */
-               *bph = bp;
-       } else {
-               for(p = *bph ; p->next != NULLBUF ; p = p->next)
-                       ;
-               p->next = bp;
+               scc->rx_bp = NULLBUF;
+               scc_enqueue_buffer(&scc->rx_buffer_pool, bptr);
+       }
+
+       bptr = scc->tx_bp;      
+       if (bptr != NULLBUF)
+       {
+               scc->tx_bp = NULLBUF;
+               scc_enqueue_buffer(&scc->tx_buffer_pool, bptr);
+       }
+       
+       bptr = scc->kiss_decode_bp;
+       if (bptr != NULLBUF)
+       {
+               scc->kiss_decode_bp = NULLBUF;
+               scc_enqueue_buffer(&scc->tx_buffer_pool, bptr);
+       }
+       
+       bptr = scc->kiss_encode_bp;
+       if (bptr != NULLBUF)
+       {
+               scc->kiss_encode_bp = NULLBUF;
+               scc_enqueue_buffer(&scc->rx_buffer_pool, bptr);
        }
        
        restore_flags(flags);
+       
+       
+       while (scc->rx_queue != NULLBUF)
+       {
+               bptr = scc_dequeue_buffer(&scc->rx_queue);
+               scc_enqueue_buffer(&scc->rx_buffer_pool, bptr);
+       }
+       
+       while (scc->tx_queue != NULLBUF)
+       {
+               bptr = scc_dequeue_buffer(&scc->tx_queue);
+               scc_enqueue_buffer(&scc->tx_buffer_pool, bptr);
+       }
+       
+       /* you want to know why we move every buffer back to the
+          buffer pool? Well, good question... ;-) 
+        */
+       
+       cnt = 0;
+       
+       /* Probably because we just want a central position in the
+          code were we actually call free()?
+        */
+       
+       while (scc->rx_buffer_pool != NULLBUF)
+       {
+               bptr = scc_dequeue_buffer(&scc->rx_buffer_pool);
+               
+               if (bptr != NULLBUF)
+               {
+                       cnt++;
+                       kfree(bptr);
+               }
+       }
+
+       if (cnt < scc->stat.rxbuffers)          /* hmm... hmm... :-( */
+               printk("z8530drv: oops, deallocated only %d of %d rx buffers\n", cnt, scc->stat.rxbuffers);
+       if (cnt > scc->stat.rxbuffers)          /* WHAT?!! */
+               printk("z8530drv: oops, deallocated %d instead of %d rx buffers. Very strange.\n", cnt, scc->stat.rxbuffers);
+               
+       cnt = 0;
+       
+       while (scc->tx_buffer_pool != NULLBUF)
+       {
+               bptr = scc_dequeue_buffer(&scc->tx_buffer_pool);
+               
+               if (bptr != NULLBUF)
+               {
+                       cnt++;
+                       kfree(bptr);
+               }
+       }
+
+       if (cnt < scc->stat.txbuffers)
+               printk("z8530drv: oops, deallocated only %d of %d tx buffers\n", cnt, scc->stat.txbuffers);
+       if (cnt > scc->stat.txbuffers)
+               printk("z8530drv: oops, deallocated %d instead of %d tx buffers. Very strange.\n", cnt, scc->stat.txbuffers);
 }
+               
+               
+/* ------ rx/tx buffer management ------ */
 
+/* 
+   get a fresh buffer from the pool if possible; if not: get one from
+   the queue. We will remove the oldest frame from the queue and hope
+   it was a good idea... ;-)
+   
+ */
 
-/* Append packet (chain of mbufs) to end of packet queue */
-void
-scc_enqueue(struct mbuf **queue,struct mbuf *bp)
+static struct mbuf * 
+scc_get_buffer(struct scc_channel *scc, char type)
 {
-       register struct mbuf *p;
-       unsigned long flags;
-
-       if(queue == NULLBUFP || bp == NULLBUF)
-               return;
-               
-       save_flags(flags); cli();
+       struct mbuf * bptr;
        
-       if(*queue == NULLBUF)
+       if (type == BT_TRANSMIT)
        {
-               /* List is empty, stick at front */
-               *queue = bp;
+               bptr = scc_dequeue_buffer(&scc->tx_buffer_pool);
+               
+               /* no free buffers in the pool anymore? */
+               
+               if (bptr == NULLBUF)
+               {
+                       printk("z8530drv: scc_get_buffer(%s): tx buffer pool empty\n", kdevname(scc->tty->device));
+                       
+                       /* use the oldest from the queue instead */
+                       
+                       bptr = scc_dequeue_buffer(&scc->tx_queue);
+                       
+                       /* this should never, ever happen... */
+                       
+                       if (bptr == NULLBUF)
+                               printk("z8530drv: scc_get_buffer(): panic - even no buffer found in tx queue\n");
+               }
        } else {
-               for(p = *queue ; p->anext != NULLBUF ; p = p->anext)
-                       ;
-               p->anext = bp;
+               bptr = scc_dequeue_buffer(&scc->rx_buffer_pool);
+               
+               if (bptr == NULLBUF)
+               {
+                       printk("z8530drv: scc_get_buffer(%s): rx buffer pool empty\n", kdevname(scc->tty->device));
+                       
+                       bptr = scc_dequeue_buffer(&scc->rx_queue);
+                       
+                       if (bptr == NULLBUF)
+                               printk("z8530drv: scc_get_buffer(): panic - even no buffer found in rx queue\n");
+               }       
        }
-       restore_flags(flags);
+       
+       if (bptr != NULLBUF)
+       {
+               bptr->rw_ptr = bptr->data;
+               bptr->cnt = 0;
+       }
+
+       return bptr;
 }
 
 
@@ -625,20 +823,6 @@ scc_isr(int irq, struct pt_regs *regs)
 /*       DCD/CTS and Rx/Tx errors                                      */
 
 
-static inline void prepare_next_txframe(register struct scc_channel *scc)
-{
-       if ((scc->tbp = scc->sndq))
-       {
-               scc->sndq = scc->sndq->anext;
-               scc->stat.tx_state = TXS_NEWFRAME;
-
-       } else {
-               scc->stat.tx_state = TXS_BUSY;
-               scc->t_tail = scc->kiss.tailtime;
-       }
-}
-
-
 /* Transmitter interrupt handler */
 static void
 scc_txint(register struct scc_channel *scc)
@@ -647,56 +831,60 @@ scc_txint(register struct scc_channel *scc)
 
        scc->stat.txints++;
 
-       bp = scc->tbp;
-                       
-       while (bp && !bp->cnt)      /* find next buffer */
-               bp = scc_return_buffer(bp, BT_TRANSMIT);
-                               
-       if (bp == NULLBUF)                      /* no more buffers in this frame */
+       bp = scc->tx_bp;
+       
+       if (bp == NULLBUF)
        {
-               if (--scc->stat.tx_queued < 0)
-                       scc->stat.tx_queued = 0;
+               do 
+               {
+                       if (bp != NULLBUF)
+                               scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
+
+                       bp = scc_dequeue_buffer(&scc->tx_queue);
                        
-               Outb(scc->ctrl,RES_Tx_P);       /* reset pending int */                                 
-               cl(scc,R10,ABUNDER);            /* frame complete, allow CRC transmit */ 
-               prepare_next_txframe(scc);
-               
-       } else {                                /* okay, send byte */
-       
-               if (scc->stat.tx_state == TXS_NEWFRAME)
-               {                               /* first byte ? */
-                       Outb(scc->ctrl, RES_Tx_CRC);    /* reset CRC generator */
-                       or(scc,R10,ABUNDER);            /* re-install underrun protection */
-                       Outb(scc->data,bp->data[bp->in_use++]);
-                                                       /* send byte */
-                       if (!scc->enhanced)             /* reset EOM latch */
-                               Outb(scc->ctrl, RES_EOM_L);
+                       if (bp == NULLBUF)
+                       {
+                               scc->stat.tx_state = TXS_BUSY;
+                               scc->t_tail = scc->kiss.tailtime;
                                
-                       scc->stat.tx_state = TXS_ACTIVE;/* next byte... */
-               } else {
-                       Outb(scc->data,bp->data[bp->in_use++]);
-               }
+                               Outb(scc->ctrl, RES_Tx_P);      /* clear int */
+                               return;
+                       }
+                       
+                       if ( scc->kiss.not_slip && (bp->cnt > 0) )
+                       {
+                               bp->rw_ptr++;
+                               bp->cnt--;
+                       }
+                       
+               } while (bp->cnt < 1);
                
-               bp->cnt--;                      /* decrease byte count */
-               scc->tbp=bp;                    /* store buffer address */
-       }
-}
-
-/* Throw away received mbuf(s) when an error occurred */
-
-static inline void
-scc_toss_buffer(register struct scc_channel *scc)
-{
-       register struct mbuf *bp;
-       
-       if((bp = scc->rbp) != NULLBUF)
+               
+               Outb(scc->ctrl, RES_Tx_CRC);    /* reset CRC generator */
+               or(scc,R10,ABUNDER);            /* re-install underrun protection */
+               Outb(scc->data,*bp->rw_ptr);    /* send byte */
+               if (!scc->enhanced)             /* reset EOM latch */
+                       Outb(scc->ctrl, RES_EOM_L);
+               
+               scc->tx_bp = bp;
+               scc->stat.tx_state = TXS_ACTIVE; /* next byte... */
+       } else
+       if (bp->cnt <= 0)
        {
-               scc_free_chain(bp->next, BT_RECEIVE);
-               bp->next = NULLBUF;
-               scc->rbp1 = bp;         /* Don't throw this one away */
-               bp->cnt = 0;            /* Simply rewind it */
-               bp->in_use = 0;
+               if (--scc->stat.tx_queued < 0) scc->stat.tx_queued = 0;
+               
+               Outb(scc->ctrl, RES_Tx_P);      /* reset pending int */
+               cl(scc, R10, ABUNDER);          /* send CRC */
+               scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
+               scc->tx_bp = NULLBUF;
+               scc->stat.tx_state = TXS_NEWFRAME; /* next frame... */
+               return;
+       } else {
+               Outb(scc->data,*bp->rw_ptr);            
        }
+       
+       bp->rw_ptr++;                   /* increment pointer */
+       bp->cnt--;                      /* decrease byte count */
 }
 
 static inline void
@@ -707,17 +895,16 @@ flush_FIFO(register struct scc_channel *scc)
        for (k=0; k<3; k++)
                Inb(scc->data);
                
-       if(scc->rbp != NULLBUF) /* did we receive something? */
+       if(scc->rx_bp != NULLBUF)       /* did we receive something? */
        {
-               if(scc->rbp->next != NULLBUF || scc->rbp->cnt > 0)
-                       scc->stat.rxerrs++;  /* then count it as an error */
-                       
-               scc_toss_buffer(scc);         /* throw away buffer */
+               scc->stat.rxerrs++;  /* then count it as an error */
+               scc_enqueue_buffer(&scc->rx_buffer_pool, scc->rx_bp);
+               
+               scc->rx_bp = NULLBUF;
        }
 }
 
 
-
 /* External/Status interrupt handler */
 static void
 scc_exint(register struct scc_channel *scc)
@@ -753,7 +940,7 @@ scc_exint(register struct scc_channel *scc)
                }
        }
 
-
+#ifdef notdef
        /* CTS: use external TxDelay (what's that good for?!) */
        
        if (chg_and_stat & CTS)                 /* CTS is now ON */
@@ -762,14 +949,20 @@ scc_exint(register struct scc_channel *scc)
                        scc->t_txdel = 0;       /* kick it! */          
                
        }
+#endif
        
-       if ((scc->stat.tx_state == TXS_ACTIVE) && (status & TxEOM))
+       if ( (status & TxEOM) && (scc->stat.tx_state == TXS_ACTIVE) )
        {
                scc->stat.tx_under++;     /* oops, an underrun! count 'em */
                Outb(scc->ctrl, RES_Tx_P);
                Outb(scc->ctrl, RES_EXT_INT);   /* reset ext/status interrupts */
                scc->t_maxk = 1;
-               scc->tbp = scc_free_chain(scc->tbp, BT_TRANSMIT);
+               
+               if (scc->tx_bp != NULLBUF)
+               {
+                       scc_enqueue_buffer(&scc->tx_buffer_pool, scc->tx_bp);
+                       scc->tx_bp = NULLBUF;
+               }
                
                if (--scc->stat.tx_queued < 0) scc->stat.tx_queued = 0;
                or(scc,R10,ABUNDER);
@@ -780,7 +973,13 @@ scc_exint(register struct scc_channel *scc)
                scc->stat.tx_under = 9999;  /* errr... yes. */
                Outb(scc->ctrl, RES_Tx_P); /* just to be sure */
                scc->t_maxk = 1;
-               scc->tbp = scc_free_chain(scc->tbp, BT_TRANSMIT);
+               
+               if (scc->tx_bp != NULLBUF)
+               {
+                       scc_enqueue_buffer(&scc->tx_buffer_pool, scc->tx_bp);
+                       scc->tx_bp = NULLBUF;
+               }
+
                if (--scc->stat.tx_queued < 0) scc->stat.tx_queued = 0;
                scc->kiss.tx_inhibit = 1;       /* don't try it again! */
        }
@@ -806,54 +1005,60 @@ scc_rxint(register struct scc_channel *scc)
                return;
        }
 
-       if ((bp = scc->rbp1) == NULLBUF || bp->cnt >= bp->size) 
-       {                               /* no buffer available or buffer full */
-               if (scc->rbp == NULLBUF)
-               {
-                       if ((bp = scc_get_buffer(BT_RECEIVE)) != NULLBUF)
-                               scc->rbp = scc->rbp1 = bp;
-                               
-               }
-               else if ((bp = scc_get_buffer(BT_RECEIVE)))
-               {
-                       scc_append_to_chain(&scc->rbp, bp);
-                       scc->rbp1 = bp;
-               }
-               
-               if (bp == NULLBUF)              /* no buffer available? */
+       bp = scc->rx_bp;
+       
+       if (bp == NULLBUF)
+       {
+               bp = scc_get_buffer(scc, BT_RECEIVE);
+               if (bp == NULLBUF)
                {
-                       Inb(scc->data);         /* discard character */
-                       or(scc,R3,ENT_HM);      /* enter hunt mode */
-                       scc_toss_buffer(scc);   /* throw away buffers */
-                       scc->stat.nospace++;    /* and count this error */
+                       printk("scc_rxint(): panic --- cannot get a buffer\n");
+                       Inb(scc->data);
+                       or(scc, R3, ENT_HM);
+                       scc->stat.nospace++;
                        return;
                }
+               
+               scc->rx_bp = bp;
        }
-
+       
+       if (bp->cnt > scc->stat.bufsize)
+       {
+#ifdef notdef
+               printk("scc_rxint(): oops, received huge frame...\n");
+#endif
+               scc_enqueue_buffer(&scc->rx_buffer_pool, bp);
+               scc->rx_bp = NULLBUF;
+               Inb(scc->data);
+               or(scc, R3, ENT_HM);
+               return;
+       }
+               
        /* now, we have a buffer. read character and store it */
-       bp->data[bp->cnt++] = Inb(scc->data);
+       *bp->rw_ptr = Inb(scc->data);
+       bp->rw_ptr++;
+       bp->cnt++;
 }
 
 /* kick rx_timer (try to send received frame or part of it ASAP) */
-/* !experimental! */
+
+/* of course we could define a "bottom half" routine to do the job,
+   but since its structures are saved in an array instead of a linked
+   list we would get in trouble if it clashes with another driver or
+   when we try to modularize the driver. IMHO we are fast enough
+   with a timer routine called on the next timer-INT... Your opinions?
+ */
 
 static inline void
 kick_rx_timer(register struct scc_channel *scc)
 {
-       register unsigned long expires;
-
-       if (!rx_timer_cb.lock)
-       {
-               expires = timer_table[SCC_TIMER].expires - jiffies;
-
-               rx_timer_cb.expires = (expires > 1)? expires:1;
-               rx_timer_cb.scc = scc;
-               rx_timer_cb.lock = 1;
-       
-               timer_table[SCC_TIMER].fn = scc_rx_timer;
-               timer_table[SCC_TIMER].expires = jiffies + 1;
-               timer_active |= 1 << SCC_TIMER;
-       }
+       if (scc->rx_t.next)
+               del_timer(&(scc->rx_t));
+               
+       scc->rx_t.expires = jiffies + 1;
+       scc->rx_t.function = scc_rx_timer;
+       scc->rx_t.data = (unsigned long) scc;
+       add_timer(&scc->rx_t);
 }
 
 /* Receive Special Condition interrupt handler */
@@ -868,32 +1073,34 @@ scc_spint(register struct scc_channel *scc)
        status = InReg(scc->ctrl,R1);           /* read receiver status */
        
        Inb(scc->data);                         /* throw away Rx byte */
+       bp = scc->rx_bp;
 
        if(status & Rx_OVR)                     /* receiver overrun */
        {
-               scc->stat.rx_over++;                /* count them */
-               or(scc,R3,ENT_HM);              /* enter hunt mode for next flag */
-               scc_toss_buffer(scc);                 /* rewind the buffer and toss */
+               scc->stat.rx_over++;             /* count them */
+               or(scc,R3,ENT_HM);               /* enter hunt mode for next flag */
+               
+               if (bp) scc_enqueue_buffer(&scc->rx_buffer_pool, bp);
+               scc->rx_bp = NULLBUF;
        }
        
-       if(status & END_FR && scc->rbp != NULLBUF)      /* end of frame */
+       if(status & END_FR && bp != NULLBUF)    /* end of frame */
        {
                /* CRC okay, frame ends on 8 bit boundary and received something ? */
                
-               if (!(status & CRC_ERR) && (status & 0xe) == RES8 && scc->rbp->cnt)
+               if (!(status & CRC_ERR) && (status & 0xe) == RES8 && bp->cnt)
                {
                        /* ignore last received byte (first of the CRC bytes) */
+                       bp->cnt--;
                        
-                       for (bp = scc->rbp; bp->next != NULLBUF; bp = bp->next) ;
-                               bp->cnt--;              /* last byte is first CRC byte */
-                               
-                       scc_enqueue(&scc->rcvq,scc->rbp);
-                       scc->rbp = scc->rbp1 = NULLBUF;
+                       scc_enqueue_buffer(&scc->rx_queue, bp);
+                       scc->rx_bp = NULLBUF;
                        scc->stat.rxframes++;
                        scc->stat.rx_queued++;
                        kick_rx_timer(scc);
                } else {                                /* a bad frame */
-                       scc_toss_buffer(scc);           /* throw away frame */
+                       scc_enqueue_buffer(&scc->rx_buffer_pool, bp);
+                       scc->rx_bp = NULLBUF;
                        scc->stat.rxerrs++;
                }
        }
@@ -925,7 +1132,8 @@ static inline void set_brg(register struct scc_channel *scc, unsigned int tc)
 
 static inline void set_speed(register struct scc_channel *scc)
 {
-       set_brg(scc, (unsigned) (Clock / (scc->modem.speed * 64)) - 2);
+       if (scc->modem.speed > 0)       /* paranoia... */
+               set_brg(scc, (unsigned) (scc->clock / (scc->modem.speed * 64)) - 2);
 }
 
 
@@ -938,19 +1146,68 @@ static inline void init_brg(register struct scc_channel *scc)
        OutReg(scc->ctrl, R14, SNRZI|scc->wreg[R14]);   /* DPLL NRZI mode */
 }
 
+/*
+ * Initalization according to the Z8530 manual (SGS-Thomson's version):
+ *
+ * 1. Modes and constants
+ *
+ * WR9 11000000        chip reset
+ * WR4 XXXXXXXX        Tx/Rx control, async or sync mode
+ * WR1 0XX00X00        select W/REQ (optional)
+ * WR2 XXXXXXXX        program interrupt vector
+ * WR3 XXXXXXX0        select Rx control
+ * WR5 XXXX0XXX        select Tx control
+ * WR6 XXXXXXXX        sync character
+ * WR7 XXXXXXXX        sync character
+ * WR9 000X0XXX        select interrupt control
+ * WR10        XXXXXXXX        miscellaneous control (optional)
+ * WR11        XXXXXXXX        clock control
+ * WR12        XXXXXXXX        time constant lower byte (optional)
+ * WR13        XXXXXXXX        time constant upper byte (optional)
+ * WR14        XXXXXXX0        miscellaneous control
+ * WR14        XXXSSSSS        commands (optional)
+ *
+ * 2. Enables
+ *
+ * WR14        000SSSS1        baud rate enable
+ * WR3 SSSSSSS1        Rx enable
+ * WR5 SSSS1SSS        Tx enable
+ * WR0 10000000        reset Tx CRG (optional)
+ * WR1 XSS00S00        DMA enable (optional)
+ *
+ * 3. Interrupt status
+ *
+ * WR15        XXXXXXXX        enable external/status
+ * WR0 00010000        reset external status
+ * WR0 00010000        reset external status twice
+ * WR1 SSSXXSXX        enable Rx, Tx and Ext/status
+ * WR9 000SXSSS        enable master interrupt enable
+ *
+ * 1 = set to one, 0 = reset to zero
+ * X = user defined, S = same as previous init
+ *
+ *
+ * Note that the implementation differs in some points from above scheme.
+ *
+ */
 static void
 init_channel(register struct scc_channel *scc)
 {
        unsigned long flags;
+       
+       if (scc->rx_t.next) del_timer(&(scc->rx_t));
+       if (scc->tx_t.next) del_timer(&(scc->tx_t));
 
        save_flags(flags); cli();
 
+       wr(scc,R4,X1CLK|SDLC);          /* *1 clock, SDLC mode */
        wr(scc,R1,0);                   /* no W/REQ operation */
        wr(scc,R3,Rx8|RxCRC_ENAB);      /* RX 8 bits/char, CRC, disabled */     
-       wr(scc,R4,X1CLK|SDLC);          /* *1 clock, SDLC mode */
        wr(scc,R5,Tx8|DTR|TxCRC_ENAB);  /* TX 8 bits/char, disabled, DTR */
        wr(scc,R6,0);                   /* SDLC address zero (not used) */
        wr(scc,R7,FLAG);                /* SDLC flag value */
+       wr(scc,R9,VIS);                 /* vector includes status */
        wr(scc,R10,(scc->modem.nrz? NRZ : NRZI)|CRCPS|ABUNDER); /* abort on underrun, preset CRC generator, NRZ(I) */
        wr(scc,R14, 0);
 
@@ -989,20 +1246,19 @@ init_channel(register struct scc_channel *scc)
                        break;
 
                case CLK_DIVIDER:
-                       wr(scc, R11, ((Board & BAYCOM)? TRxCDP : TRxCBR) | RCDPLL|TCRTxCP|TRxCOI);
+                       wr(scc, R11, ((scc->brand & BAYCOM)? TRxCDP : TRxCBR) | RCDPLL|TCRTxCP|TRxCOI);
                        init_brg(scc);
                        break;
 
                case CLK_EXTERNAL:
-                       wr(scc, R11, (Board & BAYCOM)? RCTRxCP|TCRTxCP : RCRTxCP|TCTRxCP);
+                       wr(scc, R11, (scc->brand & BAYCOM)? RCTRxCP|TCRTxCP : RCRTxCP|TCTRxCP);
                        OutReg(scc->ctrl, R14, DISDPLL);
                        break;
 
        }
        
-       /* enable CTS (not for Baycom), ABORT & DCD interrupts */
-       wr(scc,R15,((Board & BAYCOM) ? 0 : CTSIE)|BRKIE|DCDIE|TxUIE);
-
+       set_speed(scc);                 /* set baudrate */
+       
        if(scc->enhanced)
        {
                or(scc,R15,SHDLCE|FIFOE);       /* enable FIFO, SDLC/HDLC Enhancements (From now R7 is R7') */
@@ -1017,17 +1273,21 @@ init_channel(register struct scc_channel *scc)
                or(scc,R3,ENT_HM|RxENABLE);     /* enable the receiver, hunt mode */
        }
        
+       /* enable CTS (not for Baycom), ABORT & DCD interrupts */
+       wr(scc,R15,((scc->brand & BAYCOM) ? 0 : CTSIE)|BRKIE|DCDIE|TxUIE);
+       
        Outb(scc->ctrl,RES_EXT_INT);    /* reset ext/status interrupts */
        Outb(scc->ctrl,RES_EXT_INT);    /* must be done twice */
-       
-       scc->status = InReg(scc->ctrl,R0);      /* read initial status */
 
        or(scc,R1,INT_ALL_Rx|TxINT_ENAB|EXT_INT_ENAB); /* enable interrupts */
+       
+       scc->status = InReg(scc->ctrl,R0);      /* read initial status */
+       
        or(scc,R9,MIE);                 /* master interrupt enable */
+       
+       scc_init_timer(scc);
                        
        restore_flags(flags);
-       
-       set_speed(scc); 
 }
 
 
@@ -1049,10 +1309,10 @@ scc_key_trx(struct scc_channel *scc, char tx)
        if (scc->modem.speed < baud_table[1]) 
                scc->modem.speed = 1200;
                
-       if (Board & PRIMUS)
-               Outb(scc->ctrl + 4, Option | (tx? 0x80 : 0));
+       if (scc->brand & PRIMUS)
+               Outb(scc->ctrl + 4, scc->option | (tx? 0x80 : 0));
        
-       time_const = (unsigned) (Clock / (scc->modem.speed * (tx? 2:64))) - 2;
+       time_const = (unsigned) (scc->clock / (scc->modem.speed * (tx? 2:64))) - 2;
        
        if (scc->modem.clocksrc == CLK_DPLL)
        {                               /* simplex operation */
@@ -1155,10 +1415,9 @@ static inline void txdel_timeout(register struct scc_channel *scc)
        scc->t_txdel = TIMER_STOPPED;
        
        scc->t_maxk = TPS * scc->kiss.maxkeyup;
-       prepare_next_txframe(scc);
        
-       if (scc->stat.tx_state != TXS_BUSY)
-               scc_txint(scc);         
+       if (scc->tx_bp == NULLBUF)
+               scc_txint(scc); 
 }
        
 
@@ -1177,7 +1436,7 @@ static inline void tail_timeout(register struct scc_channel *scc)
         
         if (scc->kiss.fulldup < 2)
         {
-               if (scc->sndq)          /* we had a timeout? */
+               if (scc->tx_bp)         /* we had a timeout? */
                {
                        scc->stat.tx_state = TXS_BUSY;
                        scc->t_dwait = TPS * scc->kiss.mintime; /* try again */
@@ -1189,7 +1448,7 @@ static inline void tail_timeout(register struct scc_channel *scc)
                return;
         }
         
-        if (scc->sndq)                 /* maxkeyup expired */ /* ?! */
+        if (scc->tx_bp)                        /* maxkeyup expired */ /* ?! */
         {
                scc->stat.tx_state = TXS_BUSY;
                scc->t_txdel = TPS * scc->kiss.waittime;
@@ -1205,11 +1464,10 @@ static inline void busy_timeout(register struct scc_channel *scc)
 #ifdef THROW_AWAY_AFTER_BUSY_TIMEOUT
        register struct mbuf *bp;               /* not tested */
 
-       bp = scc->sndq;
-       
-       while (bp) bp = scc_free_chain(bp, BT_TRANSMIT);
-       
-       scc->sndq = NULLBUF;
+       while (bp = scc_dequeue_buffer(&scc->tx_queue))
+               scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
+               
+       scc->tx_queue = NULLBUF;
        scc->stat.tx_state = TXS_IDLE;
        
 #else
@@ -1229,81 +1487,66 @@ static inline void maxk_idle_timeout(register struct scc_channel *scc)
        scc->t_tail = scc->kiss.tailtime;
 }
 
-static inline void check_rcv_queue(register struct scc_channel *scc)
-{
-       register struct mbuf *bp;
-       
-       if (scc->stat.rx_queued > QUEUE_THRES)
-       {
-               if (scc->rcvq == NULLBUF)
-               {
-                       printk("z8530drv: Warning - scc->stat.rx_queued shows overflow"
-                              " (%d) but queue is empty\n", scc->stat.rx_queued);
-                              
-                       scc->stat.rx_queued = 0;        /* correct it */
-                       scc->stat.nospace = 12345;      /* draw attention to it */
-                       return;
-               }
-                       
-               bp = scc->rcvq->anext;  /* don't use the one we currently use */
-               
-               while (bp && (scc->stat.rx_queued > QUEUE_HYST))
-               {
-                       bp = scc_free_chain(bp, BT_RECEIVE);
-                       scc->stat.rx_queued--;
-                       scc->stat.nospace++;
-               }
-               
-               scc->rcvq->anext = bp;
-       }
-}
-
 static void
-scc_timer(void)
+scc_tx_timer(unsigned long channel)
 {
        register struct scc_channel *scc;
-       register int chan;
        unsigned long flags;
                
 
-       for (chan = 0; chan < (Nchips * 2); chan++)
-       {
-               scc = &SCC_Info[chan];
+       scc = (struct scc_channel *) channel;
                
-               if (scc->tty && scc->init)
-               {
-                       kiss_encode(scc);
-                       
-                       save_flags(flags); cli();
-                       
-                       check_rcv_queue(scc);
-                       
-                       /* KISS-TNC emulation */
-                       
-                       if (Expired(t_dwait)) dw_slot_timeout(scc)      ; else
-                       if (Expired(t_slot))  dw_slot_timeout(scc)      ; else
-                       if (Expired(t_txdel)) txdel_timeout(scc)        ; else
-                       if (Expired(t_tail))  tail_timeout(scc)         ;
-                       
-                       /* watchdogs */
+       if (scc->tty && scc->init)
+       {               
+               save_flags(flags); cli();
+               
+               /* KISS-TNC emulation */
+               
+               if (Expired(t_dwait)) dw_slot_timeout(scc)      ; else
+               if (Expired(t_slot))  dw_slot_timeout(scc)      ; else
+               if (Expired(t_txdel)) txdel_timeout(scc)        ; else
+               if (Expired(t_tail))  tail_timeout(scc)         ;
                        
-                       if (Expired(t_mbusy)) busy_timeout(scc);
-                       if (Expired(t_maxk))  maxk_idle_timeout(scc);
-                       if (Expired(t_idle))  maxk_idle_timeout(scc);
+               /* watchdogs */
+               
+               if (Expired(t_mbusy)) busy_timeout(scc);
+               if (Expired(t_maxk))  maxk_idle_timeout(scc);
+               if (Expired(t_idle))  maxk_idle_timeout(scc);
                        
-                       restore_flags(flags);
-               }
+               restore_flags(flags);
        }
        
-       save_flags(flags); cli();
+       scc->tx_t.expires = jiffies + HZ/TPS;
+       add_timer(&scc->tx_t);
+}
+                       
+
+static void
+scc_rx_timer(unsigned long channel)
+{
+       register struct scc_channel *scc;
        
-       timer_table[SCC_TIMER].fn = scc_timer;
-       timer_table[SCC_TIMER].expires = jiffies + HZ/TPS;
-       timer_active |= 1 << SCC_TIMER; 
+       scc = (struct scc_channel *) channel;
        
-       restore_flags(flags);
+       if (scc->rx_queue && scc->throttled)
+       {
+               scc->rx_t.expires = jiffies + HZ/TPS;
+               add_timer(&scc->rx_t);
+               return;
+       }
+       
+       kiss_encode(scc);
+       
+       if (scc->rx_queue && !scc->throttled)
+       {
+
+               printk("z8530drv: warning: %s should be throttled\n", 
+                      kdevname(scc->tty->device));
+                              
+               scc->rx_t.expires = jiffies + HZ/TPS;
+               add_timer(&scc->rx_t);
+       }
 }
-                       
 
 static void
 scc_init_timer(struct scc_channel *scc)
@@ -1319,26 +1562,19 @@ scc_init_timer(struct scc_channel *scc)
        Stop_Timer(t_mbusy);
        Stop_Timer(t_maxk);
        Stop_Timer(t_idle);
-       scc->stat.tx_state = TXS_IDLE;
        
-       restore_flags(flags);
-}
-
-
-static void
-scc_rx_timer(void)
-{
-       unsigned long flags;
-       
-       kiss_encode(rx_timer_cb.scc);
+       scc->stat.tx_state = TXS_IDLE;
        
-       save_flags(flags); cli();
+       if (scc->tx_t.next) 
+               del_timer(&scc->tx_t);
        
-       timer_table[SCC_TIMER].fn = scc_timer;
-       timer_table[SCC_TIMER].expires = jiffies + rx_timer_cb.expires;
-       timer_active |= 1 << SCC_TIMER;
+       scc->tx_t.data = (unsigned long) scc;
+       scc->tx_t.function = scc_tx_timer;
+       scc->tx_t.expires = jiffies + HZ/TPS;
+       add_timer(&scc->tx_t);
        
-       rx_timer_cb.lock = 0;
+       scc->rx_t.data = (unsigned long) scc;
+       scc->rx_t.function = scc_rx_timer;
        
        restore_flags(flags);
 }
@@ -1401,15 +1637,23 @@ static void kiss_interpret_frame(struct scc_channel * scc)
 {
        unsigned char kisscmd;
        unsigned long flags;
+       struct mbuf *bp;
 
-       if (scc->sndq1->cnt < 2)
+       bp = scc->kiss_decode_bp;
+       bp->rw_ptr = bp->data;
+       
+#ifdef DEBUG_BUFFERS
+       if (bp == NULLBUF)
        {
-               if (scc->sndq1) 
-                       scc_free_chain(scc->sndq1, BT_TRANSMIT);
-               else
-                       scc->sndq1 = NULLBUF;
-                       
-               scc->sndq2 = NULLBUF;
+               printk("kiss_interpret_frame(): weird --- nothing to do.\n");
+               return;
+       }
+#endif
+       
+       if (bp->cnt < 2)
+       {
+               scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
+               scc->kiss_decode_bp = NULLBUF;
                return;
        }
        
@@ -1417,20 +1661,20 @@ static void kiss_interpret_frame(struct scc_channel * scc)
        
        if (scc->kiss.not_slip)
        {
-               kisscmd = scc->sndq1->data[scc->sndq1->in_use++];
-               scc->sndq1->cnt--;
+               kisscmd = *bp->rw_ptr;
+               bp->rw_ptr++;
        } else {
                kisscmd = 0;
        }
 
        if (kisscmd & 0xa0)
        {
-               if (scc->sndq1->cnt > 2)
-                       scc->sndq1->cnt -= 2;
+               if (bp->cnt > 3) 
+                       bp->cnt -= 2;
                else
                {
-                       scc_free_chain(scc->sndq1, BT_TRANSMIT);
-                       scc->sndq2 = NULLBUF;
+                       scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
+                       scc->kiss_decode_bp = NULLBUF;
                        return;
                }
        }
@@ -1441,25 +1685,22 @@ static void kiss_interpret_frame(struct scc_channel * scc)
                
        if (kisscmd)
        {
-               kiss_set_param(scc, kisscmd, scc->sndq1->data[scc->sndq1->in_use]);
-               scc->sndq1->cnt=0;
-               scc->sndq1->in_use=0;
-                                       
-               scc_free_chain(scc->sndq1, BT_TRANSMIT);
-               scc->sndq2 = NULLBUF;
+               kiss_set_param(scc, kisscmd, *bp->rw_ptr);
+               scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
+               scc->kiss_decode_bp = NULLBUF;
                return;
        }
+
+       scc_enqueue_buffer(&scc->tx_queue, bp); /* enqueue frame */
        
-       scc_enqueue(&scc->sndq,scc->sndq1); /* scc_enqueue packet */
        scc->stat.txframes++;
        scc->stat.tx_queued++;
-       scc->sndq2 = NULLBUF;           /* acquire a new buffer next time */
+       scc->kiss_decode_bp = NULLBUF;
 
        save_flags(flags); cli();
 
        if(scc->stat.tx_state == TXS_IDLE)
        {                               /* when transmitter is idle */
-               scc_init_timer(scc);
                scc->stat.tx_state = TXS_BUSY;
                scc->t_dwait = (scc->kiss.fulldup? 0:scc->kiss.waittime);
        }
@@ -1467,21 +1708,21 @@ static void kiss_interpret_frame(struct scc_channel * scc)
        restore_flags(flags);
 }
 
-static void kiss_store_byte(struct scc_channel *scc, unsigned char ch)
+static inline void kiss_store_byte(struct scc_channel *scc, unsigned char ch)
 {
-       if (scc->sndq2 == NULLBUF) return;
+       register struct mbuf *bp = scc->kiss_decode_bp;
        
-       if(scc->sndq2->cnt == scc->sndq2->size)         /* buffer full? */
+       if (bp != NULLBUF)
        {
-               if((scc->sndq2 = scc_get_buffer(BT_TRANSMIT)) == NULLBUF)
+               if (bp->cnt > scc->stat.bufsize)
+                       printk("kiss_decode(): frame too long\n");
+               else
                {
-                       printk("\nsccdrv: running out of memory\n");
-                       return;
+                       *bp->rw_ptr = ch;
+                       bp->rw_ptr++;
+                       bp->cnt++;
                }
-               scc_append_to_chain(&scc->sndq1,scc->sndq2);         /* add buffer */
        }
-       
-       scc->sndq2->data[scc->sndq2->cnt++] = ch;
 }
 
 static inline int kiss_decode(struct scc_channel *scc, unsigned char ch)
@@ -1491,9 +1732,10 @@ static inline int kiss_decode(struct scc_channel *scc, unsigned char ch)
                case KISS_IDLE:
                        if (ch == FEND)
                        {
-                               if (!(scc->sndq2 = scc->sndq1 = scc_get_buffer(BT_TRANSMIT)))
-                                       return 0;
-                                       
+                               scc->kiss_decode_bp = scc_get_buffer(scc, BT_TRANSMIT);
+                               if (scc->kiss_decode_bp == NULLBUF)
+                                       return 1;
+
                                scc->stat.tx_kiss_state = KISS_DATA;
                        } else scc->stat.txerrs++;
                        break;
@@ -1503,7 +1745,7 @@ static inline int kiss_decode(struct scc_channel *scc, unsigned char ch)
                                scc->stat.tx_kiss_state = KISS_ESCAPE;
                        else if (ch == FEND)
                        {
-                               kiss_interpret_frame(scc);      
+                               kiss_interpret_frame(scc);
                                scc->stat.tx_kiss_state = KISS_IDLE;
                        }
                        else kiss_store_byte(scc, ch);
@@ -1522,8 +1764,8 @@ static inline int kiss_decode(struct scc_channel *scc, unsigned char ch)
                        }
                        else
                        {
-                               scc_free_chain(scc->sndq1, BT_TRANSMIT);
-                               scc->sndq2 = NULLBUF;
+                               scc_enqueue_buffer(&scc->tx_buffer_pool, scc->kiss_decode_bp);
+                               scc->kiss_decode_bp = NULLBUF;
                                scc->stat.txerrs++;
                                scc->stat.tx_kiss_state = KISS_IDLE;
                        }
@@ -1536,91 +1778,78 @@ static inline int kiss_decode(struct scc_channel *scc, unsigned char ch)
 
 /* ----> Encode received data and write it to the flip-buffer  <---- */
 
-/* receive raw frame from SCC. used for AX.25 */
 static void
 kiss_encode(register struct scc_channel *scc)
 {
-       struct mbuf *bp,*bp2;
+       struct mbuf *bp;
        struct tty_struct * tty = scc->tty;
-       unsigned long flags; 
        unsigned char ch;
 
-       if(!scc->rcvq)
-       {
-               scc->stat.rx_kiss_state = KISS_IDLE;
-               return;
-       }
+       bp = scc->kiss_encode_bp;
        
        /* worst case: FEND 0 FESC TFEND -> 4 bytes */
        
-       while(tty->flip.count < TTY_FLIPBUF_SIZE-3)
-       { 
-               if (scc->rcvq->cnt)
+       while(tty->flip.count < TTY_FLIPBUF_SIZE-4)
+       {
+               if (bp == NULLBUF)
                {
-                       bp = scc->rcvq;
+                       bp = scc_dequeue_buffer(&scc->rx_queue);
+                       scc->kiss_encode_bp = bp;
                        
-                       if (scc->stat.rx_kiss_state == KISS_IDLE)
-                       {
-                               tty_insert_flip_char(tty, FEND, 0);
-                               
-                               if (scc->kiss.not_slip)
-                                       tty_insert_flip_char(tty, 0, 0);
-                                       
-                               scc->stat.rx_kiss_state = KISS_RXFRAME;
-                       }
-                               
-                       switch(ch = bp->data[bp->in_use++])
+                       if (bp == NULLBUF)
                        {
-                               case FEND:
-                                       tty_insert_flip_char(tty, FESC, 0);
-                                       tty_insert_flip_char(tty, TFEND, 0);
-                                       break;
-                               case FESC:
-                                       tty_insert_flip_char(tty, FESC, 0);
-                                       tty_insert_flip_char(tty, TFESC, 0);
-                                       break;
-                               default:
-                                       tty_insert_flip_char(tty, ch, 0);
+                               scc->stat.rx_kiss_state = KISS_IDLE;
+                               break;
                        }
-                       
-                       bp->cnt--;
-                       
-               } else {
-                       save_flags(flags); cli();
-                       
-                       while (!scc->rcvq->cnt)
-                       {                                /* buffer empty? */
-                               bp  = scc->rcvq->next;  /* next buffer */
-                               bp2 = scc->rcvq->anext; /* next packet */
-                               
-                               
-                               scc_return_buffer(scc->rcvq, BT_RECEIVE);
-                               
-                               if (!bp)        /* end of frame ? */
-                               {
-                                       scc->rcvq = bp2;
-                                       
-                                       if (--scc->stat.rx_queued < 0)
-                                               scc->stat.rx_queued = 0;
-                                       
-                                       if (scc->stat.rx_kiss_state == KISS_RXFRAME)    /* new packet? */
-                                       {
-                                               tty_insert_flip_char(tty, FEND, 0); /* send FEND for old frame */
-                                               scc->stat.rx_kiss_state = KISS_IDLE; /* generate FEND for new frame */
-                                       }
-                                       
-                                       restore_flags(flags);
-                                       queue_task(&tty->flip.tqueue, &tq_timer);
-                                       return;
+               }
+               
+
+               if (bp->cnt <= 0)
+               {
+                       if (--scc->stat.rx_queued < 0)
+                               scc->stat.rx_queued = 0;
                                        
-                               } else scc->rcvq = bp; /* next buffer */
+                       if (scc->stat.rx_kiss_state == KISS_RXFRAME)    /* new packet? */
+                       {
+                               tty_insert_flip_char(tty, FEND, 0);  /* send FEND for old frame */
+                               scc->stat.rx_kiss_state = KISS_IDLE; /* generate FEND for new frame */
                        }
                        
-                       restore_flags(flags);
-               }                                               
+                       scc_enqueue_buffer(&scc->rx_buffer_pool, bp);
+                       
+                       bp = scc->kiss_encode_bp = NULLBUF;
+                       continue;
+               }
                
+
+               if (scc->stat.rx_kiss_state == KISS_IDLE)
+               {
+                       tty_insert_flip_char(tty, FEND, 0);
+                       
+                       if (scc->kiss.not_slip)
+                               tty_insert_flip_char(tty, 0, 0);
+                                       
+                       scc->stat.rx_kiss_state = KISS_RXFRAME;
+               }
+                               
+               switch(ch = *bp->rw_ptr)
+               {
+                       case FEND:
+                               tty_insert_flip_char(tty, FESC, 0);
+                               tty_insert_flip_char(tty, TFEND, 0);
+                               break;
+                       case FESC:
+                               tty_insert_flip_char(tty, FESC, 0);
+                               tty_insert_flip_char(tty, TFESC, 0);
+                               break;
+                       default:
+                               tty_insert_flip_char(tty, ch, 0);
+               }
+                       
+               bp->rw_ptr++;
+               bp->cnt--;
        }
-       
+               
        queue_task(&tty->flip.tqueue, &tq_timer); /* kick it... */
 }
 
@@ -1634,27 +1863,42 @@ static void
 z8530_init(void)
 {
        struct scc_channel *scc;
-       int chip;
+       int chip, k;
        unsigned long flags;
+       char *flag;
+
+
+       printk("Init Z8530 driver: %u channels, IRQ", Nchips*2);
+       
+       flag=" ";
+       for (k = 0; k < 16; k++)
+               if (Ivec[k].used) 
+               {
+                       printk("%s%d", flag, k);
+                       flag=",";
+               }
+       printk("\n");
+       
 
        /* reset and pre-init all chips in the system */
        for (chip = 0; chip < Nchips; chip++)
        {
+               scc=&SCC_Info[2*chip];
+               if (!scc->ctrl) continue;
+                       
+               save_flags(flags); cli();       /* because of 2-step accesses */
+               
                /* Special SCC cards */
 
-               if(Board & EAGLE)                       /* this is an EAGLE card */
-                       Outb(Special_Port,0x08);        /* enable interrupt on the board */
+               if(scc->brand & EAGLE)                  /* this is an EAGLE card */
+                       Outb(scc->special,0x08);        /* enable interrupt on the board */
                        
-               if(Board & (PC100 | PRIMUS))            /* this is a PC100/EAGLE card */
-                       Outb(Special_Port,Option);      /* set the MODEM mode (0x22) */
+               if(scc->brand & (PC100 | PRIMUS))       /* this is a PC100/EAGLE card */
+                       Outb(scc->special,scc->option); /* set the MODEM mode (0x22) */
+
                        
                /* Init SCC */
-               
-               scc=&SCC_Info[2*chip];
-               if (!scc->ctrl) continue;
-               
-               save_flags(flags); cli();
-               
+
                /* some general init we can do now */
                
                Outb(scc->ctrl, 0);
@@ -1666,8 +1910,6 @@ z8530_init(void)
                restore_flags(flags);
         }
 
-       if (Ivec == 2) Ivec = 9;                        /* this f... IBM AT-design! */
-       request_irq(Ivec, scc_isr,   SA_INTERRUPT, "AX.25 SCC");
  
        Driver_Initialized = 1;
 }
@@ -1685,20 +1927,23 @@ static inline int scc_paranoia_check(struct scc_channel *scc, kdev_t device,
                                     const char *routine)
 {
 #ifdef SCC_PARANOIA_CHECK
-       static const char *badmagic =
-               "Warning: bad magic number for Z8530 SCC struct (%s) in %s\n";
-        static const char *badinfo =
-                "Warning: Z8530 not found for (%s) in %s\n";
+
+static const char *badmagic = 
+       "Warning: bad magic number for Z8530 SCC struct (%s) in %s\n"; 
+static const char *badinfo =  
+       "Warning: Z8530 not found for (%s) in %s\n";
        
        if (!scc->init) 
        {
-               printk(badinfo, kdevname(device), routine);
-                return 1;
-        }
-        if (scc->magic != SCC_MAGIC) {
-               printk(badmagic, kdevname(device), routine);
-                return 1;
-        }
+                       printk(badinfo, kdevname(device), routine);
+               return 1;
+       }
+       
+       if (scc->magic != SCC_MAGIC)
+       {
+               printk(badmagic, kdevname(device), routine);
+               return 1;
+       }
 #endif
 
        return 0;
@@ -1712,46 +1957,49 @@ int scc_open(struct tty_struct *tty, struct file * filp)
        struct scc_channel *scc;
        int chan;
        
-        chan = MINOR(tty->device) - tty->driver.minor_start;
-        if ((chan < 0) || (chan >= (Nchips * 2)))
-                return -ENODEV;
+       chan = MINOR(tty->device) - tty->driver.minor_start;
+       
+       if (Driver_Initialized)
+       {
+               if ( (chan < 0) || (chan >= (Nchips * 2)) )
+                       return -ENODEV;
+        } else {
+               tty->driver_data = &SCC_Info[0];
+               MOD_INC_USE_COUNT;
+               return 0;
+        }
  
        scc = &SCC_Info[chan];
        
        tty->driver_data = scc;
-       tty->termios->c_cflag &= ~CBAUD; 
+       tty->termios->c_cflag &= ~CBAUD;
        
-       if (!Driver_Initialized)
-               return 0;
-       
        if (scc->magic != SCC_MAGIC)
        {
-               printk("ERROR: scc_open(): bad magic number for device ("
-                      "%s)",
+               printk("ERROR: scc_open(): bad magic number for device (%s)",
                       kdevname(tty->device));
                return -ENODEV;
        }               
        
+       MOD_INC_USE_COUNT;
+       
        if(scc->tty != NULL)
        {
                scc->tty_opened++;
                return 0;
        }
 
-       if(!scc->init) return 0;
-                        
        scc->tty = tty;
-       init_channel(scc);
+       alloc_buffer_pool(scc);
+       
+       if(!scc->init) return 0;
+       
+       scc->throttled = 0;
 
        scc->stat.tx_kiss_state = KISS_IDLE;    /* don't change this... */
        scc->stat.rx_kiss_state = KISS_IDLE;    /* ...or this */
-       
-       scc_init_timer(scc);
-       
-       timer_table[SCC_TIMER].fn = scc_timer;
-       timer_table[SCC_TIMER].expires = 0;     /* now! */
-       timer_active |= 1 << SCC_TIMER;
-       
+       init_channel(scc);
        return 0;
 }
 
@@ -1766,6 +2014,8 @@ scc_close(struct tty_struct *tty, struct file * filp)
 
         if (!scc || (scc->magic != SCC_MAGIC))
                 return;
+                
+        MOD_DEC_USE_COUNT;
        
        if(scc->tty_opened)
        {
@@ -1775,6 +2025,9 @@ scc_close(struct tty_struct *tty, struct file * filp)
        
        tty->driver_data = NULLBUF;
        
+       if (!Driver_Initialized)
+               return;
+       
        save_flags(flags); cli();
        
        Outb(scc->ctrl,0);              /* Make sure pointer is written */
@@ -1783,13 +2036,18 @@ scc_close(struct tty_struct *tty, struct file * filp)
        
        scc->tty = NULL;
        
+       del_timer(&scc->tx_t);
+       del_timer(&scc->rx_t);
+
+       free_buffer_pool(scc);
+       
        restore_flags(flags);
-       tty->stopped = 0;               
+       
+       scc->throttled = 0;
+       tty->stopped = 0;
 }
 
 
-
-
 /*
  * change scc_speed
  */
@@ -1797,10 +2055,15 @@ scc_close(struct tty_struct *tty, struct file * filp)
 static void
 scc_change_speed(struct scc_channel * scc)
 {
+       long speed;
+       
        if (scc->tty == NULL)
                return;
                
-       scc->modem.speed = baud_table[scc->tty->termios->c_cflag & CBAUD];
+
+       speed = baud_table[scc->tty->termios->c_cflag & CBAUD];
+       
+       if (speed > 0) scc->modem.speed = speed;
        
        if (scc->stat.tx_state == 0)    /* only switch baudrate on rx... ;-) */
                set_speed(scc);
@@ -1833,11 +2096,13 @@ scc_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned
        unsigned int result;
        unsigned int value;
        struct ioctl_command kiss_cmd;
-       int error;
+       struct scc_mem_config memcfg;
+       struct scc_hw_config hwcfg;
+       int error, chan;
 
         if (scc->magic != SCC_MAGIC) 
         {
-               printk("ERROR: scc_ioctl(): bad magic number for device %s", 
+               printk("ERROR: scc_ioctl(): bad magic number for device %s",
                        kdevname(tty->device));
                        
                 return -ENODEV;
@@ -1847,13 +2112,106 @@ scc_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned
        
        if (!Driver_Initialized)
        {
+               if (cmd == TIOCSCCCFG)
+               {
+                       int found = 1;
+                       
+                       if (!suser()) return -EPERM;
+                       if (!arg) return -EFAULT;
+                       
+                       if (Nchips >= MAXSCC) 
+                               return -EINVAL;
+                       
+                       memcpy_fromfs(&hwcfg, (void *) arg, sizeof(hwcfg));
+                       
+                       if (hwcfg.irq == 2) hwcfg.irq = 9;
+                       
+                       if (!Ivec[hwcfg.irq].used && hwcfg.irq)
+                       {
+                               if (request_irq(hwcfg.irq, scc_isr, SA_INTERRUPT, "AX.25 SCC"))
+                                       printk("z8530drv: Warning --- could not get IRQ %d\n", hwcfg.irq);
+                               else
+                                       Ivec[hwcfg.irq].used = 1;
+                       }
+                       
+                       if (hwcfg.vector_latch) 
+                               Vector_Latch = hwcfg.vector_latch;
+                               
+                       if (hwcfg.clock == 0)
+                               hwcfg.clock = DEFAULT_CLOCK;
+
+#ifndef DONT_CHECK
+                       save_flags(flags); cli();
+                       
+                       check_region(scc->ctrl, 1);
+                       Outb(hwcfg.ctrl_a, 0);
+                       udelay(5);
+                       OutReg(hwcfg.ctrl_a,R13,0x55);          /* is this chip really there? */
+                       udelay(5);
+                       
+                       if (InReg(hwcfg.ctrl_a,R13) != 0x55 )
+                               found = 0;
+                               
+                       restore_flags(flags);
+#endif
+
+                       if (found)
+                       {
+                               SCC_Info[2*Nchips  ].ctrl = hwcfg.ctrl_a;
+                               SCC_Info[2*Nchips  ].data = hwcfg.data_a;
+                               SCC_Info[2*Nchips+1].ctrl = hwcfg.ctrl_b;
+                               SCC_Info[2*Nchips+1].data = hwcfg.data_b;
+                       
+                               SCC_ctrl[2*Nchips  ] = hwcfg.ctrl_a;
+                               SCC_ctrl[2*Nchips+1] = hwcfg.ctrl_b;
+                       }
+               
+                       for (chan = 0; chan < 2; chan++)
+                       {
+                               SCC_Info[2*Nchips+chan].special = hwcfg.special;
+                               SCC_Info[2*Nchips+chan].clock = hwcfg.clock;
+                               SCC_Info[2*Nchips+chan].brand = hwcfg.brand;
+                               SCC_Info[2*Nchips+chan].option = hwcfg.option;
+                               SCC_Info[2*Nchips+chan].enhanced = hwcfg.escc;
+                       
+#ifdef DONT_CHECK
+                               printk("%s%i: data port = 0x%3.3x  control port = 0x%3.3x\n",
+                                       scc_driver.name, 2*Nchips+chan, 
+                                       SCC_Info[2*Nchips+chan].data, 
+                                       SCC_Info[2*Nchips+chan].ctrl);
+
+#else
+                               printk("%s%i: data port = 0x%3.3x  control port = 0x%3.3x -- %s\n",
+                                       scc_driver.name, 2*Nchips+chan, 
+                                       chan? hwcfg.data_b : hwcfg.data_a, 
+                                       chan? hwcfg.ctrl_b : hwcfg.ctrl_a,
+                                       found? "found" : "missing");
+#endif
+                               
+                               if (found)
+                               {
+                                       request_region(SCC_Info[2*Nchips+chan].ctrl, 1, "scc ctrl");
+                                       request_region(SCC_Info[2*Nchips+chan].data, 1, "scc data");
+                               }
+                       }
+                       
+                       if (found) Nchips++;
+                       
+                       return 0;
+               }
+               
                if (cmd == TIOCSCCINI)
                {
                        if (!suser())
                                return -EPERM;
+                               
+                       if (Nchips == 0)
+                               return -EINVAL;
                        
-                       scc_alloc_buffer_pool();
                        z8530_init();
+                       
+                       scc->tty=tty;
+                       alloc_buffer_pool(scc);
                        return 0;
                }
                
@@ -1862,7 +2220,6 @@ scc_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned
        
        if (!scc->init)
        {
-
                if (cmd == TIOCCHANINI)
                {
                        if (!arg)
@@ -1979,8 +2336,6 @@ scc_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned
        case TCSETS:
        case TCSETSF:           /* should flush first, but... */
        case TCSETSW:           /* should wait 'till flush, but... */
-               if (!suser())
-                       return -EPERM;
                if (!arg)
                        return -EFAULT;
                
@@ -1988,6 +2343,23 @@ scc_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned
                scc_change_speed(scc);
                return 0;
                
+       case TIOCCHANMEM:
+               if (!arg)
+                       return -EFAULT;
+                       
+               memcpy_fromfs(&memcfg, (void *) arg, sizeof(struct scc_mem_config));
+               
+               save_flags(flags); cli();
+               
+               free_buffer_pool(scc);
+               scc->stat.rxbuffers = memcfg.rxbuffers;
+               scc->stat.txbuffers = memcfg.txbuffers;
+               scc->stat.bufsize   = memcfg.bufsize;
+               alloc_buffer_pool(scc);
+               
+               restore_flags(flags);
+               return 0;
+               
                
        case TIOCSCCSTAT:
                error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(struct scc_stat));
@@ -1997,9 +2369,6 @@ scc_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned
                if (!arg)
                        return -EFAULT;
                        
-               scc->stat.used_buf = scc_count_used_buffers(&scc->stat.rx_alloc, 
-                                                           &scc->stat.tx_alloc);
-                       
                memcpy_tofs((void *) arg, &scc->stat, sizeof(struct scc_stat));
                return 0;
                
@@ -2051,9 +2420,6 @@ scc_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned
                if (!arg)
                        return -EFAULT;
 
-               if (!suser())
-                       return -EPERM;
-                       
                memcpy_fromfs(&kiss_cmd, (void *) arg, sizeof(struct ioctl_command));
                
                switch (kiss_cmd.command)
@@ -2092,55 +2458,14 @@ scc_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned
 }
 
 
-/* ----- TERMIOS function ----- */
-
-static void
-scc_set_termios(struct tty_struct * tty, struct termios * old_termios)
-{
-       if (tty->termios->c_cflag == old_termios->c_cflag) 
-               return;
-       scc_change_speed(tty->driver_data);
-}
-
-
-static inline void check_tx_queue(register struct scc_channel *scc)
-{
-       register struct mbuf *bp;
-       
-       if (scc->stat.tx_queued > QUEUE_THRES)
-       {
-               if (scc->sndq1 == NULLBUF)
-               {
-                       printk("z8530drv: Warning - scc->stat.tx_queued shows overflow"
-                              " (%d) but queue is empty\n", scc->stat.tx_queued);
-                              
-                       scc->stat.tx_queued = 0;        /* correct it */
-                       scc->stat.nospace = 54321;      /* draw attention to it */
-                       return;
-               }
-                       
-               bp = scc->sndq1->anext; /* don't use the one we currently use */
-               
-               while (bp && (scc->stat.tx_queued > QUEUE_HYST))
-               {
-                       bp = scc_free_chain(bp, BT_TRANSMIT);
-                       scc->stat.tx_queued--;
-                       scc->stat.nospace++;
-               }
-               
-               scc->sndq1->anext = bp;
-       }
-}
-
-
-
 /* ----> tx routine: decode KISS data and scc_enqueue it <---- */
 
 /* send raw frame to SCC. used for AX.25 */
 int scc_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
 {
        struct scc_channel * scc = tty->driver_data;
-       unsigned char tbuf[BUFSIZE], *p;
+       unsigned char *p;
+       unsigned long flags;
        int cnt, cnt2;
        
        if (!tty) return count;
@@ -2150,8 +2475,8 @@ int scc_write(struct tty_struct *tty, int from_user, const unsigned char *buf, i
 
        if (scc->kiss.tx_inhibit) return count;
        
-       check_tx_queue(scc);
-
+       save_flags(flags); cli();
+       
        cnt2 = count;
        
        while (cnt2)
@@ -2160,23 +2485,40 @@ int scc_write(struct tty_struct *tty, int from_user, const unsigned char *buf, i
                cnt2 -= cnt;
                
                if (from_user)
-                       memcpy_fromfs(tbuf, buf, cnt);
+               {
+                       down(&scc_sem);
+                       memcpy_fromfs(scc_wbuf, buf, cnt);
+                       up(&scc_sem);
+               }
                else
-                       memcpy(tbuf, buf, cnt);
+                       memcpy(scc_wbuf, buf, cnt);
+               
+               /* Strange thing. The timeout of the slip driver is */
+               /* very small, thus we'll wake him up now.          */
+
+               if (cnt2 == 0)
+               {
+                       wake_up_interruptible(&tty->write_wait);
                
-               buf += cnt;
+                       if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
+                           tty->ldisc.write_wakeup)
+                               (tty->ldisc.write_wakeup)(tty);
+               } else 
+                       buf += cnt;
                        
-               p=tbuf;
+               p=scc_wbuf;
                
                while(cnt--) 
                  if (kiss_decode(scc, *p++))
                  {
                        scc->stat.nospace++;
+                       restore_flags(flags);
                        return 0;
-                 }
-                       
+                 }                     
        } /* while cnt2 */
-       
+
+       restore_flags(flags);
+               
        return count;
 }
                                
@@ -2204,7 +2546,6 @@ static void scc_flush_chars(struct tty_struct * tty)
        return; /* no flush needed */
 }
 
-/* the kernel does NOT use this routine yet... */
 
 static int scc_write_room(struct tty_struct *tty)
 {
@@ -2213,12 +2554,6 @@ static int scc_write_room(struct tty_struct *tty)
        if (scc_paranoia_check(scc, tty->device, "scc_write_room"))
                return 0;
        
-       if (scc->stat.tx_alloc >= QUEUE_THRES)
-       {
-               printk("scc_write_room(): buffer full (ignore)\n");
-               return 0;
-       }
-               
        return BUFSIZE;
 }
 
@@ -2226,8 +2561,8 @@ static int scc_chars_in_buffer(struct tty_struct *tty)
 {
        struct scc_channel *scc = tty->driver_data;
        
-       if (scc && scc->sndq2)
-               return scc->sndq2->cnt;
+       if (scc && scc->kiss_decode_bp)
+               return scc->kiss_decode_bp->cnt;
        else
                return 0;
 }
@@ -2235,12 +2570,9 @@ static int scc_chars_in_buffer(struct tty_struct *tty)
 static void scc_flush_buffer(struct tty_struct *tty)
 {
        struct scc_channel *scc = tty->driver_data;
-       
+
        if (scc_paranoia_check(scc, tty->device, "scc_flush_buffer"))
                return;
-               
-       scc->stat.tx_kiss_state = KISS_IDLE;
-       
        wake_up_interruptible(&tty->write_wait);
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
            tty->ldisc.write_wakeup)
@@ -2253,9 +2585,15 @@ static void scc_throttle(struct tty_struct *tty)
        
        if (scc_paranoia_check(scc, tty->device, "scc_throttle"))
                return;
-               
-               
-       /* dummy */
+
+#ifdef DEBUG           
+       printk("scc: scc_throttle() called for device %d\n", MINOR(tty->device));
+#endif
+       scc->throttled = 1;
+       
+       del_timer(&(scc->rx_t));
+       scc->rx_t.expires = jiffies + HZ/TPS;
+       add_timer(&scc->rx_t);
 }
 
 static void scc_unthrottle(struct tty_struct *tty)
@@ -2265,9 +2603,16 @@ static void scc_unthrottle(struct tty_struct *tty)
        if (scc_paranoia_check(scc, tty->device, "scc_unthrottle"))
                return;
                
-       /* dummy */
+#ifdef DEBUG
+       printk("scc: scc_unthrottle() called for device %d\n", MINOR(tty->device));
+#endif         
+       scc->throttled = 0;
+       del_timer(&(scc->rx_t));
+       scc_tx_timer(scc->rx_t.data);
 }
 
+/* experimental, the easiest way to stop output is a fake scc_throttle */
+
 static void scc_start(struct tty_struct *tty)
 {
        struct scc_channel *scc = tty->driver_data;
@@ -2275,9 +2620,8 @@ static void scc_start(struct tty_struct *tty)
        if (scc_paranoia_check(scc, tty->device, "scc_start"))
                return;
                
-       /* dummy */
+       scc_unthrottle(tty);
 }
-                                                       
 
 static void scc_stop(struct tty_struct *tty)
 {
@@ -2286,7 +2630,32 @@ static void scc_stop(struct tty_struct *tty)
        if (scc_paranoia_check(scc, tty->device, "scc_stop"))
                return;
                
-       /* dummy */
+       scc_throttle(tty);
+}
+
+static void
+scc_set_termios(struct tty_struct * tty, struct termios * old_termios)
+{
+       struct scc_channel *scc = tty->driver_data;
+       
+       if (scc_paranoia_check(scc, tty->device, "scc_set_termios"))
+               return;
+               
+       if (old_termios && (tty->termios->c_cflag == old_termios->c_cflag)) 
+               return;
+               
+       scc_change_speed(scc);
+}
+
+static void
+scc_set_ldisc(struct tty_struct * tty)
+{
+       struct scc_channel *scc = tty->driver_data;
+
+       if (scc_paranoia_check(scc, tty->device, "scc_set_ldisc"))
+               return;
+               
+       scc_change_speed(scc);
 }
 
 
@@ -2294,25 +2663,24 @@ static void scc_stop(struct tty_struct *tty)
 /* *                   Init SCC driver                               * */
 /* ******************************************************************** */
 
-int scc_init (void)
+int  scc_init (void)
 {
-       int chip, chan;
-       register io_port ctrl;
-       long flags;
-       
+       int chip, chan, k;
        
+       memset(&scc_std_termios, 0, sizeof(struct termios));
         memset(&scc_driver, 0, sizeof(struct tty_driver));
         scc_driver.magic = TTY_DRIVER_MAGIC;
-        scc_driver.name = "sc";
-        scc_driver.major = Z8530_MAJOR;                
+        scc_driver.name = "scc";
+        scc_driver.major = Z8530_MAJOR;
         scc_driver.minor_start = 0;
-        scc_driver.num = Nchips*2;
+        scc_driver.num = MAXSCC*2;
         scc_driver.type = TTY_DRIVER_TYPE_SERIAL;
-        scc_driver.subtype = 0;                        /* not needed */
-        scc_driver.init_termios = tty_std_termios;
-        scc_driver.init_termios.c_cflag = B9600        | CS8 | CREAD | HUPCL | CLOCAL;
+        scc_driver.subtype = 1;                        /* not needed */
+        scc_driver.init_termios = scc_std_termios;
+        scc_driver.init_termios.c_cflag = B9600  | CREAD | CS8 | HUPCL | CLOCAL;
+        scc_driver.init_termios.c_iflag = IGNBRK | IGNPAR;
         scc_driver.flags = TTY_DRIVER_REAL_RAW;
-        scc_driver.refcount = &scc_refcount;   /* not needed yet */
+        scc_driver.refcount = &scc_refcount;
         scc_driver.table = scc_table;
         scc_driver.termios = (struct termios **) scc_termios;
         scc_driver.termios_locked = (struct termios **) scc_termios_locked;
@@ -2333,86 +2701,91 @@ int scc_init (void)
         
         scc_driver.ioctl = scc_ioctl;
         scc_driver.set_termios = scc_set_termios;
+        scc_driver.set_ldisc = scc_set_ldisc;
+
+       printk(BANNER);
         
         if (tty_register_driver(&scc_driver))
-           panic("Couldn't register Z8530 SCC driver\n");
-                                
-       printk (BANNER);
+        {
+               printk("Failed to register Z8530 SCC driver\n");
+               return -EIO;
+       }
        
-       if (Nchips > MAXSCC) Nchips = MAXSCC;   /* to avoid the "DAU" (duemmster anzunehmender User) */
-
-       /* reset and pre-init all chips in the system */
+       /* pre-init channel information */
        
-       for (chip = 0; chip < Nchips; chip++)
+       for (chip = 0; chip < MAXSCC; chip++)
        {
                memset((char *) &SCC_Info[2*chip  ], 0, sizeof(struct scc_channel));
                memset((char *) &SCC_Info[2*chip+1], 0, sizeof(struct scc_channel));
                
-               ctrl = SCC_ctrl[chip * 2];
-               if (!ctrl) continue;
+               for (chan = 0; chan < 2; chan++)
+               {
+                       SCC_Info[2*chip+chan].magic    = SCC_MAGIC;
+                       SCC_Info[2*chip+chan].stat.rxbuffers = RXBUFFERS;
+                       SCC_Info[2*chip+chan].stat.txbuffers = TXBUFFERS;
+                       SCC_Info[2*chip+chan].stat.bufsize   = BUFSIZE;
+               }
+       }
+       
+       for (k = 0; k < 16; k++) Ivec[k].used = 0;
 
-               save_flags(flags); cli();       /* because of 2-step accesses */
-               
+       return 0;
+}
 
-/* Hmm... this may fail on fast systems with cards who don't delay the INTACK */
-/* If you are sure you specified the right port addresses and the driver doesn't */
-/* recognize the chips, define DONT_CHECK in scc_config.h */
+/* ******************************************************************** */
+/* *                       Module support                            * */
+/* ******************************************************************** */
 
-#ifndef DONT_CHECK
-               check_region(ctrl, 1);
 
-               Outb(ctrl, 0);
-               OutReg(ctrl,R13,0x55);          /* is this chip realy there? */
+#ifdef MODULE
+int init_module(void)
+{
+       int result = 0;
+       
+       result = scc_init();
+       
+       if (result == 0)
+               printk("Copyright 1993,1995 Joerg Reuter DL1BKE (jreuter@lykos.tng.oche.de)\n");
                
-               if (InReg(ctrl,R13) != 0x55 )
+       return result;
+}
+
+void cleanup_module(void)
+{
+       long flags;
+       io_port ctrl;
+       int k, errno;
+       struct scc_channel *scc;
+       
+       save_flags(flags); cli();
+       if ( (errno = tty_unregister_driver(&scc_driver)) )
+       {
+               printk("Failed to unregister Z8530 SCC driver (%d)", -errno);
+               restore_flags(flags);
+               return;
+       }
+       
+       for (k = 0; k < Nchips; k++)
+               if ( (ctrl = SCC_ctrl[k*2]) )
                {
-                       restore_flags(flags);
-                       continue;
+                       Outb(ctrl, 0);
+                       OutReg(ctrl,R9,FHWRES); /* force hardware reset */
+                       udelay(50);
                }
-#endif
                
-               SCC_Info[2*chip  ].magic    = SCC_MAGIC;
-               SCC_Info[2*chip  ].ctrl     = SCC_ctrl[2*chip];
-               SCC_Info[2*chip  ].data     = SCC_data[2*chip];
-               SCC_Info[2*chip  ].enhanced = SCC_Enhanced[chip];
-                       
-               SCC_Info[2*chip+1].magic    = SCC_MAGIC;
-               SCC_Info[2*chip+1].ctrl     = SCC_ctrl[2*chip+1];
-               SCC_Info[2*chip+1].data     = SCC_data[2*chip+1];
-               SCC_Info[2*chip+1].enhanced = SCC_Enhanced[chip];
-
-               restore_flags(flags);
-        }
-
-#ifdef DO_FAST_RX
-        rx_timer_cb.lock = 0;
-#else
-       rx_timer_cb.lock = 1;
-#endif
-        
-#ifdef VERBOSE_BOOTMSG
-       printk("Init Z8530 driver: %u channels, using irq %u\n",Nchips*2,Ivec);
-       
-
-       for (chan = 0; chan < Nchips * 2 ; chan++)
+       for (k = 0; k < Nchips*2; k++)
        {
-               printk("/dev/%s%i: data port = 0x%3.3x  control port = 0x%3.3x -- %s\n",
-                       scc_driver.name, chan, SCC_data[chan], SCC_ctrl[chan],
-                       SCC_Info[chan].ctrl? "found"   : "missing");
-                       
-               if (SCC_Info[chan].ctrl == 0) 
+               scc = &SCC_Info[k];
+               if (scc)
                {
-                       SCC_ctrl[chan] = 0;
-               } else {
-                       request_region(SCC_ctrl[chan], 1, "scc ctrl");
-                       request_region(SCC_data[chan], 1, "scc data");
+                       release_region(scc->ctrl, 1);
+                       release_region(scc->data, 1);
                }
        }
-#else
-       printk("Init Z8530 driver: %u channels\n",Nchips*2);
-#endif
-
        
-       return 0;
+       for (k=0; k < 16 ; k++)
+               if (Ivec[k].used) free_irq(k);
+               
+       restore_flags(flags);
 }
+#endif
diff --git a/drivers/char/scc_config.h b/drivers/char/scc_config.h
deleted file mode 100644 (file)
index 2788957..0000000
+++ /dev/null
@@ -1,69 +0,0 @@
-#include <linux/scc.h>
-
-/********* CONFIGURATION PARAMATERES; PLEASE CHANGE THIS TO YOUR OWN SITUATION **********/
-
-/* SCC hardware parameters */
-
-/* use the following board types: 
- *
- *     PA0HZP          OptoSCC (PA0HZP)
- *     EAGLE           EAGLE
- *     PC100           PC100 
- *     PRIMUS          PRIMUS-PC (DG9BL)
- *     DRSI            DRSI PC*Packet
- *     BAYCOM          BayCom (U)SCC
- *     
- */
-
-int     Nchips      = 2        ; /* number of chips */
-io_port Vector_Latch = 0       ; /* addr. of INTACK-Latch (0 for poll mode) */
-int     Ivec        = 7        ; /* interrupt vector */
-long    Clock       = 4915200  ; /* frequency of the scc clock */
-char   Board        = BAYCOM   ; /* what type of SCC card do you use? */
-int    Option       = 0        ; /* command for extra hardware */
-io_port Special_Port = 0       ; /* port address for special hardware */
-                                 /* (for EAGLE, PC100, PRIMUS, DRSI) */
-
-                       /*      ^  never remove the semicolon !! */
-                       
-
-
-/*                     Channel    A      B         Chip        */
-/*                              ============     ========      */
-/* Control ports:                                              */
-
-io_port SCC_ctrl[MAXSCC * 2] =         {0x304, 0x305,  /* ...one...    */
-                                0x306, 0x307,  /* ...two...    */
-                                    0,     0,  /* ...three...  */
-                                    0,     0}; /* ...four...   */
-
-/* Data ports:                                                 */
-
-io_port SCC_data[MAXSCC * 2] =  {0x300, 0x301, /* ...one...    */
-                                0x302, 0x303,  /* ...two...    */
-                                    0,     0,  /* ...three...  */
-                                    0,     0}; /* ...four...   */
-
-
-/* set to '1' if you have and want ESCC chip (8580/85180/85280) support */
-
-/*                                           Chip      */
-/*                                         ========    */
-int SCC_Enhanced[MAXSCC] =     {0,     /* ...one...    */
-                                0,     /* ...two...    */
-                                0,     /* ...three...  */
-                                0};    /* ...four...   */
-
-/* some useful #defines. You might need them or not */
-
-#define VERBOSE_BOOTMSG 1
-#undef  SCC_DELAY              /* perhaps a 486DX2 is a *bit* too fast */
-#undef  SCC_LDELAY             /* slow it even a bit more down */
-#undef  DONT_CHECK             /* don't look if the SCCs you specified are available */
-#define DO_FAST_RX             /* experimental timer routine called immediateley */
-                               /* after every received frame */
-
-
-/* The external clocking, nrz and fullduplex divider configuration is gone */
-/* you can set these parameters in /etc/z8530drv.rc and initialize the  */
-/* driver with sccinit */
index ec09cfec53aa835cd0ef978e6c6247bb41319e33..9a0c0b9e87ec7083668872e30c00a6e2585989f0 100644 (file)
@@ -475,6 +475,7 @@ el2_get_8390_hdr(struct device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
 {
     unsigned int i;
     unsigned long hdr_start = dev->mem_start + ((ring_page - EL2_MB1_START_PG)<<8);
+    unsigned long fifo_watchdog;
 
     if (dev->mem_start) {       /* Use the shared memory. */
 #ifdef notdef
@@ -492,7 +493,15 @@ el2_get_8390_hdr(struct device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
     outb_p((ei_status.interface_num == 0 ? ECNTRL_THIN : ECNTRL_AUI) | ECNTRL_INPUT
           | ECNTRL_START, E33G_CNTRL);
 
-    /* Header is less than 8 bytes, so we can ignore the FIFO. */
+    /* Header is < 8 bytes, so only check the FIFO at the beginning. */
+    fifo_watchdog = jiffies;
+    while ((inb(E33G_STATUS) & ESTAT_DPRDY) == 0) {
+       if (jiffies - fifo_watchdog > 2*HZ/100) {
+               printk("%s: FIFO blocked in el2_get_8390_hdr.\n", dev->name);
+               break;
+       }
+    }
+
     for(i = 0; i < sizeof(struct e8390_pkt_hdr); i++)
        ((char *)(hdr))[i] = inb_p(E33G_FIFOH);
 
index b51ab2b92b0c8106b6ba6ef9d44d72bdebaf49e1..c09c4a55ff014b94a9337a36926f89d8986c73d1 100644 (file)
@@ -10,7 +10,7 @@
  *             be here without 3C505 technical reference provided by
  *             3Com.
  *
- * Version:    @(#)3c505.c     0.8.3   12-Nov-95
+ * Version:    @(#)3c505.c     0.8.4   17-Dec-95
  *
  * Authors:    Linux 3c505 device driver by
  *                     Craig Southeren, <craigs@ineluki.apana.org.au>
@@ -103,7 +103,7 @@ static int elp_debug = 0;
  *  3 = messages when interrupts received
  */
 
-#define        ELP_VERSION     "0.8.3"
+#define        ELP_VERSION     "0.8.4"
 
 /*****************************************************************
  *
@@ -1284,9 +1284,13 @@ elp_sense (struct device * dev)
        int addr=dev->base_addr;
        const char *name=dev->name;
        long flags;
+       byte orig_HCR, orig_HSR;
 
-       byte orig_HCR=inb_control(addr),
-               orig_HSR=inb_status(addr);
+       if (check_region(addr, 0xf)) 
+         return -1;  
+
+       orig_HCR=inb_control(addr);
+       orig_HSR=inb_status(addr);
 
        if (elp_debug > 0)
                printk(search_msg, name, addr);
@@ -1472,6 +1476,7 @@ elplus_probe (struct device *dev)
        elp_init(dev);
        return 0;
 }
+
 #ifdef MODULE
 static char devicename[9] = { 0, };
 static struct device dev_3c505 = {
index b9482d7f93f9be2608b288ba0c551d3d9278c212..3075c4e4808bbfa1868bba94170fad55bbbdcc1d 100644 (file)
@@ -174,9 +174,12 @@ struct pci_dev_info dev_info[] = {
        DEVICE( ADAPTEC,        ADAPTEC_7871,   "AIC-7871"),
        DEVICE( ADAPTEC,        ADAPTEC_7872,   "AIC-7872"),
        DEVICE( ADAPTEC,        ADAPTEC_7873,   "AIC-7873"),
+       DEVICE( ADAPTEC,        ADAPTEC_7874,   "AIC-7874"),
        DEVICE( ADAPTEC,        ADAPTEC_7880,   "AIC-7880U"),
        DEVICE( ADAPTEC,        ADAPTEC_7881,   "AIC-7881U"),
        DEVICE( ADAPTEC,        ADAPTEC_7882,   "AIC-7882U"),
+       DEVICE( ADAPTEC,        ADAPTEC_7883,   "AIC-7883U"),
+       DEVICE( ADAPTEC,        ADAPTEC_7884,   "AIC-7884U"),
        DEVICE( ATRONICS,       ATRONICS_2015,  "IDE-2015PL"),
        DEVICE( HER,            HER_STING,      "Stingray"),
        DEVICE( HER,            HER_STINGARK,   "Stingray ARK 2000PV")
index 3756492585642c94368cf94f8df7fa12c8cc66df..cc80216b40b889c44117bc83bbfe76eeb4545ffb 100644 (file)
@@ -1,20 +1,69 @@
-/* 
- * Set these options for all host adapters.
- *     - Memory mapped IO does not work on x86 because of cache
- *       problems.
- *     - 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 subscribed to the ncr53c810@colorado.edu mailing list.  To
+ * subscribe, send mail to majordomo@colorado.edu with 
+ *
+ *     subscribe ncr53c810
+ * 
+ * in the text.
+ *
+ *
+ *   OPTION_NOASYNC
+ *     Don't negotiate for asynchronous transfers on the first command 
+ *     when OPTION_ALWAYS_SYNCHRONOUS is set.  Useful for dain bramaged
+ *     devices which do something bad rather than sending a MESSAGE 
+ *     REJECT back to us like they should if they can't cope.
+ *
+ *   OPTION_SYNCHRONOUS
+ *     Enable support for synchronous transfers.  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, 
+ *     since there are some dain bramaged SCSI devices which will accept
+ *     SDTR messages but keep talking asynchronously.
+ *
+ *   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.
+ * 
+ *  If you really want to run 10MHz FAST SCSI-II transfers, you should 
+ *  know that the NCR driver currently ignores parity information.  Most
+ *  systems do 5MHz SCSI fine.  I've seen a lot that have problems faster
+ *  than 8MHz.  To play it safe, we only request 5MHz transfers.
+ *
+ *  If you'd rather get 10MHz transfers, edit sdtr_message and change 
+ *  the fourth byte from 50 to 25.
  */
 
-#define SCSI_MALLOC
+#define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1|OPTION_DISCONNECT|\
+       OPTION_SYNCHRONOUS)
 
 /*
  * Sponsored by 
  * 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.
  * 
  * For more information, please consult 
  *
- *
- * NCR 53C700/53C700-66
- * SCSI I/O Processor
- * Data Manual
- *
- * NCR53C710 
+ * NCR53C810 
  * SCSI I/O Processor
  * Programmer's Guide
  *
  * NCR 53C810/53C820
  * PCI-SCSI I/O Processor Design In Guide
  *
- * NCR Microelectronics
- * 1635 Aeroplaza Drive
- * Colorado Springs, CO 80916
- * +1 (719) 578-3400
- *
- * Toll free literature number
- * +1 (800) 334-5454
- *
+ * For literature on Symbios Logic Inc. formerly NCR, SCSI, 
+ * and Communication products please call (800) 334-5454 or
+ * (719) 536-3300. 
+ * 
  * PCI BIOS Specification Revision
  * PCI Local Bus Specification
  * PCI System Design Guide
 /*
  * Design issues : 
  * The cumulative latency needed to propagate a read/write request 
- * through the filesystem, buffer cache, driver stacks, SCSI host, and 
+ * through the file system, buffer cache, driver stacks, SCSI host, and 
  * SCSI device is ultimately the limiting factor in throughput once we 
  * have a sufficiently fast host adapter.
  *  
  * So, to maximize performance we want to keep the ratio of latency to data 
  * transfer time to a minimum by
  * 1.  Minimizing the total number of commands sent (typical command latency
- *     including drive and busmastering host overhead is as high as 4.5ms)
+ *     including drive and bus mastering host overhead is as high as 4.5ms)
  *     to transfer a given amount of data.  
  *
  *      This is accomplished by placing no arbitrary limit on the number
  *     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.
  *     This driver allows a configurable number of commands to be enqueued
  *     for each target/lun combination (experimentally, I have discovered
  *     that two seems to work best) and will ultimately allow for 
- *     SCSI-II tagged queueing.
+ *     SCSI-II tagged queuing.
  *     
  *
  * 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.
+ * This driver is built around a Linux queue of commands waiting to 
+ * be executed, and a shared Linux/NCR array of commands to start.  Commands
+ * are transfered to the array  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
- * valid status, removes these commands from the list, calls 
- * the done routine, and flags their target/luns as not busy.
+ * valid status, removes these commands from a list of running commands, 
+ * calls the done routine, and flags their target/luns as not busy.
  *
  * Due to limitations in the intelligence of the NCR chips, certain
  * concessions are made.  In many cases, it is easier to dynamically 
- * generate/fixup code rather than calculate on the NCR at run time.  
+ * generate/fix-up code rather than calculate on the NCR at run time.  
  * So, code is generated or fixed up for
  *
  * - Handling data transfers, using a variable number of MOVE instructions
  *
  */
 
+/* 
+ * Accommodate differences between stock 1.2.x and 1.3.x asm-i386/types.h
+ * so lusers can drop in 53c7,8xx.* and get something which compiles 
+ * without warnings.
+ */
+
+#if !defined(LINUX_1_2) && !defined(LINUX_1_3)
+#include <linux/version.h>
+#if LINUX_VERSION_CODE > 65536 + 3 * 256
+#define LINUX_1_3
+#else
+#define LINUX_1_2
+#endif
+#endif
+
+#ifdef LINUX_1_2
+#define u32 bogus_u32
+#define s32 bogus_s32
+#include <asm/types.h>
+#undef u32
+#undef s32
+typedef __signed__ int s32;
+typedef unsigned int  u32;
+#endif /* def LINUX_1_2 */
+
+#ifdef MODULE
 #include <linux/module.h>
+#endif
 
 #include <asm/dma.h>
 #include <asm/io.h>
 #include <linux/pci.h>
 #include <linux/proc_fs.h>
 #include <linux/string.h>
+#include <linux/malloc.h>
 #include <linux/mm.h>
+#include <linux/ioport.h>
+#include <linux/time.h>
+#ifdef LINUX_1_2
+#include "../block/blk.h"
+#else
 #include <linux/blk.h>
+#endif
+#undef current
+
 #include "scsi.h"
 #include "hosts.h"
 #include "53c7,8xx.h"
 #include "constants.h"
 #include "sd.h"
-#include<linux/stat.h>
+#include <linux/stat.h>
+#include <linux/stddef.h>
 
+#ifndef LINUX_1_2
 struct proc_dir_entry proc_scsi_ncr53c7xx = {
     PROC_SCSI_NCR53C7xx, 9, "ncr53c7xx",
     S_IFDIR | S_IRUGO | S_IXUGO, 2
 };
+#endif
 
+static int check_address (unsigned long addr, int size);
+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 (unsigned long flags);
+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;
@@ -192,8 +282,9 @@ static int ncr_halt (struct Scsi_Host *host);
 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, u32 *dsa);
-static int print_insn (struct Scsi_Host *host, u32 *insn,
+static void print_dsa (struct Scsi_Host *host, u32 *dsa,
+    const char *prefix);
+static int print_insn (struct Scsi_Host *host, const u32 *insn,
     const char *prefix, int kernel);
 
 static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd);
@@ -202,39 +293,138 @@ static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
     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 :
+ * - There is some sort of conflict when the PPP driver is compiled with 
+ *     support for 16 channels?
+ * 
+ * - On systems which predate the 1.3.x initialization order change,
+ *      the NCR driver will cause Cannot get free page messages to appear.  
+ *      These are harmless, but I don't know of an easy way to avoid them.
+ *
+ * - 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.
+ *
+ */
+
+/* 
+ * Possible future direction of architecture for max performance :
+ *
+ * We're using a single start array 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.
+ *
+ * Potential for profiling : 
+ * Call do_gettimeofday(struct timeval *tv) to get 800ns resolution.
+ */
+
 
 /*
  * TODO : 
+ * 1.  To support WIDE transfers, not much needs to happen.  We
+ *     should do CHMOVE instructions instead of MOVEs when
+ *     we have scatter/gather segments of uneven length.  When
+ *     we do this, we need to handle the case where we disconnect
+ *     between segments.
+ * 
+ * 2.  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().
  *
- * 1.  Implement single step / trace code?
+ * 3.  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.
  * 
- * 2.  The initial code has been tested on the NCR53c810.  I don't 
+ *     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.
+ *
+ * 4.  MPEE bit of CTEST4 should be set so we get interrupted if 
+ *     we detect an error.
+ *
+ *  
+ * 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.
  *
- *     NCR53c720/710 - need to add fatal interrupt or GEN code for 
- *             command completion signaling.   Need to take care of 
- *             ADD WITH CARRY instructions since carry is unimplemented.
- *             Also need to modify all SDID, SCID, etc. registers,
- *             and table indirect select code since these use bit
- *             fielded (ie 1<<target) instead of binary encoded
- *             target ids.  Also, SCNTL3 is _not_ automatically
- *             programmed on selection, so we need to add more code.
+ *     NCR53c710 - need to add fatal interrupt or GEN code for 
+ *             command completion signaling.   Need to modify all 
+ *             SDID, SCID, etc. registers, and table indirect select code 
+ *             since these use bit fielded (ie 1<<target) instead of 
+ *             binary encoded target ids.  Need to accomodate
+ *             different register mappings, probably scan through
+ *             the SCRIPT code and change the non SFBR register operand
+ *             of all MOVE instructions.
  * 
  *     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.  
  *     
@@ -246,53 +436,59 @@ static Scsi_Host_Template *the_template = NULL;
  *     the right way, we need to provide options to reverse words
  *     when the scripts are relocated.
  *
- * 4.  Implement code to include page table entries for the 
- *     area occupied by memory mapped boards so we don't have 
- *     to use the potentially slower I/O accesses.
+ * 7.  Use vremap() to access memory mapped boards.  
  */
 
 /* 
- * XXX - note that my assembler was modified so that internally,
- * the names used can take a prefix, so that there is no conflict
- * between multiple copies of the same script assembled with 
- * different defines.
- *
- *
  * Allow for simultaneous existence of multiple SCSI scripts so we 
  * can have a single driver binary for all of the family.
  *
  * - one for NCR53c700 and NCR53c700-66 chips  (not yet supported)
- * - one for NCR53c710 and NCR53c720 chips     (not yet supported)
- * - one for NCR53c810 and NCR53c820 chips     (only the NCR53c810 is
- *     currently supported)
- *
- * For the very similar chips, we should probably hack the fixup code
- * and interrupt code so that it works everywhere, but I suspect the 
- * NCR53c700 is going to need it's own fixup routine.
+ * - one for rest (only the NCR53c810, 815, 820, and 825 are currently 
+ *     supported)
+ * 
+ * So that we only need two SCSI scripts, we need to modify things so
+ * that we fixup register accesses in READ/WRITE instructions, and 
+ * we'll also have to accomodate the bit vs. binary encoding of IDs
+ * with the 7xx chips.
  */
 
 /*
- * 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;
+/* 
+ * The revision field of the PCI_CLASS_REVISION register is compared 
+ * against each of these fields if the field is not -1.  If it
+ * is less than min_revision or larger than max_revision, a warning
+ * message is printed.
+ */
     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}
 };
 
 #define NPCI_CHIP_IDS (sizeof (pci_chip_ids) / sizeof(pci_chip_ids[0]))
 
+#define ROUNDUP(adr,type)      \
+  ((void *) (((long) (adr) + sizeof(type) - 1) & ~(sizeof(type) - 1)))
 
-/* 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 */
@@ -311,7 +507,7 @@ static struct override {
            int function;
        } pci;
     } data;
-    int options;
+    long long options;
 } overrides [4] = {{0,},};
 static int commandline_current = 0;
 static int no_overrides = 0;
@@ -322,6 +518,43 @@ static int no_overrides = 0;
 #define OVERRIDE_LIMIT commandline_current
 #endif
 
+/*
+ * Function: issue_to_cmd
+ *
+ * Purpose: convert jump instruction in issue array to NCR53c7x0_cmd
+ *     structure pointer.  
+ *
+ * Inputs; issue - pointer to start of NOP or JUMP instruction
+ *     in issue array.
+ *
+ * Returns: pointer to command on success; 0 if opcode is NOP.
+ */
+
+static inline struct NCR53c7x0_cmd *
+issue_to_cmd (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
+    u32 *issue)
+{
+    return (issue[0] != hostdata->NOP_insn) ? 
+    /* 
+     * If the IF TRUE bit is set, it's a JUMP instruction.  The
+     * operand is a bus pointer to the dsa_begin routine for this DSA.  The
+     * dsa field of the NCR53c7x0_cmd structure starts with the 
+     * DSA code template.  By converting to a virtual address,
+     * subtracting the code template size, and offset of the 
+     * dsa field, we end up with a pointer to the start of the 
+     * structure (alternatively, we could use the 
+     * dsa_cmnd field, an anachronism from when we weren't
+     * sure what the relationship between the NCR structures
+     * and host structures were going to be.
+     */
+       (struct NCR53c7x0_cmd *) ((char *) bus_to_virt (issue[1]) - 
+           (hostdata->E_dsa_code_begin - hostdata->E_dsa_code_template) -
+           offsetof(struct NCR53c7x0_cmd, dsa)) 
+    /* If the IF TRUE bit is not set, it's a NOP */
+       : NULL;
+}
+
+
 /*
  * Function : static internal_setup(int board, int chip, char *str, int *ints)
  *
@@ -333,7 +566,8 @@ static int no_overrides = 0;
  * 
  */
 
-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 */
 
@@ -353,12 +587,14 @@ static void internal_setup(int board, int chip, char *str, int *ints) {
            overrides[commandline_current].data.normal.irq = ints[3];
            overrides[commandline_current].data.normal.dma = (ints[0] >= 4) ?
                ints[4] : DMA_NONE;
+           /* FIXME: options is now a long long */
            overrides[commandline_current].options = (ints[0] >= 5) ?
                ints[5] : 0;
        } else {
            overrides[commandline_current].data.pci.bus = ints[1];
            overrides[commandline_current].data.pci.device = ints[2];
            overrides[commandline_current].data.pci.function = ints[3];
+           /* FIXME: options is now a long long */
            overrides[commandline_current].options = (ints[0] >= 4) ?
                ints[4] : 0;
        }
@@ -391,32 +627,272 @@ setup_wrapper(815)
 setup_wrapper(820)
 setup_wrapper(825)
 
+/* 
+ * FIXME: we should junk these, in favor of synchronous_want and 
+ * wide_want in the NCR53c7x0_hostdata structure.
+ */
+
+/* Template for "preferred" synchronous transfer parameters. */
+
+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 */
+};
+
+/* Template for "preferred" WIDE transfer parameters */
+
+static const unsigned char wdtr_message[] = {
+    EXTENDED_MESSAGE, 2 /* length */, EXTENDED_WDTR, 1 /* 2^1 bytes */
+};
+
+/*
+ * 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) {
+       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;
+    } 
+#ifndef LINUX_1_2
+    else if (target > h->max_id) {
+       printk (KERN_ALERT "target %d exceeds maximum of %d\n", target,
+           h->max_id);
+       return -1;
+    }
+#endif
+    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;
+    u32 *current;
+    for (i = 0; i < 16; ++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;
+    for (i = 0, current = (u32 *) hostdata->schedule; 
+       i < host->can_queue; ++i, current += 2) {
+       current[0] = hostdata->NOP_insn;
+       current[1] = 0xdeadbeef;
+    }
+    current[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) | DBC_TCI_TRUE;
+    current[1] = (u32) virt_to_bus (hostdata->script) +
+       hostdata->E_wait_reselect;
+    hostdata->reconnect_dsa_head = 0;
+    hostdata->addr_reconnect_dsa_head = (u32) 
+       virt_to_bus((void *) &(hostdata->reconnect_dsa_head));
+    hostdata->expecting_iid = 0;
+    hostdata->expecting_sto = 0;
+    if (hostdata->options & OPTION_ALWAYS_SYNCHRONOUS) 
+       hostdata->initiate_sdtr = 0xffff; 
+    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 is 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) {
-    case 810:
-    case 815:
     case 820:
     case 825:
+#ifdef notyet
+       host->max_id = 15;
+#endif
+       /* Fall through */
+    case 810:
+    case 815:
        hostdata->dstat_sir_intr = NCR53c8x0_dstat_sir_intr;
        hostdata->init_save_regs = NULL;
        hostdata->dsa_fixup = NCR53c8xx_dsa_fixup;
@@ -424,7 +900,8 @@ NCR53c7x0_init (struct Scsi_Host *host) {
        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",
@@ -438,30 +915,14 @@ NCR53c7x0_init (struct Scsi_Host *host) {
     hostdata->NCR53c7xx_msg_reject = MESSAGE_REJECT;
     hostdata->NCR53c7xx_msg_abort = ABORT;
     hostdata->NCR53c7xx_msg_nop = NOP;
+    hostdata->NOP_insn = (DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24;
 
-    /*
-     * 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;
@@ -484,12 +945,26 @@ NCR53c7x0_init (struct Scsi_Host *host) {
     tmp = hostdata->this_id_mask = NCR53c7x0_read8(SCID_REG);
     for (host->this_id = 0; tmp != 1; tmp >>=1, ++host->this_id);
 #else
-    host->this_id = NCR53c7x0_read8(SCID_REG) & 7;
+    host->this_id = NCR53c7x0_read8(SCID_REG) & 15;
     hostdata->this_id_mask = 1 << host->this_id;
 #endif
 
-    printk("scsi%d : using initiator ID %d\n", host->host_no,
-       host->this_id);
+/*
+ * 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 %d, changing to 7\n",
+           host->host_no, host->this_id);
+       host->this_id = 7;
+       hostdata->this_id_mask = 1 << 7;
+       uninitialized = 1;
+    };
+
+    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.
@@ -513,12 +988,17 @@ NCR53c7x0_init (struct Scsi_Host *host) {
      * 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,
@@ -528,7 +1008,7 @@ NCR53c7x0_init (struct Scsi_Host *host) {
 
     /* 
      * 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) {
@@ -540,7 +1020,7 @@ NCR53c7x0_init (struct Scsi_Host *host) {
            case DMODE_BL_4: i = 4; break;
            case DMODE_BL_8: i = 8; break;
            case DMODE_BL_16: i = 16; break;
-            default: i = 0;
+            default: i = 0;
            }
            printk ("scsi%d : burst length %d\n", host->host_no, i);
        }
@@ -551,8 +1031,18 @@ NCR53c7x0_init (struct Scsi_Host *host) {
      * 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;
 
@@ -560,63 +1050,24 @@ NCR53c7x0_init (struct Scsi_Host *host) {
      * 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.
-     */
-
-    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; 
-       }
-
-    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.
+     * upon period.
      */
 
-    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:
-           }
-       }
+    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; 
     }
 
-    hostdata->issue_queue = hostdata->running_list = 
-       hostdata->finished_queue = NULL;
-    hostdata->issue_dsa_head = 0;
-    hostdata->issue_dsa_tail = NULL;
+    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 < 16; ++i) 
+       hostdata->cmd_allocated[i] = 0;
 
     if (hostdata->init_save_regs)
        hostdata->init_save_regs (host);
@@ -628,8 +1079,6 @@ NCR53c7x0_init (struct Scsi_Host *host) {
        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
@@ -642,25 +1091,67 @@ NCR53c7x0_init (struct Scsi_Host *host) {
 
     hostdata->soft_reset (host);
 
+#if 1
     hostdata->debug_count_limit = -1;
+#else
+    hostdata->debug_count_limit = 1;
+#endif
     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));
 
-    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;
-}
+    NCR53c7x0_driver_init (host);
 
-/* 
- * Function : static int normal_init(Scsi_Host_Template *tpnt, int board, 
- *     int chip, int base, int io_port, int irq, int dma, int pcivalid,
+    /*
+     * 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 {
+       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;
+}
+
+/* 
+ * Function : static int normal_init(Scsi_Host_Template *tpnt, int board, 
+ *     int chip, u32 base, int io_port, int irq, int dma, int pcivalid,
  *     unsigned char pci_bus, unsigned char pci_device_fn,
- *     int options);
+ *     long long options);
  *
  * Purpose : initializes a NCR53c7,8x0 based on base addresses,
  *     IRQ, and DMA channel.   
@@ -677,16 +1168,16 @@ NCR53c7x0_init (struct Scsi_Host *host) {
  *
  */
 
-static int normal_init (Scsi_Host_Template *tpnt, int board, int chip, 
+static int 
+normal_init (Scsi_Host_Template *tpnt, int board, int chip, 
     u32 base, int io_port, int irq, int dma, int pci_valid, 
-    unsigned char pci_bus, unsigned char pci_device_fn, int options) {
+    unsigned char pci_bus, unsigned char pci_device_fn, long long options) {
     struct Scsi_Host *instance;
     struct NCR53c7x0_hostdata *hostdata;
     char chip_str[80];
-    int script_len = 0, dsa_len = 0, size = 0, max_cmd_size = 0;
-    int ok = 0;
+    int script_len = 0, dsa_len = 0, size = 0, max_cmd_size = 0
+       schedule_size = 0, ok = 0;
 
-    
     options |= perm_options;
 
     switch (chip) {
@@ -694,6 +1185,7 @@ static int normal_init (Scsi_Host_Template *tpnt, int board, int chip,
     case 820:
     case 815:
     case 810:
+       schedule_size = (tpnt->can_queue + 1) * 8 /* JUMP instruction size */;
        script_len = NCR53c8xx_script_len;
        dsa_len = NCR53c8xx_dsa_len;
        options |= OPTION_INTFLY;
@@ -705,7 +1197,7 @@ static int normal_init (Scsi_Host_Template *tpnt, int board, int chip,
     }
 
     printk("scsi-ncr53c7,8xx : %s at memory 0x%x, io 0x%x, irq %d",
-       chip_str, base, io_port, irq);
+       chip_str, (unsigned) base, io_port, irq);
     if (dma == DMA_NONE)
        printk("\n");
     else 
@@ -726,13 +1218,17 @@ static int normal_init (Scsi_Host_Template *tpnt, int board, int chip,
        /* Size of dynamic part of command structure : */
        2 * /* Worst case : we don't know if we need DATA IN or DATA out */
                ( 2 * /* Current instructions per scatter/gather segment */ 
-                 tpnt->sg_tablesize + 
-                 3 /* Current startup / termination required per phase */
+                 tpnt->sg_tablesize + 
+                  3 /* Current startup / termination required per phase */
                ) *
-       8 /* Each instruction is eight bytes */;
+       8 /* Each instruction is eight bytes */
+       + (sizeof(void *) - sizeof(u32)); /* to ensure proper alignment */
     /* Note that alignment will be guaranteed, since we put the command
        allocated at probe time after the fixed-up SCSI script, which 
-       consists of 32 bit words, aligned on a 32 bit boundary. */ 
+       consists of 32 bit words, aligned on a 32 bit boundary.  But
+       on a 64bit machine we need 8 byte alignment for hostdata->free, so
+       we add in another 4 bytes to take care of potential misalignment
+       */
 
     /* Allocate fixed part of hostdata, dynamic part to hold appropriate
        SCSI SCRIPT(tm) plus a single, maximum-sized NCR53c7x0_cmd structure.
@@ -755,13 +1251,13 @@ static int normal_init (Scsi_Host_Template *tpnt, int board, int chip,
         after all device driver initialization).
     */
 
-    size = sizeof(struct NCR53c7x0_hostdata) + script_len + max_cmd_size;
+    size = sizeof(struct NCR53c7x0_hostdata) + script_len + max_cmd_size +
+       schedule_size;
 
     instance = scsi_register (tpnt, size);
     if (!instance)
        return -1;
 
-
     /* FIXME : if we ever support an ISA NCR53c7xx based board, we
        need to check if the chip is running in a 16 bit mode, and if so 
        unregister it if it is past the 16M (0x1000000) mark */
@@ -793,7 +1289,7 @@ static int normal_init (Scsi_Host_Template *tpnt, int board, int chip,
      */
 
     if (base) {
-       instance->base = (unsigned char*) (unsigned long) base;
+       instance->base = (unsigned char *) (unsigned long) base;
        /* Check for forced I/O mapping */
        if (!(options & OPTION_IO_MAPPED)) {
            options |= OPTION_MEMORY_MAPPED;
@@ -821,25 +1317,53 @@ static int normal_init (Scsi_Host_Template *tpnt, int board, int chip,
     instance->dma_channel = dma;
 
     hostdata->options = options;
-    hostdata->dsa_size = dsa_len;
+    hostdata->dsa_len = dsa_len;
     hostdata->max_cmd_size = max_cmd_size;
     hostdata->num_cmds = 1;
     /* Initialize single command */
     hostdata->free = (struct NCR53c7x0_cmd *) 
        (hostdata->script + hostdata->script_count);
+    hostdata->free = ROUNDUP(hostdata->free, void *);
     hostdata->free->real = (void *) hostdata->free;
     hostdata->free->size = max_cmd_size;
     hostdata->free->free = NULL;
     hostdata->free->next = NULL;
+    hostdata->extra_allocate = 0;
+
+    /* Allocate command start code space */
+    hostdata->schedule = (chip == 700 || chip == 70066) ?
+       NULL : (u32 *) ((char *)hostdata->free + max_cmd_size);
 
+/* 
+ * 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);
 }
 
 
 /* 
- * Function : static int pci_init(Scsi_Host_Template *tpnt, int board, 
- *     int chip, int bus, int device_fn, int options)
+ * Function : static int ncr_pci_init(Scsi_Host_Template *tpnt, int board, 
+ *     int chip, int bus, int device_fn, long long options)
  *
  * Purpose : initializes a NCR53c800 family based on the PCI
  *     bus, device, and function location of it.  Allows 
@@ -858,11 +1382,16 @@ static int normal_init (Scsi_Host_Template *tpnt, int board, int chip,
  *
  */
 
-static int ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip, 
-    unsigned char bus, unsigned char device_fn, int options) {
+static int 
+ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip, 
+    unsigned char bus, unsigned char device_fn, long long options) {
     unsigned short vendor_id, device_id, command;
-    u32 base;
-    int io_port; 
+#ifdef LINUX_1_2
+    unsigned long
+#else
+    unsigned int 
+#endif
+       base, io_port; 
     unsigned char irq, revision;
     int error, expected_chip;
     int expected_id = -1, max_revision = -1, min_revision = -1;
@@ -872,7 +1401,7 @@ static int ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip,
        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;
@@ -885,15 +1414,15 @@ static int ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip,
        (error = pcibios_read_config_word (bus, device_fn, PCI_COMMAND, 
            &command)) ||
        (error = pcibios_read_config_dword (bus, device_fn, 
-           PCI_BASE_ADDRESS_0, (int *) &io_port)) || 
+           PCI_BASE_ADDRESS_0, &io_port)) || 
        (error = pcibios_read_config_dword (bus, device_fn, 
-           PCI_BASE_ADDRESS_1, (int *) &base)) ||
+           PCI_BASE_ADDRESS_1, &base)) ||
        (error = pcibios_read_config_byte (bus, device_fn, PCI_CLASS_REVISION,
            &revision)) ||
        (error = pcibios_read_config_byte (bus, device_fn, PCI_INTERRUPT_LINE,
            &irq))) {
        printk ("scsi-ncr53c7,8xx : error %s not initializing due to error reading configuration space\n"
-               "        perhaps you specified an incorrect PCI bus, device, or function.\n"
+               "        perhaps you specified an incorrect PCI bus, device, or function.\n"
                , pcibios_strerror(error));
        return -1;
     }
@@ -916,12 +1445,13 @@ static int ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip,
     if (command & PCI_COMMAND_IO) { 
        if ((io_port & 3) != 1) {
            printk ("scsi-ncr53c7,8xx : disabling I/O mapping since base address 0 (0x%x)\n"
-                   "        bits 0..1 indicate a non-IO mapping\n", io_port);
+                   "        bits 0..1 indicate a non-IO mapping\n",
+               (unsigned) io_port);
            io_port = 0;
        } else
            io_port &= PCI_BASE_ADDRESS_IO_MASK;
     } else {
-           io_port = 0;
+       io_port = 0;
     }
 
     if (command & PCI_COMMAND_MEMORY) {
@@ -932,7 +1462,7 @@ static int ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip,
        } else 
            base &= PCI_BASE_ADDRESS_MEM_MASK;
     } else {
-           base = 0;
+       base = 0;
     }
        
     if (!io_port && !base) {
@@ -957,7 +1487,7 @@ static int ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip,
 
     if (chip && device_id != expected_id) 
        printk ("scsi-ncr53c7,8xx : warning : device id of 0x%04x doesn't\n"
-               "                   match expected 0x%04x\n",
+                "                   match expected 0x%04x\n",
            (unsigned int) device_id, (unsigned int) expected_id );
     
     if (max_revision != -1 && revision > max_revision) 
@@ -967,6 +1497,12 @@ static int ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip,
        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%x to 0x%x is in use\n",
+           (unsigned) io_port, (unsigned) io_port + 127);
+       return -1;
+    }
+
     return normal_init (tpnt, board, chip, (int) base, io_port, 
        (int) irq, DMA_NONE, 1, bus, device_fn, options);
 }
@@ -985,14 +1521,17 @@ static int ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip,
  *
  */
 
-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 */
     unsigned char pci_bus, pci_device_fn;
     static short pci_index=0;  /* Device index to PCI BIOS calls */
 
+#ifndef LINUX_1_2
     tpnt->proc_dir = &proc_scsi_ncr53c7xx;
+#endif
 
     for (current_override = count = 0; current_override < OVERRIDE_LIMIT; 
         ++current_override) {
@@ -1021,10 +1560,11 @@ int NCR53c7xx_detect(Scsi_Host_Template *tpnt) {
            for (pci_index = 0;
                !pcibios_find_device (PCI_VENDOR_ID_NCR, 
                    pci_chip_ids[i].pci_device_id, pci_index, &pci_bus, 
-                   &pci_device_fn) && 
-               !ncr_pci_init (tpnt, BOARD_GENERIC, pci_chip_ids[i].chip, 
-                   pci_bus, pci_device_fn, /* no options */ 0); 
-               ++count, ++pci_index);
+                   &pci_device_fn); 
+               ++pci_index)
+               if (!ncr_pci_init (tpnt, BOARD_GENERIC, pci_chip_ids[i].chip, 
+                   pci_bus, pci_device_fn, /* no options */ 0))
+                   ++count;
     }
     return count;
 }
@@ -1032,6 +1572,9 @@ int NCR53c7xx_detect(Scsi_Host_Template *tpnt) {
 /* 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;
 
@@ -1050,45 +1593,55 @@ NCR53c8x0_init_fixup (struct Scsi_Host *host) {
     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;
     u32 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, 
        sizeof(SCRIPT));
     /* Fixup labels */
     for (i = 0; i < PATCHES; ++i) 
-       hostdata->script[LABELPATCHES[i]] +=
-           virt_to_bus(hostdata->script);
+       hostdata->script[LABELPATCHES[i]] += 
+           virt_to_bus(hostdata->script);
     /* Fixup addresses of constants that used to be EXTERNAL */
 
     patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_abort, 
-       virt_to_bus(&hostdata->NCR53c7xx_msg_abort));
+       virt_to_bus(&(hostdata->NCR53c7xx_msg_abort)));
     patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_reject, 
-       virt_to_bus(&hostdata->NCR53c7xx_msg_reject));
+       virt_to_bus(&(hostdata->NCR53c7xx_msg_reject)));
     patch_abs_32 (hostdata->script, 0, NCR53c7xx_zero, 
-       virt_to_bus(&hostdata->NCR53c7xx_zero));
+       virt_to_bus(&(hostdata->NCR53c7xx_zero)));
     patch_abs_32 (hostdata->script, 0, NCR53c7xx_sink, 
-       virt_to_bus(&hostdata->NCR53c7xx_sink));
+       virt_to_bus(&(hostdata->NCR53c7xx_sink)));
+    patch_abs_32 (hostdata->script, 0, NOP_insn,
+       virt_to_bus(&(hostdata->NOP_insn)));
+    patch_abs_32 (hostdata->script, 0, schedule,
+       virt_to_bus((void *) hostdata->schedule));
 
     /* Fixup references to external variables: */
     for (i = 0; i < EXTERNAL_PATCHES_LEN; ++i)
-       hostdata->script[EXTERNAL_PATCHES[i].offset] +=
-         virt_to_bus(EXTERNAL_PATCHES[i].address);
+       hostdata->script[EXTERNAL_PATCHES[i].offset] +=
+         virt_to_bus(EXTERNAL_PATCHES[i].address);
 
     /* 
      * 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 
@@ -1102,14 +1655,12 @@ NCR53c8x0_init_fixup (struct Scsi_Host *host) {
        base = (u32) 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 = virt_to_phys(host->base);
-       ncr_to_ncr = memory_to_ncr = ncr_to_memory = tmp;
+       base = virt_to_bus(host->base);
+       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);
 
     /*
@@ -1119,49 +1670,84 @@ NCR53c8x0_init_fixup (struct Scsi_Host *host) {
      * hostdata structure rather than in the RELATIVE area of the 
      * SCRIPTS.
      */
-
+    
 
     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,
-                 virt_to_bus((void*)&hostdata->issue_dsa_head));
-    patch_abs_32 (hostdata->script, 0, msg_buf,
-                 virt_to_bus((void*)&hostdata->msg_buf));
-    patch_abs_32 (hostdata->script, 0, reconnect_dsa_head,
-                 virt_to_bus((void*)&hostdata->reconnect_dsa_head));
-    patch_abs_32 (hostdata->script, 0, reselected_identify,
-                 virt_to_bus((void*)&hostdata->reselected_identify));
-    patch_abs_32 (hostdata->script, 0, reselected_tag,
-                 virt_to_bus((void*)&hostdata->reselected_tag));
+    patch_abs_32 (hostdata->script, 0, msg_buf, 
+       virt_to_bus((void *)&(hostdata->msg_buf)));
+    patch_abs_32 (hostdata->script, 0, reconnect_dsa_head, 
+       virt_to_bus((void *)&(hostdata->reconnect_dsa_head)));
+    patch_abs_32 (hostdata->script, 0, addr_reconnect_dsa_head, 
+       virt_to_bus((void *)&(hostdata->addr_reconnect_dsa_head)));
+    patch_abs_32 (hostdata->script, 0, reselected_identify, 
+       virt_to_bus((void *)&(hostdata->reselected_identify)));
+/* reselected_tag is currently unused */
+#if 0
+    patch_abs_32 (hostdata->script, 0, reselected_tag, 
+       virt_to_bus((void *)&(hostdata->reselected_tag)));
+#endif
+
+    patch_abs_32 (hostdata->script, 0, test_dest, 
+       virt_to_bus((void*)&hostdata->test_dest));
+    patch_abs_32 (hostdata->script, 0, test_src, 
+       virt_to_bus(&hostdata->test_source));
 
-    patch_abs_32 (hostdata->script, 0, test_dest,
-                 virt_to_bus((void*)&hostdata->test_dest));
-    patch_abs_32 (hostdata->script, 0, test_src, virt_to_bus(&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, (u32) EVENT_SELECT);
+#endif
+#ifdef A_int_EVENT_DISCONNECT
+   patch_abs_32 (hostdata->script, 0, int_EVENT_DISCONNECT, (u32) EVENT_DISCONNECT);
+#endif
+#ifdef A_int_EVENT_RESELECT
+   patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT, (u32) EVENT_RESELECT);
+#endif
+#ifdef A_int_EVENT_COMPLETE
+   patch_abs_32 (hostdata->script, 0, int_EVENT_COMPLETE, (u32) EVENT_COMPLETE);
+#endif
+#ifdef A_int_EVENT_IDLE
+   patch_abs_32 (hostdata->script, 0, int_EVENT_IDLE, (u32) EVENT_IDLE);
+#endif
+#ifdef A_int_EVENT_SELECT_FAILED
+   patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT_FAILED, 
+       (u32) EVENT_SELECT_FAILED);
+#endif
+#ifdef A_int_EVENT_BEFORE_SELECT
+   patch_abs_32 (hostdata->script, 0, int_EVENT_BEFORE_SELECT,
+       (u32) EVENT_BEFORE_SELECT);
+#endif
+#ifdef A_int_EVENT_RESELECT_FAILED
+   patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT_FAILED, 
+       (u32) EVENT_RESELECT_FAILED);
+#endif
 
     /*
      * Make sure the NCR and Linux code agree on the location of 
      * certain fields.
      */
 
-/* 
- * XXX - for cleanness, E_* fields should be type u32 *
- * and should reflect the _relocated_ addresses.  Change this.
- */
     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;                       
     hostdata->E_select = Ent_select;
     hostdata->E_select_msgout = Ent_select_msgout;
     hostdata->E_target_abort = Ent_target_abort;
@@ -1173,6 +1759,8 @@ NCR53c8x0_init_fixup (struct Scsi_Host *host) {
 #ifdef Ent_test_3
     hostdata->E_test_3 = Ent_test_3;
 #endif
+    hostdata->E_wait_reselect = Ent_wait_reselect;
+    hostdata->E_dsa_code_begin = Ent_dsa_code_begin;
 
     hostdata->dsa_cmdout = A_dsa_cmdout;
     hostdata->dsa_cmnd = A_dsa_cmnd;
@@ -1186,6 +1774,8 @@ NCR53c8x0_init_fixup (struct Scsi_Host *host) {
     hostdata->dsa_select = A_dsa_select;
     hostdata->dsa_start = Ent_dsa_code_template - Ent_dsa_zero;
     hostdata->dsa_status = A_dsa_status;
+    hostdata->dsa_jump_dest = Ent_dsa_code_fix_jump - Ent_dsa_zero + 
+       8 /* destination operand */;
 
     /* sanity check */
     if (A_dsa_fields_start != Ent_dsa_code_template_end - 
@@ -1194,8 +1784,8 @@ NCR53c8x0_init_fixup (struct Scsi_Host *host) {
            host->host_no, A_dsa_fields_start, Ent_dsa_code_template_end - 
            Ent_dsa_zero);
 
-    printk("scsi%d : NCR code relocated to 0x%p\n", host->host_no,
-       hostdata->script);
+    printk("scsi%d : NCR code relocated to 0x%lx (virt 0x%p)\n", host->host_no,
+       virt_to_bus(hostdata->script), hostdata->script);
 }
 
 /*
@@ -1213,7 +1803,8 @@ NCR53c8x0_init_fixup (struct Scsi_Host *host) {
  * 
  */
 
-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;
@@ -1238,27 +1829,28 @@ static int NCR53c8xx_run_tests (struct Scsi_Host *host) {
      * autoprobe routine.
      */
 
-    if (hostdata->issue_dsa_head) {
-       printk ("scsi%d : hostdata->issue_dsa_head corrupt before test 1\n",
-           host->host_no);
-       hostdata->issue_dsa_head = 0;
-    }
-       
-    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;
        hostdata->test_dest = 0;
        hostdata->test_source = 0xdeadbeef;
-       start = virt_to_bus(hostdata->script) + hostdata->E_test_1;
+       start = virt_to_bus (hostdata->script) + hostdata->E_test_1;
        hostdata->state = STATE_RUNNING;
        printk ("scsi%d : test 1", host->host_no);
        NCR53c7x0_write32 (DSP_REG, start);
-       mb();
        printk (" started\n");
        sti();
 
-       timeout = jiffies + 5 * HZ / 10;        /* arbitrary */
+       /* 
+        * This is currently a .5 second timeout, since (in theory) no slow 
+        * board will take that long.  In practice, we've seen one 
+        * pentium which ocassionally fails with this, but works with 
+        * 10 times as much?
+        */
+
+       timeout = jiffies + 5 * HZ / 10;
        while ((hostdata->test_completed == -1) && jiffies < timeout)
                barrier();
 
@@ -1268,28 +1860,28 @@ static int NCR53c8xx_run_tests (struct Scsi_Host *host) {
                (hostdata->test_dest == 0xdeadbeef) ? 
                    " due to lost interrupt.\n"
                    "         Please verify that the correct IRQ is being used for your board,\n"
-                   "         and that the motherboard IRQ jumpering matches the PCI setup on\n"
+                   "         and that the motherboard IRQ jumpering matches the PCI setup on\n"
                    "         PCI systems.\n"
                    "         If you are using a NCR53c810 board in a PCI system, you should\n" 
                    "         also verify that the board is jumpered to use PCI INTA, since\n"
                    "         most PCI motherboards lack support for INTB, INTC, and INTD.\n"
                    : "");
        else if (hostdata->test_completed != 1) 
-           printk ("scsi%d : test 1 bad interrupt value (%d)\n", host->host_no,
-               hostdata->test_completed);
+           printk ("scsi%d : test 1 bad interrupt value (%d)\n", 
+               host->host_no, hostdata->test_completed);
        else 
            failed = (hostdata->test_dest != 0xdeadbeef);
 
        if (hostdata->test_dest != 0xdeadbeef) {
            printk ("scsi%d : driver test 1 read 0x%x instead of 0xdeadbeef indicating a\n"
-                   "        probable cache invalidation problem.  Please configure caching\n"
-                   "        as write-through or disabled\n",
+                    "         probable cache invalidation problem.  Please configure caching\n"
+                   "         as write-through or disabled\n",
                host->host_no, hostdata->test_dest);
        }
 
        if (failed) {
-           printk ("scsi%d : DSP = 0x%x (script at 0x%p, start at 0x%x)\n",
-               host->host_no, NCR53c7x0_read32(DSP_REG),
+           printk ("scsi%d : DSP = 0x%p (script at 0x%px, start at 0x%x)\n",
+               host->host_no, bus_to_virt(NCR53c7x0_read32(DSP_REG)),
                hostdata->script, start);
            printk ("scsi%d : DSPS = 0x%x\n", host->host_no,
                NCR53c7x0_read32(DSPS_REG));
@@ -1299,13 +1891,8 @@ static int NCR53c8xx_run_tests (struct Scsi_Host *host) {
        hostdata->test_running = 0;
     }
 
-    if (hostdata->issue_dsa_head) {
-       printk ("scsi%d : hostdata->issue_dsa_head corrupt after test 1\n",
-           host->host_no);
-       hostdata->issue_dsa_head = 0;
-    }
-
-    if (hostdata->options & OPTION_DEBUG_TEST2) {
+    if ((hostdata->options & OPTION_DEBUG_TEST2) && 
+       hostdata->state != STATE_DISABLED) {
        u32 dsa[48];
        unsigned char identify = IDENTIFY(0, 0);
        unsigned char cmd[6];
@@ -1345,14 +1932,12 @@ static int NCR53c8xx_run_tests (struct Scsi_Host *host) {
            hostdata->state = STATE_RUNNING;
            NCR53c7x0_write32 (DSA_REG, virt_to_bus(dsa));
            NCR53c7x0_write32 (DSP_REG, start);
-           mb();
            sti();
 
            timeout = jiffies + 5 * HZ; /* arbitrary */
            while ((hostdata->test_completed == -1) && jiffies < timeout)
                barrier();
            NCR53c7x0_write32 (DSA_REG, 0);
-           mb();
 
            if (hostdata->test_completed == 2) {
                data[35] = 0;
@@ -1377,11 +1962,6 @@ static int NCR53c8xx_run_tests (struct Scsi_Host *host) {
                return -1;
            } 
            hostdata->test_running = 0;
-           if (hostdata->issue_dsa_head) {
-               printk ("scsi%d : hostdata->issue_dsa_head corrupt after test 2 id %d\n",
-                   host->host_no, i);
-               hostdata->issue_dsa_head = 0;
-       }
        }
     }
 
@@ -1399,7 +1979,8 @@ static int NCR53c8xx_run_tests (struct Scsi_Host *host) {
  *     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 *)
@@ -1409,17 +1990,74 @@ static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd) {
     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(u32),
-       dsa_temp_jump_resume, virt_to_bus(cmd->dsa) + 
-       Ent_dsa_jump_resume - Ent_dsa_zero);
-    patch_abs_rwri_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
+    /* 
+     * 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(u32),
        dsa_temp_lun, c->lun);
     patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
-       dsa_temp_dsa_next, virt_to_bus(cmd->dsa) + A_dsa_next);
+       dsa_temp_addr_next, virt_to_bus(&cmd->dsa_next_addr));
     patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
-       dsa_temp_sync, hostdata->sync[c->target].select_indirect);
-    patch_abs_rwri_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
+       dsa_temp_next, virt_to_bus(cmd->dsa) + Ent_dsa_zero -
+       Ent_dsa_code_template + A_dsa_next);
+    patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32), 
+       dsa_temp_sync, virt_to_bus((void *)hostdata->sync[c->target].script));
+    patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
        dsa_temp_target, c->target);
+    /* XXX - new pointer stuff */
+    patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
+       dsa_temp_addr_saved_pointer, virt_to_bus(&cmd->saved_data_pointer));
+    patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
+       dsa_temp_addr_saved_residual, virt_to_bus(&cmd->saved_residual));
+    patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
+       dsa_temp_addr_residual, virt_to_bus(&cmd->residual));
+
+    /*  XXX - new start stuff */
+    patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
+       dsa_temp_addr_dsa_value, virt_to_bus(&cmd->dsa_addr));
+
+}
+
+/* 
+ * 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(flags);
+       /* 
+         * 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);
 }
 
 /*
@@ -1437,46 +2075,105 @@ static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd) {
  *     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 *)
        host->hostdata;
     unsigned long flags;
-    volatile u32 *prev, search;
-    int i;
+    int left, found;
+    volatile struct NCR53c7x0_cmd * linux_search;
+    volatile struct NCR53c7x0_cmd * volatile *linux_prev;
+    volatile u32 *ncr_prev, *current, ncr_search;
+
+#if 0
+    printk ("scsi%d: abnormal finished\n", host->host_no);
+#endif
 
     save_flags(flags);
     cli();
-    for (i = 0; i < 2; ++i) {
-       for (search = (i ? hostdata->issue_dsa_head :
-               hostdata->reconnect_dsa_head), prev = (i ? 
-               &hostdata->issue_dsa_head : &hostdata->reconnect_dsa_head);
-            search && ((char*)bus_to_virt(search) + hostdata->dsa_start) != (char *) cmd->dsa;
-            prev = (u32*) ((char*)bus_to_virt(search) + hostdata->dsa_next),
-               search = *prev);
+    found = 0;
+    /* 
+     * Traverse the NCR issue array until we find a match or run out 
+     * of instructions.  Instructions in the NCR issue array are 
+     * either JUMP or NOP instructions, which are 2 words in length.
+     */
 
-       if (search)
-           *prev = *(u32*) ((char*)bus_to_virt(search) + hostdata->dsa_next);
+
+    for (found = 0, left = host->can_queue, current = hostdata->schedule; 
+       left > 0; --left, current += 2)
+    {
+       if (issue_to_cmd (host, hostdata, (u32 *) current) == cmd) 
+       {
+           current[0] = hostdata->NOP_insn;
+           current[1] = 0xdeadbeef;
+           ++found;
+           break;
+       }
     }
+       
+    /* 
+     * Traverse the NCR reconnect list of DSA structures until we find 
+     * a pointer to this dsa or have found too many command structures.  
+     * We let prev point at the next field of the previous element or 
+     * head of the list, so we don't do anything different for removing 
+     * the head element.  
+     */
 
-    if (cmd->prev)
-       cmd->prev->next = cmd->next;
+    for (left = host->can_queue,
+           ncr_search = hostdata->reconnect_dsa_head, 
+           ncr_prev = &hostdata->reconnect_dsa_head;
+       left >= 0 && ncr_search && 
+           ((char*)bus_to_virt(ncr_search) + hostdata->dsa_start) 
+               != (char *) cmd->dsa;
+       ncr_prev = (u32*) ((char*)bus_to_virt(ncr_search) + 
+           hostdata->dsa_next), ncr_search = *ncr_prev, --left);
+
+    if (left < 0) 
+       printk("scsi%d: loop detected in ncr reonncect list\n",
+           host->host_no);
+    else if (ncr_search) 
+       if (found)
+           printk("scsi%d: scsi %ld in ncr issue array and reconnect lists\n",
+               host->host_no, c->pid);
+       else {
+           volatile u32 * next = (u32 *) 
+               ((char *)bus_to_virt(ncr_search) + hostdata->dsa_next);
+           *ncr_prev = *next;
+/* If we're at the tail end of the issue queue, update that pointer too. */
+           found = 1;
+       }
 
-    if (cmd->next)
-       cmd->next->prev = cmd->prev;
+    /*
+     * Traverse the host running list until we find this command or discover
+     * we have too many elements, pointing linux_prev at the next field of the 
+     * linux_previous element or head of the list, search at this element.
+     */
 
-    if (hostdata->running_list == cmd)
-       hostdata->running_list = cmd->next;
+    for (left = host->can_queue, linux_search = hostdata->running_list, 
+           linux_prev = &hostdata->running_list;
+       left >= 0 && linux_search && linux_search != cmd;
+       linux_prev = &(linux_search->next), 
+           linux_search = linux_search->next, --left);
+    
+    if (left < 0) 
+       printk ("scsi%d: loop detected in host running list for scsi pid %ld\n",
+           host->host_no, c->pid);
+    else if (linux_search) 
+       *linux_prev = linux_search->next;
 
+    /* Return the NCR command structure to the free list */
     cmd->next = hostdata->free;
     hostdata->free = cmd;
-
     c->host_scribble = NULL;
+
+    /* And return */
     c->result = result;
     c->scsi_done(c);
 
     restore_flags(flags);
+    run_process_issue_queue();
 }
 
 /* 
@@ -1491,7 +2188,8 @@ static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int result) {
  *
  */
 
-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;
@@ -1509,7 +2207,6 @@ static void intr_break (struct Scsi_Host *host, struct
      * dump the appropriate debugging information to standard 
      * output.  
      */
-
     save_flags(flags);
     cli();
     dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
@@ -1527,55 +2224,140 @@ static void intr_break (struct Scsi_Host *host, struct
 
     NCR53c7x0_write8 (hostdata->dmode, 
        NCR53c7x0_read8(hostdata->dmode)|DMODE_MAN);
-    mb();
 
     /*
      * And update the DSP register, using the size of the old 
      * instruction in bytes.
      */
 
-     restore_flags(flags);
+    restore_flags(flags);
 }
-
 /*
- * Function : static int asynchronous (struct Scsi_Host *host, int target)
+ * 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.
  *
- * Purpose : reprogram between the selected SCSI Host adapter and target 
- *      (assumed to be currently connected) for asynchronous transfers.
+ * 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 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;
+    u32 *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 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 = (u32 *) 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);
-       mb();
+#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);
 }
 
 /* 
@@ -1587,9 +2369,9 @@ static int asynchronous (struct Scsi_Host *host, int target) {
 
 /* 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}, 
@@ -1617,60 +2399,50 @@ static const struct {
  */
 
 
-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;
-    u32 *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);
+    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);
 
-    limit = (sizeof(syncs) / sizeof(syncs[0])) - 1;
-    for (i = 0; (i < limit) && (divisor < syncs[i + 1].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);
 
-    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);
 
-    msg[3] = (1000000000 / divisor / 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 = (u32*) 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);
 }
 
 /* 
@@ -1686,27 +2458,25 @@ static void synchronous (struct Scsi_Host *host, int target, char *msg) {
  *
  */
 
-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;         
     u32 dsps,*dsp;     /* Argument of the INT instruction */
     NCR53c7x0_local_setup(host);
     dsps = NCR53c7x0_read32(DSPS_REG);
-    dsp = bus_to_virt(NCR53c7x0_read32(DSP_REG));
+    dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
 
     if (hostdata->options & OPTION_DEBUG_INTR) 
        printk ("scsi%d : DSPS = 0x%x\n", host->host_no, dsps);
 
     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
@@ -1716,20 +2486,56 @@ static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
            hostdata->dsp = hostdata->script + hostdata->E_accept_message /
                sizeof(u32);
            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(u32);
+           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(u32);
            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
@@ -1740,8 +2546,11 @@ static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
         */
            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(u32);
                hostdata->dsp_changed = 1;
@@ -1749,37 +2558,23 @@ static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
            } else {
                if (hostdata->options & OPTION_SYNCHRONOUS)  {
                    cmd->flags |= CMD_FLAG_DID_SDTR;
-                   synchronous (host, c->target, (unsigned char *)
+                   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, 
-                   virt_to_bus((void*)hostdata->msg_buf));
+               patch_dsa_32 (cmd->dsa, dsa_msgout_other, 1, (u32) 
+                   virt_to_bus ((void *)&hostdata->msg_buf));
                hostdata->dsp = hostdata->script + 
-               hostdata->E_respond_message / sizeof(u32);
+                   hostdata->E_respond_message / sizeof(u32);
                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(u32);
@@ -1791,14 +2586,15 @@ static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
        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(u32);
        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(u32);
        hostdata->dsp_changed = 1;
@@ -1823,19 +2619,12 @@ static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
            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
+        hostdata->NOP_insn* 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.
         */
@@ -1862,15 +2651,18 @@ static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
         * command so overflow/underrun conditions are detected.
         */
 
-       patch_dsa_32 (cmd->dsa, dsa_dataout, 0, hostdata->E_other_transfer);
-       patch_dsa_32 (cmd->dsa, dsa_datain, 0, virt_to_bus(cmd->data_transfer_start));
+       patch_dsa_32 (cmd->dsa, dsa_dataout, 0, 
+           virt_to_bus(hostdata->script) + hostdata->E_other_transfer);
+       patch_dsa_32 (cmd->dsa, dsa_datain, 0, 
+           virt_to_bus(cmd->data_transfer_start));
        cmd->data_transfer_start[0] = (((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I | 
            DCMD_BMI_IO)) << 24) | sizeof(c->sense_buffer);
-       cmd->data_transfer_start[1] = virt_to_bus(c->sense_buffer);
+       cmd->data_transfer_start[1] = (u32) virt_to_bus(c->sense_buffer);
 
        cmd->data_transfer_start[2] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) 
            << 24) | DBC_TCI_TRUE;
-       cmd->data_transfer_start[3] = hostdata->E_other_transfer;
+       cmd->data_transfer_start[3] = (u32) virt_to_bus(hostdata->script) + 
+           hostdata->E_other_transfer;
 
        /*
         * Currently, this command is flagged as completed, ie 
@@ -1884,15 +2676,14 @@ static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
        /* 
         * Restart command as a REQUEST SENSE.
         */
-       hostdata->dsp = hostdata->script + hostdata->E_select /
+       hostdata->dsp = (u32 *) hostdata->script + hostdata->E_select /
            sizeof(u32);
        hostdata->dsp_changed = 1;
        return SPECIFIC_INT_NOTHING;
     case A_int_debug_break:
        return SPECIFIC_INT_BREAK;
     case A_int_norm_aborted:
-       hostdata->dsp = hostdata->script + hostdata->E_schedule / 
-               sizeof(u32);
+       hostdata->dsp = (u32 *) hostdata->schedule;
        hostdata->dsp_changed = 1;
        if (cmd)
            abnormal_finished (cmd, DID_ERROR << 16);
@@ -1902,14 +2693,99 @@ static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
        hostdata->idle = 1;
        hostdata->test_completed = (dsps - A_int_test_1) / 0x00010000 + 1;
        if (hostdata->options & OPTION_DEBUG_INTR)
-           printk("scsi%d : test %d complete\n", host->host_no,
+           printk("scsi%d : test%d 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 
+            */ 
+           u32 *dsa;
+           dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
+
+           printk("scsi%d : reselected_ok (DSA = 0x%x (virt 0x%p)\n", 
+               host->host_no, NCR53c7x0_read32(DSA_REG), dsa);
+           printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
+                   host->host_no, cmd->saved_data_pointer,
+                   bus_to_virt(cmd->saved_data_pointer));
+           print_insn (host, hostdata->script + Ent_reselected_ok / 
+                   sizeof(u32), "", 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, (u32 *) 
+                   hostdata->sync[c->target].script, "", 1);
+               print_insn (host, (u32 *) 
+                   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)) {
+           u32 *dsa;
+#if 0
+           u32 *code;
+#endif
+           /* 
+            * Note - this dsa is not based on location relative to 
+            * the command structure, but to location relative to the 
+            * DSA register 
+            */ 
+           dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
+           printk("scsi%d : reselected_check_next (DSA = 0x%lx (virt 0x%p))\n",
+               host->host_no, virt_to_bus(dsa), dsa);
+           if (dsa) {
+               printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
+                   host->host_no, cmd->saved_data_pointer,
+                   bus_to_virt (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) 
+                   / sizeof(u32); code < (dsa + Ent_dsa_zero / sizeof(u32)); 
+                   code += print_insn (host, code, "", 1));
+#endif
+           }
+           print_insn (host, hostdata->script + Ent_reselected_ok / 
+                   sizeof(u32), "", 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)) {
+           u32 *dsa;
+           /* 
+            * Note - this dsa is not based on location relative to 
+            * the command structure, but to location relative to the 
+            * DSA register 
+            */ 
+           dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
+           printk("scsi%d : dsa_schedule (old DSA = 0x%lx (virt 0x%p))\n", 
+               host->host_no, virt_to_bus(dsa), dsa);
+           if (dsa) 
+               printk("scsi%d : resume address is 0x%x (virt 0x%p)\n"
+                      "         (temp was 0x%x (virt 0x%p))\n",
+                   host->host_no, cmd->saved_data_pointer,
+                   bus_to_virt (cmd->saved_data_pointer),
+                   NCR53c7x0_read32 (TEMP_REG),
+                   bus_to_virt (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)) {
-           printk("scsi%d : new I/O 0x%x scheduled\n", host->host_no,
-               NCR53c7x0_read32(DSA_REG));
+           printk("scsi%d : new I/O 0x%x (virt 0x%p) scheduled\n", 
+               host->host_no, NCR53c7x0_read32(DSA_REG),
+               bus_to_virt(NCR53c7x0_read32(DSA_REG)));
        }
        return SPECIFIC_INT_RESTART;
 #endif
@@ -1925,38 +2801,194 @@ static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
        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:
        if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
-           printk("scsi%d : DSA loaded with 0x%x\n", host->host_no,
-               NCR53c7x0_read32(DSA_REG));
+           printk("scsi%d : DSA loaded with 0x%x (virt 0x%p)\n", host->host_no,
+               NCR53c7x0_read32(DSA_REG), 
+               bus_to_virt(NCR53c7x0_read32(DSA_REG)));
        }
        return SPECIFIC_INT_RESTART; 
 #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:
+#ifdef A_int_debug_disconnect_msg
+    case A_int_debug_disconnect_msg:
        if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
-           printk("scsi%d : issue_dsa_head now 0x%x\n",
-               host->host_no, hostdata->issue_dsa_head);
+           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, (u32 *) 
+                   hostdata->sync[c->target].script, "", 1);
+               print_insn (host, (u32 *) 
+                   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%x (virt 0x%p)\n",
+               host->host_no, cmd->saved_data_pointer,
+               bus_to_virt (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%x (virt 0x%p)\n",
+                   host->host_no, cmd->saved_data_pointer, bus_to_virt (
+                   cmd->saved_data_pointer));
+               size = print_insn (host, (u32 *) 
+                   bus_to_virt(cmd->saved_data_pointer), "", 1);
+               size = print_insn (host, (u32 *) 
+                   bus_to_virt(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;
+       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, dsp, "", 1);
+               size = print_insn (host, (u32 *) bus_to_virt(dsp[1]), "", 1);
+               print_insn (host, (u32 *) bus_to_virt(dsp[1]) + size, "", 1);
+          } 
+       return SPECIFIC_INT_RESTART;
+#endif
+/* 
+ * 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) & 15;
+               char *where = dsp - NCR53c7x0_insn_size(NCR53c7x0_read8 
+                       (DCMD_REG)) == hostdata->script + 
+                       Ent_select_check_dsa / sizeof(u32) ?
+                   "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%x\n",
                host->host_no, 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 = bus_to_virt(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;
        }
 
        printk ("scsi%d : unknown user interrupt 0x%x\n", 
@@ -2011,7 +3043,7 @@ static const char debugger_help =
 "ms <addr> <size> <value>      - store memory\n"
 "rp <num> <size>               - print register\n"
 "rs <num> <size> <value>       - store register\n"
-"s                             - single step\n"
+"s                              - single step\n"       
 "tb                            - begin trace \n"
 "te                            - end trace\n";
 
@@ -2052,7 +3084,8 @@ static int debugger_fn_bc (struct Scsi_Host *host, struct debugger_token *token,
 }
 
 
-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,
     u32 args[]) {
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
        host->hostdata;
@@ -2090,7 +3123,8 @@ static int debugger_fn_bl (struct Scsi_Host *host, struct debugger_token *token,
     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,
     u32 args[]) {
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
        host->hostdata;
@@ -2098,7 +3132,6 @@ static int debugger_fn_bs (struct Scsi_Host *host, struct debugger_token *token,
     char buf[80];
     size_t len;
     unsigned long flags;
-
     save_flags(flags);
     cli();
 
@@ -2119,7 +3152,7 @@ static int debugger_fn_bs (struct Scsi_Host *host, struct debugger_token *token,
     bp->address = (u32 *) args[0];
     memcpy ((void *) bp->old_instruction, (void *) bp->address, 8);
     bp->old_size = (((bp->old_instruction[0] >> 24) & DCMD_TYPE_MASK) ==
-       DCMD_TYPE_MMI ? 3 : 2);
+       DCMD_TYPE_MMI ? 3 : 2;
     bp->next = hostdata->breakpoints;
     hostdata->breakpoints = bp->next;
     memcpy ((void *) bp->address, (void *) hostdata->E_debug_break, 8);
@@ -2141,7 +3174,7 @@ static const struct debugger_token {
 
 #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;
@@ -2151,7 +3184,8 @@ static struct Scsi_Host * inode_to_host (struct inode *inode) {$
 }
 
 
-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;      
@@ -2203,19 +3237,20 @@ static debugger_user_write (struct inode *inode,struct file *filp,
     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;
     int copy, left;
     unsigned long flags;
-    
     save_flags(flags);
     cli();
     while (buflen) {
@@ -2266,14 +3301,12 @@ NCR53c8x0_soft_reset (struct Scsi_Host *host) {
      */
 
     NCR53c7x0_write8(ISTAT_REG_800, ISTAT_10_SRST);
-    mb();
     NCR53c7x0_write8(ISTAT_REG_800, 0);
-    mb();
     NCR53c7x0_write8(hostdata->dmode, hostdata->saved_dmode & ~DMODE_MAN);
 
 
     /* 
-     * 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 
@@ -2298,15 +3331,20 @@ NCR53c8x0_soft_reset (struct Scsi_Host *host) {
      */
 #if 0
     NCR53c7x0_write8(STIME0_REG_800, 
-       ((14 << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK) 
-/* Disable HTH interrupt */
+       ((selection_timeout << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK) 
        | ((15 << STIME0_800_HTH_SHIFT) & STIME0_800_HTH_MASK));
 #else
+/* Disable HTH interrupt */
     NCR53c7x0_write8(STIME0_REG_800, 
-       ((14 << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK));
+       ((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);
 
     /*
      * Enable all interrupts, except parity which we only want when
@@ -2332,90 +3370,114 @@ NCR53c8x0_soft_reset (struct Scsi_Host *host) {
     /* Enable active negation */
     NCR53c7x0_write8(STEST3_REG_800, STEST3_800_TE);
 
-    mb();
+    
 }
 
+
 /*
- * 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.
+ *
+ *     Return the first free NCR53c7x0_cmd structure (which are 
+ *     reused in a LIFO maner to minimize cache thrashing).
  *
  * Inputs : cmd - SCSI command
  *
- * Returns : NCR53c7x0_cmd structure corresponding to cmd,
+ * 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) {
     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 */
-    u32 *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 - sizeof(long)-1) */
-#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->num_commands < 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
+    if (hostdata->options & OPTION_DEBUG_ALLOCATION)
+       printk ("scsi%d : num_cmds = %d, can_queue = %d\n"
+               "         target = %d, lun = %d, %s\n",
+           host->host_no, hostdata->num_cmds, host->can_queue,
+           cmd->target, cmd->lun, (hostdata->cmd_allocated[cmd->target] &
+               (1 << cmd->lun)) ? "allready allocated" : "not allocated");
+           
+
+    if (hostdata->num_cmds < host->can_queue &&
+#ifdef LINUX_1_2
+       !in_scan_scsis &&
+#endif
+       (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->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 + sizeof(void*);
-           real = kmalloc (size, GFP_ATOMIC);
-           alignment = sizeof(void*) - (((unsigned) real) & (sizeof(void*)-1));
-           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 + sizeof (void *);
+               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;
+               }
+               tmp = ROUNDUP(real, void *);
+               tmp->real = real;
+               tmp->size = size;                       
+#ifdef LINUX_1_2
+               tmp->free = ((void (*)(void *, int)) kfree_s);
+#else
+               tmp->free = ((void (*)(void *, int)) kfree);
+#endif
+           }
+
+
+           if (!hostdata->extra_allocate) 
+               hostdata->cmd_allocated[cmd->target] |= 1 << cmd->lun;
+
+    /* 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();
@@ -2423,11 +3485,53 @@ create_cmd (Scsi_Cmnd *cmd) {
        if (tmp) {
            hostdata->free = tmp->next;
            restore_flags(flags);
-       } else {
-           restore_flags(flags);
-           return NULL;
        }
+       restore_flags(flags);
     }
+    if (!tmp)
+       printk ("scsi%d : can't allocate command for target %d lun %d\n",
+           host->host_no, cmd->target, cmd->lun);
+    return tmp;
+}
+
+
+/*
+ * 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) {
+    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 */
+    u32 *cmd_datain,           /* Address of datain/dataout code */
+       *cmd_dataout;           /* Incremented as we assemble */
+#ifdef notyet
+    unsigned char *msgptr;     /* Current byte in select message */
+    int msglen;                        /* Length of whole select message */
+#endif
+    unsigned long flags;
+    NCR53c7x0_local_setup(cmd->host);
+
+    if (!(tmp = allocate_cmd (cmd)))
+       return NULL;
+
 
     /*
      * Decide whether we need to generate commands for DATA IN,
@@ -2479,10 +3583,26 @@ create_cmd (Scsi_Cmnd *cmd) {
        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.
+     *
+     *         Furthermore, 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;
@@ -2496,13 +3616,25 @@ create_cmd (Scsi_Cmnd *cmd) {
     if (data_transfer_instructions < 2)
        data_transfer_instructions = 2;
 
+
+    /*
+     * 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 = virt_to_bus (hostdata->script) + 
+       hostdata->E_data_transfer;
+
     /*
      * Initialize Linux specific fields.
      */
 
     tmp->cmd = cmd;
     tmp->next = NULL;
-    tmp->prev = NULL;
+    tmp->flags = 0;
+    tmp->dsa_next_addr = virt_to_bus(tmp->dsa) + hostdata->dsa_next - 
+       hostdata->dsa_start;
+    tmp->dsa_addr = virt_to_bus(tmp->dsa) - hostdata->dsa_start;
 
     /* 
      * Calculate addresses of dynamic code to fill in DSA
@@ -2530,50 +3662,99 @@ create_cmd (Scsi_Cmnd *cmd) {
 
     patch_dsa_32(tmp->dsa, dsa_next, 0, 0);
     patch_dsa_32(tmp->dsa, dsa_cmnd, 0, virt_to_bus(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%x\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.
+     * Right now, we'll do the WIDE and SYNCHRONOUS negotiations on
+     * different commands; although it should be trivial to do them
+     * both at the same time.
      */
-    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_wdtr & (1 << cmd->target)) {
+       memcpy ((void *) (tmp->select + 1), (void *) wdtr_message,
+           sizeof(wdtr_message));
+       patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1 + sizeof(wdtr_message));
+       save_flags(flags);
+       cli();
+       hostdata->initiate_wdtr &= ~(1 << cmd->target);
+       restore_flags(flags);
+    } else 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, virt_to_bus(tmp->select));
     patch_dsa_32(tmp->dsa, dsa_cmdout, 0, cmd->cmd_len);
     patch_dsa_32(tmp->dsa, dsa_cmdout, 1, virt_to_bus(cmd->cmnd));
-    patch_dsa_32(tmp->dsa, dsa_dataout, 0, cmd_dataout ?
-       virt_to_bus(cmd_dataout) : virt_to_bus(hostdata->script) + hostdata->E_other_transfer);
-    patch_dsa_32(tmp->dsa, dsa_datain, 0, cmd_datain ?
-       virt_to_bus(cmd_datain) : virt_to_bus(hostdata->script) + hostdata->E_other_transfer);
+    patch_dsa_32(tmp->dsa, dsa_dataout, 0, cmd_dataout ? 
+           virt_to_bus (cmd_dataout)
+       : virt_to_bus (hostdata->script) + hostdata->E_other_transfer);
+    patch_dsa_32(tmp->dsa, dsa_datain, 0, cmd_datain ? 
+           virt_to_bus (cmd_datain) 
+       : virt_to_bus (hostdata->script) + hostdata->E_other_transfer);
     /* 
      * XXX - need to make endian aware, should use separate variables
      * for both status and message bytes.
      */
     patch_dsa_32(tmp->dsa, dsa_msgin, 0, 1);
+/* 
+ * FIXME : these only works for little endian.  We probably want to 
+ *     provide message and status fields in the NCR53c7x0_cmd 
+ *     structure, and assign them to cmd->result when we're done.
+ */
     patch_dsa_32(tmp->dsa, dsa_msgin, 1, virt_to_bus(&cmd->result) + 1);
     patch_dsa_32(tmp->dsa, dsa_status, 0, 1);
     patch_dsa_32(tmp->dsa, dsa_status, 1, virt_to_bus(&cmd->result));
     patch_dsa_32(tmp->dsa, dsa_msgout_other, 0, 1);
     patch_dsa_32(tmp->dsa, dsa_msgout_other, 1, 
-       virt_to_bus(&hostdata->NCR53c7xx_msg_nop));
-
+       virt_to_bus(&(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;
@@ -2599,7 +3780,7 @@ create_cmd (Scsi_Cmnd *cmd) {
 
     for (i = 0; cmd->use_sg ? (i < cmd->use_sg) : !i; cmd_datain += 4, 
        cmd_dataout += 4, ++i) {
-       u32 buf = cmd->use_sg ?
+       u32 buf = cmd->use_sg ? 
            virt_to_bus(((struct scatterlist *)cmd->buffer)[i].address) :
            virt_to_bus(cmd->request_buffer);
        u32 count = cmd->use_sg ?
@@ -2607,28 +3788,31 @@ create_cmd (Scsi_Cmnd *cmd) {
            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] = virt_to_bus (hostdata->script) + 
+               hostdata->E_other_in;
+           /* 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] = virt_to_bus(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] = virt_to_bus(hostdata->script) + 
+               hostdata->E_other_out;
+           /* 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] = virt_to_bus(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);
@@ -2645,8 +3829,8 @@ create_cmd (Scsi_Cmnd *cmd) {
     if (datain) {
        cmd_datain[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
            DBC_TCI_TRUE;
-       cmd_datain[1] = virt_to_bus(hostdata->script) +
-           hostdata->E_other_transfer;
+       cmd_datain[1] = virt_to_bus(hostdata->script) + 
+           hostdata->E_other_transfer;
 #if 0
        print_insn (host, cmd_datain, "dynamic jump ", 1);
 #endif
@@ -2659,224 +3843,328 @@ create_cmd (Scsi_Cmnd *cmd) {
        cmd_datain += 2;
     }
 #endif
-
-
     if (dataout) {
        cmd_dataout[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
            DBC_TCI_TRUE;
-       cmd_dataout[1] = virt_to_bus(hostdata->script) +
-           hostdata->E_other_transfer;
+       cmd_dataout[1] = virt_to_bus(hostdata->script) + 
+           hostdata->E_other_transfer;
 #if 0
        print_insn (host, cmd_dataout, "dynamic jump ", 1);
 #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) {
-       printk("scsi%d : disabled target %d lun %d\n", host->host_no,
+    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))) 
+#ifdef LINUX_1_2
+       || cmd->target > 7
+#else
+       || cmd->target > host->max_id
+#endif
+       || cmd->target == host->this_id
+       || hostdata->state == STATE_DISABLED) {
+       printk("scsi%d : disabled or bad 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);
     }
-
-    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);
-
-    /*
-     * 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.
-     * 
-     * The Linux queue includes commands which have been generated,
-     * but may be unable to execute because the device is busy, 
-     * where as the NCR queue contains commands to issue as soon
-     * 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.
-     */
-    
+    cli();
     /*
-     * 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.
+     * REQUEST SENSE commands are inserted at the head of the queue 
+     * so that we do not clear the contingent allegience condition
+     * they may be looking at.
      */
 
-    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.
-     */
-       
-    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%p\n", host->host_no, dsa);
-#endif
-
-       if (hostdata->running_list)
-           hostdata->running_list->prev = tmp;
-
-       tmp->next = (struct NCR53c7x0_cmd*) hostdata->running_list;
-
-       if (!hostdata->running_list)
-           hostdata->running_list = (struct NCR53c7x0_cmd*) tmp;
-       
-
-       if (hostdata->idle) {
-           hostdata->idle = 0;
-           hostdata->state = STATE_RUNNING;
-           NCR53c7x0_write32 (DSP_REG,  virt_to_bus(hostdata->script) +
-               hostdata->E_schedule);
-           mb();
-       }
-
-/* 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);
-#endif
-               hostdata->issue_dsa_tail = (u32 *) dsa;
-               hostdata->issue_dsa_head = virt_to_bus(dsa);
-               NCR53c7x0_write8(hostdata->istat, 
-                   NCR53c7x0_read8(hostdata->istat) | ISTAT_10_SIGP);
-               mb();
-               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);
-               hostdata->issue_dsa_tail[hostdata->dsa_next/sizeof(u32)]
-                 = virt_to_bus(dsa);
-               hostdata->issue_dsa_tail = (u32 *) dsa;
-           /*
-            * After which, one of two things will happen : 
-            * The NCR will have scheduled a command, either this
-            * one, or the next one.  In this case, we successfully
-            * added our command to the queue.
-            *
-            * The NCR will have written the hostdata->issue_dsa_head
-            * pointer with the NULL pointer terminating the list,
-            * in which case we were too late.  If this happens,
-            * we restart
-            */
-               if (hostdata->issue_dsa_head)
-                   break;
-           }
-       }
-/* XXX - end */
+    if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
+       cmd->SCp.ptr = (unsigned char *) hostdata->issue_queue;
+       hostdata->issue_queue = cmd;
     } 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;
+       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);
-    return 0;
-}
-
-
-int fix_pointers (u32 dsa) {
+    restore_flags (flags);
+    run_process_issue_queue();
     return 0;
 }
 
 /*
- * Function : static void intr_scsi (struct Scsi_Host *host, 
- *     struct NCR53c7x0_cmd *cmd)
+ * Function : void to_schedule_list (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 in the first
+ *     free slot in the schedule list 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 schedule list, with minor 
+ *      twiddling done to the host specific fields of cmd.
+ *
+ */
+
+static __inline__ void
+to_schedule_list (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 */
+    volatile u32 *current;
+
+    int i;
+    NCR53c7x0_local_setup(host);
+#if 0  
+    printk("scsi%d : new dsa is 0x%lx (virt 0x%p)\n", host->host_no, 
+       virt_to_bus(dsa), dsa);
+#endif
+
+    save_flags(flags);
+    cli();
+    
+    /* 
+     * Work arround race condition : if an interrupt fired and we 
+     * got disabled forget about this command.
+     */
+
+    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;
+    }
+
+    for (i = host->can_queue, current = hostdata->schedule; 
+       i > 0  && current[0] != hostdata->NOP_insn;
+       --i, current += 2 /* JUMP instructions are two words */);
+
+    if (i > 0) {
+       ++hostdata->busy[tmp->target][tmp->lun];
+       cmd->next = hostdata->running_list;
+       hostdata->running_list = cmd;
+
+       /* Restore this instruction to a NOP once the command starts */
+       cmd->dsa [(hostdata->dsa_jump_dest - hostdata->dsa_start) / 
+           sizeof(u32)] = (u32) virt_to_bus ((void *)current);
+       /* Replace the current jump operand.  */
+       current[1] =
+           virt_to_bus ((void *) cmd->dsa) + hostdata->E_dsa_code_begin -
+           hostdata->E_dsa_code_template;
+       /* Replace the NOP instruction with a JUMP */
+       current[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) |
+           DBC_TCI_TRUE;
+    }  else {
+       printk ("scsi%d: no free slot\n", host->host_no);
+       disable(host);
+       tmp->result = (DID_ERROR << 16);
+       cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
+       hostdata->free = cmd;
+       tmp->scsi_done(tmp);
+       restore_flags (flags);
+       return;
+    }
+
+    /* 
+     * If the NCR chip is in an idle state, start it running the scheduler
+     * immediately.  Otherwise, signal the chip to jump to schedule as 
+     * soon as it is idle.
+     */
+    if (hostdata->idle) {
+       hostdata->idle = 0;
+       hostdata->state = STATE_RUNNING;
+       NCR53c7x0_write32 (DSP_REG,  virt_to_bus ((void *)hostdata->schedule));
+    } else {
+       NCR53c7x0_write8(hostdata->istat, ISTAT_10_SIGP);
+    }
+
+    restore_flags(flags);
+}
+
+/*
+ * 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 (unsigned long flags) {
+    Scsi_Cmnd *tmp, *prev;
+    struct Scsi_Host *host;
+    struct NCR53c7x0_hostdata *hostdata;
+    int done;
+
+    /*
+     * 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.
+     *
+     */
+
+    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 (prev)
+                                   prev->SCp.ptr = tmp->SCp.ptr;
+                               else
+                                   hostdata->issue_queue = (Scsi_Cmnd *) 
+                                       tmp->SCp.ptr;
+                           tmp->SCp.ptr = NULL;
+                           if (tmp->host_scribble) {
+                               if (hostdata->options & OPTION_DEBUG_QUEUES) 
+                                   printk ("scsi%d : moving command for target %d lun %d to start list\n",
+                                       host->host_no, tmp->target, tmp->lun);
+               
+
+                               to_schedule_list (host, hostdata, 
+                                   (struct NCR53c7x0_cmd *)
+                                   tmp->host_scribble);
+                           } else {
+                               if (((tmp->result & 0xff) == 0xff) ||
+                                   ((tmp->result & 0xff00) == 0xff00)) {
+                                   printk ("scsi%d : danger Will Robinson!\n",
+                                       host->host_no);
+                                   tmp->result = DID_ERROR << 16;
+                                   disable (host);
+                               }
+                               tmp->scsi_done(tmp);
+                           }
+                           done = 0;
+                       } /* if target/lun is not busy */
+           } /* if hostdata->issue_queue */
+           if (!done)
+               restore_flags (flags);
+       } /* for host */
+    } while (!done);
+    process_issue_queue_running = 0;
+}
+
+/*
+ * Function : static void intr_scsi (struct Scsi_Host *host, 
+ *     struct NCR53c7x0_cmd *cmd)
  *
  * Purpose : handle all SCSI interrupts, indicated by the setting 
  *     of the SIP bit in the ISTAT register.
@@ -2885,18 +4173,20 @@ int fix_pointers (u32 dsa) {
  *     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;
     unsigned char sstat0_sist0, sist1,                 /* Registers */
            fatal;                              /* Did a fatal interrupt 
                                                   occur ? */
-    int is_8xx_chip;
+   
+    int is_8xx_chip;            
     NCR53c7x0_local_setup(host);
 
     fatal = 0;
-  
+
     is_8xx_chip = ((unsigned) (hostdata->chip - 800)) < 100;
     if (is_8xx_chip) {
        sstat0_sist0 = NCR53c7x0_read8(SIST0_REG_800);
@@ -2911,9 +4201,9 @@ static void intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
        printk ("scsi%d : SIST0 0x%0x, SIST1 0x%0x\n", host->host_no,
            sstat0_sist0, sist1);
 
-    /* selection timeout */
-    if ((is_8xx_chip && (sist1 & SIST1_800_STO)) ||
-       (!is_8xx_chip && (sstat0_sist0 & SSTAT0_700_STO))) {
+    /* 250ms selection timeout */
+    if ((is_8xx_chip && (sist1 & SIST1_800_STO)) || 
+        (!is_8xx_chip && (sstat0_sist0 & SSTAT0_700_STO))) {
        fatal = 1;
        if (hostdata->options & OPTION_DEBUG_INTR) {
            printk ("scsi%d : Selection Timeout\n", host->host_no);
@@ -2921,8 +4211,9 @@ static void intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
                printk("scsi%d : target %d, lun %d, command ",
                    host->host_no, cmd->cmd->target, cmd->cmd->lun);
                print_command (cmd->cmd->cmnd);
-               printk("scsi%d : dsp = 0x%x\n", host->host_no,
-                   (unsigned) NCR53c7x0_read32(DSP_REG));
+               printk("scsi%d : dsp = 0x%x (virt 0x%p)\n", host->host_no,
+                   NCR53c7x0_read32(DSP_REG),
+                   bus_to_virt(NCR53c7x0_read32(DSP_REG)));
            } else {
                printk("scsi%d : no command\n", host->host_no);
            }
@@ -2948,20 +4239,26 @@ static void intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
 #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);
-       }
-       hostdata->dsp = hostdata->script + hostdata->E_schedule / 
-           sizeof(u32);
+       } else 
+            printk("scsi%d : unexpected disconnect (no command)\n",
+               host->host_no);
+
+       hostdata->dsp = (u32 *) hostdata->schedule;
        hostdata->dsp_changed = 1;
-    /* SCSI PARITY error */
-    } 
+    }
 
+    /* SCSI PARITY error */
     if (sstat0_sist0 & SSTAT0_PAR) {
        fatal = 1;
        if (cmd && cmd->cmd) {
@@ -2982,7 +4279,27 @@ static void intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
     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(u32);
        hostdata->dsp_changed = 1;
@@ -2996,7 +4313,11 @@ static void intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
        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.
@@ -3011,26 +4332,19 @@ static void intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
 /* 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) {
+               printk ("scsi%d: Flushing DMA FIFO\n", 
+                       host->host_no);
                NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
-               mb();
                while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
                    DSTAT_DFE));
-           } else 
-#endif
-           {
+           } else {
                NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
-               mb();
                while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
            }
+           hostdata->dstat |= DSTAT_DFE;
        }
-
-       NCR53c7x0_write8 (STEST3_REG_800, STEST3_800_CSF);
-       mb();
-       while (NCR53c7x0_read8 (STEST3_REG_800) & STEST3_800_CSF);
     }
-#endif
 }
 
 /*
@@ -3044,7 +4358,8 @@ static void intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
  *     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 */
@@ -3056,26 +4371,26 @@ static void NCR53c7x0_intr (int irq, struct pt_regs * regs) {
                                                   should terminate */
     int interrupted = 0;                       /* This HA generated 
                                                   an interrupt */
-    unsigned long flags;
+    int have_intfly;                           /* Don't print warning 
+                                                  messages when we stack
+                                                  INTFLYs */
+    unsigned long flags;                               
 
 #ifdef NCR_DEBUG
     char buf[80];                              /* Debugging sprintf buffer */
     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) {
+       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 {
                int is_8xx_chip;
@@ -3084,7 +4399,7 @@ static void NCR53c7x0_intr (int irq, struct pt_regs * regs) {
                interrupted = 0;
                /*
                 * Only read istat once, since reading it again will unstack
-                * interrupts.
+                * interrupts?
                 */
                istat = NCR53c7x0_read8(hostdata->istat);
 
@@ -3094,7 +4409,8 @@ static void NCR53c7x0_intr (int irq, struct pt_regs * regs) {
                 * 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() */
                is_8xx_chip = ((unsigned) (hostdata->chip - 800)) < 100;
                if ((hostdata->options & OPTION_INTFLY) && 
                    (is_8xx_chip && (istat & ISTAT_800_INTF))) {
@@ -3103,11 +4419,10 @@ static void NCR53c7x0_intr (int irq, struct pt_regs * regs) {
                    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);
-                   mb();
 
                    if (hostdata->options & OPTION_DEBUG_INTR)
                        printk ("scsi%d : INTFLY\n", host->host_no); 
@@ -3152,19 +4467,10 @@ restart:
                        search_found = 1;
 
                        /* 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
-
-
+                       --hostdata->busy[tmp->target][tmp->lun];
                        cmd->next = hostdata->free;
                        hostdata->free = cmd;
 
@@ -3185,10 +4491,20 @@ restart:
 
                    }
                    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();
                    }
                }
 
@@ -3196,10 +4512,16 @@ restart:
                    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_cmd, in the Linux driver so
-                    * cmd = hostdata->current_cmd.
+                    * process, hostdata->current, in the Linux driver so
+                    * cmd = hostdata->current.
                     *
                     * With other chips, we must look through the commands
                     * executing and find the command structure which 
@@ -3207,7 +4529,7 @@ restart:
                     */
 
                    if (hostdata->options & OPTION_700) {
-                       cmd = (struct NCR53c7x0_cmd *) hostdata->current_cmd;
+                       cmd = (struct NCR53c7x0_cmd *) hostdata->current;
                    } else {
                        dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
                        for (cmd = (struct NCR53c7x0_cmd *) 
@@ -3244,25 +4566,22 @@ restart:
                        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) {
+                           printk ("scsi%d: Flushing DMA FIFO\n", 
+                               host->host_no);
                            NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
-                           mb();
                            while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
                                DSTAT_DFE));
                        } else 
-           #endif
                        {
                            NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
-                           mb();
                            while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
                        }
+                       hostdata->dstat |= DSTAT_DFE;
                    }
-#endif
                }
            } while (interrupted);
 
@@ -3271,27 +4590,25 @@ restart:
            if (hostdata->intrs != -1)
                hostdata->intrs++;
 #if 0
-           if (hostdata->intrs > 4) {
+           if (hostdata->intrs > 40) {
                printk("scsi%d : too many interrupts, halting", host->host_no);
-               hostdata->idle = 1;
-               hostdata->options |= OPTION_DEBUG_INIT_ONLY;
-               panic("dying...\n");
+               disable(host);
            }
 #endif
 
            if (!hostdata->idle && hostdata->state == STATE_HALTED) {
                if (!hostdata->dsp_changed) {
-                   hostdata->dsp = bus_to_virt(NCR53c7x0_read32(DSP_REG));
+                   hostdata->dsp = (u32 *) 
+                       bus_to_virt(NCR53c7x0_read32(DSP_REG));
                }
                        
 #if 0
-               printk("scsi%d : new dsp is 0x%p\n", host->host_no, 
-                   hostdata->dsp);
+               printk("scsi%d : new dsp is 0x%lx (virt 0x%p)\n",
+                   host->host_no,  virt_to_bus(hostdata->dsp), hostdata->dsp);
 #endif
                
                hostdata->state = STATE_RUNNING;
                NCR53c7x0_write32 (DSP_REG, virt_to_bus(hostdata->dsp));
-               mb();
            }
        }
     } while (!done);
@@ -3312,20 +4629,166 @@ restart:
 
 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;
+    u32 *script;
+    NCR53c7x0_local_setup(host);
+#endif
+
+    if (--counter <= 0) {
+       disable(host);
+       return 0;
+    }
 
-    hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
-       sizeof(u32);
-    hostdata->dsp_changed = 1;
     printk ("scsi%d : DANGER : abort_connected() called \n",
        host->host_no);
+
+#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) 
+ */
+
+    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(u32);
+#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, 
@@ -3340,154 +4803,313 @@ abort_connected (struct Scsi_Host *host) {
  *     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();
     u32 dbc_dcmd, *dsp, *dsp_next;
     unsigned char dcmd, sbcl;
     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
        host->hostdata;
-    const 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 = bus_to_virt(NCR53c7x0_read32(DSP_REG));
 
-    /*
-     * Like other processors, the NCR adjusts the DSP pointer before
-     * instruction decode.  Set the DSP address back to what it should
-     * be for this instruction based on its size (2 or 3 words).
+    /* 
+     * Fetch the current instruction, and remove the operands for easier 
+     * interpretation.
      */
-
     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.
-     *
-     * 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.
+     * 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 32 bit words).
      */
-    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";
-       break;
-    default:
-       phase = "unknown";
-       break;
-    }
+    dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
 
 
     /*
-     * 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.  
+     * 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.
      */
-    if (dsp >= cmd->data_transfer_start && dsp < cmd->data_transfer_end) {
+    sbcl = NCR53c7x0_read8(SBCL_REG) & SBCL_PHASE_MASK;
 
-       /*
-        * 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(u32);
-           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, virt_to_bus(dsp));
-               mb();
-               hostdata->dsp = hostdata->script + hostdata->E_msg_in /
-                   sizeof(u32);
-               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);
-           }
-           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);
-       } 
+    if (!cmd) {
+       action = ACTION_ABORT_PRINT;
+       where = "no current command";
     /*
-     * Any other phase mismatches abort the currently executing command.
-     */
-    } else {
-       printk ("scsi%d : unexpected phase %s at dsp = 0x%p\n",
-           host->host_no, phase, dsp);
-       print_insn (host, dsp, "", 1);
-       print_insn (host, dsp_next, "", 1);
-       abort_connected(host);
+     * 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.
+     *
+     * - 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)
+     */
+    } 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] = virt_to_bus(hostdata->script)
+                   + ((dcmd & DCMD_BMI_IO)
+                      ? hostdata->E_other_in : hostdata->E_other_out);
+           }
+
+           /*
+            * 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] = virt_to_bus(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:
+           hostdata->dsp = hostdata->script + hostdata->E_msg_in / 
+               sizeof(u32);
+           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 
+       (u32)) {
+       hostdata->dsp = hostdata->script + hostdata->E_data_transfer /
+           sizeof (u32);
+       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) {
+           printk ("scsi%d: Flushing DMA FIFO\n", 
+                   host->host_no);
+           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;
+    case ACTION_CONTINUE:
+       break;
+    }
+
+#if 0
+    if (hostdata->dsp_changed) {
+       printk("scsi%d: new dsp 0x%p\n", host->host_no, hostdata->dsp);
+       print_insn (host, hostdata->dsp, "", 1);
+    }
+#endif
+    
+}
+
+/*
+ * 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;
+    u32 *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 = bus_to_virt (NCR53c7x0_read32(DSP_REG));
+    dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
+/* FIXME - check chip type  */
+    dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
+
+    /*
+     * 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 ((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;
+           }
+       } 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 reason = %s\n",
+            host->host_no, reason ? reason : "unknown");
+       print_lots (host);
+    }
+
+#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.
+     */
+
+    if (retry == NEVER) {
+       printk(KERN_ALERT "          mail drew@PoohSticks.ORG\n");
+       FATAL (host);
     }
 }
 
@@ -3502,7 +5124,8 @@ static void intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd
  *     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;
@@ -3546,14 +5169,14 @@ static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
 #if 0
        /* XXX - add code here to deal with normal abort */
        if ((hostdata->options & OPTION_700) && (hostdata->state ==
-           STATE_ABORTING) {
+           STATE_ABORTING)) {
        } 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);
        }
     }
 
@@ -3572,13 +5195,13 @@ static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
 
            NCR53c7x0_write8 (DCNTL_REG, (NCR53c7x0_read8(DCNTL_REG) & 
                ~DCNTL_SSM) | DCNTL_STD);
-           mb();
            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);
        }
     }
 
@@ -3589,7 +5212,7 @@ static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
      * 
      * 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) {
@@ -3622,41 +5245,44 @@ static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
            } else {
                hostdata->expecting_sto = 1;
            }
+    /*
+     * We can't guarantee we'll be able to execute the WAIT DISCONNECT
+     * instruction within the 3.4us of bus free and arbitration delay
+     * that a target can RESELECT in and assert REQ after we've dropped
+     * ACK.  If this happens, we'll get an illegal instruction interrupt.
+     * Doing away with the WAIT DISCONNECT instructions broke everything,
+     * so instead I'll settle for moving one WAIT DISCONNECT a few 
+     * instructions closer to the CLEAR ACK before it to minimize the
+     * chances of this happening, and handle it if it occurs anyway.
+     *
+     * Simply continue with what we were doing, and control should
+     * be transfered to the schedule routine which will ultimately
+     * pass control onto the reselection or selection (not yet)
+     * code.
+     */
+       } else if (dbc_dcmd == 0x48000000 && (NCR53c7x0_read8 (SBCL_REG) &
+           SBCL_REQ)) {
+           if (!(hostdata->options & OPTION_NO_PRINT_RACE))
+           {
+               printk("scsi%d: REQ before WAIT DISCONNECT IID\n", 
+                   host->host_no);
+               hostdata->options |= OPTION_NO_PRINT_RACE;
+           }
        } else {
-           printk("scsi%d : illegal instruction ", host->host_no);
-           print_insn (host, dsp, "", 1);
-           printk("scsi%d : DSP=0x%p, DCMD|DBC=0x%x, DSA=0x%p\n"
-              "         DSPS=0x%x, TEMP=0x%x, DMODE=0x%x,\n" 
-              "         DNAD=0x%x\n",
-            host->host_no, dsp, dbc_dcmd,
-            dsa, NCR53c7x0_read32(DSPS_REG),
-            NCR53c7x0_read32(TEMP_REG), NCR53c7x0_read8(hostdata->dmode),
-            NCR53c7x0_read32(DNAD_REG));
-           panic("         mail drew@Colorado.EDU\n");
+           printk(KERN_ALERT "scsi%d : illegal instruction\n", host->host_no);
+           print_lots (host);
+           printk(KERN_ALERT "         mail drew@PoohSticks.ORG with ALL\n"
+                             "         boot messages and diagnostic output\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%p, DCMD|DBC=0x%x, DSA=0x%p\n"
-              "         DSPS=0x%x, TEMP=0x%x, DMODE=0x%x\n", 
-            host->host_no, dsp, NCR53c7x0_read32(DBC_REG),
-            dsa, NCR53c7x0_read32(DSPS_REG),
-            NCR53c7x0_read32(TEMP_REG), 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);
     }
        
 
@@ -3679,25 +5305,28 @@ static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
            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);
-    mb();
-    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);
+    }
 }
 
 /*
@@ -3714,26 +5343,60 @@ static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
  * Inputs : host, insn - host, pointer to instruction, prefix - 
  *     string to prepend, kernel - use printk instead of debugging buffer.
  *
- * Returns : size, in ints, of instruction printed.
+ * Returns : size, in u32s, of instruction printed.
+ */
+
+/*
+ * FIXME: should change kernel parameter so that it takes an ENUM
+ *     specifying severity - either KERN_ALERT or KERN_PANIC so
+ *     all panic messages are output with the same severity.
  */
 
-static int print_insn (struct Scsi_Host *host, u32 *insn,
+static int 
+print_insn (struct Scsi_Host *host, const u32 *insn, 
     const char *prefix, int kernel) {
-    char buf[80],              /* Temporary buffer and pointer */
+    char buf[160],             /* Temporary buffer and pointer.  ICKY 
+                                  arbitrary length.  */
+
+               
        *tmp;                   
     unsigned char dcmd;                /* dcmd register for *insn */
     int size;
 
-    dcmd = (insn[0] >> 24) & 0xff;
-    sprintf(buf, "%s%p : 0x%08x 0x%08x", (prefix ? prefix : ""), 
-       insn, insn[0], insn[1]);
-    tmp = buf + strlen(buf);
-    if ((dcmd & DCMD_TYPE_MASK) == DCMD_TYPE_MMI)  {
-       sprintf (tmp, " 0x%08x\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%p: address out of range\n",
+           prefix, insn);
     } else {
-       sprintf (tmp, "\n");
-       size = 2;
+/* 
+ * FIXME : (void *) cast in virt_to_bus should be unecessary, because
+ *     it should take const void * as argument.
+ */
+       sprintf(buf, "%s0x%lx (virt 0x%p) : 0x%08x 0x%08x (virt 0x%p)", 
+           (prefix ? prefix : ""), virt_to_bus((void *) insn), insn,  
+           insn[0], insn[1], bus_to_virt (insn[1]));
+       tmp = buf + strlen(buf);
+       if ((dcmd & DCMD_TYPE_MASK) == DCMD_TYPE_MMI)  {
+           sprintf (tmp, " 0x%08x (virt 0x%p)\n", insn[2], 
+               bus_to_virt(insn[2]));
+           size = 3;
+       } else {
+           sprintf (tmp, "\n");
+           size = 2;
+       }
     }
 
     if (kernel) 
@@ -3747,6 +5410,28 @@ static int print_insn (struct Scsi_Host *host, u32 *insn,
     return size;
 }
 
+/*
+ * 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)
  * 
@@ -3759,14 +5444,61 @@ static int print_insn (struct Scsi_Host *host, u32 *insn,
  * 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;
-    volatile struct NCR53c7x0_cmd *curr, **prev;
+    struct NCR53c7x0_cmd *curr, **prev;
+    Scsi_Cmnd *me, **last;
+#if 0
+    static long cache_pid = -1;
+#endif
+
+
+    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;
+    }
+    NCR53c7x0_local_setup(host);
+
+/*
+ * CHECK : I don't think that reading ISTAT will unstack any interrupts,
+ *     since we need to write the INTF bit to clear it, and SCSI/DMA
+ *     interrupts don't clear until we read SSTAT/SIST and DSTAT registers.
+ *     
+ *     See that this is the case.
+ *
+ * I suspect that several of our failures may be coming from a new fatal
+ * interrupt (possibly due to a phase mismatch) happening after we've left
+ * the interrupt handler, but before the PIC has had the interrupt condition
+ * cleared.
+ */
+
+    if (NCR53c7x0_read8(hostdata->istat) & 
+       (ISTAT_DIP|ISTAT_SIP|
+           (hostdata->chip / 100 == 8 ? ISTAT_800_INTF : 0))) {
+       printk ("scsi%d : dropped interrupt for command %ld\n", host->host_no,
+           cmd->pid);
+       NCR53c7x0_intr (host->irq, NULL);
+       return SCSI_ABORT_BUSY;
+    }
+       
     save_flags(flags);
     cli();
+#if 0
+    if (cache_pid == cmd->pid) 
+       panic ("scsi%d : bloody fetus %d\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
@@ -3778,23 +5510,24 @@ int NCR53c7xx_abort (Scsi_Cmnd *cmd) {
  * pull the command out of the old queue, and call it aborted.
  */
 
-    for (curr = (volatile struct NCR53c7x0_cmd *) hostdata->issue_queue, 
-        prev = (volatile struct NCR53c7x0_cmd **) &(hostdata->issue_queue);
-        curr && curr->cmd != cmd; prev = (volatile struct NCR53c7x0_cmd **)
-        &(curr->next), curr = (volatile 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;
     }
 
@@ -3803,38 +5536,65 @@ int NCR53c7xx_abort (Scsi_Cmnd *cmd) {
  * commands.  If this is the case, drastic measures are called for.  
  */ 
 
-    for (curr = (volatile struct NCR53c7x0_cmd *) hostdata->running_list, 
-        prev = (volatile struct NCR53c7x0_cmd **) &(hostdata->running_list);
-        curr && curr->cmd != cmd; prev = (volatile struct NCR53c7x0_cmd **) 
-        &(curr->next), curr = (volatile struct NCR53c7x0_cmd *) curr->next);
+    for (curr = (struct NCR53c7x0_cmd *) hostdata->running_list, 
+        prev = (struct NCR53c7x0_cmd **) &(hostdata->running_list);
+        curr && curr->cmd != cmd; prev = (struct NCR53c7x0_cmd **) 
+         &(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", 
            host->host_no);
+/* 
+ * FIXME : We need to add an additional flag which indicates if a 
+ * command was ever counted as BUSY, so if we end up here we can
+ * decrement the busy count if and only if it is necessary.
+ */
+        --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;
 }
 
 /*
@@ -3847,45 +5607,74 @@ int NCR53c7xx_abort (Scsi_Cmnd *cmd) {
  *
  * Returns : 0 on success.
  */
-
-int
+int 
 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;
-    NCR53c7x0_local_setup(host);
+    struct NCR53c7x0_hostdata *hostdata = 
+       (struct NCR53c7x0_hostdata *) host->hostdata;
 
+    NCR53c7x0_local_setup(host);
     save_flags(flags);
+    cli();
     ncr_halt (host);
-    NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
-    mb();
-    udelay(25);        /* Minimum amount of time to assert RST */
-    NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
-    mb();
-    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) {
+           cmd->host_scribble = NULL;
            c->next = hostdata->free;
            hostdata->free = c;
-       }
-       cmd->result = DID_RESET << 16;
-       cmd->scsi_done(cmd);
+       } else
+           printk ("scsi%d: lost command %ld\n", host->host_no, cmd->pid);
+       cmd->SCp.buffer = (struct scatterlist *) nuke_list;
+       nuke_list = cmd;
+    }
+
+    NCR53c7x0_driver_init (host);
+    hostdata->soft_reset (host);
+    if (hostdata->resets == 0) 
+       disable(host);
+    else if (hostdata->resets != -1)
+       --hostdata->resets;
+    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;
@@ -3896,24 +5685,324 @@ NCR53c7xx_reset (Scsi_Cmnd *cmd) {
  * therefore shares the scsicam_bios_param function.
  */
 
-static void print_dsa (struct Scsi_Host *host, u32 *dsa) {
+/*
+ * Function : int insn_to_offset (Scsi_Cmnd *cmd, u32 *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, u32 *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 (!check_address ((unsigned long) ncmd, sizeof (struct NCR53c7x0_cmd)) &&
+       ((insn >= ncmd->data_transfer_start &&  
+           insn < ncmd->data_transfer_end) ||
+       (insn >= ncmd->residual &&
+           insn < (ncmd->residual + 
+               sizeof(ncmd->residual))))) {
+           ptr = bus_to_virt(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%p to 0x%p\n", 
+                       cmd->host->host_no, saved, 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(u32)) &&
+              (insn <= hostdata->script +
+               hostdata->E_end_data_transfer / sizeof(u32))) {
+       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;
+    u32 *ptr;
+    NCR53c7x0_local_setup (cmd->host);
+    for (i = 0; i < 2; ++i) {
+       if (check_address ((unsigned long) ncmd, 
+           sizeof (struct NCR53c7x0_cmd)) == -1) 
+           continue;
+       if (!i) {
+           where = "saved";
+           ptr = bus_to_virt(ncmd->saved_data_pointer);
+       } else {
+           where = "active";
+           ptr = bus_to_virt (NCR53c7x0_read32 (DSP_REG) -
+               NCR53c7x0_insn_size (NCR53c7x0_read8 (DCMD_REG)) *
+               sizeof(u32));
+       } 
+       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);
+           if (ncmd) {
+               size = print_insn (cmd->host, 
+                   bus_to_virt(ncmd->saved_data_pointer), "", 1);
+               print_insn (cmd->host, 
+                   bus_to_virt(ncmd->saved_data_pointer) + size * sizeof(u32),
+                   "", 1);
+           }
+       }
+    }
+}
+
+
+static void 
+print_dsa (struct Scsi_Host *host, u32 *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%p\n"
-           "        + %d : dsa_msgout length = %d, data = 0x%x\n" ,
-           host->host_no, dsa, hostdata->dsa_msgout,
+    if (check_address ((unsigned long) dsa, hostdata->dsa_end - 
+       hostdata->dsa_start) == -1) {
+       printk("scsi%d : bad dsa virt 0x%p\n", host->host_no, dsa);
+       return;
+    }
+    printk("%sscsi%d : dsa at phys 0x%lx (virt 0x%p)\n"
+           "        + %d : dsa_msgout length = %u, data = 0x%x (virt 0x%p)\n" ,
+           prefix ? prefix : "",
+           host->host_no,  virt_to_bus (dsa), dsa, hostdata->dsa_msgout,
            dsa[hostdata->dsa_msgout / sizeof(u32)],
-           dsa[hostdata->dsa_msgout / sizeof(u32) + 1]);
+           dsa[hostdata->dsa_msgout / sizeof(u32) + 1],
+           bus_to_virt (dsa[hostdata->dsa_msgout / sizeof(u32) + 1]));
 
     for (i = dsa[hostdata->dsa_msgout / sizeof(u32)],
-       ptr = bus_to_virt(dsa[hostdata->dsa_msgout / sizeof(u32) + 1]); i > 0;
+       ptr = bus_to_virt (dsa[hostdata->dsa_msgout / sizeof(u32) + 1]); 
+       i > 0 && !check_address ((unsigned long) ptr, 1);
        ptr += len, i -= len) {
        printk("               ");
        len = print_msg (ptr);
        printk("\n");
+       if (!len)
+           break;
+    }
+
+    printk("        + %d : select_indirect = 0x%x\n",
+       hostdata->dsa_select, dsa[hostdata->dsa_select / sizeof(u32)]);
+    cmd = (Scsi_Cmnd *) bus_to_virt(dsa[hostdata->dsa_cmnd / sizeof(u32)]);
+    printk("        + %d : dsa_cmnd = 0x%x ", hostdata->dsa_cmnd,
+          (u32) virt_to_bus(cmd));
+    if (cmd) {
+       printk("               result = 0x%x, target = %d, lun = %d, cmd = ",
+           cmd->result, cmd->target, cmd->lun);
+       print_command(cmd->cmnd);
+    } else
+       printk("\n");
+    printk("        + %d : dsa_next = 0x%x\n", hostdata->dsa_next,
+       dsa[hostdata->dsa_next / sizeof(u32)]);
+    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%x ", 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;
+    u32 *dsa, *next_dsa;
+    volatile u32 *current;
+    int left;
+    Scsi_Cmnd *cmd, *next_cmd;
+    unsigned long flags;
+
+    printk ("scsi%d : issue queue\n", host->host_no);
+
+    for (left = host->can_queue, cmd = (Scsi_Cmnd *) hostdata->issue_queue; 
+           left >= 0 && cmd; 
+           cmd = next_cmd) {
+       next_cmd = (Scsi_Cmnd *) cmd->SCp.ptr;
+       save_flags(flags);
+       cli();
+       if (cmd->host_scribble) {
+           if (check_address ((unsigned long) (cmd->host_scribble), 
+               sizeof (cmd->host_scribble)) == -1)
+               printk ("scsi%d: scsi pid %ld bad pointer to NCR53c7x0_cmd\n",
+                   host->host_no, cmd->pid);
+           /* print_dsa does sanity check on address, no need to check */
+           else
+               print_dsa (host, ((struct NCR53c7x0_cmd *) cmd->host_scribble)
+                   -> dsa, "");
+       } else 
+           printk ("scsi%d : scsi pid %ld for target %d lun %d has no NCR53c7x0_cmd\n",
+               host->host_no, cmd->pid, cmd->target, cmd->lun);
+       restore_flags(flags);
+    }
+
+    if (left <= 0) {
+       printk ("scsi%d : loop detected in issue queue\n",
+           host->host_no);
+    }
+
+    /*
+     * Traverse the NCR reconnect and start DSA structures, printing out 
+     * each element until we hit the end or detect a loop.  Currently,
+     * the reconnect structure is a linked list; and the start structure
+     * is an array.  Eventually, the reconnect structure will become a 
+     * list as well, since this simplifies the code.
+     */
+
+    printk ("scsi%d : schedule dsa array :\n", host->host_no);
+    for (left = host->can_queue, current = hostdata->schedule;
+           left > 0; current += 2, --left)
+       if (current[0] != hostdata->NOP_insn) 
+/* FIXME : convert pointer to dsa_begin to pointer to dsa. */
+           print_dsa (host, bus_to_virt (current[1] - 
+               (hostdata->E_dsa_code_begin - 
+               hostdata->E_dsa_code_template)), "");
+    printk ("scsi%d : end schedule dsa array\n", host->host_no);
+    
+    printk ("scsi%d : reconnect_dsa_head :\n", host->host_no);
+           
+    for (left = host->can_queue, 
+       dsa = bus_to_virt (hostdata->reconnect_dsa_head);
+       left >= 0 && dsa; 
+       dsa = next_dsa) {
+       save_flags (flags);
+       cli();
+       if (check_address ((unsigned long) dsa, sizeof(dsa)) == -1) {
+           printk ("scsi%d: bad DSA pointer 0x%p", host->host_no,
+               dsa);
+           next_dsa = NULL;
+       }
+       else 
+       {
+           next_dsa = bus_to_virt(dsa[hostdata->dsa_next / sizeof(u32)]);
+           print_dsa (host, dsa, "");
+       }
+       restore_flags(flags);
+    }
+    printk ("scsi%d : end reconnect_dsa_head\n", host->host_no);
+    if (left < 0)
+       printk("scsi%d: possible loop in ncr reconnect list\n",
+           host->host_no);
+}
+
+static void
+print_lots (struct Scsi_Host *host) {
+    NCR53c7x0_local_declare();
+    struct NCR53c7x0_hostdata *hostdata = 
+       (struct NCR53c7x0_hostdata *) host->hostdata;
+    u32 *dsp_next, *dsp, *dsa, dbc_dcmd;
+    unsigned char dcmd, sbcl;
+    int i, size;
+    NCR53c7x0_local_setup(host);
+
+    if ((dsp_next = bus_to_virt(NCR53c7x0_read32 (DSP_REG)))) {
+       dbc_dcmd = NCR53c7x0_read32(DBC_REG);
+       dcmd = (dbc_dcmd & 0xff000000) >> 24;
+       dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
+       dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
+       sbcl = NCR53c7x0_read8 (SBCL_REG);
+           
+       
+       printk ("scsi%d : DCMD|DBC=0x%x, DSA=0x%lx (virt 0x%p)\n"
+               "         DSPS=0x%x, TEMP=0x%x (virt 0x%p), 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, virt_to_bus(dsa), dsa, 
+           NCR53c7x0_read32(DSPS_REG), NCR53c7x0_read32(TEMP_REG), 
+           bus_to_virt (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 (virt 0x%p) ->\n", host->host_no, 
+           virt_to_bus(dsp), dsp);
+       for (i = 6; i > 0; --i, dsp += size)
+           size = print_insn (host, dsp, "", 1);
+       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));
+           print_dsa (host, dsa, "");
+       }
+
+#if 1
+       print_queues (host);
+#endif
     }
 }
 
@@ -3922,11 +6011,9 @@ static void print_dsa (struct Scsi_Host *host, u32 *dsa) {
  * 
  * 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();
@@ -3936,25 +6023,176 @@ shutdown (struct Scsi_Host *host) {
     NCR53c7x0_local_setup(host);
     save_flags (flags);
     cli();
+/* Get in a state where we can reset the SCSI bus */
     ncr_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);
-    mb();
     udelay(25);        /* Minimum amount of time to assert RST */
-    NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
-    mb();
+    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,
+ *     so we don't perturb hostdata.  We don't use a field of the 
+ *     NCR53c7x0_cmd structure since we may not have allocated one 
+ *     for the command causing the reset.) 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;
+    int i;
+    u32 *current;
+    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->next = hostdata->free;
+           hostdata->free = c;
+       }
+    }
+
+    if (free) { 
+       for (i = 0, current = (u32 *) hostdata->schedule; 
+           i < host->can_queue; ++i, current += 2) {
+           current[0] = hostdata->NOP_insn;
+           current[1] = 0xdeadbeef;
+       }
+       hostdata->current = 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)
+       ncr_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 ncr_halt (struct Scsi_Host *host)
@@ -3973,14 +6211,21 @@ ncr_halt (struct Scsi_Host *host) {
     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);
-    mb();
-    /* 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) {
@@ -3991,24 +6236,131 @@ ncr_halt (struct Scsi_Host *host) {
                tmp = NCR53c7x0_read8(SSTAT0_REG);
            }
        } else if (istat & ISTAT_DIP) {
-           NCR53c7x0_write8(hostdata->istat, 0);
-           mb();
            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;
 }
 
+/* 
+ * Function: event_name (int event)
+ * 
+ * Purpose: map event enum into user-readable strings.
+ */
+
+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,
+               (long) event.time.tv_sec, (long) event.time.tv_usec,
+               event.target, event.lun);
+           if (event.dsa) 
+               printk ("         event for dsa 0x%lx (virt 0x%p)\n", 
+                   virt_to_bus(event.dsa), event.dsa);
+           if (event.pid != -1) {
+               printk ("         event for pid %ld ", event.pid);
+               print_command (event.cmnd);
+           }
+       }
+    }
+}
+
+/*
+ * Function: check_address
+ *
+ * Purpose: Check to see if a possibly corrupt pointer will fault the 
+ *     kernel.
+ *
+ * Inputs: addr - address; size - size of area
+ *
+ * Returns: 0 if area is OK, -1 on error.
+ *
+ * NOTES: should be implemented in terms of vverify on kernels 
+ *     that have it.
+ */
+
+static int 
+check_address (unsigned long addr, int size) {
+    return (MAP_NR(addr) < 1 || MAP_NR(addr + size) > MAP_NR(high_memory) ?
+           -1 : 0);
+}
+
 #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;
@@ -4021,6 +6373,25 @@ int NCR53c7x0_release(struct Scsi_Host *host) {
        }
     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 we're going to loop, try to stop it to get a more accurate
+     * count of the leaked commands.
+     */
+       cmd->next = NULL;
+       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;
index cbdcfa4e09812f7e8aadf61d6df19e9c8cb1f3bd..2b4c5fed379608fef199e2ab29f55ddfc5c27208 100644 (file)
@@ -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.
 
 #ifndef NCR53c7x0_H
 #define NCR53c7x0_H
-
-#ifdef __alpha__
-
-# define ncr_readb(a)          ((unsigned int)readb((unsigned long)(a)))
-# define ncr_readw(a)          ((unsigned int)readw((unsigned long)(a)))
-# define ncr_readl(a)          ((unsigned int)readl((unsigned long)(a)))
-# define ncr_writeb(v,a)       (writeb((v), (unsigned long)(a)))
-# define ncr_writew(v,a)       (writew((v), (unsigned long)(a)))
-# define ncr_writel(v,a)       (writel((v), (unsigned long)(a)))
-
+#if !defined(LINUX_1_2) && !defined(LINUX_1_3)
+#include <linux/version.h>
+#if LINUX_VERSION_CODE > 65536 + 3 * 256
+#define LINUX_1_3
 #else
-
-# define ncr_readb(a)          (*(unsigned char*)(a))
-# define ncr_readw(a)          (*(unsigned short*)(a))
-# define ncr_readl(a)          (*(unsigned int*)(a))
-# define ncr_writeb(v,a)       (*(unsigned char*)(a) = (v))
-# define ncr_writew(v,a)       (*(unsigned short*)(a) = (v))
-# define ncr_writel(v,a)       (*(unsigned int*)(a) = (v))
-
+#define LINUX_1_2
+#endif
 #endif
 
 /* 
@@ -78,18 +66,68 @@ extern int NCR53c7xx_release(struct Scsi_Host *);
 #define NCR53c7xx_release NULL
 #endif
 
-#define NCR53c7xx {NULL, NULL, NULL, NULL,     \
-        "NCR53c{7,8}xx (rel 4)", 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 ,         \
-        /* present */ 0, /* unchecked isa dma */ 0, DISABLE_CLUSTERING} 
+#ifdef LINUX_1_2
+#define NCR53c7xx {NULL, NULL, "NCR53c{7,8}xx (rel 13)", NCR53c7xx_detect,\
+       NULL, /* info */ NULL, /* command, deprecated */ NULL,          \
+       NCR53c7xx_queue_command, NCR53c7xx_abort, NCR53c7xx_reset,      \
+       NULL /* slave attach */, scsicam_bios_param, /* can queue */ 24, \
+       /* id */ 7, 127 /* old SG_ALL */, /* cmd per lun */ 3,          \
+       /* present */ 0, /* unchecked isa dma */ 0, DISABLE_CLUSTERING} 
+#else
+#define NCR53c7xx {NULL, NULL, NULL, NULL, \
+        "NCR53c{7,8}xx (rel 13)", NCR53c7xx_detect,\
+        NULL, /* info */ NULL, /* command, deprecated */ NULL,         \
+       NCR53c7xx_queue_command, NCR53c7xx_abort, NCR53c7xx_reset,      \
+       NULL /* slave attach */, scsicam_bios_param, /* can queue */ 24, \
+       /* id */ 7, 127 /* old SG_ALL */, /* cmd per lun */ 3,          \
+       /* present */ 0, /* unchecked isa dma */ 0, DISABLE_CLUSTERING} 
+#endif
+
 #endif /* defined(HOSTS_C) || defined(MODULE) */ 
 
 #ifndef HOSTS_C
-/* Register addresses, ordered numerically */
+#ifdef LINUX_1_2
+/*
+ * Change virtual addresses to physical addresses and vv.
+ * These are trivial on the 1:1 Linux/i386 mapping (but if we ever
+ * make the kernel segment mapped at 0, we need to do translation
+ * on the i386 as well)
+ */
+extern inline unsigned long virt_to_phys(volatile void * address)
+{       
+       return (unsigned long) address;
+}       
+
+extern inline void * phys_to_virt(unsigned long address)
+{
+       return (void *) address;      
+}
+
+/*
+ * IO bus memory addresses are also 1:1 with the physical address
+ */
+#define virt_to_bus virt_to_phys
+#define bus_to_virt phys_to_virt
+
+/*
+ * readX/writeX() are used to access memory mapped devices. On some
+ * architectures the memory mapped IO stuff needs to be accessed
+ * differently. On the x86 architecture, we just read/write the
+ * memory location directly.
+ */
+#define readb(addr) (*(volatile unsigned char *) (addr))
+#define readw(addr) (*(volatile unsigned short *) (addr))
+#define readl(addr) (*(volatile unsigned int *) (addr))
+
+#define writeb(b,addr) ((*(volatile unsigned char *) (addr)) = (b))
+#define writew(b,addr) ((*(volatile unsigned short *) (addr)) = (b))
+#define writel(b,addr) ((*(volatile unsigned int *) (addr)) = (b))
+
+#define mb()
 
+#endif /* def LINUX_1_2 */
+
+/* Register addresses, ordered numerically */
 
 /* SCSI control 0 rw, default = 0xc0 */ 
 #define SCNTL0_REG             0x00    
@@ -113,7 +151,7 @@ extern int NCR53c7xx_release(struct Scsi_Host *);
 #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
@@ -198,15 +236,16 @@ extern int NCR53c7xx_release(struct Scsi_Host *);
 #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
@@ -334,6 +373,7 @@ extern int NCR53c7xx_release(struct Scsi_Host *);
 #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,
@@ -349,6 +389,8 @@ extern int NCR53c7xx_release(struct Scsi_Host *);
 #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 */
@@ -365,6 +407,7 @@ extern int NCR53c7xx_release(struct Scsi_Host *);
 #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,
@@ -374,6 +417,13 @@ extern int NCR53c7xx_release(struct Scsi_Host *);
 #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 */
@@ -867,7 +917,8 @@ extern int NCR53c7xx_release(struct Scsi_Host *);
 #define STIME1_REG_800         0x49
 #define STIME1_800_GEN_MASK    0x0f    /* General purpose timer */
 
-#define RESPID_REG_800         0x4a    /* Response ID, bit fielded */
+#define RESPID_REG_800         0x4a    /* Response ID, bit fielded.  8
+                                          bits on narrow chips, 16 on WIDE */
 
 #define STEST0_REG_800         0x4c    
 #define STEST0_800_SLT         0x08    /* Selection response logic test */
@@ -895,10 +946,6 @@ extern int NCR53c7xx_release(struct Scsi_Host *);
 #define STEST3_800_CSF         0x02    /* Clear SCSI FIFO */
 #define STEST3_800_STW         0x01    /* SCSI FIFO test write */
 
-
-
-
-
 #define OPTION_PARITY          0x1     /* Enable parity checking */
 #define OPTION_TAGGED_QUEUE    0x2     /* Enable SCSI-II tagged queuing */
 #define OPTION_700             0x8     /* Always run NCR53c700 scripts */
@@ -919,14 +966,12 @@ extern int NCR53c7xx_release(struct Scsi_Host *);
 #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 */
 #define OPTION_DEBUG_TEST2     0x20000 /* Run test 2 */
-
 #define OPTION_DEBUG_DUMP      0x40000 /* Dump commands */
 #define OPTION_DEBUG_TARGET_LIMIT 0x80000 /* Only talk to target+luns specified */
 #define OPTION_DEBUG_NCOMMANDS_LIMIT 0x100000 /* Limit the number of commands */
@@ -934,17 +979,41 @@ extern int NCR53c7xx_release(struct Scsi_Host *);
 #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. */
+#define OPTION_NO_PRINT_RACE 0x800000000LL     /* Don't print message when
+                                                  the reselect/WAIT DISCONNECT
+                                                  race condition hits */
 #if !defined(PERM_OPTIONS)
 #define PERM_OPTIONS 0
 #endif
                                
 struct NCR53c7x0_synchronous {
     u32 select_indirect;               /* Value used for indirect selection */
-    u32 script[6];                     /* Size ?? Script used when target is 
+    u32 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 
@@ -952,15 +1021,70 @@ struct NCR53c7x0_synchronous {
 #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 {
     u32 count;
     void *address;
 };
 
+enum ncr_event { 
+    EVENT_NONE = 0,
+/* 
+ * Order is IMPORTANT, since these must correspond to the event interrupts
+ * in 53c7,8xx.scr 
+ */
+
+    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;       
+    u32 *dsa;                  /* What's in the DSA register now (virt) */
+/* 
+ * 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 *real;                                /* Real, unaligned address for
+                                          free function */
+    void (* free)(void *, int);                /* Command to deallocate; NULL
                                           for structures allocated with
                                           scsi_register, etc. */
     Scsi_Cmnd *cmd;                    /* Associated Scsi_Cmnd 
@@ -971,7 +1095,15 @@ struct NCR53c7x0_cmd {
     int size;                          /* scsi_malloc'd size of this 
                                           structure */
 
-    int flags;
+    int flags;                         /* CMD_* 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
@@ -980,26 +1112,54 @@ struct NCR53c7x0_cmd {
                                         */
 
 
-    volatile struct NCR53c7x0_cmd *next, *prev;        
-                                       /* Linux maintained lists.  Note that
-                                          hostdata->free is a singly linked
-                                          list; the rest are doubly linked */
+    volatile struct NCR53c7x0_cmd *next; /* Linux maintained lists (free,
+                                           running, eventually finished */
                                         
 
-    long dsa_size; /* Size of DSA structure */
-
     u32 *data_transfer_start;          /* Start of data transfer routines */
     u32 *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.
+ */
 
-    u32 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.  
+ * 
+ * FIXME: we want to consider the case where we have odd-length 
+ *     scatter/gather buffers and a WIDE transfer, in which case 
+ *     we'll need to use the CHAIN MOVE instruction.  Ick.
+ */
+    u32 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.
                                         */
-            
+    u32 saved_residual[6];             /* Copy of old residual, so we 
+                                          can get another partial 
+                                          transfer and still recover 
+                                        */
+               
+    u32 saved_data_pointer;            /* Saved data pointer */
+
+    u32 dsa_next_addr;                 /* _Address_ of dsa_next field  
+                                          in this dsa for RISCy 
+                                          style constant. */
+
+    u32 dsa_addr;                      /* Address of dsa; RISCy style
+                                          constant */
+
     u32 dsa[0];                                /* Variable length (depending
                                           on host type, number of scatter /
                                           gather buffers, etc).  */
@@ -1025,10 +1185,10 @@ struct NCR53c7x0_break {
  * 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 
@@ -1054,15 +1214,9 @@ struct NCR53c7x0_hostdata {
                                           bit is being used to enable
                                           termination, etc. */
 
-    int chip;                          /* set to chip type */
-       /*
-        * NCR53c700 = 700
-        * NCR53c700-66 = 70066
-        * NCR53c710 = 710
-        * NCR53c720 = 720 
-        * NCR53c810 = 810
-        */
-
+    int chip;                          /* set to chip type; 700-66 is
+                                          700-66, rest are last three
+                                          digits of part number */
     /*
      * PCI bus, device, function, only for NCR53c8x0 chips.
      * pci_valid indicates that the PCI configuration information
@@ -1110,7 +1264,7 @@ struct NCR53c7x0_hostdata {
 
     int (* dstat_sir_intr)(struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd);
 
-    long dsa_size; /* Size of DSA structure */
+    int dsa_len; /* Size of DSA structure */
 
     /*
      * Location of DSA fields for the SCSI SCRIPT corresponding to this 
@@ -1131,9 +1285,10 @@ struct NCR53c7x0_hostdata {
     s32 dsa_msgout_other;
     s32 dsa_write_sync;
     s32 dsa_write_resume;
-    s32 dsa_jump_resume;
     s32 dsa_check_reselect;
     s32 dsa_status;
+    s32 dsa_saved_pointer;
+    s32 dsa_jump_dest;
 
     /* 
      * Important entry points that generic fixup code needs
@@ -1141,14 +1296,17 @@ struct NCR53c7x0_hostdata {
      */
 
     s32 E_accept_message;
+    s32 E_command_complete;            
+    s32 E_data_transfer;
     s32 E_dsa_code_template;
     s32 E_dsa_code_template_end;
-    s32 E_command_complete;            
+    s32 E_end_data_transfer;
     s32 E_msg_in;
     s32 E_initiator_abort;
     s32 E_other_transfer;
+    s32 E_other_in;
+    s32 E_other_out;
     s32 E_target_abort;
-    s32 E_schedule;                    
     s32 E_debug_break; 
     s32 E_reject_message;
     s32 E_respond_message;
@@ -1159,13 +1317,15 @@ struct NCR53c7x0_hostdata {
     s32 E_test_2;
     s32 E_test_3;
     s32 E_dsa_zero;
-    s32 E_dsa_jump_resume;
+    s32 E_cmdout_cmdout;
+    s32 E_wait_reselect;
+    s32 E_dsa_code_begin;
 
-    int options;                       /* Bitfielded set of options enabled */
+    long long options;                 /* Bitfielded set of options enabled */
     volatile u32 test_completed;       /* Test completed */
     int test_running;                  /* Test currently running */
-    int test_source;
-    volatile int test_dest;
+    s32 test_source;
+    volatile s32 test_dest;
 
     volatile int state;                        /* state of driver, only used for 
                                           OPTION_700 */
@@ -1186,6 +1346,7 @@ struct NCR53c7x0_hostdata {
                                         */
 
     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;
@@ -1213,7 +1374,7 @@ struct NCR53c7x0_hostdata {
                                           information for if 
                                           OPTION_DEBUG_DUMP is set */ 
 
-    unsigned char debug_lun_limit[8];  /* If OPTION_DEBUG_TARGET_LIMIT
+    unsigned char debug_lun_limit[16]; /* If OPTION_DEBUG_TARGET_LIMIT
                                           set, puke if commands are sent
                                           to other target/lun combinations */
 
@@ -1225,22 +1386,20 @@ struct NCR53c7x0_hostdata {
     volatile unsigned idle:1;                  /* set to 1 if idle */
 
     /* 
-     * Table of synchronous transfer parameters set on a per-target
+     * Table of synchronous+wide transfer parameters set on a per-target
      * basis.
-     * 
-     * XXX - do we need to increase this to 16 for the WIDE-SCSI
-     * flavors of the board?
      */
     
-    volatile struct NCR53c7x0_synchronous sync[8];
+    volatile struct NCR53c7x0_synchronous sync[16];
 
-    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_cmd;        /* currently connected 
+
+    volatile struct NCR53c7x0_cmd *current;    /* currently connected 
                                                   nexus, ONLY valid for
                                                   NCR53c700/NCR53c700-66
                                                 */
@@ -1256,10 +1415,11 @@ struct NCR53c7x0_hostdata {
                                                   */
     volatile int num_cmds;                     /* Number of commands 
                                                   allocated */
-    volatile unsigned char cmd_allocated[8];   /* Have we allocated commands
+    volatile int extra_allocate;
+    volatile unsigned char cmd_allocated[16];  /* Have we allocated commands
                                                   for this target yet?  If not,
                                                   do so ASAP */
-    volatile unsigned char busy[8][8];         /* number of commands 
+    volatile unsigned char busy[16][8];        /* number of commands 
                                                   executing on each target
                                                 */
     /* 
@@ -1272,20 +1432,32 @@ struct NCR53c7x0_hostdata {
                                                
 
     /* Shared variables between SCRIPT and host driver */
-    volatile u32 issue_dsa_head;
-                                               /* commands waiting to be 
-                                                  issued, insertions are 
-                                                  done by Linux driver,
-                                                  deletions are done by
-                                                  NCR */
-    u32 *issue_dsa_tail;                       /* issue queue tail pointer;
-                                                  used by Linux driver only */
+    volatile u32 *schedule;                    /* Array of JUMPs to dsa_begin
+                                                  routines of various DSAs.  
+                                                  When not in use, replace
+                                                  with jump to next slot */
+
+
     volatile unsigned char msg_buf[16];                /* buffer for messages
                                                   other than the command
                                                   complete message */
-    volatile u32 reconnect_dsa_head;
-                                               /* disconnected commands,
-                                                  maintained by NCR */
+
+    /* Per-target default synchronous and WIDE messages */
+    volatile unsigned char synchronous_want[16][5];
+    volatile unsigned char wide_want[16][4];
+
+    /* Bit fielded set of targets we want to speak synchronously with */ 
+    volatile u16 initiate_sdtr;        
+    /* Bit fielded set of targets we want to speak wide with */
+    volatile u16 initiate_wdtr;
+    /* Bit fielded list of targets we've talked to. */
+    volatile u16 talked_to;
+    /* Array of bit-fielded lun lists that we need to request_sense */
+    volatile unsigned char request_sense[16];
+
+    u32 addr_reconnect_dsa_head;               /* RISCy style constant,
+                                                  address of following */
+    volatile u32 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 
@@ -1294,10 +1466,21 @@ struct NCR53c7x0_hostdata {
 
     s32 NCR53c7xx_zero;
     s32 NCR53c7xx_sink;
+    u32 NOP_insn;
     char NCR53c7xx_msg_reject;
     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 */
+
+    u32 *abort_script;
+
     int script_count;                          /* Size of script in words */
     u32 script[0];                             /* Relocated SCSI script */
 
@@ -1327,37 +1510,38 @@ struct NCR53c7x0_hostdata {
 
 #define NCR53c7x0_read8(address)                                       \
     (NCR53c7x0_memory_mapped ?                                                 \
-       ncr_readb(NCR53c7x0_address_memory + (address))  :              \
+       (unsigned int)readb(NCR53c7x0_address_memory + (address)) :     \
        inb(NCR53c7x0_address_io + (address)))
 
 #define NCR53c7x0_read16(address)                                      \
     (NCR53c7x0_memory_mapped ?                                                 \
-       ncr_readw(NCR53c7x0_address_memory + (address))  :              \
+       (unsigned int)readw(NCR53c7x0_address_memory + (address)) :     \
        inw(NCR53c7x0_address_io + (address)))
 
 #define NCR53c7x0_read32(address)                                      \
     (NCR53c7x0_memory_mapped ?                                                 \
-       ncr_readl(NCR53c7x0_address_memory + (address))  :              \
+       (unsigned int) readl(NCR53c7x0_address_memory + (address)) :    \
        inl(NCR53c7x0_address_io + (address)))
 
 #define NCR53c7x0_write8(address,value)                                \
     (NCR53c7x0_memory_mapped ?                                                 \
-       ncr_writeb((value), NCR53c7x0_address_memory + (address)) :     \
+     ({writeb((value), NCR53c7x0_address_memory + (address)); mb();}) :        \
        outb((value), NCR53c7x0_address_io + (address)))
 
 #define NCR53c7x0_write16(address,value)                               \
     (NCR53c7x0_memory_mapped ?                                                 \
-       ncr_writew((value), NCR53c7x0_address_memory + (address)) :     \
+     ({writew((value), NCR53c7x0_address_memory + (address)); mb();}) :        \
        outw((value), NCR53c7x0_address_io + (address)))
 
 #define NCR53c7x0_write32(address,value)                               \
     (NCR53c7x0_memory_mapped ?                                                 \
-       ncr_writel((value), NCR53c7x0_address_memory + (address)) :     \
+     ({writel((value), NCR53c7x0_address_memory + (address)); mb();}) :        \
        outl((value), NCR53c7x0_address_io + (address)))
 
+/* Patch arbitrary 32 bit words in the script */
 #define patch_abs_32(script, offset, symbol, value)                    \
        for (i = 0; i < (sizeof (A_##symbol##_used) / sizeof            \
-           (u32)); ++i) {                                              \
+           (u32)); ++i) {                                      \
            (script)[A_##symbol##_used[i] - (offset)] += (value);       \
            if (hostdata->options & OPTION_DEBUG_FIXUP)                 \
              printk("scsi%d : %s reference %d at 0x%x in %s is now 0x%x\n",\
@@ -1366,26 +1550,39 @@ struct NCR53c7x0_hostdata {
                (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            \
-           (u32)); ++i)                                                \
+           (u32)); ++i)                                        \
            (script)[A_##symbol##_used[i] - (offset)] =                 \
                ((script)[A_##symbol##_used[i] - (offset)] &            \
                ~DBC_RWRI_IMMEDIATE_MASK) |                             \
                (((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            \
+           (u32)); ++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(u32)  \
-               + (word)] = (value);                                    \
+           + (word)] = (value);                                        \
        if (hostdata->options & OPTION_DEBUG_DSA)                       \
            printk("scsi : dsa %s symbol %s(%d) word %d now 0x%x\n",    \
-               #dsa, #symbol, hostdata->##symbol,                      \
-               (word), (u32)(value));                          \
+               #dsa, #symbol, hostdata->##symbol,                      \
+               (word), (u32) (value));                                 \
        }
-    
 
+/* Paranoid people could use panic() here. */
+#define FATAL(host) shutdown((host));
 
 #endif /* NCR53c7x0_C */
 #endif /* NCR53c7x0_H */
index e6e233fcfefbfe717c96b4aad4bc2f6411d4c029..ce396027a78a94ee45c11f0c9ef600302e96388d 100644 (file)
@@ -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 
 ; NCR53c800 series have a unique combination of features, making a 
 ; a standard ingoing/outgoing mailbox system, costly, I've modified it.
 ;
-; - Commands are stored in a linked list, rather than placed in 
-;      arbitrary mailboxes.  This simplifies the amount of processing
-;      that must be done by the NCR53c810.
-;
 ; - 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,
 ; 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
+ABSOLUTE dsa_temp_addr_new_value = 0   ; Address of value for JUMP operand
+ABSOLUTE dsa_temp_addr_array_value = 0         ; Address to copy to
+ABSOLUTE dsa_temp_addr_dsa_value = 0   ; Address of this DSA value
+
+;
+; 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:
+ENTRY dsa_code_begin
+dsa_code_begin:
+       MOVE dmode_memory_to_ncr TO DMODE
+       MOVE MEMORY 4, dsa_temp_addr_dsa_value, addr_scratch
+       MOVE dmode_memory_to_memory TO DMODE
+       CALL scratch_to_dsa
+       CALL select
+; Handle the phase mismatch which may have resulted from the 
+; MOVE FROM dsa_msgout if we returned here.  The CLEAR ATN 
+; may or may not be necessary, and we should update script_asm.pl
+; to handle multiple pieces.
+    CLEAR ATN
+    CLEAR ACK
 
-; Define DSA structure used for mailboxes
+; Replace second operand with address of JUMP instruction dest operand
+; in schedule table for this DSA.  Becomes dsa_jump_dest in 53c7,8xx.c.
+ENTRY dsa_code_fix_jump
+dsa_code_fix_jump:
+       MOVE MEMORY 4, NOP_insn, 0
+       JUMP select_done
 
 ; 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 whether 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.
@@ -165,18 +276,20 @@ ABSOLUTE dsa_msgout_other = 104   ; len 8 table indirect for normal message out
                                ; (Synchronous transfer negotiation, etc).
 ABSOLUTE dsa_end = 112
 
+ABSOLUTE schedule = 0          ; Array of JUMP dsa_begin or JUMP (next),
+                               ; terminated by a call to JUMP wait_reselect
+
 ; 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 +324,56 @@ ABSOLUTE int_norm_disconnected = 0x02030000       ; Disconnected
 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
+ABSOLUTE NOP_insn = 0                  ; NOP instruction
 
-; 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 +403,15 @@ ABSOLUTE reselected_tag = 0
 
 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,48 +430,20 @@ dsa_schedule_insert:
     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
-
-; 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.
-;
-; INPUTS : issue_dsa_head - list of new commands
-;
-; CALLS : OK
-;
-; MODIFIES : SCRATCH, DSA 
-;
-; EXITS : if the issue_dsa_head pointer is non-NULL, control
-;      is passed to select.  Otherwise, control is passed to 
-;      wait_reselect.
-
-
-ENTRY schedule
-schedule:
-    ; Point DSA at the current head of the issue queue.
-    MOVE dmode_memory_to_ncr TO DMODE
-    MOVE MEMORY 4, issue_dsa_head, addr_scratch
-    MOVE dmode_memory_to_memory TO DMODE
-
-    CALL scratch_to_dsa
-
+#ifdef EVENTS
+    INT int_EVENT_DISCONNECT;
+#endif
 #if 0
-    INT int_debug_dsa_loaded
+    INT int_debug_disconnected
 #endif
-    ; Check for a null pointer.
-    MOVE DSA0 TO SFBR
-    JUMP select, IF NOT 0
-    MOVE DSA1 TO SFBR
-    JUMP select, IF NOT 0
-    MOVE DSA2 TO SFBR
-    JUMP select, IF NOT 0
-    MOVE DSA3 TO SFBR
-    JUMP wait_reselect, IF 0
-
-    
-#else /* (CHIP != 700) && (CHIP != 70066) */
-#endif /* (CHIP != 700) && (CHIP != 70066) */
+    JUMP schedule
+#endif 
 
 ;
 ; select
@@ -340,20 +459,28 @@ schedule:
 ;
 ; INPUTS : DSA - SCSI command, issue_dsa_head
 ;
-; CALLS : OK
+; CALLS : NOT OK
 ;
 ; MODIFIES : SCRATCH, issue_dsa_head
 ;
 ; EXITS : on reselection or selection, go to select_failed
-;      otherwise, fall through to data_transfer.  If a MSG_IN
-;      phase occurs before 
+;      otherwise, RETURN so control is passed back to 
+;      dsa_begin.
 ;
 
 ENTRY select
 select:
 
 #if 0
+#ifdef EVENTS
+    INT int_EVENT_BEFORE_SELECT
+#endif
+#endif
+
+#if 0
+#ifdef DEBUG
     INT int_debug_scheduled
+#endif
 #endif
     CLEAR TARGET
 
@@ -381,46 +508,29 @@ select_msgout:
     MOVE 0, 0, WHEN MSGOUT
 #endif
 
-#if (CHIP != 700) && (CHIP != 70066)
-
-    ; Calculate address of dsa_next field
-
-    CALL dsa_to_scratch
-
-    MOVE SCRATCH0 + dsa_next TO SCRATCH0
-    MOVE SCRATCH1 + 0 TO SCRATCH1 WITH CARRY
-    MOVE SCRATCH2 + 0 TO SCRATCH2 WITH CARRY
-    MOVE SCRATCH3 + 0 TO SCRATCH3 WITH CARRY
-
-    ; Patch memory to memory move
-    move dmode_ncr_to_memory TO DMODE
-    MOVE MEMORY 4, addr_scratch, issue_remove + 4
-
+#ifdef EVENTS
+   INT int_EVENT_SELECT
+#endif
+   RETURN
 
-    ; And rewrite the issue_dsa_head pointer.
-    MOVE dmode_memory_to_memory TO DMODE
-issue_remove:
-;      The actual UPDATE of the issue_dsa_head variable is 
-;      atomic, with all of the setup code being irrelevant to
-;      whether the updated value being the old or new contents of 
-;      dsa_next field.
-;
-;      To insure synchronization, the host system merely needs to 
-;      do a XCHG instruction with interrupts disabled on the 
-;      issue_dsa_head memory address.
-;
-;      The net effect will be that the XCHG instruction will return
-;      either a non-NULL value, indicating that the NCR chip will not
-;      go into the idle loop when this command DISCONNECTS, or a NULL
-;      value indicating that the NCR wrote first and that the Linux
-;      code must rewrite the issue_dsa_head pointer and set SIG_P.
-;      
+; 
+; select_done
+; 
+; PURPOSE: continue on to normal data transfer; called as the exit 
+;      point from dsa_begin.
+;
+; INPUTS: dsa
+;
+; CALLS: OK
+;
+;
 
+select_done:
 
-    MOVE MEMORY 4, 0, issue_dsa_head
-#endif /* (CHIP != 700) && (CHIP != 70066) */
-#if 0
-INT int_debug_head
+#ifdef DEBUG
+ENTRY select_check_dsa
+select_check_dsa:
+    INT int_debug_check_dsa
 #endif
 
 ; After a successful selection, we should get either a CMD phase or 
@@ -448,22 +558,31 @@ cmdout_cmdout:
 
 ;
 ; 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 +592,22 @@ cmdout_cmdout:
 ;      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 +636,52 @@ do_datain:
     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 +689,17 @@ other_transfer:
     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 +716,6 @@ other_transfer:
 ;      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 +728,28 @@ other_transfer:
 ;      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 +758,89 @@ munge_msg:
     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.
+; 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 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
@@ -666,11 +879,12 @@ reject_message:
 
 ENTRY accept_message
 accept_message:
+    CLEAR ATN
     CLEAR ACK
     RETURN
 
 ENTRY respond_message
-msg_respond:
+respond_message:
     SET ATN
     CLEAR ACK
     MOVE FROM dsa_msgout_other, WHEN MSG_OUT
@@ -716,7 +930,7 @@ command_complete_msgin:
     MOVE SCNTL2 & 0x7f TO SCNTL2
     CLEAR ACK
 #if (CHIP != 700) && (CHIP != 70066)
-    MOVE SCRATCH0 TO SFBR                      
+    WAIT DISCONNECT
 
 ;
 ; The SCSI specification states that when a UNIT ATTENTION condition
@@ -735,15 +949,17 @@ command_complete_msgin:
 ; must automatically issue the request sense command.
 
 #if 0
+    MOVE SCRATCH0 TO SFBR                      
     JUMP command_failed, IF 0x02
 #endif
     INTFLY
 #endif /* (CHIP != 700) && (CHIP != 70066) */
-    WAIT DISCONNECT
+#ifdef EVENTS
+    INT int_EVENT_COMPLETE
+#endif
 #if (CHIP != 700) && (CHIP != 70066)
     JUMP schedule
 command_failed:
-    WAIT DISCONNECT
     INT int_err_check_condition
 #else
     INT int_norm_command_complete
@@ -779,23 +995,28 @@ command_failed:
 ;      selected, the host system is interrupted with an 
 ;      int_err_selected which is usually responded to by
 ;      setting DSP to the target_abort address.
-    
+
+ENTRY wait_reselect
 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 +1030,13 @@ reselected_notag:
     ; 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 +1049,6 @@ reselected_check_next:
     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 +1061,7 @@ reselected_not_end:
     ; 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 +1079,22 @@ reselected_check:
 
 ;
 ;
+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 +1112,41 @@ selected:
 ; 
 
 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 +1215,39 @@ target_abort:
 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
index e0739d20c004a36d9b2395d479ba45ff345b1d87..c15655b37b184d19aa3dbd6ff69a05f080463573 100644 (file)
@@ -1,14 +1,16 @@
 u32 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 @@ u32 SCRIPT[] = {
 ;      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 
@@ -71,13 +72,17 @@ u32 SCRIPT[] = {
 ; NCR53c800 series have a unique combination of features, making a 
 ; a standard ingoing/outgoing mailbox system, costly, I've modified it.
 ;
-; - Commands are stored in a linked list, rather than placed in 
-;      arbitrary mailboxes.  This simplifies the amount of processing
-;      that must be done by the NCR53c810.
-;
 ; - 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,52 +91,214 @@ u32 SCRIPT[] = {
 ; 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
+ABSOLUTE dsa_temp_addr_new_value = 0   ; Address of value for JUMP operand
+ABSOLUTE dsa_temp_addr_array_value = 0         ; Address to copy to
+ABSOLUTE dsa_temp_addr_dsa_value = 0   ; Address of this DSA value
+
+;
+; 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:
+ENTRY dsa_code_begin
+dsa_code_begin:
+       MOVE dmode_memory_to_ncr TO DMODE
 
-; Define DSA structure used for mailboxes
+at 0x00000000 : */     0x78380000,0x00000000,
+/*
+       MOVE MEMORY 4, dsa_temp_addr_dsa_value, addr_scratch
+
+at 0x00000002 : */     0xc0000004,0x00000000,0x00000000,
+/*
+       MOVE dmode_memory_to_memory TO DMODE
+
+at 0x00000005 : */     0x78380000,0x00000000,
+/*
+       CALL scratch_to_dsa
+
+at 0x00000007 : */     0x88080000,0x00000960,
+/*
+       CALL select
+
+at 0x00000009 : */     0x88080000,0x000001fc,
+/*
+; Handle the phase mismatch which may have resulted from the 
+; MOVE FROM dsa_msgout if we returned here.  The CLEAR ATN 
+; may or may not be necessary, and we should update script_asm.pl
+; to handle multiple pieces.
+    CLEAR ATN
+
+at 0x0000000b : */     0x60000008,0x00000000,
+/*
+    CLEAR ACK
+
+at 0x0000000d : */     0x60000040,0x00000000,
+/*
+
+; Replace second operand with address of JUMP instruction dest operand
+; in schedule table for this DSA.  Becomes dsa_jump_dest in 53c7,8xx.c.
+ENTRY dsa_code_fix_jump
+dsa_code_fix_jump:
+       MOVE MEMORY 4, NOP_insn, 0
+
+at 0x0000000f : */     0xc0000004,0x00000000,0x00000000,
+/*
+       JUMP select_done
+
+at 0x00000012 : */     0x80080000,0x00000224,
+/*
 
 ; 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 0x00000014 : */     0xc0000004,0x00000000,0x00000758,
 /*
-
        MOVE dmode_memory_to_ncr TO DMODE       
 
-at 0x00000003 : */     0x78380000,0x00000000,
+at 0x00000017 : */     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,
+at 0x00000019 : */     0xc0000004,0x00000000,0x00000000,
 /*
        MOVE dmode_memory_to_memory TO DMODE
 
-at 0x00000008 : */     0x78380000,0x00000000,
+at 0x0000001c : */     0x78380000,0x00000000,
 /*
        CALL scratch_to_dsa
 
-at 0x0000000a : */     0x88080000,0x00000830,
+at 0x0000001e : */     0x88080000,0x00000960,
 /*
        JUMP reselected_check_next
 
-at 0x0000000c : */     0x80080000,0x000005ac,
+at 0x00000020 : */     0x80080000,0x000006a4,
+/*
+
+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 0x00000022 : */     0x78380000,0x00000000,
+/*
+       MOVE MEMORY 4, addr_temp, dsa_temp_addr_saved_pointer
+
+at 0x00000024 : */     0xc0000004,0x00000000,0x00000000,
+/*
+       MOVE dmode_memory_to_memory TO DMODE
+
+at 0x00000027 : */     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 0x00000029 : */     0xc0000018,0x00000000,0x00000000,
+/*
+        CLEAR ACK
+
+at 0x0000002c : */     0x60000040,0x00000000,
+/*
+
+
+
+       RETURN
+
+at 0x0000002e : */     0x90080000,0x00000000,
+/*
+ABSOLUTE dsa_restore_pointers = 0
+ENTRY dsa_code_restore_pointers
+dsa_code_restore_pointers:
+       MOVE dmode_memory_to_ncr TO DMODE
+
+at 0x00000030 : */     0x78380000,0x00000000,
+/*
+       MOVE MEMORY 4, dsa_temp_addr_saved_pointer, addr_temp
+
+at 0x00000032 : */     0xc0000004,0x00000000,0x00000000,
+/*
+       MOVE dmode_memory_to_memory TO DMODE
+
+at 0x00000035 : */     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 0x00000037 : */     0xc0000018,0x00000000,0x00000000,
+/*
+        CLEAR ACK
+
+at 0x0000003a : */     0x60000040,0x00000000,
+/*
+
+
+
+       RETURN
+
+at 0x0000003c : */     0x90080000,0x00000000,
 /*
 
 ABSOLUTE dsa_check_reselect = 0
@@ -141,75 +308,79 @@ ENTRY dsa_code_check_reselect
 dsa_code_check_reselect:
        MOVE SSID TO SFBR               ; SSID contains 3 bit target ID
 
-at 0x0000000e : */     0x720a0000,0x00000000,
+at 0x0000003e : */     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 0x00000040 : */     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 0x00000042 : */     0x78380000,0x00000000,
 /*
-       MOVE MEMORY 1, reselected_identify, addr_sfbr
+       MOVE MEMORY 1, reselected_identify, addr_scratch
 
-at 0x00000014 : */     0xc0000001,0x00000000,0x00000000,
+at 0x00000044 : */     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 0x00000047 : */     0x78380000,0x00000000,
 /*
-       MOVE dmode_memory_to_memory TO DMODE
+       MOVE SCRATCH0 TO SFBR
 
-at 0x00000019 : */     0x78380000,0x00000000,
+at 0x00000049 : */     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 0x0000004b : */     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 0x0000004d : */     0xc0000004,0x00000000,0x00000754,
 /*
        CALL reselected_ok
 
-at 0x0000001e : */     0x88080000,0x00000658,
+at 0x00000050 : */     0x88080000,0x00000750,
 /*
        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,
+at 0x00000052 : */     0x88080000,0x00000000,
 /*
-ENTRY dsa_zero
-dsa_zero:
-       MOVE dmode_ncr_to_memory TO DMODE                       ; 8
+; Release ACK on the IDENTIFY message _after_ we've set the synchronous 
+; transfer parameters! 
+       CLEAR ACK
 
-at 0x00000024 : */     0x78380000,0x00000000,
+at 0x00000054 : */     0x60000040,0x00000000,
 /*
-       MOVE MEMORY 4, addr_temp, dsa_temp_jump_resume          ; 16    
+; Implicitly restore pointers on reselection, so a RETURN
+; will transfer control back to the right spot.
+       CALL REL (dsa_code_restore_pointers)
 
-at 0x00000026 : */     0xc0000004,0x00000000,0x00000000,
+at 0x00000056 : */     0x88880000,0x00ffff60,
 /*
-       MOVE dmode_memory_to_memory TO DMODE                    ; 28
+       RETURN
 
-at 0x00000029 : */     0x78380000,0x00000000,
-/*
-       JUMP dsa_schedule                                       ; 36
-
-at 0x0000002b : */     0x80080000,0x000000b4,
+at 0x00000058 : */     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.
@@ -227,18 +398,20 @@ ABSOLUTE dsa_msgout_other = 104   ; len 8 table indirect for normal message out
                                ; (Synchronous transfer negotiation, etc).
 ABSOLUTE dsa_end = 112
 
+ABSOLUTE schedule = 0          ; Array of JUMP dsa_begin or JUMP (next),
+                               ; terminated by a call to JUMP wait_reselect
+
 ; 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 +446,27 @@ ABSOLUTE int_norm_disconnected = 0x02030000       ; Disconnected
 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
+ABSOLUTE NOP_insn = 0                  ; NOP instruction
 
-; 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,151 +497,96 @@ ABSOLUTE reselected_tag = 0
 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 0x0000005a : */     0x88080000,0x00000918,
 /*
-; 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 0x0000005c : */     0x7e343000,0x00000000,
 /*
     MOVE SCRATCH1 + 0 TO SCRATCH1 WITH CARRY
 
-at 0x00000031 : */     0x7f350000,0x00000000,
+at 0x0000005e : */     0x7f350000,0x00000000,
 /*
     MOVE SCRATCH2 + 0 TO SCRATCH2 WITH CARRY
 
-at 0x00000033 : */     0x7f360000,0x00000000,
+at 0x00000060 : */     0x7f360000,0x00000000,
 /*
     MOVE SCRATCH3 + 0 TO SCRATCH3 WITH CARRY
 
-at 0x00000035 : */     0x7f370000,0x00000000,
+at 0x00000062 : */     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 0x00000064 : */     0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, addr_scratch, dsa_schedule_insert + 8
 
-at 0x00000039 : */     0xc0000004,0x00000000,0x00000100,
+at 0x00000066 : */     0xc0000004,0x00000000,0x000001b4,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x0000003c : */     0x78380000,0x00000000,
+at 0x00000069 : */     0x78380000,0x00000000,
 /*
 dsa_schedule_insert:
     MOVE MEMORY 4, reconnect_dsa_head, 0 
 
-at 0x0000003e : */     0xc0000004,0x00000000,0x00000000,
+at 0x0000006b : */     0xc0000004,0x00000000,0x00000000,
 /*
 
 ; And update the head pointer.
     CALL dsa_to_scratch
 
-at 0x00000041 : */     0x88080000,0x000007e8,
+at 0x0000006e : */     0x88080000,0x00000918,
 /*
     MOVE dmode_ncr_to_memory TO DMODE  
 
-at 0x00000043 : */     0x78380000,0x00000000,
+at 0x00000070 : */     0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, addr_scratch, reconnect_dsa_head
 
-at 0x00000045 : */     0xc0000004,0x00000000,0x00000000,
+at 0x00000072 : */     0xc0000004,0x00000000,0x00000000,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x00000048 : */     0x78380000,0x00000000,
+at 0x00000075 : */     0x78380000,0x00000000,
 /*
-    WAIT DISCONNECT
 
-at 0x0000004a : */     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.
-;
-; INPUTS : issue_dsa_head - list of new commands
-;
-; CALLS : OK
-;
-; MODIFIES : SCRATCH, DSA 
-;
-; EXITS : if the issue_dsa_head pointer is non-NULL, control
-;      is passed to select.  Otherwise, control is passed to 
-;      wait_reselect.
-
-
-ENTRY schedule
-schedule:
-    ; Point DSA at the current head of the issue queue.
-    MOVE dmode_memory_to_ncr TO DMODE
-
-at 0x0000004c : */     0x78380000,0x00000000,
-/*
-    MOVE MEMORY 4, issue_dsa_head, addr_scratch
+    MOVE SCNTL2 & 0x7f TO SCNTL2
 
-at 0x0000004e : */     0xc0000004,0x00000000,0x00000000,
+at 0x00000077 : */     0x7c027f00,0x00000000,
 /*
-    MOVE dmode_memory_to_memory TO DMODE
+    CLEAR ACK
 
-at 0x00000051 : */     0x78380000,0x00000000,
+at 0x00000079 : */     0x60000040,0x00000000,
 /*
 
-    CALL scratch_to_dsa
+    WAIT DISCONNECT
 
-at 0x00000053 : */     0x88080000,0x00000830,
+at 0x0000007b : */     0x48000000,0x00000000,
 /*
 
 
 
 
-    ; Check for a null pointer.
-    MOVE DSA0 TO SFBR
-
-at 0x00000055 : */     0x72100000,0x00000000,
-/*
-    JUMP select, IF NOT 0
 
-at 0x00000057 : */     0x80040000,0x00000194,
-/*
-    MOVE DSA1 TO SFBR
-
-at 0x00000059 : */     0x72110000,0x00000000,
-/*
-    JUMP select, IF NOT 0
 
-at 0x0000005b : */     0x80040000,0x00000194,
-/*
-    MOVE DSA2 TO SFBR
-
-at 0x0000005d : */     0x72120000,0x00000000,
-/*
-    JUMP select, IF NOT 0
-
-at 0x0000005f : */     0x80040000,0x00000194,
-/*
-    MOVE DSA3 TO SFBR
+    JUMP schedule
 
-at 0x00000061 : */     0x72130000,0x00000000,
+at 0x0000007d : */     0x80080000,0x00000000,
 /*
-    JUMP wait_reselect, IF 0
-
-at 0x00000063 : */     0x800c0000,0x00000560,
-/*
-
-    
-
 
 
 ;
@@ -480,13 +603,13 @@ at 0x00000063 : */        0x800c0000,0x00000560,
 ;
 ; INPUTS : DSA - SCSI command, issue_dsa_head
 ;
-; CALLS : OK
+; CALLS : NOT OK
 ;
 ; MODIFIES : SCRATCH, issue_dsa_head
 ;
 ; EXITS : on reselection or selection, go to select_failed
-;      otherwise, fall through to data_transfer.  If a MSG_IN
-;      phase occurs before 
+;      otherwise, RETURN so control is passed back to 
+;      dsa_begin.
 ;
 
 ENTRY select
@@ -495,9 +618,17 @@ select:
 
 
 
+
+
+
+
+
+
+
+
     CLEAR TARGET
 
-at 0x00000065 : */     0x60000200,0x00000000,
+at 0x0000007f : */     0x60000200,0x00000000,
 /*
 
 ; XXX
@@ -514,17 +645,17 @@ at 0x00000065 : */        0x60000200,0x00000000,
 
     SELECT ATN FROM dsa_select, select_failed
 
-at 0x00000067 : */     0x4300003c,0x000006a4,
+at 0x00000081 : */     0x4300003c,0x00000794,
 /*
     JUMP select_msgout, WHEN MSG_OUT
 
-at 0x00000069 : */     0x860b0000,0x000001ac,
+at 0x00000083 : */     0x860b0000,0x00000214,
 /*
 ENTRY select_msgout
 select_msgout:
     MOVE FROM dsa_msgout, WHEN MSG_OUT
 
-at 0x0000006b : */     0x1e000000,0x00000040,
+at 0x00000085 : */     0x1e000000,0x00000040,
 /*
 
 
@@ -535,68 +666,27 @@ at 0x0000006b : */        0x1e000000,0x00000040,
 
 
 
-    ; Calculate address of dsa_next field
-
-    CALL dsa_to_scratch
-
-at 0x0000006d : */     0x88080000,0x000007e8,
-/*
-
-    MOVE SCRATCH0 + dsa_next TO SCRATCH0
-
-at 0x0000006f : */     0x7e343000,0x00000000,
-/*
-    MOVE SCRATCH1 + 0 TO SCRATCH1 WITH CARRY
-
-at 0x00000071 : */     0x7f350000,0x00000000,
-/*
-    MOVE SCRATCH2 + 0 TO SCRATCH2 WITH CARRY
-
-at 0x00000073 : */     0x7f360000,0x00000000,
-/*
-    MOVE SCRATCH3 + 0 TO SCRATCH3 WITH CARRY
-
-at 0x00000075 : */     0x7f370000,0x00000000,
-/*
-
-    ; Patch memory to memory move
-    move dmode_ncr_to_memory TO DMODE
 
-at 0x00000077 : */     0x78380000,0x00000000,
-/*
-    MOVE MEMORY 4, addr_scratch, issue_remove + 4
+   RETURN
 
-at 0x00000079 : */     0xc0000004,0x00000000,0x000001fc,
+at 0x00000087 : */     0x90080000,0x00000000,
 /*
 
-
-    ; And rewrite the issue_dsa_head pointer.
-    MOVE dmode_memory_to_memory TO DMODE
-
-at 0x0000007c : */     0x78380000,0x00000000,
-/*
-issue_remove:
-;      The actual UPDATE of the issue_dsa_head variable is 
-;      atomic, with all of the setup code being irrelevant to
-;      whether the updated value being the old or new contents of 
-;      dsa_next field.
+; 
+; select_done
+; 
+; PURPOSE: continue on to normal data transfer; called as the exit 
+;      point from dsa_begin.
+;
+; INPUTS: dsa
+;
+; CALLS: OK
 ;
-;      To insure synchronization, the host system merely needs to 
-;      do a XCHG instruction with interrupts disabled on the 
-;      issue_dsa_head memory address.
 ;
-;      The net effect will be that the XCHG instruction will return
-;      either a non-NULL value, indicating that the NCR chip will not
-;      go into the idle loop when this command DISCONNECTS, or a NULL
-;      value indicating that the NCR wrote first and that the Linux
-;      code must rewrite the issue_dsa_head pointer and set SIG_P.
-;      
 
+select_done:
 
-    MOVE MEMORY 4, 0, issue_dsa_head
 
-at 0x0000007e : */     0xc0000004,0x00000000,0x00000000,
-/*
 
 
 
@@ -607,27 +697,27 @@ at 0x0000007e : */        0xc0000004,0x00000000,0x00000000,
 
     JUMP cmdout, WHEN CMD
 
-at 0x00000081 : */     0x820b0000,0x00000224,
+at 0x00000089 : */     0x820b0000,0x00000244,
 /*
     INT int_err_unexpected_phase, WHEN NOT MSG_IN 
 
-at 0x00000083 : */     0x9f030000,0x00000000,
+at 0x0000008b : */     0x9f030000,0x00000000,
 /*
 
 select_msg_in:
     CALL msg_in, WHEN MSG_IN
 
-at 0x00000085 : */     0x8f0b0000,0x00000354,
+at 0x0000008d : */     0x8f0b0000,0x00000404,
 /*
     JUMP select_msg_in, WHEN MSG_IN
 
-at 0x00000087 : */     0x870b0000,0x00000214,
+at 0x0000008f : */     0x870b0000,0x00000234,
 /*
 
 cmdout:
     INT int_err_unexpected_phase, WHEN NOT CMD
 
-at 0x00000089 : */     0x9a030000,0x00000000,
+at 0x00000091 : */     0x9a030000,0x00000000,
 /*
 
 
@@ -637,7 +727,7 @@ cmdout_cmdout:
 
     MOVE FROM dsa_cmdout, WHEN CMD
 
-at 0x0000008b : */     0x1a000000,0x00000048,
+at 0x00000093 : */     0x1a000000,0x00000048,
 /*
 
 
@@ -645,22 +735,31 @@ at 0x0000008b : */        0x1a000000,0x00000048,
 
 ;
 ; 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 +769,43 @@ at 0x0000008b : */        0x1a000000,0x00000048,
 ;      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 0x00000095 : */     0x820b0000,0x0000024c,
 /*
     CALL msg_in, WHEN MSG_IN
 
-at 0x0000008f : */     0x8f0b0000,0x00000354,
+at 0x00000097 : */     0x8f0b0000,0x00000404,
 /*
     INT int_err_unexpected_phase, WHEN MSG_OUT
 
-at 0x00000091 : */     0x9e0b0000,0x00000000,
+at 0x00000099 : */     0x9e0b0000,0x00000000,
 /*
     JUMP do_dataout, WHEN DATA_OUT
 
-at 0x00000093 : */     0x800b0000,0x0000026c,
+at 0x0000009b : */     0x800b0000,0x0000028c,
 /*
     JUMP do_datain, WHEN DATA_IN
 
-at 0x00000095 : */     0x810b0000,0x000002c4,
+at 0x0000009d : */     0x810b0000,0x000002e4,
 /*
     JUMP command_complete, WHEN STATUS
 
-at 0x00000097 : */     0x830b0000,0x00000508,
+at 0x0000009f : */     0x830b0000,0x0000060c,
 /*
     JUMP data_transfer
 
-at 0x00000099 : */     0x80080000,0x00000234,
+at 0x000000a1 : */     0x80080000,0x00000254,
 /*
+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 +813,214 @@ at 0x00000099 : */      0x80080000,0x00000234,
 do_dataout:
     CALL dsa_to_scratch
 
-at 0x0000009b : */     0x88080000,0x000007e8,
+at 0x000000a3 : */     0x88080000,0x00000918,
 /*
     MOVE SCRATCH0 + dsa_dataout TO SCRATCH0    
 
-at 0x0000009d : */     0x7e345000,0x00000000,
+at 0x000000a5 : */     0x7e345000,0x00000000,
 /*
     MOVE SCRATCH1 + 0 TO SCRATCH1 WITH CARRY 
 
-at 0x0000009f : */     0x7f350000,0x00000000,
+at 0x000000a7 : */     0x7f350000,0x00000000,
 /*
     MOVE SCRATCH2 + 0 TO SCRATCH2 WITH CARRY 
 
-at 0x000000a1 : */     0x7f360000,0x00000000,
+at 0x000000a9 : */     0x7f360000,0x00000000,
 /*
     MOVE SCRATCH3 + 0 TO SCRATCH3 WITH CARRY 
 
-at 0x000000a3 : */     0x7f370000,0x00000000,
+at 0x000000ab : */     0x7f370000,0x00000000,
 /*
     MOVE dmode_ncr_to_memory TO DMODE
 
-at 0x000000a5 : */     0x78380000,0x00000000,
+at 0x000000ad : */     0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, addr_scratch, dataout_to_jump + 4
 
-at 0x000000a7 : */     0xc0000004,0x00000000,0x000002b4,
+at 0x000000af : */     0xc0000004,0x00000000,0x000002d4,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x000000aa : */     0x78380000,0x00000000,
+at 0x000000b2 : */     0x78380000,0x00000000,
 /*
 dataout_to_jump:
     MOVE MEMORY 4, 0, dataout_jump + 4 
 
-at 0x000000ac : */     0xc0000004,0x00000000,0x000002c0,
+at 0x000000b4 : */     0xc0000004,0x00000000,0x000002e0,
 /*
 dataout_jump:
     JUMP 0
 
-at 0x000000af : */     0x80080000,0x00000000,
+at 0x000000b7 : */     0x80080000,0x00000000,
 /*
 
 ; Nasty jump to dsa->dsain
 do_datain:
     CALL dsa_to_scratch
 
-at 0x000000b1 : */     0x88080000,0x000007e8,
+at 0x000000b9 : */     0x88080000,0x00000918,
 /*
     MOVE SCRATCH0 + dsa_datain TO SCRATCH0     
 
-at 0x000000b3 : */     0x7e345400,0x00000000,
+at 0x000000bb : */     0x7e345400,0x00000000,
 /*
     MOVE SCRATCH1 + 0 TO SCRATCH1 WITH CARRY 
 
-at 0x000000b5 : */     0x7f350000,0x00000000,
+at 0x000000bd : */     0x7f350000,0x00000000,
 /*
     MOVE SCRATCH2 + 0 TO SCRATCH2 WITH CARRY 
 
-at 0x000000b7 : */     0x7f360000,0x00000000,
+at 0x000000bf : */     0x7f360000,0x00000000,
 /*
     MOVE SCRATCH3 + 0 TO SCRATCH3 WITH CARRY 
 
-at 0x000000b9 : */     0x7f370000,0x00000000,
+at 0x000000c1 : */     0x7f370000,0x00000000,
 /*
     MOVE dmode_ncr_to_memory TO DMODE
 
-at 0x000000bb : */     0x78380000,0x00000000,
+at 0x000000c3 : */     0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, addr_scratch, datain_to_jump + 4
 
-at 0x000000bd : */     0xc0000004,0x00000000,0x0000030c,
+at 0x000000c5 : */     0xc0000004,0x00000000,0x0000032c,
 /*
     MOVE dmode_memory_to_memory TO DMODE               
 
-at 0x000000c0 : */     0x78380000,0x00000000,
+at 0x000000c8 : */     0x78380000,0x00000000,
 /*
+ENTRY datain_to_jump
 datain_to_jump:
     MOVE MEMORY 4, 0, datain_jump + 4
 
-at 0x000000c2 : */     0xc0000004,0x00000000,0x00000318,
+at 0x000000ca : */     0xc0000004,0x00000000,0x00000338,
 /*
+
+
+
 datain_jump:
     JUMP 0
 
-at 0x000000c5 : */     0x80080000,0x00000000,
+at 0x000000cd : */     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 0x000000c7 : */     0x9a0b0000,0x00000000,
+at 0x000000cf : */     0x9a0b0000,0x00000000,
 /*
-    CALL msg_in, WHEN MSG_IN 
+    JUMP msg_in_restart, WHEN MSG_IN 
 
-at 0x000000c9 : */     0x8f0b0000,0x00000354,
+at 0x000000d1 : */     0x870b0000,0x000003e4,
 /*
     INT int_err_unexpected_phase, WHEN MSG_OUT
 
-at 0x000000cb : */     0x9e0b0000,0x00000000,
+at 0x000000d3 : */     0x9e0b0000,0x00000000,
+/*
+    INT int_err_unexpected_phase, WHEN DATA_IN
+
+at 0x000000d5 : */     0x990b0000,0x00000000,
+/*
+    JUMP command_complete, WHEN STATUS
+
+at 0x000000d7 : */     0x830b0000,0x0000060c,
+/*
+    JUMP other_out, WHEN NOT DATA_OUT
+
+at 0x000000d9 : */     0x80030000,0x0000033c,
+/*
+    RETURN
+
+at 0x000000db : */     0x90080000,0x00000000,
+/*
+
+ENTRY other_in
+other_in:
+
+
+
+    INT int_err_unexpected_phase, WHEN CMD
+
+at 0x000000dd : */     0x9a0b0000,0x00000000,
+/*
+    JUMP msg_in_restart, WHEN MSG_IN 
+
+at 0x000000df : */     0x870b0000,0x000003e4,
+/*
+    INT int_err_unexpected_phase, WHEN MSG_OUT
+
+at 0x000000e1 : */     0x9e0b0000,0x00000000,
+/*
+    INT int_err_unexpected_phase, WHEN DATA_OUT
+
+at 0x000000e3 : */     0x980b0000,0x00000000,
+/*
+    JUMP command_complete, WHEN STATUS
+
+at 0x000000e5 : */     0x830b0000,0x0000060c,
+/*
+    JUMP other_in, WHEN NOT DATA_IN
+
+at 0x000000e7 : */     0x81030000,0x00000374,
+/*
+    RETURN
+
+at 0x000000e9 : */     0x90080000,0x00000000,
+/*
+
+
+ENTRY other_transfer
+other_transfer:
+    INT int_err_unexpected_phase, WHEN CMD
+
+at 0x000000eb : */     0x9a0b0000,0x00000000,
+/*
+    CALL msg_in, WHEN MSG_IN
+
+at 0x000000ed : */     0x8f0b0000,0x00000404,
+/*
+    INT int_err_unexpected_phase, WHEN MSG_OUT
+
+at 0x000000ef : */     0x9e0b0000,0x00000000,
 /*
     INT int_err_unexpected_phase, WHEN DATA_OUT
 
-at 0x000000cd : */     0x980b0000,0x00000000,
+at 0x000000f1 : */     0x980b0000,0x00000000,
 /*
     INT int_err_unexpected_phase, WHEN DATA_IN
 
-at 0x000000cf : */     0x990b0000,0x00000000,
+at 0x000000f3 : */     0x990b0000,0x00000000,
 /*
     JUMP command_complete, WHEN STATUS
 
-at 0x000000d1 : */     0x830b0000,0x00000508,
+at 0x000000f5 : */     0x830b0000,0x0000060c,
 /*
     JUMP other_transfer
 
-at 0x000000d3 : */     0x80080000,0x0000031c,
+at 0x000000f7 : */     0x80080000,0x000003ac,
 /*
 
 ;
+; 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 +1037,6 @@ at 0x000000d3 : */ 0x80080000,0x0000031c,
 ;      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 +1049,235 @@ at 0x000000d3 : */     0x80080000,0x0000031c,
 ;      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 0x000000f9 : */     0x7e1cf800,0x00000000,
+/*
+    MOVE TEMP1 + 0xff TO TEMP1 WITH CARRY
+
+at 0x000000fb : */     0x7f1dff00,0x00000000,
+/*
+    MOVE TEMP2 + 0xff TO TEMP2 WITH CARRY
+
+at 0x000000fd : */     0x7f1eff00,0x00000000,
+/*
+    MOVE TEMP3 + 0xff TO TEMP3 WITH CARRY
+
+at 0x000000ff : */     0x7f1fff00,0x00000000,
+/*
+
 ENTRY msg_in
 msg_in:
     MOVE 1, msg_buf, WHEN MSG_IN
 
-at 0x000000d5 : */     0x0f000001,0x00000000,
+at 0x00000101 : */     0x0f000001,0x00000000,
 /*
 
 munge_msg:
     JUMP munge_extended, IF 0x01               ; EXTENDED MESSAGE
 
-at 0x000000d7 : */     0x800c0001,0x00000428,
+at 0x00000103 : */     0x800c0001,0x00000524,
 /*
     JUMP munge_2, IF 0x20, AND MASK 0xdf       ; two byte message
 
-at 0x000000d9 : */     0x800cdf20,0x0000039c,
+at 0x00000105 : */     0x800cdf20,0x0000044c,
 /*
 ;
-; 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 0x00000107 : */     0x800c0002,0x00000454,
 /*
-    JUMP accept_message, IF 0x03               ; RESTORE POINTERS 
+    JUMP munge_restore_pointers, IF 0x03       ; RESTORE POINTERS 
 
-at 0x000000dd : */     0x800c0003,0x000004d8,
+at 0x00000109 : */     0x800c0003,0x000004b8,
 /*
     JUMP munge_disconnect, IF 0x04             ; DISCONNECT
 
-at 0x000000df : */     0x800c0004,0x000003b4,
+at 0x0000010b : */     0x800c0004,0x0000051c,
 /*
     INT int_msg_1, IF 0x07                     ; MESSAGE REJECT
 
-at 0x000000e1 : */     0x980c0007,0x01020000,
+at 0x0000010d : */     0x980c0007,0x01020000,
 /*
     INT int_msg_1, IF 0x0f                     ; INITIATE RECOVERY
 
-at 0x000000e3 : */     0x980c000f,0x01020000,
+at 0x0000010f : */     0x980c000f,0x01020000,
 /*
+
+
+
     JUMP reject_message
 
-at 0x000000e5 : */     0x80080000,0x000004b8,
+at 0x00000111 : */     0x80080000,0x000005b4,
 /*
 
 munge_2:
     JUMP reject_message
 
-at 0x000000e7 : */     0x80080000,0x000004b8,
+at 0x00000113 : */     0x80080000,0x000005b4,
 /*
+;
+; 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 0x00000115 : */     0x76100000,0x00000000,
 /*
-    RETURN
+    MOVE SFBR TO SCRATCH0
 
-at 0x000000eb : */     0x90080000,0x00000000,
+at 0x00000117 : */     0x6a340000,0x00000000,
 /*
+    MOVE DSA1 + 0xff TO SFBR WITH CARRY
 
-munge_disconnect:
-    MOVE SCNTL2 & 0x7f TO SCNTL2
+at 0x00000119 : */     0x7711ff00,0x00000000,
+/*
+    MOVE SFBR TO SCRATCH1
 
-at 0x000000ed : */     0x7c027f00,0x00000000,
+at 0x0000011b : */     0x6a350000,0x00000000,
 /*
-    CLEAR ACK
+    MOVE DSA2 + 0xff TO SFBR WITH CARRY 
 
-at 0x000000ef : */     0x60000040,0x00000000,
+at 0x0000011d : */     0x7712ff00,0x00000000,
 /*
+    MOVE SFBR TO SCRATCH2
 
+at 0x0000011f : */     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 0x00000121 : */     0x7713ff00,0x00000000,
+/*
+    MOVE SFBR TO SCRATCH3
 
-at 0x000000f1 : */     0x72100000,0x00000000,
+at 0x00000123 : */     0x6a370000,0x00000000,
+/*
+
+    MOVE dmode_ncr_to_memory TO DMODE
+
+at 0x00000125 : */     0x78380000,0x00000000,
+/*
+    MOVE MEMORY 4, addr_scratch, jump_dsa_save + 4
+
+at 0x00000127 : */     0xc0000004,0x00000000,0x000004b4,
+/*
+    MOVE dmode_memory_to_memory TO DMODE
+
+at 0x0000012a : */     0x78380000,0x00000000,
+/*
+jump_dsa_save:
+    JUMP 0
+
+at 0x0000012c : */     0x80080000,0x00000000,
+/*
+
+munge_restore_pointers:
+    MOVE DSA0 + dsa_restore_pointers TO SFBR
+
+at 0x0000012e : */     0x76100000,0x00000000,
 /*
     MOVE SFBR TO SCRATCH0
 
-at 0x000000f3 : */     0x6a340000,0x00000000,
+at 0x00000130 : */     0x6a340000,0x00000000,
 /*
-    MOVE DSA1 TO SFBR
+    MOVE DSA1 + 0xff TO SFBR WITH CARRY
 
-at 0x000000f5 : */     0x72110000,0x00000000,
+at 0x00000132 : */     0x7711ff00,0x00000000,
 /*
     MOVE SFBR TO SCRATCH1
 
-at 0x000000f7 : */     0x6a350000,0x00000000,
+at 0x00000134 : */     0x6a350000,0x00000000,
 /*
-    MOVE DSA2 TO SFBR
+    MOVE DSA2 + 0xff TO SFBR WITH CARRY
+
+at 0x00000136 : */     0x7712ff00,0x00000000,
+/*
+    MOVE SFBR TO SCRATCH2
+
+at 0x00000138 : */     0x6a360000,0x00000000,
+/*
+    MOVE DSA3 + 0xff TO SFBR WITH CARRY
+
+at 0x0000013a : */     0x7713ff00,0x00000000,
+/*
+    MOVE SFBR TO SCRATCH3
+
+at 0x0000013c : */     0x6a370000,0x00000000,
+/*
+
+    MOVE dmode_ncr_to_memory TO DMODE
+
+at 0x0000013e : */     0x78380000,0x00000000,
+/*
+    MOVE MEMORY 4, addr_scratch, jump_dsa_restore + 4
+
+at 0x00000140 : */     0xc0000004,0x00000000,0x00000518,
+/*
+    MOVE dmode_memory_to_memory TO DMODE
+
+at 0x00000143 : */     0x78380000,0x00000000,
+/*
+jump_dsa_restore:
+    JUMP 0
+
+at 0x00000145 : */     0x80080000,0x00000000,
+/*
+
+
+munge_disconnect:
+
+
+
+
+
+
 
-at 0x000000f9 : */     0x72120000,0x00000000,
-/*
-    MOVE SFBR TO SCRATCH2
 
-at 0x000000fb : */     0x6a360000,0x00000000,
-/*
-    MOVE DSA3 TO SFBR
 
-at 0x000000fd : */     0x72130000,0x00000000,
-/*
-    MOVE SFBR TO SCRATCH3
 
-at 0x000000ff : */     0x6a370000,0x00000000,
-/*
 
-    MOVE dmode_ncr_to_memory TO DMODE
 
-at 0x00000101 : */     0x78380000,0x00000000,
-/*
-    MOVE MEMORY 4, addr_scratch, jump_to_dsa + 4
 
-at 0x00000103 : */     0xc0000004,0x00000000,0x00000424,
-/*
-    MOVE dmode_memory_to_memory TO DMODE
 
-at 0x00000106 : */     0x78380000,0x00000000,
-/*
-jump_to_dsa:
-    JUMP 0
 
-at 0x00000108 : */     0x80080000,0x00000000,
+    JUMP dsa_schedule
+
+at 0x00000147 : */     0x80080000,0x00000168,
 /*
 
 
@@ -1013,128 +1287,132 @@ at 0x00000108 : */    0x80080000,0x00000000,
 munge_extended:
     CLEAR ACK
 
-at 0x0000010a : */     0x60000040,0x00000000,
+at 0x00000149 : */     0x60000040,0x00000000,
 /*
     INT int_err_unexpected_phase, WHEN NOT MSG_IN
 
-at 0x0000010c : */     0x9f030000,0x00000000,
+at 0x0000014b : */     0x9f030000,0x00000000,
 /*
     MOVE 1, msg_buf + 1, WHEN MSG_IN
 
-at 0x0000010e : */     0x0f000001,0x00000001,
+at 0x0000014d : */     0x0f000001,0x00000001,
 /*
     JUMP munge_extended_2, IF 0x02
 
-at 0x00000110 : */     0x800c0002,0x00000458,
+at 0x0000014f : */     0x800c0002,0x00000554,
 /*
     JUMP munge_extended_3, IF 0x03 
 
-at 0x00000112 : */     0x800c0003,0x00000488,
+at 0x00000151 : */     0x800c0003,0x00000584,
 /*
     JUMP reject_message
 
-at 0x00000114 : */     0x80080000,0x000004b8,
+at 0x00000153 : */     0x80080000,0x000005b4,
 /*
 
 munge_extended_2:
     CLEAR ACK
 
-at 0x00000116 : */     0x60000040,0x00000000,
+at 0x00000155 : */     0x60000040,0x00000000,
 /*
     MOVE 1, msg_buf + 2, WHEN MSG_IN
 
-at 0x00000118 : */     0x0f000001,0x00000002,
+at 0x00000157 : */     0x0f000001,0x00000002,
 /*
     JUMP reject_message, IF NOT 0x02   ; Must be WDTR
 
-at 0x0000011a : */     0x80040002,0x000004b8,
+at 0x00000159 : */     0x80040002,0x000005b4,
 /*
     CLEAR ACK
 
-at 0x0000011c : */     0x60000040,0x00000000,
+at 0x0000015b : */     0x60000040,0x00000000,
 /*
     MOVE 1, msg_buf + 3, WHEN MSG_IN
 
-at 0x0000011e : */     0x0f000001,0x00000003,
+at 0x0000015d : */     0x0f000001,0x00000003,
 /*
     INT int_msg_wdtr
 
-at 0x00000120 : */     0x98080000,0x01000000,
+at 0x0000015f : */     0x98080000,0x01000000,
 /*
 
 munge_extended_3:
     CLEAR ACK
 
-at 0x00000122 : */     0x60000040,0x00000000,
+at 0x00000161 : */     0x60000040,0x00000000,
 /*
     MOVE 1, msg_buf + 2, WHEN MSG_IN
 
-at 0x00000124 : */     0x0f000001,0x00000002,
+at 0x00000163 : */     0x0f000001,0x00000002,
 /*
     JUMP reject_message, IF NOT 0x01   ; Must be SDTR
 
-at 0x00000126 : */     0x80040001,0x000004b8,
+at 0x00000165 : */     0x80040001,0x000005b4,
 /*
     CLEAR ACK
 
-at 0x00000128 : */     0x60000040,0x00000000,
+at 0x00000167 : */     0x60000040,0x00000000,
 /*
     MOVE 2, msg_buf + 3, WHEN MSG_IN
 
-at 0x0000012a : */     0x0f000002,0x00000003,
+at 0x00000169 : */     0x0f000002,0x00000003,
 /*
     INT int_msg_sdtr
 
-at 0x0000012c : */     0x98080000,0x01010000,
+at 0x0000016b : */     0x98080000,0x01010000,
 /*
 
 ENTRY reject_message
 reject_message:
     SET ATN
 
-at 0x0000012e : */     0x58000008,0x00000000,
+at 0x0000016d : */     0x58000008,0x00000000,
 /*
     CLEAR ACK
 
-at 0x00000130 : */     0x60000040,0x00000000,
+at 0x0000016f : */     0x60000040,0x00000000,
 /*
     MOVE 1, NCR53c7xx_msg_reject, WHEN MSG_OUT
 
-at 0x00000132 : */     0x0e000001,0x00000000,
+at 0x00000171 : */     0x0e000001,0x00000000,
 /*
     RETURN
 
-at 0x00000134 : */     0x90080000,0x00000000,
+at 0x00000173 : */     0x90080000,0x00000000,
 /*
 
 ENTRY accept_message
 accept_message:
+    CLEAR ATN
+
+at 0x00000175 : */     0x60000008,0x00000000,
+/*
     CLEAR ACK
 
-at 0x00000136 : */     0x60000040,0x00000000,
+at 0x00000177 : */     0x60000040,0x00000000,
 /*
     RETURN
 
-at 0x00000138 : */     0x90080000,0x00000000,
+at 0x00000179 : */     0x90080000,0x00000000,
 /*
 
 ENTRY respond_message
-msg_respond:
+respond_message:
     SET ATN
 
-at 0x0000013a : */     0x58000008,0x00000000,
+at 0x0000017b : */     0x58000008,0x00000000,
 /*
     CLEAR ACK
 
-at 0x0000013c : */     0x60000040,0x00000000,
+at 0x0000017d : */     0x60000040,0x00000000,
 /*
     MOVE FROM dsa_msgout_other, WHEN MSG_OUT
 
-at 0x0000013e : */     0x1e000000,0x00000068,
+at 0x0000017f : */     0x1e000000,0x00000068,
 /*
     RETURN
 
-at 0x00000140 : */     0x90080000,0x00000000,
+at 0x00000181 : */     0x90080000,0x00000000,
 /*
 
 ;
@@ -1168,33 +1446,33 @@ ENTRY command_complete
 command_complete:
     MOVE FROM dsa_status, WHEN STATUS
 
-at 0x00000142 : */     0x1b000000,0x00000060,
+at 0x00000183 : */     0x1b000000,0x00000060,
 /*
 
     MOVE SFBR TO SCRATCH0              ; Save status
 
-at 0x00000144 : */     0x6a340000,0x00000000,
+at 0x00000185 : */     0x6a340000,0x00000000,
 /*
 
 ENTRY command_complete_msgin
 command_complete_msgin:
     MOVE FROM dsa_msgin, WHEN MSG_IN
 
-at 0x00000146 : */     0x1f000000,0x00000058,
+at 0x00000187 : */     0x1f000000,0x00000058,
 /*
 ; Indicate that we should be expecting a disconnect
     MOVE SCNTL2 & 0x7f TO SCNTL2
 
-at 0x00000148 : */     0x7c027f00,0x00000000,
+at 0x00000189 : */     0x7c027f00,0x00000000,
 /*
     CLEAR ACK
 
-at 0x0000014a : */     0x60000040,0x00000000,
+at 0x0000018b : */     0x60000040,0x00000000,
 /*
 
-    MOVE SCRATCH0 TO SFBR                      
+    WAIT DISCONNECT
 
-at 0x0000014c : */     0x72340000,0x00000000,
+at 0x0000018d : */     0x48000000,0x00000000,
 /*
 
 ;
@@ -1216,28 +1494,24 @@ at 0x0000014c : */      0x72340000,0x00000000,
 
 
 
+
     INTFLY
 
-at 0x0000014e : */     0x98180000,0x00000000,
+at 0x0000018f : */     0x98180000,0x00000000,
 /*
 
-    WAIT DISCONNECT
 
-at 0x00000150 : */     0x48000000,0x00000000,
-/*
+
+
 
     JUMP schedule
 
-at 0x00000152 : */     0x80080000,0x00000130,
+at 0x00000191 : */     0x80080000,0x00000000,
 /*
 command_failed:
-    WAIT DISCONNECT
-
-at 0x00000154 : */     0x48000000,0x00000000,
-/*
     INT int_err_check_condition
 
-at 0x00000156 : */     0x98080000,0x00030000,
+at 0x00000193 : */     0x98080000,0x00030000,
 /*
 
 
@@ -1273,30 +1547,38 @@ at 0x00000156 : */      0x98080000,0x00030000,
 ;      selected, the host system is interrupted with an 
 ;      int_err_selected which is usually responded to by
 ;      setting DSP to the target_abort address.
-    
+
+ENTRY wait_reselect
 wait_reselect:
 
 
 
+
+
+
     WAIT RESELECT wait_reselect_failed
 
-at 0x00000158 : */     0x50000000,0x0000067c,
+at 0x00000195 : */     0x50000000,0x0000076c,
 /*
 
 reselected:
-    ; Read all data needed to reestablish the nexus - 
-    MOVE 1, reselected_identify, WHEN MSG_IN
 
-at 0x0000015a : */     0x0f000001,0x00000000,
+
+
+    CLEAR TARGET
+
+at 0x00000197 : */     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 0x00000199 : */     0x78380000,0x00000000,
+/*
+    ; Read all data needed to reestablish the nexus - 
+    MOVE 1, reselected_identify, WHEN MSG_IN
 
-at 0x0000015c : */     0xc0000001,0x00000000,0x00000000,
+at 0x0000019b : */     0x0f000001,0x00000000,
 /*
+    ; We used to CLEAR ACK here.
 
 
 
@@ -1305,75 +1587,74 @@ at 0x0000015c : */      0xc0000001,0x00000000,0x00000000,
     ; Point DSA at the current head of the disconnected queue.
     MOVE dmode_memory_to_ncr  TO DMODE
 
-at 0x0000015f : */     0x78380000,0x00000000,
+at 0x0000019d : */     0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, reconnect_dsa_head, addr_scratch
 
-at 0x00000161 : */     0xc0000004,0x00000000,0x00000000,
+at 0x0000019f : */     0xc0000004,0x00000000,0x00000000,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x00000164 : */     0x78380000,0x00000000,
+at 0x000001a2 : */     0x78380000,0x00000000,
 /*
     CALL scratch_to_dsa
 
-at 0x00000166 : */     0x88080000,0x00000830,
+at 0x000001a4 : */     0x88080000,0x00000960,
 /*
 
     ; 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 0x000001a6 : */     0xc0000004,0x00000000,0x00000758,
 /*
 
 ENTRY reselected_check_next
 reselected_check_next:
+
+
+
     ; Check for a NULL pointer.
     MOVE DSA0 TO SFBR
 
-at 0x0000016b : */     0x72100000,0x00000000,
+at 0x000001a9 : */     0x72100000,0x00000000,
 /*
     JUMP reselected_not_end, IF NOT 0
 
-at 0x0000016d : */     0x80040000,0x000005f4,
+at 0x000001ab : */     0x80040000,0x000006ec,
 /*
     MOVE DSA1 TO SFBR
 
-at 0x0000016f : */     0x72110000,0x00000000,
+at 0x000001ad : */     0x72110000,0x00000000,
 /*
     JUMP reselected_not_end, IF NOT 0
 
-at 0x00000171 : */     0x80040000,0x000005f4,
+at 0x000001af : */     0x80040000,0x000006ec,
 /*
     MOVE DSA2 TO SFBR
 
-at 0x00000173 : */     0x72120000,0x00000000,
+at 0x000001b1 : */     0x72120000,0x00000000,
 /*
     JUMP reselected_not_end, IF NOT 0
 
-at 0x00000175 : */     0x80040000,0x000005f4,
+at 0x000001b3 : */     0x80040000,0x000006ec,
 /*
     MOVE DSA3 TO SFBR
 
-at 0x00000177 : */     0x72130000,0x00000000,
+at 0x000001b5 : */     0x72130000,0x00000000,
 /*
     JUMP reselected_not_end, IF NOT 0
 
-at 0x00000179 : */     0x80040000,0x000005f4,
+at 0x000001b7 : */     0x80040000,0x000006ec,
 /*
     INT int_err_unexpected_reselect
 
-at 0x0000017b : */     0x98080000,0x00020000,
+at 0x000001b9 : */     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 +1667,82 @@ at 0x0000017d : */      0x72100000,0x00000000,
     ; higher than the LSB.
     ;
 
+    MOVE DSA0 TO SFBR
+
+at 0x000001bb : */     0x72100000,0x00000000,
+/*
     MOVE SFBR + dsa_check_reselect TO SCRATCH0
 
-at 0x0000017f : */     0x6e340000,0x00000000,
+at 0x000001bd : */     0x6e340000,0x00000000,
 /*
     MOVE DSA1 TO SFBR
 
-at 0x00000181 : */     0x72110000,0x00000000,
+at 0x000001bf : */     0x72110000,0x00000000,
 /*
     MOVE SFBR + 0xff TO SCRATCH1 WITH CARRY
 
-at 0x00000183 : */     0x6f35ff00,0x00000000,
+at 0x000001c1 : */     0x6f35ff00,0x00000000,
 /*
     MOVE DSA2 TO SFBR
 
-at 0x00000185 : */     0x72120000,0x00000000,
+at 0x000001c3 : */     0x72120000,0x00000000,
 /*
     MOVE SFBR + 0xff TO SCRATCH2 WITH CARRY
 
-at 0x00000187 : */     0x6f36ff00,0x00000000,
+at 0x000001c5 : */     0x6f36ff00,0x00000000,
 /*
     MOVE DSA3 TO SFBR
 
-at 0x00000189 : */     0x72130000,0x00000000,
+at 0x000001c7 : */     0x72130000,0x00000000,
 /*
     MOVE SFBR + 0xff TO SCRATCH3 WITH CARRY
 
-at 0x0000018b : */     0x6f37ff00,0x00000000,
+at 0x000001c9 : */     0x6f37ff00,0x00000000,
 /*
 
     MOVE dmode_ncr_to_memory TO DMODE
 
-at 0x0000018d : */     0x78380000,0x00000000,
+at 0x000001cb : */     0x78380000,0x00000000,
 /*
     MOVE MEMORY 4, addr_scratch, reselected_check + 4
 
-at 0x0000018f : */     0xc0000004,0x00000000,0x00000654,
+at 0x000001cd : */     0xc0000004,0x00000000,0x0000074c,
 /*
     MOVE dmode_memory_to_memory TO DMODE
 
-at 0x00000192 : */     0x78380000,0x00000000,
+at 0x000001d0 : */     0x78380000,0x00000000,
 /*
 reselected_check:
     JUMP 0
 
-at 0x00000194 : */     0x80080000,0x00000000,
+at 0x000001d2 : */     0x80080000,0x00000000,
 /*
 
 
 ;
 ;
+ENTRY reselected_ok
 reselected_ok:
     MOVE MEMORY 4, 0, 0                                ; Patched : first word
 
-at 0x00000196 : */     0xc0000004,0x00000000,0x00000000,
+at 0x000001d4 : */     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 0x000001d7 : */     0x90080000,0x00000000,
 /*
 
 
@@ -1460,7 +1751,7 @@ at 0x0000019b : */        0x90080000,0x00000000,
 selected:
     INT int_err_selected;
 
-at 0x0000019d : */     0x98080000,0x00010000,
+at 0x000001d9 : */     0x98080000,0x00010000,
 /*
 
 ;
@@ -1472,60 +1763,75 @@ at 0x0000019d : */      0x98080000,0x00010000,
 ; 
 
 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 0x000001db : */     0x74422000,0x00000000,
 /*
     JUMP selected, IF 0x20
 
-at 0x000001a5 : */     0x800c0020,0x00000674,
+at 0x000001dd : */     0x800c0020,0x00000764,
+/*
+; Reading CTEST2 clears the SIG_P bit in the ISTAT register.
+    MOVE CTEST2 & 0x40 TO SFBR 
+
+at 0x000001df : */     0x741a4000,0x00000000,
+/*
+    JUMP schedule, IF 0x40
+
+at 0x000001e1 : */     0x800c0040,0x00000000,
 /*
 ; FIXME : Something bogus happened, and we shouldn't fail silently.
-    JUMP schedule
 
-at 0x000001a7 : */     0x80080000,0x00000130,
+
+
+    INT int_debug_panic
+
+at 0x000001e3 : */     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 0x000001e5 : */     0x74423000,0x00000000,
 /*
     JUMP selected, IF 0x20
 
-at 0x000001af : */     0x800c0020,0x00000674,
+at 0x000001e7 : */     0x800c0020,0x00000764,
 /*
     JUMP reselected, IF 0x10 
 
-at 0x000001b1 : */     0x800c0010,0x00000568,
+at 0x000001e9 : */     0x800c0010,0x0000065c,
+/*
+; 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 0x000001eb : */     0x741a4000,0x00000000,
+/*
+    JUMP select, IF 0x40
+
+at 0x000001ed : */     0x800c0040,0x000001fc,
 /*
 ; FIXME : Something bogus happened, and we shouldn't fail silently.
-    JUMP schedule
 
-at 0x000001b3 : */     0x80080000,0x00000130,
+
+
+    INT int_debug_panic
+
+at 0x000001ef : */     0x98080000,0x030b0000,
 /*
 
+
 ;
 ; test_1
 ; test_2
@@ -1547,11 +1853,11 @@ ENTRY test_1
 test_1:
     MOVE MEMORY 4, test_src, test_dest
 
-at 0x000001b5 : */     0xc0000004,0x00000000,0x00000000,
+at 0x000001f1 : */     0xc0000004,0x00000000,0x00000000,
 /*
     INT int_test_1
 
-at 0x000001b8 : */     0x98080000,0x04000000,
+at 0x000001f4 : */     0x98080000,0x04000000,
 /*
 
 ;
@@ -1562,61 +1868,61 @@ ENTRY test_2
 test_2:
     CLEAR TARGET
 
-at 0x000001ba : */     0x60000200,0x00000000,
+at 0x000001f6 : */     0x60000200,0x00000000,
 /*
     SELECT ATN FROM 0, test_2_fail
 
-at 0x000001bc : */     0x43000000,0x00000740,
+at 0x000001f8 : */     0x43000000,0x00000830,
 /*
     JUMP test_2_msgout, WHEN MSG_OUT
 
-at 0x000001be : */     0x860b0000,0x00000700,
+at 0x000001fa : */     0x860b0000,0x000007f0,
 /*
 ENTRY test_2_msgout
 test_2_msgout:
     MOVE FROM 8, WHEN MSG_OUT
 
-at 0x000001c0 : */     0x1e000000,0x00000008,
+at 0x000001fc : */     0x1e000000,0x00000008,
 /*
     MOVE FROM 16, WHEN CMD 
 
-at 0x000001c2 : */     0x1a000000,0x00000010,
+at 0x000001fe : */     0x1a000000,0x00000010,
 /*
     MOVE FROM 24, WHEN DATA_IN
 
-at 0x000001c4 : */     0x19000000,0x00000018,
+at 0x00000200 : */     0x19000000,0x00000018,
 /*
     MOVE FROM 32, WHEN STATUS
 
-at 0x000001c6 : */     0x1b000000,0x00000020,
+at 0x00000202 : */     0x1b000000,0x00000020,
 /*
     MOVE FROM 40, WHEN MSG_IN
 
-at 0x000001c8 : */     0x1f000000,0x00000028,
+at 0x00000204 : */     0x1f000000,0x00000028,
 /*
     MOVE SCNTL2 & 0x7f TO SCNTL2
 
-at 0x000001ca : */     0x7c027f00,0x00000000,
+at 0x00000206 : */     0x7c027f00,0x00000000,
 /*
     CLEAR ACK
 
-at 0x000001cc : */     0x60000040,0x00000000,
+at 0x00000208 : */     0x60000040,0x00000000,
 /*
     WAIT DISCONNECT
 
-at 0x000001ce : */     0x48000000,0x00000000,
+at 0x0000020a : */     0x48000000,0x00000000,
 /*
 test_2_fail:
     INT int_test_2
 
-at 0x000001d0 : */     0x98080000,0x04010000,
+at 0x0000020c : */     0x98080000,0x04010000,
 /*
 
 ENTRY debug_break
 debug_break:
     INT int_debug_break
 
-at 0x000001d2 : */     0x98080000,0x03000000,
+at 0x0000020e : */     0x98080000,0x03000000,
 /*
 
 ;
@@ -1632,96 +1938,129 @@ ENTRY target_abort
 target_abort:
     SET TARGET
 
-at 0x000001d4 : */     0x58000200,0x00000000,
+at 0x00000210 : */     0x58000200,0x00000000,
 /*
     DISCONNECT
 
-at 0x000001d6 : */     0x48000000,0x00000000,
+at 0x00000212 : */     0x48000000,0x00000000,
 /*
     CLEAR TARGET
 
-at 0x000001d8 : */     0x60000200,0x00000000,
+at 0x00000214 : */     0x60000200,0x00000000,
 /*
     JUMP schedule
 
-at 0x000001da : */     0x80080000,0x00000130,
+at 0x00000216 : */     0x80080000,0x00000000,
 /*
     
 ENTRY initiator_abort
 initiator_abort:
     SET ATN
 
-at 0x000001dc : */     0x58000008,0x00000000,
+at 0x00000218 : */     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 0x0000021a : */     0x820b0000,0x00000898,
+/*
+    JUMP eat_msgin, WHEN MSG_IN
+
+at 0x0000021c : */     0x870b0000,0x000008a8,
+/*
+    JUMP eat_datain, WHEN DATA_IN
+
+at 0x0000021e : */     0x810b0000,0x000008d8,
+/*
+    JUMP eat_status, WHEN STATUS
+
+at 0x00000220 : */     0x830b0000,0x000008c0,
+/*
+    JUMP spew_dataout, WHEN DATA_OUT
+
+at 0x00000222 : */     0x800b0000,0x000008f0,
 /*
-; 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 0x00000224 : */     0x80080000,0x000008f8,
 /*
+spew_cmd:
     MOVE 1, NCR53c7xx_zero, WHEN CMD
 
-at 0x000001e0 : */     0x0a000001,0x00000000,
+at 0x00000226 : */     0x0a000001,0x00000000,
 /*
-no_eat_cmd:
-    JUMP no_eat_msg, WHEN NOT MSG_IN
+    JUMP sated
 
-at 0x000001e2 : */     0x87030000,0x00000798,
+at 0x00000228 : */     0x80080000,0x000008f8,
 /*
+eat_msgin:
     MOVE 1, NCR53c7xx_sink, WHEN MSG_IN
 
-at 0x000001e4 : */     0x0f000001,0x00000000,
+at 0x0000022a : */     0x0f000001,0x00000000,
 /*
-no_eat_msg:
-    JUMP no_eat_data, WHEN NOT DATA_IN
+    JUMP eat_msgin, WHEN MSG_IN
 
-at 0x000001e6 : */     0x81030000,0x000007a8,
+at 0x0000022c : */     0x870b0000,0x000008a8,
 /*
-    MOVE 1, NCR53c7xx_sink, WHEN DATA_IN
+    JUMP sated
 
-at 0x000001e8 : */     0x09000001,0x00000000,
+at 0x0000022e : */     0x80080000,0x000008f8,
 /*
-no_eat_data:
-    JUMP no_eat_status, WHEN NOT STATUS
+eat_status:
+    MOVE 1, NCR53c7xx_sink, WHEN STATUS
 
-at 0x000001ea : */     0x83030000,0x000007b8,
+at 0x00000230 : */     0x0b000001,0x00000000,
 /*
-    MOVE 1, NCR53c7xx_sink, WHEN STATUS
+    JUMP eat_status, WHEN STATUS
 
-at 0x000001ec : */     0x0b000001,0x00000000,
+at 0x00000232 : */     0x830b0000,0x000008c0,
 /*
-no_eat_status:
-    JUMP no_source_data, WHEN NOT DATA_OUT
+    JUMP sated
 
-at 0x000001ee : */     0x80030000,0x000007c8,
+at 0x00000234 : */     0x80080000,0x000008f8,
 /*
+eat_datain:
+    MOVE 1, NCR53c7xx_sink, WHEN DATA_IN
+
+at 0x00000236 : */     0x09000001,0x00000000,
+/*
+    JUMP eat_datain, WHEN DATA_IN
+
+at 0x00000238 : */     0x810b0000,0x000008d8,
+/*
+    JUMP sated
+
+at 0x0000023a : */     0x80080000,0x000008f8,
+/*
+spew_dataout:
     MOVE 1, NCR53c7xx_zero, WHEN DATA_OUT
 
-at 0x000001f0 : */     0x08000001,0x00000000,
+at 0x0000023c : */     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 0x0000023e : */     0x7c027f00,0x00000000,
 /*
     MOVE 1, NCR53c7xx_msg_abort, WHEN MSG_OUT
 
-at 0x000001f4 : */     0x0e000001,0x00000000,
+at 0x00000240 : */     0x0e000001,0x00000000,
 /*
     WAIT DISCONNECT
 
-at 0x000001f6 : */     0x48000000,0x00000000,
+at 0x00000242 : */     0x48000000,0x00000000,
 /*
     INT int_norm_aborted
 
-at 0x000001f8 : */     0x98080000,0x02040000,
+at 0x00000244 : */     0x98080000,0x02040000,
 /*
 
 ;
@@ -1742,175 +2081,180 @@ at 0x000001f8 : */    0x98080000,0x02040000,
 dsa_to_scratch:
     MOVE DSA0 TO SFBR
 
-at 0x000001fa : */     0x72100000,0x00000000,
+at 0x00000246 : */     0x72100000,0x00000000,
 /*
     MOVE SFBR TO SCRATCH0
 
-at 0x000001fc : */     0x6a340000,0x00000000,
+at 0x00000248 : */     0x6a340000,0x00000000,
 /*
     MOVE DSA1 TO SFBR
 
-at 0x000001fe : */     0x72110000,0x00000000,
+at 0x0000024a : */     0x72110000,0x00000000,
 /*
     MOVE SFBR TO SCRATCH1
 
-at 0x00000200 : */     0x6a350000,0x00000000,
+at 0x0000024c : */     0x6a350000,0x00000000,
 /*
     MOVE DSA2 TO SFBR
 
-at 0x00000202 : */     0x72120000,0x00000000,
+at 0x0000024e : */     0x72120000,0x00000000,
 /*
     MOVE SFBR TO SCRATCH2
 
-at 0x00000204 : */     0x6a360000,0x00000000,
+at 0x00000250 : */     0x6a360000,0x00000000,
 /*
     MOVE DSA3 TO SFBR
 
-at 0x00000206 : */     0x72130000,0x00000000,
+at 0x00000252 : */     0x72130000,0x00000000,
 /*
     MOVE SFBR TO SCRATCH3
 
-at 0x00000208 : */     0x6a370000,0x00000000,
+at 0x00000254 : */     0x6a370000,0x00000000,
 /*
     RETURN
 
-at 0x0000020a : */     0x90080000,0x00000000,
+at 0x00000256 : */     0x90080000,0x00000000,
 /*
 
 scratch_to_dsa:
     MOVE SCRATCH0 TO SFBR
 
-at 0x0000020c : */     0x72340000,0x00000000,
+at 0x00000258 : */     0x72340000,0x00000000,
 /*
     MOVE SFBR TO DSA0
 
-at 0x0000020e : */     0x6a100000,0x00000000,
+at 0x0000025a : */     0x6a100000,0x00000000,
 /*
     MOVE SCRATCH1 TO SFBR
 
-at 0x00000210 : */     0x72350000,0x00000000,
+at 0x0000025c : */     0x72350000,0x00000000,
 /*
     MOVE SFBR TO DSA1
 
-at 0x00000212 : */     0x6a110000,0x00000000,
+at 0x0000025e : */     0x6a110000,0x00000000,
 /*
     MOVE SCRATCH2 TO SFBR
 
-at 0x00000214 : */     0x72360000,0x00000000,
+at 0x00000260 : */     0x72360000,0x00000000,
 /*
     MOVE SFBR TO DSA2
 
-at 0x00000216 : */     0x6a120000,0x00000000,
+at 0x00000262 : */     0x6a120000,0x00000000,
 /*
     MOVE SCRATCH3 TO SFBR
 
-at 0x00000218 : */     0x72370000,0x00000000,
+at 0x00000264 : */     0x72370000,0x00000000,
 /*
     MOVE SFBR TO DSA3
 
-at 0x0000021a : */     0x6a130000,0x00000000,
+at 0x00000266 : */     0x6a130000,0x00000000,
 /*
     RETURN
 
-at 0x0000021c : */     0x90080000,0x00000000,
+at 0x00000268 : */     0x90080000,0x00000000,
 };
 
 #define A_NCR53c7xx_msg_abort  0x00000000
 u32 A_NCR53c7xx_msg_abort_used[] = {
-       0x000001f5,
+       0x00000241,
 };
 
 #define A_NCR53c7xx_msg_reject 0x00000000
 u32 A_NCR53c7xx_msg_reject_used[] = {
-       0x00000133,
+       0x00000172,
 };
 
 #define A_NCR53c7xx_sink       0x00000000
 u32 A_NCR53c7xx_sink_used[] = {
-       0x000001e5,
-       0x000001e9,
-       0x000001ed,
+       0x0000022b,
+       0x00000231,
+       0x00000237,
 };
 
 #define A_NCR53c7xx_zero       0x00000000
 u32 A_NCR53c7xx_zero_used[] = {
-       0x0000015d,
-       0x000001e1,
-       0x000001f1,
+       0x00000227,
+       0x0000023d,
+};
+
+#define A_NOP_insn     0x00000000
+u32 A_NOP_insn_used[] = {
+       0x00000010,
+};
+
+#define A_addr_reconnect_dsa_head      0x00000000
+u32 A_addr_reconnect_dsa_head_used[] = {
+       0x000001a7,
 };
 
 #define A_addr_scratch 0x00000000
 u32 A_addr_scratch_used[] = {
-       0x00000007,
-       0x0000003a,
+       0x00000004,
+       0x0000001b,
        0x00000046,
-       0x00000050,
-       0x0000007a,
-       0x000000a8,
-       0x000000be,
-       0x00000104,
-       0x00000163,
-       0x00000190,
-};
-
-#define A_addr_sfbr    0x00000000
-u32 A_addr_sfbr_used[] = {
-       0x00000016,
+       0x00000067,
+       0x00000073,
+       0x000000b0,
+       0x000000c6,
+       0x00000128,
+       0x00000141,
+       0x000001a1,
+       0x000001ce,
 };
 
 #define A_addr_temp    0x00000000
 u32 A_addr_temp_used[] = {
-       0x00000027,
+       0x00000025,
+       0x00000034,
 };
 
 #define A_dmode_memory_to_memory       0x00000000
 u32 A_dmode_memory_to_memory_used[] = {
-       0x00000008,
-       0x00000019,
-       0x00000029,
-       0x0000003c,
-       0x00000048,
-       0x00000051,
-       0x0000007c,
-       0x000000aa,
-       0x000000c0,
-       0x00000106,
-       0x00000164,
-       0x00000192,
+       0x00000005,
+       0x0000001c,
+       0x00000027,
+       0x00000035,
+       0x00000047,
+       0x00000069,
+       0x00000075,
+       0x000000b2,
+       0x000000c8,
+       0x0000012a,
+       0x00000143,
+       0x00000199,
+       0x000001a2,
+       0x000001d0,
 };
 
 #define A_dmode_memory_to_ncr  0x00000000
 u32 A_dmode_memory_to_ncr_used[] = {
-       0x00000003,
-       0x00000012,
-       0x0000004c,
-       0x0000015f,
+       0x00000000,
+       0x00000017,
+       0x00000030,
+       0x00000042,
+       0x0000019d,
 };
 
 #define A_dmode_ncr_to_memory  0x00000000
 u32 A_dmode_ncr_to_memory_used[] = {
-       0x00000024,
-       0x00000037,
-       0x00000043,
-       0x00000077,
-       0x000000a5,
-       0x000000bb,
-       0x00000101,
-       0x0000018d,
-};
-
-#define A_dmode_ncr_to_ncr     0x00000000
-u32 A_dmode_ncr_to_ncr_used[] = {
+       0x00000022,
+       0x00000064,
+       0x00000070,
+       0x000000ad,
+       0x000000c3,
+       0x00000125,
+       0x0000013e,
+       0x000001cb,
 };
 
 #define A_dsa_check_reselect   0x00000000
 u32 A_dsa_check_reselect_used[] = {
-       0x0000017f,
+       0x000001bd,
 };
 
 #define A_dsa_cmdout   0x00000048
 u32 A_dsa_cmdout_used[] = {
-       0x0000008c,
+       0x00000094,
 };
 
 #define A_dsa_cmnd     0x00000038
@@ -1919,108 +2263,133 @@ u32 A_dsa_cmnd_used[] = {
 
 #define A_dsa_datain   0x00000054
 u32 A_dsa_datain_used[] = {
-       0x000000b3,
+       0x000000bb,
 };
 
 #define A_dsa_dataout  0x00000050
 u32 A_dsa_dataout_used[] = {
-       0x0000009d,
+       0x000000a5,
 };
 
 #define A_dsa_end      0x00000070
 u32 A_dsa_end_used[] = {
 };
 
-#define A_dsa_fields_start     0x00000024
+#define A_dsa_fields_start     0x00000000
 u32 A_dsa_fields_start_used[] = {
 };
 
 #define A_dsa_msgin    0x00000058
 u32 A_dsa_msgin_used[] = {
-       0x00000147,
+       0x00000188,
 };
 
 #define A_dsa_msgout   0x00000040
 u32 A_dsa_msgout_used[] = {
-       0x0000006c,
+       0x00000086,
 };
 
 #define A_dsa_msgout_other     0x00000068
 u32 A_dsa_msgout_other_used[] = {
-       0x0000013f,
+       0x00000180,
 };
 
 #define A_dsa_next     0x00000030
 u32 A_dsa_next_used[] = {
-       0x0000002f,
-       0x0000006f,
+       0x0000005c,
+};
+
+#define A_dsa_restore_pointers 0x00000000
+u32 A_dsa_restore_pointers_used[] = {
+       0x0000012e,
+};
+
+#define A_dsa_save_data_pointer        0x00000000
+u32 A_dsa_save_data_pointer_used[] = {
+       0x00000115,
 };
 
 #define A_dsa_select   0x0000003c
 u32 A_dsa_select_used[] = {
-       0x00000067,
+       0x00000081,
 };
 
 #define A_dsa_status   0x00000060
 u32 A_dsa_status_used[] = {
-       0x00000143,
+       0x00000184,
 };
 
-#define A_dsa_temp_dsa_next    0x00000000
-u32 A_dsa_temp_dsa_next_used[] = {
-       0x00000001,
-       0x00000006,
-       0x0000001c,
+#define A_dsa_temp_addr_array_value    0x00000000
+u32 A_dsa_temp_addr_array_value_used[] = {
 };
 
-#define A_dsa_temp_jump_resume 0x00000000
-u32 A_dsa_temp_jump_resume_used[] = {
-       0x00000028,
+#define A_dsa_temp_addr_dsa_value      0x00000000
+u32 A_dsa_temp_addr_dsa_value_used[] = {
+       0x00000003,
 };
 
-#define A_dsa_temp_lun 0x00000000
-u32 A_dsa_temp_lun_used[] = {
-       0x00000017,
+#define A_dsa_temp_addr_new_value      0x00000000
+u32 A_dsa_temp_addr_new_value_used[] = {
 };
 
-#define A_dsa_temp_sync        0x00000000
-u32 A_dsa_temp_sync_used[] = {
-       0x00000021,
+#define A_dsa_temp_addr_next   0x00000000
+u32 A_dsa_temp_addr_next_used[] = {
+       0x00000015,
+       0x0000004e,
 };
 
-#define A_dsa_temp_target      0x00000000
-u32 A_dsa_temp_target_used[] = {
-       0x00000010,
+#define A_dsa_temp_addr_residual       0x00000000
+u32 A_dsa_temp_addr_residual_used[] = {
+       0x0000002a,
+       0x00000039,
 };
 
-#define A_int_debug_break      0x03000000
-u32 A_int_debug_break_used[] = {
-       0x000001d3,
+#define A_dsa_temp_addr_saved_pointer  0x00000000
+u32 A_dsa_temp_addr_saved_pointer_used[] = {
+       0x00000026,
+       0x00000033,
+};
+
+#define A_dsa_temp_addr_saved_residual 0x00000000
+u32 A_dsa_temp_addr_saved_residual_used[] = {
+       0x0000002b,
+       0x00000038,
+};
+
+#define A_dsa_temp_lun 0x00000000
+u32 A_dsa_temp_lun_used[] = {
+       0x0000004b,
 };
 
-#define A_int_debug_dsa_loaded 0x03030000
-u32 A_int_debug_dsa_loaded_used[] = {
+#define A_dsa_temp_next        0x00000000
+u32 A_dsa_temp_next_used[] = {
+       0x0000001a,
 };
 
-#define A_int_debug_head       0x03050000
-u32 A_int_debug_head_used[] = {
+#define A_dsa_temp_sync        0x00000000
+u32 A_dsa_temp_sync_used[] = {
+       0x00000053,
 };
 
-#define A_int_debug_idle       0x03020000
-u32 A_int_debug_idle_used[] = {
+#define A_dsa_temp_target      0x00000000
+u32 A_dsa_temp_target_used[] = {
+       0x00000040,
 };
 
-#define A_int_debug_reselected 0x03040000
-u32 A_int_debug_reselected_used[] = {
+#define A_int_debug_break      0x03000000
+u32 A_int_debug_break_used[] = {
+       0x0000020f,
 };
 
-#define A_int_debug_scheduled  0x03010000
-u32 A_int_debug_scheduled_used[] = {
+#define A_int_debug_panic      0x030b0000
+u32 A_int_debug_panic_used[] = {
+       0x000001e4,
+       0x000001f0,
 };
 
 #define A_int_err_check_condition      0x00030000
 u32 A_int_err_check_condition_used[] = {
-       0x00000157,
+       0x00000194,
 };
 
 #define A_int_err_no_phase     0x00040000
@@ -2029,46 +2398,51 @@ u32 A_int_err_no_phase_used[] = {
 
 #define A_int_err_selected     0x00010000
 u32 A_int_err_selected_used[] = {
-       0x0000019e,
+       0x000001da,
 };
 
 #define A_int_err_unexpected_phase     0x00000000
 u32 A_int_err_unexpected_phase_used[] = {
-       0x00000084,
-       0x0000008a,
-       0x0000008e,
+       0x0000008c,
        0x00000092,
-       0x000000c8,
-       0x000000cc,
-       0x000000ce,
+       0x0000009a,
        0x000000d0,
-       0x0000010d,
+       0x000000d4,
+       0x000000d6,
+       0x000000de,
+       0x000000e2,
+       0x000000e4,
+       0x000000ec,
+       0x000000f0,
+       0x000000f2,
+       0x000000f4,
+       0x0000014c,
 };
 
 #define A_int_err_unexpected_reselect  0x00020000
 u32 A_int_err_unexpected_reselect_used[] = {
-       0x0000017c,
+       0x000001ba,
 };
 
 #define A_int_msg_1    0x01020000
 u32 A_int_msg_1_used[] = {
-       0x000000e2,
-       0x000000e4,
+       0x0000010e,
+       0x00000110,
 };
 
 #define A_int_msg_sdtr 0x01010000
 u32 A_int_msg_sdtr_used[] = {
-       0x0000012d,
+       0x0000016c,
 };
 
 #define A_int_msg_wdtr 0x01000000
 u32 A_int_msg_wdtr_used[] = {
-       0x00000121,
+       0x00000160,
 };
 
 #define A_int_norm_aborted     0x02040000
 u32 A_int_norm_aborted_used[] = {
-       0x000001f9,
+       0x00000245,
 };
 
 #define A_int_norm_command_complete    0x02020000
@@ -2093,160 +2467,177 @@ u32 A_int_norm_select_complete_used[] = {
 
 #define A_int_test_1   0x04000000
 u32 A_int_test_1_used[] = {
-       0x000001b9,
+       0x000001f5,
 };
 
 #define A_int_test_2   0x04010000
 u32 A_int_test_2_used[] = {
-       0x000001d1,
+       0x0000020d,
 };
 
 #define A_int_test_3   0x04020000
 u32 A_int_test_3_used[] = {
 };
 
-#define A_issue_dsa_head       0x00000000
-u32 A_issue_dsa_head_used[] = {
-       0x0000004f,
-       0x00000080,
-};
-
 #define A_msg_buf      0x00000000
 u32 A_msg_buf_used[] = {
-       0x000000d6,
-       0x0000010f,
-       0x00000119,
-       0x0000011f,
-       0x00000125,
-       0x0000012b,
+       0x00000102,
+       0x0000014e,
+       0x00000158,
+       0x0000015e,
+       0x00000164,
+       0x0000016a,
 };
 
 #define A_reconnect_dsa_head   0x00000000
 u32 A_reconnect_dsa_head_used[] = {
-       0x0000003f,
-       0x00000047,
-       0x00000162,
-       0x00000169,
+       0x0000006c,
+       0x00000074,
+       0x000001a0,
 };
 
 #define A_reselected_identify  0x00000000
 u32 A_reselected_identify_used[] = {
-       0x00000015,
-       0x0000015b,
+       0x00000045,
+       0x0000019c,
 };
 
 #define A_reselected_tag       0x00000000
 u32 A_reselected_tag_used[] = {
-       0x0000015e,
+};
+
+#define A_schedule     0x00000000
+u32 A_schedule_used[] = {
+       0x0000007e,
+       0x00000192,
+       0x000001e2,
+       0x00000217,
 };
 
 #define A_test_dest    0x00000000
 u32 A_test_dest_used[] = {
-       0x000001b7,
+       0x000001f3,
 };
 
 #define A_test_src     0x00000000
 u32 A_test_src_used[] = {
-       0x000001b6,
+       0x000001f2,
 };
 
-#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     0x000005d4
+#define Ent_cmdout_cmdout      0x0000024c
+#define Ent_command_complete   0x0000060c
+#define Ent_command_complete_msgin     0x0000061c
+#define Ent_data_transfer      0x00000254
+#define Ent_datain_to_jump     0x00000328
+#define Ent_debug_break        0x00000838
+#define Ent_dsa_code_begin     0x00000000
+#define Ent_dsa_code_check_reselect    0x000000f8
+#define Ent_dsa_code_fix_jump  0x0000003c
+#define Ent_dsa_code_restore_pointers  0x000000c0
+#define Ent_dsa_code_save_data_pointer 0x00000088
 #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      0x00000168
+#define Ent_dsa_schedule       0x00000168
+#define Ent_dsa_zero   0x00000168
+#define Ent_end_data_transfer  0x0000028c
+#define Ent_initiator_abort    0x00000860
+#define Ent_msg_in     0x00000404
+#define Ent_msg_in_restart     0x000003e4
+#define Ent_other_in   0x00000374
+#define Ent_other_out  0x0000033c
+#define Ent_other_transfer     0x000003ac
+#define Ent_reject_message     0x000005b4
+#define Ent_reselected_check_next      0x000006a4
+#define Ent_reselected_ok      0x00000750
+#define Ent_respond_message    0x000005ec
+#define Ent_select     0x000001fc
+#define Ent_select_msgout      0x00000214
+#define Ent_target_abort       0x00000840
+#define Ent_test_1     0x000007c4
+#define Ent_test_2     0x000007d8
+#define Ent_test_2_msgout      0x000007f0
+#define Ent_wait_reselect      0x00000654
 u32 LABELPATCHES[] = {
-       0x00000002,
-       0x0000000b,
-       0x0000000d,
-       0x0000001d,
+       0x00000008,
+       0x0000000a,
+       0x00000013,
+       0x00000016,
        0x0000001f,
-       0x0000002c,
-       0x0000002e,
-       0x0000003b,
-       0x00000042,
-       0x00000054,
-       0x00000058,
-       0x0000005c,
-       0x00000060,
-       0x00000064,
+       0x00000021,
+       0x0000004f,
+       0x00000051,
+       0x0000005b,
        0x00000068,
-       0x0000006a,
-       0x0000006e,
-       0x0000007b,
+       0x0000006f,
        0x00000082,
-       0x00000086,
-       0x00000088,
+       0x00000084,
+       0x0000008a,
+       0x0000008e,
        0x00000090,
-       0x00000094,
        0x00000096,
        0x00000098,
-       0x0000009a,
        0x0000009c,
-       0x000000a9,
-       0x000000ae,
-       0x000000b2,
-       0x000000bf,
-       0x000000c4,
-       0x000000ca,
+       0x0000009e,
+       0x000000a0,
+       0x000000a2,
+       0x000000a4,
+       0x000000b1,
+       0x000000b6,
+       0x000000ba,
+       0x000000c7,
+       0x000000cc,
        0x000000d2,
-       0x000000d4,
        0x000000d8,
        0x000000da,
-       0x000000dc,
-       0x000000de,
        0x000000e0,
        0x000000e6,
        0x000000e8,
-       0x00000105,
-       0x00000111,
-       0x00000113,
-       0x00000115,
-       0x0000011b,
-       0x00000127,
-       0x00000153,
-       0x00000159,
-       0x00000167,
-       0x0000016a,
-       0x0000016e,
-       0x00000172,
-       0x00000176,
-       0x0000017a,
-       0x00000191,
-       0x000001a2,
-       0x000001a6,
+       0x000000ee,
+       0x000000f6,
+       0x000000f8,
+       0x00000104,
+       0x00000106,
+       0x00000108,
+       0x0000010a,
+       0x0000010c,
+       0x00000112,
+       0x00000114,
+       0x00000129,
+       0x00000142,
+       0x00000148,
+       0x00000150,
+       0x00000152,
+       0x00000154,
+       0x0000015a,
+       0x00000166,
+       0x00000196,
+       0x000001a5,
        0x000001a8,
        0x000001ac,
        0x000001b0,
-       0x000001b2,
        0x000001b4,
-       0x000001bd,
-       0x000001bf,
-       0x000001db,
-       0x000001df,
-       0x000001e3,
-       0x000001e7,
-       0x000001eb,
-       0x000001ef,
+       0x000001b8,
+       0x000001cf,
+       0x000001de,
+       0x000001e8,
+       0x000001ea,
+       0x000001ee,
+       0x000001f9,
+       0x000001fb,
+       0x0000021b,
+       0x0000021d,
+       0x0000021f,
+       0x00000221,
+       0x00000223,
+       0x00000225,
+       0x00000229,
+       0x0000022d,
+       0x0000022f,
+       0x00000233,
+       0x00000235,
+       0x00000239,
+       0x0000023b,
 };
 
 struct {
@@ -2255,6 +2646,6 @@ struct {
 } EXTERNAL_PATCHES[] = {
 };
 
-u32 INSTRUCTIONS       = 260;
-u32 PATCHES    = 72;
+u32 INSTRUCTIONS       = 297;
+u32 PATCHES    = 79;
 u32 EXTERNAL_PATCHES_LEN       = 0;
index 9f0bf58b0c535951618a29895cfee4667b7d4815..c3d486fe50d7daa0f6e4b389332dd1d871c1510e 100644 (file)
@@ -2,13 +2,13 @@
 #undef A_NCR53c7xx_msg_reject
 #undef A_NCR53c7xx_sink
 #undef A_NCR53c7xx_zero
+#undef A_NOP_insn
+#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
 #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_array_value
+#undef A_dsa_temp_addr_dsa_value
+#undef A_dsa_temp_addr_new_value
+#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
 #undef A_int_test_1
 #undef A_int_test_2
 #undef A_int_test_3
-#undef A_issue_dsa_head
 #undef A_msg_buf
 #undef A_reconnect_dsa_head
 #undef A_reselected_identify
 #undef A_reselected_tag
+#undef A_schedule
 #undef A_test_dest
 #undef A_test_src
 #undef Ent_accept_message
 #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_begin
 #undef Ent_dsa_code_check_reselect
+#undef Ent_dsa_code_fix_jump
+#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
 #undef Ent_select_msgout
 #undef Ent_target_abort
 #undef Ent_test_1
 #undef Ent_test_2
 #undef Ent_test_2_msgout
+#undef Ent_wait_reselect
index 24ef306f7af9b106e968465cfbac08c1d009fd4c..41d35bd80ee118bf1f95afd6370c2c99c44673cc 100644 (file)
  * General Public License for more details.
  *
  *
- * $Id: aha152x.c,v 1.11 1995/12/06 21:18:35 fischer Exp $
+ * $Id: aha152x.c,v 1.12 1995/12/16 12:26:07 fischer Exp fischer $
  *
  * $Log: aha152x.c,v $
+ * Revision 1.12  1995/12/16  12:26:07  fischer
+ * - barrier()'s added
+ * - configurable RESET delay added
+ *
  * Revision 1.11  1995/12/06  21:18:35  fischer
  * - some minor updates
  *
  SKIP_BIOSTEST:
    Don't test for BIOS signature (AHA-1510 or disabled BIOS)
 
- SETUP0        { IOPORT, IRQ, SCSI_ID, RECONNECT, PARITY, SYNCHRONOUS }:
+ SETUP0        { IOPORT, IRQ, SCSI_ID, RECONNECT, PARITY, SYNCHRONOUS, DELAY }:
    override for the first controller
    
- SETUP1        { IOPORT, IRQ, SCSI_ID, RECONNECT, PARITY, SYNCHRONOUS }:
+ SETUP1        { IOPORT, IRQ, SCSI_ID, RECONNECT, PARITY, SYNCHRONOUS, DELAY }:
    override for the second controller
 
 
  LILO COMMAND LINE OPTIONS:
 
- aha152x=<IOPORT>[,<IRQ>[,<SCSI-ID>[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>]]]]]
+ aha152x=<IOPORT>[,<IRQ>[,<SCSI-ID>[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>]]]]]]
 
  The normal configuration can be overridden by specifying a command line.
  When you do this, the BIOS test is skipped. Entered values have to be
@@ -325,6 +329,7 @@ static struct aha152x_setup {
   int reconnect;
   int parity;
   int synchronous;
+  int delay;
 #ifdef DEBUG_AHA152X
   int debug;
 #endif
@@ -336,6 +341,7 @@ static struct Scsi_Host *aha152x_host[IRQS];
 #define CURRENT_SC       (HOSTDATA(shpnt)->current_SC)
 #define ISSUE_SC         (HOSTDATA(shpnt)->issue_SC)
 #define DISCONNECTED_SC          (HOSTDATA(shpnt)->disconnected_SC)
+#define DELAY             (HOSTDATA(shpnt)->delay)
 #define SYNCRATE         (HOSTDATA(shpnt)->syncrate[CURRENT_SC->target])
 #define MSG(i)            (HOSTDATA(shpnt)->message[i])
 #define MSGLEN            (HOSTDATA(shpnt)->message_len)
@@ -353,6 +359,7 @@ struct aha152x_hostdata {
   int           reconnect;
   int           parity;
   int           synchronous;
+  int           delay;
  
   unsigned char syncrate[8];
   
@@ -439,7 +446,7 @@ static void do_pause(unsigned amount) /* Pause for amount*10 milliseconds */
    unsigned long the_time = jiffies + amount; /* 0.01 seconds per jiffy */
 
    while (jiffies < the_time)
-     ;
+    barrier();
 }
 
 /*
@@ -497,7 +504,7 @@ static void make_acklow(struct Scsi_Host *shpnt)
   SETPORT(SXFRCTL0, CH1);
 
   while(TESTHI(SCSISIG, ACKI))
-    ;
+    barrier();
 }
 
 /*
@@ -519,7 +526,7 @@ static int getphase(struct Scsi_Host *shpnt)
       do
        {
           while(!((sstat1 = GETPORT(SSTAT1)) & (BUSFREE|SCSIRSTI|REQINIT)))
-           ;
+            barrier();
           if(sstat1 & BUSFREE)
            return P_BUSFREE;
           if(sstat1 & SCSIRSTI)
@@ -559,17 +566,18 @@ void aha152x_setup(char *str, int *ints)
   setup[setup_count].reconnect   = ints[0] >= 4 ? ints[4] : 1;
   setup[setup_count].parity      = ints[0] >= 5 ? ints[5] : 1;
   setup[setup_count].synchronous = ints[0] >= 6 ? ints[6] : 0 /* FIXME: 1 */;
+  setup[setup_count].delay       = ints[0] >= 7 ? ints[7] : 100;
 #ifdef DEBUG_AHA152X
-  setup[setup_count].debug       = ints[0] >= 7 ? ints[7] : DEBUG_DEFAULT;
-  if(ints[0]>7)
+  setup[setup_count].debug       = ints[0] >= 8 ? ints[8] : DEBUG_DEFAULT;
+  if(ints[0]>8)
     { 
-      printk("aha152x: usage: aha152x=<PORTBASE>[,<IRQ>[,<SCSI ID>"
-                "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>,[,<DEBUG>]]]]]]\n");
+      printk("aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
+                "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>[,<DEBUG>]]]]]]]\n");
 #else
-  if(ints[0]>6)
+  if(ints[0]>7)
     {
-      printk("aha152x: usage: aha152x=<PORTBASE>[,<IRQ>[,<SCSI ID>"
-             "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>]]]]]\n");
+      printk("aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
+             "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>]]]]]]\n");
 #endif
     }
   else 
@@ -665,13 +673,14 @@ int aha152x_detect(Scsi_Host_Template * tpnt)
       if(setup_count==0 || (override.io_port != setup[0].io_port))
         if(!aha152x_checksetup(&override))
        {
-                printk("\naha152x: SETUP0 (0x%x, %d, %d, %d, %d, %d) invalid\n",
+                printk("\naha152x: SETUP0 (0x%x, %d, %d, %d, %d, %d, %d) invalid\n",
                      override.io_port,
                      override.irq,
                      override.scsiid,
                      override.reconnect,
                      override.parity,
-                     override.synchronous);
+                     override.synchronous,
+                     override.delay);
        }
         else
           setup[setup_count++] = override;
@@ -686,13 +695,14 @@ int aha152x_detect(Scsi_Host_Template * tpnt)
       if(setup_count==0 || (override.io_port != setup[0].io_port))
         if(!aha152x_checksetup(&override))
        {
-                printk("\naha152x: SETUP1 (0x%x, %d, %d, %d, %d, %d) invalid\n",
+                printk("\naha152x: SETUP1 (0x%x, %d, %d, %d, %d, %d, %d) invalid\n",
                       override.io_port,
                       override.irq,
                       override.scsiid,
                       override.reconnect,
                       override.parity,
-                      override.synchronous);
+                      override.synchronous,
+                      override.delay);
     }
   else
           setup[setup_count++] = override;
@@ -764,6 +774,7 @@ int aha152x_detect(Scsi_Host_Template * tpnt)
       HOSTDATA(shpnt)->reconnect         = setup[i].reconnect;
       HOSTDATA(shpnt)->parity            = setup[i].parity;
       HOSTDATA(shpnt)->synchronous       = setup[i].synchronous;
+      HOSTDATA(shpnt)->delay             = setup[i].delay;
       HOSTDATA(shpnt)->debug             = setup[i].debug;
 
       HOSTDATA(shpnt)->aborting          = 0;
@@ -809,19 +820,20 @@ int aha152x_detect(Scsi_Host_Template * tpnt)
       SETBITS(SCSISEQ, SCSIRSTO);
   do_pause(30);
       CLRBITS(SCSISEQ, SCSIRSTO);
-  do_pause(60);
+      do_pause(setup[i].delay);
 
       aha152x_reset_ports(shpnt);
       
       printk("aha152x%d: vital data: PORTBASE=0x%03x, IRQ=%d, SCSI ID=%d,"
-             " reconnect=%s, parity=%s, synchronous=%s\n",
+             " reconnect=%s, parity=%s, synchronous=%s, delay=%d\n",
             i,
              shpnt->io_port,
              shpnt->irq,
              shpnt->this_id,
              HOSTDATA(shpnt)->reconnect ? "enabled" : "disabled",
              HOSTDATA(shpnt)->parity ? "enabled" : "disabled",
-             HOSTDATA(shpnt)->synchronous ? "enabled" : "disabled");
+             HOSTDATA(shpnt)->synchronous ? "enabled" : "disabled",
+             HOSTDATA(shpnt)->delay);
 
       request_region(shpnt->io_port, IO_RANGE, "aha152x");  /* Register */
   
@@ -1162,7 +1174,7 @@ int aha152x_reset(Scsi_Cmnd *SCpnt)
        SETPORT(SCSISEQ, SCSIRSTO);
        do_pause(30);
        SETPORT(SCSISEQ, 0);
-       do_pause(60);
+       do_pause(DELAY);
 
        SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
        SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
@@ -1251,7 +1263,7 @@ void aha152x_done(struct Scsi_Host *shpnt, int error)
        printk("BUS FREE loop, ");
 #endif
       while(TESTLO(SSTAT1, BUSFREE))
-       ;
+        barrier();
 #if defined(DEBUG_PHASES)
       if(HOSTDATA(shpnt)->debug & debug_phases)
        printk("BUS FREE\n");
@@ -1308,8 +1320,7 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
   */
   if(TESTHI(SSTAT0, SELDI) &&
       DISCONNECTED_SC &&
-      (!CURRENT_SC || (CURRENT_SC->SCp.phase & in_selection))
-    )
+      (!CURRENT_SC || (CURRENT_SC->SCp.phase & in_selection)) )
     {
       int identify_msg, target, i;
 
@@ -1453,6 +1464,18 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
          /* Enable SELECTION OUT sequence */
           SETBITS(SCSISEQ, ENSELO | ENAUTOATNO);
        
+        }
+      else
+        {
+          /* No command we are busy with and no new to issue */
+          printk("aha152x: ignoring spurious interrupt, nothing to do\n");
+          if(TESTHI(DMACNTRL0, SWINT)) {
+            printk("aha152x: SWINT is set!  Why?\n");
+            CLRBITS(DMACNTRL0, SWINT);
+          }
+          show_queues(shpnt);
+        }
+
 #if defined(DEBUG_RACE)
          leave_driver("(selecting) intr");
 #endif
@@ -1460,11 +1483,6 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
          return;
        }
 
-      /* No command we are busy with and no new to issue */
-      printk("aha152x: ignoring spurious interrupt, nothing to do\n");
-      return;
-    }
-
   /* the bus is busy with something */
 
 #if defined(DEBUG_INTR)
@@ -1630,7 +1648,7 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
         
         /* wait for data latch to become ready or a phase change */
         while(TESTLO(DMASTAT, INTSTAT))
-          ;
+          barrier();
         
 #if defined(DEBUG_MSGO)
         if(HOSTDATA(shpnt)->debug & debug_msgo)
@@ -1671,9 +1689,6 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
 
         MSGLEN=0;
 
-        if(MSGLEN>0)
-          aha152x_panic(shpnt, "oops, MSGLEN>0 !?");
-
         if(identify)
           CURRENT_SC->SCp.phase |= sent_ident;
 
@@ -1712,7 +1727,7 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
   
           /* wait for data latch to become ready or a phase change */
           while(TESTLO(DMASTAT, INTSTAT))
-           ;
+            barrier();
   
           for(i=0; i<CURRENT_SC->cmd_len && TESTLO(SSTAT1, PHASEMIS); i++)
          {
@@ -1868,7 +1883,7 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
                  }
                        else
                  {
-                           /* request SDTR is to slow, do it asynchronously */
+                           /* requested SDTR is too slow, do it asynchronously */
                            ADDMSG(MESSAGE_REJECT);
                             SYNCRATE = 0;
                          } 
@@ -1996,7 +2011,7 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
 #endif
            /* wait for PHASEMIS or full FIFO */
             while(TESTLO (DMASTAT, DFIFOFULL|INTSTAT))
-             ;
+              barrier();
 
 #if defined(DEBUG_DATAI)
             if(HOSTDATA(shpnt)->debug & debug_datai)
@@ -2009,7 +2024,7 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
              {
                /* wait for SCSI fifo to get empty */
                while(TESTLO(SSTAT2, SEMPTY))
-                 ;
+                 barrier();
 
                /* rest of data in FIFO */
                fifodata=GETPORT(FIFOSTAT);
@@ -2126,7 +2141,7 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
        /* transfer can be considered ended, when SCSIEN reads back zero */
        CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
         while(TESTHI(SXFRCTL0, SCSIEN))
-         ;
+          barrier();
         CLRBITS(DMACNTRL0, ENDMA);
 
 #if defined(DEBUG_DATAI) || defined(DEBUG_INTR)
@@ -2209,7 +2224,7 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
 
            /* wait for FIFO to get empty */
             while(TESTLO(DMASTAT, DFIFOEMP|INTSTAT))
-             ;
+              barrier();
 
 #if defined(DEBUG_DATAO)
             if(HOSTDATA(shpnt)->debug & debug_datao)
@@ -2263,7 +2278,7 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
 #endif
            /* wait for SCSI fifo to get empty */
             while(TESTLO(SSTAT2, SEMPTY))
-             ;
+              barrier();
 #if defined(DEBUG_DATAO)
             if(HOSTDATA(shpnt)->debug & debug_datao)
              printk("ok, left data (bytes=%d, buffers=%d) ",
@@ -2274,7 +2289,7 @@ void aha152x_intr(int irqno, struct pt_regs * regs)
 
            /* transfer can be considered ended, when SCSIEN reads back zero */
             while(TESTHI(SXFRCTL0, SCSIEN))
-             ;
+              barrier();
 
            CLRBITS(DMACNTRL0, ENDMA);
          }
index be1f4995e3a8752084ff5db4500073a8268704c4..3d543080237ead76b8b016bdc5397f96298c77f7 100644 (file)
@@ -2,7 +2,7 @@
 #define _AHA152X_H
 
 /*
- * $Id: aha152x.h,v 1.11 1995/12/07 01:03:20 fischer Exp root $
+ * $Id: aha152x.h,v 1.12 1995/12/07 01:03:48 fischer Exp fischer $
  */
 
 #if defined(__KERNEL__)
@@ -23,7 +23,7 @@ int aha152x_proc_info(char *buffer, char **start, off_t offset, int length, int
    (unless we support more than 1 cmd_per_lun this should do) */
 #define AHA152X_MAXQUEUE       7               
 
-#define AHA152X_REVID "Adaptec 152x SCSI driver; $Revision: 1.11 $"
+#define AHA152X_REVID "Adaptec 152x SCSI driver; $Revision: 1.12 $"
 
 extern struct proc_dir_entry proc_scsi_aha152x;
 
index 3ec69c5e3c3baf016e0a08e3db7877ac87355af7..47881aa8fac8fa589fdd13d14325ee26044550b2 100644 (file)
@@ -41,7 +41,7 @@
  *
  *    -- Daniel M. Eischen, deischen@iworks.InterWorks.org, 04/03/95
  *
- *  $Id: aic7xxx.c,v 2.10 1995/11/10 10:49:14 deang Exp $
+ *  $Id: aic7xxx.c,v 2.15 1995/12/17 19:43:20 deang Exp $
  *-M*************************************************************************/
 
 #ifdef MODULE
@@ -73,7 +73,7 @@ struct proc_dir_entry proc_scsi_aic7xxx = {
     S_IFDIR | S_IRUGO | S_IXUGO, 2
 };
 
-#define AIC7XXX_C_VERSION  "$Revision: 2.10 $"
+#define AIC7XXX_C_VERSION  "$Revision: 2.15 $"
 
 #define NUMBER(arr)     (sizeof(arr) / sizeof(arr[0]))
 #define MIN(a,b)        ((a < b) ? a : b)
@@ -160,25 +160,43 @@ struct proc_dir_entry proc_scsi_aic7xxx = {
 /* #define AIC7XXX_PROC_STATS */
 
 /*
- * Define this to use polling rather than using kernel support for waking
- * up a waiting process.
+ * For debugging the abort/reset code.
  */
-#define AIC7XXX_POLL
+/* #define AIC7XXX_DEBUG_ABORT */
+
+/*
+ * For general debug messages
+ */
+#define AIC7XXX_DEBUG
 
 /*
  * Controller type and options
  */
 typedef enum {
   AIC_NONE,
-  AIC_274x,    /* EISA aic7770 */
-  AIC_284x,    /* VLB  aic7770 */
-  AIC_7870,    /* PCI  aic7870/aic7871 motherboard or 294x */
-  AIC_7850,    /* PCI  aic7850 */
-  AIC_7872,    /* PCI  aic7870 on 394x */
-  AIC_7880,    /* PCI  aic7880/aic7881 motherboard or 294x Ultra */
-  AIC_7882,    /* PCI  aic7870 on 394x Ultra */
+  AIC_7770,    /* EISA aic7770 on motherboard */
+  AIC_7771,    /* EISA aic7771 on 274x */
+  AIC_284x,    /* VLB  aic7770 on 284x */
+  AIC_7850,    /* PCI  aic7850 */
+  AIC_7870,    /* PCI  aic7870 on motherboard */
+  AIC_7871,    /* PCI  aic7871 on 294x */
+  AIC_7872,    /* PCI  aic7872 on 3940 */
+  AIC_7873,    /* PCI  aic7873 on 3985 */
+  AIC_7874,    /* PCI  aic7874 on 294x Differential */
+  AIC_7880,    /* PCI  aic7880 on motherboard */
+  AIC_7881,    /* PCI  aic7881 on 294x Ultra */
+  AIC_7882,    /* PCI  aic7882 on 3940 Ultra */
+  AIC_7883,    /* PCI  aic7883 on 3985 Ultra */
+  AIC_7884     /* PCI  aic7884 on 294x Ultra Differential */
 } aha_type;
 
+typedef enum {
+  AIC_777x,    /* AIC-7770 based */
+  AIC_785x,    /* AIC-7850 based */
+  AIC_787x,    /* AIC-7870 based */
+  AIC_788x     /* AIC-7880 based */
+} aha_chip_type;
+
 typedef enum {
   AIC_SINGLE,  /* Single Channel */
   AIC_TWIN,    /* Twin Channel */
@@ -197,6 +215,34 @@ typedef enum {
   LIST_TAIL
 } insert_type;
 
+typedef enum {
+  ABORT_RESET_INACTIVE,
+  ABORT_RESET_PENDING,
+  ABORT_RESET_SUCCESS
+} aha_abort_reset_type;
+
+/*
+ * Define an array of board names that can be indexed by aha_type.
+ * Don't forget to change this when changing the types!
+ */
+static const char * board_names[] = {
+  "<AIC-7xxx Unknown>",                /* AIC_NONE */
+  "AIC-7770",                  /* AIC_7770 */
+  "AHA-2740",                  /* AIC_7771 */
+  "AHA-2840",                  /* AIC_284x */
+  "AIC-7850",                  /* AIC_7850 */
+  "AIC-7870",                  /* AIC_7870 */
+  "AHA-2940",                  /* AIC_7871 */
+  "AHA-3940",                  /* AIC_7872 */
+  "AHA-3985",                  /* AIC_7873 */
+  "AHA-2940 Differential",     /* AIC_7874 */
+  "AIC-7880 Ultra",            /* AIC_7880 */
+  "AHA-2940 Ultra",            /* AIC_7881 */
+  "AHA-3940 Ultra",            /* AIC_7882 */
+  "AHA-3985 Ultra",            /* AIC_7883 */
+  "AHA-2940 Ultra Differential"        /* AIC_7884 */
+};
+
 /*
  * There should be a specific return value for this in scsi.h, but
  * it seems that most drivers ignore it.
@@ -753,7 +799,7 @@ struct seeprom_config {
 #define CFXFER         0x0007          /* synchronous transfer rate */
 #define CFSYNCH                0x0008          /* enable synchronous transfer */
 #define CFDISC         0x0010          /* enable disconnection */
-#define CFWIDEB                0x0020          /* wide bus device */
+#define CFWIDEB                0x0020          /* wide bus device (wide card) */
 /* UNUSED              0x00C0 */
 #define CFSTART                0x0100          /* send start unit SCSI command */
 #define CFINCBIOS      0x0200          /* include in BIOS scan */
@@ -769,7 +815,8 @@ struct seeprom_config {
 #define CFBIOSEN       0x0004          /* BIOS enabled */
 /* UNUSED              0x0008 */
 #define CFSM2DRV       0x0010          /* support more than two drives */
-/* UNUSED              0x0060 */
+#define CF284XEXTEND   0x0020          /* extended translation (284x cards) */
+/* UNUSED              0x0040 */
 #define CFEXTEND       0x0080          /* extended translation enabled */
 /* UNUSED              0xFF00 */
   unsigned short bios_control;         /* word 16 */
@@ -779,10 +826,12 @@ struct seeprom_config {
  */
 /* UNUSED               0x0001 */
 #define CFULTRAEN       0x0002          /* Ultra SCSI speed enable (Ultra cards) */
+#define CF284XSELTO     0x0003          /* Selection timeout (284x cards) */
+#define CF284XFIFO      0x000C          /* FIFO Threshold (284x cards) */
 #define CFSTERM         0x0004          /* SCSI low byte termination (non-wide cards) */
 #define CFWSTERM        0x0008          /* SCSI high byte termination (wide card) */
 #define CFSPARITY      0x0010          /* SCSI parity */
-/* UNUSED              0x0020 */
+#define CF284XSTERM    0x0020          /* SCSI low byte termination (284x cards) */
 #define CFRESETB       0x0040          /* reset SCSI bus at IC initialization */
 /* UNUSED              0xFF80 */
   unsigned short adapter_control;      /* word 17 */
@@ -807,9 +856,6 @@ struct seeprom_config {
 
 };
 
-
-#define AIC7XXX_DEBUG
-
 /*
  * Pause the sequencer and wait for it to actually stop - this
  * is important since the sequencer can disable pausing for critical
@@ -968,15 +1014,6 @@ struct aic7xxx_scb {
        struct scatterlist  sg;
        struct scatterlist  sense_sg;
        unsigned char       sense_cmd[6];   /* Allocate 6 characters for sense command */
-#define TIMER_ENABLED          0x01
-#define TIMER_EXPIRED          0x02
-#define TIMED_CMD_DONE         0x04
-        volatile unsigned char timer_status;
-#ifndef AIC7XXX_POLL
-       struct wait_queue   *waiting;       /* wait queue for device reset command */
-        struct wait_queue   waitq;          /* waiting points to this */
-       struct timer_list   timer;          /* timeout for device reset command */
-#endif
 };
 
 typedef void (*timeout_fn)(unsigned long);
@@ -1010,6 +1047,7 @@ struct aic7xxx_host {
   int                      numscb;           /* current number of scbs */
   int                      extended;         /* extended xlate? */
   aha_type                 type;             /* card type */
+  aha_chip_type            chip_type;        /* chip base type */
   int                      ultra_enabled;    /* Ultra SCSI speed enabled */
   int                      chan_num;         /* for 3940/3985, channel number */
   aha_bus_type             bus_type;         /* normal/twin/wide bus */
@@ -1066,6 +1104,7 @@ struct aic7xxx_host_config {
   int              busrtime;   /* bus release time */
   int              walk_scbs;  /* external SCB RAM detected; walk the scb array */
   aha_type         type;       /* card type */
+  aha_chip_type    chip_type;  /* chip base type */
   int              ultra_enabled;    /* Ultra SCSI speed enabled */
   int              chan_num;   /* for 3940/3985, channel number */
   aha_bus_type     bus_type;   /* normal/twin/wide bus */
@@ -1103,7 +1142,7 @@ static struct {
 static int num_aic7xxx_syncrates =
     sizeof(aic7xxx_syncrates) / sizeof(aic7xxx_syncrates[0]);
 
-static int number_of_3940s = 0;
+static int number_of_39xxs = 0;
 
 #ifdef AIC7XXX_DEBUG
 
@@ -1140,7 +1179,7 @@ debug_config(struct aic7xxx_host_config *p)
    * release time and data FIFO threshold from the scsi_conf and
    * host_conf registers respectively.
    */
-  if ((p->type == AIC_274x) || (p->type == AIC_284x))
+  if (p->chip_type == AIC_777x)
   {
     dfthresh = (host_conf >> 6);
   }
@@ -1157,36 +1196,33 @@ debug_config(struct aic7xxx_host_config *p)
 
   switch (p->type)
   {
-    case AIC_274x:
-      printk("AIC7770%s AT EISA SLOT %d:\n", BUSW[p->bus_type], p->base >> 12);
+    case AIC_7770:
+    case AIC_7771:
+      printk("%s%s AT EISA SLOT %d:\n", board_names[p->type], BUSW[p->bus_type],
+             p->base >> 12);
       break;
 
     case AIC_284x:
-      printk("AIC7770%s AT VLB SLOT %d:\n", BUSW[p->bus_type], p->base >> 12);
-      break;
-
-    case AIC_7870:
-      printk("AIC7870/7871%s (PCI-bus):\n", BUSW[p->bus_type]);
+      printk("%s%s AT VLB SLOT %d:\n", board_names[p->type], BUSW[p->bus_type],
+             p->base >> 12);
       break;
 
     case AIC_7850:
-      printk("AIC7850%s (PCI-bus):\n", BUSW[p->bus_type]);
-      break;
-
+    case AIC_7870:
+    case AIC_7871:
     case AIC_7872:
-      printk("AIC7872%s (PCI-bus):\n", BUSW[p->bus_type]);
-      break;
-
+    case AIC_7873:
+    case AIC_7874:
     case AIC_7880:
-      printk("AIC7880/7881%s (PCI-bus):\n", BUSW[p->bus_type]);
-      break;
-
+    case AIC_7881:
     case AIC_7882:
-      printk("AIC7882%s (PCI-bus):\n", BUSW[p->bus_type]);
+    case AIC_7883:
+    case AIC_7884:
+      printk("%s%s (PCI-bus):\n", board_names[p->type], BUSW[p->bus_type]);
       break;
 
     default:
-      panic("aic7xxx debug_config: internal error\n");
+      panic("aic7xxx: (debug_config) internal error.\n");
   }
 
   printk("    irq %d\n"
@@ -1204,7 +1240,7 @@ debug_config(struct aic7xxx_host_config *p)
         SST[(scsi_conf >> 3) & 0x03],
         (scsi_conf & 0x40) ? "en" : "dis");
 
-  if (((p->type == AIC_274x) || (p->type == AIC_284x)) && p->parity == AIC_UNKNOWN)
+  if ((p->chip_type == AIC_777x) && (p->parity == AIC_UNKNOWN))
   {
     /*
      * Set the parity for 7770 based cards.
@@ -1217,7 +1253,7 @@ debug_config(struct aic7xxx_host_config *p)
           (p->parity == AIC_ENABLED) ? "en" : "dis");
   }
 
-  if (p->type == AIC_274x)
+  if ((p->type == AIC_7770) || (p->type == AIC_7771))
   {
     p->low_term = (scsi_conf & 0x80) ? AIC_ENABLED : AIC_DISABLED;
   }
@@ -1398,109 +1434,6 @@ aic7xxx_delay(int seconds)
   }
 }
 
-#ifdef AIC7XXX_POLL
-/*+F*************************************************************************
- * Function:
- *   aic7xxx_poll_scb
- *
- * Description:
- *   Function to poll for command completion when in aborting an SCB.
- *-F*************************************************************************/
-static void
-aic7xxx_poll_scb(struct aic7xxx_host *p,
-                 struct aic7xxx_scb  *scb,
-                 unsigned long       timeout_ticks)
-{
-  unsigned long timer_expiration = jiffies + timeout_ticks;
-
-  while ((jiffies < timer_expiration) && !(scb->timer_status & TIMED_CMD_DONE))
-  {
-    udelay(1000);  /* delay for 1 msec. */
-  }
-}
-
-#else
-/*+F*************************************************************************
- * Function:
- *   aic7xxx_scb_timeout
- *
- * Description:
- *   Called when a SCB reset command times out.  The input is actually
- *   a pointer to the SCB.
- *-F*************************************************************************/
-static void
-aic7xxx_scb_timeout(unsigned long data)
-{
-  struct aic7xxx_scb *scb = (struct aic7xxx_scb *) data;
-
-  scb->timer_status |= TIMER_EXPIRED;
-  wake_up(&(scb->waiting));
-}
-
-/*+F*************************************************************************
- * Function:
- *   aic7xxx_scb_untimeout
- *
- * Description:
- *   This function clears the timeout and wakes up a waiting SCB.
- *-F*************************************************************************/
-static void
-aic7xxx_scb_untimeout(struct aic7xxx_scb *scb)
-{
-  if (scb->timer_status & TIMER_ENABLED)
-  {
-    scb->timer_status = TIMED_CMD_DONE;
-    wake_up(&(scb->waiting));
-  }
-}
-#endif
-
-/*+F*************************************************************************
- * Function:
- *   aic7xxx_scb_tsleep
- *
- * Description:
- *   Emulates a BSD tsleep where a process can sleep for a specified
- *   amount of time, but may be awakened before that.  Linux provides
- *   a sleep_on, wake_up, add_timer, and del_timer which can be used to
- *   emulate tsleep, but there's not enough information available on
- *   how to use them.  For now, we'll just poll for SCB completion.
- *
- *   The parameter ticks is the number of clock ticks
- *   to wait before a timeout.  A 0 is returned if the scb does not
- *   timeout, 1 is returned for a timeout.
- *-F*************************************************************************/
-static int
-aic7xxx_scb_tsleep(struct aic7xxx_host *p,
-                   struct aic7xxx_scb  *scb,
-                   unsigned long        ticks)
-{
-  scb->timer_status = TIMER_ENABLED;
-#ifdef AIC7XXX_POLL
-  UNPAUSE_SEQUENCER(p);
-  aic7xxx_poll_scb(p, scb, ticks);
-#else
-  scb->waiting = &(scb->waitq);
-  scb->timer.expires = jiffies + ticks;
-  scb->timer.data = (unsigned long) scb;
-  scb->timer.function = (timeout_fn) aic7xxx_scb_timeout;
-  add_timer(&scb->timer);
-  UNPAUSE_SEQUENCER(p);
-  sleep_on(&(scb->waiting));
-  del_timer(&scb->timer);
-#endif
-  if (!(scb->timer_status & TIMED_CMD_DONE))
-  {
-    scb->timer_status = 0x0;
-    return (1);
-  }
-  else
-  {
-    scb->timer_status = 0x0;
-    return (0);
-  }
-}
-
 /*+F*************************************************************************
  * Function:
  *   rcs_version
@@ -1633,9 +1566,9 @@ aic7xxx_scsirate(struct aic7xxx_host *p, unsigned char *scsirate,
        */
       if (p->ultra_enabled)
       {
-        if (! (aic7xxx_syncrates[i].rate & ULTRA_SXFR))
+        if (!(aic7xxx_syncrates[i].rate & ULTRA_SXFR))
         {
-          printk ("aic7xxx: target %d, channel %c, requests %sMB/s transfers, "
+          printk ("aic7xxx: Target %d, channel %c, requests %sMB/s transfers, "
                   "but adapter in Ultra mode can only sync at 10MB/s or "
                   "above.\n", target, channel, aic7xxx_syncrates[i].english);
           break;  /* Use asynchronous transfers. */
@@ -1658,18 +1591,18 @@ aic7xxx_scsirate(struct aic7xxx_host *p, unsigned char *scsirate,
         }
       }
       *scsirate = (aic7xxx_syncrates[i].rate) | (offset & 0x0F);
-      printk("aic7xxx: target %d, channel %c, now synchronous at %sMB/s, "
-             "offset = 0x%x\n",
+      printk("aic7xxx: Target %d, channel %c, now synchronous at %sMB/s, "
+             "offset(0x%x).\n",
             target, channel, aic7xxx_syncrates[i].english, offset);
       return;
     }
   }
 
   /*
-   * Default to asyncronous transfer
+   * Default to asynchronous transfer
    */
   *scsirate = 0;
-  printk("aic7xxx: target %d, channel %c, using asynchronous transfers\n",
+  printk("aic7xxx: Target %d, channel %c, using asynchronous transfers.\n",
          target, channel);
 }
 
@@ -1760,6 +1693,10 @@ aic7xxx_match_scb(struct aic7xxx_scb *scb, int target, char channel)
   int targ = (scb->target_channel_lun >> 4) & 0x0F;
   char chan = (scb->target_channel_lun & SELBUSB) ? 'B' : 'A';
 
+#ifdef AIC7XXX_DEBUG_ABORT
+  printk ("aic7xxx: (match_scb) comparing target/channel %d/%c to scb %d/%c\n",
+          target, channel, targ, chan);
+#endif
   if (target == ALL_TARGETS)
   {
     return (chan == channel);
@@ -1783,6 +1720,10 @@ aic7xxx_unbusy_target(unsigned char target, char channel, int base)
   unsigned char active;
   unsigned long active_port = HA_ACTIVE0(base);
 
+#ifdef AIC7XXX_DEBUG_ABORT
+  printk ("aic7xxx: (unbusy_target) target/channel %d/%c\n",
+          target, channel);
+#endif
   if ((target > 0x07) || (channel == 'B'))
   {
     /*
@@ -1809,36 +1750,29 @@ aic7xxx_done(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
   long flags;
   Scsi_Cmnd *cmd = scb->cmd;
 
-  if (scb->timer_status & TIMER_ENABLED)
-  {
-#ifdef AIC7XXX_POLL
-    scb->timer_status |= TIMED_CMD_DONE;
-#else
-    aic7xxx_scb_untimeout(scb);
+#ifdef 0
+  printk ("aic7xxx: (done) target/channel %d/%d\n",
+          cmd->target, cmd->channel);
 #endif
-  }
-  else
-  {
-    /*
-     * This is a critical section, since we don't want the
-     * queue routine mucking with the host data.
-     */
-    save_flags(flags);
-    cli();
+  /*
+   * This is a critical section, since we don't want the
+   * queue routine mucking with the host data.
+   */
+  save_flags(flags);
+  cli();
 
-    /*
-     * Process the command after marking the scb as free
-     * and adding it to the free list.
-     */
-    scb->state = SCB_FREE;
-    scb->next = p->free_scb;
-    p->free_scb = &(p->scb_array[scb->position]);
-    scb->cmd = NULL;
+  /*
+   * Process the command after marking the scb as free
+   * and adding it to the free list.
+   */
+  scb->state = SCB_FREE;
+  scb->next = p->free_scb;
+  p->free_scb = &(p->scb_array[scb->position]);
+  scb->cmd = NULL;
 
-    restore_flags(flags);
+  restore_flags(flags);
 
-    cmd->scsi_done(cmd);
-  }
+  cmd->scsi_done(cmd);
 }
 
 /*+F*************************************************************************
@@ -1980,6 +1914,10 @@ aic7xxx_abort_waiting_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb,
   scb->cmd->result = (DID_RESET << 16);
   aic7xxx_done(p, scb);
 
+#ifdef AIC7XXX_DEBUG_ABORT
+  printk ("aic7xxx: (abort_waiting_scb) target/channel %d/%c, prev %d, "
+          "to_scb %d, next %d\n", target, channel, prev, timedout_scb, next);
+#endif
   return (next);
 }
 
@@ -2006,6 +1944,10 @@ aic7xxx_reset_device(struct aic7xxx_host *p, int target, char channel,
    */
   active_scb = inb(SCBPTR(base));
 
+#ifdef AIC7XXX_DEBUG_ABORT
+  printk ("aic7xxx: (reset_device) target/channel %d/%c, to_scb %d, "
+          "active_scb %d\n", target, channel, timedout_scb, active_scb);
+#endif
   /*
    * Search the QINFIFO.
    */
@@ -2075,7 +2017,7 @@ aic7xxx_reset_device(struct aic7xxx_host *p, int target, char channel,
    * are other (most likely tagged) commands that
    * were disconnected when the reset occured.
    */
-  for(i = 0; i < p->numscb; i++)
+  for (i = 0; i < p->numscb; i++)
   {
     scb = &(p->scb_array[i]);
     if ((scb->state & SCB_ACTIVE) && aic7xxx_match_scb(scb, target, channel))
@@ -2107,6 +2049,9 @@ aic7xxx_reset_device(struct aic7xxx_host *p, int target, char channel,
 static void
 aic7xxx_reset_current_bus(int base)
 {
+#ifdef AIC7XXX_DEBUG_ABORT
+    printk ("aic7xxx: (reset_current_bus)\n");
+#endif
   outb(SCSIRSTO, SCSISEQ(base));
   udelay(1000);
   outb(0, SCSISEQ(base));
@@ -2129,6 +2074,10 @@ aic7xxx_reset_channel(struct aic7xxx_host *p, char channel,
   unsigned long offset, offset_max;
   int found;
 
+#ifdef AIC7XXX_DEBUG_ABORT
+  printk ("aic7xxx: (reset_channel) channel %c, to_scb %d\n",
+          channel, timedout_scb);
+#endif
   /*
    * Clean up all the state information for the
    * pending transactions on this bus.
@@ -2189,6 +2138,10 @@ aic7xxx_reset_channel(struct aic7xxx_host *p, char channel,
   cur_channel = (sblkctl & SELBUSB) ? 'B' : 'A';
   if (cur_channel != channel)
   {
+#ifdef AIC7XXX_DEBUG_ABORT
+    printk ("aic7xxx: (reset_channel) Stealthily resetting channel %c\n",
+            channel);
+#endif
     /*
      * Stealthily reset the other bus without upsetting the current bus
      */
@@ -2203,6 +2156,10 @@ aic7xxx_reset_channel(struct aic7xxx_host *p, char channel,
    */
   else
   {
+#ifdef AIC7XXX_DEBUG_ABORT
+    printk ("aic7xxx: (reset_channel) Resetting current channel %c\n",
+            channel);
+#endif
     aic7xxx_reset_current_bus(base);
     RESTART_SEQUENCER(p);
   }
@@ -2260,7 +2217,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
     if (aic7xxx_spurious_count == 1)
     {
       aic7xxx_spurious_count = 2;
-      printk("aic7xxx_isr: Encountered spurious interrupt.\n");
+      printk("aic7xxx: (aic7xxx_isr) Encountered spurious interrupt.\n");
       return;
     }
     else
@@ -2286,7 +2243,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
      * It is sufficient that we check isr_count and not the spurious
      * interrupt count.
      */
-    printk("aic7xxx_isr: Encountered spurious interrupt.\n");
+    printk("aic7xxx: (aic7xxx_isr) Encountered spurious interrupt.\n");
     return;
   }
 
@@ -2302,7 +2259,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
     int i;
     unsigned char errno = inb(ERROR(base));
 
-    printk("aic7xxx_isr: brkadrint (0x%x):\n", errno);
+    printk("aic7xxx: (aic7xxx_isr) BRKADRINT error(0x%x):\n", errno);
     for (i = 0; i < NUMBER(hard_error); i++)
     {
       if (errno & hard_error[i].errno)
@@ -2311,7 +2268,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
       }
     }
 
-    panic("aic7xxx_isr: brkadrint, error = 0x%x, seqaddr = 0x%x\n",
+    panic("aic7xxx: (aic7xxx_isr) BRKADRINT, error(0x%x) seqaddr(0x%x).\n",
          inb(ERROR(base)), (inb(SEQADDR1(base)) << 8) | inb(SEQADDR0(base)));
   }
 
@@ -2337,21 +2294,21 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
     switch (intstat & SEQINT_MASK)
     {
       case BAD_PHASE:
-       panic("aic7xxx_isr: unknown scsi bus phase\n");
+       panic("aic7xxx: (aic7xxx_isr) Unknown scsi bus phase.\n");
        break;
 
       case SEND_REJECT:
         rej_byte = inb(HA_REJBYTE(base));
         if (rej_byte != 0x20)
         {
-          debug("aic7xxx_isr warning: issuing message reject, 1st byte 0x%x\n",
+          debug("aic7xxx: Warning - Issuing message reject, 1st byte(0x%x)\n",
                 rej_byte);
         }
         else
         {
           scb_index = inb(SCBPTR(base));
           scb = &(p->scb_array[scb_index]);
-          printk("aic7xxx_isr warning: Tagged message rejected for target %d,"
+          printk("aic7xxx: Warning - Tagged message rejected for target %d,"
                  " channel %c.\n", scsi_id, channel);
           scb->cmd->device->tagged_supported = 0;
           scb->cmd->device->tagged_queue = 0;
@@ -2359,15 +2316,15 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
        break;
 
       case NO_IDENT:
-       panic("aic7xxx_isr: Target %d, channel %c, did not send an IDENTIFY "
-             "message.  SAVED_TCL = 0x%x\n",
+       panic("aic7xxx: Target %d, channel %c, did not send an IDENTIFY "
+             "message. SAVED_TCL(0x%x).\n",
               scsi_id, channel, inb(SAVED_TCL(base)));
        break;
 
       case NO_MATCH:
-       printk("aic7xxx_isr: No active SCB for reconnecting target %d, "
-             "channel %c - issuing ABORT\n", scsi_id, channel);
-        printk("SAVED_TCL = 0x%x\n", inb(SAVED_TCL(base)));
+       printk("aic7xxx: No active SCB for reconnecting target %d, "
+             "channel %c - Issuing ABORT. SAVED_TCL(0x%x).\n",
+              scsi_id, channel, inb(SAVED_TCL(base)));
         aic7xxx_unbusy_target(scsi_id, channel, base);
         outb(SCB_NEEDDMA, SCBARRAY(base));
 
@@ -2430,7 +2387,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
            /*
             * Send our own SDTR in reply.
             */
-           printk("aic7xxx_isr: Sending SDTR!!\n");
+           printk("aic7xxx: Sending SDTR!!\n");
            outb(SEND_SDTR, HA_RETURN_1(base));
          }
        }
@@ -2450,8 +2407,8 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
       case MSG_WDTR:
       {
        bus_width = inb(ACCUM(base));
-       printk("aic7xxx_isr: Received MSG_WDTR, scsi_id %d, channel %c "
-              "needwdtr = 0x%x\n", scsi_id, channel, p->needwdtr);
+       printk("aic7xxx: Received MSG_WDTR, Target %d, channel %c "
+              "needwdtr(0x%x).\n", scsi_id, channel, p->needwdtr);
        scratch = inb(HA_TARG_SCRATCH(base) + scratch_offset);
 
        if (p->wdtr_pending & target_mask)
@@ -2467,8 +2424,8 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
              break;
 
            case BUS_16_BIT:
-             printk("aic7xxx_isr: target %d, channel %c, using 16 bit transfers\n",
-                    scsi_id, channel);
+             printk("aic7xxx: Target %d, channel %c, using 16 bit "
+                     "transfers.\n", scsi_id, channel);
              scratch |= 0x80;
              break;
          }
@@ -2478,7 +2435,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
          /*
           * Send our own WDTR in reply.
           */
-         printk("aic7xxx_isr: Will send WDTR!!\n");
+         printk("aic7xxx: Will send WDTR!!\n");
          switch (bus_width)
          {
            case BUS_8_BIT:
@@ -2493,8 +2450,8 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
              /* Yes, we mean to fall thru here. */
 
            case BUS_16_BIT:
-             printk("aic7xxx_isr: target %d, channel %c, using 16 bit transfers\n",
-                    scsi_id, channel);
+             printk("aic7xxx: Target %d, channel %c, using 16 bit "
+                     "transfers.\n", scsi_id, channel);
              scratch |= 0x80;
              break;
          }
@@ -2527,8 +2484,8 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
          p->needwdtr &= ~target_mask;
          p->wdtr_pending &= ~target_mask;
          outb(scratch, HA_TARG_SCRATCH(base) + scratch_offset);
-         printk("aic7xxx: target %d, channel %c, refusing WIDE negotiation. "
-                 "Using 8 bit transfers\n", scsi_id, channel);
+         printk("aic7xxx: Target %d, channel %c, refusing WIDE negotiation. "
+                 "Using 8 bit transfers.\n", scsi_id, channel);
        }
        else
        {
@@ -2541,8 +2498,9 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
            p->needsdtr &= ~target_mask;
            p->sdtr_pending &= ~target_mask;
            outb(scratch, HA_TARG_SCRATCH(base) + scratch_offset);
-           printk("aic7xxx: target %d, channel %c, refusing syncronous negotiation. "
-                   "Using asyncronous transfers\n", scsi_id, channel);
+           printk("aic7xxx: Target %d, channel %c, refusing synchronous "
+                   "negotiation. Using asynchronous transfers.\n",
+                   scsi_id, channel);
          }
          /*
           * Otherwise, we ignore it.
@@ -2559,8 +2517,8 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
        outb(0, HA_RETURN_1(base));   /* CHECK_CONDITION may change this */
        if ((scb->state != SCB_ACTIVE) || (scb->cmd == NULL))
        {
-         printk("aic7xxx_isr: referenced scb not valid "
-                "during seqint 0x%x scb(%d) state(%x), cmd(%x)\n",
+         printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
+                "scb(%d) state(0x%x) cmd(0x%x).\n",
                 intstat, scb_index, scb->state, (unsigned int) scb->cmd);
        }
        else
@@ -2574,7 +2532,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
          switch (status_byte(scb->target_status))
          {
            case GOOD:
-              printk("aic7xxx_isr: Interrupted for status of 0???\n");
+              printk("aic7xxx: Interrupted for status of GOOD???\n");
              break;
 
            case CHECK_CONDITION:
@@ -2648,7 +2606,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
              break;
 
            case BUSY:
-             printk("aic7xxx_isr: Target busy\n");
+             printk("aic7xxx: Target busy.\n");
              if (!aic7xxx_error(cmd))
              {
                aic7xxx_error(cmd) = DID_BUS_BUSY;
@@ -2656,7 +2614,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
              break;
 
            case QUEUE_FULL:
-             printk("aic7xxx_isr: Queue full\n");
+             printk("aic7xxx: Queue full.\n");
              if (!aic7xxx_error(cmd))
              {
                aic7xxx_error(cmd) = DID_RETRY_COMMAND;
@@ -2664,7 +2622,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
              break;
 
            default:
-             printk("aic7xxx_isr: Unexpected target status 0x%x\n",
+             printk("aic7xxx: Unexpected target status(0x%x).\n",
                     scb->target_status);
              if (!aic7xxx_error(cmd))
              {
@@ -2680,8 +2638,8 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
        scb = &(p->scb_array[scb_index]);
        if ((scb->state != SCB_ACTIVE) || (scb->cmd == NULL))
        {
-         printk("aic7xxx_isr: referenced scb not valid "
-                "during seqint 0x%x scb(%d) state(%x), cmd(%x)\n",
+         printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
+                "scb(%d) state(0x%x) cmd(0x%x).\n",
                 intstat, scb_index, scb->state, (unsigned int) scb->cmd);
        }
        else
@@ -2707,9 +2665,10 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
 
            if (actual < cmd->underflow)
            {
-             printk("aic7xxx: target %d underflow - "
-                    "wanted (at least) %u, got %u, count=%d\n",
-                    cmd->target, cmd->underflow, actual, inb(SCBARRAY(base + 18)));
+             printk("aic7xxx: Target %d underflow - "
+                    "Wanted (at least) (%u) got(%u) count(%d).\n",
+                    cmd->target, cmd->underflow, actual,
+                     inb(SCBARRAY(base + 18)));
              aic7xxx_error(cmd) = DID_RETRY_COMMAND;
              aic7xxx_status(cmd) = scb->target_status;
            }
@@ -2722,20 +2681,20 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
        scb = &(p->scb_array[scb_index]);
        if ((scb->state != SCB_ACTIVE) || (scb->cmd == NULL))
        {
-         printk("aic7xxx_isr: referenced scb not valid "
-                "during seqint 0x%x scb(%d) state(%x), cmd(%x)\n",
+         printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
+                "scb(%d) state(0x%x) cmd(0x%x)\n",
                 intstat, scb_index, scb->state, (unsigned int) scb->cmd);
        }
        else
        {
          cmd = scb->cmd;
          /*
-          * We didn't recieve a valid tag back from the target
+          * We didn't receive a valid tag back from the target
           * on a reconnect.
           */
-         printk("aic7xxx_isr: invalid tag recieved on channel %c "
-                "target %d, lun %d -- sending ABORT_TAG\n",
-                 channel, scsi_id, cmd->lun & 0x07);
+         printk("aic7xxx: Invalid tag received on target %d, channel %c, "
+                 "lun %d - Sending ABORT_TAG.\n",
+                 scsi_id, channel, cmd->lun & 0x07);
 
          cmd->result = (DID_RETRY_COMMAND << 16);
           aic7xxx_done(p, scb);
@@ -2747,8 +2706,8 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
        scb = &(p->scb_array[scb_index]);
        if ((scb->state != SCB_ACTIVE) || (scb->cmd == NULL))
        {
-         printk("aic7xxx_isr: referenced scb not valid "
-                "during seqint 0x%x scb(%d) state(%x), cmd(%x)\n",
+         printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
+                "scb(%d) state(0x%x) cmd(0x%x).\n",
                 intstat, scb_index, scb->state, (unsigned int) scb->cmd);
        }
        else
@@ -2760,13 +2719,17 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
           */
           if (scb->state & SCB_DEVICE_RESET)
           {
+#ifdef AIC7XXX_DEBUG_ABORT
+  printk ("aic7xxx: (isr) sending bus device reset to target %d\n",
+          scsi_id);
+#endif
             outb(MSG_BUS_DEVICE_RESET, HA_MSG_START(base));
             outb(1, HA_MSG_LEN(base));
           }
           else
           {
-            panic("aic7xxx_isr: AWAITING_SCB for an SCB that does "
-                  "not have a waiting message");
+            panic("aic7xxx: AWAITING_SCB for an SCB that does "
+                  "not have a waiting message.\n");
           }
        }
        break;
@@ -2774,6 +2737,10 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
       case IMMEDDONE:
         scb_index = inb(SCBPTR(base));
        scb = &(p->scb_array[scb_index]);
+#ifdef AIC7XXX_DEBUG_ABORT
+  printk ("aic7xxx: (isr) received IMMEDDONE for target %d, scb %d, state %d\n",
+          scsi_id, scb_index, scb->state);
+#endif
         if (scb->state & SCB_DEVICE_RESET)
         {
           int found;
@@ -2793,12 +2760,12 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
         }
         else
         {
-          panic("aic7xxx_isr: Immediate complete for unknown operation.\n");
+          panic("aic7xxx: Immediate complete for unknown operation.\n");
         }
         break;
 
       default:               /* unknown */
-       debug("aic7xxx_isr: seqint, intstat = 0x%x, scsisigi = 0x%x\n",
+       debug("aic7xxx: SEQINT, INTSTAT(0x%x) SCSISIGI(0x%x).\n",
              intstat, inb(SCSISIGI(base)));
        break;
     }
@@ -2814,7 +2781,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
     scb = &(p->scb_array[scb_index]);
     if ((scb->state != SCB_ACTIVE) || (scb->cmd == NULL))
     {
-      printk("aic7xxx_isr: no command for scb (scsiint)\n");
+      printk("aic7xxx: No command for SCB (SCSIINT).\n");
       /*
        * Turn off the interrupt and set status
        * to zero, so that it falls through the
@@ -2900,7 +2867,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
        RESTART_SEQUENCER(p);
         aic7xxx_done(p, scb);
 #if 0
-  printk("aic7xxx_isr: SELTO scb(%d) state(%x), cmd(%x)\n",
+  printk("aic7xxx: SELTO SCB(%d) state(0x%x) cmd(0x%x).\n",
         scb->position, scb->state, (unsigned int) scb->cmd);
 #endif
       }
@@ -2912,11 +2879,8 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
           * A parity error has occurred during a data
           * transfer phase. Flag it and continue.
           */
-         printk("aic7xxx: parity error on target %d, "
-                "channel %d, lun %d\n",
-                cmd->target,
-                cmd->channel & 0x01,
-                cmd->lun & 0x07);
+         printk("aic7xxx: Parity error on target %d, channel %d, lun %d.\n",
+                cmd->target, cmd->channel & 0x01, cmd->lun & 0x07);
          aic7xxx_error(cmd) = DID_PARITY;
 
          /*
@@ -2936,7 +2900,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
              * We don't know what's going on. Turn off the
              * interrupt source and try to continue.
              */
-            printk("aic7xxx_isr: sstat1 = 0x%x\n", status);
+            printk("aic7xxx: SSTAT1(0x%x).\n", status);
             outb(status, CLRSINT1(base));
             UNPAUSE_SEQUENCER(p);
             outb(CLRSCSIINT, CLRINT(base));
@@ -2962,9 +2926,8 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
       scb = &(p->scb_array[complete]);
       if ((scb->state != SCB_ACTIVE) || (scb->cmd == NULL))
       {
-       printk("aic7xxx warning: "
-              "no command for scb %d (cmdcmplt)\n"
-              "QOUTCNT = %d, SCB state = 0x%x, CMD = 0x%x, pos = %d\n",
+       printk("aic7xxx: Warning - No command for SCB %d (CMDCMPLT).\n"
+              "         QOUTCNT(%d) SCB state(0x%x) cmd(0x%x) pos(%d).\n",
               complete, inb(QOUTFIFO(base)),
               scb->state, (unsigned int) scb->cmd, scb->position);
        outb(CLRCMDINT, CLRINT(base));
@@ -2981,7 +2944,7 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
        cmd->flags &= ASKED_FOR_SENSE;
       }
 #if 0
-      printk("aic7xxx_intr: (complete) state = %d, cmd = 0x%x, free = 0x%x\n",
+      printk("aic7xxx: (complete) State(%d) cmd(0x%x) free(0x%x).\n",
             scb->state, (unsigned int) scb->cmd, (unsigned int) p->free_scb);
 #endif
 
@@ -2999,9 +2962,9 @@ aic7xxx_isr(int irq, struct pt_regs * regs)
 #if 0
   if (scb != &p->scb_array[scb->position])
   {
-    printk("aic7xxx_isr: (complete) address mismatch, pos %d\n", scb->position);
+    printk("aic7xxx: (complete) Address mismatch, pos(%d).\n", scb->position);
   }
-  printk("aic7xxx_isr: (complete) state = %d, cmd = 0x%x, free = 0x%x\n",
+  printk("aic7xxx: (complete) State(%d) cmd(0x%x) free(0x%x).\n",
         scb->state, (unsigned int) scb->cmd, (unsigned int) p->free_scb);
 #endif
 
@@ -3084,8 +3047,8 @@ aic7xxx_probe(int slot, int base)
     unsigned char signature[sizeof(buf)];
     aha_type type;
   } AIC7xxx[] = {
-    { 4, { 0x04, 0x90, 0x77, 0x71 }, AIC_274x },  /* host adapter 274x */
-    { 4, { 0x04, 0x90, 0x77, 0x70 }, AIC_274x },  /* motherboard 274x  */
+    { 4, { 0x04, 0x90, 0x77, 0x71 }, AIC_7771 },  /* host adapter 274x */
+    { 4, { 0x04, 0x90, 0x77, 0x70 }, AIC_7770 },  /* motherboard 7770  */
     { 4, { 0x04, 0x90, 0x77, 0x56 }, AIC_284x },  /* 284x, BIOS enabled */
     { 4, { 0x04, 0x90, 0x77, 0x57 }, AIC_284x }   /* 284x, BIOS disabled */
   };
@@ -3112,7 +3075,7 @@ aic7xxx_probe(int slot, int base)
        return (AIC7xxx[i].type);
       }
 
-      printk("aic7xxx disabled at slot %d, ignored\n", slot);
+      printk("aic7xxx: Disabled at slot %d, ignored.\n", slot);
     }
   }
 
@@ -3478,7 +3441,8 @@ detect_maxscb(aha_type type, int base, int walk_scbs)
 
   switch (type)
   {
-    case AIC_274x:
+    case AIC_7770:
+    case AIC_7771:
     case AIC_284x:
       /*
        * Check for Rev C or E boards. Rev E boards can supposedly have
@@ -3496,13 +3460,13 @@ detect_maxscb(aha_type type, int base, int walk_scbs)
         /*
          * We detected a Rev E board.
          */
-       printk("aic7770: Rev E and subsequent; using 4 SCB's\n");
+       printk("aic7770: Rev E and subsequent; using 4 SCB's.\n");
        outb(sblkctl_reg ^ AUTOFLUSHDIS, SBLKCTL(base));
        maxscb = 4;
       }
       else
       {
-       printk("aic7770: Rev C and previous; using 4 SCB's\n");
+       printk("aic7770: Rev C and previous; using 4 SCB's.\n");
        maxscb = 4;
       }
       break;
@@ -3512,12 +3476,18 @@ detect_maxscb(aha_type type, int base, int walk_scbs)
       break;
 
     case AIC_7870:
+    case AIC_7871:
+    case AIC_7874:
     case AIC_7880:
+    case AIC_7881:
+    case AIC_7884:
       maxscb = 16;
       break;
 
     case AIC_7872:
+    case AIC_7873:
     case AIC_7882:
+    case AIC_7883:
       /*
        * Is suppose to have 255 SCBs, but we'll walk the SCBs
        * looking for more if external RAM is detected.
@@ -3549,7 +3519,7 @@ detect_maxscb(aha_type type, int base, int walk_scbs)
       }
       i++;
     }
-    maxscb = i;                
+    maxscb = i;
   }
 
   return (maxscb);
@@ -3566,8 +3536,6 @@ static int
 aic7xxx_register(Scsi_Host_Template *template,
                  struct aic7xxx_host_config *config)
 {
-  static const char * board_name[] = {"", "274x", "284x", "7870", "7850",
-                                      "7872", "7881"};
   int i;
   unsigned char sblkctl;
   int max_targets;
@@ -3589,10 +3557,8 @@ aic7xxx_register(Scsi_Host_Template *template,
 
   switch (config->type)
   {
-    case AIC_274x:
-#if 0
-      printk("aha274x: HCNTRL:0x%x\n", inb(HCNTRL(base)));
-#endif
+    case AIC_7770:
+    case AIC_7771:
       /*
        * For some 274x boards, we must clear the CHIPRST bit
        * and pause the sequencer. For some reason, this makes
@@ -3611,7 +3577,7 @@ aic7xxx_register(Scsi_Host_Template *template,
       aic7xxx_delay(1);
       if (inb(HCNTRL(base)) & CHIPRST)
       {
-       printk("aic7xxx_register: Chip reset not cleared; clearing manually.\n");
+       printk("aic7xxx: Chip reset not cleared; clearing manually.\n");
       }
       outb(config->pause, HCNTRL(base));
 
@@ -3634,14 +3600,11 @@ aic7xxx_register(Scsi_Host_Template *template,
       /*
        * A reminder until this can be detected automatically.
        */
-      printk("aha274x: Extended translation %sabled\n",
+      printk("aic7xxx: Extended translation %sabled.\n",
             config->extended ? "en" : "dis");
       break;
 
     case AIC_284x:
-#if 0
-      printk("aha284x: HCNTRL:0x%x\n", inb(HCNTRL(base)));
-#endif
       outb(CHIPRST, HCNTRL(base));
       config->unpause = UNPAUSE_284X;
       config->pause = REQ_PAUSE; /* DWG would like to be like the rest */
@@ -3656,22 +3619,28 @@ aic7xxx_register(Scsi_Host_Template *template,
       }
       host_conf = inb(HA_HOSTCONF(base));
 
-      printk("aha284x: Reading SEEPROM...");
+      printk("aic7xxx: Reading SEEPROM...");
       have_seeprom = read_2840_seeprom(base, &sc);
       if (!have_seeprom)
       {
-       printk("aha284x: Unable to read SEEPROM\n");
+       printk("aic7xxx: Unable to read SEEPROM.\n");
         config->busrtime = host_conf & 0x3C;
       }
       else
       {
        printk("done.\n");
-       config->extended = ((sc.bios_control & CFEXTEND) >> 7);
+       config->extended = ((sc.bios_control & CF284XEXTEND) >> 5);
        config->scsi_id = (sc.brtime_id & CFSCSIID);
        config->parity = (sc.adapter_control & CFSPARITY) ?
                         AIC_ENABLED : AIC_DISABLED;
-       config->low_term = (sc.adapter_control & CFSTERM) ?
+       config->low_term = (sc.adapter_control & CF284XSTERM) ?
                              AIC_ENABLED : AIC_DISABLED;
+       /*
+        * XXX - Adaptec *does* make 284x wide controllers, but the
+        *       documents do not say where the high byte termination
+        *       enable bit is located.  For now, we'll just assume
+        *       that it's in the same place as for the 2940 card.
+        */
        config->high_term = (sc.adapter_control & CFWSTERM) ?
                              AIC_ENABLED : AIC_DISABLED;
        config->busrtime = ((sc.brtime_id & CFBRTIME) >> 8);
@@ -3681,19 +3650,21 @@ aic7xxx_register(Scsi_Host_Template *template,
       outb(host_conf & DFTHRSH, BUSSPD(base));
       outb((host_conf << 2) & BOFF, BUSTIME(base));
 
-      printk("aha284x: Extended translation %sabled\n",
+      printk("aic7xxx: Extended translation %sabled.\n",
             config->extended ? "en" : "dis");
       break;
 
     case AIC_7850:
     case AIC_7870:
+    case AIC_7871:
     case AIC_7872:
+    case AIC_7873:
+    case AIC_7874:
     case AIC_7880:
+    case AIC_7881:
     case AIC_7882:
-#if 0
-      printk("aic%s HCNTRL:0x%x\n", board_name[config->type], inb(HCNTRL(base)));
-#endif
-
+    case AIC_7883:
+    case AIC_7884:
       outb(CHIPRST, HCNTRL(base));
       config->unpause = UNPAUSE_294X;
       config->pause = config->unpause | PAUSE;
@@ -3703,11 +3674,11 @@ aic7xxx_register(Scsi_Host_Template *template,
       config->extended = aic7xxx_extended;
       config->scsi_id = 7;
 
-      printk("aic78xx: Reading SEEPROM...");
+      printk("aic7xxx: Reading SEEPROM...");
       have_seeprom = read_seeprom(base, config->chan_num * (sizeof(sc) / 2), &sc);
       if (!have_seeprom)
       {
-       printk("aic78xx: Unable to read SEEPROM\n");
+       printk("aic7xxx: Unable to read SEEPROM.\n");
       }
       else
       {
@@ -3721,7 +3692,8 @@ aic7xxx_register(Scsi_Host_Template *template,
        config->high_term = (sc.adapter_control & CFWSTERM) ?
                              AIC_ENABLED : AIC_DISABLED;
        config->busrtime = ((sc.brtime_id & CFBRTIME) >> 8);
-        if (((config->type == AIC_7880) || (config->type == AIC_7882)) &&
+        if (((config->type == AIC_7880) || (config->type == AIC_7882) ||
+             (config->type == AIC_7883) || (config->type == AIC_7884)) &&
             (sc.adapter_control & CFULTRAEN))
         {
           printk ("aic7xxx: Enabling support for Ultra SCSI speed.\n");
@@ -3741,25 +3713,25 @@ aic7xxx_register(Scsi_Host_Template *template,
        */
       outb(config->scsi_id, (HA_SCSICONF(base) + 1));
 
-      printk("aic%s: Extended translation %sabled\n", board_name[config->type],
+      printk("aic7xxx: Extended translation %sabled.\n",
             config->extended ? "en" : "dis");
       break;
 
     default:
-      panic("aic7xxx_register: internal error\n");
+      panic("aic7xxx: (aic7xxx_register) Internal error.\n");
   }
 
   config->maxscb = detect_maxscb(config->type, base, config->walk_scbs);
 
-  if ((config->type == AIC_274x) || (config->type == AIC_284x))
+  if (config->chip_type == AIC_777x)
   {
     if (config->pause & IRQMS)
     {
-      printk("aic7xxx: Using Level Sensitive Interrupts\n");
+      printk("aic7xxx: Using level sensitive interrupts.\n");
     }
     else
     {
-      printk("aic7xxx: Using Edge Triggered Interrupts\n");
+      printk("aic7xxx: Using edge triggered interrupts.\n");
     }
   }
 
@@ -3779,8 +3751,8 @@ aic7xxx_register(Scsi_Host_Template *template,
     case SELWIDE:     /* Wide bus */
       config->scsi_id = inb(HA_SCSICONF(base) + 1) & 0x0F;
       config->bus_type = AIC_WIDE;
-      printk("aic7xxx: Enabling wide channel of %s-Wide\n",
-            board_name[config->type]);
+      printk("aic7xxx: Enabling wide channel of %s-Wide.\n",
+            board_names[config->type]);
       outb(WIDE_BUS, HA_FLAGS(base));
       break;
 
@@ -3789,19 +3761,19 @@ aic7xxx_register(Scsi_Host_Template *template,
 #ifdef AIC7XXX_TWIN_SUPPORT
       config->scsi_id_b = inb(HA_SCSICONF(base) + 1) & 0x07;
       config->bus_type = AIC_TWIN;
-      printk("aic7xxx: Enabled channel B of %s-Twin\n",
-            board_name[config->type]);
+      printk("aic7xxx: Enabled channel B of %s-Twin.\n",
+            board_names[config->type]);
       outb(TWIN_BUS, HA_FLAGS(base));
 #else
       config->bus_type = AIC_SINGLE;
-      printk("aic7xxx: Channel B of %s-Twin will be ignored\n",
-            board_name[config->type]);
+      printk("aic7xxx: Channel B of %s-Twin will be ignored.\n",
+            board_names[config->type]);
       outb(0, HA_FLAGS(base));
 #endif
       break;
 
     default:
-      printk("aic7xxx is an unsupported type 0x%x, please "
+      printk("aic7xxx: Unsupported type 0x%x, please "
             "mail deang@ims.com\n", inb(SBLKCTL(base)));
       outb(0, HA_FLAGS(base));
       return (0);
@@ -3822,12 +3794,11 @@ aic7xxx_register(Scsi_Host_Template *template,
    *     in the lower four bits; the ECU information shows the
    *     high bit being used as well. Which is correct?
    *
-   * The 294x cards (PCI) get their interrupt from PCI BIOS.
+   * The PCI cards get their interrupt from PCI BIOS.
    */
-  if (((config->type == AIC_274x) || (config->type == AIC_284x)) &&
-      (config->irq < 9 || config->irq > 15))
+  if ((config->chip_type == AIC_777x) && ((config->irq < 9) || (config->irq > 15)))
   {
-    printk("aic7xxx uses unsupported IRQ level, ignoring.\n");
+    printk("aic7xxx: Host adapter uses unsupported IRQ level, ignoring.\n");
     return (0);
   }
 
@@ -3841,7 +3812,7 @@ aic7xxx_register(Scsi_Host_Template *template,
 #ifndef AIC7XXX_SHARE_IRQS
    if (aic7xxx_boards[config->irq] != NULL)
    {
-     printk("aic7xxx_register: Sharing of IRQs is not configured.\n");
+     printk("aic7xxx: Sharing of IRQ's is not configured.\n");
      return (0);
    }
 #endif
@@ -3866,8 +3837,8 @@ aic7xxx_register(Scsi_Host_Template *template,
 
     if (SG_STRUCT_CHECK(sg))
     {
-      printk("aic7xxx warning: kernel scatter-gather "
-            "structures changed, disabling it.\n");
+      printk("aic7xxx: Warning - Kernel scatter-gather structures changed, "
+             "disabling it.\n");
       template->sg_tablesize = SG_NONE;
     }
   }
@@ -3914,6 +3885,7 @@ aic7xxx_register(Scsi_Host_Template *template,
   p->numscb = 0;
   p->extended = config->extended;
   p->type = config->type;
+  p->chip_type = config->chip_type;
   p->ultra_enabled = config->ultra_enabled;
   p->chan_num = config->chan_num;
   p->bus_type = config->bus_type;
@@ -3941,7 +3913,7 @@ aic7xxx_register(Scsi_Host_Template *template,
      */
     if (request_irq(config->irq, aic7xxx_isr, SA_INTERRUPT, "aic7xxx"))
     {
-      printk("aic7xxx couldn't register irq %d, ignoring\n", config->irq);
+      printk("aic7xxx: Couldn't register IRQ %d, ignoring.\n", config->irq);
       aic7xxx_boards[config->irq] = NULL;
       return (0);
     }
@@ -3972,7 +3944,7 @@ aic7xxx_register(Scsi_Host_Template *template,
    */
   outb(FASTMODE, SEQCTL(base));
 
-  if ((p->type == AIC_274x) || (p->type == AIC_284x))
+  if (p->chip_type == AIC_777x)
   {
     outb(ENABLE, BCTL(base));
   }
@@ -4044,13 +4016,13 @@ aic7xxx_register(Scsi_Host_Template *template,
   {
     if (bios_disabled)
     {
-      printk("aic7xxx : Host Adapter Bios disabled. Using default SCSI "
-             "device parameters\n");
+      printk("aic7xxx : Host adapter BIOS disabled. Using default SCSI "
+             "device parameters.\n");
       p->discenable = 0xFFFF;
     }
     else
     {
-      p->discenable = ~((inb(HA_DISC_DSB(base + 1)) << 8) |
+      p->discenable = ~((inb(HA_DISC_DSB(base) + 1) << 8) |
           inb(HA_DISC_DSB(base)));
     }
   }
@@ -4194,7 +4166,6 @@ aic7xxx_register(Scsi_Host_Template *template,
 int
 aic7xxx_detect(Scsi_Host_Template *template)
 {
-  aha_type type = AIC_NONE;
   int found = 0, slot, base;
   unsigned char irq = 0;
   int i;
@@ -4236,25 +4207,20 @@ aic7xxx_detect(Scsi_Host_Template *template)
       continue;
     }
 
-    type = aic7xxx_probe(slot, HID0(base));
-    if (type != AIC_NONE)
+    config.type = aic7xxx_probe(slot, HID0(base));
+    if (config.type != AIC_NONE)
     {
       /*
        * We found a card, allow 1 spurious interrupt.
        */
       aic7xxx_spurious_count = 1;
 
-#if 0
-      printk("aic7xxx: HCNTRL:0x%x\n", inb(HCNTRL(base)));
-      outb(inb(HCNTRL(base)) | CHIPRST, HCNTRL(base));
-#endif
-
       /*
        * We "find" a AIC-7770 if we locate the card
        * signature and we can set it up and register
        * it with the kernel without incident.
        */
-      config.type = type;
+      config.chip_type = AIC_777x;
       config.base = base;
       config.irq = irq;
       config.parity = AIC_UNKNOWN;
@@ -4273,12 +4239,31 @@ aic7xxx_detect(Scsi_Host_Template *template)
   }
 
 #ifdef CONFIG_PCI
-
   /*
    * PCI-bus probe.
    */
   if (pcibios_present())
   {
+    struct
+    {
+      unsigned short vendor_id;
+      unsigned short device_id;
+      aha_type       card_type;
+      aha_chip_type  chip_type;
+    } const aic7xxx_pci_devices[] = {
+      {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7850, AIC_7850, AIC_785x},
+      {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7870, AIC_7870, AIC_787x},
+      {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7871, AIC_7871, AIC_787x},
+      {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7872, AIC_7872, AIC_787x},
+      {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7873, AIC_7873, AIC_787x},
+      {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7874, AIC_7874, AIC_787x},
+      {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7880, AIC_7880, AIC_788x},
+      {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7881, AIC_7881, AIC_788x},
+      {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7882, AIC_7882, AIC_788x},
+      {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7883, AIC_7883, AIC_788x},
+      {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7884, AIC_7884, AIC_788x}
+    };
+
     int error;
     int done = 0;
     unsigned int io_port;
@@ -4287,146 +4272,132 @@ aic7xxx_detect(Scsi_Host_Template *template)
     unsigned char devrevid, devconfig, devstatus;
     char rev_id[] = {'B', 'C', 'D'};
 
-    while (!done)
+    for (i = 0; i < NUMBER(aic7xxx_pci_devices); i++)
     {
-      if ((!pcibios_find_device(PCI_VENDOR_ID_ADAPTEC,
-                               PCI_DEVICE_ID_ADAPTEC_7870,
-                               index, &pci_bus, &pci_device_fn)) ||
-          (!pcibios_find_device(PCI_VENDOR_ID_ADAPTEC,
-                               PCI_DEVICE_ID_ADAPTEC_7871,
-                               index, &pci_bus, &pci_device_fn)))
+      done = FALSE;
+      while (!done)
       {
-       type = AIC_7870;
-      }
-      else
-      {
-       if (!pcibios_find_device(PCI_VENDOR_ID_ADAPTEC,
-                               PCI_DEVICE_ID_ADAPTEC_7850,
-                               index, &pci_bus, &pci_device_fn))
-       {
-         type = AIC_7850;
-       }
-       else
-       {
-         if (!pcibios_find_device(PCI_VENDOR_ID_ADAPTEC,
-                                 PCI_DEVICE_ID_ADAPTEC_7872,
-                                 index, &pci_bus, &pci_device_fn))
-         {
-           type = AIC_7872;
-            config.chan_num = number_of_3940s & 0x1;
-            number_of_3940s++;
-         }
-         else
-         {
-            if ((!pcibios_find_device(PCI_VENDOR_ID_ADAPTEC,
-                                     PCI_DEVICE_ID_ADAPTEC_7881,
-                                     index, &pci_bus, &pci_device_fn)) ||
-                (!pcibios_find_device(PCI_VENDOR_ID_ADAPTEC,
-                                     PCI_DEVICE_ID_ADAPTEC_7880,
-                                     index, &pci_bus, &pci_device_fn)))
-           {
-             type = AIC_7880;
-           }
-            else
-            {
-             if (!pcibios_find_device(PCI_VENDOR_ID_ADAPTEC,
-                                     PCI_DEVICE_ID_ADAPTEC_7882,
-                                     index, &pci_bus, &pci_device_fn))
-             {
-               type = AIC_7882;
+        if (pcibios_find_device(aic7xxx_pci_devices[i].vendor_id,
+                                aic7xxx_pci_devices[i].device_id,
+                                index, &pci_bus, &pci_device_fn))
+        {
+          done = TRUE;
+        }
+        else  /* Found an Adaptec PCI device. */
+        {
+          config.type = aic7xxx_pci_devices[i].card_type;
+          config.chip_type = aic7xxx_pci_devices[i].chip_type;
+          switch (config.type)
+          {
+            case AIC_7872:  /* 3940 */
+            case AIC_7882:  /* 3940-Ultra */
+              config.chan_num = number_of_39xxs & 0x1;  /* Has 2 controllers */
+              number_of_39xxs++;
+              if (number_of_39xxs == 2)
+              {
+                number_of_39xxs = 0;  /* To be consistent with 3985. */
               }
-              else
+              break;
+
+            case AIC_7873:  /* 3985 */
+            case AIC_7883:  /* 3985-Ultra */
+              config.chan_num = number_of_39xxs & 0x3;  /* Has 3 controllers */
+              number_of_39xxs++;
+              if (number_of_39xxs == 3)
               {
-               type = AIC_NONE;
-               done = 1;
+                number_of_39xxs = 0;
               }
-            }
+              break;
+
+            default:
+              break;
           }
-       }
-      }
 
-      if (!done)
-      {
-       /*
-        * Read esundry information from PCI BIOS.
-        */
-       error = pcibios_read_config_dword(pci_bus, pci_device_fn,
-                                         PCI_BASE_ADDRESS_0, &io_port);
-       if (error)
-       {
-         panic("aic7xxx_detect: error 0x%x reading i/o port.\n", error);
-       }
+         /*
+          * Read esundry information from PCI BIOS.
+          */
+         error = pcibios_read_config_dword(pci_bus, pci_device_fn,
+                                           PCI_BASE_ADDRESS_0, &io_port);
+         if (error)
+         {
+           panic("aic7xxx: (aic7xxx_detect) Error %d reading I/O port.\n",
+                  error);
+         }
 
-       error = pcibios_read_config_byte(pci_bus, pci_device_fn,
-                                        PCI_INTERRUPT_LINE, &irq);
-       if (error)
-       {
-         panic("aic7xxx_detect: error %d reading irq.\n", error);
-       }
+         error = pcibios_read_config_byte(pci_bus, pci_device_fn,
+                                          PCI_INTERRUPT_LINE, &irq);
+         if (error)
+         {
+           panic("aic7xxx: (aic7xxx_detect) Error %d reading IRQ.\n",
+                   error);
+         }
 
-       error = pcibios_read_config_byte(pci_bus, pci_device_fn,
-                                        DEVREVID, &devrevid);
-       if (error)
-       {
-         panic("aic7xxx_detect: error %d reading device revision id.\n", error);
-       }
+         error = pcibios_read_config_byte(pci_bus, pci_device_fn,
+                                          DEVREVID, &devrevid);
+         if (error)
+         {
+           panic("aic7xxx: (aic7xxx_detect) Error %d reading device "
+                  "revision ID.\n", error);
+         }
 
-       if (devrevid < 3)
-       {
-         printk("aic7xxx_detect: AIC-7870 Rev %c\n", rev_id[devrevid]);
-       }
+         if (devrevid < 3)
+         {
+           printk("aic7xxx: %s Rev %c.\n", board_names[config.type],
+                   rev_id[devrevid]);
+         }
 
-       error = pcibios_read_config_byte(pci_bus, pci_device_fn,
-                                        DEVCONFIG, &devconfig);
-       if (error)
-       {
-         panic("aic7xxx_detect: error %d reading device configuration.\n", error);
-       }
+         error = pcibios_read_config_byte(pci_bus, pci_device_fn,
+                                          DEVCONFIG, &devconfig);
+         if (error)
+         {
+           panic("aic7xxx: (aic7xxx_detect) Error %d reading device "
+                  "configuration.\n", error);
+         }
 
-       error = pcibios_read_config_byte(pci_bus, pci_device_fn,
-                                        DEVSTATUS, &devstatus);
-       if (error)
-       {
-         panic("aic7xxx_detect: error %d reading device status.\n", error);
-       }
+         error = pcibios_read_config_byte(pci_bus, pci_device_fn,
+                                          DEVSTATUS, &devstatus);
+         if (error)
+         {
+           panic("aic7xxx: (aic7xxx_detect) Error %d reading device status.\n",
+                  error);
+         }
 
-       printk("aic7xxx_detect: devconfig 0x%x, devstatus 0x%x\n",
-              devconfig, devstatus);
+         printk("aic7xxx: devconfig(0x%x) devstatus(0x%x).\n",
+                devconfig, devstatus);
 
-       /*
-        * Make the base I/O register look like EISA and VL-bus.
-        */
-       base = io_port - 0xC01;
+         /*
+          * Make the base I/O register look like EISA and VL-bus.
+          */
+         base = io_port - 0xC01;
 
-       /*
-        * I don't think we need to bother with allowing
-        * spurious interrupts for the 787x/7850, but what
-        * the hey.
-        */
-       aic7xxx_spurious_count = 1;
-
-        config.type = type;
-        config.base = base;
-        config.irq = irq;
-        config.parity = AIC_UNKNOWN;
-        config.low_term = AIC_UNKNOWN;
-        config.high_term = AIC_UNKNOWN;
-        config.busrtime = 0;
-        config.walk_scbs = FALSE;
-        config.ultra_enabled = FALSE;
-       if ((devstatus & RAMPSM) || (devconfig & SCBRAMSEL) ||
-            (type == AIC_7872))
-       {
-          config.walk_scbs = TRUE;
-       }
-       found += aic7xxx_register(template, &config);
+         /*
+          * I don't think we need to bother with allowing
+          * spurious interrupts for the 787x/7850, but what
+          * the hey.
+          */
+         aic7xxx_spurious_count = 1;
+
+          config.base = base;
+          config.irq = irq;
+          config.parity = AIC_UNKNOWN;
+          config.low_term = AIC_UNKNOWN;
+          config.high_term = AIC_UNKNOWN;
+          config.busrtime = 0;
+          config.walk_scbs = FALSE;
+          config.ultra_enabled = FALSE;
+         if ((devstatus & RAMPSM) || (devconfig & SCBRAMSEL))
+         {
+            config.walk_scbs = TRUE;
+         }
+         found += aic7xxx_register(template, &config);
 
-       /*
-        * Disable spurious interrupts.
-        */
-       aic7xxx_spurious_count = 0;
+         /*
+          * Disable spurious interrupts.
+          */
+         aic7xxx_spurious_count = 0;
 
-       index++;
+         index++;
+        }  /* Found an Adaptec PCI device. */
       }
     }
   }
@@ -4462,8 +4433,8 @@ aic7xxx_buildscb(struct aic7xxx_host *p,
   {
     if (cmd->device->tagged_queue == 0)
     {
-      printk("aic7xxx_buildscb: Enabling tagged queuing for target %d, "
-            "channel %d\n", cmd->target, cmd->channel);
+      printk("aic7xxx: Enabling tagged queuing for target %d, "
+            "channel %d.\n", cmd->target, cmd->channel);
       cmd->device->tagged_queue = 1;
       cmd->device->current_tag = 1;  /* enable tagging */
     }
@@ -4482,7 +4453,7 @@ aic7xxx_buildscb(struct aic7xxx_host *p,
     p->wdtr_pending |= mask;
     scb->control |= SCB_NEEDWDTR;
 #if 0
-    printk("Sending WDTR request to target %d.\n", cmd->target);
+    printk("aic7xxx: Sending WDTR request to target %d.\n", cmd->target);
 #endif
   }
   else
@@ -4492,13 +4463,14 @@ aic7xxx_buildscb(struct aic7xxx_host *p,
       p->sdtr_pending |= mask;
       scb->control |= SCB_NEEDSDTR;
 #if 0
-      printk("Sending SDTR request to target %d.\n", cmd->target);
+      printk("aic7xxx: Sending SDTR request to target %d.\n", cmd->target);
 #endif
     }
   }
 
 #if 0
-  printk("aic7xxx_queue: target %d, cmd 0x%x (size %u), wdtr 0x%x, mask 0x%x\n",
+  printk("aic7xxx: (build_scb) Target %d, cmd(0x%x) size(%u) wdtr(0x%x) "
+         "mask(0x%x).\n",
         cmd->target, cmd->cmnd[0], cmd->cmd_len, p->needwdtr, mask);
 #endif
   scb->target_channel_lun = ((cmd->target << 4) & 0xF0) |
@@ -4522,7 +4494,7 @@ aic7xxx_buildscb(struct aic7xxx_host *p,
   if (cmd->use_sg)
   {
 #if 0
-    debug("aic7xxx_buildscb: SG used, %d segments, length %u\n",
+    debug("aic7xxx: (build_scb) SG used, %d segments, length(%u).\n",
            cmd->use_sg, length);
 #endif
     scb->SG_segment_count = cmd->use_sg;
@@ -4537,7 +4509,7 @@ aic7xxx_buildscb(struct aic7xxx_host *p,
   else
   {
 #if 0
-  debug("aic7xxx_buildscb: Creating scatterlist, addr=0x%lx, length=%d.\n",
+  debug("aic7xxx: (build_scb) Creating scatterlist, addr(0x%lx) length(%d).\n",
        (unsigned long) cmd->request_buffer, cmd->request_bufflen);
 #endif
     if (cmd->request_bufflen == 0)
@@ -4602,7 +4574,7 @@ aic7xxx_queue(Scsi_Cmnd *cmd, void (*fn)(Scsi_Cmnd *))
   }
 
 #if 0
-  debug("aic7xxx_queue: cmd 0x%x (size %u), target %d, channel %d, lun %d\n",
+  debug("aic7xxx_queue: cmd(0x%x) size(%u), target %d, channel %d, lun %d.\n",
        cmd->cmnd[0], cmd->cmd_len, cmd->target, cmd->channel,
        cmd->lun & 0x07);
 #endif
@@ -4645,7 +4617,7 @@ aic7xxx_queue(Scsi_Cmnd *cmd, void (*fn)(Scsi_Cmnd *))
   {
     if (p->numscb >= p->maxscb)
     {
-      panic("aic7xxx_queue: couldn't find a free scb\n");
+      panic("aic7xxx: (aic7xxx_queue) Couldn't find a free SCB.\n");
     }
     else
     {
@@ -4688,9 +4660,10 @@ aic7xxx_queue(Scsi_Cmnd *cmd, void (*fn)(Scsi_Cmnd *))
 #if 0
   if (scb != &p->scb_array[scb->position])
   {
-    printk("aic7xxx_queue: address of scb by position does not match scb address\n");
+    printk("aic7xxx: (queue) Address of SCB by position does not match SCB "
+           "address.\n");
   }
-  printk("aic7xxx_queue: SCB pos=%d, cmdptr=0x%x, state=%d, freescb=0x%x\n",
+  printk("aic7xxx: (queue) SCB pos(%d) cmdptr(0x%x) state(%d) freescb(0x%x)\n",
         scb->position, (unsigned int) scb->cmd,
         scb->state, (unsigned int) p->free_scb);
 #endif
@@ -4719,13 +4692,12 @@ aic7xxx_queue(Scsi_Cmnd *cmd, void (*fn)(Scsi_Cmnd *))
   cmd->scsi_done = fn;
   aic7xxx_error(cmd) = DID_OK;
   aic7xxx_status(cmd) = 0;
-  scb->timer_status = 0x0;
   cmd->result = 0;
   memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
 
   UNPAUSE_SEQUENCER(p);
 #if 0
-  printk("aic7xxx_queue: After - cmd = 0x%lx, scb->cmd = 0x%lx, pos = %d\n",
+  printk("aic7xxx: (queue) After - cmd(0x%lx) scb->cmd(0x%lx) pos(%d).\n",
          (long) cmd, (long) scb->cmd, scb->position);
 #endif;
   restore_flags(flags);
@@ -4740,13 +4712,16 @@ aic7xxx_queue(Scsi_Cmnd *cmd, void (*fn)(Scsi_Cmnd *))
  *   Abort an scb.  If the scb has not previously been aborted, then
  *   we attempt to send a BUS_DEVICE_RESET message to the target.  If
  *   the scb has previously been unsuccessfully aborted, then we will
- *   reset the channel and have all devices renegotiate.
+ *   reset the channel and have all devices renegotiate.  Returns an
+ *   enumerated type that indicates the status of the operation.
  *-F*************************************************************************/
-static void
-aic7xxx_abort_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
+static aha_abort_reset_type
+aic7xxx_abort_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb,
+                  unsigned char errcode)
 {
   int base = p->base;
   int found = 0;
+  aha_abort_reset_type scb_status = ABORT_RESET_SUCCESS;
   char channel = scb->target_channel_lun & SELBUSB ? 'B': 'A';
 
   /*
@@ -4755,6 +4730,10 @@ aic7xxx_abort_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
    */
   PAUSE_SEQUENCER(p);
 
+#ifdef AIC7XXX_DEBUG_ABORT 
+  printk ("aic7xxx: (abort_scb) scb %d, scb_aborted 0x%x\n",
+          scb->position, (scb->state & SCB_ABORTED));
+#endif
   /*
    * First, determine if we want to do a bus reset or simply a bus device
    * reset.  If this is the first time that a transaction has timed out,
@@ -4796,6 +4775,9 @@ aic7xxx_abort_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
     outb(scb->position, SCBPTR(base));
     if (inb(SCBARRAY(base)) & SCB_DIS)
     {
+#ifdef AIC7XXX_DEBUG_ABORT
+  printk ("aic7xxx: (abort_scb) scb %d is disconnected.\n", scb->position);
+#endif
       scb->state |= (SCB_DEVICE_RESET | SCB_ABORTED);
       scb->SG_segment_count = 0;
       memset(scb->SG_list_pointer, 0, sizeof(scb->SG_list_pointer));
@@ -4809,8 +4791,10 @@ aic7xxx_abort_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
                    :"S" (scb), "c" (SCB_DOWNLOAD_SIZE), "d" (SCBARRAY(base))
                    :"si", "cx", "dx");
       outb(0, SCBCNT(base));
+      aic7xxx_error(scb->cmd) = errcode;
+      scb_status = ABORT_RESET_PENDING;
       aic7xxx_add_waiting_scb(base, scb, LIST_SECOND);
-      aic7xxx_scb_tsleep(p, scb, 2 * HZ);  /* unpauses the sequencer */
+      UNPAUSE_SEQUENCER(p);
     }
     else
     {
@@ -4822,6 +4806,10 @@ aic7xxx_abort_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
         unsigned char flags = inb(HA_FLAGS(base));
         if (flags & ACTIVE_MSG)
         {
+#ifdef AIC7XXX_DEBUG_ABORT
+          printk ("aic7xxx: (abort_scb) scb is active, needs DMA, "
+                  "ha_flags active.\n");
+#endif
           /*
            * If we're in a message phase, tacking on another message
            * may confuse the target totally. The bus is probably wedged,
@@ -4832,6 +4820,10 @@ aic7xxx_abort_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
         }
         else
         {
+#ifdef AIC7XXX_DEBUG_ABORT
+          printk ("aic7xxx: (abort_scb) scb is active, needs DMA, "
+                  "ha_flags inactive.\n");
+#endif
           /*
            * Load the message buffer and assert attention.
            */
@@ -4849,11 +4841,16 @@ aic7xxx_abort_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
              */
             ;
           }
-          aic7xxx_scb_tsleep(p, active_scbp, 2 * HZ);
+          aic7xxx_error(scb->cmd) = errcode;
+          scb_status = ABORT_RESET_PENDING;
+          UNPAUSE_SEQUENCER(p);
         }
       }
       else
       {
+#ifdef AIC7XXX_DEBUG_ABORT
+          printk ("aic7xxx: (abort_scb) no active command.\n");
+#endif
         /*
          * No active command to single out, so reset
          * the bus for the timed out target.
@@ -4862,21 +4859,24 @@ aic7xxx_abort_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
       }
     }
   }
+  return (scb_status);
 }
 
 /*+F*************************************************************************
  * Function:
- *   aic7xxx_abort
+ *   aic7xxx_abort_reset
  *
  * Description:
- *   Abort the current SCSI command(s).
+ *   Abort or reset the current SCSI command(s).  Returns an enumerated
+ *   type that indicates the status of the operation.
  *-F*************************************************************************/
-int
-aic7xxx_abort(Scsi_Cmnd *cmd)
+static aha_abort_reset_type
+aic7xxx_abort_reset(Scsi_Cmnd *cmd, unsigned char errcode)
 {
   struct aic7xxx_scb  *scb;
   struct aic7xxx_host *p;
   long flags;
+  aha_abort_reset_type scb_status = ABORT_RESET_SUCCESS;
 
   p = (struct aic7xxx_host *) cmd->host->hostdata;
   scb = &(p->scb_array[aic7xxx_position(cmd)]);
@@ -4884,6 +4884,10 @@ aic7xxx_abort(Scsi_Cmnd *cmd)
   save_flags(flags);
   cli();
 
+#ifdef AIC7XXX_DEBUG_ABORT
+  printk ("aic7xxx: (abort_reset) scb state 0x%x\n", scb->state);
+#endif
+
   if (scb->state & SCB_ACTIVE)
   {
     if (scb->state & SCB_IMMED)
@@ -4892,6 +4896,7 @@ aic7xxx_abort(Scsi_Cmnd *cmd)
        * Don't know how set the number of retries to 0.
        */
       /* cmd->retries = 0; */
+      aic7xxx_error(cmd) = errcode;
       aic7xxx_done (p, scb);
     }
     else
@@ -4899,11 +4904,65 @@ aic7xxx_abort(Scsi_Cmnd *cmd)
       /*
        * Abort the operation.
        */
-      aic7xxx_abort_scb(p, scb);
+      scb_status = aic7xxx_abort_scb(p, scb, errcode);
     }
   }
+  else
+  {
+    /*
+     * The scb is not active and must have completed after the timeout
+     * check in scsi.c and before we check the scb state above.  For
+     * this case we return SCSI_ABORT_NOT_RUNNING (if abort was called)
+     * or SCSI_RESET_SUCCESS (if reset was called).
+     */
+#ifdef AIC7XXX_DEBUG_ABORT
+    printk ("aic7xxx: (abort_reset) called with no active scb, errcode 0x%x\n",
+            errcode);
+#endif
+    scb_status = ABORT_RESET_INACTIVE;
+    /*
+     * According to the comments in scsi.h and Michael Neuffer, if we do not
+     * have an active command for abort or reset, we should not call the
+     * command done function.  Unfortunately, this hangs the system for me
+     * unless we *do* call the done function.
+     *
+     * XXX - Revisit this sometime!
+     */
+    cmd->result = errcode << 16;
+    cmd->scsi_done(cmd);
+  }
   restore_flags(flags);
-  return (0);
+  return (scb_status);
+}
+
+
+/*+F*************************************************************************
+ * Function:
+ *   aic7xxx_abort
+ *
+ * Description:
+ *   Abort the current SCSI command(s).
+ *-F*************************************************************************/
+int
+aic7xxx_abort(Scsi_Cmnd *cmd)
+{
+#ifdef AIC7XXX_DEBUG_ABORT
+  printk ("aic7xxx: (abort) target/channel %d/%d\n", cmd->target, cmd->channel);
+#endif
+
+  switch (aic7xxx_abort_reset(cmd, DID_ABORT))
+  {
+    case ABORT_RESET_INACTIVE:
+      return (SCSI_ABORT_NOT_RUNNING);
+      break;
+    case ABORT_RESET_PENDING:
+      return (SCSI_ABORT_PENDING);
+      break;
+    case ABORT_RESET_SUCCESS:
+    default:
+      return (SCSI_ABORT_SUCCESS);
+      break;
+  }
 }
 
 /*+F*************************************************************************
@@ -4919,7 +4978,21 @@ aic7xxx_abort(Scsi_Cmnd *cmd)
 int
 aic7xxx_reset(Scsi_Cmnd *cmd)
 {
-  return (aic7xxx_abort(cmd));
+#ifdef AIC7XXX_DEBUG_ABORT
+  printk ("aic7xxx: (reset) target/channel %d/%d\n", cmd->target, cmd->channel);
+#endif
+
+  switch (aic7xxx_abort_reset(cmd, DID_RESET))
+  {
+    case ABORT_RESET_PENDING:
+      return (SCSI_RESET_PENDING);
+      break;
+    case ABORT_RESET_INACTIVE:
+    case ABORT_RESET_SUCCESS:
+    default:
+      return (SCSI_RESET_SUCCESS);
+      break;
+  }
 }
 
 /*+F*************************************************************************
index 823e40181ac9c3ee0acd622a63ce0bb35918474e..2476659da84c8dcf452c75ed0659b8992913cb96 100644 (file)
@@ -24,7 +24,7 @@
  *
  *  Dean W. Gehnert, deang@ims.com, 08/30/95
  *
- *  $Id: aic7xxx_proc.c,v 2.2 1995/09/12 05:24:37 deang Exp $
+ *  $Id: aic7xxx_proc.c,v 2.5 1995/12/16 23:11:55 deang Exp $
  *-M*************************************************************************/
 
 #define BLS buffer + len + size
@@ -82,7 +82,6 @@ aic7xxx_proc_info(char *buffer, char **start, off_t offset, int length,
   int   len = 0;
   off_t begin = 0;
   off_t pos = 0;
-  static char *board_name[] = {"None", "274x", "284x", "7870", "7850", "7872"};
   static char *bus_name[] = {"Single", "Twin", "Wide"};
 
   HBAptr = NULL;
@@ -157,23 +156,18 @@ aic7xxx_proc_info(char *buffer, char **start, off_t offset, int length,
   size += sprintf(BLS, "  AIC7XXX_PROC_STATS     : Enabled\n");
 #else
   size += sprintf(BLS, "  AIC7XXX_PROC_STATS     : Disabled\n");
-#endif
-#ifdef AIC7XXX_POLL
-  size += sprintf(BLS, "  AIC7XXX_POLL           : Enabled\n");
-#else
-  size += sprintf(BLS, "  AIC7XXX_POLL           : Disabled\n");
 #endif
   len += size; pos = begin + len; size = 0;
 
   size += sprintf(BLS, "\n");
   size += sprintf(BLS, "Adapter Configuration:\n");
-  size += sprintf(BLS, "          SCSI Adapter: %s\n", board_name[p->type]);
+  size += sprintf(BLS, "          SCSI Adapter: %s\n", board_names[p->type]);
   size += sprintf(BLS, "              Host Bus: %s\n", bus_name[p->bus_type]);
   size += sprintf(BLS, "               Base IO: %#.4x\n", p->base);
   size += sprintf(BLS, "                   IRQ: %d\n", HBAptr->irq);
   size += sprintf(BLS, "                   SCB: %d (%d)\n", p->numscb, p->maxscb);
   size += sprintf(BLS, "            Interrupts: %d", p->isr_count);
-  if ((p->type == AIC_274x) || (p->type == AIC_284x))
+  if (p->chip_type == AIC_777x)
   {
     size += sprintf(BLS, " %s\n",
         (p->pause & IRQMS) ? "(Level Sensitive)" : "(Edge Triggered)");
@@ -190,6 +184,8 @@ aic7xxx_proc_info(char *buffer, char **start, off_t offset, int length,
       p->extended ? "En" : "Dis");
   size += sprintf(BLS, "        SCSI Bus Reset: %sabled\n",
       aic7xxx_no_reset ? "Dis" : "En");
+  size += sprintf(BLS, "            Ultra SCSI: %sabled\n",
+      p->ultra_enabled ? "En" : "Dis");
   len += size; pos = begin + len; size = 0;
 
 #ifdef AIC7XXX_PROC_STATS
index 8406dabbd979ef11aaa05e143403ba69bfe8b4c7..01df98c473022a1bc4324945bd27e2c9519f3788 100644 (file)
@@ -662,7 +662,7 @@ repeat0:
                                        continue;
                                };
                                
-                               if (bh->b_count || bh->b_size != size)
+                               if (bh->b_count || buffer_protected(bh) || bh->b_size != size)
                                         continue;
                                
                                /* Buffers are written in the order they are
@@ -840,10 +840,6 @@ void __bforget(struct buffer_head * buf)
                printk("Aieee... bforget(): shared buffer\n");
                return;
        }
-       if (buffer_protected(buf)) {
-               printk("Aieee... bforget(): protected buffer\n");
-               return;
-       }
        mark_buffer_clean(buf);
        buf->b_count = 0;
        remove_from_queues(buf);
index 008e9dfb47b1cd5f473a31207d94640f3951ad2a..c09766c31069059ea27a846cc3660be2db2a6435 100644 (file)
@@ -33,7 +33,6 @@
 #include <linux/mount.h>
 
 #include <asm/segment.h>
-#include <sys/sysmacros.h>
 
 #define __DQUOT_VERSION__      "dquot_5.6.0"
 
index 977dcc5fd89180096e40035b4fb7f42b8d14120d..fcfa38731f6aeded7da4d8969d783b48245ffc94 100644 (file)
 #include <linux/nfs.h>
 #include <linux/nfs_fs.h>
 #include <linux/nfs_mount.h>
-#include <netinet/in.h>
+#include <linux/in.h>
 #include <net/route.h>
 
 
 /* Range of privileged ports */
-#define STARTPORT 600
-#define ENDPORT (IPPORT_RESERVED - 1)
-#define NPORTS (ENDPORT - STARTPORT + 1)
+#define STARTPORT      600
+#define ENDPORT                1023
+#define NPORTS         (ENDPORT - STARTPORT + 1)
 
 
 
index 39809b4d82bda3029db65a41c90fe03909dbc7f7..0933ae1c52c8c2027566d6079b1ef7831f431069 100644 (file)
 #define PCI_DEVICE_ID_ADAPTEC_7871     0x7178
 #define PCI_DEVICE_ID_ADAPTEC_7872     0x7278
 #define PCI_DEVICE_ID_ADAPTEC_7873     0x7378
+#define PCI_DEVICE_ID_ADAPTEC_7874     0x7478
 #define PCI_DEVICE_ID_ADAPTEC_7880     0x8078
 #define PCI_DEVICE_ID_ADAPTEC_7881     0x8178
 #define PCI_DEVICE_ID_ADAPTEC_7882     0x8278
+#define PCI_DEVICE_ID_ADAPTEC_7883     0x8378
+#define PCI_DEVICE_ID_ADAPTEC_7884     0x8478
 
 #define PCI_VENDOR_ID_ATRONICS         0x907f
 #define PCI_DEVICE_ID_ATRONICS_2015    0x2015
index 100424a6b084515568593e0a00d025abfaec0b29..b220fa62bdc67912d29815ab7845dc5e2f1df863 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: scc.h,v 1.11 1995/08/24 21:06:24 jreuter Exp jreuter $ */
+/* $Id: scc.h,v 1.15 1995/11/16 20:19:26 jreuter Exp jreuter $ */
 
 #ifndef        _SCC_H
 #define        _SCC_H
 #define DRSI           0x08    /* hardware type for DRSI PC*Packet card */
 #define BAYCOM         0x10    /* hardware type for BayCom (U)SCC */
 
-/* Constants */
-
-#define MAXSCC         4       /* number of max. supported chips */
-#define MAX_IBUFS      200     /* change this if you run out of memory */
-#define BUFSIZE                128     /* must not exceed 4096 */
-#define TPS            25      /* scc_timer():  Ticks Per Second */
-
-#define SCC_TIMER      3
+/* Paranoia check... */
 
 #define SCC_PARANOIA_CHECK     /* tell the user if something is going wrong */
 
 /* ioctl() commands */
 
-/* !!! NEW VALUES !!! */
+#define TIOCSCCCFG     0x2200          /* set hardware parameters */
+#define TIOCSCCINI     0x2201          /* init driver */
+#define TIOCCHANINI    0x2202          /* init channel */
 
-#define TIOCSCCINI     0x5470          /* init driver */
-#define TIOCCHANINI    0x5471          /* init channel */
+#define TIOCCHANMEM    0x2210          /* adjust buffer pools */
 
-#define TIOCGKISS      0x5472          /* get kiss parameter */
-#define TIOCSKISS      0x5473          /* set kiss parameter */
+#define TIOCGKISS      0x2282          /* get kiss parameter */
+#define TIOCSKISS      0x2283          /* set kiss parameter */
 
-#define TIOCSCCSTAT    0x5474          /* get scc status */
+#define TIOCSCCSTAT    0x2284          /* get scc status */
 
 
 /* magic number */
 #define BT_RECEIVE  1          /* buffer allocated by receive */
 #define BT_TRANSMIT 2          /* buffer allocated by transmit */
 
-#define QUEUE_THRES MAX_IBUFS/20       /* maximum amount of packets being queued */
-#define QUEUE_HYST  3                  /* leave QUEUE_HYST packets untouched */
-
 #define NULLBUF  (struct mbuf *)0
 #define NULLBUFP (struct mbuf **)0
 
@@ -134,30 +125,15 @@ typedef unsigned short ioaddr;  /* old def */
 
 /* Basic message buffer structure */
 
-/* looks familiar? Hmm, yes... */
-
 struct mbuf {
-       struct mbuf *next;      /* Links mbufs belonging to single packets */
-       struct mbuf *anext;     /* Links packets on queues */
-       
-       char type;              /* who allocated this buffer? */
-       int  time_out;          /* unimplemented yet */
+       struct mbuf *next;      /* Link to next buffer */
+       struct mbuf *prev;      /* Link to previous buffer */
        
-       int size;               /* Size of associated data buffer */
-       int refcnt;             /* Reference count */
-       struct mbuf *dup;       /* Pointer to duplicated mbuf */
-       char data[BUFSIZE];     /* Active working pointers */
-       int cnt;
-       int in_use;
+       int cnt;                /* Number of bytes stored in buffer */
+       unsigned char *rw_ptr;  /* read-write pointer */
+       unsigned char data[0];  /* anchor for allocated buffer */
 };
-
-
-struct sccbuf {
-       struct mbuf *bp;
-        int inuse;
-};
-                                                                                                
-               
+       
 /* SCC channel control structure for KISS */
 
 struct scc_kiss {
@@ -203,9 +179,9 @@ struct scc_stat {
        int tx_queued;          /* tx frames enqueued */
        int rx_queued;          /* rx frames enqueued */
        
-       unsigned int rx_alloc;  /* allocated rx_buffers */
-       unsigned int tx_alloc;  /* allocated tx_buffers */
-       unsigned int used_buf;  /* used buffers (should be rx_alloc+tx_alloc) */
+       unsigned int rxbuffers; /* allocated rx_buffers */
+       unsigned int txbuffers; /* allocated tx_buffers */
+       unsigned int bufsize;   /* used buffersize */
 };
 
 
@@ -220,6 +196,33 @@ struct ioctl_command {
        unsigned param;         /* KISS-Param */
 };
 
+/* currently unused */
+
+struct scc_hw_config {
+       io_port data_a;         /* data port channel A */
+       io_port ctrl_a;         /* control port channel A */
+       io_port data_b;         /* data port channel B */
+       io_port ctrl_b;         /* control port channel B */
+       io_port vector_latch;   /* INTACK-Latch (#) */
+       io_port special;        /* special function port */
+
+       int     irq;            /* irq */
+       long    clock;          /* clock */
+       char    option;         /* command for function port */
+       
+       char brand;             /* hardware type */
+       char escc;              /* use ext. features of a 8580/85180/85280 */
+};
+
+struct scc_mem_config {
+       unsigned int rxbuffers;
+       unsigned int txbuffers;
+       unsigned int bufsize;
+};
+
+/* (#) only one INTACK latch allowed. */
+       
+
 /* SCC channel structure */
 
 struct scc_channel {
@@ -227,10 +230,17 @@ struct scc_channel {
        
        int init;               /* channel exists? */
        struct tty_struct *tty; /* link to tty control structure */
-       unsigned char tty_opened;
+       char tty_opened;        /* No. of open() calls... */
+       char throttled;         /* driver is throttled  */
+               
+       char brand;             /* manufacturer of the board */
+       long clock;             /* used clock */
        
        io_port ctrl;           /* I/O address of CONTROL register */
        io_port data;           /* I/O address of DATA register */
+       io_port special;        /* I/O address of special function port */
+       
+       char option;
        char enhanced;          /* Enhanced SCC support */
 
        unsigned char wreg[16]; /* Copy of last written value in WRx */
@@ -239,20 +249,25 @@ struct scc_channel {
         struct scc_kiss kiss;  /* control structure for KISS params */
         struct scc_stat stat;  /* statistical information */
         struct scc_modem modem; /* modem information */
-
-       struct mbuf *rbp;       /* rx: Head of mbuf chain being filled */
-       struct mbuf *rbp1;      /* rx: Pointer to mbuf currently being written */
-       struct mbuf *rcvq;      /* Pointer to mbuf packets currently received */
-       
-       struct mbuf *sndq;      /* tx: Packets awaiting transmission */
-       struct mbuf *tbp;       /* tx: Transmit mbuf being sent */
-
-       struct mbuf *sndq1;     /* Pointer to mbuf currently under construction */
-       struct mbuf *sndq2;     /* Pointer to mbuf currently under construction */
-
+        
+        struct mbuf *rx_buffer_pool; /* free buffers for rx/tx frames are */
+        struct mbuf *tx_buffer_pool; /* linked in these ring chains */
+        
+        struct mbuf *rx_queue; /* chain of received frames */
+        struct mbuf *tx_queue; /* chain of frames due to transmit */
+        struct mbuf *rx_bp;    /* pointer to frame currently received */
+        struct mbuf *tx_bp;    /* pointer to frame currently transmitted */
+        
+        struct mbuf *kiss_decode_bp; /* frame we are receiving from tty */
+        struct mbuf *kiss_encode_bp; /* frame we are sending to tty */
        
        /* Timer */
+       
+       struct timer_list tx_t; /* tx timer for this channel */
+       struct timer_list rx_t; /* rx timer */
 
+       /* rx timer counters */
+       
        unsigned int t_dwait;   /* wait time (DWAIT) */
        unsigned int t_slot;    /* channel sample frequency */
        unsigned int t_txdel;   /* TX delay */
@@ -263,14 +278,6 @@ struct scc_channel {
        unsigned int t_mbusy;   /* time until defer if channel busy */          
 };
 
-/* some variables for scc_rx_timer() bound together in a struct */
-
-struct rx_timer_CB {
-                       char               lock;
-                       unsigned long      expires;
-                       struct scc_channel *scc;
-};
-
 
 /* 8530 Serial Communications Controller Register definitions */
 #define        FLAG    0x7e
@@ -557,4 +564,8 @@ struct rx_timer_CB {
 
 /* global functions */
 
+#ifdef PREV_LINUX_1_3_33
+extern long scc_init(long kmem_start);
+#else
 extern int scc_init(void);
+#endif
index 5557b811d3cf280f5d6446295c8be87dfaefa0d9..f1c22e9aaed2bd51830fd7f9eab7c15bd6abc07f 100644 (file)
@@ -36,6 +36,7 @@
 #include <linux/sem.h>
 #include <linux/minix_fs.h>
 #include <linux/ext2_fs.h>
+#include <linux/random.h>
 
 #ifdef __alpha__
 # include <asm/io.h>
@@ -241,6 +242,7 @@ struct symbol_table symbol_table = {
        X(dcache_lookup),
        X(dcache_add),
        X(aout_core_dump),
+       X(add_blkdev_randomness),
 
        /* device registration */
        X(register_chrdev),
@@ -388,6 +390,7 @@ struct symbol_table symbol_table = {
        X(rarp_ioctl_hook),
        X(init_etherdev),
        X(ip_rt_route),
+       X(ip_rt_put),
        X(arp_send),
 #ifdef CONFIG_IP_FORWARD
        X(ip_forward),
@@ -549,6 +552,8 @@ struct symbol_table symbol_table = {
        X(generate_cluster),
        X(proc_scsi),
        X(proc_scsi_inode_operations),
+       X(proc_net_inode_operations),
+       X(proc_net),
 #endif
        /********************************************************
         * Do not add anything below this line,
index 59470252bf746fd897e993b54048ebf258ff02b3..6ed19518739fbcaec5a8c2c6ebb198c6dabcc7a7 100644 (file)
@@ -309,9 +309,10 @@ static inline void forget_pte(pte_t page)
        if (pte_none(page))
                return;
        if (pte_present(page)) {
-               free_page(pte_page(page));
-               if (mem_map[MAP_NR(pte_page(page))].reserved)
+               unsigned long addr = pte_page(page);
+               if (addr >= high_memory || mem_map[MAP_NR(addr)].reserved)
                        return;
+               free_page(addr);
                if (current->mm->rss <= 0)
                        return;
                current->mm->rss--;