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

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

DEFINITIONS

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

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