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. ncr_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. ncr_halt
  35. NCR53c7x0_release

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

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