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. NCR5380_transfer_dma
  20. NCR5380_information_transfer
  21. NCR5380_reselect
  22. NCR5380_dma_complete
  23. NCR5380_abort
  24. NCR5380_reset

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

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