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