xref: /linux/drivers/scsi/NCR5380.c (revision f281233d3eba15fb225d21ae2e228fd4553d824a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * NCR 5380 generic driver routines.  These should make it *trivial*
31da177e4SLinus Torvalds  *      to implement 5380 SCSI drivers under Linux with a non-trantor
41da177e4SLinus Torvalds  *      architecture.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *      Note that these routines also work with NR53c400 family chips.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Copyright 1993, Drew Eckhardt
91da177e4SLinus Torvalds  *      Visionary Computing
101da177e4SLinus Torvalds  *      (Unix and Linux consulting and custom programming)
111da177e4SLinus Torvalds  *      drew@colorado.edu
121da177e4SLinus Torvalds  *      +1 (303) 666-5836
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * DISTRIBUTION RELEASE 6.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * For more information, please consult
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * NCR 5380 Family
191da177e4SLinus Torvalds  * SCSI Protocol Controller
201da177e4SLinus Torvalds  * Databook
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * NCR Microelectronics
231da177e4SLinus Torvalds  * 1635 Aeroplaza Drive
241da177e4SLinus Torvalds  * Colorado Springs, CO 80916
251da177e4SLinus Torvalds  * 1+ (719) 578-3400
261da177e4SLinus Torvalds  * 1+ (800) 334-5454
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /*
301da177e4SLinus Torvalds  * $Log: NCR5380.c,v $
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds  * Revision 1.10 1998/9/2	Alan Cox
33fa195afeSAlan Cox  *				(alan@lxorguk.ukuu.org.uk)
341da177e4SLinus Torvalds  * Fixed up the timer lockups reported so far. Things still suck. Looking
351da177e4SLinus Torvalds  * forward to 2.3 and per device request queues. Then it'll be possible to
361da177e4SLinus Torvalds  * SMP thread this beast and improve life no end.
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds  * Revision 1.9  1997/7/27	Ronald van Cuijlenborg
391da177e4SLinus Torvalds  *				(ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
401da177e4SLinus Torvalds  * (hopefully) fixed and enhanced USLEEP
411da177e4SLinus Torvalds  * added support for DTC3181E card (for Mustek scanner)
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds  * Revision 1.8			Ingmar Baumgart
451da177e4SLinus Torvalds  *				(ingmar@gonzo.schwaben.de)
461da177e4SLinus Torvalds  * added support for NCR53C400a card
471da177e4SLinus Torvalds  *
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds  * Revision 1.7  1996/3/2       Ray Van Tassle (rayvt@comm.mot.com)
501da177e4SLinus Torvalds  * added proc_info
511da177e4SLinus Torvalds  * added support needed for DTC 3180/3280
521da177e4SLinus Torvalds  * fixed a couple of bugs
531da177e4SLinus Torvalds  *
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds  * Revision 1.5  1994/01/19  09:14:57  drew
561da177e4SLinus Torvalds  * Fixed udelay() hack that was being used on DATAOUT phases
571da177e4SLinus Torvalds  * instead of a proper wait for the final handshake.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * Revision 1.4  1994/01/19  06:44:25  drew
601da177e4SLinus Torvalds  * *** empty log message ***
611da177e4SLinus Torvalds  *
621da177e4SLinus Torvalds  * Revision 1.3  1994/01/19  05:24:40  drew
631da177e4SLinus Torvalds  * Added support for TCR LAST_BYTE_SENT bit.
641da177e4SLinus Torvalds  *
651da177e4SLinus Torvalds  * Revision 1.2  1994/01/15  06:14:11  drew
661da177e4SLinus Torvalds  * REAL DMA support, bug fixes.
671da177e4SLinus Torvalds  *
681da177e4SLinus Torvalds  * Revision 1.1  1994/01/15  06:00:54  drew
691da177e4SLinus Torvalds  * Initial revision
701da177e4SLinus Torvalds  *
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /*
741da177e4SLinus Torvalds  * Further development / testing that should be done :
751da177e4SLinus Torvalds  * 1.  Cleanup the NCR5380_transfer_dma function and DMA operation complete
761da177e4SLinus Torvalds  *     code so that everything does the same thing that's done at the
771da177e4SLinus Torvalds  *     end of a pseudo-DMA read operation.
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  * 2.  Fix REAL_DMA (interrupt driven, polled works fine) -
801da177e4SLinus Torvalds  *     basically, transfer size needs to be reduced by one
811da177e4SLinus Torvalds  *     and the last byte read as is done with PSEUDO_DMA.
821da177e4SLinus Torvalds  *
831da177e4SLinus Torvalds  * 4.  Test SCSI-II tagged queueing (I have no devices which support
841da177e4SLinus Torvalds  *      tagged queueing)
851da177e4SLinus Torvalds  *
861da177e4SLinus Torvalds  * 5.  Test linked command handling code after Eric is ready with
871da177e4SLinus Torvalds  *      the high level code.
881da177e4SLinus Torvalds  */
89db9dff36S #include <scsi/scsi_dbg.h>
901abfd370SMatthew Wilcox #include <scsi/scsi_transport_spi.h>
911da177e4SLinus Torvalds 
92185a7a1cSviro@ZenIV.linux.org.uk #ifndef NDEBUG
93185a7a1cSviro@ZenIV.linux.org.uk #define NDEBUG 0
94185a7a1cSviro@ZenIV.linux.org.uk #endif
95f566a576SRandy Dunlap #ifndef NDEBUG_ABORT
96185a7a1cSviro@ZenIV.linux.org.uk #define NDEBUG_ABORT 0
97185a7a1cSviro@ZenIV.linux.org.uk #endif
98185a7a1cSviro@ZenIV.linux.org.uk 
991da177e4SLinus Torvalds #if (NDEBUG & NDEBUG_LISTS)
1001da177e4SLinus Torvalds #define LIST(x,y) {printk("LINE:%d   Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
1011da177e4SLinus Torvalds #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); }
1021da177e4SLinus Torvalds #else
1031da177e4SLinus Torvalds #define LIST(x,y)
1041da177e4SLinus Torvalds #define REMOVE(w,x,y,z)
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #ifndef notyet
1081da177e4SLinus Torvalds #undef LINKED
1091da177e4SLinus Torvalds #undef REAL_DMA
1101da177e4SLinus Torvalds #endif
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds #ifdef REAL_DMA_POLL
1131da177e4SLinus Torvalds #undef READ_OVERRUNS
1141da177e4SLinus Torvalds #define READ_OVERRUNS
1151da177e4SLinus Torvalds #endif
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds #ifdef BOARD_REQUIRES_NO_DELAY
1181da177e4SLinus Torvalds #define io_recovery_delay(x)
1191da177e4SLinus Torvalds #else
1201da177e4SLinus Torvalds #define io_recovery_delay(x)	udelay(x)
1211da177e4SLinus Torvalds #endif
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /*
1241da177e4SLinus Torvalds  * Design
1251da177e4SLinus Torvalds  *
1261da177e4SLinus Torvalds  * This is a generic 5380 driver.  To use it on a different platform,
1271da177e4SLinus Torvalds  * one simply writes appropriate system specific macros (ie, data
1281da177e4SLinus Torvalds  * transfer - some PC's will use the I/O bus, 68K's must use
1291da177e4SLinus Torvalds  * memory mapped) and drops this file in their 'C' wrapper.
1301da177e4SLinus Torvalds  *
1311da177e4SLinus Torvalds  * (Note from hch:  unfortunately it was not enough for the different
1321da177e4SLinus Torvalds  * m68k folks and instead of improving this driver they copied it
1331da177e4SLinus Torvalds  * and hacked it up for their needs.  As a consequence they lost
1341da177e4SLinus Torvalds  * most updates to this driver.  Maybe someone will fix all these
1351da177e4SLinus Torvalds  * drivers to use a common core one day..)
1361da177e4SLinus Torvalds  *
1371da177e4SLinus Torvalds  * As far as command queueing, two queues are maintained for
1381da177e4SLinus Torvalds  * each 5380 in the system - commands that haven't been issued yet,
1391da177e4SLinus Torvalds  * and commands that are currently executing.  This means that an
1401da177e4SLinus Torvalds  * unlimited number of commands may be queued, letting
1411da177e4SLinus Torvalds  * more commands propagate from the higher driver levels giving higher
1421da177e4SLinus Torvalds  * throughput.  Note that both I_T_L and I_T_L_Q nexuses are supported,
1431da177e4SLinus Torvalds  * allowing multiple commands to propagate all the way to a SCSI-II device
1441da177e4SLinus Torvalds  * while a command is already executing.
1451da177e4SLinus Torvalds  *
1461da177e4SLinus Torvalds  *
1471da177e4SLinus Torvalds  * Issues specific to the NCR5380 :
1481da177e4SLinus Torvalds  *
1491da177e4SLinus Torvalds  * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
1501da177e4SLinus Torvalds  * piece of hardware that requires you to sit in a loop polling for
1511da177e4SLinus Torvalds  * the REQ signal as long as you are connected.  Some devices are
1521da177e4SLinus Torvalds  * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
1531da177e4SLinus Torvalds  * while doing long seek operations.
1541da177e4SLinus Torvalds  *
1551da177e4SLinus Torvalds  * The workaround for this is to keep track of devices that have
1561da177e4SLinus Torvalds  * disconnected.  If the device hasn't disconnected, for commands that
1571da177e4SLinus Torvalds  * should disconnect, we do something like
1581da177e4SLinus Torvalds  *
1591da177e4SLinus Torvalds  * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
1601da177e4SLinus Torvalds  *
1611da177e4SLinus Torvalds  * Some tweaking of N and M needs to be done.  An algorithm based
1621da177e4SLinus Torvalds  * on "time to data" would give the best results as long as short time
1631da177e4SLinus Torvalds  * to datas (ie, on the same track) were considered, however these
1641da177e4SLinus Torvalds  * broken devices are the exception rather than the rule and I'd rather
1651da177e4SLinus Torvalds  * spend my time optimizing for the normal case.
1661da177e4SLinus Torvalds  *
1671da177e4SLinus Torvalds  * Architecture :
1681da177e4SLinus Torvalds  *
1691da177e4SLinus Torvalds  * At the heart of the design is a coroutine, NCR5380_main,
1701da177e4SLinus Torvalds  * which is started from a workqueue for each NCR5380 host in the
1711da177e4SLinus Torvalds  * system.  It attempts to establish I_T_L or I_T_L_Q nexuses by
1721da177e4SLinus Torvalds  * removing the commands from the issue queue and calling
1731da177e4SLinus Torvalds  * NCR5380_select() if a nexus is not established.
1741da177e4SLinus Torvalds  *
1751da177e4SLinus Torvalds  * Once a nexus is established, the NCR5380_information_transfer()
1761da177e4SLinus Torvalds  * phase goes through the various phases as instructed by the target.
1771da177e4SLinus Torvalds  * if the target goes into MSG IN and sends a DISCONNECT message,
1781da177e4SLinus Torvalds  * the command structure is placed into the per instance disconnected
1791da177e4SLinus Torvalds  * queue, and NCR5380_main tries to find more work.  If the target is
1801da177e4SLinus Torvalds  * idle for too long, the system will try to sleep.
1811da177e4SLinus Torvalds  *
1821da177e4SLinus Torvalds  * If a command has disconnected, eventually an interrupt will trigger,
1831da177e4SLinus Torvalds  * calling NCR5380_intr()  which will in turn call NCR5380_reselect
1841da177e4SLinus Torvalds  * to reestablish a nexus.  This will run main if necessary.
1851da177e4SLinus Torvalds  *
1861da177e4SLinus Torvalds  * On command termination, the done function will be called as
1871da177e4SLinus Torvalds  * appropriate.
1881da177e4SLinus Torvalds  *
1891da177e4SLinus Torvalds  * SCSI pointers are maintained in the SCp field of SCSI command
1901da177e4SLinus Torvalds  * structures, being initialized after the command is connected
1911da177e4SLinus Torvalds  * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
1921da177e4SLinus Torvalds  * Note that in violation of the standard, an implicit SAVE POINTERS operation
1931da177e4SLinus Torvalds  * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /*
1971da177e4SLinus Torvalds  * Using this file :
1981da177e4SLinus Torvalds  * This file a skeleton Linux SCSI driver for the NCR 5380 series
1991da177e4SLinus Torvalds  * of chips.  To use it, you write an architecture specific functions
2001da177e4SLinus Torvalds  * and macros and include this file in your driver.
2011da177e4SLinus Torvalds  *
2021da177e4SLinus Torvalds  * These macros control options :
2031da177e4SLinus Torvalds  * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
2041da177e4SLinus Torvalds  *      defined.
2051da177e4SLinus Torvalds  *
2061da177e4SLinus Torvalds  * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
2071da177e4SLinus Torvalds  *      for commands that return with a CHECK CONDITION status.
2081da177e4SLinus Torvalds  *
2091da177e4SLinus Torvalds  * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
2101da177e4SLinus Torvalds  *      transceivers.
2111da177e4SLinus Torvalds  *
2121da177e4SLinus Torvalds  * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
2131da177e4SLinus Torvalds  *      override-configure an IRQ.
2141da177e4SLinus Torvalds  *
2151da177e4SLinus Torvalds  * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
2161da177e4SLinus Torvalds  *      bytes at a time.  Since interrupts are disabled by default during
2171da177e4SLinus Torvalds  *      these transfers, we might need this to give reasonable interrupt
2181da177e4SLinus Torvalds  *      service time if the transfer size gets too large.
2191da177e4SLinus Torvalds  *
2201da177e4SLinus Torvalds  * LINKED - if defined, linked commands are supported.
2211da177e4SLinus Torvalds  *
2221da177e4SLinus Torvalds  * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
2231da177e4SLinus Torvalds  *
2241da177e4SLinus Torvalds  * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
2251da177e4SLinus Torvalds  *
2261da177e4SLinus Torvalds  * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
2271da177e4SLinus Torvalds  *      rely on phase mismatch and EOP interrupts to determine end
2281da177e4SLinus Torvalds  *      of phase.
2291da177e4SLinus Torvalds  *
2301da177e4SLinus Torvalds  * UNSAFE - leave interrupts enabled during pseudo-DMA transfers.  You
2311da177e4SLinus Torvalds  *          only really want to use this if you're having a problem with
2321da177e4SLinus Torvalds  *          dropped characters during high speed communications, and even
2331da177e4SLinus Torvalds  *          then, you're going to be better off twiddling with transfersize
2341da177e4SLinus Torvalds  *          in the high level code.
2351da177e4SLinus Torvalds  *
2361da177e4SLinus Torvalds  * Defaults for these will be provided although the user may want to adjust
2371da177e4SLinus Torvalds  * these to allocate CPU resources to the SCSI driver or "real" code.
2381da177e4SLinus Torvalds  *
2391da177e4SLinus Torvalds  * USLEEP_SLEEP - amount of time, in jiffies, to sleep
2401da177e4SLinus Torvalds  *
2411da177e4SLinus Torvalds  * USLEEP_POLL - amount of time, in jiffies, to poll
2421da177e4SLinus Torvalds  *
2431da177e4SLinus Torvalds  * These macros MUST be defined :
2441da177e4SLinus Torvalds  * NCR5380_local_declare() - declare any local variables needed for your
2451da177e4SLinus Torvalds  *      transfer routines.
2461da177e4SLinus Torvalds  *
2471da177e4SLinus Torvalds  * NCR5380_setup(instance) - initialize any local variables needed from a given
2481da177e4SLinus Torvalds  *      instance of the host adapter for NCR5380_{read,write,pread,pwrite}
2491da177e4SLinus Torvalds  *
2501da177e4SLinus Torvalds  * NCR5380_read(register)  - read from the specified register
2511da177e4SLinus Torvalds  *
2521da177e4SLinus Torvalds  * NCR5380_write(register, value) - write to the specific register
2531da177e4SLinus Torvalds  *
2541da177e4SLinus Torvalds  * NCR5380_implementation_fields  - additional fields needed for this
2551da177e4SLinus Torvalds  *      specific implementation of the NCR5380
2561da177e4SLinus Torvalds  *
2571da177e4SLinus Torvalds  * Either real DMA *or* pseudo DMA may be implemented
2581da177e4SLinus Torvalds  * REAL functions :
2591da177e4SLinus Torvalds  * NCR5380_REAL_DMA should be defined if real DMA is to be used.
2601da177e4SLinus Torvalds  * Note that the DMA setup functions should return the number of bytes
2611da177e4SLinus Torvalds  *      that they were able to program the controller for.
2621da177e4SLinus Torvalds  *
2631da177e4SLinus Torvalds  * Also note that generic i386/PC versions of these macros are
2641da177e4SLinus Torvalds  *      available as NCR5380_i386_dma_write_setup,
2651da177e4SLinus Torvalds  *      NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
2661da177e4SLinus Torvalds  *
2671da177e4SLinus Torvalds  * NCR5380_dma_write_setup(instance, src, count) - initialize
2681da177e4SLinus Torvalds  * NCR5380_dma_read_setup(instance, dst, count) - initialize
2691da177e4SLinus Torvalds  * NCR5380_dma_residual(instance); - residual count
2701da177e4SLinus Torvalds  *
2711da177e4SLinus Torvalds  * PSEUDO functions :
2721da177e4SLinus Torvalds  * NCR5380_pwrite(instance, src, count)
2731da177e4SLinus Torvalds  * NCR5380_pread(instance, dst, count);
2741da177e4SLinus Torvalds  *
2751da177e4SLinus Torvalds  * The generic driver is initialized by calling NCR5380_init(instance),
2761da177e4SLinus Torvalds  * after setting the appropriate host specific fields and ID.  If the
2771da177e4SLinus Torvalds  * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
2781da177e4SLinus Torvalds  * possible) function may be used.
2791da177e4SLinus Torvalds  */
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds static int do_abort(struct Scsi_Host *host);
2821da177e4SLinus Torvalds static void do_reset(struct Scsi_Host *host);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /*
2851da177e4SLinus Torvalds  *	initialize_SCp		-	init the scsi pointer field
2861da177e4SLinus Torvalds  *	@cmd: command block to set up
2871da177e4SLinus Torvalds  *
2881da177e4SLinus Torvalds  *	Set up the internal fields in the SCSI command.
2891da177e4SLinus Torvalds  */
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static __inline__ void initialize_SCp(Scsi_Cmnd * cmd)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	/*
2941da177e4SLinus Torvalds 	 * Initialize the Scsi Pointer field so that all of the commands in the
2951da177e4SLinus Torvalds 	 * various queues are valid.
2961da177e4SLinus Torvalds 	 */
2971da177e4SLinus Torvalds 
2989e0fe44dSBoaz Harrosh 	if (scsi_bufflen(cmd)) {
2999e0fe44dSBoaz Harrosh 		cmd->SCp.buffer = scsi_sglist(cmd);
3009e0fe44dSBoaz Harrosh 		cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
30145711f1aSJens Axboe 		cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
3021da177e4SLinus Torvalds 		cmd->SCp.this_residual = cmd->SCp.buffer->length;
3031da177e4SLinus Torvalds 	} else {
3041da177e4SLinus Torvalds 		cmd->SCp.buffer = NULL;
3051da177e4SLinus Torvalds 		cmd->SCp.buffers_residual = 0;
3069e0fe44dSBoaz Harrosh 		cmd->SCp.ptr = NULL;
3079e0fe44dSBoaz Harrosh 		cmd->SCp.this_residual = 0;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds /**
3121da177e4SLinus Torvalds  *	NCR5380_poll_politely	-	wait for NCR5380 status bits
3131da177e4SLinus Torvalds  *	@instance: controller to poll
3141da177e4SLinus Torvalds  *	@reg: 5380 register to poll
3151da177e4SLinus Torvalds  *	@bit: Bitmask to check
3161da177e4SLinus Torvalds  *	@val: Value required to exit
3171da177e4SLinus Torvalds  *
3181da177e4SLinus Torvalds  *	Polls the NCR5380 in a reasonably efficient manner waiting for
3191da177e4SLinus Torvalds  *	an event to occur, after a short quick poll we begin giving the
3201da177e4SLinus Torvalds  *	CPU back in non IRQ contexts
3211da177e4SLinus Torvalds  *
3221da177e4SLinus Torvalds  *	Returns the value of the register or a negative error code.
3231da177e4SLinus Torvalds  */
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds static int NCR5380_poll_politely(struct Scsi_Host *instance, int reg, int bit, int val, int t)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	NCR5380_local_declare();
3281da177e4SLinus Torvalds 	int n = 500;		/* At about 8uS a cycle for the cpu access */
3291da177e4SLinus Torvalds 	unsigned long end = jiffies + t;
3301da177e4SLinus Torvalds 	int r;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	NCR5380_setup(instance);
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	while( n-- > 0)
3351da177e4SLinus Torvalds 	{
3361da177e4SLinus Torvalds 		r = NCR5380_read(reg);
3371da177e4SLinus Torvalds 		if((r & bit) == val)
3381da177e4SLinus Torvalds 			return 0;
3391da177e4SLinus Torvalds 		cpu_relax();
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	/* t time yet ? */
3431da177e4SLinus Torvalds 	while(time_before(jiffies, end))
3441da177e4SLinus Torvalds 	{
3451da177e4SLinus Torvalds 		r = NCR5380_read(reg);
3461da177e4SLinus Torvalds 		if((r & bit) == val)
3471da177e4SLinus Torvalds 			return 0;
3481da177e4SLinus Torvalds 		if(!in_interrupt())
349730a646dSAmol Lad 			cond_resched();
3501da177e4SLinus Torvalds 		else
3511da177e4SLinus Torvalds 			cpu_relax();
3521da177e4SLinus Torvalds 	}
3531da177e4SLinus Torvalds 	return -ETIMEDOUT;
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds static struct {
3571da177e4SLinus Torvalds 	unsigned char value;
3581da177e4SLinus Torvalds 	const char *name;
359702809ceSAndrew Morton } phases[] __maybe_unused = {
3601da177e4SLinus Torvalds 	{PHASE_DATAOUT, "DATAOUT"},
3611da177e4SLinus Torvalds 	{PHASE_DATAIN, "DATAIN"},
3621da177e4SLinus Torvalds 	{PHASE_CMDOUT, "CMDOUT"},
3631da177e4SLinus Torvalds 	{PHASE_STATIN, "STATIN"},
3641da177e4SLinus Torvalds 	{PHASE_MSGOUT, "MSGOUT"},
3651da177e4SLinus Torvalds 	{PHASE_MSGIN, "MSGIN"},
3661da177e4SLinus Torvalds 	{PHASE_UNKNOWN, "UNKNOWN"}
3671da177e4SLinus Torvalds };
3681da177e4SLinus Torvalds 
369185a7a1cSviro@ZenIV.linux.org.uk #if NDEBUG
3701da177e4SLinus Torvalds static struct {
3711da177e4SLinus Torvalds 	unsigned char mask;
3721da177e4SLinus Torvalds 	const char *name;
3731da177e4SLinus Torvalds } signals[] = {
3741da177e4SLinus Torvalds 	{SR_DBP, "PARITY"},
3751da177e4SLinus Torvalds 	{SR_RST, "RST"},
3761da177e4SLinus Torvalds 	{SR_BSY, "BSY"},
3771da177e4SLinus Torvalds 	{SR_REQ, "REQ"},
3781da177e4SLinus Torvalds 	{SR_MSG, "MSG"},
3791da177e4SLinus Torvalds 	{SR_CD, "CD"},
3801da177e4SLinus Torvalds 	{SR_IO, "IO"},
3811da177e4SLinus Torvalds 	{SR_SEL, "SEL"},
3821da177e4SLinus Torvalds 	{0, NULL}
3831da177e4SLinus Torvalds },
3841da177e4SLinus Torvalds basrs[] = {
3851da177e4SLinus Torvalds 	{BASR_ATN, "ATN"},
3861da177e4SLinus Torvalds 	{BASR_ACK, "ACK"},
3871da177e4SLinus Torvalds 	{0, NULL}
3881da177e4SLinus Torvalds },
3891da177e4SLinus Torvalds icrs[] = {
3901da177e4SLinus Torvalds 	{ICR_ASSERT_RST, "ASSERT RST"},
3911da177e4SLinus Torvalds 	{ICR_ASSERT_ACK, "ASSERT ACK"},
3921da177e4SLinus Torvalds 	{ICR_ASSERT_BSY, "ASSERT BSY"},
3931da177e4SLinus Torvalds 	{ICR_ASSERT_SEL, "ASSERT SEL"},
3941da177e4SLinus Torvalds 	{ICR_ASSERT_ATN, "ASSERT ATN"},
3951da177e4SLinus Torvalds 	{ICR_ASSERT_DATA, "ASSERT DATA"},
3961da177e4SLinus Torvalds 	{0, NULL}
3971da177e4SLinus Torvalds },
3981da177e4SLinus Torvalds mrs[] = {
3991da177e4SLinus Torvalds 	{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
4001da177e4SLinus Torvalds 	{MR_TARGET, "MODE TARGET"},
4011da177e4SLinus Torvalds 	{MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
4021da177e4SLinus Torvalds 	{MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
4031da177e4SLinus Torvalds 	{MR_MONITOR_BSY, "MODE MONITOR BSY"},
4041da177e4SLinus Torvalds 	{MR_DMA_MODE, "MODE DMA"},
4051da177e4SLinus Torvalds 	{MR_ARBITRATE, "MODE ARBITRATION"},
4061da177e4SLinus Torvalds 	{0, NULL}
4071da177e4SLinus Torvalds };
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds /**
4101da177e4SLinus Torvalds  *	NCR5380_print	-	print scsi bus signals
4111da177e4SLinus Torvalds  *	@instance:	adapter state to dump
4121da177e4SLinus Torvalds  *
4131da177e4SLinus Torvalds  *	Print the SCSI bus signals for debugging purposes
4141da177e4SLinus Torvalds  *
4151da177e4SLinus Torvalds  *	Locks: caller holds hostdata lock (not essential)
4161da177e4SLinus Torvalds  */
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds static void NCR5380_print(struct Scsi_Host *instance)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	NCR5380_local_declare();
4211da177e4SLinus Torvalds 	unsigned char status, data, basr, mr, icr, i;
4221da177e4SLinus Torvalds 	NCR5380_setup(instance);
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	data = NCR5380_read(CURRENT_SCSI_DATA_REG);
4251da177e4SLinus Torvalds 	status = NCR5380_read(STATUS_REG);
4261da177e4SLinus Torvalds 	mr = NCR5380_read(MODE_REG);
4271da177e4SLinus Torvalds 	icr = NCR5380_read(INITIATOR_COMMAND_REG);
4281da177e4SLinus Torvalds 	basr = NCR5380_read(BUS_AND_STATUS_REG);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	printk("STATUS_REG: %02x ", status);
4311da177e4SLinus Torvalds 	for (i = 0; signals[i].mask; ++i)
4321da177e4SLinus Torvalds 		if (status & signals[i].mask)
4331da177e4SLinus Torvalds 			printk(",%s", signals[i].name);
4341da177e4SLinus Torvalds 	printk("\nBASR: %02x ", basr);
4351da177e4SLinus Torvalds 	for (i = 0; basrs[i].mask; ++i)
4361da177e4SLinus Torvalds 		if (basr & basrs[i].mask)
4371da177e4SLinus Torvalds 			printk(",%s", basrs[i].name);
4381da177e4SLinus Torvalds 	printk("\nICR: %02x ", icr);
4391da177e4SLinus Torvalds 	for (i = 0; icrs[i].mask; ++i)
4401da177e4SLinus Torvalds 		if (icr & icrs[i].mask)
4411da177e4SLinus Torvalds 			printk(",%s", icrs[i].name);
4421da177e4SLinus Torvalds 	printk("\nMODE: %02x ", mr);
4431da177e4SLinus Torvalds 	for (i = 0; mrs[i].mask; ++i)
4441da177e4SLinus Torvalds 		if (mr & mrs[i].mask)
4451da177e4SLinus Torvalds 			printk(",%s", mrs[i].name);
4461da177e4SLinus Torvalds 	printk("\n");
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds /*
4511da177e4SLinus Torvalds  *	NCR5380_print_phase	-	show SCSI phase
4521da177e4SLinus Torvalds  *	@instance: adapter to dump
4531da177e4SLinus Torvalds  *
4541da177e4SLinus Torvalds  * 	Print the current SCSI phase for debugging purposes
4551da177e4SLinus Torvalds  *
4561da177e4SLinus Torvalds  *	Locks: none
4571da177e4SLinus Torvalds  */
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds static void NCR5380_print_phase(struct Scsi_Host *instance)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	NCR5380_local_declare();
4621da177e4SLinus Torvalds 	unsigned char status;
4631da177e4SLinus Torvalds 	int i;
4641da177e4SLinus Torvalds 	NCR5380_setup(instance);
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	status = NCR5380_read(STATUS_REG);
4671da177e4SLinus Torvalds 	if (!(status & SR_REQ))
4681da177e4SLinus Torvalds 		printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
4691da177e4SLinus Torvalds 	else {
4701da177e4SLinus Torvalds 		for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
4711da177e4SLinus Torvalds 		printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds #endif
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds /*
4771da177e4SLinus Torvalds  * These need tweaking, and would probably work best as per-device
4781da177e4SLinus Torvalds  * flags initialized differently for disk, tape, cd, etc devices.
4791da177e4SLinus Torvalds  * People with broken devices are free to experiment as to what gives
4801da177e4SLinus Torvalds  * the best results for them.
4811da177e4SLinus Torvalds  *
4821da177e4SLinus Torvalds  * USLEEP_SLEEP should be a minimum seek time.
4831da177e4SLinus Torvalds  *
4841da177e4SLinus Torvalds  * USLEEP_POLL should be a maximum rotational latency.
4851da177e4SLinus Torvalds  */
4861da177e4SLinus Torvalds #ifndef USLEEP_SLEEP
4871da177e4SLinus Torvalds /* 20 ms (reasonable hard disk speed) */
4881da177e4SLinus Torvalds #define USLEEP_SLEEP (20*HZ/1000)
4891da177e4SLinus Torvalds #endif
4901da177e4SLinus Torvalds /* 300 RPM (floppy speed) */
4911da177e4SLinus Torvalds #ifndef USLEEP_POLL
4921da177e4SLinus Torvalds #define USLEEP_POLL (200*HZ/1000)
4931da177e4SLinus Torvalds #endif
4941da177e4SLinus Torvalds #ifndef USLEEP_WAITLONG
4951da177e4SLinus Torvalds /* RvC: (reasonable time to wait on select error) */
4961da177e4SLinus Torvalds #define USLEEP_WAITLONG USLEEP_SLEEP
4971da177e4SLinus Torvalds #endif
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds /*
5001da177e4SLinus Torvalds  * Function : int should_disconnect (unsigned char cmd)
5011da177e4SLinus Torvalds  *
502d6e05edcSAndreas Mohr  * Purpose : decide whether a command would normally disconnect or
5031da177e4SLinus Torvalds  *      not, since if it won't disconnect we should go to sleep.
5041da177e4SLinus Torvalds  *
5051da177e4SLinus Torvalds  * Input : cmd - opcode of SCSI command
5061da177e4SLinus Torvalds  *
5071da177e4SLinus Torvalds  * Returns : DISCONNECT_LONG if we should disconnect for a really long
5081da177e4SLinus Torvalds  *      time (ie always, sleep, look for REQ active, sleep),
5091da177e4SLinus Torvalds  *      DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
5101da177e4SLinus Torvalds  *      time-to-data delay, DISCONNECT_NONE if this command would return
5111da177e4SLinus Torvalds  *      immediately.
5121da177e4SLinus Torvalds  *
5131da177e4SLinus Torvalds  *      Future sleep algorithms based on time to data can exploit
5141da177e4SLinus Torvalds  *      something like this so they can differentiate between "normal"
5151da177e4SLinus Torvalds  *      (ie, read, write, seek) and unusual commands (ie, * format).
5161da177e4SLinus Torvalds  *
5171da177e4SLinus Torvalds  * Note : We don't deal with commands that handle an immediate disconnect,
5181da177e4SLinus Torvalds  *
5191da177e4SLinus Torvalds  */
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds static int should_disconnect(unsigned char cmd)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	switch (cmd) {
5241da177e4SLinus Torvalds 	case READ_6:
5251da177e4SLinus Torvalds 	case WRITE_6:
5261da177e4SLinus Torvalds 	case SEEK_6:
5271da177e4SLinus Torvalds 	case READ_10:
5281da177e4SLinus Torvalds 	case WRITE_10:
5291da177e4SLinus Torvalds 	case SEEK_10:
5301da177e4SLinus Torvalds 		return DISCONNECT_TIME_TO_DATA;
5311da177e4SLinus Torvalds 	case FORMAT_UNIT:
5321da177e4SLinus Torvalds 	case SEARCH_HIGH:
5331da177e4SLinus Torvalds 	case SEARCH_LOW:
5341da177e4SLinus Torvalds 	case SEARCH_EQUAL:
5351da177e4SLinus Torvalds 		return DISCONNECT_LONG;
5361da177e4SLinus Torvalds 	default:
5371da177e4SLinus Torvalds 		return DISCONNECT_NONE;
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds static void NCR5380_set_timer(struct NCR5380_hostdata *hostdata, unsigned long timeout)
5421da177e4SLinus Torvalds {
5431da177e4SLinus Torvalds 	hostdata->time_expires = jiffies + timeout;
5441da177e4SLinus Torvalds 	schedule_delayed_work(&hostdata->coroutine, timeout);
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds static int probe_irq __initdata = 0;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds /**
5511da177e4SLinus Torvalds  *	probe_intr	-	helper for IRQ autoprobe
5521da177e4SLinus Torvalds  *	@irq: interrupt number
5531da177e4SLinus Torvalds  *	@dev_id: unused
5541da177e4SLinus Torvalds  *	@regs: unused
5551da177e4SLinus Torvalds  *
5561da177e4SLinus Torvalds  *	Set a flag to indicate the IRQ in question was received. This is
5571da177e4SLinus Torvalds  *	used by the IRQ probe code.
5581da177e4SLinus Torvalds  */
5591da177e4SLinus Torvalds 
5607d12e780SDavid Howells static irqreturn_t __init probe_intr(int irq, void *dev_id)
5611da177e4SLinus Torvalds {
5621da177e4SLinus Torvalds 	probe_irq = irq;
5631da177e4SLinus Torvalds 	return IRQ_HANDLED;
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds /**
5671da177e4SLinus Torvalds  *	NCR5380_probe_irq	-	find the IRQ of an NCR5380
5681da177e4SLinus Torvalds  *	@instance: NCR5380 controller
5691da177e4SLinus Torvalds  *	@possible: bitmask of ISA IRQ lines
5701da177e4SLinus Torvalds  *
5711da177e4SLinus Torvalds  *	Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
5721da177e4SLinus Torvalds  *	and then looking to see what interrupt actually turned up.
5731da177e4SLinus Torvalds  *
5741da177e4SLinus Torvalds  *	Locks: none, irqs must be enabled on entry
5751da177e4SLinus Torvalds  */
5761da177e4SLinus Torvalds 
577702809ceSAndrew Morton static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
578702809ceSAndrew Morton 						int possible)
5791da177e4SLinus Torvalds {
5801da177e4SLinus Torvalds 	NCR5380_local_declare();
5811da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
5821da177e4SLinus Torvalds 	unsigned long timeout;
5831da177e4SLinus Torvalds 	int trying_irqs, i, mask;
5841da177e4SLinus Torvalds 	NCR5380_setup(instance);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
5871d6f359aSThomas Gleixner 		if ((mask & possible) && (request_irq(i, &probe_intr, IRQF_DISABLED, "NCR-probe", NULL) == 0))
5881da177e4SLinus Torvalds 			trying_irqs |= mask;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	timeout = jiffies + (250 * HZ / 1000);
5911da177e4SLinus Torvalds 	probe_irq = SCSI_IRQ_NONE;
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	/*
5941da177e4SLinus Torvalds 	 * A interrupt is triggered whenever BSY = false, SEL = true
5951da177e4SLinus Torvalds 	 * and a bit set in the SELECT_ENABLE_REG is asserted on the
5961da177e4SLinus Torvalds 	 * SCSI bus.
5971da177e4SLinus Torvalds 	 *
5981da177e4SLinus Torvalds 	 * Note that the bus is only driven when the phase control signals
5991da177e4SLinus Torvalds 	 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
6001da177e4SLinus Torvalds 	 * to zero.
6011da177e4SLinus Torvalds 	 */
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, 0);
6041da177e4SLinus Torvalds 	NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
6051da177e4SLinus Torvalds 	NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
6061da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	while (probe_irq == SCSI_IRQ_NONE && time_before(jiffies, timeout))
609a9a3047dSNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	NCR5380_write(SELECT_ENABLE_REG, 0);
6121da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
6151da177e4SLinus Torvalds 		if (trying_irqs & mask)
6161da177e4SLinus Torvalds 			free_irq(i, NULL);
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	return probe_irq;
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds /**
6221da177e4SLinus Torvalds  *	NCR58380_print_options	-	show options
6231da177e4SLinus Torvalds  *	@instance: unused for now
6241da177e4SLinus Torvalds  *
6251da177e4SLinus Torvalds  *	Called by probe code indicating the NCR5380 driver options that
6261da177e4SLinus Torvalds  *	were selected. At some point this will switch to runtime options
6271da177e4SLinus Torvalds  *	read from the adapter in question
6281da177e4SLinus Torvalds  *
6291da177e4SLinus Torvalds  *	Locks: none
6301da177e4SLinus Torvalds  */
6311da177e4SLinus Torvalds 
632702809ceSAndrew Morton static void __init __maybe_unused
633702809ceSAndrew Morton NCR5380_print_options(struct Scsi_Host *instance)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	printk(" generic options"
6361da177e4SLinus Torvalds #ifdef AUTOPROBE_IRQ
6371da177e4SLinus Torvalds 	       " AUTOPROBE_IRQ"
6381da177e4SLinus Torvalds #endif
6391da177e4SLinus Torvalds #ifdef AUTOSENSE
6401da177e4SLinus Torvalds 	       " AUTOSENSE"
6411da177e4SLinus Torvalds #endif
6421da177e4SLinus Torvalds #ifdef DIFFERENTIAL
6431da177e4SLinus Torvalds 	       " DIFFERENTIAL"
6441da177e4SLinus Torvalds #endif
6451da177e4SLinus Torvalds #ifdef REAL_DMA
6461da177e4SLinus Torvalds 	       " REAL DMA"
6471da177e4SLinus Torvalds #endif
6481da177e4SLinus Torvalds #ifdef REAL_DMA_POLL
6491da177e4SLinus Torvalds 	       " REAL DMA POLL"
6501da177e4SLinus Torvalds #endif
6511da177e4SLinus Torvalds #ifdef PARITY
6521da177e4SLinus Torvalds 	       " PARITY"
6531da177e4SLinus Torvalds #endif
6541da177e4SLinus Torvalds #ifdef PSEUDO_DMA
6551da177e4SLinus Torvalds 	       " PSEUDO DMA"
6561da177e4SLinus Torvalds #endif
6571da177e4SLinus Torvalds #ifdef UNSAFE
6581da177e4SLinus Torvalds 	       " UNSAFE "
6591da177e4SLinus Torvalds #endif
6601da177e4SLinus Torvalds 	    );
6611da177e4SLinus Torvalds 	printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
6621da177e4SLinus Torvalds 	printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
6631da177e4SLinus Torvalds 	if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400) {
6641da177e4SLinus Torvalds 		printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE);
6651da177e4SLinus Torvalds 	}
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds /**
6691da177e4SLinus Torvalds  *	NCR5380_print_status 	-	dump controller info
6701da177e4SLinus Torvalds  *	@instance: controller to dump
6711da177e4SLinus Torvalds  *
6721da177e4SLinus Torvalds  *	Print commands in the various queues, called from NCR5380_abort
6731da177e4SLinus Torvalds  *	and NCR5380_debug to aid debugging.
6741da177e4SLinus Torvalds  *
6751da177e4SLinus Torvalds  *	Locks: called functions disable irqs
6761da177e4SLinus Torvalds  */
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds static void NCR5380_print_status(struct Scsi_Host *instance)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	NCR5380_dprint(NDEBUG_ANY, instance);
6811da177e4SLinus Torvalds 	NCR5380_dprint_phase(NDEBUG_ANY, instance);
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds /******************************************/
6851da177e4SLinus Torvalds /*
6861da177e4SLinus Torvalds  * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
6871da177e4SLinus Torvalds  *
6881da177e4SLinus Torvalds  * *buffer: I/O buffer
6891da177e4SLinus Torvalds  * **start: if inout == FALSE pointer into buffer where user read should start
6901da177e4SLinus Torvalds  * offset: current offset
6911da177e4SLinus Torvalds  * length: length of buffer
6921da177e4SLinus Torvalds  * hostno: Scsi_Host host_no
6931da177e4SLinus Torvalds  * inout: TRUE - user is writing; FALSE - user is reading
6941da177e4SLinus Torvalds  *
6951da177e4SLinus Torvalds  * Return the number of bytes read from or written
6961da177e4SLinus Torvalds  */
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds #undef SPRINTF
6991da177e4SLinus Torvalds #define SPRINTF(args...) do { if(pos < buffer + length-80) pos += sprintf(pos, ## args); } while(0)
7001da177e4SLinus Torvalds static
7011da177e4SLinus Torvalds char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length);
7021da177e4SLinus Torvalds static
7031da177e4SLinus Torvalds char *lprint_command(unsigned char *cmd, char *pos, char *buffer, int len);
7041da177e4SLinus Torvalds static
7051da177e4SLinus Torvalds char *lprint_opcode(int opcode, char *pos, char *buffer, int length);
7061da177e4SLinus Torvalds 
707702809ceSAndrew Morton static int __maybe_unused NCR5380_proc_info(struct Scsi_Host *instance,
708702809ceSAndrew Morton 	char *buffer, char **start, off_t offset, int length, int inout)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	char *pos = buffer;
7111da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata;
7121da177e4SLinus Torvalds 	Scsi_Cmnd *ptr;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	hostdata = (struct NCR5380_hostdata *) instance->hostdata;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	if (inout) {		/* Has data been written to the file ? */
7171da177e4SLinus Torvalds #ifdef DTC_PUBLIC_RELEASE
7181da177e4SLinus Torvalds 		dtc_wmaxi = dtc_maxi = 0;
7191da177e4SLinus Torvalds #endif
7201da177e4SLinus Torvalds #ifdef PAS16_PUBLIC_RELEASE
7211da177e4SLinus Torvalds 		pas_wmaxi = pas_maxi = 0;
7221da177e4SLinus Torvalds #endif
7231da177e4SLinus Torvalds 		return (-ENOSYS);	/* Currently this is a no-op */
7241da177e4SLinus Torvalds 	}
7251da177e4SLinus Torvalds 	SPRINTF("NCR5380 core release=%d.   ", NCR5380_PUBLIC_RELEASE);
7261da177e4SLinus Torvalds 	if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400)
7271da177e4SLinus Torvalds 		SPRINTF("ncr53c400 release=%d.  ", NCR53C400_PUBLIC_RELEASE);
7281da177e4SLinus Torvalds #ifdef DTC_PUBLIC_RELEASE
7291da177e4SLinus Torvalds 	SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE);
7301da177e4SLinus Torvalds #endif
7311da177e4SLinus Torvalds #ifdef T128_PUBLIC_RELEASE
7321da177e4SLinus Torvalds 	SPRINTF("T128 release %d", T128_PUBLIC_RELEASE);
7331da177e4SLinus Torvalds #endif
7341da177e4SLinus Torvalds #ifdef GENERIC_NCR5380_PUBLIC_RELEASE
7351da177e4SLinus Torvalds 	SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE);
7361da177e4SLinus Torvalds #endif
7371da177e4SLinus Torvalds #ifdef PAS16_PUBLIC_RELEASE
7381da177e4SLinus Torvalds 	SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE);
7391da177e4SLinus Torvalds #endif
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	SPRINTF("\nBase Addr: 0x%05lX    ", (long) instance->base);
7421da177e4SLinus Torvalds 	SPRINTF("io_port: %04x      ", (int) instance->io_port);
7431da177e4SLinus Torvalds 	if (instance->irq == SCSI_IRQ_NONE)
7441da177e4SLinus Torvalds 		SPRINTF("IRQ: None.\n");
7451da177e4SLinus Torvalds 	else
7461da177e4SLinus Torvalds 		SPRINTF("IRQ: %d.\n", instance->irq);
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds #ifdef DTC_PUBLIC_RELEASE
7491da177e4SLinus Torvalds 	SPRINTF("Highwater I/O busy_spin_counts -- write: %d  read: %d\n", dtc_wmaxi, dtc_maxi);
7501da177e4SLinus Torvalds #endif
7511da177e4SLinus Torvalds #ifdef PAS16_PUBLIC_RELEASE
7521da177e4SLinus Torvalds 	SPRINTF("Highwater I/O busy_spin_counts -- write: %d  read: %d\n", pas_wmaxi, pas_maxi);
7531da177e4SLinus Torvalds #endif
7541da177e4SLinus Torvalds 	spin_lock_irq(instance->host_lock);
7551da177e4SLinus Torvalds 	if (!hostdata->connected)
7561da177e4SLinus Torvalds 		SPRINTF("scsi%d: no currently connected command\n", instance->host_no);
7571da177e4SLinus Torvalds 	else
7581da177e4SLinus Torvalds 		pos = lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected, pos, buffer, length);
7591da177e4SLinus Torvalds 	SPRINTF("scsi%d: issue_queue\n", instance->host_no);
7601da177e4SLinus Torvalds 	for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
7611da177e4SLinus Torvalds 		pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	SPRINTF("scsi%d: disconnected_queue\n", instance->host_no);
7641da177e4SLinus Torvalds 	for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
7651da177e4SLinus Torvalds 		pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
7661da177e4SLinus Torvalds 	spin_unlock_irq(instance->host_lock);
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	*start = buffer;
7691da177e4SLinus Torvalds 	if (pos - buffer < offset)
7701da177e4SLinus Torvalds 		return 0;
7711da177e4SLinus Torvalds 	else if (pos - buffer - offset < length)
7721da177e4SLinus Torvalds 		return pos - buffer - offset;
7731da177e4SLinus Torvalds 	return length;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds static char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds 	SPRINTF("scsi%d : destination target %d, lun %d\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun);
7791da177e4SLinus Torvalds 	SPRINTF("        command = ");
7801da177e4SLinus Torvalds 	pos = lprint_command(cmd->cmnd, pos, buffer, length);
7811da177e4SLinus Torvalds 	return (pos);
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds static char *lprint_command(unsigned char *command, char *pos, char *buffer, int length)
7851da177e4SLinus Torvalds {
7861da177e4SLinus Torvalds 	int i, s;
7871da177e4SLinus Torvalds 	pos = lprint_opcode(command[0], pos, buffer, length);
7881da177e4SLinus Torvalds 	for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
7891da177e4SLinus Torvalds 		SPRINTF("%02x ", command[i]);
7901da177e4SLinus Torvalds 	SPRINTF("\n");
7911da177e4SLinus Torvalds 	return (pos);
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds static char *lprint_opcode(int opcode, char *pos, char *buffer, int length)
7951da177e4SLinus Torvalds {
7961da177e4SLinus Torvalds 	SPRINTF("%2d (0x%02x)", opcode, opcode);
7971da177e4SLinus Torvalds 	return (pos);
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds /**
8021da177e4SLinus Torvalds  *	NCR5380_init	-	initialise an NCR5380
8031da177e4SLinus Torvalds  *	@instance: adapter to configure
8041da177e4SLinus Torvalds  *	@flags: control flags
8051da177e4SLinus Torvalds  *
8061da177e4SLinus Torvalds  *	Initializes *instance and corresponding 5380 chip,
8071da177e4SLinus Torvalds  *      with flags OR'd into the initial flags value.
8081da177e4SLinus Torvalds  *
8091da177e4SLinus Torvalds  *	Notes : I assume that the host, hostno, and id bits have been
8101da177e4SLinus Torvalds  *      set correctly.  I don't care about the irq and other fields.
8111da177e4SLinus Torvalds  *
8121da177e4SLinus Torvalds  *	Returns 0 for success
8131da177e4SLinus Torvalds  *
8141da177e4SLinus Torvalds  *	Locks: interrupts must be enabled when we are called
8151da177e4SLinus Torvalds  */
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds static int __devinit NCR5380_init(struct Scsi_Host *instance, int flags)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	NCR5380_local_declare();
8201da177e4SLinus Torvalds 	int i, pass;
8211da177e4SLinus Torvalds 	unsigned long timeout;
8221da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	if(in_interrupt())
8251da177e4SLinus Torvalds 		printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
8261da177e4SLinus Torvalds 	/*
8271da177e4SLinus Torvalds 	 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
8281da177e4SLinus Torvalds 	 * the base address.
8291da177e4SLinus Torvalds 	 */
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds #ifdef NCR53C400
8321da177e4SLinus Torvalds 	if (flags & FLAG_NCR53C400)
8331da177e4SLinus Torvalds 		instance->NCR5380_instance_name += NCR53C400_address_adjust;
8341da177e4SLinus Torvalds #endif
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	NCR5380_setup(instance);
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	hostdata->aborted = 0;
8391da177e4SLinus Torvalds 	hostdata->id_mask = 1 << instance->this_id;
8401da177e4SLinus Torvalds 	for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
8411da177e4SLinus Torvalds 		if (i > hostdata->id_mask)
8421da177e4SLinus Torvalds 			hostdata->id_higher_mask |= i;
8431da177e4SLinus Torvalds 	for (i = 0; i < 8; ++i)
8441da177e4SLinus Torvalds 		hostdata->busy[i] = 0;
8451da177e4SLinus Torvalds #ifdef REAL_DMA
8461da177e4SLinus Torvalds 	hostdata->dmalen = 0;
8471da177e4SLinus Torvalds #endif
8481da177e4SLinus Torvalds 	hostdata->targets_present = 0;
8491da177e4SLinus Torvalds 	hostdata->connected = NULL;
8501da177e4SLinus Torvalds 	hostdata->issue_queue = NULL;
8511da177e4SLinus Torvalds 	hostdata->disconnected_queue = NULL;
8521da177e4SLinus Torvalds 
853c4028958SDavid Howells 	INIT_DELAYED_WORK(&hostdata->coroutine, NCR5380_main);
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds #ifdef NCR5380_STATS
8561da177e4SLinus Torvalds 	for (i = 0; i < 8; ++i) {
8571da177e4SLinus Torvalds 		hostdata->time_read[i] = 0;
8581da177e4SLinus Torvalds 		hostdata->time_write[i] = 0;
8591da177e4SLinus Torvalds 		hostdata->bytes_read[i] = 0;
8601da177e4SLinus Torvalds 		hostdata->bytes_write[i] = 0;
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 	hostdata->timebase = 0;
8631da177e4SLinus Torvalds 	hostdata->pendingw = 0;
8641da177e4SLinus Torvalds 	hostdata->pendingr = 0;
8651da177e4SLinus Torvalds #endif
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	/* The CHECK code seems to break the 53C400. Will check it later maybe */
8681da177e4SLinus Torvalds 	if (flags & FLAG_NCR53C400)
8691da177e4SLinus Torvalds 		hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
8701da177e4SLinus Torvalds 	else
8711da177e4SLinus Torvalds 		hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	hostdata->host = instance;
8741da177e4SLinus Torvalds 	hostdata->time_expires = 0;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds #ifndef AUTOSENSE
8771da177e4SLinus Torvalds 	if ((instance->cmd_per_lun > 1) || instance->can_queue > 1)
8781da177e4SLinus Torvalds 		    printk(KERN_WARNING "scsi%d : WARNING : support for multiple outstanding commands enabled\n" "         without AUTOSENSE option, contingent allegiance conditions may\n"
8791da177e4SLinus Torvalds 		    	   "         be incorrectly cleared.\n", instance->host_no);
8801da177e4SLinus Torvalds #endif				/* def AUTOSENSE */
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
8831da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
8841da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, 0);
8851da177e4SLinus Torvalds 	NCR5380_write(SELECT_ENABLE_REG, 0);
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds #ifdef NCR53C400
8881da177e4SLinus Torvalds 	if (hostdata->flags & FLAG_NCR53C400) {
8891da177e4SLinus Torvalds 		NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
8901da177e4SLinus Torvalds 	}
8911da177e4SLinus Torvalds #endif
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	/*
8941da177e4SLinus Torvalds 	 * Detect and correct bus wedge problems.
8951da177e4SLinus Torvalds 	 *
8961da177e4SLinus Torvalds 	 * If the system crashed, it may have crashed in a state
8971da177e4SLinus Torvalds 	 * where a SCSI command was still executing, and the
8981da177e4SLinus Torvalds 	 * SCSI bus is not in a BUS FREE STATE.
8991da177e4SLinus Torvalds 	 *
9001da177e4SLinus Torvalds 	 * If this is the case, we'll try to abort the currently
9011da177e4SLinus Torvalds 	 * established nexus which we know nothing about, and that
9021da177e4SLinus Torvalds 	 * failing, do a hard reset of the SCSI bus
9031da177e4SLinus Torvalds 	 */
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
9061da177e4SLinus Torvalds 		switch (pass) {
9071da177e4SLinus Torvalds 		case 1:
9081da177e4SLinus Torvalds 		case 3:
9091da177e4SLinus Torvalds 		case 5:
9101da177e4SLinus Torvalds 			printk(KERN_INFO "scsi%d: SCSI bus busy, waiting up to five seconds\n", instance->host_no);
9111da177e4SLinus Torvalds 			timeout = jiffies + 5 * HZ;
9121da177e4SLinus Torvalds 			NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, 0, 5*HZ);
9131da177e4SLinus Torvalds 			break;
9141da177e4SLinus Torvalds 		case 2:
9151da177e4SLinus Torvalds 			printk(KERN_WARNING "scsi%d: bus busy, attempting abort\n", instance->host_no);
9161da177e4SLinus Torvalds 			do_abort(instance);
9171da177e4SLinus Torvalds 			break;
9181da177e4SLinus Torvalds 		case 4:
9191da177e4SLinus Torvalds 			printk(KERN_WARNING "scsi%d: bus busy, attempting reset\n", instance->host_no);
9201da177e4SLinus Torvalds 			do_reset(instance);
9211da177e4SLinus Torvalds 			break;
9221da177e4SLinus Torvalds 		case 6:
9231da177e4SLinus Torvalds 			printk(KERN_ERR "scsi%d: bus locked solid or invalid override\n", instance->host_no);
9241da177e4SLinus Torvalds 			return -ENXIO;
9251da177e4SLinus Torvalds 		}
9261da177e4SLinus Torvalds 	}
9271da177e4SLinus Torvalds 	return 0;
9281da177e4SLinus Torvalds }
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds /**
9311da177e4SLinus Torvalds  *	NCR5380_exit	-	remove an NCR5380
9321da177e4SLinus Torvalds  *	@instance: adapter to remove
9331da177e4SLinus Torvalds  */
9341da177e4SLinus Torvalds 
935a43cf0f3SRandy Dunlap static void NCR5380_exit(struct Scsi_Host *instance)
9361da177e4SLinus Torvalds {
9371da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	cancel_delayed_work(&hostdata->coroutine);
9401da177e4SLinus Torvalds 	flush_scheduled_work();
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds /**
9441da177e4SLinus Torvalds  *	NCR5380_queue_command 		-	queue a command
9451da177e4SLinus Torvalds  *	@cmd: SCSI command
9461da177e4SLinus Torvalds  *	@done: completion handler
9471da177e4SLinus Torvalds  *
9481da177e4SLinus Torvalds  *      cmd is added to the per instance issue_queue, with minor
9491da177e4SLinus Torvalds  *      twiddling done to the host specific fields of cmd.  If the
9501da177e4SLinus Torvalds  *      main coroutine is not running, it is restarted.
9511da177e4SLinus Torvalds  *
9521da177e4SLinus Torvalds  *	Locks: host lock taken by caller
9531da177e4SLinus Torvalds  */
9541da177e4SLinus Torvalds 
955*f281233dSJeff Garzik static int NCR5380_queue_command_lck(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	struct Scsi_Host *instance = cmd->device->host;
9581da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
9591da177e4SLinus Torvalds 	Scsi_Cmnd *tmp;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds #if (NDEBUG & NDEBUG_NO_WRITE)
9621da177e4SLinus Torvalds 	switch (cmd->cmnd[0]) {
9631da177e4SLinus Torvalds 	case WRITE_6:
9641da177e4SLinus Torvalds 	case WRITE_10:
9651da177e4SLinus Torvalds 		printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
9661da177e4SLinus Torvalds 		cmd->result = (DID_ERROR << 16);
9671da177e4SLinus Torvalds 		done(cmd);
9681da177e4SLinus Torvalds 		return 0;
9691da177e4SLinus Torvalds 	}
9701da177e4SLinus Torvalds #endif				/* (NDEBUG & NDEBUG_NO_WRITE) */
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds #ifdef NCR5380_STATS
9731da177e4SLinus Torvalds 	switch (cmd->cmnd[0]) {
9741da177e4SLinus Torvalds 		case WRITE:
9751da177e4SLinus Torvalds 		case WRITE_6:
9761da177e4SLinus Torvalds 		case WRITE_10:
9771da177e4SLinus Torvalds 			hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase);
9789e0fe44dSBoaz Harrosh 			hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);
9791da177e4SLinus Torvalds 			hostdata->pendingw++;
9801da177e4SLinus Torvalds 			break;
9811da177e4SLinus Torvalds 		case READ:
9821da177e4SLinus Torvalds 		case READ_6:
9831da177e4SLinus Torvalds 		case READ_10:
9841da177e4SLinus Torvalds 			hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase);
9859e0fe44dSBoaz Harrosh 			hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);
9861da177e4SLinus Torvalds 			hostdata->pendingr++;
9871da177e4SLinus Torvalds 			break;
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds #endif
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	/*
9921da177e4SLinus Torvalds 	 * We use the host_scribble field as a pointer to the next command
9931da177e4SLinus Torvalds 	 * in a queue
9941da177e4SLinus Torvalds 	 */
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	cmd->host_scribble = NULL;
9971da177e4SLinus Torvalds 	cmd->scsi_done = done;
9981da177e4SLinus Torvalds 	cmd->result = 0;
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	/*
10011da177e4SLinus Torvalds 	 * Insert the cmd into the issue queue. Note that REQUEST SENSE
10021da177e4SLinus Torvalds 	 * commands are added to the head of the queue since any command will
10031da177e4SLinus Torvalds 	 * clear the contingent allegiance condition that exists and the
10041da177e4SLinus Torvalds 	 * sense data is only guaranteed to be valid while the condition exists.
10051da177e4SLinus Torvalds 	 */
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
10081da177e4SLinus Torvalds 		LIST(cmd, hostdata->issue_queue);
10091da177e4SLinus Torvalds 		cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
10101da177e4SLinus Torvalds 		hostdata->issue_queue = cmd;
10111da177e4SLinus Torvalds 	} else {
10121da177e4SLinus Torvalds 		for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (Scsi_Cmnd *) tmp->host_scribble);
10131da177e4SLinus Torvalds 		LIST(cmd, tmp);
10141da177e4SLinus Torvalds 		tmp->host_scribble = (unsigned char *) cmd;
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 	dprintk(NDEBUG_QUEUES, ("scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail"));
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	/* Run the coroutine if it isn't already running. */
10191da177e4SLinus Torvalds 	/* Kick off command processing */
1020c4028958SDavid Howells 	schedule_delayed_work(&hostdata->coroutine, 0);
10211da177e4SLinus Torvalds 	return 0;
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
1024*f281233dSJeff Garzik static DEF_SCSI_QCMD(NCR5380_queue_command)
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds /**
10271da177e4SLinus Torvalds  *	NCR5380_main	-	NCR state machines
10281da177e4SLinus Torvalds  *
10291da177e4SLinus Torvalds  *	NCR5380_main is a coroutine that runs as long as more work can
10301da177e4SLinus Torvalds  *      be done on the NCR5380 host adapters in a system.  Both
10311da177e4SLinus Torvalds  *      NCR5380_queue_command() and NCR5380_intr() will try to start it
10321da177e4SLinus Torvalds  *      in case it is not running.
10331da177e4SLinus Torvalds  *
10341da177e4SLinus Torvalds  *	Locks: called as its own thread with no locks held. Takes the
10351da177e4SLinus Torvalds  *	host lock and called routines may take the isa dma lock.
10361da177e4SLinus Torvalds  */
10371da177e4SLinus Torvalds 
1038c4028958SDavid Howells static void NCR5380_main(struct work_struct *work)
10391da177e4SLinus Torvalds {
1040c4028958SDavid Howells 	struct NCR5380_hostdata *hostdata =
1041c4028958SDavid Howells 		container_of(work, struct NCR5380_hostdata, coroutine.work);
10421da177e4SLinus Torvalds 	struct Scsi_Host *instance = hostdata->host;
10431da177e4SLinus Torvalds 	Scsi_Cmnd *tmp, *prev;
10441da177e4SLinus Torvalds 	int done;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	spin_lock_irq(instance->host_lock);
10471da177e4SLinus Torvalds 	do {
10481da177e4SLinus Torvalds 		/* Lock held here */
10491da177e4SLinus Torvalds 		done = 1;
10501da177e4SLinus Torvalds 		if (!hostdata->connected && !hostdata->selecting) {
10511da177e4SLinus Torvalds 			dprintk(NDEBUG_MAIN, ("scsi%d : not connected\n", instance->host_no));
10521da177e4SLinus Torvalds 			/*
10531da177e4SLinus Torvalds 			 * Search through the issue_queue for a command destined
10541da177e4SLinus Torvalds 			 * for a target that's not busy.
10551da177e4SLinus Torvalds 			 */
10561da177e4SLinus Torvalds 			for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
10571da177e4SLinus Torvalds 			{
10581da177e4SLinus Torvalds 				if (prev != tmp)
10591da177e4SLinus Torvalds 					dprintk(NDEBUG_LISTS, ("MAIN tmp=%p   target=%d   busy=%d lun=%d\n", tmp, tmp->target, hostdata->busy[tmp->target], tmp->lun));
10601da177e4SLinus Torvalds 				/*  When we find one, remove it from the issue queue. */
10611da177e4SLinus Torvalds 				if (!(hostdata->busy[tmp->device->id] & (1 << tmp->device->lun))) {
10621da177e4SLinus Torvalds 					if (prev) {
10631da177e4SLinus Torvalds 						REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
10641da177e4SLinus Torvalds 						prev->host_scribble = tmp->host_scribble;
10651da177e4SLinus Torvalds 					} else {
10661da177e4SLinus Torvalds 						REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
10671da177e4SLinus Torvalds 						hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
10681da177e4SLinus Torvalds 					}
10691da177e4SLinus Torvalds 					tmp->host_scribble = NULL;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 					/*
10721da177e4SLinus Torvalds 					 * Attempt to establish an I_T_L nexus here.
10731da177e4SLinus Torvalds 					 * On success, instance->hostdata->connected is set.
10741da177e4SLinus Torvalds 					 * On failure, we must add the command back to the
10751da177e4SLinus Torvalds 					 *   issue queue so we can keep trying.
10761da177e4SLinus Torvalds 					 */
10771da177e4SLinus Torvalds 					dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, ("scsi%d : main() : command for target %d lun %d removed from issue_queue\n", instance->host_no, tmp->target, tmp->lun));
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 					/*
10801da177e4SLinus Torvalds 					 * A successful selection is defined as one that
10811da177e4SLinus Torvalds 					 * leaves us with the command connected and
10821da177e4SLinus Torvalds 					 * in hostdata->connected, OR has terminated the
10831da177e4SLinus Torvalds 					 * command.
10841da177e4SLinus Torvalds 					 *
10851da177e4SLinus Torvalds 					 * With successful commands, we fall through
10861da177e4SLinus Torvalds 					 * and see if we can do an information transfer,
10871da177e4SLinus Torvalds 					 * with failures we will restart.
10881da177e4SLinus Torvalds 					 */
10891da177e4SLinus Torvalds 					hostdata->selecting = NULL;
10901da177e4SLinus Torvalds 					/* RvC: have to preset this to indicate a new command is being performed */
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 					if (!NCR5380_select(instance, tmp,
10931da177e4SLinus Torvalds 							    /*
10941da177e4SLinus Torvalds 							     * REQUEST SENSE commands are issued without tagged
10951da177e4SLinus Torvalds 							     * queueing, even on SCSI-II devices because the
10961da177e4SLinus Torvalds 							     * contingent allegiance condition exists for the
10971da177e4SLinus Torvalds 							     * entire unit.
10981da177e4SLinus Torvalds 							     */
10991da177e4SLinus Torvalds 							    (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
11001da177e4SLinus Torvalds 						break;
11011da177e4SLinus Torvalds 					} else {
11021da177e4SLinus Torvalds 						LIST(tmp, hostdata->issue_queue);
11031da177e4SLinus Torvalds 						tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
11041da177e4SLinus Torvalds 						hostdata->issue_queue = tmp;
11051da177e4SLinus Torvalds 						done = 0;
11061da177e4SLinus Torvalds 						dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, ("scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no));
11071da177e4SLinus Torvalds 					}
11081da177e4SLinus Torvalds 					/* lock held here still */
11091da177e4SLinus Torvalds 				}	/* if target/lun is not busy */
11101da177e4SLinus Torvalds 			}	/* for */
11111da177e4SLinus Torvalds 			/* exited locked */
11121da177e4SLinus Torvalds 		}	/* if (!hostdata->connected) */
11131da177e4SLinus Torvalds 		if (hostdata->selecting) {
11141da177e4SLinus Torvalds 			tmp = (Scsi_Cmnd *) hostdata->selecting;
11151da177e4SLinus Torvalds 			/* Selection will drop and retake the lock */
11161da177e4SLinus Torvalds 			if (!NCR5380_select(instance, tmp, (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
11171da177e4SLinus Torvalds 				/* Ok ?? */
11181da177e4SLinus Torvalds 			} else {
11191da177e4SLinus Torvalds 				/* RvC: device failed, so we wait a long time
11201da177e4SLinus Torvalds 				   this is needed for Mustek scanners, that
11211da177e4SLinus Torvalds 				   do not respond to commands immediately
11221da177e4SLinus Torvalds 				   after a scan */
11231da177e4SLinus Torvalds 				printk(KERN_DEBUG "scsi%d: device %d did not respond in time\n", instance->host_no, tmp->device->id);
11241da177e4SLinus Torvalds 				LIST(tmp, hostdata->issue_queue);
11251da177e4SLinus Torvalds 				tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
11261da177e4SLinus Torvalds 				hostdata->issue_queue = tmp;
11271da177e4SLinus Torvalds 				NCR5380_set_timer(hostdata, USLEEP_WAITLONG);
11281da177e4SLinus Torvalds 			}
11291da177e4SLinus Torvalds 		}	/* if hostdata->selecting */
11301da177e4SLinus Torvalds 		if (hostdata->connected
11311da177e4SLinus Torvalds #ifdef REAL_DMA
11321da177e4SLinus Torvalds 		    && !hostdata->dmalen
11331da177e4SLinus Torvalds #endif
11341da177e4SLinus Torvalds 		    && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies))
11351da177e4SLinus Torvalds 		    ) {
11361da177e4SLinus Torvalds 			dprintk(NDEBUG_MAIN, ("scsi%d : main() : performing information transfer\n", instance->host_no));
11371da177e4SLinus Torvalds 			NCR5380_information_transfer(instance);
11381da177e4SLinus Torvalds 			dprintk(NDEBUG_MAIN, ("scsi%d : main() : done set false\n", instance->host_no));
11391da177e4SLinus Torvalds 			done = 0;
11401da177e4SLinus Torvalds 		} else
11411da177e4SLinus Torvalds 			break;
11421da177e4SLinus Torvalds 	} while (!done);
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	spin_unlock_irq(instance->host_lock);
11451da177e4SLinus Torvalds }
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds #ifndef DONT_USE_INTR
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds /**
11501da177e4SLinus Torvalds  * 	NCR5380_intr	-	generic NCR5380 irq handler
11511da177e4SLinus Torvalds  *	@irq: interrupt number
11521da177e4SLinus Torvalds  *	@dev_id: device info
11531da177e4SLinus Torvalds  *
11541da177e4SLinus Torvalds  *	Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
11551da177e4SLinus Torvalds  *      from the disconnected queue, and restarting NCR5380_main()
11561da177e4SLinus Torvalds  *      as required.
11571da177e4SLinus Torvalds  *
11581da177e4SLinus Torvalds  *	Locks: takes the needed instance locks
11591da177e4SLinus Torvalds  */
11601da177e4SLinus Torvalds 
1161baa9aac6SJeff Garzik static irqreturn_t NCR5380_intr(int dummy, void *dev_id)
11621da177e4SLinus Torvalds {
11631da177e4SLinus Torvalds 	NCR5380_local_declare();
1164baa9aac6SJeff Garzik 	struct Scsi_Host *instance = dev_id;
11651da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
11661da177e4SLinus Torvalds 	int done;
11671da177e4SLinus Torvalds 	unsigned char basr;
11681da177e4SLinus Torvalds 	unsigned long flags;
11691da177e4SLinus Torvalds 
1170baa9aac6SJeff Garzik 	dprintk(NDEBUG_INTR, ("scsi : NCR5380 irq %d triggered\n",
1171baa9aac6SJeff Garzik 		instance->irq));
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	do {
11741da177e4SLinus Torvalds 		done = 1;
11751da177e4SLinus Torvalds 		spin_lock_irqsave(instance->host_lock, flags);
11761da177e4SLinus Torvalds 		/* Look for pending interrupts */
11771da177e4SLinus Torvalds 		NCR5380_setup(instance);
11781da177e4SLinus Torvalds 		basr = NCR5380_read(BUS_AND_STATUS_REG);
11791da177e4SLinus Torvalds 		/* XXX dispatch to appropriate routine if found and done=0 */
11801da177e4SLinus Torvalds 		if (basr & BASR_IRQ) {
11811da177e4SLinus Torvalds 			NCR5380_dprint(NDEBUG_INTR, instance);
11821da177e4SLinus Torvalds 			if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
11831da177e4SLinus Torvalds 				done = 0;
11841da177e4SLinus Torvalds 				dprintk(NDEBUG_INTR, ("scsi%d : SEL interrupt\n", instance->host_no));
11851da177e4SLinus Torvalds 				NCR5380_reselect(instance);
11861da177e4SLinus Torvalds 				(void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
11871da177e4SLinus Torvalds 			} else if (basr & BASR_PARITY_ERROR) {
11881da177e4SLinus Torvalds 				dprintk(NDEBUG_INTR, ("scsi%d : PARITY interrupt\n", instance->host_no));
11891da177e4SLinus Torvalds 				(void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
11901da177e4SLinus Torvalds 			} else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
11911da177e4SLinus Torvalds 				dprintk(NDEBUG_INTR, ("scsi%d : RESET interrupt\n", instance->host_no));
11921da177e4SLinus Torvalds 				(void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
11931da177e4SLinus Torvalds 			} else {
11941da177e4SLinus Torvalds #if defined(REAL_DMA)
11951da177e4SLinus Torvalds 				/*
11961da177e4SLinus Torvalds 				 * We should only get PHASE MISMATCH and EOP interrupts
11971da177e4SLinus Torvalds 				 * if we have DMA enabled, so do a sanity check based on
11981da177e4SLinus Torvalds 				 * the current setting of the MODE register.
11991da177e4SLinus Torvalds 				 */
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 				if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) {
12021da177e4SLinus Torvalds 					int transfered;
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 					if (!hostdata->connected)
12051da177e4SLinus Torvalds 						panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno);
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 					transfered = (hostdata->dmalen - NCR5380_dma_residual(instance));
12081da177e4SLinus Torvalds 					hostdata->connected->SCp.this_residual -= transferred;
12091da177e4SLinus Torvalds 					hostdata->connected->SCp.ptr += transferred;
12101da177e4SLinus Torvalds 					hostdata->dmalen = 0;
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 					(void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 					/* FIXME: we need to poll briefly then defer a workqueue task ! */
12151da177e4SLinus Torvalds 					NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2*HZ);
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 					NCR5380_write(MODE_REG, MR_BASE);
12181da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
12191da177e4SLinus Torvalds 				}
12201da177e4SLinus Torvalds #else
12211da177e4SLinus Torvalds 				dprintk(NDEBUG_INTR, ("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG)));
12221da177e4SLinus Torvalds 				(void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
12231da177e4SLinus Torvalds #endif
12241da177e4SLinus Torvalds 			}
12251da177e4SLinus Torvalds 		}	/* if BASR_IRQ */
12261da177e4SLinus Torvalds 		spin_unlock_irqrestore(instance->host_lock, flags);
12271da177e4SLinus Torvalds 		if(!done)
1228c4028958SDavid Howells 			schedule_delayed_work(&hostdata->coroutine, 0);
12291da177e4SLinus Torvalds 	} while (!done);
12301da177e4SLinus Torvalds 	return IRQ_HANDLED;
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds #endif
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds /**
12361da177e4SLinus Torvalds  *	collect_stats		-	collect stats on a scsi command
12371da177e4SLinus Torvalds  *	@hostdata: adapter
12381da177e4SLinus Torvalds  *	@cmd: command being issued
12391da177e4SLinus Torvalds  *
12401da177e4SLinus Torvalds  *	Update the statistical data by parsing the command in question
12411da177e4SLinus Torvalds  */
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds static void collect_stats(struct NCR5380_hostdata *hostdata, Scsi_Cmnd * cmd)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds #ifdef NCR5380_STATS
12461da177e4SLinus Torvalds 	switch (cmd->cmnd[0]) {
12471da177e4SLinus Torvalds 	case WRITE:
12481da177e4SLinus Torvalds 	case WRITE_6:
12491da177e4SLinus Torvalds 	case WRITE_10:
1250422c0d61SJeff Garzik 		hostdata->time_write[scmd_id(cmd)] += (jiffies - hostdata->timebase);
12511da177e4SLinus Torvalds 		hostdata->pendingw--;
12521da177e4SLinus Torvalds 		break;
12531da177e4SLinus Torvalds 	case READ:
12541da177e4SLinus Torvalds 	case READ_6:
12551da177e4SLinus Torvalds 	case READ_10:
1256422c0d61SJeff Garzik 		hostdata->time_read[scmd_id(cmd)] += (jiffies - hostdata->timebase);
12571da177e4SLinus Torvalds 		hostdata->pendingr--;
12581da177e4SLinus Torvalds 		break;
12591da177e4SLinus Torvalds 	}
12601da177e4SLinus Torvalds #endif
12611da177e4SLinus Torvalds }
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds /*
12651da177e4SLinus Torvalds  * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
12661da177e4SLinus Torvalds  *      int tag);
12671da177e4SLinus Torvalds  *
12681da177e4SLinus Torvalds  * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
12691da177e4SLinus Torvalds  *      including ARBITRATION, SELECTION, and initial message out for
12701da177e4SLinus Torvalds  *      IDENTIFY and queue messages.
12711da177e4SLinus Torvalds  *
12721da177e4SLinus Torvalds  * Inputs : instance - instantiation of the 5380 driver on which this
12731da177e4SLinus Torvalds  *      target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
12741da177e4SLinus Torvalds  *      new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
12751da177e4SLinus Torvalds  *      the command that is presently connected.
12761da177e4SLinus Torvalds  *
12771da177e4SLinus Torvalds  * Returns : -1 if selection could not execute for some reason,
12781da177e4SLinus Torvalds  *      0 if selection succeeded or failed because the target
12791da177e4SLinus Torvalds  *      did not respond.
12801da177e4SLinus Torvalds  *
12811da177e4SLinus Torvalds  * Side effects :
12821da177e4SLinus Torvalds  *      If bus busy, arbitration failed, etc, NCR5380_select() will exit
12831da177e4SLinus Torvalds  *              with registers as they should have been on entry - ie
12841da177e4SLinus Torvalds  *              SELECT_ENABLE will be set appropriately, the NCR5380
12851da177e4SLinus Torvalds  *              will cease to drive any SCSI bus signals.
12861da177e4SLinus Torvalds  *
12871da177e4SLinus Torvalds  *      If successful : I_T_L or I_T_L_Q nexus will be established,
12881da177e4SLinus Torvalds  *              instance->connected will be set to cmd.
12891da177e4SLinus Torvalds  *              SELECT interrupt will be disabled.
12901da177e4SLinus Torvalds  *
12911da177e4SLinus Torvalds  *      If failed (no target) : cmd->scsi_done() will be called, and the
12921da177e4SLinus Torvalds  *              cmd->result host byte set to DID_BAD_TARGET.
12931da177e4SLinus Torvalds  *
12941da177e4SLinus Torvalds  *	Locks: caller holds hostdata lock in IRQ mode
12951da177e4SLinus Torvalds  */
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag)
12981da177e4SLinus Torvalds {
12991da177e4SLinus Torvalds 	NCR5380_local_declare();
13001da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
13011da177e4SLinus Torvalds 	unsigned char tmp[3], phase;
13021da177e4SLinus Torvalds 	unsigned char *data;
13031da177e4SLinus Torvalds 	int len;
13041da177e4SLinus Torvalds 	unsigned long timeout;
13051da177e4SLinus Torvalds 	unsigned char value;
13061da177e4SLinus Torvalds 	int err;
13071da177e4SLinus Torvalds 	NCR5380_setup(instance);
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	if (hostdata->selecting)
13101da177e4SLinus Torvalds 		goto part2;
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 	hostdata->restart_select = 0;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	NCR5380_dprint(NDEBUG_ARBITRATION, instance);
13151da177e4SLinus Torvalds 	dprintk(NDEBUG_ARBITRATION, ("scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id));
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	/*
13181da177e4SLinus Torvalds 	 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
13191da177e4SLinus Torvalds 	 * data bus during SELECTION.
13201da177e4SLinus Torvalds 	 */
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, 0);
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	/*
13251da177e4SLinus Torvalds 	 * Start arbitration.
13261da177e4SLinus Torvalds 	 */
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
13291da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_ARBITRATE);
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	/* We can be relaxed here, interrupts are on, we are
13331da177e4SLinus Torvalds 	   in workqueue context, the birds are singing in the trees */
13341da177e4SLinus Torvalds 	spin_unlock_irq(instance->host_lock);
13351da177e4SLinus Torvalds 	err = NCR5380_poll_politely(instance, INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS, ICR_ARBITRATION_PROGRESS, 5*HZ);
13361da177e4SLinus Torvalds 	spin_lock_irq(instance->host_lock);
13371da177e4SLinus Torvalds 	if (err < 0) {
13381da177e4SLinus Torvalds 		printk(KERN_DEBUG "scsi: arbitration timeout at %d\n", __LINE__);
13391da177e4SLinus Torvalds 		NCR5380_write(MODE_REG, MR_BASE);
13401da177e4SLinus Torvalds 		NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
13411da177e4SLinus Torvalds 		goto failed;
13421da177e4SLinus Torvalds 	}
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	dprintk(NDEBUG_ARBITRATION, ("scsi%d : arbitration complete\n", instance->host_no));
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	/*
13471da177e4SLinus Torvalds 	 * The arbitration delay is 2.2us, but this is a minimum and there is
13481da177e4SLinus Torvalds 	 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
13491da177e4SLinus Torvalds 	 * the integral nature of udelay().
13501da177e4SLinus Torvalds 	 *
13511da177e4SLinus Torvalds 	 */
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	udelay(3);
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	/* Check for lost arbitration */
13561da177e4SLinus Torvalds 	if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) || (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
13571da177e4SLinus Torvalds 		NCR5380_write(MODE_REG, MR_BASE);
13581da177e4SLinus Torvalds 		dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no));
13591da177e4SLinus Torvalds 		goto failed;
13601da177e4SLinus Torvalds 	}
13611da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	if (!(hostdata->flags & FLAG_DTC3181E) &&
13641da177e4SLinus Torvalds 	    /* RvC: DTC3181E has some trouble with this
13651da177e4SLinus Torvalds 	     *      so we simply removed it. Seems to work with
13661da177e4SLinus Torvalds 	     *      only Mustek scanner attached
13671da177e4SLinus Torvalds 	     */
13681da177e4SLinus Torvalds 	    (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
13691da177e4SLinus Torvalds 		NCR5380_write(MODE_REG, MR_BASE);
13701da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
13711da177e4SLinus Torvalds 		dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no));
13721da177e4SLinus Torvalds 		goto failed;
13731da177e4SLinus Torvalds 	}
13741da177e4SLinus Torvalds 	/*
13751da177e4SLinus Torvalds 	 * Again, bus clear + bus settle time is 1.2us, however, this is
13761da177e4SLinus Torvalds 	 * a minimum so we'll udelay ceil(1.2)
13771da177e4SLinus Torvalds 	 */
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	udelay(2);
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	dprintk(NDEBUG_ARBITRATION, ("scsi%d : won arbitration\n", instance->host_no));
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	/*
13841da177e4SLinus Torvalds 	 * Now that we have won arbitration, start Selection process, asserting
13851da177e4SLinus Torvalds 	 * the host and target ID's on the SCSI bus.
13861da177e4SLinus Torvalds 	 */
13871da177e4SLinus Torvalds 
1388422c0d61SJeff Garzik 	NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	/*
13911da177e4SLinus Torvalds 	 * Raise ATN while SEL is true before BSY goes false from arbitration,
13921da177e4SLinus Torvalds 	 * since this is the only way to guarantee that we'll get a MESSAGE OUT
13931da177e4SLinus Torvalds 	 * phase immediately after selection.
13941da177e4SLinus Torvalds 	 */
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
13971da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	/*
14001da177e4SLinus Torvalds 	 * Reselect interrupts must be turned off prior to the dropping of BSY,
14011da177e4SLinus Torvalds 	 * otherwise we will trigger an interrupt.
14021da177e4SLinus Torvalds 	 */
14031da177e4SLinus Torvalds 	NCR5380_write(SELECT_ENABLE_REG, 0);
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	/*
14061da177e4SLinus Torvalds 	 * The initiator shall then wait at least two deskew delays and release
14071da177e4SLinus Torvalds 	 * the BSY signal.
14081da177e4SLinus Torvalds 	 */
14091da177e4SLinus Torvalds 	udelay(1);		/* wingel -- wait two bus deskew delay >2*45ns */
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	/* Reset BSY */
14121da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	/*
14151da177e4SLinus Torvalds 	 * Something weird happens when we cease to drive BSY - looks
14161da177e4SLinus Torvalds 	 * like the board/chip is letting us do another read before the
14171da177e4SLinus Torvalds 	 * appropriate propagation delay has expired, and we're confusing
14181da177e4SLinus Torvalds 	 * a BSY signal from ourselves as the target's response to SELECTION.
14191da177e4SLinus Torvalds 	 *
14201da177e4SLinus Torvalds 	 * A small delay (the 'C++' frontend breaks the pipeline with an
14211da177e4SLinus Torvalds 	 * unnecessary jump, making it work on my 386-33/Trantor T128, the
14221da177e4SLinus Torvalds 	 * tighter 'C' code breaks and requires this) solves the problem -
14231da177e4SLinus Torvalds 	 * the 1 us delay is arbitrary, and only used because this delay will
14241da177e4SLinus Torvalds 	 * be the same on other platforms and since it works here, it should
14251da177e4SLinus Torvalds 	 * work there.
14261da177e4SLinus Torvalds 	 *
14271da177e4SLinus Torvalds 	 * wingel suggests that this could be due to failing to wait
14281da177e4SLinus Torvalds 	 * one deskew delay.
14291da177e4SLinus Torvalds 	 */
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds 	udelay(1);
14321da177e4SLinus Torvalds 
1433422c0d61SJeff Garzik 	dprintk(NDEBUG_SELECTION, ("scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd)));
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	/*
14361da177e4SLinus Torvalds 	 * The SCSI specification calls for a 250 ms timeout for the actual
14371da177e4SLinus Torvalds 	 * selection.
14381da177e4SLinus Torvalds 	 */
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	timeout = jiffies + (250 * HZ / 1000);
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	/*
14431da177e4SLinus Torvalds 	 * XXX very interesting - we're seeing a bounce where the BSY we
14441da177e4SLinus Torvalds 	 * asserted is being reflected / still asserted (propagation delay?)
14451da177e4SLinus Torvalds 	 * and it's detecting as true.  Sigh.
14461da177e4SLinus Torvalds 	 */
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	hostdata->select_time = 0;	/* we count the clock ticks at which we polled */
14491da177e4SLinus Torvalds 	hostdata->selecting = cmd;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds part2:
14521da177e4SLinus Torvalds 	/* RvC: here we enter after a sleeping period, or immediately after
14531da177e4SLinus Torvalds 	   execution of part 1
14541da177e4SLinus Torvalds 	   we poll only once ech clock tick */
14551da177e4SLinus Torvalds 	value = NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO);
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	if (!value && (hostdata->select_time < HZ/4)) {
14581da177e4SLinus Torvalds 		/* RvC: we still must wait for a device response */
14591da177e4SLinus Torvalds 		hostdata->select_time++;	/* after 25 ticks the device has failed */
14601da177e4SLinus Torvalds 		NCR5380_set_timer(hostdata, 1);
14611da177e4SLinus Torvalds 		return 0;	/* RvC: we return here with hostdata->selecting set,
14621da177e4SLinus Torvalds 				   to go to sleep */
14631da177e4SLinus Torvalds 	}
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	hostdata->selecting = NULL;/* clear this pointer, because we passed the
14661da177e4SLinus Torvalds 					   waiting period */
14671da177e4SLinus Torvalds 	if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
14681da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
14691da177e4SLinus Torvalds 		NCR5380_reselect(instance);
14701da177e4SLinus Torvalds 		printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
14711da177e4SLinus Torvalds 		NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
14721da177e4SLinus Torvalds 		return -1;
14731da177e4SLinus Torvalds 	}
14741da177e4SLinus Torvalds 	/*
14751da177e4SLinus Torvalds 	 * No less than two deskew delays after the initiator detects the
14761da177e4SLinus Torvalds 	 * BSY signal is true, it shall release the SEL signal and may
14771da177e4SLinus Torvalds 	 * change the DATA BUS.                                     -wingel
14781da177e4SLinus Torvalds 	 */
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 	udelay(1);
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 	if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
14851da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1486422c0d61SJeff Garzik 		if (hostdata->targets_present & (1 << scmd_id(cmd))) {
14871da177e4SLinus Torvalds 			printk(KERN_DEBUG "scsi%d : weirdness\n", instance->host_no);
14881da177e4SLinus Torvalds 			if (hostdata->restart_select)
14891da177e4SLinus Torvalds 				printk(KERN_DEBUG "\trestart select\n");
14901da177e4SLinus Torvalds 			NCR5380_dprint(NDEBUG_SELECTION, instance);
14911da177e4SLinus Torvalds 			NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
14921da177e4SLinus Torvalds 			return -1;
14931da177e4SLinus Torvalds 		}
14941da177e4SLinus Torvalds 		cmd->result = DID_BAD_TARGET << 16;
14951da177e4SLinus Torvalds 		collect_stats(hostdata, cmd);
14961da177e4SLinus Torvalds 		cmd->scsi_done(cmd);
14971da177e4SLinus Torvalds 		NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
14981da177e4SLinus Torvalds 		dprintk(NDEBUG_SELECTION, ("scsi%d : target did not respond within 250ms\n", instance->host_no));
14991da177e4SLinus Torvalds 		NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
15001da177e4SLinus Torvalds 		return 0;
15011da177e4SLinus Torvalds 	}
1502422c0d61SJeff Garzik 	hostdata->targets_present |= (1 << scmd_id(cmd));
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds 	/*
15051da177e4SLinus Torvalds 	 * Since we followed the SCSI spec, and raised ATN while SEL
15061da177e4SLinus Torvalds 	 * was true but before BSY was false during selection, the information
15071da177e4SLinus Torvalds 	 * transfer phase should be a MESSAGE OUT phase so that we can send the
15081da177e4SLinus Torvalds 	 * IDENTIFY message.
15091da177e4SLinus Torvalds 	 *
15101da177e4SLinus Torvalds 	 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
15111da177e4SLinus Torvalds 	 * message (2 bytes) with a tag ID that we increment with every command
15121da177e4SLinus Torvalds 	 * until it wraps back to 0.
15131da177e4SLinus Torvalds 	 *
15141da177e4SLinus Torvalds 	 * XXX - it turns out that there are some broken SCSI-II devices,
15151da177e4SLinus Torvalds 	 *       which claim to support tagged queuing but fail when more than
15161da177e4SLinus Torvalds 	 *       some number of commands are issued at once.
15171da177e4SLinus Torvalds 	 */
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds 	/* Wait for start of REQ/ACK handshake */
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	spin_unlock_irq(instance->host_lock);
15221da177e4SLinus Torvalds 	err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
15231da177e4SLinus Torvalds 	spin_lock_irq(instance->host_lock);
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	if(err) {
15261da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__);
15271da177e4SLinus Torvalds 		NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
15281da177e4SLinus Torvalds 		goto failed;
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	dprintk(NDEBUG_SELECTION, ("scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id));
15321da177e4SLinus Torvalds 	tmp[0] = IDENTIFY(((instance->irq == SCSI_IRQ_NONE) ? 0 : 1), cmd->device->lun);
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	len = 1;
15351da177e4SLinus Torvalds 	cmd->tag = 0;
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	/* Send message(s) */
15381da177e4SLinus Torvalds 	data = tmp;
15391da177e4SLinus Torvalds 	phase = PHASE_MSGOUT;
15401da177e4SLinus Torvalds 	NCR5380_transfer_pio(instance, &phase, &len, &data);
15411da177e4SLinus Torvalds 	dprintk(NDEBUG_SELECTION, ("scsi%d : nexus established.\n", instance->host_no));
15421da177e4SLinus Torvalds 	/* XXX need to handle errors here */
15431da177e4SLinus Torvalds 	hostdata->connected = cmd;
15441da177e4SLinus Torvalds 	hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	initialize_SCp(cmd);
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	return 0;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	/* Selection failed */
15511da177e4SLinus Torvalds failed:
15521da177e4SLinus Torvalds 	return -1;
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds }
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds /*
15571da177e4SLinus Torvalds  * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
15581da177e4SLinus Torvalds  *      unsigned char *phase, int *count, unsigned char **data)
15591da177e4SLinus Torvalds  *
15601da177e4SLinus Torvalds  * Purpose : transfers data in given phase using polled I/O
15611da177e4SLinus Torvalds  *
15621da177e4SLinus Torvalds  * Inputs : instance - instance of driver, *phase - pointer to
15631da177e4SLinus Torvalds  *      what phase is expected, *count - pointer to number of
15641da177e4SLinus Torvalds  *      bytes to transfer, **data - pointer to data pointer.
15651da177e4SLinus Torvalds  *
15661da177e4SLinus Torvalds  * Returns : -1 when different phase is entered without transferring
15671da177e4SLinus Torvalds  *      maximum number of bytes, 0 if all bytes or transfered or exit
15681da177e4SLinus Torvalds  *      is in same phase.
15691da177e4SLinus Torvalds  *
15701da177e4SLinus Torvalds  *      Also, *phase, *count, *data are modified in place.
15711da177e4SLinus Torvalds  *
15721da177e4SLinus Torvalds  * XXX Note : handling for bus free may be useful.
15731da177e4SLinus Torvalds  */
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds /*
15761da177e4SLinus Torvalds  * Note : this code is not as quick as it could be, however it
15771da177e4SLinus Torvalds  * IS 100% reliable, and for the actual data transfer where speed
15781da177e4SLinus Torvalds  * counts, we will always do a pseudo DMA or DMA transfer.
15791da177e4SLinus Torvalds  */
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
15821da177e4SLinus Torvalds 	NCR5380_local_declare();
15831da177e4SLinus Torvalds 	unsigned char p = *phase, tmp;
15841da177e4SLinus Torvalds 	int c = *count;
15851da177e4SLinus Torvalds 	unsigned char *d = *data;
15861da177e4SLinus Torvalds 	/*
15871da177e4SLinus Torvalds 	 *      RvC: some administrative data to process polling time
15881da177e4SLinus Torvalds 	 */
15891da177e4SLinus Torvalds 	int break_allowed = 0;
15901da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
15911da177e4SLinus Torvalds 	NCR5380_setup(instance);
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	if (!(p & SR_IO))
15941da177e4SLinus Torvalds 		dprintk(NDEBUG_PIO, ("scsi%d : pio write %d bytes\n", instance->host_no, c));
15951da177e4SLinus Torvalds 	else
15961da177e4SLinus Torvalds 		dprintk(NDEBUG_PIO, ("scsi%d : pio read %d bytes\n", instance->host_no, c));
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	/*
15991da177e4SLinus Torvalds 	 * The NCR5380 chip will only drive the SCSI bus when the
16001da177e4SLinus Torvalds 	 * phase specified in the appropriate bits of the TARGET COMMAND
16011da177e4SLinus Torvalds 	 * REGISTER match the STATUS REGISTER
16021da177e4SLinus Torvalds 	 */
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	/* RvC: don't know if this is necessary, but other SCSI I/O is short
16071da177e4SLinus Torvalds 	 *      so breaks are not necessary there
16081da177e4SLinus Torvalds 	 */
16091da177e4SLinus Torvalds 	if ((p == PHASE_DATAIN) || (p == PHASE_DATAOUT)) {
16101da177e4SLinus Torvalds 		break_allowed = 1;
16111da177e4SLinus Torvalds 	}
16121da177e4SLinus Torvalds 	do {
16131da177e4SLinus Torvalds 		/*
16141da177e4SLinus Torvalds 		 * Wait for assertion of REQ, after which the phase bits will be
16151da177e4SLinus Torvalds 		 * valid
16161da177e4SLinus Torvalds 		 */
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 		/* RvC: we simply poll once, after that we stop temporarily
16191da177e4SLinus Torvalds 		 *      and let the device buffer fill up
16201da177e4SLinus Torvalds 		 *      if breaking is not allowed, we keep polling as long as needed
16211da177e4SLinus Torvalds 		 */
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 		/* FIXME */
16241da177e4SLinus Torvalds 		while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) && !break_allowed);
16251da177e4SLinus Torvalds 		if (!(tmp & SR_REQ)) {
16261da177e4SLinus Torvalds 			/* timeout condition */
16271da177e4SLinus Torvalds 			NCR5380_set_timer(hostdata, USLEEP_SLEEP);
16281da177e4SLinus Torvalds 			break;
16291da177e4SLinus Torvalds 		}
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 		dprintk(NDEBUG_HANDSHAKE, ("scsi%d : REQ detected\n", instance->host_no));
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 		/* Check for phase mismatch */
16341da177e4SLinus Torvalds 		if ((tmp & PHASE_MASK) != p) {
16351da177e4SLinus Torvalds 			dprintk(NDEBUG_HANDSHAKE, ("scsi%d : phase mismatch\n", instance->host_no));
16361da177e4SLinus Torvalds 			NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
16371da177e4SLinus Torvalds 			break;
16381da177e4SLinus Torvalds 		}
16391da177e4SLinus Torvalds 		/* Do actual transfer from SCSI bus to / from memory */
16401da177e4SLinus Torvalds 		if (!(p & SR_IO))
16411da177e4SLinus Torvalds 			NCR5380_write(OUTPUT_DATA_REG, *d);
16421da177e4SLinus Torvalds 		else
16431da177e4SLinus Torvalds 			*d = NCR5380_read(CURRENT_SCSI_DATA_REG);
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 		++d;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 		/*
16481da177e4SLinus Torvalds 		 * The SCSI standard suggests that in MSGOUT phase, the initiator
16491da177e4SLinus Torvalds 		 * should drop ATN on the last byte of the message phase
16501da177e4SLinus Torvalds 		 * after REQ has been asserted for the handshake but before
16511da177e4SLinus Torvalds 		 * the initiator raises ACK.
16521da177e4SLinus Torvalds 		 */
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 		if (!(p & SR_IO)) {
16551da177e4SLinus Torvalds 			if (!((p & SR_MSG) && c > 1)) {
16561da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
16571da177e4SLinus Torvalds 				NCR5380_dprint(NDEBUG_PIO, instance);
16581da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
16591da177e4SLinus Torvalds 			} else {
16601da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
16611da177e4SLinus Torvalds 				NCR5380_dprint(NDEBUG_PIO, instance);
16621da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
16631da177e4SLinus Torvalds 			}
16641da177e4SLinus Torvalds 		} else {
16651da177e4SLinus Torvalds 			NCR5380_dprint(NDEBUG_PIO, instance);
16661da177e4SLinus Torvalds 			NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
16671da177e4SLinus Torvalds 		}
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 		/* FIXME - if this fails bus reset ?? */
16701da177e4SLinus Torvalds 		NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 5*HZ);
16711da177e4SLinus Torvalds 		dprintk(NDEBUG_HANDSHAKE, ("scsi%d : req false, handshake complete\n", instance->host_no));
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds /*
16741da177e4SLinus Torvalds  * We have several special cases to consider during REQ/ACK handshaking :
16751da177e4SLinus Torvalds  * 1.  We were in MSGOUT phase, and we are on the last byte of the
16761da177e4SLinus Torvalds  *      message.  ATN must be dropped as ACK is dropped.
16771da177e4SLinus Torvalds  *
16781da177e4SLinus Torvalds  * 2.  We are in a MSGIN phase, and we are on the last byte of the
16791da177e4SLinus Torvalds  *      message.  We must exit with ACK asserted, so that the calling
16801da177e4SLinus Torvalds  *      code may raise ATN before dropping ACK to reject the message.
16811da177e4SLinus Torvalds  *
16821da177e4SLinus Torvalds  * 3.  ACK and ATN are clear and the target may proceed as normal.
16831da177e4SLinus Torvalds  */
16841da177e4SLinus Torvalds 		if (!(p == PHASE_MSGIN && c == 1)) {
16851da177e4SLinus Torvalds 			if (p == PHASE_MSGOUT && c > 1)
16861da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
16871da177e4SLinus Torvalds 			else
16881da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
16891da177e4SLinus Torvalds 		}
16901da177e4SLinus Torvalds 	} while (--c);
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	dprintk(NDEBUG_PIO, ("scsi%d : residual %d\n", instance->host_no, c));
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	*count = c;
16951da177e4SLinus Torvalds 	*data = d;
16961da177e4SLinus Torvalds 	tmp = NCR5380_read(STATUS_REG);
16971da177e4SLinus Torvalds 	if (tmp & SR_REQ)
16981da177e4SLinus Torvalds 		*phase = tmp & PHASE_MASK;
16991da177e4SLinus Torvalds 	else
17001da177e4SLinus Torvalds 		*phase = PHASE_UNKNOWN;
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	if (!c || (*phase == p))
17031da177e4SLinus Torvalds 		return 0;
17041da177e4SLinus Torvalds 	else
17051da177e4SLinus Torvalds 		return -1;
17061da177e4SLinus Torvalds }
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds /**
17091da177e4SLinus Torvalds  *	do_reset	-	issue a reset command
17101da177e4SLinus Torvalds  *	@host: adapter to reset
17111da177e4SLinus Torvalds  *
17121da177e4SLinus Torvalds  *	Issue a reset sequence to the NCR5380 and try and get the bus
17131da177e4SLinus Torvalds  *	back into sane shape.
17141da177e4SLinus Torvalds  *
17151da177e4SLinus Torvalds  *	Locks: caller holds queue lock
17161da177e4SLinus Torvalds  */
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds static void do_reset(struct Scsi_Host *host) {
17191da177e4SLinus Torvalds 	NCR5380_local_declare();
17201da177e4SLinus Torvalds 	NCR5380_setup(host);
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
17231da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
17241da177e4SLinus Torvalds 	udelay(25);
17251da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
17261da177e4SLinus Torvalds }
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds /*
17291da177e4SLinus Torvalds  * Function : do_abort (Scsi_Host *host)
17301da177e4SLinus Torvalds  *
17311da177e4SLinus Torvalds  * Purpose : abort the currently established nexus.  Should only be
17321da177e4SLinus Torvalds  *      called from a routine which can drop into a
17331da177e4SLinus Torvalds  *
17341da177e4SLinus Torvalds  * Returns : 0 on success, -1 on failure.
17351da177e4SLinus Torvalds  *
17361da177e4SLinus Torvalds  * Locks: queue lock held by caller
17371da177e4SLinus Torvalds  *	FIXME: sort this out and get new_eh running
17381da177e4SLinus Torvalds  */
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds static int do_abort(struct Scsi_Host *host) {
17411da177e4SLinus Torvalds 	NCR5380_local_declare();
17421da177e4SLinus Torvalds 	unsigned char *msgptr, phase, tmp;
17431da177e4SLinus Torvalds 	int len;
17441da177e4SLinus Torvalds 	int rc;
17451da177e4SLinus Torvalds 	NCR5380_setup(host);
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	/* Request message out phase */
17491da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	/*
17521da177e4SLinus Torvalds 	 * Wait for the target to indicate a valid phase by asserting
17531da177e4SLinus Torvalds 	 * REQ.  Once this happens, we'll have either a MSGOUT phase
17541da177e4SLinus Torvalds 	 * and can immediately send the ABORT message, or we'll have some
17551da177e4SLinus Torvalds 	 * other phase and will have to source/sink data.
17561da177e4SLinus Torvalds 	 *
17571da177e4SLinus Torvalds 	 * We really don't care what value was on the bus or what value
17581da177e4SLinus Torvalds 	 * the target sees, so we just handshake.
17591da177e4SLinus Torvalds 	 */
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, SR_REQ, 60 * HZ);
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds 	if(rc < 0)
17641da177e4SLinus Torvalds 		return -1;
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	tmp = (unsigned char)rc;
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
17711da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
17721da177e4SLinus Torvalds 		rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, 0, 3*HZ);
17731da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
17741da177e4SLinus Torvalds 		if(rc == -1)
17751da177e4SLinus Torvalds 			return -1;
17761da177e4SLinus Torvalds 	}
17771da177e4SLinus Torvalds 	tmp = ABORT;
17781da177e4SLinus Torvalds 	msgptr = &tmp;
17791da177e4SLinus Torvalds 	len = 1;
17801da177e4SLinus Torvalds 	phase = PHASE_MSGOUT;
17811da177e4SLinus Torvalds 	NCR5380_transfer_pio(host, &phase, &len, &msgptr);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	/*
17841da177e4SLinus Torvalds 	 * If we got here, and the command completed successfully,
17851da177e4SLinus Torvalds 	 * we're about to go into bus free state.
17861da177e4SLinus Torvalds 	 */
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	return len ? -1 : 0;
17891da177e4SLinus Torvalds }
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
17921da177e4SLinus Torvalds /*
17931da177e4SLinus Torvalds  * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
17941da177e4SLinus Torvalds  *      unsigned char *phase, int *count, unsigned char **data)
17951da177e4SLinus Torvalds  *
17961da177e4SLinus Torvalds  * Purpose : transfers data in given phase using either real
17971da177e4SLinus Torvalds  *      or pseudo DMA.
17981da177e4SLinus Torvalds  *
17991da177e4SLinus Torvalds  * Inputs : instance - instance of driver, *phase - pointer to
18001da177e4SLinus Torvalds  *      what phase is expected, *count - pointer to number of
18011da177e4SLinus Torvalds  *      bytes to transfer, **data - pointer to data pointer.
18021da177e4SLinus Torvalds  *
18031da177e4SLinus Torvalds  * Returns : -1 when different phase is entered without transferring
18041da177e4SLinus Torvalds  *      maximum number of bytes, 0 if all bytes or transfered or exit
18051da177e4SLinus Torvalds  *      is in same phase.
18061da177e4SLinus Torvalds  *
18071da177e4SLinus Torvalds  *      Also, *phase, *count, *data are modified in place.
18081da177e4SLinus Torvalds  *
18091da177e4SLinus Torvalds  *	Locks: io_request lock held by caller
18101da177e4SLinus Torvalds  */
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
18141da177e4SLinus Torvalds 	NCR5380_local_declare();
18151da177e4SLinus Torvalds 	register int c = *count;
18161da177e4SLinus Torvalds 	register unsigned char p = *phase;
18171da177e4SLinus Torvalds 	register unsigned char *d = *data;
18181da177e4SLinus Torvalds 	unsigned char tmp;
18191da177e4SLinus Torvalds 	int foo;
18201da177e4SLinus Torvalds #if defined(REAL_DMA_POLL)
18211da177e4SLinus Torvalds 	int cnt, toPIO;
18221da177e4SLinus Torvalds 	unsigned char saved_data = 0, overrun = 0, residue;
18231da177e4SLinus Torvalds #endif
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	NCR5380_setup(instance);
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
18301da177e4SLinus Torvalds 		*phase = tmp;
18311da177e4SLinus Torvalds 		return -1;
18321da177e4SLinus Torvalds 	}
18331da177e4SLinus Torvalds #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
18341da177e4SLinus Torvalds #ifdef READ_OVERRUNS
18351da177e4SLinus Torvalds 	if (p & SR_IO) {
18361da177e4SLinus Torvalds 		c -= 2;
18371da177e4SLinus Torvalds 	}
18381da177e4SLinus Torvalds #endif
18391da177e4SLinus Torvalds 	dprintk(NDEBUG_DMA, ("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n", instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" : "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d));
18401da177e4SLinus Torvalds 	hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
18411da177e4SLinus Torvalds #endif
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds #ifdef REAL_DMA
18461da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
18471da177e4SLinus Torvalds #elif defined(REAL_DMA_POLL)
18481da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
18491da177e4SLinus Torvalds #else
18501da177e4SLinus Torvalds 	/*
18511da177e4SLinus Torvalds 	 * Note : on my sample board, watch-dog timeouts occurred when interrupts
18521da177e4SLinus Torvalds 	 * were not disabled for the duration of a single DMA transfer, from
18531da177e4SLinus Torvalds 	 * before the setting of DMA mode to after transfer of the last byte.
18541da177e4SLinus Torvalds 	 */
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds #if defined(PSEUDO_DMA) && defined(UNSAFE)
18571da177e4SLinus Torvalds 	spin_unlock_irq(instance->host_lock);
18581da177e4SLinus Torvalds #endif
18591da177e4SLinus Torvalds 	/* KLL May need eop and parity in 53c400 */
18601da177e4SLinus Torvalds 	if (hostdata->flags & FLAG_NCR53C400)
1861085267acSRoel Kluin 		NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE |
1862085267acSRoel Kluin 				MR_ENABLE_PAR_CHECK | MR_ENABLE_PAR_INTR |
1863085267acSRoel Kluin 				MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
18641da177e4SLinus Torvalds 	else
18651da177e4SLinus Torvalds 		NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
18661da177e4SLinus Torvalds #endif				/* def REAL_DMA */
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	dprintk(NDEBUG_DMA, ("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG)));
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 	/*
18711da177e4SLinus Torvalds 	 *	On the PAS16 at least I/O recovery delays are not needed here.
18721da177e4SLinus Torvalds 	 *	Everyone else seems to want them.
18731da177e4SLinus Torvalds 	 */
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 	if (p & SR_IO) {
18761da177e4SLinus Torvalds 		io_recovery_delay(1);
18771da177e4SLinus Torvalds 		NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
18781da177e4SLinus Torvalds 	} else {
18791da177e4SLinus Torvalds 		io_recovery_delay(1);
18801da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
18811da177e4SLinus Torvalds 		io_recovery_delay(1);
18821da177e4SLinus Torvalds 		NCR5380_write(START_DMA_SEND_REG, 0);
18831da177e4SLinus Torvalds 		io_recovery_delay(1);
18841da177e4SLinus Torvalds 	}
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds #if defined(REAL_DMA_POLL)
18871da177e4SLinus Torvalds 	do {
18881da177e4SLinus Torvalds 		tmp = NCR5380_read(BUS_AND_STATUS_REG);
18891da177e4SLinus Torvalds 	} while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
18901da177e4SLinus Torvalds 
18911da177e4SLinus Torvalds /*
18921da177e4SLinus Torvalds    At this point, either we've completed DMA, or we have a phase mismatch,
18931da177e4SLinus Torvalds    or we've unexpectedly lost BUSY (which is a real error).
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds    For write DMAs, we want to wait until the last byte has been
18961da177e4SLinus Torvalds    transferred out over the bus before we turn off DMA mode.  Alas, there
18971da177e4SLinus Torvalds    seems to be no terribly good way of doing this on a 5380 under all
18981da177e4SLinus Torvalds    conditions.  For non-scatter-gather operations, we can wait until REQ
18991da177e4SLinus Torvalds    and ACK both go false, or until a phase mismatch occurs.  Gather-writes
19001da177e4SLinus Torvalds    are nastier, since the device will be expecting more data than we
19011da177e4SLinus Torvalds    are prepared to send it, and REQ will remain asserted.  On a 53C8[01] we
19021da177e4SLinus Torvalds    could test LAST BIT SENT to assure transfer (I imagine this is precisely
19031da177e4SLinus Torvalds    why this signal was added to the newer chips) but on the older 538[01]
19041da177e4SLinus Torvalds    this signal does not exist.  The workaround for this lack is a watchdog;
19051da177e4SLinus Torvalds    we bail out of the wait-loop after a modest amount of wait-time if
19061da177e4SLinus Torvalds    the usual exit conditions are not met.  Not a terribly clean or
19071da177e4SLinus Torvalds    correct solution :-%
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds    Reads are equally tricky due to a nasty characteristic of the NCR5380.
19101da177e4SLinus Torvalds    If the chip is in DMA mode for an READ, it will respond to a target's
19111da177e4SLinus Torvalds    REQ by latching the SCSI data into the INPUT DATA register and asserting
19121da177e4SLinus Torvalds    ACK, even if it has _already_ been notified by the DMA controller that
19131da177e4SLinus Torvalds    the current DMA transfer has completed!  If the NCR5380 is then taken
19141da177e4SLinus Torvalds    out of DMA mode, this already-acknowledged byte is lost.
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds    This is not a problem for "one DMA transfer per command" reads, because
19171da177e4SLinus Torvalds    the situation will never arise... either all of the data is DMA'ed
19181da177e4SLinus Torvalds    properly, or the target switches to MESSAGE IN phase to signal a
19191da177e4SLinus Torvalds    disconnection (either operation bringing the DMA to a clean halt).
19201da177e4SLinus Torvalds    However, in order to handle scatter-reads, we must work around the
19211da177e4SLinus Torvalds    problem.  The chosen fix is to DMA N-2 bytes, then check for the
19221da177e4SLinus Torvalds    condition before taking the NCR5380 out of DMA mode.  One or two extra
19231da177e4SLinus Torvalds    bytes are transferred via PIO as necessary to fill out the original
19241da177e4SLinus Torvalds    request.
19251da177e4SLinus Torvalds  */
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds 	if (p & SR_IO) {
19281da177e4SLinus Torvalds #ifdef READ_OVERRUNS
19291da177e4SLinus Torvalds 		udelay(10);
19301da177e4SLinus Torvalds 		if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
19311da177e4SLinus Torvalds 			saved_data = NCR5380_read(INPUT_DATA_REGISTER);
19321da177e4SLinus Torvalds 			overrun = 1;
19331da177e4SLinus Torvalds 		}
19341da177e4SLinus Torvalds #endif
19351da177e4SLinus Torvalds 	} else {
19361da177e4SLinus Torvalds 		int limit = 100;
19371da177e4SLinus Torvalds 		while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
19381da177e4SLinus Torvalds 			if (!(tmp & BASR_PHASE_MATCH))
19391da177e4SLinus Torvalds 				break;
19401da177e4SLinus Torvalds 			if (--limit < 0)
19411da177e4SLinus Torvalds 				break;
19421da177e4SLinus Torvalds 		}
19431da177e4SLinus Torvalds 	}
19441da177e4SLinus Torvalds 
19451da177e4SLinus Torvalds 	dprintk(NDEBUG_DMA, ("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG)));
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
19481da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	residue = NCR5380_dma_residual(instance);
19511da177e4SLinus Torvalds 	c -= residue;
19521da177e4SLinus Torvalds 	*count -= c;
19531da177e4SLinus Torvalds 	*data += c;
19541da177e4SLinus Torvalds 	*phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds #ifdef READ_OVERRUNS
19571da177e4SLinus Torvalds 	if (*phase == p && (p & SR_IO) && residue == 0) {
19581da177e4SLinus Torvalds 		if (overrun) {
19591da177e4SLinus Torvalds 			dprintk(NDEBUG_DMA, ("Got an input overrun, using saved byte\n"));
19601da177e4SLinus Torvalds 			**data = saved_data;
19611da177e4SLinus Torvalds 			*data += 1;
19621da177e4SLinus Torvalds 			*count -= 1;
19631da177e4SLinus Torvalds 			cnt = toPIO = 1;
19641da177e4SLinus Torvalds 		} else {
19651da177e4SLinus Torvalds 			printk("No overrun??\n");
19661da177e4SLinus Torvalds 			cnt = toPIO = 2;
19671da177e4SLinus Torvalds 		}
19681da177e4SLinus Torvalds 		dprintk(NDEBUG_DMA, ("Doing %d-byte PIO to 0x%X\n", cnt, *data));
19691da177e4SLinus Torvalds 		NCR5380_transfer_pio(instance, phase, &cnt, data);
19701da177e4SLinus Torvalds 		*count -= toPIO - cnt;
19711da177e4SLinus Torvalds 	}
19721da177e4SLinus Torvalds #endif
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	dprintk(NDEBUG_DMA, ("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n", *data, *count, *(*data + *count - 1), *(*data + *count)));
19751da177e4SLinus Torvalds 	return 0;
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds #elif defined(REAL_DMA)
19781da177e4SLinus Torvalds 	return 0;
19791da177e4SLinus Torvalds #else				/* defined(REAL_DMA_POLL) */
19801da177e4SLinus Torvalds 	if (p & SR_IO) {
19811da177e4SLinus Torvalds #ifdef DMA_WORKS_RIGHT
19821da177e4SLinus Torvalds 		foo = NCR5380_pread(instance, d, c);
19831da177e4SLinus Torvalds #else
19841da177e4SLinus Torvalds 		int diff = 1;
19851da177e4SLinus Torvalds 		if (hostdata->flags & FLAG_NCR53C400) {
19861da177e4SLinus Torvalds 			diff = 0;
19871da177e4SLinus Torvalds 		}
19881da177e4SLinus Torvalds 		if (!(foo = NCR5380_pread(instance, d, c - diff))) {
19891da177e4SLinus Torvalds 			/*
19901da177e4SLinus Torvalds 			 * We can't disable DMA mode after successfully transferring
19911da177e4SLinus Torvalds 			 * what we plan to be the last byte, since that would open up
19921da177e4SLinus Torvalds 			 * a race condition where if the target asserted REQ before
19931da177e4SLinus Torvalds 			 * we got the DMA mode reset, the NCR5380 would have latched
19941da177e4SLinus Torvalds 			 * an additional byte into the INPUT DATA register and we'd
19951da177e4SLinus Torvalds 			 * have dropped it.
19961da177e4SLinus Torvalds 			 *
19971da177e4SLinus Torvalds 			 * The workaround was to transfer one fewer bytes than we
19981da177e4SLinus Torvalds 			 * intended to with the pseudo-DMA read function, wait for
19991da177e4SLinus Torvalds 			 * the chip to latch the last byte, read it, and then disable
20001da177e4SLinus Torvalds 			 * pseudo-DMA mode.
20011da177e4SLinus Torvalds 			 *
20021da177e4SLinus Torvalds 			 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
20031da177e4SLinus Torvalds 			 * REQ is deasserted when ACK is asserted, and not reasserted
20041da177e4SLinus Torvalds 			 * until ACK goes false.  Since the NCR5380 won't lower ACK
20051da177e4SLinus Torvalds 			 * until DACK is asserted, which won't happen unless we twiddle
20061da177e4SLinus Torvalds 			 * the DMA port or we take the NCR5380 out of DMA mode, we
20071da177e4SLinus Torvalds 			 * can guarantee that we won't handshake another extra
20081da177e4SLinus Torvalds 			 * byte.
20091da177e4SLinus Torvalds 			 */
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 			if (!(hostdata->flags & FLAG_NCR53C400)) {
20121da177e4SLinus Torvalds 				while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
20131da177e4SLinus Torvalds 				/* Wait for clean handshake */
20141da177e4SLinus Torvalds 				while (NCR5380_read(STATUS_REG) & SR_REQ);
20151da177e4SLinus Torvalds 				d[c - 1] = NCR5380_read(INPUT_DATA_REG);
20161da177e4SLinus Torvalds 			}
20171da177e4SLinus Torvalds 		}
20181da177e4SLinus Torvalds #endif
20191da177e4SLinus Torvalds 	} else {
20201da177e4SLinus Torvalds #ifdef DMA_WORKS_RIGHT
20211da177e4SLinus Torvalds 		foo = NCR5380_pwrite(instance, d, c);
20221da177e4SLinus Torvalds #else
20231da177e4SLinus Torvalds 		int timeout;
20241da177e4SLinus Torvalds 		dprintk(NDEBUG_C400_PWRITE, ("About to pwrite %d bytes\n", c));
20251da177e4SLinus Torvalds 		if (!(foo = NCR5380_pwrite(instance, d, c))) {
20261da177e4SLinus Torvalds 			/*
20271da177e4SLinus Torvalds 			 * Wait for the last byte to be sent.  If REQ is being asserted for
20281da177e4SLinus Torvalds 			 * the byte we're interested, we'll ACK it and it will go false.
20291da177e4SLinus Torvalds 			 */
20301da177e4SLinus Torvalds 			if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
20311da177e4SLinus Torvalds 				timeout = 20000;
20321da177e4SLinus Torvalds 				while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 				if (!timeout)
20351da177e4SLinus Torvalds 					dprintk(NDEBUG_LAST_BYTE_SENT, ("scsi%d : timed out on last byte\n", instance->host_no));
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 				if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
20381da177e4SLinus Torvalds 					hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
20391da177e4SLinus Torvalds 					if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
20401da177e4SLinus Torvalds 						hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
20411da177e4SLinus Torvalds 						dprintk(NDEBUG_LAST_WRITE_SENT, ("scsi%d : last bit sent works\n", instance->host_no));
20421da177e4SLinus Torvalds 					}
20431da177e4SLinus Torvalds 				}
20441da177e4SLinus Torvalds 			} else {
20451da177e4SLinus Torvalds 				dprintk(NDEBUG_C400_PWRITE, ("Waiting for LASTBYTE\n"));
20461da177e4SLinus Torvalds 				while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
20471da177e4SLinus Torvalds 				dprintk(NDEBUG_C400_PWRITE, ("Got LASTBYTE\n"));
20481da177e4SLinus Torvalds 			}
20491da177e4SLinus Torvalds 		}
20501da177e4SLinus Torvalds #endif
20511da177e4SLinus Torvalds 	}
20521da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
20531da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
20561da177e4SLinus Torvalds 		dprintk(NDEBUG_C400_PWRITE, ("53C400w: Checking for IRQ\n"));
20571da177e4SLinus Torvalds 		if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
20581da177e4SLinus Torvalds 			dprintk(NDEBUG_C400_PWRITE, ("53C400w:    got it, reading reset interrupt reg\n"));
20591da177e4SLinus Torvalds 			NCR5380_read(RESET_PARITY_INTERRUPT_REG);
20601da177e4SLinus Torvalds 		} else {
20611da177e4SLinus Torvalds 			printk("53C400w:    IRQ NOT THERE!\n");
20621da177e4SLinus Torvalds 		}
20631da177e4SLinus Torvalds 	}
20641da177e4SLinus Torvalds 	*data = d + c;
20651da177e4SLinus Torvalds 	*count = 0;
20661da177e4SLinus Torvalds 	*phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
20671da177e4SLinus Torvalds #if defined(PSEUDO_DMA) && defined(UNSAFE)
20681da177e4SLinus Torvalds 	spin_lock_irq(instance->host_lock);
20691da177e4SLinus Torvalds #endif				/* defined(REAL_DMA_POLL) */
20701da177e4SLinus Torvalds 	return foo;
20711da177e4SLinus Torvalds #endif				/* def REAL_DMA */
20721da177e4SLinus Torvalds }
20731da177e4SLinus Torvalds #endif				/* defined(REAL_DMA) | defined(PSEUDO_DMA) */
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds /*
20761da177e4SLinus Torvalds  * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
20771da177e4SLinus Torvalds  *
20781da177e4SLinus Torvalds  * Purpose : run through the various SCSI phases and do as the target
20791da177e4SLinus Torvalds  *      directs us to.  Operates on the currently connected command,
20801da177e4SLinus Torvalds  *      instance->connected.
20811da177e4SLinus Torvalds  *
20821da177e4SLinus Torvalds  * Inputs : instance, instance for which we are doing commands
20831da177e4SLinus Torvalds  *
20841da177e4SLinus Torvalds  * Side effects : SCSI things happen, the disconnected queue will be
20851da177e4SLinus Torvalds  *      modified if a command disconnects, *instance->connected will
20861da177e4SLinus Torvalds  *      change.
20871da177e4SLinus Torvalds  *
20881da177e4SLinus Torvalds  * XXX Note : we need to watch for bus free or a reset condition here
20891da177e4SLinus Torvalds  *      to recover from an unexpected bus free condition.
20901da177e4SLinus Torvalds  *
20911da177e4SLinus Torvalds  * Locks: io_request_lock held by caller in IRQ mode
20921da177e4SLinus Torvalds  */
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds static void NCR5380_information_transfer(struct Scsi_Host *instance) {
20951da177e4SLinus Torvalds 	NCR5380_local_declare();
20961da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
20971da177e4SLinus Torvalds 	unsigned char msgout = NOP;
20981da177e4SLinus Torvalds 	int sink = 0;
20991da177e4SLinus Torvalds 	int len;
21001da177e4SLinus Torvalds #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
21011da177e4SLinus Torvalds 	int transfersize;
21021da177e4SLinus Torvalds #endif
21031da177e4SLinus Torvalds 	unsigned char *data;
21041da177e4SLinus Torvalds 	unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
21051da177e4SLinus Torvalds 	Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
21061da177e4SLinus Torvalds 	/* RvC: we need to set the end of the polling time */
21071da177e4SLinus Torvalds 	unsigned long poll_time = jiffies + USLEEP_POLL;
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	NCR5380_setup(instance);
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds 	while (1) {
21121da177e4SLinus Torvalds 		tmp = NCR5380_read(STATUS_REG);
21131da177e4SLinus Torvalds 		/* We only have a valid SCSI phase when REQ is asserted */
21141da177e4SLinus Torvalds 		if (tmp & SR_REQ) {
21151da177e4SLinus Torvalds 			phase = (tmp & PHASE_MASK);
21161da177e4SLinus Torvalds 			if (phase != old_phase) {
21171da177e4SLinus Torvalds 				old_phase = phase;
21181da177e4SLinus Torvalds 				NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
21191da177e4SLinus Torvalds 			}
21201da177e4SLinus Torvalds 			if (sink && (phase != PHASE_MSGOUT)) {
21211da177e4SLinus Torvalds 				NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
21241da177e4SLinus Torvalds 				while (NCR5380_read(STATUS_REG) & SR_REQ);
21251da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
21261da177e4SLinus Torvalds 				sink = 0;
21271da177e4SLinus Torvalds 				continue;
21281da177e4SLinus Torvalds 			}
21291da177e4SLinus Torvalds 			switch (phase) {
21301da177e4SLinus Torvalds 			case PHASE_DATAIN:
21311da177e4SLinus Torvalds 			case PHASE_DATAOUT:
21321da177e4SLinus Torvalds #if (NDEBUG & NDEBUG_NO_DATAOUT)
21331da177e4SLinus Torvalds 				printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
21341da177e4SLinus Torvalds 				sink = 1;
21351da177e4SLinus Torvalds 				do_abort(instance);
21361da177e4SLinus Torvalds 				cmd->result = DID_ERROR << 16;
2137cce99c69SMatthew Wilcox 				cmd->scsi_done(cmd);
21381da177e4SLinus Torvalds 				return;
21391da177e4SLinus Torvalds #endif
21401da177e4SLinus Torvalds 				/*
21411da177e4SLinus Torvalds 				 * If there is no room left in the current buffer in the
21421da177e4SLinus Torvalds 				 * scatter-gather list, move onto the next one.
21431da177e4SLinus Torvalds 				 */
21441da177e4SLinus Torvalds 
21451da177e4SLinus Torvalds 				if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
21461da177e4SLinus Torvalds 					++cmd->SCp.buffer;
21471da177e4SLinus Torvalds 					--cmd->SCp.buffers_residual;
21481da177e4SLinus Torvalds 					cmd->SCp.this_residual = cmd->SCp.buffer->length;
214945711f1aSJens Axboe 					cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
21501da177e4SLinus Torvalds 					dprintk(NDEBUG_INFORMATION, ("scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual));
21511da177e4SLinus Torvalds 				}
21521da177e4SLinus Torvalds 				/*
21531da177e4SLinus Torvalds 				 * The preferred transfer method is going to be
21541da177e4SLinus Torvalds 				 * PSEUDO-DMA for systems that are strictly PIO,
21551da177e4SLinus Torvalds 				 * since we can let the hardware do the handshaking.
21561da177e4SLinus Torvalds 				 *
21571da177e4SLinus Torvalds 				 * For this to work, we need to know the transfersize
21581da177e4SLinus Torvalds 				 * ahead of time, since the pseudo-DMA code will sit
21591da177e4SLinus Torvalds 				 * in an unconditional loop.
21601da177e4SLinus Torvalds 				 */
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
21631da177e4SLinus Torvalds 				/* KLL
21641da177e4SLinus Torvalds 				 * PSEUDO_DMA is defined here. If this is the g_NCR5380
21651da177e4SLinus Torvalds 				 * driver then it will always be defined, so the
21661da177e4SLinus Torvalds 				 * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
21671da177e4SLinus Torvalds 				 * NCR5380 case.  I think this is a fairly clean solution.
21681da177e4SLinus Torvalds 				 * We supplement these 2 if's with the flag.
21691da177e4SLinus Torvalds 				 */
21701da177e4SLinus Torvalds #ifdef NCR5380_dma_xfer_len
21711da177e4SLinus Torvalds 				if (!cmd->device->borken && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
21721da177e4SLinus Torvalds #else
21731da177e4SLinus Torvalds 				transfersize = cmd->transfersize;
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds #ifdef LIMIT_TRANSFERSIZE	/* If we have problems with interrupt service */
21761da177e4SLinus Torvalds 				if (transfersize > 512)
21771da177e4SLinus Torvalds 					transfersize = 512;
21781da177e4SLinus Torvalds #endif				/* LIMIT_TRANSFERSIZE */
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 				if (!cmd->device->borken && transfersize && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && cmd->SCp.this_residual && !(cmd->SCp.this_residual % transfersize)) {
21811da177e4SLinus Torvalds 					/* Limit transfers to 32K, for xx400 & xx406
21821da177e4SLinus Torvalds 					 * pseudoDMA that transfers in 128 bytes blocks. */
21831da177e4SLinus Torvalds 					if (transfersize > 32 * 1024)
21841da177e4SLinus Torvalds 						transfersize = 32 * 1024;
21851da177e4SLinus Torvalds #endif
21861da177e4SLinus Torvalds 					len = transfersize;
21871da177e4SLinus Torvalds 					if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
21881da177e4SLinus Torvalds 						/*
21891da177e4SLinus Torvalds 						 * If the watchdog timer fires, all future accesses to this
21901da177e4SLinus Torvalds 						 * device will use the polled-IO.
21911da177e4SLinus Torvalds 						 */
2192017560fcSJeff Garzik 						scmd_printk(KERN_INFO, cmd,
2193017560fcSJeff Garzik 							    "switching to slow handshake\n");
21941da177e4SLinus Torvalds 						cmd->device->borken = 1;
21951da177e4SLinus Torvalds 						NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
21961da177e4SLinus Torvalds 						sink = 1;
21971da177e4SLinus Torvalds 						do_abort(instance);
21981da177e4SLinus Torvalds 						cmd->result = DID_ERROR << 16;
2199cce99c69SMatthew Wilcox 						cmd->scsi_done(cmd);
22001da177e4SLinus Torvalds 						/* XXX - need to source or sink data here, as appropriate */
22011da177e4SLinus Torvalds 					} else
22021da177e4SLinus Torvalds 						cmd->SCp.this_residual -= transfersize - len;
22031da177e4SLinus Torvalds 				} else
22041da177e4SLinus Torvalds #endif				/* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
22051da177e4SLinus Torvalds 					NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
22061da177e4SLinus Torvalds 							     &cmd->SCp.ptr);
22071da177e4SLinus Torvalds 				break;
22081da177e4SLinus Torvalds 			case PHASE_MSGIN:
22091da177e4SLinus Torvalds 				len = 1;
22101da177e4SLinus Torvalds 				data = &tmp;
22111da177e4SLinus Torvalds 				NCR5380_transfer_pio(instance, &phase, &len, &data);
22121da177e4SLinus Torvalds 				cmd->SCp.Message = tmp;
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds 				switch (tmp) {
22151da177e4SLinus Torvalds 					/*
22161da177e4SLinus Torvalds 					 * Linking lets us reduce the time required to get the
22171da177e4SLinus Torvalds 					 * next command out to the device, hopefully this will
22181da177e4SLinus Torvalds 					 * mean we don't waste another revolution due to the delays
22191da177e4SLinus Torvalds 					 * required by ARBITRATION and another SELECTION.
22201da177e4SLinus Torvalds 					 *
22211da177e4SLinus Torvalds 					 * In the current implementation proposal, low level drivers
22221da177e4SLinus Torvalds 					 * merely have to start the next command, pointed to by
22231da177e4SLinus Torvalds 					 * next_link, done() is called as with unlinked commands.
22241da177e4SLinus Torvalds 					 */
22251da177e4SLinus Torvalds #ifdef LINKED
22261da177e4SLinus Torvalds 				case LINKED_CMD_COMPLETE:
22271da177e4SLinus Torvalds 				case LINKED_FLG_CMD_COMPLETE:
22281da177e4SLinus Torvalds 					/* Accept message by clearing ACK */
22291da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
22301da177e4SLinus Torvalds 					dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked command complete.\n", instance->host_no, cmd->device->id, cmd->device->lun));
22311da177e4SLinus Torvalds 					/*
22321da177e4SLinus Torvalds 					 * Sanity check : A linked command should only terminate with
22331da177e4SLinus Torvalds 					 * one of these messages if there are more linked commands
22341da177e4SLinus Torvalds 					 * available.
22351da177e4SLinus Torvalds 					 */
22361da177e4SLinus Torvalds 					if (!cmd->next_link) {
22371da177e4SLinus Torvalds 						printk("scsi%d : target %d lun %d linked command complete, no next_link\n" instance->host_no, cmd->device->id, cmd->device->lun);
22381da177e4SLinus Torvalds 						sink = 1;
22391da177e4SLinus Torvalds 						do_abort(instance);
22401da177e4SLinus Torvalds 						return;
22411da177e4SLinus Torvalds 					}
22421da177e4SLinus Torvalds 					initialize_SCp(cmd->next_link);
22431da177e4SLinus Torvalds 					/* The next command is still part of this process */
22441da177e4SLinus Torvalds 					cmd->next_link->tag = cmd->tag;
22451da177e4SLinus Torvalds 					cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
22461da177e4SLinus Torvalds 					dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked request done, calling scsi_done().\n", instance->host_no, cmd->device->id, cmd->device->lun));
22471da177e4SLinus Torvalds 					collect_stats(hostdata, cmd);
22481da177e4SLinus Torvalds 					cmd->scsi_done(cmd);
22491da177e4SLinus Torvalds 					cmd = hostdata->connected;
22501da177e4SLinus Torvalds 					break;
22511da177e4SLinus Torvalds #endif				/* def LINKED */
22521da177e4SLinus Torvalds 				case ABORT:
22531da177e4SLinus Torvalds 				case COMMAND_COMPLETE:
22541da177e4SLinus Torvalds 					/* Accept message by clearing ACK */
22551da177e4SLinus Torvalds 					sink = 1;
22561da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
22571da177e4SLinus Torvalds 					hostdata->connected = NULL;
22581da177e4SLinus Torvalds 					dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d, lun %d completed\n", instance->host_no, cmd->device->id, cmd->device->lun));
22591da177e4SLinus Torvalds 					hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
22601da177e4SLinus Torvalds 
22611da177e4SLinus Torvalds 					/*
22621da177e4SLinus Torvalds 					 * I'm not sure what the correct thing to do here is :
22631da177e4SLinus Torvalds 					 *
22641da177e4SLinus Torvalds 					 * If the command that just executed is NOT a request
22651da177e4SLinus Torvalds 					 * sense, the obvious thing to do is to set the result
22661da177e4SLinus Torvalds 					 * code to the values of the stored parameters.
22671da177e4SLinus Torvalds 					 *
22681da177e4SLinus Torvalds 					 * If it was a REQUEST SENSE command, we need some way
22691da177e4SLinus Torvalds 					 * to differentiate between the failure code of the original
22701da177e4SLinus Torvalds 					 * and the failure code of the REQUEST sense - the obvious
22711da177e4SLinus Torvalds 					 * case is success, where we fall through and leave the result
22721da177e4SLinus Torvalds 					 * code unchanged.
22731da177e4SLinus Torvalds 					 *
22741da177e4SLinus Torvalds 					 * The non-obvious place is where the REQUEST SENSE failed
22751da177e4SLinus Torvalds 					 */
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 					if (cmd->cmnd[0] != REQUEST_SENSE)
22781da177e4SLinus Torvalds 						cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
22791da177e4SLinus Torvalds 					else if (status_byte(cmd->SCp.Status) != GOOD)
22801da177e4SLinus Torvalds 						cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds #ifdef AUTOSENSE
228328424d3aSBoaz Harrosh 					if ((cmd->cmnd[0] == REQUEST_SENSE) &&
228428424d3aSBoaz Harrosh 						hostdata->ses.cmd_len) {
228528424d3aSBoaz Harrosh 						scsi_eh_restore_cmnd(cmd, &hostdata->ses);
228628424d3aSBoaz Harrosh 						hostdata->ses.cmd_len = 0 ;
228728424d3aSBoaz Harrosh 					}
22881da177e4SLinus Torvalds 
228928424d3aSBoaz Harrosh 					if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
229028424d3aSBoaz Harrosh 						scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
229128424d3aSBoaz Harrosh 
229228424d3aSBoaz Harrosh 						dprintk(NDEBUG_AUTOSENSE, ("scsi%d : performing request sense\n", instance->host_no));
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds 						LIST(cmd, hostdata->issue_queue);
22951da177e4SLinus Torvalds 						cmd->host_scribble = (unsigned char *)
22961da177e4SLinus Torvalds 						    hostdata->issue_queue;
22971da177e4SLinus Torvalds 						hostdata->issue_queue = (Scsi_Cmnd *) cmd;
22981da177e4SLinus Torvalds 						dprintk(NDEBUG_QUEUES, ("scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no));
22991da177e4SLinus Torvalds 					} else
23001da177e4SLinus Torvalds #endif				/* def AUTOSENSE */
23011da177e4SLinus Torvalds 					{
23021da177e4SLinus Torvalds 						collect_stats(hostdata, cmd);
23031da177e4SLinus Torvalds 						cmd->scsi_done(cmd);
23041da177e4SLinus Torvalds 					}
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds 					NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
23071da177e4SLinus Torvalds 					/*
23081da177e4SLinus Torvalds 					 * Restore phase bits to 0 so an interrupted selection,
23091da177e4SLinus Torvalds 					 * arbitration can resume.
23101da177e4SLinus Torvalds 					 */
23111da177e4SLinus Torvalds 					NCR5380_write(TARGET_COMMAND_REG, 0);
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds 					while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
23141da177e4SLinus Torvalds 						barrier();
23151da177e4SLinus Torvalds 					return;
23161da177e4SLinus Torvalds 				case MESSAGE_REJECT:
23171da177e4SLinus Torvalds 					/* Accept message by clearing ACK */
23181da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
23191da177e4SLinus Torvalds 					switch (hostdata->last_message) {
23201da177e4SLinus Torvalds 					case HEAD_OF_QUEUE_TAG:
23211da177e4SLinus Torvalds 					case ORDERED_QUEUE_TAG:
23221da177e4SLinus Torvalds 					case SIMPLE_QUEUE_TAG:
23231da177e4SLinus Torvalds 						cmd->device->simple_tags = 0;
23241da177e4SLinus Torvalds 						hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
23251da177e4SLinus Torvalds 						break;
23261da177e4SLinus Torvalds 					default:
23271da177e4SLinus Torvalds 						break;
23281da177e4SLinus Torvalds 					}
23291da177e4SLinus Torvalds 				case DISCONNECT:{
23301da177e4SLinus Torvalds 						/* Accept message by clearing ACK */
23311da177e4SLinus Torvalds 						NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
23321da177e4SLinus Torvalds 						cmd->device->disconnect = 1;
23331da177e4SLinus Torvalds 						LIST(cmd, hostdata->disconnected_queue);
23341da177e4SLinus Torvalds 						cmd->host_scribble = (unsigned char *)
23351da177e4SLinus Torvalds 						    hostdata->disconnected_queue;
23361da177e4SLinus Torvalds 						hostdata->connected = NULL;
23371da177e4SLinus Torvalds 						hostdata->disconnected_queue = cmd;
23381da177e4SLinus Torvalds 						dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d lun %d was moved from connected to" "  the disconnected_queue\n", instance->host_no, cmd->device->id, cmd->device->lun));
23391da177e4SLinus Torvalds 						/*
23401da177e4SLinus Torvalds 						 * Restore phase bits to 0 so an interrupted selection,
23411da177e4SLinus Torvalds 						 * arbitration can resume.
23421da177e4SLinus Torvalds 						 */
23431da177e4SLinus Torvalds 						NCR5380_write(TARGET_COMMAND_REG, 0);
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 						/* Enable reselect interrupts */
23461da177e4SLinus Torvalds 						NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
23471da177e4SLinus Torvalds 						/* Wait for bus free to avoid nasty timeouts - FIXME timeout !*/
23481da177e4SLinus Torvalds 						/* NCR538_poll_politely(instance, STATUS_REG, SR_BSY, 0, 30 * HZ); */
23491da177e4SLinus Torvalds 						while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
23501da177e4SLinus Torvalds 							barrier();
23511da177e4SLinus Torvalds 						return;
23521da177e4SLinus Torvalds 					}
23531da177e4SLinus Torvalds 					/*
23541da177e4SLinus Torvalds 					 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
23551da177e4SLinus Torvalds 					 * operation, in violation of the SCSI spec so we can safely
23561da177e4SLinus Torvalds 					 * ignore SAVE/RESTORE pointers calls.
23571da177e4SLinus Torvalds 					 *
23581da177e4SLinus Torvalds 					 * Unfortunately, some disks violate the SCSI spec and
23591da177e4SLinus Torvalds 					 * don't issue the required SAVE_POINTERS message before
23601da177e4SLinus Torvalds 					 * disconnecting, and we have to break spec to remain
23611da177e4SLinus Torvalds 					 * compatible.
23621da177e4SLinus Torvalds 					 */
23631da177e4SLinus Torvalds 				case SAVE_POINTERS:
23641da177e4SLinus Torvalds 				case RESTORE_POINTERS:
23651da177e4SLinus Torvalds 					/* Accept message by clearing ACK */
23661da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
23671da177e4SLinus Torvalds 					break;
23681da177e4SLinus Torvalds 				case EXTENDED_MESSAGE:
23691da177e4SLinus Torvalds /*
23701da177e4SLinus Torvalds  * Extended messages are sent in the following format :
23711da177e4SLinus Torvalds  * Byte
23721da177e4SLinus Torvalds  * 0            EXTENDED_MESSAGE == 1
23731da177e4SLinus Torvalds  * 1            length (includes one byte for code, doesn't
23741da177e4SLinus Torvalds  *              include first two bytes)
23751da177e4SLinus Torvalds  * 2            code
23761da177e4SLinus Torvalds  * 3..length+1  arguments
23771da177e4SLinus Torvalds  *
23781da177e4SLinus Torvalds  * Start the extended message buffer with the EXTENDED_MESSAGE
23791abfd370SMatthew Wilcox  * byte, since spi_print_msg() wants the whole thing.
23801da177e4SLinus Torvalds  */
23811da177e4SLinus Torvalds 					extended_msg[0] = EXTENDED_MESSAGE;
23821da177e4SLinus Torvalds 					/* Accept first byte by clearing ACK */
23831da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
23841da177e4SLinus Torvalds 					dprintk(NDEBUG_EXTENDED, ("scsi%d : receiving extended message\n", instance->host_no));
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds 					len = 2;
23871da177e4SLinus Torvalds 					data = extended_msg + 1;
23881da177e4SLinus Torvalds 					phase = PHASE_MSGIN;
23891da177e4SLinus Torvalds 					NCR5380_transfer_pio(instance, &phase, &len, &data);
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 					dprintk(NDEBUG_EXTENDED, ("scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]));
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds 					if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
23941da177e4SLinus Torvalds 						/* Accept third byte by clearing ACK */
23951da177e4SLinus Torvalds 						NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
23961da177e4SLinus Torvalds 						len = extended_msg[1] - 1;
23971da177e4SLinus Torvalds 						data = extended_msg + 3;
23981da177e4SLinus Torvalds 						phase = PHASE_MSGIN;
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds 						NCR5380_transfer_pio(instance, &phase, &len, &data);
24011da177e4SLinus Torvalds 						dprintk(NDEBUG_EXTENDED, ("scsi%d : message received, residual %d\n", instance->host_no, len));
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds 						switch (extended_msg[2]) {
24041da177e4SLinus Torvalds 						case EXTENDED_SDTR:
24051da177e4SLinus Torvalds 						case EXTENDED_WDTR:
24061da177e4SLinus Torvalds 						case EXTENDED_MODIFY_DATA_POINTER:
24071da177e4SLinus Torvalds 						case EXTENDED_EXTENDED_IDENTIFY:
24081da177e4SLinus Torvalds 							tmp = 0;
24091da177e4SLinus Torvalds 						}
24101da177e4SLinus Torvalds 					} else if (len) {
24111da177e4SLinus Torvalds 						printk("scsi%d: error receiving extended message\n", instance->host_no);
24121da177e4SLinus Torvalds 						tmp = 0;
24131da177e4SLinus Torvalds 					} else {
24141da177e4SLinus Torvalds 						printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
24151da177e4SLinus Torvalds 						tmp = 0;
24161da177e4SLinus Torvalds 					}
24171da177e4SLinus Torvalds 					/* Fall through to reject message */
24181da177e4SLinus Torvalds 
24191da177e4SLinus Torvalds 					/*
24201da177e4SLinus Torvalds 					 * If we get something weird that we aren't expecting,
24211da177e4SLinus Torvalds 					 * reject it.
24221da177e4SLinus Torvalds 					 */
24231da177e4SLinus Torvalds 				default:
24241da177e4SLinus Torvalds 					if (!tmp) {
24251da177e4SLinus Torvalds 						printk("scsi%d: rejecting message ", instance->host_no);
24261abfd370SMatthew Wilcox 						spi_print_msg(extended_msg);
24271da177e4SLinus Torvalds 						printk("\n");
24281da177e4SLinus Torvalds 					} else if (tmp != EXTENDED_MESSAGE)
2429017560fcSJeff Garzik 						scmd_printk(KERN_INFO, cmd,
2430017560fcSJeff Garzik 							"rejecting unknown message %02x\n",tmp);
24311da177e4SLinus Torvalds 					else
2432017560fcSJeff Garzik 						scmd_printk(KERN_INFO, cmd,
2433017560fcSJeff Garzik 							"rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
24341da177e4SLinus Torvalds 
24351da177e4SLinus Torvalds 					msgout = MESSAGE_REJECT;
24361da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
24371da177e4SLinus Torvalds 					break;
24381da177e4SLinus Torvalds 				}	/* switch (tmp) */
24391da177e4SLinus Torvalds 				break;
24401da177e4SLinus Torvalds 			case PHASE_MSGOUT:
24411da177e4SLinus Torvalds 				len = 1;
24421da177e4SLinus Torvalds 				data = &msgout;
24431da177e4SLinus Torvalds 				hostdata->last_message = msgout;
24441da177e4SLinus Torvalds 				NCR5380_transfer_pio(instance, &phase, &len, &data);
24451da177e4SLinus Torvalds 				if (msgout == ABORT) {
24461da177e4SLinus Torvalds 					hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
24471da177e4SLinus Torvalds 					hostdata->connected = NULL;
24481da177e4SLinus Torvalds 					cmd->result = DID_ERROR << 16;
24491da177e4SLinus Torvalds 					collect_stats(hostdata, cmd);
24501da177e4SLinus Torvalds 					cmd->scsi_done(cmd);
24511da177e4SLinus Torvalds 					NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
24521da177e4SLinus Torvalds 					return;
24531da177e4SLinus Torvalds 				}
24541da177e4SLinus Torvalds 				msgout = NOP;
24551da177e4SLinus Torvalds 				break;
24561da177e4SLinus Torvalds 			case PHASE_CMDOUT:
24571da177e4SLinus Torvalds 				len = cmd->cmd_len;
24581da177e4SLinus Torvalds 				data = cmd->cmnd;
24591da177e4SLinus Torvalds 				/*
24601da177e4SLinus Torvalds 				 * XXX for performance reasons, on machines with a
24611da177e4SLinus Torvalds 				 * PSEUDO-DMA architecture we should probably
24621da177e4SLinus Torvalds 				 * use the dma transfer function.
24631da177e4SLinus Torvalds 				 */
24641da177e4SLinus Torvalds 				NCR5380_transfer_pio(instance, &phase, &len, &data);
24651da177e4SLinus Torvalds 				if (!cmd->device->disconnect && should_disconnect(cmd->cmnd[0])) {
24661da177e4SLinus Torvalds 					NCR5380_set_timer(hostdata, USLEEP_SLEEP);
24671da177e4SLinus Torvalds 					dprintk(NDEBUG_USLEEP, ("scsi%d : issued command, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
24681da177e4SLinus Torvalds 					return;
24691da177e4SLinus Torvalds 				}
24701da177e4SLinus Torvalds 				break;
24711da177e4SLinus Torvalds 			case PHASE_STATIN:
24721da177e4SLinus Torvalds 				len = 1;
24731da177e4SLinus Torvalds 				data = &tmp;
24741da177e4SLinus Torvalds 				NCR5380_transfer_pio(instance, &phase, &len, &data);
24751da177e4SLinus Torvalds 				cmd->SCp.Status = tmp;
24761da177e4SLinus Torvalds 				break;
24771da177e4SLinus Torvalds 			default:
24781da177e4SLinus Torvalds 				printk("scsi%d : unknown phase\n", instance->host_no);
24791da177e4SLinus Torvalds 				NCR5380_dprint(NDEBUG_ALL, instance);
24801da177e4SLinus Torvalds 			}	/* switch(phase) */
24811da177e4SLinus Torvalds 		}		/* if (tmp * SR_REQ) */
24821da177e4SLinus Torvalds 		else {
24831da177e4SLinus Torvalds 			/* RvC: go to sleep if polling time expired
24841da177e4SLinus Torvalds 			 */
24851da177e4SLinus Torvalds 			if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time)) {
24861da177e4SLinus Torvalds 				NCR5380_set_timer(hostdata, USLEEP_SLEEP);
24871da177e4SLinus Torvalds 				dprintk(NDEBUG_USLEEP, ("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
24881da177e4SLinus Torvalds 				return;
24891da177e4SLinus Torvalds 			}
24901da177e4SLinus Torvalds 		}
24911da177e4SLinus Torvalds 	}			/* while (1) */
24921da177e4SLinus Torvalds }
24931da177e4SLinus Torvalds 
24941da177e4SLinus Torvalds /*
24951da177e4SLinus Torvalds  * Function : void NCR5380_reselect (struct Scsi_Host *instance)
24961da177e4SLinus Torvalds  *
24971da177e4SLinus Torvalds  * Purpose : does reselection, initializing the instance->connected
24981da177e4SLinus Torvalds  *      field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
24991da177e4SLinus Torvalds  *      nexus has been reestablished,
25001da177e4SLinus Torvalds  *
25011da177e4SLinus Torvalds  * Inputs : instance - this instance of the NCR5380.
25021da177e4SLinus Torvalds  *
25031da177e4SLinus Torvalds  * Locks: io_request_lock held by caller if IRQ driven
25041da177e4SLinus Torvalds  */
25051da177e4SLinus Torvalds 
25061da177e4SLinus Torvalds static void NCR5380_reselect(struct Scsi_Host *instance) {
25071da177e4SLinus Torvalds 	NCR5380_local_declare();
25081da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
25091da177e4SLinus Torvalds 	 instance->hostdata;
25101da177e4SLinus Torvalds 	unsigned char target_mask;
25111da177e4SLinus Torvalds 	unsigned char lun, phase;
25121da177e4SLinus Torvalds 	int len;
25131da177e4SLinus Torvalds 	unsigned char msg[3];
25141da177e4SLinus Torvalds 	unsigned char *data;
25151da177e4SLinus Torvalds 	Scsi_Cmnd *tmp = NULL, *prev;
25161da177e4SLinus Torvalds 	int abort = 0;
25171da177e4SLinus Torvalds 	NCR5380_setup(instance);
25181da177e4SLinus Torvalds 
25191da177e4SLinus Torvalds 	/*
25201da177e4SLinus Torvalds 	 * Disable arbitration, etc. since the host adapter obviously
25211da177e4SLinus Torvalds 	 * lost, and tell an interrupted NCR5380_select() to restart.
25221da177e4SLinus Torvalds 	 */
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
25251da177e4SLinus Torvalds 	hostdata->restart_select = 1;
25261da177e4SLinus Torvalds 
25271da177e4SLinus Torvalds 	target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
25281da177e4SLinus Torvalds 	dprintk(NDEBUG_SELECTION, ("scsi%d : reselect\n", instance->host_no));
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 	/*
25311da177e4SLinus Torvalds 	 * At this point, we have detected that our SCSI ID is on the bus,
25321da177e4SLinus Torvalds 	 * SEL is true and BSY was false for at least one bus settle delay
25331da177e4SLinus Torvalds 	 * (400 ns).
25341da177e4SLinus Torvalds 	 *
25351da177e4SLinus Torvalds 	 * We must assert BSY ourselves, until the target drops the SEL
25361da177e4SLinus Torvalds 	 * signal.
25371da177e4SLinus Torvalds 	 */
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
25401da177e4SLinus Torvalds 
25411da177e4SLinus Torvalds 	/* FIXME: timeout too long, must fail to workqueue */
25421da177e4SLinus Torvalds 	if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
25431da177e4SLinus Torvalds 		abort = 1;
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 	/*
25481da177e4SLinus Torvalds 	 * Wait for target to go into MSGIN.
25491da177e4SLinus Torvalds 	 * FIXME: timeout needed and fail to work queeu
25501da177e4SLinus Torvalds 	 */
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	if(NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 2*HZ))
25531da177e4SLinus Torvalds 		abort = 1;
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds 	len = 1;
25561da177e4SLinus Torvalds 	data = msg;
25571da177e4SLinus Torvalds 	phase = PHASE_MSGIN;
25581da177e4SLinus Torvalds 	NCR5380_transfer_pio(instance, &phase, &len, &data);
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 	if (!(msg[0] & 0x80)) {
25611da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
25621abfd370SMatthew Wilcox 		spi_print_msg(msg);
25631da177e4SLinus Torvalds 		abort = 1;
25641da177e4SLinus Torvalds 	} else {
25651da177e4SLinus Torvalds 		/* Accept message by clearing ACK */
25661da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
25671da177e4SLinus Torvalds 		lun = (msg[0] & 0x07);
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 		/*
25701da177e4SLinus Torvalds 		 * We need to add code for SCSI-II to track which devices have
25711da177e4SLinus Torvalds 		 * I_T_L_Q nexuses established, and which have simple I_T_L
25721da177e4SLinus Torvalds 		 * nexuses so we can chose to do additional data transfer.
25731da177e4SLinus Torvalds 		 */
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 		/*
25761da177e4SLinus Torvalds 		 * Find the command corresponding to the I_T_L or I_T_L_Q  nexus we
25771da177e4SLinus Torvalds 		 * just reestablished, and remove it from the disconnected queue.
25781da177e4SLinus Torvalds 		 */
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds 
25811da177e4SLinus Torvalds 		for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
25821da177e4SLinus Torvalds 			if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
25831da177e4SLinus Torvalds 			    ) {
25841da177e4SLinus Torvalds 				if (prev) {
25851da177e4SLinus Torvalds 					REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
25861da177e4SLinus Torvalds 					prev->host_scribble = tmp->host_scribble;
25871da177e4SLinus Torvalds 				} else {
25881da177e4SLinus Torvalds 					REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
25891da177e4SLinus Torvalds 					hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
25901da177e4SLinus Torvalds 				}
25911da177e4SLinus Torvalds 				tmp->host_scribble = NULL;
25921da177e4SLinus Torvalds 				break;
25931da177e4SLinus Torvalds 			}
25941da177e4SLinus Torvalds 		if (!tmp) {
25951da177e4SLinus Torvalds 			printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
25961da177e4SLinus Torvalds 			/*
25971da177e4SLinus Torvalds 			 * Since we have an established nexus that we can't do anything with,
25981da177e4SLinus Torvalds 			 * we must abort it.
25991da177e4SLinus Torvalds 			 */
26001da177e4SLinus Torvalds 			abort = 1;
26011da177e4SLinus Torvalds 		}
26021da177e4SLinus Torvalds 	}
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	if (abort) {
26051da177e4SLinus Torvalds 		do_abort(instance);
26061da177e4SLinus Torvalds 	} else {
26071da177e4SLinus Torvalds 		hostdata->connected = tmp;
26081da177e4SLinus Torvalds 		dprintk(NDEBUG_RESELECTION, ("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n", instance->host_no, tmp->target, tmp->lun, tmp->tag));
26091da177e4SLinus Torvalds 	}
26101da177e4SLinus Torvalds }
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds /*
26131da177e4SLinus Torvalds  * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
26141da177e4SLinus Torvalds  *
26151da177e4SLinus Torvalds  * Purpose : called by interrupt handler when DMA finishes or a phase
26161da177e4SLinus Torvalds  *      mismatch occurs (which would finish the DMA transfer).
26171da177e4SLinus Torvalds  *
26181da177e4SLinus Torvalds  * Inputs : instance - this instance of the NCR5380.
26191da177e4SLinus Torvalds  *
26201da177e4SLinus Torvalds  * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
26211da177e4SLinus Torvalds  *      nexus has been reestablished, on failure NULL is returned.
26221da177e4SLinus Torvalds  */
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds #ifdef REAL_DMA
26251da177e4SLinus Torvalds static void NCR5380_dma_complete(NCR5380_instance * instance) {
26261da177e4SLinus Torvalds 	NCR5380_local_declare();
2627f8343685SYoann Padioleau 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
26281da177e4SLinus Torvalds 	int transferred;
26291da177e4SLinus Torvalds 	NCR5380_setup(instance);
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds 	/*
26321da177e4SLinus Torvalds 	 * XXX this might not be right.
26331da177e4SLinus Torvalds 	 *
26341da177e4SLinus Torvalds 	 * Wait for final byte to transfer, ie wait for ACK to go false.
26351da177e4SLinus Torvalds 	 *
26361da177e4SLinus Torvalds 	 * We should use the Last Byte Sent bit, unfortunately this is
26371da177e4SLinus Torvalds 	 * not available on the 5380/5381 (only the various CMOS chips)
26381da177e4SLinus Torvalds 	 *
26391da177e4SLinus Torvalds 	 * FIXME: timeout, and need to handle long timeout/irq case
26401da177e4SLinus Torvalds 	 */
26411da177e4SLinus Torvalds 
26421da177e4SLinus Torvalds 	NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
26431da177e4SLinus Torvalds 
26441da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
26451da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
26461da177e4SLinus Torvalds 
26471da177e4SLinus Torvalds 	/*
26481da177e4SLinus Torvalds 	 * The only places we should see a phase mismatch and have to send
26491da177e4SLinus Torvalds 	 * data from the same set of pointers will be the data transfer
26501da177e4SLinus Torvalds 	 * phases.  So, residual, requested length are only important here.
26511da177e4SLinus Torvalds 	 */
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds 	if (!(hostdata->connected->SCp.phase & SR_CD)) {
26541da177e4SLinus Torvalds 		transferred = instance->dmalen - NCR5380_dma_residual();
26551da177e4SLinus Torvalds 		hostdata->connected->SCp.this_residual -= transferred;
26561da177e4SLinus Torvalds 		hostdata->connected->SCp.ptr += transferred;
26571da177e4SLinus Torvalds 	}
26581da177e4SLinus Torvalds }
26591da177e4SLinus Torvalds #endif				/* def REAL_DMA */
26601da177e4SLinus Torvalds 
26611da177e4SLinus Torvalds /*
26621da177e4SLinus Torvalds  * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
26631da177e4SLinus Torvalds  *
26641da177e4SLinus Torvalds  * Purpose : abort a command
26651da177e4SLinus Torvalds  *
26661da177e4SLinus Torvalds  * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
26671da177e4SLinus Torvalds  *      host byte of the result field to, if zero DID_ABORTED is
26681da177e4SLinus Torvalds  *      used.
26691da177e4SLinus Torvalds  *
26701da177e4SLinus Torvalds  * Returns : 0 - success, -1 on failure.
26711da177e4SLinus Torvalds  *
26721da177e4SLinus Torvalds  *	XXX - there is no way to abort the command that is currently
26731da177e4SLinus Torvalds  *	connected, you have to wait for it to complete.  If this is
26741da177e4SLinus Torvalds  *	a problem, we could implement longjmp() / setjmp(), setjmp()
26751da177e4SLinus Torvalds  *	called where the loop started in NCR5380_main().
26761da177e4SLinus Torvalds  *
26771da177e4SLinus Torvalds  * Locks: host lock taken by caller
26781da177e4SLinus Torvalds  */
26791da177e4SLinus Torvalds 
26801da177e4SLinus Torvalds static int NCR5380_abort(Scsi_Cmnd * cmd) {
26811da177e4SLinus Torvalds 	NCR5380_local_declare();
26821da177e4SLinus Torvalds 	struct Scsi_Host *instance = cmd->device->host;
26831da177e4SLinus Torvalds 	struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
26841da177e4SLinus Torvalds 	Scsi_Cmnd *tmp, **prev;
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds 	printk(KERN_WARNING "scsi%d : aborting command\n", instance->host_no);
2687db9dff36S 	scsi_print_command(cmd);
26881da177e4SLinus Torvalds 
26891da177e4SLinus Torvalds 	NCR5380_print_status(instance);
26901da177e4SLinus Torvalds 
26911da177e4SLinus Torvalds 	NCR5380_setup(instance);
26921da177e4SLinus Torvalds 
26931da177e4SLinus Torvalds 	dprintk(NDEBUG_ABORT, ("scsi%d : abort called\n", instance->host_no));
26941da177e4SLinus Torvalds 	dprintk(NDEBUG_ABORT, ("        basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG)));
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds #if 0
26971da177e4SLinus Torvalds /*
26981da177e4SLinus Torvalds  * Case 1 : If the command is the currently executing command,
26991da177e4SLinus Torvalds  * we'll set the aborted flag and return control so that
27001da177e4SLinus Torvalds  * information transfer routine can exit cleanly.
27011da177e4SLinus Torvalds  */
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds 	if (hostdata->connected == cmd) {
27041da177e4SLinus Torvalds 		dprintk(NDEBUG_ABORT, ("scsi%d : aborting connected command\n", instance->host_no));
27051da177e4SLinus Torvalds 		hostdata->aborted = 1;
27061da177e4SLinus Torvalds /*
27071da177e4SLinus Torvalds  * We should perform BSY checking, and make sure we haven't slipped
27081da177e4SLinus Torvalds  * into BUS FREE.
27091da177e4SLinus Torvalds  */
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
27121da177e4SLinus Torvalds /*
27131da177e4SLinus Torvalds  * Since we can't change phases until we've completed the current
27141da177e4SLinus Torvalds  * handshake, we have to source or sink a byte of data if the current
27151da177e4SLinus Torvalds  * phase is not MSGOUT.
27161da177e4SLinus Torvalds  */
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds /*
27191da177e4SLinus Torvalds  * Return control to the executing NCR drive so we can clear the
27201da177e4SLinus Torvalds  * aborted flag and get back into our main loop.
27211da177e4SLinus Torvalds  */
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds 		return 0;
27241da177e4SLinus Torvalds 	}
27251da177e4SLinus Torvalds #endif
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds /*
27281da177e4SLinus Torvalds  * Case 2 : If the command hasn't been issued yet, we simply remove it
27291da177e4SLinus Torvalds  *          from the issue queue.
27301da177e4SLinus Torvalds  */
27311da177e4SLinus Torvalds 
27321da177e4SLinus Torvalds 	dprintk(NDEBUG_ABORT, ("scsi%d : abort going into loop.\n", instance->host_no));
27331da177e4SLinus Torvalds 	for (prev = (Scsi_Cmnd **) & (hostdata->issue_queue), tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble)
27341da177e4SLinus Torvalds 		if (cmd == tmp) {
27351da177e4SLinus Torvalds 			REMOVE(5, *prev, tmp, tmp->host_scribble);
27361da177e4SLinus Torvalds 			(*prev) = (Scsi_Cmnd *) tmp->host_scribble;
27371da177e4SLinus Torvalds 			tmp->host_scribble = NULL;
27381da177e4SLinus Torvalds 			tmp->result = DID_ABORT << 16;
27391da177e4SLinus Torvalds 			dprintk(NDEBUG_ABORT, ("scsi%d : abort removed command from issue queue.\n", instance->host_no));
2740cce99c69SMatthew Wilcox 			tmp->scsi_done(tmp);
27411da177e4SLinus Torvalds 			return SUCCESS;
27421da177e4SLinus Torvalds 		}
27431da177e4SLinus Torvalds #if (NDEBUG  & NDEBUG_ABORT)
27441da177e4SLinus Torvalds 	/* KLL */
27451da177e4SLinus Torvalds 		else if (prev == tmp)
27461da177e4SLinus Torvalds 			printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
27471da177e4SLinus Torvalds #endif
27481da177e4SLinus Torvalds 
27491da177e4SLinus Torvalds /*
27501da177e4SLinus Torvalds  * Case 3 : If any commands are connected, we're going to fail the abort
27511da177e4SLinus Torvalds  *          and let the high level SCSI driver retry at a later time or
27521da177e4SLinus Torvalds  *          issue a reset.
27531da177e4SLinus Torvalds  *
27541da177e4SLinus Torvalds  *          Timeouts, and therefore aborted commands, will be highly unlikely
27551da177e4SLinus Torvalds  *          and handling them cleanly in this situation would make the common
27561da177e4SLinus Torvalds  *          case of noresets less efficient, and would pollute our code.  So,
27571da177e4SLinus Torvalds  *          we fail.
27581da177e4SLinus Torvalds  */
27591da177e4SLinus Torvalds 
27601da177e4SLinus Torvalds 	if (hostdata->connected) {
27611da177e4SLinus Torvalds 		dprintk(NDEBUG_ABORT, ("scsi%d : abort failed, command connected.\n", instance->host_no));
27621da177e4SLinus Torvalds 		return FAILED;
27631da177e4SLinus Torvalds 	}
27641da177e4SLinus Torvalds /*
27651da177e4SLinus Torvalds  * Case 4: If the command is currently disconnected from the bus, and
27661da177e4SLinus Torvalds  *      there are no connected commands, we reconnect the I_T_L or
27671da177e4SLinus Torvalds  *      I_T_L_Q nexus associated with it, go into message out, and send
27681da177e4SLinus Torvalds  *      an abort message.
27691da177e4SLinus Torvalds  *
27701da177e4SLinus Torvalds  * This case is especially ugly. In order to reestablish the nexus, we
27711da177e4SLinus Torvalds  * need to call NCR5380_select().  The easiest way to implement this
27721da177e4SLinus Torvalds  * function was to abort if the bus was busy, and let the interrupt
27731da177e4SLinus Torvalds  * handler triggered on the SEL for reselect take care of lost arbitrations
27741da177e4SLinus Torvalds  * where necessary, meaning interrupts need to be enabled.
27751da177e4SLinus Torvalds  *
27761da177e4SLinus Torvalds  * When interrupts are enabled, the queues may change - so we
27771da177e4SLinus Torvalds  * can't remove it from the disconnected queue before selecting it
27781da177e4SLinus Torvalds  * because that could cause a failure in hashing the nexus if that
27791da177e4SLinus Torvalds  * device reselected.
27801da177e4SLinus Torvalds  *
27811da177e4SLinus Torvalds  * Since the queues may change, we can't use the pointers from when we
27821da177e4SLinus Torvalds  * first locate it.
27831da177e4SLinus Torvalds  *
27841da177e4SLinus Torvalds  * So, we must first locate the command, and if NCR5380_select()
27851da177e4SLinus Torvalds  * succeeds, then issue the abort, relocate the command and remove
27861da177e4SLinus Torvalds  * it from the disconnected queue.
27871da177e4SLinus Torvalds  */
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds 	for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; tmp = (Scsi_Cmnd *) tmp->host_scribble)
27901da177e4SLinus Torvalds 		if (cmd == tmp) {
27911da177e4SLinus Torvalds 			dprintk(NDEBUG_ABORT, ("scsi%d : aborting disconnected command.\n", instance->host_no));
27921da177e4SLinus Torvalds 
27931da177e4SLinus Torvalds 			if (NCR5380_select(instance, cmd, (int) cmd->tag))
27941da177e4SLinus Torvalds 				return FAILED;
27951da177e4SLinus Torvalds 			dprintk(NDEBUG_ABORT, ("scsi%d : nexus reestablished.\n", instance->host_no));
27961da177e4SLinus Torvalds 
27971da177e4SLinus Torvalds 			do_abort(instance);
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds 			for (prev = (Scsi_Cmnd **) & (hostdata->disconnected_queue), tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble)
28001da177e4SLinus Torvalds 				if (cmd == tmp) {
28011da177e4SLinus Torvalds 					REMOVE(5, *prev, tmp, tmp->host_scribble);
28021da177e4SLinus Torvalds 					*prev = (Scsi_Cmnd *) tmp->host_scribble;
28031da177e4SLinus Torvalds 					tmp->host_scribble = NULL;
28041da177e4SLinus Torvalds 					tmp->result = DID_ABORT << 16;
2805cce99c69SMatthew Wilcox 					tmp->scsi_done(tmp);
28061da177e4SLinus Torvalds 					return SUCCESS;
28071da177e4SLinus Torvalds 				}
28081da177e4SLinus Torvalds 		}
28091da177e4SLinus Torvalds /*
28101da177e4SLinus Torvalds  * Case 5 : If we reached this point, the command was not found in any of
28111da177e4SLinus Torvalds  *          the queues.
28121da177e4SLinus Torvalds  *
28131da177e4SLinus Torvalds  * We probably reached this point because of an unlikely race condition
28141da177e4SLinus Torvalds  * between the command completing successfully and the abortion code,
28151da177e4SLinus Torvalds  * so we won't panic, but we will notify the user in case something really
28161da177e4SLinus Torvalds  * broke.
28171da177e4SLinus Torvalds  */
28181da177e4SLinus Torvalds 	printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
28191da177e4SLinus Torvalds 			"         before abortion\n", instance->host_no);
28201da177e4SLinus Torvalds 	return FAILED;
28211da177e4SLinus Torvalds }
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds 
28241da177e4SLinus Torvalds /*
28251da177e4SLinus Torvalds  * Function : int NCR5380_bus_reset (Scsi_Cmnd *cmd)
28261da177e4SLinus Torvalds  *
28271da177e4SLinus Torvalds  * Purpose : reset the SCSI bus.
28281da177e4SLinus Torvalds  *
28291da177e4SLinus Torvalds  * Returns : SUCCESS
28301da177e4SLinus Torvalds  *
28311da177e4SLinus Torvalds  * Locks: host lock taken by caller
28321da177e4SLinus Torvalds  */
28331da177e4SLinus Torvalds 
283468b3aa7cSJeff Garzik  static int NCR5380_bus_reset(Scsi_Cmnd * cmd)
283568b3aa7cSJeff Garzik  {
283668b3aa7cSJeff Garzik  	struct Scsi_Host *instance = cmd->device->host;
28371da177e4SLinus Torvalds 
283868b3aa7cSJeff Garzik  	NCR5380_local_declare();
283968b3aa7cSJeff Garzik  	NCR5380_setup(instance);
284068b3aa7cSJeff Garzik  	NCR5380_print_status(instance);
284168b3aa7cSJeff Garzik  
284268b3aa7cSJeff Garzik  	spin_lock_irq(instance->host_lock);
284368b3aa7cSJeff Garzik  	do_reset(instance);
284468b3aa7cSJeff Garzik  	spin_unlock_irq(instance->host_lock);
284568b3aa7cSJeff Garzik  
28461da177e4SLinus Torvalds 	return SUCCESS;
28471da177e4SLinus Torvalds }
2848