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. print_dsa
  33. shutdown
  34. halt
  35. NCR53c7x0_release

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

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