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