root/drivers/scsi/atari_NCR5380.c

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

DEFINITIONS

This source file includes following definitions.
  1. init_tags
  2. is_lun_busy
  3. cmd_get_tag
  4. cmd_free_tag
  5. free_all_tags
  6. merge_consecutive_buffers
  7. initialize_SCp
  8. NCR5380_print
  9. NCR5380_print_phase
  10. run_main
  11. NCR5380_all_init
  12. NCR5380_print_options
  13. NCR5380_print_status
  14. NCR5380_proc_info
  15. lprint_Scsi_Cmnd
  16. lprint_command
  17. lprint_opcode
  18. NCR5380_init
  19. NCR5380_queue_command
  20. NCR5380_main
  21. NCR5380_main_a
  22. NCR5380_dma_complete
  23. NCR5380_intr
  24. collect_stats
  25. NCR5380_select
  26. NCR5380_transfer_pio
  27. do_abort
  28. NCR5380_transfer_dma
  29. NCR5380_information_transfer
  30. NCR5380_reselect
  31. NCR5380_abort
  32. NCR5380_reset

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

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