root/drivers/scsi/53c7,8xx.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. internal_setup
  2. setup_wrapper
  3. normal_init
  4. pci_init
  5. NCR53c7xx_detect
  6. NCR53c8x0_init_fixup
  7. NCR53c8xx_run_tests
  8. NCR53c8xx_dsa_fixup
  9. abnormal_finished
  10. intr_break
  11. asynchronous
  12. synchronous
  13. NCR53c8x0_dstat_sir_intr
  14. debugger_fn_bc
  15. debugger_fn_bl
  16. debugger_fn_bs
  17. debugger_user_write
  18. debugger_user_read
  19. debugger_kernel_write
  20. NCR53c8x0_soft_reset
  21. create_cmd
  22. NCR53c7xx_queue_command
  23. fix_pointers
  24. intr_scsi
  25. NCR53c7x0_intr
  26. abort_connected
  27. intr_phase_mismatch
  28. intr_dma
  29. print_insn
  30. NCR53c7xx_abort
  31. NCR53c7xx_reset
  32. NCR53c7xx_info
  33. print_dsa

   1 /* 
   2  * Set these options for all host adapters.
   3  *      - Memory mapped IO does not work.
   4  *      - Test 1 does a bus mastering test, which will help
   5  *        weed out brain damaged main boards.
   6  */
   7 
   8 #define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1)
   9 
  10 /*
  11  * Define SCSI_MALLOC to use scsi_malloc instead of kmalloc.  Other than
  12  * preventing deadlock, I'm not sure why we'd want to do this.
  13  */
  14 
  15 #define SCSI_MALLOC
  16 
  17 /*
  18  * Sponsored by 
  19  *      iX Multiuser Multitasking Magazine
  20  *      Hannover, Germany
  21  *      hm@ix.de
  22  *
  23  * Copyright 1993, 1994 Drew Eckhardt
  24  *      Visionary Computing 
  25  *      (Unix and Linux consulting and custom programming)
  26  *      drew@Colorado.EDU
  27  *      +1 (303) 786-7975
  28  *
  29  * TolerANT and SCSI SCRIPTS are registered trademarks of NCR Corporation.
  30  * 
  31  * PRE-ALPHA
  32  *
  33  * For more information, please consult 
  34  *
  35  *
  36  * NCR 53C700/53C700-66
  37  * SCSI I/O Processor
  38  * Data Manual
  39  *
  40  * NCR53C710 
  41  * SCSI I/O Processor
  42  * Programmer's Guide
  43  *
  44  * NCR 53C810
  45  * PCI-SCSI I/O Processor
  46  * Data Manual
  47  *
  48  * NCR 53C810/53C820
  49  * PCI-SCSI I/O Processor Design In Guide
  50  *
  51  * NCR Microelectronics
  52  * 1635 Aeroplaza Drive
  53  * Colorado Springs, CO 80916
  54  * +1 (719) 578-3400
  55  *
  56  * Toll free literature number
  57  * +1 (800) 334-5454
  58  *
  59  * PCI BIOS Specification Revision
  60  * PCI Local Bus Specification
  61  * PCI System Design Guide
  62  *
  63  * PCI Special Interest Group
  64  * M/S HF3-15A
  65  * 5200 N.E. Elam Young Parkway
  66  * Hillsboro, Oregon 97124-6497
  67  * +1 (503) 696-2000 
  68  * +1 (800) 433-5177
  69  */
  70 
  71 /*
  72  * Design issues : 
  73  * The cumulative latency needed to propagate a read/write request 
  74  * through the filesystem, buffer cache, driver stacks, SCSI host, and 
  75  * SCSI device is ultimately the limiting factor in throughput once we 
  76  * have a sufficiently fast host adapter.
  77  *  
  78  * So, to maximize performance we want to keep the ratio of latency to data 
  79  * transfer time to a minimum by
  80  * 1.  Minimizing the total number of commands sent (typical command latency
  81  *      including drive and busmastering host overhead is as high as 4.5ms)
  82  *      to transfer a given amount of data.  
  83  *
  84  *      This is accomplished by placing no arbitrary limit on the number
  85  *      of scatter/gather buffers supported, since we can transfer 1K
  86  *      per scatter/gather buffer without Eric's cluster patches, 
  87  *      4K with.  
  88  *
  89  * 2.  Minimizing the number of fatal interrupts serviced, since
  90  *      fatal interrupts halt the SCSI I/O processor.  Basically,
  91  *      this means offloading the practical maximum amount of processing 
  92  *      to the SCSI chip.
  93  * 
  94  *      On the NCR53c810/820,  this is accomplished by using 
  95  *              interrupt-on-the-fly signals with the DSA address as a 
  96  *              parameter when commands complete, and only handling fatal 
  97  *              errors and SDTR / WDTR  messages in the host code.
  98  *
  99  *      On the NCR53c710/720, interrupts are generated as on the NCR53c8x0,
 100  *              only the lack of a interrupt-on-the-fly facility complicates
 101  *              things.  
 102  *              
 103  *      On the NCR53c700 and NCR53c700-66, operations that were done via 
 104  *              indirect, table mode on the more advanced chips have
 105  *              been replaced by calls through a jump table which 
 106  *              acts as a surrogate for the DSA.  Unfortunately, this 
 107  *              means that we must service an interrupt for each 
 108  *              disconnect/reconnect.
 109  * 
 110  * 3.  Eliminating latency by pipelining operations at the different levels.
 111  *      
 112  *      This driver allows a configurable number of commands to be enqueued
 113  *      for each target/lun combination (experimentally, I have discovered
 114  *      that two seems to work best) and will ultimately allow for 
 115  *      SCSI-II tagged queueing.
 116  *      
 117  *
 118  * Architecture : 
 119  * This driver is built around two queues of commands waiting to 
 120  * be executed - the Linux issue queue, and the shared Linux/NCR  
 121  * queue which are manipulated by the NCR53c7xx_queue_command and 
 122  * NCR53c7x0_intr routines.
 123  *
 124  * When the higher level routines pass a SCSI request down to 
 125  * NCR53c7xx_queue_command, it looks to see if that target/lun 
 126  * is currently busy. If not, the command is inserted into the 
 127  * shared Linux/NCR queue, otherwise it is inserted into the Linux 
 128  * queue.
 129  *
 130  * As commands are completed, the interrupt routine is triggered,
 131  * looks for commands in the linked list of completed commands with
 132  * valid status, removes these commands from the list, calls 
 133  * the done routine, and flags their target/luns as not busy.
 134  *
 135  * Due to limitations in the intelligence of the NCR chips, certain
 136  * concessions are made.  In many cases, it is easier to dynamically 
 137  * generate/fixup code rather than calculate on the NCR at run time.  
 138  * So, code is generated or fixed up for
 139  *
 140  * - Handling data transfers, using a variable number of MOVE instructions
 141  *      interspersed with CALL MSG_IN, WHEN MSGIN instructions.
 142  *
 143  *      The DATAIN and DATAOUT routines are separate, so that an incorrect
 144  *      direction can be trapped, and space isn't wasted. 
 145  *
 146  *      It may turn out that we're better off using some sort 
 147  *      of table indirect instruction in a loop with a variable
 148  *      sized table on the NCR53c710 and newer chips.
 149  *
 150  * - Checking for reselection (NCR53c710 and better)
 151  *
 152  * - Handling the details of SCSI context switches (NCR53c710 and better),
 153  *      such as reprogramming appropriate synchronous parameters, 
 154  *      removing the dsa structure from the NCR's queue of outstanding
 155  *      commands, etc.
 156  *
 157  */
 158 
 159 #include <asm/io.h>
 160 #include <asm/system.h>
 161 #include <linux/delay.h>
 162 #include <linux/signal.h>
 163 #include <linux/sched.h>
 164 #include <linux/errno.h>
 165 #include <linux/bios32.h>
 166 #include <linux/pci.h>
 167 #include <linux/string.h>
 168 #include "../block/blk.h"
 169 #include "scsi.h"
 170 #include "hosts.h"
 171 #include "53c7,8xx.h"
 172 #include "constants.h"
 173 #include "sd.h"
 174 
 175 static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int result);
 176 static int NCR53c8xx_run_tests (struct Scsi_Host *host);
 177 static int NCR53c8xx_script_len;
 178 static void NCR53c7x0_intr (int irq);
 179 static void intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd 
 180     *cmd);
 181 static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd);
 182 static void print_dsa (struct Scsi_Host *host, unsigned long *dsa);
 183 static int print_insn (struct Scsi_Host *host, unsigned long *insn,
 184     char *prefix, int kernel);
 185 
 186 static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd);
 187 static void NCR53c8x0_init_fixup (struct Scsi_Host *host);
 188 static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct 
 189     NCR53c7x0_cmd *cmd);
 190 static void NCR53c8x0_soft_reset (struct Scsi_Host *host);
 191 
 192 static struct Scsi_Host *first_host = NULL;     /* Head of list of NCR boards */
 193 static Scsi_Host_Template *the_template = NULL; 
 194 
 195 /* Allocate storage space for constant messages, etc. */
 196 
 197 static long NCR53c7xx_zero = 0;                 
 198 static long NCR53c7xx_sink;
 199 static char NCR53c7xx_msg_reject = MESSAGE_REJECT;
 200 static char NCR53c7xx_msg_abort = ABORT;
 201 static char NCR53c7xx_msg_nop = NOP;
 202 
 203 /* Buffer for commands run before *malloc() works */
 204 /* 
 205  * XXX - if need be, replace this with normal wait.
 206  */
 207 static int scan_scsis_buf_busy = 0;
 208 static char scan_scsis_buf[512];
 209 
 210 /*
 211  * TODO : 
 212  *
 213  * 1.  Implement single step / trace code?
 214  * 
 215  * 2.  The initial code has been tested on the NCR53c810.  I don't 
 216  *     have access to NCR53c700, 700-66 (Forex boards), NCR53c710
 217  *     (NCR Pentium systems), NCR53c720, or NCR53c820 boards to finish
 218  *     development on those platforms.
 219  *
 220  *     NCR53c820/720 - need to add wide transfer support, including WDTR 
 221  *              negotiation, programming of wide transfer capabilities
 222  *              on reselection and table indirect selection.
 223  *
 224  *     NCR53c720/710 - need to add fatal interrupt or GEN code for 
 225  *              command completion signaling.   Need to take care of 
 226  *              ADD WITH CARRY instructions since carry is unimplemented.
 227  *              Also need to modify all SDID, SCID, etc. registers,
 228  *              and table indirect select code since these use bit
 229  *              fielded (ie 1<<target) instead of binary encoded
 230  *              target ids.  Also, SCNTL3 is _not_ automatically
 231  *              programmed on selection, so we need to add more code.
 232  * 
 233  *     NCR53c700/700-66 - need to add code to refix addresses on 
 234  *              every nexus change, eliminate all table indirect code.
 235  *
 236  * 3.  The NCR53c7x0 series is very popular on other platforms that 
 237  *     could be running Linux - ie, some high performance AMIGA SCSI 
 238  *     boards use it.  
 239  *      
 240  *     So, I should include #ifdef'd code so that it is 
 241  *     compatible with these systems.
 242  *      
 243  *     Specifically, the little Endian assumptions I made in my 
 244  *     bit fields need to change, and if the NCR doesn't see memory
 245  *     the right way, we need to provide options to reverse words
 246  *     when the scripts are relocated.
 247  *
 248  * 4.  Implement code to include page table entries for the 
 249  *     area occupied by memory mapped boards so we don't have 
 250  *     to use the potentially slower I/O accesses.
 251  */
 252 
 253 /* 
 254  * XXX - note that my assembler was modified so that internally,
 255  * the names used can take a prefix, so that there is no conflict
 256  * between multiple copies of the same script assembled with 
 257  * different defines.
 258  *
 259  *
 260  * Allow for simultaneous existence of multiple SCSI scripts so we 
 261  * can have a single driver binary for all of the family.
 262  *
 263  * - one for NCR53c700 and NCR53c700-66 chips   (not yet supported)
 264  * - one for NCR53c710 and NCR53c720 chips      (not yet supported)
 265  * - one for NCR53c810 and NCR53c820 chips      (only the NCR53c810 is
 266  *      currently supported)
 267  *
 268  * For the very similar chips, we should probably hack the fixup code
 269  * and interrupt code so that it works everywhere, but I suspect the 
 270  * NCR53c700 is going
 271  * to need it's own fixup routine.
 272  */
 273 
 274 /*
 275  * Use to translate between device IDs of various types.
 276  */
 277 
 278 struct pci_chip {
 279     short pci_device_id;
 280     int chip;
 281     int max_revision;
 282     int min_revision;
 283 };
 284 
 285 static struct pci_chip pci_chip_ids[2] = { 
 286     {PCI_DEVICE_ID_NCR_53C810, 810, 1, 1}, 
 287     {PCI_DEVICE_ID_NCR_53C820, 820, -1, -1},
 288     {PCI_DEVICE_ID_NCR_53C825, 825, -1, -1},
 289 };
 290 
 291 #define NPCI_CHIP_IDS (sizeof (pci_chip_ids) / sizeof(pci_chip_ids[0]))
 292 
 293 
 294 /* Forced detection and autoprobe code for various hardware */
 295 
 296 static struct override {
 297     int chip;   /* 700, 70066, 710, 720, 810, 820 */
 298     int board;  /* Any special board level gunk */
 299     unsigned pci:1;
 300     union {
 301         struct {
 302             int base;   /* Memory address - indicates memory mapped regs */
 303             int io_port;/* I/O port address - indicates I/O mapped regs */
 304             int irq;    /* IRQ line */          
 305             int dma;    /* DMA channel          - often none */
 306         } normal;
 307         struct {
 308             int bus;
 309             int device;
 310             int function;
 311         } pci;
 312     } data;
 313     int options;
 314 } overrides [4] = {{0,},};
 315 static int commandline_current = 0;
 316 static int no_overrides = 0;
 317 
 318 #if 0
 319 #define OVERRIDE_LIMIT (sizeof(overrides) / sizeof(struct override))
 320 #else
 321 #define OVERRIDE_LIMIT commandline_current
 322 #endif
 323 
 324 /*
 325  * Function : static internal_setup(int board, int chip, char *str, int *ints)
 326  *
 327  * Purpose : LILO command line initialization of the overrides array,
 328  * 
 329  * Inputs : board - currently, unsupported.  chip - 700, 70066, 710, 720
 330  *      810, 815, 820, 825, although currently only the NCR53c810 is 
 331  *      supported.
 332  * 
 333  */
 334 
 335 static void internal_setup(int board, int chip, char *str, int *ints) {
     /* [previous][next][first][last][top][bottom][index][help] */
 336     unsigned char pci;          /* Specifies a PCI override, with bus, device,
 337                                    function */
 338 
 339     pci = (str && !strcmp (str, "pci")) ? 1 : 0;
 340     
 341 /*
 342  * Override syntaxes are as follows : 
 343  * ncr53c700,ncr53c700-66,ncr53c710,ncr53c720=mem,io,irq,dma
 344  * ncr53c810,ncr53c820,ncr53c825=mem,io,irq or pci,bus,device,function
 345  */
 346 
 347     if (commandline_current < OVERRIDE_LIMIT) {
 348         overrides[commandline_current].pci = pci ? 1 : 0;
 349         if (!pci) {
 350             overrides[commandline_current].data.normal.base = ints[1];
 351             overrides[commandline_current].data.normal.io_port = ints[2];
 352             overrides[commandline_current].data.normal.irq = ints[3];
 353             overrides[commandline_current].data.normal.dma = (ints[0] >= 4) ?
 354                 ints[4] : DMA_NONE;
 355             overrides[commandline_current].options = (ints[0] >= 5) ?
 356                 ints[5] : 0;
 357         } else {
 358             overrides[commandline_current].data.pci.bus = ints[1];
 359             overrides[commandline_current].data.pci.device = ints[2];
 360             overrides[commandline_current].data.pci.device = ints[3];
 361             overrides[commandline_current].options = (ints[0] >= 4) ?
 362                 ints[4] : 0;
 363         }
 364         overrides[commandline_current].board = board;
 365         overrides[commandline_current].chip = chip;
 366         ++commandline_current;
 367         ++no_overrides;
 368     } else {
 369         printk ("53c7,7x0.c:internal_setup() : too many overrides\n");
 370     }
 371 }
 372 
 373 /*
 374  * XXX - we might want to implement a single override function
 375  *       with a chip type field, revamp the command line configuration,
 376  *       etc.
 377  */
 378 
 379 #define setup_wrapper(x)                                \
 380 void ncr53c##x##_setup (char *str, int *ints) {         \
 381     internal_setup (BOARD_GENERIC, x, str, ints);       \
 382 }
 383 
 384 setup_wrapper(700)
     /* [previous][next][first][last][top][bottom][index][help] */
 385 setup_wrapper(70066)
 386 setup_wrapper(710)
 387 setup_wrapper(720)
 388 setup_wrapper(810)
 389 setup_wrapper(815)
 390 setup_wrapper(820)
 391 setup_wrapper(825)
 392 
 393 /* 
 394  * Function : static int NCR53c7x0_init (struct Scsi_Host *host)
 395  *
 396  * Purpose :  initialize the internal structures for a given SCSI host
 397  *
 398  * Inputs : host - pointer to this host adapter's structure/ 
 399  *
 400  * Preconditions : when this function is called, the chip_type 
 401  *      field of the hostdata structure MUST have been set.
 402  */
 403 
 404 static int NCR53c7x0_init (struct Scsi_Host *host) {
 405     NCR53c7x0_local_declare();
 406     /* unsigned char tmp; */
 407     int i, j, ccf;
 408     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 409         host->hostdata;
 410     struct Scsi_Host *search;
 411     NCR53c7x0_local_setup(host);
 412 
 413     switch (hostdata->chip) {
 414     case 810:
 415     case 815:
 416         hostdata->dstat_sir_intr = NCR53c8x0_dstat_sir_intr;
 417         hostdata->init_save_regs = NULL;
 418         hostdata->dsa_fixup = NCR53c8xx_dsa_fixup;
 419         hostdata->init_fixup = NCR53c8x0_init_fixup;
 420         hostdata->soft_reset = NCR53c8x0_soft_reset;
 421         hostdata->run_tests = NCR53c8xx_run_tests;
 422 /* Is the SCSI clock ever anything else on these chips? */
 423         hostdata->scsi_clock = 40000000;
 424         break;
 425     default:
 426         printk ("scsi%d : chip type of %d is not supported yet, detaching.\n",
 427             host->host_no, hostdata->chip);
 428         scsi_unregister (host);
 429         return -1;
 430     }
 431 
 432     /*
 433      * Set up an interrupt handler if we aren't already sharing an IRQ
 434      * with another board.
 435      */
 436 
 437     for (search = first_host; search && (search->hostt == the_template) &&
 438         (search->irq != host->irq); search=search->next);
 439 
 440     if (!search) {
 441         if (request_irq(host->irq, NCR53c7x0_intr, SA_INTERRUPT, "53c7,8xx")) {
 442             printk("scsi%d : IRQ%d not free, detaching\n", 
 443                 host->host_no, host->irq);
 444             scsi_unregister (host);
 445             return -1;
 446         } 
 447     } else {
 448         printk("scsi%d : using interrupt handler previously installed for scsi%d\n",
 449             host->host_no, search->host_no);
 450     }
 451 
 452     printk ("scsi%d : using %s mapped access\n", host->host_no, 
 453         (hostdata->options & OPTION_MEMORY_MAPPED) ? "memory" : 
 454          "io");
 455 
 456     hostdata->dmode = (hostdata->chip == 700 || hostdata->chip == 70066) ? 
 457         DMODE_REG_00 : DMODE_REG_10;
 458     hostdata->istat = ((hostdata->chip / 100) == 8) ? 
 459         ISTAT_REG_800 : ISTAT_REG_700;
 460 
 461 /* 
 462  * XXX - the NCR53c700 uses bitfielded registers for SCID, SDID, etc,
 463  *      as does the 710 with one bit per SCSI ID.  Conversely, the NCR
 464  *      uses a normal, 3 bit binary representation of these values.
 465  *
 466  * Get the rest of the NCR documentation, and FIND OUT where the change
 467  * was.
 468  */
 469 #if 0
 470     tmp = hostdata->this_id_mask = NCR53c7x0_read8(SCID_REG);
 471     for (host->this_id = 0; tmp != 1; tmp >>=1, ++host->this_id);
 472 #else
 473     host->this_id = NCR53c7x0_read8(SCID_REG) & 7;
 474     hostdata->this_id_mask = 1 << host->this_id;
 475 #endif
 476 
 477     printk("scsi%d : using initiator ID %d\n", host->host_no,
 478         host->this_id);
 479 
 480     /*
 481      * Save important registers to allow a soft reset.
 482      */
 483 
 484     if ((hostdata->chip / 100) == 8) {
 485     /* 
 486      * CTEST4 controls burst mode disable.
 487      */
 488         hostdata->saved_ctest4 = NCR53c7x0_read8(CTEST4_REG_800) & 
 489             CTEST4_800_SAVE;
 490     } else {
 491     /*
 492      * CTEST7 controls cache snooping, burst mode, and support for 
 493      * external differential drivers.
 494      */
 495         hostdata->saved_ctest7 = NCR53c7x0_read8(CTEST7_REG) & CTEST7_SAVE;
 496     }
 497 
 498     /*
 499      * On NCR53c700 series chips, DCNTL controls the SCSI clock divisor,
 500      * on 800 series chips, it allows for a totem-pole IRQ driver.
 501      */
 502     hostdata->saved_dcntl = NCR53c7x0_read8(DCNTL_REG);
 503     
 504     if ((hostdata->chip / 100) == 8)
 505         printk ("scsi%d : using %s interrupts.\n", host->host_no,
 506             (hostdata->saved_dcntl & DCNTL_800_IRQM) ? "level active" : 
 507             "edge triggered");
 508 
 509     /*
 510      * DMODE controls DMA burst length, and on 700 series chips,
 511      * 286 mode and bus width  
 512      */
 513     hostdata->saved_dmode = NCR53c7x0_read8(hostdata->dmode);
 514 
 515     /* 
 516      * Now that burst length and enabled/disabled status is known, 
 517      * clue the user in on it.
 518      */
 519    
 520     if ((hostdata->chip / 100) == 8) {
 521         if (hostdata->saved_ctest4 & CTEST4_800_BDIS) {
 522             printk ("scsi%d : burst mode disabled\n", host->host_no);
 523         } else {
 524             switch (hostdata->saved_dmode & DMODE_BL_MASK) {
 525             case DMODE_BL_2: i = 2; break;
 526             case DMODE_BL_4: i = 4; break;
 527             case DMODE_BL_8: i = 8; break;
 528             case DMODE_BL_16: i = 16; break;
 529             }
 530             printk ("scsi%d ; burst length %d\n", host->host_no, i);
 531         }
 532     }
 533 
 534     /*
 535      * On NCR53c810 and NCR53c820 chips, SCNTL3 contails the synchronous
 536      * and normal clock conversion factors.
 537      */
 538     if (hostdata->chip / 100 == 8)  {
 539         hostdata->saved_scntl3 = NCR53c7x0_read8(SCNTL3_REG_800);
 540         ccf = hostdata->saved_scntl3 & SCNTL3_800_CCF_MASK;
 541     } else
 542         ccf = 0;
 543 
 544     /*
 545      * If we don't have a SCSI clock programmed, pick one on the upper
 546      * bound of that allowed by NCR so that our transfers err on the 
 547      * slow side, since transfer period must be >= the agreed 
 548      * appon period.
 549      */
 550 
 551     if (!hostdata->scsi_clock) 
 552         switch(ccf) {
 553         case 1: hostdata->scsi_clock = 25000000; break; /* Divide by 1.0 */
 554         case 2: hostdata->scsi_clock = 37500000; break; /* Divide by 1.5 */
 555         case 3: hostdata->scsi_clock = 50000000; break; /* Divide by 2.0 */
 556         case 0:                                         /* Divide by 3.0 */
 557         case 4: hostdata->scsi_clock = 66000000; break; 
 558         default: 
 559             printk ("scsi%d : clock conversion factor %d unknown.\n"
 560                     "         synchronous transfers disabled\n",
 561                     host->host_no, ccf);
 562             hostdata->options &= ~OPTION_SYNCHRONOUS;
 563             hostdata->scsi_clock = 0; 
 564         }
 565 
 566     printk ("scsi%d : using %dMHz SCSI clock\n", host->host_no, 
 567         hostdata->scsi_clock / 1000000);
 568     /*
 569      * Initialize per-target structures, including busy flags and 
 570      * synchronous transfer parameters.
 571      */
 572 
 573     for (i = 0; i < 8; ++i) {
 574         for (j = 0; j < 8; ++j)
 575             hostdata->busy[i][j] = 0;
 576         /* 
 577          * NCR53c700 and NCR53c700-66 chips lack the DSA and use a 
 578          * different architecture.  For chips using the DSA architecture,
 579          * initialize the per-target synchronous parameters. 
 580          */
 581         if (hostdata->chip != 700 && hostdata->chip != 70066) {
 582             hostdata->sync[i].select_indirect |= (i << 16); 
 583             /* XXX - program SCSI script for immediate return */ 
 584             hostdata->sync[i].script[0] = (DCMD_TYPE_TCI|DCMD_TCI_OP_RETURN) << 24 | 
 585                 DBC_TCI_TRUE;
 586             switch (hostdata->chip) {
 587             /* Clock divisor */
 588             case 825:
 589             case 820:
 590                 /* Fall through to 810 */
 591             case 815:
 592             case 810:
 593                 hostdata->sync[i].select_indirect |= (hostdata->saved_scntl3) << 24;
 594                 break;
 595             default:
 596             }
 597         }
 598     }
 599 
 600     hostdata->issue_queue = hostdata->running_list = 
 601         hostdata->finished_queue = NULL;
 602     hostdata->issue_dsa_head = 
 603         hostdata->issue_dsa_tail = NULL;
 604 
 605     if (hostdata->init_save_regs)
 606         hostdata->init_save_regs (host);
 607     if (hostdata->init_fixup)
 608         hostdata->init_fixup (host);
 609 
 610     if (!the_template) {
 611         the_template = host->hostt;
 612         first_host = host;
 613     }
 614 
 615     hostdata->idle = 1;
 616 
 617     /* 
 618      * Linux SCSI drivers have always been plagued with initialization 
 619      * problems - some didn't work with the BIOS disabled since they expected
 620      * initialization from it, some didn't work when the networking code
 621      * was enabled and registers got scrambled, etc.
 622      *
 623      * To avoid problems like this, in the future, we will do a soft 
 624      * reset on the SCSI chip, taking it back to a sane state.
 625      */
 626 
 627     hostdata->soft_reset (host);
 628 
 629     hostdata->debug_count_limit = -1;
 630     hostdata->intrs = -1;
 631     hostdata->expecting_iid = 0;
 632     hostdata->expecting_sto = 0;
 633 
 634     if ((hostdata->run_tests && hostdata->run_tests(host) == -1) ||
 635         (hostdata->options & OPTION_DEBUG_TESTS_ONLY)) {
 636         /* XXX Should disable interrupts, etc. here */
 637         scsi_unregister (host);
 638         return -1;
 639     } else 
 640         return 0;
 641 }
 642 
 643 /* 
 644  * Function : static int normal_init(Scsi_Host_Template *tpnt, int board, 
 645  *      int chip, int base, int io_port, int irq, int dma, int pcivalid,
 646  *      unsigned char pci_bus, unsigned char pci_device_fn,
 647  *      int options);
 648  *
 649  * Purpose : initializes a NCR53c7,8x0 based on base addresses,
 650  *      IRQ, and DMA channel.   
 651  *      
 652  *      Useful where a new NCR chip is backwards compatible with
 653  *      a supported chip, but the DEVICE ID has changed so it 
 654  *      doesn't show up when the autoprobe does a pcibios_find_device.
 655  *
 656  * Inputs : tpnt - Template for this SCSI adapter, board - board level
 657  *      product, chip - 810, 820, or 825, bus - PCI bus, device_fn -
 658  *      device and function encoding as used by PCI BIOS calls.
 659  * 
 660  * Returns : 0 on success, -1 on failure.
 661  *
 662  */
 663 
 664 static int normal_init (Scsi_Host_Template *tpnt, int board, int chip, 
     /* [previous][next][first][last][top][bottom][index][help] */
 665     int base, int io_port, int irq, int dma, int pci_valid, 
 666     unsigned char pci_bus, unsigned char pci_device_fn, int options) {
 667     struct Scsi_Host *instance;
 668     struct NCR53c7x0_hostdata *hostdata;
 669     char chip_str[80];
 670     int script_len = 0, size = 0;
 671     int ok = 0;
 672 
 673     
 674     options |= PERM_OPTIONS;
 675 
 676     switch (chip) {
 677     case 825:
 678     case 820:
 679     case 815:
 680     case 810:
 681         script_len = NCR53c8xx_script_len;
 682         options |= OPTION_INTFLY;
 683         sprintf (chip_str, "NCR53c%d", chip);
 684         break;
 685     default:
 686         printk("scsi-ncr53c7,8xx : unsupported SCSI chip %d\n", chip);
 687         return -1;
 688     }
 689 
 690     printk("scsi-ncr53c7,8xx : %s at memory 0x%x, io 0x%x, irq %d",
 691         chip_str, base, io_port, irq);
 692     if (dma == DMA_NONE)
 693         printk("\n");
 694     else 
 695         printk(", dma %d\n", dma);
 696 
 697     if ((chip / 100 == 8) && !pci_valid) 
 698         printk ("scsi-ncr53c7,8xx : for better reliability and performance, please use the\n" 
 699                 "        PCI override instead.\n"
 700                 "        Syntax : ncr53c8{10,20,25}=pci,<bus>,<device>,<function>\n"
 701                 "                 <bus> and <device> are usually 0.\n");
 702 
 703     if (options & OPTION_DEBUG_PROBE_ONLY) {
 704         printk ("scsi-ncr53c7,8xx : probe only enabled, aborting initialization\n");
 705         return -1;
 706     }
 707 
 708     size = sizeof(struct NCR53c7x0_hostdata) + script_len;
 709 
 710     instance = scsi_register (tpnt, size);
 711     hostdata = (struct NCR53c7x0_hostdata *) 
 712         instance->hostdata;
 713     hostdata->size = size;
 714     hostdata->script_count = script_len / sizeof(long);
 715     hostdata = (struct NCR53c7x0_hostdata *) instance->hostdata;
 716     hostdata->board = board;
 717     hostdata->chip = chip;
 718     if ((hostdata->pci_valid = pci_valid)) {
 719         hostdata->pci_bus = pci_bus;
 720         hostdata->pci_device_fn = pci_device_fn;
 721     }
 722 
 723     /*
 724      * Being memory mapped is more desirable, since 
 725      *
 726      * - Memory accesses may be faster.
 727      *
 728      * - The destination and source address spaces are the same for 
 729      *   all instructions, meaning we don't have to twiddle dmode or 
 730      *   any other registers.
 731      *
 732      * So, we try for memory mapped, and if we don't get it,
 733      * we go for port mapped, and that failing we tell the user
 734      * it can't work.
 735      */
 736 
 737     if (base) {
 738         instance->base = (unsigned char *) base;
 739         /* Check for forced I/O mapping */
 740         if (!(options & OPTION_IO_MAPPED)) {
 741             options |= OPTION_MEMORY_MAPPED;
 742             ok = 1;
 743         }
 744     } else {
 745         options &= ~OPTION_MEMORY_MAPPED;
 746     }
 747 
 748     if (io_port) {
 749         instance->io_port = io_port;
 750         options |= OPTION_IO_MAPPED;
 751         ok = 1;
 752     } else {
 753         options &= ~OPTION_IO_MAPPED;
 754     }
 755 
 756     if (!ok) {
 757         printk ("scsi%d : not initializing, no I/O or memory mapping known \n",
 758             instance->host_no);
 759         scsi_unregister (instance);
 760         return -1;
 761     }
 762     instance->irq = irq;
 763     instance->dma_channel = dma;
 764 
 765     hostdata->options = options;
 766     
 767     return NCR53c7x0_init(instance);
 768 }
 769 
 770 
 771 /* 
 772  * Function : static int pci_init(Scsi_Host_Template *tpnt, int board, 
 773  *      int chip, int bus, int device_fn, int options)
 774  *
 775  * Purpose : initializes a NCR53c800 family based on the PCI
 776  *      bus, device, and function location of it.  Allows 
 777  *      reprogramming of latency timer and determining addresses
 778  *      and weather bus mastering, etc. are OK.
 779  *      
 780  *      Useful where a new NCR chip is backwards compatible with
 781  *      a supported chip, but the DEVICE ID has changed so it 
 782  *      doesn't show up when the autoprobe does a pcibios_find_device.
 783  *
 784  * Inputs : tpnt - Template for this SCSI adapter, board - board level
 785  *      product, chip - 810, 820, or 825, bus - PCI bus, device_fn -
 786  *      device and function encoding as used by PCI BIOS calls.
 787  * 
 788  * Returns : 0 on success, -1 on failure.
 789  *
 790  */
 791 
 792 static int pci_init (Scsi_Host_Template *tpnt, int board, int chip, 
     /* [previous][next][first][last][top][bottom][index][help] */
 793     unsigned char bus, unsigned char device_fn, int options) {
 794     unsigned short vendor_id, device_id, command;
 795     unsigned long base, io_port; 
 796     unsigned char irq, revision;
 797     int error, expected_chip, expected_id, max_revision, min_revision;
 798     int i;
 799 
 800     printk("scsi-ncr53c7,8xx : at PCI bus %d, device %d,  function %d\n",
 801         bus, (int) (device_fn & 0xf8) >> 3, 
 802         (int) device_fn & 7);
 803 
 804     if (!pcibios_present) {
 805         printk("scsi-ncr53c7,8xx : not initializing due to lack of PCI BIOS,\n"
 806                "        try using memory, port, irq override instead.\n");
 807         return -1;
 808     }
 809 
 810     if ((error = pcibios_read_config_word (bus, device_fn, PCI_VENDOR_ID, 
 811         &vendor_id)) ||
 812         (error = pcibios_read_config_word (bus, device_fn, PCI_DEVICE_ID, 
 813             &device_id)) ||
 814         (error = pcibios_read_config_word (bus, device_fn, PCI_COMMAND, 
 815             &command)) ||
 816         (error = pcibios_read_config_dword (bus, device_fn, 
 817             PCI_BASE_ADDRESS_0, &io_port)) || 
 818         (error = pcibios_read_config_dword (bus, device_fn, 
 819             PCI_BASE_ADDRESS_1, &base)) ||
 820         (error = pcibios_read_config_byte (bus, device_fn, PCI_CLASS_REVISION,
 821             &revision)) ||
 822         (error = pcibios_read_config_byte (bus, device_fn, PCI_INTERRUPT_LINE,
 823             &irq))) {
 824         printk ("scsi-ncr53c7,8xx : error %s not initializing due to error reading configuration space\n"
 825                 "        perhaps you specified an incorrect PCI bus, device, or function.\n"
 826                 , pcibios_strerror(error));
 827         return -1;
 828     }
 829 
 830     /* If any one ever clones the NCR chips, this will have to change */
 831 
 832     if (vendor_id != PCI_VENDOR_ID_NCR) {
 833         printk ("scsi-ncr53c7,8xx : not initializing, 0x%04x is not NCR vendor ID\n",
 834             (int) vendor_id);
 835         return -1;
 836     }
 837 
 838 
 839     /* 
 840      * Bit 0 is the address space indicator and must be one for I/O
 841      * space mappings, bit 1 is reserved, discard them after checking
 842      * that they have the correct value of 1.
 843      */
 844 
 845     if (command & PCI_COMMAND_IO) { 
 846         if ((io_port & 3) != 1) {
 847             printk ("scsi-ncr53c7,8xx : disabling I/O mapping since base address 0 (0x%lx)\n"
 848                     "        bits 0..1 indicate a non-IO mapping\n", io_port);
 849             io_port = 0;
 850         } else
 851             io_port &= PCI_BASE_ADDRESS_IO_MASK;
 852     }
 853 
 854     if (command & PCI_COMMAND_MEMORY) {
 855         if ((base & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_MEMORY) {
 856             printk("scsi-ncr53c7,8xx : disabling memory mapping since base address 1\n"
 857                    "        contains a non-memory mapping\n");
 858             base = 0;
 859         } else 
 860             base &= PCI_BASE_ADDRESS_MEM_MASK;
 861     }
 862         
 863     if (!io_port && !base) {
 864         printk ("scsi-ncr53c7,8xx : not initializing, both I/O and memory mappings disabled\n");
 865         return -1;
 866     }
 867         
 868     if (!(command & PCI_COMMAND_MASTER)) {
 869         printk ("scsi-ncr53c7,8xx : not initializing, BUS MASTERING was disabled\n");
 870         return -1;
 871     }
 872 
 873     for (i = 0; i < NPCI_CHIP_IDS; ++i) {
 874         if (device_id == pci_chip_ids[i].pci_device_id) {
 875             max_revision = pci_chip_ids[i].max_revision;
 876             min_revision = pci_chip_ids[i].min_revision;
 877             expected_chip = pci_chip_ids[i].chip;
 878         }
 879         if (chip == pci_chip_ids[i].chip)
 880             expected_id = pci_chip_ids[i].pci_device_id;
 881     }
 882 
 883     if (chip && device_id != expected_id) 
 884         printk ("scsi-ncr53c7,8xx : warning : device id of 0x%04x doesn't\n"
 885                 "                   match expected 0x%04x\n",
 886             (unsigned int) device_id, (unsigned int) expected_id );
 887     
 888     if (max_revision != -1 && revision > max_revision) 
 889         printk ("scsi-ncr53c7,8xx : warning : revision of %d is greater than %d.\n",
 890             (int) revision, max_revision);
 891     else if (min_revision != -1 && revision < min_revision)
 892         printk ("scsi-ncr53c7,8xx : warning : revision of %d is less than %d.\n",
 893             (int) revision, min_revision);
 894 
 895     return normal_init (tpnt, board, chip, (int) base, (int) io_port, 
 896         (int) irq, DMA_NONE, 1, bus, device_fn, options);
 897 }
 898 
 899 
 900 /* 
 901  * Function : int NCR53c7xx_detect(Scsi_Host_Template *tpnt)
 902  *
 903  * Purpose : detects and initializes NCR53c7,8x0 SCSI chips
 904  *      that were autoprobed, overridden on the LILO command line, 
 905  *      or specified at compile time.
 906  *
 907  * Inputs : tpnt - template for this SCSI adapter
 908  * 
 909  * Returns : number of host adapters detected
 910  *
 911  */
 912 
 913 int NCR53c7xx_detect(Scsi_Host_Template *tpnt) {
     /* [previous][next][first][last][top][bottom][index][help] */
 914     short current_chip;
 915     int i;
 916     int current_override;
 917     int count;                  /* Number of boards detected */
 918     unsigned char pci_bus, pci_device_fn;
 919     static short pci_index=0;   /* Device index to PCI BIOS calls */
 920     
 921 
 922     for (current_override = count = 0; current_override < OVERRIDE_LIMIT; 
 923          ++current_override) {
 924          if (overrides[current_override].pci ? 
 925             !pci_init (tpnt, overrides[current_override].board,
 926                 overrides[current_override].chip,
 927                 (unsigned char) overrides[current_override].data.pci.bus,
 928                 (((overrides[current_override].data.pci.device
 929                 << 3) & 0xf8)|(overrides[current_override].data.pci.function & 
 930                 7)), overrides[current_override].options):
 931             !normal_init (tpnt, overrides[current_override].board, 
 932                 overrides[current_override].chip, 
 933                 overrides[current_override].data.normal.base, 
 934                 overrides[current_override].data.normal.io_port,
 935                 overrides[current_override].data.normal.irq,
 936                 overrides[current_override].data.normal.dma,
 937                 0 /* PCI data invalid */, 0 /* PCI bus place holder */,  
 938                 0 /* PCI device_function place holder */,
 939                 overrides[current_override].options)) {
 940             ++count;
 941         } 
 942     }
 943 
 944     if (pcibios_present()) {
 945         for (i = 0; i < NPCI_CHIP_IDS; ++i) 
 946             for (pci_index = 0;
 947                 !pcibios_find_device (PCI_VENDOR_ID_NCR, 
 948                     pci_chip_ids[i].pci_device_id, pci_index, &pci_bus, 
 949                     &pci_device_fn) && 
 950                 !pci_init (tpnt, BOARD_GENERIC, pci_chip_ids[i].chip, 
 951                     pci_bus, pci_device_fn, /* no options */ 0); 
 952                 ++count, ++pci_index);
 953     }
 954     return count;
 955 }
 956 
 957 /* NCR53c810 and NCR53c820 script handling code */
 958 
 959 #include "53c8xx_d.h"
 960 static int NCR53c8xx_script_len = sizeof (SCRIPT);
 961 
 962 /* 
 963  * Function : static void NCR53c8x0_init_fixup (struct Scsi_Host *host)
 964  *
 965  * Purpose :  copy and fixup the SCSI SCRIPTS(tm) code for this device.
 966  *
 967  * Inputs : host - pointer to this host adapter's structure
 968  *
 969  */
 970 
 971 static void NCR53c8x0_init_fixup (struct Scsi_Host *host) {
     /* [previous][next][first][last][top][bottom][index][help] */
 972     NCR53c7x0_local_declare();
 973     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
 974         host->hostdata;
 975     unsigned char tmp;
 976     int i, ncr_to_memory, memory_to_ncr, ncr_to_ncr;
 977     unsigned long base;
 978     NCR53c7x0_local_setup(host);
 979 
 980 
 981 
 982     /* XXX - NOTE : this code MUST be made endian aware */
 983     /*  Copy code into buffer that was allocated at detection time.  */
 984     memcpy ((void *) hostdata->script, (void *) SCRIPT, 
 985         sizeof(SCRIPT));
 986     /* Fixup labels */
 987     for (i = 0; i < PATCHES; ++i) 
 988         hostdata->script[LABELPATCHES[i]] += 
 989             (unsigned long) hostdata->script;
 990 
 991     /* 
 992      * Fixup absolutes set at boot-time.
 993      * 
 994      * All Absolute variables suffixed with "dsa_" and "int_"
 995      * are constants, and need no fixup provided the assembler has done 
 996      * it for us (I don't know what the "real" NCR assembler does in 
 997      * this case, my assembler does the right magic).
 998      */
 999 
1000     /*
1001      * Just for the hell of it, preserve the settings of 
1002      * Burst Length and Enable Read Line bits from the DMODE 
1003      * register.  Make sure SCRIPTS start automagically.
1004      */
1005 
1006     tmp = NCR53c7x0_read8(DMODE_REG_10);
1007     tmp &= (DMODE_800_ERL | DMODE_BL_MASK);
1008 
1009     if (!(hostdata->options & OPTION_MEMORY_MAPPED)) {
1010         base = (long) host->io_port;
1011         memory_to_ncr = tmp|DMODE_800_DIOM;
1012         ncr_to_memory = tmp|DMODE_800_SIOM;
1013         ncr_to_ncr = tmp|DMODE_800_DIOM|DMODE_800_SIOM;
1014     } else {
1015         base = (long) host->base;
1016         ncr_to_ncr = memory_to_ncr = ncr_to_memory = tmp;
1017     }
1018 
1019     printk ("scsi%d : m_to_n = 0x%x, n_to_m = 0x%x, n_to_n = 0x%x\n",
1020             (int) host->host_no, (int) memory_to_ncr, (int) 
1021             ncr_to_memory, ncr_to_ncr);
1022 
1023     patch_abs_32 (hostdata->script, 0, addr_scratch, base + SCRATCHA_REG_800);
1024     patch_abs_32 (hostdata->script, 0, addr_sfbr, base + SFBR_REG);
1025     patch_abs_32 (hostdata->script, 0, addr_temp, base + TEMP_REG);
1026 
1027     /*
1028      * I needed some variables in the script to be accessible to 
1029      * both the NCR chip and the host processor. For these variables,
1030      * I made the arbitrary decision to store them directly in the 
1031      * hostdata structure rather than in the RELATIVE area of the 
1032      * SCRIPTS.
1033      */
1034     
1035 
1036     patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_memory, tmp);
1037     patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_ncr, memory_to_ncr);
1038     patch_abs_rwri_data (hostdata->script, 0, dmode_ncr_to_memory, ncr_to_memory);
1039     patch_abs_rwri_data (hostdata->script, 0, dmode_ncr_to_ncr, ncr_to_ncr);
1040 
1041     patch_abs_32 (hostdata->script, 0, issue_dsa_head, (long) &(hostdata->issue_dsa_head));
1042     patch_abs_32 (hostdata->script, 0, msg_buf, (long) &(hostdata->msg_buf));
1043     patch_abs_32 (hostdata->script, 0, reconnect_dsa_head, (long) &(hostdata->reconnect_dsa_head));
1044     patch_abs_32 (hostdata->script, 0, reselected_identify, (long) &(hostdata->reselected_identify));
1045     patch_abs_32 (hostdata->script, 0, reselected_tag, (long) &(hostdata->reselected_tag));
1046 
1047     patch_abs_32 (hostdata->script, 0, test_dest, (long) &(hostdata->test_dest));
1048     patch_abs_32 (hostdata->script, 0, test_src, (long) &(hostdata->test_source));
1049 
1050 
1051     /*
1052      * Make sure the NCR and Linux code agree on the location of 
1053      * certain fields.
1054      */
1055 
1056 /* 
1057  * XXX - for cleanness, E_* fields should be type unsigned long *
1058  * and should reflect the _relocated_ addresses.  Change this.
1059  */
1060     hostdata->E_accept_message = Ent_accept_message;
1061     hostdata->E_command_complete = Ent_command_complete;                
1062     hostdata->E_debug_break = Ent_debug_break;  
1063     hostdata->E_dsa_code_template = Ent_dsa_code_template;
1064     hostdata->E_dsa_code_template_end = Ent_dsa_code_template_end;
1065     hostdata->E_initiator_abort = Ent_initiator_abort;
1066     hostdata->E_msg_in = Ent_msg_in;
1067     hostdata->E_other_transfer = Ent_other_transfer;
1068     hostdata->E_reject_message = Ent_reject_message;
1069     hostdata->E_respond_message = Ent_respond_message;
1070     hostdata->E_schedule = Ent_schedule;                        
1071     hostdata->E_select = Ent_select;
1072     hostdata->E_select_msgout = Ent_select_msgout;
1073     hostdata->E_target_abort = Ent_target_abort;
1074 #ifdef Ent_test_0
1075     hostdata->E_test_0 = Ent_test_0;
1076 #endif
1077     hostdata->E_test_1 = Ent_test_1;
1078     hostdata->E_test_2 = Ent_test_2;
1079 #ifdef Ent_test_3
1080     hostdata->E_test_3 = Ent_test_3;
1081 #endif
1082 
1083     hostdata->dsa_cmdout = A_dsa_cmdout;
1084     hostdata->dsa_cmnd = A_dsa_cmnd;
1085     hostdata->dsa_datain = A_dsa_datain;
1086     hostdata->dsa_dataout = A_dsa_dataout;
1087     hostdata->dsa_end = A_dsa_end;                      
1088     hostdata->dsa_msgin = A_dsa_msgin;
1089     hostdata->dsa_msgout = A_dsa_msgout;
1090     hostdata->dsa_msgout_other = A_dsa_msgout_other;
1091     hostdata->dsa_next = A_dsa_next;
1092     hostdata->dsa_select = A_dsa_select;
1093     hostdata->dsa_start = Ent_dsa_code_template - Ent_dsa_zero;
1094     hostdata->dsa_status = A_dsa_status;
1095 
1096     /* sanity check */
1097     if (A_dsa_fields_start != Ent_dsa_code_template_end - 
1098         Ent_dsa_zero) 
1099         printk("scsi%d : NCR dsa_fields start is %d not %d\n",
1100             host->host_no, A_dsa_fields_start, Ent_dsa_code_template_end - 
1101             Ent_dsa_zero);
1102 
1103     printk("scsi%d : NCR code relocated to 0x%lx\n", host->host_no,
1104         (unsigned long) hostdata->script);
1105 }
1106 
1107 /*
1108  * Function : static int NCR53c8xx_run_tests (struct Scsi_Host *host)
1109  *
1110  * Purpose : run various verification tests on the NCR chip, 
1111  *      including interrupt generation, and proper bus mastering
1112  *      operation.
1113  * 
1114  * Inputs : host - a properly initialized Scsi_Host structure
1115  *
1116  * Preconditions : the NCR chip must be in a halted state.
1117  *
1118  * Returns : 0 if all tests were successful, -1 on error.
1119  * 
1120  */
1121 
1122 static int NCR53c8xx_run_tests (struct Scsi_Host *host) {
     /* [previous][next][first][last][top][bottom][index][help] */
1123     NCR53c7x0_local_declare();
1124     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1125         host->hostdata;
1126     unsigned long timeout, start;
1127     int old_level, failed, i;
1128     NCR53c7x0_local_setup(host);
1129 
1130     printk("scsi%d : testing\n", host->host_no);
1131 
1132     /* The NCR chip _must_ be idle to run the test scripts */
1133 
1134     old_level = splx(0);
1135     if (!hostdata->idle) {
1136         printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
1137         splx(old_level);
1138         return -1;
1139     }
1140 
1141     /* 
1142      * Check for functional interrupts, this could work as an
1143      * autoprobe routine.
1144      */
1145 
1146     if (hostdata->issue_dsa_head) {
1147         printk ("scsi%d : hostdata->issue_dsa_head corrupt before test 1\n",
1148             host->host_no);
1149         hostdata->issue_dsa_head = NULL;
1150     }
1151         
1152     if (hostdata->options & OPTION_DEBUG_TEST1) {
1153         hostdata->idle = 0;
1154         hostdata->test_running = 1;
1155         hostdata->test_completed = -1;
1156         hostdata->test_dest = 0;
1157         hostdata->test_source = 0xdeadbeef;
1158         start = ((unsigned long) hostdata->script) + hostdata->E_test_1;
1159         hostdata->state = STATE_RUNNING;
1160         printk ("scsi%d : test 1", host->host_no);
1161         NCR53c7x0_write32 (DSP_REG, start);
1162         printk (" started\n");
1163         splx(7);
1164 
1165         timeout = jiffies + 50; /* arbitrary */
1166         while ((hostdata->test_completed == -1) && jiffies < timeout);
1167 
1168         failed = 1;
1169         if (hostdata->test_completed == -1)
1170             printk ("scsi%d : driver test 1 timed out%s\n",host->host_no ,
1171                 (hostdata->test_dest == 0xdeadbeef) ? 
1172                     " due to lost interrupt.\n"
1173                     "         Please verify that the correct IRQ is being used for your board,\n"
1174                     "         and that the motherboard IRQ jumpering matches the PCI setup on\n"
1175                     "         PCI systems.\n"
1176                     "         If you are using a NCR53c810 board in a PCI system, you should\n" 
1177                     "         also verify that the board is jumpered to use PCI INTA, since\n"
1178                     "         most PCI motherboards lack support for INTB, INTC, and INTD.\n"
1179                     : "");
1180         else if (hostdata->test_completed != 1) 
1181             printk ("scsi%d : test 1 bad interrupt value (%ld)\n", host->host_no,
1182                 hostdata->test_completed);
1183         else 
1184             failed = (hostdata->test_dest != 0xdeadbeef);
1185 
1186         if (hostdata->test_dest != 0xdeadbeef) {
1187             printk ("scsi%d : driver test 1 read 0x%x instead of 0xdeadbeef indicating a\n"
1188                     "         probable cache invalidation problem.  Please configure caching\n"
1189                     "         as write-through or disabled\n",
1190                 host->host_no, hostdata->test_dest);
1191         }
1192 
1193         if (failed) {
1194             printk ("scsi%d : DSP = 0x%lx (script at 0x%lx, start at 0x%lx)\n",
1195                 host->host_no, (unsigned long) NCR53c7x0_read32(DSP_REG),
1196                 (unsigned long) hostdata->script, start);
1197             printk ("scsi%d : DSPS = 0x%lx\n", host->host_no,
1198                 (unsigned long) NCR53c7x0_read32(DSPS_REG));
1199             splx(old_level);
1200             return -1;
1201         }
1202         hostdata->test_running = 0;
1203     }
1204 
1205     if (hostdata->issue_dsa_head) {
1206         printk ("scsi%d : hostdata->issue_dsa_head corrupt after test 1\n",
1207             host->host_no);
1208         hostdata->issue_dsa_head = NULL;
1209     }
1210 
1211     if (hostdata->options & OPTION_DEBUG_TEST2) {
1212         unsigned long dsa[48];
1213         unsigned char identify = IDENTIFY(0, 0);
1214         unsigned char cmd[6];
1215         unsigned char data[36];
1216         unsigned char status = 0xff;
1217         unsigned char msg = 0xff;
1218 
1219         cmd[0] = INQUIRY;
1220         cmd[1] = cmd[2] = cmd[3] = cmd[5] = 0;
1221         cmd[4] = sizeof(data); 
1222 
1223         dsa[2] = 1;
1224         dsa[3] = (unsigned long) &identify;
1225         dsa[4] = 6;
1226         dsa[5] = (unsigned long) &cmd;
1227         dsa[6] = sizeof(data);
1228         dsa[7] = (unsigned long) &data;
1229         dsa[8] = 1;
1230         dsa[9] = (unsigned long) &status;
1231         dsa[10] = 1;
1232         dsa[11] = (unsigned long) &msg;
1233 
1234         for (i = 0; i < 3; ++i) {
1235             splx(0);
1236             if (!hostdata->idle) {
1237                 printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
1238                 splx(old_level);
1239                 return -1;
1240             }
1241 
1242             /*       SCNTL3         SDID        */
1243             dsa[0] = (0x33 << 24) | (i << 16)  ;
1244             hostdata->idle = 0;
1245             hostdata->test_running = 2;
1246             hostdata->test_completed = -1;
1247             start = ((unsigned long) hostdata->script) + hostdata->E_test_2;
1248             hostdata->state = STATE_RUNNING;
1249             NCR53c7x0_write32 (DSA_REG, (unsigned long) dsa);
1250             NCR53c7x0_write32 (DSP_REG, start);
1251             splx(7);
1252 
1253             timeout = jiffies + 500;    /* arbitrary */
1254             while ((hostdata->test_completed == -1) && jiffies < timeout);
1255             NCR53c7x0_write32 (DSA_REG, 0);
1256 
1257             if (hostdata->test_completed == 2) {
1258                 data[35] = 0;
1259                 printk ("scsi%d : test 2 INQUIRY to target %d, lun 0 : %s\n",
1260                     host->host_no, i, data + 8);
1261                 printk ("scsi%d : status ", host->host_no);
1262                 print_status (status);
1263                 printk ("\nscsi%d : message ", host->host_no);
1264                 print_msg (&msg);
1265                 printk ("\n");
1266             } else if (hostdata->test_completed == 3) {
1267                 printk("scsi%d : test 2 no connection with target %d\n",
1268                     host->host_no, i);
1269                 if (!hostdata->idle) {
1270                     printk("scsi%d : not idle\n", host->host_no);
1271                     splx(old_level);
1272                     return -1;
1273                 }
1274             } else if (hostdata->test_completed == -1) {
1275                 printk ("scsi%d : test 2 timed out\n", host->host_no);
1276                 splx(old_level);
1277                 return -1;
1278             } 
1279             hostdata->test_running = 0;
1280             if (hostdata->issue_dsa_head) {
1281                 printk ("scsi%d : hostdata->issue_dsa_head corrupt after test 2 id %d\n",
1282                     host->host_no, i);
1283                 hostdata->issue_dsa_head = NULL;
1284         }
1285         }
1286     }
1287     printk ("scsi%d : tests complete.\n", host->host_no);
1288 
1289     splx(old_level);
1290     return 0;
1291 }
1292 
1293 /*
1294  * Function : static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd)
1295  *
1296  * Purpose : copy the NCR53c8xx dsa structure into cmd's dsa buffer,
1297  *      performing all necessary relocation.
1298  *
1299  * Inputs : cmd, a NCR53c7x0_cmd structure with a dsa area large
1300  *      enough to hold the NCR53c8xx dsa.
1301  */
1302 
1303 static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
1304     Scsi_Cmnd *c = cmd->cmd;
1305     struct Scsi_Host *host = c->host;
1306     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1307         host->hostdata;
1308     int i;
1309 
1310     memcpy (cmd->dsa, hostdata->script + (hostdata->E_dsa_code_template / 4),
1311         hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template);
1312 
1313     patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(long), 
1314         dsa_temp_jump_resume, ((unsigned long) cmd->dsa) + 
1315         Ent_dsa_jump_resume - Ent_dsa_zero);
1316     patch_abs_rwri_data (cmd->dsa, Ent_dsa_code_template / sizeof(long),
1317         dsa_temp_lun, c->lun);
1318     patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(long),
1319         dsa_temp_dsa_next, ((unsigned long) cmd->dsa) + A_dsa_next);
1320     patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(long), 
1321         dsa_temp_sync, hostdata->sync[c->target].select_indirect);
1322     patch_abs_rwri_data (cmd->dsa, Ent_dsa_code_template / sizeof(long),
1323         dsa_temp_target, c->target);
1324 }
1325 
1326 /*
1327  * Function : static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int
1328  *      result)
1329  *
1330  * Purpose : mark SCSI command as finished, OR'ing the host portion 
1331  *      of the result word into the result field of the corresponding
1332  *      Scsi_Cmnd structure, and removing it from the internal queues.
1333  *
1334  * Inputs : cmd - command, result - entire result field
1335  *
1336  * Preconditions : the  NCR chip should be in a halted state when 
1337  *      abnormal_finished is run, since it modifies structures which
1338  *      the NCR expects to have exclusive access to.
1339  */
1340 
1341 static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int result) {
     /* [previous][next][first][last][top][bottom][index][help] */
1342     Scsi_Cmnd *c = cmd->cmd;
1343     struct Scsi_Host *host = c->host;
1344     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1345         host->hostdata;
1346     int old_level;
1347     char **prev, *search;
1348     int i;
1349 
1350     old_level = splx(0);
1351     for (i = 0; i < 2; ++i) {
1352         for (search = (char *) (i ? hostdata->issue_dsa_head : 
1353                 hostdata->reconnect_dsa_head), prev = (char **) (i ? 
1354                 &(hostdata->issue_dsa_head) : &(hostdata->reconnect_dsa_head));
1355              search && (search + hostdata->dsa_start) != (char *) cmd->dsa;
1356              prev = (char **) (search + hostdata->dsa_next), 
1357                 search = *prev);
1358 
1359         if (search)
1360             *prev = *(char **) (search + hostdata->dsa_next);
1361     }
1362 
1363     if (cmd->prev)
1364         cmd->prev->next = cmd->next;
1365 
1366     if (cmd->next)
1367         cmd->next->prev = cmd->prev;
1368 
1369     if (hostdata->running_list == cmd)
1370         hostdata->running_list = cmd->next;
1371 
1372     if (!scan_scsis_buf_busy) {
1373 #ifdef SCSI_MALLOC
1374         scsi_free ((void *) cmd->real, cmd->size);
1375 #else
1376         kfree_s (cmd->real, cmd->size);
1377 #endif
1378     } else {
1379         scan_scsis_buf_busy = 0;
1380     }
1381 
1382     c->host_scribble = NULL;
1383     c->result = result;
1384     c->scsi_done(c);
1385 
1386     splx(old_level);
1387 }
1388 
1389 /* 
1390  * Function : static void intr_break (struct Scsi_Host *host,
1391  *      struct NCR53c7x0_cmd *cmd)
1392  *
1393  * Purpose :  Handler for breakpoint interrupts from a SCSI script
1394  *
1395  * Inputs : host - pointer to this host adapter's structure,
1396  *      cmd - pointer to the command (if any) dsa was pointing 
1397  *      to.
1398  *
1399  */
1400 
1401 static void intr_break (struct Scsi_Host *host, struct 
     /* [previous][next][first][last][top][bottom][index][help] */
1402     NCR53c7x0_cmd *cmd) {
1403     NCR53c7x0_local_declare();
1404     struct NCR53c7x0_break *bp;
1405 #if 0
1406     Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
1407 #endif
1408     unsigned long *dsp;
1409     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1410         host->hostdata;         
1411     int old_level;
1412     NCR53c7x0_local_setup(host);
1413 
1414     /*
1415      * Find the break point corresponding to this address, and 
1416      * dump the appropriate debugging information to standard 
1417      * output.  
1418      */
1419 
1420     old_level = splx(0);
1421     dsp = (unsigned long *) NCR53c7x0_read32(DSP_REG);
1422     for (bp = hostdata->breakpoints; bp && bp->address != dsp; 
1423         bp = bp->next);
1424     if (!bp) 
1425         panic("scsi%d : break point interrupt from %p with no breakpoint!",
1426             host->host_no, dsp);
1427 
1428     /*
1429      * Configure the NCR chip for manual start mode, so that we can 
1430      * point the DSP register at the instruction that follows the 
1431      * INT int_debug_break instruction.
1432      */
1433 
1434     NCR53c7x0_write8 (hostdata->dmode, 
1435         NCR53c7x0_read8(hostdata->dmode)|DMODE_MAN);
1436 
1437     /*
1438      * And update the DSP register, using the size of the old 
1439      * instruction in bytes.
1440      */
1441 
1442     splx(old_level);
1443 }
1444 
1445 /*
1446  * Function : static int asynchronous (struct Scsi_Host *host, int target)
1447  *
1448  * Purpose : reprogram between the selected SCSI Host adapter and target 
1449  *      (assumed to be currently connected) for asynchronous transfers.
1450  *
1451  * Inputs : host - SCSI host structure, target - numeric target ID.
1452  *
1453  * Preconditions : the NCR chip should be in one of the halted states
1454  */
1455     
1456 static int asynchronous (struct Scsi_Host *host, int target) {
     /* [previous][next][first][last][top][bottom][index][help] */
1457     NCR53c7x0_local_declare();
1458     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1459         host->hostdata;
1460     NCR53c7x0_local_setup(host);
1461 
1462     if ((hostdata->chip / 100) == 8) {
1463         hostdata->sync[target].select_indirect = (hostdata->saved_scntl3 << 24)
1464             | (target << 16);
1465 /* Fill in script here */
1466     } else if ((hostdata->chip != 700) && (hostdata->chip != 70066)) {
1467         hostdata->sync[target].select_indirect = (1 << (target & 7)) << 16;
1468     }
1469 
1470 /* 
1471  * Halted implies connected, when resetting we shouldn't change the 
1472  * current parameters but must reset all targets to asynchronous.
1473  */
1474 
1475     if (hostdata->state == STATE_HALTED) {
1476         if ((hostdata->chip / 100) == 8) {
1477             NCR53c7x0_write8 (SCNTL3_REG_800, hostdata->saved_scntl3);
1478         }
1479     /* Offset = 0, transfer period = divide SCLK by 4 */
1480         NCR53c7x0_write8 (SXFER_REG, 0);
1481     }
1482     return 0;
1483 }
1484 
1485 /* 
1486  * XXX - do we want to go out of our way (ie, add extra code to selection
1487  *      in the NCR53c710/NCR53c720 script) to reprogram the synchronous
1488  *      conversion bits, or can we be content in just setting the 
1489  *      sxfer bits?
1490  */
1491 
1492 /* Table for NCR53c8xx synchronous values */
1493 static const struct {
1494     int div;
1495     unsigned char scf;
1496     unsigned char tp;
1497 } syncs[] = {
1498 /*      div     scf     tp      div     scf     tp      div     scf     tp */
1499     {   40,     1,      0}, {   50,     1,      1}, {   60,     1,      2}, 
1500     {   70,     1,      3}, {   75,     2,      1}, {   80,     1,      4},
1501     {   90,     1,      5}, {   100,    1,      6}, {   105,    2,      3},
1502     {   110,    1,      7}, {   120,    2,      4}, {   135,    2,      5},
1503     {   140,    3,      3}, {   150,    2,      6}, {   160,    3,      4},
1504     {   165,    2,      7}, {   180,    3,      5}, {   200,    3,      6},
1505     {   210,    4,      3}, {   220,    3,      7}, {   240,    4,      4},
1506     {   270,    4,      5}, {   300,    4,      6}, {   330,    4,      7}
1507 };
1508 
1509 /*
1510  * Function : static void synchronous (struct Scsi_Host *host, int target, 
1511  *      char *msg)
1512  *
1513  * Purpose : reprogram transfers between the selected SCSI initiator and 
1514  *      target for synchronous SCSI transfers such that the synchronous 
1515  *      offset is less than that requested and period at least as long 
1516  *      as that requested.  Also modify *msg such that it contains 
1517  *      an appropriate response. 
1518  *
1519  * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
1520  *      msg - synchronous transfer request.
1521  */
1522 
1523 
1524 static void synchronous (struct Scsi_Host *host, int target, char *msg) {
     /* [previous][next][first][last][top][bottom][index][help] */
1525     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1526         host->hostdata;
1527     int desire, divisor, i, limit;
1528     unsigned long *script;
1529     unsigned char scntl3, sxfer;
1530    
1531 /* Scale divisor by 10 to accommodate fractions */ 
1532     desire = 1000000000L / (msg[3] * 4);
1533     divisor = desire / (hostdata->scsi_clock / 10);
1534 
1535     if (msg[4] > 8)
1536         msg[4] = 8;
1537 
1538     printk("scsi%d : optimal synchronous divisor of %d.%01d\n", host->host_no,
1539         divisor / 10, divisor % 10);
1540 
1541     limit = (sizeof(syncs) / sizeof(syncs[0])) - 1;
1542     for (i = 0; (i < limit) && (divisor < syncs[i + 1].div); ++i);
1543 
1544     printk("scsi%d : selected synchronous divisor of %d.%01d\n", host->host_no,
1545         syncs[i].div / 10, syncs[i].div % 10);
1546 
1547     msg[3] = (1000000000 / divisor / 10 / 4);
1548 
1549     scntl3 = (hostdata->chip / 100 == 8) ? ((hostdata->saved_scntl3 & 
1550         ~SCNTL3_800_SCF_MASK) | (syncs[i].scf << SCNTL3_800_SCF_SHIFT)) : 0;
1551     sxfer = (msg[4] << SXFER_MO_SHIFT) | ((syncs[i].tp) << SXFER_TP_SHIFT);
1552 
1553     if ((hostdata->chip != 700) && (hostdata->chip != 70066)) {
1554         hostdata->sync[target].select_indirect = (scntl3 << 24) | (target << 16) | 
1555                 (sxfer << 8);
1556 
1557         script = hostdata->sync[target].script;
1558 
1559         /* XXX - add NCR53c7x0 code to reprogram SCF bits if we want to */
1560         if ((hostdata->chip / 100) == 8) {
1561             script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
1562                 DCMD_RWRI_OP_MOVE) << 24) |
1563                 (SCNTL3_REG_800 << 16) | (scntl3 << 8);
1564             script[1] = 0;
1565             script += 2;
1566         }
1567 
1568         script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
1569             DCMD_RWRI_OP_MOVE) << 24) |
1570                 (SXFER_REG << 16) | (sxfer << 8);
1571         script[1] = 0;
1572         script += 2;
1573 
1574         script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_RETURN) << 24) | DBC_TCI_TRUE;
1575         script[1] = 0;
1576         script += 2;
1577     }
1578 }
1579 
1580 /* 
1581  * Function : static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host,
1582  *      struct NCR53c7x0_cmd *cmd)
1583  *
1584  * Purpose :  Handler for INT generated instructions for the 
1585  *      NCR53c810/820 SCSI SCRIPT
1586  *
1587  * Inputs : host - pointer to this host adapter's structure,
1588  *      cmd - pointer to the command (if any) dsa was pointing 
1589  *      to.
1590  *
1591  */
1592 
1593 static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct 
     /* [previous][next][first][last][top][bottom][index][help] */
