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

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

DEFINITIONS

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

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

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