blob: 4ef44fafe6ca20321db4392b7870f123d83f4eca [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 *);
Finn Thain6b0e87a2018-09-27 11:17:11 +1000134static void bus_reset_cleanup(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Finn Thainc16df322016-01-03 16:06:08 +1100136/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100137 * initialize_SCp - init the scsi pointer field
Finn Thain594d4ba2016-01-03 16:06:10 +1100138 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100140 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
142
Finn Thain710ddd02014-11-12 16:12:02 +1100143static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Finn Thainaff0cf92016-01-03 16:06:09 +1100145 /*
146 * Initialize the Scsi Pointer field so that all of the commands in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * various queues are valid.
148 */
149
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200150 if (scsi_bufflen(cmd)) {
151 cmd->SCp.buffer = scsi_sglist(cmd);
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;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200156 cmd->SCp.ptr = NULL;
157 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100159
160 cmd->SCp.Status = 0;
161 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Finn Thain0e9fdd22019-06-18 09:37:57 +0800164static inline void advance_sg_buffer(struct scsi_cmnd *cmd)
165{
166 struct scatterlist *s = cmd->SCp.buffer;
167
168 if (!cmd->SCp.this_residual && s && !sg_is_last(s)) {
169 cmd->SCp.buffer = sg_next(s);
170 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
171 cmd->SCp.this_residual = cmd->SCp.buffer->length;
172 }
173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/**
Finn Thainb32ade12016-01-03 16:05:41 +1100176 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thaind5d37a02016-10-10 00:46:53 -0400177 * @hostdata: host private data
Finn Thainb32ade12016-01-03 16:05:41 +1100178 * @reg1: 5380 register to poll
179 * @bit1: Bitmask to check
180 * @val1: Expected value
181 * @reg2: Second 5380 register to poll
182 * @bit2: Second bitmask to check
183 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100184 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *
Finn Thain2f854b82016-01-03 16:05:22 +1100186 * Polls the chip in a reasonably efficient manner waiting for an
187 * event to occur. After a short quick poll we begin to yield the CPU
188 * (if possible). In irq contexts the time-out is arbitrarily limited.
189 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *
Finn Thainb32ade12016-01-03 16:05:41 +1100191 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Finn Thaind5d37a02016-10-10 00:46:53 -0400194static int NCR5380_poll_politely2(struct NCR5380_hostdata *hostdata,
Finn Thain61e1ce52016-10-10 00:46:53 -0400195 unsigned int reg1, u8 bit1, u8 val1,
196 unsigned int reg2, u8 bit2, u8 val2,
197 unsigned long wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100198{
Finn Thaind4408dd2016-10-10 00:46:52 -0400199 unsigned long n = hostdata->poll_loops;
Finn Thain2f854b82016-01-03 16:05:22 +1100200 unsigned long deadline = jiffies + wait;
Finn Thain2f854b82016-01-03 16:05:22 +1100201
Finn Thain2f854b82016-01-03 16:05:22 +1100202 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100203 if ((NCR5380_read(reg1) & bit1) == val1)
204 return 0;
205 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
207 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100208 } while (n--);
209
210 if (irqs_disabled() || in_interrupt())
211 return -ETIMEDOUT;
212
213 /* Repeatedly sleep for 1 ms until deadline */
214 while (time_is_after_jiffies(deadline)) {
215 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100216 if ((NCR5380_read(reg1) & bit1) == val1)
217 return 0;
218 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Finn Thain2f854b82016-01-03 16:05:22 +1100221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -ETIMEDOUT;
223}
224
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100225#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static struct {
227 unsigned char mask;
228 const char *name;
Finn Thainaff0cf92016-01-03 16:06:09 +1100229} signals[] = {
230 {SR_DBP, "PARITY"},
231 {SR_RST, "RST"},
232 {SR_BSY, "BSY"},
233 {SR_REQ, "REQ"},
234 {SR_MSG, "MSG"},
235 {SR_CD, "CD"},
236 {SR_IO, "IO"},
237 {SR_SEL, "SEL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100239},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240basrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100241 {BASR_END_DMA_TRANSFER, "END OF DMA"},
242 {BASR_DRQ, "DRQ"},
243 {BASR_PARITY_ERROR, "PARITY ERROR"},
244 {BASR_IRQ, "IRQ"},
245 {BASR_PHASE_MATCH, "PHASE MATCH"},
246 {BASR_BUSY_ERROR, "BUSY ERROR"},
Finn Thainaff0cf92016-01-03 16:06:09 +1100247 {BASR_ATN, "ATN"},
248 {BASR_ACK, "ACK"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100250},
251icrs[] = {
252 {ICR_ASSERT_RST, "ASSERT RST"},
Finn Thain12866b92016-03-23 21:10:25 +1100253 {ICR_ARBITRATION_PROGRESS, "ARB. IN PROGRESS"},
254 {ICR_ARBITRATION_LOST, "LOST ARB."},
Finn Thainaff0cf92016-01-03 16:06:09 +1100255 {ICR_ASSERT_ACK, "ASSERT ACK"},
256 {ICR_ASSERT_BSY, "ASSERT BSY"},
257 {ICR_ASSERT_SEL, "ASSERT SEL"},
258 {ICR_ASSERT_ATN, "ASSERT ATN"},
259 {ICR_ASSERT_DATA, "ASSERT DATA"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100261},
262mrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100263 {MR_BLOCK_DMA_MODE, "BLOCK DMA MODE"},
264 {MR_TARGET, "TARGET"},
265 {MR_ENABLE_PAR_CHECK, "PARITY CHECK"},
266 {MR_ENABLE_PAR_INTR, "PARITY INTR"},
267 {MR_ENABLE_EOP_INTR, "EOP INTR"},
268 {MR_MONITOR_BSY, "MONITOR BSY"},
269 {MR_DMA_MODE, "DMA MODE"},
270 {MR_ARBITRATE, "ARBITRATE"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 {0, NULL}
272};
273
274/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100275 * NCR5380_print - print scsi bus signals
276 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100278 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280
281static void NCR5380_print(struct Scsi_Host *instance)
282{
Finn Thain61e1ce52016-10-10 00:46:53 -0400283 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain8cee3e12019-03-08 08:49:02 +1100284 unsigned char status, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 status = NCR5380_read(STATUS_REG);
287 mr = NCR5380_read(MODE_REG);
288 icr = NCR5380_read(INITIATOR_COMMAND_REG);
289 basr = NCR5380_read(BUS_AND_STATUS_REG);
290
Finn Thain12866b92016-03-23 21:10:25 +1100291 printk(KERN_DEBUG "SR = 0x%02x : ", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 for (i = 0; signals[i].mask; ++i)
293 if (status & signals[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100294 printk(KERN_CONT "%s, ", signals[i].name);
295 printk(KERN_CONT "\nBASR = 0x%02x : ", basr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 for (i = 0; basrs[i].mask; ++i)
297 if (basr & basrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100298 printk(KERN_CONT "%s, ", basrs[i].name);
299 printk(KERN_CONT "\nICR = 0x%02x : ", icr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 for (i = 0; icrs[i].mask; ++i)
301 if (icr & icrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100302 printk(KERN_CONT "%s, ", icrs[i].name);
303 printk(KERN_CONT "\nMR = 0x%02x : ", mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 for (i = 0; mrs[i].mask; ++i)
305 if (mr & mrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100306 printk(KERN_CONT "%s, ", mrs[i].name);
307 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Finn Thain0d2cf862016-01-03 16:06:11 +1100310static struct {
311 unsigned char value;
312 const char *name;
313} phases[] = {
314 {PHASE_DATAOUT, "DATAOUT"},
315 {PHASE_DATAIN, "DATAIN"},
316 {PHASE_CMDOUT, "CMDOUT"},
317 {PHASE_STATIN, "STATIN"},
318 {PHASE_MSGOUT, "MSGOUT"},
319 {PHASE_MSGIN, "MSGIN"},
320 {PHASE_UNKNOWN, "UNKNOWN"}
321};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Finn Thainc16df322016-01-03 16:06:08 +1100323/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100324 * NCR5380_print_phase - show SCSI phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100325 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100327 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
329
330static void NCR5380_print_phase(struct Scsi_Host *instance)
331{
Finn Thain61e1ce52016-10-10 00:46:53 -0400332 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 unsigned char status;
334 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 status = NCR5380_read(STATUS_REG);
337 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100338 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 else {
Finn Thain0d2cf862016-01-03 16:06:11 +1100340 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
341 (phases[i].value != (status & PHASE_MASK)); ++i)
342 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100343 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345}
346#endif
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/**
Finn Thain09028462017-01-15 18:50:57 -0500349 * NCR5380_info - report driver and host information
Finn Thain594d4ba2016-01-03 16:06:10 +1100350 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100352 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
354
Finn Thain8c325132014-11-12 16:11:58 +1100355static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Finn Thain8c325132014-11-12 16:11:58 +1100357 struct NCR5380_hostdata *hostdata = shost_priv(instance);
358
359 return hostdata->info;
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100363 * NCR5380_init - initialise an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100364 * @instance: adapter to configure
365 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100367 * Initializes *instance and corresponding 5380 chip,
368 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100370 * Notes : I assume that the host, hostno, and id bits have been
Finn Thain0d2cf862016-01-03 16:06:11 +1100371 * set correctly. I don't care about the irq and other fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100373 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 */
375
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800376static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Finn Thaine8a60142016-01-03 16:05:54 +1100378 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100379 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100380 unsigned long deadline;
Finn Thaind4408dd2016-10-10 00:46:52 -0400381 unsigned long accesses_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Finn Thainae5e33a2016-03-23 21:10:23 +1100383 instance->max_lun = 7;
384
Finn Thain0d2cf862016-01-03 16:06:11 +1100385 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 hostdata->id_mask = 1 << instance->this_id;
Finn Thain0d2cf862016-01-03 16:06:11 +1100387 hostdata->id_higher_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
389 if (i > hostdata->id_mask)
390 hostdata->id_higher_mask |= i;
391 for (i = 0; i < 8; ++i)
392 hostdata->busy[i] = 0;
Finn Thaine4dec682016-03-23 21:10:12 +1100393 hostdata->dma_len = 0;
394
Finn Thain11d2f632016-01-03 16:05:51 +1100395 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100397 hostdata->sensing = NULL;
398 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100399 INIT_LIST_HEAD(&hostdata->unissued);
400 INIT_LIST_HEAD(&hostdata->disconnected);
401
Finn Thain55181be2016-01-03 16:05:42 +1100402 hostdata->flags = flags;
Finn Thainaff0cf92016-01-03 16:06:09 +1100403
Finn Thain8d8601a2016-01-03 16:05:37 +1100404 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100405 hostdata->work_q = alloc_workqueue("ncr5380_%d",
406 WQ_UNBOUND | WQ_MEM_RECLAIM,
407 1, instance->host_no);
408 if (!hostdata->work_q)
409 return -ENOMEM;
410
Finn Thain09028462017-01-15 18:50:57 -0500411 snprintf(hostdata->info, sizeof(hostdata->info),
412 "%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}",
413 instance->hostt->name, instance->irq, hostdata->io_port,
414 hostdata->base, instance->can_queue, instance->cmd_per_lun,
415 instance->sg_tablesize, instance->this_id,
416 hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
417 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
418 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "");
Finn Thain8c325132014-11-12 16:11:58 +1100419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
421 NCR5380_write(MODE_REG, MR_BASE);
422 NCR5380_write(TARGET_COMMAND_REG, 0);
423 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100424
425 /* Calibrate register polling loop */
426 i = 0;
427 deadline = jiffies + 1;
428 do {
429 cpu_relax();
430 } while (time_is_after_jiffies(deadline));
431 deadline += msecs_to_jiffies(256);
432 do {
433 NCR5380_read(STATUS_REG);
434 ++i;
435 cpu_relax();
436 } while (time_is_after_jiffies(deadline));
Finn Thaind4408dd2016-10-10 00:46:52 -0400437 accesses_per_ms = i / 256;
438 hostdata->poll_loops = NCR5380_REG_POLL_TIME * accesses_per_ms / 2;
Finn Thain2f854b82016-01-03 16:05:22 +1100439
Finn Thainb6488f92016-01-03 16:05:08 +1100440 return 0;
441}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Finn Thainb6488f92016-01-03 16:05:08 +1100443/**
444 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
445 * @instance: adapter to check
446 *
447 * If the system crashed, it may have crashed with a connected target and
448 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
449 * currently established nexus, which we know nothing about. Failing that
450 * do a bus reset.
451 *
452 * Note that a bus reset will cause the chip to assert IRQ.
453 *
454 * Returns 0 if successful, otherwise -ENXIO.
455 */
456
457static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
458{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100459 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100460 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
463 switch (pass) {
464 case 1:
465 case 3:
466 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100467 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
Finn Thaind5d37a02016-10-10 00:46:53 -0400468 NCR5380_poll_politely(hostdata,
Finn Thain636b1ec2016-01-03 16:05:10 +1100469 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100472 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 do_abort(instance);
474 break;
475 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100476 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100478 /* Wait after a reset; the SCSI standard calls for
479 * 250ms, we wait 500ms to be on the safe side.
480 * But some Toshiba CD-ROMs need ten times that.
481 */
482 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
483 msleep(2500);
484 else
485 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100488 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -ENXIO;
490 }
491 }
492 return 0;
493}
494
495/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100496 * NCR5380_exit - remove an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100497 * @instance: adapter to remove
Finn Thain0d2cf862016-01-03 16:06:11 +1100498 *
499 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
501
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800502static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Finn Thaine8a60142016-01-03 16:05:54 +1100504 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Finn Thain8d8601a2016-01-03 16:05:37 +1100506 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100507 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510/**
Finn Thain677e0192016-01-03 16:05:59 +1100511 * complete_cmd - finish processing a command and return it to the SCSI ML
512 * @instance: the host instance
513 * @cmd: command to complete
514 */
515
516static void complete_cmd(struct Scsi_Host *instance,
517 struct scsi_cmnd *cmd)
518{
519 struct NCR5380_hostdata *hostdata = shost_priv(instance);
520
521 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
522
Finn Thainf27db8e2016-01-03 16:06:00 +1100523 if (hostdata->sensing == cmd) {
524 /* Autosense processing ends here */
Finn Thain07035652018-09-27 11:17:11 +1000525 if (status_byte(cmd->result) != GOOD) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100526 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
Finn Thain07035652018-09-27 11:17:11 +1000527 } else {
Finn Thainf27db8e2016-01-03 16:06:00 +1100528 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
Finn Thain07035652018-09-27 11:17:11 +1000529 set_driver_byte(cmd, DRIVER_SENSE);
530 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100531 hostdata->sensing = NULL;
532 }
533
Finn Thain677e0192016-01-03 16:05:59 +1100534 cmd->scsi_done(cmd);
535}
536
537/**
Finn Thain1bb40582016-01-03 16:05:29 +1100538 * NCR5380_queue_command - queue a command
539 * @instance: the relevant SCSI adapter
540 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 *
Finn Thain1bb40582016-01-03 16:05:29 +1100542 * cmd is added to the per-instance issue queue, with minor
543 * twiddling done to the host specific fields of cmd. If the
544 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
546
Finn Thain1bb40582016-01-03 16:05:29 +1100547static int NCR5380_queue_command(struct Scsi_Host *instance,
548 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Finn Thain1bb40582016-01-03 16:05:29 +1100550 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100551 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100552 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554#if (NDEBUG & NDEBUG_NO_WRITE)
555 switch (cmd->cmnd[0]) {
556 case WRITE_6:
557 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100558 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100560 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return 0;
562 }
Finn Thain0d2cf862016-01-03 16:06:11 +1100563#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 cmd->result = 0;
566
Finn Thain52d3e562016-03-23 21:10:20 +1100567 if (!NCR5380_acquire_dma_irq(instance))
568 return SCSI_MLQUEUE_HOST_BUSY;
569
Finn Thain11d2f632016-01-03 16:05:51 +1100570 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100571
Finn Thainaff0cf92016-01-03 16:06:09 +1100572 /*
573 * Insert the cmd into the issue queue. Note that REQUEST SENSE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * commands are added to the head of the queue since any command will
Finn Thainaff0cf92016-01-03 16:06:09 +1100575 * clear the contingent allegiance condition that exists and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * sense data is only guaranteed to be valid while the condition exists.
577 */
578
Finn Thain32b26a12016-01-03 16:05:58 +1100579 if (cmd->cmnd[0] == REQUEST_SENSE)
580 list_add(&ncmd->list, &hostdata->unissued);
581 else
582 list_add_tail(&ncmd->list, &hostdata->unissued);
583
Finn Thain11d2f632016-01-03 16:05:51 +1100584 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100585
Finn Thaindbb6b352016-01-03 16:05:53 +1100586 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
587 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100590 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 0;
592}
593
Finn Thain52d3e562016-03-23 21:10:20 +1100594static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
595{
596 struct NCR5380_hostdata *hostdata = shost_priv(instance);
597
598 /* Caller does the locking needed to set & test these data atomically */
599 if (list_empty(&hostdata->disconnected) &&
600 list_empty(&hostdata->unissued) &&
601 list_empty(&hostdata->autosense) &&
602 !hostdata->connected &&
Finn Thain4ab2a782017-01-15 18:50:57 -0500603 !hostdata->selecting) {
Finn Thain52d3e562016-03-23 21:10:20 +1100604 NCR5380_release_dma_irq(instance);
Finn Thain4ab2a782017-01-15 18:50:57 -0500605 }
Finn Thain52d3e562016-03-23 21:10:20 +1100606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100609 * dequeue_next_cmd - dequeue a command for processing
610 * @instance: the scsi host instance
611 *
612 * Priority is given to commands on the autosense queue. These commands
613 * need autosense because of a CHECK CONDITION result.
614 *
615 * Returns a command pointer if a command is found for a target that is
616 * not already busy. Otherwise returns NULL.
617 */
618
619static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
620{
621 struct NCR5380_hostdata *hostdata = shost_priv(instance);
622 struct NCR5380_cmd *ncmd;
623 struct scsi_cmnd *cmd;
624
Finn Thain8d5dbec2016-02-23 10:07:09 +1100625 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100626 list_for_each_entry(ncmd, &hostdata->unissued, list) {
627 cmd = NCR5380_to_scmd(ncmd);
628 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
629 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
630
631 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
632 list_del(&ncmd->list);
633 dsprintk(NDEBUG_QUEUES, instance,
634 "dequeue: removed %p from issue queue\n", cmd);
635 return cmd;
636 }
637 }
638 } else {
639 /* Autosense processing begins here */
640 ncmd = list_first_entry(&hostdata->autosense,
641 struct NCR5380_cmd, list);
642 list_del(&ncmd->list);
643 cmd = NCR5380_to_scmd(ncmd);
644 dsprintk(NDEBUG_QUEUES, instance,
645 "dequeue: removed %p from autosense queue\n", cmd);
646 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
647 hostdata->sensing = cmd;
648 return cmd;
649 }
650 return NULL;
651}
652
653static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
654{
655 struct NCR5380_hostdata *hostdata = shost_priv(instance);
656 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
657
Finn Thain8d5dbec2016-02-23 10:07:09 +1100658 if (hostdata->sensing == cmd) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100659 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
660 list_add(&ncmd->list, &hostdata->autosense);
661 hostdata->sensing = NULL;
662 } else
663 list_add(&ncmd->list, &hostdata->unissued);
664}
665
666/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100667 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100669 * NCR5380_main is a coroutine that runs as long as more work can
670 * be done on the NCR5380 host adapters in a system. Both
671 * NCR5380_queue_command() and NCR5380_intr() will try to start it
672 * in case it is not running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
674
David Howellsc4028952006-11-22 14:57:56 +0000675static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
David Howellsc4028952006-11-22 14:57:56 +0000677 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100678 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 struct Scsi_Host *instance = hostdata->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int done;
Finn Thainaff0cf92016-01-03 16:06:09 +1100681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100684
Finn Thain0a4e3612016-01-03 16:06:07 +1100685 spin_lock_irq(&hostdata->lock);
Finn Thainccf6efd2016-02-23 10:07:08 +1100686 while (!hostdata->connected && !hostdata->selecting) {
687 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
688
689 if (!cmd)
690 break;
Finn Thainf27db8e2016-01-03 16:06:00 +1100691
692 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100695 * Attempt to establish an I_T_L nexus here.
696 * On success, instance->hostdata->connected is set.
697 * On failure, we must add the command back to the
698 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100700 /*
701 * REQUEST SENSE commands are issued without tagged
702 * queueing, even on SCSI-II devices because the
703 * contingent allegiance condition exists for the
704 * entire unit.
705 */
Finn Thain32b26a12016-01-03 16:05:58 +1100706
Finn Thainccf6efd2016-02-23 10:07:08 +1100707 if (!NCR5380_select(instance, cmd)) {
Finn Thain707d62b2016-01-03 16:06:02 +1100708 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thain52d3e562016-03-23 21:10:20 +1100709 maybe_release_dma_irq(instance);
Finn Thainf27db8e2016-01-03 16:06:00 +1100710 } else {
711 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
712 "main: select failed, returning %p to queue\n", cmd);
713 requeue_cmd(instance, cmd);
714 }
715 }
Finn Thaine4dec682016-03-23 21:10:12 +1100716 if (hostdata->connected && !hostdata->dma_len) {
Finn Thainb7465452016-01-03 16:06:05 +1100717 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100720 }
Finn Thain0a4e3612016-01-03 16:06:07 +1100721 spin_unlock_irq(&hostdata->lock);
722 if (!done)
723 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Finn Thain8053b0e2016-03-23 21:10:19 +1100727/*
728 * NCR5380_dma_complete - finish DMA transfer
729 * @instance: the scsi host instance
730 *
731 * Called by the interrupt handler when DMA finishes or a phase
732 * mismatch occurs (which would end the DMA transfer).
733 */
734
735static void NCR5380_dma_complete(struct Scsi_Host *instance)
736{
737 struct NCR5380_hostdata *hostdata = shost_priv(instance);
738 int transferred;
739 unsigned char **data;
740 int *count;
741 int saved_data = 0, overrun = 0;
742 unsigned char p;
743
744 if (hostdata->read_overruns) {
745 p = hostdata->connected->SCp.phase;
746 if (p & SR_IO) {
747 udelay(10);
748 if ((NCR5380_read(BUS_AND_STATUS_REG) &
749 (BASR_PHASE_MATCH | BASR_ACK)) ==
750 (BASR_PHASE_MATCH | BASR_ACK)) {
751 saved_data = NCR5380_read(INPUT_DATA_REG);
752 overrun = 1;
753 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
754 }
755 }
756 }
757
Finn Thaine9db3192016-03-23 21:10:21 +1100758#ifdef CONFIG_SUN3
759 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
760 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
761 instance->host_no);
762 BUG();
763 }
764
765 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
766 (BASR_PHASE_MATCH | BASR_ACK)) {
767 pr_err("scsi%d: BASR %02x\n", instance->host_no,
768 NCR5380_read(BUS_AND_STATUS_REG));
769 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
770 instance->host_no);
771 BUG();
772 }
773#endif
774
Finn Thain8053b0e2016-03-23 21:10:19 +1100775 NCR5380_write(MODE_REG, MR_BASE);
776 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
777 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
778
Finn Thain4a98f892016-10-10 00:46:53 -0400779 transferred = hostdata->dma_len - NCR5380_dma_residual(hostdata);
Finn Thain8053b0e2016-03-23 21:10:19 +1100780 hostdata->dma_len = 0;
781
782 data = (unsigned char **)&hostdata->connected->SCp.ptr;
783 count = &hostdata->connected->SCp.this_residual;
784 *data += transferred;
785 *count -= transferred;
786
787 if (hostdata->read_overruns) {
788 int cnt, toPIO;
789
790 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
791 cnt = toPIO = hostdata->read_overruns;
792 if (overrun) {
793 dsprintk(NDEBUG_DMA, instance,
794 "Got an input overrun, using saved byte\n");
795 *(*data)++ = saved_data;
796 (*count)--;
797 cnt--;
798 toPIO--;
799 }
800 if (toPIO > 0) {
801 dsprintk(NDEBUG_DMA, instance,
802 "Doing %d byte PIO to 0x%p\n", cnt, *data);
803 NCR5380_transfer_pio(instance, &p, &cnt, data);
804 *count -= toPIO - cnt;
805 }
806 }
807 }
808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810/**
Finn Thaincd400822016-01-03 16:05:40 +1100811 * NCR5380_intr - generic NCR5380 irq handler
812 * @irq: interrupt number
813 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 *
Finn Thaincd400822016-01-03 16:05:40 +1100815 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
816 * from the disconnected queue, and restarting NCR5380_main()
817 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 *
Finn Thaincd400822016-01-03 16:05:40 +1100819 * The chip can assert IRQ in any of six different conditions. The IRQ flag
820 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
821 * Three of these six conditions are latched in the Bus and Status Register:
822 * - End of DMA (cleared by ending DMA Mode)
823 * - Parity error (cleared by reading RPIR)
824 * - Loss of BSY (cleared by reading RPIR)
825 * Two conditions have flag bits that are not latched:
826 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
827 * - Bus reset (non-maskable)
828 * The remaining condition has no flag bit at all:
829 * - Selection/reselection
830 *
831 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
832 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
833 * claimed that "the design of the [DP8490] interrupt logic ensures
834 * interrupts will not be lost (they can be on the DP5380)."
835 * The L5380/53C80 datasheet from LOGIC Devices has more details.
836 *
837 * Checking for bus reset by reading RST is futile because of interrupt
838 * latency, but a bus reset will reset chip logic. Checking for parity error
839 * is unnecessary because that interrupt is never enabled. A Loss of BSY
840 * condition will clear DMA Mode. We can tell when this occurs because the
841 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
843
Finn Thaina46865d2016-03-23 21:10:27 +1100844static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800846 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100847 struct NCR5380_hostdata *hostdata = shost_priv(instance);
848 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 unsigned char basr;
850 unsigned long flags;
851
Finn Thain11d2f632016-01-03 16:05:51 +1100852 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Finn Thaincd400822016-01-03 16:05:40 +1100854 basr = NCR5380_read(BUS_AND_STATUS_REG);
855 if (basr & BASR_IRQ) {
856 unsigned char mr = NCR5380_read(MODE_REG);
857 unsigned char sr = NCR5380_read(STATUS_REG);
858
Finn Thainb7465452016-01-03 16:06:05 +1100859 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
860 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100861
Finn Thain8053b0e2016-03-23 21:10:19 +1100862 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
863 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
864 * We ack IRQ after clearing Mode Register. Workarounds
865 * for End of DMA errata need to happen in DMA Mode.
866 */
867
868 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
869
870 if (hostdata->connected) {
871 NCR5380_dma_complete(instance);
872 queue_work(hostdata->work_q, &hostdata->main_task);
873 } else {
874 NCR5380_write(MODE_REG, MR_BASE);
875 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
876 }
877 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
Finn Thaincd400822016-01-03 16:05:40 +1100878 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
879 /* Probably reselected */
880 NCR5380_write(SELECT_ENABLE_REG, 0);
881 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
882
Finn Thainb7465452016-01-03 16:06:05 +1100883 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100884
885 if (!hostdata->connected) {
886 NCR5380_reselect(instance);
887 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Finn Thaincd400822016-01-03 16:05:40 +1100889 if (!hostdata->connected)
890 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
891 } else {
892 /* Probably Bus Reset */
893 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
894
Finn Thain6b0e87a2018-09-27 11:17:11 +1000895 if (sr & SR_RST) {
896 /* Certainly Bus Reset */
897 shost_printk(KERN_WARNING, instance,
898 "bus reset interrupt\n");
899 bus_reset_cleanup(instance);
900 } else {
901 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
902 }
Finn Thaine9db3192016-03-23 21:10:21 +1100903#ifdef SUN3_SCSI_VME
904 dregs->csr |= CSR_DMA_ENABLE;
905#endif
Finn Thaincd400822016-01-03 16:05:40 +1100906 }
907 handled = 1;
908 } else {
Finn Thain9af9fec2016-10-10 00:46:53 -0400909 dsprintk(NDEBUG_INTR, instance, "interrupt without IRQ bit\n");
Finn Thaine9db3192016-03-23 21:10:21 +1100910#ifdef SUN3_SCSI_VME
911 dregs->csr |= CSR_DMA_ENABLE;
912#endif
Finn Thaincd400822016-01-03 16:05:40 +1100913 }
914
Finn Thain11d2f632016-01-03 16:05:51 +1100915 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100916
917 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Finn Thaindad82612018-09-27 11:17:11 +1000920/**
921 * NCR5380_select - attempt arbitration and selection for a given command
922 * @instance: the Scsi_Host instance
923 * @cmd: the scsi_cmnd to execute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 *
Finn Thaindad82612018-09-27 11:17:11 +1000925 * This routine establishes an I_T_L nexus for a SCSI command. This involves
926 * ARBITRATION, SELECTION and MESSAGE OUT phases and an IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 *
Finn Thaindad82612018-09-27 11:17:11 +1000928 * Returns true if the operation should be retried.
929 * Returns false if it should not be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100931 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +1100932 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
933 * with registers as they should have been on entry - ie
934 * SELECT_ENABLE will be set appropriately, the NCR5380
935 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 *
Finn Thaindad82612018-09-27 11:17:11 +1000937 * If successful : the I_T_L nexus will be established, and
938 * hostdata->connected will be set to cmd.
Finn Thain594d4ba2016-01-03 16:06:10 +1100939 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100941 * If failed (no target) : cmd->scsi_done() will be called, and the
942 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100944
Finn Thaindad82612018-09-27 11:17:11 +1000945static bool NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Finn Thain4ab2a782017-01-15 18:50:57 -0500946 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Finn Thaine8a60142016-01-03 16:05:54 +1100948 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 unsigned char tmp[3], phase;
950 unsigned char *data;
951 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 int err;
Finn Thaindad82612018-09-27 11:17:11 +1000953 bool ret = true;
Finn Thain7c8ed782018-09-27 11:17:11 +1000954 bool can_disconnect = instance->irq != NO_IRQ &&
955 cmd->cmnd[0] != REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +1100958 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
959 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Finn Thain707d62b2016-01-03 16:06:02 +1100961 /*
962 * Arbitration and selection phases are slow and involve dropping the
963 * lock, so we have to watch out for EH. An exception handler may
Finn Thaindad82612018-09-27 11:17:11 +1000964 * change 'selecting' to NULL. This function will then return false
Finn Thain707d62b2016-01-03 16:06:02 +1100965 * so that the caller will forget about 'cmd'. (During information
966 * transfer phases, EH may change 'connected' to NULL.)
967 */
968 hostdata->selecting = cmd;
969
Finn Thainaff0cf92016-01-03 16:06:09 +1100970 /*
971 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * data bus during SELECTION.
973 */
974
975 NCR5380_write(TARGET_COMMAND_REG, 0);
976
Finn Thainaff0cf92016-01-03 16:06:09 +1100977 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * Start arbitration.
979 */
980
981 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
982 NCR5380_write(MODE_REG, MR_ARBITRATE);
983
Finn Thain55500d92016-01-03 16:05:35 +1100984 /* The chip now waits for BUS FREE phase. Then after the 800 ns
985 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 */
987
Finn Thain11d2f632016-01-03 16:05:51 +1100988 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -0400989 err = NCR5380_poll_politely2(hostdata, MODE_REG, MR_ARBITRATE, 0,
Finn Thainb32ade12016-01-03 16:05:41 +1100990 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
991 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +1100992 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +1100993 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
994 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +1100995 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +1100996 }
Finn Thainccf6efd2016-02-23 10:07:08 +1100997 if (!hostdata->selecting) {
998 /* Command was aborted */
999 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaindad82612018-09-27 11:17:11 +10001000 return false;
Finn Thainccf6efd2016-02-23 10:07:08 +11001001 }
Finn Thainb32ade12016-01-03 16:05:41 +11001002 if (err < 0) {
1003 NCR5380_write(MODE_REG, MR_BASE);
1004 shost_printk(KERN_ERR, instance,
1005 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001006 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001007 }
Finn Thain11d2f632016-01-03 16:05:51 +11001008 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001009
1010 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 udelay(3);
1012
1013 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +11001014 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1015 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1016 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001018 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001019 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001020 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
Finn Thaincf13b082016-01-03 16:05:18 +11001022
1023 /* After/during arbitration, BSY should be asserted.
1024 * IBM DPES-31080 Version S31Q works now
1025 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1026 */
1027 NCR5380_write(INITIATOR_COMMAND_REG,
1028 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Finn Thainaff0cf92016-01-03 16:06:09 +11001030 /*
1031 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 * a minimum so we'll udelay ceil(1.2)
1033 */
1034
Finn Thain9c3f0e22016-01-03 16:05:11 +11001035 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1036 udelay(15);
1037 else
1038 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Finn Thain11d2f632016-01-03 16:05:51 +11001040 spin_lock_irq(&hostdata->lock);
1041
Finn Thain72064a72016-01-03 16:05:44 +11001042 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1043 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001044 goto out;
1045
1046 if (!hostdata->selecting) {
1047 NCR5380_write(MODE_REG, MR_BASE);
1048 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaindad82612018-09-27 11:17:11 +10001049 return false;
Finn Thain707d62b2016-01-03 16:06:02 +11001050 }
Finn Thain72064a72016-01-03 16:05:44 +11001051
Finn Thainb7465452016-01-03 16:06:05 +11001052 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Finn Thainaff0cf92016-01-03 16:06:09 +11001054 /*
1055 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 * the host and target ID's on the SCSI bus.
1057 */
1058
Finn Thain3d07d222016-01-03 16:06:13 +11001059 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Finn Thainaff0cf92016-01-03 16:06:09 +11001061 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 * Raise ATN while SEL is true before BSY goes false from arbitration,
1063 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1064 * phase immediately after selection.
1065 */
1066
Finn Thain3d07d222016-01-03 16:06:13 +11001067 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1068 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 NCR5380_write(MODE_REG, MR_BASE);
1070
Finn Thainaff0cf92016-01-03 16:06:09 +11001071 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 * Reselect interrupts must be turned off prior to the dropping of BSY,
1073 * otherwise we will trigger an interrupt.
1074 */
1075 NCR5380_write(SELECT_ENABLE_REG, 0);
1076
Finn Thain11d2f632016-01-03 16:05:51 +11001077 spin_unlock_irq(&hostdata->lock);
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001080 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 * the BSY signal.
1082 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001083 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001086 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1087 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Finn Thainaff0cf92016-01-03 16:06:09 +11001089 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001091 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 * appropriate propagation delay has expired, and we're confusing
1093 * a BSY signal from ourselves as the target's response to SELECTION.
1094 *
1095 * A small delay (the 'C++' frontend breaks the pipeline with an
1096 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001097 * tighter 'C' code breaks and requires this) solves the problem -
1098 * the 1 us delay is arbitrary, and only used because this delay will
1099 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 * work there.
1101 *
1102 * wingel suggests that this could be due to failing to wait
1103 * one deskew delay.
1104 */
1105
1106 udelay(1);
1107
Finn Thainb7465452016-01-03 16:06:05 +11001108 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Finn Thainaff0cf92016-01-03 16:06:09 +11001110 /*
1111 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * selection.
1113 */
1114
Finn Thaind5d37a02016-10-10 00:46:53 -04001115 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_BSY, SR_BSY,
Finn Thainae753a32016-01-03 16:05:23 +11001116 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001119 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1121 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001122 if (!hostdata->connected)
1123 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001124 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001125 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
Finn Thainae753a32016-01-03 16:05:23 +11001127
1128 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001129 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001130 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thainae753a32016-01-03 16:05:23 +11001131 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a162832018-09-27 11:17:11 +10001132
Finn Thain707d62b2016-01-03 16:06:02 +11001133 /* Can't touch cmd if it has been reclaimed by the scsi ML */
Finn Thain6a162832018-09-27 11:17:11 +10001134 if (!hostdata->selecting)
Finn Thaindad82612018-09-27 11:17:11 +10001135 return false;
Finn Thain6a162832018-09-27 11:17:11 +10001136
1137 cmd->result = DID_BAD_TARGET << 16;
1138 complete_cmd(instance, cmd);
1139 dsprintk(NDEBUG_SELECTION, instance,
1140 "target did not respond within 250ms\n");
Finn Thaindad82612018-09-27 11:17:11 +10001141 ret = false;
Finn Thain707d62b2016-01-03 16:06:02 +11001142 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001143 }
1144
Finn Thainaff0cf92016-01-03 16:06:09 +11001145 /*
1146 * No less than two deskew delays after the initiator detects the
1147 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 * change the DATA BUS. -wingel
1149 */
1150
1151 udelay(1);
1152
1153 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001156 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 * was true but before BSY was false during selection, the information
1158 * transfer phase should be a MESSAGE OUT phase so that we can send the
1159 * IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 */
1161
1162 /* Wait for start of REQ/ACK handshake */
1163
Finn Thaind5d37a02016-10-10 00:46:53 -04001164 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001165 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001166 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001167 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1168 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001170 goto out;
1171 }
1172 if (!hostdata->selecting) {
1173 do_abort(instance);
Finn Thaindad82612018-09-27 11:17:11 +10001174 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176
Finn Thainb7465452016-01-03 16:06:05 +11001177 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1178 scmd_id(cmd));
Finn Thain7c8ed782018-09-27 11:17:11 +10001179 tmp[0] = IDENTIFY(can_disconnect, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 len = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 data = tmp;
1183 phase = PHASE_MSGOUT;
1184 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb15e7912017-01-15 18:50:57 -05001185 if (len) {
1186 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1187 cmd->result = DID_ERROR << 16;
1188 complete_cmd(instance, cmd);
1189 dsprintk(NDEBUG_SELECTION, instance, "IDENTIFY message transfer failed\n");
Finn Thaindad82612018-09-27 11:17:11 +10001190 ret = false;
Finn Thainb15e7912017-01-15 18:50:57 -05001191 goto out;
1192 }
1193
Finn Thainb7465452016-01-03 16:06:05 +11001194 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001197 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Finn Thaine9db3192016-03-23 21:10:21 +11001199#ifdef SUN3_SCSI_VME
1200 dregs->csr |= CSR_INTR;
1201#endif
1202
Boaz Harrosh28424d32007-09-10 22:37:45 +03001203 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Finn Thaindad82612018-09-27 11:17:11 +10001205 ret = false;
Finn Thain707d62b2016-01-03 16:06:02 +11001206
1207out:
1208 if (!hostdata->selecting)
Finn Thain96edebd2018-10-24 18:45:33 +11001209 return false;
Finn Thain707d62b2016-01-03 16:06:02 +11001210 hostdata->selecting = NULL;
Finn Thaindad82612018-09-27 11:17:11 +10001211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Finn Thainaff0cf92016-01-03 16:06:09 +11001214/*
1215 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001216 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 *
1218 * Purpose : transfers data in given phase using polled I/O
1219 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001220 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001221 * what phase is expected, *count - pointer to number of
1222 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001223 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001225 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001226 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001228 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 *
1230 * XXX Note : handling for bus free may be useful.
1231 */
1232
1233/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001234 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 * IS 100% reliable, and for the actual data transfer where speed
1236 * counts, we will always do a pseudo DMA or DMA transfer.
1237 */
1238
Finn Thain0d2cf862016-01-03 16:06:11 +11001239static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1240 unsigned char *phase, int *count,
1241 unsigned char **data)
1242{
Finn Thain61e1ce52016-10-10 00:46:53 -04001243 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 unsigned char p = *phase, tmp;
1245 int c = *count;
1246 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Finn Thainaff0cf92016-01-03 16:06:09 +11001248 /*
1249 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 * phase specified in the appropriate bits of the TARGET COMMAND
1251 * REGISTER match the STATUS REGISTER
1252 */
1253
Finn Thain0d2cf862016-01-03 16:06:11 +11001254 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001257 /*
1258 * Wait for assertion of REQ, after which the phase bits will be
1259 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 */
1261
Finn Thaind5d37a02016-10-10 00:46:53 -04001262 if (NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Finn Thainb7465452016-01-03 16:06:05 +11001265 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001268 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001269 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1270 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /* Do actual transfer from SCSI bus to / from memory */
1275 if (!(p & SR_IO))
1276 NCR5380_write(OUTPUT_DATA_REG, *d);
1277 else
1278 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1279
1280 ++d;
1281
Finn Thainaff0cf92016-01-03 16:06:09 +11001282 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 * The SCSI standard suggests that in MSGOUT phase, the initiator
1284 * should drop ATN on the last byte of the message phase
1285 * after REQ has been asserted for the handshake but before
1286 * the initiator raises ACK.
1287 */
1288
1289 if (!(p & SR_IO)) {
1290 if (!((p & SR_MSG) && c > 1)) {
1291 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1292 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001293 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1294 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001296 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1297 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001299 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1300 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302 } else {
1303 NCR5380_dprint(NDEBUG_PIO, instance);
1304 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1305 }
1306
Finn Thaind5d37a02016-10-10 00:46:53 -04001307 if (NCR5380_poll_politely(hostdata,
Finn Thaina2edc4a2016-01-03 16:05:27 +11001308 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1309 break;
1310
Finn Thainb7465452016-01-03 16:06:05 +11001311 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001314 * We have several special cases to consider during REQ/ACK handshaking :
1315 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001316 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001318 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001319 * message. We must exit with ACK asserted, so that the calling
1320 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 *
1322 * 3. ACK and ATN are clear and the target may proceed as normal.
1323 */
1324 if (!(p == PHASE_MSGIN && c == 1)) {
1325 if (p == PHASE_MSGOUT && c > 1)
1326 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1327 else
1328 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1329 }
1330 } while (--c);
1331
Finn Thainb7465452016-01-03 16:06:05 +11001332 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 *count = c;
1335 *data = d;
1336 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001337 /* The phase read from the bus is valid if either REQ is (already)
1338 * asserted or if ACK hasn't been released yet. The latter applies if
1339 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1340 */
1341 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 *phase = tmp & PHASE_MASK;
1343 else
1344 *phase = PHASE_UNKNOWN;
1345
1346 if (!c || (*phase == p))
1347 return 0;
1348 else
1349 return -1;
1350}
1351
1352/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001353 * do_reset - issue a reset command
1354 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001356 * Issue a reset sequence to the NCR5380 and try and get the bus
1357 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001359 * This clears the reset interrupt flag because there may be no handler for
1360 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1361 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001363
Finn Thain54d8fe42016-01-03 16:05:06 +11001364static void do_reset(struct Scsi_Host *instance)
1365{
Finn Thain61e1ce52016-10-10 00:46:53 -04001366 struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +11001367 unsigned long flags;
1368
1369 local_irq_save(flags);
1370 NCR5380_write(TARGET_COMMAND_REG,
1371 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001373 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001375 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1376 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Finn Thain80d3eb62016-01-03 16:05:34 +11001379/**
1380 * do_abort - abort the currently established nexus by going to
1381 * MESSAGE OUT phase and sending an ABORT message.
1382 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001384 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
1386
Finn Thain54d8fe42016-01-03 16:05:06 +11001387static int do_abort(struct Scsi_Host *instance)
1388{
Finn Thain61e1ce52016-10-10 00:46:53 -04001389 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 unsigned char *msgptr, phase, tmp;
1391 int len;
1392 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* Request message out phase */
1395 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1396
Finn Thainaff0cf92016-01-03 16:06:09 +11001397 /*
1398 * Wait for the target to indicate a valid phase by asserting
1399 * REQ. Once this happens, we'll have either a MSGOUT phase
1400 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001402 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 * We really don't care what value was on the bus or what value
1404 * the target sees, so we just handshake.
1405 */
1406
Finn Thaind5d37a02016-10-10 00:46:53 -04001407 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001408 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001409 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Finn Thainf35d3472016-01-03 16:05:33 +11001411 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1414
Finn Thainf35d3472016-01-03 16:05:33 +11001415 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001416 NCR5380_write(INITIATOR_COMMAND_REG,
1417 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thaind5d37a02016-10-10 00:46:53 -04001418 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001419 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001420 goto timeout;
1421 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 tmp = ABORT;
1425 msgptr = &tmp;
1426 len = 1;
1427 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001428 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 /*
1431 * If we got here, and the command completed successfully,
1432 * we're about to go into bus free state.
1433 */
1434
1435 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001436
1437timeout:
1438 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1439 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Finn Thainaff0cf92016-01-03 16:06:09 +11001442/*
1443 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001444 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 *
1446 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001447 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001449 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001450 * what phase is expected, *count - pointer to number of
1451 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001452 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001454 * maximum number of bytes, 0 if all bytes or transferred or exit
1455 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001457 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 */
1459
1460
Finn Thain0d2cf862016-01-03 16:06:11 +11001461static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1462 unsigned char *phase, int *count,
1463 unsigned char **data)
1464{
1465 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainf0ea73a2016-03-23 21:10:26 +11001466 int c = *count;
1467 unsigned char p = *phase;
1468 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 unsigned char tmp;
Finn Thain8053b0e2016-03-23 21:10:19 +11001470 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1473 *phase = tmp;
1474 return -1;
1475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Finn Thain8053b0e2016-03-23 21:10:19 +11001477 hostdata->connected->SCp.phase = p;
1478
1479 if (p & SR_IO) {
1480 if (hostdata->read_overruns)
1481 c -= hostdata->read_overruns;
1482 else if (hostdata->flags & FLAG_DMA_FIXUP)
1483 --c;
1484 }
1485
1486 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1487 (p & SR_IO) ? "receive" : "send", c, d);
1488
Finn Thaine9db3192016-03-23 21:10:21 +11001489#ifdef CONFIG_SUN3
1490 /* send start chain */
1491 sun3scsi_dma_start(c, *data);
1492#endif
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thain8053b0e2016-03-23 21:10:19 +11001495 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1496 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Finn Thain8053b0e2016-03-23 21:10:19 +11001498 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1499 /* On the Medusa, it is a must to initialize the DMA before
1500 * starting the NCR. This is also the cleaner way for the TT.
1501 */
1502 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001503 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001504 else
Finn Thain4a98f892016-10-10 00:46:53 -04001505 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Finn Thainaff0cf92016-01-03 16:06:09 +11001508 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001509 * On the PAS16 at least I/O recovery delays are not needed here.
1510 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
1512
1513 if (p & SR_IO) {
Finn Thaine9db3192016-03-23 21:10:21 +11001514 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaine5d55d12016-03-23 21:10:16 +11001515 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1517 } else {
Finn Thaine5d55d12016-03-23 21:10:16 +11001518 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thaine5d55d12016-03-23 21:10:16 +11001520 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 NCR5380_write(START_DMA_SEND_REG, 0);
Finn Thaine5d55d12016-03-23 21:10:16 +11001522 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524
Finn Thaine9db3192016-03-23 21:10:21 +11001525#ifdef CONFIG_SUN3
1526#ifdef SUN3_SCSI_VME
1527 dregs->csr |= CSR_DMA_ENABLE;
1528#endif
1529 sun3_dma_active = 1;
1530#endif
1531
Finn Thain8053b0e2016-03-23 21:10:19 +11001532 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1533 /* On the Falcon, the DMA setup must be done after the last
1534 * NCR access, else the DMA setup gets trashed!
1535 */
1536 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001537 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001538 else
Finn Thain4a98f892016-10-10 00:46:53 -04001539 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001540 }
1541
1542 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1543 if (result < 0)
1544 return result;
1545
1546 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1547 if (result > 0) {
1548 hostdata->dma_len = result;
1549 return 0;
1550 }
1551
1552 /* The result is zero iff pseudo DMA send/receive was completed. */
1553 hostdata->dma_len = c;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/*
Finn Thaine4dec682016-03-23 21:10:12 +11001556 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001557 *
1558 * For DMA sends, we want to wait until the last byte has been
1559 * transferred out over the bus before we turn off DMA mode. Alas, there
1560 * seems to be no terribly good way of doing this on a 5380 under all
1561 * conditions. For non-scatter-gather operations, we can wait until REQ
1562 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1563 * are nastier, since the device will be expecting more data than we
1564 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1565 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1566 * why this signal was added to the newer chips) but on the older 538[01]
1567 * this signal does not exist. The workaround for this lack is a watchdog;
1568 * we bail out of the wait-loop after a modest amount of wait-time if
1569 * the usual exit conditions are not met. Not a terribly clean or
1570 * correct solution :-%
1571 *
1572 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1573 * If the chip is in DMA receive mode, it will respond to a target's
1574 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1575 * ACK, even if it has _already_ been notified by the DMA controller that
1576 * the current DMA transfer has completed! If the NCR5380 is then taken
1577 * out of DMA mode, this already-acknowledged byte is lost. This is
1578 * not a problem for "one DMA transfer per READ command", because
1579 * the situation will never arise... either all of the data is DMA'ed
1580 * properly, or the target switches to MESSAGE IN phase to signal a
1581 * disconnection (either operation bringing the DMA to a clean halt).
1582 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001583 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001584 * condition before taking the NCR5380 out of DMA mode. One or two extra
1585 * bytes are transferred via PIO as necessary to fill out the original
1586 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 */
1588
Finn Thain8053b0e2016-03-23 21:10:19 +11001589 if (hostdata->flags & FLAG_DMA_FIXUP) {
1590 if (p & SR_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001592 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001593 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 * the chip to latch the last byte, read it, and then disable
1595 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001596 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1598 * REQ is deasserted when ACK is asserted, and not reasserted
1599 * until ACK goes false. Since the NCR5380 won't lower ACK
1600 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001601 * the DMA port or we take the NCR5380 out of DMA mode, we
1602 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 * byte.
1604 */
1605
Finn Thaind5d37a02016-10-10 00:46:53 -04001606 if (NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001607 BASR_DRQ, BASR_DRQ, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001608 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001609 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
Finn Thaind5d37a02016-10-10 00:46:53 -04001611 if (NCR5380_poll_politely(hostdata, STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001612 SR_REQ, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001613 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001614 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1615 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001616 d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
1617 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001619 * Wait for the last byte to be sent. If REQ is being asserted for
1620 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 */
Finn Thaind5d37a02016-10-10 00:46:53 -04001622 if (NCR5380_poll_politely2(hostdata,
Finn Thain55181be2016-01-03 16:05:42 +11001623 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1624 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001625 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001626 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001630
1631 NCR5380_dma_complete(instance);
Finn Thain438af512016-03-23 21:10:18 +11001632 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635/*
1636 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1637 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001638 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001639 * directs us to. Operates on the currently connected command,
1640 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 *
1642 * Inputs : instance, instance for which we are doing commands
1643 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001644 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001645 * modified if a command disconnects, *instance->connected will
1646 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001648 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001649 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
1651
Finn Thain0d2cf862016-01-03 16:06:11 +11001652static void NCR5380_information_transfer(struct Scsi_Host *instance)
Finn Thain4ab2a782017-01-15 18:50:57 -05001653 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Finn Thain0d2cf862016-01-03 16:06:11 +11001654{
Finn Thaine8a60142016-01-03 16:05:54 +11001655 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 unsigned char msgout = NOP;
1657 int sink = 0;
1658 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 unsigned char *data;
1661 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001662 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Finn Thaine9db3192016-03-23 21:10:21 +11001664#ifdef SUN3_SCSI_VME
1665 dregs->csr |= CSR_INTR;
1666#endif
1667
Finn Thain11d2f632016-01-03 16:05:51 +11001668 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001669 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 tmp = NCR5380_read(STATUS_REG);
1672 /* We only have a valid SCSI phase when REQ is asserted */
1673 if (tmp & SR_REQ) {
1674 phase = (tmp & PHASE_MASK);
1675 if (phase != old_phase) {
1676 old_phase = phase;
1677 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1678 }
Finn Thaine9db3192016-03-23 21:10:21 +11001679#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04001680 if (phase == PHASE_CMDOUT &&
1681 sun3_dma_setup_done != cmd) {
1682 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11001683
Finn Thain0e9fdd22019-06-18 09:37:57 +08001684 advance_sg_buffer(cmd);
Finn Thaine9db3192016-03-23 21:10:21 +11001685
Finn Thain4a98f892016-10-10 00:46:53 -04001686 count = sun3scsi_dma_xfer_len(hostdata, cmd);
1687
1688 if (count > 0) {
1689 if (rq_data_dir(cmd->request))
1690 sun3scsi_dma_send_setup(hostdata,
1691 cmd->SCp.ptr, count);
1692 else
1693 sun3scsi_dma_recv_setup(hostdata,
1694 cmd->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11001695 sun3_dma_setup_done = cmd;
1696 }
1697#ifdef SUN3_SCSI_VME
1698 dregs->csr |= CSR_INTR;
1699#endif
1700 }
1701#endif /* CONFIG_SUN3 */
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (sink && (phase != PHASE_MSGOUT)) {
1704 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1705
Finn Thain3d07d222016-01-03 16:06:13 +11001706 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1707 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001708 while (NCR5380_read(STATUS_REG) & SR_REQ)
1709 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001710 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1711 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 sink = 0;
1713 continue;
1714 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 case PHASE_DATAOUT:
1718#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001719 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 sink = 1;
1721 do_abort(instance);
1722 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001723 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001724 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001725 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return;
1727#endif
Finn Thainbf1a0c6f2016-01-03 16:05:47 +11001728 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001729 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * If there is no room left in the current buffer in the
1731 * scatter-gather list, move onto the next one.
1732 */
1733
Finn Thain0e9fdd22019-06-18 09:37:57 +08001734 advance_sg_buffer(cmd);
1735 dsprintk(NDEBUG_INFORMATION, instance,
1736 "this residual %d, sg ents %d\n",
1737 cmd->SCp.this_residual,
1738 sg_nents(cmd->SCp.buffer));
Finn Thain0d2cf862016-01-03 16:06:11 +11001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001741 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 * PSEUDO-DMA for systems that are strictly PIO,
1743 * since we can let the hardware do the handshaking.
1744 *
1745 * For this to work, we need to know the transfersize
1746 * ahead of time, since the pseudo-DMA code will sit
1747 * in an unconditional loop.
1748 */
1749
Finn Thainff3d4572016-01-03 16:05:25 +11001750 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001751 if (!cmd->device->borken)
Finn Thain4a98f892016-10-10 00:46:53 -04001752 transfersize = NCR5380_dma_xfer_len(hostdata, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Finn Thain438af512016-03-23 21:10:18 +11001754 if (transfersize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001756 if (NCR5380_transfer_dma(instance, &phase,
1757 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001759 * If the watchdog timer fires, all future
1760 * accesses to this device will use the
1761 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001763 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001764 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 sink = 1;
1767 do_abort(instance);
1768 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /* XXX - need to source or sink data here, as appropriate */
Finn Thain8053b0e2016-03-23 21:10:19 +11001770 }
Finn Thainf825e402016-03-23 21:10:15 +11001771 } else {
Finn Thain08348b12016-08-31 14:44:56 +10001772 /* Transfer a small chunk so that the
1773 * irq mode lock is not held too long.
Finn Thain16788472016-02-23 10:07:05 +11001774 */
Finn Thain08348b12016-08-31 14:44:56 +10001775 transfersize = min(cmd->SCp.this_residual,
1776 NCR5380_PIO_CHUNK_SIZE);
Finn Thain16788472016-02-23 10:07:05 +11001777 len = transfersize;
1778 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001779 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001780 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001781 }
Finn Thaine9db3192016-03-23 21:10:21 +11001782#ifdef CONFIG_SUN3
1783 if (sun3_dma_setup_done == cmd)
1784 sun3_dma_setup_done = NULL;
1785#endif
Finn Thain16788472016-02-23 10:07:05 +11001786 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 case PHASE_MSGIN:
1788 len = 1;
1789 data = &tmp;
1790 NCR5380_transfer_pio(instance, &phase, &len, &data);
1791 cmd->SCp.Message = tmp;
1792
1793 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 case ABORT:
1795 case COMMAND_COMPLETE:
1796 /* Accept message by clearing ACK */
1797 sink = 1;
1798 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001799 dsprintk(NDEBUG_QUEUES, instance,
1800 "COMMAND COMPLETE %p target %d lun %llu\n",
1801 cmd, scmd_id(cmd), cmd->device->lun);
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001804 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Finn Thainf27db8e2016-01-03 16:06:00 +11001806 cmd->result &= ~0xffff;
1807 cmd->result |= cmd->SCp.Status;
1808 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Finn Thainf27db8e2016-01-03 16:06:00 +11001810 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001811 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001812 else {
1813 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1814 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1815 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1816 cmd);
1817 list_add_tail(&ncmd->list,
1818 &hostdata->autosense);
1819 } else
1820 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822
Finn Thainaff0cf92016-01-03 16:06:09 +11001823 /*
1824 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 * arbitration can resume.
1826 */
1827 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001828
1829 /* Enable reselect interrupts */
1830 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain52d3e562016-03-23 21:10:20 +11001831
1832 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return;
1834 case MESSAGE_REJECT:
1835 /* Accept message by clearing ACK */
1836 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1837 switch (hostdata->last_message) {
1838 case HEAD_OF_QUEUE_TAG:
1839 case ORDERED_QUEUE_TAG:
1840 case SIMPLE_QUEUE_TAG:
1841 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001842 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 break;
1844 default:
1845 break;
1846 }
Finn Thain340b9612016-01-03 16:05:31 +11001847 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001848 case DISCONNECT:
1849 /* Accept message by clearing ACK */
1850 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1851 hostdata->connected = NULL;
1852 list_add(&ncmd->list, &hostdata->disconnected);
1853 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1854 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1855 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001856
Finn Thain0d2cf862016-01-03 16:06:11 +11001857 /*
1858 * Restore phase bits to 0 so an interrupted selection,
1859 * arbitration can resume.
1860 */
1861 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Finn Thain0d2cf862016-01-03 16:06:11 +11001863 /* Enable reselect interrupts */
1864 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine9db3192016-03-23 21:10:21 +11001865#ifdef SUN3_SCSI_VME
1866 dregs->csr |= CSR_DMA_ENABLE;
1867#endif
Finn Thain0d2cf862016-01-03 16:06:11 +11001868 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001869 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001871 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 * ignore SAVE/RESTORE pointers calls.
1873 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001874 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001876 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 * compatible.
1878 */
1879 case SAVE_POINTERS:
1880 case RESTORE_POINTERS:
1881 /* Accept message by clearing ACK */
1882 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1883 break;
1884 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001885 /*
1886 * Start the message buffer with the EXTENDED_MESSAGE
1887 * byte, since spi_print_msg() wants the whole thing.
1888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 extended_msg[0] = EXTENDED_MESSAGE;
1890 /* Accept first byte by clearing ACK */
1891 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001892
1893 spin_unlock_irq(&hostdata->lock);
1894
Finn Thainb7465452016-01-03 16:06:05 +11001895 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 len = 2;
1898 data = extended_msg + 1;
1899 phase = PHASE_MSGIN;
1900 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001901 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1902 (int)extended_msg[1],
1903 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Finn Thaine0783ed2016-01-03 16:05:45 +11001905 if (!len && extended_msg[1] > 0 &&
1906 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /* Accept third byte by clearing ACK */
1908 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1909 len = extended_msg[1] - 1;
1910 data = extended_msg + 3;
1911 phase = PHASE_MSGIN;
1912
1913 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001914 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1915 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 switch (extended_msg[2]) {
1918 case EXTENDED_SDTR:
1919 case EXTENDED_WDTR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 tmp = 0;
1921 }
1922 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001923 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 tmp = 0;
1925 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001926 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1927 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 tmp = 0;
1929 }
Finn Thain11d2f632016-01-03 16:05:51 +11001930
1931 spin_lock_irq(&hostdata->lock);
1932 if (!hostdata->connected)
1933 return;
1934
Finn Thaindf135e32019-03-08 08:49:02 +11001935 /* Reject message */
1936 /* Fall through */
1937 default:
Finn Thainaff0cf92016-01-03 16:06:09 +11001938 /*
1939 * If we get something weird that we aren't expecting,
Finn Thaindf135e32019-03-08 08:49:02 +11001940 * log it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 */
Finn Thain39bef872017-10-26 16:51:50 +11001942 if (tmp == EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04001943 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001944 "rejecting unknown extended message code %02x, length %d\n",
Finn Thain39bef872017-10-26 16:51:50 +11001945 extended_msg[2], extended_msg[1]);
1946 else if (tmp)
1947 scmd_printk(KERN_INFO, cmd,
1948 "rejecting unknown message code %02x\n",
1949 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 msgout = MESSAGE_REJECT;
1952 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1953 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001954 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 break;
1956 case PHASE_MSGOUT:
1957 len = 1;
1958 data = &msgout;
1959 hostdata->last_message = msgout;
1960 NCR5380_transfer_pio(instance, &phase, &len, &data);
1961 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001963 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001965 complete_cmd(instance, cmd);
Finn Thain52d3e562016-03-23 21:10:20 +11001966 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1968 return;
1969 }
1970 msgout = NOP;
1971 break;
1972 case PHASE_CMDOUT:
1973 len = cmd->cmd_len;
1974 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11001975 /*
1976 * XXX for performance reasons, on machines with a
1977 * PSEUDO-DMA architecture we should probably
1978 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 */
1980 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 break;
1982 case PHASE_STATIN:
1983 len = 1;
1984 data = &tmp;
1985 NCR5380_transfer_pio(instance, &phase, &len, &data);
1986 cmd->SCp.Status = tmp;
1987 break;
1988 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001989 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11001990 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11001991 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11001992 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11001993 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -04001994 NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001995 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
Finn Thain11d2f632016-01-03 16:05:51 +11001997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
2000/*
2001 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2002 *
Finn Thainaff0cf92016-01-03 16:06:09 +11002003 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11002004 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2005 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11002006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 */
2009
Finn Thain0d2cf862016-01-03 16:06:11 +11002010static void NCR5380_reselect(struct Scsi_Host *instance)
2011{
Finn Thaine8a60142016-01-03 16:05:54 +11002012 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 unsigned char target_mask;
Finn Thaine9db3192016-03-23 21:10:21 +11002014 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 unsigned char msg[3];
Finn Thain32b26a12016-01-03 16:05:58 +11002016 struct NCR5380_cmd *ncmd;
2017 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 /*
2020 * Disable arbitration, etc. since the host adapter obviously
2021 * lost, and tell an interrupted NCR5380_select() to restart.
2022 */
2023
2024 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain7ef55f62018-09-27 11:17:11 +10002027 if (!target_mask || target_mask & (target_mask - 1)) {
2028 shost_printk(KERN_WARNING, instance,
2029 "reselect: bad target_mask 0x%02x\n", target_mask);
2030 return;
2031 }
Finn Thainb7465452016-01-03 16:06:05 +11002032
Finn Thainaff0cf92016-01-03 16:06:09 +11002033 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 * At this point, we have detected that our SCSI ID is on the bus,
2035 * SEL is true and BSY was false for at least one bus settle delay
2036 * (400 ns).
2037 *
2038 * We must assert BSY ourselves, until the target drops the SEL
2039 * signal.
2040 */
2041
2042 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thaind5d37a02016-10-10 00:46:53 -04002043 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002044 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
Finn Thain08267212018-09-27 11:17:11 +10002045 shost_printk(KERN_ERR, instance, "reselect: !SEL timeout\n");
Finn Thain72064a72016-01-03 16:05:44 +11002046 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2047 return;
2048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2050
2051 /*
2052 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 */
2054
Finn Thaind5d37a02016-10-10 00:46:53 -04002055 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002056 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
Finn Thainca694af2018-09-27 11:17:11 +10002057 if ((NCR5380_read(STATUS_REG) & (SR_BSY | SR_SEL)) == 0)
2058 /* BUS FREE phase */
2059 return;
Finn Thain08267212018-09-27 11:17:11 +10002060 shost_printk(KERN_ERR, instance, "reselect: REQ timeout\n");
Finn Thain72064a72016-01-03 16:05:44 +11002061 do_abort(instance);
2062 return;
2063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Finn Thaine9db3192016-03-23 21:10:21 +11002065#ifdef CONFIG_SUN3
2066 /* acknowledge toggle to MSGIN */
2067 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Finn Thaine9db3192016-03-23 21:10:21 +11002069 /* peek at the byte without really hitting the bus */
2070 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2071#else
2072 {
2073 int len = 1;
2074 unsigned char *data = msg;
2075 unsigned char phase = PHASE_MSGIN;
2076
2077 NCR5380_transfer_pio(instance, &phase, &len, &data);
2078
2079 if (len) {
2080 do_abort(instance);
2081 return;
2082 }
Finn Thain72064a72016-01-03 16:05:44 +11002083 }
Finn Thaine9db3192016-03-23 21:10:21 +11002084#endif /* CONFIG_SUN3 */
Finn Thain72064a72016-01-03 16:05:44 +11002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002087 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002088 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002089 printk("\n");
2090 do_abort(instance);
2091 return;
2092 }
2093 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Finn Thain72064a72016-01-03 16:05:44 +11002095 /*
2096 * We need to add code for SCSI-II to track which devices have
2097 * I_T_L_Q nexuses established, and which have simple I_T_L
2098 * nexuses so we can chose to do additional data transfer.
2099 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Finn Thain72064a72016-01-03 16:05:44 +11002101 /*
2102 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2103 * just reestablished, and remove it from the disconnected queue.
2104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Finn Thain32b26a12016-01-03 16:05:58 +11002106 tmp = NULL;
2107 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2108 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2109
2110 if (target_mask == (1 << scmd_id(cmd)) &&
2111 lun == (u8)cmd->device->lun) {
2112 list_del(&ncmd->list);
2113 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
2116 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002117
2118 if (tmp) {
2119 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2120 "reselect: removed %p from disconnected queue\n", tmp);
2121 } else {
Finn Thain45ddc1b2018-09-27 11:17:11 +10002122 int target = ffs(target_mask) - 1;
2123
Finn Thain72064a72016-01-03 16:05:44 +11002124 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2125 target_mask, lun);
2126 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002127 * Since we have an established nexus that we can't do anything
2128 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002129 */
Finn Thain45ddc1b2018-09-27 11:17:11 +10002130 if (do_abort(instance) == 0)
2131 hostdata->busy[target] &= ~(1 << lun);
Finn Thain72064a72016-01-03 16:05:44 +11002132 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
Finn Thain72064a72016-01-03 16:05:44 +11002134
Finn Thaine9db3192016-03-23 21:10:21 +11002135#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04002136 if (sun3_dma_setup_done != tmp) {
2137 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11002138
Finn Thain0e9fdd22019-06-18 09:37:57 +08002139 advance_sg_buffer(tmp);
Finn Thaine9db3192016-03-23 21:10:21 +11002140
Finn Thain4a98f892016-10-10 00:46:53 -04002141 count = sun3scsi_dma_xfer_len(hostdata, tmp);
2142
2143 if (count > 0) {
2144 if (rq_data_dir(tmp->request))
2145 sun3scsi_dma_send_setup(hostdata,
2146 tmp->SCp.ptr, count);
2147 else
2148 sun3scsi_dma_recv_setup(hostdata,
2149 tmp->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11002150 sun3_dma_setup_done = tmp;
2151 }
2152 }
2153
2154 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2155#endif /* CONFIG_SUN3 */
2156
Finn Thain72064a72016-01-03 16:05:44 +11002157 /* Accept message by clearing ACK */
2158 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2159
2160 hostdata->connected = tmp;
Finn Thainc4ec6f92016-03-23 21:10:22 +11002161 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2162 scmd_id(tmp), tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
Finn Thain8b00c3d2016-01-03 16:06:01 +11002165/**
2166 * list_find_cmd - test for presence of a command in a linked list
2167 * @haystack: list of commands
2168 * @needle: command to search for
2169 */
2170
2171static bool list_find_cmd(struct list_head *haystack,
2172 struct scsi_cmnd *needle)
2173{
2174 struct NCR5380_cmd *ncmd;
2175
2176 list_for_each_entry(ncmd, haystack, list)
2177 if (NCR5380_to_scmd(ncmd) == needle)
2178 return true;
2179 return false;
2180}
2181
2182/**
2183 * list_remove_cmd - remove a command from linked list
2184 * @haystack: list of commands
2185 * @needle: command to remove
2186 */
2187
2188static bool list_del_cmd(struct list_head *haystack,
2189 struct scsi_cmnd *needle)
2190{
2191 if (list_find_cmd(haystack, needle)) {
2192 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2193
2194 list_del(&ncmd->list);
2195 return true;
2196 }
2197 return false;
2198}
2199
2200/**
2201 * NCR5380_abort - scsi host eh_abort_handler() method
2202 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002204 * Try to abort a given command by removing it from queues and/or sending
2205 * the target an abort message. This may not succeed in causing a target
2206 * to abort the command. Nonetheless, the low-level driver must forget about
2207 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002209 * The normal path taken by a command is as follows. For EH we trace this
2210 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002212 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2213 * [disconnected -> connected ->]...
2214 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002216 * If cmd was not found at all then presumably it has already been completed,
2217 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002218 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002219 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002220 * We have no option but to forget the aborted command (even if it still
2221 * lacks sense data). The mid-layer may re-issue a command that is in error
2222 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2223 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002224 *
2225 * The lock protects driver data structures, but EH handlers also use it
2226 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 */
2228
Finn Thain710ddd02014-11-12 16:12:02 +11002229static int NCR5380_abort(struct scsi_cmnd *cmd)
2230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002232 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002233 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002234 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002235
Finn Thain11d2f632016-01-03 16:05:51 +11002236 spin_lock_irqsave(&hostdata->lock, flags);
2237
Finn Thain32b26a12016-01-03 16:05:58 +11002238#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002239 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002240#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002241 NCR5380_dprint(NDEBUG_ANY, instance);
2242 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Finn Thain8b00c3d2016-01-03 16:06:01 +11002244 if (list_del_cmd(&hostdata->unissued, cmd)) {
2245 dsprintk(NDEBUG_ABORT, instance,
2246 "abort: removed %p from issue queue\n", cmd);
2247 cmd->result = DID_ABORT << 16;
2248 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002249 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002250 }
2251
Finn Thain707d62b2016-01-03 16:06:02 +11002252 if (hostdata->selecting == cmd) {
2253 dsprintk(NDEBUG_ABORT, instance,
2254 "abort: cmd %p == selecting\n", cmd);
2255 hostdata->selecting = NULL;
2256 cmd->result = DID_ABORT << 16;
2257 complete_cmd(instance, cmd);
2258 goto out;
2259 }
2260
Finn Thain8b00c3d2016-01-03 16:06:01 +11002261 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2262 dsprintk(NDEBUG_ABORT, instance,
2263 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002264 /* Can't call NCR5380_select() and send ABORT because that
2265 * means releasing the lock. Need a bus reset.
2266 */
Finn Thaindc183962016-02-23 10:07:07 +11002267 set_host_byte(cmd, DID_ERROR);
2268 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002269 result = FAILED;
2270 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002271 }
2272
2273 if (hostdata->connected == cmd) {
2274 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2275 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002276 hostdata->dma_len = 0;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002277 if (do_abort(instance)) {
2278 set_host_byte(cmd, DID_ERROR);
2279 complete_cmd(instance, cmd);
2280 result = FAILED;
2281 goto out;
2282 }
2283 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002284 complete_cmd(instance, cmd);
2285 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002286 }
2287
Finn Thaindc183962016-02-23 10:07:07 +11002288 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002289 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002290 "abort: removed %p from sense queue\n", cmd);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002291 complete_cmd(instance, cmd);
2292 }
2293
2294out:
2295 if (result == FAILED)
2296 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
Finn Thain45ddc1b2018-09-27 11:17:11 +10002297 else {
2298 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002299 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
Finn Thain45ddc1b2018-09-27 11:17:11 +10002300 }
Finn Thain8b00c3d2016-01-03 16:06:01 +11002301
2302 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002303 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002304 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002305
Finn Thain8b00c3d2016-01-03 16:06:01 +11002306 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
2309
Finn Thain6b0e87a2018-09-27 11:17:11 +10002310static void bus_reset_cleanup(struct Scsi_Host *instance)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002311{
Finn Thain11d2f632016-01-03 16:05:51 +11002312 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002313 int i;
Finn Thain62717f52016-01-03 16:06:03 +11002314 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Finn Thain62717f52016-01-03 16:06:03 +11002316 /* reset NCR registers */
2317 NCR5380_write(MODE_REG, MR_BASE);
2318 NCR5380_write(TARGET_COMMAND_REG, 0);
2319 NCR5380_write(SELECT_ENABLE_REG, 0);
2320
2321 /* After the reset, there are no more connected or disconnected commands
2322 * and no busy units; so clear the low-level status here to avoid
2323 * conflicts when the mid-level code tries to wake up the affected
2324 * commands!
2325 */
2326
Finn Thain1884c282016-02-23 10:07:04 +11002327 if (hostdata->selecting) {
2328 hostdata->selecting->result = DID_RESET << 16;
2329 complete_cmd(instance, hostdata->selecting);
2330 hostdata->selecting = NULL;
2331 }
Finn Thain62717f52016-01-03 16:06:03 +11002332
2333 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2334 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2335
2336 set_host_byte(cmd, DID_RESET);
Finn Thain216fad92016-03-23 21:10:32 +11002337 complete_cmd(instance, cmd);
Finn Thain62717f52016-01-03 16:06:03 +11002338 }
Finn Thain1884c282016-02-23 10:07:04 +11002339 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002340
2341 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2342 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2343
Finn Thain62717f52016-01-03 16:06:03 +11002344 cmd->scsi_done(cmd);
2345 }
Finn Thain1884c282016-02-23 10:07:04 +11002346 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002347
2348 if (hostdata->connected) {
2349 set_host_byte(hostdata->connected, DID_RESET);
2350 complete_cmd(instance, hostdata->connected);
2351 hostdata->connected = NULL;
2352 }
2353
Finn Thain62717f52016-01-03 16:06:03 +11002354 for (i = 0; i < 8; ++i)
2355 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002356 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002357
2358 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002359 maybe_release_dma_irq(instance);
Finn Thain6b0e87a2018-09-27 11:17:11 +10002360}
2361
2362/**
2363 * NCR5380_host_reset - reset the SCSI host
2364 * @cmd: SCSI command undergoing EH
2365 *
2366 * Returns SUCCESS
2367 */
2368
2369static int NCR5380_host_reset(struct scsi_cmnd *cmd)
2370{
2371 struct Scsi_Host *instance = cmd->device->host;
2372 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2373 unsigned long flags;
2374 struct NCR5380_cmd *ncmd;
2375
2376 spin_lock_irqsave(&hostdata->lock, flags);
2377
2378#if (NDEBUG & NDEBUG_ANY)
2379 shost_printk(KERN_INFO, instance, __func__);
2380#endif
2381 NCR5380_dprint(NDEBUG_ANY, instance);
2382 NCR5380_dprint_phase(NDEBUG_ANY, instance);
2383
2384 list_for_each_entry(ncmd, &hostdata->unissued, list) {
2385 struct scsi_cmnd *scmd = NCR5380_to_scmd(ncmd);
2386
2387 scmd->result = DID_RESET << 16;
2388 scmd->scsi_done(scmd);
2389 }
2390 INIT_LIST_HEAD(&hostdata->unissued);
2391
2392 do_reset(instance);
2393 bus_reset_cleanup(instance);
2394
Finn Thain11d2f632016-01-03 16:05:51 +11002395 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return SUCCESS;
2398}