1594     NCR53c7x0_cmd *cmd) {
1595     NCR53c7x0_local_declare();
1596     Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
1597     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1598         host->hostdata;         
1599     unsigned long dsps,*dsp;    /* Argument of the INT instruction */
1600     NCR53c7x0_local_setup(host);
1601     dsps = NCR53c7x0_read32(DSPS_REG);
1602     dsp = (unsigned long *) NCR53c7x0_read32(DSP_REG);
1603 
1604     if (hostdata->options & OPTION_DEBUG_INTR) 
1605         printk ("scsi%d : DSPS = 0x%lx\n", host->host_no, dsps);
1606 
1607     switch (dsps) {
1608     case A_int_msg_1:
1609         printk ("scsi%d : message", host->host_no);
1610         if (cmd) 
1611             printk (" from target %d lun %d", c->target, c->lun);
1612         print_msg (hostdata->msg_buf);
1613         printk("\n");
1614         switch (hostdata->msg_buf[0]) {
1615         /* 
1616          * Unless we've initiated synchronous negotiation, I don't
1617          * think that this should happen.
1618          */
1619         case MESSAGE_REJECT:
1620             hostdata->dsp = hostdata->script + hostdata->E_accept_message /
1621                 sizeof(long);
1622             hostdata->dsp_changed = 1;
1623             break;
1624         case INITIATE_RECOVERY:
1625             printk ("scsi%d : extended contingent allegiance not supported yet, rejecting\n",
1626                 host->host_no);
1627             hostdata->dsp = hostdata->script + hostdata->E_reject_message /
1628                 sizeof(long);
1629             hostdata->dsp_changed = 1;
1630         }
1631         return SPECIFIC_INT_NOTHING;
1632     case A_int_msg_sdtr:
1633         if (cmd) {
1634             printk ("scsi%d : target %d %s synchronous transfer period %dns, offset%d\n",
1635                 host->host_no, c->target, (cmd->flags & CMD_FLAG_SDTR) ? "accepting" :
1636                 "requesting", hostdata->msg_buf[3] * 4, hostdata->msg_buf[4]);
1637         /* 
1638          * Initiator initiated, won't happen unless synchronous 
1639          *      transfers are enabled.  If we get a SDTR message in
1640          *      response to our SDTR, we should program our parameters
1641          *      such that 
1642          *              offset <= requested offset
1643          *              period >= requested period                      
1644          */
1645             if (cmd->flags & CMD_FLAG_SDTR) {
1646                 cmd->flags &= ~CMD_FLAG_SDTR; 
1647                 synchronous (host, c->target, hostdata->msg_buf);
1648                 hostdata->dsp = hostdata->script + hostdata->E_accept_message /
1649                     sizeof(long);
1650                 hostdata->dsp_changed = 1;
1651                 return SPECIFIC_INT_NOTHING;
1652             } else {
1653                 if (hostdata->options & OPTION_SYNCHRONOUS)  {
1654                     cmd->flags |= CMD_FLAG_DID_SDTR;
1655                     synchronous (host, c->target, hostdata->msg_buf);
1656                 } else {
1657                     hostdata->msg_buf[4] = 0;           /* 0 offset = async */
1658                 }
1659 
1660                 patch_dsa_32 (cmd->dsa, dsa_msgout_other, 0, 5);
1661                 patch_dsa_32 (cmd->dsa, dsa_msgout_other, 1, 
1662                     hostdata->msg_buf);
1663                 hostdata->dsp = hostdata->script + 
1664                 hostdata->E_respond_message / sizeof(long);
1665                 hostdata->dsp_changed = 1;
1666             }
1667 
1668             if (hostdata->msg_buf[4]) {
1669                 int Hz = 1000000000 / (hostdata->msg_buf[3] * 4);
1670                 printk ("scsi%d : setting target %d to %d.%02dMhz %s SCSI%s\n"
1671                         "         period = %dns, max offset = %d\n",
1672                         host->host_no, c->target, Hz / 1000000, Hz % 1000000,
1673                         ((hostdata->msg_buf[3] < 200) ? "FAST " : 
1674                         "synchronous") ,
1675                         ((hostdata->msg_buf[3] < 200) ? "-II" : ""),
1676                         (int) hostdata->msg_buf[3] * 4, (int) 
1677                         hostdata->msg_buf[4]);
1678             } else {
1679                 printk ("scsi%d : setting target %d to asynchronous SCSI\n",
1680                     host->host_no, c->target);
1681             }
1682             return SPECIFIC_INT_NOTHING;
1683         }
1684         /* Fall through to abort */
1685     case A_int_msg_wdtr:
1686         hostdata->dsp = hostdata->script + hostdata->E_reject_message /
1687             sizeof(long);
1688         hostdata->dsp_changed = 1;
1689         return SPECIFIC_INT_NOTHING;
1690     case A_int_err_unexpected_phase:
1691         if (hostdata->options & OPTION_DEBUG_INTR) 
1692             printk ("scsi%d : unexpected phase\n", host->host_no);
1693         return SPECIFIC_INT_ABORT;
1694     case A_int_err_selected:
1695         printk ("scsi%d : selected by target %d\n", host->host_no,
1696             (int) NCR53c7x0_read8(SSID_REG_800) &7);
1697         hostdata->dsp = hostdata->script + hostdata->E_target_abort / 
1698             sizeof(long);
1699         hostdata->dsp_changed = 1;
1700         return SPECIFIC_INT_NOTHING;
1701     case A_int_err_unexpected_reselect:
1702         printk ("scsi%d : unexpected reselect by target %d\n", host->host_no,
1703             (int) NCR53c7x0_read8(SSID_REG_800));
1704         hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
1705             sizeof(long);
1706         hostdata->dsp_changed = 1;
1707         return SPECIFIC_INT_NOTHING;
1708 /*
1709  * Since contingent allegiance conditions are cleared by the next 
1710  * command issued to a target, we must issue a REQUEST SENSE 
1711  * command after receiving a CHECK CONDITION status, before
1712  * another command is issued.
1713  * 
1714  * Since this NCR53c7x0_cmd will be freed after use, we don't 
1715  * care if we step on the various fields, so modify a few things.
1716  */
1717     case A_int_err_check_condition: 
1718 #if 0
1719         if (hostdata->options & OPTION_DEBUG_INTR) 
1720 #endif
1721             printk ("scsi%d : CHECK CONDITION\n", host->host_no);
1722         if (!c) {
1723             printk("scsi%d : CHECK CONDITION with no SCSI command\n",
1724                 host->host_no);
1725             return SPECIFIC_INT_PANIC;
1726         }
1727 
1728 /*
1729  * When a contingent allegiance condition is created, the target 
1730  * reverts to asynchronous transfers.
1731  */
1732 
1733         asynchronous (host, c->target);
1734 
1735         /* 
1736          * Use normal one-byte selection message, with no attempts to 
1737          * reestablish synchronous or wide messages since this may
1738          * be the crux of our problem.
1739          *
1740          * XXX - once SCSI-II tagged queuing is implemented, we'll
1741          *      have to set this up so that the rest of the DSA
1742          *      agrees with this being an untagged queue'd command.
1743          */
1744 
1745         patch_dsa_32 (cmd->dsa, dsa_msgout, 0, 1);
1746 
1747         /* 
1748          * Modify the table indirect for COMMAND OUT phase, since 
1749          * Request Sense is a six byte command.
1750          */
1751 
1752         patch_dsa_32 (cmd->dsa, dsa_cmdout, 0, 6);
1753 
1754         c->cmnd[0] = REQUEST_SENSE;
1755         c->cmnd[1] &= 0xe0;     /* Zero all but LUN */
1756         c->cmnd[2] = 0;
1757         c->cmnd[3] = 0;
1758         c->cmnd[4] = sizeof(c->sense_buffer);
1759         c->cmnd[5] = 0; 
1760 
1761         /*
1762          * Disable dataout phase, and program datain to transfer to the 
1763          * sense buffer, and add a jump to other_transfer after the 
1764          * command so overflow/underrun conditions are detected.
1765          */
1766 
1767         patch_dsa_32 (cmd->dsa, dsa_dataout, 0, hostdata->E_other_transfer);
1768         patch_dsa_32 (cmd->dsa, dsa_datain, 0, cmd->data_transfer_start);
1769         cmd->data_transfer_start[0] = (((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I | 
1770             DCMD_BMI_IO)) << 24) | sizeof(c->sense_buffer);
1771         cmd->data_transfer_start[1] = (unsigned long) c->sense_buffer;
1772 
1773         cmd->data_transfer_start[2] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) 
1774             << 24) | DBC_TCI_TRUE;
1775         cmd->data_transfer_start[3] = hostdata->E_other_transfer;
1776 
1777         /*
1778          * Currently, this command is flagged as completed, ie 
1779          * it has valid status and message data.  Reflag it as
1780          * incomplete.  Q - need to do something so that original
1781          * status, etc are used.
1782          */
1783 
1784         cmd->cmd->result = 0xffff;              
1785 
1786         /* 
1787          * Restart command as a REQUEST SENSE.
1788          */
1789         hostdata->dsp = hostdata->script + hostdata->E_select /
1790             sizeof(long);
1791         hostdata->dsp_changed = 1;
1792         return SPECIFIC_INT_NOTHING;
1793     case A_int_debug_break:
1794         return SPECIFIC_INT_BREAK;
1795     case A_int_norm_aborted:
1796         hostdata->dsp = hostdata->script + hostdata->E_schedule / 
1797                 sizeof(long);
1798         hostdata->dsp_changed = 1;
1799         if (cmd)
1800             abnormal_finished (cmd, DID_ERROR << 16);
1801         return SPECIFIC_INT_NOTHING;
1802     case A_int_test_1:
1803     case A_int_test_2:
1804         hostdata->idle = 1;
1805         hostdata->test_completed = (dsps - A_int_test_1) / 0x00010000 + 1;
1806         if (hostdata->options & OPTION_DEBUG_INTR)
1807             printk("scsi%d : test%ld complete\n", host->host_no,
1808                 hostdata->test_completed);
1809         return SPECIFIC_INT_NOTHING;
1810 #ifdef A_int_debug_scheduled
1811     case A_int_debug_scheduled:
1812         if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
1813             printk("scsi%d : new I/O 0x%lx scheduled\n", host->host_no,
1814                 NCR53c7x0_read32(DSA_REG));
1815         }
1816         return SPECIFIC_INT_RESTART;
1817 #endif
1818 #ifdef A_int_debug_idle
1819     case A_int_debug_idle:
1820         if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
1821             printk("scsi%d : idle\n", host->host_no);
1822         }
1823         return SPECIFIC_INT_RESTART;
1824 #endif
1825 #ifdef A_int_debug_cmd
1826     case A_int_debug_cmd:
1827         if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
1828             printk("scsi%d : command sent\n");
1829         }
1830     return SPECIFIC_INT_RESTART;
1831 #endif
1832 #ifdef A_int_debug_dsa_loaded
1833     case A_int_debug_dsa_loaded:
1834         if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
1835             printk("scsi%d : DSA loaded with 0x%lx\n", host->host_no,
1836                 NCR53c7x0_read32(DSA_REG));
1837         }
1838         return SPECIFIC_INT_RESTART; 
1839 #endif
1840 #ifdef A_int_debug_reselected
1841     case A_int_debug_reselected:
1842         if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
1843             printk("scsi%d : reselected by target %d lun %d\n",
1844                 host->host_no, (int) NCR53c7x0_read8(SSID_REG_800), 
1845                 (int) hostdata->reselected_identify & 7);
1846         }
1847     return SPECIFIC_INT_RESTART;
1848 #endif
1849 #ifdef A_int_debug_head
1850     case A_int_debug_head:
1851         if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
1852             printk("scsi%d : issue_dsa_head now 0x%lx\n",
1853                 host->host_no, (unsigned long) hostdata->issue_dsa_head);
1854         }
1855     return SPECIFIC_INT_RESTART;
1856 #endif
1857     default:
1858         if ((dsps & 0xff000000) == 0x03000000) {
1859              printk ("scsi%d : misc debug interrupt 0x%lx\n",
1860                 host->host_no, dsps);
1861             return SPECIFIC_INT_RESTART;
1862         }
1863 
1864         printk ("scsi%d : unknown user interrupt 0x%x\n", 
1865             host->host_no, (unsigned) dsps);
1866         return SPECIFIC_INT_PANIC;
1867     }
1868 }
1869 
1870 /* 
1871  * XXX - the stock NCR assembler won't output the scriptu.h file,
1872  * which undefine's all #define'd CPP symbols from the script.h
1873  * file, which will create problems if you use multiple scripts
1874  * with the same  symbol names.
1875  *
1876  * If you insist on using NCR's assembler, you could generate
1877  * scriptu.h from script.h using something like 
1878  *
1879  * grep #define script.h | \
1880  * sed 's/#define[      ][      ]*\([_a-zA-Z][_a-zA-Z0-9]*\).*$/#undefine \1/' \
1881  * > scriptu.h
1882  */
1883 
1884 #include "53c8xx_u.h"
1885 
1886 /* XXX - add alternate script handling code here */
1887 
1888 
1889 #ifdef NCR_DEBUG
1890 /*
1891  * Debugging without a debugger is no fun. So, I've provided 
1892  * a debugging interface in the NCR53c7x0 driver.  To avoid
1893  * kernel cruft, there's just enough here to act as an interface
1894  * to a user level debugger (aka, GDB).
1895  *
1896  *
1897  * The following restrictions apply to debugger commands : 
1898  * 1.  The command must be terminated by a newline.
1899  * 2.  Command length must be less than 80 bytes including the 
1900  *      newline.
1901  * 3.  The entire command must be written with one system call.
1902  */
1903 
1904 static const char debugger_help = 
1905 "bc <addr>                      - clear breakpoint\n"
1906 "bl                             - list breakpoints\n"
1907 "bs <addr>                      - set breakpoint\n"
1908 "g                              - start\n"                              
1909 "h                              - halt\n"
1910 "?                              - this message\n"
1911 "i                              - info\n"
1912 "mp <addr> <size>               - print memory\n"
1913 "ms <addr> <size> <value>       - store memory\n"
1914 "rp <num> <size>                - print register\n"
1915 "rs <num> <size> <value>        - store register\n"
1916 "s                              - single step\n"        
1917 "tb                             - begin trace \n"
1918 "te                             - end trace\n";
1919 
1920 /*
1921  * Whenever we change a break point, we should probably 
1922  * set the NCR up so that it is in a single step mode.
1923  */
1924 
1925 static int debugger_fn_bc (struct Scsi_Host *host, struct debugger_token *token,
     /* [previous][next][first][last][top][bottom][index][help] */
1926     unsigned long args[]) {
1927     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1928         instance->hostdata;
1929     struct NCR53c7x0_break *bp, **prev;
1930     int old_level;
1931     old_level = splx(0);
1932     for (bp = (struct NCR53c7x0_break *) instance->breakpoints,
1933             prev = (struct NCR53c7x0_break **) &instance->breakpoints;
1934             bp; prev = (struct NCR53c7x0_break **) &(bp->next),
1935             bp = (struct NCR53c7x0_break *) bp->next);
1936 
1937     if (!bp) {
1938         splx(old_level);
1939         return -EIO;
1940     }
1941 
1942     /* 
1943      * XXX - we need to insure that the processor is halted 
1944      * here in order to prevent a race condition.
1945      */
1946     
1947     memcpy ((void *) bp->addr, (void *) bp->old, sizeof(bp->old));
1948     if (prev)
1949         *prev = bp->next;
1950 
1951     splx(old_level);
1952     return 0;
1953 }
1954 
1955 
1956 static int debugger_fn_bl (struct Scsi_Host *host, struct debugger_token *token,
     /* [previous][next][first][last][top][bottom][index][help] */
1957     unsigned long args[]) {
1958     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1959         host->hostdata;
1960     struct NCR53c7x0_break *bp;
1961     char buf[80];
1962     size_t len;
1963     int old_level;
1964     /* 
1965      * XXX - we need to insure that the processor is halted 
1966      * here in order to prevent a race condition.  So, if the 
1967      * processor isn't halted, print an error message and continue.
1968      */
1969 
1970     sprintf (buf, "scsi%d : bp : warning : processor not halted\b",
1971         host->host_no);
1972     debugger_kernel_write (host, buf, strlen(buf));
1973 
1974     old_level=splx(0);
1975     for (bp = (struct NCR53c7x0_break *) host->breakpoints;
1976             bp; bp = (struct NCR53c7x0_break *) bp->next); {
1977             sprintf (buf, "scsi%d : bp : success : at %08x, replaces %08x %08x",
1978                 bp->addr, bp->old[0], bp->old[1]);
1979             len = strlen(buf);
1980             if ((bp->old[0] & (DCMD_TYPE_MASK << 24)) ==
1981                 (DCMD_TYPE_MMI << 24)) {
1982                 sprintf(buf + len, "%08x\n", * (long *) bp->addr);
1983             } else {
1984                 sprintf(buf + len, "\n");
1985             }
1986             len = strlen(buf);
1987             debugger_kernel_write (host, buf, len);
1988     }
1989     splx(old_level);
1990     return 0;
1991 }
1992 
1993 static int debugger_fn_bs (struct Scsi_Host *host, struct debugger_token *token,
     /* [previous][next][first][last][top][bottom][index][help] */
1994     unsigned long args[]) {
1995     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1996         host->hostdata;
1997     struct NCR53c7x0_break *bp;
1998     char buf[80];
1999     size_t len;
2000     int old_level;
2001     old_level=splx(0);
2002 
2003     if (hostdata->state != STATE_HALTED) {
2004         sprintf (buf, "scsi%d : bs : failure : NCR not halted\n", host->host_no);
2005         debugger_kernel_write (host, buf, strlen(buf));
2006         splx(old_level);
2007         return -1;
2008     }
2009 
2010     if (!(bp = kmalloc (sizeof (struct NCR53c7x0_break)))) {
2011         printk ("scsi%d : kmalloc(%d) of breakpoint structure failed, try again\n",
2012             host->host_no, sizeof(struct NCR53c7x0_break));
2013         splx(old_level);
2014         return -1;
2015     }
2016 
2017     bp->address = (unsigned long *) args[0];
2018     memcpy ((void *) bp->old_instruction, (void *) bp->address, 8);
2019     bp->old_size = (((bp->old_instruction[0] >> 24) & DCMD_TYPE_MASK) ==
2020         DCMD_TYPE_MMI ? 3 : 2;
2021     bp->next = hostdata->breakpoints;
2022     hostdata->breakpoints = bp->next;
2023     memcpy ((void *) bp->address, (void *) hostdata->E_debug_break, 8);
2024     
2025     splx(old_level);
2026     return 0;
2027 }
2028 
2029 #define TOKEN(name,nargs) {#name, nargs, debugger_fn_##name}
2030 static const struct debugger_token {
2031     char *name;
2032     int numargs;
2033     int (*fn)(struct debugger_token *token, unsigned long args[]);
2034 } debugger_tokens[] = {
2035     TOKEN(bc,1), TOKEN(bl,0), TOKEN(bs,1), TOKEN(g,0), TOKEN(halt,0),
2036     {DT_help, "?", 0} , TOKEN(h,0), TOKEN(i,0), TOKEN(mp,2), 
2037     TOKEN(ms,3), TOKEN(rp,2), TOKEN(rs,2), TOKEN(s,0), TOKEN(tb,0), TOKEN(te,0)
2038 };
2039 
2040 #define NDT sizeof(debugger_tokens / sizeof(struct debugger_token))
2041 
2042 static struct Scsi_Host * inode_to_host (struct inode *inode) {$
2043     int dev;
2044     struct Scsi_Host *tmp;
2045     for (dev = MINOR(inode->rdev), host = first_host;
2046         (host->hostt == the_template); --dev, host = host->next)
2047         if (!dev) return host;
2048     return NULL;
2049 }
2050 
2051 
2052 static debugger_user_write (struct inode *inode,struct file *filp,
     /* [previous][next][first][last][top][bottom][index][help] */
2053     char *buf,int count) {
2054     struct Scsi_Host *host;                     /* This SCSI host */
2055     struct NCR53c7x0_hostadata *hostdata;       
2056     char input_buf[80],                         /* Kernel space copy of buf */
2057         *ptr;                                   /* Pointer to argument list */
2058     unsigned long args[3];                      /* Arguments */
2059     int i, j, error, len;
2060 
2061     if (!(host = inode_to_host(inode)))
2062         return -ENXIO;
2063 
2064     hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
2065 
2066     if (error = verify_area(VERIFY_READ,buf,count))
2067         return error;
2068 
2069     if (count > 80) 
2070         return -EIO;
2071 
2072     memcpy_from_fs(input_buf, buf, count);
2073 
2074     if (input_buf[count - 1] != '\n')
2075         return -EIO;
2076 
2077     input_buf[count - 1]=0;
2078 
2079     for (i = 0; i < NDT; ++i) {
2080         len = strlen (debugger_tokens[i].name);
2081         if (!strncmp(input_buf, debugger_tokens[i].name, len)) 
2082             break;
2083     };
2084 
2085     if (i == NDT) 
2086         return -EIO;
2087 
2088     for (ptr = input_buf + len, j = 0; j < debugger_tokens[i].nargs && *ptr;) {
2089         if (*ptr == ' ' || *ptr == '\t') {
2090             ++ptr; 
2091         } else if (isdigit(*ptr)) {
2092             args[j++] = simple_strtoul (ptr, &ptr, 0);
2093         } else {
2094             return -EIO;
2095         } 
2096     }
2097 
2098     if (j != debugger_tokens[i].nargs)
2099         return -EIO;
2100 
2101     return count;
2102 } 
2103 
2104 static debugger_user_read (struct inode *inode,struct file *filp,
     /* [previous][next][first][last][top][bottom][index][help] */
2105     char *buf,int count) {
2106     struct Scsi_Host *instance;
2107     
2108 }
2109 
2110 static debugger_kernel_write (struct Scsi_Host *host, char *buf, size_t
     /* [previous][next][first][last][top][bottom][index][help] */
2111     buflen) {
2112     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2113         host->hostdata;
2114     int copy, left, old_level;
2115     
2116     old_level = splx(0);
2117     while (buflen) {
2118         left = (hostdata->debug_buf + hostdata->debug_size - 1) -
2119             hostdata->debug_write;
2120         copy = (buflen <= left) ? buflen : left;
2121         memcpy (hostdata->debug_write, buf, copy);
2122         buf += copy;
2123         buflen -= copy;
2124         hostdata->debug_count += copy;
2125         if ((hostdata->debug_write += copy) == 
2126             (hostdata->debug_buf + hostdata->debug_size))
2127             hosdata->debug_write = hostdata->debug_buf;
2128     }
2129     (void) splx(old_level);
2130 }
2131 
2132 #endif /* def NCRDEBUG */
2133 
2134 /* 
2135  * Function : static void NCR538xx_soft_reset (struct Scsi_Host *host)
2136  *
2137  * Purpose :  perform a soft reset of the NCR53c8xx chip
2138  *
2139  * Inputs : host - pointer to this host adapter's structure
2140  *
2141  * Preconditions : NCR53c7x0_init must have been called for this 
2142  *      host.
2143  * 
2144  */
2145 
2146 static void NCR53c8x0_soft_reset (struct Scsi_Host *host) {
     /* [previous][next][first][last][top][bottom][index][help] */
2147     NCR53c7x0_local_declare();
2148     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2149         host->hostdata;
2150     NCR53c7x0_local_setup(host);
2151 
2152 
2153     /*
2154      * Do a soft reset of the chip so that everything is 
2155      * reinitialized to the power-on state.
2156      *
2157      * Basically follow the procedure outlined in the NCR53c700
2158      * data manual under Chapter Six, How to Use, Steps Necessary to
2159      * Start SCRIPTS, with the exception of actually starting the 
2160      * script and setting up the synchronous transfer gunk.
2161      */
2162 
2163     NCR53c7x0_write8(ISTAT_REG_800, ISTAT_10_SRST);
2164     NCR53c7x0_write8(ISTAT_REG_800, 0);
2165     NCR53c7x0_write8(hostdata->dmode, hostdata->saved_dmode & ~DMODE_MAN);
2166 
2167 
2168     /* 
2169      * Respond to selection and reselection by targets and 
2170      * use our _initiator_ SCSI ID for arbitration. 
2171      *
2172      * XXX - Note : we must reprogram this when reselecting as 
2173      *  a target.
2174      */
2175 
2176     NCR53c7x0_write8(SCID_REG, (host->this_id & 7)|SCID_800_RRE|SCID_800_SRE);
2177     NCR53c7x0_write8(RESPID_REG_800, hostdata->this_id_mask);
2178 
2179     /*
2180      * Use a maximum (1.6) second handshake to handshake timeout,
2181      * and SCSI recommended .5s selection timeout.
2182      */
2183 
2184     NCR53c7x0_write8(STIME0_REG_800, 
2185         ((14 << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK) 
2186 /* Disable HTH interrupt */
2187 #if 0
2188         | ((15 << STIME0_800_HTH_SHIFT) & STIME0_800_HTH_MASK)
2189 #endif
2190     );
2191 
2192 
2193 
2194     /*
2195      * Enable all interrupts, except parity which we only want when
2196      * the user requests it.
2197      */
2198 
2199     NCR53c7x0_write8(DIEN_REG, DIEN_800_MDPE | DIEN_800_BF |
2200                 DIEN_ABRT | DIEN_SSI | DIEN_SIR | DIEN_800_IID);
2201 
2202     
2203     NCR53c7x0_write8(SIEN0_REG_800, ((hostdata->options & OPTION_PARITY) ?
2204             SIEN_PAR : 0) | SIEN_RST | SIEN_UDC | SIEN_SGE | SIEN_800_SEL |
2205             SIEN_800_RESEL | SIEN_MA);
2206     NCR53c7x0_write8(SIEN1_REG_800, SIEN1_800_STO | SIEN1_800_HTH);
2207 
2208     /* 
2209      * Use saved clock frequency divisor and scripts loaded in 16 bit
2210      * mode flags from the saved dcntl.
2211      */
2212 
2213     NCR53c7x0_write8(DCNTL_REG, hostdata->saved_dcntl);
2214     NCR53c7x0_write8(CTEST4_REG_800, hostdata->saved_ctest4);
2215 
2216     /* Enable active negation */
2217     NCR53c7x0_write8(STEST3_REG_800, STEST3_800_TE);
2218 
2219     
2220 }
2221 
2222 /*
2223  * Function static struct NCR53c7x0_cmd *create_cmd (Scsi_Cmnd *cmd) 
2224  *
2225  * Purpose : Using scsi_malloc() if the system is initialized,
2226  *      scan_scsis_buf if not, allocate space to store the variable
2227  *      length NCR53c7x0_cmd structure.  Initialize it based on 
2228  *      the Scsi_Cmnd structure passed in, including dsa and 
2229  *      Linux field initialization, and dsa code relocation.
2230  *
2231  * Inputs : cmd - SCSI command
2232  *
2233  * Returns : NCR53c7x0_cmd structure corresponding to cmd,
2234  *      NULL on failure.
2235  */
2236 
2237 static struct NCR53c7x0_cmd *create_cmd (Scsi_Cmnd *cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
2238     NCR53c7x0_local_declare();
2239     struct Scsi_Host *host = cmd->host;
2240     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2241         host->hostdata; 
2242     int size;                   /* Size of *tmp */
2243     struct NCR53c7x0_cmd *tmp;  /* NCR53c7x0_cmd structure for this command */
2244     int datain,                 /* Number of instructions per phase */
2245         dataout;
2246     int data_transfer_instructions, /* Count of dynamic instructions */
2247         i,                      /* Counter */
2248         alignment;              /* Alignment adjustment (0 - 4) */
2249     unsigned long *cmd_datain,  /* Address of datain/dataout code */
2250         *cmd_dataout;           /* Incremented as we assemble */
2251     void *real;                 /* Real address */
2252     NCR53c7x0_local_setup(cmd->host);
2253 
2254 
2255     /*
2256      * Decide weather we need to generate commands for DATA IN,
2257      * DATA OUT, neither, or both based on the SCSI command 
2258      */
2259 
2260     switch (cmd->cmnd[0]) {
2261     /* These commands do DATA IN */
2262     case INQUIRY:
2263     case MODE_SENSE:
2264     case READ_6:
2265     case READ_10:
2266     case READ_CAPACITY:
2267     case REQUEST_SENSE:
2268         datain = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
2269         dataout = 0;
2270         break;
2271     /* These commands do DATA OUT */
2272     case MODE_SELECT: 
2273     case WRITE_6:
2274     case WRITE_10:
2275 #if 0
2276         printk("scsi%d : command is ", host->host_no);
2277         print_command(cmd->cmnd);
2278 #endif
2279 #if 0
2280         printk ("scsi%d : %d scatter/gather segments\n", host->host_no,
2281             cmd->use_sg);
2282 #endif
2283         datain = 0;
2284         dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
2285 #if 0
2286         hostdata->options |= OPTION_DEBUG_INTR;
2287 #endif
2288         break;
2289     /* 
2290      * These commands do no data transfer, we should force an
2291      * interrupt if a data phase is attempted on them.
2292      */
2293     case START_STOP:
2294     case TEST_UNIT_READY:
2295         datain = dataout = 0;
2296         break;
2297     /*
2298      * We don't know about these commands, so generate code to handle
2299      * both DATA IN and DATA OUT phases.
2300      */
2301     default:
2302         datain = dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
2303     }
2304 
2305     /*
2306      * Allocate memory for the NCR53c7x0_cmd structure.  
2307      */
2308 
2309     /* 
2310      * For each data phase implemented, we need a JUMP instruction
2311      * to return control to other_transfer.  We also need a MOVE
2312      * and a CALL instruction for each scatter/gather segment.
2313      */
2314 
2315     data_transfer_instructions = datain + dataout;
2316 
2317     /*
2318      * When we perform a request sense, we overwrite various things,
2319      * including the data transfer code.  Make sure we have enough
2320      * space to do that.
2321      */
2322 
2323     if (data_transfer_instructions < 2)
2324         data_transfer_instructions = 2;
2325 
2326     /* 
2327      * We need enough space to store the base NCR53c7x0 structure,
2328      * DSA, and data transfer instructions at 2 long words each,
2329      * as well as padding out to the next 512 bytes for scsi_malloc.
2330      *
2331      * We also need to guarantee alignment of _4_ bytes. 
2332      */
2333 
2334 #ifdef SCSI_MALLOC
2335     size = ((sizeof (struct NCR53c7x0_cmd) + (hostdata->dsa_end - 
2336         hostdata->dsa_start) + 2 * sizeof(long) * 
2337         data_transfer_instructions + 4 + 511) / 512) * 512;             
2338 #else
2339     size = sizeof (struct NCR53c7x0_cmd) + (hostdata->dsa_end - 
2340         hostdata->dsa_start) + 2 * sizeof(long) * 
2341         data_transfer_instructions + 4;
2342 #endif
2343 
2344 
2345 #if 0
2346     if (size > 512) {
2347         printk("scsi%d : size = %d\n", host->host_no, size);
2348     }
2349 #endif
2350 
2351 #ifdef SCSI_MALLOC
2352     real = in_scan_scsis ? NULL : scsi_malloc (size);
2353 #else
2354     real = kmalloc (size, GFP_ATOMIC);
2355 #endif
2356 
2357     if (!real) {
2358         if (!scan_scsis_buf_busy && size <= sizeof(scan_scsis_buf)) {
2359             scan_scsis_buf_busy = 1;
2360             real = scan_scsis_buf;
2361         } else {
2362             panic ("scsi%d : scan_scsis_buf too small (need %d bytes)\n",
2363                 host->host_no, size);
2364         }
2365     }
2366 
2367     alignment = 4 - (((unsigned) real) & 3);
2368 
2369     tmp = (struct NCR53c7x0_cmd *) (((char *) real) + alignment);
2370 
2371     tmp->real = real;
2372 
2373 
2374     if (((unsigned long) tmp->dsa) & 0x3) 
2375         panic ("scsi%d : pid %d dsa structure not dword aligned!\n",
2376             host->host_no, cmd->pid);
2377 
2378     /*
2379      * Initialize Linux specific fields.
2380      */
2381 
2382     tmp->size = size;                   
2383     tmp->cmd = cmd;
2384     tmp->next = NULL;
2385     tmp->prev = NULL;
2386 
2387     /* 
2388      * Calculate addresses of dynamic code to fill in DSA
2389      */
2390 
2391     tmp->data_transfer_start = tmp->dsa + (hostdata->dsa_end - 
2392         hostdata->dsa_start) / sizeof(long);
2393     tmp->data_transfer_end = tmp->data_transfer_start + 
2394         2 * data_transfer_instructions;
2395 
2396     cmd_datain = datain ? tmp->data_transfer_start : NULL;
2397     cmd_dataout = dataout ? (datain ? cmd_datain + 2 * datain : tmp->
2398         data_transfer_start) : NULL;
2399 
2400     /*
2401      * Fill in the NCR53c7x0_cmd structure as follows
2402      * dsa, with fixed up DSA code
2403      * datain code
2404      * dataout code
2405      */
2406 
2407     /* Copy template code into dsa and perform all necessary fixups */
2408     if (hostdata->dsa_fixup)
2409         hostdata->dsa_fixup(tmp);
2410 
2411     patch_dsa_32(tmp->dsa, dsa_next, 0, NULL);
2412     patch_dsa_32(tmp->dsa, dsa_cmnd, 0, cmd);
2413     patch_dsa_32(tmp->dsa, dsa_select, 0, hostdata->sync[cmd->target].
2414         select_indirect);
2415     /*
2416      * XXX - we need to figure this size based on weather
2417      * or not we'll be using any additional messages.
2418      */
2419     patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1);
2420 #if 0
2421     tmp->select[0] = IDENTIFY (1, cmd->lun);
2422 #else
2423     tmp->select[0] = IDENTIFY (0, cmd->lun);
2424 #endif
2425     patch_dsa_32(tmp->dsa, dsa_msgout, 1, tmp->select);
2426     patch_dsa_32(tmp->dsa, dsa_cmdout, 0, COMMAND_SIZE(cmd->cmnd[0]));
2427     patch_dsa_32(tmp->dsa, dsa_cmdout, 1, cmd->cmnd);
2428     patch_dsa_32(tmp->dsa, dsa_dataout, 0, cmd_dataout ? 
2429         cmd_dataout : hostdata->script + hostdata->E_other_transfer / 
2430             sizeof (long));
2431     patch_dsa_32(tmp->dsa, dsa_datain, 0, cmd_datain ? 
2432         cmd_datain : hostdata->script + hostdata->E_other_transfer / 
2433             sizeof (long));
2434     /* 
2435      * XXX - need to make endian aware, should use separate variables
2436      * for both status and message bytes.
2437      */
2438     patch_dsa_32(tmp->dsa, dsa_msgin, 0, 1);
2439     patch_dsa_32(tmp->dsa, dsa_msgin, 1, (((unsigned long) &cmd->result) + 1));
2440     patch_dsa_32(tmp->dsa, dsa_status, 0, 1);
2441     patch_dsa_32(tmp->dsa, dsa_status, 1, &cmd->result);
2442     patch_dsa_32(tmp->dsa, dsa_msgout_other, 0, 1);
2443     patch_dsa_32(tmp->dsa, dsa_msgout_other, 1, 
2444         &NCR53c7xx_msg_nop);
2445 
2446     
2447     /*
2448      * Generate code for zero or more of the DATA IN, DATA OUT phases 
2449      * in the format 
2450      *
2451      * MOVE first buffer length, first buffer address, WHEN phase
2452      * CALL msgin, WHEN MSG_IN 
2453      * ...
2454      * MOVE last buffer length, last buffer address, WHEN phase
2455      * JUMP other_transfer
2456      */
2457 
2458 /* See if we're getting to data transfer */
2459 #if 0
2460     if (datain) {
2461         cmd_datain[0] = 0x98080000;
2462         cmd_datain[1] = 0x03ffd00d;
2463         cmd_datain += 2;
2464     }
2465 #endif
2466 
2467 /* 
2468  * XXX - I'm undecided weather all of this nonsense is faster
2469  * in the long run, or weather I should just go and implement a loop
2470  * on the NCR chip using table indirect mode?
2471  *
2472  * In any case, this is how it _must_ be done for 53c700/700-66 chips,
2473  * so this stays even when we come up with something better.
2474  *
2475  * When we're limited to 1 simultaneous command, no overlapping processing,
2476  * we're seeing 630K/sec, with 7% CPU usage on a slow Syquest 45M
2477  * drive.
2478  *
2479  * Not bad, not good. We'll see.
2480  */
2481 
2482     for (i = 0; cmd->use_sg ? (i < cmd->use_sg) : !i; cmd_datain += 4, 
2483         cmd_dataout += 4, ++i) {
2484         unsigned long buf = (unsigned long) (cmd->use_sg ? 
2485             ((struct scatterlist *)cmd->buffer)[i].address :
2486             cmd->request_buffer);
2487         unsigned long count = (unsigned long) (cmd->use_sg ?
2488             ((struct scatterlist *)cmd->buffer)[i].length :
2489             cmd->request_bufflen);
2490 
2491         if (datain) {
2492             cmd_datain[0] = ((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I | DCMD_BMI_IO) 
2493                 << 24) | count;
2494             cmd_datain[1] = buf;
2495             cmd_datain[2] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL | 
2496                 DCMD_TCI_CD | DCMD_TCI_IO | DCMD_TCI_MSG) << 24) | 
2497                 DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE | DBC_TCI_TRUE;
2498             cmd_datain[3] = hostdata->script + hostdata->E_msg_in / 
2499                 sizeof(long);
2500 #if 0
2501             print_insn (host, cmd_datain, "dynamic ", 1);
2502             print_insn (host, cmd_datain + 2, "dynamic ", 1);
2503 #endif
2504         }
2505         if (dataout) {
2506             cmd_dataout[0] = ((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I) << 24) 
2507                 | count;
2508             cmd_dataout[1] = buf;
2509             cmd_dataout[2] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL | 
2510                 DCMD_TCI_CD | DCMD_TCI_IO | DCMD_TCI_MSG) << 24) | 
2511                 DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE | DBC_TCI_TRUE;
2512             cmd_dataout[3] = hostdata->script + hostdata->E_msg_in /
2513                 sizeof(long);
2514 #if 0
2515             print_insn (host, cmd_dataout, "dynamic ", 1);
2516             print_insn (host, cmd_dataout + 2, "dynamic ", 1);
2517 #endif
2518         }
2519     }
2520 
2521     /*
2522      * Install JUMP instructions after the data transfer routines to return
2523      * control to the do_other_transfer routines.
2524      */
2525   
2526     
2527     if (datain) {
2528         cmd_datain[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
2529             DBC_TCI_TRUE;
2530         cmd_datain[1] = hostdata->script + hostdata->E_other_transfer
2531             / sizeof(long);
2532 #if 0
2533         print_insn (host, cmd_datain, "dynamic jump ", 1);
2534 #endif
2535         cmd_datain += 2; 
2536     }
2537 #if 0
2538     if (datain) {
2539         cmd_datain[0] = 0x98080000;
2540         cmd_datain[1] = 0x03ffdeed;
2541         cmd_datain += 2;
2542     }
2543 #endif
2544 
2545 
2546     if (dataout) {
2547         cmd_dataout[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
2548             DBC_TCI_TRUE;
2549         cmd_dataout[1] = hostdata->script + hostdata->E_other_transfer
2550             / sizeof(long);
2551 #if 0
2552         print_insn (host, cmd_dataout, "dynamic jump ", 1);
2553 #endif
2554         cmd_dataout += 2;
2555     }
2556 
2557 
2558     return tmp;
2559 }
2560     
2561 /* 
2562  * Function : int NCR53c7xx_queue_command (Scsi_Cmnd *cmd,
2563  *      void (*done)(Scsi_Cmnd *)) 
2564  *
2565  * Purpose :  enqueues a SCSI command
2566  *
2567  * Inputs : cmd - SCSI command, done - function called on completion, with
2568  *      a pointer to the command descriptor.
2569  * 
2570  * Returns : 0
2571  *
2572  * Side effects : 
2573  *      cmd is added to the per instance issue_queue, with minor 
2574  *      twiddling done to the host specific fields of cmd.  If the 
2575  *      main coroutine is not running, it is restarted.
2576  *
2577  */
2578 
2579 int NCR53c7xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *)) {
     /* [previous][next][first][last][top][bottom][index][help] */
2580     NCR53c7x0_local_declare();
2581     struct NCR53c7x0_cmd *tmp;
2582     struct Scsi_Host *host = cmd->host;
2583     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2584         host->hostdata;
2585     int old_level;
2586     unsigned char target_was_busy;
2587     NCR53c7x0_local_setup(host);
2588     
2589     if (((hostdata->options & (OPTION_DEBUG_INIT_ONLY|OPTION_DEBUG_PROBE_ONLY)) ||
2590         ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
2591         !(hostdata->debug_lun_limit[cmd->target] & (1 << cmd->lun)))) ||
2592         cmd->target > 7) {
2593         printk("scsi%d : disabled target %d lun %d\n", host->host_no,
2594             cmd->target, cmd->lun);
2595         cmd->result = (DID_BAD_TARGET << 16);
2596         done(cmd);
2597         return 0;
2598     }
2599 
2600     if (hostdata->options & OPTION_DEBUG_NCOMMANDS_LIMIT) {
2601         if (hostdata->debug_count_limit == 0) {
2602             printk("scsi%d : maximum commands exceeded\n", host->host_no);
2603             cmd->result = (DID_BAD_TARGET << 16);
2604             done(cmd);
2605             return 0;
2606         } else if (hostdata->debug_count_limit != -1) 
2607             --hostdata->debug_count_limit;
2608     }
2609    
2610     if (hostdata->options & OPTION_DEBUG_READ_ONLY) {
2611         switch (cmd->cmnd[0]) {
2612         case WRITE_6:
2613         case WRITE_10:
2614             printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
2615                 host->host_no);
2616             cmd->result = (DID_BAD_TARGET << 16);
2617             done(cmd);
2618             return 0;
2619         }
2620     }
2621 
2622     cmd->scsi_done = done;
2623     cmd->result = 0xffff;               /* The NCR will overwrite message
2624                                            and status with valid data */
2625     
2626     cmd->host_scribble = (unsigned char *) tmp = create_cmd (cmd);
2627 
2628     /*
2629      * On NCR53c710 and better chips, we have two issue queues : 
2630      * The queue maintained by the Linux driver, and the queue 
2631      * maintained by the NCR chip.
2632      * 
2633      * The Linux queue includes commands which have been generated,
2634      * but may be unable to execute because the device is busy, 
2635      * where as the NCR queue contains commands to issue as soon
2636      * as BUS FREE is detected.
2637      *
2638      * NCR53c700 and NCR53c700-66 chips use only the Linux driver
2639      * queue. 
2640      * 
2641      * So, insert into the Linux queue if the device is busy or 
2642      * we are running on an old chip, otherwise insert directly into
2643      * the NCR queue.
2644      */
2645     
2646     /*
2647      * REQUEST sense commands need to be executed before all other 
2648      * commands since any command will clear the contingent allegiance 
2649      * condition that exists and the sense data is only guaranteed to be 
2650      * valid while the condition exists.
2651      */
2652 
2653     old_level = splx(0);
2654 
2655     /* 
2656      * Consider a target busy if there are _any_ commands running
2657      * on it.  
2658      * XXX - Once we do SCSI-II tagged queuing, we want to use 
2659      *     a different definition of busy.
2660      */
2661         
2662     target_was_busy = hostdata->busy[cmd->target][cmd->lun]
2663 #ifdef LUN_BUSY
2664         ++
2665 #endif
2666 ; 
2667 
2668     if (!(hostdata->options & OPTION_700)  &&
2669         !target_was_busy) {
2670         unsigned char *dsa = ((unsigned char *) tmp->dsa)
2671                 - hostdata->dsa_start;  
2672                 /* dsa start is negative, so subtraction is used */
2673 #if 0   
2674         printk("scsi%d : new dsa is 0x%x\n", host->host_no, (unsigned) dsa);
2675 #endif
2676 
2677         if (hostdata->running_list)
2678             hostdata->running_list->prev = tmp;
2679 
2680         tmp->next = hostdata->running_list;
2681 
2682         if (!hostdata->running_list)
2683             hostdata->running_list = tmp;
2684         
2685 
2686         if (hostdata->idle) {
2687             hostdata->idle = 0;
2688             hostdata->state = STATE_RUNNING;
2689             NCR53c7x0_write32 (DSP_REG,  ((unsigned long) hostdata->script) +
2690                 hostdata->E_schedule);
2691         }
2692 
2693 /* XXX - make function */
2694         for (;;) {
2695             /* 
2696              * If the NCR doesn't have any commands waiting in its
2697              * issue queue, then we simply create a new issue queue,
2698              * and signal the NCR that we have more commands.
2699              */
2700                 
2701             if (!hostdata->issue_dsa_head) {
2702 #if 0
2703                 printk ("scsi%d : no issue queue\n", host->host_no);
2704 #endif
2705                 hostdata->issue_dsa_tail = hostdata->issue_dsa_head = dsa;
2706                 NCR53c7x0_write8(hostdata->istat, 
2707                     NCR53c7x0_read8(hostdata->istat) | ISTAT_10_SIGP);
2708                 break;
2709             /*
2710              * Otherwise, we blindly perform an atomic write 
2711              * to the next pointer of the last command we 
2712              * placed in that queue.
2713              *
2714              * Looks like it doesn't work, but I think it does - 
2715              */
2716             } else {
2717                 printk ("scsi%d : existing issue queue\n", host->host_no);
2718             /* XXX - Replace with XCHG or equivalent */
2719                 hostdata->issue_dsa_tail = *((unsigned char **) 
2720                     (hostdata->issue_dsa_tail + hostdata->dsa_next)) = dsa;
2721             /*
2722              * After which, one of two things will happen : 
2723              * The NCR will have scheduled a command, either this
2724              * one, or the next one.  In this case, we successfully
2725              * added our command to the queue.
2726              *
2727              * The NCR will have written the hostdata->issue_dsa_head
2728              * pointer with the NULL pointer terminating the list,
2729              * in which case we were too late.  If this happens,
2730              * we restart
2731              */
2732                 if (hostdata->issue_dsa_head)
2733                     break;
2734             }
2735         }
2736 /* XXX - end */
2737     } else {
2738 #if 1
2739         printk ("scsi%d : using issue_queue instead of issue_dsa_head!\n",
2740             host->host_no);
2741 #endif
2742         for (tmp = (struct NCR53c7x0_cmd *) hostdata->issue_queue; 
2743             tmp->next; tmp = (struct NCR53c7x0_cmd *) tmp->next);
2744         tmp->next = tmp;
2745     }
2746     splx(old_level);
2747     return 0;
2748 }
2749 
2750 
2751 int fix_pointers (unsigned long dsa) {
     /* [previous][next][first][last][top][bottom][index][help] */
2752     return 0;
2753 }
2754 
2755 /*
2756  * Function : static void intr_scsi (struct Scsi_Host *host, 
2757  *      struct NCR53c7x0_cmd *cmd)
2758  *
2759  * Purpose : handle all SCSI interrupts, indicated by the setting 
2760  *      of the SIP bit in the ISTAT register.
2761  *
2762  * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
2763  *      may be NULL.
2764  */
2765 
2766 static void intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
2767     NCR53c7x0_local_declare();
2768     struct NCR53c7x0_hostdata *hostdata = 
2769         (struct NCR53c7x0_hostdata *) host->hostdata;
2770     unsigned char sstat0_sist0, sist1,          /* Registers */
2771             fatal;                              /* Did a fatal interrupt 
2772                                                    occur ? */
2773     NCR53c7x0_local_setup(host);
2774 
2775     fatal = 0;
2776   
2777     if ((hostdata->chip / 100) == 8) {
2778         sstat0_sist0 = NCR53c7x0_read8(SIST0_REG_800);
2779         udelay(1);
2780         sist1 = NCR53c7x0_read8(SIST1_REG_800);
2781     } else {
2782         sstat0_sist0 = NCR53c7x0_read8(SSTAT0_REG);
2783         sist1 = 0;
2784     }
2785 
2786     if (hostdata->options & OPTION_DEBUG_INTR) 
2787         printk ("scsi%d : SIST0 0x%0x, SIST1 0x%0x\n", host->host_no,
2788             sstat0_sist0, sist1);
2789 
2790     /* 250ms selection timeout */
2791     if ((((hostdata->chip / 100) == 8) && (sist1 & SIST1_800_STO)) || 
2792         (((hostdata->chip / 100) != 8) && sstat0_sist0 && SSTAT0_700_STO)) {
2793         fatal = 1;
2794         if (hostdata->options & OPTION_DEBUG_INTR) {
2795             printk ("scsi%d : Selection Timeout\n", host->host_no);
2796             if (cmd) {
2797                 printk("scsi%d : target %d, lun %d, command ",
2798                     host->host_no, cmd->cmd->target, cmd->cmd->lun);
2799                 print_command (cmd->cmd->cmnd);
2800                 printk("scsi%d : dsp = 0x%x\n", host->host_no,
2801                     (unsigned) NCR53c7x0_read32(DSP_REG));
2802             } else {
2803                 printk("scsi%d : no command\n", host->host_no);
2804             }
2805         }
2806 /*
2807  * XXX - question : how do we want to handle the Illegal Instruction
2808  *      interrupt, which may occur before or after the Selection Timeout
2809  *      interrupt?
2810  */
2811 
2812         if (1) {
2813             hostdata->idle = 1;
2814             hostdata->expecting_sto = 0;
2815 
2816             if (hostdata->test_running) {
2817                 hostdata->test_running = 0;
2818                 hostdata->test_completed = 3;
2819             } else if (cmd) {
2820                 abnormal_finished(cmd, DID_BAD_TARGET << 16);
2821             }
2822 #if 0       
2823             hostdata->intrs = 0;
2824 #endif
2825         }
2826     } 
2827     
2828     if (sstat0_sist0 & SSTAT0_UDC) {
2829         fatal = 1;
2830         printk("scsi%d : target %d lun %d unexpected disconnect\n",
2831             host->host_no, cmd->cmd->target, cmd->cmd->lun);
2832         if (cmd) {
2833             abnormal_finished(cmd, DID_ERROR << 16);
2834         }
2835         hostdata->dsp = hostdata->script + hostdata->E_schedule / 
2836             sizeof(long);
2837         hostdata->dsp_changed = 1;
2838     /* SCSI PARITY error */
2839     } 
2840 
2841     if (sstat0_sist0 & SSTAT0_PAR) {
2842         fatal = 1;
2843         if (cmd && cmd->cmd) {
2844             printk("scsi%d : target %d lun %d parity error.\n",
2845                 host->host_no, cmd->cmd->target, cmd->cmd->lun);
2846             abnormal_finished (cmd, DID_PARITY << 16); 
2847         } else
2848             printk("scsi%d : parity error\n", host->host_no);
2849         /* Should send message out, parity error */
2850 
2851         /* XXX - Reduce synchronous transfer rate! */
2852         hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
2853             sizeof(long);
2854         hostdata->dsp_changed = 1; 
2855     /* SCSI GROSS error */
2856     } 
2857 
2858     if (sstat0_sist0 & SSTAT0_SGE) {
2859         fatal = 1;
2860         printk("scsi%d : gross error\n", host->host_no);
2861         /* XXX Reduce synchronous transfer rate! */
2862         hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
2863             sizeof(long);
2864         hostdata->dsp_changed = 1;
2865     /* Phase mismatch */
2866     } 
2867 
2868     if (sstat0_sist0 & SSTAT0_MA) {
2869         fatal = 1;
2870         if (hostdata->options & OPTION_DEBUG_INTR)
2871             printk ("scsi%d : SSTAT0_MA\n", host->host_no);
2872         intr_phase_mismatch (host, cmd);
2873     }
2874 
2875 #if 1
2876 /*
2877  * If a fatal SCSI interrupt occurs, we must insure that the DMA and
2878  * SCSI FIFOs were flushed.
2879  */
2880 
2881     if (fatal) {
2882         if (!hostdata->dstat_valid) {
2883             hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
2884             hostdata->dstat_valid = 1;
2885         }
2886 
2887 /* XXX - code check for 700/800 chips */
2888         if (!(hostdata->dstat & DSTAT_DFE)) {
2889             printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
2890 #if 0
2891             if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
2892                 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
2893                 while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
2894                     DSTAT_DFE));
2895             } else 
2896 #endif
2897             {
2898                 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
2899                 while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
2900             }
2901         }
2902 
2903         NCR53c7x0_write8 (STEST3_REG_800, STEST3_800_CSF);
2904         while (NCR53c7x0_read8 (STEST3_REG_800) & STEST3_800_CSF);
2905     }
2906 #endif
2907 }
2908 
2909 /*
2910  * Function : static void NCR53c7x0_intr (int irq)
2911  *
2912  * Purpose : handle NCR53c7x0 interrupts for all NCR devices sharing
2913  *      the same IRQ line.  
2914  * 
2915  * Inputs : Since we're using the SA_INTERRUPT interrupt handler
2916  *      semantics, irq indicates the interrupt which invoked 
2917  *      this handler.  
2918  */
2919 
2920 static void NCR53c7x0_intr (int irq) {
     /* [previous][next][first][last][top][bottom][index][help] */
2921     NCR53c7x0_local_declare();
2922     struct Scsi_Host *host;                     /* Host we are looking at */
2923     unsigned char istat;                        /* Values of interrupt regs */
2924     struct NCR53c7x0_hostdata *hostdata;        /* host->hostdata */
2925     struct NCR53c7x0_cmd *cmd,                  /* command which halted */
2926         **cmd_prev_ptr;
2927     unsigned long *dsa;                         /* DSA */
2928     int done = 1;                               /* Indicates when handler 
2929                                                    should terminate */
2930     int interrupted = 0;                        /* This HA generated 
2931                                                    an interrupt */
2932     int old_level;                              
2933 
2934 #ifdef NCR_DEBUG
2935     char buf[80];                               /* Debugging sprintf buffer */
2936     size_t buflen;                              /* Length of same */
2937 #endif
2938 
2939 #if 0
2940     printk("interrupt %d received\n", irq);
2941 #endif
2942 
2943     do {
2944         done = 1;
2945         for (host = first_host; host; host = hostdata->next ?
2946              hostdata->next : NULL) {
2947             NCR53c7x0_local_setup(host);
2948 
2949             hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
2950             hostdata->dsp_changed = 0;
2951             interrupted = 0;
2952 
2953 
2954             do {
2955                 hostdata->dstat_valid = 0;
2956                 interrupted = 0;
2957                 /*
2958                  * Only read istat once, since reading it again will unstack
2959                  * interrupts.
2960                  */
2961                 istat = NCR53c7x0_read8(hostdata->istat);
2962 
2963                 /*
2964                  * INTFLY interrupts are used by the NCR53c720, NCR53c810,
2965                  * and NCR53c820 to signify completion of a command.  Since 
2966                  * the SCSI processor continues running, we can't just look
2967                  * at the contents of the DSA register and continue running.
2968                  */
2969 /* XXX - this is getting big, and should move to intr_intfly() */
2970                 if ((hostdata->options & OPTION_INTFLY) && 
2971                     ((hostdata->chip / 100) == 8 && (istat & ISTAT_800_INTF))) {
2972                     char search_found = 0;      /* Got at least one ? */
2973                     done = 0;
2974                     interrupted = 1;
2975 
2976                     /* 
2977                      * Clear the INTF bit by writing a one.  This reset operation 
2978                      * is self-clearing.
2979                      */
2980                     NCR53c7x0_write8(hostdata->istat, istat|ISTAT_800_INTF);
2981 
2982                     if (hostdata->options & OPTION_DEBUG_INTR)
2983                         printk ("scsi%d : INTFLY\n", host->host_no); 
2984 
2985                     /*
2986                      * Traverse our list of running commands, and look
2987                      * for those with valid (non-0xff ff) status and message
2988                      * bytes encoded in the result which signify command
2989                      * completion.
2990                      */
2991 
2992 
2993                     old_level = splx(0);
2994 restart:
2995                     for (cmd_prev_ptr = (struct NCR53c7x0_cmd **) 
2996                         &(hostdata->running_list), cmd = (struct NCR53c7x0_cmd *)
2997                         hostdata->running_list; cmd ; cmd_prev_ptr = 
2998                         &(cmd->next), cmd = (struct NCR53c7x0_cmd *) cmd->next) {
2999                         Scsi_Cmnd *tmp;
3000 
3001                         if (!cmd) {
3002                             printk("scsi%d : very weird.\n", host->host_no);
3003                             break;
3004                         }
3005 
3006                         if (!(tmp = cmd->cmd)) {
3007                             printk("scsi%d : weird.  NCR53c7x0_cmd has no Scsi_Cmnd\n",
3008                                 host->host_no);
3009                                 continue;
3010                         }
3011 #if 0
3012                         printk ("scsi%d : looking at result of 0x%x\n",
3013                             host->host_no, cmd->cmd->result);
3014 #endif
3015                 
3016                         if (((tmp->result & 0xff) == 0xff) ||
3017                             ((tmp->result & 0xff00) == 0xff00))
3018                             continue;
3019 
3020                         search_found = 1;
3021 
3022                         /* Important - remove from list _before_ done is called */
3023                         /* XXX - SLL.  Seems like DLL is unnecessary */
3024                         if (cmd->prev)
3025                             cmd->prev->next = cmd->next;
3026                         if (cmd_prev_ptr)
3027                             *cmd_prev_ptr = cmd->next;
3028 
3029 #ifdef LUN_BUSY
3030                         /* Check for next command for target, add to issue queue */
3031                         if (--hostdata->busy[tmp->target][tmp->lun]) {
3032                         }
3033 #endif
3034 
3035 
3036                         if (!scan_scsis_buf_busy) {
3037 #ifdef SCSI_MALLOC
3038                             scsi_free ((void *) cmd->real, cmd->size);
3039 #else
3040                             kfree_s ((void *) cmd->real, cmd->size);
3041 #endif
3042                         } else {
3043                             scan_scsis_buf_busy = 0;
3044                         }
3045 
3046 
3047                         tmp->host_scribble = NULL;
3048 
3049                         if (hostdata->options & OPTION_DEBUG_INTR) {
3050                             printk ("scsi%d : command complete : pid %lu, id %d,lun %d result 0x%x ", 
3051                                 host->host_no, tmp->pid, tmp->target, tmp->lun, tmp->result);
3052                             print_command (tmp->cmnd);
3053                         }
3054 
3055                         
3056 #if 0
3057                         hostdata->options &= ~OPTION_DEBUG_INTR;
3058 #endif
3059                         tmp->scsi_done(tmp);
3060                         goto restart;
3061 
3062                     }
3063                     splx(old_level);
3064 
3065                     if (!search_found)  {
3066                         printk ("scsi%d : WARNING : INTFLY with no completed commands.\n",
3067                             host->host_no);
3068                     }
3069                 }
3070 
3071                 if (istat & (ISTAT_SIP|ISTAT_DIP)) {
3072                     done = 0;
3073                     interrupted = 1;
3074                     hostdata->state = STATE_HALTED;
3075                     /*
3076                      * NCR53c700 and NCR53c700-66 change the current SCSI
3077                      * process, hostdata->current, in the Linux driver so
3078                      * cmd = hostdata->current.
3079                      *
3080                      * With other chips, we must look through the commands
3081                      * executing and find the command structure which 
3082                      * corresponds to the DSA register.
3083                      */
3084 
3085                     if (hostdata->options & OPTION_700) {
3086                         cmd = (struct NCR53c7x0_cmd *) hostdata->current;
3087                     } else {
3088                         dsa = (unsigned long *) NCR53c7x0_read32(DSA_REG);
3089                         for (cmd = (struct NCR53c7x0_cmd *) 
3090                             hostdata->running_list; cmd &&
3091                             (dsa + (hostdata->dsa_start / sizeof(long))) != 
3092                                 cmd->dsa;
3093                             cmd = (struct NCR53c7x0_cmd *)(cmd->next));
3094                     }
3095                     if (hostdata->options & OPTION_DEBUG_INTR) {
3096                         if (cmd) {
3097                             printk("scsi%d : interrupt for pid %lu, id %d, lun %d ", 
3098                                 host->host_no, cmd->cmd->pid, (int) cmd->cmd->target,
3099                                 (int) cmd->cmd->lun);
3100                             print_command (cmd->cmd->cmnd);
3101                         } else {
3102                             printk("scsi%d : no active command\n", host->host_no);
3103                         }
3104                     }
3105 
3106                     if (istat & ISTAT_SIP) {
3107                         if (hostdata->options & OPTION_DEBUG_INTR) 
3108                             printk ("scsi%d : ISTAT_SIP\n", host->host_no);
3109                         intr_scsi (host, cmd);
3110                     }
3111                 
3112                     if (istat & ISTAT_DIP) {
3113                         if (hostdata->options & OPTION_DEBUG_INTR) 
3114                             printk ("scsi%d : ISTAT_DIP\n", host->host_no);
3115                         intr_dma (host, cmd);
3116                     }
3117 
3118                     if (!hostdata->dstat_valid) {
3119                         hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
3120                         hostdata->dstat_valid = 1;
3121                     }
3122 
3123 #if 1
3124             /* XXX - code check for 700/800 chips */
3125                     if (!(hostdata->dstat & DSTAT_DFE)) {
3126                         printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
3127             #if 0
3128                         if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
3129                             NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
3130                             while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
3131                                 DSTAT_DFE));
3132                         } else 
3133             #endif
3134                         {
3135                             NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
3136                             while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
3137                         }
3138                     }
3139 #endif
3140                 }
3141             } while (interrupted);
3142 
3143 
3144 
3145             if (hostdata->intrs != -1)
3146                 hostdata->intrs++;
3147 #if 0
3148             if (hostdata->intrs > 4) {
3149                 printk("scsi%d : too many interrupts, halting", host->host_no);
3150                 hostdata->idle = 1;
3151                 hostdata->options |= OPTION_DEBUG_INIT_ONLY;
3152                 panic("dying...\n");
3153             }
3154 #endif
3155 
3156             if (!hostdata->idle && hostdata->state == STATE_HALTED) {
3157                 if (!hostdata->dsp_changed) {
3158                     hostdata->dsp = (unsigned long *) NCR53c7x0_read32(DSP_REG);
3159                 }
3160                         
3161 #if 0
3162                 printk("scsi%d : new dsp is 0x%lx\n", host->host_no, 
3163                     (long) hostdata->dsp);
3164 #endif
3165                 
3166                 hostdata->state = STATE_RUNNING;
3167                 NCR53c7x0_write32 (DSP_REG, (unsigned long) hostdata->dsp);
3168             }
3169         }
3170     } while (!done);
3171 }
3172 
3173 
3174 /* 
3175  * Function : static int abort_connected (struct Scsi_Host *host)
3176  *
3177  * Purpose : Assuming that the NCR SCSI processor is currently 
3178  *      halted, break the currently established nexus.  Clean
3179  *      up of the NCR53c7x0_cmd and Scsi_Cmnd structures should
3180  *      be done on receipt of the abort interrupt.
3181  *
3182  * Inputs : host - SCSI host
3183  *
3184  */
3185 
3186 static int abort_connected (struct Scsi_Host *host) {
     /* [previous][next][first][last][top][bottom][index][help] */
3187     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3188         host->hostdata;
3189 
3190     hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
3191         sizeof(long);
3192     hostdata->dsp_changed = 1;
3193     printk ("scsi%d : DANGER : abort_connected() called \n",
3194         host->host_no);
3195 /* XXX - need to flag the command as aborted after the abort_connected
3196          code runs 
3197  */
3198     return 0;
3199 }
3200 
3201 
3202 /* 
3203  * Function : static void intr_phase_mismatch (struct Scsi_Host *host, 
3204  *      struct NCR53c7x0_cmd *cmd)
3205  *
3206  * Purpose : Handle phase mismatch interrupts
3207  *
3208  * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
3209  *      may be NULL.
3210  *
3211  * Side effects : The abort_connected() routine is called or the NCR chip 
3212  *      is restarted, jumping to the command_complete entry point, or 
3213  *      patching the address and transfer count of the current instruction 
3214  *      and calling the msg_in entry point as appropriate.
3215  *
3216  */
3217 
3218 static void intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd
     /* [previous][next][first][last][top][bottom][index][help] */
