blob: 90ea0f5d9bdbbfc78da0b474a1490b184f3bde25 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Finn Thainaff0cf92016-01-03 16:06:09 +11002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * NCR 5380 generic driver routines. These should make it *trivial*
Finn Thain594d4ba2016-01-03 16:06:10 +11004 * to implement 5380 SCSI drivers under Linux with a non-trantor
5 * architecture.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Finn Thain594d4ba2016-01-03 16:06:10 +11007 * Note that these routines also work with NR53c400 family chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright 1993, Drew Eckhardt
Finn Thain594d4ba2016-01-03 16:06:10 +110010 * Visionary Computing
11 * (Unix and Linux consulting and custom programming)
12 * drew@colorado.edu
13 * +1 (303) 666-5836
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Finn Thainaff0cf92016-01-03 16:06:09 +110015 * For more information, please consult
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * NCR 5380 Family
18 * SCSI Protocol Controller
19 * Databook
20 *
21 * NCR Microelectronics
22 * 1635 Aeroplaza Drive
23 * Colorado Springs, CO 80916
24 * 1+ (719) 578-3400
25 * 1+ (800) 334-5454
26 */
27
28/*
Finn Thainc16df322016-01-03 16:06:08 +110029 * With contributions from Ray Van Tassle, Ingmar Baumgart,
30 * Ronald van Cuijlenborg, Alan Cox and others.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
Finn Thain52d3e562016-03-23 21:10:20 +110033/* Ported to Atari by Roman Hodek and others. */
34
Finn Thaine9db3192016-03-23 21:10:21 +110035/* Adapted for the Sun 3 by Sam Creasey. */
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Design
39 *
Finn Thainaff0cf92016-01-03 16:06:09 +110040 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * one simply writes appropriate system specific macros (ie, data
Finn Thainaff0cf92016-01-03 16:06:09 +110042 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * memory mapped) and drops this file in their 'C' wrapper.
44 *
Finn Thainaff0cf92016-01-03 16:06:09 +110045 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * each 5380 in the system - commands that haven't been issued yet,
Finn Thainaff0cf92016-01-03 16:06:09 +110047 * and commands that are currently executing. This means that an
48 * unlimited number of commands may be queued, letting
49 * more commands propagate from the higher driver levels giving higher
50 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
51 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * while a command is already executing.
53 *
54 *
Finn Thainaff0cf92016-01-03 16:06:09 +110055 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 *
Finn Thainaff0cf92016-01-03 16:06:09 +110057 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
58 * piece of hardware that requires you to sit in a loop polling for
59 * the REQ signal as long as you are connected. Some devices are
60 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +110061 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * broken devices are the exception rather than the rule and I'd rather
63 * spend my time optimizing for the normal case.
64 *
65 * Architecture :
66 *
67 * At the heart of the design is a coroutine, NCR5380_main,
68 * which is started from a workqueue for each NCR5380 host in the
69 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
70 * removing the commands from the issue queue and calling
Finn Thainaff0cf92016-01-03 16:06:09 +110071 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
73 * Once a nexus is established, the NCR5380_information_transfer()
74 * phase goes through the various phases as instructed by the target.
75 * if the target goes into MSG IN and sends a DISCONNECT message,
76 * the command structure is placed into the per instance disconnected
Finn Thainaff0cf92016-01-03 16:06:09 +110077 * queue, and NCR5380_main tries to find more work. If the target is
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * idle for too long, the system will try to sleep.
79 *
80 * If a command has disconnected, eventually an interrupt will trigger,
81 * calling NCR5380_intr() which will in turn call NCR5380_reselect
82 * to reestablish a nexus. This will run main if necessary.
83 *
Finn Thainaff0cf92016-01-03 16:06:09 +110084 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * appropriate.
86 *
Finn Thainaff0cf92016-01-03 16:06:09 +110087 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * structures, being initialized after the command is connected
89 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
90 * Note that in violation of the standard, an implicit SAVE POINTERS operation
91 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
92 */
93
94/*
95 * Using this file :
96 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Finn Thainaff0cf92016-01-03 16:06:09 +110097 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * and macros and include this file in your driver.
99 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * These macros MUST be defined :
Finn Thainaff0cf92016-01-03 16:06:09 +1100101 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * NCR5380_read(register) - read from the specified register
103 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100104 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100106 * NCR5380_implementation_fields - additional fields needed for this
Finn Thain594d4ba2016-01-03 16:06:10 +1100107 * specific implementation of the NCR5380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
109 * Either real DMA *or* pseudo DMA may be implemented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
Finn Thain4a98f892016-10-10 00:46:53 -0400111 * NCR5380_dma_xfer_len - determine size of DMA/PDMA transfer
112 * NCR5380_dma_send_setup - execute DMA/PDMA from memory to 5380
113 * NCR5380_dma_recv_setup - execute DMA/PDMA from 5380 to memory
114 * NCR5380_dma_residual - residual byte count
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * The generic driver is initialized by calling NCR5380_init(instance),
Ondrej Zary906e4a3c2016-12-05 01:07:20 -0500117 * after setting the appropriate host specific fields and ID.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119
Finn Thaine5d55d12016-03-23 21:10:16 +1100120#ifndef NCR5380_io_delay
121#define NCR5380_io_delay(x)
122#endif
123
Finn Thain52d3e562016-03-23 21:10:20 +1100124#ifndef NCR5380_acquire_dma_irq
125#define NCR5380_acquire_dma_irq(x) (1)
126#endif
127
128#ifndef NCR5380_release_dma_irq
129#define NCR5380_release_dma_irq(x)
130#endif
131
Finn Thain54d8fe42016-01-03 16:05:06 +1100132static int do_abort(struct Scsi_Host *);
133static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Finn Thainc16df322016-01-03 16:06:08 +1100135/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100136 * initialize_SCp - init the scsi pointer field
Finn Thain594d4ba2016-01-03 16:06:10 +1100137 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100139 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
141
Finn Thain710ddd02014-11-12 16:12:02 +1100142static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Finn Thainaff0cf92016-01-03 16:06:09 +1100144 /*
145 * Initialize the Scsi Pointer field so that all of the commands in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * various queues are valid.
147 */
148
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200149 if (scsi_bufflen(cmd)) {
150 cmd->SCp.buffer = scsi_sglist(cmd);
151 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200152 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 cmd->SCp.this_residual = cmd->SCp.buffer->length;
154 } else {
155 cmd->SCp.buffer = NULL;
156 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200157 cmd->SCp.ptr = NULL;
158 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100160
161 cmd->SCp.Status = 0;
162 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165/**
Finn Thainb32ade12016-01-03 16:05:41 +1100166 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thaind5d37a02016-10-10 00:46:53 -0400167 * @hostdata: host private data
Finn Thainb32ade12016-01-03 16:05:41 +1100168 * @reg1: 5380 register to poll
169 * @bit1: Bitmask to check
170 * @val1: Expected value
171 * @reg2: Second 5380 register to poll
172 * @bit2: Second bitmask to check
173 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100174 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
Finn Thain2f854b82016-01-03 16:05:22 +1100176 * Polls the chip in a reasonably efficient manner waiting for an
177 * event to occur. After a short quick poll we begin to yield the CPU
178 * (if possible). In irq contexts the time-out is arbitrarily limited.
179 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 *
Finn Thainb32ade12016-01-03 16:05:41 +1100181 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Finn Thaind5d37a02016-10-10 00:46:53 -0400184static int NCR5380_poll_politely2(struct NCR5380_hostdata *hostdata,
Finn Thain61e1ce52016-10-10 00:46:53 -0400185 unsigned int reg1, u8 bit1, u8 val1,
186 unsigned int reg2, u8 bit2, u8 val2,
187 unsigned long wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100188{
Finn Thaind4408dd2016-10-10 00:46:52 -0400189 unsigned long n = hostdata->poll_loops;
Finn Thain2f854b82016-01-03 16:05:22 +1100190 unsigned long deadline = jiffies + wait;
Finn Thain2f854b82016-01-03 16:05:22 +1100191
Finn Thain2f854b82016-01-03 16:05:22 +1100192 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100193 if ((NCR5380_read(reg1) & bit1) == val1)
194 return 0;
195 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 0;
197 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100198 } while (n--);
199
200 if (irqs_disabled() || in_interrupt())
201 return -ETIMEDOUT;
202
203 /* Repeatedly sleep for 1 ms until deadline */
204 while (time_is_after_jiffies(deadline)) {
205 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100206 if ((NCR5380_read(reg1) & bit1) == val1)
207 return 0;
208 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Finn Thain2f854b82016-01-03 16:05:22 +1100211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return -ETIMEDOUT;
213}
214
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100215#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static struct {
217 unsigned char mask;
218 const char *name;
Finn Thainaff0cf92016-01-03 16:06:09 +1100219} signals[] = {
220 {SR_DBP, "PARITY"},
221 {SR_RST, "RST"},
222 {SR_BSY, "BSY"},
223 {SR_REQ, "REQ"},
224 {SR_MSG, "MSG"},
225 {SR_CD, "CD"},
226 {SR_IO, "IO"},
227 {SR_SEL, "SEL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100229},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230basrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100231 {BASR_END_DMA_TRANSFER, "END OF DMA"},
232 {BASR_DRQ, "DRQ"},
233 {BASR_PARITY_ERROR, "PARITY ERROR"},
234 {BASR_IRQ, "IRQ"},
235 {BASR_PHASE_MATCH, "PHASE MATCH"},
236 {BASR_BUSY_ERROR, "BUSY ERROR"},
Finn Thainaff0cf92016-01-03 16:06:09 +1100237 {BASR_ATN, "ATN"},
238 {BASR_ACK, "ACK"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100240},
241icrs[] = {
242 {ICR_ASSERT_RST, "ASSERT RST"},
Finn Thain12866b92016-03-23 21:10:25 +1100243 {ICR_ARBITRATION_PROGRESS, "ARB. IN PROGRESS"},
244 {ICR_ARBITRATION_LOST, "LOST ARB."},
Finn Thainaff0cf92016-01-03 16:06:09 +1100245 {ICR_ASSERT_ACK, "ASSERT ACK"},
246 {ICR_ASSERT_BSY, "ASSERT BSY"},
247 {ICR_ASSERT_SEL, "ASSERT SEL"},
248 {ICR_ASSERT_ATN, "ASSERT ATN"},
249 {ICR_ASSERT_DATA, "ASSERT DATA"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100251},
252mrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100253 {MR_BLOCK_DMA_MODE, "BLOCK DMA MODE"},
254 {MR_TARGET, "TARGET"},
255 {MR_ENABLE_PAR_CHECK, "PARITY CHECK"},
256 {MR_ENABLE_PAR_INTR, "PARITY INTR"},
257 {MR_ENABLE_EOP_INTR, "EOP INTR"},
258 {MR_MONITOR_BSY, "MONITOR BSY"},
259 {MR_DMA_MODE, "DMA MODE"},
260 {MR_ARBITRATE, "ARBITRATE"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 {0, NULL}
262};
263
264/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100265 * NCR5380_print - print scsi bus signals
266 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100268 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
270
271static void NCR5380_print(struct Scsi_Host *instance)
272{
Finn Thain61e1ce52016-10-10 00:46:53 -0400273 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
277 status = NCR5380_read(STATUS_REG);
278 mr = NCR5380_read(MODE_REG);
279 icr = NCR5380_read(INITIATOR_COMMAND_REG);
280 basr = NCR5380_read(BUS_AND_STATUS_REG);
281
Finn Thain12866b92016-03-23 21:10:25 +1100282 printk(KERN_DEBUG "SR = 0x%02x : ", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 for (i = 0; signals[i].mask; ++i)
284 if (status & signals[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100285 printk(KERN_CONT "%s, ", signals[i].name);
286 printk(KERN_CONT "\nBASR = 0x%02x : ", basr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 for (i = 0; basrs[i].mask; ++i)
288 if (basr & basrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100289 printk(KERN_CONT "%s, ", basrs[i].name);
290 printk(KERN_CONT "\nICR = 0x%02x : ", icr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 for (i = 0; icrs[i].mask; ++i)
292 if (icr & icrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100293 printk(KERN_CONT "%s, ", icrs[i].name);
294 printk(KERN_CONT "\nMR = 0x%02x : ", mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 for (i = 0; mrs[i].mask; ++i)
296 if (mr & mrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100297 printk(KERN_CONT "%s, ", mrs[i].name);
298 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Finn Thain0d2cf862016-01-03 16:06:11 +1100301static struct {
302 unsigned char value;
303 const char *name;
304} phases[] = {
305 {PHASE_DATAOUT, "DATAOUT"},
306 {PHASE_DATAIN, "DATAIN"},
307 {PHASE_CMDOUT, "CMDOUT"},
308 {PHASE_STATIN, "STATIN"},
309 {PHASE_MSGOUT, "MSGOUT"},
310 {PHASE_MSGIN, "MSGIN"},
311 {PHASE_UNKNOWN, "UNKNOWN"}
312};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Finn Thainc16df322016-01-03 16:06:08 +1100314/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100315 * NCR5380_print_phase - show SCSI phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100316 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100318 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
320
321static void NCR5380_print_phase(struct Scsi_Host *instance)
322{
Finn Thain61e1ce52016-10-10 00:46:53 -0400323 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned char status;
325 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 status = NCR5380_read(STATUS_REG);
328 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100329 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 else {
Finn Thain0d2cf862016-01-03 16:06:11 +1100331 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
332 (phases[i].value != (status & PHASE_MASK)); ++i)
333 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100334 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336}
337#endif
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/**
Finn Thain09028462017-01-15 18:50:57 -0500340 * NCR5380_info - report driver and host information
Finn Thain594d4ba2016-01-03 16:06:10 +1100341 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100343 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
345
Finn Thain8c325132014-11-12 16:11:58 +1100346static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Finn Thain8c325132014-11-12 16:11:58 +1100348 struct NCR5380_hostdata *hostdata = shost_priv(instance);
349
350 return hostdata->info;
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100354 * NCR5380_init - initialise an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100355 * @instance: adapter to configure
356 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100358 * Initializes *instance and corresponding 5380 chip,
359 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100361 * Notes : I assume that the host, hostno, and id bits have been
Finn Thain0d2cf862016-01-03 16:06:11 +1100362 * set correctly. I don't care about the irq and other fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100364 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
366
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800367static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Finn Thaine8a60142016-01-03 16:05:54 +1100369 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100370 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100371 unsigned long deadline;
Finn Thaind4408dd2016-10-10 00:46:52 -0400372 unsigned long accesses_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Finn Thainae5e33a2016-03-23 21:10:23 +1100374 instance->max_lun = 7;
375
Finn Thain0d2cf862016-01-03 16:06:11 +1100376 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 hostdata->id_mask = 1 << instance->this_id;
Finn Thain0d2cf862016-01-03 16:06:11 +1100378 hostdata->id_higher_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
380 if (i > hostdata->id_mask)
381 hostdata->id_higher_mask |= i;
382 for (i = 0; i < 8; ++i)
383 hostdata->busy[i] = 0;
Finn Thaine4dec682016-03-23 21:10:12 +1100384 hostdata->dma_len = 0;
385
Finn Thain11d2f632016-01-03 16:05:51 +1100386 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100388 hostdata->sensing = NULL;
389 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100390 INIT_LIST_HEAD(&hostdata->unissued);
391 INIT_LIST_HEAD(&hostdata->disconnected);
392
Finn Thain55181be2016-01-03 16:05:42 +1100393 hostdata->flags = flags;
Finn Thainaff0cf92016-01-03 16:06:09 +1100394
Finn Thain8d8601a2016-01-03 16:05:37 +1100395 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100396 hostdata->work_q = alloc_workqueue("ncr5380_%d",
397 WQ_UNBOUND | WQ_MEM_RECLAIM,
398 1, instance->host_no);
399 if (!hostdata->work_q)
400 return -ENOMEM;
401
Finn Thain09028462017-01-15 18:50:57 -0500402 snprintf(hostdata->info, sizeof(hostdata->info),
403 "%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}",
404 instance->hostt->name, instance->irq, hostdata->io_port,
405 hostdata->base, instance->can_queue, instance->cmd_per_lun,
406 instance->sg_tablesize, instance->this_id,
407 hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
408 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
409 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "");
Finn Thain8c325132014-11-12 16:11:58 +1100410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
412 NCR5380_write(MODE_REG, MR_BASE);
413 NCR5380_write(TARGET_COMMAND_REG, 0);
414 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100415
416 /* Calibrate register polling loop */
417 i = 0;
418 deadline = jiffies + 1;
419 do {
420 cpu_relax();
421 } while (time_is_after_jiffies(deadline));
422 deadline += msecs_to_jiffies(256);
423 do {
424 NCR5380_read(STATUS_REG);
425 ++i;
426 cpu_relax();
427 } while (time_is_after_jiffies(deadline));
Finn Thaind4408dd2016-10-10 00:46:52 -0400428 accesses_per_ms = i / 256;
429 hostdata->poll_loops = NCR5380_REG_POLL_TIME * accesses_per_ms / 2;
Finn Thain2f854b82016-01-03 16:05:22 +1100430
Finn Thainb6488f92016-01-03 16:05:08 +1100431 return 0;
432}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Finn Thainb6488f92016-01-03 16:05:08 +1100434/**
435 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
436 * @instance: adapter to check
437 *
438 * If the system crashed, it may have crashed with a connected target and
439 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
440 * currently established nexus, which we know nothing about. Failing that
441 * do a bus reset.
442 *
443 * Note that a bus reset will cause the chip to assert IRQ.
444 *
445 * Returns 0 if successful, otherwise -ENXIO.
446 */
447
448static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
449{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100450 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100451 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
454 switch (pass) {
455 case 1:
456 case 3:
457 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100458 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
Finn Thaind5d37a02016-10-10 00:46:53 -0400459 NCR5380_poll_politely(hostdata,
Finn Thain636b1ec2016-01-03 16:05:10 +1100460 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
462 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100463 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 do_abort(instance);
465 break;
466 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100467 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100469 /* Wait after a reset; the SCSI standard calls for
470 * 250ms, we wait 500ms to be on the safe side.
471 * But some Toshiba CD-ROMs need ten times that.
472 */
473 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
474 msleep(2500);
475 else
476 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 break;
478 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100479 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return -ENXIO;
481 }
482 }
483 return 0;
484}
485
486/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100487 * NCR5380_exit - remove an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100488 * @instance: adapter to remove
Finn Thain0d2cf862016-01-03 16:06:11 +1100489 *
490 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
492
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800493static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Finn Thaine8a60142016-01-03 16:05:54 +1100495 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Finn Thain8d8601a2016-01-03 16:05:37 +1100497 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100498 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501/**
Finn Thain677e0192016-01-03 16:05:59 +1100502 * complete_cmd - finish processing a command and return it to the SCSI ML
503 * @instance: the host instance
504 * @cmd: command to complete
505 */
506
507static void complete_cmd(struct Scsi_Host *instance,
508 struct scsi_cmnd *cmd)
509{
510 struct NCR5380_hostdata *hostdata = shost_priv(instance);
511
512 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
513
Finn Thainf27db8e2016-01-03 16:06:00 +1100514 if (hostdata->sensing == cmd) {
515 /* Autosense processing ends here */
516 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
517 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
518 set_host_byte(cmd, DID_ERROR);
519 } else
520 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
521 hostdata->sensing = NULL;
522 }
523
Finn Thain677e0192016-01-03 16:05:59 +1100524 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
525
526 cmd->scsi_done(cmd);
527}
528
529/**
Finn Thain1bb40582016-01-03 16:05:29 +1100530 * NCR5380_queue_command - queue a command
531 * @instance: the relevant SCSI adapter
532 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 *
Finn Thain1bb40582016-01-03 16:05:29 +1100534 * cmd is added to the per-instance issue queue, with minor
535 * twiddling done to the host specific fields of cmd. If the
536 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
538
Finn Thain1bb40582016-01-03 16:05:29 +1100539static int NCR5380_queue_command(struct Scsi_Host *instance,
540 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Finn Thain1bb40582016-01-03 16:05:29 +1100542 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100543 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100544 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546#if (NDEBUG & NDEBUG_NO_WRITE)
547 switch (cmd->cmnd[0]) {
548 case WRITE_6:
549 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100550 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100552 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 0;
554 }
Finn Thain0d2cf862016-01-03 16:06:11 +1100555#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 cmd->result = 0;
558
Finn Thain52d3e562016-03-23 21:10:20 +1100559 if (!NCR5380_acquire_dma_irq(instance))
560 return SCSI_MLQUEUE_HOST_BUSY;
561
Finn Thain11d2f632016-01-03 16:05:51 +1100562 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100563
Finn Thainaff0cf92016-01-03 16:06:09 +1100564 /*
565 * Insert the cmd into the issue queue. Note that REQUEST SENSE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * commands are added to the head of the queue since any command will
Finn Thainaff0cf92016-01-03 16:06:09 +1100567 * clear the contingent allegiance condition that exists and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 * sense data is only guaranteed to be valid while the condition exists.
569 */
570
Finn Thain32b26a12016-01-03 16:05:58 +1100571 if (cmd->cmnd[0] == REQUEST_SENSE)
572 list_add(&ncmd->list, &hostdata->unissued);
573 else
574 list_add_tail(&ncmd->list, &hostdata->unissued);
575
Finn Thain11d2f632016-01-03 16:05:51 +1100576 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100577
Finn Thaindbb6b352016-01-03 16:05:53 +1100578 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
579 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100582 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
584}
585
Finn Thain52d3e562016-03-23 21:10:20 +1100586static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
587{
588 struct NCR5380_hostdata *hostdata = shost_priv(instance);
589
590 /* Caller does the locking needed to set & test these data atomically */
591 if (list_empty(&hostdata->disconnected) &&
592 list_empty(&hostdata->unissued) &&
593 list_empty(&hostdata->autosense) &&
594 !hostdata->connected &&
Finn Thain4ab2a782017-01-15 18:50:57 -0500595 !hostdata->selecting) {
Finn Thain52d3e562016-03-23 21:10:20 +1100596 NCR5380_release_dma_irq(instance);
Finn Thain4ab2a782017-01-15 18:50:57 -0500597 }
Finn Thain52d3e562016-03-23 21:10:20 +1100598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100601 * dequeue_next_cmd - dequeue a command for processing
602 * @instance: the scsi host instance
603 *
604 * Priority is given to commands on the autosense queue. These commands
605 * need autosense because of a CHECK CONDITION result.
606 *
607 * Returns a command pointer if a command is found for a target that is
608 * not already busy. Otherwise returns NULL.
609 */
610
611static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
612{
613 struct NCR5380_hostdata *hostdata = shost_priv(instance);
614 struct NCR5380_cmd *ncmd;
615 struct scsi_cmnd *cmd;
616
Finn Thain8d5dbec2016-02-23 10:07:09 +1100617 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100618 list_for_each_entry(ncmd, &hostdata->unissued, list) {
619 cmd = NCR5380_to_scmd(ncmd);
620 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
621 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
622
623 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
624 list_del(&ncmd->list);
625 dsprintk(NDEBUG_QUEUES, instance,
626 "dequeue: removed %p from issue queue\n", cmd);
627 return cmd;
628 }
629 }
630 } else {
631 /* Autosense processing begins here */
632 ncmd = list_first_entry(&hostdata->autosense,
633 struct NCR5380_cmd, list);
634 list_del(&ncmd->list);
635 cmd = NCR5380_to_scmd(ncmd);
636 dsprintk(NDEBUG_QUEUES, instance,
637 "dequeue: removed %p from autosense queue\n", cmd);
638 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
639 hostdata->sensing = cmd;
640 return cmd;
641 }
642 return NULL;
643}
644
645static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
646{
647 struct NCR5380_hostdata *hostdata = shost_priv(instance);
648 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
649
Finn Thain8d5dbec2016-02-23 10:07:09 +1100650 if (hostdata->sensing == cmd) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100651 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
652 list_add(&ncmd->list, &hostdata->autosense);
653 hostdata->sensing = NULL;
654 } else
655 list_add(&ncmd->list, &hostdata->unissued);
656}
657
658/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100659 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100661 * NCR5380_main is a coroutine that runs as long as more work can
662 * be done on the NCR5380 host adapters in a system. Both
663 * NCR5380_queue_command() and NCR5380_intr() will try to start it
664 * in case it is not running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
666
David Howellsc4028952006-11-22 14:57:56 +0000667static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
David Howellsc4028952006-11-22 14:57:56 +0000669 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100670 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct Scsi_Host *instance = hostdata->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int done;
Finn Thainaff0cf92016-01-03 16:06:09 +1100673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100676
Finn Thain0a4e3612016-01-03 16:06:07 +1100677 spin_lock_irq(&hostdata->lock);
Finn Thainccf6efd2016-02-23 10:07:08 +1100678 while (!hostdata->connected && !hostdata->selecting) {
679 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
680
681 if (!cmd)
682 break;
Finn Thainf27db8e2016-01-03 16:06:00 +1100683
684 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100687 * Attempt to establish an I_T_L nexus here.
688 * On success, instance->hostdata->connected is set.
689 * On failure, we must add the command back to the
690 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100692 /*
693 * REQUEST SENSE commands are issued without tagged
694 * queueing, even on SCSI-II devices because the
695 * contingent allegiance condition exists for the
696 * entire unit.
697 */
Finn Thain32b26a12016-01-03 16:05:58 +1100698
Finn Thainccf6efd2016-02-23 10:07:08 +1100699 if (!NCR5380_select(instance, cmd)) {
Finn Thain707d62b2016-01-03 16:06:02 +1100700 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thain52d3e562016-03-23 21:10:20 +1100701 maybe_release_dma_irq(instance);
Finn Thainf27db8e2016-01-03 16:06:00 +1100702 } else {
703 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
704 "main: select failed, returning %p to queue\n", cmd);
705 requeue_cmd(instance, cmd);
706 }
707 }
Finn Thaine4dec682016-03-23 21:10:12 +1100708 if (hostdata->connected && !hostdata->dma_len) {
Finn Thainb7465452016-01-03 16:06:05 +1100709 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100712 }
Finn Thain0a4e3612016-01-03 16:06:07 +1100713 spin_unlock_irq(&hostdata->lock);
714 if (!done)
715 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Finn Thain8053b0e2016-03-23 21:10:19 +1100719/*
720 * NCR5380_dma_complete - finish DMA transfer
721 * @instance: the scsi host instance
722 *
723 * Called by the interrupt handler when DMA finishes or a phase
724 * mismatch occurs (which would end the DMA transfer).
725 */
726
727static void NCR5380_dma_complete(struct Scsi_Host *instance)
728{
729 struct NCR5380_hostdata *hostdata = shost_priv(instance);
730 int transferred;
731 unsigned char **data;
732 int *count;
733 int saved_data = 0, overrun = 0;
734 unsigned char p;
735
736 if (hostdata->read_overruns) {
737 p = hostdata->connected->SCp.phase;
738 if (p & SR_IO) {
739 udelay(10);
740 if ((NCR5380_read(BUS_AND_STATUS_REG) &
741 (BASR_PHASE_MATCH | BASR_ACK)) ==
742 (BASR_PHASE_MATCH | BASR_ACK)) {
743 saved_data = NCR5380_read(INPUT_DATA_REG);
744 overrun = 1;
745 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
746 }
747 }
748 }
749
Finn Thaine9db3192016-03-23 21:10:21 +1100750#ifdef CONFIG_SUN3
751 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
752 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
753 instance->host_no);
754 BUG();
755 }
756
757 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
758 (BASR_PHASE_MATCH | BASR_ACK)) {
759 pr_err("scsi%d: BASR %02x\n", instance->host_no,
760 NCR5380_read(BUS_AND_STATUS_REG));
761 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
762 instance->host_no);
763 BUG();
764 }
765#endif
766
Finn Thain8053b0e2016-03-23 21:10:19 +1100767 NCR5380_write(MODE_REG, MR_BASE);
768 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
769 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
770
Finn Thain4a98f892016-10-10 00:46:53 -0400771 transferred = hostdata->dma_len - NCR5380_dma_residual(hostdata);
Finn Thain8053b0e2016-03-23 21:10:19 +1100772 hostdata->dma_len = 0;
773
774 data = (unsigned char **)&hostdata->connected->SCp.ptr;
775 count = &hostdata->connected->SCp.this_residual;
776 *data += transferred;
777 *count -= transferred;
778
779 if (hostdata->read_overruns) {
780 int cnt, toPIO;
781
782 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
783 cnt = toPIO = hostdata->read_overruns;
784 if (overrun) {
785 dsprintk(NDEBUG_DMA, instance,
786 "Got an input overrun, using saved byte\n");
787 *(*data)++ = saved_data;
788 (*count)--;
789 cnt--;
790 toPIO--;
791 }
792 if (toPIO > 0) {
793 dsprintk(NDEBUG_DMA, instance,
794 "Doing %d byte PIO to 0x%p\n", cnt, *data);
795 NCR5380_transfer_pio(instance, &p, &cnt, data);
796 *count -= toPIO - cnt;
797 }
798 }
799 }
800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/**
Finn Thaincd400822016-01-03 16:05:40 +1100803 * NCR5380_intr - generic NCR5380 irq handler
804 * @irq: interrupt number
805 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 *
Finn Thaincd400822016-01-03 16:05:40 +1100807 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
808 * from the disconnected queue, and restarting NCR5380_main()
809 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 *
Finn Thaincd400822016-01-03 16:05:40 +1100811 * The chip can assert IRQ in any of six different conditions. The IRQ flag
812 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
813 * Three of these six conditions are latched in the Bus and Status Register:
814 * - End of DMA (cleared by ending DMA Mode)
815 * - Parity error (cleared by reading RPIR)
816 * - Loss of BSY (cleared by reading RPIR)
817 * Two conditions have flag bits that are not latched:
818 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
819 * - Bus reset (non-maskable)
820 * The remaining condition has no flag bit at all:
821 * - Selection/reselection
822 *
823 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
824 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
825 * claimed that "the design of the [DP8490] interrupt logic ensures
826 * interrupts will not be lost (they can be on the DP5380)."
827 * The L5380/53C80 datasheet from LOGIC Devices has more details.
828 *
829 * Checking for bus reset by reading RST is futile because of interrupt
830 * latency, but a bus reset will reset chip logic. Checking for parity error
831 * is unnecessary because that interrupt is never enabled. A Loss of BSY
832 * condition will clear DMA Mode. We can tell when this occurs because the
833 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
835
Finn Thaina46865d2016-03-23 21:10:27 +1100836static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800838 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100839 struct NCR5380_hostdata *hostdata = shost_priv(instance);
840 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 unsigned char basr;
842 unsigned long flags;
843
Finn Thain11d2f632016-01-03 16:05:51 +1100844 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Finn Thaincd400822016-01-03 16:05:40 +1100846 basr = NCR5380_read(BUS_AND_STATUS_REG);
847 if (basr & BASR_IRQ) {
848 unsigned char mr = NCR5380_read(MODE_REG);
849 unsigned char sr = NCR5380_read(STATUS_REG);
850
Finn Thainb7465452016-01-03 16:06:05 +1100851 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
852 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100853
Finn Thain8053b0e2016-03-23 21:10:19 +1100854 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
855 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
856 * We ack IRQ after clearing Mode Register. Workarounds
857 * for End of DMA errata need to happen in DMA Mode.
858 */
859
860 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
861
862 if (hostdata->connected) {
863 NCR5380_dma_complete(instance);
864 queue_work(hostdata->work_q, &hostdata->main_task);
865 } else {
866 NCR5380_write(MODE_REG, MR_BASE);
867 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
868 }
869 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
Finn Thaincd400822016-01-03 16:05:40 +1100870 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
871 /* Probably reselected */
872 NCR5380_write(SELECT_ENABLE_REG, 0);
873 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
874
Finn Thainb7465452016-01-03 16:06:05 +1100875 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100876
877 if (!hostdata->connected) {
878 NCR5380_reselect(instance);
879 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Finn Thaincd400822016-01-03 16:05:40 +1100881 if (!hostdata->connected)
882 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
883 } else {
884 /* Probably Bus Reset */
885 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
886
Finn Thainb7465452016-01-03 16:06:05 +1100887 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
Finn Thaine9db3192016-03-23 21:10:21 +1100888#ifdef SUN3_SCSI_VME
889 dregs->csr |= CSR_DMA_ENABLE;
890#endif
Finn Thaincd400822016-01-03 16:05:40 +1100891 }
892 handled = 1;
893 } else {
Finn Thain9af9fec2016-10-10 00:46:53 -0400894 dsprintk(NDEBUG_INTR, instance, "interrupt without IRQ bit\n");
Finn Thaine9db3192016-03-23 21:10:21 +1100895#ifdef SUN3_SCSI_VME
896 dregs->csr |= CSR_DMA_ENABLE;
897#endif
Finn Thaincd400822016-01-03 16:05:40 +1100898 }
899
Finn Thain11d2f632016-01-03 16:05:51 +1100900 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100901
902 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
Finn Thainaff0cf92016-01-03 16:06:09 +1100905/*
Finn Thain710ddd02014-11-12 16:12:02 +1100906 * Function : int NCR5380_select(struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +1100907 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 *
909 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Finn Thain594d4ba2016-01-03 16:06:10 +1100910 * including ARBITRATION, SELECTION, and initial message out for
911 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100913 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain594d4ba2016-01-03 16:06:10 +1100914 * target lives, cmd - SCSI command to execute.
Finn Thainaff0cf92016-01-03 16:06:09 +1100915 *
Finn Thain707d62b2016-01-03 16:06:02 +1100916 * Returns cmd if selection failed but should be retried,
917 * NULL if selection failed and should not be retried, or
918 * NULL if selection succeeded (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100920 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +1100921 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
922 * with registers as they should have been on entry - ie
923 * SELECT_ENABLE will be set appropriately, the NCR5380
924 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100926 * If successful : I_T_L or I_T_L_Q nexus will be established,
927 * instance->connected will be set to cmd.
928 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100930 * If failed (no target) : cmd->scsi_done() will be called, and the
931 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100933
Finn Thain707d62b2016-01-03 16:06:02 +1100934static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
935 struct scsi_cmnd *cmd)
Finn Thain4ab2a782017-01-15 18:50:57 -0500936 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Finn Thaine8a60142016-01-03 16:05:54 +1100938 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 unsigned char tmp[3], phase;
940 unsigned char *data;
941 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +1100945 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
946 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Finn Thain707d62b2016-01-03 16:06:02 +1100948 /*
949 * Arbitration and selection phases are slow and involve dropping the
950 * lock, so we have to watch out for EH. An exception handler may
951 * change 'selecting' to NULL. This function will then return NULL
952 * so that the caller will forget about 'cmd'. (During information
953 * transfer phases, EH may change 'connected' to NULL.)
954 */
955 hostdata->selecting = cmd;
956
Finn Thainaff0cf92016-01-03 16:06:09 +1100957 /*
958 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * data bus during SELECTION.
960 */
961
962 NCR5380_write(TARGET_COMMAND_REG, 0);
963
Finn Thainaff0cf92016-01-03 16:06:09 +1100964 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * Start arbitration.
966 */
967
968 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
969 NCR5380_write(MODE_REG, MR_ARBITRATE);
970
Finn Thain55500d92016-01-03 16:05:35 +1100971 /* The chip now waits for BUS FREE phase. Then after the 800 ns
972 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 */
974
Finn Thain11d2f632016-01-03 16:05:51 +1100975 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -0400976 err = NCR5380_poll_politely2(hostdata, MODE_REG, MR_ARBITRATE, 0,
Finn Thainb32ade12016-01-03 16:05:41 +1100977 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
978 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +1100979 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +1100980 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
981 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +1100982 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +1100983 }
Finn Thainccf6efd2016-02-23 10:07:08 +1100984 if (!hostdata->selecting) {
985 /* Command was aborted */
986 NCR5380_write(MODE_REG, MR_BASE);
987 goto out;
988 }
Finn Thainb32ade12016-01-03 16:05:41 +1100989 if (err < 0) {
990 NCR5380_write(MODE_REG, MR_BASE);
991 shost_printk(KERN_ERR, instance,
992 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +1100993 goto out;
Finn Thain55500d92016-01-03 16:05:35 +1100994 }
Finn Thain11d2f632016-01-03 16:05:51 +1100995 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +1100996
997 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 udelay(3);
999
1000 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +11001001 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1002 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1003 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001005 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001006 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
Finn Thaincf13b082016-01-03 16:05:18 +11001009
1010 /* After/during arbitration, BSY should be asserted.
1011 * IBM DPES-31080 Version S31Q works now
1012 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1013 */
1014 NCR5380_write(INITIATOR_COMMAND_REG,
1015 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Finn Thainaff0cf92016-01-03 16:06:09 +11001017 /*
1018 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 * a minimum so we'll udelay ceil(1.2)
1020 */
1021
Finn Thain9c3f0e22016-01-03 16:05:11 +11001022 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1023 udelay(15);
1024 else
1025 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Finn Thain11d2f632016-01-03 16:05:51 +11001027 spin_lock_irq(&hostdata->lock);
1028
Finn Thain72064a72016-01-03 16:05:44 +11001029 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1030 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001031 goto out;
1032
1033 if (!hostdata->selecting) {
1034 NCR5380_write(MODE_REG, MR_BASE);
1035 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1036 goto out;
1037 }
Finn Thain72064a72016-01-03 16:05:44 +11001038
Finn Thainb7465452016-01-03 16:06:05 +11001039 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Finn Thainaff0cf92016-01-03 16:06:09 +11001041 /*
1042 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * the host and target ID's on the SCSI bus.
1044 */
1045
Finn Thain3d07d222016-01-03 16:06:13 +11001046 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Finn Thainaff0cf92016-01-03 16:06:09 +11001048 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 * Raise ATN while SEL is true before BSY goes false from arbitration,
1050 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1051 * phase immediately after selection.
1052 */
1053
Finn Thain3d07d222016-01-03 16:06:13 +11001054 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1055 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 NCR5380_write(MODE_REG, MR_BASE);
1057
Finn Thainaff0cf92016-01-03 16:06:09 +11001058 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 * Reselect interrupts must be turned off prior to the dropping of BSY,
1060 * otherwise we will trigger an interrupt.
1061 */
1062 NCR5380_write(SELECT_ENABLE_REG, 0);
1063
Finn Thain11d2f632016-01-03 16:05:51 +11001064 spin_unlock_irq(&hostdata->lock);
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001067 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 * the BSY signal.
1069 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001070 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001073 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1074 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Finn Thainaff0cf92016-01-03 16:06:09 +11001076 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001078 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 * appropriate propagation delay has expired, and we're confusing
1080 * a BSY signal from ourselves as the target's response to SELECTION.
1081 *
1082 * A small delay (the 'C++' frontend breaks the pipeline with an
1083 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001084 * tighter 'C' code breaks and requires this) solves the problem -
1085 * the 1 us delay is arbitrary, and only used because this delay will
1086 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 * work there.
1088 *
1089 * wingel suggests that this could be due to failing to wait
1090 * one deskew delay.
1091 */
1092
1093 udelay(1);
1094
Finn Thainb7465452016-01-03 16:06:05 +11001095 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Finn Thainaff0cf92016-01-03 16:06:09 +11001097 /*
1098 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 * selection.
1100 */
1101
Finn Thaind5d37a02016-10-10 00:46:53 -04001102 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_BSY, SR_BSY,
Finn Thainae753a32016-01-03 16:05:23 +11001103 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001106 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1108 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001109 if (!hostdata->connected)
1110 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001111 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
Finn Thainae753a32016-01-03 16:05:23 +11001114
1115 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001116 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001117 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thainae753a32016-01-03 16:05:23 +11001118 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001119 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1120 if (hostdata->selecting) {
1121 cmd->result = DID_BAD_TARGET << 16;
1122 complete_cmd(instance, cmd);
1123 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1124 cmd = NULL;
1125 }
1126 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001127 }
1128
Finn Thainaff0cf92016-01-03 16:06:09 +11001129 /*
1130 * No less than two deskew delays after the initiator detects the
1131 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * change the DATA BUS. -wingel
1133 */
1134
1135 udelay(1);
1136
1137 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001140 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * was true but before BSY was false during selection, the information
1142 * transfer phase should be a MESSAGE OUT phase so that we can send the
1143 * IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 */
1145
1146 /* Wait for start of REQ/ACK handshake */
1147
Finn Thaind5d37a02016-10-10 00:46:53 -04001148 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001149 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001150 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001151 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1152 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001154 goto out;
1155 }
1156 if (!hostdata->selecting) {
1157 do_abort(instance);
1158 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160
Finn Thainb7465452016-01-03 16:06:05 +11001161 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1162 scmd_id(cmd));
Finn Thain22f5f102014-11-12 16:11:56 +11001163 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 len = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 data = tmp;
1167 phase = PHASE_MSGOUT;
1168 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb15e7912017-01-15 18:50:57 -05001169 if (len) {
1170 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1171 cmd->result = DID_ERROR << 16;
1172 complete_cmd(instance, cmd);
1173 dsprintk(NDEBUG_SELECTION, instance, "IDENTIFY message transfer failed\n");
1174 cmd = NULL;
1175 goto out;
1176 }
1177
Finn Thainb7465452016-01-03 16:06:05 +11001178 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001181 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Finn Thaine9db3192016-03-23 21:10:21 +11001183#ifdef SUN3_SCSI_VME
1184 dregs->csr |= CSR_INTR;
1185#endif
1186
Boaz Harrosh28424d32007-09-10 22:37:45 +03001187 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Finn Thain707d62b2016-01-03 16:06:02 +11001189 cmd = NULL;
1190
1191out:
1192 if (!hostdata->selecting)
1193 return NULL;
1194 hostdata->selecting = NULL;
1195 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Finn Thainaff0cf92016-01-03 16:06:09 +11001198/*
1199 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001200 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 *
1202 * Purpose : transfers data in given phase using polled I/O
1203 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001204 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001205 * what phase is expected, *count - pointer to number of
1206 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001207 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001209 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001210 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001212 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 *
1214 * XXX Note : handling for bus free may be useful.
1215 */
1216
1217/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001218 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 * IS 100% reliable, and for the actual data transfer where speed
1220 * counts, we will always do a pseudo DMA or DMA transfer.
1221 */
1222
Finn Thain0d2cf862016-01-03 16:06:11 +11001223static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1224 unsigned char *phase, int *count,
1225 unsigned char **data)
1226{
Finn Thain61e1ce52016-10-10 00:46:53 -04001227 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 unsigned char p = *phase, tmp;
1229 int c = *count;
1230 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Finn Thainaff0cf92016-01-03 16:06:09 +11001232 /*
1233 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 * phase specified in the appropriate bits of the TARGET COMMAND
1235 * REGISTER match the STATUS REGISTER
1236 */
1237
Finn Thain0d2cf862016-01-03 16:06:11 +11001238 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001241 /*
1242 * Wait for assertion of REQ, after which the phase bits will be
1243 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 */
1245
Finn Thaind5d37a02016-10-10 00:46:53 -04001246 if (NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Finn Thainb7465452016-01-03 16:06:05 +11001249 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001252 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001253 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1254 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 break;
1256 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* Do actual transfer from SCSI bus to / from memory */
1259 if (!(p & SR_IO))
1260 NCR5380_write(OUTPUT_DATA_REG, *d);
1261 else
1262 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1263
1264 ++d;
1265
Finn Thainaff0cf92016-01-03 16:06:09 +11001266 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 * The SCSI standard suggests that in MSGOUT phase, the initiator
1268 * should drop ATN on the last byte of the message phase
1269 * after REQ has been asserted for the handshake but before
1270 * the initiator raises ACK.
1271 */
1272
1273 if (!(p & SR_IO)) {
1274 if (!((p & SR_MSG) && c > 1)) {
1275 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1276 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001277 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1278 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001280 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1281 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001283 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1284 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 } else {
1287 NCR5380_dprint(NDEBUG_PIO, instance);
1288 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1289 }
1290
Finn Thaind5d37a02016-10-10 00:46:53 -04001291 if (NCR5380_poll_politely(hostdata,
Finn Thaina2edc4a2016-01-03 16:05:27 +11001292 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1293 break;
1294
Finn Thainb7465452016-01-03 16:06:05 +11001295 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001298 * We have several special cases to consider during REQ/ACK handshaking :
1299 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001300 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001302 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001303 * message. We must exit with ACK asserted, so that the calling
1304 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 *
1306 * 3. ACK and ATN are clear and the target may proceed as normal.
1307 */
1308 if (!(p == PHASE_MSGIN && c == 1)) {
1309 if (p == PHASE_MSGOUT && c > 1)
1310 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1311 else
1312 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1313 }
1314 } while (--c);
1315
Finn Thainb7465452016-01-03 16:06:05 +11001316 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 *count = c;
1319 *data = d;
1320 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001321 /* The phase read from the bus is valid if either REQ is (already)
1322 * asserted or if ACK hasn't been released yet. The latter applies if
1323 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1324 */
1325 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 *phase = tmp & PHASE_MASK;
1327 else
1328 *phase = PHASE_UNKNOWN;
1329
1330 if (!c || (*phase == p))
1331 return 0;
1332 else
1333 return -1;
1334}
1335
1336/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001337 * do_reset - issue a reset command
1338 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001340 * Issue a reset sequence to the NCR5380 and try and get the bus
1341 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001343 * This clears the reset interrupt flag because there may be no handler for
1344 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1345 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001347
Finn Thain54d8fe42016-01-03 16:05:06 +11001348static void do_reset(struct Scsi_Host *instance)
1349{
Finn Thain61e1ce52016-10-10 00:46:53 -04001350 struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +11001351 unsigned long flags;
1352
1353 local_irq_save(flags);
1354 NCR5380_write(TARGET_COMMAND_REG,
1355 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001357 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001359 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1360 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Finn Thain80d3eb62016-01-03 16:05:34 +11001363/**
1364 * do_abort - abort the currently established nexus by going to
1365 * MESSAGE OUT phase and sending an ABORT message.
1366 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001368 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
1370
Finn Thain54d8fe42016-01-03 16:05:06 +11001371static int do_abort(struct Scsi_Host *instance)
1372{
Finn Thain61e1ce52016-10-10 00:46:53 -04001373 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 unsigned char *msgptr, phase, tmp;
1375 int len;
1376 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 /* Request message out phase */
1379 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1380
Finn Thainaff0cf92016-01-03 16:06:09 +11001381 /*
1382 * Wait for the target to indicate a valid phase by asserting
1383 * REQ. Once this happens, we'll have either a MSGOUT phase
1384 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001386 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * We really don't care what value was on the bus or what value
1388 * the target sees, so we just handshake.
1389 */
1390
Finn Thaind5d37a02016-10-10 00:46:53 -04001391 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001392 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001393 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Finn Thainf35d3472016-01-03 16:05:33 +11001395 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1398
Finn Thainf35d3472016-01-03 16:05:33 +11001399 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001400 NCR5380_write(INITIATOR_COMMAND_REG,
1401 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thaind5d37a02016-10-10 00:46:53 -04001402 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001403 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001404 goto timeout;
1405 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 tmp = ABORT;
1409 msgptr = &tmp;
1410 len = 1;
1411 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001412 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 /*
1415 * If we got here, and the command completed successfully,
1416 * we're about to go into bus free state.
1417 */
1418
1419 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001420
1421timeout:
1422 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1423 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Finn Thainaff0cf92016-01-03 16:06:09 +11001426/*
1427 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001428 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 *
1430 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001431 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001433 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001434 * what phase is expected, *count - pointer to number of
1435 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001436 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001438 * maximum number of bytes, 0 if all bytes or transferred or exit
1439 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001441 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
1443
1444
Finn Thain0d2cf862016-01-03 16:06:11 +11001445static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1446 unsigned char *phase, int *count,
1447 unsigned char **data)
1448{
1449 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainf0ea73a2016-03-23 21:10:26 +11001450 int c = *count;
1451 unsigned char p = *phase;
1452 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 unsigned char tmp;
Finn Thain8053b0e2016-03-23 21:10:19 +11001454 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1457 *phase = tmp;
1458 return -1;
1459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Finn Thain8053b0e2016-03-23 21:10:19 +11001461 hostdata->connected->SCp.phase = p;
1462
1463 if (p & SR_IO) {
1464 if (hostdata->read_overruns)
1465 c -= hostdata->read_overruns;
1466 else if (hostdata->flags & FLAG_DMA_FIXUP)
1467 --c;
1468 }
1469
1470 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1471 (p & SR_IO) ? "receive" : "send", c, d);
1472
Finn Thaine9db3192016-03-23 21:10:21 +11001473#ifdef CONFIG_SUN3
1474 /* send start chain */
1475 sun3scsi_dma_start(c, *data);
1476#endif
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thain8053b0e2016-03-23 21:10:19 +11001479 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1480 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Finn Thain8053b0e2016-03-23 21:10:19 +11001482 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1483 /* On the Medusa, it is a must to initialize the DMA before
1484 * starting the NCR. This is also the cleaner way for the TT.
1485 */
1486 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001487 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001488 else
Finn Thain4a98f892016-10-10 00:46:53 -04001489 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Finn Thainaff0cf92016-01-03 16:06:09 +11001492 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001493 * On the PAS16 at least I/O recovery delays are not needed here.
1494 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 */
1496
1497 if (p & SR_IO) {
Finn Thaine9db3192016-03-23 21:10:21 +11001498 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaine5d55d12016-03-23 21:10:16 +11001499 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1501 } else {
Finn Thaine5d55d12016-03-23 21:10:16 +11001502 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thaine5d55d12016-03-23 21:10:16 +11001504 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 NCR5380_write(START_DMA_SEND_REG, 0);
Finn Thaine5d55d12016-03-23 21:10:16 +11001506 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508
Finn Thaine9db3192016-03-23 21:10:21 +11001509#ifdef CONFIG_SUN3
1510#ifdef SUN3_SCSI_VME
1511 dregs->csr |= CSR_DMA_ENABLE;
1512#endif
1513 sun3_dma_active = 1;
1514#endif
1515
Finn Thain8053b0e2016-03-23 21:10:19 +11001516 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1517 /* On the Falcon, the DMA setup must be done after the last
1518 * NCR access, else the DMA setup gets trashed!
1519 */
1520 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001521 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001522 else
Finn Thain4a98f892016-10-10 00:46:53 -04001523 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001524 }
1525
1526 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1527 if (result < 0)
1528 return result;
1529
1530 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1531 if (result > 0) {
1532 hostdata->dma_len = result;
1533 return 0;
1534 }
1535
1536 /* The result is zero iff pseudo DMA send/receive was completed. */
1537 hostdata->dma_len = c;
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539/*
Finn Thaine4dec682016-03-23 21:10:12 +11001540 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001541 *
1542 * For DMA sends, we want to wait until the last byte has been
1543 * transferred out over the bus before we turn off DMA mode. Alas, there
1544 * seems to be no terribly good way of doing this on a 5380 under all
1545 * conditions. For non-scatter-gather operations, we can wait until REQ
1546 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1547 * are nastier, since the device will be expecting more data than we
1548 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1549 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1550 * why this signal was added to the newer chips) but on the older 538[01]
1551 * this signal does not exist. The workaround for this lack is a watchdog;
1552 * we bail out of the wait-loop after a modest amount of wait-time if
1553 * the usual exit conditions are not met. Not a terribly clean or
1554 * correct solution :-%
1555 *
1556 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1557 * If the chip is in DMA receive mode, it will respond to a target's
1558 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1559 * ACK, even if it has _already_ been notified by the DMA controller that
1560 * the current DMA transfer has completed! If the NCR5380 is then taken
1561 * out of DMA mode, this already-acknowledged byte is lost. This is
1562 * not a problem for "one DMA transfer per READ command", because
1563 * the situation will never arise... either all of the data is DMA'ed
1564 * properly, or the target switches to MESSAGE IN phase to signal a
1565 * disconnection (either operation bringing the DMA to a clean halt).
1566 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001567 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001568 * condition before taking the NCR5380 out of DMA mode. One or two extra
1569 * bytes are transferred via PIO as necessary to fill out the original
1570 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 */
1572
Finn Thain8053b0e2016-03-23 21:10:19 +11001573 if (hostdata->flags & FLAG_DMA_FIXUP) {
1574 if (p & SR_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001576 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001577 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 * the chip to latch the last byte, read it, and then disable
1579 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001580 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1582 * REQ is deasserted when ACK is asserted, and not reasserted
1583 * until ACK goes false. Since the NCR5380 won't lower ACK
1584 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001585 * the DMA port or we take the NCR5380 out of DMA mode, we
1586 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 * byte.
1588 */
1589
Finn Thaind5d37a02016-10-10 00:46:53 -04001590 if (NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001591 BASR_DRQ, BASR_DRQ, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001592 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001593 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Finn Thaind5d37a02016-10-10 00:46:53 -04001595 if (NCR5380_poll_politely(hostdata, STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001596 SR_REQ, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001597 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001598 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1599 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001600 d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
1601 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001603 * Wait for the last byte to be sent. If REQ is being asserted for
1604 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
Finn Thaind5d37a02016-10-10 00:46:53 -04001606 if (NCR5380_poll_politely2(hostdata,
Finn Thain55181be2016-01-03 16:05:42 +11001607 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1608 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001609 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001610 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001614
1615 NCR5380_dma_complete(instance);
Finn Thain438af512016-03-23 21:10:18 +11001616 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619/*
1620 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1621 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001622 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001623 * directs us to. Operates on the currently connected command,
1624 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 *
1626 * Inputs : instance, instance for which we are doing commands
1627 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001628 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001629 * modified if a command disconnects, *instance->connected will
1630 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001632 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001633 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 */
1635
Finn Thain0d2cf862016-01-03 16:06:11 +11001636static void NCR5380_information_transfer(struct Scsi_Host *instance)
Finn Thain4ab2a782017-01-15 18:50:57 -05001637 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Finn Thain0d2cf862016-01-03 16:06:11 +11001638{
Finn Thaine8a60142016-01-03 16:05:54 +11001639 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 unsigned char msgout = NOP;
1641 int sink = 0;
1642 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 unsigned char *data;
1645 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001646 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Finn Thaine9db3192016-03-23 21:10:21 +11001648#ifdef SUN3_SCSI_VME
1649 dregs->csr |= CSR_INTR;
1650#endif
1651
Finn Thain11d2f632016-01-03 16:05:51 +11001652 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001653 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 tmp = NCR5380_read(STATUS_REG);
1656 /* We only have a valid SCSI phase when REQ is asserted */
1657 if (tmp & SR_REQ) {
1658 phase = (tmp & PHASE_MASK);
1659 if (phase != old_phase) {
1660 old_phase = phase;
1661 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1662 }
Finn Thaine9db3192016-03-23 21:10:21 +11001663#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04001664 if (phase == PHASE_CMDOUT &&
1665 sun3_dma_setup_done != cmd) {
1666 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11001667
1668 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
Finn Thain4a98f892016-10-10 00:46:53 -04001669 ++cmd->SCp.buffer;
1670 --cmd->SCp.buffers_residual;
1671 cmd->SCp.this_residual = cmd->SCp.buffer->length;
1672 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thaine9db3192016-03-23 21:10:21 +11001673 }
1674
Finn Thain4a98f892016-10-10 00:46:53 -04001675 count = sun3scsi_dma_xfer_len(hostdata, cmd);
1676
1677 if (count > 0) {
1678 if (rq_data_dir(cmd->request))
1679 sun3scsi_dma_send_setup(hostdata,
1680 cmd->SCp.ptr, count);
1681 else
1682 sun3scsi_dma_recv_setup(hostdata,
1683 cmd->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11001684 sun3_dma_setup_done = cmd;
1685 }
1686#ifdef SUN3_SCSI_VME
1687 dregs->csr |= CSR_INTR;
1688#endif
1689 }
1690#endif /* CONFIG_SUN3 */
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (sink && (phase != PHASE_MSGOUT)) {
1693 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1694
Finn Thain3d07d222016-01-03 16:06:13 +11001695 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1696 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001697 while (NCR5380_read(STATUS_REG) & SR_REQ)
1698 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001699 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1700 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 sink = 0;
1702 continue;
1703 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 case PHASE_DATAOUT:
1707#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001708 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 sink = 1;
1710 do_abort(instance);
1711 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001712 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001713 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return;
1715#endif
Finn Thainbf1a0c62016-01-03 16:05:47 +11001716 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001717 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 * If there is no room left in the current buffer in the
1719 * scatter-gather list, move onto the next one.
1720 */
1721
1722 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1723 ++cmd->SCp.buffer;
1724 --cmd->SCp.buffers_residual;
1725 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001726 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thainb7465452016-01-03 16:06:05 +11001727 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1728 cmd->SCp.this_residual,
1729 cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001733 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * PSEUDO-DMA for systems that are strictly PIO,
1735 * since we can let the hardware do the handshaking.
1736 *
1737 * For this to work, we need to know the transfersize
1738 * ahead of time, since the pseudo-DMA code will sit
1739 * in an unconditional loop.
1740 */
1741
Finn Thainff3d4572016-01-03 16:05:25 +11001742 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001743 if (!cmd->device->borken)
Finn Thain4a98f892016-10-10 00:46:53 -04001744 transfersize = NCR5380_dma_xfer_len(hostdata, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Finn Thain438af512016-03-23 21:10:18 +11001746 if (transfersize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001748 if (NCR5380_transfer_dma(instance, &phase,
1749 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001751 * If the watchdog timer fires, all future
1752 * accesses to this device will use the
1753 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001755 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001756 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 sink = 1;
1759 do_abort(instance);
1760 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /* XXX - need to source or sink data here, as appropriate */
Finn Thain8053b0e2016-03-23 21:10:19 +11001762 }
Finn Thainf825e402016-03-23 21:10:15 +11001763 } else {
Finn Thain08348b12016-08-31 14:44:56 +10001764 /* Transfer a small chunk so that the
1765 * irq mode lock is not held too long.
Finn Thain16788472016-02-23 10:07:05 +11001766 */
Finn Thain08348b12016-08-31 14:44:56 +10001767 transfersize = min(cmd->SCp.this_residual,
1768 NCR5380_PIO_CHUNK_SIZE);
Finn Thain16788472016-02-23 10:07:05 +11001769 len = transfersize;
1770 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001771 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001772 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001773 }
Finn Thaine9db3192016-03-23 21:10:21 +11001774#ifdef CONFIG_SUN3
1775 if (sun3_dma_setup_done == cmd)
1776 sun3_dma_setup_done = NULL;
1777#endif
Finn Thain16788472016-02-23 10:07:05 +11001778 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 case PHASE_MSGIN:
1780 len = 1;
1781 data = &tmp;
1782 NCR5380_transfer_pio(instance, &phase, &len, &data);
1783 cmd->SCp.Message = tmp;
1784
1785 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 case ABORT:
1787 case COMMAND_COMPLETE:
1788 /* Accept message by clearing ACK */
1789 sink = 1;
1790 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001791 dsprintk(NDEBUG_QUEUES, instance,
1792 "COMMAND COMPLETE %p target %d lun %llu\n",
1793 cmd, scmd_id(cmd), cmd->device->lun);
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Finn Thainf27db8e2016-01-03 16:06:00 +11001797 cmd->result &= ~0xffff;
1798 cmd->result |= cmd->SCp.Status;
1799 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Finn Thainf27db8e2016-01-03 16:06:00 +11001801 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001802 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001803 else {
1804 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1805 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1806 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1807 cmd);
1808 list_add_tail(&ncmd->list,
1809 &hostdata->autosense);
1810 } else
1811 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813
Finn Thainaff0cf92016-01-03 16:06:09 +11001814 /*
1815 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 * arbitration can resume.
1817 */
1818 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001819
1820 /* Enable reselect interrupts */
1821 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain52d3e562016-03-23 21:10:20 +11001822
1823 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return;
1825 case MESSAGE_REJECT:
1826 /* Accept message by clearing ACK */
1827 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1828 switch (hostdata->last_message) {
1829 case HEAD_OF_QUEUE_TAG:
1830 case ORDERED_QUEUE_TAG:
1831 case SIMPLE_QUEUE_TAG:
1832 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001833 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 break;
1835 default:
1836 break;
1837 }
Finn Thain340b9612016-01-03 16:05:31 +11001838 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001839 case DISCONNECT:
1840 /* Accept message by clearing ACK */
1841 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1842 hostdata->connected = NULL;
1843 list_add(&ncmd->list, &hostdata->disconnected);
1844 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1845 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1846 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001847
Finn Thain0d2cf862016-01-03 16:06:11 +11001848 /*
1849 * Restore phase bits to 0 so an interrupted selection,
1850 * arbitration can resume.
1851 */
1852 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Finn Thain0d2cf862016-01-03 16:06:11 +11001854 /* Enable reselect interrupts */
1855 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine9db3192016-03-23 21:10:21 +11001856#ifdef SUN3_SCSI_VME
1857 dregs->csr |= CSR_DMA_ENABLE;
1858#endif
Finn Thain0d2cf862016-01-03 16:06:11 +11001859 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001860 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001862 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 * ignore SAVE/RESTORE pointers calls.
1864 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001865 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001867 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 * compatible.
1869 */
1870 case SAVE_POINTERS:
1871 case RESTORE_POINTERS:
1872 /* Accept message by clearing ACK */
1873 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1874 break;
1875 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001876 /*
1877 * Start the message buffer with the EXTENDED_MESSAGE
1878 * byte, since spi_print_msg() wants the whole thing.
1879 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 extended_msg[0] = EXTENDED_MESSAGE;
1881 /* Accept first byte by clearing ACK */
1882 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001883
1884 spin_unlock_irq(&hostdata->lock);
1885
Finn Thainb7465452016-01-03 16:06:05 +11001886 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 len = 2;
1889 data = extended_msg + 1;
1890 phase = PHASE_MSGIN;
1891 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001892 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1893 (int)extended_msg[1],
1894 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Finn Thaine0783ed2016-01-03 16:05:45 +11001896 if (!len && extended_msg[1] > 0 &&
1897 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 /* Accept third byte by clearing ACK */
1899 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1900 len = extended_msg[1] - 1;
1901 data = extended_msg + 3;
1902 phase = PHASE_MSGIN;
1903
1904 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001905 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1906 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 switch (extended_msg[2]) {
1909 case EXTENDED_SDTR:
1910 case EXTENDED_WDTR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 tmp = 0;
1912 }
1913 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001914 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 tmp = 0;
1916 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001917 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1918 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 tmp = 0;
1920 }
Finn Thain11d2f632016-01-03 16:05:51 +11001921
1922 spin_lock_irq(&hostdata->lock);
1923 if (!hostdata->connected)
1924 return;
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /* Fall through to reject message */
1927
Finn Thainaff0cf92016-01-03 16:06:09 +11001928 /*
1929 * If we get something weird that we aren't expecting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 * reject it.
1931 */
1932 default:
Finn Thain39bef872017-10-26 16:51:50 +11001933 if (tmp == EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04001934 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001935 "rejecting unknown extended message code %02x, length %d\n",
Finn Thain39bef872017-10-26 16:51:50 +11001936 extended_msg[2], extended_msg[1]);
1937 else if (tmp)
1938 scmd_printk(KERN_INFO, cmd,
1939 "rejecting unknown message code %02x\n",
1940 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 msgout = MESSAGE_REJECT;
1943 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1944 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001945 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 break;
1947 case PHASE_MSGOUT:
1948 len = 1;
1949 data = &msgout;
1950 hostdata->last_message = msgout;
1951 NCR5380_transfer_pio(instance, &phase, &len, &data);
1952 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 hostdata->connected = NULL;
1954 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001955 complete_cmd(instance, cmd);
Finn Thain52d3e562016-03-23 21:10:20 +11001956 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1958 return;
1959 }
1960 msgout = NOP;
1961 break;
1962 case PHASE_CMDOUT:
1963 len = cmd->cmd_len;
1964 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11001965 /*
1966 * XXX for performance reasons, on machines with a
1967 * PSEUDO-DMA architecture we should probably
1968 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 */
1970 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 break;
1972 case PHASE_STATIN:
1973 len = 1;
1974 data = &tmp;
1975 NCR5380_transfer_pio(instance, &phase, &len, &data);
1976 cmd->SCp.Status = tmp;
1977 break;
1978 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001979 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11001980 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11001981 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11001982 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11001983 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -04001984 NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001985 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
Finn Thain11d2f632016-01-03 16:05:51 +11001987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
1990/*
1991 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
1992 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001993 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11001994 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
1995 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11001996 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 */
1999
Finn Thain0d2cf862016-01-03 16:06:11 +11002000static void NCR5380_reselect(struct Scsi_Host *instance)
2001{
Finn Thaine8a60142016-01-03 16:05:54 +11002002 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 unsigned char target_mask;
Finn Thaine9db3192016-03-23 21:10:21 +11002004 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 unsigned char msg[3];
Finn Thain32b26a12016-01-03 16:05:58 +11002006 struct NCR5380_cmd *ncmd;
2007 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 /*
2010 * Disable arbitration, etc. since the host adapter obviously
2011 * lost, and tell an interrupted NCR5380_select() to restart.
2012 */
2013
2014 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thainb7465452016-01-03 16:06:05 +11002017
2018 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Finn Thainaff0cf92016-01-03 16:06:09 +11002020 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 * At this point, we have detected that our SCSI ID is on the bus,
2022 * SEL is true and BSY was false for at least one bus settle delay
2023 * (400 ns).
2024 *
2025 * We must assert BSY ourselves, until the target drops the SEL
2026 * signal.
2027 */
2028
2029 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thaind5d37a02016-10-10 00:46:53 -04002030 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002031 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2032 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2033 return;
2034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2036
2037 /*
2038 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 */
2040
Finn Thaind5d37a02016-10-10 00:46:53 -04002041 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002042 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2043 do_abort(instance);
2044 return;
2045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Finn Thaine9db3192016-03-23 21:10:21 +11002047#ifdef CONFIG_SUN3
2048 /* acknowledge toggle to MSGIN */
2049 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Finn Thaine9db3192016-03-23 21:10:21 +11002051 /* peek at the byte without really hitting the bus */
2052 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2053#else
2054 {
2055 int len = 1;
2056 unsigned char *data = msg;
2057 unsigned char phase = PHASE_MSGIN;
2058
2059 NCR5380_transfer_pio(instance, &phase, &len, &data);
2060
2061 if (len) {
2062 do_abort(instance);
2063 return;
2064 }
Finn Thain72064a72016-01-03 16:05:44 +11002065 }
Finn Thaine9db3192016-03-23 21:10:21 +11002066#endif /* CONFIG_SUN3 */
Finn Thain72064a72016-01-03 16:05:44 +11002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002069 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002070 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002071 printk("\n");
2072 do_abort(instance);
2073 return;
2074 }
2075 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Finn Thain72064a72016-01-03 16:05:44 +11002077 /*
2078 * We need to add code for SCSI-II to track which devices have
2079 * I_T_L_Q nexuses established, and which have simple I_T_L
2080 * nexuses so we can chose to do additional data transfer.
2081 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Finn Thain72064a72016-01-03 16:05:44 +11002083 /*
2084 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2085 * just reestablished, and remove it from the disconnected queue.
2086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Finn Thain32b26a12016-01-03 16:05:58 +11002088 tmp = NULL;
2089 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2090 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2091
2092 if (target_mask == (1 << scmd_id(cmd)) &&
2093 lun == (u8)cmd->device->lun) {
2094 list_del(&ncmd->list);
2095 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002099
2100 if (tmp) {
2101 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2102 "reselect: removed %p from disconnected queue\n", tmp);
2103 } else {
Finn Thain72064a72016-01-03 16:05:44 +11002104 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2105 target_mask, lun);
2106 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002107 * Since we have an established nexus that we can't do anything
2108 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Finn Thain72064a72016-01-03 16:05:44 +11002113
Finn Thaine9db3192016-03-23 21:10:21 +11002114#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04002115 if (sun3_dma_setup_done != tmp) {
2116 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11002117
2118 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
Finn Thain4a98f892016-10-10 00:46:53 -04002119 ++tmp->SCp.buffer;
2120 --tmp->SCp.buffers_residual;
2121 tmp->SCp.this_residual = tmp->SCp.buffer->length;
2122 tmp->SCp.ptr = sg_virt(tmp->SCp.buffer);
Finn Thaine9db3192016-03-23 21:10:21 +11002123 }
2124
Finn Thain4a98f892016-10-10 00:46:53 -04002125 count = sun3scsi_dma_xfer_len(hostdata, tmp);
2126
2127 if (count > 0) {
2128 if (rq_data_dir(tmp->request))
2129 sun3scsi_dma_send_setup(hostdata,
2130 tmp->SCp.ptr, count);
2131 else
2132 sun3scsi_dma_recv_setup(hostdata,
2133 tmp->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11002134 sun3_dma_setup_done = tmp;
2135 }
2136 }
2137
2138 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2139#endif /* CONFIG_SUN3 */
2140
Finn Thain72064a72016-01-03 16:05:44 +11002141 /* Accept message by clearing ACK */
2142 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2143
2144 hostdata->connected = tmp;
Finn Thainc4ec6f92016-03-23 21:10:22 +11002145 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2146 scmd_id(tmp), tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Finn Thain8b00c3d2016-01-03 16:06:01 +11002149/**
2150 * list_find_cmd - test for presence of a command in a linked list
2151 * @haystack: list of commands
2152 * @needle: command to search for
2153 */
2154
2155static bool list_find_cmd(struct list_head *haystack,
2156 struct scsi_cmnd *needle)
2157{
2158 struct NCR5380_cmd *ncmd;
2159
2160 list_for_each_entry(ncmd, haystack, list)
2161 if (NCR5380_to_scmd(ncmd) == needle)
2162 return true;
2163 return false;
2164}
2165
2166/**
2167 * list_remove_cmd - remove a command from linked list
2168 * @haystack: list of commands
2169 * @needle: command to remove
2170 */
2171
2172static bool list_del_cmd(struct list_head *haystack,
2173 struct scsi_cmnd *needle)
2174{
2175 if (list_find_cmd(haystack, needle)) {
2176 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2177
2178 list_del(&ncmd->list);
2179 return true;
2180 }
2181 return false;
2182}
2183
2184/**
2185 * NCR5380_abort - scsi host eh_abort_handler() method
2186 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002188 * Try to abort a given command by removing it from queues and/or sending
2189 * the target an abort message. This may not succeed in causing a target
2190 * to abort the command. Nonetheless, the low-level driver must forget about
2191 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002193 * The normal path taken by a command is as follows. For EH we trace this
2194 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002196 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2197 * [disconnected -> connected ->]...
2198 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002200 * If cmd was not found at all then presumably it has already been completed,
2201 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002202 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002203 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002204 * We have no option but to forget the aborted command (even if it still
2205 * lacks sense data). The mid-layer may re-issue a command that is in error
2206 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2207 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002208 *
2209 * The lock protects driver data structures, but EH handlers also use it
2210 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 */
2212
Finn Thain710ddd02014-11-12 16:12:02 +11002213static int NCR5380_abort(struct scsi_cmnd *cmd)
2214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002216 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002217 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002218 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002219
Finn Thain11d2f632016-01-03 16:05:51 +11002220 spin_lock_irqsave(&hostdata->lock, flags);
2221
Finn Thain32b26a12016-01-03 16:05:58 +11002222#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002223 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002224#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002225 NCR5380_dprint(NDEBUG_ANY, instance);
2226 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Finn Thain8b00c3d2016-01-03 16:06:01 +11002228 if (list_del_cmd(&hostdata->unissued, cmd)) {
2229 dsprintk(NDEBUG_ABORT, instance,
2230 "abort: removed %p from issue queue\n", cmd);
2231 cmd->result = DID_ABORT << 16;
2232 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002233 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002234 }
2235
Finn Thain707d62b2016-01-03 16:06:02 +11002236 if (hostdata->selecting == cmd) {
2237 dsprintk(NDEBUG_ABORT, instance,
2238 "abort: cmd %p == selecting\n", cmd);
2239 hostdata->selecting = NULL;
2240 cmd->result = DID_ABORT << 16;
2241 complete_cmd(instance, cmd);
2242 goto out;
2243 }
2244
Finn Thain8b00c3d2016-01-03 16:06:01 +11002245 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2246 dsprintk(NDEBUG_ABORT, instance,
2247 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002248 /* Can't call NCR5380_select() and send ABORT because that
2249 * means releasing the lock. Need a bus reset.
2250 */
Finn Thaindc183962016-02-23 10:07:07 +11002251 set_host_byte(cmd, DID_ERROR);
2252 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002253 result = FAILED;
2254 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002255 }
2256
2257 if (hostdata->connected == cmd) {
2258 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2259 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002260 hostdata->dma_len = 0;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002261 if (do_abort(instance)) {
2262 set_host_byte(cmd, DID_ERROR);
2263 complete_cmd(instance, cmd);
2264 result = FAILED;
2265 goto out;
2266 }
2267 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002268 complete_cmd(instance, cmd);
2269 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002270 }
2271
Finn Thaindc183962016-02-23 10:07:07 +11002272 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002273 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002274 "abort: removed %p from sense queue\n", cmd);
2275 set_host_byte(cmd, DID_ERROR);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002276 complete_cmd(instance, cmd);
2277 }
2278
2279out:
2280 if (result == FAILED)
2281 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2282 else
2283 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2284
2285 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002286 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002287 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002288
Finn Thain8b00c3d2016-01-03 16:06:01 +11002289 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
2292
Finn Thain3be1b3e2016-01-03 16:05:12 +11002293/**
Hannes Reinecke12e5fc62017-08-25 13:57:10 +02002294 * NCR5380_host_reset - reset the SCSI host
Finn Thain3be1b3e2016-01-03 16:05:12 +11002295 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002297 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 */
2299
Hannes Reinecke12e5fc62017-08-25 13:57:10 +02002300static int NCR5380_host_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002301{
2302 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002303 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002304 int i;
Finn Thain11d2f632016-01-03 16:05:51 +11002305 unsigned long flags;
Finn Thain62717f52016-01-03 16:06:03 +11002306 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Finn Thain11d2f632016-01-03 16:05:51 +11002308 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002309
2310#if (NDEBUG & NDEBUG_ANY)
Finn Thain62717f52016-01-03 16:06:03 +11002311 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002312#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002313 NCR5380_dprint(NDEBUG_ANY, instance);
2314 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002315
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002316 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002317
Finn Thain62717f52016-01-03 16:06:03 +11002318 /* reset NCR registers */
2319 NCR5380_write(MODE_REG, MR_BASE);
2320 NCR5380_write(TARGET_COMMAND_REG, 0);
2321 NCR5380_write(SELECT_ENABLE_REG, 0);
2322
2323 /* After the reset, there are no more connected or disconnected commands
2324 * and no busy units; so clear the low-level status here to avoid
2325 * conflicts when the mid-level code tries to wake up the affected
2326 * commands!
2327 */
2328
Finn Thain1884c282016-02-23 10:07:04 +11002329 if (list_del_cmd(&hostdata->unissued, cmd)) {
2330 cmd->result = DID_RESET << 16;
2331 cmd->scsi_done(cmd);
2332 }
2333
2334 if (hostdata->selecting) {
2335 hostdata->selecting->result = DID_RESET << 16;
2336 complete_cmd(instance, hostdata->selecting);
2337 hostdata->selecting = NULL;
2338 }
Finn Thain62717f52016-01-03 16:06:03 +11002339
2340 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2341 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2342
2343 set_host_byte(cmd, DID_RESET);
Finn Thain216fad92016-03-23 21:10:32 +11002344 complete_cmd(instance, cmd);
Finn Thain62717f52016-01-03 16:06:03 +11002345 }
Finn Thain1884c282016-02-23 10:07:04 +11002346 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002347
2348 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2349 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2350
2351 set_host_byte(cmd, DID_RESET);
2352 cmd->scsi_done(cmd);
2353 }
Finn Thain1884c282016-02-23 10:07:04 +11002354 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002355
2356 if (hostdata->connected) {
2357 set_host_byte(hostdata->connected, DID_RESET);
2358 complete_cmd(instance, hostdata->connected);
2359 hostdata->connected = NULL;
2360 }
2361
Finn Thain62717f52016-01-03 16:06:03 +11002362 for (i = 0; i < 8; ++i)
2363 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002364 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002365
2366 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002367 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002368 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return SUCCESS;
2371}