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

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