Annotation of Gnu-Mach/i386/i386at/gpl/linux/scsi/NCR5380.src, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.