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