root/drivers/scsi/NCR5380.c

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

DEFINITIONS

This source file includes following definitions.
  1. initialize_SCp
  2. NCR5380_print
  3. NCR5380_print_phase
  4. run_main
  5. should_disconnect
  6. NCR5380_set_timer
  7. NCR5380_timer_fn
  8. NCR5380_all_init
  9. probe_intr
  10. NCR5380_probe_irq
  11. NCR5380_print_options
  12. NCR5380_print_status
  13. NCR5380_init
  14. NCR5380_queue_command
  15. NCR5380_main
  16. NCR5380_intr
  17. collect_stats
  18. NCR5380_select
  19. NCR5380_transfer_pio
  20. do_reset
  21. do_abort
  22. NCR5380_transfer_dma
  23. NCR5380_information_transfer
  24. NCR5380_reselect
  25. NCR5380_dma_complete
  26. NCR5380_abort
  27. NCR5380_reset

   1 #ifndef NDEBUG
   2 #define NDEBUG (NDEBUG_RESTART_SELECT | NDEBUG_ABORT)
   3 #endif
   4 /* 
   5  * NCR 5380 generic driver routines.  These should make it *trivial*
   6  *      to implement 5380 SCSI drivers under Linux with a non-trantor
   7  *      architecture.
   8  *
   9  *      Note that these routines also work with NR53c400 family chips.
  10  *
  11  * Copyright 1993, Drew Eckhardt
  12  *      Visionary Computing 
  13  *      (Unix and Linux consulting and custom programming)
  14  *      drew@colorado.edu
  15  *      +1 (303) 666-5836
  16  *
  17  * DISTRIBUTION RELEASE 6. 
  18  *
  19  * For more information, please consult 
  20  *
  21  * NCR 5380 Family
  22  * SCSI Protocol Controller
  23  * Databook
  24  *
  25  * NCR Microelectronics
  26  * 1635 Aeroplaza Drive
  27  * Colorado Springs, CO 80916
  28  * 1+ (719) 578-3400
  29  * 1+ (800) 334-5454
  30  */
  31 
  32 /*
  33  * $Log: NCR5380.c,v $
  34  * Revision 1.5  1994/01/19  09:14:57  drew
  35  * Fixed udelay() hack that was being used on DATAOUT phases
  36  * instead of a proper wait for the final handshake.
  37  *
  38  * Revision 1.4  1994/01/19  06:44:25  drew
  39  * *** empty log message ***
  40  *
  41  * Revision 1.3  1994/01/19  05:24:40  drew
  42  * Added support for TCR LAST_BYTE_SENT bit.
  43  *
  44  * Revision 1.2  1994/01/15  06:14:11  drew
  45  * REAL DMA support, bug fixes.
  46  *
  47  * Revision 1.1  1994/01/15  06:00:54  drew
  48  * Initial revision
  49  *
  50  */
  51 
  52 /*
  53  * Further development / testing that should be done : 
  54  * 1.  Cleanup the NCR5380_transfer_dma function and DMA operation complete
  55  *     code so that everything does the same thing that's done at the 
  56  *     end of a pseudo-DMA read operation.
  57  *
  58  * 2.  Fix REAL_DMA (interrupt driven, polled works fine) -
  59  *     basically, transfer size needs to be reduced by one 
  60  *     and the last byte read as is done with PSEUDO_DMA.
  61  * 
  62  * 3.  Test USLEEP code 
  63  *
  64  * 4.  Test SCSI-II tagged queueing (I have no devices which support 
  65  *      tagged queueing)
  66  *
  67  * 5.  Test linked command handling code after Eric is ready with 
  68  *      the high level code.
  69  */
  70 
  71 #if (NDEBUG & NDEBUG_LISTS)
  72 #define LIST(x,y) {printk("LINE:%d   Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
  73 #define REMOVE(w,x,y,z) {printk("LINE:%d   Removing: %p->%p  %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); }
  74 #else
  75 #define LIST(x,y)
  76 #define REMOVE(w,x,y,z)
  77 #endif
  78 
  79 #ifndef notyet
  80 #undef LINKED
  81 #undef USLEEP
  82 #undef REAL_DMA
  83 #endif
  84 
  85 #ifdef REAL_DMA_POLL
  86 #undef READ_OVERRUNS
  87 #define READ_OVERRUNS
  88 #endif
  89 
  90 /*
  91  * Design
  92  * Issues :
  93  *
  94  * The other Linux SCSI drivers were written when Linux was Intel PC-only,
  95  * and specifically for each board rather than each chip.  This makes their
  96  * adaptation to platforms like the Mac (Some of which use NCR5380's)
  97  * more difficult than it has to be.
  98  *
  99  * Also, many of the SCSI drivers were written before the command queuing
 100  * routines were implemented, meaning their implementations of queued 
 101  * commands were hacked on rather than designed in from the start.
 102  *
 103  * When I designed the Linux SCSI drivers I figured that 
 104  * while having two different SCSI boards in a system might be useful
 105  * for debugging things, two of the same type wouldn't be used.
 106  * Well, I was wrong and a number of users have mailed me about running
 107  * multiple high-performance SCSI boards in a server.
 108  *
 109  * Finally, when I get questions from users, I have no idea what 
 110  * revision of my driver they are running.
 111  *
 112  * This driver attempts to address these problems :
 113  * This is a generic 5380 driver.  To use it on a different platform, 
 114  * one simply writes appropriate system specific macros (ie, data
 115  * transfer - some PC's will use the I/O bus, 68K's must use 
 116  * memory mapped) and drops this file in their 'C' wrapper.
 117  *
 118  * As far as command queueing, two queues are maintained for 
 119  * each 5380 in the system - commands that haven't been issued yet,
 120  * and commands that are currently executing.  This means that an 
 121  * unlimited number of commands may be queued, letting 
 122  * more commands propagate from the higher driver levels giving higher 
 123  * throughput.  Note that both I_T_L and I_T_L_Q nexuses are supported, 
 124  * allowing multiple commands to propagate all the way to a SCSI-II device 
 125  * while a command is already executing.
 126  *
 127  * To solve the multiple-boards-in-the-same-system problem, 
 128  * there is a separate instance structure for each instance
 129  * of a 5380 in the system.  So, multiple NCR5380 drivers will
 130  * be able to coexist with appropriate changes to the high level
 131  * SCSI code.  
 132  *
 133  * A NCR5380_PUBLIC_REVISION macro is provided, with the release
 134  * number (updated for each public release) printed by the 
 135  * NCR5380_print_options command, which should be called from the 
 136  * wrapper detect function, so that I know what release of the driver
 137  * users are using.
 138  *
 139  * Issues specific to the NCR5380 : 
 140  *
 141  * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead 
 142  * piece of hardware that requires you to sit in a loop polling for 
 143  * the REQ signal as long as you are connected.  Some devices are 
 144  * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect 
 145  * while doing long seek operations.
 146  * 
 147  * The workaround for this is to keep track of devices that have
 148  * disconnected.  If the device hasn't disconnected, for commands that
 149  * should disconnect, we do something like 
 150  *
 151  * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
 152  * 
 153  * Some tweaking of N and M needs to be done.  An algorithm based 
 154  * on "time to data" would give the best results as long as short time
 155  * to datas (ie, on the same track) were considered, however these 
 156  * broken devices are the exception rather than the rule and I'd rather
 157  * spend my time optimizing for the normal case.
 158  *
 159  * Architecture :
 160  *
 161  * At the heart of the design is a coroutine, NCR5380_main,
 162  * which is started when not running by the interrupt handler,
 163  * timer, and queue command function.  It attempts to establish
 164  * I_T_L or I_T_L_Q nexuses by removing the commands from the 
 165  * issue queue and calling NCR5380_select() if a nexus 
 166  * is not established. 
 167  *
 168  * Once a nexus is established, the NCR5380_information_transfer()
 169  * phase goes through the various phases as instructed by the target.
 170  * if the target goes into MSG IN and sends a DISCONNECT message,
 171  * the command structure is placed into the per instance disconnected
 172  * queue, and NCR5380_main tries to find more work.  If USLEEP
 173  * was defined, and the target is idle for too long, the system
 174  * will try to sleep.
 175  *
 176  * If a command has disconnected, eventually an interrupt will trigger,
 177  * calling NCR5380_intr()  which will in turn call NCR5380_reselect
 178  * to reestablish a nexus.  This will run main if necessary.
 179  *
 180  * On command termination, the done function will be called as 
 181  * appropriate.
 182  *
 183  * SCSI pointers are maintained in the SCp field of SCSI command 
 184  * structures, being initialized after the command is connected
 185  * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
 186  * Note that in violation of the standard, an implicit SAVE POINTERS operation
 187  * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
 188  */
 189 
 190 /*
 191  * Using this file :
 192  * This file a skeleton Linux SCSI driver for the NCR 5380 series
 193  * of chips.  To use it, you write a architecture specific functions 
 194  * and macros and include this file in your driver.
 195  *
 196  * These macros control options : 
 197  * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be 
 198  *      defined.
 199  * 
 200  * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
 201  *      for commands that return with a CHECK CONDITION status. 
 202  *
 203  * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
 204  *      transceivers. 
 205  *
 206  * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
 207  *      bytes at a time.  Since interrupts are disabled by default during
 208  *      these transfers, we might need this to give reasonable interrupt
 209  *      service time if the transfer size gets too large.
 210  *
 211  * LINKED - if defined, linked commands are supported.
 212  *
 213  * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
 214  *
 215  * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
 216  *
 217  * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
 218  *      rely on phase mismatch and EOP interrupts to determine end 
 219  *      of phase.
 220  *
 221  * SCSI2 - if defined, SCSI-2 tagged queuing is used where possible
 222  *
 223  * UNSAFE - leave interrupts enabled during pseudo-DMA transfers.  You
 224  *          only really want to use this if you're having a problem with
 225  *          dropped characters during high speed communications, and even
 226  *          then, you're going to be better off twiddling with transfersize
 227  *          in the high level code.
 228  *
 229  * USLEEP - if defined, on devices that aren't disconnecting from the 
 230  *      bus, we will go to sleep so that the CPU can get real work done 
 231  *      when we run a command that won't complete immediately.
 232  *
 233  * Note that if USLEEP is defined, NCR5380_TIMER *must* also be
 234  * defined.
 235  *
 236  * Defaults for these will be provided if USLEEP is defined, although
 237  * the user may want to adjust these to allocate CPU resources to 
 238  * the SCSI driver or "real" code.
 239  * 
 240  * USLEEP_SLEEP - amount of time, in jiffies, to sleep
 241  *
 242  * USLEEP_POLL - amount of time, in jiffies, to poll
 243  *
 244  * These macros MUST be defined :
 245  * NCR5380_local_declare() - declare any local variables needed for your transfer
 246  *      routines.
 247  *
 248  * NCR5380_setup(instance) - initialize any local variables needed from a given
 249  *      instance of the host adapter for NCR5380_{read,write,pread,pwrite}
 250  * 
 251  * NCR5380_read(register)  - read from the specified register
 252  *
 253  * NCR5380_write(register, value) - write to the specific register 
 254  *
 255  * NCR5380_implementation_fields  - additional fields needed for this 
 256  *      specific implementation of the NCR5380
 257  *
 258  * Either real DMA *or* pseudo DMA may be implemented
 259  * REAL functions : 
 260  * NCR5380_REAL_DMA should be defined if real DMA is to be used.
 261  * Note that the DMA setup functions should return the number of bytes 
 262  *      that they were able to program the controller for.
 263  *
 264  * Also note that generic i386/PC versions of these macros are 
 265  *      available as NCR5380_i386_dma_write_setup,
 266  *      NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
 267  *
 268  * NCR5380_dma_write_setup(instance, src, count) - initialize
 269  * NCR5380_dma_read_setup(instance, dst, count) - initialize
 270  * NCR5380_dma_residual(instance); - residual count
 271  *
 272  * PSEUDO functions :
 273  * NCR5380_pwrite(instance, src, count)
 274  * NCR5380_pread(instance, dst, count);
 275  *
 276  * If nothing specific to this implementation needs doing (ie, with external
 277  * hardware), you must also define 
 278  *  
 279  * NCR5380_queue_command
 280  * NCR5380_reset
 281  * NCR5380_abort
 282  *
 283  * to be the global entry points into the specific driver, ie 
 284  * #define NCR5380_queue_command t128_queue_command.
 285  *
 286  * If this is not done, the routines will be defined as static functions
 287  * with the NCR5380* names and the user must provide a globally
 288  * accessible wrapper function.
 289  *
 290  * The generic driver is initialized by calling NCR5380_init(instance),
 291  * after setting the appropriate host specific fields and ID.  If the 
 292  * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
 293  * possible) function may be used.  Before the specific driver initialization
 294  * code finishes, NCR5380_print_options should be called.
 295  */
 296 
 297 static int do_abort (struct Scsi_Host *host);
 298 static void do_reset (struct Scsi_Host *host);
 299 static struct Scsi_Host *first_instance = NULL;
 300 static Scsi_Host_Template *the_template = NULL;
 301 
 302 /*
 303  * Function : void initialize_SCp(Scsi_Cmnd *cmd)
 304  *
 305  * Purpose : initialize the saved data pointers for cmd to point to the 
 306  *      start of the buffer.
 307  *
 308  * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
 309  */
 310 
 311 static __inline__ void initialize_SCp(Scsi_Cmnd *cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
 312     /* 
 313      * Initialize the Scsi Pointer field so that all of the commands in the 
 314      * various queues are valid.
 315      */
 316 
 317     if (cmd->use_sg) {
 318         cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
 319         cmd->SCp.buffers_residual = cmd->use_sg - 1;
 320         cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
 321         cmd->SCp.this_residual = cmd->SCp.buffer->length;
 322     } else {
 323         cmd->SCp.buffer = NULL;
 324         cmd->SCp.buffers_residual = 0;
 325         cmd->SCp.ptr = (char *) cmd->request_buffer;
 326         cmd->SCp.this_residual = cmd->request_bufflen;
 327     }
 328 }
 329 
 330 #include <linux/delay.h>
 331 
 332 #ifdef NDEBUG
 333 static struct {
 334     unsigned char mask;
 335     const char * name;} 
 336 signals[] = {{ SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" }, 
 337     { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD,  "CD" }, { SR_IO, "IO" }, 
 338     { SR_SEL, "SEL" }, {0, NULL}}, 
 339 basrs[] = {{BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}},
 340 icrs[] = {{ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
 341     {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"}, 
 342     {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"}, 
 343     {0, NULL}},
 344 mrs[] = {{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"}, 
 345     {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR, 
 346     "MODE PARITY INTR"}, {MR_MONITOR_BSY, "MODE MONITOR BSY"},
 347     {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"}, 
 348     {0, NULL}};
 349 
 350 /*
 351  * Function : void NCR5380_print(struct Scsi_Host *instance)
 352  *
 353  * Purpose : print the SCSI bus signals for debugging purposes
 354  *
 355  * Input : instance - which NCR5380
 356  */
 357 
 358 static void NCR5380_print(struct Scsi_Host *instance) {
     /* [previous][next][first][last][top][bottom][index][help] */
 359     NCR5380_local_declare();
 360     unsigned char status, data, basr, mr, icr, i;
 361     NCR5380_setup(instance);
 362     cli();
 363     data = NCR5380_read(CURRENT_SCSI_DATA_REG);
 364     status = NCR5380_read(STATUS_REG);
 365     mr = NCR5380_read(MODE_REG);
 366     icr = NCR5380_read(INITIATOR_COMMAND_REG);
 367     basr = NCR5380_read(BUS_AND_STATUS_REG);
 368     sti();
 369     printk("STATUS_REG: %02x ", status);
 370     for (i = 0; signals[i].mask ; ++i) 
 371         if (status & signals[i].mask)
 372             printk(",%s", signals[i].name);
 373     printk("\nBASR: %02x ", basr);
 374     for (i = 0; basrs[i].mask ; ++i) 
 375         if (basr & basrs[i].mask)
 376             printk(",%s", basrs[i].name);
 377     printk("\nICR: %02x ", icr);
 378     for (i = 0; icrs[i].mask; ++i) 
 379         if (icr & icrs[i].mask)
 380             printk(",%s", icrs[i].name);
 381     printk("\nMODE: %02x ", mr);
 382     for (i = 0; mrs[i].mask; ++i) 
 383         if (mr & mrs[i].mask)
 384             printk(",%s", mrs[i].name);
 385     printk("\n");
 386 }
 387 
 388 static struct {
 389     unsigned char value;
 390     const char *name;
 391 } phases[] = {
 392 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
 393 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
 394 {PHASE_UNKNOWN, "UNKNOWN"}};
 395 
 396 /* 
 397  * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
 398  *
 399  * Purpose : print the current SCSI phase for debugging purposes
 400  *
 401  * Input : instance - which NCR5380
 402  */
 403 
 404 static void NCR5380_print_phase(struct Scsi_Host *instance) {
     /* [previous][next][first][last][top][bottom][index][help] */
 405     NCR5380_local_declare();
 406     unsigned char status;
 407     int i;
 408     NCR5380_setup(instance);
 409 
 410     status = NCR5380_read(STATUS_REG);
 411     if (!(status & SR_REQ)) 
 412         printk("scsi%d : REQ not asserted, phase unknown.\n", 
 413             instance->host_no);
 414     else {
 415         for (i = 0; (phases[i].value != PHASE_UNKNOWN) && 
 416             (phases[i].value != (status & PHASE_MASK)); ++i); 
 417         printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
 418     }
 419 }
 420 #endif
 421 
 422 /*
 423  * We need to have our coroutine active given these constraints : 
 424  * 1.  The mutex flag, main_running, can only be set when the main 
 425  *     routine can actually process data, otherwise SCSI commands
 426  *     will never get issued.
 427  *
 428  * 2.  NCR5380_main() shouldn't be called before it has exited, because
 429  *     other drivers have had kernel stack overflows in similar
 430  *     situations.
 431  *
 432  * 3.  We don't want to inline NCR5380_main() because of space concerns,
 433  *     even though it is only called in two places.
 434  *
 435  * So, the solution is to set the mutex in an inline wrapper for the 
 436  * main coroutine, and have the main coroutine exit with interrupts 
 437  * disabled after the final search through the queues so that no race 
 438  * conditions are possible.
 439  */
 440 
 441 static volatile int main_running = 0;
 442 
 443 /* 
 444  * Function : run_main(void)
 445  * 
 446  * Purpose : insure that the coroutine is running and will process our 
 447  *      request.  main_running is checked/set here (in an inline function)
 448  *      rather than in NCR5380_main itself to reduce the chances of stack
 449  *      overflow.
 450  *
 451  */
 452 
 453 static __inline__ void run_main(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 454     cli();
 455     if (!main_running) {
 456         main_running = 1;
 457         NCR5380_main();
 458         /* 
 459          * main_running is cleared in NCR5380_main once it can't do 
 460          * more work, and NCR5380_main exits with interrupts disabled.
 461          */
 462         sti();
 463     } else 
 464         sti();
 465 }
 466 
 467 #ifdef USLEEP
 468 #ifndef NCR5380_TIMER
 469 #error "NCR5380_TIMER must be defined so that this type of NCR5380 driver gets a unique timer."
 470 #endif
 471 
 472 /*
 473  * These need tweaking, and would probably work best as per-device 
 474  * flags initialized differently for disk, tape, cd, etc devices.
 475  * People with broken devices are free to experiment as to what gives
 476  * the best results for them.
 477  *
 478  * USLEEP_SLEEP should be a minimum seek time.
 479  *
 480  * USLEEP_POLL should be a maximum rotational latency.
 481  */
 482 #ifndef USLEEP_SLEEP
 483 /* 20 ms (reasonable hard disk speed) */
 484 #define USLEEP_SLEEP 2
 485 #endif
 486 /* 300 RPM (floppy speed) */
 487 #ifndef USLEEP_POLL
 488 #define USLEEP_POLL 20
 489 #endif
 490 
 491 static struct Scsi_Host * expires_first = NULL;
 492 
 493 /* 
 494  * Function : int should_disconnect (unsigned char cmd)
 495  *
 496  * Purpose : decide weather a command would normally disconnect or 
 497  *      not, since if it won't disconnect we should go to sleep.
 498  *
 499  * Input : cmd - opcode of SCSI command
 500  *
 501  * Returns : DISCONNECT_LONG if we should disconnect for a really long 
 502  *      time (ie always, sleep, look for REQ active, sleep), 
 503  *      DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
 504  *      time-to-data delay, DISCONNECT_NONE if this command would return
 505  *      immediately.
 506  *
 507  *      Future sleep algorithms based on time to data can exploit 
 508  *      something like this so they can differentiate between "normal" 
 509  *      (ie, read, write, seek) and unusual commands (ie, * format).
 510  *
 511  * Note : We don't deal with commands that handle an immediate disconnect,
 512  *        
 513  */
 514 
 515 static int should_disconnect (unsigned char cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
 516     switch (cmd) {
 517     case READ_6:
 518     case WRITE_6:
 519     case SEEK_6:
 520     case READ_10:
 521     case WRITE_10:
 522     case SEEK_10:
 523         return DISCONNECT_TIME_TO_DATA;
 524     case FORMAT_UNIT:
 525     case SEARCH_HIGH:
 526     case SEARCH_LOW:
 527     case SEARCH_EQUAL:
 528         return DISCONNECT_LONG;
 529     default:
 530         return DISCONNECT_NONE;
 531     }
 532 }
 533 
 534 /*
 535  * Assumes instance->time_expires has been set in higher level code.
 536  */
 537 
 538 static int NCR5380_set_timer (struct Scsi_Host *instance) {
     /* [previous][next][first][last][top][bottom][index][help] */
 539     struct Scsi_Host *tmp, **prev;
 540     
 541     cli();
 542     if (((struct NCR5380_hostdata *) (instance->host_data))->next_timer) {
 543         sti();
 544         return -1;
 545     }
 546 
 547     for (prev = &expires_first, tmp = expires_first; tmp; 
 548         prev = &(((struct NCR5380_hostdata *) tmp->host_data)->next_timer), 
 549         tmp = ((struct NCR5380_hostdata *) tmp->host_data)->next_timer)
 550         if (instance->time_expires < tmp->time_expires) 
 551             break;
 552            
 553     instance->next_timer = tmp;
 554     *prev = instance;
 555     timer_table[NCR5380_TIMER].expires = expires_first->time_expires;
 556     timer_active |= 1 << NCR5380_TIMER;
 557     sti();
 558     return 0;
 559 }    
 560 
 561 /* Doing something about unwanted reentrancy here might be useful */
 562 void NCR5380_timer_fn(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 563     struct Scsi_Host *instance;
 564     cli();
 565     for (; expires_first && expires_first->time_expires >= jiffies; ) {
 566         instance = ((NCR5380_hostdata *) expires_first->host_data)->
 567             expires_next;
 568         ((NCR5380_hostdata *) expires_first->host_data)->expires_next = 
 569             NULL;
 570         ((NCR5380_hostdata *) expires_first->host_data)->time_expires = 
 571             0;
 572         expires_first = instance;
 573     }
 574 
 575     if (expires_first) {
 576         timer_table[NCR5380_TIMER].expires = ((NCR5380_hostdata *) 
 577             expires_first->host_data)->time_expires;
 578         timer_active |= (1 << NCR5380_TIMER);
 579     } else {
 580         timer_table[NCR5380_TIMER].expires = 0;
 581         timer_active &= ~(1 << MCR5380_TIMER);
 582     }
 583     sti();
 584 
 585     run_main();
 586 }
 587 #endif /* def USLEEP */
 588 
 589 static void NCR5380_all_init (void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 590     static int done = 0;
 591     if (!done) {
 592 #if (NDEBUG & NDEBUG_INIT)
 593         printk("scsi : NCR5380_all_init()\n");
 594 #endif
 595         done = 1;
 596 #ifdef USLEEP
 597         timer_table[NCR5380_TIMER].expires = 0;
 598         timer_table[NCR5380_TIMER].fn = NCR5380_timer_fn;
 599 #endif
 600     }
 601 }
 602 
 603 #ifdef AUTOPROBE_IRQ
 604 /*
 605  * Function : int NCR5380_probe_irq (struct Scsi_Host *instance, int possible)
 606  * 
 607  * Purpose : autoprobe for the IRQ line used by the NCR5380.  
 608  *
 609  * Inputs : instance - pointer to this instance of the NCR5380 driver,
 610  *          possible - bitmask of permissible interrupts.
 611  *
 612  * Returns : number of the IRQ selected, IRQ_NONE if no interrupt fired.
 613  * 
 614  * XXX no effort is made to deal with spurious interrupts. 
 615  */
 616 
 617 
 618 static int probe_irq;
 619 static void probe_intr (int irq, void *dev_id, struct pt_regs * regs) {
     /* [previous][next][first][last][top][bottom][index][help] */
 620     probe_irq = irq;
 621 };
 622 
 623 static int NCR5380_probe_irq (struct Scsi_Host *instance, int possible) {
     /* [previous][next][first][last][top][bottom][index][help] */
 624     NCR5380_local_declare();
 625     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
 626          instance->hostdata;
 627     unsigned long timeout;
 628     int trying_irqs, i, mask;
 629     NCR5380_setup(instance);
 630 
 631     for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1) 
 632         if ((mask & possible) &&  (request_irq(i, &probe_intr, SA_INTERRUPT, "NCR-probe", NULL) 
 633             == 0)) 
 634             trying_irqs |= mask;
 635 
 636     timeout = jiffies + 25;
 637     probe_irq = IRQ_NONE;
 638 
 639 /*
 640  * A interrupt is triggered whenever BSY = false, SEL = true
 641  * and a bit set in the SELECT_ENABLE_REG is asserted on the 
 642  * SCSI bus.
 643  *
 644  * Note that the bus is only driven when the phase control signals
 645  * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
 646  * to zero.
 647  */
 648 
 649     NCR5380_write(TARGET_COMMAND_REG, 0);
 650     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
 651     NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
 652     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | 
 653         ICR_ASSERT_SEL);
 654 
 655     while (probe_irq == IRQ_NONE && jiffies < timeout)
 656         barrier();
 657 
 658     NCR5380_write(SELECT_ENABLE_REG, 0);
 659     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
 660 
 661     for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
 662         if (trying_irqs & mask) 
 663             free_irq(i, NULL);
 664 
 665     return probe_irq;
 666 }
 667 #endif /* AUTOPROBE_IRQ */
 668  
 669 /*
 670  * Function : void NCR58380_print_options (struct Scsi_Host *instance)
 671  *
 672  * Purpose : called by probe code indicating the NCR5380 driver
 673  *           options that were selected.
 674  *
 675  * Inputs : instance, pointer to this instance.  Unused.
 676  */
 677 
 678 static void NCR5380_print_options (struct Scsi_Host *instance) {
     /* [previous][next][first][last][top][bottom][index][help] */
 679     printk(" generic options"
 680 #ifdef AUTOPROBE_IRQ
 681     " AUTOPROBE_IRQ"
 682 #endif
 683 #ifdef AUTOSENSE 
 684     " AUTOSENSE"
 685 #endif
 686 #ifdef DIFFERENTIAL
 687     " DIFFERENTIAL"
 688 #endif
 689 #ifdef REAL_DMA
 690     " REAL DMA"
 691 #endif
 692 #ifdef REAL_DMA_POLL
 693     " REAL DMA POLL"
 694 #endif
 695 #ifdef PARITY
 696     " PARITY"
 697 #endif
 698 #ifdef PSEUDO_DMA
 699     " PSEUDO DMA"
 700 #endif
 701 #ifdef SCSI2
 702     " SCSI-2"
 703 #endif
 704 #ifdef UNSAFE
 705     " UNSAFE "
 706 #endif
 707     );
 708 #ifdef USLEEP
 709     printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
 710 #endif
 711     printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
 712     if (((struct NCR5380_hostdata *)instance->hostdata)->flags & FLAG_NCR53C400) {
 713         printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE);
 714     }
 715 }
 716 
 717 /*
 718  * Function : void NCR5380_print_status (struct Scsi_Host *instance)
 719  *
 720  * Purpose : print commands in the various queues, called from
 721  *      NCR5380_abort and NCR5380_debug to aid debugging.
 722  *
 723  * Inputs : instance, pointer to this instance.  
 724  */
 725 
 726 static void NCR5380_print_status (struct Scsi_Host *instance) {
     /* [previous][next][first][last][top][bottom][index][help] */
 727     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) 
 728         instance->hostdata;
 729     Scsi_Cmnd *ptr;
 730 
 731 
 732     printk("NCR5380 : coroutine is%s running.\n",
 733         main_running ? "" : "n't");
 734     
 735 #ifdef NDEBUG
 736     NCR5380_print (instance);
 737     NCR5380_print_phase (instance);
 738 #endif
 739 
 740     cli();
 741     if (!hostdata->connected) {
 742         printk ("scsi%d: no currently connected command\n",
 743             instance->host_no);
 744     } else {
 745         print_Scsi_Cmnd ((Scsi_Cmnd *) hostdata->connected);
 746     }
 747 
 748     printk ("scsi%d: issue_queue\n", instance->host_no);
 749 
 750     for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; 
 751         ptr = (Scsi_Cmnd *) ptr->host_scribble) 
 752         print_Scsi_Cmnd (ptr);
 753 
 754     printk ("scsi%d: disconnected_queue\n", instance->host_no);
 755 
 756     for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; 
 757         ptr = (Scsi_Cmnd *) ptr->host_scribble) 
 758         print_Scsi_Cmnd (ptr);
 759     
 760     sti();
 761 }
 762 
 763 
 764 /* 
 765  * Function : void NCR5380_init (struct Scsi_Host *instance, flags)
 766  *
 767  * Purpose : initializes *instance and corresponding 5380 chip,
 768  *      with flags OR'd into the initial flags value.
 769  *
 770  * Inputs : instance - instantiation of the 5380 driver.  
 771  *
 772  * Notes : I assume that the host, hostno, and id bits have been
 773  *      set correctly.  I don't care about the irq and other fields. 
 774  * 
 775  */
 776 
 777 static void NCR5380_init (struct Scsi_Host *instance, int flags) {
     /* [previous][next][first][last][top][bottom][index][help] */
 778     NCR5380_local_declare();
 779     int i, pass;
 780     unsigned long timeout;
 781     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) 
 782         instance->hostdata;
 783 
 784     /* 
 785      * On NCR53C400 boards, NCR5380 registers are mapped 8 past 
 786      * the base address.
 787      */
 788 
 789 #ifdef NCR53C400
 790     if (flags & FLAG_NCR53C400)
 791         instance->NCR5380_instance_name += NCR53C400_address_adjust;
 792 #endif
 793 
 794     NCR5380_setup(instance);
 795 
 796     NCR5380_all_init();
 797 
 798     hostdata->aborted = 0;
 799     hostdata->id_mask = 1 << instance->this_id;
 800     for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
 801         if (i > hostdata->id_mask)
 802             hostdata->id_higher_mask |= i;
 803     for (i = 0; i < 8; ++i)
 804         hostdata->busy[i] = 0;
 805 #ifdef REAL_DMA
 806     hostdata->dmalen = 0;
 807 #endif
 808     hostdata->targets_present = 0;
 809     hostdata->connected = NULL;
 810     hostdata->issue_queue = NULL;
 811     hostdata->disconnected_queue = NULL;
 812 #ifdef NCR5380_STATS
 813     for (i = 0; i < 8; ++i) {
 814         hostdata->time_read[i] = 0;
 815         hostdata->time_write[i] = 0;
 816         hostdata->bytes_read[i] = 0;
 817         hostdata->bytes_write[i] = 0;
 818     }
 819     hostdata->timebase = 0;
 820     hostdata->pendingw = 0;
 821     hostdata->pendingr = 0;
 822 #endif
 823 
 824     /* The CHECK code seems to break the 53C400. Will check it later maybe */
 825     if (flags & FLAG_NCR53C400)
 826         hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
 827     else
 828         hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
 829 
 830     if (!the_template) {
 831         the_template = instance->hostt;
 832         first_instance = instance;
 833     }
 834         
 835 
 836 #ifdef USLEEP
 837     hostdata->time_expires = 0;
 838     hostdata->next_timer = NULL;
 839 #endif
 840 
 841 #ifndef AUTOSENSE
 842     if ((instance->cmd_per_lun > 1) || instance->can_queue > 1)) 
 843          printk("scsi%d : WARNING : support for multiple outstanding commands enabled\n"
 844                 "         without AUTOSENSE option, contingent allegiance conditions may\n"
 845                 "         be incorrectly cleared.\n", instance->host_no);
 846 #endif /* def AUTOSENSE */
 847 
 848     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
 849     NCR5380_write(MODE_REG, MR_BASE);
 850     NCR5380_write(TARGET_COMMAND_REG, 0);
 851     NCR5380_write(SELECT_ENABLE_REG, 0);
 852 
 853 #ifdef NCR53C400
 854     if (hostdata->flags & FLAG_NCR53C400) {
 855         NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
 856     }
 857 #endif
 858 
 859     /* 
 860      * Detect and correct bus wedge problems.
 861      *
 862      * If the system crashed, it may have crashed in a state 
 863      * where a SCSI command was still executing, and the 
 864      * SCSI bus is not in a BUS FREE STATE.
 865      *
 866      * If this is the case, we'll try to abort the currently
 867      * established nexus which we know nothing about, and that
 868      * failing, do a hard reset of the SCSI bus 
 869      */
 870 
 871     for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) &&
 872           pass <= 6 ; ++pass) {
 873         switch (pass) {
 874         case 1:
 875         case 3:
 876         case 5:
 877             printk("scsi%d: SCSI bus busy, waiting up to five seconds\n",
 878                 instance->host_no);
 879             timeout = jiffies + 500;
 880             while (jiffies < timeout && (NCR5380_read(STATUS_REG) & SR_BSY));
 881             break;
 882         case 2:
 883             printk("scsi%d: bus busy, attempting abort\n",
 884                 instance->host_no);
 885             do_abort (instance);
 886             break;
 887         case 4:
 888             printk("scsi%d: bus busy, attempting reset\n",
 889                 instance->host_no);
 890             do_reset (instance);
 891             break;
 892         case 6:
 893             printk("scsi%d: bus locked solid or invalid override\n",
 894                 instance->host_no);
 895         }
 896     }
 897 }
 898 
 899 /* 
 900  * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd, 
 901  *      void (*done)(Scsi_Cmnd *)) 
 902  *
 903  * Purpose :  enqueues a SCSI command
 904  *
 905  * Inputs : cmd - SCSI command, done - function called on completion, with
 906  *      a pointer to the command descriptor.
 907  * 
 908  * Returns : 0
 909  *
 910  * Side effects : 
 911  *      cmd is added to the per instance issue_queue, with minor 
 912  *      twiddling done to the host specific fields of cmd.  If the 
 913  *      main coroutine is not running, it is restarted.
 914  *
 915  */
 916 
 917 /* Only make static if a wrapper function is used */
 918 #ifndef NCR5380_queue_command
 919 static
 920 #endif
 921 int NCR5380_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *)) {
     /* [previous][next][first][last][top][bottom][index][help] */
 922     struct Scsi_Host *instance = cmd->host;
 923     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
 924         instance->hostdata;
 925     Scsi_Cmnd *tmp;
 926 
 927 #if (NDEBUG & NDEBUG_NO_WRITE)
 928     switch (cmd->cmnd[0]) {
 929     case WRITE:
 930     case WRITE_6:
 931     case WRITE_10:
 932         printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
 933             instance->host_no);
 934         cmd->result = (DID_ERROR << 16);
 935         done(cmd);
 936         return 0;
 937     }
 938 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
 939 
 940 #ifdef NCR5380_STATS
 941 # if 0
 942     if (!hostdata->connected && !hostdata->issue_queue &&
 943         !hostdata->disconnected_queue) {
 944         hostdata->timebase = jiffies;
 945     }
 946 # endif
 947 # ifdef NCR5380_STAT_LIMIT
 948     if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
 949 # endif
 950         switch (cmd->cmnd[0])
 951         {
 952             case WRITE:
 953             case WRITE_6:
 954             case WRITE_10:
 955                 hostdata->time_write[cmd->target] -= (jiffies - hostdata->timebase);
 956                 hostdata->bytes_write[cmd->target] += cmd->request_bufflen;
 957                 hostdata->pendingw++;
 958                 break;
 959             case READ:
 960             case READ_6:
 961             case READ_10:
 962                 hostdata->time_read[cmd->target] -= (jiffies - hostdata->timebase);
 963                 hostdata->bytes_read[cmd->target] += cmd->request_bufflen;
 964                 hostdata->pendingr++;
 965                 break;
 966         }
 967 #endif
 968 
 969     /* 
 970      * We use the host_scribble field as a pointer to the next command  
 971      * in a queue 
 972      */
 973 
 974     cmd->host_scribble = NULL;
 975     cmd->scsi_done = done;
 976 
 977     cmd->result = 0;
 978 
 979 
 980     /* 
 981      * Insert the cmd into the issue queue. Note that REQUEST SENSE 
 982      * commands are added to the head of the queue since any command will
 983      * clear the contingent allegiance condition that exists and the 
 984      * sense data is only guaranteed to be valid while the condition exists.
 985      */
 986 
 987     cli();
 988     if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
 989         LIST(cmd, hostdata->issue_queue);
 990         cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
 991         hostdata->issue_queue = cmd;
 992     } else {
 993         for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble; 
 994                 tmp = (Scsi_Cmnd *) tmp->host_scribble);
 995         LIST(cmd, tmp);
 996         tmp->host_scribble = (unsigned char *) cmd;
 997     }
 998 #if (NDEBUG & NDEBUG_QUEUES)
 999     printk("scsi%d : command added to %s of queue\n", instance->host_no,
1000         (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
1001 #endif
1002 
1003 /* Run the coroutine if it isn't already running. */
1004     run_main();
1005     return 0;
1006 }
1007 
1008 /*
1009  * Function : NCR5380_main (void) 
1010  *
1011  * Purpose : NCR5380_main is a coroutine that runs as long as more work can 
1012  *      be done on the NCR5380 host adapters in a system.  Both 
1013  *      NCR5380_queue_command() and NCR5380_intr() will try to start it 
1014  *      in case it is not running.
1015  * 
1016  * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should 
1017  *  reenable them.  This prevents reentrancy and kernel stack overflow.
1018  */     
1019     
1020 static void NCR5380_main (void) {
     /* [previous][next][first][last][top][bottom][index][help] */
1021     Scsi_Cmnd *tmp, *prev;
1022     struct Scsi_Host *instance;
1023     struct NCR5380_hostdata *hostdata;
1024     int done;
1025 
1026     /*
1027      * We run (with interrupts disabled) until we're sure that none of 
1028      * the host adapters have anything that can be done, at which point 
1029      * we set main_running to 0 and exit.
1030      *
1031      * Interrupts are enabled before doing various other internal 
1032      * instructions, after we've decided that we need to run through
1033      * the loop again.
1034      *
1035      * this should prevent any race conditions.
1036      */
1037 
1038     do {
1039         cli(); /* Freeze request queues */
1040         done = 1;
1041         for (instance = first_instance; instance && 
1042             instance->hostt == the_template; instance=instance->next) {
1043             hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1044             cli();
1045             if (!hostdata->connected) {
1046 #if (NDEBUG & NDEBUG_MAIN)
1047                 printk("scsi%d : not connected\n", instance->host_no);
1048 #endif
1049                 /*
1050                  * Search through the issue_queue for a command destined
1051                  * for a target that's not busy.
1052                  */
1053 #if (NDEBUG & NDEBUG_LISTS)
1054                 for (tmp= (Scsi_Cmnd *) hostdata->issue_queue, prev=NULL; tmp && (tmp != prev); prev=tmp, tmp=(Scsi_Cmnd*)tmp->host_scribble)
1055                     ;
1056                     /*printk("%p  ", tmp);*/
1057                 if ((tmp == prev) && tmp) printk(" LOOP\n");/* else printk("\n");*/
1058 #endif
1059                 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, 
1060                     prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) 
1061                     tmp->host_scribble) {
1062 
1063 #if (NDEBUG & NDEBUG_LISTS)
1064                     if (prev != tmp)
1065                         printk("MAIN tmp=%p   target=%d   busy=%d lun=%d\n", tmp, tmp->target, hostdata->busy[tmp->target], tmp->lun);
1066 #endif
1067                     /*  When we find one, remove it from the issue queue. */
1068                     if (!(hostdata->busy[tmp->target] & (1 << tmp->lun))) {
1069                         if (prev) {
1070                             REMOVE(prev,prev->host_scribble,tmp,tmp->host_scribble);
1071                             prev->host_scribble = tmp->host_scribble;
1072                         } else {
1073                             REMOVE(-1,hostdata->issue_queue,tmp,tmp->host_scribble);
1074                             hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
1075                         }
1076                         tmp->host_scribble = NULL;
1077 
1078                         /* reenable interrupts after finding one */
1079                         sti();
1080 
1081                         /* 
1082                          * Attempt to establish an I_T_L nexus here. 
1083                          * On success, instance->hostdata->connected is set.
1084                          * On failure, we must add the command back to the
1085                          *   issue queue so we can keep trying. 
1086                          */
1087 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1088                         printk("scsi%d : main() : command for target %d lun %d removed from issue_queue\n",
1089                             instance->host_no, tmp->target, tmp->lun);
1090 #endif
1091                 
1092                         /*
1093                          * A successful selection is defined as one that 
1094                          * leaves us with the command connected and 
1095                          * in hostdata->connected, OR has terminated the
1096                          * command.
1097                          *
1098                          * With successfull commands, we fall through
1099                          * and see if we can do an information transfer,
1100                          * with failures we will restart.
1101                          */
1102 
1103                         if (!NCR5380_select(instance, tmp, 
1104                         /* 
1105                          * REQUEST SENSE commands are issued without tagged
1106                          * queueing, even on SCSI-II devices because the 
1107                          * contingent allegiance condition exists for the 
1108                          * entire unit.
1109                          */
1110                             (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : 
1111                             TAG_NEXT)) {
1112                             break;
1113                         } else {
1114                             cli();
1115                             LIST(tmp, hostdata->issue_queue);
1116                             tmp->host_scribble = (unsigned char *) 
1117                                 hostdata->issue_queue;
1118                             hostdata->issue_queue = tmp;
1119                             done = 0;
1120                             sti();
1121 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1122                         printk("scsi%d : main(): select() failed, returned to issue_queue\n",
1123                             instance->host_no);
1124 #endif
1125                         }
1126                     } /* if target/lun is not busy */
1127                 } /* for */
1128             } /* if (!hostdata->connected) */
1129                 
1130             if (hostdata->connected 
1131 #ifdef REAL_DMA
1132                 && !hostdata->dmalen
1133 #endif
1134 #ifdef USLEEP
1135                 && (!hostdata->time_expires || hostdata->time_expires >= jiffies)
1136 #endif
1137                 ) {
1138                 sti();
1139 #if (NDEBUG & NDEBUG_MAIN)
1140                 printk("scsi%d : main() : performing information transfer\n",
1141                         instance->host_no);
1142 #endif
1143                 NCR5380_information_transfer(instance);
1144 #if (NDEBUG & NDEBUG_MAIN)
1145                 printk("scsi%d : main() : done set false\n", instance->host_no);
1146 #endif
1147                 done = 0;
1148             } else 
1149                 break;
1150         } /* for instance */
1151     } while (!done);
1152     main_running = 0;
1153 }
1154 
1155 /*
1156  * Function : void NCR5380_intr (int irq)
1157  * 
1158  * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1159  *      from the disconnected queue, and restarting NCR5380_main() 
1160  *      as required.
1161  *
1162  * Inputs : int irq, irq that caused this interrupt.
1163  *
1164  */
1165 
1166 static void NCR5380_intr (int irq, void *dev_id, struct pt_regs * regs) {
     /* [previous][next][first][last][top][bottom][index][help] */
1167     NCR5380_local_declare(); 
1168     struct Scsi_Host *instance;
1169     int done;
1170     unsigned char basr;
1171 #if (NDEBUG & NDEBUG_INTR)
1172     printk("scsi : NCR5380 irq %d triggered\n", irq);
1173 #endif
1174     do {
1175         done = 1;
1176         for (instance = first_instance; instance && (instance->hostt == 
1177             the_template); instance = instance->next)
1178             if (instance->irq == irq) {
1179                 
1180                 /* Look for pending interrupts */
1181                 NCR5380_setup(instance);
1182                 basr = NCR5380_read(BUS_AND_STATUS_REG);
1183                 /* XXX dispatch to appropriate routine if found and done=0 */
1184                 if (basr & BASR_IRQ) {
1185 #if (NDEBUG & NDEBUG_INTR)
1186                     NCR5380_print(instance);
1187 #endif
1188                     if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == 
1189                         (SR_SEL | SR_IO)) {
1190                         done = 0;
1191                         sti();
1192 #if (NDEBUG & NDEBUG_INTR)
1193                         printk("scsi%d : SEL interrupt\n", instance->host_no);
1194 #endif
1195                         NCR5380_reselect(instance);
1196                         (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1197                     } else if (basr & BASR_PARITY_ERROR) {
1198 #if (NDEBUG & NDEBUG_INTR)
1199                         printk("scsi%d : PARITY interrupt\n", instance->host_no);
1200 #endif
1201                         (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1202                     } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1203 #if (NDEBUG & NDEBUG_INTR)
1204                         printk("scsi%d : RESET interrupt\n", instance->host_no);
1205 #endif
1206                         (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1207                     } else {
1208 /*  
1209  * XXX the rest of the interrupt conditions should *only* occur during a 
1210  * DMA transfer, which I haven't gotten around to fixing yet.
1211  */
1212 
1213 #if defined(REAL_DMA)
1214                     /*
1215                      * We should only get PHASE MISMATCH and EOP interrupts
1216                      * if we have DMA enabled, so do a sanity check based on
1217                      * the current setting of the MODE register.
1218                      */
1219 
1220                         if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & 
1221                             BASR_END_DMA_TRANSFER) || 
1222                             !(basr & BASR_PHASE_MATCH))) {
1223                             int transfered;
1224 
1225                             if (!hostdata->connected) 
1226                                 panic("scsi%d : received end of DMA interrupt with no connected cmd\n",
1227                                     instance->hostno);
1228 
1229                             transfered = (hostdata->dmalen - NCR5380_dma_residual(instance));
1230                             hostdata->connected->SCp.this_residual -= transferred;
1231                             hostdata->connected->SCp.ptr += transferred;
1232                             hostdata->dmalen = 0;
1233 
1234                             (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1235 #if NCR_TIMEOUT
1236                             {
1237                               unsigned long timeout = jiffies + NCR_TIMEOUT;
1238 
1239                               while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK
1240                                      && jiffies < timeout)
1241                                 ;
1242                               if (jiffies >= timeout)
1243                                 printk("scsi%d: timeout at NCR5380.c:%d\n", 
1244                                     host->host_no, __LINE__);
1245                             }
1246 #else /* NCR_TIMEOUT */
1247                             while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
1248 #endif
1249 
1250                             NCR5380_write(MODE_REG, MR_BASE);
1251                             NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1252                         }
1253 #else
1254 #if (NDEBUG & NDEBUG_INTR)
1255                     printk("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1256 #endif
1257                     (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1258 #endif
1259                     } 
1260                 } /* if BASR_IRQ */
1261                 if (!done) 
1262                     run_main();
1263             } /* if (instance->irq == irq) */
1264     } while (!done);
1265 }
1266 
1267 #ifdef NCR5380_STATS
1268 static void collect_stats(struct NCR5380_hostdata* hostdata, Scsi_Cmnd* cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1269 {
1270 # ifdef NCR5380_STAT_LIMIT
1271     if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
1272 # endif
1273         switch (cmd->cmnd[0])
1274         {
1275             case WRITE:
1276             case WRITE_6:
1277             case WRITE_10:
1278                 hostdata->time_write[cmd->target] += (jiffies - hostdata->timebase);
1279                 /*hostdata->bytes_write[cmd->target] += cmd->request_bufflen;*/
1280                 hostdata->pendingw--;
1281                 break;
1282             case READ:
1283             case READ_6:
1284             case READ_10:
1285                 hostdata->time_read[cmd->target] += (jiffies - hostdata->timebase);
1286                 /*hostdata->bytes_read[cmd->target] += cmd->request_bufflen;*/
1287                 hostdata->pendingr--;
1288                 break;
1289         }
1290 }
1291 #endif
1292 
1293 /* 
1294  * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, 
1295  *      int tag);
1296  *
1297  * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1298  *      including ARBITRATION, SELECTION, and initial message out for 
1299  *      IDENTIFY and queue messages. 
1300  *
1301  * Inputs : instance - instantiation of the 5380 driver on which this 
1302  *      target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for 
1303  *      new tag, TAG_NONE for untagged queueing, otherwise set to the tag for 
1304  *      the command that is presently connected.
1305  * 
1306  * Returns : -1 if selection could not execute for some reason,
1307  *      0 if selection succeeded or failed because the target 
1308  *      did not respond.
1309  *
1310  * Side effects : 
1311  *      If bus busy, arbitration failed, etc, NCR5380_select() will exit 
1312  *              with registers as they should have been on entry - ie
1313  *              SELECT_ENABLE will be set appropriately, the NCR5380
1314  *              will cease to drive any SCSI bus signals.
1315  *
1316  *      If successful : I_T_L or I_T_L_Q nexus will be established, 
1317  *              instance->connected will be set to cmd.  
1318  *              SELECT interrupt will be disabled.
1319  *
1320  *      If failed (no target) : cmd->scsi_done() will be called, and the 
1321  *              cmd->result host byte set to DID_BAD_TARGET.
1322  */
1323 
1324 static int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
1325     int tag) {
1326     NCR5380_local_declare();
1327     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata*) 
1328         instance->hostdata;
1329     unsigned char tmp[3], phase;
1330     unsigned char *data;
1331     int len;
1332     unsigned long timeout;
1333     NCR5380_setup(instance);
1334 
1335     hostdata->restart_select = 0;
1336 #if defined (NDEBUG) && (NDEBUG & NDEBUG_ARBITRATION) 
1337     NCR5380_print(instance);
1338     printk("scsi%d : starting arbitration, id = %d\n", instance->host_no,
1339         instance->this_id);
1340 #endif
1341     cli(); 
1342 
1343     /* 
1344      * Set the phase bits to 0, otherwise the NCR5380 won't drive the 
1345      * data bus during SELECTION.
1346      */
1347 
1348     NCR5380_write(TARGET_COMMAND_REG, 0);
1349 
1350 
1351     /* 
1352      * Start arbitration.
1353      */
1354     
1355     NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1356     NCR5380_write(MODE_REG, MR_ARBITRATE);
1357 
1358     sti();
1359 
1360     /* Wait for arbitration logic to complete */
1361 #if NCR_TIMEOUT
1362     {
1363       unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
1364 
1365       while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1366            && jiffies < timeout)
1367         ;
1368       if (jiffies >= timeout)
1369       {
1370         printk("scsi: arbitration timeout at %d\n", __LINE__);
1371         NCR5380_write(MODE_REG, MR_BASE);
1372         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1373         return -1;
1374       }
1375     }
1376 #else /* NCR_TIMEOUT */
1377     while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS));
1378 #endif
1379 
1380 #if (NDEBUG & NDEBUG_ARBITRATION) 
1381     printk("scsi%d : arbitration complete\n", instance->host_no);
1382 /* Avoid GCC 2.4.5 asm needs to many reloads error */
1383     __asm__("nop");
1384 #endif
1385 
1386     /* 
1387      * The arbitration delay is 2.2us, but this is a minimum and there is 
1388      * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1389      * the integral nature of udelay().
1390      *
1391      */
1392 
1393     udelay(3);
1394 
1395     /* Check for lost arbitration */
1396     if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1397         (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1398         (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1399         NCR5380_write(MODE_REG, MR_BASE); 
1400 #if (NDEBUG & NDEBUG_ARBITRATION)
1401     printk("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", 
1402         instance->host_no);
1403 #endif
1404         return -1;
1405     }
1406 
1407 
1408 
1409     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
1410     
1411     if (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) {
1412         NCR5380_write(MODE_REG, MR_BASE);
1413         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1414 #if (NDEBUG & NDEBUG_ARBITRATION)
1415     printk("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", 
1416         instance->host_no);
1417 #endif
1418         return -1;
1419     }
1420 
1421     /* 
1422      * Again, bus clear + bus settle time is 1.2us, however, this is 
1423      * a minimum so we'll udelay ceil(1.2)
1424      */
1425 
1426     udelay(2);  
1427 
1428 #if (NDEBUG & NDEBUG_ARBITRATION)
1429     printk("scsi%d : won arbitration\n", instance->host_no);
1430 #endif
1431 
1432 
1433     /* 
1434      * Now that we have won arbitration, start Selection process, asserting 
1435      * the host and target ID's on the SCSI bus.
1436      */
1437 
1438     NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
1439 
1440     /* 
1441      * Raise ATN while SEL is true before BSY goes false from arbitration,
1442      * since this is the only way to guarantee that we'll get a MESSAGE OUT
1443      * phase immediately after selection.
1444      */
1445 
1446     NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | 
1447         ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
1448     NCR5380_write(MODE_REG, MR_BASE);
1449 
1450     /* 
1451      * Reselect interrupts must be turned off prior to the dropping of BSY,
1452      * otherwise we will trigger an interrupt.
1453      */
1454     NCR5380_write(SELECT_ENABLE_REG, 0);
1455 
1456     /*
1457      * The initiator shall then wait at least two deskew delays and release 
1458      * the BSY signal.
1459      */
1460     udelay(1);        /* wingel -- wait two bus deskew delay >2*45ns */
1461 
1462     /* Reset BSY */
1463     NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | 
1464         ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1465 
1466     /* 
1467      * Something weird happens when we cease to drive BSY - looks
1468      * like the board/chip is letting us do another read before the 
1469      * appropriate propagation delay has expired, and we're confusing
1470      * a BSY signal from ourselves as the target's response to SELECTION.
1471      *
1472      * A small delay (the 'C++' frontend breaks the pipeline with an
1473      * unnecessary jump, making it work on my 386-33/Trantor T128, the
1474      * tighter 'C' code breaks and requires this) solves the problem - 
1475      * the 1 us delay is arbitrary, and only used because this delay will 
1476      * be the same on other platforms and since it works here, it should 
1477      * work there.
1478      *
1479      * wingel suggests that this could be due to failing to wait
1480      * one deskew delay.
1481      */
1482 
1483     udelay(1);
1484 
1485 #if (NDEBUG & NDEBUG_SELECTION)
1486     printk("scsi%d : selecting target %d\n", instance->host_no, cmd->target);
1487 #endif
1488 
1489     /* 
1490      * The SCSI specification calls for a 250 ms timeout for the actual 
1491      * selection.
1492      */
1493 
1494     timeout = jiffies + 25; 
1495 
1496     /* 
1497      * XXX very interesting - we're seeing a bounce where the BSY we 
1498      * asserted is being reflected / still asserted (propagation delay?)
1499      * and it's detecting as true.  Sigh.
1500      */
1501 
1502     while ((jiffies < timeout) && !(NCR5380_read(STATUS_REG) & 
1503         (SR_BSY | SR_IO)));
1504 
1505     if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == 
1506             (SR_SEL | SR_IO)) {
1507             NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1508             NCR5380_reselect(instance);
1509             printk ("scsi%d : reselection after won arbitration?\n",
1510                 instance->host_no);
1511             NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1512             return -1;
1513     }
1514 
1515     /* 
1516      * No less than two deskew delays after the initiator detects the 
1517      * BSY signal is true, it shall release the SEL signal and may 
1518      * change the DATA BUS.                                     -wingel
1519      */
1520 
1521     udelay(1);
1522 
1523     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1524 
1525     if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1526         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1527         if (hostdata->targets_present & (1 << cmd->target)) {
1528             printk("scsi%d : weirdness\n", instance->host_no);
1529             if (hostdata->restart_select)
1530                 printk("\trestart select\n");
1531 #ifdef NDEBUG
1532             NCR5380_print (instance);
1533 #endif
1534             NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1535             return -1;
1536         }
1537         cmd->result = DID_BAD_TARGET << 16;
1538 #ifdef NCR5380_STATS
1539         collect_stats(hostdata, cmd);
1540 #endif
1541         cmd->scsi_done(cmd);
1542         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1543 #if (NDEBUG & NDEBUG_SELECTION)
1544         printk("scsi%d : target did not respond within 250ms\n", 
1545             instance->host_no);
1546 #endif
1547         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1548         return 0;
1549     } 
1550 
1551     hostdata->targets_present |= (1 << cmd->target);
1552 
1553     /*
1554      * Since we followed the SCSI spec, and raised ATN while SEL 
1555      * was true but before BSY was false during selection, the information
1556      * transfer phase should be a MESSAGE OUT phase so that we can send the
1557      * IDENTIFY message.
1558      * 
1559      * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1560      * message (2 bytes) with a tag ID that we increment with every command
1561      * until it wraps back to 0.
1562      *
1563      * XXX - it turns out that there are some broken SCSI-II devices,
1564      *       which claim to support tagged queuing but fail when more than
1565      *       some number of commands are issued at once.
1566      */
1567 
1568     /* Wait for start of REQ/ACK handshake */
1569 #ifdef NCR_TIMEOUT
1570     {
1571       unsigned long timeout = jiffies + NCR_TIMEOUT;
1572  
1573       while (!(NCR5380_read(STATUS_REG) & SR_REQ) && jiffies < timeout);
1574 
1575       if (jiffies >= timeout) {
1576         printk("scsi%d: timeout at NCR5380.c:%d\n", __LINE__);
1577         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1578         return -1;
1579       }
1580     }
1581 #else /* NCR_TIMEOUT */
1582     while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1583 #endif /* def NCR_TIMEOUT */
1584 
1585 #if (NDEBUG & NDEBUG_SELECTION)
1586     printk("scsi%d : target %d selected, going into MESSAGE OUT phase.\n",
1587         instance->host_no, cmd->target);
1588 #endif
1589     tmp[0] = IDENTIFY(((instance->irq == IRQ_NONE) ? 0 : 1), cmd->lun);
1590 #ifdef SCSI2
1591     if (cmd->device->tagged_queue && (tag != TAG_NONE)) {
1592         tmp[1] = SIMPLE_QUEUE_TAG;
1593         if (tag == TAG_NEXT) {
1594             /* 0 is TAG_NONE, used to imply no tag for this command */
1595             if (cmd->device->current_tag == 0)
1596                 cmd->device->current_tag = 1;
1597 
1598             cmd->tag = cmd->device->current_tag;
1599             cmd->device->current_tag++;
1600         } else  
1601             cmd->tag = (unsigned char) tag;
1602 
1603         tmp[2] = cmd->tag;
1604         hostdata->last_message = SIMPLE_QUEUE_TAG;
1605         len = 3;
1606     } else 
1607 #endif /* def SCSI2 */
1608     {
1609         len = 1;
1610         cmd->tag=0;
1611     }
1612 
1613     /* Send message(s) */
1614     data = tmp;
1615     phase = PHASE_MSGOUT;
1616     NCR5380_transfer_pio(instance, &phase, &len, &data);
1617 #if (NDEBUG & NDEBUG_SELECTION)
1618     printk("scsi%d : nexus established.\n", instance->host_no);
1619 #endif
1620     /* XXX need to handle errors here */
1621     hostdata->connected = cmd;
1622 #ifdef SCSI2
1623     if (!cmd->device->tagged_queue)
1624 #endif    
1625         hostdata->busy[cmd->target] |= (1 << cmd->lun);
1626 
1627     initialize_SCp(cmd);
1628 
1629 
1630     return 0;
1631 }
1632 
1633 /* 
1634  * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance, 
1635  *      unsigned char *phase, int *count, unsigned char **data)
1636  *
1637  * Purpose : transfers data in given phase using polled I/O
1638  *
1639  * Inputs : instance - instance of driver, *phase - pointer to 
1640  *      what phase is expected, *count - pointer to number of 
1641  *      bytes to transfer, **data - pointer to data pointer.
1642  * 
1643  * Returns : -1 when different phase is entered without transferring
1644  *      maximum number of bytes, 0 if all bytes or transfered or exit
1645  *      is in same phase.
1646  *
1647  *      Also, *phase, *count, *data are modified in place.
1648  *
1649  * XXX Note : handling for bus free may be useful.
1650  */
1651 
1652 /*
1653  * Note : this code is not as quick as it could be, however it 
1654  * IS 100% reliable, and for the actual data transfer where speed
1655  * counts, we will always do a pseudo DMA or DMA transfer.
1656  */
1657 
1658 static int NCR5380_transfer_pio (struct Scsi_Host *instance, 
     /* [previous][next][first][last][top][bottom][index][help] */
1659         unsigned char *phase, int *count, unsigned char **data) {
1660     NCR5380_local_declare();
1661     register unsigned char p = *phase, tmp;
1662     register int c = *count;
1663     register unsigned char *d = *data;
1664     NCR5380_setup(instance);
1665 
1666 #if (NDEBUG & NDEBUG_PIO)
1667     if (!(p & SR_IO))
1668       printk("scsi%d : pio write %d bytes\n", instance->host_no, c);
1669     else
1670       printk("scsi%d : pio read %d bytes\n", instance->host_no, c);
1671 #endif
1672 
1673     /* 
1674      * The NCR5380 chip will only drive the SCSI bus when the 
1675      * phase specified in the appropriate bits of the TARGET COMMAND
1676      * REGISTER match the STATUS REGISTER
1677      */
1678 
1679     NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1680 
1681     do {
1682         /* 
1683          * Wait for assertion of REQ, after which the phase bits will be 
1684          * valid 
1685          */
1686         while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ));
1687 
1688 #if (NDEBUG & NDEBUG_HANDSHAKE)
1689         printk("scsi%d : REQ detected\n", instance->host_no);
1690 #endif
1691 
1692         /* Check for phase mismatch */  
1693         if ((tmp & PHASE_MASK) != p) {
1694 #if (NDEBUG & NDEBUG_PIO)
1695             printk("scsi%d : phase mismatch\n", instance->host_no);
1696             NCR5380_print_phase(instance);
1697 #endif
1698             break;
1699         }
1700 
1701         /* Do actual transfer from SCSI bus to / from memory */
1702         if (!(p & SR_IO)) 
1703             NCR5380_write(OUTPUT_DATA_REG, *d);
1704         else 
1705             *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1706 
1707         ++d;
1708 
1709         /* 
1710          * The SCSI standard suggests that in MSGOUT phase, the initiator
1711          * should drop ATN on the last byte of the message phase
1712          * after REQ has been asserted for the handshake but before
1713          * the initiator raises ACK.
1714          */
1715 
1716         if (!(p & SR_IO)) {
1717             if (!((p & SR_MSG) && c > 1)) {
1718                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
1719                     ICR_ASSERT_DATA);
1720 #if (NDEBUG & NDEBUG_PIO)
1721         NCR5380_print(instance);
1722 #endif
1723                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
1724                         ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1725             } else {
1726                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1727                     ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1728 #if (NDEBUG & NDEBUG_PIO)
1729         NCR5380_print(instance);
1730 #endif
1731                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
1732                     ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1733             }
1734         } else {
1735 #if (NDEBUG & NDEBUG_PIO)
1736         NCR5380_print(instance);
1737 #endif
1738         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1739         }
1740 
1741         while (NCR5380_read(STATUS_REG) & SR_REQ);
1742 
1743 #if (NDEBUG & NDEBUG_HANDSHAKE)
1744             printk("scsi%d : req false, handshake complete\n", instance->host_no);
1745 #endif
1746 
1747 /*
1748  * We have several special cases to consider during REQ/ACK handshaking : 
1749  * 1.  We were in MSGOUT phase, and we are on the last byte of the 
1750  *      message.  ATN must be dropped as ACK is dropped.
1751  *
1752  * 2.  We are in a MSGIN phase, and we are on the last byte of the  
1753  *      message.  We must exit with ACK asserted, so that the calling
1754  *      code may raise ATN before dropping ACK to reject the message.
1755  *
1756  * 3.  ACK and ATN are clear and the target may proceed as normal.
1757  */
1758         if (!(p == PHASE_MSGIN && c == 1)) {  
1759             if (p == PHASE_MSGOUT && c > 1)
1760                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1761             else
1762                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1763         } 
1764     } while (--c);
1765 
1766 #if (NDEBUG & NDEBUG_PIO) 
1767     printk("scsi%d : residual %d\n", instance->host_no, c);
1768 #endif
1769 
1770     *count = c;
1771     *data = d;
1772     tmp = NCR5380_read(STATUS_REG);
1773     if (tmp & SR_REQ)
1774         *phase = tmp & PHASE_MASK;
1775     else 
1776         *phase = PHASE_UNKNOWN;
1777 
1778     if (!c || (*phase == p))
1779         return 0;
1780     else 
1781         return -1;
1782 }
1783 
1784 static void do_reset (struct Scsi_Host *host) {
     /* [previous][next][first][last][top][bottom][index][help] */
1785     NCR5380_local_declare();
1786     NCR5380_setup(host);
1787 
1788     cli();
1789     NCR5380_write(TARGET_COMMAND_REG, 
1790         PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1791     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1792     udelay(25);
1793     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1794     sti();
1795 }
1796 
1797 /*
1798  * Function : do_abort (Scsi_Host *host)
1799  * 
1800  * Purpose : abort the currently established nexus.  Should only be 
1801  *      called from a routine which can drop into a 
1802  * 
1803  * Returns : 0 on success, -1 on failure.
1804  */
1805 
1806 static int do_abort (struct Scsi_Host *host) {
     /* [previous][next][first][last][top][bottom][index][help] */
1807     NCR5380_local_declare();
1808     unsigned char tmp, *msgptr, phase;
1809     int len;
1810     NCR5380_setup(host);
1811 
1812 
1813     /* Request message out phase */
1814     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1815 
1816     /* 
1817      * Wait for the target to indicate a valid phase by asserting 
1818      * REQ.  Once this happens, we'll have either a MSGOUT phase 
1819      * and can immediately send the ABORT message, or we'll have some 
1820      * other phase and will have to source/sink data.
1821      * 
1822      * We really don't care what value was on the bus or what value
1823      * the target see's, so we just handshake.
1824      */
1825     
1826     while (!(tmp = NCR5380_read(STATUS_REG)) & SR_REQ);
1827 
1828     NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1829 
1830     if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1831         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | 
1832             ICR_ASSERT_ACK);
1833         while (NCR5380_read(STATUS_REG) & SR_REQ);
1834         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1835     }
1836    
1837     tmp = ABORT;
1838     msgptr = &tmp;
1839     len = 1;
1840     phase = PHASE_MSGOUT;
1841     NCR5380_transfer_pio (host, &phase, &len, &msgptr);
1842 
1843     /*
1844      * If we got here, and the command completed successfully,
1845      * we're about to go into bus free state.
1846      */
1847 
1848     return len ? -1 : 0;
1849 }
1850 
1851 #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1852 /* 
1853  * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance, 
1854  *      unsigned char *phase, int *count, unsigned char **data)
1855  *
1856  * Purpose : transfers data in given phase using either real
1857  *      or pseudo DMA.
1858  *
1859  * Inputs : instance - instance of driver, *phase - pointer to 
1860  *      what phase is expected, *count - pointer to number of 
1861  *      bytes to transfer, **data - pointer to data pointer.
1862  * 
1863  * Returns : -1 when different phase is entered without transferring
1864  *      maximum number of bytes, 0 if all bytes or transfered or exit
1865  *      is in same phase.
1866  *
1867  *      Also, *phase, *count, *data are modified in place.
1868  *
1869  */
1870 
1871 
1872 static int NCR5380_transfer_dma (struct Scsi_Host *instance, 
     /* [previous][next][first][last][top][bottom][index][help] */
1873     unsigned char *phase, int *count, unsigned char **data) {
1874     NCR5380_local_declare();
1875     register int c = *count;
1876     register unsigned char p = *phase;
1877     register unsigned char *d = *data;
1878     unsigned char tmp;
1879     int foo;
1880 #if defined(REAL_DMA_POLL)
1881     int cnt, toPIO;
1882     unsigned char saved_data = 0, overrun = 0, residue;
1883 #endif
1884 
1885     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) 
1886         instance->hostdata;
1887 
1888     NCR5380_setup(instance);
1889 
1890     if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1891         *phase = tmp;
1892         return -1;
1893     }
1894 #if defined(REAL_DMA) || defined(REAL_DMA_POLL) 
1895 #ifdef READ_OVERRUNS
1896      if (p & SR_IO) {
1897        c -= 2;
1898      }
1899 #endif
1900 #if (NDEBUG & NDEBUG_DMA)
1901     printk("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n",
1902         instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" :
1903         "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
1904 #endif
1905     hostdata->dma_len = (p & SR_IO) ?
1906         NCR5380_dma_read_setup(instance, d, c) : 
1907         NCR5380_dma_write_setup(instance, d, c);
1908 #endif
1909 
1910     NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1911 
1912 #ifdef REAL_DMA
1913     NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1914 #elif defined(REAL_DMA_POLL)
1915     NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1916 #else
1917     /*
1918      * Note : on my sample board, watch-dog timeouts occurred when interrupts
1919      * were not disabled for the duration of a single DMA transfer, from 
1920      * before the setting of DMA mode to after transfer of the last byte.
1921      */
1922 
1923 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
1924     cli();
1925 #endif
1926     /* KLL May need eop and parity in 53c400 */
1927     if (hostdata->flags & FLAG_NCR53C400)
1928         NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_PAR_CHECK
1929         | MR_ENABLE_PAR_INTR | MR_ENABLE_EOP_INTR | MR_DMA_MODE
1930         | MR_MONITOR_BSY);
1931     else
1932         NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1933 #endif /* def REAL_DMA */
1934 
1935 #if (NDEBUG & NDEBUG_DMA) & 0
1936     printk("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
1937 #endif
1938 
1939 /* 
1940  * FOO stuff. For some UNAPPARENT reason, I'm getting 
1941  * watchdog timers fired on bootup for NO APPARENT REASON, meaning it's
1942  * probably a timing problem.
1943  *
1944  * Since this is the only place I have back-to-back writes, perhaps this 
1945  * is the problem?
1946  */
1947 
1948     if (p & SR_IO) {
1949 #ifndef FOO
1950         udelay(1);
1951 #endif
1952         NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1953     } else {
1954 #ifndef FOO
1955         udelay(1);
1956 #endif
1957         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1958 #ifndef FOO
1959         udelay(1);
1960 #endif
1961         NCR5380_write(START_DMA_SEND_REG, 0);
1962 #ifndef FOO
1963         udelay(1);
1964 #endif
1965     }
1966 
1967 #if defined(REAL_DMA_POLL)
1968     do {
1969         tmp = NCR5380_read(BUS_AND_STATUS_REG);
1970     } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | 
1971         BASR_END_DMA_TRANSFER)));
1972 
1973 /*
1974   At this point, either we've completed DMA, or we have a phase mismatch,
1975   or we've unexpectedly lost BUSY (which is a real error).
1976 
1977   For write DMAs, we want to wait until the last byte has been
1978   transferred out over the bus before we turn off DMA mode.  Alas, there
1979   seems to be no terribly good way of doing this on a 5380 under all
1980   conditions.  For non-scatter-gather operations, we can wait until REQ
1981   and ACK both go false, or until a phase mismatch occurs.  Gather-writes
1982   are nastier, since the device will be expecting more data than we
1983   are prepared to send it, and REQ will remain asserted.  On a 53C8[01] we
1984   could test LAST BIT SENT to assure transfer (I imagine this is precisely
1985   why this signal was added to the newer chips) but on the older 538[01]
1986   this signal does not exist.  The workaround for this lack is a watchdog;
1987   we bail out of the wait-loop after a modest amount of wait-time if
1988   the usual exit conditions are not met.  Not a terribly clean or
1989   correct solution :-%
1990 
1991   Reads are equally tricky due to a nasty characteristic of the NCR5380.
1992   If the chip is in DMA mode for an READ, it will respond to a target's
1993   REQ by latching the SCSI data into the INPUT DATA register and asserting
1994   ACK, even if it has _already_ been notified by the DMA controller that
1995   the current DMA transfer has completed!  If the NCR5380 is then taken
1996   out of DMA mode, this already-acknowledged byte is lost.
1997 
1998   This is not a problem for "one DMA transfer per command" reads, because
1999   the situation will never arise... either all of the data is DMA'ed
2000   properly, or the target switches to MESSAGE IN phase to signal a
2001   disconnection (either operation bringing the DMA to a clean halt).
2002   However, in order to handle scatter-reads, we must work around the
2003   problem.  The chosen fix is to DMA N-2 bytes, then check for the
2004   condition before taking the NCR5380 out of DMA mode.  One or two extra
2005   bytes are transferred via PIO as necessary to fill out the original
2006   request.
2007 */
2008 
2009     if (p & SR_IO) {
2010 #ifdef READ_OVERRUNS
2011       udelay(10);
2012       if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH|BASR_ACK)) ==
2013            (BASR_PHASE_MATCH | BASR_ACK))) {
2014         saved_data = NCR5380_read(INPUT_DATA_REGISTER);
2015         overrun = 1;
2016       }
2017 #endif
2018     } else {
2019       int limit = 100;
2020       while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) ||
2021             (NCR5380_read(STATUS_REG) & SR_REQ)) {
2022         if (!(tmp & BASR_PHASE_MATCH)) break;
2023         if (--limit < 0) break;
2024       }
2025     }
2026 
2027 
2028 #if (NDEBUG & NDEBUG_DMA)
2029     printk("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n",
2030            instance->host_no, tmp, NCR5380_read(STATUS_REG));
2031 #endif
2032 
2033     NCR5380_write(MODE_REG, MR_BASE);
2034     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2035 
2036     residue = NCR5380_dma_residual(instance);
2037     c -= residue;
2038     *count -= c;
2039     *data += c;
2040     *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2041 
2042 #ifdef READ_OVERRUNS
2043     if (*phase == p && (p & SR_IO) && residue == 0) {
2044       if (overrun) {
2045 #if (NDEBUG & NDEBUG_DMA)
2046         printk("Got an input overrun, using saved byte\n");
2047 #endif
2048         **data = saved_data;
2049         *data += 1;
2050         *count -= 1;
2051         cnt = toPIO = 1;
2052       } else {
2053         printk("No overrun??\n");
2054         cnt = toPIO = 2;
2055       }
2056 #if (NDEBUG & NDEBUG_DMA)
2057       printk("Doing %d-byte PIO to 0x%X\n", cnt, *data);
2058 #endif
2059       NCR5380_transfer_pio(instance, phase, &cnt, data);
2060       *count -= toPIO - cnt;
2061     }
2062 #endif        
2063 
2064 #if (NDEBUG & NDEBUG_DMA)
2065      printk("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n",
2066             *data, *count, *(*data+*count-1), *(*data+*count));
2067 #endif
2068      return 0;
2069      
2070 #elif defined(REAL_DMA)
2071     return 0;
2072 #else /* defined(REAL_DMA_POLL) */
2073     if (p & SR_IO) {
2074         int diff = 1;
2075         if (hostdata->flags & FLAG_NCR53C400) {
2076             diff=0;
2077         }
2078 
2079         if (!(foo = NCR5380_pread(instance, d, c - diff))) {
2080             /*
2081              * We can't disable DMA mode after successfully transferring 
2082              * what we plan to be the last byte, since that would open up
2083              * a race condition where if the target asserted REQ before 
2084              * we got the DMA mode reset, the NCR5380 would have latched
2085              * an additional byte into the INPUT DATA register and we'd
2086              * have dropped it.
2087              * 
2088              * The workaround was to transfer one fewer bytes than we 
2089              * intended to with the pseudo-DMA read function, wait for 
2090              * the chip to latch the last byte, read it, and then disable
2091              * pseudo-DMA mode.
2092              * 
2093              * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
2094              * REQ is deasserted when ACK is asserted, and not reasserted
2095              * until ACK goes false.  Since the NCR5380 won't lower ACK
2096              * until DACK is asserted, which won't happen unless we twiddle
2097              * the DMA port or we take the NCR5380 out of DMA mode, we 
2098              * can guarantee that we won't handshake another extra 
2099              * byte.
2100              */
2101 
2102             if (!(hostdata->flags & FLAG_NCR53C400)) {
2103                 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
2104                 /* Wait for clean handshake */
2105                 while (NCR5380_read(STATUS_REG) & SR_REQ);
2106                 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
2107             }
2108         }
2109     } else {
2110         int timeout;
2111 #if (NDEBUG & NDEBUG_C400_PWRITE)
2112         printk("About to pwrite %d bytes\n", c);
2113 #endif
2114         if (!(foo = NCR5380_pwrite(instance, d, c))) {
2115             /*
2116              * Wait for the last byte to be sent.  If REQ is being asserted for 
2117              * the byte we're interested, we'll ACK it and it will go false.  
2118              */
2119             if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
2120                 timeout = 20000;
2121 #if 1
2122 #if 1
2123                 while (!(NCR5380_read(BUS_AND_STATUS_REG) & 
2124                         BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) &
2125                         BASR_PHASE_MATCH));
2126 #else
2127                 if (NCR5380_read(STATUS_REG) & SR_REQ) {
2128                     for (; timeout && 
2129                         !(NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK); 
2130                         --timeout);
2131                     for (; timeout && (NCR5380_read(STATUS_REG) & SR_REQ);
2132                         --timeout);
2133                 } 
2134 #endif
2135         
2136 
2137 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2138                 if (!timeout) 
2139                     printk("scsi%d : timed out on last byte\n",
2140                             instance->host_no);
2141 #endif
2142 
2143 
2144                 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
2145                     hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
2146                     if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
2147                         hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
2148 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2149                         printk("scsi%d : last bit sent works\n", 
2150                             instance->host_no);
2151 #endif
2152                     }
2153                 }
2154             } else  {
2155 #if (NDEBUG & NDEBUG_C400_PWRITE)
2156                 printk("Waiting for LASTBYTE\n");
2157 #endif
2158                 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
2159 #if (NDEBUG & NDEBUG_C400_PWRITE)
2160                 printk("Got LASTBYTE\n");
2161 #endif
2162             }
2163 #else
2164             udelay (5);
2165 #endif
2166         }
2167     }
2168 
2169     NCR5380_write(MODE_REG, MR_BASE);
2170     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2171 
2172     if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
2173 #if (NDEBUG & NDEBUG_C400_PWRITE)
2174         printk("53C400w: Checking for IRQ\n");
2175 #endif
2176         if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
2177 #if (NDEBUG & NDEBUG_C400_PWRITE)
2178             printk("53C400w:    got it, reading reset interupt reg\n");
2179 #endif
2180             NCR5380_read(RESET_PARITY_INTERRUPT_REG);
2181         } else {
2182             printk("53C400w:    IRQ NOT THERE!\n");
2183         }
2184     }
2185 
2186     *data = d + c;
2187     *count = 0;
2188     *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2189 #if 0
2190     NCR5380_print_phase(instance);
2191 #endif
2192 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2193     sti();
2194 #endif /* defined(REAL_DMA_POLL) */
2195     return foo;
2196 #endif /* def REAL_DMA */
2197 }
2198 #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
2199 
2200 /*
2201  * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2202  *
2203  * Purpose : run through the various SCSI phases and do as the target 
2204  *      directs us to.  Operates on the currently connected command, 
2205  *      instance->connected.
2206  *
2207  * Inputs : instance, instance for which we are doing commands
2208  *
2209  * Side effects : SCSI things happen, the disconnected queue will be 
2210  *      modified if a command disconnects, *instance->connected will
2211  *      change.
2212  *
2213  * XXX Note : we need to watch for bus free or a reset condition here 
2214  *      to recover from an unexpected bus free condition.
2215  */
2216  
2217 static void NCR5380_information_transfer (struct Scsi_Host *instance) {
     /* [previous][next][first][last][top][bottom][index][help] */
2218     NCR5380_local_declare();
2219     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) 
2220         instance->hostdata;
2221     unsigned char msgout = NOP;
2222     int sink = 0;
2223     int len;
2224 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2225     int transfersize;
2226 #endif
2227     unsigned char *data;
2228     unsigned char phase, tmp, extended_msg[10], old_phase=0xff;
2229     Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
2230     NCR5380_setup(instance);
2231 
2232     while (1) {
2233         tmp = NCR5380_read(STATUS_REG);
2234         /* We only have a valid SCSI phase when REQ is asserted */
2235         if (tmp & SR_REQ) {
2236             phase = (tmp & PHASE_MASK); 
2237             if (phase != old_phase) {
2238                 old_phase = phase;
2239 #if (NDEBUG & NDEBUG_INFORMATION)
2240                 NCR5380_print_phase(instance);
2241 #endif
2242             }
2243             
2244             if (sink && (phase != PHASE_MSGOUT)) {
2245                 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2246 
2247                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | 
2248                     ICR_ASSERT_ACK);
2249                 while (NCR5380_read(STATUS_REG) & SR_REQ);
2250                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
2251                     ICR_ASSERT_ATN);
2252                 sink = 0;
2253                 continue;
2254             }
2255 
2256             switch (phase) {
2257             case PHASE_DATAIN:
2258             case PHASE_DATAOUT:
2259 #if (NDEBUG & NDEBUG_NO_DATAOUT)
2260                 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n",
2261                     instance->host_no);
2262                 sink = 1;
2263                 do_abort(instance);
2264                 cmd->result = DID_ERROR  << 16;
2265                 cmd->done(cmd);
2266                 return;
2267 #endif
2268                 /* 
2269                  * If there is no room left in the current buffer in the
2270                  * scatter-gather list, move onto the next one.
2271                  */
2272 
2273                 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2274                     ++cmd->SCp.buffer;
2275                     --cmd->SCp.buffers_residual;
2276                     cmd->SCp.this_residual = cmd->SCp.buffer->length;
2277                     cmd->SCp.ptr = cmd->SCp.buffer->address;
2278 #if (NDEBUG & NDEBUG_INFORMATION)
2279                     printk("scsi%d : %d bytes and %d buffers left\n",
2280                         instance->host_no, cmd->SCp.this_residual,
2281                         cmd->SCp.buffers_residual);
2282 #endif
2283                 }
2284 
2285                 /*
2286                  * The preferred transfer method is going to be 
2287                  * PSEUDO-DMA for systems that are strictly PIO,
2288                  * since we can let the hardware do the handshaking.
2289                  *
2290                  * For this to work, we need to know the transfersize
2291                  * ahead of time, since the pseudo-DMA code will sit
2292                  * in an unconditional loop.
2293                  */
2294 
2295 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2296                 /* KLL
2297                  * PSEUDO_DMA is defined here. If this is the g_NCR5380
2298                  * driver then it will always be defined, so the
2299                  * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
2300                  * NCR5380 case.  I think this is a fairly clean solution.
2301                  * We supplement these 2 if's with the flag.
2302                  */
2303 #ifdef NCR5380_dma_xfer_len
2304                 if (!cmd->device->borken &&
2305                     !(hostdata->flags & FLAG_NO_PSEUDO_DMA) &&
2306                     (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
2307 #else
2308                 transfersize = cmd->transfersize;
2309 
2310 #ifdef LIMIT_TRANSFERSIZE  /* If we have problems with interrupt service */
2311                 if( transfersize > 512 )
2312                     transfersize = 512;
2313 #endif  /* LIMIT_TRANSFERSIZE */
2314 
2315                 if (!cmd->device->borken && transfersize && 
2316                     !(hostdata->flags & FLAG_NO_PSEUDO_DMA) &&
2317                     cmd->SCp.this_residual && !(cmd->SCp.this_residual % 
2318                     transfersize)) {
2319 #endif
2320                     len = transfersize;
2321                     if (NCR5380_transfer_dma(instance, &phase,
2322                         &len, (unsigned char **) &cmd->SCp.ptr)) {
2323                         /*
2324                          * If the watchdog timer fires, all future accesses to this
2325                          * device will use the polled-IO.
2326                          */ 
2327                         printk("scsi%d : switching target %d lun %d to slow handshake\n",
2328                             instance->host_no, cmd->target, cmd->lun);
2329                         cmd->device->borken = 1;
2330                         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
2331                             ICR_ASSERT_ATN);
2332                         sink = 1;
2333                         do_abort(instance);
2334                         cmd->result = DID_ERROR  << 16;
2335                         cmd->done(cmd);
2336                         /* XXX - need to source or sink data here, as appropriate */
2337                     } else
2338                         cmd->SCp.this_residual -= transfersize - len;
2339                 } else
2340 #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2341                   NCR5380_transfer_pio(instance, &phase, 
2342                     (int *) &cmd->SCp.this_residual, (unsigned char **)
2343                     &cmd->SCp.ptr);
2344                 break;
2345             case PHASE_MSGIN:
2346                 len = 1;
2347                 data = &tmp;
2348                 NCR5380_transfer_pio(instance, &phase, &len, &data);
2349                 cmd->SCp.Message = tmp;
2350 
2351                 switch (tmp) {
2352                 /*
2353                  * Linking lets us reduce the time required to get the 
2354                  * next command out to the device, hopefully this will
2355                  * mean we don't waste another revolution due to the delays
2356                  * required by ARBITRATION and another SELECTION.
2357                  *
2358                  * In the current implementation proposal, low level drivers
2359                  * merely have to start the next command, pointed to by 
2360                  * next_link, done() is called as with unlinked commands.
2361                  */
2362 #ifdef LINKED
2363                 case LINKED_CMD_COMPLETE:
2364                 case LINKED_FLG_CMD_COMPLETE:
2365                     /* Accept message by clearing ACK */
2366                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2367                     
2368 #if (NDEBUG & NDEBUG_LINKED) 
2369                     printk("scsi%d : target %d lun %d linked command complete.\n",
2370                         instance->host_no, cmd->target, cmd->lun);
2371 #endif
2372                     /* 
2373                      * Sanity check : A linked command should only terminate with
2374                      * one of these messages if there are more linked commands
2375                      * available.
2376                      */
2377 
2378                     if (!cmd->next_link) {
2379                          printk("scsi%d : target %d lun %d linked command complete, no next_link\n"
2380                             instance->host_no, cmd->target, cmd->lun);
2381                             sink = 1;
2382                             do_abort (instance);
2383                             return;
2384                     }
2385 
2386                     initialize_SCp(cmd->next_link);
2387                     /* The next command is still part of this process */
2388                     cmd->next_link->tag = cmd->tag;
2389                     cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); 
2390 #if (NDEBUG & NDEBUG_LINKED) 
2391                     printk("scsi%d : target %d lun %d linked request done, calling scsi_done().\n",
2392                         instance->host_no, cmd->target, cmd->lun);
2393 #endif
2394 #ifdef NCR5380_STATS
2395                     collect_stats(hostdata, cmd);
2396 #endif
2397                     cmd->scsi_done(cmd);
2398                     cmd = hostdata->connected;
2399                     break;
2400 #endif /* def LINKED */
2401                 case ABORT:
2402                 case COMMAND_COMPLETE: 
2403                     /* Accept message by clearing ACK */
2404                     sink = 1;
2405                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2406                     hostdata->connected = NULL;
2407 #if (NDEBUG & NDEBUG_QUEUES)
2408                     printk("scsi%d : command for target %d, lun %d completed\n",
2409                         instance->host_no, cmd->target, cmd->lun);
2410 #endif
2411                     hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2412 
2413                     /* 
2414                      * I'm not sure what the correct thing to do here is : 
2415                      * 
2416                      * If the command that just executed is NOT a request 
2417                      * sense, the obvious thing to do is to set the result
2418                      * code to the values of the stored parameters.
2419                      * 
2420                      * If it was a REQUEST SENSE command, we need some way 
2421                      * to differentiate between the failure code of the original
2422                      * and the failure code of the REQUEST sense - the obvious
2423                      * case is success, where we fall through and leave the result
2424                      * code unchanged.
2425                      * 
2426                      * The non-obvious place is where the REQUEST SENSE failed 
2427                      */
2428 
2429                     if (cmd->cmnd[0] != REQUEST_SENSE) 
2430                         cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); 
2431                     else if (cmd->SCp.Status != GOOD)
2432                         cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2433                     
2434 #ifdef AUTOSENSE
2435                     if ((cmd->cmnd[0] != REQUEST_SENSE) && 
2436                         (cmd->SCp.Status == CHECK_CONDITION)) {
2437 #if (NDEBUG & NDEBUG_AUTOSENSE) 
2438                         printk("scsi%d : performing request sense\n", 
2439                             instance->host_no);
2440 #endif
2441                         cmd->cmnd[0] = REQUEST_SENSE;
2442                         cmd->cmnd[1] &= 0xe0;
2443                         cmd->cmnd[2] = 0;
2444                         cmd->cmnd[3] = 0;
2445                         cmd->cmnd[4] = sizeof(cmd->sense_buffer);
2446                         cmd->cmnd[5] = 0;
2447 
2448                         cmd->SCp.buffer = NULL;
2449                         cmd->SCp.buffers_residual = 0;
2450                         cmd->SCp.ptr = (char *) cmd->sense_buffer;
2451                         cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
2452 
2453                         cli();
2454                         LIST(cmd,hostdata->issue_queue);
2455                         cmd->host_scribble = (unsigned char *) 
2456                             hostdata->issue_queue;
2457                         hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2458                         sti();
2459 #if (NDEBUG & NDEBUG_QUEUES)
2460                         printk("scsi%d : REQUEST SENSE added to head of issue queue\n",instance->host_no);
2461 #endif
2462                    } else {
2463 #endif /* def AUTOSENSE */
2464 #ifdef NCR5380_STATS
2465                         collect_stats(hostdata, cmd);
2466 #endif
2467                         cmd->scsi_done(cmd);
2468                    }
2469 
2470                     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2471                     /* 
2472                      * Restore phase bits to 0 so an interrupted selection, 
2473                      * arbitration can resume.
2474                      */
2475                     NCR5380_write(TARGET_COMMAND_REG, 0);
2476                     
2477                     while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2478                         barrier();
2479                     return;
2480                 case MESSAGE_REJECT:
2481                     /* Accept message by clearing ACK */
2482                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2483                     switch (hostdata->last_message) {
2484                     case HEAD_OF_QUEUE_TAG:
2485                     case ORDERED_QUEUE_TAG:
2486                     case SIMPLE_QUEUE_TAG:
2487                         cmd->device->tagged_queue = 0;
2488                         hostdata->busy[cmd->target] |= (1 << cmd->lun);
2489                         break;
2490                     default:
2491                         break;
2492                     }
2493                 case DISCONNECT:
2494                     /* Accept message by clearing ACK */
2495                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2496                     cmd->device->disconnect = 1;
2497                     cli();
2498                     LIST(cmd,hostdata->disconnected_queue);
2499                     cmd->host_scribble = (unsigned char *) 
2500                         hostdata->disconnected_queue;
2501                     hostdata->connected = NULL;
2502                     hostdata->disconnected_queue = cmd;
2503                     sti();
2504 #if (NDEBUG & NDEBUG_QUEUES)
2505                     printk("scsi%d : command for target %d lun %d was moved from connected to"
2506                            "  the disconnected_queue\n", instance->host_no, 
2507                             cmd->target, cmd->lun);
2508 #endif
2509                     /* 
2510                      * Restore phase bits to 0 so an interrupted selection, 
2511                      * arbitration can resume.
2512                      */
2513                     NCR5380_write(TARGET_COMMAND_REG, 0);
2514  
2515                     /* Enable reselect interrupts */
2516                     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2517                     /* Wait for bus free to avoid nasty timeouts */
2518                     while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2519                         barrier();
2520 #if 0
2521                     NCR5380_print_status(instance);
2522 #endif
2523                     return;
2524                 /* 
2525                  * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2526                  * operation, in violation of the SCSI spec so we can safely 
2527                  * ignore SAVE/RESTORE pointers calls.
2528                  *
2529                  * Unfortunately, some disks violate the SCSI spec and 
2530                  * don't issue the required SAVE_POINTERS message before
2531                  * disconnecting, and we have to break spec to remain 
2532                  * compatible.
2533                  */
2534                 case SAVE_POINTERS:
2535                 case RESTORE_POINTERS:
2536                     /* Accept message by clearing ACK */
2537                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2538                     break;
2539                 case EXTENDED_MESSAGE:
2540 /* 
2541  * Extended messages are sent in the following format :
2542  * Byte         
2543  * 0            EXTENDED_MESSAGE == 1
2544  * 1            length (includes one byte for code, doesn't 
2545  *              include first two bytes)
2546  * 2            code
2547  * 3..length+1  arguments
2548  *
2549  * Start the extended message buffer with the EXTENDED_MESSAGE
2550  * byte, since print_msg() wants the whole thing.  
2551  */
2552                     extended_msg[0] = EXTENDED_MESSAGE;
2553                     /* Accept first byte by clearing ACK */
2554                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2555 
2556 #if (NDEBUG & NDEBUG_EXTENDED)
2557                     printk("scsi%d : receiving extended message\n",
2558                         instance->host_no);
2559 #endif
2560 
2561                     len = 2;
2562                     data = extended_msg + 1;
2563                     phase = PHASE_MSGIN;
2564                     NCR5380_transfer_pio(instance, &phase, &len, &data);
2565 
2566 #if (NDEBUG & NDEBUG_EXTENDED)
2567                     printk("scsi%d : length=%d, code=0x%02x\n", 
2568                         instance->host_no, (int) extended_msg[1],
2569                         (int) extended_msg[2]);
2570 #endif
2571 
2572                     if (!len && extended_msg[1] <= 
2573                         (sizeof (extended_msg) - 1)) {
2574                         /* Accept third byte by clearing ACK */
2575                         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2576                         len = extended_msg[1] - 1;
2577                         data = extended_msg + 3;
2578                         phase = PHASE_MSGIN;
2579 
2580                         NCR5380_transfer_pio(instance, &phase, &len, &data);
2581 
2582 #if (NDEBUG & NDEBUG_EXTENDED)
2583                     printk("scsi%d : message received, residual %d\n",
2584                         instance->host_no, len);
2585 #endif
2586 
2587                         switch (extended_msg[2]) {
2588                         case EXTENDED_SDTR:
2589                         case EXTENDED_WDTR:
2590                         case EXTENDED_MODIFY_DATA_POINTER:
2591                         case EXTENDED_EXTENDED_IDENTIFY:
2592                             tmp = 0;
2593                         }
2594                     } else if (len) {
2595                         printk("scsi%d: error receiving extended message\n",
2596                             instance->host_no);
2597                         tmp = 0;
2598                     } else {
2599                         printk("scsi%d: extended message code %02x length %d is too long\n",
2600                             instance->host_no, extended_msg[2], extended_msg[1]);
2601                         tmp = 0;
2602                     }
2603                 /* Fall through to reject message */
2604                     
2605                 /* 
2606                  * If we get something weird that we aren't expecting, 
2607                  * reject it.
2608                  */
2609                 default:
2610                     if (!tmp) {
2611                         printk("scsi%d: rejecting message ", instance->host_no);
2612                         print_msg (extended_msg);
2613                         printk("\n");
2614                     } else if (tmp != EXTENDED_MESSAGE)
2615                         printk("scsi%d: rejecting unknown message %02x from target %d, lun %d\n",
2616                             instance->host_no, tmp, cmd->target, cmd->lun);
2617                     else
2618                         printk("scsi%d: rejecting unknown extended message code %02x, length %d from target %d, lun %d\n",
2619                             instance->host_no, extended_msg[1], extended_msg[0], cmd->target, cmd->lun);
2620 
2621                     msgout = MESSAGE_REJECT;
2622                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
2623                         ICR_ASSERT_ATN);
2624                     break;
2625                 } /* switch (tmp) */
2626                 break;
2627             case PHASE_MSGOUT:
2628                 len = 1;
2629                 data = &msgout;
2630                 hostdata->last_message = msgout;
2631                 NCR5380_transfer_pio(instance, &phase, &len, &data);
2632                 if (msgout == ABORT) {
2633                     hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2634                     hostdata->connected = NULL;
2635                     cmd->result = DID_ERROR << 16;
2636 #ifdef NCR5380_STATS
2637                     collect_stats(hostdata, cmd);
2638 #endif
2639                     cmd->scsi_done(cmd);
2640                     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2641                     return;
2642                 }
2643                 msgout = NOP;
2644                 break;
2645             case PHASE_CMDOUT:
2646                 len = cmd->cmd_len;
2647                 data = cmd->cmnd;
2648                 /* 
2649                  * XXX for performance reasons, on machines with a 
2650                  * PSEUDO-DMA architecture we should probably 
2651                  * use the dma transfer function.  
2652                  */
2653                 NCR5380_transfer_pio(instance, &phase, &len, 
2654                     &data);
2655 #ifdef USLEEP
2656                 if (!disconnect && should_disconnect(cmd->cmnd[0])) {
2657                     hostdata->time_expires = jiffies + USLEEP_SLEEP;
2658 #if (NDEBUG & NDEBUG_USLEEP)
2659                 printk("scsi%d : issued command, sleeping until %ul\n", instance->host_no,
2660                     hostdata->time_expires);
2661 #endif
2662                     NCR5380_set_timer (instance);
2663                     return;
2664                 }
2665 #endif /* def USLEEP */
2666                 break;
2667             case PHASE_STATIN:
2668                 len = 1;
2669                 data = &tmp;
2670                 NCR5380_transfer_pio(instance, &phase, &len, &data);
2671                 cmd->SCp.Status = tmp;
2672                 break;
2673             default:
2674                 printk("scsi%d : unknown phase\n", instance->host_no);
2675 #ifdef NDEBUG
2676                 NCR5380_print(instance);
2677 #endif
2678             } /* switch(phase) */
2679         } /* if (tmp * SR_REQ) */ 
2680 #ifdef USLEEP
2681         else {
2682             if (!disconnect && hostdata->time_expires && jiffies > 
2683                 hostdata->time_expires) {
2684                 hostdata->time_expires = jiffies + USLEEP_SLEEP;
2685 #if (NDEBUG & NDEBUG_USLEEP)
2686                 printk("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no,
2687                     hostdata->time_expires);
2688 #endif
2689                 NCR5380_set_timer (instance);
2690                 return;
2691             }
2692         }
2693 #endif
2694     } /* while (1) */
2695 }
2696 
2697 /*
2698  * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2699  *
2700  * Purpose : does reselection, initializing the instance->connected 
2701  *      field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q 
2702  *      nexus has been reestablished,
2703  *      
2704  * Inputs : instance - this instance of the NCR5380.
2705  *
2706  */
2707 
2708 
2709 static void NCR5380_reselect (struct Scsi_Host *instance) {
     /* [previous][next][first][last][top][bottom][index][help] */
2710     NCR5380_local_declare();
2711     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2712         instance->hostdata;
2713     unsigned char target_mask;
2714     unsigned char lun, phase;
2715     int len;
2716 #ifdef SCSI2
2717     unsigned char tag;
2718 #endif
2719     unsigned char msg[3];
2720     unsigned char *data;
2721     Scsi_Cmnd *tmp = NULL, *prev;
2722     int abort = 0;
2723     NCR5380_setup(instance);
2724 
2725     /*
2726      * Disable arbitration, etc. since the host adapter obviously
2727      * lost, and tell an interrupted NCR5380_select() to restart.
2728      */
2729 
2730     NCR5380_write(MODE_REG, MR_BASE);
2731     hostdata->restart_select = 1;
2732 
2733     target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2734 
2735 #if (NDEBUG & NDEBUG_RESELECTION)
2736     printk("scsi%d : reselect\n", instance->host_no);
2737 #endif
2738 
2739     /* 
2740      * At this point, we have detected that our SCSI ID is on the bus,
2741      * SEL is true and BSY was false for at least one bus settle delay
2742      * (400 ns).
2743      *
2744      * We must assert BSY ourselves, until the target drops the SEL
2745      * signal.
2746      */
2747 
2748     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2749     
2750     while (NCR5380_read(STATUS_REG) & SR_SEL);
2751     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2752 
2753     /*
2754      * Wait for target to go into MSGIN.
2755      */
2756 
2757     while (!(NCR5380_read(STATUS_REG) & SR_REQ));
2758 
2759     len = 1;
2760     data = msg;
2761     phase = PHASE_MSGIN;
2762     NCR5380_transfer_pio(instance, &phase, &len, &data);
2763 
2764 
2765     if (!msg[0] & 0x80) {
2766         printk("scsi%d : expecting IDENTIFY message, got ",
2767             instance->host_no);
2768         print_msg(msg);
2769         abort = 1;
2770     } else {
2771         /* Accept message by clearing ACK */
2772         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2773         lun = (msg[0] & 0x07);
2774 
2775         /* 
2776          * We need to add code for SCSI-II to track which devices have
2777          * I_T_L_Q nexuses established, and which have simple I_T_L
2778          * nexuses so we can chose to do additional data transfer.
2779          */
2780 
2781 #ifdef SCSI2
2782 #error "SCSI-II tagged queueing is not supported yet"
2783 #endif
2784 
2785         /* 
2786          * Find the command corresponding to the I_T_L or I_T_L_Q  nexus we 
2787          * just reestablished, and remove it from the disconnected queue.
2788          */
2789 
2790 
2791         for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; 
2792             tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble) 
2793             if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
2794 #ifdef SCSI2
2795             && (tag == tmp->tag) 
2796 #endif
2797 ) {
2798             if (prev) {
2799                 REMOVE(prev,prev->host_scribble,tmp,tmp->host_scribble);
2800                 prev->host_scribble = tmp->host_scribble;
2801             } else {
2802                 REMOVE(-1,hostdata->disconnected_queue,tmp,tmp->host_scribble);
2803                 hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
2804             }
2805             tmp->host_scribble = NULL;
2806             break;
2807         }
2808 
2809         if (!tmp) {
2810 #ifdef SCSI2
2811             printk("scsi%d : warning : target bitmask %02x lun %d tag %d not in disconnect_queue.\n",
2812                     instance->host_no, target_mask, lun, tag);
2813 #else
2814             printk("scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n",
2815                     instance->host_no, target_mask, lun);
2816 #endif
2817         /* 
2818          * Since we have an established nexus that we can't do anything with,
2819          * we must abort it.  
2820          */
2821         abort = 1;
2822         }
2823     }
2824 
2825     if (abort) {
2826         do_abort (instance);
2827     } else {
2828         hostdata->connected = tmp;
2829 #if (NDEBUG & NDEBUG_RESELECTION)
2830         printk("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n",
2831             instance->host_no, tmp->target, tmp->lun, tmp->tag);
2832 #endif
2833     }
2834 }
2835 
2836 /*
2837  * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2838  *
2839  * Purpose : called by interrupt handler when DMA finishes or a phase
2840  *      mismatch occurs (which would finish the DMA transfer).  
2841  *
2842  * Inputs : instance - this instance of the NCR5380.
2843  *
2844  * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
2845  *      nexus has been reestablished, on failure NULL is returned.
2846  */
2847 
2848 #ifdef REAL_DMA
2849 static void NCR5380_dma_complete (NCR5380_instance *instance) {
     /* [previous][next][first][last][top][bottom][index][help] */
2850     NCR5380_local_declare();
2851     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *
2852         instance->hostdata);
2853     int transferred;
2854     NCR5380_setup(instance);
2855     
2856     /*
2857      * XXX this might not be right.
2858      *
2859      * Wait for final byte to transfer, ie wait for ACK to go false.
2860      *
2861      * We should use the Last Byte Sent bit, unfortunately this is 
2862      * not available on the 5380/5381 (only the various CMOS chips)
2863      */
2864 
2865     while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
2866 
2867     NCR5380_write(MODE_REG, MR_BASE);
2868     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2869 
2870     /*
2871      * The only places we should see a phase mismatch and have to send
2872      * data from the same set of pointers will be the data transfer
2873      * phases.  So, residual, requested length are only important here.
2874      */
2875 
2876     if (!(hostdata->connected->SCp.phase & SR_CD)) {
2877         transferred = instance->dmalen - NCR5380_dma_residual();
2878         hostdata->connected->SCp.this_residual -= transferred;
2879         hostdata->connected->SCp.ptr += transferred;
2880     }
2881 }
2882 #endif /* def REAL_DMA */
2883 
2884 /*
2885  * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
2886  *
2887  * Purpose : abort a command
2888  *
2889  * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the 
2890  *      host byte of the result field to, if zero DID_ABORTED is 
2891  *      used.
2892  *
2893  * Returns : 0 - success, -1 on failure.
2894  *
2895  * XXX - there is no way to abort the command that is currently 
2896  *       connected, you have to wait for it to complete.  If this is 
2897  *       a problem, we could implement longjmp() / setjmp(), setjmp()
2898  *       called where the loop started in NCR5380_main().
2899  */
2900 
2901 #ifndef NCR5380_abort
2902 static
2903 #endif
2904 int NCR5380_abort (Scsi_Cmnd *cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
2905     NCR5380_local_declare();
2906     struct Scsi_Host *instance = cmd->host;
2907     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2908         instance->hostdata;
2909     Scsi_Cmnd *tmp, **prev;
2910 
2911     printk("scsi%d : aborting command\n", instance->host_no);
2912     print_Scsi_Cmnd (cmd);
2913 
2914     NCR5380_print_status (instance);
2915 
2916     printk("scsi%d : aborting command\n", instance->host_no);
2917     print_Scsi_Cmnd (cmd);
2918 
2919     NCR5380_print_status (instance);
2920 
2921     cli();
2922     NCR5380_setup(instance);
2923 
2924 #if (NDEBUG & NDEBUG_ABORT)
2925     printk("scsi%d : abort called\n", instance->host_no);
2926     printk("        basr 0x%X, sr 0x%X\n", 
2927            NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
2928 #endif
2929 
2930 #if 0
2931 /*
2932  * Case 1 : If the command is the currently executing command, 
2933  * we'll set the aborted flag and return control so that 
2934  * information transfer routine can exit cleanly.
2935  */
2936 
2937     if (hostdata->connected == cmd) {
2938 #if (NDEBUG & NDEBUG_ABORT)
2939         printk("scsi%d : aborting connected command\n", instance->host_no);
2940 #endif
2941         hostdata->aborted = 1;
2942 /*
2943  * We should perform BSY checking, and make sure we haven't slipped
2944  * into BUS FREE.
2945  */
2946 
2947         NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2948 /* 
2949  * Since we can't change phases until we've completed the current 
2950  * handshake, we have to source or sink a byte of data if the current
2951  * phase is not MSGOUT.
2952  */
2953 
2954 /* 
2955  * Return control to the executing NCR drive so we can clear the
2956  * aborted flag and get back into our main loop.
2957  */ 
2958  
2959         return 0;
2960     }
2961 #endif
2962 
2963 /* 
2964  * Case 2 : If the command hasn't been issued yet, we simply remove it 
2965  *          from the issue queue.
2966  */
2967 #if (NDEBUG & NDEBUG_ABORT)
2968     /* KLL */
2969     printk("scsi%d : abort going into loop.\n", instance->host_no);
2970 #endif
2971     for (prev = (Scsi_Cmnd **) &(hostdata->issue_queue), 
2972         tmp = (Scsi_Cmnd *) hostdata->issue_queue;
2973         tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp = 
2974         (Scsi_Cmnd *) tmp->host_scribble) 
2975         if (cmd == tmp) {
2976             REMOVE(5,*prev,tmp,tmp->host_scribble);
2977             (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
2978             tmp->host_scribble = NULL;
2979             tmp->result = DID_ABORT << 16;
2980             sti();
2981 #if (NDEBUG & NDEBUG_ABORT)
2982     printk("scsi%d : abort removed command from issue queue.\n", 
2983         instance->host_no);
2984 #endif
2985             tmp->done(tmp);
2986             return SCSI_ABORT_SUCCESS;
2987         }
2988 #if (NDEBUG  & NDEBUG_ABORT)
2989     /* KLL */
2990         else if (prev == tmp) printk("scsi%d : LOOP\n", instance->host_no);
2991 #endif
2992 
2993 /* 
2994  * Case 3 : If any commands are connected, we're going to fail the abort
2995  *          and let the high level SCSI driver retry at a later time or 
2996  *          issue a reset.
2997  *
2998  *          Timeouts, and therefore aborted commands, will be highly unlikely
2999  *          and handling them cleanly in this situation would make the common
3000  *          case of noresets less efficient, and would pollute our code.  So,
3001  *          we fail.
3002  */
3003 
3004     if (hostdata->connected) {
3005         sti();
3006 #if (NDEBUG & NDEBUG_ABORT)
3007     printk("scsi%d : abort failed, command connected.\n", instance->host_no);
3008 #endif
3009         return SCSI_ABORT_NOT_RUNNING;
3010     }
3011 
3012 /*
3013  * Case 4: If the command is currently disconnected from the bus, and 
3014  *      there are no connected commands, we reconnect the I_T_L or 
3015  *      I_T_L_Q nexus associated with it, go into message out, and send 
3016  *      an abort message.
3017  *
3018  * This case is especially ugly. In order to reestablish the nexus, we
3019  * need to call NCR5380_select().  The easiest way to implement this 
3020  * function was to abort if the bus was busy, and let the interrupt
3021  * handler triggered on the SEL for reselect take care of lost arbitrations
3022  * where necessary, meaning interrupts need to be enabled.
3023  *
3024  * When interrupts are enabled, the queues may change - so we 
3025  * can't remove it from the disconnected queue before selecting it
3026  * because that could cause a failure in hashing the nexus if that 
3027  * device reselected.
3028  * 
3029  * Since the queues may change, we can't use the pointers from when we
3030  * first locate it.
3031  *
3032  * So, we must first locate the command, and if NCR5380_select()
3033  * succeeds, then issue the abort, relocate the command and remove
3034  * it from the disconnected queue.
3035  */
3036 
3037     for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; 
3038         tmp = (Scsi_Cmnd *) tmp->host_scribble) 
3039         if (cmd == tmp) {
3040             sti(); 
3041 #if (NDEBUG & NDEBUG_ABORT)
3042     printk("scsi%d : aborting disconnected command.\n", instance->host_no);
3043 #endif
3044   
3045             if (NCR5380_select (instance, cmd, (int) cmd->tag)) 
3046                 return SCSI_ABORT_BUSY;
3047 
3048 #if (NDEBUG & NDEBUG_ABORT)
3049     printk("scsi%d : nexus reestablished.\n", instance->host_no);
3050 #endif
3051 
3052             do_abort (instance);
3053 
3054             cli();
3055             for (prev = (Scsi_Cmnd **) &(hostdata->disconnected_queue), 
3056                 tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
3057                 tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp = 
3058                 (Scsi_Cmnd *) tmp->host_scribble) 
3059                     if (cmd == tmp) {
3060                     REMOVE(5,*prev,tmp,tmp->host_scribble);
3061                     *prev = (Scsi_Cmnd *) tmp->host_scribble;
3062                     tmp->host_scribble = NULL;
3063                     tmp->result = DID_ABORT << 16;
3064                     sti();
3065                     tmp->done(tmp);
3066                     return SCSI_ABORT_SUCCESS;
3067                 }
3068         }
3069 
3070 /*
3071  * Case 5 : If we reached this point, the command was not found in any of 
3072  *          the queues.
3073  *
3074  * We probably reached this point because of an unlikely race condition
3075  * between the command completing successfully and the abortion code,
3076  * so we won't panic, but we will notify the user in case something really
3077  * broke.
3078  */
3079 
3080     sti();
3081     printk("scsi%d : warning : SCSI command probably completed successfully\n"
3082            "         before abortion\n", instance->host_no); 
3083     return SCSI_ABORT_NOT_RUNNING;
3084 }
3085 
3086 
3087 /* 
3088  * Function : int NCR5380_reset (Scsi_Cmnd *cmd)
3089  * 
3090  * Purpose : reset the SCSI bus.
3091  *
3092  * Returns : SCSI_RESET_WAKEUP
3093  *
3094  */ 
3095 
3096 #ifndef NCR5380_reset
3097 static
3098 #endif
3099 int NCR5380_reset (Scsi_Cmnd *cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
3100     NCR5380_local_declare();
3101     NCR5380_setup(cmd->host);
3102 
3103     NCR5380_print_status (cmd->host);
3104     do_reset (cmd->host);
3105 
3106     return SCSI_RESET_WAKEUP;
3107 }
3108 

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