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

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