3219     *cmd) {
3220     NCR53c7x0_local_declare();
3221     unsigned long dbc_dcmd, *dsp, *dsp_next;
3222     unsigned char dcmd, sbcl;
3223     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3224         host->hostdata;
3225     char *phase;
3226     NCR53c7x0_local_setup(host);
3227 
3228     if (!cmd) {
3229         printk ("scsi%d : phase mismatch interrupt occurred with no current command.\n",
3230             host->host_no);
3231         abort_connected(host);
3232         return;
3233     }
3234 
3235     /*
3236      * Corrective action is based on where in the SCSI SCRIPT(tm) the error 
3237      * occurred, as well as which SCSI phase we are currently in.
3238      */
3239 
3240     dsp_next = (unsigned long *) NCR53c7x0_read32(DSP_REG);
3241 
3242     /*
3243      * Like other processors, the NCR adjusts the DSP pointer before
3244      * instruction decode.  Set the DSP address back to what it should
3245      * be for this instruction based on its size (2 or 3 longs).
3246      */
3247 
3248     dbc_dcmd = NCR53c7x0_read32(DBC_REG);
3249     dcmd = (dbc_dcmd & 0xff000000) >> 24;
3250     dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
3251     
3252     /*
3253      * Read new SCSI phase from the SBCL lines.
3254      *
3255      * Note that since all of our code uses a WHEN conditional instead of an 
3256      * IF conditional, we don't need to wait for a valid REQ.
3257      */
3258     sbcl = NCR53c7x0_read8(SBCL_REG);
3259     switch (sbcl) {
3260     case SBCL_PHASE_DATAIN:
3261         phase = "DATAIN";
3262         break;
3263     case SBCL_PHASE_DATAOUT:
3264         phase = "DATAOUT";
3265         break;
3266     case SBCL_PHASE_MSGIN:
3267         phase = "MSGIN";
3268         break;
3269     case SBCL_PHASE_MSGOUT:
3270         phase = "MSGOUT";
3271         break;
3272     case SBCL_PHASE_CMDOUT:
3273         phase = "CMDOUT";
3274         break;
3275     case SBCL_PHASE_STATIN:
3276         phase = "STATUSIN";
3277         break;
3278     default:
3279         phase = "unknown";
3280         break;
3281     }
3282 
3283 
3284     /*
3285      * The way the SCSI SCRIPTS(tm) are architected, recoverable phase
3286      * mismatches should only occur in the data transfer routines, or
3287      * when a command is being aborted.  
3288      */
3289     if (dsp >= cmd->data_transfer_start & dsp < cmd->data_transfer_end) {
3290 
3291         /*
3292          * There are three instructions used in our data transfer routines with
3293          * a phase conditional on them
3294          *
3295          * 1.  MOVE count, address, WHEN DATA_IN
3296          * 2.  MOVE count, address, WHEN DATA_OUT
3297          * 3.  CALL msg_in, WHEN MSG_IN.
3298          */
3299         switch (sbcl & SBCL_PHASE_MASK) {
3300         /*
3301          * 1.  STATUS phase : pass control to command_complete as if 
3302          *     a JUMP instruction was executed.  No patches are made.
3303          */
3304         case SBCL_PHASE_STATIN:
3305             if (hostdata->options & OPTION_DEBUG_INTR) 
3306                 printk ("scsi%d : new phase = STATIN\n", host->host_no);
3307             hostdata->dsp = hostdata->script + hostdata->E_command_complete /
3308                 sizeof(long);
3309             hostdata->dsp_changed = 1;
3310             return;
3311         /*
3312          * 2.  MSGIN phase : pass control to msg_in as if a CALL
3313          *     instruction was executed.  Patch current instruction.
3314          */
3315 /* 
3316  * XXX - This is buggy.
3317  */
3318         case SBCL_PHASE_MSGIN:
3319             if (hostdata->options & OPTION_DEBUG_INTR) 
3320                 printk ("scsi%d  : new phase = MSGIN\n", host->host_no);
3321             if ((dcmd & (DCMD_TYPE_MASK|DCMD_BMI_OP_MASK|DCMD_BMI_INDIRECT|
3322                     DCMD_BMI_MSG|DCMD_BMI_CD)) == (DCMD_TYPE_BMI|
3323                     DCMD_BMI_OP_MOVE_I)) {
3324                 dsp[0] = dbc_dcmd;
3325                 dsp[1] = NCR53c7x0_read32(DNAD_REG);
3326                 NCR53c7x0_write32(TEMP_REG, (unsigned long) dsp);
3327                 hostdata->dsp = hostdata->script + hostdata->E_msg_in /
3328                     sizeof(long);
3329                 hostdata->dsp_changed = 1;
3330             } else {
3331                 printk("scsi%d : unexpected MSGIN in dynamic NCR code, dcmd=0x%x.\n",
3332                     host->host_no, dcmd);
3333                 print_insn (host, dsp, "", 1);
3334                 print_insn (host, dsp_next, "", 1);
3335                 abort_connected (host);
3336             }
3337             return;
3338         /*
3339          * MSGOUT phase - shouldn't happen, because we haven't 
3340          *              asserted ATN.
3341          * CMDOUT phase - shouldn't happen, since we've already
3342          *              sent a valid command.
3343          * DATAIN/DATAOUT - other one shouldn't happen, since 
3344          *              SCSI commands can ONLY have one or the other.
3345          *
3346          * So, we abort the command if one of these things happens.
3347          */
3348         default:
3349             printk ("scsi%d : unexpected phase %s in data routine\n",
3350                 host->host_no, phase);
3351             abort_connected(host);
3352         } 
3353     /*
3354      * Any other phase mismatches abort the currently executing command.
3355      */
3356     } else {
3357         printk ("scsi%d : unexpected phase %s at dsp = 0x%x\n",
3358             host->host_no, phase, (unsigned) dsp);
3359         print_insn (host, dsp, "", 1);
3360         print_insn (host, dsp_next, "", 1);
3361         abort_connected(host);
3362     }
3363 }
3364 
3365 /*
3366  * Function : static void intr_dma (struct Scsi_Host *host, 
3367  *      struct NCR53c7x0_cmd *cmd)
3368  *
3369  * Purpose : handle all DMA interrupts, indicated by the setting 
3370  *      of the DIP bit in the ISTAT register.
3371  *
3372  * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
3373  *      may be NULL.
3374  */
3375 
3376 static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
3377     NCR53c7x0_local_declare();
3378     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3379         host->hostdata;
3380     unsigned char dstat,        /* DSTAT */     
3381         dbc_dcmd;               /* DCMD (high eight bits) + DBC */
3382     unsigned long *dsp,
3383         *next_dsp,              /* Current dsp */
3384         *dsa;
3385         
3386 
3387     int ipl,                    /* Old ipl from splx(0) */
3388         tmp;
3389     NCR53c7x0_local_setup(host);
3390 
3391     if (!hostdata->dstat_valid) {
3392         hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
3393         hostdata->dstat_valid = 1;
3394     }
3395     
3396     dstat = hostdata->dstat;
3397     
3398     if (hostdata->options & OPTION_DEBUG_INTR)
3399         printk("scsi%d : DSTAT=0x%x\n", host->host_no, (int) dstat);
3400 
3401     dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
3402     next_dsp = (unsigned long *) NCR53c7x0_read32(DSP_REG);
3403     dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
3404 /* XXX - check chip type */
3405     dsa = (unsigned long *) NCR53c7x0_read32(DSA_REG);
3406 
3407     /*
3408      * DSTAT_ABRT is the aborted interrupt.  This is set whenever the 
3409      * SCSI chip is aborted.  
3410      * 
3411      * With NCR53c700 and NCR53c700-66 style chips, we should only 
3412      * get this when the chip is currently running the accept 
3413      * reselect/select code and we have set the abort bit in the 
3414      * ISTAT register.
3415      *
3416      */
3417     
3418     if (dstat & DSTAT_ABRT) {
3419 #if 0
3420         /* XXX - add code here to deal with normal abort */
3421         if ((hostdata->options & OPTION_700) && (hostdata->state ==
3422             STATE_ABORTING) {
3423         } else 
3424 #endif
3425         {
3426             printk("scsi%d : unexpected abort interrupt at\n" 
3427                    "         ", host->host_no);
3428             print_insn (host, dsp, "s ", 1);
3429             panic(" ");
3430         }
3431     }
3432 
3433     /*
3434      * DSTAT_SSI is the single step interrupt.  Should be generated 
3435      * whenever we have single stepped or are tracing.
3436      */
3437 
3438     if (dstat & DSTAT_SSI) {
3439         if (hostdata->options & OPTION_DEBUG_TRACE) {
3440         } else if (hostdata->options & OPTION_DEBUG_SINGLE) {
3441             print_insn (host, dsp, "s ", 0);
3442             ipl = splx(0);
3443 /* XXX - should we do this, or can we get away with writing dsp? */
3444 
3445             NCR53c7x0_write8 (DCNTL_REG, (NCR53c7x0_read8(DCNTL_REG) & 
3446                 ~DCNTL_SSM) | DCNTL_STD);
3447             splx(ipl);
3448         } else {
3449             printk("scsi%d : unexpected single step interrupt at\n"
3450                    "         ", host->host_no);
3451             print_insn (host, dsp, "", 1);
3452             panic("         mail drew@colorad.edu\n");
3453         }
3454     }
3455 
3456     /*
3457      * DSTAT_IID / DSTAT_OPC (same bit, same meaning, only the name 
3458      * is different) is generated whenever an illegal instruction is 
3459      * encountered.  
3460      * 
3461      * XXX - we may want to emulate INTFLY here, so we can use 
3462      *    the same SCSI SCRIPT (tm) for NCR53c710 through NCR53c810  
3463      *    chips once we remove the ADD WITH CARRY instructions.
3464      */
3465 
3466     if (dstat & DSTAT_OPC) {
3467     /* 
3468      * Ascertain if this IID interrupts occured before or after a STO 
3469      * interrupt.  Since the interrupt handling code now leaves 
3470      * DSP unmodified until _after_ all stacked interrupts have been
3471      * processed, reading the DSP returns the original DSP register.
3472      * This means that if dsp lies between the select code, and 
3473      * message out following the selection code (where the IID interrupt
3474      * would have to have occurred by due to the implicit wait for REQ),
3475      * we have an IID interrupt resulting from a STO condition and 
3476      * can ignore it.
3477      */
3478 
3479         if (((dsp >= (hostdata->script + hostdata->E_select / sizeof(long))) &&
3480             (dsp <= (hostdata->script + hostdata->E_select_msgout / 
3481             sizeof(long) + 8))) || (hostdata->test_running == 2)) {
3482             if (hostdata->options & OPTION_DEBUG_INTR) 
3483                 printk ("scsi%d : ignoring DSTAT_IID for SSTAT_STO\n",
3484                     host->host_no);
3485             if (hostdata->expecting_iid) {
3486                 hostdata->expecting_iid = 0;
3487                 hostdata->idle = 1;
3488                 if (hostdata->test_running == 2) {
3489                     hostdata->test_running = 0;
3490                     hostdata->test_completed = 3;
3491                 } else if (cmd) 
3492                         abnormal_finished (cmd, DID_BAD_TARGET << 16);
3493             } else {
3494                 hostdata->expecting_sto = 1;
3495             }
3496         } else {
3497             printk("scsi%d : illegal instruction ", host->host_no);
3498             print_insn (host, dsp, "", 1);
3499             printk("scsi%d : DSP=0x%lx, DCMD|DBC=0x%lx, DSA=0x%lx\n"
3500                "         DSPS=0x%lx, TEMP=0x%lx, DMODE=0x%x,\n" 
3501                "         DNAD=0x%lx\n",
3502              host->host_no, (unsigned long) dsp, dbc_dcmd,
3503              (unsigned long) dsa, NCR53c7x0_read32(DSPS_REG),
3504              NCR53c7x0_read32(TEMP_REG), (int) NCR53c7x0_read8(hostdata->dmode),
3505              NCR53c7x0_read32(DNAD_REG));
3506             panic("         mail drew@Colorado.EDU\n");
3507         }
3508     }
3509 
3510     /* 
3511      * DSTAT_BF are bus fault errors, generated when the chip has 
3512      * attempted to access an illegal address.
3513      */
3514     
3515     if (dstat & DSTAT_800_BF) {
3516         printk("scsi%d : BUS FAULT, DSP=0x%lx, DCMD|DBC=0x%lx, DSA=0x%lx\n"
3517                "         DSPS=0x%lx, TEMP=0x%lx, DMODE=0x%x\n", 
3518              host->host_no, (unsigned long) dsp, NCR53c7x0_read32(DBC_REG),
3519              (unsigned long) dsa, NCR53c7x0_read32(DSPS_REG),
3520              NCR53c7x0_read32(TEMP_REG), (int) NCR53c7x0_read8(hostdata->dmode));
3521         print_dsa (host, dsa);
3522         printk("scsi%d : DSP->\n", host->host_no);
3523         print_insn(host, dsp, "", 1);
3524         print_insn(host, next_dsp, "", 1);
3525 #if 0
3526         panic("          mail drew@Colorado.EDU\n");
3527 #else
3528         hostdata->idle = 1;
3529         hostdata->options |= OPTION_DEBUG_INIT_ONLY;
3530 #endif
3531     }
3532         
3533 
3534     /* 
3535      * DSTAT_SIR interrupts are generated by the execution of 
3536      * the INT instruction.  Since the exact values available 
3537      * are determined entirely by the SCSI script running, 
3538      * and are local to a particular script, a unique handler
3539      * is called for each script.
3540      */
3541 
3542     if (dstat & DSTAT_SIR) {
3543         if (hostdata->options & OPTION_DEBUG_INTR)
3544             printk ("scsi%d : DSTAT_SIR\n", host->host_no);
3545         switch ((tmp = hostdata->dstat_sir_intr (host, cmd))) {
3546         case SPECIFIC_INT_NOTHING:
3547         case SPECIFIC_INT_RESTART:
3548             break;
3549         case SPECIFIC_INT_ABORT:
3550             abort_connected(host);
3551             break;
3552         case SPECIFIC_INT_PANIC:
3553             printk("scsi%d : failure at ", host->host_no);
3554             print_insn (host, dsp, "", 1);
3555             panic("          dstat_sir_intr() returned SPECIFIC_INT_PANIC\n");
3556             break;
3557         case SPECIFIC_INT_BREAK:
3558             intr_break (host, cmd);
3559             break;
3560         default:
3561             printk("scsi%d : failure at ", host->host_no);
3562             print_insn (host, dsp, "", 1);
3563             panic("          dstat_sir_intr() returned unknown value %d\n", 
3564                 tmp);
3565         }
3566     } 
3567 
3568 /* All DMA interrupts are fatal.  Flush SCSI queue */
3569     NCR53c7x0_write8 (STEST3_REG_800, STEST3_800_CSF);
3570     while (NCR53c7x0_read8 (STEST3_REG_800) & STEST3_800_CSF);
3571 }
3572 
3573 /*
3574  * Function : static int print_insn (struct Scsi_Host *host, 
3575  *      unsigned long *insn, int kernel)
3576  *
3577  * Purpose : print numeric representation of the instruction pointed
3578  *      to by insn to the debugging or kernel message buffer
3579  *      as appropriate.  
3580  *
3581  *      If desired, a user level program can interpret this 
3582  *      information.
3583  *
3584  * Inputs : host, insn - host, pointer to instruction, prefix - 
3585  *      string to prepend, kernel - use printk instead of debugging buffer.
3586  *
3587  * Returns : size, in longs, of instruction printed.
3588  */
3589 
3590 static int print_insn (struct Scsi_Host *host, unsigned long *insn,
     /* [previous][next][first][last][top][bottom][index][help] */
3591     char *prefix, int kernel) {
3592     char buf[80],               /* Temporary buffer and pointer */
3593         *tmp;                   
3594     unsigned char dcmd;         /* dcmd register for *insn */
3595     int size;
3596 
3597     dcmd = (insn[0] >> 24) & 0xff;
3598     sprintf(buf, "%s%08lx : 0x%08lx 0x%08lx", (prefix ? prefix : ""), 
3599         (unsigned long) insn, insn[0], insn[1]);
3600     tmp = buf + strlen(buf);
3601     if ((dcmd & DCMD_TYPE_MASK) == DCMD_TYPE_MMI)  {
3602         sprintf (tmp, " 0x%08lx\n", insn[2]);
3603         size = 3;
3604     } else {
3605         sprintf (tmp, "\n");
3606         size = 2;
3607     }
3608 
3609     if (kernel) 
3610         printk ("%s", buf);
3611 #ifdef NCR_DEBUG
3612     else {
3613         size_t len = strlen(buf);
3614         debugger_kernel_write(host, buf, len);
3615     }
3616 #endif
3617     return size;
3618 }
3619 
3620 /*
3621  * Function : int NCR53c7xx_abort (Scsi_Cmnd *cmd)
3622  * 
3623  * Purpose : Abort an errant SCSI command, doing all necessary
3624  *      cleanup of the issue_queue, running_list, shared Linux/NCR
3625  *      dsa issue and reconnect queues.
3626  *
3627  * Inputs : cmd - command to abort, code - entire result field
3628  *
3629  * Returns : 0 on success, -1 on failure.
3630  */
3631 
3632 int NCR53c7xx_abort (Scsi_Cmnd *cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
3633     struct Scsi_Host *host = cmd->host;
3634     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *) 
3635         host->hostdata;
3636     int old_level;
3637     struct NCR53c7x0_cmd *curr, **prev;
3638     old_level = splx(0);
3639 
3640 /*
3641  * The command could be hiding in the issue_queue.  This would be very
3642  * nice, as commands can't be moved from the high level driver's issue queue 
3643  * into the shared queue until an interrupt routine is serviced, and this
3644  * moving is atomic.  
3645  *
3646  * If this is the case, we don't have to worry about anything - we simply
3647  * pull the command out of the old queue, and call it aborted.
3648  */
3649 
3650     for (curr = hostdata->issue_queue, prev = &(hostdata->issue_queue);
3651         curr && curr->cmd != cmd; prev = &(curr->next), curr = curr->next);
3652 
3653     if (curr) {
3654         *prev = curr->next;
3655 /* XXX - get rid of DLL ? */
3656         if (curr->prev)
3657             curr->prev->next = curr->next;
3658 
3659         if (!scan_scsis_buf_busy) {
3660 #ifdef SCSI_MALLOC
3661             scsi_free ((void *) curr->real, curr->size);
3662 #else
3663             kfree_s ((void *) curr->real, curr->size);
3664 #endif
3665         } else {
3666             scan_scsis_buf_busy = 0;
3667         }
3668 
3669         cmd->result = 0;
3670         cmd->scsi_done(cmd);
3671         splx(old_level);
3672         return SCSI_ABORT_SUCCESS;
3673     }
3674 
3675 /* 
3676  * That failing, the command could be in our list of already executing 
3677  * commands.  If this is the case, drastic measures are called for.  
3678  */ 
3679 
3680     for (curr = hostdata->running_list, prev = &(hostdata->running_list);
3681         curr && curr->cmd != cmd; prev = &(curr->next), curr = curr->next);
3682 
3683     if (curr) {
3684         splx(old_level);
3685         printk ("scsi%d : DANGER : command in running list, can not abort.\n",
3686             cmd->host->host_no);
3687         return SCSI_ABORT_SNOOZE;
3688     }
3689 
3690 
3691 /* 
3692  * And if we couldn't find it in any of our queues, it must have been 
3693  * a dropped interrupt.
3694  */
3695 
3696     curr = (struct NCR53c7x0_cmd *) cmd->host_scribble;
3697 
3698     if (!scan_scsis_buf_busy) {
3699 #ifdef SCSI_MALLOC
3700         scsi_free ((void *) curr->real, curr->size);
3701 #else
3702         kfree_s ((void *) curr->real, curr->size);
3703 #endif
3704     } else {
3705         scan_scsis_buf_busy = 0;
3706     }
3707 
3708     if (((cmd->result & 0xff00) == 0xff00) ||
3709         ((cmd->result & 0xff) == 0xff)) {
3710         printk ("scsi%d : did this command ever run?\n", host->host_no);
3711     } else {
3712         printk ("scsi%d : probably lost INTFLY, normal completion\n", 
3713             host->host_no);
3714     }
3715     cmd->scsi_done(cmd);
3716     splx(old_level);
3717     return SCSI_ABORT_SNOOZE;
3718 }
3719 
3720 /*
3721  * Function : int NCR53c7xx_reset (Scsi_Cmnd *cmd) 
3722  * 
3723  * Purpose : perform a hard reset of the SCSI bus and NCR
3724  *      chip.
3725  *
3726  * Inputs : cmd - command which caused the SCSI RESET
3727  *
3728  * Returns : 0 on success.
3729  */
3730  
3731 int NCR53c7xx_reset (Scsi_Cmnd *cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
3732     NCR53c7x0_local_declare();
3733     struct Scsi_Host *host = cmd ? cmd->host : NULL;
3734     struct NCR53c7x0_hostdata *hostdata = host ? 
3735         (struct NCR53c7x0_hostdata *) host->hostdata : NULL;
3736     if (host) NCR53c7x0_local_setup(host);
3737     
3738 
3739     printk ("scsi%d : DANGER : NCR53c7xx_reset is NOP\n",
3740         cmd->host->host_no);
3741     return SCSI_RESET_SNOOZE;
3742 }
3743 
3744 const char *NCR53c7xx_info (void) {
     /* [previous][next][first][last][top][bottom][index][help] */
3745     return("More info here\n");
3746 }
3747 
3748 /*
3749  * The NCR SDMS bios follows Annex A of the SCSI-CAM draft, and 
3750  * therefore shares the scsicam_bios_param function.
3751  */
3752 
3753 static void print_dsa (struct Scsi_Host *host, unsigned long *dsa) {
     /* [previous][next][first][last][top][bottom][index][help] */
3754     struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3755         host->hostdata;
3756     Scsi_Cmnd * cmd;
3757     struct NCR53c7x0_cmd * c;
3758     int i, len;
3759     char *ptr;
3760 
3761     printk("scsi%d : dsa at 0x%x\n"
3762             "        + %d : dsa_msgout length = %d, data = 0x%x\n" ,
3763             host->host_no, (unsigned) dsa, hostdata->dsa_msgout,
3764             dsa[hostdata->dsa_msgout / sizeof(long)],
3765             dsa[hostdata->dsa_msgout / sizeof(long) + 1]);
3766 
3767     for (i = dsa[hostdata->dsa_msgout / sizeof(long)],
3768         ptr = (char *) dsa[hostdata->dsa_msgout / sizeof(long) + 1]; i > 0;
3769         ptr += len, i -= len) {
3770         printk("               ");
3771         len = print_msg (ptr);
3772         printk("\n");
3773     }
3774 }
3775 

/* [previous][next][first][last][top][bottom][index][help] */