xref: /linux/drivers/scsi/NCR5380.c (revision ff1269cb3d978655fb4f61f87d08a46af9854567)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * NCR 5380 generic driver routines.  These should make it *trivial*
41da177e4SLinus Torvalds  * to implement 5380 SCSI drivers under Linux with a non-trantor
51da177e4SLinus Torvalds  * architecture.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Note that these routines also work with NR53c400 family chips.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Copyright 1993, Drew Eckhardt
101da177e4SLinus Torvalds  * Visionary Computing
111da177e4SLinus Torvalds  * (Unix and Linux consulting and custom programming)
121da177e4SLinus Torvalds  * drew@colorado.edu
131da177e4SLinus Torvalds  * +1 (303) 666-5836
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * For more information, please consult
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * NCR 5380 Family
181da177e4SLinus Torvalds  * SCSI Protocol Controller
191da177e4SLinus Torvalds  * Databook
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * NCR Microelectronics
221da177e4SLinus Torvalds  * 1635 Aeroplaza Drive
231da177e4SLinus Torvalds  * Colorado Springs, CO 80916
241da177e4SLinus Torvalds  * 1+ (719) 578-3400
251da177e4SLinus Torvalds  * 1+ (800) 334-5454
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*
29c16df32eSFinn Thain  * With contributions from Ray Van Tassle, Ingmar Baumgart,
30c16df32eSFinn Thain  * Ronald van Cuijlenborg, Alan Cox and others.
311da177e4SLinus Torvalds  */
321da177e4SLinus Torvalds 
3352d3e561SFinn Thain /* Ported to Atari by Roman Hodek and others. */
3452d3e561SFinn Thain 
35e9db3198SFinn Thain /* Adapted for the Sun 3 by Sam Creasey. */
36e9db3198SFinn Thain 
371da177e4SLinus Torvalds /*
381da177e4SLinus Torvalds  * Design
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  * This is a generic 5380 driver.  To use it on a different platform,
411da177e4SLinus Torvalds  * one simply writes appropriate system specific macros (ie, data
421da177e4SLinus Torvalds  * transfer - some PC's will use the I/O bus, 68K's must use
431da177e4SLinus Torvalds  * memory mapped) and drops this file in their 'C' wrapper.
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * As far as command queueing, two queues are maintained for
461da177e4SLinus Torvalds  * each 5380 in the system - commands that haven't been issued yet,
471da177e4SLinus Torvalds  * and commands that are currently executing.  This means that an
481da177e4SLinus Torvalds  * unlimited number of commands may be queued, letting
491da177e4SLinus Torvalds  * more commands propagate from the higher driver levels giving higher
501da177e4SLinus Torvalds  * throughput.  Note that both I_T_L and I_T_L_Q nexuses are supported,
511da177e4SLinus Torvalds  * allowing multiple commands to propagate all the way to a SCSI-II device
521da177e4SLinus Torvalds  * while a command is already executing.
531da177e4SLinus Torvalds  *
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * Issues specific to the NCR5380 :
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
581da177e4SLinus Torvalds  * piece of hardware that requires you to sit in a loop polling for
591da177e4SLinus Torvalds  * the REQ signal as long as you are connected.  Some devices are
601da177e4SLinus Torvalds  * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
61686f3990SFinn Thain  * while doing long seek operations. [...] These
621da177e4SLinus Torvalds  * broken devices are the exception rather than the rule and I'd rather
631da177e4SLinus Torvalds  * spend my time optimizing for the normal case.
641da177e4SLinus Torvalds  *
651da177e4SLinus Torvalds  * Architecture :
661da177e4SLinus Torvalds  *
671da177e4SLinus Torvalds  * At the heart of the design is a coroutine, NCR5380_main,
681da177e4SLinus Torvalds  * which is started from a workqueue for each NCR5380 host in the
691da177e4SLinus Torvalds  * system.  It attempts to establish I_T_L or I_T_L_Q nexuses by
701da177e4SLinus Torvalds  * removing the commands from the issue queue and calling
711da177e4SLinus Torvalds  * NCR5380_select() if a nexus is not established.
721da177e4SLinus Torvalds  *
731da177e4SLinus Torvalds  * Once a nexus is established, the NCR5380_information_transfer()
741da177e4SLinus Torvalds  * phase goes through the various phases as instructed by the target.
751da177e4SLinus Torvalds  * if the target goes into MSG IN and sends a DISCONNECT message,
761da177e4SLinus Torvalds  * the command structure is placed into the per instance disconnected
771da177e4SLinus Torvalds  * queue, and NCR5380_main tries to find more work.  If the target is
781da177e4SLinus Torvalds  * idle for too long, the system will try to sleep.
791da177e4SLinus Torvalds  *
801da177e4SLinus Torvalds  * If a command has disconnected, eventually an interrupt will trigger,
811da177e4SLinus Torvalds  * calling NCR5380_intr()  which will in turn call NCR5380_reselect
821da177e4SLinus Torvalds  * to reestablish a nexus.  This will run main if necessary.
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * On command termination, the done function will be called as
851da177e4SLinus Torvalds  * appropriate.
861da177e4SLinus Torvalds  *
87*ff1269cbSFinn Thain  * The command data pointer is initialized after the command is connected
881da177e4SLinus Torvalds  * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
891da177e4SLinus Torvalds  * Note that in violation of the standard, an implicit SAVE POINTERS operation
901da177e4SLinus Torvalds  * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
911da177e4SLinus Torvalds  */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /*
941da177e4SLinus Torvalds  * Using this file :
951da177e4SLinus Torvalds  * This file a skeleton Linux SCSI driver for the NCR 5380 series
961da177e4SLinus Torvalds  * of chips.  To use it, you write an architecture specific functions
971da177e4SLinus Torvalds  * and macros and include this file in your driver.
981da177e4SLinus Torvalds  *
991da177e4SLinus Torvalds  * These macros MUST be defined :
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * NCR5380_read(register)  - read from the specified register
1021da177e4SLinus Torvalds  *
1031da177e4SLinus Torvalds  * NCR5380_write(register, value) - write to the specific register
1041da177e4SLinus Torvalds  *
1051da177e4SLinus Torvalds  * NCR5380_implementation_fields  - additional fields needed for this
1061da177e4SLinus Torvalds  * specific implementation of the NCR5380
1071da177e4SLinus Torvalds  *
1081da177e4SLinus Torvalds  * Either real DMA *or* pseudo DMA may be implemented
1091da177e4SLinus Torvalds  *
1104a98f896SFinn Thain  * NCR5380_dma_xfer_len   - determine size of DMA/PDMA transfer
1114a98f896SFinn Thain  * NCR5380_dma_send_setup - execute DMA/PDMA from memory to 5380
1124a98f896SFinn Thain  * NCR5380_dma_recv_setup - execute DMA/PDMA from 5380 to memory
1134a98f896SFinn Thain  * NCR5380_dma_residual   - residual byte count
1141da177e4SLinus Torvalds  *
1151da177e4SLinus Torvalds  * The generic driver is initialized by calling NCR5380_init(instance),
116906e4a3cSOndrej Zary  * after setting the appropriate host specific fields and ID.
1171da177e4SLinus Torvalds  */
1181da177e4SLinus Torvalds 
119e5d55d1aSFinn Thain #ifndef NCR5380_io_delay
120e5d55d1aSFinn Thain #define NCR5380_io_delay(x)
121e5d55d1aSFinn Thain #endif
122e5d55d1aSFinn Thain 
12352d3e561SFinn Thain #ifndef NCR5380_acquire_dma_irq
12452d3e561SFinn Thain #define NCR5380_acquire_dma_irq(x)	(1)
12552d3e561SFinn Thain #endif
12652d3e561SFinn Thain 
12752d3e561SFinn Thain #ifndef NCR5380_release_dma_irq
12852d3e561SFinn Thain #define NCR5380_release_dma_irq(x)
12952d3e561SFinn Thain #endif
13052d3e561SFinn Thain 
1310b7a2235SFinn Thain static unsigned int disconnect_mask = ~0;
1320b7a2235SFinn Thain module_param(disconnect_mask, int, 0444);
1330b7a2235SFinn Thain 
134e7734ef1SAhmed S. Darwish static int do_abort(struct Scsi_Host *, unsigned int);
13554d8fe44SFinn Thain static void do_reset(struct Scsi_Host *);
1366b0e87a6SFinn Thain static void bus_reset_cleanup(struct Scsi_Host *);
1371da177e4SLinus Torvalds 
138c16df32eSFinn Thain /**
1391da177e4SLinus Torvalds  * initialize_SCp - init the scsi pointer field
1401da177e4SLinus Torvalds  * @cmd: command block to set up
1411da177e4SLinus Torvalds  *
1421da177e4SLinus Torvalds  * Set up the internal fields in the SCSI command.
1431da177e4SLinus Torvalds  */
1441da177e4SLinus Torvalds 
145710ddd0dSFinn Thain static inline void initialize_SCp(struct scsi_cmnd *cmd)
1461da177e4SLinus Torvalds {
147*ff1269cbSFinn Thain 	struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(cmd);
1481da177e4SLinus Torvalds 
1499e0fe44dSBoaz Harrosh 	if (scsi_bufflen(cmd)) {
150*ff1269cbSFinn Thain 		ncmd->buffer = scsi_sglist(cmd);
151*ff1269cbSFinn Thain 		ncmd->ptr = sg_virt(ncmd->buffer);
152*ff1269cbSFinn Thain 		ncmd->this_residual = ncmd->buffer->length;
1531da177e4SLinus Torvalds 	} else {
154*ff1269cbSFinn Thain 		ncmd->buffer = NULL;
155*ff1269cbSFinn Thain 		ncmd->ptr = NULL;
156*ff1269cbSFinn Thain 		ncmd->this_residual = 0;
1571da177e4SLinus Torvalds 	}
158f27db8ebSFinn Thain 
159*ff1269cbSFinn Thain 	ncmd->status = 0;
160*ff1269cbSFinn Thain 	ncmd->message = 0;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
163*ff1269cbSFinn Thain static inline void advance_sg_buffer(struct NCR5380_cmd *ncmd)
1640e9fdd2bSFinn Thain {
165*ff1269cbSFinn Thain 	struct scatterlist *s = ncmd->buffer;
1660e9fdd2bSFinn Thain 
167*ff1269cbSFinn Thain 	if (!ncmd->this_residual && s && !sg_is_last(s)) {
168*ff1269cbSFinn Thain 		ncmd->buffer = sg_next(s);
169*ff1269cbSFinn Thain 		ncmd->ptr = sg_virt(ncmd->buffer);
170*ff1269cbSFinn Thain 		ncmd->this_residual = ncmd->buffer->length;
1710e9fdd2bSFinn Thain 	}
1720e9fdd2bSFinn Thain }
1730e9fdd2bSFinn Thain 
174350767f2SFinn Thain static inline void set_resid_from_SCp(struct scsi_cmnd *cmd)
175350767f2SFinn Thain {
176*ff1269cbSFinn Thain 	struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(cmd);
177*ff1269cbSFinn Thain 	int resid = ncmd->this_residual;
178*ff1269cbSFinn Thain 	struct scatterlist *s = ncmd->buffer;
179350767f2SFinn Thain 
180350767f2SFinn Thain 	if (s)
181350767f2SFinn Thain 		while (!sg_is_last(s)) {
182350767f2SFinn Thain 			s = sg_next(s);
183350767f2SFinn Thain 			resid += s->length;
184350767f2SFinn Thain 		}
185350767f2SFinn Thain 	scsi_set_resid(cmd, resid);
186350767f2SFinn Thain }
187350767f2SFinn Thain 
1881da177e4SLinus Torvalds /**
189b32ade12SFinn Thain  * NCR5380_poll_politely2 - wait for two chip register values
190d5d37a0aSFinn Thain  * @hostdata: host private data
191b32ade12SFinn Thain  * @reg1: 5380 register to poll
192b32ade12SFinn Thain  * @bit1: Bitmask to check
193b32ade12SFinn Thain  * @val1: Expected value
194b32ade12SFinn Thain  * @reg2: Second 5380 register to poll
195b32ade12SFinn Thain  * @bit2: Second bitmask to check
196b32ade12SFinn Thain  * @val2: Second expected value
197e7734ef1SAhmed S. Darwish  * @wait: Time-out in jiffies, 0 if sleeping is not allowed
1981da177e4SLinus Torvalds  *
1992f854b82SFinn Thain  * Polls the chip in a reasonably efficient manner waiting for an
2002f854b82SFinn Thain  * event to occur. After a short quick poll we begin to yield the CPU
2012f854b82SFinn Thain  * (if possible). In irq contexts the time-out is arbitrarily limited.
2022f854b82SFinn Thain  * Callers may hold locks as long as they are held in irq mode.
2031da177e4SLinus Torvalds  *
204b32ade12SFinn Thain  * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
2051da177e4SLinus Torvalds  */
2061da177e4SLinus Torvalds 
207d5d37a0aSFinn Thain static int NCR5380_poll_politely2(struct NCR5380_hostdata *hostdata,
20861e1ce58SFinn Thain                                   unsigned int reg1, u8 bit1, u8 val1,
20961e1ce58SFinn Thain                                   unsigned int reg2, u8 bit2, u8 val2,
21061e1ce58SFinn Thain                                   unsigned long wait)
2111da177e4SLinus Torvalds {
212d4408dd7SFinn Thain 	unsigned long n = hostdata->poll_loops;
2132f854b82SFinn Thain 	unsigned long deadline = jiffies + wait;
2141da177e4SLinus Torvalds 
2152f854b82SFinn Thain 	do {
216b32ade12SFinn Thain 		if ((NCR5380_read(reg1) & bit1) == val1)
217b32ade12SFinn Thain 			return 0;
218b32ade12SFinn Thain 		if ((NCR5380_read(reg2) & bit2) == val2)
2191da177e4SLinus Torvalds 			return 0;
2201da177e4SLinus Torvalds 		cpu_relax();
2212f854b82SFinn Thain 	} while (n--);
2222f854b82SFinn Thain 
223e7734ef1SAhmed S. Darwish 	if (!wait)
2242f854b82SFinn Thain 		return -ETIMEDOUT;
2252f854b82SFinn Thain 
2262f854b82SFinn Thain 	/* Repeatedly sleep for 1 ms until deadline */
2272f854b82SFinn Thain 	while (time_is_after_jiffies(deadline)) {
2282f854b82SFinn Thain 		schedule_timeout_uninterruptible(1);
229b32ade12SFinn Thain 		if ((NCR5380_read(reg1) & bit1) == val1)
230b32ade12SFinn Thain 			return 0;
231b32ade12SFinn Thain 		if ((NCR5380_read(reg2) & bit2) == val2)
2322f854b82SFinn Thain 			return 0;
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	return -ETIMEDOUT;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
238185a7a1cSviro@ZenIV.linux.org.uk #if NDEBUG
2391da177e4SLinus Torvalds static struct {
2401da177e4SLinus Torvalds 	unsigned char mask;
2411da177e4SLinus Torvalds 	const char *name;
2421da177e4SLinus Torvalds } signals[] = {
2431da177e4SLinus Torvalds 	{SR_DBP, "PARITY"},
2441da177e4SLinus Torvalds 	{SR_RST, "RST"},
2451da177e4SLinus Torvalds 	{SR_BSY, "BSY"},
2461da177e4SLinus Torvalds 	{SR_REQ, "REQ"},
2471da177e4SLinus Torvalds 	{SR_MSG, "MSG"},
2481da177e4SLinus Torvalds 	{SR_CD, "CD"},
2491da177e4SLinus Torvalds 	{SR_IO, "IO"},
2501da177e4SLinus Torvalds 	{SR_SEL, "SEL"},
2511da177e4SLinus Torvalds 	{0, NULL}
2521da177e4SLinus Torvalds },
2531da177e4SLinus Torvalds basrs[] = {
25412866b99SFinn Thain 	{BASR_END_DMA_TRANSFER, "END OF DMA"},
25512866b99SFinn Thain 	{BASR_DRQ, "DRQ"},
25612866b99SFinn Thain 	{BASR_PARITY_ERROR, "PARITY ERROR"},
25712866b99SFinn Thain 	{BASR_IRQ, "IRQ"},
25812866b99SFinn Thain 	{BASR_PHASE_MATCH, "PHASE MATCH"},
25912866b99SFinn Thain 	{BASR_BUSY_ERROR, "BUSY ERROR"},
2601da177e4SLinus Torvalds 	{BASR_ATN, "ATN"},
2611da177e4SLinus Torvalds 	{BASR_ACK, "ACK"},
2621da177e4SLinus Torvalds 	{0, NULL}
2631da177e4SLinus Torvalds },
2641da177e4SLinus Torvalds icrs[] = {
2651da177e4SLinus Torvalds 	{ICR_ASSERT_RST, "ASSERT RST"},
26612866b99SFinn Thain 	{ICR_ARBITRATION_PROGRESS, "ARB. IN PROGRESS"},
26712866b99SFinn Thain 	{ICR_ARBITRATION_LOST, "LOST ARB."},
2681da177e4SLinus Torvalds 	{ICR_ASSERT_ACK, "ASSERT ACK"},
2691da177e4SLinus Torvalds 	{ICR_ASSERT_BSY, "ASSERT BSY"},
2701da177e4SLinus Torvalds 	{ICR_ASSERT_SEL, "ASSERT SEL"},
2711da177e4SLinus Torvalds 	{ICR_ASSERT_ATN, "ASSERT ATN"},
2721da177e4SLinus Torvalds 	{ICR_ASSERT_DATA, "ASSERT DATA"},
2731da177e4SLinus Torvalds 	{0, NULL}
2741da177e4SLinus Torvalds },
2751da177e4SLinus Torvalds mrs[] = {
27612866b99SFinn Thain 	{MR_BLOCK_DMA_MODE, "BLOCK DMA MODE"},
27712866b99SFinn Thain 	{MR_TARGET, "TARGET"},
27812866b99SFinn Thain 	{MR_ENABLE_PAR_CHECK, "PARITY CHECK"},
27912866b99SFinn Thain 	{MR_ENABLE_PAR_INTR, "PARITY INTR"},
28012866b99SFinn Thain 	{MR_ENABLE_EOP_INTR, "EOP INTR"},
28112866b99SFinn Thain 	{MR_MONITOR_BSY, "MONITOR BSY"},
28212866b99SFinn Thain 	{MR_DMA_MODE, "DMA MODE"},
28312866b99SFinn Thain 	{MR_ARBITRATE, "ARBITRATE"},
2841da177e4SLinus Torvalds 	{0, NULL}
2851da177e4SLinus Torvalds };
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds /**
2881da177e4SLinus Torvalds  * NCR5380_print - print scsi bus signals
2891da177e4SLinus Torvalds  * @instance: adapter state to dump
2901da177e4SLinus Torvalds  *
2911da177e4SLinus Torvalds  * Print the SCSI bus signals for debugging purposes
2921da177e4SLinus Torvalds  */
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static void NCR5380_print(struct Scsi_Host *instance)
2951da177e4SLinus Torvalds {
29661e1ce58SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
2978cee3e16SFinn Thain 	unsigned char status, basr, mr, icr, i;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	status = NCR5380_read(STATUS_REG);
3001da177e4SLinus Torvalds 	mr = NCR5380_read(MODE_REG);
3011da177e4SLinus Torvalds 	icr = NCR5380_read(INITIATOR_COMMAND_REG);
3021da177e4SLinus Torvalds 	basr = NCR5380_read(BUS_AND_STATUS_REG);
3031da177e4SLinus Torvalds 
30412866b99SFinn Thain 	printk(KERN_DEBUG "SR =   0x%02x : ", status);
3051da177e4SLinus Torvalds 	for (i = 0; signals[i].mask; ++i)
3061da177e4SLinus Torvalds 		if (status & signals[i].mask)
30712866b99SFinn Thain 			printk(KERN_CONT "%s, ", signals[i].name);
30812866b99SFinn Thain 	printk(KERN_CONT "\nBASR = 0x%02x : ", basr);
3091da177e4SLinus Torvalds 	for (i = 0; basrs[i].mask; ++i)
3101da177e4SLinus Torvalds 		if (basr & basrs[i].mask)
31112866b99SFinn Thain 			printk(KERN_CONT "%s, ", basrs[i].name);
31212866b99SFinn Thain 	printk(KERN_CONT "\nICR =  0x%02x : ", icr);
3131da177e4SLinus Torvalds 	for (i = 0; icrs[i].mask; ++i)
3141da177e4SLinus Torvalds 		if (icr & icrs[i].mask)
31512866b99SFinn Thain 			printk(KERN_CONT "%s, ", icrs[i].name);
31612866b99SFinn Thain 	printk(KERN_CONT "\nMR =   0x%02x : ", mr);
3171da177e4SLinus Torvalds 	for (i = 0; mrs[i].mask; ++i)
3181da177e4SLinus Torvalds 		if (mr & mrs[i].mask)
31912866b99SFinn Thain 			printk(KERN_CONT "%s, ", mrs[i].name);
32012866b99SFinn Thain 	printk(KERN_CONT "\n");
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3230d2cf867SFinn Thain static struct {
3240d2cf867SFinn Thain 	unsigned char value;
3250d2cf867SFinn Thain 	const char *name;
3260d2cf867SFinn Thain } phases[] = {
3270d2cf867SFinn Thain 	{PHASE_DATAOUT, "DATAOUT"},
3280d2cf867SFinn Thain 	{PHASE_DATAIN, "DATAIN"},
3290d2cf867SFinn Thain 	{PHASE_CMDOUT, "CMDOUT"},
3300d2cf867SFinn Thain 	{PHASE_STATIN, "STATIN"},
3310d2cf867SFinn Thain 	{PHASE_MSGOUT, "MSGOUT"},
3320d2cf867SFinn Thain 	{PHASE_MSGIN, "MSGIN"},
3330d2cf867SFinn Thain 	{PHASE_UNKNOWN, "UNKNOWN"}
3340d2cf867SFinn Thain };
3351da177e4SLinus Torvalds 
336c16df32eSFinn Thain /**
3371da177e4SLinus Torvalds  * NCR5380_print_phase - show SCSI phase
3381da177e4SLinus Torvalds  * @instance: adapter to dump
3391da177e4SLinus Torvalds  *
3401da177e4SLinus Torvalds  * Print the current SCSI phase for debugging purposes
3411da177e4SLinus Torvalds  */
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds static void NCR5380_print_phase(struct Scsi_Host *instance)
3441da177e4SLinus Torvalds {
34561e1ce58SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
3461da177e4SLinus Torvalds 	unsigned char status;
3471da177e4SLinus Torvalds 	int i;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	status = NCR5380_read(STATUS_REG);
3501da177e4SLinus Torvalds 	if (!(status & SR_REQ))
3516a6ff4acSFinn Thain 		shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
3521da177e4SLinus Torvalds 	else {
3530d2cf867SFinn Thain 		for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
3540d2cf867SFinn Thain 		     (phases[i].value != (status & PHASE_MASK)); ++i)
3550d2cf867SFinn Thain 			;
3566a6ff4acSFinn Thain 		shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds #endif
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds /**
36209028461SFinn Thain  * NCR5380_info - report driver and host information
3638c32513bSFinn Thain  * @instance: relevant scsi host instance
3641da177e4SLinus Torvalds  *
3658c32513bSFinn Thain  * For use as the host template info() handler.
3661da177e4SLinus Torvalds  */
3671da177e4SLinus Torvalds 
3688c32513bSFinn Thain static const char *NCR5380_info(struct Scsi_Host *instance)
3691da177e4SLinus Torvalds {
3708c32513bSFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
3718c32513bSFinn Thain 
3728c32513bSFinn Thain 	return hostdata->info;
3738c32513bSFinn Thain }
3748c32513bSFinn Thain 
3751da177e4SLinus Torvalds /**
3761da177e4SLinus Torvalds  * NCR5380_init - initialise an NCR5380
3771da177e4SLinus Torvalds  * @instance: adapter to configure
3781da177e4SLinus Torvalds  * @flags: control flags
3791da177e4SLinus Torvalds  *
3801da177e4SLinus Torvalds  * Initializes *instance and corresponding 5380 chip,
3811da177e4SLinus Torvalds  * with flags OR'd into the initial flags value.
3821da177e4SLinus Torvalds  *
3831da177e4SLinus Torvalds  * Notes : I assume that the host, hostno, and id bits have been
3841da177e4SLinus Torvalds  * set correctly. I don't care about the irq and other fields.
3851da177e4SLinus Torvalds  *
3861da177e4SLinus Torvalds  * Returns 0 for success
3871da177e4SLinus Torvalds  */
3881da177e4SLinus Torvalds 
3896f039790SGreg Kroah-Hartman static int NCR5380_init(struct Scsi_Host *instance, int flags)
3901da177e4SLinus Torvalds {
391e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
392b6488f97SFinn Thain 	int i;
3932f854b82SFinn Thain 	unsigned long deadline;
394d4408dd7SFinn Thain 	unsigned long accesses_per_ms;
3951da177e4SLinus Torvalds 
396ae5e33afSFinn Thain 	instance->max_lun = 7;
397ae5e33afSFinn Thain 
3980d2cf867SFinn Thain 	hostdata->host = instance;
3991da177e4SLinus Torvalds 	hostdata->id_mask = 1 << instance->this_id;
4000d2cf867SFinn Thain 	hostdata->id_higher_mask = 0;
4011da177e4SLinus Torvalds 	for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
4021da177e4SLinus Torvalds 		if (i > hostdata->id_mask)
4031da177e4SLinus Torvalds 			hostdata->id_higher_mask |= i;
4041da177e4SLinus Torvalds 	for (i = 0; i < 8; ++i)
4051da177e4SLinus Torvalds 		hostdata->busy[i] = 0;
406e4dec680SFinn Thain 	hostdata->dma_len = 0;
407e4dec680SFinn Thain 
40811d2f63bSFinn Thain 	spin_lock_init(&hostdata->lock);
4091da177e4SLinus Torvalds 	hostdata->connected = NULL;
410f27db8ebSFinn Thain 	hostdata->sensing = NULL;
411f27db8ebSFinn Thain 	INIT_LIST_HEAD(&hostdata->autosense);
41232b26a10SFinn Thain 	INIT_LIST_HEAD(&hostdata->unissued);
41332b26a10SFinn Thain 	INIT_LIST_HEAD(&hostdata->disconnected);
41432b26a10SFinn Thain 
41555181be8SFinn Thain 	hostdata->flags = flags;
4161da177e4SLinus Torvalds 
4178d8601a7SFinn Thain 	INIT_WORK(&hostdata->main_task, NCR5380_main);
4180ad0eff9SFinn Thain 	hostdata->work_q = alloc_workqueue("ncr5380_%d",
4190ad0eff9SFinn Thain 	                        WQ_UNBOUND | WQ_MEM_RECLAIM,
4200ad0eff9SFinn Thain 	                        1, instance->host_no);
4210ad0eff9SFinn Thain 	if (!hostdata->work_q)
4220ad0eff9SFinn Thain 		return -ENOMEM;
4231da177e4SLinus Torvalds 
42409028461SFinn Thain 	snprintf(hostdata->info, sizeof(hostdata->info),
42509028461SFinn Thain 		"%s, irq %d, io_port 0x%lx, base 0x%lx, can_queue %d, cmd_per_lun %d, sg_tablesize %d, this_id %d, flags { %s%s%s}",
42609028461SFinn Thain 		instance->hostt->name, instance->irq, hostdata->io_port,
42709028461SFinn Thain 		hostdata->base, instance->can_queue, instance->cmd_per_lun,
42809028461SFinn Thain 		instance->sg_tablesize, instance->this_id,
42909028461SFinn Thain 		hostdata->flags & FLAG_DMA_FIXUP     ? "DMA_FIXUP "     : "",
43009028461SFinn Thain 		hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
43109028461SFinn Thain 		hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "");
4328c32513bSFinn Thain 
4331da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
4341da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
4351da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, 0);
4361da177e4SLinus Torvalds 	NCR5380_write(SELECT_ENABLE_REG, 0);
4372f854b82SFinn Thain 
4382f854b82SFinn Thain 	/* Calibrate register polling loop */
4392f854b82SFinn Thain 	i = 0;
4402f854b82SFinn Thain 	deadline = jiffies + 1;
4412f854b82SFinn Thain 	do {
4422f854b82SFinn Thain 		cpu_relax();
4432f854b82SFinn Thain 	} while (time_is_after_jiffies(deadline));
4442f854b82SFinn Thain 	deadline += msecs_to_jiffies(256);
4452f854b82SFinn Thain 	do {
4462f854b82SFinn Thain 		NCR5380_read(STATUS_REG);
4472f854b82SFinn Thain 		++i;
4482f854b82SFinn Thain 		cpu_relax();
4492f854b82SFinn Thain 	} while (time_is_after_jiffies(deadline));
450d4408dd7SFinn Thain 	accesses_per_ms = i / 256;
451d4408dd7SFinn Thain 	hostdata->poll_loops = NCR5380_REG_POLL_TIME * accesses_per_ms / 2;
4522f854b82SFinn Thain 
453b6488f97SFinn Thain 	return 0;
454b6488f97SFinn Thain }
4551da177e4SLinus Torvalds 
456b6488f97SFinn Thain /**
457b6488f97SFinn Thain  * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
458b6488f97SFinn Thain  * @instance: adapter to check
4591da177e4SLinus Torvalds  *
460b6488f97SFinn Thain  * If the system crashed, it may have crashed with a connected target and
461b6488f97SFinn Thain  * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
462b6488f97SFinn Thain  * currently established nexus, which we know nothing about. Failing that
463b6488f97SFinn Thain  * do a bus reset.
4641da177e4SLinus Torvalds  *
465b6488f97SFinn Thain  * Note that a bus reset will cause the chip to assert IRQ.
466b6488f97SFinn Thain  *
467b6488f97SFinn Thain  * Returns 0 if successful, otherwise -ENXIO.
4681da177e4SLinus Torvalds  */
4691da177e4SLinus Torvalds 
470b6488f97SFinn Thain static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
471b6488f97SFinn Thain {
4729c3f0e2bSFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
473b6488f97SFinn Thain 	int pass;
474b6488f97SFinn Thain 
4751da177e4SLinus Torvalds 	for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
4761da177e4SLinus Torvalds 		switch (pass) {
4771da177e4SLinus Torvalds 		case 1:
4781da177e4SLinus Torvalds 		case 3:
4791da177e4SLinus Torvalds 		case 5:
480636b1ec8SFinn Thain 			shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
481d5d37a0aSFinn Thain 			NCR5380_poll_politely(hostdata,
482636b1ec8SFinn Thain 			                      STATUS_REG, SR_BSY, 0, 5 * HZ);
4831da177e4SLinus Torvalds 			break;
4841da177e4SLinus Torvalds 		case 2:
485636b1ec8SFinn Thain 			shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
486e7734ef1SAhmed S. Darwish 			do_abort(instance, 1);
4871da177e4SLinus Torvalds 			break;
4881da177e4SLinus Torvalds 		case 4:
489636b1ec8SFinn Thain 			shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
4901da177e4SLinus Torvalds 			do_reset(instance);
4919c3f0e2bSFinn Thain 			/* Wait after a reset; the SCSI standard calls for
4929c3f0e2bSFinn Thain 			 * 250ms, we wait 500ms to be on the safe side.
4939c3f0e2bSFinn Thain 			 * But some Toshiba CD-ROMs need ten times that.
4949c3f0e2bSFinn Thain 			 */
4959c3f0e2bSFinn Thain 			if (hostdata->flags & FLAG_TOSHIBA_DELAY)
4969c3f0e2bSFinn Thain 				msleep(2500);
4979c3f0e2bSFinn Thain 			else
4989c3f0e2bSFinn Thain 				msleep(500);
4991da177e4SLinus Torvalds 			break;
5001da177e4SLinus Torvalds 		case 6:
501636b1ec8SFinn Thain 			shost_printk(KERN_ERR, instance, "bus locked solid\n");
5021da177e4SLinus Torvalds 			return -ENXIO;
5031da177e4SLinus Torvalds 		}
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 	return 0;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds /**
5091da177e4SLinus Torvalds  * NCR5380_exit - remove an NCR5380
5101da177e4SLinus Torvalds  * @instance: adapter to remove
5110d2cf867SFinn Thain  *
5120d2cf867SFinn Thain  * Assumes that no more work can be queued (e.g. by NCR5380_intr).
5131da177e4SLinus Torvalds  */
5141da177e4SLinus Torvalds 
515a43cf0f3SRandy Dunlap static void NCR5380_exit(struct Scsi_Host *instance)
5161da177e4SLinus Torvalds {
517e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
5181da177e4SLinus Torvalds 
5198d8601a7SFinn Thain 	cancel_work_sync(&hostdata->main_task);
5200ad0eff9SFinn Thain 	destroy_workqueue(hostdata->work_q);
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds /**
524677e0194SFinn Thain  * complete_cmd - finish processing a command and return it to the SCSI ML
525677e0194SFinn Thain  * @instance: the host instance
526677e0194SFinn Thain  * @cmd: command to complete
527677e0194SFinn Thain  */
528677e0194SFinn Thain 
529677e0194SFinn Thain static void complete_cmd(struct Scsi_Host *instance,
530677e0194SFinn Thain                          struct scsi_cmnd *cmd)
531677e0194SFinn Thain {
532677e0194SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
533677e0194SFinn Thain 
534677e0194SFinn Thain 	dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
535677e0194SFinn Thain 
536f27db8ebSFinn Thain 	if (hostdata->sensing == cmd) {
537f27db8ebSFinn Thain 		/* Autosense processing ends here */
5383d45cefcSHannes Reinecke 		if (get_status_byte(cmd) != SAM_STAT_GOOD) {
539f27db8ebSFinn Thain 			scsi_eh_restore_cmnd(cmd, &hostdata->ses);
54007035651SFinn Thain 		} else {
541f27db8ebSFinn Thain 			scsi_eh_restore_cmnd(cmd, &hostdata->ses);
542464a00c9SHannes Reinecke 			set_status_byte(cmd, SAM_STAT_CHECK_CONDITION);
54307035651SFinn Thain 		}
544f27db8ebSFinn Thain 		hostdata->sensing = NULL;
545f27db8ebSFinn Thain 	}
546f27db8ebSFinn Thain 
547117cd238SBart Van Assche 	scsi_done(cmd);
548677e0194SFinn Thain }
549677e0194SFinn Thain 
550677e0194SFinn Thain /**
5511da177e4SLinus Torvalds  * NCR5380_queue_command - queue a command
5521bb40589SFinn Thain  * @instance: the relevant SCSI adapter
5531da177e4SLinus Torvalds  * @cmd: SCSI command
5541da177e4SLinus Torvalds  *
5551bb40589SFinn Thain  * cmd is added to the per-instance issue queue, with minor
5561da177e4SLinus Torvalds  * twiddling done to the host specific fields of cmd.  If the
5571da177e4SLinus Torvalds  * main coroutine is not running, it is restarted.
5581da177e4SLinus Torvalds  */
5591da177e4SLinus Torvalds 
5601bb40589SFinn Thain static int NCR5380_queue_command(struct Scsi_Host *instance,
5611bb40589SFinn Thain                                  struct scsi_cmnd *cmd)
5621da177e4SLinus Torvalds {
5631bb40589SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
564*ff1269cbSFinn Thain 	struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(cmd);
5651bb40589SFinn Thain 	unsigned long flags;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds #if (NDEBUG & NDEBUG_NO_WRITE)
5681da177e4SLinus Torvalds 	switch (cmd->cmnd[0]) {
5691da177e4SLinus Torvalds 	case WRITE_6:
5701da177e4SLinus Torvalds 	case WRITE_10:
571dbb6b350SFinn Thain 		shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
5721da177e4SLinus Torvalds 		cmd->result = (DID_ERROR << 16);
573117cd238SBart Van Assche 		scsi_done(cmd);
5741da177e4SLinus Torvalds 		return 0;
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	cmd->result = 0;
5791da177e4SLinus Torvalds 
58011d2f63bSFinn Thain 	spin_lock_irqsave(&hostdata->lock, flags);
5811bb40589SFinn Thain 
58203fe6a64SFinn Thain 	if (!NCR5380_acquire_dma_irq(instance)) {
58303fe6a64SFinn Thain 		spin_unlock_irqrestore(&hostdata->lock, flags);
58403fe6a64SFinn Thain 
58503fe6a64SFinn Thain 		return SCSI_MLQUEUE_HOST_BUSY;
58603fe6a64SFinn Thain 	}
58703fe6a64SFinn Thain 
5881da177e4SLinus Torvalds 	/*
5891da177e4SLinus Torvalds 	 * Insert the cmd into the issue queue. Note that REQUEST SENSE
5901da177e4SLinus Torvalds 	 * commands are added to the head of the queue since any command will
5911da177e4SLinus Torvalds 	 * clear the contingent allegiance condition that exists and the
5921da177e4SLinus Torvalds 	 * sense data is only guaranteed to be valid while the condition exists.
5931da177e4SLinus Torvalds 	 */
5941da177e4SLinus Torvalds 
59532b26a10SFinn Thain 	if (cmd->cmnd[0] == REQUEST_SENSE)
59632b26a10SFinn Thain 		list_add(&ncmd->list, &hostdata->unissued);
59732b26a10SFinn Thain 	else
59832b26a10SFinn Thain 		list_add_tail(&ncmd->list, &hostdata->unissued);
59932b26a10SFinn Thain 
60011d2f63bSFinn Thain 	spin_unlock_irqrestore(&hostdata->lock, flags);
6011bb40589SFinn Thain 
602dbb6b350SFinn Thain 	dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
603dbb6b350SFinn Thain 	         cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	/* Kick off command processing */
6068d8601a7SFinn Thain 	queue_work(hostdata->work_q, &hostdata->main_task);
6071da177e4SLinus Torvalds 	return 0;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
61052d3e561SFinn Thain static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
61152d3e561SFinn Thain {
61252d3e561SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
61352d3e561SFinn Thain 
61452d3e561SFinn Thain 	/* Caller does the locking needed to set & test these data atomically */
61552d3e561SFinn Thain 	if (list_empty(&hostdata->disconnected) &&
61652d3e561SFinn Thain 	    list_empty(&hostdata->unissued) &&
61752d3e561SFinn Thain 	    list_empty(&hostdata->autosense) &&
61852d3e561SFinn Thain 	    !hostdata->connected &&
6194ab2a787SFinn Thain 	    !hostdata->selecting) {
62052d3e561SFinn Thain 		NCR5380_release_dma_irq(instance);
62152d3e561SFinn Thain 	}
6224ab2a787SFinn Thain }
62352d3e561SFinn Thain 
6241da177e4SLinus Torvalds /**
625f27db8ebSFinn Thain  * dequeue_next_cmd - dequeue a command for processing
626f27db8ebSFinn Thain  * @instance: the scsi host instance
627f27db8ebSFinn Thain  *
628f27db8ebSFinn Thain  * Priority is given to commands on the autosense queue. These commands
629f27db8ebSFinn Thain  * need autosense because of a CHECK CONDITION result.
630f27db8ebSFinn Thain  *
631f27db8ebSFinn Thain  * Returns a command pointer if a command is found for a target that is
632f27db8ebSFinn Thain  * not already busy. Otherwise returns NULL.
633f27db8ebSFinn Thain  */
634f27db8ebSFinn Thain 
635f27db8ebSFinn Thain static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
636f27db8ebSFinn Thain {
637f27db8ebSFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
638f27db8ebSFinn Thain 	struct NCR5380_cmd *ncmd;
639f27db8ebSFinn Thain 	struct scsi_cmnd *cmd;
640f27db8ebSFinn Thain 
6418d5dbec3SFinn Thain 	if (hostdata->sensing || list_empty(&hostdata->autosense)) {
642f27db8ebSFinn Thain 		list_for_each_entry(ncmd, &hostdata->unissued, list) {
643f27db8ebSFinn Thain 			cmd = NCR5380_to_scmd(ncmd);
644f27db8ebSFinn Thain 			dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
645f27db8ebSFinn Thain 			         cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
646f27db8ebSFinn Thain 
647f27db8ebSFinn Thain 			if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
648f27db8ebSFinn Thain 				list_del(&ncmd->list);
649f27db8ebSFinn Thain 				dsprintk(NDEBUG_QUEUES, instance,
650f27db8ebSFinn Thain 				         "dequeue: removed %p from issue queue\n", cmd);
651f27db8ebSFinn Thain 				return cmd;
652f27db8ebSFinn Thain 			}
653f27db8ebSFinn Thain 		}
654f27db8ebSFinn Thain 	} else {
655f27db8ebSFinn Thain 		/* Autosense processing begins here */
656f27db8ebSFinn Thain 		ncmd = list_first_entry(&hostdata->autosense,
657f27db8ebSFinn Thain 		                        struct NCR5380_cmd, list);
658f27db8ebSFinn Thain 		list_del(&ncmd->list);
659f27db8ebSFinn Thain 		cmd = NCR5380_to_scmd(ncmd);
660f27db8ebSFinn Thain 		dsprintk(NDEBUG_QUEUES, instance,
661f27db8ebSFinn Thain 		         "dequeue: removed %p from autosense queue\n", cmd);
662f27db8ebSFinn Thain 		scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
663f27db8ebSFinn Thain 		hostdata->sensing = cmd;
664f27db8ebSFinn Thain 		return cmd;
665f27db8ebSFinn Thain 	}
666f27db8ebSFinn Thain 	return NULL;
667f27db8ebSFinn Thain }
668f27db8ebSFinn Thain 
669f27db8ebSFinn Thain static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
670f27db8ebSFinn Thain {
671f27db8ebSFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
672*ff1269cbSFinn Thain 	struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(cmd);
673f27db8ebSFinn Thain 
6748d5dbec3SFinn Thain 	if (hostdata->sensing == cmd) {
675f27db8ebSFinn Thain 		scsi_eh_restore_cmnd(cmd, &hostdata->ses);
676f27db8ebSFinn Thain 		list_add(&ncmd->list, &hostdata->autosense);
677f27db8ebSFinn Thain 		hostdata->sensing = NULL;
678f27db8ebSFinn Thain 	} else
679f27db8ebSFinn Thain 		list_add(&ncmd->list, &hostdata->unissued);
680f27db8ebSFinn Thain }
681f27db8ebSFinn Thain 
682f27db8ebSFinn Thain /**
6831da177e4SLinus Torvalds  * NCR5380_main - NCR state machines
6841da177e4SLinus Torvalds  *
6851da177e4SLinus Torvalds  * NCR5380_main is a coroutine that runs as long as more work can
6861da177e4SLinus Torvalds  * be done on the NCR5380 host adapters in a system.  Both
6871da177e4SLinus Torvalds  * NCR5380_queue_command() and NCR5380_intr() will try to start it
6881da177e4SLinus Torvalds  * in case it is not running.
6891da177e4SLinus Torvalds  */
6901da177e4SLinus Torvalds 
691c4028958SDavid Howells static void NCR5380_main(struct work_struct *work)
6921da177e4SLinus Torvalds {
693c4028958SDavid Howells 	struct NCR5380_hostdata *hostdata =
6948d8601a7SFinn Thain 		container_of(work, struct NCR5380_hostdata, main_task);
6951da177e4SLinus Torvalds 	struct Scsi_Host *instance = hostdata->host;
6961da177e4SLinus Torvalds 	int done;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	do {
6991da177e4SLinus Torvalds 		done = 1;
70011d2f63bSFinn Thain 
7010a4e3612SFinn Thain 		spin_lock_irq(&hostdata->lock);
702ccf6efd7SFinn Thain 		while (!hostdata->connected && !hostdata->selecting) {
703ccf6efd7SFinn Thain 			struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
704ccf6efd7SFinn Thain 
705ccf6efd7SFinn Thain 			if (!cmd)
706ccf6efd7SFinn Thain 				break;
70732b26a10SFinn Thain 
708f27db8ebSFinn Thain 			dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 			/*
7111da177e4SLinus Torvalds 			 * Attempt to establish an I_T_L nexus here.
7121da177e4SLinus Torvalds 			 * On success, instance->hostdata->connected is set.
7131da177e4SLinus Torvalds 			 * On failure, we must add the command back to the
7141da177e4SLinus Torvalds 			 * issue queue so we can keep trying.
7151da177e4SLinus Torvalds 			 */
7161da177e4SLinus Torvalds 			/*
7171da177e4SLinus Torvalds 			 * REQUEST SENSE commands are issued without tagged
7181da177e4SLinus Torvalds 			 * queueing, even on SCSI-II devices because the
7191da177e4SLinus Torvalds 			 * contingent allegiance condition exists for the
7201da177e4SLinus Torvalds 			 * entire unit.
7211da177e4SLinus Torvalds 			 */
72276f13b93SFinn Thain 
723ccf6efd7SFinn Thain 			if (!NCR5380_select(instance, cmd)) {
724707d62b3SFinn Thain 				dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
7251da177e4SLinus Torvalds 			} else {
726f27db8ebSFinn Thain 				dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
727f27db8ebSFinn Thain 				         "main: select failed, returning %p to queue\n", cmd);
728f27db8ebSFinn Thain 				requeue_cmd(instance, cmd);
7291da177e4SLinus Torvalds 			}
730f27db8ebSFinn Thain 		}
731e4dec680SFinn Thain 		if (hostdata->connected && !hostdata->dma_len) {
732b746545fSFinn Thain 			dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
7331da177e4SLinus Torvalds 			NCR5380_information_transfer(instance);
7341da177e4SLinus Torvalds 			done = 0;
7351d3db59dSFinn Thain 		}
736bdd1cc03SFinn Thain 		if (!hostdata->connected) {
73757f31326SFinn Thain 			NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
738bdd1cc03SFinn Thain 			maybe_release_dma_irq(instance);
739bdd1cc03SFinn Thain 		}
74011d2f63bSFinn Thain 		spin_unlock_irq(&hostdata->lock);
7410a4e3612SFinn Thain 		if (!done)
7420a4e3612SFinn Thain 			cond_resched();
7430a4e3612SFinn Thain 	} while (!done);
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7468053b0eeSFinn Thain /*
7478053b0eeSFinn Thain  * NCR5380_dma_complete - finish DMA transfer
7488053b0eeSFinn Thain  * @instance: the scsi host instance
7498053b0eeSFinn Thain  *
7508053b0eeSFinn Thain  * Called by the interrupt handler when DMA finishes or a phase
7518053b0eeSFinn Thain  * mismatch occurs (which would end the DMA transfer).
7528053b0eeSFinn Thain  */
7538053b0eeSFinn Thain 
7548053b0eeSFinn Thain static void NCR5380_dma_complete(struct Scsi_Host *instance)
7558053b0eeSFinn Thain {
7568053b0eeSFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
757*ff1269cbSFinn Thain 	struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(hostdata->connected);
7588053b0eeSFinn Thain 	int transferred;
7598053b0eeSFinn Thain 	unsigned char **data;
7608053b0eeSFinn Thain 	int *count;
7618053b0eeSFinn Thain 	int saved_data = 0, overrun = 0;
7628053b0eeSFinn Thain 	unsigned char p;
7638053b0eeSFinn Thain 
7648053b0eeSFinn Thain 	if (hostdata->read_overruns) {
765*ff1269cbSFinn Thain 		p = ncmd->phase;
7668053b0eeSFinn Thain 		if (p & SR_IO) {
7678053b0eeSFinn Thain 			udelay(10);
7688053b0eeSFinn Thain 			if ((NCR5380_read(BUS_AND_STATUS_REG) &
7698053b0eeSFinn Thain 			     (BASR_PHASE_MATCH | BASR_ACK)) ==
7708053b0eeSFinn Thain 			    (BASR_PHASE_MATCH | BASR_ACK)) {
7718053b0eeSFinn Thain 				saved_data = NCR5380_read(INPUT_DATA_REG);
7728053b0eeSFinn Thain 				overrun = 1;
7738053b0eeSFinn Thain 				dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
7748053b0eeSFinn Thain 			}
7758053b0eeSFinn Thain 		}
7768053b0eeSFinn Thain 	}
7778053b0eeSFinn Thain 
778e9db3198SFinn Thain #ifdef CONFIG_SUN3
7792e4b231aSBart Van Assche 	if (sun3scsi_dma_finish(hostdata->connected->sc_data_direction)) {
780e9db3198SFinn Thain 		pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
781e9db3198SFinn Thain 		       instance->host_no);
782e9db3198SFinn Thain 		BUG();
783e9db3198SFinn Thain 	}
784e9db3198SFinn Thain 
785e9db3198SFinn Thain 	if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
786e9db3198SFinn Thain 	    (BASR_PHASE_MATCH | BASR_ACK)) {
787e9db3198SFinn Thain 		pr_err("scsi%d: BASR %02x\n", instance->host_no,
788e9db3198SFinn Thain 		       NCR5380_read(BUS_AND_STATUS_REG));
789e9db3198SFinn Thain 		pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
790e9db3198SFinn Thain 		       instance->host_no);
791e9db3198SFinn Thain 		BUG();
792e9db3198SFinn Thain 	}
793e9db3198SFinn Thain #endif
794e9db3198SFinn Thain 
7958053b0eeSFinn Thain 	NCR5380_write(MODE_REG, MR_BASE);
7968053b0eeSFinn Thain 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
7978053b0eeSFinn Thain 	NCR5380_read(RESET_PARITY_INTERRUPT_REG);
7988053b0eeSFinn Thain 
7994a98f896SFinn Thain 	transferred = hostdata->dma_len - NCR5380_dma_residual(hostdata);
8008053b0eeSFinn Thain 	hostdata->dma_len = 0;
8018053b0eeSFinn Thain 
802*ff1269cbSFinn Thain 	data = (unsigned char **)&ncmd->ptr;
803*ff1269cbSFinn Thain 	count = &ncmd->this_residual;
8048053b0eeSFinn Thain 	*data += transferred;
8058053b0eeSFinn Thain 	*count -= transferred;
8068053b0eeSFinn Thain 
8078053b0eeSFinn Thain 	if (hostdata->read_overruns) {
8088053b0eeSFinn Thain 		int cnt, toPIO;
8098053b0eeSFinn Thain 
8108053b0eeSFinn Thain 		if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
8118053b0eeSFinn Thain 			cnt = toPIO = hostdata->read_overruns;
8128053b0eeSFinn Thain 			if (overrun) {
8138053b0eeSFinn Thain 				dsprintk(NDEBUG_DMA, instance,
8148053b0eeSFinn Thain 				         "Got an input overrun, using saved byte\n");
8158053b0eeSFinn Thain 				*(*data)++ = saved_data;
8168053b0eeSFinn Thain 				(*count)--;
8178053b0eeSFinn Thain 				cnt--;
8188053b0eeSFinn Thain 				toPIO--;
8198053b0eeSFinn Thain 			}
8208053b0eeSFinn Thain 			if (toPIO > 0) {
8218053b0eeSFinn Thain 				dsprintk(NDEBUG_DMA, instance,
8228053b0eeSFinn Thain 				         "Doing %d byte PIO to 0x%p\n", cnt, *data);
823e7734ef1SAhmed S. Darwish 				NCR5380_transfer_pio(instance, &p, &cnt, data, 0);
8248053b0eeSFinn Thain 				*count -= toPIO - cnt;
8258053b0eeSFinn Thain 			}
8268053b0eeSFinn Thain 		}
8278053b0eeSFinn Thain 	}
8288053b0eeSFinn Thain }
8298053b0eeSFinn Thain 
8301da177e4SLinus Torvalds /**
8311da177e4SLinus Torvalds  * NCR5380_intr - generic NCR5380 irq handler
8321da177e4SLinus Torvalds  * @irq: interrupt number
8331da177e4SLinus Torvalds  * @dev_id: device info
8341da177e4SLinus Torvalds  *
8351da177e4SLinus Torvalds  * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
8361da177e4SLinus Torvalds  * from the disconnected queue, and restarting NCR5380_main()
8371da177e4SLinus Torvalds  * as required.
8381da177e4SLinus Torvalds  *
839cd400825SFinn Thain  * The chip can assert IRQ in any of six different conditions. The IRQ flag
840cd400825SFinn Thain  * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
841cd400825SFinn Thain  * Three of these six conditions are latched in the Bus and Status Register:
842cd400825SFinn Thain  * - End of DMA (cleared by ending DMA Mode)
843cd400825SFinn Thain  * - Parity error (cleared by reading RPIR)
844cd400825SFinn Thain  * - Loss of BSY (cleared by reading RPIR)
845cd400825SFinn Thain  * Two conditions have flag bits that are not latched:
846cd400825SFinn Thain  * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
847cd400825SFinn Thain  * - Bus reset (non-maskable)
848cd400825SFinn Thain  * The remaining condition has no flag bit at all:
849cd400825SFinn Thain  * - Selection/reselection
850cd400825SFinn Thain  *
851cd400825SFinn Thain  * Hence, establishing the cause(s) of any interrupt is partly guesswork.
852cd400825SFinn Thain  * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
853cd400825SFinn Thain  * claimed that "the design of the [DP8490] interrupt logic ensures
854cd400825SFinn Thain  * interrupts will not be lost (they can be on the DP5380)."
855cd400825SFinn Thain  * The L5380/53C80 datasheet from LOGIC Devices has more details.
856cd400825SFinn Thain  *
857cd400825SFinn Thain  * Checking for bus reset by reading RST is futile because of interrupt
858cd400825SFinn Thain  * latency, but a bus reset will reset chip logic. Checking for parity error
859cd400825SFinn Thain  * is unnecessary because that interrupt is never enabled. A Loss of BSY
860cd400825SFinn Thain  * condition will clear DMA Mode. We can tell when this occurs because the
861cd400825SFinn Thain  * the Busy Monitor interrupt is enabled together with DMA Mode.
8621da177e4SLinus Torvalds  */
8631da177e4SLinus Torvalds 
864a46865dcSFinn Thain static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id)
8651da177e4SLinus Torvalds {
866baa9aac6SJeff Garzik 	struct Scsi_Host *instance = dev_id;
867cd400825SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
868cd400825SFinn Thain 	int handled = 0;
8691da177e4SLinus Torvalds 	unsigned char basr;
8701da177e4SLinus Torvalds 	unsigned long flags;
8711da177e4SLinus Torvalds 
87211d2f63bSFinn Thain 	spin_lock_irqsave(&hostdata->lock, flags);
873cd400825SFinn Thain 
8741da177e4SLinus Torvalds 	basr = NCR5380_read(BUS_AND_STATUS_REG);
8751da177e4SLinus Torvalds 	if (basr & BASR_IRQ) {
876cd400825SFinn Thain 		unsigned char mr = NCR5380_read(MODE_REG);
877cd400825SFinn Thain 		unsigned char sr = NCR5380_read(STATUS_REG);
878cd400825SFinn Thain 
879b746545fSFinn Thain 		dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
880b746545fSFinn Thain 		         irq, basr, sr, mr);
881cd400825SFinn Thain 
8828053b0eeSFinn Thain 		if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
8838053b0eeSFinn Thain 			/* Probably End of DMA, Phase Mismatch or Loss of BSY.
8848053b0eeSFinn Thain 			 * We ack IRQ after clearing Mode Register. Workarounds
8858053b0eeSFinn Thain 			 * for End of DMA errata need to happen in DMA Mode.
8868053b0eeSFinn Thain 			 */
8878053b0eeSFinn Thain 
8888053b0eeSFinn Thain 			dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
8898053b0eeSFinn Thain 
8908053b0eeSFinn Thain 			if (hostdata->connected) {
8918053b0eeSFinn Thain 				NCR5380_dma_complete(instance);
8928053b0eeSFinn Thain 				queue_work(hostdata->work_q, &hostdata->main_task);
8938053b0eeSFinn Thain 			} else {
8948053b0eeSFinn Thain 				NCR5380_write(MODE_REG, MR_BASE);
8958053b0eeSFinn Thain 				NCR5380_read(RESET_PARITY_INTERRUPT_REG);
8968053b0eeSFinn Thain 			}
8978053b0eeSFinn Thain 		} else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
898cd400825SFinn Thain 		    (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
899cd400825SFinn Thain 			/* Probably reselected */
900cd400825SFinn Thain 			NCR5380_write(SELECT_ENABLE_REG, 0);
901cd400825SFinn Thain 			NCR5380_read(RESET_PARITY_INTERRUPT_REG);
902cd400825SFinn Thain 
903b746545fSFinn Thain 			dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
904cd400825SFinn Thain 
905cd400825SFinn Thain 			if (!hostdata->connected) {
906cd400825SFinn Thain 				NCR5380_reselect(instance);
9078d8601a7SFinn Thain 				queue_work(hostdata->work_q, &hostdata->main_task);
908cd400825SFinn Thain 			}
909cd400825SFinn Thain 			if (!hostdata->connected)
910cd400825SFinn Thain 				NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
911cd400825SFinn Thain 		} else {
912cd400825SFinn Thain 			/* Probably Bus Reset */
913cd400825SFinn Thain 			NCR5380_read(RESET_PARITY_INTERRUPT_REG);
914cd400825SFinn Thain 
9156b0e87a6SFinn Thain 			if (sr & SR_RST) {
9166b0e87a6SFinn Thain 				/* Certainly Bus Reset */
9176b0e87a6SFinn Thain 				shost_printk(KERN_WARNING, instance,
9186b0e87a6SFinn Thain 					     "bus reset interrupt\n");
9196b0e87a6SFinn Thain 				bus_reset_cleanup(instance);
9206b0e87a6SFinn Thain 			} else {
921b746545fSFinn Thain 				dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
9226b0e87a6SFinn Thain 			}
923e9db3198SFinn Thain #ifdef SUN3_SCSI_VME
924e9db3198SFinn Thain 			dregs->csr |= CSR_DMA_ENABLE;
925e9db3198SFinn Thain #endif
926cd400825SFinn Thain 		}
927cd400825SFinn Thain 		handled = 1;
928cd400825SFinn Thain 	} else {
9299af9fecbSFinn Thain 		dsprintk(NDEBUG_INTR, instance, "interrupt without IRQ bit\n");
930e9db3198SFinn Thain #ifdef SUN3_SCSI_VME
931e9db3198SFinn Thain 		dregs->csr |= CSR_DMA_ENABLE;
932e9db3198SFinn Thain #endif
933cd400825SFinn Thain 	}
934cd400825SFinn Thain 
93511d2f63bSFinn Thain 	spin_unlock_irqrestore(&hostdata->lock, flags);
936cd400825SFinn Thain 
937cd400825SFinn Thain 	return IRQ_RETVAL(handled);
9381da177e4SLinus Torvalds }
9391da177e4SLinus Torvalds 
940dad8261eSFinn Thain /**
941dad8261eSFinn Thain  * NCR5380_select - attempt arbitration and selection for a given command
942dad8261eSFinn Thain  * @instance: the Scsi_Host instance
943dad8261eSFinn Thain  * @cmd: the scsi_cmnd to execute
9441da177e4SLinus Torvalds  *
945dad8261eSFinn Thain  * This routine establishes an I_T_L nexus for a SCSI command. This involves
946dad8261eSFinn Thain  * ARBITRATION, SELECTION and MESSAGE OUT phases and an IDENTIFY message.
9471da177e4SLinus Torvalds  *
948dad8261eSFinn Thain  * Returns true if the operation should be retried.
949dad8261eSFinn Thain  * Returns false if it should not be retried.
9501da177e4SLinus Torvalds  *
9511da177e4SLinus Torvalds  * Side effects :
9521da177e4SLinus Torvalds  * If bus busy, arbitration failed, etc, NCR5380_select() will exit
9531da177e4SLinus Torvalds  * with registers as they should have been on entry - ie
9541da177e4SLinus Torvalds  * SELECT_ENABLE will be set appropriately, the NCR5380
9551da177e4SLinus Torvalds  * will cease to drive any SCSI bus signals.
9561da177e4SLinus Torvalds  *
957dad8261eSFinn Thain  * If successful : the I_T_L nexus will be established, and
958dad8261eSFinn Thain  * hostdata->connected will be set to cmd.
9591da177e4SLinus Torvalds  * SELECT interrupt will be disabled.
9601da177e4SLinus Torvalds  *
961117cd238SBart Van Assche  * If failed (no target) : scsi_done() will be called, and the
9621da177e4SLinus Torvalds  * cmd->result host byte set to DID_BAD_TARGET.
9631da177e4SLinus Torvalds  */
9641da177e4SLinus Torvalds 
965dad8261eSFinn Thain static bool NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
9664ab2a787SFinn Thain 	__releases(&hostdata->lock) __acquires(&hostdata->lock)
9671da177e4SLinus Torvalds {
968e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
9691da177e4SLinus Torvalds 	unsigned char tmp[3], phase;
9701da177e4SLinus Torvalds 	unsigned char *data;
9711da177e4SLinus Torvalds 	int len;
9721da177e4SLinus Torvalds 	int err;
973dad8261eSFinn Thain 	bool ret = true;
9747c8ed783SFinn Thain 	bool can_disconnect = instance->irq != NO_IRQ &&
9750b7a2235SFinn Thain 			      cmd->cmnd[0] != REQUEST_SENSE &&
9760b7a2235SFinn Thain 			      (disconnect_mask & BIT(scmd_id(cmd)));
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	NCR5380_dprint(NDEBUG_ARBITRATION, instance);
979b746545fSFinn Thain 	dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
980b746545fSFinn Thain 	         instance->this_id);
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	/*
983707d62b3SFinn Thain 	 * Arbitration and selection phases are slow and involve dropping the
984707d62b3SFinn Thain 	 * lock, so we have to watch out for EH. An exception handler may
985dad8261eSFinn Thain 	 * change 'selecting' to NULL. This function will then return false
986707d62b3SFinn Thain 	 * so that the caller will forget about 'cmd'. (During information
987707d62b3SFinn Thain 	 * transfer phases, EH may change 'connected' to NULL.)
988707d62b3SFinn Thain 	 */
989707d62b3SFinn Thain 	hostdata->selecting = cmd;
990707d62b3SFinn Thain 
991707d62b3SFinn Thain 	/*
9921da177e4SLinus Torvalds 	 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
9931da177e4SLinus Torvalds 	 * data bus during SELECTION.
9941da177e4SLinus Torvalds 	 */
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, 0);
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	/*
9991da177e4SLinus Torvalds 	 * Start arbitration.
10001da177e4SLinus Torvalds 	 */
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
10031da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_ARBITRATE);
10041da177e4SLinus Torvalds 
100555500d9bSFinn Thain 	/* The chip now waits for BUS FREE phase. Then after the 800 ns
100655500d9bSFinn Thain 	 * Bus Free Delay, arbitration will begin.
10071da177e4SLinus Torvalds 	 */
10081da177e4SLinus Torvalds 
100911d2f63bSFinn Thain 	spin_unlock_irq(&hostdata->lock);
1010d5d37a0aSFinn Thain 	err = NCR5380_poll_politely2(hostdata, MODE_REG, MR_ARBITRATE, 0,
1011b32ade12SFinn Thain 	                INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1012b32ade12SFinn Thain 	                                       ICR_ARBITRATION_PROGRESS, HZ);
101311d2f63bSFinn Thain 	spin_lock_irq(&hostdata->lock);
101455500d9bSFinn Thain 	if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
101555500d9bSFinn Thain 		/* Reselection interrupt */
1016707d62b3SFinn Thain 		goto out;
101755500d9bSFinn Thain 	}
1018ccf6efd7SFinn Thain 	if (!hostdata->selecting) {
1019ccf6efd7SFinn Thain 		/* Command was aborted */
1020ccf6efd7SFinn Thain 		NCR5380_write(MODE_REG, MR_BASE);
1021dad8261eSFinn Thain 		return false;
1022ccf6efd7SFinn Thain 	}
1023b32ade12SFinn Thain 	if (err < 0) {
1024b32ade12SFinn Thain 		NCR5380_write(MODE_REG, MR_BASE);
1025b32ade12SFinn Thain 		shost_printk(KERN_ERR, instance,
1026b32ade12SFinn Thain 		             "select: arbitration timeout\n");
1027707d62b3SFinn Thain 		goto out;
102855500d9bSFinn Thain 	}
102911d2f63bSFinn Thain 	spin_unlock_irq(&hostdata->lock);
103055500d9bSFinn Thain 
103155500d9bSFinn Thain 	/* The SCSI-2 arbitration delay is 2.4 us */
10321da177e4SLinus Torvalds 	udelay(3);
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	/* Check for lost arbitration */
10350d2cf867SFinn Thain 	if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
10360d2cf867SFinn Thain 	    (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
10370d2cf867SFinn Thain 	    (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
10381da177e4SLinus Torvalds 		NCR5380_write(MODE_REG, MR_BASE);
1039b746545fSFinn Thain 		dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
104011d2f63bSFinn Thain 		spin_lock_irq(&hostdata->lock);
1041707d62b3SFinn Thain 		goto out;
10421da177e4SLinus Torvalds 	}
1043cf13b083SFinn Thain 
1044cf13b083SFinn Thain 	/* After/during arbitration, BSY should be asserted.
1045cf13b083SFinn Thain 	 * IBM DPES-31080 Version S31Q works now
1046cf13b083SFinn Thain 	 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1047cf13b083SFinn Thain 	 */
1048cf13b083SFinn Thain 	NCR5380_write(INITIATOR_COMMAND_REG,
1049cf13b083SFinn Thain 		      ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	/*
10521da177e4SLinus Torvalds 	 * Again, bus clear + bus settle time is 1.2us, however, this is
10531da177e4SLinus Torvalds 	 * a minimum so we'll udelay ceil(1.2)
10541da177e4SLinus Torvalds 	 */
10551da177e4SLinus Torvalds 
10569c3f0e2bSFinn Thain 	if (hostdata->flags & FLAG_TOSHIBA_DELAY)
10579c3f0e2bSFinn Thain 		udelay(15);
10589c3f0e2bSFinn Thain 	else
10591da177e4SLinus Torvalds 		udelay(2);
10601da177e4SLinus Torvalds 
106111d2f63bSFinn Thain 	spin_lock_irq(&hostdata->lock);
106211d2f63bSFinn Thain 
106372064a78SFinn Thain 	/* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
106472064a78SFinn Thain 	if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
1065707d62b3SFinn Thain 		goto out;
1066707d62b3SFinn Thain 
1067707d62b3SFinn Thain 	if (!hostdata->selecting) {
1068707d62b3SFinn Thain 		NCR5380_write(MODE_REG, MR_BASE);
1069707d62b3SFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1070dad8261eSFinn Thain 		return false;
1071707d62b3SFinn Thain 	}
107272064a78SFinn Thain 
1073b746545fSFinn Thain 	dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	/*
10761da177e4SLinus Torvalds 	 * Now that we have won arbitration, start Selection process, asserting
10771da177e4SLinus Torvalds 	 * the host and target ID's on the SCSI bus.
10781da177e4SLinus Torvalds 	 */
10791da177e4SLinus Torvalds 
10803d07d22bSFinn Thain 	NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	/*
10831da177e4SLinus Torvalds 	 * Raise ATN while SEL is true before BSY goes false from arbitration,
10841da177e4SLinus Torvalds 	 * since this is the only way to guarantee that we'll get a MESSAGE OUT
10851da177e4SLinus Torvalds 	 * phase immediately after selection.
10861da177e4SLinus Torvalds 	 */
10871da177e4SLinus Torvalds 
10883d07d22bSFinn Thain 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
10893d07d22bSFinn Thain 	              ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
10901da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	/*
10931da177e4SLinus Torvalds 	 * Reselect interrupts must be turned off prior to the dropping of BSY,
10941da177e4SLinus Torvalds 	 * otherwise we will trigger an interrupt.
10951da177e4SLinus Torvalds 	 */
10961da177e4SLinus Torvalds 	NCR5380_write(SELECT_ENABLE_REG, 0);
10971da177e4SLinus Torvalds 
109811d2f63bSFinn Thain 	spin_unlock_irq(&hostdata->lock);
109911d2f63bSFinn Thain 
11001da177e4SLinus Torvalds 	/*
11011da177e4SLinus Torvalds 	 * The initiator shall then wait at least two deskew delays and release
11021da177e4SLinus Torvalds 	 * the BSY signal.
11031da177e4SLinus Torvalds 	 */
11041da177e4SLinus Torvalds 	udelay(1);        /* wingel -- wait two bus deskew delay >2*45ns */
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	/* Reset BSY */
11073d07d22bSFinn Thain 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
11083d07d22bSFinn Thain 	              ICR_ASSERT_ATN | ICR_ASSERT_SEL);
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	/*
11111da177e4SLinus Torvalds 	 * Something weird happens when we cease to drive BSY - looks
11121da177e4SLinus Torvalds 	 * like the board/chip is letting us do another read before the
11131da177e4SLinus Torvalds 	 * appropriate propagation delay has expired, and we're confusing
11141da177e4SLinus Torvalds 	 * a BSY signal from ourselves as the target's response to SELECTION.
11151da177e4SLinus Torvalds 	 *
11161da177e4SLinus Torvalds 	 * A small delay (the 'C++' frontend breaks the pipeline with an
11171da177e4SLinus Torvalds 	 * unnecessary jump, making it work on my 386-33/Trantor T128, the
11181da177e4SLinus Torvalds 	 * tighter 'C' code breaks and requires this) solves the problem -
11191da177e4SLinus Torvalds 	 * the 1 us delay is arbitrary, and only used because this delay will
11201da177e4SLinus Torvalds 	 * be the same on other platforms and since it works here, it should
11211da177e4SLinus Torvalds 	 * work there.
11221da177e4SLinus Torvalds 	 *
11231da177e4SLinus Torvalds 	 * wingel suggests that this could be due to failing to wait
11241da177e4SLinus Torvalds 	 * one deskew delay.
11251da177e4SLinus Torvalds 	 */
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 	udelay(1);
11281da177e4SLinus Torvalds 
1129b746545fSFinn Thain 	dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	/*
11321da177e4SLinus Torvalds 	 * The SCSI specification calls for a 250 ms timeout for the actual
11331da177e4SLinus Torvalds 	 * selection.
11341da177e4SLinus Torvalds 	 */
11351da177e4SLinus Torvalds 
1136d5d37a0aSFinn Thain 	err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_BSY, SR_BSY,
1137ae753a33SFinn Thain 	                            msecs_to_jiffies(250));
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
114011d2f63bSFinn Thain 		spin_lock_irq(&hostdata->lock);
11411da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
11421da177e4SLinus Torvalds 		NCR5380_reselect(instance);
11436a6ff4acSFinn Thain 		shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
1144707d62b3SFinn Thain 		goto out;
11451da177e4SLinus Torvalds 	}
1146ae753a33SFinn Thain 
1147ae753a33SFinn Thain 	if (err < 0) {
114811d2f63bSFinn Thain 		spin_lock_irq(&hostdata->lock);
1149ae753a33SFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
11506a162836SFinn Thain 
1151707d62b3SFinn Thain 		/* Can't touch cmd if it has been reclaimed by the scsi ML */
11526a162836SFinn Thain 		if (!hostdata->selecting)
1153dad8261eSFinn Thain 			return false;
11546a162836SFinn Thain 
1155ae753a33SFinn Thain 		cmd->result = DID_BAD_TARGET << 16;
1156677e0194SFinn Thain 		complete_cmd(instance, cmd);
11576a162836SFinn Thain 		dsprintk(NDEBUG_SELECTION, instance,
11586a162836SFinn Thain 			"target did not respond within 250ms\n");
1159dad8261eSFinn Thain 		ret = false;
1160707d62b3SFinn Thain 		goto out;
1161ae753a33SFinn Thain 	}
1162ae753a33SFinn Thain 
11631da177e4SLinus Torvalds 	/*
11641da177e4SLinus Torvalds 	 * No less than two deskew delays after the initiator detects the
11651da177e4SLinus Torvalds 	 * BSY signal is true, it shall release the SEL signal and may
11661da177e4SLinus Torvalds 	 * change the DATA BUS.                                     -wingel
11671da177e4SLinus Torvalds 	 */
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	udelay(1);
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	/*
11741da177e4SLinus Torvalds 	 * Since we followed the SCSI spec, and raised ATN while SEL
11751da177e4SLinus Torvalds 	 * was true but before BSY was false during selection, the information
11761da177e4SLinus Torvalds 	 * transfer phase should be a MESSAGE OUT phase so that we can send the
11771da177e4SLinus Torvalds 	 * IDENTIFY message.
11781da177e4SLinus Torvalds 	 */
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	/* Wait for start of REQ/ACK handshake */
11811da177e4SLinus Torvalds 
1182d5d37a0aSFinn Thain 	err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
118311d2f63bSFinn Thain 	spin_lock_irq(&hostdata->lock);
11841cc160e1SFinn Thain 	if (err < 0) {
118555500d9bSFinn Thain 		shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
118655500d9bSFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1187707d62b3SFinn Thain 		goto out;
1188707d62b3SFinn Thain 	}
1189707d62b3SFinn Thain 	if (!hostdata->selecting) {
1190e7734ef1SAhmed S. Darwish 		do_abort(instance, 0);
1191dad8261eSFinn Thain 		return false;
11921da177e4SLinus Torvalds 	}
11931da177e4SLinus Torvalds 
1194b746545fSFinn Thain 	dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1195b746545fSFinn Thain 	         scmd_id(cmd));
11967c8ed783SFinn Thain 	tmp[0] = IDENTIFY(can_disconnect, cmd->device->lun);
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	len = 1;
11991da177e4SLinus Torvalds 	data = tmp;
12001da177e4SLinus Torvalds 	phase = PHASE_MSGOUT;
1201e7734ef1SAhmed S. Darwish 	NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
1202b15e791dSFinn Thain 	if (len) {
1203b15e791dSFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1204b15e791dSFinn Thain 		cmd->result = DID_ERROR << 16;
1205b15e791dSFinn Thain 		complete_cmd(instance, cmd);
1206b15e791dSFinn Thain 		dsprintk(NDEBUG_SELECTION, instance, "IDENTIFY message transfer failed\n");
1207dad8261eSFinn Thain 		ret = false;
1208b15e791dSFinn Thain 		goto out;
1209b15e791dSFinn Thain 	}
1210b15e791dSFinn Thain 
1211b746545fSFinn Thain 	dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
121211d2f63bSFinn Thain 
12131da177e4SLinus Torvalds 	hostdata->connected = cmd;
12143d07d22bSFinn Thain 	hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
12151da177e4SLinus Torvalds 
1216e9db3198SFinn Thain #ifdef SUN3_SCSI_VME
1217e9db3198SFinn Thain 	dregs->csr |= CSR_INTR;
1218e9db3198SFinn Thain #endif
1219e9db3198SFinn Thain 
12201da177e4SLinus Torvalds 	initialize_SCp(cmd);
12211da177e4SLinus Torvalds 
1222dad8261eSFinn Thain 	ret = false;
1223707d62b3SFinn Thain 
1224707d62b3SFinn Thain out:
1225707d62b3SFinn Thain 	if (!hostdata->selecting)
122696edebd6SFinn Thain 		return false;
1227707d62b3SFinn Thain 	hostdata->selecting = NULL;
1228dad8261eSFinn Thain 	return ret;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds /*
12321da177e4SLinus Torvalds  * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
12331da177e4SLinus Torvalds  * unsigned char *phase, int *count, unsigned char **data)
12341da177e4SLinus Torvalds  *
12351da177e4SLinus Torvalds  * Purpose : transfers data in given phase using polled I/O
12361da177e4SLinus Torvalds  *
12371da177e4SLinus Torvalds  * Inputs : instance - instance of driver, *phase - pointer to
12381da177e4SLinus Torvalds  * what phase is expected, *count - pointer to number of
1239e7734ef1SAhmed S. Darwish  * bytes to transfer, **data - pointer to data pointer,
1240e7734ef1SAhmed S. Darwish  * can_sleep - 1 or 0 when sleeping is permitted or not, respectively.
12411da177e4SLinus Torvalds  *
12421da177e4SLinus Torvalds  * Returns : -1 when different phase is entered without transferring
12430d2cf867SFinn Thain  * maximum number of bytes, 0 if all bytes are transferred or exit
12441da177e4SLinus Torvalds  * is in same phase.
12451da177e4SLinus Torvalds  *
12461da177e4SLinus Torvalds  * Also, *phase, *count, *data are modified in place.
12471da177e4SLinus Torvalds  *
12481da177e4SLinus Torvalds  * XXX Note : handling for bus free may be useful.
12491da177e4SLinus Torvalds  */
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds /*
12521da177e4SLinus Torvalds  * Note : this code is not as quick as it could be, however it
12531da177e4SLinus Torvalds  * IS 100% reliable, and for the actual data transfer where speed
12541da177e4SLinus Torvalds  * counts, we will always do a pseudo DMA or DMA transfer.
12551da177e4SLinus Torvalds  */
12561da177e4SLinus Torvalds 
12570d2cf867SFinn Thain static int NCR5380_transfer_pio(struct Scsi_Host *instance,
12580d2cf867SFinn Thain 				unsigned char *phase, int *count,
1259e7734ef1SAhmed S. Darwish 				unsigned char **data, unsigned int can_sleep)
12600d2cf867SFinn Thain {
126161e1ce58SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
12621da177e4SLinus Torvalds 	unsigned char p = *phase, tmp;
12631da177e4SLinus Torvalds 	int c = *count;
12641da177e4SLinus Torvalds 	unsigned char *d = *data;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	/*
12671da177e4SLinus Torvalds 	 * The NCR5380 chip will only drive the SCSI bus when the
12681da177e4SLinus Torvalds 	 * phase specified in the appropriate bits of the TARGET COMMAND
12691da177e4SLinus Torvalds 	 * REGISTER match the STATUS REGISTER
12701da177e4SLinus Torvalds 	 */
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	do {
12751da177e4SLinus Torvalds 		/*
12761da177e4SLinus Torvalds 		 * Wait for assertion of REQ, after which the phase bits will be
12771da177e4SLinus Torvalds 		 * valid
12781da177e4SLinus Torvalds 		 */
12791da177e4SLinus Torvalds 
1280e7734ef1SAhmed S. Darwish 		if (NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ,
1281e7734ef1SAhmed S. Darwish 					  HZ * can_sleep) < 0)
12821da177e4SLinus Torvalds 			break;
12831da177e4SLinus Torvalds 
1284b746545fSFinn Thain 		dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 		/* Check for phase mismatch */
1287686f3990SFinn Thain 		if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
1288b746545fSFinn Thain 			dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1289b746545fSFinn Thain 			NCR5380_dprint_phase(NDEBUG_PIO, instance);
12901da177e4SLinus Torvalds 			break;
12911da177e4SLinus Torvalds 		}
12920d2cf867SFinn Thain 
12931da177e4SLinus Torvalds 		/* Do actual transfer from SCSI bus to / from memory */
12941da177e4SLinus Torvalds 		if (!(p & SR_IO))
12951da177e4SLinus Torvalds 			NCR5380_write(OUTPUT_DATA_REG, *d);
12961da177e4SLinus Torvalds 		else
12971da177e4SLinus Torvalds 			*d = NCR5380_read(CURRENT_SCSI_DATA_REG);
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 		++d;
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 		/*
13021da177e4SLinus Torvalds 		 * The SCSI standard suggests that in MSGOUT phase, the initiator
13031da177e4SLinus Torvalds 		 * should drop ATN on the last byte of the message phase
13041da177e4SLinus Torvalds 		 * after REQ has been asserted for the handshake but before
13051da177e4SLinus Torvalds 		 * the initiator raises ACK.
13061da177e4SLinus Torvalds 		 */
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 		if (!(p & SR_IO)) {
13091da177e4SLinus Torvalds 			if (!((p & SR_MSG) && c > 1)) {
13101da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
13111da177e4SLinus Torvalds 				NCR5380_dprint(NDEBUG_PIO, instance);
13123d07d22bSFinn Thain 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
13133d07d22bSFinn Thain 				              ICR_ASSERT_DATA | ICR_ASSERT_ACK);
13141da177e4SLinus Torvalds 			} else {
13153d07d22bSFinn Thain 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
13163d07d22bSFinn Thain 				              ICR_ASSERT_DATA | ICR_ASSERT_ATN);
13171da177e4SLinus Torvalds 				NCR5380_dprint(NDEBUG_PIO, instance);
13183d07d22bSFinn Thain 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
13193d07d22bSFinn Thain 				              ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
13201da177e4SLinus Torvalds 			}
13211da177e4SLinus Torvalds 		} else {
13221da177e4SLinus Torvalds 			NCR5380_dprint(NDEBUG_PIO, instance);
13231da177e4SLinus Torvalds 			NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
13241da177e4SLinus Torvalds 		}
13251da177e4SLinus Torvalds 
1326d5d37a0aSFinn Thain 		if (NCR5380_poll_politely(hostdata,
1327e7734ef1SAhmed S. Darwish 		                          STATUS_REG, SR_REQ, 0, 5 * HZ * can_sleep) < 0)
1328a2edc4a6SFinn Thain 			break;
1329a2edc4a6SFinn Thain 
1330b746545fSFinn Thain 		dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds /*
13331da177e4SLinus Torvalds  * We have several special cases to consider during REQ/ACK handshaking :
13341da177e4SLinus Torvalds  * 1.  We were in MSGOUT phase, and we are on the last byte of the
13351da177e4SLinus Torvalds  * message.  ATN must be dropped as ACK is dropped.
13361da177e4SLinus Torvalds  *
13371da177e4SLinus Torvalds  * 2.  We are in a MSGIN phase, and we are on the last byte of the
13381da177e4SLinus Torvalds  * message.  We must exit with ACK asserted, so that the calling
13391da177e4SLinus Torvalds  * code may raise ATN before dropping ACK to reject the message.
13401da177e4SLinus Torvalds  *
13411da177e4SLinus Torvalds  * 3.  ACK and ATN are clear and the target may proceed as normal.
13421da177e4SLinus Torvalds  */
13431da177e4SLinus Torvalds 		if (!(p == PHASE_MSGIN && c == 1)) {
13441da177e4SLinus Torvalds 			if (p == PHASE_MSGOUT && c > 1)
13451da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
13461da177e4SLinus Torvalds 			else
13471da177e4SLinus Torvalds 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
13481da177e4SLinus Torvalds 		}
13491da177e4SLinus Torvalds 	} while (--c);
13501da177e4SLinus Torvalds 
1351b746545fSFinn Thain 	dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	*count = c;
13541da177e4SLinus Torvalds 	*data = d;
13551da177e4SLinus Torvalds 	tmp = NCR5380_read(STATUS_REG);
1356a2edc4a6SFinn Thain 	/* The phase read from the bus is valid if either REQ is (already)
1357a2edc4a6SFinn Thain 	 * asserted or if ACK hasn't been released yet. The latter applies if
1358a2edc4a6SFinn Thain 	 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1359a2edc4a6SFinn Thain 	 */
1360a2edc4a6SFinn Thain 	if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
13611da177e4SLinus Torvalds 		*phase = tmp & PHASE_MASK;
13621da177e4SLinus Torvalds 	else
13631da177e4SLinus Torvalds 		*phase = PHASE_UNKNOWN;
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	if (!c || (*phase == p))
13661da177e4SLinus Torvalds 		return 0;
13671da177e4SLinus Torvalds 	else
13681da177e4SLinus Torvalds 		return -1;
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds /**
13721da177e4SLinus Torvalds  * do_reset - issue a reset command
1373636b1ec8SFinn Thain  * @instance: adapter to reset
13741da177e4SLinus Torvalds  *
13751da177e4SLinus Torvalds  * Issue a reset sequence to the NCR5380 and try and get the bus
13761da177e4SLinus Torvalds  * back into sane shape.
13771da177e4SLinus Torvalds  *
1378636b1ec8SFinn Thain  * This clears the reset interrupt flag because there may be no handler for
1379636b1ec8SFinn Thain  * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1380636b1ec8SFinn Thain  * been installed. And when in EH we may have released the ST DMA interrupt.
13811da177e4SLinus Torvalds  */
13821da177e4SLinus Torvalds 
138354d8fe44SFinn Thain static void do_reset(struct Scsi_Host *instance)
138454d8fe44SFinn Thain {
138561e1ce58SFinn Thain 	struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance);
1386636b1ec8SFinn Thain 	unsigned long flags;
1387636b1ec8SFinn Thain 
1388636b1ec8SFinn Thain 	local_irq_save(flags);
1389636b1ec8SFinn Thain 	NCR5380_write(TARGET_COMMAND_REG,
1390636b1ec8SFinn Thain 	              PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
13911da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1392636b1ec8SFinn Thain 	udelay(50);
13931da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1394636b1ec8SFinn Thain 	(void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1395636b1ec8SFinn Thain 	local_irq_restore(flags);
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds 
139880d3eb6dSFinn Thain /**
139980d3eb6dSFinn Thain  * do_abort - abort the currently established nexus by going to
140080d3eb6dSFinn Thain  * MESSAGE OUT phase and sending an ABORT message.
140180d3eb6dSFinn Thain  * @instance: relevant scsi host instance
1402e7734ef1SAhmed S. Darwish  * @can_sleep: 1 or 0 when sleeping is permitted or not, respectively
14031da177e4SLinus Torvalds  *
1404d04fc41aSFinn Thain  * Returns 0 on success, negative error code on failure.
14051da177e4SLinus Torvalds  */
14061da177e4SLinus Torvalds 
1407e7734ef1SAhmed S. Darwish static int do_abort(struct Scsi_Host *instance, unsigned int can_sleep)
140854d8fe44SFinn Thain {
140961e1ce58SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
14101da177e4SLinus Torvalds 	unsigned char *msgptr, phase, tmp;
14111da177e4SLinus Torvalds 	int len;
14121da177e4SLinus Torvalds 	int rc;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	/* Request message out phase */
14151da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 	/*
14181da177e4SLinus Torvalds 	 * Wait for the target to indicate a valid phase by asserting
14191da177e4SLinus Torvalds 	 * REQ.  Once this happens, we'll have either a MSGOUT phase
14201da177e4SLinus Torvalds 	 * and can immediately send the ABORT message, or we'll have some
14211da177e4SLinus Torvalds 	 * other phase and will have to source/sink data.
14221da177e4SLinus Torvalds 	 *
14231da177e4SLinus Torvalds 	 * We really don't care what value was on the bus or what value
14241da177e4SLinus Torvalds 	 * the target sees, so we just handshake.
14251da177e4SLinus Torvalds 	 */
14261da177e4SLinus Torvalds 
1427e7734ef1SAhmed S. Darwish 	rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ,
1428e7734ef1SAhmed S. Darwish 				   10 * HZ * can_sleep);
14291da177e4SLinus Torvalds 	if (rc < 0)
1430d04fc41aSFinn Thain 		goto out;
14311da177e4SLinus Torvalds 
1432f35d3474SFinn Thain 	tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
14351da177e4SLinus Torvalds 
1436f35d3474SFinn Thain 	if (tmp != PHASE_MSGOUT) {
14370d2cf867SFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG,
14380d2cf867SFinn Thain 		              ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1439e7734ef1SAhmed S. Darwish 		rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, 0,
1440e7734ef1SAhmed S. Darwish 					   3 * HZ * can_sleep);
14411cc160e1SFinn Thain 		if (rc < 0)
1442d04fc41aSFinn Thain 			goto out;
144380d3eb6dSFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
14441da177e4SLinus Torvalds 	}
14450d2cf867SFinn Thain 
14461da177e4SLinus Torvalds 	tmp = ABORT;
14471da177e4SLinus Torvalds 	msgptr = &tmp;
14481da177e4SLinus Torvalds 	len = 1;
14491da177e4SLinus Torvalds 	phase = PHASE_MSGOUT;
1450e7734ef1SAhmed S. Darwish 	NCR5380_transfer_pio(instance, &phase, &len, &msgptr, can_sleep);
1451d04fc41aSFinn Thain 	if (len)
1452d04fc41aSFinn Thain 		rc = -ENXIO;
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	/*
14551da177e4SLinus Torvalds 	 * If we got here, and the command completed successfully,
14561da177e4SLinus Torvalds 	 * we're about to go into bus free state.
14571da177e4SLinus Torvalds 	 */
14581da177e4SLinus Torvalds 
1459d04fc41aSFinn Thain out:
146080d3eb6dSFinn Thain 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1461d04fc41aSFinn Thain 	return rc;
14621da177e4SLinus Torvalds }
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds /*
14651da177e4SLinus Torvalds  * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
14661da177e4SLinus Torvalds  * unsigned char *phase, int *count, unsigned char **data)
14671da177e4SLinus Torvalds  *
14681da177e4SLinus Torvalds  * Purpose : transfers data in given phase using either real
14691da177e4SLinus Torvalds  * or pseudo DMA.
14701da177e4SLinus Torvalds  *
14711da177e4SLinus Torvalds  * Inputs : instance - instance of driver, *phase - pointer to
14721da177e4SLinus Torvalds  * what phase is expected, *count - pointer to number of
14731da177e4SLinus Torvalds  * bytes to transfer, **data - pointer to data pointer.
14741da177e4SLinus Torvalds  *
14751da177e4SLinus Torvalds  * Returns : -1 when different phase is entered without transferring
147625985edcSLucas De Marchi  * maximum number of bytes, 0 if all bytes or transferred or exit
14771da177e4SLinus Torvalds  * is in same phase.
14781da177e4SLinus Torvalds  *
14791da177e4SLinus Torvalds  * Also, *phase, *count, *data are modified in place.
14801da177e4SLinus Torvalds  */
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 
14830d2cf867SFinn Thain static int NCR5380_transfer_dma(struct Scsi_Host *instance,
14840d2cf867SFinn Thain 				unsigned char *phase, int *count,
14850d2cf867SFinn Thain 				unsigned char **data)
14860d2cf867SFinn Thain {
14870d2cf867SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
1488f0ea73a4SFinn Thain 	int c = *count;
1489f0ea73a4SFinn Thain 	unsigned char p = *phase;
1490f0ea73a4SFinn Thain 	unsigned char *d = *data;
14911da177e4SLinus Torvalds 	unsigned char tmp;
14928053b0eeSFinn Thain 	int result = 0;
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 	if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
14951da177e4SLinus Torvalds 		*phase = tmp;
14961da177e4SLinus Torvalds 		return -1;
14971da177e4SLinus Torvalds 	}
14981da177e4SLinus Torvalds 
1499*ff1269cbSFinn Thain 	NCR5380_to_ncmd(hostdata->connected)->phase = p;
15008053b0eeSFinn Thain 
15018053b0eeSFinn Thain 	if (p & SR_IO) {
15028053b0eeSFinn Thain 		if (hostdata->read_overruns)
15038053b0eeSFinn Thain 			c -= hostdata->read_overruns;
15048053b0eeSFinn Thain 		else if (hostdata->flags & FLAG_DMA_FIXUP)
15058053b0eeSFinn Thain 			--c;
15068053b0eeSFinn Thain 	}
15078053b0eeSFinn Thain 
15088053b0eeSFinn Thain 	dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
15098053b0eeSFinn Thain 	         (p & SR_IO) ? "receive" : "send", c, d);
15108053b0eeSFinn Thain 
1511e9db3198SFinn Thain #ifdef CONFIG_SUN3
1512e9db3198SFinn Thain 	/* send start chain */
1513e9db3198SFinn Thain 	sun3scsi_dma_start(c, *data);
1514e9db3198SFinn Thain #endif
1515e9db3198SFinn Thain 
15161da177e4SLinus Torvalds 	NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1517cd400825SFinn Thain 	NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1518cd400825SFinn Thain 	                        MR_ENABLE_EOP_INTR);
15191da177e4SLinus Torvalds 
15208053b0eeSFinn Thain 	if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
15218053b0eeSFinn Thain 		/* On the Medusa, it is a must to initialize the DMA before
15228053b0eeSFinn Thain 		 * starting the NCR. This is also the cleaner way for the TT.
15238053b0eeSFinn Thain 		 */
15248053b0eeSFinn Thain 		if (p & SR_IO)
15254a98f896SFinn Thain 			result = NCR5380_dma_recv_setup(hostdata, d, c);
15268053b0eeSFinn Thain 		else
15274a98f896SFinn Thain 			result = NCR5380_dma_send_setup(hostdata, d, c);
15288053b0eeSFinn Thain 	}
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds 	/*
15311da177e4SLinus Torvalds 	 * On the PAS16 at least I/O recovery delays are not needed here.
15321da177e4SLinus Torvalds 	 * Everyone else seems to want them.
15331da177e4SLinus Torvalds 	 */
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	if (p & SR_IO) {
1536e9db3198SFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1537e5d55d1aSFinn Thain 		NCR5380_io_delay(1);
15381da177e4SLinus Torvalds 		NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
15391da177e4SLinus Torvalds 	} else {
1540e5d55d1aSFinn Thain 		NCR5380_io_delay(1);
15411da177e4SLinus Torvalds 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1542e5d55d1aSFinn Thain 		NCR5380_io_delay(1);
15431da177e4SLinus Torvalds 		NCR5380_write(START_DMA_SEND_REG, 0);
1544e5d55d1aSFinn Thain 		NCR5380_io_delay(1);
15451da177e4SLinus Torvalds 	}
15461da177e4SLinus Torvalds 
1547e9db3198SFinn Thain #ifdef CONFIG_SUN3
1548e9db3198SFinn Thain #ifdef SUN3_SCSI_VME
1549e9db3198SFinn Thain 	dregs->csr |= CSR_DMA_ENABLE;
1550e9db3198SFinn Thain #endif
1551e9db3198SFinn Thain 	sun3_dma_active = 1;
1552e9db3198SFinn Thain #endif
1553e9db3198SFinn Thain 
15548053b0eeSFinn Thain 	if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
15558053b0eeSFinn Thain 		/* On the Falcon, the DMA setup must be done after the last
15568053b0eeSFinn Thain 		 * NCR access, else the DMA setup gets trashed!
15578053b0eeSFinn Thain 		 */
15588053b0eeSFinn Thain 		if (p & SR_IO)
15594a98f896SFinn Thain 			result = NCR5380_dma_recv_setup(hostdata, d, c);
15608053b0eeSFinn Thain 		else
15614a98f896SFinn Thain 			result = NCR5380_dma_send_setup(hostdata, d, c);
15628053b0eeSFinn Thain 	}
15638053b0eeSFinn Thain 
15648053b0eeSFinn Thain 	/* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
15658053b0eeSFinn Thain 	if (result < 0)
15668053b0eeSFinn Thain 		return result;
15678053b0eeSFinn Thain 
15688053b0eeSFinn Thain 	/* For real DMA, result is the byte count. DMA interrupt is expected. */
15698053b0eeSFinn Thain 	if (result > 0) {
15708053b0eeSFinn Thain 		hostdata->dma_len = result;
15718053b0eeSFinn Thain 		return 0;
15728053b0eeSFinn Thain 	}
15738053b0eeSFinn Thain 
15748053b0eeSFinn Thain 	/* The result is zero iff pseudo DMA send/receive was completed. */
15758053b0eeSFinn Thain 	hostdata->dma_len = c;
15768053b0eeSFinn Thain 
15771da177e4SLinus Torvalds /*
1578e4dec680SFinn Thain  * A note regarding the DMA errata workarounds for early NMOS silicon.
1579c16df32eSFinn Thain  *
1580c16df32eSFinn Thain  * For DMA sends, we want to wait until the last byte has been
1581c16df32eSFinn Thain  * transferred out over the bus before we turn off DMA mode.  Alas, there
1582c16df32eSFinn Thain  * seems to be no terribly good way of doing this on a 5380 under all
1583c16df32eSFinn Thain  * conditions.  For non-scatter-gather operations, we can wait until REQ
1584c16df32eSFinn Thain  * and ACK both go false, or until a phase mismatch occurs.  Gather-sends
1585c16df32eSFinn Thain  * are nastier, since the device will be expecting more data than we
1586c16df32eSFinn Thain  * are prepared to send it, and REQ will remain asserted.  On a 53C8[01] we
1587c16df32eSFinn Thain  * could test Last Byte Sent to assure transfer (I imagine this is precisely
1588c16df32eSFinn Thain  * why this signal was added to the newer chips) but on the older 538[01]
1589c16df32eSFinn Thain  * this signal does not exist.  The workaround for this lack is a watchdog;
1590c16df32eSFinn Thain  * we bail out of the wait-loop after a modest amount of wait-time if
1591c16df32eSFinn Thain  * the usual exit conditions are not met.  Not a terribly clean or
1592c16df32eSFinn Thain  * correct solution :-%
1593c16df32eSFinn Thain  *
1594c16df32eSFinn Thain  * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1595c16df32eSFinn Thain  * If the chip is in DMA receive mode, it will respond to a target's
1596c16df32eSFinn Thain  * REQ by latching the SCSI data into the INPUT DATA register and asserting
1597c16df32eSFinn Thain  * ACK, even if it has _already_ been notified by the DMA controller that
1598c16df32eSFinn Thain  * the current DMA transfer has completed!  If the NCR5380 is then taken
1599c16df32eSFinn Thain  * out of DMA mode, this already-acknowledged byte is lost. This is
1600c16df32eSFinn Thain  * not a problem for "one DMA transfer per READ command", because
1601c16df32eSFinn Thain  * the situation will never arise... either all of the data is DMA'ed
1602c16df32eSFinn Thain  * properly, or the target switches to MESSAGE IN phase to signal a
1603c16df32eSFinn Thain  * disconnection (either operation bringing the DMA to a clean halt).
1604c16df32eSFinn Thain  * However, in order to handle scatter-receive, we must work around the
1605e4dec680SFinn Thain  * problem.  The chosen fix is to DMA fewer bytes, then check for the
1606c16df32eSFinn Thain  * condition before taking the NCR5380 out of DMA mode.  One or two extra
1607c16df32eSFinn Thain  * bytes are transferred via PIO as necessary to fill out the original
1608c16df32eSFinn Thain  * request.
16091da177e4SLinus Torvalds  */
16101da177e4SLinus Torvalds 
16118053b0eeSFinn Thain 	if (hostdata->flags & FLAG_DMA_FIXUP) {
16121da177e4SLinus Torvalds 		if (p & SR_IO) {
16131da177e4SLinus Torvalds 			/*
1614e4dec680SFinn Thain 			 * The workaround was to transfer fewer bytes than we
16151da177e4SLinus Torvalds 			 * intended to with the pseudo-DMA read function, wait for
16161da177e4SLinus Torvalds 			 * the chip to latch the last byte, read it, and then disable
16171da177e4SLinus Torvalds 			 * pseudo-DMA mode.
16181da177e4SLinus Torvalds 			 *
16191da177e4SLinus Torvalds 			 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
16201da177e4SLinus Torvalds 			 * REQ is deasserted when ACK is asserted, and not reasserted
16211da177e4SLinus Torvalds 			 * until ACK goes false.  Since the NCR5380 won't lower ACK
16221da177e4SLinus Torvalds 			 * until DACK is asserted, which won't happen unless we twiddle
16231da177e4SLinus Torvalds 			 * the DMA port or we take the NCR5380 out of DMA mode, we
16241da177e4SLinus Torvalds 			 * can guarantee that we won't handshake another extra
16251da177e4SLinus Torvalds 			 * byte.
16261da177e4SLinus Torvalds 			 */
16271da177e4SLinus Torvalds 
1628d5d37a0aSFinn Thain 			if (NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
1629e7734ef1SAhmed S. Darwish 			                          BASR_DRQ, BASR_DRQ, 0) < 0) {
1630438af51cSFinn Thain 				result = -1;
163155181be8SFinn Thain 				shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
163255181be8SFinn Thain 			}
1633d5d37a0aSFinn Thain 			if (NCR5380_poll_politely(hostdata, STATUS_REG,
1634e7734ef1SAhmed S. Darwish 			                          SR_REQ, 0, 0) < 0) {
1635438af51cSFinn Thain 				result = -1;
163655181be8SFinn Thain 				shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
163755181be8SFinn Thain 			}
16388053b0eeSFinn Thain 			d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
16391da177e4SLinus Torvalds 		} else {
16401da177e4SLinus Torvalds 			/*
16411da177e4SLinus Torvalds 			 * Wait for the last byte to be sent.  If REQ is being asserted for
16421da177e4SLinus Torvalds 			 * the byte we're interested, we'll ACK it and it will go false.
16431da177e4SLinus Torvalds 			 */
1644d5d37a0aSFinn Thain 			if (NCR5380_poll_politely2(hostdata,
164555181be8SFinn Thain 			     BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1646e7734ef1SAhmed S. Darwish 			     BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, 0) < 0) {
1647438af51cSFinn Thain 				result = -1;
164855181be8SFinn Thain 				shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
16491da177e4SLinus Torvalds 			}
16501da177e4SLinus Torvalds 		}
16511da177e4SLinus Torvalds 	}
16528053b0eeSFinn Thain 
16538053b0eeSFinn Thain 	NCR5380_dma_complete(instance);
1654438af51cSFinn Thain 	return result;
16551da177e4SLinus Torvalds }
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds /*
16581da177e4SLinus Torvalds  * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
16591da177e4SLinus Torvalds  *
16601da177e4SLinus Torvalds  * Purpose : run through the various SCSI phases and do as the target
16611da177e4SLinus Torvalds  * directs us to.  Operates on the currently connected command,
16621da177e4SLinus Torvalds  * instance->connected.
16631da177e4SLinus Torvalds  *
16641da177e4SLinus Torvalds  * Inputs : instance, instance for which we are doing commands
16651da177e4SLinus Torvalds  *
16661da177e4SLinus Torvalds  * Side effects : SCSI things happen, the disconnected queue will be
16671da177e4SLinus Torvalds  * modified if a command disconnects, *instance->connected will
16681da177e4SLinus Torvalds  * change.
16691da177e4SLinus Torvalds  *
16701da177e4SLinus Torvalds  * XXX Note : we need to watch for bus free or a reset condition here
16711da177e4SLinus Torvalds  * to recover from an unexpected bus free condition.
16721da177e4SLinus Torvalds  */
16731da177e4SLinus Torvalds 
16740d2cf867SFinn Thain static void NCR5380_information_transfer(struct Scsi_Host *instance)
16754ab2a787SFinn Thain 	__releases(&hostdata->lock) __acquires(&hostdata->lock)
16760d2cf867SFinn Thain {
1677e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
16781da177e4SLinus Torvalds 	unsigned char msgout = NOP;
16791da177e4SLinus Torvalds 	int sink = 0;
16801da177e4SLinus Torvalds 	int len;
16811da177e4SLinus Torvalds 	int transfersize;
16821da177e4SLinus Torvalds 	unsigned char *data;
16831da177e4SLinus Torvalds 	unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
168411d2f63bSFinn Thain 	struct scsi_cmnd *cmd;
16851da177e4SLinus Torvalds 
1686e9db3198SFinn Thain #ifdef SUN3_SCSI_VME
1687e9db3198SFinn Thain 	dregs->csr |= CSR_INTR;
1688e9db3198SFinn Thain #endif
1689e9db3198SFinn Thain 
169011d2f63bSFinn Thain 	while ((cmd = hostdata->connected)) {
1691*ff1269cbSFinn Thain 		struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(cmd);
169232b26a10SFinn Thain 
16931da177e4SLinus Torvalds 		tmp = NCR5380_read(STATUS_REG);
16941da177e4SLinus Torvalds 		/* We only have a valid SCSI phase when REQ is asserted */
16951da177e4SLinus Torvalds 		if (tmp & SR_REQ) {
16961da177e4SLinus Torvalds 			phase = (tmp & PHASE_MASK);
16971da177e4SLinus Torvalds 			if (phase != old_phase) {
16981da177e4SLinus Torvalds 				old_phase = phase;
16991da177e4SLinus Torvalds 				NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
17001da177e4SLinus Torvalds 			}
1701e9db3198SFinn Thain #ifdef CONFIG_SUN3
17024a98f896SFinn Thain 			if (phase == PHASE_CMDOUT &&
17034a98f896SFinn Thain 			    sun3_dma_setup_done != cmd) {
17044a98f896SFinn Thain 				int count;
1705e9db3198SFinn Thain 
1706*ff1269cbSFinn Thain 				advance_sg_buffer(ncmd);
1707e9db3198SFinn Thain 
17084a98f896SFinn Thain 				count = sun3scsi_dma_xfer_len(hostdata, cmd);
17094a98f896SFinn Thain 
17104a98f896SFinn Thain 				if (count > 0) {
17112e4b231aSBart Van Assche 					if (cmd->sc_data_direction == DMA_TO_DEVICE)
17124a98f896SFinn Thain 						sun3scsi_dma_send_setup(hostdata,
1713*ff1269cbSFinn Thain 									ncmd->ptr, count);
17144a98f896SFinn Thain 					else
17154a98f896SFinn Thain 						sun3scsi_dma_recv_setup(hostdata,
1716*ff1269cbSFinn Thain 									ncmd->ptr, count);
1717e9db3198SFinn Thain 					sun3_dma_setup_done = cmd;
1718e9db3198SFinn Thain 				}
1719e9db3198SFinn Thain #ifdef SUN3_SCSI_VME
1720e9db3198SFinn Thain 				dregs->csr |= CSR_INTR;
1721e9db3198SFinn Thain #endif
1722e9db3198SFinn Thain 			}
1723e9db3198SFinn Thain #endif /* CONFIG_SUN3 */
1724e9db3198SFinn Thain 
17251da177e4SLinus Torvalds 			if (sink && (phase != PHASE_MSGOUT)) {
17261da177e4SLinus Torvalds 				NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
17271da177e4SLinus Torvalds 
17283d07d22bSFinn Thain 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
17293d07d22bSFinn Thain 				              ICR_ASSERT_ACK);
17300d2cf867SFinn Thain 				while (NCR5380_read(STATUS_REG) & SR_REQ)
17310d2cf867SFinn Thain 					;
17323d07d22bSFinn Thain 				NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
17333d07d22bSFinn Thain 				              ICR_ASSERT_ATN);
17341da177e4SLinus Torvalds 				sink = 0;
17351da177e4SLinus Torvalds 				continue;
17361da177e4SLinus Torvalds 			}
17370d2cf867SFinn Thain 
17381da177e4SLinus Torvalds 			switch (phase) {
17391da177e4SLinus Torvalds 			case PHASE_DATAOUT:
17401da177e4SLinus Torvalds #if (NDEBUG & NDEBUG_NO_DATAOUT)
17416a6ff4acSFinn Thain 				shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
17421da177e4SLinus Torvalds 				sink = 1;
1743e7734ef1SAhmed S. Darwish 				do_abort(instance, 0);
17441da177e4SLinus Torvalds 				cmd->result = DID_ERROR << 16;
1745677e0194SFinn Thain 				complete_cmd(instance, cmd);
1746dc183965SFinn Thain 				hostdata->connected = NULL;
174745ddc1b2SFinn Thain 				hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
17481da177e4SLinus Torvalds 				return;
17491da177e4SLinus Torvalds #endif
1750bf1a0c6fSFinn Thain 			case PHASE_DATAIN:
17511da177e4SLinus Torvalds 				/*
17521da177e4SLinus Torvalds 				 * If there is no room left in the current buffer in the
17531da177e4SLinus Torvalds 				 * scatter-gather list, move onto the next one.
17541da177e4SLinus Torvalds 				 */
17551da177e4SLinus Torvalds 
1756*ff1269cbSFinn Thain 				advance_sg_buffer(ncmd);
17570e9fdd2bSFinn Thain 				dsprintk(NDEBUG_INFORMATION, instance,
17580e9fdd2bSFinn Thain 					"this residual %d, sg ents %d\n",
1759*ff1269cbSFinn Thain 					ncmd->this_residual,
1760*ff1269cbSFinn Thain 					sg_nents(ncmd->buffer));
17610d2cf867SFinn Thain 
17621da177e4SLinus Torvalds 				/*
17631da177e4SLinus Torvalds 				 * The preferred transfer method is going to be
17641da177e4SLinus Torvalds 				 * PSEUDO-DMA for systems that are strictly PIO,
17651da177e4SLinus Torvalds 				 * since we can let the hardware do the handshaking.
17661da177e4SLinus Torvalds 				 *
17671da177e4SLinus Torvalds 				 * For this to work, we need to know the transfersize
17681da177e4SLinus Torvalds 				 * ahead of time, since the pseudo-DMA code will sit
17691da177e4SLinus Torvalds 				 * in an unconditional loop.
17701da177e4SLinus Torvalds 				 */
17711da177e4SLinus Torvalds 
1772ff3d4578SFinn Thain 				transfersize = 0;
17737e9ec8d9SFinn Thain 				if (!cmd->device->borken)
17744a98f896SFinn Thain 					transfersize = NCR5380_dma_xfer_len(hostdata, cmd);
17751da177e4SLinus Torvalds 
1776438af51cSFinn Thain 				if (transfersize > 0) {
17771da177e4SLinus Torvalds 					len = transfersize;
17780d2cf867SFinn Thain 					if (NCR5380_transfer_dma(instance, &phase,
1779*ff1269cbSFinn Thain 					    &len, (unsigned char **)&ncmd->ptr)) {
17801da177e4SLinus Torvalds 						/*
17810d2cf867SFinn Thain 						 * If the watchdog timer fires, all future
17820d2cf867SFinn Thain 						 * accesses to this device will use the
17830d2cf867SFinn Thain 						 * polled-IO.
17841da177e4SLinus Torvalds 						 */
1785017560fcSJeff Garzik 						scmd_printk(KERN_INFO, cmd,
1786017560fcSJeff Garzik 							"switching to slow handshake\n");
17871da177e4SLinus Torvalds 						cmd->device->borken = 1;
1788f9dfed1cSFinn Thain 						do_reset(instance);
1789f9dfed1cSFinn Thain 						bus_reset_cleanup(instance);
17908053b0eeSFinn Thain 					}
1791f825e40bSFinn Thain 				} else {
179208348b1cSFinn Thain 					/* Transfer a small chunk so that the
179308348b1cSFinn Thain 					 * irq mode lock is not held too long.
17941678847eSFinn Thain 					 */
1795*ff1269cbSFinn Thain 					transfersize = min(ncmd->this_residual,
179608348b1cSFinn Thain 							   NCR5380_PIO_CHUNK_SIZE);
17971678847eSFinn Thain 					len = transfersize;
17981678847eSFinn Thain 					NCR5380_transfer_pio(instance, &phase, &len,
1799*ff1269cbSFinn Thain 							     (unsigned char **)&ncmd->ptr,
1800e7734ef1SAhmed S. Darwish 							     0);
1801*ff1269cbSFinn Thain 					ncmd->this_residual -= transfersize - len;
180211d2f63bSFinn Thain 				}
1803e9db3198SFinn Thain #ifdef CONFIG_SUN3
1804e9db3198SFinn Thain 				if (sun3_dma_setup_done == cmd)
1805e9db3198SFinn Thain 					sun3_dma_setup_done = NULL;
1806e9db3198SFinn Thain #endif
18071678847eSFinn Thain 				return;
18081da177e4SLinus Torvalds 			case PHASE_MSGIN:
18091da177e4SLinus Torvalds 				len = 1;
18101da177e4SLinus Torvalds 				data = &tmp;
1811e7734ef1SAhmed S. Darwish 				NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
1812*ff1269cbSFinn Thain 				ncmd->message = tmp;
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds 				switch (tmp) {
18151da177e4SLinus Torvalds 				case ABORT:
18167b25bdb1SHannes Reinecke 					set_host_byte(cmd, DID_ABORT);
181761f4f11bSGustavo A. R. Silva 					fallthrough;
18181da177e4SLinus Torvalds 				case COMMAND_COMPLETE:
18191da177e4SLinus Torvalds 					/* Accept message by clearing ACK */
18201da177e4SLinus Torvalds 					sink = 1;
18211da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
18220d3d9a42SFinn Thain 					dsprintk(NDEBUG_QUEUES, instance,
18230d3d9a42SFinn Thain 					         "COMMAND COMPLETE %p target %d lun %llu\n",
18240d3d9a42SFinn Thain 					         cmd, scmd_id(cmd), cmd->device->lun);
18250d3d9a42SFinn Thain 
18261da177e4SLinus Torvalds 					hostdata->connected = NULL;
182745ddc1b2SFinn Thain 					hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
18281da177e4SLinus Torvalds 
1829*ff1269cbSFinn Thain 					set_status_byte(cmd, ncmd->status);
18301da177e4SLinus Torvalds 
1831350767f2SFinn Thain 					set_resid_from_SCp(cmd);
1832350767f2SFinn Thain 
1833f27db8ebSFinn Thain 					if (cmd->cmnd[0] == REQUEST_SENSE)
1834f27db8ebSFinn Thain 						complete_cmd(instance, cmd);
1835f27db8ebSFinn Thain 					else {
1836*ff1269cbSFinn Thain 						if (ncmd->status == SAM_STAT_CHECK_CONDITION ||
1837*ff1269cbSFinn Thain 						    ncmd->status == SAM_STAT_COMMAND_TERMINATED) {
1838f27db8ebSFinn Thain 							dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1839dbb6b350SFinn Thain 							         cmd);
1840f27db8ebSFinn Thain 							list_add_tail(&ncmd->list,
1841f27db8ebSFinn Thain 							              &hostdata->autosense);
1842f27db8ebSFinn Thain 						} else
1843677e0194SFinn Thain 							complete_cmd(instance, cmd);
18441da177e4SLinus Torvalds 					}
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 					/*
18471da177e4SLinus Torvalds 					 * Restore phase bits to 0 so an interrupted selection,
18481da177e4SLinus Torvalds 					 * arbitration can resume.
18491da177e4SLinus Torvalds 					 */
18501da177e4SLinus Torvalds 					NCR5380_write(TARGET_COMMAND_REG, 0);
185172064a78SFinn Thain 
18521da177e4SLinus Torvalds 					return;
18531da177e4SLinus Torvalds 				case MESSAGE_REJECT:
18541da177e4SLinus Torvalds 					/* Accept message by clearing ACK */
18551da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
18561da177e4SLinus Torvalds 					switch (hostdata->last_message) {
18571da177e4SLinus Torvalds 					case HEAD_OF_QUEUE_TAG:
18581da177e4SLinus Torvalds 					case ORDERED_QUEUE_TAG:
18591da177e4SLinus Torvalds 					case SIMPLE_QUEUE_TAG:
18601da177e4SLinus Torvalds 						cmd->device->simple_tags = 0;
18619cb78c16SHannes Reinecke 						hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
18621da177e4SLinus Torvalds 						break;
18631da177e4SLinus Torvalds 					default:
18641da177e4SLinus Torvalds 						break;
18651da177e4SLinus Torvalds 					}
1866340b9612SFinn Thain 					break;
18670d2cf867SFinn Thain 				case DISCONNECT:
18681da177e4SLinus Torvalds 					/* Accept message by clearing ACK */
18691da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
18701da177e4SLinus Torvalds 					hostdata->connected = NULL;
187132b26a10SFinn Thain 					list_add(&ncmd->list, &hostdata->disconnected);
18720d3d9a42SFinn Thain 					dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
18730d3d9a42SFinn Thain 					         instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
18740d3d9a42SFinn Thain 					         cmd, scmd_id(cmd), cmd->device->lun);
18750d3d9a42SFinn Thain 
18761da177e4SLinus Torvalds 					/*
18771da177e4SLinus Torvalds 					 * Restore phase bits to 0 so an interrupted selection,
18781da177e4SLinus Torvalds 					 * arbitration can resume.
18791da177e4SLinus Torvalds 					 */
18801da177e4SLinus Torvalds 					NCR5380_write(TARGET_COMMAND_REG, 0);
18811da177e4SLinus Torvalds 
1882e9db3198SFinn Thain #ifdef SUN3_SCSI_VME
1883e9db3198SFinn Thain 					dregs->csr |= CSR_DMA_ENABLE;
1884e9db3198SFinn Thain #endif
18851da177e4SLinus Torvalds 					return;
18861da177e4SLinus Torvalds 					/*
18871da177e4SLinus Torvalds 					 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
18881da177e4SLinus Torvalds 					 * operation, in violation of the SCSI spec so we can safely
18891da177e4SLinus Torvalds 					 * ignore SAVE/RESTORE pointers calls.
18901da177e4SLinus Torvalds 					 *
18911da177e4SLinus Torvalds 					 * Unfortunately, some disks violate the SCSI spec and
18921da177e4SLinus Torvalds 					 * don't issue the required SAVE_POINTERS message before
18931da177e4SLinus Torvalds 					 * disconnecting, and we have to break spec to remain
18941da177e4SLinus Torvalds 					 * compatible.
18951da177e4SLinus Torvalds 					 */
18961da177e4SLinus Torvalds 				case SAVE_POINTERS:
18971da177e4SLinus Torvalds 				case RESTORE_POINTERS:
18981da177e4SLinus Torvalds 					/* Accept message by clearing ACK */
18991da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
19001da177e4SLinus Torvalds 					break;
19011da177e4SLinus Torvalds 				case EXTENDED_MESSAGE:
19021da177e4SLinus Torvalds 					/*
1903c16df32eSFinn Thain 					 * Start the message buffer with the EXTENDED_MESSAGE
19041abfd370SMatthew Wilcox 					 * byte, since spi_print_msg() wants the whole thing.
19051da177e4SLinus Torvalds 					 */
19061da177e4SLinus Torvalds 					extended_msg[0] = EXTENDED_MESSAGE;
19071da177e4SLinus Torvalds 					/* Accept first byte by clearing ACK */
19081da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
190911d2f63bSFinn Thain 
191011d2f63bSFinn Thain 					spin_unlock_irq(&hostdata->lock);
191111d2f63bSFinn Thain 
1912b746545fSFinn Thain 					dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 					len = 2;
19151da177e4SLinus Torvalds 					data = extended_msg + 1;
19161da177e4SLinus Torvalds 					phase = PHASE_MSGIN;
1917e7734ef1SAhmed S. Darwish 					NCR5380_transfer_pio(instance, &phase, &len, &data, 1);
1918b746545fSFinn Thain 					dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1919b746545fSFinn Thain 					         (int)extended_msg[1],
1920b746545fSFinn Thain 					         (int)extended_msg[2]);
19211da177e4SLinus Torvalds 
1922e0783ed3SFinn Thain 					if (!len && extended_msg[1] > 0 &&
1923e0783ed3SFinn Thain 					    extended_msg[1] <= sizeof(extended_msg) - 2) {
19241da177e4SLinus Torvalds 						/* Accept third byte by clearing ACK */
19251da177e4SLinus Torvalds 						NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
19261da177e4SLinus Torvalds 						len = extended_msg[1] - 1;
19271da177e4SLinus Torvalds 						data = extended_msg + 3;
19281da177e4SLinus Torvalds 						phase = PHASE_MSGIN;
19291da177e4SLinus Torvalds 
1930e7734ef1SAhmed S. Darwish 						NCR5380_transfer_pio(instance, &phase, &len, &data, 1);
1931b746545fSFinn Thain 						dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1932b746545fSFinn Thain 						         len);
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 						switch (extended_msg[2]) {
19351da177e4SLinus Torvalds 						case EXTENDED_SDTR:
19361da177e4SLinus Torvalds 						case EXTENDED_WDTR:
19371da177e4SLinus Torvalds 							tmp = 0;
19381da177e4SLinus Torvalds 						}
19391da177e4SLinus Torvalds 					} else if (len) {
19406a6ff4acSFinn Thain 						shost_printk(KERN_ERR, instance, "error receiving extended message\n");
19411da177e4SLinus Torvalds 						tmp = 0;
19421da177e4SLinus Torvalds 					} else {
19436a6ff4acSFinn Thain 						shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
19446a6ff4acSFinn Thain 						             extended_msg[2], extended_msg[1]);
19451da177e4SLinus Torvalds 						tmp = 0;
19461da177e4SLinus Torvalds 					}
194711d2f63bSFinn Thain 
194811d2f63bSFinn Thain 					spin_lock_irq(&hostdata->lock);
194911d2f63bSFinn Thain 					if (!hostdata->connected)
195011d2f63bSFinn Thain 						return;
195111d2f63bSFinn Thain 
1952df135e32SFinn Thain 					/* Reject message */
1953df561f66SGustavo A. R. Silva 					fallthrough;
1954df135e32SFinn Thain 				default:
19551da177e4SLinus Torvalds 					/*
19561da177e4SLinus Torvalds 					 * If we get something weird that we aren't expecting,
1957df135e32SFinn Thain 					 * log it.
19581da177e4SLinus Torvalds 					 */
195939bef87cSFinn Thain 					if (tmp == EXTENDED_MESSAGE)
1960017560fcSJeff Garzik 						scmd_printk(KERN_INFO, cmd,
19610d2cf867SFinn Thain 						            "rejecting unknown extended message code %02x, length %d\n",
196239bef87cSFinn Thain 						            extended_msg[2], extended_msg[1]);
196339bef87cSFinn Thain 					else if (tmp)
196439bef87cSFinn Thain 						scmd_printk(KERN_INFO, cmd,
196539bef87cSFinn Thain 						            "rejecting unknown message code %02x\n",
196639bef87cSFinn Thain 						            tmp);
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 					msgout = MESSAGE_REJECT;
19691da177e4SLinus Torvalds 					NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
19701da177e4SLinus Torvalds 					break;
19711da177e4SLinus Torvalds 				} /* switch (tmp) */
19721da177e4SLinus Torvalds 				break;
19731da177e4SLinus Torvalds 			case PHASE_MSGOUT:
19741da177e4SLinus Torvalds 				len = 1;
19751da177e4SLinus Torvalds 				data = &msgout;
19761da177e4SLinus Torvalds 				hostdata->last_message = msgout;
1977e7734ef1SAhmed S. Darwish 				NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
19781da177e4SLinus Torvalds 				if (msgout == ABORT) {
19791da177e4SLinus Torvalds 					hostdata->connected = NULL;
198045ddc1b2SFinn Thain 					hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
19811da177e4SLinus Torvalds 					cmd->result = DID_ERROR << 16;
1982677e0194SFinn Thain 					complete_cmd(instance, cmd);
19831da177e4SLinus Torvalds 					return;
19841da177e4SLinus Torvalds 				}
19851da177e4SLinus Torvalds 				msgout = NOP;
19861da177e4SLinus Torvalds 				break;
19871da177e4SLinus Torvalds 			case PHASE_CMDOUT:
19881da177e4SLinus Torvalds 				len = cmd->cmd_len;
19891da177e4SLinus Torvalds 				data = cmd->cmnd;
19901da177e4SLinus Torvalds 				/*
19911da177e4SLinus Torvalds 				 * XXX for performance reasons, on machines with a
19921da177e4SLinus Torvalds 				 * PSEUDO-DMA architecture we should probably
19931da177e4SLinus Torvalds 				 * use the dma transfer function.
19941da177e4SLinus Torvalds 				 */
1995e7734ef1SAhmed S. Darwish 				NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
19961da177e4SLinus Torvalds 				break;
19971da177e4SLinus Torvalds 			case PHASE_STATIN:
19981da177e4SLinus Torvalds 				len = 1;
19991da177e4SLinus Torvalds 				data = &tmp;
2000e7734ef1SAhmed S. Darwish 				NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
2001*ff1269cbSFinn Thain 				ncmd->status = tmp;
20021da177e4SLinus Torvalds 				break;
20031da177e4SLinus Torvalds 			default:
20046a6ff4acSFinn Thain 				shost_printk(KERN_ERR, instance, "unknown phase\n");
20054dde8f7dSFinn Thain 				NCR5380_dprint(NDEBUG_ANY, instance);
20061da177e4SLinus Torvalds 			} /* switch(phase) */
2007686f3990SFinn Thain 		} else {
200811d2f63bSFinn Thain 			spin_unlock_irq(&hostdata->lock);
2009d5d37a0aSFinn Thain 			NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
201011d2f63bSFinn Thain 			spin_lock_irq(&hostdata->lock);
20111da177e4SLinus Torvalds 		}
201211d2f63bSFinn Thain 	}
20131da177e4SLinus Torvalds }
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds /*
20161da177e4SLinus Torvalds  * Function : void NCR5380_reselect (struct Scsi_Host *instance)
20171da177e4SLinus Torvalds  *
20181da177e4SLinus Torvalds  * Purpose : does reselection, initializing the instance->connected
2019710ddd0dSFinn Thain  * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
20201da177e4SLinus Torvalds  * nexus has been reestablished,
20211da177e4SLinus Torvalds  *
20221da177e4SLinus Torvalds  * Inputs : instance - this instance of the NCR5380.
20231da177e4SLinus Torvalds  */
20241da177e4SLinus Torvalds 
20250d2cf867SFinn Thain static void NCR5380_reselect(struct Scsi_Host *instance)
20260d2cf867SFinn Thain {
2027e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
20281da177e4SLinus Torvalds 	unsigned char target_mask;
2029e9db3198SFinn Thain 	unsigned char lun;
20301da177e4SLinus Torvalds 	unsigned char msg[3];
203132b26a10SFinn Thain 	struct NCR5380_cmd *ncmd;
203232b26a10SFinn Thain 	struct scsi_cmnd *tmp;
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 	/*
20351da177e4SLinus Torvalds 	 * Disable arbitration, etc. since the host adapter obviously
20361da177e4SLinus Torvalds 	 * lost, and tell an interrupted NCR5380_select() to restart.
20371da177e4SLinus Torvalds 	 */
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds 	NCR5380_write(MODE_REG, MR_BASE);
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds 	target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
20427ef55f67SFinn Thain 	if (!target_mask || target_mask & (target_mask - 1)) {
20437ef55f67SFinn Thain 		shost_printk(KERN_WARNING, instance,
20447ef55f67SFinn Thain 			     "reselect: bad target_mask 0x%02x\n", target_mask);
20457ef55f67SFinn Thain 		return;
20467ef55f67SFinn Thain 	}
2047b746545fSFinn Thain 
20481da177e4SLinus Torvalds 	/*
20491da177e4SLinus Torvalds 	 * At this point, we have detected that our SCSI ID is on the bus,
20501da177e4SLinus Torvalds 	 * SEL is true and BSY was false for at least one bus settle delay
20511da177e4SLinus Torvalds 	 * (400 ns).
20521da177e4SLinus Torvalds 	 *
20531da177e4SLinus Torvalds 	 * We must assert BSY ourselves, until the target drops the SEL
20541da177e4SLinus Torvalds 	 * signal.
20551da177e4SLinus Torvalds 	 */
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2058d5d37a0aSFinn Thain 	if (NCR5380_poll_politely(hostdata,
2059e7734ef1SAhmed S. Darwish 	                          STATUS_REG, SR_SEL, 0, 0) < 0) {
206008267216SFinn Thain 		shost_printk(KERN_ERR, instance, "reselect: !SEL timeout\n");
206172064a78SFinn Thain 		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
206272064a78SFinn Thain 		return;
206372064a78SFinn Thain 	}
20641da177e4SLinus Torvalds 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	/*
20671da177e4SLinus Torvalds 	 * Wait for target to go into MSGIN.
20681da177e4SLinus Torvalds 	 */
20691da177e4SLinus Torvalds 
2070d5d37a0aSFinn Thain 	if (NCR5380_poll_politely(hostdata,
2071e7734ef1SAhmed S. Darwish 	                          STATUS_REG, SR_REQ, SR_REQ, 0) < 0) {
2072ca694afaSFinn Thain 		if ((NCR5380_read(STATUS_REG) & (SR_BSY | SR_SEL)) == 0)
2073ca694afaSFinn Thain 			/* BUS FREE phase */
2074ca694afaSFinn Thain 			return;
207508267216SFinn Thain 		shost_printk(KERN_ERR, instance, "reselect: REQ timeout\n");
2076e7734ef1SAhmed S. Darwish 		do_abort(instance, 0);
207772064a78SFinn Thain 		return;
207872064a78SFinn Thain 	}
20791da177e4SLinus Torvalds 
2080e9db3198SFinn Thain #ifdef CONFIG_SUN3
2081e9db3198SFinn Thain 	/* acknowledge toggle to MSGIN */
2082e9db3198SFinn Thain 	NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2083e9db3198SFinn Thain 
2084e9db3198SFinn Thain 	/* peek at the byte without really hitting the bus */
2085e9db3198SFinn Thain 	msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2086e9db3198SFinn Thain #else
2087e9db3198SFinn Thain 	{
2088e9db3198SFinn Thain 		int len = 1;
2089e9db3198SFinn Thain 		unsigned char *data = msg;
2090e9db3198SFinn Thain 		unsigned char phase = PHASE_MSGIN;
2091e9db3198SFinn Thain 
2092e7734ef1SAhmed S. Darwish 		NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
20931da177e4SLinus Torvalds 
209472064a78SFinn Thain 		if (len) {
2095e7734ef1SAhmed S. Darwish 			do_abort(instance, 0);
209672064a78SFinn Thain 			return;
209772064a78SFinn Thain 		}
2098e9db3198SFinn Thain 	}
2099e9db3198SFinn Thain #endif /* CONFIG_SUN3 */
210072064a78SFinn Thain 
21011da177e4SLinus Torvalds 	if (!(msg[0] & 0x80)) {
210272064a78SFinn Thain 		shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
21031abfd370SMatthew Wilcox 		spi_print_msg(msg);
210472064a78SFinn Thain 		printk("\n");
2105e7734ef1SAhmed S. Darwish 		do_abort(instance, 0);
210672064a78SFinn Thain 		return;
210772064a78SFinn Thain 	}
210872064a78SFinn Thain 	lun = msg[0] & 0x07;
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 	/*
21111da177e4SLinus Torvalds 	 * We need to add code for SCSI-II to track which devices have
21121da177e4SLinus Torvalds 	 * I_T_L_Q nexuses established, and which have simple I_T_L
21131da177e4SLinus Torvalds 	 * nexuses so we can chose to do additional data transfer.
21141da177e4SLinus Torvalds 	 */
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	/*
21171da177e4SLinus Torvalds 	 * Find the command corresponding to the I_T_L or I_T_L_Q  nexus we
21181da177e4SLinus Torvalds 	 * just reestablished, and remove it from the disconnected queue.
21191da177e4SLinus Torvalds 	 */
21201da177e4SLinus Torvalds 
212132b26a10SFinn Thain 	tmp = NULL;
212232b26a10SFinn Thain 	list_for_each_entry(ncmd, &hostdata->disconnected, list) {
212332b26a10SFinn Thain 		struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
212432b26a10SFinn Thain 
212532b26a10SFinn Thain 		if (target_mask == (1 << scmd_id(cmd)) &&
212632b26a10SFinn Thain 		    lun == (u8)cmd->device->lun) {
212732b26a10SFinn Thain 			list_del(&ncmd->list);
212832b26a10SFinn Thain 			tmp = cmd;
21291da177e4SLinus Torvalds 			break;
21301da177e4SLinus Torvalds 		}
213172064a78SFinn Thain 	}
21320d3d9a42SFinn Thain 
21330d3d9a42SFinn Thain 	if (tmp) {
21340d3d9a42SFinn Thain 		dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
21350d3d9a42SFinn Thain 		         "reselect: removed %p from disconnected queue\n", tmp);
21360d3d9a42SFinn Thain 	} else {
213745ddc1b2SFinn Thain 		int target = ffs(target_mask) - 1;
213845ddc1b2SFinn Thain 
213972064a78SFinn Thain 		shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
214072064a78SFinn Thain 		             target_mask, lun);
21411da177e4SLinus Torvalds 		/*
21420d2cf867SFinn Thain 		 * Since we have an established nexus that we can't do anything
21430d2cf867SFinn Thain 		 * with, we must abort it.
21441da177e4SLinus Torvalds 		 */
2145e7734ef1SAhmed S. Darwish 		if (do_abort(instance, 0) == 0)
214645ddc1b2SFinn Thain 			hostdata->busy[target] &= ~(1 << lun);
214772064a78SFinn Thain 		return;
21481da177e4SLinus Torvalds 	}
21491da177e4SLinus Torvalds 
2150e9db3198SFinn Thain #ifdef CONFIG_SUN3
21514a98f896SFinn Thain 	if (sun3_dma_setup_done != tmp) {
21524a98f896SFinn Thain 		int count;
2153e9db3198SFinn Thain 
2154*ff1269cbSFinn Thain 		advance_sg_buffer(ncmd);
2155e9db3198SFinn Thain 
21564a98f896SFinn Thain 		count = sun3scsi_dma_xfer_len(hostdata, tmp);
21574a98f896SFinn Thain 
21584a98f896SFinn Thain 		if (count > 0) {
21592e4b231aSBart Van Assche 			if (tmp->sc_data_direction == DMA_TO_DEVICE)
21604a98f896SFinn Thain 				sun3scsi_dma_send_setup(hostdata,
2161*ff1269cbSFinn Thain 							ncmd->ptr, count);
21624a98f896SFinn Thain 			else
21634a98f896SFinn Thain 				sun3scsi_dma_recv_setup(hostdata,
2164*ff1269cbSFinn Thain 							ncmd->ptr, count);
2165e9db3198SFinn Thain 			sun3_dma_setup_done = tmp;
2166e9db3198SFinn Thain 		}
2167e9db3198SFinn Thain 	}
2168e9db3198SFinn Thain 
2169e9db3198SFinn Thain 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2170e9db3198SFinn Thain #endif /* CONFIG_SUN3 */
2171e9db3198SFinn Thain 
217272064a78SFinn Thain 	/* Accept message by clearing ACK */
217372064a78SFinn Thain 	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
217472064a78SFinn Thain 
21751da177e4SLinus Torvalds 	hostdata->connected = tmp;
2176c4ec6f92SFinn Thain 	dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2177c4ec6f92SFinn Thain 	         scmd_id(tmp), tmp->device->lun);
21781da177e4SLinus Torvalds }
21791da177e4SLinus Torvalds 
21808b00c3d5SFinn Thain /**
21818b00c3d5SFinn Thain  * list_find_cmd - test for presence of a command in a linked list
21828b00c3d5SFinn Thain  * @haystack: list of commands
21838b00c3d5SFinn Thain  * @needle: command to search for
21848b00c3d5SFinn Thain  */
21858b00c3d5SFinn Thain 
21868b00c3d5SFinn Thain static bool list_find_cmd(struct list_head *haystack,
21878b00c3d5SFinn Thain                           struct scsi_cmnd *needle)
21888b00c3d5SFinn Thain {
21898b00c3d5SFinn Thain 	struct NCR5380_cmd *ncmd;
21908b00c3d5SFinn Thain 
21918b00c3d5SFinn Thain 	list_for_each_entry(ncmd, haystack, list)
21928b00c3d5SFinn Thain 		if (NCR5380_to_scmd(ncmd) == needle)
21938b00c3d5SFinn Thain 			return true;
21948b00c3d5SFinn Thain 	return false;
21958b00c3d5SFinn Thain }
21968b00c3d5SFinn Thain 
21978b00c3d5SFinn Thain /**
21988b00c3d5SFinn Thain  * list_remove_cmd - remove a command from linked list
21998b00c3d5SFinn Thain  * @haystack: list of commands
22008b00c3d5SFinn Thain  * @needle: command to remove
22018b00c3d5SFinn Thain  */
22028b00c3d5SFinn Thain 
22038b00c3d5SFinn Thain static bool list_del_cmd(struct list_head *haystack,
22048b00c3d5SFinn Thain                          struct scsi_cmnd *needle)
22058b00c3d5SFinn Thain {
22068b00c3d5SFinn Thain 	if (list_find_cmd(haystack, needle)) {
2207*ff1269cbSFinn Thain 		struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(needle);
22088b00c3d5SFinn Thain 
22098b00c3d5SFinn Thain 		list_del(&ncmd->list);
22108b00c3d5SFinn Thain 		return true;
22118b00c3d5SFinn Thain 	}
22128b00c3d5SFinn Thain 	return false;
22138b00c3d5SFinn Thain }
22148b00c3d5SFinn Thain 
22158b00c3d5SFinn Thain /**
22168b00c3d5SFinn Thain  * NCR5380_abort - scsi host eh_abort_handler() method
22178b00c3d5SFinn Thain  * @cmd: the command to be aborted
22181da177e4SLinus Torvalds  *
22198b00c3d5SFinn Thain  * Try to abort a given command by removing it from queues and/or sending
22208b00c3d5SFinn Thain  * the target an abort message. This may not succeed in causing a target
22218b00c3d5SFinn Thain  * to abort the command. Nonetheless, the low-level driver must forget about
22228b00c3d5SFinn Thain  * the command because the mid-layer reclaims it and it may be re-issued.
22231da177e4SLinus Torvalds  *
22248b00c3d5SFinn Thain  * The normal path taken by a command is as follows. For EH we trace this
22258b00c3d5SFinn Thain  * same path to locate and abort the command.
22261da177e4SLinus Torvalds  *
22278b00c3d5SFinn Thain  * unissued -> selecting -> [unissued -> selecting ->]... connected ->
22288b00c3d5SFinn Thain  * [disconnected -> connected ->]...
22298b00c3d5SFinn Thain  * [autosense -> connected ->] done
22301da177e4SLinus Torvalds  *
22318b00c3d5SFinn Thain  * If cmd was not found at all then presumably it has already been completed,
22328b00c3d5SFinn Thain  * in which case return SUCCESS to try to avoid further EH measures.
2233dc183965SFinn Thain  *
22348b00c3d5SFinn Thain  * If the command has not completed yet, we must not fail to find it.
2235dc183965SFinn Thain  * We have no option but to forget the aborted command (even if it still
2236dc183965SFinn Thain  * lacks sense data). The mid-layer may re-issue a command that is in error
2237dc183965SFinn Thain  * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2238dc183965SFinn Thain  * this driver are such that a command can appear on one queue only.
223971a00593SFinn Thain  *
224071a00593SFinn Thain  * The lock protects driver data structures, but EH handlers also use it
224171a00593SFinn Thain  * to serialize their own execution and prevent their own re-entry.
22421da177e4SLinus Torvalds  */
22431da177e4SLinus Torvalds 
2244710ddd0dSFinn Thain static int NCR5380_abort(struct scsi_cmnd *cmd)
2245710ddd0dSFinn Thain {
22461da177e4SLinus Torvalds 	struct Scsi_Host *instance = cmd->device->host;
2247e8a60144SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
224811d2f63bSFinn Thain 	unsigned long flags;
22498b00c3d5SFinn Thain 	int result = SUCCESS;
22501da177e4SLinus Torvalds 
225111d2f63bSFinn Thain 	spin_lock_irqsave(&hostdata->lock, flags);
225211d2f63bSFinn Thain 
225332b26a10SFinn Thain #if (NDEBUG & NDEBUG_ANY)
22548b00c3d5SFinn Thain 	scmd_printk(KERN_INFO, cmd, __func__);
225532b26a10SFinn Thain #endif
2256e5c3fddfSFinn Thain 	NCR5380_dprint(NDEBUG_ANY, instance);
2257e5c3fddfSFinn Thain 	NCR5380_dprint_phase(NDEBUG_ANY, instance);
22581da177e4SLinus Torvalds 
22598b00c3d5SFinn Thain 	if (list_del_cmd(&hostdata->unissued, cmd)) {
22608b00c3d5SFinn Thain 		dsprintk(NDEBUG_ABORT, instance,
22618b00c3d5SFinn Thain 		         "abort: removed %p from issue queue\n", cmd);
22628b00c3d5SFinn Thain 		cmd->result = DID_ABORT << 16;
2263117cd238SBart Van Assche 		scsi_done(cmd); /* No tag or busy flag to worry about */
2264dc183965SFinn Thain 		goto out;
22658b00c3d5SFinn Thain 	}
22668b00c3d5SFinn Thain 
2267707d62b3SFinn Thain 	if (hostdata->selecting == cmd) {
2268707d62b3SFinn Thain 		dsprintk(NDEBUG_ABORT, instance,
2269707d62b3SFinn Thain 		         "abort: cmd %p == selecting\n", cmd);
2270707d62b3SFinn Thain 		hostdata->selecting = NULL;
2271707d62b3SFinn Thain 		cmd->result = DID_ABORT << 16;
2272707d62b3SFinn Thain 		complete_cmd(instance, cmd);
2273707d62b3SFinn Thain 		goto out;
2274707d62b3SFinn Thain 	}
2275707d62b3SFinn Thain 
22768b00c3d5SFinn Thain 	if (list_del_cmd(&hostdata->disconnected, cmd)) {
22778b00c3d5SFinn Thain 		dsprintk(NDEBUG_ABORT, instance,
22788b00c3d5SFinn Thain 		         "abort: removed %p from disconnected list\n", cmd);
227971a00593SFinn Thain 		/* Can't call NCR5380_select() and send ABORT because that
228071a00593SFinn Thain 		 * means releasing the lock. Need a bus reset.
228171a00593SFinn Thain 		 */
2282dc183965SFinn Thain 		set_host_byte(cmd, DID_ERROR);
2283dc183965SFinn Thain 		complete_cmd(instance, cmd);
22848b00c3d5SFinn Thain 		result = FAILED;
22858b00c3d5SFinn Thain 		goto out;
22868b00c3d5SFinn Thain 	}
22878b00c3d5SFinn Thain 
22888b00c3d5SFinn Thain 	if (hostdata->connected == cmd) {
22898b00c3d5SFinn Thain 		dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
22908b00c3d5SFinn Thain 		hostdata->connected = NULL;
2291dc183965SFinn Thain 		hostdata->dma_len = 0;
2292e7734ef1SAhmed S. Darwish 		if (do_abort(instance, 0) < 0) {
22938b00c3d5SFinn Thain 			set_host_byte(cmd, DID_ERROR);
22948b00c3d5SFinn Thain 			complete_cmd(instance, cmd);
22958b00c3d5SFinn Thain 			result = FAILED;
22968b00c3d5SFinn Thain 			goto out;
22978b00c3d5SFinn Thain 		}
22988b00c3d5SFinn Thain 		set_host_byte(cmd, DID_ABORT);
22998b00c3d5SFinn Thain 		complete_cmd(instance, cmd);
2300dc183965SFinn Thain 		goto out;
23018b00c3d5SFinn Thain 	}
23028b00c3d5SFinn Thain 
23038b00c3d5SFinn Thain 	if (list_del_cmd(&hostdata->autosense, cmd)) {
23048b00c3d5SFinn Thain 		dsprintk(NDEBUG_ABORT, instance,
23058b00c3d5SFinn Thain 		         "abort: removed %p from sense queue\n", cmd);
23068b00c3d5SFinn Thain 		complete_cmd(instance, cmd);
23078b00c3d5SFinn Thain 	}
23088b00c3d5SFinn Thain 
23098b00c3d5SFinn Thain out:
23108b00c3d5SFinn Thain 	if (result == FAILED)
23118b00c3d5SFinn Thain 		dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
231245ddc1b2SFinn Thain 	else {
231345ddc1b2SFinn Thain 		hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
23148b00c3d5SFinn Thain 		dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
231545ddc1b2SFinn Thain 	}
23168b00c3d5SFinn Thain 
23178b00c3d5SFinn Thain 	queue_work(hostdata->work_q, &hostdata->main_task);
231811d2f63bSFinn Thain 	spin_unlock_irqrestore(&hostdata->lock, flags);
23191da177e4SLinus Torvalds 
23208b00c3d5SFinn Thain 	return result;
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds 
23246b0e87a6SFinn Thain static void bus_reset_cleanup(struct Scsi_Host *instance)
232568b3aa7cSJeff Garzik  {
232611d2f63bSFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
232762717f53SFinn Thain 	int i;
232862717f53SFinn Thain 	struct NCR5380_cmd *ncmd;
23291da177e4SLinus Torvalds 
233062717f53SFinn Thain 	/* reset NCR registers */
233162717f53SFinn Thain 	NCR5380_write(MODE_REG, MR_BASE);
233262717f53SFinn Thain 	NCR5380_write(TARGET_COMMAND_REG, 0);
233362717f53SFinn Thain 	NCR5380_write(SELECT_ENABLE_REG, 0);
233462717f53SFinn Thain 
233562717f53SFinn Thain 	/* After the reset, there are no more connected or disconnected commands
233662717f53SFinn Thain 	 * and no busy units; so clear the low-level status here to avoid
233762717f53SFinn Thain 	 * conflicts when the mid-level code tries to wake up the affected
233862717f53SFinn Thain 	 * commands!
233962717f53SFinn Thain 	 */
234062717f53SFinn Thain 
23411884c283SFinn Thain 	if (hostdata->selecting) {
23421884c283SFinn Thain 		hostdata->selecting->result = DID_RESET << 16;
23431884c283SFinn Thain 		complete_cmd(instance, hostdata->selecting);
234462717f53SFinn Thain 		hostdata->selecting = NULL;
23451884c283SFinn Thain 	}
234662717f53SFinn Thain 
234762717f53SFinn Thain 	list_for_each_entry(ncmd, &hostdata->disconnected, list) {
234862717f53SFinn Thain 		struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
234962717f53SFinn Thain 
235062717f53SFinn Thain 		set_host_byte(cmd, DID_RESET);
2351216fad91SFinn Thain 		complete_cmd(instance, cmd);
235262717f53SFinn Thain 	}
23531884c283SFinn Thain 	INIT_LIST_HEAD(&hostdata->disconnected);
235462717f53SFinn Thain 
235562717f53SFinn Thain 	list_for_each_entry(ncmd, &hostdata->autosense, list) {
235662717f53SFinn Thain 		struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
235762717f53SFinn Thain 
2358117cd238SBart Van Assche 		scsi_done(cmd);
235962717f53SFinn Thain 	}
23601884c283SFinn Thain 	INIT_LIST_HEAD(&hostdata->autosense);
236162717f53SFinn Thain 
236262717f53SFinn Thain 	if (hostdata->connected) {
236362717f53SFinn Thain 		set_host_byte(hostdata->connected, DID_RESET);
236462717f53SFinn Thain 		complete_cmd(instance, hostdata->connected);
236562717f53SFinn Thain 		hostdata->connected = NULL;
236662717f53SFinn Thain 	}
236762717f53SFinn Thain 
236862717f53SFinn Thain 	for (i = 0; i < 8; ++i)
236962717f53SFinn Thain 		hostdata->busy[i] = 0;
237062717f53SFinn Thain 	hostdata->dma_len = 0;
237162717f53SFinn Thain 
237262717f53SFinn Thain 	queue_work(hostdata->work_q, &hostdata->main_task);
23736b0e87a6SFinn Thain }
23746b0e87a6SFinn Thain 
23756b0e87a6SFinn Thain /**
23766b0e87a6SFinn Thain  * NCR5380_host_reset - reset the SCSI host
23776b0e87a6SFinn Thain  * @cmd: SCSI command undergoing EH
23786b0e87a6SFinn Thain  *
23796b0e87a6SFinn Thain  * Returns SUCCESS
23806b0e87a6SFinn Thain  */
23816b0e87a6SFinn Thain 
23826b0e87a6SFinn Thain static int NCR5380_host_reset(struct scsi_cmnd *cmd)
23836b0e87a6SFinn Thain {
23846b0e87a6SFinn Thain 	struct Scsi_Host *instance = cmd->device->host;
23856b0e87a6SFinn Thain 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
23866b0e87a6SFinn Thain 	unsigned long flags;
23876b0e87a6SFinn Thain 	struct NCR5380_cmd *ncmd;
23886b0e87a6SFinn Thain 
23896b0e87a6SFinn Thain 	spin_lock_irqsave(&hostdata->lock, flags);
23906b0e87a6SFinn Thain 
23916b0e87a6SFinn Thain #if (NDEBUG & NDEBUG_ANY)
23926b0e87a6SFinn Thain 	shost_printk(KERN_INFO, instance, __func__);
23936b0e87a6SFinn Thain #endif
23946b0e87a6SFinn Thain 	NCR5380_dprint(NDEBUG_ANY, instance);
23956b0e87a6SFinn Thain 	NCR5380_dprint_phase(NDEBUG_ANY, instance);
23966b0e87a6SFinn Thain 
23976b0e87a6SFinn Thain 	list_for_each_entry(ncmd, &hostdata->unissued, list) {
23986b0e87a6SFinn Thain 		struct scsi_cmnd *scmd = NCR5380_to_scmd(ncmd);
23996b0e87a6SFinn Thain 
24006b0e87a6SFinn Thain 		scmd->result = DID_RESET << 16;
2401117cd238SBart Van Assche 		scsi_done(scmd);
24026b0e87a6SFinn Thain 	}
24036b0e87a6SFinn Thain 	INIT_LIST_HEAD(&hostdata->unissued);
24046b0e87a6SFinn Thain 
24056b0e87a6SFinn Thain 	do_reset(instance);
24066b0e87a6SFinn Thain 	bus_reset_cleanup(instance);
24076b0e87a6SFinn Thain 
240811d2f63bSFinn Thain 	spin_unlock_irqrestore(&hostdata->lock, flags);
240968b3aa7cSJeff Garzik  
24101da177e4SLinus Torvalds 	return SUCCESS;
24111da177e4SLinus Torvalds }
2412