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. NCR5380_select
  18. NCR5380_transfer_pio
  19. do_reset
  20. do_abort
  21. NCR5380_transfer_dma
  22. NCR5380_information_transfer
  23. NCR5380_reselect
  24. NCR5380_dma_complete
  25. NCR5380_abort
  26. 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 
 813     /* The CHECK code seems to break the 53C400. Will check it later maybe */
 814     if (flags & FLAG_NCR53C400)
 815         hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
 816     else
 817         hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
 818 
 819     if (!the_template) {
 820         the_template = instance->hostt;
 821         first_instance = instance;
 822     }
 823         
 824 
 825 #ifdef USLEEP
 826     hostdata->time_expires = 0;
 827     hostdata->next_timer = NULL;
 828 #endif
 829 
 830 #ifndef AUTOSENSE
 831     if ((instance->cmd_per_lun > 1) || instance->can_queue > 1)) 
 832          printk("scsi%d : WARNING : support for multiple outstanding commands enabled\n"
 833                 "         without AUTOSENSE option, contingent allegiance conditions may\n"
 834                 "         be incorrectly cleared.\n", instance->host_no);
 835 #endif /* def AUTOSENSE */
 836 
 837     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
 838     NCR5380_write(MODE_REG, MR_BASE);
 839     NCR5380_write(TARGET_COMMAND_REG, 0);
 840     NCR5380_write(SELECT_ENABLE_REG, 0);
 841 
 842 #ifdef NCR53C400
 843     if (hostdata->flags & FLAG_NCR53C400) {
 844         NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
 845     }
 846 #endif
 847 
 848     /* 
 849      * Detect and correct bus wedge problems.
 850      *
 851      * If the system crashed, it may have crashed in a state 
 852      * where a SCSI command was still executing, and the 
 853      * SCSI bus is not in a BUS FREE STATE.
 854      *
 855      * If this is the case, we'll try to abort the currently
 856      * established nexus which we know nothing about, and that
 857      * failing, do a hard reset of the SCSI bus 
 858      */
 859 
 860     for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) &&
 861           pass <= 6 ; ++pass) {
 862         switch (pass) {
 863         case 1:
 864         case 3:
 865         case 5:
 866             printk("scsi%d: SCSI bus busy, waiting up to five seconds\n",
 867                 instance->host_no);
 868             timeout = jiffies + 500;
 869             while (jiffies < timeout && (NCR5380_read(STATUS_REG) & SR_BSY));
 870             break;
 871         case 2:
 872             printk("scsi%d: bus busy, attempting abort\n",
 873                 instance->host_no);
 874             do_abort (instance);
 875             break;
 876         case 4:
 877             printk("scsi%d: bus busy, attempting reset\n",
 878                 instance->host_no);
 879             do_reset (instance);
 880             break;
 881         case 6:
 882             printk("scsi%d: bus locked solid or invalid override\n",
 883                 instance->host_no);
 884         }
 885     }
 886 }
 887 
 888 /* 
 889  * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd, 
 890  *      void (*done)(Scsi_Cmnd *)) 
 891  *
 892  * Purpose :  enqueues a SCSI command
 893  *
 894  * Inputs : cmd - SCSI command, done - function called on completion, with
 895  *      a pointer to the command descriptor.
 896  * 
 897  * Returns : 0
 898  *
 899  * Side effects : 
 900  *      cmd is added to the per instance issue_queue, with minor 
 901  *      twiddling done to the host specific fields of cmd.  If the 
 902  *      main coroutine is not running, it is restarted.
 903  *
 904  */
 905 
 906 /* Only make static if a wrapper function is used */
 907 #ifndef NCR5380_queue_command
 908 static
 909 #endif
 910 int NCR5380_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *)) {
     /* [previous][next][first][last][top][bottom][index][help] */
 911     struct Scsi_Host *instance = cmd->host;
 912     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
 913         instance->hostdata;
 914     Scsi_Cmnd *tmp;
 915 
 916 #if (NDEBUG & NDEBUG_NO_WRITE)
 917     switch (cmd->cmnd[0]) {
 918     case WRITE:
 919     case WRITE_10:
 920         printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
 921             instance->host_no);
 922         cmd->result = (DID_ERROR << 16);
 923         done(cmd);
 924         return 0;
 925     }
 926 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
 927 
 928 
 929     /* 
 930      * We use the host_scribble field as a pointer to the next command  
 931      * in a queue 
 932      */
 933 
 934     cmd->host_scribble = NULL;
 935     cmd->scsi_done = done;
 936 
 937     cmd->result = 0;
 938 
 939 
 940     /* 
 941      * Insert the cmd into the issue queue. Note that REQUEST SENSE 
 942      * commands are added to the head of the queue since any command will
 943      * clear the contingent allegiance condition that exists and the 
 944      * sense data is only guaranteed to be valid while the condition exists.
 945      */
 946 
 947     cli();
 948     if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
 949         LIST(cmd, hostdata->issue_queue);
 950         cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
 951         hostdata->issue_queue = cmd;
 952     } else {
 953         for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble; 
 954                 tmp = (Scsi_Cmnd *) tmp->host_scribble);
 955         LIST(cmd, tmp);
 956         tmp->host_scribble = (unsigned char *) cmd;
 957     }
 958 #if (NDEBUG & NDEBUG_QUEUES)
 959     printk("scsi%d : command added to %s of queue\n", instance->host_no,
 960         (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
 961 #endif
 962 
 963 /* Run the coroutine if it isn't already running. */
 964     run_main();
 965     return 0;
 966 }
 967 
 968 /*
 969  * Function : NCR5380_main (void) 
 970  *
 971  * Purpose : NCR5380_main is a coroutine that runs as long as more work can 
 972  *      be done on the NCR5380 host adapters in a system.  Both 
 973  *      NCR5380_queue_command() and NCR5380_intr() will try to start it 
 974  *      in case it is not running.
 975  * 
 976  * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should 
 977  *  reenable them.  This prevents reentrancy and kernel stack overflow.
 978  */     
 979     
 980 static void NCR5380_main (void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 981     Scsi_Cmnd *tmp, *prev;
 982     struct Scsi_Host *instance;
 983     struct NCR5380_hostdata *hostdata;
 984     int done;
 985 
 986     /*
 987      * We run (with interrupts disabled) until we're sure that none of 
 988      * the host adapters have anything that can be done, at which point 
 989      * we set main_running to 0 and exit.
 990      *
 991      * Interrupts are enabled before doing various other internal 
 992      * instructions, after we've decided that we need to run through
 993      * the loop again.
 994      *
 995      * this should prevent any race conditions.
 996      */
 997 
 998     do {
 999         cli(); /* Freeze request queues */
1000         done = 1;
1001         for (instance = first_instance; instance && 
1002             instance->hostt == the_template; instance=instance->next) {
1003             hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1004             cli();
1005             if (!hostdata->connected) {
1006 #if (NDEBUG & NDEBUG_MAIN)
1007                 printk("scsi%d : not connected\n", instance->host_no);
1008 #endif
1009                 /*
1010                  * Search through the issue_queue for a command destined
1011                  * for a target that's not busy.
1012                  */
1013 #if (NDEBUG & NDEBUG_LISTS)
1014                 for (tmp= (Scsi_Cmnd *) hostdata->issue_queue, prev=NULL; tmp && (tmp != prev); prev=tmp, tmp=(Scsi_Cmnd*)tmp->host_scribble)
1015                     ;
1016                     /*printk("%p  ", tmp);*/
1017                 if ((tmp == prev) && tmp) printk(" LOOP\n");/* else printk("\n");*/
1018 #endif
1019                 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, 
1020                     prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) 
1021                     tmp->host_scribble) {
1022 
1023 #if (NDEBUG & NDEBUG_LISTS)
1024                     if (prev != tmp)
1025                         printk("MAIN tmp=%p   target=%d   busy=%d lun=%d\n", tmp, tmp->target, hostdata->busy[tmp->target], tmp->lun);
1026 #endif
1027                     /*  When we find one, remove it from the issue queue. */
1028                     if (!(hostdata->busy[tmp->target] & (1 << tmp->lun))) {
1029                         if (prev) {
1030                             REMOVE(prev,prev->host_scribble,tmp,tmp->host_scribble);
1031                             prev->host_scribble = tmp->host_scribble;
1032                         } else {
1033                             REMOVE(-1,hostdata->issue_queue,tmp,tmp->host_scribble);
1034                             hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
1035                         }
1036                         tmp->host_scribble = NULL;
1037 
1038                         /* reenable interrupts after finding one */
1039                         sti();
1040 
1041                         /* 
1042                          * Attempt to establish an I_T_L nexus here. 
1043                          * On success, instance->hostdata->connected is set.
1044                          * On failure, we must add the command back to the
1045                          *   issue queue so we can keep trying. 
1046                          */
1047 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1048                         printk("scsi%d : main() : command for target %d lun %d removed from issue_queue\n",
1049                             instance->host_no, tmp->target, tmp->lun);
1050 #endif
1051                 
1052                         /*
1053                          * A successful selection is defined as one that 
1054                          * leaves us with the command connected and 
1055                          * in hostdata->connected, OR has terminated the
1056                          * command.
1057                          *
1058                          * With successfull commands, we fall through
1059                          * and see if we can do an information transfer,
1060                          * with failures we will restart.
1061                          */
1062 
1063                         if (!NCR5380_select(instance, tmp, 
1064                         /* 
1065                          * REQUEST SENSE commands are issued without tagged
1066                          * queueing, even on SCSI-II devices because the 
1067                          * contingent allegiance condition exists for the 
1068                          * entire unit.
1069                          */
1070                             (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : 
1071                             TAG_NEXT)) {
1072                             break;
1073                         } else {
1074                             cli();
1075                             LIST(tmp, hostdata->issue_queue);
1076                             tmp->host_scribble = (unsigned char *) 
1077                                 hostdata->issue_queue;
1078                             hostdata->issue_queue = tmp;
1079                             done = 0;
1080                             sti();
1081 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1082                         printk("scsi%d : main(): select() failed, returned to issue_queue\n",
1083                             instance->host_no);
1084 #endif
1085                         }
1086                     } /* if target/lun is not busy */
1087                 } /* for */
1088             } /* if (!hostdata->connected) */
1089                 
1090             if (hostdata->connected 
1091 #ifdef REAL_DMA
1092                 && !hostdata->dmalen
1093 #endif
1094 #ifdef USLEEP
1095                 && (!hostdata->time_expires || hostdata->time_expires >= jiffies)
1096 #endif
1097                 ) {
1098                 sti();
1099 #if (NDEBUG & NDEBUG_MAIN)
1100                 printk("scsi%d : main() : performing information transfer\n",
1101                         instance->host_no);
1102 #endif
1103                 NCR5380_information_transfer(instance);
1104 #if (NDEBUG & NDEBUG_MAIN)
1105                 printk("scsi%d : main() : done set false\n", instance->host_no);
1106 #endif
1107                 done = 0;
1108             } else 
1109                 break;
1110         } /* for instance */
1111     } while (!done);
1112     main_running = 0;
1113 }
1114 
1115 /*
1116  * Function : void NCR5380_intr (int irq)
1117  * 
1118  * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1119  *      from the disconnected queue, and restarting NCR5380_main() 
1120  *      as required.
1121  *
1122  * Inputs : int irq, irq that caused this interrupt.
1123  *
1124  */
1125 
1126 static void NCR5380_intr (int irq, void *dev_id, struct pt_regs * regs) {
     /* [previous][next][first][last][top][bottom][index][help] */
1127     NCR5380_local_declare(); 
1128     struct Scsi_Host *instance;
1129     int done;
1130     unsigned char basr;
1131 #if (NDEBUG & NDEBUG_INTR)
1132     printk("scsi : NCR5380 irq %d triggered\n", irq);
1133 #endif
1134     do {
1135         done = 1;
1136         for (instance = first_instance; instance && (instance->hostt == 
1137             the_template); instance = instance->next)
1138             if (instance->irq == irq) {
1139                 
1140                 /* Look for pending interrupts */
1141                 NCR5380_setup(instance);
1142                 basr = NCR5380_read(BUS_AND_STATUS_REG);
1143                 /* XXX dispatch to appropriate routine if found and done=0 */
1144                 if (basr & BASR_IRQ) {
1145 #if (NDEBUG & NDEBUG_INTR)
1146                     NCR5380_print(instance);
1147 #endif
1148                     if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == 
1149                         (SR_SEL | SR_IO)) {
1150                         done = 0;
1151                         sti();
1152 #if (NDEBUG & NDEBUG_INTR)
1153                         printk("scsi%d : SEL interrupt\n", instance->host_no);
1154 #endif
1155                         NCR5380_reselect(instance);
1156                         (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1157                     } else if (basr & BASR_PARITY_ERROR) {
1158 #if (NDEBUG & NDEBUG_INTR)
1159                         printk("scsi%d : PARITY interrupt\n", instance->host_no);
1160 #endif
1161                         (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1162                     } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1163 #if (NDEBUG & NDEBUG_INTR)
1164                         printk("scsi%d : RESET interrupt\n", instance->host_no);
1165 #endif
1166                         (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1167                     } else {
1168 /*  
1169  * XXX the rest of the interrupt conditions should *only* occur during a 
1170  * DMA transfer, which I haven't gotten around to fixing yet.
1171  */
1172 
1173 #if defined(REAL_DMA)
1174                     /*
1175                      * We should only get PHASE MISMATCH and EOP interrupts
1176                      * if we have DMA enabled, so do a sanity check based on
1177                      * the current setting of the MODE register.
1178                      */
1179 
1180                         if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & 
1181                             BASR_END_DMA_TRANSFER) || 
1182                             !(basr & BASR_PHASE_MATCH))) {
1183                             int transfered;
1184 
1185                             if (!hostdata->connected) 
1186                                 panic("scsi%d : received end of DMA interrupt with no connected cmd\n",
1187                                     instance->hostno);
1188 
1189                             transfered = (hostdata->dmalen - NCR5380_dma_residual(instance));
1190                             hostdata->connected->SCp.this_residual -= transferred;
1191                             hostdata->connected->SCp.ptr += transferred;
1192                             hostdata->dmalen = 0;
1193 
1194                             (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1195 #if NCR_TIMEOUT
1196                             {
1197                               unsigned long timeout = jiffies + NCR_TIMEOUT;
1198 
1199                               while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK
1200                                      && jiffies < timeout)
1201                                 ;
1202                               if (jiffies >= timeout)
1203                                 printk("scsi%d: timeout at NCR5380.c:%d\n", 
1204                                     host->host_no, __LINE__);
1205                             }
1206 #else /* NCR_TIMEOUT */
1207                             while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
1208 #endif
1209 
1210                             NCR5380_write(MODE_REG, MR_BASE);
1211                             NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1212                         }
1213 #else
1214 #if (NDEBUG & NDEBUG_INTR)
1215                     printk("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1216 #endif
1217                     (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1218 #endif
1219                     } 
1220                 } /* if BASR_IRQ */
1221                 if (!done) 
1222                     run_main();
1223             } /* if (instance->irq == irq) */
1224     } while (!done);
1225 }
1226 
1227 /* 
1228  * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, 
1229  *      int tag);
1230  *
1231  * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1232  *      including ARBITRATION, SELECTION, and initial message out for 
1233  *      IDENTIFY and queue messages. 
1234  *
1235  * Inputs : instance - instantiation of the 5380 driver on which this 
1236  *      target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for 
1237  *      new tag, TAG_NONE for untagged queueing, otherwise set to the tag for 
1238  *      the command that is presently connected.
1239  * 
1240  * Returns : -1 if selection could not execute for some reason,
1241  *      0 if selection succeeded or failed because the target 
1242  *      did not respond.
1243  *
1244  * Side effects : 
1245  *      If bus busy, arbitration failed, etc, NCR5380_select() will exit 
1246  *              with registers as they should have been on entry - ie
1247  *              SELECT_ENABLE will be set appropriately, the NCR5380
1248  *              will cease to drive any SCSI bus signals.
1249  *
1250  *      If successful : I_T_L or I_T_L_Q nexus will be established, 
1251  *              instance->connected will be set to cmd.  
1252  *              SELECT interrupt will be disabled.
1253  *
1254  *      If failed (no target) : cmd->scsi_done() will be called, and the 
1255  *              cmd->result host byte set to DID_BAD_TARGET.
1256  */
1257 
1258 static int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
1259     int tag) {
1260     NCR5380_local_declare();
1261     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata*) 
1262         instance->hostdata;
1263     unsigned char tmp[3], phase;
1264     unsigned char *data;
1265     int len;
1266     unsigned long timeout;
1267     NCR5380_setup(instance);
1268 
1269     hostdata->restart_select = 0;
1270 #if defined (NDEBUG) && (NDEBUG & NDEBUG_ARBITRATION) 
1271     NCR5380_print(instance);
1272     printk("scsi%d : starting arbitration, id = %d\n", instance->host_no,
1273         instance->this_id);
1274 #endif
1275     cli(); 
1276 
1277     /* 
1278      * Set the phase bits to 0, otherwise the NCR5380 won't drive the 
1279      * data bus during SELECTION.
1280      */
1281 
1282     NCR5380_write(TARGET_COMMAND_REG, 0);
1283 
1284 
1285     /* 
1286      * Start arbitration.
1287      */
1288     
1289     NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1290     NCR5380_write(MODE_REG, MR_ARBITRATE);
1291 
1292     sti();
1293 
1294     /* Wait for arbitration logic to complete */
1295 #if NCR_TIMEOUT
1296     {
1297       unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
1298 
1299       while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1300            && jiffies < timeout)
1301         ;
1302       if (jiffies >= timeout)
1303       {
1304         printk("scsi: arbitration timeout at %d\n", __LINE__);
1305         NCR5380_write(MODE_REG, MR_BASE);
1306         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1307         return -1;
1308       }
1309     }
1310 #else /* NCR_TIMEOUT */
1311     while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS));
1312 #endif
1313 
1314 #if (NDEBUG & NDEBUG_ARBITRATION) 
1315     printk("scsi%d : arbitration complete\n", instance->host_no);
1316 /* Avoid GCC 2.4.5 asm needs to many reloads error */
1317     __asm__("nop");
1318 #endif
1319 
1320     /* 
1321      * The arbitration delay is 2.2us, but this is a minimum and there is 
1322      * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1323      * the integral nature of udelay().
1324      *
1325      */
1326 
1327     udelay(3);
1328 
1329     /* Check for lost arbitration */
1330     if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1331         (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1332         (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1333         NCR5380_write(MODE_REG, MR_BASE); 
1334 #if (NDEBUG & NDEBUG_ARBITRATION)
1335     printk("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", 
1336         instance->host_no);
1337 #endif
1338         return -1;
1339     }
1340 
1341 
1342 
1343     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
1344     
1345     if (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) {
1346         NCR5380_write(MODE_REG, MR_BASE);
1347         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1348 #if (NDEBUG & NDEBUG_ARBITRATION)
1349     printk("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", 
1350         instance->host_no);
1351 #endif
1352         return -1;
1353     }
1354 
1355     /* 
1356      * Again, bus clear + bus settle time is 1.2us, however, this is 
1357      * a minimum so we'll udelay ceil(1.2)
1358      */
1359 
1360     udelay(2);  
1361 
1362 #if (NDEBUG & NDEBUG_ARBITRATION)
1363     printk("scsi%d : won arbitration\n", instance->host_no);
1364 #endif
1365 
1366 
1367     /* 
1368      * Now that we have won arbitration, start Selection process, asserting 
1369      * the host and target ID's on the SCSI bus.
1370      */
1371 
1372     NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
1373 
1374     /* 
1375      * Raise ATN while SEL is true before BSY goes false from arbitration,
1376      * since this is the only way to guarantee that we'll get a MESSAGE OUT
1377      * phase immediately after selection.
1378      */
1379 
1380     NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | 
1381         ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
1382     NCR5380_write(MODE_REG, MR_BASE);
1383 
1384     /* 
1385      * Reselect interrupts must be turned off prior to the dropping of BSY,
1386      * otherwise we will trigger an interrupt.
1387      */
1388     NCR5380_write(SELECT_ENABLE_REG, 0);
1389 
1390     /*
1391      * The initiator shall then wait at least two deskew delays and release 
1392      * the BSY signal.
1393      */
1394     udelay(1);        /* wingel -- wait two bus deskew delay >2*45ns */
1395 
1396     /* Reset BSY */
1397     NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | 
1398         ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1399 
1400     /* 
1401      * Something weird happens when we cease to drive BSY - looks
1402      * like the board/chip is letting us do another read before the 
1403      * appropriate propagation delay has expired, and we're confusing
1404      * a BSY signal from ourselves as the target's response to SELECTION.
1405      *
1406      * A small delay (the 'C++' frontend breaks the pipeline with an
1407      * unnecessary jump, making it work on my 386-33/Trantor T128, the
1408      * tighter 'C' code breaks and requires this) solves the problem - 
1409      * the 1 us delay is arbitrary, and only used because this delay will 
1410      * be the same on other platforms and since it works here, it should 
1411      * work there.
1412      *
1413      * wingel suggests that this could be due to failing to wait
1414      * one deskew delay.
1415      */
1416 
1417     udelay(1);
1418 
1419 #if (NDEBUG & NDEBUG_SELECTION)
1420     printk("scsi%d : selecting target %d\n", instance->host_no, cmd->target);
1421 #endif
1422 
1423     /* 
1424      * The SCSI specification calls for a 250 ms timeout for the actual 
1425      * selection.
1426      */
1427 
1428     timeout = jiffies + 25; 
1429 
1430     /* 
1431      * XXX very interesting - we're seeing a bounce where the BSY we 
1432      * asserted is being reflected / still asserted (propagation delay?)
1433      * and it's detecting as true.  Sigh.
1434      */
1435 
1436     while ((jiffies < timeout) && !(NCR5380_read(STATUS_REG) & 
1437         (SR_BSY | SR_IO)));
1438 
1439     if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == 
1440             (SR_SEL | SR_IO)) {
1441             NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1442             NCR5380_reselect(instance);
1443             printk ("scsi%d : reselection after won arbitration?\n",
1444                 instance->host_no);
1445             NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1446             return -1;
1447     }
1448 
1449     /* 
1450      * No less than two deskew delays after the initiator detects the 
1451      * BSY signal is true, it shall release the SEL signal and may 
1452      * change the DATA BUS.                                     -wingel
1453      */
1454 
1455     udelay(1);
1456 
1457     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1458 
1459     if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1460         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1461         if (hostdata->targets_present & (1 << cmd->target)) {
1462             printk("scsi%d : weirdness\n", instance->host_no);
1463             if (hostdata->restart_select)
1464                 printk("\trestart select\n");
1465 #ifdef NDEBUG
1466             NCR5380_print (instance);
1467 #endif
1468             NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1469             return -1;
1470         }
1471         cmd->result = DID_BAD_TARGET << 16;
1472         cmd->scsi_done(cmd);
1473         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1474 #if (NDEBUG & NDEBUG_SELECTION)
1475         printk("scsi%d : target did not respond within 250ms\n", 
1476             instance->host_no);
1477 #endif
1478         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1479         return 0;
1480     } 
1481 
1482     hostdata->targets_present |= (1 << cmd->target);
1483 
1484     /*
1485      * Since we followed the SCSI spec, and raised ATN while SEL 
1486      * was true but before BSY was false during selection, the information
1487      * transfer phase should be a MESSAGE OUT phase so that we can send the
1488      * IDENTIFY message.
1489      * 
1490      * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1491      * message (2 bytes) with a tag ID that we increment with every command
1492      * until it wraps back to 0.
1493      *
1494      * XXX - it turns out that there are some broken SCSI-II devices,
1495      *       which claim to support tagged queuing but fail when more than
1496      *       some number of commands are issued at once.
1497      */
1498 
1499     /* Wait for start of REQ/ACK handshake */
1500 #ifdef NCR_TIMEOUT
1501     {
1502       unsigned long timeout = jiffies + NCR_TIMEOUT;
1503  
1504       while (!(NCR5380_read(STATUS_REG) & SR_REQ) && jiffies < timeout);
1505 
1506       if (jiffies >= timeout) {
1507         printk("scsi%d: timeout at NCR5380.c:%d\n", __LINE__);
1508         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1509         return -1;
1510       }
1511     }
1512 #else /* NCR_TIMEOUT */
1513     while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1514 #endif /* def NCR_TIMEOUT */
1515 
1516 #if (NDEBUG & NDEBUG_SELECTION)
1517     printk("scsi%d : target %d selected, going into MESSAGE OUT phase.\n",
1518         instance->host_no, cmd->target);
1519 #endif
1520     tmp[0] = IDENTIFY(((instance->irq == IRQ_NONE) ? 0 : 1), cmd->lun);
1521 #ifdef SCSI2
1522     if (cmd->device->tagged_queue && (tag != TAG_NONE)) {
1523         tmp[1] = SIMPLE_QUEUE_TAG;
1524         if (tag == TAG_NEXT) {
1525             /* 0 is TAG_NONE, used to imply no tag for this command */
1526             if (cmd->device->current_tag == 0)
1527                 cmd->device->current_tag = 1;
1528 
1529             cmd->tag = cmd->device->current_tag;
1530             cmd->device->current_tag++;
1531         } else  
1532             cmd->tag = (unsigned char) tag;
1533 
1534         tmp[2] = cmd->tag;
1535         hostdata->last_message = SIMPLE_QUEUE_TAG;
1536         len = 3;
1537     } else 
1538 #endif /* def SCSI2 */
1539     {
1540         len = 1;
1541         cmd->tag=0;
1542     }
1543 
1544     /* Send message(s) */
1545     data = tmp;
1546     phase = PHASE_MSGOUT;
1547     NCR5380_transfer_pio(instance, &phase, &len, &data);
1548 #if (NDEBUG & NDEBUG_SELECTION)
1549     printk("scsi%d : nexus established.\n", instance->host_no);
1550 #endif
1551     /* XXX need to handle errors here */
1552     hostdata->connected = cmd;
1553 #ifdef SCSI2
1554     if (!cmd->device->tagged_queue)
1555 #endif    
1556         hostdata->busy[cmd->target] |= (1 << cmd->lun);
1557 
1558     initialize_SCp(cmd);
1559 
1560 
1561     return 0;
1562 }
1563 
1564 /* 
1565  * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance, 
1566  *      unsigned char *phase, int *count, unsigned char **data)
1567  *
1568  * Purpose : transfers data in given phase using polled I/O
1569  *
1570  * Inputs : instance - instance of driver, *phase - pointer to 
1571  *      what phase is expected, *count - pointer to number of 
1572  *      bytes to transfer, **data - pointer to data pointer.
1573  * 
1574  * Returns : -1 when different phase is entered without transferring
1575  *      maximum number of bytes, 0 if all bytes or transfered or exit
1576  *      is in same phase.
1577  *
1578  *      Also, *phase, *count, *data are modified in place.
1579  *
1580  * XXX Note : handling for bus free may be useful.
1581  */
1582 
1583 /*
1584  * Note : this code is not as quick as it could be, however it 
1585  * IS 100% reliable, and for the actual data transfer where speed
1586  * counts, we will always do a pseudo DMA or DMA transfer.
1587  */
1588 
1589 static int NCR5380_transfer_pio (struct Scsi_Host *instance, 
     /* [previous][next][first][last][top][bottom][index][help] */
1590         unsigned char *phase, int *count, unsigned char **data) {
1591     NCR5380_local_declare();
1592     register unsigned char p = *phase, tmp;
1593     register int c = *count;
1594     register unsigned char *d = *data;
1595     NCR5380_setup(instance);
1596 
1597 #if (NDEBUG & NDEBUG_PIO)
1598     if (!(p & SR_IO))
1599       printk("scsi%d : pio write %d bytes\n", instance->host_no, c);
1600     else
1601       printk("scsi%d : pio read %d bytes\n", instance->host_no, c);
1602 #endif
1603 
1604     /* 
1605      * The NCR5380 chip will only drive the SCSI bus when the 
1606      * phase specified in the appropriate bits of the TARGET COMMAND
1607      * REGISTER match the STATUS REGISTER
1608      */
1609 
1610     NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1611 
1612     do {
1613         /* 
1614          * Wait for assertion of REQ, after which the phase bits will be 
1615          * valid 
1616          */
1617         while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ));
1618 
1619 #if (NDEBUG & NDEBUG_HANDSHAKE)
1620         printk("scsi%d : REQ detected\n", instance->host_no);
1621 #endif
1622 
1623         /* Check for phase mismatch */  
1624         if ((tmp & PHASE_MASK) != p) {
1625 #if (NDEBUG & NDEBUG_PIO)
1626             printk("scsi%d : phase mismatch\n", instance->host_no);
1627             NCR5380_print_phase(instance);
1628 #endif
1629             break;
1630         }
1631 
1632         /* Do actual transfer from SCSI bus to / from memory */
1633         if (!(p & SR_IO)) 
1634             NCR5380_write(OUTPUT_DATA_REG, *d);
1635         else 
1636             *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1637 
1638         ++d;
1639 
1640         /* 
1641          * The SCSI standard suggests that in MSGOUT phase, the initiator
1642          * should drop ATN on the last byte of the message phase
1643          * after REQ has been asserted for the handshake but before
1644          * the initiator raises ACK.
1645          */
1646 
1647         if (!(p & SR_IO)) {
1648             if (!((p & SR_MSG) && c > 1)) {
1649                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
1650                     ICR_ASSERT_DATA);
1651 #if (NDEBUG & NDEBUG_PIO)
1652         NCR5380_print(instance);
1653 #endif
1654                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
1655                         ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1656             } else {
1657                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1658                     ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1659 #if (NDEBUG & NDEBUG_PIO)
1660         NCR5380_print(instance);
1661 #endif
1662                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
1663                     ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1664             }
1665         } else {
1666 #if (NDEBUG & NDEBUG_PIO)
1667         NCR5380_print(instance);
1668 #endif
1669         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1670         }
1671 
1672         while (NCR5380_read(STATUS_REG) & SR_REQ);
1673 
1674 #if (NDEBUG & NDEBUG_HANDSHAKE)
1675             printk("scsi%d : req false, handshake complete\n", instance->host_no);
1676 #endif
1677 
1678 /*
1679  * We have several special cases to consider during REQ/ACK handshaking : 
1680  * 1.  We were in MSGOUT phase, and we are on the last byte of the 
1681  *      message.  ATN must be dropped as ACK is dropped.
1682  *
1683  * 2.  We are in a MSGIN phase, and we are on the last byte of the  
1684  *      message.  We must exit with ACK asserted, so that the calling
1685  *      code may raise ATN before dropping ACK to reject the message.
1686  *
1687  * 3.  ACK and ATN are clear and the target may proceed as normal.
1688  */
1689         if (!(p == PHASE_MSGIN && c == 1)) {  
1690             if (p == PHASE_MSGOUT && c > 1)
1691                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1692             else
1693                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1694         } 
1695     } while (--c);
1696 
1697 #if (NDEBUG & NDEBUG_PIO) 
1698     printk("scsi%d : residual %d\n", instance->host_no, c);
1699 #endif
1700 
1701     *count = c;
1702     *data = d;
1703     tmp = NCR5380_read(STATUS_REG);
1704     if (tmp & SR_REQ)
1705         *phase = tmp & PHASE_MASK;
1706     else 
1707         *phase = PHASE_UNKNOWN;
1708 
1709     if (!c || (*phase == p))
1710         return 0;
1711     else 
1712         return -1;
1713 }
1714 
1715 static void do_reset (struct Scsi_Host *host) {
     /* [previous][next][first][last][top][bottom][index][help] */
1716     NCR5380_local_declare();
1717     NCR5380_setup(host);
1718 
1719     cli();
1720     NCR5380_write(TARGET_COMMAND_REG, 
1721         PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1722     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1723     udelay(25);
1724     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1725     sti();
1726 }
1727 
1728 /*
1729  * Function : do_abort (Scsi_Host *host)
1730  * 
1731  * Purpose : abort the currently established nexus.  Should only be 
1732  *      called from a routine which can drop into a 
1733  * 
1734  * Returns : 0 on success, -1 on failure.
1735  */
1736 
1737 static int do_abort (struct Scsi_Host *host) {
     /* [previous][next][first][last][top][bottom][index][help] */
1738     NCR5380_local_declare();
1739     unsigned char tmp, *msgptr, phase;
1740     int len;
1741     NCR5380_setup(host);
1742 
1743 
1744     /* Request message out phase */
1745     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1746 
1747     /* 
1748      * Wait for the target to indicate a valid phase by asserting 
1749      * REQ.  Once this happens, we'll have either a MSGOUT phase 
1750      * and can immediately send the ABORT message, or we'll have some 
1751      * other phase and will have to source/sink data.
1752      * 
1753      * We really don't care what value was on the bus or what value
1754      * the target see's, so we just handshake.
1755      */
1756     
1757     while (!(tmp = NCR5380_read(STATUS_REG)) & SR_REQ);
1758 
1759     NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1760 
1761     if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1762         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | 
1763             ICR_ASSERT_ACK);
1764         while (NCR5380_read(STATUS_REG) & SR_REQ);
1765         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1766     }
1767    
1768     tmp = ABORT;
1769     msgptr = &tmp;
1770     len = 1;
1771     phase = PHASE_MSGOUT;
1772     NCR5380_transfer_pio (host, &phase, &len, &msgptr);
1773 
1774     /*
1775      * If we got here, and the command completed successfully,
1776      * we're about to go into bus free state.
1777      */
1778 
1779     return len ? -1 : 0;
1780 }
1781 
1782 #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1783 /* 
1784  * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance, 
1785  *      unsigned char *phase, int *count, unsigned char **data)
1786  *
1787  * Purpose : transfers data in given phase using either real
1788  *      or pseudo DMA.
1789  *
1790  * Inputs : instance - instance of driver, *phase - pointer to 
1791  *      what phase is expected, *count - pointer to number of 
1792  *      bytes to transfer, **data - pointer to data pointer.
1793  * 
1794  * Returns : -1 when different phase is entered without transferring
1795  *      maximum number of bytes, 0 if all bytes or transfered or exit
1796  *      is in same phase.
1797  *
1798  *      Also, *phase, *count, *data are modified in place.
1799  *
1800  */
1801 
1802 
1803 static int NCR5380_transfer_dma (struct Scsi_Host *instance, 
     /* [previous][next][first][last][top][bottom][index][help] */
1804     unsigned char *phase, int *count, unsigned char **data) {
1805     NCR5380_local_declare();
1806     register int c = *count;
1807     register unsigned char p = *phase;
1808     register unsigned char *d = *data;
1809     unsigned char tmp;
1810     int foo;
1811 #if defined(REAL_DMA_POLL)
1812     int cnt, toPIO;
1813     unsigned char saved_data = 0, overrun = 0, residue;
1814 #endif
1815 
1816     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) 
1817         instance->hostdata;
1818 
1819     NCR5380_setup(instance);
1820 
1821     if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1822         *phase = tmp;
1823         return -1;
1824     }
1825 #if defined(REAL_DMA) || defined(REAL_DMA_POLL) 
1826 #ifdef READ_OVERRUNS
1827      if (p & SR_IO) {
1828        c -= 2;
1829      }
1830 #endif
1831 #if (NDEBUG & NDEBUG_DMA)
1832     printk("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n",
1833         instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" :
1834         "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
1835 #endif
1836     hostdata->dma_len = (p & SR_IO) ?
1837         NCR5380_dma_read_setup(instance, d, c) : 
1838         NCR5380_dma_write_setup(instance, d, c);
1839 #endif
1840 
1841     NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1842 
1843 #ifdef REAL_DMA
1844     NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1845 #elif defined(REAL_DMA_POLL)
1846     NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1847 #else
1848     /*
1849      * Note : on my sample board, watch-dog timeouts occurred when interrupts
1850      * were not disabled for the duration of a single DMA transfer, from 
1851      * before the setting of DMA mode to after transfer of the last byte.
1852      */
1853 
1854 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
1855     cli();
1856 #endif
1857     /* KLL May need eop and parity in 53c400 */
1858     if (hostdata->flags & FLAG_NCR53C400)
1859         NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_PAR_CHECK
1860         | MR_ENABLE_PAR_INTR | MR_ENABLE_EOP_INTR | MR_DMA_MODE
1861         | MR_MONITOR_BSY);
1862     else
1863         NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1864 #endif /* def REAL_DMA */
1865 
1866 #if (NDEBUG & NDEBUG_DMA) & 0
1867     printk("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
1868 #endif
1869 
1870 /* 
1871  * FOO stuff. For some UNAPPARENT reason, I'm getting 
1872  * watchdog timers fired on bootup for NO APPARENT REASON, meaning it's
1873  * probably a timing problem.
1874  *
1875  * Since this is the only place I have back-to-back writes, perhaps this 
1876  * is the problem?
1877  */
1878 
1879     if (p & SR_IO) {
1880 #ifndef FOO
1881         udelay(1);
1882 #endif
1883         NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1884     } else {
1885 #ifndef FOO
1886         udelay(1);
1887 #endif
1888         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1889 #ifndef FOO
1890         udelay(1);
1891 #endif
1892         NCR5380_write(START_DMA_SEND_REG, 0);
1893 #ifndef FOO
1894         udelay(1);
1895 #endif
1896     }
1897 
1898 #if defined(REAL_DMA_POLL)
1899     do {
1900         tmp = NCR5380_read(BUS_AND_STATUS_REG);
1901     } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | 
1902         BASR_END_DMA_TRANSFER)));
1903 
1904 /*
1905   At this point, either we've completed DMA, or we have a phase mismatch,
1906   or we've unexpectedly lost BUSY (which is a real error).
1907 
1908   For write DMAs, we want to wait until the last byte has been
1909   transferred out over the bus before we turn off DMA mode.  Alas, there
1910   seems to be no terribly good way of doing this on a 5380 under all
1911   conditions.  For non-scatter-gather operations, we can wait until REQ
1912   and ACK both go false, or until a phase mismatch occurs.  Gather-writes
1913   are nastier, since the device will be expecting more data than we
1914   are prepared to send it, and REQ will remain asserted.  On a 53C8[01] we
1915   could test LAST BIT SENT to assure transfer (I imagine this is precisely
1916   why this signal was added to the newer chips) but on the older 538[01]
1917   this signal does not exist.  The workaround for this lack is a watchdog;
1918   we bail out of the wait-loop after a modest amount of wait-time if
1919   the usual exit conditions are not met.  Not a terribly clean or
1920   correct solution :-%
1921 
1922   Reads are equally tricky due to a nasty characteristic of the NCR5380.
1923   If the chip is in DMA mode for an READ, it will respond to a target's
1924   REQ by latching the SCSI data into the INPUT DATA register and asserting
1925   ACK, even if it has _already_ been notified by the DMA controller that
1926   the current DMA transfer has completed!  If the NCR5380 is then taken
1927   out of DMA mode, this already-acknowledged byte is lost.
1928 
1929   This is not a problem for "one DMA transfer per command" reads, because
1930   the situation will never arise... either all of the data is DMA'ed
1931   properly, or the target switches to MESSAGE IN phase to signal a
1932   disconnection (either operation bringing the DMA to a clean halt).
1933   However, in order to handle scatter-reads, we must work around the
1934   problem.  The chosen fix is to DMA N-2 bytes, then check for the
1935   condition before taking the NCR5380 out of DMA mode.  One or two extra
1936   bytes are transferred via PIO as necessary to fill out the original
1937   request.
1938 */
1939 
1940     if (p & SR_IO) {
1941 #ifdef READ_OVERRUNS
1942       udelay(10);
1943       if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH|BASR_ACK)) ==
1944            (BASR_PHASE_MATCH | BASR_ACK))) {
1945         saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1946         overrun = 1;
1947       }
1948 #endif
1949     } else {
1950       int limit = 100;
1951       while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) ||
1952             (NCR5380_read(STATUS_REG) & SR_REQ)) {
1953         if (!(tmp & BASR_PHASE_MATCH)) break;
1954         if (--limit < 0) break;
1955       }
1956     }
1957 
1958 
1959 #if (NDEBUG & NDEBUG_DMA)
1960     printk("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n",
1961            instance->host_no, tmp, NCR5380_read(STATUS_REG));
1962 #endif
1963 
1964     NCR5380_write(MODE_REG, MR_BASE);
1965     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1966 
1967     residue = NCR5380_dma_residual(instance);
1968     c -= residue;
1969     *count -= c;
1970     *data += c;
1971     *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1972 
1973 #ifdef READ_OVERRUNS
1974     if (*phase == p && (p & SR_IO) && residue == 0) {
1975       if (overrun) {
1976 #if (NDEBUG & NDEBUG_DMA)
1977         printk("Got an input overrun, using saved byte\n");
1978 #endif
1979         **data = saved_data;
1980         *data += 1;
1981         *count -= 1;
1982         cnt = toPIO = 1;
1983       } else {
1984         printk("No overrun??\n");
1985         cnt = toPIO = 2;
1986       }
1987 #if (NDEBUG & NDEBUG_DMA)
1988       printk("Doing %d-byte PIO to 0x%X\n", cnt, *data);
1989 #endif
1990       NCR5380_transfer_pio(instance, phase, &cnt, data);
1991       *count -= toPIO - cnt;
1992     }
1993 #endif        
1994 
1995 #if (NDEBUG & NDEBUG_DMA)
1996      printk("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n",
1997             *data, *count, *(*data+*count-1), *(*data+*count));
1998 #endif
1999      return 0;
2000      
2001 #elif defined(REAL_DMA)
2002     return 0;
2003 #else /* defined(REAL_DMA_POLL) */
2004     if (p & SR_IO) {
2005         int diff = 1;
2006         if (hostdata->flags & FLAG_NCR53C400) {
2007             diff=0;
2008         }
2009 
2010         if (!(foo = NCR5380_pread(instance, d, c - diff))) {
2011             /*
2012              * We can't disable DMA mode after successfully transferring 
2013              * what we plan to be the last byte, since that would open up
2014              * a race condition where if the target asserted REQ before 
2015              * we got the DMA mode reset, the NCR5380 would have latched
2016              * an additional byte into the INPUT DATA register and we'd
2017              * have dropped it.
2018              * 
2019              * The workaround was to transfer one fewer bytes than we 
2020              * intended to with the pseudo-DMA read function, wait for 
2021              * the chip to latch the last byte, read it, and then disable
2022              * pseudo-DMA mode.
2023              * 
2024              * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
2025              * REQ is deasserted when ACK is asserted, and not reasserted
2026              * until ACK goes false.  Since the NCR5380 won't lower ACK
2027              * until DACK is asserted, which won't happen unless we twiddle
2028              * the DMA port or we take the NCR5380 out of DMA mode, we 
2029              * can guarantee that we won't handshake another extra 
2030              * byte.
2031              */
2032 
2033             if (!(hostdata->flags & FLAG_NCR53C400)) {
2034                 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
2035                 /* Wait for clean handshake */
2036                 while (NCR5380_read(STATUS_REG) & SR_REQ);
2037                 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
2038             }
2039         }
2040     } else {
2041         int timeout;
2042 #if (NDEBUG & NDEBUG_C400_PWRITE)
2043         printk("About to pwrite %d bytes\n", c);
2044 #endif
2045         if (!(foo = NCR5380_pwrite(instance, d, c))) {
2046             /*
2047              * Wait for the last byte to be sent.  If REQ is being asserted for 
2048              * the byte we're interested, we'll ACK it and it will go false.  
2049              */
2050             if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
2051                 timeout = 20000;
2052 #if 1
2053 #if 1
2054                 while (!(NCR5380_read(BUS_AND_STATUS_REG) & 
2055                         BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) &
2056                         BASR_PHASE_MATCH));
2057 #else
2058                 if (NCR5380_read(STATUS_REG) & SR_REQ) {
2059                     for (; timeout && 
2060                         !(NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK); 
2061                         --timeout);
2062                     for (; timeout && (NCR5380_read(STATUS_REG) & SR_REQ);
2063                         --timeout);
2064                 } 
2065 #endif
2066         
2067 
2068 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2069                 if (!timeout) 
2070                     printk("scsi%d : timed out on last byte\n",
2071                             instance->host_no);
2072 #endif
2073 
2074 
2075                 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
2076                     hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
2077                     if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
2078                         hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
2079 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2080                         printk("scsi%d : last bit sent works\n", 
2081                             instance->host_no);
2082 #endif
2083                     }
2084                 }
2085             } else  {
2086 #if (NDEBUG & NDEBUG_C400_PWRITE)
2087                 printk("Waiting for LASTBYTE\n");
2088 #endif
2089                 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
2090 #if (NDEBUG & NDEBUG_C400_PWRITE)
2091                 printk("Got LASTBYTE\n");
2092 #endif
2093             }
2094 #else
2095             udelay (5);
2096 #endif
2097         }
2098     }
2099 
2100     NCR5380_write(MODE_REG, MR_BASE);
2101     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2102 
2103     if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
2104 #if (NDEBUG & NDEBUG_C400_PWRITE)
2105         printk("53C400w: Checking for IRQ\n");
2106 #endif
2107         if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
2108 #if (NDEBUG & NDEBUG_C400_PWRITE)
2109             printk("53C400w:    got it, reading reset interupt reg\n");
2110 #endif
2111             NCR5380_read(RESET_PARITY_INTERRUPT_REG);
2112         } else {
2113             printk("53C400w:    IRQ NOT THERE!\n");
2114         }
2115     }
2116 
2117     *data = d + c;
2118     *count = 0;
2119     *phase = (NCR5380_read(STATUS_REG & PHASE_MASK));
2120 #if 0
2121     NCR5380_print_phase(instance);
2122 #endif
2123 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2124     sti();
2125 #endif /* defined(REAL_DMA_POLL) */
2126     return foo;
2127 #endif /* def REAL_DMA */
2128 }
2129 #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
2130 
2131 /*
2132  * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2133  *
2134  * Purpose : run through the various SCSI phases and do as the target 
2135  *      directs us to.  Operates on the currently connected command, 
2136  *      instance->connected.
2137  *
2138  * Inputs : instance, instance for which we are doing commands
2139  *
2140  * Side effects : SCSI things happen, the disconnected queue will be 
2141  *      modified if a command disconnects, *instance->connected will
2142  *      change.
2143  *
2144  * XXX Note : we need to watch for bus free or a reset condition here 
2145  *      to recover from an unexpected bus free condition.
2146  */
2147  
2148 static void NCR5380_information_transfer (struct Scsi_Host *instance) {
     /* [previous][next][first][last][top][bottom][index][help] */
2149     NCR5380_local_declare();
2150     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) 
2151         instance->hostdata;
2152     unsigned char msgout = NOP;
2153     int sink = 0;
2154     int len;
2155 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2156     int transfersize;
2157 #endif
2158     unsigned char *data;
2159     unsigned char phase, tmp, extended_msg[10], old_phase=0xff;
2160     Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
2161     NCR5380_setup(instance);
2162 
2163     while (1) {
2164         tmp = NCR5380_read(STATUS_REG);
2165         /* We only have a valid SCSI phase when REQ is asserted */
2166         if (tmp & SR_REQ) {
2167             phase = (tmp & PHASE_MASK); 
2168             if (phase != old_phase) {
2169                 old_phase = phase;
2170 #if (NDEBUG & NDEBUG_INFORMATION)
2171                 NCR5380_print_phase(instance);
2172 #endif
2173             }
2174             
2175             if (sink && (phase != PHASE_MSGOUT)) {
2176                 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2177 
2178                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | 
2179                     ICR_ASSERT_ACK);
2180                 while (NCR5380_read(STATUS_REG) & SR_REQ);
2181                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
2182                     ICR_ASSERT_ATN);
2183                 sink = 0;
2184                 continue;
2185             }
2186 
2187             switch (phase) {
2188             case PHASE_DATAIN:
2189             case PHASE_DATAOUT:
2190 #if (NDEBUG & NDEBUG_NO_DATAOUT)
2191                 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n",
2192                     instance->host_no);
2193                 sink = 1;
2194                 do_abort(instance);
2195                 cmd->result = DID_ERROR  << 16;
2196                 cmd->done(cmd);
2197                 return;
2198 #endif
2199                 /* 
2200                  * If there is no room left in the current buffer in the
2201                  * scatter-gather list, move onto the next one.
2202                  */
2203 
2204                 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2205                     ++cmd->SCp.buffer;
2206                     --cmd->SCp.buffers_residual;
2207                     cmd->SCp.this_residual = cmd->SCp.buffer->length;
2208                     cmd->SCp.ptr = cmd->SCp.buffer->address;
2209 #if (NDEBUG & NDEBUG_INFORMATION)
2210                     printk("scsi%d : %d bytes and %d buffers left\n",
2211                         instance->host_no, cmd->SCp.this_residual,
2212                         cmd->SCp.buffers_residual);
2213 #endif
2214                 }
2215 
2216                 /*
2217                  * The preferred transfer method is going to be 
2218                  * PSEUDO-DMA for systems that are strictly PIO,
2219                  * since we can let the hardware do the handshaking.
2220                  *
2221                  * For this to work, we need to know the transfersize
2222                  * ahead of time, since the pseudo-DMA code will sit
2223                  * in an unconditional loop.
2224                  */
2225 
2226 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2227                 /* KLL
2228                  * PSEUDO_DMA is defined here. If this is the g_NCR5380
2229                  * driver then it will always be defined, so the
2230                  * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
2231                  * NCR5380 case.  I think this is a fairly clean solution.
2232                  * We supplement these 2 if's with the flag.
2233                  */
2234 #ifdef NCR5380_dma_xfer_len
2235                 if (!cmd->device->borken &&
2236                     !(hostdata->flags & FLAG_NO_PSEUDO_DMA) &&
2237                     (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
2238 #else
2239                 transfersize = cmd->transfersize;
2240 
2241 #ifdef LIMIT_TRANSFERSIZE  /* If we have problems with interrupt service */
2242                 if( transfersize > 512 )
2243                     transfersize = 512;
2244 #endif  /* LIMIT_TRANSFERSIZE */
2245 
2246                 if (!cmd->device->borken && transfersize && 
2247                     !(hostdata->flags & FLAG_NO_PSEUDO_DMA) &&
2248                     cmd->SCp.this_residual && !(cmd->SCp.this_residual % 
2249                     transfersize)) {
2250 #endif
2251                     len = transfersize;
2252                     if (NCR5380_transfer_dma(instance, &phase,
2253                         &len, (unsigned char **) &cmd->SCp.ptr)) {
2254                         /*
2255                          * If the watchdog timer fires, all future accesses to this
2256                          * device will use the polled-IO.
2257                          */ 
2258                         printk("scsi%d : switching target %d lun %d to slow handshake\n",
2259                             instance->host_no, cmd->target, cmd->lun);
2260                         cmd->device->borken = 1;
2261                         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
2262                             ICR_ASSERT_ATN);
2263                         sink = 1;
2264                         do_abort(instance);
2265                         cmd->result = DID_ERROR  << 16;
2266                         cmd->done(cmd);
2267                         /* XXX - need to source or sink data here, as appropriate */
2268                     } else
2269                         cmd->SCp.this_residual -= transfersize - len;
2270                 } else
2271 #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2272                   NCR5380_transfer_pio(instance, &phase, 
2273                     (int *) &cmd->SCp.this_residual, (unsigned char **)
2274                     &cmd->SCp.ptr);
2275                 break;
2276             case PHASE_MSGIN:
2277                 len = 1;
2278                 data = &tmp;
2279                 NCR5380_transfer_pio(instance, &phase, &len, &data);
2280                 cmd->SCp.Message = tmp;
2281 
2282                 switch (tmp) {
2283                 /*
2284                  * Linking lets us reduce the time required to get the 
2285                  * next command out to the device, hopefully this will
2286                  * mean we don't waste another revolution due to the delays
2287                  * required by ARBITRATION and another SELECTION.
2288                  *
2289                  * In the current implementation proposal, low level drivers
2290                  * merely have to start the next command, pointed to by 
2291                  * next_link, done() is called as with unlinked commands.
2292                  */
2293 #ifdef LINKED
2294                 case LINKED_CMD_COMPLETE:
2295                 case LINKED_FLG_CMD_COMPLETE:
2296                     /* Accept message by clearing ACK */
2297                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2298                     
2299 #if (NDEBUG & NDEBUG_LINKED) 
2300                     printk("scsi%d : target %d lun %d linked command complete.\n",
2301                         instance->host_no, cmd->target, cmd->lun);
2302 #endif
2303                     /* 
2304                      * Sanity check : A linked command should only terminate with
2305                      * one of these messages if there are more linked commands
2306                      * available.
2307                      */
2308 
2309                     if (!cmd->next_link) {
2310                          printk("scsi%d : target %d lun %d linked command complete, no next_link\n"
2311                             instance->host_no, cmd->target, cmd->lun);
2312                             sink = 1;
2313                             do_abort (instance);
2314                             return;
2315                     }
2316 
2317                     initialize_SCp(cmd->next_link);
2318                     /* The next command is still part of this process */
2319                     cmd->next_link->tag = cmd->tag;
2320                     cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); 
2321 #if (NDEBUG & NDEBUG_LINKED) 
2322                     printk("scsi%d : target %d lun %d linked request done, calling scsi_done().\n",
2323                         instance->host_no, cmd->target, cmd->lun);
2324 #endif
2325                     cmd->scsi_done(cmd);
2326                     cmd = hostdata->connected;
2327                     break;
2328 #endif /* def LINKED */
2329                 case ABORT:
2330                 case COMMAND_COMPLETE: 
2331                     /* Accept message by clearing ACK */
2332                     sink = 1;
2333                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2334                     hostdata->connected = NULL;
2335 #if (NDEBUG & NDEBUG_QUEUES)
2336                     printk("scsi%d : command for target %d, lun %d completed\n",
2337                         instance->host_no, cmd->target, cmd->lun);
2338 #endif
2339                     hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2340 
2341                     /* 
2342                      * I'm not sure what the correct thing to do here is : 
2343                      * 
2344                      * If the command that just executed is NOT a request 
2345                      * sense, the obvious thing to do is to set the result
2346                      * code to the values of the stored parameters.
2347                      * 
2348                      * If it was a REQUEST SENSE command, we need some way 
2349                      * to differentiate between the failure code of the original
2350                      * and the failure code of the REQUEST sense - the obvious
2351                      * case is success, where we fall through and leave the result
2352                      * code unchanged.
2353                      * 
2354                      * The non-obvious place is where the REQUEST SENSE failed 
2355                      */
2356 
2357                     if (cmd->cmnd[0] != REQUEST_SENSE) 
2358                         cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); 
2359                     else if (cmd->SCp.Status != GOOD)
2360                         cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2361                     
2362 #ifdef AUTOSENSE
2363                     if ((cmd->cmnd[0] != REQUEST_SENSE) && 
2364                         (cmd->SCp.Status == CHECK_CONDITION)) {
2365 #if (NDEBUG & NDEBUG_AUTOSENSE) 
2366                         printk("scsi%d : performing request sense\n", 
2367                             instance->host_no);
2368 #endif
2369                         cmd->cmnd[0] = REQUEST_SENSE;
2370                         cmd->cmnd[1] &= 0xe0;
2371                         cmd->cmnd[2] = 0;
2372                         cmd->cmnd[3] = 0;
2373                         cmd->cmnd[4] = sizeof(cmd->sense_buffer);
2374                         cmd->cmnd[5] = 0;
2375 
2376                         cmd->SCp.buffer = NULL;
2377                         cmd->SCp.buffers_residual = 0;
2378                         cmd->SCp.ptr = (char *) cmd->sense_buffer;
2379                         cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
2380 
2381                         cli();
2382                         LIST(cmd,hostdata->issue_queue);
2383                         cmd->host_scribble = (unsigned char *) 
2384                             hostdata->issue_queue;
2385                         hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2386                         sti();
2387 #if (NDEBUG & NDEBUG_QUEUES)
2388                         printk("scsi%d : REQUEST SENSE added to head of issue queue\n",instance->host_no);
2389 #endif
2390                    } else
2391 #endif /* def AUTOSENSE */
2392                         cmd->scsi_done(cmd);
2393 
2394                     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2395                     /* 
2396                      * Restore phase bits to 0 so an interrupted selection, 
2397                      * arbitration can resume.
2398                      */
2399                     NCR5380_write(TARGET_COMMAND_REG, 0);
2400                     
2401                     while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2402                         barrier();
2403                     return;
2404                 case MESSAGE_REJECT:
2405                     /* Accept message by clearing ACK */
2406                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2407                     switch (hostdata->last_message) {
2408                     case HEAD_OF_QUEUE_TAG:
2409                     case ORDERED_QUEUE_TAG:
2410                     case SIMPLE_QUEUE_TAG:
2411                         cmd->device->tagged_queue = 0;
2412                         hostdata->busy[cmd->target] |= (1 << cmd->lun);
2413                         break;
2414                     default:
2415                         break;
2416                     }
2417                 case DISCONNECT:
2418                     /* Accept message by clearing ACK */
2419                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2420                     cmd->device->disconnect = 1;
2421                     cli();
2422                     LIST(cmd,hostdata->disconnected_queue);
2423                     cmd->host_scribble = (unsigned char *) 
2424                         hostdata->disconnected_queue;
2425                     hostdata->connected = NULL;
2426                     hostdata->disconnected_queue = cmd;
2427                     sti();
2428 #if (NDEBUG & NDEBUG_QUEUES)
2429                     printk("scsi%d : command for target %d lun %d was moved from connected to"
2430                            "  the disconnected_queue\n", instance->host_no, 
2431                             cmd->target, cmd->lun);
2432 #endif
2433                     /* 
2434                      * Restore phase bits to 0 so an interrupted selection, 
2435                      * arbitration can resume.
2436                      */
2437                     NCR5380_write(TARGET_COMMAND_REG, 0);
2438  
2439                     /* Enable reselect interrupts */
2440                     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2441                     /* Wait for bus free to avoid nasty timeouts */
2442                     while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2443                         barrier();
2444 #if 0
2445                     NCR5380_print_status(instance);
2446 #endif
2447                     return;
2448                 /* 
2449                  * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2450                  * operation, in violation of the SCSI spec so we can safely 
2451                  * ignore SAVE/RESTORE pointers calls.
2452                  *
2453                  * Unfortunately, some disks violate the SCSI spec and 
2454                  * don't issue the required SAVE_POINTERS message before
2455                  * disconnecting, and we have to break spec to remain 
2456                  * compatible.
2457                  */
2458                 case SAVE_POINTERS:
2459                 case RESTORE_POINTERS:
2460                     /* Accept message by clearing ACK */
2461                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2462                     break;
2463                 case EXTENDED_MESSAGE:
2464 /* 
2465  * Extended messages are sent in the following format :
2466  * Byte         
2467  * 0            EXTENDED_MESSAGE == 1
2468  * 1            length (includes one byte for code, doesn't 
2469  *              include first two bytes)
2470  * 2            code
2471  * 3..length+1  arguments
2472  *
2473  * Start the extended message buffer with the EXTENDED_MESSAGE
2474  * byte, since print_msg() wants the whole thing.  
2475  */
2476                     extended_msg[0] = EXTENDED_MESSAGE;
2477                     /* Accept first byte by clearing ACK */
2478                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2479 
2480 #if (NDEBUG & NDEBUG_EXTENDED)
2481                     printk("scsi%d : receiving extended message\n",
2482                         instance->host_no);
2483 #endif
2484 
2485                     len = 2;
2486                     data = extended_msg + 1;
2487                     phase = PHASE_MSGIN;
2488                     NCR5380_transfer_pio(instance, &phase, &len, &data);
2489 
2490 #if (NDEBUG & NDEBUG_EXTENDED)
2491                     printk("scsi%d : length=%d, code=0x%02x\n", 
2492                         instance->host_no, (int) extended_msg[1],
2493                         (int) extended_msg[2]);
2494 #endif
2495 
2496                     if (!len && extended_msg[1] <= 
2497                         (sizeof (extended_msg) - 1)) {
2498                         /* Accept third byte by clearing ACK */
2499                         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2500                         len = extended_msg[1] - 1;
2501                         data = extended_msg + 3;
2502                         phase = PHASE_MSGIN;
2503 
2504                         NCR5380_transfer_pio(instance, &phase, &len, &data);
2505 
2506 #if (NDEBUG & NDEBUG_EXTENDED)
2507                     printk("scsi%d : message received, residual %d\n",
2508                         instance->host_no, len);
2509 #endif
2510 
2511                         switch (extended_msg[2]) {
2512                         case EXTENDED_SDTR:
2513                         case EXTENDED_WDTR:
2514                         case EXTENDED_MODIFY_DATA_POINTER:
2515                         case EXTENDED_EXTENDED_IDENTIFY:
2516                             tmp = 0;
2517                         }
2518                     } else if (len) {
2519                         printk("scsi%d: error receiving extended message\n",
2520                             instance->host_no);
2521                         tmp = 0;
2522                     } else {
2523                         printk("scsi%d: extended message code %02x length %d is too long\n",
2524                             instance->host_no, extended_msg[2], extended_msg[1]);
2525                         tmp = 0;
2526                     }
2527                 /* Fall through to reject message */
2528                     
2529                 /* 
2530                  * If we get something weird that we aren't expecting, 
2531                  * reject it.
2532                  */
2533                 default:
2534                     if (!tmp) {
2535                         printk("scsi%d: rejecting message ", instance->host_no);
2536                         print_msg (extended_msg);
2537                         printk("\n");
2538                     } else if (tmp != EXTENDED_MESSAGE)
2539                         printk("scsi%d: rejecting unknown message %02x from target %d, lun %d\n",
2540                             instance->host_no, tmp, cmd->target, cmd->lun);
2541                     else
2542                         printk("scsi%d: rejecting unknown extended message code %02x, length %d from target %d, lun %d\n",
2543                             instance->host_no, extended_msg[1], extended_msg[0], cmd->target, cmd->lun);
2544 
2545                     msgout = MESSAGE_REJECT;
2546                     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
2547                         ICR_ASSERT_ATN);
2548                     break;
2549                 } /* switch (tmp) */
2550                 break;
2551             case PHASE_MSGOUT:
2552                 len = 1;
2553                 data = &msgout;
2554                 hostdata->last_message = msgout;
2555                 NCR5380_transfer_pio(instance, &phase, &len, &data);
2556                 if (msgout == ABORT) {
2557                     hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2558                     hostdata->connected = NULL;
2559                     cmd->result = DID_ERROR << 16;
2560                     cmd->scsi_done(cmd);
2561                     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2562                     return;
2563                 }
2564                 msgout = NOP;
2565                 break;
2566             case PHASE_CMDOUT:
2567                 len = cmd->cmd_len;
2568                 data = cmd->cmnd;
2569                 /* 
2570                  * XXX for performance reasons, on machines with a 
2571                  * PSEUDO-DMA architecture we should probably 
2572                  * use the dma transfer function.  
2573                  */
2574                 NCR5380_transfer_pio(instance, &phase, &len, 
2575                     &data);
2576 #ifdef USLEEP
2577                 if (!disconnect && should_disconnect(cmd->cmnd[0])) {
2578                     hostdata->time_expires = jiffies + USLEEP_SLEEP;
2579 #if (NDEBUG & NDEBUG_USLEEP)
2580                 printk("scsi%d : issued command, sleeping until %ul\n", instance->host_no,
2581                     hostdata->time_expires);
2582 #endif
2583                     NCR5380_set_timer (instance);
2584                     return;
2585                 }
2586 #endif /* def USLEEP */
2587                 break;
2588             case PHASE_STATIN:
2589                 len = 1;
2590                 data = &tmp;
2591                 NCR5380_transfer_pio(instance, &phase, &len, &data);
2592                 cmd->SCp.Status = tmp;
2593                 break;
2594             default:
2595                 printk("scsi%d : unknown phase\n", instance->host_no);
2596 #ifdef NDEBUG
2597                 NCR5380_print(instance);
2598 #endif
2599             } /* switch(phase) */
2600         } /* if (tmp * SR_REQ) */ 
2601 #ifdef USLEEP
2602         else {
2603             if (!disconnect && hostdata->time_expires && jiffies > 
2604                 hostdata->time_expires) {
2605                 hostdata->time_expires = jiffies + USLEEP_SLEEP;
2606 #if (NDEBUG & NDEBUG_USLEEP)
2607                 printk("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no,
2608                     hostdata->time_expires);
2609 #endif
2610                 NCR5380_set_timer (instance);
2611                 return;
2612             }
2613         }
2614 #endif
2615     } /* while (1) */
2616 }
2617 
2618 /*
2619  * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2620  *
2621  * Purpose : does reselection, initializing the instance->connected 
2622  *      field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q 
2623  *      nexus has been reestablished,
2624  *      
2625  * Inputs : instance - this instance of the NCR5380.
2626  *
2627  */
2628 
2629 
2630 static void NCR5380_reselect (struct Scsi_Host *instance) {
     /* [previous][next][first][last][top][bottom][index][help] */
2631     NCR5380_local_declare();
2632     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2633         instance->hostdata;
2634     unsigned char target_mask;
2635     unsigned char lun, phase;
2636     int len;
2637 #ifdef SCSI2
2638     unsigned char tag;
2639 #endif
2640     unsigned char msg[3];
2641     unsigned char *data;
2642     Scsi_Cmnd *tmp = NULL, *prev;
2643     int abort = 0;
2644     NCR5380_setup(instance);
2645 
2646     /*
2647      * Disable arbitration, etc. since the host adapter obviously
2648      * lost, and tell an interrupted NCR5380_select() to restart.
2649      */
2650 
2651     NCR5380_write(MODE_REG, MR_BASE);
2652     hostdata->restart_select = 1;
2653 
2654     target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2655 
2656 #if (NDEBUG & NDEBUG_RESELECTION)
2657     printk("scsi%d : reselect\n", instance->host_no);
2658 #endif
2659 
2660     /* 
2661      * At this point, we have detected that our SCSI ID is on the bus,
2662      * SEL is true and BSY was false for at least one bus settle delay
2663      * (400 ns).
2664      *
2665      * We must assert BSY ourselves, until the target drops the SEL
2666      * signal.
2667      */
2668 
2669     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2670     
2671     while (NCR5380_read(STATUS_REG) & SR_SEL);
2672     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2673 
2674     /*
2675      * Wait for target to go into MSGIN.
2676      */
2677 
2678     while (!(NCR5380_read(STATUS_REG) & SR_REQ));
2679 
2680     len = 1;
2681     data = msg;
2682     phase = PHASE_MSGIN;
2683     NCR5380_transfer_pio(instance, &phase, &len, &data);
2684 
2685 
2686     if (!msg[0] & 0x80) {
2687         printk("scsi%d : expecting IDENTIFY message, got ",
2688             instance->host_no);
2689         print_msg(msg);
2690         abort = 1;
2691     } else {
2692         /* Accept message by clearing ACK */
2693         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2694         lun = (msg[0] & 0x07);
2695 
2696         /* 
2697          * We need to add code for SCSI-II to track which devices have
2698          * I_T_L_Q nexuses established, and which have simple I_T_L
2699          * nexuses so we can chose to do additional data transfer.
2700          */
2701 
2702 #ifdef SCSI2
2703 #error "SCSI-II tagged queueing is not supported yet"
2704 #endif
2705 
2706         /* 
2707          * Find the command corresponding to the I_T_L or I_T_L_Q  nexus we 
2708          * just reestablished, and remove it from the disconnected queue.
2709          */
2710 
2711 
2712         for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; 
2713             tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble) 
2714             if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
2715 #ifdef SCSI2
2716             && (tag == tmp->tag) 
2717 #endif
2718 ) {
2719             if (prev) {
2720                 REMOVE(prev,prev->host_scribble,tmp,tmp->host_scribble);
2721                 prev->host_scribble = tmp->host_scribble;
2722             } else {
2723                 REMOVE(-1,hostdata->disconnected_queue,tmp,tmp->host_scribble);
2724                 hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
2725             }
2726             tmp->host_scribble = NULL;
2727             break;
2728         }
2729 
2730         if (!tmp) {
2731 #ifdef SCSI2
2732             printk("scsi%d : warning : target bitmask %02x lun %d tag %d not in disconnect_queue.\n",
2733                     instance->host_no, target_mask, lun, tag);
2734 #else
2735             printk("scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n",
2736                     instance->host_no, target_mask, lun);
2737 #endif
2738         /* 
2739          * Since we have an established nexus that we can't do anything with,
2740          * we must abort it.  
2741          */
2742         abort = 1;
2743         }
2744     }
2745 
2746     if (abort) {
2747         do_abort (instance);
2748     } else {
2749         hostdata->connected = tmp;
2750 #if (NDEBUG & NDEBUG_RESELECTION)
2751         printk("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n",
2752             instance->host_no, tmp->target, tmp->lun, tmp->tag);
2753 #endif
2754     }
2755 }
2756 
2757 /*
2758  * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2759  *
2760  * Purpose : called by interrupt handler when DMA finishes or a phase
2761  *      mismatch occurs (which would finish the DMA transfer).  
2762  *
2763  * Inputs : instance - this instance of the NCR5380.
2764  *
2765  * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
2766  *      nexus has been reestablished, on failure NULL is returned.
2767  */
2768 
2769 #ifdef REAL_DMA
2770 static void NCR5380_dma_complete (NCR5380_instance *instance) {
     /* [previous][next][first][last][top][bottom][index][help] */
2771     NCR5380_local_declare();
2772     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *
2773         instance->hostdata);
2774     int transferred;
2775     NCR5380_setup(instance);
2776     
2777     /*
2778      * XXX this might not be right.
2779      *
2780      * Wait for final byte to transfer, ie wait for ACK to go false.
2781      *
2782      * We should use the Last Byte Sent bit, unfortunately this is 
2783      * not available on the 5380/5381 (only the various CMOS chips)
2784      */
2785 
2786     while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
2787 
2788     NCR5380_write(MODE_REG, MR_BASE);
2789     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2790 
2791     /*
2792      * The only places we should see a phase mismatch and have to send
2793      * data from the same set of pointers will be the data transfer
2794      * phases.  So, residual, requested length are only important here.
2795      */
2796 
2797     if (!(hostdata->connected->SCp.phase & SR_CD)) {
2798         transferred = instance->dmalen - NCR5380_dma_residual();
2799         hostdata->connected->SCp.this_residual -= transferred;
2800         hostdata->connected->SCp.ptr += transferred;
2801     }
2802 }
2803 #endif /* def REAL_DMA */
2804 
2805 /*
2806  * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
2807  *
2808  * Purpose : abort a command
2809  *
2810  * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the 
2811  *      host byte of the result field to, if zero DID_ABORTED is 
2812  *      used.
2813  *
2814  * Returns : 0 - success, -1 on failure.
2815  *
2816  * XXX - there is no way to abort the command that is currently 
2817  *       connected, you have to wait for it to complete.  If this is 
2818  *       a problem, we could implement longjmp() / setjmp(), setjmp()
2819  *       called where the loop started in NCR5380_main().
2820  */
2821 
2822 #ifndef NCR5380_abort
2823 static
2824 #endif
2825 int NCR5380_abort (Scsi_Cmnd *cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
2826     NCR5380_local_declare();
2827     struct Scsi_Host *instance = cmd->host;
2828     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2829         instance->hostdata;
2830     Scsi_Cmnd *tmp, **prev;
2831 
2832     printk("scsi%d : aborting command\n", instance->host_no);
2833     print_Scsi_Cmnd (cmd);
2834 
2835     NCR5380_print_status (instance);
2836 
2837     printk("scsi%d : aborting command\n", instance->host_no);
2838     print_Scsi_Cmnd (cmd);
2839 
2840     NCR5380_print_status (instance);
2841 
2842     cli();
2843     NCR5380_setup(instance);
2844     
2845 #if (NDEBUG & NDEBUG_ABORT)
2846     printk("scsi%d : abort called\n", instance->host_no);
2847     printk("        basr 0x%X, sr 0x%X\n", 
2848            NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
2849 #endif
2850 
2851 #if 0
2852 /*
2853  * Case 1 : If the command is the currently executing command, 
2854  * we'll set the aborted flag and return control so that 
2855  * information transfer routine can exit cleanly.
2856  */
2857 
2858     if (hostdata->connected == cmd) {
2859 #if (NDEBUG & NDEBUG_ABORT)
2860         printk("scsi%d : aborting connected command\n", instance->host_no);
2861 #endif
2862         hostdata->aborted = 1;
2863 /*
2864  * We should perform BSY checking, and make sure we haven't slipped
2865  * into BUS FREE.
2866  */
2867 
2868         NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2869 /* 
2870  * Since we can't change phases until we've completed the current 
2871  * handshake, we have to source or sink a byte of data if the current
2872  * phase is not MSGOUT.
2873  */
2874 
2875 /* 
2876  * Return control to the executing NCR drive so we can clear the
2877  * aborted flag and get back into our main loop.
2878  */ 
2879  
2880         return 0;
2881     }
2882 #endif
2883 
2884 /* 
2885  * Case 2 : If the command hasn't been issued yet, we simply remove it 
2886  *          from the issue queue.
2887  */
2888 #if (NDEBUG & NDEBUG_ABORT)
2889     /* KLL */
2890     printk("scsi%d : abort going into loop.\n", instance->host_no);
2891 #endif
2892     for (prev = (Scsi_Cmnd **) &(hostdata->issue_queue), 
2893         tmp = (Scsi_Cmnd *) hostdata->issue_queue;
2894         tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp = 
2895         (Scsi_Cmnd *) tmp->host_scribble) 
2896         if (cmd == tmp) {
2897             REMOVE(5,*prev,tmp,tmp->host_scribble);
2898             (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
2899             tmp->host_scribble = NULL;
2900             tmp->result = DID_ABORT << 16;
2901             sti();
2902 #if (NDEBUG & NDEBUG_ABORT)
2903     printk("scsi%d : abort removed command from issue queue.\n", 
2904         instance->host_no);
2905 #endif
2906             tmp->done(tmp);
2907             return SCSI_ABORT_SUCCESS;
2908         }
2909 #if (NDEBUG  & NDEBUG_ABORT)
2910     /* KLL */
2911         else if (prev == tmp) printk("scsi%d : LOOP\n", instance->host_no);
2912 #endif
2913 
2914 /* 
2915  * Case 3 : If any commands are connected, we're going to fail the abort
2916  *          and let the high level SCSI driver retry at a later time or 
2917  *          issue a reset.
2918  *
2919  *          Timeouts, and therefore aborted commands, will be highly unlikely
2920  *          and handling them cleanly in this situation would make the common
2921  *          case of noresets less efficient, and would pollute our code.  So,
2922  *          we fail.
2923  */
2924 
2925     if (hostdata->connected) {
2926         sti();
2927 #if (NDEBUG & NDEBUG_ABORT)
2928     printk("scsi%d : abort failed, command connected.\n", instance->host_no);
2929 #endif
2930         return SCSI_ABORT_NOT_RUNNING;
2931     }
2932 
2933 /*
2934  * Case 4: If the command is currently disconnected from the bus, and 
2935  *      there are no connected commands, we reconnect the I_T_L or 
2936  *      I_T_L_Q nexus associated with it, go into message out, and send 
2937  *      an abort message.
2938  *
2939  * This case is especially ugly. In order to reestablish the nexus, we
2940  * need to call NCR5380_select().  The easiest way to implement this 
2941  * function was to abort if the bus was busy, and let the interrupt
2942  * handler triggered on the SEL for reselect take care of lost arbitrations
2943  * where necessary, meaning interrupts need to be enabled.
2944  *
2945  * When interrupts are enabled, the queues may change - so we 
2946  * can't remove it from the disconnected queue before selecting it
2947  * because that could cause a failure in hashing the nexus if that 
2948  * device reselected.
2949  * 
2950  * Since the queues may change, we can't use the pointers from when we
2951  * first locate it.
2952  *
2953  * So, we must first locate the command, and if NCR5380_select()
2954  * succeeds, then issue the abort, relocate the command and remove
2955  * it from the disconnected queue.
2956  */
2957 
2958     for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; 
2959         tmp = (Scsi_Cmnd *) tmp->host_scribble) 
2960         if (cmd == tmp) {
2961             sti(); 
2962 #if (NDEBUG & NDEBUG_ABORT)
2963     printk("scsi%d : aborting disconnected command.\n", instance->host_no);
2964 #endif
2965   
2966             if (NCR5380_select (instance, cmd, (int) cmd->tag)) 
2967                 return SCSI_ABORT_BUSY;
2968 
2969 #if (NDEBUG & NDEBUG_ABORT)
2970     printk("scsi%d : nexus reestablished.\n", instance->host_no);
2971 #endif
2972 
2973             do_abort (instance);
2974 
2975             cli();
2976             for (prev = (Scsi_Cmnd **) &(hostdata->disconnected_queue), 
2977                 tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
2978                 tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp = 
2979                 (Scsi_Cmnd *) tmp->host_scribble) 
2980                     if (cmd == tmp) {
2981                     REMOVE(5,*prev,tmp,tmp->host_scribble);
2982                     *prev = (Scsi_Cmnd *) tmp->host_scribble;
2983                     tmp->host_scribble = NULL;
2984                     tmp->result = DID_ABORT << 16;
2985                     sti();
2986                     tmp->done(tmp);
2987                     return SCSI_ABORT_SUCCESS;
2988                 }
2989         }
2990 
2991 /*
2992  * Case 5 : If we reached this point, the command was not found in any of 
2993  *          the queues.
2994  *
2995  * We probably reached this point because of an unlikely race condition
2996  * between the command completing successfully and the abortion code,
2997  * so we won't panic, but we will notify the user in case something really
2998  * broke.
2999  */
3000 
3001     sti();
3002     printk("scsi%d : warning : SCSI command probably completed successfully\n"
3003            "         before abortion\n", instance->host_no); 
3004     return SCSI_ABORT_NOT_RUNNING;
3005 }
3006 
3007 
3008 /* 
3009  * Function : int NCR5380_reset (Scsi_Cmnd *cmd)
3010  * 
3011  * Purpose : reset the SCSI bus.
3012  *
3013  * Returns : SCSI_RESET_WAKEUP
3014  *
3015  */ 
3016 
3017 #ifndef NCR5380_reset
3018 static
3019 #endif
3020 int NCR5380_reset (Scsi_Cmnd *cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
3021     NCR5380_local_declare();
3022     NCR5380_setup(cmd->host);
3023 
3024     NCR5380_print_status (cmd->host);
3025     do_reset (cmd->host);
3026 
3027     return SCSI_RESET_WAKEUP;
3028 }
3029 

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