xref: /linux/drivers/scsi/NCR5380.c (revision e8a601444297e336a7c4355f07b4bb386962894c)
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  * For more information, please consult
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * NCR 5380 Family
171da177e4SLinus Torvalds  * SCSI Protocol Controller
181da177e4SLinus Torvalds  * Databook
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * NCR Microelectronics
211da177e4SLinus Torvalds  * 1635 Aeroplaza Drive
221da177e4SLinus Torvalds  * Colorado Springs, CO 80916
231da177e4SLinus Torvalds  * 1+ (719) 578-3400
241da177e4SLinus Torvalds  * 1+ (800) 334-5454
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds /*
281da177e4SLinus Torvalds  * Revision 1.10 1998/9/2	Alan Cox
29fa195afeSAlan Cox  *				(alan@lxorguk.ukuu.org.uk)
301da177e4SLinus Torvalds  * Fixed up the timer lockups reported so far. Things still suck. Looking
311da177e4SLinus Torvalds  * forward to 2.3 and per device request queues. Then it'll be possible to
321da177e4SLinus Torvalds  * SMP thread this beast and improve life no end.
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds  * Revision 1.9  1997/7/27	Ronald van Cuijlenborg
351da177e4SLinus Torvalds  *				(ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
361da177e4SLinus Torvalds  * (hopefully) fixed and enhanced USLEEP
371da177e4SLinus Torvalds  * added support for DTC3181E card (for Mustek scanner)
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds  * Revision 1.8			Ingmar Baumgart
411da177e4SLinus Torvalds  *				(ingmar@gonzo.schwaben.de)
421da177e4SLinus Torvalds  * added support for NCR53C400a card
431da177e4SLinus Torvalds  *
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds  * Revision 1.7  1996/3/2       Ray Van Tassle (rayvt@comm.mot.com)
461da177e4SLinus Torvalds  * added proc_info
471da177e4SLinus Torvalds  * added support needed for DTC 3180/3280
481da177e4SLinus Torvalds  * fixed a couple of bugs
491da177e4SLinus Torvalds  *
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds  * Revision 1.5  1994/01/19  09:14:57  drew
521da177e4SLinus Torvalds  * Fixed udelay() hack that was being used on DATAOUT phases
531da177e4SLinus Torvalds  * instead of a proper wait for the final handshake.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * Revision 1.4  1994/01/19  06:44:25  drew
561da177e4SLinus Torvalds  * *** empty log message ***
571da177e4SLinus Torvalds  *
581da177e4SLinus Torvalds  * Revision 1.3  1994/01/19  05:24:40  drew
591da177e4SLinus Torvalds  * Added support for TCR LAST_BYTE_SENT bit.
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * Revision 1.2  1994/01/15  06:14:11  drew
621da177e4SLinus Torvalds  * REAL DMA support, bug fixes.
631da177e4SLinus Torvalds  *
641da177e4SLinus Torvalds  * Revision 1.1  1994/01/15  06:00:54  drew
651da177e4SLinus Torvalds  * Initial revision
661da177e4SLinus Torvalds  *
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /*
701da177e4SLinus Torvalds  * Further development / testing that should be done :
711da177e4SLinus Torvalds  * 1.  Cleanup the NCR5380_transfer_dma function and DMA operation complete
721da177e4SLinus Torvalds  *     code so that everything does the same thing that's done at the
731da177e4SLinus Torvalds  *     end of a pseudo-DMA read operation.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * 2.  Fix REAL_DMA (interrupt driven, polled works fine) -
761da177e4SLinus Torvalds  *     basically, transfer size needs to be reduced by one
771da177e4SLinus Torvalds  *     and the last byte read as is done with PSEUDO_DMA.
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  * 4.  Test SCSI-II tagged queueing (I have no devices which support
801da177e4SLinus Torvalds  *      tagged queueing)
811da177e4SLinus Torvalds  */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #if (NDEBUG & NDEBUG_LISTS)
841da177e4SLinus Torvalds #define LIST(x,y) {printk("LINE:%d   Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
851da177e4SLinus 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); }
861da177e4SLinus Torvalds #else
871da177e4SLinus Torvalds #define LIST(x,y)
881da177e4SLinus Torvalds #define REMOVE(w,x,y,z)
891da177e4SLinus Torvalds #endif
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #ifndef notyet
921da177e4SLinus Torvalds #undef REAL_DMA
931da177e4SLinus Torvalds #endif
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds #ifdef BOARD_REQUIRES_NO_DELAY
961da177e4SLinus Torvalds #define io_recovery_delay(x)
971da177e4SLinus Torvalds #else
981da177e4SLinus Torvalds #define io_recovery_delay(x)	udelay(x)
991da177e4SLinus Torvalds #endif
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /*
1021da177e4SLinus Torvalds  * Design
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  * This is a generic 5380 driver.  To use it on a different platform,
1051da177e4SLinus Torvalds  * one simply writes appropriate system specific macros (ie, data
1061da177e4SLinus Torvalds  * transfer - some PC's will use the I/O bus, 68K's must use
1071da177e4SLinus Torvalds  * memory mapped) and drops this file in their 'C' wrapper.
1081da177e4SLinus Torvalds  *
1091da177e4SLinus Torvalds  * (Note from hch:  unfortunately it was not enough for the different
1101da177e4SLinus Torvalds  * m68k folks and instead of improving this driver they copied it
1111da177e4SLinus Torvalds  * and hacked it up for their needs.  As a consequence they lost
1121da177e4SLinus Torvalds  * most updates to this driver.  Maybe someone will fix all these
1131da177e4SLinus Torvalds  * drivers to use a common core one day..)
1141da177e4SLinus Torvalds  *
1151da177e4SLinus Torvalds  * As far as command queueing, two queues are maintained for
1161da177e4SLinus Torvalds  * each 5380 in the system - commands that haven't been issued yet,
1171da177e4SLinus Torvalds  * and commands that are currently executing.  This means that an
1181da177e4SLinus Torvalds  * unlimited number of commands may be queued, letting
1191da177e4SLinus Torvalds  * more commands propagate from the higher driver levels giving higher
1201da177e4SLinus Torvalds  * throughput.  Note that both I_T_L and I_T_L_Q nexuses are supported,
1211da177e4SLinus Torvalds  * allowing multiple commands to propagate all the way to a SCSI-II device
1221da177e4SLinus Torvalds  * while a command is already executing.
1231da177e4SLinus Torvalds  *
1241da177e4SLinus Torvalds  *
1251da177e4SLinus Torvalds  * Issues specific to the NCR5380 :
1261da177e4SLinus Torvalds  *
1271da177e4SLinus Torvalds  * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
1281da177e4SLinus Torvalds  * piece of hardware that requires you to sit in a loop polling for
1291da177e4SLinus Torvalds  * the REQ signal as long as you are connected.  Some devices are
1301da177e4SLinus Torvalds  * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
131686f3990SFinn Thain  * while doing long seek operations. [...] These
1321da177e4SLinus Torvalds  * broken devices are the exception rather than the rule and I'd rather
1331da177e4SLinus Torvalds  * spend my time optimizing for the normal case.
1341da177e4SLinus Torvalds  *
1351da177e4SLinus Torvalds  * Architecture :
1361da177e4SLinus Torvalds  *
1371da177e4SLinus Torvalds  * At the heart of the design is a coroutine, NCR5380_main,
1381da177e4SLinus Torvalds  * which is started from a workqueue for each NCR5380 host in the
1391da177e4SLinus Torvalds  * system.  It attempts to establish I_T_L or I_T_L_Q nexuses by
1401da177e4SLinus Torvalds  * removing the commands from the issue queue and calling
1411da177e4SLinus Torvalds  * NCR5380_select() if a nexus is not established.
1421da177e4SLinus Torvalds  *
1431da177e4SLinus Torvalds  * Once a nexus is established, the NCR5380_information_transfer()
1441da177e4SLinus Torvalds  * phase goes through the various phases as instructed by the target.
1451da177e4SLinus Torvalds  * if the target goes into MSG IN and sends a DISCONNECT message,
1461da177e4SLinus Torvalds  * the command structure is placed into the per instance disconnected
1471da177e4SLinus Torvalds  * queue, and NCR5380_main tries to find more work.  If the target is
1481da177e4SLinus Torvalds  * idle for too long, the system will try to sleep.
1491da177e4SLinus Torvalds  *
1501da177e4SLinus Torvalds  * If a command has disconnected, eventually an interrupt will trigger,
1511da177e4SLinus Torvalds  * calling NCR5380_intr()  which will in turn call NCR5380_reselect
1521da177e4SLinus Torvalds  * to reestablish a nexus.  This will run main if necessary.
1531da177e4SLinus Torvalds  *
1541da177e4SLinus Torvalds  * On command termination, the done function will be called as
1551da177e4SLinus Torvalds  * appropriate.
1561da177e4SLinus Torvalds  *
1571da177e4SLinus Torvalds  * SCSI pointers are maintained in the SCp field of SCSI command
1581da177e4SLinus Torvalds  * structures, being initialized after the command is connected
1591da177e4SLinus Torvalds  * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
1601da177e4SLinus Torvalds  * Note that in violation of the standard, an implicit SAVE POINTERS operation
1611da177e4SLinus Torvalds  * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
1621da177e4SLinus Torvalds  */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /*
1651da177e4SLinus Torvalds  * Using this file :
1661da177e4SLinus Torvalds  * This file a skeleton Linux SCSI driver for the NCR 5380 series
1671da177e4SLinus Torvalds  * of chips.  To use it, you write an architecture specific functions
1681da177e4SLinus Torvalds  * and macros and include this file in your driver.
1691da177e4SLinus Torvalds  *
1701da177e4SLinus Torvalds  * These macros control options :
1711da177e4SLinus Torvalds  * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
1721da177e4SLinus Torvalds  *      defined.
1731da177e4SLinus Torvalds  *
1741da177e4SLinus Torvalds  * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
1751da177e4SLinus Torvalds  *      for commands that return with a CHECK CONDITION status.
1761da177e4SLinus Torvalds  *
1771da177e4SLinus Torvalds  * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
1781da177e4SLinus Torvalds  *      transceivers.
1791da177e4SLinus Torvalds  *
1801da177e4SLinus Torvalds  * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
1811da177e4SLinus Torvalds  *      override-configure an IRQ.
1821da177e4SLinus Torvalds  *
1831da177e4SLinus Torvalds  * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
1841da177e4SLinus Torvalds  *
1851da177e4SLinus Torvalds  * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
1861da177e4SLinus Torvalds  *
1871da177e4SLinus Torvalds  * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
1881da177e4SLinus Torvalds  *      rely on phase mismatch and EOP interrupts to determine end
1891da177e4SLinus Torvalds  *      of phase.
1901da177e4SLinus Torvalds  *
1911da177e4SLinus Torvalds  * Defaults for these will be provided although the user may want to adjust
1921da177e4SLinus Torvalds  * these to allocate CPU resources to the SCSI driver or "real" code.
1931da177e4SLinus Torvalds  *
1941da177e4SLinus Torvalds  * These macros MUST be defined :
1951da177e4SLinus Torvalds  *
1961da177e4SLinus Torvalds  * NCR5380_read(register)  - read from the specified register
1971da177e4SLinus Torvalds  *
1981da177e4SLinus Torvalds  * NCR5380_write(register, value) - write to the specific register
1991da177e4SLinus Torvalds  *
2001da177e4SLinus Torvalds  * NCR5380_implementation_fields  - additional fields needed for this
2011da177e4SLinus Torvalds  *      specific implementation of the NCR5380
2021da177e4SLinus Torvalds  *
2031da177e4SLinus Torvalds  * Either real DMA *or* pseudo DMA may be implemented
2041da177e4SLinus Torvalds  * REAL functions :
2051da177e4SLinus Torvalds  * NCR5380_REAL_DMA should be defined if real DMA is to be used.
2061da177e4SLinus Torvalds  * Note that the DMA setup functions should return the number of bytes
2071da177e4SLinus Torvalds  *      that they were able to program the controller for.
2081da177e4SLinus Torvalds  *
2091da177e4SLinus Torvalds  * Also note that generic i386/PC versions of these macros are
2101da177e4SLinus Torvalds  *      available as NCR5380_i386_dma_write_setup,
2111da177e4SLinus Torvalds  *      NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
2121da177e4SLinus Torvalds  *
2131da177e4SLinus Torvalds  * NCR5380_dma_write_setup(instance, src, count) - initialize
2141da177e4SLinus Torvalds  * NCR5380_dma_read_setup(instance, dst, count) - initialize
2151da177e4SLinus Torvalds  * NCR5380_dma_residual(instance); - residual count
2161da177e4SLinus Torvalds  *
2171da177e4SLinus Torvalds  * PSEUDO functions :
2181da177e4SLinus Torvalds  * NCR5380_pwrite(instance, src, count)
2191da177e4SLinus Torvalds  * NCR5380_pread(instance, dst, count);
2201da177e4SLinus Torvalds  *
2211da177e4SLinus Torvalds  * The generic driver is initialized by calling NCR5380_init(instance),
2221da177e4SLinus Torvalds  * after setting the appropriate host specific fields and ID.  If the
2231da177e4SLinus Torvalds  * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
2241da177e4SLinus Torvalds  * possible) function may be used.
2251da177e4SLinus Torvalds  */
2261da177e4SLinus Torvalds 
22754d8fe44SFinn Thain static int do_abort(struct Scsi_Host *);
22854d8fe44SFinn Thain static void do_reset(struct Scsi_Host *);
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds /*
2311da177e4SLinus Torvalds  *	initialize_SCp		-	init the scsi pointer field
2321da177e4SLinus Torvalds  *	@cmd: command block to set up
2331da177e4SLinus Torvalds  *
2341da177e4SLinus Torvalds  *	Set up the internal fields in the SCSI command.
2351da177e4SLinus Torvalds  */
2361da177e4SLinus Torvalds 
237710ddd0dSFinn Thain static inline void initialize_SCp(struct scsi_cmnd *cmd)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	/*
2401da177e4SLinus Torvalds 	 * Initialize the Scsi Pointer field so that all of the commands in the
2411da177e4SLinus Torvalds 	 * various queues are valid.
2421da177e4SLinus Torvalds 	 */
2431da177e4SLinus Torvalds 
2449e0fe44dSBoaz Harrosh 	if (scsi_bufflen(cmd)) {
2459e0fe44dSBoaz Harrosh 		cmd->SCp.buffer = scsi_sglist(cmd);
2469e0fe44dSBoaz Harrosh 		cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
24745711f1aSJens Axboe 		cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
2481da177e4SLinus Torvalds 		cmd->SCp.this_residual = cmd->SCp.buffer->length;
2491da177e4SLinus Torvalds 	} else {
2501da177e4SLinus Torvalds 		cmd->SCp.buffer = NULL;
2511da177e4SLinus Torvalds 		cmd->SCp.buffers_residual = 0;
2529e0fe44dSBoaz Harrosh 		cmd->SCp.ptr = NULL;
2539e0fe44dSBoaz Harrosh 		cmd->SCp.this_residual = 0;
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds /**
258b32ade12SFinn Thain  * NCR5380_poll_politely2 - wait for two chip register values
2591da177e4SLinus Torvalds  * @instance: controller to poll
260b32ade12SFinn Thain  * @reg1: 5380 register to poll
261b32ade12SFinn Thain  * @bit1: Bitmask to check
262b32ade12SFinn Thain  * @val1: Expected value
263b32ade12SFinn Thain  * @reg2: Second 5380 register to poll
264b32ade12SFinn Thain  * @bit2: Second bitmask to check
265b32ade12SFinn Thain  * @val2: Second expected value
2662f854b82SFinn Thain  * @wait: Time-out in jiffies
2671da177e4SLinus Torvalds  *
2682f854b82SFinn Thain  * Polls the chip in a reasonably efficient manner waiting for an
2692f854b82SFinn Thain  * event to occur. After a short quick poll we begin to yield the CPU
2702f854b82SFinn Thain  * (if possible). In irq contexts the time-out is arbitrarily limited.
2712f854b82SFinn Thain  * Callers may hold locks as long as they are held in irq mode.
2721da177e4SLinus Torvalds  *
273b32ade12SFinn Thain  * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
2741da177e4SLinus Torvalds  */
2751da177e4SLinus Torvalds 
276b32ade12SFinn Thain static int NCR5380_poll_politely2(struct Scsi_Host *instance,
277b32ade12SFinn Thain                                   int reg1, int bit1, int val1,
278b32ade12SFinn Thain                                   int reg2, int bit2, int val2, int wait)
2791da177e4SLinus Torvalds {
2802f854b82SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
2812f854b82SFinn Thain 	unsigned long deadline = jiffies + wait;
2822f854b82SFinn Thain 	unsigned long n;
2831da177e4SLinus Torvalds 
2842f854b82SFinn Thain 	/* Busy-wait for up to 10 ms */
2852f854b82SFinn Thain 	n = min(10000U, jiffies_to_usecs(wait));
2862f854b82SFinn Thain 	n *= hostdata->accesses_per_ms;
287b32ade12SFinn Thain 	n /= 2000;
2882f854b82SFinn Thain 	do {
289b32ade12SFinn Thain 		if ((NCR5380_read(reg1) & bit1) == val1)
290b32ade12SFinn Thain 			return 0;
291b32ade12SFinn Thain 		if ((NCR5380_read(reg2) & bit2) == val2)
2921da177e4SLinus Torvalds 			return 0;
2931da177e4SLinus Torvalds 		cpu_relax();
2942f854b82SFinn Thain 	} while (n--);
2952f854b82SFinn Thain 
2962f854b82SFinn Thain 	if (irqs_disabled() || in_interrupt())
2972f854b82SFinn Thain 		return -ETIMEDOUT;
2982f854b82SFinn Thain 
2992f854b82SFinn Thain 	/* Repeatedly sleep for 1 ms until deadline */
3002f854b82SFinn Thain 	while (time_is_after_jiffies(deadline)) {
3012f854b82SFinn Thain 		schedule_timeout_uninterruptible(1);
302b32ade12SFinn Thain 		if ((NCR5380_read(reg1) & bit1) == val1)
303b32ade12SFinn Thain 			return 0;
304b32ade12SFinn Thain 		if ((NCR5380_read(reg2) & bit2) == val2)
3052f854b82SFinn Thain 			return 0;
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	return -ETIMEDOUT;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
311b32ade12SFinn Thain static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
312b32ade12SFinn Thain                                         int reg, int bit, int val, int wait)
313b32ade12SFinn Thain {
314b32ade12SFinn Thain 	return NCR5380_poll_politely2(instance, reg, bit, val,
315b32ade12SFinn Thain 	                                        reg, bit, val, wait);
316b32ade12SFinn Thain }
317b32ade12SFinn Thain 
3181da177e4SLinus Torvalds static struct {
3191da177e4SLinus Torvalds 	unsigned char value;
3201da177e4SLinus Torvalds 	const char *name;
321702809ceSAndrew Morton } phases[] __maybe_unused = {
3221da177e4SLinus Torvalds 	{PHASE_DATAOUT, "DATAOUT"},
3231da177e4SLinus Torvalds 	{PHASE_DATAIN, "DATAIN"},
3241da177e4SLinus Torvalds 	{PHASE_CMDOUT, "CMDOUT"},
3251da177e4SLinus Torvalds 	{PHASE_STATIN, "STATIN"},
3261da177e4SLinus Torvalds 	{PHASE_MSGOUT, "MSGOUT"},
3271da177e4SLinus Torvalds 	{PHASE_MSGIN, "MSGIN"},
3281da177e4SLinus Torvalds 	{PHASE_UNKNOWN, "UNKNOWN"}
3291da177e4SLinus Torvalds };
3301da177e4SLinus Torvalds 
331185a7a1cSviro@ZenIV.linux.org.uk #if NDEBUG
3321da177e4SLinus Torvalds static struct {
3331da177e4SLinus Torvalds 	unsigned char mask;
3341da177e4SLinus Torvalds 	const char *name;
3351da177e4SLinus Torvalds } signals[] = {
3361da177e4SLinus Torvalds 	{SR_DBP, "PARITY"},
3371da177e4SLinus Torvalds 	{SR_RST, "RST"},
3381da177e4SLinus Torvalds 	{SR_BSY, "BSY"},
3391da177e4SLinus Torvalds 	{SR_REQ, "REQ"},
3401da177e4SLinus Torvalds 	{SR_MSG, "MSG"},
3411da177e4SLinus Torvalds 	{SR_CD, "CD"},
3421da177e4SLinus Torvalds 	{SR_IO, "IO"},
3431da177e4SLinus Torvalds 	{SR_SEL, "SEL"},
3441da177e4SLinus Torvalds 	{0, NULL}
3451da177e4SLinus Torvalds },
3461da177e4SLinus Torvalds basrs[] = {
3471da177e4SLinus Torvalds 	{BASR_ATN, "ATN"},
3481da177e4SLinus Torvalds 	{BASR_ACK, "ACK"},
3491da177e4SLinus Torvalds 	{0, NULL}
3501da177e4SLinus Torvalds },
3511da177e4SLinus Torvalds icrs[] = {
3521da177e4SLinus Torvalds 	{ICR_ASSERT_RST, "ASSERT RST"},
3531da177e4SLinus Torvalds 	{ICR_ASSERT_ACK, "ASSERT ACK"},
3541da177e4SLinus Torvalds 	{ICR_ASSERT_BSY, "ASSERT BSY"},
3551da177e4SLinus Torvalds 	{ICR_ASSERT_SEL, "ASSERT SEL"},
3561da177e4SLinus Torvalds 	{ICR_ASSERT_ATN, "ASSERT ATN"},
3571da177e4SLinus Torvalds 	{ICR_ASSERT_DATA, "ASSERT DATA"},
3581da177e4SLinus Torvalds 	{0, NULL}
3591da177e4SLinus Torvalds },
3601da177e4SLinus Torvalds mrs[] = {
3611da177e4SLinus Torvalds 	{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
3621da177e4SLinus Torvalds 	{MR_TARGET, "MODE TARGET"},
3631da177e4SLinus Torvalds 	{MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
3641da177e4SLinus Torvalds 	{MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
3651da177e4SLinus Torvalds 	{MR_MONITOR_BSY, "MODE MONITOR BSY"},
3661da177e4SLinus Torvalds 	{MR_DMA_MODE, "MODE DMA"},
3671da177e4SLinus Torvalds 	{MR_ARBITRATE, "MODE ARBITRATION"},
3681da177e4SLinus Torvalds 	{0, NULL}
3691da177e4SLinus Torvalds };
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds /**
3721da177e4SLinus Torvalds  *	NCR5380_print	-	print scsi bus signals
3731da177e4SLinus Torvalds  *	@instance:	adapter state to dump
3741da177e4SLinus Torvalds  *
3751da177e4SLinus Torvalds  *	Print the SCSI bus signals for debugging purposes
3761da177e4SLinus Torvalds  *
3771da177e4SLinus Torvalds  *	Locks: caller holds hostdata lock (not essential)
3781da177e4SLinus Torvalds  */
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds static void NCR5380_print(struct Scsi_Host *instance)
3811da177e4SLinus Torvalds {
3821da177e4SLinus Torvalds 	unsigned char status, data, basr, mr, icr, i;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	data = NCR5380_read(CURRENT_SCSI_DATA_REG);
3851da177e4SLinus Torvalds 	status = NCR5380_read(STATUS_REG);
3861da177e4SLinus Torvalds 	mr = NCR5380_read(MODE_REG);
3871da177e4SLinus Torvalds 	icr = NCR5380_read(INITIATOR_COMMAND_REG);
3881da177e4SLinus Torvalds 	basr = NCR5380_read(BUS_AND_STATUS_REG);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	printk("STATUS_REG: %02x ", status);
3911da177e4SLinus Torvalds 	for (i = 0; signals[i].mask; ++i)
3921da177e4SLinus Torvalds 		if (status & signals[i].mask)
3931da177e4SLinus Torvalds 			printk(",%s", signals[i].name);
3941da177e4SLinus Torvalds 	printk("\nBASR: %02x ", basr);
3951da177e4SLinus Torvalds 	for (i = 0; basrs[i].mask; ++i)
3961da177e4SLinus Torvalds 		if (basr & basrs[i].mask)
3971da177e4SLinus Torvalds 			printk(",%s", basrs[i].name);
3981da177e4SLinus Torvalds 	printk("\nICR: %02x ", icr);
3991da177e4SLinus Torvalds 	for (i = 0; icrs[i].mask; ++i)
4001da177e4SLinus Torvalds 		if (icr & icrs[i].mask)
4011da177e4SLinus Torvalds 			printk(",%s", icrs[i].name);
4021da177e4SLinus Torvalds 	printk("\nMODE: %02x ", mr);
4031da177e4SLinus Torvalds 	for (i = 0; mrs[i].mask; ++i)
4041da177e4SLinus Torvalds 		if (mr & mrs[i].mask)
4051da177e4SLinus Torvalds 			printk(",%s", mrs[i].name);
4061da177e4SLinus Torvalds 	printk("\n");
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /*
4111da177e4SLinus Torvalds  *	NCR5380_print_phase	-	show SCSI phase
4121da177e4SLinus Torvalds  *	@instance: adapter to dump
4131da177e4SLinus Torvalds  *
4141da177e4SLinus Torvalds  * 	Print the current SCSI phase for debugging purposes
4151da177e4SLinus Torvalds  *
4161da177e4SLinus Torvalds  *	Locks: none
4171da177e4SLinus Torvalds  */
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds static void NCR5380_print_phase(struct Scsi_Host *instance)
4201da177e4SLinus Torvalds {
4211da177e4SLinus Torvalds 	unsigned char status;
4221da177e4SLinus Torvalds 	int i;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	status = NCR5380_read(STATUS_REG);
4251da177e4SLinus Torvalds 	if (!(status & SR_REQ))
4261da177e4SLinus Torvalds 		printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
4271da177e4SLinus Torvalds 	else {
4281da177e4SLinus Torvalds 		for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
4291da177e4SLinus Torvalds 		printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds #endif
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 
435d5f7e65dSFinn Thain static int probe_irq __initdata;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds /**
4381da177e4SLinus Torvalds  *	probe_intr	-	helper for IRQ autoprobe
4391da177e4SLinus Torvalds  *	@irq: interrupt number
4401da177e4SLinus Torvalds  *	@dev_id: unused
4411da177e4SLinus Torvalds  *	@regs: unused
4421da177e4SLinus Torvalds  *
4431da177e4SLinus Torvalds  *	Set a flag to indicate the IRQ in question was received. This is
4441da177e4SLinus Torvalds  *	used by the IRQ probe code.
4451da177e4SLinus Torvalds  */
4461da177e4SLinus Torvalds 
4477d12e780SDavid Howells static irqreturn_t __init probe_intr(int irq, void *dev_id)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	probe_irq = irq;
4501da177e4SLinus Torvalds 	return IRQ_HANDLED;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds /**
4541da177e4SLinus Torvalds  *	NCR5380_probe_irq	-	find the IRQ of an NCR5380
4551da177e4SLinus Torvalds  *	@instance: NCR5380 controller
4561da177e4SLinus Torvalds  *	@possible: bitmask of ISA IRQ lines
4571da177e4SLinus Torvalds  *
4581da177e4SLinus Torvalds  *	Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
4591da177e4SLinus Torvalds  *	and then looking to see what interrupt actually turned up.
4601da177e4SLinus Torvalds  *
4611da177e4SLinus Torvalds  *	Locks: none, irqs must be enabled on entry
4621da177e4SLinus Torvalds  */
4631da177e4SLinus Torvalds 
464702809ceSAndrew Morton static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
465702809ceSAndrew Morton 						int possible)
4661da177e4SLinus Torvalds {
467*e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
4681da177e4SLinus Torvalds 	unsigned long timeout;
4691da177e4SLinus Torvalds 	int trying_irqs, i, mask;
4701da177e4SLinus Torvalds 
47122f5f10dSFinn Thain 	for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
4724909cc2bSMichael Opdenacker 		if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
4731da177e4SLinus Torvalds 			trying_irqs |= mask;
4741da177e4SLinus Torvalds 
4754e5a800cSNicholas Mc Guire 	timeout = jiffies + msecs_to_jiffies(250);
47622f5f10dSFinn Thain 	probe_irq = NO_IRQ;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	/*
4791da177e4SLinus Torvalds 	 * A interrupt is triggered whenever BSY = false, SEL = true
4801da177e4SLinus Torvalds 	 * and a bit set in the SELECT_ENABLE_REG is asserted on the
4811da177e4SLinus Torvalds 	 * SCSI bus.
4821da177e4SLinus Torvalds 	 *
4831da177e4SLinus Torvalds 	 * Note that the bus is only driven when the phase control signals
4841da177e4SLinus Torvalds 	 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
4851da177e4SLinus Torvalds 	 * to zero.
4861da177e4SLinus Torvalds 	 */
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, 0);
4891da177e4SLinus Torvalds 	NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
4901da177e4SLinus Torvalds 	NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
4911da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
4921da177e4SLinus Torvalds 
49322f5f10dSFinn Thain 	while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
494a9a3047dSNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	NCR5380_write(SELECT_ENABLE_REG, 0);
4971da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
4981da177e4SLinus Torvalds 
49922f5f10dSFinn Thain 	for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
5001da177e4SLinus Torvalds 		if (trying_irqs & mask)
5011da177e4SLinus Torvalds 			free_irq(i, NULL);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	return probe_irq;
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds /**
5078c32513bSFinn Thain  *	NCR58380_info - report driver and host information
5088c32513bSFinn Thain  *	@instance: relevant scsi host instance
5091da177e4SLinus Torvalds  *
5108c32513bSFinn Thain  *	For use as the host template info() handler.
5111da177e4SLinus Torvalds  *
5121da177e4SLinus Torvalds  *	Locks: none
5131da177e4SLinus Torvalds  */
5141da177e4SLinus Torvalds 
5158c32513bSFinn Thain static const char *NCR5380_info(struct Scsi_Host *instance)
5161da177e4SLinus Torvalds {
5178c32513bSFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
5188c32513bSFinn Thain 
5198c32513bSFinn Thain 	return hostdata->info;
5208c32513bSFinn Thain }
5218c32513bSFinn Thain 
5228c32513bSFinn Thain static void prepare_info(struct Scsi_Host *instance)
5238c32513bSFinn Thain {
5248c32513bSFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
5258c32513bSFinn Thain 
5268c32513bSFinn Thain 	snprintf(hostdata->info, sizeof(hostdata->info),
5278c32513bSFinn Thain 	         "%s, io_port 0x%lx, n_io_port %d, "
5288c32513bSFinn Thain 	         "base 0x%lx, irq %d, "
5298c32513bSFinn Thain 	         "can_queue %d, cmd_per_lun %d, "
5308c32513bSFinn Thain 	         "sg_tablesize %d, this_id %d, "
531be3f4121SFinn Thain 	         "flags { %s%s%s}, "
5328c32513bSFinn Thain 	         "options { %s} ",
5338c32513bSFinn Thain 	         instance->hostt->name, instance->io_port, instance->n_io_port,
5348c32513bSFinn Thain 	         instance->base, instance->irq,
5358c32513bSFinn Thain 	         instance->can_queue, instance->cmd_per_lun,
5368c32513bSFinn Thain 	         instance->sg_tablesize, instance->this_id,
53755181be8SFinn Thain 	         hostdata->flags & FLAG_NO_DMA_FIXUP  ? "NO_DMA_FIXUP "  : "",
5388c32513bSFinn Thain 	         hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
5399c3f0e2bSFinn Thain 	         hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY "  : "",
5401da177e4SLinus Torvalds #ifdef AUTOPROBE_IRQ
5411da177e4SLinus Torvalds 	         "AUTOPROBE_IRQ "
5421da177e4SLinus Torvalds #endif
5431da177e4SLinus Torvalds #ifdef DIFFERENTIAL
5441da177e4SLinus Torvalds 	         "DIFFERENTIAL "
5451da177e4SLinus Torvalds #endif
5461da177e4SLinus Torvalds #ifdef REAL_DMA
5478c32513bSFinn Thain 	         "REAL_DMA "
5481da177e4SLinus Torvalds #endif
5491da177e4SLinus Torvalds #ifdef REAL_DMA_POLL
5508c32513bSFinn Thain 	         "REAL_DMA_POLL "
5511da177e4SLinus Torvalds #endif
5521da177e4SLinus Torvalds #ifdef PARITY
5531da177e4SLinus Torvalds 	         "PARITY "
5541da177e4SLinus Torvalds #endif
5551da177e4SLinus Torvalds #ifdef PSEUDO_DMA
5568c32513bSFinn Thain 	         "PSEUDO_DMA "
5571da177e4SLinus Torvalds #endif
5588c32513bSFinn Thain 	         "");
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds 
561a9c2dc43SFinn Thain #ifdef PSEUDO_DMA
5621da177e4SLinus Torvalds /******************************************/
5631da177e4SLinus Torvalds /*
5641da177e4SLinus Torvalds  * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
5651da177e4SLinus Torvalds  *
5661da177e4SLinus Torvalds  * *buffer: I/O buffer
5671da177e4SLinus Torvalds  * **start: if inout == FALSE pointer into buffer where user read should start
5681da177e4SLinus Torvalds  * offset: current offset
5691da177e4SLinus Torvalds  * length: length of buffer
5701da177e4SLinus Torvalds  * hostno: Scsi_Host host_no
5711da177e4SLinus Torvalds  * inout: TRUE - user is writing; FALSE - user is reading
5721da177e4SLinus Torvalds  *
5731da177e4SLinus Torvalds  * Return the number of bytes read from or written
5741da177e4SLinus Torvalds  */
5751da177e4SLinus Torvalds 
576dd7ab71bSAl Viro static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
577dd7ab71bSAl Viro 	char *buffer, int length)
5781da177e4SLinus Torvalds {
579a9c2dc43SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
580a9c2dc43SFinn Thain 
581a9c2dc43SFinn Thain 	hostdata->spin_max_r = 0;
582a9c2dc43SFinn Thain 	hostdata->spin_max_w = 0;
583a9c2dc43SFinn Thain 	return 0;
5841da177e4SLinus Torvalds }
585dd7ab71bSAl Viro 
586dd7ab71bSAl Viro static int __maybe_unused NCR5380_show_info(struct seq_file *m,
587dd7ab71bSAl Viro 	struct Scsi_Host *instance)
588dd7ab71bSAl Viro {
589*e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
590dd7ab71bSAl Viro 
5910c3de38fSRasmus Villemoes 	seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
592a9c2dc43SFinn Thain 	        hostdata->spin_max_w, hostdata->spin_max_r);
5931da177e4SLinus Torvalds 	return 0;
5941da177e4SLinus Torvalds }
595e5c3fddfSFinn Thain #endif
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds /**
5981da177e4SLinus Torvalds  *	NCR5380_init	-	initialise an NCR5380
5991da177e4SLinus Torvalds  *	@instance: adapter to configure
6001da177e4SLinus Torvalds  *	@flags: control flags
6011da177e4SLinus Torvalds  *
6021da177e4SLinus Torvalds  *	Initializes *instance and corresponding 5380 chip,
6031da177e4SLinus Torvalds  *      with flags OR'd into the initial flags value.
6041da177e4SLinus Torvalds  *
6051da177e4SLinus Torvalds  *	Notes : I assume that the host, hostno, and id bits have been
6061da177e4SLinus Torvalds  *      set correctly.  I don't care about the irq and other fields.
6071da177e4SLinus Torvalds  *
6081da177e4SLinus Torvalds  *	Returns 0 for success
6091da177e4SLinus Torvalds  *
6101da177e4SLinus Torvalds  *	Locks: interrupts must be enabled when we are called
6111da177e4SLinus Torvalds  */
6121da177e4SLinus Torvalds 
6136f039790SGreg Kroah-Hartman static int NCR5380_init(struct Scsi_Host *instance, int flags)
6141da177e4SLinus Torvalds {
615*e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
616b6488f97SFinn Thain 	int i;
6172f854b82SFinn Thain 	unsigned long deadline;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	if(in_interrupt())
6201da177e4SLinus Torvalds 		printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	hostdata->id_mask = 1 << instance->this_id;
6231da177e4SLinus Torvalds 	for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
6241da177e4SLinus Torvalds 		if (i > hostdata->id_mask)
6251da177e4SLinus Torvalds 			hostdata->id_higher_mask |= i;
6261da177e4SLinus Torvalds 	for (i = 0; i < 8; ++i)
6271da177e4SLinus Torvalds 		hostdata->busy[i] = 0;
6281da177e4SLinus Torvalds #ifdef REAL_DMA
6291da177e4SLinus Torvalds 	hostdata->dmalen = 0;
6301da177e4SLinus Torvalds #endif
63111d2f63bSFinn Thain 	spin_lock_init(&hostdata->lock);
6321da177e4SLinus Torvalds 	hostdata->connected = NULL;
6331da177e4SLinus Torvalds 	hostdata->issue_queue = NULL;
6341da177e4SLinus Torvalds 	hostdata->disconnected_queue = NULL;
63555181be8SFinn Thain 	hostdata->flags = flags;
6361da177e4SLinus Torvalds 
6378d8601a7SFinn Thain 	INIT_WORK(&hostdata->main_task, NCR5380_main);
6380ad0eff9SFinn Thain 	hostdata->work_q = alloc_workqueue("ncr5380_%d",
6390ad0eff9SFinn Thain 	                        WQ_UNBOUND | WQ_MEM_RECLAIM,
6400ad0eff9SFinn Thain 	                        1, instance->host_no);
6410ad0eff9SFinn Thain 	if (!hostdata->work_q)
6420ad0eff9SFinn Thain 		return -ENOMEM;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	hostdata->host = instance;
6451da177e4SLinus Torvalds 
6468c32513bSFinn Thain 	prepare_info(instance);
6478c32513bSFinn Thain 
6481da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
6491da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
6501da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, 0);
6511da177e4SLinus Torvalds 	NCR5380_write(SELECT_ENABLE_REG, 0);
6522f854b82SFinn Thain 
6532f854b82SFinn Thain 	/* Calibrate register polling loop */
6542f854b82SFinn Thain 	i = 0;
6552f854b82SFinn Thain 	deadline = jiffies + 1;
6562f854b82SFinn Thain 	do {
6572f854b82SFinn Thain 		cpu_relax();
6582f854b82SFinn Thain 	} while (time_is_after_jiffies(deadline));
6592f854b82SFinn Thain 	deadline += msecs_to_jiffies(256);
6602f854b82SFinn Thain 	do {
6612f854b82SFinn Thain 		NCR5380_read(STATUS_REG);
6622f854b82SFinn Thain 		++i;
6632f854b82SFinn Thain 		cpu_relax();
6642f854b82SFinn Thain 	} while (time_is_after_jiffies(deadline));
6652f854b82SFinn Thain 	hostdata->accesses_per_ms = i / 256;
6662f854b82SFinn Thain 
667b6488f97SFinn Thain 	return 0;
668b6488f97SFinn Thain }
6691da177e4SLinus Torvalds 
670b6488f97SFinn Thain /**
671b6488f97SFinn Thain  * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
672b6488f97SFinn Thain  * @instance: adapter to check
6731da177e4SLinus Torvalds  *
674b6488f97SFinn Thain  * If the system crashed, it may have crashed with a connected target and
675b6488f97SFinn Thain  * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
676b6488f97SFinn Thain  * currently established nexus, which we know nothing about. Failing that
677b6488f97SFinn Thain  * do a bus reset.
6781da177e4SLinus Torvalds  *
679b6488f97SFinn Thain  * Note that a bus reset will cause the chip to assert IRQ.
680b6488f97SFinn Thain  *
681b6488f97SFinn Thain  * Returns 0 if successful, otherwise -ENXIO.
6821da177e4SLinus Torvalds  */
6831da177e4SLinus Torvalds 
684b6488f97SFinn Thain static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
685b6488f97SFinn Thain {
6869c3f0e2bSFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
687b6488f97SFinn Thain 	int pass;
688b6488f97SFinn Thain 
6891da177e4SLinus Torvalds 	for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
6901da177e4SLinus Torvalds 		switch (pass) {
6911da177e4SLinus Torvalds 		case 1:
6921da177e4SLinus Torvalds 		case 3:
6931da177e4SLinus Torvalds 		case 5:
694636b1ec8SFinn Thain 			shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
695636b1ec8SFinn Thain 			NCR5380_poll_politely(instance,
696636b1ec8SFinn Thain 			                      STATUS_REG, SR_BSY, 0, 5 * HZ);
6971da177e4SLinus Torvalds 			break;
6981da177e4SLinus Torvalds 		case 2:
699636b1ec8SFinn Thain 			shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
7001da177e4SLinus Torvalds 			do_abort(instance);
7011da177e4SLinus Torvalds 			break;
7021da177e4SLinus Torvalds 		case 4:
703636b1ec8SFinn Thain 			shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
7041da177e4SLinus Torvalds 			do_reset(instance);
7059c3f0e2bSFinn Thain 			/* Wait after a reset; the SCSI standard calls for
7069c3f0e2bSFinn Thain 			 * 250ms, we wait 500ms to be on the safe side.
7079c3f0e2bSFinn Thain 			 * But some Toshiba CD-ROMs need ten times that.
7089c3f0e2bSFinn Thain 			 */
7099c3f0e2bSFinn Thain 			if (hostdata->flags & FLAG_TOSHIBA_DELAY)
7109c3f0e2bSFinn Thain 				msleep(2500);
7119c3f0e2bSFinn Thain 			else
7129c3f0e2bSFinn Thain 				msleep(500);
7131da177e4SLinus Torvalds 			break;
7141da177e4SLinus Torvalds 		case 6:
715636b1ec8SFinn Thain 			shost_printk(KERN_ERR, instance, "bus locked solid\n");
7161da177e4SLinus Torvalds 			return -ENXIO;
7171da177e4SLinus Torvalds 		}
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 	return 0;
7201da177e4SLinus Torvalds }
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds /**
7231da177e4SLinus Torvalds  *	NCR5380_exit	-	remove an NCR5380
7241da177e4SLinus Torvalds  *	@instance: adapter to remove
7251da177e4SLinus Torvalds  */
7261da177e4SLinus Torvalds 
727a43cf0f3SRandy Dunlap static void NCR5380_exit(struct Scsi_Host *instance)
7281da177e4SLinus Torvalds {
729*e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
7301da177e4SLinus Torvalds 
7318d8601a7SFinn Thain 	cancel_work_sync(&hostdata->main_task);
7320ad0eff9SFinn Thain 	destroy_workqueue(hostdata->work_q);
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds /**
7361da177e4SLinus Torvalds  * NCR5380_queue_command - queue a command
7371bb40589SFinn Thain  * @instance: the relevant SCSI adapter
7381da177e4SLinus Torvalds  * @cmd: SCSI command
7391da177e4SLinus Torvalds  *
7401bb40589SFinn Thain  * cmd is added to the per-instance issue queue, with minor
7411da177e4SLinus Torvalds  * twiddling done to the host specific fields of cmd.  If the
7421da177e4SLinus Torvalds  * main coroutine is not running, it is restarted.
7431da177e4SLinus Torvalds  */
7441da177e4SLinus Torvalds 
7451bb40589SFinn Thain static int NCR5380_queue_command(struct Scsi_Host *instance,
7461bb40589SFinn Thain                                  struct scsi_cmnd *cmd)
7471da177e4SLinus Torvalds {
7481bb40589SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
749710ddd0dSFinn Thain 	struct scsi_cmnd *tmp;
7501bb40589SFinn Thain 	unsigned long flags;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds #if (NDEBUG & NDEBUG_NO_WRITE)
7531da177e4SLinus Torvalds 	switch (cmd->cmnd[0]) {
7541da177e4SLinus Torvalds 	case WRITE_6:
7551da177e4SLinus Torvalds 	case WRITE_10:
756dbb6b350SFinn Thain 		shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
7571da177e4SLinus Torvalds 		cmd->result = (DID_ERROR << 16);
7581bb40589SFinn Thain 		cmd->scsi_done(cmd);
7591da177e4SLinus Torvalds 		return 0;
7601da177e4SLinus Torvalds 	}
7611da177e4SLinus Torvalds #endif				/* (NDEBUG & NDEBUG_NO_WRITE) */
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	/*
7641da177e4SLinus Torvalds 	 * We use the host_scribble field as a pointer to the next command
7651da177e4SLinus Torvalds 	 * in a queue
7661da177e4SLinus Torvalds 	 */
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	cmd->host_scribble = NULL;
7691da177e4SLinus Torvalds 	cmd->result = 0;
7701da177e4SLinus Torvalds 
77111d2f63bSFinn Thain 	spin_lock_irqsave(&hostdata->lock, flags);
7721bb40589SFinn Thain 
7731da177e4SLinus Torvalds 	/*
7741da177e4SLinus Torvalds 	 * Insert the cmd into the issue queue. Note that REQUEST SENSE
7751da177e4SLinus Torvalds 	 * commands are added to the head of the queue since any command will
7761da177e4SLinus Torvalds 	 * clear the contingent allegiance condition that exists and the
7771da177e4SLinus Torvalds 	 * sense data is only guaranteed to be valid while the condition exists.
7781da177e4SLinus Torvalds 	 */
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
7811da177e4SLinus Torvalds 		LIST(cmd, hostdata->issue_queue);
7821da177e4SLinus Torvalds 		cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
7831da177e4SLinus Torvalds 		hostdata->issue_queue = cmd;
7841da177e4SLinus Torvalds 	} else {
785710ddd0dSFinn Thain 		for (tmp = (struct scsi_cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (struct scsi_cmnd *) tmp->host_scribble);
7861da177e4SLinus Torvalds 		LIST(cmd, tmp);
7871da177e4SLinus Torvalds 		tmp->host_scribble = (unsigned char *) cmd;
7881da177e4SLinus Torvalds 	}
78911d2f63bSFinn Thain 	spin_unlock_irqrestore(&hostdata->lock, flags);
7901bb40589SFinn Thain 
791dbb6b350SFinn Thain 	dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
792dbb6b350SFinn Thain 	         cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	/* Kick off command processing */
7958d8601a7SFinn Thain 	queue_work(hostdata->work_q, &hostdata->main_task);
7961da177e4SLinus Torvalds 	return 0;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds /**
8001da177e4SLinus Torvalds  *	NCR5380_main	-	NCR state machines
8011da177e4SLinus Torvalds  *
8021da177e4SLinus Torvalds  *	NCR5380_main is a coroutine that runs as long as more work can
8031da177e4SLinus Torvalds  *      be done on the NCR5380 host adapters in a system.  Both
8041da177e4SLinus Torvalds  *      NCR5380_queue_command() and NCR5380_intr() will try to start it
8051da177e4SLinus Torvalds  *      in case it is not running.
8061da177e4SLinus Torvalds  *
8071da177e4SLinus Torvalds  *	Locks: called as its own thread with no locks held. Takes the
8081da177e4SLinus Torvalds  *	host lock and called routines may take the isa dma lock.
8091da177e4SLinus Torvalds  */
8101da177e4SLinus Torvalds 
811c4028958SDavid Howells static void NCR5380_main(struct work_struct *work)
8121da177e4SLinus Torvalds {
813c4028958SDavid Howells 	struct NCR5380_hostdata *hostdata =
8148d8601a7SFinn Thain 		container_of(work, struct NCR5380_hostdata, main_task);
8151da177e4SLinus Torvalds 	struct Scsi_Host *instance = hostdata->host;
816710ddd0dSFinn Thain 	struct scsi_cmnd *tmp, *prev;
8171da177e4SLinus Torvalds 	int done;
8181da177e4SLinus Torvalds 
81911d2f63bSFinn Thain 	spin_lock_irq(&hostdata->lock);
8201da177e4SLinus Torvalds 	do {
8211da177e4SLinus Torvalds 		done = 1;
82211d2f63bSFinn Thain 
823ae753a33SFinn Thain 		if (!hostdata->connected) {
82452a6a1cbSFinn Thain 			dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
8251da177e4SLinus Torvalds 			/*
8261da177e4SLinus Torvalds 			 * Search through the issue_queue for a command destined
8271da177e4SLinus Torvalds 			 * for a target that's not busy.
8281da177e4SLinus Torvalds 			 */
829710ddd0dSFinn Thain 			for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble)
8301da177e4SLinus Torvalds 			{
8311da177e4SLinus Torvalds 				if (prev != tmp)
8329cb78c16SHannes Reinecke 				    dprintk(NDEBUG_LISTS, "MAIN tmp=%p   target=%d   busy=%d lun=%llu\n", tmp, tmp->device->id, hostdata->busy[tmp->device->id], tmp->device->lun);
8331da177e4SLinus Torvalds 				/*  When we find one, remove it from the issue queue. */
8349cb78c16SHannes Reinecke 				if (!(hostdata->busy[tmp->device->id] &
8359cb78c16SHannes Reinecke 				      (1 << (u8)(tmp->device->lun & 0xff)))) {
8361da177e4SLinus Torvalds 					if (prev) {
8371da177e4SLinus Torvalds 						REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
8381da177e4SLinus Torvalds 						prev->host_scribble = tmp->host_scribble;
8391da177e4SLinus Torvalds 					} else {
8401da177e4SLinus Torvalds 						REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
841710ddd0dSFinn Thain 						hostdata->issue_queue = (struct scsi_cmnd *) tmp->host_scribble;
8421da177e4SLinus Torvalds 					}
8431da177e4SLinus Torvalds 					tmp->host_scribble = NULL;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 					/*
8461da177e4SLinus Torvalds 					 * Attempt to establish an I_T_L nexus here.
8471da177e4SLinus Torvalds 					 * On success, instance->hostdata->connected is set.
8481da177e4SLinus Torvalds 					 * On failure, we must add the command back to the
8491da177e4SLinus Torvalds 					 *   issue queue so we can keep trying.
8501da177e4SLinus Torvalds 					 */
8519cb78c16SHannes Reinecke 					dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main() : command for target %d lun %llu removed from issue_queue\n", instance->host_no, tmp->device->id, tmp->device->lun);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 					/*
8541da177e4SLinus Torvalds 					 * REQUEST SENSE commands are issued without tagged
8551da177e4SLinus Torvalds 					 * queueing, even on SCSI-II devices because the
8561da177e4SLinus Torvalds 					 * contingent allegiance condition exists for the
8571da177e4SLinus Torvalds 					 * entire unit.
8581da177e4SLinus Torvalds 					 */
85976f13b93SFinn Thain 
86076f13b93SFinn Thain 					if (!NCR5380_select(instance, tmp)) {
8611f1b0c74SFinn Thain 						/* OK or bad target */
8621da177e4SLinus Torvalds 					} else {
8631f1b0c74SFinn Thain 						/* Need to retry */
8641da177e4SLinus Torvalds 						LIST(tmp, hostdata->issue_queue);
8651da177e4SLinus Torvalds 						tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
8661da177e4SLinus Torvalds 						hostdata->issue_queue = tmp;
8671da177e4SLinus Torvalds 						done = 0;
86852a6a1cbSFinn Thain 						dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no);
8691da177e4SLinus Torvalds 					}
870ae753a33SFinn Thain 					if (hostdata->connected)
8711f1b0c74SFinn Thain 						break;
8721da177e4SLinus Torvalds 				}	/* if target/lun is not busy */
8731da177e4SLinus Torvalds 			}	/* for */
8741da177e4SLinus Torvalds 		}	/* if (!hostdata->connected) */
87511d2f63bSFinn Thain 
8761da177e4SLinus Torvalds 		if (hostdata->connected
8771da177e4SLinus Torvalds #ifdef REAL_DMA
8781da177e4SLinus Torvalds 		    && !hostdata->dmalen
8791da177e4SLinus Torvalds #endif
8801da177e4SLinus Torvalds 		    ) {
88152a6a1cbSFinn Thain 			dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
8821da177e4SLinus Torvalds 			NCR5380_information_transfer(instance);
88352a6a1cbSFinn Thain 			dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
8841da177e4SLinus Torvalds 			done = 0;
8851d3db59dSFinn Thain 		}
8861da177e4SLinus Torvalds 	} while (!done);
88711d2f63bSFinn Thain 	spin_unlock_irq(&hostdata->lock);
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds #ifndef DONT_USE_INTR
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds /**
8931da177e4SLinus Torvalds  * NCR5380_intr - generic NCR5380 irq handler
8941da177e4SLinus Torvalds  * @irq: interrupt number
8951da177e4SLinus Torvalds  * @dev_id: device info
8961da177e4SLinus Torvalds  *
8971da177e4SLinus Torvalds  * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
8981da177e4SLinus Torvalds  * from the disconnected queue, and restarting NCR5380_main()
8991da177e4SLinus Torvalds  * as required.
9001da177e4SLinus Torvalds  *
901cd400825SFinn Thain  * The chip can assert IRQ in any of six different conditions. The IRQ flag
902cd400825SFinn Thain  * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
903cd400825SFinn Thain  * Three of these six conditions are latched in the Bus and Status Register:
904cd400825SFinn Thain  * - End of DMA (cleared by ending DMA Mode)
905cd400825SFinn Thain  * - Parity error (cleared by reading RPIR)
906cd400825SFinn Thain  * - Loss of BSY (cleared by reading RPIR)
907cd400825SFinn Thain  * Two conditions have flag bits that are not latched:
908cd400825SFinn Thain  * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
909cd400825SFinn Thain  * - Bus reset (non-maskable)
910cd400825SFinn Thain  * The remaining condition has no flag bit at all:
911cd400825SFinn Thain  * - Selection/reselection
912cd400825SFinn Thain  *
913cd400825SFinn Thain  * Hence, establishing the cause(s) of any interrupt is partly guesswork.
914cd400825SFinn Thain  * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
915cd400825SFinn Thain  * claimed that "the design of the [DP8490] interrupt logic ensures
916cd400825SFinn Thain  * interrupts will not be lost (they can be on the DP5380)."
917cd400825SFinn Thain  * The L5380/53C80 datasheet from LOGIC Devices has more details.
918cd400825SFinn Thain  *
919cd400825SFinn Thain  * Checking for bus reset by reading RST is futile because of interrupt
920cd400825SFinn Thain  * latency, but a bus reset will reset chip logic. Checking for parity error
921cd400825SFinn Thain  * is unnecessary because that interrupt is never enabled. A Loss of BSY
922cd400825SFinn Thain  * condition will clear DMA Mode. We can tell when this occurs because the
923cd400825SFinn Thain  * the Busy Monitor interrupt is enabled together with DMA Mode.
9241da177e4SLinus Torvalds  */
9251da177e4SLinus Torvalds 
926cd400825SFinn Thain static irqreturn_t NCR5380_intr(int irq, void *dev_id)
9271da177e4SLinus Torvalds {
928baa9aac6SJeff Garzik 	struct Scsi_Host *instance = dev_id;
929cd400825SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
930cd400825SFinn Thain 	int handled = 0;
9311da177e4SLinus Torvalds 	unsigned char basr;
9321da177e4SLinus Torvalds 	unsigned long flags;
9331da177e4SLinus Torvalds 
93411d2f63bSFinn Thain 	spin_lock_irqsave(&hostdata->lock, flags);
935cd400825SFinn Thain 
9361da177e4SLinus Torvalds 	basr = NCR5380_read(BUS_AND_STATUS_REG);
9371da177e4SLinus Torvalds 	if (basr & BASR_IRQ) {
938cd400825SFinn Thain 		unsigned char mr = NCR5380_read(MODE_REG);
939cd400825SFinn Thain 		unsigned char sr = NCR5380_read(STATUS_REG);
940cd400825SFinn Thain 
941cd400825SFinn Thain 		dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
942cd400825SFinn Thain 		        instance->host_no, irq, basr, sr, mr);
943cd400825SFinn Thain 
9441da177e4SLinus Torvalds #if defined(REAL_DMA)
945cd400825SFinn Thain 		if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
946cd400825SFinn Thain 			/* Probably End of DMA, Phase Mismatch or Loss of BSY.
947cd400825SFinn Thain 			 * We ack IRQ after clearing Mode Register. Workarounds
948cd400825SFinn Thain 			 * for End of DMA errata need to happen in DMA Mode.
9491da177e4SLinus Torvalds 			 */
9501da177e4SLinus Torvalds 
951cd400825SFinn Thain 			dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", intance->host_no);
952cd400825SFinn Thain 
95325985edcSLucas De Marchi 			int transferred;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 			if (!hostdata->connected)
956cd400825SFinn Thain 				panic("scsi%d : DMA interrupt with no connected cmd\n",
957cd400825SFinn Thain 				      instance->hostno);
9581da177e4SLinus Torvalds 
959cd400825SFinn Thain 			transferred = hostdata->dmalen - NCR5380_dma_residual(instance);
9601da177e4SLinus Torvalds 			hostdata->connected->SCp.this_residual -= transferred;
9611da177e4SLinus Torvalds 			hostdata->connected->SCp.ptr += transferred;
9621da177e4SLinus Torvalds 			hostdata->dmalen = 0;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 			/* FIXME: we need to poll briefly then defer a workqueue task ! */
9651da177e4SLinus Torvalds 			NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2 * HZ);
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 			NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
968cd400825SFinn Thain 			NCR5380_write(MODE_REG, MR_BASE);
969cd400825SFinn Thain 			NCR5380_read(RESET_PARITY_INTERRUPT_REG);
970cd400825SFinn Thain 		} else
971cd400825SFinn Thain #endif /* REAL_DMA */
972cd400825SFinn Thain 		if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
973cd400825SFinn Thain 		    (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
974cd400825SFinn Thain 			/* Probably reselected */
975cd400825SFinn Thain 			NCR5380_write(SELECT_ENABLE_REG, 0);
976cd400825SFinn Thain 			NCR5380_read(RESET_PARITY_INTERRUPT_REG);
977cd400825SFinn Thain 
978cd400825SFinn Thain 			dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
979cd400825SFinn Thain 			        instance->host_no);
980cd400825SFinn Thain 
981cd400825SFinn Thain 			if (!hostdata->connected) {
982cd400825SFinn Thain 				NCR5380_reselect(instance);
9838d8601a7SFinn Thain 				queue_work(hostdata->work_q, &hostdata->main_task);
984cd400825SFinn Thain 			}
985cd400825SFinn Thain 			if (!hostdata->connected)
986cd400825SFinn Thain 				NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
987cd400825SFinn Thain 		} else {
988cd400825SFinn Thain 			/* Probably Bus Reset */
989cd400825SFinn Thain 			NCR5380_read(RESET_PARITY_INTERRUPT_REG);
990cd400825SFinn Thain 
991cd400825SFinn Thain 			dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", instance->host_no);
992cd400825SFinn Thain 		}
993cd400825SFinn Thain 		handled = 1;
994cd400825SFinn Thain 	} else {
995cd400825SFinn Thain 		shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
996cd400825SFinn Thain 	}
997cd400825SFinn Thain 
99811d2f63bSFinn Thain 	spin_unlock_irqrestore(&hostdata->lock, flags);
999cd400825SFinn Thain 
1000cd400825SFinn Thain 	return IRQ_RETVAL(handled);
10011da177e4SLinus Torvalds }
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds #endif
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds /*
1006710ddd0dSFinn Thain  * Function : int NCR5380_select(struct Scsi_Host *instance,
1007710ddd0dSFinn Thain  *                               struct scsi_cmnd *cmd)
10081da177e4SLinus Torvalds  *
10091da177e4SLinus Torvalds  * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
10101da177e4SLinus Torvalds  *      including ARBITRATION, SELECTION, and initial message out for
10111da177e4SLinus Torvalds  *      IDENTIFY and queue messages.
10121da177e4SLinus Torvalds  *
10131da177e4SLinus Torvalds  * Inputs : instance - instantiation of the 5380 driver on which this
101476f13b93SFinn Thain  *      target lives, cmd - SCSI command to execute.
10151da177e4SLinus Torvalds  *
10166323876fSFinn Thain  * Returns : -1 if selection failed but should be retried.
10176323876fSFinn Thain  *      0 if selection failed and should not be retried.
10186323876fSFinn Thain  *      0 if selection succeeded completely (hostdata->connected == cmd).
10191da177e4SLinus Torvalds  *
10201da177e4SLinus Torvalds  * Side effects :
10211da177e4SLinus Torvalds  *      If bus busy, arbitration failed, etc, NCR5380_select() will exit
10221da177e4SLinus Torvalds  *              with registers as they should have been on entry - ie
10231da177e4SLinus Torvalds  *              SELECT_ENABLE will be set appropriately, the NCR5380
10241da177e4SLinus Torvalds  *              will cease to drive any SCSI bus signals.
10251da177e4SLinus Torvalds  *
10261da177e4SLinus Torvalds  *      If successful : I_T_L or I_T_L_Q nexus will be established,
10271da177e4SLinus Torvalds  *              instance->connected will be set to cmd.
10281da177e4SLinus Torvalds  *              SELECT interrupt will be disabled.
10291da177e4SLinus Torvalds  *
10301da177e4SLinus Torvalds  *      If failed (no target) : cmd->scsi_done() will be called, and the
10311da177e4SLinus Torvalds  *              cmd->result host byte set to DID_BAD_TARGET.
10321da177e4SLinus Torvalds  *
10331da177e4SLinus Torvalds  *	Locks: caller holds hostdata lock in IRQ mode
10341da177e4SLinus Torvalds  */
10351da177e4SLinus Torvalds 
1036710ddd0dSFinn Thain static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
10371da177e4SLinus Torvalds {
1038*e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
10391da177e4SLinus Torvalds 	unsigned char tmp[3], phase;
10401da177e4SLinus Torvalds 	unsigned char *data;
10411da177e4SLinus Torvalds 	int len;
10421da177e4SLinus Torvalds 	int err;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	NCR5380_dprint(NDEBUG_ARBITRATION, instance);
104552a6a1cbSFinn Thain 	dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	/*
10481da177e4SLinus Torvalds 	 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
10491da177e4SLinus Torvalds 	 * data bus during SELECTION.
10501da177e4SLinus Torvalds 	 */
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, 0);
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	/*
10551da177e4SLinus Torvalds 	 * Start arbitration.
10561da177e4SLinus Torvalds 	 */
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
10591da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_ARBITRATE);
10601da177e4SLinus Torvalds 
106155500d9bSFinn Thain 	/* The chip now waits for BUS FREE phase. Then after the 800 ns
106255500d9bSFinn Thain 	 * Bus Free Delay, arbitration will begin.
10631da177e4SLinus Torvalds 	 */
10641da177e4SLinus Torvalds 
106511d2f63bSFinn Thain 	spin_unlock_irq(&hostdata->lock);
1066b32ade12SFinn Thain 	err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1067b32ade12SFinn Thain 	                INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1068b32ade12SFinn Thain 	                                       ICR_ARBITRATION_PROGRESS, HZ);
106911d2f63bSFinn Thain 	spin_lock_irq(&hostdata->lock);
107055500d9bSFinn Thain 	if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
107155500d9bSFinn Thain 		/* Reselection interrupt */
107255500d9bSFinn Thain 		return -1;
107355500d9bSFinn Thain 	}
1074b32ade12SFinn Thain 	if (err < 0) {
1075b32ade12SFinn Thain 		NCR5380_write(MODE_REG, MR_BASE);
1076b32ade12SFinn Thain 		shost_printk(KERN_ERR, instance,
1077b32ade12SFinn Thain 		             "select: arbitration timeout\n");
1078b32ade12SFinn Thain 		return -1;
107955500d9bSFinn Thain 	}
108011d2f63bSFinn Thain 	spin_unlock_irq(&hostdata->lock);
108155500d9bSFinn Thain 
108255500d9bSFinn Thain 	/* The SCSI-2 arbitration delay is 2.4 us */
10831da177e4SLinus Torvalds 	udelay(3);
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	/* Check for lost arbitration */
10861da177e4SLinus 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)) {
10871da177e4SLinus Torvalds 		NCR5380_write(MODE_REG, MR_BASE);
108852a6a1cbSFinn Thain 		dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
108911d2f63bSFinn Thain 		spin_lock_irq(&hostdata->lock);
10906323876fSFinn Thain 		return -1;
10911da177e4SLinus Torvalds 	}
1092cf13b083SFinn Thain 
1093cf13b083SFinn Thain 	/* After/during arbitration, BSY should be asserted.
1094cf13b083SFinn Thain 	 * IBM DPES-31080 Version S31Q works now
1095cf13b083SFinn Thain 	 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1096cf13b083SFinn Thain 	 */
1097cf13b083SFinn Thain 	NCR5380_write(INITIATOR_COMMAND_REG,
1098cf13b083SFinn Thain 		      ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	/*
11011da177e4SLinus Torvalds 	 * Again, bus clear + bus settle time is 1.2us, however, this is
11021da177e4SLinus Torvalds 	 * a minimum so we'll udelay ceil(1.2)
11031da177e4SLinus Torvalds 	 */
11041da177e4SLinus Torvalds 
11059c3f0e2bSFinn Thain 	if (hostdata->flags & FLAG_TOSHIBA_DELAY)
11069c3f0e2bSFinn Thain 		udelay(15);
11079c3f0e2bSFinn Thain 	else
11081da177e4SLinus Torvalds 		udelay(2);
11091da177e4SLinus Torvalds 
111011d2f63bSFinn Thain 	spin_lock_irq(&hostdata->lock);
111111d2f63bSFinn Thain 
111272064a78SFinn Thain 	/* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
111372064a78SFinn Thain 	if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
111472064a78SFinn Thain 		return -1;
111572064a78SFinn Thain 
111652a6a1cbSFinn Thain 	dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	/*
11191da177e4SLinus Torvalds 	 * Now that we have won arbitration, start Selection process, asserting
11201da177e4SLinus Torvalds 	 * the host and target ID's on the SCSI bus.
11211da177e4SLinus Torvalds 	 */
11221da177e4SLinus Torvalds 
1123422c0d61SJeff Garzik 	NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	/*
11261da177e4SLinus Torvalds 	 * Raise ATN while SEL is true before BSY goes false from arbitration,
11271da177e4SLinus Torvalds 	 * since this is the only way to guarantee that we'll get a MESSAGE OUT
11281da177e4SLinus Torvalds 	 * phase immediately after selection.
11291da177e4SLinus Torvalds 	 */
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
11321da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	/*
11351da177e4SLinus Torvalds 	 * Reselect interrupts must be turned off prior to the dropping of BSY,
11361da177e4SLinus Torvalds 	 * otherwise we will trigger an interrupt.
11371da177e4SLinus Torvalds 	 */
11381da177e4SLinus Torvalds 	NCR5380_write(SELECT_ENABLE_REG, 0);
11391da177e4SLinus Torvalds 
114011d2f63bSFinn Thain 	spin_unlock_irq(&hostdata->lock);
114111d2f63bSFinn Thain 
11421da177e4SLinus Torvalds 	/*
11431da177e4SLinus Torvalds 	 * The initiator shall then wait at least two deskew delays and release
11441da177e4SLinus Torvalds 	 * the BSY signal.
11451da177e4SLinus Torvalds 	 */
11461da177e4SLinus Torvalds 	udelay(1);		/* wingel -- wait two bus deskew delay >2*45ns */
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	/* Reset BSY */
11491da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	/*
11521da177e4SLinus Torvalds 	 * Something weird happens when we cease to drive BSY - looks
11531da177e4SLinus Torvalds 	 * like the board/chip is letting us do another read before the
11541da177e4SLinus Torvalds 	 * appropriate propagation delay has expired, and we're confusing
11551da177e4SLinus Torvalds 	 * a BSY signal from ourselves as the target's response to SELECTION.
11561da177e4SLinus Torvalds 	 *
11571da177e4SLinus Torvalds 	 * A small delay (the 'C++' frontend breaks the pipeline with an
11581da177e4SLinus Torvalds 	 * unnecessary jump, making it work on my 386-33/Trantor T128, the
11591da177e4SLinus Torvalds 	 * tighter 'C' code breaks and requires this) solves the problem -
11601da177e4SLinus Torvalds 	 * the 1 us delay is arbitrary, and only used because this delay will
11611da177e4SLinus Torvalds 	 * be the same on other platforms and since it works here, it should
11621da177e4SLinus Torvalds 	 * work there.
11631da177e4SLinus Torvalds 	 *
11641da177e4SLinus Torvalds 	 * wingel suggests that this could be due to failing to wait
11651da177e4SLinus Torvalds 	 * one deskew delay.
11661da177e4SLinus Torvalds 	 */
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	udelay(1);
11691da177e4SLinus Torvalds 
117052a6a1cbSFinn Thain 	dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	/*
11731da177e4SLinus Torvalds 	 * The SCSI specification calls for a 250 ms timeout for the actual
11741da177e4SLinus Torvalds 	 * selection.
11751da177e4SLinus Torvalds 	 */
11761da177e4SLinus Torvalds 
1177ae753a33SFinn Thain 	err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1178ae753a33SFinn Thain 	                            msecs_to_jiffies(250));
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
118111d2f63bSFinn Thain 		spin_lock_irq(&hostdata->lock);
11821da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
11831da177e4SLinus Torvalds 		NCR5380_reselect(instance);
1184cd400825SFinn Thain 		if (!hostdata->connected)
11851da177e4SLinus Torvalds 			NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1186cd400825SFinn Thain 		printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
11871da177e4SLinus Torvalds 		return -1;
11881da177e4SLinus Torvalds 	}
1189ae753a33SFinn Thain 
1190ae753a33SFinn Thain 	if (err < 0) {
119111d2f63bSFinn Thain 		spin_lock_irq(&hostdata->lock);
1192ae753a33SFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1193ae753a33SFinn Thain 		cmd->result = DID_BAD_TARGET << 16;
1194ae753a33SFinn Thain 		cmd->scsi_done(cmd);
1195ae753a33SFinn Thain 		NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1196ae753a33SFinn Thain 		dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n",
1197ae753a33SFinn Thain 		        instance->host_no);
1198ae753a33SFinn Thain 		return 0;
1199ae753a33SFinn Thain 	}
1200ae753a33SFinn Thain 
12011da177e4SLinus Torvalds 	/*
12021da177e4SLinus Torvalds 	 * No less than two deskew delays after the initiator detects the
12031da177e4SLinus Torvalds 	 * BSY signal is true, it shall release the SEL signal and may
12041da177e4SLinus Torvalds 	 * change the DATA BUS.                                     -wingel
12051da177e4SLinus Torvalds 	 */
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	udelay(1);
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	/*
12121da177e4SLinus Torvalds 	 * Since we followed the SCSI spec, and raised ATN while SEL
12131da177e4SLinus Torvalds 	 * was true but before BSY was false during selection, the information
12141da177e4SLinus Torvalds 	 * transfer phase should be a MESSAGE OUT phase so that we can send the
12151da177e4SLinus Torvalds 	 * IDENTIFY message.
12161da177e4SLinus Torvalds 	 *
12171da177e4SLinus Torvalds 	 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
12181da177e4SLinus Torvalds 	 * message (2 bytes) with a tag ID that we increment with every command
12191da177e4SLinus Torvalds 	 * until it wraps back to 0.
12201da177e4SLinus Torvalds 	 *
12211da177e4SLinus Torvalds 	 * XXX - it turns out that there are some broken SCSI-II devices,
12221da177e4SLinus Torvalds 	 *       which claim to support tagged queuing but fail when more than
12231da177e4SLinus Torvalds 	 *       some number of commands are issued at once.
12241da177e4SLinus Torvalds 	 */
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	/* Wait for start of REQ/ACK handshake */
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
122911d2f63bSFinn Thain 	spin_lock_irq(&hostdata->lock);
12301cc160e1SFinn Thain 	if (err < 0) {
123155500d9bSFinn Thain 		shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
123255500d9bSFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
12331da177e4SLinus Torvalds 		NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
12346323876fSFinn Thain 		return -1;
12351da177e4SLinus Torvalds 	}
12361da177e4SLinus Torvalds 
123752a6a1cbSFinn Thain 	dprintk(NDEBUG_SELECTION, "scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id);
123822f5f10dSFinn Thain 	tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	len = 1;
12411da177e4SLinus Torvalds 	cmd->tag = 0;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	/* Send message(s) */
12441da177e4SLinus Torvalds 	data = tmp;
12451da177e4SLinus Torvalds 	phase = PHASE_MSGOUT;
12461da177e4SLinus Torvalds 	NCR5380_transfer_pio(instance, &phase, &len, &data);
124752a6a1cbSFinn Thain 	dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
12481da177e4SLinus Torvalds 	/* XXX need to handle errors here */
124911d2f63bSFinn Thain 
12501da177e4SLinus Torvalds 	hostdata->connected = cmd;
12519cb78c16SHannes Reinecke 	hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	initialize_SCp(cmd);
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	return 0;
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds /*
12591da177e4SLinus Torvalds  * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
12601da177e4SLinus Torvalds  *      unsigned char *phase, int *count, unsigned char **data)
12611da177e4SLinus Torvalds  *
12621da177e4SLinus Torvalds  * Purpose : transfers data in given phase using polled I/O
12631da177e4SLinus Torvalds  *
12641da177e4SLinus Torvalds  * Inputs : instance - instance of driver, *phase - pointer to
12651da177e4SLinus Torvalds  *      what phase is expected, *count - pointer to number of
12661da177e4SLinus Torvalds  *      bytes to transfer, **data - pointer to data pointer.
12671da177e4SLinus Torvalds  *
12681da177e4SLinus Torvalds  * Returns : -1 when different phase is entered without transferring
126925985edcSLucas De Marchi  *      maximum number of bytes, 0 if all bytes or transferred or exit
12701da177e4SLinus Torvalds  *      is in same phase.
12711da177e4SLinus Torvalds  *
12721da177e4SLinus Torvalds  *      Also, *phase, *count, *data are modified in place.
12731da177e4SLinus Torvalds  *
12741da177e4SLinus Torvalds  * XXX Note : handling for bus free may be useful.
12751da177e4SLinus Torvalds  */
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds /*
12781da177e4SLinus Torvalds  * Note : this code is not as quick as it could be, however it
12791da177e4SLinus Torvalds  * IS 100% reliable, and for the actual data transfer where speed
12801da177e4SLinus Torvalds  * counts, we will always do a pseudo DMA or DMA transfer.
12811da177e4SLinus Torvalds  */
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
12841da177e4SLinus Torvalds 	unsigned char p = *phase, tmp;
12851da177e4SLinus Torvalds 	int c = *count;
12861da177e4SLinus Torvalds 	unsigned char *d = *data;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	if (!(p & SR_IO))
128952a6a1cbSFinn Thain 		dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
12901da177e4SLinus Torvalds 	else
129152a6a1cbSFinn Thain 		dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	/*
12941da177e4SLinus Torvalds 	 * The NCR5380 chip will only drive the SCSI bus when the
12951da177e4SLinus Torvalds 	 * phase specified in the appropriate bits of the TARGET COMMAND
12961da177e4SLinus Torvalds 	 * REGISTER match the STATUS REGISTER
12971da177e4SLinus Torvalds 	 */
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	do {
13021da177e4SLinus Torvalds 		/*
13031da177e4SLinus Torvalds 		 * Wait for assertion of REQ, after which the phase bits will be
13041da177e4SLinus Torvalds 		 * valid
13051da177e4SLinus Torvalds 		 */
13061da177e4SLinus Torvalds 
1307686f3990SFinn Thain 		if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
13081da177e4SLinus Torvalds 			break;
13091da177e4SLinus Torvalds 
131052a6a1cbSFinn Thain 		dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 		/* Check for phase mismatch */
1313686f3990SFinn Thain 		if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
131452a6a1cbSFinn Thain 			dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
13151da177e4SLinus Torvalds 			NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
13161da177e4SLinus Torvalds 			break;
13171da177e4SLinus Torvalds 		}
13181da177e4SLinus Torvalds 		/* Do actual transfer from SCSI bus to / from memory */
13191da177e4SLinus Torvalds 		if (!(p & SR_IO))
13201da177e4SLinus Torvalds 			NCR5380_write(OUTPUT_DATA_REG, *d);
13211da177e4SLinus Torvalds 		else
13221da177e4SLinus Torvalds 			*d = NCR5380_read(CURRENT_SCSI_DATA_REG);
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 		++d;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 		/*
13271da177e4SLinus Torvalds 		 * The SCSI standard suggests that in MSGOUT phase, the initiator
13281da177e4SLinus Torvalds 		 * should drop ATN on the last byte of the message phase
13291da177e4SLinus Torvalds 		 * after REQ has been asserted for the handshake but before
13301da177e4SLinus Torvalds 		 * the initiator raises ACK.
13311da177e4SLinus Torvalds 		 */
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 		if (!(p & SR_IO)) {
13341da177e4SLinus Torvalds 			if (!((p & SR_MSG) && c > 1)) {
13351da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
13361da177e4SLinus Torvalds 				NCR5380_dprint(NDEBUG_PIO, instance);
13371da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
13381da177e4SLinus Torvalds 			} else {
13391da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
13401da177e4SLinus Torvalds 				NCR5380_dprint(NDEBUG_PIO, instance);
13411da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
13421da177e4SLinus Torvalds 			}
13431da177e4SLinus Torvalds 		} else {
13441da177e4SLinus Torvalds 			NCR5380_dprint(NDEBUG_PIO, instance);
13451da177e4SLinus Torvalds 			NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
13461da177e4SLinus Torvalds 		}
13471da177e4SLinus Torvalds 
1348a2edc4a6SFinn Thain 		if (NCR5380_poll_politely(instance,
1349a2edc4a6SFinn Thain 		                          STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1350a2edc4a6SFinn Thain 			break;
1351a2edc4a6SFinn Thain 
135252a6a1cbSFinn Thain 		dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds /*
13551da177e4SLinus Torvalds  * We have several special cases to consider during REQ/ACK handshaking :
13561da177e4SLinus Torvalds  * 1.  We were in MSGOUT phase, and we are on the last byte of the
13571da177e4SLinus Torvalds  *      message.  ATN must be dropped as ACK is dropped.
13581da177e4SLinus Torvalds  *
13591da177e4SLinus Torvalds  * 2.  We are in a MSGIN phase, and we are on the last byte of the
13601da177e4SLinus Torvalds  *      message.  We must exit with ACK asserted, so that the calling
13611da177e4SLinus Torvalds  *      code may raise ATN before dropping ACK to reject the message.
13621da177e4SLinus Torvalds  *
13631da177e4SLinus Torvalds  * 3.  ACK and ATN are clear and the target may proceed as normal.
13641da177e4SLinus Torvalds  */
13651da177e4SLinus Torvalds 		if (!(p == PHASE_MSGIN && c == 1)) {
13661da177e4SLinus Torvalds 			if (p == PHASE_MSGOUT && c > 1)
13671da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
13681da177e4SLinus Torvalds 			else
13691da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
13701da177e4SLinus Torvalds 		}
13711da177e4SLinus Torvalds 	} while (--c);
13721da177e4SLinus Torvalds 
137352a6a1cbSFinn Thain 	dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 	*count = c;
13761da177e4SLinus Torvalds 	*data = d;
13771da177e4SLinus Torvalds 	tmp = NCR5380_read(STATUS_REG);
1378a2edc4a6SFinn Thain 	/* The phase read from the bus is valid if either REQ is (already)
1379a2edc4a6SFinn Thain 	 * asserted or if ACK hasn't been released yet. The latter applies if
1380a2edc4a6SFinn Thain 	 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1381a2edc4a6SFinn Thain 	 */
1382a2edc4a6SFinn Thain 	if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
13831da177e4SLinus Torvalds 		*phase = tmp & PHASE_MASK;
13841da177e4SLinus Torvalds 	else
13851da177e4SLinus Torvalds 		*phase = PHASE_UNKNOWN;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	if (!c || (*phase == p))
13881da177e4SLinus Torvalds 		return 0;
13891da177e4SLinus Torvalds 	else
13901da177e4SLinus Torvalds 		return -1;
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds /**
13941da177e4SLinus Torvalds  * do_reset - issue a reset command
1395636b1ec8SFinn Thain  * @instance: adapter to reset
13961da177e4SLinus Torvalds  *
13971da177e4SLinus Torvalds  * Issue a reset sequence to the NCR5380 and try and get the bus
13981da177e4SLinus Torvalds  * back into sane shape.
13991da177e4SLinus Torvalds  *
1400636b1ec8SFinn Thain  * This clears the reset interrupt flag because there may be no handler for
1401636b1ec8SFinn Thain  * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1402636b1ec8SFinn Thain  * been installed. And when in EH we may have released the ST DMA interrupt.
14031da177e4SLinus Torvalds  */
14041da177e4SLinus Torvalds 
140554d8fe44SFinn Thain static void do_reset(struct Scsi_Host *instance)
140654d8fe44SFinn Thain {
1407636b1ec8SFinn Thain 	unsigned long flags;
1408636b1ec8SFinn Thain 
1409636b1ec8SFinn Thain 	local_irq_save(flags);
1410636b1ec8SFinn Thain 	NCR5380_write(TARGET_COMMAND_REG,
1411636b1ec8SFinn Thain 	              PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
14121da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1413636b1ec8SFinn Thain 	udelay(50);
14141da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1415636b1ec8SFinn Thain 	(void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1416636b1ec8SFinn Thain 	local_irq_restore(flags);
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
141980d3eb6dSFinn Thain /**
142080d3eb6dSFinn Thain  * do_abort - abort the currently established nexus by going to
142180d3eb6dSFinn Thain  * MESSAGE OUT phase and sending an ABORT message.
142280d3eb6dSFinn Thain  * @instance: relevant scsi host instance
14231da177e4SLinus Torvalds  *
142480d3eb6dSFinn Thain  * Returns 0 on success, -1 on failure.
14251da177e4SLinus Torvalds  */
14261da177e4SLinus Torvalds 
142754d8fe44SFinn Thain static int do_abort(struct Scsi_Host *instance)
142854d8fe44SFinn Thain {
14291da177e4SLinus Torvalds 	unsigned char *msgptr, phase, tmp;
14301da177e4SLinus Torvalds 	int len;
14311da177e4SLinus Torvalds 	int rc;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	/* Request message out phase */
14341da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 	/*
14371da177e4SLinus Torvalds 	 * Wait for the target to indicate a valid phase by asserting
14381da177e4SLinus Torvalds 	 * REQ.  Once this happens, we'll have either a MSGOUT phase
14391da177e4SLinus Torvalds 	 * and can immediately send the ABORT message, or we'll have some
14401da177e4SLinus Torvalds 	 * other phase and will have to source/sink data.
14411da177e4SLinus Torvalds 	 *
14421da177e4SLinus Torvalds 	 * We really don't care what value was on the bus or what value
14431da177e4SLinus Torvalds 	 * the target sees, so we just handshake.
14441da177e4SLinus Torvalds 	 */
14451da177e4SLinus Torvalds 
144680d3eb6dSFinn Thain 	rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
14471da177e4SLinus Torvalds 	if (rc < 0)
144880d3eb6dSFinn Thain 		goto timeout;
14491da177e4SLinus Torvalds 
1450f35d3474SFinn Thain 	tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
14531da177e4SLinus Torvalds 
1454f35d3474SFinn Thain 	if (tmp != PHASE_MSGOUT) {
14551da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
145654d8fe44SFinn Thain 		rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
14571cc160e1SFinn Thain 		if (rc < 0)
145880d3eb6dSFinn Thain 			goto timeout;
145980d3eb6dSFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
14601da177e4SLinus Torvalds 	}
14611da177e4SLinus Torvalds 	tmp = ABORT;
14621da177e4SLinus Torvalds 	msgptr = &tmp;
14631da177e4SLinus Torvalds 	len = 1;
14641da177e4SLinus Torvalds 	phase = PHASE_MSGOUT;
146554d8fe44SFinn Thain 	NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	/*
14681da177e4SLinus Torvalds 	 * If we got here, and the command completed successfully,
14691da177e4SLinus Torvalds 	 * we're about to go into bus free state.
14701da177e4SLinus Torvalds 	 */
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 	return len ? -1 : 0;
147380d3eb6dSFinn Thain 
147480d3eb6dSFinn Thain timeout:
147580d3eb6dSFinn Thain 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
147680d3eb6dSFinn Thain 	return -1;
14771da177e4SLinus Torvalds }
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
14801da177e4SLinus Torvalds /*
14811da177e4SLinus Torvalds  * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
14821da177e4SLinus Torvalds  *      unsigned char *phase, int *count, unsigned char **data)
14831da177e4SLinus Torvalds  *
14841da177e4SLinus Torvalds  * Purpose : transfers data in given phase using either real
14851da177e4SLinus Torvalds  *      or pseudo DMA.
14861da177e4SLinus Torvalds  *
14871da177e4SLinus Torvalds  * Inputs : instance - instance of driver, *phase - pointer to
14881da177e4SLinus Torvalds  *      what phase is expected, *count - pointer to number of
14891da177e4SLinus Torvalds  *      bytes to transfer, **data - pointer to data pointer.
14901da177e4SLinus Torvalds  *
14911da177e4SLinus Torvalds  * Returns : -1 when different phase is entered without transferring
149225985edcSLucas De Marchi  *      maximum number of bytes, 0 if all bytes or transferred or exit
14931da177e4SLinus Torvalds  *      is in same phase.
14941da177e4SLinus Torvalds  *
14951da177e4SLinus Torvalds  *      Also, *phase, *count, *data are modified in place.
14961da177e4SLinus Torvalds  *
14971da177e4SLinus Torvalds  *	Locks: io_request lock held by caller
14981da177e4SLinus Torvalds  */
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
15021da177e4SLinus Torvalds 	register int c = *count;
15031da177e4SLinus Torvalds 	register unsigned char p = *phase;
15041da177e4SLinus Torvalds 	register unsigned char *d = *data;
15051da177e4SLinus Torvalds 	unsigned char tmp;
15061da177e4SLinus Torvalds 	int foo;
15071da177e4SLinus Torvalds #if defined(REAL_DMA_POLL)
15081da177e4SLinus Torvalds 	int cnt, toPIO;
15091da177e4SLinus Torvalds 	unsigned char saved_data = 0, overrun = 0, residue;
15101da177e4SLinus Torvalds #endif
15111da177e4SLinus Torvalds 
1512*e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 	if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
15151da177e4SLinus Torvalds 		*phase = tmp;
15161da177e4SLinus Torvalds 		return -1;
15171da177e4SLinus Torvalds 	}
15181da177e4SLinus Torvalds #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
15191da177e4SLinus Torvalds 	if (p & SR_IO) {
15209db6024eSFinn Thain 		if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS))
15211da177e4SLinus Torvalds 			c -= 2;
15221da177e4SLinus Torvalds 	}
152352a6a1cbSFinn Thain 	dprintk(NDEBUG_DMA, "scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n", instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" : "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
15241da177e4SLinus Torvalds 	hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
15251da177e4SLinus Torvalds #endif
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds #ifdef REAL_DMA
1530cd400825SFinn Thain 	NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1531cd400825SFinn Thain 	                        MR_ENABLE_EOP_INTR);
15321da177e4SLinus Torvalds #elif defined(REAL_DMA_POLL)
1533cd400825SFinn Thain 	NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
15341da177e4SLinus Torvalds #else
15351da177e4SLinus Torvalds 	/*
15361da177e4SLinus Torvalds 	 * Note : on my sample board, watch-dog timeouts occurred when interrupts
15371da177e4SLinus Torvalds 	 * were not disabled for the duration of a single DMA transfer, from
15381da177e4SLinus Torvalds 	 * before the setting of DMA mode to after transfer of the last byte.
15391da177e4SLinus Torvalds 	 */
15401da177e4SLinus Torvalds 
154155181be8SFinn Thain 	if (hostdata->flags & FLAG_NO_DMA_FIXUP)
1542cd400825SFinn Thain 		NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1543cd400825SFinn Thain 		                        MR_ENABLE_EOP_INTR);
15441da177e4SLinus Torvalds 	else
1545cd400825SFinn Thain 		NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
15461da177e4SLinus Torvalds #endif				/* def REAL_DMA */
15471da177e4SLinus Torvalds 
154852a6a1cbSFinn Thain 	dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	/*
15511da177e4SLinus Torvalds 	 *	On the PAS16 at least I/O recovery delays are not needed here.
15521da177e4SLinus Torvalds 	 *	Everyone else seems to want them.
15531da177e4SLinus Torvalds 	 */
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	if (p & SR_IO) {
15561da177e4SLinus Torvalds 		io_recovery_delay(1);
15571da177e4SLinus Torvalds 		NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
15581da177e4SLinus Torvalds 	} else {
15591da177e4SLinus Torvalds 		io_recovery_delay(1);
15601da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
15611da177e4SLinus Torvalds 		io_recovery_delay(1);
15621da177e4SLinus Torvalds 		NCR5380_write(START_DMA_SEND_REG, 0);
15631da177e4SLinus Torvalds 		io_recovery_delay(1);
15641da177e4SLinus Torvalds 	}
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds #if defined(REAL_DMA_POLL)
15671da177e4SLinus Torvalds 	do {
15681da177e4SLinus Torvalds 		tmp = NCR5380_read(BUS_AND_STATUS_REG);
15691da177e4SLinus Torvalds 	} while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds /*
15721da177e4SLinus Torvalds    At this point, either we've completed DMA, or we have a phase mismatch,
15731da177e4SLinus Torvalds    or we've unexpectedly lost BUSY (which is a real error).
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds    For write DMAs, we want to wait until the last byte has been
15761da177e4SLinus Torvalds    transferred out over the bus before we turn off DMA mode.  Alas, there
15771da177e4SLinus Torvalds    seems to be no terribly good way of doing this on a 5380 under all
15781da177e4SLinus Torvalds    conditions.  For non-scatter-gather operations, we can wait until REQ
15791da177e4SLinus Torvalds    and ACK both go false, or until a phase mismatch occurs.  Gather-writes
15801da177e4SLinus Torvalds    are nastier, since the device will be expecting more data than we
15811da177e4SLinus Torvalds    are prepared to send it, and REQ will remain asserted.  On a 53C8[01] we
15821da177e4SLinus Torvalds    could test LAST BIT SENT to assure transfer (I imagine this is precisely
15831da177e4SLinus Torvalds    why this signal was added to the newer chips) but on the older 538[01]
15841da177e4SLinus Torvalds    this signal does not exist.  The workaround for this lack is a watchdog;
15851da177e4SLinus Torvalds    we bail out of the wait-loop after a modest amount of wait-time if
15861da177e4SLinus Torvalds    the usual exit conditions are not met.  Not a terribly clean or
15871da177e4SLinus Torvalds    correct solution :-%
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds    Reads are equally tricky due to a nasty characteristic of the NCR5380.
15901da177e4SLinus Torvalds    If the chip is in DMA mode for an READ, it will respond to a target's
15911da177e4SLinus Torvalds    REQ by latching the SCSI data into the INPUT DATA register and asserting
15921da177e4SLinus Torvalds    ACK, even if it has _already_ been notified by the DMA controller that
15931da177e4SLinus Torvalds    the current DMA transfer has completed!  If the NCR5380 is then taken
15941da177e4SLinus Torvalds    out of DMA mode, this already-acknowledged byte is lost.
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds    This is not a problem for "one DMA transfer per command" reads, because
15971da177e4SLinus Torvalds    the situation will never arise... either all of the data is DMA'ed
15981da177e4SLinus Torvalds    properly, or the target switches to MESSAGE IN phase to signal a
15991da177e4SLinus Torvalds    disconnection (either operation bringing the DMA to a clean halt).
16001da177e4SLinus Torvalds    However, in order to handle scatter-reads, we must work around the
16011da177e4SLinus Torvalds    problem.  The chosen fix is to DMA N-2 bytes, then check for the
16021da177e4SLinus Torvalds    condition before taking the NCR5380 out of DMA mode.  One or two extra
16031da177e4SLinus Torvalds    bytes are transferred via PIO as necessary to fill out the original
16041da177e4SLinus Torvalds    request.
16051da177e4SLinus Torvalds  */
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 	if (p & SR_IO) {
16089db6024eSFinn Thain 		if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS)) {
16091da177e4SLinus Torvalds 			udelay(10);
16109db6024eSFinn Thain 			if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
16119db6024eSFinn Thain 			    (BASR_PHASE_MATCH | BASR_ACK)) {
16121da177e4SLinus Torvalds 				saved_data = NCR5380_read(INPUT_DATA_REGISTER);
16131da177e4SLinus Torvalds 				overrun = 1;
16141da177e4SLinus Torvalds 			}
16159db6024eSFinn Thain 		}
16161da177e4SLinus Torvalds 	} else {
16171da177e4SLinus Torvalds 		int limit = 100;
16181da177e4SLinus Torvalds 		while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
16191da177e4SLinus Torvalds 			if (!(tmp & BASR_PHASE_MATCH))
16201da177e4SLinus Torvalds 				break;
16211da177e4SLinus Torvalds 			if (--limit < 0)
16221da177e4SLinus Torvalds 				break;
16231da177e4SLinus Torvalds 		}
16241da177e4SLinus Torvalds 	}
16251da177e4SLinus Torvalds 
162652a6a1cbSFinn Thain 	dprintk(NDEBUG_DMA, "scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG));
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
16291da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 	residue = NCR5380_dma_residual(instance);
16321da177e4SLinus Torvalds 	c -= residue;
16331da177e4SLinus Torvalds 	*count -= c;
16341da177e4SLinus Torvalds 	*data += c;
16351da177e4SLinus Torvalds 	*phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
16361da177e4SLinus Torvalds 
16379db6024eSFinn Thain 	if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS) &&
16389db6024eSFinn Thain 	    *phase == p && (p & SR_IO) && residue == 0) {
16391da177e4SLinus Torvalds 		if (overrun) {
164052a6a1cbSFinn Thain 			dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
16411da177e4SLinus Torvalds 			**data = saved_data;
16421da177e4SLinus Torvalds 			*data += 1;
16431da177e4SLinus Torvalds 			*count -= 1;
16441da177e4SLinus Torvalds 			cnt = toPIO = 1;
16451da177e4SLinus Torvalds 		} else {
16461da177e4SLinus Torvalds 			printk("No overrun??\n");
16471da177e4SLinus Torvalds 			cnt = toPIO = 2;
16481da177e4SLinus Torvalds 		}
164952a6a1cbSFinn Thain 		dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
16501da177e4SLinus Torvalds 		NCR5380_transfer_pio(instance, phase, &cnt, data);
16511da177e4SLinus Torvalds 		*count -= toPIO - cnt;
16521da177e4SLinus Torvalds 	}
16531da177e4SLinus Torvalds 
165452a6a1cbSFinn Thain 	dprintk(NDEBUG_DMA, "Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n", *data, *count, *(*data + *count - 1), *(*data + *count));
16551da177e4SLinus Torvalds 	return 0;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds #elif defined(REAL_DMA)
16581da177e4SLinus Torvalds 	return 0;
16591da177e4SLinus Torvalds #else				/* defined(REAL_DMA_POLL) */
16601da177e4SLinus Torvalds 	if (p & SR_IO) {
166155181be8SFinn Thain 		foo = NCR5380_pread(instance, d,
166255181be8SFinn Thain 			hostdata->flags & FLAG_NO_DMA_FIXUP ? c : c - 1);
166355181be8SFinn Thain 		if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
16641da177e4SLinus Torvalds 			/*
16651da177e4SLinus Torvalds 			 * We can't disable DMA mode after successfully transferring
16661da177e4SLinus Torvalds 			 * what we plan to be the last byte, since that would open up
16671da177e4SLinus Torvalds 			 * a race condition where if the target asserted REQ before
16681da177e4SLinus Torvalds 			 * we got the DMA mode reset, the NCR5380 would have latched
16691da177e4SLinus Torvalds 			 * an additional byte into the INPUT DATA register and we'd
16701da177e4SLinus Torvalds 			 * have dropped it.
16711da177e4SLinus Torvalds 			 *
16721da177e4SLinus Torvalds 			 * The workaround was to transfer one fewer bytes than we
16731da177e4SLinus Torvalds 			 * intended to with the pseudo-DMA read function, wait for
16741da177e4SLinus Torvalds 			 * the chip to latch the last byte, read it, and then disable
16751da177e4SLinus Torvalds 			 * pseudo-DMA mode.
16761da177e4SLinus Torvalds 			 *
16771da177e4SLinus Torvalds 			 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
16781da177e4SLinus Torvalds 			 * REQ is deasserted when ACK is asserted, and not reasserted
16791da177e4SLinus Torvalds 			 * until ACK goes false.  Since the NCR5380 won't lower ACK
16801da177e4SLinus Torvalds 			 * until DACK is asserted, which won't happen unless we twiddle
16811da177e4SLinus Torvalds 			 * the DMA port or we take the NCR5380 out of DMA mode, we
16821da177e4SLinus Torvalds 			 * can guarantee that we won't handshake another extra
16831da177e4SLinus Torvalds 			 * byte.
16841da177e4SLinus Torvalds 			 */
16851da177e4SLinus Torvalds 
168655181be8SFinn Thain 			if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
168755181be8SFinn Thain 			                          BASR_DRQ, BASR_DRQ, HZ) < 0) {
168855181be8SFinn Thain 				foo = -1;
168955181be8SFinn Thain 				shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
169055181be8SFinn Thain 			}
169155181be8SFinn Thain 			if (NCR5380_poll_politely(instance, STATUS_REG,
169255181be8SFinn Thain 			                          SR_REQ, 0, HZ) < 0) {
169355181be8SFinn Thain 				foo = -1;
169455181be8SFinn Thain 				shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
169555181be8SFinn Thain 			}
16961da177e4SLinus Torvalds 			d[c - 1] = NCR5380_read(INPUT_DATA_REG);
16971da177e4SLinus Torvalds 		}
16981da177e4SLinus Torvalds 	} else {
16991da177e4SLinus Torvalds 		foo = NCR5380_pwrite(instance, d, c);
170055181be8SFinn Thain 		if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
17011da177e4SLinus Torvalds 			/*
17021da177e4SLinus Torvalds 			 * Wait for the last byte to be sent.  If REQ is being asserted for
17031da177e4SLinus Torvalds 			 * the byte we're interested, we'll ACK it and it will go false.
17041da177e4SLinus Torvalds 			 */
170555181be8SFinn Thain 			if (NCR5380_poll_politely2(instance,
170655181be8SFinn Thain 			     BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
170755181be8SFinn Thain 			     BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
170855181be8SFinn Thain 				foo = -1;
170955181be8SFinn Thain 				shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
17101da177e4SLinus Torvalds 			}
17111da177e4SLinus Torvalds 		}
17121da177e4SLinus Torvalds 	}
17131da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
17141da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
17151da177e4SLinus Torvalds 	NCR5380_read(RESET_PARITY_INTERRUPT_REG);
17161da177e4SLinus Torvalds 	*data = d + c;
17171da177e4SLinus Torvalds 	*count = 0;
17181da177e4SLinus Torvalds 	*phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
17191da177e4SLinus Torvalds 	return foo;
17201da177e4SLinus Torvalds #endif				/* def REAL_DMA */
17211da177e4SLinus Torvalds }
17221da177e4SLinus Torvalds #endif				/* defined(REAL_DMA) | defined(PSEUDO_DMA) */
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds /*
17251da177e4SLinus Torvalds  * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
17261da177e4SLinus Torvalds  *
17271da177e4SLinus Torvalds  * Purpose : run through the various SCSI phases and do as the target
17281da177e4SLinus Torvalds  *      directs us to.  Operates on the currently connected command,
17291da177e4SLinus Torvalds  *      instance->connected.
17301da177e4SLinus Torvalds  *
17311da177e4SLinus Torvalds  * Inputs : instance, instance for which we are doing commands
17321da177e4SLinus Torvalds  *
17331da177e4SLinus Torvalds  * Side effects : SCSI things happen, the disconnected queue will be
17341da177e4SLinus Torvalds  *      modified if a command disconnects, *instance->connected will
17351da177e4SLinus Torvalds  *      change.
17361da177e4SLinus Torvalds  *
17371da177e4SLinus Torvalds  * XXX Note : we need to watch for bus free or a reset condition here
17381da177e4SLinus Torvalds  *      to recover from an unexpected bus free condition.
17391da177e4SLinus Torvalds  *
17401da177e4SLinus Torvalds  * Locks: io_request_lock held by caller in IRQ mode
17411da177e4SLinus Torvalds  */
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds static void NCR5380_information_transfer(struct Scsi_Host *instance) {
1744*e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
17451da177e4SLinus Torvalds 	unsigned char msgout = NOP;
17461da177e4SLinus Torvalds 	int sink = 0;
17471da177e4SLinus Torvalds 	int len;
17481da177e4SLinus Torvalds #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
17491da177e4SLinus Torvalds 	int transfersize;
17501da177e4SLinus Torvalds #endif
17511da177e4SLinus Torvalds 	unsigned char *data;
17521da177e4SLinus Torvalds 	unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
175311d2f63bSFinn Thain 	struct scsi_cmnd *cmd;
17541da177e4SLinus Torvalds 
175511d2f63bSFinn Thain 	while ((cmd = hostdata->connected)) {
17561da177e4SLinus Torvalds 		tmp = NCR5380_read(STATUS_REG);
17571da177e4SLinus Torvalds 		/* We only have a valid SCSI phase when REQ is asserted */
17581da177e4SLinus Torvalds 		if (tmp & SR_REQ) {
17591da177e4SLinus Torvalds 			phase = (tmp & PHASE_MASK);
17601da177e4SLinus Torvalds 			if (phase != old_phase) {
17611da177e4SLinus Torvalds 				old_phase = phase;
17621da177e4SLinus Torvalds 				NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
17631da177e4SLinus Torvalds 			}
17641da177e4SLinus Torvalds 			if (sink && (phase != PHASE_MSGOUT)) {
17651da177e4SLinus Torvalds 				NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
17681da177e4SLinus Torvalds 				while (NCR5380_read(STATUS_REG) & SR_REQ);
17691da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
17701da177e4SLinus Torvalds 				sink = 0;
17711da177e4SLinus Torvalds 				continue;
17721da177e4SLinus Torvalds 			}
17731da177e4SLinus Torvalds 			switch (phase) {
17741da177e4SLinus Torvalds 			case PHASE_DATAOUT:
17751da177e4SLinus Torvalds #if (NDEBUG & NDEBUG_NO_DATAOUT)
17761da177e4SLinus Torvalds 				printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
17771da177e4SLinus Torvalds 				sink = 1;
17781da177e4SLinus Torvalds 				do_abort(instance);
17791da177e4SLinus Torvalds 				cmd->result = DID_ERROR << 16;
1780cce99c69SMatthew Wilcox 				cmd->scsi_done(cmd);
17811da177e4SLinus Torvalds 				return;
17821da177e4SLinus Torvalds #endif
1783bf1a0c6fSFinn Thain 			case PHASE_DATAIN:
17841da177e4SLinus Torvalds 				/*
17851da177e4SLinus Torvalds 				 * If there is no room left in the current buffer in the
17861da177e4SLinus Torvalds 				 * scatter-gather list, move onto the next one.
17871da177e4SLinus Torvalds 				 */
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 				if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
17901da177e4SLinus Torvalds 					++cmd->SCp.buffer;
17911da177e4SLinus Torvalds 					--cmd->SCp.buffers_residual;
17921da177e4SLinus Torvalds 					cmd->SCp.this_residual = cmd->SCp.buffer->length;
179345711f1aSJens Axboe 					cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
179452a6a1cbSFinn Thain 					dprintk(NDEBUG_INFORMATION, "scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual);
17951da177e4SLinus Torvalds 				}
17961da177e4SLinus Torvalds 				/*
17971da177e4SLinus Torvalds 				 * The preferred transfer method is going to be
17981da177e4SLinus Torvalds 				 * PSEUDO-DMA for systems that are strictly PIO,
17991da177e4SLinus Torvalds 				 * since we can let the hardware do the handshaking.
18001da177e4SLinus Torvalds 				 *
18011da177e4SLinus Torvalds 				 * For this to work, we need to know the transfersize
18021da177e4SLinus Torvalds 				 * ahead of time, since the pseudo-DMA code will sit
18031da177e4SLinus Torvalds 				 * in an unconditional loop.
18041da177e4SLinus Torvalds 				 */
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1807ff3d4578SFinn Thain 				transfersize = 0;
1808ff3d4578SFinn Thain 				if (!cmd->device->borken &&
1809ff3d4578SFinn Thain 				    !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1810ff3d4578SFinn Thain 					transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
18111da177e4SLinus Torvalds 
1812ff3d4578SFinn Thain 				if (transfersize) {
18131da177e4SLinus Torvalds 					len = transfersize;
18141da177e4SLinus Torvalds 					if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
18151da177e4SLinus Torvalds 						/*
18161da177e4SLinus Torvalds 						 * If the watchdog timer fires, all future accesses to this
18171da177e4SLinus Torvalds 						 * device will use the polled-IO.
18181da177e4SLinus Torvalds 						 */
1819017560fcSJeff Garzik 						scmd_printk(KERN_INFO, cmd,
1820017560fcSJeff Garzik 							    "switching to slow handshake\n");
18211da177e4SLinus Torvalds 						cmd->device->borken = 1;
18221da177e4SLinus Torvalds 						sink = 1;
18231da177e4SLinus Torvalds 						do_abort(instance);
18241da177e4SLinus Torvalds 						cmd->result = DID_ERROR << 16;
1825cce99c69SMatthew Wilcox 						cmd->scsi_done(cmd);
18261da177e4SLinus Torvalds 						/* XXX - need to source or sink data here, as appropriate */
18271da177e4SLinus Torvalds 					} else
18281da177e4SLinus Torvalds 						cmd->SCp.this_residual -= transfersize - len;
18291da177e4SLinus Torvalds 				} else
18301da177e4SLinus Torvalds #endif				/* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
183111d2f63bSFinn Thain 				{
183211d2f63bSFinn Thain 					spin_unlock_irq(&hostdata->lock);
18331da177e4SLinus Torvalds 					NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
18341da177e4SLinus Torvalds 							     &cmd->SCp.ptr);
183511d2f63bSFinn Thain 					spin_lock_irq(&hostdata->lock);
183611d2f63bSFinn Thain 				}
18371da177e4SLinus Torvalds 				break;
18381da177e4SLinus Torvalds 			case PHASE_MSGIN:
18391da177e4SLinus Torvalds 				len = 1;
18401da177e4SLinus Torvalds 				data = &tmp;
18411da177e4SLinus Torvalds 				NCR5380_transfer_pio(instance, &phase, &len, &data);
18421da177e4SLinus Torvalds 				cmd->SCp.Message = tmp;
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 				switch (tmp) {
18451da177e4SLinus Torvalds 				case ABORT:
18461da177e4SLinus Torvalds 				case COMMAND_COMPLETE:
18471da177e4SLinus Torvalds 					/* Accept message by clearing ACK */
18481da177e4SLinus Torvalds 					sink = 1;
18491da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
18501da177e4SLinus Torvalds 					hostdata->connected = NULL;
18519cb78c16SHannes Reinecke 					dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun);
18529cb78c16SHannes Reinecke 					hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds 					/*
18551da177e4SLinus Torvalds 					 * I'm not sure what the correct thing to do here is :
18561da177e4SLinus Torvalds 					 *
18571da177e4SLinus Torvalds 					 * If the command that just executed is NOT a request
18581da177e4SLinus Torvalds 					 * sense, the obvious thing to do is to set the result
18591da177e4SLinus Torvalds 					 * code to the values of the stored parameters.
18601da177e4SLinus Torvalds 					 *
18611da177e4SLinus Torvalds 					 * If it was a REQUEST SENSE command, we need some way
18621da177e4SLinus Torvalds 					 * to differentiate between the failure code of the original
18631da177e4SLinus Torvalds 					 * and the failure code of the REQUEST sense - the obvious
18641da177e4SLinus Torvalds 					 * case is success, where we fall through and leave the result
18651da177e4SLinus Torvalds 					 * code unchanged.
18661da177e4SLinus Torvalds 					 *
18671da177e4SLinus Torvalds 					 * The non-obvious place is where the REQUEST SENSE failed
18681da177e4SLinus Torvalds 					 */
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 					if (cmd->cmnd[0] != REQUEST_SENSE)
18711da177e4SLinus Torvalds 						cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
18721da177e4SLinus Torvalds 					else if (status_byte(cmd->SCp.Status) != GOOD)
18731da177e4SLinus Torvalds 						cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
18741da177e4SLinus Torvalds 
187528424d3aSBoaz Harrosh 					if ((cmd->cmnd[0] == REQUEST_SENSE) &&
187628424d3aSBoaz Harrosh 						hostdata->ses.cmd_len) {
187728424d3aSBoaz Harrosh 						scsi_eh_restore_cmnd(cmd, &hostdata->ses);
187828424d3aSBoaz Harrosh 						hostdata->ses.cmd_len = 0 ;
187928424d3aSBoaz Harrosh 					}
18801da177e4SLinus Torvalds 
188128424d3aSBoaz Harrosh 					if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
188228424d3aSBoaz Harrosh 						scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
188328424d3aSBoaz Harrosh 
188452a6a1cbSFinn Thain 						dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no);
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 						LIST(cmd, hostdata->issue_queue);
18871da177e4SLinus Torvalds 						cmd->host_scribble = (unsigned char *)
18881da177e4SLinus Torvalds 						    hostdata->issue_queue;
1889710ddd0dSFinn Thain 						hostdata->issue_queue = (struct scsi_cmnd *) cmd;
1890dbb6b350SFinn Thain 						dsprintk(NDEBUG_QUEUES, instance,
1891dbb6b350SFinn Thain 						         "REQUEST SENSE cmd %p added to head of issue queue\n",
1892dbb6b350SFinn Thain 						         cmd);
1893997acab7SFinn Thain 					} else {
18941da177e4SLinus Torvalds 						cmd->scsi_done(cmd);
18951da177e4SLinus Torvalds 					}
18961da177e4SLinus Torvalds 
18971da177e4SLinus Torvalds 					/*
18981da177e4SLinus Torvalds 					 * Restore phase bits to 0 so an interrupted selection,
18991da177e4SLinus Torvalds 					 * arbitration can resume.
19001da177e4SLinus Torvalds 					 */
19011da177e4SLinus Torvalds 					NCR5380_write(TARGET_COMMAND_REG, 0);
190272064a78SFinn Thain 
190372064a78SFinn Thain 					/* Enable reselect interrupts */
190472064a78SFinn Thain 					NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
19051da177e4SLinus Torvalds 					return;
19061da177e4SLinus Torvalds 				case MESSAGE_REJECT:
19071da177e4SLinus Torvalds 					/* Accept message by clearing ACK */
19081da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
19091da177e4SLinus Torvalds 					switch (hostdata->last_message) {
19101da177e4SLinus Torvalds 					case HEAD_OF_QUEUE_TAG:
19111da177e4SLinus Torvalds 					case ORDERED_QUEUE_TAG:
19121da177e4SLinus Torvalds 					case SIMPLE_QUEUE_TAG:
19131da177e4SLinus Torvalds 						cmd->device->simple_tags = 0;
19149cb78c16SHannes Reinecke 						hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
19151da177e4SLinus Torvalds 						break;
19161da177e4SLinus Torvalds 					default:
19171da177e4SLinus Torvalds 						break;
19181da177e4SLinus Torvalds 					}
1919340b9612SFinn Thain 					break;
19201da177e4SLinus Torvalds 				case DISCONNECT:{
19211da177e4SLinus Torvalds 						/* Accept message by clearing ACK */
19221da177e4SLinus Torvalds 						NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
19231da177e4SLinus Torvalds 						LIST(cmd, hostdata->disconnected_queue);
19241da177e4SLinus Torvalds 						cmd->host_scribble = (unsigned char *)
19251da177e4SLinus Torvalds 						    hostdata->disconnected_queue;
19261da177e4SLinus Torvalds 						hostdata->connected = NULL;
19271da177e4SLinus Torvalds 						hostdata->disconnected_queue = cmd;
19289cb78c16SHannes Reinecke 						dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d lun %llu was moved from connected to" "  the disconnected_queue\n", instance->host_no, cmd->device->id, cmd->device->lun);
19291da177e4SLinus Torvalds 						/*
19301da177e4SLinus Torvalds 						 * Restore phase bits to 0 so an interrupted selection,
19311da177e4SLinus Torvalds 						 * arbitration can resume.
19321da177e4SLinus Torvalds 						 */
19331da177e4SLinus Torvalds 						NCR5380_write(TARGET_COMMAND_REG, 0);
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 						/* Enable reselect interrupts */
19361da177e4SLinus Torvalds 						NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
19371da177e4SLinus Torvalds 						return;
19381da177e4SLinus Torvalds 					}
19391da177e4SLinus Torvalds 					/*
19401da177e4SLinus Torvalds 					 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
19411da177e4SLinus Torvalds 					 * operation, in violation of the SCSI spec so we can safely
19421da177e4SLinus Torvalds 					 * ignore SAVE/RESTORE pointers calls.
19431da177e4SLinus Torvalds 					 *
19441da177e4SLinus Torvalds 					 * Unfortunately, some disks violate the SCSI spec and
19451da177e4SLinus Torvalds 					 * don't issue the required SAVE_POINTERS message before
19461da177e4SLinus Torvalds 					 * disconnecting, and we have to break spec to remain
19471da177e4SLinus Torvalds 					 * compatible.
19481da177e4SLinus Torvalds 					 */
19491da177e4SLinus Torvalds 				case SAVE_POINTERS:
19501da177e4SLinus Torvalds 				case RESTORE_POINTERS:
19511da177e4SLinus Torvalds 					/* Accept message by clearing ACK */
19521da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
19531da177e4SLinus Torvalds 					break;
19541da177e4SLinus Torvalds 				case EXTENDED_MESSAGE:
19551da177e4SLinus Torvalds /*
19561da177e4SLinus Torvalds  * Extended messages are sent in the following format :
19571da177e4SLinus Torvalds  * Byte
19581da177e4SLinus Torvalds  * 0            EXTENDED_MESSAGE == 1
19591da177e4SLinus Torvalds  * 1            length (includes one byte for code, doesn't
19601da177e4SLinus Torvalds  *              include first two bytes)
19611da177e4SLinus Torvalds  * 2            code
19621da177e4SLinus Torvalds  * 3..length+1  arguments
19631da177e4SLinus Torvalds  *
19641da177e4SLinus Torvalds  * Start the extended message buffer with the EXTENDED_MESSAGE
19651abfd370SMatthew Wilcox  * byte, since spi_print_msg() wants the whole thing.
19661da177e4SLinus Torvalds  */
19671da177e4SLinus Torvalds 					extended_msg[0] = EXTENDED_MESSAGE;
19681da177e4SLinus Torvalds 					/* Accept first byte by clearing ACK */
19691da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
197011d2f63bSFinn Thain 
197111d2f63bSFinn Thain 					spin_unlock_irq(&hostdata->lock);
197211d2f63bSFinn Thain 
197352a6a1cbSFinn Thain 					dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 					len = 2;
19761da177e4SLinus Torvalds 					data = extended_msg + 1;
19771da177e4SLinus Torvalds 					phase = PHASE_MSGIN;
19781da177e4SLinus Torvalds 					NCR5380_transfer_pio(instance, &phase, &len, &data);
19791da177e4SLinus Torvalds 
198052a6a1cbSFinn Thain 					dprintk(NDEBUG_EXTENDED, "scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]);
19811da177e4SLinus Torvalds 
1982e0783ed3SFinn Thain 					if (!len && extended_msg[1] > 0 &&
1983e0783ed3SFinn Thain 					    extended_msg[1] <= sizeof(extended_msg) - 2) {
19841da177e4SLinus Torvalds 						/* Accept third byte by clearing ACK */
19851da177e4SLinus Torvalds 						NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
19861da177e4SLinus Torvalds 						len = extended_msg[1] - 1;
19871da177e4SLinus Torvalds 						data = extended_msg + 3;
19881da177e4SLinus Torvalds 						phase = PHASE_MSGIN;
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 						NCR5380_transfer_pio(instance, &phase, &len, &data);
199152a6a1cbSFinn Thain 						dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 						switch (extended_msg[2]) {
19941da177e4SLinus Torvalds 						case EXTENDED_SDTR:
19951da177e4SLinus Torvalds 						case EXTENDED_WDTR:
19961da177e4SLinus Torvalds 						case EXTENDED_MODIFY_DATA_POINTER:
19971da177e4SLinus Torvalds 						case EXTENDED_EXTENDED_IDENTIFY:
19981da177e4SLinus Torvalds 							tmp = 0;
19991da177e4SLinus Torvalds 						}
20001da177e4SLinus Torvalds 					} else if (len) {
20011da177e4SLinus Torvalds 						printk("scsi%d: error receiving extended message\n", instance->host_no);
20021da177e4SLinus Torvalds 						tmp = 0;
20031da177e4SLinus Torvalds 					} else {
20041da177e4SLinus Torvalds 						printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
20051da177e4SLinus Torvalds 						tmp = 0;
20061da177e4SLinus Torvalds 					}
200711d2f63bSFinn Thain 
200811d2f63bSFinn Thain 					spin_lock_irq(&hostdata->lock);
200911d2f63bSFinn Thain 					if (!hostdata->connected)
201011d2f63bSFinn Thain 						return;
201111d2f63bSFinn Thain 
20121da177e4SLinus Torvalds 					/* Fall through to reject message */
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds 					/*
20151da177e4SLinus Torvalds 					 * If we get something weird that we aren't expecting,
20161da177e4SLinus Torvalds 					 * reject it.
20171da177e4SLinus Torvalds 					 */
20181da177e4SLinus Torvalds 				default:
20191da177e4SLinus Torvalds 					if (!tmp) {
20201da177e4SLinus Torvalds 						printk("scsi%d: rejecting message ", instance->host_no);
20211abfd370SMatthew Wilcox 						spi_print_msg(extended_msg);
20221da177e4SLinus Torvalds 						printk("\n");
20231da177e4SLinus Torvalds 					} else if (tmp != EXTENDED_MESSAGE)
2024017560fcSJeff Garzik 						scmd_printk(KERN_INFO, cmd,
2025017560fcSJeff Garzik 							"rejecting unknown message %02x\n",tmp);
20261da177e4SLinus Torvalds 					else
2027017560fcSJeff Garzik 						scmd_printk(KERN_INFO, cmd,
2028017560fcSJeff Garzik 							"rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds 					msgout = MESSAGE_REJECT;
20311da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
20321da177e4SLinus Torvalds 					break;
20331da177e4SLinus Torvalds 				}	/* switch (tmp) */
20341da177e4SLinus Torvalds 				break;
20351da177e4SLinus Torvalds 			case PHASE_MSGOUT:
20361da177e4SLinus Torvalds 				len = 1;
20371da177e4SLinus Torvalds 				data = &msgout;
20381da177e4SLinus Torvalds 				hostdata->last_message = msgout;
20391da177e4SLinus Torvalds 				NCR5380_transfer_pio(instance, &phase, &len, &data);
20401da177e4SLinus Torvalds 				if (msgout == ABORT) {
20419cb78c16SHannes Reinecke 					hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
20421da177e4SLinus Torvalds 					hostdata->connected = NULL;
20431da177e4SLinus Torvalds 					cmd->result = DID_ERROR << 16;
20441da177e4SLinus Torvalds 					cmd->scsi_done(cmd);
20451da177e4SLinus Torvalds 					NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
20461da177e4SLinus Torvalds 					return;
20471da177e4SLinus Torvalds 				}
20481da177e4SLinus Torvalds 				msgout = NOP;
20491da177e4SLinus Torvalds 				break;
20501da177e4SLinus Torvalds 			case PHASE_CMDOUT:
20511da177e4SLinus Torvalds 				len = cmd->cmd_len;
20521da177e4SLinus Torvalds 				data = cmd->cmnd;
20531da177e4SLinus Torvalds 				/*
20541da177e4SLinus Torvalds 				 * XXX for performance reasons, on machines with a
20551da177e4SLinus Torvalds 				 * PSEUDO-DMA architecture we should probably
20561da177e4SLinus Torvalds 				 * use the dma transfer function.
20571da177e4SLinus Torvalds 				 */
20581da177e4SLinus Torvalds 				NCR5380_transfer_pio(instance, &phase, &len, &data);
20591da177e4SLinus Torvalds 				break;
20601da177e4SLinus Torvalds 			case PHASE_STATIN:
20611da177e4SLinus Torvalds 				len = 1;
20621da177e4SLinus Torvalds 				data = &tmp;
20631da177e4SLinus Torvalds 				NCR5380_transfer_pio(instance, &phase, &len, &data);
20641da177e4SLinus Torvalds 				cmd->SCp.Status = tmp;
20651da177e4SLinus Torvalds 				break;
20661da177e4SLinus Torvalds 			default:
20671da177e4SLinus Torvalds 				printk("scsi%d : unknown phase\n", instance->host_no);
20684dde8f7dSFinn Thain 				NCR5380_dprint(NDEBUG_ANY, instance);
20691da177e4SLinus Torvalds 			}	/* switch(phase) */
2070686f3990SFinn Thain 		} else {
207111d2f63bSFinn Thain 			spin_unlock_irq(&hostdata->lock);
2072686f3990SFinn Thain 			NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
207311d2f63bSFinn Thain 			spin_lock_irq(&hostdata->lock);
20741da177e4SLinus Torvalds 		}
207511d2f63bSFinn Thain 	}
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds /*
20791da177e4SLinus Torvalds  * Function : void NCR5380_reselect (struct Scsi_Host *instance)
20801da177e4SLinus Torvalds  *
20811da177e4SLinus Torvalds  * Purpose : does reselection, initializing the instance->connected
2082710ddd0dSFinn Thain  *      field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
20831da177e4SLinus Torvalds  *      nexus has been reestablished,
20841da177e4SLinus Torvalds  *
20851da177e4SLinus Torvalds  * Inputs : instance - this instance of the NCR5380.
20861da177e4SLinus Torvalds  *
20871da177e4SLinus Torvalds  * Locks: io_request_lock held by caller if IRQ driven
20881da177e4SLinus Torvalds  */
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds static void NCR5380_reselect(struct Scsi_Host *instance) {
2091*e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
20921da177e4SLinus Torvalds 	unsigned char target_mask;
20931da177e4SLinus Torvalds 	unsigned char lun, phase;
20941da177e4SLinus Torvalds 	int len;
20951da177e4SLinus Torvalds 	unsigned char msg[3];
20961da177e4SLinus Torvalds 	unsigned char *data;
2097710ddd0dSFinn Thain 	struct scsi_cmnd *tmp = NULL, *prev;
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds 	/*
21001da177e4SLinus Torvalds 	 * Disable arbitration, etc. since the host adapter obviously
21011da177e4SLinus Torvalds 	 * lost, and tell an interrupted NCR5380_select() to restart.
21021da177e4SLinus Torvalds 	 */
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds 	target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
210772064a78SFinn Thain 	dprintk(NDEBUG_RESELECTION, "scsi%d : reselect\n", instance->host_no);
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	/*
21101da177e4SLinus Torvalds 	 * At this point, we have detected that our SCSI ID is on the bus,
21111da177e4SLinus Torvalds 	 * SEL is true and BSY was false for at least one bus settle delay
21121da177e4SLinus Torvalds 	 * (400 ns).
21131da177e4SLinus Torvalds 	 *
21141da177e4SLinus Torvalds 	 * We must assert BSY ourselves, until the target drops the SEL
21151da177e4SLinus Torvalds 	 * signal.
21161da177e4SLinus Torvalds 	 */
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
211972064a78SFinn Thain 	if (NCR5380_poll_politely(instance,
212072064a78SFinn Thain 	                          STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
212172064a78SFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
212272064a78SFinn Thain 		return;
212372064a78SFinn Thain 	}
21241da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
21251da177e4SLinus Torvalds 
21261da177e4SLinus Torvalds 	/*
21271da177e4SLinus Torvalds 	 * Wait for target to go into MSGIN.
21281da177e4SLinus Torvalds 	 */
21291da177e4SLinus Torvalds 
21301cc160e1SFinn Thain 	if (NCR5380_poll_politely(instance,
213172064a78SFinn Thain 	                          STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
213272064a78SFinn Thain 		do_abort(instance);
213372064a78SFinn Thain 		return;
213472064a78SFinn Thain 	}
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds 	len = 1;
21371da177e4SLinus Torvalds 	data = msg;
21381da177e4SLinus Torvalds 	phase = PHASE_MSGIN;
21391da177e4SLinus Torvalds 	NCR5380_transfer_pio(instance, &phase, &len, &data);
21401da177e4SLinus Torvalds 
214172064a78SFinn Thain 	if (len) {
214272064a78SFinn Thain 		do_abort(instance);
214372064a78SFinn Thain 		return;
214472064a78SFinn Thain 	}
214572064a78SFinn Thain 
21461da177e4SLinus Torvalds 	if (!(msg[0] & 0x80)) {
214772064a78SFinn Thain 		shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
21481abfd370SMatthew Wilcox 		spi_print_msg(msg);
214972064a78SFinn Thain 		printk("\n");
215072064a78SFinn Thain 		do_abort(instance);
215172064a78SFinn Thain 		return;
215272064a78SFinn Thain 	}
215372064a78SFinn Thain 	lun = msg[0] & 0x07;
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds 	/*
21561da177e4SLinus Torvalds 	 * We need to add code for SCSI-II to track which devices have
21571da177e4SLinus Torvalds 	 * I_T_L_Q nexuses established, and which have simple I_T_L
21581da177e4SLinus Torvalds 	 * nexuses so we can chose to do additional data transfer.
21591da177e4SLinus Torvalds 	 */
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds 	/*
21621da177e4SLinus Torvalds 	 * Find the command corresponding to the I_T_L or I_T_L_Q  nexus we
21631da177e4SLinus Torvalds 	 * just reestablished, and remove it from the disconnected queue.
21641da177e4SLinus Torvalds 	 */
21651da177e4SLinus Torvalds 
216672064a78SFinn Thain 	for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
216772064a78SFinn Thain 	     tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble) {
216872064a78SFinn Thain 		if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)) {
21691da177e4SLinus Torvalds 			if (prev) {
21701da177e4SLinus Torvalds 				REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
21711da177e4SLinus Torvalds 				prev->host_scribble = tmp->host_scribble;
21721da177e4SLinus Torvalds 			} else {
21731da177e4SLinus Torvalds 				REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
217472064a78SFinn Thain 				hostdata->disconnected_queue =
217572064a78SFinn Thain 					(struct scsi_cmnd *) tmp->host_scribble;
21761da177e4SLinus Torvalds 			}
21771da177e4SLinus Torvalds 			tmp->host_scribble = NULL;
21781da177e4SLinus Torvalds 			break;
21791da177e4SLinus Torvalds 		}
218072064a78SFinn Thain 	}
21811da177e4SLinus Torvalds 	if (!tmp) {
218272064a78SFinn Thain 		shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
218372064a78SFinn Thain 		             target_mask, lun);
21841da177e4SLinus Torvalds 		/*
21851da177e4SLinus Torvalds 		 * Since we have an established nexus that we can't do anything with,
21861da177e4SLinus Torvalds 		 * we must abort it.
21871da177e4SLinus Torvalds 		 */
218872064a78SFinn Thain 		do_abort(instance);
218972064a78SFinn Thain 		return;
21901da177e4SLinus Torvalds 	}
21911da177e4SLinus Torvalds 
219272064a78SFinn Thain 	/* Accept message by clearing ACK */
219372064a78SFinn Thain 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
219472064a78SFinn Thain 
21951da177e4SLinus Torvalds 	hostdata->connected = tmp;
219672064a78SFinn Thain 	dprintk(NDEBUG_RESELECTION, "scsi%d : nexus established, target = %d, lun = %llu, tag = %d\n",
219772064a78SFinn Thain 	        instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag);
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds /*
22011da177e4SLinus Torvalds  * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
22021da177e4SLinus Torvalds  *
22031da177e4SLinus Torvalds  * Purpose : called by interrupt handler when DMA finishes or a phase
22041da177e4SLinus Torvalds  *      mismatch occurs (which would finish the DMA transfer).
22051da177e4SLinus Torvalds  *
22061da177e4SLinus Torvalds  * Inputs : instance - this instance of the NCR5380.
22071da177e4SLinus Torvalds  *
2208710ddd0dSFinn Thain  * Returns : pointer to the scsi_cmnd structure for which the I_T_L
22091da177e4SLinus Torvalds  *      nexus has been reestablished, on failure NULL is returned.
22101da177e4SLinus Torvalds  */
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds #ifdef REAL_DMA
22131da177e4SLinus Torvalds static void NCR5380_dma_complete(NCR5380_instance * instance) {
2214*e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
22151da177e4SLinus Torvalds 	int transferred;
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds 	/*
22181da177e4SLinus Torvalds 	 * XXX this might not be right.
22191da177e4SLinus Torvalds 	 *
22201da177e4SLinus Torvalds 	 * Wait for final byte to transfer, ie wait for ACK to go false.
22211da177e4SLinus Torvalds 	 *
22221da177e4SLinus Torvalds 	 * We should use the Last Byte Sent bit, unfortunately this is
22231da177e4SLinus Torvalds 	 * not available on the 5380/5381 (only the various CMOS chips)
22241da177e4SLinus Torvalds 	 *
22251da177e4SLinus Torvalds 	 * FIXME: timeout, and need to handle long timeout/irq case
22261da177e4SLinus Torvalds 	 */
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 	NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	/*
22331da177e4SLinus Torvalds 	 * The only places we should see a phase mismatch and have to send
22341da177e4SLinus Torvalds 	 * data from the same set of pointers will be the data transfer
22351da177e4SLinus Torvalds 	 * phases.  So, residual, requested length are only important here.
22361da177e4SLinus Torvalds 	 */
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds 	if (!(hostdata->connected->SCp.phase & SR_CD)) {
22391da177e4SLinus Torvalds 		transferred = instance->dmalen - NCR5380_dma_residual();
22401da177e4SLinus Torvalds 		hostdata->connected->SCp.this_residual -= transferred;
22411da177e4SLinus Torvalds 		hostdata->connected->SCp.ptr += transferred;
22421da177e4SLinus Torvalds 	}
22431da177e4SLinus Torvalds }
22441da177e4SLinus Torvalds #endif				/* def REAL_DMA */
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds /*
2247710ddd0dSFinn Thain  * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
22481da177e4SLinus Torvalds  *
22491da177e4SLinus Torvalds  * Purpose : abort a command
22501da177e4SLinus Torvalds  *
2251710ddd0dSFinn Thain  * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
22521da177e4SLinus Torvalds  *      host byte of the result field to, if zero DID_ABORTED is
22531da177e4SLinus Torvalds  *      used.
22541da177e4SLinus Torvalds  *
2255b6c92b7eSHannes Reinecke  * Returns : SUCCESS - success, FAILED on failure.
22561da177e4SLinus Torvalds  *
22571da177e4SLinus Torvalds  *	XXX - there is no way to abort the command that is currently
22581da177e4SLinus Torvalds  *	connected, you have to wait for it to complete.  If this is
22591da177e4SLinus Torvalds  *	a problem, we could implement longjmp() / setjmp(), setjmp()
22601da177e4SLinus Torvalds  *	called where the loop started in NCR5380_main().
22611da177e4SLinus Torvalds  *
22621da177e4SLinus Torvalds  * Locks: host lock taken by caller
22631da177e4SLinus Torvalds  */
22641da177e4SLinus Torvalds 
2265710ddd0dSFinn Thain static int NCR5380_abort(struct scsi_cmnd *cmd)
2266710ddd0dSFinn Thain {
22671da177e4SLinus Torvalds 	struct Scsi_Host *instance = cmd->device->host;
2268*e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
2269710ddd0dSFinn Thain 	struct scsi_cmnd *tmp, **prev;
227011d2f63bSFinn Thain 	unsigned long flags;
22711da177e4SLinus Torvalds 
22721fa6b5fbSHannes Reinecke 	scmd_printk(KERN_WARNING, cmd, "aborting command\n");
22731da177e4SLinus Torvalds 
227411d2f63bSFinn Thain 	spin_lock_irqsave(&hostdata->lock, flags);
227511d2f63bSFinn Thain 
2276e5c3fddfSFinn Thain 	NCR5380_dprint(NDEBUG_ANY, instance);
2277e5c3fddfSFinn Thain 	NCR5380_dprint_phase(NDEBUG_ANY, instance);
22781da177e4SLinus Torvalds 
227952a6a1cbSFinn Thain 	dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no);
228052a6a1cbSFinn Thain 	dprintk(NDEBUG_ABORT, "        basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds #if 0
22831da177e4SLinus Torvalds /*
22841da177e4SLinus Torvalds  * Case 1 : If the command is the currently executing command,
22851da177e4SLinus Torvalds  * we'll set the aborted flag and return control so that
22861da177e4SLinus Torvalds  * information transfer routine can exit cleanly.
22871da177e4SLinus Torvalds  */
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds 	if (hostdata->connected == cmd) {
229052a6a1cbSFinn Thain 		dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no);
22911da177e4SLinus Torvalds /*
22921da177e4SLinus Torvalds  * We should perform BSY checking, and make sure we haven't slipped
22931da177e4SLinus Torvalds  * into BUS FREE.
22941da177e4SLinus Torvalds  */
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
22971da177e4SLinus Torvalds /*
22981da177e4SLinus Torvalds  * Since we can't change phases until we've completed the current
22991da177e4SLinus Torvalds  * handshake, we have to source or sink a byte of data if the current
23001da177e4SLinus Torvalds  * phase is not MSGOUT.
23011da177e4SLinus Torvalds  */
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds /*
23041da177e4SLinus Torvalds  * Return control to the executing NCR drive so we can clear the
23051da177e4SLinus Torvalds  * aborted flag and get back into our main loop.
23061da177e4SLinus Torvalds  */
23071da177e4SLinus Torvalds 
2308b6c92b7eSHannes Reinecke 		return SUCCESS;
23091da177e4SLinus Torvalds 	}
23101da177e4SLinus Torvalds #endif
23111da177e4SLinus Torvalds 
23121da177e4SLinus Torvalds /*
23131da177e4SLinus Torvalds  * Case 2 : If the command hasn't been issued yet, we simply remove it
23141da177e4SLinus Torvalds  *          from the issue queue.
23151da177e4SLinus Torvalds  */
23161da177e4SLinus Torvalds 
231752a6a1cbSFinn Thain 	dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no);
2318710ddd0dSFinn Thain 	for (prev = (struct scsi_cmnd **) &(hostdata->issue_queue), tmp = (struct scsi_cmnd *) hostdata->issue_queue; tmp; prev = (struct scsi_cmnd **) &(tmp->host_scribble), tmp = (struct scsi_cmnd *) tmp->host_scribble)
23191da177e4SLinus Torvalds 		if (cmd == tmp) {
23201da177e4SLinus Torvalds 			REMOVE(5, *prev, tmp, tmp->host_scribble);
2321710ddd0dSFinn Thain 			(*prev) = (struct scsi_cmnd *) tmp->host_scribble;
23221da177e4SLinus Torvalds 			tmp->host_scribble = NULL;
232311d2f63bSFinn Thain 			spin_unlock_irqrestore(&hostdata->lock, flags);
23241da177e4SLinus Torvalds 			tmp->result = DID_ABORT << 16;
232552a6a1cbSFinn Thain 			dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no);
2326cce99c69SMatthew Wilcox 			tmp->scsi_done(tmp);
23271da177e4SLinus Torvalds 			return SUCCESS;
23281da177e4SLinus Torvalds 		}
23291da177e4SLinus Torvalds #if (NDEBUG  & NDEBUG_ABORT)
23301da177e4SLinus Torvalds 	/* KLL */
23311da177e4SLinus Torvalds 		else if (prev == tmp)
23321da177e4SLinus Torvalds 			printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
23331da177e4SLinus Torvalds #endif
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds /*
23361da177e4SLinus Torvalds  * Case 3 : If any commands are connected, we're going to fail the abort
23371da177e4SLinus Torvalds  *          and let the high level SCSI driver retry at a later time or
23381da177e4SLinus Torvalds  *          issue a reset.
23391da177e4SLinus Torvalds  *
23401da177e4SLinus Torvalds  *          Timeouts, and therefore aborted commands, will be highly unlikely
23411da177e4SLinus Torvalds  *          and handling them cleanly in this situation would make the common
23421da177e4SLinus Torvalds  *          case of noresets less efficient, and would pollute our code.  So,
23431da177e4SLinus Torvalds  *          we fail.
23441da177e4SLinus Torvalds  */
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	if (hostdata->connected) {
234711d2f63bSFinn Thain 		spin_unlock_irqrestore(&hostdata->lock, flags);
234852a6a1cbSFinn Thain 		dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no);
23491da177e4SLinus Torvalds 		return FAILED;
23501da177e4SLinus Torvalds 	}
23511da177e4SLinus Torvalds /*
23521da177e4SLinus Torvalds  * Case 4: If the command is currently disconnected from the bus, and
23531da177e4SLinus Torvalds  *      there are no connected commands, we reconnect the I_T_L or
23541da177e4SLinus Torvalds  *      I_T_L_Q nexus associated with it, go into message out, and send
23551da177e4SLinus Torvalds  *      an abort message.
23561da177e4SLinus Torvalds  *
23571da177e4SLinus Torvalds  * This case is especially ugly. In order to reestablish the nexus, we
23581da177e4SLinus Torvalds  * need to call NCR5380_select().  The easiest way to implement this
23591da177e4SLinus Torvalds  * function was to abort if the bus was busy, and let the interrupt
23601da177e4SLinus Torvalds  * handler triggered on the SEL for reselect take care of lost arbitrations
23611da177e4SLinus Torvalds  * where necessary, meaning interrupts need to be enabled.
23621da177e4SLinus Torvalds  *
23631da177e4SLinus Torvalds  * When interrupts are enabled, the queues may change - so we
23641da177e4SLinus Torvalds  * can't remove it from the disconnected queue before selecting it
23651da177e4SLinus Torvalds  * because that could cause a failure in hashing the nexus if that
23661da177e4SLinus Torvalds  * device reselected.
23671da177e4SLinus Torvalds  *
23681da177e4SLinus Torvalds  * Since the queues may change, we can't use the pointers from when we
23691da177e4SLinus Torvalds  * first locate it.
23701da177e4SLinus Torvalds  *
23711da177e4SLinus Torvalds  * So, we must first locate the command, and if NCR5380_select()
23721da177e4SLinus Torvalds  * succeeds, then issue the abort, relocate the command and remove
23731da177e4SLinus Torvalds  * it from the disconnected queue.
23741da177e4SLinus Torvalds  */
23751da177e4SLinus Torvalds 
2376710ddd0dSFinn Thain 	for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; tmp = (struct scsi_cmnd *) tmp->host_scribble)
23771da177e4SLinus Torvalds 		if (cmd == tmp) {
237852a6a1cbSFinn Thain 			dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no);
23791da177e4SLinus Torvalds 
23809dafbd93SFinn Thain 			if (NCR5380_select(instance, cmd)) {
238111d2f63bSFinn Thain 				spin_unlock_irq(&hostdata->lock);
23821da177e4SLinus Torvalds 				return FAILED;
23839dafbd93SFinn Thain 			}
238452a6a1cbSFinn Thain 			dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no);
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds 			do_abort(instance);
23871da177e4SLinus Torvalds 
2388710ddd0dSFinn Thain 			for (prev = (struct scsi_cmnd **) &(hostdata->disconnected_queue), tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; prev = (struct scsi_cmnd **) &(tmp->host_scribble), tmp = (struct scsi_cmnd *) tmp->host_scribble)
23891da177e4SLinus Torvalds 				if (cmd == tmp) {
23901da177e4SLinus Torvalds 					REMOVE(5, *prev, tmp, tmp->host_scribble);
2391710ddd0dSFinn Thain 					*prev = (struct scsi_cmnd *) tmp->host_scribble;
23921da177e4SLinus Torvalds 					tmp->host_scribble = NULL;
239311d2f63bSFinn Thain 					spin_unlock_irqrestore(&hostdata->lock, flags);
23941da177e4SLinus Torvalds 					tmp->result = DID_ABORT << 16;
2395cce99c69SMatthew Wilcox 					tmp->scsi_done(tmp);
23961da177e4SLinus Torvalds 					return SUCCESS;
23971da177e4SLinus Torvalds 				}
23981da177e4SLinus Torvalds 		}
23991da177e4SLinus Torvalds /*
24001da177e4SLinus Torvalds  * Case 5 : If we reached this point, the command was not found in any of
24011da177e4SLinus Torvalds  *          the queues.
24021da177e4SLinus Torvalds  *
24031da177e4SLinus Torvalds  * We probably reached this point because of an unlikely race condition
24041da177e4SLinus Torvalds  * between the command completing successfully and the abortion code,
24051da177e4SLinus Torvalds  * so we won't panic, but we will notify the user in case something really
24061da177e4SLinus Torvalds  * broke.
24071da177e4SLinus Torvalds  */
240811d2f63bSFinn Thain 	spin_unlock_irqrestore(&hostdata->lock, flags);
24091da177e4SLinus Torvalds 	printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
24101da177e4SLinus Torvalds 			"         before abortion\n", instance->host_no);
24111da177e4SLinus Torvalds 	return FAILED;
24121da177e4SLinus Torvalds }
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds 
24153be1b3eaSFinn Thain /**
24163be1b3eaSFinn Thain  * NCR5380_bus_reset - reset the SCSI bus
24173be1b3eaSFinn Thain  * @cmd: SCSI command undergoing EH
24181da177e4SLinus Torvalds  *
24193be1b3eaSFinn Thain  * Returns SUCCESS
24201da177e4SLinus Torvalds  */
24211da177e4SLinus Torvalds 
2422710ddd0dSFinn Thain static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
242368b3aa7cSJeff Garzik  {
242468b3aa7cSJeff Garzik  	struct Scsi_Host *instance = cmd->device->host;
242511d2f63bSFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
242611d2f63bSFinn Thain 	unsigned long flags;
24271da177e4SLinus Torvalds 
242811d2f63bSFinn Thain 	spin_lock_irqsave(&hostdata->lock, flags);
24293be1b3eaSFinn Thain 
24303be1b3eaSFinn Thain #if (NDEBUG & NDEBUG_ANY)
24313be1b3eaSFinn Thain 	scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
24323be1b3eaSFinn Thain #endif
2433e5c3fddfSFinn Thain 	NCR5380_dprint(NDEBUG_ANY, instance);
2434e5c3fddfSFinn Thain 	NCR5380_dprint_phase(NDEBUG_ANY, instance);
24353be1b3eaSFinn Thain 
243668b3aa7cSJeff Garzik  	do_reset(instance);
24373be1b3eaSFinn Thain 
243811d2f63bSFinn Thain 	spin_unlock_irqrestore(&hostdata->lock, flags);
243968b3aa7cSJeff Garzik  
24401da177e4SLinus Torvalds 	return SUCCESS;
24411da177e4SLinus Torvalds }
2442